GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / staging / fbtft / fb_st7789v.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * FB driver for the ST7789V LCD Controller
4  *
5  * Copyright (C) 2015 Dennis Menschel
6  */
7
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>
14
15 #include "fbtft.h"
16
17 #define DRVNAME "fb_st7789v"
18
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"
22
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"
26
27 #define HSD20_IPS 1
28
29 /**
30  * enum st7789v_command - ST7789V display controller commands
31  *
32  * @PORCTRL: porch setting
33  * @GCTRL: gate control
34  * @VCOMS: VCOM setting
35  * @VDVVRHEN: VDV and VRH command enable
36  * @VRHS: VRH set
37  * @VDVS: VDV set
38  * @VCMOFSET: VCOM offset set
39  * @PWCTRL1: power control 1
40  * @PVGAMCTRL: positive voltage gamma control
41  * @NVGAMCTRL: negative voltage gamma control
42  *
43  * The command names are the same as those found in the datasheet to ease
44  * looking up their semantics and usage.
45  *
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.
50  */
51 enum st7789v_command {
52         PORCTRL = 0xB2,
53         GCTRL = 0xB7,
54         VCOMS = 0xBB,
55         VDVVRHEN = 0xC2,
56         VRHS = 0xC3,
57         VDVS = 0xC4,
58         VCMOFSET = 0xC5,
59         PWCTRL1 = 0xD0,
60         PVGAMCTRL = 0xE0,
61         NVGAMCTRL = 0xE1,
62 };
63
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 */
68
69 /**
70  * init_display() - initialize the display controller
71  *
72  * @par: FBTFT parameter object
73  *
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.
80  *
81  * Return: 0 on success, < 0 if error occurred.
82  */
83 static int init_display(struct fbtft_par *par)
84 {
85         par->fbtftops.reset(par);
86
87         /* turn off sleep mode */
88         write_reg(par, MIPI_DCS_EXIT_SLEEP_MODE);
89         mdelay(120);
90
91         /* set pixel format to RGB-565 */
92         write_reg(par, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
93         if (HSD20_IPS)
94                 write_reg(par, PORCTRL, 0x05, 0x05, 0x00, 0x33, 0x33);
95
96         else
97                 write_reg(par, PORCTRL, 0x08, 0x08, 0x00, 0x22, 0x22);
98
99         /*
100          * VGH = 13.26V
101          * VGL = -10.43V
102          */
103         if (HSD20_IPS)
104                 write_reg(par, GCTRL, 0x75);
105         else
106                 write_reg(par, GCTRL, 0x35);
107
108         /*
109          * VDV and VRH register values come from command write
110          * (instead of NVM)
111          */
112         write_reg(par, VDVVRHEN, 0x01, 0xFF);
113
114         /*
115          * VAP =  4.1V + (VCOM + VCOM offset + 0.5 * VDV)
116          * VAN = -4.1V + (VCOM + VCOM offset + 0.5 * VDV)
117          */
118         if (HSD20_IPS)
119                 write_reg(par, VRHS, 0x13);
120         else
121                 write_reg(par, VRHS, 0x0B);
122
123         /* VDV = 0V */
124         write_reg(par, VDVS, 0x20);
125
126         /* VCOM = 0.9V */
127         if (HSD20_IPS)
128                 write_reg(par, VCOMS, 0x22);
129         else
130                 write_reg(par, VCOMS, 0x20);
131
132         /* VCOM offset = 0V */
133         write_reg(par, VCMOFSET, 0x20);
134
135         /*
136          * AVDD = 6.8V
137          * AVCL = -4.8V
138          * VDS = 2.3V
139          */
140         write_reg(par, PWCTRL1, 0xA4, 0xA1);
141
142         write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
143
144         if (HSD20_IPS)
145                 write_reg(par, MIPI_DCS_ENTER_INVERT_MODE);
146
147         return 0;
148 }
149
150 /**
151  * set_var() - apply LCD properties like rotation and BGR mode
152  *
153  * @par: FBTFT parameter object
154  *
155  * Return: 0 on success, < 0 if error occurred.
156  */
157 static int set_var(struct fbtft_par *par)
158 {
159         u8 madctl_par = 0;
160
161         if (par->bgr)
162                 madctl_par |= MADCTL_BGR;
163         switch (par->info->var.rotate) {
164         case 0:
165                 break;
166         case 90:
167                 madctl_par |= (MADCTL_MV | MADCTL_MY);
168                 break;
169         case 180:
170                 madctl_par |= (MADCTL_MX | MADCTL_MY);
171                 break;
172         case 270:
173                 madctl_par |= (MADCTL_MV | MADCTL_MX);
174                 break;
175         default:
176                 return -EINVAL;
177         }
178         write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
179         return 0;
180 }
181
182 /**
183  * set_gamma() - set gamma curves
184  *
185  * @par: FBTFT parameter object
186  * @curves: gamma curves
187  *
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.
193  *
194  * Return: 0 on success, < 0 if error occurred.
195  */
196 static int set_gamma(struct fbtft_par *par, u32 *curves)
197 {
198         int i;
199         int j;
200         int c; /* curve index offset */
201
202         /*
203          * Bitmasks for gamma curve command parameters.
204          * The masks are the same for both positive and negative voltage
205          * gamma curves.
206          */
207         static const u8 gamma_par_mask[] = {
208                 0xFF, /* V63[3:0], V0[3:0]*/
209                 0x3F, /* V1[5:0] */
210                 0x3F, /* V2[5:0] */
211                 0x1F, /* V4[4:0] */
212                 0x1F, /* V6[4:0] */
213                 0x3F, /* J0[1:0], V13[3:0] */
214                 0x7F, /* V20[6:0] */
215                 0x77, /* V36[2:0], V27[2:0] */
216                 0x7F, /* V43[6:0] */
217                 0x3F, /* J1[1:0], V50[3:0] */
218                 0x1F, /* V57[4:0] */
219                 0x1F, /* V59[4:0] */
220                 0x3F, /* V61[5:0] */
221                 0x3F, /* V62[5:0] */
222         };
223
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]);
234         }
235         return 0;
236 }
237
238 /**
239  * blank() - blank the display
240  *
241  * @par: FBTFT parameter object
242  * @on: whether to enable or disable blanking the display
243  *
244  * Return: 0 on success, < 0 if error occurred.
245  */
246 static int blank(struct fbtft_par *par, bool on)
247 {
248         if (on)
249                 write_reg(par, MIPI_DCS_SET_DISPLAY_OFF);
250         else
251                 write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
252         return 0;
253 }
254
255 static struct fbtft_display display = {
256         .regwidth = 8,
257         .width = 240,
258         .height = 320,
259         .gamma_num = 2,
260         .gamma_len = 14,
261         .gamma = HSD20_IPS_GAMMA,
262         .fbtftops = {
263                 .init_display = init_display,
264                 .set_var = set_var,
265                 .set_gamma = set_gamma,
266                 .blank = blank,
267         },
268 };
269
270 FBTFT_REGISTER_DRIVER(DRVNAME, "sitronix,st7789v", &display);
271
272 MODULE_ALIAS("spi:" DRVNAME);
273 MODULE_ALIAS("platform:" DRVNAME);
274 MODULE_ALIAS("spi:st7789v");
275 MODULE_ALIAS("platform:st7789v");
276
277 MODULE_DESCRIPTION("FB driver for the ST7789V LCD Controller");
278 MODULE_AUTHOR("Dennis Menschel");
279 MODULE_LICENSE("GPL");