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