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