gpio-pca953x.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * PCA953x 4/8/16 bit I/O ports
  3. *
  4. * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
  5. * Copyright (C) 2007 Marvell International Ltd.
  6. *
  7. * Derived from drivers/i2c/chips/pca9539.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/gpio.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/i2c.h>
  19. #include <linux/i2c/pca953x.h>
  20. #include <linux/slab.h>
  21. #ifdef CONFIG_OF_GPIO
  22. #include <linux/of_platform.h>
  23. #endif
  24. #define PCA953X_INPUT 0
  25. #define PCA953X_OUTPUT 1
  26. #define PCA953X_INVERT 2
  27. #define PCA953X_DIRECTION 3
  28. #define PCA957X_IN 0
  29. #define PCA957X_INVRT 1
  30. #define PCA957X_BKEN 2
  31. #define PCA957X_PUPD 3
  32. #define PCA957X_CFG 4
  33. #define PCA957X_OUT 5
  34. #define PCA957X_MSK 6
  35. #define PCA957X_INTS 7
  36. #define PCA_GPIO_MASK 0x00FF
  37. #define PCA_INT 0x0100
  38. #define PCA953X_TYPE 0x1000
  39. #define PCA957X_TYPE 0x2000
  40. static const struct i2c_device_id pca953x_id[] = {
  41. { "pca9534", 8 | PCA953X_TYPE | PCA_INT, },
  42. { "pca9535", 16 | PCA953X_TYPE | PCA_INT, },
  43. { "pca9536", 4 | PCA953X_TYPE, },
  44. { "pca9537", 4 | PCA953X_TYPE | PCA_INT, },
  45. { "pca9538", 8 | PCA953X_TYPE | PCA_INT, },
  46. { "pca9539", 16 | PCA953X_TYPE | PCA_INT, },
  47. { "pca9554", 8 | PCA953X_TYPE | PCA_INT, },
  48. { "pca9555", 16 | PCA953X_TYPE | PCA_INT, },
  49. { "pca9556", 8 | PCA953X_TYPE, },
  50. { "pca9557", 8 | PCA953X_TYPE, },
  51. { "pca9574", 8 | PCA957X_TYPE | PCA_INT, },
  52. { "pca9575", 16 | PCA957X_TYPE | PCA_INT, },
  53. { "max7310", 8 | PCA953X_TYPE, },
  54. { "max7312", 16 | PCA953X_TYPE | PCA_INT, },
  55. { "max7313", 16 | PCA953X_TYPE | PCA_INT, },
  56. { "max7315", 8 | PCA953X_TYPE | PCA_INT, },
  57. { "pca6107", 8 | PCA953X_TYPE | PCA_INT, },
  58. { "tca6408", 8 | PCA953X_TYPE | PCA_INT, },
  59. { "tca6416", 16 | PCA953X_TYPE | PCA_INT, },
  60. /* NYET: { "tca6424", 24, }, */
  61. { }
  62. };
  63. MODULE_DEVICE_TABLE(i2c, pca953x_id);
  64. struct pca953x_chip {
  65. unsigned gpio_start;
  66. uint16_t reg_output;
  67. uint16_t reg_direction;
  68. struct mutex i2c_lock;
  69. #ifdef CONFIG_GPIO_PCA953X_IRQ
  70. struct mutex irq_lock;
  71. uint16_t irq_mask;
  72. uint16_t irq_stat;
  73. uint16_t irq_trig_raise;
  74. uint16_t irq_trig_fall;
  75. int irq_base;
  76. #endif
  77. struct i2c_client *client;
  78. struct gpio_chip gpio_chip;
  79. const char *const *names;
  80. int chip_type;
  81. };
  82. static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
  83. {
  84. int ret = 0;
  85. if (chip->gpio_chip.ngpio <= 8)
  86. ret = i2c_smbus_write_byte_data(chip->client, reg, val);
  87. else {
  88. switch (chip->chip_type) {
  89. case PCA953X_TYPE:
  90. ret = i2c_smbus_write_word_data(chip->client,
  91. reg << 1, val);
  92. break;
  93. case PCA957X_TYPE:
  94. ret = i2c_smbus_write_byte_data(chip->client, reg << 1,
  95. val & 0xff);
  96. if (ret < 0)
  97. break;
  98. ret = i2c_smbus_write_byte_data(chip->client,
  99. (reg << 1) + 1,
  100. (val & 0xff00) >> 8);
  101. break;
  102. }
  103. }
  104. if (ret < 0) {
  105. dev_err(&chip->client->dev, "failed writing register\n");
  106. return ret;
  107. }
  108. return 0;
  109. }
  110. static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
  111. {
  112. int ret;
  113. if (chip->gpio_chip.ngpio <= 8)
  114. ret = i2c_smbus_read_byte_data(chip->client, reg);
  115. else
  116. ret = i2c_smbus_read_word_data(chip->client, reg << 1);
  117. if (ret < 0) {
  118. dev_err(&chip->client->dev, "failed reading register\n");
  119. return ret;
  120. }
  121. *val = (uint16_t)ret;
  122. return 0;
  123. }
  124. static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  125. {
  126. struct pca953x_chip *chip;
  127. uint16_t reg_val;
  128. int ret, offset = 0;
  129. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  130. mutex_lock(&chip->i2c_lock);
  131. reg_val = chip->reg_direction | (1u << off);
  132. switch (chip->chip_type) {
  133. case PCA953X_TYPE:
  134. offset = PCA953X_DIRECTION;
  135. break;
  136. case PCA957X_TYPE:
  137. offset = PCA957X_CFG;
  138. break;
  139. }
  140. ret = pca953x_write_reg(chip, offset, reg_val);
  141. if (ret)
  142. goto exit;
  143. chip->reg_direction = reg_val;
  144. ret = 0;
  145. exit:
  146. mutex_unlock(&chip->i2c_lock);
  147. return ret;
  148. }
  149. static int pca953x_gpio_direction_output(struct gpio_chip *gc,
  150. unsigned off, int val)
  151. {
  152. struct pca953x_chip *chip;
  153. uint16_t reg_val;
  154. int ret, offset = 0;
  155. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  156. mutex_lock(&chip->i2c_lock);
  157. /* set output level */
  158. if (val)
  159. reg_val = chip->reg_output | (1u << off);
  160. else
  161. reg_val = chip->reg_output & ~(1u << off);
  162. switch (chip->chip_type) {
  163. case PCA953X_TYPE:
  164. offset = PCA953X_OUTPUT;
  165. break;
  166. case PCA957X_TYPE:
  167. offset = PCA957X_OUT;
  168. break;
  169. }
  170. ret = pca953x_write_reg(chip, offset, reg_val);
  171. if (ret)
  172. goto exit;
  173. chip->reg_output = reg_val;
  174. /* then direction */
  175. reg_val = chip->reg_direction & ~(1u << off);
  176. switch (chip->chip_type) {
  177. case PCA953X_TYPE:
  178. offset = PCA953X_DIRECTION;
  179. break;
  180. case PCA957X_TYPE:
  181. offset = PCA957X_CFG;
  182. break;
  183. }
  184. ret = pca953x_write_reg(chip, offset, reg_val);
  185. if (ret)
  186. goto exit;
  187. chip->reg_direction = reg_val;
  188. ret = 0;
  189. exit:
  190. mutex_unlock(&chip->i2c_lock);
  191. return ret;
  192. }
  193. static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
  194. {
  195. struct pca953x_chip *chip;
  196. uint16_t reg_val;
  197. int ret, offset = 0;
  198. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  199. mutex_lock(&chip->i2c_lock);
  200. switch (chip->chip_type) {
  201. case PCA953X_TYPE:
  202. offset = PCA953X_INPUT;
  203. break;
  204. case PCA957X_TYPE:
  205. offset = PCA957X_IN;
  206. break;
  207. }
  208. ret = pca953x_read_reg(chip, offset, &reg_val);
  209. mutex_unlock(&chip->i2c_lock);
  210. if (ret < 0) {
  211. /* NOTE: diagnostic already emitted; that's all we should
  212. * do unless gpio_*_value_cansleep() calls become different
  213. * from their nonsleeping siblings (and report faults).
  214. */
  215. return 0;
  216. }
  217. return (reg_val & (1u << off)) ? 1 : 0;
  218. }
  219. static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  220. {
  221. struct pca953x_chip *chip;
  222. uint16_t reg_val;
  223. int ret, offset = 0;
  224. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  225. mutex_lock(&chip->i2c_lock);
  226. if (val)
  227. reg_val = chip->reg_output | (1u << off);
  228. else
  229. reg_val = chip->reg_output & ~(1u << off);
  230. switch (chip->chip_type) {
  231. case PCA953X_TYPE:
  232. offset = PCA953X_OUTPUT;
  233. break;
  234. case PCA957X_TYPE:
  235. offset = PCA957X_OUT;
  236. break;
  237. }
  238. ret = pca953x_write_reg(chip, offset, reg_val);
  239. if (ret)
  240. goto exit;
  241. chip->reg_output = reg_val;
  242. exit:
  243. mutex_unlock(&chip->i2c_lock);
  244. }
  245. static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
  246. {
  247. struct gpio_chip *gc;
  248. gc = &chip->gpio_chip;
  249. gc->direction_input = pca953x_gpio_direction_input;
  250. gc->direction_output = pca953x_gpio_direction_output;
  251. gc->get = pca953x_gpio_get_value;
  252. gc->set = pca953x_gpio_set_value;
  253. gc->can_sleep = 1;
  254. gc->base = chip->gpio_start;
  255. gc->ngpio = gpios;
  256. gc->label = chip->client->name;
  257. gc->dev = &chip->client->dev;
  258. gc->owner = THIS_MODULE;
  259. gc->names = chip->names;
  260. }
  261. #ifdef CONFIG_GPIO_PCA953X_IRQ
  262. static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
  263. {
  264. struct pca953x_chip *chip;
  265. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  266. return chip->irq_base + off;
  267. }
  268. static void pca953x_irq_mask(struct irq_data *d)
  269. {
  270. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  271. chip->irq_mask &= ~(1 << (d->irq - chip->irq_base));
  272. }
  273. static void pca953x_irq_unmask(struct irq_data *d)
  274. {
  275. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  276. chip->irq_mask |= 1 << (d->irq - chip->irq_base);
  277. }
  278. static void pca953x_irq_bus_lock(struct irq_data *d)
  279. {
  280. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  281. mutex_lock(&chip->irq_lock);
  282. }
  283. static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
  284. {
  285. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  286. uint16_t new_irqs;
  287. uint16_t level;
  288. /* Look for any newly setup interrupt */
  289. new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
  290. new_irqs &= ~chip->reg_direction;
  291. while (new_irqs) {
  292. level = __ffs(new_irqs);
  293. pca953x_gpio_direction_input(&chip->gpio_chip, level);
  294. new_irqs &= ~(1 << level);
  295. }
  296. mutex_unlock(&chip->irq_lock);
  297. }
  298. static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
  299. {
  300. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  301. uint16_t level = d->irq - chip->irq_base;
  302. uint16_t mask = 1 << level;
  303. if (!(type & IRQ_TYPE_EDGE_BOTH)) {
  304. dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
  305. d->irq, type);
  306. return -EINVAL;
  307. }
  308. if (type & IRQ_TYPE_EDGE_FALLING)
  309. chip->irq_trig_fall |= mask;
  310. else
  311. chip->irq_trig_fall &= ~mask;
  312. if (type & IRQ_TYPE_EDGE_RISING)
  313. chip->irq_trig_raise |= mask;
  314. else
  315. chip->irq_trig_raise &= ~mask;
  316. return 0;
  317. }
  318. static struct irq_chip pca953x_irq_chip = {
  319. .name = "pca953x",
  320. .irq_mask = pca953x_irq_mask,
  321. .irq_unmask = pca953x_irq_unmask,
  322. .irq_bus_lock = pca953x_irq_bus_lock,
  323. .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
  324. .irq_set_type = pca953x_irq_set_type,
  325. };
  326. static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
  327. {
  328. uint16_t cur_stat;
  329. uint16_t old_stat;
  330. uint16_t pending;
  331. uint16_t trigger;
  332. int ret, offset = 0;
  333. switch (chip->chip_type) {
  334. case PCA953X_TYPE:
  335. offset = PCA953X_INPUT;
  336. break;
  337. case PCA957X_TYPE:
  338. offset = PCA957X_IN;
  339. break;
  340. }
  341. ret = pca953x_read_reg(chip, offset, &cur_stat);
  342. if (ret)
  343. return 0;
  344. /* Remove output pins from the equation */
  345. cur_stat &= chip->reg_direction;
  346. old_stat = chip->irq_stat;
  347. trigger = (cur_stat ^ old_stat) & chip->irq_mask;
  348. if (!trigger)
  349. return 0;
  350. chip->irq_stat = cur_stat;
  351. pending = (old_stat & chip->irq_trig_fall) |
  352. (cur_stat & chip->irq_trig_raise);
  353. pending &= trigger;
  354. return pending;
  355. }
  356. static irqreturn_t pca953x_irq_handler(int irq, void *devid)
  357. {
  358. struct pca953x_chip *chip = devid;
  359. uint16_t pending;
  360. uint16_t level;
  361. pending = pca953x_irq_pending(chip);
  362. if (!pending)
  363. return IRQ_HANDLED;
  364. do {
  365. level = __ffs(pending);
  366. handle_nested_irq(level + chip->irq_base);
  367. pending &= ~(1 << level);
  368. } while (pending);
  369. return IRQ_HANDLED;
  370. }
  371. static int pca953x_irq_setup(struct pca953x_chip *chip,
  372. const struct i2c_device_id *id,
  373. int irq_base)
  374. {
  375. struct i2c_client *client = chip->client;
  376. int ret, offset = 0;
  377. if (irq_base != -1
  378. && (id->driver_data & PCA_INT)) {
  379. int lvl;
  380. switch (chip->chip_type) {
  381. case PCA953X_TYPE:
  382. offset = PCA953X_INPUT;
  383. break;
  384. case PCA957X_TYPE:
  385. offset = PCA957X_IN;
  386. break;
  387. }
  388. ret = pca953x_read_reg(chip, offset, &chip->irq_stat);
  389. if (ret)
  390. goto out_failed;
  391. /*
  392. * There is no way to know which GPIO line generated the
  393. * interrupt. We have to rely on the previous read for
  394. * this purpose.
  395. */
  396. chip->irq_stat &= chip->reg_direction;
  397. mutex_init(&chip->irq_lock);
  398. chip->irq_base = irq_alloc_descs(-1, irq_base, chip->gpio_chip.ngpio, -1);
  399. if (chip->irq_base < 0)
  400. goto out_failed;
  401. for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
  402. int irq = lvl + chip->irq_base;
  403. irq_clear_status_flags(irq, IRQ_NOREQUEST);
  404. irq_set_chip_data(irq, chip);
  405. irq_set_chip(irq, &pca953x_irq_chip);
  406. irq_set_nested_thread(irq, true);
  407. #ifdef CONFIG_ARM
  408. set_irq_flags(irq, IRQF_VALID);
  409. #else
  410. irq_set_noprobe(irq);
  411. #endif
  412. }
  413. ret = request_threaded_irq(client->irq,
  414. NULL,
  415. pca953x_irq_handler,
  416. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  417. dev_name(&client->dev), chip);
  418. if (ret) {
  419. dev_err(&client->dev, "failed to request irq %d\n",
  420. client->irq);
  421. goto out_failed;
  422. }
  423. chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
  424. }
  425. return 0;
  426. out_failed:
  427. chip->irq_base = -1;
  428. return ret;
  429. }
  430. static void pca953x_irq_teardown(struct pca953x_chip *chip)
  431. {
  432. if (chip->irq_base != -1) {
  433. irq_free_descs(chip->irq_base, chip->gpio_chip.ngpio);
  434. free_irq(chip->client->irq, chip);
  435. }
  436. }
  437. #else /* CONFIG_GPIO_PCA953X_IRQ */
  438. static int pca953x_irq_setup(struct pca953x_chip *chip,
  439. const struct i2c_device_id *id,
  440. int irq_base)
  441. {
  442. struct i2c_client *client = chip->client;
  443. if (irq_base != -1 && (id->driver_data & PCA_INT))
  444. dev_warn(&client->dev, "interrupt support not compiled in\n");
  445. return 0;
  446. }
  447. static void pca953x_irq_teardown(struct pca953x_chip *chip)
  448. {
  449. }
  450. #endif
  451. /*
  452. * Handlers for alternative sources of platform_data
  453. */
  454. #ifdef CONFIG_OF_GPIO
  455. /*
  456. * Translate OpenFirmware node properties into platform_data
  457. * WARNING: This is DEPRECATED and will be removed eventually!
  458. */
  459. static void
  460. pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, int *invert)
  461. {
  462. struct device_node *node;
  463. const __be32 *val;
  464. int size;
  465. node = client->dev.of_node;
  466. if (node == NULL)
  467. return;
  468. *gpio_base = -1;
  469. val = of_get_property(node, "linux,gpio-base", &size);
  470. WARN(val, "%s: device-tree property 'linux,gpio-base' is deprecated!", __func__);
  471. if (val) {
  472. if (size != sizeof(*val))
  473. dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
  474. node->full_name);
  475. else
  476. *gpio_base = be32_to_cpup(val);
  477. }
  478. val = of_get_property(node, "polarity", NULL);
  479. WARN(val, "%s: device-tree property 'polarity' is deprecated!", __func__);
  480. if (val)
  481. *invert = *val;
  482. }
  483. #else
  484. static void
  485. pca953x_get_alt_pdata(struct i2c_client *client, int *gpio_base, int *invert)
  486. {
  487. *gpio_base = -1;
  488. }
  489. #endif
  490. static int __devinit device_pca953x_init(struct pca953x_chip *chip, int invert)
  491. {
  492. int ret;
  493. ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
  494. if (ret)
  495. goto out;
  496. ret = pca953x_read_reg(chip, PCA953X_DIRECTION,
  497. &chip->reg_direction);
  498. if (ret)
  499. goto out;
  500. /* set platform specific polarity inversion */
  501. ret = pca953x_write_reg(chip, PCA953X_INVERT, invert);
  502. out:
  503. return ret;
  504. }
  505. static int __devinit device_pca957x_init(struct pca953x_chip *chip, int invert)
  506. {
  507. int ret;
  508. uint16_t val = 0;
  509. /* Let every port in proper state, that could save power */
  510. pca953x_write_reg(chip, PCA957X_PUPD, 0x0);
  511. pca953x_write_reg(chip, PCA957X_CFG, 0xffff);
  512. pca953x_write_reg(chip, PCA957X_OUT, 0x0);
  513. ret = pca953x_read_reg(chip, PCA957X_IN, &val);
  514. if (ret)
  515. goto out;
  516. ret = pca953x_read_reg(chip, PCA957X_OUT, &chip->reg_output);
  517. if (ret)
  518. goto out;
  519. ret = pca953x_read_reg(chip, PCA957X_CFG, &chip->reg_direction);
  520. if (ret)
  521. goto out;
  522. /* set platform specific polarity inversion */
  523. pca953x_write_reg(chip, PCA957X_INVRT, invert);
  524. /* To enable register 6, 7 to controll pull up and pull down */
  525. pca953x_write_reg(chip, PCA957X_BKEN, 0x202);
  526. return 0;
  527. out:
  528. return ret;
  529. }
  530. static int __devinit pca953x_probe(struct i2c_client *client,
  531. const struct i2c_device_id *id)
  532. {
  533. struct pca953x_platform_data *pdata;
  534. struct pca953x_chip *chip;
  535. int irq_base=0, invert=0;
  536. int ret;
  537. chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
  538. if (chip == NULL)
  539. return -ENOMEM;
  540. pdata = client->dev.platform_data;
  541. if (pdata) {
  542. irq_base = pdata->irq_base;
  543. chip->gpio_start = pdata->gpio_base;
  544. invert = pdata->invert;
  545. chip->names = pdata->names;
  546. } else {
  547. pca953x_get_alt_pdata(client, &chip->gpio_start, &invert);
  548. #ifdef CONFIG_OF_GPIO
  549. /* If I2C node has no interrupts property, disable GPIO interrupts */
  550. if (of_find_property(client->dev.of_node, "interrupts", NULL) == NULL)
  551. irq_base = -1;
  552. #endif
  553. }
  554. chip->client = client;
  555. chip->chip_type = id->driver_data & (PCA953X_TYPE | PCA957X_TYPE);
  556. mutex_init(&chip->i2c_lock);
  557. /* initialize cached registers from their original values.
  558. * we can't share this chip with another i2c master.
  559. */
  560. pca953x_setup_gpio(chip, id->driver_data & PCA_GPIO_MASK);
  561. if (chip->chip_type == PCA953X_TYPE)
  562. ret = device_pca953x_init(chip, invert);
  563. else
  564. ret = device_pca957x_init(chip, invert);
  565. if (ret)
  566. goto out_failed;
  567. ret = pca953x_irq_setup(chip, id, irq_base);
  568. if (ret)
  569. goto out_failed;
  570. ret = gpiochip_add(&chip->gpio_chip);
  571. if (ret)
  572. goto out_failed_irq;
  573. if (pdata && pdata->setup) {
  574. ret = pdata->setup(client, chip->gpio_chip.base,
  575. chip->gpio_chip.ngpio, pdata->context);
  576. if (ret < 0)
  577. dev_warn(&client->dev, "setup failed, %d\n", ret);
  578. }
  579. i2c_set_clientdata(client, chip);
  580. return 0;
  581. out_failed_irq:
  582. pca953x_irq_teardown(chip);
  583. out_failed:
  584. kfree(chip);
  585. return ret;
  586. }
  587. static int pca953x_remove(struct i2c_client *client)
  588. {
  589. struct pca953x_platform_data *pdata = client->dev.platform_data;
  590. struct pca953x_chip *chip = i2c_get_clientdata(client);
  591. int ret = 0;
  592. if (pdata && pdata->teardown) {
  593. ret = pdata->teardown(client, chip->gpio_chip.base,
  594. chip->gpio_chip.ngpio, pdata->context);
  595. if (ret < 0) {
  596. dev_err(&client->dev, "%s failed, %d\n",
  597. "teardown", ret);
  598. return ret;
  599. }
  600. }
  601. ret = gpiochip_remove(&chip->gpio_chip);
  602. if (ret) {
  603. dev_err(&client->dev, "%s failed, %d\n",
  604. "gpiochip_remove()", ret);
  605. return ret;
  606. }
  607. pca953x_irq_teardown(chip);
  608. kfree(chip);
  609. return 0;
  610. }
  611. static struct i2c_driver pca953x_driver = {
  612. .driver = {
  613. .name = "pca953x",
  614. },
  615. .probe = pca953x_probe,
  616. .remove = pca953x_remove,
  617. .id_table = pca953x_id,
  618. };
  619. static int __init pca953x_init(void)
  620. {
  621. return i2c_add_driver(&pca953x_driver);
  622. }
  623. /* register after i2c postcore initcall and before
  624. * subsys initcalls that may rely on these GPIOs
  625. */
  626. subsys_initcall(pca953x_init);
  627. static void __exit pca953x_exit(void)
  628. {
  629. i2c_del_driver(&pca953x_driver);
  630. }
  631. module_exit(pca953x_exit);
  632. MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
  633. MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
  634. MODULE_LICENSE("GPL");