blob: d7dc67de7c87c53887969960cdf94ff967e0c14f [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;
Ravi Kumar V507b9622012-06-15 17:25:44 +053031 int can_sleep;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053032};
33
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030034static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053035{
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030036 struct gpio_rc_dev *gpio_dev = dev_id;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053037 unsigned int gval;
38 int rc = 0;
39 enum raw_event_type type = IR_SPACE;
40
Ravi Kumar V507b9622012-06-15 17:25:44 +053041 if (gpio_dev->can_sleep)
42 gval = gpio_get_value_cansleep(gpio_dev->gpio_nr);
43 else
44 gval = gpio_get_value(gpio_dev->gpio_nr);
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053045
46 if (gval < 0)
47 goto err_get_value;
48
49 if (gpio_dev->active_low)
50 gval = !gval;
51
52 if (gval == 1)
53 type = IR_PULSE;
54
55 rc = ir_raw_event_store_edge(gpio_dev->rcdev, type);
56 if (rc < 0)
57 goto err_get_value;
58
59 ir_raw_event_handle(gpio_dev->rcdev);
60
61err_get_value:
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053062 return IRQ_HANDLED;
63}
64
65static int __devinit gpio_ir_recv_probe(struct platform_device *pdev)
66{
67 struct gpio_rc_dev *gpio_dev;
68 struct rc_dev *rcdev;
69 const struct gpio_ir_recv_platform_data *pdata =
70 pdev->dev.platform_data;
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030071 int rc;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053072
73 if (!pdata)
74 return -EINVAL;
75
76 if (pdata->gpio_nr < 0)
77 return -EINVAL;
78
79 gpio_dev = kzalloc(sizeof(struct gpio_rc_dev), GFP_KERNEL);
80 if (!gpio_dev)
81 return -ENOMEM;
82
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053083 rcdev = rc_allocate_device();
84 if (!rcdev) {
85 rc = -ENOMEM;
86 goto err_allocate_device;
87 }
88
89 rcdev->driver_type = RC_DRIVER_IR_RAW;
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030090 rcdev->allowed_protos = RC_TYPE_ALL;
91 rcdev->input_name = GPIO_IR_DEVICE_NAME;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053092 rcdev->input_id.bustype = BUS_HOST;
Ravi Kumar Ve25bde42012-02-28 01:51:40 -030093 rcdev->driver_name = GPIO_IR_DRIVER_NAME;
Ravi Kumar V9d229f82012-05-10 12:38:05 +053094 rcdev->map_name = RC_MAP_SAMSUNG_NECX;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053095
96 gpio_dev->rcdev = rcdev;
97 gpio_dev->gpio_nr = pdata->gpio_nr;
98 gpio_dev->active_low = pdata->active_low;
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +053099
100 rc = gpio_request(pdata->gpio_nr, "gpio-ir-recv");
101 if (rc < 0)
102 goto err_gpio_request;
Ravi Kumar V507b9622012-06-15 17:25:44 +0530103
104 gpio_dev->can_sleep = gpio_cansleep(pdata->gpio_nr);
105
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530106 rc = gpio_direction_input(pdata->gpio_nr);
107 if (rc < 0)
108 goto err_gpio_direction_input;
109
110 rc = rc_register_device(rcdev);
111 if (rc < 0) {
112 dev_err(&pdev->dev, "failed to register rc device\n");
113 goto err_register_rc_device;
114 }
115
116 platform_set_drvdata(pdev, gpio_dev);
117
Ravi Kumar Ve25bde42012-02-28 01:51:40 -0300118 rc = request_any_context_irq(gpio_to_irq(pdata->gpio_nr),
119 gpio_ir_recv_irq,
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530120 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
Ravi Kumar Ve25bde42012-02-28 01:51:40 -0300121 "gpio-ir-recv-irq", gpio_dev);
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530122 if (rc < 0)
123 goto err_request_irq;
124
Ravi Kumar Vc23ebe42012-05-07 16:00:37 +0530125 device_init_wakeup(&pdev->dev, pdata->can_wakeup);
Trilok Sonicc1f41d2012-04-19 22:24:17 +0530126
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530127 return 0;
128
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530129err_request_irq:
130 platform_set_drvdata(pdev, NULL);
131 rc_unregister_device(rcdev);
132err_register_rc_device:
133err_gpio_direction_input:
134 gpio_free(pdata->gpio_nr);
135err_gpio_request:
136 rc_free_device(rcdev);
137 rcdev = NULL;
138err_allocate_device:
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530139 kfree(gpio_dev);
140 return rc;
141}
142
143static int __devexit gpio_ir_recv_remove(struct platform_device *pdev)
144{
145 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
146
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530147 free_irq(gpio_to_irq(gpio_dev->gpio_nr), gpio_dev);
148 platform_set_drvdata(pdev, NULL);
149 rc_unregister_device(gpio_dev->rcdev);
150 gpio_free(gpio_dev->gpio_nr);
151 rc_free_device(gpio_dev->rcdev);
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530152 kfree(gpio_dev);
153 return 0;
154}
155
Ravi Kumar Ve25bde42012-02-28 01:51:40 -0300156#ifdef CONFIG_PM
157static int gpio_ir_recv_suspend(struct device *dev)
158{
159 struct platform_device *pdev = to_platform_device(dev);
160 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
161
162 if (device_may_wakeup(dev))
163 enable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
164 else
165 disable_irq(gpio_to_irq(gpio_dev->gpio_nr));
166
167 return 0;
168}
169
170static int gpio_ir_recv_resume(struct device *dev)
171{
172 struct platform_device *pdev = to_platform_device(dev);
173 struct gpio_rc_dev *gpio_dev = platform_get_drvdata(pdev);
174
175 if (device_may_wakeup(dev))
176 disable_irq_wake(gpio_to_irq(gpio_dev->gpio_nr));
177 else
178 enable_irq(gpio_to_irq(gpio_dev->gpio_nr));
179
180 return 0;
181}
182
183static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
184 .suspend = gpio_ir_recv_suspend,
185 .resume = gpio_ir_recv_resume,
186};
187#endif
188
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530189static struct platform_driver gpio_ir_recv_driver = {
190 .probe = gpio_ir_recv_probe,
191 .remove = __devexit_p(gpio_ir_recv_remove),
192 .driver = {
Ravi Kumar Ve25bde42012-02-28 01:51:40 -0300193 .name = GPIO_IR_DRIVER_NAME,
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530194 .owner = THIS_MODULE,
Ravi Kumar Ve25bde42012-02-28 01:51:40 -0300195#ifdef CONFIG_PM
196 .pm = &gpio_ir_recv_pm_ops,
197#endif
Ankush Khandelwal3fb6bc22012-01-31 19:09:58 +0530198 },
199};
200
201static int __init gpio_ir_recv_init(void)
202{
203 return platform_driver_register(&gpio_ir_recv_driver);
204}
205module_init(gpio_ir_recv_init);
206
207static void __exit gpio_ir_recv_exit(void)
208{
209 platform_driver_unregister(&gpio_ir_recv_driver);
210}
211module_exit(gpio_ir_recv_exit);
212
213MODULE_DESCRIPTION("GPIO IR Receiver driver");
214MODULE_LICENSE("GPL v2");