Linux 3.12.39
[linux/fpc-iii.git] / drivers / media / rc / gpio-ir-recv.c
blob07aacfa5903dca6430bda96cbedd733b23d5a300
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>
19 #include <linux/of_gpio.h>
20 #include <linux/platform_device.h>
21 #include <linux/irq.h>
22 #include <media/rc-core.h>
23 #include <media/gpio-ir-recv.h>
25 #define GPIO_IR_DRIVER_NAME "gpio-rc-recv"
26 #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
28 struct gpio_rc_dev {
29 struct rc_dev *rcdev;
30 int gpio_nr;
31 bool active_low;
34 #ifdef CONFIG_OF
36 * Translate OpenFirmware node properties into platform_data
38 static int gpio_ir_recv_get_devtree_pdata(struct device *dev,
39 struct gpio_ir_recv_platform_data *pdata)
41 struct device_node *np = dev->of_node;
42 enum of_gpio_flags flags;
43 int gpio;
45 gpio = of_get_gpio_flags(np, 0, &flags);
46 if (gpio < 0) {
47 if (gpio != -EPROBE_DEFER)
48 dev_err(dev, "Failed to get gpio flags (%d)\n", gpio);
49 return gpio;
52 pdata->gpio_nr = gpio;
53 pdata->active_low = (flags & OF_GPIO_ACTIVE_LOW);
54 /* probe() takes care of map_name == NULL or allowed_protos == 0 */
55 pdata->map_name = of_get_property(np, "linux,rc-map-name", NULL);
56 pdata->allowed_protos = 0;
58 return 0;
61 static struct of_device_id gpio_ir_recv_of_match[] = {
62 { .compatible = "gpio-ir-receiver", },
63 { },
65 MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
67 #else /* !CONFIG_OF */
69 #define gpio_ir_recv_get_devtree_pdata(dev, pdata) (-ENOSYS)
71 #endif
73 static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
75 struct gpio_rc_dev *gpio_dev = dev_id;
76 int gval;
77 int rc = 0;
78 enum raw_event_type type = IR_SPACE;
80 gval = gpio_get_value_cansleep(gpio_dev->gpio_nr);
82 if (gval < 0)
83 goto err_get_value;
85 if (gpio_dev->active_low)
86 gval = !gval;
88 if (gval == 1)
89 type = IR_PULSE;
91 rc = ir_raw_event_store_edge(gpio_dev->rcdev, type);
92 if (rc < 0)
93 goto err_get_value;
95 ir_raw_event_handle(gpio_dev->rcdev);
97 err_get_value:
98 return IRQ_HANDLED;
101 static int gpio_ir_recv_probe(struct platform_device *pdev)
103 struct gpio_rc_dev *gpio_dev;
104 struct rc_dev *rcdev;
105 const struct gpio_ir_recv_platform_data *pdata =
106 pdev->dev.platform_data;
107 int rc;
109 if (pdev->dev.of_node) {
110 struct gpio_ir_recv_platform_data *dtpdata =
111 devm_kzalloc(&pdev->dev, sizeof(*dtpdata), GFP_KERNEL);
112 if (!dtpdata)
113 return -ENOMEM;
114 rc = gpio_ir_recv_get_devtree_pdata(&pdev->dev, dtpdata);
115 if (rc)
116 return rc;
117 pdata = dtpdata;
120 if (!pdata)
121 return -EINVAL;
123 if (pdata->gpio_nr < 0)
124 return -EINVAL;
126 gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL);
127 if (!gpio_dev)
128 return -ENOMEM;
130 rcdev = rc_allocate_device();
131 if (!rcdev) {
132 rc = -ENOMEM;
133 goto err_allocate_device;
136 rcdev->priv = gpio_dev;
137 rcdev->driver_type = RC_DRIVER_IR_RAW;
138 rcdev->input_name = GPIO_IR_DEVICE_NAME;
139 rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
140 rcdev->input_id.bustype = BUS_HOST;
141 rcdev->input_id.vendor = 0x0001;
142 rcdev->input_id.product = 0x0001;
143 rcdev->input_id.version = 0x0100;
144 rcdev->dev.parent = &pdev->dev;
145 rcdev->driver_name = GPIO_IR_DRIVER_NAME;
146 if (pdata->allowed_protos)
147 rcdev->allowed_protos = pdata->allowed_protos;
148 else
149 rcdev->allowed_protos = RC_BIT_ALL;
150 rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY;
152 gpio_dev->rcdev = rcdev;
153 gpio_dev->gpio_nr = pdata->gpio_nr;
154 gpio_dev->active_low = pdata->active_low;
156 rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
157 if (rc < 0)
158 goto err_gpio_request;
159 rc = gpio_direction_input(pdata->gpio_nr);
160 if (rc < 0)
161 goto err_gpio_direction_input;
163 rc = rc_register_device(rcdev);
164 if (rc < 0) {
165 dev_err(&pdev->dev, "failed to register rc device\n");
166 goto err_register_rc_device;
169 platform_set_drvdata(pdev, gpio_dev);
171 rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
172 gpio_ir_recv_irq,
173 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
174 "gpio-ir-recv-irq", gpio_dev);
175 if (rc < 0)
176 goto err_request_irq;
178 return 0;
180 err_request_irq:
181 rc_unregister_device(rcdev);
182 rcdev = NULL;
183 err_register_rc_device:
184 err_gpio_direction_input:
185 gpio_free(pdata->gpio_nr);
186 err_gpio_request:
187 rc_free_device(rcdev);
188 err_allocate_device:
189 kfree(gpio_dev);
190 return rc;
193 static int gpio_ir_recv_remove(struct platform_device *pdev)
195 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
197 free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
198 rc_unregister_device(gpio_dev->rcdev);
199 gpio_free(gpio_dev->gpio_nr);
200 kfree(gpio_dev);
201 return 0;
204 #ifdef CONFIG_PM
205 static int gpio_ir_recv_suspend(struct device *dev)
207 struct platform_device *pdev = to_platform_device(dev);
208 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
210 if (device_may_wakeup(dev))
211 enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
212 else
213 disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
215 return 0;
218 static int gpio_ir_recv_resume(struct device *dev)
220 struct platform_device *pdev = to_platform_device(dev);
221 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
223 if (device_may_wakeup(dev))
224 disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
225 else
226 enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
228 return 0;
231 static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
232 .suspend = gpio_ir_recv_suspend,
233 .resume = gpio_ir_recv_resume,
235 #endif
237 static struct platform_driver gpio_ir_recv_driver = {
238 .probe = gpio_ir_recv_probe,
239 .remove = gpio_ir_recv_remove,
240 .driver = {
241 .name = GPIO_IR_DRIVER_NAME,
242 .owner = THIS_MODULE,
243 .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
244 #ifdef CONFIG_PM
245 .pm = &gpio_ir_recv_pm_ops,
246 #endif
249 module_platform_driver(gpio_ir_recv_driver);
251 MODULE_DESCRIPTION("GPIO IR Receiver driver");
252 MODULE_LICENSE("GPL v2");