pca953x.c 8.2 KB

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