blob: 49c735180bd81b8b3da7e74ec04ec249e6316ca7 [file] [log] [blame]
Donghwa Lee7258cc12012-02-08 12:47:39 -08001/* linux/drivers/video/exynos/exynos_mipi_dsi.c
2 *
3 * Samsung SoC MIPI-DSIM driver.
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd
6 *
7 * InKi Dae, <inki.dae@samsung.com>
8 * Donghwa Lee, <dh09.lee@samsung.com>
9 *
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.
13*/
14
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>
21#include <linux/fs.h>
22#include <linux/mm.h>
23#include <linux/fb.h>
24#include <linux/ctype.h>
25#include <linux/platform_device.h>
26#include <linux/io.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>
35
36#include <video/exynos_mipi_dsim.h>
37
38#include <plat/fb.h>
39
40#include "exynos_mipi_dsi_common.h"
41#include "exynos_mipi_dsi_lowlevel.h"
42
43struct mipi_dsim_ddi {
44 int bus_id;
45 struct list_head list;
46 struct mipi_dsim_lcd_device *dsim_lcd_dev;
47 struct mipi_dsim_lcd_driver *dsim_lcd_drv;
48};
49
50static LIST_HEAD(dsim_ddi_list);
51
52static DEFINE_MUTEX(mipi_dsim_lock);
53
54static struct mipi_dsim_platform_data *to_dsim_plat(struct platform_device
55 *pdev)
56{
57 return pdev->dev.platform_data;
58}
59
60static struct regulator_bulk_data supplies[] = {
61 { .supply = "vdd10", },
62 { .supply = "vdd18", },
63};
64
65static int exynos_mipi_regulator_enable(struct mipi_dsim_device *dsim)
66{
67 int ret;
68
69 mutex_lock(&dsim->lock);
70 ret = regulator_bulk_enable(ARRAY_SIZE(supplies), supplies);
71 mutex_unlock(&dsim->lock);
72
73 return ret;
74}
75
76static int exynos_mipi_regulator_disable(struct mipi_dsim_device *dsim)
77{
78 int ret;
79
80 mutex_lock(&dsim->lock);
81 ret = regulator_bulk_disable(ARRAY_SIZE(supplies), supplies);
82 mutex_unlock(&dsim->lock);
83
84 return ret;
85}
86
87/* update all register settings to MIPI DSI controller. */
88static void exynos_mipi_update_cfg(struct mipi_dsim_device *dsim)
89{
90 /*
91 * data from Display controller(FIMD) is not transferred in video mode
92 * but in case of command mode, all settings is not updated to
93 * registers.
94 */
95 exynos_mipi_dsi_stand_by(dsim, 0);
96
97 exynos_mipi_dsi_init_dsim(dsim);
98 exynos_mipi_dsi_init_link(dsim);
99
100 exynos_mipi_dsi_set_hs_enable(dsim);
101
102 /* set display timing. */
103 exynos_mipi_dsi_set_display_mode(dsim, dsim->dsim_config);
104
Donghwa Lee113b6682012-05-04 13:59:22 +0900105 exynos_mipi_dsi_init_interrupt(dsim);
106
Donghwa Lee7258cc12012-02-08 12:47:39 -0800107 /*
108 * data from Display controller(FIMD) is transferred in video mode
109 * but in case of command mode, all settigs is updated to registers.
110 */
111 exynos_mipi_dsi_stand_by(dsim, 1);
112}
113
114static int exynos_mipi_dsi_early_blank_mode(struct mipi_dsim_device *dsim,
115 int power)
116{
117 struct mipi_dsim_lcd_driver *client_drv = dsim->dsim_lcd_drv;
118 struct mipi_dsim_lcd_device *client_dev = dsim->dsim_lcd_dev;
119
120 switch (power) {
121 case FB_BLANK_POWERDOWN:
122 if (dsim->suspended)
123 return 0;
124
125 if (client_drv && client_drv->suspend)
126 client_drv->suspend(client_dev);
127
128 clk_disable(dsim->clock);
129
130 exynos_mipi_regulator_disable(dsim);
131
132 dsim->suspended = true;
133
134 break;
135 default:
136 break;
137 }
138
139 return 0;
140}
141
142static int exynos_mipi_dsi_blank_mode(struct mipi_dsim_device *dsim, int power)
143{
144 struct platform_device *pdev = to_platform_device(dsim->dev);
145 struct mipi_dsim_lcd_driver *client_drv = dsim->dsim_lcd_drv;
146 struct mipi_dsim_lcd_device *client_dev = dsim->dsim_lcd_dev;
147
148 switch (power) {
149 case FB_BLANK_UNBLANK:
150 if (!dsim->suspended)
151 return 0;
152
153 /* lcd panel power on. */
154 if (client_drv && client_drv->power_on)
155 client_drv->power_on(client_dev, 1);
156
157 exynos_mipi_regulator_disable(dsim);
158
159 /* enable MIPI-DSI PHY. */
160 if (dsim->pd->phy_enable)
161 dsim->pd->phy_enable(pdev, true);
162
163 clk_enable(dsim->clock);
164
165 exynos_mipi_update_cfg(dsim);
166
167 /* set lcd panel sequence commands. */
168 if (client_drv && client_drv->set_sequence)
169 client_drv->set_sequence(client_dev);
170
171 dsim->suspended = false;
172
173 break;
174 case FB_BLANK_NORMAL:
175 /* TODO. */
176 break;
177 default:
178 break;
179 }
180
181 return 0;
182}
183
184int exynos_mipi_dsi_register_lcd_device(struct mipi_dsim_lcd_device *lcd_dev)
185{
186 struct mipi_dsim_ddi *dsim_ddi;
187
188 if (!lcd_dev->name) {
189 pr_err("dsim_lcd_device name is NULL.\n");
190 return -EFAULT;
191 }
192
193 dsim_ddi = kzalloc(sizeof(struct mipi_dsim_ddi), GFP_KERNEL);
194 if (!dsim_ddi) {
195 pr_err("failed to allocate dsim_ddi object.\n");
196 return -ENOMEM;
197 }
198
199 dsim_ddi->dsim_lcd_dev = lcd_dev;
200
201 mutex_lock(&mipi_dsim_lock);
202 list_add_tail(&dsim_ddi->list, &dsim_ddi_list);
203 mutex_unlock(&mipi_dsim_lock);
204
205 return 0;
206}
207
208struct mipi_dsim_ddi *exynos_mipi_dsi_find_lcd_device(struct mipi_dsim_lcd_driver *lcd_drv)
209{
210 struct mipi_dsim_ddi *dsim_ddi, *next;
211 struct mipi_dsim_lcd_device *lcd_dev;
212
213 mutex_lock(&mipi_dsim_lock);
214
215 list_for_each_entry_safe(dsim_ddi, next, &dsim_ddi_list, list) {
216 if (!dsim_ddi)
217 goto out;
218
219 lcd_dev = dsim_ddi->dsim_lcd_dev;
220 if (!lcd_dev)
221 continue;
222
223 if ((strcmp(lcd_drv->name, lcd_dev->name)) == 0) {
224 /**
225 * bus_id would be used to identify
226 * connected bus.
227 */
228 dsim_ddi->bus_id = lcd_dev->bus_id;
229 mutex_unlock(&mipi_dsim_lock);
230
231 return dsim_ddi;
232 }
233
234 list_del(&dsim_ddi->list);
235 kfree(dsim_ddi);
236 }
237
238out:
239 mutex_unlock(&mipi_dsim_lock);
240
241 return NULL;
242}
243
244int exynos_mipi_dsi_register_lcd_driver(struct mipi_dsim_lcd_driver *lcd_drv)
245{
246 struct mipi_dsim_ddi *dsim_ddi;
247
248 if (!lcd_drv->name) {
249 pr_err("dsim_lcd_driver name is NULL.\n");
250 return -EFAULT;
251 }
252
253 dsim_ddi = exynos_mipi_dsi_find_lcd_device(lcd_drv);
254 if (!dsim_ddi) {
255 pr_err("mipi_dsim_ddi object not found.\n");
256 return -EFAULT;
257 }
258
259 dsim_ddi->dsim_lcd_drv = lcd_drv;
260
261 pr_info("registered panel driver(%s) to mipi-dsi driver.\n",
262 lcd_drv->name);
263
264 return 0;
265
266}
267
268struct mipi_dsim_ddi *exynos_mipi_dsi_bind_lcd_ddi(struct mipi_dsim_device *dsim,
269 const char *name)
270{
271 struct mipi_dsim_ddi *dsim_ddi, *next;
272 struct mipi_dsim_lcd_driver *lcd_drv;
273 struct mipi_dsim_lcd_device *lcd_dev;
274 int ret;
275
276 mutex_lock(&dsim->lock);
277
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))
283 continue;
284
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);
289
290 if ((strcmp(lcd_drv->name, name) == 0)) {
291 lcd_dev->master = dsim;
292
293 lcd_dev->dev.parent = dsim->dev;
294 dev_set_name(&lcd_dev->dev, "%s", lcd_drv->name);
295
296 ret = device_register(&lcd_dev->dev);
297 if (ret < 0) {
298 dev_err(dsim->dev,
299 "can't register %s, status %d\n",
300 dev_name(&lcd_dev->dev), ret);
301 mutex_unlock(&dsim->lock);
302
303 return NULL;
304 }
305
306 dsim->dsim_lcd_dev = lcd_dev;
307 dsim->dsim_lcd_drv = lcd_drv;
308
309 mutex_unlock(&dsim->lock);
310
311 return dsim_ddi;
312 }
313 }
314
315 mutex_unlock(&dsim->lock);
316
317 return NULL;
318}
319
320/* define MIPI-DSI Master operations. */
321static 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,
328};
329
330static int exynos_mipi_dsi_probe(struct platform_device *pdev)
331{
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;
337 int ret = -EINVAL;
338
339 dsim = kzalloc(sizeof(struct mipi_dsim_device), GFP_KERNEL);
340 if (!dsim) {
341 dev_err(&pdev->dev, "failed to allocate dsim object.\n");
342 return -ENOMEM;
343 }
344
345 dsim->pd = to_dsim_plat(pdev);
346 dsim->dev = &pdev->dev;
347 dsim->id = pdev->id;
348
349 /* get mipi_dsim_platform_data. */
350 dsim_pd = (struct mipi_dsim_platform_data *)dsim->pd;
351 if (dsim_pd == NULL) {
352 dev_err(&pdev->dev, "failed to get platform data for dsim.\n");
353 goto err_clock_get;
354 }
355 /* get mipi_dsim_config. */
356 dsim_config = dsim_pd->dsim_config;
357 if (dsim_config == NULL) {
358 dev_err(&pdev->dev, "failed to get dsim config data.\n");
359 goto err_clock_get;
360 }
361
362 dsim->dsim_config = dsim_config;
363 dsim->master_ops = &master_ops;
364
365 mutex_init(&dsim->lock);
366
367 ret = regulator_bulk_get(&pdev->dev, ARRAY_SIZE(supplies), supplies);
368 if (ret) {
369 dev_err(&pdev->dev, "Failed to get regulators: %d\n", ret);
370 goto err_clock_get;
371 }
372
373 dsim->clock = clk_get(&pdev->dev, "dsim0");
374 if (IS_ERR(dsim->clock)) {
375 dev_err(&pdev->dev, "failed to get dsim clock source\n");
376 goto err_clock_get;
377 }
378
379 clk_enable(dsim->clock);
380
381 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
382 if (!res) {
383 dev_err(&pdev->dev, "failed to get io memory region\n");
384 goto err_platform_get;
385 }
386
387 dsim->res = request_mem_region(res->start, resource_size(res),
388 dev_name(&pdev->dev));
389 if (!dsim->res) {
390 dev_err(&pdev->dev, "failed to request io memory region\n");
391 ret = -ENOMEM;
392 goto err_mem_region;
393 }
394
395 dsim->reg_base = ioremap(res->start, resource_size(res));
396 if (!dsim->reg_base) {
397 dev_err(&pdev->dev, "failed to remap io region\n");
398 ret = -ENOMEM;
399 goto err_ioremap;
400 }
401
402 mutex_init(&dsim->lock);
403
404 /* bind lcd ddi matched with panel name. */
405 dsim_ddi = exynos_mipi_dsi_bind_lcd_ddi(dsim, dsim_pd->lcd_panel_name);
406 if (!dsim_ddi) {
407 dev_err(&pdev->dev, "mipi_dsim_ddi object not found.\n");
408 goto err_bind;
409 }
410
411 dsim->irq = platform_get_irq(pdev, 0);
412 if (dsim->irq < 0) {
413 dev_err(&pdev->dev, "failed to request dsim irq resource\n");
414 ret = -EINVAL;
415 goto err_platform_get_irq;
416 }
417
418 ret = request_irq(dsim->irq, exynos_mipi_dsi_interrupt_handler,
419 IRQF_SHARED, pdev->name, dsim);
420 if (ret != 0) {
421 dev_err(&pdev->dev, "failed to request dsim irq\n");
422 ret = -EINVAL;
423 goto err_bind;
424 }
425
426 init_completion(&dsim_wr_comp);
427 init_completion(&dsim_rd_comp);
428
429 /* enable interrupt */
430 exynos_mipi_dsi_init_interrupt(dsim);
431
432 /* initialize mipi-dsi client(lcd panel). */
433 if (dsim_ddi->dsim_lcd_drv && dsim_ddi->dsim_lcd_drv->probe)
434 dsim_ddi->dsim_lcd_drv->probe(dsim_ddi->dsim_lcd_dev);
435
436 /* in case that mipi got enabled at bootloader. */
437 if (dsim_pd->enabled)
438 goto out;
439
440 /* lcd panel power on. */
441 if (dsim_ddi->dsim_lcd_drv && dsim_ddi->dsim_lcd_drv->power_on)
442 dsim_ddi->dsim_lcd_drv->power_on(dsim_ddi->dsim_lcd_dev, 1);
443
444 exynos_mipi_regulator_enable(dsim);
445
446 /* enable MIPI-DSI PHY. */
447 if (dsim->pd->phy_enable)
448 dsim->pd->phy_enable(pdev, true);
449
450 exynos_mipi_update_cfg(dsim);
451
452 /* set lcd panel sequence commands. */
453 if (dsim_ddi->dsim_lcd_drv && dsim_ddi->dsim_lcd_drv->set_sequence)
454 dsim_ddi->dsim_lcd_drv->set_sequence(dsim_ddi->dsim_lcd_dev);
455
456 dsim->suspended = false;
457
458out:
459 platform_set_drvdata(pdev, dsim);
460
461 dev_dbg(&pdev->dev, "mipi-dsi driver(%s mode) has been probed.\n",
462 (dsim_config->e_interface == DSIM_COMMAND) ?
463 "CPU" : "RGB");
464
465 return 0;
466
467err_bind:
468 iounmap(dsim->reg_base);
469
470err_ioremap:
471 release_mem_region(dsim->res->start, resource_size(dsim->res));
472
473err_mem_region:
474 release_resource(dsim->res);
475
476err_platform_get:
477 clk_disable(dsim->clock);
478 clk_put(dsim->clock);
479err_clock_get:
480 kfree(dsim);
481
482err_platform_get_irq:
483 return ret;
484}
485
486static int __devexit exynos_mipi_dsi_remove(struct platform_device *pdev)
487{
488 struct mipi_dsim_device *dsim = platform_get_drvdata(pdev);
489 struct mipi_dsim_ddi *dsim_ddi, *next;
490 struct mipi_dsim_lcd_driver *dsim_lcd_drv;
491
492 iounmap(dsim->reg_base);
493
494 clk_disable(dsim->clock);
495 clk_put(dsim->clock);
496
497 release_resource(dsim->res);
498 release_mem_region(dsim->res->start, resource_size(dsim->res));
499
500 list_for_each_entry_safe(dsim_ddi, next, &dsim_ddi_list, list) {
501 if (dsim_ddi) {
502 if (dsim->id != dsim_ddi->bus_id)
503 continue;
504
505 dsim_lcd_drv = dsim_ddi->dsim_lcd_drv;
506
507 if (dsim_lcd_drv->remove)
508 dsim_lcd_drv->remove(dsim_ddi->dsim_lcd_dev);
509
510 kfree(dsim_ddi);
511 }
512 }
513
514 regulator_bulk_free(ARRAY_SIZE(supplies), supplies);
515 kfree(dsim);
516
517 return 0;
518}
519
520#ifdef CONFIG_PM
521static int exynos_mipi_dsi_suspend(struct platform_device *pdev,
522 pm_message_t state)
523{
524 struct mipi_dsim_device *dsim = platform_get_drvdata(pdev);
525 struct mipi_dsim_lcd_driver *client_drv = dsim->dsim_lcd_drv;
526 struct mipi_dsim_lcd_device *client_dev = dsim->dsim_lcd_dev;
527
528 disable_irq(dsim->irq);
529
530 if (dsim->suspended)
531 return 0;
532
533 if (client_drv && client_drv->suspend)
534 client_drv->suspend(client_dev);
535
536 /* enable MIPI-DSI PHY. */
537 if (dsim->pd->phy_enable)
538 dsim->pd->phy_enable(pdev, false);
539
540 clk_disable(dsim->clock);
541
542 exynos_mipi_regulator_disable(dsim);
543
544 dsim->suspended = true;
545
546 return 0;
547}
548
549static int exynos_mipi_dsi_resume(struct platform_device *pdev)
550{
551 struct mipi_dsim_device *dsim = platform_get_drvdata(pdev);
552 struct mipi_dsim_lcd_driver *client_drv = dsim->dsim_lcd_drv;
553 struct mipi_dsim_lcd_device *client_dev = dsim->dsim_lcd_dev;
554
555 enable_irq(dsim->irq);
556
557 if (!dsim->suspended)
558 return 0;
559
560 /* lcd panel power on. */
561 if (client_drv && client_drv->power_on)
562 client_drv->power_on(client_dev, 1);
563
564 exynos_mipi_regulator_enable(dsim);
565
566 /* enable MIPI-DSI PHY. */
567 if (dsim->pd->phy_enable)
568 dsim->pd->phy_enable(pdev, true);
569
570 clk_enable(dsim->clock);
571
572 exynos_mipi_update_cfg(dsim);
573
574 /* set lcd panel sequence commands. */
575 if (client_drv && client_drv->set_sequence)
576 client_drv->set_sequence(client_dev);
577
578 dsim->suspended = false;
579
580 return 0;
581}
582#else
583#define exynos_mipi_dsi_suspend NULL
584#define exynos_mipi_dsi_resume NULL
585#endif
586
587static struct platform_driver exynos_mipi_dsi_driver = {
588 .probe = exynos_mipi_dsi_probe,
589 .remove = __devexit_p(exynos_mipi_dsi_remove),
590 .suspend = exynos_mipi_dsi_suspend,
591 .resume = exynos_mipi_dsi_resume,
592 .driver = {
593 .name = "exynos-mipi-dsim",
594 .owner = THIS_MODULE,
595 },
596};
597
598module_platform_driver(exynos_mipi_dsi_driver);
599
600MODULE_AUTHOR("InKi Dae <inki.dae@samsung.com>");
601MODULE_DESCRIPTION("Samusung SoC MIPI-DSI driver");
602MODULE_LICENSE("GPL");