pca953x.c 14 KB

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