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         .remove         = omap_dss_remove,
266         .shutdown       = omap_dss_shutdown,
267         .driver         = {
268                 .name   = "omapdss",
269                 .owner  = THIS_MODULE,
270         },
271 };
272
273 /* BUS */
274 static int dss_bus_match(struct device *dev, struct device_driver *driver)
275 {
276         struct omap_dss_device *dssdev = to_dss_device(dev);
277
278         DSSDBG("bus_match. dev %s/%s, drv %s\n",
279                         dev_name(dev), dssdev->driver_name, driver->name);
280
281         return strcmp(dssdev->driver_name, driver->name) == 0;
282 }
283
284 static ssize_t device_name_show(struct device *dev,
285                 struct device_attribute *attr, char *buf)
286 {
287         struct omap_dss_device *dssdev = to_dss_device(dev);
288         return snprintf(buf, PAGE_SIZE, "%s\n",
289                         dssdev->name ?
290                         dssdev->name : "");
291 }
292
293 static struct device_attribute default_dev_attrs[] = {
294         __ATTR(name, S_IRUGO, device_name_show, NULL),
295         __ATTR_NULL,
296 };
297
298 static ssize_t driver_name_show(struct device_driver *drv, char *buf)
299 {
300         struct omap_dss_driver *dssdrv = to_dss_driver(drv);
301         return snprintf(buf, PAGE_SIZE, "%s\n",
302                         dssdrv->driver.name ?
303                         dssdrv->driver.name : "");
304 }
305 static struct driver_attribute default_drv_attrs[] = {
306         __ATTR(name, S_IRUGO, driver_name_show, NULL),
307         __ATTR_NULL,
308 };
309
310 static struct bus_type dss_bus_type = {
311         .name = "omapdss",
312         .match = dss_bus_match,
313         .dev_attrs = default_dev_attrs,
314         .drv_attrs = default_drv_attrs,
315 };
316
317 static void dss_bus_release(struct device *dev)
318 {
319         DSSDBG("bus_release\n");
320 }
321
322 static struct device dss_bus = {
323         .release = dss_bus_release,
324 };
325
326 struct bus_type *dss_get_bus(void)
327 {
328         return &dss_bus_type;
329 }
330
331 /* DRIVER */
332 static int dss_driver_probe(struct device *dev)
333 {
334         int r;
335         struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
336         struct omap_dss_device *dssdev = to_dss_device(dev);
337         struct omap_dss_board_info *pdata = core.pdev->dev.platform_data;
338         bool force;
339
340         DSSDBG("driver_probe: dev %s/%s, drv %s\n",
341                                 dev_name(dev), dssdev->driver_name,
342                                 dssdrv->driver.name);
343
344         dss_init_device(core.pdev, dssdev);
345
346         force = pdata->default_device == dssdev;
347         dss_recheck_connections(dssdev, force);
348
349         r = dssdrv->probe(dssdev);
350
351         if (r) {
352                 DSSERR("driver probe failed: %d\n", r);
353                 dss_uninit_device(core.pdev, dssdev);
354                 return r;
355         }
356
357         DSSDBG("probe done for device %s\n", dev_name(dev));
358
359         dssdev->driver = dssdrv;
360
361         return 0;
362 }
363
364 static int dss_driver_remove(struct device *dev)
365 {
366         struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
367         struct omap_dss_device *dssdev = to_dss_device(dev);
368
369         DSSDBG("driver_remove: dev %s/%s\n", dev_name(dev),
370                         dssdev->driver_name);
371
372         dssdrv->remove(dssdev);
373
374         dss_uninit_device(core.pdev, dssdev);
375
376         dssdev->driver = NULL;
377
378         return 0;
379 }
380
381 int omap_dss_register_driver(struct omap_dss_driver *dssdriver)
382 {
383         dssdriver->driver.bus = &dss_bus_type;
384         dssdriver->driver.probe = dss_driver_probe;
385         dssdriver->driver.remove = dss_driver_remove;
386
387         if (dssdriver->get_resolution == NULL)
388                 dssdriver->get_resolution = omapdss_default_get_resolution;
389         if (dssdriver->get_recommended_bpp == NULL)
390                 dssdriver->get_recommended_bpp =
391                         omapdss_default_get_recommended_bpp;
392         if (dssdriver->get_timings == NULL)
393                 dssdriver->get_timings = omapdss_default_get_timings;
394
395         return driver_register(&dssdriver->driver);
396 }
397 EXPORT_SYMBOL(omap_dss_register_driver);
398
399 void omap_dss_unregister_driver(struct omap_dss_driver *dssdriver)
400 {
401         driver_unregister(&dssdriver->driver);
402 }
403 EXPORT_SYMBOL(omap_dss_unregister_driver);
404
405 /* DEVICE */
406 static void reset_device(struct device *dev, int check)
407 {
408         u8 *dev_p = (u8 *)dev;
409         u8 *dev_end = dev_p + sizeof(*dev);
410         void *saved_pdata;
411
412         saved_pdata = dev->platform_data;
413         if (check) {
414                 /*
415                  * Check if there is any other setting than platform_data
416                  * in struct device; warn that these will be reset by our
417                  * init.
418                  */
419                 dev->platform_data = NULL;
420                 while (dev_p < dev_end) {
421                         if (*dev_p) {
422                                 WARN("%s: struct device fields will be "
423                                                 "discarded\n",
424                                      __func__);
425                                 break;
426                         }
427                         dev_p++;
428                 }
429         }
430         memset(dev, 0, sizeof(*dev));
431         dev->platform_data = saved_pdata;
432 }
433
434
435 static void omap_dss_dev_release(struct device *dev)
436 {
437         reset_device(dev, 0);
438 }
439
440 static int omap_dss_register_device(struct omap_dss_device *dssdev)
441 {
442         static int dev_num;
443
444         WARN_ON(!dssdev->driver_name);
445
446         reset_device(&dssdev->dev, 1);
447         dssdev->dev.bus = &dss_bus_type;
448         dssdev->dev.parent = &dss_bus;
449         dssdev->dev.release = omap_dss_dev_release;
450         dev_set_name(&dssdev->dev, "display%d", dev_num++);
451         return device_register(&dssdev->dev);
452 }
453
454 static void omap_dss_unregister_device(struct omap_dss_device *dssdev)
455 {
456         device_unregister(&dssdev->dev);
457 }
458
459 /* BUS */
460 static int omap_dss_bus_register(void)
461 {
462         int r;
463
464         r = bus_register(&dss_bus_type);
465         if (r) {
466                 DSSERR("bus register failed\n");
467                 return r;
468         }
469
470         dev_set_name(&dss_bus, "omapdss");
471         r = device_register(&dss_bus);
472         if (r) {
473                 DSSERR("bus driver register failed\n");
474                 bus_unregister(&dss_bus_type);
475                 return r;
476         }
477
478         return 0;
479 }
480
481 /* INIT */
482
483 static int __init omap_dss_register_drivers(void)
484 {
485         int r;
486
487         r = platform_driver_probe(&omap_dss_driver, omap_dss_probe);
488         if (r)
489                 return r;
490
491         r = dss_init_platform_driver();
492         if (r) {
493                 DSSERR("Failed to initialize DSS platform driver\n");
494                 goto err_dss;
495         }
496
497         r = dispc_init_platform_driver();
498         if (r) {
499                 DSSERR("Failed to initialize dispc platform driver\n");
500                 goto err_dispc;
501         }
502
503         r = rfbi_init_platform_driver();
504         if (r) {
505                 DSSERR("Failed to initialize rfbi platform driver\n");
506                 goto err_rfbi;
507         }
508
509         r = venc_init_platform_driver();
510         if (r) {
511                 DSSERR("Failed to initialize venc platform driver\n");
512                 goto err_venc;
513         }
514
515         r = dsi_init_platform_driver();
516         if (r) {
517                 DSSERR("Failed to initialize DSI platform driver\n");
518                 goto err_dsi;
519         }
520
521         r = hdmi_init_platform_driver();
522         if (r) {
523                 DSSERR("Failed to initialize hdmi\n");
524                 goto err_hdmi;
525         }
526
527         return 0;
528
529 err_hdmi:
530         dsi_uninit_platform_driver();
531 err_dsi:
532         venc_uninit_platform_driver();
533 err_venc:
534         rfbi_uninit_platform_driver();
535 err_rfbi:
536         dispc_uninit_platform_driver();
537 err_dispc:
538         dss_uninit_platform_driver();
539 err_dss:
540         platform_driver_unregister(&omap_dss_driver);
541
542         return r;
543 }
544
545 static void __exit omap_dss_unregister_drivers(void)
546 {
547         hdmi_uninit_platform_driver();
548         dsi_uninit_platform_driver();
549         venc_uninit_platform_driver();
550         rfbi_uninit_platform_driver();
551         dispc_uninit_platform_driver();
552         dss_uninit_platform_driver();
553
554         platform_driver_unregister(&omap_dss_driver);
555 }
556
557 #ifdef CONFIG_OMAP2_DSS_MODULE
558 static void omap_dss_bus_unregister(void)
559 {
560         device_unregister(&dss_bus);
561
562         bus_unregister(&dss_bus_type);
563 }
564
565 static int __init omap_dss_init(void)
566 {
567         int r;
568
569         r = omap_dss_bus_register();
570         if (r)
571                 return r;
572
573         r = omap_dss_register_drivers();
574         if (r) {
575                 omap_dss_bus_unregister();
576                 return r;
577         }
578
579         return 0;
580 }
581
582 static void __exit omap_dss_exit(void)
583 {
584         if (core.vdds_dsi_reg != NULL) {
585                 regulator_put(core.vdds_dsi_reg);
586                 core.vdds_dsi_reg = NULL;
587         }
588
589         if (core.vdds_sdi_reg != NULL) {
590                 regulator_put(core.vdds_sdi_reg);
591                 core.vdds_sdi_reg = NULL;
592         }
593
594         omap_dss_unregister_drivers();
595
596         omap_dss_bus_unregister();
597 }
598
599 module_init(omap_dss_init);
600 module_exit(omap_dss_exit);
601 #else
602 static int __init omap_dss_init(void)
603 {
604         return omap_dss_bus_register();
605 }
606
607 static int __init omap_dss_init2(void)
608 {
609         return omap_dss_register_drivers();
610 }
611
612 core_initcall(omap_dss_init);
613 device_initcall(omap_dss_init2);
614 #endif
615
616 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
617 MODULE_DESCRIPTION("OMAP2/3 Display Subsystem");
618 MODULE_LICENSE("GPL v2");
619