]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/gpio/pca953x.c
i2c: Add support for device alias names
[mirror_ubuntu-zesty-kernel.git] / drivers / gpio / pca953x.c
CommitLineData
9e60fdcf 1/*
f5e8ff48 2 * pca953x.c - 4/8/16 bit I/O ports
9e60fdcf 3 *
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>
d1c057e3 17#include <linux/i2c/pca953x.h>
9e60fdcf 18
19#include <asm/gpio.h>
20
f5e8ff48
GL
21#define PCA953X_INPUT 0
22#define PCA953X_OUTPUT 1
23#define PCA953X_INVERT 2
24#define PCA953X_DIRECTION 3
9e60fdcf 25
f5e8ff48
GL
26/* 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};
9e60fdcf 31
f5e8ff48
GL
32static 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};
9e60fdcf 41
f3dc3630 42struct pca953x_chip {
9e60fdcf 43 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 */
f3dc3630 54static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
9e60fdcf 55{
f5e8ff48
GL
56 int ret;
57
58 if (chip->gpio_chip.ngpio <= 8)
59 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
9e60fdcf 60 else
f5e8ff48
GL
61 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;
9e60fdcf 69}
70
f3dc3630 71static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
9e60fdcf 72{
73 int ret;
74
f5e8ff48
GL
75 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
9e60fdcf 80 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
f3dc3630 89static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
9e60fdcf 90{
f3dc3630 91 struct pca953x_chip *chip;
9e60fdcf 92 uint16_t reg_val;
93 int ret;
94
f3dc3630 95 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 96
97 reg_val = chip->reg_direction | (1u << off);
f3dc3630 98 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
9e60fdcf 99 if (ret)
100 return ret;
101
102 chip->reg_direction = reg_val;
103 return 0;
104}
105
f3dc3630 106static int pca953x_gpio_direction_output(struct gpio_chip *gc,
9e60fdcf 107 unsigned off, int val)
108{
f3dc3630 109 struct pca953x_chip *chip;
9e60fdcf 110 uint16_t reg_val;
111 int ret;
112
f3dc3630 113 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 114
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
f3dc3630 121 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
9e60fdcf 122 if (ret)
123 return ret;
124
125 chip->reg_output = reg_val;
126
127 /* then direction */
128 reg_val = chip->reg_direction & ~(1u << off);
f3dc3630 129 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
9e60fdcf 130 if (ret)
131 return ret;
132
133 chip->reg_direction = reg_val;
134 return 0;
135}
136
f3dc3630 137static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
9e60fdcf 138{
f3dc3630 139 struct pca953x_chip *chip;
9e60fdcf 140 uint16_t reg_val;
141 int ret;
142
f3dc3630 143 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 144
f3dc3630 145 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
9e60fdcf 146 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
f3dc3630 157static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
9e60fdcf 158{
f3dc3630 159 struct pca953x_chip *chip;
9e60fdcf 160 uint16_t reg_val;
161 int ret;
162
f3dc3630 163 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 164
165 if (val)
166 reg_val = chip->reg_output | (1u << off);
167 else
168 reg_val = chip->reg_output & ~(1u << off);
169
f3dc3630 170 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
9e60fdcf 171 if (ret)
172 return;
173
174 chip->reg_output = reg_val;
175}
176
f5e8ff48 177static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
9e60fdcf 178{
179 struct gpio_chip *gc;
180
181 gc = &chip->gpio_chip;
182
f3dc3630
GL
183 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;
84207805 187 gc->can_sleep = 1;
9e60fdcf 188
189 gc->base = chip->gpio_start;
f5e8ff48
GL
190 gc->ngpio = gpios;
191 gc->label = chip->client->name;
d72cbed0 192 gc->owner = THIS_MODULE;
9e60fdcf 193}
194
d2653e92
JD
195static int __devinit pca953x_probe(struct i2c_client *client,
196 const struct i2c_device_id *did)
9e60fdcf 197{
f3dc3630
GL
198 struct pca953x_platform_data *pdata;
199 struct pca953x_chip *chip;
f5e8ff48
GL
200 int ret, i;
201 const struct pca953x_desc *id = NULL;
9e60fdcf 202
203 pdata = client->dev.platform_data;
204 if (pdata == NULL)
205 return -ENODEV;
206
f5e8ff48
GL
207 /* this loop vanishes when we get i2c_device_id */
208 for (i = 0; i < ARRAY_SIZE(pca953x_descs); i++)
209 if (!strcmp(pca953x_descs[i].name, client->name)) {
210 id = pca953x_descs + i;
211 break;
212 }
213 if (!id)
214 return -ENODEV;
215
f3dc3630 216 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
9e60fdcf 217 if (chip == NULL)
218 return -ENOMEM;
219
220 chip->client = client;
221
222 chip->gpio_start = pdata->gpio_base;
223
224 /* initialize cached registers from their original values.
225 * we can't share this chip with another i2c master.
226 */
f5e8ff48
GL
227 pca953x_setup_gpio(chip, id->driver_data);
228
f3dc3630 229 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
9e60fdcf 230 if (ret)
231 goto out_failed;
232
f3dc3630 233 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
9e60fdcf 234 if (ret)
235 goto out_failed;
236
237 /* set platform specific polarity inversion */
f3dc3630 238 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
9e60fdcf 239 if (ret)
240 goto out_failed;
241
f5e8ff48
GL
242
243 ret = gpiochip_add(&chip->gpio_chip);
9e60fdcf 244 if (ret)
245 goto out_failed;
246
247 if (pdata->setup) {
248 ret = pdata->setup(client, chip->gpio_chip.base,
249 chip->gpio_chip.ngpio, pdata->context);
250 if (ret < 0)
251 dev_warn(&client->dev, "setup failed, %d\n", ret);
252 }
253
254 i2c_set_clientdata(client, chip);
255 return 0;
256
257out_failed:
258 kfree(chip);
259 return ret;
260}
261
f3dc3630 262static int pca953x_remove(struct i2c_client *client)
9e60fdcf 263{
f3dc3630
GL
264 struct pca953x_platform_data *pdata = client->dev.platform_data;
265 struct pca953x_chip *chip = i2c_get_clientdata(client);
9e60fdcf 266 int ret = 0;
267
268 if (pdata->teardown) {
269 ret = pdata->teardown(client, chip->gpio_chip.base,
270 chip->gpio_chip.ngpio, pdata->context);
271 if (ret < 0) {
272 dev_err(&client->dev, "%s failed, %d\n",
273 "teardown", ret);
274 return ret;
275 }
276 }
277
278 ret = gpiochip_remove(&chip->gpio_chip);
279 if (ret) {
280 dev_err(&client->dev, "%s failed, %d\n",
281 "gpiochip_remove()", ret);
282 return ret;
283 }
284
285 kfree(chip);
286 return 0;
287}
288
f3dc3630 289static struct i2c_driver pca953x_driver = {
9e60fdcf 290 .driver = {
f3dc3630 291 .name = "pca953x",
9e60fdcf 292 },
f3dc3630
GL
293 .probe = pca953x_probe,
294 .remove = pca953x_remove,
9e60fdcf 295};
296
f3dc3630 297static int __init pca953x_init(void)
9e60fdcf 298{
f3dc3630 299 return i2c_add_driver(&pca953x_driver);
9e60fdcf 300}
f3dc3630 301module_init(pca953x_init);
9e60fdcf 302
f3dc3630 303static void __exit pca953x_exit(void)
9e60fdcf 304{
f3dc3630 305 i2c_del_driver(&pca953x_driver);
9e60fdcf 306}
f3dc3630 307module_exit(pca953x_exit);
9e60fdcf 308
309MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
f3dc3630 310MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
9e60fdcf 311MODULE_LICENSE("GPL");