pca953x.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. char **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. mutex_unlock(&chip->irq_lock);
  207. }
  208. static int pca953x_irq_set_type(unsigned int irq, unsigned int type)
  209. {
  210. struct pca953x_chip *chip = get_irq_chip_data(irq);
  211. uint16_t level = irq - chip->irq_base;
  212. uint16_t mask = 1 << level;
  213. if (!(type & IRQ_TYPE_EDGE_BOTH)) {
  214. dev_err(&chip->client->dev, "irq %d: unsupported type %d\n",
  215. irq, type);
  216. return -EINVAL;
  217. }
  218. if (type & IRQ_TYPE_EDGE_FALLING)
  219. chip->irq_trig_fall |= mask;
  220. else
  221. chip->irq_trig_fall &= ~mask;
  222. if (type & IRQ_TYPE_EDGE_RISING)
  223. chip->irq_trig_raise |= mask;
  224. else
  225. chip->irq_trig_raise &= ~mask;
  226. return pca953x_gpio_direction_input(&chip->gpio_chip, level);
  227. }
  228. static struct irq_chip pca953x_irq_chip = {
  229. .name = "pca953x",
  230. .mask = pca953x_irq_mask,
  231. .unmask = pca953x_irq_unmask,
  232. .bus_lock = pca953x_irq_bus_lock,
  233. .bus_sync_unlock = pca953x_irq_bus_sync_unlock,
  234. .set_type = pca953x_irq_set_type,
  235. };
  236. static uint16_t pca953x_irq_pending(struct pca953x_chip *chip)
  237. {
  238. uint16_t cur_stat;
  239. uint16_t old_stat;
  240. uint16_t pending;
  241. uint16_t trigger;
  242. int ret;
  243. ret = pca953x_read_reg(chip, PCA953X_INPUT, &cur_stat);
  244. if (ret)
  245. return 0;
  246. /* Remove output pins from the equation */
  247. cur_stat &= chip->reg_direction;
  248. old_stat = chip->irq_stat;
  249. trigger = (cur_stat ^ old_stat) & chip->irq_mask;
  250. if (!trigger)
  251. return 0;
  252. chip->irq_stat = cur_stat;
  253. pending = (old_stat & chip->irq_trig_fall) |
  254. (cur_stat & chip->irq_trig_raise);
  255. pending &= trigger;
  256. return pending;
  257. }
  258. static irqreturn_t pca953x_irq_handler(int irq, void *devid)
  259. {
  260. struct pca953x_chip *chip = devid;
  261. uint16_t pending;
  262. uint16_t level;
  263. pending = pca953x_irq_pending(chip);
  264. if (!pending)
  265. return IRQ_HANDLED;
  266. do {
  267. level = __ffs(pending);
  268. handle_nested_irq(level + chip->irq_base);
  269. pending &= ~(1 << level);
  270. } while (pending);
  271. return IRQ_HANDLED;
  272. }
  273. static int pca953x_irq_setup(struct pca953x_chip *chip,
  274. const struct i2c_device_id *id)
  275. {
  276. struct i2c_client *client = chip->client;
  277. struct pca953x_platform_data *pdata = client->dev.platform_data;
  278. int ret;
  279. if (pdata->irq_base && (id->driver_data & PCA953X_INT)) {
  280. int lvl;
  281. ret = pca953x_read_reg(chip, PCA953X_INPUT,
  282. &chip->irq_stat);
  283. if (ret)
  284. goto out_failed;
  285. /*
  286. * There is no way to know which GPIO line generated the
  287. * interrupt. We have to rely on the previous read for
  288. * this purpose.
  289. */
  290. chip->irq_stat &= chip->reg_direction;
  291. chip->irq_base = pdata->irq_base;
  292. mutex_init(&chip->irq_lock);
  293. for (lvl = 0; lvl < chip->gpio_chip.ngpio; lvl++) {
  294. int irq = lvl + chip->irq_base;
  295. set_irq_chip_data(irq, chip);
  296. set_irq_chip_and_handler(irq, &pca953x_irq_chip,
  297. handle_edge_irq);
  298. set_irq_nested_thread(irq, 1);
  299. #ifdef CONFIG_ARM
  300. set_irq_flags(irq, IRQF_VALID);
  301. #else
  302. set_irq_noprobe(irq);
  303. #endif
  304. }
  305. ret = request_threaded_irq(client->irq,
  306. NULL,
  307. pca953x_irq_handler,
  308. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  309. dev_name(&client->dev), chip);
  310. if (ret) {
  311. dev_err(&client->dev, "failed to request irq %d\n",
  312. client->irq);
  313. goto out_failed;
  314. }
  315. chip->gpio_chip.to_irq = pca953x_gpio_to_irq;
  316. }
  317. return 0;
  318. out_failed:
  319. chip->irq_base = 0;
  320. return ret;
  321. }
  322. static void pca953x_irq_teardown(struct pca953x_chip *chip)
  323. {
  324. if (chip->irq_base)
  325. free_irq(chip->client->irq, chip);
  326. }
  327. #else /* CONFIG_GPIO_PCA953X_IRQ */
  328. static int pca953x_irq_setup(struct pca953x_chip *chip,
  329. const struct i2c_device_id *id)
  330. {
  331. struct i2c_client *client = chip->client;
  332. struct pca953x_platform_data *pdata = client->dev.platform_data;
  333. if (pdata->irq_base && (id->driver_data & PCA953X_INT))
  334. dev_warn(&client->dev, "interrupt support not compiled in\n");
  335. return 0;
  336. }
  337. static void pca953x_irq_teardown(struct pca953x_chip *chip)
  338. {
  339. }
  340. #endif
  341. /*
  342. * Handlers for alternative sources of platform_data
  343. */
  344. #ifdef CONFIG_OF_GPIO
  345. /*
  346. * Translate OpenFirmware node properties into platform_data
  347. */
  348. static struct pca953x_platform_data *
  349. pca953x_get_alt_pdata(struct i2c_client *client)
  350. {
  351. struct pca953x_platform_data *pdata;
  352. struct device_node *node;
  353. const uint16_t *val;
  354. node = dev_archdata_get_node(&client->dev.archdata);
  355. if (node == NULL)
  356. return NULL;
  357. pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
  358. if (pdata == NULL) {
  359. dev_err(&client->dev, "Unable to allocate platform_data\n");
  360. return NULL;
  361. }
  362. pdata->gpio_base = -1;
  363. val = of_get_property(node, "linux,gpio-base", NULL);
  364. if (val) {
  365. if (*val < 0)
  366. dev_warn(&client->dev,
  367. "invalid gpio-base in device tree\n");
  368. else
  369. pdata->gpio_base = *val;
  370. }
  371. val = of_get_property(node, "polarity", NULL);
  372. if (val)
  373. pdata->invert = *val;
  374. return pdata;
  375. }
  376. #else
  377. static struct pca953x_platform_data *
  378. pca953x_get_alt_pdata(struct i2c_client *client)
  379. {
  380. return NULL;
  381. }
  382. #endif
  383. static int __devinit pca953x_probe(struct i2c_client *client,
  384. const struct i2c_device_id *id)
  385. {
  386. struct pca953x_platform_data *pdata;
  387. struct pca953x_chip *chip;
  388. int ret;
  389. chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
  390. if (chip == NULL)
  391. return -ENOMEM;
  392. pdata = client->dev.platform_data;
  393. if (pdata == NULL) {
  394. pdata = pca953x_get_alt_pdata(client);
  395. /*
  396. * Unlike normal platform_data, this is allocated
  397. * dynamically and must be freed in the driver
  398. */
  399. chip->dyn_pdata = pdata;
  400. }
  401. if (pdata == NULL) {
  402. dev_dbg(&client->dev, "no platform data\n");
  403. ret = -EINVAL;
  404. goto out_failed;
  405. }
  406. chip->client = client;
  407. chip->gpio_start = pdata->gpio_base;
  408. chip->names = pdata->names;
  409. /* initialize cached registers from their original values.
  410. * we can't share this chip with another i2c master.
  411. */
  412. pca953x_setup_gpio(chip, id->driver_data & PCA953X_GPIOS);
  413. ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
  414. if (ret)
  415. goto out_failed;
  416. ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
  417. if (ret)
  418. goto out_failed;
  419. /* set platform specific polarity inversion */
  420. ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
  421. if (ret)
  422. goto out_failed;
  423. ret = pca953x_irq_setup(chip, id);
  424. if (ret)
  425. goto out_failed;
  426. ret = gpiochip_add(&chip->gpio_chip);
  427. if (ret)
  428. goto out_failed;
  429. if (pdata->setup) {
  430. ret = pdata->setup(client, chip->gpio_chip.base,
  431. chip->gpio_chip.ngpio, pdata->context);
  432. if (ret < 0)
  433. dev_warn(&client->dev, "setup failed, %d\n", ret);
  434. }
  435. i2c_set_clientdata(client, chip);
  436. return 0;
  437. out_failed:
  438. pca953x_irq_teardown(chip);
  439. kfree(chip->dyn_pdata);
  440. kfree(chip);
  441. return ret;
  442. }
  443. static int pca953x_remove(struct i2c_client *client)
  444. {
  445. struct pca953x_platform_data *pdata = client->dev.platform_data;
  446. struct pca953x_chip *chip = i2c_get_clientdata(client);
  447. int ret = 0;
  448. if (pdata->teardown) {
  449. ret = pdata->teardown(client, chip->gpio_chip.base,
  450. chip->gpio_chip.ngpio, pdata->context);
  451. if (ret < 0) {
  452. dev_err(&client->dev, "%s failed, %d\n",
  453. "teardown", ret);
  454. return ret;
  455. }
  456. }
  457. ret = gpiochip_remove(&chip->gpio_chip);
  458. if (ret) {
  459. dev_err(&client->dev, "%s failed, %d\n",
  460. "gpiochip_remove()", ret);
  461. return ret;
  462. }
  463. pca953x_irq_teardown(chip);
  464. kfree(chip->dyn_pdata);
  465. kfree(chip);
  466. return 0;
  467. }
  468. static struct i2c_driver pca953x_driver = {
  469. .driver = {
  470. .name = "pca953x",
  471. },
  472. .probe = pca953x_probe,
  473. .remove = pca953x_remove,
  474. .id_table = pca953x_id,
  475. };
  476. static int __init pca953x_init(void)
  477. {
  478. return i2c_add_driver(&pca953x_driver);
  479. }
  480. /* register after i2c postcore initcall and before
  481. * subsys initcalls that may rely on these GPIOs
  482. */
  483. subsys_initcall(pca953x_init);
  484. static void __exit pca953x_exit(void)
  485. {
  486. i2c_del_driver(&pca953x_driver);
  487. }
  488. module_exit(pca953x_exit);
  489. MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
  490. MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
  491. MODULE_LICENSE("GPL");