2 +++ b/drivers/gpio/gw_i2c_pld.c
5 + * Gateworks I2C PLD GPIO expander
7 + * Copyright (C) 2009 Gateworks Corporation
9 + * This program is free software; you can redistribute it and/or modify
10 + * it under the terms of the GNU General Public License as published by
11 + * the Free Software Foundation; either version 2 of the License, or
12 + * (at your option) any later version.
14 + * This program is distributed in the hope that it will be useful,
15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 + * GNU General Public License for more details.
19 + * You should have received a copy of the GNU General Public License
20 + * along with this program; if not, write to the Free Software
21 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 +#include <linux/kernel.h>
25 +#include <linux/slab.h>
26 +#include <linux/hardirq.h>
27 +#include <linux/i2c.h>
28 +#include <linux/i2c/gw_i2c_pld.h>
29 +#include <linux/module.h>
30 +#include <linux/export.h>
31 +#include <asm/gpio.h>
33 +static const struct i2c_device_id gw_i2c_pld_id[] = {
34 + { "gw_i2c_pld", 8 },
37 +MODULE_DEVICE_TABLE(i2c, gw_i2c_pld_id);
40 + * The Gateworks I2C PLD chip only expose one read and one
41 + * write register. Writing a "one" bit (to match the reset state) lets
42 + * that pin be used as an input. It is an open-drain model.
46 + struct gpio_chip chip;
47 + struct i2c_client *client;
48 + unsigned out; /* software latch */
51 +/*-------------------------------------------------------------------------*/
54 + * The Gateworks I2C PLD chip does not properly send the acknowledge bit
55 + * thus we cannot use standard i2c_smbus functions. We have recreated
56 + * our own here, but we still use the rt_mutex_lock to lock the i2c_bus
57 + * as the device still exists on the I2C bus.
60 +#define PLD_SCL_GPIO 6
61 +#define PLD_SDA_GPIO 7
63 +#define SCL_LO() gpio_line_set(PLD_SCL_GPIO, IXP4XX_GPIO_LOW)
64 +#define SCL_HI() gpio_line_set(PLD_SCL_GPIO, IXP4XX_GPIO_HIGH)
65 +#define SCL_EN() gpio_line_config(PLD_SCL_GPIO, IXP4XX_GPIO_OUT)
66 +#define SDA_LO() gpio_line_set(PLD_SDA_GPIO, IXP4XX_GPIO_LOW)
67 +#define SDA_HI() gpio_line_set(PLD_SDA_GPIO, IXP4XX_GPIO_HIGH)
68 +#define SDA_EN() gpio_line_config(PLD_SDA_GPIO, IXP4XX_GPIO_OUT)
69 +#define SDA_DIS() gpio_line_config(PLD_SDA_GPIO, IXP4XX_GPIO_IN)
70 +#define SDA_IN(x) gpio_line_get(PLD_SDA_GPIO, &x);
72 +static int i2c_pld_write_byte(int address, int byte)
76 + address = (address << 1) & ~0x1;
85 + for (i = 7; i >= 0; i--)
87 + if (address & (1 << i))
102 + for (i = 7; i >= 0; i--)
104 + if (byte & (1 << i))
129 +static unsigned int i2c_pld_read_byte(int address)
131 + int i = 0, byte = 0;
134 + address = (address << 1) | 0x1;
143 + for (i = 7; i >= 0; i--)
145 + if (address & (1 << i))
161 + for (i = 7; i >= 0; i--)
179 +static int gw_i2c_pld_input8(struct gpio_chip *chip, unsigned offset)
182 + struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
183 + struct i2c_adapter *adap = gpio->client->adapter;
185 + if (in_atomic() || irqs_disabled()) {
186 + ret = rt_mutex_trylock(&adap->bus_lock);
188 + /* I2C activity is ongoing. */
191 + rt_mutex_lock(&adap->bus_lock);
194 + gpio->out |= (1 << offset);
196 + ret = i2c_pld_write_byte(gpio->client->addr, gpio->out);
198 + rt_mutex_unlock(&adap->bus_lock);
203 +static int gw_i2c_pld_get8(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 + value = i2c_pld_read_byte(gpio->client->addr);
221 + rt_mutex_unlock(&adap->bus_lock);
223 + return (value < 0) ? 0 : (value & (1 << offset));
226 +static int gw_i2c_pld_output8(struct gpio_chip *chip, unsigned offset, int value)
230 + struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
231 + struct i2c_adapter *adap = gpio->client->adapter;
233 + unsigned bit = 1 << offset;
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);
250 + ret = i2c_pld_write_byte(gpio->client->addr, gpio->out);
252 + rt_mutex_unlock(&adap->bus_lock);
257 +static void gw_i2c_pld_set8(struct gpio_chip *chip, unsigned offset, int value)
259 + gw_i2c_pld_output8(chip, offset, value);
262 +/*-------------------------------------------------------------------------*/
264 +static int gw_i2c_pld_probe(struct i2c_client *client,
265 + const struct i2c_device_id *id)
267 + struct gw_i2c_pld_platform_data *pdata;
268 + struct gw_i2c_pld *gpio;
271 + pdata = client->dev.platform_data;
275 + /* Allocate, initialize, and register this gpio_chip. */
276 + gpio = kzalloc(sizeof *gpio, GFP_KERNEL);
280 + gpio->chip.base = pdata->gpio_base;
281 + gpio->chip.can_sleep = 1;
282 + gpio->chip.dev = &client->dev;
283 + gpio->chip.owner = THIS_MODULE;
285 + gpio->chip.ngpio = pdata->nr_gpio;
286 + gpio->chip.direction_input = gw_i2c_pld_input8;
287 + gpio->chip.get = gw_i2c_pld_get8;
288 + gpio->chip.direction_output = gw_i2c_pld_output8;
289 + gpio->chip.set = gw_i2c_pld_set8;
291 + gpio->chip.label = client->name;
293 + gpio->client = client;
294 + i2c_set_clientdata(client, gpio);
298 + status = gpiochip_add(&gpio->chip);
302 + dev_info(&client->dev, "gpios %d..%d on a %s%s\n",
304 + gpio->chip.base + gpio->chip.ngpio - 1,
306 + client->irq ? " (irq ignored)" : "");
308 + /* Let platform code set up the GPIOs and their users.
309 + * Now is the first time anyone could use them.
311 + if (pdata->setup) {
312 + status = pdata->setup(client,
313 + gpio->chip.base, gpio->chip.ngpio,
316 + dev_warn(&client->dev, "setup --> %d\n", status);
322 + dev_dbg(&client->dev, "probe error %d for '%s'\n",
323 + status, client->name);
328 +static int gw_i2c_pld_remove(struct i2c_client *client)
330 + struct gw_i2c_pld_platform_data *pdata = client->dev.platform_data;
331 + struct gw_i2c_pld *gpio = i2c_get_clientdata(client);
334 + if (pdata->teardown) {
335 + status = pdata->teardown(client,
336 + gpio->chip.base, gpio->chip.ngpio,
339 + dev_err(&client->dev, "%s --> %d\n",
340 + "teardown", status);
345 + status = gpiochip_remove(&gpio->chip);
349 + dev_err(&client->dev, "%s --> %d\n", "remove", status);
353 +static struct i2c_driver gw_i2c_pld_driver = {
355 + .name = "gw_i2c_pld",
356 + .owner = THIS_MODULE,
358 + .probe = gw_i2c_pld_probe,
359 + .remove = gw_i2c_pld_remove,
360 + .id_table = gw_i2c_pld_id,
363 +static int __init gw_i2c_pld_init(void)
365 + return i2c_add_driver(&gw_i2c_pld_driver);
367 +module_init(gw_i2c_pld_init);
369 +static void __exit gw_i2c_pld_exit(void)
371 + i2c_del_driver(&gw_i2c_pld_driver);
373 +module_exit(gw_i2c_pld_exit);
375 +MODULE_LICENSE("GPL");
376 +MODULE_AUTHOR("Chris Lang");
377 --- a/drivers/gpio/Kconfig
378 +++ b/drivers/gpio/Kconfig
379 @@ -426,6 +426,14 @@ config GPIO_RDC321X
380 Support for the RDC R321x SoC GPIOs over southbridge
381 PCI configuration space.
383 +config GPIO_GW_I2C_PLD
384 + tristate "Gateworks I2C PLD GPIO Expander"
387 + Say yes here to provide access to the Gateworks I2C PLD GPIO
388 + Expander. This is used at least on the GW2358-4.
391 comment "SPI GPIO expanders:"
394 --- a/drivers/gpio/Makefile
395 +++ b/drivers/gpio/Makefile
396 @@ -61,3 +61,4 @@ obj-$(CONFIG_GPIO_WM831X) += gpio-wm831x
397 obj-$(CONFIG_GPIO_WM8350) += gpio-wm8350.o
398 obj-$(CONFIG_GPIO_WM8994) += gpio-wm8994.o
399 obj-$(CONFIG_GPIO_XILINX) += gpio-xilinx.o
400 +obj-$(CONFIG_GPIO_GW_I2C_PLD) += gw_i2c_pld.o
402 +++ b/include/linux/i2c/gw_i2c_pld.h
404 +#ifndef __LINUX_GW_I2C_PLD_H
405 +#define __LINUX_GW_I2C_PLD_H
408 + * The Gateworks I2C PLD Implements an additional 8 bits of GPIO through the PLD
411 +struct gw_i2c_pld_platform_data {
412 + unsigned gpio_base;
414 + int (*setup)(struct i2c_client *client,
415 + int gpio, unsigned ngpio,
417 + int (*teardown)(struct i2c_client *client,
418 + int gpio, unsigned ngpio,
423 +#endif /* __LINUX_GW_I2C_PLD_H */