i2c-mux-gpio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * I2C multiplexer using GPIO API
  3. *
  4. * Peter Korsgaard <peter.korsgaard@barco.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/i2c.h>
  11. #include <linux/i2c-mux.h>
  12. #include <linux/i2c-mux-gpio.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/gpio.h>
  17. #include <linux/of_gpio.h>
  18. struct gpiomux {
  19. struct i2c_adapter *parent;
  20. struct i2c_adapter **adap; /* child busses */
  21. struct i2c_mux_gpio_platform_data data;
  22. unsigned gpio_base;
  23. };
  24. static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
  25. {
  26. int i;
  27. for (i = 0; i < mux->data.n_gpios; i++)
  28. gpio_set_value_cansleep(mux->gpio_base + mux->data.gpios[i],
  29. val & (1 << i));
  30. }
  31. static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan)
  32. {
  33. struct gpiomux *mux = data;
  34. i2c_mux_gpio_set(mux, chan);
  35. return 0;
  36. }
  37. static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan)
  38. {
  39. struct gpiomux *mux = data;
  40. i2c_mux_gpio_set(mux, mux->data.idle);
  41. return 0;
  42. }
  43. static int match_gpio_chip_by_label(struct gpio_chip *chip,
  44. void *data)
  45. {
  46. return !strcmp(chip->label, data);
  47. }
  48. #ifdef CONFIG_OF
  49. static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
  50. struct platform_device *pdev)
  51. {
  52. struct device_node *np = pdev->dev.of_node;
  53. struct device_node *adapter_np, *child;
  54. struct i2c_adapter *adapter;
  55. unsigned *values, *gpios;
  56. int i = 0, ret;
  57. if (!np)
  58. return -ENODEV;
  59. adapter_np = of_parse_phandle(np, "i2c-parent", 0);
  60. if (!adapter_np) {
  61. dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
  62. return -ENODEV;
  63. }
  64. adapter = of_find_i2c_adapter_by_node(adapter_np);
  65. if (!adapter)
  66. return -EPROBE_DEFER;
  67. mux->data.parent = i2c_adapter_id(adapter);
  68. put_device(&adapter->dev);
  69. mux->data.n_values = of_get_child_count(np);
  70. values = devm_kzalloc(&pdev->dev,
  71. sizeof(*mux->data.values) * mux->data.n_values,
  72. GFP_KERNEL);
  73. if (!values) {
  74. dev_err(&pdev->dev, "Cannot allocate values array");
  75. return -ENOMEM;
  76. }
  77. for_each_child_of_node(np, child) {
  78. of_property_read_u32(child, "reg", values + i);
  79. i++;
  80. }
  81. mux->data.values = values;
  82. if (of_property_read_u32(np, "idle-state", &mux->data.idle))
  83. mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
  84. mux->data.n_gpios = of_gpio_named_count(np, "mux-gpios");
  85. if (mux->data.n_gpios < 0) {
  86. dev_err(&pdev->dev, "Missing mux-gpios property in the DT.\n");
  87. return -EINVAL;
  88. }
  89. gpios = devm_kzalloc(&pdev->dev,
  90. sizeof(*mux->data.gpios) * mux->data.n_gpios, GFP_KERNEL);
  91. if (!gpios) {
  92. dev_err(&pdev->dev, "Cannot allocate gpios array");
  93. return -ENOMEM;
  94. }
  95. for (i = 0; i < mux->data.n_gpios; i++) {
  96. ret = of_get_named_gpio(np, "mux-gpios", i);
  97. if (ret < 0)
  98. return ret;
  99. gpios[i] = ret;
  100. }
  101. mux->data.gpios = gpios;
  102. return 0;
  103. }
  104. #else
  105. static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
  106. struct platform_device *pdev)
  107. {
  108. return 0;
  109. }
  110. #endif
  111. static int i2c_mux_gpio_probe(struct platform_device *pdev)
  112. {
  113. struct gpiomux *mux;
  114. struct i2c_adapter *parent;
  115. int (*deselect) (struct i2c_adapter *, void *, u32);
  116. unsigned initial_state, gpio_base;
  117. int i, ret;
  118. mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
  119. if (!mux) {
  120. dev_err(&pdev->dev, "Cannot allocate gpiomux structure");
  121. return -ENOMEM;
  122. }
  123. platform_set_drvdata(pdev, mux);
  124. if (!dev_get_platdata(&pdev->dev)) {
  125. ret = i2c_mux_gpio_probe_dt(mux, pdev);
  126. if (ret < 0)
  127. return ret;
  128. } else {
  129. memcpy(&mux->data, dev_get_platdata(&pdev->dev),
  130. sizeof(mux->data));
  131. }
  132. /*
  133. * If a GPIO chip name is provided, the GPIO pin numbers provided are
  134. * relative to its base GPIO number. Otherwise they are absolute.
  135. */
  136. if (mux->data.gpio_chip) {
  137. struct gpio_chip *gpio;
  138. gpio = gpiochip_find(mux->data.gpio_chip,
  139. match_gpio_chip_by_label);
  140. if (!gpio)
  141. return -EPROBE_DEFER;
  142. gpio_base = gpio->base;
  143. } else {
  144. gpio_base = 0;
  145. }
  146. parent = i2c_get_adapter(mux->data.parent);
  147. if (!parent)
  148. return -EPROBE_DEFER;
  149. mux->parent = parent;
  150. mux->gpio_base = gpio_base;
  151. mux->adap = devm_kzalloc(&pdev->dev,
  152. sizeof(*mux->adap) * mux->data.n_values,
  153. GFP_KERNEL);
  154. if (!mux->adap) {
  155. dev_err(&pdev->dev, "Cannot allocate i2c_adapter structure");
  156. ret = -ENOMEM;
  157. goto alloc_failed;
  158. }
  159. if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
  160. initial_state = mux->data.idle;
  161. deselect = i2c_mux_gpio_deselect;
  162. } else {
  163. initial_state = mux->data.values[0];
  164. deselect = NULL;
  165. }
  166. for (i = 0; i < mux->data.n_gpios; i++) {
  167. ret = gpio_request(gpio_base + mux->data.gpios[i], "i2c-mux-gpio");
  168. if (ret) {
  169. dev_err(&pdev->dev, "Failed to request GPIO %d\n",
  170. mux->data.gpios[i]);
  171. goto err_request_gpio;
  172. }
  173. ret = gpio_direction_output(gpio_base + mux->data.gpios[i],
  174. initial_state & (1 << i));
  175. if (ret) {
  176. dev_err(&pdev->dev,
  177. "Failed to set direction of GPIO %d to output\n",
  178. mux->data.gpios[i]);
  179. i++; /* gpio_request above succeeded, so must free */
  180. goto err_request_gpio;
  181. }
  182. }
  183. for (i = 0; i < mux->data.n_values; i++) {
  184. u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
  185. unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
  186. mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr,
  187. mux->data.values[i], class,
  188. i2c_mux_gpio_select, deselect);
  189. if (!mux->adap[i]) {
  190. ret = -ENODEV;
  191. dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
  192. goto add_adapter_failed;
  193. }
  194. }
  195. dev_info(&pdev->dev, "%d port mux on %s adapter\n",
  196. mux->data.n_values, parent->name);
  197. return 0;
  198. add_adapter_failed:
  199. for (; i > 0; i--)
  200. i2c_del_mux_adapter(mux->adap[i - 1]);
  201. i = mux->data.n_gpios;
  202. err_request_gpio:
  203. for (; i > 0; i--)
  204. gpio_free(gpio_base + mux->data.gpios[i - 1]);
  205. alloc_failed:
  206. i2c_put_adapter(parent);
  207. return ret;
  208. }
  209. static int i2c_mux_gpio_remove(struct platform_device *pdev)
  210. {
  211. struct gpiomux *mux = platform_get_drvdata(pdev);
  212. int i;
  213. for (i = 0; i < mux->data.n_values; i++)
  214. i2c_del_mux_adapter(mux->adap[i]);
  215. for (i = 0; i < mux->data.n_gpios; i++)
  216. gpio_free(mux->gpio_base + mux->data.gpios[i]);
  217. i2c_put_adapter(mux->parent);
  218. return 0;
  219. }
  220. static const struct of_device_id i2c_mux_gpio_of_match[] = {
  221. { .compatible = "i2c-mux-gpio", },
  222. {},
  223. };
  224. MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
  225. static struct platform_driver i2c_mux_gpio_driver = {
  226. .probe = i2c_mux_gpio_probe,
  227. .remove = i2c_mux_gpio_remove,
  228. .driver = {
  229. .name = "i2c-mux-gpio",
  230. .of_match_table = i2c_mux_gpio_of_match,
  231. },
  232. };
  233. module_platform_driver(i2c_mux_gpio_driver);
  234. MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
  235. MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
  236. MODULE_LICENSE("GPL");
  237. MODULE_ALIAS("platform:i2c-mux-gpio");