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