2 * pca953x.c - 4/8/16 bit I/O ports
4 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
5 * Copyright (C) 2007 Marvell International Ltd.
7 * Derived from drivers/i2c/chips/pca9539.c
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; version 2 of the License.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/i2c.h>
17 #include <linux/i2c/pca953x.h>
19 #include <linux/of_platform.h>
20 #include <linux/of_gpio.h>
25 #define PCA953X_INPUT 0
26 #define PCA953X_OUTPUT 1
27 #define PCA953X_INVERT 2
28 #define PCA953X_DIRECTION 3
30 static const struct i2c_device_id pca953x_id
[] = {
46 /* NYET: { "tca6424", 24, }, */
49 MODULE_DEVICE_TABLE(i2c
, pca953x_id
);
54 uint16_t reg_direction
;
56 struct i2c_client
*client
;
57 struct pca953x_platform_data
*dyn_pdata
;
58 struct gpio_chip gpio_chip
;
62 static int pca953x_write_reg(struct pca953x_chip
*chip
, int reg
, uint16_t val
)
66 if (chip
->gpio_chip
.ngpio
<= 8)
67 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
, val
);
69 ret
= i2c_smbus_write_word_data(chip
->client
, reg
<< 1, val
);
72 dev_err(&chip
->client
->dev
, "failed writing register\n");
79 static int pca953x_read_reg(struct pca953x_chip
*chip
, int reg
, uint16_t *val
)
83 if (chip
->gpio_chip
.ngpio
<= 8)
84 ret
= i2c_smbus_read_byte_data(chip
->client
, reg
);
86 ret
= i2c_smbus_read_word_data(chip
->client
, reg
<< 1);
89 dev_err(&chip
->client
->dev
, "failed reading register\n");
97 static int pca953x_gpio_direction_input(struct gpio_chip
*gc
, unsigned off
)
99 struct pca953x_chip
*chip
;
103 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
105 reg_val
= chip
->reg_direction
| (1u << off
);
106 ret
= pca953x_write_reg(chip
, PCA953X_DIRECTION
, reg_val
);
110 chip
->reg_direction
= reg_val
;
114 static int pca953x_gpio_direction_output(struct gpio_chip
*gc
,
115 unsigned off
, int val
)
117 struct pca953x_chip
*chip
;
121 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
123 /* set output level */
125 reg_val
= chip
->reg_output
| (1u << off
);
127 reg_val
= chip
->reg_output
& ~(1u << off
);
129 ret
= pca953x_write_reg(chip
, PCA953X_OUTPUT
, reg_val
);
133 chip
->reg_output
= reg_val
;
136 reg_val
= chip
->reg_direction
& ~(1u << off
);
137 ret
= pca953x_write_reg(chip
, PCA953X_DIRECTION
, reg_val
);
141 chip
->reg_direction
= reg_val
;
145 static int pca953x_gpio_get_value(struct gpio_chip
*gc
, unsigned off
)
147 struct pca953x_chip
*chip
;
151 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
153 ret
= pca953x_read_reg(chip
, PCA953X_INPUT
, ®_val
);
155 /* NOTE: diagnostic already emitted; that's all we should
156 * do unless gpio_*_value_cansleep() calls become different
157 * from their nonsleeping siblings (and report faults).
162 return (reg_val
& (1u << off
)) ? 1 : 0;
165 static void pca953x_gpio_set_value(struct gpio_chip
*gc
, unsigned off
, int val
)
167 struct pca953x_chip
*chip
;
171 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
174 reg_val
= chip
->reg_output
| (1u << off
);
176 reg_val
= chip
->reg_output
& ~(1u << off
);
178 ret
= pca953x_write_reg(chip
, PCA953X_OUTPUT
, reg_val
);
182 chip
->reg_output
= reg_val
;
185 static void pca953x_setup_gpio(struct pca953x_chip
*chip
, int gpios
)
187 struct gpio_chip
*gc
;
189 gc
= &chip
->gpio_chip
;
191 gc
->direction_input
= pca953x_gpio_direction_input
;
192 gc
->direction_output
= pca953x_gpio_direction_output
;
193 gc
->get
= pca953x_gpio_get_value
;
194 gc
->set
= pca953x_gpio_set_value
;
197 gc
->base
= chip
->gpio_start
;
199 gc
->label
= chip
->client
->name
;
200 gc
->dev
= &chip
->client
->dev
;
201 gc
->owner
= THIS_MODULE
;
202 gc
->names
= chip
->names
;
206 * Handlers for alternative sources of platform_data
208 #ifdef CONFIG_OF_GPIO
210 * Translate OpenFirmware node properties into platform_data
212 static struct pca953x_platform_data
*
213 pca953x_get_alt_pdata(struct i2c_client
*client
)
215 struct pca953x_platform_data
*pdata
;
216 struct device_node
*node
;
219 node
= dev_archdata_get_node(&client
->dev
.archdata
);
223 pdata
= kzalloc(sizeof(struct pca953x_platform_data
), GFP_KERNEL
);
225 dev_err(&client
->dev
, "Unable to allocate platform_data\n");
229 pdata
->gpio_base
= -1;
230 val
= of_get_property(node
, "linux,gpio-base", NULL
);
233 dev_warn(&client
->dev
,
234 "invalid gpio-base in device tree\n");
236 pdata
->gpio_base
= *val
;
239 val
= of_get_property(node
, "polarity", NULL
);
241 pdata
->invert
= *val
;
246 static struct pca953x_platform_data
*
247 pca953x_get_alt_pdata(struct i2c_client
*client
)
253 static int __devinit
pca953x_probe(struct i2c_client
*client
,
254 const struct i2c_device_id
*id
)
256 struct pca953x_platform_data
*pdata
;
257 struct pca953x_chip
*chip
;
260 chip
= kzalloc(sizeof(struct pca953x_chip
), GFP_KERNEL
);
264 pdata
= client
->dev
.platform_data
;
266 pdata
= pca953x_get_alt_pdata(client
);
268 * Unlike normal platform_data, this is allocated
269 * dynamically and must be freed in the driver
271 chip
->dyn_pdata
= pdata
;
275 dev_dbg(&client
->dev
, "no platform data\n");
280 chip
->client
= client
;
282 chip
->gpio_start
= pdata
->gpio_base
;
284 chip
->names
= pdata
->names
;
286 /* initialize cached registers from their original values.
287 * we can't share this chip with another i2c master.
289 pca953x_setup_gpio(chip
, id
->driver_data
);
291 ret
= pca953x_read_reg(chip
, PCA953X_OUTPUT
, &chip
->reg_output
);
295 ret
= pca953x_read_reg(chip
, PCA953X_DIRECTION
, &chip
->reg_direction
);
299 /* set platform specific polarity inversion */
300 ret
= pca953x_write_reg(chip
, PCA953X_INVERT
, pdata
->invert
);
305 ret
= gpiochip_add(&chip
->gpio_chip
);
310 ret
= pdata
->setup(client
, chip
->gpio_chip
.base
,
311 chip
->gpio_chip
.ngpio
, pdata
->context
);
313 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
316 i2c_set_clientdata(client
, chip
);
320 kfree(chip
->dyn_pdata
);
325 static int pca953x_remove(struct i2c_client
*client
)
327 struct pca953x_platform_data
*pdata
= client
->dev
.platform_data
;
328 struct pca953x_chip
*chip
= i2c_get_clientdata(client
);
331 if (pdata
->teardown
) {
332 ret
= pdata
->teardown(client
, chip
->gpio_chip
.base
,
333 chip
->gpio_chip
.ngpio
, pdata
->context
);
335 dev_err(&client
->dev
, "%s failed, %d\n",
341 ret
= gpiochip_remove(&chip
->gpio_chip
);
343 dev_err(&client
->dev
, "%s failed, %d\n",
344 "gpiochip_remove()", ret
);
348 kfree(chip
->dyn_pdata
);
353 static struct i2c_driver pca953x_driver
= {
357 .probe
= pca953x_probe
,
358 .remove
= pca953x_remove
,
359 .id_table
= pca953x_id
,
362 static int __init
pca953x_init(void)
364 return i2c_add_driver(&pca953x_driver
);
366 /* register after i2c postcore initcall and before
367 * subsys initcalls that may rely on these GPIOs
369 subsys_initcall(pca953x_init
);
371 static void __exit
pca953x_exit(void)
373 i2c_del_driver(&pca953x_driver
);
375 module_exit(pca953x_exit
);
377 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
378 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
379 MODULE_LICENSE("GPL");