xref: /linux/drivers/media/rc/gpio-ir-recv.c (revision b4ada0618eed0fbd1b1630f73deb048c592b06a1)
1 // 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/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/pm_qos.h>
15 #include <linux/irq.h>
16 #include <media/rc-core.h>
17 
18 #define GPIO_IR_DEVICE_NAME	"gpio_ir_recv"
19 
20 struct gpio_rc_dev {
21 	struct rc_dev *rcdev;
22 	struct gpio_desc *gpiod;
23 	int irq;
24 	struct device *pmdev;
25 	struct pm_qos_request qos;
26 };
27 
28 static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
29 {
30 	int val;
31 	struct gpio_rc_dev *gpio_dev = dev_id;
32 	struct device *pmdev = gpio_dev->pmdev;
33 
34 	/*
35 	 * For some cpuidle systems, not all:
36 	 * Respond to interrupt taking more latency when cpu in idle.
37 	 * Invoke asynchronous pm runtime get from interrupt context,
38 	 * this may introduce a millisecond delay to call resume callback,
39 	 * where to disable cpuilde.
40 	 *
41 	 * Two issues lead to fail to decode first frame, one is latency to
42 	 * respond to interrupt, another is delay introduced by async api.
43 	 */
44 	if (pmdev)
45 		pm_runtime_get(pmdev);
46 
47 	val = gpiod_get_value(gpio_dev->gpiod);
48 	if (val >= 0)
49 		ir_raw_event_store_edge(gpio_dev->rcdev, val == 1);
50 
51 	if (pmdev)
52 		pm_runtime_put_autosuspend(pmdev);
53 
54 	return IRQ_HANDLED;
55 }
56 
57 static int gpio_ir_recv_probe(struct platform_device *pdev)
58 {
59 	struct device *dev = &pdev->dev;
60 	struct device_node *np = dev->of_node;
61 	struct gpio_rc_dev *gpio_dev;
62 	struct rc_dev *rcdev;
63 	u32 period = 0;
64 	int rc;
65 
66 	if (!np)
67 		return -ENODEV;
68 
69 	gpio_dev = devm_kzalloc(dev, sizeof(*gpio_dev), GFP_KERNEL);
70 	if (!gpio_dev)
71 		return -ENOMEM;
72 
73 	gpio_dev->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
74 	if (IS_ERR(gpio_dev->gpiod))
75 		return dev_err_probe(dev, PTR_ERR(gpio_dev->gpiod),
76 				     "error getting gpio\n");
77 	gpio_dev->irq = gpiod_to_irq(gpio_dev->gpiod);
78 	if (gpio_dev->irq < 0)
79 		return gpio_dev->irq;
80 
81 	rcdev = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
82 	if (!rcdev)
83 		return -ENOMEM;
84 
85 	rcdev->priv = gpio_dev;
86 	rcdev->device_name = GPIO_IR_DEVICE_NAME;
87 	rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
88 	rcdev->input_id.bustype = BUS_HOST;
89 	rcdev->input_id.vendor = 0x0001;
90 	rcdev->input_id.product = 0x0001;
91 	rcdev->input_id.version = 0x0100;
92 	rcdev->dev.parent = dev;
93 	rcdev->driver_name = KBUILD_MODNAME;
94 	rcdev->min_timeout = 1;
95 	rcdev->timeout = IR_DEFAULT_TIMEOUT;
96 	rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
97 	rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
98 	rcdev->map_name = of_get_property(np, "linux,rc-map-name", NULL);
99 	if (!rcdev->map_name)
100 		rcdev->map_name = RC_MAP_EMPTY;
101 
102 	gpio_dev->rcdev = rcdev;
103 	if (of_property_read_bool(np, "wakeup-source"))
104 		device_init_wakeup(dev, true);
105 
106 	rc = devm_rc_register_device(dev, rcdev);
107 	if (rc < 0) {
108 		dev_err(dev, "failed to register rc device (%d)\n", rc);
109 		return rc;
110 	}
111 
112 	of_property_read_u32(np, "linux,autosuspend-period", &period);
113 	if (period) {
114 		gpio_dev->pmdev = dev;
115 		pm_runtime_set_autosuspend_delay(dev, period);
116 		pm_runtime_use_autosuspend(dev);
117 		pm_runtime_set_suspended(dev);
118 		pm_runtime_enable(dev);
119 	}
120 
121 	platform_set_drvdata(pdev, gpio_dev);
122 
123 	return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq,
124 				IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
125 				"gpio-ir-recv-irq", gpio_dev);
126 }
127 
128 static void gpio_ir_recv_remove(struct platform_device *pdev)
129 {
130 	struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
131 	struct device *pmdev = gpio_dev->pmdev;
132 
133 	if (pmdev) {
134 		pm_runtime_get_sync(pmdev);
135 		cpu_latency_qos_remove_request(&gpio_dev->qos);
136 
137 		pm_runtime_disable(pmdev);
138 		pm_runtime_put_noidle(pmdev);
139 		pm_runtime_set_suspended(pmdev);
140 	}
141 }
142 
143 #ifdef CONFIG_PM
144 static int gpio_ir_recv_suspend(struct device *dev)
145 {
146 	struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
147 
148 	if (device_may_wakeup(dev))
149 		enable_irq_wake(gpio_dev->irq);
150 	else
151 		disable_irq(gpio_dev->irq);
152 
153 	return 0;
154 }
155 
156 static int gpio_ir_recv_resume(struct device *dev)
157 {
158 	struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
159 
160 	if (device_may_wakeup(dev))
161 		disable_irq_wake(gpio_dev->irq);
162 	else
163 		enable_irq(gpio_dev->irq);
164 
165 	return 0;
166 }
167 
168 static int gpio_ir_recv_runtime_suspend(struct device *dev)
169 {
170 	struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
171 
172 	cpu_latency_qos_remove_request(&gpio_dev->qos);
173 
174 	return 0;
175 }
176 
177 static int gpio_ir_recv_runtime_resume(struct device *dev)
178 {
179 	struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
180 
181 	cpu_latency_qos_add_request(&gpio_dev->qos, 0);
182 
183 	return 0;
184 }
185 
186 static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
187 	.suspend        = gpio_ir_recv_suspend,
188 	.resume         = gpio_ir_recv_resume,
189 	.runtime_suspend = gpio_ir_recv_runtime_suspend,
190 	.runtime_resume  = gpio_ir_recv_runtime_resume,
191 };
192 #endif
193 
194 static const struct of_device_id gpio_ir_recv_of_match[] = {
195 	{ .compatible = "gpio-ir-receiver", },
196 	{ },
197 };
198 MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
199 
200 static struct platform_driver gpio_ir_recv_driver = {
201 	.probe  = gpio_ir_recv_probe,
202 	.remove = gpio_ir_recv_remove,
203 	.driver = {
204 		.name   = KBUILD_MODNAME,
205 		.of_match_table = gpio_ir_recv_of_match,
206 #ifdef CONFIG_PM
207 		.pm	= &gpio_ir_recv_pm_ops,
208 #endif
209 	},
210 };
211 module_platform_driver(gpio_ir_recv_driver);
212 
213 MODULE_DESCRIPTION("GPIO IR Receiver driver");
214 MODULE_LICENSE("GPL v2");
215