gpio-ir-recv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/of_gpio.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/irq.h>
  21. #include <media/rc-core.h>
  22. #include <media/gpio-ir-recv.h>
  23. #define GPIO_IR_DRIVER_NAME "gpio-rc-recv"
  24. #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
  25. struct gpio_rc_dev {
  26. struct rc_dev *rcdev;
  27. int gpio_nr;
  28. bool active_low;
  29. };
  30. #ifdef CONFIG_OF
  31. /*
  32. * Translate OpenFirmware node properties into platform_data
  33. */
  34. static int gpio_ir_recv_get_devtree_pdata(struct device *dev,
  35. struct gpio_ir_recv_platform_data *pdata)
  36. {
  37. struct device_node *np = dev->of_node;
  38. enum of_gpio_flags flags;
  39. int gpio;
  40. gpio = of_get_gpio_flags(np, 0, &flags);
  41. if (gpio < 0) {
  42. if (gpio != -EPROBE_DEFER)
  43. dev_err(dev, "Failed to get gpio flags (%d)\n", gpio);
  44. return gpio;
  45. }
  46. pdata->gpio_nr = gpio;
  47. pdata->active_low = (flags & OF_GPIO_ACTIVE_LOW);
  48. /* probe() takes care of map_name == NULL or allowed_protos == 0 */
  49. pdata->map_name = of_get_property(np, "linux,rc-map-name", NULL);
  50. pdata->allowed_protos = 0;
  51. return 0;
  52. }
  53. static struct of_device_id gpio_ir_recv_of_match[] = {
  54. { .compatible = "gpio-ir-receiver", },
  55. { },
  56. };
  57. MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
  58. #else /* !CONFIG_OF */
  59. #define gpio_ir_recv_get_devtree_pdata(dev, pdata) (-ENOSYS)
  60. #endif
  61. static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
  62. {
  63. struct gpio_rc_dev *gpio_dev = dev_id;
  64. int gval;
  65. int rc = 0;
  66. enum raw_event_type type = IR_SPACE;
  67. gval = gpio_get_value_cansleep(gpio_dev->gpio_nr);
  68. if (gval < 0)
  69. goto err_get_value;
  70. if (gpio_dev->active_low)
  71. gval = !gval;
  72. if (gval == 1)
  73. type = IR_PULSE;
  74. rc = ir_raw_event_store_edge(gpio_dev->rcdev, type);
  75. if (rc < 0)
  76. goto err_get_value;
  77. ir_raw_event_handle(gpio_dev->rcdev);
  78. err_get_value:
  79. return IRQ_HANDLED;
  80. }
  81. static int gpio_ir_recv_probe(struct platform_device *pdev)
  82. {
  83. struct gpio_rc_dev *gpio_dev;
  84. struct rc_dev *rcdev;
  85. const struct gpio_ir_recv_platform_data *pdata =
  86. pdev->dev.platform_data;
  87. int rc;
  88. if (pdev->dev.of_node) {
  89. struct gpio_ir_recv_platform_data *dtpdata =
  90. devm_kzalloc(&pdev->dev, sizeof(*dtpdata), GFP_KERNEL);
  91. if (!dtpdata)
  92. return -ENOMEM;
  93. rc = gpio_ir_recv_get_devtree_pdata(&pdev->dev, dtpdata);
  94. if (rc)
  95. return rc;
  96. pdata = dtpdata;
  97. }
  98. if (!pdata)
  99. return -EINVAL;
  100. if (pdata->gpio_nr < 0)
  101. return -EINVAL;
  102. gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL);
  103. if (!gpio_dev)
  104. return -ENOMEM;
  105. rcdev = rc_allocate_device();
  106. if (!rcdev) {
  107. rc = -ENOMEM;
  108. goto err_allocate_device;
  109. }
  110. rcdev->priv = gpio_dev;
  111. rcdev->driver_type = RC_DRIVER_IR_RAW;
  112. rcdev->input_name = GPIO_IR_DEVICE_NAME;
  113. rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
  114. rcdev->input_id.bustype = BUS_HOST;
  115. rcdev->input_id.vendor = 0x0001;
  116. rcdev->input_id.product = 0x0001;
  117. rcdev->input_id.version = 0x0100;
  118. rcdev->dev.parent = &pdev->dev;
  119. rcdev->driver_name = GPIO_IR_DRIVER_NAME;
  120. if (pdata->allowed_protos)
  121. rcdev->allowed_protos = pdata->allowed_protos;
  122. else
  123. rcdev->allowed_protos = RC_BIT_ALL;
  124. rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY;
  125. gpio_dev->rcdev = rcdev;
  126. gpio_dev->gpio_nr = pdata->gpio_nr;
  127. gpio_dev->active_low = pdata->active_low;
  128. rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
  129. if (rc < 0)
  130. goto err_gpio_request;
  131. rc = gpio_direction_input(pdata->gpio_nr);
  132. if (rc < 0)
  133. goto err_gpio_direction_input;
  134. rc = rc_register_device(rcdev);
  135. if (rc < 0) {
  136. dev_err(&pdev->dev, "failed to register rc device\n");
  137. goto err_register_rc_device;
  138. }
  139. platform_set_drvdata(pdev, gpio_dev);
  140. rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
  141. gpio_ir_recv_irq,
  142. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  143. "gpio-ir-recv-irq", gpio_dev);
  144. if (rc < 0)
  145. goto err_request_irq;
  146. return 0;
  147. err_request_irq:
  148. platform_set_drvdata(pdev, NULL);
  149. rc_unregister_device(rcdev);
  150. rcdev = NULL;
  151. err_register_rc_device:
  152. err_gpio_direction_input:
  153. gpio_free(pdata->gpio_nr);
  154. err_gpio_request:
  155. rc_free_device(rcdev);
  156. err_allocate_device:
  157. kfree(gpio_dev);
  158. return rc;
  159. }
  160. static int gpio_ir_recv_remove(struct platform_device *pdev)
  161. {
  162. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  163. free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
  164. platform_set_drvdata(pdev, NULL);
  165. rc_unregister_device(gpio_dev->rcdev);
  166. gpio_free(gpio_dev->gpio_nr);
  167. kfree(gpio_dev);
  168. return 0;
  169. }
  170. #ifdef CONFIG_PM
  171. static int gpio_ir_recv_suspend(struct device *dev)
  172. {
  173. struct platform_device *pdev = to_platform_device(dev);
  174. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  175. if (device_may_wakeup(dev))
  176. enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  177. else
  178. disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  179. return 0;
  180. }
  181. static int gpio_ir_recv_resume(struct device *dev)
  182. {
  183. struct platform_device *pdev = to_platform_device(dev);
  184. struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
  185. if (device_may_wakeup(dev))
  186. disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
  187. else
  188. enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
  189. return 0;
  190. }
  191. static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
  192. .suspend = gpio_ir_recv_suspend,
  193. .resume = gpio_ir_recv_resume,
  194. };
  195. #endif
  196. static struct platform_driver gpio_ir_recv_driver = {
  197. .probe = gpio_ir_recv_probe,
  198. .remove = gpio_ir_recv_remove,
  199. .driver = {
  200. .name = GPIO_IR_DRIVER_NAME,
  201. .owner = THIS_MODULE,
  202. .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
  203. #ifdef CONFIG_PM
  204. .pm = &gpio_ir_recv_pm_ops,
  205. #endif
  206. },
  207. };
  208. module_platform_driver(gpio_ir_recv_driver);
  209. MODULE_DESCRIPTION("GPIO IR Receiver driver");
  210. MODULE_LICENSE("GPL v2");