Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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