1 /* linux/drivers/video/exynos/exynos_mipi_dsi.c
3 * Samsung SoC MIPI-DSIM driver.
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd
7 * InKi Dae, <inki.dae@samsung.com>
8 * Donghwa Lee, <dh09.lee@samsung.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/clk.h>
19 #include <linux/mutex.h>
20 #include <linux/wait.h>
24 #include <linux/ctype.h>
25 #include <linux/platform_device.h>
27 #include <linux/irq.h>
28 #include <linux/memory.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/kthread.h>
32 #include <linux/notifier.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/pm_runtime.h>
36 #include <video/exynos_mipi_dsim.h>
38 #include "exynos_mipi_dsi_common.h"
39 #include "exynos_mipi_dsi_lowlevel.h"
41 struct mipi_dsim_ddi {
43 struct list_head list;
44 struct mipi_dsim_lcd_device *dsim_lcd_dev;
45 struct mipi_dsim_lcd_driver *dsim_lcd_drv;
48 static LIST_HEAD(dsim_ddi_list);
50 static DEFINE_MUTEX(mipi_dsim_lock);
52 static struct mipi_dsim_platform_data *to_dsim_plat(struct platform_device
55 return pdev->dev.platform_data;
58 static struct regulator_bulk_data supplies[] = {
59 { .supply = "vdd11", },
60 { .supply = "vdd18", },
63 static int exynos_mipi_regulator_enable(struct mipi_dsim_device *dsim)
67 mutex_lock(&dsim->lock);
68 ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
69 mutex_unlock(&dsim->lock);
74 static int exynos_mipi_regulator_disable(struct mipi_dsim_device *dsim)
78 mutex_lock(&dsim->lock);
79 ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
80 mutex_unlock(&dsim->lock);
85 /* update all register settings to MIPI DSI controller. */
86 static void exynos_mipi_update_cfg(struct mipi_dsim_device *dsim)
89 * data from Display controller(FIMD) is not transferred in video mode
90 * but in case of command mode, all settings is not updated to
93 exynos_mipi_dsi_stand_by(dsim, 0);
95 exynos_mipi_dsi_init_dsim(dsim);
96 exynos_mipi_dsi_init_link(dsim);
98 exynos_mipi_dsi_set_hs_enable(dsim);
100 /* set display timing. */
101 exynos_mipi_dsi_set_display_mode(dsim, dsim->dsim_config);
103 exynos_mipi_dsi_init_interrupt(dsim);
106 * data from Display controller(FIMD) is transferred in video mode
107 * but in case of command mode, all settings are updated to registers.
109 exynos_mipi_dsi_stand_by(dsim, 1);
112 static int exynos_mipi_dsi_early_blank_mode(struct mipi_dsim_device *dsim,
115 struct mipi_dsim_lcd_driver *client_drv = dsim->dsim_lcd_drv;
116 struct mipi_dsim_lcd_device *client_dev = dsim->dsim_lcd_dev;
119 case FB_BLANK_POWERDOWN:
123 if (client_drv && client_drv->suspend)
124 client_drv->suspend(client_dev);
126 clk_disable(dsim->clock);
128 exynos_mipi_regulator_disable(dsim);
130 dsim->suspended = true;
140 static int exynos_mipi_dsi_blank_mode(struct mipi_dsim_device *dsim, int power)
142 struct platform_device *pdev = to_platform_device(dsim->dev);
143 struct mipi_dsim_lcd_driver *client_drv = dsim->dsim_lcd_drv;
144 struct mipi_dsim_lcd_device *client_dev = dsim->dsim_lcd_dev;
147 case FB_BLANK_UNBLANK:
148 if (!dsim->suspended)
151 /* lcd panel power on. */
152 if (client_drv && client_drv->power_on)
153 client_drv->power_on(client_dev, 1);
155 exynos_mipi_regulator_enable(dsim);
157 /* enable MIPI-DSI PHY. */
158 if (dsim->pd->phy_enable)
159 dsim->pd->phy_enable(pdev, true);
161 clk_enable(dsim->clock);
163 exynos_mipi_update_cfg(dsim);
165 /* set lcd panel sequence commands. */
166 if (client_drv && client_drv->set_sequence)
167 client_drv->set_sequence(client_dev);
169 dsim->suspended = false;
172 case FB_BLANK_NORMAL:
182 int exynos_mipi_dsi_register_lcd_device(struct mipi_dsim_lcd_device *lcd_dev)
184 struct mipi_dsim_ddi *dsim_ddi;
186 if (!lcd_dev->name) {
187 pr_err("dsim_lcd_device name is NULL.\n");
191 dsim_ddi = kzalloc(sizeof(struct mipi_dsim_ddi), GFP_KERNEL);
193 pr_err("failed to allocate dsim_ddi object.\n");
197 dsim_ddi->dsim_lcd_dev = lcd_dev;
199 mutex_lock(&mipi_dsim_lock);
200 list_add_tail(&dsim_ddi->list, &dsim_ddi_list);
201 mutex_unlock(&mipi_dsim_lock);
206 static struct mipi_dsim_ddi *exynos_mipi_dsi_find_lcd_device(
207 struct mipi_dsim_lcd_driver *lcd_drv)
209 struct mipi_dsim_ddi *dsim_ddi, *next;
210 struct mipi_dsim_lcd_device *lcd_dev;
212 mutex_lock(&mipi_dsim_lock);
214 list_for_each_entry_safe(dsim_ddi, next, &dsim_ddi_list, list) {
218 lcd_dev = dsim_ddi->dsim_lcd_dev;
222 if ((strcmp(lcd_drv->name, lcd_dev->name)) == 0) {
224 * bus_id would be used to identify
227 dsim_ddi->bus_id = lcd_dev->bus_id;
228 mutex_unlock(&mipi_dsim_lock);
233 list_del(&dsim_ddi->list);
238 mutex_unlock(&mipi_dsim_lock);
243 int exynos_mipi_dsi_register_lcd_driver(struct mipi_dsim_lcd_driver *lcd_drv)
245 struct mipi_dsim_ddi *dsim_ddi;
247 if (!lcd_drv->name) {
248 pr_err("dsim_lcd_driver name is NULL.\n");
252 dsim_ddi = exynos_mipi_dsi_find_lcd_device(lcd_drv);
254 pr_err("mipi_dsim_ddi object not found.\n");
258 dsim_ddi->dsim_lcd_drv = lcd_drv;
260 pr_info("registered panel driver(%s) to mipi-dsi driver.\n",
267 static struct mipi_dsim_ddi *exynos_mipi_dsi_bind_lcd_ddi(
268 struct mipi_dsim_device *dsim,
271 struct mipi_dsim_ddi *dsim_ddi, *next;
272 struct mipi_dsim_lcd_driver *lcd_drv;
273 struct mipi_dsim_lcd_device *lcd_dev;
276 mutex_lock(&dsim->lock);
278 list_for_each_entry_safe(dsim_ddi, next, &dsim_ddi_list, list) {
279 lcd_drv = dsim_ddi->dsim_lcd_drv;
280 lcd_dev = dsim_ddi->dsim_lcd_dev;
281 if (!lcd_drv || !lcd_dev ||
282 (dsim->id != dsim_ddi->bus_id))
285 dev_dbg(dsim->dev, "lcd_drv->id = %d, lcd_dev->id = %d\n",
286 lcd_drv->id, lcd_dev->id);
287 dev_dbg(dsim->dev, "lcd_dev->bus_id = %d, dsim->id = %d\n",
288 lcd_dev->bus_id, dsim->id);
290 if ((strcmp(lcd_drv->name, name) == 0)) {
291 lcd_dev->master = dsim;
293 lcd_dev->dev.parent = dsim->dev;
294 dev_set_name(&lcd_dev->dev, "%s", lcd_drv->name);
296 ret = device_register(&lcd_dev->dev);
299 "can't register %s, status %d\n",
300 dev_name(&lcd_dev->dev), ret);
301 mutex_unlock(&dsim->lock);
306 dsim->dsim_lcd_dev = lcd_dev;
307 dsim->dsim_lcd_drv = lcd_drv;
309 mutex_unlock(&dsim->lock);
315 mutex_unlock(&dsim->lock);
320 /* define MIPI-DSI Master operations. */
321 static struct mipi_dsim_master_ops master_ops = {
322 .cmd_read = exynos_mipi_dsi_rd_data,
323 .cmd_write = exynos_mipi_dsi_wr_data,
324 .get_dsim_frame_done = exynos_mipi_dsi_get_frame_done_status,
325 .clear_dsim_frame_done = exynos_mipi_dsi_clear_frame_done,
326 .set_early_blank_mode = exynos_mipi_dsi_early_blank_mode,
327 .set_blank_mode = exynos_mipi_dsi_blank_mode,
330 static int exynos_mipi_dsi_probe(struct platform_device *pdev)
332 struct resource *res;
333 struct mipi_dsim_device *dsim;
334 struct mipi_dsim_config *dsim_config;
335 struct mipi_dsim_platform_data *dsim_pd;
336 struct mipi_dsim_ddi *dsim_ddi;
339 dsim = devm_kzalloc(&pdev->dev, sizeof(struct mipi_dsim_device),
342 dev_err(&pdev->dev, "failed to allocate dsim object.\n");
346 dsim->pd = to_dsim_plat(pdev);
347 dsim->dev = &pdev->dev;
350 /* get mipi_dsim_platform_data. */
351 dsim_pd = (struct mipi_dsim_platform_data *)dsim->pd;
352 if (dsim_pd == NULL) {
353 dev_err(&pdev->dev, "failed to get platform data for dsim.\n");
356 /* get mipi_dsim_config. */
357 dsim_config = dsim_pd->dsim_config;
358 if (dsim_config == NULL) {
359 dev_err(&pdev->dev, "failed to get dsim config data.\n");
363 dsim->dsim_config = dsim_config;
364 dsim->master_ops = &master_ops;
366 mutex_init(&dsim->lock);
368 ret = devm_regulator_bulk_get(&pdev->dev, ARRAY_SIZE(supplies),
371 dev_err(&pdev->dev, "Failed to get regulators: %d\n", ret);
375 dsim->clock = devm_clk_get(&pdev->dev, "dsim0");
376 if (IS_ERR(dsim->clock)) {
377 dev_err(&pdev->dev, "failed to get dsim clock source\n");
381 clk_enable(dsim->clock);
383 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
385 dsim->reg_base = devm_request_and_ioremap(&pdev->dev, res);
386 if (!dsim->reg_base) {
387 dev_err(&pdev->dev, "failed to remap io region\n");
392 mutex_init(&dsim->lock);
394 /* bind lcd ddi matched with panel name. */
395 dsim_ddi = exynos_mipi_dsi_bind_lcd_ddi(dsim, dsim_pd->lcd_panel_name);
397 dev_err(&pdev->dev, "mipi_dsim_ddi object not found.\n");
402 dsim->irq = platform_get_irq(pdev, 0);
403 if (IS_ERR_VALUE(dsim->irq)) {
404 dev_err(&pdev->dev, "failed to request dsim irq resource\n");
409 init_completion(&dsim_wr_comp);
410 init_completion(&dsim_rd_comp);
411 platform_set_drvdata(pdev, dsim);
413 ret = devm_request_irq(&pdev->dev, dsim->irq,
414 exynos_mipi_dsi_interrupt_handler,
415 IRQF_SHARED, dev_name(&pdev->dev), dsim);
417 dev_err(&pdev->dev, "failed to request dsim irq\n");
422 /* enable interrupts */
423 exynos_mipi_dsi_init_interrupt(dsim);
425 /* initialize mipi-dsi client(lcd panel). */
426 if (dsim_ddi->dsim_lcd_drv && dsim_ddi->dsim_lcd_drv->probe)
427 dsim_ddi->dsim_lcd_drv->probe(dsim_ddi->dsim_lcd_dev);
429 /* in case mipi-dsi has been enabled by bootloader */
430 if (dsim_pd->enabled) {
431 exynos_mipi_regulator_enable(dsim);
435 /* lcd panel power on. */
436 if (dsim_ddi->dsim_lcd_drv && dsim_ddi->dsim_lcd_drv->power_on)
437 dsim_ddi->dsim_lcd_drv->power_on(dsim_ddi->dsim_lcd_dev, 1);
439 exynos_mipi_regulator_enable(dsim);
441 /* enable MIPI-DSI PHY. */
442 if (dsim->pd->phy_enable)
443 dsim->pd->phy_enable(pdev, true);
445 exynos_mipi_update_cfg(dsim);
447 /* set lcd panel sequence commands. */
448 if (dsim_ddi->dsim_lcd_drv && dsim_ddi->dsim_lcd_drv->set_sequence)
449 dsim_ddi->dsim_lcd_drv->set_sequence(dsim_ddi->dsim_lcd_dev);
451 dsim->suspended = false;
454 platform_set_drvdata(pdev, dsim);
456 dev_dbg(&pdev->dev, "%s() completed successfully (%s mode)\n", __func__,
457 dsim_config->e_interface == DSIM_COMMAND ? "CPU" : "RGB");
462 clk_disable(dsim->clock);
466 static int exynos_mipi_dsi_remove(struct platform_device *pdev)
468 struct mipi_dsim_device *dsim = platform_get_drvdata(pdev);
469 struct mipi_dsim_ddi *dsim_ddi, *next;
470 struct mipi_dsim_lcd_driver *dsim_lcd_drv;
472 clk_disable(dsim->clock);
474 list_for_each_entry_safe(dsim_ddi, next, &dsim_ddi_list, list) {
476 if (dsim->id != dsim_ddi->bus_id)
479 dsim_lcd_drv = dsim_ddi->dsim_lcd_drv;
481 if (dsim_lcd_drv->remove)
482 dsim_lcd_drv->remove(dsim_ddi->dsim_lcd_dev);
491 #ifdef CONFIG_PM_SLEEP
492 static int exynos_mipi_dsi_suspend(struct device *dev)
494 struct platform_device *pdev = to_platform_device(dev);
495 struct mipi_dsim_device *dsim = platform_get_drvdata(pdev);
496 struct mipi_dsim_lcd_driver *client_drv = dsim->dsim_lcd_drv;
497 struct mipi_dsim_lcd_device *client_dev = dsim->dsim_lcd_dev;
499 disable_irq(dsim->irq);
504 if (client_drv && client_drv->suspend)
505 client_drv->suspend(client_dev);
507 /* enable MIPI-DSI PHY. */
508 if (dsim->pd->phy_enable)
509 dsim->pd->phy_enable(pdev, false);
511 clk_disable(dsim->clock);
513 exynos_mipi_regulator_disable(dsim);
515 dsim->suspended = true;
520 static int exynos_mipi_dsi_resume(struct device *dev)
522 struct platform_device *pdev = to_platform_device(dev);
523 struct mipi_dsim_device *dsim = platform_get_drvdata(pdev);
524 struct mipi_dsim_lcd_driver *client_drv = dsim->dsim_lcd_drv;
525 struct mipi_dsim_lcd_device *client_dev = dsim->dsim_lcd_dev;
527 enable_irq(dsim->irq);
529 if (!dsim->suspended)
532 /* lcd panel power on. */
533 if (client_drv && client_drv->power_on)
534 client_drv->power_on(client_dev, 1);
536 exynos_mipi_regulator_enable(dsim);
538 /* enable MIPI-DSI PHY. */
539 if (dsim->pd->phy_enable)
540 dsim->pd->phy_enable(pdev, true);
542 clk_enable(dsim->clock);
544 exynos_mipi_update_cfg(dsim);
546 /* set lcd panel sequence commands. */
547 if (client_drv && client_drv->set_sequence)
548 client_drv->set_sequence(client_dev);
550 dsim->suspended = false;
556 static const struct dev_pm_ops exynos_mipi_dsi_pm_ops = {
557 SET_SYSTEM_SLEEP_PM_OPS(exynos_mipi_dsi_suspend, exynos_mipi_dsi_resume)
560 static struct platform_driver exynos_mipi_dsi_driver = {
561 .probe = exynos_mipi_dsi_probe,
562 .remove = exynos_mipi_dsi_remove,
564 .name = "exynos-mipi-dsim",
565 .owner = THIS_MODULE,
566 .pm = &exynos_mipi_dsi_pm_ops,
570 module_platform_driver(exynos_mipi_dsi_driver);
572 MODULE_AUTHOR("InKi Dae <inki.dae@samsung.com>");
573 MODULE_DESCRIPTION("Samusung SoC MIPI-DSI driver");
574 MODULE_LICENSE("GPL");