pca953x.c 6.6 KB

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