1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2017 Sean Young <sean@mess.org> 4 */ 5 6 #include <linux/kernel.h> 7 #include <linux/module.h> 8 #include <linux/gpio/consumer.h> 9 #include <linux/delay.h> 10 #include <linux/slab.h> 11 #include <linux/of.h> 12 #include <linux/platform_device.h> 13 #include <media/rc-core.h> 14 15 #define DRIVER_NAME "gpio-ir-tx" 16 #define DEVICE_NAME "GPIO IR Bit Banging Transmitter" 17 18 struct gpio_ir { 19 struct gpio_desc *gpio; 20 unsigned int carrier; 21 unsigned int duty_cycle; 22 }; 23 24 static const struct of_device_id gpio_ir_tx_of_match[] = { 25 { .compatible = "gpio-ir-tx", }, 26 { }, 27 }; 28 MODULE_DEVICE_TABLE(of, gpio_ir_tx_of_match); 29 30 static int gpio_ir_tx_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle) 31 { 32 struct gpio_ir *gpio_ir = dev->priv; 33 34 gpio_ir->duty_cycle = duty_cycle; 35 36 return 0; 37 } 38 39 static int gpio_ir_tx_set_carrier(struct rc_dev *dev, u32 carrier) 40 { 41 struct gpio_ir *gpio_ir = dev->priv; 42 43 if (carrier > 500000) 44 return -EINVAL; 45 46 gpio_ir->carrier = carrier; 47 48 return 0; 49 } 50 51 static void delay_until(ktime_t until) 52 { 53 /* 54 * delta should never exceed 0.5 seconds (IR_MAX_DURATION) and on 55 * m68k ndelay(s64) does not compile; so use s32 rather than s64. 56 */ 57 s32 delta; 58 59 while (true) { 60 delta = ktime_us_delta(until, ktime_get()); 61 if (delta <= 0) 62 return; 63 64 /* udelay more than 1ms may not work */ 65 delta = min(delta, 1000); 66 udelay(delta); 67 } 68 } 69 70 static void gpio_ir_tx_unmodulated(struct gpio_ir *gpio_ir, uint *txbuf, 71 uint count) 72 { 73 ktime_t edge; 74 int i; 75 76 local_irq_disable(); 77 78 edge = ktime_get(); 79 80 for (i = 0; i < count; i++) { 81 gpiod_set_value(gpio_ir->gpio, !(i % 2)); 82 83 edge = ktime_add_us(edge, txbuf[i]); 84 delay_until(edge); 85 } 86 87 gpiod_set_value(gpio_ir->gpio, 0); 88 } 89 90 static void gpio_ir_tx_modulated(struct gpio_ir *gpio_ir, uint *txbuf, 91 uint count) 92 { 93 ktime_t edge; 94 /* 95 * delta should never exceed 0.5 seconds (IR_MAX_DURATION) and on 96 * m68k ndelay(s64) does not compile; so use s32 rather than s64. 97 */ 98 s32 delta; 99 int i; 100 unsigned int pulse, space; 101 102 /* Ensure the dividend fits into 32 bit */ 103 pulse = DIV_ROUND_CLOSEST(gpio_ir->duty_cycle * (NSEC_PER_SEC / 100), 104 gpio_ir->carrier); 105 space = DIV_ROUND_CLOSEST((100 - gpio_ir->duty_cycle) * 106 (NSEC_PER_SEC / 100), gpio_ir->carrier); 107 108 local_irq_disable(); 109 110 edge = ktime_get(); 111 112 for (i = 0; i < count; i++) { 113 if (i % 2) { 114 // space 115 edge = ktime_add_us(edge, txbuf[i]); 116 delay_until(edge); 117 } else { 118 // pulse 119 ktime_t last = ktime_add_us(edge, txbuf[i]); 120 121 while (ktime_before(ktime_get(), last)) { 122 gpiod_set_value(gpio_ir->gpio, 1); 123 edge = ktime_add_ns(edge, pulse); 124 delta = ktime_to_ns(ktime_sub(edge, 125 ktime_get())); 126 if (delta > 0) 127 ndelay(delta); 128 gpiod_set_value(gpio_ir->gpio, 0); 129 edge = ktime_add_ns(edge, space); 130 delta = ktime_to_ns(ktime_sub(edge, 131 ktime_get())); 132 if (delta > 0) 133 ndelay(delta); 134 } 135 136 edge = last; 137 } 138 } 139 } 140 141 static int gpio_ir_tx(struct rc_dev *dev, unsigned int *txbuf, 142 unsigned int count) 143 { 144 struct gpio_ir *gpio_ir = dev->priv; 145 unsigned long flags; 146 147 local_irq_save(flags); 148 if (gpio_ir->carrier) 149 gpio_ir_tx_modulated(gpio_ir, txbuf, count); 150 else 151 gpio_ir_tx_unmodulated(gpio_ir, txbuf, count); 152 local_irq_restore(flags); 153 154 return count; 155 } 156 157 static int gpio_ir_tx_probe(struct platform_device *pdev) 158 { 159 struct gpio_ir *gpio_ir; 160 struct rc_dev *rcdev; 161 int rc; 162 163 gpio_ir = devm_kmalloc(&pdev->dev, sizeof(*gpio_ir), GFP_KERNEL); 164 if (!gpio_ir) 165 return -ENOMEM; 166 167 rcdev = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW_TX); 168 if (!rcdev) 169 return -ENOMEM; 170 171 gpio_ir->gpio = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW); 172 if (IS_ERR(gpio_ir->gpio)) { 173 if (PTR_ERR(gpio_ir->gpio) != -EPROBE_DEFER) 174 dev_err(&pdev->dev, "Failed to get gpio (%ld)\n", 175 PTR_ERR(gpio_ir->gpio)); 176 return PTR_ERR(gpio_ir->gpio); 177 } 178 179 rcdev->priv = gpio_ir; 180 rcdev->driver_name = DRIVER_NAME; 181 rcdev->device_name = DEVICE_NAME; 182 rcdev->tx_ir = gpio_ir_tx; 183 rcdev->s_tx_duty_cycle = gpio_ir_tx_set_duty_cycle; 184 rcdev->s_tx_carrier = gpio_ir_tx_set_carrier; 185 186 gpio_ir->carrier = 38000; 187 gpio_ir->duty_cycle = 50; 188 189 rc = devm_rc_register_device(&pdev->dev, rcdev); 190 if (rc < 0) 191 dev_err(&pdev->dev, "failed to register rc device\n"); 192 193 return rc; 194 } 195 196 static struct platform_driver gpio_ir_tx_driver = { 197 .probe = gpio_ir_tx_probe, 198 .driver = { 199 .name = DRIVER_NAME, 200 .of_match_table = of_match_ptr(gpio_ir_tx_of_match), 201 }, 202 }; 203 module_platform_driver(gpio_ir_tx_driver); 204 205 MODULE_DESCRIPTION("GPIO IR Bit Banging Transmitter"); 206 MODULE_AUTHOR("Sean Young <sean@mess.org>"); 207 MODULE_LICENSE("GPL"); 208