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/of_gpio.h>
20 #include <linux/platform_device.h>
21 #include <linux/irq.h>
22 #include <media/rc-core.h>
23 #include <media/gpio-ir-recv.h>
25 #define GPIO_IR_DRIVER_NAME "gpio-rc-recv"
26 #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
36 * Translate OpenFirmware node properties into platform_data
38 static int gpio_ir_recv_get_devtree_pdata(struct device
*dev
,
39 struct gpio_ir_recv_platform_data
*pdata
)
41 struct device_node
*np
= dev
->of_node
;
42 enum of_gpio_flags flags
;
45 gpio
= of_get_gpio_flags(np
, 0, &flags
);
47 if (gpio
!= -EPROBE_DEFER
)
48 dev_err(dev
, "Failed to get gpio flags (%d)\n", gpio
);
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;
61 static struct of_device_id gpio_ir_recv_of_match
[] = {
62 { .compatible
= "gpio-ir-receiver", },
65 MODULE_DEVICE_TABLE(of
, gpio_ir_recv_of_match
);
67 #else /* !CONFIG_OF */
69 #define gpio_ir_recv_get_devtree_pdata(dev, pdata) (-ENOSYS)
73 static irqreturn_t
gpio_ir_recv_irq(int irq
, void *dev_id
)
75 struct gpio_rc_dev
*gpio_dev
= dev_id
;
78 enum raw_event_type type
= IR_SPACE
;
80 gval
= gpio_get_value_cansleep(gpio_dev
->gpio_nr
);
85 if (gpio_dev
->active_low
)
91 rc
= ir_raw_event_store_edge(gpio_dev
->rcdev
, type
);
95 ir_raw_event_handle(gpio_dev
->rcdev
);
101 static int gpio_ir_recv_probe(struct platform_device
*pdev
)
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
;
109 if (pdev
->dev
.of_node
) {
110 struct gpio_ir_recv_platform_data
*dtpdata
=
111 devm_kzalloc(&pdev
->dev
, sizeof(*dtpdata
), GFP_KERNEL
);
114 rc
= gpio_ir_recv_get_devtree_pdata(&pdev
->dev
, dtpdata
);
123 if (pdata
->gpio_nr
< 0)
126 gpio_dev
= kzalloc(sizeof(struct gpio_rc_dev
), GFP_KERNEL
);
130 rcdev
= rc_allocate_device();
133 goto err_allocate_device
;
136 rcdev
->priv
= gpio_dev
;
137 rcdev
->driver_type
= RC_DRIVER_IR_RAW
;
138 rcdev
->input_name
= GPIO_IR_DEVICE_NAME
;
139 rcdev
->input_phys
= GPIO_IR_DEVICE_NAME
"/input0";
140 rcdev
->input_id
.bustype
= BUS_HOST
;
141 rcdev
->input_id
.vendor
= 0x0001;
142 rcdev
->input_id
.product
= 0x0001;
143 rcdev
->input_id
.version
= 0x0100;
144 rcdev
->dev
.parent
= &pdev
->dev
;
145 rcdev
->driver_name
= GPIO_IR_DRIVER_NAME
;
146 if (pdata
->allowed_protos
)
147 rcdev
->allowed_protos
= pdata
->allowed_protos
;
149 rcdev
->allowed_protos
= RC_BIT_ALL
;
150 rcdev
->map_name
= pdata
->map_name
?: RC_MAP_EMPTY
;
152 gpio_dev
->rcdev
= rcdev
;
153 gpio_dev
->gpio_nr
= pdata
->gpio_nr
;
154 gpio_dev
->active_low
= pdata
->active_low
;
156 rc
= gpio_request(pdata
->gpio_nr
, "gpio-ir-recv");
158 goto err_gpio_request
;
159 rc
= gpio_direction_input(pdata
->gpio_nr
);
161 goto err_gpio_direction_input
;
163 rc
= rc_register_device(rcdev
);
165 dev_err(&pdev
->dev
, "failed to register rc device\n");
166 goto err_register_rc_device
;
169 platform_set_drvdata(pdev
, gpio_dev
);
171 rc
= request_any_context_irq(gpio_to_irq(pdata
->gpio_nr
),
173 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
174 "gpio-ir-recv-irq", gpio_dev
);
176 goto err_request_irq
;
181 rc_unregister_device(rcdev
);
183 err_register_rc_device
:
184 err_gpio_direction_input
:
185 gpio_free(pdata
->gpio_nr
);
187 rc_free_device(rcdev
);
193 static int gpio_ir_recv_remove(struct platform_device
*pdev
)
195 struct gpio_rc_dev
*gpio_dev
= platform_get_drvdata(pdev
);
197 free_irq(gpio_to_irq(gpio_dev
->gpio_nr
), gpio_dev
);
198 rc_unregister_device(gpio_dev
->rcdev
);
199 gpio_free(gpio_dev
->gpio_nr
);
205 static int gpio_ir_recv_suspend(struct device
*dev
)
207 struct platform_device
*pdev
= to_platform_device(dev
);
208 struct gpio_rc_dev
*gpio_dev
= platform_get_drvdata(pdev
);
210 if (device_may_wakeup(dev
))
211 enable_irq_wake(gpio_to_irq(gpio_dev
->gpio_nr
));
213 disable_irq(gpio_to_irq(gpio_dev
->gpio_nr
));
218 static int gpio_ir_recv_resume(struct device
*dev
)
220 struct platform_device
*pdev
= to_platform_device(dev
);
221 struct gpio_rc_dev
*gpio_dev
= platform_get_drvdata(pdev
);
223 if (device_may_wakeup(dev
))
224 disable_irq_wake(gpio_to_irq(gpio_dev
->gpio_nr
));
226 enable_irq(gpio_to_irq(gpio_dev
->gpio_nr
));
231 static const struct dev_pm_ops gpio_ir_recv_pm_ops
= {
232 .suspend
= gpio_ir_recv_suspend
,
233 .resume
= gpio_ir_recv_resume
,
237 static struct platform_driver gpio_ir_recv_driver
= {
238 .probe
= gpio_ir_recv_probe
,
239 .remove
= gpio_ir_recv_remove
,
241 .name
= GPIO_IR_DRIVER_NAME
,
242 .owner
= THIS_MODULE
,
243 .of_match_table
= of_match_ptr(gpio_ir_recv_of_match
),
245 .pm
= &gpio_ir_recv_pm_ops
,
249 module_platform_driver(gpio_ir_recv_driver
);
251 MODULE_DESCRIPTION("GPIO IR Receiver driver");
252 MODULE_LICENSE("GPL v2");