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