David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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/of_gpio.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/irq.h> |
| 15 | #include <media/rc-core.h> |
| 16 | |
| 17 | #define GPIO_IR_DEVICE_NAME "gpio_ir_recv" |
| 18 | |
| 19 | struct gpio_rc_dev { |
| 20 | struct rc_dev *rcdev; |
| 21 | struct gpio_desc *gpiod; |
| 22 | int irq; |
| 23 | }; |
| 24 | |
| 25 | static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id) |
| 26 | { |
| 27 | int val; |
| 28 | struct gpio_rc_dev *gpio_dev = dev_id; |
| 29 | |
| 30 | val = gpiod_get_value(gpio_dev->gpiod); |
| 31 | if (val >= 0) |
| 32 | ir_raw_event_store_edge(gpio_dev->rcdev, val == 1); |
| 33 | |
| 34 | return IRQ_HANDLED; |
| 35 | } |
| 36 | |
| 37 | static int gpio_ir_recv_probe(struct platform_device *pdev) |
| 38 | { |
| 39 | struct device *dev = &pdev->dev; |
| 40 | struct device_node *np = dev->of_node; |
| 41 | struct gpio_rc_dev *gpio_dev; |
| 42 | struct rc_dev *rcdev; |
| 43 | int rc; |
| 44 | |
| 45 | if (!np) |
| 46 | return -ENODEV; |
| 47 | |
| 48 | gpio_dev = devm_kzalloc(dev, sizeof(*gpio_dev), GFP_KERNEL); |
| 49 | if (!gpio_dev) |
| 50 | return -ENOMEM; |
| 51 | |
| 52 | gpio_dev->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN); |
| 53 | if (IS_ERR(gpio_dev->gpiod)) { |
| 54 | rc = PTR_ERR(gpio_dev->gpiod); |
| 55 | /* Just try again if this happens */ |
| 56 | if (rc != -EPROBE_DEFER) |
| 57 | dev_err(dev, "error getting gpio (%d)\n", rc); |
| 58 | return rc; |
| 59 | } |
| 60 | gpio_dev->irq = gpiod_to_irq(gpio_dev->gpiod); |
| 61 | if (gpio_dev->irq < 0) |
| 62 | return gpio_dev->irq; |
| 63 | |
| 64 | rcdev = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW); |
| 65 | if (!rcdev) |
| 66 | return -ENOMEM; |
| 67 | |
| 68 | rcdev->priv = gpio_dev; |
| 69 | rcdev->device_name = GPIO_IR_DEVICE_NAME; |
| 70 | rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0"; |
| 71 | rcdev->input_id.bustype = BUS_HOST; |
| 72 | rcdev->input_id.vendor = 0x0001; |
| 73 | rcdev->input_id.product = 0x0001; |
| 74 | rcdev->input_id.version = 0x0100; |
| 75 | rcdev->dev.parent = dev; |
| 76 | rcdev->driver_name = KBUILD_MODNAME; |
| 77 | rcdev->min_timeout = 1; |
| 78 | rcdev->timeout = IR_DEFAULT_TIMEOUT; |
| 79 | rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT; |
| 80 | rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER; |
| 81 | rcdev->map_name = of_get_property(np, "linux,rc-map-name", NULL); |
| 82 | if (!rcdev->map_name) |
| 83 | rcdev->map_name = RC_MAP_EMPTY; |
| 84 | |
| 85 | gpio_dev->rcdev = rcdev; |
| 86 | |
| 87 | rc = devm_rc_register_device(dev, rcdev); |
| 88 | if (rc < 0) { |
| 89 | dev_err(dev, "failed to register rc device (%d)\n", rc); |
| 90 | return rc; |
| 91 | } |
| 92 | |
| 93 | platform_set_drvdata(pdev, gpio_dev); |
| 94 | |
| 95 | return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq, |
| 96 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, |
| 97 | "gpio-ir-recv-irq", gpio_dev); |
| 98 | } |
| 99 | |
| 100 | #ifdef CONFIG_PM |
| 101 | static int gpio_ir_recv_suspend(struct device *dev) |
| 102 | { |
| 103 | struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev); |
| 104 | |
| 105 | if (device_may_wakeup(dev)) |
| 106 | enable_irq_wake(gpio_dev->irq); |
| 107 | else |
| 108 | disable_irq(gpio_dev->irq); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int gpio_ir_recv_resume(struct device *dev) |
| 114 | { |
| 115 | struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev); |
| 116 | |
| 117 | if (device_may_wakeup(dev)) |
| 118 | disable_irq_wake(gpio_dev->irq); |
| 119 | else |
| 120 | enable_irq(gpio_dev->irq); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static const struct dev_pm_ops gpio_ir_recv_pm_ops = { |
| 126 | .suspend = gpio_ir_recv_suspend, |
| 127 | .resume = gpio_ir_recv_resume, |
| 128 | }; |
| 129 | #endif |
| 130 | |
| 131 | static const struct of_device_id gpio_ir_recv_of_match[] = { |
| 132 | { .compatible = "gpio-ir-receiver", }, |
| 133 | { }, |
| 134 | }; |
| 135 | MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match); |
| 136 | |
| 137 | static struct platform_driver gpio_ir_recv_driver = { |
| 138 | .probe = gpio_ir_recv_probe, |
| 139 | .driver = { |
| 140 | .name = KBUILD_MODNAME, |
| 141 | .of_match_table = of_match_ptr(gpio_ir_recv_of_match), |
| 142 | #ifdef CONFIG_PM |
| 143 | .pm = &gpio_ir_recv_pm_ops, |
| 144 | #endif |
| 145 | }, |
| 146 | }; |
| 147 | module_platform_driver(gpio_ir_recv_driver); |
| 148 | |
| 149 | MODULE_DESCRIPTION("GPIO IR Receiver driver"); |
| 150 | MODULE_LICENSE("GPL v2"); |