gpio-ir-recv.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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->priv = gpio_dev;
  69. rcdev->driver_type = RC_DRIVER_IR_RAW;
  70. rcdev->allowed_protos = RC_TYPE_ALL;
  71. rcdev->input_name = GPIO_IR_DEVICE_NAME;
  72. rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
  73. rcdev->input_id.bustype = BUS_HOST;
  74. rcdev->input_id.vendor = 0x0001;
  75. rcdev->input_id.product = 0x0001;
  76. rcdev->input_id.version = 0x0100;
  77. rcdev->dev.parent = &pdev->dev;
  78. rcdev->driver_name = GPIO_IR_DRIVER_NAME;
  79. rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY;
  80. gpio_dev->rcdev = rcdev;
  81. gpio_dev->gpio_nr = pdata->gpio_nr;
  82. gpio_dev->active_low = pdata->active_low;
  83. rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
  84. if (rc < 0)
  85. goto err_gpio_request;
  86. rc = gpio_direction_input(pdata->gpio_nr);
  87. if (rc < 0)
  88. goto err_gpio_direction_input;
  89. rc = rc_register_device(rcdev);
  90. if (rc < 0) {
  91. dev_err(&pdev->dev, "failed to register rc device\n");
  92. goto err_register_rc_device;
  93. }
  94. platform_set_drvdata(pdev, gpio_dev);
  95. rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
  96. gpio_ir_recv_irq,
  97. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  98. "gpio-ir-recv-irq", gpio_dev);
  99. if (rc < 0)
  100. goto err_request_irq;
  101. return 0;
  102. err_request_irq:
  103. platform_set_drvdata(pdev, NULL);
  104. rc_unregister_device(rcdev);
  105. err_register_rc_device:
  106. err_gpio_direction_input:
  107. gpio_free(pdata->gpio_nr);
  108. err_gpio_request:
  109. rc_free_device(rcdev);
  110. rcdev = NULL;
  111. err_allocate_device:
  112. kfree(gpio_dev);
  113. return rc;
  114. }
  115. static int __devexit gpio_ir_recv_remove(struct platform_device *pdev)
  116. {
  117. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  118. free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
  119. platform_set_drvdata(pdev, NULL);
  120. rc_unregister_device(gpio_dev->rcdev);
  121. gpio_free(gpio_dev->gpio_nr);
  122. rc_free_device(gpio_dev->rcdev);
  123. kfree(gpio_dev);
  124. return 0;
  125. }
  126. #ifdef CONFIG_PM
  127. static int gpio_ir_recv_suspend(struct device *dev)
  128. {
  129. struct platform_device *pdev = to_platform_device(dev);
  130. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  131. if (device_may_wakeup(dev))
  132. enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  133. else
  134. disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  135. return 0;
  136. }
  137. static int gpio_ir_recv_resume(struct device *dev)
  138. {
  139. struct platform_device *pdev = to_platform_device(dev);
  140. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  141. if (device_may_wakeup(dev))
  142. disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  143. else
  144. enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  145. return 0;
  146. }
  147. static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
  148. .suspend = gpio_ir_recv_suspend,
  149. .resume = gpio_ir_recv_resume,
  150. };
  151. #endif
  152. static struct platform_driver gpio_ir_recv_driver = {
  153. .probe = gpio_ir_recv_probe,
  154. .remove = __devexit_p(gpio_ir_recv_remove),
  155. .driver = {
  156. .name = GPIO_IR_DRIVER_NAME,
  157. .owner = THIS_MODULE,
  158. #ifdef CONFIG_PM
  159. .pm = &gpio_ir_recv_pm_ops,
  160. #endif
  161. },
  162. };
  163. static int __init gpio_ir_recv_init(void)
  164. {
  165. return platform_driver_register(&gpio_ir_recv_driver);
  166. }
  167. module_init(gpio_ir_recv_init);
  168. static void __exit gpio_ir_recv_exit(void)
  169. {
  170. platform_driver_unregister(&gpio_ir_recv_driver);
  171. }
  172. module_exit(gpio_ir_recv_exit);
  173. MODULE_DESCRIPTION("GPIO IR Receiver driver");
  174. MODULE_LICENSE("GPL v2");