w1-gpio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * w1-gpio - GPIO w1 bus master driver
  4. *
  5. * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/types.h>
  16. #include <linux/w1.h>
  17. struct w1_gpio_ddata {
  18. struct gpio_desc *gpiod;
  19. struct gpio_desc *pullup_gpiod;
  20. unsigned int pullup_duration;
  21. };
  22. static u8 w1_gpio_set_pullup(void *data, int delay)
  23. {
  24. struct w1_gpio_ddata *ddata = data;
  25. if (delay) {
  26. ddata->pullup_duration = delay;
  27. } else {
  28. if (ddata->pullup_duration) {
  29. /*
  30. * This will OVERRIDE open drain emulation and force-pull
  31. * the line high for some time.
  32. */
  33. gpiod_set_raw_value(ddata->gpiod, 1);
  34. msleep(ddata->pullup_duration);
  35. /*
  36. * This will simply set the line as input since we are doing
  37. * open drain emulation in the GPIO library.
  38. */
  39. gpiod_set_value(ddata->gpiod, 1);
  40. }
  41. ddata->pullup_duration = 0;
  42. }
  43. return 0;
  44. }
  45. static void w1_gpio_write_bit(void *data, u8 bit)
  46. {
  47. struct w1_gpio_ddata *ddata = data;
  48. gpiod_set_value(ddata->gpiod, bit);
  49. }
  50. static u8 w1_gpio_read_bit(void *data)
  51. {
  52. struct w1_gpio_ddata *ddata = data;
  53. return gpiod_get_value(ddata->gpiod) ? 1 : 0;
  54. }
  55. static int w1_gpio_probe(struct platform_device *pdev)
  56. {
  57. struct w1_bus_master *master;
  58. struct w1_gpio_ddata *ddata;
  59. struct device *dev = &pdev->dev;
  60. /* Enforce open drain mode by default */
  61. enum gpiod_flags gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
  62. int err;
  63. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  64. if (!ddata)
  65. return -ENOMEM;
  66. /*
  67. * This parameter means that something else than the gpiolib has
  68. * already set the line into open drain mode, so we should just
  69. * driver it high/low like we are in full control of the line and
  70. * open drain will happen transparently.
  71. */
  72. if (device_property_present(dev, "linux,open-drain"))
  73. gflags = GPIOD_OUT_LOW;
  74. master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL);
  75. if (!master)
  76. return -ENOMEM;
  77. ddata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags);
  78. if (IS_ERR(ddata->gpiod))
  79. return dev_err_probe(dev, PTR_ERR(ddata->gpiod), "gpio_request (pin) failed\n");
  80. ddata->pullup_gpiod =
  81. devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW);
  82. if (IS_ERR(ddata->pullup_gpiod))
  83. return dev_err_probe(dev, PTR_ERR(ddata->pullup_gpiod),
  84. "gpio_request (ext_pullup_enable_pin) failed\n");
  85. master->data = ddata;
  86. master->read_bit = w1_gpio_read_bit;
  87. gpiod_direction_output(ddata->gpiod, 1);
  88. master->write_bit = w1_gpio_write_bit;
  89. /*
  90. * If we are using open drain emulation from the GPIO library,
  91. * we need to use this pullup function that hammers the line
  92. * high using a raw accessor to provide pull-up for the w1
  93. * line.
  94. */
  95. if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)
  96. master->set_pullup = w1_gpio_set_pullup;
  97. err = w1_add_master_device(master);
  98. if (err)
  99. return dev_err_probe(dev, err, "w1_add_master device failed\n");
  100. gpiod_set_value(ddata->pullup_gpiod, 1);
  101. platform_set_drvdata(pdev, master);
  102. return 0;
  103. }
  104. static void w1_gpio_remove(struct platform_device *pdev)
  105. {
  106. struct w1_bus_master *master = platform_get_drvdata(pdev);
  107. struct w1_gpio_ddata *ddata = master->data;
  108. gpiod_set_value(ddata->pullup_gpiod, 0);
  109. w1_remove_master_device(master);
  110. }
  111. static const struct of_device_id w1_gpio_dt_ids[] = {
  112. { .compatible = "w1-gpio" },
  113. {}
  114. };
  115. MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
  116. static struct platform_driver w1_gpio_driver = {
  117. .driver = {
  118. .name = "w1-gpio",
  119. .of_match_table = w1_gpio_dt_ids,
  120. },
  121. .probe = w1_gpio_probe,
  122. .remove = w1_gpio_remove,
  123. };
  124. module_platform_driver(w1_gpio_driver);
  125. MODULE_DESCRIPTION("GPIO w1 bus master driver");
  126. MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
  127. MODULE_LICENSE("GPL");