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