w1-gpio.c 5.0 KB

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