2 * Generic FB driver for TFT LCD displays
4 * Copyright (C) 2013 Noralf Tronnes
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/vmalloc.h>
21 #include <linux/gpio.h>
22 #include <linux/spi/spi.h>
23 #include <linux/delay.h>
27 #define DRVNAME "flexfb"
30 module_param(chip
, charp
, 0000);
31 MODULE_PARM_DESC(chip
, "LCD controller");
33 static unsigned int width
;
34 module_param(width
, uint
, 0000);
35 MODULE_PARM_DESC(width
, "Display width");
37 static unsigned int height
;
38 module_param(height
, uint
, 0000);
39 MODULE_PARM_DESC(height
, "Display height");
43 module_param_array(init
, short, &init_num
, 0000);
44 MODULE_PARM_DESC(init
, "Init sequence");
46 static unsigned int setaddrwin
;
47 module_param(setaddrwin
, uint
, 0000);
48 MODULE_PARM_DESC(setaddrwin
, "Which set_addr_win() implementation to use");
50 static unsigned int buswidth
= 8;
51 module_param(buswidth
, uint
, 0000);
52 MODULE_PARM_DESC(buswidth
, "Width of databus (default: 8)");
54 static unsigned int regwidth
= 8;
55 module_param(regwidth
, uint
, 0000);
56 MODULE_PARM_DESC(regwidth
, "Width of controller register (default: 8)");
58 static bool nobacklight
;
59 module_param(nobacklight
, bool, 0000);
60 MODULE_PARM_DESC(nobacklight
, "Turn off backlight functionality.");
63 module_param(latched
, bool, 0000);
64 MODULE_PARM_DESC(latched
, "Use with latched 16-bit databus");
66 static const s16
*initp
;
69 /* default init sequences */
70 static const s16 st7735r_init
[] = {
75 -1, 0xB1, 0x01, 0x2C, 0x2D,
76 -1, 0xB2, 0x01, 0x2C, 0x2D,
77 -1, 0xB3, 0x01, 0x2C, 0x2D, 0x01, 0x2C, 0x2D,
79 -1, 0xC0, 0xA2, 0x02, 0x84,
88 -1, 0xE0, 0x0f, 0x1a, 0x0f, 0x18, 0x2f, 0x28, 0x20, 0x22,
89 0x1f, 0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10,
90 -1, 0xE1, 0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e,
91 0x30, 0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10,
99 static const s16 ssd1289_init
[] = {
145 static const s16 hx8340bn_init
[] = {
146 -1, 0xC1, 0xFF, 0x83, 0x40,
149 -1, 0xCA, 0x70, 0x00, 0xD9,
150 -1, 0xB0, 0x01, 0x11,
151 -1, 0xC9, 0x90, 0x49, 0x10, 0x28, 0x28, 0x10, 0x00, 0x06,
153 -1, 0xC2, 0x60, 0x71, 0x01, 0x0E, 0x05, 0x02, 0x09, 0x31, 0x0A,
154 -1, 0xC3, 0x67, 0x30, 0x61, 0x17, 0x48, 0x07, 0x05, 0x33,
156 -1, 0xB5, 0x35, 0x20, 0x45,
157 -1, 0xB4, 0x33, 0x25, 0x4C,
165 static const s16 ili9225_init
[] = {
207 static const s16 ili9320_init
[] = {
268 static const s16 ili9325_init
[] = {
327 static const s16 ili9341_init
[] = {
330 -1, 0xCF, 0x00, 0x83, 0x30,
331 -1, 0xED, 0x64, 0x03, 0x12, 0x81,
332 -1, 0xE8, 0x85, 0x01, 0x79,
333 -1, 0xCB, 0x39, 0x2c, 0x00, 0x34, 0x02,
335 -1, 0xEA, 0x00, 0x00,
338 -1, 0xC5, 0x35, 0x3E,
340 -1, 0xB1, 0x00, 0x1B,
341 -1, 0xB6, 0x0a, 0x82, 0x27, 0x00,
352 static const s16 ssd1351_init
[] = {
359 -1, 0x15, 0x00, 0x7f,
360 -1, 0x75, 0x00, 0x7f,
366 -1, 0xb4, 0xa0, 0xb5, 0x55,
369 -1, 0xc1, 0xc8, 0x80, 0xc8,
378 * struct flexfb_lcd_controller - Describes the LCD controller properties
379 * @name: Model name of the chip
380 * @width: Width of display in pixels
381 * @height: Height of display in pixels
382 * @setaddrwin: Which set_addr_win() implementation to use
383 * @regwidth: LCD Controller Register width in bits
384 * @init_seq: LCD initialization sequence
385 * @init_seq_sz: Size of LCD initialization sequence
387 struct flexfb_lcd_controller
{
391 unsigned int setaddrwin
;
392 unsigned int regwidth
;
397 static const struct flexfb_lcd_controller flexfb_chip_table
[] = {
402 .init_seq
= st7735r_init
,
403 .init_seq_sz
= ARRAY_SIZE(st7735r_init
),
409 .init_seq
= hx8340bn_init
,
410 .init_seq_sz
= ARRAY_SIZE(hx8340bn_init
),
417 .init_seq
= ili9225_init
,
418 .init_seq_sz
= ARRAY_SIZE(ili9225_init
),
426 .init_seq
= ili9320_init
,
427 .init_seq_sz
= ARRAY_SIZE(ili9320_init
),
435 .init_seq
= ili9325_init
,
436 .init_seq_sz
= ARRAY_SIZE(ili9325_init
),
442 .init_seq
= ili9341_init
,
443 .init_seq_sz
= ARRAY_SIZE(ili9341_init
),
451 .init_seq
= ssd1289_init
,
452 .init_seq_sz
= ARRAY_SIZE(ssd1289_init
),
459 .init_seq
= ssd1351_init
,
460 .init_seq_sz
= ARRAY_SIZE(ssd1351_init
),
464 /* ili9320, ili9325 */
465 static void flexfb_set_addr_win_1(struct fbtft_par
*par
,
466 int xs
, int ys
, int xe
, int ye
)
468 switch (par
->info
->var
.rotate
) {
469 /* R20h = Horizontal GRAM Start Address */
470 /* R21h = Vertical GRAM Start Address */
472 write_reg(par
, 0x0020, xs
);
473 write_reg(par
, 0x0021, ys
);
476 write_reg(par
, 0x0020, width
- 1 - xs
);
477 write_reg(par
, 0x0021, height
- 1 - ys
);
480 write_reg(par
, 0x0020, width
- 1 - ys
);
481 write_reg(par
, 0x0021, xs
);
484 write_reg(par
, 0x0020, ys
);
485 write_reg(par
, 0x0021, height
- 1 - xs
);
488 write_reg(par
, 0x0022); /* Write Data to GRAM */
492 static void flexfb_set_addr_win_2(struct fbtft_par
*par
,
493 int xs
, int ys
, int xe
, int ye
)
495 switch (par
->info
->var
.rotate
) {
496 /* R4Eh - Set GDDRAM X address counter */
497 /* R4Fh - Set GDDRAM Y address counter */
499 write_reg(par
, 0x4e, xs
);
500 write_reg(par
, 0x4f, ys
);
503 write_reg(par
, 0x4e, par
->info
->var
.xres
- 1 - xs
);
504 write_reg(par
, 0x4f, par
->info
->var
.yres
- 1 - ys
);
507 write_reg(par
, 0x4e, par
->info
->var
.yres
- 1 - ys
);
508 write_reg(par
, 0x4f, xs
);
511 write_reg(par
, 0x4e, ys
);
512 write_reg(par
, 0x4f, par
->info
->var
.xres
- 1 - xs
);
516 /* R22h - RAM data write */
517 write_reg(par
, 0x22, 0);
521 static void set_addr_win_3(struct fbtft_par
*par
,
522 int xs
, int ys
, int xe
, int ye
)
524 write_reg(par
, 0x15, xs
, xe
);
525 write_reg(par
, 0x75, ys
, ye
);
526 write_reg(par
, 0x5C);
529 static int flexfb_verify_gpios_dc(struct fbtft_par
*par
)
531 fbtft_par_dbg(DEBUG_VERIFY_GPIOS
, par
, "%s()\n", __func__
);
533 if (par
->gpio
.dc
< 0) {
534 dev_err(par
->info
->device
,
535 "Missing info about 'dc' gpio. Aborting.\n");
542 static int flexfb_verify_gpios_db(struct fbtft_par
*par
)
545 int num_db
= buswidth
;
547 fbtft_par_dbg(DEBUG_VERIFY_GPIOS
, par
, "%s()\n", __func__
);
549 if (par
->gpio
.dc
< 0) {
550 dev_err(par
->info
->device
, "Missing info about 'dc' gpio. Aborting.\n");
553 if (par
->gpio
.wr
< 0) {
554 dev_err(par
->info
->device
, "Missing info about 'wr' gpio. Aborting.\n");
557 if (latched
&& (par
->gpio
.latch
< 0)) {
558 dev_err(par
->info
->device
, "Missing info about 'latch' gpio. Aborting.\n");
562 num_db
= buswidth
/ 2;
563 for (i
= 0; i
< num_db
; i
++) {
564 if (par
->gpio
.db
[i
] < 0) {
565 dev_err(par
->info
->device
,
566 "Missing info about 'db%02d' gpio. Aborting.\n",
575 static void flexfb_chip_load_param(const struct flexfb_lcd_controller
*chip
)
580 height
= chip
->height
;
581 setaddrwin
= chip
->setaddrwin
;
583 regwidth
= chip
->regwidth
;
585 initp
= chip
->init_seq
;
586 initp_num
= chip
->init_seq_sz
;
590 static struct fbtft_display flex_display
= { };
592 static int flexfb_chip_init(const struct device
*dev
)
596 for (i
= 0; i
< ARRAY_SIZE(flexfb_chip_table
); i
++)
597 if (!strcmp(chip
, flexfb_chip_table
[i
].name
)) {
598 flexfb_chip_load_param(&flexfb_chip_table
[i
]);
602 dev_err(dev
, "chip=%s is not supported\n", chip
);
607 static int flexfb_probe_common(struct spi_device
*sdev
,
608 struct platform_device
*pdev
)
611 struct fb_info
*info
;
612 struct fbtft_par
*par
;
616 initp_num
= init_num
;
623 fbtft_init_dbg(dev
, "%s(%s)\n", __func__
,
624 sdev
? "'SPI device'" : "'Platform device'");
627 ret
= flexfb_chip_init(dev
);
632 if (width
== 0 || height
== 0) {
633 dev_err(dev
, "argument(s) missing: width and height has to be set.\n");
636 flex_display
.width
= width
;
637 flex_display
.height
= height
;
638 fbtft_init_dbg(dev
, "Display resolution: %dx%d\n", width
, height
);
639 fbtft_init_dbg(dev
, "chip = %s\n", chip
? chip
: "not set");
640 fbtft_init_dbg(dev
, "setaddrwin = %d\n", setaddrwin
);
641 fbtft_init_dbg(dev
, "regwidth = %d\n", regwidth
);
642 fbtft_init_dbg(dev
, "buswidth = %d\n", buswidth
);
644 info
= fbtft_framebuffer_alloc(&flex_display
, dev
, dev
->platform_data
);
653 if (!par
->init_sequence
)
654 par
->init_sequence
= initp
;
655 par
->fbtftops
.init_display
= fbtft_init_display
;
657 /* registerwrite functions */
660 par
->fbtftops
.write_register
= fbtft_write_reg8_bus8
;
663 par
->fbtftops
.write_register
= fbtft_write_reg16_bus8
;
667 "argument 'regwidth': %d is not supported.\n",
674 par
->fbtftops
.write
= fbtft_write_spi
;
677 par
->fbtftops
.write_vmem
= fbtft_write_vmem16_bus8
;
679 par
->fbtftops
.verify_gpios
= flexfb_verify_gpios_dc
;
682 if (regwidth
== 16) {
683 dev_err(dev
, "argument 'regwidth': %d is not supported with buswidth=%d and SPI.\n", regwidth
, buswidth
);
686 par
->fbtftops
.write_register
= fbtft_write_reg8_bus9
;
687 par
->fbtftops
.write_vmem
= fbtft_write_vmem16_bus9
;
688 if (par
->spi
->master
->bits_per_word_mask
690 par
->spi
->bits_per_word
= 9;
693 "9-bit SPI not available, emulating using 8-bit.\n");
694 /* allocate buffer with room for dc bits */
695 par
->extra
= devm_kzalloc(par
->info
->device
,
696 par
->txbuf
.len
+ (par
->txbuf
.len
/ 8) + 8,
702 par
->fbtftops
.write
= fbtft_write_spi_emulate_9
;
706 dev_err(dev
, "argument 'buswidth': %d is not supported with SPI.\n", buswidth
);
710 par
->fbtftops
.verify_gpios
= flexfb_verify_gpios_db
;
713 par
->fbtftops
.write
= fbtft_write_gpio8_wr
;
714 par
->fbtftops
.write_vmem
= fbtft_write_vmem16_bus8
;
717 par
->fbtftops
.write_register
= fbtft_write_reg16_bus16
;
719 par
->fbtftops
.write
= fbtft_write_gpio16_wr_latched
;
721 par
->fbtftops
.write
= fbtft_write_gpio16_wr
;
722 par
->fbtftops
.write_vmem
= fbtft_write_vmem16_bus16
;
725 dev_err(dev
, "argument 'buswidth': %d is not supported with parallel.\n", buswidth
);
730 /* set_addr_win function */
731 switch (setaddrwin
) {
736 par
->fbtftops
.set_addr_win
= flexfb_set_addr_win_1
;
739 par
->fbtftops
.set_addr_win
= flexfb_set_addr_win_2
;
742 par
->fbtftops
.set_addr_win
= set_addr_win_3
;
745 dev_err(dev
, "argument 'setaddrwin': unknown value %d.\n",
751 par
->fbtftops
.register_backlight
= fbtft_register_backlight
;
753 ret
= fbtft_register_framebuffer(info
);
760 fbtft_framebuffer_release(info
);
765 static int flexfb_remove_common(struct device
*dev
, struct fb_info
*info
)
767 struct fbtft_par
*par
;
773 fbtft_par_dbg(DEBUG_DRIVER_INIT_FUNCTIONS
, par
, "%s()\n",
775 fbtft_unregister_framebuffer(info
);
776 fbtft_framebuffer_release(info
);
781 static int flexfb_probe_spi(struct spi_device
*spi
)
783 return flexfb_probe_common(spi
, NULL
);
786 static int flexfb_remove_spi(struct spi_device
*spi
)
788 struct fb_info
*info
= spi_get_drvdata(spi
);
790 return flexfb_remove_common(&spi
->dev
, info
);
793 static int flexfb_probe_pdev(struct platform_device
*pdev
)
795 return flexfb_probe_common(NULL
, pdev
);
798 static int flexfb_remove_pdev(struct platform_device
*pdev
)
800 struct fb_info
*info
= platform_get_drvdata(pdev
);
802 return flexfb_remove_common(&pdev
->dev
, info
);
805 static struct spi_driver flexfb_spi_driver
= {
809 .probe
= flexfb_probe_spi
,
810 .remove
= flexfb_remove_spi
,
813 static const struct platform_device_id flexfb_platform_ids
[] = {
817 MODULE_DEVICE_TABLE(platform
, flexfb_platform_ids
);
819 static struct platform_driver flexfb_platform_driver
= {
823 .id_table
= flexfb_platform_ids
,
824 .probe
= flexfb_probe_pdev
,
825 .remove
= flexfb_remove_pdev
,
828 static int __init
flexfb_init(void)
832 ret
= spi_register_driver(&flexfb_spi_driver
);
833 ret2
= platform_driver_register(&flexfb_platform_driver
);
839 static void __exit
flexfb_exit(void)
841 spi_unregister_driver(&flexfb_spi_driver
);
842 platform_driver_unregister(&flexfb_platform_driver
);
845 /* ------------------------------------------------------------------------- */
847 module_init(flexfb_init
);
848 module_exit(flexfb_exit
);
850 MODULE_DESCRIPTION("Generic FB driver for TFT LCD displays");
851 MODULE_AUTHOR("Noralf Tronnes");
852 MODULE_LICENSE("GPL");