2 * Copyright (C) 2013 Noralf Tronnes
4 * This driver is inspired by:
5 * st7735fb.c, Copyright (C) 2011, Matt Porter
6 * broadsheetfb.c, Copyright (C) 2008, Jaya Kumar
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/string.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
28 #include <linux/gpio.h>
29 #include <linux/spi/spi.h>
30 #include <linux/delay.h>
31 #include <linux/uaccess.h>
32 #include <linux/backlight.h>
33 #include <linux/platform_device.h>
34 #include <linux/spinlock.h>
35 #include <linux/dma-mapping.h>
37 #include <linux/of_gpio.h>
38 #include <video/mipi_display.h>
43 static unsigned long debug;
44 module_param(debug, ulong, 0);
45 MODULE_PARM_DESC(debug, "override device debug level");
48 static bool dma = true;
49 module_param(dma, bool, 0);
50 MODULE_PARM_DESC(dma, "Use DMA buffer");
53 void fbtft_dbg_hex(const struct device *dev, int groupsize,
54 void *buf, size_t len, const char *fmt, ...)
57 static char textbuf[512];
62 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
65 hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len,
66 512 - text_len, false);
69 dev_info(dev, "%s ...\n", text);
71 dev_info(dev, "%s\n", text);
73 EXPORT_SYMBOL(fbtft_dbg_hex);
75 static unsigned long fbtft_request_gpios_match(struct fbtft_par *par,
76 const struct fbtft_gpio *gpio)
81 fbtft_par_dbg(DEBUG_REQUEST_GPIOS_MATCH, par, "%s('%s')\n",
82 __func__, gpio->name);
84 if (strcasecmp(gpio->name, "reset") == 0) {
85 par->gpio.reset = gpio->gpio;
86 return GPIOF_OUT_INIT_HIGH;
87 } else if (strcasecmp(gpio->name, "dc") == 0) {
88 par->gpio.dc = gpio->gpio;
89 return GPIOF_OUT_INIT_LOW;
90 } else if (strcasecmp(gpio->name, "cs") == 0) {
91 par->gpio.cs = gpio->gpio;
92 return GPIOF_OUT_INIT_HIGH;
93 } else if (strcasecmp(gpio->name, "wr") == 0) {
94 par->gpio.wr = gpio->gpio;
95 return GPIOF_OUT_INIT_HIGH;
96 } else if (strcasecmp(gpio->name, "rd") == 0) {
97 par->gpio.rd = gpio->gpio;
98 return GPIOF_OUT_INIT_HIGH;
99 } else if (strcasecmp(gpio->name, "latch") == 0) {
100 par->gpio.latch = gpio->gpio;
101 return GPIOF_OUT_INIT_LOW;
102 } else if (gpio->name[0] == 'd' && gpio->name[1] == 'b') {
103 ret = kstrtol(&gpio->name[2], 10, &val);
104 if (ret == 0 && val < 16) {
105 par->gpio.db[val] = gpio->gpio;
106 return GPIOF_OUT_INIT_LOW;
108 } else if (strcasecmp(gpio->name, "led") == 0) {
109 par->gpio.led[0] = gpio->gpio;
110 return GPIOF_OUT_INIT_LOW;
111 } else if (strcasecmp(gpio->name, "led_") == 0) {
112 par->gpio.led[0] = gpio->gpio;
113 return GPIOF_OUT_INIT_HIGH;
116 return FBTFT_GPIO_NO_MATCH;
119 static int fbtft_request_gpios(struct fbtft_par *par)
121 struct fbtft_platform_data *pdata = par->pdata;
122 const struct fbtft_gpio *gpio;
126 if (!(pdata && pdata->gpios))
130 while (gpio->name[0]) {
131 flags = FBTFT_GPIO_NO_MATCH;
132 /* if driver provides match function, try it first,
133 * if no match use our own
135 if (par->fbtftops.request_gpios_match)
136 flags = par->fbtftops.request_gpios_match(par, gpio);
137 if (flags == FBTFT_GPIO_NO_MATCH)
138 flags = fbtft_request_gpios_match(par, gpio);
139 if (flags != FBTFT_GPIO_NO_MATCH) {
140 ret = devm_gpio_request_one(par->info->device,
142 par->info->device->driver->name);
144 dev_err(par->info->device,
145 "%s: gpio_request_one('%s'=%d) failed with %d\n",
146 __func__, gpio->name,
150 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par,
151 "%s: '%s' = GPIO%d\n",
152 __func__, gpio->name, gpio->gpio);
161 static int fbtft_request_one_gpio(struct fbtft_par *par,
162 const char *name, int index, int *gpiop)
164 struct device *dev = par->info->device;
165 struct device_node *node = dev->of_node;
166 int gpio, flags, ret = 0;
167 enum of_gpio_flags of_flags;
169 if (of_find_property(node, name, NULL)) {
170 gpio = of_get_named_gpio_flags(node, name, index, &of_flags);
173 if (gpio == -EPROBE_DEFER)
177 "failed to get '%s' from DT\n", name);
181 /* active low translates to initially low */
182 flags = (of_flags & OF_GPIO_ACTIVE_LOW) ? GPIOF_OUT_INIT_LOW :
184 ret = devm_gpio_request_one(dev, gpio, flags,
188 "gpio_request_one('%s'=%d) failed with %d\n",
194 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par, "%s: '%s' = GPIO%d\n",
195 __func__, name, gpio);
201 static int fbtft_request_gpios_dt(struct fbtft_par *par)
206 if (!par->info->device->of_node)
209 ret = fbtft_request_one_gpio(par, "reset-gpios", 0, &par->gpio.reset);
212 ret = fbtft_request_one_gpio(par, "dc-gpios", 0, &par->gpio.dc);
215 ret = fbtft_request_one_gpio(par, "rd-gpios", 0, &par->gpio.rd);
218 ret = fbtft_request_one_gpio(par, "wr-gpios", 0, &par->gpio.wr);
221 ret = fbtft_request_one_gpio(par, "cs-gpios", 0, &par->gpio.cs);
224 ret = fbtft_request_one_gpio(par, "latch-gpios", 0, &par->gpio.latch);
227 for (i = 0; i < 16; i++) {
228 ret = fbtft_request_one_gpio(par, "db-gpios", i,
232 ret = fbtft_request_one_gpio(par, "led-gpios", i,
236 ret = fbtft_request_one_gpio(par, "aux-gpios", i,
246 #ifdef CONFIG_FB_BACKLIGHT
247 static int fbtft_backlight_update_status(struct backlight_device *bd)
249 struct fbtft_par *par = bl_get_data(bd);
250 bool polarity = !!(bd->props.state & BL_CORE_DRIVER1);
252 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
253 "%s: polarity=%d, power=%d, fb_blank=%d\n",
254 __func__, polarity, bd->props.power, bd->props.fb_blank);
256 if ((bd->props.power == FB_BLANK_UNBLANK) && (bd->props.fb_blank == FB_BLANK_UNBLANK))
257 gpio_set_value(par->gpio.led[0], polarity);
259 gpio_set_value(par->gpio.led[0], !polarity);
264 static int fbtft_backlight_get_brightness(struct backlight_device *bd)
266 return bd->props.brightness;
269 void fbtft_unregister_backlight(struct fbtft_par *par)
271 if (par->info->bl_dev) {
272 par->info->bl_dev->props.power = FB_BLANK_POWERDOWN;
273 backlight_update_status(par->info->bl_dev);
274 backlight_device_unregister(par->info->bl_dev);
275 par->info->bl_dev = NULL;
279 static const struct backlight_ops fbtft_bl_ops = {
280 .get_brightness = fbtft_backlight_get_brightness,
281 .update_status = fbtft_backlight_update_status,
284 void fbtft_register_backlight(struct fbtft_par *par)
286 struct backlight_device *bd;
287 struct backlight_properties bl_props = { 0, };
289 if (par->gpio.led[0] == -1) {
290 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
291 "%s(): led pin not set, exiting.\n", __func__);
295 bl_props.type = BACKLIGHT_RAW;
296 /* Assume backlight is off, get polarity from current state of pin */
297 bl_props.power = FB_BLANK_POWERDOWN;
298 if (!gpio_get_value(par->gpio.led[0]))
299 bl_props.state |= BL_CORE_DRIVER1;
301 bd = backlight_device_register(dev_driver_string(par->info->device),
302 par->info->device, par, &fbtft_bl_ops, &bl_props);
304 dev_err(par->info->device,
305 "cannot register backlight device (%ld)\n",
309 par->info->bl_dev = bd;
311 if (!par->fbtftops.unregister_backlight)
312 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
315 void fbtft_register_backlight(struct fbtft_par *par) { };
316 void fbtft_unregister_backlight(struct fbtft_par *par) { };
318 EXPORT_SYMBOL(fbtft_register_backlight);
319 EXPORT_SYMBOL(fbtft_unregister_backlight);
321 static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
324 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
325 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
327 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
328 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
330 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
333 static void fbtft_reset(struct fbtft_par *par)
335 if (par->gpio.reset == -1)
337 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
338 gpio_set_value(par->gpio.reset, 0);
340 gpio_set_value(par->gpio.reset, 1);
344 static void fbtft_update_display(struct fbtft_par *par, unsigned start_line,
348 ktime_t ts_start, ts_end;
349 long fps, throughput;
353 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE | DEBUG_TIME_EACH_UPDATE))) {
354 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
355 ((par->debug & DEBUG_TIME_FIRST_UPDATE) && !par->first_update_done)) {
356 ts_start = ktime_get();
362 if (start_line > end_line) {
363 dev_warn(par->info->device,
364 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
365 __func__, start_line, end_line);
367 end_line = par->info->var.yres - 1;
369 if (start_line > par->info->var.yres - 1 || end_line > par->info->var.yres - 1) {
370 dev_warn(par->info->device,
371 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
372 __func__, start_line, end_line, par->info->var.yres - 1);
374 end_line = par->info->var.yres - 1;
377 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
378 __func__, start_line, end_line);
380 if (par->fbtftops.set_addr_win)
381 par->fbtftops.set_addr_win(par, 0, start_line,
382 par->info->var.xres - 1, end_line);
384 offset = start_line * par->info->fix.line_length;
385 len = (end_line - start_line + 1) * par->info->fix.line_length;
386 ret = par->fbtftops.write_vmem(par, offset, len);
388 dev_err(par->info->device,
389 "%s: write_vmem failed to update display buffer\n",
392 if (unlikely(timeit)) {
393 ts_end = ktime_get();
394 if (ktime_to_ns(par->update_time))
395 par->update_time = ts_start;
397 par->update_time = ts_start;
398 fps = ktime_us_delta(ts_start, par->update_time);
399 fps = fps ? 1000000 / fps : 0;
401 throughput = ktime_us_delta(ts_end, ts_start);
402 throughput = throughput ? (len * 1000) / throughput : 0;
403 throughput = throughput * 1000 / 1024;
405 dev_info(par->info->device,
406 "Display update: %ld kB/s, fps=%ld\n",
408 par->first_update_done = true;
412 static void fbtft_mkdirty(struct fb_info *info, int y, int height)
414 struct fbtft_par *par = info->par;
415 struct fb_deferred_io *fbdefio = info->fbdefio;
417 /* special case, needed ? */
420 height = info->var.yres - 1;
423 /* Mark display lines/area as dirty */
424 spin_lock(&par->dirty_lock);
425 if (y < par->dirty_lines_start)
426 par->dirty_lines_start = y;
427 if (y + height - 1 > par->dirty_lines_end)
428 par->dirty_lines_end = y + height - 1;
429 spin_unlock(&par->dirty_lock);
431 /* Schedule deferred_io to update display (no-op if already on queue)*/
432 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
435 static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
437 struct fbtft_par *par = info->par;
438 unsigned dirty_lines_start, dirty_lines_end;
441 unsigned y_low = 0, y_high = 0;
444 spin_lock(&par->dirty_lock);
445 dirty_lines_start = par->dirty_lines_start;
446 dirty_lines_end = par->dirty_lines_end;
447 /* set display line markers as clean */
448 par->dirty_lines_start = par->info->var.yres - 1;
449 par->dirty_lines_end = 0;
450 spin_unlock(&par->dirty_lock);
452 /* Mark display lines as dirty */
453 list_for_each_entry(page, pagelist, lru) {
455 index = page->index << PAGE_SHIFT;
456 y_low = index / info->fix.line_length;
457 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
458 dev_dbg(info->device,
459 "page->index=%lu y_low=%d y_high=%d\n",
460 page->index, y_low, y_high);
461 if (y_high > info->var.yres - 1)
462 y_high = info->var.yres - 1;
463 if (y_low < dirty_lines_start)
464 dirty_lines_start = y_low;
465 if (y_high > dirty_lines_end)
466 dirty_lines_end = y_high;
469 par->fbtftops.update_display(info->par,
470 dirty_lines_start, dirty_lines_end);
473 static void fbtft_fb_fillrect(struct fb_info *info,
474 const struct fb_fillrect *rect)
476 struct fbtft_par *par = info->par;
479 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
480 __func__, rect->dx, rect->dy, rect->width, rect->height);
481 sys_fillrect(info, rect);
483 par->fbtftops.mkdirty(info, rect->dy, rect->height);
486 static void fbtft_fb_copyarea(struct fb_info *info,
487 const struct fb_copyarea *area)
489 struct fbtft_par *par = info->par;
492 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
493 __func__, area->dx, area->dy, area->width, area->height);
494 sys_copyarea(info, area);
496 par->fbtftops.mkdirty(info, area->dy, area->height);
499 static void fbtft_fb_imageblit(struct fb_info *info,
500 const struct fb_image *image)
502 struct fbtft_par *par = info->par;
505 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
506 __func__, image->dx, image->dy, image->width, image->height);
507 sys_imageblit(info, image);
509 par->fbtftops.mkdirty(info, image->dy, image->height);
512 static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
513 size_t count, loff_t *ppos)
515 struct fbtft_par *par = info->par;
519 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
520 res = fb_sys_write(info, buf, count, ppos);
522 /* TODO: only mark changed area update all for now */
523 par->fbtftops.mkdirty(info, -1, 0);
529 static unsigned int chan_to_field(unsigned chan, struct fb_bitfield *bf)
532 chan >>= 16 - bf->length;
533 return chan << bf->offset;
536 static int fbtft_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
537 unsigned blue, unsigned transp,
538 struct fb_info *info)
544 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
545 __func__, regno, red, green, blue, transp);
547 switch (info->fix.visual) {
548 case FB_VISUAL_TRUECOLOR:
550 u32 *pal = info->pseudo_palette;
552 val = chan_to_field(red, &info->var.red);
553 val |= chan_to_field(green, &info->var.green);
554 val |= chan_to_field(blue, &info->var.blue);
565 static int fbtft_fb_blank(int blank, struct fb_info *info)
567 struct fbtft_par *par = info->par;
570 dev_dbg(info->dev, "%s(blank=%d)\n",
573 if (!par->fbtftops.blank)
577 case FB_BLANK_POWERDOWN:
578 case FB_BLANK_VSYNC_SUSPEND:
579 case FB_BLANK_HSYNC_SUSPEND:
580 case FB_BLANK_NORMAL:
581 ret = par->fbtftops.blank(par, true);
583 case FB_BLANK_UNBLANK:
584 ret = par->fbtftops.blank(par, false);
590 static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
593 dst->write = src->write;
595 dst->read = src->read;
597 dst->write_vmem = src->write_vmem;
598 if (src->write_register)
599 dst->write_register = src->write_register;
600 if (src->set_addr_win)
601 dst->set_addr_win = src->set_addr_win;
603 dst->reset = src->reset;
605 dst->mkdirty = src->mkdirty;
606 if (src->update_display)
607 dst->update_display = src->update_display;
608 if (src->init_display)
609 dst->init_display = src->init_display;
611 dst->blank = src->blank;
612 if (src->request_gpios_match)
613 dst->request_gpios_match = src->request_gpios_match;
614 if (src->request_gpios)
615 dst->request_gpios = src->request_gpios;
616 if (src->verify_gpios)
617 dst->verify_gpios = src->verify_gpios;
618 if (src->register_backlight)
619 dst->register_backlight = src->register_backlight;
620 if (src->unregister_backlight)
621 dst->unregister_backlight = src->unregister_backlight;
623 dst->set_var = src->set_var;
625 dst->set_gamma = src->set_gamma;
629 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
631 * @display: pointer to structure describing the display
632 * @dev: pointer to the device for this fb, this can be NULL
634 * Creates a new frame buffer info structure.
636 * Also creates and populates the following structures:
639 * info->pseudo_palette
643 * Returns the new structure, or NULL if an error occurred.
646 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
648 struct fbtft_platform_data *pdata)
650 struct fb_info *info;
651 struct fbtft_par *par;
652 struct fb_ops *fbops = NULL;
653 struct fb_deferred_io *fbdefio = NULL;
659 int txbuflen = display->txbuflen;
660 unsigned bpp = display->bpp;
661 unsigned fps = display->fps;
663 int *init_sequence = display->init_sequence;
664 char *gamma = display->gamma;
665 unsigned long *gamma_curves = NULL;
668 if (display->gamma_num * display->gamma_len > FBTFT_GAMMA_MAX_VALUES_TOTAL) {
669 dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
670 FBTFT_GAMMA_MAX_VALUES_TOTAL);
681 dev_err(dev, "platform data is missing\n");
685 /* override driver values? */
689 txbuflen = pdata->txbuflen;
690 if (pdata->display.init_sequence)
691 init_sequence = pdata->display.init_sequence;
693 gamma = pdata->gamma;
694 if (pdata->display.debug)
695 display->debug = pdata->display.debug;
696 if (pdata->display.backlight)
697 display->backlight = pdata->display.backlight;
698 if (pdata->display.width)
699 display->width = pdata->display.width;
700 if (pdata->display.height)
701 display->height = pdata->display.height;
702 if (pdata->display.buswidth)
703 display->buswidth = pdata->display.buswidth;
704 if (pdata->display.regwidth)
705 display->regwidth = pdata->display.regwidth;
707 display->debug |= debug;
708 fbtft_expand_debug_value(&display->debug);
710 switch (pdata->rotate) {
713 width = display->height;
714 height = display->width;
717 width = display->width;
718 height = display->height;
721 vmem_size = display->width * display->height * bpp / 8;
722 vmem = vzalloc(vmem_size);
726 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
730 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
734 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
738 if (display->gamma_num && display->gamma_len) {
739 gamma_curves = devm_kcalloc(dev,
742 sizeof(gamma_curves[0]),
748 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
752 info->screen_buffer = vmem;
754 info->fbdefio = fbdefio;
756 fbops->owner = dev->driver->owner;
757 fbops->fb_read = fb_sys_read;
758 fbops->fb_write = fbtft_fb_write;
759 fbops->fb_fillrect = fbtft_fb_fillrect;
760 fbops->fb_copyarea = fbtft_fb_copyarea;
761 fbops->fb_imageblit = fbtft_fb_imageblit;
762 fbops->fb_setcolreg = fbtft_fb_setcolreg;
763 fbops->fb_blank = fbtft_fb_blank;
765 fbdefio->delay = HZ/fps;
766 fbdefio->deferred_io = fbtft_deferred_io;
767 fb_deferred_io_init(info);
769 strncpy(info->fix.id, dev->driver->name, 16);
770 info->fix.type = FB_TYPE_PACKED_PIXELS;
771 info->fix.visual = FB_VISUAL_TRUECOLOR;
772 info->fix.xpanstep = 0;
773 info->fix.ypanstep = 0;
774 info->fix.ywrapstep = 0;
775 info->fix.line_length = width * bpp / 8;
776 info->fix.accel = FB_ACCEL_NONE;
777 info->fix.smem_len = vmem_size;
779 info->var.rotate = pdata->rotate;
780 info->var.xres = width;
781 info->var.yres = height;
782 info->var.xres_virtual = info->var.xres;
783 info->var.yres_virtual = info->var.yres;
784 info->var.bits_per_pixel = bpp;
785 info->var.nonstd = 1;
788 info->var.red.offset = 11;
789 info->var.red.length = 5;
790 info->var.green.offset = 5;
791 info->var.green.length = 6;
792 info->var.blue.offset = 0;
793 info->var.blue.length = 5;
794 info->var.transp.offset = 0;
795 info->var.transp.length = 0;
797 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
802 par->debug = display->debug;
804 spin_lock_init(&par->dirty_lock);
805 par->bgr = pdata->bgr;
806 par->startbyte = pdata->startbyte;
807 par->init_sequence = init_sequence;
808 par->gamma.curves = gamma_curves;
809 par->gamma.num_curves = display->gamma_num;
810 par->gamma.num_values = display->gamma_len;
811 mutex_init(&par->gamma.lock);
812 info->pseudo_palette = par->pseudo_palette;
814 if (par->gamma.curves && gamma) {
815 if (fbtft_gamma_parse_str(par,
816 par->gamma.curves, gamma, strlen(gamma)))
820 /* Transmit buffer */
822 txbuflen = vmem_size + 2; /* add in case startbyte is used */
824 #ifdef __LITTLE_ENDIAN
825 if ((!txbuflen) && (bpp > 8))
826 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
830 #ifdef CONFIG_HAS_DMA
832 dev->coherent_dma_mask = ~0;
833 txbuf = dmam_alloc_coherent(dev, txbuflen, &par->txbuf.dma, GFP_DMA);
837 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
841 par->txbuf.buf = txbuf;
842 par->txbuf.len = txbuflen;
845 /* Initialize gpios to disabled */
846 par->gpio.reset = -1;
851 par->gpio.latch = -1;
852 for (i = 0; i < 16; i++) {
853 par->gpio.db[i] = -1;
854 par->gpio.led[i] = -1;
855 par->gpio.aux[i] = -1;
858 /* default fbtft operations */
859 par->fbtftops.write = fbtft_write_spi;
860 par->fbtftops.read = fbtft_read_spi;
861 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
862 par->fbtftops.write_register = fbtft_write_reg8_bus8;
863 par->fbtftops.set_addr_win = fbtft_set_addr_win;
864 par->fbtftops.reset = fbtft_reset;
865 par->fbtftops.mkdirty = fbtft_mkdirty;
866 par->fbtftops.update_display = fbtft_update_display;
867 par->fbtftops.request_gpios = fbtft_request_gpios;
868 if (display->backlight)
869 par->fbtftops.register_backlight = fbtft_register_backlight;
871 /* use driver provided functions */
872 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
881 EXPORT_SYMBOL(fbtft_framebuffer_alloc);
884 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
886 * @info: frame buffer info structure
889 void fbtft_framebuffer_release(struct fb_info *info)
891 fb_deferred_io_cleanup(info);
892 vfree(info->screen_buffer);
893 framebuffer_release(info);
895 EXPORT_SYMBOL(fbtft_framebuffer_release);
898 * fbtft_register_framebuffer - registers a tft frame buffer device
899 * @fb_info: frame buffer info structure
901 * Sets SPI driverdata if needed
902 * Requests needed gpios.
903 * Initializes display
905 * Registers a frame buffer device @fb_info.
907 * Returns negative errno on error, or zero for success.
910 int fbtft_register_framebuffer(struct fb_info *fb_info)
915 struct fbtft_par *par = fb_info->par;
916 struct spi_device *spi = par->spi;
919 if (!par->fbtftops.init_display) {
920 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
925 spi_set_drvdata(spi, fb_info);
927 platform_set_drvdata(par->pdev, fb_info);
929 ret = par->fbtftops.request_gpios(par);
933 if (par->fbtftops.verify_gpios) {
934 ret = par->fbtftops.verify_gpios(par);
939 ret = par->fbtftops.init_display(par);
942 if (par->fbtftops.set_var) {
943 ret = par->fbtftops.set_var(par);
948 /* update the entire display */
949 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
951 if (par->fbtftops.set_gamma && par->gamma.curves) {
952 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
957 if (par->fbtftops.register_backlight)
958 par->fbtftops.register_backlight(par);
960 ret = register_framebuffer(fb_info);
964 fbtft_sysfs_init(par);
967 sprintf(text1, ", %zu KiB %sbuffer memory",
968 par->txbuf.len >> 10, par->txbuf.dma ? "DMA " : "");
970 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
971 spi->chip_select, spi->max_speed_hz / 1000000);
972 dev_info(fb_info->dev,
973 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
974 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
975 fb_info->fix.smem_len >> 10, text1,
976 HZ / fb_info->fbdefio->delay, text2);
978 #ifdef CONFIG_FB_BACKLIGHT
979 /* Turn on backlight if available */
980 if (fb_info->bl_dev) {
981 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
982 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
989 if (par->fbtftops.unregister_backlight)
990 par->fbtftops.unregister_backlight(par);
994 EXPORT_SYMBOL(fbtft_register_framebuffer);
997 * fbtft_unregister_framebuffer - releases a tft frame buffer device
998 * @fb_info: frame buffer info structure
1000 * Frees SPI driverdata if needed
1002 * Unregisters frame buffer device.
1005 int fbtft_unregister_framebuffer(struct fb_info *fb_info)
1007 struct fbtft_par *par = fb_info->par;
1009 if (par->fbtftops.unregister_backlight)
1010 par->fbtftops.unregister_backlight(par);
1011 fbtft_sysfs_exit(par);
1012 return unregister_framebuffer(fb_info);
1014 EXPORT_SYMBOL(fbtft_unregister_framebuffer);
1018 * fbtft_init_display_dt() - Device Tree init_display() function
1021 * Return: 0 if successful, negative if error
1023 static int fbtft_init_display_dt(struct fbtft_par *par)
1025 struct device_node *node = par->info->device->of_node;
1026 struct property *prop;
1034 prop = of_find_property(node, "init", NULL);
1035 p = of_prop_next_u32(prop, NULL, &val);
1039 par->fbtftops.reset(par);
1040 if (par->gpio.cs != -1)
1041 gpio_set_value(par->gpio.cs, 0); /* Activate chip */
1044 if (val & FBTFT_OF_INIT_CMD) {
1047 while (p && !(val & 0xFFFF0000)) {
1049 dev_err(par->info->device,
1050 "%s: Maximum register values exceeded\n",
1055 p = of_prop_next_u32(prop, p, &val);
1057 /* make debug message */
1058 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1059 "init: write_register:\n");
1060 for (j = 0; j < i; j++)
1061 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1062 "buf[%d] = %02X\n", j, buf[j]);
1064 par->fbtftops.write_register(par, i,
1065 buf[0], buf[1], buf[2], buf[3],
1066 buf[4], buf[5], buf[6], buf[7],
1067 buf[8], buf[9], buf[10], buf[11],
1068 buf[12], buf[13], buf[14], buf[15],
1069 buf[16], buf[17], buf[18], buf[19],
1070 buf[20], buf[21], buf[22], buf[23],
1071 buf[24], buf[25], buf[26], buf[27],
1072 buf[28], buf[29], buf[30], buf[31],
1073 buf[32], buf[33], buf[34], buf[35],
1074 buf[36], buf[37], buf[38], buf[39],
1075 buf[40], buf[41], buf[42], buf[43],
1076 buf[44], buf[45], buf[46], buf[47],
1077 buf[48], buf[49], buf[50], buf[51],
1078 buf[52], buf[53], buf[54], buf[55],
1079 buf[56], buf[57], buf[58], buf[59],
1080 buf[60], buf[61], buf[62], buf[63]);
1081 } else if (val & FBTFT_OF_INIT_DELAY) {
1082 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1083 "init: msleep(%u)\n", val & 0xFFFF);
1084 msleep(val & 0xFFFF);
1085 p = of_prop_next_u32(prop, p, &val);
1087 dev_err(par->info->device, "illegal init value 0x%X\n",
1098 * fbtft_init_display() - Generic init_display() function
1101 * Uses par->init_sequence to do the initialization
1103 * Return: 0 if successful, negative if error
1105 int fbtft_init_display(struct fbtft_par *par)
1114 if (!par->init_sequence) {
1115 dev_err(par->info->device,
1116 "error: init_sequence is not set\n");
1120 /* make sure stop marker exists */
1121 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++)
1122 if (par->init_sequence[i] == -3)
1124 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1125 dev_err(par->info->device,
1126 "missing stop marker at end of init sequence\n");
1130 par->fbtftops.reset(par);
1131 if (par->gpio.cs != -1)
1132 gpio_set_value(par->gpio.cs, 0); /* Activate chip */
1135 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1136 if (par->init_sequence[i] == -3) {
1140 if (par->init_sequence[i] >= 0) {
1141 dev_err(par->info->device,
1142 "missing delimiter at position %d\n", i);
1145 if (par->init_sequence[i + 1] < 0) {
1146 dev_err(par->info->device,
1147 "missing value after delimiter %d at position %d\n",
1148 par->init_sequence[i], i);
1151 switch (par->init_sequence[i]) {
1154 /* make debug message */
1157 while (par->init_sequence[j] >= 0) {
1158 sprintf(str, "0x%02X ", par->init_sequence[j]);
1162 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1163 "init: write(0x%02X) %s\n",
1164 par->init_sequence[i], msg);
1168 while (par->init_sequence[i] >= 0) {
1170 dev_err(par->info->device,
1171 "%s: Maximum register values exceeded\n",
1175 buf[j++] = par->init_sequence[i++];
1177 par->fbtftops.write_register(par, j,
1178 buf[0], buf[1], buf[2], buf[3],
1179 buf[4], buf[5], buf[6], buf[7],
1180 buf[8], buf[9], buf[10], buf[11],
1181 buf[12], buf[13], buf[14], buf[15],
1182 buf[16], buf[17], buf[18], buf[19],
1183 buf[20], buf[21], buf[22], buf[23],
1184 buf[24], buf[25], buf[26], buf[27],
1185 buf[28], buf[29], buf[30], buf[31],
1186 buf[32], buf[33], buf[34], buf[35],
1187 buf[36], buf[37], buf[38], buf[39],
1188 buf[40], buf[41], buf[42], buf[43],
1189 buf[44], buf[45], buf[46], buf[47],
1190 buf[48], buf[49], buf[50], buf[51],
1191 buf[52], buf[53], buf[54], buf[55],
1192 buf[56], buf[57], buf[58], buf[59],
1193 buf[60], buf[61], buf[62], buf[63]);
1197 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1198 "init: mdelay(%d)\n", par->init_sequence[i]);
1199 mdelay(par->init_sequence[i++]);
1202 dev_err(par->info->device,
1203 "unknown delimiter %d at position %d\n",
1204 par->init_sequence[i], i);
1209 dev_err(par->info->device,
1210 "%s: something is wrong. Shouldn't get here.\n", __func__);
1213 EXPORT_SYMBOL(fbtft_init_display);
1216 * fbtft_verify_gpios() - Generic verify_gpios() function
1219 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1221 * Return: 0 if successful, negative if error
1223 static int fbtft_verify_gpios(struct fbtft_par *par)
1225 struct fbtft_platform_data *pdata = par->pdata;
1228 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1230 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
1232 dev_err(par->info->device,
1233 "Missing info about 'dc' gpio. Aborting.\n");
1240 if (par->gpio.wr < 0) {
1241 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1244 for (i = 0; i < pdata->display.buswidth; i++) {
1245 if (par->gpio.db[i] < 0) {
1246 dev_err(par->info->device,
1247 "Missing 'db%02d' gpio. Aborting.\n", i);
1256 /* returns 0 if the property is not present */
1257 static u32 fbtft_of_value(struct device_node *node, const char *propname)
1262 ret = of_property_read_u32(node, propname, &val);
1264 pr_info("%s: %s = %u\n", __func__, propname, val);
1269 static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1271 struct device_node *node = dev->of_node;
1272 struct fbtft_platform_data *pdata;
1275 dev_err(dev, "Missing platform data or DT\n");
1276 return ERR_PTR(-EINVAL);
1279 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1281 return ERR_PTR(-ENOMEM);
1283 pdata->display.width = fbtft_of_value(node, "width");
1284 pdata->display.height = fbtft_of_value(node, "height");
1285 pdata->display.regwidth = fbtft_of_value(node, "regwidth");
1286 pdata->display.buswidth = fbtft_of_value(node, "buswidth");
1287 pdata->display.backlight = fbtft_of_value(node, "backlight");
1288 pdata->display.bpp = fbtft_of_value(node, "bpp");
1289 pdata->display.debug = fbtft_of_value(node, "debug");
1290 pdata->rotate = fbtft_of_value(node, "rotate");
1291 pdata->bgr = of_property_read_bool(node, "bgr");
1292 pdata->fps = fbtft_of_value(node, "fps");
1293 pdata->txbuflen = fbtft_of_value(node, "txbuflen");
1294 pdata->startbyte = fbtft_of_value(node, "startbyte");
1295 of_property_read_string(node, "gamma", (const char **)&pdata->gamma);
1297 if (of_find_property(node, "led-gpios", NULL))
1298 pdata->display.backlight = 1;
1299 if (of_find_property(node, "init", NULL))
1300 pdata->display.fbtftops.init_display = fbtft_init_display_dt;
1301 pdata->display.fbtftops.request_gpios = fbtft_request_gpios_dt;
1306 static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1308 dev_err(dev, "Missing platform data\n");
1309 return ERR_PTR(-EINVAL);
1314 * fbtft_probe_common() - Generic device probe() helper function
1315 * @display: Display properties
1317 * @pdev: Platform device
1319 * Allocates, initializes and registers a framebuffer
1321 * Either @sdev or @pdev should be NULL
1323 * Return: 0 if successful, negative if error
1325 int fbtft_probe_common(struct fbtft_display *display,
1326 struct spi_device *sdev, struct platform_device *pdev)
1329 struct fb_info *info;
1330 struct fbtft_par *par;
1331 struct fbtft_platform_data *pdata;
1339 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1340 dev_info(dev, "%s()\n", __func__);
1342 pdata = dev->platform_data;
1344 pdata = fbtft_probe_dt(dev);
1346 return PTR_ERR(pdata);
1349 info = fbtft_framebuffer_alloc(display, dev, pdata);
1357 if (display->buswidth == 0) {
1358 dev_err(dev, "buswidth is not set\n");
1362 /* write register functions */
1363 if (display->regwidth == 8 && display->buswidth == 8) {
1364 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1366 if (display->regwidth == 8 && display->buswidth == 9 && par->spi) {
1367 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1368 } else if (display->regwidth == 16 && display->buswidth == 8) {
1369 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1370 } else if (display->regwidth == 16 && display->buswidth == 16) {
1371 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1374 "no default functions for regwidth=%d and buswidth=%d\n",
1375 display->regwidth, display->buswidth);
1378 /* write_vmem() functions */
1379 if (display->buswidth == 8)
1380 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1381 else if (display->buswidth == 9)
1382 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1383 else if (display->buswidth == 16)
1384 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1386 /* GPIO write() functions */
1388 if (display->buswidth == 8)
1389 par->fbtftops.write = fbtft_write_gpio8_wr;
1390 else if (display->buswidth == 16)
1391 par->fbtftops.write = fbtft_write_gpio16_wr;
1394 /* 9-bit SPI setup */
1395 if (par->spi && display->buswidth == 9) {
1396 if (par->spi->master->bits_per_word_mask & SPI_BPW_MASK(9)) {
1397 par->spi->bits_per_word = 9;
1399 dev_warn(&par->spi->dev,
1400 "9-bit SPI not available, emulating using 8-bit.\n");
1401 /* allocate buffer with room for dc bits */
1402 par->extra = devm_kzalloc(par->info->device,
1403 par->txbuf.len + (par->txbuf.len / 8) + 8,
1409 par->fbtftops.write = fbtft_write_spi_emulate_9;
1413 if (!par->fbtftops.verify_gpios)
1414 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1416 /* make sure we still use the driver provided functions */
1417 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1419 /* use init_sequence if provided */
1420 if (par->init_sequence)
1421 par->fbtftops.init_display = fbtft_init_display;
1423 /* use platform_data provided functions above all */
1424 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1426 ret = fbtft_register_framebuffer(info);
1433 fbtft_framebuffer_release(info);
1437 EXPORT_SYMBOL(fbtft_probe_common);
1440 * fbtft_remove_common() - Generic device remove() helper function
1442 * @info: Framebuffer
1444 * Unregisters and releases the framebuffer
1446 * Return: 0 if successful, negative if error
1448 int fbtft_remove_common(struct device *dev, struct fb_info *info)
1450 struct fbtft_par *par;
1456 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1457 "%s()\n", __func__);
1458 fbtft_unregister_framebuffer(info);
1459 fbtft_framebuffer_release(info);
1463 EXPORT_SYMBOL(fbtft_remove_common);
1465 MODULE_LICENSE("GPL");