blob: 8e75e001ad02b21e99d27ae7f00bfb07321221a3 [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"
Hai Lia6895542015-03-31 14:36:33 -040035
36static int dsi_get_version(const void __iomem *base, u32 *major, u32 *minor)
37{
38 u32 ver;
Hai Lia6895542015-03-31 14:36:33 -040039
40 if (!major || !minor)
41 return -EINVAL;
42
Archit Taneja648d5062015-10-09 11:10:59 +053043 /*
44 * From DSI6G(v3), addition of a 6G_HW_VERSION register at offset 0
Hai Lia6895542015-03-31 14:36:33 -040045 * makes all other registers 4-byte shifted down.
Archit Taneja648d5062015-10-09 11:10:59 +053046 *
47 * In order to identify between DSI6G(v3) and beyond, and DSIv2 and
48 * older, we read the DSI_VERSION register without any shift(offset
49 * 0x1f0). In the case of DSIv2, this hast to be a non-zero value. In
50 * the case of DSI6G, this has to be zero (the offset points to a
51 * scratch register which we never touch)
Hai Lia6895542015-03-31 14:36:33 -040052 */
Archit Taneja648d5062015-10-09 11:10:59 +053053
54 ver = msm_readl(base + REG_DSI_VERSION);
55 if (ver) {
56 /* older dsi host, there is no register shift */
Hai Lia6895542015-03-31 14:36:33 -040057 ver = FIELD(ver, DSI_VERSION_MAJOR);
58 if (ver <= MSM_DSI_VER_MAJOR_V2) {
59 /* old versions */
60 *major = ver;
61 *minor = 0;
62 return 0;
63 } else {
64 return -EINVAL;
65 }
66 } else {
Archit Taneja648d5062015-10-09 11:10:59 +053067 /*
68 * newer host, offset 0 has 6G_HW_VERSION, the rest of the
69 * registers are shifted down, read DSI_VERSION again with
70 * the shifted offset
71 */
Hai Lia6895542015-03-31 14:36:33 -040072 ver = msm_readl(base + DSI_6G_REG_SHIFT + REG_DSI_VERSION);
73 ver = FIELD(ver, DSI_VERSION_MAJOR);
74 if (ver == MSM_DSI_VER_MAJOR_6G) {
75 /* 6G version */
76 *major = ver;
Archit Taneja648d5062015-10-09 11:10:59 +053077 *minor = msm_readl(base + REG_DSI_6G_HW_VERSION);
Hai Lia6895542015-03-31 14:36:33 -040078 return 0;
79 } else {
80 return -EINVAL;
81 }
82 }
83}
84
85#define DSI_ERR_STATE_ACK 0x0000
86#define DSI_ERR_STATE_TIMEOUT 0x0001
87#define DSI_ERR_STATE_DLN0_PHY 0x0002
88#define DSI_ERR_STATE_FIFO 0x0004
89#define DSI_ERR_STATE_MDP_FIFO_UNDERFLOW 0x0008
90#define DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION 0x0010
91#define DSI_ERR_STATE_PLL_UNLOCKED 0x0020
92
93#define DSI_CLK_CTRL_ENABLE_CLKS \
94 (DSI_CLK_CTRL_AHBS_HCLK_ON | DSI_CLK_CTRL_AHBM_SCLK_ON | \
95 DSI_CLK_CTRL_PCLK_ON | DSI_CLK_CTRL_DSICLK_ON | \
96 DSI_CLK_CTRL_BYTECLK_ON | DSI_CLK_CTRL_ESCCLK_ON | \
97 DSI_CLK_CTRL_FORCE_ON_DYN_AHBM_HCLK)
98
99struct msm_dsi_host {
100 struct mipi_dsi_host base;
101
102 struct platform_device *pdev;
103 struct drm_device *dev;
104
105 int id;
106
107 void __iomem *ctrl_base;
Hai Liec31abf2015-05-15 13:04:06 -0400108 struct regulator_bulk_data supplies[DSI_DEV_REGULATOR_MAX];
Archit Taneja6e0eb522015-10-09 15:21:12 +0530109
110 struct clk *bus_clks[DSI_BUS_CLK_MAX];
111
Hai Lia6895542015-03-31 14:36:33 -0400112 struct clk *byte_clk;
113 struct clk *esc_clk;
114 struct clk *pixel_clk;
Hai Li9d32c4982015-05-15 13:04:05 -0400115 struct clk *byte_clk_src;
116 struct clk *pixel_clk_src;
117
Hai Lia6895542015-03-31 14:36:33 -0400118 u32 byte_clk_rate;
Archit Taneja4bfa9742015-10-09 16:32:38 +0530119 u32 esc_clk_rate;
120
121 /* DSI v2 specific clocks */
122 struct clk *src_clk;
123 struct clk *esc_clk_src;
124 struct clk *dsi_clk_src;
125
126 u32 src_clk_rate;
Hai Lia6895542015-03-31 14:36:33 -0400127
128 struct gpio_desc *disp_en_gpio;
129 struct gpio_desc *te_gpio;
130
Hai Lid248b612015-08-13 17:49:29 -0400131 const struct msm_dsi_cfg_handler *cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400132
133 struct completion dma_comp;
134 struct completion video_comp;
135 struct mutex dev_mutex;
136 struct mutex cmd_mutex;
137 struct mutex clk_mutex;
138 spinlock_t intr_lock; /* Protect interrupt ctrl register */
139
140 u32 err_work_state;
141 struct work_struct err_work;
Archit Taneja8d23ea42016-10-25 12:17:59 +0530142 struct work_struct hpd_work;
Hai Lia6895542015-03-31 14:36:33 -0400143 struct workqueue_struct *workqueue;
144
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530145 /* DSI 6G TX buffer*/
Hai Lia6895542015-03-31 14:36:33 -0400146 struct drm_gem_object *tx_gem_obj;
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530147
148 /* DSI v2 TX buffer */
149 void *tx_buf;
150 dma_addr_t tx_buf_paddr;
151
152 int tx_size;
153
Hai Lia6895542015-03-31 14:36:33 -0400154 u8 *rx_buf;
155
Archit Taneja0c7df472015-10-14 15:31:13 +0530156 struct regmap *sfpb;
157
Hai Lia6895542015-03-31 14:36:33 -0400158 struct drm_display_mode *mode;
159
Archit Tanejaa9ddac92015-08-03 14:05:45 +0530160 /* connected device info */
161 struct device_node *device_node;
Hai Lia6895542015-03-31 14:36:33 -0400162 unsigned int channel;
163 unsigned int lanes;
164 enum mipi_dsi_pixel_format format;
165 unsigned long mode_flags;
166
Archit Taneja26f7d1f2016-02-25 11:19:48 +0530167 /* lane data parsed via DT */
168 int dlane_swap;
169 int num_data_lanes;
170
Hai Lia6895542015-03-31 14:36:33 -0400171 u32 dma_cmd_ctrl_restore;
172
173 bool registered;
174 bool power_on;
175 int irq;
176};
177
178static u32 dsi_get_bpp(const enum mipi_dsi_pixel_format fmt)
179{
180 switch (fmt) {
181 case MIPI_DSI_FMT_RGB565: return 16;
182 case MIPI_DSI_FMT_RGB666_PACKED: return 18;
183 case MIPI_DSI_FMT_RGB666:
184 case MIPI_DSI_FMT_RGB888:
185 default: return 24;
186 }
187}
188
189static inline u32 dsi_read(struct msm_dsi_host *msm_host, u32 reg)
190{
Hai Lid248b612015-08-13 17:49:29 -0400191 return msm_readl(msm_host->ctrl_base + reg);
Hai Lia6895542015-03-31 14:36:33 -0400192}
193static inline void dsi_write(struct msm_dsi_host *msm_host, u32 reg, u32 data)
194{
Hai Lid248b612015-08-13 17:49:29 -0400195 msm_writel(data, msm_host->ctrl_base + reg);
Hai Lia6895542015-03-31 14:36:33 -0400196}
197
198static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host);
199static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host);
200
Hai Lid248b612015-08-13 17:49:29 -0400201static const struct msm_dsi_cfg_handler *dsi_get_config(
202 struct msm_dsi_host *msm_host)
Hai Lia6895542015-03-31 14:36:33 -0400203{
Hai Lid248b612015-08-13 17:49:29 -0400204 const struct msm_dsi_cfg_handler *cfg_hnd = NULL;
Archit Taneja31c92762015-10-09 12:40:39 +0530205 struct device *dev = &msm_host->pdev->dev;
Hai Lia6895542015-03-31 14:36:33 -0400206 struct regulator *gdsc_reg;
Archit Taneja31c92762015-10-09 12:40:39 +0530207 struct clk *ahb_clk;
Hai Lid248b612015-08-13 17:49:29 -0400208 int ret;
Hai Lia6895542015-03-31 14:36:33 -0400209 u32 major = 0, minor = 0;
210
Archit Taneja31c92762015-10-09 12:40:39 +0530211 gdsc_reg = regulator_get(dev, "gdsc");
Fabian Frederickbdc80de2015-05-04 19:03:55 +0200212 if (IS_ERR(gdsc_reg)) {
Hai Lia6895542015-03-31 14:36:33 -0400213 pr_err("%s: cannot get gdsc\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400214 goto exit;
Hai Lia6895542015-03-31 14:36:33 -0400215 }
Archit Taneja31c92762015-10-09 12:40:39 +0530216
217 ahb_clk = clk_get(dev, "iface_clk");
218 if (IS_ERR(ahb_clk)) {
219 pr_err("%s: cannot get interface clock\n", __func__);
220 goto put_gdsc;
221 }
222
Hai Lia6895542015-03-31 14:36:33 -0400223 ret = regulator_enable(gdsc_reg);
224 if (ret) {
225 pr_err("%s: unable to enable gdsc\n", __func__);
Archit Taneja31c92762015-10-09 12:40:39 +0530226 goto put_clk;
Hai Lia6895542015-03-31 14:36:33 -0400227 }
Archit Taneja31c92762015-10-09 12:40:39 +0530228
229 ret = clk_prepare_enable(ahb_clk);
Hai Lia6895542015-03-31 14:36:33 -0400230 if (ret) {
231 pr_err("%s: unable to enable ahb_clk\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400232 goto disable_gdsc;
Hai Lia6895542015-03-31 14:36:33 -0400233 }
234
235 ret = dsi_get_version(msm_host->ctrl_base, &major, &minor);
Hai Lia6895542015-03-31 14:36:33 -0400236 if (ret) {
237 pr_err("%s: Invalid version\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400238 goto disable_clks;
Hai Lia6895542015-03-31 14:36:33 -0400239 }
240
Hai Lid248b612015-08-13 17:49:29 -0400241 cfg_hnd = msm_dsi_cfg_get(major, minor);
Hai Lia6895542015-03-31 14:36:33 -0400242
Hai Lid248b612015-08-13 17:49:29 -0400243 DBG("%s: Version %x:%x\n", __func__, major, minor);
244
245disable_clks:
Archit Taneja31c92762015-10-09 12:40:39 +0530246 clk_disable_unprepare(ahb_clk);
Hai Lid248b612015-08-13 17:49:29 -0400247disable_gdsc:
248 regulator_disable(gdsc_reg);
Archit Taneja31c92762015-10-09 12:40:39 +0530249put_clk:
250 clk_put(ahb_clk);
Hai Lid248b612015-08-13 17:49:29 -0400251put_gdsc:
252 regulator_put(gdsc_reg);
253exit:
254 return cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400255}
256
257static inline struct msm_dsi_host *to_msm_dsi_host(struct mipi_dsi_host *host)
258{
259 return container_of(host, struct msm_dsi_host, base);
260}
261
262static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host)
263{
264 struct regulator_bulk_data *s = msm_host->supplies;
Hai Lid248b612015-08-13 17:49:29 -0400265 const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
266 int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
Hai Lia6895542015-03-31 14:36:33 -0400267 int i;
268
269 DBG("");
270 for (i = num - 1; i >= 0; i--)
271 if (regs[i].disable_load >= 0)
Dave Airlie2c33ce02015-04-20 11:32:26 +1000272 regulator_set_load(s[i].consumer,
273 regs[i].disable_load);
Hai Lia6895542015-03-31 14:36:33 -0400274
275 regulator_bulk_disable(num, s);
276}
277
278static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host)
279{
280 struct regulator_bulk_data *s = msm_host->supplies;
Hai Lid248b612015-08-13 17:49:29 -0400281 const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
282 int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
Hai Lia6895542015-03-31 14:36:33 -0400283 int ret, i;
284
285 DBG("");
286 for (i = 0; i < num; i++) {
287 if (regs[i].enable_load >= 0) {
Dave Airlie2c33ce02015-04-20 11:32:26 +1000288 ret = regulator_set_load(s[i].consumer,
289 regs[i].enable_load);
Hai Lia6895542015-03-31 14:36:33 -0400290 if (ret < 0) {
291 pr_err("regulator %d set op mode failed, %d\n",
292 i, ret);
293 goto fail;
294 }
295 }
296 }
297
298 ret = regulator_bulk_enable(num, s);
299 if (ret < 0) {
300 pr_err("regulator enable failed, %d\n", ret);
301 goto fail;
302 }
303
304 return 0;
305
306fail:
307 for (i--; i >= 0; i--)
Dave Airlie2c33ce02015-04-20 11:32:26 +1000308 regulator_set_load(s[i].consumer, regs[i].disable_load);
Hai Lia6895542015-03-31 14:36:33 -0400309 return ret;
310}
311
312static int dsi_regulator_init(struct msm_dsi_host *msm_host)
313{
314 struct regulator_bulk_data *s = msm_host->supplies;
Hai Lid248b612015-08-13 17:49:29 -0400315 const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
316 int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
Hai Lia6895542015-03-31 14:36:33 -0400317 int i, ret;
318
319 for (i = 0; i < num; i++)
320 s[i].supply = regs[i].name;
321
322 ret = devm_regulator_bulk_get(&msm_host->pdev->dev, num, s);
323 if (ret < 0) {
324 pr_err("%s: failed to init regulator, ret=%d\n",
325 __func__, ret);
326 return ret;
327 }
328
Hai Lia6895542015-03-31 14:36:33 -0400329 return 0;
330}
331
332static int dsi_clk_init(struct msm_dsi_host *msm_host)
333{
334 struct device *dev = &msm_host->pdev->dev;
Archit Taneja4bfa9742015-10-09 16:32:38 +0530335 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
336 const struct msm_dsi_config *cfg = cfg_hnd->cfg;
Archit Taneja6e0eb522015-10-09 15:21:12 +0530337 int i, ret = 0;
Hai Lia6895542015-03-31 14:36:33 -0400338
Archit Taneja6e0eb522015-10-09 15:21:12 +0530339 /* get bus clocks */
340 for (i = 0; i < cfg->num_bus_clks; i++) {
341 msm_host->bus_clks[i] = devm_clk_get(dev,
342 cfg->bus_clk_names[i]);
343 if (IS_ERR(msm_host->bus_clks[i])) {
344 ret = PTR_ERR(msm_host->bus_clks[i]);
345 pr_err("%s: Unable to get %s, ret = %d\n",
346 __func__, cfg->bus_clk_names[i], ret);
347 goto exit;
348 }
Hai Lia6895542015-03-31 14:36:33 -0400349 }
350
Archit Taneja6e0eb522015-10-09 15:21:12 +0530351 /* get link and source clocks */
Hai Lia6895542015-03-31 14:36:33 -0400352 msm_host->byte_clk = devm_clk_get(dev, "byte_clk");
353 if (IS_ERR(msm_host->byte_clk)) {
354 ret = PTR_ERR(msm_host->byte_clk);
355 pr_err("%s: can't find dsi_byte_clk. ret=%d\n",
356 __func__, ret);
357 msm_host->byte_clk = NULL;
358 goto exit;
359 }
360
361 msm_host->pixel_clk = devm_clk_get(dev, "pixel_clk");
362 if (IS_ERR(msm_host->pixel_clk)) {
363 ret = PTR_ERR(msm_host->pixel_clk);
364 pr_err("%s: can't find dsi_pixel_clk. ret=%d\n",
365 __func__, ret);
366 msm_host->pixel_clk = NULL;
367 goto exit;
368 }
369
370 msm_host->esc_clk = devm_clk_get(dev, "core_clk");
371 if (IS_ERR(msm_host->esc_clk)) {
372 ret = PTR_ERR(msm_host->esc_clk);
373 pr_err("%s: can't find dsi_esc_clk. ret=%d\n",
374 __func__, ret);
375 msm_host->esc_clk = NULL;
376 goto exit;
377 }
378
Archit Tanejae6c4c782015-11-30 17:47:17 +0530379 msm_host->byte_clk_src = clk_get_parent(msm_host->byte_clk);
380 if (!msm_host->byte_clk_src) {
381 ret = -ENODEV;
Hai Li9d32c4982015-05-15 13:04:05 -0400382 pr_err("%s: can't find byte_clk_src. ret=%d\n", __func__, ret);
Hai Li9d32c4982015-05-15 13:04:05 -0400383 goto exit;
384 }
385
Archit Tanejae6c4c782015-11-30 17:47:17 +0530386 msm_host->pixel_clk_src = clk_get_parent(msm_host->pixel_clk);
387 if (!msm_host->pixel_clk_src) {
388 ret = -ENODEV;
Hai Li9d32c4982015-05-15 13:04:05 -0400389 pr_err("%s: can't find pixel_clk_src. ret=%d\n", __func__, ret);
Archit Taneja4bfa9742015-10-09 16:32:38 +0530390 goto exit;
Hai Li9d32c4982015-05-15 13:04:05 -0400391 }
392
Archit Taneja4bfa9742015-10-09 16:32:38 +0530393 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
394 msm_host->src_clk = devm_clk_get(dev, "src_clk");
395 if (IS_ERR(msm_host->src_clk)) {
396 ret = PTR_ERR(msm_host->src_clk);
397 pr_err("%s: can't find dsi_src_clk. ret=%d\n",
398 __func__, ret);
399 msm_host->src_clk = NULL;
400 goto exit;
401 }
402
403 msm_host->esc_clk_src = clk_get_parent(msm_host->esc_clk);
404 if (!msm_host->esc_clk_src) {
405 ret = -ENODEV;
406 pr_err("%s: can't get esc_clk_src. ret=%d\n",
407 __func__, ret);
408 goto exit;
409 }
410
411 msm_host->dsi_clk_src = clk_get_parent(msm_host->src_clk);
412 if (!msm_host->dsi_clk_src) {
413 ret = -ENODEV;
414 pr_err("%s: can't get dsi_clk_src. ret=%d\n",
415 __func__, ret);
416 }
417 }
Hai Lia6895542015-03-31 14:36:33 -0400418exit:
419 return ret;
420}
421
422static int dsi_bus_clk_enable(struct msm_dsi_host *msm_host)
423{
Archit Taneja6e0eb522015-10-09 15:21:12 +0530424 const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
425 int i, ret;
Hai Lia6895542015-03-31 14:36:33 -0400426
427 DBG("id=%d", msm_host->id);
428
Archit Taneja6e0eb522015-10-09 15:21:12 +0530429 for (i = 0; i < cfg->num_bus_clks; i++) {
430 ret = clk_prepare_enable(msm_host->bus_clks[i]);
431 if (ret) {
432 pr_err("%s: failed to enable bus clock %d ret %d\n",
433 __func__, i, ret);
434 goto err;
435 }
Hai Lia6895542015-03-31 14:36:33 -0400436 }
437
438 return 0;
Archit Taneja6e0eb522015-10-09 15:21:12 +0530439err:
440 for (; i > 0; i--)
441 clk_disable_unprepare(msm_host->bus_clks[i]);
Hai Lia6895542015-03-31 14:36:33 -0400442
Hai Lia6895542015-03-31 14:36:33 -0400443 return ret;
444}
445
446static void dsi_bus_clk_disable(struct msm_dsi_host *msm_host)
447{
Archit Taneja6e0eb522015-10-09 15:21:12 +0530448 const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
449 int i;
450
Hai Lia6895542015-03-31 14:36:33 -0400451 DBG("");
Archit Taneja6e0eb522015-10-09 15:21:12 +0530452
453 for (i = cfg->num_bus_clks - 1; i >= 0; i--)
454 clk_disable_unprepare(msm_host->bus_clks[i]);
Hai Lia6895542015-03-31 14:36:33 -0400455}
456
Archit Taneja4bfa9742015-10-09 16:32:38 +0530457static int dsi_link_clk_enable_6g(struct msm_dsi_host *msm_host)
Hai Lia6895542015-03-31 14:36:33 -0400458{
459 int ret;
460
461 DBG("Set clk rates: pclk=%d, byteclk=%d",
462 msm_host->mode->clock, msm_host->byte_clk_rate);
463
464 ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
465 if (ret) {
466 pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
467 goto error;
468 }
469
470 ret = clk_set_rate(msm_host->pixel_clk, msm_host->mode->clock * 1000);
471 if (ret) {
472 pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
473 goto error;
474 }
475
476 ret = clk_prepare_enable(msm_host->esc_clk);
477 if (ret) {
478 pr_err("%s: Failed to enable dsi esc clk\n", __func__);
479 goto error;
480 }
481
482 ret = clk_prepare_enable(msm_host->byte_clk);
483 if (ret) {
484 pr_err("%s: Failed to enable dsi byte clk\n", __func__);
485 goto byte_clk_err;
486 }
487
488 ret = clk_prepare_enable(msm_host->pixel_clk);
489 if (ret) {
490 pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
491 goto pixel_clk_err;
492 }
493
494 return 0;
495
496pixel_clk_err:
497 clk_disable_unprepare(msm_host->byte_clk);
498byte_clk_err:
499 clk_disable_unprepare(msm_host->esc_clk);
500error:
501 return ret;
502}
503
Archit Taneja4bfa9742015-10-09 16:32:38 +0530504static int dsi_link_clk_enable_v2(struct msm_dsi_host *msm_host)
505{
506 int ret;
507
508 DBG("Set clk rates: pclk=%d, byteclk=%d, esc_clk=%d, dsi_src_clk=%d",
509 msm_host->mode->clock, msm_host->byte_clk_rate,
510 msm_host->esc_clk_rate, msm_host->src_clk_rate);
511
512 ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
513 if (ret) {
514 pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
515 goto error;
516 }
517
518 ret = clk_set_rate(msm_host->esc_clk, msm_host->esc_clk_rate);
519 if (ret) {
520 pr_err("%s: Failed to set rate esc clk, %d\n", __func__, ret);
521 goto error;
522 }
523
524 ret = clk_set_rate(msm_host->src_clk, msm_host->src_clk_rate);
525 if (ret) {
526 pr_err("%s: Failed to set rate src clk, %d\n", __func__, ret);
527 goto error;
528 }
529
530 ret = clk_set_rate(msm_host->pixel_clk, msm_host->mode->clock * 1000);
531 if (ret) {
532 pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
533 goto error;
534 }
535
536 ret = clk_prepare_enable(msm_host->byte_clk);
537 if (ret) {
538 pr_err("%s: Failed to enable dsi byte clk\n", __func__);
539 goto error;
540 }
541
542 ret = clk_prepare_enable(msm_host->esc_clk);
543 if (ret) {
544 pr_err("%s: Failed to enable dsi esc clk\n", __func__);
545 goto esc_clk_err;
546 }
547
548 ret = clk_prepare_enable(msm_host->src_clk);
549 if (ret) {
550 pr_err("%s: Failed to enable dsi src clk\n", __func__);
551 goto src_clk_err;
552 }
553
554 ret = clk_prepare_enable(msm_host->pixel_clk);
555 if (ret) {
556 pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
557 goto pixel_clk_err;
558 }
559
560 return 0;
561
562pixel_clk_err:
563 clk_disable_unprepare(msm_host->src_clk);
564src_clk_err:
565 clk_disable_unprepare(msm_host->esc_clk);
566esc_clk_err:
567 clk_disable_unprepare(msm_host->byte_clk);
568error:
569 return ret;
570}
571
572static int dsi_link_clk_enable(struct msm_dsi_host *msm_host)
573{
574 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
575
576 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G)
577 return dsi_link_clk_enable_6g(msm_host);
578 else
579 return dsi_link_clk_enable_v2(msm_host);
580}
581
Hai Lia6895542015-03-31 14:36:33 -0400582static void dsi_link_clk_disable(struct msm_dsi_host *msm_host)
583{
Archit Taneja4bfa9742015-10-09 16:32:38 +0530584 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
585
586 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
587 clk_disable_unprepare(msm_host->esc_clk);
588 clk_disable_unprepare(msm_host->pixel_clk);
589 clk_disable_unprepare(msm_host->byte_clk);
590 } else {
591 clk_disable_unprepare(msm_host->pixel_clk);
592 clk_disable_unprepare(msm_host->src_clk);
593 clk_disable_unprepare(msm_host->esc_clk);
594 clk_disable_unprepare(msm_host->byte_clk);
595 }
Hai Lia6895542015-03-31 14:36:33 -0400596}
597
598static int dsi_clk_ctrl(struct msm_dsi_host *msm_host, bool enable)
599{
600 int ret = 0;
601
602 mutex_lock(&msm_host->clk_mutex);
603 if (enable) {
604 ret = dsi_bus_clk_enable(msm_host);
605 if (ret) {
606 pr_err("%s: Can not enable bus clk, %d\n",
607 __func__, ret);
608 goto unlock_ret;
609 }
610 ret = dsi_link_clk_enable(msm_host);
611 if (ret) {
612 pr_err("%s: Can not enable link clk, %d\n",
613 __func__, ret);
614 dsi_bus_clk_disable(msm_host);
615 goto unlock_ret;
616 }
617 } else {
618 dsi_link_clk_disable(msm_host);
619 dsi_bus_clk_disable(msm_host);
620 }
621
622unlock_ret:
623 mutex_unlock(&msm_host->clk_mutex);
624 return ret;
625}
626
627static int dsi_calc_clk_rate(struct msm_dsi_host *msm_host)
628{
629 struct drm_display_mode *mode = msm_host->mode;
Archit Taneja4bfa9742015-10-09 16:32:38 +0530630 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400631 u8 lanes = msm_host->lanes;
632 u32 bpp = dsi_get_bpp(msm_host->format);
633 u32 pclk_rate;
634
635 if (!mode) {
636 pr_err("%s: mode not set\n", __func__);
637 return -EINVAL;
638 }
639
640 pclk_rate = mode->clock * 1000;
641 if (lanes > 0) {
642 msm_host->byte_clk_rate = (pclk_rate * bpp) / (8 * lanes);
643 } else {
644 pr_err("%s: forcing mdss_dsi lanes to 1\n", __func__);
645 msm_host->byte_clk_rate = (pclk_rate * bpp) / 8;
646 }
647
648 DBG("pclk=%d, bclk=%d", pclk_rate, msm_host->byte_clk_rate);
649
Archit Taneja4bfa9742015-10-09 16:32:38 +0530650 msm_host->esc_clk_rate = clk_get_rate(msm_host->esc_clk);
651
652 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
653 unsigned int esc_mhz, esc_div;
654 unsigned long byte_mhz;
655
656 msm_host->src_clk_rate = (pclk_rate * bpp) / 8;
657
658 /*
659 * esc clock is byte clock followed by a 4 bit divider,
660 * we need to find an escape clock frequency within the
661 * mipi DSI spec range within the maximum divider limit
662 * We iterate here between an escape clock frequencey
663 * between 20 Mhz to 5 Mhz and pick up the first one
664 * that can be supported by our divider
665 */
666
667 byte_mhz = msm_host->byte_clk_rate / 1000000;
668
669 for (esc_mhz = 20; esc_mhz >= 5; esc_mhz--) {
670 esc_div = DIV_ROUND_UP(byte_mhz, esc_mhz);
671
672 /*
673 * TODO: Ideally, we shouldn't know what sort of divider
674 * is available in mmss_cc, we're just assuming that
675 * it'll always be a 4 bit divider. Need to come up with
676 * a better way here.
677 */
678 if (esc_div >= 1 && esc_div <= 16)
679 break;
680 }
681
682 if (esc_mhz < 5)
683 return -EINVAL;
684
685 msm_host->esc_clk_rate = msm_host->byte_clk_rate / esc_div;
686
687 DBG("esc=%d, src=%d", msm_host->esc_clk_rate,
688 msm_host->src_clk_rate);
689 }
690
Hai Lia6895542015-03-31 14:36:33 -0400691 return 0;
692}
693
694static void dsi_phy_sw_reset(struct msm_dsi_host *msm_host)
695{
696 DBG("");
697 dsi_write(msm_host, REG_DSI_PHY_RESET, DSI_PHY_RESET_RESET);
698 /* Make sure fully reset */
699 wmb();
700 udelay(1000);
701 dsi_write(msm_host, REG_DSI_PHY_RESET, 0);
702 udelay(100);
703}
704
705static void dsi_intr_ctrl(struct msm_dsi_host *msm_host, u32 mask, int enable)
706{
707 u32 intr;
708 unsigned long flags;
709
710 spin_lock_irqsave(&msm_host->intr_lock, flags);
711 intr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
712
713 if (enable)
714 intr |= mask;
715 else
716 intr &= ~mask;
717
718 DBG("intr=%x enable=%d", intr, enable);
719
720 dsi_write(msm_host, REG_DSI_INTR_CTRL, intr);
721 spin_unlock_irqrestore(&msm_host->intr_lock, flags);
722}
723
724static inline enum dsi_traffic_mode dsi_get_traffic_mode(const u32 mode_flags)
725{
726 if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
727 return BURST_MODE;
728 else if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
729 return NON_BURST_SYNCH_PULSE;
730
731 return NON_BURST_SYNCH_EVENT;
732}
733
734static inline enum dsi_vid_dst_format dsi_get_vid_fmt(
735 const enum mipi_dsi_pixel_format mipi_fmt)
736{
737 switch (mipi_fmt) {
738 case MIPI_DSI_FMT_RGB888: return VID_DST_FORMAT_RGB888;
739 case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666_LOOSE;
740 case MIPI_DSI_FMT_RGB666_PACKED: return VID_DST_FORMAT_RGB666;
741 case MIPI_DSI_FMT_RGB565: return VID_DST_FORMAT_RGB565;
742 default: return VID_DST_FORMAT_RGB888;
743 }
744}
745
746static inline enum dsi_cmd_dst_format dsi_get_cmd_fmt(
747 const enum mipi_dsi_pixel_format mipi_fmt)
748{
749 switch (mipi_fmt) {
750 case MIPI_DSI_FMT_RGB888: return CMD_DST_FORMAT_RGB888;
751 case MIPI_DSI_FMT_RGB666_PACKED:
752 case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666;
753 case MIPI_DSI_FMT_RGB565: return CMD_DST_FORMAT_RGB565;
754 default: return CMD_DST_FORMAT_RGB888;
755 }
756}
757
758static void dsi_ctrl_config(struct msm_dsi_host *msm_host, bool enable,
Hai Lidceac342016-09-15 14:34:49 +0530759 struct msm_dsi_phy_shared_timings *phy_shared_timings)
Hai Lia6895542015-03-31 14:36:33 -0400760{
761 u32 flags = msm_host->mode_flags;
762 enum mipi_dsi_pixel_format mipi_fmt = msm_host->format;
Hai Lid248b612015-08-13 17:49:29 -0400763 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400764 u32 data = 0;
765
766 if (!enable) {
767 dsi_write(msm_host, REG_DSI_CTRL, 0);
768 return;
769 }
770
771 if (flags & MIPI_DSI_MODE_VIDEO) {
772 if (flags & MIPI_DSI_MODE_VIDEO_HSE)
773 data |= DSI_VID_CFG0_PULSE_MODE_HSA_HE;
774 if (flags & MIPI_DSI_MODE_VIDEO_HFP)
775 data |= DSI_VID_CFG0_HFP_POWER_STOP;
776 if (flags & MIPI_DSI_MODE_VIDEO_HBP)
777 data |= DSI_VID_CFG0_HBP_POWER_STOP;
778 if (flags & MIPI_DSI_MODE_VIDEO_HSA)
779 data |= DSI_VID_CFG0_HSA_POWER_STOP;
780 /* Always set low power stop mode for BLLP
781 * to let command engine send packets
782 */
783 data |= DSI_VID_CFG0_EOF_BLLP_POWER_STOP |
784 DSI_VID_CFG0_BLLP_POWER_STOP;
785 data |= DSI_VID_CFG0_TRAFFIC_MODE(dsi_get_traffic_mode(flags));
786 data |= DSI_VID_CFG0_DST_FORMAT(dsi_get_vid_fmt(mipi_fmt));
787 data |= DSI_VID_CFG0_VIRT_CHANNEL(msm_host->channel);
788 dsi_write(msm_host, REG_DSI_VID_CFG0, data);
789
790 /* Do not swap RGB colors */
791 data = DSI_VID_CFG1_RGB_SWAP(SWAP_RGB);
792 dsi_write(msm_host, REG_DSI_VID_CFG1, 0);
793 } else {
794 /* Do not swap RGB colors */
795 data = DSI_CMD_CFG0_RGB_SWAP(SWAP_RGB);
796 data |= DSI_CMD_CFG0_DST_FORMAT(dsi_get_cmd_fmt(mipi_fmt));
797 dsi_write(msm_host, REG_DSI_CMD_CFG0, data);
798
799 data = DSI_CMD_CFG1_WR_MEM_START(MIPI_DCS_WRITE_MEMORY_START) |
800 DSI_CMD_CFG1_WR_MEM_CONTINUE(
801 MIPI_DCS_WRITE_MEMORY_CONTINUE);
802 /* Always insert DCS command */
803 data |= DSI_CMD_CFG1_INSERT_DCS_COMMAND;
804 dsi_write(msm_host, REG_DSI_CMD_CFG1, data);
805 }
806
807 dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL,
808 DSI_CMD_DMA_CTRL_FROM_FRAME_BUFFER |
809 DSI_CMD_DMA_CTRL_LOW_POWER);
810
811 data = 0;
812 /* Always assume dedicated TE pin */
813 data |= DSI_TRIG_CTRL_TE;
814 data |= DSI_TRIG_CTRL_MDP_TRIGGER(TRIGGER_NONE);
815 data |= DSI_TRIG_CTRL_DMA_TRIGGER(TRIGGER_SW);
816 data |= DSI_TRIG_CTRL_STREAM(msm_host->channel);
Hai Lid248b612015-08-13 17:49:29 -0400817 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
818 (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_2))
Hai Lia6895542015-03-31 14:36:33 -0400819 data |= DSI_TRIG_CTRL_BLOCK_DMA_WITHIN_FRAME;
820 dsi_write(msm_host, REG_DSI_TRIG_CTRL, data);
821
Hai Lidceac342016-09-15 14:34:49 +0530822 data = DSI_CLKOUT_TIMING_CTRL_T_CLK_POST(phy_shared_timings->clk_post) |
823 DSI_CLKOUT_TIMING_CTRL_T_CLK_PRE(phy_shared_timings->clk_pre);
Hai Lia6895542015-03-31 14:36:33 -0400824 dsi_write(msm_host, REG_DSI_CLKOUT_TIMING_CTRL, data);
825
Hai Lidceac342016-09-15 14:34:49 +0530826 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
827 (cfg_hnd->minor > MSM_DSI_6G_VER_MINOR_V1_0) &&
828 phy_shared_timings->clk_pre_inc_by_2)
829 dsi_write(msm_host, REG_DSI_T_CLK_PRE_EXTEND,
830 DSI_T_CLK_PRE_EXTEND_INC_BY_2_BYTECLK);
831
Hai Lia6895542015-03-31 14:36:33 -0400832 data = 0;
833 if (!(flags & MIPI_DSI_MODE_EOT_PACKET))
834 data |= DSI_EOT_PACKET_CTRL_TX_EOT_APPEND;
835 dsi_write(msm_host, REG_DSI_EOT_PACKET_CTRL, data);
836
837 /* allow only ack-err-status to generate interrupt */
838 dsi_write(msm_host, REG_DSI_ERR_INT_MASK0, 0x13ff3fe0);
839
840 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
841
842 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
843
844 data = DSI_CTRL_CLK_EN;
845
846 DBG("lane number=%d", msm_host->lanes);
Archit Taneja26f7d1f2016-02-25 11:19:48 +0530847 data |= ((DSI_CTRL_LANE0 << msm_host->lanes) - DSI_CTRL_LANE0);
848
849 dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
850 DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(msm_host->dlane_swap));
Archit Taneja65c5e542015-04-08 11:37:40 +0530851
852 if (!(flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
853 dsi_write(msm_host, REG_DSI_LANE_CTRL,
854 DSI_LANE_CTRL_CLKLN_HS_FORCE_REQUEST);
855
Hai Lia6895542015-03-31 14:36:33 -0400856 data |= DSI_CTRL_ENABLE;
857
858 dsi_write(msm_host, REG_DSI_CTRL, data);
859}
860
861static void dsi_timing_setup(struct msm_dsi_host *msm_host)
862{
863 struct drm_display_mode *mode = msm_host->mode;
864 u32 hs_start = 0, vs_start = 0; /* take sync start as 0 */
865 u32 h_total = mode->htotal;
866 u32 v_total = mode->vtotal;
867 u32 hs_end = mode->hsync_end - mode->hsync_start;
868 u32 vs_end = mode->vsync_end - mode->vsync_start;
869 u32 ha_start = h_total - mode->hsync_start;
870 u32 ha_end = ha_start + mode->hdisplay;
871 u32 va_start = v_total - mode->vsync_start;
872 u32 va_end = va_start + mode->vdisplay;
873 u32 wc;
874
875 DBG("");
876
877 if (msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) {
878 dsi_write(msm_host, REG_DSI_ACTIVE_H,
879 DSI_ACTIVE_H_START(ha_start) |
880 DSI_ACTIVE_H_END(ha_end));
881 dsi_write(msm_host, REG_DSI_ACTIVE_V,
882 DSI_ACTIVE_V_START(va_start) |
883 DSI_ACTIVE_V_END(va_end));
884 dsi_write(msm_host, REG_DSI_TOTAL,
885 DSI_TOTAL_H_TOTAL(h_total - 1) |
886 DSI_TOTAL_V_TOTAL(v_total - 1));
887
888 dsi_write(msm_host, REG_DSI_ACTIVE_HSYNC,
889 DSI_ACTIVE_HSYNC_START(hs_start) |
890 DSI_ACTIVE_HSYNC_END(hs_end));
891 dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_HPOS, 0);
892 dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_VPOS,
893 DSI_ACTIVE_VSYNC_VPOS_START(vs_start) |
894 DSI_ACTIVE_VSYNC_VPOS_END(vs_end));
895 } else { /* command mode */
896 /* image data and 1 byte write_memory_start cmd */
897 wc = mode->hdisplay * dsi_get_bpp(msm_host->format) / 8 + 1;
898
899 dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_CTRL,
900 DSI_CMD_MDP_STREAM_CTRL_WORD_COUNT(wc) |
901 DSI_CMD_MDP_STREAM_CTRL_VIRTUAL_CHANNEL(
902 msm_host->channel) |
903 DSI_CMD_MDP_STREAM_CTRL_DATA_TYPE(
904 MIPI_DSI_DCS_LONG_WRITE));
905
906 dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_TOTAL,
907 DSI_CMD_MDP_STREAM_TOTAL_H_TOTAL(mode->hdisplay) |
908 DSI_CMD_MDP_STREAM_TOTAL_V_TOTAL(mode->vdisplay));
909 }
910}
911
912static void dsi_sw_reset(struct msm_dsi_host *msm_host)
913{
914 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
915 wmb(); /* clocks need to be enabled before reset */
916
917 dsi_write(msm_host, REG_DSI_RESET, 1);
918 wmb(); /* make sure reset happen */
919 dsi_write(msm_host, REG_DSI_RESET, 0);
920}
921
922static void dsi_op_mode_config(struct msm_dsi_host *msm_host,
923 bool video_mode, bool enable)
924{
925 u32 dsi_ctrl;
926
927 dsi_ctrl = dsi_read(msm_host, REG_DSI_CTRL);
928
929 if (!enable) {
930 dsi_ctrl &= ~(DSI_CTRL_ENABLE | DSI_CTRL_VID_MODE_EN |
931 DSI_CTRL_CMD_MODE_EN);
932 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE |
933 DSI_IRQ_MASK_VIDEO_DONE, 0);
934 } else {
935 if (video_mode) {
936 dsi_ctrl |= DSI_CTRL_VID_MODE_EN;
937 } else { /* command mode */
938 dsi_ctrl |= DSI_CTRL_CMD_MODE_EN;
939 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE, 1);
940 }
941 dsi_ctrl |= DSI_CTRL_ENABLE;
942 }
943
944 dsi_write(msm_host, REG_DSI_CTRL, dsi_ctrl);
945}
946
947static void dsi_set_tx_power_mode(int mode, struct msm_dsi_host *msm_host)
948{
949 u32 data;
950
951 data = dsi_read(msm_host, REG_DSI_CMD_DMA_CTRL);
952
953 if (mode == 0)
954 data &= ~DSI_CMD_DMA_CTRL_LOW_POWER;
955 else
956 data |= DSI_CMD_DMA_CTRL_LOW_POWER;
957
958 dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL, data);
959}
960
961static void dsi_wait4video_done(struct msm_dsi_host *msm_host)
962{
963 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 1);
964
965 reinit_completion(&msm_host->video_comp);
966
967 wait_for_completion_timeout(&msm_host->video_comp,
968 msecs_to_jiffies(70));
969
970 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 0);
971}
972
973static void dsi_wait4video_eng_busy(struct msm_dsi_host *msm_host)
974{
975 if (!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO))
976 return;
977
978 if (msm_host->power_on) {
979 dsi_wait4video_done(msm_host);
980 /* delay 4 ms to skip BLLP */
981 usleep_range(2000, 4000);
982 }
983}
984
985/* dsi_cmd */
986static int dsi_tx_buf_alloc(struct msm_dsi_host *msm_host, int size)
987{
988 struct drm_device *dev = msm_host->dev;
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530989 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400990 int ret;
Rob Clark78babc12016-11-11 12:06:46 -0500991 uint64_t iova;
Hai Lia6895542015-03-31 14:36:33 -0400992
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530993 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
994 mutex_lock(&dev->struct_mutex);
995 msm_host->tx_gem_obj = msm_gem_new(dev, size, MSM_BO_UNCACHED);
996 if (IS_ERR(msm_host->tx_gem_obj)) {
997 ret = PTR_ERR(msm_host->tx_gem_obj);
998 pr_err("%s: failed to allocate gem, %d\n",
999 __func__, ret);
1000 msm_host->tx_gem_obj = NULL;
1001 mutex_unlock(&dev->struct_mutex);
1002 return ret;
1003 }
1004
1005 ret = msm_gem_get_iova_locked(msm_host->tx_gem_obj, 0, &iova);
saurabhbeb107f2015-12-07 01:19:21 +05301006 mutex_unlock(&dev->struct_mutex);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301007 if (ret) {
1008 pr_err("%s: failed to get iova, %d\n", __func__, ret);
1009 return ret;
1010 }
Hai Lia6895542015-03-31 14:36:33 -04001011
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301012 if (iova & 0x07) {
1013 pr_err("%s: buf NOT 8 bytes aligned\n", __func__);
1014 return -EINVAL;
1015 }
Hai Lia6895542015-03-31 14:36:33 -04001016
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301017 msm_host->tx_size = msm_host->tx_gem_obj->size;
1018 } else {
1019 msm_host->tx_buf = dma_alloc_coherent(dev->dev, size,
1020 &msm_host->tx_buf_paddr, GFP_KERNEL);
1021 if (!msm_host->tx_buf) {
1022 ret = -ENOMEM;
1023 pr_err("%s: failed to allocate tx buf, %d\n",
1024 __func__, ret);
1025 return ret;
1026 }
1027
1028 msm_host->tx_size = size;
Hai Lia6895542015-03-31 14:36:33 -04001029 }
1030
1031 return 0;
1032}
1033
1034static void dsi_tx_buf_free(struct msm_dsi_host *msm_host)
1035{
1036 struct drm_device *dev = msm_host->dev;
1037
1038 if (msm_host->tx_gem_obj) {
1039 msm_gem_put_iova(msm_host->tx_gem_obj, 0);
1040 mutex_lock(&dev->struct_mutex);
1041 msm_gem_free_object(msm_host->tx_gem_obj);
1042 msm_host->tx_gem_obj = NULL;
1043 mutex_unlock(&dev->struct_mutex);
1044 }
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301045
1046 if (msm_host->tx_buf)
1047 dma_free_coherent(dev->dev, msm_host->tx_size, msm_host->tx_buf,
1048 msm_host->tx_buf_paddr);
Hai Lia6895542015-03-31 14:36:33 -04001049}
1050
1051/*
1052 * prepare cmd buffer to be txed
1053 */
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301054static int dsi_cmd_dma_add(struct msm_dsi_host *msm_host,
1055 const struct mipi_dsi_msg *msg)
Hai Lia6895542015-03-31 14:36:33 -04001056{
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301057 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -04001058 struct mipi_dsi_packet packet;
1059 int len;
1060 int ret;
1061 u8 *data;
1062
1063 ret = mipi_dsi_create_packet(&packet, msg);
1064 if (ret) {
1065 pr_err("%s: create packet failed, %d\n", __func__, ret);
1066 return ret;
1067 }
1068 len = (packet.size + 3) & (~0x3);
1069
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301070 if (len > msm_host->tx_size) {
Hai Lia6895542015-03-31 14:36:33 -04001071 pr_err("%s: packet size is too big\n", __func__);
1072 return -EINVAL;
1073 }
1074
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301075 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
Rob Clark18f23042016-05-26 16:24:35 -04001076 data = msm_gem_get_vaddr(msm_host->tx_gem_obj);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301077 if (IS_ERR(data)) {
1078 ret = PTR_ERR(data);
1079 pr_err("%s: get vaddr failed, %d\n", __func__, ret);
1080 return ret;
1081 }
1082 } else {
1083 data = msm_host->tx_buf;
Hai Lia6895542015-03-31 14:36:33 -04001084 }
1085
1086 /* MSM specific command format in memory */
1087 data[0] = packet.header[1];
1088 data[1] = packet.header[2];
1089 data[2] = packet.header[0];
1090 data[3] = BIT(7); /* Last packet */
1091 if (mipi_dsi_packet_format_is_long(msg->type))
1092 data[3] |= BIT(6);
1093 if (msg->rx_buf && msg->rx_len)
1094 data[3] |= BIT(5);
1095
1096 /* Long packet */
1097 if (packet.payload && packet.payload_length)
1098 memcpy(data + 4, packet.payload, packet.payload_length);
1099
1100 /* Append 0xff to the end */
1101 if (packet.size < len)
1102 memset(data + packet.size, 0xff, len - packet.size);
1103
Rob Clark18f23042016-05-26 16:24:35 -04001104 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G)
1105 msm_gem_put_vaddr(msm_host->tx_gem_obj);
1106
Hai Lia6895542015-03-31 14:36:33 -04001107 return len;
1108}
1109
1110/*
1111 * dsi_short_read1_resp: 1 parameter
1112 */
1113static int dsi_short_read1_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1114{
1115 u8 *data = msg->rx_buf;
1116 if (data && (msg->rx_len >= 1)) {
1117 *data = buf[1]; /* strip out dcs type */
1118 return 1;
1119 } else {
Stephane Viau981371f2015-04-30 10:39:26 -04001120 pr_err("%s: read data does not match with rx_buf len %zu\n",
Hai Lia6895542015-03-31 14:36:33 -04001121 __func__, msg->rx_len);
1122 return -EINVAL;
1123 }
1124}
1125
1126/*
1127 * dsi_short_read2_resp: 2 parameter
1128 */
1129static int dsi_short_read2_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1130{
1131 u8 *data = msg->rx_buf;
1132 if (data && (msg->rx_len >= 2)) {
1133 data[0] = buf[1]; /* strip out dcs type */
1134 data[1] = buf[2];
1135 return 2;
1136 } else {
Stephane Viau981371f2015-04-30 10:39:26 -04001137 pr_err("%s: read data does not match with rx_buf len %zu\n",
Hai Lia6895542015-03-31 14:36:33 -04001138 __func__, msg->rx_len);
1139 return -EINVAL;
1140 }
1141}
1142
1143static int dsi_long_read_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1144{
1145 /* strip out 4 byte dcs header */
1146 if (msg->rx_buf && msg->rx_len)
1147 memcpy(msg->rx_buf, buf + 4, msg->rx_len);
1148
1149 return msg->rx_len;
1150}
1151
Hai Lia6895542015-03-31 14:36:33 -04001152static int dsi_cmd_dma_tx(struct msm_dsi_host *msm_host, int len)
1153{
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301154 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -04001155 int ret;
Rob Clark78babc12016-11-11 12:06:46 -05001156 uint64_t dma_base;
Hai Lia6895542015-03-31 14:36:33 -04001157 bool triggered;
1158
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301159 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
1160 ret = msm_gem_get_iova(msm_host->tx_gem_obj, 0, &dma_base);
1161 if (ret) {
1162 pr_err("%s: failed to get iova: %d\n", __func__, ret);
1163 return ret;
1164 }
1165 } else {
1166 dma_base = msm_host->tx_buf_paddr;
Hai Lia6895542015-03-31 14:36:33 -04001167 }
1168
1169 reinit_completion(&msm_host->dma_comp);
1170
1171 dsi_wait4video_eng_busy(msm_host);
1172
1173 triggered = msm_dsi_manager_cmd_xfer_trigger(
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301174 msm_host->id, dma_base, len);
Hai Lia6895542015-03-31 14:36:33 -04001175 if (triggered) {
1176 ret = wait_for_completion_timeout(&msm_host->dma_comp,
1177 msecs_to_jiffies(200));
1178 DBG("ret=%d", ret);
1179 if (ret == 0)
1180 ret = -ETIMEDOUT;
1181 else
1182 ret = len;
1183 } else
1184 ret = len;
1185
1186 return ret;
1187}
1188
1189static int dsi_cmd_dma_rx(struct msm_dsi_host *msm_host,
1190 u8 *buf, int rx_byte, int pkt_size)
1191{
1192 u32 *lp, *temp, data;
1193 int i, j = 0, cnt;
Hai Lia6895542015-03-31 14:36:33 -04001194 u32 read_cnt;
1195 u8 reg[16];
1196 int repeated_bytes = 0;
1197 int buf_offset = buf - msm_host->rx_buf;
1198
1199 lp = (u32 *)buf;
1200 temp = (u32 *)reg;
1201 cnt = (rx_byte + 3) >> 2;
1202 if (cnt > 4)
1203 cnt = 4; /* 4 x 32 bits registers only */
1204
Hai Liec1936e2015-04-29 11:39:00 -04001205 if (rx_byte == 4)
1206 read_cnt = 4;
1207 else
1208 read_cnt = pkt_size + 6;
Hai Lia6895542015-03-31 14:36:33 -04001209
1210 /*
1211 * In case of multiple reads from the panel, after the first read, there
1212 * is possibility that there are some bytes in the payload repeating in
1213 * the RDBK_DATA registers. Since we read all the parameters from the
1214 * panel right from the first byte for every pass. We need to skip the
1215 * repeating bytes and then append the new parameters to the rx buffer.
1216 */
1217 if (read_cnt > 16) {
1218 int bytes_shifted;
1219 /* Any data more than 16 bytes will be shifted out.
1220 * The temp read buffer should already contain these bytes.
1221 * The remaining bytes in read buffer are the repeated bytes.
1222 */
1223 bytes_shifted = read_cnt - 16;
1224 repeated_bytes = buf_offset - bytes_shifted;
1225 }
1226
1227 for (i = cnt - 1; i >= 0; i--) {
1228 data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
1229 *temp++ = ntohl(data); /* to host byte order */
1230 DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
1231 }
1232
1233 for (i = repeated_bytes; i < 16; i++)
1234 buf[j++] = reg[i];
1235
1236 return j;
1237}
1238
1239static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
1240 const struct mipi_dsi_msg *msg)
1241{
1242 int len, ret;
1243 int bllp_len = msm_host->mode->hdisplay *
1244 dsi_get_bpp(msm_host->format) / 8;
1245
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301246 len = dsi_cmd_dma_add(msm_host, msg);
Hai Lia6895542015-03-31 14:36:33 -04001247 if (!len) {
1248 pr_err("%s: failed to add cmd type = 0x%x\n",
1249 __func__, msg->type);
1250 return -EINVAL;
1251 }
1252
1253 /* for video mode, do not send cmds more than
1254 * one pixel line, since it only transmit it
1255 * during BLLP.
1256 */
1257 /* TODO: if the command is sent in LP mode, the bit rate is only
1258 * half of esc clk rate. In this case, if the video is already
1259 * actively streaming, we need to check more carefully if the
1260 * command can be fit into one BLLP.
1261 */
1262 if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
1263 pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
1264 __func__, len);
1265 return -EINVAL;
1266 }
1267
1268 ret = dsi_cmd_dma_tx(msm_host, len);
1269 if (ret < len) {
1270 pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d\n",
1271 __func__, msg->type, (*(u8 *)(msg->tx_buf)), len);
1272 return -ECOMM;
1273 }
1274
1275 return len;
1276}
1277
1278static void dsi_sw_reset_restore(struct msm_dsi_host *msm_host)
1279{
1280 u32 data0, data1;
1281
1282 data0 = dsi_read(msm_host, REG_DSI_CTRL);
1283 data1 = data0;
1284 data1 &= ~DSI_CTRL_ENABLE;
1285 dsi_write(msm_host, REG_DSI_CTRL, data1);
1286 /*
1287 * dsi controller need to be disabled before
1288 * clocks turned on
1289 */
1290 wmb();
1291
1292 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
1293 wmb(); /* make sure clocks enabled */
1294
1295 /* dsi controller can only be reset while clocks are running */
1296 dsi_write(msm_host, REG_DSI_RESET, 1);
1297 wmb(); /* make sure reset happen */
1298 dsi_write(msm_host, REG_DSI_RESET, 0);
1299 wmb(); /* controller out of reset */
1300 dsi_write(msm_host, REG_DSI_CTRL, data0);
1301 wmb(); /* make sure dsi controller enabled again */
1302}
1303
Archit Taneja8d23ea42016-10-25 12:17:59 +05301304static void dsi_hpd_worker(struct work_struct *work)
1305{
1306 struct msm_dsi_host *msm_host =
1307 container_of(work, struct msm_dsi_host, hpd_work);
1308
1309 drm_helper_hpd_irq_event(msm_host->dev);
1310}
1311
Hai Lia6895542015-03-31 14:36:33 -04001312static void dsi_err_worker(struct work_struct *work)
1313{
1314 struct msm_dsi_host *msm_host =
1315 container_of(work, struct msm_dsi_host, err_work);
1316 u32 status = msm_host->err_work_state;
1317
Rob Clarkff431fa2015-05-07 15:19:02 -04001318 pr_err_ratelimited("%s: status=%x\n", __func__, status);
Hai Lia6895542015-03-31 14:36:33 -04001319 if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
1320 dsi_sw_reset_restore(msm_host);
1321
1322 /* It is safe to clear here because error irq is disabled. */
1323 msm_host->err_work_state = 0;
1324
1325 /* enable dsi error interrupt */
1326 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
1327}
1328
1329static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
1330{
1331 u32 status;
1332
1333 status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
1334
1335 if (status) {
1336 dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
1337 /* Writing of an extra 0 needed to clear error bits */
1338 dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
1339 msm_host->err_work_state |= DSI_ERR_STATE_ACK;
1340 }
1341}
1342
1343static void dsi_timeout_status(struct msm_dsi_host *msm_host)
1344{
1345 u32 status;
1346
1347 status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
1348
1349 if (status) {
1350 dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
1351 msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
1352 }
1353}
1354
1355static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
1356{
1357 u32 status;
1358
1359 status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
1360
Archit Taneja01199362015-06-25 11:29:24 +05301361 if (status & (DSI_DLN0_PHY_ERR_DLN0_ERR_ESC |
1362 DSI_DLN0_PHY_ERR_DLN0_ERR_SYNC_ESC |
1363 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTROL |
1364 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP0 |
1365 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP1)) {
Hai Lia6895542015-03-31 14:36:33 -04001366 dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
1367 msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
1368 }
1369}
1370
1371static void dsi_fifo_status(struct msm_dsi_host *msm_host)
1372{
1373 u32 status;
1374
1375 status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
1376
1377 /* fifo underflow, overflow */
1378 if (status) {
1379 dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
1380 msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
1381 if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
1382 msm_host->err_work_state |=
1383 DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
1384 }
1385}
1386
1387static void dsi_status(struct msm_dsi_host *msm_host)
1388{
1389 u32 status;
1390
1391 status = dsi_read(msm_host, REG_DSI_STATUS0);
1392
1393 if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
1394 dsi_write(msm_host, REG_DSI_STATUS0, status);
1395 msm_host->err_work_state |=
1396 DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
1397 }
1398}
1399
1400static void dsi_clk_status(struct msm_dsi_host *msm_host)
1401{
1402 u32 status;
1403
1404 status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
1405
1406 if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
1407 dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
1408 msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
1409 }
1410}
1411
1412static void dsi_error(struct msm_dsi_host *msm_host)
1413{
1414 /* disable dsi error interrupt */
1415 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
1416
1417 dsi_clk_status(msm_host);
1418 dsi_fifo_status(msm_host);
1419 dsi_ack_err_status(msm_host);
1420 dsi_timeout_status(msm_host);
1421 dsi_status(msm_host);
1422 dsi_dln0_phy_err(msm_host);
1423
1424 queue_work(msm_host->workqueue, &msm_host->err_work);
1425}
1426
1427static irqreturn_t dsi_host_irq(int irq, void *ptr)
1428{
1429 struct msm_dsi_host *msm_host = ptr;
1430 u32 isr;
1431 unsigned long flags;
1432
1433 if (!msm_host->ctrl_base)
1434 return IRQ_HANDLED;
1435
1436 spin_lock_irqsave(&msm_host->intr_lock, flags);
1437 isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
1438 dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
1439 spin_unlock_irqrestore(&msm_host->intr_lock, flags);
1440
1441 DBG("isr=0x%x, id=%d", isr, msm_host->id);
1442
1443 if (isr & DSI_IRQ_ERROR)
1444 dsi_error(msm_host);
1445
1446 if (isr & DSI_IRQ_VIDEO_DONE)
1447 complete(&msm_host->video_comp);
1448
1449 if (isr & DSI_IRQ_CMD_DMA_DONE)
1450 complete(&msm_host->dma_comp);
1451
1452 return IRQ_HANDLED;
1453}
1454
1455static int dsi_host_init_panel_gpios(struct msm_dsi_host *msm_host,
1456 struct device *panel_device)
1457{
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001458 msm_host->disp_en_gpio = devm_gpiod_get_optional(panel_device,
1459 "disp-enable",
1460 GPIOD_OUT_LOW);
Hai Lia6895542015-03-31 14:36:33 -04001461 if (IS_ERR(msm_host->disp_en_gpio)) {
1462 DBG("cannot get disp-enable-gpios %ld",
1463 PTR_ERR(msm_host->disp_en_gpio));
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001464 return PTR_ERR(msm_host->disp_en_gpio);
Hai Lia6895542015-03-31 14:36:33 -04001465 }
1466
Archit Taneja60d05cb2015-06-25 14:36:35 +05301467 msm_host->te_gpio = devm_gpiod_get_optional(panel_device, "disp-te",
1468 GPIOD_IN);
Hai Lia6895542015-03-31 14:36:33 -04001469 if (IS_ERR(msm_host->te_gpio)) {
1470 DBG("cannot get disp-te-gpios %ld", PTR_ERR(msm_host->te_gpio));
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001471 return PTR_ERR(msm_host->te_gpio);
Hai Lia6895542015-03-31 14:36:33 -04001472 }
1473
1474 return 0;
1475}
1476
1477static int dsi_host_attach(struct mipi_dsi_host *host,
1478 struct mipi_dsi_device *dsi)
1479{
1480 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1481 int ret;
1482
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301483 if (dsi->lanes > msm_host->num_data_lanes)
1484 return -EINVAL;
1485
Hai Lia6895542015-03-31 14:36:33 -04001486 msm_host->channel = dsi->channel;
1487 msm_host->lanes = dsi->lanes;
1488 msm_host->format = dsi->format;
1489 msm_host->mode_flags = dsi->mode_flags;
1490
Archit Taneja9c9f6f82016-12-05 15:24:53 +05301491 msm_dsi_manager_attach_dsi_device(msm_host->id, dsi->mode_flags);
1492
Hai Lia6895542015-03-31 14:36:33 -04001493 /* Some gpios defined in panel DT need to be controlled by host */
1494 ret = dsi_host_init_panel_gpios(msm_host, &dsi->dev);
1495 if (ret)
1496 return ret;
1497
1498 DBG("id=%d", msm_host->id);
1499 if (msm_host->dev)
Archit Taneja8d23ea42016-10-25 12:17:59 +05301500 queue_work(msm_host->workqueue, &msm_host->hpd_work);
Hai Lia6895542015-03-31 14:36:33 -04001501
1502 return 0;
1503}
1504
1505static int dsi_host_detach(struct mipi_dsi_host *host,
1506 struct mipi_dsi_device *dsi)
1507{
1508 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1509
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301510 msm_host->device_node = NULL;
Hai Lia6895542015-03-31 14:36:33 -04001511
1512 DBG("id=%d", msm_host->id);
1513 if (msm_host->dev)
Archit Taneja8d23ea42016-10-25 12:17:59 +05301514 queue_work(msm_host->workqueue, &msm_host->hpd_work);
Hai Lia6895542015-03-31 14:36:33 -04001515
1516 return 0;
1517}
1518
1519static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
1520 const struct mipi_dsi_msg *msg)
1521{
1522 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1523 int ret;
1524
1525 if (!msg || !msm_host->power_on)
1526 return -EINVAL;
1527
1528 mutex_lock(&msm_host->cmd_mutex);
1529 ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
1530 mutex_unlock(&msm_host->cmd_mutex);
1531
1532 return ret;
1533}
1534
1535static struct mipi_dsi_host_ops dsi_host_ops = {
1536 .attach = dsi_host_attach,
1537 .detach = dsi_host_detach,
1538 .transfer = dsi_host_transfer,
1539};
1540
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301541/*
1542 * List of supported physical to logical lane mappings.
1543 * For example, the 2nd entry represents the following mapping:
1544 *
1545 * "3012": Logic 3->Phys 0; Logic 0->Phys 1; Logic 1->Phys 2; Logic 2->Phys 3;
1546 */
1547static const int supported_data_lane_swaps[][4] = {
1548 { 0, 1, 2, 3 },
1549 { 3, 0, 1, 2 },
1550 { 2, 3, 0, 1 },
1551 { 1, 2, 3, 0 },
1552 { 0, 3, 2, 1 },
1553 { 1, 0, 3, 2 },
1554 { 2, 1, 0, 3 },
1555 { 3, 2, 1, 0 },
1556};
1557
1558static int dsi_host_parse_lane_data(struct msm_dsi_host *msm_host,
1559 struct device_node *ep)
1560{
1561 struct device *dev = &msm_host->pdev->dev;
1562 struct property *prop;
1563 u32 lane_map[4];
1564 int ret, i, len, num_lanes;
1565
Archit Taneja60282ce2016-06-08 16:14:19 +05301566 prop = of_find_property(ep, "data-lanes", &len);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301567 if (!prop) {
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301568 dev_dbg(dev,
1569 "failed to find data lane mapping, using default\n");
1570 return 0;
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301571 }
1572
1573 num_lanes = len / sizeof(u32);
1574
1575 if (num_lanes < 1 || num_lanes > 4) {
1576 dev_err(dev, "bad number of data lanes\n");
1577 return -EINVAL;
1578 }
1579
1580 msm_host->num_data_lanes = num_lanes;
1581
Archit Taneja60282ce2016-06-08 16:14:19 +05301582 ret = of_property_read_u32_array(ep, "data-lanes", lane_map,
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301583 num_lanes);
1584 if (ret) {
1585 dev_err(dev, "failed to read lane data\n");
1586 return ret;
1587 }
1588
1589 /*
1590 * compare DT specified physical-logical lane mappings with the ones
1591 * supported by hardware
1592 */
1593 for (i = 0; i < ARRAY_SIZE(supported_data_lane_swaps); i++) {
1594 const int *swap = supported_data_lane_swaps[i];
1595 int j;
1596
Archit Taneja60282ce2016-06-08 16:14:19 +05301597 /*
1598 * the data-lanes array we get from DT has a logical->physical
1599 * mapping. The "data lane swap" register field represents
1600 * supported configurations in a physical->logical mapping.
1601 * Translate the DT mapping to what we understand and find a
1602 * configuration that works.
1603 */
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301604 for (j = 0; j < num_lanes; j++) {
Archit Taneja60282ce2016-06-08 16:14:19 +05301605 if (lane_map[j] < 0 || lane_map[j] > 3)
1606 dev_err(dev, "bad physical lane entry %u\n",
1607 lane_map[j]);
1608
1609 if (swap[lane_map[j]] != j)
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301610 break;
1611 }
1612
1613 if (j == num_lanes) {
1614 msm_host->dlane_swap = i;
1615 return 0;
1616 }
1617 }
1618
1619 return -EINVAL;
1620}
1621
Archit Tanejaf7009d22015-06-25 11:43:40 +05301622static int dsi_host_parse_dt(struct msm_dsi_host *msm_host)
1623{
1624 struct device *dev = &msm_host->pdev->dev;
1625 struct device_node *np = dev->of_node;
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301626 struct device_node *endpoint, *device_node;
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301627 int ret = 0;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301628
Archit Tanejaf7009d22015-06-25 11:43:40 +05301629 /*
Archit Tanejab9ac76f2016-04-27 15:36:53 +05301630 * Get the endpoint of the output port of the DSI host. In our case,
1631 * this is mapped to port number with reg = 1. Don't return an error if
1632 * the remote endpoint isn't defined. It's possible that there is
1633 * nothing connected to the dsi output.
Archit Tanejaf7009d22015-06-25 11:43:40 +05301634 */
Archit Tanejab9ac76f2016-04-27 15:36:53 +05301635 endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);
Archit Tanejaf7009d22015-06-25 11:43:40 +05301636 if (!endpoint) {
1637 dev_dbg(dev, "%s: no endpoint\n", __func__);
1638 return 0;
1639 }
1640
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301641 ret = dsi_host_parse_lane_data(msm_host, endpoint);
1642 if (ret) {
1643 dev_err(dev, "%s: invalid lane configuration %d\n",
1644 __func__, ret);
1645 goto err;
1646 }
1647
Archit Tanejaf7009d22015-06-25 11:43:40 +05301648 /* Get panel node from the output port's endpoint data */
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301649 device_node = of_graph_get_remote_port_parent(endpoint);
1650 if (!device_node) {
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301651 dev_dbg(dev, "%s: no valid device\n", __func__);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301652 goto err;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301653 }
1654
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301655 msm_host->device_node = device_node;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301656
Archit Taneja0c7df472015-10-14 15:31:13 +05301657 if (of_property_read_bool(np, "syscon-sfpb")) {
1658 msm_host->sfpb = syscon_regmap_lookup_by_phandle(np,
1659 "syscon-sfpb");
1660 if (IS_ERR(msm_host->sfpb)) {
1661 dev_err(dev, "%s: failed to get sfpb regmap\n",
1662 __func__);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301663 ret = PTR_ERR(msm_host->sfpb);
Archit Taneja0c7df472015-10-14 15:31:13 +05301664 }
1665 }
1666
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301667 of_node_put(device_node);
1668
1669err:
1670 of_node_put(endpoint);
1671
1672 return ret;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301673}
1674
Archit Taneja32280d62016-06-23 15:26:04 +05301675static int dsi_host_get_id(struct msm_dsi_host *msm_host)
1676{
1677 struct platform_device *pdev = msm_host->pdev;
1678 const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
1679 struct resource *res;
1680 int i;
1681
1682 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_ctrl");
1683 if (!res)
1684 return -EINVAL;
1685
1686 for (i = 0; i < cfg->num_dsi; i++) {
1687 if (cfg->io_start[i] == res->start)
1688 return i;
1689 }
1690
1691 return -EINVAL;
1692}
1693
Hai Lia6895542015-03-31 14:36:33 -04001694int msm_dsi_host_init(struct msm_dsi *msm_dsi)
1695{
1696 struct msm_dsi_host *msm_host = NULL;
1697 struct platform_device *pdev = msm_dsi->pdev;
1698 int ret;
1699
1700 msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
1701 if (!msm_host) {
1702 pr_err("%s: FAILED: cannot alloc dsi host\n",
1703 __func__);
1704 ret = -ENOMEM;
1705 goto fail;
1706 }
1707
Archit Tanejaf7009d22015-06-25 11:43:40 +05301708 msm_host->pdev = pdev;
1709
1710 ret = dsi_host_parse_dt(msm_host);
Hai Lia6895542015-03-31 14:36:33 -04001711 if (ret) {
Archit Tanejaf7009d22015-06-25 11:43:40 +05301712 pr_err("%s: failed to parse dt\n", __func__);
Hai Lia6895542015-03-31 14:36:33 -04001713 goto fail;
1714 }
Hai Lia6895542015-03-31 14:36:33 -04001715
Hai Lia6895542015-03-31 14:36:33 -04001716 msm_host->ctrl_base = msm_ioremap(pdev, "dsi_ctrl", "DSI CTRL");
1717 if (IS_ERR(msm_host->ctrl_base)) {
1718 pr_err("%s: unable to map Dsi ctrl base\n", __func__);
1719 ret = PTR_ERR(msm_host->ctrl_base);
1720 goto fail;
1721 }
1722
Hai Lid248b612015-08-13 17:49:29 -04001723 msm_host->cfg_hnd = dsi_get_config(msm_host);
1724 if (!msm_host->cfg_hnd) {
Hai Lia6895542015-03-31 14:36:33 -04001725 ret = -EINVAL;
1726 pr_err("%s: get config failed\n", __func__);
1727 goto fail;
1728 }
1729
Archit Taneja32280d62016-06-23 15:26:04 +05301730 msm_host->id = dsi_host_get_id(msm_host);
1731 if (msm_host->id < 0) {
1732 ret = msm_host->id;
1733 pr_err("%s: unable to identify DSI host index\n", __func__);
1734 goto fail;
1735 }
1736
Hai Lid248b612015-08-13 17:49:29 -04001737 /* fixup base address by io offset */
1738 msm_host->ctrl_base += msm_host->cfg_hnd->cfg->io_offset;
1739
Hai Lia6895542015-03-31 14:36:33 -04001740 ret = dsi_regulator_init(msm_host);
1741 if (ret) {
1742 pr_err("%s: regulator init failed\n", __func__);
1743 goto fail;
1744 }
1745
Archit Taneja31c92762015-10-09 12:40:39 +05301746 ret = dsi_clk_init(msm_host);
1747 if (ret) {
1748 pr_err("%s: unable to initialize dsi clks\n", __func__);
1749 goto fail;
1750 }
1751
Hai Lia6895542015-03-31 14:36:33 -04001752 msm_host->rx_buf = devm_kzalloc(&pdev->dev, SZ_4K, GFP_KERNEL);
1753 if (!msm_host->rx_buf) {
1754 pr_err("%s: alloc rx temp buf failed\n", __func__);
1755 goto fail;
1756 }
1757
1758 init_completion(&msm_host->dma_comp);
1759 init_completion(&msm_host->video_comp);
1760 mutex_init(&msm_host->dev_mutex);
1761 mutex_init(&msm_host->cmd_mutex);
1762 mutex_init(&msm_host->clk_mutex);
1763 spin_lock_init(&msm_host->intr_lock);
1764
1765 /* setup workqueue */
1766 msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
1767 INIT_WORK(&msm_host->err_work, dsi_err_worker);
Archit Taneja8d23ea42016-10-25 12:17:59 +05301768 INIT_WORK(&msm_host->hpd_work, dsi_hpd_worker);
Hai Lia6895542015-03-31 14:36:33 -04001769
Hai Lia6895542015-03-31 14:36:33 -04001770 msm_dsi->host = &msm_host->base;
1771 msm_dsi->id = msm_host->id;
1772
1773 DBG("Dsi Host %d initialized", msm_host->id);
1774 return 0;
1775
1776fail:
1777 return ret;
1778}
1779
1780void msm_dsi_host_destroy(struct mipi_dsi_host *host)
1781{
1782 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1783
1784 DBG("");
1785 dsi_tx_buf_free(msm_host);
1786 if (msm_host->workqueue) {
1787 flush_workqueue(msm_host->workqueue);
1788 destroy_workqueue(msm_host->workqueue);
1789 msm_host->workqueue = NULL;
1790 }
1791
1792 mutex_destroy(&msm_host->clk_mutex);
1793 mutex_destroy(&msm_host->cmd_mutex);
1794 mutex_destroy(&msm_host->dev_mutex);
1795}
1796
1797int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
1798 struct drm_device *dev)
1799{
1800 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1801 struct platform_device *pdev = msm_host->pdev;
1802 int ret;
1803
1804 msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1805 if (msm_host->irq < 0) {
1806 ret = msm_host->irq;
1807 dev_err(dev->dev, "failed to get irq: %d\n", ret);
1808 return ret;
1809 }
1810
1811 ret = devm_request_irq(&pdev->dev, msm_host->irq,
1812 dsi_host_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1813 "dsi_isr", msm_host);
1814 if (ret < 0) {
1815 dev_err(&pdev->dev, "failed to request IRQ%u: %d\n",
1816 msm_host->irq, ret);
1817 return ret;
1818 }
1819
1820 msm_host->dev = dev;
1821 ret = dsi_tx_buf_alloc(msm_host, SZ_4K);
1822 if (ret) {
1823 pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
1824 return ret;
1825 }
1826
1827 return 0;
1828}
1829
1830int msm_dsi_host_register(struct mipi_dsi_host *host, bool check_defer)
1831{
1832 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lia6895542015-03-31 14:36:33 -04001833 int ret;
1834
1835 /* Register mipi dsi host */
1836 if (!msm_host->registered) {
1837 host->dev = &msm_host->pdev->dev;
1838 host->ops = &dsi_host_ops;
1839 ret = mipi_dsi_host_register(host);
1840 if (ret)
1841 return ret;
1842
1843 msm_host->registered = true;
1844
1845 /* If the panel driver has not been probed after host register,
1846 * we should defer the host's probe.
1847 * It makes sure panel is connected when fbcon detects
1848 * connector status and gets the proper display mode to
1849 * create framebuffer.
Archit Tanejaf7009d22015-06-25 11:43:40 +05301850 * Don't try to defer if there is nothing connected to the dsi
1851 * output
Hai Lia6895542015-03-31 14:36:33 -04001852 */
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301853 if (check_defer && msm_host->device_node) {
1854 if (!of_drm_find_panel(msm_host->device_node))
Archit Tanejac118e292015-07-31 14:06:10 +05301855 if (!of_drm_find_bridge(msm_host->device_node))
1856 return -EPROBE_DEFER;
Hai Lia6895542015-03-31 14:36:33 -04001857 }
1858 }
1859
1860 return 0;
1861}
1862
1863void msm_dsi_host_unregister(struct mipi_dsi_host *host)
1864{
1865 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1866
1867 if (msm_host->registered) {
1868 mipi_dsi_host_unregister(host);
1869 host->dev = NULL;
1870 host->ops = NULL;
1871 msm_host->registered = false;
1872 }
1873}
1874
1875int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
1876 const struct mipi_dsi_msg *msg)
1877{
1878 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1879
1880 /* TODO: make sure dsi_cmd_mdp is idle.
1881 * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
1882 * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
1883 * How to handle the old versions? Wait for mdp cmd done?
1884 */
1885
1886 /*
1887 * mdss interrupt is generated in mdp core clock domain
1888 * mdp clock need to be enabled to receive dsi interrupt
1889 */
1890 dsi_clk_ctrl(msm_host, 1);
1891
1892 /* TODO: vote for bus bandwidth */
1893
1894 if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
1895 dsi_set_tx_power_mode(0, msm_host);
1896
1897 msm_host->dma_cmd_ctrl_restore = dsi_read(msm_host, REG_DSI_CTRL);
1898 dsi_write(msm_host, REG_DSI_CTRL,
1899 msm_host->dma_cmd_ctrl_restore |
1900 DSI_CTRL_CMD_MODE_EN |
1901 DSI_CTRL_ENABLE);
1902 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 1);
1903
1904 return 0;
1905}
1906
1907void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
1908 const struct mipi_dsi_msg *msg)
1909{
1910 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1911
1912 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 0);
1913 dsi_write(msm_host, REG_DSI_CTRL, msm_host->dma_cmd_ctrl_restore);
1914
1915 if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
1916 dsi_set_tx_power_mode(1, msm_host);
1917
1918 /* TODO: unvote for bus bandwidth */
1919
1920 dsi_clk_ctrl(msm_host, 0);
1921}
1922
1923int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
1924 const struct mipi_dsi_msg *msg)
1925{
1926 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1927
1928 return dsi_cmds2buf_tx(msm_host, msg);
1929}
1930
1931int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
1932 const struct mipi_dsi_msg *msg)
1933{
1934 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lid248b612015-08-13 17:49:29 -04001935 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -04001936 int data_byte, rx_byte, dlen, end;
1937 int short_response, diff, pkt_size, ret = 0;
1938 char cmd;
1939 int rlen = msg->rx_len;
1940 u8 *buf;
1941
1942 if (rlen <= 2) {
1943 short_response = 1;
1944 pkt_size = rlen;
1945 rx_byte = 4;
1946 } else {
1947 short_response = 0;
1948 data_byte = 10; /* first read */
1949 if (rlen < data_byte)
1950 pkt_size = rlen;
1951 else
1952 pkt_size = data_byte;
1953 rx_byte = data_byte + 6; /* 4 header + 2 crc */
1954 }
1955
1956 buf = msm_host->rx_buf;
1957 end = 0;
1958 while (!end) {
1959 u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
1960 struct mipi_dsi_msg max_pkt_size_msg = {
1961 .channel = msg->channel,
1962 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
1963 .tx_len = 2,
1964 .tx_buf = tx,
1965 };
1966
1967 DBG("rlen=%d pkt_size=%d rx_byte=%d",
1968 rlen, pkt_size, rx_byte);
1969
1970 ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
1971 if (ret < 2) {
1972 pr_err("%s: Set max pkt size failed, %d\n",
1973 __func__, ret);
1974 return -EINVAL;
1975 }
1976
Hai Lid248b612015-08-13 17:49:29 -04001977 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
1978 (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
Hai Lia6895542015-03-31 14:36:33 -04001979 /* Clear the RDBK_DATA registers */
1980 dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
1981 DSI_RDBK_DATA_CTRL_CLR);
1982 wmb(); /* make sure the RDBK registers are cleared */
1983 dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
1984 wmb(); /* release cleared status before transfer */
1985 }
1986
1987 ret = dsi_cmds2buf_tx(msm_host, msg);
1988 if (ret < msg->tx_len) {
1989 pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
1990 return ret;
1991 }
1992
1993 /*
1994 * once cmd_dma_done interrupt received,
1995 * return data from client is ready and stored
1996 * at RDBK_DATA register already
1997 * since rx fifo is 16 bytes, dcs header is kept at first loop,
1998 * after that dcs header lost during shift into registers
1999 */
2000 dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
2001
2002 if (dlen <= 0)
2003 return 0;
2004
2005 if (short_response)
2006 break;
2007
2008 if (rlen <= data_byte) {
2009 diff = data_byte - rlen;
2010 end = 1;
2011 } else {
2012 diff = 0;
2013 rlen -= data_byte;
2014 }
2015
2016 if (!end) {
2017 dlen -= 2; /* 2 crc */
2018 dlen -= diff;
2019 buf += dlen; /* next start position */
2020 data_byte = 14; /* NOT first read */
2021 if (rlen < data_byte)
2022 pkt_size += rlen;
2023 else
2024 pkt_size += data_byte;
2025 DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
2026 }
2027 }
2028
2029 /*
2030 * For single Long read, if the requested rlen < 10,
2031 * we need to shift the start position of rx
2032 * data buffer to skip the bytes which are not
2033 * updated.
2034 */
2035 if (pkt_size < 10 && !short_response)
2036 buf = msm_host->rx_buf + (10 - rlen);
2037 else
2038 buf = msm_host->rx_buf;
2039
2040 cmd = buf[0];
2041 switch (cmd) {
2042 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
2043 pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
2044 ret = 0;
Hai Li651ad3f2015-04-29 11:38:59 -04002045 break;
Hai Lia6895542015-03-31 14:36:33 -04002046 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
2047 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
2048 ret = dsi_short_read1_resp(buf, msg);
2049 break;
2050 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
2051 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
2052 ret = dsi_short_read2_resp(buf, msg);
2053 break;
2054 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
2055 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
2056 ret = dsi_long_read_resp(buf, msg);
2057 break;
2058 default:
2059 pr_warn("%s:Invalid response cmd\n", __func__);
2060 ret = 0;
2061 }
2062
2063 return ret;
2064}
2065
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05302066void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 dma_base,
2067 u32 len)
Hai Lia6895542015-03-31 14:36:33 -04002068{
2069 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2070
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05302071 dsi_write(msm_host, REG_DSI_DMA_BASE, dma_base);
Hai Lia6895542015-03-31 14:36:33 -04002072 dsi_write(msm_host, REG_DSI_DMA_LEN, len);
2073 dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
2074
2075 /* Make sure trigger happens */
2076 wmb();
2077}
2078
Hai Li9d32c4982015-05-15 13:04:05 -04002079int msm_dsi_host_set_src_pll(struct mipi_dsi_host *host,
2080 struct msm_dsi_pll *src_pll)
2081{
2082 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Archit Taneja4bfa9742015-10-09 16:32:38 +05302083 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Li9d32c4982015-05-15 13:04:05 -04002084 struct clk *byte_clk_provider, *pixel_clk_provider;
2085 int ret;
2086
2087 ret = msm_dsi_pll_get_clk_provider(src_pll,
2088 &byte_clk_provider, &pixel_clk_provider);
2089 if (ret) {
2090 pr_info("%s: can't get provider from pll, don't set parent\n",
2091 __func__);
2092 return 0;
2093 }
2094
2095 ret = clk_set_parent(msm_host->byte_clk_src, byte_clk_provider);
2096 if (ret) {
2097 pr_err("%s: can't set parent to byte_clk_src. ret=%d\n",
2098 __func__, ret);
2099 goto exit;
2100 }
2101
2102 ret = clk_set_parent(msm_host->pixel_clk_src, pixel_clk_provider);
2103 if (ret) {
2104 pr_err("%s: can't set parent to pixel_clk_src. ret=%d\n",
2105 __func__, ret);
2106 goto exit;
2107 }
2108
Archit Taneja4bfa9742015-10-09 16:32:38 +05302109 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
2110 ret = clk_set_parent(msm_host->dsi_clk_src, pixel_clk_provider);
2111 if (ret) {
2112 pr_err("%s: can't set parent to dsi_clk_src. ret=%d\n",
2113 __func__, ret);
2114 goto exit;
2115 }
2116
2117 ret = clk_set_parent(msm_host->esc_clk_src, byte_clk_provider);
2118 if (ret) {
2119 pr_err("%s: can't set parent to esc_clk_src. ret=%d\n",
2120 __func__, ret);
2121 goto exit;
2122 }
2123 }
2124
Hai Li9d32c4982015-05-15 13:04:05 -04002125exit:
2126 return ret;
2127}
2128
Hai Lia6895542015-03-31 14:36:33 -04002129int msm_dsi_host_enable(struct mipi_dsi_host *host)
2130{
2131 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2132
2133 dsi_op_mode_config(msm_host,
2134 !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
2135
2136 /* TODO: clock should be turned off for command mode,
2137 * and only turned on before MDP START.
2138 * This part of code should be enabled once mdp driver support it.
2139 */
2140 /* if (msm_panel->mode == MSM_DSI_CMD_MODE)
2141 dsi_clk_ctrl(msm_host, 0); */
2142
2143 return 0;
2144}
2145
2146int msm_dsi_host_disable(struct mipi_dsi_host *host)
2147{
2148 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2149
2150 dsi_op_mode_config(msm_host,
2151 !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
2152
2153 /* Since we have disabled INTF, the video engine won't stop so that
2154 * the cmd engine will be blocked.
2155 * Reset to disable video engine so that we can send off cmd.
2156 */
2157 dsi_sw_reset(msm_host);
2158
2159 return 0;
2160}
2161
Archit Taneja0c7df472015-10-14 15:31:13 +05302162static void msm_dsi_sfpb_config(struct msm_dsi_host *msm_host, bool enable)
2163{
2164 enum sfpb_ahb_arb_master_port_en en;
2165
2166 if (!msm_host->sfpb)
2167 return;
2168
2169 en = enable ? SFPB_MASTER_PORT_ENABLE : SFPB_MASTER_PORT_DISABLE;
2170
2171 regmap_update_bits(msm_host->sfpb, REG_SFPB_GPREG,
2172 SFPB_GPREG_MASTER_PORT_EN__MASK,
2173 SFPB_GPREG_MASTER_PORT_EN(en));
2174}
2175
Hai Lia6895542015-03-31 14:36:33 -04002176int msm_dsi_host_power_on(struct mipi_dsi_host *host)
2177{
2178 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lidceac342016-09-15 14:34:49 +05302179 struct msm_dsi_phy_shared_timings phy_shared_timings;
Hai Lia6895542015-03-31 14:36:33 -04002180 int ret = 0;
2181
2182 mutex_lock(&msm_host->dev_mutex);
2183 if (msm_host->power_on) {
2184 DBG("dsi host already on");
2185 goto unlock_ret;
2186 }
2187
Archit Taneja0c7df472015-10-14 15:31:13 +05302188 msm_dsi_sfpb_config(msm_host, true);
2189
Hai Lia6895542015-03-31 14:36:33 -04002190 ret = dsi_calc_clk_rate(msm_host);
2191 if (ret) {
2192 pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
2193 goto unlock_ret;
2194 }
2195
2196 ret = dsi_host_regulator_enable(msm_host);
2197 if (ret) {
2198 pr_err("%s:Failed to enable vregs.ret=%d\n",
2199 __func__, ret);
2200 goto unlock_ret;
2201 }
2202
2203 ret = dsi_bus_clk_enable(msm_host);
2204 if (ret) {
2205 pr_err("%s: failed to enable bus clocks, %d\n", __func__, ret);
2206 goto fail_disable_reg;
2207 }
2208
2209 dsi_phy_sw_reset(msm_host);
2210 ret = msm_dsi_manager_phy_enable(msm_host->id,
2211 msm_host->byte_clk_rate * 8,
Archit Taneja4bfa9742015-10-09 16:32:38 +05302212 msm_host->esc_clk_rate,
Hai Lidceac342016-09-15 14:34:49 +05302213 &phy_shared_timings);
Hai Lia6895542015-03-31 14:36:33 -04002214 dsi_bus_clk_disable(msm_host);
2215 if (ret) {
2216 pr_err("%s: failed to enable phy, %d\n", __func__, ret);
2217 goto fail_disable_reg;
2218 }
2219
2220 ret = dsi_clk_ctrl(msm_host, 1);
2221 if (ret) {
2222 pr_err("%s: failed to enable clocks. ret=%d\n", __func__, ret);
2223 goto fail_disable_reg;
2224 }
2225
Hai Liab8909b2015-06-11 10:56:46 -04002226 ret = pinctrl_pm_select_default_state(&msm_host->pdev->dev);
2227 if (ret) {
2228 pr_err("%s: failed to set pinctrl default state, %d\n",
2229 __func__, ret);
2230 goto fail_disable_clk;
2231 }
2232
Hai Lia6895542015-03-31 14:36:33 -04002233 dsi_timing_setup(msm_host);
2234 dsi_sw_reset(msm_host);
Hai Lidceac342016-09-15 14:34:49 +05302235 dsi_ctrl_config(msm_host, true, &phy_shared_timings);
Hai Lia6895542015-03-31 14:36:33 -04002236
2237 if (msm_host->disp_en_gpio)
2238 gpiod_set_value(msm_host->disp_en_gpio, 1);
2239
2240 msm_host->power_on = true;
2241 mutex_unlock(&msm_host->dev_mutex);
2242
2243 return 0;
2244
Hai Liab8909b2015-06-11 10:56:46 -04002245fail_disable_clk:
2246 dsi_clk_ctrl(msm_host, 0);
Hai Lia6895542015-03-31 14:36:33 -04002247fail_disable_reg:
2248 dsi_host_regulator_disable(msm_host);
2249unlock_ret:
2250 mutex_unlock(&msm_host->dev_mutex);
2251 return ret;
2252}
2253
2254int msm_dsi_host_power_off(struct mipi_dsi_host *host)
2255{
2256 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2257
2258 mutex_lock(&msm_host->dev_mutex);
2259 if (!msm_host->power_on) {
2260 DBG("dsi host already off");
2261 goto unlock_ret;
2262 }
2263
Hai Lidceac342016-09-15 14:34:49 +05302264 dsi_ctrl_config(msm_host, false, NULL);
Hai Lia6895542015-03-31 14:36:33 -04002265
2266 if (msm_host->disp_en_gpio)
2267 gpiod_set_value(msm_host->disp_en_gpio, 0);
2268
Hai Liab8909b2015-06-11 10:56:46 -04002269 pinctrl_pm_select_sleep_state(&msm_host->pdev->dev);
2270
Hai Lia6895542015-03-31 14:36:33 -04002271 msm_dsi_manager_phy_disable(msm_host->id);
2272
2273 dsi_clk_ctrl(msm_host, 0);
2274
2275 dsi_host_regulator_disable(msm_host);
2276
Archit Taneja0c7df472015-10-14 15:31:13 +05302277 msm_dsi_sfpb_config(msm_host, false);
2278
Hai Lia6895542015-03-31 14:36:33 -04002279 DBG("-");
2280
2281 msm_host->power_on = false;
2282
2283unlock_ret:
2284 mutex_unlock(&msm_host->dev_mutex);
2285 return 0;
2286}
2287
2288int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
2289 struct drm_display_mode *mode)
2290{
2291 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2292
2293 if (msm_host->mode) {
2294 drm_mode_destroy(msm_host->dev, msm_host->mode);
2295 msm_host->mode = NULL;
2296 }
2297
2298 msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
Wei Yongjun2abe1f22016-06-18 17:26:37 +00002299 if (!msm_host->mode) {
Hai Lia6895542015-03-31 14:36:33 -04002300 pr_err("%s: cannot duplicate mode\n", __func__);
Wei Yongjun2abe1f22016-06-18 17:26:37 +00002301 return -ENOMEM;
Hai Lia6895542015-03-31 14:36:33 -04002302 }
2303
2304 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}