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 (sscanf(buf, "%d", &cmd) != -1)
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->priority != DISPLAY_PRIORITY_HDMI)
485 dev_enabled->ops->setenable(dev_enabled, 0);
486 dev_enable->ops->setenable(dev_enable, 1);
489 EXPORT_SYMBOL(rk_display_device_enable);
491 void rk_display_device_enable_other(struct rk_display_device *ddev)
493 #ifndef CONFIG_DISPLAY_AUTO_SWITCH
496 struct list_head *pos, *head;
497 struct rk_display_device *dev;
500 if (ddev->property == DISPLAY_MAIN)
501 head = &main_display_device_list;
503 head = &aux_display_device_list;
505 list_for_each_prev(pos, head) {
506 dev = list_entry(pos, struct rk_display_device, list);
508 connect = dev->ops->getstatus(dev);
510 dev->ops->setenable(dev, 1);
517 EXPORT_SYMBOL(rk_display_device_enable_other);
519 void rk_display_device_disable_other(struct rk_display_device *ddev)
521 #ifndef CONFIG_DISPLAY_AUTO_SWITCH
524 struct list_head *pos, *head;
525 struct rk_display_device *dev;
528 if (ddev->property == DISPLAY_MAIN)
529 head = &main_display_device_list;
531 head = &aux_display_device_list;
533 list_for_each(pos, head) {
534 dev = list_entry(pos, struct rk_display_device, list);
536 enable = dev->ops->getenable(dev);
538 dev->ops->setenable(dev, 0);
541 ddev->ops->setenable(ddev, 1);
544 EXPORT_SYMBOL(rk_display_device_disable_other);
546 void rk_display_device_select(int property, int priority)
548 struct list_head *pos, *head;
549 struct rk_display_device *dev;
550 int enable, found = 0;
552 if (property == DISPLAY_MAIN)
553 head = &main_display_device_list;
555 head = &aux_display_device_list;
557 list_for_each(pos, head) {
558 dev = list_entry(pos, struct rk_display_device, list);
559 if (dev->priority == priority)
564 pr_err("[%s] select display interface %d not exist\n",
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) {
574 dev->ops->setenable(dev, 1);
576 dev->ops->setenable(dev, 0);
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;
585 struct rk_display_device
586 *rk_display_device_register(struct rk_display_driver *driver,
587 struct device *parent, void *devdata)
589 struct rk_display_device *new_dev = NULL;
592 if (unlikely(!driver))
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,
601 mutex_unlock(&allocated_dsp_lock);
603 if (new_dev->idx >= 0) {
604 if (new_dev->property == DISPLAY_MAIN)
606 device_create(display_class, parent,
607 MKDEV(0, 0), new_dev,
608 "%s", new_dev->type);
611 device_create(display_class, parent,
612 MKDEV(0, 0), new_dev,
616 if (!IS_ERR(new_dev->dev)) {
617 new_dev->parent = parent;
618 new_dev->driver = driver;
620 new_dev->dev->driver = parent->driver;
621 mutex_init(&new_dev->lock);
622 /* Add new device to display device list. */
624 struct list_head *pos, *head;
625 struct rk_display_device *dev;
627 if (new_dev->property == DISPLAY_MAIN)
628 head = &main_display_device_list;
630 head = &aux_display_device_list;
632 list_for_each(pos, head) {
635 struct rk_display_device,
637 if (dev->priority > new_dev->priority)
640 list_add_tail(&new_dev->list, pos);
644 mutex_lock(&allocated_dsp_lock);
645 idr_remove(&allocated_dsp, new_dev->idx);
646 mutex_unlock(&allocated_dsp_lock);
653 EXPORT_SYMBOL(rk_display_device_register);
655 void rk_display_device_unregister(struct rk_display_device *ddev)
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);
670 EXPORT_SYMBOL(rk_display_device_unregister);
672 static int __init rk_display_class_init(void)
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;
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);
689 static void __exit rk_display_class_exit(void)
691 class_destroy(display_class);
694 subsys_initcall(rk_display_class_init);
695 module_exit(rk_display_class_exit);
698 MODULE_AUTHOR("zhengyang@rock-chips.com");
699 MODULE_DESCRIPTION("Driver for rk display device");
700 MODULE_LICENSE("GPL");