OMAPDSS: DISPC: cleanup lcd/digit enable/disable
[linux-omap-dss2:linux.git] / drivers / video / omap2 / dss / dispc.c
1 /*
2  * linux/drivers/video/omap2/dss/dispc.c
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6  *
7  * Some code and ideas taken from drivers/video/omap/ driver
8  * by Imre Deak.
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License version 2 as published by
12  * the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #define DSS_SUBSYS_NAME "DISPC"
24
25 #include <linux/kernel.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/vmalloc.h>
28 #include <linux/export.h>
29 #include <linux/clk.h>
30 #include <linux/io.h>
31 #include <linux/jiffies.h>
32 #include <linux/seq_file.h>
33 #include <linux/delay.h>
34 #include <linux/workqueue.h>
35 #include <linux/hardirq.h>
36 #include <linux/interrupt.h>
37 #include <linux/platform_device.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/sizes.h>
40
41 #include <video/omapdss.h>
42
43 #include "dss.h"
44 #include "dss_features.h"
45 #include "dispc.h"
46
47 /* DISPC */
48 #define DISPC_SZ_REGS                   SZ_4K
49
50 #define DISPC_IRQ_MASK_ERROR            (DISPC_IRQ_GFX_FIFO_UNDERFLOW | \
51                                          DISPC_IRQ_OCP_ERR | \
52                                          DISPC_IRQ_VID1_FIFO_UNDERFLOW | \
53                                          DISPC_IRQ_VID2_FIFO_UNDERFLOW | \
54                                          DISPC_IRQ_SYNC_LOST | \
55                                          DISPC_IRQ_SYNC_LOST_DIGIT)
56
57 #define DISPC_MAX_NR_ISRS               8
58
59 struct omap_dispc_isr_data {
60         omap_dispc_isr_t        isr;
61         void                    *arg;
62         u32                     mask;
63 };
64
65 enum omap_burst_size {
66         BURST_SIZE_X2 = 0,
67         BURST_SIZE_X4 = 1,
68         BURST_SIZE_X8 = 2,
69 };
70
71 #define REG_GET(idx, start, end) \
72         FLD_GET(dispc_read_reg(idx), start, end)
73
74 #define REG_FLD_MOD(idx, val, start, end)                               \
75         dispc_write_reg(idx, FLD_MOD(dispc_read_reg(idx), val, start, end))
76
77 struct dispc_irq_stats {
78         unsigned long last_reset;
79         unsigned irq_count;
80         unsigned irqs[32];
81 };
82
83 struct dispc_features {
84         u8 sw_start;
85         u8 fp_start;
86         u8 bp_start;
87         u16 sw_max;
88         u16 vp_max;
89         u16 hp_max;
90         int (*calc_scaling) (enum omap_plane plane,
91                 const struct omap_video_timings *mgr_timings,
92                 u16 width, u16 height, u16 out_width, u16 out_height,
93                 enum omap_color_mode color_mode, bool *five_taps,
94                 int *x_predecim, int *y_predecim, int *decim_x, int *decim_y,
95                 u16 pos_x, unsigned long *core_clk, bool mem_to_mem);
96         unsigned long (*calc_core_clk) (enum omap_plane plane,
97                 u16 width, u16 height, u16 out_width, u16 out_height,
98                 bool mem_to_mem);
99         u8 num_fifos;
100
101         /* swap GFX & WB fifos */
102         bool gfx_fifo_workaround:1;
103 };
104
105 #define DISPC_MAX_NR_FIFOS 5
106
107 static struct {
108         struct platform_device *pdev;
109         void __iomem    *base;
110
111         int             ctx_loss_cnt;
112
113         int irq;
114         struct clk *dss_clk;
115
116         u32 fifo_size[DISPC_MAX_NR_FIFOS];
117         /* maps which plane is using a fifo. fifo-id -> plane-id */
118         int fifo_assignment[DISPC_MAX_NR_FIFOS];
119
120         spinlock_t irq_lock;
121         u32 irq_error_mask;
122         struct omap_dispc_isr_data registered_isr[DISPC_MAX_NR_ISRS];
123         u32 error_irqs;
124         struct work_struct error_work;
125
126         bool            ctx_valid;
127         u32             ctx[DISPC_SZ_REGS / sizeof(u32)];
128
129         const struct dispc_features *feat;
130
131 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
132         spinlock_t irq_stats_lock;
133         struct dispc_irq_stats irq_stats;
134 #endif
135 } dispc;
136
137 enum omap_color_component {
138         /* used for all color formats for OMAP3 and earlier
139          * and for RGB and Y color component on OMAP4
140          */
141         DISPC_COLOR_COMPONENT_RGB_Y             = 1 << 0,
142         /* used for UV component for
143          * OMAP_DSS_COLOR_YUV2, OMAP_DSS_COLOR_UYVY, OMAP_DSS_COLOR_NV12
144          * color formats on OMAP4
145          */
146         DISPC_COLOR_COMPONENT_UV                = 1 << 1,
147 };
148
149 enum mgr_reg_fields {
150         DISPC_MGR_FLD_ENABLE,
151         DISPC_MGR_FLD_STNTFT,
152         DISPC_MGR_FLD_GO,
153         DISPC_MGR_FLD_TFTDATALINES,
154         DISPC_MGR_FLD_STALLMODE,
155         DISPC_MGR_FLD_TCKENABLE,
156         DISPC_MGR_FLD_TCKSELECTION,
157         DISPC_MGR_FLD_CPR,
158         DISPC_MGR_FLD_FIFOHANDCHECK,
159         /* used to maintain a count of the above fields */
160         DISPC_MGR_FLD_NUM,
161 };
162
163 static const struct {
164         const char *name;
165         u32 vsync_irq;
166         u32 framedone_irq;
167         u32 sync_lost_irq;
168         struct reg_field reg_desc[DISPC_MGR_FLD_NUM];
169 } mgr_desc[] = {
170         [OMAP_DSS_CHANNEL_LCD] = {
171                 .name           = "LCD",
172                 .vsync_irq      = DISPC_IRQ_VSYNC,
173                 .framedone_irq  = DISPC_IRQ_FRAMEDONE,
174                 .sync_lost_irq  = DISPC_IRQ_SYNC_LOST,
175                 .reg_desc       = {
176                         [DISPC_MGR_FLD_ENABLE]          = { DISPC_CONTROL,  0,  0 },
177                         [DISPC_MGR_FLD_STNTFT]          = { DISPC_CONTROL,  3,  3 },
178                         [DISPC_MGR_FLD_GO]              = { DISPC_CONTROL,  5,  5 },
179                         [DISPC_MGR_FLD_TFTDATALINES]    = { DISPC_CONTROL,  9,  8 },
180                         [DISPC_MGR_FLD_STALLMODE]       = { DISPC_CONTROL, 11, 11 },
181                         [DISPC_MGR_FLD_TCKENABLE]       = { DISPC_CONFIG,  10, 10 },
182                         [DISPC_MGR_FLD_TCKSELECTION]    = { DISPC_CONFIG,  11, 11 },
183                         [DISPC_MGR_FLD_CPR]             = { DISPC_CONFIG,  15, 15 },
184                         [DISPC_MGR_FLD_FIFOHANDCHECK]   = { DISPC_CONFIG,  16, 16 },
185                 },
186         },
187         [OMAP_DSS_CHANNEL_DIGIT] = {
188                 .name           = "DIGIT",
189                 .vsync_irq      = DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_EVSYNC_EVEN,
190                 .framedone_irq  = 0,
191                 .sync_lost_irq  = DISPC_IRQ_SYNC_LOST_DIGIT,
192                 .reg_desc       = {
193                         [DISPC_MGR_FLD_ENABLE]          = { DISPC_CONTROL,  1,  1 },
194                         [DISPC_MGR_FLD_STNTFT]          = { },
195                         [DISPC_MGR_FLD_GO]              = { DISPC_CONTROL,  6,  6 },
196                         [DISPC_MGR_FLD_TFTDATALINES]    = { },
197                         [DISPC_MGR_FLD_STALLMODE]       = { },
198                         [DISPC_MGR_FLD_TCKENABLE]       = { DISPC_CONFIG,  12, 12 },
199                         [DISPC_MGR_FLD_TCKSELECTION]    = { DISPC_CONFIG,  13, 13 },
200                         [DISPC_MGR_FLD_CPR]             = { },
201                         [DISPC_MGR_FLD_FIFOHANDCHECK]   = { DISPC_CONFIG,  16, 16 },
202                 },
203         },
204         [OMAP_DSS_CHANNEL_LCD2] = {
205                 .name           = "LCD2",
206                 .vsync_irq      = DISPC_IRQ_VSYNC2,
207                 .framedone_irq  = DISPC_IRQ_FRAMEDONE2,
208                 .sync_lost_irq  = DISPC_IRQ_SYNC_LOST2,
209                 .reg_desc       = {
210                         [DISPC_MGR_FLD_ENABLE]          = { DISPC_CONTROL2,  0,  0 },
211                         [DISPC_MGR_FLD_STNTFT]          = { DISPC_CONTROL2,  3,  3 },
212                         [DISPC_MGR_FLD_GO]              = { DISPC_CONTROL2,  5,  5 },
213                         [DISPC_MGR_FLD_TFTDATALINES]    = { DISPC_CONTROL2,  9,  8 },
214                         [DISPC_MGR_FLD_STALLMODE]       = { DISPC_CONTROL2, 11, 11 },
215                         [DISPC_MGR_FLD_TCKENABLE]       = { DISPC_CONFIG2,  10, 10 },
216                         [DISPC_MGR_FLD_TCKSELECTION]    = { DISPC_CONFIG2,  11, 11 },
217                         [DISPC_MGR_FLD_CPR]             = { DISPC_CONFIG2,  15, 15 },
218                         [DISPC_MGR_FLD_FIFOHANDCHECK]   = { DISPC_CONFIG2,  16, 16 },
219                 },
220         },
221         [OMAP_DSS_CHANNEL_LCD3] = {
222                 .name           = "LCD3",
223                 .vsync_irq      = DISPC_IRQ_VSYNC3,
224                 .framedone_irq  = DISPC_IRQ_FRAMEDONE3,
225                 .sync_lost_irq  = DISPC_IRQ_SYNC_LOST3,
226                 .reg_desc       = {
227                         [DISPC_MGR_FLD_ENABLE]          = { DISPC_CONTROL3,  0,  0 },
228                         [DISPC_MGR_FLD_STNTFT]          = { DISPC_CONTROL3,  3,  3 },
229                         [DISPC_MGR_FLD_GO]              = { DISPC_CONTROL3,  5,  5 },
230                         [DISPC_MGR_FLD_TFTDATALINES]    = { DISPC_CONTROL3,  9,  8 },
231                         [DISPC_MGR_FLD_STALLMODE]       = { DISPC_CONTROL3, 11, 11 },
232                         [DISPC_MGR_FLD_TCKENABLE]       = { DISPC_CONFIG3,  10, 10 },
233                         [DISPC_MGR_FLD_TCKSELECTION]    = { DISPC_CONFIG3,  11, 11 },
234                         [DISPC_MGR_FLD_CPR]             = { DISPC_CONFIG3,  15, 15 },
235                         [DISPC_MGR_FLD_FIFOHANDCHECK]   = { DISPC_CONFIG3,  16, 16 },
236                 },
237         },
238 };
239
240 struct color_conv_coef {
241         int ry, rcr, rcb, gy, gcr, gcb, by, bcr, bcb;
242         int full_range;
243 };
244
245 static void _omap_dispc_set_irqs(void);
246 static unsigned long dispc_plane_pclk_rate(enum omap_plane plane);
247 static unsigned long dispc_plane_lclk_rate(enum omap_plane plane);
248
249 static inline void dispc_write_reg(const u16 idx, u32 val)
250 {
251         __raw_writel(val, dispc.base + idx);
252 }
253
254 static inline u32 dispc_read_reg(const u16 idx)
255 {
256         return __raw_readl(dispc.base + idx);
257 }
258
259 static u32 mgr_fld_read(enum omap_channel channel, enum mgr_reg_fields regfld)
260 {
261         const struct reg_field rfld = mgr_desc[channel].reg_desc[regfld];
262         return REG_GET(rfld.reg, rfld.high, rfld.low);
263 }
264
265 static void mgr_fld_write(enum omap_channel channel,
266                                         enum mgr_reg_fields regfld, int val) {
267         const struct reg_field rfld = mgr_desc[channel].reg_desc[regfld];
268         REG_FLD_MOD(rfld.reg, val, rfld.high, rfld.low);
269 }
270
271 #define SR(reg) \
272         dispc.ctx[DISPC_##reg / sizeof(u32)] = dispc_read_reg(DISPC_##reg)
273 #define RR(reg) \
274         dispc_write_reg(DISPC_##reg, dispc.ctx[DISPC_##reg / sizeof(u32)])
275
276 static void dispc_save_context(void)
277 {
278         int i, j;
279
280         DSSDBG("dispc_save_context\n");
281
282         SR(IRQENABLE);
283         SR(CONTROL);
284         SR(CONFIG);
285         SR(LINE_NUMBER);
286         if (dss_has_feature(FEAT_ALPHA_FIXED_ZORDER) ||
287                         dss_has_feature(FEAT_ALPHA_FREE_ZORDER))
288                 SR(GLOBAL_ALPHA);
289         if (dss_has_feature(FEAT_MGR_LCD2)) {
290                 SR(CONTROL2);
291                 SR(CONFIG2);
292         }
293         if (dss_has_feature(FEAT_MGR_LCD3)) {
294                 SR(CONTROL3);
295                 SR(CONFIG3);
296         }
297
298         for (i = 0; i < dss_feat_get_num_mgrs(); i++) {
299                 SR(DEFAULT_COLOR(i));
300                 SR(TRANS_COLOR(i));
301                 SR(SIZE_MGR(i));
302                 if (i == OMAP_DSS_CHANNEL_DIGIT)
303                         continue;
304                 SR(TIMING_H(i));
305                 SR(TIMING_V(i));
306                 SR(POL_FREQ(i));
307                 SR(DIVISORo(i));
308
309                 SR(DATA_CYCLE1(i));
310                 SR(DATA_CYCLE2(i));
311                 SR(DATA_CYCLE3(i));
312
313                 if (dss_has_feature(FEAT_CPR)) {
314                         SR(CPR_COEF_R(i));
315                         SR(CPR_COEF_G(i));
316                         SR(CPR_COEF_B(i));
317                 }
318         }
319
320         for (i = 0; i < dss_feat_get_num_ovls(); i++) {
321                 SR(OVL_BA0(i));
322                 SR(OVL_BA1(i));
323                 SR(OVL_POSITION(i));
324                 SR(OVL_SIZE(i));
325                 SR(OVL_ATTRIBUTES(i));
326                 SR(OVL_FIFO_THRESHOLD(i));
327                 SR(OVL_ROW_INC(i));
328                 SR(OVL_PIXEL_INC(i));
329                 if (dss_has_feature(FEAT_PRELOAD))
330                         SR(OVL_PRELOAD(i));
331                 if (i == OMAP_DSS_GFX) {
332                         SR(OVL_WINDOW_SKIP(i));
333                         SR(OVL_TABLE_BA(i));
334                         continue;
335                 }
336                 SR(OVL_FIR(i));
337                 SR(OVL_PICTURE_SIZE(i));
338                 SR(OVL_ACCU0(i));
339                 SR(OVL_ACCU1(i));
340
341                 for (j = 0; j < 8; j++)
342                         SR(OVL_FIR_COEF_H(i, j));
343
344                 for (j = 0; j < 8; j++)
345                         SR(OVL_FIR_COEF_HV(i, j));
346
347                 for (j = 0; j < 5; j++)
348                         SR(OVL_CONV_COEF(i, j));
349
350                 if (dss_has_feature(FEAT_FIR_COEF_V)) {
351                         for (j = 0; j < 8; j++)
352                                 SR(OVL_FIR_COEF_V(i, j));
353                 }
354
355                 if (dss_has_feature(FEAT_HANDLE_UV_SEPARATE)) {
356                         SR(OVL_BA0_UV(i));
357                         SR(OVL_BA1_UV(i));
358                         SR(OVL_FIR2(i));
359                         SR(OVL_ACCU2_0(i));
360                         SR(OVL_ACCU2_1(i));
361
362                         for (j = 0; j < 8; j++)
363                                 SR(OVL_FIR_COEF_H2(i, j));
364
365                         for (j = 0; j < 8; j++)
366                                 SR(OVL_FIR_COEF_HV2(i, j));
367
368                         for (j = 0; j < 8; j++)
369                                 SR(OVL_FIR_COEF_V2(i, j));
370                 }
371                 if (dss_has_feature(FEAT_ATTR2))
372                         SR(OVL_ATTRIBUTES2(i));
373         }
374
375         if (dss_has_feature(FEAT_CORE_CLK_DIV))
376                 SR(DIVISOR);
377
378         dispc.ctx_loss_cnt = dss_get_ctx_loss_count(&dispc.pdev->dev);
379         dispc.ctx_valid = true;
380
381         DSSDBG("context saved, ctx_loss_count %d\n", dispc.ctx_loss_cnt);
382 }
383
384 static void dispc_restore_context(void)
385 {
386         int i, j, ctx;
387
388         DSSDBG("dispc_restore_context\n");
389
390         if (!dispc.ctx_valid)
391                 return;
392
393         ctx = dss_get_ctx_loss_count(&dispc.pdev->dev);
394
395         if (ctx >= 0 && ctx == dispc.ctx_loss_cnt)
396                 return;
397
398         DSSDBG("ctx_loss_count: saved %d, current %d\n",
399                         dispc.ctx_loss_cnt, ctx);
400
401         /*RR(IRQENABLE);*/
402         /*RR(CONTROL);*/
403         RR(CONFIG);
404         RR(LINE_NUMBER);
405         if (dss_has_feature(FEAT_ALPHA_FIXED_ZORDER) ||
406                         dss_has_feature(FEAT_ALPHA_FREE_ZORDER))
407                 RR(GLOBAL_ALPHA);
408         if (dss_has_feature(FEAT_MGR_LCD2))
409                 RR(CONFIG2);
410         if (dss_has_feature(FEAT_MGR_LCD3))
411                 RR(CONFIG3);
412
413         for (i = 0; i < dss_feat_get_num_mgrs(); i++) {
414                 RR(DEFAULT_COLOR(i));
415                 RR(TRANS_COLOR(i));
416                 RR(SIZE_MGR(i));
417                 if (i == OMAP_DSS_CHANNEL_DIGIT)
418                         continue;
419                 RR(TIMING_H(i));
420                 RR(TIMING_V(i));
421                 RR(POL_FREQ(i));
422                 RR(DIVISORo(i));
423
424                 RR(DATA_CYCLE1(i));
425                 RR(DATA_CYCLE2(i));
426                 RR(DATA_CYCLE3(i));
427
428                 if (dss_has_feature(FEAT_CPR)) {
429                         RR(CPR_COEF_R(i));
430                         RR(CPR_COEF_G(i));
431                         RR(CPR_COEF_B(i));
432                 }
433         }
434
435         for (i = 0; i < dss_feat_get_num_ovls(); i++) {
436                 RR(OVL_BA0(i));
437                 RR(OVL_BA1(i));
438                 RR(OVL_POSITION(i));
439                 RR(OVL_SIZE(i));
440                 RR(OVL_ATTRIBUTES(i));
441                 RR(OVL_FIFO_THRESHOLD(i));
442                 RR(OVL_ROW_INC(i));
443                 RR(OVL_PIXEL_INC(i));
444                 if (dss_has_feature(FEAT_PRELOAD))
445                         RR(OVL_PRELOAD(i));
446                 if (i == OMAP_DSS_GFX) {
447                         RR(OVL_WINDOW_SKIP(i));
448                         RR(OVL_TABLE_BA(i));
449                         continue;
450                 }
451                 RR(OVL_FIR(i));
452                 RR(OVL_PICTURE_SIZE(i));
453                 RR(OVL_ACCU0(i));
454                 RR(OVL_ACCU1(i));
455
456                 for (j = 0; j < 8; j++)
457                         RR(OVL_FIR_COEF_H(i, j));
458
459                 for (j = 0; j < 8; j++)
460                         RR(OVL_FIR_COEF_HV(i, j));
461
462                 for (j = 0; j < 5; j++)
463                         RR(OVL_CONV_COEF(i, j));
464
465                 if (dss_has_feature(FEAT_FIR_COEF_V)) {
466                         for (j = 0; j < 8; j++)
467                                 RR(OVL_FIR_COEF_V(i, j));
468                 }
469
470                 if (dss_has_feature(FEAT_HANDLE_UV_SEPARATE)) {
471                         RR(OVL_BA0_UV(i));
472                         RR(OVL_BA1_UV(i));
473                         RR(OVL_FIR2(i));
474                         RR(OVL_ACCU2_0(i));
475                         RR(OVL_ACCU2_1(i));
476
477                         for (j = 0; j < 8; j++)
478                                 RR(OVL_FIR_COEF_H2(i, j));
479
480                         for (j = 0; j < 8; j++)
481                                 RR(OVL_FIR_COEF_HV2(i, j));
482
483                         for (j = 0; j < 8; j++)
484                                 RR(OVL_FIR_COEF_V2(i, j));
485                 }
486                 if (dss_has_feature(FEAT_ATTR2))
487                         RR(OVL_ATTRIBUTES2(i));
488         }
489
490         if (dss_has_feature(FEAT_CORE_CLK_DIV))
491                 RR(DIVISOR);
492
493         /* enable last, because LCD & DIGIT enable are here */
494         RR(CONTROL);
495         if (dss_has_feature(FEAT_MGR_LCD2))
496                 RR(CONTROL2);
497         if (dss_has_feature(FEAT_MGR_LCD3))
498                 RR(CONTROL3);
499         /* clear spurious SYNC_LOST_DIGIT interrupts */
500         dispc_write_reg(DISPC_IRQSTATUS, DISPC_IRQ_SYNC_LOST_DIGIT);
501
502         /*
503          * enable last so IRQs won't trigger before
504          * the context is fully restored
505          */
506         RR(IRQENABLE);
507
508         DSSDBG("context restored\n");
509 }
510
511 #undef SR
512 #undef RR
513
514 int dispc_runtime_get(void)
515 {
516         int r;
517
518         DSSDBG("dispc_runtime_get\n");
519
520         r = pm_runtime_get_sync(&dispc.pdev->dev);
521         WARN_ON(r < 0);
522         return r < 0 ? r : 0;
523 }
524
525 void dispc_runtime_put(void)
526 {
527         int r;
528
529         DSSDBG("dispc_runtime_put\n");
530
531         r = pm_runtime_put_sync(&dispc.pdev->dev);
532         WARN_ON(r < 0 && r != -ENOSYS);
533 }
534
535 u32 dispc_mgr_get_vsync_irq(enum omap_channel channel)
536 {
537         return mgr_desc[channel].vsync_irq;
538 }
539
540 u32 dispc_mgr_get_framedone_irq(enum omap_channel channel)
541 {
542         return mgr_desc[channel].framedone_irq;
543 }
544
545 u32 dispc_mgr_get_sync_lost_irq(enum omap_channel channel)
546 {
547         return mgr_desc[channel].sync_lost_irq;
548 }
549
550 u32 dispc_wb_get_framedone_irq(void)
551 {
552         return DISPC_IRQ_FRAMEDONEWB;
553 }
554
555 bool dispc_mgr_go_busy(enum omap_channel channel)
556 {
557         return mgr_fld_read(channel, DISPC_MGR_FLD_GO) == 1;
558 }
559
560 void dispc_mgr_go(enum omap_channel channel)
561 {
562         bool enable_bit, go_bit;
563
564         /* if the channel is not enabled, we don't need GO */
565         enable_bit = mgr_fld_read(channel, DISPC_MGR_FLD_ENABLE) == 1;
566
567         if (!enable_bit)
568                 return;
569
570         go_bit = mgr_fld_read(channel, DISPC_MGR_FLD_GO) == 1;
571
572         if (go_bit) {
573                 DSSERR("GO bit not down for channel %d\n", channel);
574                 return;
575         }
576
577         DSSDBG("GO %s\n", mgr_desc[channel].name);
578
579         mgr_fld_write(channel, DISPC_MGR_FLD_GO, 1);
580 }
581
582 bool dispc_wb_go_busy(void)
583 {
584         return REG_GET(DISPC_CONTROL2, 6, 6) == 1;
585 }
586
587 void dispc_wb_go(void)
588 {
589         enum omap_plane plane = OMAP_DSS_WB;
590         bool enable, go;
591
592         enable = REG_GET(DISPC_OVL_ATTRIBUTES(plane), 0, 0) == 1;
593
594         if (!enable)
595                 return;
596
597         go = REG_GET(DISPC_CONTROL2, 6, 6) == 1;
598         if (go) {
599                 DSSERR("GO bit not down for WB\n");
600                 return;
601         }
602
603         REG_FLD_MOD(DISPC_CONTROL2, 1, 6, 6);
604 }
605
606 static void dispc_ovl_write_firh_reg(enum omap_plane plane, int reg, u32 value)
607 {
608         dispc_write_reg(DISPC_OVL_FIR_COEF_H(plane, reg), value);
609 }
610
611 static void dispc_ovl_write_firhv_reg(enum omap_plane plane, int reg, u32 value)
612 {
613         dispc_write_reg(DISPC_OVL_FIR_COEF_HV(plane, reg), value);
614 }
615
616 static void dispc_ovl_write_firv_reg(enum omap_plane plane, int reg, u32 value)
617 {
618         dispc_write_reg(DISPC_OVL_FIR_COEF_V(plane, reg), value);
619 }
620
621 static void dispc_ovl_write_firh2_reg(enum omap_plane plane, int reg, u32 value)
622 {
623         BUG_ON(plane == OMAP_DSS_GFX);
624
625         dispc_write_reg(DISPC_OVL_FIR_COEF_H2(plane, reg), value);
626 }
627
628 static void dispc_ovl_write_firhv2_reg(enum omap_plane plane, int reg,
629                 u32 value)
630 {
631         BUG_ON(plane == OMAP_DSS_GFX);
632
633         dispc_write_reg(DISPC_OVL_FIR_COEF_HV2(plane, reg), value);
634 }
635
636 static void dispc_ovl_write_firv2_reg(enum omap_plane plane, int reg, u32 value)
637 {
638         BUG_ON(plane == OMAP_DSS_GFX);
639
640         dispc_write_reg(DISPC_OVL_FIR_COEF_V2(plane, reg), value);
641 }
642
643 static void dispc_ovl_set_scale_coef(enum omap_plane plane, int fir_hinc,
644                                 int fir_vinc, int five_taps,
645                                 enum omap_color_component color_comp)
646 {
647         const struct dispc_coef *h_coef, *v_coef;
648         int i;
649
650         h_coef = dispc_ovl_get_scale_coef(fir_hinc, true);
651         v_coef = dispc_ovl_get_scale_coef(fir_vinc, five_taps);
652
653         for (i = 0; i < 8; i++) {
654                 u32 h, hv;
655
656                 h = FLD_VAL(h_coef[i].hc0_vc00, 7, 0)
657                         | FLD_VAL(h_coef[i].hc1_vc0, 15, 8)
658                         | FLD_VAL(h_coef[i].hc2_vc1, 23, 16)
659                         | FLD_VAL(h_coef[i].hc3_vc2, 31, 24);
660                 hv = FLD_VAL(h_coef[i].hc4_vc22, 7, 0)
661                         | FLD_VAL(v_coef[i].hc1_vc0, 15, 8)
662                         | FLD_VAL(v_coef[i].hc2_vc1, 23, 16)
663                         | FLD_VAL(v_coef[i].hc3_vc2, 31, 24);
664
665                 if (color_comp == DISPC_COLOR_COMPONENT_RGB_Y) {
666                         dispc_ovl_write_firh_reg(plane, i, h);
667                         dispc_ovl_write_firhv_reg(plane, i, hv);
668                 } else {
669                         dispc_ovl_write_firh2_reg(plane, i, h);
670                         dispc_ovl_write_firhv2_reg(plane, i, hv);
671                 }
672
673         }
674
675         if (five_taps) {
676                 for (i = 0; i < 8; i++) {
677                         u32 v;
678                         v = FLD_VAL(v_coef[i].hc0_vc00, 7, 0)
679                                 | FLD_VAL(v_coef[i].hc4_vc22, 15, 8);
680                         if (color_comp == DISPC_COLOR_COMPONENT_RGB_Y)
681                                 dispc_ovl_write_firv_reg(plane, i, v);
682                         else
683                                 dispc_ovl_write_firv2_reg(plane, i, v);
684                 }
685         }
686 }
687
688
689 static void dispc_ovl_write_color_conv_coef(enum omap_plane plane,
690                 const struct color_conv_coef *ct)
691 {
692 #define CVAL(x, y) (FLD_VAL(x, 26, 16) | FLD_VAL(y, 10, 0))
693
694         dispc_write_reg(DISPC_OVL_CONV_COEF(plane, 0), CVAL(ct->rcr, ct->ry));
695         dispc_write_reg(DISPC_OVL_CONV_COEF(plane, 1), CVAL(ct->gy,  ct->rcb));
696         dispc_write_reg(DISPC_OVL_CONV_COEF(plane, 2), CVAL(ct->gcb, ct->gcr));
697         dispc_write_reg(DISPC_OVL_CONV_COEF(plane, 3), CVAL(ct->bcr, ct->by));
698         dispc_write_reg(DISPC_OVL_CONV_COEF(plane, 4), CVAL(0, ct->bcb));
699
700         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), ct->full_range, 11, 11);
701
702 #undef CVAL
703 }
704
705 static void dispc_setup_color_conv_coef(void)
706 {
707         int i;
708         int num_ovl = dss_feat_get_num_ovls();
709         int num_wb = dss_feat_get_num_wbs();
710         const struct color_conv_coef ctbl_bt601_5_ovl = {
711                 298, 409, 0, 298, -208, -100, 298, 0, 517, 0,
712         };
713         const struct color_conv_coef ctbl_bt601_5_wb = {
714                 66, 112, -38, 129, -94, -74, 25, -18, 112, 0,
715         };
716
717         for (i = 1; i < num_ovl; i++)
718                 dispc_ovl_write_color_conv_coef(i, &ctbl_bt601_5_ovl);
719
720         for (; i < num_wb; i++)
721                 dispc_ovl_write_color_conv_coef(i, &ctbl_bt601_5_wb);
722 }
723
724 static void dispc_ovl_set_ba0(enum omap_plane plane, u32 paddr)
725 {
726         dispc_write_reg(DISPC_OVL_BA0(plane), paddr);
727 }
728
729 static void dispc_ovl_set_ba1(enum omap_plane plane, u32 paddr)
730 {
731         dispc_write_reg(DISPC_OVL_BA1(plane), paddr);
732 }
733
734 static void dispc_ovl_set_ba0_uv(enum omap_plane plane, u32 paddr)
735 {
736         dispc_write_reg(DISPC_OVL_BA0_UV(plane), paddr);
737 }
738
739 static void dispc_ovl_set_ba1_uv(enum omap_plane plane, u32 paddr)
740 {
741         dispc_write_reg(DISPC_OVL_BA1_UV(plane), paddr);
742 }
743
744 static void dispc_ovl_set_pos(enum omap_plane plane,
745                 enum omap_overlay_caps caps, int x, int y)
746 {
747         u32 val;
748
749         if ((caps & OMAP_DSS_OVL_CAP_POS) == 0)
750                 return;
751
752         val = FLD_VAL(y, 26, 16) | FLD_VAL(x, 10, 0);
753
754         dispc_write_reg(DISPC_OVL_POSITION(plane), val);
755 }
756
757 static void dispc_ovl_set_input_size(enum omap_plane plane, int width,
758                 int height)
759 {
760         u32 val = FLD_VAL(height - 1, 26, 16) | FLD_VAL(width - 1, 10, 0);
761
762         if (plane == OMAP_DSS_GFX || plane == OMAP_DSS_WB)
763                 dispc_write_reg(DISPC_OVL_SIZE(plane), val);
764         else
765                 dispc_write_reg(DISPC_OVL_PICTURE_SIZE(plane), val);
766 }
767
768 static void dispc_ovl_set_output_size(enum omap_plane plane, int width,
769                 int height)
770 {
771         u32 val;
772
773         BUG_ON(plane == OMAP_DSS_GFX);
774
775         val = FLD_VAL(height - 1, 26, 16) | FLD_VAL(width - 1, 10, 0);
776
777         if (plane == OMAP_DSS_WB)
778                 dispc_write_reg(DISPC_OVL_PICTURE_SIZE(plane), val);
779         else
780                 dispc_write_reg(DISPC_OVL_SIZE(plane), val);
781 }
782
783 static void dispc_ovl_set_zorder(enum omap_plane plane,
784                 enum omap_overlay_caps caps, u8 zorder)
785 {
786         if ((caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
787                 return;
788
789         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), zorder, 27, 26);
790 }
791
792 static void dispc_ovl_enable_zorder_planes(void)
793 {
794         int i;
795
796         if (!dss_has_feature(FEAT_ALPHA_FREE_ZORDER))
797                 return;
798
799         for (i = 0; i < dss_feat_get_num_ovls(); i++)
800                 REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(i), 1, 25, 25);
801 }
802
803 static void dispc_ovl_set_pre_mult_alpha(enum omap_plane plane,
804                 enum omap_overlay_caps caps, bool enable)
805 {
806         if ((caps & OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA) == 0)
807                 return;
808
809         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), enable ? 1 : 0, 28, 28);
810 }
811
812 static void dispc_ovl_setup_global_alpha(enum omap_plane plane,
813                 enum omap_overlay_caps caps, u8 global_alpha)
814 {
815         static const unsigned shifts[] = { 0, 8, 16, 24, };
816         int shift;
817
818         if ((caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
819                 return;
820
821         shift = shifts[plane];
822         REG_FLD_MOD(DISPC_GLOBAL_ALPHA, global_alpha, shift + 7, shift);
823 }
824
825 static void dispc_ovl_set_pix_inc(enum omap_plane plane, s32 inc)
826 {
827         dispc_write_reg(DISPC_OVL_PIXEL_INC(plane), inc);
828 }
829
830 static void dispc_ovl_set_row_inc(enum omap_plane plane, s32 inc)
831 {
832         dispc_write_reg(DISPC_OVL_ROW_INC(plane), inc);
833 }
834
835 static void dispc_ovl_set_color_mode(enum omap_plane plane,
836                 enum omap_color_mode color_mode)
837 {
838         u32 m = 0;
839         if (plane != OMAP_DSS_GFX) {
840                 switch (color_mode) {
841                 case OMAP_DSS_COLOR_NV12:
842                         m = 0x0; break;
843                 case OMAP_DSS_COLOR_RGBX16:
844                         m = 0x1; break;
845                 case OMAP_DSS_COLOR_RGBA16:
846                         m = 0x2; break;
847                 case OMAP_DSS_COLOR_RGB12U:
848                         m = 0x4; break;
849                 case OMAP_DSS_COLOR_ARGB16:
850                         m = 0x5; break;
851                 case OMAP_DSS_COLOR_RGB16:
852                         m = 0x6; break;
853                 case OMAP_DSS_COLOR_ARGB16_1555:
854                         m = 0x7; break;
855                 case OMAP_DSS_COLOR_RGB24U:
856                         m = 0x8; break;
857                 case OMAP_DSS_COLOR_RGB24P:
858                         m = 0x9; break;
859                 case OMAP_DSS_COLOR_YUV2:
860                         m = 0xa; break;
861                 case OMAP_DSS_COLOR_UYVY:
862                         m = 0xb; break;
863                 case OMAP_DSS_COLOR_ARGB32:
864                         m = 0xc; break;
865                 case OMAP_DSS_COLOR_RGBA32:
866                         m = 0xd; break;
867                 case OMAP_DSS_COLOR_RGBX32:
868                         m = 0xe; break;
869                 case OMAP_DSS_COLOR_XRGB16_1555:
870                         m = 0xf; break;
871                 default:
872                         BUG(); return;
873                 }
874         } else {
875                 switch (color_mode) {
876                 case OMAP_DSS_COLOR_CLUT1:
877                         m = 0x0; break;
878                 case OMAP_DSS_COLOR_CLUT2:
879                         m = 0x1; break;
880                 case OMAP_DSS_COLOR_CLUT4:
881                         m = 0x2; break;
882                 case OMAP_DSS_COLOR_CLUT8:
883                         m = 0x3; break;
884                 case OMAP_DSS_COLOR_RGB12U:
885                         m = 0x4; break;
886                 case OMAP_DSS_COLOR_ARGB16:
887                         m = 0x5; break;
888                 case OMAP_DSS_COLOR_RGB16:
889                         m = 0x6; break;
890                 case OMAP_DSS_COLOR_ARGB16_1555:
891                         m = 0x7; break;
892                 case OMAP_DSS_COLOR_RGB24U:
893                         m = 0x8; break;
894                 case OMAP_DSS_COLOR_RGB24P:
895                         m = 0x9; break;
896                 case OMAP_DSS_COLOR_RGBX16:
897                         m = 0xa; break;
898                 case OMAP_DSS_COLOR_RGBA16:
899                         m = 0xb; break;
900                 case OMAP_DSS_COLOR_ARGB32:
901                         m = 0xc; break;
902                 case OMAP_DSS_COLOR_RGBA32:
903                         m = 0xd; break;
904                 case OMAP_DSS_COLOR_RGBX32:
905                         m = 0xe; break;
906                 case OMAP_DSS_COLOR_XRGB16_1555:
907                         m = 0xf; break;
908                 default:
909                         BUG(); return;
910                 }
911         }
912
913         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), m, 4, 1);
914 }
915
916 static void dispc_ovl_configure_burst_type(enum omap_plane plane,
917                 enum omap_dss_rotation_type rotation_type)
918 {
919         if (dss_has_feature(FEAT_BURST_2D) == 0)
920                 return;
921
922         if (rotation_type == OMAP_DSS_ROT_TILER)
923                 REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), 1, 29, 29);
924         else
925                 REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), 0, 29, 29);
926 }
927
928 void dispc_ovl_set_channel_out(enum omap_plane plane, enum omap_channel channel)
929 {
930         int shift;
931         u32 val;
932         int chan = 0, chan2 = 0;
933
934         switch (plane) {
935         case OMAP_DSS_GFX:
936                 shift = 8;
937                 break;
938         case OMAP_DSS_VIDEO1:
939         case OMAP_DSS_VIDEO2:
940         case OMAP_DSS_VIDEO3:
941                 shift = 16;
942                 break;
943         default:
944                 BUG();
945                 return;
946         }
947
948         val = dispc_read_reg(DISPC_OVL_ATTRIBUTES(plane));
949         if (dss_has_feature(FEAT_MGR_LCD2)) {
950                 switch (channel) {
951                 case OMAP_DSS_CHANNEL_LCD:
952                         chan = 0;
953                         chan2 = 0;
954                         break;
955                 case OMAP_DSS_CHANNEL_DIGIT:
956                         chan = 1;
957                         chan2 = 0;
958                         break;
959                 case OMAP_DSS_CHANNEL_LCD2:
960                         chan = 0;
961                         chan2 = 1;
962                         break;
963                 case OMAP_DSS_CHANNEL_LCD3:
964                         if (dss_has_feature(FEAT_MGR_LCD3)) {
965                                 chan = 0;
966                                 chan2 = 2;
967                         } else {
968                                 BUG();
969                                 return;
970                         }
971                         break;
972                 default:
973                         BUG();
974                         return;
975                 }
976
977                 val = FLD_MOD(val, chan, shift, shift);
978                 val = FLD_MOD(val, chan2, 31, 30);
979         } else {
980                 val = FLD_MOD(val, channel, shift, shift);
981         }
982         dispc_write_reg(DISPC_OVL_ATTRIBUTES(plane), val);
983 }
984
985 static enum omap_channel dispc_ovl_get_channel_out(enum omap_plane plane)
986 {
987         int shift;
988         u32 val;
989         enum omap_channel channel;
990
991         switch (plane) {
992         case OMAP_DSS_GFX:
993                 shift = 8;
994                 break;
995         case OMAP_DSS_VIDEO1:
996         case OMAP_DSS_VIDEO2:
997         case OMAP_DSS_VIDEO3:
998                 shift = 16;
999                 break;
1000         default:
1001                 BUG();
1002                 return 0;
1003         }
1004
1005         val = dispc_read_reg(DISPC_OVL_ATTRIBUTES(plane));
1006
1007         if (dss_has_feature(FEAT_MGR_LCD3)) {
1008                 if (FLD_GET(val, 31, 30) == 0)
1009                         channel = FLD_GET(val, shift, shift);
1010                 else if (FLD_GET(val, 31, 30) == 1)
1011                         channel = OMAP_DSS_CHANNEL_LCD2;
1012                 else
1013                         channel = OMAP_DSS_CHANNEL_LCD3;
1014         } else if (dss_has_feature(FEAT_MGR_LCD2)) {
1015                 if (FLD_GET(val, 31, 30) == 0)
1016                         channel = FLD_GET(val, shift, shift);
1017                 else
1018                         channel = OMAP_DSS_CHANNEL_LCD2;
1019         } else {
1020                 channel = FLD_GET(val, shift, shift);
1021         }
1022
1023         return channel;
1024 }
1025
1026 void dispc_wb_set_channel_in(enum dss_writeback_channel channel)
1027 {
1028         enum omap_plane plane = OMAP_DSS_WB;
1029
1030         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), channel, 18, 16);
1031 }
1032
1033 static void dispc_ovl_set_burst_size(enum omap_plane plane,
1034                 enum omap_burst_size burst_size)
1035 {
1036         static const unsigned shifts[] = { 6, 14, 14, 14, 14, };
1037         int shift;
1038
1039         shift = shifts[plane];
1040         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), burst_size, shift + 1, shift);
1041 }
1042
1043 static void dispc_configure_burst_sizes(void)
1044 {
1045         int i;
1046         const int burst_size = BURST_SIZE_X8;
1047
1048         /* Configure burst size always to maximum size */
1049         for (i = 0; i < omap_dss_get_num_overlays(); ++i)
1050                 dispc_ovl_set_burst_size(i, burst_size);
1051 }
1052
1053 static u32 dispc_ovl_get_burst_size(enum omap_plane plane)
1054 {
1055         unsigned unit = dss_feat_get_burst_size_unit();
1056         /* burst multiplier is always x8 (see dispc_configure_burst_sizes()) */
1057         return unit * 8;
1058 }
1059
1060 void dispc_enable_gamma_table(bool enable)
1061 {
1062         /*
1063          * This is partially implemented to support only disabling of
1064          * the gamma table.
1065          */
1066         if (enable) {
1067                 DSSWARN("Gamma table enabling for TV not yet supported");
1068                 return;
1069         }
1070
1071         REG_FLD_MOD(DISPC_CONFIG, enable, 9, 9);
1072 }
1073
1074 static void dispc_mgr_enable_cpr(enum omap_channel channel, bool enable)
1075 {
1076         if (channel == OMAP_DSS_CHANNEL_DIGIT)
1077                 return;
1078
1079         mgr_fld_write(channel, DISPC_MGR_FLD_CPR, enable);
1080 }
1081
1082 static void dispc_mgr_set_cpr_coef(enum omap_channel channel,
1083                 const struct omap_dss_cpr_coefs *coefs)
1084 {
1085         u32 coef_r, coef_g, coef_b;
1086
1087         if (!dss_mgr_is_lcd(channel))
1088                 return;
1089
1090         coef_r = FLD_VAL(coefs->rr, 31, 22) | FLD_VAL(coefs->rg, 20, 11) |
1091                 FLD_VAL(coefs->rb, 9, 0);
1092         coef_g = FLD_VAL(coefs->gr, 31, 22) | FLD_VAL(coefs->gg, 20, 11) |
1093                 FLD_VAL(coefs->gb, 9, 0);
1094         coef_b = FLD_VAL(coefs->br, 31, 22) | FLD_VAL(coefs->bg, 20, 11) |
1095                 FLD_VAL(coefs->bb, 9, 0);
1096
1097         dispc_write_reg(DISPC_CPR_COEF_R(channel), coef_r);
1098         dispc_write_reg(DISPC_CPR_COEF_G(channel), coef_g);
1099         dispc_write_reg(DISPC_CPR_COEF_B(channel), coef_b);
1100 }
1101
1102 static void dispc_ovl_set_vid_color_conv(enum omap_plane plane, bool enable)
1103 {
1104         u32 val;
1105
1106         BUG_ON(plane == OMAP_DSS_GFX);
1107
1108         val = dispc_read_reg(DISPC_OVL_ATTRIBUTES(plane));
1109         val = FLD_MOD(val, enable, 9, 9);
1110         dispc_write_reg(DISPC_OVL_ATTRIBUTES(plane), val);
1111 }
1112
1113 static void dispc_ovl_enable_replication(enum omap_plane plane,
1114                 enum omap_overlay_caps caps, bool enable)
1115 {
1116         static const unsigned shifts[] = { 5, 10, 10, 10 };
1117         int shift;
1118
1119         if ((caps & OMAP_DSS_OVL_CAP_REPLICATION) == 0)
1120                 return;
1121
1122         shift = shifts[plane];
1123         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), enable, shift, shift);
1124 }
1125
1126 static void dispc_mgr_set_size(enum omap_channel channel, u16 width,
1127                 u16 height)
1128 {
1129         u32 val;
1130
1131         val = FLD_VAL(height - 1, 26, 16) | FLD_VAL(width - 1, 10, 0);
1132         dispc_write_reg(DISPC_SIZE_MGR(channel), val);
1133 }
1134
1135 static void dispc_init_fifos(void)
1136 {
1137         u32 size;
1138         int fifo;
1139         u8 start, end;
1140         u32 unit;
1141
1142         unit = dss_feat_get_buffer_size_unit();
1143
1144         dss_feat_get_reg_field(FEAT_REG_FIFOSIZE, &start, &end);
1145
1146         for (fifo = 0; fifo < dispc.feat->num_fifos; ++fifo) {
1147                 size = REG_GET(DISPC_OVL_FIFO_SIZE_STATUS(fifo), start, end);
1148                 size *= unit;
1149                 dispc.fifo_size[fifo] = size;
1150
1151                 /*
1152                  * By default fifos are mapped directly to overlays, fifo 0 to
1153                  * ovl 0, fifo 1 to ovl 1, etc.
1154                  */
1155                 dispc.fifo_assignment[fifo] = fifo;
1156         }
1157
1158         /*
1159          * The GFX fifo on OMAP4 is smaller than the other fifos. The small fifo
1160          * causes problems with certain use cases, like using the tiler in 2D
1161          * mode. The below hack swaps the fifos of GFX and WB planes, thus
1162          * giving GFX plane a larger fifo. WB but should work fine with a
1163          * smaller fifo.
1164          */
1165         if (dispc.feat->gfx_fifo_workaround) {
1166                 u32 v;
1167
1168                 v = dispc_read_reg(DISPC_GLOBAL_BUFFER);
1169
1170                 v = FLD_MOD(v, 4, 2, 0); /* GFX BUF top to WB */
1171                 v = FLD_MOD(v, 4, 5, 3); /* GFX BUF bottom to WB */
1172                 v = FLD_MOD(v, 0, 26, 24); /* WB BUF top to GFX */
1173                 v = FLD_MOD(v, 0, 29, 27); /* WB BUF bottom to GFX */
1174
1175                 dispc_write_reg(DISPC_GLOBAL_BUFFER, v);
1176
1177                 dispc.fifo_assignment[OMAP_DSS_GFX] = OMAP_DSS_WB;
1178                 dispc.fifo_assignment[OMAP_DSS_WB] = OMAP_DSS_GFX;
1179         }
1180 }
1181
1182 static u32 dispc_ovl_get_fifo_size(enum omap_plane plane)
1183 {
1184         int fifo;
1185         u32 size = 0;
1186
1187         for (fifo = 0; fifo < dispc.feat->num_fifos; ++fifo) {
1188                 if (dispc.fifo_assignment[fifo] == plane)
1189                         size += dispc.fifo_size[fifo];
1190         }
1191
1192         return size;
1193 }
1194
1195 void dispc_ovl_set_fifo_threshold(enum omap_plane plane, u32 low, u32 high)
1196 {
1197         u8 hi_start, hi_end, lo_start, lo_end;
1198         u32 unit;
1199
1200         unit = dss_feat_get_buffer_size_unit();
1201
1202         WARN_ON(low % unit != 0);
1203         WARN_ON(high % unit != 0);
1204
1205         low /= unit;
1206         high /= unit;
1207
1208         dss_feat_get_reg_field(FEAT_REG_FIFOHIGHTHRESHOLD, &hi_start, &hi_end);
1209         dss_feat_get_reg_field(FEAT_REG_FIFOLOWTHRESHOLD, &lo_start, &lo_end);
1210
1211         DSSDBG("fifo(%d) threshold (bytes), old %u/%u, new %u/%u\n",
1212                         plane,
1213                         REG_GET(DISPC_OVL_FIFO_THRESHOLD(plane),
1214                                 lo_start, lo_end) * unit,
1215                         REG_GET(DISPC_OVL_FIFO_THRESHOLD(plane),
1216                                 hi_start, hi_end) * unit,
1217                         low * unit, high * unit);
1218
1219         dispc_write_reg(DISPC_OVL_FIFO_THRESHOLD(plane),
1220                         FLD_VAL(high, hi_start, hi_end) |
1221                         FLD_VAL(low, lo_start, lo_end));
1222 }
1223
1224 void dispc_enable_fifomerge(bool enable)
1225 {
1226         if (!dss_has_feature(FEAT_FIFO_MERGE)) {
1227                 WARN_ON(enable);
1228                 return;
1229         }
1230
1231         DSSDBG("FIFO merge %s\n", enable ? "enabled" : "disabled");
1232         REG_FLD_MOD(DISPC_CONFIG, enable ? 1 : 0, 14, 14);
1233 }
1234
1235 void dispc_ovl_compute_fifo_thresholds(enum omap_plane plane,
1236                 u32 *fifo_low, u32 *fifo_high, bool use_fifomerge,
1237                 bool manual_update)
1238 {
1239         /*
1240          * All sizes are in bytes. Both the buffer and burst are made of
1241          * buffer_units, and the fifo thresholds must be buffer_unit aligned.
1242          */
1243
1244         unsigned buf_unit = dss_feat_get_buffer_size_unit();
1245         unsigned ovl_fifo_size, total_fifo_size, burst_size;
1246         int i;
1247
1248         burst_size = dispc_ovl_get_burst_size(plane);
1249         ovl_fifo_size = dispc_ovl_get_fifo_size(plane);
1250
1251         if (use_fifomerge) {
1252                 total_fifo_size = 0;
1253                 for (i = 0; i < omap_dss_get_num_overlays(); ++i)
1254                         total_fifo_size += dispc_ovl_get_fifo_size(i);
1255         } else {
1256                 total_fifo_size = ovl_fifo_size;
1257         }
1258
1259         /*
1260          * We use the same low threshold for both fifomerge and non-fifomerge
1261          * cases, but for fifomerge we calculate the high threshold using the
1262          * combined fifo size
1263          */
1264
1265         if (manual_update && dss_has_feature(FEAT_OMAP3_DSI_FIFO_BUG)) {
1266                 *fifo_low = ovl_fifo_size - burst_size * 2;
1267                 *fifo_high = total_fifo_size - burst_size;
1268         } else if (plane == OMAP_DSS_WB) {
1269                 /*
1270                  * Most optimal configuration for writeback is to push out data
1271                  * to the interconnect the moment writeback pushes enough pixels
1272                  * in the FIFO to form a burst
1273                  */
1274                 *fifo_low = 0;
1275                 *fifo_high = burst_size;
1276         } else {
1277                 *fifo_low = ovl_fifo_size - burst_size;
1278                 *fifo_high = total_fifo_size - buf_unit;
1279         }
1280 }
1281
1282 static void dispc_ovl_set_fir(enum omap_plane plane,
1283                                 int hinc, int vinc,
1284                                 enum omap_color_component color_comp)
1285 {
1286         u32 val;
1287
1288         if (color_comp == DISPC_COLOR_COMPONENT_RGB_Y) {
1289                 u8 hinc_start, hinc_end, vinc_start, vinc_end;
1290
1291                 dss_feat_get_reg_field(FEAT_REG_FIRHINC,
1292                                         &hinc_start, &hinc_end);
1293                 dss_feat_get_reg_field(FEAT_REG_FIRVINC,
1294                                         &vinc_start, &vinc_end);
1295                 val = FLD_VAL(vinc, vinc_start, vinc_end) |
1296                                 FLD_VAL(hinc, hinc_start, hinc_end);
1297
1298                 dispc_write_reg(DISPC_OVL_FIR(plane), val);
1299         } else {
1300                 val = FLD_VAL(vinc, 28, 16) | FLD_VAL(hinc, 12, 0);
1301                 dispc_write_reg(DISPC_OVL_FIR2(plane), val);
1302         }
1303 }
1304
1305 static void dispc_ovl_set_vid_accu0(enum omap_plane plane, int haccu, int vaccu)
1306 {
1307         u32 val;
1308         u8 hor_start, hor_end, vert_start, vert_end;
1309
1310         dss_feat_get_reg_field(FEAT_REG_HORIZONTALACCU, &hor_start, &hor_end);
1311         dss_feat_get_reg_field(FEAT_REG_VERTICALACCU, &vert_start, &vert_end);
1312
1313         val = FLD_VAL(vaccu, vert_start, vert_end) |
1314                         FLD_VAL(haccu, hor_start, hor_end);
1315
1316         dispc_write_reg(DISPC_OVL_ACCU0(plane), val);
1317 }
1318
1319 static void dispc_ovl_set_vid_accu1(enum omap_plane plane, int haccu, int vaccu)
1320 {
1321         u32 val;
1322         u8 hor_start, hor_end, vert_start, vert_end;
1323
1324         dss_feat_get_reg_field(FEAT_REG_HORIZONTALACCU, &hor_start, &hor_end);
1325         dss_feat_get_reg_field(FEAT_REG_VERTICALACCU, &vert_start, &vert_end);
1326
1327         val = FLD_VAL(vaccu, vert_start, vert_end) |
1328                         FLD_VAL(haccu, hor_start, hor_end);
1329
1330         dispc_write_reg(DISPC_OVL_ACCU1(plane), val);
1331 }
1332
1333 static void dispc_ovl_set_vid_accu2_0(enum omap_plane plane, int haccu,
1334                 int vaccu)
1335 {
1336         u32 val;
1337
1338         val = FLD_VAL(vaccu, 26, 16) | FLD_VAL(haccu, 10, 0);
1339         dispc_write_reg(DISPC_OVL_ACCU2_0(plane), val);
1340 }
1341
1342 static void dispc_ovl_set_vid_accu2_1(enum omap_plane plane, int haccu,
1343                 int vaccu)
1344 {
1345         u32 val;
1346
1347         val = FLD_VAL(vaccu, 26, 16) | FLD_VAL(haccu, 10, 0);
1348         dispc_write_reg(DISPC_OVL_ACCU2_1(plane), val);
1349 }
1350
1351 static void dispc_ovl_set_scale_param(enum omap_plane plane,
1352                 u16 orig_width, u16 orig_height,
1353                 u16 out_width, u16 out_height,
1354                 bool five_taps, u8 rotation,
1355                 enum omap_color_component color_comp)
1356 {
1357         int fir_hinc, fir_vinc;
1358
1359         fir_hinc = 1024 * orig_width / out_width;
1360         fir_vinc = 1024 * orig_height / out_height;
1361
1362         dispc_ovl_set_scale_coef(plane, fir_hinc, fir_vinc, five_taps,
1363                                 color_comp);
1364         dispc_ovl_set_fir(plane, fir_hinc, fir_vinc, color_comp);
1365 }
1366
1367 static void dispc_ovl_set_accu_uv(enum omap_plane plane,
1368                 u16 orig_width, u16 orig_height, u16 out_width, u16 out_height,
1369                 bool ilace, enum omap_color_mode color_mode, u8 rotation)
1370 {
1371         int h_accu2_0, h_accu2_1;
1372         int v_accu2_0, v_accu2_1;
1373         int chroma_hinc, chroma_vinc;
1374         int idx;
1375
1376         struct accu {
1377                 s8 h0_m, h0_n;
1378                 s8 h1_m, h1_n;
1379                 s8 v0_m, v0_n;
1380                 s8 v1_m, v1_n;
1381         };
1382
1383         const struct accu *accu_table;
1384         const struct accu *accu_val;
1385
1386         static const struct accu accu_nv12[4] = {
1387                 {  0, 1,  0, 1 , -1, 2, 0, 1 },
1388                 {  1, 2, -3, 4 ,  0, 1, 0, 1 },
1389                 { -1, 1,  0, 1 , -1, 2, 0, 1 },
1390                 { -1, 2, -1, 2 , -1, 1, 0, 1 },
1391         };
1392
1393         static const struct accu accu_nv12_ilace[4] = {
1394                 {  0, 1,  0, 1 , -3, 4, -1, 4 },
1395                 { -1, 4, -3, 4 ,  0, 1,  0, 1 },
1396                 { -1, 1,  0, 1 , -1, 4, -3, 4 },
1397                 { -3, 4, -3, 4 , -1, 1,  0, 1 },
1398         };
1399
1400         static const struct accu accu_yuv[4] = {
1401                 {  0, 1, 0, 1,  0, 1, 0, 1 },
1402                 {  0, 1, 0, 1,  0, 1, 0, 1 },
1403                 { -1, 1, 0, 1,  0, 1, 0, 1 },
1404                 {  0, 1, 0, 1, -1, 1, 0, 1 },
1405         };
1406
1407         switch (rotation) {
1408         case OMAP_DSS_ROT_0:
1409                 idx = 0;
1410                 break;
1411         case OMAP_DSS_ROT_90:
1412                 idx = 1;
1413                 break;
1414         case OMAP_DSS_ROT_180:
1415                 idx = 2;
1416                 break;
1417         case OMAP_DSS_ROT_270:
1418                 idx = 3;
1419                 break;
1420         default:
1421                 BUG();
1422                 return;
1423         }
1424
1425         switch (color_mode) {
1426         case OMAP_DSS_COLOR_NV12:
1427                 if (ilace)
1428                         accu_table = accu_nv12_ilace;
1429                 else
1430                         accu_table = accu_nv12;
1431                 break;
1432         case OMAP_DSS_COLOR_YUV2:
1433         case OMAP_DSS_COLOR_UYVY:
1434                 accu_table = accu_yuv;
1435                 break;
1436         default:
1437                 BUG();
1438                 return;
1439         }
1440
1441         accu_val = &accu_table[idx];
1442
1443         chroma_hinc = 1024 * orig_width / out_width;
1444         chroma_vinc = 1024 * orig_height / out_height;
1445
1446         h_accu2_0 = (accu_val->h0_m * chroma_hinc / accu_val->h0_n) % 1024;
1447         h_accu2_1 = (accu_val->h1_m * chroma_hinc / accu_val->h1_n) % 1024;
1448         v_accu2_0 = (accu_val->v0_m * chroma_vinc / accu_val->v0_n) % 1024;
1449         v_accu2_1 = (accu_val->v1_m * chroma_vinc / accu_val->v1_n) % 1024;
1450
1451         dispc_ovl_set_vid_accu2_0(plane, h_accu2_0, v_accu2_0);
1452         dispc_ovl_set_vid_accu2_1(plane, h_accu2_1, v_accu2_1);
1453 }
1454
1455 static void dispc_ovl_set_scaling_common(enum omap_plane plane,
1456                 u16 orig_width, u16 orig_height,
1457                 u16 out_width, u16 out_height,
1458                 bool ilace, bool five_taps,
1459                 bool fieldmode, enum omap_color_mode color_mode,
1460                 u8 rotation)
1461 {
1462         int accu0 = 0;
1463         int accu1 = 0;
1464         u32 l;
1465
1466         dispc_ovl_set_scale_param(plane, orig_width, orig_height,
1467                                 out_width, out_height, five_taps,
1468                                 rotation, DISPC_COLOR_COMPONENT_RGB_Y);
1469         l = dispc_read_reg(DISPC_OVL_ATTRIBUTES(plane));
1470
1471         /* RESIZEENABLE and VERTICALTAPS */
1472         l &= ~((0x3 << 5) | (0x1 << 21));
1473         l |= (orig_width != out_width) ? (1 << 5) : 0;
1474         l |= (orig_height != out_height) ? (1 << 6) : 0;
1475         l |= five_taps ? (1 << 21) : 0;
1476
1477         /* VRESIZECONF and HRESIZECONF */
1478         if (dss_has_feature(FEAT_RESIZECONF)) {
1479                 l &= ~(0x3 << 7);
1480                 l |= (orig_width <= out_width) ? 0 : (1 << 7);
1481                 l |= (orig_height <= out_height) ? 0 : (1 << 8);
1482         }
1483
1484         /* LINEBUFFERSPLIT */
1485         if (dss_has_feature(FEAT_LINEBUFFERSPLIT)) {
1486                 l &= ~(0x1 << 22);
1487                 l |= five_taps ? (1 << 22) : 0;
1488         }
1489
1490         dispc_write_reg(DISPC_OVL_ATTRIBUTES(plane), l);
1491
1492         /*
1493          * field 0 = even field = bottom field
1494          * field 1 = odd field = top field
1495          */
1496         if (ilace && !fieldmode) {
1497                 accu1 = 0;
1498                 accu0 = ((1024 * orig_height / out_height) / 2) & 0x3ff;
1499                 if (accu0 >= 1024/2) {
1500                         accu1 = 1024/2;
1501                         accu0 -= accu1;
1502                 }
1503         }
1504
1505         dispc_ovl_set_vid_accu0(plane, 0, accu0);
1506         dispc_ovl_set_vid_accu1(plane, 0, accu1);
1507 }
1508
1509 static void dispc_ovl_set_scaling_uv(enum omap_plane plane,
1510                 u16 orig_width, u16 orig_height,
1511                 u16 out_width, u16 out_height,
1512                 bool ilace, bool five_taps,
1513                 bool fieldmode, enum omap_color_mode color_mode,
1514                 u8 rotation)
1515 {
1516         int scale_x = out_width != orig_width;
1517         int scale_y = out_height != orig_height;
1518         bool chroma_upscale = plane != OMAP_DSS_WB ? true : false;
1519
1520         if (!dss_has_feature(FEAT_HANDLE_UV_SEPARATE))
1521                 return;
1522         if ((color_mode != OMAP_DSS_COLOR_YUV2 &&
1523                         color_mode != OMAP_DSS_COLOR_UYVY &&
1524                         color_mode != OMAP_DSS_COLOR_NV12)) {
1525                 /* reset chroma resampling for RGB formats  */
1526                 if (plane != OMAP_DSS_WB)
1527                         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES2(plane), 0, 8, 8);
1528                 return;
1529         }
1530
1531         dispc_ovl_set_accu_uv(plane, orig_width, orig_height, out_width,
1532                         out_height, ilace, color_mode, rotation);
1533
1534         switch (color_mode) {
1535         case OMAP_DSS_COLOR_NV12:
1536                 if (chroma_upscale) {
1537                         /* UV is subsampled by 2 horizontally and vertically */
1538                         orig_height >>= 1;
1539                         orig_width >>= 1;
1540                 } else {
1541                         /* UV is downsampled by 2 horizontally and vertically */
1542                         orig_height <<= 1;
1543                         orig_width <<= 1;
1544                 }
1545
1546                 break;
1547         case OMAP_DSS_COLOR_YUV2:
1548         case OMAP_DSS_COLOR_UYVY:
1549                 /* For YUV422 with 90/270 rotation, we don't upsample chroma */
1550                 if (rotation == OMAP_DSS_ROT_0 ||
1551                                 rotation == OMAP_DSS_ROT_180) {
1552                         if (chroma_upscale)
1553                                 /* UV is subsampled by 2 horizontally */
1554                                 orig_width >>= 1;
1555                         else
1556                                 /* UV is downsampled by 2 horizontally */
1557                                 orig_width <<= 1;
1558                 }
1559
1560                 /* must use FIR for YUV422 if rotated */
1561                 if (rotation != OMAP_DSS_ROT_0)
1562                         scale_x = scale_y = true;
1563
1564                 break;
1565         default:
1566                 BUG();
1567                 return;
1568         }
1569
1570         if (out_width != orig_width)
1571                 scale_x = true;
1572         if (out_height != orig_height)
1573                 scale_y = true;
1574
1575         dispc_ovl_set_scale_param(plane, orig_width, orig_height,
1576                         out_width, out_height, five_taps,
1577                                 rotation, DISPC_COLOR_COMPONENT_UV);
1578
1579         if (plane != OMAP_DSS_WB)
1580                 REG_FLD_MOD(DISPC_OVL_ATTRIBUTES2(plane),
1581                         (scale_x || scale_y) ? 1 : 0, 8, 8);
1582
1583         /* set H scaling */
1584         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), scale_x ? 1 : 0, 5, 5);
1585         /* set V scaling */
1586         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), scale_y ? 1 : 0, 6, 6);
1587 }
1588
1589 static void dispc_ovl_set_scaling(enum omap_plane plane,
1590                 u16 orig_width, u16 orig_height,
1591                 u16 out_width, u16 out_height,
1592                 bool ilace, bool five_taps,
1593                 bool fieldmode, enum omap_color_mode color_mode,
1594                 u8 rotation)
1595 {
1596         BUG_ON(plane == OMAP_DSS_GFX);
1597
1598         dispc_ovl_set_scaling_common(plane,
1599                         orig_width, orig_height,
1600                         out_width, out_height,
1601                         ilace, five_taps,
1602                         fieldmode, color_mode,
1603                         rotation);
1604
1605         dispc_ovl_set_scaling_uv(plane,
1606                 orig_width, orig_height,
1607                 out_width, out_height,
1608                 ilace, five_taps,
1609                 fieldmode, color_mode,
1610                 rotation);
1611 }
1612
1613 static void dispc_ovl_set_rotation_attrs(enum omap_plane plane, u8 rotation,
1614                 bool mirroring, enum omap_color_mode color_mode)
1615 {
1616         bool row_repeat = false;
1617         int vidrot = 0;
1618
1619         if (color_mode == OMAP_DSS_COLOR_YUV2 ||
1620                         color_mode == OMAP_DSS_COLOR_UYVY) {
1621
1622                 if (mirroring) {
1623                         switch (rotation) {
1624                         case OMAP_DSS_ROT_0:
1625                                 vidrot = 2;
1626                                 break;
1627                         case OMAP_DSS_ROT_90:
1628                                 vidrot = 1;
1629                                 break;
1630                         case OMAP_DSS_ROT_180:
1631                                 vidrot = 0;
1632                                 break;
1633                         case OMAP_DSS_ROT_270:
1634                                 vidrot = 3;
1635                                 break;
1636                         }
1637                 } else {
1638                         switch (rotation) {
1639                         case OMAP_DSS_ROT_0:
1640                                 vidrot = 0;
1641                                 break;
1642                         case OMAP_DSS_ROT_90:
1643                                 vidrot = 1;
1644                                 break;
1645                         case OMAP_DSS_ROT_180:
1646                                 vidrot = 2;
1647                                 break;
1648                         case OMAP_DSS_ROT_270:
1649                                 vidrot = 3;
1650                                 break;
1651                         }
1652                 }
1653
1654                 if (rotation == OMAP_DSS_ROT_90 || rotation == OMAP_DSS_ROT_270)
1655                         row_repeat = true;
1656                 else
1657                         row_repeat = false;
1658         }
1659
1660         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), vidrot, 13, 12);
1661         if (dss_has_feature(FEAT_ROWREPEATENABLE))
1662                 REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane),
1663                         row_repeat ? 1 : 0, 18, 18);
1664 }
1665
1666 static int color_mode_to_bpp(enum omap_color_mode color_mode)
1667 {
1668         switch (color_mode) {
1669         case OMAP_DSS_COLOR_CLUT1:
1670                 return 1;
1671         case OMAP_DSS_COLOR_CLUT2:
1672                 return 2;
1673         case OMAP_DSS_COLOR_CLUT4:
1674                 return 4;
1675         case OMAP_DSS_COLOR_CLUT8:
1676         case OMAP_DSS_COLOR_NV12:
1677                 return 8;
1678         case OMAP_DSS_COLOR_RGB12U:
1679         case OMAP_DSS_COLOR_RGB16:
1680         case OMAP_DSS_COLOR_ARGB16:
1681         case OMAP_DSS_COLOR_YUV2:
1682         case OMAP_DSS_COLOR_UYVY:
1683         case OMAP_DSS_COLOR_RGBA16:
1684         case OMAP_DSS_COLOR_RGBX16:
1685         case OMAP_DSS_COLOR_ARGB16_1555:
1686         case OMAP_DSS_COLOR_XRGB16_1555:
1687                 return 16;
1688         case OMAP_DSS_COLOR_RGB24P:
1689                 return 24;
1690         case OMAP_DSS_COLOR_RGB24U:
1691         case OMAP_DSS_COLOR_ARGB32:
1692         case OMAP_DSS_COLOR_RGBA32:
1693         case OMAP_DSS_COLOR_RGBX32:
1694                 return 32;
1695         default:
1696                 BUG();
1697                 return 0;
1698         }
1699 }
1700
1701 static s32 pixinc(int pixels, u8 ps)
1702 {
1703         if (pixels == 1)
1704                 return 1;
1705         else if (pixels > 1)
1706                 return 1 + (pixels - 1) * ps;
1707         else if (pixels < 0)
1708                 return 1 - (-pixels + 1) * ps;
1709         else
1710                 BUG();
1711                 return 0;
1712 }
1713
1714 static void calc_vrfb_rotation_offset(u8 rotation, bool mirror,
1715                 u16 screen_width,
1716                 u16 width, u16 height,
1717                 enum omap_color_mode color_mode, bool fieldmode,
1718                 unsigned int field_offset,
1719                 unsigned *offset0, unsigned *offset1,
1720                 s32 *row_inc, s32 *pix_inc, int x_predecim, int y_predecim)
1721 {
1722         u8 ps;
1723
1724         /* FIXME CLUT formats */
1725         switch (color_mode) {
1726         case OMAP_DSS_COLOR_CLUT1:
1727         case OMAP_DSS_COLOR_CLUT2:
1728         case OMAP_DSS_COLOR_CLUT4:
1729         case OMAP_DSS_COLOR_CLUT8:
1730                 BUG();
1731                 return;
1732         case OMAP_DSS_COLOR_YUV2:
1733         case OMAP_DSS_COLOR_UYVY:
1734                 ps = 4;
1735                 break;
1736         default:
1737                 ps = color_mode_to_bpp(color_mode) / 8;
1738                 break;
1739         }
1740
1741         DSSDBG("calc_rot(%d): scrw %d, %dx%d\n", rotation, screen_width,
1742                         width, height);
1743
1744         /*
1745          * field 0 = even field = bottom field
1746          * field 1 = odd field = top field
1747          */
1748         switch (rotation + mirror * 4) {
1749         case OMAP_DSS_ROT_0:
1750         case OMAP_DSS_ROT_180:
1751                 /*
1752                  * If the pixel format is YUV or UYVY divide the width
1753                  * of the image by 2 for 0 and 180 degree rotation.
1754                  */
1755                 if (color_mode == OMAP_DSS_COLOR_YUV2 ||
1756                         color_mode == OMAP_DSS_COLOR_UYVY)
1757                         width = width >> 1;
1758         case OMAP_DSS_ROT_90:
1759         case OMAP_DSS_ROT_270:
1760                 *offset1 = 0;
1761                 if (field_offset)
1762                         *offset0 = field_offset * screen_width * ps;
1763                 else
1764                         *offset0 = 0;
1765
1766                 *row_inc = pixinc(1 +
1767                         (y_predecim * screen_width - x_predecim * width) +
1768                         (fieldmode ? screen_width : 0), ps);
1769                 *pix_inc = pixinc(x_predecim, ps);
1770                 break;
1771
1772         case OMAP_DSS_ROT_0 + 4:
1773         case OMAP_DSS_ROT_180 + 4:
1774                 /* If the pixel format is YUV or UYVY divide the width
1775                  * of the image by 2  for 0 degree and 180 degree
1776                  */
1777                 if (color_mode == OMAP_DSS_COLOR_YUV2 ||
1778                         color_mode == OMAP_DSS_COLOR_UYVY)
1779                         width = width >> 1;
1780         case OMAP_DSS_ROT_90 + 4:
1781         case OMAP_DSS_ROT_270 + 4:
1782                 *offset1 = 0;
1783                 if (field_offset)
1784                         *offset0 = field_offset * screen_width * ps;
1785                 else
1786                         *offset0 = 0;
1787                 *row_inc = pixinc(1 -
1788                         (y_predecim * screen_width + x_predecim * width) -
1789                         (fieldmode ? screen_width : 0), ps);
1790                 *pix_inc = pixinc(x_predecim, ps);
1791                 break;
1792
1793         default:
1794                 BUG();
1795                 return;
1796         }
1797 }
1798
1799 static void calc_dma_rotation_offset(u8 rotation, bool mirror,
1800                 u16 screen_width,
1801                 u16 width, u16 height,
1802                 enum omap_color_mode color_mode, bool fieldmode,
1803                 unsigned int field_offset,
1804                 unsigned *offset0, unsigned *offset1,
1805                 s32 *row_inc, s32 *pix_inc, int x_predecim, int y_predecim)
1806 {
1807         u8 ps;
1808         u16 fbw, fbh;
1809
1810         /* FIXME CLUT formats */
1811         switch (color_mode) {
1812         case OMAP_DSS_COLOR_CLUT1:
1813         case OMAP_DSS_COLOR_CLUT2:
1814         case OMAP_DSS_COLOR_CLUT4:
1815         case OMAP_DSS_COLOR_CLUT8:
1816                 BUG();
1817                 return;
1818         default:
1819                 ps = color_mode_to_bpp(color_mode) / 8;
1820                 break;
1821         }
1822
1823         DSSDBG("calc_rot(%d): scrw %d, %dx%d\n", rotation, screen_width,
1824                         width, height);
1825
1826         /* width & height are overlay sizes, convert to fb sizes */
1827
1828         if (rotation == OMAP_DSS_ROT_0 || rotation == OMAP_DSS_ROT_180) {
1829                 fbw = width;
1830                 fbh = height;
1831         } else {
1832                 fbw = height;
1833                 fbh = width;
1834         }
1835
1836         /*
1837          * field 0 = even field = bottom field
1838          * field 1 = odd field = top field
1839          */
1840         switch (rotation + mirror * 4) {
1841         case OMAP_DSS_ROT_0:
1842                 *offset1 = 0;
1843                 if (field_offset)
1844                         *offset0 = *offset1 + field_offset * screen_width * ps;
1845                 else
1846                         *offset0 = *offset1;
1847                 *row_inc = pixinc(1 +
1848                         (y_predecim * screen_width - fbw * x_predecim) +
1849                         (fieldmode ? screen_width : 0), ps);
1850                 if (color_mode == OMAP_DSS_COLOR_YUV2 ||
1851                         color_mode == OMAP_DSS_COLOR_UYVY)
1852                         *pix_inc = pixinc(x_predecim, 2 * ps);
1853                 else
1854                         *pix_inc = pixinc(x_predecim, ps);
1855                 break;
1856         case OMAP_DSS_ROT_90:
1857                 *offset1 = screen_width * (fbh - 1) * ps;
1858                 if (field_offset)
1859                         *offset0 = *offset1 + field_offset * ps;
1860                 else
1861                         *offset0 = *offset1;
1862                 *row_inc = pixinc(screen_width * (fbh * x_predecim - 1) +
1863                                 y_predecim + (fieldmode ? 1 : 0), ps);
1864                 *pix_inc = pixinc(-x_predecim * screen_width, ps);
1865                 break;
1866         case OMAP_DSS_ROT_180:
1867                 *offset1 = (screen_width * (fbh - 1) + fbw - 1) * ps;
1868                 if (field_offset)
1869                         *offset0 = *offset1 - field_offset * screen_width * ps;
1870                 else
1871                         *offset0 = *offset1;
1872                 *row_inc = pixinc(-1 -
1873                         (y_predecim * screen_width - fbw * x_predecim) -
1874                         (fieldmode ? screen_width : 0), ps);
1875                 if (color_mode == OMAP_DSS_COLOR_YUV2 ||
1876                         color_mode == OMAP_DSS_COLOR_UYVY)
1877                         *pix_inc = pixinc(-x_predecim, 2 * ps);
1878                 else
1879                         *pix_inc = pixinc(-x_predecim, ps);
1880                 break;
1881         case OMAP_DSS_ROT_270:
1882                 *offset1 = (fbw - 1) * ps;
1883                 if (field_offset)
1884                         *offset0 = *offset1 - field_offset * ps;
1885                 else
1886                         *offset0 = *offset1;
1887                 *row_inc = pixinc(-screen_width * (fbh * x_predecim - 1) -
1888                                 y_predecim - (fieldmode ? 1 : 0), ps);
1889                 *pix_inc = pixinc(x_predecim * screen_width, ps);
1890                 break;
1891
1892         /* mirroring */
1893         case OMAP_DSS_ROT_0 + 4:
1894                 *offset1 = (fbw - 1) * ps;
1895                 if (field_offset)
1896                         *offset0 = *offset1 + field_offset * screen_width * ps;
1897                 else
1898                         *offset0 = *offset1;
1899                 *row_inc = pixinc(y_predecim * screen_width * 2 - 1 +
1900                                 (fieldmode ? screen_width : 0),
1901                                 ps);
1902                 if (color_mode == OMAP_DSS_COLOR_YUV2 ||
1903                         color_mode == OMAP_DSS_COLOR_UYVY)
1904                         *pix_inc = pixinc(-x_predecim, 2 * ps);
1905                 else
1906                         *pix_inc = pixinc(-x_predecim, ps);
1907                 break;
1908
1909         case OMAP_DSS_ROT_90 + 4:
1910                 *offset1 = 0;
1911                 if (field_offset)
1912                         *offset0 = *offset1 + field_offset * ps;
1913                 else
1914                         *offset0 = *offset1;
1915                 *row_inc = pixinc(-screen_width * (fbh * x_predecim - 1) +
1916                                 y_predecim + (fieldmode ? 1 : 0),
1917                                 ps);
1918                 *pix_inc = pixinc(x_predecim * screen_width, ps);
1919                 break;
1920
1921         case OMAP_DSS_ROT_180 + 4:
1922                 *offset1 = screen_width * (fbh - 1) * ps;
1923                 if (field_offset)
1924                         *offset0 = *offset1 - field_offset * screen_width * ps;
1925                 else
1926                         *offset0 = *offset1;
1927                 *row_inc = pixinc(1 - y_predecim * screen_width * 2 -
1928                                 (fieldmode ? screen_width : 0),
1929                                 ps);
1930                 if (color_mode == OMAP_DSS_COLOR_YUV2 ||
1931                         color_mode == OMAP_DSS_COLOR_UYVY)
1932                         *pix_inc = pixinc(x_predecim, 2 * ps);
1933                 else
1934                         *pix_inc = pixinc(x_predecim, ps);
1935                 break;
1936
1937         case OMAP_DSS_ROT_270 + 4:
1938                 *offset1 = (screen_width * (fbh - 1) + fbw - 1) * ps;
1939                 if (field_offset)
1940                         *offset0 = *offset1 - field_offset * ps;
1941                 else
1942                         *offset0 = *offset1;
1943                 *row_inc = pixinc(screen_width * (fbh * x_predecim - 1) -
1944                                 y_predecim - (fieldmode ? 1 : 0),
1945                                 ps);
1946                 *pix_inc = pixinc(-x_predecim * screen_width, ps);
1947                 break;
1948
1949         default:
1950                 BUG();
1951                 return;
1952         }
1953 }
1954
1955 static void calc_tiler_rotation_offset(u16 screen_width, u16 width,
1956                 enum omap_color_mode color_mode, bool fieldmode,
1957                 unsigned int field_offset, unsigned *offset0, unsigned *offset1,
1958                 s32 *row_inc, s32 *pix_inc, int x_predecim, int y_predecim)
1959 {
1960         u8 ps;
1961
1962         switch (color_mode) {
1963         case OMAP_DSS_COLOR_CLUT1:
1964         case OMAP_DSS_COLOR_CLUT2:
1965         case OMAP_DSS_COLOR_CLUT4:
1966         case OMAP_DSS_COLOR_CLUT8:
1967                 BUG();
1968                 return;
1969         default:
1970                 ps = color_mode_to_bpp(color_mode) / 8;
1971                 break;
1972         }
1973
1974         DSSDBG("scrw %d, width %d\n", screen_width, width);
1975
1976         /*
1977          * field 0 = even field = bottom field
1978          * field 1 = odd field = top field
1979          */
1980         *offset1 = 0;
1981         if (field_offset)
1982                 *offset0 = *offset1 + field_offset * screen_width * ps;
1983         else
1984                 *offset0 = *offset1;
1985         *row_inc = pixinc(1 + (y_predecim * screen_width - width * x_predecim) +
1986                         (fieldmode ? screen_width : 0), ps);
1987         if (color_mode == OMAP_DSS_COLOR_YUV2 ||
1988                 color_mode == OMAP_DSS_COLOR_UYVY)
1989                 *pix_inc = pixinc(x_predecim, 2 * ps);
1990         else
1991                 *pix_inc = pixinc(x_predecim, ps);
1992 }
1993
1994 /*
1995  * This function is used to avoid synclosts in OMAP3, because of some
1996  * undocumented horizontal position and timing related limitations.
1997  */
1998 static int check_horiz_timing_omap3(enum omap_plane plane,
1999                 const struct omap_video_timings *t, u16 pos_x,
2000                 u16 width, u16 height, u16 out_width, u16 out_height)
2001 {
2002         int DS = DIV_ROUND_UP(height, out_height);
2003         unsigned long nonactive;
2004         static const u8 limits[3] = { 8, 10, 20 };
2005         u64 val, blank;
2006         unsigned long pclk = dispc_plane_pclk_rate(plane);
2007         unsigned long lclk = dispc_plane_lclk_rate(plane);
2008         int i;
2009
2010         nonactive = t->x_res + t->hfp + t->hsw + t->hbp - out_width;
2011
2012         i = 0;
2013         if (out_height < height)
2014                 i++;
2015         if (out_width < width)
2016                 i++;
2017         blank = div_u64((u64)(t->hbp + t->hsw + t->hfp) * lclk, pclk);
2018         DSSDBG("blanking period + ppl = %llu (limit = %u)\n", blank, limits[i]);
2019         if (blank <= limits[i])
2020                 return -EINVAL;
2021
2022         /*
2023          * Pixel data should be prepared before visible display point starts.
2024          * So, atleast DS-2 lines must have already been fetched by DISPC
2025          * during nonactive - pos_x period.
2026          */
2027         val = div_u64((u64)(nonactive - pos_x) * lclk, pclk);
2028         DSSDBG("(nonactive - pos_x) * pcd = %llu max(0, DS - 2) * width = %d\n",
2029                 val, max(0, DS - 2) * width);
2030         if (val < max(0, DS - 2) * width)
2031                 return -EINVAL;
2032
2033         /*
2034          * All lines need to be refilled during the nonactive period of which
2035          * only one line can be loaded during the active period. So, atleast
2036          * DS - 1 lines should be loaded during nonactive period.
2037          */
2038         val =  div_u64((u64)nonactive * lclk, pclk);
2039         DSSDBG("nonactive * pcd  = %llu, max(0, DS - 1) * width = %d\n",
2040                 val, max(0, DS - 1) * width);
2041         if (val < max(0, DS - 1) * width)
2042                 return -EINVAL;
2043
2044         return 0;
2045 }
2046
2047 static unsigned long calc_core_clk_five_taps(enum omap_plane plane,
2048                 const struct omap_video_timings *mgr_timings, u16 width,
2049                 u16 height, u16 out_width, u16 out_height,
2050                 enum omap_color_mode color_mode)
2051 {
2052         u32 core_clk = 0;
2053         u64 tmp;
2054         unsigned long pclk = dispc_plane_pclk_rate(plane);
2055
2056         if (height <= out_height && width <= out_width)
2057                 return (unsigned long) pclk;
2058
2059         if (height > out_height) {
2060                 unsigned int ppl = mgr_timings->x_res;
2061
2062                 tmp = pclk * height * out_width;
2063                 do_div(tmp, 2 * out_height * ppl);
2064                 core_clk = tmp;
2065
2066                 if (height > 2 * out_height) {
2067                         if (ppl == out_width)
2068                                 return 0;
2069
2070                         tmp = pclk * (height - 2 * out_height) * out_width;
2071                         do_div(tmp, 2 * out_height * (ppl - out_width));
2072                         core_clk = max_t(u32, core_clk, tmp);
2073                 }
2074         }
2075
2076         if (width > out_width) {
2077                 tmp = pclk * width;
2078                 do_div(tmp, out_width);
2079                 core_clk = max_t(u32, core_clk, tmp);
2080
2081                 if (color_mode == OMAP_DSS_COLOR_RGB24U)
2082                         core_clk <<= 1;
2083         }
2084
2085         return core_clk;
2086 }
2087
2088 static unsigned long calc_core_clk_24xx(enum omap_plane plane, u16 width,
2089                 u16 height, u16 out_width, u16 out_height, bool mem_to_mem)
2090 {
2091         unsigned long pclk = dispc_plane_pclk_rate(plane);
2092
2093         if (height > out_height && width > out_width)
2094                 return pclk * 4;
2095         else
2096                 return pclk * 2;
2097 }
2098
2099 static unsigned long calc_core_clk_34xx(enum omap_plane plane, u16 width,
2100                 u16 height, u16 out_width, u16 out_height, bool mem_to_mem)
2101 {
2102         unsigned int hf, vf;
2103         unsigned long pclk = dispc_plane_pclk_rate(plane);
2104
2105         /*
2106          * FIXME how to determine the 'A' factor
2107          * for the no downscaling case ?
2108          */
2109
2110         if (width > 3 * out_width)
2111                 hf = 4;
2112         else if (width > 2 * out_width)
2113                 hf = 3;
2114         else if (width > out_width)
2115                 hf = 2;
2116         else
2117                 hf = 1;
2118         if (height > out_height)
2119                 vf = 2;
2120         else
2121                 vf = 1;
2122
2123         return pclk * vf * hf;
2124 }
2125
2126 static unsigned long calc_core_clk_44xx(enum omap_plane plane, u16 width,
2127                 u16 height, u16 out_width, u16 out_height, bool mem_to_mem)
2128 {
2129         unsigned long pclk;
2130
2131         /*
2132          * If the overlay/writeback is in mem to mem mode, there are no
2133          * downscaling limitations with respect to pixel clock, return 1 as
2134          * required core clock to represent that we have sufficient enough
2135          * core clock to do maximum downscaling
2136          */
2137         if (mem_to_mem)
2138                 return 1;
2139
2140         pclk = dispc_plane_pclk_rate(plane);
2141
2142         if (width > out_width)
2143                 return DIV_ROUND_UP(pclk, out_width) * width;
2144         else
2145                 return pclk;
2146 }
2147
2148 static int dispc_ovl_calc_scaling_24xx(enum omap_plane plane,
2149                 const struct omap_video_timings *mgr_timings,
2150                 u16 width, u16 height, u16 out_width, u16 out_height,
2151                 enum omap_color_mode color_mode, bool *five_taps,
2152                 int *x_predecim, int *y_predecim, int *decim_x, int *decim_y,
2153                 u16 pos_x, unsigned long *core_clk, bool mem_to_mem)
2154 {
2155         int error;
2156         u16 in_width, in_height;
2157         int min_factor = min(*decim_x, *decim_y);
2158         const int maxsinglelinewidth =
2159                         dss_feat_get_param_max(FEAT_PARAM_LINEWIDTH);
2160
2161         *five_taps = false;
2162
2163         do {
2164                 in_height = DIV_ROUND_UP(height, *decim_y);
2165                 in_width = DIV_ROUND_UP(width, *decim_x);
2166                 *core_clk = dispc.feat->calc_core_clk(plane, in_width,
2167                                 in_height, out_width, out_height, mem_to_mem);
2168                 error = (in_width > maxsinglelinewidth || !*core_clk ||
2169                         *core_clk > dispc_core_clk_rate());
2170                 if (error) {
2171                         if (*decim_x == *decim_y) {
2172                                 *decim_x = min_factor;
2173                                 ++*decim_y;
2174                         } else {
2175                                 swap(*decim_x, *decim_y);
2176                                 if (*decim_x < *decim_y)
2177                                         ++*decim_x;
2178                         }
2179                 }
2180         } while (*decim_x <= *x_predecim && *decim_y <= *y_predecim && error);
2181
2182         if (in_width > maxsinglelinewidth) {
2183                 DSSERR("Cannot scale max input width exceeded");
2184                 return -EINVAL;
2185         }
2186         return 0;
2187 }
2188
2189 static int dispc_ovl_calc_scaling_34xx(enum omap_plane plane,
2190                 const struct omap_video_timings *mgr_timings,
2191                 u16 width, u16 height, u16 out_width, u16 out_height,
2192                 enum omap_color_mode color_mode, bool *five_taps,
2193                 int *x_predecim, int *y_predecim, int *decim_x, int *decim_y,
2194                 u16 pos_x, unsigned long *core_clk, bool mem_to_mem)
2195 {
2196         int error;
2197         u16 in_width, in_height;
2198         int min_factor = min(*decim_x, *decim_y);
2199         const int maxsinglelinewidth =
2200                         dss_feat_get_param_max(FEAT_PARAM_LINEWIDTH);
2201
2202         do {
2203                 in_height = DIV_ROUND_UP(height, *decim_y);
2204                 in_width = DIV_ROUND_UP(width, *decim_x);
2205                 *core_clk = calc_core_clk_five_taps(plane, mgr_timings,
2206                         in_width, in_height, out_width, out_height, color_mode);
2207
2208                 error = check_horiz_timing_omap3(plane, mgr_timings,
2209                                 pos_x, in_width, in_height, out_width,
2210                                 out_height);
2211
2212                 if (in_width > maxsinglelinewidth)
2213                         if (in_height > out_height &&
2214                                                 in_height < out_height * 2)
2215                                 *five_taps = false;
2216                 if (!*five_taps)
2217                         *core_clk = dispc.feat->calc_core_clk(plane, in_width,
2218                                         in_height, out_width, out_height,
2219                                         mem_to_mem);
2220
2221                 error = (error || in_width > maxsinglelinewidth * 2 ||
2222                         (in_width > maxsinglelinewidth && *five_taps) ||
2223                         !*core_clk || *core_clk > dispc_core_clk_rate());
2224                 if (error) {
2225                         if (*decim_x == *decim_y) {
2226                                 *decim_x = min_factor;
2227                                 ++*decim_y;
2228                         } else {
2229                                 swap(*decim_x, *decim_y);
2230                                 if (*decim_x < *decim_y)
2231                                         ++*decim_x;
2232                         }
2233                 }
2234         } while (*decim_x <= *x_predecim && *decim_y <= *y_predecim && error);
2235
2236         if (check_horiz_timing_omap3(plane, mgr_timings, pos_x, width, height,
2237                 out_width, out_height)){
2238                         DSSERR("horizontal timing too tight\n");
2239                         return -EINVAL;
2240         }
2241
2242         if (in_width > (maxsinglelinewidth * 2)) {
2243                 DSSERR("Cannot setup scaling");
2244                 DSSERR("width exceeds maximum width possible");
2245                 return -EINVAL;
2246         }
2247
2248         if (in_width > maxsinglelinewidth && *five_taps) {
2249                 DSSERR("cannot setup scaling with five taps");
2250                 return -EINVAL;
2251         }
2252         return 0;
2253 }
2254
2255 static int dispc_ovl_calc_scaling_44xx(enum omap_plane plane,
2256                 const struct omap_video_timings *mgr_timings,
2257                 u16 width, u16 height, u16 out_width, u16 out_height,
2258                 enum omap_color_mode color_mode, bool *five_taps,
2259                 int *x_predecim, int *y_predecim, int *decim_x, int *decim_y,
2260                 u16 pos_x, unsigned long *core_clk, bool mem_to_mem)
2261 {
2262         u16 in_width, in_width_max;
2263         int decim_x_min = *decim_x;
2264         u16 in_height = DIV_ROUND_UP(height, *decim_y);
2265         const int maxsinglelinewidth =
2266                                 dss_feat_get_param_max(FEAT_PARAM_LINEWIDTH);
2267         unsigned long pclk = dispc_plane_pclk_rate(plane);
2268         const int maxdownscale = dss_feat_get_param_max(FEAT_PARAM_DOWNSCALE);
2269
2270         if (mem_to_mem)
2271                 in_width_max = DIV_ROUND_UP(out_width, maxdownscale);
2272         else
2273                 in_width_max = dispc_core_clk_rate() /
2274                                         DIV_ROUND_UP(pclk, out_width);
2275
2276         *decim_x = DIV_ROUND_UP(width, in_width_max);
2277
2278         *decim_x = *decim_x > decim_x_min ? *decim_x : decim_x_min;
2279         if (*decim_x > *x_predecim)
2280                 return -EINVAL;
2281
2282         do {
2283                 in_width = DIV_ROUND_UP(width, *decim_x);
2284         } while (*decim_x <= *x_predecim &&
2285                         in_width > maxsinglelinewidth && ++*decim_x);
2286
2287         if (in_width > maxsinglelinewidth) {
2288                 DSSERR("Cannot scale width exceeds max line width");
2289                 return -EINVAL;
2290         }
2291
2292         *core_clk = dispc.feat->calc_core_clk(plane, in_width, in_height,
2293                                 out_width, out_height, mem_to_mem);
2294         return 0;
2295 }
2296
2297 static int dispc_ovl_calc_scaling(enum omap_plane plane,
2298                 enum omap_overlay_caps caps,
2299                 const struct omap_video_timings *mgr_timings,
2300                 u16 width, u16 height, u16 out_width, u16 out_height,
2301                 enum omap_color_mode color_mode, bool *five_taps,
2302                 int *x_predecim, int *y_predecim, u16 pos_x,
2303                 enum omap_dss_rotation_type rotation_type, bool mem_to_mem)
2304 {
2305         const int maxdownscale = dss_feat_get_param_max(FEAT_PARAM_DOWNSCALE);
2306         const int max_decim_limit = 16;
2307         unsigned long core_clk = 0;
2308         int decim_x, decim_y, ret;
2309
2310         if (width == out_width && height == out_height)
2311                 return 0;
2312
2313         if ((caps & OMAP_DSS_OVL_CAP_SCALE) == 0)
2314                 return -EINVAL;
2315
2316         *x_predecim = max_decim_limit;
2317         *y_predecim = (rotation_type == OMAP_DSS_ROT_TILER &&
2318                         dss_has_feature(FEAT_BURST_2D)) ? 2 : max_decim_limit;
2319
2320         if (color_mode == OMAP_DSS_COLOR_CLUT1 ||
2321             color_mode == OMAP_DSS_COLOR_CLUT2 ||
2322             color_mode == OMAP_DSS_COLOR_CLUT4 ||
2323             color_mode == OMAP_DSS_COLOR_CLUT8) {
2324                 *x_predecim = 1;
2325                 *y_predecim = 1;
2326                 *five_taps = false;
2327                 return 0;
2328         }
2329
2330         decim_x = DIV_ROUND_UP(DIV_ROUND_UP(width, out_width), maxdownscale);
2331         decim_y = DIV_ROUND_UP(DIV_ROUND_UP(height, out_height), maxdownscale);
2332
2333         if (decim_x > *x_predecim || out_width > width * 8)
2334                 return -EINVAL;
2335
2336         if (decim_y > *y_predecim || out_height > height * 8)
2337                 return -EINVAL;
2338
2339         ret = dispc.feat->calc_scaling(plane, mgr_timings, width, height,
2340                 out_width, out_height, color_mode, five_taps,
2341                 x_predecim, y_predecim, &decim_x, &decim_y, pos_x, &core_clk,
2342                 mem_to_mem);
2343         if (ret)
2344                 return ret;
2345
2346         DSSDBG("required core clk rate = %lu Hz\n", core_clk);
2347         DSSDBG("current core clk rate = %lu Hz\n", dispc_core_clk_rate());
2348
2349         if (!core_clk || core_clk > dispc_core_clk_rate()) {
2350                 DSSERR("failed to set up scaling, "
2351                         "required core clk rate = %lu Hz, "
2352                         "current core clk rate = %lu Hz\n",
2353                         core_clk, dispc_core_clk_rate());
2354                 return -EINVAL;
2355         }
2356
2357         *x_predecim = decim_x;
2358         *y_predecim = decim_y;
2359         return 0;
2360 }
2361
2362 static int dispc_ovl_setup_common(enum omap_plane plane,
2363                 enum omap_overlay_caps caps, u32 paddr, u32 p_uv_addr,
2364                 u16 screen_width, int pos_x, int pos_y, u16 width, u16 height,
2365                 u16 out_width, u16 out_height, enum omap_color_mode color_mode,
2366                 u8 rotation, bool mirror, u8 zorder, u8 pre_mult_alpha,
2367                 u8 global_alpha, enum omap_dss_rotation_type rotation_type,
2368                 bool replication, const struct omap_video_timings *mgr_timings,
2369                 bool mem_to_mem)
2370 {
2371         bool five_taps = true;
2372         bool fieldmode = 0;
2373         int r, cconv = 0;
2374         unsigned offset0, offset1;
2375         s32 row_inc;
2376         s32 pix_inc;
2377         u16 frame_height = height;
2378         unsigned int field_offset = 0;
2379         u16 in_height = height;
2380         u16 in_width = width;
2381         int x_predecim = 1, y_predecim = 1;
2382         bool ilace = mgr_timings->interlace;
2383
2384         if (paddr == 0)
2385                 return -EINVAL;
2386
2387         out_width = out_width == 0 ? width : out_width;
2388         out_height = out_height == 0 ? height : out_height;
2389
2390         if (ilace && height == out_height)
2391                 fieldmode = 1;
2392
2393         if (ilace) {
2394                 if (fieldmode)
2395                         in_height /= 2;
2396                 pos_y /= 2;
2397                 out_height /= 2;
2398
2399                 DSSDBG("adjusting for ilace: height %d, pos_y %d, "
2400                         "out_height %d\n", in_height, pos_y,
2401                         out_height);
2402         }
2403
2404         if (!dss_feat_color_mode_supported(plane, color_mode))
2405                 return -EINVAL;
2406
2407         r = dispc_ovl_calc_scaling(plane, caps, mgr_timings, in_width,
2408                         in_height, out_width, out_height, color_mode,
2409                         &five_taps, &x_predecim, &y_predecim, pos_x,
2410                         rotation_type, mem_to_mem);
2411         if (r)
2412                 return r;
2413
2414         in_width = DIV_ROUND_UP(in_width, x_predecim);
2415         in_height = DIV_ROUND_UP(in_height, y_predecim);
2416
2417         if (color_mode == OMAP_DSS_COLOR_YUV2 ||
2418                         color_mode == OMAP_DSS_COLOR_UYVY ||
2419                         color_mode == OMAP_DSS_COLOR_NV12)
2420                 cconv = 1;
2421
2422         if (ilace && !fieldmode) {
2423                 /*
2424                  * when downscaling the bottom field may have to start several
2425                  * source lines below the top field. Unfortunately ACCUI
2426                  * registers will only hold the fractional part of the offset
2427                  * so the integer part must be added to the base address of the
2428                  * bottom field.
2429                  */
2430                 if (!in_height || in_height == out_height)
2431                         field_offset = 0;
2432                 else
2433                         field_offset = in_height / out_height / 2;
2434         }
2435
2436         /* Fields are independent but interleaved in memory. */
2437         if (fieldmode)
2438                 field_offset = 1;
2439
2440         offset0 = 0;
2441         offset1 = 0;
2442         row_inc = 0;
2443         pix_inc = 0;
2444
2445         if (rotation_type == OMAP_DSS_ROT_TILER)
2446                 calc_tiler_rotation_offset(screen_width, in_width,
2447                                 color_mode, fieldmode, field_offset,
2448                                 &offset0, &offset1, &row_inc, &pix_inc,
2449                                 x_predecim, y_predecim);
2450         else if (rotation_type == OMAP_DSS_ROT_DMA)
2451                 calc_dma_rotation_offset(rotation, mirror,
2452                                 screen_width, in_width, frame_height,
2453                                 color_mode, fieldmode, field_offset,
2454                                 &offset0, &offset1, &row_inc, &pix_inc,
2455                                 x_predecim, y_predecim);
2456         else
2457                 calc_vrfb_rotation_offset(rotation, mirror,
2458                                 screen_width, in_width, frame_height,
2459                                 color_mode, fieldmode, field_offset,
2460                                 &offset0, &offset1, &row_inc, &pix_inc,
2461                                 x_predecim, y_predecim);
2462
2463         DSSDBG("offset0 %u, offset1 %u, row_inc %d, pix_inc %d\n",
2464                         offset0, offset1, row_inc, pix_inc);
2465
2466         dispc_ovl_set_color_mode(plane, color_mode);
2467
2468         dispc_ovl_configure_burst_type(plane, rotation_type);
2469
2470         dispc_ovl_set_ba0(plane, paddr + offset0);
2471         dispc_ovl_set_ba1(plane, paddr + offset1);
2472
2473         if (OMAP_DSS_COLOR_NV12 == color_mode) {
2474                 dispc_ovl_set_ba0_uv(plane, p_uv_addr + offset0);
2475                 dispc_ovl_set_ba1_uv(plane, p_uv_addr + offset1);
2476         }
2477
2478         dispc_ovl_set_row_inc(plane, row_inc);
2479         dispc_ovl_set_pix_inc(plane, pix_inc);
2480
2481         DSSDBG("%d,%d %dx%d -> %dx%d\n", pos_x, pos_y, in_width,
2482                         in_height, out_width, out_height);
2483
2484         dispc_ovl_set_pos(plane, caps, pos_x, pos_y);
2485
2486         dispc_ovl_set_input_size(plane, in_width, in_height);
2487
2488         if (caps & OMAP_DSS_OVL_CAP_SCALE) {
2489                 dispc_ovl_set_scaling(plane, in_width, in_height, out_width,
2490                                    out_height, ilace, five_taps, fieldmode,
2491                                    color_mode, rotation);
2492                 dispc_ovl_set_output_size(plane, out_width, out_height);
2493                 dispc_ovl_set_vid_color_conv(plane, cconv);
2494         }
2495
2496         dispc_ovl_set_rotation_attrs(plane, rotation, mirror, color_mode);
2497
2498         dispc_ovl_set_zorder(plane, caps, zorder);
2499         dispc_ovl_set_pre_mult_alpha(plane, caps, pre_mult_alpha);
2500         dispc_ovl_setup_global_alpha(plane, caps, global_alpha);
2501
2502         dispc_ovl_enable_replication(plane, caps, replication);
2503
2504         return 0;
2505 }
2506
2507 int dispc_ovl_setup(enum omap_plane plane, const struct omap_overlay_info *oi,
2508                 bool replication, const struct omap_video_timings *mgr_timings,
2509                 bool mem_to_mem)
2510 {
2511         int r;
2512         enum omap_overlay_caps caps = dss_feat_get_overlay_caps(plane);
2513         enum omap_channel channel;
2514
2515         channel = dispc_ovl_get_channel_out(plane);
2516
2517         DSSDBG("dispc_ovl_setup %d, pa %x, pa_uv %x, sw %d, %d,%d, %dx%d -> "
2518                 "%dx%d, cmode %x, rot %d, mir %d, chan %d repl %d\n",
2519                 plane, oi->paddr, oi->p_uv_addr, oi->screen_width, oi->pos_x,
2520                 oi->pos_y, oi->width, oi->height, oi->out_width, oi->out_height,
2521                 oi->color_mode, oi->rotation, oi->mirror, channel, replication);
2522
2523         r = dispc_ovl_setup_common(plane, caps, oi->paddr, oi->p_uv_addr,
2524                 oi->screen_width, oi->pos_x, oi->pos_y, oi->width, oi->height,
2525                 oi->out_width, oi->out_height, oi->color_mode, oi->rotation,
2526                 oi->mirror, oi->zorder, oi->pre_mult_alpha, oi->global_alpha,
2527                 oi->rotation_type, replication, mgr_timings, mem_to_mem);
2528
2529         return r;
2530 }
2531
2532 int dispc_wb_setup(const struct omap_dss_writeback_info *wi,
2533                 bool mem_to_mem, const struct omap_video_timings *mgr_timings)
2534 {
2535         int r;
2536         u32 l;
2537         enum omap_plane plane = OMAP_DSS_WB;
2538         const int pos_x = 0, pos_y = 0;
2539         const u8 zorder = 0, global_alpha = 0;
2540         const bool replication = false;
2541         bool truncation;
2542         int in_width = mgr_timings->x_res;
2543         int in_height = mgr_timings->y_res;
2544         enum omap_overlay_caps caps =
2545                 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA;
2546
2547         DSSDBG("dispc_wb_setup, pa %x, pa_uv %x, %d,%d -> %dx%d, cmode %x, "
2548                 "rot %d, mir %d\n", wi->paddr, wi->p_uv_addr, in_width,
2549                 in_height, wi->width, wi->height, wi->color_mode, wi->rotation,
2550                 wi->mirror);
2551
2552         r = dispc_ovl_setup_common(plane, caps, wi->paddr, wi->p_uv_addr,
2553                 wi->buf_width, pos_x, pos_y, in_width, in_height, wi->width,
2554                 wi->height, wi->color_mode, wi->rotation, wi->mirror, zorder,
2555                 wi->pre_mult_alpha, global_alpha, wi->rotation_type,
2556                 replication, mgr_timings, mem_to_mem);
2557
2558         switch (wi->color_mode) {
2559         case OMAP_DSS_COLOR_RGB16:
2560         case OMAP_DSS_COLOR_RGB24P:
2561         case OMAP_DSS_COLOR_ARGB16:
2562         case OMAP_DSS_COLOR_RGBA16:
2563         case OMAP_DSS_COLOR_RGB12U:
2564         case OMAP_DSS_COLOR_ARGB16_1555:
2565         case OMAP_DSS_COLOR_XRGB16_1555:
2566         case OMAP_DSS_COLOR_RGBX16:
2567                 truncation = true;
2568                 break;
2569         default:
2570                 truncation = false;
2571                 break;
2572         }
2573
2574         /* setup extra DISPC_WB_ATTRIBUTES */
2575         l = dispc_read_reg(DISPC_OVL_ATTRIBUTES(plane));
2576         l = FLD_MOD(l, truncation, 10, 10);     /* TRUNCATIONENABLE */
2577         l = FLD_MOD(l, mem_to_mem, 19, 19);     /* WRITEBACKMODE */
2578         dispc_write_reg(DISPC_OVL_ATTRIBUTES(plane), l);
2579
2580         return r;
2581 }
2582
2583 int dispc_ovl_enable(enum omap_plane plane, bool enable)
2584 {
2585         DSSDBG("dispc_enable_plane %d, %d\n", plane, enable);
2586
2587         REG_FLD_MOD(DISPC_OVL_ATTRIBUTES(plane), enable ? 1 : 0, 0, 0);