• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ssd1331"
12 #define WIDTH		96
13 #define HEIGHT		64
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 
init_display(struct fbtft_par * par)25 static int init_display(struct fbtft_par *par)
26 {
27 	par->fbtftops.reset(par);
28 
29 	write_reg(par, 0xae); /* Display Off */
30 
31 	/* Set Column Address Mapping, COM Scan Direction and Colour Depth */
32 	if (par->info->var.rotate == 180)
33 		write_reg(par, 0xa0, 0x60 | (par->bgr << 2));
34 	else
35 		write_reg(par, 0xa0, 0x72 | (par->bgr << 2));
36 
37 	write_reg(par, 0x72); /* RGB colour */
38 	write_reg(par, 0xa1, 0x00); /* Set Display Start Line */
39 	write_reg(par, 0xa2, 0x00); /* Set Display Offset */
40 	write_reg(par, 0xa4); /* NORMALDISPLAY */
41 	write_reg(par, 0xa8, 0x3f); /* Set multiplex */
42 	write_reg(par, 0xad, 0x8e); /* Set master */
43 	/* write_reg(par, 0xb0, 0x0b);  Set power mode */
44 	write_reg(par, 0xb1, 0x31); /* Precharge */
45 	write_reg(par, 0xb3, 0xf0); /* Clock div */
46 	write_reg(par, 0x8a, 0x64); /* Precharge A */
47 	write_reg(par, 0x8b, 0x78); /* Precharge B */
48 	write_reg(par, 0x8c, 0x64); /* Precharge C */
49 	write_reg(par, 0xbb, 0x3a); /* Precharge level */
50 	write_reg(par, 0xbe, 0x3e); /* vcomh */
51 	write_reg(par, 0x87, 0x06); /* Master current */
52 	write_reg(par, 0x81, 0x91); /* Contrast A */
53 	write_reg(par, 0x82, 0x50); /* Contrast B */
54 	write_reg(par, 0x83, 0x7d); /* Contrast C */
55 	write_reg(par, 0xaf); /* Set Sleep Mode Display On */
56 
57 	return 0;
58 }
59 
set_addr_win(struct fbtft_par * par,int xs,int ys,int xe,int ye)60 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
61 {
62 	write_reg(par, 0x15, xs, xe);
63 	write_reg(par, 0x75, ys, ye);
64 }
65 
write_reg8_bus8(struct fbtft_par * par,int len,...)66 static void write_reg8_bus8(struct fbtft_par *par, int len, ...)
67 {
68 	va_list args;
69 	int i, ret;
70 	u8 *buf = par->buf;
71 
72 	if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) {
73 		va_start(args, len);
74 		for (i = 0; i < len; i++)
75 			buf[i] = (u8)va_arg(args, unsigned int);
76 		va_end(args);
77 		fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, par->info->device,
78 				  u8, buf, len, "%s: ", __func__);
79 	}
80 
81 	va_start(args, len);
82 
83 	*buf = (u8)va_arg(args, unsigned int);
84 	gpiod_set_value(par->gpio.dc, 0);
85 	ret = par->fbtftops.write(par, par->buf, sizeof(u8));
86 	if (ret < 0) {
87 		va_end(args);
88 		dev_err(par->info->device,
89 			"write() failed and returned %d\n", ret);
90 		return;
91 	}
92 	len--;
93 
94 	if (len) {
95 		i = len;
96 		while (i--)
97 			*buf++ = (u8)va_arg(args, unsigned int);
98 		ret = par->fbtftops.write(par, par->buf, len * (sizeof(u8)));
99 		if (ret < 0) {
100 			va_end(args);
101 			dev_err(par->info->device,
102 				"write() failed and returned %d\n", ret);
103 			return;
104 		}
105 	}
106 	gpiod_set_value(par->gpio.dc, 1);
107 	va_end(args);
108 }
109 
110 /*
111  * Grayscale Lookup Table
112  * GS1 - GS63
113  * The driver Gamma curve contains the relative values between the entries
114  * in the Lookup table.
115  *
116  * From datasheet:
117  * 8.8 Gray Scale Decoder
118  *
119  * there are total 180 Gamma Settings (Setting 0 to Setting 180)
120  * available for the Gray Scale table.
121  *
122  * The gray scale is defined in incremental way, with reference
123  * to the length of previous table entry:
124  * Setting of GS1 has to be >= 0
125  * Setting of GS2 has to be > Setting of GS1 +1
126  * Setting of GS3 has to be > Setting of GS2 +1
127  * :
128  * Setting of GS63 has to be > Setting of GS62 +1
129  *
130  */
set_gamma(struct fbtft_par * par,u32 * curves)131 static int set_gamma(struct fbtft_par *par, u32 *curves)
132 {
133 	unsigned long tmp[GAMMA_NUM * GAMMA_LEN];
134 	int i, acc = 0;
135 
136 	for (i = 0; i < 63; i++) {
137 		if (i > 0 && curves[i] < 2) {
138 			dev_err(par->info->device,
139 				"Illegal value in Grayscale Lookup Table at index %d. Must be greater than 1\n",
140 				i);
141 			return -EINVAL;
142 		}
143 		acc += curves[i];
144 		tmp[i] = acc;
145 		if (acc > 180) {
146 			dev_err(par->info->device,
147 				"Illegal value(s) in Grayscale Lookup Table. At index=%d, the accumulated value has exceeded 180\n",
148 				i);
149 			return -EINVAL;
150 		}
151 	}
152 
153 	write_reg(par, 0xB8,
154 		  tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6],
155 		  tmp[7], tmp[8], tmp[9], tmp[10], tmp[11], tmp[12], tmp[13],
156 		  tmp[14], tmp[15], tmp[16], tmp[17], tmp[18], tmp[19], tmp[20],
157 		  tmp[21], tmp[22], tmp[23], tmp[24], tmp[25], tmp[26],	tmp[27],
158 		  tmp[28], tmp[29], tmp[30], tmp[31], tmp[32], tmp[33], tmp[34],
159 		  tmp[35], tmp[36], tmp[37], tmp[38], tmp[39], tmp[40], tmp[41],
160 		  tmp[42], tmp[43], tmp[44], tmp[45], tmp[46], tmp[47], tmp[48],
161 		  tmp[49], tmp[50], tmp[51], tmp[52], tmp[53], tmp[54], tmp[55],
162 		  tmp[56], tmp[57], tmp[58], tmp[59], tmp[60], tmp[61],
163 		  tmp[62]);
164 
165 	return 0;
166 }
167 
blank(struct fbtft_par * par,bool on)168 static int blank(struct fbtft_par *par, bool on)
169 {
170 	fbtft_par_dbg(DEBUG_BLANK, par, "(%s=%s)\n",
171 		      __func__, on ? "true" : "false");
172 	if (on)
173 		write_reg(par, 0xAE);
174 	else
175 		write_reg(par, 0xAF);
176 	return 0;
177 }
178 
179 static struct fbtft_display display = {
180 	.regwidth = 8,
181 	.width = WIDTH,
182 	.height = HEIGHT,
183 	.gamma_num = GAMMA_NUM,
184 	.gamma_len = GAMMA_LEN,
185 	.gamma = DEFAULT_GAMMA,
186 	.fbtftops = {
187 		.write_register = write_reg8_bus8,
188 		.init_display = init_display,
189 		.set_addr_win = set_addr_win,
190 		.set_gamma = set_gamma,
191 		.blank = blank,
192 	},
193 };
194 
195 FBTFT_REGISTER_DRIVER(DRVNAME, "solomon,ssd1331", &display);
196 
197 MODULE_ALIAS("spi:" DRVNAME);
198 MODULE_ALIAS("platform:" DRVNAME);
199 MODULE_ALIAS("spi:ssd1331");
200 MODULE_ALIAS("platform:ssd1331");
201 
202 MODULE_DESCRIPTION("SSD1331 OLED Driver");
203 MODULE_AUTHOR("Alec Smecher (adapted from SSD1351 by James Davies)");
204 MODULE_LICENSE("GPL");
205