blob: d0d3065a755767dcfeec9108921ac431fc490235 [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#include <linux/of_platform.h>
Andy Shevchenkof32517b2015-10-01 14:20:28 +030022#include <linux/acpi.h>
eric miao9e60fdc2008-02-04 22:28:26 -080023
Haojian Zhuang33226ff2011-04-18 22:12:46 +080024#define PCA953X_INPUT 0
25#define PCA953X_OUTPUT 1
26#define PCA953X_INVERT 2
27#define PCA953X_DIRECTION 3
eric miao9e60fdc2008-02-04 22:28:26 -080028
Andreas Schallenbergae79c192012-05-09 09:46:17 +020029#define REG_ADDR_AI 0x80
30
Haojian Zhuang33226ff2011-04-18 22:12:46 +080031#define PCA957X_IN 0
32#define PCA957X_INVRT 1
33#define PCA957X_BKEN 2
34#define PCA957X_PUPD 3
35#define PCA957X_CFG 4
36#define PCA957X_OUT 5
37#define PCA957X_MSK 6
38#define PCA957X_INTS 7
39
40#define PCA_GPIO_MASK 0x00FF
41#define PCA_INT 0x0100
42#define PCA953X_TYPE 0x1000
43#define PCA957X_TYPE 0x2000
Andy Shevchenkoc6664142015-10-01 14:20:27 +030044#define PCA_TYPE_MASK 0xF000
45
46#define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
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 Brownellab5dc3722009-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, },
Thierry Reding2db8aba2015-09-29 12:55:44 +020072 { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
Aaron Sierrae73760a2014-02-07 16:36:21 -060073 { "xra1202", 8 | PCA953X_TYPE },
Jean Delvare3760f732008-04-29 23:11:40 +020074 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080075};
Jean Delvare3760f732008-04-29 23:11:40 +020076MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080077
Andy Shevchenkof32517b2015-10-01 14:20:28 +030078static const struct acpi_device_id pca953x_acpi_ids[] = {
79 { "INT3491", 16 | PCA953X_TYPE | PCA_INT, },
80 { }
81};
82MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
83
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010084#define MAX_BANK 5
85#define BANK_SZ 8
86
87#define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)
88
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080089struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080090 unsigned gpio_start;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010091 u8 reg_output[MAX_BANK];
92 u8 reg_direction[MAX_BANK];
Roland Stigge6e20fb12011-02-10 15:01:23 -080093 struct mutex i2c_lock;
eric miao9e60fdc2008-02-04 22:28:26 -080094
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080095#ifdef CONFIG_GPIO_PCA953X_IRQ
96 struct mutex irq_lock;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010097 u8 irq_mask[MAX_BANK];
98 u8 irq_stat[MAX_BANK];
99 u8 irq_trig_raise[MAX_BANK];
100 u8 irq_trig_fall[MAX_BANK];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800101#endif
102
eric miao9e60fdc2008-02-04 22:28:26 -0800103 struct i2c_client *client;
104 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700105 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800106 int chip_type;
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300107 unsigned long driver_data;
eric miao9e60fdc2008-02-04 22:28:26 -0800108};
109
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100110static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
111 int off)
112{
113 int ret;
114 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
115 int offset = off / BANK_SZ;
116
117 ret = i2c_smbus_read_byte_data(chip->client,
118 (reg << bank_shift) + offset);
119 *val = ret;
120
121 if (ret < 0) {
122 dev_err(&chip->client->dev, "failed reading register\n");
123 return ret;
124 }
125
126 return 0;
127}
128
129static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
130 int off)
131{
132 int ret = 0;
133 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
134 int offset = off / BANK_SZ;
135
136 ret = i2c_smbus_write_byte_data(chip->client,
137 (reg << bank_shift) + offset, val);
138
139 if (ret < 0) {
140 dev_err(&chip->client->dev, "failed writing register\n");
141 return ret;
142 }
143
144 return 0;
145}
146
147static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800148{
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800149 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800150
151 if (chip->gpio_chip.ngpio <= 8)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100152 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
153 else if (chip->gpio_chip.ngpio >= 24) {
154 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200155 ret = i2c_smbus_write_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100156 (reg << bank_shift) | REG_ADDR_AI,
157 NBANK(chip), val);
Laurent Navet50e44432013-03-20 13:15:58 +0100158 } else {
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800159 switch (chip->chip_type) {
160 case PCA953X_TYPE:
161 ret = i2c_smbus_write_word_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100162 reg << 1, (u16) *val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800163 break;
164 case PCA957X_TYPE:
165 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100166 val[0]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800167 if (ret < 0)
168 break;
169 ret = i2c_smbus_write_byte_data(chip->client,
170 (reg << 1) + 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100171 val[1]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800172 break;
173 }
174 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800175
176 if (ret < 0) {
177 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc3722009-01-06 14:42:27 -0800178 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800179 }
180
181 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800182}
183
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100184static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800185{
186 int ret;
187
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200188 if (chip->gpio_chip.ngpio <= 8) {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800189 ret = i2c_smbus_read_byte_data(chip->client, reg);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200190 *val = ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100191 } else if (chip->gpio_chip.ngpio >= 24) {
192 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
193
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200194 ret = i2c_smbus_read_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100195 (reg << bank_shift) | REG_ADDR_AI,
196 NBANK(chip), val);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200197 } else {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800198 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100199 val[0] = (u16)ret & 0xFF;
200 val[1] = (u16)ret >> 8;
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200201 }
eric miao9e60fdc2008-02-04 22:28:26 -0800202 if (ret < 0) {
203 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc3722009-01-06 14:42:27 -0800204 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800205 }
206
eric miao9e60fdc2008-02-04 22:28:26 -0800207 return 0;
208}
209
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800210static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800211{
Linus Walleij468e67f2015-12-07 11:20:54 +0100212 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100213 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800214 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800215
Roland Stigge6e20fb12011-02-10 15:01:23 -0800216 mutex_lock(&chip->i2c_lock);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100217 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800218
219 switch (chip->chip_type) {
220 case PCA953X_TYPE:
221 offset = PCA953X_DIRECTION;
222 break;
223 case PCA957X_TYPE:
224 offset = PCA957X_CFG;
225 break;
226 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100227 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800228 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800229 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800230
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100231 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800232 ret = 0;
233exit:
234 mutex_unlock(&chip->i2c_lock);
235 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800236}
237
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800238static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800239 unsigned off, int val)
240{
Linus Walleij468e67f2015-12-07 11:20:54 +0100241 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100242 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800243 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800244
Roland Stigge6e20fb12011-02-10 15:01:23 -0800245 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800246 /* set output level */
247 if (val)
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 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100251 reg_val = chip->reg_output[off / BANK_SZ]
252 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800253
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800254 switch (chip->chip_type) {
255 case PCA953X_TYPE:
256 offset = PCA953X_OUTPUT;
257 break;
258 case PCA957X_TYPE:
259 offset = PCA957X_OUT;
260 break;
261 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100262 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800263 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800264 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800265
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100266 chip->reg_output[off / BANK_SZ] = reg_val;
eric miao9e60fdc2008-02-04 22:28:26 -0800267
268 /* then direction */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100269 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800270 switch (chip->chip_type) {
271 case PCA953X_TYPE:
272 offset = PCA953X_DIRECTION;
273 break;
274 case PCA957X_TYPE:
275 offset = PCA957X_CFG;
276 break;
277 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100278 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800279 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800280 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800281
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100282 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800283 ret = 0;
284exit:
285 mutex_unlock(&chip->i2c_lock);
286 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800287}
288
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800289static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800290{
Linus Walleij468e67f2015-12-07 11:20:54 +0100291 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200292 u32 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800293 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800294
Roland Stigge6e20fb12011-02-10 15:01:23 -0800295 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800296 switch (chip->chip_type) {
297 case PCA953X_TYPE:
298 offset = PCA953X_INPUT;
299 break;
300 case PCA957X_TYPE:
301 offset = PCA957X_IN;
302 break;
303 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100304 ret = pca953x_read_single(chip, offset, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800305 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800306 if (ret < 0) {
307 /* NOTE: diagnostic already emitted; that's all we should
308 * do unless gpio_*_value_cansleep() calls become different
309 * from their nonsleeping siblings (and report faults).
310 */
311 return 0;
312 }
313
Andrew Ruder40a625d2013-08-07 16:52:40 -0500314 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800315}
316
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800317static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800318{
Linus Walleij468e67f2015-12-07 11:20:54 +0100319 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100320 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800321 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800322
Roland Stigge6e20fb12011-02-10 15:01:23 -0800323 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800324 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100325 reg_val = chip->reg_output[off / BANK_SZ]
326 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800327 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100328 reg_val = chip->reg_output[off / BANK_SZ]
329 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800330
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800331 switch (chip->chip_type) {
332 case PCA953X_TYPE:
333 offset = PCA953X_OUTPUT;
334 break;
335 case PCA957X_TYPE:
336 offset = PCA957X_OUT;
337 break;
338 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100339 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800340 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800341 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800342
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100343 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800344exit:
345 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800346}
347
Phil Reidb4818af2015-12-04 15:52:30 +0800348
349static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
350 unsigned long *mask, unsigned long *bits)
351{
Linus Walleij468e67f2015-12-07 11:20:54 +0100352 struct pca953x_chip *chip = gpiochip_get_data(gc);
Phil Reidb4818af2015-12-04 15:52:30 +0800353 u8 reg_val[MAX_BANK];
354 int ret, offset = 0;
355 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
356 int bank;
357
358 switch (chip->chip_type) {
359 case PCA953X_TYPE:
360 offset = PCA953X_OUTPUT;
361 break;
362 case PCA957X_TYPE:
363 offset = PCA957X_OUT;
364 break;
365 }
366
367 memcpy(reg_val, chip->reg_output, NBANK(chip));
368 mutex_lock(&chip->i2c_lock);
369 for(bank=0; bank<NBANK(chip); bank++) {
Geert Uytterhoevene0a86042016-03-11 17:31:27 +0100370 unsigned bankmask = mask[bank / sizeof(*mask)] >>
371 ((bank % sizeof(*mask)) * 8);
Phil Reidb4818af2015-12-04 15:52:30 +0800372 if(bankmask) {
Geert Uytterhoevene0a86042016-03-11 17:31:27 +0100373 unsigned bankval = bits[bank / sizeof(*bits)] >>
374 ((bank % sizeof(*bits)) * 8);
Phil Reidb4818af2015-12-04 15:52:30 +0800375 reg_val[bank] = (reg_val[bank] & ~bankmask) | bankval;
376 }
377 }
378 ret = i2c_smbus_write_i2c_block_data(chip->client, offset << bank_shift, NBANK(chip), reg_val);
379 if (ret)
380 goto exit;
381
382 memcpy(chip->reg_output, reg_val, NBANK(chip));
383exit:
384 mutex_unlock(&chip->i2c_lock);
385}
386
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800387static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800388{
389 struct gpio_chip *gc;
390
391 gc = &chip->gpio_chip;
392
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800393 gc->direction_input = pca953x_gpio_direction_input;
394 gc->direction_output = pca953x_gpio_direction_output;
395 gc->get = pca953x_gpio_get_value;
396 gc->set = pca953x_gpio_set_value;
Phil Reidb4818af2015-12-04 15:52:30 +0800397 gc->set_multiple = pca953x_gpio_set_multiple;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100398 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800399
400 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800401 gc->ngpio = gpios;
402 gc->label = chip->client->name;
Linus Walleij58383c782015-11-04 09:56:26 +0100403 gc->parent = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700404 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700405 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800406}
407
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800408#ifdef CONFIG_GPIO_PCA953X_IRQ
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800409static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800410{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200411 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100412 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800413
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100414 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800415}
416
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800417static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800418{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200419 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100420 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800421
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100422 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800423}
424
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800425static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800426{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200427 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100428 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800429
430 mutex_lock(&chip->irq_lock);
431}
432
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800433static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800434{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200435 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100436 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100437 u8 new_irqs;
438 int level, i;
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700439
440 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100441 for (i = 0; i < NBANK(chip); i++) {
442 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
443 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700444
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100445 while (new_irqs) {
446 level = __ffs(new_irqs);
447 pca953x_gpio_direction_input(&chip->gpio_chip,
448 level + (BANK_SZ * i));
449 new_irqs &= ~(1 << level);
450 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700451 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800452
453 mutex_unlock(&chip->irq_lock);
454}
455
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800456static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800457{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200458 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100459 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100460 int bank_nb = d->hwirq / BANK_SZ;
461 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800462
463 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
464 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800465 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800466 return -EINVAL;
467 }
468
469 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100470 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800471 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100472 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800473
474 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100475 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800476 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100477 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800478
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700479 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800480}
481
482static struct irq_chip pca953x_irq_chip = {
483 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800484 .irq_mask = pca953x_irq_mask,
485 .irq_unmask = pca953x_irq_unmask,
486 .irq_bus_lock = pca953x_irq_bus_lock,
487 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
488 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800489};
490
Joshua Scottb6ac1282015-05-22 12:35:12 +1200491static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800492{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100493 u8 cur_stat[MAX_BANK];
494 u8 old_stat[MAX_BANK];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200495 bool pending_seen = false;
496 bool trigger_seen = false;
497 u8 trigger[MAX_BANK];
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100498 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800499
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800500 switch (chip->chip_type) {
501 case PCA953X_TYPE:
502 offset = PCA953X_INPUT;
503 break;
504 case PCA957X_TYPE:
505 offset = PCA957X_IN;
506 break;
507 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100508 ret = pca953x_read_regs(chip, offset, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800509 if (ret)
Joshua Scottb6ac1282015-05-22 12:35:12 +1200510 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800511
512 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100513 for (i = 0; i < NBANK(chip); i++)
514 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800515
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100516 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800517
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100518 for (i = 0; i < NBANK(chip); i++) {
519 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200520 if (trigger[i])
521 trigger_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100522 }
523
Joshua Scottb6ac1282015-05-22 12:35:12 +1200524 if (!trigger_seen)
525 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800526
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100527 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800528
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100529 for (i = 0; i < NBANK(chip); i++) {
530 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
531 (cur_stat[i] & chip->irq_trig_raise[i]);
532 pending[i] &= trigger[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200533 if (pending[i])
534 pending_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100535 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800536
Joshua Scottb6ac1282015-05-22 12:35:12 +1200537 return pending_seen;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800538}
539
540static irqreturn_t pca953x_irq_handler(int irq, void *devid)
541{
542 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100543 u8 pending[MAX_BANK];
544 u8 level;
Toby Smith3275d072014-04-30 18:01:40 +1000545 unsigned nhandled = 0;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100546 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800547
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100548 if (!pca953x_irq_pending(chip, pending))
Toby Smith3275d072014-04-30 18:01:40 +1000549 return IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800550
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100551 for (i = 0; i < NBANK(chip); i++) {
552 while (pending[i]) {
553 level = __ffs(pending[i]);
Linus Walleij7bcbce52014-05-09 13:27:57 +0200554 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100555 level + (BANK_SZ * i)));
556 pending[i] &= ~(1 << level);
Toby Smith3275d072014-04-30 18:01:40 +1000557 nhandled++;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100558 }
559 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800560
Toby Smith3275d072014-04-30 18:01:40 +1000561 return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800562}
563
564static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200565 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800566{
567 struct i2c_client *client = chip->client;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100568 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800569
Markus Pargmann4bb93342014-07-29 09:24:43 +0200570 if (client->irq && irq_base != -1
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300571 && (chip->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800572
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800573 switch (chip->chip_type) {
574 case PCA953X_TYPE:
575 offset = PCA953X_INPUT;
576 break;
577 case PCA957X_TYPE:
578 offset = PCA957X_IN;
579 break;
580 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100581 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800582 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100583 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800584
585 /*
586 * There is no way to know which GPIO line generated the
587 * interrupt. We have to rely on the previous read for
588 * this purpose.
589 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100590 for (i = 0; i < NBANK(chip); i++)
591 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800592 mutex_init(&chip->irq_lock);
593
Linus Walleijb42748c2013-01-25 17:59:28 +0100594 ret = devm_request_threaded_irq(&client->dev,
595 client->irq,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800596 NULL,
597 pca953x_irq_handler,
Toby Smith91329132014-04-30 18:01:41 +1000598 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
599 IRQF_SHARED,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800600 dev_name(&client->dev), chip);
601 if (ret) {
602 dev_err(&client->dev, "failed to request irq %d\n",
603 client->irq);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100604 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800605 }
606
Linus Walleij7bcbce52014-05-09 13:27:57 +0200607 ret = gpiochip_irqchip_add(&chip->gpio_chip,
608 &pca953x_irq_chip,
609 irq_base,
610 handle_simple_irq,
611 IRQ_TYPE_NONE);
612 if (ret) {
613 dev_err(&client->dev,
614 "could not connect irqchip to gpiochip\n");
615 return ret;
616 }
Grygorii Strashkofdd50402015-07-07 17:34:49 +0300617
618 gpiochip_set_chained_irqchip(&chip->gpio_chip,
619 &pca953x_irq_chip,
620 client->irq, NULL);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800621 }
622
623 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800624}
625
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800626#else /* CONFIG_GPIO_PCA953X_IRQ */
627static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200628 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800629{
630 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800631
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300632 if (irq_base != -1 && (chip->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800633 dev_warn(&client->dev, "interrupt support not compiled in\n");
634
635 return 0;
636}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800637#endif
638
Bill Pemberton38363092012-11-19 13:22:34 -0500639static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800640{
641 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100642 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800643
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100644 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800645 if (ret)
646 goto out;
647
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100648 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
649 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800650 if (ret)
651 goto out;
652
653 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100654 if (invert)
655 memset(val, 0xFF, NBANK(chip));
656 else
657 memset(val, 0, NBANK(chip));
658
659 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800660out:
661 return ret;
662}
663
Bill Pemberton38363092012-11-19 13:22:34 -0500664static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800665{
666 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100667 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800668
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100669 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800670 if (ret)
671 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100672 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800673 if (ret)
674 goto out;
675
676 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100677 if (invert)
678 memset(val, 0xFF, NBANK(chip));
679 else
680 memset(val, 0, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400681 ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
682 if (ret)
683 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800684
Colin Cronin20a8a962015-05-18 11:41:43 -0700685 /* To enable register 6, 7 to control pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100686 memset(val, 0x02, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400687 ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
688 if (ret)
689 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800690
691 return 0;
692out:
693 return ret;
694}
695
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000696static const struct of_device_id pca953x_dt_ids[];
697
Bill Pemberton38363092012-11-19 13:22:34 -0500698static int pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200699 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800700{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800701 struct pca953x_platform_data *pdata;
702 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530703 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200704 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530705 u32 invert = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800706
Linus Walleijb42748c2013-01-25 17:59:28 +0100707 chip = devm_kzalloc(&client->dev,
708 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800709 if (chip == NULL)
710 return -ENOMEM;
711
Jingoo Hane56aee12013-07-30 17:08:05 +0900712 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200713 if (pdata) {
714 irq_base = pdata->irq_base;
715 chip->gpio_start = pdata->gpio_base;
716 invert = pdata->invert;
717 chip->names = pdata->names;
718 } else {
Markus Pargmann4bb93342014-07-29 09:24:43 +0200719 chip->gpio_start = -1;
720 irq_base = 0;
Nate Case1965d302009-06-17 16:26:17 -0700721 }
722
eric miao9e60fdc2008-02-04 22:28:26 -0800723 chip->client = client;
724
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300725 if (id) {
726 chip->driver_data = id->driver_data;
727 } else {
728 const struct acpi_device_id *id;
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000729 const struct of_device_id *match;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300730
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000731 match = of_match_device(pca953x_dt_ids, &client->dev);
732 if (match) {
733 chip->driver_data = (int)(uintptr_t)match->data;
734 } else {
735 id = acpi_match_device(pca953x_acpi_ids, &client->dev);
736 if (!id)
737 return -ENODEV;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300738
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000739 chip->driver_data = id->driver_data;
740 }
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300741 }
742
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300743 chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700744
Roland Stigge6e20fb12011-02-10 15:01:23 -0800745 mutex_init(&chip->i2c_lock);
746
eric miao9e60fdc2008-02-04 22:28:26 -0800747 /* initialize cached registers from their original values.
748 * we can't share this chip with another i2c master.
749 */
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300750 pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800751
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800752 if (chip->chip_type == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200753 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800754 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200755 ret = device_pca957x_init(chip, invert);
756 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100757 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800758
Laxman Dewangan0ece84f2016-02-22 17:43:28 +0530759 ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800760 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100761 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800762
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300763 ret = pca953x_irq_setup(chip, irq_base);
eric miao9e60fdc2008-02-04 22:28:26 -0800764 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100765 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800766
David Janderc6dcf592011-06-14 11:00:55 +0200767 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800768 ret = pdata->setup(client, chip->gpio_chip.base,
769 chip->gpio_chip.ngpio, pdata->context);
770 if (ret < 0)
771 dev_warn(&client->dev, "setup failed, %d\n", ret);
772 }
773
774 i2c_set_clientdata(client, chip);
775 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800776}
777
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800778static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800779{
Jingoo Hane56aee12013-07-30 17:08:05 +0900780 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800781 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800782 int ret = 0;
783
David Janderc6dcf592011-06-14 11:00:55 +0200784 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800785 ret = pdata->teardown(client, chip->gpio_chip.base,
786 chip->gpio_chip.ngpio, pdata->context);
787 if (ret < 0) {
788 dev_err(&client->dev, "%s failed, %d\n",
789 "teardown", ret);
790 return ret;
791 }
792 }
793
eric miao9e60fdc2008-02-04 22:28:26 -0800794 return 0;
795}
796
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000797/* convenience to stop overlong match-table lines */
798#define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
799#define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
800
Maxime Riparded326202012-11-08 18:01:52 +0100801static const struct of_device_id pca953x_dt_ids[] = {
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000802 { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
803 { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
804 { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
805 { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
806 { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
807 { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
808 { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
809 { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
810 { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
811 { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
812 { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
813 { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
814 { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
815 { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
Maxime Riparded326202012-11-08 18:01:52 +0100816
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000817 { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
818 { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
819 { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
820 { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
Maxime Riparded326202012-11-08 18:01:52 +0100821
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000822 { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
823 { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
824 { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
825 { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600826
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000827 { .compatible = "onsemi,pca9654", .data = OF_953X( 8, PCA_INT), },
Ben Dooks9f49f6d2015-12-08 10:19:25 +0000828
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000829 { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
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,
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300839 .acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
eric miao9e60fdc2008-02-04 22:28:26 -0800840 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800841 .probe = pca953x_probe,
842 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200843 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800844};
845
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800846static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800847{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800848 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800849}
David Brownell2f8d1192008-10-15 22:03:13 -0700850/* register after i2c postcore initcall and before
851 * subsys initcalls that may rely on these GPIOs
852 */
853subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800854
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800855static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800856{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800857 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800858}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800859module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800860
861MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800862MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800863MODULE_LICENSE("GPL");