pca953x.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. #ifdef CONFIG_OF_GPIO
  18. #include <linux/of_platform.h>
  19. #include <linux/of_gpio.h>
  20. #endif
  21. #include <asm/gpio.h>
  22. #define PCA953X_INPUT 0
  23. #define PCA953X_OUTPUT 1
  24. #define PCA953X_INVERT 2
  25. #define PCA953X_DIRECTION 3
  26. static const struct i2c_device_id pca953x_id[] = {
  27. { "pca9534", 8, },
  28. { "pca9535", 16, },
  29. { "pca9536", 4, },
  30. { "pca9537", 4, },
  31. { "pca9538", 8, },
  32. { "pca9539", 16, },
  33. { "pca9554", 8, },
  34. { "pca9555", 16, },
  35. { "pca9556", 8, },
  36. { "pca9557", 8, },
  37. { "max7310", 8, },
  38. { "pca6107", 8, },
  39. { "tca6408", 8, },
  40. { "tca6416", 16, },
  41. /* NYET: { "tca6424", 24, }, */
  42. { }
  43. };
  44. MODULE_DEVICE_TABLE(i2c, pca953x_id);
  45. struct pca953x_chip {
  46. unsigned gpio_start;
  47. uint16_t reg_output;
  48. uint16_t reg_direction;
  49. struct i2c_client *client;
  50. struct pca953x_platform_data *dyn_pdata;
  51. struct gpio_chip gpio_chip;
  52. char **names;
  53. };
  54. static int pca953x_write_reg(struct pca953x_chip *chip, int reg, uint16_t val)
  55. {
  56. int ret;
  57. if (chip->gpio_chip.ngpio <= 8)
  58. ret = i2c_smbus_write_byte_data(chip->client, reg, val);
  59. else
  60. ret = i2c_smbus_write_word_data(chip->client, reg << 1, val);
  61. if (ret < 0) {
  62. dev_err(&chip->client->dev, "failed writing register\n");
  63. return ret;
  64. }
  65. return 0;
  66. }
  67. static int pca953x_read_reg(struct pca953x_chip *chip, int reg, uint16_t *val)
  68. {
  69. int ret;
  70. if (chip->gpio_chip.ngpio <= 8)
  71. ret = i2c_smbus_read_byte_data(chip->client, reg);
  72. else
  73. ret = i2c_smbus_read_word_data(chip->client, reg << 1);
  74. if (ret < 0) {
  75. dev_err(&chip->client->dev, "failed reading register\n");
  76. return ret;
  77. }
  78. *val = (uint16_t)ret;
  79. return 0;
  80. }
  81. static int pca953x_gpio_direction_input(struct gpio_chip *gc, unsigned off)
  82. {
  83. struct pca953x_chip *chip;
  84. uint16_t reg_val;
  85. int ret;
  86. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  87. reg_val = chip->reg_direction | (1u << off);
  88. ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
  89. if (ret)
  90. return ret;
  91. chip->reg_direction = reg_val;
  92. return 0;
  93. }
  94. static int pca953x_gpio_direction_output(struct gpio_chip *gc,
  95. unsigned off, int val)
  96. {
  97. struct pca953x_chip *chip;
  98. uint16_t reg_val;
  99. int ret;
  100. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  101. /* set output level */
  102. if (val)
  103. reg_val = chip->reg_output | (1u << off);
  104. else
  105. reg_val = chip->reg_output & ~(1u << off);
  106. ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
  107. if (ret)
  108. return ret;
  109. chip->reg_output = reg_val;
  110. /* then direction */
  111. reg_val = chip->reg_direction & ~(1u << off);
  112. ret = pca953x_write_reg(chip, PCA953X_DIRECTION, reg_val);
  113. if (ret)
  114. return ret;
  115. chip->reg_direction = reg_val;
  116. return 0;
  117. }
  118. static int pca953x_gpio_get_value(struct gpio_chip *gc, unsigned off)
  119. {
  120. struct pca953x_chip *chip;
  121. uint16_t reg_val;
  122. int ret;
  123. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  124. ret = pca953x_read_reg(chip, PCA953X_INPUT, &reg_val);
  125. if (ret < 0) {
  126. /* NOTE: diagnostic already emitted; that's all we should
  127. * do unless gpio_*_value_cansleep() calls become different
  128. * from their nonsleeping siblings (and report faults).
  129. */
  130. return 0;
  131. }
  132. return (reg_val & (1u << off)) ? 1 : 0;
  133. }
  134. static void pca953x_gpio_set_value(struct gpio_chip *gc, unsigned off, int val)
  135. {
  136. struct pca953x_chip *chip;
  137. uint16_t reg_val;
  138. int ret;
  139. chip = container_of(gc, struct pca953x_chip, gpio_chip);
  140. if (val)
  141. reg_val = chip->reg_output | (1u << off);
  142. else
  143. reg_val = chip->reg_output & ~(1u << off);
  144. ret = pca953x_write_reg(chip, PCA953X_OUTPUT, reg_val);
  145. if (ret)
  146. return;
  147. chip->reg_output = reg_val;
  148. }
  149. static void pca953x_setup_gpio(struct pca953x_chip *chip, int gpios)
  150. {
  151. struct gpio_chip *gc;
  152. gc = &chip->gpio_chip;
  153. gc->direction_input = pca953x_gpio_direction_input;
  154. gc->direction_output = pca953x_gpio_direction_output;
  155. gc->get = pca953x_gpio_get_value;
  156. gc->set = pca953x_gpio_set_value;
  157. gc->can_sleep = 1;
  158. gc->base = chip->gpio_start;
  159. gc->ngpio = gpios;
  160. gc->label = chip->client->name;
  161. gc->dev = &chip->client->dev;
  162. gc->owner = THIS_MODULE;
  163. gc->names = chip->names;
  164. }
  165. /*
  166. * Handlers for alternative sources of platform_data
  167. */
  168. #ifdef CONFIG_OF_GPIO
  169. /*
  170. * Translate OpenFirmware node properties into platform_data
  171. */
  172. static struct pca953x_platform_data *
  173. pca953x_get_alt_pdata(struct i2c_client *client)
  174. {
  175. struct pca953x_platform_data *pdata;
  176. struct device_node *node;
  177. const uint16_t *val;
  178. node = dev_archdata_get_node(&client->dev.archdata);
  179. if (node == NULL)
  180. return NULL;
  181. pdata = kzalloc(sizeof(struct pca953x_platform_data), GFP_KERNEL);
  182. if (pdata == NULL) {
  183. dev_err(&client->dev, "Unable to allocate platform_data\n");
  184. return NULL;
  185. }
  186. pdata->gpio_base = -1;
  187. val = of_get_property(node, "linux,gpio-base", NULL);
  188. if (val) {
  189. if (*val < 0)
  190. dev_warn(&client->dev,
  191. "invalid gpio-base in device tree\n");
  192. else
  193. pdata->gpio_base = *val;
  194. }
  195. val = of_get_property(node, "polarity", NULL);
  196. if (val)
  197. pdata->invert = *val;
  198. return pdata;
  199. }
  200. #else
  201. static struct pca953x_platform_data *
  202. pca953x_get_alt_pdata(struct i2c_client *client)
  203. {
  204. return NULL;
  205. }
  206. #endif
  207. static int __devinit pca953x_probe(struct i2c_client *client,
  208. const struct i2c_device_id *id)
  209. {
  210. struct pca953x_platform_data *pdata;
  211. struct pca953x_chip *chip;
  212. int ret;
  213. chip = kzalloc(sizeof(struct pca953x_chip), GFP_KERNEL);
  214. if (chip == NULL)
  215. return -ENOMEM;
  216. pdata = client->dev.platform_data;
  217. if (pdata == NULL) {
  218. pdata = pca953x_get_alt_pdata(client);
  219. /*
  220. * Unlike normal platform_data, this is allocated
  221. * dynamically and must be freed in the driver
  222. */
  223. chip->dyn_pdata = pdata;
  224. }
  225. if (pdata == NULL) {
  226. dev_dbg(&client->dev, "no platform data\n");
  227. ret = -EINVAL;
  228. goto out_failed;
  229. }
  230. chip->client = client;
  231. chip->gpio_start = pdata->gpio_base;
  232. chip->names = pdata->names;
  233. /* initialize cached registers from their original values.
  234. * we can't share this chip with another i2c master.
  235. */
  236. pca953x_setup_gpio(chip, id->driver_data);
  237. ret = pca953x_read_reg(chip, PCA953X_OUTPUT, &chip->reg_output);
  238. if (ret)
  239. goto out_failed;
  240. ret = pca953x_read_reg(chip, PCA953X_DIRECTION, &chip->reg_direction);
  241. if (ret)
  242. goto out_failed;
  243. /* set platform specific polarity inversion */
  244. ret = pca953x_write_reg(chip, PCA953X_INVERT, pdata->invert);
  245. if (ret)
  246. goto out_failed;
  247. ret = gpiochip_add(&chip->gpio_chip);
  248. if (ret)
  249. goto out_failed;
  250. if (pdata->setup) {
  251. ret = pdata->setup(client, chip->gpio_chip.base,
  252. chip->gpio_chip.ngpio, pdata->context);
  253. if (ret < 0)
  254. dev_warn(&client->dev, "setup failed, %d\n", ret);
  255. }
  256. i2c_set_clientdata(client, chip);
  257. return 0;
  258. out_failed:
  259. kfree(chip->dyn_pdata);
  260. kfree(chip);
  261. return ret;
  262. }
  263. static int pca953x_remove(struct i2c_client *client)
  264. {
  265. struct pca953x_platform_data *pdata = client->dev.platform_data;
  266. struct pca953x_chip *chip = i2c_get_clientdata(client);
  267. int ret = 0;
  268. if (pdata->teardown) {
  269. ret = pdata->teardown(client, chip->gpio_chip.base,
  270. chip->gpio_chip.ngpio, pdata->context);
  271. if (ret < 0) {
  272. dev_err(&client->dev, "%s failed, %d\n",
  273. "teardown", ret);
  274. return ret;
  275. }
  276. }
  277. ret = gpiochip_remove(&chip->gpio_chip);
  278. if (ret) {
  279. dev_err(&client->dev, "%s failed, %d\n",
  280. "gpiochip_remove()", ret);
  281. return ret;
  282. }
  283. kfree(chip->dyn_pdata);
  284. kfree(chip);
  285. return 0;
  286. }
  287. static struct i2c_driver pca953x_driver = {
  288. .driver = {
  289. .name = "pca953x",
  290. },
  291. .probe = pca953x_probe,
  292. .remove = pca953x_remove,
  293. .id_table = pca953x_id,
  294. };
  295. static int __init pca953x_init(void)
  296. {
  297. return i2c_add_driver(&pca953x_driver);
  298. }
  299. /* register after i2c postcore initcall and before
  300. * subsys initcalls that may rely on these GPIOs
  301. */
  302. subsys_initcall(pca953x_init);
  303. static void __exit pca953x_exit(void)
  304. {
  305. i2c_del_driver(&pca953x_driver);
  306. }
  307. module_exit(pca953x_exit);
  308. MODULE_AUTHOR("eric miao <eric.miao@marvell.com>");
  309. MODULE_DESCRIPTION("GPIO expander driver for PCA953x");
  310. MODULE_LICENSE("GPL");