w1-gpio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/err.h>
  19. #include <linux/of.h>
  20. #include <linux/delay.h>
  21. #include "../w1.h"
  22. #include "../w1_int.h"
  23. static u8 w1_gpio_set_pullup(void *data, int delay)
  24. {
  25. struct w1_gpio_platform_data *pdata = data;
  26. if (delay) {
  27. pdata->pullup_duration = delay;
  28. } else {
  29. if (pdata->pullup_duration) {
  30. gpio_direction_output(pdata->pin, 1);
  31. msleep(pdata->pullup_duration);
  32. gpio_direction_input(pdata->pin);
  33. }
  34. pdata->pullup_duration = 0;
  35. }
  36. return 0;
  37. }
  38. static void w1_gpio_write_bit_dir(void *data, u8 bit)
  39. {
  40. struct w1_gpio_platform_data *pdata = data;
  41. if (bit)
  42. gpio_direction_input(pdata->pin);
  43. else
  44. gpio_direction_output(pdata->pin, 0);
  45. }
  46. static void w1_gpio_write_bit_val(void *data, u8 bit)
  47. {
  48. struct w1_gpio_platform_data *pdata = data;
  49. gpio_set_value(pdata->pin, bit);
  50. }
  51. static u8 w1_gpio_read_bit(void *data)
  52. {
  53. struct w1_gpio_platform_data *pdata = data;
  54. return gpio_get_value(pdata->pin) ? 1 : 0;
  55. }
  56. #if defined(CONFIG_OF)
  57. static const struct of_device_id w1_gpio_dt_ids[] = {
  58. { .compatible = "w1-gpio" },
  59. {}
  60. };
  61. MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
  62. #endif
  63. static int w1_gpio_probe_dt(struct platform_device *pdev)
  64. {
  65. struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  66. struct device_node *np = pdev->dev.of_node;
  67. int gpio;
  68. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  69. if (!pdata)
  70. return -ENOMEM;
  71. if (of_get_property(np, "linux,open-drain", NULL))
  72. pdata->is_open_drain = 1;
  73. gpio = of_get_gpio(np, 0);
  74. if (gpio < 0) {
  75. if (gpio != -EPROBE_DEFER)
  76. dev_err(&pdev->dev,
  77. "Failed to parse gpio property for data pin (%d)\n",
  78. gpio);
  79. return gpio;
  80. }
  81. pdata->pin = gpio;
  82. gpio = of_get_gpio(np, 1);
  83. if (gpio == -EPROBE_DEFER)
  84. return gpio;
  85. /* ignore other errors as the pullup gpio is optional */
  86. pdata->ext_pullup_enable_pin = gpio;
  87. pdev->dev.platform_data = pdata;
  88. return 0;
  89. }
  90. static int w1_gpio_probe(struct platform_device *pdev)
  91. {
  92. struct w1_bus_master *master;
  93. struct w1_gpio_platform_data *pdata;
  94. int err;
  95. if (of_have_populated_dt()) {
  96. err = w1_gpio_probe_dt(pdev);
  97. if (err < 0)
  98. return err;
  99. }
  100. pdata = dev_get_platdata(&pdev->dev);
  101. if (!pdata) {
  102. dev_err(&pdev->dev, "No configuration data\n");
  103. return -ENXIO;
  104. }
  105. master = devm_kzalloc(&pdev->dev, sizeof(struct w1_bus_master),
  106. GFP_KERNEL);
  107. if (!master) {
  108. dev_err(&pdev->dev, "Out of memory\n");
  109. return -ENOMEM;
  110. }
  111. err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
  112. if (err) {
  113. dev_err(&pdev->dev, "gpio_request (pin) failed\n");
  114. return err;
  115. }
  116. if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
  117. err = devm_gpio_request_one(&pdev->dev,
  118. pdata->ext_pullup_enable_pin, GPIOF_INIT_LOW,
  119. "w1 pullup");
  120. if (err < 0) {
  121. dev_err(&pdev->dev, "gpio_request_one "
  122. "(ext_pullup_enable_pin) failed\n");
  123. return err;
  124. }
  125. }
  126. master->data = pdata;
  127. master->read_bit = w1_gpio_read_bit;
  128. if (pdata->is_open_drain) {
  129. gpio_direction_output(pdata->pin, 1);
  130. master->write_bit = w1_gpio_write_bit_val;
  131. } else {
  132. gpio_direction_input(pdata->pin);
  133. master->write_bit = w1_gpio_write_bit_dir;
  134. master->set_pullup = w1_gpio_set_pullup;
  135. }
  136. err = w1_add_master_device(master);
  137. if (err) {
  138. dev_err(&pdev->dev, "w1_add_master device failed\n");
  139. return err;
  140. }
  141. if (pdata->enable_external_pullup)
  142. pdata->enable_external_pullup(1);
  143. if (gpio_is_valid(pdata->ext_pullup_enable_pin))
  144. gpio_set_value(pdata->ext_pullup_enable_pin, 1);
  145. platform_set_drvdata(pdev, master);
  146. return 0;
  147. }
  148. static int w1_gpio_remove(struct platform_device *pdev)
  149. {
  150. struct w1_bus_master *master = platform_get_drvdata(pdev);
  151. struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  152. if (pdata->enable_external_pullup)
  153. pdata->enable_external_pullup(0);
  154. if (gpio_is_valid(pdata->ext_pullup_enable_pin))
  155. gpio_set_value(pdata->ext_pullup_enable_pin, 0);
  156. w1_remove_master_device(master);
  157. return 0;
  158. }
  159. static int __maybe_unused w1_gpio_suspend(struct device *dev)
  160. {
  161. struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
  162. if (pdata->enable_external_pullup)
  163. pdata->enable_external_pullup(0);
  164. return 0;
  165. }
  166. static int __maybe_unused w1_gpio_resume(struct device *dev)
  167. {
  168. struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
  169. if (pdata->enable_external_pullup)
  170. pdata->enable_external_pullup(1);
  171. return 0;
  172. }
  173. static SIMPLE_DEV_PM_OPS(w1_gpio_pm_ops, w1_gpio_suspend, w1_gpio_resume);
  174. static struct platform_driver w1_gpio_driver = {
  175. .driver = {
  176. .name = "w1-gpio",
  177. .pm = &w1_gpio_pm_ops,
  178. .of_match_table = of_match_ptr(w1_gpio_dt_ids),
  179. },
  180. .probe = w1_gpio_probe,
  181. .remove = w1_gpio_remove,
  182. };
  183. module_platform_driver(w1_gpio_driver);
  184. MODULE_DESCRIPTION("GPIO w1 bus master driver");
  185. MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
  186. MODULE_LICENSE("GPL");