summaryrefslogtreecommitdiff
path: root/target/linux/ixp4xx/patches-2.6.32/020-gateworks_i2c_pld.patch
blob: a09a3becec4816762edb32e87eba1c9eff2dcc9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
--- /dev/null
+++ b/drivers/gpio/gw_i2c_pld.c
@@ -0,0 +1,371 @@
+/*
+ * Gateworks I2C PLD GPIO expander
+ *
+ * Copyright (C) 2009 Gateworks Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/hardirq.h>
+#include <linux/i2c.h>
+#include <linux/i2c/gw_i2c_pld.h>
+#include <asm/gpio.h>
+
+static const struct i2c_device_id gw_i2c_pld_id[] = {
+	{ "gw_i2c_pld", 8 },
+	{ }
+};
+MODULE_DEVICE_TABLE(i2c, gw_i2c_pld_id);
+
+/*
+ * The Gateworks I2C PLD chip only expose one read and one
+ * write register.  Writing a "one" bit (to match the reset state) lets
+ * that pin be used as an input. It is an open-drain model.
+ */
+
+struct gw_i2c_pld {
+	struct gpio_chip	chip;
+	struct i2c_client	*client;
+	unsigned		out;		/* software latch */
+};
+
+/*-------------------------------------------------------------------------*/
+
+/*
+ * The Gateworks I2C PLD chip does not properly send the acknowledge bit
+ * thus we cannot use standard i2c_smbus functions. We have recreated
+ * our own here, but we still use the mutex_lock to lock the i2c_bus
+ * as the device still exists on the I2C bus.
+*/
+
+#define PLD_SCL_GPIO 6
+#define PLD_SDA_GPIO 7
+
+#define SCL_LO()  gpio_line_set(PLD_SCL_GPIO, IXP4XX_GPIO_LOW)
+#define SCL_HI()  gpio_line_set(PLD_SCL_GPIO, IXP4XX_GPIO_HIGH)
+#define SCL_EN()  gpio_line_config(PLD_SCL_GPIO, IXP4XX_GPIO_OUT)
+#define SDA_LO()  gpio_line_set(PLD_SDA_GPIO, IXP4XX_GPIO_LOW)
+#define SDA_HI()  gpio_line_set(PLD_SDA_GPIO, IXP4XX_GPIO_HIGH)
+#define SDA_EN()  gpio_line_config(PLD_SDA_GPIO, IXP4XX_GPIO_OUT)
+#define SDA_DIS() gpio_line_config(PLD_SDA_GPIO, IXP4XX_GPIO_IN)
+#define SDA_IN(x) gpio_line_get(PLD_SDA_GPIO, &x);
+
+static int i2c_pld_write_byte(int address, int byte)
+{
+	int i;
+
+	address = (address << 1) & ~0x1;
+
+	SDA_HI();
+	SDA_EN();
+	SCL_EN();
+	SCL_HI();
+	SDA_LO();
+	SCL_LO();
+
+	for (i = 7; i >= 0; i--)
+	{
+		if (address & (1 << i))
+			SDA_HI();
+		else
+			SDA_LO();
+
+		SCL_HI();
+		SCL_LO();
+	}
+
+	SDA_DIS();
+	SCL_HI();
+	SDA_IN(i);
+	SCL_LO();
+	SDA_EN();
+
+	for (i = 7; i >= 0; i--)
+	{
+		if (byte & (1 << i))
+      SDA_HI();
+		else
+			SDA_LO();
+		SCL_HI();
+		SCL_LO();
+	}
+
+	SDA_DIS();
+	SCL_HI();
+	SDA_IN(i);
+	SCL_LO();
+
+	SDA_HI();
+	SDA_EN();
+
+	SDA_LO();
+	SCL_HI();
+	SDA_HI();
+	SCL_LO();
+	SCL_HI();
+
+	return 0;
+}
+
+static unsigned int i2c_pld_read_byte(int address)
+{
+	int i = 0, byte = 0;
+	int bit;
+
+	address = (address << 1) | 0x1;
+
+	SDA_HI();
+	SDA_EN();
+	SCL_EN();
+	SCL_HI();
+	SDA_LO();
+	SCL_LO();
+
+	for (i = 7; i >= 0; i--)
+	{
+		if (address & (1 << i))
+			SDA_HI();
+		else
+			SDA_LO();
+
+		SCL_HI();
+		SCL_LO();
+	}
+
+	SDA_DIS();
+	SCL_HI();
+	SDA_IN(i);
+	SCL_LO();
+	SDA_EN();
+
+	SDA_DIS();
+	for (i = 7; i >= 0; i--)
+	{
+		SCL_HI();
+		SDA_IN(bit);
+		byte |= bit << i;
+		SCL_LO();
+	}
+
+	SDA_LO();
+	SCL_HI();
+	SDA_HI();
+	SCL_LO();
+	SCL_HI();
+
+	return byte;
+}
+
+
+static int gw_i2c_pld_input8(struct gpio_chip *chip, unsigned offset)
+{
+	int ret;
+	struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
+	struct i2c_adapter *adap = gpio->client->adapter;
+	
+	if (in_atomic() || irqs_disabled()) {
+		ret = mutex_trylock(&adap->bus_lock);
+		if (!ret)
+			/* I2C activity is ongoing. */
+			return -EAGAIN;
+	} else {
+		mutex_lock_nested(&adap->bus_lock, adap->level);
+	}
+
+	gpio->out |= (1 << offset);
+
+	ret = i2c_pld_write_byte(gpio->client->addr, gpio->out);
+
+	mutex_unlock(&adap->bus_lock);
+
+	return ret;
+}
+
+static int gw_i2c_pld_get8(struct gpio_chip *chip, unsigned offset)
+{
+	int ret;
+	s32	value;
+	struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
+	struct i2c_adapter *adap = gpio->client->adapter;
+	
+	if (in_atomic() || irqs_disabled()) {
+		ret = mutex_trylock(&adap->bus_lock);
+		if (!ret)
+			/* I2C activity is ongoing. */
+			return -EAGAIN;
+	} else {
+		mutex_lock_nested(&adap->bus_lock, adap->level);
+	}
+
+	value = i2c_pld_read_byte(gpio->client->addr);
+
+	mutex_unlock(&adap->bus_lock);
+
+	return (value < 0) ? 0 : (value & (1 << offset));
+}
+
+static int gw_i2c_pld_output8(struct gpio_chip *chip, unsigned offset, int value)
+{
+	int ret;
+
+	struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
+	struct i2c_adapter *adap = gpio->client->adapter;
+
+	unsigned bit = 1 << offset;
+	
+	if (in_atomic() || irqs_disabled()) {
+		ret = mutex_trylock(&adap->bus_lock);
+		if (!ret)
+			/* I2C activity is ongoing. */
+			return -EAGAIN;
+	} else {
+		mutex_lock_nested(&adap->bus_lock, adap->level);
+	}
+
+
+	if (value)
+		gpio->out |= bit;
+	else
+		gpio->out &= ~bit;
+
+	ret = i2c_pld_write_byte(gpio->client->addr, gpio->out);
+
+	mutex_unlock(&adap->bus_lock);
+
+	return ret;
+}
+
+static void gw_i2c_pld_set8(struct gpio_chip *chip, unsigned offset, int value)
+{
+	gw_i2c_pld_output8(chip, offset, value);
+}
+
+/*-------------------------------------------------------------------------*/
+
+static int gw_i2c_pld_probe(struct i2c_client *client,
+			 const struct i2c_device_id *id)
+{
+	struct gw_i2c_pld_platform_data	*pdata;
+	struct gw_i2c_pld *gpio;
+	int status;
+
+	pdata = client->dev.platform_data;
+	if (!pdata)
+		return -ENODEV;
+
+	/* Allocate, initialize, and register this gpio_chip. */
+	gpio = kzalloc(sizeof *gpio, GFP_KERNEL);
+	if (!gpio)
+		return -ENOMEM;
+
+	gpio->chip.base = pdata->gpio_base;
+	gpio->chip.can_sleep = 1;
+	gpio->chip.dev = &client->dev;
+	gpio->chip.owner = THIS_MODULE;
+
+	gpio->chip.ngpio = pdata->nr_gpio;
+	gpio->chip.direction_input = gw_i2c_pld_input8;
+	gpio->chip.get = gw_i2c_pld_get8;
+	gpio->chip.direction_output = gw_i2c_pld_output8;
+	gpio->chip.set = gw_i2c_pld_set8;
+
+	gpio->chip.label = client->name;
+
+	gpio->client = client;
+	i2c_set_clientdata(client, gpio);
+
+	gpio->out = 0xFF;
+
+	status = gpiochip_add(&gpio->chip);
+	if (status < 0)
+		goto fail;
+
+	dev_info(&client->dev, "gpios %d..%d on a %s%s\n",
+			gpio->chip.base,
+			gpio->chip.base + gpio->chip.ngpio - 1,
+			client->name,
+			client->irq ? " (irq ignored)" : "");
+
+	/* Let platform code set up the GPIOs and their users.
+	 * Now is the first time anyone could use them.
+	 */
+	if (pdata->setup) {
+		status = pdata->setup(client,
+				gpio->chip.base, gpio->chip.ngpio,
+				pdata->context);
+		if (status < 0)
+			dev_warn(&client->dev, "setup --> %d\n", status);
+	}
+
+	return 0;
+
+fail:
+	dev_dbg(&client->dev, "probe error %d for '%s'\n",
+			status, client->name);
+	kfree(gpio);
+	return status;
+}
+
+static int gw_i2c_pld_remove(struct i2c_client *client)
+{
+	struct gw_i2c_pld_platform_data	*pdata = client->dev.platform_data;
+	struct gw_i2c_pld *gpio = i2c_get_clientdata(client);
+	int				status = 0;
+
+	if (pdata->teardown) {
+		status = pdata->teardown(client,
+				gpio->chip.base, gpio->chip.ngpio,
+				pdata->context);
+		if (status < 0) {
+			dev_err(&client->dev, "%s --> %d\n",
+					"teardown", status);
+			return status;
+		}
+	}
+
+	status = gpiochip_remove(&gpio->chip);
+	if (status == 0)
+		kfree(gpio);
+	else
+		dev_err(&client->dev, "%s --> %d\n", "remove", status);
+	return status;
+}
+
+static struct i2c_driver gw_i2c_pld_driver = {
+	.driver = {
+		.name	= "gw_i2c_pld",
+		.owner	= THIS_MODULE,
+	},
+	.probe	= gw_i2c_pld_probe,
+	.remove	= gw_i2c_pld_remove,
+	.id_table = gw_i2c_pld_id,
+};
+
+static int __init gw_i2c_pld_init(void)
+{
+	return i2c_add_driver(&gw_i2c_pld_driver);
+}
+module_init(gw_i2c_pld_init);
+
+static void __exit gw_i2c_pld_exit(void)
+{
+	i2c_del_driver(&gw_i2c_pld_driver);
+}
+module_exit(gw_i2c_pld_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Chris Lang");
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -196,6 +196,14 @@ config GPIO_LANGWELL
 	help
 	  Say Y here to support Intel Moorestown platform GPIO.
 
+config GPIO_GW_I2C_PLD
+	tristate "Gateworks I2C PLD GPIO Expander"
+	depends on I2C
+	help
+		Say yes here to provide access to the Gateworks I2C PLD GPIO
+		Expander. This is used at least on the GW2358-4.
+
+
 comment "SPI GPIO expanders:"
 
 config GPIO_MAX7301
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -19,3 +19,4 @@ obj-$(CONFIG_GPIO_XILINX)	+= xilinx_gpio
 obj-$(CONFIG_GPIO_BT8XX)	+= bt8xxgpio.o
 obj-$(CONFIG_GPIO_VR41XX)	+= vr41xx_giu.o
 obj-$(CONFIG_GPIO_WM831X)	+= wm831x-gpio.o
+obj-$(CONFIG_GPIO_GW_I2C_PLD)	+= gw_i2c_pld.o
--- /dev/null
+++ b/include/linux/i2c/gw_i2c_pld.h
@@ -0,0 +1,20 @@
+#ifndef __LINUX_GW_I2C_PLD_H
+#define __LINUX_GW_I2C_PLD_H
+
+/**
+ * The Gateworks I2C PLD Implements an additional 8 bits of GPIO through the PLD
+ */
+
+struct gw_i2c_pld_platform_data {
+	unsigned gpio_base;
+	unsigned nr_gpio;
+	int		(*setup)(struct i2c_client *client,
+					int gpio, unsigned ngpio,
+					void *context);
+	int		(*teardown)(struct i2c_client *client,
+					int gpio, unsigned ngpio,
+					void *context);
+	void		*context;
+};
+
+#endif /* __LINUX_GW_I2C_PLD_H */