w1-gpio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * w1-gpio - GPIO w1 bus master driver
  3. *
  4. * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/w1-gpio.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/err.h>
  18. #include <linux/of.h>
  19. #include <linux/delay.h>
  20. #include <linux/w1.h>
  21. static u8 w1_gpio_set_pullup(void *data, int delay)
  22. {
  23. struct w1_gpio_platform_data *pdata = data;
  24. if (delay) {
  25. pdata->pullup_duration = delay;
  26. } else {
  27. if (pdata->pullup_duration) {
  28. /*
  29. * This will OVERRIDE open drain emulation and force-pull
  30. * the line high for some time.
  31. */
  32. gpiod_set_raw_value(pdata->gpiod, 1);
  33. msleep(pdata->pullup_duration);
  34. /*
  35. * This will simply set the line as input since we are doing
  36. * open drain emulation in the GPIO library.
  37. */
  38. gpiod_set_value(pdata->gpiod, 1);
  39. }
  40. pdata->pullup_duration = 0;
  41. }
  42. return 0;
  43. }
  44. static void w1_gpio_write_bit(void *data, u8 bit)
  45. {
  46. struct w1_gpio_platform_data *pdata = data;
  47. gpiod_set_value(pdata->gpiod, bit);
  48. }
  49. static u8 w1_gpio_read_bit(void *data)
  50. {
  51. struct w1_gpio_platform_data *pdata = data;
  52. return gpiod_get_value(pdata->gpiod) ? 1 : 0;
  53. }
  54. #if defined(CONFIG_OF)
  55. static const struct of_device_id w1_gpio_dt_ids[] = {
  56. { .compatible = "w1-gpio" },
  57. {}
  58. };
  59. MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
  60. #endif
  61. static int w1_gpio_probe(struct platform_device *pdev)
  62. {
  63. struct w1_bus_master *master;
  64. struct w1_gpio_platform_data *pdata;
  65. struct device *dev = &pdev->dev;
  66. struct device_node *np = dev->of_node;
  67. /* Enforce open drain mode by default */
  68. enum gpiod_flags gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
  69. int err;
  70. if (of_have_populated_dt()) {
  71. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  72. if (!pdata)
  73. return -ENOMEM;
  74. /*
  75. * This parameter means that something else than the gpiolib has
  76. * already set the line into open drain mode, so we should just
  77. * driver it high/low like we are in full control of the line and
  78. * open drain will happen transparently.
  79. */
  80. if (of_get_property(np, "linux,open-drain", NULL))
  81. gflags = GPIOD_OUT_LOW;
  82. pdev->dev.platform_data = pdata;
  83. }
  84. pdata = dev_get_platdata(dev);
  85. if (!pdata) {
  86. dev_err(dev, "No configuration data\n");
  87. return -ENXIO;
  88. }
  89. master = devm_kzalloc(dev, sizeof(struct w1_bus_master),
  90. GFP_KERNEL);
  91. if (!master) {
  92. dev_err(dev, "Out of memory\n");
  93. return -ENOMEM;
  94. }
  95. pdata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags);
  96. if (IS_ERR(pdata->gpiod)) {
  97. dev_err(dev, "gpio_request (pin) failed\n");
  98. return PTR_ERR(pdata->gpiod);
  99. }
  100. pdata->pullup_gpiod =
  101. devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW);
  102. if (IS_ERR(pdata->pullup_gpiod)) {
  103. dev_err(dev, "gpio_request_one "
  104. "(ext_pullup_enable_pin) failed\n");
  105. return PTR_ERR(pdata->pullup_gpiod);
  106. }
  107. master->data = pdata;
  108. master->read_bit = w1_gpio_read_bit;
  109. gpiod_direction_output(pdata->gpiod, 1);
  110. master->write_bit = w1_gpio_write_bit;
  111. /*
  112. * If we are using open drain emulation from the GPIO library,
  113. * we need to use this pullup function that hammers the line
  114. * high using a raw accessor to provide pull-up for the w1
  115. * line.
  116. */
  117. if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)
  118. master->set_pullup = w1_gpio_set_pullup;
  119. err = w1_add_master_device(master);
  120. if (err) {
  121. dev_err(dev, "w1_add_master device failed\n");
  122. return err;
  123. }
  124. if (pdata->enable_external_pullup)
  125. pdata->enable_external_pullup(1);
  126. if (pdata->pullup_gpiod)
  127. gpiod_set_value(pdata->pullup_gpiod, 1);
  128. platform_set_drvdata(pdev, master);
  129. return 0;
  130. }
  131. static int w1_gpio_remove(struct platform_device *pdev)
  132. {
  133. struct w1_bus_master *master = platform_get_drvdata(pdev);
  134. struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  135. if (pdata->enable_external_pullup)
  136. pdata->enable_external_pullup(0);
  137. if (pdata->pullup_gpiod)
  138. gpiod_set_value(pdata->pullup_gpiod, 0);
  139. w1_remove_master_device(master);
  140. return 0;
  141. }
  142. static int __maybe_unused w1_gpio_suspend(struct device *dev)
  143. {
  144. struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
  145. if (pdata->enable_external_pullup)
  146. pdata->enable_external_pullup(0);
  147. return 0;
  148. }
  149. static int __maybe_unused w1_gpio_resume(struct device *dev)
  150. {
  151. struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
  152. if (pdata->enable_external_pullup)
  153. pdata->enable_external_pullup(1);
  154. return 0;
  155. }
  156. static SIMPLE_DEV_PM_OPS(w1_gpio_pm_ops, w1_gpio_suspend, w1_gpio_resume);
  157. static struct platform_driver w1_gpio_driver = {
  158. .driver = {
  159. .name = "w1-gpio",
  160. .pm = &w1_gpio_pm_ops,
  161. .of_match_table = of_match_ptr(w1_gpio_dt_ids),
  162. },
  163. .probe = w1_gpio_probe,
  164. .remove = w1_gpio_remove,
  165. };
  166. module_platform_driver(w1_gpio_driver);
  167. MODULE_DESCRIPTION("GPIO w1 bus master driver");
  168. MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
  169. MODULE_LICENSE("GPL");