blob: e0e0af5361083e6cd9483b971ff427cab43dad80 [file] [log] [blame]
eric miao9e60fdc2008-02-04 22:28:26 -08001/*
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -08002 * pca953x.c - 4/8/16 bit I/O ports
eric miao9e60fdc2008-02-04 22:28:26 -08003 *
4 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
5 * Copyright (C) 2007 Marvell International Ltd.
6 *
7 * Derived from drivers/i2c/chips/pca9539.c
8 *
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.
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/i2c.h>
Guennadi Liakhovetskid1c057e32008-02-06 01:39:02 -080017#include <linux/i2c/pca953x.h>
eric miao9e60fdc2008-02-04 22:28:26 -080018
19#include <asm/gpio.h>
20
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080021#define PCA953X_INPUT 0
22#define PCA953X_OUTPUT 1
23#define PCA953X_INVERT 2
24#define PCA953X_DIRECTION 3
eric miao9e60fdc2008-02-04 22:28:26 -080025
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080026/* This is temporary - in 2.6.26 i2c_driver_data should replace it. */
27struct pca953x_desc {
28 char name[I2C_NAME_SIZE];
29 unsigned long driver_data;
30};
eric miao9e60fdc2008-02-04 22:28:26 -080031
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080032static const struct pca953x_desc pca953x_descs[] = {
33 { "pca9534", 8, },
34 { "pca9535", 16, },
35 { "pca9536", 4, },
36 { "pca9537", 4, },
37 { "pca9538", 8, },
38 { "pca9539", 16, },
39 /* REVISIT several pca955x parts should work here too */
40};
eric miao9e60fdc2008-02-04 22:28:26 -080041
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080042struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080043 unsigned gpio_start;
44 uint16_t reg_output;
45 uint16_t reg_direction;
46
47 struct i2c_client *client;
48 struct gpio_chip gpio_chip;
49};
50
51/* NOTE: we can't currently rely on fault codes to come from SMBus
52 * calls, so we map all errors to EIO here and return zero otherwise.
53 */
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080054static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
eric miao9e60fdc2008-02-04 22:28:26 -080055{
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080056 int ret;
57
58 if (chip->gpio_chip.ngpio <= 8)
59 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
eric miao9e60fdc2008-02-04 22:28:26 -080060 else
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080061 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
62
63 if (ret < 0) {
64 dev_err(&chip->client->dev, "failed writing register\n");
65 return -EIO;
66 }
67
68 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -080069}
70
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080071static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
eric miao9e60fdc2008-02-04 22:28:26 -080072{
73 int ret;
74
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080075 if (chip->gpio_chip.ngpio <= 8)
76 ret = i2c_smbus_read_byte_data(chip->client, reg);
77 else
78 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
79
eric miao9e60fdc2008-02-04 22:28:26 -080080 if (ret < 0) {
81 dev_err(&chip->client->dev, "failed reading register\n");
82 return -EIO;
83 }
84
85 *val = (uint16_t)ret;
86 return 0;
87}
88
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080089static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -080090{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080091 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -080092 uint16_t reg_val;
93 int ret;
94
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080095 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -080096
97 reg_val = chip->reg_direction | (1u << off);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080098 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -080099 if (ret)
100 return ret;
101
102 chip->reg_direction = reg_val;
103 return 0;
104}
105
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800106static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800107 unsigned off, int val)
108{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800109 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800110 uint16_t reg_val;
111 int ret;
112
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800113 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800114
115 /* set output level */
116 if (val)
117 reg_val = chip->reg_output | (1u << off);
118 else
119 reg_val = chip->reg_output & ~(1u << off);
120
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800121 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800122 if (ret)
123 return ret;
124
125 chip->reg_output = reg_val;
126
127 /* then direction */
128 reg_val = chip->reg_direction & ~(1u << off);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800129 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800130 if (ret)
131 return ret;
132
133 chip->reg_direction = reg_val;
134 return 0;
135}
136
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800137static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800138{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800139 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800140 uint16_t reg_val;
141 int ret;
142
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800143 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800144
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800145 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800146 if (ret < 0) {
147 /* NOTE: diagnostic already emitted; that's all we should
148 * do unless gpio_*_value_cansleep() calls become different
149 * from their nonsleeping siblings (and report faults).
150 */
151 return 0;
152 }
153
154 return (reg_val & (1u << off)) ? 1 : 0;
155}
156
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800157static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800158{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800159 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800160 uint16_t reg_val;
161 int ret;
162
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800163 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800164
165 if (val)
166 reg_val = chip->reg_output | (1u << off);
167 else
168 reg_val = chip->reg_output & ~(1u << off);
169
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800170 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800171 if (ret)
172 return;
173
174 chip->reg_output = reg_val;
175}
176
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800177static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800178{
179 struct gpio_chip *gc;
180
181 gc = &chip->gpio_chip;
182
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800183 gc->direction_input = pca953x_gpio_direction_input;
184 gc->direction_output = pca953x_gpio_direction_output;
185 gc->get = pca953x_gpio_get_value;
186 gc->set = pca953x_gpio_set_value;
Arnaud Patard84207802008-03-10 11:43:48 -0700187 gc->can_sleep = 1;
eric miao9e60fdc2008-02-04 22:28:26 -0800188
189 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800190 gc->ngpio = gpios;
191 gc->label = chip->client->name;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700192 gc->owner = THIS_MODULE;
eric miao9e60fdc2008-02-04 22:28:26 -0800193}
194
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800195static int __devinit pca953x_probe(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800196{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800197 struct pca953x_platform_data *pdata;
198 struct pca953x_chip *chip;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800199 int ret, i;
200 const struct pca953x_desc *id = NULL;
eric miao9e60fdc2008-02-04 22:28:26 -0800201
202 pdata = client->dev.platform_data;
203 if (pdata == NULL)
204 return -ENODEV;
205
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800206 /* this loop vanishes when we get i2c_device_id */
207 for (i = 0; i < ARRAY_SIZE(pca953x_descs); i++)
208 if (!strcmp(pca953x_descs[i].name, client->name)) {
209 id = pca953x_descs + i;
210 break;
211 }
212 if (!id)
213 return -ENODEV;
214
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800215 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800216 if (chip == NULL)
217 return -ENOMEM;
218
219 chip->client = client;
220
221 chip->gpio_start = pdata->gpio_base;
222
223 /* initialize cached registers from their original values.
224 * we can't share this chip with another i2c master.
225 */
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800226 pca953x_setup_gpio(chip, id->driver_data);
227
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800228 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
eric miao9e60fdc2008-02-04 22:28:26 -0800229 if (ret)
230 goto out_failed;
231
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800232 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
eric miao9e60fdc2008-02-04 22:28:26 -0800233 if (ret)
234 goto out_failed;
235
236 /* set platform specific polarity inversion */
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800237 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
eric miao9e60fdc2008-02-04 22:28:26 -0800238 if (ret)
239 goto out_failed;
240
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800241
242 ret = gpiochip_add(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800243 if (ret)
244 goto out_failed;
245
246 if (pdata->setup) {
247 ret = pdata->setup(client, chip->gpio_chip.base,
248 chip->gpio_chip.ngpio, pdata->context);
249 if (ret < 0)
250 dev_warn(&client->dev, "setup failed, %d\n", ret);
251 }
252
253 i2c_set_clientdata(client, chip);
254 return 0;
255
256out_failed:
257 kfree(chip);
258 return ret;
259}
260
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800261static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800262{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800263 struct pca953x_platform_data *pdata = client->dev.platform_data;
264 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800265 int ret = 0;
266
267 if (pdata->teardown) {
268 ret = pdata->teardown(client, chip->gpio_chip.base,
269 chip->gpio_chip.ngpio, pdata->context);
270 if (ret < 0) {
271 dev_err(&client->dev, "%s failed, %d\n",
272 "teardown", ret);
273 return ret;
274 }
275 }
276
277 ret = gpiochip_remove(&chip->gpio_chip);
278 if (ret) {
279 dev_err(&client->dev, "%s failed, %d\n",
280 "gpiochip_remove()", ret);
281 return ret;
282 }
283
284 kfree(chip);
285 return 0;
286}
287
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800288static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800289 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800290 .name = "pca953x",
eric miao9e60fdc2008-02-04 22:28:26 -0800291 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800292 .probe = pca953x_probe,
293 .remove = pca953x_remove,
eric miao9e60fdc2008-02-04 22:28:26 -0800294};
295
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800296static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800297{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800298 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800299}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800300module_init(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800301
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800302static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800303{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800304 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800305}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800306module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800307
308MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800309MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800310MODULE_LICENSE("GPL");