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/consumer.h>
20 #include <linux/spi/spi.h>
21 #include <linux/delay.h>
22 #include <linux/uaccess.h>
23 #include <linux/backlight.h>
24 #include <linux/platform_device.h>
25 #include <linux/property.h>
26 #include <linux/spinlock.h>
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 gpiod_set_value(par->gpio.dc, dc);
43 ret = par->fbtftops.write(par, buf, len);
45 dev_err(par->info->device,
46 "write() failed and returned %d\n", ret);
49 EXPORT_SYMBOL(fbtft_write_buf_dc);
51 void fbtft_dbg_hex(const struct device *dev, int groupsize,
52 void *buf, size_t len, const char *fmt, ...)
55 static char textbuf[512];
60 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
63 hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len,
64 512 - text_len, false);
67 dev_info(dev, "%s ...\n", text);
69 dev_info(dev, "%s\n", text);
71 EXPORT_SYMBOL(fbtft_dbg_hex);
73 static int fbtft_request_one_gpio(struct fbtft_par *par,
74 const char *name, int index,
75 struct gpio_desc **gpiop)
77 struct device *dev = par->info->device;
79 *gpiop = devm_gpiod_get_index_optional(dev, name, index,
82 return dev_err_probe(dev, PTR_ERR(*gpiop), "Failed to request %s GPIO\n", name);
84 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par, "%s: '%s' GPIO\n",
90 static int fbtft_request_gpios(struct fbtft_par *par)
95 ret = fbtft_request_one_gpio(par, "reset", 0, &par->gpio.reset);
98 ret = fbtft_request_one_gpio(par, "dc", 0, &par->gpio.dc);
101 ret = fbtft_request_one_gpio(par, "rd", 0, &par->gpio.rd);
104 ret = fbtft_request_one_gpio(par, "wr", 0, &par->gpio.wr);
107 ret = fbtft_request_one_gpio(par, "cs", 0, &par->gpio.cs);
110 ret = fbtft_request_one_gpio(par, "latch", 0, &par->gpio.latch);
113 for (i = 0; i < 16; i++) {
114 ret = fbtft_request_one_gpio(par, "db", i,
118 ret = fbtft_request_one_gpio(par, "led", i,
122 ret = fbtft_request_one_gpio(par, "aux", i,
131 static int fbtft_backlight_update_status(struct backlight_device *bd)
133 struct fbtft_par *par = bl_get_data(bd);
134 bool polarity = par->polarity;
136 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
137 "%s: polarity=%d, power=%d, fb_blank=%d\n",
138 __func__, polarity, bd->props.power, bd->props.fb_blank);
140 if ((bd->props.power == FB_BLANK_UNBLANK) &&
141 (bd->props.fb_blank == FB_BLANK_UNBLANK))
142 gpiod_set_value(par->gpio.led[0], polarity);
144 gpiod_set_value(par->gpio.led[0], !polarity);
149 static int fbtft_backlight_get_brightness(struct backlight_device *bd)
151 return bd->props.brightness;
154 void fbtft_unregister_backlight(struct fbtft_par *par)
156 if (par->info->bl_dev) {
157 par->info->bl_dev->props.power = FB_BLANK_POWERDOWN;
158 backlight_update_status(par->info->bl_dev);
159 backlight_device_unregister(par->info->bl_dev);
160 par->info->bl_dev = NULL;
163 EXPORT_SYMBOL(fbtft_unregister_backlight);
165 static const struct backlight_ops fbtft_bl_ops = {
166 .get_brightness = fbtft_backlight_get_brightness,
167 .update_status = fbtft_backlight_update_status,
170 void fbtft_register_backlight(struct fbtft_par *par)
172 struct backlight_device *bd;
173 struct backlight_properties bl_props = { 0, };
175 if (!par->gpio.led[0]) {
176 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
177 "%s(): led pin not set, exiting.\n", __func__);
181 bl_props.type = BACKLIGHT_RAW;
182 /* Assume backlight is off, get polarity from current state of pin */
183 bl_props.power = FB_BLANK_POWERDOWN;
184 if (!gpiod_get_value(par->gpio.led[0]))
185 par->polarity = true;
187 bd = backlight_device_register(dev_driver_string(par->info->device),
188 par->info->device, par,
189 &fbtft_bl_ops, &bl_props);
191 dev_err(par->info->device,
192 "cannot register backlight device (%ld)\n",
196 par->info->bl_dev = bd;
198 if (!par->fbtftops.unregister_backlight)
199 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
201 EXPORT_SYMBOL(fbtft_register_backlight);
203 static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
206 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
207 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
209 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
210 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
212 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
215 static void fbtft_reset(struct fbtft_par *par)
217 if (!par->gpio.reset)
220 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
222 gpiod_set_value_cansleep(par->gpio.reset, 1);
223 usleep_range(20, 40);
224 gpiod_set_value_cansleep(par->gpio.reset, 0);
227 gpiod_set_value_cansleep(par->gpio.cs, 1); /* Activate chip */
230 static void fbtft_update_display(struct fbtft_par *par, unsigned int start_line,
231 unsigned int end_line)
234 ktime_t ts_start, ts_end;
235 long fps, throughput;
239 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE |
240 DEBUG_TIME_EACH_UPDATE))) {
241 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
242 ((par->debug & DEBUG_TIME_FIRST_UPDATE) &&
243 !par->first_update_done)) {
244 ts_start = ktime_get();
250 if (start_line > end_line) {
251 dev_warn(par->info->device,
252 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
253 __func__, start_line, end_line);
255 end_line = par->info->var.yres - 1;
257 if (start_line > par->info->var.yres - 1 ||
258 end_line > par->info->var.yres - 1) {
259 dev_warn(par->info->device,
260 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
261 __func__, start_line,
262 end_line, par->info->var.yres - 1);
264 end_line = par->info->var.yres - 1;
267 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
268 __func__, start_line, end_line);
270 if (par->fbtftops.set_addr_win)
271 par->fbtftops.set_addr_win(par, 0, start_line,
272 par->info->var.xres - 1, end_line);
274 offset = start_line * par->info->fix.line_length;
275 len = (end_line - start_line + 1) * par->info->fix.line_length;
276 ret = par->fbtftops.write_vmem(par, offset, len);
278 dev_err(par->info->device,
279 "%s: write_vmem failed to update display buffer\n",
282 if (unlikely(timeit)) {
283 ts_end = ktime_get();
284 if (!ktime_to_ns(par->update_time))
285 par->update_time = ts_start;
287 fps = ktime_us_delta(ts_start, par->update_time);
288 par->update_time = ts_start;
289 fps = fps ? 1000000 / fps : 0;
291 throughput = ktime_us_delta(ts_end, ts_start);
292 throughput = throughput ? (len * 1000) / throughput : 0;
293 throughput = throughput * 1000 / 1024;
295 dev_info(par->info->device,
296 "Display update: %ld kB/s, fps=%ld\n",
298 par->first_update_done = true;
302 static void fbtft_mkdirty(struct fb_info *info, int y, int height)
304 struct fbtft_par *par = info->par;
305 struct fb_deferred_io *fbdefio = info->fbdefio;
307 /* special case, needed ? */
310 height = info->var.yres;
313 /* Mark display lines/area as dirty */
314 spin_lock(&par->dirty_lock);
315 if (y < par->dirty_lines_start)
316 par->dirty_lines_start = y;
317 if (y + height - 1 > par->dirty_lines_end)
318 par->dirty_lines_end = y + height - 1;
319 spin_unlock(&par->dirty_lock);
321 /* Schedule deferred_io to update display (no-op if already on queue)*/
322 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
325 static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
327 struct fbtft_par *par = info->par;
328 unsigned int dirty_lines_start, dirty_lines_end;
331 unsigned int y_low = 0, y_high = 0;
334 spin_lock(&par->dirty_lock);
335 dirty_lines_start = par->dirty_lines_start;
336 dirty_lines_end = par->dirty_lines_end;
337 /* set display line markers as clean */
338 par->dirty_lines_start = par->info->var.yres - 1;
339 par->dirty_lines_end = 0;
340 spin_unlock(&par->dirty_lock);
342 /* Mark display lines as dirty */
343 list_for_each_entry(page, pagelist, lru) {
345 index = page->index << PAGE_SHIFT;
346 y_low = index / info->fix.line_length;
347 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
348 dev_dbg(info->device,
349 "page->index=%lu y_low=%d y_high=%d\n",
350 page->index, y_low, y_high);
351 if (y_high > info->var.yres - 1)
352 y_high = info->var.yres - 1;
353 if (y_low < dirty_lines_start)
354 dirty_lines_start = y_low;
355 if (y_high > dirty_lines_end)
356 dirty_lines_end = y_high;
359 par->fbtftops.update_display(info->par,
360 dirty_lines_start, dirty_lines_end);
363 static void fbtft_fb_fillrect(struct fb_info *info,
364 const struct fb_fillrect *rect)
366 struct fbtft_par *par = info->par;
369 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
370 __func__, rect->dx, rect->dy, rect->width, rect->height);
371 sys_fillrect(info, rect);
373 par->fbtftops.mkdirty(info, rect->dy, rect->height);
376 static void fbtft_fb_copyarea(struct fb_info *info,
377 const struct fb_copyarea *area)
379 struct fbtft_par *par = info->par;
382 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
383 __func__, area->dx, area->dy, area->width, area->height);
384 sys_copyarea(info, area);
386 par->fbtftops.mkdirty(info, area->dy, area->height);
389 static void fbtft_fb_imageblit(struct fb_info *info,
390 const struct fb_image *image)
392 struct fbtft_par *par = info->par;
395 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
396 __func__, image->dx, image->dy, image->width, image->height);
397 sys_imageblit(info, image);
399 par->fbtftops.mkdirty(info, image->dy, image->height);
402 static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
403 size_t count, loff_t *ppos)
405 struct fbtft_par *par = info->par;
409 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
410 res = fb_sys_write(info, buf, count, ppos);
412 /* TODO: only mark changed area update all for now */
413 par->fbtftops.mkdirty(info, -1, 0);
419 static unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
422 chan >>= 16 - bf->length;
423 return chan << bf->offset;
426 static int fbtft_fb_setcolreg(unsigned int regno, unsigned int red,
427 unsigned int green, unsigned int blue,
428 unsigned int transp, struct fb_info *info)
434 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
435 __func__, regno, red, green, blue, transp);
437 switch (info->fix.visual) {
438 case FB_VISUAL_TRUECOLOR:
440 u32 *pal = info->pseudo_palette;
442 val = chan_to_field(red, &info->var.red);
443 val |= chan_to_field(green, &info->var.green);
444 val |= chan_to_field(blue, &info->var.blue);
454 static int fbtft_fb_blank(int blank, struct fb_info *info)
456 struct fbtft_par *par = info->par;
459 dev_dbg(info->dev, "%s(blank=%d)\n",
462 if (!par->fbtftops.blank)
466 case FB_BLANK_POWERDOWN:
467 case FB_BLANK_VSYNC_SUSPEND:
468 case FB_BLANK_HSYNC_SUSPEND:
469 case FB_BLANK_NORMAL:
470 ret = par->fbtftops.blank(par, true);
472 case FB_BLANK_UNBLANK:
473 ret = par->fbtftops.blank(par, false);
479 static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
482 dst->write = src->write;
484 dst->read = src->read;
486 dst->write_vmem = src->write_vmem;
487 if (src->write_register)
488 dst->write_register = src->write_register;
489 if (src->set_addr_win)
490 dst->set_addr_win = src->set_addr_win;
492 dst->reset = src->reset;
494 dst->mkdirty = src->mkdirty;
495 if (src->update_display)
496 dst->update_display = src->update_display;
497 if (src->init_display)
498 dst->init_display = src->init_display;
500 dst->blank = src->blank;
501 if (src->request_gpios_match)
502 dst->request_gpios_match = src->request_gpios_match;
503 if (src->request_gpios)
504 dst->request_gpios = src->request_gpios;
505 if (src->verify_gpios)
506 dst->verify_gpios = src->verify_gpios;
507 if (src->register_backlight)
508 dst->register_backlight = src->register_backlight;
509 if (src->unregister_backlight)
510 dst->unregister_backlight = src->unregister_backlight;
512 dst->set_var = src->set_var;
514 dst->set_gamma = src->set_gamma;
518 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
520 * @display: pointer to structure describing the display
521 * @dev: pointer to the device for this fb, this can be NULL
522 * @pdata: platform data for the display in use
524 * Creates a new frame buffer info structure.
526 * Also creates and populates the following structures:
529 * info->pseudo_palette
533 * Returns the new structure, or NULL if an error occurred.
536 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
538 struct fbtft_platform_data *pdata)
540 struct fb_info *info;
541 struct fbtft_par *par;
542 struct fb_ops *fbops = NULL;
543 struct fb_deferred_io *fbdefio = NULL;
549 int txbuflen = display->txbuflen;
550 unsigned int bpp = display->bpp;
551 unsigned int fps = display->fps;
553 const s16 *init_sequence = display->init_sequence;
554 char *gamma = display->gamma;
555 u32 *gamma_curves = NULL;
558 if (display->gamma_num * display->gamma_len >
559 FBTFT_GAMMA_MAX_VALUES_TOTAL) {
560 dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
561 FBTFT_GAMMA_MAX_VALUES_TOTAL);
572 dev_err(dev, "platform data is missing\n");
576 /* override driver values? */
580 txbuflen = pdata->txbuflen;
581 if (pdata->display.init_sequence)
582 init_sequence = pdata->display.init_sequence;
584 gamma = pdata->gamma;
585 if (pdata->display.debug)
586 display->debug = pdata->display.debug;
587 if (pdata->display.backlight)
588 display->backlight = pdata->display.backlight;
589 if (pdata->display.width)
590 display->width = pdata->display.width;
591 if (pdata->display.height)
592 display->height = pdata->display.height;
593 if (pdata->display.buswidth)
594 display->buswidth = pdata->display.buswidth;
595 if (pdata->display.regwidth)
596 display->regwidth = pdata->display.regwidth;
598 display->debug |= debug;
599 fbtft_expand_debug_value(&display->debug);
601 switch (pdata->rotate) {
604 width = display->height;
605 height = display->width;
608 width = display->width;
609 height = display->height;
612 vmem_size = display->width * display->height * bpp / 8;
613 vmem = vzalloc(vmem_size);
617 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
621 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
625 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
629 if (display->gamma_num && display->gamma_len) {
630 gamma_curves = devm_kcalloc(dev,
633 sizeof(gamma_curves[0]),
639 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
643 info->screen_buffer = vmem;
645 info->fbdefio = fbdefio;
647 fbops->owner = dev->driver->owner;
648 fbops->fb_read = fb_sys_read;
649 fbops->fb_write = fbtft_fb_write;
650 fbops->fb_fillrect = fbtft_fb_fillrect;
651 fbops->fb_copyarea = fbtft_fb_copyarea;
652 fbops->fb_imageblit = fbtft_fb_imageblit;
653 fbops->fb_setcolreg = fbtft_fb_setcolreg;
654 fbops->fb_blank = fbtft_fb_blank;
656 fbdefio->delay = HZ / fps;
657 fbdefio->deferred_io = fbtft_deferred_io;
658 fb_deferred_io_init(info);
660 snprintf(info->fix.id, sizeof(info->fix.id), "%s", dev->driver->name);
661 info->fix.type = FB_TYPE_PACKED_PIXELS;
662 info->fix.visual = FB_VISUAL_TRUECOLOR;
663 info->fix.xpanstep = 0;
664 info->fix.ypanstep = 0;
665 info->fix.ywrapstep = 0;
666 info->fix.line_length = width * bpp / 8;
667 info->fix.accel = FB_ACCEL_NONE;
668 info->fix.smem_len = vmem_size;
670 info->var.rotate = pdata->rotate;
671 info->var.xres = width;
672 info->var.yres = height;
673 info->var.xres_virtual = info->var.xres;
674 info->var.yres_virtual = info->var.yres;
675 info->var.bits_per_pixel = bpp;
676 info->var.nonstd = 1;
679 info->var.red.offset = 11;
680 info->var.red.length = 5;
681 info->var.green.offset = 5;
682 info->var.green.length = 6;
683 info->var.blue.offset = 0;
684 info->var.blue.length = 5;
685 info->var.transp.offset = 0;
686 info->var.transp.length = 0;
688 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
693 par->debug = display->debug;
695 spin_lock_init(&par->dirty_lock);
696 par->bgr = pdata->bgr;
697 par->startbyte = pdata->startbyte;
698 par->init_sequence = init_sequence;
699 par->gamma.curves = gamma_curves;
700 par->gamma.num_curves = display->gamma_num;
701 par->gamma.num_values = display->gamma_len;
702 mutex_init(&par->gamma.lock);
703 info->pseudo_palette = par->pseudo_palette;
705 if (par->gamma.curves && gamma) {
706 if (fbtft_gamma_parse_str(par, par->gamma.curves, gamma,
708 goto release_framebuf;
711 /* Transmit buffer */
713 txbuflen = vmem_size + 2; /* add in case startbyte is used */
714 if (txbuflen >= vmem_size + 2)
717 #ifdef __LITTLE_ENDIAN
718 if ((!txbuflen) && (bpp > 8))
719 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
723 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
725 goto release_framebuf;
726 par->txbuf.buf = txbuf;
727 par->txbuf.len = txbuflen;
730 /* default fbtft operations */
731 par->fbtftops.write = fbtft_write_spi;
732 par->fbtftops.read = fbtft_read_spi;
733 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
734 par->fbtftops.write_register = fbtft_write_reg8_bus8;
735 par->fbtftops.set_addr_win = fbtft_set_addr_win;
736 par->fbtftops.reset = fbtft_reset;
737 par->fbtftops.mkdirty = fbtft_mkdirty;
738 par->fbtftops.update_display = fbtft_update_display;
739 if (display->backlight)
740 par->fbtftops.register_backlight = fbtft_register_backlight;
742 /* use driver provided functions */
743 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
748 framebuffer_release(info);
755 EXPORT_SYMBOL(fbtft_framebuffer_alloc);
758 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
760 * @info: frame buffer info structure
763 void fbtft_framebuffer_release(struct fb_info *info)
765 fb_deferred_io_cleanup(info);
766 vfree(info->screen_buffer);
767 framebuffer_release(info);
769 EXPORT_SYMBOL(fbtft_framebuffer_release);
772 * fbtft_register_framebuffer - registers a tft frame buffer device
773 * @fb_info: frame buffer info structure
775 * Sets SPI driverdata if needed
776 * Requests needed gpios.
777 * Initializes display
779 * Registers a frame buffer device @fb_info.
781 * Returns negative errno on error, or zero for success.
784 int fbtft_register_framebuffer(struct fb_info *fb_info)
789 struct fbtft_par *par = fb_info->par;
790 struct spi_device *spi = par->spi;
793 if (!par->fbtftops.init_display) {
794 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
799 spi_set_drvdata(spi, fb_info);
801 platform_set_drvdata(par->pdev, fb_info);
803 ret = par->fbtftops.request_gpios(par);
807 if (par->fbtftops.verify_gpios) {
808 ret = par->fbtftops.verify_gpios(par);
813 ret = par->fbtftops.init_display(par);
816 if (par->fbtftops.set_var) {
817 ret = par->fbtftops.set_var(par);
822 /* update the entire display */
823 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
825 if (par->fbtftops.set_gamma && par->gamma.curves) {
826 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
831 if (par->fbtftops.register_backlight)
832 par->fbtftops.register_backlight(par);
834 ret = register_framebuffer(fb_info);
838 fbtft_sysfs_init(par);
840 if (par->txbuf.buf && par->txbuf.len >= 1024)
841 sprintf(text1, ", %zu KiB buffer memory", par->txbuf.len >> 10);
843 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
844 spi->chip_select, spi->max_speed_hz / 1000000);
845 dev_info(fb_info->dev,
846 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
847 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
848 fb_info->fix.smem_len >> 10, text1,
849 HZ / fb_info->fbdefio->delay, text2);
851 /* Turn on backlight if available */
852 if (fb_info->bl_dev) {
853 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
854 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
860 if (par->fbtftops.unregister_backlight)
861 par->fbtftops.unregister_backlight(par);
865 EXPORT_SYMBOL(fbtft_register_framebuffer);
868 * fbtft_unregister_framebuffer - releases a tft frame buffer device
869 * @fb_info: frame buffer info structure
871 * Frees SPI driverdata if needed
873 * Unregisters frame buffer device.
876 int fbtft_unregister_framebuffer(struct fb_info *fb_info)
878 struct fbtft_par *par = fb_info->par;
880 if (par->fbtftops.unregister_backlight)
881 par->fbtftops.unregister_backlight(par);
882 fbtft_sysfs_exit(par);
883 unregister_framebuffer(fb_info);
887 EXPORT_SYMBOL(fbtft_unregister_framebuffer);
890 * fbtft_init_display_from_property() - Device Tree init_display() function
893 * Return: 0 if successful, negative if error
895 static int fbtft_init_display_from_property(struct fbtft_par *par)
897 struct device *dev = par->info->device;
898 int buf[64], count, index, i, j, ret;
902 count = device_property_count_u32(dev, "init");
908 values = kmalloc_array(count + 1, sizeof(*values), GFP_KERNEL);
912 ret = device_property_read_u32_array(dev, "init", values, count);
916 par->fbtftops.reset(par);
919 val = values[++index];
921 while (index < count) {
922 if (val & FBTFT_OF_INIT_CMD) {
925 while ((index < count) && !(val & 0xFFFF0000)) {
928 "%s: Maximum register values exceeded\n",
934 val = values[++index];
936 /* make debug message */
937 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
938 "init: write_register:\n");
939 for (j = 0; j < i; j++)
940 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
941 "buf[%d] = %02X\n", j, buf[j]);
943 par->fbtftops.write_register(par, i,
944 buf[0], buf[1], buf[2], buf[3],
945 buf[4], buf[5], buf[6], buf[7],
946 buf[8], buf[9], buf[10], buf[11],
947 buf[12], buf[13], buf[14], buf[15],
948 buf[16], buf[17], buf[18], buf[19],
949 buf[20], buf[21], buf[22], buf[23],
950 buf[24], buf[25], buf[26], buf[27],
951 buf[28], buf[29], buf[30], buf[31],
952 buf[32], buf[33], buf[34], buf[35],
953 buf[36], buf[37], buf[38], buf[39],
954 buf[40], buf[41], buf[42], buf[43],
955 buf[44], buf[45], buf[46], buf[47],
956 buf[48], buf[49], buf[50], buf[51],
957 buf[52], buf[53], buf[54], buf[55],
958 buf[56], buf[57], buf[58], buf[59],
959 buf[60], buf[61], buf[62], buf[63]);
960 } else if (val & FBTFT_OF_INIT_DELAY) {
961 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
962 "init: msleep(%u)\n", val & 0xFFFF);
963 msleep(val & 0xFFFF);
964 val = values[++index];
966 dev_err(dev, "illegal init value 0x%X\n", val);
978 * fbtft_init_display() - Generic init_display() function
981 * Uses par->init_sequence to do the initialization
983 * Return: 0 if successful, negative if error
985 int fbtft_init_display(struct fbtft_par *par)
994 if (!par->init_sequence) {
995 dev_err(par->info->device,
996 "error: init_sequence is not set\n");
1000 /* make sure stop marker exists */
1001 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++)
1002 if (par->init_sequence[i] == -3)
1004 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1005 dev_err(par->info->device,
1006 "missing stop marker at end of init sequence\n");
1010 par->fbtftops.reset(par);
1013 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1014 if (par->init_sequence[i] == -3) {
1018 if (par->init_sequence[i] >= 0) {
1019 dev_err(par->info->device,
1020 "missing delimiter at position %d\n", i);
1023 if (par->init_sequence[i + 1] < 0) {
1024 dev_err(par->info->device,
1025 "missing value after delimiter %d at position %d\n",
1026 par->init_sequence[i], i);
1029 switch (par->init_sequence[i]) {
1032 /* make debug message */
1035 while (par->init_sequence[j] >= 0) {
1036 sprintf(str, "0x%02X ", par->init_sequence[j]);
1040 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1041 "init: write(0x%02X) %s\n",
1042 par->init_sequence[i], msg);
1046 while (par->init_sequence[i] >= 0) {
1048 dev_err(par->info->device,
1049 "%s: Maximum register values exceeded\n",
1053 buf[j++] = par->init_sequence[i++];
1055 par->fbtftops.write_register(par, j,
1056 buf[0], buf[1], buf[2], buf[3],
1057 buf[4], buf[5], buf[6], buf[7],
1058 buf[8], buf[9], buf[10], buf[11],
1059 buf[12], buf[13], buf[14], buf[15],
1060 buf[16], buf[17], buf[18], buf[19],
1061 buf[20], buf[21], buf[22], buf[23],
1062 buf[24], buf[25], buf[26], buf[27],
1063 buf[28], buf[29], buf[30], buf[31],
1064 buf[32], buf[33], buf[34], buf[35],
1065 buf[36], buf[37], buf[38], buf[39],
1066 buf[40], buf[41], buf[42], buf[43],
1067 buf[44], buf[45], buf[46], buf[47],
1068 buf[48], buf[49], buf[50], buf[51],
1069 buf[52], buf[53], buf[54], buf[55],
1070 buf[56], buf[57], buf[58], buf[59],
1071 buf[60], buf[61], buf[62], buf[63]);
1075 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1076 "init: mdelay(%d)\n",
1077 par->init_sequence[i]);
1078 mdelay(par->init_sequence[i++]);
1081 dev_err(par->info->device,
1082 "unknown delimiter %d at position %d\n",
1083 par->init_sequence[i], i);
1088 dev_err(par->info->device,
1089 "%s: something is wrong. Shouldn't get here.\n", __func__);
1092 EXPORT_SYMBOL(fbtft_init_display);
1095 * fbtft_verify_gpios() - Generic verify_gpios() function
1098 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1100 * Return: 0 if successful, negative if error
1102 static int fbtft_verify_gpios(struct fbtft_par *par)
1104 struct fbtft_platform_data *pdata = par->pdata;
1107 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1109 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
1111 dev_err(par->info->device,
1112 "Missing info about 'dc' gpio. Aborting.\n");
1119 if (!par->gpio.wr) {
1120 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1123 for (i = 0; i < pdata->display.buswidth; i++) {
1124 if (!par->gpio.db[i]) {
1125 dev_err(par->info->device,
1126 "Missing 'db%02d' gpio. Aborting.\n", i);
1134 /* returns 0 if the property is not present */
1135 static u32 fbtft_property_value(struct device *dev, const char *propname)
1140 ret = device_property_read_u32(dev, propname, &val);
1142 dev_info(dev, "%s: %s = %u\n", __func__, propname, val);
1147 static struct fbtft_platform_data *fbtft_properties_read(struct device *dev)
1149 struct fbtft_platform_data *pdata;
1151 if (!dev_fwnode(dev)) {
1152 dev_err(dev, "Missing platform data or properties\n");
1153 return ERR_PTR(-EINVAL);
1156 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1158 return ERR_PTR(-ENOMEM);
1160 pdata->display.width = fbtft_property_value(dev, "width");
1161 pdata->display.height = fbtft_property_value(dev, "height");
1162 pdata->display.regwidth = fbtft_property_value(dev, "regwidth");
1163 pdata->display.buswidth = fbtft_property_value(dev, "buswidth");
1164 pdata->display.backlight = fbtft_property_value(dev, "backlight");
1165 pdata->display.bpp = fbtft_property_value(dev, "bpp");
1166 pdata->display.debug = fbtft_property_value(dev, "debug");
1167 pdata->rotate = fbtft_property_value(dev, "rotate");
1168 pdata->bgr = device_property_read_bool(dev, "bgr");
1169 pdata->fps = fbtft_property_value(dev, "fps");
1170 pdata->txbuflen = fbtft_property_value(dev, "txbuflen");
1171 pdata->startbyte = fbtft_property_value(dev, "startbyte");
1172 device_property_read_string(dev, "gamma", (const char **)&pdata->gamma);
1174 if (device_property_present(dev, "led-gpios"))
1175 pdata->display.backlight = 1;
1176 if (device_property_present(dev, "init"))
1177 pdata->display.fbtftops.init_display =
1178 fbtft_init_display_from_property;
1180 pdata->display.fbtftops.request_gpios = fbtft_request_gpios;
1186 * fbtft_probe_common() - Generic device probe() helper function
1187 * @display: Display properties
1189 * @pdev: Platform device
1191 * Allocates, initializes and registers a framebuffer
1193 * Either @sdev or @pdev should be NULL
1195 * Return: 0 if successful, negative if error
1197 int fbtft_probe_common(struct fbtft_display *display,
1198 struct spi_device *sdev,
1199 struct platform_device *pdev)
1202 struct fb_info *info;
1203 struct fbtft_par *par;
1204 struct fbtft_platform_data *pdata;
1212 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1213 dev_info(dev, "%s()\n", __func__);
1215 pdata = dev->platform_data;
1217 pdata = fbtft_properties_read(dev);
1219 return PTR_ERR(pdata);
1222 info = fbtft_framebuffer_alloc(display, dev, pdata);
1230 if (display->buswidth == 0) {
1231 dev_err(dev, "buswidth is not set\n");
1235 /* write register functions */
1236 if (display->regwidth == 8 && display->buswidth == 8)
1237 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1238 else if (display->regwidth == 8 && display->buswidth == 9 && par->spi)
1239 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1240 else if (display->regwidth == 16 && display->buswidth == 8)
1241 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1242 else if (display->regwidth == 16 && display->buswidth == 16)
1243 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1246 "no default functions for regwidth=%d and buswidth=%d\n",
1247 display->regwidth, display->buswidth);
1249 /* write_vmem() functions */
1250 if (display->buswidth == 8)
1251 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1252 else if (display->buswidth == 9)
1253 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1254 else if (display->buswidth == 16)
1255 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1257 /* GPIO write() functions */
1259 if (display->buswidth == 8)
1260 par->fbtftops.write = fbtft_write_gpio8_wr;
1261 else if (display->buswidth == 16)
1262 par->fbtftops.write = fbtft_write_gpio16_wr;
1265 /* 9-bit SPI setup */
1266 if (par->spi && display->buswidth == 9) {
1267 if (par->spi->master->bits_per_word_mask & SPI_BPW_MASK(9)) {
1268 par->spi->bits_per_word = 9;
1270 dev_warn(&par->spi->dev,
1271 "9-bit SPI not available, emulating using 8-bit.\n");
1272 /* allocate buffer with room for dc bits */
1273 par->extra = devm_kzalloc(par->info->device,
1275 (par->txbuf.len / 8) + 8,
1281 par->fbtftops.write = fbtft_write_spi_emulate_9;
1285 if (!par->fbtftops.verify_gpios)
1286 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1288 /* make sure we still use the driver provided functions */
1289 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1291 /* use init_sequence if provided */
1292 if (par->init_sequence)
1293 par->fbtftops.init_display = fbtft_init_display;
1295 /* use platform_data provided functions above all */
1296 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1298 ret = fbtft_register_framebuffer(info);
1305 fbtft_framebuffer_release(info);
1309 EXPORT_SYMBOL(fbtft_probe_common);
1312 * fbtft_remove_common() - Generic device remove() helper function
1314 * @info: Framebuffer
1316 * Unregisters and releases the framebuffer
1318 * Return: 0 if successful, negative if error
1320 int fbtft_remove_common(struct device *dev, struct fb_info *info)
1322 struct fbtft_par *par;
1328 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1329 "%s()\n", __func__);
1330 fbtft_unregister_framebuffer(info);
1331 fbtft_framebuffer_release(info);
1335 EXPORT_SYMBOL(fbtft_remove_common);
1337 MODULE_LICENSE("GPL");