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]
44 /* diffusing error (Floyd-Steinberg) */
45 #define DIFFUSING_MATRIX_WIDTH 2
46 #define DIFFUSING_MATRIX_HEIGHT 2
48 static const signed char
49 diffusing_matrix[DIFFUSING_MATRIX_WIDTH][DIFFUSING_MATRIX_HEIGHT] = {
54 static const unsigned char gamma_correction_table[] = {
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
56 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6,
57 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 13,
58 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21,
59 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32,
60 33, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 43, 44, 45,
61 46, 47, 48, 49, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 81,
63 82, 83, 84, 85, 87, 88, 89, 90, 91, 93, 94, 95, 97, 98, 99, 100, 102,
64 103, 105, 106, 107, 109, 110, 111, 113, 114, 116, 117, 119, 120, 121,
65 123, 124, 126, 127, 129, 130, 132, 133, 135, 137, 138, 140, 141, 143,
66 145, 146, 148, 149, 151, 153, 154, 156, 158, 159, 161, 163, 165, 166,
67 168, 170, 172, 173, 175, 177, 179, 181, 182, 184, 186, 188, 190, 192,
68 194, 196, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219,
69 221, 223, 225, 227, 229, 231, 234, 236, 238, 240, 242, 244, 246, 248,
73 static int init_display(struct fbtft_par *par)
77 par->fbtftops.reset(par);
79 for (i = 0; i < 2; ++i) {
80 write_reg(par, i, 0x3f); /* display on */
81 write_reg(par, i, 0x40); /* set x to 0 */
82 write_reg(par, i, 0xb0); /* set page to 0 */
83 write_reg(par, i, 0xc0); /* set start line to 0 */
89 static void reset(struct fbtft_par *par)
91 if (par->gpio.reset == -1)
94 dev_dbg(par->info->device, "%s()\n", __func__);
96 gpio_set_value(par->gpio.reset, 0);
98 gpio_set_value(par->gpio.reset, 1);
102 /* Check if all necessary GPIOS defined */
103 static int verify_gpios(struct fbtft_par *par)
107 dev_dbg(par->info->device,
111 dev_err(par->info->device,
112 "Missing info about 'wr' (aka E) gpio. Aborting.\n");
115 for (i = 0; i < 8; ++i) {
116 if (par->gpio.db[i] < 0) {
117 dev_err(par->info->device,
118 "Missing info about 'db[%i]' gpio. Aborting.\n",
124 dev_err(par->info->device,
125 "Missing info about 'cs0' gpio. Aborting.\n");
129 dev_err(par->info->device,
130 "Missing info about 'cs1' gpio. Aborting.\n");
134 dev_err(par->info->device,
135 "Missing info about 'rw' gpio. Aborting.\n");
143 request_gpios_match(struct fbtft_par *par, const struct fbtft_gpio *gpio)
145 dev_dbg(par->info->device,
146 "%s('%s')\n", __func__, gpio->name);
148 if (strcasecmp(gpio->name, "wr") == 0) {
149 /* left ks0108 E pin */
150 par->EPIN = gpio->gpio;
151 return GPIOF_OUT_INIT_LOW;
152 } else if (strcasecmp(gpio->name, "cs0") == 0) {
153 /* left ks0108 controller pin */
154 par->CS0 = gpio->gpio;
155 return GPIOF_OUT_INIT_HIGH;
156 } else if (strcasecmp(gpio->name, "cs1") == 0) {
157 /* right ks0108 controller pin */
158 par->CS1 = gpio->gpio;
159 return GPIOF_OUT_INIT_HIGH;
162 /* if write (rw = 0) e(1->0) perform write */
163 /* if read (rw = 1) e(0->1) set data on D0-7*/
164 else if (strcasecmp(gpio->name, "rw") == 0) {
165 par->RW = gpio->gpio;
166 return GPIOF_OUT_INIT_LOW;
169 return FBTFT_GPIO_NO_MATCH;
172 /* This function oses to enter commands
173 * first byte - destination controller 0 or 1
174 * following - commands
176 static void write_reg8_bus8(struct fbtft_par *par, int len, ...)
180 u8 *buf = (u8 *)par->buf;
182 if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) {
184 for (i = 0; i < len; i++)
185 buf[i] = (u8)va_arg(args, unsigned int);
188 fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par,
189 par->info->device, u8, buf, len, "%s: ", __func__);
194 *buf = (u8)va_arg(args, unsigned int);
198 dev_err(par->info->device,
199 "Incorrect chip select request (%d)\n", *buf);
206 gpio_set_value(par->CS0, 1);
207 gpio_set_value(par->CS1, 0);
210 gpio_set_value(par->CS0, 0);
211 gpio_set_value(par->CS1, 1);
214 gpio_set_value(par->RS, 0); /* RS->0 (command mode) */
220 *buf++ = (u8)va_arg(args, unsigned int);
221 ret = par->fbtftops.write(par, par->buf, len * (sizeof(u8)));
224 dev_err(par->info->device,
225 "write() failed and returned %d\n", ret);
235 int xs, ys_page, xe, ye_page;
238 /* save display writing zone */
239 static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye)
242 addr_win.ys_page = ys / 8;
244 addr_win.ye_page = ye / 8;
248 construct_line_bitmap(struct fbtft_par *par, u8 *dest, signed short *src,
249 int xs, int xe, int y)
253 for (x = xs; x < xe; ++x) {
256 for (i = 0; i < 8; i++)
257 if (src[(y * 8 + i) * par->info->var.xres + x])
267 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
269 u16 *vmem16 = (u16 *)par->info->screen_base;
270 u8 *buf = par->txbuf.buf;
274 /* buffer to convert RGB565 -> grayscale16 -> Dithered image 1bpp */
275 signed short *convert_buf = kmalloc(par->info->var.xres *
276 par->info->var.yres * sizeof(signed short), GFP_NOIO);
281 fbtft_par_dbg(DEBUG_WRITE_VMEM, par, "%s()\n", __func__);
283 /* converting to grayscale16 */
284 for (x = 0; x < par->info->var.xres; ++x)
285 for (y = 0; y < par->info->var.yres; ++y) {
286 u16 pixel = vmem16[y * par->info->var.xres + x];
287 u16 b = pixel & 0x1f;
288 u16 g = (pixel & (0x3f << 5)) >> 5;
289 u16 r = (pixel & (0x1f << (5 + 6))) >> (5 + 6);
291 pixel = (299 * r + 587 * g + 114 * b) / 200;
295 /* gamma-correction by table */
296 convert_buf[y * par->info->var.xres + x] =
297 (signed short)gamma_correction_table[pixel];
300 /* Image Dithering */
301 for (x = 0; x < par->info->var.xres; ++x)
302 for (y = 0; y < par->info->var.yres; ++y) {
304 convert_buf[y * par->info->var.xres + x];
305 signed short error_b = pixel - BLACK;
306 signed short error_w = pixel - WHITE;
310 /* what color close? */
311 if (abs(error_b) >= abs(error_w)) {
323 /* diffusion matrix row */
324 for (i = 0; i < DIFFUSING_MATRIX_WIDTH; ++i)
325 /* diffusion matrix column */
326 for (j = 0; j < DIFFUSING_MATRIX_HEIGHT; ++j) {
327 signed short *write_pos;
330 /* skip pixels out of zone */
332 x + i >= par->info->var.xres
333 || y + j >= par->info->var.yres)
335 write_pos = &convert_buf[
336 (y + j) * par->info->var.xres +
338 coeff = diffusing_matrix[i][j];
343 signed short p = *write_pos +
355 /* 1 string = 2 pages */
356 for (y = addr_win.ys_page; y <= addr_win.ye_page; ++y) {
357 /* left half of display */
358 if (addr_win.xs < par->info->var.xres / 2) {
359 construct_line_bitmap(par, buf, convert_buf,
360 addr_win.xs, par->info->var.xres / 2, y);
362 len = par->info->var.xres / 2 - addr_win.xs;
364 /* select left side (sc0)
367 write_reg(par, 0x00, (1 << 6) | (u8)addr_win.xs);
368 write_reg(par, 0x00, (0x17 << 3) | (u8)y);
371 gpio_set_value(par->RS, 1); /* RS->1 (data mode) */
372 ret = par->fbtftops.write(par, buf, len);
374 dev_err(par->info->device,
375 "write failed and returned: %d\n",
378 /* right half of display */
379 if (addr_win.xe >= par->info->var.xres / 2) {
380 construct_line_bitmap(par, buf,
381 convert_buf, par->info->var.xres / 2,
384 len = addr_win.xe + 1 - par->info->var.xres / 2;
386 /* select right side (sc1)
389 write_reg(par, 0x01, 1 << 6);
390 write_reg(par, 0x01, (0x17 << 3) | (u8)y);
393 gpio_set_value(par->RS, 1); /* RS->1 (data mode) */
394 par->fbtftops.write(par, buf, len);
396 dev_err(par->info->device,
397 "write failed and returned: %d\n",
403 gpio_set_value(par->CS0, 1);
404 gpio_set_value(par->CS1, 1);
409 static int write(struct fbtft_par *par, void *buf, size_t len)
411 fbtft_par_dbg_hex(DEBUG_WRITE, par, par->info->device, u8, buf, len,
412 "%s(len=%d): ", __func__, len);
414 gpio_set_value(par->RW, 0); /* set write mode */
419 data = *(u8 *) buf++;
422 for (i = 0; i < 8; ++i)
423 gpio_set_value(par->gpio.db[i], data & (1 << i));
425 gpio_set_value(par->EPIN, 1);
427 /* unset E - write */
428 gpio_set_value(par->EPIN, 0);
435 static struct fbtft_display display = {
441 .init_display = init_display,
442 .set_addr_win = set_addr_win,
443 .verify_gpios = verify_gpios,
444 .request_gpios_match = request_gpios_match,
447 .write_register = write_reg8_bus8,
448 .write_vmem = write_vmem,
452 FBTFT_REGISTER_DRIVER(DRVNAME, "displaytronic,fb_agm1264k-fl", &display);
454 MODULE_ALIAS("platform:" DRVNAME);
456 MODULE_DESCRIPTION("Two KS0108 LCD controllers in AGM1264K-FL display");
457 MODULE_AUTHOR("ololoshka2871");
458 MODULE_LICENSE("GPL");