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 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 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 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 if (par->gpio.dc) 85 gpiod_set_value(par->gpio.dc, 0); 86 ret = par->fbtftops.write(par, par->buf, sizeof(u8)); 87 if (ret < 0) { 88 va_end(args); 89 dev_err(par->info->device, 90 "write() failed and returned %d\n", ret); 91 return; 92 } 93 len--; 94 95 if (len) { 96 i = len; 97 while (i--) 98 *buf++ = (u8)va_arg(args, unsigned int); 99 ret = par->fbtftops.write(par, par->buf, len * (sizeof(u8))); 100 if (ret < 0) { 101 va_end(args); 102 dev_err(par->info->device, 103 "write() failed and returned %d\n", ret); 104 return; 105 } 106 } 107 if (par->gpio.dc) 108 gpiod_set_value(par->gpio.dc, 1); 109 va_end(args); 110 } 111 112 /* 113 * Grayscale Lookup Table 114 * GS1 - GS63 115 * The driver Gamma curve contains the relative values between the entries 116 * in the Lookup table. 117 * 118 * From datasheet: 119 * 8.8 Gray Scale Decoder 120 * 121 * there are total 180 Gamma Settings (Setting 0 to Setting 180) 122 * available for the Gray Scale table. 123 * 124 * The gray scale is defined in incremental way, with reference 125 * to the length of previous table entry: 126 * Setting of GS1 has to be >= 0 127 * Setting of GS2 has to be > Setting of GS1 +1 128 * Setting of GS3 has to be > Setting of GS2 +1 129 * : 130 * Setting of GS63 has to be > Setting of GS62 +1 131 * 132 */ 133 static int set_gamma(struct fbtft_par *par, u32 *curves) 134 { 135 unsigned long tmp[GAMMA_NUM * GAMMA_LEN]; 136 int i, acc = 0; 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. Must be greater than 1\n", 142 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. At index=%d, the accumulated value has exceeded 180\n", 150 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], 157 tmp[7], tmp[8], tmp[9], tmp[10], tmp[11], tmp[12], tmp[13], 158 tmp[14], tmp[15], tmp[16], tmp[17], tmp[18], tmp[19], tmp[20], 159 tmp[21], tmp[22], tmp[23], tmp[24], tmp[25], tmp[26], tmp[27], 160 tmp[28], tmp[29], tmp[30], tmp[31], tmp[32], tmp[33], tmp[34], 161 tmp[35], tmp[36], tmp[37], tmp[38], tmp[39], tmp[40], tmp[41], 162 tmp[42], tmp[43], tmp[44], tmp[45], tmp[46], tmp[47], tmp[48], 163 tmp[49], tmp[50], tmp[51], tmp[52], tmp[53], tmp[54], tmp[55], 164 tmp[56], tmp[57], tmp[58], tmp[59], tmp[60], tmp[61], 165 tmp[62]); 166 167 return 0; 168 } 169 170 static int blank(struct fbtft_par *par, bool on) 171 { 172 fbtft_par_dbg(DEBUG_BLANK, par, "(%s=%s)\n", 173 __func__, on ? "true" : "false"); 174 if (on) 175 write_reg(par, 0xAE); 176 else 177 write_reg(par, 0xAF); 178 return 0; 179 } 180 181 static struct fbtft_display display = { 182 .regwidth = 8, 183 .width = WIDTH, 184 .height = HEIGHT, 185 .gamma_num = GAMMA_NUM, 186 .gamma_len = GAMMA_LEN, 187 .gamma = DEFAULT_GAMMA, 188 .fbtftops = { 189 .write_register = write_reg8_bus8, 190 .init_display = init_display, 191 .set_addr_win = set_addr_win, 192 .set_gamma = set_gamma, 193 .blank = blank, 194 }, 195 }; 196 197 FBTFT_REGISTER_DRIVER(DRVNAME, "solomon,ssd1331", &display); 198 199 MODULE_ALIAS("spi:" DRVNAME); 200 MODULE_ALIAS("platform:" DRVNAME); 201 MODULE_ALIAS("spi:ssd1331"); 202 MODULE_ALIAS("platform:ssd1331"); 203 204 MODULE_DESCRIPTION("SSD1331 OLED Driver"); 205 MODULE_AUTHOR("Alec Smecher (adapted from SSD1351 by James Davies)"); 206 MODULE_LICENSE("GPL"); 207