w1-gpio.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 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. return gpio;
  76. pdata->pin = gpio;
  77. pdata->ext_pullup_enable_pin = of_get_gpio(np, 1);
  78. pdev->dev.platform_data = pdata;
  79. return 0;
  80. }
  81. static int w1_gpio_probe(struct platform_device *pdev)
  82. {
  83. struct w1_bus_master *master;
  84. struct w1_gpio_platform_data *pdata;
  85. int err;
  86. if (of_have_populated_dt()) {
  87. err = w1_gpio_probe_dt(pdev);
  88. if (err < 0) {
  89. dev_err(&pdev->dev, "Failed to parse DT\n");
  90. return err;
  91. }
  92. }
  93. pdata = dev_get_platdata(&pdev->dev);
  94. if (!pdata) {
  95. dev_err(&pdev->dev, "No configuration data\n");
  96. return -ENXIO;
  97. }
  98. master = devm_kzalloc(&pdev->dev, sizeof(struct w1_bus_master),
  99. GFP_KERNEL);
  100. if (!master) {
  101. dev_err(&pdev->dev, "Out of memory\n");
  102. return -ENOMEM;
  103. }
  104. err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
  105. if (err) {
  106. dev_err(&pdev->dev, "gpio_request (pin) failed\n");
  107. return err;
  108. }
  109. if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
  110. err = devm_gpio_request_one(&pdev->dev,
  111. pdata->ext_pullup_enable_pin, GPIOF_INIT_LOW,
  112. "w1 pullup");
  113. if (err < 0) {
  114. dev_err(&pdev->dev, "gpio_request_one "
  115. "(ext_pullup_enable_pin) failed\n");
  116. return err;
  117. }
  118. }
  119. master->data = pdata;
  120. master->read_bit = w1_gpio_read_bit;
  121. if (pdata->is_open_drain) {
  122. gpio_direction_output(pdata->pin, 1);
  123. master->write_bit = w1_gpio_write_bit_val;
  124. } else {
  125. gpio_direction_input(pdata->pin);
  126. master->write_bit = w1_gpio_write_bit_dir;
  127. master->set_pullup = w1_gpio_set_pullup;
  128. }
  129. err = w1_add_master_device(master);
  130. if (err) {
  131. dev_err(&pdev->dev, "w1_add_master device failed\n");
  132. return err;
  133. }
  134. if (pdata->enable_external_pullup)
  135. pdata->enable_external_pullup(1);
  136. if (gpio_is_valid(pdata->ext_pullup_enable_pin))
  137. gpio_set_value(pdata->ext_pullup_enable_pin, 1);
  138. platform_set_drvdata(pdev, master);
  139. return 0;
  140. }
  141. static int w1_gpio_remove(struct platform_device *pdev)
  142. {
  143. struct w1_bus_master *master = platform_get_drvdata(pdev);
  144. struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  145. if (pdata->enable_external_pullup)
  146. pdata->enable_external_pullup(0);
  147. if (gpio_is_valid(pdata->ext_pullup_enable_pin))
  148. gpio_set_value(pdata->ext_pullup_enable_pin, 0);
  149. w1_remove_master_device(master);
  150. return 0;
  151. }
  152. #ifdef CONFIG_PM
  153. static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
  154. {
  155. struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  156. if (pdata->enable_external_pullup)
  157. pdata->enable_external_pullup(0);
  158. return 0;
  159. }
  160. static int w1_gpio_resume(struct platform_device *pdev)
  161. {
  162. struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  163. if (pdata->enable_external_pullup)
  164. pdata->enable_external_pullup(1);
  165. return 0;
  166. }
  167. #else
  168. #define w1_gpio_suspend NULL
  169. #define w1_gpio_resume NULL
  170. #endif
  171. static struct platform_driver w1_gpio_driver = {
  172. .driver = {
  173. .name = "w1-gpio",
  174. .owner = THIS_MODULE,
  175. .of_match_table = of_match_ptr(w1_gpio_dt_ids),
  176. },
  177. .probe = w1_gpio_probe,
  178. .remove = w1_gpio_remove,
  179. .suspend = w1_gpio_suspend,
  180. .resume = w1_gpio_resume,
  181. };
  182. module_platform_driver(w1_gpio_driver);
  183. MODULE_DESCRIPTION("GPIO w1 bus master driver");
  184. MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
  185. MODULE_LICENSE("GPL");