gpio: sch: Consolidate similar algorithms
[deliverable/linux.git] / drivers / gpio / gpio-max732x.c
CommitLineData
bbcd6d54 1/*
c103de24 2 * MAX732x I2C Port Expander with 8/16 I/O
bbcd6d54
EM
3 *
4 * Copyright (C) 2007 Marvell International Ltd.
5 * Copyright (C) 2008 Jack Ren <jack.ren@marvell.com>
6 * Copyright (C) 2008 Eric Miao <eric.miao@marvell.com>
7 *
8 * Derived from drivers/gpio/pca953x.c
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 */
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/gpio.h>
a80a0bbe
MZ
20#include <linux/interrupt.h>
21#include <linux/irq.h>
479f8a57 22#include <linux/irqdomain.h>
bbcd6d54
EM
23#include <linux/i2c.h>
24#include <linux/i2c/max732x.h>
43c4bcf9 25#include <linux/of.h>
bbcd6d54
EM
26
27
28/*
29 * Each port of MAX732x (including MAX7319) falls into one of the
30 * following three types:
31 *
32 * - Push Pull Output
33 * - Input
34 * - Open Drain I/O
35 *
36 * designated by 'O', 'I' and 'P' individually according to MAXIM's
a80a0bbe
MZ
37 * datasheets. 'I' and 'P' ports are interrupt capables, some with
38 * a dedicated interrupt mask.
bbcd6d54
EM
39 *
40 * There are two groups of I/O ports, each group usually includes
41 * up to 8 I/O ports, and is accessed by a specific I2C address:
42 *
43 * - Group A : by I2C address 0b'110xxxx
44 * - Group B : by I2C address 0b'101xxxx
45 *
46 * where 'xxxx' is decided by the connections of pin AD2/AD0. The
47 * address used also affects the initial state of output signals.
48 *
49 * Within each group of ports, there are five known combinations of
50 * I/O ports: 4I4O, 4P4O, 8I, 8P, 8O, see the definitions below for
a80a0bbe
MZ
51 * the detailed organization of these ports. Only Goup A is interrupt
52 * capable.
bbcd6d54
EM
53 *
54 * GPIO numbers start from 'gpio_base + 0' to 'gpio_base + 8/16',
55 * and GPIOs from GROUP_A are numbered before those from GROUP_B
56 * (if there are two groups).
57 *
58 * NOTE: MAX7328/MAX7329 are drop-in replacements for PCF8574/a, so
59 * they are not supported by this driver.
60 */
61
62#define PORT_NONE 0x0 /* '/' No Port */
63#define PORT_OUTPUT 0x1 /* 'O' Push-Pull, Output Only */
64#define PORT_INPUT 0x2 /* 'I' Input Only */
65#define PORT_OPENDRAIN 0x3 /* 'P' Open-Drain, I/O */
66
67#define IO_4I4O 0x5AA5 /* O7 O6 I5 I4 I3 I2 O1 O0 */
68#define IO_4P4O 0x5FF5 /* O7 O6 P5 P4 P3 P2 O1 O0 */
69#define IO_8I 0xAAAA /* I7 I6 I5 I4 I3 I2 I1 I0 */
70#define IO_8P 0xFFFF /* P7 P6 P5 P4 P3 P2 P1 P0 */
71#define IO_8O 0x5555 /* O7 O6 O5 O4 O3 O2 O1 O0 */
72
73#define GROUP_A(x) ((x) & 0xffff) /* I2C Addr: 0b'110xxxx */
74#define GROUP_B(x) ((x) << 16) /* I2C Addr: 0b'101xxxx */
75
a80a0bbe
MZ
76#define INT_NONE 0x0 /* No interrupt capability */
77#define INT_NO_MASK 0x1 /* Has interrupts, no mask */
78#define INT_INDEP_MASK 0x2 /* Has interrupts, independent mask */
79#define INT_MERGED_MASK 0x3 /* Has interrupts, merged mask */
80
81#define INT_CAPS(x) (((uint64_t)(x)) << 32)
82
83enum {
84 MAX7319,
85 MAX7320,
86 MAX7321,
87 MAX7322,
88 MAX7323,
89 MAX7324,
90 MAX7325,
91 MAX7326,
92 MAX7327,
93};
94
95static uint64_t max732x_features[] = {
96 [MAX7319] = GROUP_A(IO_8I) | INT_CAPS(INT_MERGED_MASK),
97 [MAX7320] = GROUP_B(IO_8O),
98 [MAX7321] = GROUP_A(IO_8P) | INT_CAPS(INT_NO_MASK),
99 [MAX7322] = GROUP_A(IO_4I4O) | INT_CAPS(INT_MERGED_MASK),
100 [MAX7323] = GROUP_A(IO_4P4O) | INT_CAPS(INT_INDEP_MASK),
101 [MAX7324] = GROUP_A(IO_8I) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
102 [MAX7325] = GROUP_A(IO_8P) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
103 [MAX7326] = GROUP_A(IO_4I4O) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
104 [MAX7327] = GROUP_A(IO_4P4O) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
105};
106
bbcd6d54 107static const struct i2c_device_id max732x_id[] = {
a80a0bbe
MZ
108 { "max7319", MAX7319 },
109 { "max7320", MAX7320 },
110 { "max7321", MAX7321 },
111 { "max7322", MAX7322 },
112 { "max7323", MAX7323 },
113 { "max7324", MAX7324 },
114 { "max7325", MAX7325 },
115 { "max7326", MAX7326 },
116 { "max7327", MAX7327 },
bbcd6d54
EM
117 { },
118};
119MODULE_DEVICE_TABLE(i2c, max732x_id);
120
43c4bcf9
SP
121#ifdef CONFIG_OF
122static const struct of_device_id max732x_of_table[] = {
123 { .compatible = "maxim,max7319" },
124 { .compatible = "maxim,max7320" },
125 { .compatible = "maxim,max7321" },
126 { .compatible = "maxim,max7322" },
127 { .compatible = "maxim,max7323" },
128 { .compatible = "maxim,max7324" },
129 { .compatible = "maxim,max7325" },
130 { .compatible = "maxim,max7326" },
131 { .compatible = "maxim,max7327" },
132 { }
133};
134MODULE_DEVICE_TABLE(of, max732x_of_table);
135#endif
136
bbcd6d54
EM
137struct max732x_chip {
138 struct gpio_chip gpio_chip;
139
140 struct i2c_client *client; /* "main" client */
141 struct i2c_client *client_dummy;
142 struct i2c_client *client_group_a;
143 struct i2c_client *client_group_b;
144
145 unsigned int mask_group_a;
146 unsigned int dir_input;
147 unsigned int dir_output;
148
149 struct mutex lock;
150 uint8_t reg_out[2];
a80a0bbe
MZ
151
152#ifdef CONFIG_GPIO_MAX732X_IRQ
479f8a57
SP
153 struct irq_domain *irq_domain;
154 struct mutex irq_lock;
155 int irq_base;
156 uint8_t irq_mask;
157 uint8_t irq_mask_cur;
158 uint8_t irq_trig_raise;
159 uint8_t irq_trig_fall;
160 uint8_t irq_features;
a80a0bbe 161#endif
bbcd6d54
EM
162};
163
a80a0bbe 164static int max732x_writeb(struct max732x_chip *chip, int group_a, uint8_t val)
bbcd6d54
EM
165{
166 struct i2c_client *client;
167 int ret;
168
169 client = group_a ? chip->client_group_a : chip->client_group_b;
170 ret = i2c_smbus_write_byte(client, val);
171 if (ret < 0) {
172 dev_err(&client->dev, "failed writing\n");
173 return ret;
174 }
175
176 return 0;
177}
178
a80a0bbe 179static int max732x_readb(struct max732x_chip *chip, int group_a, uint8_t *val)
bbcd6d54
EM
180{
181 struct i2c_client *client;
182 int ret;
183
184 client = group_a ? chip->client_group_a : chip->client_group_b;
185 ret = i2c_smbus_read_byte(client);
186 if (ret < 0) {
187 dev_err(&client->dev, "failed reading\n");
188 return ret;
189 }
190
191 *val = (uint8_t)ret;
192 return 0;
193}
194
195static inline int is_group_a(struct max732x_chip *chip, unsigned off)
196{
197 return (1u << off) & chip->mask_group_a;
198}
199
200static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
201{
202 struct max732x_chip *chip;
203 uint8_t reg_val;
204 int ret;
205
206 chip = container_of(gc, struct max732x_chip, gpio_chip);
207
a80a0bbe 208 ret = max732x_readb(chip, is_group_a(chip, off), &reg_val);
bbcd6d54
EM
209 if (ret < 0)
210 return 0;
211
212 return reg_val & (1u << (off & 0x7));
213}
214
215static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
216{
217 struct max732x_chip *chip;
218 uint8_t reg_out, mask = 1u << (off & 0x7);
219 int ret;
220
221 chip = container_of(gc, struct max732x_chip, gpio_chip);
222
223 mutex_lock(&chip->lock);
224
225 reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
226 reg_out = (val) ? reg_out | mask : reg_out & ~mask;
227
a80a0bbe 228 ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
bbcd6d54
EM
229 if (ret < 0)
230 goto out;
231
232 /* update the shadow register then */
233 if (off > 7)
234 chip->reg_out[1] = reg_out;
235 else
236 chip->reg_out[0] = reg_out;
237out:
238 mutex_unlock(&chip->lock);
239}
240
241static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
242{
243 struct max732x_chip *chip;
244 unsigned int mask = 1u << off;
245
246 chip = container_of(gc, struct max732x_chip, gpio_chip);
247
248 if ((mask & chip->dir_input) == 0) {
249 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
250 chip->client->name, off);
251 return -EACCES;
252 }
253
a13c1868
MZ
254 /*
255 * Open-drain pins must be set to high impedance (which is
256 * equivalent to output-high) to be turned into an input.
257 */
258 if ((mask & chip->dir_output))
259 max732x_gpio_set_value(gc, off, 1);
260
bbcd6d54
EM
261 return 0;
262}
263
264static int max732x_gpio_direction_output(struct gpio_chip *gc,
265 unsigned off, int val)
266{
267 struct max732x_chip *chip;
268 unsigned int mask = 1u << off;
269
270 chip = container_of(gc, struct max732x_chip, gpio_chip);
271
272 if ((mask & chip->dir_output) == 0) {
273 dev_dbg(&chip->client->dev, "%s port %d is input only\n",
274 chip->client->name, off);
275 return -EACCES;
276 }
277
278 max732x_gpio_set_value(gc, off, val);
279 return 0;
280}
281
a80a0bbe
MZ
282#ifdef CONFIG_GPIO_MAX732X_IRQ
283static int max732x_writew(struct max732x_chip *chip, uint16_t val)
284{
285 int ret;
286
287 val = cpu_to_le16(val);
288
289 ret = i2c_master_send(chip->client_group_a, (char *)&val, 2);
290 if (ret < 0) {
291 dev_err(&chip->client_group_a->dev, "failed writing\n");
292 return ret;
293 }
294
295 return 0;
296}
297
298static int max732x_readw(struct max732x_chip *chip, uint16_t *val)
299{
300 int ret;
301
302 ret = i2c_master_recv(chip->client_group_a, (char *)val, 2);
303 if (ret < 0) {
304 dev_err(&chip->client_group_a->dev, "failed reading\n");
305 return ret;
306 }
307
308 *val = le16_to_cpu(*val);
309 return 0;
310}
311
312static void max732x_irq_update_mask(struct max732x_chip *chip)
313{
314 uint16_t msg;
315
316 if (chip->irq_mask == chip->irq_mask_cur)
317 return;
318
319 chip->irq_mask = chip->irq_mask_cur;
320
321 if (chip->irq_features == INT_NO_MASK)
322 return;
323
324 mutex_lock(&chip->lock);
325
326 switch (chip->irq_features) {
327 case INT_INDEP_MASK:
328 msg = (chip->irq_mask << 8) | chip->reg_out[0];
329 max732x_writew(chip, msg);
330 break;
331
332 case INT_MERGED_MASK:
333 msg = chip->irq_mask | chip->reg_out[0];
334 max732x_writeb(chip, 1, (uint8_t)msg);
335 break;
336 }
337
338 mutex_unlock(&chip->lock);
339}
340
341static int max732x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
342{
343 struct max732x_chip *chip;
344
345 chip = container_of(gc, struct max732x_chip, gpio_chip);
479f8a57
SP
346
347 if (chip->irq_domain) {
348 return irq_create_mapping(chip->irq_domain,
349 chip->irq_base + off);
350 } else {
351 return -ENXIO;
352 }
a80a0bbe
MZ
353}
354
fbc4667a 355static void max732x_irq_mask(struct irq_data *d)
a80a0bbe 356{
fbc4667a 357 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
a80a0bbe 358
479f8a57 359 chip->irq_mask_cur &= ~(1 << d->hwirq);
a80a0bbe
MZ
360}
361
fbc4667a 362static void max732x_irq_unmask(struct irq_data *d)
a80a0bbe 363{
fbc4667a 364 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
a80a0bbe 365
479f8a57 366 chip->irq_mask_cur |= 1 << d->hwirq;
a80a0bbe
MZ
367}
368
fbc4667a 369static void max732x_irq_bus_lock(struct irq_data *d)
a80a0bbe 370{
fbc4667a 371 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
a80a0bbe
MZ
372
373 mutex_lock(&chip->irq_lock);
374 chip->irq_mask_cur = chip->irq_mask;
375}
376
fbc4667a 377static void max732x_irq_bus_sync_unlock(struct irq_data *d)
a80a0bbe 378{
fbc4667a 379 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
09afa276
SP
380 uint16_t new_irqs;
381 uint16_t level;
a80a0bbe
MZ
382
383 max732x_irq_update_mask(chip);
09afa276
SP
384
385 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
386 while (new_irqs) {
387 level = __ffs(new_irqs);
388 max732x_gpio_direction_input(&chip->gpio_chip, level);
389 new_irqs &= ~(1 << level);
390 }
391
a80a0bbe
MZ
392 mutex_unlock(&chip->irq_lock);
393}
394
fbc4667a 395static int max732x_irq_set_type(struct irq_data *d, unsigned int type)
a80a0bbe 396{
fbc4667a 397 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
479f8a57 398 uint16_t off = d->hwirq;
a80a0bbe
MZ
399 uint16_t mask = 1 << off;
400
401 if (!(mask & chip->dir_input)) {
402 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
403 chip->client->name, off);
404 return -EACCES;
405 }
406
407 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
408 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
fbc4667a 409 d->irq, type);
a80a0bbe
MZ
410 return -EINVAL;
411 }
412
413 if (type & IRQ_TYPE_EDGE_FALLING)
414 chip->irq_trig_fall |= mask;
415 else
416 chip->irq_trig_fall &= ~mask;
417
418 if (type & IRQ_TYPE_EDGE_RISING)
419 chip->irq_trig_raise |= mask;
420 else
421 chip->irq_trig_raise &= ~mask;
422
09afa276 423 return 0;
a80a0bbe
MZ
424}
425
426static struct irq_chip max732x_irq_chip = {
427 .name = "max732x",
fbc4667a
LB
428 .irq_mask = max732x_irq_mask,
429 .irq_unmask = max732x_irq_unmask,
430 .irq_bus_lock = max732x_irq_bus_lock,
431 .irq_bus_sync_unlock = max732x_irq_bus_sync_unlock,
432 .irq_set_type = max732x_irq_set_type,
a80a0bbe
MZ
433};
434
435static uint8_t max732x_irq_pending(struct max732x_chip *chip)
436{
437 uint8_t cur_stat;
438 uint8_t old_stat;
439 uint8_t trigger;
440 uint8_t pending;
441 uint16_t status;
442 int ret;
443
444 ret = max732x_readw(chip, &status);
445 if (ret)
446 return 0;
447
448 trigger = status >> 8;
449 trigger &= chip->irq_mask;
450
451 if (!trigger)
452 return 0;
453
454 cur_stat = status & 0xFF;
455 cur_stat &= chip->irq_mask;
456
457 old_stat = cur_stat ^ trigger;
458
459 pending = (old_stat & chip->irq_trig_fall) |
460 (cur_stat & chip->irq_trig_raise);
461 pending &= trigger;
462
463 return pending;
464}
465
466static irqreturn_t max732x_irq_handler(int irq, void *devid)
467{
468 struct max732x_chip *chip = devid;
469 uint8_t pending;
470 uint8_t level;
471
472 pending = max732x_irq_pending(chip);
473
474 if (!pending)
475 return IRQ_HANDLED;
476
477 do {
478 level = __ffs(pending);
479f8a57 479 handle_nested_irq(irq_find_mapping(chip->irq_domain, level));
a80a0bbe
MZ
480
481 pending &= ~(1 << level);
482 } while (pending);
483
484 return IRQ_HANDLED;
485}
486
479f8a57
SP
487static int max732x_irq_map(struct irq_domain *h, unsigned int virq,
488 irq_hw_number_t hw)
489{
490 struct max732x_chip *chip = h->host_data;
491
492 if (!(chip->dir_input & (1 << hw))) {
493 dev_err(&chip->client->dev,
494 "Attempt to map output line as IRQ line: %lu\n",
495 hw);
496 return -EPERM;
497 }
498
499 irq_set_chip_data(virq, chip);
500 irq_set_chip_and_handler(virq, &max732x_irq_chip,
501 handle_edge_irq);
502 irq_set_nested_thread(virq, 1);
503#ifdef CONFIG_ARM
504 /* ARM needs us to explicitly flag the IRQ as valid
505 * and will set them noprobe when we do so. */
506 set_irq_flags(virq, IRQF_VALID);
507#else
508 irq_set_noprobe(virq);
509#endif
510
511 return 0;
512}
513
514static struct irq_domain_ops max732x_irq_domain_ops = {
515 .map = max732x_irq_map,
516 .xlate = irq_domain_xlate_twocell,
517};
518
519static void max732x_irq_teardown(struct max732x_chip *chip)
520{
521 if (chip->client->irq && chip->irq_domain)
522 irq_domain_remove(chip->irq_domain);
523}
524
a80a0bbe
MZ
525static int max732x_irq_setup(struct max732x_chip *chip,
526 const struct i2c_device_id *id)
527{
528 struct i2c_client *client = chip->client;
e56aee18 529 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
a80a0bbe
MZ
530 int has_irq = max732x_features[id->driver_data] >> 32;
531 int ret;
532
43c4bcf9
SP
533 if (((pdata && pdata->irq_base) || client->irq)
534 && has_irq != INT_NONE) {
43c4bcf9
SP
535 if (pdata)
536 chip->irq_base = pdata->irq_base;
a80a0bbe
MZ
537 chip->irq_features = has_irq;
538 mutex_init(&chip->irq_lock);
539
479f8a57
SP
540 chip->irq_domain = irq_domain_add_simple(client->dev.of_node,
541 chip->gpio_chip.ngpio, chip->irq_base,
542 &max732x_irq_domain_ops, chip);
543 if (!chip->irq_domain) {
544 dev_err(&client->dev, "Failed to create IRQ domain\n");
545 return -ENOMEM;
a80a0bbe
MZ
546 }
547
548 ret = request_threaded_irq(client->irq,
549 NULL,
550 max732x_irq_handler,
551 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
552 dev_name(&client->dev), chip);
553 if (ret) {
554 dev_err(&client->dev, "failed to request irq %d\n",
555 client->irq);
556 goto out_failed;
557 }
558
559 chip->gpio_chip.to_irq = max732x_gpio_to_irq;
560 }
561
562 return 0;
563
564out_failed:
479f8a57 565 max732x_irq_teardown(chip);
a80a0bbe
MZ
566 return ret;
567}
568
a80a0bbe
MZ
569#else /* CONFIG_GPIO_MAX732X_IRQ */
570static int max732x_irq_setup(struct max732x_chip *chip,
571 const struct i2c_device_id *id)
572{
573 struct i2c_client *client = chip->client;
e56aee18 574 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
a80a0bbe
MZ
575 int has_irq = max732x_features[id->driver_data] >> 32;
576
43c4bcf9 577 if (((pdata && pdata->irq_base) || client->irq) && has_irq != INT_NONE)
a80a0bbe
MZ
578 dev_warn(&client->dev, "interrupt support not compiled in\n");
579
580 return 0;
581}
582
583static void max732x_irq_teardown(struct max732x_chip *chip)
584{
585}
586#endif
587
3836309d 588static int max732x_setup_gpio(struct max732x_chip *chip,
bbcd6d54
EM
589 const struct i2c_device_id *id,
590 unsigned gpio_start)
591{
592 struct gpio_chip *gc = &chip->gpio_chip;
a80a0bbe 593 uint32_t id_data = (uint32_t)max732x_features[id->driver_data];
bbcd6d54
EM
594 int i, port = 0;
595
596 for (i = 0; i < 16; i++, id_data >>= 2) {
597 unsigned int mask = 1 << port;
598
599 switch (id_data & 0x3) {
600 case PORT_OUTPUT:
601 chip->dir_output |= mask;
602 break;
603 case PORT_INPUT:
604 chip->dir_input |= mask;
605 break;
606 case PORT_OPENDRAIN:
607 chip->dir_output |= mask;
608 chip->dir_input |= mask;
609 break;
610 default:
611 continue;
612 }
613
614 if (i < 8)
615 chip->mask_group_a |= mask;
616 port++;
617 }
618
619 if (chip->dir_input)
620 gc->direction_input = max732x_gpio_direction_input;
621 if (chip->dir_output) {
622 gc->direction_output = max732x_gpio_direction_output;
623 gc->set = max732x_gpio_set_value;
624 }
625 gc->get = max732x_gpio_get_value;
9fb1f39e 626 gc->can_sleep = true;
bbcd6d54
EM
627
628 gc->base = gpio_start;
629 gc->ngpio = port;
630 gc->label = chip->client->name;
631 gc->owner = THIS_MODULE;
632
633 return port;
634}
635
43c4bcf9
SP
636static struct max732x_platform_data *of_gpio_max732x(struct device *dev)
637{
638 struct max732x_platform_data *pdata;
639
640 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
641 if (!pdata)
642 return NULL;
643
644 pdata->gpio_base = -1;
645
646 return pdata;
647}
648
3836309d 649static int max732x_probe(struct i2c_client *client,
bbcd6d54
EM
650 const struct i2c_device_id *id)
651{
652 struct max732x_platform_data *pdata;
43c4bcf9 653 struct device_node *node;
bbcd6d54
EM
654 struct max732x_chip *chip;
655 struct i2c_client *c;
656 uint16_t addr_a, addr_b;
657 int ret, nr_port;
658
e56aee18 659 pdata = dev_get_platdata(&client->dev);
43c4bcf9
SP
660 node = client->dev.of_node;
661
662 if (!pdata && node)
663 pdata = of_gpio_max732x(&client->dev);
664
665 if (!pdata) {
a342d215
BD
666 dev_dbg(&client->dev, "no platform data\n");
667 return -EINVAL;
668 }
bbcd6d54 669
43c4bcf9 670 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
bbcd6d54
EM
671 if (chip == NULL)
672 return -ENOMEM;
673 chip->client = client;
674
675 nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
43c4bcf9 676 chip->gpio_chip.dev = &client->dev;
bbcd6d54
EM
677
678 addr_a = (client->addr & 0x0f) | 0x60;
679 addr_b = (client->addr & 0x0f) | 0x50;
680
681 switch (client->addr & 0x70) {
682 case 0x60:
683 chip->client_group_a = client;
5535cb68 684 if (nr_port > 8) {
bbcd6d54
EM
685 c = i2c_new_dummy(client->adapter, addr_b);
686 chip->client_group_b = chip->client_dummy = c;
687 }
688 break;
689 case 0x50:
690 chip->client_group_b = client;
5535cb68 691 if (nr_port > 8) {
bbcd6d54
EM
692 c = i2c_new_dummy(client->adapter, addr_a);
693 chip->client_group_a = chip->client_dummy = c;
694 }
695 break;
696 default:
697 dev_err(&client->dev, "invalid I2C address specified %02x\n",
698 client->addr);
699 ret = -EINVAL;
700 goto out_failed;
f561b423
KK
701 }
702
703 if (nr_port > 8 && !chip->client_dummy) {
704 dev_err(&client->dev,
705 "Failed to allocate second group I2C device\n");
706 ret = -ENODEV;
707 goto out_failed;
bbcd6d54
EM
708 }
709
710 mutex_init(&chip->lock);
711
a80a0bbe 712 max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
5535cb68 713 if (nr_port > 8)
a80a0bbe
MZ
714 max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
715
716 ret = max732x_irq_setup(chip, id);
717 if (ret)
718 goto out_failed;
bbcd6d54
EM
719
720 ret = gpiochip_add(&chip->gpio_chip);
721 if (ret)
722 goto out_failed;
723
43c4bcf9 724 if (pdata && pdata->setup) {
bbcd6d54
EM
725 ret = pdata->setup(client, chip->gpio_chip.base,
726 chip->gpio_chip.ngpio, pdata->context);
727 if (ret < 0)
728 dev_warn(&client->dev, "setup failed, %d\n", ret);
729 }
730
731 i2c_set_clientdata(client, chip);
732 return 0;
733
734out_failed:
c75793d8
KK
735 if (chip->client_dummy)
736 i2c_unregister_device(chip->client_dummy);
a80a0bbe 737 max732x_irq_teardown(chip);
bbcd6d54
EM
738 return ret;
739}
740
206210ce 741static int max732x_remove(struct i2c_client *client)
bbcd6d54 742{
e56aee18 743 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
bbcd6d54 744 struct max732x_chip *chip = i2c_get_clientdata(client);
bbcd6d54 745
43c4bcf9
SP
746 if (pdata && pdata->teardown) {
747 int ret;
748
bbcd6d54
EM
749 ret = pdata->teardown(client, chip->gpio_chip.base,
750 chip->gpio_chip.ngpio, pdata->context);
751 if (ret < 0) {
752 dev_err(&client->dev, "%s failed, %d\n",
753 "teardown", ret);
754 return ret;
755 }
756 }
757
9f5132ae 758 gpiochip_remove(&chip->gpio_chip);
bbcd6d54 759
a80a0bbe
MZ
760 max732x_irq_teardown(chip);
761
bbcd6d54
EM
762 /* unregister any dummy i2c_client */
763 if (chip->client_dummy)
764 i2c_unregister_device(chip->client_dummy);
765
bbcd6d54
EM
766 return 0;
767}
768
769static struct i2c_driver max732x_driver = {
770 .driver = {
43c4bcf9
SP
771 .name = "max732x",
772 .owner = THIS_MODULE,
773 .of_match_table = of_match_ptr(max732x_of_table),
bbcd6d54
EM
774 },
775 .probe = max732x_probe,
8283c4ff 776 .remove = max732x_remove,
bbcd6d54
EM
777 .id_table = max732x_id,
778};
779
780static int __init max732x_init(void)
781{
782 return i2c_add_driver(&max732x_driver);
783}
2f8d1197
DB
784/* register after i2c postcore initcall and before
785 * subsys initcalls that may rely on these GPIOs
786 */
787subsys_initcall(max732x_init);
bbcd6d54
EM
788
789static void __exit max732x_exit(void)
790{
791 i2c_del_driver(&max732x_driver);
792}
793module_exit(max732x_exit);
794
795MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
796MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
797MODULE_LICENSE("GPL");
This page took 0.429136 seconds and 5 git commands to generate.