blob: e7bf3b3cebcd1c13f3b66b378a913ac5a2524d3e [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>
Phil Reide23efa32016-07-29 11:39:55 +080024#include <linux/regulator/consumer.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
Yong Li44896be2016-04-07 12:56:32 +080042#define PCAL953X_IN_LATCH 34
43#define PCAL953X_INT_MASK 37
44#define PCAL953X_INT_STAT 38
45
Haojian Zhuang33226ff2011-04-18 22:12:46 +080046#define PCA_GPIO_MASK 0x00FF
47#define PCA_INT 0x0100
Andy Shevchenko8c7a92d2016-05-31 17:05:57 +030048#define PCA_PCAL 0x0200
Haojian Zhuang33226ff2011-04-18 22:12:46 +080049#define PCA953X_TYPE 0x1000
50#define PCA957X_TYPE 0x2000
Andy Shevchenkoc6664142015-10-01 14:20:27 +030051#define PCA_TYPE_MASK 0xF000
52
53#define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080054
Jean Delvare3760f732008-04-29 23:11:40 +020055static const struct i2c_device_id pca953x_id[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +010056 { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080057 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
58 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
59 { "pca9536", 4 | PCA953X_TYPE, },
60 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
61 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
62 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
63 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
64 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
65 { "pca9556", 8 | PCA953X_TYPE, },
66 { "pca9557", 8 | PCA953X_TYPE, },
67 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
68 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +010069 { "pca9698", 40 | PCA953X_TYPE, },
David Brownellab5dc372009-01-06 14:42:27 -080070
Andy Shevchenko747e42a1c2016-06-15 01:57:56 +030071 { "pcal9555a", 16 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },
72
Haojian Zhuang33226ff2011-04-18 22:12:46 +080073 { "max7310", 8 | PCA953X_TYPE, },
74 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
75 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
76 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
77 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
78 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
79 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
Andreas Schallenbergae79c192012-05-09 09:46:17 +020080 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
Thierry Reding2db8aba2015-09-29 12:55:44 +020081 { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
Aaron Sierrae73760a2014-02-07 16:36:21 -060082 { "xra1202", 8 | PCA953X_TYPE },
Jean Delvare3760f732008-04-29 23:11:40 +020083 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080084};
Jean Delvare3760f732008-04-29 23:11:40 +020085MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080086
Andy Shevchenkof32517b2015-10-01 14:20:28 +030087static const struct acpi_device_id pca953x_acpi_ids[] = {
Yong Li44896be2016-04-07 12:56:32 +080088 { "INT3491", 16 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },
Andy Shevchenkof32517b2015-10-01 14:20:28 +030089 { }
90};
91MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
92
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010093#define MAX_BANK 5
94#define BANK_SZ 8
95
Vignesh Ra246b812016-06-09 11:02:04 +053096#define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010097
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +020098struct pca953x_reg_config {
99 int direction;
100 int output;
101 int input;
102};
103
104static const struct pca953x_reg_config pca953x_regs = {
105 .direction = PCA953X_DIRECTION,
106 .output = PCA953X_OUTPUT,
107 .input = PCA953X_INPUT,
108};
109
110static const struct pca953x_reg_config pca957x_regs = {
111 .direction = PCA957X_CFG,
112 .output = PCA957X_OUT,
113 .input = PCA957X_IN,
114};
115
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800116struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -0800117 unsigned gpio_start;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100118 u8 reg_output[MAX_BANK];
119 u8 reg_direction[MAX_BANK];
Roland Stigge6e20fb12011-02-10 15:01:23 -0800120 struct mutex i2c_lock;
eric miao9e60fdc2008-02-04 22:28:26 -0800121
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800122#ifdef CONFIG_GPIO_PCA953X_IRQ
123 struct mutex irq_lock;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100124 u8 irq_mask[MAX_BANK];
125 u8 irq_stat[MAX_BANK];
126 u8 irq_trig_raise[MAX_BANK];
127 u8 irq_trig_fall[MAX_BANK];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800128#endif
129
eric miao9e60fdc2008-02-04 22:28:26 -0800130 struct i2c_client *client;
131 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700132 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800133 int chip_type;
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300134 unsigned long driver_data;
Phil Reide23efa32016-07-29 11:39:55 +0800135 struct regulator *regulator;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200136
137 const struct pca953x_reg_config *regs;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200138
139 int (*write_regs)(struct pca953x_chip *, int, u8 *);
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200140 int (*read_regs)(struct pca953x_chip *, int, u8 *);
eric miao9e60fdc2008-02-04 22:28:26 -0800141};
142
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100143static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
144 int off)
145{
146 int ret;
147 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
148 int offset = off / BANK_SZ;
149
150 ret = i2c_smbus_read_byte_data(chip->client,
151 (reg << bank_shift) + offset);
152 *val = ret;
153
154 if (ret < 0) {
155 dev_err(&chip->client->dev, "failed reading register\n");
156 return ret;
157 }
158
159 return 0;
160}
161
162static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
163 int off)
164{
Andy Shevchenko8c7a92d2016-05-31 17:05:57 +0300165 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100166 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
167 int offset = off / BANK_SZ;
168
169 ret = i2c_smbus_write_byte_data(chip->client,
170 (reg << bank_shift) + offset, val);
171
172 if (ret < 0) {
173 dev_err(&chip->client->dev, "failed writing register\n");
174 return ret;
175 }
176
177 return 0;
178}
179
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200180static int pca953x_write_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
181{
182 return i2c_smbus_write_byte_data(chip->client, reg, *val);
183}
184
185static int pca953x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
186{
187 __le16 word = cpu_to_le16(get_unaligned((u16 *)val));
188
189 return i2c_smbus_write_word_data(chip->client,
190 reg << 1, (__force u16)word);
191}
192
193static int pca957x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
194{
195 int ret;
196
197 ret = i2c_smbus_write_byte_data(chip->client, reg << 1, val[0]);
198 if (ret < 0)
199 return ret;
200
201 return i2c_smbus_write_byte_data(chip->client, (reg << 1) + 1, val[1]);
202}
203
204static int pca953x_write_regs_24(struct pca953x_chip *chip, int reg, u8 *val)
205{
206 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
207
208 return i2c_smbus_write_i2c_block_data(chip->client,
209 (reg << bank_shift) | REG_ADDR_AI,
210 NBANK(chip), val);
211}
212
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100213static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800214{
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800215 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800216
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200217 ret = chip->write_regs(chip, reg, val);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800218 if (ret < 0) {
219 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800220 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800221 }
222
223 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800224}
225
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200226static int pca953x_read_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
227{
228 int ret;
229
230 ret = i2c_smbus_read_byte_data(chip->client, reg);
231 *val = ret;
232
233 return ret;
234}
235
236static int pca953x_read_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
237{
238 int ret;
239
240 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
241 val[0] = (u16)ret & 0xFF;
242 val[1] = (u16)ret >> 8;
243
244 return ret;
245}
246
247static int pca953x_read_regs_24(struct pca953x_chip *chip, int reg, u8 *val)
248{
249 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
250
251 return i2c_smbus_read_i2c_block_data(chip->client,
252 (reg << bank_shift) | REG_ADDR_AI,
253 NBANK(chip), val);
254}
255
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100256static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800257{
258 int ret;
259
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200260 ret = chip->read_regs(chip, reg, val);
eric miao9e60fdc2008-02-04 22:28:26 -0800261 if (ret < 0) {
262 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800263 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800264 }
265
eric miao9e60fdc2008-02-04 22:28:26 -0800266 return 0;
267}
268
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800269static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800270{
Linus Walleij468e67f2015-12-07 11:20:54 +0100271 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100272 u8 reg_val;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200273 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800274
Roland Stigge6e20fb12011-02-10 15:01:23 -0800275 mutex_lock(&chip->i2c_lock);
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
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200278 ret = pca953x_write_single(chip, chip->regs->direction, 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 -0800283exit:
284 mutex_unlock(&chip->i2c_lock);
285 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800286}
287
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800288static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800289 unsigned off, int val)
290{
Linus Walleij468e67f2015-12-07 11:20:54 +0100291 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100292 u8 reg_val;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200293 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800294
Roland Stigge6e20fb12011-02-10 15:01:23 -0800295 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800296 /* set output level */
297 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100298 reg_val = chip->reg_output[off / BANK_SZ]
299 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800300 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100301 reg_val = chip->reg_output[off / BANK_SZ]
302 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800303
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200304 ret = pca953x_write_single(chip, chip->regs->output, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800305 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800306 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800307
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100308 chip->reg_output[off / BANK_SZ] = reg_val;
eric miao9e60fdc2008-02-04 22:28:26 -0800309
310 /* then direction */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100311 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200312 ret = pca953x_write_single(chip, chip->regs->direction, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800313 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800314 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800315
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100316 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800317exit:
318 mutex_unlock(&chip->i2c_lock);
319 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800320}
321
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800322static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800323{
Linus Walleij468e67f2015-12-07 11:20:54 +0100324 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200325 u32 reg_val;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200326 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800327
Roland Stigge6e20fb12011-02-10 15:01:23 -0800328 mutex_lock(&chip->i2c_lock);
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200329 ret = pca953x_read_single(chip, chip->regs->input, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800330 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800331 if (ret < 0) {
332 /* NOTE: diagnostic already emitted; that's all we should
333 * do unless gpio_*_value_cansleep() calls become different
334 * from their nonsleeping siblings (and report faults).
335 */
336 return 0;
337 }
338
Andrew Ruder40a625d2013-08-07 16:52:40 -0500339 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800340}
341
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800342static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800343{
Linus Walleij468e67f2015-12-07 11:20:54 +0100344 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100345 u8 reg_val;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200346 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800347
Roland Stigge6e20fb12011-02-10 15:01:23 -0800348 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800349 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100350 reg_val = chip->reg_output[off / BANK_SZ]
351 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800352 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100353 reg_val = chip->reg_output[off / BANK_SZ]
354 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800355
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200356 ret = pca953x_write_single(chip, chip->regs->output, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800357 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800358 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800359
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100360 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800361exit:
362 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800363}
364
Phil Reidb4818af2015-12-04 15:52:30 +0800365static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
366 unsigned long *mask, unsigned long *bits)
367{
Linus Walleij468e67f2015-12-07 11:20:54 +0100368 struct pca953x_chip *chip = gpiochip_get_data(gc);
Phil Reidb4818af2015-12-04 15:52:30 +0800369 u8 reg_val[MAX_BANK];
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200370 int ret;
Phil Reidb4818af2015-12-04 15:52:30 +0800371 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
372 int bank;
373
Phil Reidb4818af2015-12-04 15:52:30 +0800374 memcpy(reg_val, chip->reg_output, NBANK(chip));
375 mutex_lock(&chip->i2c_lock);
376 for(bank=0; bank<NBANK(chip); bank++) {
Geert Uytterhoevene0a86042016-03-11 17:31:27 +0100377 unsigned bankmask = mask[bank / sizeof(*mask)] >>
378 ((bank % sizeof(*mask)) * 8);
Phil Reidb4818af2015-12-04 15:52:30 +0800379 if(bankmask) {
Geert Uytterhoevene0a86042016-03-11 17:31:27 +0100380 unsigned bankval = bits[bank / sizeof(*bits)] >>
381 ((bank % sizeof(*bits)) * 8);
Phil Reidb4818af2015-12-04 15:52:30 +0800382 reg_val[bank] = (reg_val[bank] & ~bankmask) | bankval;
383 }
384 }
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200385 ret = i2c_smbus_write_i2c_block_data(chip->client,
386 chip->regs->output << bank_shift,
387 NBANK(chip), reg_val);
Phil Reidb4818af2015-12-04 15:52:30 +0800388 if (ret)
389 goto exit;
390
391 memcpy(chip->reg_output, reg_val, NBANK(chip));
392exit:
393 mutex_unlock(&chip->i2c_lock);
394}
395
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800396static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800397{
398 struct gpio_chip *gc;
399
400 gc = &chip->gpio_chip;
401
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800402 gc->direction_input = pca953x_gpio_direction_input;
403 gc->direction_output = pca953x_gpio_direction_output;
404 gc->get = pca953x_gpio_get_value;
405 gc->set = pca953x_gpio_set_value;
Phil Reidb4818af2015-12-04 15:52:30 +0800406 gc->set_multiple = pca953x_gpio_set_multiple;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100407 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800408
409 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800410 gc->ngpio = gpios;
411 gc->label = chip->client->name;
Linus Walleij58383c782015-11-04 09:56:26 +0100412 gc->parent = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700413 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700414 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800415}
416
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800417#ifdef CONFIG_GPIO_PCA953X_IRQ
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800418static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800419{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200420 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100421 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800422
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100423 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800424}
425
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800426static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800427{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200428 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100429 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800430
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100431 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800432}
433
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800434static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800435{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200436 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100437 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800438
439 mutex_lock(&chip->irq_lock);
440}
441
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800442static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800443{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200444 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100445 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100446 u8 new_irqs;
447 int level, i;
Yong Li44896be2016-04-07 12:56:32 +0800448 u8 invert_irq_mask[MAX_BANK];
449
450 if (chip->driver_data & PCA_PCAL) {
451 /* Enable latch on interrupt-enabled inputs */
452 pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
453
454 for (i = 0; i < NBANK(chip); i++)
455 invert_irq_mask[i] = ~chip->irq_mask[i];
456
457 /* Unmask enabled interrupts */
458 pca953x_write_regs(chip, PCAL953X_INT_MASK, invert_irq_mask);
459 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700460
461 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100462 for (i = 0; i < NBANK(chip); i++) {
463 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
464 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700465
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100466 while (new_irqs) {
467 level = __ffs(new_irqs);
468 pca953x_gpio_direction_input(&chip->gpio_chip,
469 level + (BANK_SZ * i));
470 new_irqs &= ~(1 << level);
471 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700472 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800473
474 mutex_unlock(&chip->irq_lock);
475}
476
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800477static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800478{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200479 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100480 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100481 int bank_nb = d->hwirq / BANK_SZ;
482 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800483
484 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
485 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800486 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800487 return -EINVAL;
488 }
489
490 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100491 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800492 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100493 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800494
495 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100496 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800497 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100498 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800499
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700500 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800501}
502
503static struct irq_chip pca953x_irq_chip = {
504 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800505 .irq_mask = pca953x_irq_mask,
506 .irq_unmask = pca953x_irq_unmask,
507 .irq_bus_lock = pca953x_irq_bus_lock,
508 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
509 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800510};
511
Joshua Scottb6ac1282015-05-22 12:35:12 +1200512static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800513{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100514 u8 cur_stat[MAX_BANK];
515 u8 old_stat[MAX_BANK];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200516 bool pending_seen = false;
517 bool trigger_seen = false;
518 u8 trigger[MAX_BANK];
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200519 int ret, i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800520
Yong Li44896be2016-04-07 12:56:32 +0800521 if (chip->driver_data & PCA_PCAL) {
522 /* Read the current interrupt status from the device */
523 ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
524 if (ret)
525 return false;
526
527 /* Check latched inputs and clear interrupt status */
528 ret = pca953x_read_regs(chip, PCA953X_INPUT, cur_stat);
529 if (ret)
530 return false;
531
532 for (i = 0; i < NBANK(chip); i++) {
533 /* Apply filter for rising/falling edge selection */
534 pending[i] = (~cur_stat[i] & chip->irq_trig_fall[i]) |
535 (cur_stat[i] & chip->irq_trig_raise[i]);
536 pending[i] &= trigger[i];
537 if (pending[i])
538 pending_seen = true;
539 }
540
541 return pending_seen;
542 }
543
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200544 ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800545 if (ret)
Joshua Scottb6ac1282015-05-22 12:35:12 +1200546 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800547
548 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100549 for (i = 0; i < NBANK(chip); i++)
550 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800551
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100552 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800553
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100554 for (i = 0; i < NBANK(chip); i++) {
555 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200556 if (trigger[i])
557 trigger_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100558 }
559
Joshua Scottb6ac1282015-05-22 12:35:12 +1200560 if (!trigger_seen)
561 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800562
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100563 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800564
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100565 for (i = 0; i < NBANK(chip); i++) {
566 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
567 (cur_stat[i] & chip->irq_trig_raise[i]);
568 pending[i] &= trigger[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200569 if (pending[i])
570 pending_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100571 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800572
Joshua Scottb6ac1282015-05-22 12:35:12 +1200573 return pending_seen;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800574}
575
576static irqreturn_t pca953x_irq_handler(int irq, void *devid)
577{
578 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100579 u8 pending[MAX_BANK];
580 u8 level;
Toby Smith3275d072014-04-30 18:01:40 +1000581 unsigned nhandled = 0;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100582 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800583
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100584 if (!pca953x_irq_pending(chip, pending))
Toby Smith3275d072014-04-30 18:01:40 +1000585 return IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800586
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100587 for (i = 0; i < NBANK(chip); i++) {
588 while (pending[i]) {
589 level = __ffs(pending[i]);
Linus Walleij7bcbce52014-05-09 13:27:57 +0200590 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100591 level + (BANK_SZ * i)));
592 pending[i] &= ~(1 << level);
Toby Smith3275d072014-04-30 18:01:40 +1000593 nhandled++;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100594 }
595 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800596
Toby Smith3275d072014-04-30 18:01:40 +1000597 return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800598}
599
600static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200601 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800602{
603 struct i2c_client *client = chip->client;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200604 int ret, i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800605
Markus Pargmann4bb93342014-07-29 09:24:43 +0200606 if (client->irq && irq_base != -1
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300607 && (chip->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800608
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200609 ret = pca953x_read_regs(chip,
610 chip->regs->input, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800611 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100612 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800613
614 /*
615 * There is no way to know which GPIO line generated the
616 * interrupt. We have to rely on the previous read for
617 * this purpose.
618 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100619 for (i = 0; i < NBANK(chip); i++)
620 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800621 mutex_init(&chip->irq_lock);
622
Linus Walleijb42748c2013-01-25 17:59:28 +0100623 ret = devm_request_threaded_irq(&client->dev,
624 client->irq,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800625 NULL,
626 pca953x_irq_handler,
Toby Smith91329132014-04-30 18:01:41 +1000627 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
628 IRQF_SHARED,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800629 dev_name(&client->dev), chip);
630 if (ret) {
631 dev_err(&client->dev, "failed to request irq %d\n",
632 client->irq);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100633 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800634 }
635
Linus Walleij7bcbce52014-05-09 13:27:57 +0200636 ret = gpiochip_irqchip_add(&chip->gpio_chip,
637 &pca953x_irq_chip,
638 irq_base,
639 handle_simple_irq,
640 IRQ_TYPE_NONE);
641 if (ret) {
642 dev_err(&client->dev,
643 "could not connect irqchip to gpiochip\n");
644 return ret;
645 }
Grygorii Strashkofdd50402015-07-07 17:34:49 +0300646
647 gpiochip_set_chained_irqchip(&chip->gpio_chip,
648 &pca953x_irq_chip,
649 client->irq, NULL);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800650 }
651
652 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800653}
654
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800655#else /* CONFIG_GPIO_PCA953X_IRQ */
656static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200657 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800658{
659 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800660
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300661 if (irq_base != -1 && (chip->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800662 dev_warn(&client->dev, "interrupt support not compiled in\n");
663
664 return 0;
665}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800666#endif
667
Bill Pemberton38363092012-11-19 13:22:34 -0500668static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800669{
670 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100671 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800672
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200673 chip->regs = &pca953x_regs;
674
675 ret = pca953x_read_regs(chip, chip->regs->output, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800676 if (ret)
677 goto out;
678
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200679 ret = pca953x_read_regs(chip, chip->regs->direction,
680 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800681 if (ret)
682 goto out;
683
684 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100685 if (invert)
686 memset(val, 0xFF, NBANK(chip));
687 else
688 memset(val, 0, NBANK(chip));
689
690 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800691out:
692 return ret;
693}
694
Bill Pemberton38363092012-11-19 13:22:34 -0500695static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800696{
697 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100698 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800699
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200700 chip->regs = &pca957x_regs;
701
702 ret = pca953x_read_regs(chip, chip->regs->output, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800703 if (ret)
704 goto out;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200705 ret = pca953x_read_regs(chip, chip->regs->direction,
706 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800707 if (ret)
708 goto out;
709
710 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100711 if (invert)
712 memset(val, 0xFF, NBANK(chip));
713 else
714 memset(val, 0, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400715 ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
716 if (ret)
717 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800718
Colin Cronin20a8a962015-05-18 11:41:43 -0700719 /* To enable register 6, 7 to control pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100720 memset(val, 0x02, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400721 ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
722 if (ret)
723 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800724
725 return 0;
726out:
727 return ret;
728}
729
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000730static const struct of_device_id pca953x_dt_ids[];
731
Bill Pemberton38363092012-11-19 13:22:34 -0500732static int pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200733 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800734{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800735 struct pca953x_platform_data *pdata;
736 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530737 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200738 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530739 u32 invert = 0;
Phil Reide23efa32016-07-29 11:39:55 +0800740 struct regulator *reg;
eric miao9e60fdc2008-02-04 22:28:26 -0800741
Linus Walleijb42748c2013-01-25 17:59:28 +0100742 chip = devm_kzalloc(&client->dev,
743 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800744 if (chip == NULL)
745 return -ENOMEM;
746
Jingoo Hane56aee12013-07-30 17:08:05 +0900747 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200748 if (pdata) {
749 irq_base = pdata->irq_base;
750 chip->gpio_start = pdata->gpio_base;
751 invert = pdata->invert;
752 chip->names = pdata->names;
753 } else {
Markus Pargmann4bb93342014-07-29 09:24:43 +0200754 chip->gpio_start = -1;
755 irq_base = 0;
Nate Case1965d302009-06-17 16:26:17 -0700756 }
757
eric miao9e60fdc2008-02-04 22:28:26 -0800758 chip->client = client;
759
Phil Reide23efa32016-07-29 11:39:55 +0800760 reg = devm_regulator_get(&client->dev, "vcc");
761 if (IS_ERR(reg)) {
762 ret = PTR_ERR(reg);
763 if (ret != -EPROBE_DEFER)
764 dev_err(&client->dev, "reg get err: %d\n", ret);
765 return ret;
766 }
767 ret = regulator_enable(reg);
768 if (ret) {
769 dev_err(&client->dev, "reg en err: %d\n", ret);
770 return ret;
771 }
772 chip->regulator = reg;
773
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300774 if (id) {
775 chip->driver_data = id->driver_data;
776 } else {
777 const struct acpi_device_id *id;
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000778 const struct of_device_id *match;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300779
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000780 match = of_match_device(pca953x_dt_ids, &client->dev);
781 if (match) {
782 chip->driver_data = (int)(uintptr_t)match->data;
783 } else {
784 id = acpi_match_device(pca953x_acpi_ids, &client->dev);
Phil Reide23efa32016-07-29 11:39:55 +0800785 if (!id) {
786 ret = -ENODEV;
787 goto err_exit;
788 }
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300789
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000790 chip->driver_data = id->driver_data;
791 }
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300792 }
793
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300794 chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700795
Roland Stigge6e20fb12011-02-10 15:01:23 -0800796 mutex_init(&chip->i2c_lock);
797
eric miao9e60fdc2008-02-04 22:28:26 -0800798 /* initialize cached registers from their original values.
799 * we can't share this chip with another i2c master.
800 */
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300801 pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800802
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200803 if (chip->gpio_chip.ngpio <= 8) {
804 chip->write_regs = pca953x_write_regs_8;
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200805 chip->read_regs = pca953x_read_regs_8;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200806 } else if (chip->gpio_chip.ngpio >= 24) {
807 chip->write_regs = pca953x_write_regs_24;
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200808 chip->read_regs = pca953x_read_regs_24;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200809 } else {
810 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
811 chip->write_regs = pca953x_write_regs_16;
812 else
813 chip->write_regs = pca957x_write_regs_16;
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200814 chip->read_regs = pca953x_read_regs_16;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200815 }
816
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800817 if (chip->chip_type == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200818 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800819 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200820 ret = device_pca957x_init(chip, invert);
821 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +0800822 goto err_exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800823
Laxman Dewangan0ece84f2016-02-22 17:43:28 +0530824 ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800825 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +0800826 goto err_exit;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800827
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300828 ret = pca953x_irq_setup(chip, irq_base);
eric miao9e60fdc2008-02-04 22:28:26 -0800829 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +0800830 goto err_exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800831
David Janderc6dcf592011-06-14 11:00:55 +0200832 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800833 ret = pdata->setup(client, chip->gpio_chip.base,
834 chip->gpio_chip.ngpio, pdata->context);
835 if (ret < 0)
836 dev_warn(&client->dev, "setup failed, %d\n", ret);
837 }
838
839 i2c_set_clientdata(client, chip);
840 return 0;
Phil Reide23efa32016-07-29 11:39:55 +0800841
842err_exit:
843 regulator_disable(chip->regulator);
844 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800845}
846
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800847static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800848{
Jingoo Hane56aee12013-07-30 17:08:05 +0900849 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800850 struct pca953x_chip *chip = i2c_get_clientdata(client);
Andy Shevchenko8c7a92d2016-05-31 17:05:57 +0300851 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800852
David Janderc6dcf592011-06-14 11:00:55 +0200853 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800854 ret = pdata->teardown(client, chip->gpio_chip.base,
855 chip->gpio_chip.ngpio, pdata->context);
Phil Reide23efa32016-07-29 11:39:55 +0800856 if (ret < 0)
eric miao9e60fdc2008-02-04 22:28:26 -0800857 dev_err(&client->dev, "%s failed, %d\n",
858 "teardown", ret);
Arnd Bergmannbf62efe2016-08-26 17:25:42 +0200859 } else {
860 ret = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800861 }
862
Phil Reide23efa32016-07-29 11:39:55 +0800863 regulator_disable(chip->regulator);
864
865 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800866}
867
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000868/* convenience to stop overlong match-table lines */
869#define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
870#define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
871
Maxime Riparded326202012-11-08 18:01:52 +0100872static const struct of_device_id pca953x_dt_ids[] = {
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000873 { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
874 { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
875 { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
876 { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
877 { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
878 { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
879 { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
880 { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
881 { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
882 { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
883 { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
884 { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
885 { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
886 { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
Maxime Riparded326202012-11-08 18:01:52 +0100887
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000888 { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
889 { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
890 { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
891 { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
Maxime Riparded326202012-11-08 18:01:52 +0100892
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000893 { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
Vignesh R353661d2016-05-19 12:17:29 +0530894 { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000895 { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
896 { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
897 { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600898
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000899 { .compatible = "onsemi,pca9654", .data = OF_953X( 8, PCA_INT), },
Ben Dooks9f49f6d2015-12-08 10:19:25 +0000900
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000901 { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
Maxime Riparded326202012-11-08 18:01:52 +0100902 { }
903};
904
905MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
906
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800907static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800908 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800909 .name = "pca953x",
Maxime Riparded326202012-11-08 18:01:52 +0100910 .of_match_table = pca953x_dt_ids,
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300911 .acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
eric miao9e60fdc2008-02-04 22:28:26 -0800912 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800913 .probe = pca953x_probe,
914 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200915 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800916};
917
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800918static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800919{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800920 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800921}
David Brownell2f8d1192008-10-15 22:03:13 -0700922/* register after i2c postcore initcall and before
923 * subsys initcalls that may rely on these GPIOs
924 */
925subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800926
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800927static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800928{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800929 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800930}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800931module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800932
933MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800934MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800935MODULE_LICENSE("GPL");