blob: 4e9c79ca69c55a15d8f33bb98f2f96e3a784a754 [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
Andy Shevchenkob413d7a2017-03-22 16:11:12 +020014#include <linux/acpi.h>
Linus Walleij644f3da2018-05-24 14:26:20 +020015#include <linux/gpio/driver.h>
Steve Longerbeam054ccde2017-01-10 11:29:51 -080016#include <linux/gpio/consumer.h>
eric miao9e60fdc2008-02-04 22:28:26 -080017#include <linux/i2c.h>
Andy Shevchenkob413d7a2017-03-22 16:11:12 +020018#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
Nate Case1965d302009-06-17 16:26:17 -070021#include <linux/of_platform.h>
Andy Shevchenkob413d7a2017-03-22 16:11:12 +020022#include <linux/platform_data/pca953x.h>
Phil Reide23efa32016-07-29 11:39:55 +080023#include <linux/regulator/consumer.h>
Andy Shevchenkob413d7a2017-03-22 16:11:12 +020024#include <linux/slab.h>
25
26#include <asm/unaligned.h>
eric miao9e60fdc2008-02-04 22:28:26 -080027
H. Nikolaus Schaller0950c192018-04-28 18:31:31 +020028#define PCA953X_INPUT 0x00
29#define PCA953X_OUTPUT 0x01
30#define PCA953X_INVERT 0x02
31#define PCA953X_DIRECTION 0x03
eric miao9e60fdc2008-02-04 22:28:26 -080032
Andreas Schallenbergae79c192012-05-09 09:46:17 +020033#define REG_ADDR_AI 0x80
34
H. Nikolaus Schaller0950c192018-04-28 18:31:31 +020035#define PCA957X_IN 0x00
36#define PCA957X_INVRT 0x01
37#define PCA957X_BKEN 0x02
38#define PCA957X_PUPD 0x03
39#define PCA957X_CFG 0x04
40#define PCA957X_OUT 0x05
41#define PCA957X_MSK 0x06
42#define PCA957X_INTS 0x07
Haojian Zhuang33226ff2011-04-18 22:12:46 +080043
H. Nikolaus Schaller6315d232018-04-28 18:31:32 +020044#define PCAL953X_OUT_STRENGTH 0x20
H. Nikolaus Schaller0950c192018-04-28 18:31:31 +020045#define PCAL953X_IN_LATCH 0x22
H. Nikolaus Schaller6315d232018-04-28 18:31:32 +020046#define PCAL953X_PULL_EN 0x23
47#define PCAL953X_PULL_SEL 0x24
H. Nikolaus Schaller0950c192018-04-28 18:31:31 +020048#define PCAL953X_INT_MASK 0x25
49#define PCAL953X_INT_STAT 0x26
H. Nikolaus Schaller6315d232018-04-28 18:31:32 +020050#define PCAL953X_OUT_CONF 0x27
Yong Li44896be2016-04-07 12:56:32 +080051
H. Nikolaus Schallera0ecbcc2018-04-28 18:31:33 +020052#define PCAL6524_INT_EDGE 0x28
53#define PCAL6524_INT_CLR 0x2a
54#define PCAL6524_IN_STATUS 0x2b
55#define PCAL6524_OUT_INDCONF 0x2c
56#define PCAL6524_DEBOUNCE 0x2d
57
Haojian Zhuang33226ff2011-04-18 22:12:46 +080058#define PCA_GPIO_MASK 0x00FF
H. Nikolaus Schaller394aeef2018-05-17 06:59:48 +020059
60#define PCAL_GPIO_MASK 0x1f
Marek Vasut92f45eb2018-12-12 02:39:50 +010061#define PCAL_PINCTRL_MASK 0x60
H. Nikolaus Schaller394aeef2018-05-17 06:59:48 +020062
Haojian Zhuang33226ff2011-04-18 22:12:46 +080063#define PCA_INT 0x0100
Andy Shevchenko8c7a92d2016-05-31 17:05:57 +030064#define PCA_PCAL 0x0200
H. Nikolaus Schaller0cdf21b2018-05-17 06:59:47 +020065#define PCA_LATCH_INT (PCA_PCAL | PCA_INT)
Haojian Zhuang33226ff2011-04-18 22:12:46 +080066#define PCA953X_TYPE 0x1000
67#define PCA957X_TYPE 0x2000
Andy Shevchenkoc6664142015-10-01 14:20:27 +030068#define PCA_TYPE_MASK 0xF000
69
70#define PCA_CHIP_TYPE(x) ((x) & PCA_TYPE_MASK)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080071
Jean Delvare3760f732008-04-29 23:11:40 +020072static const struct i2c_device_id pca953x_id[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +010073 { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080074 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
75 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
76 { "pca9536", 4 | PCA953X_TYPE, },
77 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
78 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
79 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
80 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
81 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
82 { "pca9556", 8 | PCA953X_TYPE, },
83 { "pca9557", 8 | PCA953X_TYPE, },
84 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
85 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
Aaron Sierraeb32b5a2014-02-13 13:59:23 +010086 { "pca9698", 40 | PCA953X_TYPE, },
David Brownellab5dc372009-01-06 14:42:27 -080087
H. Nikolaus Schaller3a711e02018-03-10 12:00:01 +010088 { "pcal6524", 24 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },
Andy Shevchenko747e42a1c2016-06-15 01:57:56 +030089 { "pcal9555a", 16 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },
90
Haojian Zhuang33226ff2011-04-18 22:12:46 +080091 { "max7310", 8 | PCA953X_TYPE, },
92 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
93 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
94 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
Marek Vasut1208c932016-10-17 18:36:49 +020095 { "max7318", 16 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080096 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
97 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
98 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
Andreas Schallenbergae79c192012-05-09 09:46:17 +020099 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
Thierry Reding2db8aba2015-09-29 12:55:44 +0200100 { "tca9539", 16 | PCA953X_TYPE | PCA_INT, },
Anders Darander1b9a0c22017-04-21 14:46:30 +0200101 { "tca9554", 8 | PCA953X_TYPE | PCA_INT, },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600102 { "xra1202", 8 | PCA953X_TYPE },
Jean Delvare3760f732008-04-29 23:11:40 +0200103 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800104};
Jean Delvare3760f732008-04-29 23:11:40 +0200105MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -0800106
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300107static const struct acpi_device_id pca953x_acpi_ids[] = {
Yong Li44896be2016-04-07 12:56:32 +0800108 { "INT3491", 16 | PCA953X_TYPE | PCA_INT | PCA_PCAL, },
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300109 { }
110};
111MODULE_DEVICE_TABLE(acpi, pca953x_acpi_ids);
112
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100113#define MAX_BANK 5
114#define BANK_SZ 8
115
Vignesh Ra246b812016-06-09 11:02:04 +0530116#define NBANK(chip) DIV_ROUND_UP(chip->gpio_chip.ngpio, BANK_SZ)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100117
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200118struct pca953x_reg_config {
119 int direction;
120 int output;
121 int input;
122};
123
124static const struct pca953x_reg_config pca953x_regs = {
125 .direction = PCA953X_DIRECTION,
126 .output = PCA953X_OUTPUT,
127 .input = PCA953X_INPUT,
128};
129
130static const struct pca953x_reg_config pca957x_regs = {
131 .direction = PCA957X_CFG,
132 .output = PCA957X_OUT,
133 .input = PCA957X_IN,
134};
135
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800136struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -0800137 unsigned gpio_start;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100138 u8 reg_output[MAX_BANK];
139 u8 reg_direction[MAX_BANK];
Roland Stigge6e20fb12011-02-10 15:01:23 -0800140 struct mutex i2c_lock;
eric miao9e60fdc2008-02-04 22:28:26 -0800141
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800142#ifdef CONFIG_GPIO_PCA953X_IRQ
143 struct mutex irq_lock;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100144 u8 irq_mask[MAX_BANK];
145 u8 irq_stat[MAX_BANK];
146 u8 irq_trig_raise[MAX_BANK];
147 u8 irq_trig_fall[MAX_BANK];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800148#endif
149
eric miao9e60fdc2008-02-04 22:28:26 -0800150 struct i2c_client *client;
151 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700152 const char *const *names;
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300153 unsigned long driver_data;
Phil Reide23efa32016-07-29 11:39:55 +0800154 struct regulator *regulator;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200155
156 const struct pca953x_reg_config *regs;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200157
158 int (*write_regs)(struct pca953x_chip *, int, u8 *);
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200159 int (*read_regs)(struct pca953x_chip *, int, u8 *);
eric miao9e60fdc2008-02-04 22:28:26 -0800160};
161
Marek Vasut873d1e82018-12-12 02:39:49 +0100162static int pca953x_bank_shift(struct pca953x_chip *chip)
163{
164 return fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
165}
166
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100167static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
168 int off)
169{
170 int ret;
Marek Vasut873d1e82018-12-12 02:39:49 +0100171 int bank_shift = pca953x_bank_shift(chip);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100172 int offset = off / BANK_SZ;
173
174 ret = i2c_smbus_read_byte_data(chip->client,
175 (reg << bank_shift) + offset);
176 *val = ret;
177
178 if (ret < 0) {
179 dev_err(&chip->client->dev, "failed reading register\n");
180 return ret;
181 }
182
183 return 0;
184}
185
186static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
187 int off)
188{
Andy Shevchenko8c7a92d2016-05-31 17:05:57 +0300189 int ret;
Marek Vasut873d1e82018-12-12 02:39:49 +0100190 int bank_shift = pca953x_bank_shift(chip);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100191 int offset = off / BANK_SZ;
192
193 ret = i2c_smbus_write_byte_data(chip->client,
194 (reg << bank_shift) + offset, val);
195
196 if (ret < 0) {
197 dev_err(&chip->client->dev, "failed writing register\n");
198 return ret;
199 }
200
201 return 0;
202}
203
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200204static int pca953x_write_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
205{
206 return i2c_smbus_write_byte_data(chip->client, reg, *val);
207}
208
209static int pca953x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
210{
Andy Shevchenkob2dc4112017-07-03 20:45:30 +0300211 u16 word = get_unaligned((u16 *)val);
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200212
Andy Shevchenkob2dc4112017-07-03 20:45:30 +0300213 return i2c_smbus_write_word_data(chip->client, reg << 1, word);
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200214}
215
216static int pca957x_write_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
217{
218 int ret;
219
220 ret = i2c_smbus_write_byte_data(chip->client, reg << 1, val[0]);
221 if (ret < 0)
222 return ret;
223
224 return i2c_smbus_write_byte_data(chip->client, (reg << 1) + 1, val[1]);
225}
226
227static int pca953x_write_regs_24(struct pca953x_chip *chip, int reg, u8 *val)
228{
Marek Vasut873d1e82018-12-12 02:39:49 +0100229 int bank_shift = pca953x_bank_shift(chip);
H. Nikolaus Schallerd5dbf9c2018-05-17 06:59:49 +0200230 int addr = (reg & PCAL_GPIO_MASK) << bank_shift;
231 int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200232
233 return i2c_smbus_write_i2c_block_data(chip->client,
H. Nikolaus Schallerd5dbf9c2018-05-17 06:59:49 +0200234 pinctrl | addr | REG_ADDR_AI,
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200235 NBANK(chip), val);
236}
237
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100238static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800239{
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800240 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800241
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200242 ret = chip->write_regs(chip, reg, val);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800243 if (ret < 0) {
244 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800245 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800246 }
247
248 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800249}
250
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200251static int pca953x_read_regs_8(struct pca953x_chip *chip, int reg, u8 *val)
252{
253 int ret;
254
255 ret = i2c_smbus_read_byte_data(chip->client, reg);
256 *val = ret;
257
258 return ret;
259}
260
261static int pca953x_read_regs_16(struct pca953x_chip *chip, int reg, u8 *val)
262{
263 int ret;
264
265 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
Andy Shevchenkob2dc4112017-07-03 20:45:30 +0300266 put_unaligned(ret, (u16 *)val);
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200267
268 return ret;
269}
270
271static int pca953x_read_regs_24(struct pca953x_chip *chip, int reg, u8 *val)
272{
Marek Vasut873d1e82018-12-12 02:39:49 +0100273 int bank_shift = pca953x_bank_shift(chip);
H. Nikolaus Schallerd5dbf9c2018-05-17 06:59:49 +0200274 int addr = (reg & PCAL_GPIO_MASK) << bank_shift;
275 int pinctrl = (reg & PCAL_PINCTRL_MASK) << 1;
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200276
277 return i2c_smbus_read_i2c_block_data(chip->client,
H. Nikolaus Schallerd5dbf9c2018-05-17 06:59:49 +0200278 pinctrl | addr | REG_ADDR_AI,
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200279 NBANK(chip), val);
280}
281
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100282static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800283{
284 int ret;
285
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200286 ret = chip->read_regs(chip, reg, val);
eric miao9e60fdc2008-02-04 22:28:26 -0800287 if (ret < 0) {
288 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800289 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800290 }
291
eric miao9e60fdc2008-02-04 22:28:26 -0800292 return 0;
293}
294
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800295static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800296{
Linus Walleij468e67f2015-12-07 11:20:54 +0100297 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100298 u8 reg_val;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200299 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800300
Roland Stigge6e20fb12011-02-10 15:01:23 -0800301 mutex_lock(&chip->i2c_lock);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100302 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800303
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200304 ret = pca953x_write_single(chip, chip->regs->direction, 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_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800309exit:
310 mutex_unlock(&chip->i2c_lock);
311 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800312}
313
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800314static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800315 unsigned off, int val)
316{
Linus Walleij468e67f2015-12-07 11:20:54 +0100317 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100318 u8 reg_val;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200319 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800320
Roland Stigge6e20fb12011-02-10 15:01:23 -0800321 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800322 /* set output level */
323 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100324 reg_val = chip->reg_output[off / BANK_SZ]
325 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800326 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100327 reg_val = chip->reg_output[off / BANK_SZ]
328 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800329
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200330 ret = pca953x_write_single(chip, chip->regs->output, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800331 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800332 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800333
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100334 chip->reg_output[off / BANK_SZ] = reg_val;
eric miao9e60fdc2008-02-04 22:28:26 -0800335
336 /* then direction */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100337 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200338 ret = pca953x_write_single(chip, chip->regs->direction, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800339 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800340 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800341
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100342 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800343exit:
344 mutex_unlock(&chip->i2c_lock);
345 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800346}
347
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800348static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800349{
Linus Walleij468e67f2015-12-07 11:20:54 +0100350 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200351 u32 reg_val;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200352 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800353
Roland Stigge6e20fb12011-02-10 15:01:23 -0800354 mutex_lock(&chip->i2c_lock);
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200355 ret = pca953x_read_single(chip, chip->regs->input, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800356 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800357 if (ret < 0) {
358 /* NOTE: diagnostic already emitted; that's all we should
359 * do unless gpio_*_value_cansleep() calls become different
360 * from their nonsleeping siblings (and report faults).
361 */
362 return 0;
363 }
364
Andrew Ruder40a625d2013-08-07 16:52:40 -0500365 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800366}
367
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800368static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800369{
Linus Walleij468e67f2015-12-07 11:20:54 +0100370 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100371 u8 reg_val;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200372 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800373
Roland Stigge6e20fb12011-02-10 15:01:23 -0800374 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800375 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100376 reg_val = chip->reg_output[off / BANK_SZ]
377 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800378 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100379 reg_val = chip->reg_output[off / BANK_SZ]
380 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800381
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200382 ret = pca953x_write_single(chip, chip->regs->output, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800383 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800384 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800385
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100386 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800387exit:
388 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800389}
390
Andy Shevchenko66e57192017-03-22 16:11:11 +0200391static int pca953x_gpio_get_direction(struct gpio_chip *gc, unsigned off)
392{
393 struct pca953x_chip *chip = gpiochip_get_data(gc);
394 u32 reg_val;
395 int ret;
396
397 mutex_lock(&chip->i2c_lock);
398 ret = pca953x_read_single(chip, chip->regs->direction, &reg_val, off);
399 mutex_unlock(&chip->i2c_lock);
400 if (ret < 0)
401 return ret;
402
403 return !!(reg_val & (1u << (off % BANK_SZ)));
404}
405
Phil Reidb4818af2015-12-04 15:52:30 +0800406static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200407 unsigned long *mask, unsigned long *bits)
Phil Reidb4818af2015-12-04 15:52:30 +0800408{
Linus Walleij468e67f2015-12-07 11:20:54 +0100409 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marek Vasut873d1e82018-12-12 02:39:49 +0100410 int bank_shift = pca953x_bank_shift(chip);
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200411 unsigned int bank_mask, bank_val;
Marek Vasut873d1e82018-12-12 02:39:49 +0100412 int bank;
Phil Reidb4818af2015-12-04 15:52:30 +0800413 u8 reg_val[MAX_BANK];
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200414 int ret;
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200415
Phil Reidb4818af2015-12-04 15:52:30 +0800416 mutex_lock(&chip->i2c_lock);
Phil Reid386377b2016-11-08 13:18:11 +0800417 memcpy(reg_val, chip->reg_output, NBANK(chip));
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200418 for (bank = 0; bank < NBANK(chip); bank++) {
419 bank_mask = mask[bank / sizeof(*mask)] >>
420 ((bank % sizeof(*mask)) * 8);
421 if (bank_mask) {
422 bank_val = bits[bank / sizeof(*bits)] >>
423 ((bank % sizeof(*bits)) * 8);
Phil Reid53f8d322016-11-08 14:00:45 +0800424 bank_val &= bank_mask;
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200425 reg_val[bank] = (reg_val[bank] & ~bank_mask) | bank_val;
Phil Reidb4818af2015-12-04 15:52:30 +0800426 }
427 }
Bartosz Golaszewskiea3d5792016-09-09 11:17:38 +0200428
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200429 ret = i2c_smbus_write_i2c_block_data(chip->client,
430 chip->regs->output << bank_shift,
431 NBANK(chip), reg_val);
Phil Reidb4818af2015-12-04 15:52:30 +0800432 if (ret)
433 goto exit;
434
435 memcpy(chip->reg_output, reg_val, NBANK(chip));
436exit:
437 mutex_unlock(&chip->i2c_lock);
438}
439
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800440static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800441{
442 struct gpio_chip *gc;
443
444 gc = &chip->gpio_chip;
445
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800446 gc->direction_input = pca953x_gpio_direction_input;
447 gc->direction_output = pca953x_gpio_direction_output;
448 gc->get = pca953x_gpio_get_value;
449 gc->set = pca953x_gpio_set_value;
Andy Shevchenko66e57192017-03-22 16:11:11 +0200450 gc->get_direction = pca953x_gpio_get_direction;
Phil Reidb4818af2015-12-04 15:52:30 +0800451 gc->set_multiple = pca953x_gpio_set_multiple;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100452 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800453
454 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800455 gc->ngpio = gpios;
456 gc->label = chip->client->name;
Linus Walleij58383c782015-11-04 09:56:26 +0100457 gc->parent = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700458 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700459 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800460}
461
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800462#ifdef CONFIG_GPIO_PCA953X_IRQ
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800463static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800464{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200465 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100466 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800467
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100468 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800469}
470
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800471static void pca953x_irq_unmask(struct irq_data *d)
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);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800475
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100476 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800477}
478
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800479static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800480{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200481 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100482 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800483
484 mutex_lock(&chip->irq_lock);
485}
486
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800487static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800488{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200489 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100490 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100491 u8 new_irqs;
492 int level, i;
Yong Li44896be2016-04-07 12:56:32 +0800493 u8 invert_irq_mask[MAX_BANK];
494
495 if (chip->driver_data & PCA_PCAL) {
496 /* Enable latch on interrupt-enabled inputs */
497 pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
498
499 for (i = 0; i < NBANK(chip); i++)
500 invert_irq_mask[i] = ~chip->irq_mask[i];
501
502 /* Unmask enabled interrupts */
503 pca953x_write_regs(chip, PCAL953X_INT_MASK, invert_irq_mask);
504 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700505
506 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100507 for (i = 0; i < NBANK(chip); i++) {
508 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
509 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700510
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100511 while (new_irqs) {
512 level = __ffs(new_irqs);
513 pca953x_gpio_direction_input(&chip->gpio_chip,
514 level + (BANK_SZ * i));
515 new_irqs &= ~(1 << level);
516 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700517 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800518
519 mutex_unlock(&chip->irq_lock);
520}
521
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800522static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800523{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200524 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100525 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100526 int bank_nb = d->hwirq / BANK_SZ;
527 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800528
529 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
530 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800531 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800532 return -EINVAL;
533 }
534
535 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100536 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800537 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100538 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800539
540 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100541 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800542 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100543 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800544
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700545 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800546}
547
Grigoryev Denis0a70fe02018-05-04 16:53:18 +0000548static void pca953x_irq_shutdown(struct irq_data *d)
549{
550 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
551 u8 mask = 1 << (d->hwirq % BANK_SZ);
552
553 chip->irq_trig_raise[d->hwirq / BANK_SZ] &= ~mask;
554 chip->irq_trig_fall[d->hwirq / BANK_SZ] &= ~mask;
555}
556
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800557static struct irq_chip pca953x_irq_chip = {
558 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800559 .irq_mask = pca953x_irq_mask,
560 .irq_unmask = pca953x_irq_unmask,
561 .irq_bus_lock = pca953x_irq_bus_lock,
562 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
563 .irq_set_type = pca953x_irq_set_type,
Grigoryev Denis0a70fe02018-05-04 16:53:18 +0000564 .irq_shutdown = pca953x_irq_shutdown,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800565};
566
Joshua Scottb6ac1282015-05-22 12:35:12 +1200567static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800568{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100569 u8 cur_stat[MAX_BANK];
570 u8 old_stat[MAX_BANK];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200571 bool pending_seen = false;
572 bool trigger_seen = false;
573 u8 trigger[MAX_BANK];
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200574 int ret, i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800575
Yong Li44896be2016-04-07 12:56:32 +0800576 if (chip->driver_data & PCA_PCAL) {
577 /* Read the current interrupt status from the device */
578 ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
579 if (ret)
580 return false;
581
582 /* Check latched inputs and clear interrupt status */
583 ret = pca953x_read_regs(chip, PCA953X_INPUT, cur_stat);
584 if (ret)
585 return false;
586
587 for (i = 0; i < NBANK(chip); i++) {
588 /* Apply filter for rising/falling edge selection */
589 pending[i] = (~cur_stat[i] & chip->irq_trig_fall[i]) |
590 (cur_stat[i] & chip->irq_trig_raise[i]);
591 pending[i] &= trigger[i];
592 if (pending[i])
593 pending_seen = true;
594 }
595
596 return pending_seen;
597 }
598
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200599 ret = pca953x_read_regs(chip, chip->regs->input, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800600 if (ret)
Joshua Scottb6ac1282015-05-22 12:35:12 +1200601 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800602
603 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100604 for (i = 0; i < NBANK(chip); i++)
605 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800606
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100607 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800608
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100609 for (i = 0; i < NBANK(chip); i++) {
610 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200611 if (trigger[i])
612 trigger_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100613 }
614
Joshua Scottb6ac1282015-05-22 12:35:12 +1200615 if (!trigger_seen)
616 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800617
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100618 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800619
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100620 for (i = 0; i < NBANK(chip); i++) {
621 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
622 (cur_stat[i] & chip->irq_trig_raise[i]);
623 pending[i] &= trigger[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200624 if (pending[i])
625 pending_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100626 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800627
Joshua Scottb6ac1282015-05-22 12:35:12 +1200628 return pending_seen;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800629}
630
631static irqreturn_t pca953x_irq_handler(int irq, void *devid)
632{
633 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100634 u8 pending[MAX_BANK];
635 u8 level;
Toby Smith3275d072014-04-30 18:01:40 +1000636 unsigned nhandled = 0;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100637 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800638
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100639 if (!pca953x_irq_pending(chip, pending))
Toby Smith3275d072014-04-30 18:01:40 +1000640 return IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800641
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100642 for (i = 0; i < NBANK(chip); i++) {
643 while (pending[i]) {
644 level = __ffs(pending[i]);
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100645 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irq.domain,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100646 level + (BANK_SZ * i)));
647 pending[i] &= ~(1 << level);
Toby Smith3275d072014-04-30 18:01:40 +1000648 nhandled++;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100649 }
650 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800651
Toby Smith3275d072014-04-30 18:01:40 +1000652 return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800653}
654
655static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200656 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800657{
658 struct i2c_client *client = chip->client;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200659 int ret, i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800660
Markus Pargmann4bb93342014-07-29 09:24:43 +0200661 if (client->irq && irq_base != -1
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300662 && (chip->driver_data & PCA_INT)) {
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200663 ret = pca953x_read_regs(chip,
664 chip->regs->input, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800665 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100666 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800667
668 /*
669 * There is no way to know which GPIO line generated the
670 * interrupt. We have to rely on the previous read for
671 * this purpose.
672 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100673 for (i = 0; i < NBANK(chip); i++)
674 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800675 mutex_init(&chip->irq_lock);
676
Linus Walleijb42748c2013-01-25 17:59:28 +0100677 ret = devm_request_threaded_irq(&client->dev,
678 client->irq,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800679 NULL,
680 pca953x_irq_handler,
Toby Smith91329132014-04-30 18:01:41 +1000681 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
682 IRQF_SHARED,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800683 dev_name(&client->dev), chip);
684 if (ret) {
685 dev_err(&client->dev, "failed to request irq %d\n",
686 client->irq);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100687 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800688 }
689
Linus Walleijd245b3f2016-11-24 10:57:25 +0100690 ret = gpiochip_irqchip_add_nested(&chip->gpio_chip,
691 &pca953x_irq_chip,
692 irq_base,
693 handle_simple_irq,
694 IRQ_TYPE_NONE);
Linus Walleij7bcbce52014-05-09 13:27:57 +0200695 if (ret) {
696 dev_err(&client->dev,
697 "could not connect irqchip to gpiochip\n");
698 return ret;
699 }
Grygorii Strashkofdd50402015-07-07 17:34:49 +0300700
Linus Walleijd245b3f2016-11-24 10:57:25 +0100701 gpiochip_set_nested_irqchip(&chip->gpio_chip,
702 &pca953x_irq_chip,
703 client->irq);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800704 }
705
706 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800707}
708
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800709#else /* CONFIG_GPIO_PCA953X_IRQ */
710static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200711 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800712{
713 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800714
Baruch Siach72b38ca2018-06-21 16:38:46 +0300715 if (client->irq && irq_base != -1 && (chip->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800716 dev_warn(&client->dev, "interrupt support not compiled in\n");
717
718 return 0;
719}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800720#endif
721
Bill Pemberton38363092012-11-19 13:22:34 -0500722static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800723{
724 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100725 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800726
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200727 chip->regs = &pca953x_regs;
728
729 ret = pca953x_read_regs(chip, chip->regs->output, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800730 if (ret)
731 goto out;
732
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200733 ret = pca953x_read_regs(chip, chip->regs->direction,
734 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800735 if (ret)
736 goto out;
737
738 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100739 if (invert)
740 memset(val, 0xFF, NBANK(chip));
741 else
742 memset(val, 0, NBANK(chip));
743
744 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800745out:
746 return ret;
747}
748
Bill Pemberton38363092012-11-19 13:22:34 -0500749static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800750{
751 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100752 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800753
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200754 chip->regs = &pca957x_regs;
755
756 ret = pca953x_read_regs(chip, chip->regs->output, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800757 if (ret)
758 goto out;
Bartosz Golaszewski53661f3b2016-09-09 11:17:34 +0200759 ret = pca953x_read_regs(chip, chip->regs->direction,
760 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800761 if (ret)
762 goto out;
763
764 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100765 if (invert)
766 memset(val, 0xFF, NBANK(chip));
767 else
768 memset(val, 0, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400769 ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
770 if (ret)
771 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800772
Colin Cronin20a8a962015-05-18 11:41:43 -0700773 /* To enable register 6, 7 to control pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100774 memset(val, 0x02, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400775 ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
776 if (ret)
777 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800778
779 return 0;
780out:
781 return ret;
782}
783
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000784static const struct of_device_id pca953x_dt_ids[];
785
Bill Pemberton38363092012-11-19 13:22:34 -0500786static int pca953x_probe(struct i2c_client *client,
Wolfram Sang6212e1d2016-09-24 11:06:29 +0200787 const struct i2c_device_id *i2c_id)
eric miao9e60fdc2008-02-04 22:28:26 -0800788{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800789 struct pca953x_platform_data *pdata;
790 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530791 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200792 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530793 u32 invert = 0;
Phil Reide23efa32016-07-29 11:39:55 +0800794 struct regulator *reg;
eric miao9e60fdc2008-02-04 22:28:26 -0800795
Linus Walleijb42748c2013-01-25 17:59:28 +0100796 chip = devm_kzalloc(&client->dev,
797 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800798 if (chip == NULL)
799 return -ENOMEM;
800
Jingoo Hane56aee12013-07-30 17:08:05 +0900801 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200802 if (pdata) {
803 irq_base = pdata->irq_base;
804 chip->gpio_start = pdata->gpio_base;
805 invert = pdata->invert;
806 chip->names = pdata->names;
807 } else {
Steve Longerbeam054ccde2017-01-10 11:29:51 -0800808 struct gpio_desc *reset_gpio;
809
Markus Pargmann4bb93342014-07-29 09:24:43 +0200810 chip->gpio_start = -1;
811 irq_base = 0;
Steve Longerbeam054ccde2017-01-10 11:29:51 -0800812
Andy Shevchenko96530b32017-03-22 16:11:13 +0200813 /*
814 * See if we need to de-assert a reset pin.
815 *
816 * There is no known ACPI-enabled platforms that are
817 * using "reset" GPIO. Otherwise any of those platform
818 * must use _DSD method with corresponding property.
819 */
Steve Longerbeam054ccde2017-01-10 11:29:51 -0800820 reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
821 GPIOD_OUT_LOW);
822 if (IS_ERR(reset_gpio))
823 return PTR_ERR(reset_gpio);
Nate Case1965d302009-06-17 16:26:17 -0700824 }
825
eric miao9e60fdc2008-02-04 22:28:26 -0800826 chip->client = client;
827
Phil Reide23efa32016-07-29 11:39:55 +0800828 reg = devm_regulator_get(&client->dev, "vcc");
829 if (IS_ERR(reg)) {
830 ret = PTR_ERR(reg);
831 if (ret != -EPROBE_DEFER)
832 dev_err(&client->dev, "reg get err: %d\n", ret);
833 return ret;
834 }
835 ret = regulator_enable(reg);
836 if (ret) {
837 dev_err(&client->dev, "reg en err: %d\n", ret);
838 return ret;
839 }
840 chip->regulator = reg;
841
Wolfram Sang6212e1d2016-09-24 11:06:29 +0200842 if (i2c_id) {
843 chip->driver_data = i2c_id->driver_data;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300844 } else {
Wolfram Sang6212e1d2016-09-24 11:06:29 +0200845 const struct acpi_device_id *acpi_id;
Thierry Reding67bab932018-04-30 09:38:14 +0200846 struct device *dev = &client->dev;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300847
Thierry Reding67bab932018-04-30 09:38:14 +0200848 chip->driver_data = (uintptr_t)of_device_get_match_data(dev);
849 if (!chip->driver_data) {
850 acpi_id = acpi_match_device(pca953x_acpi_ids, dev);
Linus Torvalds87840a22016-10-07 14:12:21 -0700851 if (!acpi_id) {
Phil Reide23efa32016-07-29 11:39:55 +0800852 ret = -ENODEV;
853 goto err_exit;
854 }
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300855
Wolfram Sang6212e1d2016-09-24 11:06:29 +0200856 chip->driver_data = acpi_id->driver_data;
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000857 }
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300858 }
859
Roland Stigge6e20fb12011-02-10 15:01:23 -0800860 mutex_init(&chip->i2c_lock);
Bartosz Golaszewski74f47f02016-09-26 11:54:15 +0200861 /*
862 * In case we have an i2c-mux controlled by a GPIO provided by an
863 * expander using the same driver higher on the device tree, read the
864 * i2c adapter nesting depth and use the retrieved value as lockdep
865 * subclass for chip->i2c_lock.
866 *
867 * REVISIT: This solution is not complete. It protects us from lockdep
868 * false positives when the expander controlling the i2c-mux is on
869 * a different level on the device tree, but not when it's on the same
870 * level on a different branch (in which case the subclass number
871 * would be the same).
872 *
873 * TODO: Once a correct solution is developed, a similar fix should be
874 * applied to all other i2c-controlled GPIO expanders (and potentially
875 * regmap-i2c).
876 */
Bartosz Golaszewski559b4692016-09-16 18:02:45 +0200877 lockdep_set_subclass(&chip->i2c_lock,
878 i2c_adapter_depth(client->adapter));
Roland Stigge6e20fb12011-02-10 15:01:23 -0800879
eric miao9e60fdc2008-02-04 22:28:26 -0800880 /* initialize cached registers from their original values.
881 * we can't share this chip with another i2c master.
882 */
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300883 pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800884
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200885 if (chip->gpio_chip.ngpio <= 8) {
886 chip->write_regs = pca953x_write_regs_8;
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200887 chip->read_regs = pca953x_read_regs_8;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200888 } else if (chip->gpio_chip.ngpio >= 24) {
889 chip->write_regs = pca953x_write_regs_24;
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200890 chip->read_regs = pca953x_read_regs_24;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200891 } else {
892 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
893 chip->write_regs = pca953x_write_regs_16;
894 else
895 chip->write_regs = pca957x_write_regs_16;
Bartosz Golaszewskic6e3cf02016-09-09 11:17:36 +0200896 chip->read_regs = pca953x_read_regs_16;
Bartosz Golaszewski7acc66e2016-09-09 11:17:35 +0200897 }
898
Bartosz Golaszewski60f547b2016-09-09 11:17:37 +0200899 if (PCA_CHIP_TYPE(chip->driver_data) == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200900 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800901 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200902 ret = device_pca957x_init(chip, invert);
903 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +0800904 goto err_exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800905
Laxman Dewangan0ece84f2016-02-22 17:43:28 +0530906 ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800907 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +0800908 goto err_exit;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800909
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300910 ret = pca953x_irq_setup(chip, irq_base);
eric miao9e60fdc2008-02-04 22:28:26 -0800911 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +0800912 goto err_exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800913
David Janderc6dcf592011-06-14 11:00:55 +0200914 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800915 ret = pdata->setup(client, chip->gpio_chip.base,
916 chip->gpio_chip.ngpio, pdata->context);
917 if (ret < 0)
918 dev_warn(&client->dev, "setup failed, %d\n", ret);
919 }
920
921 i2c_set_clientdata(client, chip);
922 return 0;
Phil Reide23efa32016-07-29 11:39:55 +0800923
924err_exit:
925 regulator_disable(chip->regulator);
926 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800927}
928
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800929static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800930{
Jingoo Hane56aee12013-07-30 17:08:05 +0900931 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800932 struct pca953x_chip *chip = i2c_get_clientdata(client);
Linus Walleijd147d542016-09-13 14:43:23 +0200933 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800934
David Janderc6dcf592011-06-14 11:00:55 +0200935 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800936 ret = pdata->teardown(client, chip->gpio_chip.base,
937 chip->gpio_chip.ngpio, pdata->context);
Phil Reide23efa32016-07-29 11:39:55 +0800938 if (ret < 0)
eric miao9e60fdc2008-02-04 22:28:26 -0800939 dev_err(&client->dev, "%s failed, %d\n",
940 "teardown", ret);
Arnd Bergmannbf62efe2016-08-26 17:25:42 +0200941 } else {
942 ret = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800943 }
944
Phil Reide23efa32016-07-29 11:39:55 +0800945 regulator_disable(chip->regulator);
946
947 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800948}
949
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000950/* convenience to stop overlong match-table lines */
951#define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
952#define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
953
Maxime Riparded326202012-11-08 18:01:52 +0100954static const struct of_device_id pca953x_dt_ids[] = {
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000955 { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
956 { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
957 { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
958 { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
959 { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
960 { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
961 { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
962 { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
963 { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
964 { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
965 { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
966 { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
967 { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
968 { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
Maxime Riparded326202012-11-08 18:01:52 +0100969
H. Nikolaus Schaller0cdf21b2018-05-17 06:59:47 +0200970 { .compatible = "nxp,pcal6524", .data = OF_953X(24, PCA_LATCH_INT), },
971 { .compatible = "nxp,pcal9555a", .data = OF_953X(16, PCA_LATCH_INT), },
H. Nikolaus Schaller3a711e02018-03-10 12:00:01 +0100972
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000973 { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
974 { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
975 { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
976 { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
Marek Vasut1208c932016-10-17 18:36:49 +0200977 { .compatible = "maxim,max7318", .data = OF_953X(16, PCA_INT), },
Maxime Riparded326202012-11-08 18:01:52 +0100978
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000979 { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
Vignesh R353661d2016-05-19 12:17:29 +0530980 { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000981 { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
982 { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
983 { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600984
Sergei Shtylyov8a64e552017-11-16 23:18:32 +0300985 { .compatible = "onnn,pca9654", .data = OF_953X( 8, PCA_INT), },
Ben Dooks9f49f6d2015-12-08 10:19:25 +0000986
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000987 { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
Maxime Riparded326202012-11-08 18:01:52 +0100988 { }
989};
990
991MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
992
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800993static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800994 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800995 .name = "pca953x",
Maxime Riparded326202012-11-08 18:01:52 +0100996 .of_match_table = pca953x_dt_ids,
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300997 .acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
eric miao9e60fdc2008-02-04 22:28:26 -0800998 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800999 .probe = pca953x_probe,
1000 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +02001001 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -08001002};
1003
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001004static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -08001005{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001006 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -08001007}
David Brownell2f8d1192008-10-15 22:03:13 -07001008/* register after i2c postcore initcall and before
1009 * subsys initcalls that may rely on these GPIOs
1010 */
1011subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -08001012
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001013static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -08001014{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001015 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -08001016}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001017module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -08001018
1019MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -08001020MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -08001021MODULE_LICENSE("GPL");