gpio-pca953x.c 18 KB

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