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 <linux/of_device.h>
30 #include <video/mipi_display.h>
35 static unsigned long debug;
36 module_param(debug, ulong, 0000);
37 MODULE_PARM_DESC(debug, "override device debug level");
39 int fbtft_write_buf_dc(struct fbtft_par *par, void *buf, size_t len, int dc)
43 gpiod_set_value(par->gpio.dc, dc);
45 ret = par->fbtftops.write(par, buf, len);
47 dev_err(par->info->device,
48 "write() failed and returned %d\n", ret);
51 EXPORT_SYMBOL(fbtft_write_buf_dc);
53 void fbtft_dbg_hex(const struct device *dev, int groupsize,
54 const void *buf, size_t len, const char *fmt, ...)
57 static char textbuf[512];
62 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
65 hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len,
66 512 - text_len, false);
69 dev_info(dev, "%s ...\n", text);
71 dev_info(dev, "%s\n", text);
73 EXPORT_SYMBOL(fbtft_dbg_hex);
75 static int fbtft_request_one_gpio(struct fbtft_par *par,
76 const char *name, int index,
77 struct gpio_desc **gpiop)
79 struct device *dev = par->info->device;
81 *gpiop = devm_gpiod_get_index_optional(dev, name, index,
84 return dev_err_probe(dev, PTR_ERR(*gpiop), "Failed to request %s GPIO\n", name);
86 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par, "%s: '%s' GPIO\n",
92 static int fbtft_request_gpios(struct fbtft_par *par)
97 ret = fbtft_request_one_gpio(par, "reset", 0, &par->gpio.reset);
100 ret = fbtft_request_one_gpio(par, "dc", 0, &par->gpio.dc);
103 ret = fbtft_request_one_gpio(par, "rd", 0, &par->gpio.rd);
106 ret = fbtft_request_one_gpio(par, "wr", 0, &par->gpio.wr);
109 ret = fbtft_request_one_gpio(par, "cs", 0, &par->gpio.cs);
112 ret = fbtft_request_one_gpio(par, "latch", 0, &par->gpio.latch);
115 for (i = 0; i < 16; i++) {
116 ret = fbtft_request_one_gpio(par, "db", i,
120 ret = fbtft_request_one_gpio(par, "led", i,
124 ret = fbtft_request_one_gpio(par, "aux", i,
133 static int fbtft_backlight_update_status(struct backlight_device *bd)
135 struct fbtft_par *par = bl_get_data(bd);
136 bool polarity = par->polarity;
138 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
139 "%s: polarity=%d, power=%d, fb_blank=%d\n",
140 __func__, polarity, bd->props.power, bd->props.fb_blank);
142 if (!backlight_is_blank(bd))
143 gpiod_set_value(par->gpio.led[0], polarity);
145 gpiod_set_value(par->gpio.led[0], !polarity);
150 static int fbtft_backlight_get_brightness(struct backlight_device *bd)
152 return bd->props.brightness;
155 void fbtft_unregister_backlight(struct fbtft_par *par)
157 if (par->info->bl_dev) {
158 par->info->bl_dev->props.power = FB_BLANK_POWERDOWN;
159 backlight_update_status(par->info->bl_dev);
160 backlight_device_unregister(par->info->bl_dev);
161 par->info->bl_dev = NULL;
164 EXPORT_SYMBOL(fbtft_unregister_backlight);
166 static const struct backlight_ops fbtft_bl_ops = {
167 .get_brightness = fbtft_backlight_get_brightness,
168 .update_status = fbtft_backlight_update_status,
171 void fbtft_register_backlight(struct fbtft_par *par)
173 struct backlight_device *bd;
174 struct backlight_properties bl_props = { 0, };
176 if (!par->gpio.led[0]) {
177 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
178 "%s(): led pin not set, exiting.\n", __func__);
182 bl_props.type = BACKLIGHT_RAW;
183 /* Assume backlight is off, get polarity from current state of pin */
184 bl_props.power = FB_BLANK_POWERDOWN;
185 if (!gpiod_get_value(par->gpio.led[0]))
186 par->polarity = true;
188 bd = backlight_device_register(dev_driver_string(par->info->device),
189 par->info->device, par,
190 &fbtft_bl_ops, &bl_props);
192 dev_err(par->info->device,
193 "cannot register backlight device (%ld)\n",
197 par->info->bl_dev = bd;
199 if (!par->fbtftops.unregister_backlight)
200 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
202 EXPORT_SYMBOL(fbtft_register_backlight);
204 static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
207 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
208 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
210 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
211 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
213 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
216 static void fbtft_reset(struct fbtft_par *par)
218 if (!par->gpio.reset)
221 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
223 gpiod_set_value_cansleep(par->gpio.reset, 1);
224 usleep_range(20, 40);
225 gpiod_set_value_cansleep(par->gpio.reset, 0);
228 gpiod_set_value_cansleep(par->gpio.cs, 1); /* Activate chip */
231 static void fbtft_update_display(struct fbtft_par *par, unsigned int start_line,
232 unsigned int end_line)
235 ktime_t ts_start, ts_end;
236 long fps, throughput;
240 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE |
241 DEBUG_TIME_EACH_UPDATE))) {
242 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
243 ((par->debug & DEBUG_TIME_FIRST_UPDATE) &&
244 !par->first_update_done)) {
245 ts_start = ktime_get();
251 if (start_line > end_line) {
252 dev_warn(par->info->device,
253 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
254 __func__, start_line, end_line);
256 end_line = par->info->var.yres - 1;
258 if (start_line > par->info->var.yres - 1 ||
259 end_line > par->info->var.yres - 1) {
260 dev_warn(par->info->device,
261 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
262 __func__, start_line,
263 end_line, par->info->var.yres - 1);
265 end_line = par->info->var.yres - 1;
268 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
269 __func__, start_line, end_line);
271 if (par->fbtftops.set_addr_win)
272 par->fbtftops.set_addr_win(par, 0, start_line,
273 par->info->var.xres - 1, end_line);
275 offset = start_line * par->info->fix.line_length;
276 len = (end_line - start_line + 1) * par->info->fix.line_length;
277 ret = par->fbtftops.write_vmem(par, offset, len);
279 dev_err(par->info->device,
280 "%s: write_vmem failed to update display buffer\n",
283 if (unlikely(timeit)) {
284 ts_end = ktime_get();
285 if (!ktime_to_ns(par->update_time))
286 par->update_time = ts_start;
288 fps = ktime_us_delta(ts_start, par->update_time);
289 par->update_time = ts_start;
290 fps = fps ? 1000000 / fps : 0;
292 throughput = ktime_us_delta(ts_end, ts_start);
293 throughput = throughput ? (len * 1000) / throughput : 0;
294 throughput = throughput * 1000 / 1024;
296 dev_info(par->info->device,
297 "Display update: %ld kB/s, fps=%ld\n",
299 par->first_update_done = true;
303 static void fbtft_mkdirty(struct fb_info *info, int y, int height)
305 struct fbtft_par *par = info->par;
306 struct fb_deferred_io *fbdefio = info->fbdefio;
308 /* special case, needed ? */
311 height = info->var.yres;
314 /* Mark display lines/area as dirty */
315 spin_lock(&par->dirty_lock);
316 if (y < par->dirty_lines_start)
317 par->dirty_lines_start = y;
318 if (y + height - 1 > par->dirty_lines_end)
319 par->dirty_lines_end = y + height - 1;
320 spin_unlock(&par->dirty_lock);
322 /* Schedule deferred_io to update display (no-op if already on queue)*/
323 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
326 static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagereflist)
328 struct fbtft_par *par = info->par;
329 unsigned int dirty_lines_start, dirty_lines_end;
330 struct fb_deferred_io_pageref *pageref;
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(pageref, pagereflist, list) {
345 y_low = pageref->offset / info->fix.line_length;
346 y_high = (pageref->offset + PAGE_SIZE - 1) / info->fix.line_length;
347 dev_dbg(info->device,
348 "page->index=%lu y_low=%d y_high=%d\n",
349 pageref->page->index, y_low, y_high);
350 if (y_high > info->var.yres - 1)
351 y_high = info->var.yres - 1;
352 if (y_low < dirty_lines_start)
353 dirty_lines_start = y_low;
354 if (y_high > dirty_lines_end)
355 dirty_lines_end = y_high;
358 par->fbtftops.update_display(info->par,
359 dirty_lines_start, dirty_lines_end);
362 static void fbtft_fb_fillrect(struct fb_info *info,
363 const struct fb_fillrect *rect)
365 struct fbtft_par *par = info->par;
368 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
369 __func__, rect->dx, rect->dy, rect->width, rect->height);
370 sys_fillrect(info, rect);
372 par->fbtftops.mkdirty(info, rect->dy, rect->height);
375 static void fbtft_fb_copyarea(struct fb_info *info,
376 const struct fb_copyarea *area)
378 struct fbtft_par *par = info->par;
381 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
382 __func__, area->dx, area->dy, area->width, area->height);
383 sys_copyarea(info, area);
385 par->fbtftops.mkdirty(info, area->dy, area->height);
388 static void fbtft_fb_imageblit(struct fb_info *info,
389 const struct fb_image *image)
391 struct fbtft_par *par = info->par;
394 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
395 __func__, image->dx, image->dy, image->width, image->height);
396 sys_imageblit(info, image);
398 par->fbtftops.mkdirty(info, image->dy, image->height);
401 static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
402 size_t count, loff_t *ppos)
404 struct fbtft_par *par = info->par;
408 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
409 res = fb_sys_write(info, buf, count, ppos);
411 /* TODO: only mark changed area update all for now */
412 par->fbtftops.mkdirty(info, -1, 0);
418 static unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
421 chan >>= 16 - bf->length;
422 return chan << bf->offset;
425 static int fbtft_fb_setcolreg(unsigned int regno, unsigned int red,
426 unsigned int green, unsigned int blue,
427 unsigned int transp, struct fb_info *info)
433 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
434 __func__, regno, red, green, blue, transp);
436 switch (info->fix.visual) {
437 case FB_VISUAL_TRUECOLOR:
439 u32 *pal = info->pseudo_palette;
441 val = chan_to_field(red, &info->var.red);
442 val |= chan_to_field(green, &info->var.green);
443 val |= chan_to_field(blue, &info->var.blue);
453 static int fbtft_fb_blank(int blank, struct fb_info *info)
455 struct fbtft_par *par = info->par;
458 dev_dbg(info->dev, "%s(blank=%d)\n",
461 if (!par->fbtftops.blank)
465 case FB_BLANK_POWERDOWN:
466 case FB_BLANK_VSYNC_SUSPEND:
467 case FB_BLANK_HSYNC_SUSPEND:
468 case FB_BLANK_NORMAL:
469 ret = par->fbtftops.blank(par, true);
471 case FB_BLANK_UNBLANK:
472 ret = par->fbtftops.blank(par, false);
478 static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
481 dst->write = src->write;
483 dst->read = src->read;
485 dst->write_vmem = src->write_vmem;
486 if (src->write_register)
487 dst->write_register = src->write_register;
488 if (src->set_addr_win)
489 dst->set_addr_win = src->set_addr_win;
491 dst->reset = src->reset;
493 dst->mkdirty = src->mkdirty;
494 if (src->update_display)
495 dst->update_display = src->update_display;
496 if (src->init_display)
497 dst->init_display = src->init_display;
499 dst->blank = src->blank;
500 if (src->request_gpios_match)
501 dst->request_gpios_match = src->request_gpios_match;
502 if (src->request_gpios)
503 dst->request_gpios = src->request_gpios;
504 if (src->verify_gpios)
505 dst->verify_gpios = src->verify_gpios;
506 if (src->register_backlight)
507 dst->register_backlight = src->register_backlight;
508 if (src->unregister_backlight)
509 dst->unregister_backlight = src->unregister_backlight;
511 dst->set_var = src->set_var;
513 dst->set_gamma = src->set_gamma;
517 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
519 * @display: pointer to structure describing the display
520 * @dev: pointer to the device for this fb, this can be NULL
521 * @pdata: platform data for the display in use
523 * Creates a new frame buffer info structure.
525 * Also creates and populates the following structures:
528 * info->pseudo_palette
532 * Returns the new structure, or NULL if an error occurred.
535 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
537 struct fbtft_platform_data *pdata)
539 struct fb_info *info;
540 struct fbtft_par *par;
541 struct fb_ops *fbops = NULL;
542 struct fb_deferred_io *fbdefio = NULL;
548 int txbuflen = display->txbuflen;
549 unsigned int bpp = display->bpp;
550 unsigned int fps = display->fps;
552 const s16 *init_sequence = display->init_sequence;
553 char *gamma = display->gamma;
554 u32 *gamma_curves = NULL;
557 if (display->gamma_num * display->gamma_len >
558 FBTFT_GAMMA_MAX_VALUES_TOTAL) {
559 dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
560 FBTFT_GAMMA_MAX_VALUES_TOTAL);
571 dev_err(dev, "platform data is missing\n");
575 /* override driver values? */
579 txbuflen = pdata->txbuflen;
580 if (pdata->display.init_sequence)
581 init_sequence = pdata->display.init_sequence;
583 gamma = pdata->gamma;
584 if (pdata->display.debug)
585 display->debug = pdata->display.debug;
586 if (pdata->display.backlight)
587 display->backlight = pdata->display.backlight;
588 if (pdata->display.width)
589 display->width = pdata->display.width;
590 if (pdata->display.height)
591 display->height = pdata->display.height;
592 if (pdata->display.buswidth)
593 display->buswidth = pdata->display.buswidth;
594 if (pdata->display.regwidth)
595 display->regwidth = pdata->display.regwidth;
597 display->debug |= debug;
598 fbtft_expand_debug_value(&display->debug);
600 switch (pdata->rotate) {
603 width = display->height;
604 height = display->width;
607 width = display->width;
608 height = display->height;
611 vmem_size = display->width * display->height * bpp / 8;
612 vmem = vzalloc(vmem_size);
616 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
620 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
624 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
628 if (display->gamma_num && display->gamma_len) {
629 gamma_curves = devm_kcalloc(dev,
632 sizeof(gamma_curves[0]),
638 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
642 info->screen_buffer = vmem;
644 info->fbdefio = fbdefio;
646 fbops->owner = dev->driver->owner;
647 fbops->fb_read = fb_sys_read;
648 fbops->fb_write = fbtft_fb_write;
649 fbops->fb_fillrect = fbtft_fb_fillrect;
650 fbops->fb_copyarea = fbtft_fb_copyarea;
651 fbops->fb_imageblit = fbtft_fb_imageblit;
652 fbops->fb_setcolreg = fbtft_fb_setcolreg;
653 fbops->fb_blank = fbtft_fb_blank;
654 fbops->fb_mmap = fb_deferred_io_mmap;
656 fbdefio->delay = HZ / fps;
657 fbdefio->sort_pagereflist = true;
658 fbdefio->deferred_io = fbtft_deferred_io;
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;
669 fb_deferred_io_init(info);
671 info->var.rotate = pdata->rotate;
672 info->var.xres = width;
673 info->var.yres = height;
674 info->var.xres_virtual = info->var.xres;
675 info->var.yres_virtual = info->var.yres;
676 info->var.bits_per_pixel = bpp;
677 info->var.nonstd = 1;
680 info->var.red.offset = 11;
681 info->var.red.length = 5;
682 info->var.green.offset = 5;
683 info->var.green.length = 6;
684 info->var.blue.offset = 0;
685 info->var.blue.length = 5;
686 info->var.transp.offset = 0;
687 info->var.transp.length = 0;
689 info->flags = FBINFO_VIRTFB;
694 par->debug = display->debug;
696 spin_lock_init(&par->dirty_lock);
697 par->bgr = pdata->bgr;
698 par->startbyte = pdata->startbyte;
699 par->init_sequence = init_sequence;
700 par->gamma.curves = gamma_curves;
701 par->gamma.num_curves = display->gamma_num;
702 par->gamma.num_values = display->gamma_len;
703 mutex_init(&par->gamma.lock);
704 info->pseudo_palette = par->pseudo_palette;
706 if (par->gamma.curves && gamma) {
707 if (fbtft_gamma_parse_str(par, par->gamma.curves, gamma,
709 goto release_framebuf;
712 /* Transmit buffer */
714 txbuflen = vmem_size + 2; /* add in case startbyte is used */
715 if (txbuflen >= vmem_size + 2)
718 #ifdef __LITTLE_ENDIAN
719 if ((!txbuflen) && (bpp > 8))
720 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
724 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
726 goto release_framebuf;
727 par->txbuf.buf = txbuf;
728 par->txbuf.len = txbuflen;
731 /* default fbtft operations */
732 par->fbtftops.write = fbtft_write_spi;
733 par->fbtftops.read = fbtft_read_spi;
734 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
735 par->fbtftops.write_register = fbtft_write_reg8_bus8;
736 par->fbtftops.set_addr_win = fbtft_set_addr_win;
737 par->fbtftops.reset = fbtft_reset;
738 par->fbtftops.mkdirty = fbtft_mkdirty;
739 par->fbtftops.update_display = fbtft_update_display;
740 if (display->backlight)
741 par->fbtftops.register_backlight = fbtft_register_backlight;
743 /* use driver provided functions */
744 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
749 framebuffer_release(info);
756 EXPORT_SYMBOL(fbtft_framebuffer_alloc);
759 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
761 * @info: frame buffer info structure
764 void fbtft_framebuffer_release(struct fb_info *info)
766 fb_deferred_io_cleanup(info);
767 vfree(info->screen_buffer);
768 framebuffer_release(info);
770 EXPORT_SYMBOL(fbtft_framebuffer_release);
773 * fbtft_register_framebuffer - registers a tft frame buffer device
774 * @fb_info: frame buffer info structure
776 * Sets SPI driverdata if needed
777 * Requests needed gpios.
778 * Initializes display
780 * Registers a frame buffer device @fb_info.
782 * Returns negative errno on error, or zero for success.
785 int fbtft_register_framebuffer(struct fb_info *fb_info)
790 struct fbtft_par *par = fb_info->par;
791 struct spi_device *spi = par->spi;
794 if (!par->fbtftops.init_display) {
795 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
800 spi_set_drvdata(spi, fb_info);
802 platform_set_drvdata(par->pdev, fb_info);
804 ret = par->fbtftops.request_gpios(par);
808 if (par->fbtftops.verify_gpios) {
809 ret = par->fbtftops.verify_gpios(par);
814 ret = par->fbtftops.init_display(par);
817 if (par->fbtftops.set_var) {
818 ret = par->fbtftops.set_var(par);
823 /* update the entire display */
824 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
826 if (par->fbtftops.set_gamma && par->gamma.curves) {
827 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
832 if (par->fbtftops.register_backlight)
833 par->fbtftops.register_backlight(par);
835 ret = register_framebuffer(fb_info);
839 fbtft_sysfs_init(par);
841 if (par->txbuf.buf && par->txbuf.len >= 1024)
842 sprintf(text1, ", %zu KiB buffer memory", par->txbuf.len >> 10);
844 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
845 spi_get_chipselect(spi, 0), spi->max_speed_hz / 1000000);
846 dev_info(fb_info->dev,
847 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
848 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
849 fb_info->fix.smem_len >> 10, text1,
850 HZ / fb_info->fbdefio->delay, text2);
852 /* Turn on backlight if available */
853 if (fb_info->bl_dev) {
854 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
855 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
861 if (par->fbtftops.unregister_backlight)
862 par->fbtftops.unregister_backlight(par);
866 EXPORT_SYMBOL(fbtft_register_framebuffer);
869 * fbtft_unregister_framebuffer - releases a tft frame buffer device
870 * @fb_info: frame buffer info structure
872 * Frees SPI driverdata if needed
874 * Unregisters frame buffer device.
877 int fbtft_unregister_framebuffer(struct fb_info *fb_info)
879 struct fbtft_par *par = fb_info->par;
881 if (par->fbtftops.unregister_backlight)
882 par->fbtftops.unregister_backlight(par);
883 fbtft_sysfs_exit(par);
884 unregister_framebuffer(fb_info);
888 EXPORT_SYMBOL(fbtft_unregister_framebuffer);
891 * fbtft_init_display_from_property() - Device Tree init_display() function
894 * Return: 0 if successful, negative if error
896 static int fbtft_init_display_from_property(struct fbtft_par *par)
898 struct device *dev = par->info->device;
899 int buf[64], count, index, i, j, ret;
903 count = device_property_count_u32(dev, "init");
909 values = kmalloc_array(count + 1, sizeof(*values), GFP_KERNEL);
913 ret = device_property_read_u32_array(dev, "init", values, count);
917 par->fbtftops.reset(par);
920 val = values[++index];
922 while (index < count) {
923 if (val & FBTFT_OF_INIT_CMD) {
926 while ((index < count) && !(val & 0xFFFF0000)) {
929 "%s: Maximum register values exceeded\n",
935 val = values[++index];
937 /* make debug message */
938 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
939 "init: write_register:\n");
940 for (j = 0; j < i; j++)
941 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
942 "buf[%d] = %02X\n", j, buf[j]);
944 par->fbtftops.write_register(par, i,
945 buf[0], buf[1], buf[2], buf[3],
946 buf[4], buf[5], buf[6], buf[7],
947 buf[8], buf[9], buf[10], buf[11],
948 buf[12], buf[13], buf[14], buf[15],
949 buf[16], buf[17], buf[18], buf[19],
950 buf[20], buf[21], buf[22], buf[23],
951 buf[24], buf[25], buf[26], buf[27],
952 buf[28], buf[29], buf[30], buf[31],
953 buf[32], buf[33], buf[34], buf[35],
954 buf[36], buf[37], buf[38], buf[39],
955 buf[40], buf[41], buf[42], buf[43],
956 buf[44], buf[45], buf[46], buf[47],
957 buf[48], buf[49], buf[50], buf[51],
958 buf[52], buf[53], buf[54], buf[55],
959 buf[56], buf[57], buf[58], buf[59],
960 buf[60], buf[61], buf[62], buf[63]);
961 } else if (val & FBTFT_OF_INIT_DELAY) {
962 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
963 "init: msleep(%u)\n", val & 0xFFFF);
964 msleep(val & 0xFFFF);
965 val = values[++index];
967 dev_err(dev, "illegal init value 0x%X\n", val);
979 * fbtft_init_display() - Generic init_display() function
982 * Uses par->init_sequence to do the initialization
984 * Return: 0 if successful, negative if error
986 int fbtft_init_display(struct fbtft_par *par)
993 if (!par->init_sequence) {
994 dev_err(par->info->device,
995 "error: init_sequence is not set\n");
999 /* make sure stop marker exists */
1000 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++) {
1001 if (par->init_sequence[i] == -3)
1005 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1006 dev_err(par->info->device,
1007 "missing stop marker at end of init sequence\n");
1011 par->fbtftops.reset(par);
1014 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1015 if (par->init_sequence[i] == -3) {
1019 if (par->init_sequence[i] >= 0) {
1020 dev_err(par->info->device,
1021 "missing delimiter at position %d\n", i);
1024 if (par->init_sequence[i + 1] < 0) {
1025 dev_err(par->info->device,
1026 "missing value after delimiter %d at position %d\n",
1027 par->init_sequence[i], i);
1030 switch (par->init_sequence[i]) {
1034 /* make debug message */
1035 for (j = 0; par->init_sequence[i + 1 + j] >= 0; j++)
1038 fbtft_par_dbg_hex(DEBUG_INIT_DISPLAY, par, par->info->device,
1039 s16, &par->init_sequence[i + 1], j,
1040 "init: write(0x%02X)", par->init_sequence[i]);
1044 while (par->init_sequence[i] >= 0) {
1046 dev_err(par->info->device,
1047 "%s: Maximum register values exceeded\n",
1051 buf[j++] = par->init_sequence[i++];
1053 par->fbtftops.write_register(par, j,
1054 buf[0], buf[1], buf[2], buf[3],
1055 buf[4], buf[5], buf[6], buf[7],
1056 buf[8], buf[9], buf[10], buf[11],
1057 buf[12], buf[13], buf[14], buf[15],
1058 buf[16], buf[17], buf[18], buf[19],
1059 buf[20], buf[21], buf[22], buf[23],
1060 buf[24], buf[25], buf[26], buf[27],
1061 buf[28], buf[29], buf[30], buf[31],
1062 buf[32], buf[33], buf[34], buf[35],
1063 buf[36], buf[37], buf[38], buf[39],
1064 buf[40], buf[41], buf[42], buf[43],
1065 buf[44], buf[45], buf[46], buf[47],
1066 buf[48], buf[49], buf[50], buf[51],
1067 buf[52], buf[53], buf[54], buf[55],
1068 buf[56], buf[57], buf[58], buf[59],
1069 buf[60], buf[61], buf[62], buf[63]);
1073 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1074 "init: mdelay(%d)\n",
1075 par->init_sequence[i]);
1076 mdelay(par->init_sequence[i++]);
1079 dev_err(par->info->device,
1080 "unknown delimiter %d at position %d\n",
1081 par->init_sequence[i], i);
1086 dev_err(par->info->device,
1087 "%s: something is wrong. Shouldn't get here.\n", __func__);
1090 EXPORT_SYMBOL(fbtft_init_display);
1093 * fbtft_verify_gpios() - Generic verify_gpios() function
1096 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1098 * Return: 0 if successful, negative if error
1100 static int fbtft_verify_gpios(struct fbtft_par *par)
1102 struct fbtft_platform_data *pdata = par->pdata;
1105 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1107 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
1109 dev_err(par->info->device,
1110 "Missing info about 'dc' gpio. Aborting.\n");
1117 if (!par->gpio.wr) {
1118 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1121 for (i = 0; i < pdata->display.buswidth; i++) {
1122 if (!par->gpio.db[i]) {
1123 dev_err(par->info->device,
1124 "Missing 'db%02d' gpio. Aborting.\n", i);
1132 /* returns 0 if the property is not present */
1133 static u32 fbtft_property_value(struct device *dev, const char *propname)
1138 ret = device_property_read_u32(dev, propname, &val);
1140 dev_info(dev, "%s: %s = %u\n", __func__, propname, val);
1145 static struct fbtft_platform_data *fbtft_properties_read(struct device *dev)
1147 struct fbtft_platform_data *pdata;
1149 if (!dev_fwnode(dev)) {
1150 dev_err(dev, "Missing platform data or properties\n");
1151 return ERR_PTR(-EINVAL);
1154 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1156 return ERR_PTR(-ENOMEM);
1158 pdata->display.width = fbtft_property_value(dev, "width");
1159 pdata->display.height = fbtft_property_value(dev, "height");
1160 pdata->display.regwidth = fbtft_property_value(dev, "regwidth");
1161 pdata->display.buswidth = fbtft_property_value(dev, "buswidth");
1162 pdata->display.backlight = fbtft_property_value(dev, "backlight");
1163 pdata->display.bpp = fbtft_property_value(dev, "bpp");
1164 pdata->display.debug = fbtft_property_value(dev, "debug");
1165 pdata->rotate = fbtft_property_value(dev, "rotate");
1166 pdata->bgr = device_property_read_bool(dev, "bgr");
1167 pdata->fps = fbtft_property_value(dev, "fps");
1168 pdata->txbuflen = fbtft_property_value(dev, "txbuflen");
1169 pdata->startbyte = fbtft_property_value(dev, "startbyte");
1170 device_property_read_string(dev, "gamma", (const char **)&pdata->gamma);
1172 if (device_property_present(dev, "led-gpios"))
1173 pdata->display.backlight = 1;
1174 if (device_property_present(dev, "init"))
1175 pdata->display.fbtftops.init_display =
1176 fbtft_init_display_from_property;
1178 pdata->display.fbtftops.request_gpios = fbtft_request_gpios;
1184 * fbtft_probe_common() - Generic device probe() helper function
1185 * @display: Display properties
1187 * @pdev: Platform device
1188 * @dt_ids: Compatible string table
1190 * Allocates, initializes and registers a framebuffer
1192 * Either @sdev or @pdev should be NULL
1194 * Return: 0 if successful, negative if error
1196 int fbtft_probe_common(struct fbtft_display *display,
1197 struct spi_device *sdev,
1198 struct platform_device *pdev,
1199 const struct of_device_id *dt_ids)
1202 struct fb_info *info;
1203 struct fbtft_par *par;
1204 struct fbtft_platform_data *pdata;
1205 const struct of_device_id *match;
1206 int (*variant)(struct fbtft_display *);
1214 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1215 dev_info(dev, "%s()\n", __func__);
1217 pdata = dev->platform_data;
1219 pdata = fbtft_properties_read(dev);
1221 return PTR_ERR(pdata);
1222 match = of_match_device(dt_ids, dev);
1223 if (match && match->data) {
1224 /* apply the variant */
1225 variant = match->data;
1226 ret = (*variant)(display);
1232 info = fbtft_framebuffer_alloc(display, dev, pdata);
1240 if (display->buswidth == 0) {
1241 dev_err(dev, "buswidth is not set\n");
1245 /* write register functions */
1246 if (display->regwidth == 8 && display->buswidth == 8)
1247 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1248 else if (display->regwidth == 8 && display->buswidth == 9 && par->spi)
1249 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1250 else if (display->regwidth == 16 && display->buswidth == 8)
1251 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1252 else if (display->regwidth == 16 && display->buswidth == 16)
1253 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1256 "no default functions for regwidth=%d and buswidth=%d\n",
1257 display->regwidth, display->buswidth);
1259 /* write_vmem() functions */
1260 if (display->buswidth == 8)
1261 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1262 else if (display->buswidth == 9)
1263 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1264 else if (display->buswidth == 16)
1265 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1267 /* GPIO write() functions */
1269 if (display->buswidth == 8)
1270 par->fbtftops.write = fbtft_write_gpio8_wr;
1271 else if (display->buswidth == 16)
1272 par->fbtftops.write = fbtft_write_gpio16_wr;
1275 /* 9-bit SPI setup */
1276 if (par->spi && display->buswidth == 9) {
1277 if (par->spi->master->bits_per_word_mask & SPI_BPW_MASK(9)) {
1278 par->spi->bits_per_word = 9;
1280 dev_warn(&par->spi->dev,
1281 "9-bit SPI not available, emulating using 8-bit.\n");
1282 /* allocate buffer with room for dc bits */
1283 par->extra = devm_kzalloc(par->info->device,
1285 (par->txbuf.len / 8) + 8,
1291 par->fbtftops.write = fbtft_write_spi_emulate_9;
1295 if (!par->fbtftops.verify_gpios)
1296 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1298 /* make sure we still use the driver provided functions */
1299 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1301 /* use init_sequence if provided */
1302 if (par->init_sequence)
1303 par->fbtftops.init_display = fbtft_init_display;
1305 /* use platform_data provided functions above all */
1306 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1308 ret = fbtft_register_framebuffer(info);
1315 fbtft_framebuffer_release(info);
1319 EXPORT_SYMBOL(fbtft_probe_common);
1322 * fbtft_remove_common() - Generic device remove() helper function
1324 * @info: Framebuffer
1326 * Unregisters and releases the framebuffer
1328 void fbtft_remove_common(struct device *dev, struct fb_info *info)
1330 struct fbtft_par *par;
1334 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1335 "%s()\n", __func__);
1336 fbtft_unregister_framebuffer(info);
1337 fbtft_framebuffer_release(info);
1339 EXPORT_SYMBOL(fbtft_remove_common);
1341 MODULE_LICENSE("GPL");