-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathws2812b-spi-fb.c
367 lines (331 loc) · 8.55 KB
/
ws2812b-spi-fb.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* linux/drivers/video/fb/ws2812-spi-fb.c
*
* (c) Martin Sperl <[email protected]>
*
* Frame buffer code for WS2812B LED strip/Panel using
* SPI-MOSI transfer only encoding clock in MOSI ignoring
* SPI-SCK (just used to push the bits at the correct rate).
*
* Typically setup via a 74HCT125 for level translation to 5V
* where:
* SPI-CS is connected to 1/OE
* SPI-MOSI is connected to 1A
* WS2812-DI is connected to 1Y
* (or any other of the buffers on the 74HCT125)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/spi/spi.h>
#include "rgbled-fb.h"
#define DEVICE_NAME "ws2812b-spi-fb"
/* encoded byte tables
* we present each bit as 3 bits (oversampling by a factor of 3)
* zero is represented as 0b100
* one is represented as 0b110
* so each byte is actually represented as 3 bytes
*
* the following are tables to avoid repeated computations
* and are sent via spi as high, medium, low at 3* required HZ
*/
const char byte2encoding_h[/* (value >> 5) & 0x07 */] = {
0x92,
0x93,
0x9a,
0x9b,
0xd2,
0xd3,
0xda,
0xdb
};
const char byte2encoding_m[/* (value >> 3) & 0x03 */] = {
0x49,
0x4d,
0x69,
0x6d
};
const char byte2encoding_l[/* value & 0x07 */] = {
0x24,
0x26,
0x34,
0x36,
0xa4,
0xa6,
0xb4,
0xb6
};
/* an encoded rgb-pixel */
struct ws2812b_encoding {
u8 h, m, l;
};
struct ws2812b_pixel {
struct ws2812b_encoding g, r, b;
};
/* generic information about this device */
struct ws2812b_device_info {
char *name;
struct rgbled_panel_info *panels;
int clock_speed;
u32 led_current_max_red;
u32 led_current_max_green;
u32 led_current_max_blue;
u32 led_current_base;
};
/* the private data structure for this device */
struct ws2812b_data {
struct spi_device *spi;
struct rgbled_fb *rgbled_fb;
struct ws2812b_pixel *spi_data;
struct spi_message spi_msg;
struct spi_transfer spi_xfer;
};
static const struct of_device_id ws2812b_of_match[];
/* implementation details */
static inline void ws2812b_set_encoded_pixel(struct ws2812b_encoding *enc,
u8 val)
{
enc->h = byte2encoding_h[(val >> 5) & 0x07];
enc->m = byte2encoding_m[(val >> 3) & 0x03];
enc->l = byte2encoding_l[(val >> 0) & 0x07];
}
static void ws2812b_set_pixel_value(struct rgbled_fb *rfb,
struct rgbled_panel_info *panel,
int pixel_num,
struct rgbled_pixel *pix)
{
struct ws2812b_data *bs = rfb->par;
struct ws2812b_pixel *spix = &bs->spi_data[pixel_num];
int r = pix->red * pix->brightness / 255;
int g = pix->green * pix->brightness / 255;
int b = pix->blue * pix->brightness / 255;
/* assign the encoded values */
ws2812b_set_encoded_pixel(&spix->g, g);
ws2812b_set_encoded_pixel(&spix->r, r);
ws2812b_set_encoded_pixel(&spix->b, b);
}
static void ws2812b_finish_work(struct rgbled_fb *rfb)
{
struct ws2812b_data *bs = rfb->par;
/* just issue spi_sync */
spi_sync(bs->spi, &bs->spi_msg);
}
static int ws2812b_probe(struct spi_device *spi)
{
struct ws2812b_data *bs;
int len;
const struct of_device_id *of_id;
const struct ws2812b_device_info *dinfo;
struct rgbled_fb *rfb;
/* get the panels for this panel */
of_id = of_match_device(ws2812b_of_match, &spi->dev);
if (!of_id)
return -EINVAL;
dinfo = (const struct ws2812b_device_info *)of_id->data;
/* allocate our buffer */
bs = devm_kzalloc(&spi->dev, sizeof(*bs), GFP_KERNEL);
if (!bs)
return -ENOMEM;
rfb = rgbled_alloc(&spi->dev, dinfo->name, dinfo->panels);
bs->rgbled_fb = rfb;
if (!rfb)
return -ENOMEM;
if (IS_ERR(rfb))
return PTR_ERR(rfb);
/* set up the spi-message and buffers */
len = rfb->pixel * sizeof(struct ws2812b_pixel)
+ 15;
bs->spi_data = devm_kzalloc(&spi->dev, len, GFP_KERNEL);
if (!bs->spi_data)
return -ENOMEM;
/* setting up SPI */
bs->spi = spi;
spi_message_init(&bs->spi_msg);
bs->spi_xfer.len = len;
bs->spi_xfer.tx_buf = bs->spi_data;
spi_message_add_tail(&bs->spi_xfer, &bs->spi_msg);
/* and estimate the refresh rate */
rfb->deferred_io.delay = max_t(unsigned long, 1,
HZ * len * 8 / spi->max_speed_hz);
/* setting up deferred work */
rfb->set_pixel_value = ws2812b_set_pixel_value;
rfb->finish_work = ws2812b_finish_work;
/* copy the current values */
rfb->led_current_max_red = dinfo->led_current_max_red;
rfb->led_current_max_green = dinfo->led_current_max_green;
rfb->led_current_max_blue = dinfo->led_current_max_blue;
rfb->led_current_base = dinfo->led_current_base;
/* set the reverse pointer */
rfb->par = bs;
/* and register */
return rgbled_register(rfb);
}
/* define the different panel types for the ws2812b chip*/
static struct rgbled_panel_info ws2812b_panels[] = {
{
.compatible = "worldsemi,ws2812b,strip",
.width = 1,
.height = 1,
.flags = RGBLED_FLAG_CHANGE_WHLP,
},
{
.compatible = "adafruit,neopixel,strip,30",
.width = 1,
.height = 1,
.pitch = 30,
.flags = RGBLED_FLAG_CHANGE_WHL,
},
{
.compatible = "adafruit,neopixel,strip,60",
.width = 1,
.height = 1,
.pitch = 60,
.flags = RGBLED_FLAG_CHANGE_WHL,
},
{
.compatible = "adafruit,neopixel,strip,144",
.width = 1,
.height = 1,
.pitch = 144,
.flags = RGBLED_FLAG_CHANGE_WHL,
},
#ifdef VERIFIED_SETTINGS
{
.compatible = "adafruit,neopixel,ring,12",
.pixel = 12,
.width = 6,
.height = 6,
/*
.get_pixel_coords = ws2812b_get_pixel_coordinates_ring12,
.pitch = 112,
*/
},
{
.compatible = "adafruit,neopixel,ring,16",
.pixel = 16,
.width = 8,
.height = 8,
/*
.get_pixel_coords = ws2812b_get_pixel_coordinates_ring16,
.pitch = 112,
*/
},
{
.compatible = "adafruit,neopixel,ring,24",
.pixel = 24,
.width = 10,
.height = 10,
/*
.get_pixel_coords = ws2812b_get_pixel_coordinates_ring24,
.pitch = 112,
*/
},
{
.compatible = "adafruit,neopixel,arc,15",
.pixel = 15,
.width = 8,
.height = 8,
/*
.get_pixel_coords = ws2812b_get_pixel_coordinates_arc15,
.pitch = 112,
*/
},
#endif
{
.compatible = "adafruit,neopixel,matrix,8x8",
.width = 8,
.height = 8,
.get_pixel_coords = rgbled_get_pixel_coords_meander,
.pitch = 112,
.multiple = rgbled_panel_multiple_height,
},
{
.compatible = "adafruit,neopixel,matrix,16x16",
.width = 16,
.height = 16,
.get_pixel_coords = rgbled_get_pixel_coords_meander,
.pitch = 112,
.multiple = rgbled_panel_multiple_height,
},
{
.compatible = "adafruit,neopixel,matrix,32x8",
.width = 32,
.height = 8,
.pixel = 256,
.get_pixel_coords = rgbled_get_pixel_coords_meander,
.layout_yx = true,
.pitch = 112,
.multiple = rgbled_panel_multiple_width,
},
{
.compatible = "adafruit,neopixel,stick,8",
.width = 8,
.height = 1,
.pitch = 156,
.multiple = rgbled_panel_multiple_height,
},
{ }
};
static struct ws2812b_device_info ws2812b_device_info = {
.name = "ws2812b-spi-fb",
.panels = ws2812b_panels,
.clock_speed = 800000,
.led_current_max_red = 17,
.led_current_max_green = 17,
.led_current_max_blue = 17,
.led_current_base = 1,
};
/* define the different panel types for the ws2812b chip*/
static struct rgbled_panel_info ws2812_panels[] = {
{
.compatible = "worldsemi,ws2812,strip",
.width = 1,
.height = 1,
.flags = RGBLED_FLAG_CHANGE_WHLP,
},
{ }
};
static struct ws2812b_device_info ws2812_device_info = {
.name = "ws2812-spi-fb",
.panels = ws2812_panels,
.clock_speed = 400000,
.led_current_max_red = 17,
.led_current_max_green = 17,
.led_current_max_blue = 17,
.led_current_base = 1,
};
/* define the match table */
static const struct of_device_id ws2812b_of_match[] = {
{
.compatible = "worldsemi,ws2812b",
.data = &ws2812b_device_info,
},
{
.compatible = "worldsemi,ws2812",
.data = &ws2812_device_info,
},
{ }
};
MODULE_DEVICE_TABLE(of, ws2812b_of_match);
static struct spi_driver ws2812b_driver = {
.driver = {
.name = DEVICE_NAME,
.owner = THIS_MODULE,
.of_match_table = ws2812b_of_match,
},
.probe = ws2812b_probe,
};
module_spi_driver(ws2812b_driver);
MODULE_AUTHOR("Martin Sperl <[email protected]>");
MODULE_DESCRIPTION("WS2812B RGB LED FB-driver via SPI");
MODULE_LICENSE("GPL");