Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: rc: gpio-ir-recv: add QoS support for cpuidle system

GPIO IR receive is much rely on interrupt response, uneven interrupt
latency will lead to incorrect timing, so the decoder fails to decode
it. The issue is particularly acute on some systems which support
cpuidle, not all, dynamically disable and enable cpuidle can solve this
problem to a great extent.

However, there is a downside to this approach, the measurement of header
on the first frame may incorrect. Test on i.MX8M serials, when enable
cpuidle, interrupt latency could be about 500us.

With this patch:
1. has no side effect on non-cpuidle system, even runtime pm api won't
be invoked to avoid a bunch of pm busy work for devices that do not need
it, including spinlocks, ktime, etc.
2. latency is still much longer for the first gpio interrupt on cpuidle
system, so the first frame may not be decoded. Generally, RC would transmit
multiple frames at once press, we can sacrifice the first frame.
3. add "linux,autosuspend-period" property in device tree if you also
suffer this cpuidle issue.

Signed-off-by: Joakim Zhang <qiangqing.zhang@nxp.com>
Signed-off-by: Sean Young <sean@mess.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>

authored by

Joakim Zhang and committed by
Mauro Carvalho Chehab
ff1c9223 2d4ffef1

+53
+53
drivers/media/rc/gpio-ir-recv.c
··· 11 11 #include <linux/of.h> 12 12 #include <linux/of_gpio.h> 13 13 #include <linux/platform_device.h> 14 + #include <linux/pm_runtime.h> 15 + #include <linux/pm_qos.h> 14 16 #include <linux/irq.h> 15 17 #include <media/rc-core.h> 16 18 ··· 22 20 struct rc_dev *rcdev; 23 21 struct gpio_desc *gpiod; 24 22 int irq; 23 + struct device *pmdev; 24 + struct pm_qos_request qos; 25 25 }; 26 26 27 27 static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id) 28 28 { 29 29 int val; 30 30 struct gpio_rc_dev *gpio_dev = dev_id; 31 + struct device *pmdev = gpio_dev->pmdev; 32 + 33 + /* 34 + * For some cpuidle systems, not all: 35 + * Respond to interrupt taking more latency when cpu in idle. 36 + * Invoke asynchronous pm runtime get from interrupt context, 37 + * this may introduce a millisecond delay to call resume callback, 38 + * where to disable cpuilde. 39 + * 40 + * Two issues lead to fail to decode first frame, one is latency to 41 + * respond to interrupt, another is delay introduced by async api. 42 + */ 43 + if (pmdev) 44 + pm_runtime_get(pmdev); 31 45 32 46 val = gpiod_get_value(gpio_dev->gpiod); 33 47 if (val >= 0) 34 48 ir_raw_event_store_edge(gpio_dev->rcdev, val == 1); 49 + 50 + if (pmdev) { 51 + pm_runtime_mark_last_busy(pmdev); 52 + pm_runtime_put_autosuspend(pmdev); 53 + } 35 54 36 55 return IRQ_HANDLED; 37 56 } ··· 63 40 struct device_node *np = dev->of_node; 64 41 struct gpio_rc_dev *gpio_dev; 65 42 struct rc_dev *rcdev; 43 + u32 period = 0; 66 44 int rc; 67 45 68 46 if (!np) ··· 114 90 return rc; 115 91 } 116 92 93 + of_property_read_u32(np, "linux,autosuspend-period", &period); 94 + if (period) { 95 + gpio_dev->pmdev = dev; 96 + pm_runtime_set_autosuspend_delay(dev, period); 97 + pm_runtime_use_autosuspend(dev); 98 + pm_runtime_set_suspended(dev); 99 + pm_runtime_enable(dev); 100 + } 101 + 117 102 platform_set_drvdata(pdev, gpio_dev); 118 103 119 104 return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq, ··· 155 122 return 0; 156 123 } 157 124 125 + static int gpio_ir_recv_runtime_suspend(struct device *dev) 126 + { 127 + struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev); 128 + 129 + cpu_latency_qos_remove_request(&gpio_dev->qos); 130 + 131 + return 0; 132 + } 133 + 134 + static int gpio_ir_recv_runtime_resume(struct device *dev) 135 + { 136 + struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev); 137 + 138 + cpu_latency_qos_add_request(&gpio_dev->qos, 0); 139 + 140 + return 0; 141 + } 142 + 158 143 static const struct dev_pm_ops gpio_ir_recv_pm_ops = { 159 144 .suspend = gpio_ir_recv_suspend, 160 145 .resume = gpio_ir_recv_resume, 146 + .runtime_suspend = gpio_ir_recv_runtime_suspend, 147 + .runtime_resume = gpio_ir_recv_runtime_resume, 161 148 }; 162 149 #endif 163 150