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