w1-gpio.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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);
  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. platform_set_drvdata(pdev, master);
  61. return 0;
  62. free_gpio:
  63. gpio_free(pdata->pin);
  64. free_master:
  65. kfree(master);
  66. return err;
  67. }
  68. static int __exit w1_gpio_remove(struct platform_device *pdev)
  69. {
  70. struct w1_bus_master *master = platform_get_drvdata(pdev);
  71. struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
  72. w1_remove_master_device(master);
  73. gpio_free(pdata->pin);
  74. kfree(master);
  75. return 0;
  76. }
  77. static struct platform_driver w1_gpio_driver = {
  78. .driver = {
  79. .name = "w1-gpio",
  80. .owner = THIS_MODULE,
  81. },
  82. .remove = __exit_p(w1_gpio_remove),
  83. };
  84. static int __init w1_gpio_init(void)
  85. {
  86. return platform_driver_probe(&w1_gpio_driver, w1_gpio_probe);
  87. }
  88. static void __exit w1_gpio_exit(void)
  89. {
  90. platform_driver_unregister(&w1_gpio_driver);
  91. }
  92. module_init(w1_gpio_init);
  93. module_exit(w1_gpio_exit);
  94. MODULE_DESCRIPTION("GPIO w1 bus master driver");
  95. MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
  96. MODULE_LICENSE("GPL");