blob: 24059462c87fb23f6481304ae8d1bf819c2785e4 [file] [log] [blame]
eric miao9e60fdc2008-02-04 22:28:26 -08001/*
Grant Likelyc103de22011-06-04 18:38:28 -06002 * PCA953x 4/8/16 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>
18#include <linux/irq.h>
Maxime Ripard55ecd262012-11-08 18:01:51 +010019#include <linux/irqdomain.h>
eric miao9e60fdc2008-02-04 22:28:26 -080020#include <linux/i2c.h>
Guennadi Liakhovetskid1c057e32008-02-06 01:39:02 -080021#include <linux/i2c/pca953x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Nate Case1965d302009-06-17 16:26:17 -070023#ifdef CONFIG_OF_GPIO
24#include <linux/of_platform.h>
Nate Case1965d302009-06-17 16:26:17 -070025#endif
eric miao9e60fdc2008-02-04 22:28:26 -080026
Haojian Zhuang33226ff2011-04-18 22:12:46 +080027#define PCA953X_INPUT 0
28#define PCA953X_OUTPUT 1
29#define PCA953X_INVERT 2
30#define PCA953X_DIRECTION 3
eric miao9e60fdc2008-02-04 22:28:26 -080031
Andreas Schallenbergae79c192012-05-09 09:46:17 +020032#define REG_ADDR_AI 0x80
33
Haojian Zhuang33226ff2011-04-18 22:12:46 +080034#define PCA957X_IN 0
35#define PCA957X_INVRT 1
36#define PCA957X_BKEN 2
37#define PCA957X_PUPD 3
38#define PCA957X_CFG 4
39#define PCA957X_OUT 5
40#define PCA957X_MSK 6
41#define PCA957X_INTS 7
42
43#define PCA_GPIO_MASK 0x00FF
44#define PCA_INT 0x0100
45#define PCA953X_TYPE 0x1000
46#define PCA957X_TYPE 0x2000
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080047
Jean Delvare3760f732008-04-29 23:11:40 +020048static const struct i2c_device_id pca953x_id[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +010049 { "pca9505", 40 | PCA953X_TYPE | PCA_INT, },
Haojian Zhuang33226ff2011-04-18 22:12:46 +080050 { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
51 { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
52 { "pca9536", 4 | PCA953X_TYPE, },
53 { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
54 { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
55 { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
56 { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
57 { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
58 { "pca9556", 8 | PCA953X_TYPE, },
59 { "pca9557", 8 | PCA953X_TYPE, },
60 { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
61 { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
David Brownellab5dc3722009-01-06 14:42:27 -080062
Haojian Zhuang33226ff2011-04-18 22:12:46 +080063 { "max7310", 8 | PCA953X_TYPE, },
64 { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
65 { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
66 { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
67 { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
68 { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
69 { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
Andreas Schallenbergae79c192012-05-09 09:46:17 +020070 { "tca6424", 24 | PCA953X_TYPE | PCA_INT, },
Jean Delvare3760f732008-04-29 23:11:40 +020071 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080072};
Jean Delvare3760f732008-04-29 23:11:40 +020073MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080074
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010075#define MAX_BANK 5
76#define BANK_SZ 8
77
78#define NBANK(chip) (chip->gpio_chip.ngpio / BANK_SZ)
79
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080080struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080081 unsigned gpio_start;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010082 u8 reg_output[MAX_BANK];
83 u8 reg_direction[MAX_BANK];
Roland Stigge6e20fb12011-02-10 15:01:23 -080084 struct mutex i2c_lock;
eric miao9e60fdc2008-02-04 22:28:26 -080085
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080086#ifdef CONFIG_GPIO_PCA953X_IRQ
87 struct mutex irq_lock;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +010088 u8 irq_mask[MAX_BANK];
89 u8 irq_stat[MAX_BANK];
90 u8 irq_trig_raise[MAX_BANK];
91 u8 irq_trig_fall[MAX_BANK];
Maxime Ripard55ecd262012-11-08 18:01:51 +010092 struct irq_domain *domain;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080093#endif
94
eric miao9e60fdc2008-02-04 22:28:26 -080095 struct i2c_client *client;
96 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -070097 const char *const *names;
Haojian Zhuang33226ff2011-04-18 22:12:46 +080098 int chip_type;
eric miao9e60fdc2008-02-04 22:28:26 -080099};
100
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100101static int pca953x_read_single(struct pca953x_chip *chip, int reg, u32 *val,
102 int off)
103{
104 int ret;
105 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
106 int offset = off / BANK_SZ;
107
108 ret = i2c_smbus_read_byte_data(chip->client,
109 (reg << bank_shift) + offset);
110 *val = ret;
111
112 if (ret < 0) {
113 dev_err(&chip->client->dev, "failed reading register\n");
114 return ret;
115 }
116
117 return 0;
118}
119
120static int pca953x_write_single(struct pca953x_chip *chip, int reg, u32 val,
121 int off)
122{
123 int ret = 0;
124 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
125 int offset = off / BANK_SZ;
126
127 ret = i2c_smbus_write_byte_data(chip->client,
128 (reg << bank_shift) + offset, val);
129
130 if (ret < 0) {
131 dev_err(&chip->client->dev, "failed writing register\n");
132 return ret;
133 }
134
135 return 0;
136}
137
138static int pca953x_write_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800139{
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800140 int ret = 0;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800141
142 if (chip->gpio_chip.ngpio <= 8)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100143 ret = i2c_smbus_write_byte_data(chip->client, reg, *val);
144 else if (chip->gpio_chip.ngpio >= 24) {
145 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200146 ret = i2c_smbus_write_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100147 (reg << bank_shift) | REG_ADDR_AI,
148 NBANK(chip), val);
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200149 }
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800150 else {
151 switch (chip->chip_type) {
152 case PCA953X_TYPE:
153 ret = i2c_smbus_write_word_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100154 reg << 1, (u16) *val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800155 break;
156 case PCA957X_TYPE:
157 ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100158 val[0]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800159 if (ret < 0)
160 break;
161 ret = i2c_smbus_write_byte_data(chip->client,
162 (reg << 1) + 1,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100163 val[1]);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800164 break;
165 }
166 }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800167
168 if (ret < 0) {
169 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc3722009-01-06 14:42:27 -0800170 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800171 }
172
173 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800174}
175
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100176static int pca953x_read_regs(struct pca953x_chip *chip, int reg, u8 *val)
eric miao9e60fdc2008-02-04 22:28:26 -0800177{
178 int ret;
179
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200180 if (chip->gpio_chip.ngpio <= 8) {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800181 ret = i2c_smbus_read_byte_data(chip->client, reg);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200182 *val = ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100183 } else if (chip->gpio_chip.ngpio >= 24) {
184 int bank_shift = fls((chip->gpio_chip.ngpio - 1) / BANK_SZ);
185
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200186 ret = i2c_smbus_read_i2c_block_data(chip->client,
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100187 (reg << bank_shift) | REG_ADDR_AI,
188 NBANK(chip), val);
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200189 } else {
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800190 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100191 val[0] = (u16)ret & 0xFF;
192 val[1] = (u16)ret >> 8;
Andreas Schallenberg96b70642012-05-21 09:52:43 +0200193 }
eric miao9e60fdc2008-02-04 22:28:26 -0800194 if (ret < 0) {
195 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc3722009-01-06 14:42:27 -0800196 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800197 }
198
eric miao9e60fdc2008-02-04 22:28:26 -0800199 return 0;
200}
201
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800202static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800203{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800204 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100205 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800206 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800207
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800208 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800209
Roland Stigge6e20fb12011-02-10 15:01:23 -0800210 mutex_lock(&chip->i2c_lock);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100211 reg_val = chip->reg_direction[off / BANK_SZ] | (1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800212
213 switch (chip->chip_type) {
214 case PCA953X_TYPE:
215 offset = PCA953X_DIRECTION;
216 break;
217 case PCA957X_TYPE:
218 offset = PCA957X_CFG;
219 break;
220 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100221 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800222 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800223 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800224
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100225 chip->reg_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800226 ret = 0;
227exit:
228 mutex_unlock(&chip->i2c_lock);
229 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800230}
231
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800232static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800233 unsigned off, int val)
234{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800235 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100236 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800237 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800238
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800239 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800240
Roland Stigge6e20fb12011-02-10 15:01:23 -0800241 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800242 /* set output level */
243 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100244 reg_val = chip->reg_output[off / BANK_SZ]
245 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800246 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100247 reg_val = chip->reg_output[off / BANK_SZ]
248 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800249
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800250 switch (chip->chip_type) {
251 case PCA953X_TYPE:
252 offset = PCA953X_OUTPUT;
253 break;
254 case PCA957X_TYPE:
255 offset = PCA957X_OUT;
256 break;
257 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100258 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800259 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800260 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800261
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100262 chip->reg_output[off / BANK_SZ] = reg_val;
eric miao9e60fdc2008-02-04 22:28:26 -0800263
264 /* then direction */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100265 reg_val = chip->reg_direction[off / BANK_SZ] & ~(1u << (off % BANK_SZ));
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800266 switch (chip->chip_type) {
267 case PCA953X_TYPE:
268 offset = PCA953X_DIRECTION;
269 break;
270 case PCA957X_TYPE:
271 offset = PCA957X_CFG;
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_direction[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800279 ret = 0;
280exit:
281 mutex_unlock(&chip->i2c_lock);
282 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800283}
284
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800285static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800286{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800287 struct pca953x_chip *chip;
Andreas Schallenbergae79c192012-05-09 09:46:17 +0200288 u32 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800289 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800290
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800291 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800292
Roland Stigge6e20fb12011-02-10 15:01:23 -0800293 mutex_lock(&chip->i2c_lock);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800294 switch (chip->chip_type) {
295 case PCA953X_TYPE:
296 offset = PCA953X_INPUT;
297 break;
298 case PCA957X_TYPE:
299 offset = PCA957X_IN;
300 break;
301 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100302 ret = pca953x_read_single(chip, offset, &reg_val, off);
Roland Stigge6e20fb12011-02-10 15:01:23 -0800303 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800304 if (ret < 0) {
305 /* NOTE: diagnostic already emitted; that's all we should
306 * do unless gpio_*_value_cansleep() calls become different
307 * from their nonsleeping siblings (and report faults).
308 */
309 return 0;
310 }
311
312 return (reg_val & (1u << off)) ? 1 : 0;
313}
314
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800315static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800316{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800317 struct pca953x_chip *chip;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100318 u8 reg_val;
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800319 int ret, offset = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800320
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800321 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800322
Roland Stigge6e20fb12011-02-10 15:01:23 -0800323 mutex_lock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800324 if (val)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100325 reg_val = chip->reg_output[off / BANK_SZ]
326 | (1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800327 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100328 reg_val = chip->reg_output[off / BANK_SZ]
329 & ~(1u << (off % BANK_SZ));
eric miao9e60fdc2008-02-04 22:28:26 -0800330
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800331 switch (chip->chip_type) {
332 case PCA953X_TYPE:
333 offset = PCA953X_OUTPUT;
334 break;
335 case PCA957X_TYPE:
336 offset = PCA957X_OUT;
337 break;
338 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100339 ret = pca953x_write_single(chip, offset, reg_val, off);
eric miao9e60fdc2008-02-04 22:28:26 -0800340 if (ret)
Roland Stigge6e20fb12011-02-10 15:01:23 -0800341 goto exit;
eric miao9e60fdc2008-02-04 22:28:26 -0800342
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100343 chip->reg_output[off / BANK_SZ] = reg_val;
Roland Stigge6e20fb12011-02-10 15:01:23 -0800344exit:
345 mutex_unlock(&chip->i2c_lock);
eric miao9e60fdc2008-02-04 22:28:26 -0800346}
347
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800348static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800349{
350 struct gpio_chip *gc;
351
352 gc = &chip->gpio_chip;
353
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800354 gc->direction_input = pca953x_gpio_direction_input;
355 gc->direction_output = pca953x_gpio_direction_output;
356 gc->get = pca953x_gpio_get_value;
357 gc->set = pca953x_gpio_set_value;
Arnaud Patard84207802008-03-10 11:43:48 -0700358 gc->can_sleep = 1;
eric miao9e60fdc2008-02-04 22:28:26 -0800359
360 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800361 gc->ngpio = gpios;
362 gc->label = chip->client->name;
David Brownelld8f388d82008-07-25 01:46:07 -0700363 gc->dev = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700364 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700365 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800366}
367
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800368#ifdef CONFIG_GPIO_PCA953X_IRQ
369static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
370{
371 struct pca953x_chip *chip;
372
373 chip = container_of(gc, struct pca953x_chip, gpio_chip);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100374 return irq_create_mapping(chip->domain, off);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800375}
376
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800377static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800378{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800379 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800380
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100381 chip->irq_mask[d->hwirq / BANK_SZ] &= ~(1 << (d->hwirq % BANK_SZ));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800382}
383
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800384static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800385{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800386 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800387
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100388 chip->irq_mask[d->hwirq / BANK_SZ] |= 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800389}
390
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800391static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800392{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800393 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800394
395 mutex_lock(&chip->irq_lock);
396}
397
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800398static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800399{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800400 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100401 u8 new_irqs;
402 int level, i;
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700403
404 /* Look for any newly setup interrupt */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100405 for (i = 0; i < NBANK(chip); i++) {
406 new_irqs = chip->irq_trig_fall[i] | chip->irq_trig_raise[i];
407 new_irqs &= ~chip->reg_direction[i];
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700408
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100409 while (new_irqs) {
410 level = __ffs(new_irqs);
411 pca953x_gpio_direction_input(&chip->gpio_chip,
412 level + (BANK_SZ * i));
413 new_irqs &= ~(1 << level);
414 }
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700415 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800416
417 mutex_unlock(&chip->irq_lock);
418}
419
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800420static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800421{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800422 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100423 int bank_nb = d->hwirq / BANK_SZ;
424 u8 mask = 1 << (d->hwirq % BANK_SZ);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800425
426 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
427 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800428 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800429 return -EINVAL;
430 }
431
432 if (type & IRQ_TYPE_EDGE_FALLING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100433 chip->irq_trig_fall[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800434 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100435 chip->irq_trig_fall[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800436
437 if (type & IRQ_TYPE_EDGE_RISING)
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100438 chip->irq_trig_raise[bank_nb] |= mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800439 else
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100440 chip->irq_trig_raise[bank_nb] &= ~mask;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800441
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700442 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800443}
444
445static struct irq_chip pca953x_irq_chip = {
446 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800447 .irq_mask = pca953x_irq_mask,
448 .irq_unmask = pca953x_irq_unmask,
449 .irq_bus_lock = pca953x_irq_bus_lock,
450 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
451 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800452};
453
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100454static u8 pca953x_irq_pending(struct pca953x_chip *chip, u8 *pending)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800455{
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100456 u8 cur_stat[MAX_BANK];
457 u8 old_stat[MAX_BANK];
458 u8 pendings = 0;
459 u8 trigger[MAX_BANK], triggers = 0;
460 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800461
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800462 switch (chip->chip_type) {
463 case PCA953X_TYPE:
464 offset = PCA953X_INPUT;
465 break;
466 case PCA957X_TYPE:
467 offset = PCA957X_IN;
468 break;
469 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100470 ret = pca953x_read_regs(chip, offset, cur_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800471 if (ret)
472 return 0;
473
474 /* Remove output pins from the equation */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100475 for (i = 0; i < NBANK(chip); i++)
476 cur_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800477
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100478 memcpy(old_stat, chip->irq_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800479
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100480 for (i = 0; i < NBANK(chip); i++) {
481 trigger[i] = (cur_stat[i] ^ old_stat[i]) & chip->irq_mask[i];
482 triggers += trigger[i];
483 }
484
485 if (!triggers)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800486 return 0;
487
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100488 memcpy(chip->irq_stat, cur_stat, NBANK(chip));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800489
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100490 for (i = 0; i < NBANK(chip); i++) {
491 pending[i] = (old_stat[i] & chip->irq_trig_fall[i]) |
492 (cur_stat[i] & chip->irq_trig_raise[i]);
493 pending[i] &= trigger[i];
494 pendings += pending[i];
495 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800496
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100497 return pendings;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800498}
499
500static irqreturn_t pca953x_irq_handler(int irq, void *devid)
501{
502 struct pca953x_chip *chip = devid;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100503 u8 pending[MAX_BANK];
504 u8 level;
505 int i;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800506
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100507 if (!pca953x_irq_pending(chip, pending))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800508 return IRQ_HANDLED;
509
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100510 for (i = 0; i < NBANK(chip); i++) {
511 while (pending[i]) {
512 level = __ffs(pending[i]);
513 handle_nested_irq(irq_find_mapping(chip->domain,
514 level + (BANK_SZ * i)));
515 pending[i] &= ~(1 << level);
516 }
517 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800518
519 return IRQ_HANDLED;
520}
521
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100522static int pca953x_gpio_irq_map(struct irq_domain *d, unsigned int irq,
523 irq_hw_number_t hwirq)
524{
525 irq_clear_status_flags(irq, IRQ_NOREQUEST);
526 irq_set_chip_data(irq, d->host_data);
527 irq_set_chip(irq, &pca953x_irq_chip);
528 irq_set_nested_thread(irq, true);
529#ifdef CONFIG_ARM
530 set_irq_flags(irq, IRQF_VALID);
531#else
532 irq_set_noprobe(irq);
533#endif
534
535 return 0;
536}
537
538static const struct irq_domain_ops pca953x_irq_simple_ops = {
539 .map = pca953x_gpio_irq_map,
540 .xlate = irq_domain_xlate_twocell,
541};
542
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800543static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200544 const struct i2c_device_id *id,
545 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800546{
547 struct i2c_client *client = chip->client;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100548 int ret, i, offset = 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800549
David Janderc6dcf592011-06-14 11:00:55 +0200550 if (irq_base != -1
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800551 && (id->driver_data & PCA_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800552
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800553 switch (chip->chip_type) {
554 case PCA953X_TYPE:
555 offset = PCA953X_INPUT;
556 break;
557 case PCA957X_TYPE:
558 offset = PCA957X_IN;
559 break;
560 }
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100561 ret = pca953x_read_regs(chip, offset, chip->irq_stat);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800562 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100563 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800564
565 /*
566 * There is no way to know which GPIO line generated the
567 * interrupt. We have to rely on the previous read for
568 * this purpose.
569 */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100570 for (i = 0; i < NBANK(chip); i++)
571 chip->irq_stat[i] &= chip->reg_direction[i];
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800572 mutex_init(&chip->irq_lock);
573
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100574 chip->domain = irq_domain_add_simple(client->dev.of_node,
Maxime Ripard55ecd262012-11-08 18:01:51 +0100575 chip->gpio_chip.ngpio,
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100576 irq_base,
577 &pca953x_irq_simple_ops,
Maxime Ripard55ecd262012-11-08 18:01:51 +0100578 NULL);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100579 if (!chip->domain)
580 return -ENODEV;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800581
Linus Walleijb42748c2013-01-25 17:59:28 +0100582 ret = devm_request_threaded_irq(&client->dev,
583 client->irq,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800584 NULL,
585 pca953x_irq_handler,
David Jander17e8b422011-06-08 11:34:41 -0600586 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800587 dev_name(&client->dev), chip);
588 if (ret) {
589 dev_err(&client->dev, "failed to request irq %d\n",
590 client->irq);
Gregory CLEMENT0e8f2fd2013-01-25 17:59:27 +0100591 return ret;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800592 }
593
594 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
595 }
596
597 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800598}
599
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800600#else /* CONFIG_GPIO_PCA953X_IRQ */
601static int pca953x_irq_setup(struct pca953x_chip *chip,
David Janderc6dcf592011-06-14 11:00:55 +0200602 const struct i2c_device_id *id,
603 int irq_base)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800604{
605 struct i2c_client *client = chip->client;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800606
David Janderc6dcf592011-06-14 11:00:55 +0200607 if (irq_base != -1 && (id->driver_data & PCA_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800608 dev_warn(&client->dev, "interrupt support not compiled in\n");
609
610 return 0;
611}
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800612#endif
613
Nate Case1965d302009-06-17 16:26:17 -0700614/*
615 * Handlers for alternative sources of platform_data
616 */
617#ifdef CONFIG_OF_GPIO
618/*
619 * Translate OpenFirmware node properties into platform_data
David Jandera57339b2011-06-14 11:00:56 +0200620 * WARNING: This is DEPRECATED and will be removed eventually!
Nate Case1965d302009-06-17 16:26:17 -0700621 */
Mark Brown404ba2b2011-11-21 17:57:25 +0000622static void
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530623pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
Nate Case1965d302009-06-17 16:26:17 -0700624{
Nate Case1965d302009-06-17 16:26:17 -0700625 struct device_node *node;
Dirk Eibach16482372011-02-24 10:20:43 +0100626 const __be32 *val;
627 int size;
Nate Case1965d302009-06-17 16:26:17 -0700628
Grant Likely61c7a082010-04-13 16:12:29 -0700629 node = client->dev.of_node;
Nate Case1965d302009-06-17 16:26:17 -0700630 if (node == NULL)
David Janderc6dcf592011-06-14 11:00:55 +0200631 return;
Nate Case1965d302009-06-17 16:26:17 -0700632
David Janderc6dcf592011-06-14 11:00:55 +0200633 *gpio_base = -1;
Dirk Eibach16482372011-02-24 10:20:43 +0100634 val = of_get_property(node, "linux,gpio-base", &size);
David Jandera57339b2011-06-14 11:00:56 +0200635 WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
Nate Case1965d302009-06-17 16:26:17 -0700636 if (val) {
Dirk Eibach16482372011-02-24 10:20:43 +0100637 if (size != sizeof(*val))
638 dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
639 node->full_name);
Nate Case1965d302009-06-17 16:26:17 -0700640 else
David Janderc6dcf592011-06-14 11:00:55 +0200641 *gpio_base = be32_to_cpup(val);
Nate Case1965d302009-06-17 16:26:17 -0700642 }
643
644 val = of_get_property(node, "polarity", NULL);
David Jandera57339b2011-06-14 11:00:56 +0200645 WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
Nate Case1965d302009-06-17 16:26:17 -0700646 if (val)
David Janderc6dcf592011-06-14 11:00:55 +0200647 *invert = *val;
Nate Case1965d302009-06-17 16:26:17 -0700648}
649#else
Mark Brown404ba2b2011-11-21 17:57:25 +0000650static void
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530651pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, u32 *invert)
Nate Case1965d302009-06-17 16:26:17 -0700652{
Hartmut Knaack25fcf2b2011-10-11 00:22:45 +0200653 *gpio_base = -1;
Nate Case1965d302009-06-17 16:26:17 -0700654}
655#endif
656
Bill Pemberton38363092012-11-19 13:22:34 -0500657static int device_pca953x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800658{
659 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100660 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800661
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100662 ret = pca953x_read_regs(chip, PCA953X_OUTPUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800663 if (ret)
664 goto out;
665
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100666 ret = pca953x_read_regs(chip, PCA953X_DIRECTION,
667 chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800668 if (ret)
669 goto out;
670
671 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100672 if (invert)
673 memset(val, 0xFF, NBANK(chip));
674 else
675 memset(val, 0, NBANK(chip));
676
677 ret = pca953x_write_regs(chip, PCA953X_INVERT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800678out:
679 return ret;
680}
681
Bill Pemberton38363092012-11-19 13:22:34 -0500682static int device_pca957x_init(struct pca953x_chip *chip, u32 invert)
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800683{
684 int ret;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100685 u8 val[MAX_BANK];
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800686
687 /* Let every port in proper state, that could save power */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100688 memset(val, 0, NBANK(chip));
689 pca953x_write_regs(chip, PCA957X_PUPD, val);
690 memset(val, 0xFF, NBANK(chip));
691 pca953x_write_regs(chip, PCA957X_CFG, val);
692 memset(val, 0, NBANK(chip));
693 pca953x_write_regs(chip, PCA957X_OUT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800694
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100695 ret = pca953x_read_regs(chip, PCA957X_IN, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800696 if (ret)
697 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100698 ret = pca953x_read_regs(chip, PCA957X_OUT, chip->reg_output);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800699 if (ret)
700 goto out;
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100701 ret = pca953x_read_regs(chip, PCA957X_CFG, chip->reg_direction);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800702 if (ret)
703 goto out;
704
705 /* set platform specific polarity inversion */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100706 if (invert)
707 memset(val, 0xFF, NBANK(chip));
708 else
709 memset(val, 0, NBANK(chip));
710 pca953x_write_regs(chip, PCA957X_INVRT, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800711
712 /* To enable register 6, 7 to controll pull up and pull down */
Gregory CLEMENTf5f0b7a2013-01-22 22:10:23 +0100713 memset(val, 0x02, NBANK(chip));
714 pca953x_write_regs(chip, PCA957X_BKEN, val);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800715
716 return 0;
717out:
718 return ret;
719}
720
Bill Pemberton38363092012-11-19 13:22:34 -0500721static int pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200722 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800723{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800724 struct pca953x_platform_data *pdata;
725 struct pca953x_chip *chip;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530726 int irq_base = 0;
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200727 int ret;
Chandrabhanu Mahapatra6a7b36a2012-07-10 19:05:37 +0530728 u32 invert = 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800729
Linus Walleijb42748c2013-01-25 17:59:28 +0100730 chip = devm_kzalloc(&client->dev,
731 sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800732 if (chip == NULL)
733 return -ENOMEM;
734
Nate Case1965d302009-06-17 16:26:17 -0700735 pdata = client->dev.platform_data;
David Janderc6dcf592011-06-14 11:00:55 +0200736 if (pdata) {
737 irq_base = pdata->irq_base;
738 chip->gpio_start = pdata->gpio_base;
739 invert = pdata->invert;
740 chip->names = pdata->names;
741 } else {
742 pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
David Jandera57339b2011-06-14 11:00:56 +0200743#ifdef CONFIG_OF_GPIO
744 /* If I2C node has no interrupts property, disable GPIO interrupts */
745 if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
746 irq_base = -1;
747#endif
Nate Case1965d302009-06-17 16:26:17 -0700748 }
749
eric miao9e60fdc2008-02-04 22:28:26 -0800750 chip->client = client;
751
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800752 chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
Daniel Silverstone77906a542009-06-17 16:26:15 -0700753
Roland Stigge6e20fb12011-02-10 15:01:23 -0800754 mutex_init(&chip->i2c_lock);
755
eric miao9e60fdc2008-02-04 22:28:26 -0800756 /* initialize cached registers from their original values.
757 * we can't share this chip with another i2c master.
758 */
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800759 pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800760
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800761 if (chip->chip_type == PCA953X_TYPE)
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200762 ret = device_pca953x_init(chip, invert);
Haojian Zhuang33226ff2011-04-18 22:12:46 +0800763 else
Wolfram Sang7ea2aa22011-10-14 15:32:00 +0200764 ret = device_pca957x_init(chip, invert);
765 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100766 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800767
David Janderc6dcf592011-06-14 11:00:55 +0200768 ret = pca953x_irq_setup(chip, id, irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800769 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100770 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800771
772 ret = gpiochip_add(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800773 if (ret)
Linus Walleijb42748c2013-01-25 17:59:28 +0100774 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800775
David Janderc6dcf592011-06-14 11:00:55 +0200776 if (pdata && pdata->setup) {
eric miao9e60fdc2008-02-04 22:28:26 -0800777 ret = pdata->setup(client, chip->gpio_chip.base,
778 chip->gpio_chip.ngpio, pdata->context);
779 if (ret < 0)
780 dev_warn(&client->dev, "setup failed, %d\n", ret);
781 }
782
783 i2c_set_clientdata(client, chip);
784 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -0800785}
786
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800787static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800788{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800789 struct pca953x_platform_data *pdata = client->dev.platform_data;
790 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800791 int ret = 0;
792
David Janderc6dcf592011-06-14 11:00:55 +0200793 if (pdata && pdata->teardown) {
eric miao9e60fdc2008-02-04 22:28:26 -0800794 ret = pdata->teardown(client, chip->gpio_chip.base,
795 chip->gpio_chip.ngpio, pdata->context);
796 if (ret < 0) {
797 dev_err(&client->dev, "%s failed, %d\n",
798 "teardown", ret);
799 return ret;
800 }
801 }
802
803 ret = gpiochip_remove(&chip->gpio_chip);
804 if (ret) {
805 dev_err(&client->dev, "%s failed, %d\n",
806 "gpiochip_remove()", ret);
807 return ret;
808 }
809
eric miao9e60fdc2008-02-04 22:28:26 -0800810 return 0;
811}
812
Maxime Riparded326202012-11-08 18:01:52 +0100813static const struct of_device_id pca953x_dt_ids[] = {
Gregory CLEMENT89f5df02013-01-22 22:10:24 +0100814 { .compatible = "nxp,pca9505", },
Maxime Riparded326202012-11-08 18:01:52 +0100815 { .compatible = "nxp,pca9534", },
816 { .compatible = "nxp,pca9535", },
817 { .compatible = "nxp,pca9536", },
818 { .compatible = "nxp,pca9537", },
819 { .compatible = "nxp,pca9538", },
820 { .compatible = "nxp,pca9539", },
821 { .compatible = "nxp,pca9554", },
822 { .compatible = "nxp,pca9555", },
823 { .compatible = "nxp,pca9556", },
824 { .compatible = "nxp,pca9557", },
825 { .compatible = "nxp,pca9574", },
826 { .compatible = "nxp,pca9575", },
827
828 { .compatible = "maxim,max7310", },
829 { .compatible = "maxim,max7312", },
830 { .compatible = "maxim,max7313", },
831 { .compatible = "maxim,max7315", },
832
833 { .compatible = "ti,pca6107", },
834 { .compatible = "ti,tca6408", },
835 { .compatible = "ti,tca6416", },
836 { .compatible = "ti,tca6424", },
837 { }
838};
839
840MODULE_DEVICE_TABLE(of, pca953x_dt_ids);
841
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800842static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800843 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800844 .name = "pca953x",
Maxime Riparded326202012-11-08 18:01:52 +0100845 .of_match_table = pca953x_dt_ids,
eric miao9e60fdc2008-02-04 22:28:26 -0800846 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800847 .probe = pca953x_probe,
848 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200849 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800850};
851
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800852static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800853{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800854 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800855}
David Brownell2f8d1192008-10-15 22:03:13 -0700856/* register after i2c postcore initcall and before
857 * subsys initcalls that may rely on these GPIOs
858 */
859subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800860
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800861static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800862{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800863 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800864}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800865module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800866
867MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800868MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800869MODULE_LICENSE("GPL");