1 /* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
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.
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.
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>
24 #define GPIO_IR_DRIVER_NAME "gpio-rc-recv"
25 #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
33 static irqreturn_t
gpio_ir_recv_irq(int irq
, void *dev_id
)
35 struct gpio_rc_dev
*gpio_dev
= dev_id
;
38 enum raw_event_type type
= IR_SPACE
;
40 gval
= gpio_get_value_cansleep(gpio_dev
->gpio_nr
);
45 if (gpio_dev
->active_low
)
51 rc
= ir_raw_event_store_edge(gpio_dev
->rcdev
, type
);
55 ir_raw_event_handle(gpio_dev
->rcdev
);
61 static int __devinit
gpio_ir_recv_probe(struct platform_device
*pdev
)
63 struct gpio_rc_dev
*gpio_dev
;
65 const struct gpio_ir_recv_platform_data
*pdata
=
66 pdev
->dev
.platform_data
;
72 if (pdata
->gpio_nr
< 0)
75 gpio_dev
= kzalloc(sizeof(struct gpio_rc_dev
), GFP_KERNEL
);
79 rcdev
= rc_allocate_device();
82 goto err_allocate_device
;
85 rcdev
->priv
= gpio_dev
;
86 rcdev
->driver_type
= RC_DRIVER_IR_RAW
;
87 rcdev
->input_name
= GPIO_IR_DEVICE_NAME
;
88 rcdev
->input_phys
= GPIO_IR_DEVICE_NAME
"/input0";
89 rcdev
->input_id
.bustype
= BUS_HOST
;
90 rcdev
->input_id
.vendor
= 0x0001;
91 rcdev
->input_id
.product
= 0x0001;
92 rcdev
->input_id
.version
= 0x0100;
93 rcdev
->dev
.parent
= &pdev
->dev
;
94 rcdev
->driver_name
= GPIO_IR_DRIVER_NAME
;
95 if (pdata
->allowed_protos
)
96 rcdev
->allowed_protos
= pdata
->allowed_protos
;
98 rcdev
->allowed_protos
= RC_TYPE_ALL
;
99 rcdev
->map_name
= pdata
->map_name
?: RC_MAP_EMPTY
;
101 gpio_dev
->rcdev
= rcdev
;
102 gpio_dev
->gpio_nr
= pdata
->gpio_nr
;
103 gpio_dev
->active_low
= pdata
->active_low
;
105 rc
= gpio_request(pdata
->gpio_nr
, "gpio-ir-recv");
107 goto err_gpio_request
;
108 rc
= gpio_direction_input(pdata
->gpio_nr
);
110 goto err_gpio_direction_input
;
112 rc
= rc_register_device(rcdev
);
114 dev_err(&pdev
->dev
, "failed to register rc device\n");
115 goto err_register_rc_device
;
118 platform_set_drvdata(pdev
, gpio_dev
);
120 rc
= request_any_context_irq(gpio_to_irq(pdata
->gpio_nr
),
122 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
123 "gpio-ir-recv-irq", gpio_dev
);
125 goto err_request_irq
;
130 platform_set_drvdata(pdev
, NULL
);
131 rc_unregister_device(rcdev
);
132 err_register_rc_device
:
133 err_gpio_direction_input
:
134 gpio_free(pdata
->gpio_nr
);
136 rc_free_device(rcdev
);
143 static int __devexit
gpio_ir_recv_remove(struct platform_device
*pdev
)
145 struct gpio_rc_dev
*gpio_dev
= platform_get_drvdata(pdev
);
147 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
);
157 static int gpio_ir_recv_suspend(struct device
*dev
)
159 struct platform_device
*pdev
= to_platform_device(dev
);
160 struct gpio_rc_dev
*gpio_dev
= platform_get_drvdata(pdev
);
162 if (device_may_wakeup(dev
))
163 enable_irq_wake(gpio_to_irq(gpio_dev
->gpio_nr
));
165 disable_irq(gpio_to_irq(gpio_dev
->gpio_nr
));
170 static int gpio_ir_recv_resume(struct device
*dev
)
172 struct platform_device
*pdev
= to_platform_device(dev
);
173 struct gpio_rc_dev
*gpio_dev
= platform_get_drvdata(pdev
);
175 if (device_may_wakeup(dev
))
176 disable_irq_wake(gpio_to_irq(gpio_dev
->gpio_nr
));
178 enable_irq(gpio_to_irq(gpio_dev
->gpio_nr
));
183 static const struct dev_pm_ops gpio_ir_recv_pm_ops
= {
184 .suspend
= gpio_ir_recv_suspend
,
185 .resume
= gpio_ir_recv_resume
,
189 static struct platform_driver gpio_ir_recv_driver
= {
190 .probe
= gpio_ir_recv_probe
,
191 .remove
= __devexit_p(gpio_ir_recv_remove
),
193 .name
= GPIO_IR_DRIVER_NAME
,
194 .owner
= THIS_MODULE
,
196 .pm
= &gpio_ir_recv_pm_ops
,
200 module_platform_driver(gpio_ir_recv_driver
);
202 MODULE_DESCRIPTION("GPIO IR Receiver driver");
203 MODULE_LICENSE("GPL v2");