pca953x.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/i2c.h>
  16. #include <linux/i2c/pca953x.h>
  17. #include <asm/gpio.h>
  18. #define PCA953X_INPUT 0
  19. #define PCA953X_OUTPUT 1
  20. #define PCA953X_INVERT 2
  21. #define PCA953X_DIRECTION 3
  22. static const struct i2c_device_id pca953x_id[] = {
  23. { "pca9534", 8, },
  24. { "pca9535", 16, },
  25. { "pca9536", 4, },
  26. { "pca9537", 4, },
  27. { "pca9538", 8, },
  28. { "pca9539", 16, },
  29. { "pca9554", 8, },
  30. { "pca9555", 16, },
  31. { "pca9557", 8, },
  32. { "max7310", 8, },
  33. { "pca6107", 8, },
  34. { "tca6408", 8, },
  35. { "tca6416", 16, },
  36. /* NYET: { "tca6424", 24, }, */
  37. { }
  38. };
  39. MODULE_DEVICE_TABLE(i2c, pca953x_id);
  40. struct pca953x_chip {
  41. unsigned gpio_start;
  42. uint16_t reg_output;
  43. uint16_t reg_direction;
  44. struct i2c_client *client;
  45. struct gpio_chip gpio_chip;
  46. char **names;
  47. };
  48. static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
  49. {
  50. int ret;
  51. if (chip->gpio_chip.ngpio <= 8)
  52. ret = i2c_smbus_write_byte_data(chip->client, reg, val);
  53. else
  54. ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
  55. if (ret < 0) {
  56. dev_err(&chip->client->dev, "failed writing register\n");
  57. return ret;
  58. }
  59. return 0;
  60. }
  61. static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
  62. {
  63. int ret;
  64. if (chip->gpio_chip.ngpio <= 8)
  65. ret = i2c_smbus_read_byte_data(chip->client, reg);
  66. else
  67. ret = i2c_smbus_read_word_data(chip->client, reg << 1);
  68. if (ret < 0) {
  69. dev_err(&chip->client->dev, "failed reading register\n");
  70. return ret;
  71. }
  72. *val = (uint16_t)ret;
  73. return 0;
  74. }
  75. static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  76. {
  77. struct pca953x_chip *chip;
  78. uint16_t reg_val;
  79. int ret;
  80. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  81. reg_val = chip->reg_direction | (1u << off);
  82. ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
  83. if (ret)
  84. return ret;
  85. chip->reg_direction = reg_val;
  86. return 0;
  87. }
  88. static int pca953x_gpio_direction_output(struct gpio_chip *gc,
  89. unsigned off, int val)
  90. {
  91. struct pca953x_chip *chip;
  92. uint16_t reg_val;
  93. int ret;
  94. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  95. /* set output level */
  96. if (val)
  97. reg_val = chip->reg_output | (1u << off);
  98. else
  99. reg_val = chip->reg_output & ~(1u << off);
  100. ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
  101. if (ret)
  102. return ret;
  103. chip->reg_output = reg_val;
  104. /* then direction */
  105. reg_val = chip->reg_direction & ~(1u << off);
  106. ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
  107. if (ret)
  108. return ret;
  109. chip->reg_direction = reg_val;
  110. return 0;
  111. }
  112. static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
  113. {
  114. struct pca953x_chip *chip;
  115. uint16_t reg_val;
  116. int ret;
  117. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  118. ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
  119. if (ret < 0) {
  120. /* NOTE: diagnostic already emitted; that's all we should
  121. * do unless gpio_*_value_cansleep() calls become different
  122. * from their nonsleeping siblings (and report faults).
  123. */
  124. return 0;
  125. }
  126. return (reg_val & (1u << off)) ? 1 : 0;
  127. }
  128. static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  129. {
  130. struct pca953x_chip *chip;
  131. uint16_t reg_val;
  132. int ret;
  133. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  134. if (val)
  135. reg_val = chip->reg_output | (1u << off);
  136. else
  137. reg_val = chip->reg_output & ~(1u << off);
  138. ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
  139. if (ret)
  140. return;
  141. chip->reg_output = reg_val;
  142. }
  143. static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
  144. {
  145. struct gpio_chip *gc;
  146. gc = &chip->gpio_chip;
  147. gc->direction_input = pca953x_gpio_direction_input;
  148. gc->direction_output = pca953x_gpio_direction_output;
  149. gc->get = pca953x_gpio_get_value;
  150. gc->set = pca953x_gpio_set_value;
  151. gc->can_sleep = 1;
  152. gc->base = chip->gpio_start;
  153. gc->ngpio = gpios;
  154. gc->label = chip->client->name;
  155. gc->dev = &chip->client->dev;
  156. gc->owner = THIS_MODULE;
  157. gc->names = chip->names;
  158. }
  159. static int __devinit pca953x_probe(struct i2c_client *client,
  160. const struct i2c_device_id *id)
  161. {
  162. struct pca953x_platform_data *pdata;
  163. struct pca953x_chip *chip;
  164. int ret;
  165. pdata = client->dev.platform_data;
  166. if (pdata == NULL) {
  167. dev_dbg(&client->dev, "no platform data\n");
  168. return -EINVAL;
  169. }
  170. chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
  171. if (chip == NULL)
  172. return -ENOMEM;
  173. chip->client = client;
  174. chip->gpio_start = pdata->gpio_base;
  175. chip->names = pdata->names;
  176. /* initialize cached registers from their original values.
  177. * we can't share this chip with another i2c master.
  178. */
  179. pca953x_setup_gpio(chip, id->driver_data);
  180. ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
  181. if (ret)
  182. goto out_failed;
  183. ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
  184. if (ret)
  185. goto out_failed;
  186. /* set platform specific polarity inversion */
  187. ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
  188. if (ret)
  189. goto out_failed;
  190. ret = gpiochip_add(&chip->gpio_chip);
  191. if (ret)
  192. goto out_failed;
  193. if (pdata->setup) {
  194. ret = pdata->setup(client, chip->gpio_chip.base,
  195. chip->gpio_chip.ngpio, pdata->context);
  196. if (ret < 0)
  197. dev_warn(&client->dev, "setup failed, %d\n", ret);
  198. }
  199. i2c_set_clientdata(client, chip);
  200. return 0;
  201. out_failed:
  202. kfree(chip);
  203. return ret;
  204. }
  205. static int pca953x_remove(struct i2c_client *client)
  206. {
  207. struct pca953x_platform_data *pdata = client->dev.platform_data;
  208. struct pca953x_chip *chip = i2c_get_clientdata(client);
  209. int ret = 0;
  210. if (pdata->teardown) {
  211. ret = pdata->teardown(client, chip->gpio_chip.base,
  212. chip->gpio_chip.ngpio, pdata->context);
  213. if (ret < 0) {
  214. dev_err(&client->dev, "%s failed, %d\n",
  215. "teardown", ret);
  216. return ret;
  217. }
  218. }
  219. ret = gpiochip_remove(&chip->gpio_chip);
  220. if (ret) {
  221. dev_err(&client->dev, "%s failed, %d\n",
  222. "gpiochip_remove()", ret);
  223. return ret;
  224. }
  225. kfree(chip);
  226. return 0;
  227. }
  228. static struct i2c_driver pca953x_driver = {
  229. .driver = {
  230. .name = "pca953x",
  231. },
  232. .probe = pca953x_probe,
  233. .remove = pca953x_remove,
  234. .id_table = pca953x_id,
  235. };
  236. static int __init pca953x_init(void)
  237. {
  238. return i2c_add_driver(&pca953x_driver);
  239. }
  240. /* register after i2c postcore initcall and before
  241. * subsys initcalls that may rely on these GPIOs
  242. */
  243. subsys_initcall(pca953x_init);
  244. static void __exit pca953x_exit(void)
  245. {
  246. i2c_del_driver(&pca953x_driver);
  247. }
  248. module_exit(pca953x_exit);
  249. MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
  250. MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
  251. MODULE_LICENSE("GPL");