Merge tag 'fbdev-v4.17' of git://github.com/bzolnier/linux
[sfrench/cifs-2.6.git] / drivers / video / fbdev / 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/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>
22
23 #include <video/auo_k190xfb.h>
24
25 #include "auo_k190x.h"
26
27 struct panel_info {
28         int w;
29         int h;
30 };
31
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,
38         },
39         /* standard 9" */
40         [AUOK190X_RESOLUTION_1024_768] = {
41                 .w = 1024,
42                 .h = 768,
43         },
44         [AUOK190X_RESOLUTION_600_800] = {
45                 .w = 600,
46                 .h = 800,
47         },
48         [AUOK190X_RESOLUTION_768_1024] = {
49                 .w = 768,
50                 .h = 1024,
51         },
52 };
53
54 /*
55  * private I80 interface to the board driver
56  */
57
58 static void auok190x_issue_data(struct auok190xfb_par *par, u16 data)
59 {
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);
63 }
64
65 static void auok190x_issue_cmd(struct auok190xfb_par *par, u16 data)
66 {
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);
70 }
71
72 /**
73  * Conversion of 16bit color to 4bit grayscale
74  * does roughly (0.3 * R + 0.6 G + 0.1 B) / 2
75  */
76 static inline int rgb565_to_gray4(u16 data, struct fb_var_screeninfo *var)
77 {
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);
81 }
82
83 static int auok190x_issue_pixels_rgb565(struct auok190xfb_par *par, int size,
84                                         u16 *data)
85 {
86         struct fb_var_screeninfo *var = &par->info->var;
87         struct device *dev = par->info->device;
88         int i;
89         u16 tmp;
90
91         if (size & 7) {
92                 dev_err(dev, "issue_pixels: size %d must be a multiple of 8\n",
93                         size);
94                 return -EINVAL;
95         }
96
97         for (i = 0; i < (size >> 2); i++) {
98                 par->board->set_ctl(par, AUOK190X_I80_WR, 0);
99
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;
104
105                 par->board->set_hdb(par, tmp);
106                 par->board->set_ctl(par, AUOK190X_I80_WR, 1);
107         }
108
109         return 0;
110 }
111
112 static int auok190x_issue_pixels_gray8(struct auok190xfb_par *par, int size,
113                                        u16 *data)
114 {
115         struct device *dev = par->info->device;
116         int i;
117         u16 tmp;
118
119         if (size & 3) {
120                 dev_err(dev, "issue_pixels: size %d must be a multiple of 4\n",
121                         size);
122                 return -EINVAL;
123         }
124
125         for (i = 0; i < (size >> 1); i++) {
126                 par->board->set_ctl(par, AUOK190X_I80_WR, 0);
127
128                 /* simple reduction of 8bit staticgray to 4bit gray
129                  * combines 4 * 4bit pixel values into a 16bit value
130                  */
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);
135
136                 par->board->set_hdb(par, tmp);
137                 par->board->set_ctl(par, AUOK190X_I80_WR, 1);
138         }
139
140         return 0;
141 }
142
143 static int auok190x_issue_pixels(struct auok190xfb_par *par, int size,
144                                  u16 *data)
145 {
146         struct fb_info *info = par->info;
147         struct device *dev = par->info->device;
148
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);
156
157         return 0;
158 }
159
160 static u16 auok190x_read_data(struct auok190xfb_par *par)
161 {
162         u16 data;
163
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);
167
168         return data;
169 }
170
171 /*
172  * Command interface for the controller drivers
173  */
174
175 void auok190x_send_command_nowait(struct auok190xfb_par *par, u16 data)
176 {
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);
180 }
181 EXPORT_SYMBOL_GPL(auok190x_send_command_nowait);
182
183 void auok190x_send_cmdargs_nowait(struct auok190xfb_par *par, u16 cmd,
184                                   int argc, u16 *argv)
185 {
186         int i;
187
188         par->board->set_ctl(par, AUOK190X_I80_CS, 0);
189         auok190x_issue_cmd(par, cmd);
190
191         for (i = 0; i < argc; i++)
192                 auok190x_issue_data(par, argv[i]);
193         par->board->set_ctl(par, AUOK190X_I80_CS, 1);
194 }
195 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_nowait);
196
197 int auok190x_send_command(struct auok190xfb_par *par, 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_command_nowait(par, data);
206         return 0;
207 }
208 EXPORT_SYMBOL_GPL(auok190x_send_command);
209
210 int auok190x_send_cmdargs(struct auok190xfb_par *par, u16 cmd,
211                            int argc, u16 *argv)
212 {
213         int ret;
214
215         ret = par->board->wait_for_rdy(par);
216         if (ret)
217                 return ret;
218
219         auok190x_send_cmdargs_nowait(par, cmd, argc, argv);
220         return 0;
221 }
222 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs);
223
224 int auok190x_read_cmdargs(struct auok190xfb_par *par, u16 cmd,
225                            int argc, u16 *argv)
226 {
227         int i, ret;
228
229         ret = par->board->wait_for_rdy(par);
230         if (ret)
231                 return ret;
232
233         par->board->set_ctl(par, AUOK190X_I80_CS, 0);
234         auok190x_issue_cmd(par, cmd);
235
236         for (i = 0; i < argc; i++)
237                 argv[i] = auok190x_read_data(par);
238         par->board->set_ctl(par, AUOK190X_I80_CS, 1);
239
240         return 0;
241 }
242 EXPORT_SYMBOL_GPL(auok190x_read_cmdargs);
243
244 void auok190x_send_cmdargs_pixels_nowait(struct auok190xfb_par *par, u16 cmd,
245                                   int argc, u16 *argv, int size, u16 *data)
246 {
247         int i;
248
249         par->board->set_ctl(par, AUOK190X_I80_CS, 0);
250
251         auok190x_issue_cmd(par, cmd);
252
253         for (i = 0; i < argc; i++)
254                 auok190x_issue_data(par, argv[i]);
255
256         auok190x_issue_pixels(par, size, data);
257
258         par->board->set_ctl(par, AUOK190X_I80_CS, 1);
259 }
260 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_pixels_nowait);
261
262 int auok190x_send_cmdargs_pixels(struct auok190xfb_par *par, u16 cmd,
263                                   int argc, u16 *argv, int size, u16 *data)
264 {
265         int ret;
266
267         ret = par->board->wait_for_rdy(par);
268         if (ret)
269                 return ret;
270
271         auok190x_send_cmdargs_pixels_nowait(par, cmd, argc, argv, size, data);
272
273         return 0;
274 }
275 EXPORT_SYMBOL_GPL(auok190x_send_cmdargs_pixels);
276
277 /*
278  * fbdefio callbacks - common on both controllers.
279  */
280
281 static void auok190xfb_dpy_first_io(struct fb_info *info)
282 {
283         /* tell runtime-pm that we wish to use the device in a short time */
284         pm_runtime_get(info->device);
285 }
286
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)
290 {
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;
300
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.
304                  */
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.
310                  */
311                 pm_runtime_get_sync(info->device);
312
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.
316          */
317         if (par->need_refresh(par)) {
318                 par->update_all(par);
319                 goto out;
320         }
321
322         /* height increment is fixed per page */
323         h_inc = DIV_ROUND_UP(PAGE_SIZE , line_length);
324
325         /* calculate number of pages from pixel height */
326         threshold = par->consecutive_threshold / h_inc;
327         if (threshold < 1)
328                 threshold = 1;
329
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);
342
343                         /* start over with our non consecutive page */
344                         y1 = (cur->index << PAGE_SHIFT) / line_length;
345                         h = h_inc;
346                 }
347                 prev_index = cur->index;
348         }
349
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));
356
357 out:
358         pm_runtime_mark_last_busy(info->device);
359         pm_runtime_put_autosuspend(info->device);
360 }
361
362 /*
363  * framebuffer operations
364  */
365
366 /*
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
369  */
370 static ssize_t auok190xfb_write(struct fb_info *info, const char __user *buf,
371                                 size_t count, loff_t *ppos)
372 {
373         struct auok190xfb_par *par = info->par;
374         unsigned long p = *ppos;
375         void *dst;
376         int err = 0;
377         unsigned long total_size;
378
379         if (info->state != FBINFO_STATE_RUNNING)
380                 return -EPERM;
381
382         total_size = info->fix.smem_len;
383
384         if (p > total_size)
385                 return -EFBIG;
386
387         if (count > total_size) {
388                 err = -EFBIG;
389                 count = total_size;
390         }
391
392         if (count + p > total_size) {
393                 if (!err)
394                         err = -ENOSPC;
395
396                 count = total_size - p;
397         }
398
399         dst = (void *)(info->screen_base + p);
400
401         if (copy_from_user(dst, buf, count))
402                 err = -EFAULT;
403
404         if  (!err)
405                 *ppos += count;
406
407         par->update_all(par);
408
409         return (err) ? err : count;
410 }
411
412 static void auok190xfb_fillrect(struct fb_info *info,
413                                    const struct fb_fillrect *rect)
414 {
415         struct auok190xfb_par *par = info->par;
416
417         sys_fillrect(info, rect);
418
419         par->update_all(par);
420 }
421
422 static void auok190xfb_copyarea(struct fb_info *info,
423                                    const struct fb_copyarea *area)
424 {
425         struct auok190xfb_par *par = info->par;
426
427         sys_copyarea(info, area);
428
429         par->update_all(par);
430 }
431
432 static void auok190xfb_imageblit(struct fb_info *info,
433                                 const struct fb_image *image)
434 {
435         struct auok190xfb_par *par = info->par;
436
437         sys_imageblit(info, image);
438
439         par->update_all(par);
440 }
441
442 static int auok190xfb_check_var(struct fb_var_screeninfo *var,
443                                    struct fb_info *info)
444 {
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;
449
450         /*
451          * Color depth
452          */
453
454         if (var->bits_per_pixel == 8 && var->grayscale == 1) {
455                 /*
456                  * For 8-bit grayscale, R, G, and B offset are equal.
457                  */
458                 var->red.length = 8;
459                 var->red.offset = 0;
460                 var->red.msb_right = 0;
461
462                 var->green.length = 8;
463                 var->green.offset = 0;
464                 var->green.msb_right = 0;
465
466                 var->blue.length = 8;
467                 var->blue.offset = 0;
468                 var->blue.msb_right = 0;
469
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;
477
478                 var->green.length = 6;
479                 var->green.offset = 5;
480                 var->green.msb_right = 0;
481
482                 var->blue.length = 5;
483                 var->blue.offset = 0;
484                 var->blue.msb_right = 0;
485
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;
493         }
494
495         /*
496          * Dimensions
497          */
498
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;
513         }
514
515         var->xres_virtual = var->xres;
516         var->yres_virtual = var->yres;
517
518         /*
519          *  Memory limit
520          */
521
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;
527         }
528
529         return 0;
530 }
531
532 static int auok190xfb_set_fix(struct fb_info *info)
533 {
534         struct fb_fix_screeninfo *fix = &info->fix;
535         struct fb_var_screeninfo *var = &info->var;
536
537         fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
538
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;
546
547         return 0;
548 }
549
550 static int auok190xfb_set_par(struct fb_info *info)
551 {
552         struct auok190xfb_par *par = info->par;
553
554         par->rotation = info->var.rotate;
555         auok190xfb_set_fix(info);
556
557         /* reinit the controller to honor the rotation */
558         par->init(par);
559
560         /* wait for init to complete */
561         par->board->wait_for_rdy(par);
562
563         return 0;
564 }
565
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,
575 };
576
577 /*
578  * Controller-functions common to both K1900 and K1901
579  */
580
581 static int auok190x_read_temperature(struct auok190xfb_par *par)
582 {
583         struct device *dev = par->info->device;
584         u16 data[4];
585         int temp;
586
587         pm_runtime_get_sync(dev);
588
589         mutex_lock(&(par->io_lock));
590
591         auok190x_read_cmdargs(par, AUOK190X_CMD_READ_VERSION, 4, data);
592
593         mutex_unlock(&(par->io_lock));
594
595         pm_runtime_mark_last_busy(dev);
596         pm_runtime_put_autosuspend(dev);
597
598         /* sanitize and split of half-degrees for now */
599         temp = ((data[0] & AUOK190X_VERSION_TEMP_MASK) >> 1);
600
601         /* handle positive and negative temperatures */
602         if (temp >= 201)
603                 return (255 - temp + 1) * (-1);
604         else
605                 return temp;
606 }
607
608 static void auok190x_identify(struct auok190xfb_par *par)
609 {
610         struct device *dev = par->info->device;
611         u16 data[4];
612
613         pm_runtime_get_sync(dev);
614
615         mutex_lock(&(par->io_lock));
616
617         auok190x_read_cmdargs(par, AUOK190X_CMD_READ_VERSION, 4, data);
618
619         mutex_unlock(&(par->io_lock));
620
621         par->epd_type = data[1] & AUOK190X_VERSION_TEMP_MASK;
622
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]);
626
627         par->tcon_version = AUOK190X_VERSION_TCON(data[3]);
628         par->lut_version = AUOK190X_VERSION_LUT(data[3]);
629
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);
633
634         pm_runtime_mark_last_busy(dev);
635         pm_runtime_put_autosuspend(dev);
636 }
637
638 /*
639  * Sysfs functions
640  */
641
642 static ssize_t update_mode_show(struct device *dev,
643                                 struct device_attribute *attr, char *buf)
644 {
645         struct fb_info *info = dev_get_drvdata(dev);
646         struct auok190xfb_par *par = info->par;
647
648         return sprintf(buf, "%d\n", par->update_mode);
649 }
650
651 static ssize_t update_mode_store(struct device *dev,
652                                  struct device_attribute *attr,
653                                  const char *buf, size_t count)
654 {
655         struct fb_info *info = dev_get_drvdata(dev);
656         struct auok190xfb_par *par = info->par;
657         int mode, ret;
658
659         ret = kstrtoint(buf, 10, &mode);
660         if (ret)
661                 return ret;
662
663         par->update_mode = mode;
664
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);
668
669         return count;
670 }
671
672 static ssize_t flash_show(struct device *dev, struct device_attribute *attr,
673                           char *buf)
674 {
675         struct fb_info *info = dev_get_drvdata(dev);
676         struct auok190xfb_par *par = info->par;
677
678         return sprintf(buf, "%d\n", par->flash);
679 }
680
681 static ssize_t flash_store(struct device *dev, struct device_attribute *attr,
682                            const char *buf, size_t count)
683 {
684         struct fb_info *info = dev_get_drvdata(dev);
685         struct auok190xfb_par *par = info->par;
686         int flash, ret;
687
688         ret = kstrtoint(buf, 10, &flash);
689         if (ret)
690                 return ret;
691
692         if (flash > 0)
693                 par->flash = 1;
694         else
695                 par->flash = 0;
696
697         return count;
698 }
699
700 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
701                          char *buf)
702 {
703         struct fb_info *info = dev_get_drvdata(dev);
704         struct auok190xfb_par *par = info->par;
705         int temp;
706
707         temp = auok190x_read_temperature(par);
708         return sprintf(buf, "%d\n", temp);
709 }
710
711 static DEVICE_ATTR_RW(update_mode);
712 static DEVICE_ATTR_RW(flash);
713 static DEVICE_ATTR(temp, 0644, temp_show, NULL);
714
715 static struct attribute *auok190x_attributes[] = {
716         &dev_attr_update_mode.attr,
717         &dev_attr_flash.attr,
718         &dev_attr_temp.attr,
719         NULL
720 };
721
722 static const struct attribute_group auok190x_attr_group = {
723         .attrs          = auok190x_attributes,
724 };
725
726 static int auok190x_power(struct auok190xfb_par *par, bool on)
727 {
728         struct auok190x_board *board = par->board;
729         int ret;
730
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)
734                  */
735                 ret = regulator_enable(par->regulator);
736                 if (ret)
737                         return ret;
738
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);
747         }
748
749         return 0;
750 }
751
752 /*
753  * Recovery - powercycle the controller
754  */
755
756 static void auok190x_recover(struct auok190xfb_par *par)
757 {
758         struct device *dev = par->info->device;
759
760         auok190x_power(par, 0);
761         msleep(100);
762         auok190x_power(par, 1);
763
764         /* after powercycling the device, it's always active */
765         pm_runtime_set_active(dev);
766         par->standby = 0;
767
768         par->init(par);
769
770         /* wait for init to complete */
771         par->board->wait_for_rdy(par);
772 }
773
774 /*
775  * Power-management
776  */
777 static int __maybe_unused auok190x_runtime_suspend(struct device *dev)
778 {
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;
784
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
787          */
788         mutex_lock(&(par->io_lock));
789
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;
794         }
795
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
799          */
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.
806                  */
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);
814         }
815
816         msleep(64);
817
818 finish:
819         par->standby = 1;
820
821         return 0;
822 }
823
824 static int __maybe_unused auok190x_runtime_resume(struct device *dev)
825 {
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;
830
831         if (!par->standby) {
832                 dev_warn(dev, "not in standby, runtime-pm pairing mismatch\n");
833                 return 0;
834         }
835
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
841                  */
842                 dev_dbg(dev, "runtime resume from standby\n");
843                 auok190x_send_command_nowait(par, AUOK190X_CMD_WAKEUP);
844
845                 msleep(160);
846
847                 /* wait for the controller to be ready and release the lock */
848                 board->wait_for_rdy(par);
849         }
850
851         par->standby = 0;
852
853         mutex_unlock(&(par->io_lock));
854
855         return 0;
856 }
857
858 static int __maybe_unused auok190x_suspend(struct device *dev)
859 {
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;
865
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");
870
871                 auok190x_power(par, 0);
872         } else {
873                 dev_dbg(dev, "suspend using sleep\n");
874
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.
879                  */
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;
886                         }
887                         par->manual_standby = 1;
888                 }
889
890                 gpio_direction_output(board->gpio_nsleep, 0);
891         }
892
893         msleep(100);
894
895         return 0;
896 }
897
898 static int __maybe_unused auok190x_resume(struct device *dev)
899 {
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;
904
905         dev_dbg(dev, "resume\n");
906         if (board->quirks & AUOK190X_QUIRK_STANDBYBROKEN) {
907                 dev_dbg(dev, "resume with broken standby\n");
908
909                 auok190x_power(par, 1);
910
911                 par->init(par);
912         } else {
913                 dev_dbg(dev, "resume from sleep\n");
914
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.
918                  */
919                 gpio_direction_output(board->gpio_nsleep, 1);
920                 msleep(100);
921
922                 /* an additional init call seems to be necessary after sleep */
923                 auok190x_runtime_resume(dev);
924                 par->init(par);
925
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;
931         }
932
933         return 0;
934 }
935
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)
940 };
941 EXPORT_SYMBOL_GPL(auok190x_pm);
942
943 /*
944  * Common probe and remove code
945  */
946
947 int auok190x_common_probe(struct platform_device *pdev,
948                           struct auok190x_init_data *init)
949 {
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;
956
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;
962
963         info = framebuffer_alloc(sizeof(struct auok190xfb_par), &pdev->dev);
964         if (!info)
965                 return -ENOMEM;
966
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;
975
976         /* init update modes */
977         par->update_cnt = 0;
978         par->update_mode = -1;
979         par->last_mode = -1;
980         par->flash = 0;
981
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;
987         }
988
989         ret = board->init(par);
990         if (ret) {
991                 dev_err(info->device, "board init failed, %d\n", ret);
992                 goto err_board;
993         }
994
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;
1000         }
1001
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;
1006         }
1007
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;
1013         }
1014
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;
1019         }
1020
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;
1026         }
1027
1028         mutex_init(&par->io_lock);
1029
1030         init_waitqueue_head(&par->waitq);
1031
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;
1036         }
1037
1038         /* wait for init to complete */
1039         par->board->wait_for_rdy(par);
1040
1041         /*
1042          * From here on the controller can talk to us
1043          */
1044
1045         /* initialise fix, var, resolution and rotation */
1046
1047         strlcpy(info->fix.id, init->id, 16);
1048         info->var.bits_per_pixel = 8;
1049         info->var.grayscale = 1;
1050
1051         panel = &panel_table[board->resolution];
1052
1053         par->resolution = board->resolution;
1054         par->rotation = 0;
1055
1056         /* videomemory handling */
1057
1058         videomemorysize = roundup((panel->w * panel->h) * 2, PAGE_SIZE);
1059         videomemory = vzalloc(videomemorysize);
1060         if (!videomemory) {
1061                 ret = -ENOMEM;
1062                 goto err_irq;
1063         }
1064
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
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");