Commit | Line | Data |
---|---|---|
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> | |
2fd7f398 | 19 | #include <linux/of_gpio.h> |
fd0f6851 RK |
20 | #include <linux/platform_device.h> |
21 | #include <linux/irq.h> | |
22 | #include <media/rc-core.h> | |
23 | #include <media/gpio-ir-recv.h> | |
24 | ||
25 | #define GPIO_IR_DRIVER_NAME "gpio-rc-recv" | |
26 | #define GPIO_IR_DEVICE_NAME "gpio_ir_recv" | |
27 | ||
28 | struct gpio_rc_dev { | |
29 | struct rc_dev *rcdev; | |
955b4430 | 30 | int gpio_nr; |
fd0f6851 RK |
31 | bool active_low; |
32 | }; | |
33 | ||
2fd7f398 SH |
34 | #ifdef CONFIG_OF |
35 | /* | |
36 | * Translate OpenFirmware node properties into platform_data | |
37 | */ | |
38 | static int gpio_ir_recv_get_devtree_pdata(struct device *dev, | |
39 | struct gpio_ir_recv_platform_data *pdata) | |
40 | { | |
41 | struct device_node *np = dev->of_node; | |
42 | enum of_gpio_flags flags; | |
43 | int gpio; | |
44 | ||
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; | |
50 | } | |
51 | ||
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; | |
57 | ||
58 | return 0; | |
59 | } | |
60 | ||
61 | static struct of_device_id gpio_ir_recv_of_match[] = { | |
62 | { .compatible = "gpio-ir-receiver", }, | |
63 | { }, | |
64 | }; | |
65 | MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match); | |
66 | ||
67 | #else /* !CONFIG_OF */ | |
68 | ||
69 | #define gpio_ir_recv_get_devtree_pdata(dev, pdata) (-ENOSYS) | |
70 | ||
71 | #endif | |
72 | ||
fd0f6851 RK |
73 | static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id) |
74 | { | |
75 | struct gpio_rc_dev *gpio_dev = dev_id; | |
955b4430 | 76 | int gval; |
fd0f6851 RK |
77 | int rc = 0; |
78 | enum raw_event_type type = IR_SPACE; | |
79 | ||
80 | gval = gpio_get_value_cansleep(gpio_dev->gpio_nr); | |
81 | ||
82 | if (gval < 0) | |
83 | goto err_get_value; | |
84 | ||
85 | if (gpio_dev->active_low) | |
86 | gval = !gval; | |
87 | ||
88 | if (gval == 1) | |
89 | type = IR_PULSE; | |
90 | ||
91 | rc = ir_raw_event_store_edge(gpio_dev->rcdev, type); | |
92 | if (rc < 0) | |
93 | goto err_get_value; | |
94 | ||
95 | ir_raw_event_handle(gpio_dev->rcdev); | |
96 | ||
97 | err_get_value: | |
98 | return IRQ_HANDLED; | |
99 | } | |
100 | ||
4c62e976 | 101 | static int gpio_ir_recv_probe(struct platform_device *pdev) |
fd0f6851 RK |
102 | { |
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; | |
108 | ||
2fd7f398 SH |
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; | |
118 | } | |
119 | ||
fd0f6851 RK |
120 | if (!pdata) |
121 | return -EINVAL; | |
122 | ||
123 | if (pdata->gpio_nr < 0) | |
124 | return -EINVAL; | |
125 | ||
126 | gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL); | |
127 | if (!gpio_dev) | |
128 | return -ENOMEM; | |
129 | ||
130 | rcdev = rc_allocate_device(); | |
131 | if (!rcdev) { | |
132 | rc = -ENOMEM; | |
133 | goto err_allocate_device; | |
134 | } | |
135 | ||
975ef32e | 136 | rcdev->priv = gpio_dev; |
fd0f6851 | 137 | rcdev->driver_type = RC_DRIVER_IR_RAW; |
fd0f6851 | 138 | rcdev->input_name = GPIO_IR_DEVICE_NAME; |
975ef32e | 139 | rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0"; |
fd0f6851 | 140 | rcdev->input_id.bustype = BUS_HOST; |
975ef32e BT |
141 | rcdev->input_id.vendor = 0x0001; |
142 | rcdev->input_id.product = 0x0001; | |
143 | rcdev->input_id.version = 0x0100; | |
144 | rcdev->dev.parent = &pdev->dev; | |
fd0f6851 | 145 | rcdev->driver_name = GPIO_IR_DRIVER_NAME; |
500c3201 DC |
146 | if (pdata->allowed_protos) |
147 | rcdev->allowed_protos = pdata->allowed_protos; | |
148 | else | |
c003ab1b | 149 | rcdev->allowed_protos = RC_BIT_ALL; |
2bd237b8 | 150 | rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY; |
fd0f6851 RK |
151 | |
152 | gpio_dev->rcdev = rcdev; | |
153 | gpio_dev->gpio_nr = pdata->gpio_nr; | |
154 | gpio_dev->active_low = pdata->active_low; | |
155 | ||
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; | |
162 | ||
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; | |
167 | } | |
168 | ||
169 | platform_set_drvdata(pdev, gpio_dev); | |
170 | ||
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; | |
177 | ||
178 | return 0; | |
179 | ||
180 | err_request_irq: | |
181 | platform_set_drvdata(pdev, NULL); | |
182 | rc_unregister_device(rcdev); | |
e5d85b9a | 183 | rcdev = NULL; |
fd0f6851 RK |
184 | err_register_rc_device: |
185 | err_gpio_direction_input: | |
186 | gpio_free(pdata->gpio_nr); | |
187 | err_gpio_request: | |
188 | rc_free_device(rcdev); | |
fd0f6851 RK |
189 | err_allocate_device: |
190 | kfree(gpio_dev); | |
191 | return rc; | |
192 | } | |
193 | ||
4c62e976 | 194 | static int gpio_ir_recv_remove(struct platform_device *pdev) |
fd0f6851 RK |
195 | { |
196 | struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); | |
197 | ||
198 | free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev); | |
199 | platform_set_drvdata(pdev, NULL); | |
200 | rc_unregister_device(gpio_dev->rcdev); | |
201 | gpio_free(gpio_dev->gpio_nr); | |
fd0f6851 RK |
202 | kfree(gpio_dev); |
203 | return 0; | |
204 | } | |
205 | ||
206 | #ifdef CONFIG_PM | |
207 | static int gpio_ir_recv_suspend(struct device *dev) | |
208 | { | |
209 | struct platform_device *pdev = to_platform_device(dev); | |
210 | struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); | |
211 | ||
212 | if (device_may_wakeup(dev)) | |
213 | enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr)); | |
214 | else | |
215 | disable_irq(gpio_to_irq(gpio_dev->gpio_nr)); | |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
220 | static int gpio_ir_recv_resume(struct device *dev) | |
221 | { | |
222 | struct platform_device *pdev = to_platform_device(dev); | |
223 | struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); | |
224 | ||
225 | if (device_may_wakeup(dev)) | |
226 | disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr)); | |
227 | else | |
228 | enable_irq(gpio_to_irq(gpio_dev->gpio_nr)); | |
229 | ||
230 | return 0; | |
231 | } | |
232 | ||
233 | static const struct dev_pm_ops gpio_ir_recv_pm_ops = { | |
234 | .suspend = gpio_ir_recv_suspend, | |
235 | .resume = gpio_ir_recv_resume, | |
236 | }; | |
237 | #endif | |
238 | ||
239 | static struct platform_driver gpio_ir_recv_driver = { | |
240 | .probe = gpio_ir_recv_probe, | |
4c62e976 | 241 | .remove = gpio_ir_recv_remove, |
fd0f6851 RK |
242 | .driver = { |
243 | .name = GPIO_IR_DRIVER_NAME, | |
244 | .owner = THIS_MODULE, | |
2fd7f398 | 245 | .of_match_table = of_match_ptr(gpio_ir_recv_of_match), |
fd0f6851 RK |
246 | #ifdef CONFIG_PM |
247 | .pm = &gpio_ir_recv_pm_ops, | |
248 | #endif | |
249 | }, | |
250 | }; | |
81d75e9f | 251 | module_platform_driver(gpio_ir_recv_driver); |
fd0f6851 RK |
252 | |
253 | MODULE_DESCRIPTION("GPIO IR Receiver driver"); | |
254 | MODULE_LICENSE("GPL v2"); |