OMAPDSS: Fix crash with DT boot
[linux-3.10.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 #include <linux/slab.h>
37
38 #include <video/omapdss.h>
39
40 #include "dss.h"
41 #include "dss_features.h"
42
43 static struct {
44         struct platform_device *pdev;
45
46         struct regulator *vdds_dsi_reg;
47         struct regulator *vdds_sdi_reg;
48
49         const char *default_display_name;
50 } core;
51
52 static char *def_disp_name;
53 module_param_named(def_disp, def_disp_name, charp, 0);
54 MODULE_PARM_DESC(def_disp, "default display name");
55
56 static bool dss_initialized;
57
58 const char *omapdss_get_default_display_name(void)
59 {
60         return core.default_display_name;
61 }
62 EXPORT_SYMBOL(omapdss_get_default_display_name);
63
64 enum omapdss_version omapdss_get_version(void)
65 {
66         struct omap_dss_board_info *pdata = core.pdev->dev.platform_data;
67         return pdata->version;
68 }
69 EXPORT_SYMBOL(omapdss_get_version);
70
71 bool omapdss_is_initialized(void)
72 {
73         return dss_initialized;
74 }
75 EXPORT_SYMBOL(omapdss_is_initialized);
76
77 struct platform_device *dss_get_core_pdev(void)
78 {
79         return core.pdev;
80 }
81
82 /* REGULATORS */
83
84 struct regulator *dss_get_vdds_dsi(void)
85 {
86         struct regulator *reg;
87
88         if (core.vdds_dsi_reg != NULL)
89                 return core.vdds_dsi_reg;
90
91         reg = regulator_get(&core.pdev->dev, "vdds_dsi");
92         if (!IS_ERR(reg))
93                 core.vdds_dsi_reg = reg;
94
95         return reg;
96 }
97
98 struct regulator *dss_get_vdds_sdi(void)
99 {
100         struct regulator *reg;
101
102         if (core.vdds_sdi_reg != NULL)
103                 return core.vdds_sdi_reg;
104
105         reg = regulator_get(&core.pdev->dev, "vdds_sdi");
106         if (!IS_ERR(reg))
107                 core.vdds_sdi_reg = reg;
108
109         return reg;
110 }
111
112 int dss_dsi_enable_pads(int dsi_id, unsigned lane_mask)
113 {
114         struct omap_dss_board_info *board_data = core.pdev->dev.platform_data;
115
116         if (!board_data->dsi_enable_pads)
117                 return -ENOENT;
118
119         return board_data->dsi_enable_pads(dsi_id, lane_mask);
120 }
121
122 void dss_dsi_disable_pads(int dsi_id, unsigned lane_mask)
123 {
124         struct omap_dss_board_info *board_data = core.pdev->dev.platform_data;
125
126         if (!board_data->dsi_disable_pads)
127                 return;
128
129         return board_data->dsi_disable_pads(dsi_id, lane_mask);
130 }
131
132 int dss_set_min_bus_tput(struct device *dev, unsigned long tput)
133 {
134         struct omap_dss_board_info *pdata = core.pdev->dev.platform_data;
135
136         if (pdata->set_min_bus_tput)
137                 return pdata->set_min_bus_tput(dev, tput);
138         else
139                 return 0;
140 }
141
142 #if defined(CONFIG_OMAP2_DSS_DEBUGFS)
143 static int dss_debug_show(struct seq_file *s, void *unused)
144 {
145         void (*func)(struct seq_file *) = s->private;
146         func(s);
147         return 0;
148 }
149
150 static int dss_debug_open(struct inode *inode, struct file *file)
151 {
152         return single_open(file, dss_debug_show, inode->i_private);
153 }
154
155 static const struct file_operations dss_debug_fops = {
156         .open           = dss_debug_open,
157         .read           = seq_read,
158         .llseek         = seq_lseek,
159         .release        = single_release,
160 };
161
162 static struct dentry *dss_debugfs_dir;
163
164 static int dss_initialize_debugfs(void)
165 {
166         dss_debugfs_dir = debugfs_create_dir("omapdss", NULL);
167         if (IS_ERR(dss_debugfs_dir)) {
168                 int err = PTR_ERR(dss_debugfs_dir);
169                 dss_debugfs_dir = NULL;
170                 return err;
171         }
172
173         debugfs_create_file("clk", S_IRUGO, dss_debugfs_dir,
174                         &dss_debug_dump_clocks, &dss_debug_fops);
175
176         return 0;
177 }
178
179 static void dss_uninitialize_debugfs(void)
180 {
181         if (dss_debugfs_dir)
182                 debugfs_remove_recursive(dss_debugfs_dir);
183 }
184
185 int dss_debugfs_create_file(const char *name, void (*write)(struct seq_file *))
186 {
187         struct dentry *d;
188
189         d = debugfs_create_file(name, S_IRUGO, dss_debugfs_dir,
190                         write, &dss_debug_fops);
191
192         if (IS_ERR(d))
193                 return PTR_ERR(d);
194
195         return 0;
196 }
197 #else /* CONFIG_OMAP2_DSS_DEBUGFS */
198 static inline int dss_initialize_debugfs(void)
199 {
200         return 0;
201 }
202 static inline void dss_uninitialize_debugfs(void)
203 {
204 }
205 int dss_debugfs_create_file(const char *name, void (*write)(struct seq_file *))
206 {
207         return 0;
208 }
209 #endif /* CONFIG_OMAP2_DSS_DEBUGFS */
210
211 /* PLATFORM DEVICE */
212 static int omap_dss_pm_notif(struct notifier_block *b, unsigned long v, void *d)
213 {
214         DSSDBG("pm notif %lu\n", v);
215
216         switch (v) {
217         case PM_SUSPEND_PREPARE:
218                 DSSDBG("suspending displays\n");
219                 return dss_suspend_all_devices();
220
221         case PM_POST_SUSPEND:
222                 DSSDBG("resuming displays\n");
223                 return dss_resume_all_devices();
224
225         default:
226                 return 0;
227         }
228 }
229
230 static struct notifier_block omap_dss_pm_notif_block = {
231         .notifier_call = omap_dss_pm_notif,
232 };
233
234 static int __init omap_dss_probe(struct platform_device *pdev)
235 {
236         struct omap_dss_board_info *pdata = pdev->dev.platform_data;
237         int r;
238
239         core.pdev = pdev;
240
241         dss_features_init(omapdss_get_version());
242
243         r = dss_initialize_debugfs();
244         if (r)
245                 goto err_debugfs;
246
247         if (def_disp_name)
248                 core.default_display_name = def_disp_name;
249         else if (pdata->default_device)
250                 core.default_display_name = pdata->default_device->name;
251
252         register_pm_notifier(&omap_dss_pm_notif_block);
253
254         return 0;
255
256 err_debugfs:
257
258         return r;
259 }
260
261 static int omap_dss_remove(struct platform_device *pdev)
262 {
263         unregister_pm_notifier(&omap_dss_pm_notif_block);
264
265         dss_uninitialize_debugfs();
266
267         return 0;
268 }
269
270 static void omap_dss_shutdown(struct platform_device *pdev)
271 {
272         DSSDBG("shutdown\n");
273         dss_disable_all_devices();
274 }
275
276 static struct platform_driver omap_dss_driver = {
277         .remove         = omap_dss_remove,
278         .shutdown       = omap_dss_shutdown,
279         .driver         = {
280                 .name   = "omapdss",
281                 .owner  = THIS_MODULE,
282         },
283 };
284
285 /* BUS */
286 static int dss_bus_match(struct device *dev, struct device_driver *driver)
287 {
288         struct omap_dss_device *dssdev = to_dss_device(dev);
289
290         DSSDBG("bus_match. dev %s/%s, drv %s\n",
291                         dev_name(dev), dssdev->driver_name, driver->name);
292
293         return strcmp(dssdev->driver_name, driver->name) == 0;
294 }
295
296 static ssize_t device_name_show(struct device *dev,
297                 struct device_attribute *attr, char *buf)
298 {
299         struct omap_dss_device *dssdev = to_dss_device(dev);
300         return snprintf(buf, PAGE_SIZE, "%s\n",
301                         dssdev->name ?
302                         dssdev->name : "");
303 }
304
305 static struct device_attribute default_dev_attrs[] = {
306         __ATTR(name, S_IRUGO, device_name_show, NULL),
307         __ATTR_NULL,
308 };
309
310 static ssize_t driver_name_show(struct device_driver *drv, char *buf)
311 {
312         struct omap_dss_driver *dssdrv = to_dss_driver(drv);
313         return snprintf(buf, PAGE_SIZE, "%s\n",
314                         dssdrv->driver.name ?
315                         dssdrv->driver.name : "");
316 }
317 static struct driver_attribute default_drv_attrs[] = {
318         __ATTR(name, S_IRUGO, driver_name_show, NULL),
319         __ATTR_NULL,
320 };
321
322 static struct bus_type dss_bus_type = {
323         .name = "omapdss",
324         .match = dss_bus_match,
325         .dev_attrs = default_dev_attrs,
326         .drv_attrs = default_drv_attrs,
327 };
328
329 static void dss_bus_release(struct device *dev)
330 {
331         DSSDBG("bus_release\n");
332 }
333
334 static struct device dss_bus = {
335         .release = dss_bus_release,
336 };
337
338 struct bus_type *dss_get_bus(void)
339 {
340         return &dss_bus_type;
341 }
342
343 /* DRIVER */
344 static int dss_driver_probe(struct device *dev)
345 {
346         int r;
347         struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
348         struct omap_dss_device *dssdev = to_dss_device(dev);
349
350         DSSDBG("driver_probe: dev %s/%s, drv %s\n",
351                                 dev_name(dev), dssdev->driver_name,
352                                 dssdrv->driver.name);
353
354         r = dssdrv->probe(dssdev);
355
356         if (r) {
357                 DSSERR("driver probe failed: %d\n", r);
358                 return r;
359         }
360
361         DSSDBG("probe done for device %s\n", dev_name(dev));
362
363         dssdev->driver = dssdrv;
364
365         return 0;
366 }
367
368 static int dss_driver_remove(struct device *dev)
369 {
370         struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
371         struct omap_dss_device *dssdev = to_dss_device(dev);
372
373         DSSDBG("driver_remove: dev %s/%s\n", dev_name(dev),
374                         dssdev->driver_name);
375
376         dssdrv->remove(dssdev);
377
378         dssdev->driver = NULL;
379
380         return 0;
381 }
382
383 int omap_dss_register_driver(struct omap_dss_driver *dssdriver)
384 {
385         dssdriver->driver.bus = &dss_bus_type;
386         dssdriver->driver.probe = dss_driver_probe;
387         dssdriver->driver.remove = dss_driver_remove;
388
389         if (dssdriver->get_resolution == NULL)
390                 dssdriver->get_resolution = omapdss_default_get_resolution;
391         if (dssdriver->get_recommended_bpp == NULL)
392                 dssdriver->get_recommended_bpp =
393                         omapdss_default_get_recommended_bpp;
394         if (dssdriver->get_timings == NULL)
395                 dssdriver->get_timings = omapdss_default_get_timings;
396
397         return driver_register(&dssdriver->driver);
398 }
399 EXPORT_SYMBOL(omap_dss_register_driver);
400
401 void omap_dss_unregister_driver(struct omap_dss_driver *dssdriver)
402 {
403         driver_unregister(&dssdriver->driver);
404 }
405 EXPORT_SYMBOL(omap_dss_unregister_driver);
406
407 /* DEVICE */
408
409 static void omap_dss_dev_release(struct device *dev)
410 {
411         struct omap_dss_device *dssdev = to_dss_device(dev);
412         kfree(dssdev);
413 }
414
415 static int disp_num_counter;
416
417 struct omap_dss_device *dss_alloc_and_init_device(struct device *parent)
418 {
419         struct omap_dss_device *dssdev;
420
421         dssdev = kzalloc(sizeof(*dssdev), GFP_KERNEL);
422         if (!dssdev)
423                 return NULL;
424
425         dssdev->dev.bus = &dss_bus_type;
426         dssdev->dev.parent = parent;
427         dssdev->dev.release = omap_dss_dev_release;
428         dev_set_name(&dssdev->dev, "display%d", disp_num_counter++);
429
430         device_initialize(&dssdev->dev);
431
432         return dssdev;
433 }
434
435 int dss_add_device(struct omap_dss_device *dssdev)
436 {
437         return device_add(&dssdev->dev);
438 }
439
440 void dss_put_device(struct omap_dss_device *dssdev)
441 {
442         put_device(&dssdev->dev);
443 }
444
445 void dss_unregister_device(struct omap_dss_device *dssdev)
446 {
447         device_unregister(&dssdev->dev);
448 }
449
450 static int dss_unregister_dss_dev(struct device *dev, void *data)
451 {
452         struct omap_dss_device *dssdev = to_dss_device(dev);
453         dss_unregister_device(dssdev);
454         return 0;
455 }
456
457 void dss_unregister_child_devices(struct device *parent)
458 {
459         device_for_each_child(parent, NULL, dss_unregister_dss_dev);
460 }
461
462 void dss_copy_device_pdata(struct omap_dss_device *dst,
463                 const struct omap_dss_device *src)
464 {
465         u8 *d = (u8 *)dst;
466         u8 *s = (u8 *)src;
467         size_t dsize = sizeof(struct device);
468
469         memcpy(d + dsize, s + dsize, sizeof(struct omap_dss_device) - dsize);
470 }
471
472 /* BUS */
473 static int __init omap_dss_bus_register(void)
474 {
475         int r;
476
477         r = bus_register(&dss_bus_type);
478         if (r) {
479                 DSSERR("bus register failed\n");
480                 return r;
481         }
482
483         dev_set_name(&dss_bus, "omapdss");
484         r = device_register(&dss_bus);
485         if (r) {
486                 DSSERR("bus driver register failed\n");
487                 bus_unregister(&dss_bus_type);
488                 return r;
489         }
490
491         return 0;
492 }
493
494 /* INIT */
495 static int (*dss_output_drv_reg_funcs[])(void) __initdata = {
496 #ifdef CONFIG_OMAP2_DSS_DSI
497         dsi_init_platform_driver,
498 #endif
499 #ifdef CONFIG_OMAP2_DSS_DPI
500         dpi_init_platform_driver,
501 #endif
502 #ifdef CONFIG_OMAP2_DSS_SDI
503         sdi_init_platform_driver,
504 #endif
505 #ifdef CONFIG_OMAP2_DSS_RFBI
506         rfbi_init_platform_driver,
507 #endif
508 #ifdef CONFIG_OMAP2_DSS_VENC
509         venc_init_platform_driver,
510 #endif
511 #ifdef CONFIG_OMAP4_DSS_HDMI
512         hdmi_init_platform_driver,
513 #endif
514 };
515
516 static void (*dss_output_drv_unreg_funcs[])(void) __exitdata = {
517 #ifdef CONFIG_OMAP2_DSS_DSI
518         dsi_uninit_platform_driver,
519 #endif
520 #ifdef CONFIG_OMAP2_DSS_DPI
521         dpi_uninit_platform_driver,
522 #endif
523 #ifdef CONFIG_OMAP2_DSS_SDI
524         sdi_uninit_platform_driver,
525 #endif
526 #ifdef CONFIG_OMAP2_DSS_RFBI
527         rfbi_uninit_platform_driver,
528 #endif
529 #ifdef CONFIG_OMAP2_DSS_VENC
530         venc_uninit_platform_driver,
531 #endif
532 #ifdef CONFIG_OMAP4_DSS_HDMI
533         hdmi_uninit_platform_driver,
534 #endif
535 };
536
537 static bool dss_output_drv_loaded[ARRAY_SIZE(dss_output_drv_reg_funcs)];
538
539 static int __init omap_dss_register_drivers(void)
540 {
541         int r;
542         int i;
543
544         r = platform_driver_probe(&omap_dss_driver, omap_dss_probe);
545         if (r)
546                 return r;
547
548         r = dss_init_platform_driver();
549         if (r) {
550                 DSSERR("Failed to initialize DSS platform driver\n");
551                 goto err_dss;
552         }
553
554         r = dispc_init_platform_driver();
555         if (r) {
556                 DSSERR("Failed to initialize dispc platform driver\n");
557                 goto err_dispc;
558         }
559
560         /*
561          * It's ok if the output-driver register fails. It happens, for example,
562          * when there is no output-device (e.g. SDI for OMAP4).
563          */
564         for (i = 0; i < ARRAY_SIZE(dss_output_drv_reg_funcs); ++i) {
565                 r = dss_output_drv_reg_funcs[i]();
566                 if (r == 0)
567                         dss_output_drv_loaded[i] = true;
568         }
569
570         return 0;
571
572 err_dispc:
573         dss_uninit_platform_driver();
574 err_dss:
575         platform_driver_unregister(&omap_dss_driver);
576
577         return r;
578 }
579
580 static void __exit omap_dss_unregister_drivers(void)
581 {
582         int i;
583
584         for (i = 0; i < ARRAY_SIZE(dss_output_drv_unreg_funcs); ++i) {
585                 if (dss_output_drv_loaded[i])
586                         dss_output_drv_unreg_funcs[i]();
587         }
588
589         dispc_uninit_platform_driver();
590         dss_uninit_platform_driver();
591
592         platform_driver_unregister(&omap_dss_driver);
593 }
594
595 #ifdef CONFIG_OMAP2_DSS_MODULE
596 static void omap_dss_bus_unregister(void)
597 {
598         device_unregister(&dss_bus);
599
600         bus_unregister(&dss_bus_type);
601 }
602
603 static int __init omap_dss_init(void)
604 {
605         int r;
606
607         r = omap_dss_bus_register();
608         if (r)
609                 return r;
610
611         r = omap_dss_register_drivers();
612         if (r) {
613                 omap_dss_bus_unregister();
614                 return r;
615         }
616
617         dss_initialized = true;
618
619         return 0;
620 }
621
622 static void __exit omap_dss_exit(void)
623 {
624         if (core.vdds_dsi_reg != NULL) {
625                 regulator_put(core.vdds_dsi_reg);
626                 core.vdds_dsi_reg = NULL;
627         }
628
629         if (core.vdds_sdi_reg != NULL) {
630                 regulator_put(core.vdds_sdi_reg);
631                 core.vdds_sdi_reg = NULL;
632         }
633
634         omap_dss_unregister_drivers();
635
636         omap_dss_bus_unregister();
637 }
638
639 module_init(omap_dss_init);
640 module_exit(omap_dss_exit);
641 #else
642 static int __init omap_dss_init(void)
643 {
644         return omap_dss_bus_register();
645 }
646
647 static int __init omap_dss_init2(void)
648 {
649         int r;
650
651         r = omap_dss_register_drivers();
652         if (r)
653                 return r;
654
655         dss_initialized = true;
656
657         return 0;
658 }
659
660 core_initcall(omap_dss_init);
661 device_initcall(omap_dss_init2);
662 #endif
663
664 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
665 MODULE_DESCRIPTION("OMAP2/3 Display Subsystem");
666 MODULE_LICENSE("GPL v2");
667