pca953x.c 17 KB

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