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, ...)
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, par->info->device,
189 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 void iterate_diffusion_matrix(u32 xres, u32 yres, int x,
268 int y, signed short *convert_buf,
269 signed short pixel, signed short error)
273 /* diffusion matrix row */
274 for (i = 0; i < DIFFUSING_MATRIX_WIDTH; ++i)
275 /* diffusion matrix column */
276 for (j = 0; j < DIFFUSING_MATRIX_HEIGHT; ++j) {
277 signed short *write_pos;
280 /* skip pixels out of zone */
281 if (x + i < 0 || x + i >= xres || y + j >= yres)
283 write_pos = &convert_buf[(y + j) * xres + x + i];
284 coeff = diffusing_matrix[i][j];
289 signed short p = *write_pos + error * coeff;
300 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
302 u16 *vmem16 = (u16 *)par->info->screen_buffer;
303 u8 *buf = par->txbuf.buf;
307 /* buffer to convert RGB565 -> grayscale16 -> Dithered image 1bpp */
308 signed short *convert_buf = kmalloc_array(par->info->var.xres *
309 par->info->var.yres, sizeof(signed short), GFP_NOIO);
314 /* converting to grayscale16 */
315 for (x = 0; x < par->info->var.xres; ++x)
316 for (y = 0; y < par->info->var.yres; ++y) {
317 u16 pixel = vmem16[y * par->info->var.xres + x];
318 u16 b = pixel & 0x1f;
319 u16 g = (pixel & (0x3f << 5)) >> 5;
320 u16 r = (pixel & (0x1f << (5 + 6))) >> (5 + 6);
322 pixel = (299 * r + 587 * g + 114 * b) / 200;
326 /* gamma-correction by table */
327 convert_buf[y * par->info->var.xres + x] =
328 (signed short)gamma_correction_table[pixel];
331 /* Image Dithering */
332 for (x = 0; x < par->info->var.xres; ++x)
333 for (y = 0; y < par->info->var.yres; ++y) {
335 convert_buf[y * par->info->var.xres + x];
336 signed short error_b = pixel - BLACK;
337 signed short error_w = pixel - WHITE;
340 /* what color close? */
341 if (abs(error_b) >= abs(error_w)) {
353 iterate_diffusion_matrix(par->info->var.xres,
359 /* 1 string = 2 pages */
360 for (y = addr_win.ys_page; y <= addr_win.ye_page; ++y) {
361 /* left half of display */
362 if (addr_win.xs < par->info->var.xres / 2) {
363 construct_line_bitmap(par, buf, convert_buf,
365 par->info->var.xres / 2, y);
367 len = par->info->var.xres / 2 - addr_win.xs;
369 /* select left side (sc0)
372 write_reg(par, 0x00, BIT(6) | (u8)addr_win.xs);
373 write_reg(par, 0x00, (0x17 << 3) | (u8)y);
376 gpio_set_value(par->RS, 1); /* RS->1 (data mode) */
377 ret = par->fbtftops.write(par, buf, len);
379 dev_err(par->info->device,
380 "write failed and returned: %d\n",
383 /* right half of display */
384 if (addr_win.xe >= par->info->var.xres / 2) {
385 construct_line_bitmap(par, buf,
387 par->info->var.xres / 2,
390 len = addr_win.xe + 1 - par->info->var.xres / 2;
392 /* select right side (sc1)
395 write_reg(par, 0x01, 1 << 6);
396 write_reg(par, 0x01, (0x17 << 3) | (u8)y);
399 gpio_set_value(par->RS, 1); /* RS->1 (data mode) */
400 par->fbtftops.write(par, buf, len);
402 dev_err(par->info->device,
403 "write failed and returned: %d\n",
409 gpio_set_value(par->CS0, 1);
410 gpio_set_value(par->CS1, 1);
415 static int write(struct fbtft_par *par, void *buf, size_t len)
417 fbtft_par_dbg_hex(DEBUG_WRITE, par, par->info->device, u8, buf, len,
418 "%s(len=%d): ", __func__, len);
420 gpio_set_value(par->RW, 0); /* set write mode */
428 for (i = 0; i < 8; ++i)
429 gpio_set_value(par->gpio.db[i], data & (1 << i));
431 gpio_set_value(par->EPIN, 1);
433 /* unset E - write */
434 gpio_set_value(par->EPIN, 0);
441 static struct fbtft_display display = {
447 .init_display = init_display,
448 .set_addr_win = set_addr_win,
449 .verify_gpios = verify_gpios,
450 .request_gpios_match = request_gpios_match,
453 .write_register = write_reg8_bus8,
454 .write_vmem = write_vmem,
458 FBTFT_REGISTER_DRIVER(DRVNAME, "displaytronic,fb_agm1264k-fl", &display);
460 MODULE_ALIAS("platform:" DRVNAME);
462 MODULE_DESCRIPTION("Two KS0108 LCD controllers in AGM1264K-FL display");
463 MODULE_AUTHOR("ololoshka2871");
464 MODULE_LICENSE("GPL");