blob: bc2e405758e3957b99342defbca1619ccdf6b529 [file] [log] [blame]
Hai Lia6895542015-03-31 14:36:33 -04001/*
2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/gpio.h>
18#include <linux/interrupt.h>
19#include <linux/of_device.h>
20#include <linux/of_gpio.h>
21#include <linux/of_irq.h>
22#include <linux/regulator/consumer.h>
23#include <linux/spinlock.h>
24#include <video/mipi_display.h>
25
26#include "dsi.h"
27#include "dsi.xml.h"
28
29#define MSM_DSI_VER_MAJOR_V2 0x02
30#define MSM_DSI_VER_MAJOR_6G 0x03
31#define MSM_DSI_6G_VER_MINOR_V1_0 0x10000000
32#define MSM_DSI_6G_VER_MINOR_V1_1 0x10010000
33#define MSM_DSI_6G_VER_MINOR_V1_1_1 0x10010001
34#define MSM_DSI_6G_VER_MINOR_V1_2 0x10020000
35#define MSM_DSI_6G_VER_MINOR_V1_3_1 0x10030001
36
37#define DSI_6G_REG_SHIFT 4
38
Hai Lia6895542015-03-31 14:36:33 -040039struct dsi_config {
40 u32 major;
41 u32 minor;
42 u32 io_offset;
Hai Lia6895542015-03-31 14:36:33 -040043 struct dsi_reg_config reg_cfg;
44};
45
46static const struct dsi_config dsi_cfgs[] = {
Hai Liec31abf2015-05-15 13:04:06 -040047 {MSM_DSI_VER_MAJOR_V2, 0, 0, {0,} },
Hai Lia6895542015-03-31 14:36:33 -040048 { /* 8974 v1 */
49 .major = MSM_DSI_VER_MAJOR_6G,
50 .minor = MSM_DSI_6G_VER_MINOR_V1_0,
51 .io_offset = DSI_6G_REG_SHIFT,
Hai Lia6895542015-03-31 14:36:33 -040052 .reg_cfg = {
53 .num = 4,
54 .regs = {
55 {"gdsc", -1, -1, -1, -1},
56 {"vdd", 3000000, 3000000, 150000, 100},
57 {"vdda", 1200000, 1200000, 100000, 100},
58 {"vddio", 1800000, 1800000, 100000, 100},
59 },
60 },
61 },
62 { /* 8974 v2 */
63 .major = MSM_DSI_VER_MAJOR_6G,
64 .minor = MSM_DSI_6G_VER_MINOR_V1_1,
65 .io_offset = DSI_6G_REG_SHIFT,
Hai Lia6895542015-03-31 14:36:33 -040066 .reg_cfg = {
67 .num = 4,
68 .regs = {
69 {"gdsc", -1, -1, -1, -1},
70 {"vdd", 3000000, 3000000, 150000, 100},
71 {"vdda", 1200000, 1200000, 100000, 100},
72 {"vddio", 1800000, 1800000, 100000, 100},
73 },
74 },
75 },
76 { /* 8974 v3 */
77 .major = MSM_DSI_VER_MAJOR_6G,
78 .minor = MSM_DSI_6G_VER_MINOR_V1_1_1,
79 .io_offset = DSI_6G_REG_SHIFT,
Hai Lia6895542015-03-31 14:36:33 -040080 .reg_cfg = {
81 .num = 4,
82 .regs = {
83 {"gdsc", -1, -1, -1, -1},
84 {"vdd", 3000000, 3000000, 150000, 100},
85 {"vdda", 1200000, 1200000, 100000, 100},
86 {"vddio", 1800000, 1800000, 100000, 100},
87 },
88 },
89 },
90 { /* 8084 */
91 .major = MSM_DSI_VER_MAJOR_6G,
92 .minor = MSM_DSI_6G_VER_MINOR_V1_2,
93 .io_offset = DSI_6G_REG_SHIFT,
Hai Lia6895542015-03-31 14:36:33 -040094 .reg_cfg = {
95 .num = 4,
96 .regs = {
97 {"gdsc", -1, -1, -1, -1},
98 {"vdd", 3000000, 3000000, 150000, 100},
99 {"vdda", 1200000, 1200000, 100000, 100},
100 {"vddio", 1800000, 1800000, 100000, 100},
101 },
102 },
103 },
104 { /* 8916 */
105 .major = MSM_DSI_VER_MAJOR_6G,
106 .minor = MSM_DSI_6G_VER_MINOR_V1_3_1,
107 .io_offset = DSI_6G_REG_SHIFT,
Hai Lia6895542015-03-31 14:36:33 -0400108 .reg_cfg = {
109 .num = 4,
110 .regs = {
111 {"gdsc", -1, -1, -1, -1},
112 {"vdd", 2850000, 2850000, 100000, 100},
113 {"vdda", 1200000, 1200000, 100000, 100},
114 {"vddio", 1800000, 1800000, 100000, 100},
115 },
116 },
117 },
118};
119
120static int dsi_get_version(const void __iomem *base, u32 *major, u32 *minor)
121{
122 u32 ver;
123 u32 ver_6g;
124
125 if (!major || !minor)
126 return -EINVAL;
127
128 /* From DSI6G(v3), addition of a 6G_HW_VERSION register at offset 0
129 * makes all other registers 4-byte shifted down.
130 */
131 ver_6g = msm_readl(base + REG_DSI_6G_HW_VERSION);
132 if (ver_6g == 0) {
133 ver = msm_readl(base + REG_DSI_VERSION);
134 ver = FIELD(ver, DSI_VERSION_MAJOR);
135 if (ver <= MSM_DSI_VER_MAJOR_V2) {
136 /* old versions */
137 *major = ver;
138 *minor = 0;
139 return 0;
140 } else {
141 return -EINVAL;
142 }
143 } else {
144 ver = msm_readl(base + DSI_6G_REG_SHIFT + REG_DSI_VERSION);
145 ver = FIELD(ver, DSI_VERSION_MAJOR);
146 if (ver == MSM_DSI_VER_MAJOR_6G) {
147 /* 6G version */
148 *major = ver;
149 *minor = ver_6g;
150 return 0;
151 } else {
152 return -EINVAL;
153 }
154 }
155}
156
157#define DSI_ERR_STATE_ACK 0x0000
158#define DSI_ERR_STATE_TIMEOUT 0x0001
159#define DSI_ERR_STATE_DLN0_PHY 0x0002
160#define DSI_ERR_STATE_FIFO 0x0004
161#define DSI_ERR_STATE_MDP_FIFO_UNDERFLOW 0x0008
162#define DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION 0x0010
163#define DSI_ERR_STATE_PLL_UNLOCKED 0x0020
164
165#define DSI_CLK_CTRL_ENABLE_CLKS \
166 (DSI_CLK_CTRL_AHBS_HCLK_ON | DSI_CLK_CTRL_AHBM_SCLK_ON | \
167 DSI_CLK_CTRL_PCLK_ON | DSI_CLK_CTRL_DSICLK_ON | \
168 DSI_CLK_CTRL_BYTECLK_ON | DSI_CLK_CTRL_ESCCLK_ON | \
169 DSI_CLK_CTRL_FORCE_ON_DYN_AHBM_HCLK)
170
171struct msm_dsi_host {
172 struct mipi_dsi_host base;
173
174 struct platform_device *pdev;
175 struct drm_device *dev;
176
177 int id;
178
179 void __iomem *ctrl_base;
Hai Liec31abf2015-05-15 13:04:06 -0400180 struct regulator_bulk_data supplies[DSI_DEV_REGULATOR_MAX];
Hai Lia6895542015-03-31 14:36:33 -0400181 struct clk *mdp_core_clk;
182 struct clk *ahb_clk;
183 struct clk *axi_clk;
184 struct clk *mmss_misc_ahb_clk;
185 struct clk *byte_clk;
186 struct clk *esc_clk;
187 struct clk *pixel_clk;
Hai Li9d32c4982015-05-15 13:04:05 -0400188 struct clk *byte_clk_src;
189 struct clk *pixel_clk_src;
190
Hai Lia6895542015-03-31 14:36:33 -0400191 u32 byte_clk_rate;
192
193 struct gpio_desc *disp_en_gpio;
194 struct gpio_desc *te_gpio;
195
196 const struct dsi_config *cfg;
197
198 struct completion dma_comp;
199 struct completion video_comp;
200 struct mutex dev_mutex;
201 struct mutex cmd_mutex;
202 struct mutex clk_mutex;
203 spinlock_t intr_lock; /* Protect interrupt ctrl register */
204
205 u32 err_work_state;
206 struct work_struct err_work;
207 struct workqueue_struct *workqueue;
208
209 struct drm_gem_object *tx_gem_obj;
210 u8 *rx_buf;
211
212 struct drm_display_mode *mode;
213
214 /* Panel info */
215 struct device_node *panel_node;
216 unsigned int channel;
217 unsigned int lanes;
218 enum mipi_dsi_pixel_format format;
219 unsigned long mode_flags;
220
221 u32 dma_cmd_ctrl_restore;
222
223 bool registered;
224 bool power_on;
225 int irq;
226};
227
228static u32 dsi_get_bpp(const enum mipi_dsi_pixel_format fmt)
229{
230 switch (fmt) {
231 case MIPI_DSI_FMT_RGB565: return 16;
232 case MIPI_DSI_FMT_RGB666_PACKED: return 18;
233 case MIPI_DSI_FMT_RGB666:
234 case MIPI_DSI_FMT_RGB888:
235 default: return 24;
236 }
237}
238
239static inline u32 dsi_read(struct msm_dsi_host *msm_host, u32 reg)
240{
241 return msm_readl(msm_host->ctrl_base + msm_host->cfg->io_offset + reg);
242}
243static inline void dsi_write(struct msm_dsi_host *msm_host, u32 reg, u32 data)
244{
245 msm_writel(data, msm_host->ctrl_base + msm_host->cfg->io_offset + reg);
246}
247
248static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host);
249static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host);
250
251static const struct dsi_config *dsi_get_config(struct msm_dsi_host *msm_host)
252{
253 const struct dsi_config *cfg;
254 struct regulator *gdsc_reg;
255 int i, ret;
256 u32 major = 0, minor = 0;
257
258 gdsc_reg = regulator_get(&msm_host->pdev->dev, "gdsc");
Fabian Frederickbdc80de2015-05-04 19:03:55 +0200259 if (IS_ERR(gdsc_reg)) {
Hai Lia6895542015-03-31 14:36:33 -0400260 pr_err("%s: cannot get gdsc\n", __func__);
261 goto fail;
262 }
263 ret = regulator_enable(gdsc_reg);
264 if (ret) {
265 pr_err("%s: unable to enable gdsc\n", __func__);
266 regulator_put(gdsc_reg);
267 goto fail;
268 }
269 ret = clk_prepare_enable(msm_host->ahb_clk);
270 if (ret) {
271 pr_err("%s: unable to enable ahb_clk\n", __func__);
272 regulator_disable(gdsc_reg);
273 regulator_put(gdsc_reg);
274 goto fail;
275 }
276
277 ret = dsi_get_version(msm_host->ctrl_base, &major, &minor);
278
279 clk_disable_unprepare(msm_host->ahb_clk);
280 regulator_disable(gdsc_reg);
281 regulator_put(gdsc_reg);
282 if (ret) {
283 pr_err("%s: Invalid version\n", __func__);
284 goto fail;
285 }
286
287 for (i = 0; i < ARRAY_SIZE(dsi_cfgs); i++) {
288 cfg = dsi_cfgs + i;
289 if ((cfg->major == major) && (cfg->minor == minor))
290 return cfg;
291 }
292 pr_err("%s: Version %x:%x not support\n", __func__, major, minor);
293
294fail:
295 return NULL;
296}
297
298static inline struct msm_dsi_host *to_msm_dsi_host(struct mipi_dsi_host *host)
299{
300 return container_of(host, struct msm_dsi_host, base);
301}
302
303static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host)
304{
305 struct regulator_bulk_data *s = msm_host->supplies;
306 const struct dsi_reg_entry *regs = msm_host->cfg->reg_cfg.regs;
307 int num = msm_host->cfg->reg_cfg.num;
308 int i;
309
310 DBG("");
311 for (i = num - 1; i >= 0; i--)
312 if (regs[i].disable_load >= 0)
Dave Airlie2c33ce02015-04-20 11:32:26 +1000313 regulator_set_load(s[i].consumer,
314 regs[i].disable_load);
Hai Lia6895542015-03-31 14:36:33 -0400315
316 regulator_bulk_disable(num, s);
317}
318
319static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host)
320{
321 struct regulator_bulk_data *s = msm_host->supplies;
322 const struct dsi_reg_entry *regs = msm_host->cfg->reg_cfg.regs;
323 int num = msm_host->cfg->reg_cfg.num;
324 int ret, i;
325
326 DBG("");
327 for (i = 0; i < num; i++) {
328 if (regs[i].enable_load >= 0) {
Dave Airlie2c33ce02015-04-20 11:32:26 +1000329 ret = regulator_set_load(s[i].consumer,
330 regs[i].enable_load);
Hai Lia6895542015-03-31 14:36:33 -0400331 if (ret < 0) {
332 pr_err("regulator %d set op mode failed, %d\n",
333 i, ret);
334 goto fail;
335 }
336 }
337 }
338
339 ret = regulator_bulk_enable(num, s);
340 if (ret < 0) {
341 pr_err("regulator enable failed, %d\n", ret);
342 goto fail;
343 }
344
345 return 0;
346
347fail:
348 for (i--; i >= 0; i--)
Dave Airlie2c33ce02015-04-20 11:32:26 +1000349 regulator_set_load(s[i].consumer, regs[i].disable_load);
Hai Lia6895542015-03-31 14:36:33 -0400350 return ret;
351}
352
353static int dsi_regulator_init(struct msm_dsi_host *msm_host)
354{
355 struct regulator_bulk_data *s = msm_host->supplies;
356 const struct dsi_reg_entry *regs = msm_host->cfg->reg_cfg.regs;
357 int num = msm_host->cfg->reg_cfg.num;
358 int i, ret;
359
360 for (i = 0; i < num; i++)
361 s[i].supply = regs[i].name;
362
363 ret = devm_regulator_bulk_get(&msm_host->pdev->dev, num, s);
364 if (ret < 0) {
365 pr_err("%s: failed to init regulator, ret=%d\n",
366 __func__, ret);
367 return ret;
368 }
369
370 for (i = 0; i < num; i++) {
371 if ((regs[i].min_voltage >= 0) && (regs[i].max_voltage >= 0)) {
372 ret = regulator_set_voltage(s[i].consumer,
373 regs[i].min_voltage, regs[i].max_voltage);
374 if (ret < 0) {
375 pr_err("regulator %d set voltage failed, %d\n",
376 i, ret);
377 return ret;
378 }
379 }
380 }
381
382 return 0;
383}
384
385static int dsi_clk_init(struct msm_dsi_host *msm_host)
386{
387 struct device *dev = &msm_host->pdev->dev;
388 int ret = 0;
389
390 msm_host->mdp_core_clk = devm_clk_get(dev, "mdp_core_clk");
391 if (IS_ERR(msm_host->mdp_core_clk)) {
392 ret = PTR_ERR(msm_host->mdp_core_clk);
393 pr_err("%s: Unable to get mdp core clk. ret=%d\n",
394 __func__, ret);
395 goto exit;
396 }
397
398 msm_host->ahb_clk = devm_clk_get(dev, "iface_clk");
399 if (IS_ERR(msm_host->ahb_clk)) {
400 ret = PTR_ERR(msm_host->ahb_clk);
401 pr_err("%s: Unable to get mdss ahb clk. ret=%d\n",
402 __func__, ret);
403 goto exit;
404 }
405
406 msm_host->axi_clk = devm_clk_get(dev, "bus_clk");
407 if (IS_ERR(msm_host->axi_clk)) {
408 ret = PTR_ERR(msm_host->axi_clk);
409 pr_err("%s: Unable to get axi bus clk. ret=%d\n",
410 __func__, ret);
411 goto exit;
412 }
413
414 msm_host->mmss_misc_ahb_clk = devm_clk_get(dev, "core_mmss_clk");
415 if (IS_ERR(msm_host->mmss_misc_ahb_clk)) {
416 ret = PTR_ERR(msm_host->mmss_misc_ahb_clk);
417 pr_err("%s: Unable to get mmss misc ahb clk. ret=%d\n",
418 __func__, ret);
419 goto exit;
420 }
421
422 msm_host->byte_clk = devm_clk_get(dev, "byte_clk");
423 if (IS_ERR(msm_host->byte_clk)) {
424 ret = PTR_ERR(msm_host->byte_clk);
425 pr_err("%s: can't find dsi_byte_clk. ret=%d\n",
426 __func__, ret);
427 msm_host->byte_clk = NULL;
428 goto exit;
429 }
430
431 msm_host->pixel_clk = devm_clk_get(dev, "pixel_clk");
432 if (IS_ERR(msm_host->pixel_clk)) {
433 ret = PTR_ERR(msm_host->pixel_clk);
434 pr_err("%s: can't find dsi_pixel_clk. ret=%d\n",
435 __func__, ret);
436 msm_host->pixel_clk = NULL;
437 goto exit;
438 }
439
440 msm_host->esc_clk = devm_clk_get(dev, "core_clk");
441 if (IS_ERR(msm_host->esc_clk)) {
442 ret = PTR_ERR(msm_host->esc_clk);
443 pr_err("%s: can't find dsi_esc_clk. ret=%d\n",
444 __func__, ret);
445 msm_host->esc_clk = NULL;
446 goto exit;
447 }
448
Hai Li9d32c4982015-05-15 13:04:05 -0400449 msm_host->byte_clk_src = devm_clk_get(dev, "byte_clk_src");
450 if (IS_ERR(msm_host->byte_clk_src)) {
451 ret = PTR_ERR(msm_host->byte_clk_src);
452 pr_err("%s: can't find byte_clk_src. ret=%d\n", __func__, ret);
453 msm_host->byte_clk_src = NULL;
454 goto exit;
455 }
456
457 msm_host->pixel_clk_src = devm_clk_get(dev, "pixel_clk_src");
458 if (IS_ERR(msm_host->pixel_clk_src)) {
459 ret = PTR_ERR(msm_host->pixel_clk_src);
460 pr_err("%s: can't find pixel_clk_src. ret=%d\n", __func__, ret);
461 msm_host->pixel_clk_src = NULL;
462 goto exit;
463 }
464
Hai Lia6895542015-03-31 14:36:33 -0400465exit:
466 return ret;
467}
468
469static int dsi_bus_clk_enable(struct msm_dsi_host *msm_host)
470{
471 int ret;
472
473 DBG("id=%d", msm_host->id);
474
475 ret = clk_prepare_enable(msm_host->mdp_core_clk);
476 if (ret) {
477 pr_err("%s: failed to enable mdp_core_clock, %d\n",
478 __func__, ret);
479 goto core_clk_err;
480 }
481
482 ret = clk_prepare_enable(msm_host->ahb_clk);
483 if (ret) {
484 pr_err("%s: failed to enable ahb clock, %d\n", __func__, ret);
485 goto ahb_clk_err;
486 }
487
488 ret = clk_prepare_enable(msm_host->axi_clk);
489 if (ret) {
490 pr_err("%s: failed to enable ahb clock, %d\n", __func__, ret);
491 goto axi_clk_err;
492 }
493
494 ret = clk_prepare_enable(msm_host->mmss_misc_ahb_clk);
495 if (ret) {
496 pr_err("%s: failed to enable mmss misc ahb clk, %d\n",
497 __func__, ret);
498 goto misc_ahb_clk_err;
499 }
500
501 return 0;
502
503misc_ahb_clk_err:
504 clk_disable_unprepare(msm_host->axi_clk);
505axi_clk_err:
506 clk_disable_unprepare(msm_host->ahb_clk);
507ahb_clk_err:
508 clk_disable_unprepare(msm_host->mdp_core_clk);
509core_clk_err:
510 return ret;
511}
512
513static void dsi_bus_clk_disable(struct msm_dsi_host *msm_host)
514{
515 DBG("");
516 clk_disable_unprepare(msm_host->mmss_misc_ahb_clk);
517 clk_disable_unprepare(msm_host->axi_clk);
518 clk_disable_unprepare(msm_host->ahb_clk);
519 clk_disable_unprepare(msm_host->mdp_core_clk);
520}
521
522static int dsi_link_clk_enable(struct msm_dsi_host *msm_host)
523{
524 int ret;
525
526 DBG("Set clk rates: pclk=%d, byteclk=%d",
527 msm_host->mode->clock, msm_host->byte_clk_rate);
528
529 ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
530 if (ret) {
531 pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
532 goto error;
533 }
534
535 ret = clk_set_rate(msm_host->pixel_clk, msm_host->mode->clock * 1000);
536 if (ret) {
537 pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
538 goto error;
539 }
540
541 ret = clk_prepare_enable(msm_host->esc_clk);
542 if (ret) {
543 pr_err("%s: Failed to enable dsi esc clk\n", __func__);
544 goto error;
545 }
546
547 ret = clk_prepare_enable(msm_host->byte_clk);
548 if (ret) {
549 pr_err("%s: Failed to enable dsi byte clk\n", __func__);
550 goto byte_clk_err;
551 }
552
553 ret = clk_prepare_enable(msm_host->pixel_clk);
554 if (ret) {
555 pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
556 goto pixel_clk_err;
557 }
558
559 return 0;
560
561pixel_clk_err:
562 clk_disable_unprepare(msm_host->byte_clk);
563byte_clk_err:
564 clk_disable_unprepare(msm_host->esc_clk);
565error:
566 return ret;
567}
568
569static void dsi_link_clk_disable(struct msm_dsi_host *msm_host)
570{
571 clk_disable_unprepare(msm_host->esc_clk);
572 clk_disable_unprepare(msm_host->pixel_clk);
573 clk_disable_unprepare(msm_host->byte_clk);
574}
575
576static int dsi_clk_ctrl(struct msm_dsi_host *msm_host, bool enable)
577{
578 int ret = 0;
579
580 mutex_lock(&msm_host->clk_mutex);
581 if (enable) {
582 ret = dsi_bus_clk_enable(msm_host);
583 if (ret) {
584 pr_err("%s: Can not enable bus clk, %d\n",
585 __func__, ret);
586 goto unlock_ret;
587 }
588 ret = dsi_link_clk_enable(msm_host);
589 if (ret) {
590 pr_err("%s: Can not enable link clk, %d\n",
591 __func__, ret);
592 dsi_bus_clk_disable(msm_host);
593 goto unlock_ret;
594 }
595 } else {
596 dsi_link_clk_disable(msm_host);
597 dsi_bus_clk_disable(msm_host);
598 }
599
600unlock_ret:
601 mutex_unlock(&msm_host->clk_mutex);
602 return ret;
603}
604
605static int dsi_calc_clk_rate(struct msm_dsi_host *msm_host)
606{
607 struct drm_display_mode *mode = msm_host->mode;
608 u8 lanes = msm_host->lanes;
609 u32 bpp = dsi_get_bpp(msm_host->format);
610 u32 pclk_rate;
611
612 if (!mode) {
613 pr_err("%s: mode not set\n", __func__);
614 return -EINVAL;
615 }
616
617 pclk_rate = mode->clock * 1000;
618 if (lanes > 0) {
619 msm_host->byte_clk_rate = (pclk_rate * bpp) / (8 * lanes);
620 } else {
621 pr_err("%s: forcing mdss_dsi lanes to 1\n", __func__);
622 msm_host->byte_clk_rate = (pclk_rate * bpp) / 8;
623 }
624
625 DBG("pclk=%d, bclk=%d", pclk_rate, msm_host->byte_clk_rate);
626
627 return 0;
628}
629
630static void dsi_phy_sw_reset(struct msm_dsi_host *msm_host)
631{
632 DBG("");
633 dsi_write(msm_host, REG_DSI_PHY_RESET, DSI_PHY_RESET_RESET);
634 /* Make sure fully reset */
635 wmb();
636 udelay(1000);
637 dsi_write(msm_host, REG_DSI_PHY_RESET, 0);
638 udelay(100);
639}
640
641static void dsi_intr_ctrl(struct msm_dsi_host *msm_host, u32 mask, int enable)
642{
643 u32 intr;
644 unsigned long flags;
645
646 spin_lock_irqsave(&msm_host->intr_lock, flags);
647 intr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
648
649 if (enable)
650 intr |= mask;
651 else
652 intr &= ~mask;
653
654 DBG("intr=%x enable=%d", intr, enable);
655
656 dsi_write(msm_host, REG_DSI_INTR_CTRL, intr);
657 spin_unlock_irqrestore(&msm_host->intr_lock, flags);
658}
659
660static inline enum dsi_traffic_mode dsi_get_traffic_mode(const u32 mode_flags)
661{
662 if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
663 return BURST_MODE;
664 else if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
665 return NON_BURST_SYNCH_PULSE;
666
667 return NON_BURST_SYNCH_EVENT;
668}
669
670static inline enum dsi_vid_dst_format dsi_get_vid_fmt(
671 const enum mipi_dsi_pixel_format mipi_fmt)
672{
673 switch (mipi_fmt) {
674 case MIPI_DSI_FMT_RGB888: return VID_DST_FORMAT_RGB888;
675 case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666_LOOSE;
676 case MIPI_DSI_FMT_RGB666_PACKED: return VID_DST_FORMAT_RGB666;
677 case MIPI_DSI_FMT_RGB565: return VID_DST_FORMAT_RGB565;
678 default: return VID_DST_FORMAT_RGB888;
679 }
680}
681
682static inline enum dsi_cmd_dst_format dsi_get_cmd_fmt(
683 const enum mipi_dsi_pixel_format mipi_fmt)
684{
685 switch (mipi_fmt) {
686 case MIPI_DSI_FMT_RGB888: return CMD_DST_FORMAT_RGB888;
687 case MIPI_DSI_FMT_RGB666_PACKED:
688 case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666;
689 case MIPI_DSI_FMT_RGB565: return CMD_DST_FORMAT_RGB565;
690 default: return CMD_DST_FORMAT_RGB888;
691 }
692}
693
694static void dsi_ctrl_config(struct msm_dsi_host *msm_host, bool enable,
695 u32 clk_pre, u32 clk_post)
696{
697 u32 flags = msm_host->mode_flags;
698 enum mipi_dsi_pixel_format mipi_fmt = msm_host->format;
699 u32 data = 0;
700
701 if (!enable) {
702 dsi_write(msm_host, REG_DSI_CTRL, 0);
703 return;
704 }
705
706 if (flags & MIPI_DSI_MODE_VIDEO) {
707 if (flags & MIPI_DSI_MODE_VIDEO_HSE)
708 data |= DSI_VID_CFG0_PULSE_MODE_HSA_HE;
709 if (flags & MIPI_DSI_MODE_VIDEO_HFP)
710 data |= DSI_VID_CFG0_HFP_POWER_STOP;
711 if (flags & MIPI_DSI_MODE_VIDEO_HBP)
712 data |= DSI_VID_CFG0_HBP_POWER_STOP;
713 if (flags & MIPI_DSI_MODE_VIDEO_HSA)
714 data |= DSI_VID_CFG0_HSA_POWER_STOP;
715 /* Always set low power stop mode for BLLP
716 * to let command engine send packets
717 */
718 data |= DSI_VID_CFG0_EOF_BLLP_POWER_STOP |
719 DSI_VID_CFG0_BLLP_POWER_STOP;
720 data |= DSI_VID_CFG0_TRAFFIC_MODE(dsi_get_traffic_mode(flags));
721 data |= DSI_VID_CFG0_DST_FORMAT(dsi_get_vid_fmt(mipi_fmt));
722 data |= DSI_VID_CFG0_VIRT_CHANNEL(msm_host->channel);
723 dsi_write(msm_host, REG_DSI_VID_CFG0, data);
724
725 /* Do not swap RGB colors */
726 data = DSI_VID_CFG1_RGB_SWAP(SWAP_RGB);
727 dsi_write(msm_host, REG_DSI_VID_CFG1, 0);
728 } else {
729 /* Do not swap RGB colors */
730 data = DSI_CMD_CFG0_RGB_SWAP(SWAP_RGB);
731 data |= DSI_CMD_CFG0_DST_FORMAT(dsi_get_cmd_fmt(mipi_fmt));
732 dsi_write(msm_host, REG_DSI_CMD_CFG0, data);
733
734 data = DSI_CMD_CFG1_WR_MEM_START(MIPI_DCS_WRITE_MEMORY_START) |
735 DSI_CMD_CFG1_WR_MEM_CONTINUE(
736 MIPI_DCS_WRITE_MEMORY_CONTINUE);
737 /* Always insert DCS command */
738 data |= DSI_CMD_CFG1_INSERT_DCS_COMMAND;
739 dsi_write(msm_host, REG_DSI_CMD_CFG1, data);
740 }
741
742 dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL,
743 DSI_CMD_DMA_CTRL_FROM_FRAME_BUFFER |
744 DSI_CMD_DMA_CTRL_LOW_POWER);
745
746 data = 0;
747 /* Always assume dedicated TE pin */
748 data |= DSI_TRIG_CTRL_TE;
749 data |= DSI_TRIG_CTRL_MDP_TRIGGER(TRIGGER_NONE);
750 data |= DSI_TRIG_CTRL_DMA_TRIGGER(TRIGGER_SW);
751 data |= DSI_TRIG_CTRL_STREAM(msm_host->channel);
752 if ((msm_host->cfg->major == MSM_DSI_VER_MAJOR_6G) &&
753 (msm_host->cfg->minor >= MSM_DSI_6G_VER_MINOR_V1_2))
754 data |= DSI_TRIG_CTRL_BLOCK_DMA_WITHIN_FRAME;
755 dsi_write(msm_host, REG_DSI_TRIG_CTRL, data);
756
757 data = DSI_CLKOUT_TIMING_CTRL_T_CLK_POST(clk_post) |
758 DSI_CLKOUT_TIMING_CTRL_T_CLK_PRE(clk_pre);
759 dsi_write(msm_host, REG_DSI_CLKOUT_TIMING_CTRL, data);
760
761 data = 0;
762 if (!(flags & MIPI_DSI_MODE_EOT_PACKET))
763 data |= DSI_EOT_PACKET_CTRL_TX_EOT_APPEND;
764 dsi_write(msm_host, REG_DSI_EOT_PACKET_CTRL, data);
765
766 /* allow only ack-err-status to generate interrupt */
767 dsi_write(msm_host, REG_DSI_ERR_INT_MASK0, 0x13ff3fe0);
768
769 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
770
771 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
772
773 data = DSI_CTRL_CLK_EN;
774
775 DBG("lane number=%d", msm_host->lanes);
776 if (msm_host->lanes == 2) {
777 data |= DSI_CTRL_LANE1 | DSI_CTRL_LANE2;
778 /* swap lanes for 2-lane panel for better performance */
779 dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
780 DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(LANE_SWAP_1230));
781 } else {
782 /* Take 4 lanes as default */
783 data |= DSI_CTRL_LANE0 | DSI_CTRL_LANE1 | DSI_CTRL_LANE2 |
784 DSI_CTRL_LANE3;
785 /* Do not swap lanes for 4-lane panel */
786 dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
787 DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(LANE_SWAP_0123));
788 }
Archit Taneja65c5e542015-04-08 11:37:40 +0530789
790 if (!(flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
791 dsi_write(msm_host, REG_DSI_LANE_CTRL,
792 DSI_LANE_CTRL_CLKLN_HS_FORCE_REQUEST);
793
Hai Lia6895542015-03-31 14:36:33 -0400794 data |= DSI_CTRL_ENABLE;
795
796 dsi_write(msm_host, REG_DSI_CTRL, data);
797}
798
799static void dsi_timing_setup(struct msm_dsi_host *msm_host)
800{
801 struct drm_display_mode *mode = msm_host->mode;
802 u32 hs_start = 0, vs_start = 0; /* take sync start as 0 */
803 u32 h_total = mode->htotal;
804 u32 v_total = mode->vtotal;
805 u32 hs_end = mode->hsync_end - mode->hsync_start;
806 u32 vs_end = mode->vsync_end - mode->vsync_start;
807 u32 ha_start = h_total - mode->hsync_start;
808 u32 ha_end = ha_start + mode->hdisplay;
809 u32 va_start = v_total - mode->vsync_start;
810 u32 va_end = va_start + mode->vdisplay;
811 u32 wc;
812
813 DBG("");
814
815 if (msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) {
816 dsi_write(msm_host, REG_DSI_ACTIVE_H,
817 DSI_ACTIVE_H_START(ha_start) |
818 DSI_ACTIVE_H_END(ha_end));
819 dsi_write(msm_host, REG_DSI_ACTIVE_V,
820 DSI_ACTIVE_V_START(va_start) |
821 DSI_ACTIVE_V_END(va_end));
822 dsi_write(msm_host, REG_DSI_TOTAL,
823 DSI_TOTAL_H_TOTAL(h_total - 1) |
824 DSI_TOTAL_V_TOTAL(v_total - 1));
825
826 dsi_write(msm_host, REG_DSI_ACTIVE_HSYNC,
827 DSI_ACTIVE_HSYNC_START(hs_start) |
828 DSI_ACTIVE_HSYNC_END(hs_end));
829 dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_HPOS, 0);
830 dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_VPOS,
831 DSI_ACTIVE_VSYNC_VPOS_START(vs_start) |
832 DSI_ACTIVE_VSYNC_VPOS_END(vs_end));
833 } else { /* command mode */
834 /* image data and 1 byte write_memory_start cmd */
835 wc = mode->hdisplay * dsi_get_bpp(msm_host->format) / 8 + 1;
836
837 dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_CTRL,
838 DSI_CMD_MDP_STREAM_CTRL_WORD_COUNT(wc) |
839 DSI_CMD_MDP_STREAM_CTRL_VIRTUAL_CHANNEL(
840 msm_host->channel) |
841 DSI_CMD_MDP_STREAM_CTRL_DATA_TYPE(
842 MIPI_DSI_DCS_LONG_WRITE));
843
844 dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_TOTAL,
845 DSI_CMD_MDP_STREAM_TOTAL_H_TOTAL(mode->hdisplay) |
846 DSI_CMD_MDP_STREAM_TOTAL_V_TOTAL(mode->vdisplay));
847 }
848}
849
850static void dsi_sw_reset(struct msm_dsi_host *msm_host)
851{
852 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
853 wmb(); /* clocks need to be enabled before reset */
854
855 dsi_write(msm_host, REG_DSI_RESET, 1);
856 wmb(); /* make sure reset happen */
857 dsi_write(msm_host, REG_DSI_RESET, 0);
858}
859
860static void dsi_op_mode_config(struct msm_dsi_host *msm_host,
861 bool video_mode, bool enable)
862{
863 u32 dsi_ctrl;
864
865 dsi_ctrl = dsi_read(msm_host, REG_DSI_CTRL);
866
867 if (!enable) {
868 dsi_ctrl &= ~(DSI_CTRL_ENABLE | DSI_CTRL_VID_MODE_EN |
869 DSI_CTRL_CMD_MODE_EN);
870 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE |
871 DSI_IRQ_MASK_VIDEO_DONE, 0);
872 } else {
873 if (video_mode) {
874 dsi_ctrl |= DSI_CTRL_VID_MODE_EN;
875 } else { /* command mode */
876 dsi_ctrl |= DSI_CTRL_CMD_MODE_EN;
877 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE, 1);
878 }
879 dsi_ctrl |= DSI_CTRL_ENABLE;
880 }
881
882 dsi_write(msm_host, REG_DSI_CTRL, dsi_ctrl);
883}
884
885static void dsi_set_tx_power_mode(int mode, struct msm_dsi_host *msm_host)
886{
887 u32 data;
888
889 data = dsi_read(msm_host, REG_DSI_CMD_DMA_CTRL);
890
891 if (mode == 0)
892 data &= ~DSI_CMD_DMA_CTRL_LOW_POWER;
893 else
894 data |= DSI_CMD_DMA_CTRL_LOW_POWER;
895
896 dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL, data);
897}
898
899static void dsi_wait4video_done(struct msm_dsi_host *msm_host)
900{
901 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 1);
902
903 reinit_completion(&msm_host->video_comp);
904
905 wait_for_completion_timeout(&msm_host->video_comp,
906 msecs_to_jiffies(70));
907
908 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 0);
909}
910
911static void dsi_wait4video_eng_busy(struct msm_dsi_host *msm_host)
912{
913 if (!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO))
914 return;
915
916 if (msm_host->power_on) {
917 dsi_wait4video_done(msm_host);
918 /* delay 4 ms to skip BLLP */
919 usleep_range(2000, 4000);
920 }
921}
922
923/* dsi_cmd */
924static int dsi_tx_buf_alloc(struct msm_dsi_host *msm_host, int size)
925{
926 struct drm_device *dev = msm_host->dev;
927 int ret;
928 u32 iova;
929
930 mutex_lock(&dev->struct_mutex);
931 msm_host->tx_gem_obj = msm_gem_new(dev, size, MSM_BO_UNCACHED);
932 if (IS_ERR(msm_host->tx_gem_obj)) {
933 ret = PTR_ERR(msm_host->tx_gem_obj);
934 pr_err("%s: failed to allocate gem, %d\n", __func__, ret);
935 msm_host->tx_gem_obj = NULL;
936 mutex_unlock(&dev->struct_mutex);
937 return ret;
938 }
939
940 ret = msm_gem_get_iova_locked(msm_host->tx_gem_obj, 0, &iova);
941 if (ret) {
942 pr_err("%s: failed to get iova, %d\n", __func__, ret);
943 return ret;
944 }
945 mutex_unlock(&dev->struct_mutex);
946
947 if (iova & 0x07) {
948 pr_err("%s: buf NOT 8 bytes aligned\n", __func__);
949 return -EINVAL;
950 }
951
952 return 0;
953}
954
955static void dsi_tx_buf_free(struct msm_dsi_host *msm_host)
956{
957 struct drm_device *dev = msm_host->dev;
958
959 if (msm_host->tx_gem_obj) {
960 msm_gem_put_iova(msm_host->tx_gem_obj, 0);
961 mutex_lock(&dev->struct_mutex);
962 msm_gem_free_object(msm_host->tx_gem_obj);
963 msm_host->tx_gem_obj = NULL;
964 mutex_unlock(&dev->struct_mutex);
965 }
966}
967
968/*
969 * prepare cmd buffer to be txed
970 */
971static int dsi_cmd_dma_add(struct drm_gem_object *tx_gem,
972 const struct mipi_dsi_msg *msg)
973{
974 struct mipi_dsi_packet packet;
975 int len;
976 int ret;
977 u8 *data;
978
979 ret = mipi_dsi_create_packet(&packet, msg);
980 if (ret) {
981 pr_err("%s: create packet failed, %d\n", __func__, ret);
982 return ret;
983 }
984 len = (packet.size + 3) & (~0x3);
985
986 if (len > tx_gem->size) {
987 pr_err("%s: packet size is too big\n", __func__);
988 return -EINVAL;
989 }
990
991 data = msm_gem_vaddr(tx_gem);
992
993 if (IS_ERR(data)) {
994 ret = PTR_ERR(data);
995 pr_err("%s: get vaddr failed, %d\n", __func__, ret);
996 return ret;
997 }
998
999 /* MSM specific command format in memory */
1000 data[0] = packet.header[1];
1001 data[1] = packet.header[2];
1002 data[2] = packet.header[0];
1003 data[3] = BIT(7); /* Last packet */
1004 if (mipi_dsi_packet_format_is_long(msg->type))
1005 data[3] |= BIT(6);
1006 if (msg->rx_buf && msg->rx_len)
1007 data[3] |= BIT(5);
1008
1009 /* Long packet */
1010 if (packet.payload && packet.payload_length)
1011 memcpy(data + 4, packet.payload, packet.payload_length);
1012
1013 /* Append 0xff to the end */
1014 if (packet.size < len)
1015 memset(data + packet.size, 0xff, len - packet.size);
1016
1017 return len;
1018}
1019
1020/*
1021 * dsi_short_read1_resp: 1 parameter
1022 */
1023static int dsi_short_read1_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1024{
1025 u8 *data = msg->rx_buf;
1026 if (data && (msg->rx_len >= 1)) {
1027 *data = buf[1]; /* strip out dcs type */
1028 return 1;
1029 } else {
Stephane Viau981371f2015-04-30 10:39:26 -04001030 pr_err("%s: read data does not match with rx_buf len %zu\n",
Hai Lia6895542015-03-31 14:36:33 -04001031 __func__, msg->rx_len);
1032 return -EINVAL;
1033 }
1034}
1035
1036/*
1037 * dsi_short_read2_resp: 2 parameter
1038 */
1039static int dsi_short_read2_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1040{
1041 u8 *data = msg->rx_buf;
1042 if (data && (msg->rx_len >= 2)) {
1043 data[0] = buf[1]; /* strip out dcs type */
1044 data[1] = buf[2];
1045 return 2;
1046 } else {
Stephane Viau981371f2015-04-30 10:39:26 -04001047 pr_err("%s: read data does not match with rx_buf len %zu\n",
Hai Lia6895542015-03-31 14:36:33 -04001048 __func__, msg->rx_len);
1049 return -EINVAL;
1050 }
1051}
1052
1053static int dsi_long_read_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1054{
1055 /* strip out 4 byte dcs header */
1056 if (msg->rx_buf && msg->rx_len)
1057 memcpy(msg->rx_buf, buf + 4, msg->rx_len);
1058
1059 return msg->rx_len;
1060}
1061
1062
1063static int dsi_cmd_dma_tx(struct msm_dsi_host *msm_host, int len)
1064{
1065 int ret;
1066 u32 iova;
1067 bool triggered;
1068
1069 ret = msm_gem_get_iova(msm_host->tx_gem_obj, 0, &iova);
1070 if (ret) {
1071 pr_err("%s: failed to get iova: %d\n", __func__, ret);
1072 return ret;
1073 }
1074
1075 reinit_completion(&msm_host->dma_comp);
1076
1077 dsi_wait4video_eng_busy(msm_host);
1078
1079 triggered = msm_dsi_manager_cmd_xfer_trigger(
1080 msm_host->id, iova, len);
1081 if (triggered) {
1082 ret = wait_for_completion_timeout(&msm_host->dma_comp,
1083 msecs_to_jiffies(200));
1084 DBG("ret=%d", ret);
1085 if (ret == 0)
1086 ret = -ETIMEDOUT;
1087 else
1088 ret = len;
1089 } else
1090 ret = len;
1091
1092 return ret;
1093}
1094
1095static int dsi_cmd_dma_rx(struct msm_dsi_host *msm_host,
1096 u8 *buf, int rx_byte, int pkt_size)
1097{
1098 u32 *lp, *temp, data;
1099 int i, j = 0, cnt;
Hai Lia6895542015-03-31 14:36:33 -04001100 u32 read_cnt;
1101 u8 reg[16];
1102 int repeated_bytes = 0;
1103 int buf_offset = buf - msm_host->rx_buf;
1104
1105 lp = (u32 *)buf;
1106 temp = (u32 *)reg;
1107 cnt = (rx_byte + 3) >> 2;
1108 if (cnt > 4)
1109 cnt = 4; /* 4 x 32 bits registers only */
1110
Hai Liec1936e2015-04-29 11:39:00 -04001111 if (rx_byte == 4)
1112 read_cnt = 4;
1113 else
1114 read_cnt = pkt_size + 6;
Hai Lia6895542015-03-31 14:36:33 -04001115
1116 /*
1117 * In case of multiple reads from the panel, after the first read, there
1118 * is possibility that there are some bytes in the payload repeating in
1119 * the RDBK_DATA registers. Since we read all the parameters from the
1120 * panel right from the first byte for every pass. We need to skip the
1121 * repeating bytes and then append the new parameters to the rx buffer.
1122 */
1123 if (read_cnt > 16) {
1124 int bytes_shifted;
1125 /* Any data more than 16 bytes will be shifted out.
1126 * The temp read buffer should already contain these bytes.
1127 * The remaining bytes in read buffer are the repeated bytes.
1128 */
1129 bytes_shifted = read_cnt - 16;
1130 repeated_bytes = buf_offset - bytes_shifted;
1131 }
1132
1133 for (i = cnt - 1; i >= 0; i--) {
1134 data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
1135 *temp++ = ntohl(data); /* to host byte order */
1136 DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
1137 }
1138
1139 for (i = repeated_bytes; i < 16; i++)
1140 buf[j++] = reg[i];
1141
1142 return j;
1143}
1144
1145static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
1146 const struct mipi_dsi_msg *msg)
1147{
1148 int len, ret;
1149 int bllp_len = msm_host->mode->hdisplay *
1150 dsi_get_bpp(msm_host->format) / 8;
1151
1152 len = dsi_cmd_dma_add(msm_host->tx_gem_obj, msg);
1153 if (!len) {
1154 pr_err("%s: failed to add cmd type = 0x%x\n",
1155 __func__, msg->type);
1156 return -EINVAL;
1157 }
1158
1159 /* for video mode, do not send cmds more than
1160 * one pixel line, since it only transmit it
1161 * during BLLP.
1162 */
1163 /* TODO: if the command is sent in LP mode, the bit rate is only
1164 * half of esc clk rate. In this case, if the video is already
1165 * actively streaming, we need to check more carefully if the
1166 * command can be fit into one BLLP.
1167 */
1168 if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
1169 pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
1170 __func__, len);
1171 return -EINVAL;
1172 }
1173
1174 ret = dsi_cmd_dma_tx(msm_host, len);
1175 if (ret < len) {
1176 pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d\n",
1177 __func__, msg->type, (*(u8 *)(msg->tx_buf)), len);
1178 return -ECOMM;
1179 }
1180
1181 return len;
1182}
1183
1184static void dsi_sw_reset_restore(struct msm_dsi_host *msm_host)
1185{
1186 u32 data0, data1;
1187
1188 data0 = dsi_read(msm_host, REG_DSI_CTRL);
1189 data1 = data0;
1190 data1 &= ~DSI_CTRL_ENABLE;
1191 dsi_write(msm_host, REG_DSI_CTRL, data1);
1192 /*
1193 * dsi controller need to be disabled before
1194 * clocks turned on
1195 */
1196 wmb();
1197
1198 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
1199 wmb(); /* make sure clocks enabled */
1200
1201 /* dsi controller can only be reset while clocks are running */
1202 dsi_write(msm_host, REG_DSI_RESET, 1);
1203 wmb(); /* make sure reset happen */
1204 dsi_write(msm_host, REG_DSI_RESET, 0);
1205 wmb(); /* controller out of reset */
1206 dsi_write(msm_host, REG_DSI_CTRL, data0);
1207 wmb(); /* make sure dsi controller enabled again */
1208}
1209
1210static void dsi_err_worker(struct work_struct *work)
1211{
1212 struct msm_dsi_host *msm_host =
1213 container_of(work, struct msm_dsi_host, err_work);
1214 u32 status = msm_host->err_work_state;
1215
Rob Clarkff431fa2015-05-07 15:19:02 -04001216 pr_err_ratelimited("%s: status=%x\n", __func__, status);
Hai Lia6895542015-03-31 14:36:33 -04001217 if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
1218 dsi_sw_reset_restore(msm_host);
1219
1220 /* It is safe to clear here because error irq is disabled. */
1221 msm_host->err_work_state = 0;
1222
1223 /* enable dsi error interrupt */
1224 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
1225}
1226
1227static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
1228{
1229 u32 status;
1230
1231 status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
1232
1233 if (status) {
1234 dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
1235 /* Writing of an extra 0 needed to clear error bits */
1236 dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
1237 msm_host->err_work_state |= DSI_ERR_STATE_ACK;
1238 }
1239}
1240
1241static void dsi_timeout_status(struct msm_dsi_host *msm_host)
1242{
1243 u32 status;
1244
1245 status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
1246
1247 if (status) {
1248 dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
1249 msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
1250 }
1251}
1252
1253static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
1254{
1255 u32 status;
1256
1257 status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
1258
1259 if (status) {
1260 dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
1261 msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
1262 }
1263}
1264
1265static void dsi_fifo_status(struct msm_dsi_host *msm_host)
1266{
1267 u32 status;
1268
1269 status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
1270
1271 /* fifo underflow, overflow */
1272 if (status) {
1273 dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
1274 msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
1275 if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
1276 msm_host->err_work_state |=
1277 DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
1278 }
1279}
1280
1281static void dsi_status(struct msm_dsi_host *msm_host)
1282{
1283 u32 status;
1284
1285 status = dsi_read(msm_host, REG_DSI_STATUS0);
1286
1287 if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
1288 dsi_write(msm_host, REG_DSI_STATUS0, status);
1289 msm_host->err_work_state |=
1290 DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
1291 }
1292}
1293
1294static void dsi_clk_status(struct msm_dsi_host *msm_host)
1295{
1296 u32 status;
1297
1298 status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
1299
1300 if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
1301 dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
1302 msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
1303 }
1304}
1305
1306static void dsi_error(struct msm_dsi_host *msm_host)
1307{
1308 /* disable dsi error interrupt */
1309 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
1310
1311 dsi_clk_status(msm_host);
1312 dsi_fifo_status(msm_host);
1313 dsi_ack_err_status(msm_host);
1314 dsi_timeout_status(msm_host);
1315 dsi_status(msm_host);
1316 dsi_dln0_phy_err(msm_host);
1317
1318 queue_work(msm_host->workqueue, &msm_host->err_work);
1319}
1320
1321static irqreturn_t dsi_host_irq(int irq, void *ptr)
1322{
1323 struct msm_dsi_host *msm_host = ptr;
1324 u32 isr;
1325 unsigned long flags;
1326
1327 if (!msm_host->ctrl_base)
1328 return IRQ_HANDLED;
1329
1330 spin_lock_irqsave(&msm_host->intr_lock, flags);
1331 isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
1332 dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
1333 spin_unlock_irqrestore(&msm_host->intr_lock, flags);
1334
1335 DBG("isr=0x%x, id=%d", isr, msm_host->id);
1336
1337 if (isr & DSI_IRQ_ERROR)
1338 dsi_error(msm_host);
1339
1340 if (isr & DSI_IRQ_VIDEO_DONE)
1341 complete(&msm_host->video_comp);
1342
1343 if (isr & DSI_IRQ_CMD_DMA_DONE)
1344 complete(&msm_host->dma_comp);
1345
1346 return IRQ_HANDLED;
1347}
1348
1349static int dsi_host_init_panel_gpios(struct msm_dsi_host *msm_host,
1350 struct device *panel_device)
1351{
1352 int ret;
1353
1354 msm_host->disp_en_gpio = devm_gpiod_get(panel_device,
1355 "disp-enable");
1356 if (IS_ERR(msm_host->disp_en_gpio)) {
1357 DBG("cannot get disp-enable-gpios %ld",
1358 PTR_ERR(msm_host->disp_en_gpio));
1359 msm_host->disp_en_gpio = NULL;
1360 }
1361 if (msm_host->disp_en_gpio) {
1362 ret = gpiod_direction_output(msm_host->disp_en_gpio, 0);
1363 if (ret) {
1364 pr_err("cannot set dir to disp-en-gpios %d\n", ret);
1365 return ret;
1366 }
1367 }
1368
1369 msm_host->te_gpio = devm_gpiod_get(panel_device, "disp-te");
1370 if (IS_ERR(msm_host->te_gpio)) {
1371 DBG("cannot get disp-te-gpios %ld", PTR_ERR(msm_host->te_gpio));
1372 msm_host->te_gpio = NULL;
1373 }
1374
1375 if (msm_host->te_gpio) {
1376 ret = gpiod_direction_input(msm_host->te_gpio);
1377 if (ret) {
1378 pr_err("%s: cannot set dir to disp-te-gpios, %d\n",
1379 __func__, ret);
1380 return ret;
1381 }
1382 }
1383
1384 return 0;
1385}
1386
1387static int dsi_host_attach(struct mipi_dsi_host *host,
1388 struct mipi_dsi_device *dsi)
1389{
1390 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1391 int ret;
1392
1393 msm_host->channel = dsi->channel;
1394 msm_host->lanes = dsi->lanes;
1395 msm_host->format = dsi->format;
1396 msm_host->mode_flags = dsi->mode_flags;
1397
1398 msm_host->panel_node = dsi->dev.of_node;
1399
1400 /* Some gpios defined in panel DT need to be controlled by host */
1401 ret = dsi_host_init_panel_gpios(msm_host, &dsi->dev);
1402 if (ret)
1403 return ret;
1404
1405 DBG("id=%d", msm_host->id);
1406 if (msm_host->dev)
1407 drm_helper_hpd_irq_event(msm_host->dev);
1408
1409 return 0;
1410}
1411
1412static int dsi_host_detach(struct mipi_dsi_host *host,
1413 struct mipi_dsi_device *dsi)
1414{
1415 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1416
1417 msm_host->panel_node = NULL;
1418
1419 DBG("id=%d", msm_host->id);
1420 if (msm_host->dev)
1421 drm_helper_hpd_irq_event(msm_host->dev);
1422
1423 return 0;
1424}
1425
1426static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
1427 const struct mipi_dsi_msg *msg)
1428{
1429 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1430 int ret;
1431
1432 if (!msg || !msm_host->power_on)
1433 return -EINVAL;
1434
1435 mutex_lock(&msm_host->cmd_mutex);
1436 ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
1437 mutex_unlock(&msm_host->cmd_mutex);
1438
1439 return ret;
1440}
1441
1442static struct mipi_dsi_host_ops dsi_host_ops = {
1443 .attach = dsi_host_attach,
1444 .detach = dsi_host_detach,
1445 .transfer = dsi_host_transfer,
1446};
1447
1448int msm_dsi_host_init(struct msm_dsi *msm_dsi)
1449{
1450 struct msm_dsi_host *msm_host = NULL;
1451 struct platform_device *pdev = msm_dsi->pdev;
1452 int ret;
1453
1454 msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
1455 if (!msm_host) {
1456 pr_err("%s: FAILED: cannot alloc dsi host\n",
1457 __func__);
1458 ret = -ENOMEM;
1459 goto fail;
1460 }
1461
1462 ret = of_property_read_u32(pdev->dev.of_node,
1463 "qcom,dsi-host-index", &msm_host->id);
1464 if (ret) {
1465 dev_err(&pdev->dev,
1466 "%s: host index not specified, ret=%d\n",
1467 __func__, ret);
1468 goto fail;
1469 }
1470 msm_host->pdev = pdev;
1471
1472 ret = dsi_clk_init(msm_host);
1473 if (ret) {
1474 pr_err("%s: unable to initialize dsi clks\n", __func__);
1475 goto fail;
1476 }
1477
1478 msm_host->ctrl_base = msm_ioremap(pdev, "dsi_ctrl", "DSI CTRL");
1479 if (IS_ERR(msm_host->ctrl_base)) {
1480 pr_err("%s: unable to map Dsi ctrl base\n", __func__);
1481 ret = PTR_ERR(msm_host->ctrl_base);
1482 goto fail;
1483 }
1484
1485 msm_host->cfg = dsi_get_config(msm_host);
1486 if (!msm_host->cfg) {
1487 ret = -EINVAL;
1488 pr_err("%s: get config failed\n", __func__);
1489 goto fail;
1490 }
1491
1492 ret = dsi_regulator_init(msm_host);
1493 if (ret) {
1494 pr_err("%s: regulator init failed\n", __func__);
1495 goto fail;
1496 }
1497
1498 msm_host->rx_buf = devm_kzalloc(&pdev->dev, SZ_4K, GFP_KERNEL);
1499 if (!msm_host->rx_buf) {
1500 pr_err("%s: alloc rx temp buf failed\n", __func__);
1501 goto fail;
1502 }
1503
1504 init_completion(&msm_host->dma_comp);
1505 init_completion(&msm_host->video_comp);
1506 mutex_init(&msm_host->dev_mutex);
1507 mutex_init(&msm_host->cmd_mutex);
1508 mutex_init(&msm_host->clk_mutex);
1509 spin_lock_init(&msm_host->intr_lock);
1510
1511 /* setup workqueue */
1512 msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
1513 INIT_WORK(&msm_host->err_work, dsi_err_worker);
1514
Hai Lia6895542015-03-31 14:36:33 -04001515 msm_dsi->host = &msm_host->base;
1516 msm_dsi->id = msm_host->id;
1517
1518 DBG("Dsi Host %d initialized", msm_host->id);
1519 return 0;
1520
1521fail:
1522 return ret;
1523}
1524
1525void msm_dsi_host_destroy(struct mipi_dsi_host *host)
1526{
1527 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1528
1529 DBG("");
1530 dsi_tx_buf_free(msm_host);
1531 if (msm_host->workqueue) {
1532 flush_workqueue(msm_host->workqueue);
1533 destroy_workqueue(msm_host->workqueue);
1534 msm_host->workqueue = NULL;
1535 }
1536
1537 mutex_destroy(&msm_host->clk_mutex);
1538 mutex_destroy(&msm_host->cmd_mutex);
1539 mutex_destroy(&msm_host->dev_mutex);
1540}
1541
1542int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
1543 struct drm_device *dev)
1544{
1545 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1546 struct platform_device *pdev = msm_host->pdev;
1547 int ret;
1548
1549 msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1550 if (msm_host->irq < 0) {
1551 ret = msm_host->irq;
1552 dev_err(dev->dev, "failed to get irq: %d\n", ret);
1553 return ret;
1554 }
1555
1556 ret = devm_request_irq(&pdev->dev, msm_host->irq,
1557 dsi_host_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1558 "dsi_isr", msm_host);
1559 if (ret < 0) {
1560 dev_err(&pdev->dev, "failed to request IRQ%u: %d\n",
1561 msm_host->irq, ret);
1562 return ret;
1563 }
1564
1565 msm_host->dev = dev;
1566 ret = dsi_tx_buf_alloc(msm_host, SZ_4K);
1567 if (ret) {
1568 pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
1569 return ret;
1570 }
1571
1572 return 0;
1573}
1574
1575int msm_dsi_host_register(struct mipi_dsi_host *host, bool check_defer)
1576{
1577 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1578 struct device_node *node;
1579 int ret;
1580
1581 /* Register mipi dsi host */
1582 if (!msm_host->registered) {
1583 host->dev = &msm_host->pdev->dev;
1584 host->ops = &dsi_host_ops;
1585 ret = mipi_dsi_host_register(host);
1586 if (ret)
1587 return ret;
1588
1589 msm_host->registered = true;
1590
1591 /* If the panel driver has not been probed after host register,
1592 * we should defer the host's probe.
1593 * It makes sure panel is connected when fbcon detects
1594 * connector status and gets the proper display mode to
1595 * create framebuffer.
1596 */
1597 if (check_defer) {
1598 node = of_get_child_by_name(msm_host->pdev->dev.of_node,
1599 "panel");
1600 if (node) {
1601 if (!of_drm_find_panel(node))
1602 return -EPROBE_DEFER;
1603 }
1604 }
1605 }
1606
1607 return 0;
1608}
1609
1610void msm_dsi_host_unregister(struct mipi_dsi_host *host)
1611{
1612 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1613
1614 if (msm_host->registered) {
1615 mipi_dsi_host_unregister(host);
1616 host->dev = NULL;
1617 host->ops = NULL;
1618 msm_host->registered = false;
1619 }
1620}
1621
1622int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
1623 const struct mipi_dsi_msg *msg)
1624{
1625 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1626
1627 /* TODO: make sure dsi_cmd_mdp is idle.
1628 * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
1629 * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
1630 * How to handle the old versions? Wait for mdp cmd done?
1631 */
1632
1633 /*
1634 * mdss interrupt is generated in mdp core clock domain
1635 * mdp clock need to be enabled to receive dsi interrupt
1636 */
1637 dsi_clk_ctrl(msm_host, 1);
1638
1639 /* TODO: vote for bus bandwidth */
1640
1641 if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
1642 dsi_set_tx_power_mode(0, msm_host);
1643
1644 msm_host->dma_cmd_ctrl_restore = dsi_read(msm_host, REG_DSI_CTRL);
1645 dsi_write(msm_host, REG_DSI_CTRL,
1646 msm_host->dma_cmd_ctrl_restore |
1647 DSI_CTRL_CMD_MODE_EN |
1648 DSI_CTRL_ENABLE);
1649 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 1);
1650
1651 return 0;
1652}
1653
1654void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
1655 const struct mipi_dsi_msg *msg)
1656{
1657 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1658
1659 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 0);
1660 dsi_write(msm_host, REG_DSI_CTRL, msm_host->dma_cmd_ctrl_restore);
1661
1662 if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
1663 dsi_set_tx_power_mode(1, msm_host);
1664
1665 /* TODO: unvote for bus bandwidth */
1666
1667 dsi_clk_ctrl(msm_host, 0);
1668}
1669
1670int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
1671 const struct mipi_dsi_msg *msg)
1672{
1673 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1674
1675 return dsi_cmds2buf_tx(msm_host, msg);
1676}
1677
1678int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
1679 const struct mipi_dsi_msg *msg)
1680{
1681 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1682 int data_byte, rx_byte, dlen, end;
1683 int short_response, diff, pkt_size, ret = 0;
1684 char cmd;
1685 int rlen = msg->rx_len;
1686 u8 *buf;
1687
1688 if (rlen <= 2) {
1689 short_response = 1;
1690 pkt_size = rlen;
1691 rx_byte = 4;
1692 } else {
1693 short_response = 0;
1694 data_byte = 10; /* first read */
1695 if (rlen < data_byte)
1696 pkt_size = rlen;
1697 else
1698 pkt_size = data_byte;
1699 rx_byte = data_byte + 6; /* 4 header + 2 crc */
1700 }
1701
1702 buf = msm_host->rx_buf;
1703 end = 0;
1704 while (!end) {
1705 u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
1706 struct mipi_dsi_msg max_pkt_size_msg = {
1707 .channel = msg->channel,
1708 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
1709 .tx_len = 2,
1710 .tx_buf = tx,
1711 };
1712
1713 DBG("rlen=%d pkt_size=%d rx_byte=%d",
1714 rlen, pkt_size, rx_byte);
1715
1716 ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
1717 if (ret < 2) {
1718 pr_err("%s: Set max pkt size failed, %d\n",
1719 __func__, ret);
1720 return -EINVAL;
1721 }
1722
1723 if ((msm_host->cfg->major == MSM_DSI_VER_MAJOR_6G) &&
1724 (msm_host->cfg->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
1725 /* Clear the RDBK_DATA registers */
1726 dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
1727 DSI_RDBK_DATA_CTRL_CLR);
1728 wmb(); /* make sure the RDBK registers are cleared */
1729 dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
1730 wmb(); /* release cleared status before transfer */
1731 }
1732
1733 ret = dsi_cmds2buf_tx(msm_host, msg);
1734 if (ret < msg->tx_len) {
1735 pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
1736 return ret;
1737 }
1738
1739 /*
1740 * once cmd_dma_done interrupt received,
1741 * return data from client is ready and stored
1742 * at RDBK_DATA register already
1743 * since rx fifo is 16 bytes, dcs header is kept at first loop,
1744 * after that dcs header lost during shift into registers
1745 */
1746 dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
1747
1748 if (dlen <= 0)
1749 return 0;
1750
1751 if (short_response)
1752 break;
1753
1754 if (rlen <= data_byte) {
1755 diff = data_byte - rlen;
1756 end = 1;
1757 } else {
1758 diff = 0;
1759 rlen -= data_byte;
1760 }
1761
1762 if (!end) {
1763 dlen -= 2; /* 2 crc */
1764 dlen -= diff;
1765 buf += dlen; /* next start position */
1766 data_byte = 14; /* NOT first read */
1767 if (rlen < data_byte)
1768 pkt_size += rlen;
1769 else
1770 pkt_size += data_byte;
1771 DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
1772 }
1773 }
1774
1775 /*
1776 * For single Long read, if the requested rlen < 10,
1777 * we need to shift the start position of rx
1778 * data buffer to skip the bytes which are not
1779 * updated.
1780 */
1781 if (pkt_size < 10 && !short_response)
1782 buf = msm_host->rx_buf + (10 - rlen);
1783 else
1784 buf = msm_host->rx_buf;
1785
1786 cmd = buf[0];
1787 switch (cmd) {
1788 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
1789 pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
1790 ret = 0;
Hai Li651ad3f2015-04-29 11:38:59 -04001791 break;
Hai Lia6895542015-03-31 14:36:33 -04001792 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
1793 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
1794 ret = dsi_short_read1_resp(buf, msg);
1795 break;
1796 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
1797 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
1798 ret = dsi_short_read2_resp(buf, msg);
1799 break;
1800 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
1801 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
1802 ret = dsi_long_read_resp(buf, msg);
1803 break;
1804 default:
1805 pr_warn("%s:Invalid response cmd\n", __func__);
1806 ret = 0;
1807 }
1808
1809 return ret;
1810}
1811
1812void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 iova, u32 len)
1813{
1814 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1815
1816 dsi_write(msm_host, REG_DSI_DMA_BASE, iova);
1817 dsi_write(msm_host, REG_DSI_DMA_LEN, len);
1818 dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
1819
1820 /* Make sure trigger happens */
1821 wmb();
1822}
1823
Hai Li9d32c4982015-05-15 13:04:05 -04001824int msm_dsi_host_set_src_pll(struct mipi_dsi_host *host,
1825 struct msm_dsi_pll *src_pll)
1826{
1827 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1828 struct clk *byte_clk_provider, *pixel_clk_provider;
1829 int ret;
1830
1831 ret = msm_dsi_pll_get_clk_provider(src_pll,
1832 &byte_clk_provider, &pixel_clk_provider);
1833 if (ret) {
1834 pr_info("%s: can't get provider from pll, don't set parent\n",
1835 __func__);
1836 return 0;
1837 }
1838
1839 ret = clk_set_parent(msm_host->byte_clk_src, byte_clk_provider);
1840 if (ret) {
1841 pr_err("%s: can't set parent to byte_clk_src. ret=%d\n",
1842 __func__, ret);
1843 goto exit;
1844 }
1845
1846 ret = clk_set_parent(msm_host->pixel_clk_src, pixel_clk_provider);
1847 if (ret) {
1848 pr_err("%s: can't set parent to pixel_clk_src. ret=%d\n",
1849 __func__, ret);
1850 goto exit;
1851 }
1852
1853exit:
1854 return ret;
1855}
1856
Hai Lia6895542015-03-31 14:36:33 -04001857int msm_dsi_host_enable(struct mipi_dsi_host *host)
1858{
1859 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1860
1861 dsi_op_mode_config(msm_host,
1862 !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
1863
1864 /* TODO: clock should be turned off for command mode,
1865 * and only turned on before MDP START.
1866 * This part of code should be enabled once mdp driver support it.
1867 */
1868 /* if (msm_panel->mode == MSM_DSI_CMD_MODE)
1869 dsi_clk_ctrl(msm_host, 0); */
1870
1871 return 0;
1872}
1873
1874int msm_dsi_host_disable(struct mipi_dsi_host *host)
1875{
1876 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1877
1878 dsi_op_mode_config(msm_host,
1879 !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
1880
1881 /* Since we have disabled INTF, the video engine won't stop so that
1882 * the cmd engine will be blocked.
1883 * Reset to disable video engine so that we can send off cmd.
1884 */
1885 dsi_sw_reset(msm_host);
1886
1887 return 0;
1888}
1889
1890int msm_dsi_host_power_on(struct mipi_dsi_host *host)
1891{
1892 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1893 u32 clk_pre = 0, clk_post = 0;
1894 int ret = 0;
1895
1896 mutex_lock(&msm_host->dev_mutex);
1897 if (msm_host->power_on) {
1898 DBG("dsi host already on");
1899 goto unlock_ret;
1900 }
1901
1902 ret = dsi_calc_clk_rate(msm_host);
1903 if (ret) {
1904 pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
1905 goto unlock_ret;
1906 }
1907
1908 ret = dsi_host_regulator_enable(msm_host);
1909 if (ret) {
1910 pr_err("%s:Failed to enable vregs.ret=%d\n",
1911 __func__, ret);
1912 goto unlock_ret;
1913 }
1914
1915 ret = dsi_bus_clk_enable(msm_host);
1916 if (ret) {
1917 pr_err("%s: failed to enable bus clocks, %d\n", __func__, ret);
1918 goto fail_disable_reg;
1919 }
1920
1921 dsi_phy_sw_reset(msm_host);
1922 ret = msm_dsi_manager_phy_enable(msm_host->id,
1923 msm_host->byte_clk_rate * 8,
1924 clk_get_rate(msm_host->esc_clk),
1925 &clk_pre, &clk_post);
1926 dsi_bus_clk_disable(msm_host);
1927 if (ret) {
1928 pr_err("%s: failed to enable phy, %d\n", __func__, ret);
1929 goto fail_disable_reg;
1930 }
1931
1932 ret = dsi_clk_ctrl(msm_host, 1);
1933 if (ret) {
1934 pr_err("%s: failed to enable clocks. ret=%d\n", __func__, ret);
1935 goto fail_disable_reg;
1936 }
1937
1938 dsi_timing_setup(msm_host);
1939 dsi_sw_reset(msm_host);
1940 dsi_ctrl_config(msm_host, true, clk_pre, clk_post);
1941
1942 if (msm_host->disp_en_gpio)
1943 gpiod_set_value(msm_host->disp_en_gpio, 1);
1944
1945 msm_host->power_on = true;
1946 mutex_unlock(&msm_host->dev_mutex);
1947
1948 return 0;
1949
1950fail_disable_reg:
1951 dsi_host_regulator_disable(msm_host);
1952unlock_ret:
1953 mutex_unlock(&msm_host->dev_mutex);
1954 return ret;
1955}
1956
1957int msm_dsi_host_power_off(struct mipi_dsi_host *host)
1958{
1959 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1960
1961 mutex_lock(&msm_host->dev_mutex);
1962 if (!msm_host->power_on) {
1963 DBG("dsi host already off");
1964 goto unlock_ret;
1965 }
1966
1967 dsi_ctrl_config(msm_host, false, 0, 0);
1968
1969 if (msm_host->disp_en_gpio)
1970 gpiod_set_value(msm_host->disp_en_gpio, 0);
1971
1972 msm_dsi_manager_phy_disable(msm_host->id);
1973
1974 dsi_clk_ctrl(msm_host, 0);
1975
1976 dsi_host_regulator_disable(msm_host);
1977
1978 DBG("-");
1979
1980 msm_host->power_on = false;
1981
1982unlock_ret:
1983 mutex_unlock(&msm_host->dev_mutex);
1984 return 0;
1985}
1986
1987int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
1988 struct drm_display_mode *mode)
1989{
1990 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1991
1992 if (msm_host->mode) {
1993 drm_mode_destroy(msm_host->dev, msm_host->mode);
1994 msm_host->mode = NULL;
1995 }
1996
1997 msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
1998 if (IS_ERR(msm_host->mode)) {
1999 pr_err("%s: cannot duplicate mode\n", __func__);
2000 return PTR_ERR(msm_host->mode);
2001 }
2002
2003 return 0;
2004}
2005
2006struct drm_panel *msm_dsi_host_get_panel(struct mipi_dsi_host *host,
2007 unsigned long *panel_flags)
2008{
2009 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2010 struct drm_panel *panel;
2011
2012 panel = of_drm_find_panel(msm_host->panel_node);
2013 if (panel_flags)
2014 *panel_flags = msm_host->mode_flags;
2015
2016 return panel;
2017}
2018