xref: /linux/drivers/media/rc/ir-spi.c (revision fcc8487d477a3452a1d0ccbdd4c5e0e1e3cb8bed)
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  * Author: Andi Shyti <andi.shyti@samsung.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * SPI driven IR LED device driver
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/fs.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/of_gpio.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/spi/spi.h>
19 #include <media/rc-core.h>
20 
21 #define IR_SPI_DRIVER_NAME		"ir-spi"
22 
23 /* pulse value for different duty cycles */
24 #define IR_SPI_PULSE_DC_50		0xff00
25 #define IR_SPI_PULSE_DC_60		0xfc00
26 #define IR_SPI_PULSE_DC_70		0xf800
27 #define IR_SPI_PULSE_DC_75		0xf000
28 #define IR_SPI_PULSE_DC_80		0xc000
29 #define IR_SPI_PULSE_DC_90		0x8000
30 
31 #define IR_SPI_DEFAULT_FREQUENCY	38000
32 #define IR_SPI_BIT_PER_WORD		    8
33 #define IR_SPI_MAX_BUFSIZE		 4096
34 
35 struct ir_spi_data {
36 	u32 freq;
37 	u8 duty_cycle;
38 	bool negated;
39 
40 	u16 tx_buf[IR_SPI_MAX_BUFSIZE];
41 	u16 pulse;
42 	u16 space;
43 
44 	struct rc_dev *rc;
45 	struct spi_device *spi;
46 	struct regulator *regulator;
47 };
48 
49 static int ir_spi_tx(struct rc_dev *dev,
50 		     unsigned int *buffer, unsigned int count)
51 {
52 	int i;
53 	int ret;
54 	unsigned int len = 0;
55 	struct ir_spi_data *idata = dev->priv;
56 	struct spi_transfer xfer;
57 
58 	/* convert the pulse/space signal to raw binary signal */
59 	for (i = 0; i < count; i++) {
60 		int j;
61 		u16 val = ((i + 1) % 2) ? idata->pulse : idata->space;
62 
63 		if (len + buffer[i] >= IR_SPI_MAX_BUFSIZE)
64 			return -EINVAL;
65 
66 		/*
67 		 * the first value in buffer is a pulse, so that 0, 2, 4, ...
68 		 * contain a pulse duration. On the contrary, 1, 3, 5, ...
69 		 * contain a space duration.
70 		 */
71 		val = (i % 2) ? idata->space : idata->pulse;
72 		for (j = 0; j < buffer[i]; j++)
73 			idata->tx_buf[len++] = val;
74 	}
75 
76 	memset(&xfer, 0, sizeof(xfer));
77 
78 	xfer.speed_hz = idata->freq;
79 	xfer.len = len * sizeof(*idata->tx_buf);
80 	xfer.tx_buf = idata->tx_buf;
81 
82 	ret = regulator_enable(idata->regulator);
83 	if (ret)
84 		return ret;
85 
86 	ret = spi_sync_transfer(idata->spi, &xfer, 1);
87 	if (ret)
88 		dev_err(&idata->spi->dev, "unable to deliver the signal\n");
89 
90 	regulator_disable(idata->regulator);
91 
92 	return ret ? ret : count;
93 }
94 
95 static int ir_spi_set_tx_carrier(struct rc_dev *dev, u32 carrier)
96 {
97 	struct ir_spi_data *idata = dev->priv;
98 
99 	if (!carrier)
100 		return -EINVAL;
101 
102 	idata->freq = carrier;
103 
104 	return 0;
105 }
106 
107 static int ir_spi_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
108 {
109 	struct ir_spi_data *idata = dev->priv;
110 
111 	if (duty_cycle >= 90)
112 		idata->pulse = IR_SPI_PULSE_DC_90;
113 	else if (duty_cycle >= 80)
114 		idata->pulse = IR_SPI_PULSE_DC_80;
115 	else if (duty_cycle >= 75)
116 		idata->pulse = IR_SPI_PULSE_DC_75;
117 	else if (duty_cycle >= 70)
118 		idata->pulse = IR_SPI_PULSE_DC_70;
119 	else if (duty_cycle >= 60)
120 		idata->pulse = IR_SPI_PULSE_DC_60;
121 	else
122 		idata->pulse = IR_SPI_PULSE_DC_50;
123 
124 	if (idata->negated) {
125 		idata->pulse = ~idata->pulse;
126 		idata->space = 0xffff;
127 	} else {
128 		idata->space = 0;
129 	}
130 
131 	return 0;
132 }
133 
134 static int ir_spi_probe(struct spi_device *spi)
135 {
136 	int ret;
137 	u8 dc;
138 	struct ir_spi_data *idata;
139 
140 	idata = devm_kzalloc(&spi->dev, sizeof(*idata), GFP_KERNEL);
141 	if (!idata)
142 		return -ENOMEM;
143 
144 	idata->regulator = devm_regulator_get(&spi->dev, "irda_regulator");
145 	if (IS_ERR(idata->regulator))
146 		return PTR_ERR(idata->regulator);
147 
148 	idata->rc = devm_rc_allocate_device(&spi->dev, RC_DRIVER_IR_RAW_TX);
149 	if (!idata->rc)
150 		return -ENOMEM;
151 
152 	idata->rc->tx_ir           = ir_spi_tx;
153 	idata->rc->s_tx_carrier    = ir_spi_set_tx_carrier;
154 	idata->rc->s_tx_duty_cycle = ir_spi_set_duty_cycle;
155 	idata->rc->driver_name     = IR_SPI_DRIVER_NAME;
156 	idata->rc->priv            = idata;
157 	idata->spi                 = spi;
158 
159 	idata->negated = of_property_read_bool(spi->dev.of_node,
160 							"led-active-low");
161 	ret = of_property_read_u8(spi->dev.of_node, "duty-cycle", &dc);
162 	if (ret)
163 		dc = 50;
164 
165 	/* ir_spi_set_duty_cycle cannot fail,
166 	 * it returns int to be compatible with the
167 	 * rc->s_tx_duty_cycle function
168 	 */
169 	ir_spi_set_duty_cycle(idata->rc, dc);
170 
171 	idata->freq = IR_SPI_DEFAULT_FREQUENCY;
172 
173 	return devm_rc_register_device(&spi->dev, idata->rc);
174 }
175 
176 static int ir_spi_remove(struct spi_device *spi)
177 {
178 	return 0;
179 }
180 
181 static const struct of_device_id ir_spi_of_match[] = {
182 	{ .compatible = "ir-spi-led" },
183 	{},
184 };
185 
186 static struct spi_driver ir_spi_driver = {
187 	.probe = ir_spi_probe,
188 	.remove = ir_spi_remove,
189 	.driver = {
190 		.name = IR_SPI_DRIVER_NAME,
191 		.of_match_table = ir_spi_of_match,
192 	},
193 };
194 
195 module_spi_driver(ir_spi_driver);
196 
197 MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>");
198 MODULE_DESCRIPTION("SPI IR LED");
199 MODULE_LICENSE("GPL v2");
200