pca953x.c 6.6 KB

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