max732x.c 17 KB

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