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