pca953x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 PCA953X_GPIOS 0x00FF
  30. #define PCA953X_INT 0x0100
  31. static const struct i2c_device_id pca953x_id[] = {
  32. { "pca9534", 8 | PCA953X_INT, },
  33. { "pca9535", 16 | PCA953X_INT, },
  34. { "pca9536", 4, },
  35. { "pca9537", 4 | PCA953X_INT, },
  36. { "pca9538", 8 | PCA953X_INT, },
  37. { "pca9539", 16 | PCA953X_INT, },
  38. { "pca9554", 8 | PCA953X_INT, },
  39. { "pca9555", 16 | PCA953X_INT, },
  40. { "pca9556", 8, },
  41. { "pca9557", 8, },
  42. { "max7310", 8, },
  43. { "max7312", 16 | PCA953X_INT, },
  44. { "max7313", 16 | PCA953X_INT, },
  45. { "max7315", 8 | PCA953X_INT, },
  46. { "pca6107", 8 | PCA953X_INT, },
  47. { "tca6408", 8 | PCA953X_INT, },
  48. { "tca6416", 16 | PCA953X_INT, },
  49. /* NYET: { "tca6424", 24, }, */
  50. { }
  51. };
  52. MODULE_DEVICE_TABLE(i2c, pca953x_id);
  53. struct pca953x_chip {
  54. unsigned gpio_start;
  55. uint16_t reg_output;
  56. uint16_t reg_direction;
  57. struct mutex i2c_lock;
  58. #ifdef CONFIG_GPIO_PCA953X_IRQ
  59. struct mutex irq_lock;
  60. uint16_t irq_mask;
  61. uint16_t irq_stat;
  62. uint16_t irq_trig_raise;
  63. uint16_t irq_trig_fall;
  64. int irq_base;
  65. #endif
  66. struct i2c_client *client;
  67. struct pca953x_platform_data *dyn_pdata;
  68. struct gpio_chip gpio_chip;
  69. const char *const *names;
  70. };
  71. static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
  72. {
  73. int ret;
  74. if (chip->gpio_chip.ngpio <= 8)
  75. ret = i2c_smbus_write_byte_data(chip->client, reg, val);
  76. else
  77. ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
  78. if (ret < 0) {
  79. dev_err(&chip->client->dev, "failed writing register\n");
  80. return ret;
  81. }
  82. return 0;
  83. }
  84. static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
  85. {
  86. int ret;
  87. if (chip->gpio_chip.ngpio <= 8)
  88. ret = i2c_smbus_read_byte_data(chip->client, reg);
  89. else
  90. ret = i2c_smbus_read_word_data(chip->client, reg << 1);
  91. if (ret < 0) {
  92. dev_err(&chip->client->dev, "failed reading register\n");
  93. return ret;
  94. }
  95. *val = (uint16_t)ret;
  96. return 0;
  97. }
  98. static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  99. {
  100. struct pca953x_chip *chip;
  101. uint16_t reg_val;
  102. int ret;
  103. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  104. mutex_lock(&chip->i2c_lock);
  105. reg_val = chip->reg_direction | (1u << off);
  106. ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
  107. if (ret)
  108. goto exit;
  109. chip->reg_direction = reg_val;
  110. ret = 0;
  111. exit:
  112. mutex_unlock(&chip->i2c_lock);
  113. return ret;
  114. }
  115. static int pca953x_gpio_direction_output(struct gpio_chip *gc,
  116. unsigned off, int val)
  117. {
  118. struct pca953x_chip *chip;
  119. uint16_t reg_val;
  120. int ret;
  121. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  122. mutex_lock(&chip->i2c_lock);
  123. /* set output level */
  124. if (val)
  125. reg_val = chip->reg_output | (1u << off);
  126. else
  127. reg_val = chip->reg_output & ~(1u << off);
  128. ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
  129. if (ret)
  130. goto exit;
  131. chip->reg_output = reg_val;
  132. /* then direction */
  133. reg_val = chip->reg_direction & ~(1u << off);
  134. ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
  135. if (ret)
  136. goto exit;
  137. chip->reg_direction = reg_val;
  138. ret = 0;
  139. exit:
  140. mutex_unlock(&chip->i2c_lock);
  141. return ret;
  142. }
  143. static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
  144. {
  145. struct pca953x_chip *chip;
  146. uint16_t reg_val;
  147. int ret;
  148. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  149. mutex_lock(&chip->i2c_lock);
  150. ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
  151. mutex_unlock(&chip->i2c_lock);
  152. if (ret < 0) {
  153. /* NOTE: diagnostic already emitted; that's all we should
  154. * do unless gpio_*_value_cansleep() calls become different
  155. * from their nonsleeping siblings (and report faults).
  156. */
  157. return 0;
  158. }
  159. return (reg_val & (1u << off)) ? 1 : 0;
  160. }
  161. static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  162. {
  163. struct pca953x_chip *chip;
  164. uint16_t reg_val;
  165. int ret;
  166. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  167. mutex_lock(&chip->i2c_lock);
  168. if (val)
  169. reg_val = chip->reg_output | (1u << off);
  170. else
  171. reg_val = chip->reg_output & ~(1u << off);
  172. ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
  173. if (ret)
  174. goto exit;
  175. chip->reg_output = reg_val;
  176. exit:
  177. mutex_unlock(&chip->i2c_lock);
  178. }
  179. static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
  180. {
  181. struct gpio_chip *gc;
  182. gc = &chip->gpio_chip;
  183. gc->direction_input = pca953x_gpio_direction_input;
  184. gc->direction_output = pca953x_gpio_direction_output;
  185. gc->get = pca953x_gpio_get_value;
  186. gc->set = pca953x_gpio_set_value;
  187. gc->can_sleep = 1;
  188. gc->base = chip->gpio_start;
  189. gc->ngpio = gpios;
  190. gc->label = chip->client->name;
  191. gc->dev = &chip->client->dev;
  192. gc->owner = THIS_MODULE;
  193. gc->names = chip->names;
  194. }
  195. #ifdef CONFIG_GPIO_PCA953X_IRQ
  196. static int pca953x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
  197. {
  198. struct pca953x_chip *chip;
  199. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  200. return chip->irq_base + off;
  201. }
  202. static void pca953x_irq_mask(struct irq_data *d)
  203. {
  204. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  205. chip->irq_mask &= ~(1 << (d->irq - chip->irq_base));
  206. }
  207. static void pca953x_irq_unmask(struct irq_data *d)
  208. {
  209. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  210. chip->irq_mask |= 1 << (d->irq - chip->irq_base);
  211. }
  212. static void pca953x_irq_bus_lock(struct irq_data *d)
  213. {
  214. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  215. mutex_lock(&chip->irq_lock);
  216. }
  217. static void pca953x_irq_bus_sync_unlock(struct irq_data *d)
  218. {
  219. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  220. uint16_t new_irqs;
  221. uint16_t level;
  222. /* Look for any newly setup interrupt */
  223. new_irqs = chip->irq_trig_fall | chip->irq_trig_raise;
  224. new_irqs &= ~chip->reg_direction;
  225. while (new_irqs) {
  226. level = __ffs(new_irqs);
  227. pca953x_gpio_direction_input(&chip->gpio_chip, level);
  228. new_irqs &= ~(1 << level);
  229. }
  230. mutex_unlock(&chip->irq_lock);
  231. }
  232. static int pca953x_irq_set_type(struct irq_data *d, unsigned int type)
  233. {
  234. struct pca953x_chip *chip = irq_data_get_irq_chip_data(d);
  235. uint16_t level = d->irq - chip->irq_base;
  236. uint16_t mask = 1 << level;
  237. if (!(type & IRQ_TYPE_EDGE_BOTH)) {
  238. dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
  239. d->irq, type);
  240. return -EINVAL;
  241. }
  242. if (type & IRQ_TYPE_EDGE_FALLING)
  243. chip->irq_trig_fall |= mask;
  244. else
  245. chip->irq_trig_fall &= ~mask;
  246. if (type & IRQ_TYPE_EDGE_RISING)
  247. chip->irq_trig_raise |= mask;
  248. else
  249. chip->irq_trig_raise &= ~mask;
  250. return 0;
  251. }
  252. static struct irq_chip pca953x_irq_chip = {
  253. .name = "pca953x",
  254. .irq_mask = pca953x_irq_mask,
  255. .irq_unmask = pca953x_irq_unmask,
  256. .irq_bus_lock = pca953x_irq_bus_lock,
  257. .irq_bus_sync_unlock = pca953x_irq_bus_sync_unlock,
  258. .irq_set_type = pca953x_irq_set_type,
  259. };
  260. static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
  261. {
  262. uint16_t cur_stat;
  263. uint16_t old_stat;
  264. uint16_t pending;
  265. uint16_t trigger;
  266. int ret;
  267. ret = pca953x_read_reg(chip, PCA953X_INPUT, &cur_stat);
  268. if (ret)
  269. return 0;
  270. /* Remove output pins from the equation */
  271. cur_stat &= chip->reg_direction;
  272. old_stat = chip->irq_stat;
  273. trigger = (cur_stat ^ old_stat) & chip->irq_mask;
  274. if (!trigger)
  275. return 0;
  276. chip->irq_stat = cur_stat;
  277. pending = (old_stat & chip->irq_trig_fall) |
  278. (cur_stat & chip->irq_trig_raise);
  279. pending &= trigger;
  280. return pending;
  281. }
  282. static irqreturn_t pca953x_irq_handler(int irq, void *devid)
  283. {
  284. struct pca953x_chip *chip = devid;
  285. uint16_t pending;
  286. uint16_t level;
  287. pending = pca953x_irq_pending(chip);
  288. if (!pending)
  289. return IRQ_HANDLED;
  290. do {
  291. level = __ffs(pending);
  292. generic_handle_irq(level + chip->irq_base);
  293. pending &= ~(1 << level);
  294. } while (pending);
  295. return IRQ_HANDLED;
  296. }
  297. static int pca953x_irq_setup(struct pca953x_chip *chip,
  298. const struct i2c_device_id *id)
  299. {
  300. struct i2c_client *client = chip->client;
  301. struct pca953x_platform_data *pdata = client->dev.platform_data;
  302. int ret;
  303. if (pdata->irq_base != -1
  304. && (id->driver_data & PCA953X_INT)) {
  305. int lvl;
  306. ret = pca953x_read_reg(chip, PCA953X_INPUT,
  307. &chip->irq_stat);
  308. if (ret)
  309. goto out_failed;
  310. /*
  311. * There is no way to know which GPIO line generated the
  312. * interrupt. We have to rely on the previous read for
  313. * this purpose.
  314. */
  315. chip->irq_stat &= chip->reg_direction;
  316. chip->irq_base = pdata->irq_base;
  317. mutex_init(&chip->irq_lock);
  318. for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
  319. int irq = lvl + chip->irq_base;
  320. irq_set_chip_data(irq, chip);
  321. irq_set_chip_and_handler(irq, &pca953x_irq_chip,
  322. handle_edge_irq);
  323. #ifdef CONFIG_ARM
  324. set_irq_flags(irq, IRQF_VALID);
  325. #else
  326. irq_set_noprobe(irq);
  327. #endif
  328. }
  329. ret = request_threaded_irq(client->irq,
  330. NULL,
  331. pca953x_irq_handler,
  332. IRQF_TRIGGER_RISING |
  333. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  334. dev_name(&client->dev), chip);
  335. if (ret) {
  336. dev_err(&client->dev, "failed to request irq %d\n",
  337. client->irq);
  338. goto out_failed;
  339. }
  340. chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
  341. }
  342. return 0;
  343. out_failed:
  344. chip->irq_base = -1;
  345. return ret;
  346. }
  347. static void pca953x_irq_teardown(struct pca953x_chip *chip)
  348. {
  349. if (chip->irq_base != -1)
  350. free_irq(chip->client->irq, chip);
  351. }
  352. #else /* CONFIG_GPIO_PCA953X_IRQ */
  353. static int pca953x_irq_setup(struct pca953x_chip *chip,
  354. const struct i2c_device_id *id)
  355. {
  356. struct i2c_client *client = chip->client;
  357. struct pca953x_platform_data *pdata = client->dev.platform_data;
  358. if (pdata->irq_base != -1 && (id->driver_data & PCA953X_INT))
  359. dev_warn(&client->dev, "interrupt support not compiled in\n");
  360. return 0;
  361. }
  362. static void pca953x_irq_teardown(struct pca953x_chip *chip)
  363. {
  364. }
  365. #endif
  366. /*
  367. * Handlers for alternative sources of platform_data
  368. */
  369. #ifdef CONFIG_OF_GPIO
  370. /*
  371. * Translate OpenFirmware node properties into platform_data
  372. */
  373. static struct pca953x_platform_data *
  374. pca953x_get_alt_pdata(struct i2c_client *client)
  375. {
  376. struct pca953x_platform_data *pdata;
  377. struct device_node *node;
  378. const __be32 *val;
  379. int size;
  380. node = client->dev.of_node;
  381. if (node == NULL)
  382. return NULL;
  383. pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
  384. if (pdata == NULL) {
  385. dev_err(&client->dev, "Unable to allocate platform_data\n");
  386. return NULL;
  387. }
  388. pdata->gpio_base = -1;
  389. val = of_get_property(node, "linux,gpio-base", &size);
  390. if (val) {
  391. if (size != sizeof(*val))
  392. dev_warn(&client->dev, "%s: wrong linux,gpio-base\n",
  393. node->full_name);
  394. else
  395. pdata->gpio_base = be32_to_cpup(val);
  396. }
  397. val = of_get_property(node, "polarity", NULL);
  398. if (val)
  399. pdata->invert = *val;
  400. return pdata;
  401. }
  402. #else
  403. static struct pca953x_platform_data *
  404. pca953x_get_alt_pdata(struct i2c_client *client)
  405. {
  406. return NULL;
  407. }
  408. #endif
  409. static int __devinit pca953x_probe(struct i2c_client *client,
  410. const struct i2c_device_id *id)
  411. {
  412. struct pca953x_platform_data *pdata;
  413. struct pca953x_chip *chip;
  414. int ret;
  415. chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
  416. if (chip == NULL)
  417. return -ENOMEM;
  418. pdata = client->dev.platform_data;
  419. if (pdata == NULL) {
  420. pdata = pca953x_get_alt_pdata(client);
  421. /*
  422. * Unlike normal platform_data, this is allocated
  423. * dynamically and must be freed in the driver
  424. */
  425. chip->dyn_pdata = pdata;
  426. }
  427. if (pdata == NULL) {
  428. dev_dbg(&client->dev, "no platform data\n");
  429. ret = -EINVAL;
  430. goto out_failed;
  431. }
  432. chip->client = client;
  433. chip->gpio_start = pdata->gpio_base;
  434. chip->names = pdata->names;
  435. mutex_init(&chip->i2c_lock);
  436. /* initialize cached registers from their original values.
  437. * we can't share this chip with another i2c master.
  438. */
  439. pca953x_setup_gpio(chip, id->driver_data & PCA953X_GPIOS);
  440. ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
  441. if (ret)
  442. goto out_failed;
  443. ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
  444. if (ret)
  445. goto out_failed;
  446. /* set platform specific polarity inversion */
  447. ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
  448. if (ret)
  449. goto out_failed;
  450. ret = pca953x_irq_setup(chip, id);
  451. if (ret)
  452. goto out_failed;
  453. ret = gpiochip_add(&chip->gpio_chip);
  454. if (ret)
  455. goto out_failed;
  456. if (pdata->setup) {
  457. ret = pdata->setup(client, chip->gpio_chip.base,
  458. chip->gpio_chip.ngpio, pdata->context);
  459. if (ret < 0)
  460. dev_warn(&client->dev, "setup failed, %d\n", ret);
  461. }
  462. i2c_set_clientdata(client, chip);
  463. return 0;
  464. out_failed:
  465. pca953x_irq_teardown(chip);
  466. kfree(chip->dyn_pdata);
  467. kfree(chip);
  468. return ret;
  469. }
  470. static int pca953x_remove(struct i2c_client *client)
  471. {
  472. struct pca953x_platform_data *pdata = client->dev.platform_data;
  473. struct pca953x_chip *chip = i2c_get_clientdata(client);
  474. int ret = 0;
  475. if (pdata->teardown) {
  476. ret = pdata->teardown(client, chip->gpio_chip.base,
  477. chip->gpio_chip.ngpio, pdata->context);
  478. if (ret < 0) {
  479. dev_err(&client->dev, "%s failed, %d\n",
  480. "teardown", ret);
  481. return ret;
  482. }
  483. }
  484. ret = gpiochip_remove(&chip->gpio_chip);
  485. if (ret) {
  486. dev_err(&client->dev, "%s failed, %d\n",
  487. "gpiochip_remove()", ret);
  488. return ret;
  489. }
  490. pca953x_irq_teardown(chip);
  491. kfree(chip->dyn_pdata);
  492. kfree(chip);
  493. return 0;
  494. }
  495. static struct i2c_driver pca953x_driver = {
  496. .driver = {
  497. .name = "pca953x",
  498. },
  499. .probe = pca953x_probe,
  500. .remove = pca953x_remove,
  501. .id_table = pca953x_id,
  502. };
  503. static int __init pca953x_init(void)
  504. {
  505. return i2c_add_driver(&pca953x_driver);
  506. }
  507. /* register after i2c postcore initcall and before
  508. * subsys initcalls that may rely on these GPIOs
  509. */
  510. subsys_initcall(pca953x_init);
  511. static void __exit pca953x_exit(void)
  512. {
  513. i2c_del_driver(&pca953x_driver);
  514. }
  515. module_exit(pca953x_exit);
  516. MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
  517. MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
  518. MODULE_LICENSE("GPL");