i2c-arb-gpio-challenge.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. * @parent: Parent adapter
  29. * @child: Child bus
  30. * @our_gpio: GPIO we'll use to claim.
  31. * @our_gpio_release: 0 if active high; 1 if active low; AKA if the GPIO ==
  32. * this then consider it released.
  33. * @their_gpio: GPIO that the other side will use to claim.
  34. * @their_gpio_release: 0 if active high; 1 if active low; AKA if the GPIO ==
  35. * this then consider it released.
  36. * @slew_delay_us: microseconds to wait for a GPIO to go high.
  37. * @wait_retry_us: we'll attempt another claim after this many microseconds.
  38. * @wait_free_us: we'll give up after this many microseconds.
  39. */
  40. struct i2c_arbitrator_data {
  41. struct i2c_adapter *parent;
  42. struct i2c_adapter *child;
  43. int our_gpio;
  44. int our_gpio_release;
  45. int their_gpio;
  46. int their_gpio_release;
  47. unsigned int slew_delay_us;
  48. unsigned int wait_retry_us;
  49. unsigned int wait_free_us;
  50. };
  51. /**
  52. * i2c_arbitrator_select - claim the I2C bus
  53. *
  54. * Use the GPIO-based signalling protocol; return -EBUSY if we fail.
  55. */
  56. static int i2c_arbitrator_select(struct i2c_adapter *adap, void *data, u32 chan)
  57. {
  58. const struct i2c_arbitrator_data *arb = data;
  59. unsigned long stop_retry, stop_time;
  60. /* Start a round of trying to claim the bus */
  61. stop_time = jiffies + usecs_to_jiffies(arb->wait_free_us) + 1;
  62. do {
  63. /* Indicate that we want to claim the bus */
  64. gpio_set_value(arb->our_gpio, !arb->our_gpio_release);
  65. udelay(arb->slew_delay_us);
  66. /* Wait for the other master to release it */
  67. stop_retry = jiffies + usecs_to_jiffies(arb->wait_retry_us) + 1;
  68. while (time_before(jiffies, stop_retry)) {
  69. int gpio_val = !!gpio_get_value(arb->their_gpio);
  70. if (gpio_val == arb->their_gpio_release) {
  71. /* We got it, so return */
  72. return 0;
  73. }
  74. usleep_range(50, 200);
  75. }
  76. /* It didn't release, so give up, wait, and try again */
  77. gpio_set_value(arb->our_gpio, arb->our_gpio_release);
  78. usleep_range(arb->wait_retry_us, arb->wait_retry_us * 2);
  79. } while (time_before(jiffies, stop_time));
  80. /* Give up, release our claim */
  81. gpio_set_value(arb->our_gpio, arb->our_gpio_release);
  82. udelay(arb->slew_delay_us);
  83. dev_err(&adap->dev, "Could not claim bus, timeout\n");
  84. return -EBUSY;
  85. }
  86. /**
  87. * i2c_arbitrator_deselect - release the I2C bus
  88. *
  89. * Release the I2C bus using the GPIO-based signalling protocol.
  90. */
  91. static int i2c_arbitrator_deselect(struct i2c_adapter *adap, void *data,
  92. u32 chan)
  93. {
  94. const struct i2c_arbitrator_data *arb = data;
  95. /* Release the bus and wait for the other master to notice */
  96. gpio_set_value(arb->our_gpio, arb->our_gpio_release);
  97. udelay(arb->slew_delay_us);
  98. return 0;
  99. }
  100. static int i2c_arbitrator_probe(struct platform_device *pdev)
  101. {
  102. struct device *dev = &pdev->dev;
  103. struct device_node *np = dev->of_node;
  104. struct device_node *parent_np;
  105. struct i2c_arbitrator_data *arb;
  106. enum of_gpio_flags gpio_flags;
  107. unsigned long out_init;
  108. int ret;
  109. /* We only support probing from device tree; no platform_data */
  110. if (!np) {
  111. dev_err(dev, "Cannot find device tree node\n");
  112. return -ENODEV;
  113. }
  114. if (dev_get_platdata(dev)) {
  115. dev_err(dev, "Platform data is not supported\n");
  116. return -EINVAL;
  117. }
  118. arb = devm_kzalloc(dev, sizeof(*arb), GFP_KERNEL);
  119. if (!arb) {
  120. dev_err(dev, "Cannot allocate i2c_arbitrator_data\n");
  121. return -ENOMEM;
  122. }
  123. platform_set_drvdata(pdev, arb);
  124. /* Request GPIOs */
  125. ret = of_get_named_gpio_flags(np, "our-claim-gpio", 0, &gpio_flags);
  126. if (!gpio_is_valid(ret)) {
  127. if (ret != -EPROBE_DEFER)
  128. dev_err(dev, "Error getting our-claim-gpio\n");
  129. return ret;
  130. }
  131. arb->our_gpio = ret;
  132. arb->our_gpio_release = !!(gpio_flags & OF_GPIO_ACTIVE_LOW);
  133. out_init = (gpio_flags & OF_GPIO_ACTIVE_LOW) ?
  134. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  135. ret = devm_gpio_request_one(dev, arb->our_gpio, out_init,
  136. "our-claim-gpio");
  137. if (ret) {
  138. if (ret != -EPROBE_DEFER)
  139. dev_err(dev, "Error requesting our-claim-gpio\n");
  140. return ret;
  141. }
  142. ret = of_get_named_gpio_flags(np, "their-claim-gpios", 0, &gpio_flags);
  143. if (!gpio_is_valid(ret)) {
  144. if (ret != -EPROBE_DEFER)
  145. dev_err(dev, "Error getting their-claim-gpio\n");
  146. return ret;
  147. }
  148. arb->their_gpio = ret;
  149. arb->their_gpio_release = !!(gpio_flags & OF_GPIO_ACTIVE_LOW);
  150. ret = devm_gpio_request_one(dev, arb->their_gpio, GPIOF_IN,
  151. "their-claim-gpio");
  152. if (ret) {
  153. if (ret != -EPROBE_DEFER)
  154. dev_err(dev, "Error requesting their-claim-gpio\n");
  155. return ret;
  156. }
  157. /* At the moment we only support a single two master (us + 1 other) */
  158. if (gpio_is_valid(of_get_named_gpio(np, "their-claim-gpios", 1))) {
  159. dev_err(dev, "Only one other master is supported\n");
  160. return -EINVAL;
  161. }
  162. /* Arbitration parameters */
  163. if (of_property_read_u32(np, "slew-delay-us", &arb->slew_delay_us))
  164. arb->slew_delay_us = 10;
  165. if (of_property_read_u32(np, "wait-retry-us", &arb->wait_retry_us))
  166. arb->wait_retry_us = 3000;
  167. if (of_property_read_u32(np, "wait-free-us", &arb->wait_free_us))
  168. arb->wait_free_us = 50000;
  169. /* Find our parent */
  170. parent_np = of_parse_phandle(np, "i2c-parent", 0);
  171. if (!parent_np) {
  172. dev_err(dev, "Cannot parse i2c-parent\n");
  173. return -EINVAL;
  174. }
  175. arb->parent = of_get_i2c_adapter_by_node(parent_np);
  176. of_node_put(parent_np);
  177. if (!arb->parent) {
  178. dev_err(dev, "Cannot find parent bus\n");
  179. return -EPROBE_DEFER;
  180. }
  181. /* Actually add the mux adapter */
  182. arb->child = i2c_add_mux_adapter(arb->parent, dev, arb, 0, 0, 0,
  183. i2c_arbitrator_select,
  184. i2c_arbitrator_deselect);
  185. if (!arb->child) {
  186. dev_err(dev, "Failed to add adapter\n");
  187. ret = -ENODEV;
  188. i2c_put_adapter(arb->parent);
  189. }
  190. return ret;
  191. }
  192. static int i2c_arbitrator_remove(struct platform_device *pdev)
  193. {
  194. struct i2c_arbitrator_data *arb = platform_get_drvdata(pdev);
  195. i2c_del_mux_adapter(arb->child);
  196. i2c_put_adapter(arb->parent);
  197. return 0;
  198. }
  199. static const struct of_device_id i2c_arbitrator_of_match[] = {
  200. { .compatible = "i2c-arb-gpio-challenge", },
  201. {},
  202. };
  203. MODULE_DEVICE_TABLE(of, i2c_arbitrator_of_match);
  204. static struct platform_driver i2c_arbitrator_driver = {
  205. .probe = i2c_arbitrator_probe,
  206. .remove = i2c_arbitrator_remove,
  207. .driver = {
  208. .name = "i2c-arb-gpio-challenge",
  209. .of_match_table = i2c_arbitrator_of_match,
  210. },
  211. };
  212. module_platform_driver(i2c_arbitrator_driver);
  213. MODULE_DESCRIPTION("GPIO-based I2C Arbitration");
  214. MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
  215. MODULE_LICENSE("GPL v2");
  216. MODULE_ALIAS("platform:i2c-arb-gpio-challenge");