forked from notro/fbtft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbtft-core.c
1516 lines (1323 loc) · 39.8 KB
/
fbtft-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
/*
* Copyright (C) 2013 Noralf Tronnes
*
* This driver is inspired by:
* st7735fb.c, Copyright (C) 2011, Matt Porter
* broadsheetfb.c, Copyright (C) 2008, Jaya Kumar
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fb.h>
#include <linux/gpio.h>
#include <linux/spi/spi.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <linux/backlight.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>
#include <linux/dma-mapping.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include "fbtft.h"
extern void fbtft_sysfs_init(struct fbtft_par *par);
extern void fbtft_sysfs_exit(struct fbtft_par *par);
extern void fbtft_expand_debug_value(unsigned long *debug);
extern int fbtft_gamma_parse_str(struct fbtft_par *par, unsigned long *curves,
const char *str, int size);
static unsigned long debug;
module_param(debug, ulong , 0);
MODULE_PARM_DESC(debug, "override device debug level");
static bool dma = true;
module_param(dma, bool, 0);
MODULE_PARM_DESC(dma, "Use DMA buffer");
void fbtft_dbg_hex(const struct device *dev, int groupsize,
void *buf, size_t len, const char *fmt, ...)
{
va_list args;
static char textbuf[512];
char *text = textbuf;
size_t text_len;
va_start(args, fmt);
text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
va_end(args);
hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len,
512 - text_len, false);
if (len > 32)
dev_info(dev, "%s ...\n", text);
else
dev_info(dev, "%s\n", text);
}
EXPORT_SYMBOL(fbtft_dbg_hex);
unsigned long fbtft_request_gpios_match(struct fbtft_par *par,
const struct fbtft_gpio *gpio)
{
int ret;
long val;
fbtft_par_dbg(DEBUG_REQUEST_GPIOS_MATCH, par, "%s('%s')\n",
__func__, gpio->name);
if (strcasecmp(gpio->name, "reset") == 0) {
par->gpio.reset = gpio->gpio;
return GPIOF_OUT_INIT_HIGH;
} else if (strcasecmp(gpio->name, "dc") == 0) {
par->gpio.dc = gpio->gpio;
return GPIOF_OUT_INIT_LOW;
} else if (strcasecmp(gpio->name, "cs") == 0) {
par->gpio.cs = gpio->gpio;
return GPIOF_OUT_INIT_HIGH;
} else if (strcasecmp(gpio->name, "wr") == 0) {
par->gpio.wr = gpio->gpio;
return GPIOF_OUT_INIT_HIGH;
} else if (strcasecmp(gpio->name, "rd") == 0) {
par->gpio.rd = gpio->gpio;
return GPIOF_OUT_INIT_HIGH;
} else if (strcasecmp(gpio->name, "latch") == 0) {
par->gpio.latch = gpio->gpio;
return GPIOF_OUT_INIT_LOW;
} else if (gpio->name[0] == 'd' && gpio->name[1] == 'b') {
ret = kstrtol(&gpio->name[2], 10, &val);
if (ret == 0 && val < 16) {
par->gpio.db[val] = gpio->gpio;
return GPIOF_OUT_INIT_LOW;
}
} else if (strcasecmp(gpio->name, "led") == 0) {
par->gpio.led[0] = gpio->gpio;
return GPIOF_OUT_INIT_LOW;
} else if (strcasecmp(gpio->name, "led_") == 0) {
par->gpio.led[0] = gpio->gpio;
return GPIOF_OUT_INIT_HIGH;
}
return FBTFT_GPIO_NO_MATCH;
}
int fbtft_request_gpios(struct fbtft_par *par)
{
struct fbtft_platform_data *pdata = par->pdata;
const struct fbtft_gpio *gpio;
unsigned long flags;
int ret;
if (pdata && pdata->gpios) {
gpio = pdata->gpios;
while (gpio->name[0]) {
flags = FBTFT_GPIO_NO_MATCH;
/* if driver provides match function, try it first,
if no match use our own */
if (par->fbtftops.request_gpios_match)
flags = par->fbtftops.request_gpios_match(par, gpio);
if (flags == FBTFT_GPIO_NO_MATCH)
flags = fbtft_request_gpios_match(par, gpio);
if (flags != FBTFT_GPIO_NO_MATCH) {
ret = devm_gpio_request_one(par->info->device,
gpio->gpio, flags,
par->info->device->driver->name);
if (ret < 0) {
dev_err(par->info->device,
"%s: gpio_request_one('%s'=%d) failed with %d\n",
__func__, gpio->name,
gpio->gpio, ret);
return ret;
}
fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par,
"%s: '%s' = GPIO%d\n",
__func__, gpio->name, gpio->gpio);
}
gpio++;
}
}
return 0;
}
#ifdef CONFIG_OF
static int fbtft_request_one_gpio(struct fbtft_par *par,
const char *name, int index, int *gpiop)
{
struct device *dev = par->info->device;
struct device_node *node = dev->of_node;
int gpio, flags, ret = 0;
enum of_gpio_flags of_flags;
if (of_find_property(node, name, NULL)) {
gpio = of_get_named_gpio_flags(node, name, index, &of_flags);
if (gpio == -ENOENT)
return 0;
if (gpio == -EPROBE_DEFER)
return gpio;
if (gpio < 0) {
dev_err(dev,
"failed to get '%s' from DT\n", name);
return gpio;
}
/* active low translates to initially low */
flags = (of_flags & OF_GPIO_ACTIVE_LOW) ? GPIOF_OUT_INIT_LOW :
GPIOF_OUT_INIT_HIGH;
ret = devm_gpio_request_one(dev, gpio, flags,
dev->driver->name);
if (ret) {
dev_err(dev,
"gpio_request_one('%s'=%d) failed with %d\n",
name, gpio, ret);
return ret;
}
if (gpiop)
*gpiop = gpio;
fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par, "%s: '%s' = GPIO%d\n",
__func__, name, gpio);
}
return ret;
}
static int fbtft_request_gpios_dt(struct fbtft_par *par)
{
int i;
int ret;
if (!par->info->device->of_node)
return -EINVAL;
ret = fbtft_request_one_gpio(par, "reset-gpios", 0, &par->gpio.reset);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "dc-gpios", 0, &par->gpio.dc);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "rd-gpios", 0, &par->gpio.rd);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "wr-gpios", 0, &par->gpio.wr);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "cs-gpios", 0, &par->gpio.cs);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "latch-gpios", 0, &par->gpio.latch);
if (ret)
return ret;
for (i = 0; i < 16; i++) {
ret = fbtft_request_one_gpio(par, "db-gpios", i,
&par->gpio.db[i]);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "led-gpios", i,
&par->gpio.led[i]);
if (ret)
return ret;
ret = fbtft_request_one_gpio(par, "aux-gpios", i,
&par->gpio.aux[i]);
if (ret)
return ret;
}
return 0;
}
#endif
#ifdef CONFIG_FB_BACKLIGHT
int fbtft_backlight_update_status(struct backlight_device *bd)
{
struct fbtft_par *par = bl_get_data(bd);
bool polarity = !!(bd->props.state & BL_CORE_DRIVER1);
fbtft_par_dbg(DEBUG_BACKLIGHT, par,
"%s: polarity=%d, power=%d, fb_blank=%d\n",
__func__, polarity, bd->props.power, bd->props.fb_blank);
if ((bd->props.power == FB_BLANK_UNBLANK) && (bd->props.fb_blank == FB_BLANK_UNBLANK))
gpio_set_value(par->gpio.led[0], polarity);
else
gpio_set_value(par->gpio.led[0], !polarity);
return 0;
}
int fbtft_backlight_get_brightness(struct backlight_device *bd)
{
return bd->props.brightness;
}
void fbtft_unregister_backlight(struct fbtft_par *par)
{
const struct backlight_ops *bl_ops;
fbtft_par_dbg(DEBUG_BACKLIGHT, par, "%s()\n", __func__);
if (par->info->bl_dev) {
par->info->bl_dev->props.power = FB_BLANK_POWERDOWN;
backlight_update_status(par->info->bl_dev);
bl_ops = par->info->bl_dev->ops;
backlight_device_unregister(par->info->bl_dev);
par->info->bl_dev = NULL;
}
}
void fbtft_register_backlight(struct fbtft_par *par)
{
struct backlight_device *bd;
struct backlight_properties bl_props = { 0, };
struct backlight_ops *bl_ops;
fbtft_par_dbg(DEBUG_BACKLIGHT, par, "%s()\n", __func__);
if (par->gpio.led[0] == -1) {
fbtft_par_dbg(DEBUG_BACKLIGHT, par,
"%s(): led pin not set, exiting.\n", __func__);
return;
}
bl_ops = devm_kzalloc(par->info->device, sizeof(struct backlight_ops),
GFP_KERNEL);
if (!bl_ops) {
dev_err(par->info->device,
"%s: could not allocate memeory for backlight operations.\n",
__func__);
return;
}
bl_ops->get_brightness = fbtft_backlight_get_brightness;
bl_ops->update_status = fbtft_backlight_update_status;
bl_props.type = BACKLIGHT_RAW;
/* Assume backlight is off, get polarity from current state of pin */
bl_props.power = FB_BLANK_POWERDOWN;
if (!gpio_get_value(par->gpio.led[0]))
bl_props.state |= BL_CORE_DRIVER1;
bd = backlight_device_register(dev_driver_string(par->info->device),
par->info->device, par, bl_ops, &bl_props);
if (IS_ERR(bd)) {
dev_err(par->info->device,
"cannot register backlight device (%ld)\n",
PTR_ERR(bd));
return;
}
par->info->bl_dev = bd;
if (!par->fbtftops.unregister_backlight)
par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
}
#else
void fbtft_register_backlight(struct fbtft_par *par) { };
void fbtft_unregister_backlight(struct fbtft_par *par) { };
#endif
EXPORT_SYMBOL(fbtft_register_backlight);
EXPORT_SYMBOL(fbtft_unregister_backlight);
void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
{
fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
"%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe, ye);
/* Column address set */
write_reg(par, 0x2A,
(xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
/* Row adress set */
write_reg(par, 0x2B,
(ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
/* Memory write */
write_reg(par, 0x2C);
}
void fbtft_reset(struct fbtft_par *par)
{
if (par->gpio.reset == -1)
return;
fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
gpio_set_value(par->gpio.reset, 0);
udelay(20);
gpio_set_value(par->gpio.reset, 1);
mdelay(120);
}
void fbtft_update_display(struct fbtft_par *par, unsigned start_line, unsigned end_line)
{
size_t offset, len;
struct timespec ts_start, ts_end, ts_fps, ts_duration;
long fps_ms, fps_us, duration_ms, duration_us;
long fps, throughput;
bool timeit = false;
int ret = 0;
if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE | DEBUG_TIME_EACH_UPDATE))) {
if ((par->debug & DEBUG_TIME_EACH_UPDATE) || \
((par->debug & DEBUG_TIME_FIRST_UPDATE) && !par->first_update_done)) {
getnstimeofday(&ts_start);
timeit = true;
}
}
/* Sanity checks */
if (start_line > end_line) {
dev_warn(par->info->device,
"%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
__func__, start_line, end_line);
start_line = 0;
end_line = par->info->var.yres - 1;
}
if (start_line > par->info->var.yres - 1 || end_line > par->info->var.yres - 1) {
dev_warn(par->info->device,
"%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
__func__, start_line, end_line, par->info->var.yres - 1);
start_line = 0;
end_line = par->info->var.yres - 1;
}
fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
__func__, start_line, end_line);
if (par->fbtftops.set_addr_win)
par->fbtftops.set_addr_win(par, 0, start_line,
par->info->var.xres-1, end_line);
offset = start_line * par->info->fix.line_length;
len = (end_line - start_line + 1) * par->info->fix.line_length;
ret = par->fbtftops.write_vmem(par, offset, len);
if (ret < 0)
dev_err(par->info->device,
"%s: write_vmem failed to update display buffer\n",
__func__);
if (unlikely(timeit)) {
getnstimeofday(&ts_end);
if (par->update_time.tv_nsec == 0 && par->update_time.tv_sec == 0) {
par->update_time.tv_sec = ts_start.tv_sec;
par->update_time.tv_nsec = ts_start.tv_nsec;
}
ts_fps = timespec_sub(ts_start, par->update_time);
par->update_time.tv_sec = ts_start.tv_sec;
par->update_time.tv_nsec = ts_start.tv_nsec;
fps_ms = (ts_fps.tv_sec * 1000) + ((ts_fps.tv_nsec / 1000000) % 1000);
fps_us = (ts_fps.tv_nsec / 1000) % 1000;
fps = fps_ms * 1000 + fps_us;
fps = fps ? 1000000 / fps : 0;
ts_duration = timespec_sub(ts_end, ts_start);
duration_ms = (ts_duration.tv_sec * 1000) + ((ts_duration.tv_nsec / 1000000) % 1000);
duration_us = (ts_duration.tv_nsec / 1000) % 1000;
throughput = duration_ms * 1000 + duration_us;
throughput = throughput ? (len * 1000) / throughput : 0;
throughput = throughput * 1000 / 1024;
dev_info(par->info->device,
"Display update: %ld kB/s (%ld.%.3ld ms), fps=%ld (%ld.%.3ld ms)\n",
throughput, duration_ms, duration_us,
fps, fps_ms, fps_us);
par->first_update_done = true;
}
}
void fbtft_mkdirty(struct fb_info *info, int y, int height)
{
struct fbtft_par *par = info->par;
struct fb_deferred_io *fbdefio = info->fbdefio;
/* special case, needed ? */
if (y == -1) {
y = 0;
height = info->var.yres - 1;
}
/* Mark display lines/area as dirty */
spin_lock(&par->dirty_lock);
if (y < par->dirty_lines_start)
par->dirty_lines_start = y;
if (y + height - 1 > par->dirty_lines_end)
par->dirty_lines_end = y + height - 1;
spin_unlock(&par->dirty_lock);
/* Schedule deferred_io to update display (no-op if already on queue)*/
schedule_delayed_work(&info->deferred_work, fbdefio->delay);
}
void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
{
struct fbtft_par *par = info->par;
unsigned dirty_lines_start, dirty_lines_end;
struct page *page;
unsigned long index;
unsigned y_low = 0, y_high = 0;
int count = 0;
spin_lock(&par->dirty_lock);
dirty_lines_start = par->dirty_lines_start;
dirty_lines_end = par->dirty_lines_end;
/* set display line markers as clean */
par->dirty_lines_start = par->info->var.yres - 1;
par->dirty_lines_end = 0;
spin_unlock(&par->dirty_lock);
/* Mark display lines as dirty */
list_for_each_entry(page, pagelist, lru) {
count++;
index = page->index << PAGE_SHIFT;
y_low = index / info->fix.line_length;
y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
fbtft_dev_dbg(DEBUG_DEFERRED_IO, par, info->device,
"page->index=%lu y_low=%d y_high=%d\n",
page->index, y_low, y_high);
if (y_high > info->var.yres - 1)
y_high = info->var.yres - 1;
if (y_low < dirty_lines_start)
dirty_lines_start = y_low;
if (y_high > dirty_lines_end)
dirty_lines_end = y_high;
}
par->fbtftops.update_display(info->par,
dirty_lines_start, dirty_lines_end);
}
void fbtft_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
{
struct fbtft_par *par = info->par;
fbtft_dev_dbg(DEBUG_FB_FILLRECT, par, info->dev,
"%s: dx=%d, dy=%d, width=%d, height=%d\n",
__func__, rect->dx, rect->dy, rect->width, rect->height);
sys_fillrect(info, rect);
par->fbtftops.mkdirty(info, rect->dy, rect->height);
}
void fbtft_fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
{
struct fbtft_par *par = info->par;
fbtft_dev_dbg(DEBUG_FB_COPYAREA, par, info->dev,
"%s: dx=%d, dy=%d, width=%d, height=%d\n",
__func__, area->dx, area->dy, area->width, area->height);
sys_copyarea(info, area);
par->fbtftops.mkdirty(info, area->dy, area->height);
}
void fbtft_fb_imageblit(struct fb_info *info, const struct fb_image *image)
{
struct fbtft_par *par = info->par;
fbtft_dev_dbg(DEBUG_FB_IMAGEBLIT, par, info->dev,
"%s: dx=%d, dy=%d, width=%d, height=%d\n",
__func__, image->dx, image->dy, image->width, image->height);
sys_imageblit(info, image);
par->fbtftops.mkdirty(info, image->dy, image->height);
}
ssize_t fbtft_fb_write(struct fb_info *info,
const char __user *buf, size_t count, loff_t *ppos)
{
struct fbtft_par *par = info->par;
ssize_t res;
fbtft_dev_dbg(DEBUG_FB_WRITE, par, info->dev,
"%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
res = fb_sys_write(info, buf, count, ppos);
/* TODO: only mark changed area
update all for now */
par->fbtftops.mkdirty(info, -1, 0);
return res;
}
/* from pxafb.c */
unsigned int chan_to_field(unsigned chan, struct fb_bitfield *bf)
{
chan &= 0xffff;
chan >>= 16 - bf->length;
return chan << bf->offset;
}
int fbtft_fb_setcolreg(unsigned regno,
unsigned red, unsigned green, unsigned blue,
unsigned transp, struct fb_info *info)
{
struct fbtft_par *par = info->par;
unsigned val;
int ret = 1;
fbtft_dev_dbg(DEBUG_FB_SETCOLREG, par, info->dev,
"%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
__func__, regno, red, green, blue, transp);
switch (info->fix.visual) {
case FB_VISUAL_TRUECOLOR:
if (regno < 16) {
u32 *pal = info->pseudo_palette;
val = chan_to_field(red, &info->var.red);
val |= chan_to_field(green, &info->var.green);
val |= chan_to_field(blue, &info->var.blue);
pal[regno] = val;
ret = 0;
}
break;
}
return ret;
}
int fbtft_fb_blank(int blank, struct fb_info *info)
{
struct fbtft_par *par = info->par;
int ret = -EINVAL;
fbtft_dev_dbg(DEBUG_FB_BLANK, par, info->dev, "%s(blank=%d)\n",
__func__, blank);
if (!par->fbtftops.blank)
return ret;
switch (blank) {
case FB_BLANK_POWERDOWN:
case FB_BLANK_VSYNC_SUSPEND:
case FB_BLANK_HSYNC_SUSPEND:
case FB_BLANK_NORMAL:
ret = par->fbtftops.blank(par, true);
break;
case FB_BLANK_UNBLANK:
ret = par->fbtftops.blank(par, false);
break;
}
return ret;
}
void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
{
if (src->write)
dst->write = src->write;
if (src->read)
dst->read = src->read;
if (src->write_vmem)
dst->write_vmem = src->write_vmem;
if (src->write_register)
dst->write_register = src->write_register;
if (src->set_addr_win)
dst->set_addr_win = src->set_addr_win;
if (src->reset)
dst->reset = src->reset;
if (src->mkdirty)
dst->mkdirty = src->mkdirty;
if (src->update_display)
dst->update_display = src->update_display;
if (src->init_display)
dst->init_display = src->init_display;
if (src->blank)
dst->blank = src->blank;
if (src->request_gpios_match)
dst->request_gpios_match = src->request_gpios_match;
if (src->request_gpios)
dst->request_gpios = src->request_gpios;
if (src->verify_gpios)
dst->verify_gpios = src->verify_gpios;
if (src->register_backlight)
dst->register_backlight = src->register_backlight;
if (src->unregister_backlight)
dst->unregister_backlight = src->unregister_backlight;
if (src->set_var)
dst->set_var = src->set_var;
if (src->set_gamma)
dst->set_gamma = src->set_gamma;
}
/**
* fbtft_framebuffer_alloc - creates a new frame buffer info structure
*
* @display: pointer to structure describing the display
* @dev: pointer to the device for this fb, this can be NULL
*
* Creates a new frame buffer info structure.
*
* Also creates and populates the following structures:
* info->fbops
* info->fbdefio
* info->pseudo_palette
* par->fbtftops
* par->txbuf
*
* Returns the new structure, or NULL if an error occurred.
*
*/
struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
struct device *dev)
{
struct fb_info *info;
struct fbtft_par *par;
struct fb_ops *fbops = NULL;
struct fb_deferred_io *fbdefio = NULL;
struct fbtft_platform_data *pdata = dev->platform_data;
u8 *vmem = NULL;
void *txbuf = NULL;
void *buf = NULL;
unsigned width;
unsigned height;
int txbuflen = display->txbuflen;
unsigned bpp = display->bpp;
unsigned fps = display->fps;
int vmem_size, i;
int *init_sequence = display->init_sequence;
char *gamma = display->gamma;
unsigned long *gamma_curves = NULL;
/* sanity check */
if (display->gamma_num * display->gamma_len > FBTFT_GAMMA_MAX_VALUES_TOTAL) {
dev_err(dev,
"%s: FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
__func__, FBTFT_GAMMA_MAX_VALUES_TOTAL);
return NULL;
}
/* defaults */
if (!fps)
fps = 20;
if (!bpp)
bpp = 16;
if (!pdata) {
dev_err(dev, "platform data is missing\n");
return NULL;
}
/* override driver values? */
if (pdata->fps)
fps = pdata->fps;
if (pdata->txbuflen)
txbuflen = pdata->txbuflen;
if (pdata->display.init_sequence)
init_sequence = pdata->display.init_sequence;
if (pdata->gamma)
gamma = pdata->gamma;
if (pdata->display.debug)
display->debug = pdata->display.debug;
if (pdata->display.backlight)
display->backlight = pdata->display.backlight;
if (pdata->display.width)
display->width = pdata->display.width;
if (pdata->display.height)
display->height = pdata->display.height;
if (pdata->display.buswidth)
display->buswidth = pdata->display.buswidth;
if (pdata->display.regwidth)
display->regwidth = pdata->display.regwidth;
display->debug |= debug;
fbtft_expand_debug_value(&display->debug);
switch (pdata->rotate) {
case 90:
case 270:
width = display->height;
height = display->width;
break;
default:
width = display->width;
height = display->height;
}
vmem_size = display->width * display->height * bpp / 8;
vmem = vzalloc(vmem_size);
if (!vmem)
goto alloc_fail;
fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
if (!fbops)
goto alloc_fail;
fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
if (!fbdefio)
goto alloc_fail;
buf = devm_kzalloc(dev, 128, GFP_KERNEL);
if (!buf)
goto alloc_fail;
if (display->gamma_num && display->gamma_len) {
gamma_curves = devm_kzalloc(dev, display->gamma_num * display->gamma_len * sizeof(gamma_curves[0]),
GFP_KERNEL);
if (!gamma_curves)
goto alloc_fail;
}
info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
if (!info)
goto alloc_fail;
info->screen_base = (u8 __force __iomem *)vmem;
info->fbops = fbops;
info->fbdefio = fbdefio;
fbops->owner = dev->driver->owner;
fbops->fb_read = fb_sys_read;
fbops->fb_write = fbtft_fb_write;
fbops->fb_fillrect = fbtft_fb_fillrect;
fbops->fb_copyarea = fbtft_fb_copyarea;
fbops->fb_imageblit = fbtft_fb_imageblit;
fbops->fb_setcolreg = fbtft_fb_setcolreg;
fbops->fb_blank = fbtft_fb_blank;
fbdefio->delay = HZ/fps;
fbdefio->deferred_io = fbtft_deferred_io;
fb_deferred_io_init(info);
strncpy(info->fix.id, dev->driver->name, 16);
info->fix.type = FB_TYPE_PACKED_PIXELS;
info->fix.visual = FB_VISUAL_TRUECOLOR;
info->fix.xpanstep = 0;
info->fix.ypanstep = 0;
info->fix.ywrapstep = 0;
info->fix.line_length = width*bpp/8;
info->fix.accel = FB_ACCEL_NONE;
info->fix.smem_len = vmem_size;
info->var.rotate = pdata->rotate;
info->var.xres = width;
info->var.yres = height;
info->var.xres_virtual = info->var.xres;
info->var.yres_virtual = info->var.yres;
info->var.bits_per_pixel = bpp;
info->var.nonstd = 1;
/* RGB565 */
info->var.red.offset = 11;
info->var.red.length = 5;
info->var.green.offset = 5;
info->var.green.length = 6;
info->var.blue.offset = 0;
info->var.blue.length = 5;
info->var.transp.offset = 0;
info->var.transp.length = 0;
info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
par = info->par;
par->info = info;
par->pdata = dev->platform_data;
par->debug = display->debug;
par->buf = buf;
spin_lock_init(&par->dirty_lock);
par->bgr = pdata->bgr;
par->startbyte = pdata->startbyte;
par->init_sequence = init_sequence;
par->gamma.curves = gamma_curves;
par->gamma.num_curves = display->gamma_num;
par->gamma.num_values = display->gamma_len;
mutex_init(&par->gamma.lock);
info->pseudo_palette = par->pseudo_palette;
if (par->gamma.curves && gamma) {
if (fbtft_gamma_parse_str(par,
par->gamma.curves, gamma, strlen(gamma)))
goto alloc_fail;
}
/* Transmit buffer */
if (txbuflen == -1)
txbuflen = vmem_size + 2; /* add in case startbyte is used */
#ifdef __LITTLE_ENDIAN
if ((!txbuflen) && (bpp > 8))
txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
#endif
if (txbuflen > 0) {
if (dma) {
dev->coherent_dma_mask = ~0;
txbuf = dmam_alloc_coherent(dev, txbuflen, &par->txbuf.dma, GFP_DMA);
} else {
txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
}
if (!txbuf)
goto alloc_fail;
par->txbuf.buf = txbuf;
par->txbuf.len = txbuflen;
}
/* Initialize gpios to disabled */
par->gpio.reset = -1;
par->gpio.dc = -1;
par->gpio.rd = -1;
par->gpio.wr = -1;
par->gpio.cs = -1;
par->gpio.latch = -1;
for (i = 0; i < 16; i++) {
par->gpio.db[i] = -1;
par->gpio.led[i] = -1;
par->gpio.aux[i] = -1;
}
/* default fbtft operations */
par->fbtftops.write = fbtft_write_spi;
par->fbtftops.read = fbtft_read_spi;
par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
par->fbtftops.write_register = fbtft_write_reg8_bus8;
par->fbtftops.set_addr_win = fbtft_set_addr_win;
par->fbtftops.reset = fbtft_reset;
par->fbtftops.mkdirty = fbtft_mkdirty;
par->fbtftops.update_display = fbtft_update_display;
par->fbtftops.request_gpios = fbtft_request_gpios;
if (display->backlight)
par->fbtftops.register_backlight = fbtft_register_backlight;
/* use driver provided functions */
fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
return info;
alloc_fail:
vfree(vmem);
return NULL;
}
EXPORT_SYMBOL(fbtft_framebuffer_alloc);
/**
* fbtft_framebuffer_release - frees up all memory used by the framebuffer
*
* @info: frame buffer info structure
*
*/
void fbtft_framebuffer_release(struct fb_info *info)
{
fb_deferred_io_cleanup(info);
vfree(info->screen_base);
framebuffer_release(info);
}
EXPORT_SYMBOL(fbtft_framebuffer_release);
/**
* fbtft_register_framebuffer - registers a tft frame buffer device
* @fb_info: frame buffer info structure
*
* Sets SPI driverdata if needed
* Requests needed gpios.
* Initializes display
* Updates display.
* Registers a frame buffer device @fb_info.
*
* Returns negative errno on error, or zero for success.
*
*/
int fbtft_register_framebuffer(struct fb_info *fb_info)
{
int ret;
char text1[50] = "";
char text2[50] = "";
struct fbtft_par *par = fb_info->par;
struct spi_device *spi = par->spi;
/* sanity checks */
if (!par->fbtftops.init_display) {
dev_err(fb_info->device, "missing fbtftops.init_display()\n");
return -EINVAL;
}
if (spi)
spi_set_drvdata(spi, fb_info);
if (par->pdev)
platform_set_drvdata(par->pdev, fb_info);
ret = par->fbtftops.request_gpios(par);
if (ret < 0)
goto reg_fail;
if (par->fbtftops.verify_gpios) {
ret = par->fbtftops.verify_gpios(par);
if (ret < 0)
goto reg_fail;
}
ret = par->fbtftops.init_display(par);
if (ret < 0)
goto reg_fail;
if (par->fbtftops.set_var) {
ret = par->fbtftops.set_var(par);
if (ret < 0)
goto reg_fail;
}
/* update the entire display */
par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
if (par->fbtftops.set_gamma && par->gamma.curves) {
ret = par->fbtftops.set_gamma(par, par->gamma.curves);
if (ret)
goto reg_fail;
}
if (par->fbtftops.register_backlight)
par->fbtftops.register_backlight(par);
ret = register_framebuffer(fb_info);
if (ret < 0)
goto reg_fail;
fbtft_sysfs_init(par);
if (par->txbuf.buf)
sprintf(text1, ", %d KiB %sbuffer memory",
par->txbuf.len >> 10, par->txbuf.dma ? "DMA " : "");
if (spi)