blob: 93f916720b139e40b20591839126609db0e778a0 [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, },
Will Newtonf39e5782008-05-01 04:35:10 -070033 { "pca9555", 16, },
34 { "pca9557", 8, },
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080035 /* REVISIT several pca955x parts should work here too */
Jean Delvare3760f732008-04-29 23:11:40 +020036 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080037};
Jean Delvare3760f732008-04-29 23:11:40 +020038MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080039
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080040struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080041 unsigned gpio_start;
42 uint16_t reg_output;
43 uint16_t reg_direction;
44
45 struct i2c_client *client;
46 struct gpio_chip gpio_chip;
47};
48
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.
51 */
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080052static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
eric miao9e60fdc2008-02-04 22:28:26 -080053{
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080054 int ret;
55
56 if (chip->gpio_chip.ngpio <= 8)
57 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
eric miao9e60fdc2008-02-04 22:28:26 -080058 else
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080059 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
60
61 if (ret < 0) {
62 dev_err(&chip->client->dev, "failed writing register\n");
63 return -EIO;
64 }
65
66 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -080067}
68
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080069static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
eric miao9e60fdc2008-02-04 22:28:26 -080070{
71 int ret;
72
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080073 if (chip->gpio_chip.ngpio <= 8)
74 ret = i2c_smbus_read_byte_data(chip->client, reg);
75 else
76 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
77
eric miao9e60fdc2008-02-04 22:28:26 -080078 if (ret < 0) {
79 dev_err(&chip->client->dev, "failed reading register\n");
80 return -EIO;
81 }
82
83 *val = (uint16_t)ret;
84 return 0;
85}
86
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080087static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -080088{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080089 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -080090 uint16_t reg_val;
91 int ret;
92
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080093 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -080094
95 reg_val = chip->reg_direction | (1u << off);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080096 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -080097 if (ret)
98 return ret;
99
100 chip->reg_direction = reg_val;
101 return 0;
102}
103
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800104static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800105 unsigned off, int val)
106{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800107 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800108 uint16_t reg_val;
109 int ret;
110
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800111 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800112
113 /* set output level */
114 if (val)
115 reg_val = chip->reg_output | (1u << off);
116 else
117 reg_val = chip->reg_output & ~(1u << off);
118
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800119 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800120 if (ret)
121 return ret;
122
123 chip->reg_output = reg_val;
124
125 /* then direction */
126 reg_val = chip->reg_direction & ~(1u << off);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800127 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800128 if (ret)
129 return ret;
130
131 chip->reg_direction = reg_val;
132 return 0;
133}
134
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800135static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800136{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800137 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800138 uint16_t reg_val;
139 int ret;
140
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800141 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800142
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800143 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800144 if (ret < 0) {
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).
148 */
149 return 0;
150 }
151
152 return (reg_val & (1u << off)) ? 1 : 0;
153}
154
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800155static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800156{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800157 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800158 uint16_t reg_val;
159 int ret;
160
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800161 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800162
163 if (val)
164 reg_val = chip->reg_output | (1u << off);
165 else
166 reg_val = chip->reg_output & ~(1u << off);
167
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800168 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800169 if (ret)
170 return;
171
172 chip->reg_output = reg_val;
173}
174
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800175static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800176{
177 struct gpio_chip *gc;
178
179 gc = &chip->gpio_chip;
180
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800181 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;
Arnaud Patard84207802008-03-10 11:43:48 -0700185 gc->can_sleep = 1;
eric miao9e60fdc2008-02-04 22:28:26 -0800186
187 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800188 gc->ngpio = gpios;
189 gc->label = chip->client->name;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700190 gc->owner = THIS_MODULE;
eric miao9e60fdc2008-02-04 22:28:26 -0800191}
192
Jean Delvared2653e92008-04-29 23:11:39 +0200193static int __devinit pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200194 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800195{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800196 struct pca953x_platform_data *pdata;
197 struct pca953x_chip *chip;
Will Newtonf39e5782008-05-01 04:35:10 -0700198 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800199
200 pdata = client->dev.platform_data;
201 if (pdata == NULL)
202 return -ENODEV;
203
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800204 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800205 if (chip == NULL)
206 return -ENOMEM;
207
208 chip->client = client;
209
210 chip->gpio_start = pdata->gpio_base;
211
212 /* initialize cached registers from their original values.
213 * we can't share this chip with another i2c master.
214 */
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800215 pca953x_setup_gpio(chip, id->driver_data);
216
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800217 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
eric miao9e60fdc2008-02-04 22:28:26 -0800218 if (ret)
219 goto out_failed;
220
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800221 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
eric miao9e60fdc2008-02-04 22:28:26 -0800222 if (ret)
223 goto out_failed;
224
225 /* set platform specific polarity inversion */
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800226 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
eric miao9e60fdc2008-02-04 22:28:26 -0800227 if (ret)
228 goto out_failed;
229
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800230
231 ret = gpiochip_add(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800232 if (ret)
233 goto out_failed;
234
235 if (pdata->setup) {
236 ret = pdata->setup(client, chip->gpio_chip.base,
237 chip->gpio_chip.ngpio, pdata->context);
238 if (ret < 0)
239 dev_warn(&client->dev, "setup failed, %d\n", ret);
240 }
241
242 i2c_set_clientdata(client, chip);
243 return 0;
244
245out_failed:
246 kfree(chip);
247 return ret;
248}
249
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800250static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800251{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800252 struct pca953x_platform_data *pdata = client->dev.platform_data;
253 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800254 int ret = 0;
255
256 if (pdata->teardown) {
257 ret = pdata->teardown(client, chip->gpio_chip.base,
258 chip->gpio_chip.ngpio, pdata->context);
259 if (ret < 0) {
260 dev_err(&client->dev, "%s failed, %d\n",
261 "teardown", ret);
262 return ret;
263 }
264 }
265
266 ret = gpiochip_remove(&chip->gpio_chip);
267 if (ret) {
268 dev_err(&client->dev, "%s failed, %d\n",
269 "gpiochip_remove()", ret);
270 return ret;
271 }
272
273 kfree(chip);
274 return 0;
275}
276
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800277static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800278 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800279 .name = "pca953x",
eric miao9e60fdc2008-02-04 22:28:26 -0800280 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800281 .probe = pca953x_probe,
282 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200283 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800284};
285
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800286static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800287{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800288 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800289}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800290module_init(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800291
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800292static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800293{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800294 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800295}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800296module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800297
298MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800299MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800300MODULE_LICENSE("GPL");