gpio-ir-recv.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/gpio.h>
  17. #include <linux/slab.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/irq.h>
  20. #include <media/rc-core.h>
  21. #include <media/gpio-ir-recv.h>
  22. #define GPIO_IR_DRIVER_NAME "gpio-rc-recv"
  23. #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
  24. struct gpio_rc_dev {
  25. struct rc_dev *rcdev;
  26. int gpio_nr;
  27. bool active_low;
  28. };
  29. static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
  30. {
  31. struct gpio_rc_dev *gpio_dev = dev_id;
  32. int gval;
  33. int rc = 0;
  34. enum raw_event_type type = IR_SPACE;
  35. gval = gpio_get_value_cansleep(gpio_dev->gpio_nr);
  36. if (gval < 0)
  37. goto err_get_value;
  38. if (gpio_dev->active_low)
  39. gval = !gval;
  40. if (gval == 1)
  41. type = IR_PULSE;
  42. rc = ir_raw_event_store_edge(gpio_dev->rcdev, type);
  43. if (rc < 0)
  44. goto err_get_value;
  45. ir_raw_event_handle(gpio_dev->rcdev);
  46. err_get_value:
  47. return IRQ_HANDLED;
  48. }
  49. static int __devinit gpio_ir_recv_probe(struct platform_device *pdev)
  50. {
  51. struct gpio_rc_dev *gpio_dev;
  52. struct rc_dev *rcdev;
  53. const struct gpio_ir_recv_platform_data *pdata =
  54. pdev->dev.platform_data;
  55. int rc;
  56. if (!pdata)
  57. return -EINVAL;
  58. if (pdata->gpio_nr < 0)
  59. return -EINVAL;
  60. gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL);
  61. if (!gpio_dev)
  62. return -ENOMEM;
  63. rcdev = rc_allocate_device();
  64. if (!rcdev) {
  65. rc = -ENOMEM;
  66. goto err_allocate_device;
  67. }
  68. rcdev->driver_type = RC_DRIVER_IR_RAW;
  69. rcdev->allowed_protos = RC_TYPE_ALL;
  70. rcdev->input_name = GPIO_IR_DEVICE_NAME;
  71. rcdev->input_id.bustype = BUS_HOST;
  72. rcdev->driver_name = GPIO_IR_DRIVER_NAME;
  73. rcdev->map_name = RC_MAP_EMPTY;
  74. gpio_dev->rcdev = rcdev;
  75. gpio_dev->gpio_nr = pdata->gpio_nr;
  76. gpio_dev->active_low = pdata->active_low;
  77. rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
  78. if (rc < 0)
  79. goto err_gpio_request;
  80. rc = gpio_direction_input(pdata->gpio_nr);
  81. if (rc < 0)
  82. goto err_gpio_direction_input;
  83. rc = rc_register_device(rcdev);
  84. if (rc < 0) {
  85. dev_err(&pdev->dev, "failed to register rc device\n");
  86. goto err_register_rc_device;
  87. }
  88. platform_set_drvdata(pdev, gpio_dev);
  89. rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
  90. gpio_ir_recv_irq,
  91. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  92. "gpio-ir-recv-irq", gpio_dev);
  93. if (rc < 0)
  94. goto err_request_irq;
  95. return 0;
  96. err_request_irq:
  97. platform_set_drvdata(pdev, NULL);
  98. rc_unregister_device(rcdev);
  99. err_register_rc_device:
  100. err_gpio_direction_input:
  101. gpio_free(pdata->gpio_nr);
  102. err_gpio_request:
  103. rc_free_device(rcdev);
  104. rcdev = NULL;
  105. err_allocate_device:
  106. kfree(gpio_dev);
  107. return rc;
  108. }
  109. static int __devexit gpio_ir_recv_remove(struct platform_device *pdev)
  110. {
  111. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  112. free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
  113. platform_set_drvdata(pdev, NULL);
  114. rc_unregister_device(gpio_dev->rcdev);
  115. gpio_free(gpio_dev->gpio_nr);
  116. rc_free_device(gpio_dev->rcdev);
  117. kfree(gpio_dev);
  118. return 0;
  119. }
  120. #ifdef CONFIG_PM
  121. static int gpio_ir_recv_suspend(struct device *dev)
  122. {
  123. struct platform_device *pdev = to_platform_device(dev);
  124. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  125. if (device_may_wakeup(dev))
  126. enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  127. else
  128. disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  129. return 0;
  130. }
  131. static int gpio_ir_recv_resume(struct device *dev)
  132. {
  133. struct platform_device *pdev = to_platform_device(dev);
  134. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  135. if (device_may_wakeup(dev))
  136. disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  137. else
  138. enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  139. return 0;
  140. }
  141. static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
  142. .suspend = gpio_ir_recv_suspend,
  143. .resume = gpio_ir_recv_resume,
  144. };
  145. #endif
  146. static struct platform_driver gpio_ir_recv_driver = {
  147. .probe = gpio_ir_recv_probe,
  148. .remove = __devexit_p(gpio_ir_recv_remove),
  149. .driver = {
  150. .name = GPIO_IR_DRIVER_NAME,
  151. .owner = THIS_MODULE,
  152. #ifdef CONFIG_PM
  153. .pm = &gpio_ir_recv_pm_ops,
  154. #endif
  155. },
  156. };
  157. static int __init gpio_ir_recv_init(void)
  158. {
  159. return platform_driver_register(&gpio_ir_recv_driver);
  160. }
  161. module_init(gpio_ir_recv_init);
  162. static void __exit gpio_ir_recv_exit(void)
  163. {
  164. platform_driver_unregister(&gpio_ir_recv_driver);
  165. }
  166. module_exit(gpio_ir_recv_exit);
  167. MODULE_DESCRIPTION("GPIO IR Receiver driver");
  168. MODULE_LICENSE("GPL v2");