video: tegra: dc: support for vblank sync
[linux-2.6.git] / drivers / video / tegra / dc / dc.c
1 /*
2  * drivers/video/tegra/dc/dc.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  * Author: Erik Gilling <konkers@android.com>
6  *
7  * Copyright (c) 2010-2012, NVIDIA CORPORATION, All rights reserved.
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/err.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
26 #include <linux/io.h>
27 #include <linux/clk.h>
28 #include <linux/mutex.h>
29 #include <linux/delay.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/workqueue.h>
32 #include <linux/ktime.h>
33 #include <linux/debugfs.h>
34 #include <linux/seq_file.h>
35 #include <linux/backlight.h>
36 #include <linux/gpio.h>
37 #include <linux/nvhost.h>
38 #include <video/tegrafb.h>
39 #include <drm/drm_fixed.h>
40 #ifdef CONFIG_SWITCH
41 #include <linux/switch.h>
42 #endif
43
44 #include <mach/clk.h>
45 #include <mach/dc.h>
46 #include <mach/fb.h>
47 #include <mach/mc.h>
48 #include <linux/nvhost.h>
49 #include <mach/latency_allowance.h>
50
51 #include "dc_reg.h"
52 #include "dc_config.h"
53 #include "dc_priv.h"
54 #include "nvsd.h"
55
56 #define TEGRA_CRC_LATCHED_DELAY         34
57
58 #define DC_COM_PIN_OUTPUT_POLARITY1_INIT_VAL    0x01000000
59 #define DC_COM_PIN_OUTPUT_POLARITY3_INIT_VAL    0x0
60
61 static struct fb_videomode tegra_dc_hdmi_fallback_mode = {
62         .refresh = 60,
63         .xres = 640,
64         .yres = 480,
65         .pixclock = KHZ2PICOS(25200),
66         .hsync_len = 96,        /* h_sync_width */
67         .vsync_len = 2,         /* v_sync_width */
68         .left_margin = 48,      /* h_back_porch */
69         .upper_margin = 33,     /* v_back_porch */
70         .right_margin = 16,     /* h_front_porch */
71         .lower_margin = 10,     /* v_front_porch */
72         .vmode = 0,
73         .sync = 0,
74 };
75
76 static struct tegra_dc_mode override_disp_mode[3];
77
78 static void _tegra_dc_controller_disable(struct tegra_dc *dc);
79
80 struct tegra_dc *tegra_dcs[TEGRA_MAX_DC];
81
82 DEFINE_MUTEX(tegra_dc_lock);
83 DEFINE_MUTEX(shared_lock);
84
85 void tegra_dc_clk_enable(struct tegra_dc *dc)
86 {
87         if (!tegra_is_clk_enabled(dc->clk)) {
88                 clk_enable(dc->clk);
89                 tegra_dvfs_set_rate(dc->clk, dc->mode.pclk);
90         }
91 }
92
93 void tegra_dc_clk_disable(struct tegra_dc *dc)
94 {
95         if (tegra_is_clk_enabled(dc->clk)) {
96                 clk_disable(dc->clk);
97                 tegra_dvfs_set_rate(dc->clk, 0);
98         }
99 }
100
101 void tegra_dc_hold_dc_out(struct tegra_dc *dc)
102 {
103         if (dc->out_ops->hold)
104                 dc->out_ops->hold(dc);
105 }
106
107 void tegra_dc_release_dc_out(struct tegra_dc *dc)
108 {
109         if (dc->out_ops->release)
110                 dc->out_ops->release(dc);
111 }
112
113 #define DUMP_REG(a) do {                        \
114         snprintf(buff, sizeof(buff), "%-32s\t%03x\t%08lx\n", \
115                  #a, a, tegra_dc_readl(dc, a));               \
116         print(data, buff);                                    \
117         } while (0)
118
119 #define print_underflow_info(dc) do {                 \
120         trace_printk("%s:Underflow stats: underflows : %llu, "      \
121                         "undeflows_a : %llu, "                          \
122                         "underflows_b : %llu, "                         \
123                         "underflows_c : %llu\n",                        \
124                         dc->ndev->name,                                 \
125                         dc->stats.underflows,                           \
126                         dc->stats.underflows_a, dc->stats.underflows_b, \
127                         dc->stats.underflows_c);                        \
128         } while (0)
129
130 static void _dump_regs(struct tegra_dc *dc, void *data,
131                        void (* print)(void *data, const char *str))
132 {
133         int i;
134         char buff[256];
135
136         mutex_lock(&dc->lock);
137         tegra_dc_hold_dc_out(dc);
138         tegra_dc_io_start(dc);
139
140         DUMP_REG(DC_CMD_DISPLAY_COMMAND_OPTION0);
141         DUMP_REG(DC_CMD_DISPLAY_COMMAND);
142         DUMP_REG(DC_CMD_SIGNAL_RAISE);
143         DUMP_REG(DC_CMD_INT_STATUS);
144         DUMP_REG(DC_CMD_INT_MASK);
145         DUMP_REG(DC_CMD_INT_ENABLE);
146         DUMP_REG(DC_CMD_INT_TYPE);
147         DUMP_REG(DC_CMD_INT_POLARITY);
148         DUMP_REG(DC_CMD_SIGNAL_RAISE1);
149         DUMP_REG(DC_CMD_SIGNAL_RAISE2);
150         DUMP_REG(DC_CMD_SIGNAL_RAISE3);
151         DUMP_REG(DC_CMD_STATE_ACCESS);
152         DUMP_REG(DC_CMD_STATE_CONTROL);
153         DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
154         DUMP_REG(DC_CMD_REG_ACT_CONTROL);
155
156         DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
157         DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS1);
158         DUMP_REG(DC_DISP_DISP_WIN_OPTIONS);
159         DUMP_REG(DC_DISP_MEM_HIGH_PRIORITY);
160         DUMP_REG(DC_DISP_MEM_HIGH_PRIORITY_TIMER);
161         DUMP_REG(DC_DISP_DISP_TIMING_OPTIONS);
162         DUMP_REG(DC_DISP_REF_TO_SYNC);
163         DUMP_REG(DC_DISP_SYNC_WIDTH);
164         DUMP_REG(DC_DISP_BACK_PORCH);
165         DUMP_REG(DC_DISP_DISP_ACTIVE);
166         DUMP_REG(DC_DISP_FRONT_PORCH);
167         DUMP_REG(DC_DISP_H_PULSE0_CONTROL);
168         DUMP_REG(DC_DISP_H_PULSE0_POSITION_A);
169         DUMP_REG(DC_DISP_H_PULSE0_POSITION_B);
170         DUMP_REG(DC_DISP_H_PULSE0_POSITION_C);
171         DUMP_REG(DC_DISP_H_PULSE0_POSITION_D);
172         DUMP_REG(DC_DISP_H_PULSE1_CONTROL);
173         DUMP_REG(DC_DISP_H_PULSE1_POSITION_A);
174         DUMP_REG(DC_DISP_H_PULSE1_POSITION_B);
175         DUMP_REG(DC_DISP_H_PULSE1_POSITION_C);
176         DUMP_REG(DC_DISP_H_PULSE1_POSITION_D);
177         DUMP_REG(DC_DISP_H_PULSE2_CONTROL);
178         DUMP_REG(DC_DISP_H_PULSE2_POSITION_A);
179         DUMP_REG(DC_DISP_H_PULSE2_POSITION_B);
180         DUMP_REG(DC_DISP_H_PULSE2_POSITION_C);
181         DUMP_REG(DC_DISP_H_PULSE2_POSITION_D);
182         DUMP_REG(DC_DISP_V_PULSE0_CONTROL);
183         DUMP_REG(DC_DISP_V_PULSE0_POSITION_A);
184         DUMP_REG(DC_DISP_V_PULSE0_POSITION_B);
185         DUMP_REG(DC_DISP_V_PULSE0_POSITION_C);
186         DUMP_REG(DC_DISP_V_PULSE1_CONTROL);
187         DUMP_REG(DC_DISP_V_PULSE1_POSITION_A);
188         DUMP_REG(DC_DISP_V_PULSE1_POSITION_B);
189         DUMP_REG(DC_DISP_V_PULSE1_POSITION_C);
190         DUMP_REG(DC_DISP_V_PULSE2_CONTROL);
191         DUMP_REG(DC_DISP_V_PULSE2_POSITION_A);
192         DUMP_REG(DC_DISP_V_PULSE3_CONTROL);
193         DUMP_REG(DC_DISP_V_PULSE3_POSITION_A);
194         DUMP_REG(DC_DISP_M0_CONTROL);
195         DUMP_REG(DC_DISP_M1_CONTROL);
196         DUMP_REG(DC_DISP_DI_CONTROL);
197         DUMP_REG(DC_DISP_PP_CONTROL);
198         DUMP_REG(DC_DISP_PP_SELECT_A);
199         DUMP_REG(DC_DISP_PP_SELECT_B);
200         DUMP_REG(DC_DISP_PP_SELECT_C);
201         DUMP_REG(DC_DISP_PP_SELECT_D);
202         DUMP_REG(DC_DISP_DISP_CLOCK_CONTROL);
203         DUMP_REG(DC_DISP_DISP_INTERFACE_CONTROL);
204         DUMP_REG(DC_DISP_DISP_COLOR_CONTROL);
205         DUMP_REG(DC_DISP_SHIFT_CLOCK_OPTIONS);
206         DUMP_REG(DC_DISP_DATA_ENABLE_OPTIONS);
207         DUMP_REG(DC_DISP_SERIAL_INTERFACE_OPTIONS);
208         DUMP_REG(DC_DISP_LCD_SPI_OPTIONS);
209         DUMP_REG(DC_DISP_BORDER_COLOR);
210         DUMP_REG(DC_DISP_COLOR_KEY0_LOWER);
211         DUMP_REG(DC_DISP_COLOR_KEY0_UPPER);
212         DUMP_REG(DC_DISP_COLOR_KEY1_LOWER);
213         DUMP_REG(DC_DISP_COLOR_KEY1_UPPER);
214         DUMP_REG(DC_DISP_CURSOR_FOREGROUND);
215         DUMP_REG(DC_DISP_CURSOR_BACKGROUND);
216         DUMP_REG(DC_DISP_CURSOR_START_ADDR);
217         DUMP_REG(DC_DISP_CURSOR_START_ADDR_NS);
218         DUMP_REG(DC_DISP_CURSOR_POSITION);
219         DUMP_REG(DC_DISP_CURSOR_POSITION_NS);
220         DUMP_REG(DC_DISP_INIT_SEQ_CONTROL);
221         DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_A);
222         DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_B);
223         DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_C);
224         DUMP_REG(DC_DISP_SPI_INIT_SEQ_DATA_D);
225         DUMP_REG(DC_DISP_DC_MCCIF_FIFOCTRL);
226         DUMP_REG(DC_DISP_MCCIF_DISPLAY0A_HYST);
227         DUMP_REG(DC_DISP_MCCIF_DISPLAY0B_HYST);
228         DUMP_REG(DC_DISP_MCCIF_DISPLAY0C_HYST);
229         DUMP_REG(DC_DISP_MCCIF_DISPLAY1B_HYST);
230         DUMP_REG(DC_DISP_DAC_CRT_CTRL);
231         DUMP_REG(DC_DISP_DISP_MISC_CONTROL);
232
233
234         for (i = 0; i < 3; i++) {
235                 print(data, "\n");
236                 snprintf(buff, sizeof(buff), "WINDOW %c:\n", 'A' + i);
237                 print(data, buff);
238
239                 tegra_dc_writel(dc, WINDOW_A_SELECT << i,
240                                 DC_CMD_DISPLAY_WINDOW_HEADER);
241                 DUMP_REG(DC_CMD_DISPLAY_WINDOW_HEADER);
242                 DUMP_REG(DC_WIN_WIN_OPTIONS);
243                 DUMP_REG(DC_WIN_BYTE_SWAP);
244                 DUMP_REG(DC_WIN_BUFFER_CONTROL);
245                 DUMP_REG(DC_WIN_COLOR_DEPTH);
246                 DUMP_REG(DC_WIN_POSITION);
247                 DUMP_REG(DC_WIN_SIZE);
248                 DUMP_REG(DC_WIN_PRESCALED_SIZE);
249                 DUMP_REG(DC_WIN_H_INITIAL_DDA);
250                 DUMP_REG(DC_WIN_V_INITIAL_DDA);
251                 DUMP_REG(DC_WIN_DDA_INCREMENT);
252                 DUMP_REG(DC_WIN_LINE_STRIDE);
253                 DUMP_REG(DC_WIN_BUF_STRIDE);
254                 DUMP_REG(DC_WIN_UV_BUF_STRIDE);
255                 DUMP_REG(DC_WIN_BLEND_NOKEY);
256                 DUMP_REG(DC_WIN_BLEND_1WIN);
257                 DUMP_REG(DC_WIN_BLEND_2WIN_X);
258                 DUMP_REG(DC_WIN_BLEND_2WIN_Y);
259                 DUMP_REG(DC_WIN_BLEND_3WIN_XY);
260                 DUMP_REG(DC_WINBUF_START_ADDR);
261                 DUMP_REG(DC_WINBUF_START_ADDR_U);
262                 DUMP_REG(DC_WINBUF_START_ADDR_V);
263                 DUMP_REG(DC_WINBUF_ADDR_H_OFFSET);
264                 DUMP_REG(DC_WINBUF_ADDR_V_OFFSET);
265                 DUMP_REG(DC_WINBUF_UFLOW_STATUS);
266                 DUMP_REG(DC_WIN_CSC_YOF);
267                 DUMP_REG(DC_WIN_CSC_KYRGB);
268                 DUMP_REG(DC_WIN_CSC_KUR);
269                 DUMP_REG(DC_WIN_CSC_KVR);
270                 DUMP_REG(DC_WIN_CSC_KUG);
271                 DUMP_REG(DC_WIN_CSC_KVG);
272                 DUMP_REG(DC_WIN_CSC_KUB);
273                 DUMP_REG(DC_WIN_CSC_KVB);
274         }
275
276         DUMP_REG(DC_CMD_DISPLAY_POWER_CONTROL);
277         DUMP_REG(DC_COM_PIN_OUTPUT_ENABLE2);
278         DUMP_REG(DC_COM_PIN_OUTPUT_POLARITY2);
279         DUMP_REG(DC_COM_PIN_OUTPUT_DATA2);
280         DUMP_REG(DC_COM_PIN_INPUT_ENABLE2);
281         DUMP_REG(DC_COM_PIN_OUTPUT_SELECT5);
282         DUMP_REG(DC_DISP_DISP_SIGNAL_OPTIONS0);
283         DUMP_REG(DC_DISP_M1_CONTROL);
284         DUMP_REG(DC_COM_PM1_CONTROL);
285         DUMP_REG(DC_COM_PM1_DUTY_CYCLE);
286         DUMP_REG(DC_DISP_SD_CONTROL);
287
288         tegra_dc_io_end(dc);
289         tegra_dc_release_dc_out(dc);
290         mutex_unlock(&dc->lock);
291 }
292
293 #undef DUMP_REG
294
295 #ifdef DEBUG
296 static void dump_regs_print(void *data, const char *str)
297 {
298         struct tegra_dc *dc = data;
299         dev_dbg(&dc->ndev->dev, "%s", str);
300 }
301
302 static void dump_regs(struct tegra_dc *dc)
303 {
304         _dump_regs(dc, dc, dump_regs_print);
305 }
306 #else /* !DEBUG */
307
308 static void dump_regs(struct tegra_dc *dc) {}
309
310 #endif /* DEBUG */
311
312 #ifdef CONFIG_DEBUG_FS
313
314 static void dbg_regs_print(void *data, const char *str)
315 {
316         struct seq_file *s = data;
317
318         seq_printf(s, "%s", str);
319 }
320
321 #undef DUMP_REG
322
323 static int dbg_dc_show(struct seq_file *s, void *unused)
324 {
325         struct tegra_dc *dc = s->private;
326
327         _dump_regs(dc, s, dbg_regs_print);
328
329         return 0;
330 }
331
332
333 static int dbg_dc_open(struct inode *inode, struct file *file)
334 {
335         return single_open(file, dbg_dc_show, inode->i_private);
336 }
337
338 static const struct file_operations regs_fops = {
339         .open           = dbg_dc_open,
340         .read           = seq_read,
341         .llseek         = seq_lseek,
342         .release        = single_release,
343 };
344
345 static int dbg_dc_mode_show(struct seq_file *s, void *unused)
346 {
347         struct tegra_dc *dc = s->private;
348         struct tegra_dc_mode *m;
349
350         mutex_lock(&dc->lock);
351         m = &dc->mode;
352         seq_printf(s,
353                 "pclk: %d\n"
354                 "h_ref_to_sync: %d\n"
355                 "v_ref_to_sync: %d\n"
356                 "h_sync_width: %d\n"
357                 "v_sync_width: %d\n"
358                 "h_back_porch: %d\n"
359                 "v_back_porch: %d\n"
360                 "h_active: %d\n"
361                 "v_active: %d\n"
362                 "h_front_porch: %d\n"
363                 "v_front_porch: %d\n"
364                 "stereo_mode: %d\n",
365                 m->pclk, m->h_ref_to_sync, m->v_ref_to_sync,
366                 m->h_sync_width, m->v_sync_width,
367                 m->h_back_porch, m->v_back_porch,
368                 m->h_active, m->v_active,
369                 m->h_front_porch, m->v_front_porch,
370                 m->stereo_mode);
371         mutex_unlock(&dc->lock);
372         return 0;
373 }
374
375 static int dbg_dc_mode_open(struct inode *inode, struct file *file)
376 {
377         return single_open(file, dbg_dc_mode_show, inode->i_private);
378 }
379
380 static const struct file_operations mode_fops = {
381         .open           = dbg_dc_mode_open,
382         .read           = seq_read,
383         .llseek         = seq_lseek,
384         .release        = single_release,
385 };
386
387 static int dbg_dc_stats_show(struct seq_file *s, void *unused)
388 {
389         struct tegra_dc *dc = s->private;
390
391         mutex_lock(&dc->lock);
392         seq_printf(s,
393                 "underflows: %llu\n"
394                 "underflows_a: %llu\n"
395                 "underflows_b: %llu\n"
396                 "underflows_c: %llu\n",
397                 dc->stats.underflows,
398                 dc->stats.underflows_a,
399                 dc->stats.underflows_b,
400                 dc->stats.underflows_c);
401         mutex_unlock(&dc->lock);
402
403         return 0;
404 }
405
406 static int dbg_dc_stats_open(struct inode *inode, struct file *file)
407 {
408         return single_open(file, dbg_dc_stats_show, inode->i_private);
409 }
410
411 static const struct file_operations stats_fops = {
412         .open           = dbg_dc_stats_open,
413         .read           = seq_read,
414         .llseek         = seq_lseek,
415         .release        = single_release,
416 };
417
418 static void __devexit tegra_dc_remove_debugfs(struct tegra_dc *dc)
419 {
420         if (dc->debugdir)
421                 debugfs_remove_recursive(dc->debugdir);
422         dc->debugdir = NULL;
423 }
424
425 static void tegra_dc_create_debugfs(struct tegra_dc *dc)
426 {
427         struct dentry *retval;
428
429         dc->debugdir = debugfs_create_dir(dev_name(&dc->ndev->dev), NULL);
430         if (!dc->debugdir)
431                 goto remove_out;
432
433         retval = debugfs_create_file("regs", S_IRUGO, dc->debugdir, dc,
434                 &regs_fops);
435         if (!retval)
436                 goto remove_out;
437
438         retval = debugfs_create_file("mode", S_IRUGO, dc->debugdir, dc,
439                 &mode_fops);
440         if (!retval)
441                 goto remove_out;
442
443         retval = debugfs_create_file("stats", S_IRUGO, dc->debugdir, dc,
444                 &stats_fops);
445         if (!retval)
446                 goto remove_out;
447
448         return;
449 remove_out:
450         dev_err(&dc->ndev->dev, "could not create debugfs\n");
451         tegra_dc_remove_debugfs(dc);
452 }
453
454 #else /* !CONFIG_DEBUGFS */
455 static inline void tegra_dc_create_debugfs(struct tegra_dc *dc) { };
456 static inline void __devexit tegra_dc_remove_debugfs(struct tegra_dc *dc) { };
457 #endif /* CONFIG_DEBUGFS */
458
459 static int tegra_dc_set(struct tegra_dc *dc, int index)
460 {
461         int ret = 0;
462
463         mutex_lock(&tegra_dc_lock);
464         if (index >= TEGRA_MAX_DC) {
465                 ret = -EINVAL;
466                 goto out;
467         }
468
469         if (dc != NULL && tegra_dcs[index] != NULL) {
470                 ret = -EBUSY;
471                 goto out;
472         }
473
474         tegra_dcs[index] = dc;
475
476 out:
477         mutex_unlock(&tegra_dc_lock);
478
479         return ret;
480 }
481
482 unsigned int tegra_dc_has_multiple_dc(void)
483 {
484         unsigned int idx;
485         unsigned int cnt = 0;
486         struct tegra_dc *dc;
487
488         mutex_lock(&tegra_dc_lock);
489         for (idx = 0; idx < TEGRA_MAX_DC; idx++)
490                 cnt += ((dc = tegra_dcs[idx]) != NULL && dc->enabled) ? 1 : 0;
491         mutex_unlock(&tegra_dc_lock);
492
493         return (cnt > 1);
494 }
495
496 /* get the stride size of a window.
497  * return: stride size in bytes for window win. or 0 if unavailble. */
498 int tegra_dc_get_stride(struct tegra_dc *dc, unsigned win)
499 {
500         u32 stride;
501
502         if (!dc->enabled)
503                 return 0;
504         BUG_ON(win > DC_N_WINDOWS);
505         mutex_lock(&dc->lock);
506         tegra_dc_hold_dc_out(dc);
507         tegra_dc_writel(dc, WINDOW_A_SELECT << win,
508                 DC_CMD_DISPLAY_WINDOW_HEADER);
509         stride = tegra_dc_readl(dc, DC_WIN_LINE_STRIDE);
510         tegra_dc_release_dc_out(dc);
511         mutex_unlock(&dc->lock);
512         return GET_LINE_STRIDE(stride);
513 }
514 EXPORT_SYMBOL(tegra_dc_get_stride);
515
516 struct tegra_dc *tegra_dc_get_dc(unsigned idx)
517 {
518         if (idx < TEGRA_MAX_DC)
519                 return tegra_dcs[idx];
520         else
521                 return NULL;
522 }
523 EXPORT_SYMBOL(tegra_dc_get_dc);
524
525 struct tegra_dc_win *tegra_dc_get_window(struct tegra_dc *dc, unsigned win)
526 {
527         if (win >= dc->n_windows)
528                 return NULL;
529
530         return &dc->windows[win];
531 }
532 EXPORT_SYMBOL(tegra_dc_get_window);
533
534 bool tegra_dc_get_connected(struct tegra_dc *dc)
535 {
536         return dc->connected;
537 }
538 EXPORT_SYMBOL(tegra_dc_get_connected);
539
540 bool tegra_dc_hpd(struct tegra_dc *dc)
541 {
542         int sense;
543         int level;
544
545         level = gpio_get_value(dc->out->hotplug_gpio);
546
547         sense = dc->out->flags & TEGRA_DC_OUT_HOTPLUG_MASK;
548
549         return (sense == TEGRA_DC_OUT_HOTPLUG_HIGH && level) ||
550                 (sense == TEGRA_DC_OUT_HOTPLUG_LOW && !level);
551 }
552 EXPORT_SYMBOL(tegra_dc_hpd);
553
554 static void tegra_dc_set_scaling_filter(struct tegra_dc *dc)
555 {
556         unsigned i;
557         unsigned v0 = 128;
558         unsigned v1 = 0;
559         /* linear horizontal and vertical filters */
560         for (i = 0; i < 16; i++) {
561                 tegra_dc_writel(dc, (v1 << 16) | (v0 << 8),
562                                 DC_WIN_H_FILTER_P(i));
563
564                 tegra_dc_writel(dc, v0,
565                                 DC_WIN_V_FILTER_P(i));
566                 v0 -= 8;
567                 v1 += 8;
568         }
569 }
570
571 static inline void disable_dc_irq(unsigned int irq)
572 {
573         disable_irq(irq);
574 }
575
576 u32 tegra_dc_get_syncpt_id(const struct tegra_dc *dc, int i)
577 {
578         return dc->syncpt[i].id;
579 }
580 EXPORT_SYMBOL(tegra_dc_get_syncpt_id);
581
582 u32 tegra_dc_incr_syncpt_max(struct tegra_dc *dc, int i)
583 {
584         u32 max;
585
586         mutex_lock(&dc->lock);
587         tegra_dc_hold_dc_out(dc);
588         max = nvhost_syncpt_incr_max_ext(dc->ndev,
589                 dc->syncpt[i].id, ((dc->enabled) ? 1 : 0));
590         dc->syncpt[i].max = max;
591         tegra_dc_release_dc_out(dc);
592         mutex_unlock(&dc->lock);
593
594         return max;
595 }
596
597 void tegra_dc_incr_syncpt_min(struct tegra_dc *dc, int i, u32 val)
598 {
599         mutex_lock(&dc->lock);
600         if (dc->enabled) {
601                 tegra_dc_hold_dc_out(dc);
602                 while (dc->syncpt[i].min < val) {
603                         dc->syncpt[i].min++;
604                         nvhost_syncpt_cpu_incr_ext(dc->ndev, dc->syncpt[i].id);
605                 }
606                 tegra_dc_release_dc_out(dc);
607         }
608         mutex_unlock(&dc->lock);
609 }
610
611 void
612 tegra_dc_config_pwm(struct tegra_dc *dc, struct tegra_dc_pwm_params *cfg)
613 {
614         unsigned int ctrl;
615         unsigned long out_sel;
616         unsigned long cmd_state;
617
618         mutex_lock(&dc->lock);
619         if (!dc->enabled) {
620                 mutex_unlock(&dc->lock);
621                 return;
622         }
623
624         tegra_dc_hold_dc_out(dc);
625
626         ctrl = ((cfg->period << PM_PERIOD_SHIFT) |
627                 (cfg->clk_div << PM_CLK_DIVIDER_SHIFT) |
628                 cfg->clk_select);
629
630         /* The new value should be effected immediately */
631         cmd_state = tegra_dc_readl(dc, DC_CMD_STATE_ACCESS);
632         tegra_dc_writel(dc, (cmd_state | (1 << 2)), DC_CMD_STATE_ACCESS);
633
634         switch (cfg->which_pwm) {
635         case TEGRA_PWM_PM0:
636                 /* Select the LM0 on PM0 */
637                 out_sel = tegra_dc_readl(dc, DC_COM_PIN_OUTPUT_SELECT5);
638                 out_sel &= ~(7 << 0);
639                 out_sel |= (3 << 0);
640                 tegra_dc_writel(dc, out_sel, DC_COM_PIN_OUTPUT_SELECT5);
641                 tegra_dc_writel(dc, ctrl, DC_COM_PM0_CONTROL);
642                 tegra_dc_writel(dc, cfg->duty_cycle, DC_COM_PM0_DUTY_CYCLE);
643                 break;
644         case TEGRA_PWM_PM1:
645                 /* Select the LM1 on PM1 */
646                 out_sel = tegra_dc_readl(dc, DC_COM_PIN_OUTPUT_SELECT5);
647                 out_sel &= ~(7 << 4);
648                 out_sel |= (3 << 4);
649                 tegra_dc_writel(dc, out_sel, DC_COM_PIN_OUTPUT_SELECT5);
650                 tegra_dc_writel(dc, ctrl, DC_COM_PM1_CONTROL);
651                 tegra_dc_writel(dc, cfg->duty_cycle, DC_COM_PM1_DUTY_CYCLE);
652                 break;
653         default:
654                 dev_err(&dc->ndev->dev, "Error: Need which_pwm\n");
655                 break;
656         }
657         tegra_dc_writel(dc, cmd_state, DC_CMD_STATE_ACCESS);
658         tegra_dc_release_dc_out(dc);
659         mutex_unlock(&dc->lock);
660 }
661 EXPORT_SYMBOL(tegra_dc_config_pwm);
662
663 void tegra_dc_set_out_pin_polars(struct tegra_dc *dc,
664                                 const struct tegra_dc_out_pin *pins,
665                                 const unsigned int n_pins)
666 {
667         unsigned int i;
668
669         int name;
670         int pol;
671
672         u32 pol1, pol3;
673
674         u32 set1, unset1;
675         u32 set3, unset3;
676
677         set1 = set3 = unset1 = unset3 = 0;
678
679         for (i = 0; i < n_pins; i++) {
680                 name = (pins + i)->name;
681                 pol  = (pins + i)->pol;
682
683                 /* set polarity by name */
684                 switch (name) {
685                 case TEGRA_DC_OUT_PIN_DATA_ENABLE:
686                         if (pol == TEGRA_DC_OUT_PIN_POL_LOW)
687                                 set3 |= LSPI_OUTPUT_POLARITY_LOW;
688                         else
689                                 unset3 |= LSPI_OUTPUT_POLARITY_LOW;
690                         break;
691                 case TEGRA_DC_OUT_PIN_H_SYNC:
692                         if (pol == TEGRA_DC_OUT_PIN_POL_LOW)
693                                 set1 |= LHS_OUTPUT_POLARITY_LOW;
694                         else
695                                 unset1 |= LHS_OUTPUT_POLARITY_LOW;
696                         break;
697                 case TEGRA_DC_OUT_PIN_V_SYNC:
698                         if (pol == TEGRA_DC_OUT_PIN_POL_LOW)
699                                 set1 |= LVS_OUTPUT_POLARITY_LOW;
700                         else
701                                 unset1 |= LVS_OUTPUT_POLARITY_LOW;
702                         break;
703                 case TEGRA_DC_OUT_PIN_PIXEL_CLOCK:
704                         if (pol == TEGRA_DC_OUT_PIN_POL_LOW)
705                                 set1 |= LSC0_OUTPUT_POLARITY_LOW;
706                         else
707                                 unset1 |= LSC0_OUTPUT_POLARITY_LOW;
708                         break;
709                 default:
710                         printk("Invalid argument in function %s\n",
711                                __FUNCTION__);
712                         break;
713                 }
714         }
715
716         pol1 = DC_COM_PIN_OUTPUT_POLARITY1_INIT_VAL;
717         pol3 = DC_COM_PIN_OUTPUT_POLARITY3_INIT_VAL;
718
719         pol1 |= set1;
720         pol1 &= ~unset1;
721
722         pol3 |= set3;
723         pol3 &= ~unset3;
724
725         tegra_dc_writel(dc, pol1, DC_COM_PIN_OUTPUT_POLARITY1);
726         tegra_dc_writel(dc, pol3, DC_COM_PIN_OUTPUT_POLARITY3);
727 }
728
729 static struct tegra_dc_mode *tegra_dc_get_override_mode(struct tegra_dc *dc)
730 {
731         if (dc->out->type == TEGRA_DC_OUT_RGB ||
732                 dc->out->type == TEGRA_DC_OUT_HDMI ||
733                 dc->out->type == TEGRA_DC_OUT_DSI)
734                 return override_disp_mode[dc->out->type].pclk ?
735                         &override_disp_mode[dc->out->type] : NULL;
736         else
737                 return NULL;
738 }
739
740 static void tegra_dc_set_out(struct tegra_dc *dc, struct tegra_dc_out *out)
741 {
742         struct tegra_dc_mode *mode;
743
744         dc->out = out;
745         mode = tegra_dc_get_override_mode(dc);
746
747         if (mode)
748                 tegra_dc_set_mode(dc, mode);
749         else if (out->n_modes > 0)
750                 tegra_dc_set_mode(dc, &dc->out->modes[0]);
751
752         switch (out->type) {
753         case TEGRA_DC_OUT_RGB:
754                 dc->out_ops = &tegra_dc_rgb_ops;
755                 break;
756
757         case TEGRA_DC_OUT_HDMI:
758                 dc->out_ops = &tegra_dc_hdmi_ops;
759                 break;
760
761         case TEGRA_DC_OUT_DSI:
762                 dc->out_ops = &tegra_dc_dsi_ops;
763                 break;
764
765         default:
766                 dc->out_ops = NULL;
767                 break;
768         }
769
770         if (dc->out_ops && dc->out_ops->init)
771                 dc->out_ops->init(dc);
772
773 }
774
775 unsigned tegra_dc_get_out_height(const struct tegra_dc *dc)
776 {
777         if (dc->out)
778                 return dc->out->height;
779         else
780                 return 0;
781 }
782 EXPORT_SYMBOL(tegra_dc_get_out_height);
783
784 unsigned tegra_dc_get_out_width(const struct tegra_dc *dc)
785 {
786         if (dc->out)
787                 return dc->out->width;
788         else
789                 return 0;
790 }
791 EXPORT_SYMBOL(tegra_dc_get_out_width);
792
793 unsigned tegra_dc_get_out_max_pixclock(const struct tegra_dc *dc)
794 {
795         if (dc->out && dc->out->max_pixclock)
796                 return dc->out->max_pixclock;
797         else
798                 return 0;
799 }
800 EXPORT_SYMBOL(tegra_dc_get_out_max_pixclock);
801
802 void tegra_dc_enable_crc(struct tegra_dc *dc)
803 {
804         u32 val;
805
806         mutex_lock(&dc->lock);
807         tegra_dc_hold_dc_out(dc);
808         tegra_dc_io_start(dc);
809
810         val = CRC_ALWAYS_ENABLE | CRC_INPUT_DATA_ACTIVE_DATA |
811                 CRC_ENABLE_ENABLE;
812         tegra_dc_writel(dc, val, DC_COM_CRC_CONTROL);
813         tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
814         tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
815         tegra_dc_release_dc_out(dc);
816         mutex_unlock(&dc->lock);
817 }
818
819 void tegra_dc_disable_crc(struct tegra_dc *dc)
820 {
821         mutex_lock(&dc->lock);
822         tegra_dc_hold_dc_out(dc);
823         tegra_dc_writel(dc, 0x0, DC_COM_CRC_CONTROL);
824         tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
825         tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
826
827         tegra_dc_io_end(dc);
828         tegra_dc_release_dc_out(dc);
829         mutex_unlock(&dc->lock);
830 }
831
832 u32 tegra_dc_read_checksum_latched(struct tegra_dc *dc)
833 {
834         int crc = 0;
835
836         if (!dc) {
837                 dev_err(&dc->ndev->dev, "Failed to get dc.\n");
838                 goto crc_error;
839         }
840
841         /* TODO: Replace mdelay with code to sync VBlANK, since
842          * DC_COM_CRC_CHECKSUM_LATCHED is available after VBLANK */
843         mdelay(TEGRA_CRC_LATCHED_DELAY);
844
845         mutex_lock(&dc->lock);
846         tegra_dc_hold_dc_out(dc);
847         crc = tegra_dc_readl(dc, DC_COM_CRC_CHECKSUM_LATCHED);
848         tegra_dc_release_dc_out(dc);
849         mutex_unlock(&dc->lock);
850 crc_error:
851         return crc;
852 }
853
854 static bool tegra_dc_windows_are_dirty(struct tegra_dc *dc)
855 {
856 #ifndef CONFIG_TEGRA_SIMULATION_PLATFORM
857         u32 val;
858
859         val = tegra_dc_readl(dc, DC_CMD_STATE_CONTROL);
860         if (val & (WIN_A_ACT_REQ | WIN_B_ACT_REQ | WIN_C_ACT_REQ))
861             return true;
862 #endif
863         return false;
864 }
865
866 static inline void enable_dc_irq(unsigned int irq)
867 {
868 #ifndef CONFIG_TEGRA_FPGA_PLATFORM
869         enable_irq(irq);
870 #else
871         /* Always disable DC interrupts on FPGA. */
872         disable_irq(irq);
873 #endif
874 }
875
876 void tegra_dc_get_fbvblank(struct tegra_dc *dc, struct fb_vblank *vblank)
877 {
878         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
879                 vblank->flags = FB_VBLANK_HAVE_VSYNC;
880 }
881
882 int tegra_dc_wait_for_vsync(struct tegra_dc *dc)
883 {
884         int ret = -ENOTTY;
885
886         if (!(dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE) || !dc->enabled)
887                 return ret;
888
889         /*
890          * Logic is as follows
891          * a) Indicate we need a vblank.
892          * b) Wait for completion to be signalled from isr.
893          * c) Initialize completion for next iteration.
894          */
895
896         tegra_dc_hold_dc_out(dc);
897         dc->out->user_needs_vblank = true;
898
899         ret = wait_for_completion_interruptible(&dc->out->user_vblank_comp);
900         init_completion(&dc->out->user_vblank_comp);
901         tegra_dc_release_dc_out(dc);
902
903         return ret;
904 }
905
906 static void tegra_dc_vblank(struct work_struct *work)
907 {
908         struct tegra_dc *dc = container_of(work, struct tegra_dc, vblank_work);
909         bool nvsd_updated = false;
910
911         mutex_lock(&dc->lock);
912
913         if (!dc->enabled) {
914                 mutex_unlock(&dc->lock);
915                 return;
916         }
917
918         tegra_dc_hold_dc_out(dc);
919         /* use the new frame's bandwidth setting instead of max(current, new),
920          * skip this if we're using tegra_dc_one_shot_worker() */
921         if (!(dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE))
922                 tegra_dc_program_bandwidth(dc, true);
923
924         /* Clear the V_BLANK_FLIP bit of vblank ref-count if update is clean. */
925         if (!tegra_dc_windows_are_dirty(dc))
926                 clear_bit(V_BLANK_FLIP, &dc->vblank_ref_count);
927
928         /* Update the SD brightness */
929         if (dc->enabled && dc->out->sd_settings) {
930                 nvsd_updated = nvsd_update_brightness(dc);
931                 /* Ref-count vblank if nvsd is on-going. Otherwise, clean the
932                  * V_BLANK_NVSD bit of vblank ref-count. */
933                 if (nvsd_updated) {
934                         set_bit(V_BLANK_NVSD, &dc->vblank_ref_count);
935                         tegra_dc_unmask_interrupt(dc, V_BLANK_INT);
936                 } else {
937                         clear_bit(V_BLANK_NVSD, &dc->vblank_ref_count);
938                 }
939         }
940
941         /* Mask vblank interrupt if ref-count is zero. */
942         if (!dc->vblank_ref_count)
943                 tegra_dc_mask_interrupt(dc, V_BLANK_INT);
944
945         tegra_dc_release_dc_out(dc);
946         mutex_unlock(&dc->lock);
947
948         /* Do the actual brightness update outside of the mutex */
949         if (nvsd_updated && dc->out->sd_settings &&
950             dc->out->sd_settings->bl_device) {
951
952                 struct platform_device *pdev = dc->out->sd_settings->bl_device;
953                 struct backlight_device *bl = platform_get_drvdata(pdev);
954                 if (bl)
955                         backlight_update_status(bl);
956         }
957 }
958
959 static void tegra_dc_one_shot_worker(struct work_struct *work)
960 {
961         struct tegra_dc *dc = container_of(
962                 to_delayed_work(work), struct tegra_dc, one_shot_work);
963         mutex_lock(&dc->lock);
964
965         /* memory client has gone idle */
966         tegra_dc_clear_bandwidth(dc);
967
968         if (dc->out_ops->idle)
969                 dc->out_ops->idle(dc);
970
971         mutex_unlock(&dc->lock);
972 }
973
974 /* return an arbitrarily large number if count overflow occurs.
975  * make it a nice base-10 number to show up in stats output */
976 static u64 tegra_dc_underflow_count(struct tegra_dc *dc, unsigned reg)
977 {
978         unsigned count = tegra_dc_readl(dc, reg);
979         tegra_dc_writel(dc, 0, reg);
980         return ((count & 0x80000000) == 0) ? count : 10000000000ll;
981 }
982
983 static void tegra_dc_underflow_handler(struct tegra_dc *dc)
984 {
985         u32 val;
986         int i;
987
988         dc->stats.underflows++;
989         if (dc->underflow_mask & WIN_A_UF_INT) {
990                 dc->stats.underflows_a += tegra_dc_underflow_count(dc,
991                         DC_WINBUF_AD_UFLOW_STATUS);
992                 trace_printk("%s:Window A Underflow\n", dc->ndev->name);
993         }
994         if (dc->underflow_mask & WIN_B_UF_INT) {
995                 dc->stats.underflows_b += tegra_dc_underflow_count(dc,
996                         DC_WINBUF_BD_UFLOW_STATUS);
997                 trace_printk("%s:Window B Underflow\n", dc->ndev->name);
998         }
999         if (dc->underflow_mask & WIN_C_UF_INT) {
1000                 dc->stats.underflows_c += tegra_dc_underflow_count(dc,
1001                         DC_WINBUF_CD_UFLOW_STATUS);
1002                 trace_printk("%s:Window C Underflow\n", dc->ndev->name);
1003         }
1004
1005         /* Check for any underflow reset conditions */
1006         for (i = 0; i < DC_N_WINDOWS; i++) {
1007                 if (dc->underflow_mask & (WIN_A_UF_INT << i)) {
1008                         dc->windows[i].underflows++;
1009
1010 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1011                         if (dc->windows[i].underflows > 4) {
1012                                 schedule_work(&dc->reset_work);
1013                                 /* reset counter */
1014                                 dc->windows[i].underflows = 0;
1015                                 trace_printk("%s:Reset work scheduled for "
1016                                                 "window %c\n",
1017                                                 dc->ndev->name, (65 + i));
1018                         }
1019 #endif
1020 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
1021                         if (dc->windows[i].underflows > 4) {
1022                                 trace_printk("%s:window %c in underflow state."
1023                                         " enable UF_LINE_FLUSH to clear up\n",
1024                                         dc->ndev->name, (65 + i));
1025                                 tegra_dc_writel(dc, UF_LINE_FLUSH,
1026                                                 DC_DISP_DISP_MISC_CONTROL);
1027                                 tegra_dc_writel(dc, GENERAL_UPDATE,
1028                                                 DC_CMD_STATE_CONTROL);
1029                                 tegra_dc_writel(dc, GENERAL_ACT_REQ,
1030                                                 DC_CMD_STATE_CONTROL);
1031
1032                                 tegra_dc_writel(dc, 0,
1033                                                 DC_DISP_DISP_MISC_CONTROL);
1034                                 tegra_dc_writel(dc, GENERAL_UPDATE,
1035                                                 DC_CMD_STATE_CONTROL);
1036                                 tegra_dc_writel(dc, GENERAL_ACT_REQ,
1037                                                 DC_CMD_STATE_CONTROL);
1038                         }
1039 #endif
1040                 } else {
1041                         dc->windows[i].underflows = 0;
1042                 }
1043         }
1044
1045         /* Clear the underflow mask now that we've checked it. */
1046         tegra_dc_writel(dc, dc->underflow_mask, DC_CMD_INT_STATUS);
1047         dc->underflow_mask = 0;
1048         val = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1049         tegra_dc_writel(dc, val | ALL_UF_INT, DC_CMD_INT_MASK);
1050         print_underflow_info(dc);
1051 }
1052
1053 #ifndef CONFIG_TEGRA_FPGA_PLATFORM
1054 static void tegra_dc_one_shot_irq(struct tegra_dc *dc, unsigned long status)
1055 {
1056         /* pending user vblank, so wakeup */
1057         if ((status & (V_BLANK_INT | MSF_INT)) &&
1058             (dc->out->user_needs_vblank)) {
1059                 dc->out->user_needs_vblank = false;
1060                 complete(&dc->out->user_vblank_comp);
1061         }
1062
1063         if (status & V_BLANK_INT) {
1064                 /* Sync up windows. */
1065                 tegra_dc_trigger_windows(dc);
1066
1067                 /* Schedule any additional bottom-half vblank actvities. */
1068                 queue_work(system_freezable_wq, &dc->vblank_work);
1069         }
1070
1071         if (status & FRAME_END_INT) {
1072                 /* Mark the frame_end as complete. */
1073                 if (!completion_done(&dc->frame_end_complete))
1074                         complete(&dc->frame_end_complete);
1075         }
1076 }
1077
1078 static void tegra_dc_continuous_irq(struct tegra_dc *dc, unsigned long status)
1079 {
1080         /* Schedule any additional bottom-half vblank actvities. */
1081         if (status & V_BLANK_INT)
1082                 queue_work(system_freezable_wq, &dc->vblank_work);
1083
1084         if (status & FRAME_END_INT) {
1085                 struct timespec tm = CURRENT_TIME;
1086                 dc->frame_end_timestamp = timespec_to_ns(&tm);
1087                 wake_up(&dc->timestamp_wq);
1088
1089                 /* Mark the frame_end as complete. */
1090                 if (!completion_done(&dc->frame_end_complete))
1091                         complete(&dc->frame_end_complete);
1092
1093                 tegra_dc_trigger_windows(dc);
1094         }
1095 }
1096
1097 /* XXX: Not sure if we limit look ahead to 1 frame */
1098 bool tegra_dc_is_within_n_vsync(struct tegra_dc *dc, s64 ts)
1099 {
1100         return ((ts - dc->frame_end_timestamp) < dc->frametime_ns);
1101 }
1102
1103 bool tegra_dc_does_vsync_separate(struct tegra_dc *dc, s64 new_ts, s64 old_ts)
1104 {
1105         return (((new_ts - old_ts) > dc->frametime_ns)
1106                 || (div_s64((new_ts - dc->frame_end_timestamp), dc->frametime_ns)
1107                         != div_s64((old_ts - dc->frame_end_timestamp),
1108                                 dc->frametime_ns)));
1109 }
1110 #endif
1111
1112 static irqreturn_t tegra_dc_irq(int irq, void *ptr)
1113 {
1114 #ifndef CONFIG_TEGRA_FPGA_PLATFORM
1115         struct tegra_dc *dc = ptr;
1116         unsigned long status;
1117         unsigned long underflow_mask;
1118         u32 val;
1119
1120         if (!nvhost_module_powered_ext(nvhost_get_parent(dc->ndev))) {
1121                 WARN(1, "IRQ when DC not powered!\n");
1122                 tegra_dc_io_start(dc);
1123                 status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1124                 tegra_dc_writel(dc, status, DC_CMD_INT_STATUS);
1125                 tegra_dc_io_end(dc);
1126                 return IRQ_HANDLED;
1127         }
1128
1129         /* clear all status flags except underflow, save those for the worker */
1130         status = tegra_dc_readl(dc, DC_CMD_INT_STATUS);
1131         tegra_dc_writel(dc, status & ~ALL_UF_INT, DC_CMD_INT_STATUS);
1132         val = tegra_dc_readl(dc, DC_CMD_INT_MASK);
1133         tegra_dc_writel(dc, val & ~ALL_UF_INT, DC_CMD_INT_MASK);
1134
1135         /*
1136          * Overlays can get thier internal state corrupted during and underflow
1137          * condition.  The only way to fix this state is to reset the DC.
1138          * if we get 4 consecutive frames with underflows, assume we're
1139          * hosed and reset.
1140          */
1141         underflow_mask = status & ALL_UF_INT;
1142
1143         /* Check underflow */
1144         if (underflow_mask) {
1145                 dc->underflow_mask |= underflow_mask;
1146                 schedule_delayed_work(&dc->underflow_work,
1147                         msecs_to_jiffies(1));
1148         }
1149
1150         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
1151                 tegra_dc_one_shot_irq(dc, status);
1152         else
1153                 tegra_dc_continuous_irq(dc, status);
1154
1155         return IRQ_HANDLED;
1156 #else /* CONFIG_TEGRA_FPGA_PLATFORM */
1157         return IRQ_NONE;
1158 #endif /* !CONFIG_TEGRA_FPGA_PLATFORM */
1159 }
1160
1161 static void tegra_dc_set_color_control(struct tegra_dc *dc)
1162 {
1163         u32 color_control;
1164
1165         switch (dc->out->depth) {
1166         case 3:
1167                 color_control = BASE_COLOR_SIZE111;
1168                 break;
1169
1170         case 6:
1171                 color_control = BASE_COLOR_SIZE222;
1172                 break;
1173
1174         case 8:
1175                 color_control = BASE_COLOR_SIZE332;
1176                 break;
1177
1178         case 9:
1179                 color_control = BASE_COLOR_SIZE333;
1180                 break;
1181
1182         case 12:
1183                 color_control = BASE_COLOR_SIZE444;
1184                 break;
1185
1186         case 15:
1187                 color_control = BASE_COLOR_SIZE555;
1188                 break;
1189
1190         case 16:
1191                 color_control = BASE_COLOR_SIZE565;
1192                 break;
1193
1194         case 18:
1195                 color_control = BASE_COLOR_SIZE666;
1196                 break;
1197
1198         default:
1199                 color_control = BASE_COLOR_SIZE888;
1200                 break;
1201         }
1202
1203         switch (dc->out->dither) {
1204         case TEGRA_DC_DISABLE_DITHER:
1205                 color_control |= DITHER_CONTROL_DISABLE;
1206                 break;
1207         case TEGRA_DC_ORDERED_DITHER:
1208                 color_control |= DITHER_CONTROL_ORDERED;
1209                 break;
1210         case TEGRA_DC_ERRDIFF_DITHER:
1211                 /* The line buffer for error-diffusion dither is limited
1212                  * to 1280 pixels per line. This limits the maximum
1213                  * horizontal active area size to 1280 pixels when error
1214                  * diffusion is enabled.
1215                  */
1216                 BUG_ON(dc->mode.h_active > 1280);
1217                 color_control |= DITHER_CONTROL_ERRDIFF;
1218                 break;
1219         }
1220
1221         tegra_dc_writel(dc, color_control, DC_DISP_DISP_COLOR_CONTROL);
1222 }
1223
1224 static u32 get_syncpt(struct tegra_dc *dc, int idx)
1225 {
1226         u32 syncpt_id;
1227
1228         switch (dc->ndev->id) {
1229         case 0:
1230                 switch (idx) {
1231                 case 0:
1232                         syncpt_id = NVSYNCPT_DISP0_A;
1233                         break;
1234                 case 1:
1235                         syncpt_id = NVSYNCPT_DISP0_B;
1236                         break;
1237                 case 2:
1238                         syncpt_id = NVSYNCPT_DISP0_C;
1239                         break;
1240                 default:
1241                         BUG();
1242                         break;
1243                 }
1244                 break;
1245         case 1:
1246                 switch (idx) {
1247                 case 0:
1248                         syncpt_id = NVSYNCPT_DISP1_A;
1249                         break;
1250                 case 1:
1251                         syncpt_id = NVSYNCPT_DISP1_B;
1252                         break;
1253                 case 2:
1254                         syncpt_id = NVSYNCPT_DISP1_C;
1255                         break;
1256                 default:
1257                         BUG();
1258                         break;
1259                 }
1260                 break;
1261         default:
1262                 BUG();
1263                 break;
1264         }
1265
1266         return syncpt_id;
1267 }
1268
1269 static int tegra_dc_init(struct tegra_dc *dc)
1270 {
1271         int i;
1272         int int_enable;
1273
1274         tegra_dc_writel(dc, 0x00000100, DC_CMD_GENERAL_INCR_SYNCPT_CNTRL);
1275         if (dc->ndev->id == 0) {
1276                 tegra_mc_set_priority(TEGRA_MC_CLIENT_DISPLAY0A,
1277                                       TEGRA_MC_PRIO_MED);
1278                 tegra_mc_set_priority(TEGRA_MC_CLIENT_DISPLAY0B,
1279                                       TEGRA_MC_PRIO_MED);
1280                 tegra_mc_set_priority(TEGRA_MC_CLIENT_DISPLAY0C,
1281                                       TEGRA_MC_PRIO_MED);
1282                 tegra_mc_set_priority(TEGRA_MC_CLIENT_DISPLAY1B,
1283                                       TEGRA_MC_PRIO_MED);
1284                 tegra_mc_set_priority(TEGRA_MC_CLIENT_DISPLAYHC,
1285                                       TEGRA_MC_PRIO_HIGH);
1286         } else if (dc->ndev->id == 1) {
1287                 tegra_mc_set_priority(TEGRA_MC_CLIENT_DISPLAY0AB,
1288                                       TEGRA_MC_PRIO_MED);
1289                 tegra_mc_set_priority(TEGRA_MC_CLIENT_DISPLAY0BB,
1290                                       TEGRA_MC_PRIO_MED);
1291                 tegra_mc_set_priority(TEGRA_MC_CLIENT_DISPLAY0CB,
1292                                       TEGRA_MC_PRIO_MED);
1293                 tegra_mc_set_priority(TEGRA_MC_CLIENT_DISPLAY1BB,
1294                                       TEGRA_MC_PRIO_MED);
1295                 tegra_mc_set_priority(TEGRA_MC_CLIENT_DISPLAYHCB,
1296                                       TEGRA_MC_PRIO_HIGH);
1297         }
1298         tegra_dc_writel(dc, 0x00000100 | dc->vblank_syncpt,
1299                         DC_CMD_CONT_SYNCPT_VSYNC);
1300         tegra_dc_writel(dc, 0x00004700, DC_CMD_INT_TYPE);
1301         tegra_dc_writel(dc, 0x0001c700, DC_CMD_INT_POLARITY);
1302         tegra_dc_writel(dc, 0x00202020, DC_DISP_MEM_HIGH_PRIORITY);
1303         tegra_dc_writel(dc, 0x00010101, DC_DISP_MEM_HIGH_PRIORITY_TIMER);
1304 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
1305         tegra_dc_writel(dc, 0x00000000, DC_DISP_DISP_MISC_CONTROL);
1306 #endif
1307         /* enable interrupts for vblank, frame_end and underflows */
1308         int_enable = (FRAME_END_INT | V_BLANK_INT | ALL_UF_INT);
1309         /* for panels with one-shot mode enable tearing effect interrupt */
1310         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
1311                 int_enable |= MSF_INT;
1312
1313         tegra_dc_writel(dc, int_enable, DC_CMD_INT_ENABLE);
1314         tegra_dc_writel(dc, ALL_UF_INT, DC_CMD_INT_MASK);
1315
1316         tegra_dc_writel(dc, 0x00000000, DC_DISP_BORDER_COLOR);
1317
1318         tegra_dc_set_color_control(dc);
1319         for (i = 0; i < DC_N_WINDOWS; i++) {
1320                 struct tegra_dc_win *win = &dc->windows[i];
1321                 tegra_dc_writel(dc, WINDOW_A_SELECT << i,
1322                                 DC_CMD_DISPLAY_WINDOW_HEADER);
1323                 tegra_dc_set_csc(dc, &win->csc);
1324                 tegra_dc_set_lut(dc, win);
1325                 tegra_dc_set_scaling_filter(dc);
1326         }
1327
1328
1329         for (i = 0; i < dc->n_windows; i++) {
1330                 u32 syncpt = get_syncpt(dc, i);
1331
1332                 dc->syncpt[i].id = syncpt;
1333
1334                 dc->syncpt[i].min = dc->syncpt[i].max =
1335                         nvhost_syncpt_read_ext(dc->ndev, syncpt);
1336         }
1337
1338         print_mode_info(dc, dc->mode);
1339
1340         if (dc->mode.pclk)
1341                 if (tegra_dc_program_mode(dc, &dc->mode))
1342                         return -EINVAL;
1343
1344         /* Initialize SD AFTER the modeset.
1345            nvsd_init handles the sd_settings = NULL case. */
1346         nvsd_init(dc, dc->out->sd_settings);
1347
1348         return 0;
1349 }
1350
1351 static bool _tegra_dc_controller_enable(struct tegra_dc *dc)
1352 {
1353         int failed_init = 0;
1354
1355         if (dc->out->enable)
1356                 dc->out->enable();
1357
1358         tegra_dc_setup_clk(dc, dc->clk);
1359         tegra_dc_clk_enable(dc);
1360
1361         /* do not accept interrupts during initialization */
1362         tegra_dc_writel(dc, 0, DC_CMD_INT_MASK);
1363
1364         enable_dc_irq(dc->irq);
1365
1366         failed_init = tegra_dc_init(dc);
1367         if (failed_init) {
1368                 tegra_dc_writel(dc, 0, DC_CMD_INT_MASK);
1369                 disable_irq(dc->irq);
1370                 tegra_dc_clear_bandwidth(dc);
1371                 tegra_dc_clk_disable(dc);
1372                 if (dc->out && dc->out->disable)
1373                         dc->out->disable();
1374                 return false;
1375         }
1376
1377         if (dc->out_ops && dc->out_ops->enable)
1378                 dc->out_ops->enable(dc);
1379
1380         /* force a full blending update */
1381         dc->blend.z[0] = -1;
1382
1383         tegra_dc_ext_enable(dc->ext);
1384
1385         trace_printk("%s:enable\n", dc->ndev->name);
1386
1387         tegra_dc_writel(dc, GENERAL_UPDATE, DC_CMD_STATE_CONTROL);
1388         tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
1389
1390         if (dc->out->postpoweron)
1391                 dc->out->postpoweron();
1392
1393         return true;
1394 }
1395
1396 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1397 static bool _tegra_dc_controller_reset_enable(struct tegra_dc *dc)
1398 {
1399         bool ret = true;
1400
1401         if (dc->out->enable)
1402                 dc->out->enable();
1403
1404         tegra_dc_setup_clk(dc, dc->clk);
1405         tegra_dc_clk_enable(dc);
1406
1407         if (dc->ndev->id == 0 && tegra_dcs[1] != NULL) {
1408                 mutex_lock(&tegra_dcs[1]->lock);
1409                 disable_irq(tegra_dcs[1]->irq);
1410         } else if (dc->ndev->id == 1 && tegra_dcs[0] != NULL) {
1411                 mutex_lock(&tegra_dcs[0]->lock);
1412                 disable_irq(tegra_dcs[0]->irq);
1413         }
1414
1415         msleep(5);
1416         tegra_periph_reset_assert(dc->clk);
1417         msleep(2);
1418 #ifdef CONFIG_TEGRA_SILICON_PLATFORM
1419         tegra_periph_reset_deassert(dc->clk);
1420         msleep(1);
1421 #endif
1422
1423         if (dc->ndev->id == 0 && tegra_dcs[1] != NULL) {
1424                 enable_dc_irq(tegra_dcs[1]->irq);
1425                 mutex_unlock(&tegra_dcs[1]->lock);
1426         } else if (dc->ndev->id == 1 && tegra_dcs[0] != NULL) {
1427                 enable_dc_irq(tegra_dcs[0]->irq);
1428                 mutex_unlock(&tegra_dcs[0]->lock);
1429         }
1430
1431         enable_dc_irq(dc->irq);
1432
1433         if (tegra_dc_init(dc)) {
1434                 dev_err(&dc->ndev->dev, "cannot initialize\n");
1435                 ret = false;
1436         }
1437
1438         if (dc->out_ops && dc->out_ops->enable)
1439                 dc->out_ops->enable(dc);
1440
1441         if (dc->out->postpoweron)
1442                 dc->out->postpoweron();
1443
1444         /* force a full blending update */
1445         dc->blend.z[0] = -1;
1446
1447         tegra_dc_ext_enable(dc->ext);
1448
1449         if (!ret) {
1450                 dev_err(&dc->ndev->dev, "initialization failed,disabling");
1451                 _tegra_dc_controller_disable(dc);
1452         }
1453
1454         trace_printk("%s:reset enable\n", dc->ndev->name);
1455         return ret;
1456 }
1457 #endif
1458
1459 static int _tegra_dc_set_default_videomode(struct tegra_dc *dc)
1460 {
1461         return tegra_dc_set_fb_mode(dc, &tegra_dc_hdmi_fallback_mode, 0);
1462 }
1463
1464 static bool _tegra_dc_enable(struct tegra_dc *dc)
1465 {
1466         if (dc->mode.pclk == 0) {
1467                 switch (dc->out->type) {
1468                 case TEGRA_DC_OUT_HDMI:
1469                 /* DC enable called but no videomode is loaded.
1470                      Check if HDMI is connected, then set fallback mdoe */
1471                 if (tegra_dc_hpd(dc)) {
1472                         if (_tegra_dc_set_default_videomode(dc))
1473                                 return false;
1474                 } else
1475                         return false;
1476
1477                 break;
1478
1479                 /* Do nothing for other outputs for now */
1480                 case TEGRA_DC_OUT_RGB:
1481
1482                 case TEGRA_DC_OUT_DSI:
1483
1484                 default:
1485                         return false;
1486                 }
1487         }
1488
1489         if (!dc->out)
1490                 return false;
1491
1492         tegra_dc_io_start(dc);
1493
1494         if (!_tegra_dc_controller_enable(dc)) {
1495                 tegra_dc_io_end(dc);
1496                 return false;
1497         }
1498         return true;
1499 }
1500
1501 void tegra_dc_enable(struct tegra_dc *dc)
1502 {
1503         mutex_lock(&dc->lock);
1504
1505         if (!dc->enabled)
1506                 dc->enabled = _tegra_dc_enable(dc);
1507
1508         mutex_unlock(&dc->lock);
1509         print_mode_info(dc, dc->mode);
1510 }
1511
1512 static void _tegra_dc_controller_disable(struct tegra_dc *dc)
1513 {
1514         unsigned i;
1515
1516         if (dc->out && dc->out->prepoweroff)
1517                 dc->out->prepoweroff();
1518
1519         if (dc->out_ops && dc->out_ops->disable)
1520                 dc->out_ops->disable(dc);
1521
1522         tegra_dc_writel(dc, 0, DC_CMD_INT_MASK);
1523         tegra_dc_writel(dc, 0, DC_CMD_INT_ENABLE);
1524         disable_irq(dc->irq);
1525
1526         tegra_dc_clear_bandwidth(dc);
1527         tegra_dc_clk_disable(dc);
1528
1529         if (dc->out && dc->out->disable)
1530                 dc->out->disable();
1531
1532         for (i = 0; i < dc->n_windows; i++) {
1533                 struct tegra_dc_win *w = &dc->windows[i];
1534
1535                 /* reset window bandwidth */
1536                 w->bandwidth = 0;
1537                 w->new_bandwidth = 0;
1538
1539                 /* disable windows */
1540                 w->flags &= ~TEGRA_WIN_FLAG_ENABLED;
1541
1542                 /* flush any pending syncpt waits */
1543                 while (dc->syncpt[i].min < dc->syncpt[i].max) {
1544                         trace_printk("%s:syncpt flush id=%d\n", dc->ndev->name,
1545                                 dc->syncpt[i].id);
1546                         dc->syncpt[i].min++;
1547                         nvhost_syncpt_cpu_incr_ext(dc->ndev, dc->syncpt[i].id);
1548                 }
1549         }
1550         trace_printk("%s:disabled\n", dc->ndev->name);
1551 }
1552
1553 void tegra_dc_stats_enable(struct tegra_dc *dc, bool enable)
1554 {
1555 #if 0 /* underflow interrupt is already enabled by dc reset worker */
1556         u32 val;
1557         if (dc->enabled)  {
1558                 val = tegra_dc_readl(dc, DC_CMD_INT_ENABLE);
1559                 if (enable)
1560                         val |= (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT);
1561                 else
1562                         val &= ~(WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT);
1563                 tegra_dc_writel(dc, val, DC_CMD_INT_ENABLE);
1564         }
1565 #endif
1566 }
1567
1568 bool tegra_dc_stats_get(struct tegra_dc *dc)
1569 {
1570 #if 0 /* right now it is always enabled */
1571         u32 val;
1572         bool res;
1573
1574         if (dc->enabled)  {
1575                 val = tegra_dc_readl(dc, DC_CMD_INT_ENABLE);
1576                 res = !!(val & (WIN_A_UF_INT | WIN_B_UF_INT | WIN_C_UF_INT));
1577         } else {
1578                 res = false;
1579         }
1580
1581         return res;
1582 #endif
1583         return true;
1584 }
1585
1586 /* make the screen blank by disabling all windows */
1587 void tegra_dc_blank(struct tegra_dc *dc)
1588 {
1589         struct tegra_dc_win *dcwins[DC_N_WINDOWS];
1590         unsigned i;
1591
1592         for (i = 0; i < DC_N_WINDOWS; i++) {
1593                 dcwins[i] = tegra_dc_get_window(dc, i);
1594                 dcwins[i]->flags &= ~TEGRA_WIN_FLAG_ENABLED;
1595         }
1596
1597         tegra_dc_update_windows(dcwins, DC_N_WINDOWS);
1598         tegra_dc_sync_windows(dcwins, DC_N_WINDOWS);
1599 }
1600
1601 static void _tegra_dc_disable(struct tegra_dc *dc)
1602 {
1603         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE) {
1604                 mutex_lock(&dc->one_shot_lock);
1605                 cancel_delayed_work_sync(&dc->one_shot_work);
1606         }
1607
1608         tegra_dc_hold_dc_out(dc);
1609
1610         _tegra_dc_controller_disable(dc);
1611         tegra_dc_io_end(dc);
1612
1613         tegra_dc_release_dc_out(dc);
1614
1615         if (dc->out->flags & TEGRA_DC_OUT_ONE_SHOT_MODE)
1616                 mutex_unlock(&dc->one_shot_lock);
1617 }
1618
1619 void tegra_dc_disable(struct tegra_dc *dc)
1620 {
1621         tegra_dc_ext_disable(dc->ext);
1622
1623         /* it's important that new underflow work isn't scheduled before the
1624          * lock is acquired. */
1625         cancel_delayed_work_sync(&dc->underflow_work);
1626
1627         mutex_lock(&dc->lock);
1628
1629         if (dc->enabled) {
1630                 dc->enabled = false;
1631
1632                 if (!dc->suspended)
1633                         _tegra_dc_disable(dc);
1634         }
1635
1636 #ifdef CONFIG_SWITCH
1637         switch_set_state(&dc->modeset_switch, 0);
1638 #endif
1639
1640         mutex_unlock(&dc->lock);
1641         print_mode_info(dc, dc->mode);
1642 }
1643
1644 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1645 static void tegra_dc_reset_worker(struct work_struct *work)
1646 {
1647         struct tegra_dc *dc =
1648                 container_of(work, struct tegra_dc, reset_work);
1649
1650         unsigned long val = 0;
1651
1652         mutex_lock(&shared_lock);
1653
1654         dev_warn(&dc->ndev->dev,
1655                 "overlay stuck in underflow state.  resetting.\n");
1656
1657         tegra_dc_ext_disable(dc->ext);
1658
1659         mutex_lock(&dc->lock);
1660
1661         if (dc->enabled == false)
1662                 goto unlock;
1663
1664         dc->enabled = false;
1665
1666         /*
1667          * off host read bus
1668          */
1669         val = tegra_dc_readl(dc, DC_CMD_CONT_SYNCPT_VSYNC);
1670         val &= ~(0x00000100);
1671         tegra_dc_writel(dc, val, DC_CMD_CONT_SYNCPT_VSYNC);
1672
1673         /*
1674          * set DC to STOP mode
1675          */
1676         tegra_dc_writel(dc, DISP_CTRL_MODE_STOP, DC_CMD_DISPLAY_COMMAND);
1677
1678         msleep(10);
1679
1680         _tegra_dc_controller_disable(dc);
1681
1682         /* _tegra_dc_controller_reset_enable deasserts reset */
1683         _tegra_dc_controller_reset_enable(dc);
1684
1685         dc->enabled = true;
1686
1687         /* reopen host read bus */
1688         val = tegra_dc_readl(dc, DC_CMD_CONT_SYNCPT_VSYNC);
1689         val &= ~(0x00000100);
1690         val |= 0x100;
1691         tegra_dc_writel(dc, val, DC_CMD_CONT_SYNCPT_VSYNC);
1692
1693 unlock:
1694         mutex_unlock(&dc->lock);
1695         mutex_unlock(&shared_lock);
1696         trace_printk("%s:reset complete\n", dc->ndev->name);
1697 }
1698 #endif
1699
1700 static void tegra_dc_underflow_worker(struct work_struct *work)
1701 {
1702         struct tegra_dc *dc = container_of(
1703                 to_delayed_work(work), struct tegra_dc, underflow_work);
1704
1705         mutex_lock(&dc->lock);
1706         tegra_dc_hold_dc_out(dc);
1707
1708         if (dc->enabled) {
1709                 tegra_dc_underflow_handler(dc);
1710         }
1711         tegra_dc_release_dc_out(dc);
1712         mutex_unlock(&dc->lock);
1713 }
1714
1715 #ifdef CONFIG_SWITCH
1716 static ssize_t switch_modeset_print_mode(struct switch_dev *sdev, char *buf)
1717 {
1718         struct tegra_dc *dc =
1719                 container_of(sdev, struct tegra_dc, modeset_switch);
1720
1721         if (!sdev->state)
1722                 return sprintf(buf, "offline\n");
1723
1724         return sprintf(buf, "%dx%d\n", dc->mode.h_active, dc->mode.v_active);
1725 }
1726 #endif
1727
1728 static int tegra_dc_probe(struct nvhost_device *ndev,
1729         struct nvhost_device_id *id_table)
1730 {
1731         struct tegra_dc *dc;
1732         struct tegra_dc_mode *mode;
1733         struct clk *clk;
1734         struct clk *emc_clk;
1735         struct resource *res;
1736         struct resource *base_res;
1737         struct resource *fb_mem = NULL;
1738         int ret = 0;
1739         void __iomem *base;
1740         int irq;
1741         int i;
1742
1743         if (!ndev->dev.platform_data) {
1744                 dev_err(&ndev->dev, "no platform data\n");
1745                 return -ENOENT;
1746         }
1747
1748         dc = kzalloc(sizeof(struct tegra_dc), GFP_KERNEL);
1749         if (!dc) {
1750                 dev_err(&ndev->dev, "can't allocate memory for tegra_dc\n");
1751                 return -ENOMEM;
1752         }
1753
1754         irq = nvhost_get_irq_byname(ndev, "irq");
1755         if (irq <= 0) {
1756                 dev_err(&ndev->dev, "no irq\n");
1757                 ret = -ENOENT;
1758                 goto err_free;
1759         }
1760
1761         res = nvhost_get_resource_byname(ndev, IORESOURCE_MEM, "regs");
1762         if (!res) {
1763                 dev_err(&ndev->dev, "no mem resource\n");
1764                 ret = -ENOENT;
1765                 goto err_free;
1766         }
1767
1768         base_res = request_mem_region(res->start, resource_size(res),
1769                 ndev->name);
1770         if (!base_res) {
1771                 dev_err(&ndev->dev, "request_mem_region failed\n");
1772                 ret = -EBUSY;
1773                 goto err_free;
1774         }
1775
1776         base = ioremap(res->start, resource_size(res));
1777         if (!base) {
1778                 dev_err(&ndev->dev, "registers can't be mapped\n");
1779                 ret = -EBUSY;
1780                 goto err_release_resource_reg;
1781         }
1782
1783         fb_mem = nvhost_get_resource_byname(ndev, IORESOURCE_MEM, "fbmem");
1784
1785         clk = clk_get(&ndev->dev, NULL);
1786         if (IS_ERR_OR_NULL(clk)) {
1787                 dev_err(&ndev->dev, "can't get clock\n");
1788                 ret = -ENOENT;
1789                 goto err_iounmap_reg;
1790         }
1791
1792         emc_clk = clk_get(&ndev->dev, "emc");
1793         if (IS_ERR_OR_NULL(emc_clk)) {
1794                 dev_err(&ndev->dev, "can't get emc clock\n");
1795                 ret = -ENOENT;
1796                 goto err_put_clk;
1797         }
1798
1799         dc->clk = clk;
1800         dc->emc_clk = emc_clk;
1801         dc->shift_clk_div = 1;
1802         /* Initialize one shot work delay, it will be assigned by dsi
1803          * according to refresh rate later. */
1804         dc->one_shot_delay_ms = 40;
1805
1806         dc->base_res = base_res;
1807         dc->base = base;
1808         dc->irq = irq;
1809         dc->ndev = ndev;
1810         dc->pdata = ndev->dev.platform_data;
1811
1812         /*
1813          * The emc is a shared clock, it will be set based on
1814          * the requirements for each user on the bus.
1815          */
1816         dc->emc_clk_rate = 0;
1817
1818         mutex_init(&dc->lock);
1819         mutex_init(&dc->one_shot_lock);
1820         init_completion(&dc->frame_end_complete);
1821         init_waitqueue_head(&dc->wq);
1822         init_waitqueue_head(&dc->timestamp_wq);
1823 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1824         INIT_WORK(&dc->reset_work, tegra_dc_reset_worker);
1825 #endif
1826         INIT_WORK(&dc->vblank_work, tegra_dc_vblank);
1827         dc->vblank_ref_count = 0;
1828         INIT_DELAYED_WORK(&dc->underflow_work, tegra_dc_underflow_worker);
1829         INIT_DELAYED_WORK(&dc->one_shot_work, tegra_dc_one_shot_worker);
1830
1831         tegra_dc_init_lut_defaults(&dc->fb_lut);
1832
1833         dc->n_windows = DC_N_WINDOWS;
1834         for (i = 0; i < dc->n_windows; i++) {
1835                 struct tegra_dc_win *win = &dc->windows[i];
1836                 win->idx = i;
1837                 win->dc = dc;
1838                 tegra_dc_init_csc_defaults(&win->csc);
1839                 tegra_dc_init_lut_defaults(&win->lut);
1840         }
1841
1842         ret = tegra_dc_set(dc, ndev->id);
1843         if (ret < 0) {
1844                 dev_err(&ndev->dev, "can't add dc\n");
1845                 goto err_free_irq;
1846         }
1847
1848         nvhost_set_drvdata(ndev, dc);
1849
1850 #ifdef CONFIG_SWITCH
1851         dc->modeset_switch.name = dev_name(&ndev->dev);
1852         dc->modeset_switch.state = 0;
1853         dc->modeset_switch.print_state = switch_modeset_print_mode;
1854         switch_dev_register(&dc->modeset_switch);
1855 #endif
1856
1857         tegra_dc_feature_register(dc);
1858
1859         if (dc->pdata->default_out)
1860                 tegra_dc_set_out(dc, dc->pdata->default_out);
1861         else
1862                 dev_err(&ndev->dev, "No default output specified.  Leaving output disabled.\n");
1863
1864         dc->vblank_syncpt = (dc->ndev->id == 0) ?
1865                 NVSYNCPT_VBLANK0 : NVSYNCPT_VBLANK1;
1866
1867         dc->ext = tegra_dc_ext_register(ndev, dc);
1868         if (IS_ERR_OR_NULL(dc->ext)) {
1869                 dev_warn(&ndev->dev, "Failed to enable Tegra DC extensions.\n");
1870                 dc->ext = NULL;
1871         }
1872
1873         mutex_lock(&dc->lock);
1874         if (dc->pdata->flags & TEGRA_DC_FLAG_ENABLED)
1875                 dc->enabled = _tegra_dc_enable(dc);
1876         mutex_unlock(&dc->lock);
1877
1878         /* interrupt handler must be registered before tegra_fb_register() */
1879         if (request_irq(irq, tegra_dc_irq, 0,
1880                         dev_name(&ndev->dev), dc)) {
1881                 dev_err(&ndev->dev, "request_irq %d failed\n", irq);
1882                 ret = -EBUSY;
1883                 goto err_put_emc_clk;
1884         }
1885
1886         tegra_dc_create_debugfs(dc);
1887
1888         dev_info(&ndev->dev, "probed\n");
1889
1890         if (dc->pdata->fb) {
1891                 if (dc->enabled && dc->pdata->fb->bits_per_pixel == -1) {
1892                         unsigned long fmt;
1893                         tegra_dc_writel(dc,
1894                                         WINDOW_A_SELECT << dc->pdata->fb->win,
1895                                         DC_CMD_DISPLAY_WINDOW_HEADER);
1896
1897                         fmt = tegra_dc_readl(dc, DC_WIN_COLOR_DEPTH);
1898                         dc->pdata->fb->bits_per_pixel =
1899                                 tegra_dc_fmt_bpp(fmt);
1900                 }
1901
1902                 mode = tegra_dc_get_override_mode(dc);
1903                 if (mode) {
1904                         dc->pdata->fb->xres = mode->h_active;
1905                         dc->pdata->fb->yres = mode->v_active;
1906                 }
1907
1908                 dc->fb = tegra_fb_register(ndev, dc, dc->pdata->fb, fb_mem);
1909                 if (IS_ERR_OR_NULL(dc->fb))
1910                         dc->fb = NULL;
1911         }
1912
1913         if (dc->out && dc->out->hotplug_init)
1914                 dc->out->hotplug_init();
1915
1916         if (dc->out_ops && dc->out_ops->detect)
1917                 dc->out_ops->detect(dc);
1918         else
1919                 dc->connected = true;
1920
1921         tegra_dc_create_sysfs(&dc->ndev->dev);
1922
1923         return 0;
1924
1925 err_free_irq:
1926         free_irq(irq, dc);
1927 err_put_emc_clk:
1928         clk_put(emc_clk);
1929 err_put_clk:
1930         clk_put(clk);
1931 err_iounmap_reg:
1932         iounmap(base);
1933         if (fb_mem)
1934                 release_resource(fb_mem);
1935 err_release_resource_reg:
1936         release_resource(base_res);
1937 err_free:
1938         kfree(dc);
1939
1940         return ret;
1941 }
1942
1943 static int tegra_dc_remove(struct nvhost_device *ndev)
1944 {
1945         struct tegra_dc *dc = nvhost_get_drvdata(ndev);
1946
1947         tegra_dc_remove_sysfs(&dc->ndev->dev);
1948         tegra_dc_remove_debugfs(dc);
1949
1950         if (dc->fb) {
1951                 tegra_fb_unregister(dc->fb);
1952                 if (dc->fb_mem)
1953                         release_resource(dc->fb_mem);
1954         }
1955
1956         tegra_dc_ext_disable(dc->ext);
1957
1958         if (dc->ext)
1959                 tegra_dc_ext_unregister(dc->ext);
1960
1961         if (dc->enabled)
1962                 _tegra_dc_disable(dc);
1963
1964 #ifdef CONFIG_SWITCH
1965         switch_dev_unregister(&dc->modeset_switch);
1966 #endif
1967         free_irq(dc->irq, dc);
1968         clk_put(dc->emc_clk);
1969         clk_put(dc->clk);
1970         iounmap(dc->base);
1971         if (dc->fb_mem)
1972                 release_resource(dc->base_res);
1973         kfree(dc);
1974         tegra_dc_set(NULL, ndev->id);
1975         return 0;
1976 }
1977
1978 #ifdef CONFIG_PM
1979 static int tegra_dc_suspend(struct nvhost_device *ndev, pm_message_t state)
1980 {
1981         struct tegra_dc *dc = nvhost_get_drvdata(ndev);
1982
1983         trace_printk("%s:suspend\n", dc->ndev->name);
1984         dev_info(&ndev->dev, "suspend\n");
1985
1986         tegra_dc_ext_disable(dc->ext);
1987
1988         mutex_lock(&dc->lock);
1989
1990         if (dc->out_ops && dc->out_ops->suspend)
1991                 dc->out_ops->suspend(dc);
1992
1993         if (dc->enabled) {
1994                 _tegra_dc_disable(dc);
1995
1996                 dc->suspended = true;
1997         }
1998
1999         if (dc->out && dc->out->postsuspend) {
2000                 dc->out->postsuspend();
2001                 if (dc->out->type && dc->out->type == TEGRA_DC_OUT_HDMI)
2002                         /*
2003                          * avoid resume event due to voltage falling
2004                          */
2005                         msleep(100);
2006         }
2007
2008         mutex_unlock(&dc->lock);
2009
2010         return 0;
2011 }
2012
2013 static int tegra_dc_resume(struct nvhost_device *ndev)
2014 {
2015         struct tegra_dc *dc = nvhost_get_drvdata(ndev);
2016
2017         trace_printk("%s:resume\n", dc->ndev->name);
2018         dev_info(&ndev->dev, "resume\n");
2019
2020         mutex_lock(&dc->lock);
2021         dc->suspended = false;
2022
2023         if (dc->enabled)
2024                 _tegra_dc_enable(dc);
2025
2026         if (dc->out && dc->out->hotplug_init)
2027                 dc->out->hotplug_init();
2028
2029         if (dc->out_ops && dc->out_ops->resume)
2030                 dc->out_ops->resume(dc);
2031         mutex_unlock(&dc->lock);
2032
2033         return 0;
2034 }
2035
2036 #endif /* CONFIG_PM */
2037
2038 static void tegra_dc_shutdown(struct nvhost_device *ndev)
2039 {
2040         struct tegra_dc *dc = nvhost_get_drvdata(ndev);
2041
2042         if (!dc || !dc->enabled)
2043                 return;
2044
2045         tegra_dc_blank(dc);
2046         tegra_dc_disable(dc);
2047 }
2048
2049 extern int suspend_set(const char *val, struct kernel_param *kp)
2050 {
2051         if (!strcmp(val, "dump"))
2052                 dump_regs(tegra_dcs[0]);
2053 #ifdef CONFIG_PM
2054         else if (!strcmp(val, "suspend"))
2055                 tegra_dc_suspend(tegra_dcs[0]->ndev, PMSG_SUSPEND);
2056         else if (!strcmp(val, "resume"))
2057                 tegra_dc_resume(tegra_dcs[0]->ndev);
2058 #endif
2059
2060         return 0;
2061 }
2062
2063 extern int suspend_get(char *buffer, struct kernel_param *kp)
2064 {
2065         return 0;
2066 }
2067
2068 int suspend;
2069
2070 module_param_call(suspend, suspend_set, suspend_get, &suspend, 0644);
2071
2072 struct nvhost_driver tegra_dc_driver = {
2073         .driver = {
2074                 .name = "tegradc",
2075                 .owner = THIS_MODULE,
2076         },
2077         .probe = tegra_dc_probe,
2078         .remove = tegra_dc_remove,
2079 #ifdef CONFIG_PM
2080         .suspend = tegra_dc_suspend,
2081         .resume = tegra_dc_resume,
2082 #endif
2083         .shutdown = tegra_dc_shutdown,
2084 };
2085
2086 #ifndef MODULE
2087 static int __init parse_disp_params(char *options, struct tegra_dc_mode *mode)
2088 {
2089         int i, params[11];
2090         char *p;
2091
2092         for (i = 0; i < ARRAY_SIZE(params); i++) {
2093                 if ((p = strsep(&options, ",")) != NULL) {
2094                         if (*p)
2095                                 params[i] = simple_strtoul(p, &p, 10);
2096                 } else
2097                         return -EINVAL;
2098         }
2099
2100         if ((mode->pclk = params[0]) == 0)
2101                 return -EINVAL;
2102
2103         mode->h_active      = params[1];
2104         mode->v_active      = params[2];
2105         mode->h_ref_to_sync = params[3];
2106         mode->v_ref_to_sync = params[4];
2107         mode->h_sync_width  = params[5];
2108         mode->v_sync_width  = params[6];
2109         mode->h_back_porch  = params[7];
2110         mode->v_back_porch  = params[8];
2111         mode->h_front_porch = params[9];
2112         mode->v_front_porch = params[10];
2113
2114         return 0;
2115 }
2116
2117 static int __init tegra_dc_mode_override(char *str)
2118 {
2119         char *p = str, *options;
2120
2121         if (!p || !*p)
2122                 return -EINVAL;
2123
2124         p = strstr(str, "hdmi:");
2125         if (p) {
2126                 p += 5;
2127                 options = strsep(&p, ";");
2128                 if (parse_disp_params(options, &override_disp_mode[TEGRA_DC_OUT_HDMI]))
2129                         return -EINVAL;
2130         }
2131
2132         p = strstr(str, "rgb:");
2133         if (p) {
2134                 p += 4;
2135                 options = strsep(&p, ";");
2136                 if (parse_disp_params(options, &override_disp_mode[TEGRA_DC_OUT_RGB]))
2137                         return -EINVAL;
2138         }
2139
2140         p = strstr(str, "dsi:");
2141         if (p) {
2142                 p += 4;
2143                 options = strsep(&p, ";");
2144                 if (parse_disp_params(options, &override_disp_mode[TEGRA_DC_OUT_DSI]))
2145                         return -EINVAL;
2146         }
2147
2148         return 0;
2149 }
2150
2151 __setup("disp_params=", tegra_dc_mode_override);
2152 #endif
2153
2154 static int __init tegra_dc_module_init(void)
2155 {
2156         int ret = tegra_dc_ext_module_init();
2157         if (ret)
2158                 return ret;
2159         return nvhost_driver_register(&tegra_dc_driver);
2160 }
2161
2162 static void __exit tegra_dc_module_exit(void)
2163 {
2164         nvhost_driver_unregister(&tegra_dc_driver);
2165         tegra_dc_ext_module_exit();
2166 }
2167
2168 module_exit(tegra_dc_module_exit);
2169 module_init(tegra_dc_module_init);