]>
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> | |
19 | #include <linux/platform_device.h> | |
20 | #include <linux/irq.h> | |
21 | #include <media/rc-core.h> | |
22 | #include <media/gpio-ir-recv.h> | |
23 | ||
24 | #define GPIO_IR_DRIVER_NAME "gpio-rc-recv" | |
25 | #define GPIO_IR_DEVICE_NAME "gpio_ir_recv" | |
26 | ||
27 | struct gpio_rc_dev { | |
28 | struct rc_dev *rcdev; | |
955b4430 | 29 | int gpio_nr; |
fd0f6851 RK |
30 | bool active_low; |
31 | }; | |
32 | ||
33 | static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id) | |
34 | { | |
35 | struct gpio_rc_dev *gpio_dev = dev_id; | |
955b4430 | 36 | int gval; |
fd0f6851 RK |
37 | int rc = 0; |
38 | enum raw_event_type type = IR_SPACE; | |
39 | ||
40 | gval = gpio_get_value_cansleep(gpio_dev->gpio_nr); | |
41 | ||
42 | if (gval < 0) | |
43 | goto err_get_value; | |
44 | ||
45 | if (gpio_dev->active_low) | |
46 | gval = !gval; | |
47 | ||
48 | if (gval == 1) | |
49 | type = IR_PULSE; | |
50 | ||
51 | rc = ir_raw_event_store_edge(gpio_dev->rcdev, type); | |
52 | if (rc < 0) | |
53 | goto err_get_value; | |
54 | ||
55 | ir_raw_event_handle(gpio_dev->rcdev); | |
56 | ||
57 | err_get_value: | |
58 | return IRQ_HANDLED; | |
59 | } | |
60 | ||
61 | static int __devinit gpio_ir_recv_probe(struct platform_device *pdev) | |
62 | { | |
63 | struct gpio_rc_dev *gpio_dev; | |
64 | struct rc_dev *rcdev; | |
65 | const struct gpio_ir_recv_platform_data *pdata = | |
66 | pdev->dev.platform_data; | |
67 | int rc; | |
68 | ||
69 | if (!pdata) | |
70 | return -EINVAL; | |
71 | ||
72 | if (pdata->gpio_nr < 0) | |
73 | return -EINVAL; | |
74 | ||
75 | gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL); | |
76 | if (!gpio_dev) | |
77 | return -ENOMEM; | |
78 | ||
79 | rcdev = rc_allocate_device(); | |
80 | if (!rcdev) { | |
81 | rc = -ENOMEM; | |
82 | goto err_allocate_device; | |
83 | } | |
84 | ||
975ef32e | 85 | rcdev->priv = gpio_dev; |
fd0f6851 RK |
86 | rcdev->driver_type = RC_DRIVER_IR_RAW; |
87 | rcdev->allowed_protos = RC_TYPE_ALL; | |
88 | rcdev->input_name = GPIO_IR_DEVICE_NAME; | |
975ef32e | 89 | rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0"; |
fd0f6851 | 90 | rcdev->input_id.bustype = BUS_HOST; |
975ef32e BT |
91 | rcdev->input_id.vendor = 0x0001; |
92 | rcdev->input_id.product = 0x0001; | |
93 | rcdev->input_id.version = 0x0100; | |
94 | rcdev->dev.parent = &pdev->dev; | |
fd0f6851 | 95 | rcdev->driver_name = GPIO_IR_DRIVER_NAME; |
2bd237b8 | 96 | rcdev->map_name = pdata->map_name ?: RC_MAP_EMPTY; |
fd0f6851 RK |
97 | |
98 | gpio_dev->rcdev = rcdev; | |
99 | gpio_dev->gpio_nr = pdata->gpio_nr; | |
100 | gpio_dev->active_low = pdata->active_low; | |
101 | ||
102 | rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv"); | |
103 | if (rc < 0) | |
104 | goto err_gpio_request; | |
105 | rc = gpio_direction_input(pdata->gpio_nr); | |
106 | if (rc < 0) | |
107 | goto err_gpio_direction_input; | |
108 | ||
109 | rc = rc_register_device(rcdev); | |
110 | if (rc < 0) { | |
111 | dev_err(&pdev->dev, "failed to register rc device\n"); | |
112 | goto err_register_rc_device; | |
113 | } | |
114 | ||
115 | platform_set_drvdata(pdev, gpio_dev); | |
116 | ||
117 | rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr), | |
118 | gpio_ir_recv_irq, | |
119 | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, | |
120 | "gpio-ir-recv-irq", gpio_dev); | |
121 | if (rc < 0) | |
122 | goto err_request_irq; | |
123 | ||
124 | return 0; | |
125 | ||
126 | err_request_irq: | |
127 | platform_set_drvdata(pdev, NULL); | |
128 | rc_unregister_device(rcdev); | |
129 | err_register_rc_device: | |
130 | err_gpio_direction_input: | |
131 | gpio_free(pdata->gpio_nr); | |
132 | err_gpio_request: | |
133 | rc_free_device(rcdev); | |
134 | rcdev = NULL; | |
135 | err_allocate_device: | |
136 | kfree(gpio_dev); | |
137 | return rc; | |
138 | } | |
139 | ||
140 | static int __devexit gpio_ir_recv_remove(struct platform_device *pdev) | |
141 | { | |
142 | struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); | |
143 | ||
144 | free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev); | |
145 | platform_set_drvdata(pdev, NULL); | |
146 | rc_unregister_device(gpio_dev->rcdev); | |
147 | gpio_free(gpio_dev->gpio_nr); | |
148 | rc_free_device(gpio_dev->rcdev); | |
149 | kfree(gpio_dev); | |
150 | return 0; | |
151 | } | |
152 | ||
153 | #ifdef CONFIG_PM | |
154 | static int gpio_ir_recv_suspend(struct device *dev) | |
155 | { | |
156 | struct platform_device *pdev = to_platform_device(dev); | |
157 | struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); | |
158 | ||
159 | if (device_may_wakeup(dev)) | |
160 | enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr)); | |
161 | else | |
162 | disable_irq(gpio_to_irq(gpio_dev->gpio_nr)); | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
167 | static int gpio_ir_recv_resume(struct device *dev) | |
168 | { | |
169 | struct platform_device *pdev = to_platform_device(dev); | |
170 | struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev); | |
171 | ||
172 | if (device_may_wakeup(dev)) | |
173 | disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr)); | |
174 | else | |
175 | enable_irq(gpio_to_irq(gpio_dev->gpio_nr)); | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
180 | static const struct dev_pm_ops gpio_ir_recv_pm_ops = { | |
181 | .suspend = gpio_ir_recv_suspend, | |
182 | .resume = gpio_ir_recv_resume, | |
183 | }; | |
184 | #endif | |
185 | ||
186 | static struct platform_driver gpio_ir_recv_driver = { | |
187 | .probe = gpio_ir_recv_probe, | |
188 | .remove = __devexit_p(gpio_ir_recv_remove), | |
189 | .driver = { | |
190 | .name = GPIO_IR_DRIVER_NAME, | |
191 | .owner = THIS_MODULE, | |
192 | #ifdef CONFIG_PM | |
193 | .pm = &gpio_ir_recv_pm_ops, | |
194 | #endif | |
195 | }, | |
196 | }; | |
81d75e9f | 197 | module_platform_driver(gpio_ir_recv_driver); |
fd0f6851 RK |
198 | |
199 | MODULE_DESCRIPTION("GPIO IR Receiver driver"); | |
200 | MODULE_LICENSE("GPL v2"); |