1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Noralf Tronnes
5 * This driver is inspired by:
6 * st7735fb.c, Copyright (C) 2011, Matt Porter
7 * broadsheetfb.c, Copyright (C) 2008, Jaya Kumar
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/string.h>
15 #include <linux/vmalloc.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
19 #include <linux/gpio.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/spinlock.h>
27 #include <linux/of_gpio.h>
28 #include <video/mipi_display.h>
33 static unsigned long debug;
34 module_param(debug, ulong, 0000);
35 MODULE_PARM_DESC(debug, "override device debug level");
37 int fbtft_write_buf_dc(struct fbtft_par *par, void *buf, size_t len, int dc)
41 if (gpio_is_valid(par->gpio.dc))
42 gpio_set_value(par->gpio.dc, dc);
44 ret = par->fbtftops.write(par, buf, len);
46 dev_err(par->info->device,
47 "write() failed and returned %d\n", ret);
50 EXPORT_SYMBOL(fbtft_write_buf_dc);
52 void fbtft_dbg_hex(const struct device *dev, int groupsize,
53 void *buf, size_t len, const char *fmt, ...)
56 static char textbuf[512];
61 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
64 hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len,
65 512 - text_len, false);
68 dev_info(dev, "%s ...\n", text);
70 dev_info(dev, "%s\n", text);
72 EXPORT_SYMBOL(fbtft_dbg_hex);
74 static unsigned long fbtft_request_gpios_match(struct fbtft_par *par,
75 const struct fbtft_gpio *gpio)
80 fbtft_par_dbg(DEBUG_REQUEST_GPIOS_MATCH, par, "%s('%s')\n",
81 __func__, gpio->name);
83 if (strcasecmp(gpio->name, "reset") == 0) {
84 par->gpio.reset = gpio->gpio;
85 return GPIOF_OUT_INIT_HIGH;
86 } else if (strcasecmp(gpio->name, "dc") == 0) {
87 par->gpio.dc = gpio->gpio;
88 return GPIOF_OUT_INIT_LOW;
89 } else if (strcasecmp(gpio->name, "cs") == 0) {
90 par->gpio.cs = gpio->gpio;
91 return GPIOF_OUT_INIT_HIGH;
92 } else if (strcasecmp(gpio->name, "wr") == 0) {
93 par->gpio.wr = gpio->gpio;
94 return GPIOF_OUT_INIT_HIGH;
95 } else if (strcasecmp(gpio->name, "rd") == 0) {
96 par->gpio.rd = gpio->gpio;
97 return GPIOF_OUT_INIT_HIGH;
98 } else if (strcasecmp(gpio->name, "latch") == 0) {
99 par->gpio.latch = gpio->gpio;
100 return GPIOF_OUT_INIT_LOW;
101 } else if (gpio->name[0] == 'd' && gpio->name[1] == 'b') {
102 ret = kstrtouint(&gpio->name[2], 10, &val);
103 if (ret == 0 && val < 16) {
104 par->gpio.db[val] = gpio->gpio;
105 return GPIOF_OUT_INIT_LOW;
107 } else if (strcasecmp(gpio->name, "led") == 0) {
108 par->gpio.led[0] = gpio->gpio;
109 return GPIOF_OUT_INIT_LOW;
110 } else if (strcasecmp(gpio->name, "led_") == 0) {
111 par->gpio.led[0] = gpio->gpio;
112 return GPIOF_OUT_INIT_HIGH;
115 return FBTFT_GPIO_NO_MATCH;
118 static int fbtft_request_gpios(struct fbtft_par *par)
120 struct fbtft_platform_data *pdata = par->pdata;
121 const struct fbtft_gpio *gpio;
125 if (!(pdata && pdata->gpios))
129 while (gpio->name[0]) {
130 flags = FBTFT_GPIO_NO_MATCH;
131 /* if driver provides match function, try it first,
132 * if no match use our own
134 if (par->fbtftops.request_gpios_match)
135 flags = par->fbtftops.request_gpios_match(par, gpio);
136 if (flags == FBTFT_GPIO_NO_MATCH)
137 flags = fbtft_request_gpios_match(par, gpio);
138 if (flags != FBTFT_GPIO_NO_MATCH) {
139 ret = devm_gpio_request_one(par->info->device,
141 par->info->device->driver->name);
143 dev_err(par->info->device,
144 "%s: gpio_request_one('%s'=%d) failed with %d\n",
145 __func__, gpio->name,
149 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par,
150 "%s: '%s' = GPIO%d\n",
151 __func__, gpio->name, gpio->gpio);
160 static int fbtft_request_one_gpio(struct fbtft_par *par,
161 const char *name, int index, int *gpiop)
163 struct device *dev = par->info->device;
164 struct device_node *node = dev->of_node;
165 int gpio, flags, ret = 0;
166 enum of_gpio_flags of_flags;
168 if (of_find_property(node, name, NULL)) {
169 gpio = of_get_named_gpio_flags(node, name, index, &of_flags);
172 if (gpio == -EPROBE_DEFER)
176 "failed to get '%s' from DT\n", name);
180 /* active low translates to initially low */
181 flags = (of_flags & OF_GPIO_ACTIVE_LOW) ? GPIOF_OUT_INIT_LOW :
183 ret = devm_gpio_request_one(dev, gpio, flags,
187 "gpio_request_one('%s'=%d) failed with %d\n",
193 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par, "%s: '%s' = GPIO%d\n",
194 __func__, name, gpio);
200 static int fbtft_request_gpios_dt(struct fbtft_par *par)
205 if (!par->info->device->of_node)
208 ret = fbtft_request_one_gpio(par, "reset-gpios", 0, &par->gpio.reset);
211 ret = fbtft_request_one_gpio(par, "dc-gpios", 0, &par->gpio.dc);
214 ret = fbtft_request_one_gpio(par, "rd-gpios", 0, &par->gpio.rd);
217 ret = fbtft_request_one_gpio(par, "wr-gpios", 0, &par->gpio.wr);
220 ret = fbtft_request_one_gpio(par, "cs-gpios", 0, &par->gpio.cs);
223 ret = fbtft_request_one_gpio(par, "latch-gpios", 0, &par->gpio.latch);
226 for (i = 0; i < 16; i++) {
227 ret = fbtft_request_one_gpio(par, "db-gpios", i,
231 ret = fbtft_request_one_gpio(par, "led-gpios", i,
235 ret = fbtft_request_one_gpio(par, "aux-gpios", i,
245 #ifdef CONFIG_FB_BACKLIGHT
246 static int fbtft_backlight_update_status(struct backlight_device *bd)
248 struct fbtft_par *par = bl_get_data(bd);
249 bool polarity = par->polarity;
251 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
252 "%s: polarity=%d, power=%d, fb_blank=%d\n",
253 __func__, polarity, bd->props.power, bd->props.fb_blank);
255 if ((bd->props.power == FB_BLANK_UNBLANK) &&
256 (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 par->polarity = true;
301 bd = backlight_device_register(dev_driver_string(par->info->device),
302 par->info->device, par,
303 &fbtft_bl_ops, &bl_props);
305 dev_err(par->info->device,
306 "cannot register backlight device (%ld)\n",
310 par->info->bl_dev = bd;
312 if (!par->fbtftops.unregister_backlight)
313 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
316 void fbtft_register_backlight(struct fbtft_par *par) { };
317 void fbtft_unregister_backlight(struct fbtft_par *par) { };
319 EXPORT_SYMBOL(fbtft_register_backlight);
320 EXPORT_SYMBOL(fbtft_unregister_backlight);
322 static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
325 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
326 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
328 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
329 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
331 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
334 static void fbtft_reset(struct fbtft_par *par)
336 if (par->gpio.reset == -1)
338 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
339 gpio_set_value_cansleep(par->gpio.reset, 0);
340 usleep_range(20, 40);
341 gpio_set_value_cansleep(par->gpio.reset, 1);
345 static void fbtft_update_display(struct fbtft_par *par, unsigned int start_line,
346 unsigned int end_line)
349 ktime_t ts_start, ts_end;
350 long fps, throughput;
354 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE |
355 DEBUG_TIME_EACH_UPDATE))) {
356 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
357 ((par->debug & DEBUG_TIME_FIRST_UPDATE) &&
358 !par->first_update_done)) {
359 ts_start = ktime_get();
365 if (start_line > end_line) {
366 dev_warn(par->info->device,
367 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
368 __func__, start_line, end_line);
370 end_line = par->info->var.yres - 1;
372 if (start_line > par->info->var.yres - 1 ||
373 end_line > par->info->var.yres - 1) {
374 dev_warn(par->info->device,
375 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
376 __func__, start_line,
377 end_line, par->info->var.yres - 1);
379 end_line = par->info->var.yres - 1;
382 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
383 __func__, start_line, end_line);
385 if (par->fbtftops.set_addr_win)
386 par->fbtftops.set_addr_win(par, 0, start_line,
387 par->info->var.xres - 1, end_line);
389 offset = start_line * par->info->fix.line_length;
390 len = (end_line - start_line + 1) * par->info->fix.line_length;
391 ret = par->fbtftops.write_vmem(par, offset, len);
393 dev_err(par->info->device,
394 "%s: write_vmem failed to update display buffer\n",
397 if (unlikely(timeit)) {
398 ts_end = ktime_get();
399 if (!ktime_to_ns(par->update_time))
400 par->update_time = ts_start;
402 fps = ktime_us_delta(ts_start, par->update_time);
403 par->update_time = ts_start;
404 fps = fps ? 1000000 / fps : 0;
406 throughput = ktime_us_delta(ts_end, ts_start);
407 throughput = throughput ? (len * 1000) / throughput : 0;
408 throughput = throughput * 1000 / 1024;
410 dev_info(par->info->device,
411 "Display update: %ld kB/s, fps=%ld\n",
413 par->first_update_done = true;
417 static void fbtft_mkdirty(struct fb_info *info, int y, int height)
419 struct fbtft_par *par = info->par;
420 struct fb_deferred_io *fbdefio = info->fbdefio;
422 /* special case, needed ? */
425 height = info->var.yres - 1;
428 /* Mark display lines/area as dirty */
429 spin_lock(&par->dirty_lock);
430 if (y < par->dirty_lines_start)
431 par->dirty_lines_start = y;
432 if (y + height - 1 > par->dirty_lines_end)
433 par->dirty_lines_end = y + height - 1;
434 spin_unlock(&par->dirty_lock);
436 /* Schedule deferred_io to update display (no-op if already on queue)*/
437 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
440 static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
442 struct fbtft_par *par = info->par;
443 unsigned int dirty_lines_start, dirty_lines_end;
446 unsigned int y_low = 0, y_high = 0;
449 spin_lock(&par->dirty_lock);
450 dirty_lines_start = par->dirty_lines_start;
451 dirty_lines_end = par->dirty_lines_end;
452 /* set display line markers as clean */
453 par->dirty_lines_start = par->info->var.yres - 1;
454 par->dirty_lines_end = 0;
455 spin_unlock(&par->dirty_lock);
457 /* Mark display lines as dirty */
458 list_for_each_entry(page, pagelist, lru) {
460 index = page->index << PAGE_SHIFT;
461 y_low = index / info->fix.line_length;
462 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
463 dev_dbg(info->device,
464 "page->index=%lu y_low=%d y_high=%d\n",
465 page->index, y_low, y_high);
466 if (y_high > info->var.yres - 1)
467 y_high = info->var.yres - 1;
468 if (y_low < dirty_lines_start)
469 dirty_lines_start = y_low;
470 if (y_high > dirty_lines_end)
471 dirty_lines_end = y_high;
474 par->fbtftops.update_display(info->par,
475 dirty_lines_start, dirty_lines_end);
478 static void fbtft_fb_fillrect(struct fb_info *info,
479 const struct fb_fillrect *rect)
481 struct fbtft_par *par = info->par;
484 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
485 __func__, rect->dx, rect->dy, rect->width, rect->height);
486 sys_fillrect(info, rect);
488 par->fbtftops.mkdirty(info, rect->dy, rect->height);
491 static void fbtft_fb_copyarea(struct fb_info *info,
492 const struct fb_copyarea *area)
494 struct fbtft_par *par = info->par;
497 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
498 __func__, area->dx, area->dy, area->width, area->height);
499 sys_copyarea(info, area);
501 par->fbtftops.mkdirty(info, area->dy, area->height);
504 static void fbtft_fb_imageblit(struct fb_info *info,
505 const struct fb_image *image)
507 struct fbtft_par *par = info->par;
510 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
511 __func__, image->dx, image->dy, image->width, image->height);
512 sys_imageblit(info, image);
514 par->fbtftops.mkdirty(info, image->dy, image->height);
517 static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
518 size_t count, loff_t *ppos)
520 struct fbtft_par *par = info->par;
524 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
525 res = fb_sys_write(info, buf, count, ppos);
527 /* TODO: only mark changed area update all for now */
528 par->fbtftops.mkdirty(info, -1, 0);
534 static unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
537 chan >>= 16 - bf->length;
538 return chan << bf->offset;
541 static int fbtft_fb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
542 unsigned int blue, unsigned int transp,
543 struct fb_info *info)
549 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
550 __func__, regno, red, green, blue, transp);
552 switch (info->fix.visual) {
553 case FB_VISUAL_TRUECOLOR:
555 u32 *pal = info->pseudo_palette;
557 val = chan_to_field(red, &info->var.red);
558 val |= chan_to_field(green, &info->var.green);
559 val |= chan_to_field(blue, &info->var.blue);
569 static int fbtft_fb_blank(int blank, struct fb_info *info)
571 struct fbtft_par *par = info->par;
574 dev_dbg(info->dev, "%s(blank=%d)\n",
577 if (!par->fbtftops.blank)
581 case FB_BLANK_POWERDOWN:
582 case FB_BLANK_VSYNC_SUSPEND:
583 case FB_BLANK_HSYNC_SUSPEND:
584 case FB_BLANK_NORMAL:
585 ret = par->fbtftops.blank(par, true);
587 case FB_BLANK_UNBLANK:
588 ret = par->fbtftops.blank(par, false);
594 static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
597 dst->write = src->write;
599 dst->read = src->read;
601 dst->write_vmem = src->write_vmem;
602 if (src->write_register)
603 dst->write_register = src->write_register;
604 if (src->set_addr_win)
605 dst->set_addr_win = src->set_addr_win;
607 dst->reset = src->reset;
609 dst->mkdirty = src->mkdirty;
610 if (src->update_display)
611 dst->update_display = src->update_display;
612 if (src->init_display)
613 dst->init_display = src->init_display;
615 dst->blank = src->blank;
616 if (src->request_gpios_match)
617 dst->request_gpios_match = src->request_gpios_match;
618 if (src->request_gpios)
619 dst->request_gpios = src->request_gpios;
620 if (src->verify_gpios)
621 dst->verify_gpios = src->verify_gpios;
622 if (src->register_backlight)
623 dst->register_backlight = src->register_backlight;
624 if (src->unregister_backlight)
625 dst->unregister_backlight = src->unregister_backlight;
627 dst->set_var = src->set_var;
629 dst->set_gamma = src->set_gamma;
633 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
635 * @display: pointer to structure describing the display
636 * @dev: pointer to the device for this fb, this can be NULL
638 * Creates a new frame buffer info structure.
640 * Also creates and populates the following structures:
643 * info->pseudo_palette
647 * Returns the new structure, or NULL if an error occurred.
650 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
652 struct fbtft_platform_data *pdata)
654 struct fb_info *info;
655 struct fbtft_par *par;
656 struct fb_ops *fbops = NULL;
657 struct fb_deferred_io *fbdefio = NULL;
663 int txbuflen = display->txbuflen;
664 unsigned int bpp = display->bpp;
665 unsigned int fps = display->fps;
667 const s16 *init_sequence = display->init_sequence;
668 char *gamma = display->gamma;
669 u32 *gamma_curves = NULL;
672 if (display->gamma_num * display->gamma_len >
673 FBTFT_GAMMA_MAX_VALUES_TOTAL) {
674 dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
675 FBTFT_GAMMA_MAX_VALUES_TOTAL);
686 dev_err(dev, "platform data is missing\n");
690 /* override driver values? */
694 txbuflen = pdata->txbuflen;
695 if (pdata->display.init_sequence)
696 init_sequence = pdata->display.init_sequence;
698 gamma = pdata->gamma;
699 if (pdata->display.debug)
700 display->debug = pdata->display.debug;
701 if (pdata->display.backlight)
702 display->backlight = pdata->display.backlight;
703 if (pdata->display.width)
704 display->width = pdata->display.width;
705 if (pdata->display.height)
706 display->height = pdata->display.height;
707 if (pdata->display.buswidth)
708 display->buswidth = pdata->display.buswidth;
709 if (pdata->display.regwidth)
710 display->regwidth = pdata->display.regwidth;
712 display->debug |= debug;
713 fbtft_expand_debug_value(&display->debug);
715 switch (pdata->rotate) {
718 width = display->height;
719 height = display->width;
722 width = display->width;
723 height = display->height;
726 vmem_size = display->width * display->height * bpp / 8;
727 vmem = vzalloc(vmem_size);
731 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
735 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
739 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
743 if (display->gamma_num && display->gamma_len) {
744 gamma_curves = devm_kcalloc(dev,
747 sizeof(gamma_curves[0]),
753 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
757 info->screen_buffer = vmem;
759 info->fbdefio = fbdefio;
761 fbops->owner = dev->driver->owner;
762 fbops->fb_read = fb_sys_read;
763 fbops->fb_write = fbtft_fb_write;
764 fbops->fb_fillrect = fbtft_fb_fillrect;
765 fbops->fb_copyarea = fbtft_fb_copyarea;
766 fbops->fb_imageblit = fbtft_fb_imageblit;
767 fbops->fb_setcolreg = fbtft_fb_setcolreg;
768 fbops->fb_blank = fbtft_fb_blank;
770 fbdefio->delay = HZ / fps;
771 fbdefio->deferred_io = fbtft_deferred_io;
772 fb_deferred_io_init(info);
774 snprintf(info->fix.id, sizeof(info->fix.id), "%s", dev->driver->name);
775 info->fix.type = FB_TYPE_PACKED_PIXELS;
776 info->fix.visual = FB_VISUAL_TRUECOLOR;
777 info->fix.xpanstep = 0;
778 info->fix.ypanstep = 0;
779 info->fix.ywrapstep = 0;
780 info->fix.line_length = width * bpp / 8;
781 info->fix.accel = FB_ACCEL_NONE;
782 info->fix.smem_len = vmem_size;
784 info->var.rotate = pdata->rotate;
785 info->var.xres = width;
786 info->var.yres = height;
787 info->var.xres_virtual = info->var.xres;
788 info->var.yres_virtual = info->var.yres;
789 info->var.bits_per_pixel = bpp;
790 info->var.nonstd = 1;
793 info->var.red.offset = 11;
794 info->var.red.length = 5;
795 info->var.green.offset = 5;
796 info->var.green.length = 6;
797 info->var.blue.offset = 0;
798 info->var.blue.length = 5;
799 info->var.transp.offset = 0;
800 info->var.transp.length = 0;
802 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
807 par->debug = display->debug;
809 spin_lock_init(&par->dirty_lock);
810 par->bgr = pdata->bgr;
811 par->startbyte = pdata->startbyte;
812 par->init_sequence = init_sequence;
813 par->gamma.curves = gamma_curves;
814 par->gamma.num_curves = display->gamma_num;
815 par->gamma.num_values = display->gamma_len;
816 mutex_init(&par->gamma.lock);
817 info->pseudo_palette = par->pseudo_palette;
819 if (par->gamma.curves && gamma) {
820 if (fbtft_gamma_parse_str(par, par->gamma.curves, gamma,
822 goto release_framebuf;
825 /* Transmit buffer */
827 txbuflen = vmem_size + 2; /* add in case startbyte is used */
828 if (txbuflen >= vmem_size + 2)
831 #ifdef __LITTLE_ENDIAN
832 if ((!txbuflen) && (bpp > 8))
833 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
837 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
839 goto release_framebuf;
840 par->txbuf.buf = txbuf;
841 par->txbuf.len = txbuflen;
844 /* Initialize gpios to disabled */
845 par->gpio.reset = -1;
850 par->gpio.latch = -1;
851 for (i = 0; i < 16; i++) {
852 par->gpio.db[i] = -1;
853 par->gpio.led[i] = -1;
854 par->gpio.aux[i] = -1;
857 /* default fbtft operations */
858 par->fbtftops.write = fbtft_write_spi;
859 par->fbtftops.read = fbtft_read_spi;
860 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
861 par->fbtftops.write_register = fbtft_write_reg8_bus8;
862 par->fbtftops.set_addr_win = fbtft_set_addr_win;
863 par->fbtftops.reset = fbtft_reset;
864 par->fbtftops.mkdirty = fbtft_mkdirty;
865 par->fbtftops.update_display = fbtft_update_display;
866 par->fbtftops.request_gpios = fbtft_request_gpios;
867 if (display->backlight)
868 par->fbtftops.register_backlight = fbtft_register_backlight;
870 /* use driver provided functions */
871 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
876 framebuffer_release(info);
883 EXPORT_SYMBOL(fbtft_framebuffer_alloc);
886 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
888 * @info: frame buffer info structure
891 void fbtft_framebuffer_release(struct fb_info *info)
893 fb_deferred_io_cleanup(info);
894 vfree(info->screen_buffer);
895 framebuffer_release(info);
897 EXPORT_SYMBOL(fbtft_framebuffer_release);
900 * fbtft_register_framebuffer - registers a tft frame buffer device
901 * @fb_info: frame buffer info structure
903 * Sets SPI driverdata if needed
904 * Requests needed gpios.
905 * Initializes display
907 * Registers a frame buffer device @fb_info.
909 * Returns negative errno on error, or zero for success.
912 int fbtft_register_framebuffer(struct fb_info *fb_info)
917 struct fbtft_par *par = fb_info->par;
918 struct spi_device *spi = par->spi;
921 if (!par->fbtftops.init_display) {
922 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
927 spi_set_drvdata(spi, fb_info);
929 platform_set_drvdata(par->pdev, fb_info);
931 ret = par->fbtftops.request_gpios(par);
935 if (par->fbtftops.verify_gpios) {
936 ret = par->fbtftops.verify_gpios(par);
941 ret = par->fbtftops.init_display(par);
944 if (par->fbtftops.set_var) {
945 ret = par->fbtftops.set_var(par);
950 /* update the entire display */
951 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
953 if (par->fbtftops.set_gamma && par->gamma.curves) {
954 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
959 if (par->fbtftops.register_backlight)
960 par->fbtftops.register_backlight(par);
962 ret = register_framebuffer(fb_info);
966 fbtft_sysfs_init(par);
968 if (par->txbuf.buf && par->txbuf.len >= 1024)
969 sprintf(text1, ", %zu KiB buffer memory", par->txbuf.len >> 10);
971 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
972 spi->chip_select, spi->max_speed_hz / 1000000);
973 dev_info(fb_info->dev,
974 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
975 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
976 fb_info->fix.smem_len >> 10, text1,
977 HZ / fb_info->fbdefio->delay, text2);
979 #ifdef CONFIG_FB_BACKLIGHT
980 /* Turn on backlight if available */
981 if (fb_info->bl_dev) {
982 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
983 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
990 if (par->fbtftops.unregister_backlight)
991 par->fbtftops.unregister_backlight(par);
995 EXPORT_SYMBOL(fbtft_register_framebuffer);
998 * fbtft_unregister_framebuffer - releases a tft frame buffer device
999 * @fb_info: frame buffer info structure
1001 * Frees SPI driverdata if needed
1003 * Unregisters frame buffer device.
1006 int fbtft_unregister_framebuffer(struct fb_info *fb_info)
1008 struct fbtft_par *par = fb_info->par;
1010 if (par->fbtftops.unregister_backlight)
1011 par->fbtftops.unregister_backlight(par);
1012 fbtft_sysfs_exit(par);
1013 return unregister_framebuffer(fb_info);
1015 EXPORT_SYMBOL(fbtft_unregister_framebuffer);
1019 * fbtft_init_display_dt() - Device Tree init_display() function
1022 * Return: 0 if successful, negative if error
1024 static int fbtft_init_display_dt(struct fbtft_par *par)
1026 struct device_node *node = par->info->device->of_node;
1027 struct property *prop;
1035 prop = of_find_property(node, "init", NULL);
1036 p = of_prop_next_u32(prop, NULL, &val);
1040 par->fbtftops.reset(par);
1041 if (par->gpio.cs != -1)
1042 gpio_set_value(par->gpio.cs, 0); /* Activate chip */
1045 if (val & FBTFT_OF_INIT_CMD) {
1048 while (p && !(val & 0xFFFF0000)) {
1050 dev_err(par->info->device,
1051 "%s: Maximum register values exceeded\n",
1056 p = of_prop_next_u32(prop, p, &val);
1058 /* make debug message */
1059 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1060 "init: write_register:\n");
1061 for (j = 0; j < i; j++)
1062 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1063 "buf[%d] = %02X\n", j, buf[j]);
1065 par->fbtftops.write_register(par, i,
1066 buf[0], buf[1], buf[2], buf[3],
1067 buf[4], buf[5], buf[6], buf[7],
1068 buf[8], buf[9], buf[10], buf[11],
1069 buf[12], buf[13], buf[14], buf[15],
1070 buf[16], buf[17], buf[18], buf[19],
1071 buf[20], buf[21], buf[22], buf[23],
1072 buf[24], buf[25], buf[26], buf[27],
1073 buf[28], buf[29], buf[30], buf[31],
1074 buf[32], buf[33], buf[34], buf[35],
1075 buf[36], buf[37], buf[38], buf[39],
1076 buf[40], buf[41], buf[42], buf[43],
1077 buf[44], buf[45], buf[46], buf[47],
1078 buf[48], buf[49], buf[50], buf[51],
1079 buf[52], buf[53], buf[54], buf[55],
1080 buf[56], buf[57], buf[58], buf[59],
1081 buf[60], buf[61], buf[62], buf[63]);
1082 } else if (val & FBTFT_OF_INIT_DELAY) {
1083 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1084 "init: msleep(%u)\n", val & 0xFFFF);
1085 msleep(val & 0xFFFF);
1086 p = of_prop_next_u32(prop, p, &val);
1088 dev_err(par->info->device, "illegal init value 0x%X\n",
1099 * fbtft_init_display() - Generic init_display() function
1102 * Uses par->init_sequence to do the initialization
1104 * Return: 0 if successful, negative if error
1106 int fbtft_init_display(struct fbtft_par *par)
1115 if (!par->init_sequence) {
1116 dev_err(par->info->device,
1117 "error: init_sequence is not set\n");
1121 /* make sure stop marker exists */
1122 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++)
1123 if (par->init_sequence[i] == -3)
1125 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1126 dev_err(par->info->device,
1127 "missing stop marker at end of init sequence\n");
1131 par->fbtftops.reset(par);
1132 if (par->gpio.cs != -1)
1133 gpio_set_value(par->gpio.cs, 0); /* Activate chip */
1136 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1137 if (par->init_sequence[i] == -3) {
1141 if (par->init_sequence[i] >= 0) {
1142 dev_err(par->info->device,
1143 "missing delimiter at position %d\n", i);
1146 if (par->init_sequence[i + 1] < 0) {
1147 dev_err(par->info->device,
1148 "missing value after delimiter %d at position %d\n",
1149 par->init_sequence[i], i);
1152 switch (par->init_sequence[i]) {
1155 /* make debug message */
1158 while (par->init_sequence[j] >= 0) {
1159 sprintf(str, "0x%02X ", par->init_sequence[j]);
1163 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1164 "init: write(0x%02X) %s\n",
1165 par->init_sequence[i], msg);
1169 while (par->init_sequence[i] >= 0) {
1171 dev_err(par->info->device,
1172 "%s: Maximum register values exceeded\n",
1176 buf[j++] = par->init_sequence[i++];
1178 par->fbtftops.write_register(par, j,
1179 buf[0], buf[1], buf[2], buf[3],
1180 buf[4], buf[5], buf[6], buf[7],
1181 buf[8], buf[9], buf[10], buf[11],
1182 buf[12], buf[13], buf[14], buf[15],
1183 buf[16], buf[17], buf[18], buf[19],
1184 buf[20], buf[21], buf[22], buf[23],
1185 buf[24], buf[25], buf[26], buf[27],
1186 buf[28], buf[29], buf[30], buf[31],
1187 buf[32], buf[33], buf[34], buf[35],
1188 buf[36], buf[37], buf[38], buf[39],
1189 buf[40], buf[41], buf[42], buf[43],
1190 buf[44], buf[45], buf[46], buf[47],
1191 buf[48], buf[49], buf[50], buf[51],
1192 buf[52], buf[53], buf[54], buf[55],
1193 buf[56], buf[57], buf[58], buf[59],
1194 buf[60], buf[61], buf[62], buf[63]);
1198 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1199 "init: mdelay(%d)\n",
1200 par->init_sequence[i]);
1201 mdelay(par->init_sequence[i++]);
1204 dev_err(par->info->device,
1205 "unknown delimiter %d at position %d\n",
1206 par->init_sequence[i], i);
1211 dev_err(par->info->device,
1212 "%s: something is wrong. Shouldn't get here.\n", __func__);
1215 EXPORT_SYMBOL(fbtft_init_display);
1218 * fbtft_verify_gpios() - Generic verify_gpios() function
1221 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1223 * Return: 0 if successful, negative if error
1225 static int fbtft_verify_gpios(struct fbtft_par *par)
1227 struct fbtft_platform_data *pdata = par->pdata;
1230 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1232 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
1234 dev_err(par->info->device,
1235 "Missing info about 'dc' gpio. Aborting.\n");
1242 if (par->gpio.wr < 0) {
1243 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1246 for (i = 0; i < pdata->display.buswidth; i++) {
1247 if (par->gpio.db[i] < 0) {
1248 dev_err(par->info->device,
1249 "Missing 'db%02d' gpio. Aborting.\n", i);
1258 /* returns 0 if the property is not present */
1259 static u32 fbtft_of_value(struct device_node *node, const char *propname)
1264 ret = of_property_read_u32(node, propname, &val);
1266 pr_info("%s: %s = %u\n", __func__, propname, val);
1271 static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1273 struct device_node *node = dev->of_node;
1274 struct fbtft_platform_data *pdata;
1277 dev_err(dev, "Missing platform data or DT\n");
1278 return ERR_PTR(-EINVAL);
1281 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1283 return ERR_PTR(-ENOMEM);
1285 pdata->display.width = fbtft_of_value(node, "width");
1286 pdata->display.height = fbtft_of_value(node, "height");
1287 pdata->display.regwidth = fbtft_of_value(node, "regwidth");
1288 pdata->display.buswidth = fbtft_of_value(node, "buswidth");
1289 pdata->display.backlight = fbtft_of_value(node, "backlight");
1290 pdata->display.bpp = fbtft_of_value(node, "bpp");
1291 pdata->display.debug = fbtft_of_value(node, "debug");
1292 pdata->rotate = fbtft_of_value(node, "rotate");
1293 pdata->bgr = of_property_read_bool(node, "bgr");
1294 pdata->fps = fbtft_of_value(node, "fps");
1295 pdata->txbuflen = fbtft_of_value(node, "txbuflen");
1296 pdata->startbyte = fbtft_of_value(node, "startbyte");
1297 of_property_read_string(node, "gamma", (const char **)&pdata->gamma);
1299 if (of_find_property(node, "led-gpios", NULL))
1300 pdata->display.backlight = 1;
1301 if (of_find_property(node, "init", NULL))
1302 pdata->display.fbtftops.init_display = fbtft_init_display_dt;
1303 pdata->display.fbtftops.request_gpios = fbtft_request_gpios_dt;
1308 static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1310 dev_err(dev, "Missing platform data\n");
1311 return ERR_PTR(-EINVAL);
1316 * fbtft_probe_common() - Generic device probe() helper function
1317 * @display: Display properties
1319 * @pdev: Platform device
1321 * Allocates, initializes and registers a framebuffer
1323 * Either @sdev or @pdev should be NULL
1325 * Return: 0 if successful, negative if error
1327 int fbtft_probe_common(struct fbtft_display *display,
1328 struct spi_device *sdev,
1329 struct platform_device *pdev)
1332 struct fb_info *info;
1333 struct fbtft_par *par;
1334 struct fbtft_platform_data *pdata;
1342 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1343 dev_info(dev, "%s()\n", __func__);
1345 pdata = dev->platform_data;
1347 pdata = fbtft_probe_dt(dev);
1349 return PTR_ERR(pdata);
1352 info = fbtft_framebuffer_alloc(display, dev, pdata);
1360 if (display->buswidth == 0) {
1361 dev_err(dev, "buswidth is not set\n");
1365 /* write register functions */
1366 if (display->regwidth == 8 && display->buswidth == 8)
1367 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1368 else if (display->regwidth == 8 && display->buswidth == 9 && par->spi)
1369 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1370 else if (display->regwidth == 16 && display->buswidth == 8)
1371 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1372 else if (display->regwidth == 16 && display->buswidth == 16)
1373 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1376 "no default functions for regwidth=%d and buswidth=%d\n",
1377 display->regwidth, display->buswidth);
1379 /* write_vmem() functions */
1380 if (display->buswidth == 8)
1381 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1382 else if (display->buswidth == 9)
1383 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1384 else if (display->buswidth == 16)
1385 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1387 /* GPIO write() functions */
1389 if (display->buswidth == 8)
1390 par->fbtftops.write = fbtft_write_gpio8_wr;
1391 else if (display->buswidth == 16)
1392 par->fbtftops.write = fbtft_write_gpio16_wr;
1395 /* 9-bit SPI setup */
1396 if (par->spi && display->buswidth == 9) {
1397 if (par->spi->master->bits_per_word_mask & SPI_BPW_MASK(9)) {
1398 par->spi->bits_per_word = 9;
1400 dev_warn(&par->spi->dev,
1401 "9-bit SPI not available, emulating using 8-bit.\n");
1402 /* allocate buffer with room for dc bits */
1403 par->extra = devm_kzalloc(par->info->device,
1405 (par->txbuf.len / 8) + 8,
1411 par->fbtftops.write = fbtft_write_spi_emulate_9;
1415 if (!par->fbtftops.verify_gpios)
1416 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1418 /* make sure we still use the driver provided functions */
1419 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1421 /* use init_sequence if provided */
1422 if (par->init_sequence)
1423 par->fbtftops.init_display = fbtft_init_display;
1425 /* use platform_data provided functions above all */
1426 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1428 ret = fbtft_register_framebuffer(info);
1435 fbtft_framebuffer_release(info);
1439 EXPORT_SYMBOL(fbtft_probe_common);
1442 * fbtft_remove_common() - Generic device remove() helper function
1444 * @info: Framebuffer
1446 * Unregisters and releases the framebuffer
1448 * Return: 0 if successful, negative if error
1450 int fbtft_remove_common(struct device *dev, struct fb_info *info)
1452 struct fbtft_par *par;
1458 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1459 "%s()\n", __func__);
1460 fbtft_unregister_framebuffer(info);
1461 fbtft_framebuffer_release(info);
1465 EXPORT_SYMBOL(fbtft_remove_common);
1467 MODULE_LICENSE("GPL");