.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* 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 | 3 | */ |
---|
12 | 4 | |
---|
13 | 5 | #include <linux/kernel.h> |
---|
.. | .. |
---|
19 | 11 | #include <linux/of.h> |
---|
20 | 12 | #include <linux/of_gpio.h> |
---|
21 | 13 | #include <linux/platform_device.h> |
---|
| 14 | +#include <linux/pm_runtime.h> |
---|
| 15 | +#include <linux/pm_qos.h> |
---|
22 | 16 | #include <linux/irq.h> |
---|
23 | 17 | #include <media/rc-core.h> |
---|
24 | 18 | |
---|
.. | .. |
---|
28 | 22 | struct rc_dev *rcdev; |
---|
29 | 23 | struct gpio_desc *gpiod; |
---|
30 | 24 | int irq; |
---|
| 25 | + struct device *pmdev; |
---|
| 26 | + struct pm_qos_request qos; |
---|
31 | 27 | }; |
---|
32 | 28 | |
---|
33 | 29 | static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id) |
---|
34 | 30 | { |
---|
35 | 31 | int val; |
---|
36 | 32 | struct gpio_rc_dev *gpio_dev = dev_id; |
---|
| 33 | + struct device *pmdev = gpio_dev->pmdev; |
---|
| 34 | + |
---|
| 35 | + /* |
---|
| 36 | + * For some cpuidle systems, not all: |
---|
| 37 | + * Respond to interrupt taking more latency when cpu in idle. |
---|
| 38 | + * Invoke asynchronous pm runtime get from interrupt context, |
---|
| 39 | + * this may introduce a millisecond delay to call resume callback, |
---|
| 40 | + * where to disable cpuilde. |
---|
| 41 | + * |
---|
| 42 | + * Two issues lead to fail to decode first frame, one is latency to |
---|
| 43 | + * respond to interrupt, another is delay introduced by async api. |
---|
| 44 | + */ |
---|
| 45 | + if (pmdev) |
---|
| 46 | + pm_runtime_get(pmdev); |
---|
37 | 47 | |
---|
38 | 48 | val = gpiod_get_value(gpio_dev->gpiod); |
---|
39 | 49 | if (val >= 0) |
---|
40 | 50 | ir_raw_event_store_edge(gpio_dev->rcdev, val == 1); |
---|
| 51 | + |
---|
| 52 | + if (pmdev) { |
---|
| 53 | + pm_runtime_mark_last_busy(pmdev); |
---|
| 54 | + pm_runtime_put_autosuspend(pmdev); |
---|
| 55 | + } |
---|
41 | 56 | |
---|
42 | 57 | return IRQ_HANDLED; |
---|
43 | 58 | } |
---|
.. | .. |
---|
48 | 63 | struct device_node *np = dev->of_node; |
---|
49 | 64 | struct gpio_rc_dev *gpio_dev; |
---|
50 | 65 | struct rc_dev *rcdev; |
---|
| 66 | + u32 period = 0; |
---|
51 | 67 | int rc; |
---|
52 | 68 | |
---|
53 | 69 | if (!np) |
---|
.. | .. |
---|
91 | 107 | rcdev->map_name = RC_MAP_EMPTY; |
---|
92 | 108 | |
---|
93 | 109 | gpio_dev->rcdev = rcdev; |
---|
| 110 | + if (of_property_read_bool(np, "wakeup-source")) |
---|
| 111 | + device_init_wakeup(dev, true); |
---|
94 | 112 | |
---|
95 | 113 | rc = devm_rc_register_device(dev, rcdev); |
---|
96 | 114 | if (rc < 0) { |
---|
.. | .. |
---|
98 | 116 | return rc; |
---|
99 | 117 | } |
---|
100 | 118 | |
---|
| 119 | + of_property_read_u32(np, "linux,autosuspend-period", &period); |
---|
| 120 | + if (period) { |
---|
| 121 | + gpio_dev->pmdev = dev; |
---|
| 122 | + pm_runtime_set_autosuspend_delay(dev, period); |
---|
| 123 | + pm_runtime_use_autosuspend(dev); |
---|
| 124 | + pm_runtime_set_suspended(dev); |
---|
| 125 | + pm_runtime_enable(dev); |
---|
| 126 | + } |
---|
| 127 | + |
---|
101 | 128 | platform_set_drvdata(pdev, gpio_dev); |
---|
102 | 129 | |
---|
103 | 130 | return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq, |
---|
104 | 131 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
---|
105 | 132 | "gpio-ir-recv-irq", gpio_dev); |
---|
| 133 | +} |
---|
| 134 | + |
---|
| 135 | +static int gpio_ir_recv_remove(struct platform_device *pdev) |
---|
| 136 | +{ |
---|
| 137 | + struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); |
---|
| 138 | + struct device *pmdev = gpio_dev->pmdev; |
---|
| 139 | + |
---|
| 140 | + if (pmdev) { |
---|
| 141 | + pm_runtime_get_sync(pmdev); |
---|
| 142 | + cpu_latency_qos_remove_request(&gpio_dev->qos); |
---|
| 143 | + |
---|
| 144 | + pm_runtime_disable(pmdev); |
---|
| 145 | + pm_runtime_put_noidle(pmdev); |
---|
| 146 | + pm_runtime_set_suspended(pmdev); |
---|
| 147 | + } |
---|
| 148 | + |
---|
| 149 | + return 0; |
---|
106 | 150 | } |
---|
107 | 151 | |
---|
108 | 152 | #ifdef CONFIG_PM |
---|
.. | .. |
---|
130 | 174 | return 0; |
---|
131 | 175 | } |
---|
132 | 176 | |
---|
| 177 | +static int gpio_ir_recv_runtime_suspend(struct device *dev) |
---|
| 178 | +{ |
---|
| 179 | + struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev); |
---|
| 180 | + |
---|
| 181 | + cpu_latency_qos_remove_request(&gpio_dev->qos); |
---|
| 182 | + |
---|
| 183 | + return 0; |
---|
| 184 | +} |
---|
| 185 | + |
---|
| 186 | +static int gpio_ir_recv_runtime_resume(struct device *dev) |
---|
| 187 | +{ |
---|
| 188 | + struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev); |
---|
| 189 | + |
---|
| 190 | + cpu_latency_qos_add_request(&gpio_dev->qos, 0); |
---|
| 191 | + |
---|
| 192 | + return 0; |
---|
| 193 | +} |
---|
| 194 | + |
---|
133 | 195 | static const struct dev_pm_ops gpio_ir_recv_pm_ops = { |
---|
134 | 196 | .suspend = gpio_ir_recv_suspend, |
---|
135 | 197 | .resume = gpio_ir_recv_resume, |
---|
| 198 | + .runtime_suspend = gpio_ir_recv_runtime_suspend, |
---|
| 199 | + .runtime_resume = gpio_ir_recv_runtime_resume, |
---|
136 | 200 | }; |
---|
137 | 201 | #endif |
---|
138 | 202 | |
---|
.. | .. |
---|
144 | 208 | |
---|
145 | 209 | static struct platform_driver gpio_ir_recv_driver = { |
---|
146 | 210 | .probe = gpio_ir_recv_probe, |
---|
| 211 | + .remove = gpio_ir_recv_remove, |
---|
147 | 212 | .driver = { |
---|
148 | 213 | .name = KBUILD_MODNAME, |
---|
149 | 214 | .of_match_table = of_match_ptr(gpio_ir_recv_of_match), |
---|