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 par->fbtftops.reset(par);
87 /* turn off sleep mode */
88 write_reg(par, MIPI_DCS_EXIT_SLEEP_MODE);
91 /* set pixel format to RGB-565 */
92 write_reg(par, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
94 write_reg(par, PORCTRL, 0x05, 0x05, 0x00, 0x33, 0x33);
97 write_reg(par, PORCTRL, 0x08, 0x08, 0x00, 0x22, 0x22);
104 write_reg(par, GCTRL, 0x75);
106 write_reg(par, GCTRL, 0x35);
109 * VDV and VRH register values come from command write
112 write_reg(par, VDVVRHEN, 0x01, 0xFF);
115 * VAP = 4.1V + (VCOM + VCOM offset + 0.5 * VDV)
116 * VAN = -4.1V + (VCOM + VCOM offset + 0.5 * VDV)
119 write_reg(par, VRHS, 0x13);
121 write_reg(par, VRHS, 0x0B);
124 write_reg(par, VDVS, 0x20);
128 write_reg(par, VCOMS, 0x22);
130 write_reg(par, VCOMS, 0x20);
132 /* VCOM offset = 0V */
133 write_reg(par, VCMOFSET, 0x20);
140 write_reg(par, PWCTRL1, 0xA4, 0xA1);
142 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
145 write_reg(par, MIPI_DCS_ENTER_INVERT_MODE);
151 * set_var() - apply LCD properties like rotation and BGR mode
153 * @par: FBTFT parameter object
155 * Return: 0 on success, < 0 if error occurred.
157 static int set_var(struct fbtft_par *par)
162 madctl_par |= MADCTL_BGR;
163 switch (par->info->var.rotate) {
167 madctl_par |= (MADCTL_MV | MADCTL_MY);
170 madctl_par |= (MADCTL_MX | MADCTL_MY);
173 madctl_par |= (MADCTL_MV | MADCTL_MX);
178 write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
183 * set_gamma() - set gamma curves
185 * @par: FBTFT parameter object
186 * @curves: gamma curves
188 * Before the gamma curves are applied, they are preprocessed with a bitmask
189 * to ensure syntactically correct input for the display controller.
190 * This implies that the curves input parameter might be changed by this
191 * function and that illegal gamma values are auto-corrected and not
192 * reported as errors.
194 * Return: 0 on success, < 0 if error occurred.
196 static int set_gamma(struct fbtft_par *par, u32 *curves)
200 int c; /* curve index offset */
203 * Bitmasks for gamma curve command parameters.
204 * The masks are the same for both positive and negative voltage
207 static const u8 gamma_par_mask[] = {
208 0xFF, /* V63[3:0], V0[3:0]*/
213 0x3F, /* J0[1:0], V13[3:0] */
215 0x77, /* V36[2:0], V27[2:0] */
217 0x3F, /* J1[1:0], V50[3:0] */
224 for (i = 0; i < par->gamma.num_curves; i++) {
225 c = i * par->gamma.num_values;
226 for (j = 0; j < par->gamma.num_values; j++)
227 curves[c + j] &= gamma_par_mask[j];
228 write_reg(par, PVGAMCTRL + i,
229 curves[c + 0], curves[c + 1], curves[c + 2],
230 curves[c + 3], curves[c + 4], curves[c + 5],
231 curves[c + 6], curves[c + 7], curves[c + 8],
232 curves[c + 9], curves[c + 10], curves[c + 11],
233 curves[c + 12], curves[c + 13]);
239 * blank() - blank the display
241 * @par: FBTFT parameter object
242 * @on: whether to enable or disable blanking the display
244 * Return: 0 on success, < 0 if error occurred.
246 static int blank(struct fbtft_par *par, bool on)
249 write_reg(par, MIPI_DCS_SET_DISPLAY_OFF);
251 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
255 static struct fbtft_display display = {
261 .gamma = HSD20_IPS_GAMMA,
263 .init_display = init_display,
265 .set_gamma = set_gamma,
270 FBTFT_REGISTER_DRIVER(DRVNAME, "sitronix,st7789v", &display);
272 MODULE_ALIAS("spi:" DRVNAME);
273 MODULE_ALIAS("platform:" DRVNAME);
274 MODULE_ALIAS("spi:st7789v");
275 MODULE_ALIAS("platform:st7789v");
277 MODULE_DESCRIPTION("FB driver for the ST7789V LCD Controller");
278 MODULE_AUTHOR("Dennis Menschel");
279 MODULE_LICENSE("GPL");