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