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