1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2013 Noralf Tronnes 4 * 5 * This driver is inspired by: 6 * st7735fb.c, Copyright (C) 2011, Matt Porter 7 * broadsheetfb.c, Copyright (C) 2008, Jaya Kumar 8 */ 9 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/errno.h> 13 #include <linux/string.h> 14 #include <linux/mm.h> 15 #include <linux/vmalloc.h> 16 #include <linux/slab.h> 17 #include <linux/init.h> 18 #include <linux/fb.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/spinlock.h> 26 #include <linux/of.h> 27 #include <video/mipi_display.h> 28 29 #include "fbtft.h" 30 #include "internal.h" 31 32 static unsigned long debug; 33 module_param(debug, ulong, 0000); 34 MODULE_PARM_DESC(debug, "override device debug level"); 35 36 int fbtft_write_buf_dc(struct fbtft_par *par, void *buf, size_t len, int dc) 37 { 38 int ret; 39 40 if (par->gpio.dc) 41 gpiod_set_value(par->gpio.dc, dc); 42 43 ret = par->fbtftops.write(par, buf, len); 44 if (ret < 0) 45 dev_err(par->info->device, 46 "write() failed and returned %d\n", ret); 47 return ret; 48 } 49 EXPORT_SYMBOL(fbtft_write_buf_dc); 50 51 void fbtft_dbg_hex(const struct device *dev, int groupsize, 52 void *buf, size_t len, const char *fmt, ...) 53 { 54 va_list args; 55 static char textbuf[512]; 56 char *text = textbuf; 57 size_t text_len; 58 59 va_start(args, fmt); 60 text_len = vscnprintf(text, sizeof(textbuf), fmt, args); 61 va_end(args); 62 63 hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len, 64 512 - text_len, false); 65 66 if (len > 32) 67 dev_info(dev, "%s ...\n", text); 68 else 69 dev_info(dev, "%s\n", text); 70 } 71 EXPORT_SYMBOL(fbtft_dbg_hex); 72 73 #ifdef CONFIG_OF 74 static int fbtft_request_one_gpio(struct fbtft_par *par, 75 const char *name, int index, 76 struct gpio_desc **gpiop) 77 { 78 struct device *dev = par->info->device; 79 struct device_node *node = dev->of_node; 80 int ret = 0; 81 82 if (of_find_property(node, name, NULL)) { 83 *gpiop = devm_gpiod_get_index(dev, dev->driver->name, index, 84 GPIOD_OUT_HIGH); 85 if (IS_ERR(*gpiop)) { 86 ret = PTR_ERR(*gpiop); 87 dev_err(dev, 88 "Failed to request %s GPIO:%d\n", name, ret); 89 return ret; 90 } 91 fbtft_par_dbg(DEBUG_REQUEST_GPIOS, par, "%s: '%s' GPIO\n", 92 __func__, name); 93 } 94 95 return ret; 96 } 97 98 static int fbtft_request_gpios_dt(struct fbtft_par *par) 99 { 100 int i; 101 int ret; 102 103 if (!par->info->device->of_node) 104 return -EINVAL; 105 106 ret = fbtft_request_one_gpio(par, "reset-gpios", 0, &par->gpio.reset); 107 if (ret) 108 return ret; 109 ret = fbtft_request_one_gpio(par, "dc-gpios", 0, &par->gpio.dc); 110 if (ret) 111 return ret; 112 ret = fbtft_request_one_gpio(par, "rd-gpios", 0, &par->gpio.rd); 113 if (ret) 114 return ret; 115 ret = fbtft_request_one_gpio(par, "wr-gpios", 0, &par->gpio.wr); 116 if (ret) 117 return ret; 118 ret = fbtft_request_one_gpio(par, "cs-gpios", 0, &par->gpio.cs); 119 if (ret) 120 return ret; 121 ret = fbtft_request_one_gpio(par, "latch-gpios", 0, &par->gpio.latch); 122 if (ret) 123 return ret; 124 for (i = 0; i < 16; i++) { 125 ret = fbtft_request_one_gpio(par, "db-gpios", i, 126 &par->gpio.db[i]); 127 if (ret) 128 return ret; 129 ret = fbtft_request_one_gpio(par, "led-gpios", i, 130 &par->gpio.led[i]); 131 if (ret) 132 return ret; 133 ret = fbtft_request_one_gpio(par, "aux-gpios", i, 134 &par->gpio.aux[i]); 135 if (ret) 136 return ret; 137 } 138 139 return 0; 140 } 141 #endif 142 143 #ifdef CONFIG_FB_BACKLIGHT 144 static int fbtft_backlight_update_status(struct backlight_device *bd) 145 { 146 struct fbtft_par *par = bl_get_data(bd); 147 bool polarity = par->polarity; 148 149 fbtft_par_dbg(DEBUG_BACKLIGHT, par, 150 "%s: polarity=%d, power=%d, fb_blank=%d\n", 151 __func__, polarity, bd->props.power, bd->props.fb_blank); 152 153 if ((bd->props.power == FB_BLANK_UNBLANK) && 154 (bd->props.fb_blank == FB_BLANK_UNBLANK)) 155 gpiod_set_value(par->gpio.led[0], polarity); 156 else 157 gpiod_set_value(par->gpio.led[0], !polarity); 158 159 return 0; 160 } 161 162 static int fbtft_backlight_get_brightness(struct backlight_device *bd) 163 { 164 return bd->props.brightness; 165 } 166 167 void fbtft_unregister_backlight(struct fbtft_par *par) 168 { 169 if (par->info->bl_dev) { 170 par->info->bl_dev->props.power = FB_BLANK_POWERDOWN; 171 backlight_update_status(par->info->bl_dev); 172 backlight_device_unregister(par->info->bl_dev); 173 par->info->bl_dev = NULL; 174 } 175 } 176 177 static const struct backlight_ops fbtft_bl_ops = { 178 .get_brightness = fbtft_backlight_get_brightness, 179 .update_status = fbtft_backlight_update_status, 180 }; 181 182 void fbtft_register_backlight(struct fbtft_par *par) 183 { 184 struct backlight_device *bd; 185 struct backlight_properties bl_props = { 0, }; 186 187 if (!par->gpio.led[0]) { 188 fbtft_par_dbg(DEBUG_BACKLIGHT, par, 189 "%s(): led pin not set, exiting.\n", __func__); 190 return; 191 } 192 193 bl_props.type = BACKLIGHT_RAW; 194 /* Assume backlight is off, get polarity from current state of pin */ 195 bl_props.power = FB_BLANK_POWERDOWN; 196 if (!gpiod_get_value(par->gpio.led[0])) 197 par->polarity = true; 198 199 bd = backlight_device_register(dev_driver_string(par->info->device), 200 par->info->device, par, 201 &fbtft_bl_ops, &bl_props); 202 if (IS_ERR(bd)) { 203 dev_err(par->info->device, 204 "cannot register backlight device (%ld)\n", 205 PTR_ERR(bd)); 206 return; 207 } 208 par->info->bl_dev = bd; 209 210 if (!par->fbtftops.unregister_backlight) 211 par->fbtftops.unregister_backlight = fbtft_unregister_backlight; 212 } 213 #else 214 void fbtft_register_backlight(struct fbtft_par *par) { }; 215 void fbtft_unregister_backlight(struct fbtft_par *par) { }; 216 #endif 217 EXPORT_SYMBOL(fbtft_register_backlight); 218 EXPORT_SYMBOL(fbtft_unregister_backlight); 219 220 static void fbtft_set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, 221 int ye) 222 { 223 write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS, 224 (xs >> 8) & 0xFF, xs & 0xFF, (xe >> 8) & 0xFF, xe & 0xFF); 225 226 write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS, 227 (ys >> 8) & 0xFF, ys & 0xFF, (ye >> 8) & 0xFF, ye & 0xFF); 228 229 write_reg(par, MIPI_DCS_WRITE_MEMORY_START); 230 } 231 232 static void fbtft_reset(struct fbtft_par *par) 233 { 234 if (!par->gpio.reset) 235 return; 236 fbtft_par_dbg(DEBUG_RESET, par, "%s()\n", __func__); 237 gpiod_set_value_cansleep(par->gpio.reset, 0); 238 usleep_range(20, 40); 239 gpiod_set_value_cansleep(par->gpio.reset, 1); 240 msleep(120); 241 } 242 243 static void fbtft_update_display(struct fbtft_par *par, unsigned int start_line, 244 unsigned int end_line) 245 { 246 size_t offset, len; 247 ktime_t ts_start, ts_end; 248 long fps, throughput; 249 bool timeit = false; 250 int ret = 0; 251 252 if (unlikely(par->debug & (DEBUG_TIME_FIRST_UPDATE | 253 DEBUG_TIME_EACH_UPDATE))) { 254 if ((par->debug & DEBUG_TIME_EACH_UPDATE) || 255 ((par->debug & DEBUG_TIME_FIRST_UPDATE) && 256 !par->first_update_done)) { 257 ts_start = ktime_get(); 258 timeit = true; 259 } 260 } 261 262 /* Sanity checks */ 263 if (start_line > end_line) { 264 dev_warn(par->info->device, 265 "%s: start_line=%u is larger than end_line=%u. Shouldn't happen, will do full display update\n", 266 __func__, start_line, end_line); 267 start_line = 0; 268 end_line = par->info->var.yres - 1; 269 } 270 if (start_line > par->info->var.yres - 1 || 271 end_line > par->info->var.yres - 1) { 272 dev_warn(par->info->device, 273 "%s: start_line=%u or end_line=%u is larger than max=%d. Shouldn't happen, will do full display update\n", 274 __func__, start_line, 275 end_line, par->info->var.yres - 1); 276 start_line = 0; 277 end_line = par->info->var.yres - 1; 278 } 279 280 fbtft_par_dbg(DEBUG_UPDATE_DISPLAY, par, "%s(start_line=%u, end_line=%u)\n", 281 __func__, start_line, end_line); 282 283 if (par->fbtftops.set_addr_win) 284 par->fbtftops.set_addr_win(par, 0, start_line, 285 par->info->var.xres - 1, end_line); 286 287 offset = start_line * par->info->fix.line_length; 288 len = (end_line - start_line + 1) * par->info->fix.line_length; 289 ret = par->fbtftops.write_vmem(par, offset, len); 290 if (ret < 0) 291 dev_err(par->info->device, 292 "%s: write_vmem failed to update display buffer\n", 293 __func__); 294 295 if (unlikely(timeit)) { 296 ts_end = ktime_get(); 297 if (!ktime_to_ns(par->update_time)) 298 par->update_time = ts_start; 299 300 fps = ktime_us_delta(ts_start, par->update_time); 301 par->update_time = ts_start; 302 fps = fps ? 1000000 / fps : 0; 303 304 throughput = ktime_us_delta(ts_end, ts_start); 305 throughput = throughput ? (len * 1000) / throughput : 0; 306 throughput = throughput * 1000 / 1024; 307 308 dev_info(par->info->device, 309 "Display update: %ld kB/s, fps=%ld\n", 310 throughput, fps); 311 par->first_update_done = true; 312 } 313 } 314 315 static void fbtft_mkdirty(struct fb_info *info, int y, int height) 316 { 317 struct fbtft_par *par = info->par; 318 struct fb_deferred_io *fbdefio = info->fbdefio; 319 320 /* special case, needed ? */ 321 if (y == -1) { 322 y = 0; 323 height = info->var.yres - 1; 324 } 325 326 /* Mark display lines/area as dirty */ 327 spin_lock(&par->dirty_lock); 328 if (y < par->dirty_lines_start) 329 par->dirty_lines_start = y; 330 if (y + height - 1 > par->dirty_lines_end) 331 par->dirty_lines_end = y + height - 1; 332 spin_unlock(&par->dirty_lock); 333 334 /* Schedule deferred_io to update display (no-op if already on queue)*/ 335 schedule_delayed_work(&info->deferred_work, fbdefio->delay); 336 } 337 338 static void fbtft_deferred_io(struct fb_info *info, struct list_head *pagelist) 339 { 340 struct fbtft_par *par = info->par; 341 unsigned int dirty_lines_start, dirty_lines_end; 342 struct page *page; 343 unsigned long index; 344 unsigned int y_low = 0, y_high = 0; 345 int count = 0; 346 347 spin_lock(&par->dirty_lock); 348 dirty_lines_start = par->dirty_lines_start; 349 dirty_lines_end = par->dirty_lines_end; 350 /* set display line markers as clean */ 351 par->dirty_lines_start = par->info->var.yres - 1; 352 par->dirty_lines_end = 0; 353 spin_unlock(&par->dirty_lock); 354 355 /* Mark display lines as dirty */ 356 list_for_each_entry(page, pagelist, lru) { 357 count++; 358 index = page->index << PAGE_SHIFT; 359 y_low = index / info->fix.line_length; 360 y_high = (index + PAGE_SIZE - 1) / info->fix.line_length; 361 dev_dbg(info->device, 362 "page->index=%lu y_low=%d y_high=%d\n", 363 page->index, y_low, y_high); 364 if (y_high > info->var.yres - 1) 365 y_high = info->var.yres - 1; 366 if (y_low < dirty_lines_start) 367 dirty_lines_start = y_low; 368 if (y_high > dirty_lines_end) 369 dirty_lines_end = y_high; 370 } 371 372 par->fbtftops.update_display(info->par, 373 dirty_lines_start, dirty_lines_end); 374 } 375 376 static void fbtft_fb_fillrect(struct fb_info *info, 377 const struct fb_fillrect *rect) 378 { 379 struct fbtft_par *par = info->par; 380 381 dev_dbg(info->dev, 382 "%s: dx=%d, dy=%d, width=%d, height=%d\n", 383 __func__, rect->dx, rect->dy, rect->width, rect->height); 384 sys_fillrect(info, rect); 385 386 par->fbtftops.mkdirty(info, rect->dy, rect->height); 387 } 388 389 static void fbtft_fb_copyarea(struct fb_info *info, 390 const struct fb_copyarea *area) 391 { 392 struct fbtft_par *par = info->par; 393 394 dev_dbg(info->dev, 395 "%s: dx=%d, dy=%d, width=%d, height=%d\n", 396 __func__, area->dx, area->dy, area->width, area->height); 397 sys_copyarea(info, area); 398 399 par->fbtftops.mkdirty(info, area->dy, area->height); 400 } 401 402 static void fbtft_fb_imageblit(struct fb_info *info, 403 const struct fb_image *image) 404 { 405 struct fbtft_par *par = info->par; 406 407 dev_dbg(info->dev, 408 "%s: dx=%d, dy=%d, width=%d, height=%d\n", 409 __func__, image->dx, image->dy, image->width, image->height); 410 sys_imageblit(info, image); 411 412 par->fbtftops.mkdirty(info, image->dy, image->height); 413 } 414 415 static ssize_t fbtft_fb_write(struct fb_info *info, const char __user *buf, 416 size_t count, loff_t *ppos) 417 { 418 struct fbtft_par *par = info->par; 419 ssize_t res; 420 421 dev_dbg(info->dev, 422 "%s: count=%zd, ppos=%llu\n", __func__, count, *ppos); 423 res = fb_sys_write(info, buf, count, ppos); 424 425 /* TODO: only mark changed area update all for now */ 426 par->fbtftops.mkdirty(info, -1, 0); 427 428 return res; 429 } 430 431 /* from pxafb.c */ 432 static unsigned int chan_to_field(unsigned int chan, struct fb_bitfield *bf) 433 { 434 chan &= 0xffff; 435 chan >>= 16 - bf->length; 436 return chan << bf->offset; 437 } 438 439 static int fbtft_fb_setcolreg(unsigned int regno, unsigned int red, 440 unsigned int green, unsigned int blue, 441 unsigned int transp, struct fb_info *info) 442 { 443 unsigned int val; 444 int ret = 1; 445 446 dev_dbg(info->dev, 447 "%s(regno=%u, red=0x%X, green=0x%X, blue=0x%X, trans=0x%X)\n", 448 __func__, regno, red, green, blue, transp); 449 450 switch (info->fix.visual) { 451 case FB_VISUAL_TRUECOLOR: 452 if (regno < 16) { 453 u32 *pal = info->pseudo_palette; 454 455 val = chan_to_field(red, &info->var.red); 456 val |= chan_to_field(green, &info->var.green); 457 val |= chan_to_field(blue, &info->var.blue); 458 459 pal[regno] = val; 460 ret = 0; 461 } 462 break; 463 } 464 return ret; 465 } 466 467 static int fbtft_fb_blank(int blank, struct fb_info *info) 468 { 469 struct fbtft_par *par = info->par; 470 int ret = -EINVAL; 471 472 dev_dbg(info->dev, "%s(blank=%d)\n", 473 __func__, blank); 474 475 if (!par->fbtftops.blank) 476 return ret; 477 478 switch (blank) { 479 case FB_BLANK_POWERDOWN: 480 case FB_BLANK_VSYNC_SUSPEND: 481 case FB_BLANK_HSYNC_SUSPEND: 482 case FB_BLANK_NORMAL: 483 ret = par->fbtftops.blank(par, true); 484 break; 485 case FB_BLANK_UNBLANK: 486 ret = par->fbtftops.blank(par, false); 487 break; 488 } 489 return ret; 490 } 491 492 static void fbtft_merge_fbtftops(struct fbtft_ops *dst, struct fbtft_ops *src) 493 { 494 if (src->write) 495 dst->write = src->write; 496 if (src->read) 497 dst->read = src->read; 498 if (src->write_vmem) 499 dst->write_vmem = src->write_vmem; 500 if (src->write_register) 501 dst->write_register = src->write_register; 502 if (src->set_addr_win) 503 dst->set_addr_win = src->set_addr_win; 504 if (src->reset) 505 dst->reset = src->reset; 506 if (src->mkdirty) 507 dst->mkdirty = src->mkdirty; 508 if (src->update_display) 509 dst->update_display = src->update_display; 510 if (src->init_display) 511 dst->init_display = src->init_display; 512 if (src->blank) 513 dst->blank = src->blank; 514 if (src->request_gpios_match) 515 dst->request_gpios_match = src->request_gpios_match; 516 if (src->request_gpios) 517 dst->request_gpios = src->request_gpios; 518 if (src->verify_gpios) 519 dst->verify_gpios = src->verify_gpios; 520 if (src->register_backlight) 521 dst->register_backlight = src->register_backlight; 522 if (src->unregister_backlight) 523 dst->unregister_backlight = src->unregister_backlight; 524 if (src->set_var) 525 dst->set_var = src->set_var; 526 if (src->set_gamma) 527 dst->set_gamma = src->set_gamma; 528 } 529 530 /** 531 * fbtft_framebuffer_alloc - creates a new frame buffer info structure 532 * 533 * @display: pointer to structure describing the display 534 * @dev: pointer to the device for this fb, this can be NULL 535 * 536 * Creates a new frame buffer info structure. 537 * 538 * Also creates and populates the following structures: 539 * info->fbops 540 * info->fbdefio 541 * info->pseudo_palette 542 * par->fbtftops 543 * par->txbuf 544 * 545 * Returns the new structure, or NULL if an error occurred. 546 * 547 */ 548 struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display, 549 struct device *dev, 550 struct fbtft_platform_data *pdata) 551 { 552 struct fb_info *info; 553 struct fbtft_par *par; 554 struct fb_ops *fbops = NULL; 555 struct fb_deferred_io *fbdefio = NULL; 556 u8 *vmem = NULL; 557 void *txbuf = NULL; 558 void *buf = NULL; 559 unsigned int width; 560 unsigned int height; 561 int txbuflen = display->txbuflen; 562 unsigned int bpp = display->bpp; 563 unsigned int fps = display->fps; 564 int vmem_size; 565 const s16 *init_sequence = display->init_sequence; 566 char *gamma = display->gamma; 567 u32 *gamma_curves = NULL; 568 569 /* sanity check */ 570 if (display->gamma_num * display->gamma_len > 571 FBTFT_GAMMA_MAX_VALUES_TOTAL) { 572 dev_err(dev, "FBTFT_GAMMA_MAX_VALUES_TOTAL=%d is exceeded\n", 573 FBTFT_GAMMA_MAX_VALUES_TOTAL); 574 return NULL; 575 } 576 577 /* defaults */ 578 if (!fps) 579 fps = 20; 580 if (!bpp) 581 bpp = 16; 582 583 if (!pdata) { 584 dev_err(dev, "platform data is missing\n"); 585 return NULL; 586 } 587 588 /* override driver values? */ 589 if (pdata->fps) 590 fps = pdata->fps; 591 if (pdata->txbuflen) 592 txbuflen = pdata->txbuflen; 593 if (pdata->display.init_sequence) 594 init_sequence = pdata->display.init_sequence; 595 if (pdata->gamma) 596 gamma = pdata->gamma; 597 if (pdata->display.debug) 598 display->debug = pdata->display.debug; 599 if (pdata->display.backlight) 600 display->backlight = pdata->display.backlight; 601 if (pdata->display.width) 602 display->width = pdata->display.width; 603 if (pdata->display.height) 604 display->height = pdata->display.height; 605 if (pdata->display.buswidth) 606 display->buswidth = pdata->display.buswidth; 607 if (pdata->display.regwidth) 608 display->regwidth = pdata->display.regwidth; 609 610 display->debug |= debug; 611 fbtft_expand_debug_value(&display->debug); 612 613 switch (pdata->rotate) { 614 case 90: 615 case 270: 616 width = display->height; 617 height = display->width; 618 break; 619 default: 620 width = display->width; 621 height = display->height; 622 } 623 624 vmem_size = display->width * display->height * bpp / 8; 625 vmem = vzalloc(vmem_size); 626 if (!vmem) 627 goto alloc_fail; 628 629 fbops = devm_kzalloc(dev, sizeof(struct fb_ops), GFP_KERNEL); 630 if (!fbops) 631 goto alloc_fail; 632 633 fbdefio = devm_kzalloc(dev, sizeof(struct fb_deferred_io), GFP_KERNEL); 634 if (!fbdefio) 635 goto alloc_fail; 636 637 buf = devm_kzalloc(dev, 128, GFP_KERNEL); 638 if (!buf) 639 goto alloc_fail; 640 641 if (display->gamma_num && display->gamma_len) { 642 gamma_curves = devm_kcalloc(dev, 643 display->gamma_num * 644 display->gamma_len, 645 sizeof(gamma_curves[0]), 646 GFP_KERNEL); 647 if (!gamma_curves) 648 goto alloc_fail; 649 } 650 651 info = framebuffer_alloc(sizeof(struct fbtft_par), dev); 652 if (!info) 653 goto alloc_fail; 654 655 info->screen_buffer = vmem; 656 info->fbops = fbops; 657 info->fbdefio = fbdefio; 658 659 fbops->owner = dev->driver->owner; 660 fbops->fb_read = fb_sys_read; 661 fbops->fb_write = fbtft_fb_write; 662 fbops->fb_fillrect = fbtft_fb_fillrect; 663 fbops->fb_copyarea = fbtft_fb_copyarea; 664 fbops->fb_imageblit = fbtft_fb_imageblit; 665 fbops->fb_setcolreg = fbtft_fb_setcolreg; 666 fbops->fb_blank = fbtft_fb_blank; 667 668 fbdefio->delay = HZ / fps; 669 fbdefio->deferred_io = fbtft_deferred_io; 670 fb_deferred_io_init(info); 671 672 strncpy(info->fix.id, dev->driver->name, 16); 673 info->fix.type = FB_TYPE_PACKED_PIXELS; 674 info->fix.visual = FB_VISUAL_TRUECOLOR; 675 info->fix.xpanstep = 0; 676 info->fix.ypanstep = 0; 677 info->fix.ywrapstep = 0; 678 info->fix.line_length = width * bpp / 8; 679 info->fix.accel = FB_ACCEL_NONE; 680 info->fix.smem_len = vmem_size; 681 682 info->var.rotate = pdata->rotate; 683 info->var.xres = width; 684 info->var.yres = height; 685 info->var.xres_virtual = info->var.xres; 686 info->var.yres_virtual = info->var.yres; 687 info->var.bits_per_pixel = bpp; 688 info->var.nonstd = 1; 689 690 /* RGB565 */ 691 info->var.red.offset = 11; 692 info->var.red.length = 5; 693 info->var.green.offset = 5; 694 info->var.green.length = 6; 695 info->var.blue.offset = 0; 696 info->var.blue.length = 5; 697 info->var.transp.offset = 0; 698 info->var.transp.length = 0; 699 700 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB; 701 702 par = info->par; 703 par->info = info; 704 par->pdata = pdata; 705 par->debug = display->debug; 706 par->buf = buf; 707 spin_lock_init(&par->dirty_lock); 708 par->bgr = pdata->bgr; 709 par->startbyte = pdata->startbyte; 710 par->init_sequence = init_sequence; 711 par->gamma.curves = gamma_curves; 712 par->gamma.num_curves = display->gamma_num; 713 par->gamma.num_values = display->gamma_len; 714 mutex_init(&par->gamma.lock); 715 info->pseudo_palette = par->pseudo_palette; 716 717 if (par->gamma.curves && gamma) { 718 if (fbtft_gamma_parse_str(par, par->gamma.curves, gamma, 719 strlen(gamma))) 720 goto alloc_fail; 721 } 722 723 /* Transmit buffer */ 724 if (txbuflen == -1) 725 txbuflen = vmem_size + 2; /* add in case startbyte is used */ 726 if (txbuflen >= vmem_size + 2) 727 txbuflen = 0; 728 729 #ifdef __LITTLE_ENDIAN 730 if ((!txbuflen) && (bpp > 8)) 731 txbuflen = PAGE_SIZE; /* need buffer for byteswapping */ 732 #endif 733 734 if (txbuflen > 0) { 735 txbuf = devm_kzalloc(par->info->device, txbuflen, GFP_KERNEL); 736 if (!txbuf) 737 goto alloc_fail; 738 par->txbuf.buf = txbuf; 739 par->txbuf.len = txbuflen; 740 } 741 742 /* default fbtft operations */ 743 par->fbtftops.write = fbtft_write_spi; 744 par->fbtftops.read = fbtft_read_spi; 745 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8; 746 par->fbtftops.write_register = fbtft_write_reg8_bus8; 747 par->fbtftops.set_addr_win = fbtft_set_addr_win; 748 par->fbtftops.reset = fbtft_reset; 749 par->fbtftops.mkdirty = fbtft_mkdirty; 750 par->fbtftops.update_display = fbtft_update_display; 751 if (display->backlight) 752 par->fbtftops.register_backlight = fbtft_register_backlight; 753 754 /* use driver provided functions */ 755 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops); 756 757 return info; 758 759 alloc_fail: 760 vfree(vmem); 761 762 return NULL; 763 } 764 EXPORT_SYMBOL(fbtft_framebuffer_alloc); 765 766 /** 767 * fbtft_framebuffer_release - frees up all memory used by the framebuffer 768 * 769 * @info: frame buffer info structure 770 * 771 */ 772 void fbtft_framebuffer_release(struct fb_info *info) 773 { 774 fb_deferred_io_cleanup(info); 775 vfree(info->screen_buffer); 776 framebuffer_release(info); 777 } 778 EXPORT_SYMBOL(fbtft_framebuffer_release); 779 780 /** 781 * fbtft_register_framebuffer - registers a tft frame buffer device 782 * @fb_info: frame buffer info structure 783 * 784 * Sets SPI driverdata if needed 785 * Requests needed gpios. 786 * Initializes display 787 * Updates display. 788 * Registers a frame buffer device @fb_info. 789 * 790 * Returns negative errno on error, or zero for success. 791 * 792 */ 793 int fbtft_register_framebuffer(struct fb_info *fb_info) 794 { 795 int ret; 796 char text1[50] = ""; 797 char text2[50] = ""; 798 struct fbtft_par *par = fb_info->par; 799 struct spi_device *spi = par->spi; 800 801 /* sanity checks */ 802 if (!par->fbtftops.init_display) { 803 dev_err(fb_info->device, "missing fbtftops.init_display()\n"); 804 return -EINVAL; 805 } 806 807 if (spi) 808 spi_set_drvdata(spi, fb_info); 809 if (par->pdev) 810 platform_set_drvdata(par->pdev, fb_info); 811 812 ret = par->fbtftops.request_gpios(par); 813 if (ret < 0) 814 goto reg_fail; 815 816 if (par->fbtftops.verify_gpios) { 817 ret = par->fbtftops.verify_gpios(par); 818 if (ret < 0) 819 goto reg_fail; 820 } 821 822 ret = par->fbtftops.init_display(par); 823 if (ret < 0) 824 goto reg_fail; 825 if (par->fbtftops.set_var) { 826 ret = par->fbtftops.set_var(par); 827 if (ret < 0) 828 goto reg_fail; 829 } 830 831 /* update the entire display */ 832 par->fbtftops.update_display(par, 0, par->info->var.yres - 1); 833 834 if (par->fbtftops.set_gamma && par->gamma.curves) { 835 ret = par->fbtftops.set_gamma(par, par->gamma.curves); 836 if (ret) 837 goto reg_fail; 838 } 839 840 if (par->fbtftops.register_backlight) 841 par->fbtftops.register_backlight(par); 842 843 ret = register_framebuffer(fb_info); 844 if (ret < 0) 845 goto reg_fail; 846 847 fbtft_sysfs_init(par); 848 849 if (par->txbuf.buf && par->txbuf.len >= 1024) 850 sprintf(text1, ", %zu KiB buffer memory", par->txbuf.len >> 10); 851 if (spi) 852 sprintf(text2, ", spi%d.%d at %d MHz", spi->master->bus_num, 853 spi->chip_select, spi->max_speed_hz / 1000000); 854 dev_info(fb_info->dev, 855 "%s frame buffer, %dx%d, %d KiB video memory%s, fps=%lu%s\n", 856 fb_info->fix.id, fb_info->var.xres, fb_info->var.yres, 857 fb_info->fix.smem_len >> 10, text1, 858 HZ / fb_info->fbdefio->delay, text2); 859 860 #ifdef CONFIG_FB_BACKLIGHT 861 /* Turn on backlight if available */ 862 if (fb_info->bl_dev) { 863 fb_info->bl_dev->props.power = FB_BLANK_UNBLANK; 864 fb_info->bl_dev->ops->update_status(fb_info->bl_dev); 865 } 866 #endif 867 868 return 0; 869 870 reg_fail: 871 if (par->fbtftops.unregister_backlight) 872 par->fbtftops.unregister_backlight(par); 873 874 return ret; 875 } 876 EXPORT_SYMBOL(fbtft_register_framebuffer); 877 878 /** 879 * fbtft_unregister_framebuffer - releases a tft frame buffer device 880 * @fb_info: frame buffer info structure 881 * 882 * Frees SPI driverdata if needed 883 * Frees gpios. 884 * Unregisters frame buffer device. 885 * 886 */ 887 int fbtft_unregister_framebuffer(struct fb_info *fb_info) 888 { 889 struct fbtft_par *par = fb_info->par; 890 891 if (par->fbtftops.unregister_backlight) 892 par->fbtftops.unregister_backlight(par); 893 fbtft_sysfs_exit(par); 894 return unregister_framebuffer(fb_info); 895 } 896 EXPORT_SYMBOL(fbtft_unregister_framebuffer); 897 898 #ifdef CONFIG_OF 899 /** 900 * fbtft_init_display_dt() - Device Tree init_display() function 901 * @par: Driver data 902 * 903 * Return: 0 if successful, negative if error 904 */ 905 static int fbtft_init_display_dt(struct fbtft_par *par) 906 { 907 struct device_node *node = par->info->device->of_node; 908 struct property *prop; 909 const __be32 *p; 910 u32 val; 911 int buf[64], i, j; 912 913 if (!node) 914 return -EINVAL; 915 916 prop = of_find_property(node, "init", NULL); 917 p = of_prop_next_u32(prop, NULL, &val); 918 if (!p) 919 return -EINVAL; 920 921 par->fbtftops.reset(par); 922 if (!par->gpio.cs) 923 gpiod_set_value(par->gpio.cs, 0); /* Activate chip */ 924 925 while (p) { 926 if (val & FBTFT_OF_INIT_CMD) { 927 val &= 0xFFFF; 928 i = 0; 929 while (p && !(val & 0xFFFF0000)) { 930 if (i > 63) { 931 dev_err(par->info->device, 932 "%s: Maximum register values exceeded\n", 933 __func__); 934 return -EINVAL; 935 } 936 buf[i++] = val; 937 p = of_prop_next_u32(prop, p, &val); 938 } 939 /* make debug message */ 940 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, 941 "init: write_register:\n"); 942 for (j = 0; j < i; j++) 943 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, 944 "buf[%d] = %02X\n", j, buf[j]); 945 946 par->fbtftops.write_register(par, i, 947 buf[0], buf[1], buf[2], buf[3], 948 buf[4], buf[5], buf[6], buf[7], 949 buf[8], buf[9], buf[10], buf[11], 950 buf[12], buf[13], buf[14], buf[15], 951 buf[16], buf[17], buf[18], buf[19], 952 buf[20], buf[21], buf[22], buf[23], 953 buf[24], buf[25], buf[26], buf[27], 954 buf[28], buf[29], buf[30], buf[31], 955 buf[32], buf[33], buf[34], buf[35], 956 buf[36], buf[37], buf[38], buf[39], 957 buf[40], buf[41], buf[42], buf[43], 958 buf[44], buf[45], buf[46], buf[47], 959 buf[48], buf[49], buf[50], buf[51], 960 buf[52], buf[53], buf[54], buf[55], 961 buf[56], buf[57], buf[58], buf[59], 962 buf[60], buf[61], buf[62], buf[63]); 963 } else if (val & FBTFT_OF_INIT_DELAY) { 964 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, 965 "init: msleep(%u)\n", val & 0xFFFF); 966 msleep(val & 0xFFFF); 967 p = of_prop_next_u32(prop, p, &val); 968 } else { 969 dev_err(par->info->device, "illegal init value 0x%X\n", 970 val); 971 return -EINVAL; 972 } 973 } 974 975 return 0; 976 } 977 #endif 978 979 /** 980 * fbtft_init_display() - Generic init_display() function 981 * @par: Driver data 982 * 983 * Uses par->init_sequence to do the initialization 984 * 985 * Return: 0 if successful, negative if error 986 */ 987 int fbtft_init_display(struct fbtft_par *par) 988 { 989 int buf[64]; 990 char msg[128]; 991 char str[16]; 992 int i = 0; 993 int j; 994 995 /* sanity check */ 996 if (!par->init_sequence) { 997 dev_err(par->info->device, 998 "error: init_sequence is not set\n"); 999 return -EINVAL; 1000 } 1001 1002 /* make sure stop marker exists */ 1003 for (i = 0; i < FBTFT_MAX_INIT_SEQUENCE; i++) 1004 if (par->init_sequence[i] == -3) 1005 break; 1006 if (i == FBTFT_MAX_INIT_SEQUENCE) { 1007 dev_err(par->info->device, 1008 "missing stop marker at end of init sequence\n"); 1009 return -EINVAL; 1010 } 1011 1012 par->fbtftops.reset(par); 1013 if (!par->gpio.cs) 1014 gpiod_set_value(par->gpio.cs, 0); /* Activate chip */ 1015 1016 i = 0; 1017 while (i < FBTFT_MAX_INIT_SEQUENCE) { 1018 if (par->init_sequence[i] == -3) { 1019 /* done */ 1020 return 0; 1021 } 1022 if (par->init_sequence[i] >= 0) { 1023 dev_err(par->info->device, 1024 "missing delimiter at position %d\n", i); 1025 return -EINVAL; 1026 } 1027 if (par->init_sequence[i + 1] < 0) { 1028 dev_err(par->info->device, 1029 "missing value after delimiter %d at position %d\n", 1030 par->init_sequence[i], i); 1031 return -EINVAL; 1032 } 1033 switch (par->init_sequence[i]) { 1034 case -1: 1035 i++; 1036 /* make debug message */ 1037 strcpy(msg, ""); 1038 j = i + 1; 1039 while (par->init_sequence[j] >= 0) { 1040 sprintf(str, "0x%02X ", par->init_sequence[j]); 1041 strcat(msg, str); 1042 j++; 1043 } 1044 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, 1045 "init: write(0x%02X) %s\n", 1046 par->init_sequence[i], msg); 1047 1048 /* Write */ 1049 j = 0; 1050 while (par->init_sequence[i] >= 0) { 1051 if (j > 63) { 1052 dev_err(par->info->device, 1053 "%s: Maximum register values exceeded\n", 1054 __func__); 1055 return -EINVAL; 1056 } 1057 buf[j++] = par->init_sequence[i++]; 1058 } 1059 par->fbtftops.write_register(par, j, 1060 buf[0], buf[1], buf[2], buf[3], 1061 buf[4], buf[5], buf[6], buf[7], 1062 buf[8], buf[9], buf[10], buf[11], 1063 buf[12], buf[13], buf[14], buf[15], 1064 buf[16], buf[17], buf[18], buf[19], 1065 buf[20], buf[21], buf[22], buf[23], 1066 buf[24], buf[25], buf[26], buf[27], 1067 buf[28], buf[29], buf[30], buf[31], 1068 buf[32], buf[33], buf[34], buf[35], 1069 buf[36], buf[37], buf[38], buf[39], 1070 buf[40], buf[41], buf[42], buf[43], 1071 buf[44], buf[45], buf[46], buf[47], 1072 buf[48], buf[49], buf[50], buf[51], 1073 buf[52], buf[53], buf[54], buf[55], 1074 buf[56], buf[57], buf[58], buf[59], 1075 buf[60], buf[61], buf[62], buf[63]); 1076 break; 1077 case -2: 1078 i++; 1079 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, 1080 "init: mdelay(%d)\n", 1081 par->init_sequence[i]); 1082 mdelay(par->init_sequence[i++]); 1083 break; 1084 default: 1085 dev_err(par->info->device, 1086 "unknown delimiter %d at position %d\n", 1087 par->init_sequence[i], i); 1088 return -EINVAL; 1089 } 1090 } 1091 1092 dev_err(par->info->device, 1093 "%s: something is wrong. Shouldn't get here.\n", __func__); 1094 return -EINVAL; 1095 } 1096 EXPORT_SYMBOL(fbtft_init_display); 1097 1098 /** 1099 * fbtft_verify_gpios() - Generic verify_gpios() function 1100 * @par: Driver data 1101 * 1102 * Uses @spi, @pdev and @buswidth to determine which GPIOs is needed 1103 * 1104 * Return: 0 if successful, negative if error 1105 */ 1106 static int fbtft_verify_gpios(struct fbtft_par *par) 1107 { 1108 struct fbtft_platform_data *pdata = par->pdata; 1109 int i; 1110 1111 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__); 1112 1113 if (pdata->display.buswidth != 9 && par->startbyte == 0 && 1114 !par->gpio.dc) { 1115 dev_err(par->info->device, 1116 "Missing info about 'dc' gpio. Aborting.\n"); 1117 return -EINVAL; 1118 } 1119 1120 if (!par->pdev) 1121 return 0; 1122 1123 if (!par->gpio.wr) { 1124 dev_err(par->info->device, "Missing 'wr' gpio. Aborting.\n"); 1125 return -EINVAL; 1126 } 1127 for (i = 0; i < pdata->display.buswidth; i++) { 1128 if (!par->gpio.db[i]) { 1129 dev_err(par->info->device, 1130 "Missing 'db%02d' gpio. Aborting.\n", i); 1131 return -EINVAL; 1132 } 1133 } 1134 1135 return 0; 1136 } 1137 1138 #ifdef CONFIG_OF 1139 /* returns 0 if the property is not present */ 1140 static u32 fbtft_of_value(struct device_node *node, const char *propname) 1141 { 1142 int ret; 1143 u32 val = 0; 1144 1145 ret = of_property_read_u32(node, propname, &val); 1146 if (ret == 0) 1147 pr_info("%s: %s = %u\n", __func__, propname, val); 1148 1149 return val; 1150 } 1151 1152 static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev) 1153 { 1154 struct device_node *node = dev->of_node; 1155 struct fbtft_platform_data *pdata; 1156 1157 if (!node) { 1158 dev_err(dev, "Missing platform data or DT\n"); 1159 return ERR_PTR(-EINVAL); 1160 } 1161 1162 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 1163 if (!pdata) 1164 return ERR_PTR(-ENOMEM); 1165 1166 pdata->display.width = fbtft_of_value(node, "width"); 1167 pdata->display.height = fbtft_of_value(node, "height"); 1168 pdata->display.regwidth = fbtft_of_value(node, "regwidth"); 1169 pdata->display.buswidth = fbtft_of_value(node, "buswidth"); 1170 pdata->display.backlight = fbtft_of_value(node, "backlight"); 1171 pdata->display.bpp = fbtft_of_value(node, "bpp"); 1172 pdata->display.debug = fbtft_of_value(node, "debug"); 1173 pdata->rotate = fbtft_of_value(node, "rotate"); 1174 pdata->bgr = of_property_read_bool(node, "bgr"); 1175 pdata->fps = fbtft_of_value(node, "fps"); 1176 pdata->txbuflen = fbtft_of_value(node, "txbuflen"); 1177 pdata->startbyte = fbtft_of_value(node, "startbyte"); 1178 of_property_read_string(node, "gamma", (const char **)&pdata->gamma); 1179 1180 if (of_find_property(node, "led-gpios", NULL)) 1181 pdata->display.backlight = 1; 1182 if (of_find_property(node, "init", NULL)) 1183 pdata->display.fbtftops.init_display = fbtft_init_display_dt; 1184 pdata->display.fbtftops.request_gpios = fbtft_request_gpios_dt; 1185 1186 return pdata; 1187 } 1188 #else 1189 static struct fbtft_platform_data *fbtft_probe_dt(struct device *dev) 1190 { 1191 dev_err(dev, "Missing platform data\n"); 1192 return ERR_PTR(-EINVAL); 1193 } 1194 #endif 1195 1196 /** 1197 * fbtft_probe_common() - Generic device probe() helper function 1198 * @display: Display properties 1199 * @sdev: SPI device 1200 * @pdev: Platform device 1201 * 1202 * Allocates, initializes and registers a framebuffer 1203 * 1204 * Either @sdev or @pdev should be NULL 1205 * 1206 * Return: 0 if successful, negative if error 1207 */ 1208 int fbtft_probe_common(struct fbtft_display *display, 1209 struct spi_device *sdev, 1210 struct platform_device *pdev) 1211 { 1212 struct device *dev; 1213 struct fb_info *info; 1214 struct fbtft_par *par; 1215 struct fbtft_platform_data *pdata; 1216 int ret; 1217 1218 if (sdev) 1219 dev = &sdev->dev; 1220 else 1221 dev = &pdev->dev; 1222 1223 if (unlikely(display->debug & DEBUG_DRIVER_INIT_FUNCTIONS)) 1224 dev_info(dev, "%s()\n", __func__); 1225 1226 pdata = dev->platform_data; 1227 if (!pdata) { 1228 pdata = fbtft_probe_dt(dev); 1229 if (IS_ERR(pdata)) 1230 return PTR_ERR(pdata); 1231 } 1232 1233 info = fbtft_framebuffer_alloc(display, dev, pdata); 1234 if (!info) 1235 return -ENOMEM; 1236 1237 par = info->par; 1238 par->spi = sdev; 1239 par->pdev = pdev; 1240 1241 if (display->buswidth == 0) { 1242 dev_err(dev, "buswidth is not set\n"); 1243 return -EINVAL; 1244 } 1245 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; 1255 else 1256 dev_warn(dev, 1257 "no default functions for regwidth=%d and buswidth=%d\n", 1258 display->regwidth, display->buswidth); 1259 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; 1267 1268 /* GPIO write() functions */ 1269 if (par->pdev) { 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; 1274 } 1275 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; 1280 } else { 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, 1285 par->txbuf.len + 1286 (par->txbuf.len / 8) + 8, 1287 GFP_KERNEL); 1288 if (!par->extra) { 1289 ret = -ENOMEM; 1290 goto out_release; 1291 } 1292 par->fbtftops.write = fbtft_write_spi_emulate_9; 1293 } 1294 } 1295 1296 if (!par->fbtftops.verify_gpios) 1297 par->fbtftops.verify_gpios = fbtft_verify_gpios; 1298 1299 /* make sure we still use the driver provided functions */ 1300 fbtft_merge_fbtftops(&par->fbtftops, &display->fbtftops); 1301 1302 /* use init_sequence if provided */ 1303 if (par->init_sequence) 1304 par->fbtftops.init_display = fbtft_init_display; 1305 1306 /* use platform_data provided functions above all */ 1307 fbtft_merge_fbtftops(&par->fbtftops, &pdata->display.fbtftops); 1308 1309 ret = fbtft_register_framebuffer(info); 1310 if (ret < 0) 1311 goto out_release; 1312 1313 return 0; 1314 1315 out_release: 1316 fbtft_framebuffer_release(info); 1317 1318 return ret; 1319 } 1320 EXPORT_SYMBOL(fbtft_probe_common); 1321 1322 /** 1323 * fbtft_remove_common() - Generic device remove() helper function 1324 * @dev: Device 1325 * @info: Framebuffer 1326 * 1327 * Unregisters and releases the framebuffer 1328 * 1329 * Return: 0 if successful, negative if error 1330 */ 1331 int fbtft_remove_common(struct device *dev, struct fb_info *info) 1332 { 1333 struct fbtft_par *par; 1334 1335 if (!info) 1336 return -EINVAL; 1337 par = info->par; 1338 if (par) 1339 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par, 1340 "%s()\n", __func__); 1341 fbtft_unregister_framebuffer(info); 1342 fbtft_framebuffer_release(info); 1343 1344 return 0; 1345 } 1346 EXPORT_SYMBOL(fbtft_remove_common); 1347 1348 MODULE_LICENSE("GPL"); 1349