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 display_device_list;
10 static ssize_t display_show_name(struct device *dev,
11 struct device_attribute *attr, char *buf)
13 struct rk_display_device *dsp = dev_get_drvdata(dev);
14 return snprintf(buf, PAGE_SIZE, "%s\n", dsp->name);
17 static ssize_t display_show_type(struct device *dev,
18 struct device_attribute *attr, char *buf)
20 struct rk_display_device *dsp = dev_get_drvdata(dev);
21 return snprintf(buf, PAGE_SIZE, "%s\n", dsp->type);
24 static ssize_t display_show_enable(struct device *dev,
25 struct device_attribute *attr, char *buf)
27 struct rk_display_device *dsp = dev_get_drvdata(dev);
29 if(dsp->ops && dsp->ops->getenable)
30 enable = dsp->ops->getenable(dsp);
33 return snprintf(buf, PAGE_SIZE, "%d\n", enable);
36 static ssize_t display_store_enable(struct device *dev,
37 struct device_attribute *attr,
38 const char *buf, size_t size)
40 struct rk_display_device *dsp = dev_get_drvdata(dev);
43 sscanf(buf, "%d", &enable);
44 if(dsp->ops && dsp->ops->setenable)
45 dsp->ops->setenable(dsp, enable);
49 static ssize_t display_show_connect(struct device *dev,
50 struct device_attribute *attr, char *buf)
52 struct rk_display_device *dsp = dev_get_drvdata(dev);
54 if(dsp->ops && dsp->ops->getstatus)
55 connect = dsp->ops->getstatus(dsp);
58 return snprintf(buf, PAGE_SIZE, "%d\n", connect);
61 static int mode_string(char *buf, unsigned int offset,
62 const struct fb_videomode *mode)
67 // if (mode->flag & FB_MODE_IS_DETAILED)
69 // if (mode->flag & FB_MODE_IS_VESA)
71 // if (mode->flag & FB_MODE_IS_STANDARD)
74 if (mode->vmode & FB_VMODE_INTERLACED)
76 if (mode->vmode & FB_VMODE_DOUBLE)
79 return snprintf(&buf[offset], PAGE_SIZE - offset, "%dx%d%c-%d\n",
80 mode->xres, mode->yres, v, mode->refresh);
82 static ssize_t display_show_modes(struct device *dev,
83 struct device_attribute *attr, char *buf)
85 struct rk_display_device *dsp = dev_get_drvdata(dev);
86 struct list_head *modelist, *pos;
87 struct fb_modelist *fb_modelist;
88 const struct fb_videomode *mode;
90 if(dsp->ops && dsp->ops->getmodelist)
92 if(dsp->ops->getmodelist(dsp, &modelist))
99 list_for_each(pos, modelist) {
100 fb_modelist = list_entry(pos, struct fb_modelist, list);
101 mode = &fb_modelist->mode;
102 i += mode_string(buf, i, mode);
107 static ssize_t display_show_mode(struct device *dev,
108 struct device_attribute *attr, char *buf)
110 struct rk_display_device *dsp = dev_get_drvdata(dev);
111 struct fb_videomode mode;
113 if(dsp->ops && dsp->ops->getmode)
114 if(dsp->ops->getmode(dsp, &mode) == 0)
115 return mode_string(buf, 0, &mode);
119 static ssize_t display_store_mode(struct device *dev,
120 struct device_attribute *attr,
121 const char *buf, size_t count)
123 struct rk_display_device *dsp = dev_get_drvdata(dev);
125 struct list_head *modelist, *pos;
126 struct fb_modelist *fb_modelist;
127 struct fb_videomode *mode;
130 if(dsp->ops && dsp->ops->getmodelist)
132 if(dsp->ops && dsp->ops->getmodelist)
134 if(dsp->ops->getmodelist(dsp, &modelist))
137 list_for_each(pos, modelist) {
138 fb_modelist = list_entry(pos, struct fb_modelist, list);
139 mode = &fb_modelist->mode;
140 i = mode_string(mstr, 0, mode);
141 if (strncmp(mstr, buf, max(count, i)) == 0) {
142 if(dsp->ops && dsp->ops->setmode)
143 dsp->ops->setmode(dsp, mode);
151 static ssize_t display_show_scale(struct device *dev,
152 struct device_attribute *attr, char *buf)
154 struct rk_display_device *dsp = dev_get_drvdata(dev);
157 if(dsp->ops && dsp->ops->getscale) {
158 xscale = dsp->ops->getscale(dsp, DISPLAY_SCALE_X);
159 yscale = dsp->ops->getscale(dsp, DISPLAY_SCALE_Y);
161 return snprintf(buf, PAGE_SIZE, "xscale=%d yscale=%d\n", xscale, yscale);
166 static ssize_t display_store_scale(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t count)
170 struct rk_display_device *dsp = dev_get_drvdata(dev);
173 if(dsp->ops && dsp->ops->setscale) {
174 if(!strncmp(buf, "xscale", 6)) {
175 sscanf(buf, "xscale=%d", &scale);
176 dsp->ops->setscale(dsp, DISPLAY_SCALE_X, scale);
178 else if(!strncmp(buf, "yscale", 6)) {
179 sscanf(buf, "yscale=%d", &scale);
180 dsp->ops->setscale(dsp, DISPLAY_SCALE_Y, scale);
183 sscanf(buf, "%d", &scale);
184 dsp->ops->setscale(dsp, DISPLAY_SCALE_X, scale);
185 dsp->ops->setscale(dsp, DISPLAY_SCALE_Y, scale);
192 static struct device_attribute display_attrs[] = {
193 __ATTR(name, S_IRUGO, display_show_name, NULL),
194 __ATTR(type, S_IRUGO, display_show_type, NULL),
195 __ATTR(enable, 0664, display_show_enable, display_store_enable),
196 __ATTR(connect, S_IRUGO, display_show_connect, NULL),
197 __ATTR(modes, S_IRUGO, display_show_modes, NULL),
198 __ATTR(mode, 0664, display_show_mode, display_store_mode),
199 __ATTR(scale, 0664, display_show_scale, display_store_scale),
203 static int display_suspend(struct device *dev, pm_message_t state)
205 struct rk_display_device *dsp = dev_get_drvdata(dev);
207 mutex_lock(&dsp->lock);
208 if (likely(dsp->driver->suspend))
209 dsp->driver->suspend(dsp, state);
210 mutex_unlock(&dsp->lock);
214 static int display_resume(struct device *dev)
216 struct rk_display_device *dsp = dev_get_drvdata(dev);
218 mutex_lock(&dsp->lock);
219 if (likely(dsp->driver->resume))
220 dsp->driver->resume(dsp);
221 mutex_unlock(&dsp->lock);
225 void rk_display_device_enable(struct rk_display_device *ddev)
227 #ifndef CONFIG_DISPLAY_AUTO_SWITCH
230 struct list_head *pos, *head = &display_device_list;
231 struct rk_display_device *dev = NULL, *dev_enabled = NULL, *dev_enable = NULL;
232 int enable = 0,connect, has_connect = 0;
234 list_for_each(pos, head) {
235 dev = list_entry(pos, struct rk_display_device, list);
236 enable = dev->ops->getenable(dev);
237 connect = dev->ops->getstatus(dev);
243 // If no device is connected, enable highest priority device.
244 if(dev_enable == NULL) {
245 dev->ops->setenable(dev, 1);
249 if(dev_enable == dev_enabled) {
250 if(dev_enable != ddev)
251 ddev->ops->setenable(ddev, 0);
255 dev_enabled->ops->setenable(dev_enabled, 0);
256 dev_enable->ops->setenable(dev_enable, 1);
262 EXPORT_SYMBOL(rk_display_device_enable);
264 void rk_display_device_enable_other(struct rk_display_device *ddev)
266 #ifndef CONFIG_DISPLAY_AUTO_SWITCH
269 struct list_head *pos, *head = &display_device_list;
270 struct rk_display_device *dev;
273 list_for_each_prev(pos, head) {
274 dev = list_entry(pos, struct rk_display_device, list);
277 connect = dev->ops->getstatus(dev);
280 dev->ops->setenable(dev, 1);
287 EXPORT_SYMBOL(rk_display_device_enable_other);
289 void rk_display_device_disable_other(struct rk_display_device *ddev)
291 #ifndef CONFIG_DISPLAY_AUTO_SWITCH
294 struct list_head *pos, *head = &display_device_list;
295 struct rk_display_device *dev;
298 list_for_each(pos, head) {
299 dev = list_entry(pos, struct rk_display_device, list);
302 enable = dev->ops->getenable(dev);
304 dev->ops->setenable(dev, 0);
307 ddev->ops->setenable(ddev, 1);
310 EXPORT_SYMBOL(rk_display_device_disable_other);
312 void rk_display_device_select(int priority)
314 struct list_head *pos, *head = &display_device_list;
315 struct rk_display_device *dev;
316 int enable, found = 0;
318 list_for_each(pos, head) {
319 dev = list_entry(pos, struct rk_display_device, list);
320 if(dev->priority == priority)
326 printk("[%s] select display interface %d not exist\n", __FUNCTION__, priority);
330 list_for_each(pos, head) {
331 dev = list_entry(pos, struct rk_display_device, list);
332 enable = dev->ops->getenable(dev);
333 if(dev->priority == priority)
336 dev->ops->setenable(dev, 1);
339 dev->ops->setenable(dev, 0);
342 EXPORT_SYMBOL(rk_display_device_select);
343 static struct mutex allocated_dsp_lock;
344 static DEFINE_IDR(allocated_dsp);
345 static struct class *display_class;
347 struct rk_display_device *rk_display_device_register(struct rk_display_driver *driver,
348 struct device *parent, void *devdata)
350 struct rk_display_device *new_dev = NULL;
353 if (unlikely(!driver))
356 mutex_lock(&allocated_dsp_lock);
357 ret = idr_pre_get(&allocated_dsp, GFP_KERNEL);
358 mutex_unlock(&allocated_dsp_lock);
362 new_dev = kzalloc(sizeof(struct rk_display_device), GFP_KERNEL);
363 if (likely(new_dev) && unlikely(driver->probe(new_dev, devdata))) {
364 // Reserve the index for this display
365 mutex_lock(&allocated_dsp_lock);
366 ret = idr_get_new(&allocated_dsp, new_dev, &new_dev->idx);
367 mutex_unlock(&allocated_dsp_lock);
370 new_dev->dev = device_create(display_class, parent,
371 MKDEV(0, 0), new_dev,
372 "%s", new_dev->type);
373 if (!IS_ERR(new_dev->dev)) {
374 new_dev->parent = parent;
375 new_dev->driver = driver;
377 new_dev->dev->driver = parent->driver;
378 mutex_init(&new_dev->lock);
379 // Add new device to display device list.
381 struct list_head *pos, *head = &display_device_list;
382 struct rk_display_device *dev;
384 list_for_each(pos, head) {
385 dev = list_entry(pos, struct rk_display_device, list);
386 if(dev->priority > new_dev->priority)
389 list_add_tail(&new_dev->list, pos);
393 mutex_lock(&allocated_dsp_lock);
394 idr_remove(&allocated_dsp, new_dev->idx);
395 mutex_unlock(&allocated_dsp_lock);
402 EXPORT_SYMBOL(rk_display_device_register);
404 void rk_display_device_unregister(struct rk_display_device *ddev)
409 mutex_lock(&ddev->lock);
410 device_unregister(ddev->dev);
411 mutex_unlock(&ddev->lock);
412 // Mark device index as avaliable
413 mutex_lock(&allocated_dsp_lock);
414 idr_remove(&allocated_dsp, ddev->idx);
415 mutex_unlock(&allocated_dsp_lock);
416 list_del(&ddev->list);
419 EXPORT_SYMBOL(rk_display_device_unregister);
421 static int __init rk_display_class_init(void)
423 display_class = class_create(THIS_MODULE, "display");
424 if (IS_ERR(display_class)) {
425 printk(KERN_ERR "Failed to create display class\n");
426 display_class = NULL;
429 display_class->dev_attrs = display_attrs;
430 display_class->suspend = display_suspend;
431 display_class->resume = display_resume;
432 mutex_init(&allocated_dsp_lock);
433 INIT_LIST_HEAD(&display_device_list);
437 static void __exit rk_display_class_exit(void)
439 class_destroy(display_class);
442 subsys_initcall(rk_display_class_init);
443 module_exit(rk_display_class_exit);
446 MODULE_AUTHOR("zhengyang@rock-chips.com");
447 MODULE_DESCRIPTION("Driver for rk display device");
448 MODULE_LICENSE("GPL");