blob: f675975c26559ae254018f10069543beee2da60a [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;
Archit Tanejac1d97082018-01-17 15:04:44 +0530118 struct clk *byte_intf_clk;
Hai Li9d32c4982015-05-15 13:04:05 -0400119
Hai Lia6895542015-03-31 14:36:33 -0400120 u32 byte_clk_rate;
Archit Taneja4bfa9742015-10-09 16:32:38 +0530121 u32 esc_clk_rate;
122
123 /* DSI v2 specific clocks */
124 struct clk *src_clk;
125 struct clk *esc_clk_src;
126 struct clk *dsi_clk_src;
127
128 u32 src_clk_rate;
Hai Lia6895542015-03-31 14:36:33 -0400129
130 struct gpio_desc *disp_en_gpio;
131 struct gpio_desc *te_gpio;
132
Hai Lid248b612015-08-13 17:49:29 -0400133 const struct msm_dsi_cfg_handler *cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400134
135 struct completion dma_comp;
136 struct completion video_comp;
137 struct mutex dev_mutex;
138 struct mutex cmd_mutex;
Hai Lia6895542015-03-31 14:36:33 -0400139 spinlock_t intr_lock; /* Protect interrupt ctrl register */
140
141 u32 err_work_state;
142 struct work_struct err_work;
Archit Taneja8d23ea42016-10-25 12:17:59 +0530143 struct work_struct hpd_work;
Hai Lia6895542015-03-31 14:36:33 -0400144 struct workqueue_struct *workqueue;
145
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530146 /* DSI 6G TX buffer*/
Hai Lia6895542015-03-31 14:36:33 -0400147 struct drm_gem_object *tx_gem_obj;
Archit Taneja4ff9d4c2015-10-13 12:20:47 +0530148
149 /* DSI v2 TX buffer */
150 void *tx_buf;
151 dma_addr_t tx_buf_paddr;
152
153 int tx_size;
154
Hai Lia6895542015-03-31 14:36:33 -0400155 u8 *rx_buf;
156
Archit Taneja0c7df472015-10-14 15:31:13 +0530157 struct regmap *sfpb;
158
Hai Lia6895542015-03-31 14:36:33 -0400159 struct drm_display_mode *mode;
160
Archit Tanejaa9ddac92015-08-03 14:05:45 +0530161 /* connected device info */
162 struct device_node *device_node;
Hai Lia6895542015-03-31 14:36:33 -0400163 unsigned int channel;
164 unsigned int lanes;
165 enum mipi_dsi_pixel_format format;
166 unsigned long mode_flags;
167
Archit Taneja26f7d1f2016-02-25 11:19:48 +0530168 /* lane data parsed via DT */
169 int dlane_swap;
170 int num_data_lanes;
171
Hai Lia6895542015-03-31 14:36:33 -0400172 u32 dma_cmd_ctrl_restore;
173
174 bool registered;
175 bool power_on;
176 int irq;
177};
178
179static u32 dsi_get_bpp(const enum mipi_dsi_pixel_format fmt)
180{
181 switch (fmt) {
182 case MIPI_DSI_FMT_RGB565: return 16;
183 case MIPI_DSI_FMT_RGB666_PACKED: return 18;
184 case MIPI_DSI_FMT_RGB666:
185 case MIPI_DSI_FMT_RGB888:
186 default: return 24;
187 }
188}
189
190static inline u32 dsi_read(struct msm_dsi_host *msm_host, u32 reg)
191{
Hai Lid248b612015-08-13 17:49:29 -0400192 return msm_readl(msm_host->ctrl_base + reg);
Hai Lia6895542015-03-31 14:36:33 -0400193}
194static inline void dsi_write(struct msm_dsi_host *msm_host, u32 reg, u32 data)
195{
Hai Lid248b612015-08-13 17:49:29 -0400196 msm_writel(data, msm_host->ctrl_base + reg);
Hai Lia6895542015-03-31 14:36:33 -0400197}
198
199static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host);
200static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host);
201
Hai Lid248b612015-08-13 17:49:29 -0400202static const struct msm_dsi_cfg_handler *dsi_get_config(
203 struct msm_dsi_host *msm_host)
Hai Lia6895542015-03-31 14:36:33 -0400204{
Hai Lid248b612015-08-13 17:49:29 -0400205 const struct msm_dsi_cfg_handler *cfg_hnd = NULL;
Archit Taneja31c92762015-10-09 12:40:39 +0530206 struct device *dev = &msm_host->pdev->dev;
Hai Lia6895542015-03-31 14:36:33 -0400207 struct regulator *gdsc_reg;
Archit Taneja31c92762015-10-09 12:40:39 +0530208 struct clk *ahb_clk;
Hai Lid248b612015-08-13 17:49:29 -0400209 int ret;
Hai Lia6895542015-03-31 14:36:33 -0400210 u32 major = 0, minor = 0;
211
Archit Taneja31c92762015-10-09 12:40:39 +0530212 gdsc_reg = regulator_get(dev, "gdsc");
Fabian Frederickbdc80de2015-05-04 19:03:55 +0200213 if (IS_ERR(gdsc_reg)) {
Hai Lia6895542015-03-31 14:36:33 -0400214 pr_err("%s: cannot get gdsc\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400215 goto exit;
Hai Lia6895542015-03-31 14:36:33 -0400216 }
Archit Taneja31c92762015-10-09 12:40:39 +0530217
Archit Taneja29a11572018-01-17 15:04:42 +0530218 ahb_clk = msm_clk_get(msm_host->pdev, "iface");
Archit Taneja31c92762015-10-09 12:40:39 +0530219 if (IS_ERR(ahb_clk)) {
220 pr_err("%s: cannot get interface clock\n", __func__);
221 goto put_gdsc;
222 }
223
Archit Tanejaf6be1122017-07-28 16:17:03 +0530224 pm_runtime_get_sync(dev);
225
Hai Lia6895542015-03-31 14:36:33 -0400226 ret = regulator_enable(gdsc_reg);
227 if (ret) {
228 pr_err("%s: unable to enable gdsc\n", __func__);
Archit Taneja29a11572018-01-17 15:04:42 +0530229 goto put_gdsc;
Hai Lia6895542015-03-31 14:36:33 -0400230 }
Archit Taneja31c92762015-10-09 12:40:39 +0530231
232 ret = clk_prepare_enable(ahb_clk);
Hai Lia6895542015-03-31 14:36:33 -0400233 if (ret) {
234 pr_err("%s: unable to enable ahb_clk\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400235 goto disable_gdsc;
Hai Lia6895542015-03-31 14:36:33 -0400236 }
237
238 ret = dsi_get_version(msm_host->ctrl_base, &major, &minor);
Hai Lia6895542015-03-31 14:36:33 -0400239 if (ret) {
240 pr_err("%s: Invalid version\n", __func__);
Hai Lid248b612015-08-13 17:49:29 -0400241 goto disable_clks;
Hai Lia6895542015-03-31 14:36:33 -0400242 }
243
Hai Lid248b612015-08-13 17:49:29 -0400244 cfg_hnd = msm_dsi_cfg_get(major, minor);
Hai Lia6895542015-03-31 14:36:33 -0400245
Hai Lid248b612015-08-13 17:49:29 -0400246 DBG("%s: Version %x:%x\n", __func__, major, minor);
247
248disable_clks:
Archit Taneja31c92762015-10-09 12:40:39 +0530249 clk_disable_unprepare(ahb_clk);
Hai Lid248b612015-08-13 17:49:29 -0400250disable_gdsc:
251 regulator_disable(gdsc_reg);
Archit Tanejaa18a0ea2017-10-06 16:27:06 +0530252 pm_runtime_put_sync(dev);
Hai Lid248b612015-08-13 17:49:29 -0400253put_gdsc:
254 regulator_put(gdsc_reg);
255exit:
256 return cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400257}
258
259static inline struct msm_dsi_host *to_msm_dsi_host(struct mipi_dsi_host *host)
260{
261 return container_of(host, struct msm_dsi_host, base);
262}
263
264static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host)
265{
266 struct regulator_bulk_data *s = msm_host->supplies;
Hai Lid248b612015-08-13 17:49:29 -0400267 const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
268 int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
Hai Lia6895542015-03-31 14:36:33 -0400269 int i;
270
271 DBG("");
272 for (i = num - 1; i >= 0; i--)
273 if (regs[i].disable_load >= 0)
Dave Airlie2c33ce02015-04-20 11:32:26 +1000274 regulator_set_load(s[i].consumer,
275 regs[i].disable_load);
Hai Lia6895542015-03-31 14:36:33 -0400276
277 regulator_bulk_disable(num, s);
278}
279
280static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host)
281{
282 struct regulator_bulk_data *s = msm_host->supplies;
Hai Lid248b612015-08-13 17:49:29 -0400283 const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
284 int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
Hai Lia6895542015-03-31 14:36:33 -0400285 int ret, i;
286
287 DBG("");
288 for (i = 0; i < num; i++) {
289 if (regs[i].enable_load >= 0) {
Dave Airlie2c33ce02015-04-20 11:32:26 +1000290 ret = regulator_set_load(s[i].consumer,
291 regs[i].enable_load);
Hai Lia6895542015-03-31 14:36:33 -0400292 if (ret < 0) {
293 pr_err("regulator %d set op mode failed, %d\n",
294 i, ret);
295 goto fail;
296 }
297 }
298 }
299
300 ret = regulator_bulk_enable(num, s);
301 if (ret < 0) {
302 pr_err("regulator enable failed, %d\n", ret);
303 goto fail;
304 }
305
306 return 0;
307
308fail:
309 for (i--; i >= 0; i--)
Dave Airlie2c33ce02015-04-20 11:32:26 +1000310 regulator_set_load(s[i].consumer, regs[i].disable_load);
Hai Lia6895542015-03-31 14:36:33 -0400311 return ret;
312}
313
314static int dsi_regulator_init(struct msm_dsi_host *msm_host)
315{
316 struct regulator_bulk_data *s = msm_host->supplies;
Hai Lid248b612015-08-13 17:49:29 -0400317 const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
318 int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
Hai Lia6895542015-03-31 14:36:33 -0400319 int i, ret;
320
321 for (i = 0; i < num; i++)
322 s[i].supply = regs[i].name;
323
324 ret = devm_regulator_bulk_get(&msm_host->pdev->dev, num, s);
325 if (ret < 0) {
326 pr_err("%s: failed to init regulator, ret=%d\n",
327 __func__, ret);
328 return ret;
329 }
330
Hai Lia6895542015-03-31 14:36:33 -0400331 return 0;
332}
333
334static int dsi_clk_init(struct msm_dsi_host *msm_host)
335{
Rob Clarkdb9a3752017-10-16 13:35:57 -0400336 struct platform_device *pdev = msm_host->pdev;
Archit Taneja4bfa9742015-10-09 16:32:38 +0530337 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
338 const struct msm_dsi_config *cfg = cfg_hnd->cfg;
Archit Taneja6e0eb522015-10-09 15:21:12 +0530339 int i, ret = 0;
Hai Lia6895542015-03-31 14:36:33 -0400340
Archit Taneja6e0eb522015-10-09 15:21:12 +0530341 /* get bus clocks */
342 for (i = 0; i < cfg->num_bus_clks; i++) {
Rob Clarkdb9a3752017-10-16 13:35:57 -0400343 msm_host->bus_clks[i] = msm_clk_get(pdev,
Archit Taneja6e0eb522015-10-09 15:21:12 +0530344 cfg->bus_clk_names[i]);
345 if (IS_ERR(msm_host->bus_clks[i])) {
346 ret = PTR_ERR(msm_host->bus_clks[i]);
Rob Clarkdb9a3752017-10-16 13:35:57 -0400347 pr_err("%s: Unable to get %s clock, ret = %d\n",
Archit Taneja6e0eb522015-10-09 15:21:12 +0530348 __func__, cfg->bus_clk_names[i], ret);
349 goto exit;
350 }
Hai Lia6895542015-03-31 14:36:33 -0400351 }
352
Archit Taneja6e0eb522015-10-09 15:21:12 +0530353 /* get link and source clocks */
Rob Clarkdb9a3752017-10-16 13:35:57 -0400354 msm_host->byte_clk = msm_clk_get(pdev, "byte");
Hai Lia6895542015-03-31 14:36:33 -0400355 if (IS_ERR(msm_host->byte_clk)) {
356 ret = PTR_ERR(msm_host->byte_clk);
Rob Clarkdb9a3752017-10-16 13:35:57 -0400357 pr_err("%s: can't find dsi_byte clock. ret=%d\n",
Hai Lia6895542015-03-31 14:36:33 -0400358 __func__, ret);
359 msm_host->byte_clk = NULL;
360 goto exit;
361 }
362
Rob Clarkdb9a3752017-10-16 13:35:57 -0400363 msm_host->pixel_clk = msm_clk_get(pdev, "pixel");
Hai Lia6895542015-03-31 14:36:33 -0400364 if (IS_ERR(msm_host->pixel_clk)) {
365 ret = PTR_ERR(msm_host->pixel_clk);
Rob Clarkdb9a3752017-10-16 13:35:57 -0400366 pr_err("%s: can't find dsi_pixel clock. ret=%d\n",
Hai Lia6895542015-03-31 14:36:33 -0400367 __func__, ret);
368 msm_host->pixel_clk = NULL;
369 goto exit;
370 }
371
Rob Clarkdb9a3752017-10-16 13:35:57 -0400372 msm_host->esc_clk = msm_clk_get(pdev, "core");
Hai Lia6895542015-03-31 14:36:33 -0400373 if (IS_ERR(msm_host->esc_clk)) {
374 ret = PTR_ERR(msm_host->esc_clk);
Rob Clarkdb9a3752017-10-16 13:35:57 -0400375 pr_err("%s: can't find dsi_esc clock. ret=%d\n",
Hai Lia6895542015-03-31 14:36:33 -0400376 __func__, ret);
377 msm_host->esc_clk = NULL;
378 goto exit;
379 }
380
Archit Tanejac1d97082018-01-17 15:04:44 +0530381 msm_host->byte_intf_clk = msm_clk_get(pdev, "byte_intf");
382 if (IS_ERR(msm_host->byte_intf_clk)) {
383 ret = PTR_ERR(msm_host->byte_intf_clk);
384 pr_debug("%s: can't find byte_intf clock. ret=%d\n",
385 __func__, ret);
386 msm_host->byte_intf_clk = NULL;
387 }
388
Archit Tanejae6c4c782015-11-30 17:47:17 +0530389 msm_host->byte_clk_src = clk_get_parent(msm_host->byte_clk);
390 if (!msm_host->byte_clk_src) {
391 ret = -ENODEV;
Rob Clarkdb9a3752017-10-16 13:35:57 -0400392 pr_err("%s: can't find byte_clk clock. ret=%d\n", __func__, ret);
Hai Li9d32c4982015-05-15 13:04:05 -0400393 goto exit;
394 }
395
Archit Tanejae6c4c782015-11-30 17:47:17 +0530396 msm_host->pixel_clk_src = clk_get_parent(msm_host->pixel_clk);
397 if (!msm_host->pixel_clk_src) {
398 ret = -ENODEV;
Rob Clarkdb9a3752017-10-16 13:35:57 -0400399 pr_err("%s: can't find pixel_clk clock. ret=%d\n", __func__, ret);
Archit Taneja4bfa9742015-10-09 16:32:38 +0530400 goto exit;
Hai Li9d32c4982015-05-15 13:04:05 -0400401 }
402
Archit Taneja4bfa9742015-10-09 16:32:38 +0530403 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
Rob Clarkdb9a3752017-10-16 13:35:57 -0400404 msm_host->src_clk = msm_clk_get(pdev, "src");
Archit Taneja4bfa9742015-10-09 16:32:38 +0530405 if (IS_ERR(msm_host->src_clk)) {
406 ret = PTR_ERR(msm_host->src_clk);
Rob Clarkdb9a3752017-10-16 13:35:57 -0400407 pr_err("%s: can't find src clock. ret=%d\n",
Archit Taneja4bfa9742015-10-09 16:32:38 +0530408 __func__, ret);
409 msm_host->src_clk = NULL;
410 goto exit;
411 }
412
413 msm_host->esc_clk_src = clk_get_parent(msm_host->esc_clk);
414 if (!msm_host->esc_clk_src) {
415 ret = -ENODEV;
Rob Clarkdb9a3752017-10-16 13:35:57 -0400416 pr_err("%s: can't get esc clock parent. ret=%d\n",
Archit Taneja4bfa9742015-10-09 16:32:38 +0530417 __func__, ret);
418 goto exit;
419 }
420
421 msm_host->dsi_clk_src = clk_get_parent(msm_host->src_clk);
422 if (!msm_host->dsi_clk_src) {
423 ret = -ENODEV;
Rob Clarkdb9a3752017-10-16 13:35:57 -0400424 pr_err("%s: can't get src clock parent. ret=%d\n",
Archit Taneja4bfa9742015-10-09 16:32:38 +0530425 __func__, ret);
426 }
427 }
Hai Lia6895542015-03-31 14:36:33 -0400428exit:
429 return ret;
430}
431
432static int dsi_bus_clk_enable(struct msm_dsi_host *msm_host)
433{
Archit Taneja6e0eb522015-10-09 15:21:12 +0530434 const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
435 int i, ret;
Hai Lia6895542015-03-31 14:36:33 -0400436
437 DBG("id=%d", msm_host->id);
438
Archit Taneja6e0eb522015-10-09 15:21:12 +0530439 for (i = 0; i < cfg->num_bus_clks; i++) {
440 ret = clk_prepare_enable(msm_host->bus_clks[i]);
441 if (ret) {
442 pr_err("%s: failed to enable bus clock %d ret %d\n",
443 __func__, i, ret);
444 goto err;
445 }
Hai Lia6895542015-03-31 14:36:33 -0400446 }
447
448 return 0;
Archit Taneja6e0eb522015-10-09 15:21:12 +0530449err:
450 for (; i > 0; i--)
451 clk_disable_unprepare(msm_host->bus_clks[i]);
Hai Lia6895542015-03-31 14:36:33 -0400452
Hai Lia6895542015-03-31 14:36:33 -0400453 return ret;
454}
455
456static void dsi_bus_clk_disable(struct msm_dsi_host *msm_host)
457{
Archit Taneja6e0eb522015-10-09 15:21:12 +0530458 const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
459 int i;
460
Hai Lia6895542015-03-31 14:36:33 -0400461 DBG("");
Archit Taneja6e0eb522015-10-09 15:21:12 +0530462
463 for (i = cfg->num_bus_clks - 1; i >= 0; i--)
464 clk_disable_unprepare(msm_host->bus_clks[i]);
Hai Lia6895542015-03-31 14:36:33 -0400465}
466
Archit Tanejaf54ca1a2017-07-28 16:17:04 +0530467int msm_dsi_runtime_suspend(struct device *dev)
468{
469 struct platform_device *pdev = to_platform_device(dev);
470 struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
471 struct mipi_dsi_host *host = msm_dsi->host;
472 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
473
474 if (!msm_host->cfg_hnd)
475 return 0;
476
477 dsi_bus_clk_disable(msm_host);
478
479 return 0;
480}
481
482int msm_dsi_runtime_resume(struct device *dev)
483{
484 struct platform_device *pdev = to_platform_device(dev);
485 struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
486 struct mipi_dsi_host *host = msm_dsi->host;
487 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
488
489 if (!msm_host->cfg_hnd)
490 return 0;
491
492 return dsi_bus_clk_enable(msm_host);
493}
494
Archit Taneja4bfa9742015-10-09 16:32:38 +0530495static int dsi_link_clk_enable_6g(struct msm_dsi_host *msm_host)
Hai Lia6895542015-03-31 14:36:33 -0400496{
497 int ret;
498
499 DBG("Set clk rates: pclk=%d, byteclk=%d",
500 msm_host->mode->clock, msm_host->byte_clk_rate);
501
502 ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
503 if (ret) {
504 pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
505 goto error;
506 }
507
508 ret = clk_set_rate(msm_host->pixel_clk, msm_host->mode->clock * 1000);
509 if (ret) {
510 pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
511 goto error;
512 }
513
Archit Tanejac1d97082018-01-17 15:04:44 +0530514 if (msm_host->byte_intf_clk) {
515 ret = clk_set_rate(msm_host->byte_intf_clk,
516 msm_host->byte_clk_rate / 2);
517 if (ret) {
518 pr_err("%s: Failed to set rate byte intf clk, %d\n",
519 __func__, ret);
520 goto error;
521 }
522 }
523
Hai Lia6895542015-03-31 14:36:33 -0400524 ret = clk_prepare_enable(msm_host->esc_clk);
525 if (ret) {
526 pr_err("%s: Failed to enable dsi esc clk\n", __func__);
527 goto error;
528 }
529
530 ret = clk_prepare_enable(msm_host->byte_clk);
531 if (ret) {
532 pr_err("%s: Failed to enable dsi byte clk\n", __func__);
533 goto byte_clk_err;
534 }
535
536 ret = clk_prepare_enable(msm_host->pixel_clk);
537 if (ret) {
538 pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
539 goto pixel_clk_err;
540 }
541
Archit Tanejac1d97082018-01-17 15:04:44 +0530542 if (msm_host->byte_intf_clk) {
543 ret = clk_prepare_enable(msm_host->byte_intf_clk);
544 if (ret) {
545 pr_err("%s: Failed to enable byte intf clk\n",
546 __func__);
547 goto byte_intf_clk_err;
548 }
549 }
550
Hai Lia6895542015-03-31 14:36:33 -0400551 return 0;
552
Archit Tanejac1d97082018-01-17 15:04:44 +0530553byte_intf_clk_err:
554 clk_disable_unprepare(msm_host->pixel_clk);
Hai Lia6895542015-03-31 14:36:33 -0400555pixel_clk_err:
556 clk_disable_unprepare(msm_host->byte_clk);
557byte_clk_err:
558 clk_disable_unprepare(msm_host->esc_clk);
559error:
560 return ret;
561}
562
Archit Taneja4bfa9742015-10-09 16:32:38 +0530563static int dsi_link_clk_enable_v2(struct msm_dsi_host *msm_host)
564{
565 int ret;
566
567 DBG("Set clk rates: pclk=%d, byteclk=%d, esc_clk=%d, dsi_src_clk=%d",
568 msm_host->mode->clock, msm_host->byte_clk_rate,
569 msm_host->esc_clk_rate, msm_host->src_clk_rate);
570
571 ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
572 if (ret) {
573 pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
574 goto error;
575 }
576
577 ret = clk_set_rate(msm_host->esc_clk, msm_host->esc_clk_rate);
578 if (ret) {
579 pr_err("%s: Failed to set rate esc clk, %d\n", __func__, ret);
580 goto error;
581 }
582
583 ret = clk_set_rate(msm_host->src_clk, msm_host->src_clk_rate);
584 if (ret) {
585 pr_err("%s: Failed to set rate src clk, %d\n", __func__, ret);
586 goto error;
587 }
588
589 ret = clk_set_rate(msm_host->pixel_clk, msm_host->mode->clock * 1000);
590 if (ret) {
591 pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
592 goto error;
593 }
594
595 ret = clk_prepare_enable(msm_host->byte_clk);
596 if (ret) {
597 pr_err("%s: Failed to enable dsi byte clk\n", __func__);
598 goto error;
599 }
600
601 ret = clk_prepare_enable(msm_host->esc_clk);
602 if (ret) {
603 pr_err("%s: Failed to enable dsi esc clk\n", __func__);
604 goto esc_clk_err;
605 }
606
607 ret = clk_prepare_enable(msm_host->src_clk);
608 if (ret) {
609 pr_err("%s: Failed to enable dsi src clk\n", __func__);
610 goto src_clk_err;
611 }
612
613 ret = clk_prepare_enable(msm_host->pixel_clk);
614 if (ret) {
615 pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
616 goto pixel_clk_err;
617 }
618
619 return 0;
620
621pixel_clk_err:
622 clk_disable_unprepare(msm_host->src_clk);
623src_clk_err:
624 clk_disable_unprepare(msm_host->esc_clk);
625esc_clk_err:
626 clk_disable_unprepare(msm_host->byte_clk);
627error:
628 return ret;
629}
630
631static int dsi_link_clk_enable(struct msm_dsi_host *msm_host)
632{
633 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
634
635 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G)
636 return dsi_link_clk_enable_6g(msm_host);
637 else
638 return dsi_link_clk_enable_v2(msm_host);
639}
640
Hai Lia6895542015-03-31 14:36:33 -0400641static void dsi_link_clk_disable(struct msm_dsi_host *msm_host)
642{
Archit Taneja4bfa9742015-10-09 16:32:38 +0530643 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
644
645 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
646 clk_disable_unprepare(msm_host->esc_clk);
647 clk_disable_unprepare(msm_host->pixel_clk);
Archit Tanejac1d97082018-01-17 15:04:44 +0530648 if (msm_host->byte_intf_clk)
649 clk_disable_unprepare(msm_host->byte_intf_clk);
Archit Taneja4bfa9742015-10-09 16:32:38 +0530650 clk_disable_unprepare(msm_host->byte_clk);
651 } else {
652 clk_disable_unprepare(msm_host->pixel_clk);
653 clk_disable_unprepare(msm_host->src_clk);
654 clk_disable_unprepare(msm_host->esc_clk);
655 clk_disable_unprepare(msm_host->byte_clk);
656 }
Hai Lia6895542015-03-31 14:36:33 -0400657}
658
Hai Lia6895542015-03-31 14:36:33 -0400659static int dsi_calc_clk_rate(struct msm_dsi_host *msm_host)
660{
661 struct drm_display_mode *mode = msm_host->mode;
Archit Taneja4bfa9742015-10-09 16:32:38 +0530662 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400663 u8 lanes = msm_host->lanes;
664 u32 bpp = dsi_get_bpp(msm_host->format);
665 u32 pclk_rate;
666
667 if (!mode) {
668 pr_err("%s: mode not set\n", __func__);
669 return -EINVAL;
670 }
671
672 pclk_rate = mode->clock * 1000;
673 if (lanes > 0) {
674 msm_host->byte_clk_rate = (pclk_rate * bpp) / (8 * lanes);
675 } else {
676 pr_err("%s: forcing mdss_dsi lanes to 1\n", __func__);
677 msm_host->byte_clk_rate = (pclk_rate * bpp) / 8;
678 }
679
680 DBG("pclk=%d, bclk=%d", pclk_rate, msm_host->byte_clk_rate);
681
Archit Taneja4bfa9742015-10-09 16:32:38 +0530682 msm_host->esc_clk_rate = clk_get_rate(msm_host->esc_clk);
683
684 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
685 unsigned int esc_mhz, esc_div;
686 unsigned long byte_mhz;
687
688 msm_host->src_clk_rate = (pclk_rate * bpp) / 8;
689
690 /*
691 * esc clock is byte clock followed by a 4 bit divider,
692 * we need to find an escape clock frequency within the
693 * mipi DSI spec range within the maximum divider limit
694 * We iterate here between an escape clock frequencey
695 * between 20 Mhz to 5 Mhz and pick up the first one
696 * that can be supported by our divider
697 */
698
699 byte_mhz = msm_host->byte_clk_rate / 1000000;
700
701 for (esc_mhz = 20; esc_mhz >= 5; esc_mhz--) {
702 esc_div = DIV_ROUND_UP(byte_mhz, esc_mhz);
703
704 /*
705 * TODO: Ideally, we shouldn't know what sort of divider
706 * is available in mmss_cc, we're just assuming that
707 * it'll always be a 4 bit divider. Need to come up with
708 * a better way here.
709 */
710 if (esc_div >= 1 && esc_div <= 16)
711 break;
712 }
713
714 if (esc_mhz < 5)
715 return -EINVAL;
716
717 msm_host->esc_clk_rate = msm_host->byte_clk_rate / esc_div;
718
719 DBG("esc=%d, src=%d", msm_host->esc_clk_rate,
720 msm_host->src_clk_rate);
721 }
722
Hai Lia6895542015-03-31 14:36:33 -0400723 return 0;
724}
725
Hai Lia6895542015-03-31 14:36:33 -0400726static void dsi_intr_ctrl(struct msm_dsi_host *msm_host, u32 mask, int enable)
727{
728 u32 intr;
729 unsigned long flags;
730
731 spin_lock_irqsave(&msm_host->intr_lock, flags);
732 intr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
733
734 if (enable)
735 intr |= mask;
736 else
737 intr &= ~mask;
738
739 DBG("intr=%x enable=%d", intr, enable);
740
741 dsi_write(msm_host, REG_DSI_INTR_CTRL, intr);
742 spin_unlock_irqrestore(&msm_host->intr_lock, flags);
743}
744
745static inline enum dsi_traffic_mode dsi_get_traffic_mode(const u32 mode_flags)
746{
747 if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
748 return BURST_MODE;
749 else if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
750 return NON_BURST_SYNCH_PULSE;
751
752 return NON_BURST_SYNCH_EVENT;
753}
754
755static inline enum dsi_vid_dst_format dsi_get_vid_fmt(
756 const enum mipi_dsi_pixel_format mipi_fmt)
757{
758 switch (mipi_fmt) {
759 case MIPI_DSI_FMT_RGB888: return VID_DST_FORMAT_RGB888;
760 case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666_LOOSE;
761 case MIPI_DSI_FMT_RGB666_PACKED: return VID_DST_FORMAT_RGB666;
762 case MIPI_DSI_FMT_RGB565: return VID_DST_FORMAT_RGB565;
763 default: return VID_DST_FORMAT_RGB888;
764 }
765}
766
767static inline enum dsi_cmd_dst_format dsi_get_cmd_fmt(
768 const enum mipi_dsi_pixel_format mipi_fmt)
769{
770 switch (mipi_fmt) {
771 case MIPI_DSI_FMT_RGB888: return CMD_DST_FORMAT_RGB888;
772 case MIPI_DSI_FMT_RGB666_PACKED:
773 case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666;
774 case MIPI_DSI_FMT_RGB565: return CMD_DST_FORMAT_RGB565;
775 default: return CMD_DST_FORMAT_RGB888;
776 }
777}
778
779static void dsi_ctrl_config(struct msm_dsi_host *msm_host, bool enable,
Hai Lidceac342016-09-15 14:34:49 +0530780 struct msm_dsi_phy_shared_timings *phy_shared_timings)
Hai Lia6895542015-03-31 14:36:33 -0400781{
782 u32 flags = msm_host->mode_flags;
783 enum mipi_dsi_pixel_format mipi_fmt = msm_host->format;
Hai Lid248b612015-08-13 17:49:29 -0400784 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -0400785 u32 data = 0;
786
787 if (!enable) {
788 dsi_write(msm_host, REG_DSI_CTRL, 0);
789 return;
790 }
791
792 if (flags & MIPI_DSI_MODE_VIDEO) {
793 if (flags & MIPI_DSI_MODE_VIDEO_HSE)
794 data |= DSI_VID_CFG0_PULSE_MODE_HSA_HE;
795 if (flags & MIPI_DSI_MODE_VIDEO_HFP)
796 data |= DSI_VID_CFG0_HFP_POWER_STOP;
797 if (flags & MIPI_DSI_MODE_VIDEO_HBP)
798 data |= DSI_VID_CFG0_HBP_POWER_STOP;
799 if (flags & MIPI_DSI_MODE_VIDEO_HSA)
800 data |= DSI_VID_CFG0_HSA_POWER_STOP;
801 /* Always set low power stop mode for BLLP
802 * to let command engine send packets
803 */
804 data |= DSI_VID_CFG0_EOF_BLLP_POWER_STOP |
805 DSI_VID_CFG0_BLLP_POWER_STOP;
806 data |= DSI_VID_CFG0_TRAFFIC_MODE(dsi_get_traffic_mode(flags));
807 data |= DSI_VID_CFG0_DST_FORMAT(dsi_get_vid_fmt(mipi_fmt));
808 data |= DSI_VID_CFG0_VIRT_CHANNEL(msm_host->channel);
809 dsi_write(msm_host, REG_DSI_VID_CFG0, data);
810
811 /* Do not swap RGB colors */
812 data = DSI_VID_CFG1_RGB_SWAP(SWAP_RGB);
813 dsi_write(msm_host, REG_DSI_VID_CFG1, 0);
814 } else {
815 /* Do not swap RGB colors */
816 data = DSI_CMD_CFG0_RGB_SWAP(SWAP_RGB);
817 data |= DSI_CMD_CFG0_DST_FORMAT(dsi_get_cmd_fmt(mipi_fmt));
818 dsi_write(msm_host, REG_DSI_CMD_CFG0, data);
819
820 data = DSI_CMD_CFG1_WR_MEM_START(MIPI_DCS_WRITE_MEMORY_START) |
821 DSI_CMD_CFG1_WR_MEM_CONTINUE(
822 MIPI_DCS_WRITE_MEMORY_CONTINUE);
823 /* Always insert DCS command */
824 data |= DSI_CMD_CFG1_INSERT_DCS_COMMAND;
825 dsi_write(msm_host, REG_DSI_CMD_CFG1, data);
826 }
827
828 dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL,
829 DSI_CMD_DMA_CTRL_FROM_FRAME_BUFFER |
830 DSI_CMD_DMA_CTRL_LOW_POWER);
831
832 data = 0;
833 /* Always assume dedicated TE pin */
834 data |= DSI_TRIG_CTRL_TE;
835 data |= DSI_TRIG_CTRL_MDP_TRIGGER(TRIGGER_NONE);
836 data |= DSI_TRIG_CTRL_DMA_TRIGGER(TRIGGER_SW);
837 data |= DSI_TRIG_CTRL_STREAM(msm_host->channel);
Hai Lid248b612015-08-13 17:49:29 -0400838 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
839 (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_2))
Hai Lia6895542015-03-31 14:36:33 -0400840 data |= DSI_TRIG_CTRL_BLOCK_DMA_WITHIN_FRAME;
841 dsi_write(msm_host, REG_DSI_TRIG_CTRL, data);
842
Hai Lidceac342016-09-15 14:34:49 +0530843 data = DSI_CLKOUT_TIMING_CTRL_T_CLK_POST(phy_shared_timings->clk_post) |
844 DSI_CLKOUT_TIMING_CTRL_T_CLK_PRE(phy_shared_timings->clk_pre);
Hai Lia6895542015-03-31 14:36:33 -0400845 dsi_write(msm_host, REG_DSI_CLKOUT_TIMING_CTRL, data);
846
Hai Lidceac342016-09-15 14:34:49 +0530847 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
848 (cfg_hnd->minor > MSM_DSI_6G_VER_MINOR_V1_0) &&
849 phy_shared_timings->clk_pre_inc_by_2)
850 dsi_write(msm_host, REG_DSI_T_CLK_PRE_EXTEND,
851 DSI_T_CLK_PRE_EXTEND_INC_BY_2_BYTECLK);
852
Hai Lia6895542015-03-31 14:36:33 -0400853 data = 0;
854 if (!(flags & MIPI_DSI_MODE_EOT_PACKET))
855 data |= DSI_EOT_PACKET_CTRL_TX_EOT_APPEND;
856 dsi_write(msm_host, REG_DSI_EOT_PACKET_CTRL, data);
857
858 /* allow only ack-err-status to generate interrupt */
859 dsi_write(msm_host, REG_DSI_ERR_INT_MASK0, 0x13ff3fe0);
860
861 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
862
863 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
864
865 data = DSI_CTRL_CLK_EN;
866
867 DBG("lane number=%d", msm_host->lanes);
Archit Taneja26f7d1f2016-02-25 11:19:48 +0530868 data |= ((DSI_CTRL_LANE0 << msm_host->lanes) - DSI_CTRL_LANE0);
869
870 dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
871 DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(msm_host->dlane_swap));
Archit Taneja65c5e542015-04-08 11:37:40 +0530872
873 if (!(flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
874 dsi_write(msm_host, REG_DSI_LANE_CTRL,
875 DSI_LANE_CTRL_CLKLN_HS_FORCE_REQUEST);
876
Hai Lia6895542015-03-31 14:36:33 -0400877 data |= DSI_CTRL_ENABLE;
878
879 dsi_write(msm_host, REG_DSI_CTRL, data);
880}
881
882static void dsi_timing_setup(struct msm_dsi_host *msm_host)
883{
884 struct drm_display_mode *mode = msm_host->mode;
885 u32 hs_start = 0, vs_start = 0; /* take sync start as 0 */
886 u32 h_total = mode->htotal;
887 u32 v_total = mode->vtotal;
888 u32 hs_end = mode->hsync_end - mode->hsync_start;
889 u32 vs_end = mode->vsync_end - mode->vsync_start;
890 u32 ha_start = h_total - mode->hsync_start;
891 u32 ha_end = ha_start + mode->hdisplay;
892 u32 va_start = v_total - mode->vsync_start;
893 u32 va_end = va_start + mode->vdisplay;
894 u32 wc;
895
896 DBG("");
897
898 if (msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) {
899 dsi_write(msm_host, REG_DSI_ACTIVE_H,
900 DSI_ACTIVE_H_START(ha_start) |
901 DSI_ACTIVE_H_END(ha_end));
902 dsi_write(msm_host, REG_DSI_ACTIVE_V,
903 DSI_ACTIVE_V_START(va_start) |
904 DSI_ACTIVE_V_END(va_end));
905 dsi_write(msm_host, REG_DSI_TOTAL,
906 DSI_TOTAL_H_TOTAL(h_total - 1) |
907 DSI_TOTAL_V_TOTAL(v_total - 1));
908
909 dsi_write(msm_host, REG_DSI_ACTIVE_HSYNC,
910 DSI_ACTIVE_HSYNC_START(hs_start) |
911 DSI_ACTIVE_HSYNC_END(hs_end));
912 dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_HPOS, 0);
913 dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_VPOS,
914 DSI_ACTIVE_VSYNC_VPOS_START(vs_start) |
915 DSI_ACTIVE_VSYNC_VPOS_END(vs_end));
916 } else { /* command mode */
917 /* image data and 1 byte write_memory_start cmd */
918 wc = mode->hdisplay * dsi_get_bpp(msm_host->format) / 8 + 1;
919
920 dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_CTRL,
921 DSI_CMD_MDP_STREAM_CTRL_WORD_COUNT(wc) |
922 DSI_CMD_MDP_STREAM_CTRL_VIRTUAL_CHANNEL(
923 msm_host->channel) |
924 DSI_CMD_MDP_STREAM_CTRL_DATA_TYPE(
925 MIPI_DSI_DCS_LONG_WRITE));
926
927 dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_TOTAL,
928 DSI_CMD_MDP_STREAM_TOTAL_H_TOTAL(mode->hdisplay) |
929 DSI_CMD_MDP_STREAM_TOTAL_V_TOTAL(mode->vdisplay));
930 }
931}
932
933static void dsi_sw_reset(struct msm_dsi_host *msm_host)
934{
935 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
936 wmb(); /* clocks need to be enabled before reset */
937
938 dsi_write(msm_host, REG_DSI_RESET, 1);
939 wmb(); /* make sure reset happen */
940 dsi_write(msm_host, REG_DSI_RESET, 0);
941}
942
943static void dsi_op_mode_config(struct msm_dsi_host *msm_host,
944 bool video_mode, bool enable)
945{
946 u32 dsi_ctrl;
947
948 dsi_ctrl = dsi_read(msm_host, REG_DSI_CTRL);
949
950 if (!enable) {
951 dsi_ctrl &= ~(DSI_CTRL_ENABLE | DSI_CTRL_VID_MODE_EN |
952 DSI_CTRL_CMD_MODE_EN);
953 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE |
954 DSI_IRQ_MASK_VIDEO_DONE, 0);
955 } else {
956 if (video_mode) {
957 dsi_ctrl |= DSI_CTRL_VID_MODE_EN;
958 } else { /* command mode */
959 dsi_ctrl |= DSI_CTRL_CMD_MODE_EN;
960 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE, 1);
961 }
962 dsi_ctrl |= DSI_CTRL_ENABLE;
963 }
964
965 dsi_write(msm_host, REG_DSI_CTRL, dsi_ctrl);
966}
967
968static void dsi_set_tx_power_mode(int mode, struct msm_dsi_host *msm_host)
969{
970 u32 data;
971
972 data = dsi_read(msm_host, REG_DSI_CMD_DMA_CTRL);
973
974 if (mode == 0)
975 data &= ~DSI_CMD_DMA_CTRL_LOW_POWER;
976 else
977 data |= DSI_CMD_DMA_CTRL_LOW_POWER;
978
979 dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL, data);
980}
981
982static void dsi_wait4video_done(struct msm_dsi_host *msm_host)
983{
984 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 1);
985
986 reinit_completion(&msm_host->video_comp);
987
988 wait_for_completion_timeout(&msm_host->video_comp,
989 msecs_to_jiffies(70));
990
991 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 0);
992}
993
994static void dsi_wait4video_eng_busy(struct msm_dsi_host *msm_host)
995{
996 if (!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO))
997 return;
998
999 if (msm_host->power_on) {
1000 dsi_wait4video_done(msm_host);
1001 /* delay 4 ms to skip BLLP */
1002 usleep_range(2000, 4000);
1003 }
1004}
1005
1006/* dsi_cmd */
1007static int dsi_tx_buf_alloc(struct msm_dsi_host *msm_host, int size)
1008{
1009 struct drm_device *dev = msm_host->dev;
Rob Clarkf59f62d2017-06-13 10:22:37 -04001010 struct msm_drm_private *priv = dev->dev_private;
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301011 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -04001012 int ret;
Rob Clark78babc12016-11-11 12:06:46 -05001013 uint64_t iova;
Hai Lia6895542015-03-31 14:36:33 -04001014
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301015 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301016 msm_host->tx_gem_obj = msm_gem_new(dev, size, MSM_BO_UNCACHED);
1017 if (IS_ERR(msm_host->tx_gem_obj)) {
1018 ret = PTR_ERR(msm_host->tx_gem_obj);
1019 pr_err("%s: failed to allocate gem, %d\n",
1020 __func__, ret);
1021 msm_host->tx_gem_obj = NULL;
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301022 return ret;
1023 }
1024
Sushmita Susheelendra0e082702017-06-13 16:52:54 -06001025 ret = msm_gem_get_iova(msm_host->tx_gem_obj,
Rob Clark8bdcd942017-06-13 11:07:08 -04001026 priv->kms->aspace, &iova);
saurabhbeb107f2015-12-07 01:19:21 +05301027 mutex_unlock(&dev->struct_mutex);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301028 if (ret) {
1029 pr_err("%s: failed to get iova, %d\n", __func__, ret);
1030 return ret;
1031 }
Hai Lia6895542015-03-31 14:36:33 -04001032
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301033 if (iova & 0x07) {
1034 pr_err("%s: buf NOT 8 bytes aligned\n", __func__);
1035 return -EINVAL;
1036 }
Hai Lia6895542015-03-31 14:36:33 -04001037
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301038 msm_host->tx_size = msm_host->tx_gem_obj->size;
1039 } else {
1040 msm_host->tx_buf = dma_alloc_coherent(dev->dev, size,
1041 &msm_host->tx_buf_paddr, GFP_KERNEL);
1042 if (!msm_host->tx_buf) {
1043 ret = -ENOMEM;
1044 pr_err("%s: failed to allocate tx buf, %d\n",
1045 __func__, ret);
1046 return ret;
1047 }
1048
1049 msm_host->tx_size = size;
Hai Lia6895542015-03-31 14:36:33 -04001050 }
1051
1052 return 0;
1053}
1054
1055static void dsi_tx_buf_free(struct msm_dsi_host *msm_host)
1056{
1057 struct drm_device *dev = msm_host->dev;
1058
1059 if (msm_host->tx_gem_obj) {
1060 msm_gem_put_iova(msm_host->tx_gem_obj, 0);
1061 mutex_lock(&dev->struct_mutex);
1062 msm_gem_free_object(msm_host->tx_gem_obj);
1063 msm_host->tx_gem_obj = NULL;
1064 mutex_unlock(&dev->struct_mutex);
1065 }
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301066
1067 if (msm_host->tx_buf)
1068 dma_free_coherent(dev->dev, msm_host->tx_size, msm_host->tx_buf,
1069 msm_host->tx_buf_paddr);
Hai Lia6895542015-03-31 14:36:33 -04001070}
1071
1072/*
1073 * prepare cmd buffer to be txed
1074 */
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301075static int dsi_cmd_dma_add(struct msm_dsi_host *msm_host,
1076 const struct mipi_dsi_msg *msg)
Hai Lia6895542015-03-31 14:36:33 -04001077{
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301078 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -04001079 struct mipi_dsi_packet packet;
1080 int len;
1081 int ret;
1082 u8 *data;
1083
1084 ret = mipi_dsi_create_packet(&packet, msg);
1085 if (ret) {
1086 pr_err("%s: create packet failed, %d\n", __func__, ret);
1087 return ret;
1088 }
1089 len = (packet.size + 3) & (~0x3);
1090
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301091 if (len > msm_host->tx_size) {
Hai Lia6895542015-03-31 14:36:33 -04001092 pr_err("%s: packet size is too big\n", __func__);
1093 return -EINVAL;
1094 }
1095
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301096 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
Rob Clark18f23042016-05-26 16:24:35 -04001097 data = msm_gem_get_vaddr(msm_host->tx_gem_obj);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301098 if (IS_ERR(data)) {
1099 ret = PTR_ERR(data);
1100 pr_err("%s: get vaddr failed, %d\n", __func__, ret);
1101 return ret;
1102 }
1103 } else {
1104 data = msm_host->tx_buf;
Hai Lia6895542015-03-31 14:36:33 -04001105 }
1106
1107 /* MSM specific command format in memory */
1108 data[0] = packet.header[1];
1109 data[1] = packet.header[2];
1110 data[2] = packet.header[0];
1111 data[3] = BIT(7); /* Last packet */
1112 if (mipi_dsi_packet_format_is_long(msg->type))
1113 data[3] |= BIT(6);
1114 if (msg->rx_buf && msg->rx_len)
1115 data[3] |= BIT(5);
1116
1117 /* Long packet */
1118 if (packet.payload && packet.payload_length)
1119 memcpy(data + 4, packet.payload, packet.payload_length);
1120
1121 /* Append 0xff to the end */
1122 if (packet.size < len)
1123 memset(data + packet.size, 0xff, len - packet.size);
1124
Rob Clark18f23042016-05-26 16:24:35 -04001125 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G)
1126 msm_gem_put_vaddr(msm_host->tx_gem_obj);
1127
Hai Lia6895542015-03-31 14:36:33 -04001128 return len;
1129}
1130
1131/*
1132 * dsi_short_read1_resp: 1 parameter
1133 */
1134static int dsi_short_read1_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1135{
1136 u8 *data = msg->rx_buf;
1137 if (data && (msg->rx_len >= 1)) {
1138 *data = buf[1]; /* strip out dcs type */
1139 return 1;
1140 } else {
Stephane Viau981371f2015-04-30 10:39:26 -04001141 pr_err("%s: read data does not match with rx_buf len %zu\n",
Hai Lia6895542015-03-31 14:36:33 -04001142 __func__, msg->rx_len);
1143 return -EINVAL;
1144 }
1145}
1146
1147/*
1148 * dsi_short_read2_resp: 2 parameter
1149 */
1150static int dsi_short_read2_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1151{
1152 u8 *data = msg->rx_buf;
1153 if (data && (msg->rx_len >= 2)) {
1154 data[0] = buf[1]; /* strip out dcs type */
1155 data[1] = buf[2];
1156 return 2;
1157 } else {
Stephane Viau981371f2015-04-30 10:39:26 -04001158 pr_err("%s: read data does not match with rx_buf len %zu\n",
Hai Lia6895542015-03-31 14:36:33 -04001159 __func__, msg->rx_len);
1160 return -EINVAL;
1161 }
1162}
1163
1164static int dsi_long_read_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1165{
1166 /* strip out 4 byte dcs header */
1167 if (msg->rx_buf && msg->rx_len)
1168 memcpy(msg->rx_buf, buf + 4, msg->rx_len);
1169
1170 return msg->rx_len;
1171}
1172
Hai Lia6895542015-03-31 14:36:33 -04001173static int dsi_cmd_dma_tx(struct msm_dsi_host *msm_host, int len)
1174{
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301175 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Rob Clarkf59f62d2017-06-13 10:22:37 -04001176 struct drm_device *dev = msm_host->dev;
1177 struct msm_drm_private *priv = dev->dev_private;
Hai Lia6895542015-03-31 14:36:33 -04001178 int ret;
Rob Clark78babc12016-11-11 12:06:46 -05001179 uint64_t dma_base;
Hai Lia6895542015-03-31 14:36:33 -04001180 bool triggered;
1181
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301182 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
Rob Clarkf59f62d2017-06-13 10:22:37 -04001183 ret = msm_gem_get_iova(msm_host->tx_gem_obj,
Rob Clark8bdcd942017-06-13 11:07:08 -04001184 priv->kms->aspace, &dma_base);
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301185 if (ret) {
1186 pr_err("%s: failed to get iova: %d\n", __func__, ret);
1187 return ret;
1188 }
1189 } else {
1190 dma_base = msm_host->tx_buf_paddr;
Hai Lia6895542015-03-31 14:36:33 -04001191 }
1192
1193 reinit_completion(&msm_host->dma_comp);
1194
1195 dsi_wait4video_eng_busy(msm_host);
1196
1197 triggered = msm_dsi_manager_cmd_xfer_trigger(
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301198 msm_host->id, dma_base, len);
Hai Lia6895542015-03-31 14:36:33 -04001199 if (triggered) {
1200 ret = wait_for_completion_timeout(&msm_host->dma_comp,
1201 msecs_to_jiffies(200));
1202 DBG("ret=%d", ret);
1203 if (ret == 0)
1204 ret = -ETIMEDOUT;
1205 else
1206 ret = len;
1207 } else
1208 ret = len;
1209
1210 return ret;
1211}
1212
1213static int dsi_cmd_dma_rx(struct msm_dsi_host *msm_host,
1214 u8 *buf, int rx_byte, int pkt_size)
1215{
1216 u32 *lp, *temp, data;
1217 int i, j = 0, cnt;
Hai Lia6895542015-03-31 14:36:33 -04001218 u32 read_cnt;
1219 u8 reg[16];
1220 int repeated_bytes = 0;
1221 int buf_offset = buf - msm_host->rx_buf;
1222
1223 lp = (u32 *)buf;
1224 temp = (u32 *)reg;
1225 cnt = (rx_byte + 3) >> 2;
1226 if (cnt > 4)
1227 cnt = 4; /* 4 x 32 bits registers only */
1228
Hai Liec1936e2015-04-29 11:39:00 -04001229 if (rx_byte == 4)
1230 read_cnt = 4;
1231 else
1232 read_cnt = pkt_size + 6;
Hai Lia6895542015-03-31 14:36:33 -04001233
1234 /*
1235 * In case of multiple reads from the panel, after the first read, there
1236 * is possibility that there are some bytes in the payload repeating in
1237 * the RDBK_DATA registers. Since we read all the parameters from the
1238 * panel right from the first byte for every pass. We need to skip the
1239 * repeating bytes and then append the new parameters to the rx buffer.
1240 */
1241 if (read_cnt > 16) {
1242 int bytes_shifted;
1243 /* Any data more than 16 bytes will be shifted out.
1244 * The temp read buffer should already contain these bytes.
1245 * The remaining bytes in read buffer are the repeated bytes.
1246 */
1247 bytes_shifted = read_cnt - 16;
1248 repeated_bytes = buf_offset - bytes_shifted;
1249 }
1250
1251 for (i = cnt - 1; i >= 0; i--) {
1252 data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
1253 *temp++ = ntohl(data); /* to host byte order */
1254 DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
1255 }
1256
1257 for (i = repeated_bytes; i < 16; i++)
1258 buf[j++] = reg[i];
1259
1260 return j;
1261}
1262
1263static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
1264 const struct mipi_dsi_msg *msg)
1265{
1266 int len, ret;
1267 int bllp_len = msm_host->mode->hdisplay *
1268 dsi_get_bpp(msm_host->format) / 8;
1269
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05301270 len = dsi_cmd_dma_add(msm_host, msg);
Hai Lia6895542015-03-31 14:36:33 -04001271 if (!len) {
1272 pr_err("%s: failed to add cmd type = 0x%x\n",
1273 __func__, msg->type);
1274 return -EINVAL;
1275 }
1276
1277 /* for video mode, do not send cmds more than
1278 * one pixel line, since it only transmit it
1279 * during BLLP.
1280 */
1281 /* TODO: if the command is sent in LP mode, the bit rate is only
1282 * half of esc clk rate. In this case, if the video is already
1283 * actively streaming, we need to check more carefully if the
1284 * command can be fit into one BLLP.
1285 */
1286 if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
1287 pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
1288 __func__, len);
1289 return -EINVAL;
1290 }
1291
1292 ret = dsi_cmd_dma_tx(msm_host, len);
1293 if (ret < len) {
1294 pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d\n",
1295 __func__, msg->type, (*(u8 *)(msg->tx_buf)), len);
1296 return -ECOMM;
1297 }
1298
1299 return len;
1300}
1301
1302static void dsi_sw_reset_restore(struct msm_dsi_host *msm_host)
1303{
1304 u32 data0, data1;
1305
1306 data0 = dsi_read(msm_host, REG_DSI_CTRL);
1307 data1 = data0;
1308 data1 &= ~DSI_CTRL_ENABLE;
1309 dsi_write(msm_host, REG_DSI_CTRL, data1);
1310 /*
1311 * dsi controller need to be disabled before
1312 * clocks turned on
1313 */
1314 wmb();
1315
1316 dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
1317 wmb(); /* make sure clocks enabled */
1318
1319 /* dsi controller can only be reset while clocks are running */
1320 dsi_write(msm_host, REG_DSI_RESET, 1);
1321 wmb(); /* make sure reset happen */
1322 dsi_write(msm_host, REG_DSI_RESET, 0);
1323 wmb(); /* controller out of reset */
1324 dsi_write(msm_host, REG_DSI_CTRL, data0);
1325 wmb(); /* make sure dsi controller enabled again */
1326}
1327
Archit Taneja8d23ea42016-10-25 12:17:59 +05301328static void dsi_hpd_worker(struct work_struct *work)
1329{
1330 struct msm_dsi_host *msm_host =
1331 container_of(work, struct msm_dsi_host, hpd_work);
1332
1333 drm_helper_hpd_irq_event(msm_host->dev);
1334}
1335
Hai Lia6895542015-03-31 14:36:33 -04001336static void dsi_err_worker(struct work_struct *work)
1337{
1338 struct msm_dsi_host *msm_host =
1339 container_of(work, struct msm_dsi_host, err_work);
1340 u32 status = msm_host->err_work_state;
1341
Rob Clarkff431fa2015-05-07 15:19:02 -04001342 pr_err_ratelimited("%s: status=%x\n", __func__, status);
Hai Lia6895542015-03-31 14:36:33 -04001343 if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
1344 dsi_sw_reset_restore(msm_host);
1345
1346 /* It is safe to clear here because error irq is disabled. */
1347 msm_host->err_work_state = 0;
1348
1349 /* enable dsi error interrupt */
1350 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
1351}
1352
1353static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
1354{
1355 u32 status;
1356
1357 status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
1358
1359 if (status) {
1360 dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
1361 /* Writing of an extra 0 needed to clear error bits */
1362 dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
1363 msm_host->err_work_state |= DSI_ERR_STATE_ACK;
1364 }
1365}
1366
1367static void dsi_timeout_status(struct msm_dsi_host *msm_host)
1368{
1369 u32 status;
1370
1371 status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
1372
1373 if (status) {
1374 dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
1375 msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
1376 }
1377}
1378
1379static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
1380{
1381 u32 status;
1382
1383 status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
1384
Archit Taneja01199362015-06-25 11:29:24 +05301385 if (status & (DSI_DLN0_PHY_ERR_DLN0_ERR_ESC |
1386 DSI_DLN0_PHY_ERR_DLN0_ERR_SYNC_ESC |
1387 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTROL |
1388 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP0 |
1389 DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP1)) {
Hai Lia6895542015-03-31 14:36:33 -04001390 dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
1391 msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
1392 }
1393}
1394
1395static void dsi_fifo_status(struct msm_dsi_host *msm_host)
1396{
1397 u32 status;
1398
1399 status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
1400
1401 /* fifo underflow, overflow */
1402 if (status) {
1403 dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
1404 msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
1405 if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
1406 msm_host->err_work_state |=
1407 DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
1408 }
1409}
1410
1411static void dsi_status(struct msm_dsi_host *msm_host)
1412{
1413 u32 status;
1414
1415 status = dsi_read(msm_host, REG_DSI_STATUS0);
1416
1417 if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
1418 dsi_write(msm_host, REG_DSI_STATUS0, status);
1419 msm_host->err_work_state |=
1420 DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
1421 }
1422}
1423
1424static void dsi_clk_status(struct msm_dsi_host *msm_host)
1425{
1426 u32 status;
1427
1428 status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
1429
1430 if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
1431 dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
1432 msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
1433 }
1434}
1435
1436static void dsi_error(struct msm_dsi_host *msm_host)
1437{
1438 /* disable dsi error interrupt */
1439 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
1440
1441 dsi_clk_status(msm_host);
1442 dsi_fifo_status(msm_host);
1443 dsi_ack_err_status(msm_host);
1444 dsi_timeout_status(msm_host);
1445 dsi_status(msm_host);
1446 dsi_dln0_phy_err(msm_host);
1447
1448 queue_work(msm_host->workqueue, &msm_host->err_work);
1449}
1450
1451static irqreturn_t dsi_host_irq(int irq, void *ptr)
1452{
1453 struct msm_dsi_host *msm_host = ptr;
1454 u32 isr;
1455 unsigned long flags;
1456
1457 if (!msm_host->ctrl_base)
1458 return IRQ_HANDLED;
1459
1460 spin_lock_irqsave(&msm_host->intr_lock, flags);
1461 isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
1462 dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
1463 spin_unlock_irqrestore(&msm_host->intr_lock, flags);
1464
1465 DBG("isr=0x%x, id=%d", isr, msm_host->id);
1466
1467 if (isr & DSI_IRQ_ERROR)
1468 dsi_error(msm_host);
1469
1470 if (isr & DSI_IRQ_VIDEO_DONE)
1471 complete(&msm_host->video_comp);
1472
1473 if (isr & DSI_IRQ_CMD_DMA_DONE)
1474 complete(&msm_host->dma_comp);
1475
1476 return IRQ_HANDLED;
1477}
1478
1479static int dsi_host_init_panel_gpios(struct msm_dsi_host *msm_host,
1480 struct device *panel_device)
1481{
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001482 msm_host->disp_en_gpio = devm_gpiod_get_optional(panel_device,
1483 "disp-enable",
1484 GPIOD_OUT_LOW);
Hai Lia6895542015-03-31 14:36:33 -04001485 if (IS_ERR(msm_host->disp_en_gpio)) {
1486 DBG("cannot get disp-enable-gpios %ld",
1487 PTR_ERR(msm_host->disp_en_gpio));
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001488 return PTR_ERR(msm_host->disp_en_gpio);
Hai Lia6895542015-03-31 14:36:33 -04001489 }
1490
Archit Taneja60d05cb2015-06-25 14:36:35 +05301491 msm_host->te_gpio = devm_gpiod_get_optional(panel_device, "disp-te",
1492 GPIOD_IN);
Hai Lia6895542015-03-31 14:36:33 -04001493 if (IS_ERR(msm_host->te_gpio)) {
1494 DBG("cannot get disp-te-gpios %ld", PTR_ERR(msm_host->te_gpio));
Uwe Kleine-König9590e692015-05-20 09:21:41 +02001495 return PTR_ERR(msm_host->te_gpio);
Hai Lia6895542015-03-31 14:36:33 -04001496 }
1497
1498 return 0;
1499}
1500
1501static int dsi_host_attach(struct mipi_dsi_host *host,
1502 struct mipi_dsi_device *dsi)
1503{
1504 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1505 int ret;
1506
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301507 if (dsi->lanes > msm_host->num_data_lanes)
1508 return -EINVAL;
1509
Hai Lia6895542015-03-31 14:36:33 -04001510 msm_host->channel = dsi->channel;
1511 msm_host->lanes = dsi->lanes;
1512 msm_host->format = dsi->format;
1513 msm_host->mode_flags = dsi->mode_flags;
1514
Archit Taneja9c9f6f82016-12-05 15:24:53 +05301515 msm_dsi_manager_attach_dsi_device(msm_host->id, dsi->mode_flags);
1516
Hai Lia6895542015-03-31 14:36:33 -04001517 /* Some gpios defined in panel DT need to be controlled by host */
1518 ret = dsi_host_init_panel_gpios(msm_host, &dsi->dev);
1519 if (ret)
1520 return ret;
1521
1522 DBG("id=%d", msm_host->id);
1523 if (msm_host->dev)
Archit Taneja8d23ea42016-10-25 12:17:59 +05301524 queue_work(msm_host->workqueue, &msm_host->hpd_work);
Hai Lia6895542015-03-31 14:36:33 -04001525
1526 return 0;
1527}
1528
1529static int dsi_host_detach(struct mipi_dsi_host *host,
1530 struct mipi_dsi_device *dsi)
1531{
1532 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1533
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301534 msm_host->device_node = NULL;
Hai Lia6895542015-03-31 14:36:33 -04001535
1536 DBG("id=%d", msm_host->id);
1537 if (msm_host->dev)
Archit Taneja8d23ea42016-10-25 12:17:59 +05301538 queue_work(msm_host->workqueue, &msm_host->hpd_work);
Hai Lia6895542015-03-31 14:36:33 -04001539
1540 return 0;
1541}
1542
1543static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
1544 const struct mipi_dsi_msg *msg)
1545{
1546 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1547 int ret;
1548
1549 if (!msg || !msm_host->power_on)
1550 return -EINVAL;
1551
1552 mutex_lock(&msm_host->cmd_mutex);
1553 ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
1554 mutex_unlock(&msm_host->cmd_mutex);
1555
1556 return ret;
1557}
1558
1559static struct mipi_dsi_host_ops dsi_host_ops = {
1560 .attach = dsi_host_attach,
1561 .detach = dsi_host_detach,
1562 .transfer = dsi_host_transfer,
1563};
1564
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301565/*
1566 * List of supported physical to logical lane mappings.
1567 * For example, the 2nd entry represents the following mapping:
1568 *
1569 * "3012": Logic 3->Phys 0; Logic 0->Phys 1; Logic 1->Phys 2; Logic 2->Phys 3;
1570 */
1571static const int supported_data_lane_swaps[][4] = {
1572 { 0, 1, 2, 3 },
1573 { 3, 0, 1, 2 },
1574 { 2, 3, 0, 1 },
1575 { 1, 2, 3, 0 },
1576 { 0, 3, 2, 1 },
1577 { 1, 0, 3, 2 },
1578 { 2, 1, 0, 3 },
1579 { 3, 2, 1, 0 },
1580};
1581
1582static int dsi_host_parse_lane_data(struct msm_dsi_host *msm_host,
1583 struct device_node *ep)
1584{
1585 struct device *dev = &msm_host->pdev->dev;
1586 struct property *prop;
1587 u32 lane_map[4];
1588 int ret, i, len, num_lanes;
1589
Archit Taneja60282ce2016-06-08 16:14:19 +05301590 prop = of_find_property(ep, "data-lanes", &len);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301591 if (!prop) {
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301592 dev_dbg(dev,
1593 "failed to find data lane mapping, using default\n");
1594 return 0;
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301595 }
1596
1597 num_lanes = len / sizeof(u32);
1598
1599 if (num_lanes < 1 || num_lanes > 4) {
1600 dev_err(dev, "bad number of data lanes\n");
1601 return -EINVAL;
1602 }
1603
1604 msm_host->num_data_lanes = num_lanes;
1605
Archit Taneja60282ce2016-06-08 16:14:19 +05301606 ret = of_property_read_u32_array(ep, "data-lanes", lane_map,
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301607 num_lanes);
1608 if (ret) {
1609 dev_err(dev, "failed to read lane data\n");
1610 return ret;
1611 }
1612
1613 /*
1614 * compare DT specified physical-logical lane mappings with the ones
1615 * supported by hardware
1616 */
1617 for (i = 0; i < ARRAY_SIZE(supported_data_lane_swaps); i++) {
1618 const int *swap = supported_data_lane_swaps[i];
1619 int j;
1620
Archit Taneja60282ce2016-06-08 16:14:19 +05301621 /*
1622 * the data-lanes array we get from DT has a logical->physical
1623 * mapping. The "data lane swap" register field represents
1624 * supported configurations in a physical->logical mapping.
1625 * Translate the DT mapping to what we understand and find a
1626 * configuration that works.
1627 */
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301628 for (j = 0; j < num_lanes; j++) {
Archit Taneja60282ce2016-06-08 16:14:19 +05301629 if (lane_map[j] < 0 || lane_map[j] > 3)
1630 dev_err(dev, "bad physical lane entry %u\n",
1631 lane_map[j]);
1632
1633 if (swap[lane_map[j]] != j)
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301634 break;
1635 }
1636
1637 if (j == num_lanes) {
1638 msm_host->dlane_swap = i;
1639 return 0;
1640 }
1641 }
1642
1643 return -EINVAL;
1644}
1645
Archit Tanejaf7009d22015-06-25 11:43:40 +05301646static int dsi_host_parse_dt(struct msm_dsi_host *msm_host)
1647{
1648 struct device *dev = &msm_host->pdev->dev;
1649 struct device_node *np = dev->of_node;
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301650 struct device_node *endpoint, *device_node;
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301651 int ret = 0;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301652
Archit Tanejaf7009d22015-06-25 11:43:40 +05301653 /*
Archit Tanejab9ac76f2016-04-27 15:36:53 +05301654 * Get the endpoint of the output port of the DSI host. In our case,
1655 * this is mapped to port number with reg = 1. Don't return an error if
1656 * the remote endpoint isn't defined. It's possible that there is
1657 * nothing connected to the dsi output.
Archit Tanejaf7009d22015-06-25 11:43:40 +05301658 */
Archit Tanejab9ac76f2016-04-27 15:36:53 +05301659 endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);
Archit Tanejaf7009d22015-06-25 11:43:40 +05301660 if (!endpoint) {
1661 dev_dbg(dev, "%s: no endpoint\n", __func__);
1662 return 0;
1663 }
1664
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301665 ret = dsi_host_parse_lane_data(msm_host, endpoint);
1666 if (ret) {
1667 dev_err(dev, "%s: invalid lane configuration %d\n",
1668 __func__, ret);
1669 goto err;
1670 }
1671
Archit Tanejaf7009d22015-06-25 11:43:40 +05301672 /* Get panel node from the output port's endpoint data */
Rob Herring86418f92017-03-22 08:26:06 -05001673 device_node = of_graph_get_remote_node(np, 1, 0);
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301674 if (!device_node) {
Archit Tanejaa1b1a4f2017-01-04 14:14:58 +05301675 dev_dbg(dev, "%s: no valid device\n", __func__);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301676 goto err;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301677 }
1678
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301679 msm_host->device_node = device_node;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301680
Archit Taneja0c7df472015-10-14 15:31:13 +05301681 if (of_property_read_bool(np, "syscon-sfpb")) {
1682 msm_host->sfpb = syscon_regmap_lookup_by_phandle(np,
1683 "syscon-sfpb");
1684 if (IS_ERR(msm_host->sfpb)) {
1685 dev_err(dev, "%s: failed to get sfpb regmap\n",
1686 __func__);
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301687 ret = PTR_ERR(msm_host->sfpb);
Archit Taneja0c7df472015-10-14 15:31:13 +05301688 }
1689 }
1690
Archit Taneja26f7d1f2016-02-25 11:19:48 +05301691 of_node_put(device_node);
1692
1693err:
1694 of_node_put(endpoint);
1695
1696 return ret;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301697}
1698
Archit Taneja32280d62016-06-23 15:26:04 +05301699static int dsi_host_get_id(struct msm_dsi_host *msm_host)
1700{
1701 struct platform_device *pdev = msm_host->pdev;
1702 const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
1703 struct resource *res;
1704 int i;
1705
1706 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_ctrl");
1707 if (!res)
1708 return -EINVAL;
1709
1710 for (i = 0; i < cfg->num_dsi; i++) {
1711 if (cfg->io_start[i] == res->start)
1712 return i;
1713 }
1714
1715 return -EINVAL;
1716}
1717
Hai Lia6895542015-03-31 14:36:33 -04001718int msm_dsi_host_init(struct msm_dsi *msm_dsi)
1719{
1720 struct msm_dsi_host *msm_host = NULL;
1721 struct platform_device *pdev = msm_dsi->pdev;
1722 int ret;
1723
1724 msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
1725 if (!msm_host) {
1726 pr_err("%s: FAILED: cannot alloc dsi host\n",
1727 __func__);
1728 ret = -ENOMEM;
1729 goto fail;
1730 }
1731
Archit Tanejaf7009d22015-06-25 11:43:40 +05301732 msm_host->pdev = pdev;
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05301733 msm_dsi->host = &msm_host->base;
Archit Tanejaf7009d22015-06-25 11:43:40 +05301734
1735 ret = dsi_host_parse_dt(msm_host);
Hai Lia6895542015-03-31 14:36:33 -04001736 if (ret) {
Archit Tanejaf7009d22015-06-25 11:43:40 +05301737 pr_err("%s: failed to parse dt\n", __func__);
Hai Lia6895542015-03-31 14:36:33 -04001738 goto fail;
1739 }
Hai Lia6895542015-03-31 14:36:33 -04001740
Hai Lia6895542015-03-31 14:36:33 -04001741 msm_host->ctrl_base = msm_ioremap(pdev, "dsi_ctrl", "DSI CTRL");
1742 if (IS_ERR(msm_host->ctrl_base)) {
1743 pr_err("%s: unable to map Dsi ctrl base\n", __func__);
1744 ret = PTR_ERR(msm_host->ctrl_base);
1745 goto fail;
1746 }
1747
Archit Tanejaf6be1122017-07-28 16:17:03 +05301748 pm_runtime_enable(&pdev->dev);
1749
Hai Lid248b612015-08-13 17:49:29 -04001750 msm_host->cfg_hnd = dsi_get_config(msm_host);
1751 if (!msm_host->cfg_hnd) {
Hai Lia6895542015-03-31 14:36:33 -04001752 ret = -EINVAL;
1753 pr_err("%s: get config failed\n", __func__);
1754 goto fail;
1755 }
1756
Archit Taneja32280d62016-06-23 15:26:04 +05301757 msm_host->id = dsi_host_get_id(msm_host);
1758 if (msm_host->id < 0) {
1759 ret = msm_host->id;
1760 pr_err("%s: unable to identify DSI host index\n", __func__);
1761 goto fail;
1762 }
1763
Hai Lid248b612015-08-13 17:49:29 -04001764 /* fixup base address by io offset */
1765 msm_host->ctrl_base += msm_host->cfg_hnd->cfg->io_offset;
1766
Hai Lia6895542015-03-31 14:36:33 -04001767 ret = dsi_regulator_init(msm_host);
1768 if (ret) {
1769 pr_err("%s: regulator init failed\n", __func__);
1770 goto fail;
1771 }
1772
Archit Taneja31c92762015-10-09 12:40:39 +05301773 ret = dsi_clk_init(msm_host);
1774 if (ret) {
1775 pr_err("%s: unable to initialize dsi clks\n", __func__);
1776 goto fail;
1777 }
1778
Hai Lia6895542015-03-31 14:36:33 -04001779 msm_host->rx_buf = devm_kzalloc(&pdev->dev, SZ_4K, GFP_KERNEL);
1780 if (!msm_host->rx_buf) {
Wei Yongjuncd57b48a42017-02-09 15:19:07 +00001781 ret = -ENOMEM;
Hai Lia6895542015-03-31 14:36:33 -04001782 pr_err("%s: alloc rx temp buf failed\n", __func__);
1783 goto fail;
1784 }
1785
1786 init_completion(&msm_host->dma_comp);
1787 init_completion(&msm_host->video_comp);
1788 mutex_init(&msm_host->dev_mutex);
1789 mutex_init(&msm_host->cmd_mutex);
Hai Lia6895542015-03-31 14:36:33 -04001790 spin_lock_init(&msm_host->intr_lock);
1791
1792 /* setup workqueue */
1793 msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
1794 INIT_WORK(&msm_host->err_work, dsi_err_worker);
Archit Taneja8d23ea42016-10-25 12:17:59 +05301795 INIT_WORK(&msm_host->hpd_work, dsi_hpd_worker);
Hai Lia6895542015-03-31 14:36:33 -04001796
Hai Lia6895542015-03-31 14:36:33 -04001797 msm_dsi->id = msm_host->id;
1798
1799 DBG("Dsi Host %d initialized", msm_host->id);
1800 return 0;
1801
1802fail:
1803 return ret;
1804}
1805
1806void msm_dsi_host_destroy(struct mipi_dsi_host *host)
1807{
1808 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1809
1810 DBG("");
1811 dsi_tx_buf_free(msm_host);
1812 if (msm_host->workqueue) {
1813 flush_workqueue(msm_host->workqueue);
1814 destroy_workqueue(msm_host->workqueue);
1815 msm_host->workqueue = NULL;
1816 }
1817
Hai Lia6895542015-03-31 14:36:33 -04001818 mutex_destroy(&msm_host->cmd_mutex);
1819 mutex_destroy(&msm_host->dev_mutex);
Archit Tanejaf6be1122017-07-28 16:17:03 +05301820
1821 pm_runtime_disable(&msm_host->pdev->dev);
Hai Lia6895542015-03-31 14:36:33 -04001822}
1823
1824int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
1825 struct drm_device *dev)
1826{
1827 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1828 struct platform_device *pdev = msm_host->pdev;
1829 int ret;
1830
1831 msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
1832 if (msm_host->irq < 0) {
1833 ret = msm_host->irq;
1834 dev_err(dev->dev, "failed to get irq: %d\n", ret);
1835 return ret;
1836 }
1837
1838 ret = devm_request_irq(&pdev->dev, msm_host->irq,
1839 dsi_host_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1840 "dsi_isr", msm_host);
1841 if (ret < 0) {
1842 dev_err(&pdev->dev, "failed to request IRQ%u: %d\n",
1843 msm_host->irq, ret);
1844 return ret;
1845 }
1846
1847 msm_host->dev = dev;
1848 ret = dsi_tx_buf_alloc(msm_host, SZ_4K);
1849 if (ret) {
1850 pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
1851 return ret;
1852 }
1853
1854 return 0;
1855}
1856
1857int msm_dsi_host_register(struct mipi_dsi_host *host, bool check_defer)
1858{
1859 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lia6895542015-03-31 14:36:33 -04001860 int ret;
1861
1862 /* Register mipi dsi host */
1863 if (!msm_host->registered) {
1864 host->dev = &msm_host->pdev->dev;
1865 host->ops = &dsi_host_ops;
1866 ret = mipi_dsi_host_register(host);
1867 if (ret)
1868 return ret;
1869
1870 msm_host->registered = true;
1871
1872 /* If the panel driver has not been probed after host register,
1873 * we should defer the host's probe.
1874 * It makes sure panel is connected when fbcon detects
1875 * connector status and gets the proper display mode to
1876 * create framebuffer.
Archit Tanejaf7009d22015-06-25 11:43:40 +05301877 * Don't try to defer if there is nothing connected to the dsi
1878 * output
Hai Lia6895542015-03-31 14:36:33 -04001879 */
Archit Tanejaa9ddac92015-08-03 14:05:45 +05301880 if (check_defer && msm_host->device_node) {
1881 if (!of_drm_find_panel(msm_host->device_node))
Archit Tanejac118e292015-07-31 14:06:10 +05301882 if (!of_drm_find_bridge(msm_host->device_node))
1883 return -EPROBE_DEFER;
Hai Lia6895542015-03-31 14:36:33 -04001884 }
1885 }
1886
1887 return 0;
1888}
1889
1890void msm_dsi_host_unregister(struct mipi_dsi_host *host)
1891{
1892 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1893
1894 if (msm_host->registered) {
1895 mipi_dsi_host_unregister(host);
1896 host->dev = NULL;
1897 host->ops = NULL;
1898 msm_host->registered = false;
1899 }
1900}
1901
1902int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
1903 const struct mipi_dsi_msg *msg)
1904{
1905 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1906
1907 /* TODO: make sure dsi_cmd_mdp is idle.
1908 * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
1909 * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
1910 * How to handle the old versions? Wait for mdp cmd done?
1911 */
1912
1913 /*
1914 * mdss interrupt is generated in mdp core clock domain
1915 * mdp clock need to be enabled to receive dsi interrupt
1916 */
Archit Tanejaf6be1122017-07-28 16:17:03 +05301917 pm_runtime_get_sync(&msm_host->pdev->dev);
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05301918 dsi_link_clk_enable(msm_host);
Hai Lia6895542015-03-31 14:36:33 -04001919
1920 /* TODO: vote for bus bandwidth */
1921
1922 if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
1923 dsi_set_tx_power_mode(0, msm_host);
1924
1925 msm_host->dma_cmd_ctrl_restore = dsi_read(msm_host, REG_DSI_CTRL);
1926 dsi_write(msm_host, REG_DSI_CTRL,
1927 msm_host->dma_cmd_ctrl_restore |
1928 DSI_CTRL_CMD_MODE_EN |
1929 DSI_CTRL_ENABLE);
1930 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 1);
1931
1932 return 0;
1933}
1934
1935void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
1936 const struct mipi_dsi_msg *msg)
1937{
1938 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1939
1940 dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 0);
1941 dsi_write(msm_host, REG_DSI_CTRL, msm_host->dma_cmd_ctrl_restore);
1942
1943 if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
1944 dsi_set_tx_power_mode(1, msm_host);
1945
1946 /* TODO: unvote for bus bandwidth */
1947
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05301948 dsi_link_clk_disable(msm_host);
Archit Tanejaf6be1122017-07-28 16:17:03 +05301949 pm_runtime_put_autosuspend(&msm_host->pdev->dev);
Hai Lia6895542015-03-31 14:36:33 -04001950}
1951
1952int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
1953 const struct mipi_dsi_msg *msg)
1954{
1955 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1956
1957 return dsi_cmds2buf_tx(msm_host, msg);
1958}
1959
1960int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
1961 const struct mipi_dsi_msg *msg)
1962{
1963 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lid248b612015-08-13 17:49:29 -04001964 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Lia6895542015-03-31 14:36:33 -04001965 int data_byte, rx_byte, dlen, end;
1966 int short_response, diff, pkt_size, ret = 0;
1967 char cmd;
1968 int rlen = msg->rx_len;
1969 u8 *buf;
1970
1971 if (rlen <= 2) {
1972 short_response = 1;
1973 pkt_size = rlen;
1974 rx_byte = 4;
1975 } else {
1976 short_response = 0;
1977 data_byte = 10; /* first read */
1978 if (rlen < data_byte)
1979 pkt_size = rlen;
1980 else
1981 pkt_size = data_byte;
1982 rx_byte = data_byte + 6; /* 4 header + 2 crc */
1983 }
1984
1985 buf = msm_host->rx_buf;
1986 end = 0;
1987 while (!end) {
1988 u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
1989 struct mipi_dsi_msg max_pkt_size_msg = {
1990 .channel = msg->channel,
1991 .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
1992 .tx_len = 2,
1993 .tx_buf = tx,
1994 };
1995
1996 DBG("rlen=%d pkt_size=%d rx_byte=%d",
1997 rlen, pkt_size, rx_byte);
1998
1999 ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
2000 if (ret < 2) {
2001 pr_err("%s: Set max pkt size failed, %d\n",
2002 __func__, ret);
2003 return -EINVAL;
2004 }
2005
Hai Lid248b612015-08-13 17:49:29 -04002006 if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
2007 (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
Hai Lia6895542015-03-31 14:36:33 -04002008 /* Clear the RDBK_DATA registers */
2009 dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
2010 DSI_RDBK_DATA_CTRL_CLR);
2011 wmb(); /* make sure the RDBK registers are cleared */
2012 dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
2013 wmb(); /* release cleared status before transfer */
2014 }
2015
2016 ret = dsi_cmds2buf_tx(msm_host, msg);
2017 if (ret < msg->tx_len) {
2018 pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
2019 return ret;
2020 }
2021
2022 /*
2023 * once cmd_dma_done interrupt received,
2024 * return data from client is ready and stored
2025 * at RDBK_DATA register already
2026 * since rx fifo is 16 bytes, dcs header is kept at first loop,
2027 * after that dcs header lost during shift into registers
2028 */
2029 dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
2030
2031 if (dlen <= 0)
2032 return 0;
2033
2034 if (short_response)
2035 break;
2036
2037 if (rlen <= data_byte) {
2038 diff = data_byte - rlen;
2039 end = 1;
2040 } else {
2041 diff = 0;
2042 rlen -= data_byte;
2043 }
2044
2045 if (!end) {
2046 dlen -= 2; /* 2 crc */
2047 dlen -= diff;
2048 buf += dlen; /* next start position */
2049 data_byte = 14; /* NOT first read */
2050 if (rlen < data_byte)
2051 pkt_size += rlen;
2052 else
2053 pkt_size += data_byte;
2054 DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
2055 }
2056 }
2057
2058 /*
2059 * For single Long read, if the requested rlen < 10,
2060 * we need to shift the start position of rx
2061 * data buffer to skip the bytes which are not
2062 * updated.
2063 */
2064 if (pkt_size < 10 && !short_response)
2065 buf = msm_host->rx_buf + (10 - rlen);
2066 else
2067 buf = msm_host->rx_buf;
2068
2069 cmd = buf[0];
2070 switch (cmd) {
2071 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
2072 pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
2073 ret = 0;
Hai Li651ad3f2015-04-29 11:38:59 -04002074 break;
Hai Lia6895542015-03-31 14:36:33 -04002075 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
2076 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
2077 ret = dsi_short_read1_resp(buf, msg);
2078 break;
2079 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
2080 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
2081 ret = dsi_short_read2_resp(buf, msg);
2082 break;
2083 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
2084 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
2085 ret = dsi_long_read_resp(buf, msg);
2086 break;
2087 default:
2088 pr_warn("%s:Invalid response cmd\n", __func__);
2089 ret = 0;
2090 }
2091
2092 return ret;
2093}
2094
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05302095void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 dma_base,
2096 u32 len)
Hai Lia6895542015-03-31 14:36:33 -04002097{
2098 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2099
Archit Taneja4ff9d4c2015-10-13 12:20:47 +05302100 dsi_write(msm_host, REG_DSI_DMA_BASE, dma_base);
Hai Lia6895542015-03-31 14:36:33 -04002101 dsi_write(msm_host, REG_DSI_DMA_LEN, len);
2102 dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
2103
2104 /* Make sure trigger happens */
2105 wmb();
2106}
2107
Hai Li9d32c4982015-05-15 13:04:05 -04002108int msm_dsi_host_set_src_pll(struct mipi_dsi_host *host,
2109 struct msm_dsi_pll *src_pll)
2110{
2111 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Archit Taneja4bfa9742015-10-09 16:32:38 +05302112 const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
Hai Li9d32c4982015-05-15 13:04:05 -04002113 struct clk *byte_clk_provider, *pixel_clk_provider;
2114 int ret;
2115
2116 ret = msm_dsi_pll_get_clk_provider(src_pll,
2117 &byte_clk_provider, &pixel_clk_provider);
2118 if (ret) {
2119 pr_info("%s: can't get provider from pll, don't set parent\n",
2120 __func__);
2121 return 0;
2122 }
2123
2124 ret = clk_set_parent(msm_host->byte_clk_src, byte_clk_provider);
2125 if (ret) {
2126 pr_err("%s: can't set parent to byte_clk_src. ret=%d\n",
2127 __func__, ret);
2128 goto exit;
2129 }
2130
2131 ret = clk_set_parent(msm_host->pixel_clk_src, pixel_clk_provider);
2132 if (ret) {
2133 pr_err("%s: can't set parent to pixel_clk_src. ret=%d\n",
2134 __func__, ret);
2135 goto exit;
2136 }
2137
Archit Taneja4bfa9742015-10-09 16:32:38 +05302138 if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
2139 ret = clk_set_parent(msm_host->dsi_clk_src, pixel_clk_provider);
2140 if (ret) {
2141 pr_err("%s: can't set parent to dsi_clk_src. ret=%d\n",
2142 __func__, ret);
2143 goto exit;
2144 }
2145
2146 ret = clk_set_parent(msm_host->esc_clk_src, byte_clk_provider);
2147 if (ret) {
2148 pr_err("%s: can't set parent to esc_clk_src. ret=%d\n",
2149 __func__, ret);
2150 goto exit;
2151 }
2152 }
2153
Hai Li9d32c4982015-05-15 13:04:05 -04002154exit:
2155 return ret;
2156}
2157
Archit Taneja34d95452015-07-29 12:14:12 -04002158void msm_dsi_host_reset_phy(struct mipi_dsi_host *host)
2159{
2160 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2161
2162 DBG("");
2163 dsi_write(msm_host, REG_DSI_PHY_RESET, DSI_PHY_RESET_RESET);
2164 /* Make sure fully reset */
2165 wmb();
2166 udelay(1000);
2167 dsi_write(msm_host, REG_DSI_PHY_RESET, 0);
2168 udelay(100);
2169}
2170
Hai Lib62aa702017-01-07 14:24:38 +05302171void msm_dsi_host_get_phy_clk_req(struct mipi_dsi_host *host,
2172 struct msm_dsi_phy_clk_request *clk_req)
2173{
2174 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Archit Tanejad4cea382017-07-12 15:09:55 +05302175 int ret;
2176
2177 ret = dsi_calc_clk_rate(msm_host);
2178 if (ret) {
2179 pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
2180 return;
2181 }
Hai Lib62aa702017-01-07 14:24:38 +05302182
2183 clk_req->bitclk_rate = msm_host->byte_clk_rate * 8;
2184 clk_req->escclk_rate = msm_host->esc_clk_rate;
2185}
2186
Hai Lia6895542015-03-31 14:36:33 -04002187int msm_dsi_host_enable(struct mipi_dsi_host *host)
2188{
2189 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2190
2191 dsi_op_mode_config(msm_host,
2192 !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
2193
2194 /* TODO: clock should be turned off for command mode,
2195 * and only turned on before MDP START.
2196 * This part of code should be enabled once mdp driver support it.
2197 */
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05302198 /* if (msm_panel->mode == MSM_DSI_CMD_MODE) {
2199 * dsi_link_clk_disable(msm_host);
2200 * pm_runtime_put_autosuspend(&msm_host->pdev->dev);
2201 * }
2202 */
Hai Lia6895542015-03-31 14:36:33 -04002203
2204 return 0;
2205}
2206
2207int msm_dsi_host_disable(struct mipi_dsi_host *host)
2208{
2209 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2210
2211 dsi_op_mode_config(msm_host,
2212 !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
2213
2214 /* Since we have disabled INTF, the video engine won't stop so that
2215 * the cmd engine will be blocked.
2216 * Reset to disable video engine so that we can send off cmd.
2217 */
2218 dsi_sw_reset(msm_host);
2219
2220 return 0;
2221}
2222
Archit Taneja0c7df472015-10-14 15:31:13 +05302223static void msm_dsi_sfpb_config(struct msm_dsi_host *msm_host, bool enable)
2224{
2225 enum sfpb_ahb_arb_master_port_en en;
2226
2227 if (!msm_host->sfpb)
2228 return;
2229
2230 en = enable ? SFPB_MASTER_PORT_ENABLE : SFPB_MASTER_PORT_DISABLE;
2231
2232 regmap_update_bits(msm_host->sfpb, REG_SFPB_GPREG,
2233 SFPB_GPREG_MASTER_PORT_EN__MASK,
2234 SFPB_GPREG_MASTER_PORT_EN(en));
2235}
2236
Hai Lib62aa702017-01-07 14:24:38 +05302237int msm_dsi_host_power_on(struct mipi_dsi_host *host,
2238 struct msm_dsi_phy_shared_timings *phy_shared_timings)
Hai Lia6895542015-03-31 14:36:33 -04002239{
2240 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
Hai Lia6895542015-03-31 14:36:33 -04002241 int ret = 0;
2242
2243 mutex_lock(&msm_host->dev_mutex);
2244 if (msm_host->power_on) {
2245 DBG("dsi host already on");
2246 goto unlock_ret;
2247 }
2248
Archit Taneja0c7df472015-10-14 15:31:13 +05302249 msm_dsi_sfpb_config(msm_host, true);
2250
Hai Lia6895542015-03-31 14:36:33 -04002251 ret = dsi_host_regulator_enable(msm_host);
2252 if (ret) {
2253 pr_err("%s:Failed to enable vregs.ret=%d\n",
2254 __func__, ret);
2255 goto unlock_ret;
2256 }
2257
Archit Tanejaf6be1122017-07-28 16:17:03 +05302258 pm_runtime_get_sync(&msm_host->pdev->dev);
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05302259 ret = dsi_link_clk_enable(msm_host);
Hai Lia6895542015-03-31 14:36:33 -04002260 if (ret) {
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05302261 pr_err("%s: failed to enable link clocks. ret=%d\n",
2262 __func__, ret);
Hai Lia6895542015-03-31 14:36:33 -04002263 goto fail_disable_reg;
2264 }
2265
Hai Liab8909b2015-06-11 10:56:46 -04002266 ret = pinctrl_pm_select_default_state(&msm_host->pdev->dev);
2267 if (ret) {
2268 pr_err("%s: failed to set pinctrl default state, %d\n",
2269 __func__, ret);
2270 goto fail_disable_clk;
2271 }
2272
Hai Lia6895542015-03-31 14:36:33 -04002273 dsi_timing_setup(msm_host);
2274 dsi_sw_reset(msm_host);
Hai Lib62aa702017-01-07 14:24:38 +05302275 dsi_ctrl_config(msm_host, true, phy_shared_timings);
Hai Lia6895542015-03-31 14:36:33 -04002276
2277 if (msm_host->disp_en_gpio)
2278 gpiod_set_value(msm_host->disp_en_gpio, 1);
2279
2280 msm_host->power_on = true;
2281 mutex_unlock(&msm_host->dev_mutex);
2282
2283 return 0;
2284
Hai Liab8909b2015-06-11 10:56:46 -04002285fail_disable_clk:
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05302286 dsi_link_clk_disable(msm_host);
2287 pm_runtime_put_autosuspend(&msm_host->pdev->dev);
Hai Lia6895542015-03-31 14:36:33 -04002288fail_disable_reg:
2289 dsi_host_regulator_disable(msm_host);
2290unlock_ret:
2291 mutex_unlock(&msm_host->dev_mutex);
2292 return ret;
2293}
2294
2295int msm_dsi_host_power_off(struct mipi_dsi_host *host)
2296{
2297 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2298
2299 mutex_lock(&msm_host->dev_mutex);
2300 if (!msm_host->power_on) {
2301 DBG("dsi host already off");
2302 goto unlock_ret;
2303 }
2304
Hai Lidceac342016-09-15 14:34:49 +05302305 dsi_ctrl_config(msm_host, false, NULL);
Hai Lia6895542015-03-31 14:36:33 -04002306
2307 if (msm_host->disp_en_gpio)
2308 gpiod_set_value(msm_host->disp_en_gpio, 0);
2309
Hai Liab8909b2015-06-11 10:56:46 -04002310 pinctrl_pm_select_sleep_state(&msm_host->pdev->dev);
2311
Archit Tanejaf54ca1a2017-07-28 16:17:04 +05302312 dsi_link_clk_disable(msm_host);
Archit Tanejaf6be1122017-07-28 16:17:03 +05302313 pm_runtime_put_autosuspend(&msm_host->pdev->dev);
Hai Lia6895542015-03-31 14:36:33 -04002314
2315 dsi_host_regulator_disable(msm_host);
2316
Archit Taneja0c7df472015-10-14 15:31:13 +05302317 msm_dsi_sfpb_config(msm_host, false);
2318
Hai Lia6895542015-03-31 14:36:33 -04002319 DBG("-");
2320
2321 msm_host->power_on = false;
2322
2323unlock_ret:
2324 mutex_unlock(&msm_host->dev_mutex);
2325 return 0;
2326}
2327
2328int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
2329 struct drm_display_mode *mode)
2330{
2331 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2332
2333 if (msm_host->mode) {
2334 drm_mode_destroy(msm_host->dev, msm_host->mode);
2335 msm_host->mode = NULL;
2336 }
2337
2338 msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
Wei Yongjun2abe1f22016-06-18 17:26:37 +00002339 if (!msm_host->mode) {
Hai Lia6895542015-03-31 14:36:33 -04002340 pr_err("%s: cannot duplicate mode\n", __func__);
Wei Yongjun2abe1f22016-06-18 17:26:37 +00002341 return -ENOMEM;
Hai Lia6895542015-03-31 14:36:33 -04002342 }
2343
2344 return 0;
2345}
2346
2347struct drm_panel *msm_dsi_host_get_panel(struct mipi_dsi_host *host,
2348 unsigned long *panel_flags)
2349{
2350 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2351 struct drm_panel *panel;
2352
Archit Tanejaa9ddac92015-08-03 14:05:45 +05302353 panel = of_drm_find_panel(msm_host->device_node);
Hai Lia6895542015-03-31 14:36:33 -04002354 if (panel_flags)
2355 *panel_flags = msm_host->mode_flags;
2356
2357 return panel;
2358}
2359
Archit Tanejac118e292015-07-31 14:06:10 +05302360struct drm_bridge *msm_dsi_host_get_bridge(struct mipi_dsi_host *host)
2361{
2362 struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2363
2364 return of_drm_find_bridge(msm_host->device_node);
2365}