pca953x.c 6.6 KB

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