• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 Sean Young <sean@mess.org>
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 
gpio_ir_tx_set_duty_cycle(struct rc_dev * dev,u32 duty_cycle)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 
gpio_ir_tx_set_carrier(struct rc_dev * dev,u32 carrier)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 
gpio_ir_tx(struct rc_dev * dev,unsigned int * txbuf,unsigned int count)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 > 0)
91 				udelay(delta);
92 		} else {
93 			// pulse
94 			ktime_t last = ktime_add_us(edge, txbuf[i]);
95 
96 			while (ktime_before(ktime_get(), last)) {
97 				gpiod_set_value(gpio_ir->gpio, 1);
98 				edge = ktime_add_ns(edge, pulse);
99 				delta = ktime_to_ns(ktime_sub(edge,
100 							      ktime_get()));
101 				if (delta > 0)
102 					ndelay(delta);
103 				gpiod_set_value(gpio_ir->gpio, 0);
104 				edge = ktime_add_ns(edge, space);
105 				delta = ktime_to_ns(ktime_sub(edge,
106 							      ktime_get()));
107 				if (delta > 0)
108 					ndelay(delta);
109 			}
110 
111 			edge = last;
112 		}
113 	}
114 
115 	spin_unlock_irqrestore(&gpio_ir->lock, flags);
116 
117 	return count;
118 }
119 
gpio_ir_tx_probe(struct platform_device * pdev)120 static int gpio_ir_tx_probe(struct platform_device *pdev)
121 {
122 	struct gpio_ir *gpio_ir;
123 	struct rc_dev *rcdev;
124 	int rc;
125 
126 	gpio_ir = devm_kmalloc(&pdev->dev, sizeof(*gpio_ir), GFP_KERNEL);
127 	if (!gpio_ir)
128 		return -ENOMEM;
129 
130 	rcdev = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW_TX);
131 	if (!rcdev)
132 		return -ENOMEM;
133 
134 	gpio_ir->gpio = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_LOW);
135 	if (IS_ERR(gpio_ir->gpio)) {
136 		if (PTR_ERR(gpio_ir->gpio) != -EPROBE_DEFER)
137 			dev_err(&pdev->dev, "Failed to get gpio (%ld)\n",
138 				PTR_ERR(gpio_ir->gpio));
139 		return PTR_ERR(gpio_ir->gpio);
140 	}
141 
142 	rcdev->priv = gpio_ir;
143 	rcdev->driver_name = DRIVER_NAME;
144 	rcdev->device_name = DEVICE_NAME;
145 	rcdev->tx_ir = gpio_ir_tx;
146 	rcdev->s_tx_duty_cycle = gpio_ir_tx_set_duty_cycle;
147 	rcdev->s_tx_carrier = gpio_ir_tx_set_carrier;
148 
149 	gpio_ir->carrier = 38000;
150 	gpio_ir->duty_cycle = 50;
151 	spin_lock_init(&gpio_ir->lock);
152 
153 	rc = devm_rc_register_device(&pdev->dev, rcdev);
154 	if (rc < 0)
155 		dev_err(&pdev->dev, "failed to register rc device\n");
156 
157 	return rc;
158 }
159 
160 static struct platform_driver gpio_ir_tx_driver = {
161 	.probe	= gpio_ir_tx_probe,
162 	.driver = {
163 		.name	= DRIVER_NAME,
164 		.of_match_table = of_match_ptr(gpio_ir_tx_of_match),
165 	},
166 };
167 module_platform_driver(gpio_ir_tx_driver);
168 
169 MODULE_DESCRIPTION("GPIO IR Bit Banging Transmitter");
170 MODULE_AUTHOR("Sean Young <sean@mess.org>");
171 MODULE_LICENSE("GPL");
172