w1-gpio.c 3.8 KB

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