i2c-mux-gpio.c 6.8 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. of_node_put(adapter_np);
  66. if (!adapter)
  67. return -EPROBE_DEFER;
  68. mux->data.parent = i2c_adapter_id(adapter);
  69. put_device(&adapter->dev);
  70. mux->data.n_values = of_get_child_count(np);
  71. values = devm_kzalloc(&pdev->dev,
  72. sizeof(*mux->data.values) * mux->data.n_values,
  73. GFP_KERNEL);
  74. if (!values) {
  75. dev_err(&pdev->dev, "Cannot allocate values array");
  76. return -ENOMEM;
  77. }
  78. for_each_child_of_node(np, child) {
  79. of_property_read_u32(child, "reg", values + i);
  80. i++;
  81. }
  82. mux->data.values = values;
  83. if (of_property_read_u32(np, "idle-state", &mux->data.idle))
  84. mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
  85. mux->data.n_gpios = of_gpio_named_count(np, "mux-gpios");
  86. if (mux->data.n_gpios < 0) {
  87. dev_err(&pdev->dev, "Missing mux-gpios property in the DT.\n");
  88. return -EINVAL;
  89. }
  90. gpios = devm_kzalloc(&pdev->dev,
  91. sizeof(*mux->data.gpios) * mux->data.n_gpios, GFP_KERNEL);
  92. if (!gpios) {
  93. dev_err(&pdev->dev, "Cannot allocate gpios array");
  94. return -ENOMEM;
  95. }
  96. for (i = 0; i < mux->data.n_gpios; i++) {
  97. ret = of_get_named_gpio(np, "mux-gpios", i);
  98. if (ret < 0)
  99. return ret;
  100. gpios[i] = ret;
  101. }
  102. mux->data.gpios = gpios;
  103. return 0;
  104. }
  105. #else
  106. static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
  107. struct platform_device *pdev)
  108. {
  109. return 0;
  110. }
  111. #endif
  112. static int i2c_mux_gpio_probe(struct platform_device *pdev)
  113. {
  114. struct gpiomux *mux;
  115. struct i2c_adapter *parent;
  116. int (*deselect) (struct i2c_adapter *, void *, u32);
  117. unsigned initial_state, gpio_base;
  118. int i, ret;
  119. mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
  120. if (!mux) {
  121. dev_err(&pdev->dev, "Cannot allocate gpiomux structure");
  122. return -ENOMEM;
  123. }
  124. platform_set_drvdata(pdev, mux);
  125. if (!dev_get_platdata(&pdev->dev)) {
  126. ret = i2c_mux_gpio_probe_dt(mux, pdev);
  127. if (ret < 0)
  128. return ret;
  129. } else {
  130. memcpy(&mux->data, dev_get_platdata(&pdev->dev),
  131. sizeof(mux->data));
  132. }
  133. /*
  134. * If a GPIO chip name is provided, the GPIO pin numbers provided are
  135. * relative to its base GPIO number. Otherwise they are absolute.
  136. */
  137. if (mux->data.gpio_chip) {
  138. struct gpio_chip *gpio;
  139. gpio = gpiochip_find(mux->data.gpio_chip,
  140. match_gpio_chip_by_label);
  141. if (!gpio)
  142. return -EPROBE_DEFER;
  143. gpio_base = gpio->base;
  144. } else {
  145. gpio_base = 0;
  146. }
  147. parent = i2c_get_adapter(mux->data.parent);
  148. if (!parent)
  149. return -EPROBE_DEFER;
  150. mux->parent = parent;
  151. mux->gpio_base = gpio_base;
  152. mux->adap = devm_kzalloc(&pdev->dev,
  153. sizeof(*mux->adap) * mux->data.n_values,
  154. GFP_KERNEL);
  155. if (!mux->adap) {
  156. dev_err(&pdev->dev, "Cannot allocate i2c_adapter structure");
  157. ret = -ENOMEM;
  158. goto alloc_failed;
  159. }
  160. if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
  161. initial_state = mux->data.idle;
  162. deselect = i2c_mux_gpio_deselect;
  163. } else {
  164. initial_state = mux->data.values[0];
  165. deselect = NULL;
  166. }
  167. for (i = 0; i < mux->data.n_gpios; i++) {
  168. ret = gpio_request(gpio_base + mux->data.gpios[i], "i2c-mux-gpio");
  169. if (ret) {
  170. dev_err(&pdev->dev, "Failed to request GPIO %d\n",
  171. mux->data.gpios[i]);
  172. goto err_request_gpio;
  173. }
  174. ret = gpio_direction_output(gpio_base + mux->data.gpios[i],
  175. initial_state & (1 << i));
  176. if (ret) {
  177. dev_err(&pdev->dev,
  178. "Failed to set direction of GPIO %d to output\n",
  179. mux->data.gpios[i]);
  180. i++; /* gpio_request above succeeded, so must free */
  181. goto err_request_gpio;
  182. }
  183. }
  184. for (i = 0; i < mux->data.n_values; i++) {
  185. u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
  186. unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
  187. mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr,
  188. mux->data.values[i], class,
  189. i2c_mux_gpio_select, deselect);
  190. if (!mux->adap[i]) {
  191. ret = -ENODEV;
  192. dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
  193. goto add_adapter_failed;
  194. }
  195. }
  196. dev_info(&pdev->dev, "%d port mux on %s adapter\n",
  197. mux->data.n_values, parent->name);
  198. return 0;
  199. add_adapter_failed:
  200. for (; i > 0; i--)
  201. i2c_del_mux_adapter(mux->adap[i - 1]);
  202. i = mux->data.n_gpios;
  203. err_request_gpio:
  204. for (; i > 0; i--)
  205. gpio_free(gpio_base + mux->data.gpios[i - 1]);
  206. alloc_failed:
  207. i2c_put_adapter(parent);
  208. return ret;
  209. }
  210. static int i2c_mux_gpio_remove(struct platform_device *pdev)
  211. {
  212. struct gpiomux *mux = platform_get_drvdata(pdev);
  213. int i;
  214. for (i = 0; i < mux->data.n_values; i++)
  215. i2c_del_mux_adapter(mux->adap[i]);
  216. for (i = 0; i < mux->data.n_gpios; i++)
  217. gpio_free(mux->gpio_base + mux->data.gpios[i]);
  218. i2c_put_adapter(mux->parent);
  219. return 0;
  220. }
  221. static const struct of_device_id i2c_mux_gpio_of_match[] = {
  222. { .compatible = "i2c-mux-gpio", },
  223. {},
  224. };
  225. MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
  226. static struct platform_driver i2c_mux_gpio_driver = {
  227. .probe = i2c_mux_gpio_probe,
  228. .remove = i2c_mux_gpio_remove,
  229. .driver = {
  230. .name = "i2c-mux-gpio",
  231. .of_match_table = i2c_mux_gpio_of_match,
  232. },
  233. };
  234. module_platform_driver(i2c_mux_gpio_driver);
  235. MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
  236. MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
  237. MODULE_LICENSE("GPL");
  238. MODULE_ALIAS("platform:i2c-mux-gpio");