Linux 4.16.11
[linux/fpc-iii.git] / drivers / video / fbdev / auo_k190x.c
blob9d24d1b3e9ef3e7ca92b99554bdd646f7710b5fe
1 /*
2 * Common code for AUO-K190X framebuffer drivers
4 * Copyright (C) 2012 Heiko Stuebner <heiko@sntech.de>
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 */
11 #include <linux/module.h>
12 #include <linux/sched/mm.h>
13 #include <linux/kernel.h>
14 #include <linux/gpio.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/fb.h>
18 #include <linux/delay.h>
19 #include <linux/uaccess.h>
20 #include <linux/vmalloc.h>
21 #include <linux/regulator/consumer.h>
23 #include <video/auo_k190xfb.h>
25 #include "auo_k190x.h"
27 struct panel_info {
28 int w;
29 int h;
32 /* table of panel specific parameters to be indexed into by the board drivers */
33 static struct panel_info panel_table[] = {
34 /* standard 6" */
35 [AUOK190X_RESOLUTION_800_600] = {
36 .w = 800,
37 .h = 600,
39 /* standard 9" */
40 [AUOK190X_RESOLUTION_1024_768] = {
41 .w = 1024,
42 .h = 768,
44 [AUOK190X_RESOLUTION_600_800] = {
45 .w = 600,
46 .h = 800,
48 [AUOK190X_RESOLUTION_768_1024] = {
49 .w = 768,
50 .h = 1024,
55 * private I80 interface to the board driver
58 static void auok190x_issue_data(struct auok190xfb_par *par, u16 data)
60 par->board->set_ctl(par, AUOK190X_I80_WR, 0);
61 par->board->set_hdb(par, data);
62 par->board->set_ctl(par, AUOK190X_I80_WR, 1);
65 static void auok190x_issue_cmd(struct auok190xfb_par *par, u16 data)
67 par->board->set_ctl(par, AUOK190X_I80_DC, 0);
68 auok190x_issue_data(par, data);
69 par->board->set_ctl(par, AUOK190X_I80_DC, 1);
72 /**
73 * Conversion of 16bit color to 4bit grayscale
74 * does roughly (0.3 * R + 0.6 G + 0.1 B) / 2
76 static inline int rgb565_to_gray4(u16 data, struct fb_var_screeninfo *var)
78 return ((((data & 0xF800) >> var->red.offset) * 77 +
79 ((data & 0x07E0) >> (var->green.offset + 1)) * 151 +
80 ((data & 0x1F) >> var->blue.offset) * 28) >> 8 >> 1);
83 static int auok190x_issue_pixels_rgb565(struct auok190xfb_par *par, int size,
84 u16 *data)
86 struct fb_var_screeninfo *var = &par->info->var;
87 struct device *dev = par->info->device;
88 int i;
89 u16 tmp;
91 if (size & 7) {
92 dev_err(dev, "issue_pixels: size %d must be a multiple of 8\n",
93 size);
94 return -EINVAL;
97 for (i = 0; i < (size >> 2); i++) {
98 par->board->set_ctl(par, AUOK190X_I80_WR, 0);
100 tmp = (rgb565_to_gray4(data[4*i], var) & 0x000F);
101 tmp |= (rgb565_to_gray4(data[4*i+1], var) << 4) & 0x00F0;
102 tmp |= (rgb565_to_gray4(data[4*i+2], var) << 8) & 0x0F00;
103 tmp |= (rgb565_to_gray4(data[4*i+3], var) << 12) & 0xF000;
105 par->board->set_hdb(par, tmp);
106 par->board->set_ctl(par, AUOK190X_I80_WR, 1);
109 return 0;
112 static int auok190x_issue_pixels_gray8(struct auok190xfb_par *par, int size,
113 u16 *data)
115 struct device *dev = par->info->device;
116 int i;
117 u16 tmp;
119 if (size & 3) {
120 dev_err(dev, "issue_pixels: size %d must be a multiple of 4\n",
121 size);
122 return -EINVAL;
125 for (i = 0; i < (size >> 1); i++) {
126 par->board->set_ctl(par, AUOK190X_I80_WR, 0);
128 /* simple reduction of 8bit staticgray to 4bit gray
129 * combines 4 * 4bit pixel values into a 16bit value
131 tmp = (data[2*i] & 0xF0) >> 4;
132 tmp |= (data[2*i] & 0xF000) >> 8;
133 tmp |= (data[2*i+1] & 0xF0) << 4;
134 tmp |= (data[2*i+1] & 0xF000);
136 par->board->set_hdb(par, tmp);
137 par->board->set_ctl(par, AUOK190X_I80_WR, 1);
140 return 0;
143 static int auok190x_issue_pixels(struct auok190xfb_par *par, int size,
144 u16 *data)
146 struct fb_info *info = par->info;
147 struct device *dev = par->info->device;
149 if (info->var.bits_per_pixel == 8 && info->var.grayscale)
150 auok190x_issue_pixels_gray8(par, size, data);
151 else if (info->var.bits_per_pixel == 16)
152 auok190x_issue_pixels_rgb565(par, size, data);
153 else
154 dev_err(dev, "unsupported color mode (bits: %d, gray: %d)\n",
155 info->var.bits_per_pixel, info->var.grayscale);
157 return 0;
160 static u16 auok190x_read_data(struct auok190xfb_par *par)
162 u16 data;
164 par->board->set_ctl(par, AUOK190X_I80_OE, 0);
165 data = par->board->get_hdb(par);
166 par->board->set_ctl(par, AUOK190X_I80_OE, 1);
168 return data;
172 * Command interface for the controller drivers
175 void auok190x_send_command_nowait(struct auok190xfb_par *par, u16 data)
177 par->board->set_ctl(par, AUOK190X_I80_CS, 0);
178 auok190x_issue_cmd(par, data);
179 par->board->set_ctl(par, AUOK190X_I80_CS, 1);
181 EXPORT_SYMBOL_GPL(auok190x_send_command_nowait);
183 void auok190x_send_cmdargs_nowait(struct auok190xfb_par *par, u16 cmd,
184 int argc, u16 *argv)
186 int i;
188 par->board->set_ctl(par, AUOK190X_I80_CS, 0);
189 auok190x_issue_cmd(par, cmd);
191 for (i = 0; i < argc; i++)
192 auok190x_issue_data(par, argv[i]);
193 par->board->set_ctl(par, AUOK190X_I80_CS, 1);
195 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_nowait);
197 int auok190x_send_command(struct auok190xfb_par *par, u16 data)
199 int ret;
201 ret = par->board->wait_for_rdy(par);
202 if (ret)
203 return ret;
205 auok190x_send_command_nowait(par, data);
206 return 0;
208 EXPORT_SYMBOL_GPL(auok190x_send_command);
210 int auok190x_send_cmdargs(struct auok190xfb_par *par, u16 cmd,
211 int argc, u16 *argv)
213 int ret;
215 ret = par->board->wait_for_rdy(par);
216 if (ret)
217 return ret;
219 auok190x_send_cmdargs_nowait(par, cmd, argc, argv);
220 return 0;
222 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs);
224 int auok190x_read_cmdargs(struct auok190xfb_par *par, u16 cmd,
225 int argc, u16 *argv)
227 int i, ret;
229 ret = par->board->wait_for_rdy(par);
230 if (ret)
231 return ret;
233 par->board->set_ctl(par, AUOK190X_I80_CS, 0);
234 auok190x_issue_cmd(par, cmd);
236 for (i = 0; i < argc; i++)
237 argv[i] = auok190x_read_data(par);
238 par->board->set_ctl(par, AUOK190X_I80_CS, 1);
240 return 0;
242 EXPORT_SYMBOL_GPL(auok190x_read_cmdargs);
244 void auok190x_send_cmdargs_pixels_nowait(struct auok190xfb_par *par, u16 cmd,
245 int argc, u16 *argv, int size, u16 *data)
247 int i;
249 par->board->set_ctl(par, AUOK190X_I80_CS, 0);
251 auok190x_issue_cmd(par, cmd);
253 for (i = 0; i < argc; i++)
254 auok190x_issue_data(par, argv[i]);
256 auok190x_issue_pixels(par, size, data);
258 par->board->set_ctl(par, AUOK190X_I80_CS, 1);
260 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_pixels_nowait);
262 int auok190x_send_cmdargs_pixels(struct auok190xfb_par *par, u16 cmd,
263 int argc, u16 *argv, int size, u16 *data)
265 int ret;
267 ret = par->board->wait_for_rdy(par);
268 if (ret)
269 return ret;
271 auok190x_send_cmdargs_pixels_nowait(par, cmd, argc, argv, size, data);
273 return 0;
275 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_pixels);
278 * fbdefio callbacks - common on both controllers.
281 static void auok190xfb_dpy_first_io(struct fb_info *info)
283 /* tell runtime-pm that we wish to use the device in a short time */
284 pm_runtime_get(info->device);
287 /* this is called back from the deferred io workqueue */
288 static void auok190xfb_dpy_deferred_io(struct fb_info *info,
289 struct list_head *pagelist)
291 struct fb_deferred_io *fbdefio = info->fbdefio;
292 struct auok190xfb_par *par = info->par;
293 u16 line_length = info->fix.line_length;
294 u16 yres = info->var.yres;
295 u16 y1 = 0, h = 0;
296 int prev_index = -1;
297 struct page *cur;
298 int h_inc;
299 int threshold;
301 if (!list_empty(pagelist))
302 /* the device resume should've been requested through first_io,
303 * if the resume did not finish until now, wait for it.
305 pm_runtime_barrier(info->device);
306 else
307 /* We reached this via the fsync or some other way.
308 * In either case the first_io function did not run,
309 * so we runtime_resume the device here synchronously.
311 pm_runtime_get_sync(info->device);
313 /* Do a full screen update every n updates to prevent
314 * excessive darkening of the Sipix display.
315 * If we do this, there is no need to walk the pages.
317 if (par->need_refresh(par)) {
318 par->update_all(par);
319 goto out;
322 /* height increment is fixed per page */
323 h_inc = DIV_ROUND_UP(PAGE_SIZE , line_length);
325 /* calculate number of pages from pixel height */
326 threshold = par->consecutive_threshold / h_inc;
327 if (threshold < 1)
328 threshold = 1;
330 /* walk the written page list and swizzle the data */
331 list_for_each_entry(cur, &fbdefio->pagelist, lru) {
332 if (prev_index < 0) {
333 /* just starting so assign first page */
334 y1 = (cur->index << PAGE_SHIFT) / line_length;
335 h = h_inc;
336 } else if ((cur->index - prev_index) <= threshold) {
337 /* page is within our threshold for single updates */
338 h += h_inc * (cur->index - prev_index);
339 } else {
340 /* page not consecutive, issue previous update first */
341 par->update_partial(par, y1, y1 + h);
343 /* start over with our non consecutive page */
344 y1 = (cur->index << PAGE_SHIFT) / line_length;
345 h = h_inc;
347 prev_index = cur->index;
350 /* if we still have any pages to update we do so now */
351 if (h >= yres)
352 /* its a full screen update, just do it */
353 par->update_all(par);
354 else
355 par->update_partial(par, y1, min((u16) (y1 + h), yres));
357 out:
358 pm_runtime_mark_last_busy(info->device);
359 pm_runtime_put_autosuspend(info->device);
363 * framebuffer operations
367 * this is the slow path from userspace. they can seek and write to
368 * the fb. it's inefficient to do anything less than a full screen draw
370 static ssize_t auok190xfb_write(struct fb_info *info, const char __user *buf,
371 size_t count, loff_t *ppos)
373 struct auok190xfb_par *par = info->par;
374 unsigned long p = *ppos;
375 void *dst;
376 int err = 0;
377 unsigned long total_size;
379 if (info->state != FBINFO_STATE_RUNNING)
380 return -EPERM;
382 total_size = info->fix.smem_len;
384 if (p > total_size)
385 return -EFBIG;
387 if (count > total_size) {
388 err = -EFBIG;
389 count = total_size;
392 if (count + p > total_size) {
393 if (!err)
394 err = -ENOSPC;
396 count = total_size - p;
399 dst = (void *)(info->screen_base + p);
401 if (copy_from_user(dst, buf, count))
402 err = -EFAULT;
404 if (!err)
405 *ppos += count;
407 par->update_all(par);
409 return (err) ? err : count;
412 static void auok190xfb_fillrect(struct fb_info *info,
413 const struct fb_fillrect *rect)
415 struct auok190xfb_par *par = info->par;
417 sys_fillrect(info, rect);
419 par->update_all(par);
422 static void auok190xfb_copyarea(struct fb_info *info,
423 const struct fb_copyarea *area)
425 struct auok190xfb_par *par = info->par;
427 sys_copyarea(info, area);
429 par->update_all(par);
432 static void auok190xfb_imageblit(struct fb_info *info,
433 const struct fb_image *image)
435 struct auok190xfb_par *par = info->par;
437 sys_imageblit(info, image);
439 par->update_all(par);
442 static int auok190xfb_check_var(struct fb_var_screeninfo *var,
443 struct fb_info *info)
445 struct device *dev = info->device;
446 struct auok190xfb_par *par = info->par;
447 struct panel_info *panel = &panel_table[par->resolution];
448 int size;
451 * Color depth
454 if (var->bits_per_pixel == 8 && var->grayscale == 1) {
456 * For 8-bit grayscale, R, G, and B offset are equal.
458 var->red.length = 8;
459 var->red.offset = 0;
460 var->red.msb_right = 0;
462 var->green.length = 8;
463 var->green.offset = 0;
464 var->green.msb_right = 0;
466 var->blue.length = 8;
467 var->blue.offset = 0;
468 var->blue.msb_right = 0;
470 var->transp.length = 0;
471 var->transp.offset = 0;
472 var->transp.msb_right = 0;
473 } else if (var->bits_per_pixel == 16) {
474 var->red.length = 5;
475 var->red.offset = 11;
476 var->red.msb_right = 0;
478 var->green.length = 6;
479 var->green.offset = 5;
480 var->green.msb_right = 0;
482 var->blue.length = 5;
483 var->blue.offset = 0;
484 var->blue.msb_right = 0;
486 var->transp.length = 0;
487 var->transp.offset = 0;
488 var->transp.msb_right = 0;
489 } else {
490 dev_warn(dev, "unsupported color mode (bits: %d, grayscale: %d)\n",
491 info->var.bits_per_pixel, info->var.grayscale);
492 return -EINVAL;
496 * Dimensions
499 switch (var->rotate) {
500 case FB_ROTATE_UR:
501 case FB_ROTATE_UD:
502 var->xres = panel->w;
503 var->yres = panel->h;
504 break;
505 case FB_ROTATE_CW:
506 case FB_ROTATE_CCW:
507 var->xres = panel->h;
508 var->yres = panel->w;
509 break;
510 default:
511 dev_dbg(dev, "Invalid rotation request\n");
512 return -EINVAL;
515 var->xres_virtual = var->xres;
516 var->yres_virtual = var->yres;
519 * Memory limit
522 size = var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8;
523 if (size > info->fix.smem_len) {
524 dev_err(dev, "Memory limit exceeded, requested %dK\n",
525 size >> 10);
526 return -ENOMEM;
529 return 0;
532 static int auok190xfb_set_fix(struct fb_info *info)
534 struct fb_fix_screeninfo *fix = &info->fix;
535 struct fb_var_screeninfo *var = &info->var;
537 fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
539 fix->type = FB_TYPE_PACKED_PIXELS;
540 fix->accel = FB_ACCEL_NONE;
541 fix->visual = (var->grayscale) ? FB_VISUAL_STATIC_PSEUDOCOLOR
542 : FB_VISUAL_TRUECOLOR;
543 fix->xpanstep = 0;
544 fix->ypanstep = 0;
545 fix->ywrapstep = 0;
547 return 0;
550 static int auok190xfb_set_par(struct fb_info *info)
552 struct auok190xfb_par *par = info->par;
554 par->rotation = info->var.rotate;
555 auok190xfb_set_fix(info);
557 /* reinit the controller to honor the rotation */
558 par->init(par);
560 /* wait for init to complete */
561 par->board->wait_for_rdy(par);
563 return 0;
566 static struct fb_ops auok190xfb_ops = {
567 .owner = THIS_MODULE,
568 .fb_read = fb_sys_read,
569 .fb_write = auok190xfb_write,
570 .fb_fillrect = auok190xfb_fillrect,
571 .fb_copyarea = auok190xfb_copyarea,
572 .fb_imageblit = auok190xfb_imageblit,
573 .fb_check_var = auok190xfb_check_var,
574 .fb_set_par = auok190xfb_set_par,
578 * Controller-functions common to both K1900 and K1901
581 static int auok190x_read_temperature(struct auok190xfb_par *par)
583 struct device *dev = par->info->device;
584 u16 data[4];
585 int temp;
587 pm_runtime_get_sync(dev);
589 mutex_lock(&(par->io_lock));
591 auok190x_read_cmdargs(par, AUOK190X_CMD_READ_VERSION, 4, data);
593 mutex_unlock(&(par->io_lock));
595 pm_runtime_mark_last_busy(dev);
596 pm_runtime_put_autosuspend(dev);
598 /* sanitize and split of half-degrees for now */
599 temp = ((data[0] & AUOK190X_VERSION_TEMP_MASK) >> 1);
601 /* handle positive and negative temperatures */
602 if (temp >= 201)
603 return (255 - temp + 1) * (-1);
604 else
605 return temp;
608 static void auok190x_identify(struct auok190xfb_par *par)
610 struct device *dev = par->info->device;
611 u16 data[4];
613 pm_runtime_get_sync(dev);
615 mutex_lock(&(par->io_lock));
617 auok190x_read_cmdargs(par, AUOK190X_CMD_READ_VERSION, 4, data);
619 mutex_unlock(&(par->io_lock));
621 par->epd_type = data[1] & AUOK190X_VERSION_TEMP_MASK;
623 par->panel_size_int = AUOK190X_VERSION_SIZE_INT(data[2]);
624 par->panel_size_float = AUOK190X_VERSION_SIZE_FLOAT(data[2]);
625 par->panel_model = AUOK190X_VERSION_MODEL(data[2]);
627 par->tcon_version = AUOK190X_VERSION_TCON(data[3]);
628 par->lut_version = AUOK190X_VERSION_LUT(data[3]);
630 dev_dbg(dev, "panel %d.%din, model 0x%x, EPD 0x%x TCON-rev 0x%x, LUT-rev 0x%x",
631 par->panel_size_int, par->panel_size_float, par->panel_model,
632 par->epd_type, par->tcon_version, par->lut_version);
634 pm_runtime_mark_last_busy(dev);
635 pm_runtime_put_autosuspend(dev);
639 * Sysfs functions
642 static ssize_t update_mode_show(struct device *dev,
643 struct device_attribute *attr, char *buf)
645 struct fb_info *info = dev_get_drvdata(dev);
646 struct auok190xfb_par *par = info->par;
648 return sprintf(buf, "%d\n", par->update_mode);
651 static ssize_t update_mode_store(struct device *dev,
652 struct device_attribute *attr,
653 const char *buf, size_t count)
655 struct fb_info *info = dev_get_drvdata(dev);
656 struct auok190xfb_par *par = info->par;
657 int mode, ret;
659 ret = kstrtoint(buf, 10, &mode);
660 if (ret)
661 return ret;
663 par->update_mode = mode;
665 /* if we enter a better mode, do a full update */
666 if (par->last_mode > 1 && mode < par->last_mode)
667 par->update_all(par);
669 return count;
672 static ssize_t flash_show(struct device *dev, struct device_attribute *attr,
673 char *buf)
675 struct fb_info *info = dev_get_drvdata(dev);
676 struct auok190xfb_par *par = info->par;
678 return sprintf(buf, "%d\n", par->flash);
681 static ssize_t flash_store(struct device *dev, struct device_attribute *attr,
682 const char *buf, size_t count)
684 struct fb_info *info = dev_get_drvdata(dev);
685 struct auok190xfb_par *par = info->par;
686 int flash, ret;
688 ret = kstrtoint(buf, 10, &flash);
689 if (ret)
690 return ret;
692 if (flash > 0)
693 par->flash = 1;
694 else
695 par->flash = 0;
697 return count;
700 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
701 char *buf)
703 struct fb_info *info = dev_get_drvdata(dev);
704 struct auok190xfb_par *par = info->par;
705 int temp;
707 temp = auok190x_read_temperature(par);
708 return sprintf(buf, "%d\n", temp);
711 static DEVICE_ATTR_RW(update_mode);
712 static DEVICE_ATTR_RW(flash);
713 static DEVICE_ATTR(temp, 0644, temp_show, NULL);
715 static struct attribute *auok190x_attributes[] = {
716 &dev_attr_update_mode.attr,
717 &dev_attr_flash.attr,
718 &dev_attr_temp.attr,
719 NULL
722 static const struct attribute_group auok190x_attr_group = {
723 .attrs = auok190x_attributes,
726 static int auok190x_power(struct auok190xfb_par *par, bool on)
728 struct auok190x_board *board = par->board;
729 int ret;
731 if (on) {
732 /* We should maintain POWER up for at least 80ms before set
733 * RST_N and SLP_N to high (TCON spec 20100803_v35 p59)
735 ret = regulator_enable(par->regulator);
736 if (ret)
737 return ret;
739 msleep(200);
740 gpio_set_value(board->gpio_nrst, 1);
741 gpio_set_value(board->gpio_nsleep, 1);
742 msleep(200);
743 } else {
744 regulator_disable(par->regulator);
745 gpio_set_value(board->gpio_nrst, 0);
746 gpio_set_value(board->gpio_nsleep, 0);
749 return 0;
753 * Recovery - powercycle the controller
756 static void auok190x_recover(struct auok190xfb_par *par)
758 struct device *dev = par->info->device;
760 auok190x_power(par, 0);
761 msleep(100);
762 auok190x_power(par, 1);
764 /* after powercycling the device, it's always active */
765 pm_runtime_set_active(dev);
766 par->standby = 0;
768 par->init(par);
770 /* wait for init to complete */
771 par->board->wait_for_rdy(par);
775 * Power-management
777 static int __maybe_unused auok190x_runtime_suspend(struct device *dev)
779 struct platform_device *pdev = to_platform_device(dev);
780 struct fb_info *info = platform_get_drvdata(pdev);
781 struct auok190xfb_par *par = info->par;
782 struct auok190x_board *board = par->board;
783 u16 standby_param;
785 /* take and keep the lock until we are resumed, as the controller
786 * will never reach the non-busy state when in standby mode
788 mutex_lock(&(par->io_lock));
790 if (par->standby) {
791 dev_warn(dev, "already in standby, runtime-pm pairing mismatch\n");
792 mutex_unlock(&(par->io_lock));
793 return 0;
796 /* according to runtime_pm.txt runtime_suspend only means, that the
797 * device will not process data and will not communicate with the CPU
798 * As we hold the lock, this stays true even without standby
800 if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
801 dev_dbg(dev, "runtime suspend without standby\n");
802 goto finish;
803 } else if (board->quirks & AUOK190X_QUIRK_STANDBYPARAM) {
804 /* for some TCON versions STANDBY expects a parameter (0) but
805 * it seems the real tcon version has to be determined yet.
807 dev_dbg(dev, "runtime suspend with additional empty param\n");
808 standby_param = 0;
809 auok190x_send_cmdargs(par, AUOK190X_CMD_STANDBY, 1,
810 &standby_param);
811 } else {
812 dev_dbg(dev, "runtime suspend without param\n");
813 auok190x_send_command(par, AUOK190X_CMD_STANDBY);
816 msleep(64);
818 finish:
819 par->standby = 1;
821 return 0;
824 static int __maybe_unused auok190x_runtime_resume(struct device *dev)
826 struct platform_device *pdev = to_platform_device(dev);
827 struct fb_info *info = platform_get_drvdata(pdev);
828 struct auok190xfb_par *par = info->par;
829 struct auok190x_board *board = par->board;
831 if (!par->standby) {
832 dev_warn(dev, "not in standby, runtime-pm pairing mismatch\n");
833 return 0;
836 if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
837 dev_dbg(dev, "runtime resume without standby\n");
838 } else {
839 /* when in standby, controller is always busy
840 * and only accepts the wakeup command
842 dev_dbg(dev, "runtime resume from standby\n");
843 auok190x_send_command_nowait(par, AUOK190X_CMD_WAKEUP);
845 msleep(160);
847 /* wait for the controller to be ready and release the lock */
848 board->wait_for_rdy(par);
851 par->standby = 0;
853 mutex_unlock(&(par->io_lock));
855 return 0;
858 static int __maybe_unused auok190x_suspend(struct device *dev)
860 struct platform_device *pdev = to_platform_device(dev);
861 struct fb_info *info = platform_get_drvdata(pdev);
862 struct auok190xfb_par *par = info->par;
863 struct auok190x_board *board = par->board;
864 int ret;
866 dev_dbg(dev, "suspend\n");
867 if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
868 /* suspend via powering off the ic */
869 dev_dbg(dev, "suspend with broken standby\n");
871 auok190x_power(par, 0);
872 } else {
873 dev_dbg(dev, "suspend using sleep\n");
875 /* the sleep state can only be entered from the standby state.
876 * pm_runtime_get_noresume gets called before the suspend call.
877 * So the devices usage count is >0 but it is not necessarily
878 * active.
880 if (!pm_runtime_status_suspended(dev)) {
881 ret = auok190x_runtime_suspend(dev);
882 if (ret < 0) {
883 dev_err(dev, "auok190x_runtime_suspend failed with %d\n",
884 ret);
885 return ret;
887 par->manual_standby = 1;
890 gpio_direction_output(board->gpio_nsleep, 0);
893 msleep(100);
895 return 0;
898 static int __maybe_unused auok190x_resume(struct device *dev)
900 struct platform_device *pdev = to_platform_device(dev);
901 struct fb_info *info = platform_get_drvdata(pdev);
902 struct auok190xfb_par *par = info->par;
903 struct auok190x_board *board = par->board;
905 dev_dbg(dev, "resume\n");
906 if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
907 dev_dbg(dev, "resume with broken standby\n");
909 auok190x_power(par, 1);
911 par->init(par);
912 } else {
913 dev_dbg(dev, "resume from sleep\n");
915 /* device should be in runtime suspend when we were suspended
916 * and pm_runtime_put_sync gets called after this function.
917 * So there is no need to touch the standby mode here at all.
919 gpio_direction_output(board->gpio_nsleep, 1);
920 msleep(100);
922 /* an additional init call seems to be necessary after sleep */
923 auok190x_runtime_resume(dev);
924 par->init(par);
926 /* if we were runtime-suspended before, suspend again*/
927 if (!par->manual_standby)
928 auok190x_runtime_suspend(dev);
929 else
930 par->manual_standby = 0;
933 return 0;
936 const struct dev_pm_ops auok190x_pm = {
937 SET_RUNTIME_PM_OPS(auok190x_runtime_suspend, auok190x_runtime_resume,
938 NULL)
939 SET_SYSTEM_SLEEP_PM_OPS(auok190x_suspend, auok190x_resume)
941 EXPORT_SYMBOL_GPL(auok190x_pm);
944 * Common probe and remove code
947 int auok190x_common_probe(struct platform_device *pdev,
948 struct auok190x_init_data *init)
950 struct auok190x_board *board = init->board;
951 struct auok190xfb_par *par;
952 struct fb_info *info;
953 struct panel_info *panel;
954 int videomemorysize, ret;
955 unsigned char *videomemory;
957 /* check board contents */
958 if (!board->init || !board->cleanup || !board->wait_for_rdy
959 || !board->set_ctl || !board->set_hdb || !board->get_hdb
960 || !board->setup_irq)
961 return -EINVAL;
963 info = framebuffer_alloc(sizeof(struct auok190xfb_par), &pdev->dev);
964 if (!info)
965 return -ENOMEM;
967 par = info->par;
968 par->info = info;
969 par->board = board;
970 par->recover = auok190x_recover;
971 par->update_partial = init->update_partial;
972 par->update_all = init->update_all;
973 par->need_refresh = init->need_refresh;
974 par->init = init->init;
976 /* init update modes */
977 par->update_cnt = 0;
978 par->update_mode = -1;
979 par->last_mode = -1;
980 par->flash = 0;
982 par->regulator = regulator_get(info->device, "vdd");
983 if (IS_ERR(par->regulator)) {
984 ret = PTR_ERR(par->regulator);
985 dev_err(info->device, "Failed to get regulator: %d\n", ret);
986 goto err_reg;
989 ret = board->init(par);
990 if (ret) {
991 dev_err(info->device, "board init failed, %d\n", ret);
992 goto err_board;
995 ret = gpio_request(board->gpio_nsleep, "AUOK190x sleep");
996 if (ret) {
997 dev_err(info->device, "could not request sleep gpio, %d\n",
998 ret);
999 goto err_gpio1;
1002 ret = gpio_direction_output(board->gpio_nsleep, 0);
1003 if (ret) {
1004 dev_err(info->device, "could not set sleep gpio, %d\n", ret);
1005 goto err_gpio2;
1008 ret = gpio_request(board->gpio_nrst, "AUOK190x reset");
1009 if (ret) {
1010 dev_err(info->device, "could not request reset gpio, %d\n",
1011 ret);
1012 goto err_gpio2;
1015 ret = gpio_direction_output(board->gpio_nrst, 0);
1016 if (ret) {
1017 dev_err(info->device, "could not set reset gpio, %d\n", ret);
1018 goto err_gpio3;
1021 ret = auok190x_power(par, 1);
1022 if (ret) {
1023 dev_err(info->device, "could not power on the device, %d\n",
1024 ret);
1025 goto err_gpio3;
1028 mutex_init(&par->io_lock);
1030 init_waitqueue_head(&par->waitq);
1032 ret = par->board->setup_irq(par->info);
1033 if (ret) {
1034 dev_err(info->device, "could not setup ready-irq, %d\n", ret);
1035 goto err_irq;
1038 /* wait for init to complete */
1039 par->board->wait_for_rdy(par);
1042 * From here on the controller can talk to us
1045 /* initialise fix, var, resolution and rotation */
1047 strlcpy(info->fix.id, init->id, 16);
1048 info->var.bits_per_pixel = 8;
1049 info->var.grayscale = 1;
1051 panel = &panel_table[board->resolution];
1053 par->resolution = board->resolution;
1054 par->rotation = 0;
1056 /* videomemory handling */
1058 videomemorysize = roundup((panel->w * panel->h) * 2, PAGE_SIZE);
1059 videomemory = vzalloc(videomemorysize);
1060 if (!videomemory) {
1061 ret = -ENOMEM;
1062 goto err_irq;
1065 info->screen_base = (char *)videomemory;
1066 info->fix.smem_len = videomemorysize;
1068 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
1069 info->fbops = &auok190xfb_ops;
1071 ret = auok190xfb_check_var(&info->var, info);
1072 if (ret)
1073 goto err_defio;
1075 auok190xfb_set_fix(info);
1077 /* deferred io init */
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;
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);
1094 /* color map */
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;
1102 /* controller init */
1104 par->consecutive_threshold = 100;
1105 par->init(par);
1106 auok190x_identify(par);
1108 platform_set_drvdata(pdev, info);
1110 ret = register_framebuffer(info);
1111 if (ret < 0)
1112 goto err_regfb;
1114 ret = sysfs_create_group(&info->device->kobj, &auok190x_attr_group);
1115 if (ret)
1116 goto err_sysfs;
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);
1122 /* increase autosuspend_delay when we use alternative methods
1123 * for runtime_pm
1125 par->autosuspend_delay = (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN)
1126 ? 1000 : 200;
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);
1133 return 0;
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);
1156 return ret;
1158 EXPORT_SYMBOL_GPL(auok190x_common_probe);
1160 int auok190x_common_remove(struct platform_device *pdev)
1162 struct fb_info *info = platform_get_drvdata(pdev);
1163 struct auok190xfb_par *par = info->par;
1164 struct auok190x_board *board = par->board;
1166 pm_runtime_disable(info->device);
1168 sysfs_remove_group(&info->device->kobj, &auok190x_attr_group);
1170 unregister_framebuffer(info);
1172 fb_dealloc_cmap(&info->cmap);
1174 fb_deferred_io_cleanup(info);
1176 vfree((void *)info->screen_base);
1178 auok190x_power(par, 0);
1180 gpio_free(board->gpio_nrst);
1181 gpio_free(board->gpio_nsleep);
1183 board->cleanup(par);
1185 regulator_put(par->regulator);
1187 framebuffer_release(info);
1189 return 0;
1191 EXPORT_SYMBOL_GPL(auok190x_common_remove);
1193 MODULE_DESCRIPTION("Common code for AUO-K190X controllers");
1194 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
1195 MODULE_LICENSE("GPL");