forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlc5971.c
352 lines (295 loc) · 11.6 KB
/
tlc5971.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Copyright (c) 2022 Esco Medical ApS
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT ti_tlc5971
#include <zephyr/kernel.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/led_strip.h>
#include <zephyr/drivers/led_strip/tlc5971.h>
#include <zephyr/dt-bindings/led/led.h>
#include <zephyr/sys/util.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(tlc5971, CONFIG_LED_STRIP_LOG_LEVEL);
struct tlc5971_config {
struct spi_dt_spec bus;
const uint8_t *color_mapping;
uint8_t num_pixels;
uint8_t num_colors;
};
struct tlc5971_data {
uint8_t *data_buffer;
uint8_t gbc_color_1;
uint8_t gbc_color_2;
uint8_t gbc_color_3;
uint8_t control_data;
};
/** SPI operation word constant, SPI mode 0, CPOL = 0, CPHA = 0 */
#define TLC5971_SPI_OPERATION (SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | SPI_WORD_SET(8))
/** Number of supported colors */
#define TLC5971_NUMBER_OF_COLORS 3
/**
* @brief Number of RGB pixels per TLC5791 device
*
* The TLC5971 has 4x RGB outputs per device, where each RGB group constitues a pixel from this
* drivers point of view.
*/
#define TLC5971_PIXELS_PER_DEVICE 4
/** Length in bytes of data packet per TLC5791 device */
#define TLC5971_PACKET_LEN 28
/** write command for writing control data and GS data to internal registers */
#define TLC5971_WRITE_COMMAND 0x25
/** GS reference clock select bit in FC data (0 = internal oscillator clock, 1 = SCKI clock). */
#define TLC5971_BYTE27_CTRL_BIT_EXTGCK BIT(0)
/** GS reference clock edge select bit for OUTXn on-off timing control in FC data */
#define TLC5971_BYTE27_CTRL_BIT_OUTTMG BIT(1)
/** Constant-current output enable bit in FC data (0 = output control enabled, 1 = blank). */
#define TLC5971_BYTE26_CTRL_BIT_BLANK BIT(5)
/** Auto display repeat mode enable bit in FC data (0 = disabled, 1 = enabled). */
#define TLC5971_BYTE26_CTRL_BIT_DSPRPT BIT(6)
/** Display timing reset mode enable bit in FC data (0 = disabled, 1 = enabled). */
#define TLC5971_BYTE26_CTRL_BIT_TMGRST BIT(7)
/** Bit mask for write cmd in data byte 27 */
#define TLC5971_BYTE27_WRITE_CMD_MASK GENMASK(7, 2)
/** Bit mask for control bits in data byte 27 */
#define TLC5971_BYTE27_CTRL_MASK GENMASK(1, 0)
/** Bit mask for control bits in data byte 26 */
#define TLC5971_BYTE26_CTRL_MASK GENMASK(7, 5)
/** Bit mask for global brightness control for color 1 in data byte 26, upper 5 bits of GBC */
#define TLC5971_BYTE26_GBC1_MASK GENMASK(4, 0)
/** Bit mask for global brightness control for color 1 in data byte 25, lower 2 bits of GBC */
#define TLC5971_BYTE25_GBC1_MASK GENMASK(7, 6)
/** Bit mask for global brightness control for color 2 in data byte 25, upper 6 bits of GBC */
#define TLC5971_BYTE25_GBC2_MASK GENMASK(5, 0)
/** Bit mask for global brightness control for color 2 in data byte 24, lower 1 bits of GBC */
#define TLC5971_BYTE24_GBC2_MASK BIT(7)
/** Bit mask for global brightness control for color 3 in data byte 24, all 7 bits of GBC */
#define TLC5971_BYTE24_GBC3_MASK GENMASK(6, 0)
/**
* @brief create data byte 27 from control data
*
* @param control_data control bits
* @return uint8_t the serialized data byte 27
*/
static inline uint8_t tlc5971_data_byte27(uint8_t control_data)
{
return FIELD_PREP(TLC5971_BYTE27_WRITE_CMD_MASK, TLC5971_WRITE_COMMAND) |
FIELD_PREP(TLC5971_BYTE27_CTRL_MASK, control_data);
}
/**
* @brief create data byte 26 from control data and color 1 GBC
*
* @param control_data control bits
* @param gbc_color_1 global brightness control for color 1 LEDs
* @return uint8_t the serialized data byte 26
*/
static inline uint8_t tlc5971_data_byte26(uint8_t control_data, uint8_t gbc_color_1)
{
return FIELD_PREP(TLC5971_BYTE26_CTRL_MASK, control_data) |
FIELD_PREP(TLC5971_BYTE26_GBC1_MASK, (gbc_color_1 >> 2));
}
/**
* @brief create data byte 25 from color 1 and 2 GBC
*
* @param gbc_color_1 global brightness control for color 1 LEDs
* @param gbc_color_2 global brightness control for color 2 LEDs
* @return uint8_t the serialized data byte 25
*/
static inline uint8_t tlc5971_data_byte25(uint8_t gbc_color_1, uint8_t gbc_color_2)
{
return FIELD_PREP(TLC5971_BYTE25_GBC1_MASK, gbc_color_1) |
FIELD_PREP(TLC5971_BYTE25_GBC2_MASK, (gbc_color_2 >> 1));
}
/**
* @brief create data byte 24 from color 2 and 3 GBC
*
* @param gbc_color_2 global brightness control for color 2 LEDs
* @param gbc_color_3 global brightness control for color 3 LEDs
* @return uint8_t the serialized data byte 24
*/
static inline uint8_t tlc5971_data_byte24(uint8_t gbc_color_2, uint8_t gbc_color_3)
{
return FIELD_PREP(TLC5971_BYTE24_GBC2_MASK, gbc_color_2) |
FIELD_PREP(TLC5971_BYTE24_GBC3_MASK, gbc_color_3);
}
/**
* @brief map user colors to tlc5971 color order
*
* @param color_id color id from color mapping
* @param pixel_data rgb data to be mapped
* @return the mapped color value
*/
static uint8_t tlc5971_map_color(int color_id, const struct led_rgb *pixel_data)
{
uint8_t temp = 0;
switch (color_id) {
case LED_COLOR_ID_RED:
temp = pixel_data->r;
break;
case LED_COLOR_ID_GREEN:
temp = pixel_data->g;
break;
case LED_COLOR_ID_BLUE:
temp = pixel_data->b;
break;
default:
temp = 0;
break;
}
return temp;
}
/**
* @brief serialize control data and pixel data for device daisy chain
*
* the serializer only supports "full" devices, meaning each device is expected
* to be mounted with all 4 LEDs.
*
* @param dev device pointer
* @param pixels pixel RGB data for daisy chain
* @param num_pixels number of pixels in daisy chain
*/
static void tlc5971_fill_data_buffer(const struct device *dev, struct led_rgb *pixels,
size_t num_pixels)
{
const struct tlc5971_config *cfg = dev->config;
struct tlc5971_data *data = dev->data;
uint8_t *data_buffer = data->data_buffer;
int count = 0;
/*
* tlc5971 device order is reversed as the rgb data for the last device in the daisy chain
* should be transmitted first.
*/
for (int device = (num_pixels / TLC5971_PIXELS_PER_DEVICE) - 1; device >= 0; device--) {
/*
* The SPI frame format expects a BGR color order for the global brightness control
* values, but since the led_strip API allows custom color mappings, we simply use
* color_x terms to keep things generic.
*/
data_buffer[count++] = tlc5971_data_byte27(data->control_data);
data_buffer[count++] = tlc5971_data_byte26(data->control_data, data->gbc_color_1);
data_buffer[count++] = tlc5971_data_byte25(data->gbc_color_1, data->gbc_color_2);
data_buffer[count++] = tlc5971_data_byte24(data->gbc_color_2, data->gbc_color_3);
for (int pixel = (TLC5971_PIXELS_PER_DEVICE - 1); pixel >= 0; pixel--) {
/* data is "reversed" so RGB0 comes last, i.e at byte 0 */
const struct led_rgb *pixel_data =
&pixels[(device * TLC5971_PIXELS_PER_DEVICE) + pixel];
/*
* Convert pixel data into SPI frames, mapping user colors to tlc5971
* data frame color order (BGR).
*/
for (int color = 0; color < cfg->num_colors; color++) {
uint8_t temp =
tlc5971_map_color(cfg->color_mapping[color], pixel_data);
/*
* The tlc5971 rgb values are 16 bit but zephyr's rgb values are
* 8 bit. Simply upscale to 16 bit by using the 8 bit value for both
* LSB and MSB of the 16 bit word.
*/
data_buffer[count++] = temp;
data_buffer[count++] = temp;
}
}
}
}
static int tlc5971_transmit_data(const struct device *dev, size_t num_pixels)
{
const struct tlc5971_config *cfg = dev->config;
struct tlc5971_data *data = dev->data;
struct spi_buf buf = {
.buf = data->data_buffer,
.len = (num_pixels / TLC5971_PIXELS_PER_DEVICE) * TLC5971_PACKET_LEN,
};
const struct spi_buf_set tx = {
.buffers = &buf,
.count = 1,
};
return spi_write_dt(&cfg->bus, &tx);
}
static int tlc5971_update_rgb(const struct device *dev, struct led_rgb *pixels, size_t num_pixels)
{
tlc5971_fill_data_buffer(dev, pixels, num_pixels);
return tlc5971_transmit_data(dev, num_pixels);
}
static size_t tlc5971_length(const struct device *dev)
{
const struct tlc5971_config *cfg = dev->config;
return (size_t)cfg->num_pixels;
}
int tlc5971_set_global_brightness(const struct device *dev, struct led_rgb pixel)
{
const struct tlc5971_config *cfg = dev->config;
struct tlc5971_data *data = dev->data;
int res = -EINVAL;
if ((pixel.r <= TLC5971_GLOBAL_BRIGHTNESS_CONTROL_MAX) &&
(pixel.g <= TLC5971_GLOBAL_BRIGHTNESS_CONTROL_MAX) &&
(pixel.b <= TLC5971_GLOBAL_BRIGHTNESS_CONTROL_MAX)) {
data->gbc_color_1 = tlc5971_map_color(cfg->color_mapping[0], &pixel);
data->gbc_color_2 = tlc5971_map_color(cfg->color_mapping[1], &pixel);
data->gbc_color_3 = tlc5971_map_color(cfg->color_mapping[2], &pixel);
res = 0;
}
return res;
}
static int tlc5971_init(const struct device *dev)
{
const struct tlc5971_config *cfg = dev->config;
struct tlc5971_data *data = dev->data;
if (!spi_is_ready_dt(&cfg->bus)) {
LOG_ERR("%s: SPI device %s not ready", dev->name, cfg->bus.bus->name);
return -ENODEV;
}
if ((cfg->num_pixels % TLC5971_PIXELS_PER_DEVICE) != 0) {
LOG_ERR("%s: chain length must be multiple of 4", dev->name);
return -EINVAL;
}
if (cfg->num_colors != TLC5971_NUMBER_OF_COLORS) {
LOG_ERR("%s: the tlc5971 only supports %i colors", dev->name,
TLC5971_NUMBER_OF_COLORS);
return -EINVAL;
}
for (int i = 0; i < cfg->num_colors; i++) {
switch (cfg->color_mapping[i]) {
case LED_COLOR_ID_RED:
case LED_COLOR_ID_GREEN:
case LED_COLOR_ID_BLUE:
break;
default:
LOG_ERR("%s: invalid color mapping", dev->name);
return -EINVAL;
}
}
/*
* set up sane defaults for control data.
* unblanks leds, enables auto display repeat, enables timing resetm uses internal clock for
* PWM generation and sets the GS reference clock edge select to rising edge
*/
data->control_data = TLC5971_BYTE27_CTRL_BIT_OUTTMG | TLC5971_BYTE26_CTRL_BIT_DSPRPT |
TLC5971_BYTE26_CTRL_BIT_TMGRST;
return 0;
}
static DEVICE_API(led_strip, tlc5971_api) = {
.update_rgb = tlc5971_update_rgb,
.length = tlc5971_length,
};
#define TLC5971_DATA_BUFFER_LENGTH(inst) \
(DT_INST_PROP(inst, chain_length) / TLC5971_PIXELS_PER_DEVICE) * TLC5971_PACKET_LEN
#define TLC5971_DEVICE(inst) \
static const uint8_t tlc5971_##inst##_color_mapping[] = DT_INST_PROP(inst, color_mapping); \
static const struct tlc5971_config tlc5971_##inst##_config = { \
.bus = SPI_DT_SPEC_INST_GET(inst, TLC5971_SPI_OPERATION, 0), \
.num_pixels = DT_INST_PROP(inst, chain_length), \
.num_colors = DT_INST_PROP_LEN(inst, color_mapping), \
.color_mapping = tlc5971_##inst##_color_mapping, \
}; \
static uint8_t tlc5971_##inst##_data_buffer[TLC5971_DATA_BUFFER_LENGTH(inst)]; \
static struct tlc5971_data tlc5971_##inst##_data = { \
.data_buffer = tlc5971_##inst##_data_buffer, \
.gbc_color_1 = TLC5971_GLOBAL_BRIGHTNESS_CONTROL_MAX, \
.gbc_color_2 = TLC5971_GLOBAL_BRIGHTNESS_CONTROL_MAX, \
.gbc_color_3 = TLC5971_GLOBAL_BRIGHTNESS_CONTROL_MAX, \
}; \
DEVICE_DT_INST_DEFINE(inst, tlc5971_init, NULL, &tlc5971_##inst##_data, \
&tlc5971_##inst##_config, POST_KERNEL, \
CONFIG_LED_STRIP_INIT_PRIORITY, &tlc5971_api);
DT_INST_FOREACH_STATUS_OKAY(TLC5971_DEVICE)