blob: 4b8446e98de5c31f5cc8100796293e29299eeec7 [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;
Nate Case1965d302009-06-17 16:26:17 -070088 struct pca953x_platform_data *dyn_pdata;
eric miao9e60fdc2008-02-04 22:28:26 -080089 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -070090 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +080091 int chip_type;
eric miao9e60fdc2008-02-04 22:28:26 -080092};
93
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080094static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
eric miao9e60fdc2008-02-04 22:28:26 -080095{
Haojian Zhuang33226ff2011-04-18 22:12:46 +080096 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080097
98 if (chip->gpio_chip.ngpio <= 8)
99 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800100 else {
101 switch (chip->chip_type) {
102 case PCA953X_TYPE:
103 ret = i2c_smbus_write_word_data(chip->client,
104 reg << 1, val);
105 break;
106 case PCA957X_TYPE:
107 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
108 val & 0xff);
109 if (ret < 0)
110 break;
111 ret = i2c_smbus_write_byte_data(chip->client,
112 (reg << 1) + 1,
113 (val & 0xff00) >> 8);
114 break;
115 }
116 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800117
118 if (ret < 0) {
119 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800120 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800121 }
122
123 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800124}
125
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800126static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800127{
128 int ret;
129
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800130 if (chip->gpio_chip.ngpio <= 8)
131 ret = i2c_smbus_read_byte_data(chip->client, reg);
132 else
133 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
134
eric miao9e60fdc2008-02-04 22:28:26 -0800135 if (ret < 0) {
136 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800137 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800138 }
139
140 *val = (uint16_t)ret;
141 return 0;
142}
143
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800144static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800145{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800146 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800147 uint16_t reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800148 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800149
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800150 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800151
Roland Stigge6e20fb12011-02-10 15:01:23 -0800152 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800153 reg_val = chip->reg_direction | (1u << off);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800154
155 switch (chip->chip_type) {
156 case PCA953X_TYPE:
157 offset = PCA953X_DIRECTION;
158 break;
159 case PCA957X_TYPE:
160 offset = PCA957X_CFG;
161 break;
162 }
163 ret = pca953x_write_reg(chip, offset, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800164 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800165 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800166
167 chip->reg_direction = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800168 ret = 0;
169exit:
170 mutex_unlock(&chip->i2c_lock);
171 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800172}
173
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800174static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800175 unsigned off, int val)
176{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800177 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800178 uint16_t reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800179 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800180
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800181 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800182
Roland Stigge6e20fb12011-02-10 15:01:23 -0800183 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800184 /* set output level */
185 if (val)
186 reg_val = chip->reg_output | (1u << off);
187 else
188 reg_val = chip->reg_output & ~(1u << off);
189
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800190 switch (chip->chip_type) {
191 case PCA953X_TYPE:
192 offset = PCA953X_OUTPUT;
193 break;
194 case PCA957X_TYPE:
195 offset = PCA957X_OUT;
196 break;
197 }
198 ret = pca953x_write_reg(chip, offset, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800199 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800200 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800201
202 chip->reg_output = reg_val;
203
204 /* then direction */
205 reg_val = chip->reg_direction & ~(1u << off);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800206 switch (chip->chip_type) {
207 case PCA953X_TYPE:
208 offset = PCA953X_DIRECTION;
209 break;
210 case PCA957X_TYPE:
211 offset = PCA957X_CFG;
212 break;
213 }
214 ret = pca953x_write_reg(chip, offset, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800215 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800216 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800217
218 chip->reg_direction = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800219 ret = 0;
220exit:
221 mutex_unlock(&chip->i2c_lock);
222 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800223}
224
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800225static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800226{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800227 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800228 uint16_t reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800229 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800230
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800231 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800232
Roland Stigge6e20fb12011-02-10 15:01:23 -0800233 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800234 switch (chip->chip_type) {
235 case PCA953X_TYPE:
236 offset = PCA953X_INPUT;
237 break;
238 case PCA957X_TYPE:
239 offset = PCA957X_IN;
240 break;
241 }
242 ret = pca953x_read_reg(chip, offset, &reg_val);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800243 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800244 if (ret < 0) {
245 /* NOTE: diagnostic already emitted; that's all we should
246 * do unless gpio_*_value_cansleep() calls become different
247 * from their nonsleeping siblings (and report faults).
248 */
249 return 0;
250 }
251
252 return (reg_val & (1u << off)) ? 1 : 0;
253}
254
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800255static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800256{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800257 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800258 uint16_t reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800259 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800260
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800261 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800262
Roland Stigge6e20fb12011-02-10 15:01:23 -0800263 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800264 if (val)
265 reg_val = chip->reg_output | (1u << off);
266 else
267 reg_val = chip->reg_output & ~(1u << off);
268
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800269 switch (chip->chip_type) {
270 case PCA953X_TYPE:
271 offset = PCA953X_OUTPUT;
272 break;
273 case PCA957X_TYPE:
274 offset = PCA957X_OUT;
275 break;
276 }
277 ret = pca953x_write_reg(chip, offset, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800278 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800279 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800280
281 chip->reg_output = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800282exit:
283 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800284}
285
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800286static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800287{
288 struct gpio_chip *gc;
289
290 gc = &chip->gpio_chip;
291
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800292 gc->direction_input = pca953x_gpio_direction_input;
293 gc->direction_output = pca953x_gpio_direction_output;
294 gc->get = pca953x_gpio_get_value;
295 gc->set = pca953x_gpio_set_value;
Arnaud Patard84207802008-03-10 11:43:48 -0700296 gc->can_sleep = 1;
eric miao9e60fdc2008-02-04 22:28:26 -0800297
298 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800299 gc->ngpio = gpios;
300 gc->label = chip->client->name;
David Brownelld8f388d2008-07-25 01:46:07 -0700301 gc->dev = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700302 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700303 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800304}
305
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800306#ifdef CONFIG_GPIO_PCA953X_IRQ
307static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
308{
309 struct pca953x_chip *chip;
310
311 chip = container_of(gc, struct pca953x_chip, gpio_chip);
312 return chip->irq_base + off;
313}
314
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800315static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800316{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800317 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800318
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800319 chip->irq_mask &= ~(1 << (d->irq - chip->irq_base));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800320}
321
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800322static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800323{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800324 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800325
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800326 chip->irq_mask |= 1 << (d->irq - chip->irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800327}
328
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800329static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800330{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800331 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800332
333 mutex_lock(&chip->irq_lock);
334}
335
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800336static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800337{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800338 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700339 uint16_t new_irqs;
340 uint16_t level;
341
342 /* Look for any newly setup interrupt */
343 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
344 new_irqs &= ~chip->reg_direction;
345
346 while (new_irqs) {
347 level = __ffs(new_irqs);
348 pca953x_gpio_direction_input(&chip->gpio_chip, level);
349 new_irqs &= ~(1 << level);
350 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800351
352 mutex_unlock(&chip->irq_lock);
353}
354
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800355static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800356{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800357 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
358 uint16_t level = d->irq - chip->irq_base;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800359 uint16_t mask = 1 << level;
360
361 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
362 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800363 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800364 return -EINVAL;
365 }
366
367 if (type & IRQ_TYPE_EDGE_FALLING)
368 chip->irq_trig_fall |= mask;
369 else
370 chip->irq_trig_fall &= ~mask;
371
372 if (type & IRQ_TYPE_EDGE_RISING)
373 chip->irq_trig_raise |= mask;
374 else
375 chip->irq_trig_raise &= ~mask;
376
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700377 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800378}
379
380static struct irq_chip pca953x_irq_chip = {
381 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800382 .irq_mask = pca953x_irq_mask,
383 .irq_unmask = pca953x_irq_unmask,
384 .irq_bus_lock = pca953x_irq_bus_lock,
385 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
386 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800387};
388
389static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
390{
391 uint16_t cur_stat;
392 uint16_t old_stat;
393 uint16_t pending;
394 uint16_t trigger;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800395 int ret, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800396
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800397 switch (chip->chip_type) {
398 case PCA953X_TYPE:
399 offset = PCA953X_INPUT;
400 break;
401 case PCA957X_TYPE:
402 offset = PCA957X_IN;
403 break;
404 }
405 ret = pca953x_read_reg(chip, offset, &cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800406 if (ret)
407 return 0;
408
409 /* Remove output pins from the equation */
410 cur_stat &= chip->reg_direction;
411
412 old_stat = chip->irq_stat;
413 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
414
415 if (!trigger)
416 return 0;
417
418 chip->irq_stat = cur_stat;
419
420 pending = (old_stat & chip->irq_trig_fall) |
421 (cur_stat & chip->irq_trig_raise);
422 pending &= trigger;
423
424 return pending;
425}
426
427static irqreturn_t pca953x_irq_handler(int irq, void *devid)
428{
429 struct pca953x_chip *chip = devid;
430 uint16_t pending;
431 uint16_t level;
432
433 pending = pca953x_irq_pending(chip);
434
435 if (!pending)
436 return IRQ_HANDLED;
437
438 do {
439 level = __ffs(pending);
David Jander6dd599f2011-06-08 11:37:45 -0600440 handle_nested_irq(level + chip->irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800441
442 pending &= ~(1 << level);
443 } while (pending);
444
445 return IRQ_HANDLED;
446}
447
448static int pca953x_irq_setup(struct pca953x_chip *chip,
449 const struct i2c_device_id *id)
450{
451 struct i2c_client *client = chip->client;
452 struct pca953x_platform_data *pdata = client->dev.platform_data;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800453 int ret, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800454
Alek Du8a233f02010-10-26 14:22:41 -0700455 if (pdata->irq_base != -1
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800456 && (id->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800457 int lvl;
458
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800459 switch (chip->chip_type) {
460 case PCA953X_TYPE:
461 offset = PCA953X_INPUT;
462 break;
463 case PCA957X_TYPE:
464 offset = PCA957X_IN;
465 break;
466 }
467 ret = pca953x_read_reg(chip, offset, &chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800468 if (ret)
469 goto out_failed;
470
471 /*
472 * There is no way to know which GPIO line generated the
473 * interrupt. We have to rely on the previous read for
474 * this purpose.
475 */
476 chip->irq_stat &= chip->reg_direction;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800477 mutex_init(&chip->irq_lock);
478
David Jander910c8fb2011-06-08 11:34:37 -0600479 chip->irq_base = irq_alloc_descs(-1, pdata->irq_base, chip->gpio_chip.ngpio, -1);
480 if (chip->irq_base < 0)
481 goto out_failed;
482
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800483 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
484 int irq = lvl + chip->irq_base;
485
David Jander910c8fb2011-06-08 11:34:37 -0600486 irq_clear_status_flags(irq, IRQ_NOREQUEST);
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000487 irq_set_chip_data(irq, chip);
David Jander6dd599f2011-06-08 11:37:45 -0600488 irq_set_chip(irq, &pca953x_irq_chip);
489 irq_set_nested_thread(irq, true);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800490#ifdef CONFIG_ARM
491 set_irq_flags(irq, IRQF_VALID);
492#else
Thomas Gleixnerb51804b2011-03-24 21:27:36 +0000493 irq_set_noprobe(irq);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800494#endif
495 }
496
497 ret = request_threaded_irq(client->irq,
498 NULL,
499 pca953x_irq_handler,
David Jander17e8b422011-06-08 11:34:41 -0600500 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800501 dev_name(&client->dev), chip);
502 if (ret) {
503 dev_err(&client->dev, "failed to request irq %d\n",
504 client->irq);
505 goto out_failed;
506 }
507
508 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
509 }
510
511 return 0;
512
513out_failed:
Alek Du8a233f02010-10-26 14:22:41 -0700514 chip->irq_base = -1;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800515 return ret;
516}
517
518static void pca953x_irq_teardown(struct pca953x_chip *chip)
519{
Alek Du8a233f02010-10-26 14:22:41 -0700520 if (chip->irq_base != -1)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800521 free_irq(chip->client->irq, chip);
522}
523#else /* CONFIG_GPIO_PCA953X_IRQ */
524static int pca953x_irq_setup(struct pca953x_chip *chip,
525 const struct i2c_device_id *id)
526{
527 struct i2c_client *client = chip->client;
528 struct pca953x_platform_data *pdata = client->dev.platform_data;
529
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800530 if (pdata->irq_base != -1 && (id->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800531 dev_warn(&client->dev, "interrupt support not compiled in\n");
532
533 return 0;
534}
535
536static void pca953x_irq_teardown(struct pca953x_chip *chip)
537{
538}
539#endif
540
Nate Case1965d302009-06-17 16:26:17 -0700541/*
542 * Handlers for alternative sources of platform_data
543 */
544#ifdef CONFIG_OF_GPIO
545/*
546 * Translate OpenFirmware node properties into platform_data
547 */
548static struct pca953x_platform_data *
549pca953x_get_alt_pdata(struct i2c_client *client)
550{
551 struct pca953x_platform_data *pdata;
552 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)
558 return NULL;
559
560 pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
561 if (pdata == NULL) {
562 dev_err(&client->dev, "Unable to allocate platform_data\n");
563 return NULL;
564 }
565
566 pdata->gpio_base = -1;
Dirk Eibach16482372011-02-24 10:20:43 +0100567 val = of_get_property(node, "linux,gpio-base", &size);
Nate Case1965d302009-06-17 16:26:17 -0700568 if (val) {
Dirk Eibach16482372011-02-24 10:20:43 +0100569 if (size != sizeof(*val))
570 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
571 node->full_name);
Nate Case1965d302009-06-17 16:26:17 -0700572 else
Dirk Eibach16482372011-02-24 10:20:43 +0100573 pdata->gpio_base = be32_to_cpup(val);
Nate Case1965d302009-06-17 16:26:17 -0700574 }
575
576 val = of_get_property(node, "polarity", NULL);
577 if (val)
578 pdata->invert = *val;
579
580 return pdata;
581}
582#else
583static struct pca953x_platform_data *
584pca953x_get_alt_pdata(struct i2c_client *client)
585{
586 return NULL;
587}
588#endif
589
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800590static int __devinit device_pca953x_init(struct pca953x_chip *chip, int invert)
591{
592 int ret;
593
594 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
595 if (ret)
596 goto out;
597
598 ret = pca953x_read_reg(chip, PCA953X_DIRECTION,
599 &chip->reg_direction);
600 if (ret)
601 goto out;
602
603 /* set platform specific polarity inversion */
604 ret = pca953x_write_reg(chip, PCA953X_INVERT, invert);
605 if (ret)
606 goto out;
607 return 0;
608out:
609 return ret;
610}
611
612static int __devinit device_pca957x_init(struct pca953x_chip *chip, int invert)
613{
614 int ret;
615 uint16_t val = 0;
616
617 /* Let every port in proper state, that could save power */
618 pca953x_write_reg(chip, PCA957X_PUPD, 0x0);
619 pca953x_write_reg(chip, PCA957X_CFG, 0xffff);
620 pca953x_write_reg(chip, PCA957X_OUT, 0x0);
621
622 ret = pca953x_read_reg(chip, PCA957X_IN, &val);
623 if (ret)
624 goto out;
625 ret = pca953x_read_reg(chip, PCA957X_OUT, &chip->reg_output);
626 if (ret)
627 goto out;
628 ret = pca953x_read_reg(chip, PCA957X_CFG, &chip->reg_direction);
629 if (ret)
630 goto out;
631
632 /* set platform specific polarity inversion */
633 pca953x_write_reg(chip, PCA957X_INVRT, invert);
634
635 /* To enable register 6, 7 to controll pull up and pull down */
636 pca953x_write_reg(chip, PCA957X_BKEN, 0x202);
637
638 return 0;
639out:
640 return ret;
641}
642
Jean Delvared2653e92008-04-29 23:11:39 +0200643static int __devinit pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200644 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800645{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800646 struct pca953x_platform_data *pdata;
647 struct pca953x_chip *chip;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800648 int ret = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800649
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800650 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800651 if (chip == NULL)
652 return -ENOMEM;
653
Nate Case1965d302009-06-17 16:26:17 -0700654 pdata = client->dev.platform_data;
655 if (pdata == NULL) {
656 pdata = pca953x_get_alt_pdata(client);
657 /*
658 * Unlike normal platform_data, this is allocated
659 * dynamically and must be freed in the driver
660 */
661 chip->dyn_pdata = pdata;
662 }
663
664 if (pdata == NULL) {
665 dev_dbg(&client->dev, "no platform data\n");
666 ret = -EINVAL;
667 goto out_failed;
668 }
669
eric miao9e60fdc2008-02-04 22:28:26 -0800670 chip->client = client;
671
672 chip->gpio_start = pdata->gpio_base;
673
Daniel Silverstone77906a542009-06-17 16:26:15 -0700674 chip->names = pdata->names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800675 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700676
Roland Stigge6e20fb12011-02-10 15:01:23 -0800677 mutex_init(&chip->i2c_lock);
678
eric miao9e60fdc2008-02-04 22:28:26 -0800679 /* initialize cached registers from their original values.
680 * we can't share this chip with another i2c master.
681 */
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800682 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800683
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800684 if (chip->chip_type == PCA953X_TYPE)
685 device_pca953x_init(chip, pdata->invert);
686 else if (chip->chip_type == PCA957X_TYPE)
687 device_pca957x_init(chip, pdata->invert);
688 else
eric miao9e60fdc2008-02-04 22:28:26 -0800689 goto out_failed;
690
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800691 ret = pca953x_irq_setup(chip, id);
692 if (ret)
693 goto out_failed;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800694
695 ret = gpiochip_add(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800696 if (ret)
Ben Dooks272df502011-03-31 07:18:46 +0900697 goto out_failed_irq;
eric miao9e60fdc2008-02-04 22:28:26 -0800698
699 if (pdata->setup) {
700 ret = pdata->setup(client, chip->gpio_chip.base,
701 chip->gpio_chip.ngpio, pdata->context);
702 if (ret < 0)
703 dev_warn(&client->dev, "setup failed, %d\n", ret);
704 }
705
706 i2c_set_clientdata(client, chip);
707 return 0;
708
Ben Dooks272df502011-03-31 07:18:46 +0900709out_failed_irq:
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800710 pca953x_irq_teardown(chip);
Ben Dooks272df502011-03-31 07:18:46 +0900711out_failed:
Nate Case1965d302009-06-17 16:26:17 -0700712 kfree(chip->dyn_pdata);
eric miao9e60fdc2008-02-04 22:28:26 -0800713 kfree(chip);
714 return ret;
715}
716
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800717static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800718{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800719 struct pca953x_platform_data *pdata = client->dev.platform_data;
720 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800721 int ret = 0;
722
723 if (pdata->teardown) {
724 ret = pdata->teardown(client, chip->gpio_chip.base,
725 chip->gpio_chip.ngpio, pdata->context);
726 if (ret < 0) {
727 dev_err(&client->dev, "%s failed, %d\n",
728 "teardown", ret);
729 return ret;
730 }
731 }
732
733 ret = gpiochip_remove(&chip->gpio_chip);
734 if (ret) {
735 dev_err(&client->dev, "%s failed, %d\n",
736 "gpiochip_remove()", ret);
737 return ret;
738 }
739
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800740 pca953x_irq_teardown(chip);
Nate Case1965d302009-06-17 16:26:17 -0700741 kfree(chip->dyn_pdata);
eric miao9e60fdc2008-02-04 22:28:26 -0800742 kfree(chip);
743 return 0;
744}
745
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800746static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800747 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800748 .name = "pca953x",
eric miao9e60fdc2008-02-04 22:28:26 -0800749 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800750 .probe = pca953x_probe,
751 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200752 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800753};
754
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800755static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800756{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800757 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800758}
David Brownell2f8d1192008-10-15 22:03:13 -0700759/* register after i2c postcore initcall and before
760 * subsys initcalls that may rely on these GPIOs
761 */
762subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800763
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800764static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800765{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800766 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800767}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800768module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800769
770MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800771MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800772MODULE_LICENSE("GPL");