blob: d8233bed4234b49a0d738c09bdcd81dc00b48feb [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>
Yong Li9b8e3ec2016-03-30 14:49:14 +080021#include <asm/unaligned.h>
Nate Case1965d302009-06-17 16:26:17 -070022#include <linux/of_platform.h>
Andy Shevchenkof32517b2015-10-01 14:20:28 +030023#include <linux/acpi.h>
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
Yong Li44896be2016-04-07 12:56:32 +080041#define PCAL953X_IN_LATCH 34
42#define PCAL953X_INT_MASK 37
43#define PCAL953X_INT_STAT 38
44
Haojian Zhuang33226ff2011-04-18 22:12:46 +080045#define PCA_GPIO_MASK 0x00FF
46#define PCA_INT 0x0100
Andy Shevchenko8c7a92d2016-05-31 17:05:57 +030047#define PCA_PCAL 0x0200
Haojian Zhuang33226ff2011-04-18 22:12:46 +080048#define PCA953X_TYPE 0x1000
49#define PCA957X_TYPE 0x2000
Andy Shevchenkoc6664142015-10-01 14:20:27 +030050#define PCA_TYPE_MASK 0xF000
51
52#define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080053
Jean Delvare3760f732008-04-29 23:11:40 +020054static const struct i2c_device_id pca953x_id[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +010055 { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080056 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
57 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
58 { "pca9536", 4 | PCA953X_TYPE, },
59 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
60 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
61 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
62 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
63 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
64 { "pca9556", 8 | PCA953X_TYPE, },
65 { "pca9557", 8 | PCA953X_TYPE, },
66 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
67 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +010068 { "pca9698", 40 | PCA953X_TYPE, },
David Brownellab5dc372009-01-06 14:42:27 -080069
Haojian Zhuang33226ff2011-04-18 22:12:46 +080070 { "max7310", 8 | PCA953X_TYPE, },
71 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
72 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
73 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
74 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
75 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
76 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
Andreas Schallenbergae79c192012-05-09 09:46:17 +020077 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
Thierry Reding2db8aba2015-09-29 12:55:44 +020078 { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
Aaron Sierrae73760a2014-02-07 16:36:21 -060079 { "xra1202", 8 | PCA953X_TYPE },
Jean Delvare3760f732008-04-29 23:11:40 +020080 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080081};
Jean Delvare3760f732008-04-29 23:11:40 +020082MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080083
Andy Shevchenkof32517b2015-10-01 14:20:28 +030084static const struct acpi_device_id pca953x_acpi_ids[] = {
Yong Li44896be2016-04-07 12:56:32 +080085 { "INT3491", 16 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },
Andy Shevchenkof32517b2015-10-01 14:20:28 +030086 { }
87};
88MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
89
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010090#define MAX_BANK 5
91#define BANK_SZ 8
92
93#define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)
94
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080095struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080096 unsigned gpio_start;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010097 u8 reg_output[MAX_BANK];
98 u8 reg_direction[MAX_BANK];
Roland Stigge6e20fb12011-02-10 15:01:23 -080099 struct mutex i2c_lock;
eric miao9e60fdc2008-02-04 22:28:26 -0800100
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800101#ifdef CONFIG_GPIO_PCA953X_IRQ
102 struct mutex irq_lock;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100103 u8 irq_mask[MAX_BANK];
104 u8 irq_stat[MAX_BANK];
105 u8 irq_trig_raise[MAX_BANK];
106 u8 irq_trig_fall[MAX_BANK];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800107#endif
108
eric miao9e60fdc2008-02-04 22:28:26 -0800109 struct i2c_client *client;
110 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700111 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800112 int chip_type;
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300113 unsigned long driver_data;
eric miao9e60fdc2008-02-04 22:28:26 -0800114};
115
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100116static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
117 int off)
118{
119 int ret;
120 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
121 int offset = off / BANK_SZ;
122
123 ret = i2c_smbus_read_byte_data(chip->client,
124 (reg << bank_shift) + offset);
125 *val = ret;
126
127 if (ret < 0) {
128 dev_err(&chip->client->dev, "failed reading register\n");
129 return ret;
130 }
131
132 return 0;
133}
134
135static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
136 int off)
137{
Andy Shevchenko8c7a92d2016-05-31 17:05:57 +0300138 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100139 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
140 int offset = off / BANK_SZ;
141
142 ret = i2c_smbus_write_byte_data(chip->client,
143 (reg << bank_shift) + offset, val);
144
145 if (ret < 0) {
146 dev_err(&chip->client->dev, "failed writing register\n");
147 return ret;
148 }
149
150 return 0;
151}
152
153static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800154{
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800155 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800156
157 if (chip->gpio_chip.ngpio <= 8)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100158 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
159 else if (chip->gpio_chip.ngpio >= 24) {
160 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200161 ret = i2c_smbus_write_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100162 (reg << bank_shift) | REG_ADDR_AI,
163 NBANK(chip), val);
Laurent Navet50e44432013-03-20 13:15:58 +0100164 } else {
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800165 switch (chip->chip_type) {
166 case PCA953X_TYPE:
167 ret = i2c_smbus_write_word_data(chip->client,
Yong Li9b8e3ec2016-03-30 14:49:14 +0800168 reg << 1, cpu_to_le16(get_unaligned((u16 *)val)));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800169 break;
170 case PCA957X_TYPE:
171 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100172 val[0]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800173 if (ret < 0)
174 break;
175 ret = i2c_smbus_write_byte_data(chip->client,
176 (reg << 1) + 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100177 val[1]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800178 break;
179 }
180 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800181
182 if (ret < 0) {
183 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800184 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800185 }
186
187 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800188}
189
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100190static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800191{
192 int ret;
193
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200194 if (chip->gpio_chip.ngpio <= 8) {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800195 ret = i2c_smbus_read_byte_data(chip->client, reg);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200196 *val = ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100197 } else if (chip->gpio_chip.ngpio >= 24) {
198 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
199
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200200 ret = i2c_smbus_read_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100201 (reg << bank_shift) | REG_ADDR_AI,
202 NBANK(chip), val);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200203 } else {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800204 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100205 val[0] = (u16)ret & 0xFF;
206 val[1] = (u16)ret >> 8;
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200207 }
eric miao9e60fdc2008-02-04 22:28:26 -0800208 if (ret < 0) {
209 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800210 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800211 }
212
eric miao9e60fdc2008-02-04 22:28:26 -0800213 return 0;
214}
215
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800216static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800217{
Linus Walleij468e67f2015-12-07 11:20:54 +0100218 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100219 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800220 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800221
Roland Stigge6e20fb12011-02-10 15:01:23 -0800222 mutex_lock(&chip->i2c_lock);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100223 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800224
225 switch (chip->chip_type) {
226 case PCA953X_TYPE:
227 offset = PCA953X_DIRECTION;
228 break;
229 case PCA957X_TYPE:
230 offset = PCA957X_CFG;
231 break;
232 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100233 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800234 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800235 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800236
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100237 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800238exit:
239 mutex_unlock(&chip->i2c_lock);
240 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800241}
242
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800243static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800244 unsigned off, int val)
245{
Linus Walleij468e67f2015-12-07 11:20:54 +0100246 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100247 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800248 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800249
Roland Stigge6e20fb12011-02-10 15:01:23 -0800250 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800251 /* set output level */
252 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100253 reg_val = chip->reg_output[off / BANK_SZ]
254 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800255 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100256 reg_val = chip->reg_output[off / BANK_SZ]
257 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800258
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800259 switch (chip->chip_type) {
260 case PCA953X_TYPE:
261 offset = PCA953X_OUTPUT;
262 break;
263 case PCA957X_TYPE:
264 offset = PCA957X_OUT;
265 break;
266 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100267 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800268 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800269 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800270
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100271 chip->reg_output[off / BANK_SZ] = reg_val;
eric miao9e60fdc2008-02-04 22:28:26 -0800272
273 /* then direction */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100274 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800275 switch (chip->chip_type) {
276 case PCA953X_TYPE:
277 offset = PCA953X_DIRECTION;
278 break;
279 case PCA957X_TYPE:
280 offset = PCA957X_CFG;
281 break;
282 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100283 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800284 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800285 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800286
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100287 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800288exit:
289 mutex_unlock(&chip->i2c_lock);
290 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800291}
292
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800293static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800294{
Linus Walleij468e67f2015-12-07 11:20:54 +0100295 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200296 u32 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800297 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800298
Roland Stigge6e20fb12011-02-10 15:01:23 -0800299 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800300 switch (chip->chip_type) {
301 case PCA953X_TYPE:
302 offset = PCA953X_INPUT;
303 break;
304 case PCA957X_TYPE:
305 offset = PCA957X_IN;
306 break;
307 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100308 ret = pca953x_read_single(chip, offset, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800309 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800310 if (ret < 0) {
311 /* NOTE: diagnostic already emitted; that's all we should
312 * do unless gpio_*_value_cansleep() calls become different
313 * from their nonsleeping siblings (and report faults).
314 */
315 return 0;
316 }
317
Andrew Ruder40a625d2013-08-07 16:52:40 -0500318 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800319}
320
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800321static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800322{
Linus Walleij468e67f2015-12-07 11:20:54 +0100323 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100324 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800325 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800326
Roland Stigge6e20fb12011-02-10 15:01:23 -0800327 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800328 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100329 reg_val = chip->reg_output[off / BANK_SZ]
330 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800331 else
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
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800335 switch (chip->chip_type) {
336 case PCA953X_TYPE:
337 offset = PCA953X_OUTPUT;
338 break;
339 case PCA957X_TYPE:
340 offset = PCA957X_OUT;
341 break;
342 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100343 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800344 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800345 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800346
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100347 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800348exit:
349 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800350}
351
Phil Reidb4818af2015-12-04 15:52:30 +0800352static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
353 unsigned long *mask, unsigned long *bits)
354{
Linus Walleij468e67f2015-12-07 11:20:54 +0100355 struct pca953x_chip *chip = gpiochip_get_data(gc);
Phil Reidb4818af2015-12-04 15:52:30 +0800356 u8 reg_val[MAX_BANK];
357 int ret, offset = 0;
358 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
359 int bank;
360
361 switch (chip->chip_type) {
362 case PCA953X_TYPE:
363 offset = PCA953X_OUTPUT;
364 break;
365 case PCA957X_TYPE:
366 offset = PCA957X_OUT;
367 break;
368 }
369
370 memcpy(reg_val, chip->reg_output, NBANK(chip));
371 mutex_lock(&chip->i2c_lock);
372 for(bank=0; bank<NBANK(chip); bank++) {
Geert Uytterhoevene0a86042016-03-11 17:31:27 +0100373 unsigned bankmask = mask[bank / sizeof(*mask)] >>
374 ((bank % sizeof(*mask)) * 8);
Phil Reidb4818af2015-12-04 15:52:30 +0800375 if(bankmask) {
Geert Uytterhoevene0a86042016-03-11 17:31:27 +0100376 unsigned bankval = bits[bank / sizeof(*bits)] >>
377 ((bank % sizeof(*bits)) * 8);
Phil Reidb4818af2015-12-04 15:52:30 +0800378 reg_val[bank] = (reg_val[bank] & ~bankmask) | bankval;
379 }
380 }
381 ret = i2c_smbus_write_i2c_block_data(chip->client, offset << bank_shift, NBANK(chip), reg_val);
382 if (ret)
383 goto exit;
384
385 memcpy(chip->reg_output, reg_val, NBANK(chip));
386exit:
387 mutex_unlock(&chip->i2c_lock);
388}
389
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800390static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800391{
392 struct gpio_chip *gc;
393
394 gc = &chip->gpio_chip;
395
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800396 gc->direction_input = pca953x_gpio_direction_input;
397 gc->direction_output = pca953x_gpio_direction_output;
398 gc->get = pca953x_gpio_get_value;
399 gc->set = pca953x_gpio_set_value;
Phil Reidb4818af2015-12-04 15:52:30 +0800400 gc->set_multiple = pca953x_gpio_set_multiple;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100401 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800402
403 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800404 gc->ngpio = gpios;
405 gc->label = chip->client->name;
Linus Walleij58383c782015-11-04 09:56:26 +0100406 gc->parent = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700407 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700408 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800409}
410
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800411#ifdef CONFIG_GPIO_PCA953X_IRQ
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800412static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800413{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200414 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100415 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800416
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100417 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800418}
419
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800420static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800421{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200422 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100423 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800424
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100425 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800426}
427
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800428static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800429{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200430 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100431 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800432
433 mutex_lock(&chip->irq_lock);
434}
435
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800436static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800437{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200438 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100439 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100440 u8 new_irqs;
441 int level, i;
Yong Li44896be2016-04-07 12:56:32 +0800442 u8 invert_irq_mask[MAX_BANK];
443
444 if (chip->driver_data & PCA_PCAL) {
445 /* Enable latch on interrupt-enabled inputs */
446 pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
447
448 for (i = 0; i < NBANK(chip); i++)
449 invert_irq_mask[i] = ~chip->irq_mask[i];
450
451 /* Unmask enabled interrupts */
452 pca953x_write_regs(chip, PCAL953X_INT_MASK, invert_irq_mask);
453 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700454
455 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100456 for (i = 0; i < NBANK(chip); i++) {
457 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
458 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700459
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100460 while (new_irqs) {
461 level = __ffs(new_irqs);
462 pca953x_gpio_direction_input(&chip->gpio_chip,
463 level + (BANK_SZ * i));
464 new_irqs &= ~(1 << level);
465 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700466 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800467
468 mutex_unlock(&chip->irq_lock);
469}
470
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800471static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800472{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200473 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100474 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100475 int bank_nb = d->hwirq / BANK_SZ;
476 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800477
478 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
479 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800480 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800481 return -EINVAL;
482 }
483
484 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100485 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800486 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100487 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800488
489 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100490 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800491 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100492 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800493
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700494 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800495}
496
497static struct irq_chip pca953x_irq_chip = {
498 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800499 .irq_mask = pca953x_irq_mask,
500 .irq_unmask = pca953x_irq_unmask,
501 .irq_bus_lock = pca953x_irq_bus_lock,
502 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
503 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800504};
505
Joshua Scottb6ac1282015-05-22 12:35:12 +1200506static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800507{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100508 u8 cur_stat[MAX_BANK];
509 u8 old_stat[MAX_BANK];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200510 bool pending_seen = false;
511 bool trigger_seen = false;
512 u8 trigger[MAX_BANK];
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100513 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800514
Yong Li44896be2016-04-07 12:56:32 +0800515 if (chip->driver_data & PCA_PCAL) {
516 /* Read the current interrupt status from the device */
517 ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
518 if (ret)
519 return false;
520
521 /* Check latched inputs and clear interrupt status */
522 ret = pca953x_read_regs(chip, PCA953X_INPUT, cur_stat);
523 if (ret)
524 return false;
525
526 for (i = 0; i < NBANK(chip); i++) {
527 /* Apply filter for rising/falling edge selection */
528 pending[i] = (~cur_stat[i] & chip->irq_trig_fall[i]) |
529 (cur_stat[i] & chip->irq_trig_raise[i]);
530 pending[i] &= trigger[i];
531 if (pending[i])
532 pending_seen = true;
533 }
534
535 return pending_seen;
536 }
537
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800538 switch (chip->chip_type) {
539 case PCA953X_TYPE:
540 offset = PCA953X_INPUT;
541 break;
542 case PCA957X_TYPE:
543 offset = PCA957X_IN;
544 break;
545 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100546 ret = pca953x_read_regs(chip, offset, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800547 if (ret)
Joshua Scottb6ac1282015-05-22 12:35:12 +1200548 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800549
550 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100551 for (i = 0; i < NBANK(chip); i++)
552 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800553
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100554 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800555
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100556 for (i = 0; i < NBANK(chip); i++) {
557 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200558 if (trigger[i])
559 trigger_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100560 }
561
Joshua Scottb6ac1282015-05-22 12:35:12 +1200562 if (!trigger_seen)
563 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800564
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100565 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800566
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100567 for (i = 0; i < NBANK(chip); i++) {
568 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
569 (cur_stat[i] & chip->irq_trig_raise[i]);
570 pending[i] &= trigger[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200571 if (pending[i])
572 pending_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100573 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800574
Joshua Scottb6ac1282015-05-22 12:35:12 +1200575 return pending_seen;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800576}
577
578static irqreturn_t pca953x_irq_handler(int irq, void *devid)
579{
580 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100581 u8 pending[MAX_BANK];
582 u8 level;
Toby Smith3275d072014-04-30 18:01:40 +1000583 unsigned nhandled = 0;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100584 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800585
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100586 if (!pca953x_irq_pending(chip, pending))
Toby Smith3275d072014-04-30 18:01:40 +1000587 return IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800588
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100589 for (i = 0; i < NBANK(chip); i++) {
590 while (pending[i]) {
591 level = __ffs(pending[i]);
Linus Walleij7bcbce52014-05-09 13:27:57 +0200592 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100593 level + (BANK_SZ * i)));
594 pending[i] &= ~(1 << level);
Toby Smith3275d072014-04-30 18:01:40 +1000595 nhandled++;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100596 }
597 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800598
Toby Smith3275d072014-04-30 18:01:40 +1000599 return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800600}
601
602static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200603 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800604{
605 struct i2c_client *client = chip->client;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100606 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800607
Markus Pargmann4bb93342014-07-29 09:24:43 +0200608 if (client->irq && irq_base != -1
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300609 && (chip->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800610
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800611 switch (chip->chip_type) {
612 case PCA953X_TYPE:
613 offset = PCA953X_INPUT;
614 break;
615 case PCA957X_TYPE:
616 offset = PCA957X_IN;
617 break;
618 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100619 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800620 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100621 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800622
623 /*
624 * There is no way to know which GPIO line generated the
625 * interrupt. We have to rely on the previous read for
626 * this purpose.
627 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100628 for (i = 0; i < NBANK(chip); i++)
629 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800630 mutex_init(&chip->irq_lock);
631
Linus Walleijb42748c2013-01-25 17:59:28 +0100632 ret = devm_request_threaded_irq(&client->dev,
633 client->irq,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800634 NULL,
635 pca953x_irq_handler,
Toby Smith91329132014-04-30 18:01:41 +1000636 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
637 IRQF_SHARED,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800638 dev_name(&client->dev), chip);
639 if (ret) {
640 dev_err(&client->dev, "failed to request irq %d\n",
641 client->irq);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100642 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800643 }
644
Linus Walleij7bcbce52014-05-09 13:27:57 +0200645 ret = gpiochip_irqchip_add(&chip->gpio_chip,
646 &pca953x_irq_chip,
647 irq_base,
648 handle_simple_irq,
649 IRQ_TYPE_NONE);
650 if (ret) {
651 dev_err(&client->dev,
652 "could not connect irqchip to gpiochip\n");
653 return ret;
654 }
Grygorii Strashkofdd50402015-07-07 17:34:49 +0300655
656 gpiochip_set_chained_irqchip(&chip->gpio_chip,
657 &pca953x_irq_chip,
658 client->irq, NULL);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800659 }
660
661 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800662}
663
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800664#else /* CONFIG_GPIO_PCA953X_IRQ */
665static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200666 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800667{
668 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800669
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300670 if (irq_base != -1 && (chip->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800671 dev_warn(&client->dev, "interrupt support not compiled in\n");
672
673 return 0;
674}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800675#endif
676
Bill Pemberton38363092012-11-19 13:22:34 -0500677static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800678{
679 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100680 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800681
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100682 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800683 if (ret)
684 goto out;
685
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100686 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
687 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800688 if (ret)
689 goto out;
690
691 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100692 if (invert)
693 memset(val, 0xFF, NBANK(chip));
694 else
695 memset(val, 0, NBANK(chip));
696
697 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800698out:
699 return ret;
700}
701
Bill Pemberton38363092012-11-19 13:22:34 -0500702static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800703{
704 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100705 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800706
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100707 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800708 if (ret)
709 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100710 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800711 if (ret)
712 goto out;
713
714 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100715 if (invert)
716 memset(val, 0xFF, NBANK(chip));
717 else
718 memset(val, 0, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400719 ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
720 if (ret)
721 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800722
Colin Cronin20a8a962015-05-18 11:41:43 -0700723 /* To enable register 6, 7 to control pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100724 memset(val, 0x02, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400725 ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
726 if (ret)
727 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800728
729 return 0;
730out:
731 return ret;
732}
733
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000734static const struct of_device_id pca953x_dt_ids[];
735
Bill Pemberton38363092012-11-19 13:22:34 -0500736static int pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200737 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800738{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800739 struct pca953x_platform_data *pdata;
740 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530741 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200742 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530743 u32 invert = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800744
Linus Walleijb42748c2013-01-25 17:59:28 +0100745 chip = devm_kzalloc(&client->dev,
746 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800747 if (chip == NULL)
748 return -ENOMEM;
749
Jingoo Hane56aee12013-07-30 17:08:05 +0900750 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200751 if (pdata) {
752 irq_base = pdata->irq_base;
753 chip->gpio_start = pdata->gpio_base;
754 invert = pdata->invert;
755 chip->names = pdata->names;
756 } else {
Markus Pargmann4bb93342014-07-29 09:24:43 +0200757 chip->gpio_start = -1;
758 irq_base = 0;
Nate Case1965d302009-06-17 16:26:17 -0700759 }
760
eric miao9e60fdc2008-02-04 22:28:26 -0800761 chip->client = client;
762
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300763 if (id) {
764 chip->driver_data = id->driver_data;
765 } else {
766 const struct acpi_device_id *id;
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000767 const struct of_device_id *match;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300768
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000769 match = of_match_device(pca953x_dt_ids, &client->dev);
770 if (match) {
771 chip->driver_data = (int)(uintptr_t)match->data;
772 } else {
773 id = acpi_match_device(pca953x_acpi_ids, &client->dev);
774 if (!id)
775 return -ENODEV;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300776
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000777 chip->driver_data = id->driver_data;
778 }
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300779 }
780
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300781 chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700782
Roland Stigge6e20fb12011-02-10 15:01:23 -0800783 mutex_init(&chip->i2c_lock);
784
eric miao9e60fdc2008-02-04 22:28:26 -0800785 /* initialize cached registers from their original values.
786 * we can't share this chip with another i2c master.
787 */
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300788 pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800789
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800790 if (chip->chip_type == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200791 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800792 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200793 ret = device_pca957x_init(chip, invert);
794 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100795 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800796
Laxman Dewangan0ece84f2016-02-22 17:43:28 +0530797 ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800798 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100799 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800800
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300801 ret = pca953x_irq_setup(chip, irq_base);
eric miao9e60fdc2008-02-04 22:28:26 -0800802 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100803 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800804
David Janderc6dcf592011-06-14 11:00:55 +0200805 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800806 ret = pdata->setup(client, chip->gpio_chip.base,
807 chip->gpio_chip.ngpio, pdata->context);
808 if (ret < 0)
809 dev_warn(&client->dev, "setup failed, %d\n", ret);
810 }
811
812 i2c_set_clientdata(client, chip);
813 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800814}
815
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800816static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800817{
Jingoo Hane56aee12013-07-30 17:08:05 +0900818 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800819 struct pca953x_chip *chip = i2c_get_clientdata(client);
Andy Shevchenko8c7a92d2016-05-31 17:05:57 +0300820 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800821
David Janderc6dcf592011-06-14 11:00:55 +0200822 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800823 ret = pdata->teardown(client, chip->gpio_chip.base,
824 chip->gpio_chip.ngpio, pdata->context);
825 if (ret < 0) {
826 dev_err(&client->dev, "%s failed, %d\n",
827 "teardown", ret);
828 return ret;
829 }
830 }
831
eric miao9e60fdc2008-02-04 22:28:26 -0800832 return 0;
833}
834
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000835/* convenience to stop overlong match-table lines */
836#define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
837#define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
838
Maxime Riparded326202012-11-08 18:01:52 +0100839static const struct of_device_id pca953x_dt_ids[] = {
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000840 { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
841 { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
842 { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
843 { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
844 { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
845 { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
846 { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
847 { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
848 { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
849 { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
850 { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
851 { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
852 { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
853 { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
Maxime Riparded326202012-11-08 18:01:52 +0100854
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000855 { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
856 { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
857 { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
858 { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
Maxime Riparded326202012-11-08 18:01:52 +0100859
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000860 { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
Vignesh R353661d2016-05-19 12:17:29 +0530861 { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000862 { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
863 { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
864 { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600865
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000866 { .compatible = "onsemi,pca9654", .data = OF_953X( 8, PCA_INT), },
Ben Dooks9f49f6d2015-12-08 10:19:25 +0000867
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000868 { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
Maxime Riparded326202012-11-08 18:01:52 +0100869 { }
870};
871
872MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
873
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800874static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800875 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800876 .name = "pca953x",
Maxime Riparded326202012-11-08 18:01:52 +0100877 .of_match_table = pca953x_dt_ids,
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300878 .acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
eric miao9e60fdc2008-02-04 22:28:26 -0800879 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800880 .probe = pca953x_probe,
881 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200882 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800883};
884
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800885static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800886{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800887 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800888}
David Brownell2f8d1192008-10-15 22:03:13 -0700889/* register after i2c postcore initcall and before
890 * subsys initcalls that may rely on these GPIOs
891 */
892subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800893
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800894static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800895{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800896 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800897}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800898module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800899
900MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800901MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800902MODULE_LICENSE("GPL");