gpio-max732x.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * MAX732x I2C Port Expander with 8/16 I/O
  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. * Copyright (C) 2015 Linus Walleij <linus.walleij@linaro.org>
  8. *
  9. * Derived from drivers/gpio/pca953x.c
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  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/driver.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/i2c.h>
  22. #include <linux/i2c/max732x.h>
  23. #include <linux/of.h>
  24. /*
  25. * Each port of MAX732x (including MAX7319) falls into one of the
  26. * following three types:
  27. *
  28. * - Push Pull Output
  29. * - Input
  30. * - Open Drain I/O
  31. *
  32. * designated by 'O', 'I' and 'P' individually according to MAXIM's
  33. * datasheets. 'I' and 'P' ports are interrupt capables, some with
  34. * a dedicated interrupt mask.
  35. *
  36. * There are two groups of I/O ports, each group usually includes
  37. * up to 8 I/O ports, and is accessed by a specific I2C address:
  38. *
  39. * - Group A : by I2C address 0b'110xxxx
  40. * - Group B : by I2C address 0b'101xxxx
  41. *
  42. * where 'xxxx' is decided by the connections of pin AD2/AD0. The
  43. * address used also affects the initial state of output signals.
  44. *
  45. * Within each group of ports, there are five known combinations of
  46. * I/O ports: 4I4O, 4P4O, 8I, 8P, 8O, see the definitions below for
  47. * the detailed organization of these ports. Only Goup A is interrupt
  48. * capable.
  49. *
  50. * GPIO numbers start from 'gpio_base + 0' to 'gpio_base + 8/16',
  51. * and GPIOs from GROUP_A are numbered before those from GROUP_B
  52. * (if there are two groups).
  53. *
  54. * NOTE: MAX7328/MAX7329 are drop-in replacements for PCF8574/a, so
  55. * they are not supported by this driver.
  56. */
  57. #define PORT_NONE 0x0 /* '/' No Port */
  58. #define PORT_OUTPUT 0x1 /* 'O' Push-Pull, Output Only */
  59. #define PORT_INPUT 0x2 /* 'I' Input Only */
  60. #define PORT_OPENDRAIN 0x3 /* 'P' Open-Drain, I/O */
  61. #define IO_4I4O 0x5AA5 /* O7 O6 I5 I4 I3 I2 O1 O0 */
  62. #define IO_4P4O 0x5FF5 /* O7 O6 P5 P4 P3 P2 O1 O0 */
  63. #define IO_8I 0xAAAA /* I7 I6 I5 I4 I3 I2 I1 I0 */
  64. #define IO_8P 0xFFFF /* P7 P6 P5 P4 P3 P2 P1 P0 */
  65. #define IO_8O 0x5555 /* O7 O6 O5 O4 O3 O2 O1 O0 */
  66. #define GROUP_A(x) ((x) & 0xffff) /* I2C Addr: 0b'110xxxx */
  67. #define GROUP_B(x) ((x) << 16) /* I2C Addr: 0b'101xxxx */
  68. #define INT_NONE 0x0 /* No interrupt capability */
  69. #define INT_NO_MASK 0x1 /* Has interrupts, no mask */
  70. #define INT_INDEP_MASK 0x2 /* Has interrupts, independent mask */
  71. #define INT_MERGED_MASK 0x3 /* Has interrupts, merged mask */
  72. #define INT_CAPS(x) (((uint64_t)(x)) << 32)
  73. enum {
  74. MAX7319,
  75. MAX7320,
  76. MAX7321,
  77. MAX7322,
  78. MAX7323,
  79. MAX7324,
  80. MAX7325,
  81. MAX7326,
  82. MAX7327,
  83. };
  84. static uint64_t max732x_features[] = {
  85. [MAX7319] = GROUP_A(IO_8I) | INT_CAPS(INT_MERGED_MASK),
  86. [MAX7320] = GROUP_B(IO_8O),
  87. [MAX7321] = GROUP_A(IO_8P) | INT_CAPS(INT_NO_MASK),
  88. [MAX7322] = GROUP_A(IO_4I4O) | INT_CAPS(INT_MERGED_MASK),
  89. [MAX7323] = GROUP_A(IO_4P4O) | INT_CAPS(INT_INDEP_MASK),
  90. [MAX7324] = GROUP_A(IO_8I) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
  91. [MAX7325] = GROUP_A(IO_8P) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
  92. [MAX7326] = GROUP_A(IO_4I4O) | GROUP_B(IO_8O) | INT_CAPS(INT_MERGED_MASK),
  93. [MAX7327] = GROUP_A(IO_4P4O) | GROUP_B(IO_8O) | INT_CAPS(INT_NO_MASK),
  94. };
  95. static const struct i2c_device_id max732x_id[] = {
  96. { "max7319", MAX7319 },
  97. { "max7320", MAX7320 },
  98. { "max7321", MAX7321 },
  99. { "max7322", MAX7322 },
  100. { "max7323", MAX7323 },
  101. { "max7324", MAX7324 },
  102. { "max7325", MAX7325 },
  103. { "max7326", MAX7326 },
  104. { "max7327", MAX7327 },
  105. { },
  106. };
  107. MODULE_DEVICE_TABLE(i2c, max732x_id);
  108. #ifdef CONFIG_OF
  109. static const struct of_device_id max732x_of_table[] = {
  110. { .compatible = "maxim,max7319" },
  111. { .compatible = "maxim,max7320" },
  112. { .compatible = "maxim,max7321" },
  113. { .compatible = "maxim,max7322" },
  114. { .compatible = "maxim,max7323" },
  115. { .compatible = "maxim,max7324" },
  116. { .compatible = "maxim,max7325" },
  117. { .compatible = "maxim,max7326" },
  118. { .compatible = "maxim,max7327" },
  119. { }
  120. };
  121. MODULE_DEVICE_TABLE(of, max732x_of_table);
  122. #endif
  123. struct max732x_chip {
  124. struct gpio_chip gpio_chip;
  125. struct i2c_client *client; /* "main" client */
  126. struct i2c_client *client_dummy;
  127. struct i2c_client *client_group_a;
  128. struct i2c_client *client_group_b;
  129. unsigned int mask_group_a;
  130. unsigned int dir_input;
  131. unsigned int dir_output;
  132. struct mutex lock;
  133. uint8_t reg_out[2];
  134. #ifdef CONFIG_GPIO_MAX732X_IRQ
  135. struct mutex irq_lock;
  136. uint8_t irq_mask;
  137. uint8_t irq_mask_cur;
  138. uint8_t irq_trig_raise;
  139. uint8_t irq_trig_fall;
  140. uint8_t irq_features;
  141. #endif
  142. };
  143. static inline struct max732x_chip *to_max732x(struct gpio_chip *gc)
  144. {
  145. return container_of(gc, struct max732x_chip, gpio_chip);
  146. }
  147. static int max732x_writeb(struct max732x_chip *chip, int group_a, uint8_t val)
  148. {
  149. struct i2c_client *client;
  150. int ret;
  151. client = group_a ? chip->client_group_a : chip->client_group_b;
  152. ret = i2c_smbus_write_byte(client, val);
  153. if (ret < 0) {
  154. dev_err(&client->dev, "failed writing\n");
  155. return ret;
  156. }
  157. return 0;
  158. }
  159. static int max732x_readb(struct max732x_chip *chip, int group_a, uint8_t *val)
  160. {
  161. struct i2c_client *client;
  162. int ret;
  163. client = group_a ? chip->client_group_a : chip->client_group_b;
  164. ret = i2c_smbus_read_byte(client);
  165. if (ret < 0) {
  166. dev_err(&client->dev, "failed reading\n");
  167. return ret;
  168. }
  169. *val = (uint8_t)ret;
  170. return 0;
  171. }
  172. static inline int is_group_a(struct max732x_chip *chip, unsigned off)
  173. {
  174. return (1u << off) & chip->mask_group_a;
  175. }
  176. static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
  177. {
  178. struct max732x_chip *chip = to_max732x(gc);
  179. uint8_t reg_val;
  180. int ret;
  181. ret = max732x_readb(chip, is_group_a(chip, off), &reg_val);
  182. if (ret < 0)
  183. return 0;
  184. return reg_val & (1u << (off & 0x7));
  185. }
  186. static void max732x_gpio_set_mask(struct gpio_chip *gc, unsigned off, int mask,
  187. int val)
  188. {
  189. struct max732x_chip *chip = to_max732x(gc);
  190. uint8_t reg_out;
  191. int ret;
  192. mutex_lock(&chip->lock);
  193. reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
  194. reg_out = (reg_out & ~mask) | (val & mask);
  195. ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
  196. if (ret < 0)
  197. goto out;
  198. /* update the shadow register then */
  199. if (off > 7)
  200. chip->reg_out[1] = reg_out;
  201. else
  202. chip->reg_out[0] = reg_out;
  203. out:
  204. mutex_unlock(&chip->lock);
  205. }
  206. static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  207. {
  208. unsigned base = off & ~0x7;
  209. uint8_t mask = 1u << (off & 0x7);
  210. max732x_gpio_set_mask(gc, base, mask, val << (off & 0x7));
  211. }
  212. static void max732x_gpio_set_multiple(struct gpio_chip *gc,
  213. unsigned long *mask, unsigned long *bits)
  214. {
  215. unsigned mask_lo = mask[0] & 0xff;
  216. unsigned mask_hi = (mask[0] >> 8) & 0xff;
  217. if (mask_lo)
  218. max732x_gpio_set_mask(gc, 0, mask_lo, bits[0] & 0xff);
  219. if (mask_hi)
  220. max732x_gpio_set_mask(gc, 8, mask_hi, (bits[0] >> 8) & 0xff);
  221. }
  222. static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  223. {
  224. struct max732x_chip *chip = to_max732x(gc);
  225. unsigned int mask = 1u << off;
  226. if ((mask & chip->dir_input) == 0) {
  227. dev_dbg(&chip->client->dev, "%s port %d is output only\n",
  228. chip->client->name, off);
  229. return -EACCES;
  230. }
  231. /*
  232. * Open-drain pins must be set to high impedance (which is
  233. * equivalent to output-high) to be turned into an input.
  234. */
  235. if ((mask & chip->dir_output))
  236. max732x_gpio_set_value(gc, off, 1);
  237. return 0;
  238. }
  239. static int max732x_gpio_direction_output(struct gpio_chip *gc,
  240. unsigned off, int val)
  241. {
  242. struct max732x_chip *chip = to_max732x(gc);
  243. unsigned int mask = 1u << off;
  244. if ((mask & chip->dir_output) == 0) {
  245. dev_dbg(&chip->client->dev, "%s port %d is input only\n",
  246. chip->client->name, off);
  247. return -EACCES;
  248. }
  249. max732x_gpio_set_value(gc, off, val);
  250. return 0;
  251. }
  252. #ifdef CONFIG_GPIO_MAX732X_IRQ
  253. static int max732x_writew(struct max732x_chip *chip, uint16_t val)
  254. {
  255. int ret;
  256. val = cpu_to_le16(val);
  257. ret = i2c_master_send(chip->client_group_a, (char *)&val, 2);
  258. if (ret < 0) {
  259. dev_err(&chip->client_group_a->dev, "failed writing\n");
  260. return ret;
  261. }
  262. return 0;
  263. }
  264. static int max732x_readw(struct max732x_chip *chip, uint16_t *val)
  265. {
  266. int ret;
  267. ret = i2c_master_recv(chip->client_group_a, (char *)val, 2);
  268. if (ret < 0) {
  269. dev_err(&chip->client_group_a->dev, "failed reading\n");
  270. return ret;
  271. }
  272. *val = le16_to_cpu(*val);
  273. return 0;
  274. }
  275. static void max732x_irq_update_mask(struct max732x_chip *chip)
  276. {
  277. uint16_t msg;
  278. if (chip->irq_mask == chip->irq_mask_cur)
  279. return;
  280. chip->irq_mask = chip->irq_mask_cur;
  281. if (chip->irq_features == INT_NO_MASK)
  282. return;
  283. mutex_lock(&chip->lock);
  284. switch (chip->irq_features) {
  285. case INT_INDEP_MASK:
  286. msg = (chip->irq_mask << 8) | chip->reg_out[0];
  287. max732x_writew(chip, msg);
  288. break;
  289. case INT_MERGED_MASK:
  290. msg = chip->irq_mask | chip->reg_out[0];
  291. max732x_writeb(chip, 1, (uint8_t)msg);
  292. break;
  293. }
  294. mutex_unlock(&chip->lock);
  295. }
  296. static void max732x_irq_mask(struct irq_data *d)
  297. {
  298. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  299. struct max732x_chip *chip = to_max732x(gc);
  300. chip->irq_mask_cur &= ~(1 << d->hwirq);
  301. }
  302. static void max732x_irq_unmask(struct irq_data *d)
  303. {
  304. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  305. struct max732x_chip *chip = to_max732x(gc);
  306. chip->irq_mask_cur |= 1 << d->hwirq;
  307. }
  308. static void max732x_irq_bus_lock(struct irq_data *d)
  309. {
  310. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  311. struct max732x_chip *chip = to_max732x(gc);
  312. mutex_lock(&chip->irq_lock);
  313. chip->irq_mask_cur = chip->irq_mask;
  314. }
  315. static void max732x_irq_bus_sync_unlock(struct irq_data *d)
  316. {
  317. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  318. struct max732x_chip *chip = to_max732x(gc);
  319. uint16_t new_irqs;
  320. uint16_t level;
  321. max732x_irq_update_mask(chip);
  322. new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
  323. while (new_irqs) {
  324. level = __ffs(new_irqs);
  325. max732x_gpio_direction_input(&chip->gpio_chip, level);
  326. new_irqs &= ~(1 << level);
  327. }
  328. mutex_unlock(&chip->irq_lock);
  329. }
  330. static int max732x_irq_set_type(struct irq_data *d, unsigned int type)
  331. {
  332. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  333. struct max732x_chip *chip = to_max732x(gc);
  334. uint16_t off = d->hwirq;
  335. uint16_t mask = 1 << off;
  336. if (!(mask & chip->dir_input)) {
  337. dev_dbg(&chip->client->dev, "%s port %d is output only\n",
  338. chip->client->name, off);
  339. return -EACCES;
  340. }
  341. if (!(type & IRQ_TYPE_EDGE_BOTH)) {
  342. dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
  343. d->irq, type);
  344. return -EINVAL;
  345. }
  346. if (type & IRQ_TYPE_EDGE_FALLING)
  347. chip->irq_trig_fall |= mask;
  348. else
  349. chip->irq_trig_fall &= ~mask;
  350. if (type & IRQ_TYPE_EDGE_RISING)
  351. chip->irq_trig_raise |= mask;
  352. else
  353. chip->irq_trig_raise &= ~mask;
  354. return 0;
  355. }
  356. static int max732x_irq_set_wake(struct irq_data *data, unsigned int on)
  357. {
  358. struct max732x_chip *chip = irq_data_get_irq_chip_data(data);
  359. irq_set_irq_wake(chip->client->irq, on);
  360. return 0;
  361. }
  362. static struct irq_chip max732x_irq_chip = {
  363. .name = "max732x",
  364. .irq_mask = max732x_irq_mask,
  365. .irq_unmask = max732x_irq_unmask,
  366. .irq_bus_lock = max732x_irq_bus_lock,
  367. .irq_bus_sync_unlock = max732x_irq_bus_sync_unlock,
  368. .irq_set_type = max732x_irq_set_type,
  369. .irq_set_wake = max732x_irq_set_wake,
  370. };
  371. static uint8_t max732x_irq_pending(struct max732x_chip *chip)
  372. {
  373. uint8_t cur_stat;
  374. uint8_t old_stat;
  375. uint8_t trigger;
  376. uint8_t pending;
  377. uint16_t status;
  378. int ret;
  379. ret = max732x_readw(chip, &status);
  380. if (ret)
  381. return 0;
  382. trigger = status >> 8;
  383. trigger &= chip->irq_mask;
  384. if (!trigger)
  385. return 0;
  386. cur_stat = status & 0xFF;
  387. cur_stat &= chip->irq_mask;
  388. old_stat = cur_stat ^ trigger;
  389. pending = (old_stat & chip->irq_trig_fall) |
  390. (cur_stat & chip->irq_trig_raise);
  391. pending &= trigger;
  392. return pending;
  393. }
  394. static irqreturn_t max732x_irq_handler(int irq, void *devid)
  395. {
  396. struct max732x_chip *chip = devid;
  397. uint8_t pending;
  398. uint8_t level;
  399. pending = max732x_irq_pending(chip);
  400. if (!pending)
  401. return IRQ_HANDLED;
  402. do {
  403. level = __ffs(pending);
  404. handle_nested_irq(irq_find_mapping(chip->gpio_chip.irqdomain,
  405. level));
  406. pending &= ~(1 << level);
  407. } while (pending);
  408. return IRQ_HANDLED;
  409. }
  410. static int max732x_irq_setup(struct max732x_chip *chip,
  411. const struct i2c_device_id *id)
  412. {
  413. struct i2c_client *client = chip->client;
  414. struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
  415. int has_irq = max732x_features[id->driver_data] >> 32;
  416. int irq_base = 0;
  417. int ret;
  418. if (((pdata && pdata->irq_base) || client->irq)
  419. && has_irq != INT_NONE) {
  420. if (pdata)
  421. irq_base = pdata->irq_base;
  422. chip->irq_features = has_irq;
  423. mutex_init(&chip->irq_lock);
  424. ret = devm_request_threaded_irq(&client->dev, client->irq,
  425. NULL, max732x_irq_handler, IRQF_ONESHOT |
  426. IRQF_TRIGGER_FALLING | IRQF_SHARED,
  427. dev_name(&client->dev), chip);
  428. if (ret) {
  429. dev_err(&client->dev, "failed to request irq %d\n",
  430. client->irq);
  431. return ret;
  432. }
  433. ret = gpiochip_irqchip_add(&chip->gpio_chip,
  434. &max732x_irq_chip,
  435. irq_base,
  436. handle_simple_irq,
  437. IRQ_TYPE_NONE);
  438. if (ret) {
  439. dev_err(&client->dev,
  440. "could not connect irqchip to gpiochip\n");
  441. return ret;
  442. }
  443. gpiochip_set_chained_irqchip(&chip->gpio_chip,
  444. &max732x_irq_chip,
  445. client->irq,
  446. NULL);
  447. }
  448. return 0;
  449. }
  450. #else /* CONFIG_GPIO_MAX732X_IRQ */
  451. static int max732x_irq_setup(struct max732x_chip *chip,
  452. const struct i2c_device_id *id)
  453. {
  454. struct i2c_client *client = chip->client;
  455. struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
  456. int has_irq = max732x_features[id->driver_data] >> 32;
  457. if (((pdata && pdata->irq_base) || client->irq) && has_irq != INT_NONE)
  458. dev_warn(&client->dev, "interrupt support not compiled in\n");
  459. return 0;
  460. }
  461. #endif
  462. static int max732x_setup_gpio(struct max732x_chip *chip,
  463. const struct i2c_device_id *id,
  464. unsigned gpio_start)
  465. {
  466. struct gpio_chip *gc = &chip->gpio_chip;
  467. uint32_t id_data = (uint32_t)max732x_features[id->driver_data];
  468. int i, port = 0;
  469. for (i = 0; i < 16; i++, id_data >>= 2) {
  470. unsigned int mask = 1 << port;
  471. switch (id_data & 0x3) {
  472. case PORT_OUTPUT:
  473. chip->dir_output |= mask;
  474. break;
  475. case PORT_INPUT:
  476. chip->dir_input |= mask;
  477. break;
  478. case PORT_OPENDRAIN:
  479. chip->dir_output |= mask;
  480. chip->dir_input |= mask;
  481. break;
  482. default:
  483. continue;
  484. }
  485. if (i < 8)
  486. chip->mask_group_a |= mask;
  487. port++;
  488. }
  489. if (chip->dir_input)
  490. gc->direction_input = max732x_gpio_direction_input;
  491. if (chip->dir_output) {
  492. gc->direction_output = max732x_gpio_direction_output;
  493. gc->set = max732x_gpio_set_value;
  494. gc->set_multiple = max732x_gpio_set_multiple;
  495. }
  496. gc->get = max732x_gpio_get_value;
  497. gc->can_sleep = true;
  498. gc->base = gpio_start;
  499. gc->ngpio = port;
  500. gc->label = chip->client->name;
  501. gc->dev = &chip->client->dev;
  502. gc->owner = THIS_MODULE;
  503. return port;
  504. }
  505. static struct max732x_platform_data *of_gpio_max732x(struct device *dev)
  506. {
  507. struct max732x_platform_data *pdata;
  508. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  509. if (!pdata)
  510. return NULL;
  511. pdata->gpio_base = -1;
  512. return pdata;
  513. }
  514. static int max732x_probe(struct i2c_client *client,
  515. const struct i2c_device_id *id)
  516. {
  517. struct max732x_platform_data *pdata;
  518. struct device_node *node;
  519. struct max732x_chip *chip;
  520. struct i2c_client *c;
  521. uint16_t addr_a, addr_b;
  522. int ret, nr_port;
  523. pdata = dev_get_platdata(&client->dev);
  524. node = client->dev.of_node;
  525. if (!pdata && node)
  526. pdata = of_gpio_max732x(&client->dev);
  527. if (!pdata) {
  528. dev_dbg(&client->dev, "no platform data\n");
  529. return -EINVAL;
  530. }
  531. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  532. if (chip == NULL)
  533. return -ENOMEM;
  534. chip->client = client;
  535. nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
  536. chip->gpio_chip.dev = &client->dev;
  537. addr_a = (client->addr & 0x0f) | 0x60;
  538. addr_b = (client->addr & 0x0f) | 0x50;
  539. switch (client->addr & 0x70) {
  540. case 0x60:
  541. chip->client_group_a = client;
  542. if (nr_port > 8) {
  543. c = i2c_new_dummy(client->adapter, addr_b);
  544. chip->client_group_b = chip->client_dummy = c;
  545. }
  546. break;
  547. case 0x50:
  548. chip->client_group_b = client;
  549. if (nr_port > 8) {
  550. c = i2c_new_dummy(client->adapter, addr_a);
  551. chip->client_group_a = chip->client_dummy = c;
  552. }
  553. break;
  554. default:
  555. dev_err(&client->dev, "invalid I2C address specified %02x\n",
  556. client->addr);
  557. ret = -EINVAL;
  558. goto out_failed;
  559. }
  560. if (nr_port > 8 && !chip->client_dummy) {
  561. dev_err(&client->dev,
  562. "Failed to allocate second group I2C device\n");
  563. ret = -ENODEV;
  564. goto out_failed;
  565. }
  566. mutex_init(&chip->lock);
  567. ret = max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
  568. if (ret)
  569. goto out_failed;
  570. if (nr_port > 8) {
  571. ret = max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
  572. if (ret)
  573. goto out_failed;
  574. }
  575. ret = gpiochip_add(&chip->gpio_chip);
  576. if (ret)
  577. goto out_failed;
  578. ret = max732x_irq_setup(chip, id);
  579. if (ret) {
  580. gpiochip_remove(&chip->gpio_chip);
  581. goto out_failed;
  582. }
  583. if (pdata && pdata->setup) {
  584. ret = pdata->setup(client, chip->gpio_chip.base,
  585. chip->gpio_chip.ngpio, pdata->context);
  586. if (ret < 0)
  587. dev_warn(&client->dev, "setup failed, %d\n", ret);
  588. }
  589. i2c_set_clientdata(client, chip);
  590. return 0;
  591. out_failed:
  592. if (chip->client_dummy)
  593. i2c_unregister_device(chip->client_dummy);
  594. return ret;
  595. }
  596. static int max732x_remove(struct i2c_client *client)
  597. {
  598. struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
  599. struct max732x_chip *chip = i2c_get_clientdata(client);
  600. if (pdata && pdata->teardown) {
  601. int ret;
  602. ret = pdata->teardown(client, chip->gpio_chip.base,
  603. chip->gpio_chip.ngpio, pdata->context);
  604. if (ret < 0) {
  605. dev_err(&client->dev, "%s failed, %d\n",
  606. "teardown", ret);
  607. return ret;
  608. }
  609. }
  610. gpiochip_remove(&chip->gpio_chip);
  611. /* unregister any dummy i2c_client */
  612. if (chip->client_dummy)
  613. i2c_unregister_device(chip->client_dummy);
  614. return 0;
  615. }
  616. static struct i2c_driver max732x_driver = {
  617. .driver = {
  618. .name = "max732x",
  619. .owner = THIS_MODULE,
  620. .of_match_table = of_match_ptr(max732x_of_table),
  621. },
  622. .probe = max732x_probe,
  623. .remove = max732x_remove,
  624. .id_table = max732x_id,
  625. };
  626. static int __init max732x_init(void)
  627. {
  628. return i2c_add_driver(&max732x_driver);
  629. }
  630. /* register after i2c postcore initcall and before
  631. * subsys initcalls that may rely on these GPIOs
  632. */
  633. subsys_initcall(max732x_init);
  634. static void __exit max732x_exit(void)
  635. {
  636. i2c_del_driver(&max732x_driver);
  637. }
  638. module_exit(max732x_exit);
  639. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
  640. MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
  641. MODULE_LICENSE("GPL");