w1-gpio.c 3.2 KB

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