blob: 745698d977a519d6e5f1fad14a6ede026321bb4f [file] [log] [blame]
Eric Miaobbcd6d52008-07-25 01:46:14 -07001/*
Grant Likelyc103de22011-06-04 18:38:28 -06002 * MAX732x I2C Port Expander with 8/16 I/O
Eric Miaobbcd6d52008-07-25 01:46:14 -07003 *
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>
Marc Zyngiera80a0bb2010-05-26 14:42:16 -070020#include <linux/interrupt.h>
21#include <linux/irq.h>
Semen Protsenko479f8a52015-01-13 15:41:43 +020022#include <linux/irqdomain.h>
Eric Miaobbcd6d52008-07-25 01:46:14 -070023#include <linux/i2c.h>
24#include <linux/i2c/max732x.h>
Semen Protsenko43c4bcf2015-01-13 15:41:42 +020025#include <linux/of.h>
Eric Miaobbcd6d52008-07-25 01:46:14 -070026
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
Marc Zyngiera80a0bb2010-05-26 14:42:16 -070037 * datasheets. 'I' and 'P' ports are interrupt capables, some with
38 * a dedicated interrupt mask.
Eric Miaobbcd6d52008-07-25 01:46:14 -070039 *
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
Marc Zyngiera80a0bb2010-05-26 14:42:16 -070051 * the detailed organization of these ports. Only Goup A is interrupt
52 * capable.
Eric Miaobbcd6d52008-07-25 01:46:14 -070053 *
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
Marc Zyngiera80a0bb2010-05-26 14:42:16 -070076#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
Eric Miaobbcd6d52008-07-25 01:46:14 -0700107static const struct i2c_device_id max732x_id[] = {
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700108 { "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 },
Eric Miaobbcd6d52008-07-25 01:46:14 -0700117 { },
118};
119MODULE_DEVICE_TABLE(i2c, max732x_id);
120
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200121#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
Eric Miaobbcd6d52008-07-25 01:46:14 -0700137struct 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];
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700151
152#ifdef CONFIG_GPIO_MAX732X_IRQ
Semen Protsenko479f8a52015-01-13 15:41:43 +0200153 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;
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700161#endif
Eric Miaobbcd6d52008-07-25 01:46:14 -0700162};
163
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700164static int max732x_writeb(struct max732x_chip *chip, int group_a, uint8_t val)
Eric Miaobbcd6d52008-07-25 01:46:14 -0700165{
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
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700179static int max732x_readb(struct max732x_chip *chip, int group_a, uint8_t *val)
Eric Miaobbcd6d52008-07-25 01:46:14 -0700180{
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
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700208 ret = max732x_readb(chip, is_group_a(chip, off), &reg_val);
Eric Miaobbcd6d52008-07-25 01:46:14 -0700209 if (ret < 0)
210 return 0;
211
212 return reg_val & (1u << (off & 0x7));
213}
214
Mans Rullgard161af6c2015-01-21 17:17:49 +0000215static void max732x_gpio_set_mask(struct gpio_chip *gc, unsigned off, int mask,
216 int val)
Eric Miaobbcd6d52008-07-25 01:46:14 -0700217{
218 struct max732x_chip *chip;
Mans Rullgard161af6c2015-01-21 17:17:49 +0000219 uint8_t reg_out;
Eric Miaobbcd6d52008-07-25 01:46:14 -0700220 int ret;
221
222 chip = container_of(gc, struct max732x_chip, gpio_chip);
223
224 mutex_lock(&chip->lock);
225
226 reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
Mans Rullgard161af6c2015-01-21 17:17:49 +0000227 reg_out = (reg_out & ~mask) | (val & mask);
Eric Miaobbcd6d52008-07-25 01:46:14 -0700228
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700229 ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
Eric Miaobbcd6d52008-07-25 01:46:14 -0700230 if (ret < 0)
231 goto out;
232
233 /* update the shadow register then */
234 if (off > 7)
235 chip->reg_out[1] = reg_out;
236 else
237 chip->reg_out[0] = reg_out;
238out:
239 mutex_unlock(&chip->lock);
240}
241
Mans Rullgard161af6c2015-01-21 17:17:49 +0000242static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
243{
244 unsigned base = off & ~0x7;
245 uint8_t mask = 1u << (off & 0x7);
246
247 max732x_gpio_set_mask(gc, base, mask, val << (off & 0x7));
248}
249
250static void max732x_gpio_set_multiple(struct gpio_chip *gc,
251 unsigned long *mask, unsigned long *bits)
252{
253 unsigned mask_lo = mask[0] & 0xff;
254 unsigned mask_hi = (mask[0] >> 8) & 0xff;
255
256 if (mask_lo)
257 max732x_gpio_set_mask(gc, 0, mask_lo, bits[0] & 0xff);
258 if (mask_hi)
259 max732x_gpio_set_mask(gc, 8, mask_hi, (bits[0] >> 8) & 0xff);
260}
261
Eric Miaobbcd6d52008-07-25 01:46:14 -0700262static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
263{
264 struct max732x_chip *chip;
265 unsigned int mask = 1u << off;
266
267 chip = container_of(gc, struct max732x_chip, gpio_chip);
268
269 if ((mask & chip->dir_input) == 0) {
270 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
271 chip->client->name, off);
272 return -EACCES;
273 }
274
Marc Zyngiera13c1862010-05-26 14:42:21 -0700275 /*
276 * Open-drain pins must be set to high impedance (which is
277 * equivalent to output-high) to be turned into an input.
278 */
279 if ((mask & chip->dir_output))
280 max732x_gpio_set_value(gc, off, 1);
281
Eric Miaobbcd6d52008-07-25 01:46:14 -0700282 return 0;
283}
284
285static int max732x_gpio_direction_output(struct gpio_chip *gc,
286 unsigned off, int val)
287{
288 struct max732x_chip *chip;
289 unsigned int mask = 1u << off;
290
291 chip = container_of(gc, struct max732x_chip, gpio_chip);
292
293 if ((mask & chip->dir_output) == 0) {
294 dev_dbg(&chip->client->dev, "%s port %d is input only\n",
295 chip->client->name, off);
296 return -EACCES;
297 }
298
299 max732x_gpio_set_value(gc, off, val);
300 return 0;
301}
302
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700303#ifdef CONFIG_GPIO_MAX732X_IRQ
304static int max732x_writew(struct max732x_chip *chip, uint16_t val)
305{
306 int ret;
307
308 val = cpu_to_le16(val);
309
310 ret = i2c_master_send(chip->client_group_a, (char *)&val, 2);
311 if (ret < 0) {
312 dev_err(&chip->client_group_a->dev, "failed writing\n");
313 return ret;
314 }
315
316 return 0;
317}
318
319static int max732x_readw(struct max732x_chip *chip, uint16_t *val)
320{
321 int ret;
322
323 ret = i2c_master_recv(chip->client_group_a, (char *)val, 2);
324 if (ret < 0) {
325 dev_err(&chip->client_group_a->dev, "failed reading\n");
326 return ret;
327 }
328
329 *val = le16_to_cpu(*val);
330 return 0;
331}
332
333static void max732x_irq_update_mask(struct max732x_chip *chip)
334{
335 uint16_t msg;
336
337 if (chip->irq_mask == chip->irq_mask_cur)
338 return;
339
340 chip->irq_mask = chip->irq_mask_cur;
341
342 if (chip->irq_features == INT_NO_MASK)
343 return;
344
345 mutex_lock(&chip->lock);
346
347 switch (chip->irq_features) {
348 case INT_INDEP_MASK:
349 msg = (chip->irq_mask << 8) | chip->reg_out[0];
350 max732x_writew(chip, msg);
351 break;
352
353 case INT_MERGED_MASK:
354 msg = chip->irq_mask | chip->reg_out[0];
355 max732x_writeb(chip, 1, (uint8_t)msg);
356 break;
357 }
358
359 mutex_unlock(&chip->lock);
360}
361
362static int max732x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
363{
364 struct max732x_chip *chip;
365
366 chip = container_of(gc, struct max732x_chip, gpio_chip);
Semen Protsenko479f8a52015-01-13 15:41:43 +0200367
368 if (chip->irq_domain) {
369 return irq_create_mapping(chip->irq_domain,
370 chip->irq_base + off);
371 } else {
372 return -ENXIO;
373 }
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700374}
375
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800376static void max732x_irq_mask(struct irq_data *d)
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700377{
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800378 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700379
Semen Protsenko479f8a52015-01-13 15:41:43 +0200380 chip->irq_mask_cur &= ~(1 << d->hwirq);
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700381}
382
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800383static void max732x_irq_unmask(struct irq_data *d)
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700384{
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800385 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700386
Semen Protsenko479f8a52015-01-13 15:41:43 +0200387 chip->irq_mask_cur |= 1 << d->hwirq;
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700388}
389
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800390static void max732x_irq_bus_lock(struct irq_data *d)
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700391{
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800392 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700393
394 mutex_lock(&chip->irq_lock);
395 chip->irq_mask_cur = chip->irq_mask;
396}
397
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800398static void max732x_irq_bus_sync_unlock(struct irq_data *d)
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700399{
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800400 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
Semen Protsenko09afa272015-01-13 15:41:44 +0200401 uint16_t new_irqs;
402 uint16_t level;
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700403
404 max732x_irq_update_mask(chip);
Semen Protsenko09afa272015-01-13 15:41:44 +0200405
406 new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
407 while (new_irqs) {
408 level = __ffs(new_irqs);
409 max732x_gpio_direction_input(&chip->gpio_chip, level);
410 new_irqs &= ~(1 << level);
411 }
412
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700413 mutex_unlock(&chip->irq_lock);
414}
415
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800416static int max732x_irq_set_type(struct irq_data *d, unsigned int type)
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700417{
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800418 struct max732x_chip *chip = irq_data_get_irq_chip_data(d);
Semen Protsenko479f8a52015-01-13 15:41:43 +0200419 uint16_t off = d->hwirq;
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700420 uint16_t mask = 1 << off;
421
422 if (!(mask & chip->dir_input)) {
423 dev_dbg(&chip->client->dev, "%s port %d is output only\n",
424 chip->client->name, off);
425 return -EACCES;
426 }
427
428 if (!(type & IRQ_TYPE_EDGE_BOTH)) {
429 dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800430 d->irq, type);
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700431 return -EINVAL;
432 }
433
434 if (type & IRQ_TYPE_EDGE_FALLING)
435 chip->irq_trig_fall |= mask;
436 else
437 chip->irq_trig_fall &= ~mask;
438
439 if (type & IRQ_TYPE_EDGE_RISING)
440 chip->irq_trig_raise |= mask;
441 else
442 chip->irq_trig_raise &= ~mask;
443
Semen Protsenko09afa272015-01-13 15:41:44 +0200444 return 0;
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700445}
446
447static struct irq_chip max732x_irq_chip = {
448 .name = "max732x",
Lennert Buytenhekfbc46672011-01-12 17:00:14 -0800449 .irq_mask = max732x_irq_mask,
450 .irq_unmask = max732x_irq_unmask,
451 .irq_bus_lock = max732x_irq_bus_lock,
452 .irq_bus_sync_unlock = max732x_irq_bus_sync_unlock,
453 .irq_set_type = max732x_irq_set_type,
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700454};
455
456static uint8_t max732x_irq_pending(struct max732x_chip *chip)
457{
458 uint8_t cur_stat;
459 uint8_t old_stat;
460 uint8_t trigger;
461 uint8_t pending;
462 uint16_t status;
463 int ret;
464
465 ret = max732x_readw(chip, &status);
466 if (ret)
467 return 0;
468
469 trigger = status >> 8;
470 trigger &= chip->irq_mask;
471
472 if (!trigger)
473 return 0;
474
475 cur_stat = status & 0xFF;
476 cur_stat &= chip->irq_mask;
477
478 old_stat = cur_stat ^ trigger;
479
480 pending = (old_stat & chip->irq_trig_fall) |
481 (cur_stat & chip->irq_trig_raise);
482 pending &= trigger;
483
484 return pending;
485}
486
487static irqreturn_t max732x_irq_handler(int irq, void *devid)
488{
489 struct max732x_chip *chip = devid;
490 uint8_t pending;
491 uint8_t level;
492
493 pending = max732x_irq_pending(chip);
494
495 if (!pending)
496 return IRQ_HANDLED;
497
498 do {
499 level = __ffs(pending);
Semen Protsenko479f8a52015-01-13 15:41:43 +0200500 handle_nested_irq(irq_find_mapping(chip->irq_domain, level));
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700501
502 pending &= ~(1 << level);
503 } while (pending);
504
505 return IRQ_HANDLED;
506}
507
Semen Protsenko479f8a52015-01-13 15:41:43 +0200508static int max732x_irq_map(struct irq_domain *h, unsigned int virq,
509 irq_hw_number_t hw)
510{
511 struct max732x_chip *chip = h->host_data;
512
513 if (!(chip->dir_input & (1 << hw))) {
514 dev_err(&chip->client->dev,
515 "Attempt to map output line as IRQ line: %lu\n",
516 hw);
517 return -EPERM;
518 }
519
520 irq_set_chip_data(virq, chip);
521 irq_set_chip_and_handler(virq, &max732x_irq_chip,
522 handle_edge_irq);
523 irq_set_nested_thread(virq, 1);
524#ifdef CONFIG_ARM
525 /* ARM needs us to explicitly flag the IRQ as valid
526 * and will set them noprobe when we do so. */
527 set_irq_flags(virq, IRQF_VALID);
528#else
529 irq_set_noprobe(virq);
530#endif
531
532 return 0;
533}
534
535static struct irq_domain_ops max732x_irq_domain_ops = {
536 .map = max732x_irq_map,
537 .xlate = irq_domain_xlate_twocell,
538};
539
540static void max732x_irq_teardown(struct max732x_chip *chip)
541{
542 if (chip->client->irq && chip->irq_domain)
543 irq_domain_remove(chip->irq_domain);
544}
545
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700546static int max732x_irq_setup(struct max732x_chip *chip,
547 const struct i2c_device_id *id)
548{
549 struct i2c_client *client = chip->client;
Jingoo Hane56aee12013-07-30 17:08:05 +0900550 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700551 int has_irq = max732x_features[id->driver_data] >> 32;
552 int ret;
553
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200554 if (((pdata && pdata->irq_base) || client->irq)
555 && has_irq != INT_NONE) {
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200556 if (pdata)
557 chip->irq_base = pdata->irq_base;
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700558 chip->irq_features = has_irq;
559 mutex_init(&chip->irq_lock);
560
Semen Protsenko479f8a52015-01-13 15:41:43 +0200561 chip->irq_domain = irq_domain_add_simple(client->dev.of_node,
562 chip->gpio_chip.ngpio, chip->irq_base,
563 &max732x_irq_domain_ops, chip);
564 if (!chip->irq_domain) {
565 dev_err(&client->dev, "Failed to create IRQ domain\n");
566 return -ENOMEM;
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700567 }
568
569 ret = request_threaded_irq(client->irq,
570 NULL,
571 max732x_irq_handler,
572 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
573 dev_name(&client->dev), chip);
574 if (ret) {
575 dev_err(&client->dev, "failed to request irq %d\n",
576 client->irq);
577 goto out_failed;
578 }
579
580 chip->gpio_chip.to_irq = max732x_gpio_to_irq;
581 }
582
583 return 0;
584
585out_failed:
Semen Protsenko479f8a52015-01-13 15:41:43 +0200586 max732x_irq_teardown(chip);
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700587 return ret;
588}
589
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700590#else /* CONFIG_GPIO_MAX732X_IRQ */
591static int max732x_irq_setup(struct max732x_chip *chip,
592 const struct i2c_device_id *id)
593{
594 struct i2c_client *client = chip->client;
Jingoo Hane56aee12013-07-30 17:08:05 +0900595 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700596 int has_irq = max732x_features[id->driver_data] >> 32;
597
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200598 if (((pdata && pdata->irq_base) || client->irq) && has_irq != INT_NONE)
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700599 dev_warn(&client->dev, "interrupt support not compiled in\n");
600
601 return 0;
602}
603
604static void max732x_irq_teardown(struct max732x_chip *chip)
605{
606}
607#endif
608
Bill Pemberton38363092012-11-19 13:22:34 -0500609static int max732x_setup_gpio(struct max732x_chip *chip,
Eric Miaobbcd6d52008-07-25 01:46:14 -0700610 const struct i2c_device_id *id,
611 unsigned gpio_start)
612{
613 struct gpio_chip *gc = &chip->gpio_chip;
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700614 uint32_t id_data = (uint32_t)max732x_features[id->driver_data];
Eric Miaobbcd6d52008-07-25 01:46:14 -0700615 int i, port = 0;
616
617 for (i = 0; i < 16; i++, id_data >>= 2) {
618 unsigned int mask = 1 << port;
619
620 switch (id_data & 0x3) {
621 case PORT_OUTPUT:
622 chip->dir_output |= mask;
623 break;
624 case PORT_INPUT:
625 chip->dir_input |= mask;
626 break;
627 case PORT_OPENDRAIN:
628 chip->dir_output |= mask;
629 chip->dir_input |= mask;
630 break;
631 default:
632 continue;
633 }
634
635 if (i < 8)
636 chip->mask_group_a |= mask;
637 port++;
638 }
639
640 if (chip->dir_input)
641 gc->direction_input = max732x_gpio_direction_input;
642 if (chip->dir_output) {
643 gc->direction_output = max732x_gpio_direction_output;
644 gc->set = max732x_gpio_set_value;
Mans Rullgard161af6c2015-01-21 17:17:49 +0000645 gc->set_multiple = max732x_gpio_set_multiple;
Eric Miaobbcd6d52008-07-25 01:46:14 -0700646 }
647 gc->get = max732x_gpio_get_value;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100648 gc->can_sleep = true;
Eric Miaobbcd6d52008-07-25 01:46:14 -0700649
650 gc->base = gpio_start;
651 gc->ngpio = port;
652 gc->label = chip->client->name;
653 gc->owner = THIS_MODULE;
654
655 return port;
656}
657
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200658static struct max732x_platform_data *of_gpio_max732x(struct device *dev)
659{
660 struct max732x_platform_data *pdata;
661
662 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
663 if (!pdata)
664 return NULL;
665
666 pdata->gpio_base = -1;
667
668 return pdata;
669}
670
Bill Pemberton38363092012-11-19 13:22:34 -0500671static int max732x_probe(struct i2c_client *client,
Eric Miaobbcd6d52008-07-25 01:46:14 -0700672 const struct i2c_device_id *id)
673{
674 struct max732x_platform_data *pdata;
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200675 struct device_node *node;
Eric Miaobbcd6d52008-07-25 01:46:14 -0700676 struct max732x_chip *chip;
677 struct i2c_client *c;
678 uint16_t addr_a, addr_b;
679 int ret, nr_port;
680
Jingoo Hane56aee12013-07-30 17:08:05 +0900681 pdata = dev_get_platdata(&client->dev);
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200682 node = client->dev.of_node;
683
684 if (!pdata && node)
685 pdata = of_gpio_max732x(&client->dev);
686
687 if (!pdata) {
Ben Dooksa342d212009-01-15 13:50:45 -0800688 dev_dbg(&client->dev, "no platform data\n");
689 return -EINVAL;
690 }
Eric Miaobbcd6d52008-07-25 01:46:14 -0700691
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200692 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
Eric Miaobbcd6d52008-07-25 01:46:14 -0700693 if (chip == NULL)
694 return -ENOMEM;
695 chip->client = client;
696
697 nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200698 chip->gpio_chip.dev = &client->dev;
Eric Miaobbcd6d52008-07-25 01:46:14 -0700699
700 addr_a = (client->addr & 0x0f) | 0x60;
701 addr_b = (client->addr & 0x0f) | 0x50;
702
703 switch (client->addr & 0x70) {
704 case 0x60:
705 chip->client_group_a = client;
Axel Lin5535cb62010-05-26 14:42:20 -0700706 if (nr_port > 8) {
Eric Miaobbcd6d52008-07-25 01:46:14 -0700707 c = i2c_new_dummy(client->adapter, addr_b);
708 chip->client_group_b = chip->client_dummy = c;
709 }
710 break;
711 case 0x50:
712 chip->client_group_b = client;
Axel Lin5535cb62010-05-26 14:42:20 -0700713 if (nr_port > 8) {
Eric Miaobbcd6d52008-07-25 01:46:14 -0700714 c = i2c_new_dummy(client->adapter, addr_a);
715 chip->client_group_a = chip->client_dummy = c;
716 }
717 break;
718 default:
719 dev_err(&client->dev, "invalid I2C address specified %02x\n",
720 client->addr);
721 ret = -EINVAL;
722 goto out_failed;
723 }
724
Krzysztof Kozlowskif561b422014-03-06 10:31:16 +0100725 if (nr_port > 8 && !chip->client_dummy) {
726 dev_err(&client->dev,
727 "Failed to allocate second group I2C device\n");
728 ret = -ENODEV;
729 goto out_failed;
730 }
731
Eric Miaobbcd6d52008-07-25 01:46:14 -0700732 mutex_init(&chip->lock);
733
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700734 max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
Axel Lin5535cb62010-05-26 14:42:20 -0700735 if (nr_port > 8)
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700736 max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
737
738 ret = max732x_irq_setup(chip, id);
739 if (ret)
740 goto out_failed;
Eric Miaobbcd6d52008-07-25 01:46:14 -0700741
742 ret = gpiochip_add(&chip->gpio_chip);
743 if (ret)
744 goto out_failed;
745
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200746 if (pdata && pdata->setup) {
Eric Miaobbcd6d52008-07-25 01:46:14 -0700747 ret = pdata->setup(client, chip->gpio_chip.base,
748 chip->gpio_chip.ngpio, pdata->context);
749 if (ret < 0)
750 dev_warn(&client->dev, "setup failed, %d\n", ret);
751 }
752
753 i2c_set_clientdata(client, chip);
754 return 0;
755
756out_failed:
Krzysztof Kozlowskic75793d2014-03-06 10:31:15 +0100757 if (chip->client_dummy)
758 i2c_unregister_device(chip->client_dummy);
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700759 max732x_irq_teardown(chip);
Eric Miaobbcd6d52008-07-25 01:46:14 -0700760 return ret;
761}
762
Bill Pemberton206210c2012-11-19 13:25:50 -0500763static int max732x_remove(struct i2c_client *client)
Eric Miaobbcd6d52008-07-25 01:46:14 -0700764{
Jingoo Hane56aee12013-07-30 17:08:05 +0900765 struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
Eric Miaobbcd6d52008-07-25 01:46:14 -0700766 struct max732x_chip *chip = i2c_get_clientdata(client);
Eric Miaobbcd6d52008-07-25 01:46:14 -0700767
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200768 if (pdata && pdata->teardown) {
769 int ret;
770
Eric Miaobbcd6d52008-07-25 01:46:14 -0700771 ret = pdata->teardown(client, chip->gpio_chip.base,
772 chip->gpio_chip.ngpio, pdata->context);
773 if (ret < 0) {
774 dev_err(&client->dev, "%s failed, %d\n",
775 "teardown", ret);
776 return ret;
777 }
778 }
779
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200780 gpiochip_remove(&chip->gpio_chip);
Eric Miaobbcd6d52008-07-25 01:46:14 -0700781
Marc Zyngiera80a0bb2010-05-26 14:42:16 -0700782 max732x_irq_teardown(chip);
783
Eric Miaobbcd6d52008-07-25 01:46:14 -0700784 /* unregister any dummy i2c_client */
785 if (chip->client_dummy)
786 i2c_unregister_device(chip->client_dummy);
787
Eric Miaobbcd6d52008-07-25 01:46:14 -0700788 return 0;
789}
790
791static struct i2c_driver max732x_driver = {
792 .driver = {
Semen Protsenko43c4bcf2015-01-13 15:41:42 +0200793 .name = "max732x",
794 .owner = THIS_MODULE,
795 .of_match_table = of_match_ptr(max732x_of_table),
Eric Miaobbcd6d52008-07-25 01:46:14 -0700796 },
797 .probe = max732x_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500798 .remove = max732x_remove,
Eric Miaobbcd6d52008-07-25 01:46:14 -0700799 .id_table = max732x_id,
800};
801
802static int __init max732x_init(void)
803{
804 return i2c_add_driver(&max732x_driver);
805}
David Brownell2f8d1192008-10-15 22:03:13 -0700806/* register after i2c postcore initcall and before
807 * subsys initcalls that may rely on these GPIOs
808 */
809subsys_initcall(max732x_init);
Eric Miaobbcd6d52008-07-25 01:46:14 -0700810
811static void __exit max732x_exit(void)
812{
813 i2c_del_driver(&max732x_driver);
814}
815module_exit(max732x_exit);
816
817MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
818MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
819MODULE_LICENSE("GPL");