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