i2c-arb-gpio-challenge.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * GPIO-based I2C Arbitration Using a Challenge & Response Mechanism
  3. *
  4. * Copyright (C) 2012 Google, Inc
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/gpio.h>
  18. #include <linux/kernel.h>
  19. #include <linux/i2c.h>
  20. #include <linux/i2c-mux.h>
  21. #include <linux/module.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. /**
  26. * struct i2c_arbitrator_data - Driver data for I2C arbitrator
  27. *
  28. * @our_gpio: GPIO we'll use to claim.
  29. * @our_gpio_release: 0 if active high; 1 if active low; AKA if the GPIO ==
  30. * this then consider it released.
  31. * @their_gpio: GPIO that the other side will use to claim.
  32. * @their_gpio_release: 0 if active high; 1 if active low; AKA if the GPIO ==
  33. * this then consider it released.
  34. * @slew_delay_us: microseconds to wait for a GPIO to go high.
  35. * @wait_retry_us: we'll attempt another claim after this many microseconds.
  36. * @wait_free_us: we'll give up after this many microseconds.
  37. */
  38. struct i2c_arbitrator_data {
  39. int our_gpio;
  40. int our_gpio_release;
  41. int their_gpio;
  42. int their_gpio_release;
  43. unsigned int slew_delay_us;
  44. unsigned int wait_retry_us;
  45. unsigned int wait_free_us;
  46. };
  47. /**
  48. * i2c_arbitrator_select - claim the I2C bus
  49. *
  50. * Use the GPIO-based signalling protocol; return -EBUSY if we fail.
  51. */
  52. static int i2c_arbitrator_select(struct i2c_mux_core *muxc, u32 chan)
  53. {
  54. const struct i2c_arbitrator_data *arb = i2c_mux_priv(muxc);
  55. unsigned long stop_retry, stop_time;
  56. /* Start a round of trying to claim the bus */
  57. stop_time = jiffies + usecs_to_jiffies(arb->wait_free_us) + 1;
  58. do {
  59. /* Indicate that we want to claim the bus */
  60. gpio_set_value(arb->our_gpio, !arb->our_gpio_release);
  61. udelay(arb->slew_delay_us);
  62. /* Wait for the other master to release it */
  63. stop_retry = jiffies + usecs_to_jiffies(arb->wait_retry_us) + 1;
  64. while (time_before(jiffies, stop_retry)) {
  65. int gpio_val = !!gpio_get_value(arb->their_gpio);
  66. if (gpio_val == arb->their_gpio_release) {
  67. /* We got it, so return */
  68. return 0;
  69. }
  70. usleep_range(50, 200);
  71. }
  72. /* It didn't release, so give up, wait, and try again */
  73. gpio_set_value(arb->our_gpio, arb->our_gpio_release);
  74. usleep_range(arb->wait_retry_us, arb->wait_retry_us * 2);
  75. } while (time_before(jiffies, stop_time));
  76. /* Give up, release our claim */
  77. gpio_set_value(arb->our_gpio, arb->our_gpio_release);
  78. udelay(arb->slew_delay_us);
  79. dev_err(muxc->dev, "Could not claim bus, timeout\n");
  80. return -EBUSY;
  81. }
  82. /**
  83. * i2c_arbitrator_deselect - release the I2C bus
  84. *
  85. * Release the I2C bus using the GPIO-based signalling protocol.
  86. */
  87. static int i2c_arbitrator_deselect(struct i2c_mux_core *muxc, u32 chan)
  88. {
  89. const struct i2c_arbitrator_data *arb = i2c_mux_priv(muxc);
  90. /* Release the bus and wait for the other master to notice */
  91. gpio_set_value(arb->our_gpio, arb->our_gpio_release);
  92. udelay(arb->slew_delay_us);
  93. return 0;
  94. }
  95. static int i2c_arbitrator_probe(struct platform_device *pdev)
  96. {
  97. struct device *dev = &pdev->dev;
  98. struct device_node *np = dev->of_node;
  99. struct device_node *parent_np;
  100. struct i2c_mux_core *muxc;
  101. struct i2c_arbitrator_data *arb;
  102. enum of_gpio_flags gpio_flags;
  103. unsigned long out_init;
  104. int ret;
  105. /* We only support probing from device tree; no platform_data */
  106. if (!np) {
  107. dev_err(dev, "Cannot find device tree node\n");
  108. return -ENODEV;
  109. }
  110. if (dev_get_platdata(dev)) {
  111. dev_err(dev, "Platform data is not supported\n");
  112. return -EINVAL;
  113. }
  114. muxc = i2c_mux_alloc(NULL, dev, 1, sizeof(*arb), I2C_MUX_ARBITRATOR,
  115. i2c_arbitrator_select, i2c_arbitrator_deselect);
  116. if (!muxc)
  117. return -ENOMEM;
  118. arb = i2c_mux_priv(muxc);
  119. platform_set_drvdata(pdev, muxc);
  120. /* Request GPIOs */
  121. ret = of_get_named_gpio_flags(np, "our-claim-gpio", 0, &gpio_flags);
  122. if (!gpio_is_valid(ret)) {
  123. if (ret != -EPROBE_DEFER)
  124. dev_err(dev, "Error getting our-claim-gpio\n");
  125. return ret;
  126. }
  127. arb->our_gpio = ret;
  128. arb->our_gpio_release = !!(gpio_flags & OF_GPIO_ACTIVE_LOW);
  129. out_init = (gpio_flags & OF_GPIO_ACTIVE_LOW) ?
  130. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  131. ret = devm_gpio_request_one(dev, arb->our_gpio, out_init,
  132. "our-claim-gpio");
  133. if (ret) {
  134. if (ret != -EPROBE_DEFER)
  135. dev_err(dev, "Error requesting our-claim-gpio\n");
  136. return ret;
  137. }
  138. ret = of_get_named_gpio_flags(np, "their-claim-gpios", 0, &gpio_flags);
  139. if (!gpio_is_valid(ret)) {
  140. if (ret != -EPROBE_DEFER)
  141. dev_err(dev, "Error getting their-claim-gpio\n");
  142. return ret;
  143. }
  144. arb->their_gpio = ret;
  145. arb->their_gpio_release = !!(gpio_flags & OF_GPIO_ACTIVE_LOW);
  146. ret = devm_gpio_request_one(dev, arb->their_gpio, GPIOF_IN,
  147. "their-claim-gpio");
  148. if (ret) {
  149. if (ret != -EPROBE_DEFER)
  150. dev_err(dev, "Error requesting their-claim-gpio\n");
  151. return ret;
  152. }
  153. /* At the moment we only support a single two master (us + 1 other) */
  154. if (gpio_is_valid(of_get_named_gpio(np, "their-claim-gpios", 1))) {
  155. dev_err(dev, "Only one other master is supported\n");
  156. return -EINVAL;
  157. }
  158. /* Arbitration parameters */
  159. if (of_property_read_u32(np, "slew-delay-us", &arb->slew_delay_us))
  160. arb->slew_delay_us = 10;
  161. if (of_property_read_u32(np, "wait-retry-us", &arb->wait_retry_us))
  162. arb->wait_retry_us = 3000;
  163. if (of_property_read_u32(np, "wait-free-us", &arb->wait_free_us))
  164. arb->wait_free_us = 50000;
  165. /* Find our parent */
  166. parent_np = of_parse_phandle(np, "i2c-parent", 0);
  167. if (!parent_np) {
  168. dev_err(dev, "Cannot parse i2c-parent\n");
  169. return -EINVAL;
  170. }
  171. muxc->parent = of_get_i2c_adapter_by_node(parent_np);
  172. of_node_put(parent_np);
  173. if (!muxc->parent) {
  174. dev_err(dev, "Cannot find parent bus\n");
  175. return -EPROBE_DEFER;
  176. }
  177. /* Actually add the mux adapter */
  178. ret = i2c_mux_add_adapter(muxc, 0, 0, 0);
  179. if (ret)
  180. i2c_put_adapter(muxc->parent);
  181. return ret;
  182. }
  183. static int i2c_arbitrator_remove(struct platform_device *pdev)
  184. {
  185. struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
  186. i2c_mux_del_adapters(muxc);
  187. i2c_put_adapter(muxc->parent);
  188. return 0;
  189. }
  190. static const struct of_device_id i2c_arbitrator_of_match[] = {
  191. { .compatible = "i2c-arb-gpio-challenge", },
  192. {},
  193. };
  194. MODULE_DEVICE_TABLE(of, i2c_arbitrator_of_match);
  195. static struct platform_driver i2c_arbitrator_driver = {
  196. .probe = i2c_arbitrator_probe,
  197. .remove = i2c_arbitrator_remove,
  198. .driver = {
  199. .name = "i2c-arb-gpio-challenge",
  200. .of_match_table = i2c_arbitrator_of_match,
  201. },
  202. };
  203. module_platform_driver(i2c_arbitrator_driver);
  204. MODULE_DESCRIPTION("GPIO-based I2C Arbitration");
  205. MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
  206. MODULE_LICENSE("GPL v2");
  207. MODULE_ALIAS("platform:i2c-arb-gpio-challenge");