blob: a261972f603d56003d5446eeb1c09511d80b4bf1 [file] [log] [blame]
eric miao9e60fdc2008-02-04 22:28:26 -08001/*
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -08002 * pca953x.c - 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>
eric miao9e60fdc2008-02-04 22:28:26 -080019#include <linux/i2c.h>
Guennadi Liakhovetskid1c057e32008-02-06 01:39:02 -080020#include <linux/i2c/pca953x.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Nate Case1965d302009-06-17 16:26:17 -070022#ifdef CONFIG_OF_GPIO
23#include <linux/of_platform.h>
24#include <linux/of_gpio.h>
25#endif
eric miao9e60fdc2008-02-04 22:28:26 -080026
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -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
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080032#define PCA953X_GPIOS 0x00FF
33#define PCA953X_INT 0x0100
34
Jean Delvare3760f732008-04-29 23:11:40 +020035static const struct i2c_device_id pca953x_id[] = {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080036 { "pca9534", 8 | PCA953X_INT, },
37 { "pca9535", 16 | PCA953X_INT, },
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080038 { "pca9536", 4, },
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080039 { "pca9537", 4 | PCA953X_INT, },
40 { "pca9538", 8 | PCA953X_INT, },
41 { "pca9539", 16 | PCA953X_INT, },
42 { "pca9554", 8 | PCA953X_INT, },
43 { "pca9555", 16 | PCA953X_INT, },
Nate Case10dfb542009-06-17 16:26:18 -070044 { "pca9556", 8, },
Will Newtonf39e5782008-05-01 04:35:10 -070045 { "pca9557", 8, },
David Brownellab5dc372009-01-06 14:42:27 -080046
David Brownell7059d4b2008-07-04 09:59:37 -070047 { "max7310", 8, },
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080048 { "max7312", 16 | PCA953X_INT, },
49 { "max7313", 16 | PCA953X_INT, },
50 { "max7315", 8 | PCA953X_INT, },
51 { "pca6107", 8 | PCA953X_INT, },
52 { "tca6408", 8 | PCA953X_INT, },
53 { "tca6416", 16 | PCA953X_INT, },
David Brownellab5dc372009-01-06 14:42:27 -080054 /* NYET: { "tca6424", 24, }, */
Jean Delvare3760f732008-04-29 23:11:40 +020055 { }
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080056};
Jean Delvare3760f732008-04-29 23:11:40 +020057MODULE_DEVICE_TABLE(i2c, pca953x_id);
eric miao9e60fdc2008-02-04 22:28:26 -080058
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080059struct pca953x_chip {
eric miao9e60fdc2008-02-04 22:28:26 -080060 unsigned gpio_start;
61 uint16_t reg_output;
62 uint16_t reg_direction;
63
Marc Zyngier89ea8bb2010-03-05 13:44:36 -080064#ifdef CONFIG_GPIO_PCA953X_IRQ
65 struct mutex irq_lock;
66 uint16_t irq_mask;
67 uint16_t irq_stat;
68 uint16_t irq_trig_raise;
69 uint16_t irq_trig_fall;
70 int irq_base;
71#endif
72
eric miao9e60fdc2008-02-04 22:28:26 -080073 struct i2c_client *client;
Nate Case1965d302009-06-17 16:26:17 -070074 struct pca953x_platform_data *dyn_pdata;
eric miao9e60fdc2008-02-04 22:28:26 -080075 struct gpio_chip gpio_chip;
Uwe Kleine-König62154992010-05-26 14:42:17 -070076 const char *const *names;
eric miao9e60fdc2008-02-04 22:28:26 -080077};
78
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080079static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
eric miao9e60fdc2008-02-04 22:28:26 -080080{
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080081 int ret;
82
83 if (chip->gpio_chip.ngpio <= 8)
84 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
eric miao9e60fdc2008-02-04 22:28:26 -080085 else
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080086 ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
87
88 if (ret < 0) {
89 dev_err(&chip->client->dev, "failed writing register\n");
David Brownellab5dc372009-01-06 14:42:27 -080090 return ret;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -080091 }
92
93 return 0;
eric miao9e60fdc2008-02-04 22:28:26 -080094}
95
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -080096static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
eric miao9e60fdc2008-02-04 22:28:26 -080097{
98 int ret;
99
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800100 if (chip->gpio_chip.ngpio <= 8)
101 ret = i2c_smbus_read_byte_data(chip->client, reg);
102 else
103 ret = i2c_smbus_read_word_data(chip->client, reg << 1);
104
eric miao9e60fdc2008-02-04 22:28:26 -0800105 if (ret < 0) {
106 dev_err(&chip->client->dev, "failed reading register\n");
David Brownellab5dc372009-01-06 14:42:27 -0800107 return ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800108 }
109
110 *val = (uint16_t)ret;
111 return 0;
112}
113
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800114static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800115{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800116 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800117 uint16_t reg_val;
118 int ret;
119
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800120 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800121
122 reg_val = chip->reg_direction | (1u << off);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800123 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800124 if (ret)
125 return ret;
126
127 chip->reg_direction = reg_val;
128 return 0;
129}
130
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800131static int pca953x_gpio_direction_output(struct gpio_chip *gc,
eric miao9e60fdc2008-02-04 22:28:26 -0800132 unsigned off, int val)
133{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800134 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800135 uint16_t reg_val;
136 int ret;
137
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800138 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800139
140 /* set output level */
141 if (val)
142 reg_val = chip->reg_output | (1u << off);
143 else
144 reg_val = chip->reg_output & ~(1u << off);
145
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800146 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800147 if (ret)
148 return ret;
149
150 chip->reg_output = reg_val;
151
152 /* then direction */
153 reg_val = chip->reg_direction & ~(1u << off);
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800154 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800155 if (ret)
156 return ret;
157
158 chip->reg_direction = reg_val;
159 return 0;
160}
161
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800162static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
eric miao9e60fdc2008-02-04 22:28:26 -0800163{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800164 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800165 uint16_t reg_val;
166 int ret;
167
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800168 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800169
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800170 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800171 if (ret < 0) {
172 /* NOTE: diagnostic already emitted; that's all we should
173 * do unless gpio_*_value_cansleep() calls become different
174 * from their nonsleeping siblings (and report faults).
175 */
176 return 0;
177 }
178
179 return (reg_val & (1u << off)) ? 1 : 0;
180}
181
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800182static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
eric miao9e60fdc2008-02-04 22:28:26 -0800183{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800184 struct pca953x_chip *chip;
eric miao9e60fdc2008-02-04 22:28:26 -0800185 uint16_t reg_val;
186 int ret;
187
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800188 chip = container_of(gc, struct pca953x_chip, gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800189
190 if (val)
191 reg_val = chip->reg_output | (1u << off);
192 else
193 reg_val = chip->reg_output & ~(1u << off);
194
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800195 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
eric miao9e60fdc2008-02-04 22:28:26 -0800196 if (ret)
197 return;
198
199 chip->reg_output = reg_val;
200}
201
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800202static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
eric miao9e60fdc2008-02-04 22:28:26 -0800203{
204 struct gpio_chip *gc;
205
206 gc = &chip->gpio_chip;
207
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800208 gc->direction_input = pca953x_gpio_direction_input;
209 gc->direction_output = pca953x_gpio_direction_output;
210 gc->get = pca953x_gpio_get_value;
211 gc->set = pca953x_gpio_set_value;
Arnaud Patard84207802008-03-10 11:43:48 -0700212 gc->can_sleep = 1;
eric miao9e60fdc2008-02-04 22:28:26 -0800213
214 gc->base = chip->gpio_start;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800215 gc->ngpio = gpios;
216 gc->label = chip->client->name;
David Brownelld8f388d2008-07-25 01:46:07 -0700217 gc->dev = &chip->client->dev;
Guennadi Liakhovetskid72cbed2008-04-28 02:14:45 -0700218 gc->owner = THIS_MODULE;
Daniel Silverstone77906a542009-06-17 16:26:15 -0700219 gc->names = chip->names;
eric miao9e60fdc2008-02-04 22:28:26 -0800220}
221
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800222#ifdef CONFIG_GPIO_PCA953X_IRQ
223static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
224{
225 struct pca953x_chip *chip;
226
227 chip = container_of(gc, struct pca953x_chip, gpio_chip);
228 return chip->irq_base + off;
229}
230
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800231static void pca953x_irq_mask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800232{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800233 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800234
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800235 chip->irq_mask &= ~(1 << (d->irq - chip->irq_base));
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800236}
237
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800238static void pca953x_irq_unmask(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800239{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800240 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800241
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800242 chip->irq_mask |= 1 << (d->irq - chip->irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800243}
244
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800245static void pca953x_irq_bus_lock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800246{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800247 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800248
249 mutex_lock(&chip->irq_lock);
250}
251
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800252static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800253{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800254 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700255 uint16_t new_irqs;
256 uint16_t level;
257
258 /* Look for any newly setup interrupt */
259 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
260 new_irqs &= ~chip->reg_direction;
261
262 while (new_irqs) {
263 level = __ffs(new_irqs);
264 pca953x_gpio_direction_input(&chip->gpio_chip, level);
265 new_irqs &= ~(1 << level);
266 }
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800267
268 mutex_unlock(&chip->irq_lock);
269}
270
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800271static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800272{
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800273 struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
274 uint16_t level = d->irq - chip->irq_base;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800275 uint16_t mask = 1 << level;
276
277 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
278 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800279 d->irq, type);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800280 return -EINVAL;
281 }
282
283 if (type & IRQ_TYPE_EDGE_FALLING)
284 chip->irq_trig_fall |= mask;
285 else
286 chip->irq_trig_fall &= ~mask;
287
288 if (type & IRQ_TYPE_EDGE_RISING)
289 chip->irq_trig_raise |= mask;
290 else
291 chip->irq_trig_raise &= ~mask;
292
Marc Zyngiera2cb9ae2010-04-27 13:13:07 -0700293 return 0;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800294}
295
296static struct irq_chip pca953x_irq_chip = {
297 .name = "pca953x",
Lennert Buytenhek6f5cfc02011-01-12 17:00:15 -0800298 .irq_mask = pca953x_irq_mask,
299 .irq_unmask = pca953x_irq_unmask,
300 .irq_bus_lock = pca953x_irq_bus_lock,
301 .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
302 .irq_set_type = pca953x_irq_set_type,
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800303};
304
305static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
306{
307 uint16_t cur_stat;
308 uint16_t old_stat;
309 uint16_t pending;
310 uint16_t trigger;
311 int ret;
312
313 ret = pca953x_read_reg(chip, PCA953X_INPUT, &cur_stat);
314 if (ret)
315 return 0;
316
317 /* Remove output pins from the equation */
318 cur_stat &= chip->reg_direction;
319
320 old_stat = chip->irq_stat;
321 trigger = (cur_stat ^ old_stat) & chip->irq_mask;
322
323 if (!trigger)
324 return 0;
325
326 chip->irq_stat = cur_stat;
327
328 pending = (old_stat & chip->irq_trig_fall) |
329 (cur_stat & chip->irq_trig_raise);
330 pending &= trigger;
331
332 return pending;
333}
334
335static irqreturn_t pca953x_irq_handler(int irq, void *devid)
336{
337 struct pca953x_chip *chip = devid;
338 uint16_t pending;
339 uint16_t level;
340
341 pending = pca953x_irq_pending(chip);
342
343 if (!pending)
344 return IRQ_HANDLED;
345
346 do {
347 level = __ffs(pending);
Alek Du8a233f02010-10-26 14:22:41 -0700348 generic_handle_irq(level + chip->irq_base);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800349
350 pending &= ~(1 << level);
351 } while (pending);
352
353 return IRQ_HANDLED;
354}
355
356static int pca953x_irq_setup(struct pca953x_chip *chip,
357 const struct i2c_device_id *id)
358{
359 struct i2c_client *client = chip->client;
360 struct pca953x_platform_data *pdata = client->dev.platform_data;
361 int ret;
362
Alek Du8a233f02010-10-26 14:22:41 -0700363 if (pdata->irq_base != -1
364 && (id->driver_data & PCA953X_INT)) {
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800365 int lvl;
366
367 ret = pca953x_read_reg(chip, PCA953X_INPUT,
368 &chip->irq_stat);
369 if (ret)
370 goto out_failed;
371
372 /*
373 * There is no way to know which GPIO line generated the
374 * interrupt. We have to rely on the previous read for
375 * this purpose.
376 */
377 chip->irq_stat &= chip->reg_direction;
378 chip->irq_base = pdata->irq_base;
379 mutex_init(&chip->irq_lock);
380
381 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
382 int irq = lvl + chip->irq_base;
383
384 set_irq_chip_data(irq, chip);
385 set_irq_chip_and_handler(irq, &pca953x_irq_chip,
386 handle_edge_irq);
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800387#ifdef CONFIG_ARM
388 set_irq_flags(irq, IRQF_VALID);
389#else
390 set_irq_noprobe(irq);
391#endif
392 }
393
394 ret = request_threaded_irq(client->irq,
395 NULL,
396 pca953x_irq_handler,
Alek Du8a233f02010-10-26 14:22:41 -0700397 IRQF_TRIGGER_RISING |
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800398 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
399 dev_name(&client->dev), chip);
400 if (ret) {
401 dev_err(&client->dev, "failed to request irq %d\n",
402 client->irq);
403 goto out_failed;
404 }
405
406 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
407 }
408
409 return 0;
410
411out_failed:
Alek Du8a233f02010-10-26 14:22:41 -0700412 chip->irq_base = -1;
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800413 return ret;
414}
415
416static void pca953x_irq_teardown(struct pca953x_chip *chip)
417{
Alek Du8a233f02010-10-26 14:22:41 -0700418 if (chip->irq_base != -1)
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800419 free_irq(chip->client->irq, chip);
420}
421#else /* CONFIG_GPIO_PCA953X_IRQ */
422static int pca953x_irq_setup(struct pca953x_chip *chip,
423 const struct i2c_device_id *id)
424{
425 struct i2c_client *client = chip->client;
426 struct pca953x_platform_data *pdata = client->dev.platform_data;
427
Alek Du8a233f02010-10-26 14:22:41 -0700428 if (pdata->irq_base != -1 && (id->driver_data & PCA953X_INT))
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800429 dev_warn(&client->dev, "interrupt support not compiled in\n");
430
431 return 0;
432}
433
434static void pca953x_irq_teardown(struct pca953x_chip *chip)
435{
436}
437#endif
438
Nate Case1965d302009-06-17 16:26:17 -0700439/*
440 * Handlers for alternative sources of platform_data
441 */
442#ifdef CONFIG_OF_GPIO
443/*
444 * Translate OpenFirmware node properties into platform_data
445 */
446static struct pca953x_platform_data *
447pca953x_get_alt_pdata(struct i2c_client *client)
448{
449 struct pca953x_platform_data *pdata;
450 struct device_node *node;
451 const uint16_t *val;
452
Grant Likely61c7a082010-04-13 16:12:29 -0700453 node = client->dev.of_node;
Nate Case1965d302009-06-17 16:26:17 -0700454 if (node == NULL)
455 return NULL;
456
457 pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
458 if (pdata == NULL) {
459 dev_err(&client->dev, "Unable to allocate platform_data\n");
460 return NULL;
461 }
462
463 pdata->gpio_base = -1;
464 val = of_get_property(node, "linux,gpio-base", NULL);
465 if (val) {
466 if (*val < 0)
467 dev_warn(&client->dev,
468 "invalid gpio-base in device tree\n");
469 else
470 pdata->gpio_base = *val;
471 }
472
473 val = of_get_property(node, "polarity", NULL);
474 if (val)
475 pdata->invert = *val;
476
477 return pdata;
478}
479#else
480static struct pca953x_platform_data *
481pca953x_get_alt_pdata(struct i2c_client *client)
482{
483 return NULL;
484}
485#endif
486
Jean Delvared2653e92008-04-29 23:11:39 +0200487static int __devinit pca953x_probe(struct i2c_client *client,
Jean Delvare3760f732008-04-29 23:11:40 +0200488 const struct i2c_device_id *id)
eric miao9e60fdc2008-02-04 22:28:26 -0800489{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800490 struct pca953x_platform_data *pdata;
491 struct pca953x_chip *chip;
Will Newtonf39e5782008-05-01 04:35:10 -0700492 int ret;
eric miao9e60fdc2008-02-04 22:28:26 -0800493
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800494 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
eric miao9e60fdc2008-02-04 22:28:26 -0800495 if (chip == NULL)
496 return -ENOMEM;
497
Nate Case1965d302009-06-17 16:26:17 -0700498 pdata = client->dev.platform_data;
499 if (pdata == NULL) {
500 pdata = pca953x_get_alt_pdata(client);
501 /*
502 * Unlike normal platform_data, this is allocated
503 * dynamically and must be freed in the driver
504 */
505 chip->dyn_pdata = pdata;
506 }
507
508 if (pdata == NULL) {
509 dev_dbg(&client->dev, "no platform data\n");
510 ret = -EINVAL;
511 goto out_failed;
512 }
513
eric miao9e60fdc2008-02-04 22:28:26 -0800514 chip->client = client;
515
516 chip->gpio_start = pdata->gpio_base;
517
Daniel Silverstone77906a542009-06-17 16:26:15 -0700518 chip->names = pdata->names;
519
eric miao9e60fdc2008-02-04 22:28:26 -0800520 /* initialize cached registers from their original values.
521 * we can't share this chip with another i2c master.
522 */
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800523 pca953x_setup_gpio(chip, id->driver_data & PCA953X_GPIOS);
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800524
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800525 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
eric miao9e60fdc2008-02-04 22:28:26 -0800526 if (ret)
527 goto out_failed;
528
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800529 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
eric miao9e60fdc2008-02-04 22:28:26 -0800530 if (ret)
531 goto out_failed;
532
533 /* set platform specific polarity inversion */
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800534 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
eric miao9e60fdc2008-02-04 22:28:26 -0800535 if (ret)
536 goto out_failed;
537
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800538 ret = pca953x_irq_setup(chip, id);
539 if (ret)
540 goto out_failed;
Guennadi Liakhovetskif5e8ff42008-02-06 01:39:04 -0800541
542 ret = gpiochip_add(&chip->gpio_chip);
eric miao9e60fdc2008-02-04 22:28:26 -0800543 if (ret)
544 goto out_failed;
545
546 if (pdata->setup) {
547 ret = pdata->setup(client, chip->gpio_chip.base,
548 chip->gpio_chip.ngpio, pdata->context);
549 if (ret < 0)
550 dev_warn(&client->dev, "setup failed, %d\n", ret);
551 }
552
553 i2c_set_clientdata(client, chip);
554 return 0;
555
556out_failed:
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800557 pca953x_irq_teardown(chip);
Nate Case1965d302009-06-17 16:26:17 -0700558 kfree(chip->dyn_pdata);
eric miao9e60fdc2008-02-04 22:28:26 -0800559 kfree(chip);
560 return ret;
561}
562
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800563static int pca953x_remove(struct i2c_client *client)
eric miao9e60fdc2008-02-04 22:28:26 -0800564{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800565 struct pca953x_platform_data *pdata = client->dev.platform_data;
566 struct pca953x_chip *chip = i2c_get_clientdata(client);
eric miao9e60fdc2008-02-04 22:28:26 -0800567 int ret = 0;
568
569 if (pdata->teardown) {
570 ret = pdata->teardown(client, chip->gpio_chip.base,
571 chip->gpio_chip.ngpio, pdata->context);
572 if (ret < 0) {
573 dev_err(&client->dev, "%s failed, %d\n",
574 "teardown", ret);
575 return ret;
576 }
577 }
578
579 ret = gpiochip_remove(&chip->gpio_chip);
580 if (ret) {
581 dev_err(&client->dev, "%s failed, %d\n",
582 "gpiochip_remove()", ret);
583 return ret;
584 }
585
Marc Zyngier89ea8bb2010-03-05 13:44:36 -0800586 pca953x_irq_teardown(chip);
Nate Case1965d302009-06-17 16:26:17 -0700587 kfree(chip->dyn_pdata);
eric miao9e60fdc2008-02-04 22:28:26 -0800588 kfree(chip);
589 return 0;
590}
591
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800592static struct i2c_driver pca953x_driver = {
eric miao9e60fdc2008-02-04 22:28:26 -0800593 .driver = {
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800594 .name = "pca953x",
eric miao9e60fdc2008-02-04 22:28:26 -0800595 },
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800596 .probe = pca953x_probe,
597 .remove = pca953x_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200598 .id_table = pca953x_id,
eric miao9e60fdc2008-02-04 22:28:26 -0800599};
600
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800601static int __init pca953x_init(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800602{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800603 return i2c_add_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800604}
David Brownell2f8d1192008-10-15 22:03:13 -0700605/* register after i2c postcore initcall and before
606 * subsys initcalls that may rely on these GPIOs
607 */
608subsys_initcall(pca953x_init);
eric miao9e60fdc2008-02-04 22:28:26 -0800609
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800610static void __exit pca953x_exit(void)
eric miao9e60fdc2008-02-04 22:28:26 -0800611{
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800612 i2c_del_driver(&pca953x_driver);
eric miao9e60fdc2008-02-04 22:28:26 -0800613}
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800614module_exit(pca953x_exit);
eric miao9e60fdc2008-02-04 22:28:26 -0800615
616MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
Guennadi Liakhovetskif3dc3632008-02-06 01:39:03 -0800617MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
eric miao9e60fdc2008-02-04 22:28:26 -0800618MODULE_LICENSE("GPL");