w1-gpio.c 4.7 KB

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