Linux-libre 5.4.48-gnu
[librecmc/linux-libre.git] / drivers / staging / fbtft / fb_ssd1351.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/gpio/consumer.h>
6 #include <linux/spi/spi.h>
7 #include <linux/delay.h>
8
9 #include "fbtft.h"
10
11 #define DRVNAME         "fb_ssd1351"
12 #define WIDTH           128
13 #define HEIGHT          128
14 #define GAMMA_NUM       1
15 #define GAMMA_LEN       63
16 #define DEFAULT_GAMMA   "0 2 2 2 2 2 2 2 " \
17                         "2 2 2 2 2 2 2 2 " \
18                         "2 2 2 2 2 2 2 2 " \
19                         "2 2 2 2 2 2 2 2 " \
20                         "2 2 2 2 2 2 2 2 " \
21                         "2 2 2 2 2 2 2 2 " \
22                         "2 2 2 2 2 2 2 2 " \
23                         "2 2 2 2 2 2 2" \
24
25 static void register_onboard_backlight(struct fbtft_par *par);
26
27 static int init_display(struct fbtft_par *par)
28 {
29         if (par->pdata &&
30             par->pdata->display.backlight == FBTFT_ONBOARD_BACKLIGHT) {
31                 /* module uses onboard GPIO for panel power */
32                 par->fbtftops.register_backlight = register_onboard_backlight;
33         }
34
35         par->fbtftops.reset(par);
36
37         write_reg(par, 0xfd, 0x12); /* Command Lock */
38         write_reg(par, 0xfd, 0xb1); /* Command Lock */
39         write_reg(par, 0xae); /* Display Off */
40         write_reg(par, 0xb3, 0xf1); /* Front Clock Div */
41         write_reg(par, 0xca, 0x7f); /* Set Mux Ratio */
42         write_reg(par, 0x15, 0x00, 0x7f); /* Set Column Address */
43         write_reg(par, 0x75, 0x00, 0x7f); /* Set Row Address */
44         write_reg(par, 0xa1, 0x00); /* Set Display Start Line */
45         write_reg(par, 0xa2, 0x00); /* Set Display Offset */
46         write_reg(par, 0xb5, 0x00); /* Set GPIO */
47         write_reg(par, 0xab, 0x01); /* Set Function Selection */
48         write_reg(par, 0xb1, 0x32); /* Set Phase Length */
49         write_reg(par, 0xb4, 0xa0, 0xb5, 0x55); /* Set Segment Low Voltage */
50         write_reg(par, 0xbb, 0x17); /* Set Precharge Voltage */
51         write_reg(par, 0xbe, 0x05); /* Set VComH Voltage */
52         write_reg(par, 0xc1, 0xc8, 0x80, 0xc8); /* Set Contrast */
53         write_reg(par, 0xc7, 0x0f); /* Set Master Contrast */
54         write_reg(par, 0xb6, 0x01); /* Set Second Precharge Period */
55         write_reg(par, 0xa6); /* Set Display Mode Reset */
56         write_reg(par, 0xaf); /* Set Sleep Mode Display On */
57
58         return 0;
59 }
60
61 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
62 {
63         write_reg(par, 0x15, xs, xe);
64         write_reg(par, 0x75, ys, ye);
65         write_reg(par, 0x5c);
66 }
67
68 static int set_var(struct fbtft_par *par)
69 {
70         unsigned int remap;
71
72         if (par->fbtftops.init_display != init_display) {
73                 /* don't risk messing up register A0h */
74                 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par,
75                               "%s: skipping since custom init_display() is used\n",
76                                __func__);
77                 return 0;
78         }
79
80         remap = 0x60 | (par->bgr << 2); /* Set Colour Depth */
81
82         switch (par->info->var.rotate) {
83         case 0:
84                 write_reg(par, 0xA0, remap | 0x00 | BIT(4));
85                 break;
86         case 270:
87                 write_reg(par, 0xA0, remap | 0x03 | BIT(4));
88                 break;
89         case 180:
90                 write_reg(par, 0xA0, remap | 0x02);
91                 break;
92         case 90:
93                 write_reg(par, 0xA0, remap | 0x01);
94                 break;
95         }
96
97         return 0;
98 }
99
100 /*
101  * Grayscale Lookup Table
102  * GS1 - GS63
103  * The driver Gamma curve contains the relative values between the entries
104  * in the Lookup table.
105  *
106  * From datasheet:
107  * 8.8 Gray Scale Decoder
108  *
109  *      there are total 180 Gamma Settings (Setting 0 to Setting 180)
110  *      available for the Gray Scale table.
111  *
112  *      The gray scale is defined in incremental way, with reference
113  *      to the length of previous table entry:
114  *              Setting of GS1 has to be >= 0
115  *              Setting of GS2 has to be > Setting of GS1 +1
116  *              Setting of GS3 has to be > Setting of GS2 +1
117  *              :
118  *              Setting of GS63 has to be > Setting of GS62 +1
119  *
120  */
121 static int set_gamma(struct fbtft_par *par, u32 *curves)
122 {
123         unsigned long tmp[GAMMA_NUM * GAMMA_LEN];
124         int i, acc = 0;
125
126         for (i = 0; i < 63; i++) {
127                 if (i > 0 && curves[i] < 2) {
128                         dev_err(par->info->device,
129                                 "Illegal value in Grayscale Lookup Table at index %d : %d. Must be greater than 1\n",
130                                 i, curves[i]);
131                         return -EINVAL;
132                 }
133                 acc += curves[i];
134                 tmp[i] = acc;
135                 if (acc > 180) {
136                         dev_err(par->info->device,
137                                 "Illegal value(s) in Grayscale Lookup Table. At index=%d : %d, the accumulated value has exceeded 180\n",
138                                 i, acc);
139                         return -EINVAL;
140                 }
141         }
142
143         write_reg(par, 0xB8,
144                   tmp[0],  tmp[1],  tmp[2],  tmp[3],
145                   tmp[4],  tmp[5],  tmp[6],  tmp[7],
146                   tmp[8],  tmp[9],  tmp[10], tmp[11],
147                   tmp[12], tmp[13], tmp[14], tmp[15],
148                   tmp[16], tmp[17], tmp[18], tmp[19],
149                   tmp[20], tmp[21], tmp[22], tmp[23],
150                   tmp[24], tmp[25], tmp[26], tmp[27],
151                   tmp[28], tmp[29], tmp[30], tmp[31],
152                   tmp[32], tmp[33], tmp[34], tmp[35],
153                   tmp[36], tmp[37], tmp[38], tmp[39],
154                   tmp[40], tmp[41], tmp[42], tmp[43],
155                   tmp[44], tmp[45], tmp[46], tmp[47],
156                   tmp[48], tmp[49], tmp[50], tmp[51],
157                   tmp[52], tmp[53], tmp[54], tmp[55],
158                   tmp[56], tmp[57], tmp[58], tmp[59],
159                   tmp[60], tmp[61], tmp[62]);
160
161         return 0;
162 }
163
164 static int blank(struct fbtft_par *par, bool on)
165 {
166         fbtft_par_dbg(DEBUG_BLANK, par, "(%s=%s)\n",
167                       __func__, on ? "true" : "false");
168         if (on)
169                 write_reg(par, 0xAE);
170         else
171                 write_reg(par, 0xAF);
172         return 0;
173 }
174
175 static struct fbtft_display display = {
176         .regwidth = 8,
177         .width = WIDTH,
178         .height = HEIGHT,
179         .gamma_num = GAMMA_NUM,
180         .gamma_len = GAMMA_LEN,
181         .gamma = DEFAULT_GAMMA,
182         .fbtftops = {
183                 .init_display = init_display,
184                 .set_addr_win = set_addr_win,
185                 .set_var = set_var,
186                 .set_gamma = set_gamma,
187                 .blank = blank,
188         },
189 };
190
191 #ifdef CONFIG_FB_BACKLIGHT
192 static int update_onboard_backlight(struct backlight_device *bd)
193 {
194         struct fbtft_par *par = bl_get_data(bd);
195         bool on;
196
197         fbtft_par_dbg(DEBUG_BACKLIGHT, par,
198                       "%s: power=%d, fb_blank=%d\n",
199                       __func__, bd->props.power, bd->props.fb_blank);
200
201         on = (bd->props.power == FB_BLANK_UNBLANK) &&
202              (bd->props.fb_blank == FB_BLANK_UNBLANK);
203         /* Onboard backlight connected to GPIO0 on SSD1351, GPIO1 unused */
204         write_reg(par, 0xB5, on ? 0x03 : 0x02);
205
206         return 0;
207 }
208
209 static const struct backlight_ops bl_ops = {
210         .update_status = update_onboard_backlight,
211 };
212
213 static void register_onboard_backlight(struct fbtft_par *par)
214 {
215         struct backlight_device *bd;
216         struct backlight_properties bl_props = { 0, };
217
218         bl_props.type = BACKLIGHT_RAW;
219         bl_props.power = FB_BLANK_POWERDOWN;
220
221         bd = backlight_device_register(dev_driver_string(par->info->device),
222                                        par->info->device, par, &bl_ops,
223                                        &bl_props);
224         if (IS_ERR(bd)) {
225                 dev_err(par->info->device,
226                         "cannot register backlight device (%ld)\n",
227                         PTR_ERR(bd));
228                 return;
229         }
230         par->info->bl_dev = bd;
231
232         if (!par->fbtftops.unregister_backlight)
233                 par->fbtftops.unregister_backlight = fbtft_unregister_backlight;
234 }
235 #else
236 static void register_onboard_backlight(struct fbtft_par *par) { };
237 #endif
238
239 FBTFT_REGISTER_DRIVER(DRVNAME, "solomon,ssd1351", &display);
240
241 MODULE_ALIAS("spi:" DRVNAME);
242 MODULE_ALIAS("platform:" DRVNAME);
243 MODULE_ALIAS("spi:ssd1351");
244 MODULE_ALIAS("platform:ssd1351");
245
246 MODULE_DESCRIPTION("SSD1351 OLED Driver");
247 MODULE_AUTHOR("James Davies");
248 MODULE_LICENSE("GPL");