1 /* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
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.
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.
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/slab.h>
20 #include <linux/of_gpio.h>
21 #include <linux/platform_device.h>
22 #include <linux/irq.h>
23 #include <media/rc-core.h>
25 #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
29 struct gpio_desc *gpiod;
33 static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
36 struct gpio_rc_dev *gpio_dev = dev_id;
38 val = gpiod_get_value(gpio_dev->gpiod);
40 ir_raw_event_store_edge(gpio_dev->rcdev, val == 1);
45 static int gpio_ir_recv_probe(struct platform_device *pdev)
47 struct device *dev = &pdev->dev;
48 struct device_node *np = dev->of_node;
49 struct gpio_rc_dev *gpio_dev;
56 gpio_dev = devm_kzalloc(dev, sizeof(*gpio_dev), GFP_KERNEL);
60 gpio_dev->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
61 if (IS_ERR(gpio_dev->gpiod)) {
62 rc = PTR_ERR(gpio_dev->gpiod);
63 /* Just try again if this happens */
64 if (rc != -EPROBE_DEFER)
65 dev_err(dev, "error getting gpio (%d)\n", rc);
68 gpio_dev->irq = gpiod_to_irq(gpio_dev->gpiod);
69 if (gpio_dev->irq < 0)
72 rcdev = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
76 rcdev->priv = gpio_dev;
77 rcdev->device_name = GPIO_IR_DEVICE_NAME;
78 rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
79 rcdev->input_id.bustype = BUS_HOST;
80 rcdev->input_id.vendor = 0x0001;
81 rcdev->input_id.product = 0x0001;
82 rcdev->input_id.version = 0x0100;
83 rcdev->dev.parent = dev;
84 rcdev->driver_name = KBUILD_MODNAME;
85 rcdev->min_timeout = 1;
86 rcdev->timeout = IR_DEFAULT_TIMEOUT;
87 rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
88 rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
89 rcdev->map_name = of_get_property(np, "linux,rc-map-name", NULL);
91 rcdev->map_name = RC_MAP_EMPTY;
93 gpio_dev->rcdev = rcdev;
94 if (of_property_read_bool(np, "wakeup-source"))
95 device_init_wakeup(dev, true);
97 rc = devm_rc_register_device(dev, rcdev);
99 dev_err(dev, "failed to register rc device (%d)\n", rc);
103 platform_set_drvdata(pdev, gpio_dev);
105 return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq,
106 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
107 "gpio-ir-recv-irq", gpio_dev);
111 static int gpio_ir_recv_suspend(struct device *dev)
113 struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
115 if (device_may_wakeup(dev))
116 enable_irq_wake(gpio_dev->irq);
118 disable_irq(gpio_dev->irq);
123 static int gpio_ir_recv_resume(struct device *dev)
125 struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
127 if (device_may_wakeup(dev))
128 disable_irq_wake(gpio_dev->irq);
130 enable_irq(gpio_dev->irq);
135 static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
136 .suspend = gpio_ir_recv_suspend,
137 .resume = gpio_ir_recv_resume,
141 static const struct of_device_id gpio_ir_recv_of_match[] = {
142 { .compatible = "gpio-ir-receiver", },
145 MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
147 static struct platform_driver gpio_ir_recv_driver = {
148 .probe = gpio_ir_recv_probe,
150 .name = KBUILD_MODNAME,
151 .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
153 .pm = &gpio_ir_recv_pm_ops,
157 module_platform_driver(gpio_ir_recv_driver);
159 MODULE_DESCRIPTION("GPIO IR Receiver driver");
160 MODULE_LICENSE("GPL v2");