• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Common code for AUO-K190X framebuffer drivers
3  *
4  * Copyright (C) 2012 Heiko Stuebner <heiko@sntech.de>
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 version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/gpio.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/fb.h>
17 #include <linux/delay.h>
18 #include <linux/uaccess.h>
19 #include <linux/vmalloc.h>
20 #include <linux/regulator/consumer.h>
21 
22 #include <video/auo_k190xfb.h>
23 
24 #include "auo_k190x.h"
25 
26 struct panel_info {
27 	int w;
28 	int h;
29 };
30 
31 /* table of panel specific parameters to be indexed into by the board drivers */
32 static struct panel_info panel_table[] = {
33 	/* standard 6" */
34 	[AUOK190X_RESOLUTION_800_600] = {
35 		.w = 800,
36 		.h = 600,
37 	},
38 	/* standard 9" */
39 	[AUOK190X_RESOLUTION_1024_768] = {
40 		.w = 1024,
41 		.h = 768,
42 	},
43 	[AUOK190X_RESOLUTION_600_800] = {
44 		.w = 600,
45 		.h = 800,
46 	},
47 	[AUOK190X_RESOLUTION_768_1024] = {
48 		.w = 768,
49 		.h = 1024,
50 	},
51 };
52 
53 /*
54  * private I80 interface to the board driver
55  */
56 
auok190x_issue_data(struct auok190xfb_par * par,u16 data)57 static void auok190x_issue_data(struct auok190xfb_par *par, u16 data)
58 {
59 	par->board->set_ctl(par, AUOK190X_I80_WR, 0);
60 	par->board->set_hdb(par, data);
61 	par->board->set_ctl(par, AUOK190X_I80_WR, 1);
62 }
63 
auok190x_issue_cmd(struct auok190xfb_par * par,u16 data)64 static void auok190x_issue_cmd(struct auok190xfb_par *par, u16 data)
65 {
66 	par->board->set_ctl(par, AUOK190X_I80_DC, 0);
67 	auok190x_issue_data(par, data);
68 	par->board->set_ctl(par, AUOK190X_I80_DC, 1);
69 }
70 
71 /**
72  * Conversion of 16bit color to 4bit grayscale
73  * does roughly (0.3 * R + 0.6 G + 0.1 B) / 2
74  */
rgb565_to_gray4(u16 data,struct fb_var_screeninfo * var)75 static inline int rgb565_to_gray4(u16 data, struct fb_var_screeninfo *var)
76 {
77 	return ((((data & 0xF800) >> var->red.offset) * 77 +
78 		 ((data & 0x07E0) >> (var->green.offset + 1)) * 151 +
79 		 ((data & 0x1F) >> var->blue.offset) * 28) >> 8 >> 1);
80 }
81 
auok190x_issue_pixels_rgb565(struct auok190xfb_par * par,int size,u16 * data)82 static int auok190x_issue_pixels_rgb565(struct auok190xfb_par *par, int size,
83 					u16 *data)
84 {
85 	struct fb_var_screeninfo *var = &par->info->var;
86 	struct device *dev = par->info->device;
87 	int i;
88 	u16 tmp;
89 
90 	if (size & 7) {
91 		dev_err(dev, "issue_pixels: size %d must be a multiple of 8\n",
92 			size);
93 		return -EINVAL;
94 	}
95 
96 	for (i = 0; i < (size >> 2); i++) {
97 		par->board->set_ctl(par, AUOK190X_I80_WR, 0);
98 
99 		tmp  = (rgb565_to_gray4(data[4*i], var) & 0x000F);
100 		tmp |= (rgb565_to_gray4(data[4*i+1], var) << 4) & 0x00F0;
101 		tmp |= (rgb565_to_gray4(data[4*i+2], var) << 8) & 0x0F00;
102 		tmp |= (rgb565_to_gray4(data[4*i+3], var) << 12) & 0xF000;
103 
104 		par->board->set_hdb(par, tmp);
105 		par->board->set_ctl(par, AUOK190X_I80_WR, 1);
106 	}
107 
108 	return 0;
109 }
110 
auok190x_issue_pixels_gray8(struct auok190xfb_par * par,int size,u16 * data)111 static int auok190x_issue_pixels_gray8(struct auok190xfb_par *par, int size,
112 				       u16 *data)
113 {
114 	struct device *dev = par->info->device;
115 	int i;
116 	u16 tmp;
117 
118 	if (size & 3) {
119 		dev_err(dev, "issue_pixels: size %d must be a multiple of 4\n",
120 			size);
121 		return -EINVAL;
122 	}
123 
124 	for (i = 0; i < (size >> 1); i++) {
125 		par->board->set_ctl(par, AUOK190X_I80_WR, 0);
126 
127 		/* simple reduction of 8bit staticgray to 4bit gray
128 		 * combines 4 * 4bit pixel values into a 16bit value
129 		 */
130 		tmp  = (data[2*i] & 0xF0) >> 4;
131 		tmp |= (data[2*i] & 0xF000) >> 8;
132 		tmp |= (data[2*i+1] & 0xF0) << 4;
133 		tmp |= (data[2*i+1] & 0xF000);
134 
135 		par->board->set_hdb(par, tmp);
136 		par->board->set_ctl(par, AUOK190X_I80_WR, 1);
137 	}
138 
139 	return 0;
140 }
141 
auok190x_issue_pixels(struct auok190xfb_par * par,int size,u16 * data)142 static int auok190x_issue_pixels(struct auok190xfb_par *par, int size,
143 				 u16 *data)
144 {
145 	struct fb_info *info = par->info;
146 	struct device *dev = par->info->device;
147 
148 	if (info->var.bits_per_pixel == 8 && info->var.grayscale)
149 		auok190x_issue_pixels_gray8(par, size, data);
150 	else if (info->var.bits_per_pixel == 16)
151 		auok190x_issue_pixels_rgb565(par, size, data);
152 	else
153 		dev_err(dev, "unsupported color mode (bits: %d, gray: %d)\n",
154 			info->var.bits_per_pixel, info->var.grayscale);
155 
156 	return 0;
157 }
158 
auok190x_read_data(struct auok190xfb_par * par)159 static u16 auok190x_read_data(struct auok190xfb_par *par)
160 {
161 	u16 data;
162 
163 	par->board->set_ctl(par, AUOK190X_I80_OE, 0);
164 	data = par->board->get_hdb(par);
165 	par->board->set_ctl(par, AUOK190X_I80_OE, 1);
166 
167 	return data;
168 }
169 
170 /*
171  * Command interface for the controller drivers
172  */
173 
auok190x_send_command_nowait(struct auok190xfb_par * par,u16 data)174 void auok190x_send_command_nowait(struct auok190xfb_par *par, u16 data)
175 {
176 	par->board->set_ctl(par, AUOK190X_I80_CS, 0);
177 	auok190x_issue_cmd(par, data);
178 	par->board->set_ctl(par, AUOK190X_I80_CS, 1);
179 }
180 EXPORT_SYMBOL_GPL(auok190x_send_command_nowait);
181 
auok190x_send_cmdargs_nowait(struct auok190xfb_par * par,u16 cmd,int argc,u16 * argv)182 void auok190x_send_cmdargs_nowait(struct auok190xfb_par *par, u16 cmd,
183 				  int argc, u16 *argv)
184 {
185 	int i;
186 
187 	par->board->set_ctl(par, AUOK190X_I80_CS, 0);
188 	auok190x_issue_cmd(par, cmd);
189 
190 	for (i = 0; i < argc; i++)
191 		auok190x_issue_data(par, argv[i]);
192 	par->board->set_ctl(par, AUOK190X_I80_CS, 1);
193 }
194 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_nowait);
195 
auok190x_send_command(struct auok190xfb_par * par,u16 data)196 int auok190x_send_command(struct auok190xfb_par *par, u16 data)
197 {
198 	int ret;
199 
200 	ret = par->board->wait_for_rdy(par);
201 	if (ret)
202 		return ret;
203 
204 	auok190x_send_command_nowait(par, data);
205 	return 0;
206 }
207 EXPORT_SYMBOL_GPL(auok190x_send_command);
208 
auok190x_send_cmdargs(struct auok190xfb_par * par,u16 cmd,int argc,u16 * argv)209 int auok190x_send_cmdargs(struct auok190xfb_par *par, u16 cmd,
210 			   int argc, u16 *argv)
211 {
212 	int ret;
213 
214 	ret = par->board->wait_for_rdy(par);
215 	if (ret)
216 		return ret;
217 
218 	auok190x_send_cmdargs_nowait(par, cmd, argc, argv);
219 	return 0;
220 }
221 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs);
222 
auok190x_read_cmdargs(struct auok190xfb_par * par,u16 cmd,int argc,u16 * argv)223 int auok190x_read_cmdargs(struct auok190xfb_par *par, u16 cmd,
224 			   int argc, u16 *argv)
225 {
226 	int i, ret;
227 
228 	ret = par->board->wait_for_rdy(par);
229 	if (ret)
230 		return ret;
231 
232 	par->board->set_ctl(par, AUOK190X_I80_CS, 0);
233 	auok190x_issue_cmd(par, cmd);
234 
235 	for (i = 0; i < argc; i++)
236 		argv[i] = auok190x_read_data(par);
237 	par->board->set_ctl(par, AUOK190X_I80_CS, 1);
238 
239 	return 0;
240 }
241 EXPORT_SYMBOL_GPL(auok190x_read_cmdargs);
242 
auok190x_send_cmdargs_pixels_nowait(struct auok190xfb_par * par,u16 cmd,int argc,u16 * argv,int size,u16 * data)243 void auok190x_send_cmdargs_pixels_nowait(struct auok190xfb_par *par, u16 cmd,
244 				  int argc, u16 *argv, int size, u16 *data)
245 {
246 	int i;
247 
248 	par->board->set_ctl(par, AUOK190X_I80_CS, 0);
249 
250 	auok190x_issue_cmd(par, cmd);
251 
252 	for (i = 0; i < argc; i++)
253 		auok190x_issue_data(par, argv[i]);
254 
255 	auok190x_issue_pixels(par, size, data);
256 
257 	par->board->set_ctl(par, AUOK190X_I80_CS, 1);
258 }
259 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_pixels_nowait);
260 
auok190x_send_cmdargs_pixels(struct auok190xfb_par * par,u16 cmd,int argc,u16 * argv,int size,u16 * data)261 int auok190x_send_cmdargs_pixels(struct auok190xfb_par *par, u16 cmd,
262 				  int argc, u16 *argv, int size, u16 *data)
263 {
264 	int ret;
265 
266 	ret = par->board->wait_for_rdy(par);
267 	if (ret)
268 		return ret;
269 
270 	auok190x_send_cmdargs_pixels_nowait(par, cmd, argc, argv, size, data);
271 
272 	return 0;
273 }
274 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_pixels);
275 
276 /*
277  * fbdefio callbacks - common on both controllers.
278  */
279 
auok190xfb_dpy_first_io(struct fb_info * info)280 static void auok190xfb_dpy_first_io(struct fb_info *info)
281 {
282 	/* tell runtime-pm that we wish to use the device in a short time */
283 	pm_runtime_get(info->device);
284 }
285 
286 /* this is called back from the deferred io workqueue */
auok190xfb_dpy_deferred_io(struct fb_info * info,struct list_head * pagelist)287 static void auok190xfb_dpy_deferred_io(struct fb_info *info,
288 				struct list_head *pagelist)
289 {
290 	struct fb_deferred_io *fbdefio = info->fbdefio;
291 	struct auok190xfb_par *par = info->par;
292 	u16 line_length = info->fix.line_length;
293 	u16 yres = info->var.yres;
294 	u16 y1 = 0, h = 0;
295 	int prev_index = -1;
296 	struct page *cur;
297 	int h_inc;
298 	int threshold;
299 
300 	if (!list_empty(pagelist))
301 		/* the device resume should've been requested through first_io,
302 		 * if the resume did not finish until now, wait for it.
303 		 */
304 		pm_runtime_barrier(info->device);
305 	else
306 		/* We reached this via the fsync or some other way.
307 		 * In either case the first_io function did not run,
308 		 * so we runtime_resume the device here synchronously.
309 		 */
310 		pm_runtime_get_sync(info->device);
311 
312 	/* Do a full screen update every n updates to prevent
313 	 * excessive darkening of the Sipix display.
314 	 * If we do this, there is no need to walk the pages.
315 	 */
316 	if (par->need_refresh(par)) {
317 		par->update_all(par);
318 		goto out;
319 	}
320 
321 	/* height increment is fixed per page */
322 	h_inc = DIV_ROUND_UP(PAGE_SIZE , line_length);
323 
324 	/* calculate number of pages from pixel height */
325 	threshold = par->consecutive_threshold / h_inc;
326 	if (threshold < 1)
327 		threshold = 1;
328 
329 	/* walk the written page list and swizzle the data */
330 	list_for_each_entry(cur, &fbdefio->pagelist, lru) {
331 		if (prev_index < 0) {
332 			/* just starting so assign first page */
333 			y1 = (cur->index << PAGE_SHIFT) / line_length;
334 			h = h_inc;
335 		} else if ((cur->index - prev_index) <= threshold) {
336 			/* page is within our threshold for single updates */
337 			h += h_inc * (cur->index - prev_index);
338 		} else {
339 			/* page not consecutive, issue previous update first */
340 			par->update_partial(par, y1, y1 + h);
341 
342 			/* start over with our non consecutive page */
343 			y1 = (cur->index << PAGE_SHIFT) / line_length;
344 			h = h_inc;
345 		}
346 		prev_index = cur->index;
347 	}
348 
349 	/* if we still have any pages to update we do so now */
350 	if (h >= yres)
351 		/* its a full screen update, just do it */
352 		par->update_all(par);
353 	else
354 		par->update_partial(par, y1, min((u16) (y1 + h), yres));
355 
356 out:
357 	pm_runtime_mark_last_busy(info->device);
358 	pm_runtime_put_autosuspend(info->device);
359 }
360 
361 /*
362  * framebuffer operations
363  */
364 
365 /*
366  * this is the slow path from userspace. they can seek and write to
367  * the fb. it's inefficient to do anything less than a full screen draw
368  */
auok190xfb_write(struct fb_info * info,const char __user * buf,size_t count,loff_t * ppos)369 static ssize_t auok190xfb_write(struct fb_info *info, const char __user *buf,
370 				size_t count, loff_t *ppos)
371 {
372 	struct auok190xfb_par *par = info->par;
373 	unsigned long p = *ppos;
374 	void *dst;
375 	int err = 0;
376 	unsigned long total_size;
377 
378 	if (info->state != FBINFO_STATE_RUNNING)
379 		return -EPERM;
380 
381 	total_size = info->fix.smem_len;
382 
383 	if (p > total_size)
384 		return -EFBIG;
385 
386 	if (count > total_size) {
387 		err = -EFBIG;
388 		count = total_size;
389 	}
390 
391 	if (count + p > total_size) {
392 		if (!err)
393 			err = -ENOSPC;
394 
395 		count = total_size - p;
396 	}
397 
398 	dst = (void *)(info->screen_base + p);
399 
400 	if (copy_from_user(dst, buf, count))
401 		err = -EFAULT;
402 
403 	if  (!err)
404 		*ppos += count;
405 
406 	par->update_all(par);
407 
408 	return (err) ? err : count;
409 }
410 
auok190xfb_fillrect(struct fb_info * info,const struct fb_fillrect * rect)411 static void auok190xfb_fillrect(struct fb_info *info,
412 				   const struct fb_fillrect *rect)
413 {
414 	struct auok190xfb_par *par = info->par;
415 
416 	sys_fillrect(info, rect);
417 
418 	par->update_all(par);
419 }
420 
auok190xfb_copyarea(struct fb_info * info,const struct fb_copyarea * area)421 static void auok190xfb_copyarea(struct fb_info *info,
422 				   const struct fb_copyarea *area)
423 {
424 	struct auok190xfb_par *par = info->par;
425 
426 	sys_copyarea(info, area);
427 
428 	par->update_all(par);
429 }
430 
auok190xfb_imageblit(struct fb_info * info,const struct fb_image * image)431 static void auok190xfb_imageblit(struct fb_info *info,
432 				const struct fb_image *image)
433 {
434 	struct auok190xfb_par *par = info->par;
435 
436 	sys_imageblit(info, image);
437 
438 	par->update_all(par);
439 }
440 
auok190xfb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)441 static int auok190xfb_check_var(struct fb_var_screeninfo *var,
442 				   struct fb_info *info)
443 {
444 	struct device *dev = info->device;
445 	struct auok190xfb_par *par = info->par;
446 	struct panel_info *panel = &panel_table[par->resolution];
447 	int size;
448 
449 	/*
450 	 * Color depth
451 	 */
452 
453 	if (var->bits_per_pixel == 8 && var->grayscale == 1) {
454 		/*
455 		 * For 8-bit grayscale, R, G, and B offset are equal.
456 		 */
457 		var->red.length = 8;
458 		var->red.offset = 0;
459 		var->red.msb_right = 0;
460 
461 		var->green.length = 8;
462 		var->green.offset = 0;
463 		var->green.msb_right = 0;
464 
465 		var->blue.length = 8;
466 		var->blue.offset = 0;
467 		var->blue.msb_right = 0;
468 
469 		var->transp.length = 0;
470 		var->transp.offset = 0;
471 		var->transp.msb_right = 0;
472 	} else if (var->bits_per_pixel == 16) {
473 		var->red.length = 5;
474 		var->red.offset = 11;
475 		var->red.msb_right = 0;
476 
477 		var->green.length = 6;
478 		var->green.offset = 5;
479 		var->green.msb_right = 0;
480 
481 		var->blue.length = 5;
482 		var->blue.offset = 0;
483 		var->blue.msb_right = 0;
484 
485 		var->transp.length = 0;
486 		var->transp.offset = 0;
487 		var->transp.msb_right = 0;
488 	} else {
489 		dev_warn(dev, "unsupported color mode (bits: %d, grayscale: %d)\n",
490 			info->var.bits_per_pixel, info->var.grayscale);
491 		return -EINVAL;
492 	}
493 
494 	/*
495 	 * Dimensions
496 	 */
497 
498 	switch (var->rotate) {
499 	case FB_ROTATE_UR:
500 	case FB_ROTATE_UD:
501 		var->xres = panel->w;
502 		var->yres = panel->h;
503 		break;
504 	case FB_ROTATE_CW:
505 	case FB_ROTATE_CCW:
506 		var->xres = panel->h;
507 		var->yres = panel->w;
508 		break;
509 	default:
510 		dev_dbg(dev, "Invalid rotation request\n");
511 		return -EINVAL;
512 	}
513 
514 	var->xres_virtual = var->xres;
515 	var->yres_virtual = var->yres;
516 
517 	/*
518 	 *  Memory limit
519 	 */
520 
521 	size = var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8;
522 	if (size > info->fix.smem_len) {
523 		dev_err(dev, "Memory limit exceeded, requested %dK\n",
524 			size >> 10);
525 		return -ENOMEM;
526 	}
527 
528 	return 0;
529 }
530 
auok190xfb_set_fix(struct fb_info * info)531 static int auok190xfb_set_fix(struct fb_info *info)
532 {
533 	struct fb_fix_screeninfo *fix = &info->fix;
534 	struct fb_var_screeninfo *var = &info->var;
535 
536 	fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
537 
538 	fix->type = FB_TYPE_PACKED_PIXELS;
539 	fix->accel = FB_ACCEL_NONE;
540 	fix->visual = (var->grayscale) ? FB_VISUAL_STATIC_PSEUDOCOLOR
541 				       : FB_VISUAL_TRUECOLOR;
542 	fix->xpanstep = 0;
543 	fix->ypanstep = 0;
544 	fix->ywrapstep = 0;
545 
546 	return 0;
547 }
548 
auok190xfb_set_par(struct fb_info * info)549 static int auok190xfb_set_par(struct fb_info *info)
550 {
551 	struct auok190xfb_par *par = info->par;
552 
553 	par->rotation = info->var.rotate;
554 	auok190xfb_set_fix(info);
555 
556 	/* reinit the controller to honor the rotation */
557 	par->init(par);
558 
559 	/* wait for init to complete */
560 	par->board->wait_for_rdy(par);
561 
562 	return 0;
563 }
564 
565 static struct fb_ops auok190xfb_ops = {
566 	.owner		= THIS_MODULE,
567 	.fb_read	= fb_sys_read,
568 	.fb_write	= auok190xfb_write,
569 	.fb_fillrect	= auok190xfb_fillrect,
570 	.fb_copyarea	= auok190xfb_copyarea,
571 	.fb_imageblit	= auok190xfb_imageblit,
572 	.fb_check_var	= auok190xfb_check_var,
573 	.fb_set_par     = auok190xfb_set_par,
574 };
575 
576 /*
577  * Controller-functions common to both K1900 and K1901
578  */
579 
auok190x_read_temperature(struct auok190xfb_par * par)580 static int auok190x_read_temperature(struct auok190xfb_par *par)
581 {
582 	struct device *dev = par->info->device;
583 	u16 data[4];
584 	int temp;
585 
586 	pm_runtime_get_sync(dev);
587 
588 	mutex_lock(&(par->io_lock));
589 
590 	auok190x_read_cmdargs(par, AUOK190X_CMD_READ_VERSION, 4, data);
591 
592 	mutex_unlock(&(par->io_lock));
593 
594 	pm_runtime_mark_last_busy(dev);
595 	pm_runtime_put_autosuspend(dev);
596 
597 	/* sanitize and split of half-degrees for now */
598 	temp = ((data[0] & AUOK190X_VERSION_TEMP_MASK) >> 1);
599 
600 	/* handle positive and negative temperatures */
601 	if (temp >= 201)
602 		return (255 - temp + 1) * (-1);
603 	else
604 		return temp;
605 }
606 
auok190x_identify(struct auok190xfb_par * par)607 static void auok190x_identify(struct auok190xfb_par *par)
608 {
609 	struct device *dev = par->info->device;
610 	u16 data[4];
611 
612 	pm_runtime_get_sync(dev);
613 
614 	mutex_lock(&(par->io_lock));
615 
616 	auok190x_read_cmdargs(par, AUOK190X_CMD_READ_VERSION, 4, data);
617 
618 	mutex_unlock(&(par->io_lock));
619 
620 	par->epd_type = data[1] & AUOK190X_VERSION_TEMP_MASK;
621 
622 	par->panel_size_int = AUOK190X_VERSION_SIZE_INT(data[2]);
623 	par->panel_size_float = AUOK190X_VERSION_SIZE_FLOAT(data[2]);
624 	par->panel_model = AUOK190X_VERSION_MODEL(data[2]);
625 
626 	par->tcon_version = AUOK190X_VERSION_TCON(data[3]);
627 	par->lut_version = AUOK190X_VERSION_LUT(data[3]);
628 
629 	dev_dbg(dev, "panel %d.%din, model 0x%x, EPD 0x%x TCON-rev 0x%x, LUT-rev 0x%x",
630 		par->panel_size_int, par->panel_size_float, par->panel_model,
631 		par->epd_type, par->tcon_version, par->lut_version);
632 
633 	pm_runtime_mark_last_busy(dev);
634 	pm_runtime_put_autosuspend(dev);
635 }
636 
637 /*
638  * Sysfs functions
639  */
640 
update_mode_show(struct device * dev,struct device_attribute * attr,char * buf)641 static ssize_t update_mode_show(struct device *dev,
642 				struct device_attribute *attr, char *buf)
643 {
644 	struct fb_info *info = dev_get_drvdata(dev);
645 	struct auok190xfb_par *par = info->par;
646 
647 	return sprintf(buf, "%d\n", par->update_mode);
648 }
649 
update_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)650 static ssize_t update_mode_store(struct device *dev,
651 				 struct device_attribute *attr,
652 				 const char *buf, size_t count)
653 {
654 	struct fb_info *info = dev_get_drvdata(dev);
655 	struct auok190xfb_par *par = info->par;
656 	int mode, ret;
657 
658 	ret = kstrtoint(buf, 10, &mode);
659 	if (ret)
660 		return ret;
661 
662 	par->update_mode = mode;
663 
664 	/* if we enter a better mode, do a full update */
665 	if (par->last_mode > 1 && mode < par->last_mode)
666 		par->update_all(par);
667 
668 	return count;
669 }
670 
flash_show(struct device * dev,struct device_attribute * attr,char * buf)671 static ssize_t flash_show(struct device *dev, struct device_attribute *attr,
672 			  char *buf)
673 {
674 	struct fb_info *info = dev_get_drvdata(dev);
675 	struct auok190xfb_par *par = info->par;
676 
677 	return sprintf(buf, "%d\n", par->flash);
678 }
679 
flash_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)680 static ssize_t flash_store(struct device *dev, struct device_attribute *attr,
681 			   const char *buf, size_t count)
682 {
683 	struct fb_info *info = dev_get_drvdata(dev);
684 	struct auok190xfb_par *par = info->par;
685 	int flash, ret;
686 
687 	ret = kstrtoint(buf, 10, &flash);
688 	if (ret)
689 		return ret;
690 
691 	if (flash > 0)
692 		par->flash = 1;
693 	else
694 		par->flash = 0;
695 
696 	return count;
697 }
698 
temp_show(struct device * dev,struct device_attribute * attr,char * buf)699 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
700 			 char *buf)
701 {
702 	struct fb_info *info = dev_get_drvdata(dev);
703 	struct auok190xfb_par *par = info->par;
704 	int temp;
705 
706 	temp = auok190x_read_temperature(par);
707 	return sprintf(buf, "%d\n", temp);
708 }
709 
710 static DEVICE_ATTR(update_mode, 0644, update_mode_show, update_mode_store);
711 static DEVICE_ATTR(flash, 0644, flash_show, flash_store);
712 static DEVICE_ATTR(temp, 0644, temp_show, NULL);
713 
714 static struct attribute *auok190x_attributes[] = {
715 	&dev_attr_update_mode.attr,
716 	&dev_attr_flash.attr,
717 	&dev_attr_temp.attr,
718 	NULL
719 };
720 
721 static const struct attribute_group auok190x_attr_group = {
722 	.attrs		= auok190x_attributes,
723 };
724 
auok190x_power(struct auok190xfb_par * par,bool on)725 static int auok190x_power(struct auok190xfb_par *par, bool on)
726 {
727 	struct auok190x_board *board = par->board;
728 	int ret;
729 
730 	if (on) {
731 		/* We should maintain POWER up for at least 80ms before set
732 		 * RST_N and SLP_N to high (TCON spec 20100803_v35 p59)
733 		 */
734 		ret = regulator_enable(par->regulator);
735 		if (ret)
736 			return ret;
737 
738 		msleep(200);
739 		gpio_set_value(board->gpio_nrst, 1);
740 		gpio_set_value(board->gpio_nsleep, 1);
741 		msleep(200);
742 	} else {
743 		regulator_disable(par->regulator);
744 		gpio_set_value(board->gpio_nrst, 0);
745 		gpio_set_value(board->gpio_nsleep, 0);
746 	}
747 
748 	return 0;
749 }
750 
751 /*
752  * Recovery - powercycle the controller
753  */
754 
auok190x_recover(struct auok190xfb_par * par)755 static void auok190x_recover(struct auok190xfb_par *par)
756 {
757 	struct device *dev = par->info->device;
758 
759 	auok190x_power(par, 0);
760 	msleep(100);
761 	auok190x_power(par, 1);
762 
763 	/* after powercycling the device, it's always active */
764 	pm_runtime_set_active(dev);
765 	par->standby = 0;
766 
767 	par->init(par);
768 
769 	/* wait for init to complete */
770 	par->board->wait_for_rdy(par);
771 }
772 
773 /*
774  * Power-management
775  */
auok190x_runtime_suspend(struct device * dev)776 static int __maybe_unused auok190x_runtime_suspend(struct device *dev)
777 {
778 	struct platform_device *pdev = to_platform_device(dev);
779 	struct fb_info *info = platform_get_drvdata(pdev);
780 	struct auok190xfb_par *par = info->par;
781 	struct auok190x_board *board = par->board;
782 	u16 standby_param;
783 
784 	/* take and keep the lock until we are resumed, as the controller
785 	 * will never reach the non-busy state when in standby mode
786 	 */
787 	mutex_lock(&(par->io_lock));
788 
789 	if (par->standby) {
790 		dev_warn(dev, "already in standby, runtime-pm pairing mismatch\n");
791 		mutex_unlock(&(par->io_lock));
792 		return 0;
793 	}
794 
795 	/* according to runtime_pm.txt runtime_suspend only means, that the
796 	 * device will not process data and will not communicate with the CPU
797 	 * As we hold the lock, this stays true even without standby
798 	 */
799 	if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
800 		dev_dbg(dev, "runtime suspend without standby\n");
801 		goto finish;
802 	} else if (board->quirks & AUOK190X_QUIRK_STANDBYPARAM) {
803 		/* for some TCON versions STANDBY expects a parameter (0) but
804 		 * it seems the real tcon version has to be determined yet.
805 		 */
806 		dev_dbg(dev, "runtime suspend with additional empty param\n");
807 		standby_param = 0;
808 		auok190x_send_cmdargs(par, AUOK190X_CMD_STANDBY, 1,
809 				      &standby_param);
810 	} else {
811 		dev_dbg(dev, "runtime suspend without param\n");
812 		auok190x_send_command(par, AUOK190X_CMD_STANDBY);
813 	}
814 
815 	msleep(64);
816 
817 finish:
818 	par->standby = 1;
819 
820 	return 0;
821 }
822 
auok190x_runtime_resume(struct device * dev)823 static int __maybe_unused auok190x_runtime_resume(struct device *dev)
824 {
825 	struct platform_device *pdev = to_platform_device(dev);
826 	struct fb_info *info = platform_get_drvdata(pdev);
827 	struct auok190xfb_par *par = info->par;
828 	struct auok190x_board *board = par->board;
829 
830 	if (!par->standby) {
831 		dev_warn(dev, "not in standby, runtime-pm pairing mismatch\n");
832 		return 0;
833 	}
834 
835 	if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
836 		dev_dbg(dev, "runtime resume without standby\n");
837 	} else {
838 		/* when in standby, controller is always busy
839 		 * and only accepts the wakeup command
840 		 */
841 		dev_dbg(dev, "runtime resume from standby\n");
842 		auok190x_send_command_nowait(par, AUOK190X_CMD_WAKEUP);
843 
844 		msleep(160);
845 
846 		/* wait for the controller to be ready and release the lock */
847 		board->wait_for_rdy(par);
848 	}
849 
850 	par->standby = 0;
851 
852 	mutex_unlock(&(par->io_lock));
853 
854 	return 0;
855 }
856 
auok190x_suspend(struct device * dev)857 static int __maybe_unused auok190x_suspend(struct device *dev)
858 {
859 	struct platform_device *pdev = to_platform_device(dev);
860 	struct fb_info *info = platform_get_drvdata(pdev);
861 	struct auok190xfb_par *par = info->par;
862 	struct auok190x_board *board = par->board;
863 	int ret;
864 
865 	dev_dbg(dev, "suspend\n");
866 	if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
867 		/* suspend via powering off the ic */
868 		dev_dbg(dev, "suspend with broken standby\n");
869 
870 		auok190x_power(par, 0);
871 	} else {
872 		dev_dbg(dev, "suspend using sleep\n");
873 
874 		/* the sleep state can only be entered from the standby state.
875 		 * pm_runtime_get_noresume gets called before the suspend call.
876 		 * So the devices usage count is >0 but it is not necessarily
877 		 * active.
878 		 */
879 		if (!pm_runtime_status_suspended(dev)) {
880 			ret = auok190x_runtime_suspend(dev);
881 			if (ret < 0) {
882 				dev_err(dev, "auok190x_runtime_suspend failed with %d\n",
883 					ret);
884 				return ret;
885 			}
886 			par->manual_standby = 1;
887 		}
888 
889 		gpio_direction_output(board->gpio_nsleep, 0);
890 	}
891 
892 	msleep(100);
893 
894 	return 0;
895 }
896 
auok190x_resume(struct device * dev)897 static int __maybe_unused auok190x_resume(struct device *dev)
898 {
899 	struct platform_device *pdev = to_platform_device(dev);
900 	struct fb_info *info = platform_get_drvdata(pdev);
901 	struct auok190xfb_par *par = info->par;
902 	struct auok190x_board *board = par->board;
903 
904 	dev_dbg(dev, "resume\n");
905 	if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
906 		dev_dbg(dev, "resume with broken standby\n");
907 
908 		auok190x_power(par, 1);
909 
910 		par->init(par);
911 	} else {
912 		dev_dbg(dev, "resume from sleep\n");
913 
914 		/* device should be in runtime suspend when we were suspended
915 		 * and pm_runtime_put_sync gets called after this function.
916 		 * So there is no need to touch the standby mode here at all.
917 		 */
918 		gpio_direction_output(board->gpio_nsleep, 1);
919 		msleep(100);
920 
921 		/* an additional init call seems to be necessary after sleep */
922 		auok190x_runtime_resume(dev);
923 		par->init(par);
924 
925 		/* if we were runtime-suspended before, suspend again*/
926 		if (!par->manual_standby)
927 			auok190x_runtime_suspend(dev);
928 		else
929 			par->manual_standby = 0;
930 	}
931 
932 	return 0;
933 }
934 
935 const struct dev_pm_ops auok190x_pm = {
936 	SET_RUNTIME_PM_OPS(auok190x_runtime_suspend, auok190x_runtime_resume,
937 			   NULL)
938 	SET_SYSTEM_SLEEP_PM_OPS(auok190x_suspend, auok190x_resume)
939 };
940 EXPORT_SYMBOL_GPL(auok190x_pm);
941 
942 /*
943  * Common probe and remove code
944  */
945 
auok190x_common_probe(struct platform_device * pdev,struct auok190x_init_data * init)946 int auok190x_common_probe(struct platform_device *pdev,
947 			  struct auok190x_init_data *init)
948 {
949 	struct auok190x_board *board = init->board;
950 	struct auok190xfb_par *par;
951 	struct fb_info *info;
952 	struct panel_info *panel;
953 	int videomemorysize, ret;
954 	unsigned char *videomemory;
955 
956 	/* check board contents */
957 	if (!board->init || !board->cleanup || !board->wait_for_rdy
958 	    || !board->set_ctl || !board->set_hdb || !board->get_hdb
959 	    || !board->setup_irq)
960 		return -EINVAL;
961 
962 	info = framebuffer_alloc(sizeof(struct auok190xfb_par), &pdev->dev);
963 	if (!info)
964 		return -ENOMEM;
965 
966 	par = info->par;
967 	par->info = info;
968 	par->board = board;
969 	par->recover = auok190x_recover;
970 	par->update_partial = init->update_partial;
971 	par->update_all = init->update_all;
972 	par->need_refresh = init->need_refresh;
973 	par->init = init->init;
974 
975 	/* init update modes */
976 	par->update_cnt = 0;
977 	par->update_mode = -1;
978 	par->last_mode = -1;
979 	par->flash = 0;
980 
981 	par->regulator = regulator_get(info->device, "vdd");
982 	if (IS_ERR(par->regulator)) {
983 		ret = PTR_ERR(par->regulator);
984 		dev_err(info->device, "Failed to get regulator: %d\n", ret);
985 		goto err_reg;
986 	}
987 
988 	ret = board->init(par);
989 	if (ret) {
990 		dev_err(info->device, "board init failed, %d\n", ret);
991 		goto err_board;
992 	}
993 
994 	ret = gpio_request(board->gpio_nsleep, "AUOK190x sleep");
995 	if (ret) {
996 		dev_err(info->device, "could not request sleep gpio, %d\n",
997 			ret);
998 		goto err_gpio1;
999 	}
1000 
1001 	ret = gpio_direction_output(board->gpio_nsleep, 0);
1002 	if (ret) {
1003 		dev_err(info->device, "could not set sleep gpio, %d\n", ret);
1004 		goto err_gpio2;
1005 	}
1006 
1007 	ret = gpio_request(board->gpio_nrst, "AUOK190x reset");
1008 	if (ret) {
1009 		dev_err(info->device, "could not request reset gpio, %d\n",
1010 			ret);
1011 		goto err_gpio2;
1012 	}
1013 
1014 	ret = gpio_direction_output(board->gpio_nrst, 0);
1015 	if (ret) {
1016 		dev_err(info->device, "could not set reset gpio, %d\n", ret);
1017 		goto err_gpio3;
1018 	}
1019 
1020 	ret = auok190x_power(par, 1);
1021 	if (ret) {
1022 		dev_err(info->device, "could not power on the device, %d\n",
1023 			ret);
1024 		goto err_gpio3;
1025 	}
1026 
1027 	mutex_init(&par->io_lock);
1028 
1029 	init_waitqueue_head(&par->waitq);
1030 
1031 	ret = par->board->setup_irq(par->info);
1032 	if (ret) {
1033 		dev_err(info->device, "could not setup ready-irq, %d\n", ret);
1034 		goto err_irq;
1035 	}
1036 
1037 	/* wait for init to complete */
1038 	par->board->wait_for_rdy(par);
1039 
1040 	/*
1041 	 * From here on the controller can talk to us
1042 	 */
1043 
1044 	/* initialise fix, var, resolution and rotation */
1045 
1046 	strlcpy(info->fix.id, init->id, 16);
1047 	info->var.bits_per_pixel = 8;
1048 	info->var.grayscale = 1;
1049 
1050 	panel = &panel_table[board->resolution];
1051 
1052 	par->resolution = board->resolution;
1053 	par->rotation = 0;
1054 
1055 	/* videomemory handling */
1056 
1057 	videomemorysize = roundup((panel->w * panel->h) * 2, PAGE_SIZE);
1058 	videomemory = vmalloc(videomemorysize);
1059 	if (!videomemory) {
1060 		ret = -ENOMEM;
1061 		goto err_irq;
1062 	}
1063 
1064 	memset(videomemory, 0, videomemorysize);
1065 	info->screen_base = (char *)videomemory;
1066 	info->fix.smem_len = videomemorysize;
1067 
1068 	info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
1069 	info->fbops = &auok190xfb_ops;
1070 
1071 	ret = auok190xfb_check_var(&info->var, info);
1072 	if (ret)
1073 		goto err_defio;
1074 
1075 	auok190xfb_set_fix(info);
1076 
1077 	/* deferred io init */
1078 
1079 	info->fbdefio = devm_kzalloc(info->device,
1080 				     sizeof(struct fb_deferred_io),
1081 				     GFP_KERNEL);
1082 	if (!info->fbdefio) {
1083 		dev_err(info->device, "Failed to allocate memory\n");
1084 		ret = -ENOMEM;
1085 		goto err_defio;
1086 	}
1087 
1088 	dev_dbg(info->device, "targeting %d frames per second\n", board->fps);
1089 	info->fbdefio->delay = HZ / board->fps;
1090 	info->fbdefio->first_io = auok190xfb_dpy_first_io,
1091 	info->fbdefio->deferred_io = auok190xfb_dpy_deferred_io,
1092 	fb_deferred_io_init(info);
1093 
1094 	/* color map */
1095 
1096 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
1097 	if (ret < 0) {
1098 		dev_err(info->device, "Failed to allocate colormap\n");
1099 		goto err_cmap;
1100 	}
1101 
1102 	/* controller init */
1103 
1104 	par->consecutive_threshold = 100;
1105 	par->init(par);
1106 	auok190x_identify(par);
1107 
1108 	platform_set_drvdata(pdev, info);
1109 
1110 	ret = register_framebuffer(info);
1111 	if (ret < 0)
1112 		goto err_regfb;
1113 
1114 	ret = sysfs_create_group(&info->device->kobj, &auok190x_attr_group);
1115 	if (ret)
1116 		goto err_sysfs;
1117 
1118 	dev_info(info->device, "fb%d: %dx%d using %dK of video memory\n",
1119 		 info->node, info->var.xres, info->var.yres,
1120 		 videomemorysize >> 10);
1121 
1122 	/* increase autosuspend_delay when we use alternative methods
1123 	 * for runtime_pm
1124 	 */
1125 	par->autosuspend_delay = (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN)
1126 					? 1000 : 200;
1127 
1128 	pm_runtime_set_active(info->device);
1129 	pm_runtime_enable(info->device);
1130 	pm_runtime_set_autosuspend_delay(info->device, par->autosuspend_delay);
1131 	pm_runtime_use_autosuspend(info->device);
1132 
1133 	return 0;
1134 
1135 err_sysfs:
1136 	unregister_framebuffer(info);
1137 err_regfb:
1138 	fb_dealloc_cmap(&info->cmap);
1139 err_cmap:
1140 	fb_deferred_io_cleanup(info);
1141 err_defio:
1142 	vfree((void *)info->screen_base);
1143 err_irq:
1144 	auok190x_power(par, 0);
1145 err_gpio3:
1146 	gpio_free(board->gpio_nrst);
1147 err_gpio2:
1148 	gpio_free(board->gpio_nsleep);
1149 err_gpio1:
1150 	board->cleanup(par);
1151 err_board:
1152 	regulator_put(par->regulator);
1153 err_reg:
1154 	framebuffer_release(info);
1155 
1156 	return ret;
1157 }
1158 EXPORT_SYMBOL_GPL(auok190x_common_probe);
1159 
auok190x_common_remove(struct platform_device * pdev)1160 int  auok190x_common_remove(struct platform_device *pdev)
1161 {
1162 	struct fb_info *info = platform_get_drvdata(pdev);
1163 	struct auok190xfb_par *par = info->par;
1164 	struct auok190x_board *board = par->board;
1165 
1166 	pm_runtime_disable(info->device);
1167 
1168 	sysfs_remove_group(&info->device->kobj, &auok190x_attr_group);
1169 
1170 	unregister_framebuffer(info);
1171 
1172 	fb_dealloc_cmap(&info->cmap);
1173 
1174 	fb_deferred_io_cleanup(info);
1175 
1176 	vfree((void *)info->screen_base);
1177 
1178 	auok190x_power(par, 0);
1179 
1180 	gpio_free(board->gpio_nrst);
1181 	gpio_free(board->gpio_nsleep);
1182 
1183 	board->cleanup(par);
1184 
1185 	regulator_put(par->regulator);
1186 
1187 	framebuffer_release(info);
1188 
1189 	return 0;
1190 }
1191 EXPORT_SYMBOL_GPL(auok190x_common_remove);
1192 
1193 MODULE_DESCRIPTION("Common code for AUO-K190X controllers");
1194 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
1195 MODULE_LICENSE("GPL");
1196