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.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>
24 #include <linux/platform_data/media/gpio-ir-recv.h>
26 #define GPIO_IR_DRIVER_NAME "gpio-rc-recv"
27 #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
37 * Translate OpenFirmware node properties into platform_data
39 static int gpio_ir_recv_get_devtree_pdata(struct device *dev,
40 struct gpio_ir_recv_platform_data *pdata)
42 struct device_node *np = dev->of_node;
43 enum of_gpio_flags flags;
46 gpio = of_get_gpio_flags(np, 0, &flags);
48 if (gpio != -EPROBE_DEFER)
49 dev_err(dev, "Failed to get gpio flags (%d)\n", gpio);
53 pdata->gpio_nr = gpio;
54 pdata->active_low = (flags & OF_GPIO_ACTIVE_LOW);
55 /* probe() takes care of map_name == NULL or allowed_protos == 0 */
56 pdata->map_name = of_get_property(np, "linux,rc-map-name", NULL);
57 pdata->allowed_protos = 0;
62 static const struct of_device_id gpio_ir_recv_of_match[] = {
63 { .compatible = "gpio-ir-receiver", },
66 MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
68 #else /* !CONFIG_OF */
70 #define gpio_ir_recv_get_devtree_pdata(dev, pdata) (-ENOSYS)
74 static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
76 struct gpio_rc_dev *gpio_dev = dev_id;
80 gval = gpio_get_value(gpio_dev->gpio_nr);
85 if (gpio_dev->active_low)
88 rc = ir_raw_event_store_edge(gpio_dev->rcdev, gval == 1);
96 static int gpio_ir_recv_probe(struct platform_device *pdev)
98 struct gpio_rc_dev *gpio_dev;
100 const struct gpio_ir_recv_platform_data *pdata =
101 pdev->dev.platform_data;
104 if (pdev->dev.of_node) {
105 struct gpio_ir_recv_platform_data *dtpdata =
106 devm_kzalloc(&pdev->dev, sizeof(*dtpdata), GFP_KERNEL);
109 rc = gpio_ir_recv_get_devtree_pdata(&pdev->dev, dtpdata);
118 if (pdata->gpio_nr < 0)
121 gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL);
125 rcdev = rc_allocate_device(RC_DRIVER_IR_RAW);
128 goto err_allocate_device;
131 rcdev->priv = gpio_dev;
132 rcdev->device_name = GPIO_IR_DEVICE_NAME;
133 rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
134 rcdev->input_id.bustype = BUS_HOST;
135 rcdev->input_id.vendor = 0x0001;
136 rcdev->input_id.product = 0x0001;
137 rcdev->input_id.version = 0x0100;
138 rcdev->dev.parent = &pdev->dev;
139 rcdev->driver_name = GPIO_IR_DRIVER_NAME;
140 rcdev->min_timeout = 1;
141 rcdev->timeout = IR_DEFAULT_TIMEOUT;
142 rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
143 if (pdata->allowed_protos)
144 rcdev->allowed_protocols = pdata->allowed_protos;
146 rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
147 rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY;
149 gpio_dev->rcdev = rcdev;
150 gpio_dev->gpio_nr = pdata->gpio_nr;
151 gpio_dev->active_low = pdata->active_low;
153 rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
155 goto err_gpio_request;
156 rc = gpio_direction_input(pdata->gpio_nr);
158 goto err_gpio_direction_input;
160 rc = rc_register_device(rcdev);
162 dev_err(&pdev->dev, "failed to register rc device\n");
163 goto err_register_rc_device;
166 platform_set_drvdata(pdev, gpio_dev);
168 rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
170 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
171 "gpio-ir-recv-irq", gpio_dev);
173 goto err_request_irq;
178 rc_unregister_device(rcdev);
180 err_register_rc_device:
181 err_gpio_direction_input:
182 gpio_free(pdata->gpio_nr);
184 rc_free_device(rcdev);
190 static int gpio_ir_recv_remove(struct platform_device *pdev)
192 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
194 free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
195 rc_unregister_device(gpio_dev->rcdev);
196 gpio_free(gpio_dev->gpio_nr);
202 static int gpio_ir_recv_suspend(struct device *dev)
204 struct platform_device *pdev = to_platform_device(dev);
205 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
207 if (device_may_wakeup(dev))
208 enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
210 disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
215 static int gpio_ir_recv_resume(struct device *dev)
217 struct platform_device *pdev = to_platform_device(dev);
218 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
220 if (device_may_wakeup(dev))
221 disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
223 enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
228 static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
229 .suspend = gpio_ir_recv_suspend,
230 .resume = gpio_ir_recv_resume,
234 static struct platform_driver gpio_ir_recv_driver = {
235 .probe = gpio_ir_recv_probe,
236 .remove = gpio_ir_recv_remove,
238 .name = GPIO_IR_DRIVER_NAME,
239 .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
241 .pm = &gpio_ir_recv_pm_ops,
245 module_platform_driver(gpio_ir_recv_driver);
247 MODULE_DESCRIPTION("GPIO IR Receiver driver");
248 MODULE_LICENSE("GPL v2");