Merge branch 'kbuild' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[GitHub/mt8127/android_kernel_alcatel_ttab.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 #include <linux/slab.h>
37
38 #include <video/omapdss.h>
39
40 #include "dss.h"
41 #include "dss_features.h"
42
43 static struct {
44 struct platform_device *pdev;
45
46 struct regulator *vdds_dsi_reg;
47 struct regulator *vdds_sdi_reg;
48
49 const char *default_display_name;
50 } core;
51
52 static char *def_disp_name;
53 module_param_named(def_disp, def_disp_name, charp, 0);
54 MODULE_PARM_DESC(def_disp, "default display name");
55
56 const char *omapdss_get_default_display_name(void)
57 {
58 return core.default_display_name;
59 }
60 EXPORT_SYMBOL(omapdss_get_default_display_name);
61
62 enum omapdss_version omapdss_get_version(void)
63 {
64 struct omap_dss_board_info *pdata = core.pdev->dev.platform_data;
65 return pdata->version;
66 }
67 EXPORT_SYMBOL(omapdss_get_version);
68
69 struct platform_device *dss_get_core_pdev(void)
70 {
71 return core.pdev;
72 }
73
74 /* REGULATORS */
75
76 struct regulator *dss_get_vdds_dsi(void)
77 {
78 struct regulator *reg;
79
80 if (core.vdds_dsi_reg != NULL)
81 return core.vdds_dsi_reg;
82
83 reg = regulator_get(&core.pdev->dev, "vdds_dsi");
84 if (!IS_ERR(reg))
85 core.vdds_dsi_reg = reg;
86
87 return reg;
88 }
89
90 struct regulator *dss_get_vdds_sdi(void)
91 {
92 struct regulator *reg;
93
94 if (core.vdds_sdi_reg != NULL)
95 return core.vdds_sdi_reg;
96
97 reg = regulator_get(&core.pdev->dev, "vdds_sdi");
98 if (!IS_ERR(reg))
99 core.vdds_sdi_reg = reg;
100
101 return reg;
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_disable_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_OMAP2_DSS_DEBUGFS)
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 return PTR_RET(d);
185 }
186 #else /* CONFIG_OMAP2_DSS_DEBUGFS */
187 static inline int dss_initialize_debugfs(void)
188 {
189 return 0;
190 }
191 static inline void dss_uninitialize_debugfs(void)
192 {
193 }
194 int dss_debugfs_create_file(const char *name, void (*write)(struct seq_file *))
195 {
196 return 0;
197 }
198 #endif /* CONFIG_OMAP2_DSS_DEBUGFS */
199
200 /* PLATFORM DEVICE */
201 static int omap_dss_pm_notif(struct notifier_block *b, unsigned long v, void *d)
202 {
203 DSSDBG("pm notif %lu\n", v);
204
205 switch (v) {
206 case PM_SUSPEND_PREPARE:
207 DSSDBG("suspending displays\n");
208 return dss_suspend_all_devices();
209
210 case PM_POST_SUSPEND:
211 DSSDBG("resuming displays\n");
212 return dss_resume_all_devices();
213
214 default:
215 return 0;
216 }
217 }
218
219 static struct notifier_block omap_dss_pm_notif_block = {
220 .notifier_call = omap_dss_pm_notif,
221 };
222
223 static int __init omap_dss_probe(struct platform_device *pdev)
224 {
225 struct omap_dss_board_info *pdata = pdev->dev.platform_data;
226 int r;
227
228 core.pdev = pdev;
229
230 dss_features_init(omapdss_get_version());
231
232 r = dss_initialize_debugfs();
233 if (r)
234 goto err_debugfs;
235
236 if (def_disp_name)
237 core.default_display_name = def_disp_name;
238 else if (pdata->default_device)
239 core.default_display_name = pdata->default_device->name;
240
241 register_pm_notifier(&omap_dss_pm_notif_block);
242
243 return 0;
244
245 err_debugfs:
246
247 return r;
248 }
249
250 static int omap_dss_remove(struct platform_device *pdev)
251 {
252 unregister_pm_notifier(&omap_dss_pm_notif_block);
253
254 dss_uninitialize_debugfs();
255
256 return 0;
257 }
258
259 static void omap_dss_shutdown(struct platform_device *pdev)
260 {
261 DSSDBG("shutdown\n");
262 dss_disable_all_devices();
263 }
264
265 static struct platform_driver omap_dss_driver = {
266 .remove = omap_dss_remove,
267 .shutdown = omap_dss_shutdown,
268 .driver = {
269 .name = "omapdss",
270 .owner = THIS_MODULE,
271 },
272 };
273
274 /* BUS */
275 static int dss_bus_match(struct device *dev, struct device_driver *driver)
276 {
277 struct omap_dss_device *dssdev = to_dss_device(dev);
278
279 DSSDBG("bus_match. dev %s/%s, drv %s\n",
280 dev_name(dev), dssdev->driver_name, driver->name);
281
282 return strcmp(dssdev->driver_name, driver->name) == 0;
283 }
284
285 static ssize_t device_name_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287 {
288 struct omap_dss_device *dssdev = to_dss_device(dev);
289 return snprintf(buf, PAGE_SIZE, "%s\n",
290 dssdev->name ?
291 dssdev->name : "");
292 }
293
294 static struct device_attribute default_dev_attrs[] = {
295 __ATTR(name, S_IRUGO, device_name_show, NULL),
296 __ATTR_NULL,
297 };
298
299 static ssize_t driver_name_show(struct device_driver *drv, char *buf)
300 {
301 struct omap_dss_driver *dssdrv = to_dss_driver(drv);
302 return snprintf(buf, PAGE_SIZE, "%s\n",
303 dssdrv->driver.name ?
304 dssdrv->driver.name : "");
305 }
306 static struct driver_attribute default_drv_attrs[] = {
307 __ATTR(name, S_IRUGO, driver_name_show, NULL),
308 __ATTR_NULL,
309 };
310
311 static struct bus_type dss_bus_type = {
312 .name = "omapdss",
313 .match = dss_bus_match,
314 .dev_attrs = default_dev_attrs,
315 .drv_attrs = default_drv_attrs,
316 };
317
318 static void dss_bus_release(struct device *dev)
319 {
320 DSSDBG("bus_release\n");
321 }
322
323 static struct device dss_bus = {
324 .release = dss_bus_release,
325 };
326
327 struct bus_type *dss_get_bus(void)
328 {
329 return &dss_bus_type;
330 }
331
332 /* DRIVER */
333 static int dss_driver_probe(struct device *dev)
334 {
335 int r;
336 struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
337 struct omap_dss_device *dssdev = to_dss_device(dev);
338
339 DSSDBG("driver_probe: dev %s/%s, drv %s\n",
340 dev_name(dev), dssdev->driver_name,
341 dssdrv->driver.name);
342
343 r = dssdrv->probe(dssdev);
344
345 if (r) {
346 DSSERR("driver probe failed: %d\n", r);
347 return r;
348 }
349
350 DSSDBG("probe done for device %s\n", dev_name(dev));
351
352 dssdev->driver = dssdrv;
353
354 return 0;
355 }
356
357 static int dss_driver_remove(struct device *dev)
358 {
359 struct omap_dss_driver *dssdrv = to_dss_driver(dev->driver);
360 struct omap_dss_device *dssdev = to_dss_device(dev);
361
362 DSSDBG("driver_remove: dev %s/%s\n", dev_name(dev),
363 dssdev->driver_name);
364
365 dssdrv->remove(dssdev);
366
367 dssdev->driver = NULL;
368
369 return 0;
370 }
371
372 int omap_dss_register_driver(struct omap_dss_driver *dssdriver)
373 {
374 dssdriver->driver.bus = &dss_bus_type;
375 dssdriver->driver.probe = dss_driver_probe;
376 dssdriver->driver.remove = dss_driver_remove;
377
378 if (dssdriver->get_resolution == NULL)
379 dssdriver->get_resolution = omapdss_default_get_resolution;
380 if (dssdriver->get_recommended_bpp == NULL)
381 dssdriver->get_recommended_bpp =
382 omapdss_default_get_recommended_bpp;
383 if (dssdriver->get_timings == NULL)
384 dssdriver->get_timings = omapdss_default_get_timings;
385
386 return driver_register(&dssdriver->driver);
387 }
388 EXPORT_SYMBOL(omap_dss_register_driver);
389
390 void omap_dss_unregister_driver(struct omap_dss_driver *dssdriver)
391 {
392 driver_unregister(&dssdriver->driver);
393 }
394 EXPORT_SYMBOL(omap_dss_unregister_driver);
395
396 /* DEVICE */
397
398 static void omap_dss_dev_release(struct device *dev)
399 {
400 struct omap_dss_device *dssdev = to_dss_device(dev);
401 kfree(dssdev);
402 }
403
404 static int disp_num_counter;
405
406 struct omap_dss_device *dss_alloc_and_init_device(struct device *parent)
407 {
408 struct omap_dss_device *dssdev;
409
410 dssdev = kzalloc(sizeof(*dssdev), GFP_KERNEL);
411 if (!dssdev)
412 return NULL;
413
414 dssdev->dev.bus = &dss_bus_type;
415 dssdev->dev.parent = parent;
416 dssdev->dev.release = omap_dss_dev_release;
417 dev_set_name(&dssdev->dev, "display%d", disp_num_counter++);
418
419 device_initialize(&dssdev->dev);
420
421 return dssdev;
422 }
423
424 int dss_add_device(struct omap_dss_device *dssdev)
425 {
426 return device_add(&dssdev->dev);
427 }
428
429 void dss_put_device(struct omap_dss_device *dssdev)
430 {
431 put_device(&dssdev->dev);
432 }
433
434 void dss_unregister_device(struct omap_dss_device *dssdev)
435 {
436 device_unregister(&dssdev->dev);
437 }
438
439 static int dss_unregister_dss_dev(struct device *dev, void *data)
440 {
441 struct omap_dss_device *dssdev = to_dss_device(dev);
442 dss_unregister_device(dssdev);
443 return 0;
444 }
445
446 void dss_unregister_child_devices(struct device *parent)
447 {
448 device_for_each_child(parent, NULL, dss_unregister_dss_dev);
449 }
450
451 void dss_copy_device_pdata(struct omap_dss_device *dst,
452 const struct omap_dss_device *src)
453 {
454 u8 *d = (u8 *)dst;
455 u8 *s = (u8 *)src;
456 size_t dsize = sizeof(struct device);
457
458 memcpy(d + dsize, s + dsize, sizeof(struct omap_dss_device) - dsize);
459 }
460
461 /* BUS */
462 static int __init omap_dss_bus_register(void)
463 {
464 int r;
465
466 r = bus_register(&dss_bus_type);
467 if (r) {
468 DSSERR("bus register failed\n");
469 return r;
470 }
471
472 dev_set_name(&dss_bus, "omapdss");
473 r = device_register(&dss_bus);
474 if (r) {
475 DSSERR("bus driver register failed\n");
476 bus_unregister(&dss_bus_type);
477 return r;
478 }
479
480 return 0;
481 }
482
483 /* INIT */
484 static int (*dss_output_drv_reg_funcs[])(void) __initdata = {
485 #ifdef CONFIG_OMAP2_DSS_DSI
486 dsi_init_platform_driver,
487 #endif
488 #ifdef CONFIG_OMAP2_DSS_DPI
489 dpi_init_platform_driver,
490 #endif
491 #ifdef CONFIG_OMAP2_DSS_SDI
492 sdi_init_platform_driver,
493 #endif
494 #ifdef CONFIG_OMAP2_DSS_RFBI
495 rfbi_init_platform_driver,
496 #endif
497 #ifdef CONFIG_OMAP2_DSS_VENC
498 venc_init_platform_driver,
499 #endif
500 #ifdef CONFIG_OMAP4_DSS_HDMI
501 hdmi_init_platform_driver,
502 #endif
503 };
504
505 static void (*dss_output_drv_unreg_funcs[])(void) __exitdata = {
506 #ifdef CONFIG_OMAP2_DSS_DSI
507 dsi_uninit_platform_driver,
508 #endif
509 #ifdef CONFIG_OMAP2_DSS_DPI
510 dpi_uninit_platform_driver,
511 #endif
512 #ifdef CONFIG_OMAP2_DSS_SDI
513 sdi_uninit_platform_driver,
514 #endif
515 #ifdef CONFIG_OMAP2_DSS_RFBI
516 rfbi_uninit_platform_driver,
517 #endif
518 #ifdef CONFIG_OMAP2_DSS_VENC
519 venc_uninit_platform_driver,
520 #endif
521 #ifdef CONFIG_OMAP4_DSS_HDMI
522 hdmi_uninit_platform_driver,
523 #endif
524 };
525
526 static bool dss_output_drv_loaded[ARRAY_SIZE(dss_output_drv_reg_funcs)];
527
528 static int __init omap_dss_register_drivers(void)
529 {
530 int r;
531 int i;
532
533 r = platform_driver_probe(&omap_dss_driver, omap_dss_probe);
534 if (r)
535 return r;
536
537 r = dss_init_platform_driver();
538 if (r) {
539 DSSERR("Failed to initialize DSS platform driver\n");
540 goto err_dss;
541 }
542
543 r = dispc_init_platform_driver();
544 if (r) {
545 DSSERR("Failed to initialize dispc platform driver\n");
546 goto err_dispc;
547 }
548
549 /*
550 * It's ok if the output-driver register fails. It happens, for example,
551 * when there is no output-device (e.g. SDI for OMAP4).
552 */
553 for (i = 0; i < ARRAY_SIZE(dss_output_drv_reg_funcs); ++i) {
554 r = dss_output_drv_reg_funcs[i]();
555 if (r == 0)
556 dss_output_drv_loaded[i] = true;
557 }
558
559 return 0;
560
561 err_dispc:
562 dss_uninit_platform_driver();
563 err_dss:
564 platform_driver_unregister(&omap_dss_driver);
565
566 return r;
567 }
568
569 static void __exit omap_dss_unregister_drivers(void)
570 {
571 int i;
572
573 for (i = 0; i < ARRAY_SIZE(dss_output_drv_unreg_funcs); ++i) {
574 if (dss_output_drv_loaded[i])
575 dss_output_drv_unreg_funcs[i]();
576 }
577
578 dispc_uninit_platform_driver();
579 dss_uninit_platform_driver();
580
581 platform_driver_unregister(&omap_dss_driver);
582 }
583
584 #ifdef CONFIG_OMAP2_DSS_MODULE
585 static void omap_dss_bus_unregister(void)
586 {
587 device_unregister(&dss_bus);
588
589 bus_unregister(&dss_bus_type);
590 }
591
592 static int __init omap_dss_init(void)
593 {
594 int r;
595
596 r = omap_dss_bus_register();
597 if (r)
598 return r;
599
600 r = omap_dss_register_drivers();
601 if (r) {
602 omap_dss_bus_unregister();
603 return r;
604 }
605
606 return 0;
607 }
608
609 static void __exit omap_dss_exit(void)
610 {
611 if (core.vdds_dsi_reg != NULL) {
612 regulator_put(core.vdds_dsi_reg);
613 core.vdds_dsi_reg = NULL;
614 }
615
616 if (core.vdds_sdi_reg != NULL) {
617 regulator_put(core.vdds_sdi_reg);
618 core.vdds_sdi_reg = NULL;
619 }
620
621 omap_dss_unregister_drivers();
622
623 omap_dss_bus_unregister();
624 }
625
626 module_init(omap_dss_init);
627 module_exit(omap_dss_exit);
628 #else
629 static int __init omap_dss_init(void)
630 {
631 return omap_dss_bus_register();
632 }
633
634 static int __init omap_dss_init2(void)
635 {
636 return omap_dss_register_drivers();
637 }
638
639 core_initcall(omap_dss_init);
640 device_initcall(omap_dss_init2);
641 #endif
642
643 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@nokia.com>");
644 MODULE_DESCRIPTION("OMAP2/3 Display Subsystem");
645 MODULE_LICENSE("GPL v2");
646