gpio-pca953x.c 17 KB

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