display-sys: list supported 3d format of all resolution.
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / display-sys.c
1 #include <linux/module.h>
2 #include <linux/ctype.h>
3 #include <linux/idr.h>
4 #include <linux/err.h>
5 #include <linux/kdev_t.h>
6 #include <linux/display-sys.h>
7
8 static struct list_head main_display_device_list;
9 static struct list_head aux_display_device_list;
10
11 static ssize_t display_show_name(struct device *dev,
12                                  struct device_attribute *attr, char *buf)
13 {
14         struct rk_display_device *dsp = dev_get_drvdata(dev);
15
16         return snprintf(buf, PAGE_SIZE, "%s\n", dsp->name);
17 }
18
19 static ssize_t display_show_type(struct device *dev,
20                                  struct device_attribute *attr, char *buf)
21 {
22         struct rk_display_device *dsp = dev_get_drvdata(dev);
23
24         return snprintf(buf, PAGE_SIZE, "%s\n", dsp->type);
25 }
26
27 static ssize_t display_show_property(struct device *dev,
28                                      struct device_attribute *attr, char *buf)
29 {
30         struct rk_display_device *dsp = dev_get_drvdata(dev);
31
32         return snprintf(buf, PAGE_SIZE, "%d\n", dsp->property);
33 }
34
35 static ssize_t display_show_enable(struct device *dev,
36                                    struct device_attribute *attr, char *buf)
37 {
38         struct rk_display_device *dsp = dev_get_drvdata(dev);
39         int enable;
40
41         if (dsp->ops && dsp->ops->getenable)
42                 enable = dsp->ops->getenable(dsp);
43         else
44                 return 0;
45         return snprintf(buf, PAGE_SIZE, "%d\n", enable);
46 }
47
48 static ssize_t display_store_enable(struct device *dev,
49                                     struct device_attribute *attr,
50                                     const char *buf, size_t size)
51 {
52         struct rk_display_device *dsp = dev_get_drvdata(dev);
53         int enable;
54
55         if (kstrtoint(buf, 0, &enable))
56                 return size;
57         if (dsp->ops && dsp->ops->setenable)
58                 dsp->ops->setenable(dsp, enable);
59         return size;
60 }
61
62 static ssize_t display_show_connect(struct device *dev,
63                                     struct device_attribute *attr, char *buf)
64 {
65         struct rk_display_device *dsp = dev_get_drvdata(dev);
66         int connect;
67
68         if (dsp->ops && dsp->ops->getstatus)
69                 connect = dsp->ops->getstatus(dsp);
70         else
71                 return 0;
72         return snprintf(buf, PAGE_SIZE, "%d\n", connect);
73 }
74
75 static int mode_string(char *buf, unsigned int offset,
76                        const struct fb_videomode *mode)
77 {
78         char v = 'p';
79
80         if (mode->xres == 0 && mode->yres == 0)
81                 return snprintf(&buf[offset], PAGE_SIZE - offset, "auto\n");
82 /*
83         if (mode->flag & FB_MODE_IS_DETAILED)
84                 m = 'D';
85         if (mode->flag & FB_MODE_IS_VESA)
86                 m = 'V';
87         if (mode->flag & FB_MODE_IS_STANDARD)
88                 m = 'S';
89 */
90         if (mode->vmode & FB_VMODE_INTERLACED)
91                 v = 'i';
92         if (mode->vmode & FB_VMODE_DOUBLE)
93                 v = 'd';
94         if (mode->flag)
95                 return snprintf(&buf[offset], PAGE_SIZE - offset,
96                                 "%dx%d%c-%d(YCbCr420)\n",
97                                 mode->xres, mode->yres, v, mode->refresh);
98         else
99                 return snprintf(&buf[offset], PAGE_SIZE - offset,
100                                 "%dx%d%c-%d\n",
101                                 mode->xres, mode->yres, v, mode->refresh);
102 }
103
104 static ssize_t display_show_modes(struct device *dev,
105                                   struct device_attribute *attr, char *buf)
106 {
107         struct rk_display_device *dsp = dev_get_drvdata(dev);
108         struct list_head *modelist, *pos;
109         struct display_modelist *display_modelist;
110         const struct fb_videomode *mode;
111         int i;
112
113         if (dsp->ops && dsp->ops->getmodelist) {
114                 if (dsp->ops->getmodelist(dsp, &modelist))
115                         return -EINVAL;
116         } else {
117                 return 0;
118         }
119         i = 0;
120         if (dsp->priority == DISPLAY_PRIORITY_HDMI)
121                 i += snprintf(buf, PAGE_SIZE, "auto\n");
122
123         list_for_each(pos, modelist) {
124                 display_modelist = list_entry(pos,
125                                               struct display_modelist,
126                                               list);
127                 mode = &display_modelist->mode;
128                 i += mode_string(buf, i, mode);
129         }
130         return i;
131 }
132
133 static ssize_t display_show_mode(struct device *dev,
134                                  struct device_attribute *attr, char *buf)
135 {
136         struct rk_display_device *dsp = dev_get_drvdata(dev);
137         struct fb_videomode mode;
138
139         if (dsp->ops && dsp->ops->getmode)
140                 if (dsp->ops->getmode(dsp, &mode) == 0)
141                         return mode_string(buf, 0, &mode);
142         return 0;
143 }
144
145 static ssize_t display_store_mode(struct device *dev,
146                                   struct device_attribute *attr,
147                                   const char *buf, size_t count)
148 {
149         struct rk_display_device *dsp = dev_get_drvdata(dev);
150         char mstr[100];
151         struct list_head *modelist, *pos;
152         struct display_modelist *display_modelist;
153         struct fb_videomode *mode;
154         size_t i;
155
156         if (!memcmp(buf, "auto", 4)) {
157                 if (dsp->ops && dsp->ops->setmode)
158                         dsp->ops->setmode(dsp, NULL);
159                 return count;
160         }
161
162         if (dsp->ops && dsp->ops->getmodelist) {
163                 if (dsp->ops && dsp->ops->getmodelist) {
164                         if (dsp->ops->getmodelist(dsp, &modelist))
165                                 return -EINVAL;
166                 }
167                 list_for_each(pos, modelist) {
168                         display_modelist = list_entry(pos,
169                                                       struct display_modelist,
170                                                       list);
171                         mode = &display_modelist->mode;
172                         i = mode_string(mstr, 0, mode);
173                         if (strncmp(mstr, buf, max(count, i)) == 0) {
174                                 if (dsp->ops && dsp->ops->setmode)
175                                         dsp->ops->setmode(dsp, mode);
176                                 return count;
177                         }
178                 }
179         }
180         return -EINVAL;
181 }
182
183 static ssize_t display_show_scale(struct device *dev,
184                                   struct device_attribute *attr,
185                                   char *buf)
186 {
187         struct rk_display_device *dsp = dev_get_drvdata(dev);
188         int xscale, yscale;
189
190         if (dsp->ops && dsp->ops->getscale) {
191                 xscale = dsp->ops->getscale(dsp, DISPLAY_SCALE_X);
192                 yscale = dsp->ops->getscale(dsp, DISPLAY_SCALE_Y);
193                 if (xscale && yscale)
194                         return snprintf(buf, PAGE_SIZE,
195                                         "xscale=%d yscale=%d\n",
196                                         xscale, yscale);
197         }
198         return -EINVAL;
199 }
200
201 static ssize_t display_store_scale(struct device *dev,
202                                    struct device_attribute *attr,
203                                    const char *buf, size_t count)
204 {
205         struct rk_display_device *dsp = dev_get_drvdata(dev);
206         int scale = 100;
207
208         if (dsp->ops && dsp->ops->setscale) {
209                 if (!strncmp(buf, "xscale", 6)) {
210                         if (!kstrtoint(buf, 0, &scale))
211                                 dsp->ops->setscale(dsp,
212                                                    DISPLAY_SCALE_X,
213                                                    scale);
214                 } else if (!strncmp(buf, "yscale", 6)) {
215                         if (!kstrtoint(buf, 0, &scale))
216                                 dsp->ops->setscale(dsp,
217                                                    DISPLAY_SCALE_Y,
218                                                    scale);
219                 } else {
220                         if (!kstrtoint(buf, 0, &scale)) {
221                                 dsp->ops->setscale(dsp,
222                                                    DISPLAY_SCALE_X,
223                                                    scale);
224                                 dsp->ops->setscale(dsp,
225                                                    DISPLAY_SCALE_Y,
226                                                    scale);
227                         }
228                 }
229                 return count;
230         }
231         return -EINVAL;
232 }
233
234 static ssize_t display_show_3dmode(struct device *dev,
235                                    struct device_attribute *attr, char *buf)
236 {
237         struct rk_display_device *dsp = dev_get_drvdata(dev);
238         struct list_head *modelist, *pos;
239         struct display_modelist *display_modelist;
240         struct fb_videomode mode;
241         int i = 0, cur_3d_mode = -1;
242         char mode_str[128];
243         int mode_strlen, format_3d;
244
245         if (dsp->ops && dsp->ops->getmodelist) {
246                 if (dsp->ops->getmodelist(dsp, &modelist))
247                         return -EINVAL;
248         } else {
249                 return 0;
250         }
251
252         if (dsp->ops && dsp->ops->getmode) {
253                 if (dsp->ops->getmode(dsp, &mode))
254                         return -EINVAL;
255         } else {
256                 return 0;
257         }
258
259         list_for_each(pos, modelist) {
260                 display_modelist = list_entry(pos,
261                                               struct display_modelist,
262                                               list);
263                 if (fb_mode_is_equal(&mode, &display_modelist->mode))
264                         break;
265                 else
266                         display_modelist = NULL;
267         }
268         if (display_modelist)
269                 i = snprintf(buf, PAGE_SIZE, "3dmodes=%d\n",
270                              display_modelist->format_3d);
271         else
272                 i = snprintf(buf, PAGE_SIZE, "3dmodes=0\n");
273
274         if (dsp->ops && dsp->ops->get3dmode)
275                 cur_3d_mode = dsp->ops->get3dmode(dsp);
276         i += snprintf(buf + i, PAGE_SIZE - i, "cur3dmode=%d\n", cur_3d_mode);
277
278         list_for_each(pos, modelist) {
279                 display_modelist = list_entry(pos,
280                                               struct display_modelist,
281                                               list);
282                 mode_strlen = mode_string(mode_str, 0,
283                                           &(display_modelist->mode));
284                 mode_str[mode_strlen-1] = 0;
285                 format_3d = display_modelist->format_3d;
286                 i += snprintf(buf+i, PAGE_SIZE, "%s,%d\n",
287                               mode_str, format_3d);
288         }
289         return i;
290 }
291
292 static ssize_t display_store_3dmode(struct device *dev,
293                                     struct device_attribute *attr,
294                                     const char *buf, size_t count)
295 {
296         struct rk_display_device *dsp = dev_get_drvdata(dev);
297         int mode;
298
299         if (dsp->ops && dsp->ops->set3dmode) {
300                 if (!kstrtoint(buf, 0, &mode))
301                         dsp->ops->set3dmode(dsp, mode);
302                 return count;
303         }
304         return -EINVAL;
305 }
306
307 static ssize_t display_show_color(struct device *dev,
308                                   struct device_attribute *attr, char *buf)
309 {
310         struct rk_display_device *dsp = dev_get_drvdata(dev);
311         
312         if(dsp->ops && dsp->ops->getcolor)
313                 return dsp->ops->getcolor(dsp, buf);
314         else
315                 return 0;
316 }
317
318 static ssize_t display_store_color(struct device *dev, 
319                                    struct device_attribute *attr,
320                                    const char *buf, size_t count)
321 {
322         struct rk_display_device *dsp = dev_get_drvdata(dev);
323
324         if(dsp->ops && dsp->ops->setcolor) {
325                 if (!dsp->ops->setcolor(dsp, buf, count));
326                         return count;
327         }
328         return -EINVAL;
329 }
330
331 static ssize_t display_show_sinkaudioinfo(struct device *dev,
332                                           struct device_attribute *attr,
333                                           char *buf)
334 {
335         struct rk_display_device *dsp = dev_get_drvdata(dev);
336         char audioinfo[200];
337         int ret = 0;
338
339         if (dsp->ops && dsp->ops->getedidaudioinfo) {
340                 ret = dsp->ops->getedidaudioinfo(dsp, audioinfo, 200);
341                 if (!ret)
342                         return snprintf(buf, PAGE_SIZE, "%s\n", audioinfo);
343         }
344         return -EINVAL;
345 }
346
347 static ssize_t display_show_monspecs(struct device *dev,
348                                      struct device_attribute *attr, char *buf)
349 {
350         struct rk_display_device *dsp = dev_get_drvdata(dev);
351         struct fb_monspecs monspecs;
352         int ret = 0;
353
354         if (dsp->ops && dsp->ops->getmonspecs) {
355                 ret = dsp->ops->getmonspecs(dsp, &monspecs);
356                 if (!ret) {
357                         memcpy(buf, &monspecs, sizeof(struct fb_monspecs));
358                         return sizeof(struct fb_monspecs);
359                 }
360         }
361         return -EINVAL;
362 }
363
364 static ssize_t display_show_debug(struct device *dev,
365                                   struct device_attribute *attr, char *buf)
366 {
367         return -EINVAL;
368 }
369
370 static ssize_t display_store_debug(struct device *dev,
371                                    struct device_attribute *attr,
372                                    const char *buf, size_t count)
373 {
374         int cmd;
375         struct rk_display_device *dsp = dev_get_drvdata(dev);
376
377         if(dsp->ops && dsp->ops->setdebug) {
378                 if (sscanf(buf, "%d", &cmd) != -1)
379                         dsp->ops->setdebug(dsp, cmd);
380                 return count;
381         }
382         return -EINVAL;
383 }
384
385 static struct device_attribute display_attrs[] = {
386         __ATTR(name, S_IRUGO, display_show_name, NULL),
387         __ATTR(type, S_IRUGO, display_show_type, NULL),
388         __ATTR(property, S_IRUGO, display_show_property, NULL),
389         __ATTR(enable, 0666, display_show_enable, display_store_enable),
390         __ATTR(connect, S_IRUGO, display_show_connect, NULL),
391         __ATTR(modes, S_IRUGO, display_show_modes, NULL),
392         __ATTR(mode, 0666, display_show_mode, display_store_mode),
393         __ATTR(scale, 0666, display_show_scale, display_store_scale),
394         __ATTR(3dmode, 0666, display_show_3dmode, display_store_3dmode),
395         __ATTR(color, 0666, display_show_color, display_store_color),
396         __ATTR(audioinfo, S_IRUGO, display_show_sinkaudioinfo, NULL),
397         __ATTR(monspecs, S_IRUGO, display_show_monspecs, NULL),
398         __ATTR(debug, 0664, display_show_debug, display_store_debug),
399         __ATTR_NULL
400 };
401
402 static int display_suspend(struct device *dev, pm_message_t state)
403 {
404         struct rk_display_device *dsp = dev_get_drvdata(dev);
405
406         mutex_lock(&dsp->lock);
407         if (likely(dsp->driver->suspend))
408                 dsp->driver->suspend(dsp, state);
409         mutex_unlock(&dsp->lock);
410         return 0;
411 };
412
413 static int display_resume(struct device *dev)
414 {
415         struct rk_display_device *dsp = dev_get_drvdata(dev);
416
417         mutex_lock(&dsp->lock);
418         if (likely(dsp->driver->resume))
419                 dsp->driver->resume(dsp);
420         mutex_unlock(&dsp->lock);
421         return 0;
422 };
423
424 int display_add_videomode(const struct fb_videomode *mode,
425                           struct list_head *head)
426 {
427         struct list_head *pos;
428         struct display_modelist *modelist;
429         struct fb_videomode *m;
430         int found = 0;
431
432         list_for_each(pos, head) {
433                 modelist = list_entry(pos, struct display_modelist, list);
434                 m = &modelist->mode;
435                 if (fb_mode_is_equal(m, mode)) {
436                         found = 1;
437                         break;
438                 }
439         }
440         if (!found) {
441                 modelist = kmalloc(sizeof(*modelist),
442                                    GFP_KERNEL);
443
444                 if (!modelist)
445                         return -ENOMEM;
446                 modelist->mode = *mode;
447                 list_add(&modelist->list, head);
448         }
449         return 0;
450 }
451
452 void rk_display_device_enable(struct rk_display_device *ddev)
453 {
454         struct list_head *pos, *head;
455         struct rk_display_device *dev = NULL, *dev_enabled = NULL;
456         struct rk_display_device *dev_enable = NULL;
457         int enable = 0, connect;
458
459         if (ddev->property == DISPLAY_MAIN)
460                 head = &main_display_device_list;
461         else
462                 head = &aux_display_device_list;
463
464         list_for_each(pos, head) {
465                 dev = list_entry(pos, struct rk_display_device, list);
466                 enable = dev->ops->getenable(dev);
467                 connect = dev->ops->getstatus(dev);
468                 if (connect)
469                         dev_enable = dev;
470                 if (enable == 1)
471                         dev_enabled = dev;
472         }
473         /* If no device is connected, enable highest priority device. */
474         if (dev_enable == NULL) {
475                 dev->ops->setenable(dev, 1);
476                 return;
477         }
478
479         if (dev_enable == dev_enabled) {
480                 if (dev_enable != ddev)
481                         ddev->ops->setenable(ddev, 0);
482         } else {
483                 if (dev_enabled &&
484                     dev_enabled->priority != DISPLAY_PRIORITY_HDMI)
485                         dev_enabled->ops->setenable(dev_enabled, 0);
486                 dev_enable->ops->setenable(dev_enable, 1);
487         }
488 }
489 EXPORT_SYMBOL(rk_display_device_enable);
490
491 void rk_display_device_enable_other(struct rk_display_device *ddev)
492 {
493 #ifndef CONFIG_DISPLAY_AUTO_SWITCH
494         return;
495 #else
496         struct list_head *pos, *head;
497         struct rk_display_device *dev;
498         int connect = 0;
499
500         if (ddev->property == DISPLAY_MAIN)
501                 head = &main_display_device_list;
502         else
503                 head = &aux_display_device_list;
504
505         list_for_each_prev(pos, head) {
506                 dev = list_entry(pos, struct rk_display_device, list);
507                 if (dev != ddev) {
508                         connect = dev->ops->getstatus(dev);
509                         if (connect) {
510                                 dev->ops->setenable(dev, 1);
511                                 return;
512                         }
513                 }
514         }
515 #endif
516 }
517 EXPORT_SYMBOL(rk_display_device_enable_other);
518
519 void rk_display_device_disable_other(struct rk_display_device *ddev)
520 {
521 #ifndef CONFIG_DISPLAY_AUTO_SWITCH
522         return;
523 #else
524         struct list_head *pos, *head;
525         struct rk_display_device *dev;
526         int enable = 0;
527
528         if (ddev->property == DISPLAY_MAIN)
529                 head = &main_display_device_list;
530         else
531                 head = &aux_display_device_list;
532
533         list_for_each(pos, head) {
534                 dev = list_entry(pos, struct rk_display_device, list);
535                 if (dev != ddev) {
536                         enable = dev->ops->getenable(dev);
537                         if (enable)
538                                 dev->ops->setenable(dev, 0);
539                 }
540         }
541         ddev->ops->setenable(ddev, 1);
542 #endif
543 }
544 EXPORT_SYMBOL(rk_display_device_disable_other);
545
546 void rk_display_device_select(int property, int priority)
547 {
548         struct list_head *pos, *head;
549         struct rk_display_device *dev;
550         int enable, found = 0;
551
552         if (property == DISPLAY_MAIN)
553                 head = &main_display_device_list;
554         else
555                 head = &aux_display_device_list;
556
557         list_for_each(pos, head) {
558                 dev = list_entry(pos, struct rk_display_device, list);
559                 if (dev->priority == priority)
560                         found = 1;
561         }
562
563         if (!found) {
564                 pr_err("[%s] select display interface %d not exist\n",
565                        __func__, priority);
566                 return;
567         }
568
569         list_for_each(pos, head) {
570                 dev = list_entry(pos, struct rk_display_device, list);
571                 enable = dev->ops->getenable(dev);
572                 if (dev->priority == priority) {
573                         if (!enable)
574                                 dev->ops->setenable(dev, 1);
575                 } else if (enable) {
576                         dev->ops->setenable(dev, 0);
577                 }
578         }
579 }
580 EXPORT_SYMBOL(rk_display_device_select);
581 static struct mutex allocated_dsp_lock;
582 static DEFINE_IDR(allocated_dsp);
583 static struct class *display_class;
584
585 struct rk_display_device
586         *rk_display_device_register(struct rk_display_driver *driver,
587                                     struct device *parent, void *devdata)
588 {
589         struct rk_display_device *new_dev = NULL;
590         int ret = -EINVAL;
591
592         if (unlikely(!driver))
593                 return ERR_PTR(ret);
594
595         new_dev = kzalloc(sizeof(*new_dev), GFP_KERNEL);
596         if (likely(new_dev) && unlikely(driver->probe(new_dev, devdata))) {
597                 /* Reserve the index for this display */
598                 mutex_lock(&allocated_dsp_lock);
599                 new_dev->idx = idr_alloc(&allocated_dsp, new_dev,
600                                          0, 0, GFP_KERNEL);
601                 mutex_unlock(&allocated_dsp_lock);
602
603                 if (new_dev->idx >= 0) {
604                         if (new_dev->property == DISPLAY_MAIN)
605                                 new_dev->dev =
606                                 device_create(display_class, parent,
607                                               MKDEV(0, 0), new_dev,
608                                               "%s", new_dev->type);
609                         else
610                                 new_dev->dev =
611                                 device_create(display_class, parent,
612                                               MKDEV(0, 0), new_dev,
613                                               "display%d.%s",
614                                               new_dev->property,
615                                               new_dev->type);
616                         if (!IS_ERR(new_dev->dev)) {
617                                 new_dev->parent = parent;
618                                 new_dev->driver = driver;
619                                 if (parent)
620                                         new_dev->dev->driver = parent->driver;
621                                 mutex_init(&new_dev->lock);
622                                 /* Add new device to display device list. */
623                                 {
624                                 struct list_head *pos, *head;
625                                 struct rk_display_device *dev;
626
627                                 if (new_dev->property == DISPLAY_MAIN)
628                                         head = &main_display_device_list;
629                                 else
630                                         head = &aux_display_device_list;
631
632                                 list_for_each(pos, head) {
633                                         dev =
634                                         list_entry(pos,
635                                                    struct rk_display_device,
636                                                    list);
637                                         if (dev->priority > new_dev->priority)
638                                                 break;
639                                 }
640                                 list_add_tail(&new_dev->list, pos);
641                                 return new_dev;
642                                 }
643                         }
644                         mutex_lock(&allocated_dsp_lock);
645                         idr_remove(&allocated_dsp, new_dev->idx);
646                         mutex_unlock(&allocated_dsp_lock);
647                         ret = -EINVAL;
648                 }
649         }
650         kfree(new_dev);
651         return ERR_PTR(ret);
652 }
653 EXPORT_SYMBOL(rk_display_device_register);
654
655 void rk_display_device_unregister(struct rk_display_device *ddev)
656 {
657         if (!ddev)
658                 return;
659         /* Free device */
660         mutex_lock(&ddev->lock);
661         device_unregister(ddev->dev);
662         mutex_unlock(&ddev->lock);
663         /* Mark device index as avaliable */
664         mutex_lock(&allocated_dsp_lock);
665         idr_remove(&allocated_dsp, ddev->idx);
666         mutex_unlock(&allocated_dsp_lock);
667         list_del(&ddev->list);
668         kfree(ddev);
669 }
670 EXPORT_SYMBOL(rk_display_device_unregister);
671
672 static int __init rk_display_class_init(void)
673 {
674         display_class = class_create(THIS_MODULE, "display");
675         if (IS_ERR(display_class)) {
676                 pr_err("Failed to create display class\n");
677                 display_class = NULL;
678                 return -EINVAL;
679         }
680         display_class->dev_attrs = display_attrs;
681         display_class->suspend = display_suspend;
682         display_class->resume = display_resume;
683         mutex_init(&allocated_dsp_lock);
684         INIT_LIST_HEAD(&main_display_device_list);
685         INIT_LIST_HEAD(&aux_display_device_list);
686         return 0;
687 }
688
689 static void __exit rk_display_class_exit(void)
690 {
691         class_destroy(display_class);
692 }
693
694 subsys_initcall(rk_display_class_init);
695 module_exit(rk_display_class_exit);
696
697
698 MODULE_AUTHOR("zhengyang@rock-chips.com");
699 MODULE_DESCRIPTION("Driver for rk display device");
700 MODULE_LICENSE("GPL");