1 // SPDX-License-Identifier: GPL-2.0+
3 * FB driver for the ST7789V LCD Controller
5 * Copyright (C) 2015 Dennis Menschel
8 #include <linux/bitops.h>
9 #include <linux/delay.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <video/mipi_display.h>
17 #define DRVNAME "fb_st7789v"
19 #define DEFAULT_GAMMA \
20 "70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25\n" \
21 "70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25"
23 #define HSD20_IPS_GAMMA \
24 "D0 05 0A 09 08 05 2E 44 45 0F 17 16 2B 33\n" \
25 "D0 05 0A 09 08 05 2E 43 45 0F 16 16 2B 33"
30 * enum st7789v_command - ST7789V display controller commands
32 * @PORCTRL: porch setting
33 * @GCTRL: gate control
34 * @VCOMS: VCOM setting
35 * @VDVVRHEN: VDV and VRH command enable
38 * @VCMOFSET: VCOM offset set
39 * @PWCTRL1: power control 1
40 * @PVGAMCTRL: positive voltage gamma control
41 * @NVGAMCTRL: negative voltage gamma control
43 * The command names are the same as those found in the datasheet to ease
44 * looking up their semantics and usage.
46 * Note that the ST7789V display controller offers quite a few more commands
47 * which have been omitted from this list as they are not used at the moment.
48 * Furthermore, commands that are compliant with the MIPI DCS have been left
49 * out as well to avoid duplicate entries.
51 enum st7789v_command {
64 #define MADCTL_BGR BIT(3) /* bitmask for RGB/BGR order */
65 #define MADCTL_MV BIT(5) /* bitmask for page/column order */
66 #define MADCTL_MX BIT(6) /* bitmask for column address order */
67 #define MADCTL_MY BIT(7) /* bitmask for page address order */
70 * init_display() - initialize the display controller
72 * @par: FBTFT parameter object
74 * Most of the commands in this init function set their parameters to the
75 * same default values which are already in place after the display has been
76 * powered up. (The main exception to this rule is the pixel format which
77 * would default to 18 instead of 16 bit per pixel.)
78 * Nonetheless, this sequence can be used as a template for concrete
79 * displays which usually need some adjustments.
81 * Return: 0 on success, < 0 if error occurred.
83 static int init_display(struct fbtft_par *par)
85 /* turn off sleep mode */
86 write_reg(par, MIPI_DCS_EXIT_SLEEP_MODE);
89 /* set pixel format to RGB-565 */
90 write_reg(par, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
92 write_reg(par, PORCTRL, 0x05, 0x05, 0x00, 0x33, 0x33);
95 write_reg(par, PORCTRL, 0x08, 0x08, 0x00, 0x22, 0x22);
102 write_reg(par, GCTRL, 0x75);
104 write_reg(par, GCTRL, 0x35);
107 * VDV and VRH register values come from command write
110 write_reg(par, VDVVRHEN, 0x01, 0xFF);
113 * VAP = 4.1V + (VCOM + VCOM offset + 0.5 * VDV)
114 * VAN = -4.1V + (VCOM + VCOM offset + 0.5 * VDV)
117 write_reg(par, VRHS, 0x13);
119 write_reg(par, VRHS, 0x0B);
122 write_reg(par, VDVS, 0x20);
126 write_reg(par, VCOMS, 0x22);
128 write_reg(par, VCOMS, 0x20);
130 /* VCOM offset = 0V */
131 write_reg(par, VCMOFSET, 0x20);
138 write_reg(par, PWCTRL1, 0xA4, 0xA1);
140 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
143 write_reg(par, MIPI_DCS_ENTER_INVERT_MODE);
149 * set_var() - apply LCD properties like rotation and BGR mode
151 * @par: FBTFT parameter object
153 * Return: 0 on success, < 0 if error occurred.
155 static int set_var(struct fbtft_par *par)
160 madctl_par |= MADCTL_BGR;
161 switch (par->info->var.rotate) {
165 madctl_par |= (MADCTL_MV | MADCTL_MY);
168 madctl_par |= (MADCTL_MX | MADCTL_MY);
171 madctl_par |= (MADCTL_MV | MADCTL_MX);
176 write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
181 * set_gamma() - set gamma curves
183 * @par: FBTFT parameter object
184 * @curves: gamma curves
186 * Before the gamma curves are applied, they are preprocessed with a bitmask
187 * to ensure syntactically correct input for the display controller.
188 * This implies that the curves input parameter might be changed by this
189 * function and that illegal gamma values are auto-corrected and not
190 * reported as errors.
192 * Return: 0 on success, < 0 if error occurred.
194 static int set_gamma(struct fbtft_par *par, u32 *curves)
198 int c; /* curve index offset */
201 * Bitmasks for gamma curve command parameters.
202 * The masks are the same for both positive and negative voltage
205 static const u8 gamma_par_mask[] = {
206 0xFF, /* V63[3:0], V0[3:0]*/
211 0x3F, /* J0[1:0], V13[3:0] */
213 0x77, /* V36[2:0], V27[2:0] */
215 0x3F, /* J1[1:0], V50[3:0] */
222 for (i = 0; i < par->gamma.num_curves; i++) {
223 c = i * par->gamma.num_values;
224 for (j = 0; j < par->gamma.num_values; j++)
225 curves[c + j] &= gamma_par_mask[j];
226 write_reg(par, PVGAMCTRL + i,
227 curves[c + 0], curves[c + 1], curves[c + 2],
228 curves[c + 3], curves[c + 4], curves[c + 5],
229 curves[c + 6], curves[c + 7], curves[c + 8],
230 curves[c + 9], curves[c + 10], curves[c + 11],
231 curves[c + 12], curves[c + 13]);
237 * blank() - blank the display
239 * @par: FBTFT parameter object
240 * @on: whether to enable or disable blanking the display
242 * Return: 0 on success, < 0 if error occurred.
244 static int blank(struct fbtft_par *par, bool on)
247 write_reg(par, MIPI_DCS_SET_DISPLAY_OFF);
249 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
253 static struct fbtft_display display = {
259 .gamma = HSD20_IPS_GAMMA,
261 .init_display = init_display,
263 .set_gamma = set_gamma,
268 FBTFT_REGISTER_DRIVER(DRVNAME, "sitronix,st7789v", &display);
270 MODULE_ALIAS("spi:" DRVNAME);
271 MODULE_ALIAS("platform:" DRVNAME);
272 MODULE_ALIAS("spi:st7789v");
273 MODULE_ALIAS("platform:st7789v");
275 MODULE_DESCRIPTION("FB driver for the ST7789V LCD Controller");
276 MODULE_AUTHOR("Dennis Menschel");
277 MODULE_LICENSE("GPL");