i2c-arb-gpio-challenge.c 7.1 KB

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