2 * FB driver for the ST7789V LCD Controller
4 * Copyright (C) 2015 Dennis Menschel
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/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <video/mipi_display.h>
26 #define DRVNAME "fb_st7789v"
28 #define DEFAULT_GAMMA \
29 "70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25\n" \
30 "70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25"
33 * enum st7789v_command - ST7789V display controller commands
35 * @PORCTRL: porch setting
36 * @GCTRL: gate control
37 * @VCOMS: VCOM setting
38 * @VDVVRHEN: VDV and VRH command enable
41 * @VCMOFSET: VCOM offset set
42 * @PWCTRL1: power control 1
43 * @PVGAMCTRL: positive voltage gamma control
44 * @NVGAMCTRL: negative voltage gamma control
46 * The command names are the same as those found in the datasheet to ease
47 * looking up their semantics and usage.
49 * Note that the ST7789V display controller offers quite a few more commands
50 * which have been omitted from this list as they are not used at the moment.
51 * Furthermore, commands that are compliant with the MIPI DCS have been left
52 * out as well to avoid duplicate entries.
54 enum st7789v_command {
67 #define MADCTL_BGR BIT(3) /* bitmask for RGB/BGR order */
68 #define MADCTL_MV BIT(5) /* bitmask for page/column order */
69 #define MADCTL_MX BIT(6) /* bitmask for column address order */
70 #define MADCTL_MY BIT(7) /* bitmask for page address order */
73 * init_display() - initialize the display controller
75 * @par: FBTFT parameter object
77 * Most of the commands in this init function set their parameters to the
78 * same default values which are already in place after the display has been
79 * powered up. (The main exception to this rule is the pixel format which
80 * would default to 18 instead of 16 bit per pixel.)
81 * Nonetheless, this sequence can be used as a template for concrete
82 * displays which usually need some adjustments.
84 * Return: 0 on success, < 0 if error occurred.
86 static int init_display(struct fbtft_par *par)
88 par->fbtftops.reset(par);
90 /* turn off sleep mode */
91 write_reg(par, MIPI_DCS_EXIT_SLEEP_MODE);
94 /* set pixel format to RGB-565 */
95 write_reg(par, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
97 write_reg(par, PORCTRL, 0x08, 0x08, 0x00, 0x22, 0x22);
103 write_reg(par, GCTRL, 0x35);
106 * VDV and VRH register values come from command write
109 write_reg(par, VDVVRHEN, 0x01, 0xFF);
112 * VAP = 4.1V + (VCOM + VCOM offset + 0.5 * VDV)
113 * VAN = -4.1V + (VCOM + VCOM offset + 0.5 * VDV)
115 write_reg(par, VRHS, 0x0B);
118 write_reg(par, VDVS, 0x20);
121 write_reg(par, VCOMS, 0x20);
123 /* VCOM offset = 0V */
124 write_reg(par, VCMOFSET, 0x20);
131 write_reg(par, PWCTRL1, 0xA4, 0xA1);
133 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
138 * set_var() - apply LCD properties like rotation and BGR mode
140 * @par: FBTFT parameter object
142 * Return: 0 on success, < 0 if error occurred.
144 static int set_var(struct fbtft_par *par)
149 madctl_par |= MADCTL_BGR;
150 switch (par->info->var.rotate) {
154 madctl_par |= (MADCTL_MV | MADCTL_MY);
157 madctl_par |= (MADCTL_MX | MADCTL_MY);
160 madctl_par |= (MADCTL_MV | MADCTL_MX);
165 write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
170 * set_gamma() - set gamma curves
172 * @par: FBTFT parameter object
173 * @curves: gamma curves
175 * Before the gamma curves are applied, they are preprocessed with a bitmask
176 * to ensure syntactically correct input for the display controller.
177 * This implies that the curves input parameter might be changed by this
178 * function and that illegal gamma values are auto-corrected and not
179 * reported as errors.
181 * Return: 0 on success, < 0 if error occurred.
183 static int set_gamma(struct fbtft_par *par, u32 *curves)
187 int c; /* curve index offset */
190 * Bitmasks for gamma curve command parameters.
191 * The masks are the same for both positive and negative voltage
194 static const u8 gamma_par_mask[] = {
195 0xFF, /* V63[3:0], V0[3:0]*/
200 0x3F, /* J0[1:0], V13[3:0] */
202 0x77, /* V36[2:0], V27[2:0] */
204 0x3F, /* J1[1:0], V50[3:0] */
211 for (i = 0; i < par->gamma.num_curves; i++) {
212 c = i * par->gamma.num_values;
213 for (j = 0; j < par->gamma.num_values; j++)
214 curves[c + j] &= gamma_par_mask[j];
217 curves[c + 0], curves[c + 1], curves[c + 2],
218 curves[c + 3], curves[c + 4], curves[c + 5],
219 curves[c + 6], curves[c + 7], curves[c + 8],
220 curves[c + 9], curves[c + 10], curves[c + 11],
221 curves[c + 12], curves[c + 13]);
227 * blank() - blank the display
229 * @par: FBTFT parameter object
230 * @on: whether to enable or disable blanking the display
232 * Return: 0 on success, < 0 if error occurred.
234 static int blank(struct fbtft_par *par, bool on)
237 write_reg(par, MIPI_DCS_SET_DISPLAY_OFF);
239 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
243 static struct fbtft_display display = {
249 .gamma = DEFAULT_GAMMA,
251 .init_display = init_display,
253 .set_gamma = set_gamma,
258 FBTFT_REGISTER_DRIVER(DRVNAME, "sitronix,st7789v", &display);
260 MODULE_ALIAS("spi:" DRVNAME);
261 MODULE_ALIAS("platform:" DRVNAME);
262 MODULE_ALIAS("spi:st7789v");
263 MODULE_ALIAS("platform:st7789v");
265 MODULE_DESCRIPTION("FB driver for the ST7789V LCD Controller");
266 MODULE_AUTHOR("Dennis Menschel");
267 MODULE_LICENSE("GPL");