Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / video / omap2 / dss / core.c
1 /*
2  * linux/drivers/video/omap2/dss/core.c
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6  *
7  * Some code and ideas taken from drivers/video/omap/ driver
8  * by Imre Deak.
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License version 2 as published by
12  * the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #define DSS_SUBSYS_NAME "CORE"
24
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/clk.h>
28 #include <linux/err.h>
29 #include <linux/platform_device.h>
30 #include <linux/seq_file.h>
31 #include <linux/debugfs.h>
32 #include <linux/io.h>
33 #include <linux/device.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/suspend.h>
36
37 #include <video/omapdss.h>
38
39 #include "dss.h"
40 #include "dss_features.h"
41
42 static struct {
43         struct platform_device *pdev;
44
45         struct regulator *vdds_dsi_reg;
46         struct regulator *vdds_sdi_reg;
47 } core;
48
49 static char *def_disp_name;
50 module_param_named(def_disp, def_disp_name, charp, 0);
51 MODULE_PARM_DESC(def_disp, "default display name");
52
53 #ifdef DEBUG
54 unsigned int dss_debug;
55 module_param_named(debug, dss_debug, bool, 0644);
56 #endif
57
58 static int omap_dss_register_device(struct omap_dss_device *);
59 static void omap_dss_unregister_device(struct omap_dss_device *);
60
61 /* REGULATORS */
62
63 struct regulator *dss_get_vdds_dsi(void)
64 {
65         struct regulator *reg;
66
67         if (core.vdds_dsi_reg != NULL)
68                 return core.vdds_dsi_reg;
69
70         reg = regulator_get(&core.pdev->dev, "vdds_dsi");
71         if (!IS_ERR(reg))
72                 core.vdds_dsi_reg = reg;
73
74         return reg;
75 }
76
77 struct regulator *dss_get_vdds_sdi(void)
78 {
79         struct regulator *reg;
80
81         if (core.vdds_sdi_reg != NULL)
82                 return core.vdds_sdi_reg;
83
84         reg = regulator_get(&core.pdev->dev, "vdds_sdi");
85         if (!IS_ERR(reg))
86                 core.vdds_sdi_reg = reg;
87
88         return reg;
89 }
90
91 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_OMAP2_DSS_DEBUG_SUPPORT)
92 static int dss_debug_show(struct seq_file *s, void *unused)
93 {
94         void (*func)(struct seq_file *) = s->private;
95         func(s);
96         return 0;
97 }
98
99 static int dss_debug_open(struct inode *inode, struct file *file)
100 {
101         return single_open(file, dss_debug_show, inode->i_private);
102 }
103
104 static const struct file_operations dss_debug_fops = {
105         .open           = dss_debug_open,
106         .read           = seq_read,
107         .llseek         = seq_lseek,
108         .release        = single_release,
109 };
110
111 static struct dentry *dss_debugfs_dir;
112
113 static int dss_initialize_debugfs(void)
114 {
115         dss_debugfs_dir = debugfs_create_dir("omapdss", NULL);
116         if (IS_ERR(dss_debugfs_dir)) {
117                 int err = PTR_ERR(dss_debugfs_dir);
118                 dss_debugfs_dir = NULL;
119                 return err;
120         }
121
122         debugfs_create_file("clk", S_IRUGO, dss_debugfs_dir,
123                         &dss_debug_dump_clocks, &dss_debug_fops);
124
125 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
126         debugfs_create_file("dispc_irq", S_IRUGO, dss_debugfs_dir,
127                         &dispc_dump_irqs, &dss_debug_fops);
128 #endif
129
130 #if defined(CONFIG_OMAP2_DSS_DSI) && defined(CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS)
131         dsi_create_debugfs_files_irq(dss_debugfs_dir, &dss_debug_fops);
132 #endif
133
134         debugfs_create_file("dss", S_IRUGO, dss_debugfs_dir,
135                         &dss_dump_regs, &dss_debug_fops);
136         debugfs_create_file("dispc", S_IRUGO, dss_debugfs_dir,
137                         &dispc_dump_regs, &dss_debug_fops);
138 #ifdef CONFIG_OMAP2_DSS_RFBI
139         debugfs_create_file("rfbi", S_IRUGO, dss_debugfs_dir,
140                         &rfbi_dump_regs, &dss_debug_fops);
141 #endif
142 #ifdef CONFIG_OMAP2_DSS_DSI
143         dsi_create_debugfs_files_reg(dss_debugfs_dir, &dss_debug_fops);
144 #endif
145 #ifdef CONFIG_OMAP2_DSS_VENC
146         debugfs_create_file("venc", S_IRUGO, dss_debugfs_dir,
147                         &venc_dump_regs, &dss_debug_fops);
148 #endif
149 #ifdef CONFIG_OMAP4_DSS_HDMI
150         debugfs_create_file("hdmi", S_IRUGO, dss_debugfs_dir,
151                         &hdmi_dump_regs, &dss_debug_fops);
152 #endif
153         return 0;
154 }
155
156 static void dss_uninitialize_debugfs(void)
157 {
158         if (dss_debugfs_dir)
159                 debugfs_remove_recursive(dss_debugfs_dir);
160 }
161 #else /* CONFIG_DEBUG_FS && CONFIG_OMAP2_DSS_DEBUG_SUPPORT */
162 static inline int dss_initialize_debugfs(void)
163 {
164         return 0;
165 }
166 static inline void dss_uninitialize_debugfs(void)
167 {
168 }
169 #endif /* CONFIG_DEBUG_FS && CONFIG_OMAP2_DSS_DEBUG_SUPPORT */
170
171 /* PLATFORM DEVICE */
172 static int omap_dss_pm_notif(struct notifier_block *b, unsigned long v, void *d)
173 {
174         DSSDBG("pm notif %lu\n", v);
175
176         switch (v) {
177         case PM_SUSPEND_PREPARE:
178                 DSSDBG("suspending displays\n");
179                 return dss_suspend_all_devices();
180
181         case PM_POST_SUSPEND:
182                 DSSDBG("resuming displays\n");
183                 return dss_resume_all_devices();
184
185         default:
186                 return 0;
187         }
188 }
189
190 static struct notifier_block omap_dss_pm_notif_block = {
191         .notifier_call = omap_dss_pm_notif,
192 };
193
194 static int __init omap_dss_probe(struct platform_device *pdev)
195 {
196         struct omap_dss_board_info *pdata = pdev->dev.platform_data;
197         int r;
198         int i;
199
200         core.pdev = pdev;
201
202         dss_features_init();
203
204         dss_init_overlay_managers(pdev);
205         dss_init_overlays(pdev);
206
207         r = dss_initialize_debugfs();
208         if (r)
209                 goto err_debugfs;
210
211         for (i = 0; i < pdata->num_devices; ++i) {
212                 struct omap_dss_device *dssdev = pdata->devices[i];
213
214                 r = omap_dss_register_device(dssdev);
215                 if (r) {
216                         DSSERR("device %d %s register failed %d\n", i,
217                                 dssdev->name ?: "unnamed", r);
218
219                         while (--i >= 0)
220                                 omap_dss_unregister_device(pdata->devices[i]);
221
222                         goto err_register;
223                 }
224
225                 if (def_disp_name && strcmp(def_disp_name, dssdev->name) == 0)
226                         pdata->default_device = dssdev;
227         }
228
229         register_pm_notifier(&omap_dss_pm_notif_block);
230
231         return 0;
232
233 err_register:
234         dss_uninitialize_debugfs();
235 err_debugfs:
236
237         return r;
238 }
239
240 static int omap_dss_remove(struct platform_device *pdev)
241 {
242         struct omap_dss_board_info *pdata = pdev->dev.platform_data;
243         int i;
244
245         unregister_pm_notifier(&omap_dss_pm_notif_block);
246
247         dss_uninitialize_debugfs();
248
249         dss_uninit_overlays(pdev);
250         dss_uninit_overlay_managers(pdev);
251
252         for (i = 0; i < pdata->num_devices; ++i)
253                 omap_dss_unregister_device(pdata->devices[i]);
254
255         return 0;
256 }
257
258 static void omap_dss_shutdown(struct platform_device *pdev)
259 {
260         DSSDBG("shutdown\n");
261         dss_disable_all_devices();
262 }
263
264 static struct platform_driver omap_dss_driver = {
265         .probe          = omap_dss_probe,
266         .remove         = omap_dss_remove,
267         .shutdown       = omap_dss_shutdown,
268         .driver         = {
269                 .name   = "omapdss",
270                 .owner  = THIS_MODULE,
271         },
272 };
273
274 /* BUS */
275 static int dss_bus_match(struct device *dev, struct device_driver *driver)
276 {
277         struct omap_dss_device *dssdev = to_dss_device(dev);
278
279         DSSDBG("bus_match. dev %s/%s, drv %s\n",
280                         dev_name(dev), dssdev->driver_name, driver->name);
281
282         return strcmp(dssdev->driver_name, driver->name) == 0;
283 }
284
285 static ssize_t device_name_show(struct device *dev,
286                 struct device_attribute *attr, char *buf)
287 {
288         struct omap_dss_device *dssdev = to_dss_device(dev);
289         return snprintf(buf, PAGE_SIZE, "%s\n",
290                         dssdev->name ?
291                         dssdev->name : "");
292 }
293
294 static struct device_attribute default_dev_attrs[] = {
295         __ATTR(name, S_IRUGO, device_name_show, NULL),
296         __ATTR_NULL,
297 };
298
299 static ssize_t driver_name_show(struct device_driver *drv, char *buf)
300 {
301         struct omap_dss_driver *dssdrv = to_dss_driver(drv);
302         return snprintf(buf, PAGE_SIZE, "%s\n",
303                         dssdrv->driver.name ?
304                         dssdrv->driver.name : "");
305 }
306 static struct driver_attribute default_drv_attrs[] = {
307         __ATTR(name, S_IRUGO, driver_name_show, NULL),
308         __ATTR_NULL,
309 };
310
311 static struct bus_type dss_bus_type = {
312         .name = "omapdss",
313         .match = dss_bus_match,
314         .dev_attrs = default_dev_attrs,
315         .drv_attrs = default_drv_attrs,
316 };
317
318 static void dss_bus_release(struct device *dev)
319 {
320         DSSDBG("bus_release\n");
321 }
322
323 static struct device dss_bus = {
324         .release = dss_bus_release,
325 };
326
327 struct bus_type *dss_get_bus(void)
328 {
329         return &dss_bus_type;
330 }
331
332 /* DRIVER */
333 static int dss_driver_probe(struct device *dev)
334 {
335         int r;
336         struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
337         struct omap_dss_device *dssdev = to_dss_device(dev);
338         struct omap_dss_board_info *pdata = core.pdev->dev.platform_data;
339         bool force;
340
341         DSSDBG("driver_probe: dev %s/%s, drv %s\n",
342                                 dev_name(dev), dssdev->driver_name,
343                                 dssdrv->driver.name);
344
345         dss_init_device(core.pdev, dssdev);
346
347         force = pdata->default_device == dssdev;
348         dss_recheck_connections(dssdev, force);
349
350         r = dssdrv->probe(dssdev);
351
352         if (r) {
353                 DSSERR("driver probe failed: %d\n", r);
354                 dss_uninit_device(core.pdev, dssdev);
355                 return r;
356         }
357
358         DSSDBG("probe done for device %s\n", dev_name(dev));
359
360         dssdev->driver = dssdrv;
361
362         return 0;
363 }
364
365 static int dss_driver_remove(struct device *dev)
366 {
367         struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
368         struct omap_dss_device *dssdev = to_dss_device(dev);
369
370         DSSDBG("driver_remove: dev %s/%s\n", dev_name(dev),
371                         dssdev->driver_name);
372
373         dssdrv->remove(dssdev);
374
375         dss_uninit_device(core.pdev, dssdev);
376
377         dssdev->driver = NULL;
378
379         return 0;
380 }
381
382 int omap_dss_register_driver(struct omap_dss_driver *dssdriver)
383 {
384         dssdriver->driver.bus = &dss_bus_type;
385         dssdriver->driver.probe = dss_driver_probe;
386         dssdriver->driver.remove = dss_driver_remove;
387
388         if (dssdriver->get_resolution == NULL)
389                 dssdriver->get_resolution = omapdss_default_get_resolution;
390         if (dssdriver->get_recommended_bpp == NULL)
391                 dssdriver->get_recommended_bpp =
392                         omapdss_default_get_recommended_bpp;
393         if (dssdriver->get_timings == NULL)
394                 dssdriver->get_timings = omapdss_default_get_timings;
395
396         return driver_register(&dssdriver->driver);
397 }
398 EXPORT_SYMBOL(omap_dss_register_driver);
399
400 void omap_dss_unregister_driver(struct omap_dss_driver *dssdriver)
401 {
402         driver_unregister(&dssdriver->driver);
403 }
404 EXPORT_SYMBOL(omap_dss_unregister_driver);
405
406 /* DEVICE */
407 static void reset_device(struct device *dev, int check)
408 {
409         u8 *dev_p = (u8 *)dev;
410         u8 *dev_end = dev_p + sizeof(*dev);
411         void *saved_pdata;
412
413         saved_pdata = dev->platform_data;
414         if (check) {
415                 /*
416                  * Check if there is any other setting than platform_data
417                  * in struct device; warn that these will be reset by our
418                  * init.
419                  */
420                 dev->platform_data = NULL;
421                 while (dev_p < dev_end) {
422                         if (*dev_p) {
423                                 WARN("%s: struct device fields will be "
424                                                 "discarded\n",
425                                      __func__);
426                                 break;
427                         }
428                         dev_p++;
429                 }
430         }
431         memset(dev, 0, sizeof(*dev));
432         dev->platform_data = saved_pdata;
433 }
434
435
436 static void omap_dss_dev_release(struct device *dev)
437 {
438         reset_device(dev, 0);
439 }
440
441 static int omap_dss_register_device(struct omap_dss_device *dssdev)
442 {
443         static int dev_num;
444
445         WARN_ON(!dssdev->driver_name);
446
447         reset_device(&dssdev->dev, 1);
448         dssdev->dev.bus = &dss_bus_type;
449         dssdev->dev.parent = &dss_bus;
450         dssdev->dev.release = omap_dss_dev_release;
451         dev_set_name(&dssdev->dev, "display%d", dev_num++);
452         return device_register(&dssdev->dev);
453 }
454
455 static void omap_dss_unregister_device(struct omap_dss_device *dssdev)
456 {
457         device_unregister(&dssdev->dev);
458 }
459
460 /* BUS */
461 static int omap_dss_bus_register(void)
462 {
463         int r;
464
465         r = bus_register(&dss_bus_type);
466         if (r) {
467                 DSSERR("bus register failed\n");
468                 return r;
469         }
470
471         dev_set_name(&dss_bus, "omapdss");
472         r = device_register(&dss_bus);
473         if (r) {
474                 DSSERR("bus driver register failed\n");
475                 bus_unregister(&dss_bus_type);
476                 return r;
477         }
478
479         return 0;
480 }
481
482 /* INIT */
483
484 static int __init omap_dss_register_drivers(void)
485 {
486         int r;
487
488         r = platform_driver_register(&omap_dss_driver);
489         if (r)
490                 return r;
491
492         r = dss_init_platform_driver();
493         if (r) {
494                 DSSERR("Failed to initialize DSS platform driver\n");
495                 goto err_dss;
496         }
497
498         r = dispc_init_platform_driver();
499         if (r) {
500                 DSSERR("Failed to initialize dispc platform driver\n");
501                 goto err_dispc;
502         }
503
504         r = rfbi_init_platform_driver();
505         if (r) {
506                 DSSERR("Failed to initialize rfbi platform driver\n");
507                 goto err_rfbi;
508         }
509
510         r = venc_init_platform_driver();
511         if (r) {
512                 DSSERR("Failed to initialize venc platform driver\n");
513                 goto err_venc;
514         }
515
516         r = dsi_init_platform_driver();
517         if (r) {
518                 DSSERR("Failed to initialize DSI platform driver\n");
519                 goto err_dsi;
520         }
521
522         r = hdmi_init_platform_driver();
523         if (r) {
524                 DSSERR("Failed to initialize hdmi\n");
525                 goto err_hdmi;
526         }
527
528         return 0;
529
530 err_hdmi:
531         dsi_uninit_platform_driver();
532 err_dsi:
533         venc_uninit_platform_driver();
534 err_venc:
535         rfbi_uninit_platform_driver();
536 err_rfbi:
537         dispc_uninit_platform_driver();
538 err_dispc:
539         dss_uninit_platform_driver();
540 err_dss:
541         platform_driver_unregister(&omap_dss_driver);
542
543         return r;
544 }
545
546 static void __exit omap_dss_unregister_drivers(void)
547 {
548         hdmi_uninit_platform_driver();
549         dsi_uninit_platform_driver();
550         venc_uninit_platform_driver();
551         rfbi_uninit_platform_driver();
552         dispc_uninit_platform_driver();
553         dss_uninit_platform_driver();
554
555         platform_driver_unregister(&omap_dss_driver);
556 }
557
558 #ifdef CONFIG_OMAP2_DSS_MODULE
559 static void omap_dss_bus_unregister(void)
560 {
561         device_unregister(&dss_bus);
562
563         bus_unregister(&dss_bus_type);
564 }
565
566 static int __init omap_dss_init(void)
567 {
568         int r;
569
570         r = omap_dss_bus_register();
571         if (r)
572                 return r;
573
574         r = omap_dss_register_drivers();
575         if (r) {
576                 omap_dss_bus_unregister();
577                 return r;
578         }
579
580         return 0;
581 }
582
583 static void __exit omap_dss_exit(void)
584 {
585         if (core.vdds_dsi_reg != NULL) {
586                 regulator_put(core.vdds_dsi_reg);
587                 core.vdds_dsi_reg = NULL;
588         }
589
590         if (core.vdds_sdi_reg != NULL) {
591                 regulator_put(core.vdds_sdi_reg);
592                 core.vdds_sdi_reg = NULL;
593         }
594
595         omap_dss_unregister_drivers();
596
597         omap_dss_bus_unregister();
598 }
599
600 module_init(omap_dss_init);
601 module_exit(omap_dss_exit);
602 #else
603 static int __init omap_dss_init(void)
604 {
605         return omap_dss_bus_register();
606 }
607
608 static int __init omap_dss_init2(void)
609 {
610         return omap_dss_register_drivers();
611 }
612
613 core_initcall(omap_dss_init);
614 device_initcall(omap_dss_init2);
615 #endif
616
617 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
618 MODULE_DESCRIPTION("OMAP2/3 Display Subsystem");
619 MODULE_LICENSE("GPL v2");
620