1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
5 #include <linux/kernel.h>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/interrupt.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/slab.h>
12 #include <linux/of_gpio.h>
13 #include <linux/platform_device.h>
14 #include <linux/irq.h>
15 #include <media/rc-core.h>
17 #define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
21 struct gpio_desc
*gpiod
;
25 static irqreturn_t
gpio_ir_recv_irq(int irq
, void *dev_id
)
28 struct gpio_rc_dev
*gpio_dev
= dev_id
;
30 val
= gpiod_get_value(gpio_dev
->gpiod
);
32 ir_raw_event_store_edge(gpio_dev
->rcdev
, val
== 1);
37 static int gpio_ir_recv_probe(struct platform_device
*pdev
)
39 struct device
*dev
= &pdev
->dev
;
40 struct device_node
*np
= dev
->of_node
;
41 struct gpio_rc_dev
*gpio_dev
;
48 gpio_dev
= devm_kzalloc(dev
, sizeof(*gpio_dev
), GFP_KERNEL
);
52 gpio_dev
->gpiod
= devm_gpiod_get(dev
, NULL
, GPIOD_IN
);
53 if (IS_ERR(gpio_dev
->gpiod
)) {
54 rc
= PTR_ERR(gpio_dev
->gpiod
);
55 /* Just try again if this happens */
56 if (rc
!= -EPROBE_DEFER
)
57 dev_err(dev
, "error getting gpio (%d)\n", rc
);
60 gpio_dev
->irq
= gpiod_to_irq(gpio_dev
->gpiod
);
61 if (gpio_dev
->irq
< 0)
64 rcdev
= devm_rc_allocate_device(dev
, RC_DRIVER_IR_RAW
);
68 rcdev
->priv
= gpio_dev
;
69 rcdev
->device_name
= GPIO_IR_DEVICE_NAME
;
70 rcdev
->input_phys
= GPIO_IR_DEVICE_NAME
"/input0";
71 rcdev
->input_id
.bustype
= BUS_HOST
;
72 rcdev
->input_id
.vendor
= 0x0001;
73 rcdev
->input_id
.product
= 0x0001;
74 rcdev
->input_id
.version
= 0x0100;
75 rcdev
->dev
.parent
= dev
;
76 rcdev
->driver_name
= KBUILD_MODNAME
;
77 rcdev
->min_timeout
= 1;
78 rcdev
->timeout
= IR_DEFAULT_TIMEOUT
;
79 rcdev
->max_timeout
= 10 * IR_DEFAULT_TIMEOUT
;
80 rcdev
->allowed_protocols
= RC_PROTO_BIT_ALL_IR_DECODER
;
81 rcdev
->map_name
= of_get_property(np
, "linux,rc-map-name", NULL
);
83 rcdev
->map_name
= RC_MAP_EMPTY
;
85 gpio_dev
->rcdev
= rcdev
;
87 rc
= devm_rc_register_device(dev
, rcdev
);
89 dev_err(dev
, "failed to register rc device (%d)\n", rc
);
93 platform_set_drvdata(pdev
, gpio_dev
);
95 return devm_request_irq(dev
, gpio_dev
->irq
, gpio_ir_recv_irq
,
96 IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
,
97 "gpio-ir-recv-irq", gpio_dev
);
101 static int gpio_ir_recv_suspend(struct device
*dev
)
103 struct gpio_rc_dev
*gpio_dev
= dev_get_drvdata(dev
);
105 if (device_may_wakeup(dev
))
106 enable_irq_wake(gpio_dev
->irq
);
108 disable_irq(gpio_dev
->irq
);
113 static int gpio_ir_recv_resume(struct device
*dev
)
115 struct gpio_rc_dev
*gpio_dev
= dev_get_drvdata(dev
);
117 if (device_may_wakeup(dev
))
118 disable_irq_wake(gpio_dev
->irq
);
120 enable_irq(gpio_dev
->irq
);
125 static const struct dev_pm_ops gpio_ir_recv_pm_ops
= {
126 .suspend
= gpio_ir_recv_suspend
,
127 .resume
= gpio_ir_recv_resume
,
131 static const struct of_device_id gpio_ir_recv_of_match
[] = {
132 { .compatible
= "gpio-ir-receiver", },
135 MODULE_DEVICE_TABLE(of
, gpio_ir_recv_of_match
);
137 static struct platform_driver gpio_ir_recv_driver
= {
138 .probe
= gpio_ir_recv_probe
,
140 .name
= KBUILD_MODNAME
,
141 .of_match_table
= of_match_ptr(gpio_ir_recv_of_match
),
143 .pm
= &gpio_ir_recv_pm_ops
,
147 module_platform_driver(gpio_ir_recv_driver
);
149 MODULE_DESCRIPTION("GPIO IR Receiver driver");
150 MODULE_LICENSE("GPL v2");