2 * pca9539.c - 16-bit I/O port with interrupt and reset
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/pca9539.h>
22 #define NR_PCA9539_GPIOS 16
24 #define PCA9539_INPUT 0
25 #define PCA9539_OUTPUT 2
26 #define PCA9539_INVERT 4
27 #define PCA9539_DIRECTION 6
32 uint16_t reg_direction
;
34 struct i2c_client
*client
;
35 struct gpio_chip gpio_chip
;
38 /* NOTE: we can't currently rely on fault codes to come from SMBus
39 * calls, so we map all errors to EIO here and return zero otherwise.
41 static int pca9539_write_reg(struct pca9539_chip
*chip
, int reg
, uint16_t val
)
43 if (i2c_smbus_write_word_data(chip
->client
, reg
, val
) < 0)
49 static int pca9539_read_reg(struct pca9539_chip
*chip
, int reg
, uint16_t *val
)
53 ret
= i2c_smbus_read_word_data(chip
->client
, reg
);
55 dev_err(&chip
->client
->dev
, "failed reading register\n");
63 static int pca9539_gpio_direction_input(struct gpio_chip
*gc
, unsigned off
)
65 struct pca9539_chip
*chip
;
69 chip
= container_of(gc
, struct pca9539_chip
, gpio_chip
);
71 reg_val
= chip
->reg_direction
| (1u << off
);
72 ret
= pca9539_write_reg(chip
, PCA9539_DIRECTION
, reg_val
);
76 chip
->reg_direction
= reg_val
;
80 static int pca9539_gpio_direction_output(struct gpio_chip
*gc
,
81 unsigned off
, int val
)
83 struct pca9539_chip
*chip
;
87 chip
= container_of(gc
, struct pca9539_chip
, gpio_chip
);
89 /* set output level */
91 reg_val
= chip
->reg_output
| (1u << off
);
93 reg_val
= chip
->reg_output
& ~(1u << off
);
95 ret
= pca9539_write_reg(chip
, PCA9539_OUTPUT
, reg_val
);
99 chip
->reg_output
= reg_val
;
102 reg_val
= chip
->reg_direction
& ~(1u << off
);
103 ret
= pca9539_write_reg(chip
, PCA9539_DIRECTION
, reg_val
);
107 chip
->reg_direction
= reg_val
;
111 static int pca9539_gpio_get_value(struct gpio_chip
*gc
, unsigned off
)
113 struct pca9539_chip
*chip
;
117 chip
= container_of(gc
, struct pca9539_chip
, gpio_chip
);
119 ret
= pca9539_read_reg(chip
, PCA9539_INPUT
, ®_val
);
121 /* NOTE: diagnostic already emitted; that's all we should
122 * do unless gpio_*_value_cansleep() calls become different
123 * from their nonsleeping siblings (and report faults).
128 return (reg_val
& (1u << off
)) ? 1 : 0;
131 static void pca9539_gpio_set_value(struct gpio_chip
*gc
, unsigned off
, int val
)
133 struct pca9539_chip
*chip
;
137 chip
= container_of(gc
, struct pca9539_chip
, gpio_chip
);
140 reg_val
= chip
->reg_output
| (1u << off
);
142 reg_val
= chip
->reg_output
& ~(1u << off
);
144 ret
= pca9539_write_reg(chip
, PCA9539_OUTPUT
, reg_val
);
148 chip
->reg_output
= reg_val
;
151 static int pca9539_init_gpio(struct pca9539_chip
*chip
)
153 struct gpio_chip
*gc
;
155 gc
= &chip
->gpio_chip
;
157 gc
->direction_input
= pca9539_gpio_direction_input
;
158 gc
->direction_output
= pca9539_gpio_direction_output
;
159 gc
->get
= pca9539_gpio_get_value
;
160 gc
->set
= pca9539_gpio_set_value
;
162 gc
->base
= chip
->gpio_start
;
163 gc
->ngpio
= NR_PCA9539_GPIOS
;
164 gc
->label
= "pca9539";
166 return gpiochip_add(gc
);
169 static int __devinit
pca9539_probe(struct i2c_client
*client
)
171 struct pca9539_platform_data
*pdata
;
172 struct pca9539_chip
*chip
;
175 pdata
= client
->dev
.platform_data
;
179 chip
= kzalloc(sizeof(struct pca9539_chip
), GFP_KERNEL
);
183 chip
->client
= client
;
185 chip
->gpio_start
= pdata
->gpio_base
;
187 /* initialize cached registers from their original values.
188 * we can't share this chip with another i2c master.
190 ret
= pca9539_read_reg(chip
, PCA9539_OUTPUT
, &chip
->reg_output
);
194 ret
= pca9539_read_reg(chip
, PCA9539_DIRECTION
, &chip
->reg_direction
);
198 /* set platform specific polarity inversion */
199 ret
= pca9539_write_reg(chip
, PCA9539_INVERT
, pdata
->invert
);
203 ret
= pca9539_init_gpio(chip
);
208 ret
= pdata
->setup(client
, chip
->gpio_chip
.base
,
209 chip
->gpio_chip
.ngpio
, pdata
->context
);
211 dev_warn(&client
->dev
, "setup failed, %d\n", ret
);
214 i2c_set_clientdata(client
, chip
);
222 static int pca9539_remove(struct i2c_client
*client
)
224 struct pca9539_platform_data
*pdata
= client
->dev
.platform_data
;
225 struct pca9539_chip
*chip
= i2c_get_clientdata(client
);
228 if (pdata
->teardown
) {
229 ret
= pdata
->teardown(client
, chip
->gpio_chip
.base
,
230 chip
->gpio_chip
.ngpio
, pdata
->context
);
232 dev_err(&client
->dev
, "%s failed, %d\n",
238 ret
= gpiochip_remove(&chip
->gpio_chip
);
240 dev_err(&client
->dev
, "%s failed, %d\n",
241 "gpiochip_remove()", ret
);
249 static struct i2c_driver pca9539_driver
= {
253 .probe
= pca9539_probe
,
254 .remove
= pca9539_remove
,
257 static int __init
pca9539_init(void)
259 return i2c_add_driver(&pca9539_driver
);
261 module_init(pca9539_init
);
263 static void __exit
pca9539_exit(void)
265 i2c_del_driver(&pca9539_driver
);
267 module_exit(pca9539_exit
);
269 MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
270 MODULE_DESCRIPTION("GPIO expander driver for PCA9539");
271 MODULE_LICENSE("GPL");