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/gpio/consumer.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/interrupt.h>
14 #include <linux/completion.h>
15 #include <linux/module.h>
17 #include <video/mipi_display.h>
21 #define DRVNAME "fb_st7789v"
23 #define DEFAULT_GAMMA \
24 "70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25\n" \
25 "70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25"
27 #define HSD20_IPS_GAMMA \
28 "D0 05 0A 09 08 05 2E 44 45 0F 17 16 2B 33\n" \
29 "D0 05 0A 09 08 05 2E 43 45 0F 16 16 2B 33"
34 * enum st7789v_command - ST7789V display controller commands
36 * @PORCTRL: porch setting
37 * @GCTRL: gate control
38 * @VCOMS: VCOM setting
39 * @VDVVRHEN: VDV and VRH command enable
42 * @VCMOFSET: VCOM offset set
43 * @PWCTRL1: power control 1
44 * @PVGAMCTRL: positive voltage gamma control
45 * @NVGAMCTRL: negative voltage gamma control
47 * The command names are the same as those found in the datasheet to ease
48 * looking up their semantics and usage.
50 * Note that the ST7789V display controller offers quite a few more commands
51 * which have been omitted from this list as they are not used at the moment.
52 * Furthermore, commands that are compliant with the MIPI DCS have been left
53 * out as well to avoid duplicate entries.
55 enum st7789v_command
{
68 #define MADCTL_BGR BIT(3) /* bitmask for RGB/BGR order */
69 #define MADCTL_MV BIT(5) /* bitmask for page/column order */
70 #define MADCTL_MX BIT(6) /* bitmask for column address order */
71 #define MADCTL_MY BIT(7) /* bitmask for page address order */
73 /* 60Hz for 16.6ms, configured as 2*16.6ms */
74 #define PANEL_TE_TIMEOUT_MS 33
76 static struct completion panel_te
; /* completion for panel TE line */
77 static int irq_te
; /* Linux IRQ for LCD TE line */
79 static irqreturn_t
panel_te_handler(int irq
, void *data
)
86 * init_tearing_effect_line() - init tearing effect line.
87 * @par: FBTFT parameter object.
89 * Return: 0 on success, or a negative error code otherwise.
91 static int init_tearing_effect_line(struct fbtft_par
*par
)
93 struct device
*dev
= par
->info
->device
;
97 te
= gpiod_get_optional(dev
, "te", GPIOD_IN
);
99 return dev_err_probe(dev
, PTR_ERR(te
), "Failed to request te GPIO\n");
101 /* if te is NULL, indicating no configuration, directly return success */
107 irq
= gpiod_to_irq(te
);
109 /* GPIO is locked as an IRQ, we may drop the reference */
116 init_completion(&panel_te
);
118 /* The effective state is high and lasts no more than 1000 microseconds */
119 rc
= devm_request_irq(dev
, irq_te
, panel_te_handler
,
120 IRQF_TRIGGER_RISING
, "TE_GPIO", par
);
122 return dev_err_probe(dev
, rc
, "TE IRQ request failed.\n");
124 disable_irq_nosync(irq_te
);
130 * init_display() - initialize the display controller
132 * @par: FBTFT parameter object
134 * Most of the commands in this init function set their parameters to the
135 * same default values which are already in place after the display has been
136 * powered up. (The main exception to this rule is the pixel format which
137 * would default to 18 instead of 16 bit per pixel.)
138 * Nonetheless, this sequence can be used as a template for concrete
139 * displays which usually need some adjustments.
141 * Return: 0 on success, < 0 if error occurred.
143 static int init_display(struct fbtft_par
*par
)
147 par
->fbtftops
.reset(par
);
149 rc
= init_tearing_effect_line(par
);
153 /* turn off sleep mode */
154 write_reg(par
, MIPI_DCS_EXIT_SLEEP_MODE
);
157 /* set pixel format to RGB-565 */
158 write_reg(par
, MIPI_DCS_SET_PIXEL_FORMAT
, MIPI_DCS_PIXEL_FMT_16BIT
);
160 write_reg(par
, PORCTRL
, 0x05, 0x05, 0x00, 0x33, 0x33);
163 write_reg(par
, PORCTRL
, 0x08, 0x08, 0x00, 0x22, 0x22);
170 write_reg(par
, GCTRL
, 0x75);
172 write_reg(par
, GCTRL
, 0x35);
175 * VDV and VRH register values come from command write
178 write_reg(par
, VDVVRHEN
, 0x01, 0xFF);
181 * VAP = 4.1V + (VCOM + VCOM offset + 0.5 * VDV)
182 * VAN = -4.1V + (VCOM + VCOM offset + 0.5 * VDV)
185 write_reg(par
, VRHS
, 0x13);
187 write_reg(par
, VRHS
, 0x0B);
190 write_reg(par
, VDVS
, 0x20);
194 write_reg(par
, VCOMS
, 0x22);
196 write_reg(par
, VCOMS
, 0x20);
198 /* VCOM offset = 0V */
199 write_reg(par
, VCMOFSET
, 0x20);
206 write_reg(par
, PWCTRL1
, 0xA4, 0xA1);
208 /* TE line output is off by default when powering on */
210 write_reg(par
, MIPI_DCS_SET_TEAR_ON
, 0x00);
212 write_reg(par
, MIPI_DCS_SET_DISPLAY_ON
);
215 write_reg(par
, MIPI_DCS_ENTER_INVERT_MODE
);
221 * write_vmem() - write data to display.
222 * @par: FBTFT parameter object.
223 * @offset: offset from screen_buffer.
224 * @len: the length of data to be writte.
226 * Return: 0 on success, or a negative error code otherwise.
228 static int write_vmem(struct fbtft_par
*par
, size_t offset
, size_t len
)
230 struct device
*dev
= par
->info
->device
;
235 reinit_completion(&panel_te
);
236 ret
= wait_for_completion_timeout(&panel_te
,
237 msecs_to_jiffies(PANEL_TE_TIMEOUT_MS
));
239 dev_err(dev
, "wait panel TE timeout\n");
244 switch (par
->pdata
->display
.buswidth
) {
246 ret
= fbtft_write_vmem16_bus8(par
, offset
, len
);
249 ret
= fbtft_write_vmem16_bus9(par
, offset
, len
);
252 ret
= fbtft_write_vmem16_bus16(par
, offset
, len
);
255 dev_err(dev
, "Unsupported buswidth %d\n",
256 par
->pdata
->display
.buswidth
);
265 * set_var() - apply LCD properties like rotation and BGR mode
267 * @par: FBTFT parameter object
269 * Return: 0 on success, < 0 if error occurred.
271 static int set_var(struct fbtft_par
*par
)
276 madctl_par
|= MADCTL_BGR
;
277 switch (par
->info
->var
.rotate
) {
281 madctl_par
|= (MADCTL_MV
| MADCTL_MY
);
284 madctl_par
|= (MADCTL_MX
| MADCTL_MY
);
287 madctl_par
|= (MADCTL_MV
| MADCTL_MX
);
292 write_reg(par
, MIPI_DCS_SET_ADDRESS_MODE
, madctl_par
);
297 * set_gamma() - set gamma curves
299 * @par: FBTFT parameter object
300 * @curves: gamma curves
302 * Before the gamma curves are applied, they are preprocessed with a bitmask
303 * to ensure syntactically correct input for the display controller.
304 * This implies that the curves input parameter might be changed by this
305 * function and that illegal gamma values are auto-corrected and not
306 * reported as errors.
308 * Return: 0 on success, < 0 if error occurred.
310 static int set_gamma(struct fbtft_par
*par
, u32
*curves
)
314 int c
; /* curve index offset */
317 * Bitmasks for gamma curve command parameters.
318 * The masks are the same for both positive and negative voltage
321 static const u8 gamma_par_mask
[] = {
322 0xFF, /* V63[3:0], V0[3:0]*/
327 0x3F, /* J0[1:0], V13[3:0] */
329 0x77, /* V36[2:0], V27[2:0] */
331 0x3F, /* J1[1:0], V50[3:0] */
338 for (i
= 0; i
< par
->gamma
.num_curves
; i
++) {
339 c
= i
* par
->gamma
.num_values
;
340 for (j
= 0; j
< par
->gamma
.num_values
; j
++)
341 curves
[c
+ j
] &= gamma_par_mask
[j
];
342 write_reg(par
, PVGAMCTRL
+ i
,
343 curves
[c
+ 0], curves
[c
+ 1], curves
[c
+ 2],
344 curves
[c
+ 3], curves
[c
+ 4], curves
[c
+ 5],
345 curves
[c
+ 6], curves
[c
+ 7], curves
[c
+ 8],
346 curves
[c
+ 9], curves
[c
+ 10], curves
[c
+ 11],
347 curves
[c
+ 12], curves
[c
+ 13]);
353 * blank() - blank the display
355 * @par: FBTFT parameter object
356 * @on: whether to enable or disable blanking the display
358 * Return: 0 on success, < 0 if error occurred.
360 static int blank(struct fbtft_par
*par
, bool on
)
363 write_reg(par
, MIPI_DCS_SET_DISPLAY_OFF
);
365 write_reg(par
, MIPI_DCS_SET_DISPLAY_ON
);
369 static struct fbtft_display display
= {
375 .gamma
= HSD20_IPS_GAMMA
,
377 .init_display
= init_display
,
378 .write_vmem
= write_vmem
,
380 .set_gamma
= set_gamma
,
385 FBTFT_REGISTER_DRIVER(DRVNAME
, "sitronix,st7789v", &display
);
387 MODULE_ALIAS("spi:" DRVNAME
);
388 MODULE_ALIAS("platform:" DRVNAME
);
389 MODULE_ALIAS("spi:st7789v");
390 MODULE_ALIAS("platform:st7789v");
392 MODULE_DESCRIPTION("FB driver for the ST7789V LCD Controller");
393 MODULE_AUTHOR("Dennis Menschel");
394 MODULE_LICENSE("GPL");