Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
0003  */
0004 
0005 #include <linux/kernel.h>
0006 #include <linux/init.h>
0007 #include <linux/module.h>
0008 #include <linux/interrupt.h>
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/slab.h>
0011 #include <linux/of.h>
0012 #include <linux/of_gpio.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/pm_runtime.h>
0015 #include <linux/pm_qos.h>
0016 #include <linux/irq.h>
0017 #include <media/rc-core.h>
0018 
0019 #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
0020 
0021 struct gpio_rc_dev {
0022     struct rc_dev *rcdev;
0023     struct gpio_desc *gpiod;
0024     int irq;
0025     struct device *pmdev;
0026     struct pm_qos_request qos;
0027 };
0028 
0029 static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
0030 {
0031     int val;
0032     struct gpio_rc_dev *gpio_dev = dev_id;
0033     struct device *pmdev = gpio_dev->pmdev;
0034 
0035     /*
0036      * For some cpuidle systems, not all:
0037      * Respond to interrupt taking more latency when cpu in idle.
0038      * Invoke asynchronous pm runtime get from interrupt context,
0039      * this may introduce a millisecond delay to call resume callback,
0040      * where to disable cpuilde.
0041      *
0042      * Two issues lead to fail to decode first frame, one is latency to
0043      * respond to interrupt, another is delay introduced by async api.
0044      */
0045     if (pmdev)
0046         pm_runtime_get(pmdev);
0047 
0048     val = gpiod_get_value(gpio_dev->gpiod);
0049     if (val >= 0)
0050         ir_raw_event_store_edge(gpio_dev->rcdev, val == 1);
0051 
0052     if (pmdev) {
0053         pm_runtime_mark_last_busy(pmdev);
0054         pm_runtime_put_autosuspend(pmdev);
0055     }
0056 
0057     return IRQ_HANDLED;
0058 }
0059 
0060 static int gpio_ir_recv_probe(struct platform_device *pdev)
0061 {
0062     struct device *dev = &pdev->dev;
0063     struct device_node *np = dev->of_node;
0064     struct gpio_rc_dev *gpio_dev;
0065     struct rc_dev *rcdev;
0066     u32 period = 0;
0067     int rc;
0068 
0069     if (!np)
0070         return -ENODEV;
0071 
0072     gpio_dev = devm_kzalloc(dev, sizeof(*gpio_dev), GFP_KERNEL);
0073     if (!gpio_dev)
0074         return -ENOMEM;
0075 
0076     gpio_dev->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
0077     if (IS_ERR(gpio_dev->gpiod)) {
0078         rc = PTR_ERR(gpio_dev->gpiod);
0079         /* Just try again if this happens */
0080         if (rc != -EPROBE_DEFER)
0081             dev_err(dev, "error getting gpio (%d)\n", rc);
0082         return rc;
0083     }
0084     gpio_dev->irq = gpiod_to_irq(gpio_dev->gpiod);
0085     if (gpio_dev->irq < 0)
0086         return gpio_dev->irq;
0087 
0088     rcdev = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
0089     if (!rcdev)
0090         return -ENOMEM;
0091 
0092     rcdev->priv = gpio_dev;
0093     rcdev->device_name = GPIO_IR_DEVICE_NAME;
0094     rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
0095     rcdev->input_id.bustype = BUS_HOST;
0096     rcdev->input_id.vendor = 0x0001;
0097     rcdev->input_id.product = 0x0001;
0098     rcdev->input_id.version = 0x0100;
0099     rcdev->dev.parent = dev;
0100     rcdev->driver_name = KBUILD_MODNAME;
0101     rcdev->min_timeout = 1;
0102     rcdev->timeout = IR_DEFAULT_TIMEOUT;
0103     rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
0104     rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
0105     rcdev->map_name = of_get_property(np, "linux,rc-map-name", NULL);
0106     if (!rcdev->map_name)
0107         rcdev->map_name = RC_MAP_EMPTY;
0108 
0109     gpio_dev->rcdev = rcdev;
0110 
0111     rc = devm_rc_register_device(dev, rcdev);
0112     if (rc < 0) {
0113         dev_err(dev, "failed to register rc device (%d)\n", rc);
0114         return rc;
0115     }
0116 
0117     of_property_read_u32(np, "linux,autosuspend-period", &period);
0118     if (period) {
0119         gpio_dev->pmdev = dev;
0120         pm_runtime_set_autosuspend_delay(dev, period);
0121         pm_runtime_use_autosuspend(dev);
0122         pm_runtime_set_suspended(dev);
0123         pm_runtime_enable(dev);
0124     }
0125 
0126     platform_set_drvdata(pdev, gpio_dev);
0127 
0128     return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq,
0129                 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
0130                 "gpio-ir-recv-irq", gpio_dev);
0131 }
0132 
0133 #ifdef CONFIG_PM
0134 static int gpio_ir_recv_suspend(struct device *dev)
0135 {
0136     struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
0137 
0138     if (device_may_wakeup(dev))
0139         enable_irq_wake(gpio_dev->irq);
0140     else
0141         disable_irq(gpio_dev->irq);
0142 
0143     return 0;
0144 }
0145 
0146 static int gpio_ir_recv_resume(struct device *dev)
0147 {
0148     struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
0149 
0150     if (device_may_wakeup(dev))
0151         disable_irq_wake(gpio_dev->irq);
0152     else
0153         enable_irq(gpio_dev->irq);
0154 
0155     return 0;
0156 }
0157 
0158 static int gpio_ir_recv_runtime_suspend(struct device *dev)
0159 {
0160     struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
0161 
0162     cpu_latency_qos_remove_request(&gpio_dev->qos);
0163 
0164     return 0;
0165 }
0166 
0167 static int gpio_ir_recv_runtime_resume(struct device *dev)
0168 {
0169     struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
0170 
0171     cpu_latency_qos_add_request(&gpio_dev->qos, 0);
0172 
0173     return 0;
0174 }
0175 
0176 static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
0177     .suspend        = gpio_ir_recv_suspend,
0178     .resume         = gpio_ir_recv_resume,
0179     .runtime_suspend = gpio_ir_recv_runtime_suspend,
0180     .runtime_resume  = gpio_ir_recv_runtime_resume,
0181 };
0182 #endif
0183 
0184 static const struct of_device_id gpio_ir_recv_of_match[] = {
0185     { .compatible = "gpio-ir-receiver", },
0186     { },
0187 };
0188 MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
0189 
0190 static struct platform_driver gpio_ir_recv_driver = {
0191     .probe  = gpio_ir_recv_probe,
0192     .driver = {
0193         .name   = KBUILD_MODNAME,
0194         .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
0195 #ifdef CONFIG_PM
0196         .pm = &gpio_ir_recv_pm_ops,
0197 #endif
0198     },
0199 };
0200 module_platform_driver(gpio_ir_recv_driver);
0201 
0202 MODULE_DESCRIPTION("GPIO IR Receiver driver");
0203 MODULE_LICENSE("GPL v2");