1
2
3
4
5
6
7
8
9
10#include <linux/module.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/string.h>
14#include <linux/mm.h>
15#include <linux/vmalloc.h>
16#include <linux/slab.h>
17#include <linux/init.h>
18#include <linux/fb.h>
19#include <linux/gpio/consumer.h>
20#include <linux/spi/spi.h>
21#include <linux/delay.h>
22#include <linux/uaccess.h>
23#include <linux/backlight.h>
24#include <linux/platform_device.h>
25#include <linux/property.h>
26#include <linux/spinlock.h>
27
28#include <video/mipi_display.h>
29
30#include "fbtft.h"
31#include "internal.h"
32
33static unsigned long debug;
34module_param(debug, ulong, 0000);
35MODULE_PARM_DESC(debug, "override device debug level");
36
37int fbtft_write_buf_dc(struct fbtft_par *par, void *buf, size_t len, int dc)
38{
39 int ret;
40
41 if (par->gpio.dc)
42 gpiod_set_value(par->gpio.dc, dc);
43
44 ret = par->fbtftops.write(par, buf, len);
45 if (ret < 0)
46 dev_err(par->info->device,
47 "write() failed and returned %d\n", ret);
48 return ret;
49}
50EXPORT_SYMBOL(fbtft_write_buf_dc);
51
52void fbtft_dbg_hex(const struct device *dev, int groupsize,
53 void *buf, size_t len, const char *fmt, ...)
54{
55 va_list args;
56 static char textbuf[512];
57 char *text = textbuf;
58 size_t text_len;
59
60 va_start(args, fmt);
61 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
62 va_end(args);
63
64 hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len,
65 512 - text_len, false);
66
67 if (len > 32)
68 dev_info(dev, "%s ...\n", text);
69 else
70 dev_info(dev, "%s\n", text);
71}
72EXPORT_SYMBOL(fbtft_dbg_hex);
73
74static int fbtft_request_one_gpio(struct fbtft_par *par,
75 const char *name, int index,
76 struct gpio_desc **gpiop)
77{
78 struct device *dev = par->info->device;
79 int ret = 0;
80
81 *gpiop = devm_gpiod_get_index_optional(dev, name, index,
82 GPIOD_OUT_HIGH);
83 if (IS_ERR(*gpiop)) {
84 ret = PTR_ERR(*gpiop);
85 dev_err(dev,
86 "Failed to request %s GPIO: %d\n", name, ret);
87 return ret;
88 }
89 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par, "%s: '%s' GPIO\n",
90 __func__, name);
91
92 return ret;
93}
94
95static int fbtft_request_gpios(struct fbtft_par *par)
96{
97 int i;
98 int ret;
99
100 ret = fbtft_request_one_gpio(par, "reset", 0, &par->gpio.reset);
101 if (ret)
102 return ret;
103 ret = fbtft_request_one_gpio(par, "dc", 0, &par->gpio.dc);
104 if (ret)
105 return ret;
106 ret = fbtft_request_one_gpio(par, "rd", 0, &par->gpio.rd);
107 if (ret)
108 return ret;
109 ret = fbtft_request_one_gpio(par, "wr", 0, &par->gpio.wr);
110 if (ret)
111 return ret;
112 ret = fbtft_request_one_gpio(par, "cs", 0, &par->gpio.cs);
113 if (ret)
114 return ret;
115 ret = fbtft_request_one_gpio(par, "latch", 0, &par->gpio.latch);
116 if (ret)
117 return ret;
118 for (i = 0; i < 16; i++) {
119 ret = fbtft_request_one_gpio(par, "db", i,
120 &par->gpio.db[i]);
121 if (ret)
122 return ret;
123 ret = fbtft_request_one_gpio(par, "led", i,
124 &par->gpio.led[i]);
125 if (ret)
126 return ret;
127 ret = fbtft_request_one_gpio(par, "aux", i,
128 &par->gpio.aux[i]);
129 if (ret)
130 return ret;
131 }
132
133 return 0;
134}
135
136#ifdef CONFIG_FB_BACKLIGHT
137static int fbtft_backlight_update_status(struct backlight_device *bd)
138{
139 struct fbtft_par *par = bl_get_data(bd);
140 bool polarity = par->polarity;
141
142 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
143 "%s: polarity=%d, power=%d, fb_blank=%d\n",
144 __func__, polarity, bd->props.power, bd->props.fb_blank);
145
146 if ((bd->props.power == FB_BLANK_UNBLANK) &&
147 (bd->props.fb_blank == FB_BLANK_UNBLANK))
148 gpiod_set_value(par->gpio.led[0], polarity);
149 else
150 gpiod_set_value(par->gpio.led[0], !polarity);
151
152 return 0;
153}
154
155static int fbtft_backlight_get_brightness(struct backlight_device *bd)
156{
157 return bd->props.brightness;
158}
159
160void fbtft_unregister_backlight(struct fbtft_par *par)
161{
162 if (par->info->bl_dev) {
163 par->info->bl_dev->props.power = FB_BLANK_POWERDOWN;
164 backlight_update_status(par->info->bl_dev);
165 backlight_device_unregister(par->info->bl_dev);
166 par->info->bl_dev = NULL;
167 }
168}
169
170static const struct backlight_ops fbtft_bl_ops = {
171 .get_brightness = fbtft_backlight_get_brightness,
172 .update_status = fbtft_backlight_update_status,
173};
174
175void fbtft_register_backlight(struct fbtft_par *par)
176{
177 struct backlight_device *bd;
178 struct backlight_properties bl_props = { 0, };
179
180 if (!par->gpio.led[0]) {
181 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
182 "%s(): led pin not set, exiting.\n", __func__);
183 return;
184 }
185
186 bl_props.type = BACKLIGHT_RAW;
187
188 bl_props.power = FB_BLANK_POWERDOWN;
189 if (!gpiod_get_value(par->gpio.led[0]))
190 par->polarity = true;
191
192 bd = backlight_device_register(dev_driver_string(par->info->device),
193 par->info->device, par,
194 &fbtft_bl_ops, &bl_props);
195 if (IS_ERR(bd)) {
196 dev_err(par->info->device,
197 "cannot register backlight device (%ld)\n",
198 PTR_ERR(bd));
199 return;
200 }
201 par->info->bl_dev = bd;
202
203 if (!par->fbtftops.unregister_backlight)
204 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
205}
206#else
207void fbtft_register_backlight(struct fbtft_par *par) { };
208void fbtft_unregister_backlight(struct fbtft_par *par) { };
209#endif
210EXPORT_SYMBOL(fbtft_register_backlight);
211EXPORT_SYMBOL(fbtft_unregister_backlight);
212
213static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
214 int ye)
215{
216 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
217 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
218
219 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
220 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
221
222 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
223}
224
225static void fbtft_reset(struct fbtft_par *par)
226{
227 if (!par->gpio.reset)
228 return;
229 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
230 gpiod_set_value_cansleep(par->gpio.reset, 1);
231 usleep_range(20, 40);
232 gpiod_set_value_cansleep(par->gpio.reset, 0);
233 msleep(120);
234}
235
236static void fbtft_update_display(struct fbtft_par *par, unsigned int start_line,
237 unsigned int end_line)
238{
239 size_t offset, len;
240 ktime_t ts_start, ts_end;
241 long fps, throughput;
242 bool timeit = false;
243 int ret = 0;
244
245 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE |
246 DEBUG_TIME_EACH_UPDATE))) {
247 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
248 ((par->debug & DEBUG_TIME_FIRST_UPDATE) &&
249 !par->first_update_done)) {
250 ts_start = ktime_get();
251 timeit = true;
252 }
253 }
254
255
256 if (start_line > end_line) {
257 dev_warn(par->info->device,
258 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
259 __func__, start_line, end_line);
260 start_line = 0;
261 end_line = par->info->var.yres - 1;
262 }
263 if (start_line > par->info->var.yres - 1 ||
264 end_line > par->info->var.yres - 1) {
265 dev_warn(par->info->device,
266 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
267 __func__, start_line,
268 end_line, par->info->var.yres - 1);
269 start_line = 0;
270 end_line = par->info->var.yres - 1;
271 }
272
273 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
274 __func__, start_line, end_line);
275
276 if (par->fbtftops.set_addr_win)
277 par->fbtftops.set_addr_win(par, 0, start_line,
278 par->info->var.xres - 1, end_line);
279
280 offset = start_line * par->info->fix.line_length;
281 len = (end_line - start_line + 1) * par->info->fix.line_length;
282 ret = par->fbtftops.write_vmem(par, offset, len);
283 if (ret < 0)
284 dev_err(par->info->device,
285 "%s: write_vmem failed to update display buffer\n",
286 __func__);
287
288 if (unlikely(timeit)) {
289 ts_end = ktime_get();
290 if (!ktime_to_ns(par->update_time))
291 par->update_time = ts_start;
292
293 fps = ktime_us_delta(ts_start, par->update_time);
294 par->update_time = ts_start;
295 fps = fps ? 1000000 / fps : 0;
296
297 throughput = ktime_us_delta(ts_end, ts_start);
298 throughput = throughput ? (len * 1000) / throughput : 0;
299 throughput = throughput * 1000 / 1024;
300
301 dev_info(par->info->device,
302 "Display update: %ld kB/s, fps=%ld\n",
303 throughput, fps);
304 par->first_update_done = true;
305 }
306}
307
308static void fbtft_mkdirty(struct fb_info *info, int y, int height)
309{
310 struct fbtft_par *par = info->par;
311 struct fb_deferred_io *fbdefio = info->fbdefio;
312
313
314 if (y == -1) {
315 y = 0;
316 height = info->var.yres;
317 }
318
319
320 spin_lock(&par->dirty_lock);
321 if (y < par->dirty_lines_start)
322 par->dirty_lines_start = y;
323 if (y + height - 1 > par->dirty_lines_end)
324 par->dirty_lines_end = y + height - 1;
325 spin_unlock(&par->dirty_lock);
326
327
328 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
329}
330
331static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
332{
333 struct fbtft_par *par = info->par;
334 unsigned int dirty_lines_start, dirty_lines_end;
335 struct page *page;
336 unsigned long index;
337 unsigned int y_low = 0, y_high = 0;
338 int count = 0;
339
340 spin_lock(&par->dirty_lock);
341 dirty_lines_start = par->dirty_lines_start;
342 dirty_lines_end = par->dirty_lines_end;
343
344 par->dirty_lines_start = par->info->var.yres - 1;
345 par->dirty_lines_end = 0;
346 spin_unlock(&par->dirty_lock);
347
348
349 list_for_each_entry(page, pagelist, lru) {
350 count++;
351 index = page->index << PAGE_SHIFT;
352 y_low = index / info->fix.line_length;
353 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
354 dev_dbg(info->device,
355 "page->index=%lu y_low=%d y_high=%d\n",
356 page->index, y_low, y_high);
357 if (y_high > info->var.yres - 1)
358 y_high = info->var.yres - 1;
359 if (y_low < dirty_lines_start)
360 dirty_lines_start = y_low;
361 if (y_high > dirty_lines_end)
362 dirty_lines_end = y_high;
363 }
364
365 par->fbtftops.update_display(info->par,
366 dirty_lines_start, dirty_lines_end);
367}
368
369static void fbtft_fb_fillrect(struct fb_info *info,
370 const struct fb_fillrect *rect)
371{
372 struct fbtft_par *par = info->par;
373
374 dev_dbg(info->dev,
375 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
376 __func__, rect->dx, rect->dy, rect->width, rect->height);
377 sys_fillrect(info, rect);
378
379 par->fbtftops.mkdirty(info, rect->dy, rect->height);
380}
381
382static void fbtft_fb_copyarea(struct fb_info *info,
383 const struct fb_copyarea *area)
384{
385 struct fbtft_par *par = info->par;
386
387 dev_dbg(info->dev,
388 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
389 __func__, area->dx, area->dy, area->width, area->height);
390 sys_copyarea(info, area);
391
392 par->fbtftops.mkdirty(info, area->dy, area->height);
393}
394
395static void fbtft_fb_imageblit(struct fb_info *info,
396 const struct fb_image *image)
397{
398 struct fbtft_par *par = info->par;
399
400 dev_dbg(info->dev,
401 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
402 __func__, image->dx, image->dy, image->width, image->height);
403 sys_imageblit(info, image);
404
405 par->fbtftops.mkdirty(info, image->dy, image->height);
406}
407
408static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
409 size_t count, loff_t *ppos)
410{
411 struct fbtft_par *par = info->par;
412 ssize_t res;
413
414 dev_dbg(info->dev,
415 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
416 res = fb_sys_write(info, buf, count, ppos);
417
418
419 par->fbtftops.mkdirty(info, -1, 0);
420
421 return res;
422}
423
424
425static unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
426{
427 chan &= 0xffff;
428 chan >>= 16 - bf->length;
429 return chan << bf->offset;
430}
431
432static int fbtft_fb_setcolreg(unsigned int regno, unsigned int red,
433 unsigned int green, unsigned int blue,
434 unsigned int transp, struct fb_info *info)
435{
436 unsigned int val;
437 int ret = 1;
438
439 dev_dbg(info->dev,
440 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
441 __func__, regno, red, green, blue, transp);
442
443 switch (info->fix.visual) {
444 case FB_VISUAL_TRUECOLOR:
445 if (regno < 16) {
446 u32 *pal = info->pseudo_palette;
447
448 val = chan_to_field(red, &info->var.red);
449 val |= chan_to_field(green, &info->var.green);
450 val |= chan_to_field(blue, &info->var.blue);
451
452 pal[regno] = val;
453 ret = 0;
454 }
455 break;
456 }
457 return ret;
458}
459
460static int fbtft_fb_blank(int blank, struct fb_info *info)
461{
462 struct fbtft_par *par = info->par;
463 int ret = -EINVAL;
464
465 dev_dbg(info->dev, "%s(blank=%d)\n",
466 __func__, blank);
467
468 if (!par->fbtftops.blank)
469 return ret;
470
471 switch (blank) {
472 case FB_BLANK_POWERDOWN:
473 case FB_BLANK_VSYNC_SUSPEND:
474 case FB_BLANK_HSYNC_SUSPEND:
475 case FB_BLANK_NORMAL:
476 ret = par->fbtftops.blank(par, true);
477 break;
478 case FB_BLANK_UNBLANK:
479 ret = par->fbtftops.blank(par, false);
480 break;
481 }
482 return ret;
483}
484
485static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
486{
487 if (src->write)
488 dst->write = src->write;
489 if (src->read)
490 dst->read = src->read;
491 if (src->write_vmem)
492 dst->write_vmem = src->write_vmem;
493 if (src->write_register)
494 dst->write_register = src->write_register;
495 if (src->set_addr_win)
496 dst->set_addr_win = src->set_addr_win;
497 if (src->reset)
498 dst->reset = src->reset;
499 if (src->mkdirty)
500 dst->mkdirty = src->mkdirty;
501 if (src->update_display)
502 dst->update_display = src->update_display;
503 if (src->init_display)
504 dst->init_display = src->init_display;
505 if (src->blank)
506 dst->blank = src->blank;
507 if (src->request_gpios_match)
508 dst->request_gpios_match = src->request_gpios_match;
509 if (src->request_gpios)
510 dst->request_gpios = src->request_gpios;
511 if (src->verify_gpios)
512 dst->verify_gpios = src->verify_gpios;
513 if (src->register_backlight)
514 dst->register_backlight = src->register_backlight;
515 if (src->unregister_backlight)
516 dst->unregister_backlight = src->unregister_backlight;
517 if (src->set_var)
518 dst->set_var = src->set_var;
519 if (src->set_gamma)
520 dst->set_gamma = src->set_gamma;
521}
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
543 struct device *dev,
544 struct fbtft_platform_data *pdata)
545{
546 struct fb_info *info;
547 struct fbtft_par *par;
548 struct fb_ops *fbops = NULL;
549 struct fb_deferred_io *fbdefio = NULL;
550 u8 *vmem = NULL;
551 void *txbuf = NULL;
552 void *buf = NULL;
553 unsigned int width;
554 unsigned int height;
555 int txbuflen = display->txbuflen;
556 unsigned int bpp = display->bpp;
557 unsigned int fps = display->fps;
558 int vmem_size;
559 const s16 *init_sequence = display->init_sequence;
560 char *gamma = display->gamma;
561 u32 *gamma_curves = NULL;
562
563
564 if (display->gamma_num * display->gamma_len >
565 FBTFT_GAMMA_MAX_VALUES_TOTAL) {
566 dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
567 FBTFT_GAMMA_MAX_VALUES_TOTAL);
568 return NULL;
569 }
570
571
572 if (!fps)
573 fps = 20;
574 if (!bpp)
575 bpp = 16;
576
577 if (!pdata) {
578 dev_err(dev, "platform data is missing\n");
579 return NULL;
580 }
581
582
583 if (pdata->fps)
584 fps = pdata->fps;
585 if (pdata->txbuflen)
586 txbuflen = pdata->txbuflen;
587 if (pdata->display.init_sequence)
588 init_sequence = pdata->display.init_sequence;
589 if (pdata->gamma)
590 gamma = pdata->gamma;
591 if (pdata->display.debug)
592 display->debug = pdata->display.debug;
593 if (pdata->display.backlight)
594 display->backlight = pdata->display.backlight;
595 if (pdata->display.width)
596 display->width = pdata->display.width;
597 if (pdata->display.height)
598 display->height = pdata->display.height;
599 if (pdata->display.buswidth)
600 display->buswidth = pdata->display.buswidth;
601 if (pdata->display.regwidth)
602 display->regwidth = pdata->display.regwidth;
603
604 display->debug |= debug;
605 fbtft_expand_debug_value(&display->debug);
606
607 switch (pdata->rotate) {
608 case 90:
609 case 270:
610 width = display->height;
611 height = display->width;
612 break;
613 default:
614 width = display->width;
615 height = display->height;
616 }
617
618 vmem_size = display->width * display->height * bpp / 8;
619 vmem = vzalloc(vmem_size);
620 if (!vmem)
621 goto alloc_fail;
622
623 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
624 if (!fbops)
625 goto alloc_fail;
626
627 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
628 if (!fbdefio)
629 goto alloc_fail;
630
631 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
632 if (!buf)
633 goto alloc_fail;
634
635 if (display->gamma_num && display->gamma_len) {
636 gamma_curves = devm_kcalloc(dev,
637 display->gamma_num *
638 display->gamma_len,
639 sizeof(gamma_curves[0]),
640 GFP_KERNEL);
641 if (!gamma_curves)
642 goto alloc_fail;
643 }
644
645 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
646 if (!info)
647 goto alloc_fail;
648
649 info->screen_buffer = vmem;
650 info->fbops = fbops;
651 info->fbdefio = fbdefio;
652
653 fbops->owner = dev->driver->owner;
654 fbops->fb_read = fb_sys_read;
655 fbops->fb_write = fbtft_fb_write;
656 fbops->fb_fillrect = fbtft_fb_fillrect;
657 fbops->fb_copyarea = fbtft_fb_copyarea;
658 fbops->fb_imageblit = fbtft_fb_imageblit;
659 fbops->fb_setcolreg = fbtft_fb_setcolreg;
660 fbops->fb_blank = fbtft_fb_blank;
661
662 fbdefio->delay = HZ / fps;
663 fbdefio->deferred_io = fbtft_deferred_io;
664 fb_deferred_io_init(info);
665
666 snprintf(info->fix.id, sizeof(info->fix.id), "%s", dev->driver->name);
667 info->fix.type = FB_TYPE_PACKED_PIXELS;
668 info->fix.visual = FB_VISUAL_TRUECOLOR;
669 info->fix.xpanstep = 0;
670 info->fix.ypanstep = 0;
671 info->fix.ywrapstep = 0;
672 info->fix.line_length = width * bpp / 8;
673 info->fix.accel = FB_ACCEL_NONE;
674 info->fix.smem_len = vmem_size;
675
676 info->var.rotate = pdata->rotate;
677 info->var.xres = width;
678 info->var.yres = height;
679 info->var.xres_virtual = info->var.xres;
680 info->var.yres_virtual = info->var.yres;
681 info->var.bits_per_pixel = bpp;
682 info->var.nonstd = 1;
683
684
685 info->var.red.offset = 11;
686 info->var.red.length = 5;
687 info->var.green.offset = 5;
688 info->var.green.length = 6;
689 info->var.blue.offset = 0;
690 info->var.blue.length = 5;
691 info->var.transp.offset = 0;
692 info->var.transp.length = 0;
693
694 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
695
696 par = info->par;
697 par->info = info;
698 par->pdata = pdata;
699 par->debug = display->debug;
700 par->buf = buf;
701 spin_lock_init(&par->dirty_lock);
702 par->bgr = pdata->bgr;
703 par->startbyte = pdata->startbyte;
704 par->init_sequence = init_sequence;
705 par->gamma.curves = gamma_curves;
706 par->gamma.num_curves = display->gamma_num;
707 par->gamma.num_values = display->gamma_len;
708 mutex_init(&par->gamma.lock);
709 info->pseudo_palette = par->pseudo_palette;
710
711 if (par->gamma.curves && gamma) {
712 if (fbtft_gamma_parse_str(par, par->gamma.curves, gamma,
713 strlen(gamma)))
714 goto release_framebuf;
715 }
716
717
718 if (txbuflen == -1)
719 txbuflen = vmem_size + 2;
720 if (txbuflen >= vmem_size + 2)
721 txbuflen = 0;
722
723#ifdef __LITTLE_ENDIAN
724 if ((!txbuflen) && (bpp > 8))
725 txbuflen = PAGE_SIZE;
726#endif
727
728 if (txbuflen > 0) {
729 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
730 if (!txbuf)
731 goto release_framebuf;
732 par->txbuf.buf = txbuf;
733 par->txbuf.len = txbuflen;
734 }
735
736
737 par->fbtftops.write = fbtft_write_spi;
738 par->fbtftops.read = fbtft_read_spi;
739 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
740 par->fbtftops.write_register = fbtft_write_reg8_bus8;
741 par->fbtftops.set_addr_win = fbtft_set_addr_win;
742 par->fbtftops.reset = fbtft_reset;
743 par->fbtftops.mkdirty = fbtft_mkdirty;
744 par->fbtftops.update_display = fbtft_update_display;
745 if (display->backlight)
746 par->fbtftops.register_backlight = fbtft_register_backlight;
747
748
749 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
750
751 return info;
752
753release_framebuf:
754 framebuffer_release(info);
755
756alloc_fail:
757 vfree(vmem);
758
759 return NULL;
760}
761EXPORT_SYMBOL(fbtft_framebuffer_alloc);
762
763
764
765
766
767
768
769void fbtft_framebuffer_release(struct fb_info *info)
770{
771 fb_deferred_io_cleanup(info);
772 vfree(info->screen_buffer);
773 framebuffer_release(info);
774}
775EXPORT_SYMBOL(fbtft_framebuffer_release);
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790int fbtft_register_framebuffer(struct fb_info *fb_info)
791{
792 int ret;
793 char text1[50] = "";
794 char text2[50] = "";
795 struct fbtft_par *par = fb_info->par;
796 struct spi_device *spi = par->spi;
797
798
799 if (!par->fbtftops.init_display) {
800 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
801 return -EINVAL;
802 }
803
804 if (spi)
805 spi_set_drvdata(spi, fb_info);
806 if (par->pdev)
807 platform_set_drvdata(par->pdev, fb_info);
808
809 ret = par->fbtftops.request_gpios(par);
810 if (ret < 0)
811 goto reg_fail;
812
813 if (par->fbtftops.verify_gpios) {
814 ret = par->fbtftops.verify_gpios(par);
815 if (ret < 0)
816 goto reg_fail;
817 }
818
819 ret = par->fbtftops.init_display(par);
820 if (ret < 0)
821 goto reg_fail;
822 if (par->fbtftops.set_var) {
823 ret = par->fbtftops.set_var(par);
824 if (ret < 0)
825 goto reg_fail;
826 }
827
828
829 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
830
831 if (par->fbtftops.set_gamma && par->gamma.curves) {
832 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
833 if (ret)
834 goto reg_fail;
835 }
836
837 if (par->fbtftops.register_backlight)
838 par->fbtftops.register_backlight(par);
839
840 ret = register_framebuffer(fb_info);
841 if (ret < 0)
842 goto reg_fail;
843
844 fbtft_sysfs_init(par);
845
846 if (par->txbuf.buf && par->txbuf.len >= 1024)
847 sprintf(text1, ", %zu KiB buffer memory", par->txbuf.len >> 10);
848 if (spi)
849 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
850 spi->chip_select, spi->max_speed_hz / 1000000);
851 dev_info(fb_info->dev,
852 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
853 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
854 fb_info->fix.smem_len >> 10, text1,
855 HZ / fb_info->fbdefio->delay, text2);
856
857#ifdef CONFIG_FB_BACKLIGHT
858
859 if (fb_info->bl_dev) {
860 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
861 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
862 }
863#endif
864
865 return 0;
866
867reg_fail:
868 if (par->fbtftops.unregister_backlight)
869 par->fbtftops.unregister_backlight(par);
870
871 return ret;
872}
873EXPORT_SYMBOL(fbtft_register_framebuffer);
874
875
876
877
878
879
880
881
882
883
884int fbtft_unregister_framebuffer(struct fb_info *fb_info)
885{
886 struct fbtft_par *par = fb_info->par;
887
888 if (par->fbtftops.unregister_backlight)
889 par->fbtftops.unregister_backlight(par);
890 fbtft_sysfs_exit(par);
891 unregister_framebuffer(fb_info);
892
893 return 0;
894}
895EXPORT_SYMBOL(fbtft_unregister_framebuffer);
896
897
898
899
900
901
902
903static int fbtft_init_display_from_property(struct fbtft_par *par)
904{
905 struct device *dev = par->info->device;
906 int buf[64], count, index, i, j, ret;
907 u32 *values;
908 u32 val;
909
910 count = device_property_count_u32(dev, "init");
911 if (count < 0)
912 return count;
913 if (count == 0)
914 return -EINVAL;
915
916 values = kmalloc_array(count + 1, sizeof(*values), GFP_KERNEL);
917 if (!values)
918 return -ENOMEM;
919
920 ret = device_property_read_u32_array(dev, "init", values, count);
921 if (ret)
922 goto out_free;
923
924 par->fbtftops.reset(par);
925 if (par->gpio.cs)
926 gpiod_set_value(par->gpio.cs, 0);
927
928 index = -1;
929 val = values[++index];
930
931 while (index < count) {
932 if (val & FBTFT_OF_INIT_CMD) {
933 val &= 0xFFFF;
934 i = 0;
935 while ((index < count) && !(val & 0xFFFF0000)) {
936 if (i > 63) {
937 dev_err(dev,
938 "%s: Maximum register values exceeded\n",
939 __func__);
940 ret = -EINVAL;
941 goto out_free;
942 }
943 buf[i++] = val;
944 val = values[++index];
945 }
946
947 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
948 "init: write_register:\n");
949 for (j = 0; j < i; j++)
950 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
951 "buf[%d] = %02X\n", j, buf[j]);
952
953 par->fbtftops.write_register(par, i,
954 buf[0], buf[1], buf[2], buf[3],
955 buf[4], buf[5], buf[6], buf[7],
956 buf[8], buf[9], buf[10], buf[11],
957 buf[12], buf[13], buf[14], buf[15],
958 buf[16], buf[17], buf[18], buf[19],
959 buf[20], buf[21], buf[22], buf[23],
960 buf[24], buf[25], buf[26], buf[27],
961 buf[28], buf[29], buf[30], buf[31],
962 buf[32], buf[33], buf[34], buf[35],
963 buf[36], buf[37], buf[38], buf[39],
964 buf[40], buf[41], buf[42], buf[43],
965 buf[44], buf[45], buf[46], buf[47],
966 buf[48], buf[49], buf[50], buf[51],
967 buf[52], buf[53], buf[54], buf[55],
968 buf[56], buf[57], buf[58], buf[59],
969 buf[60], buf[61], buf[62], buf[63]);
970 } else if (val & FBTFT_OF_INIT_DELAY) {
971 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
972 "init: msleep(%u)\n", val & 0xFFFF);
973 msleep(val & 0xFFFF);
974 val = values[++index];
975 } else {
976 dev_err(dev, "illegal init value 0x%X\n", val);
977 ret = -EINVAL;
978 goto out_free;
979 }
980 }
981
982out_free:
983 kfree(values);
984 return ret;
985}
986
987
988
989
990
991
992
993
994
995int fbtft_init_display(struct fbtft_par *par)
996{
997 int buf[64];
998 char msg[128];
999 char str[16];
1000 int i = 0;
1001 int j;
1002
1003
1004 if (!par->init_sequence) {
1005 dev_err(par->info->device,
1006 "error: init_sequence is not set\n");
1007 return -EINVAL;
1008 }
1009
1010
1011 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++)
1012 if (par->init_sequence[i] == -3)
1013 break;
1014 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1015 dev_err(par->info->device,
1016 "missing stop marker at end of init sequence\n");
1017 return -EINVAL;
1018 }
1019
1020 par->fbtftops.reset(par);
1021 if (par->gpio.cs)
1022 gpiod_set_value(par->gpio.cs, 0);
1023
1024 i = 0;
1025 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1026 if (par->init_sequence[i] == -3) {
1027
1028 return 0;
1029 }
1030 if (par->init_sequence[i] >= 0) {
1031 dev_err(par->info->device,
1032 "missing delimiter at position %d\n", i);
1033 return -EINVAL;
1034 }
1035 if (par->init_sequence[i + 1] < 0) {
1036 dev_err(par->info->device,
1037 "missing value after delimiter %d at position %d\n",
1038 par->init_sequence[i], i);
1039 return -EINVAL;
1040 }
1041 switch (par->init_sequence[i]) {
1042 case -1:
1043 i++;
1044
1045 strcpy(msg, "");
1046 j = i + 1;
1047 while (par->init_sequence[j] >= 0) {
1048 sprintf(str, "0x%02X ", par->init_sequence[j]);
1049 strcat(msg, str);
1050 j++;
1051 }
1052 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1053 "init: write(0x%02X) %s\n",
1054 par->init_sequence[i], msg);
1055
1056
1057 j = 0;
1058 while (par->init_sequence[i] >= 0) {
1059 if (j > 63) {
1060 dev_err(par->info->device,
1061 "%s: Maximum register values exceeded\n",
1062 __func__);
1063 return -EINVAL;
1064 }
1065 buf[j++] = par->init_sequence[i++];
1066 }
1067 par->fbtftops.write_register(par, j,
1068 buf[0], buf[1], buf[2], buf[3],
1069 buf[4], buf[5], buf[6], buf[7],
1070 buf[8], buf[9], buf[10], buf[11],
1071 buf[12], buf[13], buf[14], buf[15],
1072 buf[16], buf[17], buf[18], buf[19],
1073 buf[20], buf[21], buf[22], buf[23],
1074 buf[24], buf[25], buf[26], buf[27],
1075 buf[28], buf[29], buf[30], buf[31],
1076 buf[32], buf[33], buf[34], buf[35],
1077 buf[36], buf[37], buf[38], buf[39],
1078 buf[40], buf[41], buf[42], buf[43],
1079 buf[44], buf[45], buf[46], buf[47],
1080 buf[48], buf[49], buf[50], buf[51],
1081 buf[52], buf[53], buf[54], buf[55],
1082 buf[56], buf[57], buf[58], buf[59],
1083 buf[60], buf[61], buf[62], buf[63]);
1084 break;
1085 case -2:
1086 i++;
1087 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1088 "init: mdelay(%d)\n",
1089 par->init_sequence[i]);
1090 mdelay(par->init_sequence[i++]);
1091 break;
1092 default:
1093 dev_err(par->info->device,
1094 "unknown delimiter %d at position %d\n",
1095 par->init_sequence[i], i);
1096 return -EINVAL;
1097 }
1098 }
1099
1100 dev_err(par->info->device,
1101 "%s: something is wrong. Shouldn't get here.\n", __func__);
1102 return -EINVAL;
1103}
1104EXPORT_SYMBOL(fbtft_init_display);
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114static int fbtft_verify_gpios(struct fbtft_par *par)
1115{
1116 struct fbtft_platform_data *pdata = par->pdata;
1117 int i;
1118
1119 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1120
1121 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
1122 !par->gpio.dc) {
1123 dev_err(par->info->device,
1124 "Missing info about 'dc' gpio. Aborting.\n");
1125 return -EINVAL;
1126 }
1127
1128 if (!par->pdev)
1129 return 0;
1130
1131 if (!par->gpio.wr) {
1132 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1133 return -EINVAL;
1134 }
1135 for (i = 0; i < pdata->display.buswidth; i++) {
1136 if (!par->gpio.db[i]) {
1137 dev_err(par->info->device,
1138 "Missing 'db%02d' gpio. Aborting.\n", i);
1139 return -EINVAL;
1140 }
1141 }
1142
1143 return 0;
1144}
1145
1146
1147static u32 fbtft_property_value(struct device *dev, const char *propname)
1148{
1149 int ret;
1150 u32 val = 0;
1151
1152 ret = device_property_read_u32(dev, propname, &val);
1153 if (ret == 0)
1154 dev_info(dev, "%s: %s = %u\n", __func__, propname, val);
1155
1156 return val;
1157}
1158
1159static struct fbtft_platform_data *fbtft_properties_read(struct device *dev)
1160{
1161 struct fbtft_platform_data *pdata;
1162
1163 if (!dev_fwnode(dev)) {
1164 dev_err(dev, "Missing platform data or properties\n");
1165 return ERR_PTR(-EINVAL);
1166 }
1167
1168 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1169 if (!pdata)
1170 return ERR_PTR(-ENOMEM);
1171
1172 pdata->display.width = fbtft_property_value(dev, "width");
1173 pdata->display.height = fbtft_property_value(dev, "height");
1174 pdata->display.regwidth = fbtft_property_value(dev, "regwidth");
1175 pdata->display.buswidth = fbtft_property_value(dev, "buswidth");
1176 pdata->display.backlight = fbtft_property_value(dev, "backlight");
1177 pdata->display.bpp = fbtft_property_value(dev, "bpp");
1178 pdata->display.debug = fbtft_property_value(dev, "debug");
1179 pdata->rotate = fbtft_property_value(dev, "rotate");
1180 pdata->bgr = device_property_read_bool(dev, "bgr");
1181 pdata->fps = fbtft_property_value(dev, "fps");
1182 pdata->txbuflen = fbtft_property_value(dev, "txbuflen");
1183 pdata->startbyte = fbtft_property_value(dev, "startbyte");
1184 device_property_read_string(dev, "gamma", (const char **)&pdata->gamma);
1185
1186 if (device_property_present(dev, "led-gpios"))
1187 pdata->display.backlight = 1;
1188 if (device_property_present(dev, "init"))
1189 pdata->display.fbtftops.init_display = fbtft_init_display_from_property;
1190 pdata->display.fbtftops.request_gpios = fbtft_request_gpios;
1191
1192 return pdata;
1193}
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207int fbtft_probe_common(struct fbtft_display *display,
1208 struct spi_device *sdev,
1209 struct platform_device *pdev)
1210{
1211 struct device *dev;
1212 struct fb_info *info;
1213 struct fbtft_par *par;
1214 struct fbtft_platform_data *pdata;
1215 int ret;
1216
1217 if (sdev)
1218 dev = &sdev->dev;
1219 else
1220 dev = &pdev->dev;
1221
1222 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1223 dev_info(dev, "%s()\n", __func__);
1224
1225 pdata = dev->platform_data;
1226 if (!pdata) {
1227 pdata = fbtft_properties_read(dev);
1228 if (IS_ERR(pdata))
1229 return PTR_ERR(pdata);
1230 }
1231
1232 info = fbtft_framebuffer_alloc(display, dev, pdata);
1233 if (!info)
1234 return -ENOMEM;
1235
1236 par = info->par;
1237 par->spi = sdev;
1238 par->pdev = pdev;
1239
1240 if (display->buswidth == 0) {
1241 dev_err(dev, "buswidth is not set\n");
1242 return -EINVAL;
1243 }
1244
1245
1246 if (display->regwidth == 8 && display->buswidth == 8)
1247 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1248 else if (display->regwidth == 8 && display->buswidth == 9 && par->spi)
1249 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1250 else if (display->regwidth == 16 && display->buswidth == 8)
1251 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1252 else if (display->regwidth == 16 && display->buswidth == 16)
1253 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1254 else
1255 dev_warn(dev,
1256 "no default functions for regwidth=%d and buswidth=%d\n",
1257 display->regwidth, display->buswidth);
1258
1259
1260 if (display->buswidth == 8)
1261 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1262 else if (display->buswidth == 9)
1263 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1264 else if (display->buswidth == 16)
1265 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1266
1267
1268 if (par->pdev) {
1269 if (display->buswidth == 8)
1270 par->fbtftops.write = fbtft_write_gpio8_wr;
1271 else if (display->buswidth == 16)
1272 par->fbtftops.write = fbtft_write_gpio16_wr;
1273 }
1274
1275
1276 if (par->spi && display->buswidth == 9) {
1277 if (par->spi->master->bits_per_word_mask & SPI_BPW_MASK(9)) {
1278 par->spi->bits_per_word = 9;
1279 } else {
1280 dev_warn(&par->spi->dev,
1281 "9-bit SPI not available, emulating using 8-bit.\n");
1282
1283 par->extra = devm_kzalloc(par->info->device,
1284 par->txbuf.len +
1285 (par->txbuf.len / 8) + 8,
1286 GFP_KERNEL);
1287 if (!par->extra) {
1288 ret = -ENOMEM;
1289 goto out_release;
1290 }
1291 par->fbtftops.write = fbtft_write_spi_emulate_9;
1292 }
1293 }
1294
1295 if (!par->fbtftops.verify_gpios)
1296 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1297
1298
1299 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1300
1301
1302 if (par->init_sequence)
1303 par->fbtftops.init_display = fbtft_init_display;
1304
1305
1306 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1307
1308 ret = fbtft_register_framebuffer(info);
1309 if (ret < 0)
1310 goto out_release;
1311
1312 return 0;
1313
1314out_release:
1315 fbtft_framebuffer_release(info);
1316
1317 return ret;
1318}
1319EXPORT_SYMBOL(fbtft_probe_common);
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330int fbtft_remove_common(struct device *dev, struct fb_info *info)
1331{
1332 struct fbtft_par *par;
1333
1334 if (!info)
1335 return -EINVAL;
1336 par = info->par;
1337 if (par)
1338 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1339 "%s()\n", __func__);
1340 fbtft_unregister_framebuffer(info);
1341 fbtft_framebuffer_release(info);
1342
1343 return 0;
1344}
1345EXPORT_SYMBOL(fbtft_remove_common);
1346
1347MODULE_LICENSE("GPL");
1348