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
++) {
61 u16 val
= ((i
+ 1) % 2) ? idata
->pulse
: idata
->space
;
63 if (len
+ buffer
[i
] >= IR_SPI_MAX_BUFSIZE
)
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.
71 val
= (i
% 2) ? idata
->space
: idata
->pulse
;
72 for (j
= 0; j
< buffer
[i
]; j
++)
73 idata
->tx_buf
[len
++] = val
;
76 memset(&xfer
, 0, sizeof(xfer
));
78 xfer
.speed_hz
= idata
->freq
;
79 xfer
.len
= len
* sizeof(*idata
->tx_buf
);
80 xfer
.tx_buf
= idata
->tx_buf
;
82 ret
= regulator_enable(idata
->regulator
);
86 ret
= spi_sync_transfer(idata
->spi
, &xfer
, 1);
88 dev_err(&idata
->spi
->dev
, "unable to deliver the signal\n");
90 regulator_disable(idata
->regulator
);
92 return ret
? ret
: count
;
95 static int ir_spi_set_tx_carrier(struct rc_dev
*dev
, u32 carrier
)
97 struct ir_spi_data
*idata
= dev
->priv
;
102 idata
->freq
= carrier
;
107 static int ir_spi_set_duty_cycle(struct rc_dev
*dev
, u32 duty_cycle
)
109 struct ir_spi_data
*idata
= dev
->priv
;
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
;
122 idata
->pulse
= IR_SPI_PULSE_DC_50
;
124 if (idata
->negated
) {
125 idata
->pulse
= ~idata
->pulse
;
126 idata
->space
= 0xffff;
134 static int ir_spi_probe(struct spi_device
*spi
)
138 struct ir_spi_data
*idata
;
140 idata
= devm_kzalloc(&spi
->dev
, sizeof(*idata
), GFP_KERNEL
);
144 idata
->regulator
= devm_regulator_get(&spi
->dev
, "irda_regulator");
145 if (IS_ERR(idata
->regulator
))
146 return PTR_ERR(idata
->regulator
);
148 idata
->rc
= devm_rc_allocate_device(&spi
->dev
, RC_DRIVER_IR_RAW_TX
);
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
;
159 idata
->negated
= of_property_read_bool(spi
->dev
.of_node
,
161 ret
= of_property_read_u8(spi
->dev
.of_node
, "duty-cycle", &dc
);
165 /* ir_spi_set_duty_cycle cannot fail,
166 * it returns int to be compatible with the
167 * rc->s_tx_duty_cycle function
169 ir_spi_set_duty_cycle(idata
->rc
, dc
);
171 idata
->freq
= IR_SPI_DEFAULT_FREQUENCY
;
173 return devm_rc_register_device(&spi
->dev
, idata
->rc
);
176 static int ir_spi_remove(struct spi_device
*spi
)
181 static const struct of_device_id ir_spi_of_match
[] = {
182 { .compatible
= "ir-spi-led" },
186 static struct spi_driver ir_spi_driver
= {
187 .probe
= ir_spi_probe
,
188 .remove
= ir_spi_remove
,
190 .name
= IR_SPI_DRIVER_NAME
,
191 .of_match_table
= ir_spi_of_match
,
195 module_spi_driver(ir_spi_driver
);
197 MODULE_AUTHOR("Andi Shyti <andi.shyti@samsung.com>");
198 MODULE_DESCRIPTION("SPI IR LED");
199 MODULE_LICENSE("GPL v2");