blob: e6805b97d047bc503d0306a40db9d239005f1469 [file] [log] [blame]
eric miao9e60fdc2008-02-04 22:28:26 -08001/*
Grant Likelyc103de22011-06-04 18:38:28 -06002 * PCA953x 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>
H Hartley Sweetend120c172009-09-22 16:46:37 -070016#include <linux/gpio.h>
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080017#include <linux/interrupt.h>
18#include <linux/irq.h>
eric miao9e60fdc2008-02-04 22:28:26 -080019#include <linux/i2c.h>
Guennadi Liakhovetskid1c057e32008-02-06 01:39:02 -080020#include <linux/i2c/pca953x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Nate Case1965d302009-06-17 16:26:17 -070022#ifdef CONFIG_OF_GPIO
23#include <linux/of_platform.h>
24#include <linux/of_gpio.h>
25#endif
eric miao9e60fdc2008-02-04 22:28:26 -080026
Haojian Zhuang33226ff2011-04-18 22:12:46 +080027#define PCA953X_INPUT 0
28#define PCA953X_OUTPUT 1
29#define PCA953X_INVERT 2
30#define PCA953X_DIRECTION 3
eric miao9e60fdc2008-02-04 22:28:26 -080031
Haojian Zhuang33226ff2011-04-18 22:12:46 +080032#define PCA957X_IN 0
33#define PCA957X_INVRT 1
34#define PCA957X_BKEN 2
35#define PCA957X_PUPD 3
36#define PCA957X_CFG 4
37#define PCA957X_OUT 5
38#define PCA957X_MSK 6
39#define PCA957X_INTS 7
40
41#define PCA_GPIO_MASK 0x00FF
42#define PCA_INT 0x0100
43#define PCA953X_TYPE 0x1000
44#define PCA957X_TYPE 0x2000
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080045
Jean Delvare3760f732008-04-29 23:11:40 +020046static const struct i2c_device_id pca953x_id[] = {
Haojian Zhuang33226ff2011-04-18 22:12:46 +080047 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
48 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
49 { "pca9536", 4 | PCA953X_TYPE, },
50 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
51 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
52 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
53 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
54 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
55 { "pca9556", 8 | PCA953X_TYPE, },
56 { "pca9557", 8 | PCA953X_TYPE, },
57 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
58 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
David Brownellab5dc372009-01-06 14:42:27 -080059
Haojian Zhuang33226ff2011-04-18 22:12:46 +080060 { "max7310", 8 | PCA953X_TYPE, },
61 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
62 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
63 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
64 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
65 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
66 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
David Brownellab5dc372009-01-06 14:42:27 -080067 /* NYET: { "tca6424", 24, }, */
Jean Delvare3760f732008-04-29 23:11:40 +020068 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080069};
Jean Delvare3760f732008-04-29 23:11:40 +020070MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080071
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080072struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080073 unsigned gpio_start;
74 uint16_t reg_output;
75 uint16_t reg_direction;
Roland Stigge6e20fb12011-02-10 15:01:23 -080076 struct mutex i2c_lock;
eric miao9e60fdc2008-02-04 22:28:26 -080077
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080078#ifdef CONFIG_GPIO_PCA953X_IRQ
79 struct mutex irq_lock;
80 uint16_t irq_mask;
81 uint16_t irq_stat;
82 uint16_t irq_trig_raise;
83 uint16_t irq_trig_fall;
84 int irq_base;
85#endif
86
eric miao9e60fdc2008-02-04 22:28:26 -080087 struct i2c_client *client;
88 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -070089 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +080090 int chip_type;
eric miao9e60fdc2008-02-04 22:28:26 -080091};
92
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080093static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
eric miao9e60fdc2008-02-04 22:28:26 -080094{
Haojian Zhuang33226ff2011-04-18 22:12:46 +080095 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080096
97 if (chip->gpio_chip.ngpio <= 8)
98 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +080099 else {
100 switch (chip->chip_type) {
101 case PCA953X_TYPE:
102 ret = i2c_smbus_write_word_data(chip->client,
103 reg << 1, val);
104 break;
105 case PCA957X_TYPE:
106 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
107 val & 0xff);
108 if (ret < 0)
109 break;
110 ret = i2c_smbus_write_byte_data(chip->client,
111 (reg << 1) + 1,
112 (val & 0xff00) >> 8);
113 break;
114 }
115 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800116
117 if (ret < 0) {
118 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800119 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800120 }
121
122 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800123}
124
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800125static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800126{
127 int ret;
128
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800129 if (chip->gpio_chip.ngpio <= 8)
130 ret = i2c_smbus_read_byte_data(chip->client, reg);
131 else
132 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
133
eric miao9e60fdc2008-02-04 22:28:26 -0800134 if (ret < 0) {
135 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800136 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800137 }
138
139 *val = (uint16_t)ret;
140 return 0;
141}
142
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800143static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800144{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800145 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800146 uint16_t reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800147 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800148
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800149 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800150
Roland Stigge6e20fb12011-02-10 15:01:23 -0800151 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800152 reg_val = chip->reg_direction | (1u << off);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800153
154 switch (chip->chip_type) {
155 case PCA953X_TYPE:
156 offset = PCA953X_DIRECTION;
157 break;
158 case PCA957X_TYPE:
159 offset = PCA957X_CFG;
160 break;
161 }
162 ret = pca953x_write_reg(chip, offset, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800163 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800164 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800165
166 chip->reg_direction = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800167 ret = 0;
168exit:
169 mutex_unlock(&chip->i2c_lock);
170 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800171}
172
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800173static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800174 unsigned off, int val)
175{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800176 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800177 uint16_t reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800178 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800179
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800180 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800181
Roland Stigge6e20fb12011-02-10 15:01:23 -0800182 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800183 /* set output level */
184 if (val)
185 reg_val = chip->reg_output | (1u << off);
186 else
187 reg_val = chip->reg_output & ~(1u << off);
188
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800189 switch (chip->chip_type) {
190 case PCA953X_TYPE:
191 offset = PCA953X_OUTPUT;
192 break;
193 case PCA957X_TYPE:
194 offset = PCA957X_OUT;
195 break;
196 }
197 ret = pca953x_write_reg(chip, offset, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800198 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800199 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800200
201 chip->reg_output = reg_val;
202
203 /* then direction */
204 reg_val = chip->reg_direction & ~(1u << off);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800205 switch (chip->chip_type) {
206 case PCA953X_TYPE:
207 offset = PCA953X_DIRECTION;
208 break;
209 case PCA957X_TYPE:
210 offset = PCA957X_CFG;
211 break;
212 }
213 ret = pca953x_write_reg(chip, offset, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800214 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800215 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800216
217 chip->reg_direction = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800218 ret = 0;
219exit:
220 mutex_unlock(&chip->i2c_lock);
221 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800222}
223
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800224static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800225{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800226 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800227 uint16_t reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800228 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800229
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800230 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800231
Roland Stigge6e20fb12011-02-10 15:01:23 -0800232 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800233 switch (chip->chip_type) {
234 case PCA953X_TYPE:
235 offset = PCA953X_INPUT;
236 break;
237 case PCA957X_TYPE:
238 offset = PCA957X_IN;
239 break;
240 }
241 ret = pca953x_read_reg(chip, offset, &reg_val);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800242 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800243 if (ret < 0) {
244 /* NOTE: diagnostic already emitted; that's all we should
245 * do unless gpio_*_value_cansleep() calls become different
246 * from their nonsleeping siblings (and report faults).
247 */
248 return 0;
249 }
250
251 return (reg_val & (1u << off)) ? 1 : 0;
252}
253
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800254static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800255{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800256 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800257 uint16_t reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800258 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800259
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800260 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800261
Roland Stigge6e20fb12011-02-10 15:01:23 -0800262 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800263 if (val)
264 reg_val = chip->reg_output | (1u << off);
265 else
266 reg_val = chip->reg_output & ~(1u << off);
267
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800268 switch (chip->chip_type) {
269 case PCA953X_TYPE:
270 offset = PCA953X_OUTPUT;
271 break;
272 case PCA957X_TYPE:
273 offset = PCA957X_OUT;
274 break;
275 }
276 ret = pca953x_write_reg(chip, offset, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800277 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800278 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800279
280 chip->reg_output = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800281exit:
282 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800283}
284
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800285static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800286{
287 struct gpio_chip *gc;
288
289 gc = &chip->gpio_chip;
290
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800291 gc->direction_input = pca953x_gpio_direction_input;
292 gc->direction_output = pca953x_gpio_direction_output;
293 gc->get = pca953x_gpio_get_value;
294 gc->set = pca953x_gpio_set_value;
Arnaud Patard84207802008-03-10 11:43:48 -0700295 gc->can_sleep = 1;
eric miao9e60fdc2008-02-04 22:28:26 -0800296
297 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800298 gc->ngpio = gpios;
299 gc->label = chip->client->name;
David Brownelld8f388d82008-07-25 01:46:07 -0700300 gc->dev = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700301 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700302 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800303}
304
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800305#ifdef CONFIG_GPIO_PCA953X_IRQ
306static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
307{
308 struct pca953x_chip *chip;
309
310 chip = container_of(gc, struct pca953x_chip, gpio_chip);
311 return chip->irq_base + off;
312}
313
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800314static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800315{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800316 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800317
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800318 chip->irq_mask &= ~(1 << (d->irq - chip->irq_base));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800319}
320
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800321static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800322{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800323 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800324
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800325 chip->irq_mask |= 1 << (d->irq - chip->irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800326}
327
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800328static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800329{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800330 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800331
332 mutex_lock(&chip->irq_lock);
333}
334
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800335static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800336{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800337 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700338 uint16_t new_irqs;
339 uint16_t level;
340
341 /* Look for any newly setup interrupt */
342 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
343 new_irqs &= ~chip->reg_direction;
344
345 while (new_irqs) {
346 level = __ffs(new_irqs);
347 pca953x_gpio_direction_input(&chip->gpio_chip, level);
348 new_irqs &= ~(1 << level);
349 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800350
351 mutex_unlock(&chip->irq_lock);
352}
353
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800354static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800355{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800356 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
357 uint16_t level = d->irq - chip->irq_base;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800358 uint16_t mask = 1 << level;
359
360 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
361 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800362 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800363 return -EINVAL;
364 }
365
366 if (type & IRQ_TYPE_EDGE_FALLING)
367 chip->irq_trig_fall |= mask;
368 else
369 chip->irq_trig_fall &= ~mask;
370
371 if (type & IRQ_TYPE_EDGE_RISING)
372 chip->irq_trig_raise |= mask;
373 else
374 chip->irq_trig_raise &= ~mask;
375
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700376 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800377}
378
379static struct irq_chip pca953x_irq_chip = {
380 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800381 .irq_mask = pca953x_irq_mask,
382 .irq_unmask = pca953x_irq_unmask,
383 .irq_bus_lock = pca953x_irq_bus_lock,
384 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
385 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800386};
387
388static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
389{
390 uint16_t cur_stat;
391 uint16_t old_stat;
392 uint16_t pending;
393 uint16_t trigger;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800394 int ret, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800395
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800396 switch (chip->chip_type) {
397 case PCA953X_TYPE:
398 offset = PCA953X_INPUT;
399 break;
400 case PCA957X_TYPE:
401 offset = PCA957X_IN;
402 break;
403 }
404 ret = pca953x_read_reg(chip, offset, &cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800405 if (ret)
406 return 0;
407
408 /* Remove output pins from the equation */
409 cur_stat &= chip->reg_direction;
410
411 old_stat = chip->irq_stat;
412 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
413
414 if (!trigger)
415 return 0;
416
417 chip->irq_stat = cur_stat;
418
419 pending = (old_stat & chip->irq_trig_fall) |
420 (cur_stat & chip->irq_trig_raise);
421 pending &= trigger;
422
423 return pending;
424}
425
426static irqreturn_t pca953x_irq_handler(int irq, void *devid)
427{
428 struct pca953x_chip *chip = devid;
429 uint16_t pending;
430 uint16_t level;
431
432 pending = pca953x_irq_pending(chip);
433
434 if (!pending)
435 return IRQ_HANDLED;
436
437 do {
438 level = __ffs(pending);
David Jander6dd599f2011-06-08 11:37:45 -0600439 handle_nested_irq(level + chip->irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800440
441 pending &= ~(1 << level);
442 } while (pending);
443
444 return IRQ_HANDLED;
445}
446
447static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200448 const struct i2c_device_id *id,
449 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800450{
451 struct i2c_client *client = chip->client;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800452 int ret, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800453
David Janderc6dcf592011-06-14 11:00:55 +0200454 if (irq_base != -1
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800455 && (id->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800456 int lvl;
457
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800458 switch (chip->chip_type) {
459 case PCA953X_TYPE:
460 offset = PCA953X_INPUT;
461 break;
462 case PCA957X_TYPE:
463 offset = PCA957X_IN;
464 break;
465 }
466 ret = pca953x_read_reg(chip, offset, &chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800467 if (ret)
468 goto out_failed;
469
470 /*
471 * There is no way to know which GPIO line generated the
472 * interrupt. We have to rely on the previous read for
473 * this purpose.
474 */
475 chip->irq_stat &= chip->reg_direction;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800476 mutex_init(&chip->irq_lock);
477
David Janderc6dcf592011-06-14 11:00:55 +0200478 chip->irq_base = irq_alloc_descs(-1, irq_base, chip->gpio_chip.ngpio, -1);
David Jander910c8fb2011-06-08 11:34:37 -0600479 if (chip->irq_base < 0)
480 goto out_failed;
481
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800482 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
483 int irq = lvl + chip->irq_base;
484
David Jander910c8fb2011-06-08 11:34:37 -0600485 irq_clear_status_flags(irq, IRQ_NOREQUEST);
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000486 irq_set_chip_data(irq, chip);
David Jander6dd599f2011-06-08 11:37:45 -0600487 irq_set_chip(irq, &pca953x_irq_chip);
488 irq_set_nested_thread(irq, true);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800489#ifdef CONFIG_ARM
490 set_irq_flags(irq, IRQF_VALID);
491#else
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000492 irq_set_noprobe(irq);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800493#endif
494 }
495
496 ret = request_threaded_irq(client->irq,
497 NULL,
498 pca953x_irq_handler,
David Jander17e8b422011-06-08 11:34:41 -0600499 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800500 dev_name(&client->dev), chip);
501 if (ret) {
502 dev_err(&client->dev, "failed to request irq %d\n",
503 client->irq);
504 goto out_failed;
505 }
506
507 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
508 }
509
510 return 0;
511
512out_failed:
Alek Du8a233f012010-10-26 14:22:41 -0700513 chip->irq_base = -1;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800514 return ret;
515}
516
517static void pca953x_irq_teardown(struct pca953x_chip *chip)
518{
David Janderc609c052011-06-14 11:00:54 +0200519 if (chip->irq_base != -1) {
520 irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800521 free_irq(chip->client->irq, chip);
David Janderc609c052011-06-14 11:00:54 +0200522 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800523}
524#else /* CONFIG_GPIO_PCA953X_IRQ */
525static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200526 const struct i2c_device_id *id,
527 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800528{
529 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800530
David Janderc6dcf592011-06-14 11:00:55 +0200531 if (irq_base != -1 && (id->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800532 dev_warn(&client->dev, "interrupt support not compiled in\n");
533
534 return 0;
535}
536
537static void pca953x_irq_teardown(struct pca953x_chip *chip)
538{
539}
540#endif
541
Nate Case1965d302009-06-17 16:26:17 -0700542/*
543 * Handlers for alternative sources of platform_data
544 */
545#ifdef CONFIG_OF_GPIO
546/*
547 * Translate OpenFirmware node properties into platform_data
548 */
David Janderc6dcf592011-06-14 11:00:55 +0200549void
550pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, int *invert)
Nate Case1965d302009-06-17 16:26:17 -0700551{
Nate Case1965d302009-06-17 16:26:17 -0700552 struct device_node *node;
Dirk Eibach16482372011-02-24 10:20:43 +0100553 const __be32 *val;
554 int size;
Nate Case1965d302009-06-17 16:26:17 -0700555
Grant Likely61c7a082010-04-13 16:12:29 -0700556 node = client->dev.of_node;
Nate Case1965d302009-06-17 16:26:17 -0700557 if (node == NULL)
David Janderc6dcf592011-06-14 11:00:55 +0200558 return;
Nate Case1965d302009-06-17 16:26:17 -0700559
David Janderc6dcf592011-06-14 11:00:55 +0200560 *gpio_base = -1;
Dirk Eibach16482372011-02-24 10:20:43 +0100561 val = of_get_property(node, "linux,gpio-base", &size);
Nate Case1965d302009-06-17 16:26:17 -0700562 if (val) {
Dirk Eibach16482372011-02-24 10:20:43 +0100563 if (size != sizeof(*val))
564 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
565 node->full_name);
Nate Case1965d302009-06-17 16:26:17 -0700566 else
David Janderc6dcf592011-06-14 11:00:55 +0200567 *gpio_base = be32_to_cpup(val);
Nate Case1965d302009-06-17 16:26:17 -0700568 }
569
570 val = of_get_property(node, "polarity", NULL);
571 if (val)
David Janderc6dcf592011-06-14 11:00:55 +0200572 *invert = *val;
Nate Case1965d302009-06-17 16:26:17 -0700573}
574#else
David Janderc6dcf592011-06-14 11:00:55 +0200575void
576pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, int *invert)
Nate Case1965d302009-06-17 16:26:17 -0700577{
Nate Case1965d302009-06-17 16:26:17 -0700578}
579#endif
580
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800581static int __devinit device_pca953x_init(struct pca953x_chip *chip, int invert)
582{
583 int ret;
584
585 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
586 if (ret)
587 goto out;
588
589 ret = pca953x_read_reg(chip, PCA953X_DIRECTION,
590 &chip->reg_direction);
591 if (ret)
592 goto out;
593
594 /* set platform specific polarity inversion */
595 ret = pca953x_write_reg(chip, PCA953X_INVERT, invert);
596 if (ret)
597 goto out;
598 return 0;
599out:
600 return ret;
601}
602
603static int __devinit device_pca957x_init(struct pca953x_chip *chip, int invert)
604{
605 int ret;
606 uint16_t val = 0;
607
608 /* Let every port in proper state, that could save power */
609 pca953x_write_reg(chip, PCA957X_PUPD, 0x0);
610 pca953x_write_reg(chip, PCA957X_CFG, 0xffff);
611 pca953x_write_reg(chip, PCA957X_OUT, 0x0);
612
613 ret = pca953x_read_reg(chip, PCA957X_IN, &val);
614 if (ret)
615 goto out;
616 ret = pca953x_read_reg(chip, PCA957X_OUT, &chip->reg_output);
617 if (ret)
618 goto out;
619 ret = pca953x_read_reg(chip, PCA957X_CFG, &chip->reg_direction);
620 if (ret)
621 goto out;
622
623 /* set platform specific polarity inversion */
624 pca953x_write_reg(chip, PCA957X_INVRT, invert);
625
626 /* To enable register 6, 7 to controll pull up and pull down */
627 pca953x_write_reg(chip, PCA957X_BKEN, 0x202);
628
629 return 0;
630out:
631 return ret;
632}
633
Jean Delvared2653e92008-04-29 23:11:39 +0200634static int __devinit pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200635 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800636{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800637 struct pca953x_platform_data *pdata;
638 struct pca953x_chip *chip;
David Janderc6dcf592011-06-14 11:00:55 +0200639 int irq_base=-1, invert=0;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800640 int ret = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800641
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800642 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800643 if (chip == NULL)
644 return -ENOMEM;
645
Nate Case1965d302009-06-17 16:26:17 -0700646 pdata = client->dev.platform_data;
David Janderc6dcf592011-06-14 11:00:55 +0200647 if (pdata) {
648 irq_base = pdata->irq_base;
649 chip->gpio_start = pdata->gpio_base;
650 invert = pdata->invert;
651 chip->names = pdata->names;
652 } else {
653 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
Nate Case1965d302009-06-17 16:26:17 -0700654 }
655
eric miao9e60fdc2008-02-04 22:28:26 -0800656 chip->client = client;
657
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800658 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700659
Roland Stigge6e20fb12011-02-10 15:01:23 -0800660 mutex_init(&chip->i2c_lock);
661
eric miao9e60fdc2008-02-04 22:28:26 -0800662 /* initialize cached registers from their original values.
663 * we can't share this chip with another i2c master.
664 */
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800665 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800666
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800667 if (chip->chip_type == PCA953X_TYPE)
David Janderc6dcf592011-06-14 11:00:55 +0200668 device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800669 else if (chip->chip_type == PCA957X_TYPE)
David Janderc6dcf592011-06-14 11:00:55 +0200670 device_pca957x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800671 else
eric miao9e60fdc2008-02-04 22:28:26 -0800672 goto out_failed;
673
David Janderc6dcf592011-06-14 11:00:55 +0200674 ret = pca953x_irq_setup(chip, id, irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800675 if (ret)
676 goto out_failed;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800677
678 ret = gpiochip_add(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800679 if (ret)
Ben Dooks272df502011-03-31 07:18:46 +0900680 goto out_failed_irq;
eric miao9e60fdc2008-02-04 22:28:26 -0800681
David Janderc6dcf592011-06-14 11:00:55 +0200682 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800683 ret = pdata->setup(client, chip->gpio_chip.base,
684 chip->gpio_chip.ngpio, pdata->context);
685 if (ret < 0)
686 dev_warn(&client->dev, "setup failed, %d\n", ret);
687 }
688
689 i2c_set_clientdata(client, chip);
690 return 0;
691
Ben Dooks272df502011-03-31 07:18:46 +0900692out_failed_irq:
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800693 pca953x_irq_teardown(chip);
Ben Dooks272df502011-03-31 07:18:46 +0900694out_failed:
eric miao9e60fdc2008-02-04 22:28:26 -0800695 kfree(chip);
696 return ret;
697}
698
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800699static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800700{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800701 struct pca953x_platform_data *pdata = client->dev.platform_data;
702 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800703 int ret = 0;
704
David Janderc6dcf592011-06-14 11:00:55 +0200705 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800706 ret = pdata->teardown(client, chip->gpio_chip.base,
707 chip->gpio_chip.ngpio, pdata->context);
708 if (ret < 0) {
709 dev_err(&client->dev, "%s failed, %d\n",
710 "teardown", ret);
711 return ret;
712 }
713 }
714
715 ret = gpiochip_remove(&chip->gpio_chip);
716 if (ret) {
717 dev_err(&client->dev, "%s failed, %d\n",
718 "gpiochip_remove()", ret);
719 return ret;
720 }
721
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800722 pca953x_irq_teardown(chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800723 kfree(chip);
724 return 0;
725}
726
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800727static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800728 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800729 .name = "pca953x",
eric miao9e60fdc2008-02-04 22:28:26 -0800730 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800731 .probe = pca953x_probe,
732 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200733 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800734};
735
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800736static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800737{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800738 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800739}
David Brownell2f8d1192008-10-15 22:03:13 -0700740/* register after i2c postcore initcall and before
741 * subsys initcalls that may rely on these GPIOs
742 */
743subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800744
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800745static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800746{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800747 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800748}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800749module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800750
751MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800752MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800753MODULE_LICENSE("GPL");