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