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