pca953x.c 6.9 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. /* This is temporary - in 2.6.26 i2c_driver_data should replace it. */
  23. struct pca953x_desc {
  24. char name[I2C_NAME_SIZE];
  25. unsigned long driver_data;
  26. };
  27. static const struct pca953x_desc pca953x_descs[] = {
  28. { "pca9534", 8, },
  29. { "pca9535", 16, },
  30. { "pca9536", 4, },
  31. { "pca9537", 4, },
  32. { "pca9538", 8, },
  33. { "pca9539", 16, },
  34. /* REVISIT several pca955x parts should work here too */
  35. };
  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. }
  154. static int __devinit pca953x_probe(struct i2c_client *client)
  155. {
  156. struct pca953x_platform_data *pdata;
  157. struct pca953x_chip *chip;
  158. int ret, i;
  159. const struct pca953x_desc *id = NULL;
  160. pdata = client->dev.platform_data;
  161. if (pdata == NULL)
  162. return -ENODEV;
  163. /* this loop vanishes when we get i2c_device_id */
  164. for (i = 0; i < ARRAY_SIZE(pca953x_descs); i++)
  165. if (!strcmp(pca953x_descs[i].name, client->name)) {
  166. id = pca953x_descs + i;
  167. break;
  168. }
  169. if (!id)
  170. return -ENODEV;
  171. chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
  172. if (chip == NULL)
  173. return -ENOMEM;
  174. chip->client = client;
  175. chip->gpio_start = pdata->gpio_base;
  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. };
  235. static int __init pca953x_init(void)
  236. {
  237. return i2c_add_driver(&pca953x_driver);
  238. }
  239. module_init(pca953x_init);
  240. static void __exit pca953x_exit(void)
  241. {
  242. i2c_del_driver(&pca953x_driver);
  243. }
  244. module_exit(pca953x_exit);
  245. MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
  246. MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
  247. MODULE_LICENSE("GPL");