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>
36 #include <linux/of_gpio.h>
37 #include <video/mipi_display.h>
42 static unsigned long debug;
43 module_param(debug, ulong, 0000);
44 MODULE_PARM_DESC(debug, "override device debug level");
46 void fbtft_dbg_hex(const struct device *dev, int groupsize,
47 void *buf, size_t len, const char *fmt, ...)
50 static char textbuf[512];
55 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
58 hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len,
59 512 - text_len, false);
62 dev_info(dev, "%s ...\n", text);
64 dev_info(dev, "%s\n", text);
66 EXPORT_SYMBOL(fbtft_dbg_hex);
68 static unsigned long fbtft_request_gpios_match(struct fbtft_par *par,
69 const struct fbtft_gpio *gpio)
74 fbtft_par_dbg(DEBUG_REQUEST_GPIOS_MATCH, par, "%s('%s')\n",
75 __func__, gpio->name);
77 if (strcasecmp(gpio->name, "reset") == 0) {
78 par->gpio.reset = gpio->gpio;
79 return GPIOF_OUT_INIT_HIGH;
80 } else if (strcasecmp(gpio->name, "dc") == 0) {
81 par->gpio.dc = gpio->gpio;
82 return GPIOF_OUT_INIT_LOW;
83 } else if (strcasecmp(gpio->name, "cs") == 0) {
84 par->gpio.cs = gpio->gpio;
85 return GPIOF_OUT_INIT_HIGH;
86 } else if (strcasecmp(gpio->name, "wr") == 0) {
87 par->gpio.wr = gpio->gpio;
88 return GPIOF_OUT_INIT_HIGH;
89 } else if (strcasecmp(gpio->name, "rd") == 0) {
90 par->gpio.rd = gpio->gpio;
91 return GPIOF_OUT_INIT_HIGH;
92 } else if (strcasecmp(gpio->name, "latch") == 0) {
93 par->gpio.latch = gpio->gpio;
94 return GPIOF_OUT_INIT_LOW;
95 } else if (gpio->name[0] == 'd' && gpio->name[1] == 'b') {
96 ret = kstrtol(&gpio->name[2], 10, &val);
97 if (ret == 0 && val < 16) {
98 par->gpio.db[val] = gpio->gpio;
99 return GPIOF_OUT_INIT_LOW;
101 } else if (strcasecmp(gpio->name, "led") == 0) {
102 par->gpio.led[0] = gpio->gpio;
103 return GPIOF_OUT_INIT_LOW;
104 } else if (strcasecmp(gpio->name, "led_") == 0) {
105 par->gpio.led[0] = gpio->gpio;
106 return GPIOF_OUT_INIT_HIGH;
109 return FBTFT_GPIO_NO_MATCH;
112 static int fbtft_request_gpios(struct fbtft_par *par)
114 struct fbtft_platform_data *pdata = par->pdata;
115 const struct fbtft_gpio *gpio;
119 if (!(pdata && pdata->gpios))
123 while (gpio->name[0]) {
124 flags = FBTFT_GPIO_NO_MATCH;
125 /* if driver provides match function, try it first,
126 * if no match use our own
128 if (par->fbtftops.request_gpios_match)
129 flags = par->fbtftops.request_gpios_match(par, gpio);
130 if (flags == FBTFT_GPIO_NO_MATCH)
131 flags = fbtft_request_gpios_match(par, gpio);
132 if (flags != FBTFT_GPIO_NO_MATCH) {
133 ret = devm_gpio_request_one(par->info->device,
135 par->info->device->driver->name);
137 dev_err(par->info->device,
138 "%s: gpio_request_one('%s'=%d) failed with %d\n",
139 __func__, gpio->name,
143 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par,
144 "%s: '%s' = GPIO%d\n",
145 __func__, gpio->name, gpio->gpio);
154 static int fbtft_request_one_gpio(struct fbtft_par *par,
155 const char *name, int index, int *gpiop)
157 struct device *dev = par->info->device;
158 struct device_node *node = dev->of_node;
159 int gpio, flags, ret = 0;
160 enum of_gpio_flags of_flags;
162 if (of_find_property(node, name, NULL)) {
163 gpio = of_get_named_gpio_flags(node, name, index, &of_flags);
166 if (gpio == -EPROBE_DEFER)
170 "failed to get '%s' from DT\n", name);
174 /* active low translates to initially low */
175 flags = (of_flags & OF_GPIO_ACTIVE_LOW) ? GPIOF_OUT_INIT_LOW :
177 ret = devm_gpio_request_one(dev, gpio, flags,
181 "gpio_request_one('%s'=%d) failed with %d\n",
187 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par, "%s: '%s' = GPIO%d\n",
188 __func__, name, gpio);
194 static int fbtft_request_gpios_dt(struct fbtft_par *par)
199 if (!par->info->device->of_node)
202 ret = fbtft_request_one_gpio(par, "reset-gpios", 0, &par->gpio.reset);
205 ret = fbtft_request_one_gpio(par, "dc-gpios", 0, &par->gpio.dc);
208 ret = fbtft_request_one_gpio(par, "rd-gpios", 0, &par->gpio.rd);
211 ret = fbtft_request_one_gpio(par, "wr-gpios", 0, &par->gpio.wr);
214 ret = fbtft_request_one_gpio(par, "cs-gpios", 0, &par->gpio.cs);
217 ret = fbtft_request_one_gpio(par, "latch-gpios", 0, &par->gpio.latch);
220 for (i = 0; i < 16; i++) {
221 ret = fbtft_request_one_gpio(par, "db-gpios", i,
225 ret = fbtft_request_one_gpio(par, "led-gpios", i,
229 ret = fbtft_request_one_gpio(par, "aux-gpios", i,
239 #ifdef CONFIG_FB_BACKLIGHT
240 static int fbtft_backlight_update_status(struct backlight_device *bd)
242 struct fbtft_par *par = bl_get_data(bd);
243 bool polarity = !!(bd->props.state & BL_CORE_DRIVER1);
245 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
246 "%s: polarity=%d, power=%d, fb_blank=%d\n",
247 __func__, polarity, bd->props.power, bd->props.fb_blank);
249 if ((bd->props.power == FB_BLANK_UNBLANK) &&
250 (bd->props.fb_blank == FB_BLANK_UNBLANK))
251 gpio_set_value(par->gpio.led[0], polarity);
253 gpio_set_value(par->gpio.led[0], !polarity);
258 static int fbtft_backlight_get_brightness(struct backlight_device *bd)
260 return bd->props.brightness;
263 void fbtft_unregister_backlight(struct fbtft_par *par)
265 if (par->info->bl_dev) {
266 par->info->bl_dev->props.power = FB_BLANK_POWERDOWN;
267 backlight_update_status(par->info->bl_dev);
268 backlight_device_unregister(par->info->bl_dev);
269 par->info->bl_dev = NULL;
273 static const struct backlight_ops fbtft_bl_ops = {
274 .get_brightness = fbtft_backlight_get_brightness,
275 .update_status = fbtft_backlight_update_status,
278 void fbtft_register_backlight(struct fbtft_par *par)
280 struct backlight_device *bd;
281 struct backlight_properties bl_props = { 0, };
283 if (par->gpio.led[0] == -1) {
284 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
285 "%s(): led pin not set, exiting.\n", __func__);
289 bl_props.type = BACKLIGHT_RAW;
290 /* Assume backlight is off, get polarity from current state of pin */
291 bl_props.power = FB_BLANK_POWERDOWN;
292 if (!gpio_get_value(par->gpio.led[0]))
293 bl_props.state |= BL_CORE_DRIVER1;
295 bd = backlight_device_register(dev_driver_string(par->info->device),
296 par->info->device, par,
297 &fbtft_bl_ops, &bl_props);
299 dev_err(par->info->device,
300 "cannot register backlight device (%ld)\n",
304 par->info->bl_dev = bd;
306 if (!par->fbtftops.unregister_backlight)
307 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
310 void fbtft_register_backlight(struct fbtft_par *par) { };
311 void fbtft_unregister_backlight(struct fbtft_par *par) { };
313 EXPORT_SYMBOL(fbtft_register_backlight);
314 EXPORT_SYMBOL(fbtft_unregister_backlight);
316 static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
319 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
320 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
322 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
323 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
325 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
328 static void fbtft_reset(struct fbtft_par *par)
330 if (par->gpio.reset == -1)
332 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
333 gpio_set_value_cansleep(par->gpio.reset, 0);
334 usleep_range(20, 40);
335 gpio_set_value_cansleep(par->gpio.reset, 1);
339 static void fbtft_update_display(struct fbtft_par *par, unsigned int start_line,
340 unsigned int end_line)
343 ktime_t ts_start, ts_end;
344 long fps, throughput;
348 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE |
349 DEBUG_TIME_EACH_UPDATE))) {
350 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
351 ((par->debug & DEBUG_TIME_FIRST_UPDATE) &&
352 !par->first_update_done)) {
353 ts_start = ktime_get();
359 if (start_line > end_line) {
360 dev_warn(par->info->device,
361 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
362 __func__, start_line, end_line);
364 end_line = par->info->var.yres - 1;
366 if (start_line > par->info->var.yres - 1 ||
367 end_line > par->info->var.yres - 1) {
368 dev_warn(par->info->device,
369 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
370 __func__, start_line,
371 end_line, par->info->var.yres - 1);
373 end_line = par->info->var.yres - 1;
376 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
377 __func__, start_line, end_line);
379 if (par->fbtftops.set_addr_win)
380 par->fbtftops.set_addr_win(par, 0, start_line,
381 par->info->var.xres - 1, end_line);
383 offset = start_line * par->info->fix.line_length;
384 len = (end_line - start_line + 1) * par->info->fix.line_length;
385 ret = par->fbtftops.write_vmem(par, offset, len);
387 dev_err(par->info->device,
388 "%s: write_vmem failed to update display buffer\n",
391 if (unlikely(timeit)) {
392 ts_end = ktime_get();
393 if (!ktime_to_ns(par->update_time))
394 par->update_time = ts_start;
396 fps = ktime_us_delta(ts_start, par->update_time);
397 par->update_time = ts_start;
398 fps = fps ? 1000000 / fps : 0;
400 throughput = ktime_us_delta(ts_end, ts_start);
401 throughput = throughput ? (len * 1000) / throughput : 0;
402 throughput = throughput * 1000 / 1024;
404 dev_info(par->info->device,
405 "Display update: %ld kB/s, fps=%ld\n",
407 par->first_update_done = true;
411 static void fbtft_mkdirty(struct fb_info *info, int y, int height)
413 struct fbtft_par *par = info->par;
414 struct fb_deferred_io *fbdefio = info->fbdefio;
416 /* special case, needed ? */
419 height = info->var.yres - 1;
422 /* Mark display lines/area as dirty */
423 spin_lock(&par->dirty_lock);
424 if (y < par->dirty_lines_start)
425 par->dirty_lines_start = y;
426 if (y + height - 1 > par->dirty_lines_end)
427 par->dirty_lines_end = y + height - 1;
428 spin_unlock(&par->dirty_lock);
430 /* Schedule deferred_io to update display (no-op if already on queue)*/
431 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
434 static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
436 struct fbtft_par *par = info->par;
437 unsigned int dirty_lines_start, dirty_lines_end;
440 unsigned int y_low = 0, y_high = 0;
443 spin_lock(&par->dirty_lock);
444 dirty_lines_start = par->dirty_lines_start;
445 dirty_lines_end = par->dirty_lines_end;
446 /* set display line markers as clean */
447 par->dirty_lines_start = par->info->var.yres - 1;
448 par->dirty_lines_end = 0;
449 spin_unlock(&par->dirty_lock);
451 /* Mark display lines as dirty */
452 list_for_each_entry(page, pagelist, lru) {
454 index = page->index << PAGE_SHIFT;
455 y_low = index / info->fix.line_length;
456 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
457 dev_dbg(info->device,
458 "page->index=%lu y_low=%d y_high=%d\n",
459 page->index, y_low, y_high);
460 if (y_high > info->var.yres - 1)
461 y_high = info->var.yres - 1;
462 if (y_low < dirty_lines_start)
463 dirty_lines_start = y_low;
464 if (y_high > dirty_lines_end)
465 dirty_lines_end = y_high;
468 par->fbtftops.update_display(info->par,
469 dirty_lines_start, dirty_lines_end);
472 static void fbtft_fb_fillrect(struct fb_info *info,
473 const struct fb_fillrect *rect)
475 struct fbtft_par *par = info->par;
478 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
479 __func__, rect->dx, rect->dy, rect->width, rect->height);
480 sys_fillrect(info, rect);
482 par->fbtftops.mkdirty(info, rect->dy, rect->height);
485 static void fbtft_fb_copyarea(struct fb_info *info,
486 const struct fb_copyarea *area)
488 struct fbtft_par *par = info->par;
491 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
492 __func__, area->dx, area->dy, area->width, area->height);
493 sys_copyarea(info, area);
495 par->fbtftops.mkdirty(info, area->dy, area->height);
498 static void fbtft_fb_imageblit(struct fb_info *info,
499 const struct fb_image *image)
501 struct fbtft_par *par = info->par;
504 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
505 __func__, image->dx, image->dy, image->width, image->height);
506 sys_imageblit(info, image);
508 par->fbtftops.mkdirty(info, image->dy, image->height);
511 static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
512 size_t count, loff_t *ppos)
514 struct fbtft_par *par = info->par;
518 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
519 res = fb_sys_write(info, buf, count, ppos);
521 /* TODO: only mark changed area update all for now */
522 par->fbtftops.mkdirty(info, -1, 0);
528 static unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
531 chan >>= 16 - bf->length;
532 return chan << bf->offset;
535 static int fbtft_fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
536 unsigned int blue, unsigned int transp,
537 struct fb_info *info)
543 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
544 __func__, regno, red, green, blue, transp);
546 switch (info->fix.visual) {
547 case FB_VISUAL_TRUECOLOR:
549 u32 *pal = info->pseudo_palette;
551 val = chan_to_field(red, &info->var.red);
552 val |= chan_to_field(green, &info->var.green);
553 val |= chan_to_field(blue, &info->var.blue);
563 static int fbtft_fb_blank(int blank, struct fb_info *info)
565 struct fbtft_par *par = info->par;
568 dev_dbg(info->dev, "%s(blank=%d)\n",
571 if (!par->fbtftops.blank)
575 case FB_BLANK_POWERDOWN:
576 case FB_BLANK_VSYNC_SUSPEND:
577 case FB_BLANK_HSYNC_SUSPEND:
578 case FB_BLANK_NORMAL:
579 ret = par->fbtftops.blank(par, true);
581 case FB_BLANK_UNBLANK:
582 ret = par->fbtftops.blank(par, false);
588 static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
591 dst->write = src->write;
593 dst->read = src->read;
595 dst->write_vmem = src->write_vmem;
596 if (src->write_register)
597 dst->write_register = src->write_register;
598 if (src->set_addr_win)
599 dst->set_addr_win = src->set_addr_win;
601 dst->reset = src->reset;
603 dst->mkdirty = src->mkdirty;
604 if (src->update_display)
605 dst->update_display = src->update_display;
606 if (src->init_display)
607 dst->init_display = src->init_display;
609 dst->blank = src->blank;
610 if (src->request_gpios_match)
611 dst->request_gpios_match = src->request_gpios_match;
612 if (src->request_gpios)
613 dst->request_gpios = src->request_gpios;
614 if (src->verify_gpios)
615 dst->verify_gpios = src->verify_gpios;
616 if (src->register_backlight)
617 dst->register_backlight = src->register_backlight;
618 if (src->unregister_backlight)
619 dst->unregister_backlight = src->unregister_backlight;
621 dst->set_var = src->set_var;
623 dst->set_gamma = src->set_gamma;
627 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
629 * @display: pointer to structure describing the display
630 * @dev: pointer to the device for this fb, this can be NULL
632 * Creates a new frame buffer info structure.
634 * Also creates and populates the following structures:
637 * info->pseudo_palette
641 * Returns the new structure, or NULL if an error occurred.
644 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
646 struct fbtft_platform_data *pdata)
648 struct fb_info *info;
649 struct fbtft_par *par;
650 struct fb_ops *fbops = NULL;
651 struct fb_deferred_io *fbdefio = NULL;
657 int txbuflen = display->txbuflen;
658 unsigned int bpp = display->bpp;
659 unsigned int fps = display->fps;
661 const s16 *init_sequence = display->init_sequence;
662 char *gamma = display->gamma;
663 u32 *gamma_curves = NULL;
666 if (display->gamma_num * display->gamma_len >
667 FBTFT_GAMMA_MAX_VALUES_TOTAL) {
668 dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
669 FBTFT_GAMMA_MAX_VALUES_TOTAL);
680 dev_err(dev, "platform data is missing\n");
684 /* override driver values? */
688 txbuflen = pdata->txbuflen;
689 if (pdata->display.init_sequence)
690 init_sequence = pdata->display.init_sequence;
692 gamma = pdata->gamma;
693 if (pdata->display.debug)
694 display->debug = pdata->display.debug;
695 if (pdata->display.backlight)
696 display->backlight = pdata->display.backlight;
697 if (pdata->display.width)
698 display->width = pdata->display.width;
699 if (pdata->display.height)
700 display->height = pdata->display.height;
701 if (pdata->display.buswidth)
702 display->buswidth = pdata->display.buswidth;
703 if (pdata->display.regwidth)
704 display->regwidth = pdata->display.regwidth;
706 display->debug |= debug;
707 fbtft_expand_debug_value(&display->debug);
709 switch (pdata->rotate) {
712 width = display->height;
713 height = display->width;
716 width = display->width;
717 height = display->height;
720 vmem_size = display->width * display->height * bpp / 8;
721 vmem = vzalloc(vmem_size);
725 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
729 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
733 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
737 if (display->gamma_num && display->gamma_len) {
738 gamma_curves = devm_kcalloc(dev,
741 sizeof(gamma_curves[0]),
747 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
751 info->screen_buffer = vmem;
753 info->fbdefio = fbdefio;
755 fbops->owner = dev->driver->owner;
756 fbops->fb_read = fb_sys_read;
757 fbops->fb_write = fbtft_fb_write;
758 fbops->fb_fillrect = fbtft_fb_fillrect;
759 fbops->fb_copyarea = fbtft_fb_copyarea;
760 fbops->fb_imageblit = fbtft_fb_imageblit;
761 fbops->fb_setcolreg = fbtft_fb_setcolreg;
762 fbops->fb_blank = fbtft_fb_blank;
764 fbdefio->delay = HZ/fps;
765 fbdefio->deferred_io = fbtft_deferred_io;
766 fb_deferred_io_init(info);
768 strncpy(info->fix.id, dev->driver->name, 16);
769 info->fix.type = FB_TYPE_PACKED_PIXELS;
770 info->fix.visual = FB_VISUAL_TRUECOLOR;
771 info->fix.xpanstep = 0;
772 info->fix.ypanstep = 0;
773 info->fix.ywrapstep = 0;
774 info->fix.line_length = width * bpp / 8;
775 info->fix.accel = FB_ACCEL_NONE;
776 info->fix.smem_len = vmem_size;
778 info->var.rotate = pdata->rotate;
779 info->var.xres = width;
780 info->var.yres = height;
781 info->var.xres_virtual = info->var.xres;
782 info->var.yres_virtual = info->var.yres;
783 info->var.bits_per_pixel = bpp;
784 info->var.nonstd = 1;
787 info->var.red.offset = 11;
788 info->var.red.length = 5;
789 info->var.green.offset = 5;
790 info->var.green.length = 6;
791 info->var.blue.offset = 0;
792 info->var.blue.length = 5;
793 info->var.transp.offset = 0;
794 info->var.transp.length = 0;
796 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
801 par->debug = display->debug;
803 spin_lock_init(&par->dirty_lock);
804 par->bgr = pdata->bgr;
805 par->startbyte = pdata->startbyte;
806 par->init_sequence = init_sequence;
807 par->gamma.curves = gamma_curves;
808 par->gamma.num_curves = display->gamma_num;
809 par->gamma.num_values = display->gamma_len;
810 mutex_init(&par->gamma.lock);
811 info->pseudo_palette = par->pseudo_palette;
813 if (par->gamma.curves && gamma) {
814 if (fbtft_gamma_parse_str(par,
815 par->gamma.curves, gamma, strlen(gamma)))
819 /* Transmit buffer */
821 txbuflen = vmem_size + 2; /* add in case startbyte is used */
822 if (txbuflen >= vmem_size + 2)
825 #ifdef __LITTLE_ENDIAN
826 if ((!txbuflen) && (bpp > 8))
827 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
831 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
834 par->txbuf.buf = txbuf;
835 par->txbuf.len = txbuflen;
838 /* Initialize gpios to disabled */
839 par->gpio.reset = -1;
844 par->gpio.latch = -1;
845 for (i = 0; i < 16; i++) {
846 par->gpio.db[i] = -1;
847 par->gpio.led[i] = -1;
848 par->gpio.aux[i] = -1;
851 /* default fbtft operations */
852 par->fbtftops.write = fbtft_write_spi;
853 par->fbtftops.read = fbtft_read_spi;
854 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
855 par->fbtftops.write_register = fbtft_write_reg8_bus8;
856 par->fbtftops.set_addr_win = fbtft_set_addr_win;
857 par->fbtftops.reset = fbtft_reset;
858 par->fbtftops.mkdirty = fbtft_mkdirty;
859 par->fbtftops.update_display = fbtft_update_display;
860 par->fbtftops.request_gpios = fbtft_request_gpios;
861 if (display->backlight)
862 par->fbtftops.register_backlight = fbtft_register_backlight;
864 /* use driver provided functions */
865 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
874 EXPORT_SYMBOL(fbtft_framebuffer_alloc);
877 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
879 * @info: frame buffer info structure
882 void fbtft_framebuffer_release(struct fb_info *info)
884 fb_deferred_io_cleanup(info);
885 vfree(info->screen_buffer);
886 framebuffer_release(info);
888 EXPORT_SYMBOL(fbtft_framebuffer_release);
891 * fbtft_register_framebuffer - registers a tft frame buffer device
892 * @fb_info: frame buffer info structure
894 * Sets SPI driverdata if needed
895 * Requests needed gpios.
896 * Initializes display
898 * Registers a frame buffer device @fb_info.
900 * Returns negative errno on error, or zero for success.
903 int fbtft_register_framebuffer(struct fb_info *fb_info)
908 struct fbtft_par *par = fb_info->par;
909 struct spi_device *spi = par->spi;
912 if (!par->fbtftops.init_display) {
913 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
918 spi_set_drvdata(spi, fb_info);
920 platform_set_drvdata(par->pdev, fb_info);
922 ret = par->fbtftops.request_gpios(par);
926 if (par->fbtftops.verify_gpios) {
927 ret = par->fbtftops.verify_gpios(par);
932 ret = par->fbtftops.init_display(par);
935 if (par->fbtftops.set_var) {
936 ret = par->fbtftops.set_var(par);
941 /* update the entire display */
942 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
944 if (par->fbtftops.set_gamma && par->gamma.curves) {
945 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
950 if (par->fbtftops.register_backlight)
951 par->fbtftops.register_backlight(par);
953 ret = register_framebuffer(fb_info);
957 fbtft_sysfs_init(par);
959 if (par->txbuf.buf && par->txbuf.len >= 1024)
960 sprintf(text1, ", %zu KiB buffer memory", par->txbuf.len >> 10);
962 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
963 spi->chip_select, spi->max_speed_hz / 1000000);
964 dev_info(fb_info->dev,
965 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
966 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
967 fb_info->fix.smem_len >> 10, text1,
968 HZ / fb_info->fbdefio->delay, text2);
970 #ifdef CONFIG_FB_BACKLIGHT
971 /* Turn on backlight if available */
972 if (fb_info->bl_dev) {
973 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
974 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
981 if (par->fbtftops.unregister_backlight)
982 par->fbtftops.unregister_backlight(par);
986 EXPORT_SYMBOL(fbtft_register_framebuffer);
989 * fbtft_unregister_framebuffer - releases a tft frame buffer device
990 * @fb_info: frame buffer info structure
992 * Frees SPI driverdata if needed
994 * Unregisters frame buffer device.
997 int fbtft_unregister_framebuffer(struct fb_info *fb_info)
999 struct fbtft_par *par = fb_info->par;
1001 if (par->fbtftops.unregister_backlight)
1002 par->fbtftops.unregister_backlight(par);
1003 fbtft_sysfs_exit(par);
1004 return unregister_framebuffer(fb_info);
1006 EXPORT_SYMBOL(fbtft_unregister_framebuffer);
1010 * fbtft_init_display_dt() - Device Tree init_display() function
1013 * Return: 0 if successful, negative if error
1015 static int fbtft_init_display_dt(struct fbtft_par *par)
1017 struct device_node *node = par->info->device->of_node;
1018 struct property *prop;
1026 prop = of_find_property(node, "init", NULL);
1027 p = of_prop_next_u32(prop, NULL, &val);
1031 par->fbtftops.reset(par);
1032 if (par->gpio.cs != -1)
1033 gpio_set_value(par->gpio.cs, 0); /* Activate chip */
1036 if (val & FBTFT_OF_INIT_CMD) {
1039 while (p && !(val & 0xFFFF0000)) {
1041 dev_err(par->info->device,
1042 "%s: Maximum register values exceeded\n",
1047 p = of_prop_next_u32(prop, p, &val);
1049 /* make debug message */
1050 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1051 "init: write_register:\n");
1052 for (j = 0; j < i; j++)
1053 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1054 "buf[%d] = %02X\n", j, buf[j]);
1056 par->fbtftops.write_register(par, i,
1057 buf[0], buf[1], buf[2], buf[3],
1058 buf[4], buf[5], buf[6], buf[7],
1059 buf[8], buf[9], buf[10], buf[11],
1060 buf[12], buf[13], buf[14], buf[15],
1061 buf[16], buf[17], buf[18], buf[19],
1062 buf[20], buf[21], buf[22], buf[23],
1063 buf[24], buf[25], buf[26], buf[27],
1064 buf[28], buf[29], buf[30], buf[31],
1065 buf[32], buf[33], buf[34], buf[35],
1066 buf[36], buf[37], buf[38], buf[39],
1067 buf[40], buf[41], buf[42], buf[43],
1068 buf[44], buf[45], buf[46], buf[47],
1069 buf[48], buf[49], buf[50], buf[51],
1070 buf[52], buf[53], buf[54], buf[55],
1071 buf[56], buf[57], buf[58], buf[59],
1072 buf[60], buf[61], buf[62], buf[63]);
1073 } else if (val & FBTFT_OF_INIT_DELAY) {
1074 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1075 "init: msleep(%u)\n", val & 0xFFFF);
1076 msleep(val & 0xFFFF);
1077 p = of_prop_next_u32(prop, p, &val);
1079 dev_err(par->info->device, "illegal init value 0x%X\n",
1090 * fbtft_init_display() - Generic init_display() function
1093 * Uses par->init_sequence to do the initialization
1095 * Return: 0 if successful, negative if error
1097 int fbtft_init_display(struct fbtft_par *par)
1106 if (!par->init_sequence) {
1107 dev_err(par->info->device,
1108 "error: init_sequence is not set\n");
1112 /* make sure stop marker exists */
1113 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++)
1114 if (par->init_sequence[i] == -3)
1116 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1117 dev_err(par->info->device,
1118 "missing stop marker at end of init sequence\n");
1122 par->fbtftops.reset(par);
1123 if (par->gpio.cs != -1)
1124 gpio_set_value(par->gpio.cs, 0); /* Activate chip */
1127 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1128 if (par->init_sequence[i] == -3) {
1132 if (par->init_sequence[i] >= 0) {
1133 dev_err(par->info->device,
1134 "missing delimiter at position %d\n", i);
1137 if (par->init_sequence[i + 1] < 0) {
1138 dev_err(par->info->device,
1139 "missing value after delimiter %d at position %d\n",
1140 par->init_sequence[i], i);
1143 switch (par->init_sequence[i]) {
1146 /* make debug message */
1149 while (par->init_sequence[j] >= 0) {
1150 sprintf(str, "0x%02X ", par->init_sequence[j]);
1154 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1155 "init: write(0x%02X) %s\n",
1156 par->init_sequence[i], msg);
1160 while (par->init_sequence[i] >= 0) {
1162 dev_err(par->info->device,
1163 "%s: Maximum register values exceeded\n",
1167 buf[j++] = par->init_sequence[i++];
1169 par->fbtftops.write_register(par, j,
1170 buf[0], buf[1], buf[2], buf[3],
1171 buf[4], buf[5], buf[6], buf[7],
1172 buf[8], buf[9], buf[10], buf[11],
1173 buf[12], buf[13], buf[14], buf[15],
1174 buf[16], buf[17], buf[18], buf[19],
1175 buf[20], buf[21], buf[22], buf[23],
1176 buf[24], buf[25], buf[26], buf[27],
1177 buf[28], buf[29], buf[30], buf[31],
1178 buf[32], buf[33], buf[34], buf[35],
1179 buf[36], buf[37], buf[38], buf[39],
1180 buf[40], buf[41], buf[42], buf[43],
1181 buf[44], buf[45], buf[46], buf[47],
1182 buf[48], buf[49], buf[50], buf[51],
1183 buf[52], buf[53], buf[54], buf[55],
1184 buf[56], buf[57], buf[58], buf[59],
1185 buf[60], buf[61], buf[62], buf[63]);
1189 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1190 "init: mdelay(%d)\n", par->init_sequence[i]);
1191 mdelay(par->init_sequence[i++]);
1194 dev_err(par->info->device,
1195 "unknown delimiter %d at position %d\n",
1196 par->init_sequence[i], i);
1201 dev_err(par->info->device,
1202 "%s: something is wrong. Shouldn't get here.\n", __func__);
1205 EXPORT_SYMBOL(fbtft_init_display);
1208 * fbtft_verify_gpios() - Generic verify_gpios() function
1211 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1213 * Return: 0 if successful, negative if error
1215 static int fbtft_verify_gpios(struct fbtft_par *par)
1217 struct fbtft_platform_data *pdata = par->pdata;
1220 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1222 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
1224 dev_err(par->info->device,
1225 "Missing info about 'dc' gpio. Aborting.\n");
1232 if (par->gpio.wr < 0) {
1233 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1236 for (i = 0; i < pdata->display.buswidth; i++) {
1237 if (par->gpio.db[i] < 0) {
1238 dev_err(par->info->device,
1239 "Missing 'db%02d' gpio. Aborting.\n", i);
1248 /* returns 0 if the property is not present */
1249 static u32 fbtft_of_value(struct device_node *node, const char *propname)
1254 ret = of_property_read_u32(node, propname, &val);
1256 pr_info("%s: %s = %u\n", __func__, propname, val);
1261 static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1263 struct device_node *node = dev->of_node;
1264 struct fbtft_platform_data *pdata;
1267 dev_err(dev, "Missing platform data or DT\n");
1268 return ERR_PTR(-EINVAL);
1271 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1273 return ERR_PTR(-ENOMEM);
1275 pdata->display.width = fbtft_of_value(node, "width");
1276 pdata->display.height = fbtft_of_value(node, "height");
1277 pdata->display.regwidth = fbtft_of_value(node, "regwidth");
1278 pdata->display.buswidth = fbtft_of_value(node, "buswidth");
1279 pdata->display.backlight = fbtft_of_value(node, "backlight");
1280 pdata->display.bpp = fbtft_of_value(node, "bpp");
1281 pdata->display.debug = fbtft_of_value(node, "debug");
1282 pdata->rotate = fbtft_of_value(node, "rotate");
1283 pdata->bgr = of_property_read_bool(node, "bgr");
1284 pdata->fps = fbtft_of_value(node, "fps");
1285 pdata->txbuflen = fbtft_of_value(node, "txbuflen");
1286 pdata->startbyte = fbtft_of_value(node, "startbyte");
1287 of_property_read_string(node, "gamma", (const char **)&pdata->gamma);
1289 if (of_find_property(node, "led-gpios", NULL))
1290 pdata->display.backlight = 1;
1291 if (of_find_property(node, "init", NULL))
1292 pdata->display.fbtftops.init_display = fbtft_init_display_dt;
1293 pdata->display.fbtftops.request_gpios = fbtft_request_gpios_dt;
1298 static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1300 dev_err(dev, "Missing platform data\n");
1301 return ERR_PTR(-EINVAL);
1306 * fbtft_probe_common() - Generic device probe() helper function
1307 * @display: Display properties
1309 * @pdev: Platform device
1311 * Allocates, initializes and registers a framebuffer
1313 * Either @sdev or @pdev should be NULL
1315 * Return: 0 if successful, negative if error
1317 int fbtft_probe_common(struct fbtft_display *display,
1318 struct spi_device *sdev, struct platform_device *pdev)
1321 struct fb_info *info;
1322 struct fbtft_par *par;
1323 struct fbtft_platform_data *pdata;
1331 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1332 dev_info(dev, "%s()\n", __func__);
1334 pdata = dev->platform_data;
1336 pdata = fbtft_probe_dt(dev);
1338 return PTR_ERR(pdata);
1341 info = fbtft_framebuffer_alloc(display, dev, pdata);
1349 if (display->buswidth == 0) {
1350 dev_err(dev, "buswidth is not set\n");
1354 /* write register functions */
1355 if (display->regwidth == 8 && display->buswidth == 8) {
1356 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1358 if (display->regwidth == 8 && display->buswidth == 9 && par->spi) {
1359 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1360 } else if (display->regwidth == 16 && display->buswidth == 8) {
1361 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1362 } else if (display->regwidth == 16 && display->buswidth == 16) {
1363 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1366 "no default functions for regwidth=%d and buswidth=%d\n",
1367 display->regwidth, display->buswidth);
1370 /* write_vmem() functions */
1371 if (display->buswidth == 8)
1372 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1373 else if (display->buswidth == 9)
1374 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1375 else if (display->buswidth == 16)
1376 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1378 /* GPIO write() functions */
1380 if (display->buswidth == 8)
1381 par->fbtftops.write = fbtft_write_gpio8_wr;
1382 else if (display->buswidth == 16)
1383 par->fbtftops.write = fbtft_write_gpio16_wr;
1386 /* 9-bit SPI setup */
1387 if (par->spi && display->buswidth == 9) {
1388 if (par->spi->master->bits_per_word_mask & SPI_BPW_MASK(9)) {
1389 par->spi->bits_per_word = 9;
1391 dev_warn(&par->spi->dev,
1392 "9-bit SPI not available, emulating using 8-bit.\n");
1393 /* allocate buffer with room for dc bits */
1394 par->extra = devm_kzalloc(par->info->device,
1395 par->txbuf.len + (par->txbuf.len / 8) + 8,
1401 par->fbtftops.write = fbtft_write_spi_emulate_9;
1405 if (!par->fbtftops.verify_gpios)
1406 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1408 /* make sure we still use the driver provided functions */
1409 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1411 /* use init_sequence if provided */
1412 if (par->init_sequence)
1413 par->fbtftops.init_display = fbtft_init_display;
1415 /* use platform_data provided functions above all */
1416 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1418 ret = fbtft_register_framebuffer(info);
1425 fbtft_framebuffer_release(info);
1429 EXPORT_SYMBOL(fbtft_probe_common);
1432 * fbtft_remove_common() - Generic device remove() helper function
1434 * @info: Framebuffer
1436 * Unregisters and releases the framebuffer
1438 * Return: 0 if successful, negative if error
1440 int fbtft_remove_common(struct device *dev, struct fb_info *info)
1442 struct fbtft_par *par;
1448 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1449 "%s()\n", __func__);
1450 fbtft_unregister_framebuffer(info);
1451 fbtft_framebuffer_release(info);
1455 EXPORT_SYMBOL(fbtft_remove_common);
1457 MODULE_LICENSE("GPL");