gpio-max732x.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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/platform_data/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 int max732x_writeb(struct max732x_chip *chip, int group_a, uint8_t val)
  144. {
  145. struct i2c_client *client;
  146. int ret;
  147. client = group_a ? chip->client_group_a : chip->client_group_b;
  148. ret = i2c_smbus_write_byte(client, val);
  149. if (ret < 0) {
  150. dev_err(&client->dev, "failed writing\n");
  151. return ret;
  152. }
  153. return 0;
  154. }
  155. static int max732x_readb(struct max732x_chip *chip, int group_a, uint8_t *val)
  156. {
  157. struct i2c_client *client;
  158. int ret;
  159. client = group_a ? chip->client_group_a : chip->client_group_b;
  160. ret = i2c_smbus_read_byte(client);
  161. if (ret < 0) {
  162. dev_err(&client->dev, "failed reading\n");
  163. return ret;
  164. }
  165. *val = (uint8_t)ret;
  166. return 0;
  167. }
  168. static inline int is_group_a(struct max732x_chip *chip, unsigned off)
  169. {
  170. return (1u << off) & chip->mask_group_a;
  171. }
  172. static int max732x_gpio_get_value(struct gpio_chip *gc, unsigned off)
  173. {
  174. struct max732x_chip *chip = gpiochip_get_data(gc);
  175. uint8_t reg_val;
  176. int ret;
  177. ret = max732x_readb(chip, is_group_a(chip, off), &reg_val);
  178. if (ret < 0)
  179. return ret;
  180. return !!(reg_val & (1u << (off & 0x7)));
  181. }
  182. static void max732x_gpio_set_mask(struct gpio_chip *gc, unsigned off, int mask,
  183. int val)
  184. {
  185. struct max732x_chip *chip = gpiochip_get_data(gc);
  186. uint8_t reg_out;
  187. int ret;
  188. mutex_lock(&chip->lock);
  189. reg_out = (off > 7) ? chip->reg_out[1] : chip->reg_out[0];
  190. reg_out = (reg_out & ~mask) | (val & mask);
  191. ret = max732x_writeb(chip, is_group_a(chip, off), reg_out);
  192. if (ret < 0)
  193. goto out;
  194. /* update the shadow register then */
  195. if (off > 7)
  196. chip->reg_out[1] = reg_out;
  197. else
  198. chip->reg_out[0] = reg_out;
  199. out:
  200. mutex_unlock(&chip->lock);
  201. }
  202. static void max732x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  203. {
  204. unsigned base = off & ~0x7;
  205. uint8_t mask = 1u << (off & 0x7);
  206. max732x_gpio_set_mask(gc, base, mask, val << (off & 0x7));
  207. }
  208. static void max732x_gpio_set_multiple(struct gpio_chip *gc,
  209. unsigned long *mask, unsigned long *bits)
  210. {
  211. unsigned mask_lo = mask[0] & 0xff;
  212. unsigned mask_hi = (mask[0] >> 8) & 0xff;
  213. if (mask_lo)
  214. max732x_gpio_set_mask(gc, 0, mask_lo, bits[0] & 0xff);
  215. if (mask_hi)
  216. max732x_gpio_set_mask(gc, 8, mask_hi, (bits[0] >> 8) & 0xff);
  217. }
  218. static int max732x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  219. {
  220. struct max732x_chip *chip = gpiochip_get_data(gc);
  221. unsigned int mask = 1u << off;
  222. if ((mask & chip->dir_input) == 0) {
  223. dev_dbg(&chip->client->dev, "%s port %d is output only\n",
  224. chip->client->name, off);
  225. return -EACCES;
  226. }
  227. /*
  228. * Open-drain pins must be set to high impedance (which is
  229. * equivalent to output-high) to be turned into an input.
  230. */
  231. if ((mask & chip->dir_output))
  232. max732x_gpio_set_value(gc, off, 1);
  233. return 0;
  234. }
  235. static int max732x_gpio_direction_output(struct gpio_chip *gc,
  236. unsigned off, int val)
  237. {
  238. struct max732x_chip *chip = gpiochip_get_data(gc);
  239. unsigned int mask = 1u << off;
  240. if ((mask & chip->dir_output) == 0) {
  241. dev_dbg(&chip->client->dev, "%s port %d is input only\n",
  242. chip->client->name, off);
  243. return -EACCES;
  244. }
  245. max732x_gpio_set_value(gc, off, val);
  246. return 0;
  247. }
  248. #ifdef CONFIG_GPIO_MAX732X_IRQ
  249. static int max732x_writew(struct max732x_chip *chip, uint16_t val)
  250. {
  251. int ret;
  252. val = cpu_to_le16(val);
  253. ret = i2c_master_send(chip->client_group_a, (char *)&val, 2);
  254. if (ret < 0) {
  255. dev_err(&chip->client_group_a->dev, "failed writing\n");
  256. return ret;
  257. }
  258. return 0;
  259. }
  260. static int max732x_readw(struct max732x_chip *chip, uint16_t *val)
  261. {
  262. int ret;
  263. ret = i2c_master_recv(chip->client_group_a, (char *)val, 2);
  264. if (ret < 0) {
  265. dev_err(&chip->client_group_a->dev, "failed reading\n");
  266. return ret;
  267. }
  268. *val = le16_to_cpu(*val);
  269. return 0;
  270. }
  271. static void max732x_irq_update_mask(struct max732x_chip *chip)
  272. {
  273. uint16_t msg;
  274. if (chip->irq_mask == chip->irq_mask_cur)
  275. return;
  276. chip->irq_mask = chip->irq_mask_cur;
  277. if (chip->irq_features == INT_NO_MASK)
  278. return;
  279. mutex_lock(&chip->lock);
  280. switch (chip->irq_features) {
  281. case INT_INDEP_MASK:
  282. msg = (chip->irq_mask << 8) | chip->reg_out[0];
  283. max732x_writew(chip, msg);
  284. break;
  285. case INT_MERGED_MASK:
  286. msg = chip->irq_mask | chip->reg_out[0];
  287. max732x_writeb(chip, 1, (uint8_t)msg);
  288. break;
  289. }
  290. mutex_unlock(&chip->lock);
  291. }
  292. static void max732x_irq_mask(struct irq_data *d)
  293. {
  294. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  295. struct max732x_chip *chip = gpiochip_get_data(gc);
  296. chip->irq_mask_cur &= ~(1 << d->hwirq);
  297. }
  298. static void max732x_irq_unmask(struct irq_data *d)
  299. {
  300. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  301. struct max732x_chip *chip = gpiochip_get_data(gc);
  302. chip->irq_mask_cur |= 1 << d->hwirq;
  303. }
  304. static void max732x_irq_bus_lock(struct irq_data *d)
  305. {
  306. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  307. struct max732x_chip *chip = gpiochip_get_data(gc);
  308. mutex_lock(&chip->irq_lock);
  309. chip->irq_mask_cur = chip->irq_mask;
  310. }
  311. static void max732x_irq_bus_sync_unlock(struct irq_data *d)
  312. {
  313. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  314. struct max732x_chip *chip = gpiochip_get_data(gc);
  315. uint16_t new_irqs;
  316. uint16_t level;
  317. max732x_irq_update_mask(chip);
  318. new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
  319. while (new_irqs) {
  320. level = __ffs(new_irqs);
  321. max732x_gpio_direction_input(&chip->gpio_chip, level);
  322. new_irqs &= ~(1 << level);
  323. }
  324. mutex_unlock(&chip->irq_lock);
  325. }
  326. static int max732x_irq_set_type(struct irq_data *d, unsigned int type)
  327. {
  328. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  329. struct max732x_chip *chip = gpiochip_get_data(gc);
  330. uint16_t off = d->hwirq;
  331. uint16_t mask = 1 << off;
  332. if (!(mask & chip->dir_input)) {
  333. dev_dbg(&chip->client->dev, "%s port %d is output only\n",
  334. chip->client->name, off);
  335. return -EACCES;
  336. }
  337. if (!(type & IRQ_TYPE_EDGE_BOTH)) {
  338. dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
  339. d->irq, type);
  340. return -EINVAL;
  341. }
  342. if (type & IRQ_TYPE_EDGE_FALLING)
  343. chip->irq_trig_fall |= mask;
  344. else
  345. chip->irq_trig_fall &= ~mask;
  346. if (type & IRQ_TYPE_EDGE_RISING)
  347. chip->irq_trig_raise |= mask;
  348. else
  349. chip->irq_trig_raise &= ~mask;
  350. return 0;
  351. }
  352. static int max732x_irq_set_wake(struct irq_data *data, unsigned int on)
  353. {
  354. struct max732x_chip *chip = irq_data_get_irq_chip_data(data);
  355. irq_set_irq_wake(chip->client->irq, on);
  356. return 0;
  357. }
  358. static struct irq_chip max732x_irq_chip = {
  359. .name = "max732x",
  360. .irq_mask = max732x_irq_mask,
  361. .irq_unmask = max732x_irq_unmask,
  362. .irq_bus_lock = max732x_irq_bus_lock,
  363. .irq_bus_sync_unlock = max732x_irq_bus_sync_unlock,
  364. .irq_set_type = max732x_irq_set_type,
  365. .irq_set_wake = max732x_irq_set_wake,
  366. };
  367. static uint8_t max732x_irq_pending(struct max732x_chip *chip)
  368. {
  369. uint8_t cur_stat;
  370. uint8_t old_stat;
  371. uint8_t trigger;
  372. uint8_t pending;
  373. uint16_t status;
  374. int ret;
  375. ret = max732x_readw(chip, &status);
  376. if (ret)
  377. return 0;
  378. trigger = status >> 8;
  379. trigger &= chip->irq_mask;
  380. if (!trigger)
  381. return 0;
  382. cur_stat = status & 0xFF;
  383. cur_stat &= chip->irq_mask;
  384. old_stat = cur_stat ^ trigger;
  385. pending = (old_stat & chip->irq_trig_fall) |
  386. (cur_stat & chip->irq_trig_raise);
  387. pending &= trigger;
  388. return pending;
  389. }
  390. static irqreturn_t max732x_irq_handler(int irq, void *devid)
  391. {
  392. struct max732x_chip *chip = devid;
  393. uint8_t pending;
  394. uint8_t level;
  395. pending = max732x_irq_pending(chip);
  396. if (!pending)
  397. return IRQ_HANDLED;
  398. do {
  399. level = __ffs(pending);
  400. handle_nested_irq(irq_find_mapping(chip->gpio_chip.irq.domain,
  401. level));
  402. pending &= ~(1 << level);
  403. } while (pending);
  404. return IRQ_HANDLED;
  405. }
  406. static int max732x_irq_setup(struct max732x_chip *chip,
  407. const struct i2c_device_id *id)
  408. {
  409. struct i2c_client *client = chip->client;
  410. struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
  411. int has_irq = max732x_features[id->driver_data] >> 32;
  412. int irq_base = 0;
  413. int ret;
  414. if (((pdata && pdata->irq_base) || client->irq)
  415. && has_irq != INT_NONE) {
  416. if (pdata)
  417. irq_base = pdata->irq_base;
  418. chip->irq_features = has_irq;
  419. mutex_init(&chip->irq_lock);
  420. ret = devm_request_threaded_irq(&client->dev, client->irq,
  421. NULL, max732x_irq_handler, IRQF_ONESHOT |
  422. IRQF_TRIGGER_FALLING | IRQF_SHARED,
  423. dev_name(&client->dev), chip);
  424. if (ret) {
  425. dev_err(&client->dev, "failed to request irq %d\n",
  426. client->irq);
  427. return ret;
  428. }
  429. ret = gpiochip_irqchip_add_nested(&chip->gpio_chip,
  430. &max732x_irq_chip,
  431. irq_base,
  432. handle_simple_irq,
  433. IRQ_TYPE_NONE);
  434. if (ret) {
  435. dev_err(&client->dev,
  436. "could not connect irqchip to gpiochip\n");
  437. return ret;
  438. }
  439. gpiochip_set_nested_irqchip(&chip->gpio_chip,
  440. &max732x_irq_chip,
  441. client->irq);
  442. }
  443. return 0;
  444. }
  445. #else /* CONFIG_GPIO_MAX732X_IRQ */
  446. static int max732x_irq_setup(struct max732x_chip *chip,
  447. const struct i2c_device_id *id)
  448. {
  449. struct i2c_client *client = chip->client;
  450. struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
  451. int has_irq = max732x_features[id->driver_data] >> 32;
  452. if (((pdata && pdata->irq_base) || client->irq) && has_irq != INT_NONE)
  453. dev_warn(&client->dev, "interrupt support not compiled in\n");
  454. return 0;
  455. }
  456. #endif
  457. static int max732x_setup_gpio(struct max732x_chip *chip,
  458. const struct i2c_device_id *id,
  459. unsigned gpio_start)
  460. {
  461. struct gpio_chip *gc = &chip->gpio_chip;
  462. uint32_t id_data = (uint32_t)max732x_features[id->driver_data];
  463. int i, port = 0;
  464. for (i = 0; i < 16; i++, id_data >>= 2) {
  465. unsigned int mask = 1 << port;
  466. switch (id_data & 0x3) {
  467. case PORT_OUTPUT:
  468. chip->dir_output |= mask;
  469. break;
  470. case PORT_INPUT:
  471. chip->dir_input |= mask;
  472. break;
  473. case PORT_OPENDRAIN:
  474. chip->dir_output |= mask;
  475. chip->dir_input |= mask;
  476. break;
  477. default:
  478. continue;
  479. }
  480. if (i < 8)
  481. chip->mask_group_a |= mask;
  482. port++;
  483. }
  484. if (chip->dir_input)
  485. gc->direction_input = max732x_gpio_direction_input;
  486. if (chip->dir_output) {
  487. gc->direction_output = max732x_gpio_direction_output;
  488. gc->set = max732x_gpio_set_value;
  489. gc->set_multiple = max732x_gpio_set_multiple;
  490. }
  491. gc->get = max732x_gpio_get_value;
  492. gc->can_sleep = true;
  493. gc->base = gpio_start;
  494. gc->ngpio = port;
  495. gc->label = chip->client->name;
  496. gc->parent = &chip->client->dev;
  497. gc->owner = THIS_MODULE;
  498. return port;
  499. }
  500. static struct max732x_platform_data *of_gpio_max732x(struct device *dev)
  501. {
  502. struct max732x_platform_data *pdata;
  503. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  504. if (!pdata)
  505. return NULL;
  506. pdata->gpio_base = -1;
  507. return pdata;
  508. }
  509. static int max732x_probe(struct i2c_client *client,
  510. const struct i2c_device_id *id)
  511. {
  512. struct max732x_platform_data *pdata;
  513. struct device_node *node;
  514. struct max732x_chip *chip;
  515. struct i2c_client *c;
  516. uint16_t addr_a, addr_b;
  517. int ret, nr_port;
  518. pdata = dev_get_platdata(&client->dev);
  519. node = client->dev.of_node;
  520. if (!pdata && node)
  521. pdata = of_gpio_max732x(&client->dev);
  522. if (!pdata) {
  523. dev_dbg(&client->dev, "no platform data\n");
  524. return -EINVAL;
  525. }
  526. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  527. if (chip == NULL)
  528. return -ENOMEM;
  529. chip->client = client;
  530. nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base);
  531. chip->gpio_chip.parent = &client->dev;
  532. addr_a = (client->addr & 0x0f) | 0x60;
  533. addr_b = (client->addr & 0x0f) | 0x50;
  534. switch (client->addr & 0x70) {
  535. case 0x60:
  536. chip->client_group_a = client;
  537. if (nr_port > 8) {
  538. c = i2c_new_dummy(client->adapter, addr_b);
  539. if (!c) {
  540. dev_err(&client->dev,
  541. "Failed to allocate I2C device\n");
  542. ret = -ENODEV;
  543. goto out_failed;
  544. }
  545. chip->client_group_b = chip->client_dummy = c;
  546. }
  547. break;
  548. case 0x50:
  549. chip->client_group_b = client;
  550. if (nr_port > 8) {
  551. c = i2c_new_dummy(client->adapter, addr_a);
  552. if (!c) {
  553. dev_err(&client->dev,
  554. "Failed to allocate I2C device\n");
  555. ret = -ENODEV;
  556. goto out_failed;
  557. }
  558. chip->client_group_a = chip->client_dummy = c;
  559. }
  560. break;
  561. default:
  562. dev_err(&client->dev, "invalid I2C address specified %02x\n",
  563. client->addr);
  564. ret = -EINVAL;
  565. goto out_failed;
  566. }
  567. if (nr_port > 8 && !chip->client_dummy) {
  568. dev_err(&client->dev,
  569. "Failed to allocate second group I2C device\n");
  570. ret = -ENODEV;
  571. goto out_failed;
  572. }
  573. mutex_init(&chip->lock);
  574. ret = max732x_readb(chip, is_group_a(chip, 0), &chip->reg_out[0]);
  575. if (ret)
  576. goto out_failed;
  577. if (nr_port > 8) {
  578. ret = max732x_readb(chip, is_group_a(chip, 8), &chip->reg_out[1]);
  579. if (ret)
  580. goto out_failed;
  581. }
  582. ret = gpiochip_add_data(&chip->gpio_chip, chip);
  583. if (ret)
  584. goto out_failed;
  585. ret = max732x_irq_setup(chip, id);
  586. if (ret) {
  587. gpiochip_remove(&chip->gpio_chip);
  588. goto out_failed;
  589. }
  590. if (pdata && pdata->setup) {
  591. ret = pdata->setup(client, chip->gpio_chip.base,
  592. chip->gpio_chip.ngpio, pdata->context);
  593. if (ret < 0)
  594. dev_warn(&client->dev, "setup failed, %d\n", ret);
  595. }
  596. i2c_set_clientdata(client, chip);
  597. return 0;
  598. out_failed:
  599. i2c_unregister_device(chip->client_dummy);
  600. return ret;
  601. }
  602. static int max732x_remove(struct i2c_client *client)
  603. {
  604. struct max732x_platform_data *pdata = dev_get_platdata(&client->dev);
  605. struct max732x_chip *chip = i2c_get_clientdata(client);
  606. if (pdata && pdata->teardown) {
  607. int ret;
  608. ret = pdata->teardown(client, chip->gpio_chip.base,
  609. chip->gpio_chip.ngpio, pdata->context);
  610. if (ret < 0) {
  611. dev_err(&client->dev, "%s failed, %d\n",
  612. "teardown", ret);
  613. return ret;
  614. }
  615. }
  616. gpiochip_remove(&chip->gpio_chip);
  617. /* unregister any dummy i2c_client */
  618. i2c_unregister_device(chip->client_dummy);
  619. return 0;
  620. }
  621. static struct i2c_driver max732x_driver = {
  622. .driver = {
  623. .name = "max732x",
  624. .of_match_table = of_match_ptr(max732x_of_table),
  625. },
  626. .probe = max732x_probe,
  627. .remove = max732x_remove,
  628. .id_table = max732x_id,
  629. };
  630. static int __init max732x_init(void)
  631. {
  632. return i2c_add_driver(&max732x_driver);
  633. }
  634. /* register after i2c postcore initcall and before
  635. * subsys initcalls that may rely on these GPIOs
  636. */
  637. subsys_initcall(max732x_init);
  638. static void __exit max732x_exit(void)
  639. {
  640. i2c_del_driver(&max732x_driver);
  641. }
  642. module_exit(max732x_exit);
  643. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
  644. MODULE_DESCRIPTION("GPIO expander driver for MAX732X");
  645. MODULE_LICENSE("GPL");