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