blob: f50398b3ed5f49894fa31e68911b2aeac596fbb7 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
3 */
4
5#include <linux/kernel.h>
6#include <linux/init.h>
7#include <linux/module.h>
8#include <linux/interrupt.h>
9#include <linux/gpio/consumer.h>
10#include <linux/slab.h>
11#include <linux/of.h>
12#include <linux/of_gpio.h>
13#include <linux/platform_device.h>
14#include <linux/irq.h>
15#include <media/rc-core.h>
16
17#define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
18
19struct gpio_rc_dev {
20 struct rc_dev *rcdev;
21 struct gpio_desc *gpiod;
22 int irq;
23};
24
25static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
26{
27 int val;
28 struct gpio_rc_dev *gpio_dev = dev_id;
29
30 val = gpiod_get_value(gpio_dev->gpiod);
31 if (val >= 0)
32 ir_raw_event_store_edge(gpio_dev->rcdev, val == 1);
33
34 return IRQ_HANDLED;
35}
36
37static int gpio_ir_recv_probe(struct platform_device *pdev)
38{
39 struct device *dev = &pdev->dev;
40 struct device_node *np = dev->of_node;
41 struct gpio_rc_dev *gpio_dev;
42 struct rc_dev *rcdev;
43 int rc;
44
45 if (!np)
46 return -ENODEV;
47
48 gpio_dev = devm_kzalloc(dev, sizeof(*gpio_dev), GFP_KERNEL);
49 if (!gpio_dev)
50 return -ENOMEM;
51
52 gpio_dev->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
53 if (IS_ERR(gpio_dev->gpiod)) {
54 rc = PTR_ERR(gpio_dev->gpiod);
55 /* Just try again if this happens */
56 if (rc != -EPROBE_DEFER)
57 dev_err(dev, "error getting gpio (%d)\n", rc);
58 return rc;
59 }
60 gpio_dev->irq = gpiod_to_irq(gpio_dev->gpiod);
61 if (gpio_dev->irq < 0)
62 return gpio_dev->irq;
63
64 rcdev = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
65 if (!rcdev)
66 return -ENOMEM;
67
68 rcdev->priv = gpio_dev;
69 rcdev->device_name = GPIO_IR_DEVICE_NAME;
70 rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
71 rcdev->input_id.bustype = BUS_HOST;
72 rcdev->input_id.vendor = 0x0001;
73 rcdev->input_id.product = 0x0001;
74 rcdev->input_id.version = 0x0100;
75 rcdev->dev.parent = dev;
76 rcdev->driver_name = KBUILD_MODNAME;
77 rcdev->min_timeout = 1;
78 rcdev->timeout = IR_DEFAULT_TIMEOUT;
79 rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
80 rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
81 rcdev->map_name = of_get_property(np, "linux,rc-map-name", NULL);
82 if (!rcdev->map_name)
83 rcdev->map_name = RC_MAP_EMPTY;
84
85 gpio_dev->rcdev = rcdev;
86 if (of_property_read_bool(np, "wakeup-source"))
87 device_init_wakeup(dev, true);
88
89 rc = devm_rc_register_device(dev, rcdev);
90 if (rc < 0) {
91 dev_err(dev, "failed to register rc device (%d)\n", rc);
92 return rc;
93 }
94
95 platform_set_drvdata(pdev, gpio_dev);
96
97 return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq,
98 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
99 "gpio-ir-recv-irq", gpio_dev);
100}
101
102#ifdef CONFIG_PM
103static int gpio_ir_recv_suspend(struct device *dev)
104{
105 struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
106
107 if (device_may_wakeup(dev))
108 enable_irq_wake(gpio_dev->irq);
109 else
110 disable_irq(gpio_dev->irq);
111
112 return 0;
113}
114
115static int gpio_ir_recv_resume(struct device *dev)
116{
117 struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
118
119 if (device_may_wakeup(dev))
120 disable_irq_wake(gpio_dev->irq);
121 else
122 enable_irq(gpio_dev->irq);
123
124 return 0;
125}
126
127static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
128 .suspend = gpio_ir_recv_suspend,
129 .resume = gpio_ir_recv_resume,
130};
131#endif
132
133static const struct of_device_id gpio_ir_recv_of_match[] = {
134 { .compatible = "gpio-ir-receiver", },
135 { },
136};
137MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
138
139static struct platform_driver gpio_ir_recv_driver = {
140 .probe = gpio_ir_recv_probe,
141 .driver = {
142 .name = KBUILD_MODNAME,
143 .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
144#ifdef CONFIG_PM
145 .pm = &gpio_ir_recv_pm_ops,
146#endif
147 },
148};
149module_platform_driver(gpio_ir_recv_driver);
150
151MODULE_DESCRIPTION("GPIO IR Receiver driver");
152MODULE_LICENSE("GPL v2");