blob: 019b23b955a2884c6f2b63233819cdd4d3beee73 [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>
Vivien Didelot58774572013-08-29 15:24:14 -040021#include <linux/platform_data/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 Brownellab5dc3722009-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);
Laurent Navet50e44432013-03-20 13:15:58 +0100149 } else {
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800150 switch (chip->chip_type) {
151 case PCA953X_TYPE:
152 ret = i2c_smbus_write_word_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100153 reg << 1, (u16) *val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800154 break;
155 case PCA957X_TYPE:
156 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100157 val[0]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800158 if (ret < 0)
159 break;
160 ret = i2c_smbus_write_byte_data(chip->client,
161 (reg << 1) + 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100162 val[1]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800163 break;
164 }
165 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800166
167 if (ret < 0) {
168 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc3722009-01-06 14:42:27 -0800169 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800170 }
171
172 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800173}
174
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100175static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800176{
177 int ret;
178
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200179 if (chip->gpio_chip.ngpio <= 8) {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800180 ret = i2c_smbus_read_byte_data(chip->client, reg);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200181 *val = ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100182 } else if (chip->gpio_chip.ngpio >= 24) {
183 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
184
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200185 ret = i2c_smbus_read_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100186 (reg << bank_shift) | REG_ADDR_AI,
187 NBANK(chip), val);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200188 } else {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800189 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100190 val[0] = (u16)ret & 0xFF;
191 val[1] = (u16)ret >> 8;
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200192 }
eric miao9e60fdc2008-02-04 22:28:26 -0800193 if (ret < 0) {
194 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc3722009-01-06 14:42:27 -0800195 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800196 }
197
eric miao9e60fdc2008-02-04 22:28:26 -0800198 return 0;
199}
200
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800201static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800202{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800203 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100204 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800205 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800206
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800207 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800208
Roland Stigge6e20fb12011-02-10 15:01:23 -0800209 mutex_lock(&chip->i2c_lock);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100210 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800211
212 switch (chip->chip_type) {
213 case PCA953X_TYPE:
214 offset = PCA953X_DIRECTION;
215 break;
216 case PCA957X_TYPE:
217 offset = PCA957X_CFG;
218 break;
219 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100220 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800221 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800222 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800223
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100224 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800225 ret = 0;
226exit:
227 mutex_unlock(&chip->i2c_lock);
228 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800229}
230
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800231static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800232 unsigned off, int val)
233{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800234 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100235 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800236 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800237
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800238 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800239
Roland Stigge6e20fb12011-02-10 15:01:23 -0800240 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800241 /* set output level */
242 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100243 reg_val = chip->reg_output[off / BANK_SZ]
244 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800245 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100246 reg_val = chip->reg_output[off / BANK_SZ]
247 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800248
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800249 switch (chip->chip_type) {
250 case PCA953X_TYPE:
251 offset = PCA953X_OUTPUT;
252 break;
253 case PCA957X_TYPE:
254 offset = PCA957X_OUT;
255 break;
256 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100257 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800258 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800259 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800260
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100261 chip->reg_output[off / BANK_SZ] = reg_val;
eric miao9e60fdc2008-02-04 22:28:26 -0800262
263 /* then direction */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100264 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800265 switch (chip->chip_type) {
266 case PCA953X_TYPE:
267 offset = PCA953X_DIRECTION;
268 break;
269 case PCA957X_TYPE:
270 offset = PCA957X_CFG;
271 break;
272 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100273 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800274 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800275 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800276
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100277 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800278 ret = 0;
279exit:
280 mutex_unlock(&chip->i2c_lock);
281 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800282}
283
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800284static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800285{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800286 struct pca953x_chip *chip;
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200287 u32 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800288 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800289
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800290 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800291
Roland Stigge6e20fb12011-02-10 15:01:23 -0800292 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800293 switch (chip->chip_type) {
294 case PCA953X_TYPE:
295 offset = PCA953X_INPUT;
296 break;
297 case PCA957X_TYPE:
298 offset = PCA957X_IN;
299 break;
300 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100301 ret = pca953x_read_single(chip, offset, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800302 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800303 if (ret < 0) {
304 /* NOTE: diagnostic already emitted; that's all we should
305 * do unless gpio_*_value_cansleep() calls become different
306 * from their nonsleeping siblings (and report faults).
307 */
308 return 0;
309 }
310
Andrew Ruder40a625d2013-08-07 16:52:40 -0500311 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800312}
313
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800314static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800315{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800316 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100317 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800318 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800319
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800320 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800321
Roland Stigge6e20fb12011-02-10 15:01:23 -0800322 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800323 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100324 reg_val = chip->reg_output[off / BANK_SZ]
325 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800326 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100327 reg_val = chip->reg_output[off / BANK_SZ]
328 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800329
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800330 switch (chip->chip_type) {
331 case PCA953X_TYPE:
332 offset = PCA953X_OUTPUT;
333 break;
334 case PCA957X_TYPE:
335 offset = PCA957X_OUT;
336 break;
337 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100338 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800339 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800340 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800341
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100342 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800343exit:
344 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800345}
346
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800347static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800348{
349 struct gpio_chip *gc;
350
351 gc = &chip->gpio_chip;
352
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800353 gc->direction_input = pca953x_gpio_direction_input;
354 gc->direction_output = pca953x_gpio_direction_output;
355 gc->get = pca953x_gpio_get_value;
356 gc->set = pca953x_gpio_set_value;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100357 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800358
359 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800360 gc->ngpio = gpios;
361 gc->label = chip->client->name;
David Brownelld8f388d82008-07-25 01:46:07 -0700362 gc->dev = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700363 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700364 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800365}
366
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800367#ifdef CONFIG_GPIO_PCA953X_IRQ
368static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
369{
370 struct pca953x_chip *chip;
371
372 chip = container_of(gc, struct pca953x_chip, gpio_chip);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100373 return irq_create_mapping(chip->domain, off);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800374}
375
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800376static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800377{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800378 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800379
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100380 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800381}
382
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800383static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800384{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800385 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800386
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100387 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800388}
389
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800390static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800391{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800392 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800393
394 mutex_lock(&chip->irq_lock);
395}
396
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800397static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800398{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800399 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100400 u8 new_irqs;
401 int level, i;
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700402
403 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100404 for (i = 0; i < NBANK(chip); i++) {
405 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
406 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700407
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100408 while (new_irqs) {
409 level = __ffs(new_irqs);
410 pca953x_gpio_direction_input(&chip->gpio_chip,
411 level + (BANK_SZ * i));
412 new_irqs &= ~(1 << level);
413 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700414 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800415
416 mutex_unlock(&chip->irq_lock);
417}
418
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800419static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800420{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800421 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100422 int bank_nb = d->hwirq / BANK_SZ;
423 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800424
425 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
426 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800427 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800428 return -EINVAL;
429 }
430
431 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100432 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800433 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100434 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800435
436 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100437 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800438 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100439 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800440
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700441 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800442}
443
444static struct irq_chip pca953x_irq_chip = {
445 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800446 .irq_mask = pca953x_irq_mask,
447 .irq_unmask = pca953x_irq_unmask,
448 .irq_bus_lock = pca953x_irq_bus_lock,
449 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
450 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800451};
452
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100453static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800454{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100455 u8 cur_stat[MAX_BANK];
456 u8 old_stat[MAX_BANK];
457 u8 pendings = 0;
458 u8 trigger[MAX_BANK], triggers = 0;
459 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800460
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800461 switch (chip->chip_type) {
462 case PCA953X_TYPE:
463 offset = PCA953X_INPUT;
464 break;
465 case PCA957X_TYPE:
466 offset = PCA957X_IN;
467 break;
468 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100469 ret = pca953x_read_regs(chip, offset, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800470 if (ret)
471 return 0;
472
473 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100474 for (i = 0; i < NBANK(chip); i++)
475 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800476
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100477 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800478
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100479 for (i = 0; i < NBANK(chip); i++) {
480 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
481 triggers += trigger[i];
482 }
483
484 if (!triggers)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800485 return 0;
486
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100487 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800488
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100489 for (i = 0; i < NBANK(chip); i++) {
490 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
491 (cur_stat[i] & chip->irq_trig_raise[i]);
492 pending[i] &= trigger[i];
493 pendings += pending[i];
494 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800495
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100496 return pendings;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800497}
498
499static irqreturn_t pca953x_irq_handler(int irq, void *devid)
500{
501 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100502 u8 pending[MAX_BANK];
503 u8 level;
504 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800505
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100506 if (!pca953x_irq_pending(chip, pending))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800507 return IRQ_HANDLED;
508
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100509 for (i = 0; i < NBANK(chip); i++) {
510 while (pending[i]) {
511 level = __ffs(pending[i]);
512 handle_nested_irq(irq_find_mapping(chip->domain,
513 level + (BANK_SZ * i)));
514 pending[i] &= ~(1 << level);
515 }
516 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800517
518 return IRQ_HANDLED;
519}
520
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100521static int pca953x_gpio_irq_map(struct irq_domain *d, unsigned int irq,
522 irq_hw_number_t hwirq)
523{
524 irq_clear_status_flags(irq, IRQ_NOREQUEST);
525 irq_set_chip_data(irq, d->host_data);
526 irq_set_chip(irq, &pca953x_irq_chip);
527 irq_set_nested_thread(irq, true);
528#ifdef CONFIG_ARM
529 set_irq_flags(irq, IRQF_VALID);
530#else
531 irq_set_noprobe(irq);
532#endif
533
534 return 0;
535}
536
537static const struct irq_domain_ops pca953x_irq_simple_ops = {
538 .map = pca953x_gpio_irq_map,
539 .xlate = irq_domain_xlate_twocell,
540};
541
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800542static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200543 const struct i2c_device_id *id,
544 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800545{
546 struct i2c_client *client = chip->client;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100547 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800548
David Janderc6dcf592011-06-14 11:00:55 +0200549 if (irq_base != -1
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800550 && (id->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800551
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800552 switch (chip->chip_type) {
553 case PCA953X_TYPE:
554 offset = PCA953X_INPUT;
555 break;
556 case PCA957X_TYPE:
557 offset = PCA957X_IN;
558 break;
559 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100560 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800561 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100562 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800563
564 /*
565 * There is no way to know which GPIO line generated the
566 * interrupt. We have to rely on the previous read for
567 * this purpose.
568 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100569 for (i = 0; i < NBANK(chip); i++)
570 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800571 mutex_init(&chip->irq_lock);
572
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100573 chip->domain = irq_domain_add_simple(client->dev.of_node,
Maxime Ripard55ecd262012-11-08 18:01:51 +0100574 chip->gpio_chip.ngpio,
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100575 irq_base,
576 &pca953x_irq_simple_ops,
Alexandre Bellonie7a7f972013-04-03 19:40:14 +0200577 chip);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100578 if (!chip->domain)
579 return -ENODEV;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800580
Linus Walleijb42748c2013-01-25 17:59:28 +0100581 ret = devm_request_threaded_irq(&client->dev,
582 client->irq,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800583 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
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800599#else /* CONFIG_GPIO_PCA953X_IRQ */
600static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200601 const struct i2c_device_id *id,
602 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800603{
604 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800605
David Janderc6dcf592011-06-14 11:00:55 +0200606 if (irq_base != -1 && (id->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800607 dev_warn(&client->dev, "interrupt support not compiled in\n");
608
609 return 0;
610}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800611#endif
612
Nate Case1965d302009-06-17 16:26:17 -0700613/*
614 * Handlers for alternative sources of platform_data
615 */
616#ifdef CONFIG_OF_GPIO
617/*
618 * Translate OpenFirmware node properties into platform_data
David Jandera57339b2011-06-14 11:00:56 +0200619 * WARNING: This is DEPRECATED and will be removed eventually!
Nate Case1965d302009-06-17 16:26:17 -0700620 */
Mark Brown404ba2b2011-11-21 17:57:25 +0000621static void
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530622pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
Nate Case1965d302009-06-17 16:26:17 -0700623{
Nate Case1965d302009-06-17 16:26:17 -0700624 struct device_node *node;
Dirk Eibach16482372011-02-24 10:20:43 +0100625 const __be32 *val;
626 int size;
Nate Case1965d302009-06-17 16:26:17 -0700627
Grant Likely61c7a082010-04-13 16:12:29 -0700628 node = client->dev.of_node;
Nate Case1965d302009-06-17 16:26:17 -0700629 if (node == NULL)
David Janderc6dcf592011-06-14 11:00:55 +0200630 return;
Nate Case1965d302009-06-17 16:26:17 -0700631
David Janderc6dcf592011-06-14 11:00:55 +0200632 *gpio_base = -1;
Dirk Eibach16482372011-02-24 10:20:43 +0100633 val = of_get_property(node, "linux,gpio-base", &size);
David Jandera57339b2011-06-14 11:00:56 +0200634 WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
Nate Case1965d302009-06-17 16:26:17 -0700635 if (val) {
Dirk Eibach16482372011-02-24 10:20:43 +0100636 if (size != sizeof(*val))
637 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
638 node->full_name);
Nate Case1965d302009-06-17 16:26:17 -0700639 else
David Janderc6dcf592011-06-14 11:00:55 +0200640 *gpio_base = be32_to_cpup(val);
Nate Case1965d302009-06-17 16:26:17 -0700641 }
642
643 val = of_get_property(node, "polarity", NULL);
David Jandera57339b2011-06-14 11:00:56 +0200644 WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
Nate Case1965d302009-06-17 16:26:17 -0700645 if (val)
David Janderc6dcf592011-06-14 11:00:55 +0200646 *invert = *val;
Nate Case1965d302009-06-17 16:26:17 -0700647}
648#else
Mark Brown404ba2b2011-11-21 17:57:25 +0000649static void
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530650pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
Nate Case1965d302009-06-17 16:26:17 -0700651{
Hartmut Knaack25fcf2b2011-10-11 00:22:45 +0200652 *gpio_base = -1;
Nate Case1965d302009-06-17 16:26:17 -0700653}
654#endif
655
Bill Pemberton38363092012-11-19 13:22:34 -0500656static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800657{
658 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100659 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800660
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100661 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800662 if (ret)
663 goto out;
664
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100665 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
666 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800667 if (ret)
668 goto out;
669
670 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100671 if (invert)
672 memset(val, 0xFF, NBANK(chip));
673 else
674 memset(val, 0, NBANK(chip));
675
676 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800677out:
678 return ret;
679}
680
Bill Pemberton38363092012-11-19 13:22:34 -0500681static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800682{
683 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100684 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800685
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100686 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800687 if (ret)
688 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100689 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800690 if (ret)
691 goto out;
692
693 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100694 if (invert)
695 memset(val, 0xFF, NBANK(chip));
696 else
697 memset(val, 0, NBANK(chip));
698 pca953x_write_regs(chip, PCA957X_INVRT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800699
700 /* To enable register 6, 7 to controll pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100701 memset(val, 0x02, NBANK(chip));
702 pca953x_write_regs(chip, PCA957X_BKEN, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800703
704 return 0;
705out:
706 return ret;
707}
708
Bill Pemberton38363092012-11-19 13:22:34 -0500709static int pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200710 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800711{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800712 struct pca953x_platform_data *pdata;
713 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530714 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200715 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530716 u32 invert = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800717
Linus Walleijb42748c2013-01-25 17:59:28 +0100718 chip = devm_kzalloc(&client->dev,
719 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800720 if (chip == NULL)
721 return -ENOMEM;
722
Jingoo Hane56aee12013-07-30 17:08:05 +0900723 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200724 if (pdata) {
725 irq_base = pdata->irq_base;
726 chip->gpio_start = pdata->gpio_base;
727 invert = pdata->invert;
728 chip->names = pdata->names;
729 } else {
730 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
David Jandera57339b2011-06-14 11:00:56 +0200731#ifdef CONFIG_OF_GPIO
732 /* If I2C node has no interrupts property, disable GPIO interrupts */
733 if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
734 irq_base = -1;
735#endif
Nate Case1965d302009-06-17 16:26:17 -0700736 }
737
eric miao9e60fdc2008-02-04 22:28:26 -0800738 chip->client = client;
739
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800740 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700741
Roland Stigge6e20fb12011-02-10 15:01:23 -0800742 mutex_init(&chip->i2c_lock);
743
eric miao9e60fdc2008-02-04 22:28:26 -0800744 /* initialize cached registers from their original values.
745 * we can't share this chip with another i2c master.
746 */
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800747 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800748
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800749 if (chip->chip_type == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200750 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800751 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200752 ret = device_pca957x_init(chip, invert);
753 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100754 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800755
David Janderc6dcf592011-06-14 11:00:55 +0200756 ret = pca953x_irq_setup(chip, id, irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800757 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100758 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800759
760 ret = gpiochip_add(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800761 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100762 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800763
David Janderc6dcf592011-06-14 11:00:55 +0200764 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800765 ret = pdata->setup(client, chip->gpio_chip.base,
766 chip->gpio_chip.ngpio, pdata->context);
767 if (ret < 0)
768 dev_warn(&client->dev, "setup failed, %d\n", ret);
769 }
770
771 i2c_set_clientdata(client, chip);
772 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800773}
774
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800775static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800776{
Jingoo Hane56aee12013-07-30 17:08:05 +0900777 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800778 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800779 int ret = 0;
780
David Janderc6dcf592011-06-14 11:00:55 +0200781 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800782 ret = pdata->teardown(client, chip->gpio_chip.base,
783 chip->gpio_chip.ngpio, pdata->context);
784 if (ret < 0) {
785 dev_err(&client->dev, "%s failed, %d\n",
786 "teardown", ret);
787 return ret;
788 }
789 }
790
791 ret = gpiochip_remove(&chip->gpio_chip);
792 if (ret) {
793 dev_err(&client->dev, "%s failed, %d\n",
794 "gpiochip_remove()", ret);
795 return ret;
796 }
797
eric miao9e60fdc2008-02-04 22:28:26 -0800798 return 0;
799}
800
Maxime Riparded326202012-11-08 18:01:52 +0100801static const struct of_device_id pca953x_dt_ids[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +0100802 { .compatible = "nxp,pca9505", },
Maxime Riparded326202012-11-08 18:01:52 +0100803 { .compatible = "nxp,pca9534", },
804 { .compatible = "nxp,pca9535", },
805 { .compatible = "nxp,pca9536", },
806 { .compatible = "nxp,pca9537", },
807 { .compatible = "nxp,pca9538", },
808 { .compatible = "nxp,pca9539", },
809 { .compatible = "nxp,pca9554", },
810 { .compatible = "nxp,pca9555", },
811 { .compatible = "nxp,pca9556", },
812 { .compatible = "nxp,pca9557", },
813 { .compatible = "nxp,pca9574", },
814 { .compatible = "nxp,pca9575", },
815
816 { .compatible = "maxim,max7310", },
817 { .compatible = "maxim,max7312", },
818 { .compatible = "maxim,max7313", },
819 { .compatible = "maxim,max7315", },
820
821 { .compatible = "ti,pca6107", },
822 { .compatible = "ti,tca6408", },
823 { .compatible = "ti,tca6416", },
824 { .compatible = "ti,tca6424", },
825 { }
826};
827
828MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
829
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800830static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800831 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800832 .name = "pca953x",
Maxime Riparded326202012-11-08 18:01:52 +0100833 .of_match_table = pca953x_dt_ids,
eric miao9e60fdc2008-02-04 22:28:26 -0800834 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800835 .probe = pca953x_probe,
836 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200837 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800838};
839
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800840static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800841{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800842 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800843}
David Brownell2f8d1192008-10-15 22:03:13 -0700844/* register after i2c postcore initcall and before
845 * subsys initcalls that may rely on these GPIOs
846 */
847subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800848
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800849static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800850{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800851 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800852}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800853module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800854
855MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800856MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800857MODULE_LICENSE("GPL");