xref: /linux/drivers/staging/fbtft/fb_ssd1325.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * FB driver for the SSD1325 OLED Controller
4  */
5 
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/delay.h>
11 
12 #include "fbtft.h"
13 
14 #define DRVNAME		"fb_ssd1325"
15 
16 #define WIDTH 128
17 #define HEIGHT 64
18 #define GAMMA_NUM   1
19 #define GAMMA_LEN   15
20 #define DEFAULT_GAMMA "7 1 1 1 1 2 2 3 3 4 4 5 5 6 6"
21 
22 /*
23  * write_reg() caveat:
24  *
25  *    This doesn't work because D/C has to be LOW for both values:
26  *      write_reg(par, val1, val2);
27  *
28  *    Do it like this:
29  *      write_reg(par, val1);
30  *      write_reg(par, val2);
31  */
32 
33 /* Init sequence taken from the Adafruit SSD1306 Arduino library */
init_display(struct fbtft_par * par)34 static int init_display(struct fbtft_par *par)
35 {
36 	par->fbtftops.reset(par);
37 
38 	write_reg(par, 0xb3);
39 	write_reg(par, 0xf0);
40 	write_reg(par, 0xae);
41 	write_reg(par, 0xa1);
42 	write_reg(par, 0x00);
43 	write_reg(par, 0xa8);
44 	write_reg(par, 0x3f);
45 	write_reg(par, 0xa0);
46 	write_reg(par, 0x45);
47 	write_reg(par, 0xa2);
48 	write_reg(par, 0x40);
49 	write_reg(par, 0x75);
50 	write_reg(par, 0x00);
51 	write_reg(par, 0x3f);
52 	write_reg(par, 0x15);
53 	write_reg(par, 0x00);
54 	write_reg(par, 0x7f);
55 	write_reg(par, 0xa4);
56 	write_reg(par, 0xaf);
57 
58 	return 0;
59 }
60 
rgb565_to_g16(u16 pixel)61 static uint8_t rgb565_to_g16(u16 pixel)
62 {
63 	u16 b = pixel & 0x1f;
64 	u16 g = (pixel & (0x3f << 5)) >> 5;
65 	u16 r = (pixel & (0x1f << (5 + 6))) >> (5 + 6);
66 
67 	pixel = (299 * r + 587 * g + 114 * b) / 195;
68 	if (pixel > 255)
69 		pixel = 255;
70 	return (uint8_t)pixel / 16;
71 }
72 
set_addr_win(struct fbtft_par * par,int xs,int ys,int xe,int ye)73 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
74 {
75 	write_reg(par, 0x75);
76 	write_reg(par, 0x00);
77 	write_reg(par, 0x3f);
78 	write_reg(par, 0x15);
79 	write_reg(par, 0x00);
80 	write_reg(par, 0x7f);
81 }
82 
blank(struct fbtft_par * par,bool on)83 static int blank(struct fbtft_par *par, bool on)
84 {
85 	if (on)
86 		write_reg(par, 0xAE);
87 	else
88 		write_reg(par, 0xAF);
89 	return 0;
90 }
91 
92 /*
93  * Grayscale Lookup Table
94  * GS1 - GS15
95  * The "Gamma curve" contains the relative values between the entries
96  * in the Lookup table.
97  *
98  * 0 = Setting of GS1 < Setting of GS2 < Setting of GS3.....<
99  * Setting of GS14 < Setting of GS15
100  */
set_gamma(struct fbtft_par * par,u32 * curves)101 static int set_gamma(struct fbtft_par *par, u32 *curves)
102 {
103 	int i;
104 
105 	for (i = 0; i < GAMMA_LEN; i++) {
106 		if (i > 0 && curves[i] < 1) {
107 			dev_err(par->info->device,
108 				"Illegal value in Grayscale Lookup Table at index %d.\n"
109 				"Must be greater than 0\n", i);
110 			return -EINVAL;
111 		}
112 		if (curves[i] > 7) {
113 			dev_err(par->info->device,
114 				"Illegal value(s) in Grayscale Lookup Table.\n"
115 				"At index=%d, the accumulated value has exceeded 7\n",
116 				i);
117 			return -EINVAL;
118 		}
119 	}
120 	write_reg(par, 0xB8);
121 	for (i = 0; i < 8; i++)
122 		write_reg(par, (curves[i] & 0xFF));
123 	return 0;
124 }
125 
write_vmem(struct fbtft_par * par,size_t offset,size_t len)126 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
127 {
128 	u16 *vmem16 = (u16 *)par->info->screen_buffer;
129 	u8 *buf = par->txbuf.buf;
130 	u8 n1;
131 	u8 n2;
132 	int y, x;
133 	int ret;
134 
135 	for (x = 0; x < par->info->var.xres; x++) {
136 		if (x % 2)
137 			continue;
138 		for (y = 0; y < par->info->var.yres; y++) {
139 			n1 = rgb565_to_g16(vmem16[y * par->info->var.xres + x]);
140 			n2 = rgb565_to_g16(vmem16
141 					   [y * par->info->var.xres + x + 1]);
142 			*buf = (n1 << 4) | n2;
143 			buf++;
144 		}
145 	}
146 
147 	gpiod_set_value(par->gpio.dc, 1);
148 
149 	/* Write data */
150 	ret = par->fbtftops.write(par, par->txbuf.buf,
151 				par->info->var.xres * par->info->var.yres / 2);
152 	if (ret < 0)
153 		dev_err(par->info->device,
154 			"%s: write failed and returned: %d\n", __func__, ret);
155 
156 	return ret;
157 }
158 
159 static struct fbtft_display display = {
160 	.regwidth = 8,
161 	.width = WIDTH,
162 	.height = HEIGHT,
163 	.txbuflen = WIDTH * HEIGHT / 2,
164 	.gamma_num = GAMMA_NUM,
165 	.gamma_len = GAMMA_LEN,
166 	.gamma = DEFAULT_GAMMA,
167 	.fbtftops = {
168 		.write_vmem = write_vmem,
169 		.init_display = init_display,
170 		.set_addr_win = set_addr_win,
171 		.blank = blank,
172 		.set_gamma = set_gamma,
173 	},
174 };
175 
176 FBTFT_REGISTER_DRIVER(DRVNAME, "solomon,ssd1325", &display);
177 
178 MODULE_ALIAS("spi:" DRVNAME);
179 MODULE_ALIAS("platform:" DRVNAME);
180 MODULE_ALIAS("spi:ssd1325");
181 MODULE_ALIAS("platform:ssd1325");
182 
183 MODULE_DESCRIPTION("SSD1325 OLED Driver");
184 MODULE_AUTHOR("Alexey Mednyy");
185 MODULE_LICENSE("GPL");
186