blob: 2d4892cc70fb0c4acc149dd6a9864439644a1673 [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
Andy Shevchenkof32517b2015-10-01 14:20:28 +030024#include <linux/acpi.h>
eric miao9e60fdc2008-02-04 22:28:26 -080025
Haojian Zhuang33226ff2011-04-18 22:12:46 +080026#define PCA953X_INPUT 0
27#define PCA953X_OUTPUT 1
28#define PCA953X_INVERT 2
29#define PCA953X_DIRECTION 3
eric miao9e60fdc2008-02-04 22:28:26 -080030
Andreas Schallenbergae79c192012-05-09 09:46:17 +020031#define REG_ADDR_AI 0x80
32
Haojian Zhuang33226ff2011-04-18 22:12:46 +080033#define PCA957X_IN 0
34#define PCA957X_INVRT 1
35#define PCA957X_BKEN 2
36#define PCA957X_PUPD 3
37#define PCA957X_CFG 4
38#define PCA957X_OUT 5
39#define PCA957X_MSK 6
40#define PCA957X_INTS 7
41
42#define PCA_GPIO_MASK 0x00FF
43#define PCA_INT 0x0100
44#define PCA953X_TYPE 0x1000
45#define PCA957X_TYPE 0x2000
Andy Shevchenkoc6664142015-10-01 14:20:27 +030046#define PCA_TYPE_MASK 0xF000
47
48#define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080049
Jean Delvare3760f732008-04-29 23:11:40 +020050static const struct i2c_device_id pca953x_id[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +010051 { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080052 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
53 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
54 { "pca9536", 4 | PCA953X_TYPE, },
55 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
56 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
57 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
58 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
59 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
60 { "pca9556", 8 | PCA953X_TYPE, },
61 { "pca9557", 8 | PCA953X_TYPE, },
62 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
63 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +010064 { "pca9698", 40 | PCA953X_TYPE, },
David Brownellab5dc3722009-01-06 14:42:27 -080065
Haojian Zhuang33226ff2011-04-18 22:12:46 +080066 { "max7310", 8 | PCA953X_TYPE, },
67 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
68 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
69 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
70 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
71 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
72 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
Andreas Schallenbergae79c192012-05-09 09:46:17 +020073 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
Thierry Reding2db8aba2015-09-29 12:55:44 +020074 { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
Aaron Sierrae73760a2014-02-07 16:36:21 -060075 { "xra1202", 8 | PCA953X_TYPE },
Jean Delvare3760f732008-04-29 23:11:40 +020076 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080077};
Jean Delvare3760f732008-04-29 23:11:40 +020078MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080079
Andy Shevchenkof32517b2015-10-01 14:20:28 +030080static const struct acpi_device_id pca953x_acpi_ids[] = {
81 { "INT3491", 16 | PCA953X_TYPE | PCA_INT, },
82 { }
83};
84MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
85
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010086#define MAX_BANK 5
87#define BANK_SZ 8
88
89#define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)
90
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080091struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080092 unsigned gpio_start;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010093 u8 reg_output[MAX_BANK];
94 u8 reg_direction[MAX_BANK];
Roland Stigge6e20fb12011-02-10 15:01:23 -080095 struct mutex i2c_lock;
eric miao9e60fdc2008-02-04 22:28:26 -080096
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080097#ifdef CONFIG_GPIO_PCA953X_IRQ
98 struct mutex irq_lock;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010099 u8 irq_mask[MAX_BANK];
100 u8 irq_stat[MAX_BANK];
101 u8 irq_trig_raise[MAX_BANK];
102 u8 irq_trig_fall[MAX_BANK];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800103#endif
104
eric miao9e60fdc2008-02-04 22:28:26 -0800105 struct i2c_client *client;
106 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700107 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800108 int chip_type;
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300109 unsigned long driver_data;
eric miao9e60fdc2008-02-04 22:28:26 -0800110};
111
Linus Walleij7bcbce52014-05-09 13:27:57 +0200112static inline struct pca953x_chip *to_pca(struct gpio_chip *gc)
113{
114 return container_of(gc, struct pca953x_chip, gpio_chip);
115}
116
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100117static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
118 int off)
119{
120 int ret;
121 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
122 int offset = off / BANK_SZ;
123
124 ret = i2c_smbus_read_byte_data(chip->client,
125 (reg << bank_shift) + offset);
126 *val = ret;
127
128 if (ret < 0) {
129 dev_err(&chip->client->dev, "failed reading register\n");
130 return ret;
131 }
132
133 return 0;
134}
135
136static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
137 int off)
138{
139 int ret = 0;
140 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
141 int offset = off / BANK_SZ;
142
143 ret = i2c_smbus_write_byte_data(chip->client,
144 (reg << bank_shift) + offset, val);
145
146 if (ret < 0) {
147 dev_err(&chip->client->dev, "failed writing register\n");
148 return ret;
149 }
150
151 return 0;
152}
153
154static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800155{
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800156 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800157
158 if (chip->gpio_chip.ngpio <= 8)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100159 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
160 else if (chip->gpio_chip.ngpio >= 24) {
161 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200162 ret = i2c_smbus_write_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100163 (reg << bank_shift) | REG_ADDR_AI,
164 NBANK(chip), val);
Laurent Navet50e44432013-03-20 13:15:58 +0100165 } else {
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800166 switch (chip->chip_type) {
167 case PCA953X_TYPE:
168 ret = i2c_smbus_write_word_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100169 reg << 1, (u16) *val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800170 break;
171 case PCA957X_TYPE:
172 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100173 val[0]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800174 if (ret < 0)
175 break;
176 ret = i2c_smbus_write_byte_data(chip->client,
177 (reg << 1) + 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100178 val[1]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800179 break;
180 }
181 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800182
183 if (ret < 0) {
184 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc3722009-01-06 14:42:27 -0800185 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800186 }
187
188 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800189}
190
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100191static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800192{
193 int ret;
194
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200195 if (chip->gpio_chip.ngpio <= 8) {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800196 ret = i2c_smbus_read_byte_data(chip->client, reg);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200197 *val = ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100198 } else if (chip->gpio_chip.ngpio >= 24) {
199 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
200
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200201 ret = i2c_smbus_read_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100202 (reg << bank_shift) | REG_ADDR_AI,
203 NBANK(chip), val);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200204 } else {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800205 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100206 val[0] = (u16)ret & 0xFF;
207 val[1] = (u16)ret >> 8;
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200208 }
eric miao9e60fdc2008-02-04 22:28:26 -0800209 if (ret < 0) {
210 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc3722009-01-06 14:42:27 -0800211 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800212 }
213
eric miao9e60fdc2008-02-04 22:28:26 -0800214 return 0;
215}
216
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800217static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800218{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200219 struct pca953x_chip *chip = to_pca(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100220 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800221 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800222
Roland Stigge6e20fb12011-02-10 15:01:23 -0800223 mutex_lock(&chip->i2c_lock);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100224 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800225
226 switch (chip->chip_type) {
227 case PCA953X_TYPE:
228 offset = PCA953X_DIRECTION;
229 break;
230 case PCA957X_TYPE:
231 offset = PCA957X_CFG;
232 break;
233 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100234 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800235 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800236 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800237
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100238 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800239 ret = 0;
240exit:
241 mutex_unlock(&chip->i2c_lock);
242 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800243}
244
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800245static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800246 unsigned off, int val)
247{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200248 struct pca953x_chip *chip = to_pca(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100249 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800250 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800251
Roland Stigge6e20fb12011-02-10 15:01:23 -0800252 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800253 /* set output level */
254 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100255 reg_val = chip->reg_output[off / BANK_SZ]
256 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800257 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100258 reg_val = chip->reg_output[off / BANK_SZ]
259 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800260
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800261 switch (chip->chip_type) {
262 case PCA953X_TYPE:
263 offset = PCA953X_OUTPUT;
264 break;
265 case PCA957X_TYPE:
266 offset = PCA957X_OUT;
267 break;
268 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100269 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800270 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800271 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800272
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100273 chip->reg_output[off / BANK_SZ] = reg_val;
eric miao9e60fdc2008-02-04 22:28:26 -0800274
275 /* then direction */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100276 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800277 switch (chip->chip_type) {
278 case PCA953X_TYPE:
279 offset = PCA953X_DIRECTION;
280 break;
281 case PCA957X_TYPE:
282 offset = PCA957X_CFG;
283 break;
284 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100285 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800286 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800287 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800288
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100289 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800290 ret = 0;
291exit:
292 mutex_unlock(&chip->i2c_lock);
293 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800294}
295
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800296static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800297{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200298 struct pca953x_chip *chip = to_pca(gc);
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200299 u32 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800300 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800301
Roland Stigge6e20fb12011-02-10 15:01:23 -0800302 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800303 switch (chip->chip_type) {
304 case PCA953X_TYPE:
305 offset = PCA953X_INPUT;
306 break;
307 case PCA957X_TYPE:
308 offset = PCA957X_IN;
309 break;
310 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100311 ret = pca953x_read_single(chip, offset, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800312 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800313 if (ret < 0) {
314 /* NOTE: diagnostic already emitted; that's all we should
315 * do unless gpio_*_value_cansleep() calls become different
316 * from their nonsleeping siblings (and report faults).
317 */
318 return 0;
319 }
320
Andrew Ruder40a625d2013-08-07 16:52:40 -0500321 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800322}
323
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800324static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800325{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200326 struct pca953x_chip *chip = to_pca(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100327 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800328 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800329
Roland Stigge6e20fb12011-02-10 15:01:23 -0800330 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800331 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100332 reg_val = chip->reg_output[off / BANK_SZ]
333 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800334 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100335 reg_val = chip->reg_output[off / BANK_SZ]
336 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800337
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800338 switch (chip->chip_type) {
339 case PCA953X_TYPE:
340 offset = PCA953X_OUTPUT;
341 break;
342 case PCA957X_TYPE:
343 offset = PCA957X_OUT;
344 break;
345 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100346 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800347 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800348 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800349
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100350 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800351exit:
352 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800353}
354
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800355static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800356{
357 struct gpio_chip *gc;
358
359 gc = &chip->gpio_chip;
360
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800361 gc->direction_input = pca953x_gpio_direction_input;
362 gc->direction_output = pca953x_gpio_direction_output;
363 gc->get = pca953x_gpio_get_value;
364 gc->set = pca953x_gpio_set_value;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100365 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800366
367 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800368 gc->ngpio = gpios;
369 gc->label = chip->client->name;
David Brownelld8f388d82008-07-25 01:46:07 -0700370 gc->dev = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700371 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700372 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800373}
374
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800375#ifdef CONFIG_GPIO_PCA953X_IRQ
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800376static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800377{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200378 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
379 struct pca953x_chip *chip = to_pca(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800380
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100381 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800382}
383
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800384static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800385{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200386 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
387 struct pca953x_chip *chip = to_pca(gc);
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{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200394 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
395 struct pca953x_chip *chip = to_pca(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800396
397 mutex_lock(&chip->irq_lock);
398}
399
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800400static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800401{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200402 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
403 struct pca953x_chip *chip = to_pca(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100404 u8 new_irqs;
405 int level, i;
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700406
407 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100408 for (i = 0; i < NBANK(chip); i++) {
409 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
410 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700411
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100412 while (new_irqs) {
413 level = __ffs(new_irqs);
414 pca953x_gpio_direction_input(&chip->gpio_chip,
415 level + (BANK_SZ * i));
416 new_irqs &= ~(1 << level);
417 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700418 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800419
420 mutex_unlock(&chip->irq_lock);
421}
422
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800423static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800424{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200425 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
426 struct pca953x_chip *chip = to_pca(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100427 int bank_nb = d->hwirq / BANK_SZ;
428 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800429
430 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
431 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800432 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800433 return -EINVAL;
434 }
435
436 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100437 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800438 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100439 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800440
441 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100442 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800443 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100444 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800445
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700446 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800447}
448
449static struct irq_chip pca953x_irq_chip = {
450 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800451 .irq_mask = pca953x_irq_mask,
452 .irq_unmask = pca953x_irq_unmask,
453 .irq_bus_lock = pca953x_irq_bus_lock,
454 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
455 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800456};
457
Joshua Scottb6ac1282015-05-22 12:35:12 +1200458static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800459{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100460 u8 cur_stat[MAX_BANK];
461 u8 old_stat[MAX_BANK];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200462 bool pending_seen = false;
463 bool trigger_seen = false;
464 u8 trigger[MAX_BANK];
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100465 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800466
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800467 switch (chip->chip_type) {
468 case PCA953X_TYPE:
469 offset = PCA953X_INPUT;
470 break;
471 case PCA957X_TYPE:
472 offset = PCA957X_IN;
473 break;
474 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100475 ret = pca953x_read_regs(chip, offset, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800476 if (ret)
Joshua Scottb6ac1282015-05-22 12:35:12 +1200477 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800478
479 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100480 for (i = 0; i < NBANK(chip); i++)
481 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800482
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100483 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800484
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100485 for (i = 0; i < NBANK(chip); i++) {
486 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200487 if (trigger[i])
488 trigger_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100489 }
490
Joshua Scottb6ac1282015-05-22 12:35:12 +1200491 if (!trigger_seen)
492 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800493
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100494 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800495
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100496 for (i = 0; i < NBANK(chip); i++) {
497 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
498 (cur_stat[i] & chip->irq_trig_raise[i]);
499 pending[i] &= trigger[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200500 if (pending[i])
501 pending_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100502 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800503
Joshua Scottb6ac1282015-05-22 12:35:12 +1200504 return pending_seen;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800505}
506
507static irqreturn_t pca953x_irq_handler(int irq, void *devid)
508{
509 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100510 u8 pending[MAX_BANK];
511 u8 level;
Toby Smith3275d072014-04-30 18:01:40 +1000512 unsigned nhandled = 0;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100513 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800514
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100515 if (!pca953x_irq_pending(chip, pending))
Toby Smith3275d072014-04-30 18:01:40 +1000516 return IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800517
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100518 for (i = 0; i < NBANK(chip); i++) {
519 while (pending[i]) {
520 level = __ffs(pending[i]);
Linus Walleij7bcbce52014-05-09 13:27:57 +0200521 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100522 level + (BANK_SZ * i)));
523 pending[i] &= ~(1 << level);
Toby Smith3275d072014-04-30 18:01:40 +1000524 nhandled++;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100525 }
526 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800527
Toby Smith3275d072014-04-30 18:01:40 +1000528 return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800529}
530
531static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200532 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800533{
534 struct i2c_client *client = chip->client;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100535 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800536
Markus Pargmann4bb93342014-07-29 09:24:43 +0200537 if (client->irq && irq_base != -1
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300538 && (chip->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800539
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800540 switch (chip->chip_type) {
541 case PCA953X_TYPE:
542 offset = PCA953X_INPUT;
543 break;
544 case PCA957X_TYPE:
545 offset = PCA957X_IN;
546 break;
547 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100548 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800549 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100550 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800551
552 /*
553 * There is no way to know which GPIO line generated the
554 * interrupt. We have to rely on the previous read for
555 * this purpose.
556 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100557 for (i = 0; i < NBANK(chip); i++)
558 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800559 mutex_init(&chip->irq_lock);
560
Linus Walleijb42748c2013-01-25 17:59:28 +0100561 ret = devm_request_threaded_irq(&client->dev,
562 client->irq,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800563 NULL,
564 pca953x_irq_handler,
Toby Smith91329132014-04-30 18:01:41 +1000565 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
566 IRQF_SHARED,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800567 dev_name(&client->dev), chip);
568 if (ret) {
569 dev_err(&client->dev, "failed to request irq %d\n",
570 client->irq);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100571 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800572 }
573
Linus Walleij7bcbce52014-05-09 13:27:57 +0200574 ret = gpiochip_irqchip_add(&chip->gpio_chip,
575 &pca953x_irq_chip,
576 irq_base,
577 handle_simple_irq,
578 IRQ_TYPE_NONE);
579 if (ret) {
580 dev_err(&client->dev,
581 "could not connect irqchip to gpiochip\n");
582 return ret;
583 }
Grygorii Strashkofdd50402015-07-07 17:34:49 +0300584
585 gpiochip_set_chained_irqchip(&chip->gpio_chip,
586 &pca953x_irq_chip,
587 client->irq, NULL);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800588 }
589
590 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800591}
592
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800593#else /* CONFIG_GPIO_PCA953X_IRQ */
594static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200595 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800596{
597 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800598
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300599 if (irq_base != -1 && (chip->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800600 dev_warn(&client->dev, "interrupt support not compiled in\n");
601
602 return 0;
603}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800604#endif
605
Bill Pemberton38363092012-11-19 13:22:34 -0500606static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800607{
608 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100609 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800610
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100611 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800612 if (ret)
613 goto out;
614
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100615 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
616 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800617 if (ret)
618 goto out;
619
620 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100621 if (invert)
622 memset(val, 0xFF, NBANK(chip));
623 else
624 memset(val, 0, NBANK(chip));
625
626 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800627out:
628 return ret;
629}
630
Bill Pemberton38363092012-11-19 13:22:34 -0500631static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800632{
633 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100634 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800635
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100636 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800637 if (ret)
638 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100639 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800640 if (ret)
641 goto out;
642
643 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100644 if (invert)
645 memset(val, 0xFF, NBANK(chip));
646 else
647 memset(val, 0, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400648 ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
649 if (ret)
650 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800651
Colin Cronin20a8a962015-05-18 11:41:43 -0700652 /* To enable register 6, 7 to control pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100653 memset(val, 0x02, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400654 ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
655 if (ret)
656 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800657
658 return 0;
659out:
660 return ret;
661}
662
Bill Pemberton38363092012-11-19 13:22:34 -0500663static int pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200664 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800665{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800666 struct pca953x_platform_data *pdata;
667 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530668 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200669 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530670 u32 invert = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800671
Linus Walleijb42748c2013-01-25 17:59:28 +0100672 chip = devm_kzalloc(&client->dev,
673 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800674 if (chip == NULL)
675 return -ENOMEM;
676
Jingoo Hane56aee12013-07-30 17:08:05 +0900677 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200678 if (pdata) {
679 irq_base = pdata->irq_base;
680 chip->gpio_start = pdata->gpio_base;
681 invert = pdata->invert;
682 chip->names = pdata->names;
683 } else {
Markus Pargmann4bb93342014-07-29 09:24:43 +0200684 chip->gpio_start = -1;
685 irq_base = 0;
Nate Case1965d302009-06-17 16:26:17 -0700686 }
687
eric miao9e60fdc2008-02-04 22:28:26 -0800688 chip->client = client;
689
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300690 if (id) {
691 chip->driver_data = id->driver_data;
692 } else {
693 const struct acpi_device_id *id;
694
695 id = acpi_match_device(pca953x_acpi_ids, &client->dev);
696 if (!id)
697 return -ENODEV;
698
699 chip->driver_data = id->driver_data;
700 }
701
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300702 chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700703
Roland Stigge6e20fb12011-02-10 15:01:23 -0800704 mutex_init(&chip->i2c_lock);
705
eric miao9e60fdc2008-02-04 22:28:26 -0800706 /* initialize cached registers from their original values.
707 * we can't share this chip with another i2c master.
708 */
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300709 pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800710
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800711 if (chip->chip_type == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200712 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800713 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200714 ret = device_pca957x_init(chip, invert);
715 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100716 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800717
Linus Walleij7bcbce52014-05-09 13:27:57 +0200718 ret = gpiochip_add(&chip->gpio_chip);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800719 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100720 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800721
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300722 ret = pca953x_irq_setup(chip, irq_base);
eric miao9e60fdc2008-02-04 22:28:26 -0800723 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100724 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800725
David Janderc6dcf592011-06-14 11:00:55 +0200726 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800727 ret = pdata->setup(client, chip->gpio_chip.base,
728 chip->gpio_chip.ngpio, pdata->context);
729 if (ret < 0)
730 dev_warn(&client->dev, "setup failed, %d\n", ret);
731 }
732
733 i2c_set_clientdata(client, chip);
734 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800735}
736
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800737static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800738{
Jingoo Hane56aee12013-07-30 17:08:05 +0900739 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800740 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800741 int ret = 0;
742
David Janderc6dcf592011-06-14 11:00:55 +0200743 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800744 ret = pdata->teardown(client, chip->gpio_chip.base,
745 chip->gpio_chip.ngpio, pdata->context);
746 if (ret < 0) {
747 dev_err(&client->dev, "%s failed, %d\n",
748 "teardown", ret);
749 return ret;
750 }
751 }
752
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200753 gpiochip_remove(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800754
eric miao9e60fdc2008-02-04 22:28:26 -0800755 return 0;
756}
757
Maxime Riparded326202012-11-08 18:01:52 +0100758static const struct of_device_id pca953x_dt_ids[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +0100759 { .compatible = "nxp,pca9505", },
Maxime Riparded326202012-11-08 18:01:52 +0100760 { .compatible = "nxp,pca9534", },
761 { .compatible = "nxp,pca9535", },
762 { .compatible = "nxp,pca9536", },
763 { .compatible = "nxp,pca9537", },
764 { .compatible = "nxp,pca9538", },
765 { .compatible = "nxp,pca9539", },
766 { .compatible = "nxp,pca9554", },
767 { .compatible = "nxp,pca9555", },
768 { .compatible = "nxp,pca9556", },
769 { .compatible = "nxp,pca9557", },
770 { .compatible = "nxp,pca9574", },
771 { .compatible = "nxp,pca9575", },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +0100772 { .compatible = "nxp,pca9698", },
Maxime Riparded326202012-11-08 18:01:52 +0100773
774 { .compatible = "maxim,max7310", },
775 { .compatible = "maxim,max7312", },
776 { .compatible = "maxim,max7313", },
777 { .compatible = "maxim,max7315", },
778
779 { .compatible = "ti,pca6107", },
780 { .compatible = "ti,tca6408", },
781 { .compatible = "ti,tca6416", },
782 { .compatible = "ti,tca6424", },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600783
784 { .compatible = "exar,xra1202", },
Maxime Riparded326202012-11-08 18:01:52 +0100785 { }
786};
787
788MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
789
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800790static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800791 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800792 .name = "pca953x",
Maxime Riparded326202012-11-08 18:01:52 +0100793 .of_match_table = pca953x_dt_ids,
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300794 .acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
eric miao9e60fdc2008-02-04 22:28:26 -0800795 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800796 .probe = pca953x_probe,
797 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200798 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800799};
800
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800801static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800802{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800803 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800804}
David Brownell2f8d1192008-10-15 22:03:13 -0700805/* register after i2c postcore initcall and before
806 * subsys initcalls that may rely on these GPIOs
807 */
808subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800809
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800810static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800811{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800812 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800813}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800814module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800815
816MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800817MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800818MODULE_LICENSE("GPL");