blob: 8dc0164bd51e036af7ae4986644957cf6a4c8b94 [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
Jean Delvare3760f732008-04-29 23:11:40 +020026static const struct i2c_device_id pca953x_id[] = {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080027 { "pca9534", 8, },
28 { "pca9535", 16, },
29 { "pca9536", 4, },
30 { "pca9537", 4, },
31 { "pca9538", 8, },
32 { "pca9539", 16, },
David Brownell69292b32008-05-23 13:04:42 -070033 { "pca9554", 8, },
Will Newtonf39e5782008-05-01 04:35:10 -070034 { "pca9555", 16, },
35 { "pca9557", 8, },
David Brownellab5dc3722009-01-06 14:42:27 -080036
David Brownell7059d4b2008-07-04 09:59:37 -070037 { "max7310", 8, },
David Brownellab5dc3722009-01-06 14:42:27 -080038 { "pca6107", 8, },
39 { "tca6408", 8, },
40 { "tca6416", 16, },
41 /* NYET: { "tca6424", 24, }, */
Jean Delvare3760f732008-04-29 23:11:40 +020042 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080043};
Jean Delvare3760f732008-04-29 23:11:40 +020044MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080045
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080046struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080047 unsigned gpio_start;
48 uint16_t reg_output;
49 uint16_t reg_direction;
50
51 struct i2c_client *client;
52 struct gpio_chip gpio_chip;
53};
54
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080055static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
eric miao9e60fdc2008-02-04 22:28:26 -080056{
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080057 int ret;
58
59 if (chip->gpio_chip.ngpio <= 8)
60 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
eric miao9e60fdc2008-02-04 22:28:26 -080061 else
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080062 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
63
64 if (ret < 0) {
65 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc3722009-01-06 14:42:27 -080066 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080067 }
68
69 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -080070}
71
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080072static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
eric miao9e60fdc2008-02-04 22:28:26 -080073{
74 int ret;
75
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080076 if (chip->gpio_chip.ngpio <= 8)
77 ret = i2c_smbus_read_byte_data(chip->client, reg);
78 else
79 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
80
eric miao9e60fdc2008-02-04 22:28:26 -080081 if (ret < 0) {
82 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc3722009-01-06 14:42:27 -080083 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -080084 }
85
86 *val = (uint16_t)ret;
87 return 0;
88}
89
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080090static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -080091{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080092 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -080093 uint16_t reg_val;
94 int ret;
95
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080096 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -080097
98 reg_val = chip->reg_direction | (1u << off);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080099 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800100 if (ret)
101 return ret;
102
103 chip->reg_direction = reg_val;
104 return 0;
105}
106
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800107static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800108 unsigned off, int val)
109{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800110 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800111 uint16_t reg_val;
112 int ret;
113
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800114 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800115
116 /* set output level */
117 if (val)
118 reg_val = chip->reg_output | (1u << off);
119 else
120 reg_val = chip->reg_output & ~(1u << off);
121
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800122 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800123 if (ret)
124 return ret;
125
126 chip->reg_output = reg_val;
127
128 /* then direction */
129 reg_val = chip->reg_direction & ~(1u << off);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800130 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800131 if (ret)
132 return ret;
133
134 chip->reg_direction = reg_val;
135 return 0;
136}
137
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800138static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800139{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800140 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800141 uint16_t reg_val;
142 int ret;
143
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800144 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800145
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800146 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800147 if (ret < 0) {
148 /* NOTE: diagnostic already emitted; that's all we should
149 * do unless gpio_*_value_cansleep() calls become different
150 * from their nonsleeping siblings (and report faults).
151 */
152 return 0;
153 }
154
155 return (reg_val & (1u << off)) ? 1 : 0;
156}
157
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800158static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800159{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800160 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800161 uint16_t reg_val;
162 int ret;
163
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800164 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800165
166 if (val)
167 reg_val = chip->reg_output | (1u << off);
168 else
169 reg_val = chip->reg_output & ~(1u << off);
170
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800171 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800172 if (ret)
173 return;
174
175 chip->reg_output = reg_val;
176}
177
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800178static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800179{
180 struct gpio_chip *gc;
181
182 gc = &chip->gpio_chip;
183
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800184 gc->direction_input = pca953x_gpio_direction_input;
185 gc->direction_output = pca953x_gpio_direction_output;
186 gc->get = pca953x_gpio_get_value;
187 gc->set = pca953x_gpio_set_value;
Arnaud Patard84207802008-03-10 11:43:48 -0700188 gc->can_sleep = 1;
eric miao9e60fdc2008-02-04 22:28:26 -0800189
190 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800191 gc->ngpio = gpios;
192 gc->label = chip->client->name;
David Brownelld8f388d82008-07-25 01:46:07 -0700193 gc->dev = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700194 gc->owner = THIS_MODULE;
eric miao9e60fdc2008-02-04 22:28:26 -0800195}
196
Jean Delvared2653e92008-04-29 23:11:39 +0200197static int __devinit pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200198 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800199{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800200 struct pca953x_platform_data *pdata;
201 struct pca953x_chip *chip;
Will Newtonf39e5782008-05-01 04:35:10 -0700202 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800203
204 pdata = client->dev.platform_data;
Ben Dooksa342d212009-01-15 13:50:45 -0800205 if (pdata == NULL) {
206 dev_dbg(&client->dev, "no platform data\n");
207 return -EINVAL;
208 }
eric miao9e60fdc2008-02-04 22:28:26 -0800209
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800210 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800211 if (chip == NULL)
212 return -ENOMEM;
213
214 chip->client = client;
215
216 chip->gpio_start = pdata->gpio_base;
217
218 /* initialize cached registers from their original values.
219 * we can't share this chip with another i2c master.
220 */
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800221 pca953x_setup_gpio(chip, id->driver_data);
222
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800223 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
eric miao9e60fdc2008-02-04 22:28:26 -0800224 if (ret)
225 goto out_failed;
226
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800227 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
eric miao9e60fdc2008-02-04 22:28:26 -0800228 if (ret)
229 goto out_failed;
230
231 /* set platform specific polarity inversion */
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800232 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
eric miao9e60fdc2008-02-04 22:28:26 -0800233 if (ret)
234 goto out_failed;
235
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800236
237 ret = gpiochip_add(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800238 if (ret)
239 goto out_failed;
240
241 if (pdata->setup) {
242 ret = pdata->setup(client, chip->gpio_chip.base,
243 chip->gpio_chip.ngpio, pdata->context);
244 if (ret < 0)
245 dev_warn(&client->dev, "setup failed, %d\n", ret);
246 }
247
248 i2c_set_clientdata(client, chip);
249 return 0;
250
251out_failed:
252 kfree(chip);
253 return ret;
254}
255
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800256static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800257{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800258 struct pca953x_platform_data *pdata = client->dev.platform_data;
259 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800260 int ret = 0;
261
262 if (pdata->teardown) {
263 ret = pdata->teardown(client, chip->gpio_chip.base,
264 chip->gpio_chip.ngpio, pdata->context);
265 if (ret < 0) {
266 dev_err(&client->dev, "%s failed, %d\n",
267 "teardown", ret);
268 return ret;
269 }
270 }
271
272 ret = gpiochip_remove(&chip->gpio_chip);
273 if (ret) {
274 dev_err(&client->dev, "%s failed, %d\n",
275 "gpiochip_remove()", ret);
276 return ret;
277 }
278
279 kfree(chip);
280 return 0;
281}
282
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800283static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800284 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800285 .name = "pca953x",
eric miao9e60fdc2008-02-04 22:28:26 -0800286 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800287 .probe = pca953x_probe,
288 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200289 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800290};
291
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800292static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800293{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800294 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800295}
David Brownell2f8d1192008-10-15 22:03:13 -0700296/* register after i2c postcore initcall and before
297 * subsys initcalls that may rely on these GPIOs
298 */
299subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800300
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800301static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800302{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800303 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800304}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800305module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800306
307MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800308MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800309MODULE_LICENSE("GPL");