pca953x.c 13 KB

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