1 --- a/drivers/gpio/Kconfig
2 +++ b/drivers/gpio/Kconfig
3 @@ -722,6 +722,14 @@ config GPIO_RDC321X
4 Support for the RDC R321x SoC GPIOs over southbridge
5 PCI configuration space.
7 +config GPIO_GW_I2C_PLD
8 + tristate "Gateworks I2C PLD GPIO Expander"
11 + Say yes here to provide access to the Gateworks I2C PLD GPIO
12 + Expander. This is used at least on the GW2358-4.
15 comment "SPI GPIO expanders:"
18 --- a/drivers/gpio/Makefile
19 +++ b/drivers/gpio/Makefile
20 @@ -99,3 +99,4 @@ obj-$(CONFIG_GPIO_WM8350) += gpio-wm8350
21 obj-$(CONFIG_GPIO_WM8994) += gpio-wm8994.o
22 obj-$(CONFIG_GPIO_XILINX) += gpio-xilinx.o
23 obj-$(CONFIG_GPIO_XTENSA) += gpio-xtensa.o
24 +obj-$(CONFIG_GPIO_GW_I2C_PLD) += gw_i2c_pld.o
26 +++ b/drivers/gpio/gw_i2c_pld.c
29 + * Gateworks I2C PLD GPIO expander
31 + * Copyright (C) 2009 Gateworks Corporation
33 + * This program is free software; you can redistribute it and/or modify
34 + * it under the terms of the GNU General Public License as published by
35 + * the Free Software Foundation; either version 2 of the License, or
36 + * (at your option) any later version.
38 + * This program is distributed in the hope that it will be useful,
39 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 + * GNU General Public License for more details.
43 + * You should have received a copy of the GNU General Public License
44 + * along with this program; if not, write to the Free Software
45 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
48 +#include <linux/kernel.h>
49 +#include <linux/slab.h>
50 +#include <linux/hardirq.h>
51 +#include <linux/i2c.h>
52 +#include <linux/i2c/gw_i2c_pld.h>
53 +#include <linux/module.h>
54 +#include <linux/export.h>
55 +#include <asm/gpio.h>
56 +#include <mach/hardware.h>
58 +static const struct i2c_device_id gw_i2c_pld_id[] = {
59 + { "gw_i2c_pld", 8 },
62 +MODULE_DEVICE_TABLE(i2c, gw_i2c_pld_id);
65 + * The Gateworks I2C PLD chip only expose one read and one
66 + * write register. Writing a "one" bit (to match the reset state) lets
67 + * that pin be used as an input. It is an open-drain model.
71 + struct gpio_chip chip;
72 + struct i2c_client *client;
73 + unsigned out; /* software latch */
76 +/*-------------------------------------------------------------------------*/
79 + * The Gateworks I2C PLD chip does not properly send the acknowledge bit
80 + * thus we cannot use standard i2c_smbus functions. We have recreated
81 + * our own here, but we still use the rt_mutex_lock to lock the i2c_bus
82 + * as the device still exists on the I2C bus.
85 +#define PLD_SCL_GPIO 6
86 +#define PLD_SDA_GPIO 7
88 +#define SCL_LO() gpio_line_set(PLD_SCL_GPIO, IXP4XX_GPIO_LOW)
89 +#define SCL_HI() gpio_line_set(PLD_SCL_GPIO, IXP4XX_GPIO_HIGH)
90 +#define SCL_EN() gpio_line_config(PLD_SCL_GPIO, IXP4XX_GPIO_OUT)
91 +#define SDA_LO() gpio_line_set(PLD_SDA_GPIO, IXP4XX_GPIO_LOW)
92 +#define SDA_HI() gpio_line_set(PLD_SDA_GPIO, IXP4XX_GPIO_HIGH)
93 +#define SDA_EN() gpio_line_config(PLD_SDA_GPIO, IXP4XX_GPIO_OUT)
94 +#define SDA_DIS() gpio_line_config(PLD_SDA_GPIO, IXP4XX_GPIO_IN)
95 +#define SDA_IN(x) gpio_line_get(PLD_SDA_GPIO, &x);
97 +static int i2c_pld_write_byte(int address, int byte)
101 + address = (address << 1) & ~0x1;
110 + for (i = 7; i >= 0; i--)
112 + if (address & (1 << i))
127 + for (i = 7; i >= 0; i--)
129 + if (byte & (1 << i))
154 +static unsigned int i2c_pld_read_byte(int address)
156 + int i = 0, byte = 0;
159 + address = (address << 1) | 0x1;
168 + for (i = 7; i >= 0; i--)
170 + if (address & (1 << i))
186 + for (i = 7; i >= 0; i--)
204 +static int gw_i2c_pld_input8(struct gpio_chip *chip, unsigned offset)
207 + struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
208 + struct i2c_adapter *adap = gpio->client->adapter;
210 + if (in_atomic() || irqs_disabled()) {
211 + ret = rt_mutex_trylock(&adap->bus_lock);
213 + /* I2C activity is ongoing. */
216 + rt_mutex_lock(&adap->bus_lock);
219 + gpio->out |= (1 << offset);
221 + ret = i2c_pld_write_byte(gpio->client->addr, gpio->out);
223 + rt_mutex_unlock(&adap->bus_lock);
228 +static int gw_i2c_pld_get8(struct gpio_chip *chip, unsigned offset)
232 + struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
233 + struct i2c_adapter *adap = gpio->client->adapter;
235 + if (in_atomic() || irqs_disabled()) {
236 + ret = rt_mutex_trylock(&adap->bus_lock);
238 + /* I2C activity is ongoing. */
241 + rt_mutex_lock(&adap->bus_lock);
244 + value = i2c_pld_read_byte(gpio->client->addr);
246 + rt_mutex_unlock(&adap->bus_lock);
248 + return (value < 0) ? 0 : (value & (1 << offset));
251 +static int gw_i2c_pld_output8(struct gpio_chip *chip, unsigned offset, int value)
255 + struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
256 + struct i2c_adapter *adap = gpio->client->adapter;
258 + unsigned bit = 1 << offset;
260 + if (in_atomic() || irqs_disabled()) {
261 + ret = rt_mutex_trylock(&adap->bus_lock);
263 + /* I2C activity is ongoing. */
266 + rt_mutex_lock(&adap->bus_lock);
275 + ret = i2c_pld_write_byte(gpio->client->addr, gpio->out);
277 + rt_mutex_unlock(&adap->bus_lock);
282 +static void gw_i2c_pld_set8(struct gpio_chip *chip, unsigned offset, int value)
284 + gw_i2c_pld_output8(chip, offset, value);
287 +/*-------------------------------------------------------------------------*/
289 +static int gw_i2c_pld_probe(struct i2c_client *client,
290 + const struct i2c_device_id *id)
292 + struct gw_i2c_pld_platform_data *pdata;
293 + struct gw_i2c_pld *gpio;
296 + pdata = client->dev.platform_data;
300 + /* Allocate, initialize, and register this gpio_chip. */
301 + gpio = kzalloc(sizeof *gpio, GFP_KERNEL);
305 + gpio->chip.base = pdata->gpio_base;
306 + gpio->chip.can_sleep = 1;
307 + gpio->chip.dev = &client->dev;
308 + gpio->chip.owner = THIS_MODULE;
310 + gpio->chip.ngpio = pdata->nr_gpio;
311 + gpio->chip.direction_input = gw_i2c_pld_input8;
312 + gpio->chip.get = gw_i2c_pld_get8;
313 + gpio->chip.direction_output = gw_i2c_pld_output8;
314 + gpio->chip.set = gw_i2c_pld_set8;
316 + gpio->chip.label = client->name;
318 + gpio->client = client;
319 + i2c_set_clientdata(client, gpio);
323 + status = gpiochip_add(&gpio->chip);
327 + dev_info(&client->dev, "gpios %d..%d on a %s%s\n",
329 + gpio->chip.base + gpio->chip.ngpio - 1,
331 + client->irq ? " (irq ignored)" : "");
333 + /* Let platform code set up the GPIOs and their users.
334 + * Now is the first time anyone could use them.
336 + if (pdata->setup) {
337 + status = pdata->setup(client,
338 + gpio->chip.base, gpio->chip.ngpio,
341 + dev_warn(&client->dev, "setup --> %d\n", status);
347 + dev_dbg(&client->dev, "probe error %d for '%s'\n",
348 + status, client->name);
353 +static int gw_i2c_pld_remove(struct i2c_client *client)
355 + struct gw_i2c_pld_platform_data *pdata = client->dev.platform_data;
356 + struct gw_i2c_pld *gpio = i2c_get_clientdata(client);
359 + if (pdata->teardown) {
360 + status = pdata->teardown(client,
361 + gpio->chip.base, gpio->chip.ngpio,
364 + dev_err(&client->dev, "%s --> %d\n",
365 + "teardown", status);
370 + status = gpiochip_remove(&gpio->chip);
374 + dev_err(&client->dev, "%s --> %d\n", "remove", status);
378 +static struct i2c_driver gw_i2c_pld_driver = {
380 + .name = "gw_i2c_pld",
381 + .owner = THIS_MODULE,
383 + .probe = gw_i2c_pld_probe,
384 + .remove = gw_i2c_pld_remove,
385 + .id_table = gw_i2c_pld_id,
388 +static int __init gw_i2c_pld_init(void)
390 + return i2c_add_driver(&gw_i2c_pld_driver);
392 +module_init(gw_i2c_pld_init);
394 +static void __exit gw_i2c_pld_exit(void)
396 + i2c_del_driver(&gw_i2c_pld_driver);
398 +module_exit(gw_i2c_pld_exit);
400 +MODULE_LICENSE("GPL");
401 +MODULE_AUTHOR("Chris Lang");
403 +++ b/include/linux/i2c/gw_i2c_pld.h
405 +#ifndef __LINUX_GW_I2C_PLD_H
406 +#define __LINUX_GW_I2C_PLD_H
409 + * The Gateworks I2C PLD Implements an additional 8 bits of GPIO through the PLD
412 +struct gw_i2c_pld_platform_data {
413 + unsigned gpio_base;
415 + int (*setup)(struct i2c_client *client,
416 + int gpio, unsigned ngpio,
418 + int (*teardown)(struct i2c_client *client,
419 + int gpio, unsigned ngpio,
424 +#endif /* __LINUX_GW_I2C_PLD_H */