w1-gpio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "../w1.h"
  16. #include "../w1_int.h"
  17. #include <asm/gpio.h>
  18. static void w1_gpio_write_bit_dir(void *data, u8 bit)
  19. {
  20. struct w1_gpio_platform_data *pdata = data;
  21. if (bit)
  22. gpio_direction_input(pdata->pin);
  23. else
  24. gpio_direction_output(pdata->pin, 0);
  25. }
  26. static void w1_gpio_write_bit_val(void *data, u8 bit)
  27. {
  28. struct w1_gpio_platform_data *pdata = data;
  29. gpio_set_value(pdata->pin, bit);
  30. }
  31. static u8 w1_gpio_read_bit(void *data)
  32. {
  33. struct w1_gpio_platform_data *pdata = data;
  34. return gpio_get_value(pdata->pin) ? 1 : 0;
  35. }
  36. static int __init w1_gpio_probe(struct platform_device *pdev)
  37. {
  38. struct w1_bus_master *master;
  39. struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
  40. int err;
  41. if (!pdata)
  42. return -ENXIO;
  43. master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
  44. if (!master)
  45. return -ENOMEM;
  46. err = gpio_request(pdata->pin, "w1");
  47. if (err)
  48. goto free_master;
  49. master->data = pdata;
  50. master->read_bit = w1_gpio_read_bit;
  51. if (pdata->is_open_drain) {
  52. gpio_direction_output(pdata->pin, 1);
  53. master->write_bit = w1_gpio_write_bit_val;
  54. } else {
  55. gpio_direction_input(pdata->pin);
  56. master->write_bit = w1_gpio_write_bit_dir;
  57. }
  58. err = w1_add_master_device(master);
  59. if (err)
  60. goto free_gpio;
  61. if (pdata->enable_external_pullup)
  62. pdata->enable_external_pullup(1);
  63. platform_set_drvdata(pdev, master);
  64. return 0;
  65. free_gpio:
  66. gpio_free(pdata->pin);
  67. free_master:
  68. kfree(master);
  69. return err;
  70. }
  71. static int __exit w1_gpio_remove(struct platform_device *pdev)
  72. {
  73. struct w1_bus_master *master = platform_get_drvdata(pdev);
  74. struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
  75. if (pdata->enable_external_pullup)
  76. pdata->enable_external_pullup(0);
  77. w1_remove_master_device(master);
  78. gpio_free(pdata->pin);
  79. kfree(master);
  80. return 0;
  81. }
  82. #ifdef CONFIG_PM
  83. static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
  84. {
  85. struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
  86. if (pdata->enable_external_pullup)
  87. pdata->enable_external_pullup(0);
  88. return 0;
  89. }
  90. static int w1_gpio_resume(struct platform_device *pdev)
  91. {
  92. struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
  93. if (pdata->enable_external_pullup)
  94. pdata->enable_external_pullup(1);
  95. return 0;
  96. }
  97. #else
  98. #define w1_gpio_suspend NULL
  99. #define w1_gpio_resume NULL
  100. #endif
  101. static struct platform_driver w1_gpio_driver = {
  102. .driver = {
  103. .name = "w1-gpio",
  104. .owner = THIS_MODULE,
  105. },
  106. .remove = __exit_p(w1_gpio_remove),
  107. .suspend = w1_gpio_suspend,
  108. .resume = w1_gpio_resume,
  109. };
  110. static int __init w1_gpio_init(void)
  111. {
  112. return platform_driver_probe(&w1_gpio_driver, w1_gpio_probe);
  113. }
  114. static void __exit w1_gpio_exit(void)
  115. {
  116. platform_driver_unregister(&w1_gpio_driver);
  117. }
  118. module_init(w1_gpio_init);
  119. module_exit(w1_gpio_exit);
  120. MODULE_DESCRIPTION("GPIO w1 bus master driver");
  121. MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
  122. MODULE_LICENSE("GPL");