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 const 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 (!backlight_is_blank(bd))
141 gpiod_set_value(par->gpio.led[0], polarity);
143 gpiod_set_value(par->gpio.led[0], !polarity);
148 static int fbtft_backlight_get_brightness(struct backlight_device *bd)
150 return bd->props.brightness;
153 void fbtft_unregister_backlight(struct fbtft_par *par)
155 if (par->info->bl_dev) {
156 par->info->bl_dev->props.power = FB_BLANK_POWERDOWN;
157 backlight_update_status(par->info->bl_dev);
158 backlight_device_unregister(par->info->bl_dev);
159 par->info->bl_dev = NULL;
162 EXPORT_SYMBOL(fbtft_unregister_backlight);
164 static const struct backlight_ops fbtft_bl_ops = {
165 .get_brightness = fbtft_backlight_get_brightness,
166 .update_status = fbtft_backlight_update_status,
169 void fbtft_register_backlight(struct fbtft_par *par)
171 struct backlight_device *bd;
172 struct backlight_properties bl_props = { 0, };
174 if (!par->gpio.led[0]) {
175 fbtft_par_dbg(DEBUG_BACKLIGHT, par,
176 "%s(): led pin not set, exiting.\n", __func__);
180 bl_props.type = BACKLIGHT_RAW;
181 /* Assume backlight is off, get polarity from current state of pin */
182 bl_props.power = FB_BLANK_POWERDOWN;
183 if (!gpiod_get_value(par->gpio.led[0]))
184 par->polarity = true;
186 bd = backlight_device_register(dev_driver_string(par->info->device),
187 par->info->device, par,
188 &fbtft_bl_ops, &bl_props);
190 dev_err(par->info->device,
191 "cannot register backlight device (%ld)\n",
195 par->info->bl_dev = bd;
197 if (!par->fbtftops.unregister_backlight)
198 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
200 EXPORT_SYMBOL(fbtft_register_backlight);
202 static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe,
205 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
206 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF);
208 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
209 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF);
211 write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
214 static void fbtft_reset(struct fbtft_par *par)
216 if (!par->gpio.reset)
219 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__);
221 gpiod_set_value_cansleep(par->gpio.reset, 1);
222 usleep_range(20, 40);
223 gpiod_set_value_cansleep(par->gpio.reset, 0);
226 gpiod_set_value_cansleep(par->gpio.cs, 1); /* Activate chip */
229 static void fbtft_update_display(struct fbtft_par *par, unsigned int start_line,
230 unsigned int end_line)
233 ktime_t ts_start, ts_end;
234 long fps, throughput;
238 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE |
239 DEBUG_TIME_EACH_UPDATE))) {
240 if ((par->debug & DEBUG_TIME_EACH_UPDATE) ||
241 ((par->debug & DEBUG_TIME_FIRST_UPDATE) &&
242 !par->first_update_done)) {
243 ts_start = ktime_get();
249 if (start_line > end_line) {
250 dev_warn(par->info->device,
251 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n",
252 __func__, start_line, end_line);
254 end_line = par->info->var.yres - 1;
256 if (start_line > par->info->var.yres - 1 ||
257 end_line > par->info->var.yres - 1) {
258 dev_warn(par->info->device,
259 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n",
260 __func__, start_line,
261 end_line, par->info->var.yres - 1);
263 end_line = par->info->var.yres - 1;
266 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n",
267 __func__, start_line, end_line);
269 if (par->fbtftops.set_addr_win)
270 par->fbtftops.set_addr_win(par, 0, start_line,
271 par->info->var.xres - 1, end_line);
273 offset = start_line * par->info->fix.line_length;
274 len = (end_line - start_line + 1) * par->info->fix.line_length;
275 ret = par->fbtftops.write_vmem(par, offset, len);
277 dev_err(par->info->device,
278 "%s: write_vmem failed to update display buffer\n",
281 if (unlikely(timeit)) {
282 ts_end = ktime_get();
283 if (!ktime_to_ns(par->update_time))
284 par->update_time = ts_start;
286 fps = ktime_us_delta(ts_start, par->update_time);
287 par->update_time = ts_start;
288 fps = fps ? 1000000 / fps : 0;
290 throughput = ktime_us_delta(ts_end, ts_start);
291 throughput = throughput ? (len * 1000) / throughput : 0;
292 throughput = throughput * 1000 / 1024;
294 dev_info(par->info->device,
295 "Display update: %ld kB/s, fps=%ld\n",
297 par->first_update_done = true;
301 static void fbtft_mkdirty(struct fb_info *info, int y, int height)
303 struct fbtft_par *par = info->par;
304 struct fb_deferred_io *fbdefio = info->fbdefio;
306 /* special case, needed ? */
309 height = info->var.yres;
312 /* Mark display lines/area as dirty */
313 spin_lock(&par->dirty_lock);
314 if (y < par->dirty_lines_start)
315 par->dirty_lines_start = y;
316 if (y + height - 1 > par->dirty_lines_end)
317 par->dirty_lines_end = y + height - 1;
318 spin_unlock(&par->dirty_lock);
320 /* Schedule deferred_io to update display (no-op if already on queue)*/
321 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
324 static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagereflist)
326 struct fbtft_par *par = info->par;
327 unsigned int dirty_lines_start, dirty_lines_end;
328 struct fb_deferred_io_pageref *pageref;
329 unsigned int y_low = 0, y_high = 0;
332 spin_lock(&par->dirty_lock);
333 dirty_lines_start = par->dirty_lines_start;
334 dirty_lines_end = par->dirty_lines_end;
335 /* set display line markers as clean */
336 par->dirty_lines_start = par->info->var.yres - 1;
337 par->dirty_lines_end = 0;
338 spin_unlock(&par->dirty_lock);
340 /* Mark display lines as dirty */
341 list_for_each_entry(pageref, pagereflist, list) {
343 y_low = pageref->offset / info->fix.line_length;
344 y_high = (pageref->offset + PAGE_SIZE - 1) / info->fix.line_length;
345 dev_dbg(info->device,
346 "page->index=%lu y_low=%d y_high=%d\n",
347 pageref->page->index, y_low, y_high);
348 if (y_high > info->var.yres - 1)
349 y_high = info->var.yres - 1;
350 if (y_low < dirty_lines_start)
351 dirty_lines_start = y_low;
352 if (y_high > dirty_lines_end)
353 dirty_lines_end = y_high;
356 par->fbtftops.update_display(info->par,
357 dirty_lines_start, dirty_lines_end);
361 static unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf)
364 chan >>= 16 - bf->length;
365 return chan << bf->offset;
368 static int fbtft_fb_setcolreg(unsigned int regno, unsigned int red,
369 unsigned int green, unsigned int blue,
370 unsigned int transp, struct fb_info *info)
376 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n",
377 __func__, regno, red, green, blue, transp);
379 switch (info->fix.visual) {
380 case FB_VISUAL_TRUECOLOR:
382 u32 *pal = info->pseudo_palette;
384 val = chan_to_field(red, &info->var.red);
385 val |= chan_to_field(green, &info->var.green);
386 val |= chan_to_field(blue, &info->var.blue);
396 static int fbtft_fb_blank(int blank, struct fb_info *info)
398 struct fbtft_par *par = info->par;
401 dev_dbg(info->dev, "%s(blank=%d)\n",
404 if (!par->fbtftops.blank)
408 case FB_BLANK_POWERDOWN:
409 case FB_BLANK_VSYNC_SUSPEND:
410 case FB_BLANK_HSYNC_SUSPEND:
411 case FB_BLANK_NORMAL:
412 ret = par->fbtftops.blank(par, true);
414 case FB_BLANK_UNBLANK:
415 ret = par->fbtftops.blank(par, false);
421 static void fbtft_ops_damage_range(struct fb_info *info, off_t off, size_t len)
423 struct fbtft_par *par = info->par;
425 /* TODO: only mark changed area update all for now */
426 par->fbtftops.mkdirty(info, -1, 0);
429 static void fbtft_ops_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height)
431 struct fbtft_par *par = info->par;
433 par->fbtftops.mkdirty(info, y, height);
436 FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(fbtft_ops,
437 fbtft_ops_damage_range,
438 fbtft_ops_damage_area)
440 static const struct fb_ops fbtft_ops = {
441 .owner = THIS_MODULE,
442 FB_DEFAULT_DEFERRED_OPS(fbtft_ops),
443 .fb_setcolreg = fbtft_fb_setcolreg,
444 .fb_blank = fbtft_fb_blank,
447 static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src)
450 dst->write = src->write;
452 dst->read = src->read;
454 dst->write_vmem = src->write_vmem;
455 if (src->write_register)
456 dst->write_register = src->write_register;
457 if (src->set_addr_win)
458 dst->set_addr_win = src->set_addr_win;
460 dst->reset = src->reset;
462 dst->mkdirty = src->mkdirty;
463 if (src->update_display)
464 dst->update_display = src->update_display;
465 if (src->init_display)
466 dst->init_display = src->init_display;
468 dst->blank = src->blank;
469 if (src->request_gpios_match)
470 dst->request_gpios_match = src->request_gpios_match;
471 if (src->request_gpios)
472 dst->request_gpios = src->request_gpios;
473 if (src->verify_gpios)
474 dst->verify_gpios = src->verify_gpios;
475 if (src->register_backlight)
476 dst->register_backlight = src->register_backlight;
477 if (src->unregister_backlight)
478 dst->unregister_backlight = src->unregister_backlight;
480 dst->set_var = src->set_var;
482 dst->set_gamma = src->set_gamma;
486 * fbtft_framebuffer_alloc - creates a new frame buffer info structure
488 * @display: pointer to structure describing the display
489 * @dev: pointer to the device for this fb, this can be NULL
490 * @pdata: platform data for the display in use
492 * Creates a new frame buffer info structure.
494 * Also creates and populates the following structures:
496 * info->pseudo_palette
500 * Returns the new structure, or NULL if an error occurred.
503 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
505 struct fbtft_platform_data *pdata)
507 struct fb_info *info;
508 struct fbtft_par *par;
509 struct fb_deferred_io *fbdefio = NULL;
515 int txbuflen = display->txbuflen;
516 unsigned int bpp = display->bpp;
517 unsigned int fps = display->fps;
519 const s16 *init_sequence = display->init_sequence;
520 char *gamma = display->gamma;
521 u32 *gamma_curves = NULL;
524 if (display->gamma_num * display->gamma_len >
525 FBTFT_GAMMA_MAX_VALUES_TOTAL) {
526 dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n",
527 FBTFT_GAMMA_MAX_VALUES_TOTAL);
538 dev_err(dev, "platform data is missing\n");
542 /* override driver values? */
546 txbuflen = pdata->txbuflen;
547 if (pdata->display.init_sequence)
548 init_sequence = pdata->display.init_sequence;
550 gamma = pdata->gamma;
551 if (pdata->display.debug)
552 display->debug = pdata->display.debug;
553 if (pdata->display.backlight)
554 display->backlight = pdata->display.backlight;
555 if (pdata->display.width)
556 display->width = pdata->display.width;
557 if (pdata->display.height)
558 display->height = pdata->display.height;
559 if (pdata->display.buswidth)
560 display->buswidth = pdata->display.buswidth;
561 if (pdata->display.regwidth)
562 display->regwidth = pdata->display.regwidth;
564 display->debug |= debug;
565 fbtft_expand_debug_value(&display->debug);
567 switch (pdata->rotate) {
570 width = display->height;
571 height = display->width;
574 width = display->width;
575 height = display->height;
578 vmem_size = display->width * display->height * bpp / 8;
579 vmem = vzalloc(vmem_size);
583 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL);
587 buf = devm_kzalloc(dev, 128, GFP_KERNEL);
591 if (display->gamma_num && display->gamma_len) {
592 gamma_curves = devm_kcalloc(dev,
595 sizeof(gamma_curves[0]),
601 info = framebuffer_alloc(sizeof(struct fbtft_par), dev);
605 info->screen_buffer = vmem;
606 info->fbops = &fbtft_ops;
607 info->fbdefio = fbdefio;
609 fbdefio->delay = HZ / fps;
610 fbdefio->sort_pagereflist = true;
611 fbdefio->deferred_io = fbtft_deferred_io;
613 snprintf(info->fix.id, sizeof(info->fix.id), "%s", dev->driver->name);
614 info->fix.type = FB_TYPE_PACKED_PIXELS;
615 info->fix.visual = FB_VISUAL_TRUECOLOR;
616 info->fix.xpanstep = 0;
617 info->fix.ypanstep = 0;
618 info->fix.ywrapstep = 0;
619 info->fix.line_length = width * bpp / 8;
620 info->fix.accel = FB_ACCEL_NONE;
621 info->fix.smem_len = vmem_size;
622 fb_deferred_io_init(info);
624 info->var.rotate = pdata->rotate;
625 info->var.xres = width;
626 info->var.yres = height;
627 info->var.xres_virtual = info->var.xres;
628 info->var.yres_virtual = info->var.yres;
629 info->var.bits_per_pixel = bpp;
630 info->var.nonstd = 1;
633 info->var.red.offset = 11;
634 info->var.red.length = 5;
635 info->var.green.offset = 5;
636 info->var.green.length = 6;
637 info->var.blue.offset = 0;
638 info->var.blue.length = 5;
639 info->var.transp.offset = 0;
640 info->var.transp.length = 0;
642 info->flags = FBINFO_VIRTFB;
647 par->debug = display->debug;
649 spin_lock_init(&par->dirty_lock);
650 par->bgr = pdata->bgr;
651 par->startbyte = pdata->startbyte;
652 par->init_sequence = init_sequence;
653 par->gamma.curves = gamma_curves;
654 par->gamma.num_curves = display->gamma_num;
655 par->gamma.num_values = display->gamma_len;
656 mutex_init(&par->gamma.lock);
657 info->pseudo_palette = par->pseudo_palette;
659 if (par->gamma.curves && gamma) {
660 if (fbtft_gamma_parse_str(par, par->gamma.curves, gamma,
662 goto release_framebuf;
665 /* Transmit buffer */
667 txbuflen = vmem_size + 2; /* add in case startbyte is used */
668 if (txbuflen >= vmem_size + 2)
671 #ifdef __LITTLE_ENDIAN
672 if ((!txbuflen) && (bpp > 8))
673 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */
677 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL);
679 goto release_framebuf;
680 par->txbuf.buf = txbuf;
681 par->txbuf.len = txbuflen;
684 /* default fbtft operations */
685 par->fbtftops.write = fbtft_write_spi;
686 par->fbtftops.read = fbtft_read_spi;
687 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
688 par->fbtftops.write_register = fbtft_write_reg8_bus8;
689 par->fbtftops.set_addr_win = fbtft_set_addr_win;
690 par->fbtftops.reset = fbtft_reset;
691 par->fbtftops.mkdirty = fbtft_mkdirty;
692 par->fbtftops.update_display = fbtft_update_display;
693 if (display->backlight)
694 par->fbtftops.register_backlight = fbtft_register_backlight;
696 /* use driver provided functions */
697 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
702 framebuffer_release(info);
709 EXPORT_SYMBOL(fbtft_framebuffer_alloc);
712 * fbtft_framebuffer_release - frees up all memory used by the framebuffer
714 * @info: frame buffer info structure
717 void fbtft_framebuffer_release(struct fb_info *info)
719 fb_deferred_io_cleanup(info);
720 vfree(info->screen_buffer);
721 framebuffer_release(info);
723 EXPORT_SYMBOL(fbtft_framebuffer_release);
726 * fbtft_register_framebuffer - registers a tft frame buffer device
727 * @fb_info: frame buffer info structure
729 * Sets SPI driverdata if needed
730 * Requests needed gpios.
731 * Initializes display
733 * Registers a frame buffer device @fb_info.
735 * Returns negative errno on error, or zero for success.
738 int fbtft_register_framebuffer(struct fb_info *fb_info)
743 struct fbtft_par *par = fb_info->par;
744 struct spi_device *spi = par->spi;
747 if (!par->fbtftops.init_display) {
748 dev_err(fb_info->device, "missing fbtftops.init_display()\n");
753 spi_set_drvdata(spi, fb_info);
755 platform_set_drvdata(par->pdev, fb_info);
757 ret = par->fbtftops.request_gpios(par);
761 if (par->fbtftops.verify_gpios) {
762 ret = par->fbtftops.verify_gpios(par);
767 ret = par->fbtftops.init_display(par);
770 if (par->fbtftops.set_var) {
771 ret = par->fbtftops.set_var(par);
776 /* update the entire display */
777 par->fbtftops.update_display(par, 0, par->info->var.yres - 1);
779 if (par->fbtftops.set_gamma && par->gamma.curves) {
780 ret = par->fbtftops.set_gamma(par, par->gamma.curves);
785 if (par->fbtftops.register_backlight)
786 par->fbtftops.register_backlight(par);
788 ret = register_framebuffer(fb_info);
792 fbtft_sysfs_init(par);
794 if (par->txbuf.buf && par->txbuf.len >= 1024)
795 sprintf(text1, ", %zu KiB buffer memory", par->txbuf.len >> 10);
797 sprintf(text2, ", spi%d.%d at %d MHz", spi->controller->bus_num,
798 spi_get_chipselect(spi, 0), spi->max_speed_hz / 1000000);
799 dev_info(fb_info->dev,
800 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n",
801 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres,
802 fb_info->fix.smem_len >> 10, text1,
803 HZ / fb_info->fbdefio->delay, text2);
805 /* Turn on backlight if available */
806 if (fb_info->bl_dev) {
807 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK;
808 fb_info->bl_dev->ops->update_status(fb_info->bl_dev);
814 if (par->fbtftops.unregister_backlight)
815 par->fbtftops.unregister_backlight(par);
819 EXPORT_SYMBOL(fbtft_register_framebuffer);
822 * fbtft_unregister_framebuffer - releases a tft frame buffer device
823 * @fb_info: frame buffer info structure
825 * Frees SPI driverdata if needed
827 * Unregisters frame buffer device.
830 int fbtft_unregister_framebuffer(struct fb_info *fb_info)
832 struct fbtft_par *par = fb_info->par;
834 if (par->fbtftops.unregister_backlight)
835 par->fbtftops.unregister_backlight(par);
836 fbtft_sysfs_exit(par);
837 unregister_framebuffer(fb_info);
841 EXPORT_SYMBOL(fbtft_unregister_framebuffer);
844 * fbtft_init_display_from_property() - Device Tree init_display() function
847 * Return: 0 if successful, negative if error
849 static int fbtft_init_display_from_property(struct fbtft_par *par)
851 struct device *dev = par->info->device;
852 int buf[64], count, index, i, j, ret;
856 count = device_property_count_u32(dev, "init");
862 values = kmalloc_array(count + 1, sizeof(*values), GFP_KERNEL);
866 ret = device_property_read_u32_array(dev, "init", values, count);
870 par->fbtftops.reset(par);
873 val = values[++index];
875 while (index < count) {
876 if (val & FBTFT_OF_INIT_CMD) {
879 while ((index < count) && !(val & 0xFFFF0000)) {
882 "%s: Maximum register values exceeded\n",
888 val = values[++index];
890 /* make debug message */
891 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
892 "init: write_register:\n");
893 for (j = 0; j < i; j++)
894 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
895 "buf[%d] = %02X\n", j, buf[j]);
897 par->fbtftops.write_register(par, i,
898 buf[0], buf[1], buf[2], buf[3],
899 buf[4], buf[5], buf[6], buf[7],
900 buf[8], buf[9], buf[10], buf[11],
901 buf[12], buf[13], buf[14], buf[15],
902 buf[16], buf[17], buf[18], buf[19],
903 buf[20], buf[21], buf[22], buf[23],
904 buf[24], buf[25], buf[26], buf[27],
905 buf[28], buf[29], buf[30], buf[31],
906 buf[32], buf[33], buf[34], buf[35],
907 buf[36], buf[37], buf[38], buf[39],
908 buf[40], buf[41], buf[42], buf[43],
909 buf[44], buf[45], buf[46], buf[47],
910 buf[48], buf[49], buf[50], buf[51],
911 buf[52], buf[53], buf[54], buf[55],
912 buf[56], buf[57], buf[58], buf[59],
913 buf[60], buf[61], buf[62], buf[63]);
914 } else if (val & FBTFT_OF_INIT_DELAY) {
915 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
916 "init: msleep(%u)\n", val & 0xFFFF);
917 msleep(val & 0xFFFF);
918 val = values[++index];
920 dev_err(dev, "illegal init value 0x%X\n", val);
932 * fbtft_init_display() - Generic init_display() function
935 * Uses par->init_sequence to do the initialization
937 * Return: 0 if successful, negative if error
939 int fbtft_init_display(struct fbtft_par *par)
946 if (!par->init_sequence) {
947 dev_err(par->info->device,
948 "error: init_sequence is not set\n");
952 /* make sure stop marker exists */
953 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++) {
954 if (par->init_sequence[i] == -3)
958 if (i == FBTFT_MAX_INIT_SEQUENCE) {
959 dev_err(par->info->device,
960 "missing stop marker at end of init sequence\n");
964 par->fbtftops.reset(par);
967 while (i < FBTFT_MAX_INIT_SEQUENCE) {
968 if (par->init_sequence[i] == -3) {
972 if (par->init_sequence[i] >= 0) {
973 dev_err(par->info->device,
974 "missing delimiter at position %d\n", i);
977 if (par->init_sequence[i + 1] < 0) {
978 dev_err(par->info->device,
979 "missing value after delimiter %d at position %d\n",
980 par->init_sequence[i], i);
983 switch (par->init_sequence[i]) {
987 /* make debug message */
988 for (j = 0; par->init_sequence[i + 1 + j] >= 0; j++)
991 fbtft_par_dbg_hex(DEBUG_INIT_DISPLAY, par, par->info->device,
992 s16, &par->init_sequence[i + 1], j,
993 "init: write(0x%02X)", par->init_sequence[i]);
997 while (par->init_sequence[i] >= 0) {
999 dev_err(par->info->device,
1000 "%s: Maximum register values exceeded\n",
1004 buf[j++] = par->init_sequence[i++];
1006 par->fbtftops.write_register(par, j,
1007 buf[0], buf[1], buf[2], buf[3],
1008 buf[4], buf[5], buf[6], buf[7],
1009 buf[8], buf[9], buf[10], buf[11],
1010 buf[12], buf[13], buf[14], buf[15],
1011 buf[16], buf[17], buf[18], buf[19],
1012 buf[20], buf[21], buf[22], buf[23],
1013 buf[24], buf[25], buf[26], buf[27],
1014 buf[28], buf[29], buf[30], buf[31],
1015 buf[32], buf[33], buf[34], buf[35],
1016 buf[36], buf[37], buf[38], buf[39],
1017 buf[40], buf[41], buf[42], buf[43],
1018 buf[44], buf[45], buf[46], buf[47],
1019 buf[48], buf[49], buf[50], buf[51],
1020 buf[52], buf[53], buf[54], buf[55],
1021 buf[56], buf[57], buf[58], buf[59],
1022 buf[60], buf[61], buf[62], buf[63]);
1026 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
1027 "init: mdelay(%d)\n",
1028 par->init_sequence[i]);
1029 mdelay(par->init_sequence[i++]);
1032 dev_err(par->info->device,
1033 "unknown delimiter %d at position %d\n",
1034 par->init_sequence[i], i);
1039 dev_err(par->info->device,
1040 "%s: something is wrong. Shouldn't get here.\n", __func__);
1043 EXPORT_SYMBOL(fbtft_init_display);
1046 * fbtft_verify_gpios() - Generic verify_gpios() function
1049 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed
1051 * Return: 0 if successful, negative if error
1053 static int fbtft_verify_gpios(struct fbtft_par *par)
1055 struct fbtft_platform_data *pdata = par->pdata;
1058 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
1060 if (pdata->display.buswidth != 9 && par->startbyte == 0 &&
1062 dev_err(par->info->device,
1063 "Missing info about 'dc' gpio. Aborting.\n");
1070 if (!par->gpio.wr) {
1071 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n");
1074 for (i = 0; i < pdata->display.buswidth; i++) {
1075 if (!par->gpio.db[i]) {
1076 dev_err(par->info->device,
1077 "Missing 'db%02d' gpio. Aborting.\n", i);
1085 /* returns 0 if the property is not present */
1086 static u32 fbtft_property_value(struct device *dev, const char *propname)
1091 ret = device_property_read_u32(dev, propname, &val);
1093 dev_info(dev, "%s: %s = %u\n", __func__, propname, val);
1098 static struct fbtft_platform_data *fbtft_properties_read(struct device *dev)
1100 struct fbtft_platform_data *pdata;
1102 if (!dev_fwnode(dev)) {
1103 dev_err(dev, "Missing platform data or properties\n");
1104 return ERR_PTR(-EINVAL);
1107 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1109 return ERR_PTR(-ENOMEM);
1111 pdata->display.width = fbtft_property_value(dev, "width");
1112 pdata->display.height = fbtft_property_value(dev, "height");
1113 pdata->display.regwidth = fbtft_property_value(dev, "regwidth");
1114 pdata->display.buswidth = fbtft_property_value(dev, "buswidth");
1115 pdata->display.backlight = fbtft_property_value(dev, "backlight");
1116 pdata->display.bpp = fbtft_property_value(dev, "bpp");
1117 pdata->display.debug = fbtft_property_value(dev, "debug");
1118 pdata->rotate = fbtft_property_value(dev, "rotate");
1119 pdata->bgr = device_property_read_bool(dev, "bgr");
1120 pdata->fps = fbtft_property_value(dev, "fps");
1121 pdata->txbuflen = fbtft_property_value(dev, "txbuflen");
1122 pdata->startbyte = fbtft_property_value(dev, "startbyte");
1123 device_property_read_string(dev, "gamma", (const char **)&pdata->gamma);
1125 if (device_property_present(dev, "led-gpios"))
1126 pdata->display.backlight = 1;
1127 if (device_property_present(dev, "init"))
1128 pdata->display.fbtftops.init_display =
1129 fbtft_init_display_from_property;
1131 pdata->display.fbtftops.request_gpios = fbtft_request_gpios;
1137 * fbtft_probe_common() - Generic device probe() helper function
1138 * @display: Display properties
1140 * @pdev: Platform device
1142 * Allocates, initializes and registers a framebuffer
1144 * Either @sdev or @pdev should be NULL
1146 * Return: 0 if successful, negative if error
1148 int fbtft_probe_common(struct fbtft_display *display,
1149 struct spi_device *sdev,
1150 struct platform_device *pdev)
1153 struct fb_info *info;
1154 struct fbtft_par *par;
1155 struct fbtft_platform_data *pdata;
1163 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS))
1164 dev_info(dev, "%s()\n", __func__);
1166 pdata = dev->platform_data;
1168 pdata = fbtft_properties_read(dev);
1170 return PTR_ERR(pdata);
1173 info = fbtft_framebuffer_alloc(display, dev, pdata);
1181 if (display->buswidth == 0) {
1182 dev_err(dev, "buswidth is not set\n");
1186 /* write register functions */
1187 if (display->regwidth == 8 && display->buswidth == 8)
1188 par->fbtftops.write_register = fbtft_write_reg8_bus8;
1189 else if (display->regwidth == 8 && display->buswidth == 9 && par->spi)
1190 par->fbtftops.write_register = fbtft_write_reg8_bus9;
1191 else if (display->regwidth == 16 && display->buswidth == 8)
1192 par->fbtftops.write_register = fbtft_write_reg16_bus8;
1193 else if (display->regwidth == 16 && display->buswidth == 16)
1194 par->fbtftops.write_register = fbtft_write_reg16_bus16;
1197 "no default functions for regwidth=%d and buswidth=%d\n",
1198 display->regwidth, display->buswidth);
1200 /* write_vmem() functions */
1201 if (display->buswidth == 8)
1202 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
1203 else if (display->buswidth == 9)
1204 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
1205 else if (display->buswidth == 16)
1206 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
1208 /* GPIO write() functions */
1210 if (display->buswidth == 8)
1211 par->fbtftops.write = fbtft_write_gpio8_wr;
1212 else if (display->buswidth == 16)
1213 par->fbtftops.write = fbtft_write_gpio16_wr;
1216 /* 9-bit SPI setup */
1217 if (par->spi && display->buswidth == 9) {
1218 if (par->spi->controller->bits_per_word_mask & SPI_BPW_MASK(9)) {
1219 par->spi->bits_per_word = 9;
1221 dev_warn(&par->spi->dev,
1222 "9-bit SPI not available, emulating using 8-bit.\n");
1223 /* allocate buffer with room for dc bits */
1224 par->extra = devm_kzalloc(par->info->device,
1226 (par->txbuf.len / 8) + 8,
1232 par->fbtftops.write = fbtft_write_spi_emulate_9;
1236 if (!par->fbtftops.verify_gpios)
1237 par->fbtftops.verify_gpios = fbtft_verify_gpios;
1239 /* make sure we still use the driver provided functions */
1240 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops);
1242 /* use init_sequence if provided */
1243 if (par->init_sequence)
1244 par->fbtftops.init_display = fbtft_init_display;
1246 /* use platform_data provided functions above all */
1247 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops);
1249 ret = fbtft_register_framebuffer(info);
1256 fbtft_framebuffer_release(info);
1260 EXPORT_SYMBOL(fbtft_probe_common);
1263 * fbtft_remove_common() - Generic device remove() helper function
1265 * @info: Framebuffer
1267 * Unregisters and releases the framebuffer
1269 void fbtft_remove_common(struct device *dev, struct fb_info *info)
1271 struct fbtft_par *par;
1275 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par,
1276 "%s()\n", __func__);
1277 fbtft_unregister_framebuffer(info);
1278 fbtft_framebuffer_release(info);
1280 EXPORT_SYMBOL(fbtft_remove_common);
1282 MODULE_LICENSE("GPL");