w1-gpio.c 4.8 KB

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