Skip to content

Commit 24d79eb

Browse files
seanyoungmchehab
authored andcommitted
media: rc: gpio-ir-tx: add new driver
This is a simple bit-banging GPIO IR TX driver. Signed-off-by: Sean Young <[email protected]> Signed-off-by: Matthias Reichl <[email protected]> Signed-off-by: Mauro Carvalho Chehab <[email protected]>
1 parent 219cb08 commit 24d79eb

File tree

4 files changed

+192
-0
lines changed

4 files changed

+192
-0
lines changed

MAINTAINERS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5769,6 +5769,12 @@ S: Maintained
57695769
F: Documentation/acpi/gpio-properties.txt
57705770
F: drivers/gpio/gpiolib-acpi.c
57715771

5772+
GPIO IR Transmitter
5773+
M: Sean Young <[email protected]>
5774+
5775+
S: Maintained
5776+
F: drivers/media/rc/gpio-ir-tx.c
5777+
57725778
GPIO MOCKUP DRIVER
57735779
M: Bamvor Jian Zhang <[email protected]>
57745780

drivers/media/rc/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,17 @@ config IR_GPIO_CIR
399399
To compile this driver as a module, choose M here: the module will
400400
be called gpio-ir-recv.
401401

402+
config IR_GPIO_TX
403+
tristate "GPIO IR Bit Banging Transmitter"
404+
depends on RC_CORE
405+
depends on LIRC
406+
---help---
407+
Say Y if you want to a GPIO based IR transmitter. This is a
408+
bit banging driver.
409+
410+
To compile this driver as a module, choose M here: the module will
411+
be called gpio-ir-tx.
412+
402413
config RC_ST
403414
tristate "ST remote control receiver"
404415
depends on RC_CORE

drivers/media/rc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ obj-$(CONFIG_IR_STREAMZAP) += streamzap.o
3232
obj-$(CONFIG_IR_WINBOND_CIR) += winbond-cir.o
3333
obj-$(CONFIG_RC_LOOPBACK) += rc-loopback.o
3434
obj-$(CONFIG_IR_GPIO_CIR) += gpio-ir-recv.o
35+
obj-$(CONFIG_IR_GPIO_TX) += gpio-ir-tx.o
3536
obj-$(CONFIG_IR_IGORPLUGUSB) += igorplugusb.o
3637
obj-$(CONFIG_IR_IGUANA) += iguanair.o
3738
obj-$(CONFIG_IR_TTUSBIR) += ttusbir.o

