1 // SPDX-License-Identifier: GPL-2.0+
3 * FB driver for Two KS0108 LCD controllers in AGM1264K-FL display
5 * Copyright (C) 2014 ololoshka2871
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
17 /* Uncomment text line to use negative image on display */
23 #define DRVNAME "fb_agm1264k-fl"
26 #define TOTALWIDTH (WIDTH * 2) /* because 2 x ks0108 in one display */
31 #define RW gpio.aux[2]
32 #define CS0 gpio.aux[0]
33 #define CS1 gpio.aux[1]
35 /* diffusing error (Floyd-Steinberg) */
36 #define DIFFUSING_MATRIX_WIDTH 2
37 #define DIFFUSING_MATRIX_HEIGHT 2
39 static const signed char
40 diffusing_matrix[DIFFUSING_MATRIX_WIDTH][DIFFUSING_MATRIX_HEIGHT] = {
45 static const unsigned char gamma_correction_table[] = {
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
47 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6,
48 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 13,
49 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21,
50 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32,
51 33, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 43, 44, 45,
52 46, 47, 48, 49, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
53 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 81,
54 82, 83, 84, 85, 87, 88, 89, 90, 91, 93, 94, 95, 97, 98, 99, 100, 102,
55 103, 105, 106, 107, 109, 110, 111, 113, 114, 116, 117, 119, 120, 121,
56 123, 124, 126, 127, 129, 130, 132, 133, 135, 137, 138, 140, 141, 143,
57 145, 146, 148, 149, 151, 153, 154, 156, 158, 159, 161, 163, 165, 166,
58 168, 170, 172, 173, 175, 177, 179, 181, 182, 184, 186, 188, 190, 192,
59 194, 196, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219,
60 221, 223, 225, 227, 229, 231, 234, 236, 238, 240, 242, 244, 246, 248,
64 static int init_display(struct fbtft_par *par)
68 par->fbtftops.reset(par);
70 for (i = 0; i < 2; ++i) {
71 write_reg(par, i, 0x3f); /* display on */
72 write_reg(par, i, 0x40); /* set x to 0 */
73 write_reg(par, i, 0xb0); /* set page to 0 */
74 write_reg(par, i, 0xc0); /* set start line to 0 */
80 static void reset(struct fbtft_par *par)
85 dev_dbg(par->info->device, "%s()\n", __func__);
87 gpiod_set_value(par->gpio.reset, 1);
89 gpiod_set_value(par->gpio.reset, 0);
93 /* Check if all necessary GPIOS defined */
94 static int verify_gpios(struct fbtft_par *par)
98 dev_dbg(par->info->device,
102 dev_err(par->info->device,
103 "Missing info about 'wr' (aka E) gpio. Aborting.\n");
106 for (i = 0; i < 8; ++i) {
107 if (!par->gpio.db[i]) {
108 dev_err(par->info->device,
109 "Missing info about 'db[%i]' gpio. Aborting.\n",
115 dev_err(par->info->device,
116 "Missing info about 'cs0' gpio. Aborting.\n");
120 dev_err(par->info->device,
121 "Missing info about 'cs1' gpio. Aborting.\n");
125 dev_err(par->info->device,
126 "Missing info about 'rw' gpio. Aborting.\n");
134 request_gpios_match(struct fbtft_par *par, const struct fbtft_gpio *gpio)
136 dev_dbg(par->info->device,
137 "%s('%s')\n", __func__, gpio->name);
139 if (strcasecmp(gpio->name, "wr") == 0) {
140 /* left ks0108 E pin */
141 par->EPIN = gpio->gpio;
142 return GPIOD_OUT_LOW;
143 } else if (strcasecmp(gpio->name, "cs0") == 0) {
144 /* left ks0108 controller pin */
145 par->CS0 = gpio->gpio;
146 return GPIOD_OUT_HIGH;
147 } else if (strcasecmp(gpio->name, "cs1") == 0) {
148 /* right ks0108 controller pin */
149 par->CS1 = gpio->gpio;
150 return GPIOD_OUT_HIGH;
153 /* if write (rw = 0) e(1->0) perform write */
154 /* if read (rw = 1) e(0->1) set data on D0-7*/
155 else if (strcasecmp(gpio->name, "rw") == 0) {
156 par->RW = gpio->gpio;
157 return GPIOD_OUT_LOW;
160 return FBTFT_GPIO_NO_MATCH;
163 /* This function oses to enter commands
164 * first byte - destination controller 0 or 1
165 * following - commands
167 static void write_reg8_bus8(struct fbtft_par *par, int len, ...)
173 if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) {
175 for (i = 0; i < len; i++)
176 buf[i] = (u8)va_arg(args, unsigned int);
179 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, par->info->device,
180 u8, buf, len, "%s: ", __func__);
185 *buf = (u8)va_arg(args, unsigned int);
189 dev_err(par->info->device,
190 "Incorrect chip select request (%d)\n", *buf);
197 gpiod_set_value(par->CS0, 0);
198 gpiod_set_value(par->CS1, 1);
201 gpiod_set_value(par->CS0, 1);
202 gpiod_set_value(par->CS1, 0);
205 gpiod_set_value(par->RS, 0); /* RS->0 (command mode) */
211 *buf++ = (u8)va_arg(args, unsigned int);
212 ret = par->fbtftops.write(par, par->buf, len * (sizeof(u8)));
215 dev_err(par->info->device,
216 "write() failed and returned %d\n", ret);
226 int xs, ys_page, xe, ye_page;
229 /* save display writing zone */
230 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
233 addr_win.ys_page = ys / 8;
235 addr_win.ye_page = ye / 8;
239 construct_line_bitmap(struct fbtft_par *par, u8 *dest, signed short *src,
240 int xs, int xe, int y)
244 for (x = xs; x < xe; ++x) {
247 for (i = 0; i < 8; i++)
248 if (src[(y * 8 + i) * par->info->var.xres + x])
258 static void iterate_diffusion_matrix(u32 xres, u32 yres, int x,
259 int y, signed short *convert_buf,
260 signed short pixel, signed short error)
264 /* diffusion matrix row */
265 for (i = 0; i < DIFFUSING_MATRIX_WIDTH; ++i)
266 /* diffusion matrix column */
267 for (j = 0; j < DIFFUSING_MATRIX_HEIGHT; ++j) {
268 signed short *write_pos;
271 /* skip pixels out of zone */
272 if (x + i < 0 || x + i >= xres || y + j >= yres)
274 write_pos = &convert_buf[(y + j) * xres + x + i];
275 coeff = diffusing_matrix[i][j];
280 signed short p = *write_pos + error * coeff;
291 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
293 u16 *vmem16 = (u16 *)par->info->screen_buffer;
294 u8 *buf = par->txbuf.buf;
298 /* buffer to convert RGB565 -> grayscale16 -> Dithered image 1bpp */
299 signed short *convert_buf = kmalloc_array(par->info->var.xres *
300 par->info->var.yres, sizeof(signed short), GFP_NOIO);
305 /* converting to grayscale16 */
306 for (x = 0; x < par->info->var.xres; ++x)
307 for (y = 0; y < par->info->var.yres; ++y) {
308 u16 pixel = vmem16[y * par->info->var.xres + x];
309 u16 b = pixel & 0x1f;
310 u16 g = (pixel & (0x3f << 5)) >> 5;
311 u16 r = (pixel & (0x1f << (5 + 6))) >> (5 + 6);
313 pixel = (299 * r + 587 * g + 114 * b) / 200;
317 /* gamma-correction by table */
318 convert_buf[y * par->info->var.xres + x] =
319 (signed short)gamma_correction_table[pixel];
322 /* Image Dithering */
323 for (x = 0; x < par->info->var.xres; ++x)
324 for (y = 0; y < par->info->var.yres; ++y) {
326 convert_buf[y * par->info->var.xres + x];
327 signed short error_b = pixel - BLACK;
328 signed short error_w = pixel - WHITE;
331 /* what color close? */
332 if (abs(error_b) >= abs(error_w)) {
344 iterate_diffusion_matrix(par->info->var.xres,
350 /* 1 string = 2 pages */
351 for (y = addr_win.ys_page; y <= addr_win.ye_page; ++y) {
352 /* left half of display */
353 if (addr_win.xs < par->info->var.xres / 2) {
354 construct_line_bitmap(par, buf, convert_buf,
356 par->info->var.xres / 2, y);
358 len = par->info->var.xres / 2 - addr_win.xs;
360 /* select left side (sc0)
363 write_reg(par, 0x00, BIT(6) | (u8)addr_win.xs);
364 write_reg(par, 0x00, (0x17 << 3) | (u8)y);
367 gpiod_set_value(par->RS, 1); /* RS->1 (data mode) */
368 ret = par->fbtftops.write(par, buf, len);
370 dev_err(par->info->device,
371 "write failed and returned: %d\n",
374 /* right half of display */
375 if (addr_win.xe >= par->info->var.xres / 2) {
376 construct_line_bitmap(par, buf,
378 par->info->var.xres / 2,
381 len = addr_win.xe + 1 - par->info->var.xres / 2;
383 /* select right side (sc1)
386 write_reg(par, 0x01, BIT(6));
387 write_reg(par, 0x01, (0x17 << 3) | (u8)y);
390 gpiod_set_value(par->RS, 1); /* RS->1 (data mode) */
391 par->fbtftops.write(par, buf, len);
393 dev_err(par->info->device,
394 "write failed and returned: %d\n",
400 gpiod_set_value(par->CS0, 0);
401 gpiod_set_value(par->CS1, 0);
406 static int write(struct fbtft_par *par, void *buf, size_t len)
408 fbtft_par_dbg_hex(DEBUG_WRITE, par, par->info->device, u8, buf, len,
409 "%s(len=%zu): ", __func__, len);
411 gpiod_set_value(par->RW, 0); /* set write mode */
419 for (i = 0; i < 8; ++i)
420 gpiod_set_value(par->gpio.db[i], data & (1 << i));
422 gpiod_set_value(par->EPIN, 0);
424 /* unset E - write */
425 gpiod_set_value(par->EPIN, 1);
432 static struct fbtft_display display = {
438 .init_display = init_display,
439 .set_addr_win = set_addr_win,
440 .verify_gpios = verify_gpios,
441 .request_gpios_match = request_gpios_match,
444 .write_register = write_reg8_bus8,
445 .write_vmem = write_vmem,
449 FBTFT_REGISTER_DRIVER(DRVNAME, "displaytronic,fb_agm1264k-fl", &display);
451 MODULE_ALIAS("platform:" DRVNAME);
453 MODULE_DESCRIPTION("Two KS0108 LCD controllers in AGM1264K-FL display");
454 MODULE_AUTHOR("ololoshka2871");
455 MODULE_LICENSE("GPL");