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