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>
21 #define PCA953X_INPUT 0
22 #define PCA953X_OUTPUT 1
23 #define PCA953X_INVERT 2
24 #define PCA953X_DIRECTION 3
26 static const struct i2c_device_id pca953x_id
[] = {
35 /* REVISIT several pca955x parts should work here too */
38 MODULE_DEVICE_TABLE(i2c
, pca953x_id
);
43 uint16_t reg_direction
;
45 struct i2c_client
*client
;
46 struct gpio_chip gpio_chip
;
49 /* NOTE: we can't currently rely on fault codes to come from SMBus
50 * calls, so we map all errors to EIO here and return zero otherwise.
52 static int pca953x_write_reg(struct pca953x_chip
*chip
, int reg
, uint16_t val
)
56 if (chip
->gpio_chip
.ngpio
<= 8)
57 ret
= i2c_smbus_write_byte_data(chip
->client
, reg
, val
);
59 ret
= i2c_smbus_write_word_data(chip
->client
, reg
<< 1, val
);
62 dev_err(&chip
->client
->dev
, "failed writing register\n");
69 static int pca953x_read_reg(struct pca953x_chip
*chip
, int reg
, uint16_t *val
)
73 if (chip
->gpio_chip
.ngpio
<= 8)
74 ret
= i2c_smbus_read_byte_data(chip
->client
, reg
);
76 ret
= i2c_smbus_read_word_data(chip
->client
, reg
<< 1);
79 dev_err(&chip
->client
->dev
, "failed reading register\n");
87 static int pca953x_gpio_direction_input(struct gpio_chip
*gc
, unsigned off
)
89 struct pca953x_chip
*chip
;
93 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
95 reg_val
= chip
->reg_direction
| (1u << off
);
96 ret
= pca953x_write_reg(chip
, PCA953X_DIRECTION
, reg_val
);
100 chip
->reg_direction
= reg_val
;
104 static int pca953x_gpio_direction_output(struct gpio_chip
*gc
,
105 unsigned off
, int val
)
107 struct pca953x_chip
*chip
;
111 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
113 /* set output level */
115 reg_val
= chip
->reg_output
| (1u << off
);
117 reg_val
= chip
->reg_output
& ~(1u << off
);
119 ret
= pca953x_write_reg(chip
, PCA953X_OUTPUT
, reg_val
);
123 chip
->reg_output
= reg_val
;
126 reg_val
= chip
->reg_direction
& ~(1u << off
);
127 ret
= pca953x_write_reg(chip
, PCA953X_DIRECTION
, reg_val
);
131 chip
->reg_direction
= reg_val
;
135 static int pca953x_gpio_get_value(struct gpio_chip
*gc
, unsigned off
)
137 struct pca953x_chip
*chip
;
141 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
143 ret
= pca953x_read_reg(chip
, PCA953X_INPUT
, ®_val
);
145 /* NOTE: diagnostic already emitted; that's all we should
146 * do unless gpio_*_value_cansleep() calls become different
147 * from their nonsleeping siblings (and report faults).
152 return (reg_val
& (1u << off
)) ? 1 : 0;
155 static void pca953x_gpio_set_value(struct gpio_chip
*gc
, unsigned off
, int val
)
157 struct pca953x_chip
*chip
;
161 chip
= container_of(gc
, struct pca953x_chip
, gpio_chip
);
164 reg_val
= chip
->reg_output
| (1u << off
);
166 reg_val
= chip
->reg_output
& ~(1u << off
);
168 ret
= pca953x_write_reg(chip
, PCA953X_OUTPUT
, reg_val
);
172 chip
->reg_output
= reg_val
;
175 static void pca953x_setup_gpio(struct pca953x_chip
*chip
, int gpios
)
177 struct gpio_chip
*gc
;
179 gc
= &chip
->gpio_chip
;
181 gc
->direction_input
= pca953x_gpio_direction_input
;
182 gc
->direction_output
= pca953x_gpio_direction_output
;
183 gc
->get
= pca953x_gpio_get_value
;
184 gc
->set
= pca953x_gpio_set_value
;
187 gc
->base
= chip
->gpio_start
;
189 gc
->label
= chip
->client
->name
;
190 gc
->owner
= THIS_MODULE
;
193 static int __devinit
pca953x_probe(struct i2c_client
*client
,
194 const struct i2c_device_id
*id
)
196 struct pca953x_platform_data
*pdata
;
197 struct pca953x_chip
*chip
;
200 pdata
= client
->dev
.platform_data
;
204 chip
= kzalloc(sizeof(struct pca953x_chip
), GFP_KERNEL
);
208 chip
->client
= client
;
210 chip
->gpio_start
= pdata
->gpio_base
;
212 /* initialize cached registers from their original values.
213 * we can't share this chip with another i2c master.
215 pca953x_setup_gpio(chip
, id
->driver_data
);
217 ret
= pca953x_read_reg(chip
, PCA953X_OUTPUT
, &chip
->reg_output
);
221 ret
= pca953x_read_reg(chip
, PCA953X_DIRECTION
, &chip
->reg_direction
);
225 /* set platform specific polarity inversion */
226 ret
= pca953x_write_reg(chip
, PCA953X_INVERT
, pdata
->invert
);
231 ret
= gpiochip_add(&chip
->gpio_chip
);
236 ret
= pdata
->setup(client
, chip
->gpio_chip
.base
,
237 chip
->gpio_chip
.ngpio
, pdata
->context
);
239 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
242 i2c_set_clientdata(client
, chip
);
250 static int pca953x_remove(struct i2c_client
*client
)
252 struct pca953x_platform_data
*pdata
= client
->dev
.platform_data
;
253 struct pca953x_chip
*chip
= i2c_get_clientdata(client
);
256 if (pdata
->teardown
) {
257 ret
= pdata
->teardown(client
, chip
->gpio_chip
.base
,
258 chip
->gpio_chip
.ngpio
, pdata
->context
);
260 dev_err(&client
->dev
, "%s failed, %d\n",
266 ret
= gpiochip_remove(&chip
->gpio_chip
);
268 dev_err(&client
->dev
, "%s failed, %d\n",
269 "gpiochip_remove()", ret
);
277 static struct i2c_driver pca953x_driver
= {
281 .probe
= pca953x_probe
,
282 .remove
= pca953x_remove
,
283 .id_table
= pca953x_id
,
286 static int __init
pca953x_init(void)
288 return i2c_add_driver(&pca953x_driver
);
290 module_init(pca953x_init
);
292 static void __exit
pca953x_exit(void)
294 i2c_del_driver(&pca953x_driver
);
296 module_exit(pca953x_exit
);
298 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
299 MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
300 MODULE_LICENSE("GPL");