w1-gpio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* SPDX-License-Identifier: GPL-2.0+
  2. *
  3. * Copyright (c) 2015 Free Electrons
  4. * Copyright (c) 2015 NextThing Co
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <w1.h>
  12. #include <asm/gpio.h>
  13. #define W1_TIMING_A 6
  14. #define W1_TIMING_B 64
  15. #define W1_TIMING_C 60
  16. #define W1_TIMING_D 10
  17. #define W1_TIMING_E 9
  18. #define W1_TIMING_F 55
  19. #define W1_TIMING_G 0
  20. #define W1_TIMING_H 480
  21. #define W1_TIMING_I 70
  22. #define W1_TIMING_J 410
  23. struct w1_gpio_pdata {
  24. struct gpio_desc gpio;
  25. u64 search_id;
  26. };
  27. static bool w1_gpio_read_bit(struct udevice *dev)
  28. {
  29. struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
  30. int val;
  31. dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
  32. udelay(W1_TIMING_A);
  33. dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
  34. udelay(W1_TIMING_E);
  35. val = dm_gpio_get_value(&pdata->gpio);
  36. if (val < 0)
  37. debug("error in retrieving GPIO value");
  38. udelay(W1_TIMING_F);
  39. return val;
  40. }
  41. static u8 w1_gpio_read_byte(struct udevice *dev)
  42. {
  43. int i;
  44. u8 ret = 0;
  45. for (i = 0; i < 8; ++i)
  46. ret |= (w1_gpio_read_bit(dev) ? 1 : 0) << i;
  47. return ret;
  48. }
  49. static void w1_gpio_write_bit(struct udevice *dev, bool bit)
  50. {
  51. struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
  52. dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
  53. bit ? udelay(W1_TIMING_A) : udelay(W1_TIMING_C);
  54. dm_gpio_set_value(&pdata->gpio, 1);
  55. bit ? udelay(W1_TIMING_B) : udelay(W1_TIMING_D);
  56. }
  57. static void w1_gpio_write_byte(struct udevice *dev, u8 byte)
  58. {
  59. int i;
  60. for (i = 0; i < 8; ++i)
  61. w1_gpio_write_bit(dev, (byte >> i) & 0x1);
  62. }
  63. static bool w1_gpio_reset(struct udevice *dev)
  64. {
  65. struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
  66. int val;
  67. /* initiate the reset pulse. first we must pull the bus to low */
  68. dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  69. udelay(W1_TIMING_G);
  70. dm_gpio_set_value(&pdata->gpio, 0);
  71. /* wait for the specified time with the bus kept low */
  72. udelay(W1_TIMING_H);
  73. /* now we must read the presence pulse */
  74. dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
  75. udelay(W1_TIMING_I);
  76. val = dm_gpio_get_value(&pdata->gpio);
  77. if (val < 0)
  78. debug("error in retrieving GPIO value");
  79. /* if nobody pulled the bus down , it means nobody is on the bus */
  80. if (val != 0)
  81. return 1;
  82. /* we have the bus pulled down, let's wait for the specified presence time */
  83. udelay(W1_TIMING_J);
  84. /* read again, the other end should leave the bus free */
  85. val = dm_gpio_get_value(&pdata->gpio);
  86. if (val < 0)
  87. debug("error in retrieving GPIO value");
  88. /* bus is not going up again, so we have an error */
  89. if (val != 1)
  90. return 1;
  91. /* all good, presence detected */
  92. return 0;
  93. }
  94. static u8 w1_gpio_triplet(struct udevice *dev, bool bdir)
  95. {
  96. u8 id_bit = w1_gpio_read_bit(dev);
  97. u8 comp_bit = w1_gpio_read_bit(dev);
  98. u8 retval;
  99. if (id_bit && comp_bit)
  100. return 0x03; /* error */
  101. if (!id_bit && !comp_bit) {
  102. /* Both bits are valid, take the direction given */
  103. retval = bdir ? 0x04 : 0;
  104. } else {
  105. /* Only one bit is valid, take that direction */
  106. bdir = id_bit;
  107. retval = id_bit ? 0x05 : 0x02;
  108. }
  109. w1_gpio_write_bit(dev, bdir);
  110. return retval;
  111. }
  112. static const struct w1_ops w1_gpio_ops = {
  113. .read_byte = w1_gpio_read_byte,
  114. .reset = w1_gpio_reset,
  115. .triplet = w1_gpio_triplet,
  116. .write_byte = w1_gpio_write_byte,
  117. };
  118. static int w1_gpio_ofdata_to_platdata(struct udevice *dev)
  119. {
  120. struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
  121. int ret;
  122. ret = gpio_request_by_name(dev, "gpios", 0, &pdata->gpio, 0);
  123. if (ret < 0)
  124. printf("Error claiming GPIO %d\n", ret);
  125. return ret;
  126. };
  127. static const struct udevice_id w1_gpio_id[] = {
  128. { "w1-gpio", 0 },
  129. { },
  130. };
  131. U_BOOT_DRIVER(w1_gpio_drv) = {
  132. .id = UCLASS_W1,
  133. .name = "w1_gpio_drv",
  134. .of_match = w1_gpio_id,
  135. .ofdata_to_platdata = w1_gpio_ofdata_to_platdata,
  136. .ops = &w1_gpio_ops,
  137. .platdata_auto_alloc_size = sizeof(struct w1_gpio_pdata),
  138. };