blob: 1dc99062eb144df55cd04c6425da1396e8067261 [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>
Maxime Ripard55ecd262012-11-08 18:01:51 +010019#include <linux/irqdomain.h>
eric miao9e60fdc2008-02-04 22:28:26 -080020#include <linux/i2c.h>
Guennadi Liakhovetskid1c057e32008-02-06 01:39:02 -080021#include <linux/i2c/pca953x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Nate Case1965d302009-06-17 16:26:17 -070023#ifdef CONFIG_OF_GPIO
24#include <linux/of_platform.h>
Nate Case1965d302009-06-17 16:26:17 -070025#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
Andreas Schallenbergae79c192012-05-09 09:46:17 +020032#define REG_ADDR_AI 0x80
33
Haojian Zhuang33226ff2011-04-18 22:12:46 +080034#define PCA957X_IN 0
35#define PCA957X_INVRT 1
36#define PCA957X_BKEN 2
37#define PCA957X_PUPD 3
38#define PCA957X_CFG 4
39#define PCA957X_OUT 5
40#define PCA957X_MSK 6
41#define PCA957X_INTS 7
42
43#define PCA_GPIO_MASK 0x00FF
44#define PCA_INT 0x0100
45#define PCA953X_TYPE 0x1000
46#define PCA957X_TYPE 0x2000
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080047
Jean Delvare3760f732008-04-29 23:11:40 +020048static const struct i2c_device_id pca953x_id[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +010049 { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080050 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
51 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
52 { "pca9536", 4 | PCA953X_TYPE, },
53 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
54 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
55 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
56 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
57 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
58 { "pca9556", 8 | PCA953X_TYPE, },
59 { "pca9557", 8 | PCA953X_TYPE, },
60 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
61 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
David Brownellab5dc372009-01-06 14:42:27 -080062
Haojian Zhuang33226ff2011-04-18 22:12:46 +080063 { "max7310", 8 | PCA953X_TYPE, },
64 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
65 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
66 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
67 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
68 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
69 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
Andreas Schallenbergae79c192012-05-09 09:46:17 +020070 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
Jean Delvare3760f732008-04-29 23:11:40 +020071 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080072};
Jean Delvare3760f732008-04-29 23:11:40 +020073MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080074
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010075#define MAX_BANK 5
76#define BANK_SZ 8
77
78#define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)
79
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080080struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080081 unsigned gpio_start;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010082 u8 reg_output[MAX_BANK];
83 u8 reg_direction[MAX_BANK];
Roland Stigge6e20fb12011-02-10 15:01:23 -080084 struct mutex i2c_lock;
eric miao9e60fdc2008-02-04 22:28:26 -080085
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080086#ifdef CONFIG_GPIO_PCA953X_IRQ
87 struct mutex irq_lock;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010088 u8 irq_mask[MAX_BANK];
89 u8 irq_stat[MAX_BANK];
90 u8 irq_trig_raise[MAX_BANK];
91 u8 irq_trig_fall[MAX_BANK];
Maxime Ripard55ecd262012-11-08 18:01:51 +010092 struct irq_domain *domain;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080093#endif
94
eric miao9e60fdc2008-02-04 22:28:26 -080095 struct i2c_client *client;
96 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -070097 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +080098 int chip_type;
eric miao9e60fdc2008-02-04 22:28:26 -080099};
100
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100101static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
102 int off)
103{
104 int ret;
105 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
106 int offset = off / BANK_SZ;
107
108 ret = i2c_smbus_read_byte_data(chip->client,
109 (reg << bank_shift) + offset);
110 *val = ret;
111
112 if (ret < 0) {
113 dev_err(&chip->client->dev, "failed reading register\n");
114 return ret;
115 }
116
117 return 0;
118}
119
120static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
121 int off)
122{
123 int ret = 0;
124 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
125 int offset = off / BANK_SZ;
126
127 ret = i2c_smbus_write_byte_data(chip->client,
128 (reg << bank_shift) + offset, val);
129
130 if (ret < 0) {
131 dev_err(&chip->client->dev, "failed writing register\n");
132 return ret;
133 }
134
135 return 0;
136}
137
138static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800139{
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800140 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800141
142 if (chip->gpio_chip.ngpio <= 8)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100143 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
144 else if (chip->gpio_chip.ngpio >= 24) {
145 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200146 ret = i2c_smbus_write_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100147 (reg << bank_shift) | REG_ADDR_AI,
148 NBANK(chip), val);
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200149 }
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800150 else {
151 switch (chip->chip_type) {
152 case PCA953X_TYPE:
153 ret = i2c_smbus_write_word_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100154 reg << 1, (u16) *val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800155 break;
156 case PCA957X_TYPE:
157 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100158 val[0]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800159 if (ret < 0)
160 break;
161 ret = i2c_smbus_write_byte_data(chip->client,
162 (reg << 1) + 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100163 val[1]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800164 break;
165 }
166 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800167
168 if (ret < 0) {
169 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800170 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800171 }
172
173 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800174}
175
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100176static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800177{
178 int ret;
179
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200180 if (chip->gpio_chip.ngpio <= 8) {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800181 ret = i2c_smbus_read_byte_data(chip->client, reg);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200182 *val = ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100183 } else if (chip->gpio_chip.ngpio >= 24) {
184 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
185
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200186 ret = i2c_smbus_read_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100187 (reg << bank_shift) | REG_ADDR_AI,
188 NBANK(chip), val);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200189 } else {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800190 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100191 val[0] = (u16)ret & 0xFF;
192 val[1] = (u16)ret >> 8;
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200193 }
eric miao9e60fdc2008-02-04 22:28:26 -0800194 if (ret < 0) {
195 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800196 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800197 }
198
eric miao9e60fdc2008-02-04 22:28:26 -0800199 return 0;
200}
201
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800202static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800203{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800204 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100205 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800206 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800207
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800208 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800209
Roland Stigge6e20fb12011-02-10 15:01:23 -0800210 mutex_lock(&chip->i2c_lock);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100211 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800212
213 switch (chip->chip_type) {
214 case PCA953X_TYPE:
215 offset = PCA953X_DIRECTION;
216 break;
217 case PCA957X_TYPE:
218 offset = PCA957X_CFG;
219 break;
220 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100221 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800222 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800223 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800224
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100225 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800226 ret = 0;
227exit:
228 mutex_unlock(&chip->i2c_lock);
229 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800230}
231
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800232static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800233 unsigned off, int val)
234{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800235 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100236 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800237 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800238
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800239 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800240
Roland Stigge6e20fb12011-02-10 15:01:23 -0800241 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800242 /* set output level */
243 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100244 reg_val = chip->reg_output[off / BANK_SZ]
245 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800246 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100247 reg_val = chip->reg_output[off / BANK_SZ]
248 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800249
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800250 switch (chip->chip_type) {
251 case PCA953X_TYPE:
252 offset = PCA953X_OUTPUT;
253 break;
254 case PCA957X_TYPE:
255 offset = PCA957X_OUT;
256 break;
257 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100258 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800259 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800260 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800261
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100262 chip->reg_output[off / BANK_SZ] = reg_val;
eric miao9e60fdc2008-02-04 22:28:26 -0800263
264 /* then direction */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100265 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800266 switch (chip->chip_type) {
267 case PCA953X_TYPE:
268 offset = PCA953X_DIRECTION;
269 break;
270 case PCA957X_TYPE:
271 offset = PCA957X_CFG;
272 break;
273 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100274 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800275 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800276 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800277
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100278 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800279 ret = 0;
280exit:
281 mutex_unlock(&chip->i2c_lock);
282 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800283}
284
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800285static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800286{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800287 struct pca953x_chip *chip;
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200288 u32 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800289 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800290
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800291 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800292
Roland Stigge6e20fb12011-02-10 15:01:23 -0800293 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800294 switch (chip->chip_type) {
295 case PCA953X_TYPE:
296 offset = PCA953X_INPUT;
297 break;
298 case PCA957X_TYPE:
299 offset = PCA957X_IN;
300 break;
301 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100302 ret = pca953x_read_single(chip, offset, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800303 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800304 if (ret < 0) {
305 /* NOTE: diagnostic already emitted; that's all we should
306 * do unless gpio_*_value_cansleep() calls become different
307 * from their nonsleeping siblings (and report faults).
308 */
309 return 0;
310 }
311
312 return (reg_val & (1u << off)) ? 1 : 0;
313}
314
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800315static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800316{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800317 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100318 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800319 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800320
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800321 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800322
Roland Stigge6e20fb12011-02-10 15:01:23 -0800323 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800324 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100325 reg_val = chip->reg_output[off / BANK_SZ]
326 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800327 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100328 reg_val = chip->reg_output[off / BANK_SZ]
329 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800330
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800331 switch (chip->chip_type) {
332 case PCA953X_TYPE:
333 offset = PCA953X_OUTPUT;
334 break;
335 case PCA957X_TYPE:
336 offset = PCA957X_OUT;
337 break;
338 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100339 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800340 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800341 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800342
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100343 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800344exit:
345 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800346}
347
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800348static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800349{
350 struct gpio_chip *gc;
351
352 gc = &chip->gpio_chip;
353
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800354 gc->direction_input = pca953x_gpio_direction_input;
355 gc->direction_output = pca953x_gpio_direction_output;
356 gc->get = pca953x_gpio_get_value;
357 gc->set = pca953x_gpio_set_value;
Arnaud Patard84207802008-03-10 11:43:48 -0700358 gc->can_sleep = 1;
eric miao9e60fdc2008-02-04 22:28:26 -0800359
360 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800361 gc->ngpio = gpios;
362 gc->label = chip->client->name;
David Brownelld8f388d82008-07-25 01:46:07 -0700363 gc->dev = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700364 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700365 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800366}
367
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800368#ifdef CONFIG_GPIO_PCA953X_IRQ
369static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
370{
371 struct pca953x_chip *chip;
372
373 chip = container_of(gc, struct pca953x_chip, gpio_chip);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100374 return irq_create_mapping(chip->domain, off);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800375}
376
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800377static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800378{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800379 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800380
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100381 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800382}
383
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800384static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800385{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800386 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800387
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100388 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800389}
390
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800391static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800392{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800393 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800394
395 mutex_lock(&chip->irq_lock);
396}
397
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800398static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800399{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800400 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100401 u8 new_irqs;
402 int level, i;
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700403
404 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100405 for (i = 0; i < NBANK(chip); i++) {
406 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
407 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700408
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100409 while (new_irqs) {
410 level = __ffs(new_irqs);
411 pca953x_gpio_direction_input(&chip->gpio_chip,
412 level + (BANK_SZ * i));
413 new_irqs &= ~(1 << level);
414 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700415 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800416
417 mutex_unlock(&chip->irq_lock);
418}
419
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800420static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800421{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800422 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100423 int bank_nb = d->hwirq / BANK_SZ;
424 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800425
426 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
427 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800428 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800429 return -EINVAL;
430 }
431
432 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100433 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800434 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100435 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800436
437 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100438 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800439 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100440 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800441
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700442 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800443}
444
445static struct irq_chip pca953x_irq_chip = {
446 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800447 .irq_mask = pca953x_irq_mask,
448 .irq_unmask = pca953x_irq_unmask,
449 .irq_bus_lock = pca953x_irq_bus_lock,
450 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
451 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800452};
453
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100454static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800455{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100456 u8 cur_stat[MAX_BANK];
457 u8 old_stat[MAX_BANK];
458 u8 pendings = 0;
459 u8 trigger[MAX_BANK], triggers = 0;
460 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800461
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800462 switch (chip->chip_type) {
463 case PCA953X_TYPE:
464 offset = PCA953X_INPUT;
465 break;
466 case PCA957X_TYPE:
467 offset = PCA957X_IN;
468 break;
469 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100470 ret = pca953x_read_regs(chip, offset, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800471 if (ret)
472 return 0;
473
474 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100475 for (i = 0; i < NBANK(chip); i++)
476 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800477
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100478 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800479
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100480 for (i = 0; i < NBANK(chip); i++) {
481 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
482 triggers += trigger[i];
483 }
484
485 if (!triggers)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800486 return 0;
487
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100488 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800489
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100490 for (i = 0; i < NBANK(chip); i++) {
491 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
492 (cur_stat[i] & chip->irq_trig_raise[i]);
493 pending[i] &= trigger[i];
494 pendings += pending[i];
495 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800496
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100497 return pendings;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800498}
499
500static irqreturn_t pca953x_irq_handler(int irq, void *devid)
501{
502 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100503 u8 pending[MAX_BANK];
504 u8 level;
505 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800506
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100507 if (!pca953x_irq_pending(chip, pending))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800508 return IRQ_HANDLED;
509
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100510 for (i = 0; i < NBANK(chip); i++) {
511 while (pending[i]) {
512 level = __ffs(pending[i]);
513 handle_nested_irq(irq_find_mapping(chip->domain,
514 level + (BANK_SZ * i)));
515 pending[i] &= ~(1 << level);
516 }
517 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800518
519 return IRQ_HANDLED;
520}
521
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100522static int pca953x_gpio_irq_map(struct irq_domain *d, unsigned int irq,
523 irq_hw_number_t hwirq)
524{
525 irq_clear_status_flags(irq, IRQ_NOREQUEST);
526 irq_set_chip_data(irq, d->host_data);
527 irq_set_chip(irq, &pca953x_irq_chip);
528 irq_set_nested_thread(irq, true);
529#ifdef CONFIG_ARM
530 set_irq_flags(irq, IRQF_VALID);
531#else
532 irq_set_noprobe(irq);
533#endif
534
535 return 0;
536}
537
538static const struct irq_domain_ops pca953x_irq_simple_ops = {
539 .map = pca953x_gpio_irq_map,
540 .xlate = irq_domain_xlate_twocell,
541};
542
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800543static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200544 const struct i2c_device_id *id,
545 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800546{
547 struct i2c_client *client = chip->client;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100548 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800549
David Janderc6dcf592011-06-14 11:00:55 +0200550 if (irq_base != -1
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800551 && (id->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800552
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800553 switch (chip->chip_type) {
554 case PCA953X_TYPE:
555 offset = PCA953X_INPUT;
556 break;
557 case PCA957X_TYPE:
558 offset = PCA957X_IN;
559 break;
560 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100561 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800562 if (ret)
563 goto out_failed;
564
565 /*
566 * There is no way to know which GPIO line generated the
567 * interrupt. We have to rely on the previous read for
568 * this purpose.
569 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100570 for (i = 0; i < NBANK(chip); i++)
571 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800572 mutex_init(&chip->irq_lock);
573
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100574 chip->domain = irq_domain_add_simple(client->dev.of_node,
Maxime Ripard55ecd262012-11-08 18:01:51 +0100575 chip->gpio_chip.ngpio,
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100576 irq_base,
577 &pca953x_irq_simple_ops,
Maxime Ripard55ecd262012-11-08 18:01:51 +0100578 NULL);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100579 if (!chip->domain)
580 return -ENODEV;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800581
582 ret = request_threaded_irq(client->irq,
583 NULL,
584 pca953x_irq_handler,
David Jander17e8b422011-06-08 11:34:41 -0600585 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800586 dev_name(&client->dev), chip);
587 if (ret) {
588 dev_err(&client->dev, "failed to request irq %d\n",
589 client->irq);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100590 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800591 }
592
593 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
594 }
595
596 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800597}
598
599static void pca953x_irq_teardown(struct pca953x_chip *chip)
600{
David Janderc609c052011-06-14 11:00:54 +0200601 if (chip->irq_base != -1) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800602 free_irq(chip->client->irq, chip);
David Janderc609c052011-06-14 11:00:54 +0200603 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800604}
605#else /* CONFIG_GPIO_PCA953X_IRQ */
606static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200607 const struct i2c_device_id *id,
608 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800609{
610 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800611
David Janderc6dcf592011-06-14 11:00:55 +0200612 if (irq_base != -1 && (id->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800613 dev_warn(&client->dev, "interrupt support not compiled in\n");
614
615 return 0;
616}
617
618static void pca953x_irq_teardown(struct pca953x_chip *chip)
619{
620}
621#endif
622
Nate Case1965d302009-06-17 16:26:17 -0700623/*
624 * Handlers for alternative sources of platform_data
625 */
626#ifdef CONFIG_OF_GPIO
627/*
628 * Translate OpenFirmware node properties into platform_data
David Jandera57339b2011-06-14 11:00:56 +0200629 * WARNING: This is DEPRECATED and will be removed eventually!
Nate Case1965d302009-06-17 16:26:17 -0700630 */
Mark Brown404ba2b2011-11-21 17:57:25 +0000631static void
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530632pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
Nate Case1965d302009-06-17 16:26:17 -0700633{
Nate Case1965d302009-06-17 16:26:17 -0700634 struct device_node *node;
Dirk Eibach16482372011-02-24 10:20:43 +0100635 const __be32 *val;
636 int size;
Nate Case1965d302009-06-17 16:26:17 -0700637
Grant Likely61c7a082010-04-13 16:12:29 -0700638 node = client->dev.of_node;
Nate Case1965d302009-06-17 16:26:17 -0700639 if (node == NULL)
David Janderc6dcf592011-06-14 11:00:55 +0200640 return;
Nate Case1965d302009-06-17 16:26:17 -0700641
David Janderc6dcf592011-06-14 11:00:55 +0200642 *gpio_base = -1;
Dirk Eibach16482372011-02-24 10:20:43 +0100643 val = of_get_property(node, "linux,gpio-base", &size);
David Jandera57339b2011-06-14 11:00:56 +0200644 WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
Nate Case1965d302009-06-17 16:26:17 -0700645 if (val) {
Dirk Eibach16482372011-02-24 10:20:43 +0100646 if (size != sizeof(*val))
647 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
648 node->full_name);
Nate Case1965d302009-06-17 16:26:17 -0700649 else
David Janderc6dcf592011-06-14 11:00:55 +0200650 *gpio_base = be32_to_cpup(val);
Nate Case1965d302009-06-17 16:26:17 -0700651 }
652
653 val = of_get_property(node, "polarity", NULL);
David Jandera57339b2011-06-14 11:00:56 +0200654 WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
Nate Case1965d302009-06-17 16:26:17 -0700655 if (val)
David Janderc6dcf592011-06-14 11:00:55 +0200656 *invert = *val;
Nate Case1965d302009-06-17 16:26:17 -0700657}
658#else
Mark Brown404ba2b2011-11-21 17:57:25 +0000659static void
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530660pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
Nate Case1965d302009-06-17 16:26:17 -0700661{
Hartmut Knaack25fcf2b2011-10-11 00:22:45 +0200662 *gpio_base = -1;
Nate Case1965d302009-06-17 16:26:17 -0700663}
664#endif
665
Bill Pemberton38363092012-11-19 13:22:34 -0500666static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800667{
668 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100669 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800670
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100671 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800672 if (ret)
673 goto out;
674
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100675 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
676 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800677 if (ret)
678 goto out;
679
680 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100681 if (invert)
682 memset(val, 0xFF, NBANK(chip));
683 else
684 memset(val, 0, NBANK(chip));
685
686 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800687out:
688 return ret;
689}
690
Bill Pemberton38363092012-11-19 13:22:34 -0500691static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800692{
693 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100694 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800695
696 /* Let every port in proper state, that could save power */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100697 memset(val, 0, NBANK(chip));
698 pca953x_write_regs(chip, PCA957X_PUPD, val);
699 memset(val, 0xFF, NBANK(chip));
700 pca953x_write_regs(chip, PCA957X_CFG, val);
701 memset(val, 0, NBANK(chip));
702 pca953x_write_regs(chip, PCA957X_OUT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800703
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100704 ret = pca953x_read_regs(chip, PCA957X_IN, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800705 if (ret)
706 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100707 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800708 if (ret)
709 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100710 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800711 if (ret)
712 goto out;
713
714 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100715 if (invert)
716 memset(val, 0xFF, NBANK(chip));
717 else
718 memset(val, 0, NBANK(chip));
719 pca953x_write_regs(chip, PCA957X_INVRT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800720
721 /* To enable register 6, 7 to controll pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100722 memset(val, 0x02, NBANK(chip));
723 pca953x_write_regs(chip, PCA957X_BKEN, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800724
725 return 0;
726out:
727 return ret;
728}
729
Bill Pemberton38363092012-11-19 13:22:34 -0500730static int pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200731 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800732{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800733 struct pca953x_platform_data *pdata;
734 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530735 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200736 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530737 u32 invert = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800738
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800739 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800740 if (chip == NULL)
741 return -ENOMEM;
742
Nate Case1965d302009-06-17 16:26:17 -0700743 pdata = client->dev.platform_data;
David Janderc6dcf592011-06-14 11:00:55 +0200744 if (pdata) {
745 irq_base = pdata->irq_base;
746 chip->gpio_start = pdata->gpio_base;
747 invert = pdata->invert;
748 chip->names = pdata->names;
749 } else {
750 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
David Jandera57339b2011-06-14 11:00:56 +0200751#ifdef CONFIG_OF_GPIO
752 /* If I2C node has no interrupts property, disable GPIO interrupts */
753 if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
754 irq_base = -1;
755#endif
Nate Case1965d302009-06-17 16:26:17 -0700756 }
757
eric miao9e60fdc2008-02-04 22:28:26 -0800758 chip->client = client;
759
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800760 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700761
Roland Stigge6e20fb12011-02-10 15:01:23 -0800762 mutex_init(&chip->i2c_lock);
763
eric miao9e60fdc2008-02-04 22:28:26 -0800764 /* initialize cached registers from their original values.
765 * we can't share this chip with another i2c master.
766 */
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800767 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800768
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800769 if (chip->chip_type == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200770 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800771 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200772 ret = device_pca957x_init(chip, invert);
773 if (ret)
eric miao9e60fdc2008-02-04 22:28:26 -0800774 goto out_failed;
775
David Janderc6dcf592011-06-14 11:00:55 +0200776 ret = pca953x_irq_setup(chip, id, irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800777 if (ret)
778 goto out_failed;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800779
780 ret = gpiochip_add(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800781 if (ret)
Ben Dooks272df502011-03-31 07:18:46 +0900782 goto out_failed_irq;
eric miao9e60fdc2008-02-04 22:28:26 -0800783
David Janderc6dcf592011-06-14 11:00:55 +0200784 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800785 ret = pdata->setup(client, chip->gpio_chip.base,
786 chip->gpio_chip.ngpio, pdata->context);
787 if (ret < 0)
788 dev_warn(&client->dev, "setup failed, %d\n", ret);
789 }
790
791 i2c_set_clientdata(client, chip);
792 return 0;
793
Ben Dooks272df502011-03-31 07:18:46 +0900794out_failed_irq:
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800795 pca953x_irq_teardown(chip);
Ben Dooks272df502011-03-31 07:18:46 +0900796out_failed:
eric miao9e60fdc2008-02-04 22:28:26 -0800797 kfree(chip);
798 return ret;
799}
800
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800801static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800802{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800803 struct pca953x_platform_data *pdata = client->dev.platform_data;
804 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800805 int ret = 0;
806
David Janderc6dcf592011-06-14 11:00:55 +0200807 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800808 ret = pdata->teardown(client, chip->gpio_chip.base,
809 chip->gpio_chip.ngpio, pdata->context);
810 if (ret < 0) {
811 dev_err(&client->dev, "%s failed, %d\n",
812 "teardown", ret);
813 return ret;
814 }
815 }
816
817 ret = gpiochip_remove(&chip->gpio_chip);
818 if (ret) {
819 dev_err(&client->dev, "%s failed, %d\n",
820 "gpiochip_remove()", ret);
821 return ret;
822 }
823
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800824 pca953x_irq_teardown(chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800825 kfree(chip);
826 return 0;
827}
828
Maxime Riparded326202012-11-08 18:01:52 +0100829static const struct of_device_id pca953x_dt_ids[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +0100830 { .compatible = "nxp,pca9505", },
Maxime Riparded326202012-11-08 18:01:52 +0100831 { .compatible = "nxp,pca9534", },
832 { .compatible = "nxp,pca9535", },
833 { .compatible = "nxp,pca9536", },
834 { .compatible = "nxp,pca9537", },
835 { .compatible = "nxp,pca9538", },
836 { .compatible = "nxp,pca9539", },
837 { .compatible = "nxp,pca9554", },
838 { .compatible = "nxp,pca9555", },
839 { .compatible = "nxp,pca9556", },
840 { .compatible = "nxp,pca9557", },
841 { .compatible = "nxp,pca9574", },
842 { .compatible = "nxp,pca9575", },
843
844 { .compatible = "maxim,max7310", },
845 { .compatible = "maxim,max7312", },
846 { .compatible = "maxim,max7313", },
847 { .compatible = "maxim,max7315", },
848
849 { .compatible = "ti,pca6107", },
850 { .compatible = "ti,tca6408", },
851 { .compatible = "ti,tca6416", },
852 { .compatible = "ti,tca6424", },
853 { }
854};
855
856MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
857
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800858static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800859 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800860 .name = "pca953x",
Maxime Riparded326202012-11-08 18:01:52 +0100861 .of_match_table = pca953x_dt_ids,
eric miao9e60fdc2008-02-04 22:28:26 -0800862 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800863 .probe = pca953x_probe,
864 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200865 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800866};
867
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800868static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800869{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800870 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800871}
David Brownell2f8d1192008-10-15 22:03:13 -0700872/* register after i2c postcore initcall and before
873 * subsys initcalls that may rely on these GPIOs
874 */
875subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800876
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800877static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800878{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800879 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800880}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800881module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800882
883MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800884MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800885MODULE_LICENSE("GPL");