blob: e721a37c3473efb5168dcb8c664fe1abe0c2a6d7 [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>
eric miao9e60fdc2008-02-04 22:28:26 -080018#include <linux/i2c.h>
Vivien Didelot58774572013-08-29 15:24:14 -040019#include <linux/platform_data/pca953x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Nate Case1965d302009-06-17 16:26:17 -070021#ifdef CONFIG_OF_GPIO
22#include <linux/of_platform.h>
Nate Case1965d302009-06-17 16:26:17 -070023#endif
eric miao9e60fdc2008-02-04 22:28:26 -080024
Haojian Zhuang33226ff2011-04-18 22:12:46 +080025#define PCA953X_INPUT 0
26#define PCA953X_OUTPUT 1
27#define PCA953X_INVERT 2
28#define PCA953X_DIRECTION 3
eric miao9e60fdc2008-02-04 22:28:26 -080029
Andreas Schallenbergae79c192012-05-09 09:46:17 +020030#define REG_ADDR_AI 0x80
31
Haojian Zhuang33226ff2011-04-18 22:12:46 +080032#define PCA957X_IN 0
33#define PCA957X_INVRT 1
34#define PCA957X_BKEN 2
35#define PCA957X_PUPD 3
36#define PCA957X_CFG 4
37#define PCA957X_OUT 5
38#define PCA957X_MSK 6
39#define PCA957X_INTS 7
40
41#define PCA_GPIO_MASK 0x00FF
42#define PCA_INT 0x0100
43#define PCA953X_TYPE 0x1000
44#define PCA957X_TYPE 0x2000
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080045
Jean Delvare3760f732008-04-29 23:11:40 +020046static const struct i2c_device_id pca953x_id[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +010047 { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080048 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
49 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
50 { "pca9536", 4 | PCA953X_TYPE, },
51 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
52 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
53 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
54 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
55 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
56 { "pca9556", 8 | PCA953X_TYPE, },
57 { "pca9557", 8 | PCA953X_TYPE, },
58 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
59 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +010060 { "pca9698", 40 | PCA953X_TYPE, },
David Brownellab5dc372009-01-06 14:42:27 -080061
Haojian Zhuang33226ff2011-04-18 22:12:46 +080062 { "max7310", 8 | PCA953X_TYPE, },
63 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
64 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
65 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
66 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
67 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
68 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
Andreas Schallenbergae79c192012-05-09 09:46:17 +020069 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
Aaron Sierrae73760a2014-02-07 16:36:21 -060070 { "xra1202", 8 | PCA953X_TYPE },
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];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080092#endif
93
eric miao9e60fdc2008-02-04 22:28:26 -080094 struct i2c_client *client;
95 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -070096 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +080097 int chip_type;
eric miao9e60fdc2008-02-04 22:28:26 -080098};
99
Linus Walleij7bcbce52014-05-09 13:27:57 +0200100static inline struct pca953x_chip *to_pca(struct gpio_chip *gc)
101{
102 return container_of(gc, struct pca953x_chip, gpio_chip);
103}
104
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100105static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
106 int off)
107{
108 int ret;
109 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
110 int offset = off / BANK_SZ;
111
112 ret = i2c_smbus_read_byte_data(chip->client,
113 (reg << bank_shift) + offset);
114 *val = ret;
115
116 if (ret < 0) {
117 dev_err(&chip->client->dev, "failed reading register\n");
118 return ret;
119 }
120
121 return 0;
122}
123
124static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
125 int off)
126{
127 int ret = 0;
128 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
129 int offset = off / BANK_SZ;
130
131 ret = i2c_smbus_write_byte_data(chip->client,
132 (reg << bank_shift) + offset, val);
133
134 if (ret < 0) {
135 dev_err(&chip->client->dev, "failed writing register\n");
136 return ret;
137 }
138
139 return 0;
140}
141
142static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800143{
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800144 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800145
146 if (chip->gpio_chip.ngpio <= 8)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100147 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
148 else if (chip->gpio_chip.ngpio >= 24) {
149 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200150 ret = i2c_smbus_write_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100151 (reg << bank_shift) | REG_ADDR_AI,
152 NBANK(chip), val);
Laurent Navet50e44432013-03-20 13:15:58 +0100153 } else {
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800154 switch (chip->chip_type) {
155 case PCA953X_TYPE:
156 ret = i2c_smbus_write_word_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100157 reg << 1, (u16) *val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800158 break;
159 case PCA957X_TYPE:
160 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100161 val[0]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800162 if (ret < 0)
163 break;
164 ret = i2c_smbus_write_byte_data(chip->client,
165 (reg << 1) + 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100166 val[1]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800167 break;
168 }
169 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800170
171 if (ret < 0) {
172 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800173 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800174 }
175
176 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800177}
178
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100179static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800180{
181 int ret;
182
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200183 if (chip->gpio_chip.ngpio <= 8) {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800184 ret = i2c_smbus_read_byte_data(chip->client, reg);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200185 *val = ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100186 } else if (chip->gpio_chip.ngpio >= 24) {
187 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
188
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200189 ret = i2c_smbus_read_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100190 (reg << bank_shift) | REG_ADDR_AI,
191 NBANK(chip), val);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200192 } else {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800193 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100194 val[0] = (u16)ret & 0xFF;
195 val[1] = (u16)ret >> 8;
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200196 }
eric miao9e60fdc2008-02-04 22:28:26 -0800197 if (ret < 0) {
198 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800199 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800200 }
201
eric miao9e60fdc2008-02-04 22:28:26 -0800202 return 0;
203}
204
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800205static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800206{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200207 struct pca953x_chip *chip = to_pca(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100208 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800209 int ret, offset = 0;
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{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200236 struct pca953x_chip *chip = to_pca(gc);
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
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{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200286 struct pca953x_chip *chip = to_pca(gc);
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
Roland Stigge6e20fb12011-02-10 15:01:23 -0800290 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800291 switch (chip->chip_type) {
292 case PCA953X_TYPE:
293 offset = PCA953X_INPUT;
294 break;
295 case PCA957X_TYPE:
296 offset = PCA957X_IN;
297 break;
298 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100299 ret = pca953x_read_single(chip, offset, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800300 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800301 if (ret < 0) {
302 /* NOTE: diagnostic already emitted; that's all we should
303 * do unless gpio_*_value_cansleep() calls become different
304 * from their nonsleeping siblings (and report faults).
305 */
306 return 0;
307 }
308
Andrew Ruder40a625d2013-08-07 16:52:40 -0500309 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800310}
311
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800312static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800313{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200314 struct pca953x_chip *chip = to_pca(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100315 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800316 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800317
Roland Stigge6e20fb12011-02-10 15:01:23 -0800318 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800319 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100320 reg_val = chip->reg_output[off / BANK_SZ]
321 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800322 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100323 reg_val = chip->reg_output[off / BANK_SZ]
324 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800325
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800326 switch (chip->chip_type) {
327 case PCA953X_TYPE:
328 offset = PCA953X_OUTPUT;
329 break;
330 case PCA957X_TYPE:
331 offset = PCA957X_OUT;
332 break;
333 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100334 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800335 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800336 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800337
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100338 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800339exit:
340 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800341}
342
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800343static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800344{
345 struct gpio_chip *gc;
346
347 gc = &chip->gpio_chip;
348
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800349 gc->direction_input = pca953x_gpio_direction_input;
350 gc->direction_output = pca953x_gpio_direction_output;
351 gc->get = pca953x_gpio_get_value;
352 gc->set = pca953x_gpio_set_value;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100353 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800354
355 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800356 gc->ngpio = gpios;
357 gc->label = chip->client->name;
David Brownelld8f388d2008-07-25 01:46:07 -0700358 gc->dev = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700359 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700360 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800361}
362
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800363#ifdef CONFIG_GPIO_PCA953X_IRQ
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800364static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800365{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200366 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
367 struct pca953x_chip *chip = to_pca(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800368
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100369 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800370}
371
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800372static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800373{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200374 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
375 struct pca953x_chip *chip = to_pca(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800376
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100377 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800378}
379
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800380static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800381{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200382 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
383 struct pca953x_chip *chip = to_pca(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800384
385 mutex_lock(&chip->irq_lock);
386}
387
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800388static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800389{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200390 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
391 struct pca953x_chip *chip = to_pca(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100392 u8 new_irqs;
393 int level, i;
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700394
395 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100396 for (i = 0; i < NBANK(chip); i++) {
397 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
398 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700399
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100400 while (new_irqs) {
401 level = __ffs(new_irqs);
402 pca953x_gpio_direction_input(&chip->gpio_chip,
403 level + (BANK_SZ * i));
404 new_irqs &= ~(1 << level);
405 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700406 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800407
408 mutex_unlock(&chip->irq_lock);
409}
410
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800411static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800412{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200413 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
414 struct pca953x_chip *chip = to_pca(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100415 int bank_nb = d->hwirq / BANK_SZ;
416 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800417
418 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
419 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800420 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800421 return -EINVAL;
422 }
423
424 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100425 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800426 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100427 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800428
429 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100430 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800431 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100432 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800433
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700434 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800435}
436
437static struct irq_chip pca953x_irq_chip = {
438 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800439 .irq_mask = pca953x_irq_mask,
440 .irq_unmask = pca953x_irq_unmask,
441 .irq_bus_lock = pca953x_irq_bus_lock,
442 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
443 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800444};
445
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100446static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800447{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100448 u8 cur_stat[MAX_BANK];
449 u8 old_stat[MAX_BANK];
450 u8 pendings = 0;
451 u8 trigger[MAX_BANK], triggers = 0;
452 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800453
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800454 switch (chip->chip_type) {
455 case PCA953X_TYPE:
456 offset = PCA953X_INPUT;
457 break;
458 case PCA957X_TYPE:
459 offset = PCA957X_IN;
460 break;
461 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100462 ret = pca953x_read_regs(chip, offset, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800463 if (ret)
464 return 0;
465
466 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100467 for (i = 0; i < NBANK(chip); i++)
468 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800469
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100470 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800471
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100472 for (i = 0; i < NBANK(chip); i++) {
473 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
474 triggers += trigger[i];
475 }
476
477 if (!triggers)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800478 return 0;
479
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100480 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800481
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100482 for (i = 0; i < NBANK(chip); i++) {
483 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
484 (cur_stat[i] & chip->irq_trig_raise[i]);
485 pending[i] &= trigger[i];
486 pendings += pending[i];
487 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800488
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100489 return pendings;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800490}
491
492static irqreturn_t pca953x_irq_handler(int irq, void *devid)
493{
494 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100495 u8 pending[MAX_BANK];
496 u8 level;
Toby Smith3275d072014-04-30 18:01:40 +1000497 unsigned nhandled = 0;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100498 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800499
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100500 if (!pca953x_irq_pending(chip, pending))
Toby Smith3275d072014-04-30 18:01:40 +1000501 return IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800502
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100503 for (i = 0; i < NBANK(chip); i++) {
504 while (pending[i]) {
505 level = __ffs(pending[i]);
Linus Walleij7bcbce52014-05-09 13:27:57 +0200506 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100507 level + (BANK_SZ * i)));
508 pending[i] &= ~(1 << level);
Toby Smith3275d072014-04-30 18:01:40 +1000509 nhandled++;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100510 }
511 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800512
Toby Smith3275d072014-04-30 18:01:40 +1000513 return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800514}
515
516static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200517 const struct i2c_device_id *id,
518 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800519{
520 struct i2c_client *client = chip->client;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100521 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800522
David Janderc6dcf592011-06-14 11:00:55 +0200523 if (irq_base != -1
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800524 && (id->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800525
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800526 switch (chip->chip_type) {
527 case PCA953X_TYPE:
528 offset = PCA953X_INPUT;
529 break;
530 case PCA957X_TYPE:
531 offset = PCA957X_IN;
532 break;
533 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100534 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800535 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100536 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800537
538 /*
539 * There is no way to know which GPIO line generated the
540 * interrupt. We have to rely on the previous read for
541 * this purpose.
542 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100543 for (i = 0; i < NBANK(chip); i++)
544 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800545 mutex_init(&chip->irq_lock);
546
Linus Walleijb42748c2013-01-25 17:59:28 +0100547 ret = devm_request_threaded_irq(&client->dev,
548 client->irq,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800549 NULL,
550 pca953x_irq_handler,
Toby Smith91329132014-04-30 18:01:41 +1000551 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
552 IRQF_SHARED,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800553 dev_name(&client->dev), chip);
554 if (ret) {
555 dev_err(&client->dev, "failed to request irq %d\n",
556 client->irq);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100557 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800558 }
559
Linus Walleij7bcbce52014-05-09 13:27:57 +0200560 ret = gpiochip_irqchip_add(&chip->gpio_chip,
561 &pca953x_irq_chip,
562 irq_base,
563 handle_simple_irq,
564 IRQ_TYPE_NONE);
565 if (ret) {
566 dev_err(&client->dev,
567 "could not connect irqchip to gpiochip\n");
568 return ret;
569 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800570 }
571
572 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800573}
574
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800575#else /* CONFIG_GPIO_PCA953X_IRQ */
576static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200577 const struct i2c_device_id *id,
578 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800579{
580 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800581
David Janderc6dcf592011-06-14 11:00:55 +0200582 if (irq_base != -1 && (id->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800583 dev_warn(&client->dev, "interrupt support not compiled in\n");
584
585 return 0;
586}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800587#endif
588
Nate Case1965d302009-06-17 16:26:17 -0700589/*
590 * Handlers for alternative sources of platform_data
591 */
592#ifdef CONFIG_OF_GPIO
593/*
594 * Translate OpenFirmware node properties into platform_data
David Jandera57339b2011-06-14 11:00:56 +0200595 * WARNING: This is DEPRECATED and will be removed eventually!
Nate Case1965d302009-06-17 16:26:17 -0700596 */
Mark Brown404ba2b2011-11-21 17:57:25 +0000597static void
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530598pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
Nate Case1965d302009-06-17 16:26:17 -0700599{
Nate Case1965d302009-06-17 16:26:17 -0700600 struct device_node *node;
Dirk Eibach16482372011-02-24 10:20:43 +0100601 const __be32 *val;
602 int size;
Nate Case1965d302009-06-17 16:26:17 -0700603
Aaron Sierra2b1f5972014-02-07 19:23:04 -0600604 *gpio_base = -1;
605
Grant Likely61c7a082010-04-13 16:12:29 -0700606 node = client->dev.of_node;
Nate Case1965d302009-06-17 16:26:17 -0700607 if (node == NULL)
David Janderc6dcf592011-06-14 11:00:55 +0200608 return;
Nate Case1965d302009-06-17 16:26:17 -0700609
Dirk Eibach16482372011-02-24 10:20:43 +0100610 val = of_get_property(node, "linux,gpio-base", &size);
David Jandera57339b2011-06-14 11:00:56 +0200611 WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
Nate Case1965d302009-06-17 16:26:17 -0700612 if (val) {
Dirk Eibach16482372011-02-24 10:20:43 +0100613 if (size != sizeof(*val))
614 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
615 node->full_name);
Nate Case1965d302009-06-17 16:26:17 -0700616 else
David Janderc6dcf592011-06-14 11:00:55 +0200617 *gpio_base = be32_to_cpup(val);
Nate Case1965d302009-06-17 16:26:17 -0700618 }
619
620 val = of_get_property(node, "polarity", NULL);
David Jandera57339b2011-06-14 11:00:56 +0200621 WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
Nate Case1965d302009-06-17 16:26:17 -0700622 if (val)
David Janderc6dcf592011-06-14 11:00:55 +0200623 *invert = *val;
Nate Case1965d302009-06-17 16:26:17 -0700624}
625#else
Mark Brown404ba2b2011-11-21 17:57:25 +0000626static void
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530627pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
Nate Case1965d302009-06-17 16:26:17 -0700628{
Hartmut Knaack25fcf2b2011-10-11 00:22:45 +0200629 *gpio_base = -1;
Nate Case1965d302009-06-17 16:26:17 -0700630}
631#endif
632
Bill Pemberton38363092012-11-19 13:22:34 -0500633static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800634{
635 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100636 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800637
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100638 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800639 if (ret)
640 goto out;
641
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100642 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
643 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800644 if (ret)
645 goto out;
646
647 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100648 if (invert)
649 memset(val, 0xFF, NBANK(chip));
650 else
651 memset(val, 0, NBANK(chip));
652
653 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800654out:
655 return ret;
656}
657
Bill Pemberton38363092012-11-19 13:22:34 -0500658static int device_pca957x_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, PCA957X_OUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800664 if (ret)
665 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100666 ret = pca953x_read_regs(chip, PCA957X_CFG, 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 pca953x_write_regs(chip, PCA957X_INVRT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800676
677 /* To enable register 6, 7 to controll pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100678 memset(val, 0x02, NBANK(chip));
679 pca953x_write_regs(chip, PCA957X_BKEN, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800680
681 return 0;
682out:
683 return ret;
684}
685
Bill Pemberton38363092012-11-19 13:22:34 -0500686static int pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200687 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800688{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800689 struct pca953x_platform_data *pdata;
690 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530691 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200692 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530693 u32 invert = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800694
Linus Walleijb42748c2013-01-25 17:59:28 +0100695 chip = devm_kzalloc(&client->dev,
696 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800697 if (chip == NULL)
698 return -ENOMEM;
699
Jingoo Hane56aee12013-07-30 17:08:05 +0900700 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200701 if (pdata) {
702 irq_base = pdata->irq_base;
703 chip->gpio_start = pdata->gpio_base;
704 invert = pdata->invert;
705 chip->names = pdata->names;
706 } else {
707 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
David Jandera57339b2011-06-14 11:00:56 +0200708#ifdef CONFIG_OF_GPIO
709 /* If I2C node has no interrupts property, disable GPIO interrupts */
710 if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
711 irq_base = -1;
712#endif
Nate Case1965d302009-06-17 16:26:17 -0700713 }
714
eric miao9e60fdc2008-02-04 22:28:26 -0800715 chip->client = client;
716
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800717 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700718
Roland Stigge6e20fb12011-02-10 15:01:23 -0800719 mutex_init(&chip->i2c_lock);
720
eric miao9e60fdc2008-02-04 22:28:26 -0800721 /* initialize cached registers from their original values.
722 * we can't share this chip with another i2c master.
723 */
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800724 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800725
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800726 if (chip->chip_type == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200727 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800728 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200729 ret = device_pca957x_init(chip, invert);
730 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100731 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800732
Linus Walleij7bcbce52014-05-09 13:27:57 +0200733 ret = gpiochip_add(&chip->gpio_chip);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800734 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100735 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800736
Linus Walleij7bcbce52014-05-09 13:27:57 +0200737 ret = pca953x_irq_setup(chip, id, irq_base);
eric miao9e60fdc2008-02-04 22:28:26 -0800738 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100739 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800740
David Janderc6dcf592011-06-14 11:00:55 +0200741 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800742 ret = pdata->setup(client, chip->gpio_chip.base,
743 chip->gpio_chip.ngpio, pdata->context);
744 if (ret < 0)
745 dev_warn(&client->dev, "setup failed, %d\n", ret);
746 }
747
748 i2c_set_clientdata(client, chip);
749 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800750}
751
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800752static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800753{
Jingoo Hane56aee12013-07-30 17:08:05 +0900754 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800755 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800756 int ret = 0;
757
David Janderc6dcf592011-06-14 11:00:55 +0200758 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800759 ret = pdata->teardown(client, chip->gpio_chip.base,
760 chip->gpio_chip.ngpio, pdata->context);
761 if (ret < 0) {
762 dev_err(&client->dev, "%s failed, %d\n",
763 "teardown", ret);
764 return ret;
765 }
766 }
767
768 ret = gpiochip_remove(&chip->gpio_chip);
769 if (ret) {
770 dev_err(&client->dev, "%s failed, %d\n",
771 "gpiochip_remove()", ret);
772 return ret;
773 }
774
eric miao9e60fdc2008-02-04 22:28:26 -0800775 return 0;
776}
777
Maxime Riparded326202012-11-08 18:01:52 +0100778static const struct of_device_id pca953x_dt_ids[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +0100779 { .compatible = "nxp,pca9505", },
Maxime Riparded326202012-11-08 18:01:52 +0100780 { .compatible = "nxp,pca9534", },
781 { .compatible = "nxp,pca9535", },
782 { .compatible = "nxp,pca9536", },
783 { .compatible = "nxp,pca9537", },
784 { .compatible = "nxp,pca9538", },
785 { .compatible = "nxp,pca9539", },
786 { .compatible = "nxp,pca9554", },
787 { .compatible = "nxp,pca9555", },
788 { .compatible = "nxp,pca9556", },
789 { .compatible = "nxp,pca9557", },
790 { .compatible = "nxp,pca9574", },
791 { .compatible = "nxp,pca9575", },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +0100792 { .compatible = "nxp,pca9698", },
Maxime Riparded326202012-11-08 18:01:52 +0100793
794 { .compatible = "maxim,max7310", },
795 { .compatible = "maxim,max7312", },
796 { .compatible = "maxim,max7313", },
797 { .compatible = "maxim,max7315", },
798
799 { .compatible = "ti,pca6107", },
800 { .compatible = "ti,tca6408", },
801 { .compatible = "ti,tca6416", },
802 { .compatible = "ti,tca6424", },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600803
804 { .compatible = "exar,xra1202", },
Maxime Riparded326202012-11-08 18:01:52 +0100805 { }
806};
807
808MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
809
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800810static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800811 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800812 .name = "pca953x",
Maxime Riparded326202012-11-08 18:01:52 +0100813 .of_match_table = pca953x_dt_ids,
eric miao9e60fdc2008-02-04 22:28:26 -0800814 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800815 .probe = pca953x_probe,
816 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200817 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800818};
819
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800820static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800821{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800822 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800823}
David Brownell2f8d1192008-10-15 22:03:13 -0700824/* register after i2c postcore initcall and before
825 * subsys initcalls that may rely on these GPIOs
826 */
827subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800828
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800829static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800830{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800831 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800832}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800833module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800834
835MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800836MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800837MODULE_LICENSE("GPL");