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"
24 * enum st7789v_command - ST7789V display controller commands
26 * @PORCTRL: porch setting
27 * @GCTRL: gate control
28 * @VCOMS: VCOM setting
29 * @VDVVRHEN: VDV and VRH command enable
32 * @VCMOFSET: VCOM offset set
33 * @PWCTRL1: power control 1
34 * @PVGAMCTRL: positive voltage gamma control
35 * @NVGAMCTRL: negative voltage gamma control
37 * The command names are the same as those found in the datasheet to ease
38 * looking up their semantics and usage.
40 * Note that the ST7789V display controller offers quite a few more commands
41 * which have been omitted from this list as they are not used at the moment.
42 * Furthermore, commands that are compliant with the MIPI DCS have been left
43 * out as well to avoid duplicate entries.
45 enum st7789v_command {
58 #define MADCTL_BGR BIT(3) /* bitmask for RGB/BGR order */
59 #define MADCTL_MV BIT(5) /* bitmask for page/column order */
60 #define MADCTL_MX BIT(6) /* bitmask for column address order */
61 #define MADCTL_MY BIT(7) /* bitmask for page address order */
64 * init_display() - initialize the display controller
66 * @par: FBTFT parameter object
68 * Most of the commands in this init function set their parameters to the
69 * same default values which are already in place after the display has been
70 * powered up. (The main exception to this rule is the pixel format which
71 * would default to 18 instead of 16 bit per pixel.)
72 * Nonetheless, this sequence can be used as a template for concrete
73 * displays which usually need some adjustments.
75 * Return: 0 on success, < 0 if error occurred.
77 static int init_display(struct fbtft_par *par)
79 par->fbtftops.reset(par);
81 /* turn off sleep mode */
82 write_reg(par, MIPI_DCS_EXIT_SLEEP_MODE);
85 /* set pixel format to RGB-565 */
86 write_reg(par, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
88 write_reg(par, PORCTRL, 0x08, 0x08, 0x00, 0x22, 0x22);
94 write_reg(par, GCTRL, 0x35);
97 * VDV and VRH register values come from command write
100 write_reg(par, VDVVRHEN, 0x01, 0xFF);
103 * VAP = 4.1V + (VCOM + VCOM offset + 0.5 * VDV)
104 * VAN = -4.1V + (VCOM + VCOM offset + 0.5 * VDV)
106 write_reg(par, VRHS, 0x0B);
109 write_reg(par, VDVS, 0x20);
112 write_reg(par, VCOMS, 0x20);
114 /* VCOM offset = 0V */
115 write_reg(par, VCMOFSET, 0x20);
122 write_reg(par, PWCTRL1, 0xA4, 0xA1);
124 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
129 * set_var() - apply LCD properties like rotation and BGR mode
131 * @par: FBTFT parameter object
133 * Return: 0 on success, < 0 if error occurred.
135 static int set_var(struct fbtft_par *par)
140 madctl_par |= MADCTL_BGR;
141 switch (par->info->var.rotate) {
145 madctl_par |= (MADCTL_MV | MADCTL_MY);
148 madctl_par |= (MADCTL_MX | MADCTL_MY);
151 madctl_par |= (MADCTL_MV | MADCTL_MX);
156 write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
161 * set_gamma() - set gamma curves
163 * @par: FBTFT parameter object
164 * @curves: gamma curves
166 * Before the gamma curves are applied, they are preprocessed with a bitmask
167 * to ensure syntactically correct input for the display controller.
168 * This implies that the curves input parameter might be changed by this
169 * function and that illegal gamma values are auto-corrected and not
170 * reported as errors.
172 * Return: 0 on success, < 0 if error occurred.
174 static int set_gamma(struct fbtft_par *par, u32 *curves)
178 int c; /* curve index offset */
181 * Bitmasks for gamma curve command parameters.
182 * The masks are the same for both positive and negative voltage
185 static const u8 gamma_par_mask[] = {
186 0xFF, /* V63[3:0], V0[3:0]*/
191 0x3F, /* J0[1:0], V13[3:0] */
193 0x77, /* V36[2:0], V27[2:0] */
195 0x3F, /* J1[1:0], V50[3:0] */
202 for (i = 0; i < par->gamma.num_curves; i++) {
203 c = i * par->gamma.num_values;
204 for (j = 0; j < par->gamma.num_values; j++)
205 curves[c + j] &= gamma_par_mask[j];
206 write_reg(par, PVGAMCTRL + i,
207 curves[c + 0], curves[c + 1], curves[c + 2],
208 curves[c + 3], curves[c + 4], curves[c + 5],
209 curves[c + 6], curves[c + 7], curves[c + 8],
210 curves[c + 9], curves[c + 10], curves[c + 11],
211 curves[c + 12], curves[c + 13]);
217 * blank() - blank the display
219 * @par: FBTFT parameter object
220 * @on: whether to enable or disable blanking the display
222 * Return: 0 on success, < 0 if error occurred.
224 static int blank(struct fbtft_par *par, bool on)
227 write_reg(par, MIPI_DCS_SET_DISPLAY_OFF);
229 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
233 static struct fbtft_display display = {
239 .gamma = DEFAULT_GAMMA,
241 .init_display = init_display,
243 .set_gamma = set_gamma,
248 FBTFT_REGISTER_DRIVER(DRVNAME, "sitronix,st7789v", &display);
250 MODULE_ALIAS("spi:" DRVNAME);
251 MODULE_ALIAS("platform:" DRVNAME);
252 MODULE_ALIAS("spi:st7789v");
253 MODULE_ALIAS("platform:st7789v");
255 MODULE_DESCRIPTION("FB driver for the ST7789V LCD Controller");
256 MODULE_AUTHOR("Dennis Menschel");
257 MODULE_LICENSE("GPL");