blob: cbe2824461ebe8cf84eb585b6af482bccbbe0a66 [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
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080098struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080099 unsigned gpio_start;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100100 u8 reg_output[MAX_BANK];
101 u8 reg_direction[MAX_BANK];
Roland Stigge6e20fb12011-02-10 15:01:23 -0800102 struct mutex i2c_lock;
eric miao9e60fdc2008-02-04 22:28:26 -0800103
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800104#ifdef CONFIG_GPIO_PCA953X_IRQ
105 struct mutex irq_lock;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100106 u8 irq_mask[MAX_BANK];
107 u8 irq_stat[MAX_BANK];
108 u8 irq_trig_raise[MAX_BANK];
109 u8 irq_trig_fall[MAX_BANK];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800110#endif
111
eric miao9e60fdc2008-02-04 22:28:26 -0800112 struct i2c_client *client;
113 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -0700114 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800115 int chip_type;
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300116 unsigned long driver_data;
Phil Reide23efa32016-07-29 11:39:55 +0800117 struct regulator *regulator;
eric miao9e60fdc2008-02-04 22:28:26 -0800118};
119
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100120static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
121 int off)
122{
123 int ret;
124 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
125 int offset = off / BANK_SZ;
126
127 ret = i2c_smbus_read_byte_data(chip->client,
128 (reg << bank_shift) + offset);
129 *val = ret;
130
131 if (ret < 0) {
132 dev_err(&chip->client->dev, "failed reading register\n");
133 return ret;
134 }
135
136 return 0;
137}
138
139static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
140 int off)
141{
Andy Shevchenko8c7a92d2016-05-31 17:05:57 +0300142 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100143 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
144 int offset = off / BANK_SZ;
145
146 ret = i2c_smbus_write_byte_data(chip->client,
147 (reg << bank_shift) + offset, val);
148
149 if (ret < 0) {
150 dev_err(&chip->client->dev, "failed writing register\n");
151 return ret;
152 }
153
154 return 0;
155}
156
157static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800158{
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800159 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800160
161 if (chip->gpio_chip.ngpio <= 8)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100162 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
163 else if (chip->gpio_chip.ngpio >= 24) {
164 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200165 ret = i2c_smbus_write_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100166 (reg << bank_shift) | REG_ADDR_AI,
167 NBANK(chip), val);
Laurent Navet50e44432013-03-20 13:15:58 +0100168 } else {
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800169 switch (chip->chip_type) {
Andy Shevchenkoc4d1cbd2016-05-31 22:03:43 +0300170 case PCA953X_TYPE: {
171 __le16 word = cpu_to_le16(get_unaligned((u16 *)val));
172
173 ret = i2c_smbus_write_word_data(chip->client, reg << 1,
174 (__force u16)word);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800175 break;
Andy Shevchenkoc4d1cbd2016-05-31 22:03:43 +0300176 }
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800177 case PCA957X_TYPE:
178 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100179 val[0]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800180 if (ret < 0)
181 break;
182 ret = i2c_smbus_write_byte_data(chip->client,
183 (reg << 1) + 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100184 val[1]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800185 break;
186 }
187 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800188
189 if (ret < 0) {
190 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800191 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800192 }
193
194 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800195}
196
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100197static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800198{
199 int ret;
200
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200201 if (chip->gpio_chip.ngpio <= 8) {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800202 ret = i2c_smbus_read_byte_data(chip->client, reg);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200203 *val = ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100204 } else if (chip->gpio_chip.ngpio >= 24) {
205 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
206
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200207 ret = i2c_smbus_read_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100208 (reg << bank_shift) | REG_ADDR_AI,
209 NBANK(chip), val);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200210 } else {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800211 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100212 val[0] = (u16)ret & 0xFF;
213 val[1] = (u16)ret >> 8;
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200214 }
eric miao9e60fdc2008-02-04 22:28:26 -0800215 if (ret < 0) {
216 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800217 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800218 }
219
eric miao9e60fdc2008-02-04 22:28:26 -0800220 return 0;
221}
222
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800223static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800224{
Linus Walleij468e67f2015-12-07 11:20:54 +0100225 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100226 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800227 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800228
Roland Stigge6e20fb12011-02-10 15:01:23 -0800229 mutex_lock(&chip->i2c_lock);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100230 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800231
232 switch (chip->chip_type) {
233 case PCA953X_TYPE:
234 offset = PCA953X_DIRECTION;
235 break;
236 case PCA957X_TYPE:
237 offset = PCA957X_CFG;
238 break;
239 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100240 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800241 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800242 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800243
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100244 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800245exit:
246 mutex_unlock(&chip->i2c_lock);
247 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800248}
249
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800250static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800251 unsigned off, int val)
252{
Linus Walleij468e67f2015-12-07 11:20:54 +0100253 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100254 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800255 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800256
Roland Stigge6e20fb12011-02-10 15:01:23 -0800257 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800258 /* set output level */
259 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100260 reg_val = chip->reg_output[off / BANK_SZ]
261 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800262 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100263 reg_val = chip->reg_output[off / BANK_SZ]
264 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800265
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800266 switch (chip->chip_type) {
267 case PCA953X_TYPE:
268 offset = PCA953X_OUTPUT;
269 break;
270 case PCA957X_TYPE:
271 offset = PCA957X_OUT;
272 break;
273 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100274 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800275 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800276 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800277
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100278 chip->reg_output[off / BANK_SZ] = reg_val;
eric miao9e60fdc2008-02-04 22:28:26 -0800279
280 /* then direction */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100281 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800282 switch (chip->chip_type) {
283 case PCA953X_TYPE:
284 offset = PCA953X_DIRECTION;
285 break;
286 case PCA957X_TYPE:
287 offset = PCA957X_CFG;
288 break;
289 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100290 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800291 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800292 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800293
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100294 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800295exit:
296 mutex_unlock(&chip->i2c_lock);
297 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800298}
299
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800300static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800301{
Linus Walleij468e67f2015-12-07 11:20:54 +0100302 struct pca953x_chip *chip = gpiochip_get_data(gc);
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200303 u32 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800304 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800305
Roland Stigge6e20fb12011-02-10 15:01:23 -0800306 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800307 switch (chip->chip_type) {
308 case PCA953X_TYPE:
309 offset = PCA953X_INPUT;
310 break;
311 case PCA957X_TYPE:
312 offset = PCA957X_IN;
313 break;
314 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100315 ret = pca953x_read_single(chip, offset, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800316 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800317 if (ret < 0) {
318 /* NOTE: diagnostic already emitted; that's all we should
319 * do unless gpio_*_value_cansleep() calls become different
320 * from their nonsleeping siblings (and report faults).
321 */
322 return 0;
323 }
324
Andrew Ruder40a625d2013-08-07 16:52:40 -0500325 return (reg_val & (1u << (off % BANK_SZ))) ? 1 : 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800326}
327
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800328static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800329{
Linus Walleij468e67f2015-12-07 11:20:54 +0100330 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100331 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800332 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800333
Roland Stigge6e20fb12011-02-10 15:01:23 -0800334 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800335 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100336 reg_val = chip->reg_output[off / BANK_SZ]
337 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800338 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100339 reg_val = chip->reg_output[off / BANK_SZ]
340 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800341
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800342 switch (chip->chip_type) {
343 case PCA953X_TYPE:
344 offset = PCA953X_OUTPUT;
345 break;
346 case PCA957X_TYPE:
347 offset = PCA957X_OUT;
348 break;
349 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100350 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800351 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800352 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800353
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100354 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800355exit:
356 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800357}
358
Phil Reidb4818af2015-12-04 15:52:30 +0800359static void pca953x_gpio_set_multiple(struct gpio_chip *gc,
360 unsigned long *mask, unsigned long *bits)
361{
Linus Walleij468e67f2015-12-07 11:20:54 +0100362 struct pca953x_chip *chip = gpiochip_get_data(gc);
Phil Reidb4818af2015-12-04 15:52:30 +0800363 u8 reg_val[MAX_BANK];
364 int ret, offset = 0;
365 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
366 int bank;
367
368 switch (chip->chip_type) {
369 case PCA953X_TYPE:
370 offset = PCA953X_OUTPUT;
371 break;
372 case PCA957X_TYPE:
373 offset = PCA957X_OUT;
374 break;
375 }
376
377 memcpy(reg_val, chip->reg_output, NBANK(chip));
378 mutex_lock(&chip->i2c_lock);
379 for(bank=0; bank<NBANK(chip); bank++) {
Geert Uytterhoevene0a86042016-03-11 17:31:27 +0100380 unsigned bankmask = mask[bank / sizeof(*mask)] >>
381 ((bank % sizeof(*mask)) * 8);
Phil Reidb4818af2015-12-04 15:52:30 +0800382 if(bankmask) {
Geert Uytterhoevene0a86042016-03-11 17:31:27 +0100383 unsigned bankval = bits[bank / sizeof(*bits)] >>
384 ((bank % sizeof(*bits)) * 8);
Phil Reidb4818af2015-12-04 15:52:30 +0800385 reg_val[bank] = (reg_val[bank] & ~bankmask) | bankval;
386 }
387 }
388 ret = i2c_smbus_write_i2c_block_data(chip->client, offset << bank_shift, NBANK(chip), reg_val);
389 if (ret)
390 goto exit;
391
392 memcpy(chip->reg_output, reg_val, NBANK(chip));
393exit:
394 mutex_unlock(&chip->i2c_lock);
395}
396
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800397static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800398{
399 struct gpio_chip *gc;
400
401 gc = &chip->gpio_chip;
402
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800403 gc->direction_input = pca953x_gpio_direction_input;
404 gc->direction_output = pca953x_gpio_direction_output;
405 gc->get = pca953x_gpio_get_value;
406 gc->set = pca953x_gpio_set_value;
Phil Reidb4818af2015-12-04 15:52:30 +0800407 gc->set_multiple = pca953x_gpio_set_multiple;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100408 gc->can_sleep = true;
eric miao9e60fdc2008-02-04 22:28:26 -0800409
410 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800411 gc->ngpio = gpios;
412 gc->label = chip->client->name;
Linus Walleij58383c72015-11-04 09:56:26 +0100413 gc->parent = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700414 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700415 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800416}
417
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800418#ifdef CONFIG_GPIO_PCA953X_IRQ
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800419static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800420{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200421 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100422 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800423
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100424 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800425}
426
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800427static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800428{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200429 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100430 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800431
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100432 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800433}
434
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800435static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800436{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200437 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100438 struct pca953x_chip *chip = gpiochip_get_data(gc);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800439
440 mutex_lock(&chip->irq_lock);
441}
442
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800443static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800444{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200445 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100446 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100447 u8 new_irqs;
448 int level, i;
Yong Li44896be2016-04-07 12:56:32 +0800449 u8 invert_irq_mask[MAX_BANK];
450
451 if (chip->driver_data & PCA_PCAL) {
452 /* Enable latch on interrupt-enabled inputs */
453 pca953x_write_regs(chip, PCAL953X_IN_LATCH, chip->irq_mask);
454
455 for (i = 0; i < NBANK(chip); i++)
456 invert_irq_mask[i] = ~chip->irq_mask[i];
457
458 /* Unmask enabled interrupts */
459 pca953x_write_regs(chip, PCAL953X_INT_MASK, invert_irq_mask);
460 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700461
462 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100463 for (i = 0; i < NBANK(chip); i++) {
464 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
465 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700466
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100467 while (new_irqs) {
468 level = __ffs(new_irqs);
469 pca953x_gpio_direction_input(&chip->gpio_chip,
470 level + (BANK_SZ * i));
471 new_irqs &= ~(1 << level);
472 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700473 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800474
475 mutex_unlock(&chip->irq_lock);
476}
477
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800478static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800479{
Linus Walleij7bcbce52014-05-09 13:27:57 +0200480 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleij468e67f2015-12-07 11:20:54 +0100481 struct pca953x_chip *chip = gpiochip_get_data(gc);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100482 int bank_nb = d->hwirq / BANK_SZ;
483 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800484
485 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
486 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800487 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800488 return -EINVAL;
489 }
490
491 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100492 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800493 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100494 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800495
496 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100497 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800498 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100499 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800500
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700501 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800502}
503
504static struct irq_chip pca953x_irq_chip = {
505 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800506 .irq_mask = pca953x_irq_mask,
507 .irq_unmask = pca953x_irq_unmask,
508 .irq_bus_lock = pca953x_irq_bus_lock,
509 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
510 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800511};
512
Joshua Scottb6ac1282015-05-22 12:35:12 +1200513static bool pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800514{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100515 u8 cur_stat[MAX_BANK];
516 u8 old_stat[MAX_BANK];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200517 bool pending_seen = false;
518 bool trigger_seen = false;
519 u8 trigger[MAX_BANK];
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100520 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800521
Yong Li44896be2016-04-07 12:56:32 +0800522 if (chip->driver_data & PCA_PCAL) {
523 /* Read the current interrupt status from the device */
524 ret = pca953x_read_regs(chip, PCAL953X_INT_STAT, trigger);
525 if (ret)
526 return false;
527
528 /* Check latched inputs and clear interrupt status */
529 ret = pca953x_read_regs(chip, PCA953X_INPUT, cur_stat);
530 if (ret)
531 return false;
532
533 for (i = 0; i < NBANK(chip); i++) {
534 /* Apply filter for rising/falling edge selection */
535 pending[i] = (~cur_stat[i] & chip->irq_trig_fall[i]) |
536 (cur_stat[i] & chip->irq_trig_raise[i]);
537 pending[i] &= trigger[i];
538 if (pending[i])
539 pending_seen = true;
540 }
541
542 return pending_seen;
543 }
544
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800545 switch (chip->chip_type) {
546 case PCA953X_TYPE:
547 offset = PCA953X_INPUT;
548 break;
549 case PCA957X_TYPE:
550 offset = PCA957X_IN;
551 break;
552 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100553 ret = pca953x_read_regs(chip, offset, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800554 if (ret)
Joshua Scottb6ac1282015-05-22 12:35:12 +1200555 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800556
557 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100558 for (i = 0; i < NBANK(chip); i++)
559 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800560
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100561 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800562
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100563 for (i = 0; i < NBANK(chip); i++) {
564 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200565 if (trigger[i])
566 trigger_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100567 }
568
Joshua Scottb6ac1282015-05-22 12:35:12 +1200569 if (!trigger_seen)
570 return false;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800571
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100572 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800573
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100574 for (i = 0; i < NBANK(chip); i++) {
575 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
576 (cur_stat[i] & chip->irq_trig_raise[i]);
577 pending[i] &= trigger[i];
Joshua Scottb6ac1282015-05-22 12:35:12 +1200578 if (pending[i])
579 pending_seen = true;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100580 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800581
Joshua Scottb6ac1282015-05-22 12:35:12 +1200582 return pending_seen;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800583}
584
585static irqreturn_t pca953x_irq_handler(int irq, void *devid)
586{
587 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100588 u8 pending[MAX_BANK];
589 u8 level;
Toby Smith3275d072014-04-30 18:01:40 +1000590 unsigned nhandled = 0;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100591 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800592
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100593 if (!pca953x_irq_pending(chip, pending))
Toby Smith3275d072014-04-30 18:01:40 +1000594 return IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800595
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100596 for (i = 0; i < NBANK(chip); i++) {
597 while (pending[i]) {
598 level = __ffs(pending[i]);
Linus Walleij7bcbce52014-05-09 13:27:57 +0200599 handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100600 level + (BANK_SZ * i)));
601 pending[i] &= ~(1 << level);
Toby Smith3275d072014-04-30 18:01:40 +1000602 nhandled++;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100603 }
604 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800605
Toby Smith3275d072014-04-30 18:01:40 +1000606 return (nhandled > 0) ? IRQ_HANDLED : IRQ_NONE;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800607}
608
609static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200610 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800611{
612 struct i2c_client *client = chip->client;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100613 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800614
Markus Pargmann4bb93342014-07-29 09:24:43 +0200615 if (client->irq && irq_base != -1
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300616 && (chip->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800617
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800618 switch (chip->chip_type) {
619 case PCA953X_TYPE:
620 offset = PCA953X_INPUT;
621 break;
622 case PCA957X_TYPE:
623 offset = PCA957X_IN;
624 break;
625 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100626 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800627 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100628 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800629
630 /*
631 * There is no way to know which GPIO line generated the
632 * interrupt. We have to rely on the previous read for
633 * this purpose.
634 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100635 for (i = 0; i < NBANK(chip); i++)
636 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800637 mutex_init(&chip->irq_lock);
638
Linus Walleijb42748c2013-01-25 17:59:28 +0100639 ret = devm_request_threaded_irq(&client->dev,
640 client->irq,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800641 NULL,
642 pca953x_irq_handler,
Toby Smith91329132014-04-30 18:01:41 +1000643 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
644 IRQF_SHARED,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800645 dev_name(&client->dev), chip);
646 if (ret) {
647 dev_err(&client->dev, "failed to request irq %d\n",
648 client->irq);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100649 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800650 }
651
Linus Walleij7bcbce52014-05-09 13:27:57 +0200652 ret = gpiochip_irqchip_add(&chip->gpio_chip,
653 &pca953x_irq_chip,
654 irq_base,
655 handle_simple_irq,
656 IRQ_TYPE_NONE);
657 if (ret) {
658 dev_err(&client->dev,
659 "could not connect irqchip to gpiochip\n");
660 return ret;
661 }
Grygorii Strashkofdd50402015-07-07 17:34:49 +0300662
663 gpiochip_set_chained_irqchip(&chip->gpio_chip,
664 &pca953x_irq_chip,
665 client->irq, NULL);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800666 }
667
668 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800669}
670
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800671#else /* CONFIG_GPIO_PCA953X_IRQ */
672static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200673 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800674{
675 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800676
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300677 if (irq_base != -1 && (chip->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800678 dev_warn(&client->dev, "interrupt support not compiled in\n");
679
680 return 0;
681}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800682#endif
683
Bill Pemberton38363092012-11-19 13:22:34 -0500684static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800685{
686 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100687 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800688
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100689 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800690 if (ret)
691 goto out;
692
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100693 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
694 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800695 if (ret)
696 goto out;
697
698 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100699 if (invert)
700 memset(val, 0xFF, NBANK(chip));
701 else
702 memset(val, 0, NBANK(chip));
703
704 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800705out:
706 return ret;
707}
708
Bill Pemberton38363092012-11-19 13:22:34 -0500709static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800710{
711 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100712 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800713
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100714 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800715 if (ret)
716 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100717 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800718 if (ret)
719 goto out;
720
721 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100722 if (invert)
723 memset(val, 0xFF, NBANK(chip));
724 else
725 memset(val, 0, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400726 ret = pca953x_write_regs(chip, PCA957X_INVRT, val);
727 if (ret)
728 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800729
Colin Cronin20a8a962015-05-18 11:41:43 -0700730 /* To enable register 6, 7 to control pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100731 memset(val, 0x02, NBANK(chip));
Nicholas Krausec75a3772015-08-26 17:52:19 -0400732 ret = pca953x_write_regs(chip, PCA957X_BKEN, val);
733 if (ret)
734 goto out;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800735
736 return 0;
737out:
738 return ret;
739}
740
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000741static const struct of_device_id pca953x_dt_ids[];
742
Bill Pemberton38363092012-11-19 13:22:34 -0500743static int pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200744 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800745{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800746 struct pca953x_platform_data *pdata;
747 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530748 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200749 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530750 u32 invert = 0;
Phil Reide23efa32016-07-29 11:39:55 +0800751 struct regulator *reg;
eric miao9e60fdc2008-02-04 22:28:26 -0800752
Linus Walleijb42748c2013-01-25 17:59:28 +0100753 chip = devm_kzalloc(&client->dev,
754 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800755 if (chip == NULL)
756 return -ENOMEM;
757
Jingoo Hane56aee12013-07-30 17:08:05 +0900758 pdata = dev_get_platdata(&client->dev);
David Janderc6dcf592011-06-14 11:00:55 +0200759 if (pdata) {
760 irq_base = pdata->irq_base;
761 chip->gpio_start = pdata->gpio_base;
762 invert = pdata->invert;
763 chip->names = pdata->names;
764 } else {
Markus Pargmann4bb93342014-07-29 09:24:43 +0200765 chip->gpio_start = -1;
766 irq_base = 0;
Nate Case1965d302009-06-17 16:26:17 -0700767 }
768
eric miao9e60fdc2008-02-04 22:28:26 -0800769 chip->client = client;
770
Phil Reide23efa32016-07-29 11:39:55 +0800771 reg = devm_regulator_get(&client->dev, "vcc");
772 if (IS_ERR(reg)) {
773 ret = PTR_ERR(reg);
774 if (ret != -EPROBE_DEFER)
775 dev_err(&client->dev, "reg get err: %d\n", ret);
776 return ret;
777 }
778 ret = regulator_enable(reg);
779 if (ret) {
780 dev_err(&client->dev, "reg en err: %d\n", ret);
781 return ret;
782 }
783 chip->regulator = reg;
784
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300785 if (id) {
786 chip->driver_data = id->driver_data;
787 } else {
788 const struct acpi_device_id *id;
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000789 const struct of_device_id *match;
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300790
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000791 match = of_match_device(pca953x_dt_ids, &client->dev);
792 if (match) {
793 chip->driver_data = (int)(uintptr_t)match->data;
794 } else {
795 id = acpi_match_device(pca953x_acpi_ids, &client->dev);
Phil Reide23efa32016-07-29 11:39:55 +0800796 if (!id) {
797 ret = -ENODEV;
798 goto err_exit;
799 }
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300800
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000801 chip->driver_data = id->driver_data;
802 }
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300803 }
804
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300805 chip->chip_type = PCA_CHIP_TYPE(chip->driver_data);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700806
Roland Stigge6e20fb12011-02-10 15:01:23 -0800807 mutex_init(&chip->i2c_lock);
808
eric miao9e60fdc2008-02-04 22:28:26 -0800809 /* initialize cached registers from their original values.
810 * we can't share this chip with another i2c master.
811 */
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300812 pca953x_setup_gpio(chip, chip->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800813
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800814 if (chip->chip_type == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200815 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800816 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200817 ret = device_pca957x_init(chip, invert);
818 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +0800819 goto err_exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800820
Laxman Dewangan0ece84f2016-02-22 17:43:28 +0530821 ret = devm_gpiochip_add_data(&client->dev, &chip->gpio_chip, chip);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800822 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +0800823 goto err_exit;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800824
Andy Shevchenkoc6664142015-10-01 14:20:27 +0300825 ret = pca953x_irq_setup(chip, irq_base);
eric miao9e60fdc2008-02-04 22:28:26 -0800826 if (ret)
Phil Reide23efa32016-07-29 11:39:55 +0800827 goto err_exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800828
David Janderc6dcf592011-06-14 11:00:55 +0200829 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800830 ret = pdata->setup(client, chip->gpio_chip.base,
831 chip->gpio_chip.ngpio, pdata->context);
832 if (ret < 0)
833 dev_warn(&client->dev, "setup failed, %d\n", ret);
834 }
835
836 i2c_set_clientdata(client, chip);
837 return 0;
Phil Reide23efa32016-07-29 11:39:55 +0800838
839err_exit:
840 regulator_disable(chip->regulator);
841 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800842}
843
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800844static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800845{
Jingoo Hane56aee12013-07-30 17:08:05 +0900846 struct pca953x_platform_data *pdata = dev_get_platdata(&client->dev);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800847 struct pca953x_chip *chip = i2c_get_clientdata(client);
Andy Shevchenko8c7a92d2016-05-31 17:05:57 +0300848 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800849
David Janderc6dcf592011-06-14 11:00:55 +0200850 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800851 ret = pdata->teardown(client, chip->gpio_chip.base,
852 chip->gpio_chip.ngpio, pdata->context);
Phil Reide23efa32016-07-29 11:39:55 +0800853 if (ret < 0)
eric miao9e60fdc2008-02-04 22:28:26 -0800854 dev_err(&client->dev, "%s failed, %d\n",
855 "teardown", ret);
eric miao9e60fdc2008-02-04 22:28:26 -0800856 }
857
Phil Reide23efa32016-07-29 11:39:55 +0800858 regulator_disable(chip->regulator);
859
860 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800861}
862
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000863/* convenience to stop overlong match-table lines */
864#define OF_953X(__nrgpio, __int) (void *)(__nrgpio | PCA953X_TYPE | __int)
865#define OF_957X(__nrgpio, __int) (void *)(__nrgpio | PCA957X_TYPE | __int)
866
Maxime Riparded326202012-11-08 18:01:52 +0100867static const struct of_device_id pca953x_dt_ids[] = {
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000868 { .compatible = "nxp,pca9505", .data = OF_953X(40, PCA_INT), },
869 { .compatible = "nxp,pca9534", .data = OF_953X( 8, PCA_INT), },
870 { .compatible = "nxp,pca9535", .data = OF_953X(16, PCA_INT), },
871 { .compatible = "nxp,pca9536", .data = OF_953X( 4, 0), },
872 { .compatible = "nxp,pca9537", .data = OF_953X( 4, PCA_INT), },
873 { .compatible = "nxp,pca9538", .data = OF_953X( 8, PCA_INT), },
874 { .compatible = "nxp,pca9539", .data = OF_953X(16, PCA_INT), },
875 { .compatible = "nxp,pca9554", .data = OF_953X( 8, PCA_INT), },
876 { .compatible = "nxp,pca9555", .data = OF_953X(16, PCA_INT), },
877 { .compatible = "nxp,pca9556", .data = OF_953X( 8, 0), },
878 { .compatible = "nxp,pca9557", .data = OF_953X( 8, 0), },
879 { .compatible = "nxp,pca9574", .data = OF_957X( 8, PCA_INT), },
880 { .compatible = "nxp,pca9575", .data = OF_957X(16, PCA_INT), },
881 { .compatible = "nxp,pca9698", .data = OF_953X(40, 0), },
Maxime Riparded326202012-11-08 18:01:52 +0100882
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000883 { .compatible = "maxim,max7310", .data = OF_953X( 8, 0), },
884 { .compatible = "maxim,max7312", .data = OF_953X(16, PCA_INT), },
885 { .compatible = "maxim,max7313", .data = OF_953X(16, PCA_INT), },
886 { .compatible = "maxim,max7315", .data = OF_953X( 8, PCA_INT), },
Maxime Riparded326202012-11-08 18:01:52 +0100887
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000888 { .compatible = "ti,pca6107", .data = OF_953X( 8, PCA_INT), },
Vignesh R353661d2016-05-19 12:17:29 +0530889 { .compatible = "ti,pca9536", .data = OF_953X( 4, 0), },
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000890 { .compatible = "ti,tca6408", .data = OF_953X( 8, PCA_INT), },
891 { .compatible = "ti,tca6416", .data = OF_953X(16, PCA_INT), },
892 { .compatible = "ti,tca6424", .data = OF_953X(24, PCA_INT), },
Aaron Sierrae73760a2014-02-07 16:36:21 -0600893
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000894 { .compatible = "onsemi,pca9654", .data = OF_953X( 8, PCA_INT), },
Ben Dooks9f49f6d2015-12-08 10:19:25 +0000895
Ben Dooks6f29c9a2015-12-08 10:19:26 +0000896 { .compatible = "exar,xra1202", .data = OF_953X( 8, 0), },
Maxime Riparded326202012-11-08 18:01:52 +0100897 { }
898};
899
900MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
901
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800902static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800903 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800904 .name = "pca953x",
Maxime Riparded326202012-11-08 18:01:52 +0100905 .of_match_table = pca953x_dt_ids,
Andy Shevchenkof32517b2015-10-01 14:20:28 +0300906 .acpi_match_table = ACPI_PTR(pca953x_acpi_ids),
eric miao9e60fdc2008-02-04 22:28:26 -0800907 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800908 .probe = pca953x_probe,
909 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200910 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800911};
912
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800913static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800914{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800915 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800916}
David Brownell2f8d1192008-10-15 22:03:13 -0700917/* register after i2c postcore initcall and before
918 * subsys initcalls that may rely on these GPIOs
919 */
920subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800921
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800922static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800923{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800924 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800925}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800926module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800927
928MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800929MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800930MODULE_LICENSE("GPL");