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