2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3 * Author: Andi Shyti <andi.shyti@samsung.com>
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.
9 * SPI driven IR LED device driver
12 #include <linux/delay.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>
21 #define IR_SPI_DRIVER_NAME "ir-spi"
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
31 #define IR_SPI_DEFAULT_FREQUENCY 38000
32 #define IR_SPI_BIT_PER_WORD 8
33 #define IR_SPI_MAX_BUFSIZE 4096
40 u16 tx_buf[IR_SPI_MAX_BUFSIZE];
45 struct spi_device *spi;
46 struct regulator *regulator;
49 static int ir_spi_tx(struct rc_dev *dev,
50 unsigned int *buffer, unsigned int count)
55 struct ir_spi_data *idata = dev->priv;
56 struct spi_transfer xfer;
58 /* convert the pulse/space signal to raw binary signal */
59 for (i = 0; i < count; i++) {
64 periods = DIV_ROUND_CLOSEST(buffer[i] * idata->freq, 1000000);
66 if (len + periods >= IR_SPI_MAX_BUFSIZE)
70 * the first value in buffer is a pulse, so that 0, 2, 4, ...
71 * contain a pulse duration. On the contrary, 1, 3, 5, ...
72 * contain a space duration.
74 val = (i % 2) ? idata->space : idata->pulse;
75 for (j = 0; j < periods; j++)
76 idata->tx_buf[len++] = val;
79 memset(&xfer, 0, sizeof(xfer));
81 xfer.speed_hz = idata->freq * 16;
82 xfer.len = len * sizeof(*idata->tx_buf);
83 xfer.tx_buf = idata->tx_buf;
85 ret = regulator_enable(idata->regulator);
89 ret = spi_sync_transfer(idata->spi, &xfer, 1);
91 dev_err(&idata->spi->dev, "unable to deliver the signal\n");
93 regulator_disable(idata->regulator);
95 return ret ? ret : count;
98 static int ir_spi_set_tx_carrier(struct rc_dev *dev, u32 carrier)
100 struct ir_spi_data *idata = dev->priv;
105 idata->freq = carrier;
110 static int ir_spi_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
112 struct ir_spi_data *idata = dev->priv;
114 if (duty_cycle >= 90)
115 idata->pulse = IR_SPI_PULSE_DC_90;
116 else if (duty_cycle >= 80)
117 idata->pulse = IR_SPI_PULSE_DC_80;
118 else if (duty_cycle >= 75)
119 idata->pulse = IR_SPI_PULSE_DC_75;
120 else if (duty_cycle >= 70)
121 idata->pulse = IR_SPI_PULSE_DC_70;
122 else if (duty_cycle >= 60)
123 idata->pulse = IR_SPI_PULSE_DC_60;
125 idata->pulse = IR_SPI_PULSE_DC_50;
127 if (idata->negated) {
128 idata->pulse = ~idata->pulse;
129 idata->space = 0xffff;
137 static int ir_spi_probe(struct spi_device *spi)
141 struct ir_spi_data *idata;
143 idata = devm_kzalloc(&spi->dev, sizeof(*idata), GFP_KERNEL);
147 idata->regulator = devm_regulator_get(&spi->dev, "irda_regulator");
148 if (IS_ERR(idata->regulator))
149 return PTR_ERR(idata->regulator);
151 idata->rc = devm_rc_allocate_device(&spi->dev, RC_DRIVER_IR_RAW_TX);
155 idata->rc->tx_ir = ir_spi_tx;
156 idata->rc->s_tx_carrier = ir_spi_set_tx_carrier;
157 idata->rc->s_tx_duty_cycle = ir_spi_set_duty_cycle;
158 idata->rc->driver_name = IR_SPI_DRIVER_NAME;
159 idata->rc->priv = idata;
162 idata->negated = of_property_read_bool(spi->dev.of_node,
164 ret = of_property_read_u8(spi->dev.of_node, "duty-cycle", &dc);
168 /* ir_spi_set_duty_cycle cannot fail,
169 * it returns int to be compatible with the
170 * rc->s_tx_duty_cycle function
172 ir_spi_set_duty_cycle(idata->rc, dc);
174 idata->freq = IR_SPI_DEFAULT_FREQUENCY;
176 return devm_rc_register_device(&spi->dev, idata->rc);
179 static int ir_spi_remove(struct spi_device *spi)
184 static const struct of_device_id ir_spi_of_match[] = {
185 { .compatible = "ir-spi-led" },
189 static struct spi_driver ir_spi_driver = {
190 .probe = ir_spi_probe,
191 .remove = ir_spi_remove,
193 .name = IR_SPI_DRIVER_NAME,
194 .of_match_table = ir_spi_of_match,
198 module_spi_driver(ir_spi_driver);
200 MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>");
201 MODULE_DESCRIPTION("SPI IR LED");
202 MODULE_LICENSE("GPL v2");