w1-gpio.c 5.0 KB

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