pca953x.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * pca9539.c - 16-bit I/O port with interrupt and reset
  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/i2c.h>
  16. #include <linux/i2c/pca953x.h>
  17. #include <asm/gpio.h>
  18. #define NR_PCA9539_GPIOS 16
  19. #define PCA9539_INPUT 0
  20. #define PCA9539_OUTPUT 2
  21. #define PCA9539_INVERT 4
  22. #define PCA9539_DIRECTION 6
  23. struct pca9539_chip {
  24. unsigned gpio_start;
  25. uint16_t reg_output;
  26. uint16_t reg_direction;
  27. struct i2c_client *client;
  28. struct gpio_chip gpio_chip;
  29. };
  30. /* NOTE: we can't currently rely on fault codes to come from SMBus
  31. * calls, so we map all errors to EIO here and return zero otherwise.
  32. */
  33. static int pca9539_write_reg(struct pca9539_chip *chip, int reg, uint16_t val)
  34. {
  35. if (i2c_smbus_write_word_data(chip->client, reg, val) < 0)
  36. return -EIO;
  37. else
  38. return 0;
  39. }
  40. static int pca9539_read_reg(struct pca9539_chip *chip, int reg, uint16_t *val)
  41. {
  42. int ret;
  43. ret = i2c_smbus_read_word_data(chip->client, reg);
  44. if (ret < 0) {
  45. dev_err(&chip->client->dev, "failed reading register\n");
  46. return -EIO;
  47. }
  48. *val = (uint16_t)ret;
  49. return 0;
  50. }
  51. static int pca9539_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  52. {
  53. struct pca9539_chip *chip;
  54. uint16_t reg_val;
  55. int ret;
  56. chip = container_of(gc, struct pca9539_chip, gpio_chip);
  57. reg_val = chip->reg_direction | (1u << off);
  58. ret = pca9539_write_reg(chip, PCA9539_DIRECTION, reg_val);
  59. if (ret)
  60. return ret;
  61. chip->reg_direction = reg_val;
  62. return 0;
  63. }
  64. static int pca9539_gpio_direction_output(struct gpio_chip *gc,
  65. unsigned off, int val)
  66. {
  67. struct pca9539_chip *chip;
  68. uint16_t reg_val;
  69. int ret;
  70. chip = container_of(gc, struct pca9539_chip, gpio_chip);
  71. /* set output level */
  72. if (val)
  73. reg_val = chip->reg_output | (1u << off);
  74. else
  75. reg_val = chip->reg_output & ~(1u << off);
  76. ret = pca9539_write_reg(chip, PCA9539_OUTPUT, reg_val);
  77. if (ret)
  78. return ret;
  79. chip->reg_output = reg_val;
  80. /* then direction */
  81. reg_val = chip->reg_direction & ~(1u << off);
  82. ret = pca9539_write_reg(chip, PCA9539_DIRECTION, reg_val);
  83. if (ret)
  84. return ret;
  85. chip->reg_direction = reg_val;
  86. return 0;
  87. }
  88. static int pca9539_gpio_get_value(struct gpio_chip *gc, unsigned off)
  89. {
  90. struct pca9539_chip *chip;
  91. uint16_t reg_val;
  92. int ret;
  93. chip = container_of(gc, struct pca9539_chip, gpio_chip);
  94. ret = pca9539_read_reg(chip, PCA9539_INPUT, &reg_val);
  95. if (ret < 0) {
  96. /* NOTE: diagnostic already emitted; that's all we should
  97. * do unless gpio_*_value_cansleep() calls become different
  98. * from their nonsleeping siblings (and report faults).
  99. */
  100. return 0;
  101. }
  102. return (reg_val & (1u << off)) ? 1 : 0;
  103. }
  104. static void pca9539_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  105. {
  106. struct pca9539_chip *chip;
  107. uint16_t reg_val;
  108. int ret;
  109. chip = container_of(gc, struct pca9539_chip, gpio_chip);
  110. if (val)
  111. reg_val = chip->reg_output | (1u << off);
  112. else
  113. reg_val = chip->reg_output & ~(1u << off);
  114. ret = pca9539_write_reg(chip, PCA9539_OUTPUT, reg_val);
  115. if (ret)
  116. return;
  117. chip->reg_output = reg_val;
  118. }
  119. static int pca9539_init_gpio(struct pca9539_chip *chip)
  120. {
  121. struct gpio_chip *gc;
  122. gc = &chip->gpio_chip;
  123. gc->direction_input = pca9539_gpio_direction_input;
  124. gc->direction_output = pca9539_gpio_direction_output;
  125. gc->get = pca9539_gpio_get_value;
  126. gc->set = pca9539_gpio_set_value;
  127. gc->base = chip->gpio_start;
  128. gc->ngpio = NR_PCA9539_GPIOS;
  129. gc->label = "pca9539";
  130. return gpiochip_add(gc);
  131. }
  132. static int __devinit pca9539_probe(struct i2c_client *client)
  133. {
  134. struct pca9539_platform_data *pdata;
  135. struct pca9539_chip *chip;
  136. int ret;
  137. pdata = client->dev.platform_data;
  138. if (pdata == NULL)
  139. return -ENODEV;
  140. chip = kzalloc(sizeof(struct pca9539_chip), GFP_KERNEL);
  141. if (chip == NULL)
  142. return -ENOMEM;
  143. chip->client = client;
  144. chip->gpio_start = pdata->gpio_base;
  145. /* initialize cached registers from their original values.
  146. * we can't share this chip with another i2c master.
  147. */
  148. ret = pca9539_read_reg(chip, PCA9539_OUTPUT, &chip->reg_output);
  149. if (ret)
  150. goto out_failed;
  151. ret = pca9539_read_reg(chip, PCA9539_DIRECTION, &chip->reg_direction);
  152. if (ret)
  153. goto out_failed;
  154. /* set platform specific polarity inversion */
  155. ret = pca9539_write_reg(chip, PCA9539_INVERT, pdata->invert);
  156. if (ret)
  157. goto out_failed;
  158. ret = pca9539_init_gpio(chip);
  159. if (ret)
  160. goto out_failed;
  161. if (pdata->setup) {
  162. ret = pdata->setup(client, chip->gpio_chip.base,
  163. chip->gpio_chip.ngpio, pdata->context);
  164. if (ret < 0)
  165. dev_warn(&client->dev, "setup failed, %d\n", ret);
  166. }
  167. i2c_set_clientdata(client, chip);
  168. return 0;
  169. out_failed:
  170. kfree(chip);
  171. return ret;
  172. }
  173. static int pca9539_remove(struct i2c_client *client)
  174. {
  175. struct pca9539_platform_data *pdata = client->dev.platform_data;
  176. struct pca9539_chip *chip = i2c_get_clientdata(client);
  177. int ret = 0;
  178. if (pdata->teardown) {
  179. ret = pdata->teardown(client, chip->gpio_chip.base,
  180. chip->gpio_chip.ngpio, pdata->context);
  181. if (ret < 0) {
  182. dev_err(&client->dev, "%s failed, %d\n",
  183. "teardown", ret);
  184. return ret;
  185. }
  186. }
  187. ret = gpiochip_remove(&chip->gpio_chip);
  188. if (ret) {
  189. dev_err(&client->dev, "%s failed, %d\n",
  190. "gpiochip_remove()", ret);
  191. return ret;
  192. }
  193. kfree(chip);
  194. return 0;
  195. }
  196. static struct i2c_driver pca9539_driver = {
  197. .driver = {
  198. .name = "pca9539",
  199. },
  200. .probe = pca9539_probe,
  201. .remove = pca9539_remove,
  202. };
  203. static int __init pca9539_init(void)
  204. {
  205. return i2c_add_driver(&pca9539_driver);
  206. }
  207. module_init(pca9539_init);
  208. static void __exit pca9539_exit(void)
  209. {
  210. i2c_del_driver(&pca9539_driver);
  211. }
  212. module_exit(pca9539_exit);
  213. MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
  214. MODULE_DESCRIPTION("GPIO expander driver for PCA9539");
  215. MODULE_LICENSE("GPL");