1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * FB driver for the ILI9340 LCD Controller
4  *
5  * Copyright (C) 2013 Noralf Tronnes
6  */
7 
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/gpio.h>
12 #include <linux/delay.h>
13 #include <video/mipi_display.h>
14 
15 #include "fbtft.h"
16 
17 #define DRVNAME		"fb_ili9340"
18 #define WIDTH		240
19 #define HEIGHT		320
20 
21 /* Init sequence taken from: Arduino Library for the Adafruit 2.2" display */
init_display(struct fbtft_par * par)22 static int init_display(struct fbtft_par *par)
23 {
24 	par->fbtftops.reset(par);
25 
26 	write_reg(par, 0xEF, 0x03, 0x80, 0x02);
27 	write_reg(par, 0xCF, 0x00, 0XC1, 0X30);
28 	write_reg(par, 0xED, 0x64, 0x03, 0X12, 0X81);
29 	write_reg(par, 0xE8, 0x85, 0x00, 0x78);
30 	write_reg(par, 0xCB, 0x39, 0x2C, 0x00, 0x34, 0x02);
31 	write_reg(par, 0xF7, 0x20);
32 	write_reg(par, 0xEA, 0x00, 0x00);
33 
34 	/* Power Control 1 */
35 	write_reg(par, 0xC0, 0x23);
36 
37 	/* Power Control 2 */
38 	write_reg(par, 0xC1, 0x10);
39 
40 	/* VCOM Control 1 */
41 	write_reg(par, 0xC5, 0x3e, 0x28);
42 
43 	/* VCOM Control 2 */
44 	write_reg(par, 0xC7, 0x86);
45 
46 	/* COLMOD: Pixel Format Set */
47 	/* 16 bits/pixel */
48 	write_reg(par, MIPI_DCS_SET_PIXEL_FORMAT, 0x55);
49 
50 	/* Frame Rate Control */
51 	/* Division ratio = fosc, Frame Rate = 79Hz */
52 	write_reg(par, 0xB1, 0x00, 0x18);
53 
54 	/* Display Function Control */
55 	write_reg(par, 0xB6, 0x08, 0x82, 0x27);
56 
57 	/* Gamma Function Disable */
58 	write_reg(par, 0xF2, 0x00);
59 
60 	/* Gamma curve selection */
61 	write_reg(par, MIPI_DCS_SET_GAMMA_CURVE, 0x01);
62 
63 	/* Positive Gamma Correction */
64 	write_reg(par, 0xE0,
65 		  0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1,
66 		  0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00);
67 
68 	/* Negative Gamma Correction */
69 	write_reg(par, 0xE1,
70 		  0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1,
71 		  0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F);
72 
73 	write_reg(par, MIPI_DCS_EXIT_SLEEP_MODE);
74 
75 	mdelay(120);
76 
77 	write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
78 
79 	return 0;
80 }
81 
set_addr_win(struct fbtft_par * par,int xs,int ys,int xe,int ye)82 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
83 {
84 	write_reg(par, MIPI_DCS_SET_COLUMN_ADDRESS,
85 		  xs >> 8, xs & 0xFF, xe >> 8, xe & 0xFF);
86 
87 	write_reg(par, MIPI_DCS_SET_PAGE_ADDRESS,
88 		  ys >> 8, ys & 0xFF, ye >> 8, ye & 0xFF);
89 
90 	write_reg(par, MIPI_DCS_WRITE_MEMORY_START);
91 }
92 
93 #define ILI9340_MADCTL_MV  0x20
94 #define ILI9340_MADCTL_MX  0x40
95 #define ILI9340_MADCTL_MY  0x80
set_var(struct fbtft_par * par)96 static int set_var(struct fbtft_par *par)
97 {
98 	u8 val;
99 
100 	switch (par->info->var.rotate) {
101 	case 270:
102 		val = ILI9340_MADCTL_MV;
103 		break;
104 	case 180:
105 		val = ILI9340_MADCTL_MY;
106 		break;
107 	case 90:
108 		val = ILI9340_MADCTL_MV | ILI9340_MADCTL_MY | ILI9340_MADCTL_MX;
109 		break;
110 	default:
111 		val = ILI9340_MADCTL_MX;
112 		break;
113 	}
114 	/* Memory Access Control  */
115 	write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, val | (par->bgr << 3));
116 
117 	return 0;
118 }
119 
120 static struct fbtft_display display = {
121 	.regwidth = 8,
122 	.width = WIDTH,
123 	.height = HEIGHT,
124 	.fbtftops = {
125 		.init_display = init_display,
126 		.set_addr_win = set_addr_win,
127 		.set_var = set_var,
128 	},
129 };
130 
131 FBTFT_REGISTER_DRIVER(DRVNAME, "ilitek,ili9340", &display);
132 
133 MODULE_ALIAS("spi:" DRVNAME);
134 MODULE_ALIAS("platform:" DRVNAME);
135 MODULE_ALIAS("spi:ili9340");
136 MODULE_ALIAS("platform:ili9340");
137 
138 MODULE_DESCRIPTION("FB driver for the ILI9340 LCD Controller");
139 MODULE_AUTHOR("Noralf Tronnes");
140 MODULE_LICENSE("GPL");
141