2 * Copyright (C) 2017 Sean Young <sean@mess.org>
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.
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.
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>
20 #include <linux/platform_device.h>
21 #include <media/rc-core.h>
23 #define DRIVER_NAME "gpio-ir-tx"
24 #define DEVICE_NAME "GPIO IR Bit Banging Transmitter"
27 struct gpio_desc
*gpio
;
29 unsigned int duty_cycle
;
30 /* we need a spinlock to hold the cpu while transmitting */
34 static const struct of_device_id gpio_ir_tx_of_match
[] = {
35 { .compatible
= "gpio-ir-tx", },
38 MODULE_DEVICE_TABLE(of
, gpio_ir_tx_of_match
);
40 static int gpio_ir_tx_set_duty_cycle(struct rc_dev
*dev
, u32 duty_cycle
)
42 struct gpio_ir
*gpio_ir
= dev
->priv
;
44 gpio_ir
->duty_cycle
= duty_cycle
;
49 static int gpio_ir_tx_set_carrier(struct rc_dev
*dev
, u32 carrier
)
51 struct gpio_ir
*gpio_ir
= dev
->priv
;
56 gpio_ir
->carrier
= carrier
;
61 static int gpio_ir_tx(struct rc_dev
*dev
, unsigned int *txbuf
,
64 struct gpio_ir
*gpio_ir
= dev
->priv
;
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.
73 unsigned int pulse
, space
;
75 /* Ensure the dividend fits into 32 bit */
76 pulse
= DIV_ROUND_CLOSEST(gpio_ir
->duty_cycle
* (NSEC_PER_SEC
/ 100),
78 space
= DIV_ROUND_CLOSEST((100 - gpio_ir
->duty_cycle
) *
79 (NSEC_PER_SEC
/ 100), gpio_ir
->carrier
);
81 spin_lock_irqsave(&gpio_ir
->lock
, flags
);
85 for (i
= 0; i
< count
; i
++) {
88 edge
= ktime_add_us(edge
, txbuf
[i
]);
89 delta
= ktime_us_delta(edge
, ktime_get());
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) {
99 ktime_t last
= ktime_add_us(edge
, txbuf
[i
]);
101 while (ktime_before(ktime_get(), last
)) {
102 gpiod_set_value(gpio_ir
->gpio
, 1);
103 edge
= ktime_add_ns(edge
, pulse
);
104 delta
= ktime_to_ns(ktime_sub(edge
,
108 gpiod_set_value(gpio_ir
->gpio
, 0);
109 edge
= ktime_add_ns(edge
, space
);
110 delta
= ktime_to_ns(ktime_sub(edge
,
120 spin_unlock_irqrestore(&gpio_ir
->lock
, flags
);
125 static int gpio_ir_tx_probe(struct platform_device
*pdev
)
127 struct gpio_ir
*gpio_ir
;
128 struct rc_dev
*rcdev
;
131 gpio_ir
= devm_kmalloc(&pdev
->dev
, sizeof(*gpio_ir
), GFP_KERNEL
);
135 rcdev
= devm_rc_allocate_device(&pdev
->dev
, RC_DRIVER_IR_RAW_TX
);
139 gpio_ir
->gpio
= devm_gpiod_get(&pdev
->dev
, NULL
, GPIOD_OUT_LOW
);
140 if (IS_ERR(gpio_ir
->gpio
)) {
141 if (PTR_ERR(gpio_ir
->gpio
) != -EPROBE_DEFER
)
142 dev_err(&pdev
->dev
, "Failed to get gpio (%ld)\n",
143 PTR_ERR(gpio_ir
->gpio
));
144 return PTR_ERR(gpio_ir
->gpio
);
147 rcdev
->priv
= gpio_ir
;
148 rcdev
->driver_name
= DRIVER_NAME
;
149 rcdev
->device_name
= DEVICE_NAME
;
150 rcdev
->tx_ir
= gpio_ir_tx
;
151 rcdev
->s_tx_duty_cycle
= gpio_ir_tx_set_duty_cycle
;
152 rcdev
->s_tx_carrier
= gpio_ir_tx_set_carrier
;
154 gpio_ir
->carrier
= 38000;
155 gpio_ir
->duty_cycle
= 50;
156 spin_lock_init(&gpio_ir
->lock
);
158 rc
= devm_rc_register_device(&pdev
->dev
, rcdev
);
160 dev_err(&pdev
->dev
, "failed to register rc device\n");
165 static struct platform_driver gpio_ir_tx_driver
= {
166 .probe
= gpio_ir_tx_probe
,
169 .of_match_table
= of_match_ptr(gpio_ir_tx_of_match
),
172 module_platform_driver(gpio_ir_tx_driver
);
174 MODULE_DESCRIPTION("GPIO IR Bit Banging Transmitter");
175 MODULE_AUTHOR("Sean Young <sean@mess.org>");
176 MODULE_LICENSE("GPL");