gpio-ir-recv.c 4.8 KB

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