video: tegra: dc: Optimize OS idle display client wakeup
[linux-3.10.git] / drivers / video / tegra / dc / dsi.c
1 /*
2  * drivers/video/tegra/dc/dsi.c
3  *
4  * Copyright (c) 2011-2013, NVIDIA CORPORATION, All rights reserved.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/clk.h>
19 #include <linux/delay.h>
20 #include <linux/err.h>
21 #include <linux/fb.h>
22 #include <linux/gpio.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/workqueue.h>
27 #include <linux/moduleparam.h>
28 #include <linux/export.h>
29 #include <linux/debugfs.h>
30 #include <linux/seq_file.h>
31 #include <linux/nvhost.h>
32 #include <linux/lcm.h>
33 #include <linux/regulator/consumer.h>
34
35 #include <mach/clk.h>
36 #include <mach/dc.h>
37 #include <mach/fb.h>
38 #include <mach/csi.h>
39 #include <linux/nvhost.h>
40
41 #include "dc_reg.h"
42 #include "dc_priv.h"
43 #include "dev.h"
44 #include "dsi_regs.h"
45 #include "dsi.h"
46 #include "mipi_cal.h"
47
48 /* HACK! This needs to come from DT */
49 #include "../../../../arch/arm/mach-tegra/iomap.h"
50
51 #define APB_MISC_GP_MIPI_PAD_CTRL_0     (TEGRA_APB_MISC_BASE + 0x820)
52 #define DSIB_MODE_ENABLE                0x2
53
54 #define DSI_USE_SYNC_POINTS             0
55 #define S_TO_MS(x)                      (1000 * (x))
56 #define MS_TO_US(x)                     (1000 * (x))
57
58 #define DSI_MODULE_NOT_INIT             0x0
59 #define DSI_MODULE_INIT                 0x1
60
61 #define DSI_LPHS_NOT_INIT               0x0
62 #define DSI_LPHS_IN_LP_MODE             0x1
63 #define DSI_LPHS_IN_HS_MODE             0x2
64
65 #define DSI_VIDEO_TYPE_NOT_INIT         0x0
66 #define DSI_VIDEO_TYPE_VIDEO_MODE       0x1
67 #define DSI_VIDEO_TYPE_CMD_MODE         0x2
68
69 #define DSI_DRIVEN_MODE_NOT_INIT        0x0
70 #define DSI_DRIVEN_MODE_DC              0x1
71 #define DSI_DRIVEN_MODE_HOST            0x2
72
73 #define DSI_PHYCLK_OUT_DIS              0x0
74 #define DSI_PHYCLK_OUT_EN               0x1
75
76 #define DSI_PHYCLK_NOT_INIT             0x0
77 #define DSI_PHYCLK_CONTINUOUS           0x1
78 #define DSI_PHYCLK_TX_ONLY              0x2
79
80 #define DSI_CLK_BURST_NOT_INIT          0x0
81 #define DSI_CLK_BURST_NONE_BURST        0x1
82 #define DSI_CLK_BURST_BURST_MODE        0x2
83
84 #define DSI_DC_STREAM_DISABLE           0x0
85 #define DSI_DC_STREAM_ENABLE            0x1
86
87 #define DSI_LP_OP_NOT_INIT              0x0
88 #define DSI_LP_OP_WRITE                 0x1
89 #define DSI_LP_OP_READ                  0x2
90
91 #define DSI_HOST_IDLE_PERIOD            1000
92 static atomic_t dsi_syncpt_rst = ATOMIC_INIT(0);
93
94 static bool enable_read_debug;
95 module_param(enable_read_debug, bool, 0644);
96 MODULE_PARM_DESC(enable_read_debug,
97                 "Enable to print read fifo and return packet type");
98
99 /* source of video data */
100 enum {
101         TEGRA_DSI_DRIVEN_BY_DC,
102         TEGRA_DSI_DRIVEN_BY_HOST,
103 };
104
105 static struct tegra_dc_dsi_data *tegra_dsi_instance[MAX_DSI_INSTANCE];
106
107 const u32 dsi_pkt_seq_reg[NUMOF_PKT_SEQ] = {
108         DSI_PKT_SEQ_0_LO,
109         DSI_PKT_SEQ_0_HI,
110         DSI_PKT_SEQ_1_LO,
111         DSI_PKT_SEQ_1_HI,
112         DSI_PKT_SEQ_2_LO,
113         DSI_PKT_SEQ_2_HI,
114         DSI_PKT_SEQ_3_LO,
115         DSI_PKT_SEQ_3_HI,
116         DSI_PKT_SEQ_4_LO,
117         DSI_PKT_SEQ_4_HI,
118         DSI_PKT_SEQ_5_LO,
119         DSI_PKT_SEQ_5_HI,
120 };
121
122 const u32 dsi_pkt_seq_video_non_burst_syne[NUMOF_PKT_SEQ] = {
123         PKT_ID0(CMD_VS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
124         0,
125         PKT_ID0(CMD_VE) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
126         0,
127         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
128         0,
129         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
130         PKT_ID2(CMD_HE) | PKT_LEN2(0),
131         PKT_ID3(CMD_BLNK) | PKT_LEN3(2) | PKT_ID4(CMD_RGB) | PKT_LEN4(3) |
132         PKT_ID5(CMD_BLNK) | PKT_LEN5(4),
133         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
134         0,
135         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
136         PKT_ID2(CMD_HE) | PKT_LEN2(0),
137         PKT_ID3(CMD_BLNK) | PKT_LEN3(2) | PKT_ID4(CMD_RGB) | PKT_LEN4(3) |
138         PKT_ID5(CMD_BLNK) | PKT_LEN5(4),
139 };
140
141 const u32 dsi_pkt_seq_video_non_burst[NUMOF_PKT_SEQ] = {
142         PKT_ID0(CMD_VS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
143         0,
144         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
145         0,
146         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
147         0,
148         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2) |
149         PKT_ID2(CMD_RGB) | PKT_LEN2(3),
150         PKT_ID3(CMD_BLNK) | PKT_LEN3(4),
151         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
152         0,
153         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2) |
154         PKT_ID2(CMD_RGB) | PKT_LEN2(3),
155         PKT_ID3(CMD_BLNK) | PKT_LEN3(4),
156 };
157
158 const u32 dsi_pkt_seq_video_non_burst_no_eot_no_lp_no_hbp[NUMOF_PKT_SEQ] = {
159         PKT_ID0(CMD_VS) | PKT_LEN0(0),
160         0,
161         PKT_ID0(CMD_HS) | PKT_LEN0(0),
162         0,
163         PKT_ID0(CMD_HS) | PKT_LEN0(0),
164         0,
165         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_RGB) | PKT_LEN1(3) |
166         PKT_ID2(CMD_BLNK) | PKT_LEN2(4),
167         0,
168         PKT_ID0(CMD_HS) | PKT_LEN0(0),
169         0,
170         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_RGB) | PKT_LEN1(3) |
171         PKT_ID2(CMD_BLNK) | PKT_LEN2(4),
172         0,
173 };
174
175 static const u32 dsi_pkt_seq_video_burst[NUMOF_PKT_SEQ] = {
176         PKT_ID0(CMD_VS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
177         0,
178         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
179         0,
180         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
181         0,
182         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2)|
183         PKT_ID2(CMD_RGB) | PKT_LEN2(3) | PKT_LP,
184         PKT_ID0(CMD_EOT) | PKT_LEN0(7),
185         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
186         0,
187         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2)|
188         PKT_ID2(CMD_RGB) | PKT_LEN2(3) | PKT_LP,
189         PKT_ID0(CMD_EOT) | PKT_LEN0(7),
190 };
191
192 static const u32 dsi_pkt_seq_video_burst_no_eot[NUMOF_PKT_SEQ] = {
193         PKT_ID0(CMD_VS) | PKT_LEN0(0) | PKT_LP,
194         0,
195         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_LP,
196         0,
197         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_LP,
198         0,
199         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2)|
200         PKT_ID2(CMD_RGB) | PKT_LEN2(3) | PKT_LP,
201         0,
202         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_LP,
203         0,
204         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2)|
205         PKT_ID2(CMD_RGB) | PKT_LEN2(3) | PKT_LP,
206         0,
207 };
208
209 /* TODO: verify with hw about this format */
210 const u32 dsi_pkt_seq_cmd_mode[NUMOF_PKT_SEQ] = {
211         0,
212         0,
213         0,
214         0,
215         0,
216         0,
217         PKT_ID0(CMD_LONGW) | PKT_LEN0(3) | PKT_ID1(CMD_EOT) | PKT_LEN1(7),
218         0,
219         0,
220         0,
221         PKT_ID0(CMD_LONGW) | PKT_LEN0(3) | PKT_ID1(CMD_EOT) | PKT_LEN1(7),
222         0,
223 };
224
225 const u32 init_reg[] = {
226         DSI_INT_ENABLE,
227         DSI_INT_STATUS,
228         DSI_INT_MASK,
229         DSI_INIT_SEQ_DATA_0,
230         DSI_INIT_SEQ_DATA_1,
231         DSI_INIT_SEQ_DATA_2,
232         DSI_INIT_SEQ_DATA_3,
233         DSI_INIT_SEQ_DATA_4,
234         DSI_INIT_SEQ_DATA_5,
235         DSI_INIT_SEQ_DATA_6,
236         DSI_INIT_SEQ_DATA_7,
237         DSI_DCS_CMDS,
238         DSI_PKT_SEQ_0_LO,
239         DSI_PKT_SEQ_1_LO,
240         DSI_PKT_SEQ_2_LO,
241         DSI_PKT_SEQ_3_LO,
242         DSI_PKT_SEQ_4_LO,
243         DSI_PKT_SEQ_5_LO,
244         DSI_PKT_SEQ_0_HI,
245         DSI_PKT_SEQ_1_HI,
246         DSI_PKT_SEQ_2_HI,
247         DSI_PKT_SEQ_3_HI,
248         DSI_PKT_SEQ_4_HI,
249         DSI_PKT_SEQ_5_HI,
250         DSI_CONTROL,
251         DSI_HOST_DSI_CONTROL,
252         DSI_PAD_CONTROL,
253         DSI_PAD_CONTROL_CD,
254         DSI_SOL_DELAY,
255         DSI_MAX_THRESHOLD,
256         DSI_TRIGGER,
257         DSI_TX_CRC,
258         DSI_INIT_SEQ_CONTROL,
259         DSI_PKT_LEN_0_1,
260         DSI_PKT_LEN_2_3,
261         DSI_PKT_LEN_4_5,
262         DSI_PKT_LEN_6_7,
263 };
264
265 const u32 init_reg_vs1_ext[] = {
266         DSI_PAD_CONTROL_0_VS1,
267         DSI_PAD_CONTROL_CD_VS1,
268         DSI_PAD_CD_STATUS_VS1,
269         DSI_PAD_CONTROL_1_VS1,
270         DSI_PAD_CONTROL_2_VS1,
271         DSI_PAD_CONTROL_3_VS1,
272         DSI_PAD_CONTROL_4_VS1,
273         DSI_GANGED_MODE_CONTROL,
274         DSI_GANGED_MODE_START,
275         DSI_GANGED_MODE_SIZE,
276 };
277
278 static int tegra_dsi_host_suspend(struct tegra_dc *dc);
279 static int tegra_dsi_host_resume(struct tegra_dc *dc);
280 static void tegra_dc_dsi_idle_work(struct work_struct *work);
281 static void tegra_dsi_send_dc_frames(struct tegra_dc *dc,
282                                      struct tegra_dc_dsi_data *dsi,
283                                      int no_of_frames);
284
285 inline unsigned long tegra_dsi_readl(struct tegra_dc_dsi_data *dsi, u32 reg)
286 {
287         unsigned long ret;
288
289         BUG_ON(!nvhost_module_powered_ext(dsi->dc->ndev));
290         ret = readl(dsi->base + reg * 4);
291         trace_display_readl(dsi->dc, ret, dsi->base + reg * 4);
292         return ret;
293 }
294 EXPORT_SYMBOL(tegra_dsi_readl);
295
296 inline void tegra_dsi_writel(struct tegra_dc_dsi_data *dsi, u32 val, u32 reg)
297 {
298         BUG_ON(!nvhost_module_powered_ext(dsi->dc->ndev));
299         trace_display_writel(dsi->dc, val, dsi->base + reg * 4);
300         writel(val, dsi->base + reg * 4);
301 }
302 EXPORT_SYMBOL(tegra_dsi_writel);
303
304 #ifdef CONFIG_DEBUG_FS
305 static int dbg_dsi_show(struct seq_file *s, void *unused)
306 {
307         struct tegra_dc_dsi_data *dsi = s->private;
308
309 #define DUMP_REG(a) do {                                                \
310                 seq_printf(s, "%-32s\t%03x\t%08lx\n",                   \
311                        #a, a, tegra_dsi_readl(dsi, a));         \
312         } while (0)
313
314         tegra_dc_io_start(dsi->dc);
315         clk_prepare_enable(dsi->dsi_clk);
316
317         DUMP_REG(DSI_INCR_SYNCPT_CNTRL);
318         DUMP_REG(DSI_INCR_SYNCPT_ERROR);
319         DUMP_REG(DSI_CTXSW);
320         DUMP_REG(DSI_POWER_CONTROL);
321         DUMP_REG(DSI_INT_ENABLE);
322         DUMP_REG(DSI_HOST_DSI_CONTROL);
323         DUMP_REG(DSI_CONTROL);
324         DUMP_REG(DSI_SOL_DELAY);
325         DUMP_REG(DSI_MAX_THRESHOLD);
326         DUMP_REG(DSI_TRIGGER);
327         DUMP_REG(DSI_TX_CRC);
328         DUMP_REG(DSI_STATUS);
329         DUMP_REG(DSI_INIT_SEQ_CONTROL);
330         DUMP_REG(DSI_INIT_SEQ_DATA_0);
331         DUMP_REG(DSI_INIT_SEQ_DATA_1);
332         DUMP_REG(DSI_INIT_SEQ_DATA_2);
333         DUMP_REG(DSI_INIT_SEQ_DATA_3);
334         DUMP_REG(DSI_INIT_SEQ_DATA_4);
335         DUMP_REG(DSI_INIT_SEQ_DATA_5);
336         DUMP_REG(DSI_INIT_SEQ_DATA_6);
337         DUMP_REG(DSI_INIT_SEQ_DATA_7);
338         DUMP_REG(DSI_PKT_SEQ_0_LO);
339         DUMP_REG(DSI_PKT_SEQ_0_HI);
340         DUMP_REG(DSI_PKT_SEQ_1_LO);
341         DUMP_REG(DSI_PKT_SEQ_1_HI);
342         DUMP_REG(DSI_PKT_SEQ_2_LO);
343         DUMP_REG(DSI_PKT_SEQ_2_HI);
344         DUMP_REG(DSI_PKT_SEQ_3_LO);
345         DUMP_REG(DSI_PKT_SEQ_3_HI);
346         DUMP_REG(DSI_PKT_SEQ_4_LO);
347         DUMP_REG(DSI_PKT_SEQ_4_HI);
348         DUMP_REG(DSI_PKT_SEQ_5_LO);
349         DUMP_REG(DSI_PKT_SEQ_5_HI);
350         DUMP_REG(DSI_DCS_CMDS);
351         DUMP_REG(DSI_PKT_LEN_0_1);
352         DUMP_REG(DSI_PKT_LEN_2_3);
353         DUMP_REG(DSI_PKT_LEN_4_5);
354         DUMP_REG(DSI_PKT_LEN_6_7);
355         DUMP_REG(DSI_PHY_TIMING_0);
356         DUMP_REG(DSI_PHY_TIMING_1);
357         DUMP_REG(DSI_PHY_TIMING_2);
358         DUMP_REG(DSI_BTA_TIMING);
359         DUMP_REG(DSI_TIMEOUT_0);
360         DUMP_REG(DSI_TIMEOUT_1);
361         DUMP_REG(DSI_TO_TALLY);
362         DUMP_REG(DSI_PAD_CONTROL);
363         DUMP_REG(DSI_PAD_CONTROL_CD);
364         DUMP_REG(DSI_PAD_CD_STATUS);
365         DUMP_REG(DSI_VID_MODE_CONTROL);
366 #undef DUMP_REG
367
368         clk_disable_unprepare(dsi->dsi_clk);
369         tegra_dc_io_end(dsi->dc);
370
371         return 0;
372 }
373
374 static int dbg_dsi_open(struct inode *inode, struct file *file)
375 {
376         return single_open(file, dbg_dsi_show, inode->i_private);
377 }
378
379 static const struct file_operations dbg_fops = {
380         .open           = dbg_dsi_open,
381         .read           = seq_read,
382         .llseek         = seq_lseek,
383         .release        = single_release,
384 };
385
386 static struct dentry *dsidir;
387
388 static void tegra_dc_dsi_debug_create(struct tegra_dc_dsi_data *dsi)
389 {
390         struct dentry *retval;
391
392         dsidir = debugfs_create_dir("tegra_dsi", NULL);
393         if (!dsidir)
394                 return;
395         retval = debugfs_create_file("regs", S_IRUGO, dsidir, dsi,
396                 &dbg_fops);
397         if (!retval)
398                 goto free_out;
399         return;
400 free_out:
401         debugfs_remove_recursive(dsidir);
402         dsidir = NULL;
403         return;
404 }
405 #else
406 static inline void tegra_dc_dsi_debug_create(struct tegra_dc_dsi_data *dsi)
407 { }
408 #endif
409
410 static inline void tegra_dsi_clk_enable(struct tegra_dc_dsi_data *dsi)
411 {
412         if (!tegra_is_clk_enabled(dsi->dsi_clk))
413                 clk_prepare_enable(dsi->dsi_clk);
414 }
415
416 static inline void tegra_dsi_clk_disable(struct tegra_dc_dsi_data *dsi)
417 {
418         if (tegra_is_clk_enabled(dsi->dsi_clk))
419                 clk_disable_unprepare(dsi->dsi_clk);
420 }
421
422 static void __maybe_unused tegra_dsi_syncpt_reset(
423                                 struct tegra_dc_dsi_data *dsi)
424 {
425         tegra_dsi_writel(dsi, 0x1, DSI_INCR_SYNCPT_CNTRL);
426         /* stabilization delay */
427         udelay(300);
428         tegra_dsi_writel(dsi, 0x0, DSI_INCR_SYNCPT_CNTRL);
429         /* stabilization delay */
430         udelay(300);
431 }
432
433 static int __maybe_unused tegra_dsi_syncpt(struct tegra_dc_dsi_data *dsi)
434 {
435         u32 val;
436         int ret = 0;
437
438         dsi->syncpt_val = nvhost_syncpt_read_ext(dsi->dc->ndev, dsi->syncpt_id);
439
440         val = DSI_INCR_SYNCPT_COND(OP_DONE) |
441                 DSI_INCR_SYNCPT_INDX(dsi->syncpt_id);
442         tegra_dsi_writel(dsi, val, DSI_INCR_SYNCPT);
443
444         ret = nvhost_syncpt_wait_timeout_ext(dsi->dc->ndev, dsi->syncpt_id,
445                 dsi->syncpt_val + 1, MAX_SCHEDULE_TIMEOUT, NULL, NULL);
446         if (ret < 0) {
447                 dev_err(&dsi->dc->ndev->dev, "DSI sync point failure\n");
448                 goto fail;
449         }
450
451         (dsi->syncpt_val)++;
452         return 0;
453 fail:
454         return ret;
455 }
456
457 static u32 tegra_dsi_get_hs_clk_rate(struct tegra_dc_dsi_data *dsi)
458 {
459         u32 dsi_clock_rate_khz;
460
461         switch (dsi->info.video_burst_mode) {
462         case TEGRA_DSI_VIDEO_BURST_MODE_LOW_SPEED:
463         case TEGRA_DSI_VIDEO_BURST_MODE_MEDIUM_SPEED:
464         case TEGRA_DSI_VIDEO_BURST_MODE_FAST_SPEED:
465         case TEGRA_DSI_VIDEO_BURST_MODE_FASTEST_SPEED:
466                 /* Calculate DSI HS clock rate for DSI burst mode */
467                 dsi_clock_rate_khz = dsi->default_pixel_clk_khz *
468                                         dsi->shift_clk_div.mul /
469                                         dsi->shift_clk_div.div;
470                 break;
471         case TEGRA_DSI_VIDEO_NONE_BURST_MODE:
472         case TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END:
473         case TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED:
474         default:
475                 /* Clock rate is default DSI clock rate for non-burst mode */
476                 dsi_clock_rate_khz = dsi->default_hs_clk_khz;
477                 break;
478         }
479
480         return dsi_clock_rate_khz;
481 }
482
483 static u32 tegra_dsi_get_lp_clk_rate(struct tegra_dc_dsi_data *dsi, u8 lp_op)
484 {
485         u32 dsi_clock_rate_khz;
486
487         if (dsi->info.enable_hs_clock_on_lp_cmd_mode)
488                 if (dsi->info.hs_clk_in_lp_cmd_mode_freq_khz)
489                         dsi_clock_rate_khz =
490                                 dsi->info.hs_clk_in_lp_cmd_mode_freq_khz;
491                 else
492                         dsi_clock_rate_khz = tegra_dsi_get_hs_clk_rate(dsi);
493         else
494                 if (lp_op == DSI_LP_OP_READ)
495                         dsi_clock_rate_khz =
496                                 dsi->info.lp_read_cmd_mode_freq_khz;
497                 else
498                         dsi_clock_rate_khz =
499                                 dsi->info.lp_cmd_mode_freq_khz;
500
501         return dsi_clock_rate_khz;
502 }
503
504 static struct tegra_dc_shift_clk_div tegra_dsi_get_shift_clk_div(
505                                                 struct tegra_dc_dsi_data *dsi)
506 {
507         struct tegra_dc_shift_clk_div shift_clk_div;
508         struct tegra_dc_shift_clk_div max_shift_clk_div;
509         u32 temp_lcm;
510         u32 burst_width;
511         u32 burst_width_max;
512
513         /* Get the real value of default shift_clk_div. default_shift_clk_div
514          * holds the real value of shift_clk_div.
515          */
516         shift_clk_div = dsi->default_shift_clk_div;
517
518         /* Calculate shift_clk_div which can match the video_burst_mode. */
519         if (dsi->info.video_burst_mode >=
520                         TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED) {
521                 if (dsi->info.max_panel_freq_khz >= dsi->default_hs_clk_khz) {
522                         /* formula:
523                          * dsi->info.max_panel_freq_khz * shift_clk_div /
524                          * dsi->default_hs_clk_khz
525                          */
526                         max_shift_clk_div.mul = dsi->info.max_panel_freq_khz *
527                                                 shift_clk_div.mul;
528                         max_shift_clk_div.div = dsi->default_hs_clk_khz *
529                                                 dsi->default_shift_clk_div.div;
530                 } else {
531                         max_shift_clk_div = shift_clk_div;
532                 }
533
534                 burst_width = dsi->info.video_burst_mode
535                                 - TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED;
536                 burst_width_max = TEGRA_DSI_VIDEO_BURST_MODE_FASTEST_SPEED
537                                 - TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED;
538
539                 /* formula:
540                  * (max_shift_clk_div - shift_clk_div) *
541                  * burst_width / burst_width_max
542                  */
543                 temp_lcm = lcm(max_shift_clk_div.div, shift_clk_div.div);
544                 shift_clk_div.mul = (max_shift_clk_div.mul * temp_lcm /
545                                         max_shift_clk_div.div -
546                                         shift_clk_div.mul * temp_lcm /
547                                         shift_clk_div.div)*
548                                         burst_width;
549                 shift_clk_div.div = temp_lcm * burst_width_max;
550         }
551
552         return shift_clk_div;
553 }
554
555 static void tegra_dsi_pix_correction(struct tegra_dc *dc,
556                                         struct tegra_dc_dsi_data *dsi)
557 {
558         u32 h_width_pixels;
559         u32 h_act_corr = 0;
560         u32 hfp_corr = 0;
561         u32 temp = 0;
562
563         h_width_pixels = dc->mode.h_back_porch + dc->mode.h_front_porch +
564                         dc->mode.h_sync_width + dc->mode.h_active;
565
566         if (dsi->info.ganged_type == TEGRA_DSI_GANGED_SYMMETRIC_EVEN_ODD) {
567                 temp = dc->mode.h_active % dsi->info.n_data_lanes;
568                 if (temp) {
569                         h_act_corr = dsi->info.n_data_lanes - temp;
570                         h_width_pixels += h_act_corr;
571                 }
572         }
573
574         temp = h_width_pixels % dsi->info.n_data_lanes;
575         if (temp) {
576                 hfp_corr = dsi->info.n_data_lanes - temp;
577                 h_width_pixels += hfp_corr;
578         }
579
580         while (1) {
581                 temp = (h_width_pixels * dsi->pixel_scaler_mul /
582                         dsi->pixel_scaler_div) % dsi->info.n_data_lanes;
583                 if (temp) {
584                         hfp_corr += dsi->info.n_data_lanes;
585                         h_width_pixels += dsi->info.n_data_lanes;
586                 }
587                 else
588                         break;
589         }
590
591         dc->mode.h_front_porch += hfp_corr;
592         dc->mode.h_active += h_act_corr;
593 }
594
595 static void tegra_dsi_init_sw(struct tegra_dc *dc,
596                         struct tegra_dc_dsi_data *dsi)
597 {
598         u32 h_width_pixels;
599         u32 v_width_lines;
600         u32 pixel_clk_hz;
601         u32 byte_clk_hz;
602         u32 plld_clk_mhz;
603         u8 n_data_lanes;
604
605         switch (dsi->info.pixel_format) {
606         case TEGRA_DSI_PIXEL_FORMAT_16BIT_P:
607                 /* 2 bytes per pixel */
608                 dsi->pixel_scaler_mul = 2;
609                 dsi->pixel_scaler_div = 1;
610                 break;
611         case TEGRA_DSI_PIXEL_FORMAT_18BIT_P:
612                 /* 2.25 bytes per pixel */
613                 dsi->pixel_scaler_mul = 9;
614                 dsi->pixel_scaler_div = 4;
615                 break;
616         case TEGRA_DSI_PIXEL_FORMAT_18BIT_NP:
617         case TEGRA_DSI_PIXEL_FORMAT_24BIT_P:
618                 /* 3 bytes per pixel */
619                 dsi->pixel_scaler_mul = 3;
620                 dsi->pixel_scaler_div = 1;
621                 break;
622         default:
623                 break;
624         }
625
626         dsi->ulpm = false;
627         dsi->enabled = false;
628         dsi->clk_ref = false;
629
630         if (dsi->info.ganged_type == TEGRA_DSI_GANGED_SYMMETRIC_LEFT_RIGHT ||
631                 dsi->info.ganged_type == TEGRA_DSI_GANGED_SYMMETRIC_EVEN_ODD)
632                 n_data_lanes = dsi->info.n_data_lanes / 2;
633
634         dsi->dsi_control_val =
635                         DSI_CONTROL_VIRTUAL_CHANNEL(dsi->info.virtual_channel) |
636                         DSI_CONTROL_NUM_DATA_LANES(dsi->info.n_data_lanes - 1) |
637                         DSI_CONTROL_VID_SOURCE(dc->ndev->id) |
638                         DSI_CONTROL_DATA_FORMAT(dsi->info.pixel_format);
639
640         if (dsi->info.ganged_type)
641                 tegra_dsi_pix_correction(dc, dsi);
642
643         /* Below we are going to calculate dsi and dc clock rate.
644          * Calcuate the horizontal and vertical width.
645          */
646         h_width_pixels = dc->mode.h_back_porch + dc->mode.h_front_porch +
647                         dc->mode.h_sync_width + dc->mode.h_active;
648
649         v_width_lines = dc->mode.v_back_porch + dc->mode.v_front_porch +
650                         dc->mode.v_sync_width + dc->mode.v_active;
651
652         /* Calculate minimum required pixel rate. */
653         pixel_clk_hz = h_width_pixels * v_width_lines * dsi->info.refresh_rate;
654         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE) {
655                 if (dsi->info.rated_refresh_rate >= dsi->info.refresh_rate)
656                         dev_info(&dc->ndev->dev, "DSI: measured refresh rate "
657                                 "should be larger than rated refresh rate.\n");
658                 dc->mode.rated_pclk = h_width_pixels * v_width_lines *
659                                                 dsi->info.rated_refresh_rate;
660         }
661
662         /* Calculate minimum byte rate on DSI interface. */
663         byte_clk_hz = (pixel_clk_hz * dsi->pixel_scaler_mul) /
664                         (dsi->pixel_scaler_div * dsi->info.n_data_lanes);
665
666         /* Round up to multiple of mega hz. */
667         plld_clk_mhz = DIV_ROUND_UP((byte_clk_hz * NUMOF_BIT_PER_BYTE),
668                                                                 1000000);
669
670         /* Calculate default real shift_clk_div. */
671         dsi->default_shift_clk_div.mul = NUMOF_BIT_PER_BYTE *
672                                         dsi->pixel_scaler_mul;
673         dsi->default_shift_clk_div.div = 2 * dsi->pixel_scaler_div *
674                                         dsi->info.n_data_lanes;
675
676         /* Calculate default DSI hs clock. DSI interface is double data rate.
677          * Data is transferred on both rising and falling edge of clk, div by 2
678          * to get the actual clock rate.
679          */
680         dsi->default_hs_clk_khz = plld_clk_mhz * 1000 / 2;
681
682         dsi->default_pixel_clk_khz = (plld_clk_mhz * 1000 *
683                                         dsi->default_shift_clk_div.div) /
684                                         (2 * dsi->default_shift_clk_div.mul);
685
686         /* Get the actual shift_clk_div and clock rates. */
687         dsi->shift_clk_div = tegra_dsi_get_shift_clk_div(dsi);
688         dsi->target_lp_clk_khz =
689                         tegra_dsi_get_lp_clk_rate(dsi, DSI_LP_OP_WRITE);
690         dsi->target_hs_clk_khz = tegra_dsi_get_hs_clk_rate(dsi);
691
692         dev_info(&dc->ndev->dev, "DSI: HS clock rate is %d\n",
693                                         dsi->target_hs_clk_khz);
694
695 #if DSI_USE_SYNC_POINTS
696         dsi->syncpt_id = NVSYNCPT_DSI;
697 #endif
698
699         /*
700          * Force video clock to be continuous mode if
701          * enable_hs_clock_on_lp_cmd_mode is set
702          */
703         if (dsi->info.enable_hs_clock_on_lp_cmd_mode) {
704                 if (dsi->info.video_clock_mode !=
705                                         TEGRA_DSI_VIDEO_CLOCK_CONTINUOUS)
706                         dev_warn(&dc->ndev->dev,
707                                 "Force clock continuous mode\n");
708
709                 dsi->info.video_clock_mode = TEGRA_DSI_VIDEO_CLOCK_CONTINUOUS;
710         }
711
712         atomic_set(&dsi->host_ref, 0);
713         dsi->host_suspended = false;
714         mutex_init(&dsi->host_lock);
715         init_completion(&dc->out->user_vblank_comp);
716         INIT_DELAYED_WORK(&dsi->idle_work, tegra_dc_dsi_idle_work);
717         dsi->idle_delay = msecs_to_jiffies(DSI_HOST_IDLE_PERIOD);
718 }
719
720 #define SELECT_T_PHY(platform_t_phy_ns, default_phy, clk_ns, hw_inc) ( \
721 (platform_t_phy_ns) ? ( \
722 ((DSI_CONVERT_T_PHY_NS_TO_T_PHY(platform_t_phy_ns, clk_ns, hw_inc)) < 0 ? 0 : \
723 (DSI_CONVERT_T_PHY_NS_TO_T_PHY(platform_t_phy_ns, clk_ns, hw_inc)))) : \
724 ((default_phy) < 0 ? 0 : (default_phy)))
725
726 static void tegra_dsi_get_clk_phy_timing(struct tegra_dc_dsi_data *dsi,
727                 struct dsi_phy_timing_inclk *phy_timing_clk, u32 clk_ns)
728 {
729         phy_timing_clk->t_tlpx = SELECT_T_PHY(
730                 dsi->info.phy_timing.t_tlpx_ns,
731                 T_TLPX_DEFAULT(clk_ns), clk_ns, T_TLPX_HW_INC);
732
733         phy_timing_clk->t_clktrail = SELECT_T_PHY(
734                 dsi->info.phy_timing.t_clktrail_ns,
735                 T_CLKTRAIL_DEFAULT(clk_ns), clk_ns, T_CLKTRAIL_HW_INC);
736
737         phy_timing_clk->t_clkpost = SELECT_T_PHY(
738                 dsi->info.phy_timing.t_clkpost_ns,
739                 T_CLKPOST_DEFAULT(clk_ns), clk_ns, T_CLKPOST_HW_INC);
740
741         phy_timing_clk->t_clkzero = SELECT_T_PHY(
742                 dsi->info.phy_timing.t_clkzero_ns,
743                 T_CLKZERO_DEFAULT(clk_ns), clk_ns, T_CLKZERO_HW_INC);
744
745         phy_timing_clk->t_clkprepare = SELECT_T_PHY(
746                 dsi->info.phy_timing.t_clkprepare_ns,
747                 T_CLKPREPARE_DEFAULT(clk_ns), clk_ns, T_CLKPREPARE_HW_INC);
748
749         phy_timing_clk->t_clkpre = SELECT_T_PHY(
750                 dsi->info.phy_timing.t_clkpre_ns,
751                 T_CLKPRE_DEFAULT, clk_ns, T_CLKPRE_HW_INC);
752 }
753
754 static void tegra_dsi_get_hs_phy_timing(struct tegra_dc_dsi_data *dsi,
755                 struct dsi_phy_timing_inclk *phy_timing_clk, u32 clk_ns)
756 {
757         phy_timing_clk->t_tlpx = SELECT_T_PHY(
758                 dsi->info.phy_timing.t_tlpx_ns,
759                 T_TLPX_DEFAULT(clk_ns), clk_ns, T_TLPX_HW_INC);
760
761         phy_timing_clk->t_hsdexit = SELECT_T_PHY(
762                 dsi->info.phy_timing.t_hsdexit_ns,
763                 T_HSEXIT_DEFAULT(clk_ns), clk_ns, T_HSEXIT_HW_INC);
764
765         phy_timing_clk->t_hstrail = SELECT_T_PHY(
766                 dsi->info.phy_timing.t_hstrail_ns,
767                 T_HSTRAIL_DEFAULT(clk_ns), clk_ns, T_HSTRAIL_HW_INC);
768
769         phy_timing_clk->t_datzero = SELECT_T_PHY(
770                 dsi->info.phy_timing.t_datzero_ns,
771                 T_DATZERO_DEFAULT(clk_ns), clk_ns, T_DATZERO_HW_INC);
772
773         phy_timing_clk->t_hsprepare = SELECT_T_PHY(
774                 dsi->info.phy_timing.t_hsprepare_ns,
775                 T_HSPREPARE_DEFAULT(clk_ns), clk_ns, T_HSPREPARE_HW_INC);
776 }
777
778 static void tegra_dsi_get_escape_phy_timing(struct tegra_dc_dsi_data *dsi,
779                 struct dsi_phy_timing_inclk *phy_timing_clk, u32 clk_ns)
780 {
781         phy_timing_clk->t_tlpx = SELECT_T_PHY(
782                 dsi->info.phy_timing.t_tlpx_ns,
783                 T_TLPX_DEFAULT(clk_ns), clk_ns, T_TLPX_HW_INC);
784 }
785
786 static void tegra_dsi_get_bta_phy_timing(struct tegra_dc_dsi_data *dsi,
787                 struct dsi_phy_timing_inclk *phy_timing_clk, u32 clk_ns)
788 {
789         phy_timing_clk->t_tlpx = SELECT_T_PHY(
790                 dsi->info.phy_timing.t_tlpx_ns,
791                 T_TLPX_DEFAULT(clk_ns), clk_ns, T_TLPX_HW_INC);
792
793         phy_timing_clk->t_taget = SELECT_T_PHY(
794                 dsi->info.phy_timing.t_taget_ns,
795                 T_TAGET_DEFAULT(clk_ns), clk_ns, T_TAGET_HW_INC);
796
797         phy_timing_clk->t_tasure = SELECT_T_PHY(
798                 dsi->info.phy_timing.t_tasure_ns,
799                 T_TASURE_DEFAULT(clk_ns), clk_ns, T_TASURE_HW_INC);
800
801         phy_timing_clk->t_tago = SELECT_T_PHY(
802                 dsi->info.phy_timing.t_tago_ns,
803                 T_TAGO_DEFAULT(clk_ns), clk_ns, T_TAGO_HW_INC);
804 }
805
806 static void tegra_dsi_get_ulps_phy_timing(struct tegra_dc_dsi_data *dsi,
807                 struct dsi_phy_timing_inclk *phy_timing_clk, u32 clk_ns)
808 {
809         phy_timing_clk->t_tlpx = SELECT_T_PHY(
810                 dsi->info.phy_timing.t_tlpx_ns,
811                 T_TLPX_DEFAULT(clk_ns), clk_ns, T_TLPX_HW_INC);
812
813         phy_timing_clk->t_wakeup = SELECT_T_PHY(
814                 dsi->info.phy_timing.t_wakeup_ns,
815                 T_WAKEUP_DEFAULT, clk_ns, T_WAKEUP_HW_INC);
816 }
817
818 #undef SELECT_T_PHY
819
820 static void tegra_dsi_get_phy_timing(struct tegra_dc_dsi_data *dsi,
821                                 struct dsi_phy_timing_inclk *phy_timing_clk,
822                                 u32 clk_ns, u8 lphs)
823 {
824         if (tegra_platform_is_fpga() && !(dsi->info.ganged_type)) {
825                 clk_ns = (1000 * 1000) / (dsi->info.fpga_freq_khz ?
826                         dsi->info.fpga_freq_khz : DEFAULT_FPGA_FREQ_KHZ);
827         }
828
829         phy_timing_clk->t_hsdexit = dsi->info.phy_timing.t_hsdexit_ns ?
830                         (dsi->info.phy_timing.t_hsdexit_ns / clk_ns) :
831                         (T_HSEXIT_DEFAULT(clk_ns));
832
833         if (lphs == DSI_LPHS_IN_HS_MODE) {
834                 tegra_dsi_get_clk_phy_timing(dsi, phy_timing_clk, clk_ns);
835                 tegra_dsi_get_hs_phy_timing(dsi, phy_timing_clk, clk_ns);
836         } else {
837                 /* default is LP mode */
838                 tegra_dsi_get_escape_phy_timing(dsi, phy_timing_clk, clk_ns);
839                 tegra_dsi_get_bta_phy_timing(dsi, phy_timing_clk, clk_ns);
840                 tegra_dsi_get_ulps_phy_timing(dsi, phy_timing_clk, clk_ns);
841                 if (dsi->info.enable_hs_clock_on_lp_cmd_mode)
842                         tegra_dsi_get_clk_phy_timing(dsi, phy_timing_clk, clk_ns);
843         }
844 }
845
846 static int tegra_dsi_mipi_phy_timing_range(struct tegra_dc_dsi_data *dsi,
847                                 struct dsi_phy_timing_inclk *phy_timing,
848                                 u32 clk_ns, u8 lphs)
849 {
850         int err = 0;
851
852 #define CHECK_RANGE(val, min, max) ( \
853                 ((min) == NOT_DEFINED ? 0 : (val) < (min)) || \
854                 ((max) == NOT_DEFINED ? 0 : (val) > (max)) ? -EINVAL : 0)
855
856         if (tegra_platform_is_fpga())
857                 clk_ns = dsi->info.fpga_freq_khz ?
858                         ((1000 * 1000) / dsi->info.fpga_freq_khz) :
859                         DEFAULT_FPGA_FREQ_KHZ;
860
861         err = CHECK_RANGE(
862         DSI_CONVERT_T_PHY_TO_T_PHY_NS(
863                         phy_timing->t_tlpx, clk_ns, T_TLPX_HW_INC),
864                         MIPI_T_TLPX_NS_MIN, MIPI_T_TLPX_NS_MAX);
865         if (err < 0) {
866                 dev_warn(&dsi->dc->ndev->dev,
867                         "dsi: Tlpx mipi range violated\n");
868                 goto fail;
869         }
870
871         if (lphs == DSI_LPHS_IN_HS_MODE) {
872                 err = CHECK_RANGE(
873                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
874                         phy_timing->t_hsdexit, clk_ns, T_HSEXIT_HW_INC),
875                         MIPI_T_HSEXIT_NS_MIN, MIPI_T_HSEXIT_NS_MAX);
876                 if (err < 0) {
877                         dev_warn(&dsi->dc->ndev->dev,
878                                 "dsi: HsExit mipi range violated\n");
879                         goto fail;
880                 }
881
882                 err = CHECK_RANGE(
883                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
884                         phy_timing->t_hstrail, clk_ns, T_HSTRAIL_HW_INC),
885                         MIPI_T_HSTRAIL_NS_MIN(clk_ns), MIPI_T_HSTRAIL_NS_MAX);
886                 if (err < 0) {
887                         dev_warn(&dsi->dc->ndev->dev,
888                                 "dsi: HsTrail mipi range violated\n");
889                         goto fail;
890                 }
891
892                 err = CHECK_RANGE(
893                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
894                         phy_timing->t_datzero, clk_ns, T_DATZERO_HW_INC),
895                         MIPI_T_HSZERO_NS_MIN, MIPI_T_HSZERO_NS_MAX);
896                 if (err < 0) {
897                         dev_warn(&dsi->dc->ndev->dev,
898                                 "dsi: HsZero mipi range violated\n");
899                         goto fail;
900                 }
901
902                 err = CHECK_RANGE(
903                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
904                         phy_timing->t_hsprepare, clk_ns, T_HSPREPARE_HW_INC),
905                         MIPI_T_HSPREPARE_NS_MIN(clk_ns),
906                         MIPI_T_HSPREPARE_NS_MAX(clk_ns));
907                 if (err < 0) {
908                         dev_warn(&dsi->dc->ndev->dev,
909                                 "dsi: HsPrepare mipi range violated\n");
910                         goto fail;
911                 }
912
913                 err = CHECK_RANGE(
914                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
915                 phy_timing->t_hsprepare, clk_ns, T_HSPREPARE_HW_INC) +
916                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
917                         phy_timing->t_datzero, clk_ns, T_DATZERO_HW_INC),
918                         MIPI_T_HSPREPARE_ADD_HSZERO_NS_MIN(clk_ns),
919                         MIPI_T_HSPREPARE_ADD_HSZERO_NS_MAX);
920                 if (err < 0) {
921                         dev_warn(&dsi->dc->ndev->dev,
922                         "dsi: HsPrepare + HsZero mipi range violated\n");
923                         goto fail;
924                 }
925         } else {
926                 /* default is LP mode */
927                 err = CHECK_RANGE(
928                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
929                         phy_timing->t_wakeup, clk_ns, T_WAKEUP_HW_INC),
930                         MIPI_T_WAKEUP_NS_MIN, MIPI_T_WAKEUP_NS_MAX);
931                 if (err < 0) {
932                         dev_warn(&dsi->dc->ndev->dev,
933                                 "dsi: WakeUp mipi range violated\n");
934                         goto fail;
935                 }
936
937                 err = CHECK_RANGE(
938                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
939                         phy_timing->t_tasure, clk_ns, T_TASURE_HW_INC),
940                         MIPI_T_TASURE_NS_MIN(DSI_CONVERT_T_PHY_TO_T_PHY_NS(
941                         phy_timing->t_tlpx, clk_ns, T_TLPX_HW_INC)),
942                         MIPI_T_TASURE_NS_MAX(DSI_CONVERT_T_PHY_TO_T_PHY_NS(
943                         phy_timing->t_tlpx, clk_ns, T_TLPX_HW_INC)));
944                 if (err < 0) {
945                         dev_warn(&dsi->dc->ndev->dev,
946                                 "dsi: TaSure mipi range violated\n");
947                         goto fail;
948                 }
949         }
950
951         if (lphs == DSI_LPHS_IN_HS_MODE ||
952                 dsi->info.enable_hs_clock_on_lp_cmd_mode) {
953                 err = CHECK_RANGE(
954                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
955                         phy_timing->t_clktrail, clk_ns, T_CLKTRAIL_HW_INC),
956                         MIPI_T_CLKTRAIL_NS_MIN, MIPI_T_CLKTRAIL_NS_MAX);
957                 if (err < 0) {
958                         dev_warn(&dsi->dc->ndev->dev,
959                                 "dsi: ClkTrail mipi range violated\n");
960                         goto fail;
961                 }
962
963                 err = CHECK_RANGE(
964                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
965                         phy_timing->t_clkpost, clk_ns, T_CLKPOST_HW_INC),
966                         MIPI_T_CLKPOST_NS_MIN(clk_ns), MIPI_T_CLKPOST_NS_MAX);
967                 if (err < 0) {
968                         dev_warn(&dsi->dc->ndev->dev,
969                                 "dsi: ClkPost mipi range violated\n");
970                         goto fail;
971                 }
972
973                 err = CHECK_RANGE(
974                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
975                         phy_timing->t_clkzero, clk_ns, T_CLKZERO_HW_INC),
976                         MIPI_T_CLKZERO_NS_MIN, MIPI_T_CLKZERO_NS_MAX);
977                 if (err < 0) {
978                         dev_warn(&dsi->dc->ndev->dev,
979                                 "dsi: ClkZero mipi range violated\n");
980                         goto fail;
981                 }
982
983                 err = CHECK_RANGE(
984                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
985                         phy_timing->t_clkprepare, clk_ns, T_CLKPREPARE_HW_INC),
986                         MIPI_T_CLKPREPARE_NS_MIN, MIPI_T_CLKPREPARE_NS_MAX);
987                 if (err < 0) {
988                         dev_warn(&dsi->dc->ndev->dev,
989                                 "dsi: ClkPrepare mipi range violated\n");
990                         goto fail;
991                 }
992
993                 err = CHECK_RANGE(
994                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
995                         phy_timing->t_clkpre, clk_ns, T_CLKPRE_HW_INC),
996                         MIPI_T_CLKPRE_NS_MIN, MIPI_T_CLKPRE_NS_MAX);
997                 if (err < 0) {
998                         dev_warn(&dsi->dc->ndev->dev,
999                                 "dsi: ClkPre mipi range violated\n");
1000                         goto fail;
1001                 }
1002
1003                 err = CHECK_RANGE(
1004                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1005                 phy_timing->t_clkprepare, clk_ns, T_CLKPREPARE_HW_INC) +
1006                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1007                         phy_timing->t_clkzero, clk_ns, T_CLKZERO_HW_INC),
1008                         MIPI_T_CLKPREPARE_ADD_CLKZERO_NS_MIN,
1009                         MIPI_T_CLKPREPARE_ADD_CLKZERO_NS_MAX);
1010                 if (err < 0) {
1011                         dev_warn(&dsi->dc->ndev->dev,
1012                         "dsi: ClkPrepare + ClkZero mipi range violated\n");
1013                         goto fail;
1014                 }
1015         }
1016 fail:
1017 #undef CHECK_RANGE
1018         return err;
1019 }
1020
1021 static int tegra_dsi_hs_phy_len(struct tegra_dc_dsi_data *dsi,
1022                                 struct dsi_phy_timing_inclk *phy_timing,
1023                                 u32 clk_ns, u8 lphs)
1024 {
1025         u32 hs_t_phy_ns = 0;
1026         u32 clk_t_phy_ns = 0;
1027         u32 t_phy_ns;
1028         u32 h_blank_ns;
1029         struct tegra_dc_mode *modes;
1030         u32 t_pix_ns;
1031         int err = 0;
1032
1033         if (!(lphs == DSI_LPHS_IN_HS_MODE))
1034                 goto fail;
1035
1036         if (dsi->info.video_data_type ==
1037                 TEGRA_DSI_VIDEO_TYPE_VIDEO_MODE &&
1038                 dsi->info.video_burst_mode <=
1039                 TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END)
1040                 goto fail;
1041
1042         modes = dsi->dc->out->modes;
1043         t_pix_ns = clk_ns * BITS_PER_BYTE *
1044                 dsi->pixel_scaler_mul / dsi->pixel_scaler_div;
1045
1046         hs_t_phy_ns =
1047                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1048                 phy_timing->t_tlpx, clk_ns, T_TLPX_HW_INC) +
1049                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1050                 phy_timing->t_tlpx, clk_ns, T_TLPX_HW_INC) +
1051                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1052                 phy_timing->t_hsprepare, clk_ns, T_HSPREPARE_HW_INC) +
1053                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1054                 phy_timing->t_datzero, clk_ns, T_DATZERO_HW_INC) +
1055                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1056                 phy_timing->t_hstrail, clk_ns, T_HSTRAIL_HW_INC) +
1057                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1058                 phy_timing->t_hsdexit, clk_ns, T_HSEXIT_HW_INC);
1059
1060         if (dsi->info.video_clock_mode == TEGRA_DSI_VIDEO_CLOCK_TX_ONLY) {
1061                 clk_t_phy_ns =
1062                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1063                 phy_timing->t_clkpost, clk_ns, T_CLKPOST_HW_INC) +
1064                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1065                 phy_timing->t_clktrail, clk_ns, T_CLKTRAIL_HW_INC) +
1066                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1067                 phy_timing->t_hsdexit, clk_ns, T_HSEXIT_HW_INC) +
1068                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1069                 phy_timing->t_tlpx, clk_ns, T_TLPX_HW_INC) +
1070                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1071                 phy_timing->t_clkprepare, clk_ns, T_CLKPREPARE_HW_INC) +
1072                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1073                 phy_timing->t_clkzero, clk_ns, T_CLKZERO_HW_INC) +
1074                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1075                 phy_timing->t_clkpre, clk_ns, T_CLKPRE_HW_INC);
1076
1077                 /* clk_pre overlaps LP-11 hs mode start sequence */
1078                 hs_t_phy_ns -= DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1079                         phy_timing->t_tlpx, clk_ns, T_TLPX_HW_INC);
1080         }
1081
1082         h_blank_ns = t_pix_ns * (modes->h_sync_width + modes->h_back_porch +
1083                                                 modes->h_front_porch);
1084
1085         /* Extra tlpx and byte cycle required by dsi HW */
1086         t_phy_ns = dsi->info.n_data_lanes * (hs_t_phy_ns + clk_t_phy_ns +
1087                 DSI_CONVERT_T_PHY_TO_T_PHY_NS(
1088                 phy_timing->t_tlpx, clk_ns, T_TLPX_HW_INC) +
1089                 clk_ns * BITS_PER_BYTE);
1090
1091         if (h_blank_ns < t_phy_ns) {
1092                 err = -EINVAL;
1093                 dev_WARN(&dsi->dc->ndev->dev,
1094                         "dsi: Hblank is smaller than HS trans phy timing\n");
1095                 goto fail;
1096         }
1097
1098         return 0;
1099 fail:
1100         return err;
1101 }
1102
1103 static int tegra_dsi_constraint_phy_timing(struct tegra_dc_dsi_data *dsi,
1104                                 struct dsi_phy_timing_inclk *phy_timing,
1105                                 u32 clk_ns, u8 lphs)
1106 {
1107         int err = 0;
1108
1109         err = tegra_dsi_mipi_phy_timing_range(dsi, phy_timing, clk_ns, lphs);
1110         if (err < 0) {
1111                 dev_warn(&dsi->dc->ndev->dev, "dsi: mipi range violated\n");
1112                 goto fail;
1113         }
1114
1115         err = tegra_dsi_hs_phy_len(dsi, phy_timing, clk_ns, lphs);
1116         if (err < 0) {
1117                 dev_err(&dsi->dc->ndev->dev, "dsi: Hblank too short\n");
1118                 goto fail;
1119         }
1120
1121         /* TODO: add more contraints */
1122 fail:
1123         return err;
1124 }
1125
1126 static void tegra_dsi_set_phy_timing(struct tegra_dc_dsi_data *dsi, u8 lphs)
1127 {
1128         u32 val;
1129         struct dsi_phy_timing_inclk phy_timing = dsi->phy_timing;
1130
1131         tegra_dsi_get_phy_timing
1132                 (dsi, &phy_timing, dsi->current_bit_clk_ns, lphs);
1133
1134         tegra_dsi_constraint_phy_timing(dsi, &phy_timing,
1135                                         dsi->current_bit_clk_ns, lphs);
1136
1137         if (tegra_platform_is_fpga() && dsi->info.ganged_type) {
1138                 phy_timing.t_hsdexit += T_HSEXIT_HW_INC;
1139                 phy_timing.t_hstrail += T_HSTRAIL_HW_INC + 3;
1140                 phy_timing.t_datzero += T_DATZERO_HW_INC;
1141                 phy_timing.t_hsprepare += T_HSPREPARE_HW_INC;
1142
1143                 phy_timing.t_clktrail += T_CLKTRAIL_HW_INC;
1144                 phy_timing.t_clkpost += T_CLKPOST_HW_INC;
1145                 phy_timing.t_clkzero += T_CLKZERO_HW_INC;
1146                 phy_timing.t_tlpx += T_TLPX_HW_INC;
1147
1148                 phy_timing.t_clkprepare += T_CLKPREPARE_HW_INC;
1149                 phy_timing.t_clkpre += T_CLKPRE_HW_INC;
1150                 phy_timing.t_wakeup += T_WAKEUP_HW_INC;
1151
1152                 phy_timing.t_taget += T_TAGET_HW_INC;
1153                 phy_timing.t_tasure += T_TASURE_HW_INC;
1154                 phy_timing.t_tago += T_TAGO_HW_INC;
1155         }
1156         val = DSI_PHY_TIMING_0_THSDEXIT(phy_timing.t_hsdexit) |
1157                         DSI_PHY_TIMING_0_THSTRAIL(phy_timing.t_hstrail) |
1158                         DSI_PHY_TIMING_0_TDATZERO(phy_timing.t_datzero) |
1159                         DSI_PHY_TIMING_0_THSPREPR(phy_timing.t_hsprepare);
1160         tegra_dsi_writel(dsi, val, DSI_PHY_TIMING_0);
1161
1162         val = DSI_PHY_TIMING_1_TCLKTRAIL(phy_timing.t_clktrail) |
1163                         DSI_PHY_TIMING_1_TCLKPOST(phy_timing.t_clkpost) |
1164                         DSI_PHY_TIMING_1_TCLKZERO(phy_timing.t_clkzero) |
1165                         DSI_PHY_TIMING_1_TTLPX(phy_timing.t_tlpx);
1166         tegra_dsi_writel(dsi, val, DSI_PHY_TIMING_1);
1167
1168         val = DSI_PHY_TIMING_2_TCLKPREPARE(phy_timing.t_clkprepare) |
1169                 DSI_PHY_TIMING_2_TCLKPRE(phy_timing.t_clkpre) |
1170                         DSI_PHY_TIMING_2_TWAKEUP(phy_timing.t_wakeup);
1171         tegra_dsi_writel(dsi, val, DSI_PHY_TIMING_2);
1172
1173         val = DSI_BTA_TIMING_TTAGET(phy_timing.t_taget) |
1174                         DSI_BTA_TIMING_TTASURE(phy_timing.t_tasure) |
1175                         DSI_BTA_TIMING_TTAGO(phy_timing.t_tago);
1176         tegra_dsi_writel(dsi, val, DSI_BTA_TIMING);
1177
1178         dsi->phy_timing = phy_timing;
1179 }
1180
1181 static u32 tegra_dsi_sol_delay_burst(struct tegra_dc *dc,
1182                                 struct tegra_dc_dsi_data *dsi)
1183 {
1184         u32 dsi_to_pixel_clk_ratio;
1185         u32 temp;
1186         u32 temp1;
1187         u32 mipi_clk_adj_kHz = 0;
1188         u32 sol_delay;
1189         struct tegra_dc_mode *dc_modes = &dc->mode;
1190
1191         /* Get Fdsi/Fpixel ration (note: Fdsi is in bit format) */
1192         dsi_to_pixel_clk_ratio = (dsi->current_dsi_clk_khz * 2 +
1193                 dsi->default_pixel_clk_khz - 1) / dsi->default_pixel_clk_khz;
1194
1195         /* Convert Fdsi to byte format */
1196         dsi_to_pixel_clk_ratio *= 1000/8;
1197
1198         /* Multiplying by 1000 so that we don't loose the fraction part */
1199         temp = dc_modes->h_active * 1000;
1200         temp1 = dc_modes->h_active + dc_modes->h_back_porch +
1201                         dc_modes->h_sync_width;
1202
1203         sol_delay = temp1 * dsi_to_pixel_clk_ratio -
1204                         temp * dsi->pixel_scaler_mul /
1205                         (dsi->pixel_scaler_div * dsi->info.n_data_lanes);
1206
1207         /* Do rounding on sol delay */
1208         sol_delay = (sol_delay + 1000 - 1)/1000;
1209
1210         /* TODO:
1211          * 1. find out the correct sol fifo depth to use
1212          * 2. verify with hw about the clamping function
1213          */
1214         if (sol_delay > (480 * 4)) {
1215                 sol_delay = (480 * 4);
1216                 mipi_clk_adj_kHz = sol_delay +
1217                         (dc_modes->h_active * dsi->pixel_scaler_mul) /
1218                         (dsi->info.n_data_lanes * dsi->pixel_scaler_div);
1219
1220                 mipi_clk_adj_kHz *= (dsi->default_pixel_clk_khz / temp1);
1221
1222                 mipi_clk_adj_kHz *= 4;
1223         }
1224
1225         dsi->target_hs_clk_khz = mipi_clk_adj_kHz;
1226
1227         return sol_delay;
1228 }
1229
1230 static void tegra_dsi_set_sol_delay(struct tegra_dc *dc,
1231                                 struct tegra_dc_dsi_data *dsi)
1232 {
1233         u32 sol_delay;
1234         u32 internal_delay;
1235         u32 h_width_byte_clk;
1236         u32 h_width_pixels;
1237         u32 h_width_ganged_byte_clk;
1238         u8 n_data_lanes_this_cont = 0;
1239         u8 n_data_lanes_ganged = 0;
1240
1241         if (!(dsi->info.ganged_type)) {
1242                 if (dsi->info.video_burst_mode ==
1243                         TEGRA_DSI_VIDEO_NONE_BURST_MODE ||
1244                         dsi->info.video_burst_mode ==
1245                         TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END) {
1246 #define VIDEO_FIFO_LATENCY_PIXEL_CLK 8
1247                         sol_delay = VIDEO_FIFO_LATENCY_PIXEL_CLK *
1248                                 dsi->pixel_scaler_mul / dsi->pixel_scaler_div;
1249 #undef VIDEO_FIFO_LATENCY_PIXEL_CLK
1250                         dsi->status.clk_burst = DSI_CLK_BURST_NONE_BURST;
1251                 } else {
1252                         sol_delay = tegra_dsi_sol_delay_burst(dc, dsi);
1253                         dsi->status.clk_burst = DSI_CLK_BURST_BURST_MODE;
1254                 }
1255         } else {
1256 #define SOL_TO_VALID_PIX_CLK_DELAY 4
1257 #define VALID_TO_FIFO_PIX_CLK_DELAY 4
1258 #define FIFO_WR_PIX_CLK_DELAY 2
1259 #define FIFO_RD_BYTE_CLK_DELAY 6
1260 #define TOT_INTERNAL_PIX_DELAY (SOL_TO_VALID_PIX_CLK_DELAY + \
1261                                 VALID_TO_FIFO_PIX_CLK_DELAY + \
1262                                 FIFO_WR_PIX_CLK_DELAY)
1263
1264                 internal_delay = DIV_ROUND_UP(
1265                                 TOT_INTERNAL_PIX_DELAY * dsi->pixel_scaler_mul,
1266                                 dsi->pixel_scaler_div * dsi->info.n_data_lanes)
1267                                 + FIFO_RD_BYTE_CLK_DELAY;
1268
1269                 h_width_pixels = dc->mode.h_sync_width +
1270                                         dc->mode.h_back_porch +
1271                                         dc->mode.h_active +
1272                                         dc->mode.h_front_porch;
1273
1274                 h_width_byte_clk = DIV_ROUND_UP(h_width_pixels *
1275                                         dsi->pixel_scaler_mul,
1276                                         dsi->pixel_scaler_div *
1277                                         dsi->info.n_data_lanes);
1278
1279                 if (dsi->info.ganged_type ==
1280                         TEGRA_DSI_GANGED_SYMMETRIC_LEFT_RIGHT ||
1281                         dsi->info.ganged_type ==
1282                         TEGRA_DSI_GANGED_SYMMETRIC_EVEN_ODD) {
1283                         n_data_lanes_this_cont = dsi->info.n_data_lanes / 2;
1284                         n_data_lanes_ganged = dsi->info.n_data_lanes;
1285                 }
1286
1287                 h_width_ganged_byte_clk = DIV_ROUND_UP(
1288                                         n_data_lanes_this_cont *
1289                                         h_width_byte_clk,
1290                                         n_data_lanes_ganged);
1291
1292                 sol_delay = h_width_byte_clk - h_width_ganged_byte_clk +
1293                                                         internal_delay;
1294                 sol_delay = (dsi->info.video_data_type ==
1295                                 TEGRA_DSI_VIDEO_TYPE_COMMAND_MODE) ?
1296                                 sol_delay + 20: sol_delay;
1297
1298 #undef SOL_TO_VALID_PIX_CLK_DELAY
1299 #undef VALID_TO_FIFO_PIX_CLK_DELAY
1300 #undef FIFO_WR_PIX_CLK_DELAY
1301 #undef FIFO_RD_BYTE_CLK_DELAY
1302 #undef TOT_INTERNAL_PIX_DELAY
1303         }
1304
1305         tegra_dsi_writel(dsi, DSI_SOL_DELAY_SOL_DELAY(sol_delay),
1306                                                 DSI_SOL_DELAY);
1307 }
1308
1309 static void tegra_dsi_set_timeout(struct tegra_dc_dsi_data *dsi)
1310 {
1311         u32 val;
1312         u32 bytes_per_frame;
1313         u32 timeout = 0;
1314
1315         /* TODO: verify the following equation */
1316         bytes_per_frame = dsi->current_dsi_clk_khz * 1000 * 2 /
1317                                                 (dsi->info.refresh_rate * 8);
1318         timeout = bytes_per_frame / DSI_CYCLE_COUNTER_VALUE;
1319         timeout = (timeout + DSI_HTX_TO_MARGIN) & 0xffff;
1320
1321         val = DSI_TIMEOUT_0_LRXH_TO(DSI_LRXH_TO_VALUE) |
1322                         DSI_TIMEOUT_0_HTX_TO(timeout);
1323         tegra_dsi_writel(dsi, val, DSI_TIMEOUT_0);
1324
1325         if (dsi->info.panel_reset_timeout_msec)
1326                 timeout = (dsi->info.panel_reset_timeout_msec * 1000*1000)
1327                                         / dsi->current_bit_clk_ns;
1328         else
1329                 timeout = DSI_PR_TO_VALUE;
1330
1331         val = DSI_TIMEOUT_1_PR_TO(timeout) |
1332                 DSI_TIMEOUT_1_TA_TO(DSI_TA_TO_VALUE);
1333         tegra_dsi_writel(dsi, val, DSI_TIMEOUT_1);
1334
1335         val = DSI_TO_TALLY_P_RESET_STATUS(IN_RESET) |
1336                 DSI_TO_TALLY_TA_TALLY(DSI_TA_TALLY_VALUE)|
1337                 DSI_TO_TALLY_LRXH_TALLY(DSI_LRXH_TALLY_VALUE)|
1338                 DSI_TO_TALLY_HTX_TALLY(DSI_HTX_TALLY_VALUE);
1339         tegra_dsi_writel(dsi, val, DSI_TO_TALLY);
1340 }
1341
1342 static void tegra_dsi_setup_ganged_mode_pkt_length(struct tegra_dc *dc,
1343                                                 struct tegra_dc_dsi_data *dsi)
1344 {
1345         u32 hact_pkt_len_pix_orig = dc->mode.h_active;
1346         u32 hact_pkt_len_pix = 0;
1347         u32 hact_pkt_len_bytes = 0;
1348         u32 hfp_pkt_len_bytes = 0;
1349         u32 pix_per_line_orig = 0;
1350         u32 pix_per_line = 0;
1351         u32 val = 0;
1352
1353 /* hsync + hact + hfp = (4) + (4+2) + (4+2) */
1354 #define HEADER_OVERHEAD 16
1355
1356         pix_per_line_orig = dc->mode.h_sync_width + dc->mode.h_back_porch +
1357                         dc->mode.h_active + dc->mode.h_front_porch;
1358
1359         if (dsi->info.ganged_type == TEGRA_DSI_GANGED_SYMMETRIC_LEFT_RIGHT ||
1360                 dsi->info.ganged_type == TEGRA_DSI_GANGED_SYMMETRIC_EVEN_ODD) {
1361                 hact_pkt_len_pix = DIV_ROUND_UP(hact_pkt_len_pix_orig, 2);
1362                 pix_per_line = DIV_ROUND_UP(pix_per_line_orig, 2);
1363                 if (dsi->controller_index) {
1364                         hact_pkt_len_pix =
1365                                 hact_pkt_len_pix_orig - hact_pkt_len_pix;
1366                         pix_per_line = pix_per_line_orig - pix_per_line;
1367                 }
1368                 hact_pkt_len_bytes = hact_pkt_len_pix *
1369                         dsi->pixel_scaler_mul / dsi->pixel_scaler_div;
1370                 hfp_pkt_len_bytes = pix_per_line *
1371                         dsi->pixel_scaler_mul / dsi->pixel_scaler_div -
1372                         hact_pkt_len_bytes - HEADER_OVERHEAD;
1373         }
1374
1375         val = DSI_PKT_LEN_0_1_LENGTH_0(0) |
1376                 DSI_PKT_LEN_0_1_LENGTH_1(0);
1377         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_0_1);
1378
1379         val = DSI_PKT_LEN_2_3_LENGTH_2(0x0) |
1380                 DSI_PKT_LEN_2_3_LENGTH_3(hact_pkt_len_bytes);
1381         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_2_3);
1382
1383         val = DSI_PKT_LEN_4_5_LENGTH_4(hfp_pkt_len_bytes) |
1384                 DSI_PKT_LEN_4_5_LENGTH_5(0);
1385         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_4_5);
1386
1387         val = DSI_PKT_LEN_6_7_LENGTH_6(0) |
1388                 DSI_PKT_LEN_6_7_LENGTH_7(0);
1389         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_6_7);
1390
1391 #undef HEADER_OVERHEAD
1392 }
1393
1394 static void tegra_dsi_setup_video_mode_pkt_length(struct tegra_dc *dc,
1395                                                 struct tegra_dc_dsi_data *dsi)
1396 {
1397         u32 val;
1398         u32 hact_pkt_len;
1399         u32 hsa_pkt_len;
1400         u32 hbp_pkt_len;
1401         u32 hfp_pkt_len;
1402
1403         hact_pkt_len = dc->mode.h_active * dsi->pixel_scaler_mul /
1404                                                         dsi->pixel_scaler_div;
1405         hsa_pkt_len = dc->mode.h_sync_width * dsi->pixel_scaler_mul /
1406                                                         dsi->pixel_scaler_div;
1407         hbp_pkt_len = dc->mode.h_back_porch * dsi->pixel_scaler_mul /
1408                                                         dsi->pixel_scaler_div;
1409         hfp_pkt_len = dc->mode.h_front_porch * dsi->pixel_scaler_mul /
1410                                                         dsi->pixel_scaler_div;
1411
1412         if (dsi->info.video_burst_mode !=
1413                                 TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END)
1414                 hbp_pkt_len += hsa_pkt_len;
1415
1416         hsa_pkt_len -= DSI_HSYNC_BLNK_PKT_OVERHEAD;
1417         hbp_pkt_len -= DSI_HBACK_PORCH_PKT_OVERHEAD;
1418         hfp_pkt_len -= DSI_HFRONT_PORCH_PKT_OVERHEAD;
1419
1420         val = DSI_PKT_LEN_0_1_LENGTH_0(0) |
1421                         DSI_PKT_LEN_0_1_LENGTH_1(hsa_pkt_len);
1422         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_0_1);
1423
1424         val = DSI_PKT_LEN_2_3_LENGTH_2(hbp_pkt_len) |
1425                         DSI_PKT_LEN_2_3_LENGTH_3(hact_pkt_len);
1426         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_2_3);
1427
1428         val = DSI_PKT_LEN_4_5_LENGTH_4(hfp_pkt_len) |
1429                         DSI_PKT_LEN_4_5_LENGTH_5(0);
1430         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_4_5);
1431
1432         val = DSI_PKT_LEN_6_7_LENGTH_6(0) | DSI_PKT_LEN_6_7_LENGTH_7(0x0f0f);
1433         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_6_7);
1434 }
1435
1436 static void tegra_dsi_setup_cmd_mode_pkt_length(struct tegra_dc *dc,
1437                                                 struct tegra_dc_dsi_data *dsi)
1438 {
1439         unsigned long   val;
1440         unsigned long   act_bytes;
1441
1442         act_bytes = dc->mode.h_active * dsi->pixel_scaler_mul /
1443                         dsi->pixel_scaler_div + 1;
1444
1445         val = DSI_PKT_LEN_0_1_LENGTH_0(0) | DSI_PKT_LEN_0_1_LENGTH_1(0);
1446         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_0_1);
1447
1448         val = DSI_PKT_LEN_2_3_LENGTH_2(0) | DSI_PKT_LEN_2_3_LENGTH_3(act_bytes);
1449         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_2_3);
1450
1451         val = DSI_PKT_LEN_4_5_LENGTH_4(0) | DSI_PKT_LEN_4_5_LENGTH_5(act_bytes);
1452         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_4_5);
1453
1454         val = DSI_PKT_LEN_6_7_LENGTH_6(0) | DSI_PKT_LEN_6_7_LENGTH_7(0x0f0f);
1455         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_6_7);
1456 }
1457
1458 static void tegra_dsi_set_pkt_length(struct tegra_dc *dc,
1459                                 struct tegra_dc_dsi_data *dsi)
1460 {
1461         if (dsi->driven_mode == TEGRA_DSI_DRIVEN_BY_HOST)
1462                 return;
1463
1464         if (dsi->info.video_data_type == TEGRA_DSI_VIDEO_TYPE_VIDEO_MODE) {
1465                 if (dsi->info.ganged_type)
1466                         tegra_dsi_setup_ganged_mode_pkt_length(dc, dsi);
1467                 else
1468                         tegra_dsi_setup_video_mode_pkt_length(dc, dsi);
1469         } else {
1470                 tegra_dsi_setup_cmd_mode_pkt_length(dc, dsi);
1471         }
1472 }
1473
1474 static void tegra_dsi_set_pkt_seq(struct tegra_dc *dc,
1475                                 struct tegra_dc_dsi_data *dsi)
1476 {
1477         const u32 *pkt_seq;
1478         u32 rgb_info;
1479         u32 pkt_seq_3_5_rgb_lo;
1480         u32 pkt_seq_3_5_rgb_hi;
1481         u32     val;
1482         u32 reg;
1483         u8  i;
1484
1485         if (dsi->driven_mode == TEGRA_DSI_DRIVEN_BY_HOST)
1486                 return;
1487
1488         switch (dsi->info.pixel_format) {
1489         case TEGRA_DSI_PIXEL_FORMAT_16BIT_P:
1490                 rgb_info = CMD_RGB_16BPP;
1491                 break;
1492         case TEGRA_DSI_PIXEL_FORMAT_18BIT_P:
1493                 rgb_info = CMD_RGB_18BPP;
1494                 break;
1495         case TEGRA_DSI_PIXEL_FORMAT_18BIT_NP:
1496                 rgb_info = CMD_RGB_18BPPNP;
1497                 break;
1498         case TEGRA_DSI_PIXEL_FORMAT_24BIT_P:
1499         default:
1500                 rgb_info = CMD_RGB_24BPP;
1501                 break;
1502         }
1503
1504         pkt_seq_3_5_rgb_lo = 0;
1505         pkt_seq_3_5_rgb_hi = 0;
1506         if (dsi->info.pkt_seq)
1507                 pkt_seq = dsi->info.pkt_seq;
1508         else if (dsi->info.video_data_type == TEGRA_DSI_VIDEO_TYPE_COMMAND_MODE)
1509                 pkt_seq = dsi_pkt_seq_cmd_mode;
1510         else {
1511                 switch (dsi->info.video_burst_mode) {
1512                 case TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED:
1513                 case TEGRA_DSI_VIDEO_BURST_MODE_LOW_SPEED:
1514                 case TEGRA_DSI_VIDEO_BURST_MODE_MEDIUM_SPEED:
1515                 case TEGRA_DSI_VIDEO_BURST_MODE_FAST_SPEED:
1516                 case TEGRA_DSI_VIDEO_BURST_MODE_FASTEST_SPEED:
1517                         pkt_seq_3_5_rgb_lo =
1518                                         DSI_PKT_SEQ_3_LO_PKT_32_ID(rgb_info);
1519                         if (!dsi->info.no_pkt_seq_eot)
1520                                 pkt_seq = dsi_pkt_seq_video_burst;
1521                         else
1522                                 pkt_seq = dsi_pkt_seq_video_burst_no_eot;
1523                         break;
1524                 case TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END:
1525                         pkt_seq_3_5_rgb_hi =
1526                                         DSI_PKT_SEQ_3_HI_PKT_34_ID(rgb_info);
1527                         pkt_seq = dsi_pkt_seq_video_non_burst_syne;
1528                         break;
1529                 case TEGRA_DSI_VIDEO_NONE_BURST_MODE:
1530                 default:
1531                         if (dsi->info.ganged_type) {
1532                                 pkt_seq_3_5_rgb_lo =
1533                                         DSI_PKT_SEQ_3_LO_PKT_31_ID(rgb_info);
1534                                 pkt_seq =
1535                                 dsi_pkt_seq_video_non_burst_no_eot_no_lp_no_hbp;
1536                         } else {
1537                                 pkt_seq_3_5_rgb_lo =
1538                                         DSI_PKT_SEQ_3_LO_PKT_32_ID(rgb_info);
1539                                 pkt_seq = dsi_pkt_seq_video_non_burst;
1540                         }
1541                         break;
1542                 }
1543         }
1544
1545         for (i = 0; i < NUMOF_PKT_SEQ; i++) {
1546                 val = pkt_seq[i];
1547                 reg = dsi_pkt_seq_reg[i];
1548                 if ((reg == DSI_PKT_SEQ_3_LO) || (reg == DSI_PKT_SEQ_5_LO))
1549                         val |= pkt_seq_3_5_rgb_lo;
1550                 if ((reg == DSI_PKT_SEQ_3_HI) || (reg == DSI_PKT_SEQ_5_HI))
1551                         val |= pkt_seq_3_5_rgb_hi;
1552                 tegra_dsi_writel(dsi, val, reg);
1553         }
1554 }
1555
1556 static void tegra_dsi_reset_underflow_overflow
1557                                 (struct tegra_dc_dsi_data *dsi)
1558 {
1559         u32 val;
1560
1561         val = tegra_dsi_readl(dsi, DSI_STATUS);
1562         val &= (DSI_STATUS_LB_OVERFLOW(0x1) | DSI_STATUS_LB_UNDERFLOW(0x1));
1563         if (val) {
1564                 if (val & DSI_STATUS_LB_OVERFLOW(0x1))
1565                         dev_warn(&dsi->dc->ndev->dev,
1566                                 "dsi: video fifo overflow. Resetting flag\n");
1567                 if (val & DSI_STATUS_LB_UNDERFLOW(0x1))
1568                         dev_warn(&dsi->dc->ndev->dev,
1569                                 "dsi: video fifo underflow. Resetting flag\n");
1570                 val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
1571                 val |= DSI_HOST_CONTROL_FIFO_STAT_RESET(0x1);
1572                 tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
1573                 udelay(5);
1574         }
1575 }
1576
1577 static void tegra_dsi_soft_reset(struct tegra_dc_dsi_data *dsi)
1578 {
1579         u32 trigger;
1580         u32 val;
1581         u32 frame_period = DIV_ROUND_UP(S_TO_MS(1), dsi->info.refresh_rate);
1582         struct tegra_dc_mode mode = dsi->dc->mode;
1583         u32 tot_lines = mode.v_sync_width + mode.v_back_porch +
1584                                 mode.v_active + mode.v_front_porch;
1585         u32 line_period = DIV_ROUND_UP(MS_TO_US(frame_period), tot_lines);
1586         u32 timeout_cnt = 0;
1587
1588 /* wait for 1 frame duration + few extra cycles for dsi to go idle */
1589 #define DSI_IDLE_TIMEOUT        (tot_lines + 5)
1590
1591         val = tegra_dsi_readl(dsi, DSI_STATUS);
1592         while (!(val & DSI_STATUS_IDLE(0x1))) {
1593                 cpu_relax();
1594                 udelay(line_period);
1595                 val = tegra_dsi_readl(dsi, DSI_STATUS);
1596                 if (timeout_cnt++ > DSI_IDLE_TIMEOUT) {
1597                         dev_warn(&dsi->dc->ndev->dev, "dsi not idle when soft reset\n");
1598                         break;
1599                 }
1600         }
1601
1602         tegra_dsi_writel(dsi,
1603                 DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_DISABLE),
1604                 DSI_POWER_CONTROL);
1605         /* stabilization delay */
1606         udelay(300);
1607
1608         tegra_dsi_writel(dsi,
1609                 DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_ENABLE),
1610                 DSI_POWER_CONTROL);
1611         /* stabilization delay */
1612         udelay(300);
1613
1614         /* dsi HW does not clear host trigger bit automatically
1615          * on dsi interface disable if host fifo is empty or in mid
1616          * of host transmission
1617          */
1618         trigger = tegra_dsi_readl(dsi, DSI_TRIGGER);
1619         if (trigger)
1620                 tegra_dsi_writel(dsi, 0x0, DSI_TRIGGER);
1621
1622 #undef DSI_IDLE_TIMEOUT
1623 }
1624
1625 static void tegra_dsi_stop_dc_stream(struct tegra_dc *dc,
1626                                         struct tegra_dc_dsi_data *dsi)
1627 {
1628         /* extra reference to dc clk */
1629         clk_prepare_enable(dc->clk);
1630
1631         tegra_dc_writel(dc, DISP_CTRL_MODE_STOP, DC_CMD_DISPLAY_COMMAND);
1632         tegra_dc_writel(dc, 0, DC_DISP_DISP_WIN_OPTIONS);
1633         tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
1634         tegra_dc_writel(dc, GENERAL_ACT_REQ , DC_CMD_STATE_CONTROL);
1635
1636         /* balance extra dc clk reference */
1637         clk_disable_unprepare(dc->clk);
1638
1639         dsi->status.dc_stream = DSI_DC_STREAM_DISABLE;
1640 }
1641
1642 /* wait for frame end interrupt or (timeout_n_frames * 1 frame duration)
1643  * whichever happens to occur first
1644  */
1645 static int tegra_dsi_wait_frame_end(struct tegra_dc *dc,
1646                                 struct tegra_dc_dsi_data *dsi,
1647                                 u32 timeout_n_frames)
1648 {
1649         int val;
1650         long timeout;
1651         u32 frame_period = DIV_ROUND_UP(S_TO_MS(1), dsi->info.refresh_rate);
1652         struct tegra_dc_mode mode = dc->mode;
1653         u32 line_period = DIV_ROUND_UP(
1654                                 MS_TO_US(frame_period),
1655                                 mode.v_sync_width + mode.v_back_porch +
1656                                 mode.v_active + mode.v_front_porch);
1657
1658         if (timeout_n_frames < 2)
1659                 dev_WARN(&dc->ndev->dev,
1660                 "dsi: to stop at next frame give at least 2 frame delay\n");
1661
1662         INIT_COMPLETION(dc->frame_end_complete);
1663
1664         tegra_dc_flush_interrupt(dc, FRAME_END_INT);
1665         /* unmask frame end interrupt */
1666         val = tegra_dc_unmask_interrupt(dc, FRAME_END_INT);
1667
1668         timeout = wait_for_completion_interruptible_timeout(
1669                         &dc->frame_end_complete,
1670                         msecs_to_jiffies(timeout_n_frames * frame_period));
1671
1672         /* reinstate interrupt mask */
1673         tegra_dc_writel(dc, val, DC_CMD_INT_MASK);
1674
1675         /* wait for v_ref_to_sync no. of lines after frame end interrupt */
1676         udelay(mode.v_ref_to_sync * line_period);
1677
1678         return timeout;
1679 }
1680
1681 static void tegra_dsi_stop_dc_stream_at_frame_end(struct tegra_dc *dc,
1682                                                 struct tegra_dc_dsi_data *dsi,
1683                                                 u32 timeout_n_frames)
1684 {
1685         tegra_dsi_stop_dc_stream(dc, dsi);
1686
1687         tegra_dsi_wait_frame_end(dc, dsi, timeout_n_frames);
1688
1689         tegra_dsi_soft_reset(dsi);
1690
1691         tegra_dsi_reset_underflow_overflow(dsi);
1692 }
1693
1694 static void tegra_dc_gpio_to_spio(struct tegra_dc_dsi_data *dsi, unsigned gpio)
1695 {
1696         int err;
1697
1698         /* convert to spio */
1699         err = gpio_request(gpio, "temp_request");
1700         if (err < 0) {
1701                 dev_err(&dsi->dc->ndev->dev,
1702                         "dsi: %s: gpio request failed %d\n", __func__, err);
1703                 return;
1704         }
1705         gpio_free(gpio);
1706 }
1707
1708 static void tegra_dsi_start_dc_stream(struct tegra_dc *dc,
1709                                         struct tegra_dc_dsi_data *dsi)
1710 {
1711         u32 val;
1712
1713         /* take extra reference to dc clk */
1714         clk_prepare_enable(dc->clk);
1715
1716         tegra_dc_writel(dc, DSI_ENABLE, DC_DISP_DISP_WIN_OPTIONS);
1717
1718         /* TODO: clean up */
1719         tegra_dc_writel(dc, PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
1720                         PW4_ENABLE | PM0_ENABLE | PM1_ENABLE,
1721                         DC_CMD_DISPLAY_POWER_CONTROL);
1722
1723         /* Configure one-shot mode or continuous mode */
1724         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE) {
1725                 /* disable LSPI/LCD_DE output */
1726                 val = PIN_OUTPUT_LSPI_OUTPUT_DIS;
1727                 tegra_dc_writel(dc, val, DC_COM_PIN_OUTPUT_ENABLE3);
1728
1729                 /* enable MSF & set MSF polarity */
1730                 val = MSF_ENABLE | MSF_LSPI;
1731                 if (!dsi->info.te_polarity_low)
1732                         val |= MSF_POLARITY_HIGH;
1733                 else
1734                         val |= MSF_POLARITY_LOW;
1735                 tegra_dc_writel(dc, val, DC_CMD_DISPLAY_COMMAND_OPTION0);
1736
1737                 /* set non-continuous mode */
1738                 tegra_dc_writel(dc, DISP_CTRL_MODE_NC_DISPLAY,
1739                                                 DC_CMD_DISPLAY_COMMAND);
1740                 tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
1741                 tegra_dc_writel(dc, GENERAL_ACT_REQ | NC_HOST_TRIG,
1742                                                 DC_CMD_STATE_CONTROL);
1743
1744                 if (dsi->info.te_gpio)
1745                         tegra_dc_gpio_to_spio(dsi, dsi->info.te_gpio);
1746         } else {
1747                 /* set continuous mode */
1748                 tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY,
1749                                                 DC_CMD_DISPLAY_COMMAND);
1750                 tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
1751                 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
1752         }
1753
1754         /* balance extra dc clk reference */
1755         clk_disable_unprepare(dc->clk);
1756
1757         dsi->status.dc_stream = DSI_DC_STREAM_ENABLE;
1758 }
1759
1760 static void tegra_dsi_set_dc_clk(struct tegra_dc *dc,
1761                                 struct tegra_dc_dsi_data *dsi)
1762 {
1763         u32 shift_clk_div_register;
1764         u32 val;
1765
1766         /* formula: (dsi->shift_clk_div - 1) * 2 */
1767         shift_clk_div_register = (dsi->shift_clk_div.mul -
1768                                 dsi->shift_clk_div.div) * 2 /
1769                                 dsi->shift_clk_div.div;
1770
1771         if (tegra_platform_is_fpga()) {
1772                 shift_clk_div_register = 1;
1773                 if (dsi->info.ganged_type)
1774                         shift_clk_div_register = 0;
1775         }
1776
1777         val = PIXEL_CLK_DIVIDER_PCD1 |
1778                 SHIFT_CLK_DIVIDER(shift_clk_div_register + 2);
1779
1780         /* SW WAR for bug 1045373. To make the shift clk dividor effect under
1781          * all circumstances, write N+2 to SHIFT_CLK_DIVIDER and activate it.
1782          * After 2us delay, write the target values to it. */
1783 #if defined(CONFIG_ARCH_TEGRA_14x_SOC)
1784         tegra_dc_writel(dc, val, DC_DISP_DISP_CLOCK_CONTROL);
1785         tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
1786         tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
1787
1788         udelay(2);
1789 #endif
1790
1791         /* TODO: find out if PCD3 option is required */
1792         val = PIXEL_CLK_DIVIDER_PCD1 |
1793                 SHIFT_CLK_DIVIDER(shift_clk_div_register);
1794
1795         tegra_dc_writel(dc, val, DC_DISP_DISP_CLOCK_CONTROL);
1796 }
1797
1798 static void tegra_dsi_set_dsi_clk(struct tegra_dc *dc,
1799                         struct tegra_dc_dsi_data *dsi, u32 clk)
1800 {
1801         u32 rm;
1802         u32 pclk_khz;
1803
1804         /* Round up to MHz */
1805         rm = clk % 1000;
1806         if (rm != 0)
1807                 clk -= rm;
1808
1809         /* Set up pixel clock */
1810         pclk_khz = (clk * dsi->shift_clk_div.div) /
1811                                 dsi->shift_clk_div.mul;
1812
1813         dc->mode.pclk = pclk_khz * 1000;
1814
1815         dc->shift_clk_div.mul = dsi->shift_clk_div.mul;
1816         dc->shift_clk_div.div = dsi->shift_clk_div.div;
1817
1818         /* TODO: Define one shot work delay in board file. */
1819         /* Since for one-shot mode, refresh rate is usually set larger than
1820          * expected refresh rate, it needs at least 3 frame period. Less
1821          * delay one shot work is, more powering saving we have. */
1822         dc->one_shot_delay_ms = 4 *
1823                         DIV_ROUND_UP(S_TO_MS(1), dsi->info.refresh_rate);
1824
1825         /* Enable DSI clock */
1826         tegra_dc_setup_clk(dc, dsi->dsi_clk);
1827         tegra_dsi_clk_enable(dsi);
1828         tegra_periph_reset_deassert(dsi->dsi_clk);
1829
1830         dsi->current_dsi_clk_khz = clk_get_rate(dsi->dsi_clk) / 1000;
1831         dsi->current_bit_clk_ns =  1000*1000 / (dsi->current_dsi_clk_khz * 2);
1832 }
1833
1834 static void tegra_dsi_hs_clk_out_enable(struct tegra_dc_dsi_data *dsi)
1835 {
1836         u32 val;
1837
1838         val = tegra_dsi_readl(dsi, DSI_CONTROL);
1839         val &= ~DSI_CONTROL_HS_CLK_CTRL(1);
1840
1841         if (dsi->info.video_clock_mode == TEGRA_DSI_VIDEO_CLOCK_CONTINUOUS) {
1842                 val |= DSI_CONTROL_HS_CLK_CTRL(CONTINUOUS);
1843                 dsi->status.clk_mode = DSI_PHYCLK_CONTINUOUS;
1844         } else {
1845                 val |= DSI_CONTROL_HS_CLK_CTRL(TX_ONLY);
1846                 dsi->status.clk_mode = DSI_PHYCLK_TX_ONLY;
1847         }
1848         tegra_dsi_writel(dsi, val, DSI_CONTROL);
1849
1850         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
1851         val &= ~DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(1);
1852         val |= DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(TEGRA_DSI_HIGH);
1853         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
1854
1855         dsi->status.clk_out = DSI_PHYCLK_OUT_EN;
1856 }
1857
1858 static void tegra_dsi_hs_clk_out_enable_in_lp(struct tegra_dc_dsi_data *dsi)
1859 {
1860         u32 val;
1861         tegra_dsi_hs_clk_out_enable(dsi);
1862
1863         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
1864         val &= ~DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(1);
1865         val |= DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(TEGRA_DSI_LOW);
1866         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
1867 }
1868
1869 static void tegra_dsi_hs_clk_out_disable(struct tegra_dc *dc,
1870                                                 struct tegra_dc_dsi_data *dsi)
1871 {
1872         u32 val;
1873
1874         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
1875                 tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi, 2);
1876
1877         tegra_dsi_writel(dsi, TEGRA_DSI_DISABLE, DSI_POWER_CONTROL);
1878         /* stabilization delay */
1879         udelay(300);
1880
1881         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
1882         val &= ~DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(1);
1883         val |= DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(TEGRA_DSI_LOW);
1884         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
1885
1886         tegra_dsi_writel(dsi, TEGRA_DSI_ENABLE, DSI_POWER_CONTROL);
1887         /* stabilization delay */
1888         udelay(300);
1889
1890         dsi->status.clk_mode = DSI_PHYCLK_NOT_INIT;
1891         dsi->status.clk_out = DSI_PHYCLK_OUT_DIS;
1892 }
1893
1894 static void tegra_dsi_set_control_reg_lp(struct tegra_dc_dsi_data *dsi)
1895 {
1896         u32 dsi_control;
1897         u32 host_dsi_control;
1898         u32 max_threshold;
1899
1900         dsi_control = dsi->dsi_control_val | DSI_CTRL_HOST_DRIVEN;
1901         host_dsi_control = HOST_DSI_CTRL_COMMON |
1902                         HOST_DSI_CTRL_HOST_DRIVEN |
1903                         DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(TEGRA_DSI_LOW);
1904         max_threshold = DSI_MAX_THRESHOLD_MAX_THRESHOLD(DSI_HOST_FIFO_DEPTH);
1905
1906         tegra_dsi_writel(dsi, max_threshold, DSI_MAX_THRESHOLD);
1907         tegra_dsi_writel(dsi, dsi_control, DSI_CONTROL);
1908         tegra_dsi_writel(dsi, host_dsi_control, DSI_HOST_DSI_CONTROL);
1909
1910         dsi->status.driven = DSI_DRIVEN_MODE_HOST;
1911         dsi->status.clk_burst = DSI_CLK_BURST_NOT_INIT;
1912         dsi->status.vtype = DSI_VIDEO_TYPE_NOT_INIT;
1913 }
1914
1915 static void tegra_dsi_set_control_reg_hs(struct tegra_dc_dsi_data *dsi,
1916                                                 u8 driven_mode)
1917 {
1918         u32 dsi_control;
1919         u32 host_dsi_control;
1920         u32 max_threshold;
1921         u32 dcs_cmd;
1922
1923         dsi_control = dsi->dsi_control_val;
1924         host_dsi_control = HOST_DSI_CTRL_COMMON;
1925         max_threshold = 0;
1926         dcs_cmd = 0;
1927
1928         if (driven_mode == TEGRA_DSI_DRIVEN_BY_HOST) {
1929                 dsi_control |= DSI_CTRL_HOST_DRIVEN;
1930                 host_dsi_control |= HOST_DSI_CTRL_HOST_DRIVEN;
1931                 max_threshold =
1932                         DSI_MAX_THRESHOLD_MAX_THRESHOLD(DSI_HOST_FIFO_DEPTH);
1933                 dsi->status.driven = DSI_DRIVEN_MODE_HOST;
1934         } else {
1935                 dsi_control |= DSI_CTRL_DC_DRIVEN;
1936                 host_dsi_control |= HOST_DSI_CTRL_DC_DRIVEN;
1937                 max_threshold =
1938                         DSI_MAX_THRESHOLD_MAX_THRESHOLD(DSI_VIDEO_FIFO_DEPTH);
1939                 dsi->status.driven = DSI_DRIVEN_MODE_DC;
1940
1941                 if (dsi->info.video_data_type ==
1942                         TEGRA_DSI_VIDEO_TYPE_COMMAND_MODE) {
1943                         dsi_control |= DSI_CTRL_CMD_MODE;
1944                         dcs_cmd = DSI_DCS_CMDS_LT5_DCS_CMD(
1945                                 DSI_WRITE_MEMORY_START)|
1946                                 DSI_DCS_CMDS_LT3_DCS_CMD(
1947                                 DSI_WRITE_MEMORY_CONTINUE);
1948                         dsi->status.vtype = DSI_VIDEO_TYPE_CMD_MODE;
1949                 } else {
1950                         dsi_control |= DSI_CTRL_VIDEO_MODE;
1951                         dsi->status.vtype = DSI_VIDEO_TYPE_VIDEO_MODE;
1952                 }
1953         }
1954
1955         tegra_dsi_writel(dsi, max_threshold, DSI_MAX_THRESHOLD);
1956         tegra_dsi_writel(dsi, dcs_cmd, DSI_DCS_CMDS);
1957         tegra_dsi_writel(dsi, dsi_control, DSI_CONTROL);
1958         tegra_dsi_writel(dsi, host_dsi_control, DSI_HOST_DSI_CONTROL);
1959 }
1960
1961 static void tegra_dsi_pad_disable(struct tegra_dc_dsi_data *dsi)
1962 {
1963         u32 val;
1964
1965         if (dsi->info.controller_vs == DSI_VS_1) {
1966                 val = tegra_dsi_readl(dsi, DSI_PAD_CONTROL_0_VS1);
1967                 val &= ~(DSI_PAD_CONTROL_0_VS1_PAD_PDIO(0xf) |
1968                         DSI_PAD_CONTROL_0_VS1_PAD_PDIO_CLK(0x1) |
1969                         DSI_PAD_CONTROL_0_VS1_PAD_PULLDN_ENAB(0xf) |
1970                         DSI_PAD_CONTROL_0_VS1_PAD_PULLDN_CLK_ENAB(0x1));
1971                 val |= DSI_PAD_CONTROL_0_VS1_PAD_PDIO(0xf) |
1972                         DSI_PAD_CONTROL_0_VS1_PAD_PDIO_CLK
1973                                                 (TEGRA_DSI_PAD_DISABLE) |
1974                         DSI_PAD_CONTROL_0_VS1_PAD_PULLDN_ENAB(0xf) |
1975                         DSI_PAD_CONTROL_0_VS1_PAD_PULLDN_CLK_ENAB
1976                                                 (TEGRA_DSI_PAD_DISABLE);
1977                 tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL_0_VS1);
1978         } else {
1979                 val = tegra_dsi_readl(dsi, DSI_PAD_CONTROL);
1980                 val &= ~(DSI_PAD_CONTROL_PAD_PDIO(0x3) |
1981                         DSI_PAD_CONTROL_PAD_PDIO_CLK(0x1) |
1982                         DSI_PAD_CONTROL_PAD_PULLDN_ENAB(0x1));
1983                 val |= DSI_PAD_CONTROL_PAD_PDIO(0x3) |
1984                         DSI_PAD_CONTROL_PAD_PDIO_CLK(TEGRA_DSI_PAD_DISABLE) |
1985                         DSI_PAD_CONTROL_PAD_PULLDN_ENAB(TEGRA_DSI_PAD_DISABLE);
1986                 tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL);
1987         }
1988 }
1989
1990 static void tegra_dsi_pad_enable(struct tegra_dc_dsi_data *dsi)
1991 {
1992         u32 val;
1993
1994         if (dsi->info.controller_vs == DSI_VS_1) {
1995                 val = tegra_dsi_readl(dsi, DSI_PAD_CONTROL_0_VS1);
1996                 val &= ~(DSI_PAD_CONTROL_0_VS1_PAD_PDIO(0xf) |
1997                         DSI_PAD_CONTROL_0_VS1_PAD_PDIO_CLK(0x1) |
1998                         DSI_PAD_CONTROL_0_VS1_PAD_PULLDN_ENAB(0xf) |
1999                         DSI_PAD_CONTROL_0_VS1_PAD_PULLDN_CLK_ENAB(0x1));
2000                 val |= DSI_PAD_CONTROL_0_VS1_PAD_PDIO(TEGRA_DSI_PAD_ENABLE) |
2001                         DSI_PAD_CONTROL_0_VS1_PAD_PDIO_CLK(
2002                                                 TEGRA_DSI_PAD_ENABLE) |
2003                         DSI_PAD_CONTROL_0_VS1_PAD_PULLDN_ENAB(
2004                                                 TEGRA_DSI_PAD_ENABLE) |
2005                         DSI_PAD_CONTROL_0_VS1_PAD_PULLDN_CLK_ENAB(
2006                                                 TEGRA_DSI_PAD_ENABLE);
2007                 tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL_0_VS1);
2008         } else {
2009                 val = tegra_dsi_readl(dsi, DSI_PAD_CONTROL);
2010                 val &= ~(DSI_PAD_CONTROL_PAD_PDIO(0x3) |
2011                         DSI_PAD_CONTROL_PAD_PDIO_CLK(0x1) |
2012                         DSI_PAD_CONTROL_PAD_PULLDN_ENAB(0x1));
2013                 val |= DSI_PAD_CONTROL_PAD_PDIO(TEGRA_DSI_PAD_ENABLE) |
2014                         DSI_PAD_CONTROL_PAD_PDIO_CLK(TEGRA_DSI_PAD_ENABLE) |
2015                         DSI_PAD_CONTROL_PAD_PULLDN_ENAB(TEGRA_DSI_PAD_ENABLE);
2016                 tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL);
2017         }
2018 }
2019
2020 static void tegra_dsi_pad_calibration(struct tegra_dc_dsi_data *dsi)
2021 {
2022         u32 val;
2023
2024         if (!dsi->ulpm)
2025                 tegra_dsi_pad_enable(dsi);
2026         else
2027                 tegra_dsi_pad_disable(dsi);
2028
2029         if (dsi->info.controller_vs == DSI_VS_1) {
2030                 /* TODO: characterization parameters */
2031                 tegra_mipi_cal_clk_enable(dsi->mipi_cal);
2032
2033                 tegra_mipi_cal_init_hw(dsi->mipi_cal);
2034
2035                 tegra_mipi_cal_write(dsi->mipi_cal,
2036                                 MIPI_BIAS_PAD_E_VCLAMP_REF(0x1),
2037                                 MIPI_CAL_MIPI_BIAS_PAD_CFG0_0);
2038                 tegra_mipi_cal_write(dsi->mipi_cal,
2039                                 PAD_PDVREG(0x0),
2040                                 MIPI_CAL_MIPI_BIAS_PAD_CFG2_0);
2041
2042                 tegra_mipi_cal_clk_disable(dsi->mipi_cal);
2043         } else {
2044                 val = tegra_dsi_readl(dsi, DSI_PAD_CONTROL);
2045                 val &= ~(DSI_PAD_CONTROL_PAD_LPUPADJ(0x3) |
2046                         DSI_PAD_CONTROL_PAD_LPDNADJ(0x3) |
2047                         DSI_PAD_CONTROL_PAD_PREEMP_EN(0x1) |
2048                         DSI_PAD_CONTROL_PAD_SLEWDNADJ(0x7) |
2049                         DSI_PAD_CONTROL_PAD_SLEWUPADJ(0x7));
2050
2051                 val |= DSI_PAD_CONTROL_PAD_LPUPADJ(0x1) |
2052                         DSI_PAD_CONTROL_PAD_LPDNADJ(0x1) |
2053                         DSI_PAD_CONTROL_PAD_PREEMP_EN(0x1) |
2054                         DSI_PAD_CONTROL_PAD_SLEWDNADJ(0x6) |
2055                         DSI_PAD_CONTROL_PAD_SLEWUPADJ(0x6);
2056
2057                 tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL);
2058
2059                 val = MIPI_CAL_TERMOSA(0x4);
2060                 tegra_vi_csi_writel(val, CSI_CILA_MIPI_CAL_CONFIG_0);
2061
2062                 val = MIPI_CAL_TERMOSB(0x4);
2063                 tegra_vi_csi_writel(val, CSI_CILB_MIPI_CAL_CONFIG_0);
2064
2065                 val = MIPI_CAL_HSPUOSD(0x3) | MIPI_CAL_HSPDOSD(0x4);
2066                 tegra_vi_csi_writel(val, CSI_DSI_MIPI_CAL_CONFIG);
2067
2068                 val = PAD_DRIV_DN_REF(0x5) | PAD_DRIV_UP_REF(0x7);
2069                 tegra_vi_csi_writel(val, CSI_MIPIBIAS_PAD_CONFIG);
2070         }
2071
2072         val = PAD_CIL_PDVREG(0x0);
2073         tegra_vi_csi_writel(val, CSI_CIL_PAD_CONFIG);
2074 }
2075
2076 static void tegra_dsi_panelB_enable(void)
2077 {
2078         unsigned int val;
2079
2080         val = readl(IO_ADDRESS(APB_MISC_GP_MIPI_PAD_CTRL_0));
2081         val |= DSIB_MODE_ENABLE;
2082         writel(val, (IO_ADDRESS(APB_MISC_GP_MIPI_PAD_CTRL_0)));
2083 }
2084
2085 static int tegra_dsi_init_hw(struct tegra_dc *dc,
2086                                 struct tegra_dc_dsi_data *dsi)
2087 {
2088         u32 i;
2089
2090         regulator_enable(dsi->avdd_dsi_csi);
2091         /* stablization delay */
2092         mdelay(50);
2093
2094         tegra_dsi_set_dsi_clk(dc, dsi, dsi->target_lp_clk_khz);
2095
2096         /* Stop DC stream before configuring DSI registers
2097          * to avoid visible glitches on panel during transition
2098          * from bootloader to kernel driver
2099          */
2100         tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi, 2);
2101
2102         tegra_dsi_writel(dsi,
2103                 DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_DISABLE),
2104                 DSI_POWER_CONTROL);
2105         /* stabilization delay */
2106         udelay(300);
2107
2108         if (dsi->info.dsi_instance)
2109                 tegra_dsi_panelB_enable();
2110
2111         tegra_dsi_set_phy_timing(dsi, DSI_LPHS_IN_LP_MODE);
2112
2113         /* Initialize DSI registers */
2114         for (i = 0; i < ARRAY_SIZE(init_reg); i++)
2115                 tegra_dsi_writel(dsi, 0, init_reg[i]);
2116         if (dsi->info.controller_vs == DSI_VS_1) {
2117                 for (i = 0; i < ARRAY_SIZE(init_reg_vs1_ext); i++)
2118                         tegra_dsi_writel(dsi, 0, init_reg_vs1_ext[i]);
2119         }
2120
2121         tegra_dsi_pad_calibration(dsi);
2122
2123         tegra_dsi_writel(dsi,
2124                 DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_ENABLE),
2125                 DSI_POWER_CONTROL);
2126         /* stabilization delay */
2127         udelay(300);
2128
2129         dsi->status.init = DSI_MODULE_INIT;
2130         dsi->status.lphs = DSI_LPHS_NOT_INIT;
2131         dsi->status.vtype = DSI_VIDEO_TYPE_NOT_INIT;
2132         dsi->status.driven = DSI_DRIVEN_MODE_NOT_INIT;
2133         dsi->status.clk_out = DSI_PHYCLK_OUT_DIS;
2134         dsi->status.clk_mode = DSI_PHYCLK_NOT_INIT;
2135         dsi->status.clk_burst = DSI_CLK_BURST_NOT_INIT;
2136         dsi->status.dc_stream = DSI_DC_STREAM_DISABLE;
2137         dsi->status.lp_op = DSI_LP_OP_NOT_INIT;
2138
2139         return 0;
2140 }
2141
2142 static int tegra_dsi_set_to_lp_mode(struct tegra_dc *dc,
2143                         struct tegra_dc_dsi_data *dsi, u8 lp_op)
2144 {
2145         int err;
2146
2147         if (dsi->status.init != DSI_MODULE_INIT) {
2148                 err = -EPERM;
2149                 goto fail;
2150         }
2151
2152         if (dsi->status.lphs == DSI_LPHS_IN_LP_MODE &&
2153                         dsi->status.lp_op == lp_op)
2154                 goto success;
2155
2156         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
2157                 tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi, 2);
2158
2159         /* disable/enable hs clk according to enable_hs_clock_on_lp_cmd_mode */
2160         if ((dsi->status.clk_out == DSI_PHYCLK_OUT_EN) &&
2161                 (!dsi->info.enable_hs_clock_on_lp_cmd_mode))
2162                 tegra_dsi_hs_clk_out_disable(dc, dsi);
2163
2164         dsi->target_lp_clk_khz = tegra_dsi_get_lp_clk_rate(dsi, lp_op);
2165         if (dsi->current_dsi_clk_khz != dsi->target_lp_clk_khz) {
2166                 tegra_dsi_set_dsi_clk(dc, dsi, dsi->target_lp_clk_khz);
2167                 tegra_dsi_set_timeout(dsi);
2168         }
2169
2170         tegra_dsi_set_phy_timing(dsi, DSI_LPHS_IN_LP_MODE);
2171
2172         tegra_dsi_set_control_reg_lp(dsi);
2173
2174         if ((dsi->status.clk_out == DSI_PHYCLK_OUT_DIS) &&
2175                 (dsi->info.enable_hs_clock_on_lp_cmd_mode))
2176                 tegra_dsi_hs_clk_out_enable_in_lp(dsi);
2177
2178         dsi->status.lphs = DSI_LPHS_IN_LP_MODE;
2179         dsi->status.lp_op = lp_op;
2180         dsi->driven_mode = TEGRA_DSI_DRIVEN_BY_HOST;
2181 success:
2182         err = 0;
2183 fail:
2184         return err;
2185 }
2186
2187 static void tegra_dsi_ganged(struct tegra_dc *dc,
2188                                 struct tegra_dc_dsi_data *dsi)
2189 {
2190         u32 start = 0;
2191         u32 low_width = 0;
2192         u32 high_width = 0;
2193         u32 h_active = dc->out->modes->h_active;
2194         u32 val = 0;
2195
2196         if (dsi->info.controller_vs < DSI_VS_1) {
2197                 dev_err(&dc->ndev->dev, "dsi: ganged mode not"
2198                 "supported with current controller version\n");
2199                 return;
2200         }
2201
2202         if (dsi->info.ganged_type ==
2203                         TEGRA_DSI_GANGED_SYMMETRIC_LEFT_RIGHT) {
2204                 low_width = DIV_ROUND_UP(h_active, 2);
2205                 high_width = h_active - low_width;
2206                 if (dsi->controller_index)
2207                         start = h_active / 2;
2208                 else
2209                         start = 0;
2210         } else if (dsi->info.ganged_type ==
2211                         TEGRA_DSI_GANGED_SYMMETRIC_EVEN_ODD) {
2212                 low_width = 0x1;
2213                 high_width = 0x1;
2214                 if (dsi->controller_index)
2215                         start = 1;
2216                 else
2217                         start = 0;
2218         }
2219
2220         tegra_dsi_writel(dsi, DSI_GANGED_MODE_START_POINTER(start),
2221                                                 DSI_GANGED_MODE_START);
2222
2223         val = DSI_GANGED_MODE_SIZE_VALID_LOW_WIDTH(low_width) |
2224                 DSI_GANGED_MODE_SIZE_VALID_HIGH_WIDTH(high_width);
2225         tegra_dsi_writel(dsi, val, DSI_GANGED_MODE_SIZE);
2226
2227         tegra_dsi_writel(dsi, DSI_GANGED_MODE_CONTROL_EN(TEGRA_DSI_ENABLE),
2228                                                 DSI_GANGED_MODE_CONTROL);
2229 }
2230
2231 static int tegra_dsi_set_to_hs_mode(struct tegra_dc *dc,
2232                                         struct tegra_dc_dsi_data *dsi,
2233                                         u8 driven_mode)
2234 {
2235         int err;
2236
2237         if (dsi->status.init != DSI_MODULE_INIT) {
2238                 err = -EPERM;
2239                 goto fail;
2240         }
2241
2242         if (dsi->status.lphs == DSI_LPHS_IN_HS_MODE &&
2243                 dsi->driven_mode == driven_mode)
2244                 goto success;
2245
2246         dsi->driven_mode = driven_mode;
2247
2248         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
2249                 tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi, 2);
2250
2251         if ((dsi->status.clk_out == DSI_PHYCLK_OUT_EN) &&
2252                 (!dsi->info.enable_hs_clock_on_lp_cmd_mode))
2253                 tegra_dsi_hs_clk_out_disable(dc, dsi);
2254
2255         if (dsi->current_dsi_clk_khz != dsi->target_hs_clk_khz) {
2256                 tegra_dsi_set_dsi_clk(dc, dsi, dsi->target_hs_clk_khz);
2257                 tegra_dsi_set_timeout(dsi);
2258         }
2259
2260         tegra_dsi_set_phy_timing(dsi, DSI_LPHS_IN_HS_MODE);
2261
2262         if (driven_mode == TEGRA_DSI_DRIVEN_BY_DC) {
2263                 tegra_dsi_set_pkt_seq(dc, dsi);
2264                 tegra_dsi_set_pkt_length(dc, dsi);
2265                 tegra_dsi_set_sol_delay(dc, dsi);
2266                 tegra_dsi_set_dc_clk(dc, dsi);
2267         }
2268
2269         tegra_dsi_set_control_reg_hs(dsi, driven_mode);
2270
2271         if (dsi->info.ganged_type)
2272                 tegra_dsi_ganged(dc, dsi);
2273
2274         if (dsi->status.clk_out == DSI_PHYCLK_OUT_DIS ||
2275                 dsi->info.enable_hs_clock_on_lp_cmd_mode)
2276                 tegra_dsi_hs_clk_out_enable(dsi);
2277
2278         dsi->status.lphs = DSI_LPHS_IN_HS_MODE;
2279 success:
2280         dsi->status.lp_op = DSI_LP_OP_NOT_INIT;
2281         err = 0;
2282 fail:
2283         return err;
2284 }
2285
2286 static bool tegra_dsi_write_busy(struct tegra_dc_dsi_data *dsi)
2287 {
2288         u32 timeout = 0;
2289         bool retVal = true;
2290
2291         while (timeout <= DSI_MAX_COMMAND_DELAY_USEC) {
2292                 if (!(DSI_TRIGGER_HOST_TRIGGER(0x1) &
2293                         tegra_dsi_readl(dsi, DSI_TRIGGER))) {
2294                         retVal = false;
2295                         break;
2296                 }
2297                 udelay(DSI_COMMAND_DELAY_STEPS_USEC);
2298                 timeout += DSI_COMMAND_DELAY_STEPS_USEC;
2299         }
2300
2301         return retVal;
2302 }
2303
2304 static bool tegra_dsi_read_busy(struct tegra_dc_dsi_data *dsi)
2305 {
2306         u32 timeout = 0;
2307         bool retVal = true;
2308
2309         while (timeout <  DSI_STATUS_POLLING_DURATION_USEC) {
2310                 if (!(DSI_HOST_DSI_CONTROL_IMM_BTA(0x1) &
2311                         tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL))) {
2312                         retVal = false;
2313                         break;
2314                 }
2315                 udelay(DSI_STATUS_POLLING_DELAY_USEC);
2316                 timeout += DSI_STATUS_POLLING_DELAY_USEC;
2317         }
2318
2319         return retVal;
2320 }
2321
2322 static bool tegra_dsi_host_busy(struct tegra_dc_dsi_data *dsi)
2323 {
2324         int err = 0;
2325
2326         if (tegra_dsi_write_busy(dsi)) {
2327                 err = -EBUSY;
2328                 dev_err(&dsi->dc->ndev->dev,
2329                         "DSI trigger bit already set\n");
2330                 goto fail;
2331         }
2332
2333         if (tegra_dsi_read_busy(dsi)) {
2334                 err = -EBUSY;
2335                 dev_err(&dsi->dc->ndev->dev,
2336                         "DSI immediate bta bit already set\n");
2337                 goto fail;
2338         }
2339 fail:
2340         return (err < 0 ? true : false);
2341 }
2342
2343 static void tegra_dsi_reset_read_count(struct tegra_dc_dsi_data *dsi)
2344 {
2345         u32 val;
2346
2347         val = tegra_dsi_readl(dsi, DSI_STATUS);
2348         val &= DSI_STATUS_RD_FIFO_COUNT(0x1f);
2349         if (val) {
2350                 dev_warn(&dsi->dc->ndev->dev,
2351                         "DSI read count not zero, resetting\n");
2352                 tegra_dsi_soft_reset(dsi);
2353         }
2354 }
2355
2356 static struct dsi_status *tegra_dsi_save_state_switch_to_host_cmd_mode(
2357                                                 struct tegra_dc_dsi_data *dsi,
2358                                                 struct tegra_dc *dc,
2359                                                 u8 lp_op)
2360 {
2361         struct dsi_status *init_status = NULL;
2362         int err;
2363
2364         if (dsi->status.init != DSI_MODULE_INIT ||
2365                 dsi->status.lphs == DSI_LPHS_NOT_INIT) {
2366                 err = -EPERM;
2367                 goto fail;
2368         }
2369
2370         init_status = kzalloc(sizeof(*init_status), GFP_KERNEL);
2371         if (!init_status)
2372                 return ERR_PTR(-ENOMEM);
2373
2374         *init_status = dsi->status;
2375
2376         if (dsi->info.hs_cmd_mode_supported) {
2377                 err = tegra_dsi_set_to_hs_mode(dc, dsi,
2378                                 TEGRA_DSI_DRIVEN_BY_HOST);
2379                 if (err < 0) {
2380                         dev_err(&dc->ndev->dev,
2381                         "Switch to HS host mode failed\n");
2382                         goto fail;
2383                 }
2384
2385                 goto success;
2386         }
2387
2388         if (dsi->status.lp_op != lp_op) {
2389                 err = tegra_dsi_set_to_lp_mode(dc, dsi, lp_op);
2390                 if (err < 0) {
2391                         dev_err(&dc->ndev->dev,
2392                         "DSI failed to go to LP mode\n");
2393                         goto fail;
2394                 }
2395         }
2396 success:
2397         return init_status;
2398 fail:
2399         kfree(init_status);
2400         return ERR_PTR(err);
2401 }
2402
2403 static struct dsi_status *tegra_dsi_prepare_host_transmission(
2404                                 struct tegra_dc *dc,
2405                                 struct tegra_dc_dsi_data *dsi,
2406                                 u8 lp_op)
2407 {
2408         int err = 0;
2409         struct dsi_status *init_status;
2410         bool restart_dc_stream = false;
2411
2412         if (dsi->status.init != DSI_MODULE_INIT ||
2413                 dsi->ulpm) {
2414                 err = -EPERM;
2415                 goto fail;
2416         }
2417
2418         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE) {
2419                 restart_dc_stream = true;
2420                 tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi, 2);
2421         }
2422
2423         if (tegra_dsi_host_busy(dsi)) {
2424                 tegra_dsi_soft_reset(dsi);
2425                 if (tegra_dsi_host_busy(dsi)) {
2426                         err = -EBUSY;
2427                         dev_err(&dc->ndev->dev, "DSI host busy\n");
2428                         goto fail;
2429                 }
2430         }
2431
2432         if (lp_op == DSI_LP_OP_READ)
2433                 tegra_dsi_reset_read_count(dsi);
2434
2435         if (dsi->status.lphs == DSI_LPHS_NOT_INIT) {
2436                 err = tegra_dsi_set_to_lp_mode(dc, dsi, lp_op);
2437                 if (err < 0) {
2438                         dev_err(&dc->ndev->dev, "Failed to config LP write\n");
2439                         goto fail;
2440                 }
2441         }
2442
2443         init_status = tegra_dsi_save_state_switch_to_host_cmd_mode
2444                                         (dsi, dc, lp_op);
2445         if (IS_ERR_OR_NULL(init_status)) {
2446                 err = PTR_ERR(init_status);
2447                 dev_err(&dc->ndev->dev, "DSI state saving failed\n");
2448                 goto fail;
2449         }
2450
2451         if (restart_dc_stream)
2452                 init_status->dc_stream = DSI_DC_STREAM_ENABLE;
2453
2454         if (atomic_read(&dsi_syncpt_rst))
2455                 tegra_dsi_syncpt_reset(dsi);
2456
2457         return init_status;
2458 fail:
2459         return ERR_PTR(err);
2460 }
2461
2462 static int tegra_dsi_restore_state(struct tegra_dc *dc,
2463                                 struct tegra_dc_dsi_data *dsi,
2464                                 struct dsi_status *init_status)
2465 {
2466         int err = 0;
2467
2468         if (init_status->lphs == DSI_LPHS_IN_LP_MODE) {
2469                 err = tegra_dsi_set_to_lp_mode(dc, dsi, init_status->lp_op);
2470                 if (err < 0) {
2471                         dev_err(&dc->ndev->dev,
2472                                 "Failed to config LP mode\n");
2473                         goto fail;
2474                 }
2475                 goto success;
2476         }
2477
2478         if (init_status->lphs == DSI_LPHS_IN_HS_MODE) {
2479                 u8 driven = (init_status->driven == DSI_DRIVEN_MODE_DC) ?
2480                         TEGRA_DSI_DRIVEN_BY_DC : TEGRA_DSI_DRIVEN_BY_HOST;
2481                 err = tegra_dsi_set_to_hs_mode(dc, dsi, driven);
2482                 if (err < 0) {
2483                         dev_err(&dc->ndev->dev, "Failed to config HS mode\n");
2484                         goto fail;
2485                 }
2486         }
2487
2488         if (init_status->dc_stream == DSI_DC_STREAM_ENABLE)
2489                 tegra_dsi_start_dc_stream(dc, dsi);
2490 success:
2491 fail:
2492         kfree(init_status);
2493         return err;
2494 }
2495
2496 static int tegra_dsi_host_trigger(struct tegra_dc_dsi_data *dsi)
2497 {
2498         int status = 0;
2499
2500         if (tegra_dsi_readl(dsi, DSI_TRIGGER)) {
2501                 status = -EBUSY;
2502                 goto fail;
2503         }
2504
2505         tegra_dsi_writel(dsi,
2506                 DSI_TRIGGER_HOST_TRIGGER(TEGRA_DSI_ENABLE), DSI_TRIGGER);
2507
2508 #if DSI_USE_SYNC_POINTS
2509         status = tegra_dsi_syncpt(dsi);
2510         if (status < 0) {
2511                 dev_err(&dsi->dc->ndev->dev,
2512                         "DSI syncpt for host trigger failed\n");
2513                 goto fail;
2514         }
2515 #else
2516         if (tegra_dsi_write_busy(dsi)) {
2517                 status = -EBUSY;
2518                 dev_err(&dsi->dc->ndev->dev,
2519                         "Timeout waiting on write completion\n");
2520         }
2521 #endif
2522
2523 fail:
2524         return status;
2525 }
2526
2527 static int _tegra_dsi_write_data(struct tegra_dc_dsi_data *dsi,
2528                                         u8 *pdata, u8 data_id, u16 data_len)
2529 {
2530         u8 virtual_channel;
2531         u32 val;
2532         int err;
2533
2534         err = 0;
2535
2536         virtual_channel = dsi->info.virtual_channel <<
2537                                                 DSI_VIR_CHANNEL_BIT_POSITION;
2538
2539         /* always use hw for ecc */
2540         val = (virtual_channel | data_id) << 0 |
2541                         data_len << 8;
2542         tegra_dsi_writel(dsi, val, DSI_WR_DATA);
2543
2544         /* if pdata != NULL, pkt type is long pkt */
2545         if (pdata != NULL) {
2546                 while (data_len) {
2547                         if (data_len >= 4) {
2548                                 val = ((u32 *) pdata)[0];
2549                                 data_len -= 4;
2550                                 pdata += 4;
2551                         } else {
2552                                 val = 0;
2553                                 memcpy(&val, pdata, data_len);
2554                                 pdata += data_len;
2555                                 data_len = 0;
2556                         }
2557                         tegra_dsi_writel(dsi, val, DSI_WR_DATA);
2558                 }
2559         }
2560
2561         err = tegra_dsi_host_trigger(dsi);
2562         if (err < 0)
2563                 dev_err(&dsi->dc->ndev->dev, "DSI host trigger failed\n");
2564
2565         return err;
2566 }
2567
2568 static void tegra_dc_dsi_hold_host(struct tegra_dc *dc)
2569 {
2570         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
2571
2572         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_LP_MODE) {
2573                 atomic_inc(&dsi->host_ref);
2574                 tegra_dsi_host_resume(dc);
2575         }
2576 }
2577
2578 static void tegra_dc_dsi_release_host(struct tegra_dc *dc)
2579 {
2580         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
2581
2582         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_LP_MODE) {
2583                 atomic_dec(&dsi->host_ref);
2584
2585                 if (!atomic_read(&dsi->host_ref) &&
2586                     (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE))
2587                         schedule_delayed_work(&dsi->idle_work, dsi->idle_delay);
2588         }
2589 }
2590
2591 static void tegra_dc_dsi_idle_work(struct work_struct *work)
2592 {
2593         struct tegra_dc_dsi_data *dsi = container_of(
2594                 to_delayed_work(work), struct tegra_dc_dsi_data, idle_work);
2595
2596         if (dsi->dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_LP_MODE)
2597                 tegra_dsi_host_suspend(dsi->dc);
2598 }
2599
2600 static int tegra_dsi_write_data_nosync(struct tegra_dc *dc,
2601                         struct tegra_dc_dsi_data *dsi,
2602                         u8 *pdata, u8 data_id, u16 data_len)
2603 {
2604         int err = 0;
2605         struct dsi_status *init_status;
2606
2607         init_status = tegra_dsi_prepare_host_transmission(
2608                                 dc, dsi, DSI_LP_OP_WRITE);
2609         if (IS_ERR_OR_NULL(init_status)) {
2610                 err = PTR_ERR(init_status);
2611                 dev_err(&dc->ndev->dev, "DSI host config failed\n");
2612                 goto fail;
2613         }
2614
2615         err = _tegra_dsi_write_data(dsi, pdata, data_id, data_len);
2616 fail:
2617         err = tegra_dsi_restore_state(dc, dsi, init_status);
2618         if (err < 0)
2619                 dev_err(&dc->ndev->dev, "Failed to restore prev state\n");
2620
2621         return err;
2622 }
2623
2624 int tegra_dsi_write_data(struct tegra_dc *dc,
2625                         struct tegra_dc_dsi_data *dsi,
2626                         u8 *pdata, u8 data_id, u16 data_len)
2627 {
2628         int err;
2629
2630         tegra_dc_io_start(dc);
2631         tegra_dc_dsi_hold_host(dc);
2632
2633         err = tegra_dsi_write_data_nosync(dc, dsi, pdata, data_id, data_len);
2634
2635         tegra_dc_dsi_release_host(dc);
2636         tegra_dc_io_end(dc);
2637
2638         return err;
2639 }
2640
2641 EXPORT_SYMBOL(tegra_dsi_write_data);
2642
2643 static int tegra_dsi_send_panel_cmd(struct tegra_dc *dc,
2644                                         struct tegra_dc_dsi_data *dsi,
2645                                         struct tegra_dsi_cmd *cmd,
2646                                         u32 n_cmd)
2647 {
2648         u32 i;
2649         int err;
2650
2651         err = 0;
2652         for (i = 0; i < n_cmd; i++) {
2653                 struct tegra_dsi_cmd *cur_cmd;
2654                 cur_cmd = &cmd[i];
2655
2656                 /*
2657                  * Some Panels need reset midway in the command sequence.
2658                  */
2659                 if (cur_cmd->cmd_type == TEGRA_DSI_GPIO_SET) {
2660                         gpio_set_value(cur_cmd->sp_len_dly.gpio,
2661                                        cur_cmd->data_id);
2662                 } else if (cur_cmd->cmd_type == TEGRA_DSI_DELAY_MS) {
2663                         mdelay(cur_cmd->sp_len_dly.delay_ms);
2664                 } else if (cur_cmd->cmd_type == TEGRA_DSI_SEND_FRAME) {
2665                                 tegra_dsi_send_dc_frames(dc,
2666                                                 dsi,
2667                                                 cur_cmd->sp_len_dly.frame_cnt);
2668                 } else {
2669                         err = tegra_dsi_write_data_nosync(dc, dsi,
2670                                                 cur_cmd->pdata,
2671                                                 cur_cmd->data_id,
2672                                                 cur_cmd->sp_len_dly.data_len);
2673                         mdelay(1);
2674                         if (err < 0)
2675                                 break;
2676                 }
2677         }
2678         return err;
2679 }
2680
2681 static u8 tegra_dsi_ecc(u32 header)
2682 {
2683         char ecc_parity[24] = {
2684                 0x07, 0x0b, 0x0d, 0x0e, 0x13, 0x15, 0x16, 0x19,
2685                 0x1a, 0x1c, 0x23, 0x25, 0x26, 0x29, 0x2a, 0x2c,
2686                 0x31, 0x32, 0x34, 0x38, 0x1f, 0x2f, 0x37, 0x3b
2687         };
2688         u8 ecc_byte;
2689         int i;
2690
2691         ecc_byte = 0;
2692         for (i = 0; i < 24; i++)
2693                 ecc_byte ^= ((header >> i) & 1) ? ecc_parity[i] : 0x00;
2694
2695         return ecc_byte;
2696 }
2697
2698 static u16 tegra_dsi_cs(char *pdata, u16 data_len)
2699 {
2700         u16 byte_cnt;
2701         u8 bit_cnt;
2702         char curr_byte;
2703         u16 crc = 0xFFFF;
2704         u16 poly = 0x8408;
2705
2706         if (data_len > 0) {
2707                 for (byte_cnt = 0; byte_cnt < data_len; byte_cnt++) {
2708                         curr_byte = pdata[byte_cnt];
2709                         for (bit_cnt = 0; bit_cnt < 8; bit_cnt++) {
2710                                 if (((crc & 0x0001 ) ^
2711                                         (curr_byte & 0x0001)) > 0)
2712                                         crc = ((crc >> 1) & 0x7FFF) ^ poly;
2713                                 else
2714                                         crc = (crc >> 1) & 0x7FFF;
2715
2716                                 curr_byte = (curr_byte >> 1 ) & 0x7F;
2717                         }
2718                 }
2719         }
2720         return crc;
2721 }
2722
2723 static int tegra_dsi_dcs_pkt_seq_ctrl_init(struct tegra_dc_dsi_data *dsi,
2724                                                 struct tegra_dsi_cmd *cmd)
2725 {
2726         u8 virtual_channel;
2727         u32 val;
2728         u16 data_len = cmd->sp_len_dly.data_len;
2729         u8 seq_ctrl_reg = 0;
2730
2731         virtual_channel = dsi->info.virtual_channel <<
2732                                 DSI_VIR_CHANNEL_BIT_POSITION;
2733
2734         val = (virtual_channel | cmd->data_id) << 0 |
2735                 data_len << 8;
2736
2737         val |= tegra_dsi_ecc(val) << 24;
2738
2739         tegra_dsi_writel(dsi, val, DSI_INIT_SEQ_DATA_0 + seq_ctrl_reg++);
2740
2741         /* if pdata != NULL, pkt type is long pkt */
2742         if (cmd->pdata != NULL) {
2743                 u8 *pdata;
2744                 u8 *pdata_mem;
2745                 /* allocate memory for pdata + 2 bytes checksum */
2746                 pdata_mem = kzalloc(sizeof(u8) * data_len + 2, GFP_KERNEL);
2747                 if (!pdata_mem) {
2748                         dev_err(&dsi->dc->ndev->dev, "dsi: memory err\n");
2749                         tegra_dsi_soft_reset(dsi);
2750                         return -ENOMEM;
2751                 }
2752
2753                 memcpy(pdata_mem, cmd->pdata, data_len);
2754                 pdata = pdata_mem;
2755                 *((u16 *)(pdata + data_len)) = tegra_dsi_cs(pdata, data_len);
2756
2757                 /* data_len = length of pdata + 2 byte checksum */
2758                 data_len += 2;
2759
2760                 while (data_len) {
2761                         if (data_len >= 4) {
2762                                 val = ((u32 *) pdata)[0];
2763                                 data_len -= 4;
2764                                 pdata += 4;
2765                         } else {
2766                                 val = 0;
2767                                 memcpy(&val, pdata, data_len);
2768                                 pdata += data_len;
2769                                 data_len = 0;
2770                         }
2771                         tegra_dsi_writel(dsi, val, DSI_INIT_SEQ_DATA_0 +
2772                                                         seq_ctrl_reg++);
2773                 }
2774                 kfree(pdata_mem);
2775         }
2776
2777         return 0;
2778 }
2779
2780 int tegra_dsi_start_host_cmd_v_blank_dcs(struct tegra_dc_dsi_data * dsi,
2781                                                 struct tegra_dsi_cmd *cmd)
2782 {
2783 #define PKT_HEADER_LEN_BYTE     4
2784 #define CHECKSUM_LEN_BYTE       2
2785
2786         int err = 0;
2787         u32 val;
2788         u16 tot_pkt_len = PKT_HEADER_LEN_BYTE;
2789         struct tegra_dc *dc = dsi->dc;
2790
2791         if (cmd->cmd_type != TEGRA_DSI_PACKET_CMD)
2792                 return -EINVAL;
2793
2794         mutex_lock(&dsi->lock);
2795         tegra_dc_io_start(dc);
2796         tegra_dc_dsi_hold_host(dc);
2797
2798 #if DSI_USE_SYNC_POINTS
2799         atomic_set(&dsi_syncpt_rst, 1);
2800 #endif
2801
2802         err = tegra_dsi_dcs_pkt_seq_ctrl_init(dsi, cmd);
2803         if (err < 0) {
2804                 dev_err(&dsi->dc->ndev->dev,
2805                         "dsi: dcs pkt seq ctrl init failed\n");
2806                 goto fail;
2807         }
2808
2809         if (cmd->pdata) {
2810                 u16 data_len = cmd->sp_len_dly.data_len;
2811                 tot_pkt_len += data_len + CHECKSUM_LEN_BYTE;
2812         }
2813
2814         val = DSI_INIT_SEQ_CONTROL_DSI_FRAME_INIT_BYTE_COUNT(tot_pkt_len) |
2815                 DSI_INIT_SEQ_CONTROL_DSI_SEND_INIT_SEQUENCE(
2816                                                 TEGRA_DSI_ENABLE);
2817         tegra_dsi_writel(dsi, val, DSI_INIT_SEQ_CONTROL);
2818
2819 fail:
2820         tegra_dc_dsi_release_host(dc);
2821         tegra_dc_io_end(dc);
2822         mutex_unlock(&dsi->lock);
2823         return err;
2824
2825 #undef PKT_HEADER_LEN_BYTE
2826 #undef CHECKSUM_LEN_BYTE
2827 }
2828 EXPORT_SYMBOL(tegra_dsi_start_host_cmd_v_blank_dcs);
2829
2830 void tegra_dsi_stop_host_cmd_v_blank_dcs(struct tegra_dc_dsi_data * dsi)
2831 {
2832         struct tegra_dc *dc = dsi->dc;
2833         u32 cnt;
2834
2835         mutex_lock(&dsi->lock);
2836         tegra_dc_io_start(dc);
2837         tegra_dc_dsi_hold_host(dc);
2838
2839         if (atomic_read(&dsi_syncpt_rst)) {
2840                 tegra_dsi_wait_frame_end(dc, dsi, 2);
2841                 tegra_dsi_syncpt_reset(dsi);
2842                 atomic_set(&dsi_syncpt_rst, 0);
2843         }
2844
2845         tegra_dsi_writel(dsi, TEGRA_DSI_DISABLE, DSI_INIT_SEQ_CONTROL);
2846
2847         /* clear seq data registers */
2848         for (cnt = 0; cnt < 8; cnt++)
2849                 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_0 + cnt);
2850
2851         tegra_dc_dsi_release_host(dc);
2852         tegra_dc_io_end(dc);
2853
2854         mutex_unlock(&dsi->lock);
2855 }
2856 EXPORT_SYMBOL(tegra_dsi_stop_host_cmd_v_blank_dcs);
2857
2858 static int tegra_dsi_bta(struct tegra_dc_dsi_data *dsi)
2859 {
2860         u32 val;
2861         int err = 0;
2862
2863         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
2864         val |= DSI_HOST_DSI_CONTROL_IMM_BTA(TEGRA_DSI_ENABLE);
2865         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
2866
2867 #if DSI_USE_SYNC_POINTS
2868         err = tegra_dsi_syncpt(dsi);
2869         if (err < 0) {
2870                 dev_err(&dsi->dc->ndev->dev,
2871                         "DSI syncpt for bta failed\n");
2872         }
2873 #else
2874         if (tegra_dsi_read_busy(dsi)) {
2875                 err = -EBUSY;
2876                 dev_err(&dsi->dc->ndev->dev,
2877                         "Timeout wating on read completion\n");
2878         }
2879 #endif
2880
2881         return err;
2882 }
2883
2884 static int tegra_dsi_parse_read_response(struct tegra_dc *dc,
2885                                         u32 rd_fifo_cnt, u8 *read_fifo)
2886 {
2887         int err;
2888         u32 payload_size;
2889
2890         payload_size = 0;
2891         err = 0;
2892
2893         switch (read_fifo[0]) {
2894         case DSI_ESCAPE_CMD:
2895                 dev_info(&dc->ndev->dev, "escape cmd[0x%x]\n", read_fifo[0]);
2896                 break;
2897         case DSI_ACK_NO_ERR:
2898                 dev_info(&dc->ndev->dev,
2899                         "Panel ack, no err[0x%x]\n", read_fifo[0]);
2900                 return err;
2901         default:
2902                 dev_info(&dc->ndev->dev, "Invalid read response\n");
2903                 break;
2904         }
2905
2906         switch (read_fifo[4] & 0xff) {
2907         case GEN_LONG_RD_RES:
2908                 /* Fall through */
2909         case DCS_LONG_RD_RES:
2910                 payload_size = (read_fifo[5] |
2911                                 (read_fifo[6] << 8)) & 0xFFFF;
2912                 dev_info(&dc->ndev->dev, "Long read response Packet\n"
2913                                 "payload_size[0x%x]\n", payload_size);
2914                 break;
2915         case GEN_1_BYTE_SHORT_RD_RES:
2916                 /* Fall through */
2917         case DCS_1_BYTE_SHORT_RD_RES:
2918                 payload_size = 1;
2919                 dev_info(&dc->ndev->dev, "Short read response Packet\n"
2920                         "payload_size[0x%x]\n", payload_size);
2921                 break;
2922         case GEN_2_BYTE_SHORT_RD_RES:
2923                 /* Fall through */
2924         case DCS_2_BYTE_SHORT_RD_RES:
2925                 payload_size = 2;
2926                 dev_info(&dc->ndev->dev, "Short read response Packet\n"
2927                         "payload_size[0x%x]\n", payload_size);
2928                 break;
2929         case ACK_ERR_RES:
2930                 payload_size = 2;
2931                 dev_info(&dc->ndev->dev, "Acknowledge error report response\n"
2932                         "Packet payload_size[0x%x]\n", payload_size);
2933                 break;
2934         default:
2935                 dev_info(&dc->ndev->dev, "Invalid response packet\n");
2936                 err = -EINVAL;
2937                 break;
2938         }
2939         return err;
2940 }
2941
2942 static int tegra_dsi_read_fifo(struct tegra_dc *dc,
2943                         struct tegra_dc_dsi_data *dsi,
2944                         u8 *read_fifo)
2945 {
2946         u32 val;
2947         u32 i;
2948         u32 poll_time = 0;
2949         u32 rd_fifo_cnt;
2950         int err = 0;
2951         u8 *read_fifo_cp = read_fifo;
2952
2953         while (poll_time <  DSI_DELAY_FOR_READ_FIFO) {
2954                 mdelay(1);
2955                 val = tegra_dsi_readl(dsi, DSI_STATUS);
2956                 rd_fifo_cnt = val & DSI_STATUS_RD_FIFO_COUNT(0x1f);
2957                 if (rd_fifo_cnt << 2 > DSI_READ_FIFO_DEPTH) {
2958                         dev_err(&dc->ndev->dev,
2959                         "DSI RD_FIFO_CNT is greater than RD_FIFO_DEPTH\n");
2960                         break;
2961                 }
2962                 poll_time++;
2963         }
2964
2965         if (rd_fifo_cnt == 0) {
2966                 dev_info(&dc->ndev->dev,
2967                         "DSI RD_FIFO_CNT is zero\n");
2968                 err = -EINVAL;
2969                 goto fail;
2970         }
2971
2972         if (val & (DSI_STATUS_LB_UNDERFLOW(0x1) |
2973                 DSI_STATUS_LB_OVERFLOW(0x1))) {
2974                 dev_warn(&dc->ndev->dev,
2975                         "DSI overflow/underflow error\n");
2976         }
2977
2978         /* Read data from FIFO */
2979         for (i = 0; i < rd_fifo_cnt; i++) {
2980                 val = tegra_dsi_readl(dsi, DSI_RD_DATA);
2981                 if (enable_read_debug)
2982                         dev_info(&dc->ndev->dev,
2983                         "Read data[%d]: 0x%x\n", i, val);
2984                 memcpy(read_fifo, &val, 4);
2985                 read_fifo += 4;
2986         }
2987
2988         /* Make sure all the data is read from the FIFO */
2989         val = tegra_dsi_readl(dsi, DSI_STATUS);
2990         val &= DSI_STATUS_RD_FIFO_COUNT(0x1f);
2991         if (val)
2992                 dev_err(&dc->ndev->dev, "DSI FIFO_RD_CNT not zero"
2993                 " even after reading FIFO_RD_CNT words from read fifo\n");
2994
2995         if (enable_read_debug) {
2996                 err =
2997                 tegra_dsi_parse_read_response(dc, rd_fifo_cnt, read_fifo_cp);
2998                 if (err < 0)
2999                         dev_warn(&dc->ndev->dev, "Unexpected read data\n");
3000         }
3001 fail:
3002         return err;
3003 }
3004
3005 int tegra_dsi_read_data(struct tegra_dc *dc,
3006                                 struct tegra_dc_dsi_data *dsi,
3007                                 u32 max_ret_payload_size,
3008                                 u32 panel_reg_addr, u8 *read_data)
3009 {
3010         int err = 0;
3011         struct dsi_status *init_status;
3012
3013         mutex_lock(&dsi->lock);
3014         tegra_dc_io_start(dc);
3015         clk_prepare_enable(dsi->dsi_fixed_clk);
3016
3017         init_status = tegra_dsi_prepare_host_transmission(
3018                                 dc, dsi, DSI_LP_OP_WRITE);
3019         if (IS_ERR_OR_NULL(init_status)) {
3020                 err = PTR_ERR(init_status);
3021                 dev_err(&dc->ndev->dev, "DSI host config failed\n");
3022                 goto fail;
3023         }
3024
3025         /* Set max return payload size in words */
3026         err = _tegra_dsi_write_data(dsi, NULL,
3027                 dsi_command_max_return_pkt_size,
3028                 max_ret_payload_size);
3029         if (err < 0) {
3030                 dev_err(&dc->ndev->dev,
3031                                 "DSI write failed\n");
3032                 goto fail;
3033         }
3034
3035         /* DCS to read given panel register */
3036         err = _tegra_dsi_write_data(dsi, NULL,
3037                 dsi_command_dcs_read_with_no_params,
3038                 panel_reg_addr);
3039         if (err < 0) {
3040                 dev_err(&dc->ndev->dev,
3041                                 "DSI write failed\n");
3042                 goto fail;
3043         }
3044
3045         tegra_dsi_reset_read_count(dsi);
3046
3047         if (dsi->status.lp_op == DSI_LP_OP_WRITE) {
3048                 err = tegra_dsi_set_to_lp_mode(dc, dsi, DSI_LP_OP_READ);
3049                 if (err < 0) {
3050                         dev_err(&dc->ndev->dev,
3051                         "DSI failed to go to LP read mode\n");
3052                         goto fail;
3053                 }
3054         }
3055
3056         err = tegra_dsi_bta(dsi);
3057         if (err < 0) {
3058                 dev_err(&dc->ndev->dev,
3059                         "DSI IMM BTA timeout\n");
3060                 goto fail;
3061         }
3062
3063         err = tegra_dsi_read_fifo(dc, dsi, read_data);
3064         if (err < 0) {
3065                 dev_err(&dc->ndev->dev, "DSI read fifo failure\n");
3066                 goto fail;
3067         }
3068 fail:
3069         err = tegra_dsi_restore_state(dc, dsi, init_status);
3070         if (err < 0)
3071                 dev_err(&dc->ndev->dev, "Failed to restore prev state\n");
3072         clk_disable_unprepare(dsi->dsi_fixed_clk);
3073         tegra_dc_io_end(dc);
3074         mutex_unlock(&dsi->lock);
3075         return err;
3076 }
3077 EXPORT_SYMBOL(tegra_dsi_read_data);
3078
3079 int tegra_dsi_panel_sanity_check(struct tegra_dc *dc,
3080                                 struct tegra_dc_dsi_data *dsi)
3081 {
3082         int err = 0;
3083         u8 read_fifo[DSI_READ_FIFO_DEPTH];
3084         struct dsi_status *init_status;
3085         static struct tegra_dsi_cmd dsi_nop_cmd =
3086                         DSI_CMD_SHORT(0x05, 0x0, 0x0);
3087
3088         tegra_dc_io_start(dc);
3089         clk_prepare_enable(dsi->dsi_fixed_clk);
3090
3091         init_status = tegra_dsi_prepare_host_transmission(
3092                                         dc, dsi, DSI_LP_OP_WRITE);
3093         if (IS_ERR_OR_NULL(init_status)) {
3094                 err = PTR_ERR(init_status);
3095                 dev_err(&dc->ndev->dev, "DSI host config failed\n");
3096                 goto fail;
3097         }
3098
3099         err = _tegra_dsi_write_data(dsi, NULL, dsi_nop_cmd.data_id, 0x0);
3100         if (err < 0) {
3101                 dev_err(&dc->ndev->dev, "DSI nop write failed\n");
3102                 goto fail;
3103         }
3104
3105         tegra_dsi_reset_read_count(dsi);
3106
3107         if (dsi->status.lp_op == DSI_LP_OP_WRITE) {
3108                 err = tegra_dsi_set_to_lp_mode(dc, dsi, DSI_LP_OP_READ);
3109                 if (err < 0) {
3110                         dev_err(&dc->ndev->dev,
3111                         "DSI failed to go to LP read mode\n");
3112                         goto fail;
3113                 }
3114         }
3115
3116         err = tegra_dsi_bta(dsi);
3117         if (err < 0) {
3118                 dev_err(&dc->ndev->dev, "DSI BTA failed\n");
3119                 goto fail;
3120         }
3121
3122         err = tegra_dsi_read_fifo(dc, dsi, read_fifo);
3123         if (err < 0) {
3124                 dev_err(&dc->ndev->dev, "DSI read fifo failure\n");
3125                 goto fail;
3126         }
3127
3128         if (read_fifo[0] != DSI_ACK_NO_ERR) {
3129                 dev_warn(&dc->ndev->dev,
3130                         "Ack no error trigger message not received\n");
3131                 err = -EAGAIN;
3132         }
3133 fail:
3134         err = tegra_dsi_restore_state(dc, dsi, init_status);
3135         if (err < 0)
3136                 dev_err(&dc->ndev->dev, "Failed to restore prev state\n");
3137         clk_disable_unprepare(dsi->dsi_fixed_clk);
3138         tegra_dc_io_end(dc);
3139         return err;
3140 }
3141 EXPORT_SYMBOL(tegra_dsi_panel_sanity_check);
3142
3143 static int tegra_dsi_enter_ulpm(struct tegra_dc_dsi_data *dsi)
3144 {
3145         u32 val;
3146         int ret = 0;
3147
3148         if (atomic_read(&dsi_syncpt_rst))
3149                 tegra_dsi_syncpt_reset(dsi);
3150
3151         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
3152         val &= ~DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(3);
3153         val |= DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(ENTER_ULPM);
3154         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
3155
3156 #if DSI_USE_SYNC_POINTS
3157         ret = tegra_dsi_syncpt(dsi);
3158         if (ret < 0) {
3159                 dev_err(&dsi->dc->ndev->dev,
3160                         "DSI syncpt for ulpm enter failed\n");
3161                 return ret;
3162         }
3163 #else
3164         /* TODO: Find exact delay required */
3165         mdelay(10);
3166 #endif
3167         dsi->ulpm = true;
3168
3169         return ret;
3170 }
3171
3172 static int tegra_dsi_exit_ulpm(struct tegra_dc_dsi_data *dsi)
3173 {
3174         u32 val;
3175         int ret = 0;
3176
3177         if (atomic_read(&dsi_syncpt_rst))
3178                 tegra_dsi_syncpt_reset(dsi);
3179
3180         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
3181         val &= ~DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(3);
3182         val |= DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(EXIT_ULPM);
3183         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
3184
3185 #if DSI_USE_SYNC_POINTS
3186         ret = tegra_dsi_syncpt(dsi);
3187         if (ret < 0) {
3188                 dev_err(&dsi->dc->ndev->dev,
3189                         "DSI syncpt for ulpm exit failed\n");
3190                 return ret;
3191         }
3192 #else
3193         /* TODO: Find exact delay required */
3194         mdelay(10);
3195 #endif
3196         dsi->ulpm = false;
3197
3198         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
3199         val &= ~DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(0x3);
3200         val |= DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(NORMAL);
3201         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
3202
3203         return ret;
3204 }
3205
3206 static void tegra_dsi_send_dc_frames(struct tegra_dc *dc,
3207                                      struct tegra_dc_dsi_data *dsi,
3208                                      int no_of_frames)
3209 {
3210         int err;
3211         u32 frame_period = DIV_ROUND_UP(S_TO_MS(1), dsi->info.refresh_rate);
3212         u8 lp_op = dsi->status.lp_op;
3213         bool switch_to_lp = (dsi->status.lphs == DSI_LPHS_IN_LP_MODE);
3214
3215         if (dsi->status.lphs != DSI_LPHS_IN_HS_MODE) {
3216                 err = tegra_dsi_set_to_hs_mode(dc, dsi,
3217                                 TEGRA_DSI_DRIVEN_BY_DC);
3218                 if (err < 0) {
3219                         dev_err(&dc->ndev->dev,
3220                                 "Switch to HS host mode failed\n");
3221                         return;
3222                 }
3223         }
3224
3225         /*
3226          * Some panels need DC frames be sent under certain
3227          * conditions. We are working on the right fix for this
3228          * requirement, while using this current fix.
3229          */
3230         tegra_dsi_start_dc_stream(dc, dsi);
3231
3232         /*
3233          * Send frames in Continuous or One-shot mode.
3234          */
3235         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE) {
3236                 while (no_of_frames--) {
3237                         tegra_dc_writel(dc, GENERAL_ACT_REQ | NC_HOST_TRIG,
3238                                         DC_CMD_STATE_CONTROL);
3239                         mdelay(frame_period);
3240                 }
3241         } else
3242                 mdelay(no_of_frames * frame_period);
3243
3244         tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi, 2);
3245
3246         if (switch_to_lp) {
3247                 err = tegra_dsi_set_to_lp_mode(dc, dsi, lp_op);
3248                 if (err < 0)
3249                         dev_err(&dc->ndev->dev,
3250                                 "DSI failed to go to LP mode\n");
3251         }
3252 }
3253
3254 static void tegra_dsi_setup_initialized_panel(struct tegra_dc_dsi_data *dsi)
3255 {
3256         regulator_enable(dsi->avdd_dsi_csi);
3257
3258         dsi->status.init = DSI_MODULE_INIT;
3259         dsi->status.lphs = DSI_LPHS_IN_HS_MODE;
3260         dsi->status.driven = DSI_DRIVEN_MODE_DC;
3261         dsi->driven_mode = TEGRA_DSI_DRIVEN_BY_DC;
3262         dsi->status.clk_out = DSI_PHYCLK_OUT_EN;
3263         dsi->status.lp_op = DSI_LP_OP_NOT_INIT;
3264         dsi->status.dc_stream = DSI_DC_STREAM_ENABLE;
3265
3266         if (dsi->info.video_clock_mode == TEGRA_DSI_VIDEO_CLOCK_CONTINUOUS)
3267                 dsi->status.clk_mode = DSI_PHYCLK_CONTINUOUS;
3268         else
3269                 dsi->status.clk_mode = DSI_PHYCLK_TX_ONLY;
3270
3271         if (!(dsi->info.ganged_type)) {
3272                 if (dsi->info.video_burst_mode ==
3273                         TEGRA_DSI_VIDEO_NONE_BURST_MODE ||
3274                         dsi->info.video_burst_mode ==
3275                         TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END)
3276                         dsi->status.clk_burst = DSI_CLK_BURST_NONE_BURST;
3277                 else
3278                         dsi->status.clk_burst = DSI_CLK_BURST_BURST_MODE;
3279         }
3280
3281         if (dsi->info.video_data_type == TEGRA_DSI_VIDEO_TYPE_COMMAND_MODE)
3282                 dsi->status.vtype = DSI_VIDEO_TYPE_CMD_MODE;
3283         else
3284                 dsi->status.vtype = DSI_VIDEO_TYPE_VIDEO_MODE;
3285
3286         tegra_dsi_clk_enable(dsi);
3287
3288         dsi->enabled = true;
3289 }
3290
3291 static void _tegra_dc_dsi_enable(struct tegra_dc *dc)
3292 {
3293         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
3294         int err = 0;
3295
3296         mutex_lock(&dsi->lock);
3297         tegra_dc_io_start(dc);
3298
3299         /*
3300          * Do not program this panel as the bootloader as has already
3301          * initialized it. This avoids periods of blanking during boot.
3302          */
3303         if (dc->out->flags & TEGRA_DC_OUT_INITIALIZED_MODE) {
3304                 tegra_dsi_setup_initialized_panel(dsi);
3305                 goto fail;
3306         }
3307
3308         /* Stop DC stream before configuring DSI registers
3309          * to avoid visible glitches on panel during transition
3310          * from bootloader to kernel driver
3311          */
3312         tegra_dsi_stop_dc_stream(dc, dsi);
3313
3314         if (dsi->enabled) {
3315                 if (dsi->ulpm) {
3316                         if (tegra_dsi_exit_ulpm(dsi) < 0) {
3317                                 dev_err(&dc->ndev->dev,
3318                                         "DSI failed to exit ulpm\n");
3319                                 goto fail;
3320                         }
3321                 }
3322
3323                 if (dsi->info.panel_reset) {
3324                         /*
3325                          * Certain panels need dc frames be sent before
3326                          * waking panel.
3327                          */
3328                         if (dsi->info.panel_send_dc_frames)
3329                                 tegra_dsi_send_dc_frames(dc, dsi, 2);
3330
3331                         err = tegra_dsi_send_panel_cmd(dc, dsi,
3332                                                         dsi->info.dsi_init_cmd,
3333                                                         dsi->info.n_init_cmd);
3334                         if (err < 0) {
3335                                 dev_err(&dc->ndev->dev,
3336                                 "dsi: error sending dsi init cmd\n");
3337                                 goto fail;
3338                         }
3339                 } else if (dsi->info.dsi_late_resume_cmd) {
3340                         err = tegra_dsi_send_panel_cmd(dc, dsi,
3341                                                 dsi->info.dsi_late_resume_cmd,
3342                                                 dsi->info.n_late_resume_cmd);
3343                         if (err < 0) {
3344                                 dev_err(&dc->ndev->dev,
3345                                 "dsi: error sending late resume cmd\n");
3346                                 goto fail;
3347                         }
3348                 }
3349         } else {
3350                 err = tegra_dsi_init_hw(dc, dsi);
3351                 if (err < 0) {
3352                         dev_err(&dc->ndev->dev,
3353                                 "dsi: not able to init dsi hardware\n");
3354                         goto fail;
3355                 }
3356
3357                 if (dsi->ulpm) {
3358                         if (tegra_dsi_enter_ulpm(dsi) < 0) {
3359                                 dev_err(&dc->ndev->dev,
3360                                         "DSI failed to enter ulpm\n");
3361                                 goto fail;
3362                         }
3363
3364                         tegra_dsi_pad_enable(dsi);
3365
3366                         if (tegra_dsi_exit_ulpm(dsi) < 0) {
3367                                 dev_err(&dc->ndev->dev,
3368                                         "DSI failed to exit ulpm\n");
3369                                 goto fail;
3370                         }
3371                 }
3372
3373                 /*
3374                  * Certain panels need dc frames be sent before
3375                  * waking panel.
3376                  */
3377                 if (dsi->info.panel_send_dc_frames)
3378                         tegra_dsi_send_dc_frames(dc, dsi, 2);
3379
3380                 err = tegra_dsi_set_to_lp_mode(dc, dsi, DSI_LP_OP_WRITE);
3381                 if (err < 0) {
3382                         dev_err(&dc->ndev->dev,
3383                                 "dsi: not able to set to lp mode\n");
3384                         goto fail;
3385                 }
3386
3387                 err = tegra_dsi_send_panel_cmd(dc, dsi, dsi->info.dsi_init_cmd,
3388                                                 dsi->info.n_init_cmd);
3389                 if (err < 0) {
3390                         dev_err(&dc->ndev->dev,
3391                                 "dsi: error while sending dsi init cmd\n");
3392                         goto fail;
3393                 }
3394
3395                 err = tegra_dsi_set_to_hs_mode(dc, dsi,
3396                                 TEGRA_DSI_DRIVEN_BY_DC);
3397                 if (err < 0) {
3398                         dev_err(&dc->ndev->dev,
3399                                 "dsi: not able to set to hs mode\n");
3400                         goto fail;
3401                 }
3402
3403                 dsi->enabled = true;
3404         }
3405
3406         if (dsi->out_ops && dsi->out_ops->enable)
3407                 dsi->out_ops->enable(dsi);
3408
3409         if (dsi->status.driven == DSI_DRIVEN_MODE_DC)
3410                 tegra_dsi_start_dc_stream(dc, dsi);
3411
3412         dsi->host_suspended = false;
3413 fail:
3414         tegra_dc_io_end(dc);
3415         mutex_unlock(&dsi->lock);
3416 }
3417
3418 static void __tegra_dc_dsi_init(struct tegra_dc *dc)
3419 {
3420         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
3421
3422         tegra_dc_dsi_debug_create(dsi);
3423
3424         if (dsi->info.dsi2lvds_bridge_enable)
3425                 dsi->out_ops = &tegra_dsi2lvds_ops;
3426         else if (dsi->info.dsi2edp_bridge_enable)
3427                 dsi->out_ops = &tegra_dsi2edp_ops;
3428         else
3429                 dsi->out_ops = NULL;
3430
3431         if (dsi->out_ops && dsi->out_ops->init)
3432                 dsi->out_ops->init(dsi);
3433
3434         tegra_dsi_init_sw(dc, dsi);
3435 }
3436
3437 static int tegra_dc_dsi_cp_p_cmd(struct tegra_dsi_cmd *src,
3438                                         struct tegra_dsi_cmd *dst, u16 n_cmd)
3439 {
3440         u16 i;
3441         u16 len;
3442
3443         memcpy(dst, src, sizeof(*dst) * n_cmd);
3444
3445         for (i = 0; i < n_cmd; i++)
3446                 if (src[i].pdata) {
3447                         len = sizeof(*src[i].pdata) *
3448                                         src[i].sp_len_dly.data_len;
3449                         dst[i].pdata = kzalloc(len, GFP_KERNEL);
3450                         if (!dst[i].pdata)
3451                                 goto free_cmd_pdata;
3452                         memcpy(dst[i].pdata, src[i].pdata, len);
3453                 }
3454
3455         return 0;
3456
3457 free_cmd_pdata:
3458         while (i--)
3459                 if (dst[i].pdata)
3460                         kfree(dst[i].pdata);
3461         return -ENOMEM;
3462 }
3463
3464 static int tegra_dc_dsi_cp_info(struct tegra_dc_dsi_data *dsi,
3465                                         struct tegra_dsi_out *p_dsi)
3466 {
3467         struct tegra_dsi_cmd *p_init_cmd;
3468         struct tegra_dsi_cmd *p_early_suspend_cmd = NULL;
3469         struct tegra_dsi_cmd *p_late_resume_cmd = NULL;
3470         struct tegra_dsi_cmd *p_suspend_cmd;
3471         int err;
3472
3473         if (p_dsi->n_data_lanes > MAX_DSI_DATA_LANES)
3474                 return -EINVAL;
3475
3476         p_init_cmd = kzalloc(sizeof(*p_init_cmd) *
3477                                 p_dsi->n_init_cmd, GFP_KERNEL);
3478         if (!p_init_cmd)
3479                 return -ENOMEM;
3480
3481         if (p_dsi->dsi_early_suspend_cmd) {
3482                 p_early_suspend_cmd = kzalloc(sizeof(*p_early_suspend_cmd) *
3483                                         p_dsi->n_early_suspend_cmd,
3484                                         GFP_KERNEL);
3485                 if (!p_early_suspend_cmd) {
3486                         err = -ENOMEM;
3487                         goto err_free_init_cmd;
3488                 }
3489         }
3490
3491         if (p_dsi->dsi_late_resume_cmd) {
3492                 p_late_resume_cmd = kzalloc(sizeof(*p_late_resume_cmd) *
3493                                         p_dsi->n_late_resume_cmd,
3494                                         GFP_KERNEL);
3495                 if (!p_late_resume_cmd) {
3496                         err = -ENOMEM;
3497                         goto err_free_p_early_suspend_cmd;
3498                 }
3499         }
3500
3501         p_suspend_cmd = kzalloc(sizeof(*p_suspend_cmd) * p_dsi->n_suspend_cmd,
3502                                 GFP_KERNEL);
3503         if (!p_suspend_cmd) {
3504                 err = -ENOMEM;
3505                 goto err_free_p_late_resume_cmd;
3506         }
3507
3508         memcpy(&dsi->info, p_dsi, sizeof(dsi->info));
3509
3510         /* Copy panel init cmd */
3511         err = tegra_dc_dsi_cp_p_cmd(p_dsi->dsi_init_cmd,
3512                                                 p_init_cmd, p_dsi->n_init_cmd);
3513         if (err < 0)
3514                 goto err_free;
3515         dsi->info.dsi_init_cmd = p_init_cmd;
3516
3517         /* Copy panel early suspend cmd */
3518         if (p_dsi->dsi_early_suspend_cmd) {
3519                 err = tegra_dc_dsi_cp_p_cmd(p_dsi->dsi_early_suspend_cmd,
3520                                         p_early_suspend_cmd,
3521                                         p_dsi->n_early_suspend_cmd);
3522                 if (err < 0)
3523                         goto err_free;
3524                 dsi->info.dsi_early_suspend_cmd = p_early_suspend_cmd;
3525         }
3526
3527         /* Copy panel late resume cmd */
3528         if (p_dsi->dsi_late_resume_cmd) {
3529                 err = tegra_dc_dsi_cp_p_cmd(p_dsi->dsi_late_resume_cmd,
3530                                                 p_late_resume_cmd,
3531                                                 p_dsi->n_late_resume_cmd);
3532                 if (err < 0)
3533                         goto err_free;
3534                 dsi->info.dsi_late_resume_cmd = p_late_resume_cmd;
3535         }
3536
3537         /* Copy panel suspend cmd */
3538         err = tegra_dc_dsi_cp_p_cmd(p_dsi->dsi_suspend_cmd, p_suspend_cmd,
3539                                         p_dsi->n_suspend_cmd);
3540         if (err < 0)
3541                 goto err_free;
3542         dsi->info.dsi_suspend_cmd = p_suspend_cmd;
3543
3544         if (!dsi->info.panel_reset_timeout_msec)
3545                 dsi->info.panel_reset_timeout_msec =
3546                                                 DEFAULT_PANEL_RESET_TIMEOUT;
3547
3548         if (!dsi->info.panel_buffer_size_byte)
3549                 dsi->info.panel_buffer_size_byte = DEFAULT_PANEL_BUFFER_BYTE;
3550
3551         if (!dsi->info.max_panel_freq_khz) {
3552                 dsi->info.max_panel_freq_khz = DEFAULT_MAX_DSI_PHY_CLK_KHZ;
3553
3554                 if (dsi->info.video_burst_mode >
3555                                 TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END){
3556                         dev_err(&dsi->dc->ndev->dev, "DSI: max_panel_freq_khz"
3557                                         "is not set for DSI burst mode.\n");
3558                         dsi->info.video_burst_mode =
3559                                 TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED;
3560                 }
3561         }
3562
3563         if (!dsi->info.lp_cmd_mode_freq_khz)
3564                 dsi->info.lp_cmd_mode_freq_khz = DEFAULT_LP_CMD_MODE_CLK_KHZ;
3565
3566         if (!dsi->info.chip_id || !dsi->info.chip_rev)
3567                 dev_warn(&dsi->dc->ndev->dev,
3568                         "DSI: Failed to get chip info\n");
3569
3570         if (!dsi->info.lp_read_cmd_mode_freq_khz)
3571                 dsi->info.lp_read_cmd_mode_freq_khz =
3572                         dsi->info.lp_cmd_mode_freq_khz;
3573
3574         /* host mode is for testing only */
3575         dsi->driven_mode = TEGRA_DSI_DRIVEN_BY_DC;
3576         return 0;
3577
3578 err_free:
3579         kfree(p_suspend_cmd);
3580 err_free_p_late_resume_cmd:
3581         kfree(p_late_resume_cmd);
3582 err_free_p_early_suspend_cmd:
3583         kfree(p_early_suspend_cmd);
3584 err_free_init_cmd:
3585         kfree(p_init_cmd);
3586         return err;
3587 }
3588
3589 /* returns next null enumeration from  tegra_dsi_instance */
3590 static inline int tegra_dsi_get_enumeration(void)
3591 {
3592         int i = 0;
3593         for (; i < MAX_DSI_INSTANCE; i++) {
3594                 if (!tegra_dsi_instance[i])
3595                         return i;
3596         }
3597         return -EINVAL;
3598 }
3599
3600 static int _tegra_dc_dsi_init(struct tegra_dc *dc)
3601 {
3602         struct tegra_dc_dsi_data *dsi;
3603         struct resource *res;
3604         struct resource *base_res;
3605         void __iomem *base;
3606         struct clk *dc_clk = NULL;
3607         struct clk *dsi_clk = NULL;
3608         struct clk *dsi_fixed_clk = NULL;
3609         struct tegra_dsi_out *dsi_pdata;
3610         int err = 0;
3611         int dsi_enum = -1;
3612
3613         if (dc->pdata->default_out->dsi->dsi_instance)
3614                 dsi_enum = 1;
3615         else
3616                 dsi_enum = tegra_dsi_get_enumeration();
3617         if (dsi_enum < 0) {
3618                 err = -EINVAL;
3619                 dev_err(&dc->ndev->dev, "dsi: invalid enum retured\n");
3620                 return err;
3621         }
3622
3623         dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
3624         if (!dsi) {
3625                 dev_err(&dc->ndev->dev, "dsi: memory allocation failed\n");
3626                 return -ENOMEM;
3627         }
3628         tegra_dsi_instance[dsi_enum] = dsi;
3629
3630         if (dc->out->dsi->ganged_type) {
3631                 if (dsi_enum)
3632                         res = platform_get_resource_byname(dc->ndev,
3633                                                 IORESOURCE_MEM,
3634                                                 "ganged_dsib_regs");
3635                 else
3636                         res = platform_get_resource_byname(dc->ndev,
3637                                                 IORESOURCE_MEM,
3638                                                 "ganged_dsia_regs");
3639         } else {
3640                 res = platform_get_resource_byname(dc->ndev,
3641                                         IORESOURCE_MEM,
3642                                         "dsi_regs");
3643         }
3644
3645         if (!res) {
3646                 dev_err(&dc->ndev->dev, "dsi: no mem resource\n");
3647                 err = -ENOENT;
3648                 goto err_free_dsi;
3649         }
3650
3651         base_res = request_mem_region(res->start, resource_size(res),
3652                                 dc->ndev->name);
3653         if (!base_res) {
3654                 dev_err(&dc->ndev->dev, "dsi: request_mem_region failed\n");
3655                 err = -EBUSY;
3656                 goto err_free_dsi;
3657         }
3658
3659         base = ioremap(res->start, resource_size(res));
3660         if (!base) {
3661                 dev_err(&dc->ndev->dev, "dsi: registers can't be mapped\n");
3662                 err = -EBUSY;
3663                 goto err_release_regs;
3664         }
3665
3666         dsi_pdata = dc->pdata->default_out->dsi;
3667         if (!dsi_pdata) {
3668                 dev_err(&dc->ndev->dev, "dsi: dsi data not available\n");
3669                 goto err_release_regs;
3670         }
3671
3672         if (dsi_enum)
3673                 dsi_clk = clk_get(&dc->ndev->dev, "dsib");
3674         else
3675                 dsi_clk = clk_get(&dc->ndev->dev, "dsia");
3676
3677         dsi_fixed_clk = clk_get(&dc->ndev->dev, "dsi-fixed");
3678
3679         if (IS_ERR_OR_NULL(dsi_clk) || IS_ERR_OR_NULL(dsi_fixed_clk)) {
3680                 dev_err(&dc->ndev->dev, "dsi: can't get clock\n");
3681                 err = -EBUSY;
3682                 goto err_release_regs;
3683         }
3684
3685         dc_clk = clk_get_sys(dev_name(&dc->ndev->dev), NULL);
3686         if (IS_ERR_OR_NULL(dc_clk)) {
3687                 dev_err(&dc->ndev->dev, "dsi: dc clock %s unavailable\n",
3688                         dev_name(&dc->ndev->dev));
3689                 err = -EBUSY;
3690                 goto err_dsi_clk_put;
3691         }
3692
3693         mutex_init(&dsi->lock);
3694         dsi->controller_index = dsi_enum;
3695         dsi->dc = dc;
3696         dsi->base = base;
3697         dsi->base_res = base_res;
3698         dsi->dc_clk = dc_clk;
3699         dsi->dsi_clk = dsi_clk;
3700         dsi->dsi_fixed_clk = dsi_fixed_clk;
3701
3702         err = tegra_dc_dsi_cp_info(dsi, dsi_pdata);
3703         if (err < 0)
3704                 goto err_dc_clk_put;
3705
3706         tegra_dc_set_outdata(dc, dsi);
3707         __tegra_dc_dsi_init(dc);
3708
3709         return 0;
3710
3711 err_dc_clk_put:
3712         clk_put(dc_clk);
3713 err_dsi_clk_put:
3714         clk_put(dsi_clk);
3715         clk_put(dsi_fixed_clk);
3716 err_release_regs:
3717         release_resource(base_res);
3718 err_free_dsi:
3719         kfree(dsi);
3720
3721         return err;
3722 }
3723
3724 static void _tegra_dc_dsi_destroy(struct tegra_dc *dc)
3725 {
3726         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
3727         u16 i;
3728         u32 val;
3729
3730         mutex_lock(&dsi->lock);
3731
3732         if (dsi->out_ops && dsi->out_ops->destroy)
3733                 dsi->out_ops->destroy(dsi);
3734
3735         /* free up the pdata */
3736         for (i = 0; i < dsi->info.n_init_cmd; i++) {
3737                 if (dsi->info.dsi_init_cmd[i].pdata)
3738                         kfree(dsi->info.dsi_init_cmd[i].pdata);
3739         }
3740         kfree(dsi->info.dsi_init_cmd);
3741
3742         /* Disable dc stream */
3743         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
3744                 tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi, 2);
3745
3746         /* Disable dsi phy clock */
3747         if (dsi->status.clk_out == DSI_PHYCLK_OUT_EN)
3748                 tegra_dsi_hs_clk_out_disable(dc, dsi);
3749
3750         val = DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_DISABLE);
3751         tegra_dsi_writel(dsi, val, DSI_POWER_CONTROL);
3752
3753         iounmap(dsi->base);
3754         release_resource(dsi->base_res);
3755
3756         clk_put(dsi->dc_clk);
3757         clk_put(dsi->dsi_clk);
3758
3759         mutex_unlock(&dsi->lock);
3760
3761         mutex_destroy(&dsi->lock);
3762         kfree(dsi);
3763 }
3764
3765 static void tegra_dsi_config_phy_clk(struct tegra_dc_dsi_data *dsi,
3766                                                         u32 settings)
3767 {
3768         struct clk *parent_clk = NULL;
3769         struct clk *base_clk = NULL;
3770
3771         parent_clk = clk_get_parent(dsi->dsi_clk);
3772         base_clk = clk_get_parent(parent_clk);
3773 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
3774         if (dsi->info.dsi_instance)
3775                 tegra_clk_cfg_ex(base_clk,
3776                                 TEGRA_CLK_PLLD_CSI_OUT_ENB,
3777                                 settings);
3778         else
3779                 tegra_clk_cfg_ex(base_clk,
3780                                 TEGRA_CLK_PLLD_DSI_OUT_ENB,
3781                                 settings);
3782 #else
3783         tegra_clk_cfg_ex(base_clk,
3784                         TEGRA_CLK_PLLD_DSI_OUT_ENB,
3785                         settings);
3786 #endif
3787 }
3788
3789 static int _tegra_dsi_host_suspend(struct tegra_dc *dc,
3790                                         struct tegra_dc_dsi_data *dsi,
3791                                         u32 suspend_aggr)
3792 {
3793         u32 val = 0;
3794         int err = 0;
3795
3796         switch (suspend_aggr) {
3797         case DSI_HOST_SUSPEND_LV2:
3798                 if (!dsi->ulpm) {
3799                         err = tegra_dsi_enter_ulpm(dsi);
3800                         if (err < 0) {
3801                                 dev_err(&dc->ndev->dev,
3802                                         "DSI failed to enter ulpm\n");
3803                                 goto fail;
3804                         }
3805                 }
3806
3807                 val = tegra_dsi_readl(dsi, DSI_PAD_CONTROL);
3808                 val |= DSI_PAD_CONTROL_PAD_PDIO(0x3) |
3809                                 DSI_PAD_CONTROL_PAD_PDIO_CLK(0x1) |
3810                                 DSI_PAD_CONTROL_PAD_PULLDN_ENAB(0x1);
3811                 tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL);
3812
3813                 /* Suspend core-logic */
3814                 val = DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_DISABLE);
3815                 tegra_dsi_writel(dsi, val, DSI_POWER_CONTROL);
3816                 /* fall through */
3817         case DSI_HOST_SUSPEND_LV1:
3818                 /* Disable dsi fast and slow clock */
3819                 tegra_dsi_config_phy_clk(dsi, TEGRA_DSI_DISABLE);
3820                 /* fall through */
3821         case DSI_HOST_SUSPEND_LV0:
3822                 /* Disable dsi source clock */
3823                 tegra_dsi_clk_disable(dsi);
3824                 break;
3825         case DSI_NO_SUSPEND:
3826                 break;
3827         default:
3828                 dev_err(&dc->ndev->dev, "DSI suspend aggressiveness"
3829                                                 "is not supported.\n");
3830         }
3831
3832         return 0;
3833 fail:
3834         return err;
3835 }
3836
3837 static int _tegra_dsi_host_resume(struct tegra_dc *dc,
3838                                         struct tegra_dc_dsi_data *dsi,
3839                                         u32 suspend_aggr)
3840 {
3841         u32 val;
3842         int err;
3843
3844         switch (dsi->info.suspend_aggr) {
3845         case DSI_HOST_SUSPEND_LV0:
3846                 tegra_dsi_clk_enable(dsi);
3847                 break;
3848         case DSI_HOST_SUSPEND_LV1:
3849                 tegra_dsi_config_phy_clk(dsi, TEGRA_DSI_ENABLE);
3850                 tegra_dsi_clk_enable(dsi);
3851                 break;
3852         case DSI_HOST_SUSPEND_LV2:
3853                 tegra_dsi_config_phy_clk(dsi, TEGRA_DSI_ENABLE);
3854                 tegra_dsi_clk_enable(dsi);
3855
3856                 tegra_dsi_writel(dsi,
3857                         DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_ENABLE),
3858                         DSI_POWER_CONTROL);
3859
3860                 if (dsi->ulpm) {
3861                         err = tegra_dsi_enter_ulpm(dsi);
3862                         if (err < 0) {
3863                                 dev_err(&dc->ndev->dev,
3864                                         "DSI failed to enter ulpm\n");
3865                                 goto fail;
3866                         }
3867
3868                         val = tegra_dsi_readl(dsi, DSI_PAD_CONTROL);
3869                         val &= ~(DSI_PAD_CONTROL_PAD_PDIO(0x3) |
3870                                 DSI_PAD_CONTROL_PAD_PDIO_CLK(0x1) |
3871                                 DSI_PAD_CONTROL_PAD_PULLDN_ENAB(0x1));
3872                         tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL);
3873
3874                         if (tegra_dsi_exit_ulpm(dsi) < 0) {
3875                                 dev_err(&dc->ndev->dev,
3876                                         "DSI failed to exit ulpm\n");
3877                                 goto fail;
3878                         }
3879                 }
3880                 break;
3881         case DSI_NO_SUSPEND:
3882                 break;
3883         default:
3884                 dev_err(&dc->ndev->dev, "DSI suspend aggressivenes"
3885                                                 "is not supported.\n");
3886         }
3887
3888         return 0;
3889 fail:
3890         return err;
3891 }
3892
3893 static int tegra_dsi_host_suspend(struct tegra_dc *dc)
3894 {
3895         int err = 0;
3896         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
3897
3898         if (!dsi->enabled)
3899                 return -EINVAL;
3900
3901         if (dsi->host_suspended)
3902                 return 0;
3903
3904         mutex_lock(&dsi->host_lock);
3905         tegra_dc_io_start(dc);
3906         dsi->host_suspended = true;
3907
3908         tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi, 2);
3909
3910         err = _tegra_dsi_host_suspend(dc, dsi, dsi->info.suspend_aggr);
3911         if (err < 0)
3912                 dev_err(&dc->ndev->dev,
3913                         "DSI host suspend failed\n");
3914
3915         tegra_dc_io_end(dc);
3916         mutex_unlock(&dsi->host_lock);
3917         return err;
3918 }
3919
3920 static int tegra_dsi_deep_sleep(struct tegra_dc *dc,
3921                                 struct tegra_dc_dsi_data *dsi)
3922 {
3923         int val = 0;
3924         int err = 0;
3925
3926         if (!dsi->enabled)
3927                 return 0;
3928
3929         err = tegra_dsi_set_to_lp_mode(dc, dsi, DSI_LP_OP_WRITE);
3930         if (err < 0) {
3931                 dev_err(&dc->ndev->dev,
3932                 "DSI failed to go to LP mode\n");
3933                 goto fail;
3934         }
3935
3936         /* Suspend DSI panel */
3937         err = tegra_dsi_send_panel_cmd(dc, dsi,
3938                         dsi->info.dsi_suspend_cmd,
3939                         dsi->info.n_suspend_cmd);
3940
3941         /*
3942          * Certain panels need dc frames be sent after
3943          * putting panel to sleep.
3944          */
3945         if (dsi->info.panel_send_dc_frames)
3946                 tegra_dsi_send_dc_frames(dc, dsi, 2);
3947
3948         if (err < 0) {
3949                 dev_err(&dc->ndev->dev,
3950                         "dsi: Error sending suspend cmd\n");
3951                 goto fail;
3952         }
3953
3954         if (!dsi->ulpm) {
3955                 err = tegra_dsi_enter_ulpm(dsi);
3956                 if (err < 0) {
3957                         dev_err(&dc->ndev->dev,
3958                                 "DSI failed to enter ulpm\n");
3959                         goto fail;
3960                 }
3961         }
3962
3963         val = DSI_PAD_CONTROL_PAD_PDIO(0x3) |
3964                 DSI_PAD_CONTROL_PAD_PDIO_CLK(0x1) |
3965                 DSI_PAD_CONTROL_PAD_PULLDN_ENAB(TEGRA_DSI_ENABLE);
3966         tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL);
3967
3968         /* Suspend core-logic */
3969         val = DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_DISABLE);
3970         tegra_dsi_writel(dsi, val, DSI_POWER_CONTROL);
3971
3972         /* Disable dsi fast and slow clock */
3973         tegra_dsi_config_phy_clk(dsi, TEGRA_DSI_DISABLE);
3974
3975         /* Disable dsi source clock */
3976         tegra_dsi_clk_disable(dsi);
3977
3978         regulator_disable(dsi->avdd_dsi_csi);
3979
3980         dsi->enabled = false;
3981         dsi->host_suspended = true;
3982
3983         return 0;
3984 fail:
3985         return err;
3986 }
3987
3988 static int tegra_dsi_host_resume(struct tegra_dc *dc)
3989 {
3990         int err = 0;
3991         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
3992
3993         if (!dsi->enabled)
3994                 return -EINVAL;
3995
3996         mutex_lock(&dsi->host_lock);
3997         cancel_delayed_work_sync(&dsi->idle_work);
3998         if (!dsi->host_suspended) {
3999                 mutex_unlock(&dsi->host_lock);
4000                 return 0;
4001         }
4002
4003         tegra_dc_io_start(dc);
4004
4005         err = _tegra_dsi_host_resume(dc, dsi, dsi->info.suspend_aggr);
4006         if (err < 0) {
4007                 dev_err(&dc->ndev->dev,
4008                         "DSI host resume failed\n");
4009                 goto fail;
4010         }
4011
4012         tegra_dsi_start_dc_stream(dc, dsi);
4013         dsi->host_suspended = false;
4014 fail:
4015         tegra_dc_io_end(dc);
4016         mutex_unlock(&dsi->host_lock);
4017         return err;
4018 }
4019
4020 static void _tegra_dc_dsi_disable(struct tegra_dc *dc)
4021 {
4022         int err;
4023         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
4024
4025         if (dsi->host_suspended)
4026                 tegra_dsi_host_resume(dc);
4027
4028         mutex_lock(&dsi->lock);
4029         tegra_dc_io_start(dc);
4030
4031         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
4032                 tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi, 2);
4033
4034         if (dsi->out_ops && dsi->out_ops->disable)
4035                 dsi->out_ops->disable(dsi);
4036
4037         if (dsi->info.power_saving_suspend) {
4038                 if (tegra_dsi_deep_sleep(dc, dsi) < 0) {
4039                         dev_err(&dc->ndev->dev,
4040                                 "DSI failed to enter deep sleep\n");
4041                         goto fail;
4042                 }
4043         } else {
4044                 if (dsi->info.dsi_early_suspend_cmd) {
4045                         err = tegra_dsi_send_panel_cmd(dc, dsi,
4046                                 dsi->info.dsi_early_suspend_cmd,
4047                                 dsi->info.n_early_suspend_cmd);
4048                         if (err < 0) {
4049                                 dev_err(&dc->ndev->dev,
4050                                 "dsi: Error sending early suspend cmd\n");
4051                                 goto fail;
4052                         }
4053                 }
4054
4055                 if (!dsi->ulpm) {
4056                         if (tegra_dsi_enter_ulpm(dsi) < 0) {
4057                                 dev_err(&dc->ndev->dev,
4058                                         "DSI failed to enter ulpm\n");
4059                                 goto fail;
4060                         }
4061                 }
4062         }
4063 fail:
4064         mutex_unlock(&dsi->lock);
4065         tegra_dc_io_end(dc);
4066 }
4067
4068 static void tegra_dc_dsi_disable(struct tegra_dc *dc)
4069 {
4070         _tegra_dc_dsi_disable(dc);
4071
4072         if (dc->out->dsi->ganged_type) {
4073                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_1]);
4074                 _tegra_dc_dsi_disable(dc);
4075                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_0]);
4076         }
4077 }
4078
4079
4080 #ifdef CONFIG_PM
4081 static void _tegra_dc_dsi_suspend(struct tegra_dc *dc)
4082 {
4083         struct tegra_dc_dsi_data *dsi;
4084
4085         dsi = tegra_dc_get_outdata(dc);
4086
4087         if (!dsi->enabled)
4088                 return;
4089
4090         if (dsi->host_suspended)
4091                 tegra_dsi_host_resume(dc);
4092
4093         tegra_dc_io_start(dc);
4094         mutex_lock(&dsi->lock);
4095
4096         if (dsi->out_ops && dsi->out_ops->suspend)
4097                 dsi->out_ops->suspend(dsi);
4098
4099         if (!dsi->info.power_saving_suspend) {
4100                 if (dsi->ulpm) {
4101                         if (tegra_dsi_exit_ulpm(dsi) < 0) {
4102                                 dev_err(&dc->ndev->dev,
4103                                         "DSI failed to exit ulpm");
4104                                 goto fail;
4105                         }
4106                 }
4107
4108                 if (tegra_dsi_deep_sleep(dc, dsi) < 0) {
4109                         dev_err(&dc->ndev->dev,
4110                                 "DSI failed to enter deep sleep\n");
4111                         goto fail;
4112                 }
4113         }
4114 fail:
4115         mutex_unlock(&dsi->lock);
4116         tegra_dc_io_end(dc);
4117 }
4118
4119 static void _tegra_dc_dsi_resume(struct tegra_dc *dc)
4120 {
4121         struct tegra_dc_dsi_data *dsi;
4122
4123         dsi = tegra_dc_get_outdata(dc);
4124
4125         /* No dsi config required since tegra_dc_dsi_enable
4126          * will reconfigure the controller from scratch
4127          */
4128
4129          if (dsi->out_ops && dsi->out_ops->resume)
4130                 dsi->out_ops->resume(dsi);
4131 }
4132
4133 static void tegra_dc_dsi_suspend(struct tegra_dc *dc)
4134 {
4135         _tegra_dc_dsi_suspend(dc);
4136
4137         if (dc->out->dsi->ganged_type) {
4138                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_1]);
4139                 _tegra_dc_dsi_suspend(dc);
4140                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_0]);
4141         }
4142 }
4143
4144 static void tegra_dc_dsi_resume(struct tegra_dc *dc)
4145 {
4146         _tegra_dc_dsi_resume(dc);
4147
4148         if (dc->out->dsi->ganged_type) {
4149                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_1]);
4150                 _tegra_dc_dsi_resume(dc);
4151                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_0]);
4152         }
4153 }
4154 #endif
4155
4156 static int tegra_dc_dsi_init(struct tegra_dc *dc)
4157 {
4158         struct tegra_dc_dsi_data *dsi;
4159         int err = 0;
4160
4161         err = _tegra_dc_dsi_init(dc);
4162         if (err < 0) {
4163                 dev_err(&dc->ndev->dev,
4164                         "dsi: Instance A init failed\n");
4165                 goto err;
4166         }
4167
4168         dsi = tegra_dc_get_outdata(dc);
4169
4170         dsi->avdd_dsi_csi =  regulator_get(&dc->ndev->dev, "avdd_dsi_csi");
4171         if (IS_ERR_OR_NULL(dsi->avdd_dsi_csi)) {
4172                 dev_err(&dc->ndev->dev, "dsi: avdd_dsi_csi reg get failed\n");
4173                 err = PTR_ERR(dsi->avdd_dsi_csi);
4174                 goto err_reg;
4175         }
4176
4177         dsi->mipi_cal = tegra_mipi_cal_init_sw(dc);
4178         if (IS_ERR(dsi->mipi_cal)) {
4179                 dev_err(&dc->ndev->dev, "dsi: mipi_cal sw init failed\n");
4180                 err = PTR_ERR(dsi->mipi_cal);
4181                 goto err_mipi;
4182         }
4183
4184         if (dc->out->dsi->ganged_type) {
4185                 err = _tegra_dc_dsi_init(dc);
4186                 if (err < 0) {
4187                         dev_err(&dc->ndev->dev,
4188                                 "dsi: Instance B init failed\n");
4189                         goto err_ganged;
4190                 }
4191                 tegra_dsi_instance[DSI_INSTANCE_1]->avdd_dsi_csi =
4192                                                         dsi->avdd_dsi_csi;
4193                 tegra_dsi_instance[DSI_INSTANCE_1]->mipi_cal =
4194                                                         dsi->mipi_cal;
4195                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_0]);
4196         }
4197         return 0;
4198 err_ganged:
4199         tegra_mipi_cal_destroy(dc);
4200 err_mipi:
4201         regulator_put(dsi->avdd_dsi_csi);
4202 err_reg:
4203         _tegra_dc_dsi_destroy(dc);
4204 err:
4205         return err;
4206 }
4207
4208 static void tegra_dc_dsi_destroy(struct tegra_dc *dc)
4209 {
4210         struct regulator *avdd_dsi_csi;
4211         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
4212
4213         avdd_dsi_csi = dsi->avdd_dsi_csi;
4214
4215         _tegra_dc_dsi_destroy(dc);
4216
4217         if (dc->out->dsi->ganged_type) {
4218                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_1]);
4219                 _tegra_dc_dsi_destroy(dc);
4220                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_0]);
4221         }
4222
4223         regulator_put(avdd_dsi_csi);
4224         tegra_mipi_cal_destroy(dc);
4225 }
4226
4227 static void tegra_dc_dsi_enable(struct tegra_dc *dc)
4228 {
4229         _tegra_dc_dsi_enable(dc);
4230
4231         if (dc->out->dsi->ganged_type) {
4232                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_1]);
4233                 _tegra_dc_dsi_enable(dc);
4234                 tegra_dc_set_outdata(dc, tegra_dsi_instance[DSI_INSTANCE_0]);
4235         }
4236 }
4237
4238 static long tegra_dc_dsi_setup_clk(struct tegra_dc *dc, struct clk *clk)
4239 {
4240         unsigned long rate;
4241         struct clk *parent_clk;
4242         struct clk *base_clk;
4243         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
4244
4245         /* divide by 1000 to avoid overflow */
4246         dc->mode.pclk /= 1000;
4247         rate = (dc->mode.pclk * dc->shift_clk_div.mul * 2)
4248                                 / dc->shift_clk_div.div;
4249         rate *= 1000;
4250         dc->mode.pclk *= 1000;
4251
4252         if (dc->out->flags & TEGRA_DC_OUT_INITIALIZED_MODE)
4253                 goto skip_setup;
4254
4255         if (clk == dc->clk) {
4256                 parent_clk = clk_get_sys(NULL,
4257                                 dc->out->parent_clk ? : "pll_d_out0");
4258                 base_clk = clk_get_parent(parent_clk);
4259         } else {
4260                 if (dc->pdata->default_out->dsi->dsi_instance) {
4261                         parent_clk = clk_get_sys(NULL,
4262                                 dc->out->parent_clk ? : "pll_d2_out0");
4263                         base_clk = clk_get_parent(parent_clk);
4264                 } else {
4265                         parent_clk = clk_get_sys(NULL,
4266                                 dc->out->parent_clk ? : "pll_d_out0");
4267                         base_clk = clk_get_parent(parent_clk);
4268                 }
4269         }
4270         tegra_dsi_config_phy_clk(dsi, TEGRA_DSI_ENABLE);
4271
4272         if (rate != clk_get_rate(base_clk))
4273                 clk_set_rate(base_clk, rate);
4274
4275         if (clk_get_parent(clk) != parent_clk)
4276                 clk_set_parent(clk, parent_clk);
4277
4278 skip_setup:
4279         tegra_dvfs_set_rate(dc->clk, dc->mode.pclk);
4280
4281         return tegra_dc_pclk_round_rate(dc, dc->mode.pclk);
4282 }
4283
4284 struct tegra_dc_out_ops tegra_dc_dsi_ops = {
4285         .init = tegra_dc_dsi_init,
4286         .destroy = tegra_dc_dsi_destroy,
4287         .enable = tegra_dc_dsi_enable,
4288         .disable = tegra_dc_dsi_disable,
4289         .hold = tegra_dc_dsi_hold_host,
4290         .release = tegra_dc_dsi_release_host,
4291 #ifdef CONFIG_PM
4292         .suspend = tegra_dc_dsi_suspend,
4293         .resume = tegra_dc_dsi_resume,
4294 #endif
4295         .setup_clk = tegra_dc_dsi_setup_clk,
4296 };