blob: 3416c9183b537e89830f8fb87d4aad00cd5d0e50 [file] [log] [blame]
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +05301/* 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
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030024#define GPIO_IR_DRIVER_NAME "gpio-rc-recv"
25#define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053026
27struct gpio_rc_dev {
28 struct rc_dev *rcdev;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053029 unsigned int gpio_nr;
30 bool active_low;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053031};
32
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030033static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053034{
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030035 struct gpio_rc_dev *gpio_dev = dev_id;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053036 unsigned int gval;
37 int rc = 0;
38 enum raw_event_type type = IR_SPACE;
39
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053040 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
57err_get_value:
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053058 return IRQ_HANDLED;
59}
60
61static 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;
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030067 int rc;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053068
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
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053079 rcdev = rc_allocate_device();
80 if (!rcdev) {
81 rc = -ENOMEM;
82 goto err_allocate_device;
83 }
84
85 rcdev->driver_type = RC_DRIVER_IR_RAW;
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030086 rcdev->allowed_protos = RC_TYPE_ALL;
87 rcdev->input_name = GPIO_IR_DEVICE_NAME;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053088 rcdev->input_id.bustype = BUS_HOST;
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030089 rcdev->driver_name = GPIO_IR_DRIVER_NAME;
Ravi Kumar V9d229f82012-05-10 12:38:05 +053090 rcdev->map_name = RC_MAP_SAMSUNG_NECX;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053091
92 gpio_dev->rcdev = rcdev;
93 gpio_dev->gpio_nr = pdata->gpio_nr;
94 gpio_dev->active_low = pdata->active_low;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053095
96 rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
97 if (rc < 0)
98 goto err_gpio_request;
99 rc = gpio_direction_input(pdata->gpio_nr);
100 if (rc < 0)
101 goto err_gpio_direction_input;
102
103 rc = rc_register_device(rcdev);
104 if (rc < 0) {
105 dev_err(&pdev->dev, "failed to register rc device\n");
106 goto err_register_rc_device;
107 }
108
109 platform_set_drvdata(pdev, gpio_dev);
110
Ravi Kumar Ve25bde42012-02-28 01:51:40 -0300111 rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
112 gpio_ir_recv_irq,
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530113 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
Ravi Kumar Ve25bde42012-02-28 01:51:40 -0300114 "gpio-ir-recv-irq", gpio_dev);
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530115 if (rc < 0)
116 goto err_request_irq;
117
Ravi Kumar Vc23ebe42012-05-07 16:00:37 +0530118 device_init_wakeup(&pdev->dev, pdata->can_wakeup);
Trilok Sonicc1f41d2012-04-19 22:24:17 +0530119
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530120 return 0;
121
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530122err_request_irq:
123 platform_set_drvdata(pdev, NULL);
124 rc_unregister_device(rcdev);
125err_register_rc_device:
126err_gpio_direction_input:
127 gpio_free(pdata->gpio_nr);
128err_gpio_request:
129 rc_free_device(rcdev);
130 rcdev = NULL;
131err_allocate_device:
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530132 kfree(gpio_dev);
133 return rc;
134}
135
136static int __devexit gpio_ir_recv_remove(struct platform_device *pdev)
137{
138 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
139
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530140 free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
141 platform_set_drvdata(pdev, NULL);
142 rc_unregister_device(gpio_dev->rcdev);
143 gpio_free(gpio_dev->gpio_nr);
144 rc_free_device(gpio_dev->rcdev);
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530145 kfree(gpio_dev);
146 return 0;
147}
148
Ravi Kumar Ve25bde42012-02-28 01:51:40 -0300149#ifdef CONFIG_PM
150static int gpio_ir_recv_suspend(struct device *dev)
151{
152 struct platform_device *pdev = to_platform_device(dev);
153 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
154
155 if (device_may_wakeup(dev))
156 enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
157 else
158 disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
159
160 return 0;
161}
162
163static int gpio_ir_recv_resume(struct device *dev)
164{
165 struct platform_device *pdev = to_platform_device(dev);
166 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
167
168 if (device_may_wakeup(dev))
169 disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
170 else
171 enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
172
173 return 0;
174}
175
176static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
177 .suspend = gpio_ir_recv_suspend,
178 .resume = gpio_ir_recv_resume,
179};
180#endif
181
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530182static struct platform_driver gpio_ir_recv_driver = {
183 .probe = gpio_ir_recv_probe,
184 .remove = __devexit_p(gpio_ir_recv_remove),
185 .driver = {
Ravi Kumar Ve25bde42012-02-28 01:51:40 -0300186 .name = GPIO_IR_DRIVER_NAME,
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530187 .owner = THIS_MODULE,
Ravi Kumar Ve25bde42012-02-28 01:51:40 -0300188#ifdef CONFIG_PM
189 .pm = &gpio_ir_recv_pm_ops,
190#endif
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530191 },
192};
193
194static int __init gpio_ir_recv_init(void)
195{
196 return platform_driver_register(&gpio_ir_recv_driver);
197}
198module_init(gpio_ir_recv_init);
199
200static void __exit gpio_ir_recv_exit(void)
201{
202 platform_driver_unregister(&gpio_ir_recv_driver);
203}
204module_exit(gpio_ir_recv_exit);
205
206MODULE_DESCRIPTION("GPIO IR Receiver driver");
207MODULE_LICENSE("GPL v2");