blob: 7611fe0140362668e37b6c6bb6951e610429bfa6 [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;
Hai Lia6895542015-03-31 14:36:33 -0400138 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
Archit Taneja29a11572018-01-17 15:04:42 +0530217 ahb_clk = msm_clk_get(msm_host->pdev, "iface");
Archit Taneja31c92762015-10-09 12:40:39 +0530218 if (IS_ERR(ahb_clk)) {
219 pr_err("%s: cannot get interface clock\n", __func__);
220 goto put_gdsc;
221 }
222
Archit Tanejaf6be1122017-07-28 16:17:03 +0530223 pm_runtime_get_sync(dev);
224
Hai Lia6895542015-03-31 14:36:33 -0400225 ret = regulator_enable(gdsc_reg);
226 if (ret) {
227 pr_err("%s: unable to enable gdsc\n", __func__);
Archit Taneja29a11572018-01-17 15:04:42 +0530228 goto put_gdsc;
Hai Lia6895542015-03-31 14:36:33 -0400229 }
Archit Taneja31c92762015-10-09 12:40:39 +0530230
231 ret = clk_prepare_enable(ahb_clk);
Hai Lia6895542015-03-31 14:36:33 -0400232 if (ret) {
233 pr_err("%s: unable to enable ahb_clk\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400234 goto disable_gdsc;
Hai Lia6895542015-03-31 14:36:33 -0400235 }
236
237 ret = dsi_get_version(msm_host->ctrl_base, &major, &minor);
Hai Lia6895542015-03-31 14:36:33 -0400238 if (ret) {
239 pr_err("%s: Invalid version\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400240 goto disable_clks;
Hai Lia6895542015-03-31 14:36:33 -0400241 }
242
Hai Lid248b612015-08-13 17:49:29 -0400243 cfg_hnd = msm_dsi_cfg_get(major, minor);
Hai Lia6895542015-03-31 14:36:33 -0400244
Hai Lid248b612015-08-13 17:49:29 -0400245 DBG("%s: Version %x:%x\n", __func__, major, minor);
246
247disable_clks:
Archit Taneja31c92762015-10-09 12:40:39 +0530248 clk_disable_unprepare(ahb_clk);
Hai Lid248b612015-08-13 17:49:29 -0400249disable_gdsc:
250 regulator_disable(gdsc_reg);
Archit Tanejaa18a0ea2017-10-06 16:27:06 +0530251 pm_runtime_put_sync(dev);
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{
Rob Clarkdb9a3752017-10-16 13:35:57 -0400335 struct platform_device *pdev = msm_host->pdev;
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++) {
Rob Clarkdb9a3752017-10-16 13:35:57 -0400342 msm_host->bus_clks[i] = msm_clk_get(pdev,
Archit Taneja6e0eb522015-10-09 15:21:12 +0530343 cfg->bus_clk_names[i]);
344 if (IS_ERR(msm_host->bus_clks[i])) {
345 ret = PTR_ERR(msm_host->bus_clks[i]);
Rob Clarkdb9a3752017-10-16 13:35:57 -0400346 pr_err("%s: Unable to get %s clock, ret = %d\n",
Archit Taneja6e0eb522015-10-09 15:21:12 +0530347 __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 */
Rob Clarkdb9a3752017-10-16 13:35:57 -0400353 msm_host->byte_clk = msm_clk_get(pdev, "byte");
Hai Lia6895542015-03-31 14:36:33 -0400354 if (IS_ERR(msm_host->byte_clk)) {
355 ret = PTR_ERR(msm_host->byte_clk);
Rob Clarkdb9a3752017-10-16 13:35:57 -0400356 pr_err("%s: can't find dsi_byte clock. ret=%d\n",
Hai Lia6895542015-03-31 14:36:33 -0400357 __func__, ret);
358 msm_host->byte_clk = NULL;
359 goto exit;
360 }
361
Rob Clarkdb9a3752017-10-16 13:35:57 -0400362 msm_host->pixel_clk = msm_clk_get(pdev, "pixel");
Hai Lia6895542015-03-31 14:36:33 -0400363 if (IS_ERR(msm_host->pixel_clk)) {
364 ret = PTR_ERR(msm_host->pixel_clk);
Rob Clarkdb9a3752017-10-16 13:35:57 -0400365 pr_err("%s: can't find dsi_pixel clock. ret=%d\n",
Hai Lia6895542015-03-31 14:36:33 -0400366 __func__, ret);
367 msm_host->pixel_clk = NULL;
368 goto exit;
369 }
370
Rob Clarkdb9a3752017-10-16 13:35:57 -0400371 msm_host->esc_clk = msm_clk_get(pdev, "core");
Hai Lia6895542015-03-31 14:36:33 -0400372 if (IS_ERR(msm_host->esc_clk)) {
373 ret = PTR_ERR(msm_host->esc_clk);
Rob Clarkdb9a3752017-10-16 13:35:57 -0400374 pr_err("%s: can't find dsi_esc clock. ret=%d\n",
Hai Lia6895542015-03-31 14:36:33 -0400375 __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;
Rob Clarkdb9a3752017-10-16 13:35:57 -0400383 pr_err("%s: can't find byte_clk clock. 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;
Rob Clarkdb9a3752017-10-16 13:35:57 -0400390 pr_err("%s: can't find pixel_clk clock. 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) {
Rob Clarkdb9a3752017-10-16 13:35:57 -0400395 msm_host->src_clk = msm_clk_get(pdev, "src");
Archit Taneja4bfa9742015-10-09 16:32:38 +0530396 if (IS_ERR(msm_host->src_clk)) {
397 ret = PTR_ERR(msm_host->src_clk);
Rob Clarkdb9a3752017-10-16 13:35:57 -0400398 pr_err("%s: can't find src clock. ret=%d\n",
Archit Taneja4bfa9742015-10-09 16:32:38 +0530399 __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;
Rob Clarkdb9a3752017-10-16 13:35:57 -0400407 pr_err("%s: can't get esc clock parent. ret=%d\n",
Archit Taneja4bfa9742015-10-09 16:32:38 +0530408 __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;
Rob Clarkdb9a3752017-10-16 13:35:57 -0400415 pr_err("%s: can't get src clock parent. ret=%d\n",
Archit Taneja4bfa9742015-10-09 16:32:38 +0530416 __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 Tanejaf54ca1a2017-07-28 16:17:04 +0530458int msm_dsi_runtime_suspend(struct device *dev)
459{
460 struct platform_device *pdev = to_platform_device(dev);
461 struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
462 struct mipi_dsi_host *host = msm_dsi->host;
463 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
464
465 if (!msm_host->cfg_hnd)
466 return 0;
467
468 dsi_bus_clk_disable(msm_host);
469
470 return 0;
471}
472
473int msm_dsi_runtime_resume(struct device *dev)
474{
475 struct platform_device *pdev = to_platform_device(dev);
476 struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
477 struct mipi_dsi_host *host = msm_dsi->host;
478 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
479
480 if (!msm_host->cfg_hnd)
481 return 0;
482
483 return dsi_bus_clk_enable(msm_host);
484}
485
Archit Taneja4bfa9742015-10-09 16:32:38 +0530486static int dsi_link_clk_enable_6g(struct msm_dsi_host *msm_host)
Hai Lia6895542015-03-31 14:36:33 -0400487{
488 int ret;
489
490 DBG("Set clk rates: pclk=%d, byteclk=%d",
491 msm_host->mode->clock, msm_host->byte_clk_rate);
492
493 ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
494 if (ret) {
495 pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
496 goto error;
497 }
498
499 ret = clk_set_rate(msm_host->pixel_clk, msm_host->mode->clock * 1000);
500 if (ret) {
501 pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
502 goto error;
503 }
504
505 ret = clk_prepare_enable(msm_host->esc_clk);
506 if (ret) {
507 pr_err("%s: Failed to enable dsi esc clk\n", __func__);
508 goto error;
509 }
510
511 ret = clk_prepare_enable(msm_host->byte_clk);
512 if (ret) {
513 pr_err("%s: Failed to enable dsi byte clk\n", __func__);
514 goto byte_clk_err;
515 }
516
517 ret = clk_prepare_enable(msm_host->pixel_clk);
518 if (ret) {
519 pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
520 goto pixel_clk_err;
521 }
522
523 return 0;
524
525pixel_clk_err:
526 clk_disable_unprepare(msm_host->byte_clk);
527byte_clk_err:
528 clk_disable_unprepare(msm_host->esc_clk);
529error:
530 return ret;
531}
532
Archit Taneja4bfa9742015-10-09 16:32:38 +0530533static int dsi_link_clk_enable_v2(struct msm_dsi_host *msm_host)
534{
535 int ret;
536
537 DBG("Set clk rates: pclk=%d, byteclk=%d, esc_clk=%d, dsi_src_clk=%d",
538 msm_host->mode->clock, msm_host->byte_clk_rate,
539 msm_host->esc_clk_rate, msm_host->src_clk_rate);
540
541 ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
542 if (ret) {
543 pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
544 goto error;
545 }
546
547 ret = clk_set_rate(msm_host->esc_clk, msm_host->esc_clk_rate);
548 if (ret) {
549 pr_err("%s: Failed to set rate esc clk, %d\n", __func__, ret);
550 goto error;
551 }
552
553 ret = clk_set_rate(msm_host->src_clk, msm_host->src_clk_rate);
554 if (ret) {
555 pr_err("%s: Failed to set rate src clk, %d\n", __func__, ret);
556 goto error;
557 }
558
559 ret = clk_set_rate(msm_host->pixel_clk, msm_host->mode->clock * 1000);
560 if (ret) {
561 pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
562 goto error;
563 }
564
565 ret = clk_prepare_enable(msm_host->byte_clk);
566 if (ret) {
567 pr_err("%s: Failed to enable dsi byte clk\n", __func__);
568 goto error;
569 }
570
571 ret = clk_prepare_enable(msm_host->esc_clk);
572 if (ret) {
573 pr_err("%s: Failed to enable dsi esc clk\n", __func__);
574 goto esc_clk_err;
575 }
576
577 ret = clk_prepare_enable(msm_host->src_clk);
578 if (ret) {
579 pr_err("%s: Failed to enable dsi src clk\n", __func__);
580 goto src_clk_err;
581 }
582
583 ret = clk_prepare_enable(msm_host->pixel_clk);
584 if (ret) {
585 pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
586 goto pixel_clk_err;
587 }
588
589 return 0;
590
591pixel_clk_err:
592 clk_disable_unprepare(msm_host->src_clk);
593src_clk_err:
594 clk_disable_unprepare(msm_host->esc_clk);
595esc_clk_err:
596 clk_disable_unprepare(msm_host->byte_clk);
597error:
598 return ret;
599}
600
601static int dsi_link_clk_enable(struct msm_dsi_host *msm_host)
602{
603 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
604
605 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G)
606 return dsi_link_clk_enable_6g(msm_host);
607 else
608 return dsi_link_clk_enable_v2(msm_host);
609}
610
Hai Lia6895542015-03-31 14:36:33 -0400611static void dsi_link_clk_disable(struct msm_dsi_host *msm_host)
612{
Archit Taneja4bfa9742015-10-09 16:32:38 +0530613 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
614
615 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
616 clk_disable_unprepare(msm_host->esc_clk);
617 clk_disable_unprepare(msm_host->pixel_clk);
618 clk_disable_unprepare(msm_host->byte_clk);
619 } else {
620 clk_disable_unprepare(msm_host->pixel_clk);
621 clk_disable_unprepare(msm_host->src_clk);
622 clk_disable_unprepare(msm_host->esc_clk);
623 clk_disable_unprepare(msm_host->byte_clk);
624 }
Hai Lia6895542015-03-31 14:36:33 -0400625}
626
Hai Lia6895542015-03-31 14:36:33 -0400627static 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
Hai Lia6895542015-03-31 14:36:33 -0400694static void dsi_intr_ctrl(struct msm_dsi_host *msm_host, u32 mask, int enable)
695{
696 u32 intr;
697 unsigned long flags;
698
699 spin_lock_irqsave(&msm_host->intr_lock, flags);
700 intr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
701
702 if (enable)
703 intr |= mask;
704 else
705 intr &= ~mask;
706
707 DBG("intr=%x enable=%d", intr, enable);
708
709 dsi_write(msm_host, REG_DSI_INTR_CTRL, intr);
710 spin_unlock_irqrestore(&msm_host->intr_lock, flags);
711}
712
713static inline enum dsi_traffic_mode dsi_get_traffic_mode(const u32 mode_flags)
714{
715 if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
716 return BURST_MODE;
717 else if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
718 return NON_BURST_SYNCH_PULSE;
719
720 return NON_BURST_SYNCH_EVENT;
721}
722
723static inline enum dsi_vid_dst_format dsi_get_vid_fmt(
724 const enum mipi_dsi_pixel_format mipi_fmt)
725{
726 switch (mipi_fmt) {
727 case MIPI_DSI_FMT_RGB888: return VID_DST_FORMAT_RGB888;
728 case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666_LOOSE;
729 case MIPI_DSI_FMT_RGB666_PACKED: return VID_DST_FORMAT_RGB666;
730 case MIPI_DSI_FMT_RGB565: return VID_DST_FORMAT_RGB565;
731 default: return VID_DST_FORMAT_RGB888;
732 }
733}
734
735static inline enum dsi_cmd_dst_format dsi_get_cmd_fmt(
736 const enum mipi_dsi_pixel_format mipi_fmt)
737{
738 switch (mipi_fmt) {
739 case MIPI_DSI_FMT_RGB888: return CMD_DST_FORMAT_RGB888;
740 case MIPI_DSI_FMT_RGB666_PACKED:
741 case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666;
742 case MIPI_DSI_FMT_RGB565: return CMD_DST_FORMAT_RGB565;
743 default: return CMD_DST_FORMAT_RGB888;
744 }
745}
746
747static void dsi_ctrl_config(struct msm_dsi_host *msm_host, bool enable,
Hai Lidceac342016-09-15 14:34:49 +0530748 struct msm_dsi_phy_shared_timings *phy_shared_timings)
Hai Lia6895542015-03-31 14:36:33 -0400749{
750 u32 flags = msm_host->mode_flags;
751 enum mipi_dsi_pixel_format mipi_fmt = msm_host->format;
Hai Lid248b612015-08-13 17:49:29 -0400752 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400753 u32 data = 0;
754
755 if (!enable) {
756 dsi_write(msm_host, REG_DSI_CTRL, 0);
757 return;
758 }
759
760 if (flags & MIPI_DSI_MODE_VIDEO) {
761 if (flags & MIPI_DSI_MODE_VIDEO_HSE)
762 data |= DSI_VID_CFG0_PULSE_MODE_HSA_HE;
763 if (flags & MIPI_DSI_MODE_VIDEO_HFP)
764 data |= DSI_VID_CFG0_HFP_POWER_STOP;
765 if (flags & MIPI_DSI_MODE_VIDEO_HBP)
766 data |= DSI_VID_CFG0_HBP_POWER_STOP;
767 if (flags & MIPI_DSI_MODE_VIDEO_HSA)
768 data |= DSI_VID_CFG0_HSA_POWER_STOP;
769 /* Always set low power stop mode for BLLP
770 * to let command engine send packets
771 */
772 data |= DSI_VID_CFG0_EOF_BLLP_POWER_STOP |
773 DSI_VID_CFG0_BLLP_POWER_STOP;
774 data |= DSI_VID_CFG0_TRAFFIC_MODE(dsi_get_traffic_mode(flags));
775 data |= DSI_VID_CFG0_DST_FORMAT(dsi_get_vid_fmt(mipi_fmt));
776 data |= DSI_VID_CFG0_VIRT_CHANNEL(msm_host->channel);
777 dsi_write(msm_host, REG_DSI_VID_CFG0, data);
778
779 /* Do not swap RGB colors */
780 data = DSI_VID_CFG1_RGB_SWAP(SWAP_RGB);
781 dsi_write(msm_host, REG_DSI_VID_CFG1, 0);
782 } else {
783 /* Do not swap RGB colors */
784 data = DSI_CMD_CFG0_RGB_SWAP(SWAP_RGB);
785 data |= DSI_CMD_CFG0_DST_FORMAT(dsi_get_cmd_fmt(mipi_fmt));
786 dsi_write(msm_host, REG_DSI_CMD_CFG0, data);
787
788 data = DSI_CMD_CFG1_WR_MEM_START(MIPI_DCS_WRITE_MEMORY_START) |
789 DSI_CMD_CFG1_WR_MEM_CONTINUE(
790 MIPI_DCS_WRITE_MEMORY_CONTINUE);
791 /* Always insert DCS command */
792 data |= DSI_CMD_CFG1_INSERT_DCS_COMMAND;
793 dsi_write(msm_host, REG_DSI_CMD_CFG1, data);
794 }
795
796 dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL,
797 DSI_CMD_DMA_CTRL_FROM_FRAME_BUFFER |
798 DSI_CMD_DMA_CTRL_LOW_POWER);
799
800 data = 0;
801 /* Always assume dedicated TE pin */
802 data |= DSI_TRIG_CTRL_TE;
803 data |= DSI_TRIG_CTRL_MDP_TRIGGER(TRIGGER_NONE);
804 data |= DSI_TRIG_CTRL_DMA_TRIGGER(TRIGGER_SW);
805 data |= DSI_TRIG_CTRL_STREAM(msm_host->channel);
Hai Lid248b612015-08-13 17:49:29 -0400806 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
807 (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_2))
Hai Lia6895542015-03-31 14:36:33 -0400808 data |= DSI_TRIG_CTRL_BLOCK_DMA_WITHIN_FRAME;
809 dsi_write(msm_host, REG_DSI_TRIG_CTRL, data);
810
Hai Lidceac342016-09-15 14:34:49 +0530811 data = DSI_CLKOUT_TIMING_CTRL_T_CLK_POST(phy_shared_timings->clk_post) |
812 DSI_CLKOUT_TIMING_CTRL_T_CLK_PRE(phy_shared_timings->clk_pre);
Hai Lia6895542015-03-31 14:36:33 -0400813 dsi_write(msm_host, REG_DSI_CLKOUT_TIMING_CTRL, data);
814
Hai Lidceac342016-09-15 14:34:49 +0530815 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
816 (cfg_hnd->minor > MSM_DSI_6G_VER_MINOR_V1_0) &&
817 phy_shared_timings->clk_pre_inc_by_2)
818 dsi_write(msm_host, REG_DSI_T_CLK_PRE_EXTEND,
819 DSI_T_CLK_PRE_EXTEND_INC_BY_2_BYTECLK);
820
Hai Lia6895542015-03-31 14:36:33 -0400821 data = 0;
822 if (!(flags & MIPI_DSI_MODE_EOT_PACKET))
823 data |= DSI_EOT_PACKET_CTRL_TX_EOT_APPEND;
824 dsi_write(msm_host, REG_DSI_EOT_PACKET_CTRL, data);
825
826 /* allow only ack-err-status to generate interrupt */
827 dsi_write(msm_host, REG_DSI_ERR_INT_MASK0, 0x13ff3fe0);
828
829 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
830
831 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
832
833 data = DSI_CTRL_CLK_EN;
834
835 DBG("lane number=%d", msm_host->lanes);
Archit Taneja26f7d1f2016-02-25 11:19:48 +0530836 data |= ((DSI_CTRL_LANE0 << msm_host->lanes) - DSI_CTRL_LANE0);
837
838 dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
839 DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(msm_host->dlane_swap));
Archit Taneja65c5e542015-04-08 11:37:40 +0530840
841 if (!(flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
842 dsi_write(msm_host, REG_DSI_LANE_CTRL,
843 DSI_LANE_CTRL_CLKLN_HS_FORCE_REQUEST);
844
Hai Lia6895542015-03-31 14:36:33 -0400845 data |= DSI_CTRL_ENABLE;
846
847 dsi_write(msm_host, REG_DSI_CTRL, data);
848}
849
850static void dsi_timing_setup(struct msm_dsi_host *msm_host)
851{
852 struct drm_display_mode *mode = msm_host->mode;
853 u32 hs_start = 0, vs_start = 0; /* take sync start as 0 */
854 u32 h_total = mode->htotal;
855 u32 v_total = mode->vtotal;
856 u32 hs_end = mode->hsync_end - mode->hsync_start;
857 u32 vs_end = mode->vsync_end - mode->vsync_start;
858 u32 ha_start = h_total - mode->hsync_start;
859 u32 ha_end = ha_start + mode->hdisplay;
860 u32 va_start = v_total - mode->vsync_start;
861 u32 va_end = va_start + mode->vdisplay;
862 u32 wc;
863
864 DBG("");
865
866 if (msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) {
867 dsi_write(msm_host, REG_DSI_ACTIVE_H,
868 DSI_ACTIVE_H_START(ha_start) |
869 DSI_ACTIVE_H_END(ha_end));
870 dsi_write(msm_host, REG_DSI_ACTIVE_V,
871 DSI_ACTIVE_V_START(va_start) |
872 DSI_ACTIVE_V_END(va_end));
873 dsi_write(msm_host, REG_DSI_TOTAL,
874 DSI_TOTAL_H_TOTAL(h_total - 1) |
875 DSI_TOTAL_V_TOTAL(v_total - 1));
876
877 dsi_write(msm_host, REG_DSI_ACTIVE_HSYNC,
878 DSI_ACTIVE_HSYNC_START(hs_start) |
879 DSI_ACTIVE_HSYNC_END(hs_end));
880 dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_HPOS, 0);
881 dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_VPOS,
882 DSI_ACTIVE_VSYNC_VPOS_START(vs_start) |
883 DSI_ACTIVE_VSYNC_VPOS_END(vs_end));
884 } else { /* command mode */
885 /* image data and 1 byte write_memory_start cmd */
886 wc = mode->hdisplay * dsi_get_bpp(msm_host->format) / 8 + 1;
887
888 dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_CTRL,
889 DSI_CMD_MDP_STREAM_CTRL_WORD_COUNT(wc) |
890 DSI_CMD_MDP_STREAM_CTRL_VIRTUAL_CHANNEL(
891 msm_host->channel) |
892 DSI_CMD_MDP_STREAM_CTRL_DATA_TYPE(
893 MIPI_DSI_DCS_LONG_WRITE));
894
895 dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_TOTAL,
896 DSI_CMD_MDP_STREAM_TOTAL_H_TOTAL(mode->hdisplay) |
897 DSI_CMD_MDP_STREAM_TOTAL_V_TOTAL(mode->vdisplay));
898 }
899}
900
901static void dsi_sw_reset(struct msm_dsi_host *msm_host)
902{
903 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
904 wmb(); /* clocks need to be enabled before reset */
905
906 dsi_write(msm_host, REG_DSI_RESET, 1);
907 wmb(); /* make sure reset happen */
908 dsi_write(msm_host, REG_DSI_RESET, 0);
909}
910
911static void dsi_op_mode_config(struct msm_dsi_host *msm_host,
912 bool video_mode, bool enable)
913{
914 u32 dsi_ctrl;
915
916 dsi_ctrl = dsi_read(msm_host, REG_DSI_CTRL);
917
918 if (!enable) {
919 dsi_ctrl &= ~(DSI_CTRL_ENABLE | DSI_CTRL_VID_MODE_EN |
920 DSI_CTRL_CMD_MODE_EN);
921 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE |
922 DSI_IRQ_MASK_VIDEO_DONE, 0);
923 } else {
924 if (video_mode) {
925 dsi_ctrl |= DSI_CTRL_VID_MODE_EN;
926 } else { /* command mode */
927 dsi_ctrl |= DSI_CTRL_CMD_MODE_EN;
928 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE, 1);
929 }
930 dsi_ctrl |= DSI_CTRL_ENABLE;
931 }
932
933 dsi_write(msm_host, REG_DSI_CTRL, dsi_ctrl);
934}
935
936static void dsi_set_tx_power_mode(int mode, struct msm_dsi_host *msm_host)
937{
938 u32 data;
939
940 data = dsi_read(msm_host, REG_DSI_CMD_DMA_CTRL);
941
942 if (mode == 0)
943 data &= ~DSI_CMD_DMA_CTRL_LOW_POWER;
944 else
945 data |= DSI_CMD_DMA_CTRL_LOW_POWER;
946
947 dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL, data);
948}
949
950static void dsi_wait4video_done(struct msm_dsi_host *msm_host)
951{
952 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 1);
953
954 reinit_completion(&msm_host->video_comp);
955
956 wait_for_completion_timeout(&msm_host->video_comp,
957 msecs_to_jiffies(70));
958
959 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 0);
960}
961
962static void dsi_wait4video_eng_busy(struct msm_dsi_host *msm_host)
963{
964 if (!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO))
965 return;
966
967 if (msm_host->power_on) {
968 dsi_wait4video_done(msm_host);
969 /* delay 4 ms to skip BLLP */
970 usleep_range(2000, 4000);
971 }
972}
973
974/* dsi_cmd */
975static int dsi_tx_buf_alloc(struct msm_dsi_host *msm_host, int size)
976{
977 struct drm_device *dev = msm_host->dev;
Rob Clarkf59f62d2017-06-13 10:22:37 -0400978 struct msm_drm_private *priv = dev->dev_private;
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530979 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400980 int ret;
Rob Clark78babc12016-11-11 12:06:46 -0500981 uint64_t iova;
Hai Lia6895542015-03-31 14:36:33 -0400982
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530983 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530984 msm_host->tx_gem_obj = msm_gem_new(dev, size, MSM_BO_UNCACHED);
985 if (IS_ERR(msm_host->tx_gem_obj)) {
986 ret = PTR_ERR(msm_host->tx_gem_obj);
987 pr_err("%s: failed to allocate gem, %d\n",
988 __func__, ret);
989 msm_host->tx_gem_obj = NULL;
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530990 return ret;
991 }
992
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600993 ret = msm_gem_get_iova(msm_host->tx_gem_obj,
Rob Clark8bdcd942017-06-13 11:07:08 -0400994 priv->kms->aspace, &iova);
saurabhbeb107f2015-12-07 01:19:21 +0530995 mutex_unlock(&dev->struct_mutex);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530996 if (ret) {
997 pr_err("%s: failed to get iova, %d\n", __func__, ret);
998 return ret;
999 }
Hai Lia6895542015-03-31 14:36:33 -04001000
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301001 if (iova & 0x07) {
1002 pr_err("%s: buf NOT 8 bytes aligned\n", __func__);
1003 return -EINVAL;
1004 }
Hai Lia6895542015-03-31 14:36:33 -04001005
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301006 msm_host->tx_size = msm_host->tx_gem_obj->size;
1007 } else {
1008 msm_host->tx_buf = dma_alloc_coherent(dev->dev, size,
1009 &msm_host->tx_buf_paddr, GFP_KERNEL);
1010 if (!msm_host->tx_buf) {
1011 ret = -ENOMEM;
1012 pr_err("%s: failed to allocate tx buf, %d\n",
1013 __func__, ret);
1014 return ret;
1015 }
1016
1017 msm_host->tx_size = size;
Hai Lia6895542015-03-31 14:36:33 -04001018 }
1019
1020 return 0;
1021}
1022
1023static void dsi_tx_buf_free(struct msm_dsi_host *msm_host)
1024{
1025 struct drm_device *dev = msm_host->dev;
1026
1027 if (msm_host->tx_gem_obj) {
1028 msm_gem_put_iova(msm_host->tx_gem_obj, 0);
1029 mutex_lock(&dev->struct_mutex);
1030 msm_gem_free_object(msm_host->tx_gem_obj);
1031 msm_host->tx_gem_obj = NULL;
1032 mutex_unlock(&dev->struct_mutex);
1033 }
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301034
1035 if (msm_host->tx_buf)
1036 dma_free_coherent(dev->dev, msm_host->tx_size, msm_host->tx_buf,
1037 msm_host->tx_buf_paddr);
Hai Lia6895542015-03-31 14:36:33 -04001038}
1039
1040/*
1041 * prepare cmd buffer to be txed
1042 */
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301043static int dsi_cmd_dma_add(struct msm_dsi_host *msm_host,
1044 const struct mipi_dsi_msg *msg)
Hai Lia6895542015-03-31 14:36:33 -04001045{
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301046 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -04001047 struct mipi_dsi_packet packet;
1048 int len;
1049 int ret;
1050 u8 *data;
1051
1052 ret = mipi_dsi_create_packet(&packet, msg);
1053 if (ret) {
1054 pr_err("%s: create packet failed, %d\n", __func__, ret);
1055 return ret;
1056 }
1057 len = (packet.size + 3) & (~0x3);
1058
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301059 if (len > msm_host->tx_size) {
Hai Lia6895542015-03-31 14:36:33 -04001060 pr_err("%s: packet size is too big\n", __func__);
1061 return -EINVAL;
1062 }
1063
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301064 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
Rob Clark18f23042016-05-26 16:24:35 -04001065 data = msm_gem_get_vaddr(msm_host->tx_gem_obj);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301066 if (IS_ERR(data)) {
1067 ret = PTR_ERR(data);
1068 pr_err("%s: get vaddr failed, %d\n", __func__, ret);
1069 return ret;
1070 }
1071 } else {
1072 data = msm_host->tx_buf;
Hai Lia6895542015-03-31 14:36:33 -04001073 }
1074
1075 /* MSM specific command format in memory */
1076 data[0] = packet.header[1];
1077 data[1] = packet.header[2];
1078 data[2] = packet.header[0];
1079 data[3] = BIT(7); /* Last packet */
1080 if (mipi_dsi_packet_format_is_long(msg->type))
1081 data[3] |= BIT(6);
1082 if (msg->rx_buf && msg->rx_len)
1083 data[3] |= BIT(5);
1084
1085 /* Long packet */
1086 if (packet.payload && packet.payload_length)
1087 memcpy(data + 4, packet.payload, packet.payload_length);
1088
1089 /* Append 0xff to the end */
1090 if (packet.size < len)
1091 memset(data + packet.size, 0xff, len - packet.size);
1092
Rob Clark18f23042016-05-26 16:24:35 -04001093 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G)
1094 msm_gem_put_vaddr(msm_host->tx_gem_obj);
1095
Hai Lia6895542015-03-31 14:36:33 -04001096 return len;
1097}
1098
1099/*
1100 * dsi_short_read1_resp: 1 parameter
1101 */
1102static int dsi_short_read1_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1103{
1104 u8 *data = msg->rx_buf;
1105 if (data && (msg->rx_len >= 1)) {
1106 *data = buf[1]; /* strip out dcs type */
1107 return 1;
1108 } else {
Stephane Viau981371f2015-04-30 10:39:26 -04001109 pr_err("%s: read data does not match with rx_buf len %zu\n",
Hai Lia6895542015-03-31 14:36:33 -04001110 __func__, msg->rx_len);
1111 return -EINVAL;
1112 }
1113}
1114
1115/*
1116 * dsi_short_read2_resp: 2 parameter
1117 */
1118static int dsi_short_read2_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1119{
1120 u8 *data = msg->rx_buf;
1121 if (data && (msg->rx_len >= 2)) {
1122 data[0] = buf[1]; /* strip out dcs type */
1123 data[1] = buf[2];
1124 return 2;
1125 } else {
Stephane Viau981371f2015-04-30 10:39:26 -04001126 pr_err("%s: read data does not match with rx_buf len %zu\n",
Hai Lia6895542015-03-31 14:36:33 -04001127 __func__, msg->rx_len);
1128 return -EINVAL;
1129 }
1130}
1131
1132static int dsi_long_read_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1133{
1134 /* strip out 4 byte dcs header */
1135 if (msg->rx_buf && msg->rx_len)
1136 memcpy(msg->rx_buf, buf + 4, msg->rx_len);
1137
1138 return msg->rx_len;
1139}
1140
Hai Lia6895542015-03-31 14:36:33 -04001141static int dsi_cmd_dma_tx(struct msm_dsi_host *msm_host, int len)
1142{
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301143 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Rob Clarkf59f62d2017-06-13 10:22:37 -04001144 struct drm_device *dev = msm_host->dev;
1145 struct msm_drm_private *priv = dev->dev_private;
Hai Lia6895542015-03-31 14:36:33 -04001146 int ret;
Rob Clark78babc12016-11-11 12:06:46 -05001147 uint64_t dma_base;
Hai Lia6895542015-03-31 14:36:33 -04001148 bool triggered;
1149
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301150 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
Rob Clarkf59f62d2017-06-13 10:22:37 -04001151 ret = msm_gem_get_iova(msm_host->tx_gem_obj,
Rob Clark8bdcd942017-06-13 11:07:08 -04001152 priv->kms->aspace, &dma_base);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301153 if (ret) {
1154 pr_err("%s: failed to get iova: %d\n", __func__, ret);
1155 return ret;
1156 }
1157 } else {
1158 dma_base = msm_host->tx_buf_paddr;
Hai Lia6895542015-03-31 14:36:33 -04001159 }
1160
1161 reinit_completion(&msm_host->dma_comp);
1162
1163 dsi_wait4video_eng_busy(msm_host);
1164
1165 triggered = msm_dsi_manager_cmd_xfer_trigger(
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301166 msm_host->id, dma_base, len);
Hai Lia6895542015-03-31 14:36:33 -04001167 if (triggered) {
1168 ret = wait_for_completion_timeout(&msm_host->dma_comp,
1169 msecs_to_jiffies(200));
1170 DBG("ret=%d", ret);
1171 if (ret == 0)
1172 ret = -ETIMEDOUT;
1173 else
1174 ret = len;
1175 } else
1176 ret = len;
1177
1178 return ret;
1179}
1180
1181static int dsi_cmd_dma_rx(struct msm_dsi_host *msm_host,
1182 u8 *buf, int rx_byte, int pkt_size)
1183{
1184 u32 *lp, *temp, data;
1185 int i, j = 0, cnt;
Hai Lia6895542015-03-31 14:36:33 -04001186 u32 read_cnt;
1187 u8 reg[16];
1188 int repeated_bytes = 0;
1189 int buf_offset = buf - msm_host->rx_buf;
1190
1191 lp = (u32 *)buf;
1192 temp = (u32 *)reg;
1193 cnt = (rx_byte + 3) >> 2;
1194 if (cnt > 4)
1195 cnt = 4; /* 4 x 32 bits registers only */
1196
Hai Liec1936e2015-04-29 11:39:00 -04001197 if (rx_byte == 4)
1198 read_cnt = 4;
1199 else
1200 read_cnt = pkt_size + 6;
Hai Lia6895542015-03-31 14:36:33 -04001201
1202 /*
1203 * In case of multiple reads from the panel, after the first read, there
1204 * is possibility that there are some bytes in the payload repeating in
1205 * the RDBK_DATA registers. Since we read all the parameters from the
1206 * panel right from the first byte for every pass. We need to skip the
1207 * repeating bytes and then append the new parameters to the rx buffer.
1208 */
1209 if (read_cnt > 16) {
1210 int bytes_shifted;
1211 /* Any data more than 16 bytes will be shifted out.
1212 * The temp read buffer should already contain these bytes.
1213 * The remaining bytes in read buffer are the repeated bytes.
1214 */
1215 bytes_shifted = read_cnt - 16;
1216 repeated_bytes = buf_offset - bytes_shifted;
1217 }
1218
1219 for (i = cnt - 1; i >= 0; i--) {
1220 data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
1221 *temp++ = ntohl(data); /* to host byte order */
1222 DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
1223 }
1224
1225 for (i = repeated_bytes; i < 16; i++)
1226 buf[j++] = reg[i];
1227
1228 return j;
1229}
1230
1231static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
1232 const struct mipi_dsi_msg *msg)
1233{
1234 int len, ret;
1235 int bllp_len = msm_host->mode->hdisplay *
1236 dsi_get_bpp(msm_host->format) / 8;
1237
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301238 len = dsi_cmd_dma_add(msm_host, msg);
Hai Lia6895542015-03-31 14:36:33 -04001239 if (!len) {
1240 pr_err("%s: failed to add cmd type = 0x%x\n",
1241 __func__, msg->type);
1242 return -EINVAL;
1243 }
1244
1245 /* for video mode, do not send cmds more than
1246 * one pixel line, since it only transmit it
1247 * during BLLP.
1248 */
1249 /* TODO: if the command is sent in LP mode, the bit rate is only
1250 * half of esc clk rate. In this case, if the video is already
1251 * actively streaming, we need to check more carefully if the
1252 * command can be fit into one BLLP.
1253 */
1254 if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
1255 pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
1256 __func__, len);
1257 return -EINVAL;
1258 }
1259
1260 ret = dsi_cmd_dma_tx(msm_host, len);
1261 if (ret < len) {
1262 pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d\n",
1263 __func__, msg->type, (*(u8 *)(msg->tx_buf)), len);
1264 return -ECOMM;
1265 }
1266
1267 return len;
1268}
1269
1270static void dsi_sw_reset_restore(struct msm_dsi_host *msm_host)
1271{
1272 u32 data0, data1;
1273
1274 data0 = dsi_read(msm_host, REG_DSI_CTRL);
1275 data1 = data0;
1276 data1 &= ~DSI_CTRL_ENABLE;
1277 dsi_write(msm_host, REG_DSI_CTRL, data1);
1278 /*
1279 * dsi controller need to be disabled before
1280 * clocks turned on
1281 */
1282 wmb();
1283
1284 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
1285 wmb(); /* make sure clocks enabled */
1286
1287 /* dsi controller can only be reset while clocks are running */
1288 dsi_write(msm_host, REG_DSI_RESET, 1);
1289 wmb(); /* make sure reset happen */
1290 dsi_write(msm_host, REG_DSI_RESET, 0);
1291 wmb(); /* controller out of reset */
1292 dsi_write(msm_host, REG_DSI_CTRL, data0);
1293 wmb(); /* make sure dsi controller enabled again */
1294}
1295
Archit Taneja8d23ea42016-10-25 12:17:59 +05301296static void dsi_hpd_worker(struct work_struct *work)
1297{
1298 struct msm_dsi_host *msm_host =
1299 container_of(work, struct msm_dsi_host, hpd_work);
1300
1301 drm_helper_hpd_irq_event(msm_host->dev);
1302}
1303
Hai Lia6895542015-03-31 14:36:33 -04001304static void dsi_err_worker(struct work_struct *work)
1305{
1306 struct msm_dsi_host *msm_host =
1307 container_of(work, struct msm_dsi_host, err_work);
1308 u32 status = msm_host->err_work_state;
1309
Rob Clarkff431fa2015-05-07 15:19:02 -04001310 pr_err_ratelimited("%s: status=%x\n", __func__, status);
Hai Lia6895542015-03-31 14:36:33 -04001311 if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
1312 dsi_sw_reset_restore(msm_host);
1313
1314 /* It is safe to clear here because error irq is disabled. */
1315 msm_host->err_work_state = 0;
1316
1317 /* enable dsi error interrupt */
1318 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
1319}
1320
1321static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
1322{
1323 u32 status;
1324
1325 status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
1326
1327 if (status) {
1328 dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
1329 /* Writing of an extra 0 needed to clear error bits */
1330 dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
1331 msm_host->err_work_state |= DSI_ERR_STATE_ACK;
1332 }
1333}
1334
1335static void dsi_timeout_status(struct msm_dsi_host *msm_host)
1336{
1337 u32 status;
1338
1339 status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
1340
1341 if (status) {
1342 dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
1343 msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
1344 }
1345}
1346
1347static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
1348{
1349 u32 status;
1350
1351 status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
1352
Archit Taneja01199362015-06-25 11:29:24 +05301353 if (status & (DSI_DLN0_PHY_ERR_DLN0_ERR_ESC |
1354 DSI_DLN0_PHY_ERR_DLN0_ERR_SYNC_ESC |
1355 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTROL |
1356 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP0 |
1357 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP1)) {
Hai Lia6895542015-03-31 14:36:33 -04001358 dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
1359 msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
1360 }
1361}
1362
1363static void dsi_fifo_status(struct msm_dsi_host *msm_host)
1364{
1365 u32 status;
1366
1367 status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
1368
1369 /* fifo underflow, overflow */
1370 if (status) {
1371 dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
1372 msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
1373 if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
1374 msm_host->err_work_state |=
1375 DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
1376 }
1377}
1378
1379static void dsi_status(struct msm_dsi_host *msm_host)
1380{
1381 u32 status;
1382
1383 status = dsi_read(msm_host, REG_DSI_STATUS0);
1384
1385 if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
1386 dsi_write(msm_host, REG_DSI_STATUS0, status);
1387 msm_host->err_work_state |=
1388 DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
1389 }
1390}
1391
1392static void dsi_clk_status(struct msm_dsi_host *msm_host)
1393{
1394 u32 status;
1395
1396 status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
1397
1398 if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
1399 dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
1400 msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
1401 }
1402}
1403
1404static void dsi_error(struct msm_dsi_host *msm_host)
1405{
1406 /* disable dsi error interrupt */
1407 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
1408
1409 dsi_clk_status(msm_host);
1410 dsi_fifo_status(msm_host);
1411 dsi_ack_err_status(msm_host);
1412 dsi_timeout_status(msm_host);
1413 dsi_status(msm_host);
1414 dsi_dln0_phy_err(msm_host);
1415
1416 queue_work(msm_host->workqueue, &msm_host->err_work);
1417}
1418
1419static irqreturn_t dsi_host_irq(int irq, void *ptr)
1420{
1421 struct msm_dsi_host *msm_host = ptr;
1422 u32 isr;
1423 unsigned long flags;
1424
1425 if (!msm_host->ctrl_base)
1426 return IRQ_HANDLED;
1427
1428 spin_lock_irqsave(&msm_host->intr_lock, flags);
1429 isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
1430 dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
1431 spin_unlock_irqrestore(&msm_host->intr_lock, flags);
1432
1433 DBG("isr=0x%x, id=%d", isr, msm_host->id);
1434
1435 if (isr & DSI_IRQ_ERROR)
1436 dsi_error(msm_host);
1437
1438 if (isr & DSI_IRQ_VIDEO_DONE)
1439 complete(&msm_host->video_comp);
1440
1441 if (isr & DSI_IRQ_CMD_DMA_DONE)
1442 complete(&msm_host->dma_comp);
1443
1444 return IRQ_HANDLED;
1445}
1446
1447static int dsi_host_init_panel_gpios(struct msm_dsi_host *msm_host,
1448 struct device *panel_device)
1449{
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001450 msm_host->disp_en_gpio = devm_gpiod_get_optional(panel_device,
1451 "disp-enable",
1452 GPIOD_OUT_LOW);
Hai Lia6895542015-03-31 14:36:33 -04001453 if (IS_ERR(msm_host->disp_en_gpio)) {
1454 DBG("cannot get disp-enable-gpios %ld",
1455 PTR_ERR(msm_host->disp_en_gpio));
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001456 return PTR_ERR(msm_host->disp_en_gpio);
Hai Lia6895542015-03-31 14:36:33 -04001457 }
1458
Archit Taneja60d05cb2015-06-25 14:36:35 +05301459 msm_host->te_gpio = devm_gpiod_get_optional(panel_device, "disp-te",
1460 GPIOD_IN);
Hai Lia6895542015-03-31 14:36:33 -04001461 if (IS_ERR(msm_host->te_gpio)) {
1462 DBG("cannot get disp-te-gpios %ld", PTR_ERR(msm_host->te_gpio));
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001463 return PTR_ERR(msm_host->te_gpio);
Hai Lia6895542015-03-31 14:36:33 -04001464 }
1465
1466 return 0;
1467}
1468
1469static int dsi_host_attach(struct mipi_dsi_host *host,
1470 struct mipi_dsi_device *dsi)
1471{
1472 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1473 int ret;
1474
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301475 if (dsi->lanes > msm_host->num_data_lanes)
1476 return -EINVAL;
1477
Hai Lia6895542015-03-31 14:36:33 -04001478 msm_host->channel = dsi->channel;
1479 msm_host->lanes = dsi->lanes;
1480 msm_host->format = dsi->format;
1481 msm_host->mode_flags = dsi->mode_flags;
1482
Archit Taneja9c9f6f82016-12-05 15:24:53 +05301483 msm_dsi_manager_attach_dsi_device(msm_host->id, dsi->mode_flags);
1484
Hai Lia6895542015-03-31 14:36:33 -04001485 /* Some gpios defined in panel DT need to be controlled by host */
1486 ret = dsi_host_init_panel_gpios(msm_host, &dsi->dev);
1487 if (ret)
1488 return ret;
1489
1490 DBG("id=%d", msm_host->id);
1491 if (msm_host->dev)
Archit Taneja8d23ea42016-10-25 12:17:59 +05301492 queue_work(msm_host->workqueue, &msm_host->hpd_work);
Hai Lia6895542015-03-31 14:36:33 -04001493
1494 return 0;
1495}
1496
1497static int dsi_host_detach(struct mipi_dsi_host *host,
1498 struct mipi_dsi_device *dsi)
1499{
1500 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1501
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301502 msm_host->device_node = NULL;
Hai Lia6895542015-03-31 14:36:33 -04001503
1504 DBG("id=%d", msm_host->id);
1505 if (msm_host->dev)
Archit Taneja8d23ea42016-10-25 12:17:59 +05301506 queue_work(msm_host->workqueue, &msm_host->hpd_work);
Hai Lia6895542015-03-31 14:36:33 -04001507
1508 return 0;
1509}
1510
1511static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
1512 const struct mipi_dsi_msg *msg)
1513{
1514 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1515 int ret;
1516
1517 if (!msg || !msm_host->power_on)
1518 return -EINVAL;
1519
1520 mutex_lock(&msm_host->cmd_mutex);
1521 ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
1522 mutex_unlock(&msm_host->cmd_mutex);
1523
1524 return ret;
1525}
1526
1527static struct mipi_dsi_host_ops dsi_host_ops = {
1528 .attach = dsi_host_attach,
1529 .detach = dsi_host_detach,
1530 .transfer = dsi_host_transfer,
1531};
1532
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301533/*
1534 * List of supported physical to logical lane mappings.
1535 * For example, the 2nd entry represents the following mapping:
1536 *
1537 * "3012": Logic 3->Phys 0; Logic 0->Phys 1; Logic 1->Phys 2; Logic 2->Phys 3;
1538 */
1539static const int supported_data_lane_swaps[][4] = {
1540 { 0, 1, 2, 3 },
1541 { 3, 0, 1, 2 },
1542 { 2, 3, 0, 1 },
1543 { 1, 2, 3, 0 },
1544 { 0, 3, 2, 1 },
1545 { 1, 0, 3, 2 },
1546 { 2, 1, 0, 3 },
1547 { 3, 2, 1, 0 },
1548};
1549
1550static int dsi_host_parse_lane_data(struct msm_dsi_host *msm_host,
1551 struct device_node *ep)
1552{
1553 struct device *dev = &msm_host->pdev->dev;
1554 struct property *prop;
1555 u32 lane_map[4];
1556 int ret, i, len, num_lanes;
1557
Archit Taneja60282ce2016-06-08 16:14:19 +05301558 prop = of_find_property(ep, "data-lanes", &len);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301559 if (!prop) {
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301560 dev_dbg(dev,
1561 "failed to find data lane mapping, using default\n");
1562 return 0;
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301563 }
1564
1565 num_lanes = len / sizeof(u32);
1566
1567 if (num_lanes < 1 || num_lanes > 4) {
1568 dev_err(dev, "bad number of data lanes\n");
1569 return -EINVAL;
1570 }
1571
1572 msm_host->num_data_lanes = num_lanes;
1573
Archit Taneja60282ce2016-06-08 16:14:19 +05301574 ret = of_property_read_u32_array(ep, "data-lanes", lane_map,
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301575 num_lanes);
1576 if (ret) {
1577 dev_err(dev, "failed to read lane data\n");
1578 return ret;
1579 }
1580
1581 /*
1582 * compare DT specified physical-logical lane mappings with the ones
1583 * supported by hardware
1584 */
1585 for (i = 0; i < ARRAY_SIZE(supported_data_lane_swaps); i++) {
1586 const int *swap = supported_data_lane_swaps[i];
1587 int j;
1588
Archit Taneja60282ce2016-06-08 16:14:19 +05301589 /*
1590 * the data-lanes array we get from DT has a logical->physical
1591 * mapping. The "data lane swap" register field represents
1592 * supported configurations in a physical->logical mapping.
1593 * Translate the DT mapping to what we understand and find a
1594 * configuration that works.
1595 */
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301596 for (j = 0; j < num_lanes; j++) {
Archit Taneja60282ce2016-06-08 16:14:19 +05301597 if (lane_map[j] < 0 || lane_map[j] > 3)
1598 dev_err(dev, "bad physical lane entry %u\n",
1599 lane_map[j]);
1600
1601 if (swap[lane_map[j]] != j)
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301602 break;
1603 }
1604
1605 if (j == num_lanes) {
1606 msm_host->dlane_swap = i;
1607 return 0;
1608 }
1609 }
1610
1611 return -EINVAL;
1612}
1613
Archit Tanejaf7009d22015-06-25 11:43:40 +05301614static int dsi_host_parse_dt(struct msm_dsi_host *msm_host)
1615{
1616 struct device *dev = &msm_host->pdev->dev;
1617 struct device_node *np = dev->of_node;
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301618 struct device_node *endpoint, *device_node;
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301619 int ret = 0;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301620
Archit Tanejaf7009d22015-06-25 11:43:40 +05301621 /*
Archit Tanejab9ac76f2016-04-27 15:36:53 +05301622 * Get the endpoint of the output port of the DSI host. In our case,
1623 * this is mapped to port number with reg = 1. Don't return an error if
1624 * the remote endpoint isn't defined. It's possible that there is
1625 * nothing connected to the dsi output.
Archit Tanejaf7009d22015-06-25 11:43:40 +05301626 */
Archit Tanejab9ac76f2016-04-27 15:36:53 +05301627 endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);
Archit Tanejaf7009d22015-06-25 11:43:40 +05301628 if (!endpoint) {
1629 dev_dbg(dev, "%s: no endpoint\n", __func__);
1630 return 0;
1631 }
1632
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301633 ret = dsi_host_parse_lane_data(msm_host, endpoint);
1634 if (ret) {
1635 dev_err(dev, "%s: invalid lane configuration %d\n",
1636 __func__, ret);
1637 goto err;
1638 }
1639
Archit Tanejaf7009d22015-06-25 11:43:40 +05301640 /* Get panel node from the output port's endpoint data */
Rob Herring86418f92017-03-22 08:26:06 -05001641 device_node = of_graph_get_remote_node(np, 1, 0);
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301642 if (!device_node) {
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301643 dev_dbg(dev, "%s: no valid device\n", __func__);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301644 goto err;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301645 }
1646
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301647 msm_host->device_node = device_node;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301648
Archit Taneja0c7df472015-10-14 15:31:13 +05301649 if (of_property_read_bool(np, "syscon-sfpb")) {
1650 msm_host->sfpb = syscon_regmap_lookup_by_phandle(np,
1651 "syscon-sfpb");
1652 if (IS_ERR(msm_host->sfpb)) {
1653 dev_err(dev, "%s: failed to get sfpb regmap\n",
1654 __func__);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301655 ret = PTR_ERR(msm_host->sfpb);
Archit Taneja0c7df472015-10-14 15:31:13 +05301656 }
1657 }
1658
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301659 of_node_put(device_node);
1660
1661err:
1662 of_node_put(endpoint);
1663
1664 return ret;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301665}
1666
Archit Taneja32280d62016-06-23 15:26:04 +05301667static int dsi_host_get_id(struct msm_dsi_host *msm_host)
1668{
1669 struct platform_device *pdev = msm_host->pdev;
1670 const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
1671 struct resource *res;
1672 int i;
1673
1674 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_ctrl");
1675 if (!res)
1676 return -EINVAL;
1677
1678 for (i = 0; i < cfg->num_dsi; i++) {
1679 if (cfg->io_start[i] == res->start)
1680 return i;
1681 }
1682
1683 return -EINVAL;
1684}
1685
Hai Lia6895542015-03-31 14:36:33 -04001686int msm_dsi_host_init(struct msm_dsi *msm_dsi)
1687{
1688 struct msm_dsi_host *msm_host = NULL;
1689 struct platform_device *pdev = msm_dsi->pdev;
1690 int ret;
1691
1692 msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
1693 if (!msm_host) {
1694 pr_err("%s: FAILED: cannot alloc dsi host\n",
1695 __func__);
1696 ret = -ENOMEM;
1697 goto fail;
1698 }
1699
Archit Tanejaf7009d22015-06-25 11:43:40 +05301700 msm_host->pdev = pdev;
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05301701 msm_dsi->host = &msm_host->base;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301702
1703 ret = dsi_host_parse_dt(msm_host);
Hai Lia6895542015-03-31 14:36:33 -04001704 if (ret) {
Archit Tanejaf7009d22015-06-25 11:43:40 +05301705 pr_err("%s: failed to parse dt\n", __func__);
Hai Lia6895542015-03-31 14:36:33 -04001706 goto fail;
1707 }
Hai Lia6895542015-03-31 14:36:33 -04001708
Hai Lia6895542015-03-31 14:36:33 -04001709 msm_host->ctrl_base = msm_ioremap(pdev, "dsi_ctrl", "DSI CTRL");
1710 if (IS_ERR(msm_host->ctrl_base)) {
1711 pr_err("%s: unable to map Dsi ctrl base\n", __func__);
1712 ret = PTR_ERR(msm_host->ctrl_base);
1713 goto fail;
1714 }
1715
Archit Tanejaf6be1122017-07-28 16:17:03 +05301716 pm_runtime_enable(&pdev->dev);
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);
Hai Lia6895542015-03-31 14:36:33 -04001758 spin_lock_init(&msm_host->intr_lock);
1759
1760 /* setup workqueue */
1761 msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
1762 INIT_WORK(&msm_host->err_work, dsi_err_worker);
Archit Taneja8d23ea42016-10-25 12:17:59 +05301763 INIT_WORK(&msm_host->hpd_work, dsi_hpd_worker);
Hai Lia6895542015-03-31 14:36:33 -04001764
Hai Lia6895542015-03-31 14:36:33 -04001765 msm_dsi->id = msm_host->id;
1766
1767 DBG("Dsi Host %d initialized", msm_host->id);
1768 return 0;
1769
1770fail:
1771 return ret;
1772}
1773
1774void msm_dsi_host_destroy(struct mipi_dsi_host *host)
1775{
1776 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1777
1778 DBG("");
1779 dsi_tx_buf_free(msm_host);
1780 if (msm_host->workqueue) {
1781 flush_workqueue(msm_host->workqueue);
1782 destroy_workqueue(msm_host->workqueue);
1783 msm_host->workqueue = NULL;
1784 }
1785
Hai Lia6895542015-03-31 14:36:33 -04001786 mutex_destroy(&msm_host->cmd_mutex);
1787 mutex_destroy(&msm_host->dev_mutex);
Archit Tanejaf6be1122017-07-28 16:17:03 +05301788
1789 pm_runtime_disable(&msm_host->pdev->dev);
Hai Lia6895542015-03-31 14:36:33 -04001790}
1791
1792int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
1793 struct drm_device *dev)
1794{
1795 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1796 struct platform_device *pdev = msm_host->pdev;
1797 int ret;
1798
1799 msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1800 if (msm_host->irq < 0) {
1801 ret = msm_host->irq;
1802 dev_err(dev->dev, "failed to get irq: %d\n", ret);
1803 return ret;
1804 }
1805
1806 ret = devm_request_irq(&pdev->dev, msm_host->irq,
1807 dsi_host_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1808 "dsi_isr", msm_host);
1809 if (ret < 0) {
1810 dev_err(&pdev->dev, "failed to request IRQ%u: %d\n",
1811 msm_host->irq, ret);
1812 return ret;
1813 }
1814
1815 msm_host->dev = dev;
1816 ret = dsi_tx_buf_alloc(msm_host, SZ_4K);
1817 if (ret) {
1818 pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
1819 return ret;
1820 }
1821
1822 return 0;
1823}
1824
1825int msm_dsi_host_register(struct mipi_dsi_host *host, bool check_defer)
1826{
1827 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lia6895542015-03-31 14:36:33 -04001828 int ret;
1829
1830 /* Register mipi dsi host */
1831 if (!msm_host->registered) {
1832 host->dev = &msm_host->pdev->dev;
1833 host->ops = &dsi_host_ops;
1834 ret = mipi_dsi_host_register(host);
1835 if (ret)
1836 return ret;
1837
1838 msm_host->registered = true;
1839
1840 /* If the panel driver has not been probed after host register,
1841 * we should defer the host's probe.
1842 * It makes sure panel is connected when fbcon detects
1843 * connector status and gets the proper display mode to
1844 * create framebuffer.
Archit Tanejaf7009d22015-06-25 11:43:40 +05301845 * Don't try to defer if there is nothing connected to the dsi
1846 * output
Hai Lia6895542015-03-31 14:36:33 -04001847 */
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301848 if (check_defer && msm_host->device_node) {
1849 if (!of_drm_find_panel(msm_host->device_node))
Archit Tanejac118e292015-07-31 14:06:10 +05301850 if (!of_drm_find_bridge(msm_host->device_node))
1851 return -EPROBE_DEFER;
Hai Lia6895542015-03-31 14:36:33 -04001852 }
1853 }
1854
1855 return 0;
1856}
1857
1858void msm_dsi_host_unregister(struct mipi_dsi_host *host)
1859{
1860 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1861
1862 if (msm_host->registered) {
1863 mipi_dsi_host_unregister(host);
1864 host->dev = NULL;
1865 host->ops = NULL;
1866 msm_host->registered = false;
1867 }
1868}
1869
1870int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
1871 const struct mipi_dsi_msg *msg)
1872{
1873 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1874
1875 /* TODO: make sure dsi_cmd_mdp is idle.
1876 * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
1877 * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
1878 * How to handle the old versions? Wait for mdp cmd done?
1879 */
1880
1881 /*
1882 * mdss interrupt is generated in mdp core clock domain
1883 * mdp clock need to be enabled to receive dsi interrupt
1884 */
Archit Tanejaf6be1122017-07-28 16:17:03 +05301885 pm_runtime_get_sync(&msm_host->pdev->dev);
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05301886 dsi_link_clk_enable(msm_host);
Hai Lia6895542015-03-31 14:36:33 -04001887
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
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05301916 dsi_link_clk_disable(msm_host);
Archit Tanejaf6be1122017-07-28 16:17:03 +05301917 pm_runtime_put_autosuspend(&msm_host->pdev->dev);
Hai Lia6895542015-03-31 14:36:33 -04001918}
1919
1920int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
1921 const struct mipi_dsi_msg *msg)
1922{
1923 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1924
1925 return dsi_cmds2buf_tx(msm_host, msg);
1926}
1927
1928int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
1929 const struct mipi_dsi_msg *msg)
1930{
1931 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lid248b612015-08-13 17:49:29 -04001932 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -04001933 int data_byte, rx_byte, dlen, end;
1934 int short_response, diff, pkt_size, ret = 0;
1935 char cmd;
1936 int rlen = msg->rx_len;
1937 u8 *buf;
1938
1939 if (rlen <= 2) {
1940 short_response = 1;
1941 pkt_size = rlen;
1942 rx_byte = 4;
1943 } else {
1944 short_response = 0;
1945 data_byte = 10; /* first read */
1946 if (rlen < data_byte)
1947 pkt_size = rlen;
1948 else
1949 pkt_size = data_byte;
1950 rx_byte = data_byte + 6; /* 4 header + 2 crc */
1951 }
1952
1953 buf = msm_host->rx_buf;
1954 end = 0;
1955 while (!end) {
1956 u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
1957 struct mipi_dsi_msg max_pkt_size_msg = {
1958 .channel = msg->channel,
1959 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
1960 .tx_len = 2,
1961 .tx_buf = tx,
1962 };
1963
1964 DBG("rlen=%d pkt_size=%d rx_byte=%d",
1965 rlen, pkt_size, rx_byte);
1966
1967 ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
1968 if (ret < 2) {
1969 pr_err("%s: Set max pkt size failed, %d\n",
1970 __func__, ret);
1971 return -EINVAL;
1972 }
1973
Hai Lid248b612015-08-13 17:49:29 -04001974 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
1975 (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
Hai Lia6895542015-03-31 14:36:33 -04001976 /* Clear the RDBK_DATA registers */
1977 dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
1978 DSI_RDBK_DATA_CTRL_CLR);
1979 wmb(); /* make sure the RDBK registers are cleared */
1980 dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
1981 wmb(); /* release cleared status before transfer */
1982 }
1983
1984 ret = dsi_cmds2buf_tx(msm_host, msg);
1985 if (ret < msg->tx_len) {
1986 pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
1987 return ret;
1988 }
1989
1990 /*
1991 * once cmd_dma_done interrupt received,
1992 * return data from client is ready and stored
1993 * at RDBK_DATA register already
1994 * since rx fifo is 16 bytes, dcs header is kept at first loop,
1995 * after that dcs header lost during shift into registers
1996 */
1997 dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
1998
1999 if (dlen <= 0)
2000 return 0;
2001
2002 if (short_response)
2003 break;
2004
2005 if (rlen <= data_byte) {
2006 diff = data_byte - rlen;
2007 end = 1;
2008 } else {
2009 diff = 0;
2010 rlen -= data_byte;
2011 }
2012
2013 if (!end) {
2014 dlen -= 2; /* 2 crc */
2015 dlen -= diff;
2016 buf += dlen; /* next start position */
2017 data_byte = 14; /* NOT first read */
2018 if (rlen < data_byte)
2019 pkt_size += rlen;
2020 else
2021 pkt_size += data_byte;
2022 DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
2023 }
2024 }
2025
2026 /*
2027 * For single Long read, if the requested rlen < 10,
2028 * we need to shift the start position of rx
2029 * data buffer to skip the bytes which are not
2030 * updated.
2031 */
2032 if (pkt_size < 10 && !short_response)
2033 buf = msm_host->rx_buf + (10 - rlen);
2034 else
2035 buf = msm_host->rx_buf;
2036
2037 cmd = buf[0];
2038 switch (cmd) {
2039 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
2040 pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
2041 ret = 0;
Hai Li651ad3f2015-04-29 11:38:59 -04002042 break;
Hai Lia6895542015-03-31 14:36:33 -04002043 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
2044 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
2045 ret = dsi_short_read1_resp(buf, msg);
2046 break;
2047 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
2048 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
2049 ret = dsi_short_read2_resp(buf, msg);
2050 break;
2051 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
2052 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
2053 ret = dsi_long_read_resp(buf, msg);
2054 break;
2055 default:
2056 pr_warn("%s:Invalid response cmd\n", __func__);
2057 ret = 0;
2058 }
2059
2060 return ret;
2061}
2062
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05302063void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 dma_base,
2064 u32 len)
Hai Lia6895542015-03-31 14:36:33 -04002065{
2066 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2067
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05302068 dsi_write(msm_host, REG_DSI_DMA_BASE, dma_base);
Hai Lia6895542015-03-31 14:36:33 -04002069 dsi_write(msm_host, REG_DSI_DMA_LEN, len);
2070 dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
2071
2072 /* Make sure trigger happens */
2073 wmb();
2074}
2075
Hai Li9d32c4982015-05-15 13:04:05 -04002076int msm_dsi_host_set_src_pll(struct mipi_dsi_host *host,
2077 struct msm_dsi_pll *src_pll)
2078{
2079 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Archit Taneja4bfa9742015-10-09 16:32:38 +05302080 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Li9d32c4982015-05-15 13:04:05 -04002081 struct clk *byte_clk_provider, *pixel_clk_provider;
2082 int ret;
2083
2084 ret = msm_dsi_pll_get_clk_provider(src_pll,
2085 &byte_clk_provider, &pixel_clk_provider);
2086 if (ret) {
2087 pr_info("%s: can't get provider from pll, don't set parent\n",
2088 __func__);
2089 return 0;
2090 }
2091
2092 ret = clk_set_parent(msm_host->byte_clk_src, byte_clk_provider);
2093 if (ret) {
2094 pr_err("%s: can't set parent to byte_clk_src. ret=%d\n",
2095 __func__, ret);
2096 goto exit;
2097 }
2098
2099 ret = clk_set_parent(msm_host->pixel_clk_src, pixel_clk_provider);
2100 if (ret) {
2101 pr_err("%s: can't set parent to pixel_clk_src. ret=%d\n",
2102 __func__, ret);
2103 goto exit;
2104 }
2105
Archit Taneja4bfa9742015-10-09 16:32:38 +05302106 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
2107 ret = clk_set_parent(msm_host->dsi_clk_src, pixel_clk_provider);
2108 if (ret) {
2109 pr_err("%s: can't set parent to dsi_clk_src. ret=%d\n",
2110 __func__, ret);
2111 goto exit;
2112 }
2113
2114 ret = clk_set_parent(msm_host->esc_clk_src, byte_clk_provider);
2115 if (ret) {
2116 pr_err("%s: can't set parent to esc_clk_src. ret=%d\n",
2117 __func__, ret);
2118 goto exit;
2119 }
2120 }
2121
Hai Li9d32c4982015-05-15 13:04:05 -04002122exit:
2123 return ret;
2124}
2125
Archit Taneja34d95452015-07-29 12:14:12 -04002126void msm_dsi_host_reset_phy(struct mipi_dsi_host *host)
2127{
2128 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2129
2130 DBG("");
2131 dsi_write(msm_host, REG_DSI_PHY_RESET, DSI_PHY_RESET_RESET);
2132 /* Make sure fully reset */
2133 wmb();
2134 udelay(1000);
2135 dsi_write(msm_host, REG_DSI_PHY_RESET, 0);
2136 udelay(100);
2137}
2138
Hai Lib62aa702017-01-07 14:24:38 +05302139void msm_dsi_host_get_phy_clk_req(struct mipi_dsi_host *host,
2140 struct msm_dsi_phy_clk_request *clk_req)
2141{
2142 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Archit Tanejad4cea382017-07-12 15:09:55 +05302143 int ret;
2144
2145 ret = dsi_calc_clk_rate(msm_host);
2146 if (ret) {
2147 pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
2148 return;
2149 }
Hai Lib62aa702017-01-07 14:24:38 +05302150
2151 clk_req->bitclk_rate = msm_host->byte_clk_rate * 8;
2152 clk_req->escclk_rate = msm_host->esc_clk_rate;
2153}
2154
Hai Lia6895542015-03-31 14:36:33 -04002155int msm_dsi_host_enable(struct mipi_dsi_host *host)
2156{
2157 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2158
2159 dsi_op_mode_config(msm_host,
2160 !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
2161
2162 /* TODO: clock should be turned off for command mode,
2163 * and only turned on before MDP START.
2164 * This part of code should be enabled once mdp driver support it.
2165 */
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05302166 /* if (msm_panel->mode == MSM_DSI_CMD_MODE) {
2167 * dsi_link_clk_disable(msm_host);
2168 * pm_runtime_put_autosuspend(&msm_host->pdev->dev);
2169 * }
2170 */
Hai Lia6895542015-03-31 14:36:33 -04002171
2172 return 0;
2173}
2174
2175int msm_dsi_host_disable(struct mipi_dsi_host *host)
2176{
2177 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2178
2179 dsi_op_mode_config(msm_host,
2180 !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
2181
2182 /* Since we have disabled INTF, the video engine won't stop so that
2183 * the cmd engine will be blocked.
2184 * Reset to disable video engine so that we can send off cmd.
2185 */
2186 dsi_sw_reset(msm_host);
2187
2188 return 0;
2189}
2190
Archit Taneja0c7df472015-10-14 15:31:13 +05302191static void msm_dsi_sfpb_config(struct msm_dsi_host *msm_host, bool enable)
2192{
2193 enum sfpb_ahb_arb_master_port_en en;
2194
2195 if (!msm_host->sfpb)
2196 return;
2197
2198 en = enable ? SFPB_MASTER_PORT_ENABLE : SFPB_MASTER_PORT_DISABLE;
2199
2200 regmap_update_bits(msm_host->sfpb, REG_SFPB_GPREG,
2201 SFPB_GPREG_MASTER_PORT_EN__MASK,
2202 SFPB_GPREG_MASTER_PORT_EN(en));
2203}
2204
Hai Lib62aa702017-01-07 14:24:38 +05302205int msm_dsi_host_power_on(struct mipi_dsi_host *host,
2206 struct msm_dsi_phy_shared_timings *phy_shared_timings)
Hai Lia6895542015-03-31 14:36:33 -04002207{
2208 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lia6895542015-03-31 14:36:33 -04002209 int ret = 0;
2210
2211 mutex_lock(&msm_host->dev_mutex);
2212 if (msm_host->power_on) {
2213 DBG("dsi host already on");
2214 goto unlock_ret;
2215 }
2216
Archit Taneja0c7df472015-10-14 15:31:13 +05302217 msm_dsi_sfpb_config(msm_host, true);
2218
Hai Lia6895542015-03-31 14:36:33 -04002219 ret = dsi_host_regulator_enable(msm_host);
2220 if (ret) {
2221 pr_err("%s:Failed to enable vregs.ret=%d\n",
2222 __func__, ret);
2223 goto unlock_ret;
2224 }
2225
Archit Tanejaf6be1122017-07-28 16:17:03 +05302226 pm_runtime_get_sync(&msm_host->pdev->dev);
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05302227 ret = dsi_link_clk_enable(msm_host);
Hai Lia6895542015-03-31 14:36:33 -04002228 if (ret) {
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05302229 pr_err("%s: failed to enable link clocks. ret=%d\n",
2230 __func__, ret);
Hai Lia6895542015-03-31 14:36:33 -04002231 goto fail_disable_reg;
2232 }
2233
Hai Liab8909b2015-06-11 10:56:46 -04002234 ret = pinctrl_pm_select_default_state(&msm_host->pdev->dev);
2235 if (ret) {
2236 pr_err("%s: failed to set pinctrl default state, %d\n",
2237 __func__, ret);
2238 goto fail_disable_clk;
2239 }
2240
Hai Lia6895542015-03-31 14:36:33 -04002241 dsi_timing_setup(msm_host);
2242 dsi_sw_reset(msm_host);
Hai Lib62aa702017-01-07 14:24:38 +05302243 dsi_ctrl_config(msm_host, true, phy_shared_timings);
Hai Lia6895542015-03-31 14:36:33 -04002244
2245 if (msm_host->disp_en_gpio)
2246 gpiod_set_value(msm_host->disp_en_gpio, 1);
2247
2248 msm_host->power_on = true;
2249 mutex_unlock(&msm_host->dev_mutex);
2250
2251 return 0;
2252
Hai Liab8909b2015-06-11 10:56:46 -04002253fail_disable_clk:
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05302254 dsi_link_clk_disable(msm_host);
2255 pm_runtime_put_autosuspend(&msm_host->pdev->dev);
Hai Lia6895542015-03-31 14:36:33 -04002256fail_disable_reg:
2257 dsi_host_regulator_disable(msm_host);
2258unlock_ret:
2259 mutex_unlock(&msm_host->dev_mutex);
2260 return ret;
2261}
2262
2263int msm_dsi_host_power_off(struct mipi_dsi_host *host)
2264{
2265 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2266
2267 mutex_lock(&msm_host->dev_mutex);
2268 if (!msm_host->power_on) {
2269 DBG("dsi host already off");
2270 goto unlock_ret;
2271 }
2272
Hai Lidceac342016-09-15 14:34:49 +05302273 dsi_ctrl_config(msm_host, false, NULL);
Hai Lia6895542015-03-31 14:36:33 -04002274
2275 if (msm_host->disp_en_gpio)
2276 gpiod_set_value(msm_host->disp_en_gpio, 0);
2277
Hai Liab8909b2015-06-11 10:56:46 -04002278 pinctrl_pm_select_sleep_state(&msm_host->pdev->dev);
2279
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05302280 dsi_link_clk_disable(msm_host);
Archit Tanejaf6be1122017-07-28 16:17:03 +05302281 pm_runtime_put_autosuspend(&msm_host->pdev->dev);
Hai Lia6895542015-03-31 14:36:33 -04002282
2283 dsi_host_regulator_disable(msm_host);
2284
Archit Taneja0c7df472015-10-14 15:31:13 +05302285 msm_dsi_sfpb_config(msm_host, false);
2286
Hai Lia6895542015-03-31 14:36:33 -04002287 DBG("-");
2288
2289 msm_host->power_on = false;
2290
2291unlock_ret:
2292 mutex_unlock(&msm_host->dev_mutex);
2293 return 0;
2294}
2295
2296int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
2297 struct drm_display_mode *mode)
2298{
2299 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2300
2301 if (msm_host->mode) {
2302 drm_mode_destroy(msm_host->dev, msm_host->mode);
2303 msm_host->mode = NULL;
2304 }
2305
2306 msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
Wei Yongjun2abe1f22016-06-18 17:26:37 +00002307 if (!msm_host->mode) {
Hai Lia6895542015-03-31 14:36:33 -04002308 pr_err("%s: cannot duplicate mode\n", __func__);
Wei Yongjun2abe1f22016-06-18 17:26:37 +00002309 return -ENOMEM;
Hai Lia6895542015-03-31 14:36:33 -04002310 }
2311
2312 return 0;
2313}
2314
2315struct drm_panel *msm_dsi_host_get_panel(struct mipi_dsi_host *host,
2316 unsigned long *panel_flags)
2317{
2318 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2319 struct drm_panel *panel;
2320
Archit Tanejaa9ddac92015-08-03 14:05:45 +05302321 panel = of_drm_find_panel(msm_host->device_node);
Hai Lia6895542015-03-31 14:36:33 -04002322 if (panel_flags)
2323 *panel_flags = msm_host->mode_flags;
2324
2325 return panel;
2326}
2327
Archit Tanejac118e292015-07-31 14:06:10 +05302328struct drm_bridge *msm_dsi_host_get_bridge(struct mipi_dsi_host *host)
2329{
2330 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2331
2332 return of_drm_find_bridge(msm_host->device_node);
2333}