blob: 2e7077194b21452bc2b59ce86790c3f63ab8f803 [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>
Archit Taneja0c7df472015-10-14 15:31:13 +053027#include <linux/mfd/syscon.h>
28#include <linux/regmap.h>
Hai Lia6895542015-03-31 14:36:33 -040029#include <video/mipi_display.h>
30
31#include "dsi.h"
32#include "dsi.xml.h"
Archit Taneja0c7df472015-10-14 15:31:13 +053033#include "sfpb.xml.h"
Hai Lid248b612015-08-13 17:49:29 -040034#include "dsi_cfg.h"
Rob Clarkf59f62d2017-06-13 10:22:37 -040035#include "msm_kms.h"
Hai Lia6895542015-03-31 14:36:33 -040036
37static int dsi_get_version(const void __iomem *base, u32 *major, u32 *minor)
38{
39 u32 ver;
Hai Lia6895542015-03-31 14:36:33 -040040
41 if (!major || !minor)
42 return -EINVAL;
43
Archit Taneja648d5062015-10-09 11:10:59 +053044 /*
45 * From DSI6G(v3), addition of a 6G_HW_VERSION register at offset 0
Hai Lia6895542015-03-31 14:36:33 -040046 * makes all other registers 4-byte shifted down.
Archit Taneja648d5062015-10-09 11:10:59 +053047 *
48 * In order to identify between DSI6G(v3) and beyond, and DSIv2 and
49 * older, we read the DSI_VERSION register without any shift(offset
50 * 0x1f0). In the case of DSIv2, this hast to be a non-zero value. In
51 * the case of DSI6G, this has to be zero (the offset points to a
52 * scratch register which we never touch)
Hai Lia6895542015-03-31 14:36:33 -040053 */
Archit Taneja648d5062015-10-09 11:10:59 +053054
55 ver = msm_readl(base + REG_DSI_VERSION);
56 if (ver) {
57 /* older dsi host, there is no register shift */
Hai Lia6895542015-03-31 14:36:33 -040058 ver = FIELD(ver, DSI_VERSION_MAJOR);
59 if (ver <= MSM_DSI_VER_MAJOR_V2) {
60 /* old versions */
61 *major = ver;
62 *minor = 0;
63 return 0;
64 } else {
65 return -EINVAL;
66 }
67 } else {
Archit Taneja648d5062015-10-09 11:10:59 +053068 /*
69 * newer host, offset 0 has 6G_HW_VERSION, the rest of the
70 * registers are shifted down, read DSI_VERSION again with
71 * the shifted offset
72 */
Hai Lia6895542015-03-31 14:36:33 -040073 ver = msm_readl(base + DSI_6G_REG_SHIFT + REG_DSI_VERSION);
74 ver = FIELD(ver, DSI_VERSION_MAJOR);
75 if (ver == MSM_DSI_VER_MAJOR_6G) {
76 /* 6G version */
77 *major = ver;
Archit Taneja648d5062015-10-09 11:10:59 +053078 *minor = msm_readl(base + REG_DSI_6G_HW_VERSION);
Hai Lia6895542015-03-31 14:36:33 -040079 return 0;
80 } else {
81 return -EINVAL;
82 }
83 }
84}
85
86#define DSI_ERR_STATE_ACK 0x0000
87#define DSI_ERR_STATE_TIMEOUT 0x0001
88#define DSI_ERR_STATE_DLN0_PHY 0x0002
89#define DSI_ERR_STATE_FIFO 0x0004
90#define DSI_ERR_STATE_MDP_FIFO_UNDERFLOW 0x0008
91#define DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION 0x0010
92#define DSI_ERR_STATE_PLL_UNLOCKED 0x0020
93
94#define DSI_CLK_CTRL_ENABLE_CLKS \
95 (DSI_CLK_CTRL_AHBS_HCLK_ON | DSI_CLK_CTRL_AHBM_SCLK_ON | \
96 DSI_CLK_CTRL_PCLK_ON | DSI_CLK_CTRL_DSICLK_ON | \
97 DSI_CLK_CTRL_BYTECLK_ON | DSI_CLK_CTRL_ESCCLK_ON | \
98 DSI_CLK_CTRL_FORCE_ON_DYN_AHBM_HCLK)
99
100struct msm_dsi_host {
101 struct mipi_dsi_host base;
102
103 struct platform_device *pdev;
104 struct drm_device *dev;
105
106 int id;
107
108 void __iomem *ctrl_base;
Hai Liec31abf2015-05-15 13:04:06 -0400109 struct regulator_bulk_data supplies[DSI_DEV_REGULATOR_MAX];
Archit Taneja6e0eb522015-10-09 15:21:12 +0530110
111 struct clk *bus_clks[DSI_BUS_CLK_MAX];
112
Hai Lia6895542015-03-31 14:36:33 -0400113 struct clk *byte_clk;
114 struct clk *esc_clk;
115 struct clk *pixel_clk;
Hai Li9d32c4982015-05-15 13:04:05 -0400116 struct clk *byte_clk_src;
117 struct clk *pixel_clk_src;
118
Hai Lia6895542015-03-31 14:36:33 -0400119 u32 byte_clk_rate;
Archit Taneja4bfa9742015-10-09 16:32:38 +0530120 u32 esc_clk_rate;
121
122 /* DSI v2 specific clocks */
123 struct clk *src_clk;
124 struct clk *esc_clk_src;
125 struct clk *dsi_clk_src;
126
127 u32 src_clk_rate;
Hai Lia6895542015-03-31 14:36:33 -0400128
129 struct gpio_desc *disp_en_gpio;
130 struct gpio_desc *te_gpio;
131
Hai Lid248b612015-08-13 17:49:29 -0400132 const struct msm_dsi_cfg_handler *cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400133
134 struct completion dma_comp;
135 struct completion video_comp;
136 struct mutex dev_mutex;
137 struct mutex cmd_mutex;
138 struct mutex clk_mutex;
139 spinlock_t intr_lock; /* Protect interrupt ctrl register */
140
141 u32 err_work_state;
142 struct work_struct err_work;
Archit Taneja8d23ea42016-10-25 12:17:59 +0530143 struct work_struct hpd_work;
Hai Lia6895542015-03-31 14:36:33 -0400144 struct workqueue_struct *workqueue;
145
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530146 /* DSI 6G TX buffer*/
Hai Lia6895542015-03-31 14:36:33 -0400147 struct drm_gem_object *tx_gem_obj;
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530148
149 /* DSI v2 TX buffer */
150 void *tx_buf;
151 dma_addr_t tx_buf_paddr;
152
153 int tx_size;
154
Hai Lia6895542015-03-31 14:36:33 -0400155 u8 *rx_buf;
156
Archit Taneja0c7df472015-10-14 15:31:13 +0530157 struct regmap *sfpb;
158
Hai Lia6895542015-03-31 14:36:33 -0400159 struct drm_display_mode *mode;
160
Archit Tanejaa9ddac92015-08-03 14:05:45 +0530161 /* connected device info */
162 struct device_node *device_node;
Hai Lia6895542015-03-31 14:36:33 -0400163 unsigned int channel;
164 unsigned int lanes;
165 enum mipi_dsi_pixel_format format;
166 unsigned long mode_flags;
167
Archit Taneja26f7d1f2016-02-25 11:19:48 +0530168 /* lane data parsed via DT */
169 int dlane_swap;
170 int num_data_lanes;
171
Hai Lia6895542015-03-31 14:36:33 -0400172 u32 dma_cmd_ctrl_restore;
173
174 bool registered;
175 bool power_on;
176 int irq;
177};
178
179static u32 dsi_get_bpp(const enum mipi_dsi_pixel_format fmt)
180{
181 switch (fmt) {
182 case MIPI_DSI_FMT_RGB565: return 16;
183 case MIPI_DSI_FMT_RGB666_PACKED: return 18;
184 case MIPI_DSI_FMT_RGB666:
185 case MIPI_DSI_FMT_RGB888:
186 default: return 24;
187 }
188}
189
190static inline u32 dsi_read(struct msm_dsi_host *msm_host, u32 reg)
191{
Hai Lid248b612015-08-13 17:49:29 -0400192 return msm_readl(msm_host->ctrl_base + reg);
Hai Lia6895542015-03-31 14:36:33 -0400193}
194static inline void dsi_write(struct msm_dsi_host *msm_host, u32 reg, u32 data)
195{
Hai Lid248b612015-08-13 17:49:29 -0400196 msm_writel(data, msm_host->ctrl_base + reg);
Hai Lia6895542015-03-31 14:36:33 -0400197}
198
199static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host);
200static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host);
201
Hai Lid248b612015-08-13 17:49:29 -0400202static const struct msm_dsi_cfg_handler *dsi_get_config(
203 struct msm_dsi_host *msm_host)
Hai Lia6895542015-03-31 14:36:33 -0400204{
Hai Lid248b612015-08-13 17:49:29 -0400205 const struct msm_dsi_cfg_handler *cfg_hnd = NULL;
Archit Taneja31c92762015-10-09 12:40:39 +0530206 struct device *dev = &msm_host->pdev->dev;
Hai Lia6895542015-03-31 14:36:33 -0400207 struct regulator *gdsc_reg;
Archit Taneja31c92762015-10-09 12:40:39 +0530208 struct clk *ahb_clk;
Hai Lid248b612015-08-13 17:49:29 -0400209 int ret;
Hai Lia6895542015-03-31 14:36:33 -0400210 u32 major = 0, minor = 0;
211
Archit Taneja31c92762015-10-09 12:40:39 +0530212 gdsc_reg = regulator_get(dev, "gdsc");
Fabian Frederickbdc80de2015-05-04 19:03:55 +0200213 if (IS_ERR(gdsc_reg)) {
Hai Lia6895542015-03-31 14:36:33 -0400214 pr_err("%s: cannot get gdsc\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400215 goto exit;
Hai Lia6895542015-03-31 14:36:33 -0400216 }
Archit Taneja31c92762015-10-09 12:40:39 +0530217
218 ahb_clk = clk_get(dev, "iface_clk");
219 if (IS_ERR(ahb_clk)) {
220 pr_err("%s: cannot get interface clock\n", __func__);
221 goto put_gdsc;
222 }
223
Hai Lia6895542015-03-31 14:36:33 -0400224 ret = regulator_enable(gdsc_reg);
225 if (ret) {
226 pr_err("%s: unable to enable gdsc\n", __func__);
Archit Taneja31c92762015-10-09 12:40:39 +0530227 goto put_clk;
Hai Lia6895542015-03-31 14:36:33 -0400228 }
Archit Taneja31c92762015-10-09 12:40:39 +0530229
230 ret = clk_prepare_enable(ahb_clk);
Hai Lia6895542015-03-31 14:36:33 -0400231 if (ret) {
232 pr_err("%s: unable to enable ahb_clk\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400233 goto disable_gdsc;
Hai Lia6895542015-03-31 14:36:33 -0400234 }
235
236 ret = dsi_get_version(msm_host->ctrl_base, &major, &minor);
Hai Lia6895542015-03-31 14:36:33 -0400237 if (ret) {
238 pr_err("%s: Invalid version\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400239 goto disable_clks;
Hai Lia6895542015-03-31 14:36:33 -0400240 }
241
Hai Lid248b612015-08-13 17:49:29 -0400242 cfg_hnd = msm_dsi_cfg_get(major, minor);
Hai Lia6895542015-03-31 14:36:33 -0400243
Hai Lid248b612015-08-13 17:49:29 -0400244 DBG("%s: Version %x:%x\n", __func__, major, minor);
245
246disable_clks:
Archit Taneja31c92762015-10-09 12:40:39 +0530247 clk_disable_unprepare(ahb_clk);
Hai Lid248b612015-08-13 17:49:29 -0400248disable_gdsc:
249 regulator_disable(gdsc_reg);
Archit Taneja31c92762015-10-09 12:40:39 +0530250put_clk:
251 clk_put(ahb_clk);
Hai Lid248b612015-08-13 17:49:29 -0400252put_gdsc:
253 regulator_put(gdsc_reg);
254exit:
255 return cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400256}
257
258static inline struct msm_dsi_host *to_msm_dsi_host(struct mipi_dsi_host *host)
259{
260 return container_of(host, struct msm_dsi_host, base);
261}
262
263static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host)
264{
265 struct regulator_bulk_data *s = msm_host->supplies;
Hai Lid248b612015-08-13 17:49:29 -0400266 const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
267 int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
Hai Lia6895542015-03-31 14:36:33 -0400268 int i;
269
270 DBG("");
271 for (i = num - 1; i >= 0; i--)
272 if (regs[i].disable_load >= 0)
Dave Airlie2c33ce02015-04-20 11:32:26 +1000273 regulator_set_load(s[i].consumer,
274 regs[i].disable_load);
Hai Lia6895542015-03-31 14:36:33 -0400275
276 regulator_bulk_disable(num, s);
277}
278
279static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host)
280{
281 struct regulator_bulk_data *s = msm_host->supplies;
Hai Lid248b612015-08-13 17:49:29 -0400282 const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
283 int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
Hai Lia6895542015-03-31 14:36:33 -0400284 int ret, i;
285
286 DBG("");
287 for (i = 0; i < num; i++) {
288 if (regs[i].enable_load >= 0) {
Dave Airlie2c33ce02015-04-20 11:32:26 +1000289 ret = regulator_set_load(s[i].consumer,
290 regs[i].enable_load);
Hai Lia6895542015-03-31 14:36:33 -0400291 if (ret < 0) {
292 pr_err("regulator %d set op mode failed, %d\n",
293 i, ret);
294 goto fail;
295 }
296 }
297 }
298
299 ret = regulator_bulk_enable(num, s);
300 if (ret < 0) {
301 pr_err("regulator enable failed, %d\n", ret);
302 goto fail;
303 }
304
305 return 0;
306
307fail:
308 for (i--; i >= 0; i--)
Dave Airlie2c33ce02015-04-20 11:32:26 +1000309 regulator_set_load(s[i].consumer, regs[i].disable_load);
Hai Lia6895542015-03-31 14:36:33 -0400310 return ret;
311}
312
313static int dsi_regulator_init(struct msm_dsi_host *msm_host)
314{
315 struct regulator_bulk_data *s = msm_host->supplies;
Hai Lid248b612015-08-13 17:49:29 -0400316 const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
317 int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
Hai Lia6895542015-03-31 14:36:33 -0400318 int i, ret;
319
320 for (i = 0; i < num; i++)
321 s[i].supply = regs[i].name;
322
323 ret = devm_regulator_bulk_get(&msm_host->pdev->dev, num, s);
324 if (ret < 0) {
325 pr_err("%s: failed to init regulator, ret=%d\n",
326 __func__, ret);
327 return ret;
328 }
329
Hai Lia6895542015-03-31 14:36:33 -0400330 return 0;
331}
332
333static int dsi_clk_init(struct msm_dsi_host *msm_host)
334{
335 struct device *dev = &msm_host->pdev->dev;
Archit Taneja4bfa9742015-10-09 16:32:38 +0530336 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
337 const struct msm_dsi_config *cfg = cfg_hnd->cfg;
Archit Taneja6e0eb522015-10-09 15:21:12 +0530338 int i, ret = 0;
Hai Lia6895542015-03-31 14:36:33 -0400339
Archit Taneja6e0eb522015-10-09 15:21:12 +0530340 /* get bus clocks */
341 for (i = 0; i < cfg->num_bus_clks; i++) {
342 msm_host->bus_clks[i] = devm_clk_get(dev,
343 cfg->bus_clk_names[i]);
344 if (IS_ERR(msm_host->bus_clks[i])) {
345 ret = PTR_ERR(msm_host->bus_clks[i]);
346 pr_err("%s: Unable to get %s, ret = %d\n",
347 __func__, cfg->bus_clk_names[i], ret);
348 goto exit;
349 }
Hai Lia6895542015-03-31 14:36:33 -0400350 }
351
Archit Taneja6e0eb522015-10-09 15:21:12 +0530352 /* get link and source clocks */
Hai Lia6895542015-03-31 14:36:33 -0400353 msm_host->byte_clk = devm_clk_get(dev, "byte_clk");
354 if (IS_ERR(msm_host->byte_clk)) {
355 ret = PTR_ERR(msm_host->byte_clk);
356 pr_err("%s: can't find dsi_byte_clk. ret=%d\n",
357 __func__, ret);
358 msm_host->byte_clk = NULL;
359 goto exit;
360 }
361
362 msm_host->pixel_clk = devm_clk_get(dev, "pixel_clk");
363 if (IS_ERR(msm_host->pixel_clk)) {
364 ret = PTR_ERR(msm_host->pixel_clk);
365 pr_err("%s: can't find dsi_pixel_clk. ret=%d\n",
366 __func__, ret);
367 msm_host->pixel_clk = NULL;
368 goto exit;
369 }
370
371 msm_host->esc_clk = devm_clk_get(dev, "core_clk");
372 if (IS_ERR(msm_host->esc_clk)) {
373 ret = PTR_ERR(msm_host->esc_clk);
374 pr_err("%s: can't find dsi_esc_clk. ret=%d\n",
375 __func__, ret);
376 msm_host->esc_clk = NULL;
377 goto exit;
378 }
379
Archit Tanejae6c4c782015-11-30 17:47:17 +0530380 msm_host->byte_clk_src = clk_get_parent(msm_host->byte_clk);
381 if (!msm_host->byte_clk_src) {
382 ret = -ENODEV;
Hai Li9d32c4982015-05-15 13:04:05 -0400383 pr_err("%s: can't find byte_clk_src. ret=%d\n", __func__, ret);
Hai Li9d32c4982015-05-15 13:04:05 -0400384 goto exit;
385 }
386
Archit Tanejae6c4c782015-11-30 17:47:17 +0530387 msm_host->pixel_clk_src = clk_get_parent(msm_host->pixel_clk);
388 if (!msm_host->pixel_clk_src) {
389 ret = -ENODEV;
Hai Li9d32c4982015-05-15 13:04:05 -0400390 pr_err("%s: can't find pixel_clk_src. ret=%d\n", __func__, ret);
Archit Taneja4bfa9742015-10-09 16:32:38 +0530391 goto exit;
Hai Li9d32c4982015-05-15 13:04:05 -0400392 }
393
Archit Taneja4bfa9742015-10-09 16:32:38 +0530394 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
395 msm_host->src_clk = devm_clk_get(dev, "src_clk");
396 if (IS_ERR(msm_host->src_clk)) {
397 ret = PTR_ERR(msm_host->src_clk);
398 pr_err("%s: can't find dsi_src_clk. ret=%d\n",
399 __func__, ret);
400 msm_host->src_clk = NULL;
401 goto exit;
402 }
403
404 msm_host->esc_clk_src = clk_get_parent(msm_host->esc_clk);
405 if (!msm_host->esc_clk_src) {
406 ret = -ENODEV;
407 pr_err("%s: can't get esc_clk_src. ret=%d\n",
408 __func__, ret);
409 goto exit;
410 }
411
412 msm_host->dsi_clk_src = clk_get_parent(msm_host->src_clk);
413 if (!msm_host->dsi_clk_src) {
414 ret = -ENODEV;
415 pr_err("%s: can't get dsi_clk_src. ret=%d\n",
416 __func__, ret);
417 }
418 }
Hai Lia6895542015-03-31 14:36:33 -0400419exit:
420 return ret;
421}
422
423static int dsi_bus_clk_enable(struct msm_dsi_host *msm_host)
424{
Archit Taneja6e0eb522015-10-09 15:21:12 +0530425 const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
426 int i, ret;
Hai Lia6895542015-03-31 14:36:33 -0400427
428 DBG("id=%d", msm_host->id);
429
Archit Taneja6e0eb522015-10-09 15:21:12 +0530430 for (i = 0; i < cfg->num_bus_clks; i++) {
431 ret = clk_prepare_enable(msm_host->bus_clks[i]);
432 if (ret) {
433 pr_err("%s: failed to enable bus clock %d ret %d\n",
434 __func__, i, ret);
435 goto err;
436 }
Hai Lia6895542015-03-31 14:36:33 -0400437 }
438
439 return 0;
Archit Taneja6e0eb522015-10-09 15:21:12 +0530440err:
441 for (; i > 0; i--)
442 clk_disable_unprepare(msm_host->bus_clks[i]);
Hai Lia6895542015-03-31 14:36:33 -0400443
Hai Lia6895542015-03-31 14:36:33 -0400444 return ret;
445}
446
447static void dsi_bus_clk_disable(struct msm_dsi_host *msm_host)
448{
Archit Taneja6e0eb522015-10-09 15:21:12 +0530449 const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
450 int i;
451
Hai Lia6895542015-03-31 14:36:33 -0400452 DBG("");
Archit Taneja6e0eb522015-10-09 15:21:12 +0530453
454 for (i = cfg->num_bus_clks - 1; i >= 0; i--)
455 clk_disable_unprepare(msm_host->bus_clks[i]);
Hai Lia6895542015-03-31 14:36:33 -0400456}
457
Archit Taneja4bfa9742015-10-09 16:32:38 +0530458static int dsi_link_clk_enable_6g(struct msm_dsi_host *msm_host)
Hai Lia6895542015-03-31 14:36:33 -0400459{
460 int ret;
461
462 DBG("Set clk rates: pclk=%d, byteclk=%d",
463 msm_host->mode->clock, msm_host->byte_clk_rate);
464
465 ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
466 if (ret) {
467 pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
468 goto error;
469 }
470
471 ret = clk_set_rate(msm_host->pixel_clk, msm_host->mode->clock * 1000);
472 if (ret) {
473 pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
474 goto error;
475 }
476
477 ret = clk_prepare_enable(msm_host->esc_clk);
478 if (ret) {
479 pr_err("%s: Failed to enable dsi esc clk\n", __func__);
480 goto error;
481 }
482
483 ret = clk_prepare_enable(msm_host->byte_clk);
484 if (ret) {
485 pr_err("%s: Failed to enable dsi byte clk\n", __func__);
486 goto byte_clk_err;
487 }
488
489 ret = clk_prepare_enable(msm_host->pixel_clk);
490 if (ret) {
491 pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
492 goto pixel_clk_err;
493 }
494
495 return 0;
496
497pixel_clk_err:
498 clk_disable_unprepare(msm_host->byte_clk);
499byte_clk_err:
500 clk_disable_unprepare(msm_host->esc_clk);
501error:
502 return ret;
503}
504
Archit Taneja4bfa9742015-10-09 16:32:38 +0530505static int dsi_link_clk_enable_v2(struct msm_dsi_host *msm_host)
506{
507 int ret;
508
509 DBG("Set clk rates: pclk=%d, byteclk=%d, esc_clk=%d, dsi_src_clk=%d",
510 msm_host->mode->clock, msm_host->byte_clk_rate,
511 msm_host->esc_clk_rate, msm_host->src_clk_rate);
512
513 ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
514 if (ret) {
515 pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
516 goto error;
517 }
518
519 ret = clk_set_rate(msm_host->esc_clk, msm_host->esc_clk_rate);
520 if (ret) {
521 pr_err("%s: Failed to set rate esc clk, %d\n", __func__, ret);
522 goto error;
523 }
524
525 ret = clk_set_rate(msm_host->src_clk, msm_host->src_clk_rate);
526 if (ret) {
527 pr_err("%s: Failed to set rate src clk, %d\n", __func__, ret);
528 goto error;
529 }
530
531 ret = clk_set_rate(msm_host->pixel_clk, msm_host->mode->clock * 1000);
532 if (ret) {
533 pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
534 goto error;
535 }
536
537 ret = clk_prepare_enable(msm_host->byte_clk);
538 if (ret) {
539 pr_err("%s: Failed to enable dsi byte clk\n", __func__);
540 goto error;
541 }
542
543 ret = clk_prepare_enable(msm_host->esc_clk);
544 if (ret) {
545 pr_err("%s: Failed to enable dsi esc clk\n", __func__);
546 goto esc_clk_err;
547 }
548
549 ret = clk_prepare_enable(msm_host->src_clk);
550 if (ret) {
551 pr_err("%s: Failed to enable dsi src clk\n", __func__);
552 goto src_clk_err;
553 }
554
555 ret = clk_prepare_enable(msm_host->pixel_clk);
556 if (ret) {
557 pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
558 goto pixel_clk_err;
559 }
560
561 return 0;
562
563pixel_clk_err:
564 clk_disable_unprepare(msm_host->src_clk);
565src_clk_err:
566 clk_disable_unprepare(msm_host->esc_clk);
567esc_clk_err:
568 clk_disable_unprepare(msm_host->byte_clk);
569error:
570 return ret;
571}
572
573static int dsi_link_clk_enable(struct msm_dsi_host *msm_host)
574{
575 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
576
577 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G)
578 return dsi_link_clk_enable_6g(msm_host);
579 else
580 return dsi_link_clk_enable_v2(msm_host);
581}
582
Hai Lia6895542015-03-31 14:36:33 -0400583static void dsi_link_clk_disable(struct msm_dsi_host *msm_host)
584{
Archit Taneja4bfa9742015-10-09 16:32:38 +0530585 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
586
587 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
588 clk_disable_unprepare(msm_host->esc_clk);
589 clk_disable_unprepare(msm_host->pixel_clk);
590 clk_disable_unprepare(msm_host->byte_clk);
591 } else {
592 clk_disable_unprepare(msm_host->pixel_clk);
593 clk_disable_unprepare(msm_host->src_clk);
594 clk_disable_unprepare(msm_host->esc_clk);
595 clk_disable_unprepare(msm_host->byte_clk);
596 }
Hai Lia6895542015-03-31 14:36:33 -0400597}
598
599static int dsi_clk_ctrl(struct msm_dsi_host *msm_host, bool enable)
600{
601 int ret = 0;
602
603 mutex_lock(&msm_host->clk_mutex);
604 if (enable) {
605 ret = dsi_bus_clk_enable(msm_host);
606 if (ret) {
607 pr_err("%s: Can not enable bus clk, %d\n",
608 __func__, ret);
609 goto unlock_ret;
610 }
611 ret = dsi_link_clk_enable(msm_host);
612 if (ret) {
613 pr_err("%s: Can not enable link clk, %d\n",
614 __func__, ret);
615 dsi_bus_clk_disable(msm_host);
616 goto unlock_ret;
617 }
618 } else {
619 dsi_link_clk_disable(msm_host);
620 dsi_bus_clk_disable(msm_host);
621 }
622
623unlock_ret:
624 mutex_unlock(&msm_host->clk_mutex);
625 return ret;
626}
627
628static int dsi_calc_clk_rate(struct msm_dsi_host *msm_host)
629{
630 struct drm_display_mode *mode = msm_host->mode;
Archit Taneja4bfa9742015-10-09 16:32:38 +0530631 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400632 u8 lanes = msm_host->lanes;
633 u32 bpp = dsi_get_bpp(msm_host->format);
634 u32 pclk_rate;
635
636 if (!mode) {
637 pr_err("%s: mode not set\n", __func__);
638 return -EINVAL;
639 }
640
641 pclk_rate = mode->clock * 1000;
642 if (lanes > 0) {
643 msm_host->byte_clk_rate = (pclk_rate * bpp) / (8 * lanes);
644 } else {
645 pr_err("%s: forcing mdss_dsi lanes to 1\n", __func__);
646 msm_host->byte_clk_rate = (pclk_rate * bpp) / 8;
647 }
648
649 DBG("pclk=%d, bclk=%d", pclk_rate, msm_host->byte_clk_rate);
650
Archit Taneja4bfa9742015-10-09 16:32:38 +0530651 msm_host->esc_clk_rate = clk_get_rate(msm_host->esc_clk);
652
653 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
654 unsigned int esc_mhz, esc_div;
655 unsigned long byte_mhz;
656
657 msm_host->src_clk_rate = (pclk_rate * bpp) / 8;
658
659 /*
660 * esc clock is byte clock followed by a 4 bit divider,
661 * we need to find an escape clock frequency within the
662 * mipi DSI spec range within the maximum divider limit
663 * We iterate here between an escape clock frequencey
664 * between 20 Mhz to 5 Mhz and pick up the first one
665 * that can be supported by our divider
666 */
667
668 byte_mhz = msm_host->byte_clk_rate / 1000000;
669
670 for (esc_mhz = 20; esc_mhz >= 5; esc_mhz--) {
671 esc_div = DIV_ROUND_UP(byte_mhz, esc_mhz);
672
673 /*
674 * TODO: Ideally, we shouldn't know what sort of divider
675 * is available in mmss_cc, we're just assuming that
676 * it'll always be a 4 bit divider. Need to come up with
677 * a better way here.
678 */
679 if (esc_div >= 1 && esc_div <= 16)
680 break;
681 }
682
683 if (esc_mhz < 5)
684 return -EINVAL;
685
686 msm_host->esc_clk_rate = msm_host->byte_clk_rate / esc_div;
687
688 DBG("esc=%d, src=%d", msm_host->esc_clk_rate,
689 msm_host->src_clk_rate);
690 }
691
Hai Lia6895542015-03-31 14:36:33 -0400692 return 0;
693}
694
Hai Lia6895542015-03-31 14:36:33 -0400695static void dsi_intr_ctrl(struct msm_dsi_host *msm_host, u32 mask, int enable)
696{
697 u32 intr;
698 unsigned long flags;
699
700 spin_lock_irqsave(&msm_host->intr_lock, flags);
701 intr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
702
703 if (enable)
704 intr |= mask;
705 else
706 intr &= ~mask;
707
708 DBG("intr=%x enable=%d", intr, enable);
709
710 dsi_write(msm_host, REG_DSI_INTR_CTRL, intr);
711 spin_unlock_irqrestore(&msm_host->intr_lock, flags);
712}
713
714static inline enum dsi_traffic_mode dsi_get_traffic_mode(const u32 mode_flags)
715{
716 if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
717 return BURST_MODE;
718 else if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
719 return NON_BURST_SYNCH_PULSE;
720
721 return NON_BURST_SYNCH_EVENT;
722}
723
724static inline enum dsi_vid_dst_format dsi_get_vid_fmt(
725 const enum mipi_dsi_pixel_format mipi_fmt)
726{
727 switch (mipi_fmt) {
728 case MIPI_DSI_FMT_RGB888: return VID_DST_FORMAT_RGB888;
729 case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666_LOOSE;
730 case MIPI_DSI_FMT_RGB666_PACKED: return VID_DST_FORMAT_RGB666;
731 case MIPI_DSI_FMT_RGB565: return VID_DST_FORMAT_RGB565;
732 default: return VID_DST_FORMAT_RGB888;
733 }
734}
735
736static inline enum dsi_cmd_dst_format dsi_get_cmd_fmt(
737 const enum mipi_dsi_pixel_format mipi_fmt)
738{
739 switch (mipi_fmt) {
740 case MIPI_DSI_FMT_RGB888: return CMD_DST_FORMAT_RGB888;
741 case MIPI_DSI_FMT_RGB666_PACKED:
742 case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666;
743 case MIPI_DSI_FMT_RGB565: return CMD_DST_FORMAT_RGB565;
744 default: return CMD_DST_FORMAT_RGB888;
745 }
746}
747
748static void dsi_ctrl_config(struct msm_dsi_host *msm_host, bool enable,
Hai Lidceac342016-09-15 14:34:49 +0530749 struct msm_dsi_phy_shared_timings *phy_shared_timings)
Hai Lia6895542015-03-31 14:36:33 -0400750{
751 u32 flags = msm_host->mode_flags;
752 enum mipi_dsi_pixel_format mipi_fmt = msm_host->format;
Hai Lid248b612015-08-13 17:49:29 -0400753 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400754 u32 data = 0;
755
756 if (!enable) {
757 dsi_write(msm_host, REG_DSI_CTRL, 0);
758 return;
759 }
760
761 if (flags & MIPI_DSI_MODE_VIDEO) {
762 if (flags & MIPI_DSI_MODE_VIDEO_HSE)
763 data |= DSI_VID_CFG0_PULSE_MODE_HSA_HE;
764 if (flags & MIPI_DSI_MODE_VIDEO_HFP)
765 data |= DSI_VID_CFG0_HFP_POWER_STOP;
766 if (flags & MIPI_DSI_MODE_VIDEO_HBP)
767 data |= DSI_VID_CFG0_HBP_POWER_STOP;
768 if (flags & MIPI_DSI_MODE_VIDEO_HSA)
769 data |= DSI_VID_CFG0_HSA_POWER_STOP;
770 /* Always set low power stop mode for BLLP
771 * to let command engine send packets
772 */
773 data |= DSI_VID_CFG0_EOF_BLLP_POWER_STOP |
774 DSI_VID_CFG0_BLLP_POWER_STOP;
775 data |= DSI_VID_CFG0_TRAFFIC_MODE(dsi_get_traffic_mode(flags));
776 data |= DSI_VID_CFG0_DST_FORMAT(dsi_get_vid_fmt(mipi_fmt));
777 data |= DSI_VID_CFG0_VIRT_CHANNEL(msm_host->channel);
778 dsi_write(msm_host, REG_DSI_VID_CFG0, data);
779
780 /* Do not swap RGB colors */
781 data = DSI_VID_CFG1_RGB_SWAP(SWAP_RGB);
782 dsi_write(msm_host, REG_DSI_VID_CFG1, 0);
783 } else {
784 /* Do not swap RGB colors */
785 data = DSI_CMD_CFG0_RGB_SWAP(SWAP_RGB);
786 data |= DSI_CMD_CFG0_DST_FORMAT(dsi_get_cmd_fmt(mipi_fmt));
787 dsi_write(msm_host, REG_DSI_CMD_CFG0, data);
788
789 data = DSI_CMD_CFG1_WR_MEM_START(MIPI_DCS_WRITE_MEMORY_START) |
790 DSI_CMD_CFG1_WR_MEM_CONTINUE(
791 MIPI_DCS_WRITE_MEMORY_CONTINUE);
792 /* Always insert DCS command */
793 data |= DSI_CMD_CFG1_INSERT_DCS_COMMAND;
794 dsi_write(msm_host, REG_DSI_CMD_CFG1, data);
795 }
796
797 dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL,
798 DSI_CMD_DMA_CTRL_FROM_FRAME_BUFFER |
799 DSI_CMD_DMA_CTRL_LOW_POWER);
800
801 data = 0;
802 /* Always assume dedicated TE pin */
803 data |= DSI_TRIG_CTRL_TE;
804 data |= DSI_TRIG_CTRL_MDP_TRIGGER(TRIGGER_NONE);
805 data |= DSI_TRIG_CTRL_DMA_TRIGGER(TRIGGER_SW);
806 data |= DSI_TRIG_CTRL_STREAM(msm_host->channel);
Hai Lid248b612015-08-13 17:49:29 -0400807 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
808 (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_2))
Hai Lia6895542015-03-31 14:36:33 -0400809 data |= DSI_TRIG_CTRL_BLOCK_DMA_WITHIN_FRAME;
810 dsi_write(msm_host, REG_DSI_TRIG_CTRL, data);
811
Hai Lidceac342016-09-15 14:34:49 +0530812 data = DSI_CLKOUT_TIMING_CTRL_T_CLK_POST(phy_shared_timings->clk_post) |
813 DSI_CLKOUT_TIMING_CTRL_T_CLK_PRE(phy_shared_timings->clk_pre);
Hai Lia6895542015-03-31 14:36:33 -0400814 dsi_write(msm_host, REG_DSI_CLKOUT_TIMING_CTRL, data);
815
Hai Lidceac342016-09-15 14:34:49 +0530816 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
817 (cfg_hnd->minor > MSM_DSI_6G_VER_MINOR_V1_0) &&
818 phy_shared_timings->clk_pre_inc_by_2)
819 dsi_write(msm_host, REG_DSI_T_CLK_PRE_EXTEND,
820 DSI_T_CLK_PRE_EXTEND_INC_BY_2_BYTECLK);
821
Hai Lia6895542015-03-31 14:36:33 -0400822 data = 0;
823 if (!(flags & MIPI_DSI_MODE_EOT_PACKET))
824 data |= DSI_EOT_PACKET_CTRL_TX_EOT_APPEND;
825 dsi_write(msm_host, REG_DSI_EOT_PACKET_CTRL, data);
826
827 /* allow only ack-err-status to generate interrupt */
828 dsi_write(msm_host, REG_DSI_ERR_INT_MASK0, 0x13ff3fe0);
829
830 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
831
832 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
833
834 data = DSI_CTRL_CLK_EN;
835
836 DBG("lane number=%d", msm_host->lanes);
Archit Taneja26f7d1f2016-02-25 11:19:48 +0530837 data |= ((DSI_CTRL_LANE0 << msm_host->lanes) - DSI_CTRL_LANE0);
838
839 dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
840 DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(msm_host->dlane_swap));
Archit Taneja65c5e542015-04-08 11:37:40 +0530841
842 if (!(flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
843 dsi_write(msm_host, REG_DSI_LANE_CTRL,
844 DSI_LANE_CTRL_CLKLN_HS_FORCE_REQUEST);
845
Hai Lia6895542015-03-31 14:36:33 -0400846 data |= DSI_CTRL_ENABLE;
847
848 dsi_write(msm_host, REG_DSI_CTRL, data);
849}
850
851static void dsi_timing_setup(struct msm_dsi_host *msm_host)
852{
853 struct drm_display_mode *mode = msm_host->mode;
854 u32 hs_start = 0, vs_start = 0; /* take sync start as 0 */
855 u32 h_total = mode->htotal;
856 u32 v_total = mode->vtotal;
857 u32 hs_end = mode->hsync_end - mode->hsync_start;
858 u32 vs_end = mode->vsync_end - mode->vsync_start;
859 u32 ha_start = h_total - mode->hsync_start;
860 u32 ha_end = ha_start + mode->hdisplay;
861 u32 va_start = v_total - mode->vsync_start;
862 u32 va_end = va_start + mode->vdisplay;
863 u32 wc;
864
865 DBG("");
866
867 if (msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) {
868 dsi_write(msm_host, REG_DSI_ACTIVE_H,
869 DSI_ACTIVE_H_START(ha_start) |
870 DSI_ACTIVE_H_END(ha_end));
871 dsi_write(msm_host, REG_DSI_ACTIVE_V,
872 DSI_ACTIVE_V_START(va_start) |
873 DSI_ACTIVE_V_END(va_end));
874 dsi_write(msm_host, REG_DSI_TOTAL,
875 DSI_TOTAL_H_TOTAL(h_total - 1) |
876 DSI_TOTAL_V_TOTAL(v_total - 1));
877
878 dsi_write(msm_host, REG_DSI_ACTIVE_HSYNC,
879 DSI_ACTIVE_HSYNC_START(hs_start) |
880 DSI_ACTIVE_HSYNC_END(hs_end));
881 dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_HPOS, 0);
882 dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_VPOS,
883 DSI_ACTIVE_VSYNC_VPOS_START(vs_start) |
884 DSI_ACTIVE_VSYNC_VPOS_END(vs_end));
885 } else { /* command mode */
886 /* image data and 1 byte write_memory_start cmd */
887 wc = mode->hdisplay * dsi_get_bpp(msm_host->format) / 8 + 1;
888
889 dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_CTRL,
890 DSI_CMD_MDP_STREAM_CTRL_WORD_COUNT(wc) |
891 DSI_CMD_MDP_STREAM_CTRL_VIRTUAL_CHANNEL(
892 msm_host->channel) |
893 DSI_CMD_MDP_STREAM_CTRL_DATA_TYPE(
894 MIPI_DSI_DCS_LONG_WRITE));
895
896 dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_TOTAL,
897 DSI_CMD_MDP_STREAM_TOTAL_H_TOTAL(mode->hdisplay) |
898 DSI_CMD_MDP_STREAM_TOTAL_V_TOTAL(mode->vdisplay));
899 }
900}
901
902static void dsi_sw_reset(struct msm_dsi_host *msm_host)
903{
904 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
905 wmb(); /* clocks need to be enabled before reset */
906
907 dsi_write(msm_host, REG_DSI_RESET, 1);
908 wmb(); /* make sure reset happen */
909 dsi_write(msm_host, REG_DSI_RESET, 0);
910}
911
912static void dsi_op_mode_config(struct msm_dsi_host *msm_host,
913 bool video_mode, bool enable)
914{
915 u32 dsi_ctrl;
916
917 dsi_ctrl = dsi_read(msm_host, REG_DSI_CTRL);
918
919 if (!enable) {
920 dsi_ctrl &= ~(DSI_CTRL_ENABLE | DSI_CTRL_VID_MODE_EN |
921 DSI_CTRL_CMD_MODE_EN);
922 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE |
923 DSI_IRQ_MASK_VIDEO_DONE, 0);
924 } else {
925 if (video_mode) {
926 dsi_ctrl |= DSI_CTRL_VID_MODE_EN;
927 } else { /* command mode */
928 dsi_ctrl |= DSI_CTRL_CMD_MODE_EN;
929 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE, 1);
930 }
931 dsi_ctrl |= DSI_CTRL_ENABLE;
932 }
933
934 dsi_write(msm_host, REG_DSI_CTRL, dsi_ctrl);
935}
936
937static void dsi_set_tx_power_mode(int mode, struct msm_dsi_host *msm_host)
938{
939 u32 data;
940
941 data = dsi_read(msm_host, REG_DSI_CMD_DMA_CTRL);
942
943 if (mode == 0)
944 data &= ~DSI_CMD_DMA_CTRL_LOW_POWER;
945 else
946 data |= DSI_CMD_DMA_CTRL_LOW_POWER;
947
948 dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL, data);
949}
950
951static void dsi_wait4video_done(struct msm_dsi_host *msm_host)
952{
953 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 1);
954
955 reinit_completion(&msm_host->video_comp);
956
957 wait_for_completion_timeout(&msm_host->video_comp,
958 msecs_to_jiffies(70));
959
960 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 0);
961}
962
963static void dsi_wait4video_eng_busy(struct msm_dsi_host *msm_host)
964{
965 if (!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO))
966 return;
967
968 if (msm_host->power_on) {
969 dsi_wait4video_done(msm_host);
970 /* delay 4 ms to skip BLLP */
971 usleep_range(2000, 4000);
972 }
973}
974
975/* dsi_cmd */
976static int dsi_tx_buf_alloc(struct msm_dsi_host *msm_host, int size)
977{
978 struct drm_device *dev = msm_host->dev;
Rob Clarkf59f62d2017-06-13 10:22:37 -0400979 struct msm_drm_private *priv = dev->dev_private;
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530980 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400981 int ret;
Rob Clark78babc12016-11-11 12:06:46 -0500982 uint64_t iova;
Hai Lia6895542015-03-31 14:36:33 -0400983
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530984 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
985 mutex_lock(&dev->struct_mutex);
986 msm_host->tx_gem_obj = msm_gem_new(dev, size, MSM_BO_UNCACHED);
987 if (IS_ERR(msm_host->tx_gem_obj)) {
988 ret = PTR_ERR(msm_host->tx_gem_obj);
989 pr_err("%s: failed to allocate gem, %d\n",
990 __func__, ret);
991 msm_host->tx_gem_obj = NULL;
992 mutex_unlock(&dev->struct_mutex);
993 return ret;
994 }
995
Rob Clarkf59f62d2017-06-13 10:22:37 -0400996 ret = msm_gem_get_iova_locked(msm_host->tx_gem_obj,
Rob Clark8bdcd942017-06-13 11:07:08 -0400997 priv->kms->aspace, &iova);
saurabhbeb107f2015-12-07 01:19:21 +0530998 mutex_unlock(&dev->struct_mutex);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530999 if (ret) {
1000 pr_err("%s: failed to get iova, %d\n", __func__, ret);
1001 return ret;
1002 }
Hai Lia6895542015-03-31 14:36:33 -04001003
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301004 if (iova & 0x07) {
1005 pr_err("%s: buf NOT 8 bytes aligned\n", __func__);
1006 return -EINVAL;
1007 }
Hai Lia6895542015-03-31 14:36:33 -04001008
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301009 msm_host->tx_size = msm_host->tx_gem_obj->size;
1010 } else {
1011 msm_host->tx_buf = dma_alloc_coherent(dev->dev, size,
1012 &msm_host->tx_buf_paddr, GFP_KERNEL);
1013 if (!msm_host->tx_buf) {
1014 ret = -ENOMEM;
1015 pr_err("%s: failed to allocate tx buf, %d\n",
1016 __func__, ret);
1017 return ret;
1018 }
1019
1020 msm_host->tx_size = size;
Hai Lia6895542015-03-31 14:36:33 -04001021 }
1022
1023 return 0;
1024}
1025
1026static void dsi_tx_buf_free(struct msm_dsi_host *msm_host)
1027{
1028 struct drm_device *dev = msm_host->dev;
1029
1030 if (msm_host->tx_gem_obj) {
1031 msm_gem_put_iova(msm_host->tx_gem_obj, 0);
1032 mutex_lock(&dev->struct_mutex);
1033 msm_gem_free_object(msm_host->tx_gem_obj);
1034 msm_host->tx_gem_obj = NULL;
1035 mutex_unlock(&dev->struct_mutex);
1036 }
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301037
1038 if (msm_host->tx_buf)
1039 dma_free_coherent(dev->dev, msm_host->tx_size, msm_host->tx_buf,
1040 msm_host->tx_buf_paddr);
Hai Lia6895542015-03-31 14:36:33 -04001041}
1042
1043/*
1044 * prepare cmd buffer to be txed
1045 */
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301046static int dsi_cmd_dma_add(struct msm_dsi_host *msm_host,
1047 const struct mipi_dsi_msg *msg)
Hai Lia6895542015-03-31 14:36:33 -04001048{
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301049 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -04001050 struct mipi_dsi_packet packet;
1051 int len;
1052 int ret;
1053 u8 *data;
1054
1055 ret = mipi_dsi_create_packet(&packet, msg);
1056 if (ret) {
1057 pr_err("%s: create packet failed, %d\n", __func__, ret);
1058 return ret;
1059 }
1060 len = (packet.size + 3) & (~0x3);
1061
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301062 if (len > msm_host->tx_size) {
Hai Lia6895542015-03-31 14:36:33 -04001063 pr_err("%s: packet size is too big\n", __func__);
1064 return -EINVAL;
1065 }
1066
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301067 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
Rob Clark18f23042016-05-26 16:24:35 -04001068 data = msm_gem_get_vaddr(msm_host->tx_gem_obj);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301069 if (IS_ERR(data)) {
1070 ret = PTR_ERR(data);
1071 pr_err("%s: get vaddr failed, %d\n", __func__, ret);
1072 return ret;
1073 }
1074 } else {
1075 data = msm_host->tx_buf;
Hai Lia6895542015-03-31 14:36:33 -04001076 }
1077
1078 /* MSM specific command format in memory */
1079 data[0] = packet.header[1];
1080 data[1] = packet.header[2];
1081 data[2] = packet.header[0];
1082 data[3] = BIT(7); /* Last packet */
1083 if (mipi_dsi_packet_format_is_long(msg->type))
1084 data[3] |= BIT(6);
1085 if (msg->rx_buf && msg->rx_len)
1086 data[3] |= BIT(5);
1087
1088 /* Long packet */
1089 if (packet.payload && packet.payload_length)
1090 memcpy(data + 4, packet.payload, packet.payload_length);
1091
1092 /* Append 0xff to the end */
1093 if (packet.size < len)
1094 memset(data + packet.size, 0xff, len - packet.size);
1095
Rob Clark18f23042016-05-26 16:24:35 -04001096 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G)
1097 msm_gem_put_vaddr(msm_host->tx_gem_obj);
1098
Hai Lia6895542015-03-31 14:36:33 -04001099 return len;
1100}
1101
1102/*
1103 * dsi_short_read1_resp: 1 parameter
1104 */
1105static int dsi_short_read1_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1106{
1107 u8 *data = msg->rx_buf;
1108 if (data && (msg->rx_len >= 1)) {
1109 *data = buf[1]; /* strip out dcs type */
1110 return 1;
1111 } else {
Stephane Viau981371f2015-04-30 10:39:26 -04001112 pr_err("%s: read data does not match with rx_buf len %zu\n",
Hai Lia6895542015-03-31 14:36:33 -04001113 __func__, msg->rx_len);
1114 return -EINVAL;
1115 }
1116}
1117
1118/*
1119 * dsi_short_read2_resp: 2 parameter
1120 */
1121static int dsi_short_read2_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1122{
1123 u8 *data = msg->rx_buf;
1124 if (data && (msg->rx_len >= 2)) {
1125 data[0] = buf[1]; /* strip out dcs type */
1126 data[1] = buf[2];
1127 return 2;
1128 } else {
Stephane Viau981371f2015-04-30 10:39:26 -04001129 pr_err("%s: read data does not match with rx_buf len %zu\n",
Hai Lia6895542015-03-31 14:36:33 -04001130 __func__, msg->rx_len);
1131 return -EINVAL;
1132 }
1133}
1134
1135static int dsi_long_read_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1136{
1137 /* strip out 4 byte dcs header */
1138 if (msg->rx_buf && msg->rx_len)
1139 memcpy(msg->rx_buf, buf + 4, msg->rx_len);
1140
1141 return msg->rx_len;
1142}
1143
Hai Lia6895542015-03-31 14:36:33 -04001144static int dsi_cmd_dma_tx(struct msm_dsi_host *msm_host, int len)
1145{
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301146 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Rob Clarkf59f62d2017-06-13 10:22:37 -04001147 struct drm_device *dev = msm_host->dev;
1148 struct msm_drm_private *priv = dev->dev_private;
Hai Lia6895542015-03-31 14:36:33 -04001149 int ret;
Rob Clark78babc12016-11-11 12:06:46 -05001150 uint64_t dma_base;
Hai Lia6895542015-03-31 14:36:33 -04001151 bool triggered;
1152
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301153 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
Rob Clarkf59f62d2017-06-13 10:22:37 -04001154 ret = msm_gem_get_iova(msm_host->tx_gem_obj,
Rob Clark8bdcd942017-06-13 11:07:08 -04001155 priv->kms->aspace, &dma_base);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301156 if (ret) {
1157 pr_err("%s: failed to get iova: %d\n", __func__, ret);
1158 return ret;
1159 }
1160 } else {
1161 dma_base = msm_host->tx_buf_paddr;
Hai Lia6895542015-03-31 14:36:33 -04001162 }
1163
1164 reinit_completion(&msm_host->dma_comp);
1165
1166 dsi_wait4video_eng_busy(msm_host);
1167
1168 triggered = msm_dsi_manager_cmd_xfer_trigger(
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301169 msm_host->id, dma_base, len);
Hai Lia6895542015-03-31 14:36:33 -04001170 if (triggered) {
1171 ret = wait_for_completion_timeout(&msm_host->dma_comp,
1172 msecs_to_jiffies(200));
1173 DBG("ret=%d", ret);
1174 if (ret == 0)
1175 ret = -ETIMEDOUT;
1176 else
1177 ret = len;
1178 } else
1179 ret = len;
1180
1181 return ret;
1182}
1183
1184static int dsi_cmd_dma_rx(struct msm_dsi_host *msm_host,
1185 u8 *buf, int rx_byte, int pkt_size)
1186{
1187 u32 *lp, *temp, data;
1188 int i, j = 0, cnt;
Hai Lia6895542015-03-31 14:36:33 -04001189 u32 read_cnt;
1190 u8 reg[16];
1191 int repeated_bytes = 0;
1192 int buf_offset = buf - msm_host->rx_buf;
1193
1194 lp = (u32 *)buf;
1195 temp = (u32 *)reg;
1196 cnt = (rx_byte + 3) >> 2;
1197 if (cnt > 4)
1198 cnt = 4; /* 4 x 32 bits registers only */
1199
Hai Liec1936e2015-04-29 11:39:00 -04001200 if (rx_byte == 4)
1201 read_cnt = 4;
1202 else
1203 read_cnt = pkt_size + 6;
Hai Lia6895542015-03-31 14:36:33 -04001204
1205 /*
1206 * In case of multiple reads from the panel, after the first read, there
1207 * is possibility that there are some bytes in the payload repeating in
1208 * the RDBK_DATA registers. Since we read all the parameters from the
1209 * panel right from the first byte for every pass. We need to skip the
1210 * repeating bytes and then append the new parameters to the rx buffer.
1211 */
1212 if (read_cnt > 16) {
1213 int bytes_shifted;
1214 /* Any data more than 16 bytes will be shifted out.
1215 * The temp read buffer should already contain these bytes.
1216 * The remaining bytes in read buffer are the repeated bytes.
1217 */
1218 bytes_shifted = read_cnt - 16;
1219 repeated_bytes = buf_offset - bytes_shifted;
1220 }
1221
1222 for (i = cnt - 1; i >= 0; i--) {
1223 data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
1224 *temp++ = ntohl(data); /* to host byte order */
1225 DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
1226 }
1227
1228 for (i = repeated_bytes; i < 16; i++)
1229 buf[j++] = reg[i];
1230
1231 return j;
1232}
1233
1234static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
1235 const struct mipi_dsi_msg *msg)
1236{
1237 int len, ret;
1238 int bllp_len = msm_host->mode->hdisplay *
1239 dsi_get_bpp(msm_host->format) / 8;
1240
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301241 len = dsi_cmd_dma_add(msm_host, msg);
Hai Lia6895542015-03-31 14:36:33 -04001242 if (!len) {
1243 pr_err("%s: failed to add cmd type = 0x%x\n",
1244 __func__, msg->type);
1245 return -EINVAL;
1246 }
1247
1248 /* for video mode, do not send cmds more than
1249 * one pixel line, since it only transmit it
1250 * during BLLP.
1251 */
1252 /* TODO: if the command is sent in LP mode, the bit rate is only
1253 * half of esc clk rate. In this case, if the video is already
1254 * actively streaming, we need to check more carefully if the
1255 * command can be fit into one BLLP.
1256 */
1257 if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
1258 pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
1259 __func__, len);
1260 return -EINVAL;
1261 }
1262
1263 ret = dsi_cmd_dma_tx(msm_host, len);
1264 if (ret < len) {
1265 pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d\n",
1266 __func__, msg->type, (*(u8 *)(msg->tx_buf)), len);
1267 return -ECOMM;
1268 }
1269
1270 return len;
1271}
1272
1273static void dsi_sw_reset_restore(struct msm_dsi_host *msm_host)
1274{
1275 u32 data0, data1;
1276
1277 data0 = dsi_read(msm_host, REG_DSI_CTRL);
1278 data1 = data0;
1279 data1 &= ~DSI_CTRL_ENABLE;
1280 dsi_write(msm_host, REG_DSI_CTRL, data1);
1281 /*
1282 * dsi controller need to be disabled before
1283 * clocks turned on
1284 */
1285 wmb();
1286
1287 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
1288 wmb(); /* make sure clocks enabled */
1289
1290 /* dsi controller can only be reset while clocks are running */
1291 dsi_write(msm_host, REG_DSI_RESET, 1);
1292 wmb(); /* make sure reset happen */
1293 dsi_write(msm_host, REG_DSI_RESET, 0);
1294 wmb(); /* controller out of reset */
1295 dsi_write(msm_host, REG_DSI_CTRL, data0);
1296 wmb(); /* make sure dsi controller enabled again */
1297}
1298
Archit Taneja8d23ea42016-10-25 12:17:59 +05301299static void dsi_hpd_worker(struct work_struct *work)
1300{
1301 struct msm_dsi_host *msm_host =
1302 container_of(work, struct msm_dsi_host, hpd_work);
1303
1304 drm_helper_hpd_irq_event(msm_host->dev);
1305}
1306
Hai Lia6895542015-03-31 14:36:33 -04001307static void dsi_err_worker(struct work_struct *work)
1308{
1309 struct msm_dsi_host *msm_host =
1310 container_of(work, struct msm_dsi_host, err_work);
1311 u32 status = msm_host->err_work_state;
1312
Rob Clarkff431fa2015-05-07 15:19:02 -04001313 pr_err_ratelimited("%s: status=%x\n", __func__, status);
Hai Lia6895542015-03-31 14:36:33 -04001314 if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
1315 dsi_sw_reset_restore(msm_host);
1316
1317 /* It is safe to clear here because error irq is disabled. */
1318 msm_host->err_work_state = 0;
1319
1320 /* enable dsi error interrupt */
1321 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
1322}
1323
1324static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
1325{
1326 u32 status;
1327
1328 status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
1329
1330 if (status) {
1331 dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
1332 /* Writing of an extra 0 needed to clear error bits */
1333 dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
1334 msm_host->err_work_state |= DSI_ERR_STATE_ACK;
1335 }
1336}
1337
1338static void dsi_timeout_status(struct msm_dsi_host *msm_host)
1339{
1340 u32 status;
1341
1342 status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
1343
1344 if (status) {
1345 dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
1346 msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
1347 }
1348}
1349
1350static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
1351{
1352 u32 status;
1353
1354 status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
1355
Archit Taneja01199362015-06-25 11:29:24 +05301356 if (status & (DSI_DLN0_PHY_ERR_DLN0_ERR_ESC |
1357 DSI_DLN0_PHY_ERR_DLN0_ERR_SYNC_ESC |
1358 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTROL |
1359 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP0 |
1360 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP1)) {
Hai Lia6895542015-03-31 14:36:33 -04001361 dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
1362 msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
1363 }
1364}
1365
1366static void dsi_fifo_status(struct msm_dsi_host *msm_host)
1367{
1368 u32 status;
1369
1370 status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
1371
1372 /* fifo underflow, overflow */
1373 if (status) {
1374 dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
1375 msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
1376 if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
1377 msm_host->err_work_state |=
1378 DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
1379 }
1380}
1381
1382static void dsi_status(struct msm_dsi_host *msm_host)
1383{
1384 u32 status;
1385
1386 status = dsi_read(msm_host, REG_DSI_STATUS0);
1387
1388 if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
1389 dsi_write(msm_host, REG_DSI_STATUS0, status);
1390 msm_host->err_work_state |=
1391 DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
1392 }
1393}
1394
1395static void dsi_clk_status(struct msm_dsi_host *msm_host)
1396{
1397 u32 status;
1398
1399 status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
1400
1401 if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
1402 dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
1403 msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
1404 }
1405}
1406
1407static void dsi_error(struct msm_dsi_host *msm_host)
1408{
1409 /* disable dsi error interrupt */
1410 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
1411
1412 dsi_clk_status(msm_host);
1413 dsi_fifo_status(msm_host);
1414 dsi_ack_err_status(msm_host);
1415 dsi_timeout_status(msm_host);
1416 dsi_status(msm_host);
1417 dsi_dln0_phy_err(msm_host);
1418
1419 queue_work(msm_host->workqueue, &msm_host->err_work);
1420}
1421
1422static irqreturn_t dsi_host_irq(int irq, void *ptr)
1423{
1424 struct msm_dsi_host *msm_host = ptr;
1425 u32 isr;
1426 unsigned long flags;
1427
1428 if (!msm_host->ctrl_base)
1429 return IRQ_HANDLED;
1430
1431 spin_lock_irqsave(&msm_host->intr_lock, flags);
1432 isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
1433 dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
1434 spin_unlock_irqrestore(&msm_host->intr_lock, flags);
1435
1436 DBG("isr=0x%x, id=%d", isr, msm_host->id);
1437
1438 if (isr & DSI_IRQ_ERROR)
1439 dsi_error(msm_host);
1440
1441 if (isr & DSI_IRQ_VIDEO_DONE)
1442 complete(&msm_host->video_comp);
1443
1444 if (isr & DSI_IRQ_CMD_DMA_DONE)
1445 complete(&msm_host->dma_comp);
1446
1447 return IRQ_HANDLED;
1448}
1449
1450static int dsi_host_init_panel_gpios(struct msm_dsi_host *msm_host,
1451 struct device *panel_device)
1452{
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001453 msm_host->disp_en_gpio = devm_gpiod_get_optional(panel_device,
1454 "disp-enable",
1455 GPIOD_OUT_LOW);
Hai Lia6895542015-03-31 14:36:33 -04001456 if (IS_ERR(msm_host->disp_en_gpio)) {
1457 DBG("cannot get disp-enable-gpios %ld",
1458 PTR_ERR(msm_host->disp_en_gpio));
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001459 return PTR_ERR(msm_host->disp_en_gpio);
Hai Lia6895542015-03-31 14:36:33 -04001460 }
1461
Archit Taneja60d05cb2015-06-25 14:36:35 +05301462 msm_host->te_gpio = devm_gpiod_get_optional(panel_device, "disp-te",
1463 GPIOD_IN);
Hai Lia6895542015-03-31 14:36:33 -04001464 if (IS_ERR(msm_host->te_gpio)) {
1465 DBG("cannot get disp-te-gpios %ld", PTR_ERR(msm_host->te_gpio));
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001466 return PTR_ERR(msm_host->te_gpio);
Hai Lia6895542015-03-31 14:36:33 -04001467 }
1468
1469 return 0;
1470}
1471
1472static int dsi_host_attach(struct mipi_dsi_host *host,
1473 struct mipi_dsi_device *dsi)
1474{
1475 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1476 int ret;
1477
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301478 if (dsi->lanes > msm_host->num_data_lanes)
1479 return -EINVAL;
1480
Hai Lia6895542015-03-31 14:36:33 -04001481 msm_host->channel = dsi->channel;
1482 msm_host->lanes = dsi->lanes;
1483 msm_host->format = dsi->format;
1484 msm_host->mode_flags = dsi->mode_flags;
1485
Archit Taneja9c9f6f82016-12-05 15:24:53 +05301486 msm_dsi_manager_attach_dsi_device(msm_host->id, dsi->mode_flags);
1487
Hai Lia6895542015-03-31 14:36:33 -04001488 /* Some gpios defined in panel DT need to be controlled by host */
1489 ret = dsi_host_init_panel_gpios(msm_host, &dsi->dev);
1490 if (ret)
1491 return ret;
1492
1493 DBG("id=%d", msm_host->id);
1494 if (msm_host->dev)
Archit Taneja8d23ea42016-10-25 12:17:59 +05301495 queue_work(msm_host->workqueue, &msm_host->hpd_work);
Hai Lia6895542015-03-31 14:36:33 -04001496
1497 return 0;
1498}
1499
1500static int dsi_host_detach(struct mipi_dsi_host *host,
1501 struct mipi_dsi_device *dsi)
1502{
1503 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1504
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301505 msm_host->device_node = NULL;
Hai Lia6895542015-03-31 14:36:33 -04001506
1507 DBG("id=%d", msm_host->id);
1508 if (msm_host->dev)
Archit Taneja8d23ea42016-10-25 12:17:59 +05301509 queue_work(msm_host->workqueue, &msm_host->hpd_work);
Hai Lia6895542015-03-31 14:36:33 -04001510
1511 return 0;
1512}
1513
1514static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
1515 const struct mipi_dsi_msg *msg)
1516{
1517 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1518 int ret;
1519
1520 if (!msg || !msm_host->power_on)
1521 return -EINVAL;
1522
1523 mutex_lock(&msm_host->cmd_mutex);
1524 ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
1525 mutex_unlock(&msm_host->cmd_mutex);
1526
1527 return ret;
1528}
1529
1530static struct mipi_dsi_host_ops dsi_host_ops = {
1531 .attach = dsi_host_attach,
1532 .detach = dsi_host_detach,
1533 .transfer = dsi_host_transfer,
1534};
1535
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301536/*
1537 * List of supported physical to logical lane mappings.
1538 * For example, the 2nd entry represents the following mapping:
1539 *
1540 * "3012": Logic 3->Phys 0; Logic 0->Phys 1; Logic 1->Phys 2; Logic 2->Phys 3;
1541 */
1542static const int supported_data_lane_swaps[][4] = {
1543 { 0, 1, 2, 3 },
1544 { 3, 0, 1, 2 },
1545 { 2, 3, 0, 1 },
1546 { 1, 2, 3, 0 },
1547 { 0, 3, 2, 1 },
1548 { 1, 0, 3, 2 },
1549 { 2, 1, 0, 3 },
1550 { 3, 2, 1, 0 },
1551};
1552
1553static int dsi_host_parse_lane_data(struct msm_dsi_host *msm_host,
1554 struct device_node *ep)
1555{
1556 struct device *dev = &msm_host->pdev->dev;
1557 struct property *prop;
1558 u32 lane_map[4];
1559 int ret, i, len, num_lanes;
1560
Archit Taneja60282ce2016-06-08 16:14:19 +05301561 prop = of_find_property(ep, "data-lanes", &len);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301562 if (!prop) {
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301563 dev_dbg(dev,
1564 "failed to find data lane mapping, using default\n");
1565 return 0;
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301566 }
1567
1568 num_lanes = len / sizeof(u32);
1569
1570 if (num_lanes < 1 || num_lanes > 4) {
1571 dev_err(dev, "bad number of data lanes\n");
1572 return -EINVAL;
1573 }
1574
1575 msm_host->num_data_lanes = num_lanes;
1576
Archit Taneja60282ce2016-06-08 16:14:19 +05301577 ret = of_property_read_u32_array(ep, "data-lanes", lane_map,
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301578 num_lanes);
1579 if (ret) {
1580 dev_err(dev, "failed to read lane data\n");
1581 return ret;
1582 }
1583
1584 /*
1585 * compare DT specified physical-logical lane mappings with the ones
1586 * supported by hardware
1587 */
1588 for (i = 0; i < ARRAY_SIZE(supported_data_lane_swaps); i++) {
1589 const int *swap = supported_data_lane_swaps[i];
1590 int j;
1591
Archit Taneja60282ce2016-06-08 16:14:19 +05301592 /*
1593 * the data-lanes array we get from DT has a logical->physical
1594 * mapping. The "data lane swap" register field represents
1595 * supported configurations in a physical->logical mapping.
1596 * Translate the DT mapping to what we understand and find a
1597 * configuration that works.
1598 */
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301599 for (j = 0; j < num_lanes; j++) {
Archit Taneja60282ce2016-06-08 16:14:19 +05301600 if (lane_map[j] < 0 || lane_map[j] > 3)
1601 dev_err(dev, "bad physical lane entry %u\n",
1602 lane_map[j]);
1603
1604 if (swap[lane_map[j]] != j)
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301605 break;
1606 }
1607
1608 if (j == num_lanes) {
1609 msm_host->dlane_swap = i;
1610 return 0;
1611 }
1612 }
1613
1614 return -EINVAL;
1615}
1616
Archit Tanejaf7009d22015-06-25 11:43:40 +05301617static int dsi_host_parse_dt(struct msm_dsi_host *msm_host)
1618{
1619 struct device *dev = &msm_host->pdev->dev;
1620 struct device_node *np = dev->of_node;
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301621 struct device_node *endpoint, *device_node;
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301622 int ret = 0;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301623
Archit Tanejaf7009d22015-06-25 11:43:40 +05301624 /*
Archit Tanejab9ac76f2016-04-27 15:36:53 +05301625 * Get the endpoint of the output port of the DSI host. In our case,
1626 * this is mapped to port number with reg = 1. Don't return an error if
1627 * the remote endpoint isn't defined. It's possible that there is
1628 * nothing connected to the dsi output.
Archit Tanejaf7009d22015-06-25 11:43:40 +05301629 */
Archit Tanejab9ac76f2016-04-27 15:36:53 +05301630 endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);
Archit Tanejaf7009d22015-06-25 11:43:40 +05301631 if (!endpoint) {
1632 dev_dbg(dev, "%s: no endpoint\n", __func__);
1633 return 0;
1634 }
1635
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301636 ret = dsi_host_parse_lane_data(msm_host, endpoint);
1637 if (ret) {
1638 dev_err(dev, "%s: invalid lane configuration %d\n",
1639 __func__, ret);
1640 goto err;
1641 }
1642
Archit Tanejaf7009d22015-06-25 11:43:40 +05301643 /* Get panel node from the output port's endpoint data */
Rob Herring86418f92017-03-22 08:26:06 -05001644 device_node = of_graph_get_remote_node(np, 1, 0);
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301645 if (!device_node) {
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301646 dev_dbg(dev, "%s: no valid device\n", __func__);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301647 goto err;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301648 }
1649
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301650 msm_host->device_node = device_node;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301651
Archit Taneja0c7df472015-10-14 15:31:13 +05301652 if (of_property_read_bool(np, "syscon-sfpb")) {
1653 msm_host->sfpb = syscon_regmap_lookup_by_phandle(np,
1654 "syscon-sfpb");
1655 if (IS_ERR(msm_host->sfpb)) {
1656 dev_err(dev, "%s: failed to get sfpb regmap\n",
1657 __func__);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301658 ret = PTR_ERR(msm_host->sfpb);
Archit Taneja0c7df472015-10-14 15:31:13 +05301659 }
1660 }
1661
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301662 of_node_put(device_node);
1663
1664err:
1665 of_node_put(endpoint);
1666
1667 return ret;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301668}
1669
Archit Taneja32280d62016-06-23 15:26:04 +05301670static int dsi_host_get_id(struct msm_dsi_host *msm_host)
1671{
1672 struct platform_device *pdev = msm_host->pdev;
1673 const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
1674 struct resource *res;
1675 int i;
1676
1677 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_ctrl");
1678 if (!res)
1679 return -EINVAL;
1680
1681 for (i = 0; i < cfg->num_dsi; i++) {
1682 if (cfg->io_start[i] == res->start)
1683 return i;
1684 }
1685
1686 return -EINVAL;
1687}
1688
Hai Lia6895542015-03-31 14:36:33 -04001689int msm_dsi_host_init(struct msm_dsi *msm_dsi)
1690{
1691 struct msm_dsi_host *msm_host = NULL;
1692 struct platform_device *pdev = msm_dsi->pdev;
1693 int ret;
1694
1695 msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
1696 if (!msm_host) {
1697 pr_err("%s: FAILED: cannot alloc dsi host\n",
1698 __func__);
1699 ret = -ENOMEM;
1700 goto fail;
1701 }
1702
Archit Tanejaf7009d22015-06-25 11:43:40 +05301703 msm_host->pdev = pdev;
1704
1705 ret = dsi_host_parse_dt(msm_host);
Hai Lia6895542015-03-31 14:36:33 -04001706 if (ret) {
Archit Tanejaf7009d22015-06-25 11:43:40 +05301707 pr_err("%s: failed to parse dt\n", __func__);
Hai Lia6895542015-03-31 14:36:33 -04001708 goto fail;
1709 }
Hai Lia6895542015-03-31 14:36:33 -04001710
Hai Lia6895542015-03-31 14:36:33 -04001711 msm_host->ctrl_base = msm_ioremap(pdev, "dsi_ctrl", "DSI CTRL");
1712 if (IS_ERR(msm_host->ctrl_base)) {
1713 pr_err("%s: unable to map Dsi ctrl base\n", __func__);
1714 ret = PTR_ERR(msm_host->ctrl_base);
1715 goto fail;
1716 }
1717
Hai Lid248b612015-08-13 17:49:29 -04001718 msm_host->cfg_hnd = dsi_get_config(msm_host);
1719 if (!msm_host->cfg_hnd) {
Hai Lia6895542015-03-31 14:36:33 -04001720 ret = -EINVAL;
1721 pr_err("%s: get config failed\n", __func__);
1722 goto fail;
1723 }
1724
Archit Taneja32280d62016-06-23 15:26:04 +05301725 msm_host->id = dsi_host_get_id(msm_host);
1726 if (msm_host->id < 0) {
1727 ret = msm_host->id;
1728 pr_err("%s: unable to identify DSI host index\n", __func__);
1729 goto fail;
1730 }
1731
Hai Lid248b612015-08-13 17:49:29 -04001732 /* fixup base address by io offset */
1733 msm_host->ctrl_base += msm_host->cfg_hnd->cfg->io_offset;
1734
Hai Lia6895542015-03-31 14:36:33 -04001735 ret = dsi_regulator_init(msm_host);
1736 if (ret) {
1737 pr_err("%s: regulator init failed\n", __func__);
1738 goto fail;
1739 }
1740
Archit Taneja31c92762015-10-09 12:40:39 +05301741 ret = dsi_clk_init(msm_host);
1742 if (ret) {
1743 pr_err("%s: unable to initialize dsi clks\n", __func__);
1744 goto fail;
1745 }
1746
Hai Lia6895542015-03-31 14:36:33 -04001747 msm_host->rx_buf = devm_kzalloc(&pdev->dev, SZ_4K, GFP_KERNEL);
1748 if (!msm_host->rx_buf) {
Wei Yongjuncd57b48a42017-02-09 15:19:07 +00001749 ret = -ENOMEM;
Hai Lia6895542015-03-31 14:36:33 -04001750 pr_err("%s: alloc rx temp buf failed\n", __func__);
1751 goto fail;
1752 }
1753
1754 init_completion(&msm_host->dma_comp);
1755 init_completion(&msm_host->video_comp);
1756 mutex_init(&msm_host->dev_mutex);
1757 mutex_init(&msm_host->cmd_mutex);
1758 mutex_init(&msm_host->clk_mutex);
1759 spin_lock_init(&msm_host->intr_lock);
1760
1761 /* setup workqueue */
1762 msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
1763 INIT_WORK(&msm_host->err_work, dsi_err_worker);
Archit Taneja8d23ea42016-10-25 12:17:59 +05301764 INIT_WORK(&msm_host->hpd_work, dsi_hpd_worker);
Hai Lia6895542015-03-31 14:36:33 -04001765
Hai Lia6895542015-03-31 14:36:33 -04001766 msm_dsi->host = &msm_host->base;
1767 msm_dsi->id = msm_host->id;
1768
1769 DBG("Dsi Host %d initialized", msm_host->id);
1770 return 0;
1771
1772fail:
1773 return ret;
1774}
1775
1776void msm_dsi_host_destroy(struct mipi_dsi_host *host)
1777{
1778 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1779
1780 DBG("");
1781 dsi_tx_buf_free(msm_host);
1782 if (msm_host->workqueue) {
1783 flush_workqueue(msm_host->workqueue);
1784 destroy_workqueue(msm_host->workqueue);
1785 msm_host->workqueue = NULL;
1786 }
1787
1788 mutex_destroy(&msm_host->clk_mutex);
1789 mutex_destroy(&msm_host->cmd_mutex);
1790 mutex_destroy(&msm_host->dev_mutex);
1791}
1792
1793int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
1794 struct drm_device *dev)
1795{
1796 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1797 struct platform_device *pdev = msm_host->pdev;
1798 int ret;
1799
1800 msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1801 if (msm_host->irq < 0) {
1802 ret = msm_host->irq;
1803 dev_err(dev->dev, "failed to get irq: %d\n", ret);
1804 return ret;
1805 }
1806
1807 ret = devm_request_irq(&pdev->dev, msm_host->irq,
1808 dsi_host_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1809 "dsi_isr", msm_host);
1810 if (ret < 0) {
1811 dev_err(&pdev->dev, "failed to request IRQ%u: %d\n",
1812 msm_host->irq, ret);
1813 return ret;
1814 }
1815
1816 msm_host->dev = dev;
1817 ret = dsi_tx_buf_alloc(msm_host, SZ_4K);
1818 if (ret) {
1819 pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
1820 return ret;
1821 }
1822
1823 return 0;
1824}
1825
1826int msm_dsi_host_register(struct mipi_dsi_host *host, bool check_defer)
1827{
1828 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lia6895542015-03-31 14:36:33 -04001829 int ret;
1830
1831 /* Register mipi dsi host */
1832 if (!msm_host->registered) {
1833 host->dev = &msm_host->pdev->dev;
1834 host->ops = &dsi_host_ops;
1835 ret = mipi_dsi_host_register(host);
1836 if (ret)
1837 return ret;
1838
1839 msm_host->registered = true;
1840
1841 /* If the panel driver has not been probed after host register,
1842 * we should defer the host's probe.
1843 * It makes sure panel is connected when fbcon detects
1844 * connector status and gets the proper display mode to
1845 * create framebuffer.
Archit Tanejaf7009d22015-06-25 11:43:40 +05301846 * Don't try to defer if there is nothing connected to the dsi
1847 * output
Hai Lia6895542015-03-31 14:36:33 -04001848 */
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301849 if (check_defer && msm_host->device_node) {
1850 if (!of_drm_find_panel(msm_host->device_node))
Archit Tanejac118e292015-07-31 14:06:10 +05301851 if (!of_drm_find_bridge(msm_host->device_node))
1852 return -EPROBE_DEFER;
Hai Lia6895542015-03-31 14:36:33 -04001853 }
1854 }
1855
1856 return 0;
1857}
1858
1859void msm_dsi_host_unregister(struct mipi_dsi_host *host)
1860{
1861 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1862
1863 if (msm_host->registered) {
1864 mipi_dsi_host_unregister(host);
1865 host->dev = NULL;
1866 host->ops = NULL;
1867 msm_host->registered = false;
1868 }
1869}
1870
1871int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
1872 const struct mipi_dsi_msg *msg)
1873{
1874 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1875
1876 /* TODO: make sure dsi_cmd_mdp is idle.
1877 * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
1878 * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
1879 * How to handle the old versions? Wait for mdp cmd done?
1880 */
1881
1882 /*
1883 * mdss interrupt is generated in mdp core clock domain
1884 * mdp clock need to be enabled to receive dsi interrupt
1885 */
1886 dsi_clk_ctrl(msm_host, 1);
1887
1888 /* TODO: vote for bus bandwidth */
1889
1890 if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
1891 dsi_set_tx_power_mode(0, msm_host);
1892
1893 msm_host->dma_cmd_ctrl_restore = dsi_read(msm_host, REG_DSI_CTRL);
1894 dsi_write(msm_host, REG_DSI_CTRL,
1895 msm_host->dma_cmd_ctrl_restore |
1896 DSI_CTRL_CMD_MODE_EN |
1897 DSI_CTRL_ENABLE);
1898 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 1);
1899
1900 return 0;
1901}
1902
1903void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
1904 const struct mipi_dsi_msg *msg)
1905{
1906 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1907
1908 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 0);
1909 dsi_write(msm_host, REG_DSI_CTRL, msm_host->dma_cmd_ctrl_restore);
1910
1911 if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
1912 dsi_set_tx_power_mode(1, msm_host);
1913
1914 /* TODO: unvote for bus bandwidth */
1915
1916 dsi_clk_ctrl(msm_host, 0);
1917}
1918
1919int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
1920 const struct mipi_dsi_msg *msg)
1921{
1922 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1923
1924 return dsi_cmds2buf_tx(msm_host, msg);
1925}
1926
1927int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
1928 const struct mipi_dsi_msg *msg)
1929{
1930 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lid248b612015-08-13 17:49:29 -04001931 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -04001932 int data_byte, rx_byte, dlen, end;
1933 int short_response, diff, pkt_size, ret = 0;
1934 char cmd;
1935 int rlen = msg->rx_len;
1936 u8 *buf;
1937
1938 if (rlen <= 2) {
1939 short_response = 1;
1940 pkt_size = rlen;
1941 rx_byte = 4;
1942 } else {
1943 short_response = 0;
1944 data_byte = 10; /* first read */
1945 if (rlen < data_byte)
1946 pkt_size = rlen;
1947 else
1948 pkt_size = data_byte;
1949 rx_byte = data_byte + 6; /* 4 header + 2 crc */
1950 }
1951
1952 buf = msm_host->rx_buf;
1953 end = 0;
1954 while (!end) {
1955 u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
1956 struct mipi_dsi_msg max_pkt_size_msg = {
1957 .channel = msg->channel,
1958 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
1959 .tx_len = 2,
1960 .tx_buf = tx,
1961 };
1962
1963 DBG("rlen=%d pkt_size=%d rx_byte=%d",
1964 rlen, pkt_size, rx_byte);
1965
1966 ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
1967 if (ret < 2) {
1968 pr_err("%s: Set max pkt size failed, %d\n",
1969 __func__, ret);
1970 return -EINVAL;
1971 }
1972
Hai Lid248b612015-08-13 17:49:29 -04001973 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
1974 (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
Hai Lia6895542015-03-31 14:36:33 -04001975 /* Clear the RDBK_DATA registers */
1976 dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
1977 DSI_RDBK_DATA_CTRL_CLR);
1978 wmb(); /* make sure the RDBK registers are cleared */
1979 dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
1980 wmb(); /* release cleared status before transfer */
1981 }
1982
1983 ret = dsi_cmds2buf_tx(msm_host, msg);
1984 if (ret < msg->tx_len) {
1985 pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
1986 return ret;
1987 }
1988
1989 /*
1990 * once cmd_dma_done interrupt received,
1991 * return data from client is ready and stored
1992 * at RDBK_DATA register already
1993 * since rx fifo is 16 bytes, dcs header is kept at first loop,
1994 * after that dcs header lost during shift into registers
1995 */
1996 dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
1997
1998 if (dlen <= 0)
1999 return 0;
2000
2001 if (short_response)
2002 break;
2003
2004 if (rlen <= data_byte) {
2005 diff = data_byte - rlen;
2006 end = 1;
2007 } else {
2008 diff = 0;
2009 rlen -= data_byte;
2010 }
2011
2012 if (!end) {
2013 dlen -= 2; /* 2 crc */
2014 dlen -= diff;
2015 buf += dlen; /* next start position */
2016 data_byte = 14; /* NOT first read */
2017 if (rlen < data_byte)
2018 pkt_size += rlen;
2019 else
2020 pkt_size += data_byte;
2021 DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
2022 }
2023 }
2024
2025 /*
2026 * For single Long read, if the requested rlen < 10,
2027 * we need to shift the start position of rx
2028 * data buffer to skip the bytes which are not
2029 * updated.
2030 */
2031 if (pkt_size < 10 && !short_response)
2032 buf = msm_host->rx_buf + (10 - rlen);
2033 else
2034 buf = msm_host->rx_buf;
2035
2036 cmd = buf[0];
2037 switch (cmd) {
2038 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
2039 pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
2040 ret = 0;
Hai Li651ad3f2015-04-29 11:38:59 -04002041 break;
Hai Lia6895542015-03-31 14:36:33 -04002042 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
2043 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
2044 ret = dsi_short_read1_resp(buf, msg);
2045 break;
2046 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
2047 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
2048 ret = dsi_short_read2_resp(buf, msg);
2049 break;
2050 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
2051 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
2052 ret = dsi_long_read_resp(buf, msg);
2053 break;
2054 default:
2055 pr_warn("%s:Invalid response cmd\n", __func__);
2056 ret = 0;
2057 }
2058
2059 return ret;
2060}
2061
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05302062void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 dma_base,
2063 u32 len)
Hai Lia6895542015-03-31 14:36:33 -04002064{
2065 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2066
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05302067 dsi_write(msm_host, REG_DSI_DMA_BASE, dma_base);
Hai Lia6895542015-03-31 14:36:33 -04002068 dsi_write(msm_host, REG_DSI_DMA_LEN, len);
2069 dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
2070
2071 /* Make sure trigger happens */
2072 wmb();
2073}
2074
Hai Li9d32c4982015-05-15 13:04:05 -04002075int msm_dsi_host_set_src_pll(struct mipi_dsi_host *host,
2076 struct msm_dsi_pll *src_pll)
2077{
2078 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Archit Taneja4bfa9742015-10-09 16:32:38 +05302079 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Li9d32c4982015-05-15 13:04:05 -04002080 struct clk *byte_clk_provider, *pixel_clk_provider;
2081 int ret;
2082
2083 ret = msm_dsi_pll_get_clk_provider(src_pll,
2084 &byte_clk_provider, &pixel_clk_provider);
2085 if (ret) {
2086 pr_info("%s: can't get provider from pll, don't set parent\n",
2087 __func__);
2088 return 0;
2089 }
2090
2091 ret = clk_set_parent(msm_host->byte_clk_src, byte_clk_provider);
2092 if (ret) {
2093 pr_err("%s: can't set parent to byte_clk_src. ret=%d\n",
2094 __func__, ret);
2095 goto exit;
2096 }
2097
2098 ret = clk_set_parent(msm_host->pixel_clk_src, pixel_clk_provider);
2099 if (ret) {
2100 pr_err("%s: can't set parent to pixel_clk_src. ret=%d\n",
2101 __func__, ret);
2102 goto exit;
2103 }
2104
Archit Taneja4bfa9742015-10-09 16:32:38 +05302105 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
2106 ret = clk_set_parent(msm_host->dsi_clk_src, pixel_clk_provider);
2107 if (ret) {
2108 pr_err("%s: can't set parent to dsi_clk_src. ret=%d\n",
2109 __func__, ret);
2110 goto exit;
2111 }
2112
2113 ret = clk_set_parent(msm_host->esc_clk_src, byte_clk_provider);
2114 if (ret) {
2115 pr_err("%s: can't set parent to esc_clk_src. ret=%d\n",
2116 __func__, ret);
2117 goto exit;
2118 }
2119 }
2120
Hai Li9d32c4982015-05-15 13:04:05 -04002121exit:
2122 return ret;
2123}
2124
Archit Taneja34d95452015-07-29 12:14:12 -04002125void msm_dsi_host_reset_phy(struct mipi_dsi_host *host)
2126{
2127 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2128
2129 DBG("");
2130 dsi_write(msm_host, REG_DSI_PHY_RESET, DSI_PHY_RESET_RESET);
2131 /* Make sure fully reset */
2132 wmb();
2133 udelay(1000);
2134 dsi_write(msm_host, REG_DSI_PHY_RESET, 0);
2135 udelay(100);
2136}
2137
Hai Lib62aa702017-01-07 14:24:38 +05302138void msm_dsi_host_get_phy_clk_req(struct mipi_dsi_host *host,
2139 struct msm_dsi_phy_clk_request *clk_req)
2140{
2141 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2142
2143 clk_req->bitclk_rate = msm_host->byte_clk_rate * 8;
2144 clk_req->escclk_rate = msm_host->esc_clk_rate;
2145}
2146
Hai Lia6895542015-03-31 14:36:33 -04002147int msm_dsi_host_enable(struct mipi_dsi_host *host)
2148{
2149 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2150
2151 dsi_op_mode_config(msm_host,
2152 !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
2153
2154 /* TODO: clock should be turned off for command mode,
2155 * and only turned on before MDP START.
2156 * This part of code should be enabled once mdp driver support it.
2157 */
2158 /* if (msm_panel->mode == MSM_DSI_CMD_MODE)
2159 dsi_clk_ctrl(msm_host, 0); */
2160
2161 return 0;
2162}
2163
2164int msm_dsi_host_disable(struct mipi_dsi_host *host)
2165{
2166 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2167
2168 dsi_op_mode_config(msm_host,
2169 !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
2170
2171 /* Since we have disabled INTF, the video engine won't stop so that
2172 * the cmd engine will be blocked.
2173 * Reset to disable video engine so that we can send off cmd.
2174 */
2175 dsi_sw_reset(msm_host);
2176
2177 return 0;
2178}
2179
Archit Taneja0c7df472015-10-14 15:31:13 +05302180static void msm_dsi_sfpb_config(struct msm_dsi_host *msm_host, bool enable)
2181{
2182 enum sfpb_ahb_arb_master_port_en en;
2183
2184 if (!msm_host->sfpb)
2185 return;
2186
2187 en = enable ? SFPB_MASTER_PORT_ENABLE : SFPB_MASTER_PORT_DISABLE;
2188
2189 regmap_update_bits(msm_host->sfpb, REG_SFPB_GPREG,
2190 SFPB_GPREG_MASTER_PORT_EN__MASK,
2191 SFPB_GPREG_MASTER_PORT_EN(en));
2192}
2193
Hai Lib62aa702017-01-07 14:24:38 +05302194int msm_dsi_host_power_on(struct mipi_dsi_host *host,
2195 struct msm_dsi_phy_shared_timings *phy_shared_timings)
Hai Lia6895542015-03-31 14:36:33 -04002196{
2197 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lia6895542015-03-31 14:36:33 -04002198 int ret = 0;
2199
2200 mutex_lock(&msm_host->dev_mutex);
2201 if (msm_host->power_on) {
2202 DBG("dsi host already on");
2203 goto unlock_ret;
2204 }
2205
Archit Taneja0c7df472015-10-14 15:31:13 +05302206 msm_dsi_sfpb_config(msm_host, true);
2207
Hai Lia6895542015-03-31 14:36:33 -04002208 ret = dsi_host_regulator_enable(msm_host);
2209 if (ret) {
2210 pr_err("%s:Failed to enable vregs.ret=%d\n",
2211 __func__, ret);
2212 goto unlock_ret;
2213 }
2214
Hai Lia6895542015-03-31 14:36:33 -04002215 ret = dsi_clk_ctrl(msm_host, 1);
2216 if (ret) {
2217 pr_err("%s: failed to enable clocks. ret=%d\n", __func__, ret);
2218 goto fail_disable_reg;
2219 }
2220
Hai Liab8909b2015-06-11 10:56:46 -04002221 ret = pinctrl_pm_select_default_state(&msm_host->pdev->dev);
2222 if (ret) {
2223 pr_err("%s: failed to set pinctrl default state, %d\n",
2224 __func__, ret);
2225 goto fail_disable_clk;
2226 }
2227
Hai Lia6895542015-03-31 14:36:33 -04002228 dsi_timing_setup(msm_host);
2229 dsi_sw_reset(msm_host);
Hai Lib62aa702017-01-07 14:24:38 +05302230 dsi_ctrl_config(msm_host, true, phy_shared_timings);
Hai Lia6895542015-03-31 14:36:33 -04002231
2232 if (msm_host->disp_en_gpio)
2233 gpiod_set_value(msm_host->disp_en_gpio, 1);
2234
2235 msm_host->power_on = true;
2236 mutex_unlock(&msm_host->dev_mutex);
2237
2238 return 0;
2239
Hai Liab8909b2015-06-11 10:56:46 -04002240fail_disable_clk:
2241 dsi_clk_ctrl(msm_host, 0);
Hai Lia6895542015-03-31 14:36:33 -04002242fail_disable_reg:
2243 dsi_host_regulator_disable(msm_host);
2244unlock_ret:
2245 mutex_unlock(&msm_host->dev_mutex);
2246 return ret;
2247}
2248
2249int msm_dsi_host_power_off(struct mipi_dsi_host *host)
2250{
2251 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2252
2253 mutex_lock(&msm_host->dev_mutex);
2254 if (!msm_host->power_on) {
2255 DBG("dsi host already off");
2256 goto unlock_ret;
2257 }
2258
Hai Lidceac342016-09-15 14:34:49 +05302259 dsi_ctrl_config(msm_host, false, NULL);
Hai Lia6895542015-03-31 14:36:33 -04002260
2261 if (msm_host->disp_en_gpio)
2262 gpiod_set_value(msm_host->disp_en_gpio, 0);
2263
Hai Liab8909b2015-06-11 10:56:46 -04002264 pinctrl_pm_select_sleep_state(&msm_host->pdev->dev);
2265
Hai Lia6895542015-03-31 14:36:33 -04002266 dsi_clk_ctrl(msm_host, 0);
2267
2268 dsi_host_regulator_disable(msm_host);
2269
Archit Taneja0c7df472015-10-14 15:31:13 +05302270 msm_dsi_sfpb_config(msm_host, false);
2271
Hai Lia6895542015-03-31 14:36:33 -04002272 DBG("-");
2273
2274 msm_host->power_on = false;
2275
2276unlock_ret:
2277 mutex_unlock(&msm_host->dev_mutex);
2278 return 0;
2279}
2280
2281int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
2282 struct drm_display_mode *mode)
2283{
2284 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lib62aa702017-01-07 14:24:38 +05302285 int ret;
Hai Lia6895542015-03-31 14:36:33 -04002286
2287 if (msm_host->mode) {
2288 drm_mode_destroy(msm_host->dev, msm_host->mode);
2289 msm_host->mode = NULL;
2290 }
2291
2292 msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
Wei Yongjun2abe1f22016-06-18 17:26:37 +00002293 if (!msm_host->mode) {
Hai Lia6895542015-03-31 14:36:33 -04002294 pr_err("%s: cannot duplicate mode\n", __func__);
Wei Yongjun2abe1f22016-06-18 17:26:37 +00002295 return -ENOMEM;
Hai Lia6895542015-03-31 14:36:33 -04002296 }
2297
Hai Lib62aa702017-01-07 14:24:38 +05302298 ret = dsi_calc_clk_rate(msm_host);
2299 if (ret) {
2300 pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
2301 return ret;
2302 }
2303
Hai Lia6895542015-03-31 14:36:33 -04002304 return 0;
2305}
2306
2307struct drm_panel *msm_dsi_host_get_panel(struct mipi_dsi_host *host,
2308 unsigned long *panel_flags)
2309{
2310 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2311 struct drm_panel *panel;
2312
Archit Tanejaa9ddac92015-08-03 14:05:45 +05302313 panel = of_drm_find_panel(msm_host->device_node);
Hai Lia6895542015-03-31 14:36:33 -04002314 if (panel_flags)
2315 *panel_flags = msm_host->mode_flags;
2316
2317 return panel;
2318}
2319
Archit Tanejac118e292015-07-31 14:06:10 +05302320struct drm_bridge *msm_dsi_host_get_bridge(struct mipi_dsi_host *host)
2321{
2322 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2323
2324 return of_drm_find_bridge(msm_host->device_node);
2325}