]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/gpio/pca953x.c
Merge branch 'fix' of git://git.kernel.org/pub/scm/linux/kernel/git/ycmiao/pxa-linux-2.6
[net-next-2.6.git] / drivers / gpio / pca953x.c
CommitLineData
9e60fdcf 1/*
f5e8ff48 2 * pca953x.c - 4/8/16 bit I/O ports
9e60fdcf 3 *
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>
d120c17f 16#include <linux/gpio.h>
89ea8bbe
MZ
17#include <linux/interrupt.h>
18#include <linux/irq.h>
9e60fdcf 19#include <linux/i2c.h>
d1c057e3 20#include <linux/i2c/pca953x.h>
5a0e3ad6 21#include <linux/slab.h>
1965d303
NC
22#ifdef CONFIG_OF_GPIO
23#include <linux/of_platform.h>
24#include <linux/of_gpio.h>
25#endif
9e60fdcf 26
f5e8ff48
GL
27#define PCA953X_INPUT 0
28#define PCA953X_OUTPUT 1
29#define PCA953X_INVERT 2
30#define PCA953X_DIRECTION 3
9e60fdcf 31
89ea8bbe
MZ
32#define PCA953X_GPIOS 0x00FF
33#define PCA953X_INT 0x0100
34
3760f736 35static const struct i2c_device_id pca953x_id[] = {
89ea8bbe
MZ
36 { "pca9534", 8 | PCA953X_INT, },
37 { "pca9535", 16 | PCA953X_INT, },
f5e8ff48 38 { "pca9536", 4, },
89ea8bbe
MZ
39 { "pca9537", 4 | PCA953X_INT, },
40 { "pca9538", 8 | PCA953X_INT, },
41 { "pca9539", 16 | PCA953X_INT, },
42 { "pca9554", 8 | PCA953X_INT, },
43 { "pca9555", 16 | PCA953X_INT, },
10dfb54c 44 { "pca9556", 8, },
f39e5781 45 { "pca9557", 8, },
ab5dc372 46
7059d4b0 47 { "max7310", 8, },
89ea8bbe
MZ
48 { "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, },
ab5dc372 54 /* NYET: { "tca6424", 24, }, */
3760f736 55 { }
f5e8ff48 56};
3760f736 57MODULE_DEVICE_TABLE(i2c, pca953x_id);
9e60fdcf 58
f3dc3630 59struct pca953x_chip {
9e60fdcf 60 unsigned gpio_start;
61 uint16_t reg_output;
62 uint16_t reg_direction;
63
89ea8bbe
MZ
64#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
9e60fdcf 73 struct i2c_client *client;
1965d303 74 struct pca953x_platform_data *dyn_pdata;
9e60fdcf 75 struct gpio_chip gpio_chip;
62154991 76 const char *const *names;
9e60fdcf 77};
78
f3dc3630 79static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
9e60fdcf 80{
f5e8ff48
GL
81 int ret;
82
83 if (chip->gpio_chip.ngpio <= 8)
84 ret = i2c_smbus_write_byte_data(chip->client, reg, val);
9e60fdcf 85 else
f5e8ff48
GL
86 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");
ab5dc372 90 return ret;
f5e8ff48
GL
91 }
92
93 return 0;
9e60fdcf 94}
95
f3dc3630 96static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
9e60fdcf 97{
98 int ret;
99
f5e8ff48
GL
100 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
9e60fdcf 105 if (ret < 0) {
106 dev_err(&chip->client->dev, "failed reading register\n");
ab5dc372 107 return ret;
9e60fdcf 108 }
109
110 *val = (uint16_t)ret;
111 return 0;
112}
113
f3dc3630 114static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
9e60fdcf 115{
f3dc3630 116 struct pca953x_chip *chip;
9e60fdcf 117 uint16_t reg_val;
118 int ret;
119
f3dc3630 120 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 121
122 reg_val = chip->reg_direction | (1u << off);
f3dc3630 123 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
9e60fdcf 124 if (ret)
125 return ret;
126
127 chip->reg_direction = reg_val;
128 return 0;
129}
130
f3dc3630 131static int pca953x_gpio_direction_output(struct gpio_chip *gc,
9e60fdcf 132 unsigned off, int val)
133{
f3dc3630 134 struct pca953x_chip *chip;
9e60fdcf 135 uint16_t reg_val;
136 int ret;
137
f3dc3630 138 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 139
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
f3dc3630 146 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
9e60fdcf 147 if (ret)
148 return ret;
149
150 chip->reg_output = reg_val;
151
152 /* then direction */
153 reg_val = chip->reg_direction & ~(1u << off);
f3dc3630 154 ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
9e60fdcf 155 if (ret)
156 return ret;
157
158 chip->reg_direction = reg_val;
159 return 0;
160}
161
f3dc3630 162static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
9e60fdcf 163{
f3dc3630 164 struct pca953x_chip *chip;
9e60fdcf 165 uint16_t reg_val;
166 int ret;
167
f3dc3630 168 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 169
f3dc3630 170 ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
9e60fdcf 171 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
f3dc3630 182static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
9e60fdcf 183{
f3dc3630 184 struct pca953x_chip *chip;
9e60fdcf 185 uint16_t reg_val;
186 int ret;
187
f3dc3630 188 chip = container_of(gc, struct pca953x_chip, gpio_chip);
9e60fdcf 189
190 if (val)
191 reg_val = chip->reg_output | (1u << off);
192 else
193 reg_val = chip->reg_output & ~(1u << off);
194
f3dc3630 195 ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
9e60fdcf 196 if (ret)
197 return;
198
199 chip->reg_output = reg_val;
200}
201
f5e8ff48 202static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
9e60fdcf 203{
204 struct gpio_chip *gc;
205
206 gc = &chip->gpio_chip;
207
f3dc3630
GL
208 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;
84207805 212 gc->can_sleep = 1;
9e60fdcf 213
214 gc->base = chip->gpio_start;
f5e8ff48
GL
215 gc->ngpio = gpios;
216 gc->label = chip->client->name;
d8f388d8 217 gc->dev = &chip->client->dev;
d72cbed0 218 gc->owner = THIS_MODULE;
77906a54 219 gc->names = chip->names;
9e60fdcf 220}
221
89ea8bbe
MZ
222#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
231static void pca953x_irq_mask(unsigned int irq)
232{
233 struct pca953x_chip *chip = get_irq_chip_data(irq);
234
235 chip->irq_mask &= ~(1 << (irq - chip->irq_base));
236}
237
238static void pca953x_irq_unmask(unsigned int irq)
239{
240 struct pca953x_chip *chip = get_irq_chip_data(irq);
241
242 chip->irq_mask |= 1 << (irq - chip->irq_base);
243}
244
245static void pca953x_irq_bus_lock(unsigned int irq)
246{
247 struct pca953x_chip *chip = get_irq_chip_data(irq);
248
249 mutex_lock(&chip->irq_lock);
250}
251
252static void pca953x_irq_bus_sync_unlock(unsigned int irq)
253{
254 struct pca953x_chip *chip = get_irq_chip_data(irq);
a2cb9aeb
MZ
255 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 }
89ea8bbe
MZ
267
268 mutex_unlock(&chip->irq_lock);
269}
270
271static int pca953x_irq_set_type(unsigned int irq, unsigned int type)
272{
273 struct pca953x_chip *chip = get_irq_chip_data(irq);
274 uint16_t level = irq - chip->irq_base;
275 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",
279 irq, type);
280 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
a2cb9aeb 293 return 0;
89ea8bbe
MZ
294}
295
296static struct irq_chip pca953x_irq_chip = {
297 .name = "pca953x",
298 .mask = pca953x_irq_mask,
299 .unmask = pca953x_irq_unmask,
300 .bus_lock = pca953x_irq_bus_lock,
301 .bus_sync_unlock = pca953x_irq_bus_sync_unlock,
302 .set_type = pca953x_irq_set_type,
303};
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);
348 handle_nested_irq(level + chip->irq_base);
349
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
363 if (pdata->irq_base && (id->driver_data & PCA953X_INT)) {
364 int lvl;
365
366 ret = pca953x_read_reg(chip, PCA953X_INPUT,
367 &chip->irq_stat);
368 if (ret)
369 goto out_failed;
370
371 /*
372 * There is no way to know which GPIO line generated the
373 * interrupt. We have to rely on the previous read for
374 * this purpose.
375 */
376 chip->irq_stat &= chip->reg_direction;
377 chip->irq_base = pdata->irq_base;
378 mutex_init(&chip->irq_lock);
379
380 for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
381 int irq = lvl + chip->irq_base;
382
383 set_irq_chip_data(irq, chip);
384 set_irq_chip_and_handler(irq, &pca953x_irq_chip,
385 handle_edge_irq);
386 set_irq_nested_thread(irq, 1);
387#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,
397 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
398 dev_name(&client->dev), chip);
399 if (ret) {
400 dev_err(&client->dev, "failed to request irq %d\n",
401 client->irq);
402 goto out_failed;
403 }
404
405 chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
406 }
407
408 return 0;
409
410out_failed:
411 chip->irq_base = 0;
412 return ret;
413}
414
415static void pca953x_irq_teardown(struct pca953x_chip *chip)
416{
417 if (chip->irq_base)
418 free_irq(chip->client->irq, chip);
419}
420#else /* CONFIG_GPIO_PCA953X_IRQ */
421static int pca953x_irq_setup(struct pca953x_chip *chip,
422 const struct i2c_device_id *id)
423{
424 struct i2c_client *client = chip->client;
425 struct pca953x_platform_data *pdata = client->dev.platform_data;
426
427 if (pdata->irq_base && (id->driver_data & PCA953X_INT))
428 dev_warn(&client->dev, "interrupt support not compiled in\n");
429
430 return 0;
431}
432
433static void pca953x_irq_teardown(struct pca953x_chip *chip)
434{
435}
436#endif
437
1965d303
NC
438/*
439 * Handlers for alternative sources of platform_data
440 */
441#ifdef CONFIG_OF_GPIO
442/*
443 * Translate OpenFirmware node properties into platform_data
444 */
445static struct pca953x_platform_data *
446pca953x_get_alt_pdata(struct i2c_client *client)
447{
448 struct pca953x_platform_data *pdata;
449 struct device_node *node;
450 const uint16_t *val;
451
61c7a080 452 node = client->dev.of_node;
1965d303
NC
453 if (node == NULL)
454 return NULL;
455
456 pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
457 if (pdata == NULL) {
458 dev_err(&client->dev, "Unable to allocate platform_data\n");
459 return NULL;
460 }
461
462 pdata->gpio_base = -1;
463 val = of_get_property(node, "linux,gpio-base", NULL);
464 if (val) {
465 if (*val < 0)
466 dev_warn(&client->dev,
467 "invalid gpio-base in device tree\n");
468 else
469 pdata->gpio_base = *val;
470 }
471
472 val = of_get_property(node, "polarity", NULL);
473 if (val)
474 pdata->invert = *val;
475
476 return pdata;
477}
478#else
479static struct pca953x_platform_data *
480pca953x_get_alt_pdata(struct i2c_client *client)
481{
482 return NULL;
483}
484#endif
485
d2653e92 486static int __devinit pca953x_probe(struct i2c_client *client,
3760f736 487 const struct i2c_device_id *id)
9e60fdcf 488{
f3dc3630
GL
489 struct pca953x_platform_data *pdata;
490 struct pca953x_chip *chip;
f39e5781 491 int ret;
9e60fdcf 492
1965d303
NC
493 chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
494 if (chip == NULL)
495 return -ENOMEM;
496
9e60fdcf 497 pdata = client->dev.platform_data;
a342d215 498 if (pdata == NULL) {
1965d303
NC
499 pdata = pca953x_get_alt_pdata(client);
500 /*
501 * Unlike normal platform_data, this is allocated
502 * dynamically and must be freed in the driver
503 */
504 chip->dyn_pdata = pdata;
a342d215 505 }
9e60fdcf 506
1965d303
NC
507 if (pdata == NULL) {
508 dev_dbg(&client->dev, "no platform data\n");
509 ret = -EINVAL;
510 goto out_failed;
511 }
9e60fdcf 512
513 chip->client = client;
514
515 chip->gpio_start = pdata->gpio_base;
516
77906a54
DS
517 chip->names = pdata->names;
518
9e60fdcf 519 /* initialize cached registers from their original values.
520 * we can't share this chip with another i2c master.
521 */
89ea8bbe 522 pca953x_setup_gpio(chip, id->driver_data & PCA953X_GPIOS);
f5e8ff48 523
f3dc3630 524 ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
9e60fdcf 525 if (ret)
526 goto out_failed;
527
f3dc3630 528 ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
9e60fdcf 529 if (ret)
530 goto out_failed;
531
532 /* set platform specific polarity inversion */
f3dc3630 533 ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
9e60fdcf 534 if (ret)
535 goto out_failed;
536
89ea8bbe
MZ
537 ret = pca953x_irq_setup(chip, id);
538 if (ret)
539 goto out_failed;
f5e8ff48
GL
540
541 ret = gpiochip_add(&chip->gpio_chip);
9e60fdcf 542 if (ret)
543 goto out_failed;
544
545 if (pdata->setup) {
546 ret = pdata->setup(client, chip->gpio_chip.base,
547 chip->gpio_chip.ngpio, pdata->context);
548 if (ret < 0)
549 dev_warn(&client->dev, "setup failed, %d\n", ret);
550 }
551
552 i2c_set_clientdata(client, chip);
553 return 0;
554
555out_failed:
89ea8bbe 556 pca953x_irq_teardown(chip);
1965d303 557 kfree(chip->dyn_pdata);
9e60fdcf 558 kfree(chip);
559 return ret;
560}
561
f3dc3630 562static int pca953x_remove(struct i2c_client *client)
9e60fdcf 563{
f3dc3630
GL
564 struct pca953x_platform_data *pdata = client->dev.platform_data;
565 struct pca953x_chip *chip = i2c_get_clientdata(client);
9e60fdcf 566 int ret = 0;
567
568 if (pdata->teardown) {
569 ret = pdata->teardown(client, chip->gpio_chip.base,
570 chip->gpio_chip.ngpio, pdata->context);
571 if (ret < 0) {
572 dev_err(&client->dev, "%s failed, %d\n",
573 "teardown", ret);
574 return ret;
575 }
576 }
577
578 ret = gpiochip_remove(&chip->gpio_chip);
579 if (ret) {
580 dev_err(&client->dev, "%s failed, %d\n",
581 "gpiochip_remove()", ret);
582 return ret;
583 }
584
89ea8bbe 585 pca953x_irq_teardown(chip);
1965d303 586 kfree(chip->dyn_pdata);
9e60fdcf 587 kfree(chip);
588 return 0;
589}
590
f3dc3630 591static struct i2c_driver pca953x_driver = {
9e60fdcf 592 .driver = {
f3dc3630 593 .name = "pca953x",
9e60fdcf 594 },
f3dc3630
GL
595 .probe = pca953x_probe,
596 .remove = pca953x_remove,
3760f736 597 .id_table = pca953x_id,
9e60fdcf 598};
599
f3dc3630 600static int __init pca953x_init(void)
9e60fdcf 601{
f3dc3630 602 return i2c_add_driver(&pca953x_driver);
9e60fdcf 603}
2f8d1197
DB
604/* register after i2c postcore initcall and before
605 * subsys initcalls that may rely on these GPIOs
606 */
607subsys_initcall(pca953x_init);
9e60fdcf 608
f3dc3630 609static void __exit pca953x_exit(void)
9e60fdcf 610{
f3dc3630 611 i2c_del_driver(&pca953x_driver);
9e60fdcf 612}
f3dc3630 613module_exit(pca953x_exit);
9e60fdcf 614
615MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
f3dc3630 616MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
9e60fdcf 617MODULE_LICENSE("GPL");