OMAPDSS: Remove passive matrix LCD support (part 2)
[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 #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         {
211         case PM_SUSPEND_PREPARE:
212                 DSSDBG("suspending displays\n");
213                 return dss_suspend_all_devices();
214
215         case PM_POST_SUSPEND:
216                 DSSDBG("resuming displays\n");
217                 return dss_resume_all_devices();
218
219         default:
220                 return 0;
221         }
222 }
223
224 static struct notifier_block omap_dss_pm_notif_block =
225 {
226         .notifier_call = omap_dss_pm_notif,
227 };
228
229 static int __init omap_dss_probe(struct platform_device *pdev)
230 {
231         struct omap_dss_board_info *pdata = pdev->dev.platform_data;
232         int r;
233
234         core.pdev = pdev;
235
236         dss_features_init();
237
238         dss_apply_init();
239
240         dss_init_overlay_managers(pdev);
241         dss_init_overlays(pdev);
242
243         r = dss_initialize_debugfs();
244         if (r)
245                 goto err_debugfs;
246
247         if (def_disp_name)
248                 core.default_display_name = def_disp_name;
249         else if (pdata->default_device)
250                 core.default_display_name = pdata->default_device->name;
251
252         register_pm_notifier(&omap_dss_pm_notif_block);
253
254         return 0;
255
256 err_debugfs:
257
258         return r;
259 }
260
261 static int omap_dss_remove(struct platform_device *pdev)
262 {
263         unregister_pm_notifier(&omap_dss_pm_notif_block);
264
265         dss_uninitialize_debugfs();
266
267         dss_uninit_overlays(pdev);
268         dss_uninit_overlay_managers(pdev);
269
270         return 0;
271 }
272
273 static void omap_dss_shutdown(struct platform_device *pdev)
274 {
275         DSSDBG("shutdown\n");
276         dss_disable_all_devices();
277 }
278
279 static struct platform_driver omap_dss_driver = {
280         .remove         = omap_dss_remove,
281         .shutdown       = omap_dss_shutdown,
282         .driver         = {
283                 .name   = "omapdss",
284                 .owner  = THIS_MODULE,
285         },
286 };
287
288 /* BUS */
289 static int dss_bus_match(struct device *dev, struct device_driver *driver)
290 {
291         struct omap_dss_device *dssdev = to_dss_device(dev);
292
293         DSSDBG("bus_match. dev %s/%s, drv %s\n",
294                         dev_name(dev), dssdev->driver_name, driver->name);
295
296         return strcmp(dssdev->driver_name, driver->name) == 0;
297 }
298
299 static ssize_t device_name_show(struct device *dev,
300                 struct device_attribute *attr, char *buf)
301 {
302         struct omap_dss_device *dssdev = to_dss_device(dev);
303         return snprintf(buf, PAGE_SIZE, "%s\n",
304                         dssdev->name ?
305                         dssdev->name : "");
306 }
307
308 static struct device_attribute default_dev_attrs[] = {
309         __ATTR(name, S_IRUGO, device_name_show, NULL),
310         __ATTR_NULL,
311 };
312
313 static ssize_t driver_name_show(struct device_driver *drv, char *buf)
314 {
315         struct omap_dss_driver *dssdrv = to_dss_driver(drv);
316         return snprintf(buf, PAGE_SIZE, "%s\n",
317                         dssdrv->driver.name ?
318                         dssdrv->driver.name : "");
319 }
320 static struct driver_attribute default_drv_attrs[] = {
321         __ATTR(name, S_IRUGO, driver_name_show, NULL),
322         __ATTR_NULL,
323 };
324
325 static struct bus_type dss_bus_type = {
326         .name = "omapdss",
327         .match = dss_bus_match,
328         .dev_attrs = default_dev_attrs,
329         .drv_attrs = default_drv_attrs,
330 };
331
332 static void dss_bus_release(struct device *dev)
333 {
334         DSSDBG("bus_release\n");
335 }
336
337 static struct device dss_bus = {
338         .release = dss_bus_release,
339 };
340
341 struct bus_type *dss_get_bus(void)
342 {
343         return &dss_bus_type;
344 }
345
346 /* DRIVER */
347 static int dss_driver_probe(struct device *dev)
348 {
349         int r;
350         struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
351         struct omap_dss_device *dssdev = to_dss_device(dev);
352         bool force;
353
354         DSSDBG("driver_probe: dev %s/%s, drv %s\n",
355                                 dev_name(dev), dssdev->driver_name,
356                                 dssdrv->driver.name);
357
358         dss_init_device(core.pdev, dssdev);
359
360         force = core.default_display_name &&
361                 strcmp(core.default_display_name, dssdev->name) == 0;
362         dss_recheck_connections(dssdev, force);
363
364         r = dssdrv->probe(dssdev);
365
366         if (r) {
367                 DSSERR("driver probe failed: %d\n", r);
368                 dss_uninit_device(core.pdev, dssdev);
369                 return r;
370         }
371
372         DSSDBG("probe done for device %s\n", dev_name(dev));
373
374         dssdev->driver = dssdrv;
375
376         return 0;
377 }
378
379 static int dss_driver_remove(struct device *dev)
380 {
381         struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
382         struct omap_dss_device *dssdev = to_dss_device(dev);
383
384         DSSDBG("driver_remove: dev %s/%s\n", dev_name(dev),
385                         dssdev->driver_name);
386
387         dssdrv->remove(dssdev);
388
389         dss_uninit_device(core.pdev, dssdev);
390
391         dssdev->driver = NULL;
392
393         return 0;
394 }
395
396 int omap_dss_register_driver(struct omap_dss_driver *dssdriver)
397 {
398         dssdriver->driver.bus = &dss_bus_type;
399         dssdriver->driver.probe = dss_driver_probe;
400         dssdriver->driver.remove = dss_driver_remove;
401
402         if (dssdriver->get_resolution == NULL)
403                 dssdriver->get_resolution = omapdss_default_get_resolution;
404         if (dssdriver->get_recommended_bpp == NULL)
405                 dssdriver->get_recommended_bpp =
406                         omapdss_default_get_recommended_bpp;
407         if (dssdriver->get_timings == NULL)
408                 dssdriver->get_timings = omapdss_default_get_timings;
409
410         return driver_register(&dssdriver->driver);
411 }
412 EXPORT_SYMBOL(omap_dss_register_driver);
413
414 void omap_dss_unregister_driver(struct omap_dss_driver *dssdriver)
415 {
416         driver_unregister(&dssdriver->driver);
417 }
418 EXPORT_SYMBOL(omap_dss_unregister_driver);
419
420 /* DEVICE */
421 static void reset_device(struct device *dev, int check)
422 {
423         u8 *dev_p = (u8 *)dev;
424         u8 *dev_end = dev_p + sizeof(*dev);
425         void *saved_pdata;
426
427         saved_pdata = dev->platform_data;
428         if (check) {
429                 /*
430                  * Check if there is any other setting than platform_data
431                  * in struct device; warn that these will be reset by our
432                  * init.
433                  */
434                 dev->platform_data = NULL;
435                 while (dev_p < dev_end) {
436                         if (*dev_p) {
437                                 WARN("%s: struct device fields will be "
438                                                 "discarded\n",
439                                      __func__);
440                                 break;
441                         }
442                         dev_p++;
443                 }
444         }
445         memset(dev, 0, sizeof(*dev));
446         dev->platform_data = saved_pdata;
447 }
448
449
450 static void omap_dss_dev_release(struct device *dev)
451 {
452         reset_device(dev, 0);
453 }
454
455 int omap_dss_register_device(struct omap_dss_device *dssdev,
456                 struct device *parent, int disp_num)
457 {
458         WARN_ON(!dssdev->driver_name);
459
460         reset_device(&dssdev->dev, 1);
461         dssdev->dev.bus = &dss_bus_type;
462         dssdev->dev.parent = parent;
463         dssdev->dev.release = omap_dss_dev_release;
464         dev_set_name(&dssdev->dev, "display%d", disp_num);
465         return device_register(&dssdev->dev);
466 }
467
468 void omap_dss_unregister_device(struct omap_dss_device *dssdev)
469 {
470         device_unregister(&dssdev->dev);
471 }
472
473 static int dss_unregister_dss_dev(struct device *dev, void *data)
474 {
475         struct omap_dss_device *dssdev = to_dss_device(dev);
476         omap_dss_unregister_device(dssdev);
477         return 0;
478 }
479
480 void omap_dss_unregister_child_devices(struct device *parent)
481 {
482         device_for_each_child(parent, NULL, dss_unregister_dss_dev);
483 }
484
485 /* BUS */
486 static int __init omap_dss_bus_register(void)
487 {
488         int r;
489
490         r = bus_register(&dss_bus_type);
491         if (r) {
492                 DSSERR("bus register failed\n");
493                 return r;
494         }
495
496         dev_set_name(&dss_bus, "omapdss");
497         r = device_register(&dss_bus);
498         if (r) {
499                 DSSERR("bus driver register failed\n");
500                 bus_unregister(&dss_bus_type);
501                 return r;
502         }
503
504         return 0;
505 }
506
507 /* INIT */
508 static int (*dss_output_drv_reg_funcs[])(void) __initdata = {
509 #ifdef CONFIG_OMAP2_DSS_DPI
510         dpi_init_platform_driver,
511 #endif
512 #ifdef CONFIG_OMAP2_DSS_SDI
513         sdi_init_platform_driver,
514 #endif
515 #ifdef CONFIG_OMAP2_DSS_RFBI
516         rfbi_init_platform_driver,
517 #endif
518 #ifdef CONFIG_OMAP2_DSS_VENC
519         venc_init_platform_driver,
520 #endif
521 #ifdef CONFIG_OMAP2_DSS_DSI
522         dsi_init_platform_driver,
523 #endif
524 #ifdef CONFIG_OMAP4_DSS_HDMI
525         hdmi_init_platform_driver,
526 #endif
527 };
528
529 static void (*dss_output_drv_unreg_funcs[])(void) __exitdata = {
530 #ifdef CONFIG_OMAP2_DSS_DPI
531         dpi_uninit_platform_driver,
532 #endif
533 #ifdef CONFIG_OMAP2_DSS_SDI
534         sdi_uninit_platform_driver,
535 #endif
536 #ifdef CONFIG_OMAP2_DSS_RFBI
537         rfbi_uninit_platform_driver,
538 #endif
539 #ifdef CONFIG_OMAP2_DSS_VENC
540         venc_uninit_platform_driver,
541 #endif
542 #ifdef CONFIG_OMAP2_DSS_DSI
543         dsi_uninit_platform_driver,
544 #endif
545 #ifdef CONFIG_OMAP4_DSS_HDMI
546         hdmi_uninit_platform_driver,
547 #endif
548 };
549
550 static bool dss_output_drv_loaded[ARRAY_SIZE(dss_output_drv_reg_funcs)];
551
552 static int __init omap_dss_register_drivers(void)
553 {
554         int r;
555         int i;
556
557         r = platform_driver_probe(&omap_dss_driver, omap_dss_probe);
558         if (r)
559                 return r;
560
561         r = dss_init_platform_driver();
562         if (r) {
563                 DSSERR("Failed to initialize DSS platform driver\n");
564                 goto err_dss;
565         }
566
567         r = dispc_init_platform_driver();
568         if (r) {
569                 DSSERR("Failed to initialize dispc platform driver\n");
570                 goto err_dispc;
571         }
572
573         /*
574          * It's ok if the output-driver register fails. It happens, for example,
575          * when there is no output-device (e.g. SDI for OMAP4).
576          */
577         for (i = 0; i < ARRAY_SIZE(dss_output_drv_reg_funcs); ++i) {
578                 r = dss_output_drv_reg_funcs[i]();
579                 if (r == 0)
580                         dss_output_drv_loaded[i] = true;
581         }
582
583         return 0;
584
585 err_dispc:
586         dss_uninit_platform_driver();
587 err_dss:
588         platform_driver_unregister(&omap_dss_driver);
589
590         return r;
591 }
592
593 static void __exit omap_dss_unregister_drivers(void)
594 {
595         int i;
596
597         for (i = 0; i < ARRAY_SIZE(dss_output_drv_unreg_funcs); ++i) {
598                 if (dss_output_drv_loaded[i])
599                         dss_output_drv_unreg_funcs[i]();
600         }
601
602         dispc_uninit_platform_driver();
603         dss_uninit_platform_driver();
604
605         platform_driver_unregister(&omap_dss_driver);
606 }
607
608 #ifdef CONFIG_OMAP2_DSS_MODULE
609 static void omap_dss_bus_unregister(void)
610 {
611         device_unregister(&dss_bus);
612
613         bus_unregister(&dss_bus_type);
614 }
615
616 static int __init omap_dss_init(void)
617 {
618         int r;
619
620         r = omap_dss_bus_register();
621         if (r)
622                 return r;
623
624         r = omap_dss_register_drivers();
625         if (r) {
626                 omap_dss_bus_unregister();
627                 return r;
628         }
629
630         return 0;
631 }
632
633 static void __exit omap_dss_exit(void)
634 {
635         if (core.vdds_dsi_reg != NULL) {
636                 regulator_put(core.vdds_dsi_reg);
637                 core.vdds_dsi_reg = NULL;
638         }
639
640         if (core.vdds_sdi_reg != NULL) {
641                 regulator_put(core.vdds_sdi_reg);
642                 core.vdds_sdi_reg = NULL;
643         }
644
645         omap_dss_unregister_drivers();
646
647         omap_dss_bus_unregister();
648 }
649
650 module_init(omap_dss_init);
651 module_exit(omap_dss_exit);
652 #else
653 static int __init omap_dss_init(void)
654 {
655         return omap_dss_bus_register();
656 }
657
658 static int __init omap_dss_init2(void)
659 {
660         return omap_dss_register_drivers();
661 }
662
663 core_initcall(omap_dss_init);
664 device_initcall(omap_dss_init2);
665 #endif
666
667 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
668 MODULE_DESCRIPTION("OMAP2/3 Display Subsystem");
669 MODULE_LICENSE("GPL v2");
670