2 * FB driver for Two KS0108 LCD controllers in AGM1264K-FL display
4 * Copyright (C) 2014 ololoshka2871
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/gpio.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
26 /* Uncomment text line to use negative image on display */
32 #define DRVNAME "fb_agm1264k-fl"
35 #define TOTALWIDTH (WIDTH * 2) /* because 2 x ks0108 in one display */
40 #define RW gpio.aux[2]
41 #define CS0 gpio.aux[0]
42 #define CS1 gpio.aux[1]
45 /* diffusing error (
\93Floyd-Steinberg
\94) */
46 #define DIFFUSING_MATRIX_WIDTH 2
47 #define DIFFUSING_MATRIX_HEIGHT 2
49 static const signed char
50 diffusing_matrix[DIFFUSING_MATRIX_WIDTH][DIFFUSING_MATRIX_HEIGHT] = {
55 static const unsigned char gamma_correction_table[] = {
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
57 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6,
58 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 13,
59 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21,
60 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32,
61 33, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 43, 44, 45,
62 46, 47, 48, 49, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
63 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 81,
64 82, 83, 84, 85, 87, 88, 89, 90, 91, 93, 94, 95, 97, 98, 99, 100, 102,
65 103, 105, 106, 107, 109, 110, 111, 113, 114, 116, 117, 119, 120, 121,
66 123, 124, 126, 127, 129, 130, 132, 133, 135, 137, 138, 140, 141, 143,
67 145, 146, 148, 149, 151, 153, 154, 156, 158, 159, 161, 163, 165, 166,
68 168, 170, 172, 173, 175, 177, 179, 181, 182, 184, 186, 188, 190, 192,
69 194, 196, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219,
70 221, 223, 225, 227, 229, 231, 234, 236, 238, 240, 242, 244, 246, 248,
74 static int init_display(struct fbtft_par *par)
78 fbtft_par_dbg(DEBUG_INIT_DISPLAY, par, "%s()\n", __func__);
80 par->fbtftops.reset(par);
82 for (i = 0; i < 2; ++i) {
83 write_reg(par, i, 0x3f); /* display on */
84 write_reg(par, i, 0x40); /* set x to 0 */
85 write_reg(par, i, 0xb0); /* set page to 0 */
86 write_reg(par, i, 0xc0); /* set start line to 0 */
92 static void reset(struct fbtft_par *par)
94 if (par->gpio.reset == -1)
97 dev_dbg(par->info->device, "%s()\n", __func__);
99 gpio_set_value(par->gpio.reset, 0);
101 gpio_set_value(par->gpio.reset, 1);
105 /* Check if all necessary GPIOS defined */
106 static int verify_gpios(struct fbtft_par *par)
110 dev_dbg(par->info->device,
114 dev_err(par->info->device,
115 "Missing info about 'wr' (aka E) gpio. Aborting.\n");
118 for (i = 0; i < 8; ++i) {
119 if (par->gpio.db[i] < 0) {
120 dev_err(par->info->device,
121 "Missing info about 'db[%i]' gpio. Aborting.\n",
127 dev_err(par->info->device,
128 "Missing info about 'cs0' gpio. Aborting.\n");
132 dev_err(par->info->device,
133 "Missing info about 'cs1' gpio. Aborting.\n");
137 dev_err(par->info->device,
138 "Missing info about 'rw' gpio. Aborting.\n");
146 request_gpios_match(struct fbtft_par *par, const struct fbtft_gpio *gpio)
148 dev_dbg(par->info->device,
149 "%s('%s')\n", __func__, gpio->name);
151 if (strcasecmp(gpio->name, "wr") == 0) {
152 /* left ks0108 E pin */
153 par->EPIN = gpio->gpio;
154 return GPIOF_OUT_INIT_LOW;
155 } else if (strcasecmp(gpio->name, "cs0") == 0) {
156 /* left ks0108 controller pin */
157 par->CS0 = gpio->gpio;
158 return GPIOF_OUT_INIT_HIGH;
159 } else if (strcasecmp(gpio->name, "cs1") == 0) {
160 /* right ks0108 controller pin */
161 par->CS1 = gpio->gpio;
162 return GPIOF_OUT_INIT_HIGH;
165 /* if write (rw = 0) e(1->0) perform write */
166 /* if read (rw = 1) e(0->1) set data on D0-7*/
167 else if (strcasecmp(gpio->name, "rw") == 0) {
168 par->RW = gpio->gpio;
169 return GPIOF_OUT_INIT_LOW;
172 return FBTFT_GPIO_NO_MATCH;
175 /* This function oses to enter commands
176 * first byte - destination controller 0 or 1
177 * following - commands
179 static void write_reg8_bus8(struct fbtft_par *par, int len, ...)
183 u8 *buf = (u8 *)par->buf;
185 if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) {
187 for (i = 0; i < len; i++)
188 buf[i] = (u8)va_arg(args, unsigned int);
191 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par,
192 par->info->device, u8, buf, len, "%s: ", __func__);
197 *buf = (u8)va_arg(args, unsigned int);
201 dev_err(par->info->device,
202 "Incorrect chip select request (%d)\n", *buf);
209 gpio_set_value(par->CS0, 1);
210 gpio_set_value(par->CS1, 0);
213 gpio_set_value(par->CS0, 0);
214 gpio_set_value(par->CS1, 1);
217 gpio_set_value(par->RS, 0); /* RS->0 (command mode) */
223 *buf++ = (u8)va_arg(args, unsigned int);
224 ret = par->fbtftops.write(par, par->buf, len * (sizeof(u8)));
227 dev_err(par->info->device,
228 "write() failed and returned %d\n", ret);
238 int xs, ys_page, xe, ye_page;
241 /* save display writing zone */
242 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
245 addr_win.ys_page = ys / 8;
247 addr_win.ye_page = ye / 8;
249 fbtft_par_dbg(DEBUG_SET_ADDR_WIN, par,
250 "%s(xs=%d, ys_page=%d, xe=%d, ye_page=%d)\n", __func__,
251 addr_win.xs, addr_win.ys_page, addr_win.xe, addr_win.ye_page);
255 construct_line_bitmap(struct fbtft_par *par, u8 *dest, signed short *src,
256 int xs, int xe, int y)
260 for (x = xs; x < xe; ++x) {
263 for (i = 0; i < 8; i++)
264 if (src[(y * 8 + i) * par->info->var.xres + x])
274 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
276 u16 *vmem16 = (u16 *)par->info->screen_base;
277 u8 *buf = par->txbuf.buf;
281 /* buffer to convert RGB565 -> grayscale16 -> Dithered image 1bpp */
282 signed short *convert_buf = kmalloc(par->info->var.xres *
283 par->info->var.yres * sizeof(signed short), GFP_NOIO);
288 fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s()\n", __func__);
290 /* converting to grayscale16 */
291 for (x = 0; x < par->info->var.xres; ++x)
292 for (y = 0; y < par->info->var.yres; ++y) {
293 u16 pixel = vmem16[y * par->info->var.xres + x];
294 u16 b = pixel & 0x1f;
295 u16 g = (pixel & (0x3f << 5)) >> 5;
296 u16 r = (pixel & (0x1f << (5 + 6))) >> (5 + 6);
298 pixel = (299 * r + 587 * g + 114 * b) / 200;
302 /* gamma-correction by table */
303 convert_buf[y * par->info->var.xres + x] =
304 (signed short)gamma_correction_table[pixel];
307 /* Image Dithering */
308 for (x = 0; x < par->info->var.xres; ++x)
309 for (y = 0; y < par->info->var.yres; ++y) {
311 convert_buf[y * par->info->var.xres + x];
312 signed short error_b = pixel - BLACK;
313 signed short error_w = pixel - WHITE;
317 /* what color close? */
318 if (abs(error_b) >= abs(error_w)) {
330 /* diffusion matrix row */
331 for (i = 0; i < DIFFUSING_MATRIX_WIDTH; ++i)
332 /* diffusion matrix column */
333 for (j = 0; j < DIFFUSING_MATRIX_HEIGHT; ++j) {
334 signed short *write_pos;
337 /* skip pixels out of zone */
339 x + i >= par->info->var.xres
340 || y + j >= par->info->var.yres)
342 write_pos = &convert_buf[
343 (y + j) * par->info->var.xres +
345 coeff = diffusing_matrix[i][j];
350 signed short p = *write_pos +
362 /* 1 string = 2 pages */
363 for (y = addr_win.ys_page; y <= addr_win.ye_page; ++y) {
364 /* left half of display */
365 if (addr_win.xs < par->info->var.xres / 2) {
366 construct_line_bitmap(par, buf, convert_buf,
367 addr_win.xs, par->info->var.xres / 2, y);
369 len = par->info->var.xres / 2 - addr_win.xs;
371 /* select left side (sc0)
374 write_reg(par, 0x00, (1 << 6) | (u8)addr_win.xs);
375 write_reg(par, 0x00, (0x17 << 3) | (u8)y);
378 gpio_set_value(par->RS, 1); /* RS->1 (data mode) */
379 ret = par->fbtftops.write(par, buf, len);
381 dev_err(par->info->device,
382 "write failed and returned: %d\n",
385 /* right half of display */
386 if (addr_win.xe >= par->info->var.xres / 2) {
387 construct_line_bitmap(par, buf,
388 convert_buf, par->info->var.xres / 2,
391 len = addr_win.xe + 1 - par->info->var.xres / 2;
393 /* select right side (sc1)
396 write_reg(par, 0x01, 1 << 6);
397 write_reg(par, 0x01, (0x17 << 3) | (u8)y);
400 gpio_set_value(par->RS, 1); /* RS->1 (data mode) */
401 par->fbtftops.write(par, buf, len);
403 dev_err(par->info->device,
404 "write failed and returned: %d\n",
410 gpio_set_value(par->CS0, 1);
411 gpio_set_value(par->CS1, 1);
416 static int write(struct fbtft_par *par, void *buf, size_t len)
418 fbtft_par_dbg_hex(DEBUG_WRITE, par, par->info->device, u8, buf, len,
419 "%s(len=%d): ", __func__, len);
421 gpio_set_value(par->RW, 0); /* set write mode */
427 data = *(u8 *) buf++;
430 for (i = 0; i < 8; ++i)
431 gpio_set_value(par->gpio.db[i], data & (1 << i));
433 gpio_set_value(par->EPIN, 1);
435 /* unset E - write */
436 gpio_set_value(par->EPIN, 0);
443 static struct fbtft_display display = {
449 .init_display = init_display,
450 .set_addr_win = set_addr_win,
451 .verify_gpios = verify_gpios,
452 .request_gpios_match = request_gpios_match,
455 .write_register = write_reg8_bus8,
456 .write_vmem = write_vmem,
459 FBTFT_REGISTER_DRIVER(DRVNAME, "displaytronic,fb_agm1264k-fl", &display);
461 MODULE_ALIAS("platform:" DRVNAME);
463 MODULE_DESCRIPTION("Two KS0108 LCD controllers in AGM1264K-FL display");
464 MODULE_AUTHOR("ololoshka2871");
465 MODULE_LICENSE("GPL");