switch_gpio.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * drivers/switch/switch_gpio.c
  3. *
  4. * Copyright (C) 2008 Google, Inc.
  5. * Author: Mike Lockwood <lockwood@android.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/switch.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/gpio.h>
  26. struct gpio_switch_data {
  27. struct switch_dev sdev;
  28. unsigned gpio;
  29. const char *name_on;
  30. const char *name_off;
  31. const char *state_on;
  32. const char *state_off;
  33. int irq;
  34. struct work_struct work;
  35. };
  36. static void gpio_switch_work(struct work_struct *work)
  37. {
  38. int state;
  39. struct gpio_switch_data *data =
  40. container_of(work, struct gpio_switch_data, work);
  41. state = gpio_get_value(data->gpio);
  42. switch_set_state(&data->sdev, state);
  43. }
  44. static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
  45. {
  46. struct gpio_switch_data *switch_data =
  47. (struct gpio_switch_data *)dev_id;
  48. schedule_work(&switch_data->work);
  49. return IRQ_HANDLED;
  50. }
  51. static ssize_t switch_gpio_print_state(struct switch_dev *sdev, char *buf)
  52. {
  53. struct gpio_switch_data *switch_data =
  54. container_of(sdev, struct gpio_switch_data, sdev);
  55. const char *state;
  56. if (switch_get_state(sdev))
  57. state = switch_data->state_on;
  58. else
  59. state = switch_data->state_off;
  60. if (state)
  61. return sprintf(buf, "%s\n", state);
  62. return -1;
  63. }
  64. static int gpio_switch_probe(struct platform_device *pdev)
  65. {
  66. struct gpio_switch_platform_data *pdata = pdev->dev.platform_data;
  67. struct gpio_switch_data *switch_data;
  68. int ret = 0;
  69. if (!pdata)
  70. return -EBUSY;
  71. switch_data = kzalloc(sizeof(struct gpio_switch_data), GFP_KERNEL);
  72. if (!switch_data)
  73. return -ENOMEM;
  74. switch_data->sdev.name = pdata->name;
  75. switch_data->gpio = pdata->gpio;
  76. switch_data->name_on = pdata->name_on;
  77. switch_data->name_off = pdata->name_off;
  78. switch_data->state_on = pdata->state_on;
  79. switch_data->state_off = pdata->state_off;
  80. switch_data->sdev.print_state = switch_gpio_print_state;
  81. ret = switch_dev_register(&switch_data->sdev);
  82. if (ret < 0)
  83. goto err_switch_dev_register;
  84. ret = gpio_request(switch_data->gpio, pdev->name);
  85. if (ret < 0)
  86. goto err_request_gpio;
  87. ret = gpio_direction_input(switch_data->gpio);
  88. if (ret < 0)
  89. goto err_set_gpio_input;
  90. INIT_WORK(&switch_data->work, gpio_switch_work);
  91. switch_data->irq = gpio_to_irq(switch_data->gpio);
  92. if (switch_data->irq < 0) {
  93. ret = switch_data->irq;
  94. goto err_detect_irq_num_failed;
  95. }
  96. ret = request_irq(switch_data->irq, gpio_irq_handler,
  97. IRQF_TRIGGER_LOW, pdev->name, switch_data);
  98. if (ret < 0)
  99. goto err_request_irq;
  100. /* Perform initial detection */
  101. gpio_switch_work(&switch_data->work);
  102. return 0;
  103. err_request_irq:
  104. err_detect_irq_num_failed:
  105. err_set_gpio_input:
  106. gpio_free(switch_data->gpio);
  107. err_request_gpio:
  108. switch_dev_unregister(&switch_data->sdev);
  109. err_switch_dev_register:
  110. kfree(switch_data);
  111. return ret;
  112. }
  113. static int __devexit gpio_switch_remove(struct platform_device *pdev)
  114. {
  115. struct gpio_switch_data *switch_data = platform_get_drvdata(pdev);
  116. cancel_work_sync(&switch_data->work);
  117. gpio_free(switch_data->gpio);
  118. switch_dev_unregister(&switch_data->sdev);
  119. kfree(switch_data);
  120. return 0;
  121. }
  122. static struct platform_driver gpio_switch_driver = {
  123. .probe = gpio_switch_probe,
  124. .remove = __devexit_p(gpio_switch_remove),
  125. .driver = {
  126. .name = "switch-gpio",
  127. .owner = THIS_MODULE,
  128. },
  129. };
  130. static int __init gpio_switch_init(void)
  131. {
  132. return platform_driver_register(&gpio_switch_driver);
  133. }
  134. static void __exit gpio_switch_exit(void)
  135. {
  136. platform_driver_unregister(&gpio_switch_driver);
  137. }
  138. module_init(gpio_switch_init);
  139. module_exit(gpio_switch_exit);
  140. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  141. MODULE_DESCRIPTION("GPIO Switch driver");
  142. MODULE_LICENSE("GPL");