]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/rc/gpio-ir-recv.c
Merge tag 'media/v4.11-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / rc / gpio-ir-recv.c
CommitLineData
fd0f6851
RK
1/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
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.
6 *
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.
11 */
12
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>
8ab1aa87 19#include <linux/of.h>
2fd7f398 20#include <linux/of_gpio.h>
fd0f6851
RK
21#include <linux/platform_device.h>
22#include <linux/irq.h>
23#include <media/rc-core.h>
eb4b0ec7 24#include <linux/platform_data/media/gpio-ir-recv.h>
fd0f6851
RK
25
26#define GPIO_IR_DRIVER_NAME "gpio-rc-recv"
27#define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
28
29struct gpio_rc_dev {
30 struct rc_dev *rcdev;
955b4430 31 int gpio_nr;
fd0f6851 32 bool active_low;
3fb136f3 33 struct timer_list flush_timer;
fd0f6851
RK
34};
35
2fd7f398
SH
36#ifdef CONFIG_OF
37/*
38 * Translate OpenFirmware node properties into platform_data
39 */
40static int gpio_ir_recv_get_devtree_pdata(struct device *dev,
41 struct gpio_ir_recv_platform_data *pdata)
42{
43 struct device_node *np = dev->of_node;
44 enum of_gpio_flags flags;
45 int gpio;
46
47 gpio = of_get_gpio_flags(np, 0, &flags);
48 if (gpio < 0) {
49 if (gpio != -EPROBE_DEFER)
50 dev_err(dev, "Failed to get gpio flags (%d)\n", gpio);
51 return gpio;
52 }
53
54 pdata->gpio_nr = gpio;
55 pdata->active_low = (flags & OF_GPIO_ACTIVE_LOW);
56 /* probe() takes care of map_name == NULL or allowed_protos == 0 */
57 pdata->map_name = of_get_property(np, "linux,rc-map-name", NULL);
58 pdata->allowed_protos = 0;
59
60 return 0;
61}
62
7f099a75 63static const struct of_device_id gpio_ir_recv_of_match[] = {
2fd7f398
SH
64 { .compatible = "gpio-ir-receiver", },
65 { },
66};
67MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
68
69#else /* !CONFIG_OF */
70
71#define gpio_ir_recv_get_devtree_pdata(dev, pdata) (-ENOSYS)
72
73#endif
74
fd0f6851
RK
75static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
76{
77 struct gpio_rc_dev *gpio_dev = dev_id;
955b4430 78 int gval;
fd0f6851
RK
79 int rc = 0;
80 enum raw_event_type type = IR_SPACE;
81
64657066 82 gval = gpio_get_value(gpio_dev->gpio_nr);
fd0f6851
RK
83
84 if (gval < 0)
85 goto err_get_value;
86
87 if (gpio_dev->active_low)
88 gval = !gval;
89
90 if (gval == 1)
91 type = IR_PULSE;
92
93 rc = ir_raw_event_store_edge(gpio_dev->rcdev, type);
94 if (rc < 0)
95 goto err_get_value;
96
3fb136f3
EN
97 mod_timer(&gpio_dev->flush_timer,
98 jiffies + nsecs_to_jiffies(gpio_dev->rcdev->timeout));
99
fd0f6851
RK
100 ir_raw_event_handle(gpio_dev->rcdev);
101
102err_get_value:
103 return IRQ_HANDLED;
104}
105
3fb136f3
EN
106static void flush_timer(unsigned long arg)
107{
108 struct gpio_rc_dev *gpio_dev = (struct gpio_rc_dev *)arg;
109 DEFINE_IR_RAW_EVENT(ev);
110
111 ev.timeout = true;
112 ev.duration = gpio_dev->rcdev->timeout;
113 ir_raw_event_store(gpio_dev->rcdev, &ev);
114 ir_raw_event_handle(gpio_dev->rcdev);
115}
116
4c62e976 117static int gpio_ir_recv_probe(struct platform_device *pdev)
fd0f6851
RK
118{
119 struct gpio_rc_dev *gpio_dev;
120 struct rc_dev *rcdev;
121 const struct gpio_ir_recv_platform_data *pdata =
122 pdev->dev.platform_data;
123 int rc;
124
2fd7f398
SH
125 if (pdev->dev.of_node) {
126 struct gpio_ir_recv_platform_data *dtpdata =
127 devm_kzalloc(&pdev->dev, sizeof(*dtpdata), GFP_KERNEL);
128 if (!dtpdata)
129 return -ENOMEM;
130 rc = gpio_ir_recv_get_devtree_pdata(&pdev->dev, dtpdata);
131 if (rc)
132 return rc;
133 pdata = dtpdata;
134 }
135
fd0f6851
RK
136 if (!pdata)
137 return -EINVAL;
138
139 if (pdata->gpio_nr < 0)
140 return -EINVAL;
141
142 gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL);
143 if (!gpio_dev)
144 return -ENOMEM;
145
0f7499fd 146 rcdev = rc_allocate_device(RC_DRIVER_IR_RAW);
fd0f6851
RK
147 if (!rcdev) {
148 rc = -ENOMEM;
149 goto err_allocate_device;
150 }
151
975ef32e 152 rcdev->priv = gpio_dev;
fd0f6851 153 rcdev->input_name = GPIO_IR_DEVICE_NAME;
975ef32e 154 rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
fd0f6851 155 rcdev->input_id.bustype = BUS_HOST;
975ef32e
BT
156 rcdev->input_id.vendor = 0x0001;
157 rcdev->input_id.product = 0x0001;
158 rcdev->input_id.version = 0x0100;
159 rcdev->dev.parent = &pdev->dev;
fd0f6851 160 rcdev->driver_name = GPIO_IR_DRIVER_NAME;
3fb136f3
EN
161 rcdev->min_timeout = 0;
162 rcdev->timeout = IR_DEFAULT_TIMEOUT;
163 rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
500c3201 164 if (pdata->allowed_protos)
c5540fbb 165 rcdev->allowed_protocols = pdata->allowed_protos;
500c3201 166 else
8c34b5c4 167 rcdev->allowed_protocols = RC_BIT_ALL_IR_DECODER;
2bd237b8 168 rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY;
fd0f6851
RK
169
170 gpio_dev->rcdev = rcdev;
171 gpio_dev->gpio_nr = pdata->gpio_nr;
172 gpio_dev->active_low = pdata->active_low;
173
3fb136f3
EN
174 setup_timer(&gpio_dev->flush_timer, flush_timer,
175 (unsigned long)gpio_dev);
176
fd0f6851
RK
177 rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
178 if (rc < 0)
179 goto err_gpio_request;
180 rc = gpio_direction_input(pdata->gpio_nr);
181 if (rc < 0)
182 goto err_gpio_direction_input;
183
184 rc = rc_register_device(rcdev);
185 if (rc < 0) {
186 dev_err(&pdev->dev, "failed to register rc device\n");
187 goto err_register_rc_device;
188 }
189
190 platform_set_drvdata(pdev, gpio_dev);
191
192 rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
193 gpio_ir_recv_irq,
194 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
195 "gpio-ir-recv-irq", gpio_dev);
196 if (rc < 0)
197 goto err_request_irq;
198
199 return 0;
200
201err_request_irq:
fd0f6851 202 rc_unregister_device(rcdev);
e5d85b9a 203 rcdev = NULL;
fd0f6851
RK
204err_register_rc_device:
205err_gpio_direction_input:
206 gpio_free(pdata->gpio_nr);
207err_gpio_request:
208 rc_free_device(rcdev);
fd0f6851
RK
209err_allocate_device:
210 kfree(gpio_dev);
211 return rc;
212}
213
4c62e976 214static int gpio_ir_recv_remove(struct platform_device *pdev)
fd0f6851
RK
215{
216 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
217
218 free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
3fb136f3 219 del_timer_sync(&gpio_dev->flush_timer);
fd0f6851
RK
220 rc_unregister_device(gpio_dev->rcdev);
221 gpio_free(gpio_dev->gpio_nr);
fd0f6851
RK
222 kfree(gpio_dev);
223 return 0;
224}
225
226#ifdef CONFIG_PM
227static int gpio_ir_recv_suspend(struct device *dev)
228{
229 struct platform_device *pdev = to_platform_device(dev);
230 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
231
232 if (device_may_wakeup(dev))
233 enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
234 else
235 disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
236
237 return 0;
238}
239
240static int gpio_ir_recv_resume(struct device *dev)
241{
242 struct platform_device *pdev = to_platform_device(dev);
243 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
244
245 if (device_may_wakeup(dev))
246 disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
247 else
248 enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
249
250 return 0;
251}
252
253static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
254 .suspend = gpio_ir_recv_suspend,
255 .resume = gpio_ir_recv_resume,
256};
257#endif
258
259static struct platform_driver gpio_ir_recv_driver = {
260 .probe = gpio_ir_recv_probe,
4c62e976 261 .remove = gpio_ir_recv_remove,
fd0f6851
RK
262 .driver = {
263 .name = GPIO_IR_DRIVER_NAME,
2fd7f398 264 .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
fd0f6851
RK
265#ifdef CONFIG_PM
266 .pm = &gpio_ir_recv_pm_ops,
267#endif
268 },
269};
81d75e9f 270module_platform_driver(gpio_ir_recv_driver);
fd0f6851
RK
271
272MODULE_DESCRIPTION("GPIO IR Receiver driver");
273MODULE_LICENSE("GPL v2");