blob: 3d99b51384ac9f3329eccd50efe67049e7237e0c [file] [log] [blame]
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -03001/* 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>
Ladislav Michlc1301072017-10-05 09:11:06 -030017#include <linux/gpio/consumer.h>
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030018#include <linux/slab.h>
Sachin Kamat8ab1aa82013-10-18 00:07:15 -030019#include <linux/of.h>
Sebastian Hesselbarth2fd7f392013-02-08 18:47:30 -030020#include <linux/of_gpio.h>
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030021#include <linux/platform_device.h>
22#include <linux/irq.h>
23#include <media/rc-core.h>
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030024
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030025#define GPIO_IR_DEVICE_NAME "gpio_ir_recv"
26
27struct gpio_rc_dev {
28 struct rc_dev *rcdev;
Ladislav Michleed008e2017-09-07 20:41:32 -030029 struct gpio_desc *gpiod;
30 int irq;
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030031};
32
33static irqreturn_t gpio_ir_recv_irq(int irq, void *dev_id)
34{
Ladislav Michleed008e2017-09-07 20:41:32 -030035 int val;
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030036 struct gpio_rc_dev *gpio_dev = dev_id;
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030037
Ladislav Michleed008e2017-09-07 20:41:32 -030038 val = gpiod_get_value(gpio_dev->gpiod);
39 if (val >= 0)
40 ir_raw_event_store_edge(gpio_dev->rcdev, val == 1);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030041
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030042 return IRQ_HANDLED;
43}
44
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -080045static int gpio_ir_recv_probe(struct platform_device *pdev)
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030046{
Ladislav Michl94d40b22017-09-07 20:34:35 -030047 struct device *dev = &pdev->dev;
Ladislav Michl5c958782017-09-07 20:39:45 -030048 struct device_node *np = dev->of_node;
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030049 struct gpio_rc_dev *gpio_dev;
50 struct rc_dev *rcdev;
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030051 int rc;
52
Ladislav Michl5c958782017-09-07 20:39:45 -030053 if (!np)
54 return -ENODEV;
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030055
Ladislav Michl08d94272017-09-07 20:35:22 -030056 gpio_dev = devm_kzalloc(dev, sizeof(*gpio_dev), GFP_KERNEL);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030057 if (!gpio_dev)
58 return -ENOMEM;
59
Ladislav Michleed008e2017-09-07 20:41:32 -030060 gpio_dev->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
61 if (IS_ERR(gpio_dev->gpiod)) {
62 rc = PTR_ERR(gpio_dev->gpiod);
63 /* Just try again if this happens */
Ladislav Michl5c958782017-09-07 20:39:45 -030064 if (rc != -EPROBE_DEFER)
Ladislav Michleed008e2017-09-07 20:41:32 -030065 dev_err(dev, "error getting gpio (%d)\n", rc);
Ladislav Michl5c958782017-09-07 20:39:45 -030066 return rc;
67 }
Ladislav Michleed008e2017-09-07 20:41:32 -030068 gpio_dev->irq = gpiod_to_irq(gpio_dev->gpiod);
69 if (gpio_dev->irq < 0)
70 return gpio_dev->irq;
Ladislav Michl5c958782017-09-07 20:39:45 -030071
Ladislav Michlf4940b52017-09-07 20:36:11 -030072 rcdev = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
Ladislav Michl08d94272017-09-07 20:35:22 -030073 if (!rcdev)
74 return -ENOMEM;
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030075
Benoît Thébaudeau975ef322012-06-18 15:02:20 -030076 rcdev->priv = gpio_dev;
Sean Young518f4b22017-07-01 12:13:19 -040077 rcdev->device_name = GPIO_IR_DEVICE_NAME;
Benoît Thébaudeau975ef322012-06-18 15:02:20 -030078 rcdev->input_phys = GPIO_IR_DEVICE_NAME "/input0";
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030079 rcdev->input_id.bustype = BUS_HOST;
Benoît Thébaudeau975ef322012-06-18 15:02:20 -030080 rcdev->input_id.vendor = 0x0001;
81 rcdev->input_id.product = 0x0001;
82 rcdev->input_id.version = 0x0100;
Ladislav Michl94d40b22017-09-07 20:34:35 -030083 rcdev->dev.parent = dev;
Ladislav Michl916d1c92017-09-07 20:39:14 -030084 rcdev->driver_name = KBUILD_MODNAME;
Sean Youngee5310e2017-01-26 14:37:33 -020085 rcdev->min_timeout = 1;
Eric Nelson3fb136f2015-09-23 11:07:08 -030086 rcdev->timeout = IR_DEFAULT_TIMEOUT;
87 rcdev->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
Ladislav Michl5c958782017-09-07 20:39:45 -030088 rcdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
89 rcdev->map_name = of_get_property(np, "linux,rc-map-name", NULL);
90 if (!rcdev->map_name)
91 rcdev->map_name = RC_MAP_EMPTY;
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030092
93 gpio_dev->rcdev = rcdev;
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030094
Ladislav Michl52ea7992017-09-07 20:37:07 -030095 rc = devm_rc_register_device(dev, rcdev);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030096 if (rc < 0) {
Ladislav Michl94d40b22017-09-07 20:34:35 -030097 dev_err(dev, "failed to register rc device (%d)\n", rc);
Ladislav Michl52ea7992017-09-07 20:37:07 -030098 return rc;
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -030099 }
100
101 platform_set_drvdata(pdev, gpio_dev);
102
Ladislav Michleed008e2017-09-07 20:41:32 -0300103 return devm_request_irq(dev, gpio_dev->irq, gpio_ir_recv_irq,
Ladislav Michl1a2a60b2017-09-07 20:38:20 -0300104 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
105 "gpio-ir-recv-irq", gpio_dev);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300106}
107
108#ifdef CONFIG_PM
109static int gpio_ir_recv_suspend(struct device *dev)
110{
Ladislav Michleed008e2017-09-07 20:41:32 -0300111 struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300112
113 if (device_may_wakeup(dev))
Ladislav Michleed008e2017-09-07 20:41:32 -0300114 enable_irq_wake(gpio_dev->irq);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300115 else
Ladislav Michleed008e2017-09-07 20:41:32 -0300116 disable_irq(gpio_dev->irq);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300117
118 return 0;
119}
120
121static int gpio_ir_recv_resume(struct device *dev)
122{
Ladislav Michleed008e2017-09-07 20:41:32 -0300123 struct gpio_rc_dev *gpio_dev = dev_get_drvdata(dev);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300124
125 if (device_may_wakeup(dev))
Ladislav Michleed008e2017-09-07 20:41:32 -0300126 disable_irq_wake(gpio_dev->irq);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300127 else
Ladislav Michleed008e2017-09-07 20:41:32 -0300128 enable_irq(gpio_dev->irq);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300129
130 return 0;
131}
132
133static const struct dev_pm_ops gpio_ir_recv_pm_ops = {
134 .suspend = gpio_ir_recv_suspend,
135 .resume = gpio_ir_recv_resume,
136};
137#endif
138
Ladislav Michl5c958782017-09-07 20:39:45 -0300139static const struct of_device_id gpio_ir_recv_of_match[] = {
140 { .compatible = "gpio-ir-receiver", },
141 { },
142};
143MODULE_DEVICE_TABLE(of, gpio_ir_recv_of_match);
144
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300145static struct platform_driver gpio_ir_recv_driver = {
146 .probe = gpio_ir_recv_probe,
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300147 .driver = {
Ladislav Michl916d1c92017-09-07 20:39:14 -0300148 .name = KBUILD_MODNAME,
Sebastian Hesselbarth2fd7f392013-02-08 18:47:30 -0300149 .of_match_table = of_match_ptr(gpio_ir_recv_of_match),
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300150#ifdef CONFIG_PM
151 .pm = &gpio_ir_recv_pm_ops,
152#endif
153 },
154};
Benoît Thébaudeau81d75e92012-06-18 15:03:06 -0300155module_platform_driver(gpio_ir_recv_driver);
Ravi Kumar Vfd0f6852012-02-28 01:51:40 -0300156
157MODULE_DESCRIPTION("GPIO IR Receiver driver");
158MODULE_LICENSE("GPL v2");