pca953x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. handle_nested_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 && (id->driver_data & PCA953X_INT)) {
  290. int lvl;
  291. ret = pca953x_read_reg(chip, PCA953X_INPUT,
  292. &chip->irq_stat);
  293. if (ret)
  294. goto out_failed;
  295. /*
  296. * There is no way to know which GPIO line generated the
  297. * interrupt. We have to rely on the previous read for
  298. * this purpose.
  299. */
  300. chip->irq_stat &= chip->reg_direction;
  301. chip->irq_base = pdata->irq_base;
  302. mutex_init(&chip->irq_lock);
  303. for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
  304. int irq = lvl + chip->irq_base;
  305. set_irq_chip_data(irq, chip);
  306. set_irq_chip_and_handler(irq, &pca953x_irq_chip,
  307. handle_edge_irq);
  308. set_irq_nested_thread(irq, 1);
  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_FALLING | IRQF_ONESHOT,
  319. dev_name(&client->dev), chip);
  320. if (ret) {
  321. dev_err(&client->dev, "failed to request irq %d\n",
  322. client->irq);
  323. goto out_failed;
  324. }
  325. chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
  326. }
  327. return 0;
  328. out_failed:
  329. chip->irq_base = 0;
  330. return ret;
  331. }
  332. static void pca953x_irq_teardown(struct pca953x_chip *chip)
  333. {
  334. if (chip->irq_base)
  335. free_irq(chip->client->irq, chip);
  336. }
  337. #else /* CONFIG_GPIO_PCA953X_IRQ */
  338. static int pca953x_irq_setup(struct pca953x_chip *chip,
  339. const struct i2c_device_id *id)
  340. {
  341. struct i2c_client *client = chip->client;
  342. struct pca953x_platform_data *pdata = client->dev.platform_data;
  343. if (pdata->irq_base && (id->driver_data & PCA953X_INT))
  344. dev_warn(&client->dev, "interrupt support not compiled in\n");
  345. return 0;
  346. }
  347. static void pca953x_irq_teardown(struct pca953x_chip *chip)
  348. {
  349. }
  350. #endif
  351. /*
  352. * Handlers for alternative sources of platform_data
  353. */
  354. #ifdef CONFIG_OF_GPIO
  355. /*
  356. * Translate OpenFirmware node properties into platform_data
  357. */
  358. static struct pca953x_platform_data *
  359. pca953x_get_alt_pdata(struct i2c_client *client)
  360. {
  361. struct pca953x_platform_data *pdata;
  362. struct device_node *node;
  363. const uint16_t *val;
  364. node = client->dev.of_node;
  365. if (node == NULL)
  366. return NULL;
  367. pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
  368. if (pdata == NULL) {
  369. dev_err(&client->dev, "Unable to allocate platform_data\n");
  370. return NULL;
  371. }
  372. pdata->gpio_base = -1;
  373. val = of_get_property(node, "linux,gpio-base", NULL);
  374. if (val) {
  375. if (*val < 0)
  376. dev_warn(&client->dev,
  377. "invalid gpio-base in device tree\n");
  378. else
  379. pdata->gpio_base = *val;
  380. }
  381. val = of_get_property(node, "polarity", NULL);
  382. if (val)
  383. pdata->invert = *val;
  384. return pdata;
  385. }
  386. #else
  387. static struct pca953x_platform_data *
  388. pca953x_get_alt_pdata(struct i2c_client *client)
  389. {
  390. return NULL;
  391. }
  392. #endif
  393. static int __devinit pca953x_probe(struct i2c_client *client,
  394. const struct i2c_device_id *id)
  395. {
  396. struct pca953x_platform_data *pdata;
  397. struct pca953x_chip *chip;
  398. int ret;
  399. chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
  400. if (chip == NULL)
  401. return -ENOMEM;
  402. pdata = client->dev.platform_data;
  403. if (pdata == NULL) {
  404. pdata = pca953x_get_alt_pdata(client);
  405. /*
  406. * Unlike normal platform_data, this is allocated
  407. * dynamically and must be freed in the driver
  408. */
  409. chip->dyn_pdata = pdata;
  410. }
  411. if (pdata == NULL) {
  412. dev_dbg(&client->dev, "no platform data\n");
  413. ret = -EINVAL;
  414. goto out_failed;
  415. }
  416. chip->client = client;
  417. chip->gpio_start = pdata->gpio_base;
  418. chip->names = pdata->names;
  419. /* initialize cached registers from their original values.
  420. * we can't share this chip with another i2c master.
  421. */
  422. pca953x_setup_gpio(chip, id->driver_data & PCA953X_GPIOS);
  423. ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
  424. if (ret)
  425. goto out_failed;
  426. ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
  427. if (ret)
  428. goto out_failed;
  429. /* set platform specific polarity inversion */
  430. ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
  431. if (ret)
  432. goto out_failed;
  433. ret = pca953x_irq_setup(chip, id);
  434. if (ret)
  435. goto out_failed;
  436. ret = gpiochip_add(&chip->gpio_chip);
  437. if (ret)
  438. goto out_failed;
  439. if (pdata->setup) {
  440. ret = pdata->setup(client, chip->gpio_chip.base,
  441. chip->gpio_chip.ngpio, pdata->context);
  442. if (ret < 0)
  443. dev_warn(&client->dev, "setup failed, %d\n", ret);
  444. }
  445. i2c_set_clientdata(client, chip);
  446. return 0;
  447. out_failed:
  448. pca953x_irq_teardown(chip);
  449. kfree(chip->dyn_pdata);
  450. kfree(chip);
  451. return ret;
  452. }
  453. static int pca953x_remove(struct i2c_client *client)
  454. {
  455. struct pca953x_platform_data *pdata = client->dev.platform_data;
  456. struct pca953x_chip *chip = i2c_get_clientdata(client);
  457. int ret = 0;
  458. if (pdata->teardown) {
  459. ret = pdata->teardown(client, chip->gpio_chip.base,
  460. chip->gpio_chip.ngpio, pdata->context);
  461. if (ret < 0) {
  462. dev_err(&client->dev, "%s failed, %d\n",
  463. "teardown", ret);
  464. return ret;
  465. }
  466. }
  467. ret = gpiochip_remove(&chip->gpio_chip);
  468. if (ret) {
  469. dev_err(&client->dev, "%s failed, %d\n",
  470. "gpiochip_remove()", ret);
  471. return ret;
  472. }
  473. pca953x_irq_teardown(chip);
  474. kfree(chip->dyn_pdata);
  475. kfree(chip);
  476. return 0;
  477. }
  478. static struct i2c_driver pca953x_driver = {
  479. .driver = {
  480. .name = "pca953x",
  481. },
  482. .probe = pca953x_probe,
  483. .remove = pca953x_remove,
  484. .id_table = pca953x_id,
  485. };
  486. static int __init pca953x_init(void)
  487. {
  488. return i2c_add_driver(&pca953x_driver);
  489. }
  490. /* register after i2c postcore initcall and before
  491. * subsys initcalls that may rely on these GPIOs
  492. */
  493. subsys_initcall(pca953x_init);
  494. static void __exit pca953x_exit(void)
  495. {
  496. i2c_del_driver(&pca953x_driver);
  497. }
  498. module_exit(pca953x_exit);
  499. MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
  500. MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
  501. MODULE_LICENSE("GPL");