blob: 948a0741c2211a334f33ee5e3ce0fe1625d7d2b1 [file] [log] [blame]
eric miao9e60fdc2008-02-04 22:28:26 -08001/*
Aaron Sierra1e191692014-02-07 16:35:48 -06002 * PCA953x 4/8/16/24/40 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, },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +010062 { "pca9698", 40 | PCA953X_TYPE, },
David Brownellab5dc372009-01-06 14:42:27 -080063
Haojian Zhuang33226ff2011-04-18 22:12:46 +080064 { "max7310", 8 | PCA953X_TYPE, },
65 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
66 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
67 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
68 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
69 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
70 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
Andreas Schallenbergae79c192012-05-09 09:46:17 +020071 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
Aaron Sierrae73760a2014-02-07 16:36:21 -060072 { "xra1202", 8 | PCA953X_TYPE },
Jean Delvare3760f732008-04-29 23:11:40 +020073 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080074};
Jean Delvare3760f732008-04-29 23:11:40 +020075MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080076
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010077#define MAX_BANK 5
78#define BANK_SZ 8
79
80#define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)
81
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080082struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080083 unsigned gpio_start;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010084 u8 reg_output[MAX_BANK];
85 u8 reg_direction[MAX_BANK];
Roland Stigge6e20fb12011-02-10 15:01:23 -080086 struct mutex i2c_lock;
eric miao9e60fdc2008-02-04 22:28:26 -080087
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080088#ifdef CONFIG_GPIO_PCA953X_IRQ
89 struct mutex irq_lock;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010090 u8 irq_mask[MAX_BANK];
91 u8 irq_stat[MAX_BANK];
92 u8 irq_trig_raise[MAX_BANK];
93 u8 irq_trig_fall[MAX_BANK];
Maxime Ripard55ecd262012-11-08 18:01:51 +010094 struct irq_domain *domain;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080095#endif
96
eric miao9e60fdc2008-02-04 22:28:26 -080097 struct i2c_client *client;
98 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -070099 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800100 int chip_type;
eric miao9e60fdc2008-02-04 22:28:26 -0800101};
102
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100103static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
104 int off)
105{
106 int ret;
107 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
108 int offset = off / BANK_SZ;
109
110 ret = i2c_smbus_read_byte_data(chip->client,
111 (reg << bank_shift) + offset);
112 *val = ret;
113
114 if (ret < 0) {
115 dev_err(&chip->client->dev, "failed reading register\n");
116 return ret;
117 }
118
119 return 0;
120}
121
122static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
123 int off)
124{
125 int ret = 0;
126 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
127 int offset = off / BANK_SZ;
128
129 ret = i2c_smbus_write_byte_data(chip->client,
130 (reg << bank_shift) + offset, val);
131
132 if (ret < 0) {
133 dev_err(&chip->client->dev, "failed writing register\n");
134 return ret;
135 }
136
137 return 0;
138}
139
140static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800141{
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800142 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800143
144 if (chip->gpio_chip.ngpio <= 8)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100145 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
146 else if (chip->gpio_chip.ngpio >= 24) {
147 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200148 ret = i2c_smbus_write_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100149 (reg << bank_shift) | REG_ADDR_AI,
150 NBANK(chip), val);
Laurent Navet50e44432013-03-20 13:15:58 +0100151 } else {
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800152 switch (chip->chip_type) {
153 case PCA953X_TYPE:
154 ret = i2c_smbus_write_word_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100155 reg << 1, (u16) *val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800156 break;
157 case PCA957X_TYPE:
158 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100159 val[0]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800160 if (ret < 0)
161 break;
162 ret = i2c_smbus_write_byte_data(chip->client,
163 (reg << 1) + 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100164 val[1]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800165 break;
166 }
167 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800168
169 if (ret < 0) {
170 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800171 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800172 }
173
174 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800175}
176
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100177static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800178{
179 int ret;
180
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200181 if (chip->gpio_chip.ngpio <= 8) {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800182 ret = i2c_smbus_read_byte_data(chip->client, reg);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200183 *val = ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100184 } else if (chip->gpio_chip.ngpio >= 24) {
185 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
186
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200187 ret = i2c_smbus_read_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100188 (reg << bank_shift) | REG_ADDR_AI,
189 NBANK(chip), val);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200190 } else {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800191 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100192 val[0] = (u16)ret & 0xFF;
193 val[1] = (u16)ret >> 8;
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200194 }
eric miao9e60fdc2008-02-04 22:28:26 -0800195 if (ret < 0) {
196 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800197 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800198 }
199
eric miao9e60fdc2008-02-04 22:28:26 -0800200 return 0;
201}
202
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800203static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800204{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800205 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100206 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800207 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800208
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800209 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800210
Roland Stigge6e20fb12011-02-10 15:01:23 -0800211 mutex_lock(&chip->i2c_lock);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100212 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800213
214 switch (chip->chip_type) {
215 case PCA953X_TYPE:
216 offset = PCA953X_DIRECTION;
217 break;
218 case PCA957X_TYPE:
219 offset = PCA957X_CFG;
220 break;
221 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100222 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800223 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800224 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800225
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100226 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800227 ret = 0;
228exit:
229 mutex_unlock(&chip->i2c_lock);
230 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800231}
232
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800233static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800234 unsigned off, int val)
235{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800236 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100237 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800238 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800239
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800240 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800241
Roland Stigge6e20fb12011-02-10 15:01:23 -0800242 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800243 /* set output level */
244 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100245 reg_val = chip->reg_output[off / BANK_SZ]
246 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800247 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100248 reg_val = chip->reg_output[off / BANK_SZ]
249 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800250
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800251 switch (chip->chip_type) {
252 case PCA953X_TYPE:
253 offset = PCA953X_OUTPUT;
254 break;
255 case PCA957X_TYPE:
256 offset = PCA957X_OUT;
257 break;
258 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100259 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800260 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800261 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800262
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100263 chip->reg_output[off / BANK_SZ] = reg_val;
eric miao9e60fdc2008-02-04 22:28:26 -0800264
265 /* then direction */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100266 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800267 switch (chip->chip_type) {
268 case PCA953X_TYPE:
269 offset = PCA953X_DIRECTION;
270 break;
271 case PCA957X_TYPE:
272 offset = PCA957X_CFG;
273 break;
274 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100275 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800276 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800277 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800278
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100279 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800280 ret = 0;
281exit:
282 mutex_unlock(&chip->i2c_lock);
283 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800284}
285
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800286static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800287{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800288 struct pca953x_chip *chip;
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200289 u32 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800290 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800291
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800292 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800293
Roland Stigge6e20fb12011-02-10 15:01:23 -0800294 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800295 switch (chip->chip_type) {
296 case PCA953X_TYPE:
297 offset = PCA953X_INPUT;
298 break;
299 case PCA957X_TYPE:
300 offset = PCA957X_IN;
301 break;
302 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100303 ret = pca953x_read_single(chip, offset, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800304 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800305 if (ret < 0) {
306 /* NOTE: diagnostic already emitted; that's all we should
307 * do unless gpio_*_value_cansleep() calls become different
308 * from their nonsleeping siblings (and report faults).
309 */
310 return 0;
311 }
312
Andrew Ruder40a625d2013-08-07 16:52:40 -0500313 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800314}
315
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800316static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800317{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800318 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100319 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800320 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800321
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800322 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800323
Roland Stigge6e20fb12011-02-10 15:01:23 -0800324 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800325 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100326 reg_val = chip->reg_output[off / BANK_SZ]
327 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800328 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100329 reg_val = chip->reg_output[off / BANK_SZ]
330 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800331
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800332 switch (chip->chip_type) {
333 case PCA953X_TYPE:
334 offset = PCA953X_OUTPUT;
335 break;
336 case PCA957X_TYPE:
337 offset = PCA957X_OUT;
338 break;
339 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100340 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800341 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800342 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800343
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100344 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800345exit:
346 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800347}
348
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800349static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800350{
351 struct gpio_chip *gc;
352
353 gc = &chip->gpio_chip;
354
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800355 gc->direction_input = pca953x_gpio_direction_input;
356 gc->direction_output = pca953x_gpio_direction_output;
357 gc->get = pca953x_gpio_get_value;
358 gc->set = pca953x_gpio_set_value;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100359 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800360
361 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800362 gc->ngpio = gpios;
363 gc->label = chip->client->name;
David Brownelld8f388d82008-07-25 01:46:07 -0700364 gc->dev = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700365 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700366 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800367}
368
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800369#ifdef CONFIG_GPIO_PCA953X_IRQ
370static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
371{
372 struct pca953x_chip *chip;
373
374 chip = container_of(gc, struct pca953x_chip, gpio_chip);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100375 return irq_create_mapping(chip->domain, off);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800376}
377
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800378static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800379{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800380 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800381
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100382 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800383}
384
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800385static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800386{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800387 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800388
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100389 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800390}
391
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800392static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800393{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800394 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800395
396 mutex_lock(&chip->irq_lock);
397}
398
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800399static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800400{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800401 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100402 u8 new_irqs;
403 int level, i;
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700404
405 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100406 for (i = 0; i < NBANK(chip); i++) {
407 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
408 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700409
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100410 while (new_irqs) {
411 level = __ffs(new_irqs);
412 pca953x_gpio_direction_input(&chip->gpio_chip,
413 level + (BANK_SZ * i));
414 new_irqs &= ~(1 << level);
415 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700416 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800417
418 mutex_unlock(&chip->irq_lock);
419}
420
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800421static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800422{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800423 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100424 int bank_nb = d->hwirq / BANK_SZ;
425 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800426
427 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
428 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800429 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800430 return -EINVAL;
431 }
432
433 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100434 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800435 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100436 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800437
438 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100439 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800440 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100441 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800442
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700443 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800444}
445
446static struct irq_chip pca953x_irq_chip = {
447 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800448 .irq_mask = pca953x_irq_mask,
449 .irq_unmask = pca953x_irq_unmask,
450 .irq_bus_lock = pca953x_irq_bus_lock,
451 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
452 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800453};
454
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100455static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800456{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100457 u8 cur_stat[MAX_BANK];
458 u8 old_stat[MAX_BANK];
459 u8 pendings = 0;
460 u8 trigger[MAX_BANK], triggers = 0;
461 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800462
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800463 switch (chip->chip_type) {
464 case PCA953X_TYPE:
465 offset = PCA953X_INPUT;
466 break;
467 case PCA957X_TYPE:
468 offset = PCA957X_IN;
469 break;
470 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100471 ret = pca953x_read_regs(chip, offset, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800472 if (ret)
473 return 0;
474
475 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100476 for (i = 0; i < NBANK(chip); i++)
477 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800478
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100479 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800480
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100481 for (i = 0; i < NBANK(chip); i++) {
482 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
483 triggers += trigger[i];
484 }
485
486 if (!triggers)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800487 return 0;
488
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100489 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800490
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100491 for (i = 0; i < NBANK(chip); i++) {
492 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
493 (cur_stat[i] & chip->irq_trig_raise[i]);
494 pending[i] &= trigger[i];
495 pendings += pending[i];
496 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800497
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100498 return pendings;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800499}
500
501static irqreturn_t pca953x_irq_handler(int irq, void *devid)
502{
503 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100504 u8 pending[MAX_BANK];
505 u8 level;
506 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800507
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100508 if (!pca953x_irq_pending(chip, pending))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800509 return IRQ_HANDLED;
510
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100511 for (i = 0; i < NBANK(chip); i++) {
512 while (pending[i]) {
513 level = __ffs(pending[i]);
514 handle_nested_irq(irq_find_mapping(chip->domain,
515 level + (BANK_SZ * i)));
516 pending[i] &= ~(1 << level);
517 }
518 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800519
520 return IRQ_HANDLED;
521}
522
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100523static int pca953x_gpio_irq_map(struct irq_domain *d, unsigned int irq,
524 irq_hw_number_t hwirq)
525{
526 irq_clear_status_flags(irq, IRQ_NOREQUEST);
527 irq_set_chip_data(irq, d->host_data);
528 irq_set_chip(irq, &pca953x_irq_chip);
529 irq_set_nested_thread(irq, true);
530#ifdef CONFIG_ARM
531 set_irq_flags(irq, IRQF_VALID);
532#else
533 irq_set_noprobe(irq);
534#endif
535
536 return 0;
537}
538
539static const struct irq_domain_ops pca953x_irq_simple_ops = {
540 .map = pca953x_gpio_irq_map,
541 .xlate = irq_domain_xlate_twocell,
542};
543
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800544static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200545 const struct i2c_device_id *id,
546 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800547{
548 struct i2c_client *client = chip->client;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100549 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800550
David Janderc6dcf592011-06-14 11:00:55 +0200551 if (irq_base != -1
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800552 && (id->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800553
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800554 switch (chip->chip_type) {
555 case PCA953X_TYPE:
556 offset = PCA953X_INPUT;
557 break;
558 case PCA957X_TYPE:
559 offset = PCA957X_IN;
560 break;
561 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100562 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800563 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100564 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800565
566 /*
567 * There is no way to know which GPIO line generated the
568 * interrupt. We have to rely on the previous read for
569 * this purpose.
570 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100571 for (i = 0; i < NBANK(chip); i++)
572 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800573 mutex_init(&chip->irq_lock);
574
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100575 chip->domain = irq_domain_add_simple(client->dev.of_node,
Maxime Ripard55ecd262012-11-08 18:01:51 +0100576 chip->gpio_chip.ngpio,
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100577 irq_base,
578 &pca953x_irq_simple_ops,
Alexandre Bellonie7a7f972013-04-03 19:40:14 +0200579 chip);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100580 if (!chip->domain)
581 return -ENODEV;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800582
Linus Walleijb42748c2013-01-25 17:59:28 +0100583 ret = devm_request_threaded_irq(&client->dev,
584 client->irq,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800585 NULL,
586 pca953x_irq_handler,
David Jander17e8b422011-06-08 11:34:41 -0600587 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800588 dev_name(&client->dev), chip);
589 if (ret) {
590 dev_err(&client->dev, "failed to request irq %d\n",
591 client->irq);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100592 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800593 }
594
595 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
596 }
597
598 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800599}
600
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800601#else /* CONFIG_GPIO_PCA953X_IRQ */
602static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200603 const struct i2c_device_id *id,
604 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800605{
606 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800607
David Janderc6dcf592011-06-14 11:00:55 +0200608 if (irq_base != -1 && (id->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800609 dev_warn(&client->dev, "interrupt support not compiled in\n");
610
611 return 0;
612}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800613#endif
614
Nate Case1965d302009-06-17 16:26:17 -0700615/*
616 * Handlers for alternative sources of platform_data
617 */
618#ifdef CONFIG_OF_GPIO
619/*
620 * Translate OpenFirmware node properties into platform_data
David Jandera57339b2011-06-14 11:00:56 +0200621 * WARNING: This is DEPRECATED and will be removed eventually!
Nate Case1965d302009-06-17 16:26:17 -0700622 */
Mark Brown404ba2b2011-11-21 17:57:25 +0000623static void
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530624pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
Nate Case1965d302009-06-17 16:26:17 -0700625{
Nate Case1965d302009-06-17 16:26:17 -0700626 struct device_node *node;
Dirk Eibach16482372011-02-24 10:20:43 +0100627 const __be32 *val;
628 int size;
Nate Case1965d302009-06-17 16:26:17 -0700629
Grant Likely61c7a082010-04-13 16:12:29 -0700630 node = client->dev.of_node;
Nate Case1965d302009-06-17 16:26:17 -0700631 if (node == NULL)
David Janderc6dcf592011-06-14 11:00:55 +0200632 return;
Nate Case1965d302009-06-17 16:26:17 -0700633
David Janderc6dcf592011-06-14 11:00:55 +0200634 *gpio_base = -1;
Dirk Eibach16482372011-02-24 10:20:43 +0100635 val = of_get_property(node, "linux,gpio-base", &size);
David Jandera57339b2011-06-14 11:00:56 +0200636 WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
Nate Case1965d302009-06-17 16:26:17 -0700637 if (val) {
Dirk Eibach16482372011-02-24 10:20:43 +0100638 if (size != sizeof(*val))
639 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
640 node->full_name);
Nate Case1965d302009-06-17 16:26:17 -0700641 else
David Janderc6dcf592011-06-14 11:00:55 +0200642 *gpio_base = be32_to_cpup(val);
Nate Case1965d302009-06-17 16:26:17 -0700643 }
644
645 val = of_get_property(node, "polarity", NULL);
David Jandera57339b2011-06-14 11:00:56 +0200646 WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
Nate Case1965d302009-06-17 16:26:17 -0700647 if (val)
David Janderc6dcf592011-06-14 11:00:55 +0200648 *invert = *val;
Nate Case1965d302009-06-17 16:26:17 -0700649}
650#else
Mark Brown404ba2b2011-11-21 17:57:25 +0000651static void
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530652pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
Nate Case1965d302009-06-17 16:26:17 -0700653{
Hartmut Knaack25fcf2b2011-10-11 00:22:45 +0200654 *gpio_base = -1;
Nate Case1965d302009-06-17 16:26:17 -0700655}
656#endif
657
Bill Pemberton38363092012-11-19 13:22:34 -0500658static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800659{
660 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100661 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800662
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100663 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800664 if (ret)
665 goto out;
666
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100667 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
668 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800669 if (ret)
670 goto out;
671
672 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100673 if (invert)
674 memset(val, 0xFF, NBANK(chip));
675 else
676 memset(val, 0, NBANK(chip));
677
678 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800679out:
680 return ret;
681}
682
Bill Pemberton38363092012-11-19 13:22:34 -0500683static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800684{
685 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100686 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800687
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100688 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800689 if (ret)
690 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100691 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800692 if (ret)
693 goto out;
694
695 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100696 if (invert)
697 memset(val, 0xFF, NBANK(chip));
698 else
699 memset(val, 0, NBANK(chip));
700 pca953x_write_regs(chip, PCA957X_INVRT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800701
702 /* To enable register 6, 7 to controll pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100703 memset(val, 0x02, NBANK(chip));
704 pca953x_write_regs(chip, PCA957X_BKEN, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800705
706 return 0;
707out:
708 return ret;
709}
710
Bill Pemberton38363092012-11-19 13:22:34 -0500711static int pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200712 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800713{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800714 struct pca953x_platform_data *pdata;
715 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530716 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200717 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530718 u32 invert = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800719
Linus Walleijb42748c2013-01-25 17:59:28 +0100720 chip = devm_kzalloc(&client->dev,
721 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800722 if (chip == NULL)
723 return -ENOMEM;
724
Jingoo Hane56aee12013-07-30 17:08:05 +0900725 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200726 if (pdata) {
727 irq_base = pdata->irq_base;
728 chip->gpio_start = pdata->gpio_base;
729 invert = pdata->invert;
730 chip->names = pdata->names;
731 } else {
732 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
David Jandera57339b2011-06-14 11:00:56 +0200733#ifdef CONFIG_OF_GPIO
734 /* If I2C node has no interrupts property, disable GPIO interrupts */
735 if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
736 irq_base = -1;
737#endif
Nate Case1965d302009-06-17 16:26:17 -0700738 }
739
eric miao9e60fdc2008-02-04 22:28:26 -0800740 chip->client = client;
741
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800742 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700743
Roland Stigge6e20fb12011-02-10 15:01:23 -0800744 mutex_init(&chip->i2c_lock);
745
eric miao9e60fdc2008-02-04 22:28:26 -0800746 /* initialize cached registers from their original values.
747 * we can't share this chip with another i2c master.
748 */
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800749 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800750
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800751 if (chip->chip_type == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200752 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800753 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200754 ret = device_pca957x_init(chip, invert);
755 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100756 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800757
David Janderc6dcf592011-06-14 11:00:55 +0200758 ret = pca953x_irq_setup(chip, id, irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800759 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100760 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800761
762 ret = gpiochip_add(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800763 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100764 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800765
David Janderc6dcf592011-06-14 11:00:55 +0200766 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800767 ret = pdata->setup(client, chip->gpio_chip.base,
768 chip->gpio_chip.ngpio, pdata->context);
769 if (ret < 0)
770 dev_warn(&client->dev, "setup failed, %d\n", ret);
771 }
772
773 i2c_set_clientdata(client, chip);
774 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800775}
776
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800777static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800778{
Jingoo Hane56aee12013-07-30 17:08:05 +0900779 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800780 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800781 int ret = 0;
782
David Janderc6dcf592011-06-14 11:00:55 +0200783 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800784 ret = pdata->teardown(client, chip->gpio_chip.base,
785 chip->gpio_chip.ngpio, pdata->context);
786 if (ret < 0) {
787 dev_err(&client->dev, "%s failed, %d\n",
788 "teardown", ret);
789 return ret;
790 }
791 }
792
793 ret = gpiochip_remove(&chip->gpio_chip);
794 if (ret) {
795 dev_err(&client->dev, "%s failed, %d\n",
796 "gpiochip_remove()", ret);
797 return ret;
798 }
799
eric miao9e60fdc2008-02-04 22:28:26 -0800800 return 0;
801}
802
Maxime Riparded326202012-11-08 18:01:52 +0100803static const struct of_device_id pca953x_dt_ids[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +0100804 { .compatible = "nxp,pca9505", },
Maxime Riparded326202012-11-08 18:01:52 +0100805 { .compatible = "nxp,pca9534", },
806 { .compatible = "nxp,pca9535", },
807 { .compatible = "nxp,pca9536", },
808 { .compatible = "nxp,pca9537", },
809 { .compatible = "nxp,pca9538", },
810 { .compatible = "nxp,pca9539", },
811 { .compatible = "nxp,pca9554", },
812 { .compatible = "nxp,pca9555", },
813 { .compatible = "nxp,pca9556", },
814 { .compatible = "nxp,pca9557", },
815 { .compatible = "nxp,pca9574", },
816 { .compatible = "nxp,pca9575", },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +0100817 { .compatible = "nxp,pca9698", },
Maxime Riparded326202012-11-08 18:01:52 +0100818
819 { .compatible = "maxim,max7310", },
820 { .compatible = "maxim,max7312", },
821 { .compatible = "maxim,max7313", },
822 { .compatible = "maxim,max7315", },
823
824 { .compatible = "ti,pca6107", },
825 { .compatible = "ti,tca6408", },
826 { .compatible = "ti,tca6416", },
827 { .compatible = "ti,tca6424", },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600828
829 { .compatible = "exar,xra1202", },
Maxime Riparded326202012-11-08 18:01:52 +0100830 { }
831};
832
833MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
834
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800835static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800836 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800837 .name = "pca953x",
Maxime Riparded326202012-11-08 18:01:52 +0100838 .of_match_table = pca953x_dt_ids,
eric miao9e60fdc2008-02-04 22:28:26 -0800839 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800840 .probe = pca953x_probe,
841 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200842 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800843};
844
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800845static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800846{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800847 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800848}
David Brownell2f8d1192008-10-15 22:03:13 -0700849/* register after i2c postcore initcall and before
850 * subsys initcalls that may rely on these GPIOs
851 */
852subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800853
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800854static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800855{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800856 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800857}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800858module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800859
860MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800861MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800862MODULE_LICENSE("GPL");