pca953x.c 6.7 KB

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