blob: c58f2d38a4582ec3ab126bd471a1861b7e42f215 [file] [log] [blame]
Andi Shyti8ea636d2017-12-12 02:47:20 -05001// SPDX-License-Identifier: GPL-2.0
2// SPI driven IR LED device driver
3//
4// Copyright (c) 2016 Samsung Electronics Co., Ltd.
Andi Shytic42c3e62018-04-13 09:48:29 -04005// Copyright (c) Andi Shyti <andi@etezian.org>
Andi Shytife052da2016-12-16 04:12:18 -02006
7#include <linux/delay.h>
8#include <linux/fs.h>
9#include <linux/module.h>
10#include <linux/mutex.h>
11#include <linux/of_gpio.h>
12#include <linux/regulator/consumer.h>
13#include <linux/spi/spi.h>
14#include <media/rc-core.h>
15
16#define IR_SPI_DRIVER_NAME "ir-spi"
17
Andi Shytife052da2016-12-16 04:12:18 -020018#define IR_SPI_DEFAULT_FREQUENCY 38000
Andi Shytife052da2016-12-16 04:12:18 -020019#define IR_SPI_MAX_BUFSIZE 4096
20
21struct ir_spi_data {
22 u32 freq;
Andi Shytife052da2016-12-16 04:12:18 -020023 bool negated;
24
25 u16 tx_buf[IR_SPI_MAX_BUFSIZE];
26 u16 pulse;
27 u16 space;
28
29 struct rc_dev *rc;
30 struct spi_device *spi;
31 struct regulator *regulator;
32};
33
34static int ir_spi_tx(struct rc_dev *dev,
35 unsigned int *buffer, unsigned int count)
36{
37 int i;
38 int ret;
39 unsigned int len = 0;
40 struct ir_spi_data *idata = dev->priv;
41 struct spi_transfer xfer;
42
43 /* convert the pulse/space signal to raw binary signal */
44 for (i = 0; i < count; i++) {
Anton Blanchardcc20ba42017-05-06 22:00:11 -030045 unsigned int periods;
Andi Shytife052da2016-12-16 04:12:18 -020046 int j;
Andi Shyti956bd182017-03-27 10:34:35 -030047 u16 val;
Andi Shytife052da2016-12-16 04:12:18 -020048
Anton Blanchardcc20ba42017-05-06 22:00:11 -030049 periods = DIV_ROUND_CLOSEST(buffer[i] * idata->freq, 1000000);
50
51 if (len + periods >= IR_SPI_MAX_BUFSIZE)
Andi Shytife052da2016-12-16 04:12:18 -020052 return -EINVAL;
53
54 /*
55 * the first value in buffer is a pulse, so that 0, 2, 4, ...
56 * contain a pulse duration. On the contrary, 1, 3, 5, ...
57 * contain a space duration.
58 */
59 val = (i % 2) ? idata->space : idata->pulse;
Anton Blanchardcc20ba42017-05-06 22:00:11 -030060 for (j = 0; j < periods; j++)
Andi Shytife052da2016-12-16 04:12:18 -020061 idata->tx_buf[len++] = val;
62 }
63
64 memset(&xfer, 0, sizeof(xfer));
65
Anton Blanchardcc20ba42017-05-06 22:00:11 -030066 xfer.speed_hz = idata->freq * 16;
Andi Shytife052da2016-12-16 04:12:18 -020067 xfer.len = len * sizeof(*idata->tx_buf);
68 xfer.tx_buf = idata->tx_buf;
69
70 ret = regulator_enable(idata->regulator);
71 if (ret)
72 return ret;
73
74 ret = spi_sync_transfer(idata->spi, &xfer, 1);
75 if (ret)
76 dev_err(&idata->spi->dev, "unable to deliver the signal\n");
77
78 regulator_disable(idata->regulator);
79
80 return ret ? ret : count;
81}
82
83static int ir_spi_set_tx_carrier(struct rc_dev *dev, u32 carrier)
84{
85 struct ir_spi_data *idata = dev->priv;
86
87 if (!carrier)
88 return -EINVAL;
89
90 idata->freq = carrier;
91
92 return 0;
93}
94
95static int ir_spi_set_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
96{
97 struct ir_spi_data *idata = dev->priv;
Sean Young069edf82018-01-27 09:05:37 -050098 int bits = (duty_cycle * 15) / 100;
Andi Shytife052da2016-12-16 04:12:18 -020099
Sean Young069edf82018-01-27 09:05:37 -0500100 idata->pulse = GENMASK(bits, 0);
Andi Shytife052da2016-12-16 04:12:18 -0200101
102 if (idata->negated) {
103 idata->pulse = ~idata->pulse;
104 idata->space = 0xffff;
105 } else {
106 idata->space = 0;
107 }
108
109 return 0;
110}
111
112static int ir_spi_probe(struct spi_device *spi)
113{
114 int ret;
115 u8 dc;
116 struct ir_spi_data *idata;
117
118 idata = devm_kzalloc(&spi->dev, sizeof(*idata), GFP_KERNEL);
119 if (!idata)
120 return -ENOMEM;
121
122 idata->regulator = devm_regulator_get(&spi->dev, "irda_regulator");
123 if (IS_ERR(idata->regulator))
124 return PTR_ERR(idata->regulator);
125
126 idata->rc = devm_rc_allocate_device(&spi->dev, RC_DRIVER_IR_RAW_TX);
127 if (!idata->rc)
128 return -ENOMEM;
129
130 idata->rc->tx_ir = ir_spi_tx;
131 idata->rc->s_tx_carrier = ir_spi_set_tx_carrier;
132 idata->rc->s_tx_duty_cycle = ir_spi_set_duty_cycle;
Sean Young518f4b22017-07-01 12:13:19 -0400133 idata->rc->device_name = "IR SPI";
Andi Shytife052da2016-12-16 04:12:18 -0200134 idata->rc->driver_name = IR_SPI_DRIVER_NAME;
135 idata->rc->priv = idata;
136 idata->spi = spi;
137
138 idata->negated = of_property_read_bool(spi->dev.of_node,
139 "led-active-low");
140 ret = of_property_read_u8(spi->dev.of_node, "duty-cycle", &dc);
141 if (ret)
142 dc = 50;
143
144 /* ir_spi_set_duty_cycle cannot fail,
145 * it returns int to be compatible with the
146 * rc->s_tx_duty_cycle function
147 */
148 ir_spi_set_duty_cycle(idata->rc, dc);
149
150 idata->freq = IR_SPI_DEFAULT_FREQUENCY;
151
152 return devm_rc_register_device(&spi->dev, idata->rc);
153}
154
155static int ir_spi_remove(struct spi_device *spi)
156{
157 return 0;
158}
159
160static const struct of_device_id ir_spi_of_match[] = {
161 { .compatible = "ir-spi-led" },
162 {},
163};
Daniel Gomez24e4cf72019-04-22 15:10:20 -0400164MODULE_DEVICE_TABLE(of, ir_spi_of_match);
Andi Shytife052da2016-12-16 04:12:18 -0200165
166static struct spi_driver ir_spi_driver = {
167 .probe = ir_spi_probe,
168 .remove = ir_spi_remove,
169 .driver = {
170 .name = IR_SPI_DRIVER_NAME,
171 .of_match_table = ir_spi_of_match,
172 },
173};
174
175module_spi_driver(ir_spi_driver);
176
Andi Shytic42c3e62018-04-13 09:48:29 -0400177MODULE_AUTHOR("Andi Shyti <andi@etezian.org>");
Andi Shytife052da2016-12-16 04:12:18 -0200178MODULE_DESCRIPTION("SPI IR LED");
179MODULE_LICENSE("GPL v2");