d60fb084eab9990016aff5cd4aa9f5d4990440cd
[linux-2.6.git] / drivers / video / tegra / dc / dsi.c
1 /*
2  * drivers/video/tegra/dc/dsi.c
3  *
4  * Copyright (c) 2011, NVIDIA Corporation.
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/clk.h>
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/fb.h>
21 #include <linux/gpio.h>
22 #include <linux/interrupt.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
26 #include <linux/workqueue.h>
27
28 #include <mach/clk.h>
29 #include <mach/dc.h>
30 #include <mach/fb.h>
31 #include <mach/nvhost.h>
32 #include <../gpio-names.h>
33
34 #include "dc_reg.h"
35 #include "dc_priv.h"
36 #include "dsi_regs.h"
37 #include "dsi.h"
38
39 #define DSI_USE_SYNC_POINTS 1
40
41 #define DSI_STOP_DC_DURATION_MSEC 1000
42
43 #define DSI_MODULE_NOT_INIT             0x0
44 #define DSI_MODULE_INIT                 0x1
45
46 #define DSI_LPHS_NOT_INIT               0x0
47 #define DSI_LPHS_IN_LP_MODE             0x1
48 #define DSI_LPHS_IN_HS_MODE             0x2
49
50 #define DSI_VIDEO_TYPE_NOT_INIT         0x0
51 #define DSI_VIDEO_TYPE_VIDEO_MODE       0x1
52 #define DSI_VIDEO_TYPE_CMD_MODE         0x2
53
54 #define DSI_DRIVEN_MODE_NOT_INIT        0x0
55 #define DSI_DRIVEN_MODE_DC              0x1
56 #define DSI_DRIVEN_MODE_HOST            0x2
57
58 #define DSI_PHYCLK_OUT_DIS              0x0
59 #define DSI_PHYCLK_OUT_EN               0x1
60
61 #define DSI_PHYCLK_NOT_INIT             0x0
62 #define DSI_PHYCLK_CONTINUOUS           0x1
63 #define DSI_PHYCLK_TX_ONLY              0x2
64
65 #define DSI_CLK_BURST_NOT_INIT          0x0
66 #define DSI_CLK_BURST_NONE_BURST        0x1
67 #define DSI_CLK_BURST_BURST_MODE        0x2
68
69 #define DSI_DC_STREAM_DISABLE           0x0
70 #define DSI_DC_STREAM_ENABLE            0x1
71
72 struct dsi_status {
73         unsigned        init:2;
74
75         unsigned        lphs:2;
76
77         unsigned        vtype:2;
78         unsigned        driven:2;
79
80         unsigned        clk_out:2;
81         unsigned        clk_mode:2;
82         unsigned        clk_burst:2;
83
84         unsigned        dc_stream:1;
85 };
86
87 /* source of video data */
88 enum{
89         TEGRA_DSI_DRIVEN_BY_DC,
90         TEGRA_DSI_DRIVEN_BY_HOST,
91 };
92
93 struct tegra_dc_dsi_data {
94         struct tegra_dc *dc;
95         void __iomem    *base;
96         struct resource *base_res;
97
98         struct clk              *dc_clk;
99         struct clk              *dsi_clk;
100         bool                    clk_ref;
101
102         struct mutex    lock;
103
104         /* data from board info */
105         struct tegra_dsi_out info;
106
107         struct dsi_status       status;
108
109         u8              driven_mode;
110         u8              controller_index;
111
112         u8              pixel_scaler_mul;
113         u8              pixel_scaler_div;
114
115         u32             default_shift_clk_div;
116         u32             default_pixel_clk_khz;
117         u32             default_hs_clk_khz;
118
119         u32             shift_clk_div;
120         u32             target_hs_clk_khz;
121         u32             target_lp_clk_khz;
122
123         u32             syncpt_id;
124         u32             syncpt_val;
125
126         u16             current_bit_clk_ns;
127         u32             current_dsi_clk_khz;
128
129         u32             dsi_control_val;
130
131         bool            ulpm;
132         bool            enabled;
133 };
134
135 const u32 dsi_pkt_seq_reg[NUMOF_PKT_SEQ] = {
136         DSI_PKT_SEQ_0_LO,
137         DSI_PKT_SEQ_0_HI,
138         DSI_PKT_SEQ_1_LO,
139         DSI_PKT_SEQ_1_HI,
140         DSI_PKT_SEQ_2_LO,
141         DSI_PKT_SEQ_2_HI,
142         DSI_PKT_SEQ_3_LO,
143         DSI_PKT_SEQ_3_HI,
144         DSI_PKT_SEQ_4_LO,
145         DSI_PKT_SEQ_4_HI,
146         DSI_PKT_SEQ_5_LO,
147         DSI_PKT_SEQ_5_HI,
148 };
149
150 const u32 dsi_pkt_seq_video_non_burst_syne[NUMOF_PKT_SEQ] = {
151         PKT_ID0(CMD_VS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
152         0,
153         PKT_ID0(CMD_VE) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
154         0,
155         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
156         0,
157         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
158                 PKT_ID2(CMD_HE) | PKT_LEN2(0),
159         PKT_ID3(CMD_BLNK) | PKT_LEN3(2) | PKT_ID4(CMD_RGB) | PKT_LEN4(3) |
160                 PKT_ID5(CMD_BLNK) | PKT_LEN5(4),
161         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
162         0,
163         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
164                 PKT_ID2(CMD_HE) | PKT_LEN2(0),
165         PKT_ID3(CMD_BLNK) | PKT_LEN3(2) | PKT_ID4(CMD_RGB) | PKT_LEN4(3) |
166                 PKT_ID5(CMD_BLNK) | PKT_LEN5(4),
167 };
168
169 const u32 dsi_pkt_seq_video_non_burst[NUMOF_PKT_SEQ] = {
170         PKT_ID0(CMD_VS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
171         0,
172         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
173         0,
174         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
175         0,
176         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2) |
177                 PKT_ID2(CMD_RGB) | PKT_LEN2(3),
178         PKT_ID3(CMD_BLNK) | PKT_LEN3(4),
179         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
180         0,
181         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2) |
182                 PKT_ID2(CMD_RGB) | PKT_LEN2(3),
183         PKT_ID3(CMD_BLNK) | PKT_LEN3(4),
184 };
185
186 static const u32 dsi_pkt_seq_video_burst[NUMOF_PKT_SEQ] = {
187         PKT_ID0(CMD_VS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
188         0,
189         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
190         0,
191         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
192         0,
193         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2)|
194         PKT_ID2(CMD_RGB) | PKT_LEN2(3) | PKT_LP,
195         PKT_ID0(CMD_EOT) | PKT_LEN0(7),
196         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(7) | PKT_LP,
197         0,
198         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2)|
199         PKT_ID2(CMD_RGB) | PKT_LEN2(3) | PKT_LP,
200         PKT_ID0(CMD_EOT) | PKT_LEN0(7),
201 };
202
203 static const u32 dsi_pkt_seq_video_burst_no_eot[NUMOF_PKT_SEQ] = {
204         PKT_ID0(CMD_VS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
205         0,
206         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
207         0,
208         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
209         0,
210         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2)|
211         PKT_ID2(CMD_RGB) | PKT_LEN2(3) | PKT_LP,
212         PKT_ID0(CMD_EOT) | PKT_LEN0(0),
213         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_EOT) | PKT_LEN1(0) | PKT_LP,
214         0,
215         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(2)|
216         PKT_ID2(CMD_RGB) | PKT_LEN2(3) | PKT_LP,
217         PKT_ID0(CMD_EOT) | PKT_LEN0(0),
218 };
219
220 /* TODO: verify with hw about this format */
221 const u32 dsi_pkt_seq_cmd_mode[NUMOF_PKT_SEQ] = {
222         0,
223         0,
224         0,
225         0,
226         0,
227         0,
228         PKT_ID0(CMD_LONGW) | PKT_LEN0(3) | PKT_ID1(CMD_EOT) | PKT_LEN1(7),
229         0,
230         0,
231         0,
232         PKT_ID0(CMD_LONGW) | PKT_LEN0(3) | PKT_ID1(CMD_EOT) | PKT_LEN1(7),
233         0,
234 };
235
236 const u32 init_reg[] = {
237         DSI_INT_ENABLE,
238         DSI_INT_STATUS,
239         DSI_INT_MASK,
240         DSI_INIT_SEQ_DATA_0,
241         DSI_INIT_SEQ_DATA_1,
242         DSI_INIT_SEQ_DATA_2,
243         DSI_INIT_SEQ_DATA_3,
244         DSI_DCS_CMDS,
245         DSI_PKT_SEQ_0_LO,
246         DSI_PKT_SEQ_1_LO,
247         DSI_PKT_SEQ_2_LO,
248         DSI_PKT_SEQ_3_LO,
249         DSI_PKT_SEQ_4_LO,
250         DSI_PKT_SEQ_5_LO,
251         DSI_PKT_SEQ_0_HI,
252         DSI_PKT_SEQ_1_HI,
253         DSI_PKT_SEQ_2_HI,
254         DSI_PKT_SEQ_3_HI,
255         DSI_PKT_SEQ_4_HI,
256         DSI_PKT_SEQ_5_HI,
257         DSI_CONTROL,
258         DSI_HOST_DSI_CONTROL,
259         DSI_PAD_CONTROL,
260         DSI_PAD_CONTROL_CD,
261         DSI_SOL_DELAY,
262         DSI_MAX_THRESHOLD,
263         DSI_TRIGGER,
264         DSI_TX_CRC,
265         DSI_INIT_SEQ_CONTROL,
266         DSI_PKT_LEN_0_1,
267         DSI_PKT_LEN_2_3,
268         DSI_PKT_LEN_4_5,
269         DSI_PKT_LEN_6_7,
270 };
271
272 inline unsigned long tegra_dsi_readl(struct tegra_dc_dsi_data *dsi, u32 reg)
273 {
274         return readl(dsi->base + reg * 4);
275 }
276 EXPORT_SYMBOL(tegra_dsi_readl);
277
278 inline void tegra_dsi_writel(struct tegra_dc_dsi_data *dsi, u32 val, u32 reg)
279 {
280         writel(val, dsi->base + reg * 4);
281 }
282 EXPORT_SYMBOL(tegra_dsi_writel);
283
284 static int tegra_dsi_syncpt(struct tegra_dc_dsi_data *dsi)
285 {
286         u32 val;
287         int ret;
288
289         ret = 0;
290
291         dsi->syncpt_val = nvhost_syncpt_read(
292                         &dsi->dc->ndev->host->syncpt, dsi->syncpt_id);
293
294         val = DSI_INCR_SYNCPT_COND(OP_DONE) |
295                 DSI_INCR_SYNCPT_INDX(dsi->syncpt_id);
296         tegra_dsi_writel(dsi, val, DSI_INCR_SYNCPT);
297
298         /* TODO: Use interrupt rather than polling */
299         ret = nvhost_syncpt_wait(&dsi->dc->ndev->host->syncpt,
300                 dsi->syncpt_id, dsi->syncpt_val + 1);
301         if (ret < 0) {
302                 printk(KERN_ERR "DSI sync point failure\n");
303                 goto fail;
304         }
305
306         (dsi->syncpt_val)++;
307         return 0;
308 fail:
309         return ret;
310 }
311
312 static u32 tegra_dsi_get_hs_clk_rate(struct tegra_dc_dsi_data *dsi)
313 {
314         u32 dsi_clock_rate_khz;
315
316         switch (dsi->info.video_burst_mode) {
317         case TEGRA_DSI_VIDEO_BURST_MODE_LOW_SPEED:
318         case TEGRA_DSI_VIDEO_BURST_MODE_MEDIUM_SPEED:
319         case TEGRA_DSI_VIDEO_BURST_MODE_FAST_SPEED:
320         case TEGRA_DSI_VIDEO_BURST_MODE_FASTEST_SPEED:
321                 /* Calculate DSI HS clock rate for DSI burst mode */
322                 dsi_clock_rate_khz = dsi->default_pixel_clk_khz *
323                                                         dsi->shift_clk_div;
324                 break;
325         case TEGRA_DSI_VIDEO_NONE_BURST_MODE:
326         case TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END:
327         case TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED:
328         default:
329                 /* Clock rate is default DSI clock rate for non-burst mode */
330                 dsi_clock_rate_khz = dsi->default_hs_clk_khz;
331                 break;
332         }
333
334         return dsi_clock_rate_khz;
335 }
336
337 static u32 tegra_dsi_get_lp_clk_rate(struct tegra_dc_dsi_data *dsi)
338 {
339         u32 dsi_clock_rate_khz;
340
341         if (dsi->info.enable_hs_clock_on_lp_cmd_mode)
342                 if (dsi->info.hs_clk_in_lp_cmd_mode_freq_khz)
343                         dsi_clock_rate_khz =
344                                 dsi->info.hs_clk_in_lp_cmd_mode_freq_khz;
345                 else
346                         dsi_clock_rate_khz = tegra_dsi_get_hs_clk_rate(dsi);
347         else
348                 dsi_clock_rate_khz = dsi->info.lp_cmd_mode_freq_khz;
349
350         return dsi_clock_rate_khz;
351 }
352
353 static u32 tegra_dsi_get_shift_clk_div(struct tegra_dc_dsi_data *dsi)
354 {
355         u32 shift_clk_div;
356         u32 max_shift_clk_div;
357         u32 burst_width;
358         u32 burst_width_max;
359
360         /* Get the real value of default shift_clk_div. default_shift_clk_div
361          * holds the real value of shift_clk_div. */
362         shift_clk_div = dsi->default_shift_clk_div;
363
364         /* Calculate shift_clk_div which can matche the video_burst_mode.*/
365         if (dsi->info.video_burst_mode >=
366                         TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED) {
367                 /* The max_shift_clk_div is multiplied by 10 to save the
368                  * fraction */
369                 if (dsi->info.max_panel_freq_khz >= dsi->default_hs_clk_khz)
370                         max_shift_clk_div = dsi->info.max_panel_freq_khz
371                                 * shift_clk_div * 10 / dsi->default_hs_clk_khz;
372                 else
373                         max_shift_clk_div = shift_clk_div * 10;
374
375                 burst_width = dsi->info.video_burst_mode
376                                 - TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED;
377                 burst_width_max = TEGRA_DSI_VIDEO_BURST_MODE_FASTEST_SPEED
378                                 - TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED;
379
380                 shift_clk_div = (max_shift_clk_div - shift_clk_div * 10) *
381                         burst_width / (burst_width_max * 10) + shift_clk_div;
382         }
383
384         return shift_clk_div;
385 }
386
387 static void tegra_dsi_init_sw(struct tegra_dc *dc,
388                                                 struct tegra_dc_dsi_data *dsi)
389 {
390         u32 h_width_pixels;
391         u32 v_width_lines;
392         u32 pixel_clk_hz;
393         u32 byte_clk_hz;
394         u32 plld_clk_mhz;
395
396         switch (dsi->info.pixel_format) {
397         case TEGRA_DSI_PIXEL_FORMAT_16BIT_P:
398                 /* 2 bytes per pixel */
399                 dsi->pixel_scaler_mul = 2;
400                 dsi->pixel_scaler_div = 1;
401                 break;
402         case TEGRA_DSI_PIXEL_FORMAT_18BIT_P:
403                 /* 2.25 bytes per pixel */
404                 dsi->pixel_scaler_mul = 9;
405                 dsi->pixel_scaler_div = 4;
406                 break;
407         case TEGRA_DSI_PIXEL_FORMAT_18BIT_NP:
408         case TEGRA_DSI_PIXEL_FORMAT_24BIT_P:
409                 /* 3 bytes per pixel */
410                 dsi->pixel_scaler_mul = 3;
411                 dsi->pixel_scaler_div = 1;
412                 break;
413         default:
414                 break;
415         }
416
417         dsi->controller_index = dc->ndev->id;
418         dsi->ulpm = false;
419         dsi->enabled = false;
420         dsi->clk_ref = false;
421
422         dsi->dsi_control_val =
423                         DSI_CONTROL_VIRTUAL_CHANNEL(dsi->info.virtual_channel) |
424                         DSI_CONTROL_NUM_DATA_LANES(dsi->info.n_data_lanes - 1) |
425                         DSI_CONTROL_VID_SOURCE(dsi->controller_index) |
426                         DSI_CONTROL_DATA_FORMAT(dsi->info.pixel_format);
427
428         /* Below we are going to calculate dsi and dc clock rate.
429          * Calcuate the horizontal and vertical width. */
430         h_width_pixels = dc->mode.h_back_porch + dc->mode.h_front_porch +
431                         dc->mode.h_sync_width + dc->mode.h_active;
432         v_width_lines = dc->mode.v_back_porch + dc->mode.v_front_porch +
433                         dc->mode.v_sync_width + dc->mode.v_active;
434
435         /* Calculate minimum required pixel rate. */
436         pixel_clk_hz = h_width_pixels * v_width_lines * dsi->info.refresh_rate;
437
438         dc->pixel_clk = pixel_clk_hz;
439
440         /* Calculate minimum byte rate on DSI interface. */
441         byte_clk_hz = (pixel_clk_hz * dsi->pixel_scaler_mul) /
442                         (dsi->pixel_scaler_div * dsi->info.n_data_lanes);
443
444         /* Round up to multiple of mega hz. */
445         plld_clk_mhz = DIV_ROUND_UP((byte_clk_hz * NUMOF_BIT_PER_BYTE),
446                                                                 1000000);
447
448         /* Calculate default real shift_clk_div. */
449         dsi->default_shift_clk_div = (NUMOF_BIT_PER_BYTE / 2) *
450                 dsi->pixel_scaler_mul / (dsi->pixel_scaler_div *
451                 dsi->info.n_data_lanes);
452         /* Calculate default DSI hs clock. DSI interface is double data rate.
453          * Data is transferred on both rising and falling edge of clk, div by 2
454          * to get the actual clock rate.
455          * */
456         dsi->default_hs_clk_khz = plld_clk_mhz * 1000 / 2;
457         dsi->default_pixel_clk_khz = plld_clk_mhz * 1000 / 2
458                                                 / dsi->default_shift_clk_div;
459
460         /* Get the actual shift_clk_div and clock rates. */
461         dsi->shift_clk_div = tegra_dsi_get_shift_clk_div(dsi);
462         dsi->target_lp_clk_khz = tegra_dsi_get_lp_clk_rate(dsi);
463         dsi->target_hs_clk_khz = tegra_dsi_get_hs_clk_rate(dsi);
464
465         dev_info(&dc->ndev->dev, "DSI: HS clock rate is %d\n",
466                                                         dsi->target_hs_clk_khz);
467
468         dsi->controller_index = dc->ndev->id;
469
470 #if DSI_USE_SYNC_POINTS
471         dsi->syncpt_id = NVSYNCPT_DSI;
472 #endif
473
474         /*
475          * Force video clock to be continuous mode if
476          * enable_hs_clock_on_lp_cmd_mode is set
477          */
478         if (dsi->info.enable_hs_clock_on_lp_cmd_mode) {
479                 if (dsi->info.video_clock_mode !=
480                                         TEGRA_DSI_VIDEO_CLOCK_CONTINUOUS)
481                         printk("Force to clock continuous mode\n");
482
483                 dsi->info.video_clock_mode = TEGRA_DSI_VIDEO_CLOCK_CONTINUOUS;
484         }
485
486 }
487
488 static void tegra_dsi_get_phy_timing(struct tegra_dc_dsi_data *dsi,
489                                 struct dsi_phy_timing_inclk *phy_timing_clk,
490                                 u32 clk_ns)
491 {
492
493         phy_timing_clk->t_hsdexit = dsi->info.phy_timing.t_hsdexit_ns ?
494                         (dsi->info.phy_timing.t_hsdexit_ns / clk_ns) :
495                         (T_HSEXIT_DEFAULT(clk_ns));
496
497         phy_timing_clk->t_hstrail = dsi->info.phy_timing.t_hstrail_ns ?
498                         (dsi->info.phy_timing.t_hstrail_ns / clk_ns) :
499                         (T_HSTRAIL_DEFAULT(clk_ns));
500
501         phy_timing_clk->t_datzero = dsi->info.phy_timing.t_datzero_ns ?
502                         (dsi->info.phy_timing.t_datzero_ns / clk_ns) :
503                         (T_DATZERO_DEFAULT(clk_ns));
504
505         phy_timing_clk->t_hsprepr = dsi->info.phy_timing.t_hsprepr_ns ?
506                         (dsi->info.phy_timing.t_hsprepr_ns / clk_ns) :
507                         (T_HSPREPR_DEFAULT(clk_ns));
508
509         phy_timing_clk->t_clktrail = dsi->info.phy_timing.t_clktrail_ns ?
510                                 (dsi->info.phy_timing.t_clktrail_ns / clk_ns) :
511                                 (T_CLKTRAIL_DEFAULT(clk_ns));
512
513         phy_timing_clk->t_clkpost = dsi->info.phy_timing.t_clkpost_ns ?
514                                 (dsi->info.phy_timing.t_clkpost_ns / clk_ns) :
515                                 (T_CLKPOST_DEFAULT(clk_ns));
516
517         phy_timing_clk->t_clkzero = dsi->info.phy_timing.t_clkzero_ns ?
518                                 (dsi->info.phy_timing.t_clkzero_ns / clk_ns) :
519                                 (T_CLKZERO_DEFAULT(clk_ns));
520
521         phy_timing_clk->t_tlpx = dsi->info.phy_timing.t_tlpx_ns ?
522                                 (dsi->info.phy_timing.t_tlpx_ns / clk_ns) :
523                                 (T_TLPX_DEFAULT(clk_ns));
524
525         phy_timing_clk->t_clkpre = T_CLKPRE_DEFAULT(clk_ns);
526         phy_timing_clk->t_clkprepare = T_CLKPREPARE_DEFAULT(clk_ns);
527         phy_timing_clk->t_wakeup = T_WAKEUP_DEFAULT(clk_ns);
528
529         phy_timing_clk->t_taget = 5 * phy_timing_clk->t_tlpx;
530         phy_timing_clk->t_tasure = 2 * phy_timing_clk->t_tlpx;
531         phy_timing_clk->t_tago = 4 * phy_timing_clk->t_tlpx;
532 }
533
534 static void tegra_dsi_set_phy_timing(struct tegra_dc_dsi_data *dsi)
535 {
536         u32 val;
537         struct dsi_phy_timing_inclk     phy_timing;
538
539         tegra_dsi_get_phy_timing(dsi, &phy_timing, dsi->current_bit_clk_ns);
540
541         val = DSI_PHY_TIMING_0_THSDEXIT(phy_timing.t_hsdexit) |
542                         DSI_PHY_TIMING_0_THSTRAIL(phy_timing.t_hstrail) |
543                         DSI_PHY_TIMING_0_TDATZERO(phy_timing.t_datzero) |
544                         DSI_PHY_TIMING_0_THSPREPR(phy_timing.t_hsprepr);
545         tegra_dsi_writel(dsi, val, DSI_PHY_TIMING_0);
546
547         val = DSI_PHY_TIMING_1_TCLKTRAIL(phy_timing.t_clktrail) |
548                         DSI_PHY_TIMING_1_TCLKPOST(phy_timing.t_clkpost) |
549                         DSI_PHY_TIMING_1_TCLKZERO(phy_timing.t_clkzero) |
550                         DSI_PHY_TIMING_1_TTLPX(phy_timing.t_tlpx);
551         tegra_dsi_writel(dsi, val, DSI_PHY_TIMING_1);
552
553         val = DSI_PHY_TIMING_2_TCLKPREPARE(phy_timing.t_clkprepare) |
554                 DSI_PHY_TIMING_2_TCLKPRE(phy_timing.t_clkpre) |
555                         DSI_PHY_TIMING_2_TWAKEUP(phy_timing.t_wakeup);
556         tegra_dsi_writel(dsi, val, DSI_PHY_TIMING_2);
557
558         val = DSI_BTA_TIMING_TTAGET(phy_timing.t_taget) |
559                         DSI_BTA_TIMING_TTASURE(phy_timing.t_tasure) |
560                         DSI_BTA_TIMING_TTAGO(phy_timing.t_tago);
561         tegra_dsi_writel(dsi, val, DSI_BTA_TIMING);
562 }
563
564 static u32 tegra_dsi_sol_delay_burst(struct tegra_dc *dc,
565                                                 struct tegra_dc_dsi_data *dsi)
566 {
567         u32 dsi_to_pixel_clk_ratio;
568         u32 temp;
569         u32 temp1;
570         u32 mipi_clk_adj_kHz;
571         u32 sol_delay;
572         struct tegra_dc_mode *dc_modes = &dc->mode;
573
574         /* Get Fdsi/Fpixel ration (note: Fdsi si in bit format) */
575         dsi_to_pixel_clk_ratio = (dsi->current_dsi_clk_khz * 2 +
576                 dsi->default_pixel_clk_khz - 1) / dsi->default_pixel_clk_khz;
577
578         /* Convert Fdsi to byte format */
579         dsi_to_pixel_clk_ratio *= 1000/8;
580
581         /* Multiplying by 1000 so that we don't loose the fraction part */
582         temp = dc_modes->h_active * 1000;
583         temp1 = dc_modes->h_active + dc_modes->h_back_porch +
584                         dc_modes->h_sync_width;
585
586         sol_delay = temp1 * dsi_to_pixel_clk_ratio -
587                         temp * dsi->pixel_scaler_mul /
588                         (dsi->pixel_scaler_div * dsi->info.n_data_lanes);
589
590         /* Do rounding on sol delay */
591         sol_delay = (sol_delay + 1000 - 1)/1000;
592
593         /* TODO:
594          * 1. find out the correct sol fifo depth to use
595          * 2. verify with hw about the clamping function
596          */
597         if (sol_delay > (480 * 4)) {
598                 sol_delay = (480 * 4);
599                 mipi_clk_adj_kHz = sol_delay +
600                         (dc_modes->h_active * dsi->pixel_scaler_mul) /
601                         (dsi->info.n_data_lanes * dsi->pixel_scaler_div);
602
603                 mipi_clk_adj_kHz *= (dsi->default_pixel_clk_khz / temp1);
604
605                 mipi_clk_adj_kHz *= 4;
606         }
607
608         dsi->target_hs_clk_khz = mipi_clk_adj_kHz;
609
610         return sol_delay;
611 }
612
613 static void tegra_dsi_set_sol_delay(struct tegra_dc *dc,
614                                                 struct tegra_dc_dsi_data *dsi)
615 {
616         u32 sol_delay;
617
618         if (dsi->info.video_burst_mode == TEGRA_DSI_VIDEO_NONE_BURST_MODE ||
619                 dsi->info.video_burst_mode ==
620                                 TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END) {
621                 sol_delay = NUMOF_BIT_PER_BYTE * dsi->pixel_scaler_mul /
622                         (dsi->pixel_scaler_div * dsi->info.n_data_lanes);
623                 dsi->status.clk_burst = DSI_CLK_BURST_NONE_BURST;
624         } else {
625                 sol_delay = tegra_dsi_sol_delay_burst(dc, dsi);
626                 dsi->status.clk_burst = DSI_CLK_BURST_BURST_MODE;
627         }
628
629         tegra_dsi_writel(dsi, DSI_SOL_DELAY_SOL_DELAY(sol_delay),
630                                                                 DSI_SOL_DELAY);
631 }
632
633 static void tegra_dsi_set_timeout(struct tegra_dc_dsi_data *dsi)
634 {
635         u32 val;
636         u32 bytes_per_frame;
637         u32 timeout = 0;
638
639         /* TODO: verify the following eq */
640         bytes_per_frame = dsi->current_dsi_clk_khz * 1000 * 2 /
641                                                 (dsi->info.refresh_rate * 8);
642         timeout = bytes_per_frame / DSI_CYCLE_COUNTER_VALUE;
643         timeout = (timeout + DSI_HTX_TO_MARGIN) & 0xffff;
644
645         val = DSI_TIMEOUT_0_LRXH_TO(DSI_LRXH_TO_VALUE) |
646                         DSI_TIMEOUT_0_HTX_TO(timeout);
647         tegra_dsi_writel(dsi, val, DSI_TIMEOUT_0);
648
649         if (dsi->info.panel_reset_timeout_msec)
650                 timeout = (dsi->info.panel_reset_timeout_msec * 1000*1000)
651                                         / dsi->current_bit_clk_ns;
652         else
653                 timeout = DSI_PR_TO_VALUE;
654
655         val = DSI_TIMEOUT_1_PR_TO(timeout) |
656                 DSI_TIMEOUT_1_TA_TO(DSI_TA_TO_VALUE);
657         tegra_dsi_writel(dsi, val, DSI_TIMEOUT_1);
658
659         val = DSI_TO_TALLY_P_RESET_STATUS(IN_RESET) |
660                 DSI_TO_TALLY_TA_TALLY(DSI_TA_TALLY_VALUE)|
661                 DSI_TO_TALLY_LRXH_TALLY(DSI_LRXH_TALLY_VALUE)|
662                 DSI_TO_TALLY_HTX_TALLY(DSI_HTX_TALLY_VALUE);
663         tegra_dsi_writel(dsi, val, DSI_TO_TALLY);
664 }
665
666 static void tegra_dsi_setup_video_mode_pkt_length(struct tegra_dc *dc,
667                                                 struct tegra_dc_dsi_data *dsi)
668 {
669         u32 val;
670         u32 hact_pkt_len;
671         u32 hsa_pkt_len;
672         u32 hbp_pkt_len;
673         u32 hfp_pkt_len;
674
675         hact_pkt_len = dc->mode.h_active * dsi->pixel_scaler_mul /
676                                                         dsi->pixel_scaler_div;
677         hsa_pkt_len = dc->mode.h_sync_width * dsi->pixel_scaler_mul /
678                                                         dsi->pixel_scaler_div;
679         hbp_pkt_len = dc->mode.h_back_porch * dsi->pixel_scaler_mul /
680                                                         dsi->pixel_scaler_div;
681         hfp_pkt_len = dc->mode.h_front_porch * dsi->pixel_scaler_mul /
682                                                         dsi->pixel_scaler_div;
683
684         if (dsi->info.video_burst_mode !=
685                                 TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END)
686                 hbp_pkt_len += hsa_pkt_len;
687
688         hsa_pkt_len -= DSI_HSYNC_BLNK_PKT_OVERHEAD;
689         hbp_pkt_len -= DSI_HBACK_PORCH_PKT_OVERHEAD;
690         hfp_pkt_len -= DSI_HFRONT_PORCH_PKT_OVERHEAD;
691
692         val = DSI_PKT_LEN_0_1_LENGTH_0(0) |
693                         DSI_PKT_LEN_0_1_LENGTH_1(hsa_pkt_len);
694         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_0_1);
695
696         val = DSI_PKT_LEN_2_3_LENGTH_2(hbp_pkt_len) |
697                         DSI_PKT_LEN_2_3_LENGTH_3(hact_pkt_len);
698         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_2_3);
699
700         val = DSI_PKT_LEN_4_5_LENGTH_4(hfp_pkt_len) |
701                         DSI_PKT_LEN_4_5_LENGTH_5(0);
702         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_4_5);
703
704         val = DSI_PKT_LEN_6_7_LENGTH_6(0) | DSI_PKT_LEN_6_7_LENGTH_7(0);
705         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_6_7);
706 }
707
708 static void tegra_dsi_setup_cmd_mode_pkt_length(struct tegra_dc *dc,
709                                                 struct tegra_dc_dsi_data *dsi)
710 {
711         unsigned long   val;
712         unsigned long   act_bytes;
713
714         act_bytes = dc->mode.h_active * dsi->pixel_scaler_mul /
715                         dsi->pixel_scaler_div + 1;
716
717         val = DSI_PKT_LEN_0_1_LENGTH_0(0) | DSI_PKT_LEN_0_1_LENGTH_1(0);
718         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_0_1);
719
720         val = DSI_PKT_LEN_2_3_LENGTH_2(0) | DSI_PKT_LEN_2_3_LENGTH_3(act_bytes);
721         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_2_3);
722
723         val = DSI_PKT_LEN_4_5_LENGTH_4(0) | DSI_PKT_LEN_4_5_LENGTH_5(act_bytes);
724         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_4_5);
725
726         val = DSI_PKT_LEN_6_7_LENGTH_6(0) | DSI_PKT_LEN_6_7_LENGTH_7(0x0f0f);
727         tegra_dsi_writel(dsi, val, DSI_PKT_LEN_6_7);
728 }
729
730 static void tegra_dsi_set_pkt_length(struct tegra_dc *dc,
731                                                 struct tegra_dc_dsi_data *dsi)
732 {
733         if (dsi->driven_mode == TEGRA_DSI_DRIVEN_BY_HOST)
734                 return;
735
736         if (dsi->info.video_data_type == TEGRA_DSI_VIDEO_TYPE_VIDEO_MODE)
737                 tegra_dsi_setup_video_mode_pkt_length(dc, dsi);
738         else
739                 tegra_dsi_setup_cmd_mode_pkt_length(dc, dsi);
740 }
741
742 static void tegra_dsi_set_pkt_seq(struct tegra_dc *dc,
743                                                 struct tegra_dc_dsi_data *dsi)
744 {
745         const u32 *pkt_seq;
746         u32 rgb_info;
747         u32 pkt_seq_3_5_rgb_lo;
748         u32 pkt_seq_3_5_rgb_hi;
749         u32     val;
750         u32 reg;
751         u8  i;
752
753         if (dsi->driven_mode == TEGRA_DSI_DRIVEN_BY_HOST)
754                 return;
755
756         switch (dsi->info.pixel_format) {
757         case TEGRA_DSI_PIXEL_FORMAT_16BIT_P:
758                 rgb_info = CMD_RGB_16BPP;
759                 break;
760         case TEGRA_DSI_PIXEL_FORMAT_18BIT_P:
761                 rgb_info = CMD_RGB_18BPP;
762                 break;
763         case TEGRA_DSI_PIXEL_FORMAT_18BIT_NP:
764                 rgb_info = CMD_RGB_18BPPNP;
765                 break;
766         case TEGRA_DSI_PIXEL_FORMAT_24BIT_P:
767         default:
768                 rgb_info = CMD_RGB_24BPP;
769                 break;
770         }
771
772         pkt_seq_3_5_rgb_lo = 0;
773         pkt_seq_3_5_rgb_hi = 0;
774         if (dsi->info.video_data_type == TEGRA_DSI_VIDEO_TYPE_COMMAND_MODE)
775                 pkt_seq = dsi_pkt_seq_cmd_mode;
776         else {
777                 switch (dsi->info.video_burst_mode) {
778                 case TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED:
779                 case TEGRA_DSI_VIDEO_BURST_MODE_LOW_SPEED:
780                 case TEGRA_DSI_VIDEO_BURST_MODE_MEDIUM_SPEED:
781                 case TEGRA_DSI_VIDEO_BURST_MODE_FAST_SPEED:
782                 case TEGRA_DSI_VIDEO_BURST_MODE_FASTEST_SPEED:
783                         pkt_seq_3_5_rgb_lo =
784                                         DSI_PKT_SEQ_3_LO_PKT_32_ID(rgb_info);
785                         if (!dsi->info.no_pkt_seq_eot)
786                                 pkt_seq = dsi_pkt_seq_video_burst;
787                         else
788                                 pkt_seq = dsi_pkt_seq_video_burst_no_eot;
789                         break;
790                 case TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END:
791                         pkt_seq_3_5_rgb_hi =
792                                         DSI_PKT_SEQ_3_HI_PKT_34_ID(rgb_info);
793                         pkt_seq = dsi_pkt_seq_video_non_burst_syne;
794                         break;
795                 case TEGRA_DSI_VIDEO_NONE_BURST_MODE:
796                 default:
797                         pkt_seq_3_5_rgb_lo =
798                                         DSI_PKT_SEQ_3_LO_PKT_32_ID(rgb_info);
799                         pkt_seq = dsi_pkt_seq_video_non_burst;
800                         break;
801                 }
802         }
803
804         for (i = 0; i < NUMOF_PKT_SEQ; i++) {
805                 val = pkt_seq[i];
806                 reg = dsi_pkt_seq_reg[i];
807                 if ((reg == DSI_PKT_SEQ_3_LO) || (reg == DSI_PKT_SEQ_5_LO))
808                         val |= pkt_seq_3_5_rgb_lo;
809                 if ((reg == DSI_PKT_SEQ_3_HI) || (reg == DSI_PKT_SEQ_5_HI))
810                         val |= pkt_seq_3_5_rgb_hi;
811                 tegra_dsi_writel(dsi, val, reg);
812         }
813 }
814
815 static void tegra_dsi_stop_dc_stream(struct tegra_dc *dc,
816                                                 struct tegra_dc_dsi_data *dsi)
817 {
818         tegra_dc_writel(dc, 0, DC_DISP_DISP_WIN_OPTIONS);
819         tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
820         tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
821
822         dsi->status.dc_stream = DSI_DC_STREAM_DISABLE;
823 }
824
825 void tegra_dsi_stop_dc_stream_at_frame_end(struct tegra_dc *dc,
826                                                 struct tegra_dc_dsi_data *dsi)
827 {
828         int val;
829         long timeout;
830
831         /* stop dc */
832         tegra_dsi_stop_dc_stream(dc, dsi);
833
834         /* enable vblank interrupt */
835         val = tegra_dc_readl(dc, DC_CMD_INT_ENABLE);
836         val |= V_BLANK_INT;
837         tegra_dc_writel(dc, val, DC_CMD_INT_ENABLE);
838
839         val = tegra_dc_readl(dc, DC_CMD_INT_MASK);
840         val |= V_BLANK_INT;
841         tegra_dc_writel(dc, val, DC_CMD_INT_MASK);
842
843         /* wait for vblank completion */
844         timeout = wait_for_completion_interruptible_timeout(
845                 &dc->vblank_complete, DSI_STOP_DC_DURATION_MSEC);
846
847         /* disable vblank interrupt */
848         val = tegra_dc_readl(dc, DC_CMD_INT_ENABLE);
849         val &= ~V_BLANK_INT;
850         tegra_dc_writel(dc, val, DC_CMD_INT_ENABLE);
851
852         if (timeout == 0)
853                 printk("Warning: dc dosen't stop at the end of the frame.\n");
854 }
855
856 static void tegra_dsi_start_dc_stream(struct tegra_dc *dc,
857                                                 struct tegra_dc_dsi_data *dsi)
858 {
859         u32 val;
860
861         tegra_dc_writel(dc, DSI_ENABLE, DC_DISP_DISP_WIN_OPTIONS);
862
863         /* TODO: clean up */
864         tegra_dc_writel(dc, PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
865                         PW4_ENABLE | PM0_ENABLE | PM1_ENABLE,
866                         DC_CMD_DISPLAY_POWER_CONTROL);
867
868         /* Configure one-shot mode or continuous mode */
869         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE) {
870                 /* disable LSPI/LCD_DE output */
871                 val = PIN_OUTPUT_LSPI_OUTPUT_DIS;
872                 tegra_dc_writel(dc, val, DC_COM_PIN_OUTPUT_ENABLE3);
873
874                 /* enable MSF & set MSF polarity */
875                 val = MSF_ENABLE | MSF_LSPI;
876                 if (!dsi->info.te_polarity_low)
877                         val |= MSF_POLARITY_HIGH;
878                 else
879                         val |= MSF_POLARITY_LOW;
880                 tegra_dc_writel(dc, val, DC_CMD_DISPLAY_COMMAND_OPTION0);
881
882                 /* set non-continuous mode */
883                 tegra_dc_writel(dc, DISP_CTRL_MODE_NC_DISPLAY,
884                                                 DC_CMD_DISPLAY_COMMAND);
885                 tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
886                 tegra_dc_writel(dc, GENERAL_ACT_REQ | NC_HOST_TRIG,
887                                                 DC_CMD_STATE_CONTROL);
888         } else {
889                 /* set continuous mode */
890                 tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY,
891                                                 DC_CMD_DISPLAY_COMMAND);
892                 tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
893                 tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
894         }
895
896         dsi->status.dc_stream = DSI_DC_STREAM_ENABLE;
897 }
898
899 static void tegra_dsi_set_dc_clk(struct tegra_dc *dc,
900                                                 struct tegra_dc_dsi_data *dsi)
901 {
902         u32 shift_clk_div_register;
903         u32 val;
904
905         /* Get the corresponding register value of shift_clk_div. */
906         shift_clk_div_register = dsi->shift_clk_div * 2 - 2;
907
908 #ifndef CONFIG_TEGRA_SILICON_PLATFORM
909         shift_clk_div_register = 1;
910 #endif
911
912         /* TODO: find out if PCD3 option is required */
913         val = PIXEL_CLK_DIVIDER_PCD1 |
914                                 SHIFT_CLK_DIVIDER(shift_clk_div_register);
915         tegra_dc_writel(dc, val, DC_DISP_DISP_CLOCK_CONTROL);
916 }
917
918 static void tegra_dsi_set_dsi_clk(struct tegra_dc *dc,
919                                         struct tegra_dc_dsi_data *dsi, u32 clk)
920 {
921         u32 rm;
922
923         rm = clk % 1000;
924         if (rm != 0)
925                 clk -= rm;
926
927         clk *= 2;       /*
928                          * Value for PLLD routine is required to be twice as
929                          * the desired clock rate
930                          */
931
932         dc->mode.pclk = clk*1000;
933         tegra_dc_setup_clk(dc, dsi->dsi_clk);
934         if (dsi->clk_ref == true)
935                 clk_disable(dsi->dsi_clk);
936         else
937                 dsi->clk_ref = true;
938         clk_enable(dsi->dsi_clk);
939         tegra_periph_reset_deassert(dsi->dsi_clk);
940
941         dsi->current_dsi_clk_khz = clk_get_rate(dsi->dsi_clk) / 1000;
942
943         dsi->current_bit_clk_ns =  1000*1000 / (dsi->current_dsi_clk_khz * 2);
944 }
945
946 static void tegra_dsi_hs_clk_out_enable(struct tegra_dc_dsi_data *dsi)
947 {
948         u32 val;
949         struct clk *base_clk;
950
951         val = tegra_dsi_readl(dsi, DSI_CONTROL);
952         val &= ~DSI_CONTROL_HS_CLK_CTRL(1);
953
954         if (dsi->info.video_clock_mode == TEGRA_DSI_VIDEO_CLOCK_CONTINUOUS) {
955                 val |= DSI_CONTROL_HS_CLK_CTRL(CONTINUOUS);
956                 dsi->status.clk_mode = DSI_PHYCLK_CONTINUOUS;
957         } else {
958                 val |= DSI_CONTROL_HS_CLK_CTRL(TX_ONLY);
959                 dsi->status.clk_mode = DSI_PHYCLK_TX_ONLY;
960         }
961         tegra_dsi_writel(dsi, val, DSI_CONTROL);
962
963         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
964         val &= ~DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(1);
965         val |= DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(TEGRA_DSI_HIGH);
966         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
967
968         dsi->status.clk_out = DSI_PHYCLK_OUT_EN;
969 }
970
971 static void tegra_dsi_hs_clk_out_enable_in_lp(struct tegra_dc_dsi_data *dsi)
972 {
973         u32 val;
974         tegra_dsi_hs_clk_out_enable(dsi);
975
976         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
977         val &= ~DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(1);
978         val |= DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(TEGRA_DSI_LOW);
979         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
980 }
981
982 static void tegra_dsi_hs_clk_out_disable(struct tegra_dc *dc,
983                                                 struct tegra_dc_dsi_data *dsi)
984 {
985         u32 val;
986         struct clk *base_clk;
987
988         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
989                 tegra_dsi_stop_dc_stream(dc, dsi);
990
991         val = tegra_dsi_readl(dsi, DSI_CONTROL);
992         val &= ~DSI_CONTROL_HS_CLK_CTRL(1);
993         val |= DSI_CONTROL_HS_CLK_CTRL(TX_ONLY);
994         tegra_dsi_writel(dsi, val, DSI_CONTROL);
995
996         /* TODO: issue a cmd */
997
998         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
999         val &= ~DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(1);
1000         val |= DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(TEGRA_DSI_LOW);
1001         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
1002
1003         dsi->status.clk_mode = DSI_PHYCLK_NOT_INIT;
1004         dsi->status.clk_out = DSI_PHYCLK_OUT_DIS;
1005 }
1006
1007 static void tegra_dsi_set_control_reg_lp(struct tegra_dc_dsi_data *dsi)
1008 {
1009         u32 dsi_control;
1010         u32 host_dsi_control;
1011         u32 max_threshold;
1012
1013         dsi_control = dsi->dsi_control_val | DSI_CTRL_HOST_DRIVEN;
1014         host_dsi_control = HOST_DSI_CTRL_COMMON |
1015                         HOST_DSI_CTRL_HOST_DRIVEN |
1016                         DSI_HOST_DSI_CONTROL_HIGH_SPEED_TRANS(TEGRA_DSI_LOW);
1017         max_threshold = DSI_MAX_THRESHOLD_MAX_THRESHOLD(DSI_HOST_FIFO_DEPTH);
1018
1019         tegra_dsi_writel(dsi, max_threshold, DSI_MAX_THRESHOLD);
1020         tegra_dsi_writel(dsi, dsi_control, DSI_CONTROL);
1021         tegra_dsi_writel(dsi, host_dsi_control, DSI_HOST_DSI_CONTROL);
1022
1023         dsi->status.driven = DSI_DRIVEN_MODE_HOST;
1024         dsi->status.clk_burst = DSI_CLK_BURST_NOT_INIT;
1025         dsi->status.vtype = DSI_VIDEO_TYPE_NOT_INIT;
1026 }
1027
1028 static void tegra_dsi_set_control_reg_hs(struct tegra_dc_dsi_data *dsi)
1029 {
1030         u32 dsi_control;
1031         u32 host_dsi_control;
1032         u32 max_threshold;
1033         u32 dcs_cmd;
1034
1035         dsi_control = dsi->dsi_control_val;
1036         host_dsi_control = HOST_DSI_CTRL_COMMON;
1037         max_threshold = 0;
1038         dcs_cmd = 0;
1039
1040         if (dsi->driven_mode == TEGRA_DSI_DRIVEN_BY_HOST) {
1041                 dsi_control |= DSI_CTRL_HOST_DRIVEN;
1042                 host_dsi_control |= HOST_DSI_CTRL_HOST_DRIVEN;
1043                 max_threshold =
1044                         DSI_MAX_THRESHOLD_MAX_THRESHOLD(DSI_HOST_FIFO_DEPTH);
1045                 dsi->status.driven = DSI_DRIVEN_MODE_HOST;
1046         } else {
1047                 dsi_control |= DSI_CTRL_DC_DRIVEN;
1048                 host_dsi_control |= HOST_DSI_CTRL_DC_DRIVEN;
1049                 max_threshold =
1050                         DSI_MAX_THRESHOLD_MAX_THRESHOLD(DSI_VIDEO_FIFO_DEPTH);
1051                 dsi->status.driven = DSI_DRIVEN_MODE_DC;
1052         }
1053
1054         if (dsi->info.video_data_type == TEGRA_DSI_VIDEO_TYPE_COMMAND_MODE) {
1055                 dsi_control |= DSI_CTRL_CMD_MODE;
1056                 host_dsi_control |= HOST_DSI_CTRL_CMD_MODE;
1057                 dcs_cmd = DSI_DCS_CMDS_LT5_DCS_CMD(DSI_WRITE_MEMORY_START)|
1058                         DSI_DCS_CMDS_LT3_DCS_CMD(DSI_WRITE_MEMORY_CONTINUE);
1059                 dsi->status.vtype = DSI_VIDEO_TYPE_CMD_MODE;
1060
1061         } else {
1062                 dsi_control |= DSI_CTRL_VIDEO_MODE;
1063                 host_dsi_control |= HOST_DSI_CTRL_VIDEO_MODE;
1064                 dsi->status.vtype = DSI_VIDEO_TYPE_VIDEO_MODE;
1065         }
1066
1067         tegra_dsi_writel(dsi, max_threshold, DSI_MAX_THRESHOLD);
1068         tegra_dsi_writel(dsi, dcs_cmd, DSI_DCS_CMDS);
1069         tegra_dsi_writel(dsi, dsi_control, DSI_CONTROL);
1070         tegra_dsi_writel(dsi, host_dsi_control, DSI_HOST_DSI_CONTROL);
1071 }
1072
1073 static int tegra_dsi_init_hw(struct tegra_dc *dc,
1074                                                 struct tegra_dc_dsi_data *dsi)
1075 {
1076         u32 val;
1077         u32 i;
1078
1079         tegra_dsi_set_dsi_clk(dc, dsi, dsi->target_lp_clk_khz);
1080         if (dsi->info.dsi_instance) {
1081                 /* TODO:Set the misc register*/
1082         }
1083
1084         /* TODO: only need to change the timing for bta */
1085         tegra_dsi_set_phy_timing(dsi);
1086
1087         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
1088                 tegra_dsi_stop_dc_stream(dc, dsi);
1089
1090         /* Initializing DSI registers */
1091         for (i = 0; i < ARRAY_SIZE(init_reg); i++)
1092                 tegra_dsi_writel(dsi, 0, init_reg[i]);
1093
1094         tegra_dsi_writel(dsi, dsi->dsi_control_val, DSI_CONTROL);
1095         /* Initialize DSI_PAD_CONTROL register. */
1096         val =   DSI_PAD_CONTROL_PAD_LPUPADJ(0x1) |
1097                 DSI_PAD_CONTROL_PAD_LPDNADJ(0x1) |
1098                 DSI_PAD_CONTROL_PAD_PREEMP_EN(0x1) |
1099                 DSI_PAD_CONTROL_PAD_SLEWDNADJ(0x6) |
1100                 DSI_PAD_CONTROL_PAD_SLEWUPADJ(0x6);
1101
1102         if (!dsi->ulpm) {
1103                 val |=  DSI_PAD_CONTROL_PAD_PDIO(0) |
1104                         DSI_PAD_CONTROL_PAD_PDIO_CLK(0) |
1105                         DSI_PAD_CONTROL_PAD_PULLDN_ENAB(TEGRA_DSI_DISABLE);
1106         } else {
1107                 val |=  DSI_PAD_CONTROL_PAD_PDIO(0x3) |
1108                         DSI_PAD_CONTROL_PAD_PDIO_CLK(0x1) |
1109                         DSI_PAD_CONTROL_PAD_PULLDN_ENAB(TEGRA_DSI_ENABLE);
1110         }
1111         tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL);
1112
1113         val = DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_ENABLE);
1114         tegra_dsi_writel(dsi, val, DSI_POWER_CONTROL);
1115
1116         while (tegra_dsi_readl(dsi, DSI_POWER_CONTROL) != val)
1117                 tegra_dsi_writel(dsi, val, DSI_POWER_CONTROL);
1118
1119         dsi->status.init = DSI_MODULE_INIT;
1120         dsi->status.lphs = DSI_LPHS_NOT_INIT;
1121         dsi->status.vtype = DSI_VIDEO_TYPE_NOT_INIT;
1122         dsi->status.driven = DSI_DRIVEN_MODE_NOT_INIT;
1123         dsi->status.clk_out = DSI_PHYCLK_OUT_DIS;
1124         dsi->status.clk_mode = DSI_PHYCLK_NOT_INIT;
1125         dsi->status.clk_burst = DSI_CLK_BURST_NOT_INIT;
1126         dsi->status.dc_stream = DSI_DC_STREAM_DISABLE;
1127
1128         return 0;
1129 }
1130
1131 static int tegra_dsi_set_to_lp_mode(struct tegra_dc *dc,
1132                                                 struct tegra_dc_dsi_data *dsi)
1133 {
1134         int     err;
1135
1136         if (dsi->status.init != DSI_MODULE_INIT) {
1137                 err = -EPERM;
1138                 goto fail;
1139         }
1140
1141         if (dsi->status.lphs == DSI_LPHS_IN_LP_MODE)
1142                 goto success;
1143
1144         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
1145                 tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi);
1146
1147         /* disable/enable hs clk according to enable_hs_clock_on_lp_cmd_mode */
1148         if ((dsi->status.clk_out == DSI_PHYCLK_OUT_EN) &&
1149                 (!dsi->info.enable_hs_clock_on_lp_cmd_mode))
1150                 tegra_dsi_hs_clk_out_disable(dc, dsi);
1151
1152         if (dsi->current_dsi_clk_khz != dsi->target_lp_clk_khz) {
1153                 tegra_dsi_set_dsi_clk(dc, dsi, dsi->target_lp_clk_khz);
1154                 tegra_dsi_set_timeout(dsi);
1155         }
1156
1157         tegra_dsi_set_control_reg_lp(dsi);
1158
1159         if ((dsi->status.clk_out == DSI_PHYCLK_OUT_DIS) &&
1160                 (dsi->info.enable_hs_clock_on_lp_cmd_mode))
1161                 tegra_dsi_hs_clk_out_enable_in_lp(dsi);
1162
1163 success:
1164         dsi->status.lphs = DSI_LPHS_IN_LP_MODE;
1165         err = 0;
1166 fail:
1167         return err;
1168 }
1169
1170 static int tegra_dsi_set_to_hs_mode(struct tegra_dc *dc,
1171                                                 struct tegra_dc_dsi_data *dsi)
1172 {
1173         int err;
1174
1175         if (dsi->status.init != DSI_MODULE_INIT) {
1176                 err = -EPERM;
1177                 goto fail;
1178         }
1179
1180         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
1181                 tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi);
1182
1183         if ((dsi->status.clk_out == DSI_PHYCLK_OUT_EN) &&
1184                 (!dsi->info.enable_hs_clock_on_lp_cmd_mode))
1185                 tegra_dsi_hs_clk_out_disable(dc, dsi);
1186
1187         if (dsi->current_dsi_clk_khz != dsi->target_hs_clk_khz) {
1188                 tegra_dsi_set_dsi_clk(dc, dsi, dsi->target_hs_clk_khz);
1189                 tegra_dsi_set_timeout(dsi);
1190         }
1191
1192         tegra_dsi_set_phy_timing(dsi);
1193
1194         if (dsi->driven_mode == TEGRA_DSI_DRIVEN_BY_DC) {
1195                 tegra_dsi_set_pkt_seq(dc, dsi);
1196                 tegra_dsi_set_pkt_length(dc, dsi);
1197                 tegra_dsi_set_sol_delay(dc, dsi);
1198                 tegra_dsi_set_dc_clk(dc, dsi);
1199         }
1200
1201         tegra_dsi_set_control_reg_hs(dsi);
1202
1203         if (dsi->status.clk_out == DSI_PHYCLK_OUT_DIS)
1204                 tegra_dsi_hs_clk_out_enable(dsi);
1205
1206         dsi->status.lphs = DSI_LPHS_IN_HS_MODE;
1207         err = 0;
1208 fail:
1209         return err;
1210 }
1211
1212 static bool tegra_dsi_is_controller_idle(struct tegra_dc_dsi_data *dsi)
1213 {
1214         u32 timeout = 0;
1215         bool retVal;
1216
1217         retVal = false;
1218         while (timeout <= DSI_MAX_COMMAND_DELAY_USEC) {
1219                 if (!tegra_dsi_readl(dsi, DSI_TRIGGER)) {
1220                         retVal = true;
1221                         break;
1222                 }
1223                 udelay(DSI_COMMAND_DELAY_STEPS_USEC);
1224                 timeout += DSI_COMMAND_DELAY_STEPS_USEC;
1225         }
1226
1227         return retVal;
1228 }
1229
1230 static int tegra_dsi_host_trigger(struct tegra_dc_dsi_data *dsi)
1231 {
1232         int status = 0;
1233
1234         if (tegra_dsi_readl(dsi, DSI_TRIGGER)) {
1235                 status = -EBUSY;
1236                 goto fail;
1237         }
1238
1239         tegra_dsi_writel(dsi,
1240                 DSI_TRIGGER_HOST_TRIGGER(TEGRA_DSI_ENABLE), DSI_TRIGGER);
1241
1242 #if DSI_USE_SYNC_POINTS
1243         status = tegra_dsi_syncpt(dsi);
1244         if (status < 0) {
1245                 printk(KERN_ERR "DSI syncpt for host trigger failed\n");
1246                 goto fail;
1247         }
1248 #else
1249         if (!tegra_dsi_is_controller_idle(dsi))
1250                 status = -EIO;
1251 #endif
1252
1253 fail:
1254         return status;
1255 }
1256
1257 static int _tegra_dsi_write_data(struct tegra_dc_dsi_data *dsi,
1258                                         u8 *pdata, u8 data_id, u16 data_len)
1259 {
1260         u8 virtual_channel;
1261         u8 *pval;
1262         u32 val;
1263         int err;
1264
1265         err = 0;
1266
1267         virtual_channel = dsi->info.virtual_channel <<
1268                                                 DSI_VIR_CHANNEL_BIT_POSITION;
1269
1270         /* always use hw for ecc */
1271         val = (virtual_channel | data_id) << 0 |
1272                         data_len << 8;
1273         tegra_dsi_writel(dsi, val, DSI_WR_DATA);
1274
1275         /* if pdata != NULL, pkt type is long pkt */
1276         if (pdata != NULL) {
1277                 while (data_len) {
1278                         if (data_len >= 4) {
1279                                 val = ((u32 *) pdata)[0];
1280                                 data_len -= 4;
1281                                 pdata += 4;
1282                         } else {
1283                                 val = 0;
1284                                 pval = (u8 *) &val;
1285                                 do
1286                                         *pval++ = *pdata++;
1287                                 while (--data_len);
1288                         }
1289                         tegra_dsi_writel(dsi, val, DSI_WR_DATA);
1290                 }
1291         }
1292
1293         err = tegra_dsi_host_trigger(dsi);
1294         if (err < 0)
1295                 printk(KERN_ERR "DSI host trigger failed\n");
1296
1297         return err;
1298 }
1299
1300 int tegra_dsi_write_data(struct tegra_dc *dc,
1301                                         struct tegra_dc_dsi_data *dsi,
1302                                         u8 *pdata, u8 data_id, u16 data_len)
1303 {
1304         bool switch_back_to_hs_mode;
1305         bool switch_back_to_dc_mode;
1306         int err;
1307
1308         err = 0;
1309         switch_back_to_hs_mode = false;
1310         switch_back_to_dc_mode = false;
1311
1312         if ((dsi->status.init != DSI_MODULE_INIT) ||
1313                 (dsi->status.lphs == DSI_LPHS_NOT_INIT)) {
1314                 err = -EPERM;
1315                 goto fail;
1316         }
1317
1318         if (!tegra_dsi_is_controller_idle(dsi)) {
1319                 err = -EBUSY;
1320                 goto fail;
1321         }
1322
1323         err = 0;
1324
1325         if (dsi->status.lphs == DSI_LPHS_IN_HS_MODE) {
1326                 if (dsi->info.hs_cmd_mode_supported) {
1327                         if (dsi->status.driven == DSI_DRIVEN_MODE_DC) {
1328                                 dsi->driven_mode = TEGRA_DSI_DRIVEN_BY_HOST;
1329                                 tegra_dsi_set_to_hs_mode(dc, dsi);
1330                                 switch_back_to_dc_mode = true;
1331                         }
1332                 } else {
1333                         tegra_dsi_set_to_lp_mode(dc, dsi);
1334                         switch_back_to_hs_mode = true;
1335                 }
1336         }
1337
1338         err = _tegra_dsi_write_data(dsi, pdata, data_id, data_len);
1339
1340
1341         if (switch_back_to_dc_mode)
1342                 dsi->driven_mode = TEGRA_DSI_DRIVEN_BY_DC;
1343         if (switch_back_to_dc_mode || switch_back_to_hs_mode)
1344                 tegra_dsi_set_to_hs_mode(dc, dsi);
1345
1346 fail:
1347         return err;
1348 }
1349 EXPORT_SYMBOL(tegra_dsi_write_data);
1350
1351 static int tegra_dsi_send_panel_cmd(struct tegra_dc *dc,
1352                                                 struct tegra_dc_dsi_data *dsi,
1353                                                 struct tegra_dsi_cmd *cmd,
1354                                                 u32 n_cmd)
1355 {
1356         u32 i;
1357         int err;
1358
1359         err = 0;
1360         for (i = 0; i < n_cmd; i++) {
1361                 struct tegra_dsi_cmd *cur_cmd;
1362                 cur_cmd = &cmd[i];
1363
1364                 if (cur_cmd->cmd_type == TEGRA_DSI_DELAY_MS)
1365                         mdelay(cur_cmd->sp_len_dly.delay_ms);
1366                 else {
1367                         err = tegra_dsi_write_data(dc, dsi,
1368                                                 cur_cmd->pdata,
1369                                                 cur_cmd->data_id,
1370                                                 cur_cmd->sp_len_dly.data_len);
1371                         if (err < 0)
1372                                 break;
1373                 }
1374         }
1375         return err;
1376 }
1377
1378 static int tegra_dsi_bta(struct tegra_dc_dsi_data *dsi)
1379 {
1380         u32 val;
1381         u32 poll_time;
1382         int err;
1383
1384         poll_time = 0;
1385         err = 0;
1386
1387         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
1388         val |= DSI_HOST_DSI_CONTROL_IMM_BTA(TEGRA_DSI_ENABLE);
1389         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
1390
1391 #if DSI_USE_SYNC_POINTS
1392         /* FIXME: Workaround for nvhost_syncpt_read */
1393         dsi->syncpt_val = nvhost_syncpt_update_min(
1394                         &dsi->dc->ndev->host->syncpt, dsi->syncpt_id);
1395
1396         val = DSI_INCR_SYNCPT_COND(OP_DONE) |
1397                 DSI_INCR_SYNCPT_INDX(dsi->syncpt_id);
1398         tegra_dsi_writel(dsi, val, DSI_INCR_SYNCPT);
1399
1400         /* TODO: Use interrupt rather than polling */
1401         err = nvhost_syncpt_wait(&dsi->dc->ndev->host->syncpt,
1402                 dsi->syncpt_id, dsi->syncpt_val + 1);
1403         if (err < 0)
1404                 printk(KERN_ERR "DSI sync point failure\n");
1405         else
1406                 (dsi->syncpt_val)++;
1407 #else
1408         while (poll_time <  DSI_STATUS_POLLING_DURATION_USEC) {
1409                 val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
1410                 val &= DSI_HOST_DSI_CONTROL_IMM_BTA(TEGRA_DSI_ENABLE);
1411                 if (!val)
1412                         break;
1413                 udelay(DSI_STATUS_POLLING_DELAY_USEC);
1414                 poll_time += DSI_STATUS_POLLING_DELAY_USEC;
1415         }
1416         if (poll_time > DSI_STATUS_POLLING_DURATION_USEC)
1417                 err = -EBUSY;
1418 #endif
1419
1420         return err;
1421 }
1422
1423 static void tegra_dsi_read_fifo(struct tegra_dc *dc,
1424                         struct tegra_dc_dsi_data *dsi,
1425                         u32 rd_fifo_cnt,
1426                         u8 *read_fifo)
1427 {
1428         u32 val;
1429         u32 i;
1430
1431         /* Read data from FIFO */
1432         for (i = 0; i < rd_fifo_cnt; i++) {
1433                 val = tegra_dsi_readl(dsi, DSI_RD_DATA);
1434                 printk(KERN_INFO "Read data[%d]: 0x%x\n", i, val);
1435                 memcpy(read_fifo, &val, 4);
1436                 read_fifo += 4;
1437         }
1438
1439         /* Make sure all the data is read from the FIFO */
1440         val = tegra_dsi_readl(dsi, DSI_STATUS);
1441         val &= DSI_STATUS_RD_FIFO_COUNT(0x1f);
1442         if (val)
1443                 dev_err(&dc->ndev->dev, "DSI FIFO_RD_CNT not zero"
1444                 " even after reading FIFO_RD_CNT words from read fifo\n");
1445 }
1446
1447 static int tegra_dsi_parse_read_response(struct tegra_dc *dc,
1448                                 u32 rd_fifo_cnt, u8 *read_fifo)
1449 {
1450         int err;
1451         u32 payload_size;
1452
1453         payload_size = 0;
1454         err = 0;
1455
1456         printk(KERN_INFO "escape sequence[0x%x]\n", read_fifo[0]);
1457         switch (read_fifo[4] & 0xff) {
1458         case GEN_LONG_RD_RES:
1459                 /* Fall through */
1460         case DCS_LONG_RD_RES:
1461                 payload_size = (read_fifo[5] |
1462                                 (read_fifo[6] << 8)) & 0xFFFF;
1463                 printk(KERN_INFO "Long read response Packet\n"
1464                                 "payload_size[0x%x]\n", payload_size);
1465                 break;
1466         case GEN_1_BYTE_SHORT_RD_RES:
1467                 /* Fall through */
1468         case DCS_1_BYTE_SHORT_RD_RES:
1469                 payload_size = 1;
1470                 printk(KERN_INFO "Short read response Packet\n"
1471                         "payload_size[0x%x]\n", payload_size);
1472                 break;
1473         case GEN_2_BYTE_SHORT_RD_RES:
1474                 /* Fall through */
1475         case DCS_2_BYTE_SHORT_RD_RES:
1476                 payload_size = 2;
1477                 printk(KERN_INFO "Short read response Packet\n"
1478                         "payload_size[0x%x]\n", payload_size);
1479                 break;
1480         case ACK_ERR_RES:
1481                 payload_size = 2;
1482                 printk(KERN_INFO "Acknowledge error report response\n"
1483                         "Packet payload_size[0x%x]\n", payload_size);
1484                 break;
1485         default:
1486                 /*reading from RD_FIFO_COUNT*/
1487                 printk(KERN_INFO "Invalid read response payload_size\n");
1488                 err = -EINVAL;
1489                 break;
1490         }
1491         return err;
1492 }
1493
1494 int tegra_dsi_read_data(struct tegra_dc *dc,
1495                                 struct tegra_dc_dsi_data *dsi,
1496                                 u32 max_ret_payload_size,
1497                                 u32 panel_reg_addr, u8 *read_data)
1498 {
1499         u32 val;
1500         int err;
1501         u32 poll_time;
1502         u32 rd_fifo_cnt;
1503         bool switch_back_to_hs_mode;
1504         bool restart_dc_stream;
1505         bool switch_back_to_dc_mode;
1506
1507         err = 0;
1508         switch_back_to_hs_mode = false;
1509         restart_dc_stream = false;
1510         switch_back_to_dc_mode = false;
1511
1512         if ((dsi->status.init != DSI_MODULE_INIT) ||
1513                 (dsi->status.lphs == DSI_LPHS_NOT_INIT) ||
1514                 (dsi->status.driven == DSI_DRIVEN_MODE_NOT_INIT)) {
1515                 err = -EPERM;
1516                 goto fail;
1517         }
1518
1519         val = tegra_dsi_readl(dsi, DSI_STATUS);
1520         val &= DSI_STATUS_RD_FIFO_COUNT(0x1f);
1521         if (val) {
1522                 err = -EBUSY;
1523                 dev_err(&dc->ndev->dev, "DSI fifo count not zero\n");
1524                 goto fail;
1525         }
1526
1527         if (!tegra_dsi_is_controller_idle(dsi)) {
1528                 err = -EBUSY;
1529                 dev_err(&dc->ndev->dev, "DSI trigger bit is already set\n");
1530                 goto fail;
1531         }
1532
1533         if (dsi->status.lphs == DSI_LPHS_IN_HS_MODE) {
1534                 if (dsi->status.driven == DSI_DRIVEN_MODE_DC) {
1535                         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
1536                                 restart_dc_stream = true;
1537                         dsi->driven_mode = TEGRA_DSI_DRIVEN_BY_HOST;
1538                         switch_back_to_dc_mode = true;
1539                         if (dsi->info.hs_cmd_mode_supported) {
1540                                 err = tegra_dsi_set_to_hs_mode(dc, dsi);
1541                                 if (err < 0) {
1542                                         dev_err(&dc->ndev->dev,
1543                                         "DSI failed to go to HS mode host driven\n");
1544                                         goto fail;
1545                                 }
1546                         }
1547                 }
1548                 if (!dsi->info.hs_cmd_mode_supported) {
1549                         err = tegra_dsi_set_to_lp_mode(dc, dsi);
1550                         if (err < 0) {
1551                                 dev_err(&dc->ndev->dev,
1552                                 "DSI failed to go to LP mode\n");
1553                                 goto fail;
1554                         }
1555                         switch_back_to_hs_mode = true;
1556                 }
1557         }
1558
1559         /* Set max return payload size in words */
1560         err = _tegra_dsi_write_data(dsi, NULL,
1561                 dsi_command_max_return_pkt_size,
1562                 max_ret_payload_size);
1563         if (err < 0) {
1564                 dev_err(&dc->ndev->dev,
1565                                 "DSI write failed\n");
1566                 goto fail;
1567         }
1568
1569         /* DCS to read given panel register */
1570         err = _tegra_dsi_write_data(dsi, NULL,
1571                 dsi_command_dcs_read_with_no_params,
1572                 panel_reg_addr);
1573         if (err < 0) {
1574                 dev_err(&dc->ndev->dev,
1575                                 "DSI write failed\n");
1576                 goto fail;
1577         }
1578
1579         err = tegra_dsi_bta(dsi);
1580         if (err < 0) {
1581                 dev_err(&dc->ndev->dev,
1582                         "DSI IMM BTA timeout\n");
1583                 goto fail;
1584         }
1585
1586         poll_time = 0;
1587         while (poll_time <  DSI_DELAY_FOR_READ_FIFO) {
1588                 mdelay(1);
1589                 val = tegra_dsi_readl(dsi, DSI_STATUS);
1590                 rd_fifo_cnt = val & DSI_STATUS_RD_FIFO_COUNT(0x1f);
1591                 if (rd_fifo_cnt << 2 > DSI_READ_FIFO_DEPTH)
1592                         dev_err(&dc->ndev->dev,
1593                         "DSI RD_FIFO_CNT is greater than RD_FIFO_DEPTH\n");
1594                         break;
1595                 poll_time++;
1596         }
1597
1598         if (rd_fifo_cnt == 0) {
1599                 dev_info(&dc->ndev->dev,
1600                         "DSI RD_FIFO_CNT is zero\n");
1601                 err = -EINVAL;
1602                 goto fail;
1603         }
1604
1605         if (val & DSI_STATUS_LB_UNDERFLOW(0x1) ||
1606                 val & DSI_STATUS_LB_OVERFLOW(0x1)) {
1607                 dev_err(&dc->ndev->dev,
1608                         "DSI overflow/underflow error\n");
1609                 err = -EINVAL;
1610                 goto fail;
1611         }
1612
1613         tegra_dsi_read_fifo(dc, dsi, rd_fifo_cnt, read_data);
1614
1615         err = tegra_dsi_parse_read_response(dc, rd_fifo_cnt, read_data);
1616 fail:
1617         if (switch_back_to_dc_mode)
1618                 dsi->driven_mode = TEGRA_DSI_DRIVEN_BY_DC;
1619         if (switch_back_to_dc_mode || switch_back_to_hs_mode)
1620                 tegra_dsi_set_to_hs_mode(dc, dsi);
1621         if (restart_dc_stream)
1622                 tegra_dsi_start_dc_stream(dc, dsi);
1623
1624         return err;
1625 }
1626 EXPORT_SYMBOL(tegra_dsi_read_data);
1627
1628 static int tegra_dsi_enter_ulpm(struct tegra_dc_dsi_data *dsi)
1629 {
1630         u32 val;
1631         int ret;
1632
1633         ret = 0;
1634
1635         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
1636         val &= ~DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(3);
1637         val |= DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(ENTER_ULPM);
1638         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
1639
1640 #if DSI_USE_SYNC_POINTS
1641         ret = tegra_dsi_syncpt(dsi);
1642         if (ret < 0) {
1643                 printk(KERN_ERR "DSI syncpt for ulpm enter failed\n");
1644                 goto fail;
1645         }
1646 #else
1647         /* TODO: Find exact delay required */
1648         mdelay(10);
1649 #endif
1650         dsi->ulpm = true;
1651 fail:
1652         return ret;
1653 }
1654
1655 static int tegra_dsi_exit_ulpm(struct tegra_dc_dsi_data *dsi)
1656 {
1657         u32 val;
1658         int ret;
1659
1660         ret = 0;
1661
1662         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
1663         val &= ~DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(3);
1664         val |= DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(EXIT_ULPM);
1665         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
1666
1667 #if DSI_USE_SYNC_POINTS
1668         ret = tegra_dsi_syncpt(dsi);
1669         if (ret < 0) {
1670                 printk(KERN_ERR "DSI syncpt for ulpm exit failed\n");
1671                 goto fail;
1672         }
1673 #else
1674         /* TODO: Find exact delay required */
1675         mdelay(10);
1676 #endif
1677         dsi->ulpm = false;
1678
1679         val = tegra_dsi_readl(dsi, DSI_HOST_DSI_CONTROL);
1680         val &= ~DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(0x3);
1681         val |= DSI_HOST_DSI_CONTROL_ULTRA_LOW_POWER(NORMAL);
1682         tegra_dsi_writel(dsi, val, DSI_HOST_DSI_CONTROL);
1683 fail:
1684         return ret;
1685
1686 }
1687
1688 static void tegra_dc_dsi_enable(struct tegra_dc *dc)
1689 {
1690         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
1691         int err;
1692         u32 val;
1693
1694         tegra_dc_io_start(dc);
1695         mutex_lock(&dsi->lock);
1696
1697         /* Stop DC stream before configuring DSI registers
1698          * to avoid visible glitches on panel during transition
1699          * from bootloader to kernel driver
1700          */
1701         tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi);
1702
1703         if (dsi->enabled) {
1704                 if (dsi->ulpm) {
1705                         if (tegra_dsi_exit_ulpm(dsi) < 0) {
1706                                 printk(KERN_ERR "DSI failed to exit ulpm\n");
1707                                 goto fail;
1708                         }
1709                 }
1710
1711                 if (dsi->info.panel_reset) {
1712                         err = tegra_dsi_send_panel_cmd(dc, dsi,
1713                                                         dsi->info.dsi_init_cmd,
1714                                                         dsi->info.n_init_cmd);
1715                         if (err < 0) {
1716                                 dev_err(&dc->ndev->dev,
1717                                 "dsi: error sending dsi init cmd\n");
1718                                 goto fail;
1719                         }
1720                 } else if (dsi->info.dsi_late_resume_cmd) {
1721                         err = tegra_dsi_send_panel_cmd(dc, dsi,
1722                                                 dsi->info.dsi_late_resume_cmd,
1723                                                 dsi->info.n_late_resume_cmd);
1724                         if (err < 0) {
1725                                 dev_err(&dc->ndev->dev,
1726                                 "dsi: error sending late resume cmd\n");
1727                                 goto fail;
1728                         }
1729                 }
1730         } else {
1731                 err = tegra_dsi_init_hw(dc, dsi);
1732                 if (err < 0) {
1733                         dev_err(&dc->ndev->dev,
1734                                 "dsi: not able to init dsi hardware\n");
1735                         goto fail;
1736                 }
1737
1738                 if (dsi->ulpm) {
1739                         if (tegra_dsi_enter_ulpm(dsi) < 0) {
1740                                 printk(KERN_ERR "DSI failed to enter ulpm\n");
1741                                 goto fail;
1742                         }
1743                         val = DSI_PAD_CONTROL_PAD_PDIO(0) |
1744                                 DSI_PAD_CONTROL_PAD_PDIO_CLK(0) |
1745                                 DSI_PAD_CONTROL_PAD_PULLDN_ENAB(TEGRA_DSI_DISABLE);
1746                         tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL);
1747                         if (tegra_dsi_exit_ulpm(dsi) < 0) {
1748                                 printk(KERN_ERR "DSI failed to exit ulpm\n");
1749                                 goto fail;
1750                         }
1751                 }
1752
1753                 err = tegra_dsi_set_to_lp_mode(dc, dsi);
1754                 if (err < 0) {
1755                         dev_err(&dc->ndev->dev,
1756                                 "dsi: not able to set to lp mode\n");
1757                         goto fail;
1758                 }
1759
1760                 err = tegra_dsi_send_panel_cmd(dc, dsi, dsi->info.dsi_init_cmd,
1761                                                 dsi->info.n_init_cmd);
1762                 if (err < 0) {
1763                         dev_err(&dc->ndev->dev,
1764                                 "dsi: error while sending dsi init cmd\n");
1765                         goto fail;
1766                 }
1767
1768                 err = tegra_dsi_set_to_hs_mode(dc, dsi);
1769                 if (err < 0) {
1770                         dev_err(&dc->ndev->dev,
1771                                 "dsi: not able to set to hs mode\n");
1772                         goto fail;
1773                 }
1774
1775                 dsi->enabled = true;
1776         }
1777
1778         if (dsi->status.driven == DSI_DRIVEN_MODE_DC)
1779                 tegra_dsi_start_dc_stream(dc, dsi);
1780 fail:
1781         mutex_unlock(&dsi->lock);
1782         tegra_dc_io_end(dc);
1783 }
1784
1785 static void _tegra_dc_dsi_init(struct tegra_dc *dc)
1786 {
1787         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
1788
1789         tegra_dsi_init_sw(dc, dsi);
1790         /* TODO: Configure the CSI pad configuration */
1791 }
1792
1793 static int tegra_dc_dsi_cp_p_cmd(struct tegra_dsi_cmd *src,
1794                                         struct tegra_dsi_cmd *dst, u16 n_cmd)
1795 {
1796         u16 i;
1797         u16 len;
1798
1799         memcpy(dst, src, sizeof(*dst) * n_cmd);
1800
1801         for (i = 0; i < n_cmd; i++)
1802                 if (src[i].pdata) {
1803                         len = sizeof(*src[i].pdata) *
1804                                         src[i].sp_len_dly.data_len;
1805                         dst[i].pdata = kzalloc(len, GFP_KERNEL);
1806                         if (!dst[i].pdata)
1807                                 goto free_cmd_pdata;
1808                         memcpy(dst[i].pdata, src[i].pdata, len);
1809                 }
1810
1811         return 0;
1812
1813 free_cmd_pdata:
1814         for (--i; i >= 0; i--)
1815                 if (dst[i].pdata)
1816                         kfree(dst[i].pdata);
1817         return -ENOMEM;
1818 }
1819
1820 static int tegra_dc_dsi_cp_info(struct tegra_dc_dsi_data *dsi,
1821                                                 struct tegra_dsi_out *p_dsi)
1822 {
1823         struct tegra_dsi_cmd *p_init_cmd;
1824         struct tegra_dsi_cmd *p_early_suspend_cmd;
1825         struct tegra_dsi_cmd *p_late_resume_cmd;
1826         struct tegra_dsi_cmd *p_suspend_cmd;
1827         int err;
1828
1829         if (p_dsi->n_data_lanes > MAX_DSI_DATA_LANES)
1830                 return -EINVAL;
1831
1832         p_init_cmd = kzalloc(sizeof(*p_init_cmd) *
1833                                 p_dsi->n_init_cmd, GFP_KERNEL);
1834         if (!p_init_cmd)
1835                 return -ENOMEM;
1836
1837         if (p_dsi->dsi_early_suspend_cmd) {
1838                 p_early_suspend_cmd = kzalloc(sizeof(*p_early_suspend_cmd) *
1839                                         p_dsi->n_early_suspend_cmd,
1840                                         GFP_KERNEL);
1841                 if (!p_early_suspend_cmd) {
1842                         err = -ENOMEM;
1843                         goto err_free_init_cmd;
1844                 }
1845         }
1846
1847         if (p_dsi->dsi_late_resume_cmd) {
1848                 p_late_resume_cmd = kzalloc(sizeof(*p_late_resume_cmd) *
1849                                         p_dsi->n_late_resume_cmd,
1850                                         GFP_KERNEL);
1851                 if (!p_late_resume_cmd) {
1852                         err = -ENOMEM;
1853                         goto err_free_p_early_suspend_cmd;
1854                 }
1855         }
1856
1857         p_suspend_cmd = kzalloc(sizeof(*p_suspend_cmd) * p_dsi->n_suspend_cmd,
1858                                 GFP_KERNEL);
1859         if (!p_suspend_cmd) {
1860                 err = -ENOMEM;
1861                 goto err_free_p_late_resume_cmd;
1862         }
1863
1864         memcpy(&dsi->info, p_dsi, sizeof(dsi->info));
1865
1866         /* Copy panel init cmd */
1867         err = tegra_dc_dsi_cp_p_cmd(p_dsi->dsi_init_cmd,
1868                                                 p_init_cmd, p_dsi->n_init_cmd);
1869         if (err < 0)
1870                 goto err_free;
1871         dsi->info.dsi_init_cmd = p_init_cmd;
1872
1873         /* Copy panel early suspend cmd */
1874         if (p_dsi->dsi_early_suspend_cmd) {
1875                 err = tegra_dc_dsi_cp_p_cmd(p_dsi->dsi_early_suspend_cmd,
1876                                         p_early_suspend_cmd,
1877                                         p_dsi->n_early_suspend_cmd);
1878                 if (err < 0)
1879                         goto err_free;
1880                 dsi->info.dsi_early_suspend_cmd = p_early_suspend_cmd;
1881         }
1882
1883         /* Copy panel late resume cmd */
1884         if (p_dsi->dsi_late_resume_cmd) {
1885                 err = tegra_dc_dsi_cp_p_cmd(p_dsi->dsi_late_resume_cmd,
1886                                                 p_late_resume_cmd,
1887                                                 p_dsi->n_late_resume_cmd);
1888                 if (err < 0)
1889                         goto err_free;
1890                 dsi->info.dsi_late_resume_cmd = p_late_resume_cmd;
1891         }
1892
1893         /* Copy panel suspend cmd */
1894         err = tegra_dc_dsi_cp_p_cmd(p_dsi->dsi_suspend_cmd, p_suspend_cmd,
1895                                         p_dsi->n_suspend_cmd);
1896         if (err < 0)
1897                 goto err_free;
1898         dsi->info.dsi_suspend_cmd = p_suspend_cmd;
1899
1900         if (!dsi->info.panel_reset_timeout_msec)
1901                 dsi->info.panel_reset_timeout_msec =
1902                                                 DEFAULT_PANEL_RESET_TIMEOUT;
1903
1904         if (!dsi->info.panel_buffer_size_byte)
1905                 dsi->info.panel_buffer_size_byte = DEFAULT_PANEL_BUFFER_BYTE;
1906
1907         if (!dsi->info.max_panel_freq_khz) {
1908                 dsi->info.max_panel_freq_khz = DEFAULT_MAX_DSI_PHY_CLK_KHZ;
1909
1910                 if (dsi->info.video_burst_mode >
1911                                 TEGRA_DSI_VIDEO_NONE_BURST_MODE_WITH_SYNC_END){
1912                         dev_err(&dsi->dc->ndev->dev, "DSI: max_panel_freq_khz"
1913                                         "is not set for DSI burst mode.\n");
1914                         dsi->info.video_burst_mode =
1915                                 TEGRA_DSI_VIDEO_BURST_MODE_LOWEST_SPEED;
1916                 }
1917         }
1918
1919         if (!dsi->info.lp_cmd_mode_freq_khz)
1920                 dsi->info.lp_cmd_mode_freq_khz = DEFAULT_LP_CMD_MODE_CLK_KHZ;
1921
1922         /* host mode is for testing only*/
1923         dsi->driven_mode = TEGRA_DSI_DRIVEN_BY_DC;
1924
1925         return 0;
1926
1927 err_free:
1928         kfree(p_suspend_cmd);
1929 err_free_p_late_resume_cmd:
1930         kfree(p_late_resume_cmd);
1931 err_free_p_early_suspend_cmd:
1932         kfree(p_early_suspend_cmd);
1933 err_free_init_cmd:
1934         kfree(p_init_cmd);
1935         return err;
1936 }
1937
1938 static int tegra_dc_dsi_init(struct tegra_dc *dc)
1939 {
1940         struct tegra_dc_dsi_data *dsi;
1941         struct resource *res;
1942         struct resource *base_res;
1943         void __iomem *base;
1944         struct clk *dc_clk = NULL;
1945         struct clk *dsi_clk = NULL;
1946         struct tegra_dsi_out *dsi_pdata;
1947         int err;
1948
1949         err = 0;
1950
1951         dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
1952         if (!dsi)
1953                 return -ENOMEM;
1954
1955         res = nvhost_get_resource_byname(dc->ndev, IORESOURCE_MEM,
1956                                         "dsi_regs");
1957         if (!res) {
1958                 dev_err(&dc->ndev->dev, "dsi: no mem resource\n");
1959                 err = -ENOENT;
1960                 goto err_free_dsi;
1961         }
1962
1963         base_res = request_mem_region(res->start, resource_size(res),
1964                                 dc->ndev->name);
1965         if (!base_res) {
1966                 dev_err(&dc->ndev->dev, "dsi: request_mem_region failed\n");
1967                 err = -EBUSY;
1968                 goto err_free_dsi;
1969         }
1970
1971         base = ioremap(res->start, resource_size(res));
1972         if (!base) {
1973                 dev_err(&dc->ndev->dev, "dsi: registers can't be mapped\n");
1974                 err = -EBUSY;
1975                 goto err_release_regs;
1976         }
1977
1978         dsi_pdata = dc->pdata->default_out->dsi;
1979         if (!dsi_pdata) {
1980                 dev_err(&dc->ndev->dev, "dsi: dsi data not available\n");
1981                 goto err_release_regs;
1982         }
1983
1984         if (dsi_pdata->dsi_instance)
1985                 dsi_clk = clk_get(&dc->ndev->dev, "dsib");
1986         else
1987                 dsi_clk = clk_get(&dc->ndev->dev, "dsia");
1988
1989         if (IS_ERR_OR_NULL(dsi_clk)) {
1990                 dev_err(&dc->ndev->dev, "dsi: can't get clock\n");
1991                 err = -EBUSY;
1992                 goto err_release_regs;
1993         }
1994
1995         dc_clk = clk_get_sys(dev_name(&dc->ndev->dev), NULL);
1996         if (IS_ERR_OR_NULL(dc_clk)) {
1997                 dev_err(&dc->ndev->dev, "dsi: dc clock %s unavailable\n",
1998                         dev_name(&dc->ndev->dev));
1999                 err = -EBUSY;
2000                 goto err_clk_put;
2001         }
2002
2003         mutex_init(&dsi->lock);
2004         dsi->dc = dc;
2005         dsi->base = base;
2006         dsi->base_res = base_res;
2007         dsi->dc_clk = dc_clk;
2008         dsi->dsi_clk = dsi_clk;
2009
2010         err = tegra_dc_dsi_cp_info(dsi, dsi_pdata);
2011         if (err < 0)
2012                 goto err_dsi_data;
2013
2014         tegra_dc_set_outdata(dc, dsi);
2015         _tegra_dc_dsi_init(dc);
2016
2017         return 0;
2018
2019 err_dsi_data:
2020 err_clk_put:
2021         clk_put(dsi_clk);
2022 err_release_regs:
2023         release_resource(base_res);
2024 err_free_dsi:
2025         kfree(dsi);
2026
2027         return err;
2028 }
2029
2030 static void tegra_dc_dsi_destroy(struct tegra_dc *dc)
2031 {
2032         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
2033         u16 i;
2034         u32 val;
2035
2036         mutex_lock(&dsi->lock);
2037
2038         /* free up the pdata*/
2039         for (i = 0; i < dsi->info.n_init_cmd; i++) {
2040                 if (dsi->info.dsi_init_cmd[i].pdata)
2041                         kfree(dsi->info.dsi_init_cmd[i].pdata);
2042         }
2043         kfree(dsi->info.dsi_init_cmd);
2044
2045         /* Disable dc stream*/
2046         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
2047                 tegra_dsi_stop_dc_stream(dc, dsi);
2048
2049         /* Disable dsi phy clock*/
2050         if (dsi->status.clk_out == DSI_PHYCLK_OUT_EN)
2051                 tegra_dsi_hs_clk_out_disable(dc, dsi);
2052
2053         val = DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_DISABLE);
2054         tegra_dsi_writel(dsi, val, DSI_POWER_CONTROL);
2055
2056         iounmap(dsi->base);
2057         release_resource(dsi->base_res);
2058
2059         clk_put(dsi->dc_clk);
2060         clk_put(dsi->dsi_clk);
2061
2062         mutex_unlock(&dsi->lock);
2063
2064         mutex_destroy(&dsi->lock);
2065         kfree(dsi);
2066 }
2067
2068 static void tegra_dc_dsi_disable(struct tegra_dc *dc)
2069 {
2070         int err;
2071         u32 val;
2072         struct clk *base_clk;
2073         struct tegra_dc_dsi_data *dsi = tegra_dc_get_outdata(dc);
2074
2075         tegra_dc_io_start(dc);
2076         mutex_lock(&dsi->lock);
2077
2078         if (dsi->status.dc_stream == DSI_DC_STREAM_ENABLE)
2079                 tegra_dsi_stop_dc_stream_at_frame_end(dc, dsi);
2080
2081         if (dsi->info.power_saving_suspend) {
2082                 if (!dsi->enabled)
2083                         goto fail;
2084
2085                 err = tegra_dsi_set_to_lp_mode(dc, dsi);
2086                 if (err < 0) {
2087                         dev_err(&dc->ndev->dev,
2088                         "DSI failed to go to LP mode\n");
2089                         goto fail;
2090                 }
2091
2092                 err = tegra_dsi_send_panel_cmd(dc, dsi,
2093                                 dsi->info.dsi_suspend_cmd,
2094                                 dsi->info.n_suspend_cmd);
2095                 if (err < 0) {
2096                         dev_err(&dc->ndev->dev,
2097                                 "dsi: Error sending suspend cmd\n");
2098                         goto fail;
2099                 }
2100
2101                 if (!dsi->ulpm) {
2102                         if (tegra_dsi_enter_ulpm(dsi) < 0)
2103                                 printk(KERN_ERR "DSI failed to enter ulpm\n");
2104                 }
2105
2106                 /* Suspend pad */
2107                 val = tegra_dsi_readl(dsi, DSI_PAD_CONTROL);
2108                 val = DSI_PAD_CONTROL_PAD_PDIO(0x3) |
2109                         DSI_PAD_CONTROL_PAD_PDIO_CLK(0x1) |
2110                         DSI_PAD_CONTROL_PAD_PULLDN_ENAB(TEGRA_DSI_ENABLE);
2111                 tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL);
2112
2113                 /* Suspend core-logic */
2114                 val = DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_DISABLE);
2115                 tegra_dsi_writel(dsi, val, DSI_POWER_CONTROL);
2116
2117                 /* Disable phy clock */
2118                 base_clk = clk_get_parent(dsi->dsi_clk);
2119                 if (dsi->info.dsi_instance)
2120                         tegra_clk_cfg_ex(base_clk,
2121                                         TEGRA_CLK_PLLD_CSI_OUT_ENB,
2122                                         0);
2123                 else
2124                         tegra_clk_cfg_ex(base_clk,
2125                                         TEGRA_CLK_PLLD_DSI_OUT_ENB,
2126                                         0);
2127
2128                 /* Disable DSI source clock */
2129                 clk_disable(dsi->dsi_clk);
2130                 dsi->clk_ref = false;
2131                 dsi->enabled = false;
2132         } else {
2133                 if (dsi->info.dsi_early_suspend_cmd) {
2134                         err = tegra_dsi_send_panel_cmd(dc, dsi,
2135                                 dsi->info.dsi_early_suspend_cmd,
2136                                 dsi->info.n_early_suspend_cmd);
2137                         if (err < 0) {
2138                                 dev_err(&dc->ndev->dev,
2139                                 "dsi: Error sending early suspend cmd\n");
2140                                 goto fail;
2141                         }
2142                 }
2143
2144                 if (!dsi->ulpm) {
2145                         if (tegra_dsi_enter_ulpm(dsi) < 0)
2146                                 printk(KERN_ERR "DSI failed to enter ulpm\n");
2147                 }
2148         }
2149
2150 fail:
2151         mutex_unlock(&dsi->lock);
2152         tegra_dc_io_end(dc);
2153 }
2154
2155 #ifdef CONFIG_PM
2156 static void tegra_dc_dsi_suspend(struct tegra_dc *dc)
2157 {
2158         struct tegra_dc_dsi_data *dsi;
2159         int err;
2160         u32 val;
2161
2162         dsi = tegra_dc_get_outdata(dc);
2163
2164         tegra_dc_io_start(dc);
2165         mutex_lock(&dsi->lock);
2166
2167         if (!dsi->enabled)
2168                 goto fail;
2169
2170         if (!dsi->info.power_saving_suspend) {
2171                 if (dsi->ulpm) {
2172                         if (tegra_dsi_exit_ulpm(dsi) < 0) {
2173                                 printk(KERN_ERR "DSI failed to exit ulpm");
2174                                 goto fail;
2175                         }
2176                 }
2177
2178                 /* Suspend Panel */
2179                 err = tegra_dsi_send_panel_cmd(dc, dsi,
2180                                 dsi->info.dsi_suspend_cmd,
2181                                 dsi->info.n_suspend_cmd);
2182                 if (err < 0) {
2183                         dev_err(&dc->ndev->dev,
2184                                 "dsi: Error sending suspend cmd\n");
2185                         goto fail;
2186                 }
2187                 if (!dsi->ulpm) {
2188                         if (tegra_dsi_enter_ulpm(dsi) < 0) {
2189                                 printk(KERN_ERR "DSI failed to enter ulpm\n");
2190                                 goto fail;
2191                         }
2192                 }
2193
2194                 /* Suspend pad */
2195                 val = tegra_dsi_readl(dsi, DSI_PAD_CONTROL);
2196                 val = DSI_PAD_CONTROL_PAD_PDIO(0x3) |
2197                         DSI_PAD_CONTROL_PAD_PDIO_CLK(0x1) |
2198                         DSI_PAD_CONTROL_PAD_PULLDN_ENAB(TEGRA_DSI_ENABLE);
2199                 tegra_dsi_writel(dsi, val, DSI_PAD_CONTROL);
2200
2201                 /* Suspend core-logic */
2202                 val = DSI_POWER_CONTROL_LEG_DSI_ENABLE(TEGRA_DSI_DISABLE);
2203                 tegra_dsi_writel(dsi, val, DSI_POWER_CONTROL);
2204
2205                 dsi->enabled = false;
2206
2207                 clk_disable(dsi->dsi_clk);
2208                 dsi->clk_ref = false;
2209         }
2210 fail:
2211         mutex_unlock(&dsi->lock);
2212         tegra_dc_io_end(dc);
2213 }
2214
2215 static void tegra_dc_dsi_resume(struct tegra_dc *dc)
2216 {
2217         /* Not required since tegra_dc_dsi_enable
2218          * will reconfigure the controller from scratch
2219          */
2220 }
2221 #endif
2222
2223 struct tegra_dc_out_ops tegra_dc_dsi_ops = {
2224         .init = tegra_dc_dsi_init,
2225         .destroy = tegra_dc_dsi_destroy,
2226         .enable = tegra_dc_dsi_enable,
2227         .disable = tegra_dc_dsi_disable,
2228 #ifdef CONFIG_PM
2229         .suspend = tegra_dc_dsi_suspend,
2230         .resume = tegra_dc_dsi_resume,
2231 #endif
2232 };