1 // SPDX-License-Identifier: GPL-2.0+
3 * Generic FB driver for TFT LCD displays
5 * Copyright (C) 2013 Noralf Tronnes
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/vmalloc.h>
12 #include <linux/gpio.h>
13 #include <linux/spi/spi.h>
14 #include <linux/delay.h>
18 #define DRVNAME "flexfb"
21 module_param(chip, charp, 0000);
22 MODULE_PARM_DESC(chip, "LCD controller");
24 static unsigned int width;
25 module_param(width, uint, 0000);
26 MODULE_PARM_DESC(width, "Display width");
28 static unsigned int height;
29 module_param(height, uint, 0000);
30 MODULE_PARM_DESC(height, "Display height");
34 module_param_array(init, short, &init_num, 0000);
35 MODULE_PARM_DESC(init, "Init sequence");
37 static unsigned int setaddrwin;
38 module_param(setaddrwin, uint, 0000);
39 MODULE_PARM_DESC(setaddrwin, "Which set_addr_win() implementation to use");
41 static unsigned int buswidth = 8;
42 module_param(buswidth, uint, 0000);
43 MODULE_PARM_DESC(buswidth, "Width of databus (default: 8)");
45 static unsigned int regwidth = 8;
46 module_param(regwidth, uint, 0000);
47 MODULE_PARM_DESC(regwidth, "Width of controller register (default: 8)");
49 static bool nobacklight;
50 module_param(nobacklight, bool, 0000);
51 MODULE_PARM_DESC(nobacklight, "Turn off backlight functionality.");
54 module_param(latched, bool, 0000);
55 MODULE_PARM_DESC(latched, "Use with latched 16-bit databus");
57 static const s16 *initp;
60 /* default init sequences */
61 static const s16 st7735r_init[] = {
66 -1, 0xB1, 0x01, 0x2C, 0x2D,
67 -1, 0xB2, 0x01, 0x2C, 0x2D,
68 -1, 0xB3, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D,
70 -1, 0xC0, 0xA2, 0x02, 0x84,
79 -1, 0xE0, 0x0f, 0x1a, 0x0f, 0x18, 0x2f, 0x28, 0x20, 0x22,
80 0x1f, 0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10,
81 -1, 0xE1, 0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e,
82 0x30, 0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10,
90 static const s16 ssd1289_init[] = {
136 static const s16 hx8340bn_init[] = {
137 -1, 0xC1, 0xFF, 0x83, 0x40,
140 -1, 0xCA, 0x70, 0x00, 0xD9,
141 -1, 0xB0, 0x01, 0x11,
142 -1, 0xC9, 0x90, 0x49, 0x10, 0x28, 0x28, 0x10, 0x00, 0x06,
144 -1, 0xC2, 0x60, 0x71, 0x01, 0x0E, 0x05, 0x02, 0x09, 0x31, 0x0A,
145 -1, 0xC3, 0x67, 0x30, 0x61, 0x17, 0x48, 0x07, 0x05, 0x33,
147 -1, 0xB5, 0x35, 0x20, 0x45,
148 -1, 0xB4, 0x33, 0x25, 0x4C,
156 static const s16 ili9225_init[] = {
198 static const s16 ili9320_init[] = {
259 static const s16 ili9325_init[] = {
318 static const s16 ili9341_init[] = {
321 -1, 0xCF, 0x00, 0x83, 0x30,
322 -1, 0xED, 0x64, 0x03, 0x12, 0x81,
323 -1, 0xE8, 0x85, 0x01, 0x79,
324 -1, 0xCB, 0x39, 0x2c, 0x00, 0x34, 0x02,
326 -1, 0xEA, 0x00, 0x00,
329 -1, 0xC5, 0x35, 0x3E,
331 -1, 0xB1, 0x00, 0x1B,
332 -1, 0xB6, 0x0a, 0x82, 0x27, 0x00,
343 static const s16 ssd1351_init[] = {
350 -1, 0x15, 0x00, 0x7f,
351 -1, 0x75, 0x00, 0x7f,
357 -1, 0xb4, 0xa0, 0xb5, 0x55,
360 -1, 0xc1, 0xc8, 0x80, 0xc8,
369 * struct flexfb_lcd_controller - Describes the LCD controller properties
370 * @name: Model name of the chip
371 * @width: Width of display in pixels
372 * @height: Height of display in pixels
373 * @setaddrwin: Which set_addr_win() implementation to use
374 * @regwidth: LCD Controller Register width in bits
375 * @init_seq: LCD initialization sequence
376 * @init_seq_sz: Size of LCD initialization sequence
378 struct flexfb_lcd_controller {
382 unsigned int setaddrwin;
383 unsigned int regwidth;
388 static const struct flexfb_lcd_controller flexfb_chip_table[] = {
393 .init_seq = st7735r_init,
394 .init_seq_sz = ARRAY_SIZE(st7735r_init),
400 .init_seq = hx8340bn_init,
401 .init_seq_sz = ARRAY_SIZE(hx8340bn_init),
408 .init_seq = ili9225_init,
409 .init_seq_sz = ARRAY_SIZE(ili9225_init),
417 .init_seq = ili9320_init,
418 .init_seq_sz = ARRAY_SIZE(ili9320_init),
426 .init_seq = ili9325_init,
427 .init_seq_sz = ARRAY_SIZE(ili9325_init),
433 .init_seq = ili9341_init,
434 .init_seq_sz = ARRAY_SIZE(ili9341_init),
442 .init_seq = ssd1289_init,
443 .init_seq_sz = ARRAY_SIZE(ssd1289_init),
450 .init_seq = ssd1351_init,
451 .init_seq_sz = ARRAY_SIZE(ssd1351_init),
455 /* ili9320, ili9325 */
456 static void flexfb_set_addr_win_1(struct fbtft_par *par,
457 int xs, int ys, int xe, int ye)
459 switch (par->info->var.rotate) {
460 /* R20h = Horizontal GRAM Start Address */
461 /* R21h = Vertical GRAM Start Address */
463 write_reg(par, 0x0020, xs);
464 write_reg(par, 0x0021, ys);
467 write_reg(par, 0x0020, width - 1 - xs);
468 write_reg(par, 0x0021, height - 1 - ys);
471 write_reg(par, 0x0020, width - 1 - ys);
472 write_reg(par, 0x0021, xs);
475 write_reg(par, 0x0020, ys);
476 write_reg(par, 0x0021, height - 1 - xs);
479 write_reg(par, 0x0022); /* Write Data to GRAM */
483 static void flexfb_set_addr_win_2(struct fbtft_par *par,
484 int xs, int ys, int xe, int ye)
486 switch (par->info->var.rotate) {
487 /* R4Eh - Set GDDRAM X address counter */
488 /* R4Fh - Set GDDRAM Y address counter */
490 write_reg(par, 0x4e, xs);
491 write_reg(par, 0x4f, ys);
494 write_reg(par, 0x4e, par->info->var.xres - 1 - xs);
495 write_reg(par, 0x4f, par->info->var.yres - 1 - ys);
498 write_reg(par, 0x4e, par->info->var.yres - 1 - ys);
499 write_reg(par, 0x4f, xs);
502 write_reg(par, 0x4e, ys);
503 write_reg(par, 0x4f, par->info->var.xres - 1 - xs);
507 /* R22h - RAM data write */
508 write_reg(par, 0x22, 0);
512 static void set_addr_win_3(struct fbtft_par *par,
513 int xs, int ys, int xe, int ye)
515 write_reg(par, 0x15, xs, xe);
516 write_reg(par, 0x75, ys, ye);
517 write_reg(par, 0x5C);
520 static int flexfb_verify_gpios_dc(struct fbtft_par *par)
522 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
524 if (par->gpio.dc < 0) {
525 dev_err(par->info->device,
526 "Missing info about 'dc' gpio. Aborting.\n");
533 static int flexfb_verify_gpios_db(struct fbtft_par *par)
536 int num_db = buswidth;
538 fbtft_par_dbg(DEBUG_VERIFY_GPIOS, par, "%s()\n", __func__);
540 if (par->gpio.dc < 0) {
541 dev_err(par->info->device, "Missing info about 'dc' gpio. Aborting.\n");
544 if (par->gpio.wr < 0) {
545 dev_err(par->info->device, "Missing info about 'wr' gpio. Aborting.\n");
548 if (latched && (par->gpio.latch < 0)) {
549 dev_err(par->info->device, "Missing info about 'latch' gpio. Aborting.\n");
553 num_db = buswidth / 2;
554 for (i = 0; i < num_db; i++) {
555 if (par->gpio.db[i] < 0) {
556 dev_err(par->info->device,
557 "Missing info about 'db%02d' gpio. Aborting.\n",
566 static void flexfb_chip_load_param(const struct flexfb_lcd_controller *chip)
571 height = chip->height;
572 setaddrwin = chip->setaddrwin;
574 regwidth = chip->regwidth;
576 initp = chip->init_seq;
577 initp_num = chip->init_seq_sz;
581 static struct fbtft_display flex_display = { };
583 static int flexfb_chip_init(const struct device *dev)
587 for (i = 0; i < ARRAY_SIZE(flexfb_chip_table); i++)
588 if (!strcmp(chip, flexfb_chip_table[i].name)) {
589 flexfb_chip_load_param(&flexfb_chip_table[i]);
593 dev_err(dev, "chip=%s is not supported\n", chip);
598 static int flexfb_probe_common(struct spi_device *sdev,
599 struct platform_device *pdev)
602 struct fb_info *info;
603 struct fbtft_par *par;
607 initp_num = init_num;
614 fbtft_init_dbg(dev, "%s(%s)\n", __func__,
615 sdev ? "'SPI device'" : "'Platform device'");
618 ret = flexfb_chip_init(dev);
623 if (width == 0 || height == 0) {
624 dev_err(dev, "argument(s) missing: width and height has to be set.\n");
627 flex_display.width = width;
628 flex_display.height = height;
629 fbtft_init_dbg(dev, "Display resolution: %dx%d\n", width, height);
630 fbtft_init_dbg(dev, "chip = %s\n", chip ? chip : "not set");
631 fbtft_init_dbg(dev, "setaddrwin = %d\n", setaddrwin);
632 fbtft_init_dbg(dev, "regwidth = %d\n", regwidth);
633 fbtft_init_dbg(dev, "buswidth = %d\n", buswidth);
635 info = fbtft_framebuffer_alloc(&flex_display, dev, dev->platform_data);
644 if (!par->init_sequence)
645 par->init_sequence = initp;
646 par->fbtftops.init_display = fbtft_init_display;
648 /* registerwrite functions */
651 par->fbtftops.write_register = fbtft_write_reg8_bus8;
654 par->fbtftops.write_register = fbtft_write_reg16_bus8;
658 "argument 'regwidth': %d is not supported.\n",
665 par->fbtftops.write = fbtft_write_spi;
668 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
670 par->fbtftops.verify_gpios = flexfb_verify_gpios_dc;
673 if (regwidth == 16) {
674 dev_err(dev, "argument 'regwidth': %d is not supported with buswidth=%d and SPI.\n", regwidth, buswidth);
677 par->fbtftops.write_register = fbtft_write_reg8_bus9;
678 par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
679 if (par->spi->master->bits_per_word_mask
681 par->spi->bits_per_word = 9;
684 "9-bit SPI not available, emulating using 8-bit.\n");
685 /* allocate buffer with room for dc bits */
686 par->extra = devm_kzalloc(par->info->device,
687 par->txbuf.len + (par->txbuf.len / 8) + 8,
693 par->fbtftops.write = fbtft_write_spi_emulate_9;
697 dev_err(dev, "argument 'buswidth': %d is not supported with SPI.\n", buswidth);
701 par->fbtftops.verify_gpios = flexfb_verify_gpios_db;
704 par->fbtftops.write = fbtft_write_gpio8_wr;
705 par->fbtftops.write_vmem = fbtft_write_vmem16_bus8;
708 par->fbtftops.write_register = fbtft_write_reg16_bus16;
710 par->fbtftops.write = fbtft_write_gpio16_wr_latched;
712 par->fbtftops.write = fbtft_write_gpio16_wr;
713 par->fbtftops.write_vmem = fbtft_write_vmem16_bus16;
716 dev_err(dev, "argument 'buswidth': %d is not supported with parallel.\n", buswidth);
721 /* set_addr_win function */
722 switch (setaddrwin) {
727 par->fbtftops.set_addr_win = flexfb_set_addr_win_1;
730 par->fbtftops.set_addr_win = flexfb_set_addr_win_2;
733 par->fbtftops.set_addr_win = set_addr_win_3;
736 dev_err(dev, "argument 'setaddrwin': unknown value %d.\n",
742 par->fbtftops.register_backlight = fbtft_register_backlight;
744 ret = fbtft_register_framebuffer(info);
751 fbtft_framebuffer_release(info);
756 static int flexfb_remove_common(struct device *dev, struct fb_info *info)
758 struct fbtft_par *par;
764 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS, par, "%s()\n",
766 fbtft_unregister_framebuffer(info);
767 fbtft_framebuffer_release(info);
772 static int flexfb_probe_spi(struct spi_device *spi)
774 return flexfb_probe_common(spi, NULL);
777 static int flexfb_remove_spi(struct spi_device *spi)
779 struct fb_info *info = spi_get_drvdata(spi);
781 return flexfb_remove_common(&spi->dev, info);
784 static int flexfb_probe_pdev(struct platform_device *pdev)
786 return flexfb_probe_common(NULL, pdev);
789 static int flexfb_remove_pdev(struct platform_device *pdev)
791 struct fb_info *info = platform_get_drvdata(pdev);
793 return flexfb_remove_common(&pdev->dev, info);
796 static struct spi_driver flexfb_spi_driver = {
800 .probe = flexfb_probe_spi,
801 .remove = flexfb_remove_spi,
804 static const struct platform_device_id flexfb_platform_ids[] = {
808 MODULE_DEVICE_TABLE(platform, flexfb_platform_ids);
810 static struct platform_driver flexfb_platform_driver = {
814 .id_table = flexfb_platform_ids,
815 .probe = flexfb_probe_pdev,
816 .remove = flexfb_remove_pdev,
819 static int __init flexfb_init(void)
823 ret = spi_register_driver(&flexfb_spi_driver);
824 ret2 = platform_driver_register(&flexfb_platform_driver);
830 static void __exit flexfb_exit(void)
832 spi_unregister_driver(&flexfb_spi_driver);
833 platform_driver_unregister(&flexfb_platform_driver);
836 /* ------------------------------------------------------------------------- */
838 module_init(flexfb_init);
839 module_exit(flexfb_exit);
841 MODULE_DESCRIPTION("Generic FB driver for TFT LCD displays");
842 MODULE_AUTHOR("Noralf Tronnes");
843 MODULE_LICENSE("GPL");