-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrgbled-fb-core.c
1013 lines (852 loc) · 24.4 KB
/
rgbled-fb-core.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* linux/drivers/video/fb/rgbled-fb.c
*
* (c) Martin Sperl <[email protected]>
*
* generic Frame buffer code for LED strips
*
* 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/device.h>
#include <linux/fb.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/leds.h>
#include <linux/list.h>
#include <linux/list_sort.h>
#include <linux/module.h>
#include <linux/spi/spi.h>
#include <linux/vmalloc.h>
#include "rgbled-fb.h"
/* exposure of component data in sysfs */
#if 0
#define SYSFS_PANEL_HELPER_SHOW(name, field) \
static ssize_t panel_ ## name ## _show(struct device *dev, \
struct device_attribute *a, \
char *buf) \
{ \
struct fb_info *fb = dev_get_drvdata(dev); \
struct rgbled_fb *rfb = fb->par; \
u32 val; \
\
spin_lock(&rfb->lock); \
val = rfb->field; \
spin_unlock(&rfb->lock); \
\
return sprintf(buf, "%i\n", val); \
}
#define SYSFS_PANEL_HELPER_RO(name, field) \
SYSFS_PANEL_HELPER_SHOW(name, field) \
static struct panel_##name## DEVICE_ATTR_RO(name)
#undef current
SYSFS_HELPER_RO(current, current_active);
SYSFS_HELPER_RO(current_max, current_max);
struct attribute *panel_attrs[]={
NULL,
};
struct kobj_type panel_ktype = {
.default_attrs = panel_attrs,
};
#endif
static int rgbled_register_sysfs_panels(struct rgbled_fb *rfb)
{
#if 0
struct fb_info *fb = rfb->info;
struct rgbled_panel_info *panel;
int err;
/* iterate over all panels */
list_for_each_entry(panel, &rfb->panels, list) {
/* init and register kobject */
printk(KERN_ERR "Register : %s\n", panel->name);
err = kobject_init_and_add(&panel->kobj,
&panel_ktype,
&fb->dev->kobj,
panel->name);
if (err)
return err;
}
#endif
return 0;
}
#define SYSFS_HELPER_SHOW(name, field) \
static ssize_t name ## _show(struct device *dev, \
struct device_attribute *a, \
char *buf) \
{ \
struct fb_info *fb = dev_get_drvdata(dev); \
struct rgbled_fb *rfb = fb->par; \
u32 val; \
\
spin_lock(&rfb->lock); \
val = rfb->field; \
spin_unlock(&rfb->lock); \
\
return sprintf(buf, "%i\n", val); \
}
#define SYSFS_HELPER_STORE(name, field, max) \
static ssize_t name ## _store(struct device *dev, \
struct device_attribute *a, \
const char *buf, size_t count) \
{ \
struct fb_info *fb = dev_get_drvdata(dev); \
struct rgbled_fb *rfb = fb->par; \
int err; \
u32 val; \
\
err = kstrtou32(buf, 0, &val); \
if (err) \
return err; \
if (val > max) \
return -EINVAL; \
\
spin_lock(&rfb->lock); \
rfb->field = val; \
rfb->current_max = 0; \
spin_unlock(&rfb->lock); \
\
rgbled_schedule(fb); \
\
return count; \
}
#define SYSFS_HELPER_RO(name, field) \
SYSFS_HELPER_SHOW(name, field) \
static DEVICE_ATTR_RO(name)
#define SYSFS_HELPER_RW(name, field, maxv) \
SYSFS_HELPER_SHOW(name, field) \
SYSFS_HELPER_STORE(name, field, maxv) \
static DEVICE_ATTR_RW(name)
/* this is unfortunately needed - otherwise "current" is expanded by cpp */
#undef current
SYSFS_HELPER_RW(brightness, brightness, 255);
SYSFS_HELPER_RO(current, current_active);
SYSFS_HELPER_RO(current_max, current_max);
SYSFS_HELPER_RW(current_limit, current_limit, 100000000);
SYSFS_HELPER_RW(led_current_max_red, led_current_max_red, 10000);
SYSFS_HELPER_RW(led_current_max_green, led_current_max_green, 10000);
SYSFS_HELPER_RW(led_current_max_blue, led_current_max_blue, 10000);
SYSFS_HELPER_RW(led_current_base, led_current_base, 10000);
SYSFS_HELPER_RO(led_count, pixel);
SYSFS_HELPER_RO(updates, screen_updates);
static struct device_attribute *device_attrs[] = {
&dev_attr_brightness,
&dev_attr_current,
&dev_attr_current_max,
&dev_attr_current_limit,
&dev_attr_led_current_max_red,
&dev_attr_led_current_max_green,
&dev_attr_led_current_max_blue,
&dev_attr_led_current_base,
&dev_attr_led_count,
&dev_attr_updates,
};
int rgbled_register_sysfs(struct rgbled_fb *rfb)
{
struct fb_info *fb = rfb->info;
int i;
int err = 0;
/* register panels */
err = rgbled_register_sysfs_panels(rfb);
if (err)
return err;
/* register all device_attributes */
for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
/* create the file */
err = device_create_file(fb->dev, device_attrs[i]);
if (err)
break;
}
if (err) {
while (--i >= 0)
device_remove_file(fb->dev, device_attrs[i]);
}
return err;
}
/* sysled support */
struct rgbled_led_data {
struct led_classdev cdev;
struct rgbled_fb *rfb;
struct rgbled_pixel *pixel;
enum rgbled_pixeltype type;
};
static void rgbled_unregister_single_led(struct device *dev, void *res)
{
struct rgbled_led_data *led = res;
led_classdev_unregister(&led->cdev);
}
static void rgbled_brightness_set(struct led_classdev *led_cdev,
enum led_brightness brightness)
{
struct rgbled_led_data *led = container_of(led_cdev,
typeof(*led), cdev);
/* set the value itself */
switch (led->type) {
case rgbled_pixeltype_red:
led->pixel->red = brightness; break;
case rgbled_pixeltype_green:
led->pixel->green = brightness; break;
case rgbled_pixeltype_blue:
led->pixel->blue = brightness; break;
case rgbled_pixeltype_brightness:
led->pixel->brightness = brightness;
break;
}
/* modifications to make things visible if nothing is set */
switch (led->type) {
case rgbled_pixeltype_red:
case rgbled_pixeltype_green:
case rgbled_pixeltype_blue:
if (!led->pixel->brightness)
led->pixel->brightness = 255;
break;
case rgbled_pixeltype_brightness:
/* if the rgb values are off, so set to white */
if ((!led->pixel->red) &&
(!led->pixel->green) &&
(!led->pixel->blue)) {
led->pixel->red = 255;
led->pixel->green = 255;
led->pixel->blue = 255;
}
break;
}
rgbled_schedule(led->rfb->info);
}
static enum led_brightness rgbled_brightness_get(struct led_classdev *led_cdev)
{
struct rgbled_led_data *led = container_of(led_cdev,
typeof(*led), cdev);
switch (led->type) {
case rgbled_pixeltype_red:
return led->pixel->red;
case rgbled_pixeltype_green:
return led->pixel->green;
case rgbled_pixeltype_blue:
return led->pixel->blue;
case rgbled_pixeltype_brightness:
return led->pixel->brightness;
}
return 0;
}
static int rgbled_register_single_led(struct rgbled_fb *rfb,
struct rgbled_panel_info *panel,
const char *label,
struct rgbled_coordinates *coord,
enum rgbled_pixeltype type,
const char *trigger)
{
struct rgbled_led_data *led;
struct rgbled_pixel *vpix;
int err;
/* get the pixel */
vpix = rgbled_get_raw_pixel(rfb, coord);
if (!vpix)
return -EINVAL;
/* get a new led instance */
led = devres_alloc(rgbled_unregister_single_led,
sizeof(*led), GFP_KERNEL);
if (!led)
return -ENOMEM;
led->rfb = rfb;
led->cdev.name = devm_kstrdup(rfb->info->dev, label, GFP_KERNEL);
led->cdev.max_brightness = 255;
led->cdev.brightness_set = rgbled_brightness_set;
led->cdev.brightness_get = rgbled_brightness_get;
led->cdev.default_trigger = trigger;
led->pixel = vpix;
led->type = type;
/* register the led */
err = led_classdev_register(rfb->info->dev, &led->cdev);
if (err) {
devres_free(led);
return err;
}
/* add the resource to get removed */
devres_add(rfb->info->device, led);
return 0;
}
static int rgbled_register_panel_led_all(struct rgbled_fb *rfb,
struct rgbled_panel_info *panel)
{
int i, err;
char label[32];
struct rgbled_coordinates coord;
for (i = 0; i < panel->pixel; i++) {
/* translate coordinates */
rgbled_get_pixel_coords(rfb, panel, i, &coord);
/* now register the individual led components */
snprintf(label, sizeof(label), "%s:%i:%i:red",
rfb->name, coord.x, coord.y);
err = rgbled_register_single_led(rfb, panel, label, &coord,
rgbled_pixeltype_red,
NULL);
if (err)
return err;
snprintf(label, sizeof(label), "%s:%i:%i:green",
rfb->name, coord.x, coord.y);
err = rgbled_register_single_led(rfb, panel, label, &coord,
rgbled_pixeltype_green,
NULL);
if (err)
return err;
snprintf(label, sizeof(label), "%s:%i:%i:blue",
rfb->name, coord.x, coord.y);
err = rgbled_register_single_led(rfb, panel, label, &coord,
rgbled_pixeltype_blue,
NULL);
if (err)
return err;
snprintf(label, sizeof(label), "%s:%i:%i:brightness",
rfb->name, coord.x, coord.y);
err = rgbled_register_single_led(rfb, panel, label, &coord,
rgbled_pixeltype_brightness,
NULL);
if (err)
return err;
}
return 0;
}
int rgbled_register_panel_sysled(struct rgbled_fb *rfb,
struct rgbled_panel_info *panel)
{
int err = 0;
/* TODO: expose specific leds with custom settings */
/* expose all leds via led-api if requested */
if (rfb->expose_all_led || panel->expose_all_led)
err = rgbled_register_panel_led_all(rfb, panel);
return err;
}
/* all the comented out are to get filled in */
static struct fb_fix_screeninfo fb_fix_screeninfo_default = {
.id = "rgbled-fb", /* overrridden by implementation */
.type = FB_TYPE_PACKED_PIXELS,
.visual = FB_VISUAL_TRUECOLOR,
.xpanstep = 0,
.ypanstep = 0,
.ywrapstep = 0,
.accel = FB_ACCEL_NONE,
/* .line_length = sizeof(ws2812b_pixel) * width */
/* .smem_start = 0 */
/* .smem_len = sizeof(ws2812b_pixel) * width * height */
.capabilities = 0,
};
static struct fb_var_screeninfo fb_var_screeninfo_default = {
/*
.xres = width,
.yres = height,
.xres_virtual = width,
.yres_virtual = height,
.width = width * xscale;
.height = width * yscale;
*/
.bits_per_pixel = 8 * sizeof(struct rgbled_pixel),
#define OFFSETS(name) { \
8 * offsetof(struct rgbled_pixel, name), \
8 * sizeof((((struct rgbled_pixel *)0)->name)), \
0 }
.red = OFFSETS(red),
.green = OFFSETS(green),
.blue = OFFSETS(blue),
.transp = OFFSETS(brightness),
};
static void rgbled_deferred_io(struct fb_info *fb,
struct list_head *pagelist);
static struct fb_deferred_io fb_deferred_io_default = {
/* .delay = HZ / 100, */
.deferred_io = rgbled_deferred_io,
};
/* framebuffer operations */
static ssize_t rgbled_write(struct fb_info *info,
const char __user *buf, size_t count,
loff_t *ppos)
{
ssize_t res = fb_sys_write(info, buf, count, ppos);
rgbled_schedule(info);
return res;
}
static void rgbled_fillrect(struct fb_info *info,
const struct fb_fillrect *rect)
{
sys_fillrect(info, rect);
rgbled_schedule(info);
}
static void rgbled_copyarea(struct fb_info *info,
const struct fb_copyarea *area)
{
sys_copyarea(info, area);
rgbled_schedule(info);
}
static void rgbled_imageblit(struct fb_info *info,
const struct fb_image *image)
{
sys_imageblit(info, image);
rgbled_schedule(info);
}
static struct fb_ops rgbled_ops = {
.fb_read = fb_sys_read,
.fb_write = rgbled_write,
.fb_fillrect = rgbled_fillrect,
.fb_copyarea = rgbled_copyarea,
.fb_imageblit = rgbled_imageblit,
};
void rgbled_get_pixel_coords_generic(
struct rgbled_fb *rfb,
struct rgbled_panel_info *panel,
int panel_pixel_num,
struct rgbled_coordinates *coord)
{
int x, y;
if (panel->layout_yx) {
y = panel_pixel_num % panel->height;
x = panel_pixel_num / panel->height;
} else {
x = panel_pixel_num % panel->width;
y = panel_pixel_num / panel->width;
}
if (panel->inverted_x)
x = panel->width - 1 - x;
if (panel->inverted_y)
y = panel->height - 1 - y;
coord->x = x;
coord->y = y;
}
void rgbled_get_pixel_coords_linear(
struct rgbled_fb *rfb,
struct rgbled_panel_info *panel,
int panel_pixel_num,
struct rgbled_coordinates *coord)
{
rgbled_get_pixel_coords_generic(rfb, panel, panel_pixel_num, coord);
coord->x += panel->x;
coord->y += panel->y;
}
EXPORT_SYMBOL_GPL(rgbled_get_pixel_coords_linear);
void rgbled_get_pixel_coords_meander(
struct rgbled_fb *rfb,
struct rgbled_panel_info *panel,
int panel_pixel_num,
struct rgbled_coordinates *coord)
{
rgbled_get_pixel_coords_generic(rfb, panel, panel_pixel_num, coord);
/* handle layout */
if (panel->layout_yx) {
if (coord->x & 1)
coord->y = panel->height - 1 - coord->y;
} else {
if (coord->y & 1)
coord->x = panel->width - 1 - coord->x;
coord->y += panel->y;
}
coord->x += panel->x;
coord->y += panel->y;
}
EXPORT_SYMBOL_GPL(rgbled_get_pixel_coords_meander);
static inline void rgbled_get_pixel_value_set(struct rgbled_fb *rfb,
struct rgbled_panel_info *panel,
struct rgbled_pixel *pix,
u8 r, u8 g, u8 b, u8 bright)
{
pix->red = r;
pix->green = g;
pix->blue = b;
pix->brightness = (u32)bright *
rfb->brightness *
panel->brightness /
255 / 255;
}
static void rgbled_get_pixel_value_default(struct rgbled_fb *rfb,
struct rgbled_panel_info *panel,
struct rgbled_coordinates *coord,
struct rgbled_pixel *pix)
{
struct rgbled_pixel *vpix;
if (coord->x > rfb->width)
return rgbled_get_pixel_value_set(rfb, panel, pix,
0, 0, 0, 0);
if (coord->y > rfb->height)
return rgbled_get_pixel_value_set(rfb, panel, pix,
0, 0, 0, 0);
/* copy pixel data */
vpix = rgbled_get_raw_pixel(rfb, coord);
rgbled_get_pixel_value_set(rfb, panel, pix,
vpix->red, vpix->green, vpix->blue,
vpix->brightness);
}
static u8 rgbled_handle_panel(struct rgbled_fb *rfb,
int start_pixel,
struct rgbled_panel_info *panel)
{
struct rgbled_coordinates coord;
struct rgbled_pixel pix;
int i;
u64 c = 0; /* current - need 64bit temporarily because of scaling */
/* iterate over all pixel */
for (i = 0; i < panel->pixel; i++) {
/* get the coordinates */
rgbled_get_pixel_coords(rfb, panel, i, &coord);
/* now get the corresponding value */
rgbled_get_pixel_value(rfb, panel, &coord, &pix);
/* here we could add gamma control if needed */
/* and set it */
rfb->set_pixel_value(rfb, panel, start_pixel + i, &pix);
/* and calculate current estimate */
c += pix.red * pix.brightness * rfb->led_current_max_red;
c += pix.green * pix.brightness * rfb->led_current_max_green;
c += pix.blue * pix.brightness * rfb->led_current_max_blue;
}
/* and scale down back */
do_div(c, 255 * 255);
/* add base panel-consumption (after scaling!)*/
c += rfb->led_current_base * panel->pixel;
/* and assign/add it */
panel->current_tmp = c;
rfb->current_tmp += c;
/* return 255 for a "constant scale" - not rescaling */
if (!panel->current_limit)
return 255;
if (panel->current_limit >= c)
return 255;
/* so we exceed the limit, so warn */
fb_warn(rfb->info,
"panel %s consumes %llu mA and exceeded current limit of %i mA\n",
panel->name, c, panel->current_limit);
/* and return a different scale */
return (u32)254 * panel->current_limit / (u32)c;
}
static u8 rgbled_handle_panels(struct rgbled_fb *rfb)
{
struct rgbled_panel_info *panel;
int start_pixel = 0;
u8 rescale;
/* reset current estimation */
rfb->current_tmp = 0;
/* iterate over all panels */
list_for_each_entry(panel, &rfb->panels, list) {
rescale = rgbled_handle_panel(rfb, start_pixel, panel);
/* handle rescale request by propagating */
if (rescale != 255)
return rescale;
start_pixel += panel->pixel;
}
/* check current */
if (!rfb->current_limit)
return 255;
if (rfb->current_limit >= rfb->current_tmp)
return 255;
fb_warn(rfb->info,
"total panel consumes %i mA and exceeded current limit of %i mA\n",
rfb->current_tmp, rfb->current_limit);
/* need to return with new scaling */
return (u32)254 * rfb->current_limit / rfb->current_tmp;
}
static void rgbled_update_stats(struct rgbled_fb *rfb)
{
struct rgbled_panel_info *panel;
spin_lock(&rfb->lock);
/* commit currents */
list_for_each_entry(panel, &rfb->panels, list) {
panel->current_active = panel->current_tmp;
if (panel->current_active > panel->current_max)
panel->current_max = panel->current_active;
}
rfb->current_active = rfb->current_tmp;
if (rfb->current_active > rfb->current_max)
rfb->current_max = rfb->current_active;
rfb->screen_updates++;
spin_unlock(&rfb->lock);
}
static void rgbled_deferred_work_default(struct rgbled_fb *rfb)
{
int iterations = 0;
u8 rescale = rgbled_handle_panels(rfb);
/* rescale the global brightness */
while (rescale < 255) {
/* change brightness */
rfb->brightness = rfb->brightness * rescale / 255;
/* and rerun the calculation */
rescale = rgbled_handle_panels(rfb);
/* and exit early after a few loops */
iterations++;
if (iterations > 256) {
fb_warn(rfb->info,
"could not reduce brightness enough to reach required current limit - not updating display");
return;
}
}
/* commit the calculated currents */
rgbled_update_stats(rfb);
/* and handle the final step */
if (rfb->finish_work)
rfb->finish_work(rfb);
}
static void rgbled_deferred_io(struct fb_info *fb,
struct list_head *pagelist)
{
struct rgbled_fb *rfb = fb->par;
rfb->deferred_work(rfb);
}
static inline struct rgbled_panel_info *to_panel_info(
struct list_head *list)
{
return list ? container_of(list, struct rgbled_panel_info, list) :
NULL;
}
static int rgbled_panel_info_cmp(void *priv,
struct list_head *a,
struct list_head *b)
{
struct rgbled_fb *rfb = priv;
struct rgbled_panel_info *ad = to_panel_info(a);
struct rgbled_panel_info *bd = to_panel_info(b);
if (ad->id < bd->id)
return -1;
if (ad->id > bd->id)
return 1;
/* there should be no identical IDs, so mark as duplicates */
rfb->duplicate_id = 1;
return 0;
}
int rgbled_panel_multiple_width(struct rgbled_panel_info *panel, u32 val)
{
panel->width *= val;
panel->pixel *= val;
return 0;
}
EXPORT_SYMBOL_GPL(rgbled_panel_multiple_width);
int rgbled_panel_multiple_height(struct rgbled_panel_info *panel, u32 val)
{
panel->height *= val;
panel->pixel *= val;
return 0;
}
EXPORT_SYMBOL_GPL(rgbled_panel_multiple_height);
int rgbled_register_panel(struct rgbled_fb *rfb,
struct rgbled_panel_info *panel)
{
/* add to list */
list_add(&panel->list, &rfb->panels);
/* calculate size of panel in pixel if not set */
if (!panel->pixel)
panel->pixel = panel->width * panel->height;
/* check values */
if (!panel->width)
return -EINVAL;
if (!panel->height)
return -EINVAL;
if (!panel->pixel)
return -EINVAL;
/* add the number of pixel to the chain */
rfb->pixel += panel->pixel;
/* setting max coordinates for the framebuffer */
if (rfb->width < panel->x + panel->width)
rfb->width = panel->x + panel->width;
if (rfb->height < panel->x + panel->height)
rfb->height = panel->y + panel->height;
return 0;
}
EXPORT_SYMBOL_GPL(rgbled_register_panel);
int rgbled_scan_panels(struct rgbled_fb *rfb,
struct rgbled_panel_info *panels)
{
struct device *dev = rfb->info->device;
int err;
/* scan for panels in device tree */
err = rgbled_scan_panels_of(rfb, panels);
if (err)
return err;
/* sort list - used to shift out the data
* this also checks that there are no duplicate ids...
*/
list_sort(rfb, &rfb->panels, rgbled_panel_info_cmp);
/* if we got a duplicate, then fail */
if (rfb->duplicate_id) {
dev_err(dev, "duplicate\n");
return -EINVAL;
}
/* if we got no pixel, then fail */
if (!rfb->pixel) {
dev_err(dev, "nopixel\n");
return -EINVAL;
}
return 0;
}
static void rgbled_unregister_framebuffer(struct device *dev, void *res)
{
struct rgbled_fb *rfb = *(struct rgbled_fb **)res;
fb_deferred_io_cleanup(rfb->info);
vfree(rfb->vmem);
rfb->vmem = NULL;
unregister_framebuffer(rfb->info);
}
static void rgbled_framebuffer_release(struct device *dev, void *res)
{
struct rgbled_fb *rfb = *(struct rgbled_fb **)res;
framebuffer_release(rfb->info);
rfb->info = NULL;
}
struct rgbled_fb *rgbled_alloc(struct device *dev,
const char *name,
struct rgbled_panel_info *panels)
{
struct fb_info *fb;
struct rgbled_fb *rfb;
struct rgbled_fb **ptr;
int err;
/* initialize our own structure */
rfb = devm_kzalloc(dev, sizeof(*rfb), GFP_KERNEL);
/* first copy the defaults */
memcpy(&rfb->deferred_io,
&fb_deferred_io_default,
sizeof(fb_deferred_io_default));
/* now set up specific things */
INIT_LIST_HEAD(&rfb->panels);
spin_lock_init(&rfb->lock);
/* now allocate the framebuffer_info via devres */
ptr = devres_alloc(rgbled_framebuffer_release,
sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return ERR_PTR(-ENOMEM);
/* allocate framebuffer */
fb = framebuffer_alloc(0, dev);
if (!fb) {
devres_free(ptr);
return ERR_PTR(-ENOMEM);
}
*ptr = rfb;
devres_add(dev, ptr);
/* and add a pointer back and forth */
fb->par = rfb;
rfb->info = fb;
/* set up basics */
fb->fbops = &rgbled_ops;
fb->fbdefio = &rfb->deferred_io;
fb->fix = fb_fix_screeninfo_default;
fb->var = fb_var_screeninfo_default;
fb->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
/* needs to happen agfter the assignement above */
strncpy(fb->fix.id, name, sizeof(fb->fix.id));
/* scan panels to get string size */
err = rgbled_scan_panels(rfb, panels);
if (err)
return ERR_PTR(err);
return rfb;
}
EXPORT_SYMBOL_GPL(rgbled_alloc);
static int rgbled_fix_up_structures(struct rgbled_fb *rfb)
{
struct rgbled_panel_info *p;
/* fill in those empty vectors */
if (!rfb->deferred_work) {
rfb->deferred_work = rgbled_deferred_work_default;
/* if there is no custom implementation,
* then we need set_pixel_value
* finish_work is optional...
*/
if (!rfb->set_pixel_value) {
fb_err(rfb->info,
"no set_pixel_value method configured\n");
return -EINVAL;
}
}
/* fill in those empty vectors with defaults */
if (!rfb->get_pixel_value)
rfb->get_pixel_value = rgbled_get_pixel_value_default;
/* and the panels */
list_for_each_entry(p, &rfb->panels, list) {
if (!p->get_pixel_coords)
p->get_pixel_coords = rgbled_get_pixel_coords_linear;
if (!p->get_pixel_value)
p->get_pixel_value = rfb->get_pixel_value;
}
return 0;
}
int rgbled_register_panels_sysled(struct rgbled_fb *rfb)
{
struct rgbled_panel_info *panel;
int err;
/* register each led in each panel - if given */
list_for_each_entry(panel, &rfb->panels, list) {
err = rgbled_register_panel_sysled(rfb, panel);
if (err)
return err;
}
return 0;
}
int rgbled_register(struct rgbled_fb *rfb)
{
struct fb_info *fb = rfb->info;
int err;
struct rgbled_fb **ptr;
/* fill in the default vectors */
err = rgbled_fix_up_structures(rfb);
if (err)
return err;
/* register devices via device-tree */
err = rgbled_register_of(rfb);
if (err)
return err;
/* prepare release */
ptr = devres_alloc(rgbled_unregister_framebuffer,
sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return -ENOMEM;
*ptr = rfb;
/* set up sizes */
fb->var.xres_virtual = rfb->width;
fb->var.yres_virtual = rfb->height;
fb->var.xres = rfb->width;
fb->var.yres = rfb->height;
fb->fix.line_length = sizeof(struct rgbled_pixel) * rfb->width;
rfb->vmem_size = fb->fix.line_length * rfb->height;
/* allocate memory */
rfb->vmem = vzalloc(rfb->vmem_size);
if (!rfb->vmem) {
devres_free(ptr);
return -ENOMEM;
}
/* set vmem data */
fb->fix.smem_len = rfb->vmem_size;
fb->screen_size = rfb->vmem_size;
fb->screen_base = (typeof(fb->screen_base))rfb->vmem;
fb->fix.smem_start = (typeof(fb->fix.smem_start))rfb->vmem;
/* initialize deferred io as a devm device */
fb_deferred_io_init(fb);
/* register fb */
err = register_framebuffer(fb);
if (err) {
vfree(rfb->vmem);
devres_free(ptr);
return err;
}
/* now register resource cleanup */
devres_add(fb->device, ptr);
/* and register additional information in sysfs */
err = rgbled_register_sysfs(rfb);
if (err)
return err;
/* and register panel leds */
err = rgbled_register_panels_sysled(rfb);
if (err)
return err;
/* calculate refresh rate to use */
if (!rfb->deferred_io.delay)
rfb->deferred_io.delay = HZ / 100;
/* and start an initial update of the framebuffer to clean it */