1 // SPDX-License-Identifier: GPL-2.0+
3 * FB driver for the SSD1325 OLED Controller
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>
14 #define DRVNAME "fb_ssd1325"
20 #define DEFAULT_GAMMA "7 1 1 1 1 2 2 3 3 4 4 5 5 6 6"
25 * This doesn't work because D/C has to be LOW for both values:
26 * write_reg(par, val1, val2);
29 * write_reg(par, val1);
30 * write_reg(par, val2);
33 /* Init sequence taken from the Adafruit SSD1306 Arduino library */
34 static int init_display(struct fbtft_par *par)
36 par->fbtftops.reset(par);
61 static uint8_t rgb565_to_g16(u16 pixel)
64 u16 g = (pixel & (0x3f << 5)) >> 5;
65 u16 r = (pixel & (0x1f << (5 + 6))) >> (5 + 6);
67 pixel = (299 * r + 587 * g + 114 * b) / 195;
70 return (uint8_t)pixel / 16;
73 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
75 fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
76 "%s(xs=%d, ys=%d, xe=%d, ye=%d)\n", __func__, xs, ys, xe,
87 static int blank(struct fbtft_par *par, bool on)
89 fbtft_par_dbg(DEBUG_BLANK, par, "(%s=%s)\n",
90 __func__, on ? "true" : "false");
100 * Grayscale Lookup Table
102 * The "Gamma curve" contains the relative values between the entries
103 * in the Lookup table.
105 * 0 = Setting of GS1 < Setting of GS2 < Setting of GS3.....<
106 * Setting of GS14 < Setting of GS15
108 static int set_gamma(struct fbtft_par *par, u32 *curves)
112 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
114 for (i = 0; i < GAMMA_LEN; i++) {
115 if (i > 0 && curves[i] < 1) {
116 dev_err(par->info->device,
117 "Illegal value in Grayscale Lookup Table at index %d.\n"
118 "Must be greater than 0\n", i);
122 dev_err(par->info->device,
123 "Illegal value(s) in Grayscale Lookup Table.\n"
124 "At index=%d, the accumulated value has exceeded 7\n",
129 write_reg(par, 0xB8);
130 for (i = 0; i < 8; i++)
131 write_reg(par, (curves[i] & 0xFF));
135 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
137 u16 *vmem16 = (u16 *)par->info->screen_buffer;
138 u8 *buf = par->txbuf.buf;
144 for (x = 0; x < par->info->var.xres; x++) {
147 for (y = 0; y < par->info->var.yres; y++) {
148 n1 = rgb565_to_g16(vmem16[y * par->info->var.xres + x]);
149 n2 = rgb565_to_g16(vmem16
150 [y * par->info->var.xres + x + 1]);
151 *buf = (n1 << 4) | n2;
156 gpiod_set_value(par->gpio.dc, 1);
159 ret = par->fbtftops.write(par, par->txbuf.buf,
160 par->info->var.xres * par->info->var.yres / 2);
162 dev_err(par->info->device,
163 "%s: write failed and returned: %d\n", __func__, ret);
168 static struct fbtft_display display = {
172 .txbuflen = WIDTH * HEIGHT / 2,
173 .gamma_num = GAMMA_NUM,
174 .gamma_len = GAMMA_LEN,
175 .gamma = DEFAULT_GAMMA,
177 .write_vmem = write_vmem,
178 .init_display = init_display,
179 .set_addr_win = set_addr_win,
181 .set_gamma = set_gamma,
185 FBTFT_REGISTER_DRIVER(DRVNAME, "solomon,ssd1325", &display);
187 MODULE_ALIAS("spi:" DRVNAME);
188 MODULE_ALIAS("platform:" DRVNAME);
189 MODULE_ALIAS("spi:ssd1325");
190 MODULE_ALIAS("platform:ssd1325");
192 MODULE_DESCRIPTION("SSD1325 OLED Driver");
193 MODULE_AUTHOR("Alexey Mednyy");
194 MODULE_LICENSE("GPL");