Commit | Line | Data |
---|---|---|
b2ebd4be TP |
1 | /* |
2 | * FB driver for Two KS0108 LCD controllers in AGM1264K-FL display | |
3 | * | |
4 | * Copyright (C) 2014 ololoshka2871 | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | */ | |
16 | ||
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> | |
23 | ||
24 | #include "fbtft.h" | |
25 | ||
26 | /* Uncomment text line to use negative image on display */ | |
27 | /*#define NEGATIVE*/ | |
28 | ||
29 | #define WHITE 0xff | |
30 | #define BLACK 0 | |
31 | ||
32 | #define DRVNAME "fb_agm1264k-fl" | |
33 | #define WIDTH 64 | |
34 | #define HEIGHT 64 | |
35 | #define TOTALWIDTH (WIDTH * 2) /* because 2 x ks0108 in one display */ | |
36 | #define FPS 20 | |
37 | ||
38 | #define EPIN gpio.wr | |
39 | #define RS gpio.dc | |
40 | #define RW gpio.aux[2] | |
41 | #define CS0 gpio.aux[0] | |
42 | #define CS1 gpio.aux[1] | |
43 | ||
c3eeca50 | 44 | /* diffusing error (Floyd-Steinberg) */ |
b2ebd4be TP |
45 | #define DIFFUSING_MATRIX_WIDTH 2 |
46 | #define DIFFUSING_MATRIX_HEIGHT 2 | |
47 | ||
48 | static const signed char | |
49 | diffusing_matrix[DIFFUSING_MATRIX_WIDTH][DIFFUSING_MATRIX_HEIGHT] = { | |
50 | {-1, 3}, | |
51 | {3, 2}, | |
52 | }; | |
53 | ||
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, | |
70 | 251, 253, 255 | |
71 | }; | |
72 | ||
73 | static int init_display(struct fbtft_par *par) | |
74 | { | |
75 | u8 i; | |
76 | ||
b2ebd4be TP |
77 | par->fbtftops.reset(par); |
78 | ||
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 */ | |
84 | } | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
074e6fd1 | 89 | static void reset(struct fbtft_par *par) |
b2ebd4be TP |
90 | { |
91 | if (par->gpio.reset == -1) | |
92 | return; | |
93 | ||
b38c760a | 94 | dev_dbg(par->info->device, "%s()\n", __func__); |
b2ebd4be TP |
95 | |
96 | gpio_set_value(par->gpio.reset, 0); | |
97 | udelay(20); | |
98 | gpio_set_value(par->gpio.reset, 1); | |
99 | mdelay(120); | |
100 | } | |
101 | ||
102 | /* Check if all necessary GPIOS defined */ | |
103 | static int verify_gpios(struct fbtft_par *par) | |
104 | { | |
105 | int i; | |
106 | ||
b38c760a | 107 | dev_dbg(par->info->device, |
b2ebd4be TP |
108 | "%s()\n", __func__); |
109 | ||
110 | if (par->EPIN < 0) { | |
111 | dev_err(par->info->device, | |
112 | "Missing info about 'wr' (aka E) gpio. Aborting.\n"); | |
113 | return -EINVAL; | |
114 | } | |
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", | |
119 | i); | |
120 | return -EINVAL; | |
121 | } | |
122 | } | |
123 | if (par->CS0 < 0) { | |
124 | dev_err(par->info->device, | |
125 | "Missing info about 'cs0' gpio. Aborting.\n"); | |
126 | return -EINVAL; | |
127 | } | |
128 | if (par->CS1 < 0) { | |
129 | dev_err(par->info->device, | |
130 | "Missing info about 'cs1' gpio. Aborting.\n"); | |
131 | return -EINVAL; | |
132 | } | |
133 | if (par->RW < 0) { | |
134 | dev_err(par->info->device, | |
135 | "Missing info about 'rw' gpio. Aborting.\n"); | |
136 | return -EINVAL; | |
137 | } | |
138 | ||
139 | return 0; | |
140 | } | |
141 | ||
142 | static unsigned long | |
143 | request_gpios_match(struct fbtft_par *par, const struct fbtft_gpio *gpio) | |
144 | { | |
b38c760a | 145 | dev_dbg(par->info->device, |
b2ebd4be TP |
146 | "%s('%s')\n", __func__, gpio->name); |
147 | ||
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; | |
160 | } | |
161 | ||
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; | |
167 | } | |
168 | ||
169 | return FBTFT_GPIO_NO_MATCH; | |
170 | } | |
171 | ||
172 | /* This function oses to enter commands | |
173 | * first byte - destination controller 0 or 1 | |
92def781 | 174 | * following - commands |
b2ebd4be TP |
175 | */ |
176 | static void write_reg8_bus8(struct fbtft_par *par, int len, ...) | |
177 | { | |
178 | va_list args; | |
179 | int i, ret; | |
193ef315 | 180 | u8 *buf = par->buf; |
b2ebd4be TP |
181 | |
182 | if (unlikely(par->debug & DEBUG_WRITE_REGISTER)) { | |
183 | va_start(args, len); | |
184 | for (i = 0; i < len; i++) | |
185 | buf[i] = (u8)va_arg(args, unsigned int); | |
186 | ||
187 | va_end(args); | |
188 | fbtft_par_dbg_hex(DEBUG_WRITE_REGISTER, par, | |
189 | par->info->device, u8, buf, len, "%s: ", __func__); | |
190 | } | |
191 | ||
192 | va_start(args, len); | |
193 | ||
194 | *buf = (u8)va_arg(args, unsigned int); | |
195 | ||
196 | if (*buf > 1) { | |
197 | va_end(args); | |
aed1c72e | 198 | dev_err(par->info->device, |
92def781 | 199 | "Incorrect chip select request (%d)\n", *buf); |
b2ebd4be TP |
200 | return; |
201 | } | |
202 | ||
203 | /* select chip */ | |
204 | if (*buf) { | |
205 | /* cs1 */ | |
206 | gpio_set_value(par->CS0, 1); | |
207 | gpio_set_value(par->CS1, 0); | |
208 | } else { | |
209 | /* cs0 */ | |
210 | gpio_set_value(par->CS0, 0); | |
211 | gpio_set_value(par->CS1, 1); | |
212 | } | |
213 | ||
214 | gpio_set_value(par->RS, 0); /* RS->0 (command mode) */ | |
215 | len--; | |
216 | ||
217 | if (len) { | |
218 | i = len; | |
219 | while (i--) | |
220 | *buf++ = (u8)va_arg(args, unsigned int); | |
221 | ret = par->fbtftops.write(par, par->buf, len * (sizeof(u8))); | |
222 | if (ret < 0) { | |
223 | va_end(args); | |
aed1c72e HM |
224 | dev_err(par->info->device, |
225 | "write() failed and returned %d\n", ret); | |
b2ebd4be TP |
226 | return; |
227 | } | |
228 | } | |
229 | ||
230 | va_end(args); | |
231 | } | |
232 | ||
233 | static struct | |
234 | { | |
235 | int xs, ys_page, xe, ye_page; | |
236 | } addr_win; | |
237 | ||
238 | /* save display writing zone */ | |
239 | static void set_addr_win(struct fbtft_par *par, int xs, int ys, int xe, int ye) | |
240 | { | |
241 | addr_win.xs = xs; | |
242 | addr_win.ys_page = ys / 8; | |
243 | addr_win.xe = xe; | |
244 | addr_win.ye_page = ye / 8; | |
b2ebd4be TP |
245 | } |
246 | ||
247 | static void | |
248 | construct_line_bitmap(struct fbtft_par *par, u8 *dest, signed short *src, | |
249 | int xs, int xe, int y) | |
250 | { | |
251 | int x, i; | |
252 | ||
253 | for (x = xs; x < xe; ++x) { | |
254 | u8 res = 0; | |
255 | ||
256 | for (i = 0; i < 8; i++) | |
257 | if (src[(y * 8 + i) * par->info->var.xres + x]) | |
258 | res |= 1 << i; | |
259 | #ifdef NEGATIVE | |
260 | *dest++ = res; | |
261 | #else | |
262 | *dest++ = ~res; | |
263 | #endif | |
264 | } | |
265 | } | |
266 | ||
267 | static int write_vmem(struct fbtft_par *par, size_t offset, size_t len) | |
268 | { | |
4b6dc179 | 269 | u16 *vmem16 = (u16 *)par->info->screen_buffer; |
b2ebd4be TP |
270 | u8 *buf = par->txbuf.buf; |
271 | int x, y; | |
272 | int ret = 0; | |
273 | ||
92def781 | 274 | /* buffer to convert RGB565 -> grayscale16 -> Dithered image 1bpp */ |
7ce1b3d3 BS |
275 | signed short *convert_buf = kmalloc_array(par->info->var.xres * |
276 | par->info->var.yres, sizeof(signed short), GFP_NOIO); | |
b2ebd4be | 277 | |
6a938a8c RM |
278 | if (!convert_buf) |
279 | return -ENOMEM; | |
280 | ||
b2ebd4be TP |
281 | /* converting to grayscale16 */ |
282 | for (x = 0; x < par->info->var.xres; ++x) | |
283 | for (y = 0; y < par->info->var.yres; ++y) { | |
284 | u16 pixel = vmem16[y * par->info->var.xres + x]; | |
285 | u16 b = pixel & 0x1f; | |
286 | u16 g = (pixel & (0x3f << 5)) >> 5; | |
287 | u16 r = (pixel & (0x1f << (5 + 6))) >> (5 + 6); | |
288 | ||
289 | pixel = (299 * r + 587 * g + 114 * b) / 200; | |
290 | if (pixel > 255) | |
291 | pixel = 255; | |
292 | ||
293 | /* gamma-correction by table */ | |
294 | convert_buf[y * par->info->var.xres + x] = | |
295 | (signed short)gamma_correction_table[pixel]; | |
296 | } | |
297 | ||
298 | /* Image Dithering */ | |
299 | for (x = 0; x < par->info->var.xres; ++x) | |
300 | for (y = 0; y < par->info->var.yres; ++y) { | |
301 | signed short pixel = | |
302 | convert_buf[y * par->info->var.xres + x]; | |
303 | signed short error_b = pixel - BLACK; | |
304 | signed short error_w = pixel - WHITE; | |
305 | signed short error; | |
306 | u16 i, j; | |
307 | ||
308 | /* what color close? */ | |
309 | if (abs(error_b) >= abs(error_w)) { | |
310 | /* white */ | |
311 | error = error_w; | |
312 | pixel = 0xff; | |
313 | } else { | |
314 | /* black */ | |
315 | error = error_b; | |
316 | pixel = 0; | |
317 | } | |
318 | ||
319 | error /= 8; | |
320 | ||
321 | /* diffusion matrix row */ | |
322 | for (i = 0; i < DIFFUSING_MATRIX_WIDTH; ++i) | |
323 | /* diffusion matrix column */ | |
324 | for (j = 0; j < DIFFUSING_MATRIX_HEIGHT; ++j) { | |
325 | signed short *write_pos; | |
326 | signed char coeff; | |
327 | ||
328 | /* skip pixels out of zone */ | |
329 | if (x + i < 0 || | |
330 | x + i >= par->info->var.xres | |
331 | || y + j >= par->info->var.yres) | |
332 | continue; | |
333 | write_pos = &convert_buf[ | |
334 | (y + j) * par->info->var.xres + | |
335 | x + i]; | |
336 | coeff = diffusing_matrix[i][j]; | |
337 | if (coeff == -1) | |
338 | /* pixel itself */ | |
339 | *write_pos = pixel; | |
340 | else { | |
341 | signed short p = *write_pos + | |
342 | error * coeff; | |
343 | ||
344 | if (p > WHITE) | |
345 | p = WHITE; | |
346 | if (p < BLACK) | |
347 | p = BLACK; | |
348 | *write_pos = p; | |
349 | } | |
350 | } | |
351 | } | |
352 | ||
ea40d674 PR |
353 | /* 1 string = 2 pages */ |
354 | for (y = addr_win.ys_page; y <= addr_win.ye_page; ++y) { | |
b2ebd4be TP |
355 | /* left half of display */ |
356 | if (addr_win.xs < par->info->var.xres / 2) { | |
357 | construct_line_bitmap(par, buf, convert_buf, | |
358 | addr_win.xs, par->info->var.xres / 2, y); | |
359 | ||
360 | len = par->info->var.xres / 2 - addr_win.xs; | |
361 | ||
362 | /* select left side (sc0) | |
363 | * set addr | |
364 | */ | |
365 | write_reg(par, 0x00, (1 << 6) | (u8)addr_win.xs); | |
366 | write_reg(par, 0x00, (0x17 << 3) | (u8)y); | |
367 | ||
368 | /* write bitmap */ | |
369 | gpio_set_value(par->RS, 1); /* RS->1 (data mode) */ | |
370 | ret = par->fbtftops.write(par, buf, len); | |
371 | if (ret < 0) | |
372 | dev_err(par->info->device, | |
aed1c72e HM |
373 | "write failed and returned: %d\n", |
374 | ret); | |
b2ebd4be TP |
375 | } |
376 | /* right half of display */ | |
377 | if (addr_win.xe >= par->info->var.xres / 2) { | |
378 | construct_line_bitmap(par, buf, | |
379 | convert_buf, par->info->var.xres / 2, | |
380 | addr_win.xe + 1, y); | |
381 | ||
382 | len = addr_win.xe + 1 - par->info->var.xres / 2; | |
383 | ||
384 | /* select right side (sc1) | |
385 | * set addr | |
386 | */ | |
6b384527 | 387 | write_reg(par, 0x01, 1 << 6); |
b2ebd4be TP |
388 | write_reg(par, 0x01, (0x17 << 3) | (u8)y); |
389 | ||
390 | /* write bitmap */ | |
391 | gpio_set_value(par->RS, 1); /* RS->1 (data mode) */ | |
392 | par->fbtftops.write(par, buf, len); | |
393 | if (ret < 0) | |
394 | dev_err(par->info->device, | |
aed1c72e HM |
395 | "write failed and returned: %d\n", |
396 | ret); | |
b2ebd4be TP |
397 | } |
398 | } | |
399 | kfree(convert_buf); | |
400 | ||
401 | gpio_set_value(par->CS0, 1); | |
402 | gpio_set_value(par->CS1, 1); | |
403 | ||
404 | return ret; | |
405 | } | |
406 | ||
407 | static int write(struct fbtft_par *par, void *buf, size_t len) | |
408 | { | |
409 | fbtft_par_dbg_hex(DEBUG_WRITE, par, par->info->device, u8, buf, len, | |
410 | "%s(len=%d): ", __func__, len); | |
411 | ||
412 | gpio_set_value(par->RW, 0); /* set write mode */ | |
413 | ||
b2ebd4be TP |
414 | while (len--) { |
415 | u8 i, data; | |
416 | ||
60abe351 | 417 | data = *(u8 *)buf++; |
b2ebd4be TP |
418 | |
419 | /* set data bus */ | |
420 | for (i = 0; i < 8; ++i) | |
421 | gpio_set_value(par->gpio.db[i], data & (1 << i)); | |
422 | /* set E */ | |
423 | gpio_set_value(par->EPIN, 1); | |
424 | udelay(5); | |
425 | /* unset E - write */ | |
426 | gpio_set_value(par->EPIN, 0); | |
427 | udelay(1); | |
428 | } | |
429 | ||
430 | return 0; | |
431 | } | |
432 | ||
433 | static struct fbtft_display display = { | |
434 | .regwidth = 8, | |
435 | .width = TOTALWIDTH, | |
436 | .height = HEIGHT, | |
437 | .fps = FPS, | |
438 | .fbtftops = { | |
439 | .init_display = init_display, | |
440 | .set_addr_win = set_addr_win, | |
441 | .verify_gpios = verify_gpios, | |
442 | .request_gpios_match = request_gpios_match, | |
443 | .reset = reset, | |
444 | .write = write, | |
445 | .write_register = write_reg8_bus8, | |
446 | .write_vmem = write_vmem, | |
447 | }, | |
448 | }; | |
1014c2ce | 449 | |
b2ebd4be TP |
450 | FBTFT_REGISTER_DRIVER(DRVNAME, "displaytronic,fb_agm1264k-fl", &display); |
451 | ||
452 | MODULE_ALIAS("platform:" DRVNAME); | |
453 | ||
454 | MODULE_DESCRIPTION("Two KS0108 LCD controllers in AGM1264K-FL display"); | |
455 | MODULE_AUTHOR("ololoshka2871"); | |
456 | MODULE_LICENSE("GPL"); |