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/i2c.h>
27 +#include <linux/i2c/gw_i2c_pld.h>
28 +#include <asm/gpio.h>
30 +static const struct i2c_device_id gw_i2c_pld_id[] = {
31 + { "gw_i2c_pld", 8 },
34 +MODULE_DEVICE_TABLE(i2c, gw_i2c_pld_id);
37 + * The Gateworks I2C PLD chip only expose one read and one
38 + * write register. Writing a "one" bit (to match the reset state) lets
39 + * that pin be used as an input. It is an open-drain model.
43 + struct gpio_chip chip;
44 + struct i2c_client *client;
45 + unsigned out; /* software latch */
48 +/*-------------------------------------------------------------------------*/
51 + * The Gateworks I2C PLD chip does not properly send the acknowledge bit
52 + * thus we cannot use standard i2c_smbus functions. We have recreated
53 + * our own here, but we still use the mutex_lock to lock the i2c_bus
54 + * as the device still exists on the I2C bus.
57 +#define PLD_SCL_GPIO 6
58 +#define PLD_SDA_GPIO 7
60 +#define SCL_LO() gpio_line_set(PLD_SCL_GPIO, IXP4XX_GPIO_LOW)
61 +#define SCL_HI() gpio_line_set(PLD_SCL_GPIO, IXP4XX_GPIO_HIGH)
62 +#define SCL_EN() gpio_line_config(PLD_SCL_GPIO, IXP4XX_GPIO_OUT)
63 +#define SDA_LO() gpio_line_set(PLD_SDA_GPIO, IXP4XX_GPIO_LOW)
64 +#define SDA_HI() gpio_line_set(PLD_SDA_GPIO, IXP4XX_GPIO_HIGH)
65 +#define SDA_EN() gpio_line_config(PLD_SDA_GPIO, IXP4XX_GPIO_OUT)
66 +#define SDA_DIS() gpio_line_config(PLD_SDA_GPIO, IXP4XX_GPIO_IN)
67 +#define SDA_IN(x) gpio_line_get(PLD_SDA_GPIO, &x);
69 +static int i2c_pld_write_byte(int address, int byte)
73 + address = (address << 1) & ~0x1;
82 + for (i = 7; i >= 0; i--)
84 + if (address & (1 << i))
99 + for (i = 7; i >= 0; i--)
101 + if (byte & (1 << i))
126 +static unsigned int i2c_pld_read_byte(int address)
128 + int i = 0, byte = 0;
131 + address = (address << 1) | 0x1;
140 + for (i = 7; i >= 0; i--)
142 + if (address & (1 << i))
158 + for (i = 7; i >= 0; i--)
176 +static int gw_i2c_pld_input8(struct gpio_chip *chip, unsigned offset)
179 + struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
180 + struct i2c_adapter *adap = gpio->client->adapter;
182 + if (in_atomic() || irqs_disabled()) {
183 + ret = mutex_trylock(&adap->bus_lock);
185 + /* I2C activity is ongoing. */
188 + mutex_lock_nested(&adap->bus_lock, adap->level);
191 + gpio->out |= (1 << offset);
193 + ret = i2c_pld_write_byte(gpio->client->addr, gpio->out);
195 + mutex_unlock(&adap->bus_lock);
200 +static int gw_i2c_pld_get8(struct gpio_chip *chip, unsigned offset)
204 + struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
205 + struct i2c_adapter *adap = gpio->client->adapter;
207 + if (in_atomic() || irqs_disabled()) {
208 + ret = mutex_trylock(&adap->bus_lock);
210 + /* I2C activity is ongoing. */
213 + mutex_lock_nested(&adap->bus_lock, adap->level);
216 + value = i2c_pld_read_byte(gpio->client->addr);
218 + mutex_unlock(&adap->bus_lock);
220 + return (value < 0) ? 0 : (value & (1 << offset));
223 +static int gw_i2c_pld_output8(struct gpio_chip *chip, unsigned offset, int value)
227 + struct gw_i2c_pld *gpio = container_of(chip, struct gw_i2c_pld, chip);
228 + struct i2c_adapter *adap = gpio->client->adapter;
230 + unsigned bit = 1 << offset;
232 + if (in_atomic() || irqs_disabled()) {
233 + ret = mutex_trylock(&adap->bus_lock);
235 + /* I2C activity is ongoing. */
238 + mutex_lock_nested(&adap->bus_lock, adap->level);
247 + ret = i2c_pld_write_byte(gpio->client->addr, gpio->out);
249 + mutex_unlock(&adap->bus_lock);
254 +static void gw_i2c_pld_set8(struct gpio_chip *chip, unsigned offset, int value)
256 + gw_i2c_pld_output8(chip, offset, value);
259 +/*-------------------------------------------------------------------------*/
261 +static int gw_i2c_pld_probe(struct i2c_client *client,
262 + const struct i2c_device_id *id)
264 + struct gw_i2c_pld_platform_data *pdata;
265 + struct gw_i2c_pld *gpio;
268 + pdata = client->dev.platform_data;
272 + /* Allocate, initialize, and register this gpio_chip. */
273 + gpio = kzalloc(sizeof *gpio, GFP_KERNEL);
277 + gpio->chip.base = pdata->gpio_base;
278 + gpio->chip.can_sleep = 1;
279 + gpio->chip.dev = &client->dev;
280 + gpio->chip.owner = THIS_MODULE;
282 + gpio->chip.ngpio = pdata->nr_gpio;
283 + gpio->chip.direction_input = gw_i2c_pld_input8;
284 + gpio->chip.get = gw_i2c_pld_get8;
285 + gpio->chip.direction_output = gw_i2c_pld_output8;
286 + gpio->chip.set = gw_i2c_pld_set8;
288 + gpio->chip.label = client->name;
290 + gpio->client = client;
291 + i2c_set_clientdata(client, gpio);
295 + status = gpiochip_add(&gpio->chip);
299 + dev_info(&client->dev, "gpios %d..%d on a %s%s\n",
301 + gpio->chip.base + gpio->chip.ngpio - 1,
303 + client->irq ? " (irq ignored)" : "");
305 + /* Let platform code set up the GPIOs and their users.
306 + * Now is the first time anyone could use them.
308 + if (pdata->setup) {
309 + status = pdata->setup(client,
310 + gpio->chip.base, gpio->chip.ngpio,
313 + dev_warn(&client->dev, "setup --> %d\n", status);
319 + dev_dbg(&client->dev, "probe error %d for '%s'\n",
320 + status, client->name);
325 +static int gw_i2c_pld_remove(struct i2c_client *client)
327 + struct gw_i2c_pld_platform_data *pdata = client->dev.platform_data;
328 + struct gw_i2c_pld *gpio = i2c_get_clientdata(client);
331 + if (pdata->teardown) {
332 + status = pdata->teardown(client,
333 + gpio->chip.base, gpio->chip.ngpio,
336 + dev_err(&client->dev, "%s --> %d\n",
337 + "teardown", status);
342 + status = gpiochip_remove(&gpio->chip);
346 + dev_err(&client->dev, "%s --> %d\n", "remove", status);
350 +static struct i2c_driver gw_i2c_pld_driver = {
352 + .name = "gw_i2c_pld",
353 + .owner = THIS_MODULE,
355 + .probe = gw_i2c_pld_probe,
356 + .remove = gw_i2c_pld_remove,
357 + .id_table = gw_i2c_pld_id,
360 +static int __init gw_i2c_pld_init(void)
362 + return i2c_add_driver(&gw_i2c_pld_driver);
364 +module_init(gw_i2c_pld_init);
366 +static void __exit gw_i2c_pld_exit(void)
368 + i2c_del_driver(&gw_i2c_pld_driver);
370 +module_exit(gw_i2c_pld_exit);
372 +MODULE_LICENSE("GPL");
373 +MODULE_AUTHOR("Chris Lang");
374 --- a/drivers/gpio/Kconfig
375 +++ b/drivers/gpio/Kconfig
376 @@ -160,6 +160,14 @@ config GPIO_BT8XX
380 +config GPIO_GW_I2C_PLD
381 + tristate "Gateworks I2C PLD GPIO Expander"
384 + Say yes here to provide access to the Gateworks I2C PLD GPIO
385 + Expander. This is used at least on the GW2358-4.
388 comment "SPI GPIO expanders:"
391 --- a/drivers/gpio/Makefile
392 +++ b/drivers/gpio/Makefile
393 @@ -12,3 +12,4 @@ obj-$(CONFIG_GPIO_PCF857X) += pcf857x.o
394 obj-$(CONFIG_GPIO_TWL4030) += twl4030-gpio.o
395 obj-$(CONFIG_GPIO_XILINX) += xilinx_gpio.o
396 obj-$(CONFIG_GPIO_BT8XX) += bt8xxgpio.o
397 +obj-$(CONFIG_GPIO_GW_I2C_PLD) += gw_i2c_pld.o
399 +++ b/include/linux/i2c/gw_i2c_pld.h
401 +#ifndef __LINUX_GW_I2C_PLD_H
402 +#define __LINUX_GW_I2C_PLD_H
405 + * The Gateworks I2C PLD Implements an additional 8 bits of GPIO through the PLD
408 +struct gw_i2c_pld_platform_data {
409 + unsigned gpio_base;
411 + int (*setup)(struct i2c_client *client,
412 + int gpio, unsigned ngpio,
414 + int (*teardown)(struct i2c_client *client,
415 + int gpio, unsigned ngpio,
420 +#endif /* __LINUX_GW_I2C_PLD_H */