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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
28 #include <linux/vmalloc.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
32 #include <linux/gpio.h>
33 #include <linux/spi/spi.h>
34 #include <linux/delay.h>
35 #include <linux/uaccess.h>
36 #include <linux/backlight.h>
37 #include <linux/platform_device.h>
38 #include <linux/spinlock.h>
39 #include <linux/dma-mapping.h>
41 #include <linux/of_gpio.h>
46 static unsigned long debug;
47 module_param(debug, ulong, 0);
48 MODULE_PARM_DESC(debug, "override device debug level");
50 static bool dma = true;
51 module_param(dma, bool, 0);
52 MODULE_PARM_DESC(dma, "Use DMA buffer");
55 void fbtft_dbg_hex(const struct device *dev, int groupsize,
56 void *buf, size_t len, const char *fmt, ...)
59 static char textbuf[512];
64 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
67 hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len,
68 512 - text_len, false);
71 dev_info(dev, "%s ...\n", text);
73 dev_info(dev, "%s\n", text);
75 EXPORT_SYMBOL(fbtft_dbg_hex);
77 static unsigned long fbtft_request_gpios_match(struct fbtft_par *par,
78 const struct fbtft_gpio *gpio)
83 fbtft_par_dbg(DEBUG_REQUEST_GPIOS_MATCH, par, "%s('%s')\n",
84 __func__, gpio->name);
86 if (strcasecmp(gpio->name, "reset") == 0) {
87 par->gpio.reset = gpio->gpio;
88 return GPIOF_OUT_INIT_HIGH;
89 } else if (strcasecmp(gpio->name, "dc") == 0) {
90 par->gpio.dc = gpio->gpio;
91 return GPIOF_OUT_INIT_LOW;
92 } else if (strcasecmp(gpio->name, "cs") == 0) {
93 par->gpio.cs = gpio->gpio;
94 return GPIOF_OUT_INIT_HIGH;
95 } else if (strcasecmp(gpio->name, "wr") == 0) {
96 par->gpio.wr = gpio->gpio;
97 return GPIOF_OUT_INIT_HIGH;
98 } else if (strcasecmp(gpio->name, "rd") == 0) {
99 par->gpio.rd = gpio->gpio;
100 return GPIOF_OUT_INIT_HIGH;
101 } else if (strcasecmp(gpio->name, "latch") == 0) {
102 par->gpio.latch = gpio->gpio;
103 return GPIOF_OUT_INIT_LOW;
104 } else if (gpio->name[0] == 'd' && gpio->name[1] == 'b') {
105 ret = kstrtol(&gpio->name[2], 10, &val);
106 if (ret == 0 && val < 16) {
107 par->gpio.db[val] = gpio->gpio;
108 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_LOW;
113 } else if (strcasecmp(gpio->name, "led_") == 0) {
114 par->gpio.led[0] = gpio->gpio;
115 return GPIOF_OUT_INIT_HIGH;
118 return FBTFT_GPIO_NO_MATCH;
121 static int fbtft_request_gpios(struct fbtft_par *par)
123 struct fbtft_platform_data *pdata = par->pdata;
124 const struct fbtft_gpio *gpio;
128 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 */
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);
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 const struct backlight_ops *bl_ops;
273 fbtft_par_dbg(DEBUG_BACKLIGHT, par, "%s()\n", __func__);
275 if (par->info->bl_dev) {
276 par->info->bl_dev->props.power = FB_BLANK_POWERDOWN;
277 backlight_update_status(par->info->bl_dev);
278 bl_ops = par->info->bl_dev->ops;
279 backlight_device_unregister(par->info->bl_dev);
280 par->info->bl_dev = NULL;
284 void fbtft_register_backlight(struct fbtft_par *par)
286 struct backlight_device *bd;
287 struct backlight_properties bl_props = { 0, };
288 struct backlight_ops *bl_ops;
290 fbtft_par_dbg(DEBUG_BACKLIGHT, par, "%s()\n", __func__);
292 if (par->gpio.led[0] == -1) {
293 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
294 "%s(): led pin not set, exiting.\n", __func__);
298 bl_ops = devm_kzalloc(par->info->device, sizeof(struct backlight_ops),
303 bl_ops->get_brightness = fbtft_backlight_get_brightness;
304 bl_ops->update_status = fbtft_backlight_update_status;
305 bl_props.type = BACKLIGHT_RAW;
306 /* Assume backlight is off, get polarity from current state of pin */
307 bl_props.power = FB_BLANK_POWERDOWN;
308 if (!gpio_get_value(par->gpio.led[0]))
309 bl_props.state |= BL_CORE_DRIVER1;
311 bd = backlight_device_register(dev_driver_string(par->info->device),
312 par->info->device, par, bl_ops, &bl_props);
314 dev_err(par->info->device,
315 "cannot register backlight device (%ld)\n",
319 par->info->bl_dev = bd;
321 if (!par->fbtftops.unregister_backlight)
322 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
325 void fbtft_register_backlight(struct fbtft_par *par) { };
326 void fbtft_unregister_backlight(struct fbtft_par *par) { };
328 EXPORT_SYMBOL(fbtft_register_backlight);
329 EXPORT_SYMBOL(fbtft_unregister_backlight);
331 static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
334 fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
335 "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe, ye);
337 /* Column address set */
339 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
341 /* Row address set */
343 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
346 write_reg(par, 0x2C);
350 static void fbtft_reset(struct fbtft_par *par)
352 if (par->gpio.reset == -1)
354 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
355 gpio_set_value(par->gpio.reset, 0);
357 gpio_set_value(par->gpio.reset, 1);
362 static void fbtft_update_display(struct fbtft_par *par, unsigned start_line,
366 struct timespec ts_start, ts_end, ts_fps, ts_duration;
367 long fps_ms, fps_us, duration_ms, duration_us;
368 long fps, throughput;
372 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE | DEBUG_TIME_EACH_UPDATE))) {
373 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
374 ((par->debug & DEBUG_TIME_FIRST_UPDATE) && !par->first_update_done)) {
375 getnstimeofday(&ts_start);
381 if (start_line > end_line) {
382 dev_warn(par->info->device,
383 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
384 __func__, start_line, end_line);
386 end_line = par->info->var.yres - 1;
388 if (start_line > par->info->var.yres - 1 || end_line > par->info->var.yres - 1) {
389 dev_warn(par->info->device,
390 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
391 __func__, start_line, end_line, par->info->var.yres - 1);
393 end_line = par->info->var.yres - 1;
396 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
397 __func__, start_line, end_line);
399 if (par->fbtftops.set_addr_win)
400 par->fbtftops.set_addr_win(par, 0, start_line,
401 par->info->var.xres-1, end_line);
403 offset = start_line * par->info->fix.line_length;
404 len = (end_line - start_line + 1) * par->info->fix.line_length;
405 ret = par->fbtftops.write_vmem(par, offset, len);
407 dev_err(par->info->device,
408 "%s: write_vmem failed to update display buffer\n",
411 if (unlikely(timeit)) {
412 getnstimeofday(&ts_end);
413 if (par->update_time.tv_nsec == 0 && par->update_time.tv_sec == 0) {
414 par->update_time.tv_sec = ts_start.tv_sec;
415 par->update_time.tv_nsec = ts_start.tv_nsec;
417 ts_fps = timespec_sub(ts_start, par->update_time);
418 par->update_time.tv_sec = ts_start.tv_sec;
419 par->update_time.tv_nsec = ts_start.tv_nsec;
420 fps_ms = (ts_fps.tv_sec * 1000) + ((ts_fps.tv_nsec / 1000000) % 1000);
421 fps_us = (ts_fps.tv_nsec / 1000) % 1000;
422 fps = fps_ms * 1000 + fps_us;
423 fps = fps ? 1000000 / fps : 0;
425 ts_duration = timespec_sub(ts_end, ts_start);
426 duration_ms = (ts_duration.tv_sec * 1000) + ((ts_duration.tv_nsec / 1000000) % 1000);
427 duration_us = (ts_duration.tv_nsec / 1000) % 1000;
428 throughput = duration_ms * 1000 + duration_us;
429 throughput = throughput ? (len * 1000) / throughput : 0;
430 throughput = throughput * 1000 / 1024;
432 dev_info(par->info->device,
433 "Display update: %ld kB/s (%ld.%.3ld ms), fps=%ld (%ld.%.3ld ms)\n",
434 throughput, duration_ms, duration_us,
435 fps, fps_ms, fps_us);
436 par->first_update_done = true;
441 static void fbtft_mkdirty(struct fb_info *info, int y, int height)
443 struct fbtft_par *par = info->par;
444 struct fb_deferred_io *fbdefio = info->fbdefio;
446 /* special case, needed ? */
449 height = info->var.yres - 1;
452 /* Mark display lines/area as dirty */
453 spin_lock(&par->dirty_lock);
454 if (y < par->dirty_lines_start)
455 par->dirty_lines_start = y;
456 if (y + height - 1 > par->dirty_lines_end)
457 par->dirty_lines_end = y + height - 1;
458 spin_unlock(&par->dirty_lock);
460 /* Schedule deferred_io to update display (no-op if already on queue)*/
461 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
464 static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
466 struct fbtft_par *par = info->par;
467 unsigned dirty_lines_start, dirty_lines_end;
470 unsigned y_low = 0, y_high = 0;
473 spin_lock(&par->dirty_lock);
474 dirty_lines_start = par->dirty_lines_start;
475 dirty_lines_end = par->dirty_lines_end;
476 /* set display line markers as clean */
477 par->dirty_lines_start = par->info->var.yres - 1;
478 par->dirty_lines_end = 0;
479 spin_unlock(&par->dirty_lock);
481 /* Mark display lines as dirty */
482 list_for_each_entry(page, pagelist, lru) {
484 index = page->index << PAGE_SHIFT;
485 y_low = index / info->fix.line_length;
486 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
487 fbtft_dev_dbg(DEBUG_DEFERRED_IO, par, info->device,
488 "page->index=%lu y_low=%d y_high=%d\n",
489 page->index, y_low, y_high);
490 if (y_high > info->var.yres - 1)
491 y_high = info->var.yres - 1;
492 if (y_low < dirty_lines_start)
493 dirty_lines_start = y_low;
494 if (y_high > dirty_lines_end)
495 dirty_lines_end = y_high;
498 par->fbtftops.update_display(info->par,
499 dirty_lines_start, dirty_lines_end);
503 static void fbtft_fb_fillrect(struct fb_info *info,
504 const struct fb_fillrect *rect)
506 struct fbtft_par *par = info->par;
508 fbtft_dev_dbg(DEBUG_FB_FILLRECT, par, info->dev,
509 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
510 __func__, rect->dx, rect->dy, rect->width, rect->height);
511 sys_fillrect(info, rect);
513 par->fbtftops.mkdirty(info, rect->dy, rect->height);
516 static void fbtft_fb_copyarea(struct fb_info *info,
517 const struct fb_copyarea *area)
519 struct fbtft_par *par = info->par;
521 fbtft_dev_dbg(DEBUG_FB_COPYAREA, par, info->dev,
522 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
523 __func__, area->dx, area->dy, area->width, area->height);
524 sys_copyarea(info, area);
526 par->fbtftops.mkdirty(info, area->dy, area->height);
529 static void fbtft_fb_imageblit(struct fb_info *info,
530 const struct fb_image *image)
532 struct fbtft_par *par = info->par;
534 fbtft_dev_dbg(DEBUG_FB_IMAGEBLIT, par, info->dev,
535 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
536 __func__, image->dx, image->dy, image->width, image->height);
537 sys_imageblit(info, image);
539 par->fbtftops.mkdirty(info, image->dy, image->height);
542 static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
543 size_t count, loff_t *ppos)
545 struct fbtft_par *par = info->par;
548 fbtft_dev_dbg(DEBUG_FB_WRITE, par, info->dev,
549 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
550 res = fb_sys_write(info, buf, count, ppos);
552 /* TODO: only mark changed area
553 update all for now */
554 par->fbtftops.mkdirty(info, -1, 0);
560 static unsigned int chan_to_field(unsigned chan, struct fb_bitfield *bf)
563 chan >>= 16 - bf->length;
564 return chan << bf->offset;
567 static int fbtft_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
568 unsigned blue, unsigned transp,
569 struct fb_info *info)
571 struct fbtft_par *par = info->par;
575 fbtft_dev_dbg(DEBUG_FB_SETCOLREG, par, info->dev,
576 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
577 __func__, regno, red, green, blue, transp);
579 switch (info->fix.visual) {
580 case FB_VISUAL_TRUECOLOR:
582 u32 *pal = info->pseudo_palette;
584 val = chan_to_field(red, &info->var.red);
585 val |= chan_to_field(green, &info->var.green);
586 val |= chan_to_field(blue, &info->var.blue);
597 static int fbtft_fb_blank(int blank, struct fb_info *info)
599 struct fbtft_par *par = info->par;
602 fbtft_dev_dbg(DEBUG_FB_BLANK, par, info->dev, "%s(blank=%d)\n",
605 if (!par->fbtftops.blank)
609 case FB_BLANK_POWERDOWN:
610 case FB_BLANK_VSYNC_SUSPEND:
611 case FB_BLANK_HSYNC_SUSPEND:
612 case FB_BLANK_NORMAL:
613 ret = par->fbtftops.blank(par, true);
615 case FB_BLANK_UNBLANK:
616 ret = par->fbtftops.blank(par, false);
622 static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
625 dst->write = src->write;
627 dst->read = src->read;
629 dst->write_vmem = src->write_vmem;
630 if (src->write_register)
631 dst->write_register = src->write_register;
632 if (src->set_addr_win)
633 dst->set_addr_win = src->set_addr_win;
635 dst->reset = src->reset;
637 dst->mkdirty = src->mkdirty;
638 if (src->update_display)
639 dst->update_display = src->update_display;
640 if (src->init_display)
641 dst->init_display = src->init_display;
643 dst->blank = src->blank;
644 if (src->request_gpios_match)
645 dst->request_gpios_match = src->request_gpios_match;
646 if (src->request_gpios)
647 dst->request_gpios = src->request_gpios;
648 if (src->verify_gpios)
649 dst->verify_gpios = src->verify_gpios;
650 if (src->register_backlight)
651 dst->register_backlight = src->register_backlight;
652 if (src->unregister_backlight)
653 dst->unregister_backlight = src->unregister_backlight;
655 dst->set_var = src->set_var;
657 dst->set_gamma = src->set_gamma;
661 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
663 * @display: pointer to structure describing the display
664 * @dev: pointer to the device for this fb, this can be NULL
666 * Creates a new frame buffer info structure.
668 * Also creates and populates the following structures:
671 * info->pseudo_palette
675 * Returns the new structure, or NULL if an error occurred.
678 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
681 struct fb_info *info;
682 struct fbtft_par *par;
683 struct fb_ops *fbops = NULL;
684 struct fb_deferred_io *fbdefio = NULL;
685 struct fbtft_platform_data *pdata = dev->platform_data;
691 int txbuflen = display->txbuflen;
692 unsigned bpp = display->bpp;
693 unsigned fps = display->fps;
695 int *init_sequence = display->init_sequence;
696 char *gamma = display->gamma;
697 unsigned long *gamma_curves = NULL;
700 if (display->gamma_num * display->gamma_len > FBTFT_GAMMA_MAX_VALUES_TOTAL) {
701 dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
702 FBTFT_GAMMA_MAX_VALUES_TOTAL);
713 dev_err(dev, "platform data is missing\n");
717 /* override driver values? */
721 txbuflen = pdata->txbuflen;
722 if (pdata->display.init_sequence)
723 init_sequence = pdata->display.init_sequence;
725 gamma = pdata->gamma;
726 if (pdata->display.debug)
727 display->debug = pdata->display.debug;
728 if (pdata->display.backlight)
729 display->backlight = pdata->display.backlight;
730 if (pdata->display.width)
731 display->width = pdata->display.width;
732 if (pdata->display.height)
733 display->height = pdata->display.height;
734 if (pdata->display.buswidth)
735 display->buswidth = pdata->display.buswidth;
736 if (pdata->display.regwidth)
737 display->regwidth = pdata->display.regwidth;
739 display->debug |= debug;
740 fbtft_expand_debug_value(&display->debug);
742 switch (pdata->rotate) {
745 width = display->height;
746 height = display->width;
749 width = display->width;
750 height = display->height;
753 vmem_size = display->width * display->height * bpp / 8;
754 vmem = vzalloc(vmem_size);
758 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
762 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
766 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
770 if (display->gamma_num && display->gamma_len) {
771 gamma_curves = devm_kzalloc(dev, display->gamma_num * display->gamma_len * sizeof(gamma_curves[0]),
777 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
781 info->screen_base = (u8 __force __iomem *)vmem;
783 info->fbdefio = fbdefio;
785 fbops->owner = dev->driver->owner;
786 fbops->fb_read = fb_sys_read;
787 fbops->fb_write = fbtft_fb_write;
788 fbops->fb_fillrect = fbtft_fb_fillrect;
789 fbops->fb_copyarea = fbtft_fb_copyarea;
790 fbops->fb_imageblit = fbtft_fb_imageblit;
791 fbops->fb_setcolreg = fbtft_fb_setcolreg;
792 fbops->fb_blank = fbtft_fb_blank;
794 fbdefio->delay = HZ/fps;
795 fbdefio->deferred_io = fbtft_deferred_io;
796 fb_deferred_io_init(info);
798 strncpy(info->fix.id, dev->driver->name, 16);
799 info->fix.type = FB_TYPE_PACKED_PIXELS;
800 info->fix.visual = FB_VISUAL_TRUECOLOR;
801 info->fix.xpanstep = 0;
802 info->fix.ypanstep = 0;
803 info->fix.ywrapstep = 0;
804 info->fix.line_length = width*bpp/8;
805 info->fix.accel = FB_ACCEL_NONE;
806 info->fix.smem_len = vmem_size;
808 info->var.rotate = pdata->rotate;
809 info->var.xres = width;
810 info->var.yres = height;
811 info->var.xres_virtual = info->var.xres;
812 info->var.yres_virtual = info->var.yres;
813 info->var.bits_per_pixel = bpp;
814 info->var.nonstd = 1;
817 info->var.red.offset = 11;
818 info->var.red.length = 5;
819 info->var.green.offset = 5;
820 info->var.green.length = 6;
821 info->var.blue.offset = 0;
822 info->var.blue.length = 5;
823 info->var.transp.offset = 0;
824 info->var.transp.length = 0;
826 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
830 par->pdata = dev->platform_data;
831 par->debug = display->debug;
833 spin_lock_init(&par->dirty_lock);
834 par->bgr = pdata->bgr;
835 par->startbyte = pdata->startbyte;
836 par->init_sequence = init_sequence;
837 par->gamma.curves = gamma_curves;
838 par->gamma.num_curves = display->gamma_num;
839 par->gamma.num_values = display->gamma_len;
840 mutex_init(&par->gamma.lock);
841 info->pseudo_palette = par->pseudo_palette;
843 if (par->gamma.curves && gamma) {
844 if (fbtft_gamma_parse_str(par,
845 par->gamma.curves, gamma, strlen(gamma)))
849 /* Transmit buffer */
851 txbuflen = vmem_size + 2; /* add in case startbyte is used */
853 #ifdef __LITTLE_ENDIAN
854 if ((!txbuflen) && (bpp > 8))
855 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
860 dev->coherent_dma_mask = ~0;
861 txbuf = dmam_alloc_coherent(dev, txbuflen, &par->txbuf.dma, GFP_DMA);
863 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
867 par->txbuf.buf = txbuf;
868 par->txbuf.len = txbuflen;
871 /* Initialize gpios to disabled */
872 par->gpio.reset = -1;
877 par->gpio.latch = -1;
878 for (i = 0; i < 16; i++) {
879 par->gpio.db[i] = -1;
880 par->gpio.led[i] = -1;
881 par->gpio.aux[i] = -1;
884 /* default fbtft operations */
885 par->fbtftops.write = fbtft_write_spi;
886 par->fbtftops.read = fbtft_read_spi;
887 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
888 par->fbtftops.write_register = fbtft_write_reg8_bus8;
889 par->fbtftops.set_addr_win = fbtft_set_addr_win;
890 par->fbtftops.reset = fbtft_reset;
891 par->fbtftops.mkdirty = fbtft_mkdirty;
892 par->fbtftops.update_display = fbtft_update_display;
893 par->fbtftops.request_gpios = fbtft_request_gpios;
894 if (display->backlight)
895 par->fbtftops.register_backlight = fbtft_register_backlight;
897 /* use driver provided functions */
898 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
907 EXPORT_SYMBOL(fbtft_framebuffer_alloc);
910 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
912 * @info: frame buffer info structure
915 void fbtft_framebuffer_release(struct fb_info *info)
917 fb_deferred_io_cleanup(info);
918 vfree(info->screen_base);
919 framebuffer_release(info);
921 EXPORT_SYMBOL(fbtft_framebuffer_release);
924 * fbtft_register_framebuffer - registers a tft frame buffer device
925 * @fb_info: frame buffer info structure
927 * Sets SPI driverdata if needed
928 * Requests needed gpios.
929 * Initializes display
931 * Registers a frame buffer device @fb_info.
933 * Returns negative errno on error, or zero for success.
936 int fbtft_register_framebuffer(struct fb_info *fb_info)
941 struct fbtft_par *par = fb_info->par;
942 struct spi_device *spi = par->spi;
945 if (!par->fbtftops.init_display) {
946 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
951 spi_set_drvdata(spi, fb_info);
953 platform_set_drvdata(par->pdev, fb_info);
955 ret = par->fbtftops.request_gpios(par);
959 if (par->fbtftops.verify_gpios) {
960 ret = par->fbtftops.verify_gpios(par);
965 ret = par->fbtftops.init_display(par);
968 if (par->fbtftops.set_var) {
969 ret = par->fbtftops.set_var(par);
974 /* update the entire display */
975 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
977 if (par->fbtftops.set_gamma && par->gamma.curves) {
978 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
983 if (par->fbtftops.register_backlight)
984 par->fbtftops.register_backlight(par);
986 ret = register_framebuffer(fb_info);
990 fbtft_sysfs_init(par);
993 sprintf(text1, ", %zu KiB %sbuffer memory",
994 par->txbuf.len >> 10, par->txbuf.dma ? "DMA " : "");
996 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
997 spi->chip_select, spi->max_speed_hz/1000000);
998 dev_info(fb_info->dev,
999 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
1000 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
1001 fb_info->fix.smem_len >> 10, text1,
1002 HZ/fb_info->fbdefio->delay, text2);
1004 #ifdef CONFIG_FB_BACKLIGHT
1005 /* Turn on backlight if available */
1006 if (fb_info->bl_dev) {
1007 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
1008 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
1015 if (par->fbtftops.unregister_backlight)
1016 par->fbtftops.unregister_backlight(par);
1018 spi_set_drvdata(spi, NULL);
1020 platform_set_drvdata(par->pdev, NULL);
1024 EXPORT_SYMBOL(fbtft_register_framebuffer);
1027 * fbtft_unregister_framebuffer - releases a tft frame buffer device
1028 * @fb_info: frame buffer info structure
1030 * Frees SPI driverdata if needed
1032 * Unregisters frame buffer device.
1035 int fbtft_unregister_framebuffer(struct fb_info *fb_info)
1037 struct fbtft_par *par = fb_info->par;
1038 struct spi_device *spi = par->spi;
1041 spi_set_drvdata(spi, NULL);
1043 platform_set_drvdata(par->pdev, NULL);
1044 if (par->fbtftops.unregister_backlight)
1045 par->fbtftops.unregister_backlight(par);
1046 fbtft_sysfs_exit(par);
1047 return unregister_framebuffer(fb_info);
1049 EXPORT_SYMBOL(fbtft_unregister_framebuffer);
1053 * fbtft_init_display_dt() - Device Tree init_display() function
1056 * Return: 0 if successful, negative if error
1058 static int fbtft_init_display_dt(struct fbtft_par *par)
1060 struct device_node *node = par->info->device->of_node;
1061 struct property *prop;
1068 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
1073 prop = of_find_property(node, "init", NULL);
1074 p = of_prop_next_u32(prop, NULL, &val);
1078 if (val & FBTFT_OF_INIT_CMD) {
1081 while (p && !(val & 0xFFFF0000)) {
1083 dev_err(par->info->device,
1084 "%s: Maximum register values exceeded\n",
1089 p = of_prop_next_u32(prop, p, &val);
1091 /* make debug message */
1093 for (j = 0; j < i; j++) {
1094 snprintf(str, 128, " %02X", buf[j]);
1097 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1098 "init: write_register:%s\n", msg);
1100 par->fbtftops.write_register(par, i,
1101 buf[0], buf[1], buf[2], buf[3],
1102 buf[4], buf[5], buf[6], buf[7],
1103 buf[8], buf[9], buf[10], buf[11],
1104 buf[12], buf[13], buf[14], buf[15],
1105 buf[16], buf[17], buf[18], buf[19],
1106 buf[20], buf[21], buf[22], buf[23],
1107 buf[24], buf[25], buf[26], buf[27],
1108 buf[28], buf[29], buf[30], buf[31],
1109 buf[32], buf[33], buf[34], buf[35],
1110 buf[36], buf[37], buf[38], buf[39],
1111 buf[40], buf[41], buf[42], buf[43],
1112 buf[44], buf[45], buf[46], buf[47],
1113 buf[48], buf[49], buf[50], buf[51],
1114 buf[52], buf[53], buf[54], buf[55],
1115 buf[56], buf[57], buf[58], buf[59],
1116 buf[60], buf[61], buf[62], buf[63]);
1117 } else if (val & FBTFT_OF_INIT_DELAY) {
1118 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1119 "init: msleep(%u)\n", val & 0xFFFF);
1120 msleep(val & 0xFFFF);
1121 p = of_prop_next_u32(prop, p, &val);
1123 dev_err(par->info->device, "illegal init value 0x%X\n",
1134 * fbtft_init_display() - Generic init_display() function
1137 * Uses par->init_sequence to do the initialization
1139 * Return: 0 if successful, negative if error
1141 int fbtft_init_display(struct fbtft_par *par)
1149 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
1152 if (!par->init_sequence) {
1153 dev_err(par->info->device,
1154 "error: init_sequence is not set\n");
1158 /* make sure stop marker exists */
1159 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++)
1160 if (par->init_sequence[i] == -3)
1162 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1163 dev_err(par->info->device,
1164 "missing stop marker at end of init sequence\n");
1168 par->fbtftops.reset(par);
1169 if (par->gpio.cs != -1)
1170 gpio_set_value(par->gpio.cs, 0); /* Activate chip */
1173 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1174 if (par->init_sequence[i] == -3) {
1178 if (par->init_sequence[i] >= 0) {
1179 dev_err(par->info->device,
1180 "missing delimiter at position %d\n", i);
1183 if (par->init_sequence[i+1] < 0) {
1184 dev_err(par->info->device,
1185 "missing value after delimiter %d at position %d\n",
1186 par->init_sequence[i], i);
1189 switch (par->init_sequence[i]) {
1192 /* make debug message */
1195 while (par->init_sequence[j] >= 0) {
1196 sprintf(str, "0x%02X ", par->init_sequence[j]);
1200 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1201 "init: write(0x%02X) %s\n",
1202 par->init_sequence[i], msg);
1206 while (par->init_sequence[i] >= 0) {
1208 dev_err(par->info->device,
1209 "%s: Maximum register values exceeded\n",
1213 buf[j++] = par->init_sequence[i++];
1215 par->fbtftops.write_register(par, j,
1216 buf[0], buf[1], buf[2], buf[3],
1217 buf[4], buf[5], buf[6], buf[7],
1218 buf[8], buf[9], buf[10], buf[11],
1219 buf[12], buf[13], buf[14], buf[15],
1220 buf[16], buf[17], buf[18], buf[19],
1221 buf[20], buf[21], buf[22], buf[23],
1222 buf[24], buf[25], buf[26], buf[27],
1223 buf[28], buf[29], buf[30], buf[31],
1224 buf[32], buf[33], buf[34], buf[35],
1225 buf[36], buf[37], buf[38], buf[39],
1226 buf[40], buf[41], buf[42], buf[43],
1227 buf[44], buf[45], buf[46], buf[47],
1228 buf[48], buf[49], buf[50], buf[51],
1229 buf[52], buf[53], buf[54], buf[55],
1230 buf[56], buf[57], buf[58], buf[59],
1231 buf[60], buf[61], buf[62], buf[63]);
1235 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1236 "init: mdelay(%d)\n", par->init_sequence[i]);
1237 mdelay(par->init_sequence[i++]);
1240 dev_err(par->info->device,
1241 "unknown delimiter %d at position %d\n",
1242 par->init_sequence[i], i);
1247 dev_err(par->info->device,
1248 "%s: something is wrong. Shouldn't get here.\n", __func__);
1251 EXPORT_SYMBOL(fbtft_init_display);
1254 * fbtft_verify_gpios() - Generic verify_gpios() function
1257 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1259 * Return: 0 if successful, negative if error
1261 static int fbtft_verify_gpios(struct fbtft_par *par)
1263 struct fbtft_platform_data *pdata;
1266 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1268 pdata = par->info->device->platform_data;
1269 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
1271 dev_err(par->info->device,
1272 "Missing info about 'dc' gpio. Aborting.\n");
1279 if (par->gpio.wr < 0) {
1280 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1283 for (i = 0; i < pdata->display.buswidth; i++) {
1284 if (par->gpio.db[i] < 0) {
1285 dev_err(par->info->device,
1286 "Missing 'db%02d' gpio. Aborting.\n", i);
1295 /* returns 0 if the property is not present */
1296 static u32 fbtft_of_value(struct device_node *node, const char *propname)
1301 ret = of_property_read_u32(node, propname, &val);
1303 pr_info("%s: %s = %u\n", __func__, propname, val);
1308 static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1310 struct device_node *node = dev->of_node;
1311 struct fbtft_platform_data *pdata;
1314 dev_err(dev, "Missing platform data or DT\n");
1315 return ERR_PTR(-EINVAL);
1318 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1320 return ERR_PTR(-ENOMEM);
1322 pdata->display.width = fbtft_of_value(node, "width");
1323 pdata->display.height = fbtft_of_value(node, "height");
1324 pdata->display.regwidth = fbtft_of_value(node, "regwidth");
1325 pdata->display.buswidth = fbtft_of_value(node, "buswidth");
1326 pdata->display.backlight = fbtft_of_value(node, "backlight");
1327 pdata->display.bpp = fbtft_of_value(node, "bpp");
1328 pdata->display.debug = fbtft_of_value(node, "debug");
1329 pdata->rotate = fbtft_of_value(node, "rotate");
1330 pdata->bgr = of_property_read_bool(node, "bgr");
1331 pdata->fps = fbtft_of_value(node, "fps");
1332 pdata->txbuflen = fbtft_of_value(node, "txbuflen");
1333 pdata->startbyte = fbtft_of_value(node, "startbyte");
1334 of_property_read_string(node, "gamma", (const char **)&pdata->gamma);
1336 if (of_find_property(node, "led-gpios", NULL))
1337 pdata->display.backlight = 1;
1338 if (of_find_property(node, "init", NULL))
1339 pdata->display.fbtftops.init_display = fbtft_init_display_dt;
1340 pdata->display.fbtftops.request_gpios = fbtft_request_gpios_dt;
1345 static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev)
1347 dev_err(dev, "Missing platform data\n");
1348 return ERR_PTR(-EINVAL);
1353 * fbtft_probe_common() - Generic device probe() helper function
1354 * @display: Display properties
1356 * @pdev: Platform device
1358 * Allocates, initializes and registers a framebuffer
1360 * Either @sdev or @pdev should be NULL
1362 * Return: 0 if successful, negative if error
1364 int fbtft_probe_common(struct fbtft_display *display,
1365 struct spi_device *sdev, struct platform_device *pdev)
1368 struct fb_info *info;
1369 struct fbtft_par *par;
1370 struct fbtft_platform_data *pdata;
1378 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1379 dev_info(dev, "%s()\n", __func__);
1381 pdata = dev->platform_data;
1383 pdata = fbtft_probe_dt(dev);
1385 return PTR_ERR(pdata);
1386 dev->platform_data = pdata;
1389 info = fbtft_framebuffer_alloc(display, dev);
1397 if (display->buswidth == 0) {
1398 dev_err(dev, "buswidth is not set\n");
1402 /* write register functions */
1403 if (display->regwidth == 8 && display->buswidth == 8) {
1404 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1406 if (display->regwidth == 8 && display->buswidth == 9 && par->spi) {
1407 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1408 } else if (display->regwidth == 16 && display->buswidth == 8) {
1409 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1410 } else if (display->regwidth == 16 && display->buswidth == 16) {
1411 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1414 "no default functions for regwidth=%d and buswidth=%d\n",
1415 display->regwidth, display->buswidth);
1418 /* write_vmem() functions */
1419 if (display->buswidth == 8)
1420 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1421 else if (display->buswidth == 9)
1422 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1423 else if (display->buswidth == 16)
1424 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1426 /* GPIO write() functions */
1428 if (display->buswidth == 8)
1429 par->fbtftops.write = fbtft_write_gpio8_wr;
1430 else if (display->buswidth == 16)
1431 par->fbtftops.write = fbtft_write_gpio16_wr;
1434 /* 9-bit SPI setup */
1435 if (par->spi && display->buswidth == 9) {
1436 par->spi->bits_per_word = 9;
1437 ret = par->spi->master->setup(par->spi);
1439 dev_warn(&par->spi->dev,
1440 "9-bit SPI not available, emulating using 8-bit.\n");
1441 par->spi->bits_per_word = 8;
1442 ret = par->spi->master->setup(par->spi);
1445 /* allocate buffer with room for dc bits */
1446 par->extra = devm_kzalloc(par->info->device,
1447 par->txbuf.len + (par->txbuf.len / 8) + 8,
1453 par->fbtftops.write = fbtft_write_spi_emulate_9;
1457 if (!par->fbtftops.verify_gpios)
1458 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1460 /* make sure we still use the driver provided functions */
1461 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1463 /* use init_sequence if provided */
1464 if (par->init_sequence)
1465 par->fbtftops.init_display = fbtft_init_display;
1467 /* use platform_data provided functions above all */
1468 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1470 ret = fbtft_register_framebuffer(info);
1477 fbtft_framebuffer_release(info);
1481 EXPORT_SYMBOL(fbtft_probe_common);
1484 * fbtft_remove_common() - Generic device remove() helper function
1486 * @info: Framebuffer
1488 * Unregisters and releases the framebuffer
1490 * Return: 0 if successful, negative if error
1492 int fbtft_remove_common(struct device *dev, struct fb_info *info)
1494 struct fbtft_par *par;
1500 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1501 "%s()\n", __func__);
1502 fbtft_unregister_framebuffer(info);
1503 fbtft_framebuffer_release(info);
1507 EXPORT_SYMBOL(fbtft_remove_common);
1509 MODULE_LICENSE("GPL");