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 
44 /* diffusing error (Floyd-Steinberg) */
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 
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 
89 static void reset(struct fbtft_par *par)
90 {
91 	if (par->gpio.reset == -1)
92 		return;
93 
94 	dev_dbg(par->info->device, "%s()\n", __func__);
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 
107 	dev_dbg(par->info->device,
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 {
145 	dev_dbg(par->info->device,
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
174  * following - commands
175  */
176 static void write_reg8_bus8(struct fbtft_par *par, int len, ...)
177 {
178 	va_list args;
179 	int i, ret;
180 	u8 *buf = par->buf;
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, par->info->device,
189 				  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);
198 		dev_err(par->info->device,
199 			"Incorrect chip select request (%d)\n", *buf);
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);
224 			dev_err(par->info->device,
225 				"write() failed and returned %d\n", ret);
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;
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 void iterate_diffusion_matrix(u32 xres, u32 yres, int x,
268 				     int y, signed short *convert_buf,
269 				     signed short pixel, signed short error)
270 {
271 	u16 i, j;
272 
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;
278 			signed char coeff;
279 
280 			/* skip pixels out of zone */
281 			if (x + i < 0 || x + i >= xres || y + j >= yres)
282 				continue;
283 			write_pos = &convert_buf[(y + j) * xres + x + i];
284 			coeff = diffusing_matrix[i][j];
285 			if (-1 == coeff) {
286 				/* pixel itself */
287 				*write_pos = pixel;
288 			} else {
289 				signed short p = *write_pos + error * coeff;
290 
291 				if (p > WHITE)
292 					p = WHITE;
293 				if (p < BLACK)
294 					p = BLACK;
295 				*write_pos = p;
296 			}
297 		}
298 }
299 
300 static int write_vmem(struct fbtft_par *par, size_t offset, size_t len)
301 {
302 	u16 *vmem16 = (u16 *)par->info->screen_buffer;
303 	u8 *buf = par->txbuf.buf;
304 	int x, y;
305 	int ret = 0;
306 
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);
310 
311 	if (!convert_buf)
312 		return -ENOMEM;
313 
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);
321 
322 			pixel = (299 * r + 587 * g + 114 * b) / 200;
323 			if (pixel > 255)
324 				pixel = 255;
325 
326 			/* gamma-correction by table */
327 			convert_buf[y *  par->info->var.xres + x] =
328 				(signed short)gamma_correction_table[pixel];
329 		}
330 
331 	/* Image Dithering */
332 	for (x = 0; x < par->info->var.xres; ++x)
333 		for (y = 0; y < par->info->var.yres; ++y) {
334 			signed short pixel =
335 				convert_buf[y *  par->info->var.xres + x];
336 			signed short error_b = pixel - BLACK;
337 			signed short error_w = pixel - WHITE;
338 			signed short error;
339 
340 			/* what color close? */
341 			if (abs(error_b) >= abs(error_w)) {
342 				/* white */
343 				error = error_w;
344 				pixel = 0xff;
345 			} else {
346 				/* black */
347 				error = error_b;
348 				pixel = 0;
349 			}
350 
351 			error /= 8;
352 
353 			iterate_diffusion_matrix(par->info->var.xres,
354 						 par->info->var.yres,
355 						 x, y, convert_buf,
356 						 pixel, error);
357 		}
358 
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,
364 					      addr_win.xs,
365 					      par->info->var.xres / 2, y);
366 
367 			len = par->info->var.xres / 2 - addr_win.xs;
368 
369 			/* select left side (sc0)
370 			 * set addr
371 			 */
372 			write_reg(par, 0x00, BIT(6) | (u8)addr_win.xs);
373 			write_reg(par, 0x00, (0x17 << 3) | (u8)y);
374 
375 			/* write bitmap */
376 			gpio_set_value(par->RS, 1); /* RS->1 (data mode) */
377 			ret = par->fbtftops.write(par, buf, len);
378 			if (ret < 0)
379 				dev_err(par->info->device,
380 					"write failed and returned: %d\n",
381 					ret);
382 		}
383 		/* right half of display */
384 		if (addr_win.xe >= par->info->var.xres / 2) {
385 			construct_line_bitmap(par, buf,
386 					      convert_buf,
387 					      par->info->var.xres / 2,
388 					      addr_win.xe + 1, y);
389 
390 			len = addr_win.xe + 1 - par->info->var.xres / 2;
391 
392 			/* select right side (sc1)
393 			 * set addr
394 			 */
395 			write_reg(par, 0x01, 1 << 6);
396 			write_reg(par, 0x01, (0x17 << 3) | (u8)y);
397 
398 			/* write bitmap */
399 			gpio_set_value(par->RS, 1); /* RS->1 (data mode) */
400 			par->fbtftops.write(par, buf, len);
401 			if (ret < 0)
402 				dev_err(par->info->device,
403 					"write failed and returned: %d\n",
404 					ret);
405 		}
406 	}
407 	kfree(convert_buf);
408 
409 	gpio_set_value(par->CS0, 1);
410 	gpio_set_value(par->CS1, 1);
411 
412 	return ret;
413 }
414 
415 static int write(struct fbtft_par *par, void *buf, size_t len)
416 {
417 	fbtft_par_dbg_hex(DEBUG_WRITE, par, par->info->device, u8, buf, len,
418 			  "%s(len=%d): ", __func__, len);
419 
420 	gpio_set_value(par->RW, 0); /* set write mode */
421 
422 	while (len--) {
423 		u8 i, data;
424 
425 		data = *(u8 *)buf++;
426 
427 		/* set data bus */
428 		for (i = 0; i < 8; ++i)
429 			gpio_set_value(par->gpio.db[i], data & (1 << i));
430 		/* set E */
431 		gpio_set_value(par->EPIN, 1);
432 		udelay(5);
433 		/* unset E - write */
434 		gpio_set_value(par->EPIN, 0);
435 		udelay(1);
436 	}
437 
438 	return 0;
439 }
440 
441 static struct fbtft_display display = {
442 	.regwidth = 8,
443 	.width = TOTALWIDTH,
444 	.height = HEIGHT,
445 	.fps = FPS,
446 	.fbtftops = {
447 		.init_display = init_display,
448 		.set_addr_win = set_addr_win,
449 		.verify_gpios = verify_gpios,
450 		.request_gpios_match = request_gpios_match,
451 		.reset = reset,
452 		.write = write,
453 		.write_register = write_reg8_bus8,
454 		.write_vmem = write_vmem,
455 	},
456 };
457 
458 FBTFT_REGISTER_DRIVER(DRVNAME, "displaytronic,fb_agm1264k-fl", &display);
459 
460 MODULE_ALIAS("platform:" DRVNAME);
461 
462 MODULE_DESCRIPTION("Two KS0108 LCD controllers in AGM1264K-FL display");
463 MODULE_AUTHOR("ololoshka2871");
464 MODULE_LICENSE("GPL");
465