gpio-ir-recv.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/consumer.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. #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
  24. struct gpio_rc_dev {
  25. struct rc_dev *rcdev;
  26. struct gpio_desc *gpiod;
  27. int irq;
  28. };
  29. static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
  30. {
  31. int val;
  32. struct gpio_rc_dev *gpio_dev = dev_id;
  33. val = gpiod_get_value(gpio_dev->gpiod);
  34. if (val >= 0)
  35. ir_raw_event_store_edge(gpio_dev->rcdev, val == 1);
  36. return IRQ_HANDLED;
  37. }
  38. static int gpio_ir_recv_probe(struct platform_device *pdev)
  39. {
  40. struct device *dev = &pdev->dev;
  41. struct device_node *np = dev->of_node;
  42. struct gpio_rc_dev *gpio_dev;
  43. struct rc_dev *rcdev;
  44. int rc;
  45. if (!np)
  46. return -ENODEV;
  47. gpio_dev = devm_kzalloc(dev, sizeof(*gpio_dev), GFP_KERNEL);
  48. if (!gpio_dev)
  49. return -ENOMEM;
  50. gpio_dev->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
  51. if (IS_ERR(gpio_dev->gpiod)) {
  52. rc = PTR_ERR(gpio_dev->gpiod);
  53. /* Just try again if this happens */
  54. if (rc != -EPROBE_DEFER)
  55. dev_err(dev, "error getting gpio (%d)\n", rc);
  56. return rc;
  57. }
  58. gpio_dev->irq = gpiod_to_irq(gpio_dev->gpiod);
  59. if (gpio_dev->irq < 0)
  60. return gpio_dev->irq;
  61. rcdev = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
  62. if (!rcdev)
  63. return -ENOMEM;
  64. rcdev->priv = gpio_dev;
  65. rcdev->device_name = GPIO_IR_DEVICE_NAME;
  66. rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
  67. rcdev->input_id.bustype = BUS_HOST;
  68. rcdev->input_id.vendor = 0x0001;
  69. rcdev->input_id.product = 0x0001;
  70. rcdev->input_id.version = 0x0100;
  71. rcdev->dev.parent = dev;
  72. rcdev->driver_name = KBUILD_MODNAME;
  73. rcdev->min_timeout = 1;
  74. rcdev->timeout = IR_DEFAULT_TIMEOUT;
  75. rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  76. rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  77. rcdev->map_name = of_get_property(np, "linux,rc-map-name", NULL);
  78. if (!rcdev->map_name)
  79. rcdev->map_name = RC_MAP_EMPTY;
  80. gpio_dev->rcdev = rcdev;
  81. rc = devm_rc_register_device(dev, rcdev);
  82. if (rc < 0) {
  83. dev_err(dev, "failed to register rc device (%d)\n", rc);
  84. return rc;
  85. }
  86. platform_set_drvdata(pdev, gpio_dev);
  87. return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq,
  88. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  89. "gpio-ir-recv-irq", gpio_dev);
  90. }
  91. #ifdef CONFIG_PM
  92. static int gpio_ir_recv_suspend(struct device *dev)
  93. {
  94. struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
  95. if (device_may_wakeup(dev))
  96. enable_irq_wake(gpio_dev->irq);
  97. else
  98. disable_irq(gpio_dev->irq);
  99. return 0;
  100. }
  101. static int gpio_ir_recv_resume(struct device *dev)
  102. {
  103. struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
  104. if (device_may_wakeup(dev))
  105. disable_irq_wake(gpio_dev->irq);
  106. else
  107. enable_irq(gpio_dev->irq);
  108. return 0;
  109. }
  110. static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
  111. .suspend = gpio_ir_recv_suspend,
  112. .resume = gpio_ir_recv_resume,
  113. };
  114. #endif
  115. static const struct of_device_id gpio_ir_recv_of_match[] = {
  116. { .compatible = "gpio-ir-receiver", },
  117. { },
  118. };
  119. MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
  120. static struct platform_driver gpio_ir_recv_driver = {
  121. .probe = gpio_ir_recv_probe,
  122. .driver = {
  123. .name = KBUILD_MODNAME,
  124. .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
  125. #ifdef CONFIG_PM
  126. .pm = &gpio_ir_recv_pm_ops,
  127. #endif
  128. },
  129. };
  130. module_platform_driver(gpio_ir_recv_driver);
  131. MODULE_DESCRIPTION("GPIO IR Receiver driver");
  132. MODULE_LICENSE("GPL v2");