gpio-ir-recv.c 6.0 KB

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