1 #include <linux/module.h>
2 #include <linux/ctype.h>
5 #include <linux/kdev_t.h>
6 #include <linux/display-sys.h>
8 static struct list_head main_display_device_list;
9 static struct list_head aux_display_device_list;
11 static ssize_t display_show_name(struct device *dev,
12 struct device_attribute *attr, char *buf)
14 struct rk_display_device *dsp = dev_get_drvdata(dev);
16 return snprintf(buf, PAGE_SIZE, "%s\n", dsp->name);
19 static ssize_t display_show_type(struct device *dev,
20 struct device_attribute *attr, char *buf)
22 struct rk_display_device *dsp = dev_get_drvdata(dev);
24 return snprintf(buf, PAGE_SIZE, "%s\n", dsp->type);
27 static ssize_t display_show_property(struct device *dev,
28 struct device_attribute *attr, char *buf)
30 struct rk_display_device *dsp = dev_get_drvdata(dev);
32 return snprintf(buf, PAGE_SIZE, "%d\n", dsp->property);
35 static ssize_t display_show_enable(struct device *dev,
36 struct device_attribute *attr, char *buf)
38 struct rk_display_device *dsp = dev_get_drvdata(dev);
41 if (dsp->ops && dsp->ops->getenable)
42 enable = dsp->ops->getenable(dsp);
45 return snprintf(buf, PAGE_SIZE, "%d\n", enable);
48 static ssize_t display_store_enable(struct device *dev,
49 struct device_attribute *attr,
50 const char *buf, size_t size)
52 struct rk_display_device *dsp = dev_get_drvdata(dev);
55 if (kstrtoint(buf, 0, &enable))
57 if (dsp->ops && dsp->ops->setenable)
58 dsp->ops->setenable(dsp, enable);
62 static ssize_t display_show_connect(struct device *dev,
63 struct device_attribute *attr, char *buf)
65 struct rk_display_device *dsp = dev_get_drvdata(dev);
68 if (dsp->ops && dsp->ops->getstatus)
69 connect = dsp->ops->getstatus(dsp);
72 return snprintf(buf, PAGE_SIZE, "%d\n", connect);
75 static int mode_string(char *buf, unsigned int offset,
76 const struct fb_videomode *mode)
80 if (mode->xres == 0 && mode->yres == 0)
81 return snprintf(&buf[offset], PAGE_SIZE - offset, "auto\n");
83 if (mode->flag & FB_MODE_IS_DETAILED)
85 if (mode->flag & FB_MODE_IS_VESA)
87 if (mode->flag & FB_MODE_IS_STANDARD)
90 if (mode->vmode & FB_VMODE_INTERLACED)
92 if (mode->vmode & FB_VMODE_DOUBLE)
95 return snprintf(&buf[offset], PAGE_SIZE - offset,
96 "%dx%d%c-%d(YCbCr420)\n",
97 mode->xres, mode->yres, v, mode->refresh);
99 return snprintf(&buf[offset], PAGE_SIZE - offset,
101 mode->xres, mode->yres, v, mode->refresh);
104 static ssize_t display_show_modes(struct device *dev,
105 struct device_attribute *attr, char *buf)
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;
113 if (dsp->ops && dsp->ops->getmodelist) {
114 if (dsp->ops->getmodelist(dsp, &modelist))
120 if (dsp->priority == DISPLAY_PRIORITY_HDMI)
121 i += snprintf(buf, PAGE_SIZE, "auto\n");
123 list_for_each(pos, modelist) {
124 display_modelist = list_entry(pos,
125 struct display_modelist,
127 mode = &display_modelist->mode;
128 i += mode_string(buf, i, mode);
133 static ssize_t display_show_mode(struct device *dev,
134 struct device_attribute *attr, char *buf)
136 struct rk_display_device *dsp = dev_get_drvdata(dev);
137 struct fb_videomode mode;
139 if (dsp->ops && dsp->ops->getmode)
140 if (dsp->ops->getmode(dsp, &mode) == 0)
141 return mode_string(buf, 0, &mode);
145 static ssize_t display_store_mode(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf, size_t count)
149 struct rk_display_device *dsp = dev_get_drvdata(dev);
151 struct list_head *modelist, *pos;
152 struct display_modelist *display_modelist;
153 struct fb_videomode *mode;
156 if (!memcmp(buf, "auto", 4)) {
157 if (dsp->ops && dsp->ops->setmode)
158 dsp->ops->setmode(dsp, NULL);
162 if (dsp->ops && dsp->ops->getmodelist) {
163 if (dsp->ops && dsp->ops->getmodelist) {
164 if (dsp->ops->getmodelist(dsp, &modelist))
167 list_for_each(pos, modelist) {
168 display_modelist = list_entry(pos,
169 struct display_modelist,
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);
183 static ssize_t display_show_scale(struct device *dev,
184 struct device_attribute *attr,
187 struct rk_display_device *dsp = dev_get_drvdata(dev);
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",
201 static ssize_t display_store_scale(struct device *dev,
202 struct device_attribute *attr,
203 const char *buf, size_t count)
205 struct rk_display_device *dsp = dev_get_drvdata(dev);
208 if (dsp->ops && dsp->ops->setscale) {
209 if (!strncmp(buf, "xscale", 6)) {
210 if (!kstrtoint(buf, 0, &scale))
211 dsp->ops->setscale(dsp,
214 } else if (!strncmp(buf, "yscale", 6)) {
215 if (!kstrtoint(buf, 0, &scale))
216 dsp->ops->setscale(dsp,
220 if (!kstrtoint(buf, 0, &scale)) {
221 dsp->ops->setscale(dsp,
224 dsp->ops->setscale(dsp,
234 static ssize_t display_show_3dmode(struct device *dev,
235 struct device_attribute *attr, char *buf)
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;
243 int mode_strlen, format_3d;
245 if (dsp->ops && dsp->ops->getmodelist) {
246 if (dsp->ops->getmodelist(dsp, &modelist))
252 if (dsp->ops && dsp->ops->getmode) {
253 if (dsp->ops->getmode(dsp, &mode))
259 list_for_each(pos, modelist) {
260 display_modelist = list_entry(pos,
261 struct display_modelist,
263 if (fb_mode_is_equal(&mode, &display_modelist->mode))
266 display_modelist = NULL;
268 if (display_modelist)
269 i = snprintf(buf, PAGE_SIZE, "3dmodes=%d\n",
270 display_modelist->format_3d);
272 i = snprintf(buf, PAGE_SIZE, "3dmodes=0\n");
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);
278 list_for_each(pos, modelist) {
279 display_modelist = list_entry(pos,
280 struct display_modelist,
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);
292 static ssize_t display_store_3dmode(struct device *dev,
293 struct device_attribute *attr,
294 const char *buf, size_t count)
296 struct rk_display_device *dsp = dev_get_drvdata(dev);
299 if (dsp->ops && dsp->ops->set3dmode) {
300 if (!kstrtoint(buf, 0, &mode))
301 dsp->ops->set3dmode(dsp, mode);
307 static ssize_t display_show_color(struct device *dev,
308 struct device_attribute *attr, char *buf)
310 struct rk_display_device *dsp = dev_get_drvdata(dev);
312 if (dsp->ops && dsp->ops->getcolor)
313 return dsp->ops->getcolor(dsp, buf);
318 static ssize_t display_store_color(struct device *dev,
319 struct device_attribute *attr,
320 const char *buf, size_t count)
322 struct rk_display_device *dsp = dev_get_drvdata(dev);
324 if (dsp->ops && dsp->ops->setcolor) {
325 if (!dsp->ops->setcolor(dsp, buf, count))
331 static ssize_t display_show_sinkaudioinfo(struct device *dev,
332 struct device_attribute *attr,
335 struct rk_display_device *dsp = dev_get_drvdata(dev);
339 if (dsp->ops && dsp->ops->getedidaudioinfo) {
340 ret = dsp->ops->getedidaudioinfo(dsp, audioinfo, 200);
342 return snprintf(buf, PAGE_SIZE, "%s\n", audioinfo);
347 static ssize_t display_show_monspecs(struct device *dev,
348 struct device_attribute *attr, char *buf)
350 struct rk_display_device *dsp = dev_get_drvdata(dev);
351 struct fb_monspecs monspecs;
354 if (dsp->ops && dsp->ops->getmonspecs) {
355 ret = dsp->ops->getmonspecs(dsp, &monspecs);
357 memcpy(buf, &monspecs, sizeof(struct fb_monspecs));
358 return sizeof(struct fb_monspecs);
364 static ssize_t display_show_debug(struct device *dev,
365 struct device_attribute *attr, char *buf)
370 static ssize_t display_store_debug(struct device *dev,
371 struct device_attribute *attr,
372 const char *buf, size_t count)
375 struct rk_display_device *dsp = dev_get_drvdata(dev);
377 if (dsp->ops && dsp->ops->setdebug) {
378 if (kstrtoint(buf, 0, &cmd) == 0)
379 dsp->ops->setdebug(dsp, cmd);
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),
402 static int display_suspend(struct device *dev, pm_message_t state)
404 struct rk_display_device *dsp = dev_get_drvdata(dev);
406 mutex_lock(&dsp->lock);
407 if (likely(dsp->driver->suspend))
408 dsp->driver->suspend(dsp, state);
409 mutex_unlock(&dsp->lock);
413 static int display_resume(struct device *dev)
415 struct rk_display_device *dsp = dev_get_drvdata(dev);
417 mutex_lock(&dsp->lock);
418 if (likely(dsp->driver->resume))
419 dsp->driver->resume(dsp);
420 mutex_unlock(&dsp->lock);
424 int display_add_videomode(const struct fb_videomode *mode,
425 struct list_head *head)
427 struct list_head *pos;
428 struct display_modelist *modelist;
429 struct fb_videomode *m;
432 list_for_each(pos, head) {
433 modelist = list_entry(pos, struct display_modelist, list);
435 if (fb_mode_is_equal(m, mode)) {
441 modelist = kmalloc(sizeof(*modelist),
446 modelist->mode = *mode;
447 list_add(&modelist->list, head);
452 void rk_display_device_enable(struct rk_display_device *ddev)
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;
459 if (ddev->property == DISPLAY_MAIN)
460 head = &main_display_device_list;
462 head = &aux_display_device_list;
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);
473 /* If no device is connected, enable highest priority device. */
474 if (dev_enable == NULL) {
475 dev->ops->setenable(dev, 1);
479 if (dev_enable == dev_enabled) {
480 if (dev_enable != ddev)
481 ddev->ops->setenable(ddev, 0);
484 dev_enabled->ops->setenable(dev_enabled, 0);
485 dev_enable->ops->setenable(dev_enable, 1);
488 EXPORT_SYMBOL(rk_display_device_enable);
490 void rk_display_device_enable_other(struct rk_display_device *ddev)
492 #ifndef CONFIG_DISPLAY_AUTO_SWITCH
495 struct list_head *pos, *head;
496 struct rk_display_device *dev;
499 if (ddev->property == DISPLAY_MAIN)
500 head = &main_display_device_list;
502 head = &aux_display_device_list;
504 list_for_each_prev(pos, head) {
505 dev = list_entry(pos, struct rk_display_device, list);
507 connect = dev->ops->getstatus(dev);
509 dev->ops->setenable(dev, 1);
516 EXPORT_SYMBOL(rk_display_device_enable_other);
518 void rk_display_device_disable_other(struct rk_display_device *ddev)
520 #ifndef CONFIG_DISPLAY_AUTO_SWITCH
523 struct list_head *pos, *head;
524 struct rk_display_device *dev;
527 if (ddev->property == DISPLAY_MAIN)
528 head = &main_display_device_list;
530 head = &aux_display_device_list;
532 list_for_each(pos, head) {
533 dev = list_entry(pos, struct rk_display_device, list);
535 enable = dev->ops->getenable(dev);
537 dev->ops->setenable(dev, 0);
540 ddev->ops->setenable(ddev, 1);
543 EXPORT_SYMBOL(rk_display_device_disable_other);
545 void rk_display_device_select(int property, int priority)
547 struct list_head *pos, *head;
548 struct rk_display_device *dev;
549 int enable, found = 0;
551 if (property == DISPLAY_MAIN)
552 head = &main_display_device_list;
554 head = &aux_display_device_list;
556 list_for_each(pos, head) {
557 dev = list_entry(pos, struct rk_display_device, list);
558 if (dev->priority == priority)
563 pr_err("[%s] select display interface %d not exist\n",
568 list_for_each(pos, head) {
569 dev = list_entry(pos, struct rk_display_device, list);
570 enable = dev->ops->getenable(dev);
571 if (dev->priority == priority) {
573 dev->ops->setenable(dev, 1);
575 dev->ops->setenable(dev, 0);
579 EXPORT_SYMBOL(rk_display_device_select);
580 static struct mutex allocated_dsp_lock;
581 static DEFINE_IDR(allocated_dsp);
582 static struct class *display_class;
584 struct rk_display_device
585 *rk_display_device_register(struct rk_display_driver *driver,
586 struct device *parent, void *devdata)
588 struct rk_display_device *new_dev = NULL;
591 if (unlikely(!driver))
594 new_dev = kzalloc(sizeof(*new_dev), GFP_KERNEL);
595 if (likely(new_dev) && unlikely(driver->probe(new_dev, devdata))) {
596 /* Reserve the index for this display */
597 mutex_lock(&allocated_dsp_lock);
598 new_dev->idx = idr_alloc(&allocated_dsp, new_dev,
600 mutex_unlock(&allocated_dsp_lock);
602 if (new_dev->idx >= 0) {
603 if (new_dev->property == DISPLAY_MAIN)
605 device_create(display_class, parent,
606 MKDEV(0, 0), new_dev,
607 "%s", new_dev->type);
610 device_create(display_class, parent,
611 MKDEV(0, 0), new_dev,
615 if (!IS_ERR(new_dev->dev)) {
616 new_dev->parent = parent;
617 new_dev->driver = driver;
619 new_dev->dev->driver = parent->driver;
620 mutex_init(&new_dev->lock);
621 /* Add new device to display device list. */
623 struct list_head *pos, *head;
624 struct rk_display_device *dev;
626 if (new_dev->property == DISPLAY_MAIN)
627 head = &main_display_device_list;
629 head = &aux_display_device_list;
631 list_for_each(pos, head) {
634 struct rk_display_device,
636 if (dev->priority > new_dev->priority)
639 list_add_tail(&new_dev->list, pos);
643 mutex_lock(&allocated_dsp_lock);
644 idr_remove(&allocated_dsp, new_dev->idx);
645 mutex_unlock(&allocated_dsp_lock);
652 EXPORT_SYMBOL(rk_display_device_register);
654 void rk_display_device_unregister(struct rk_display_device *ddev)
659 mutex_lock(&ddev->lock);
660 device_unregister(ddev->dev);
661 mutex_unlock(&ddev->lock);
662 /* Mark device index as avaliable */
663 mutex_lock(&allocated_dsp_lock);
664 idr_remove(&allocated_dsp, ddev->idx);
665 mutex_unlock(&allocated_dsp_lock);
666 list_del(&ddev->list);
669 EXPORT_SYMBOL(rk_display_device_unregister);
671 static int __init rk_display_class_init(void)
673 display_class = class_create(THIS_MODULE, "display");
674 if (IS_ERR(display_class)) {
675 pr_err("Failed to create display class\n");
676 display_class = NULL;
679 display_class->dev_attrs = display_attrs;
680 display_class->suspend = display_suspend;
681 display_class->resume = display_resume;
682 mutex_init(&allocated_dsp_lock);
683 INIT_LIST_HEAD(&main_display_device_list);
684 INIT_LIST_HEAD(&aux_display_device_list);
688 static void __exit rk_display_class_exit(void)
690 class_destroy(display_class);
693 subsys_initcall(rk_display_class_init);
694 module_exit(rk_display_class_exit);
697 MODULE_AUTHOR("zhengyang@rock-chips.com");
698 MODULE_DESCRIPTION("Driver for rk display device");
699 MODULE_LICENSE("GPL");