i2c-arb-gpio-challenge.c 7.1 KB

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