i2c-mux-gpio.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/gpio.h>
  18. #include <linux/of_i2c.h>
  19. #include <linux/of_gpio.h>
  20. struct gpiomux {
  21. struct i2c_adapter *parent;
  22. struct i2c_adapter **adap; /* child busses */
  23. struct i2c_mux_gpio_platform_data data;
  24. unsigned gpio_base;
  25. };
  26. static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
  27. {
  28. int i;
  29. for (i = 0; i < mux->data.n_gpios; i++)
  30. gpio_set_value(mux->gpio_base + mux->data.gpios[i],
  31. val & (1 << i));
  32. }
  33. static int i2c_mux_gpio_select(struct i2c_adapter *adap, void *data, u32 chan)
  34. {
  35. struct gpiomux *mux = data;
  36. i2c_mux_gpio_set(mux, mux->data.values[chan]);
  37. return 0;
  38. }
  39. static int i2c_mux_gpio_deselect(struct i2c_adapter *adap, void *data, u32 chan)
  40. {
  41. struct gpiomux *mux = data;
  42. i2c_mux_gpio_set(mux, mux->data.idle);
  43. return 0;
  44. }
  45. static int match_gpio_chip_by_label(struct gpio_chip *chip,
  46. void *data)
  47. {
  48. return !strcmp(chip->label, data);
  49. }
  50. #ifdef CONFIG_OF
  51. static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
  52. struct platform_device *pdev)
  53. {
  54. struct device_node *np = pdev->dev.of_node;
  55. struct device_node *adapter_np, *child;
  56. struct i2c_adapter *adapter;
  57. unsigned *values, *gpios;
  58. int i = 0;
  59. if (!np)
  60. return -ENODEV;
  61. adapter_np = of_parse_phandle(np, "i2c-parent", 0);
  62. if (!adapter_np) {
  63. dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
  64. return -ENODEV;
  65. }
  66. adapter = of_find_i2c_adapter_by_node(adapter_np);
  67. if (!adapter) {
  68. dev_err(&pdev->dev, "Cannot find parent bus\n");
  69. return -ENODEV;
  70. }
  71. mux->data.parent = i2c_adapter_id(adapter);
  72. put_device(&adapter->dev);
  73. mux->data.n_values = of_get_child_count(np);
  74. values = devm_kzalloc(&pdev->dev,
  75. sizeof(*mux->data.values) * mux->data.n_values,
  76. GFP_KERNEL);
  77. if (!values) {
  78. dev_err(&pdev->dev, "Cannot allocate values array");
  79. return -ENOMEM;
  80. }
  81. for_each_child_of_node(np, child) {
  82. of_property_read_u32(child, "reg", values + i);
  83. i++;
  84. }
  85. mux->data.values = values;
  86. if (of_property_read_u32(np, "idle-state", &mux->data.idle))
  87. mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
  88. mux->data.n_gpios = of_gpio_named_count(np, "mux-gpios");
  89. if (mux->data.n_gpios < 0) {
  90. dev_err(&pdev->dev, "Missing mux-gpios property in the DT.\n");
  91. return -EINVAL;
  92. }
  93. gpios = devm_kzalloc(&pdev->dev,
  94. sizeof(*mux->data.gpios) * mux->data.n_gpios, GFP_KERNEL);
  95. if (!gpios) {
  96. dev_err(&pdev->dev, "Cannot allocate gpios array");
  97. return -ENOMEM;
  98. }
  99. for (i = 0; i < mux->data.n_gpios; i++)
  100. gpios[i] = of_get_named_gpio(np, "mux-gpios", i);
  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 (!pdev->dev.platform_data) {
  125. ret = i2c_mux_gpio_probe_dt(mux, pdev);
  126. if (ret < 0)
  127. return ret;
  128. } else
  129. memcpy(&mux->data, pdev->dev.platform_data, sizeof(mux->data));
  130. /*
  131. * If a GPIO chip name is provided, the GPIO pin numbers provided are
  132. * relative to its base GPIO number. Otherwise they are absolute.
  133. */
  134. if (mux->data.gpio_chip) {
  135. struct gpio_chip *gpio;
  136. gpio = gpiochip_find(mux->data.gpio_chip,
  137. match_gpio_chip_by_label);
  138. if (!gpio)
  139. return -EPROBE_DEFER;
  140. gpio_base = gpio->base;
  141. } else {
  142. gpio_base = 0;
  143. }
  144. parent = i2c_get_adapter(mux->data.parent);
  145. if (!parent) {
  146. dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
  147. mux->data.parent);
  148. return -ENODEV;
  149. }
  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. goto err_request_gpio;
  171. gpio_direction_output(gpio_base + mux->data.gpios[i],
  172. initial_state & (1 << i));
  173. }
  174. for (i = 0; i < mux->data.n_values; i++) {
  175. u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
  176. unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
  177. mux->adap[i] = i2c_add_mux_adapter(parent, &pdev->dev, mux, nr,
  178. i, class,
  179. i2c_mux_gpio_select, deselect);
  180. if (!mux->adap[i]) {
  181. ret = -ENODEV;
  182. dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
  183. goto add_adapter_failed;
  184. }
  185. }
  186. dev_info(&pdev->dev, "%d port mux on %s adapter\n",
  187. mux->data.n_values, parent->name);
  188. return 0;
  189. add_adapter_failed:
  190. for (; i > 0; i--)
  191. i2c_del_mux_adapter(mux->adap[i - 1]);
  192. i = mux->data.n_gpios;
  193. err_request_gpio:
  194. for (; i > 0; i--)
  195. gpio_free(gpio_base + mux->data.gpios[i - 1]);
  196. alloc_failed:
  197. i2c_put_adapter(parent);
  198. return ret;
  199. }
  200. static int i2c_mux_gpio_remove(struct platform_device *pdev)
  201. {
  202. struct gpiomux *mux = platform_get_drvdata(pdev);
  203. int i;
  204. for (i = 0; i < mux->data.n_values; i++)
  205. i2c_del_mux_adapter(mux->adap[i]);
  206. for (i = 0; i < mux->data.n_gpios; i++)
  207. gpio_free(mux->gpio_base + mux->data.gpios[i]);
  208. i2c_put_adapter(mux->parent);
  209. return 0;
  210. }
  211. static const struct of_device_id i2c_mux_gpio_of_match[] = {
  212. { .compatible = "i2c-mux-gpio", },
  213. {},
  214. };
  215. MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
  216. static struct platform_driver i2c_mux_gpio_driver = {
  217. .probe = i2c_mux_gpio_probe,
  218. .remove = i2c_mux_gpio_remove,
  219. .driver = {
  220. .owner = THIS_MODULE,
  221. .name = "i2c-mux-gpio",
  222. .of_match_table = of_match_ptr(i2c_mux_gpio_of_match),
  223. },
  224. };
  225. module_platform_driver(i2c_mux_gpio_driver);
  226. MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
  227. MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
  228. MODULE_LICENSE("GPL");
  229. MODULE_ALIAS("platform:i2c-mux-gpio");