drivers/media/rc/gpio-ir-tx.c

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright (C) 2017 Sean Young <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*/
13+
14+
#include <linux/kernel.h>
15+
#include <linux/module.h>
16+
#include <linux/gpio/consumer.h>
17+
#include <linux/delay.h>
18+
#include <linux/slab.h>
19+
#include <linux/of.h>
20+
#include <linux/platform_device.h>
21+
#include <media/rc-core.h>
22+
23+
#define DRIVER_NAME "gpio-ir-tx"
24+
#define DEVICE_NAME "GPIO IR Bit Banging Transmitter"
25+
26+
struct gpio_ir {
27+
struct gpio_desc *gpio;
28+
unsigned int carrier;
29+
unsigned int duty_cycle;
30+
/* we need a spinlock to hold the cpu while transmitting */
31+
spinlock_t lock;
32+
};
33+
34+
static const struct of_device_id gpio_ir_tx_of_match[] = {
35+
{ .compatible = "gpio-ir-tx", },
36+
{ },
37+
};
38+
MODULE_DEVICE_TABLE(of, gpio_ir_tx_of_match);
39+
40+
static int gpio_ir_tx_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
41+
{
42+
struct gpio_ir *gpio_ir = dev->priv;
43+
44+
gpio_ir->duty_cycle = duty_cycle;
45+
46+
return 0;
47+
}
48+
49+
static int gpio_ir_tx_set_carrier(struct rc_dev *dev, u32 carrier)
50+
{
51+
struct gpio_ir *gpio_ir = dev->priv;
52+
53+
if (!carrier)
54+
return -EINVAL;
55+
56+
gpio_ir->carrier = carrier;
57+
58+
return 0;
59+
}
60+
61+
static int gpio_ir_tx(struct rc_dev *dev, unsigned int *txbuf,
62+
unsigned int count)
63+
{
64+
struct gpio_ir *gpio_ir = dev->priv;
65+
unsigned long flags;
66+
ktime_t edge;
67+
/*
68+
* delta should never exceed 0.5 seconds (IR_MAX_DURATION) and on
69+
* m68k ndelay(s64) does not compile; so use s32 rather than s64.
70+
*/
71+
s32 delta;
72+
int i;
73+
unsigned int pulse, space;
74+
75+
/* Ensure the dividend fits into 32 bit */
76+
pulse = DIV_ROUND_CLOSEST(gpio_ir->duty_cycle * (NSEC_PER_SEC / 100),
77+
gpio_ir->carrier);
78+
space = DIV_ROUND_CLOSEST((100 - gpio_ir->duty_cycle) *
79+
(NSEC_PER_SEC / 100), gpio_ir->carrier);
80+
81+
spin_lock_irqsave(&gpio_ir->lock, flags);
82+
83+
edge = ktime_get();
84+
85+
for (i = 0; i < count; i++) {
86+
if (i % 2) {
87+
// space
88+
edge = ktime_add_us(edge, txbuf[i]);
89+
delta = ktime_us_delta(edge, ktime_get());
90+
if (delta > 10) {
91+
spin_unlock_irqrestore(&gpio_ir->lock, flags);
92+
usleep_range(delta, delta + 10);
93+
spin_lock_irqsave(&gpio_ir->lock, flags);
94+
} else if (delta > 0) {
95+
udelay(delta);
96+
}
97+
} else {
98+
// pulse
99+
ktime_t last = ktime_add_us(edge, txbuf[i]);
100+
101+
while (ktime_get() < last) {
102+
gpiod_set_value(gpio_ir->gpio, 1);
103+
edge += pulse;
104+
delta = edge - ktime_get();
105+
if (delta > 0)
106+
ndelay(delta);
107+
gpiod_set_value(gpio_ir->gpio, 0);
108+
edge += space;
109+
delta = edge - ktime_get();
110+
if (delta > 0)
111+
ndelay(delta);
112+
}
113+
114+
edge = last;
115+
}
116+
}
117+
118+
spin_unlock_irqrestore(&gpio_ir->lock, flags);
119+
120+
return count;
121+
}
122+
123+
static int gpio_ir_tx_probe(struct platform_device *pdev)
124+
{
125+
struct gpio_ir *gpio_ir;
126+
struct rc_dev *rcdev;
127+
int rc;
128+
129+
gpio_ir = devm_kmalloc(&pdev->dev, sizeof(*gpio_ir), GFP_KERNEL);
130+
if (!gpio_ir)
131+
return -ENOMEM;
132+
133+
rcdev = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW_TX);
134+
if (!rcdev)
135+
return -ENOMEM;
136+
137+
gpio_ir->gpio = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
138+
if (IS_ERR(gpio_ir->gpio)) {
139+
if (PTR_ERR(gpio_ir->gpio) != -EPROBE_DEFER)
140+
dev_err(&pdev->dev, "Failed to get gpio (%ld)\n",
141+
PTR_ERR(gpio_ir->gpio));
142+
return PTR_ERR(gpio_ir->gpio);
143+
}
144+
145+
rcdev->priv = gpio_ir;
146+
rcdev->driver_name = DRIVER_NAME;
147+
rcdev->device_name = DEVICE_NAME;
148+
rcdev->tx_ir = gpio_ir_tx;
149+
rcdev->s_tx_duty_cycle = gpio_ir_tx_set_duty_cycle;
150+
rcdev->s_tx_carrier = gpio_ir_tx_set_carrier;
151+
152+
gpio_ir->carrier = 38000;
153+
gpio_ir->duty_cycle = 50;
154+
spin_lock_init(&gpio_ir->lock);
155+
156+
rc = devm_rc_register_device(&pdev->dev, rcdev);
157+
if (rc < 0)
158+
dev_err(&pdev->dev, "failed to register rc device\n");
159+
160+
return rc;
161+
}
162+
163+
static struct platform_driver gpio_ir_tx_driver = {
164+
.probe = gpio_ir_tx_probe,
165+
.driver = {
166+
.name = DRIVER_NAME,
167+
.of_match_table = of_match_ptr(gpio_ir_tx_of_match),
168+
},
169+
};
170+
module_platform_driver(gpio_ir_tx_driver);
171+
172+
MODULE_DESCRIPTION("GPIO IR Bit Banging Transmitter");
173+
MODULE_AUTHOR("Sean Young <[email protected]>");
174+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)