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 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 ((bd->props.power == FB_BLANK_UNBLANK) &&
143 (bd->props.fb_blank == FB_BLANK_UNBLANK))
144 gpiod_set_value(par->gpio.led[0], polarity);
146 gpiod_set_value(par->gpio.led[0], !polarity);
151 static int fbtft_backlight_get_brightness(struct backlight_device *bd)
153 return bd->props.brightness;
156 void fbtft_unregister_backlight(struct fbtft_par *par)
158 if (par->info->bl_dev) {
159 par->info->bl_dev->props.power = FB_BLANK_POWERDOWN;
160 backlight_update_status(par->info->bl_dev);
161 backlight_device_unregister(par->info->bl_dev);
162 par->info->bl_dev = NULL;
165 EXPORT_SYMBOL(fbtft_unregister_backlight);
167 static const struct backlight_ops fbtft_bl_ops = {
168 .get_brightness = fbtft_backlight_get_brightness,
169 .update_status = fbtft_backlight_update_status,
172 void fbtft_register_backlight(struct fbtft_par *par)
174 struct backlight_device *bd;
175 struct backlight_properties bl_props = { 0, };
177 if (!par->gpio.led[0]) {
178 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
179 "%s(): led pin not set, exiting.\n", __func__);
183 bl_props.type = BACKLIGHT_RAW;
184 /* Assume backlight is off, get polarity from current state of pin */
185 bl_props.power = FB_BLANK_POWERDOWN;
186 if (!gpiod_get_value(par->gpio.led[0]))
187 par->polarity = true;
189 bd = backlight_device_register(dev_driver_string(par->info->device),
190 par->info->device, par,
191 &fbtft_bl_ops, &bl_props);
193 dev_err(par->info->device,
194 "cannot register backlight device (%ld)\n",
198 par->info->bl_dev = bd;
200 if (!par->fbtftops.unregister_backlight)
201 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
203 EXPORT_SYMBOL(fbtft_register_backlight);
205 static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
208 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
209 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
211 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
212 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
214 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
217 static void fbtft_reset(struct fbtft_par *par)
219 if (!par->gpio.reset)
222 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
224 gpiod_set_value_cansleep(par->gpio.reset, 1);
225 usleep_range(20, 40);
226 gpiod_set_value_cansleep(par->gpio.reset, 0);
229 gpiod_set_value_cansleep(par->gpio.cs, 1); /* Activate chip */
232 static void fbtft_update_display(struct fbtft_par *par, unsigned int start_line,
233 unsigned int end_line)
236 ktime_t ts_start, ts_end;
237 long fps, throughput;
241 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE |
242 DEBUG_TIME_EACH_UPDATE))) {
243 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
244 ((par->debug & DEBUG_TIME_FIRST_UPDATE) &&
245 !par->first_update_done)) {
246 ts_start = ktime_get();
252 if (start_line > end_line) {
253 dev_warn(par->info->device,
254 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
255 __func__, start_line, end_line);
257 end_line = par->info->var.yres - 1;
259 if (start_line > par->info->var.yres - 1 ||
260 end_line > par->info->var.yres - 1) {
261 dev_warn(par->info->device,
262 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
263 __func__, start_line,
264 end_line, par->info->var.yres - 1);
266 end_line = par->info->var.yres - 1;
269 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
270 __func__, start_line, end_line);
272 if (par->fbtftops.set_addr_win)
273 par->fbtftops.set_addr_win(par, 0, start_line,
274 par->info->var.xres - 1, end_line);
276 offset = start_line * par->info->fix.line_length;
277 len = (end_line - start_line + 1) * par->info->fix.line_length;
278 ret = par->fbtftops.write_vmem(par, offset, len);
280 dev_err(par->info->device,
281 "%s: write_vmem failed to update display buffer\n",
284 if (unlikely(timeit)) {
285 ts_end = ktime_get();
286 if (!ktime_to_ns(par->update_time))
287 par->update_time = ts_start;
289 fps = ktime_us_delta(ts_start, par->update_time);
290 par->update_time = ts_start;
291 fps = fps ? 1000000 / fps : 0;
293 throughput = ktime_us_delta(ts_end, ts_start);
294 throughput = throughput ? (len * 1000) / throughput : 0;
295 throughput = throughput * 1000 / 1024;
297 dev_info(par->info->device,
298 "Display update: %ld kB/s, fps=%ld\n",
300 par->first_update_done = true;
304 static void fbtft_mkdirty(struct fb_info *info, int y, int height)
306 struct fbtft_par *par = info->par;
307 struct fb_deferred_io *fbdefio = info->fbdefio;
309 /* special case, needed ? */
312 height = info->var.yres;
315 /* Mark display lines/area as dirty */
316 spin_lock(&par->dirty_lock);
317 if (y < par->dirty_lines_start)
318 par->dirty_lines_start = y;
319 if (y + height - 1 > par->dirty_lines_end)
320 par->dirty_lines_end = y + height - 1;
321 spin_unlock(&par->dirty_lock);
323 /* Schedule deferred_io to update display (no-op if already on queue)*/
324 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
327 static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist)
329 struct fbtft_par *par = info->par;
330 unsigned int dirty_lines_start, dirty_lines_end;
333 unsigned int y_low = 0, y_high = 0;
336 spin_lock(&par->dirty_lock);
337 dirty_lines_start = par->dirty_lines_start;
338 dirty_lines_end = par->dirty_lines_end;
339 /* set display line markers as clean */
340 par->dirty_lines_start = par->info->var.yres - 1;
341 par->dirty_lines_end = 0;
342 spin_unlock(&par->dirty_lock);
344 /* Mark display lines as dirty */
345 list_for_each_entry(page, pagelist, lru) {
347 index = page->index << PAGE_SHIFT;
348 y_low = index / info->fix.line_length;
349 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length;
350 dev_dbg(info->device,
351 "page->index=%lu y_low=%d y_high=%d\n",
352 page->index, y_low, y_high);
353 if (y_high > info->var.yres - 1)
354 y_high = info->var.yres - 1;
355 if (y_low < dirty_lines_start)
356 dirty_lines_start = y_low;
357 if (y_high > dirty_lines_end)
358 dirty_lines_end = y_high;
361 par->fbtftops.update_display(info->par,
362 dirty_lines_start, dirty_lines_end);
365 static void fbtft_fb_fillrect(struct fb_info *info,
366 const struct fb_fillrect *rect)
368 struct fbtft_par *par = info->par;
371 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
372 __func__, rect->dx, rect->dy, rect->width, rect->height);
373 sys_fillrect(info, rect);
375 par->fbtftops.mkdirty(info, rect->dy, rect->height);
378 static void fbtft_fb_copyarea(struct fb_info *info,
379 const struct fb_copyarea *area)
381 struct fbtft_par *par = info->par;
384 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
385 __func__, area->dx, area->dy, area->width, area->height);
386 sys_copyarea(info, area);
388 par->fbtftops.mkdirty(info, area->dy, area->height);
391 static void fbtft_fb_imageblit(struct fb_info *info,
392 const struct fb_image *image)
394 struct fbtft_par *par = info->par;
397 "%s: dx=%d, dy=%d, width=%d, height=%d\n",
398 __func__, image->dx, image->dy, image->width, image->height);
399 sys_imageblit(info, image);
401 par->fbtftops.mkdirty(info, image->dy, image->height);
404 static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf,
405 size_t count, loff_t *ppos)
407 struct fbtft_par *par = info->par;
411 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos);
412 res = fb_sys_write(info, buf, count, ppos);
414 /* TODO: only mark changed area update all for now */
415 par->fbtftops.mkdirty(info, -1, 0);
421 static unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
424 chan >>= 16 - bf->length;
425 return chan << bf->offset;
428 static int fbtft_fb_setcolreg(unsigned int regno, unsigned int red,
429 unsigned int green, unsigned int blue,
430 unsigned int transp, struct fb_info *info)
436 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
437 __func__, regno, red, green, blue, transp);
439 switch (info->fix.visual) {
440 case FB_VISUAL_TRUECOLOR:
442 u32 *pal = info->pseudo_palette;
444 val = chan_to_field(red, &info->var.red);
445 val |= chan_to_field(green, &info->var.green);
446 val |= chan_to_field(blue, &info->var.blue);
456 static int fbtft_fb_blank(int blank, struct fb_info *info)
458 struct fbtft_par *par = info->par;
461 dev_dbg(info->dev, "%s(blank=%d)\n",
464 if (!par->fbtftops.blank)
468 case FB_BLANK_POWERDOWN:
469 case FB_BLANK_VSYNC_SUSPEND:
470 case FB_BLANK_HSYNC_SUSPEND:
471 case FB_BLANK_NORMAL:
472 ret = par->fbtftops.blank(par, true);
474 case FB_BLANK_UNBLANK:
475 ret = par->fbtftops.blank(par, false);
481 static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
484 dst->write = src->write;
486 dst->read = src->read;
488 dst->write_vmem = src->write_vmem;
489 if (src->write_register)
490 dst->write_register = src->write_register;
491 if (src->set_addr_win)
492 dst->set_addr_win = src->set_addr_win;
494 dst->reset = src->reset;
496 dst->mkdirty = src->mkdirty;
497 if (src->update_display)
498 dst->update_display = src->update_display;
499 if (src->init_display)
500 dst->init_display = src->init_display;
502 dst->blank = src->blank;
503 if (src->request_gpios_match)
504 dst->request_gpios_match = src->request_gpios_match;
505 if (src->request_gpios)
506 dst->request_gpios = src->request_gpios;
507 if (src->verify_gpios)
508 dst->verify_gpios = src->verify_gpios;
509 if (src->register_backlight)
510 dst->register_backlight = src->register_backlight;
511 if (src->unregister_backlight)
512 dst->unregister_backlight = src->unregister_backlight;
514 dst->set_var = src->set_var;
516 dst->set_gamma = src->set_gamma;
520 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
522 * @display: pointer to structure describing the display
523 * @dev: pointer to the device for this fb, this can be NULL
524 * @pdata: platform data for the display in use
526 * Creates a new frame buffer info structure.
528 * Also creates and populates the following structures:
531 * info->pseudo_palette
535 * Returns the new structure, or NULL if an error occurred.
538 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
540 struct fbtft_platform_data *pdata)
542 struct fb_info *info;
543 struct fbtft_par *par;
544 struct fb_ops *fbops = NULL;
545 struct fb_deferred_io *fbdefio = NULL;
551 int txbuflen = display->txbuflen;
552 unsigned int bpp = display->bpp;
553 unsigned int fps = display->fps;
555 const s16 *init_sequence = display->init_sequence;
556 char *gamma = display->gamma;
557 u32 *gamma_curves = NULL;
560 if (display->gamma_num * display->gamma_len >
561 FBTFT_GAMMA_MAX_VALUES_TOTAL) {
562 dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
563 FBTFT_GAMMA_MAX_VALUES_TOTAL);
574 dev_err(dev, "platform data is missing\n");
578 /* override driver values? */
582 txbuflen = pdata->txbuflen;
583 if (pdata->display.init_sequence)
584 init_sequence = pdata->display.init_sequence;
586 gamma = pdata->gamma;
587 if (pdata->display.debug)
588 display->debug = pdata->display.debug;
589 if (pdata->display.backlight)
590 display->backlight = pdata->display.backlight;
591 if (pdata->display.width)
592 display->width = pdata->display.width;
593 if (pdata->display.height)
594 display->height = pdata->display.height;
595 if (pdata->display.buswidth)
596 display->buswidth = pdata->display.buswidth;
597 if (pdata->display.regwidth)
598 display->regwidth = pdata->display.regwidth;
600 display->debug |= debug;
601 fbtft_expand_debug_value(&display->debug);
603 switch (pdata->rotate) {
606 width = display->height;
607 height = display->width;
610 width = display->width;
611 height = display->height;
614 vmem_size = display->width * display->height * bpp / 8;
615 vmem = vzalloc(vmem_size);
619 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL);
623 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
627 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
631 if (display->gamma_num && display->gamma_len) {
632 gamma_curves = devm_kcalloc(dev,
635 sizeof(gamma_curves[0]),
641 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
645 info->screen_buffer = vmem;
647 info->fbdefio = fbdefio;
649 fbops->owner = dev->driver->owner;
650 fbops->fb_read = fb_sys_read;
651 fbops->fb_write = fbtft_fb_write;
652 fbops->fb_fillrect = fbtft_fb_fillrect;
653 fbops->fb_copyarea = fbtft_fb_copyarea;
654 fbops->fb_imageblit = fbtft_fb_imageblit;
655 fbops->fb_setcolreg = fbtft_fb_setcolreg;
656 fbops->fb_blank = fbtft_fb_blank;
658 fbdefio->delay = HZ / fps;
659 fbdefio->deferred_io = fbtft_deferred_io;
660 fb_deferred_io_init(info);
662 snprintf(info->fix.id, sizeof(info->fix.id), "%s", dev->driver->name);
663 info->fix.type = FB_TYPE_PACKED_PIXELS;
664 info->fix.visual = FB_VISUAL_TRUECOLOR;
665 info->fix.xpanstep = 0;
666 info->fix.ypanstep = 0;
667 info->fix.ywrapstep = 0;
668 info->fix.line_length = width * bpp / 8;
669 info->fix.accel = FB_ACCEL_NONE;
670 info->fix.smem_len = vmem_size;
672 info->var.rotate = pdata->rotate;
673 info->var.xres = width;
674 info->var.yres = height;
675 info->var.xres_virtual = info->var.xres;
676 info->var.yres_virtual = info->var.yres;
677 info->var.bits_per_pixel = bpp;
678 info->var.nonstd = 1;
681 info->var.red.offset = 11;
682 info->var.red.length = 5;
683 info->var.green.offset = 5;
684 info->var.green.length = 6;
685 info->var.blue.offset = 0;
686 info->var.blue.length = 5;
687 info->var.transp.offset = 0;
688 info->var.transp.length = 0;
690 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
695 par->debug = display->debug;
697 spin_lock_init(&par->dirty_lock);
698 par->bgr = pdata->bgr;
699 par->startbyte = pdata->startbyte;
700 par->init_sequence = init_sequence;
701 par->gamma.curves = gamma_curves;
702 par->gamma.num_curves = display->gamma_num;
703 par->gamma.num_values = display->gamma_len;
704 mutex_init(&par->gamma.lock);
705 info->pseudo_palette = par->pseudo_palette;
707 if (par->gamma.curves && gamma) {
708 if (fbtft_gamma_parse_str(par, par->gamma.curves, gamma,
710 goto release_framebuf;
713 /* Transmit buffer */
715 txbuflen = vmem_size + 2; /* add in case startbyte is used */
716 if (txbuflen >= vmem_size + 2)
719 #ifdef __LITTLE_ENDIAN
720 if ((!txbuflen) && (bpp > 8))
721 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
725 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
727 goto release_framebuf;
728 par->txbuf.buf = txbuf;
729 par->txbuf.len = txbuflen;
732 /* default fbtft operations */
733 par->fbtftops.write = fbtft_write_spi;
734 par->fbtftops.read = fbtft_read_spi;
735 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
736 par->fbtftops.write_register = fbtft_write_reg8_bus8;
737 par->fbtftops.set_addr_win = fbtft_set_addr_win;
738 par->fbtftops.reset = fbtft_reset;
739 par->fbtftops.mkdirty = fbtft_mkdirty;
740 par->fbtftops.update_display = fbtft_update_display;
741 if (display->backlight)
742 par->fbtftops.register_backlight = fbtft_register_backlight;
744 /* use driver provided functions */
745 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
750 framebuffer_release(info);
757 EXPORT_SYMBOL(fbtft_framebuffer_alloc);
760 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
762 * @info: frame buffer info structure
765 void fbtft_framebuffer_release(struct fb_info *info)
767 fb_deferred_io_cleanup(info);
768 vfree(info->screen_buffer);
769 framebuffer_release(info);
771 EXPORT_SYMBOL(fbtft_framebuffer_release);
774 * fbtft_register_framebuffer - registers a tft frame buffer device
775 * @fb_info: frame buffer info structure
777 * Sets SPI driverdata if needed
778 * Requests needed gpios.
779 * Initializes display
781 * Registers a frame buffer device @fb_info.
783 * Returns negative errno on error, or zero for success.
786 int fbtft_register_framebuffer(struct fb_info *fb_info)
791 struct fbtft_par *par = fb_info->par;
792 struct spi_device *spi = par->spi;
795 if (!par->fbtftops.init_display) {
796 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
801 spi_set_drvdata(spi, fb_info);
803 platform_set_drvdata(par->pdev, fb_info);
805 ret = par->fbtftops.request_gpios(par);
809 if (par->fbtftops.verify_gpios) {
810 ret = par->fbtftops.verify_gpios(par);
815 ret = par->fbtftops.init_display(par);
818 if (par->fbtftops.set_var) {
819 ret = par->fbtftops.set_var(par);
824 /* update the entire display */
825 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
827 if (par->fbtftops.set_gamma && par->gamma.curves) {
828 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
833 if (par->fbtftops.register_backlight)
834 par->fbtftops.register_backlight(par);
836 ret = register_framebuffer(fb_info);
840 fbtft_sysfs_init(par);
842 if (par->txbuf.buf && par->txbuf.len >= 1024)
843 sprintf(text1, ", %zu KiB buffer memory", par->txbuf.len >> 10);
845 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num,
846 spi->chip_select, spi->max_speed_hz / 1000000);
847 dev_info(fb_info->dev,
848 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
849 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
850 fb_info->fix.smem_len >> 10, text1,
851 HZ / fb_info->fbdefio->delay, text2);
853 /* Turn on backlight if available */
854 if (fb_info->bl_dev) {
855 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
856 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
862 if (par->fbtftops.unregister_backlight)
863 par->fbtftops.unregister_backlight(par);
867 EXPORT_SYMBOL(fbtft_register_framebuffer);
870 * fbtft_unregister_framebuffer - releases a tft frame buffer device
871 * @fb_info: frame buffer info structure
873 * Frees SPI driverdata if needed
875 * Unregisters frame buffer device.
878 int fbtft_unregister_framebuffer(struct fb_info *fb_info)
880 struct fbtft_par *par = fb_info->par;
882 if (par->fbtftops.unregister_backlight)
883 par->fbtftops.unregister_backlight(par);
884 fbtft_sysfs_exit(par);
885 unregister_framebuffer(fb_info);
889 EXPORT_SYMBOL(fbtft_unregister_framebuffer);
892 * fbtft_init_display_from_property() - Device Tree init_display() function
895 * Return: 0 if successful, negative if error
897 static int fbtft_init_display_from_property(struct fbtft_par *par)
899 struct device *dev = par->info->device;
900 int buf[64], count, index, i, j, ret;
904 count = device_property_count_u32(dev, "init");
910 values = kmalloc_array(count + 1, sizeof(*values), GFP_KERNEL);
914 ret = device_property_read_u32_array(dev, "init", values, count);
918 par->fbtftops.reset(par);
921 val = values[++index];
923 while (index < count) {
924 if (val & FBTFT_OF_INIT_CMD) {
927 while ((index < count) && !(val & 0xFFFF0000)) {
930 "%s: Maximum register values exceeded\n",
936 val = values[++index];
938 /* make debug message */
939 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
940 "init: write_register:\n");
941 for (j = 0; j < i; j++)
942 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
943 "buf[%d] = %02X\n", j, buf[j]);
945 par->fbtftops.write_register(par, i,
946 buf[0], buf[1], buf[2], buf[3],
947 buf[4], buf[5], buf[6], buf[7],
948 buf[8], buf[9], buf[10], buf[11],
949 buf[12], buf[13], buf[14], buf[15],
950 buf[16], buf[17], buf[18], buf[19],
951 buf[20], buf[21], buf[22], buf[23],
952 buf[24], buf[25], buf[26], buf[27],
953 buf[28], buf[29], buf[30], buf[31],
954 buf[32], buf[33], buf[34], buf[35],
955 buf[36], buf[37], buf[38], buf[39],
956 buf[40], buf[41], buf[42], buf[43],
957 buf[44], buf[45], buf[46], buf[47],
958 buf[48], buf[49], buf[50], buf[51],
959 buf[52], buf[53], buf[54], buf[55],
960 buf[56], buf[57], buf[58], buf[59],
961 buf[60], buf[61], buf[62], buf[63]);
962 } else if (val & FBTFT_OF_INIT_DELAY) {
963 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
964 "init: msleep(%u)\n", val & 0xFFFF);
965 msleep(val & 0xFFFF);
966 val = values[++index];
968 dev_err(dev, "illegal init value 0x%X\n", val);
980 * fbtft_init_display() - Generic init_display() function
983 * Uses par->init_sequence to do the initialization
985 * Return: 0 if successful, negative if error
987 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)
1006 if (i == FBTFT_MAX_INIT_SEQUENCE) {
1007 dev_err(par->info->device,
1008 "missing stop marker at end of init sequence\n");
1012 par->fbtftops.reset(par);
1015 while (i < FBTFT_MAX_INIT_SEQUENCE) {
1016 if (par->init_sequence[i] == -3) {
1020 if (par->init_sequence[i] >= 0) {
1021 dev_err(par->info->device,
1022 "missing delimiter at position %d\n", i);
1025 if (par->init_sequence[i + 1] < 0) {
1026 dev_err(par->info->device,
1027 "missing value after delimiter %d at position %d\n",
1028 par->init_sequence[i], i);
1031 switch (par->init_sequence[i]) {
1035 /* make debug message */
1036 for (j = 0; par->init_sequence[i + 1 + j] >= 0; j++);
1038 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1039 "init: write(0x%02X) %*ph\n",
1040 par->init_sequence[i], j,
1041 &par->init_sequence[i + 1]);
1045 while (par->init_sequence[i] >= 0) {
1047 dev_err(par->info->device,
1048 "%s: Maximum register values exceeded\n",
1052 buf[j++] = par->init_sequence[i++];
1054 par->fbtftops.write_register(par, j,
1055 buf[0], buf[1], buf[2], buf[3],
1056 buf[4], buf[5], buf[6], buf[7],
1057 buf[8], buf[9], buf[10], buf[11],
1058 buf[12], buf[13], buf[14], buf[15],
1059 buf[16], buf[17], buf[18], buf[19],
1060 buf[20], buf[21], buf[22], buf[23],
1061 buf[24], buf[25], buf[26], buf[27],
1062 buf[28], buf[29], buf[30], buf[31],
1063 buf[32], buf[33], buf[34], buf[35],
1064 buf[36], buf[37], buf[38], buf[39],
1065 buf[40], buf[41], buf[42], buf[43],
1066 buf[44], buf[45], buf[46], buf[47],
1067 buf[48], buf[49], buf[50], buf[51],
1068 buf[52], buf[53], buf[54], buf[55],
1069 buf[56], buf[57], buf[58], buf[59],
1070 buf[60], buf[61], buf[62], buf[63]);
1074 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1075 "init: mdelay(%d)\n",
1076 par->init_sequence[i]);
1077 mdelay(par->init_sequence[i++]);
1080 dev_err(par->info->device,
1081 "unknown delimiter %d at position %d\n",
1082 par->init_sequence[i], i);
1087 dev_err(par->info->device,
1088 "%s: something is wrong. Shouldn't get here.\n", __func__);
1091 EXPORT_SYMBOL(fbtft_init_display);
1094 * fbtft_verify_gpios() - Generic verify_gpios() function
1097 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1099 * Return: 0 if successful, negative if error
1101 static int fbtft_verify_gpios(struct fbtft_par *par)
1103 struct fbtft_platform_data *pdata = par->pdata;
1106 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1108 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
1110 dev_err(par->info->device,
1111 "Missing info about 'dc' gpio. Aborting.\n");
1118 if (!par->gpio.wr) {
1119 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1122 for (i = 0; i < pdata->display.buswidth; i++) {
1123 if (!par->gpio.db[i]) {
1124 dev_err(par->info->device,
1125 "Missing 'db%02d' gpio. Aborting.\n", i);
1133 /* returns 0 if the property is not present */
1134 static u32 fbtft_property_value(struct device *dev, const char *propname)
1139 ret = device_property_read_u32(dev, propname, &val);
1141 dev_info(dev, "%s: %s = %u\n", __func__, propname, val);
1146 static struct fbtft_platform_data *fbtft_properties_read(struct device *dev)
1148 struct fbtft_platform_data *pdata;
1150 if (!dev_fwnode(dev)) {
1151 dev_err(dev, "Missing platform data or properties\n");
1152 return ERR_PTR(-EINVAL);
1155 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1157 return ERR_PTR(-ENOMEM);
1159 pdata->display.width = fbtft_property_value(dev, "width");
1160 pdata->display.height = fbtft_property_value(dev, "height");
1161 pdata->display.regwidth = fbtft_property_value(dev, "regwidth");
1162 pdata->display.buswidth = fbtft_property_value(dev, "buswidth");
1163 pdata->display.backlight = fbtft_property_value(dev, "backlight");
1164 pdata->display.bpp = fbtft_property_value(dev, "bpp");
1165 pdata->display.debug = fbtft_property_value(dev, "debug");
1166 pdata->rotate = fbtft_property_value(dev, "rotate");
1167 pdata->bgr = device_property_read_bool(dev, "bgr");
1168 pdata->fps = fbtft_property_value(dev, "fps");
1169 pdata->txbuflen = fbtft_property_value(dev, "txbuflen");
1170 pdata->startbyte = fbtft_property_value(dev, "startbyte");
1171 device_property_read_string(dev, "gamma", (const char **)&pdata->gamma);
1173 if (device_property_present(dev, "led-gpios"))
1174 pdata->display.backlight = 1;
1175 if (device_property_present(dev, "init"))
1176 pdata->display.fbtftops.init_display =
1177 fbtft_init_display_from_property;
1179 pdata->display.fbtftops.request_gpios = fbtft_request_gpios;
1185 * fbtft_probe_common() - Generic device probe() helper function
1186 * @display: Display properties
1188 * @pdev: Platform device
1189 * @dt_ids: Compatible string table
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,
1200 const struct of_device_id *dt_ids)
1203 struct fb_info *info;
1204 struct fbtft_par *par;
1205 struct fbtft_platform_data *pdata;
1206 const struct of_device_id *match;
1207 int (*variant)(struct fbtft_display *);
1215 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1216 dev_info(dev, "%s()\n", __func__);
1218 pdata = dev->platform_data;
1220 pdata = fbtft_properties_read(dev);
1222 return PTR_ERR(pdata);
1223 match = of_match_device(dt_ids, dev);
1224 if (match && match->data) {
1225 /* apply the variant */
1226 variant = match->data;
1227 ret = (*variant)(display);
1233 info = fbtft_framebuffer_alloc(display, dev, pdata);
1241 if (display->buswidth == 0) {
1242 dev_err(dev, "buswidth is not set\n");
1246 /* write register functions */
1247 if (display->regwidth == 8 && display->buswidth == 8)
1248 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1249 else if (display->regwidth == 8 && display->buswidth == 9 && par->spi)
1250 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1251 else if (display->regwidth == 16 && display->buswidth == 8)
1252 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1253 else if (display->regwidth == 16 && display->buswidth == 16)
1254 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1257 "no default functions for regwidth=%d and buswidth=%d\n",
1258 display->regwidth, display->buswidth);
1260 /* write_vmem() functions */
1261 if (display->buswidth == 8)
1262 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1263 else if (display->buswidth == 9)
1264 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1265 else if (display->buswidth == 16)
1266 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1268 /* GPIO write() functions */
1270 if (display->buswidth == 8)
1271 par->fbtftops.write = fbtft_write_gpio8_wr;
1272 else if (display->buswidth == 16)
1273 par->fbtftops.write = fbtft_write_gpio16_wr;
1276 /* 9-bit SPI setup */
1277 if (par->spi && display->buswidth == 9) {
1278 if (par->spi->master->bits_per_word_mask & SPI_BPW_MASK(9)) {
1279 par->spi->bits_per_word = 9;
1281 dev_warn(&par->spi->dev,
1282 "9-bit SPI not available, emulating using 8-bit.\n");
1283 /* allocate buffer with room for dc bits */
1284 par->extra = devm_kzalloc(par->info->device,
1286 (par->txbuf.len / 8) + 8,
1292 par->fbtftops.write = fbtft_write_spi_emulate_9;
1296 if (!par->fbtftops.verify_gpios)
1297 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1299 /* make sure we still use the driver provided functions */
1300 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1302 /* use init_sequence if provided */
1303 if (par->init_sequence)
1304 par->fbtftops.init_display = fbtft_init_display;
1306 /* use platform_data provided functions above all */
1307 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1309 ret = fbtft_register_framebuffer(info);
1316 fbtft_framebuffer_release(info);
1320 EXPORT_SYMBOL(fbtft_probe_common);
1323 * fbtft_remove_common() - Generic device remove() helper function
1325 * @info: Framebuffer
1327 * Unregisters and releases the framebuffer
1329 * Return: 0 if successful, negative if error
1331 int fbtft_remove_common(struct device *dev, struct fb_info *info)
1333 struct fbtft_par *par;
1339 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1340 "%s()\n", __func__);
1341 fbtft_unregister_framebuffer(info);
1342 fbtft_framebuffer_release(info);
1346 EXPORT_SYMBOL(fbtft_remove_common);
1348 MODULE_LICENSE("GPL");