Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2009 Nokia Corporation
4 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
5 *
6 * Some code and ideas taken from drivers/video/omap/ driver
7 * by Imre Deak.
8 */
9
10#define DSS_SUBSYS_NAME "DISPC"
11
12#include <linux/kernel.h>
13#include <linux/dma-mapping.h>
14#include <linux/vmalloc.h>
15#include <linux/export.h>
16#include <linux/clk.h>
17#include <linux/io.h>
18#include <linux/jiffies.h>
19#include <linux/seq_file.h>
20#include <linux/delay.h>
21#include <linux/workqueue.h>
22#include <linux/hardirq.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/property.h>
26#include <linux/sizes.h>
27#include <linux/mfd/syscon.h>
28#include <linux/regmap.h>
29#include <linux/of.h>
30#include <linux/component.h>
31#include <linux/sys_soc.h>
32#include <drm/drm_fourcc.h>
33#include <drm/drm_blend.h>
34
35#include "omapdss.h"
36#include "dss.h"
37#include "dispc.h"
38
39struct dispc_device;
40
41/* DISPC */
42#define DISPC_SZ_REGS SZ_4K
43
44enum omap_burst_size {
45 BURST_SIZE_X2 = 0,
46 BURST_SIZE_X4 = 1,
47 BURST_SIZE_X8 = 2,
48};
49
50#define REG_GET(dispc, idx, start, end) \
51 FLD_GET(dispc_read_reg(dispc, idx), start, end)
52
53#define REG_FLD_MOD(dispc, idx, val, start, end) \
54 dispc_write_reg(dispc, idx, \
55 FLD_MOD(dispc_read_reg(dispc, idx), val, start, end))
56
57/* DISPC has feature id */
58enum dispc_feature_id {
59 FEAT_LCDENABLEPOL,
60 FEAT_LCDENABLESIGNAL,
61 FEAT_PCKFREEENABLE,
62 FEAT_FUNCGATED,
63 FEAT_MGR_LCD2,
64 FEAT_MGR_LCD3,
65 FEAT_LINEBUFFERSPLIT,
66 FEAT_ROWREPEATENABLE,
67 FEAT_RESIZECONF,
68 /* Independent core clk divider */
69 FEAT_CORE_CLK_DIV,
70 FEAT_HANDLE_UV_SEPARATE,
71 FEAT_ATTR2,
72 FEAT_CPR,
73 FEAT_PRELOAD,
74 FEAT_FIR_COEF_V,
75 FEAT_ALPHA_FIXED_ZORDER,
76 FEAT_ALPHA_FREE_ZORDER,
77 FEAT_FIFO_MERGE,
78 /* An unknown HW bug causing the normal FIFO thresholds not to work */
79 FEAT_OMAP3_DSI_FIFO_BUG,
80 FEAT_BURST_2D,
81 FEAT_MFLAG,
82};
83
84struct dispc_features {
85 u8 sw_start;
86 u8 fp_start;
87 u8 bp_start;
88 u16 sw_max;
89 u16 vp_max;
90 u16 hp_max;
91 u8 mgr_width_start;
92 u8 mgr_height_start;
93 u16 mgr_width_max;
94 u16 mgr_height_max;
95 u16 ovl_width_max;
96 u16 ovl_height_max;
97 unsigned long max_lcd_pclk;
98 unsigned long max_tv_pclk;
99 unsigned int max_downscale;
100 unsigned int max_line_width;
101 unsigned int min_pcd;
102 int (*calc_scaling)(struct dispc_device *dispc,
103 unsigned long pclk, unsigned long lclk,
104 const struct videomode *vm,
105 u16 width, u16 height, u16 out_width, u16 out_height,
106 u32 fourcc, bool *five_taps,
107 int *x_predecim, int *y_predecim, int *decim_x, int *decim_y,
108 u16 pos_x, unsigned long *core_clk, bool mem_to_mem);
109 unsigned long (*calc_core_clk) (unsigned long pclk,
110 u16 width, u16 height, u16 out_width, u16 out_height,
111 bool mem_to_mem);
112 u8 num_fifos;
113 const enum dispc_feature_id *features;
114 unsigned int num_features;
115 const struct dss_reg_field *reg_fields;
116 const unsigned int num_reg_fields;
117 const enum omap_overlay_caps *overlay_caps;
118 const u32 **supported_color_modes;
119 const u32 *supported_scaler_color_modes;
120 unsigned int num_mgrs;
121 unsigned int num_ovls;
122 unsigned int buffer_size_unit;
123 unsigned int burst_size_unit;
124
125 /* swap GFX & WB fifos */
126 bool gfx_fifo_workaround:1;
127
128 /* no DISPC_IRQ_FRAMEDONETV on this SoC */
129 bool no_framedone_tv:1;
130
131 /* revert to the OMAP4 mechanism of DISPC Smart Standby operation */
132 bool mstandby_workaround:1;
133
134 bool set_max_preload:1;
135
136 /* PIXEL_INC is not added to the last pixel of a line */
137 bool last_pixel_inc_missing:1;
138
139 /* POL_FREQ has ALIGN bit */
140 bool supports_sync_align:1;
141
142 bool has_writeback:1;
143
144 bool supports_double_pixel:1;
145
146 /*
147 * Field order for VENC is different than HDMI. We should handle this in
148 * some intelligent manner, but as the SoCs have either HDMI or VENC,
149 * never both, we can just use this flag for now.
150 */
151 bool reverse_ilace_field_order:1;
152
153 bool has_gamma_table:1;
154
155 bool has_gamma_i734_bug:1;
156};
157
158#define DISPC_MAX_NR_FIFOS 5
159#define DISPC_MAX_CHANNEL_GAMMA 4
160
161struct dispc_device {
162 struct platform_device *pdev;
163 void __iomem *base;
164 struct dss_device *dss;
165
166 struct dss_debugfs_entry *debugfs;
167
168 int irq;
169 irq_handler_t user_handler;
170 void *user_data;
171
172 unsigned long core_clk_rate;
173 unsigned long tv_pclk_rate;
174
175 u32 fifo_size[DISPC_MAX_NR_FIFOS];
176 /* maps which plane is using a fifo. fifo-id -> plane-id */
177 int fifo_assignment[DISPC_MAX_NR_FIFOS];
178
179 bool ctx_valid;
180 u32 ctx[DISPC_SZ_REGS / sizeof(u32)];
181
182 u32 *gamma_table[DISPC_MAX_CHANNEL_GAMMA];
183
184 const struct dispc_features *feat;
185
186 bool is_enabled;
187
188 struct regmap *syscon_pol;
189 u32 syscon_pol_offset;
190};
191
192enum omap_color_component {
193 /* used for all color formats for OMAP3 and earlier
194 * and for RGB and Y color component on OMAP4
195 */
196 DISPC_COLOR_COMPONENT_RGB_Y = 1 << 0,
197 /* used for UV component for
198 * DRM_FORMAT_YUYV, DRM_FORMAT_UYVY, DRM_FORMAT_NV12
199 * color formats on OMAP4
200 */
201 DISPC_COLOR_COMPONENT_UV = 1 << 1,
202};
203
204enum mgr_reg_fields {
205 DISPC_MGR_FLD_ENABLE,
206 DISPC_MGR_FLD_STNTFT,
207 DISPC_MGR_FLD_GO,
208 DISPC_MGR_FLD_TFTDATALINES,
209 DISPC_MGR_FLD_STALLMODE,
210 DISPC_MGR_FLD_TCKENABLE,
211 DISPC_MGR_FLD_TCKSELECTION,
212 DISPC_MGR_FLD_CPR,
213 DISPC_MGR_FLD_FIFOHANDCHECK,
214 /* used to maintain a count of the above fields */
215 DISPC_MGR_FLD_NUM,
216};
217
218/* DISPC register field id */
219enum dispc_feat_reg_field {
220 FEAT_REG_FIRHINC,
221 FEAT_REG_FIRVINC,
222 FEAT_REG_FIFOHIGHTHRESHOLD,
223 FEAT_REG_FIFOLOWTHRESHOLD,
224 FEAT_REG_FIFOSIZE,
225 FEAT_REG_HORIZONTALACCU,
226 FEAT_REG_VERTICALACCU,
227};
228
229struct dispc_reg_field {
230 u16 reg;
231 u8 high;
232 u8 low;
233};
234
235struct dispc_gamma_desc {
236 u32 len;
237 u32 bits;
238 u16 reg;
239 bool has_index;
240};
241
242static const struct {
243 const char *name;
244 u32 vsync_irq;
245 u32 framedone_irq;
246 u32 sync_lost_irq;
247 struct dispc_gamma_desc gamma;
248 struct dispc_reg_field reg_desc[DISPC_MGR_FLD_NUM];
249} mgr_desc[] = {
250 [OMAP_DSS_CHANNEL_LCD] = {
251 .name = "LCD",
252 .vsync_irq = DISPC_IRQ_VSYNC,
253 .framedone_irq = DISPC_IRQ_FRAMEDONE,
254 .sync_lost_irq = DISPC_IRQ_SYNC_LOST,
255 .gamma = {
256 .len = 256,
257 .bits = 8,
258 .reg = DISPC_GAMMA_TABLE0,
259 .has_index = true,
260 },
261 .reg_desc = {
262 [DISPC_MGR_FLD_ENABLE] = { DISPC_CONTROL, 0, 0 },
263 [DISPC_MGR_FLD_STNTFT] = { DISPC_CONTROL, 3, 3 },
264 [DISPC_MGR_FLD_GO] = { DISPC_CONTROL, 5, 5 },
265 [DISPC_MGR_FLD_TFTDATALINES] = { DISPC_CONTROL, 9, 8 },
266 [DISPC_MGR_FLD_STALLMODE] = { DISPC_CONTROL, 11, 11 },
267 [DISPC_MGR_FLD_TCKENABLE] = { DISPC_CONFIG, 10, 10 },
268 [DISPC_MGR_FLD_TCKSELECTION] = { DISPC_CONFIG, 11, 11 },
269 [DISPC_MGR_FLD_CPR] = { DISPC_CONFIG, 15, 15 },
270 [DISPC_MGR_FLD_FIFOHANDCHECK] = { DISPC_CONFIG, 16, 16 },
271 },
272 },
273 [OMAP_DSS_CHANNEL_DIGIT] = {
274 .name = "DIGIT",
275 .vsync_irq = DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_EVSYNC_EVEN,
276 .framedone_irq = DISPC_IRQ_FRAMEDONETV,
277 .sync_lost_irq = DISPC_IRQ_SYNC_LOST_DIGIT,
278 .gamma = {
279 .len = 1024,
280 .bits = 10,
281 .reg = DISPC_GAMMA_TABLE2,
282 .has_index = false,
283 },
284 .reg_desc = {
285 [DISPC_MGR_FLD_ENABLE] = { DISPC_CONTROL, 1, 1 },
286 [DISPC_MGR_FLD_STNTFT] = { },
287 [DISPC_MGR_FLD_GO] = { DISPC_CONTROL, 6, 6 },
288 [DISPC_MGR_FLD_TFTDATALINES] = { },
289 [DISPC_MGR_FLD_STALLMODE] = { },
290 [DISPC_MGR_FLD_TCKENABLE] = { DISPC_CONFIG, 12, 12 },
291 [DISPC_MGR_FLD_TCKSELECTION] = { DISPC_CONFIG, 13, 13 },
292 [DISPC_MGR_FLD_CPR] = { },
293 [DISPC_MGR_FLD_FIFOHANDCHECK] = { DISPC_CONFIG, 16, 16 },
294 },
295 },
296 [OMAP_DSS_CHANNEL_LCD2] = {
297 .name = "LCD2",
298 .vsync_irq = DISPC_IRQ_VSYNC2,
299 .framedone_irq = DISPC_IRQ_FRAMEDONE2,
300 .sync_lost_irq = DISPC_IRQ_SYNC_LOST2,
301 .gamma = {
302 .len = 256,
303 .bits = 8,
304 .reg = DISPC_GAMMA_TABLE1,
305 .has_index = true,
306 },
307 .reg_desc = {
308 [DISPC_MGR_FLD_ENABLE] = { DISPC_CONTROL2, 0, 0 },
309 [DISPC_MGR_FLD_STNTFT] = { DISPC_CONTROL2, 3, 3 },
310 [DISPC_MGR_FLD_GO] = { DISPC_CONTROL2, 5, 5 },
311 [DISPC_MGR_FLD_TFTDATALINES] = { DISPC_CONTROL2, 9, 8 },
312 [DISPC_MGR_FLD_STALLMODE] = { DISPC_CONTROL2, 11, 11 },
313 [DISPC_MGR_FLD_TCKENABLE] = { DISPC_CONFIG2, 10, 10 },
314 [DISPC_MGR_FLD_TCKSELECTION] = { DISPC_CONFIG2, 11, 11 },
315 [DISPC_MGR_FLD_CPR] = { DISPC_CONFIG2, 15, 15 },
316 [DISPC_MGR_FLD_FIFOHANDCHECK] = { DISPC_CONFIG2, 16, 16 },
317 },
318 },
319 [OMAP_DSS_CHANNEL_LCD3] = {
320 .name = "LCD3",
321 .vsync_irq = DISPC_IRQ_VSYNC3,
322 .framedone_irq = DISPC_IRQ_FRAMEDONE3,
323 .sync_lost_irq = DISPC_IRQ_SYNC_LOST3,
324 .gamma = {
325 .len = 256,
326 .bits = 8,
327 .reg = DISPC_GAMMA_TABLE3,
328 .has_index = true,
329 },
330 .reg_desc = {
331 [DISPC_MGR_FLD_ENABLE] = { DISPC_CONTROL3, 0, 0 },
332 [DISPC_MGR_FLD_STNTFT] = { DISPC_CONTROL3, 3, 3 },
333 [DISPC_MGR_FLD_GO] = { DISPC_CONTROL3, 5, 5 },
334 [DISPC_MGR_FLD_TFTDATALINES] = { DISPC_CONTROL3, 9, 8 },
335 [DISPC_MGR_FLD_STALLMODE] = { DISPC_CONTROL3, 11, 11 },
336 [DISPC_MGR_FLD_TCKENABLE] = { DISPC_CONFIG3, 10, 10 },
337 [DISPC_MGR_FLD_TCKSELECTION] = { DISPC_CONFIG3, 11, 11 },
338 [DISPC_MGR_FLD_CPR] = { DISPC_CONFIG3, 15, 15 },
339 [DISPC_MGR_FLD_FIFOHANDCHECK] = { DISPC_CONFIG3, 16, 16 },
340 },
341 },
342};
343
344static unsigned long dispc_fclk_rate(struct dispc_device *dispc);
345static unsigned long dispc_core_clk_rate(struct dispc_device *dispc);
346static unsigned long dispc_mgr_lclk_rate(struct dispc_device *dispc,
347 enum omap_channel channel);
348static unsigned long dispc_mgr_pclk_rate(struct dispc_device *dispc,
349 enum omap_channel channel);
350
351static unsigned long dispc_plane_pclk_rate(struct dispc_device *dispc,
352 enum omap_plane_id plane);
353static unsigned long dispc_plane_lclk_rate(struct dispc_device *dispc,
354 enum omap_plane_id plane);
355
356static inline void dispc_write_reg(struct dispc_device *dispc, u16 idx, u32 val)
357{
358 __raw_writel(val, dispc->base + idx);
359}
360
361static inline u32 dispc_read_reg(struct dispc_device *dispc, u16 idx)
362{
363 return __raw_readl(dispc->base + idx);
364}
365
366static u32 mgr_fld_read(struct dispc_device *dispc, enum omap_channel channel,
367 enum mgr_reg_fields regfld)
368{
369 const struct dispc_reg_field *rfld = &mgr_desc[channel].reg_desc[regfld];
370
371 return REG_GET(dispc, rfld->reg, rfld->high, rfld->low);
372}
373
374static void mgr_fld_write(struct dispc_device *dispc, enum omap_channel channel,
375 enum mgr_reg_fields regfld, int val)
376{
377 const struct dispc_reg_field *rfld = &mgr_desc[channel].reg_desc[regfld];
378
379 REG_FLD_MOD(dispc, rfld->reg, val, rfld->high, rfld->low);
380}
381
382int dispc_get_num_ovls(struct dispc_device *dispc)
383{
384 return dispc->feat->num_ovls;
385}
386
387int dispc_get_num_mgrs(struct dispc_device *dispc)
388{
389 return dispc->feat->num_mgrs;
390}
391
392static void dispc_get_reg_field(struct dispc_device *dispc,
393 enum dispc_feat_reg_field id,
394 u8 *start, u8 *end)
395{
396 BUG_ON(id >= dispc->feat->num_reg_fields);
397
398 *start = dispc->feat->reg_fields[id].start;
399 *end = dispc->feat->reg_fields[id].end;
400}
401
402static bool dispc_has_feature(struct dispc_device *dispc,
403 enum dispc_feature_id id)
404{
405 unsigned int i;
406
407 for (i = 0; i < dispc->feat->num_features; i++) {
408 if (dispc->feat->features[i] == id)
409 return true;
410 }
411
412 return false;
413}
414
415#define SR(dispc, reg) \
416 dispc->ctx[DISPC_##reg / sizeof(u32)] = dispc_read_reg(dispc, DISPC_##reg)
417#define RR(dispc, reg) \
418 dispc_write_reg(dispc, DISPC_##reg, dispc->ctx[DISPC_##reg / sizeof(u32)])
419
420static void dispc_save_context(struct dispc_device *dispc)
421{
422 int i, j;
423
424 DSSDBG("dispc_save_context\n");
425
426 SR(dispc, IRQENABLE);
427 SR(dispc, CONTROL);
428 SR(dispc, CONFIG);
429 SR(dispc, LINE_NUMBER);
430 if (dispc_has_feature(dispc, FEAT_ALPHA_FIXED_ZORDER) ||
431 dispc_has_feature(dispc, FEAT_ALPHA_FREE_ZORDER))
432 SR(dispc, GLOBAL_ALPHA);
433 if (dispc_has_feature(dispc, FEAT_MGR_LCD2)) {
434 SR(dispc, CONTROL2);
435 SR(dispc, CONFIG2);
436 }
437 if (dispc_has_feature(dispc, FEAT_MGR_LCD3)) {
438 SR(dispc, CONTROL3);
439 SR(dispc, CONFIG3);
440 }
441
442 for (i = 0; i < dispc_get_num_mgrs(dispc); i++) {
443 SR(dispc, DEFAULT_COLOR(i));
444 SR(dispc, TRANS_COLOR(i));
445 SR(dispc, SIZE_MGR(i));
446 if (i == OMAP_DSS_CHANNEL_DIGIT)
447 continue;
448 SR(dispc, TIMING_H(i));
449 SR(dispc, TIMING_V(i));
450 SR(dispc, POL_FREQ(i));
451 SR(dispc, DIVISORo(i));
452
453 SR(dispc, DATA_CYCLE1(i));
454 SR(dispc, DATA_CYCLE2(i));
455 SR(dispc, DATA_CYCLE3(i));
456
457 if (dispc_has_feature(dispc, FEAT_CPR)) {
458 SR(dispc, CPR_COEF_R(i));
459 SR(dispc, CPR_COEF_G(i));
460 SR(dispc, CPR_COEF_B(i));
461 }
462 }
463
464 for (i = 0; i < dispc_get_num_ovls(dispc); i++) {
465 SR(dispc, OVL_BA0(i));
466 SR(dispc, OVL_BA1(i));
467 SR(dispc, OVL_POSITION(i));
468 SR(dispc, OVL_SIZE(i));
469 SR(dispc, OVL_ATTRIBUTES(i));
470 SR(dispc, OVL_FIFO_THRESHOLD(i));
471 SR(dispc, OVL_ROW_INC(i));
472 SR(dispc, OVL_PIXEL_INC(i));
473 if (dispc_has_feature(dispc, FEAT_PRELOAD))
474 SR(dispc, OVL_PRELOAD(i));
475 if (i == OMAP_DSS_GFX) {
476 SR(dispc, OVL_WINDOW_SKIP(i));
477 SR(dispc, OVL_TABLE_BA(i));
478 continue;
479 }
480 SR(dispc, OVL_FIR(i));
481 SR(dispc, OVL_PICTURE_SIZE(i));
482 SR(dispc, OVL_ACCU0(i));
483 SR(dispc, OVL_ACCU1(i));
484
485 for (j = 0; j < 8; j++)
486 SR(dispc, OVL_FIR_COEF_H(i, j));
487
488 for (j = 0; j < 8; j++)
489 SR(dispc, OVL_FIR_COEF_HV(i, j));
490
491 for (j = 0; j < 5; j++)
492 SR(dispc, OVL_CONV_COEF(i, j));
493
494 if (dispc_has_feature(dispc, FEAT_FIR_COEF_V)) {
495 for (j = 0; j < 8; j++)
496 SR(dispc, OVL_FIR_COEF_V(i, j));
497 }
498
499 if (dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE)) {
500 SR(dispc, OVL_BA0_UV(i));
501 SR(dispc, OVL_BA1_UV(i));
502 SR(dispc, OVL_FIR2(i));
503 SR(dispc, OVL_ACCU2_0(i));
504 SR(dispc, OVL_ACCU2_1(i));
505
506 for (j = 0; j < 8; j++)
507 SR(dispc, OVL_FIR_COEF_H2(i, j));
508
509 for (j = 0; j < 8; j++)
510 SR(dispc, OVL_FIR_COEF_HV2(i, j));
511
512 for (j = 0; j < 8; j++)
513 SR(dispc, OVL_FIR_COEF_V2(i, j));
514 }
515 if (dispc_has_feature(dispc, FEAT_ATTR2))
516 SR(dispc, OVL_ATTRIBUTES2(i));
517 }
518
519 if (dispc_has_feature(dispc, FEAT_CORE_CLK_DIV))
520 SR(dispc, DIVISOR);
521
522 dispc->ctx_valid = true;
523
524 DSSDBG("context saved\n");
525}
526
527static void dispc_restore_context(struct dispc_device *dispc)
528{
529 int i, j;
530
531 DSSDBG("dispc_restore_context\n");
532
533 if (!dispc->ctx_valid)
534 return;
535
536 /*RR(dispc, IRQENABLE);*/
537 /*RR(dispc, CONTROL);*/
538 RR(dispc, CONFIG);
539 RR(dispc, LINE_NUMBER);
540 if (dispc_has_feature(dispc, FEAT_ALPHA_FIXED_ZORDER) ||
541 dispc_has_feature(dispc, FEAT_ALPHA_FREE_ZORDER))
542 RR(dispc, GLOBAL_ALPHA);
543 if (dispc_has_feature(dispc, FEAT_MGR_LCD2))
544 RR(dispc, CONFIG2);
545 if (dispc_has_feature(dispc, FEAT_MGR_LCD3))
546 RR(dispc, CONFIG3);
547
548 for (i = 0; i < dispc_get_num_mgrs(dispc); i++) {
549 RR(dispc, DEFAULT_COLOR(i));
550 RR(dispc, TRANS_COLOR(i));
551 RR(dispc, SIZE_MGR(i));
552 if (i == OMAP_DSS_CHANNEL_DIGIT)
553 continue;
554 RR(dispc, TIMING_H(i));
555 RR(dispc, TIMING_V(i));
556 RR(dispc, POL_FREQ(i));
557 RR(dispc, DIVISORo(i));
558
559 RR(dispc, DATA_CYCLE1(i));
560 RR(dispc, DATA_CYCLE2(i));
561 RR(dispc, DATA_CYCLE3(i));
562
563 if (dispc_has_feature(dispc, FEAT_CPR)) {
564 RR(dispc, CPR_COEF_R(i));
565 RR(dispc, CPR_COEF_G(i));
566 RR(dispc, CPR_COEF_B(i));
567 }
568 }
569
570 for (i = 0; i < dispc_get_num_ovls(dispc); i++) {
571 RR(dispc, OVL_BA0(i));
572 RR(dispc, OVL_BA1(i));
573 RR(dispc, OVL_POSITION(i));
574 RR(dispc, OVL_SIZE(i));
575 RR(dispc, OVL_ATTRIBUTES(i));
576 RR(dispc, OVL_FIFO_THRESHOLD(i));
577 RR(dispc, OVL_ROW_INC(i));
578 RR(dispc, OVL_PIXEL_INC(i));
579 if (dispc_has_feature(dispc, FEAT_PRELOAD))
580 RR(dispc, OVL_PRELOAD(i));
581 if (i == OMAP_DSS_GFX) {
582 RR(dispc, OVL_WINDOW_SKIP(i));
583 RR(dispc, OVL_TABLE_BA(i));
584 continue;
585 }
586 RR(dispc, OVL_FIR(i));
587 RR(dispc, OVL_PICTURE_SIZE(i));
588 RR(dispc, OVL_ACCU0(i));
589 RR(dispc, OVL_ACCU1(i));
590
591 for (j = 0; j < 8; j++)
592 RR(dispc, OVL_FIR_COEF_H(i, j));
593
594 for (j = 0; j < 8; j++)
595 RR(dispc, OVL_FIR_COEF_HV(i, j));
596
597 for (j = 0; j < 5; j++)
598 RR(dispc, OVL_CONV_COEF(i, j));
599
600 if (dispc_has_feature(dispc, FEAT_FIR_COEF_V)) {
601 for (j = 0; j < 8; j++)
602 RR(dispc, OVL_FIR_COEF_V(i, j));
603 }
604
605 if (dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE)) {
606 RR(dispc, OVL_BA0_UV(i));
607 RR(dispc, OVL_BA1_UV(i));
608 RR(dispc, OVL_FIR2(i));
609 RR(dispc, OVL_ACCU2_0(i));
610 RR(dispc, OVL_ACCU2_1(i));
611
612 for (j = 0; j < 8; j++)
613 RR(dispc, OVL_FIR_COEF_H2(i, j));
614
615 for (j = 0; j < 8; j++)
616 RR(dispc, OVL_FIR_COEF_HV2(i, j));
617
618 for (j = 0; j < 8; j++)
619 RR(dispc, OVL_FIR_COEF_V2(i, j));
620 }
621 if (dispc_has_feature(dispc, FEAT_ATTR2))
622 RR(dispc, OVL_ATTRIBUTES2(i));
623 }
624
625 if (dispc_has_feature(dispc, FEAT_CORE_CLK_DIV))
626 RR(dispc, DIVISOR);
627
628 /* enable last, because LCD & DIGIT enable are here */
629 RR(dispc, CONTROL);
630 if (dispc_has_feature(dispc, FEAT_MGR_LCD2))
631 RR(dispc, CONTROL2);
632 if (dispc_has_feature(dispc, FEAT_MGR_LCD3))
633 RR(dispc, CONTROL3);
634 /* clear spurious SYNC_LOST_DIGIT interrupts */
635 dispc_clear_irqstatus(dispc, DISPC_IRQ_SYNC_LOST_DIGIT);
636
637 /*
638 * enable last so IRQs won't trigger before
639 * the context is fully restored
640 */
641 RR(dispc, IRQENABLE);
642
643 DSSDBG("context restored\n");
644}
645
646#undef SR
647#undef RR
648
649int dispc_runtime_get(struct dispc_device *dispc)
650{
651 int r;
652
653 DSSDBG("dispc_runtime_get\n");
654
655 r = pm_runtime_get_sync(&dispc->pdev->dev);
656 if (WARN_ON(r < 0)) {
657 pm_runtime_put_noidle(&dispc->pdev->dev);
658 return r;
659 }
660 return 0;
661}
662
663void dispc_runtime_put(struct dispc_device *dispc)
664{
665 int r;
666
667 DSSDBG("dispc_runtime_put\n");
668
669 r = pm_runtime_put_sync(&dispc->pdev->dev);
670 WARN_ON(r < 0 && r != -ENOSYS);
671}
672
673u32 dispc_mgr_get_vsync_irq(struct dispc_device *dispc,
674 enum omap_channel channel)
675{
676 return mgr_desc[channel].vsync_irq;
677}
678
679u32 dispc_mgr_get_framedone_irq(struct dispc_device *dispc,
680 enum omap_channel channel)
681{
682 if (channel == OMAP_DSS_CHANNEL_DIGIT && dispc->feat->no_framedone_tv)
683 return 0;
684
685 return mgr_desc[channel].framedone_irq;
686}
687
688u32 dispc_mgr_get_sync_lost_irq(struct dispc_device *dispc,
689 enum omap_channel channel)
690{
691 return mgr_desc[channel].sync_lost_irq;
692}
693
694void dispc_mgr_enable(struct dispc_device *dispc,
695 enum omap_channel channel, bool enable)
696{
697 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_ENABLE, enable);
698 /* flush posted write */
699 mgr_fld_read(dispc, channel, DISPC_MGR_FLD_ENABLE);
700}
701
702static bool dispc_mgr_is_enabled(struct dispc_device *dispc,
703 enum omap_channel channel)
704{
705 return !!mgr_fld_read(dispc, channel, DISPC_MGR_FLD_ENABLE);
706}
707
708bool dispc_mgr_go_busy(struct dispc_device *dispc,
709 enum omap_channel channel)
710{
711 return mgr_fld_read(dispc, channel, DISPC_MGR_FLD_GO) == 1;
712}
713
714void dispc_mgr_go(struct dispc_device *dispc, enum omap_channel channel)
715{
716 WARN_ON(!dispc_mgr_is_enabled(dispc, channel));
717 WARN_ON(dispc_mgr_go_busy(dispc, channel));
718
719 DSSDBG("GO %s\n", mgr_desc[channel].name);
720
721 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_GO, 1);
722}
723
724static void dispc_ovl_write_firh_reg(struct dispc_device *dispc,
725 enum omap_plane_id plane, int reg,
726 u32 value)
727{
728 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_H(plane, reg), value);
729}
730
731static void dispc_ovl_write_firhv_reg(struct dispc_device *dispc,
732 enum omap_plane_id plane, int reg,
733 u32 value)
734{
735 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_HV(plane, reg), value);
736}
737
738static void dispc_ovl_write_firv_reg(struct dispc_device *dispc,
739 enum omap_plane_id plane, int reg,
740 u32 value)
741{
742 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_V(plane, reg), value);
743}
744
745static void dispc_ovl_write_firh2_reg(struct dispc_device *dispc,
746 enum omap_plane_id plane, int reg,
747 u32 value)
748{
749 BUG_ON(plane == OMAP_DSS_GFX);
750
751 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_H2(plane, reg), value);
752}
753
754static void dispc_ovl_write_firhv2_reg(struct dispc_device *dispc,
755 enum omap_plane_id plane, int reg,
756 u32 value)
757{
758 BUG_ON(plane == OMAP_DSS_GFX);
759
760 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_HV2(plane, reg), value);
761}
762
763static void dispc_ovl_write_firv2_reg(struct dispc_device *dispc,
764 enum omap_plane_id plane, int reg,
765 u32 value)
766{
767 BUG_ON(plane == OMAP_DSS_GFX);
768
769 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_V2(plane, reg), value);
770}
771
772static void dispc_ovl_set_scale_coef(struct dispc_device *dispc,
773 enum omap_plane_id plane, int fir_hinc,
774 int fir_vinc, int five_taps,
775 enum omap_color_component color_comp)
776{
777 const struct dispc_coef *h_coef, *v_coef;
778 int i;
779
780 h_coef = dispc_ovl_get_scale_coef(fir_hinc, true);
781 v_coef = dispc_ovl_get_scale_coef(fir_vinc, five_taps);
782
783 if (!h_coef || !v_coef) {
784 dev_err(&dispc->pdev->dev, "%s: failed to find scale coefs\n",
785 __func__);
786 return;
787 }
788
789 for (i = 0; i < 8; i++) {
790 u32 h, hv;
791
792 h = FLD_VAL(h_coef[i].hc0_vc00, 7, 0)
793 | FLD_VAL(h_coef[i].hc1_vc0, 15, 8)
794 | FLD_VAL(h_coef[i].hc2_vc1, 23, 16)
795 | FLD_VAL(h_coef[i].hc3_vc2, 31, 24);
796 hv = FLD_VAL(h_coef[i].hc4_vc22, 7, 0)
797 | FLD_VAL(v_coef[i].hc1_vc0, 15, 8)
798 | FLD_VAL(v_coef[i].hc2_vc1, 23, 16)
799 | FLD_VAL(v_coef[i].hc3_vc2, 31, 24);
800
801 if (color_comp == DISPC_COLOR_COMPONENT_RGB_Y) {
802 dispc_ovl_write_firh_reg(dispc, plane, i, h);
803 dispc_ovl_write_firhv_reg(dispc, plane, i, hv);
804 } else {
805 dispc_ovl_write_firh2_reg(dispc, plane, i, h);
806 dispc_ovl_write_firhv2_reg(dispc, plane, i, hv);
807 }
808
809 }
810
811 if (five_taps) {
812 for (i = 0; i < 8; i++) {
813 u32 v;
814 v = FLD_VAL(v_coef[i].hc0_vc00, 7, 0)
815 | FLD_VAL(v_coef[i].hc4_vc22, 15, 8);
816 if (color_comp == DISPC_COLOR_COMPONENT_RGB_Y)
817 dispc_ovl_write_firv_reg(dispc, plane, i, v);
818 else
819 dispc_ovl_write_firv2_reg(dispc, plane, i, v);
820 }
821 }
822}
823
824struct csc_coef_yuv2rgb {
825 int ry, rcb, rcr, gy, gcb, gcr, by, bcb, bcr;
826 bool full_range;
827};
828
829static void dispc_ovl_write_color_conv_coef(struct dispc_device *dispc,
830 enum omap_plane_id plane,
831 const struct csc_coef_yuv2rgb *ct)
832{
833#define CVAL(x, y) (FLD_VAL(x, 26, 16) | FLD_VAL(y, 10, 0))
834
835 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 0), CVAL(ct->rcr, ct->ry));
836 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 1), CVAL(ct->gy, ct->rcb));
837 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 2), CVAL(ct->gcb, ct->gcr));
838 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 3), CVAL(ct->bcr, ct->by));
839 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 4), CVAL(0, ct->bcb));
840
841 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), ct->full_range, 11, 11);
842
843#undef CVAL
844}
845
846/* YUV -> RGB, ITU-R BT.601, full range */
847static const struct csc_coef_yuv2rgb coefs_yuv2rgb_bt601_full = {
848 256, 0, 358, /* ry, rcb, rcr |1.000 0.000 1.402|*/
849 256, -88, -182, /* gy, gcb, gcr |1.000 -0.344 -0.714|*/
850 256, 452, 0, /* by, bcb, bcr |1.000 1.772 0.000|*/
851 true, /* full range */
852};
853
854/* YUV -> RGB, ITU-R BT.601, limited range */
855static const struct csc_coef_yuv2rgb coefs_yuv2rgb_bt601_lim = {
856 298, 0, 409, /* ry, rcb, rcr |1.164 0.000 1.596|*/
857 298, -100, -208, /* gy, gcb, gcr |1.164 -0.392 -0.813|*/
858 298, 516, 0, /* by, bcb, bcr |1.164 2.017 0.000|*/
859 false, /* limited range */
860};
861
862/* YUV -> RGB, ITU-R BT.709, full range */
863static const struct csc_coef_yuv2rgb coefs_yuv2rgb_bt709_full = {
864 256, 0, 402, /* ry, rcb, rcr |1.000 0.000 1.570|*/
865 256, -48, -120, /* gy, gcb, gcr |1.000 -0.187 -0.467|*/
866 256, 475, 0, /* by, bcb, bcr |1.000 1.856 0.000|*/
867 true, /* full range */
868};
869
870/* YUV -> RGB, ITU-R BT.709, limited range */
871static const struct csc_coef_yuv2rgb coefs_yuv2rgb_bt709_lim = {
872 298, 0, 459, /* ry, rcb, rcr |1.164 0.000 1.793|*/
873 298, -55, -136, /* gy, gcb, gcr |1.164 -0.213 -0.533|*/
874 298, 541, 0, /* by, bcb, bcr |1.164 2.112 0.000|*/
875 false, /* limited range */
876};
877
878static void dispc_ovl_set_csc(struct dispc_device *dispc,
879 enum omap_plane_id plane,
880 enum drm_color_encoding color_encoding,
881 enum drm_color_range color_range)
882{
883 const struct csc_coef_yuv2rgb *csc;
884
885 switch (color_encoding) {
886 default:
887 case DRM_COLOR_YCBCR_BT601:
888 if (color_range == DRM_COLOR_YCBCR_FULL_RANGE)
889 csc = &coefs_yuv2rgb_bt601_full;
890 else
891 csc = &coefs_yuv2rgb_bt601_lim;
892 break;
893 case DRM_COLOR_YCBCR_BT709:
894 if (color_range == DRM_COLOR_YCBCR_FULL_RANGE)
895 csc = &coefs_yuv2rgb_bt709_full;
896 else
897 csc = &coefs_yuv2rgb_bt709_lim;
898 break;
899 }
900
901 dispc_ovl_write_color_conv_coef(dispc, plane, csc);
902}
903
904static void dispc_ovl_set_ba0(struct dispc_device *dispc,
905 enum omap_plane_id plane, u32 paddr)
906{
907 dispc_write_reg(dispc, DISPC_OVL_BA0(plane), paddr);
908}
909
910static void dispc_ovl_set_ba1(struct dispc_device *dispc,
911 enum omap_plane_id plane, u32 paddr)
912{
913 dispc_write_reg(dispc, DISPC_OVL_BA1(plane), paddr);
914}
915
916static void dispc_ovl_set_ba0_uv(struct dispc_device *dispc,
917 enum omap_plane_id plane, u32 paddr)
918{
919 dispc_write_reg(dispc, DISPC_OVL_BA0_UV(plane), paddr);
920}
921
922static void dispc_ovl_set_ba1_uv(struct dispc_device *dispc,
923 enum omap_plane_id plane, u32 paddr)
924{
925 dispc_write_reg(dispc, DISPC_OVL_BA1_UV(plane), paddr);
926}
927
928static void dispc_ovl_set_pos(struct dispc_device *dispc,
929 enum omap_plane_id plane,
930 enum omap_overlay_caps caps, int x, int y)
931{
932 u32 val;
933
934 if ((caps & OMAP_DSS_OVL_CAP_POS) == 0)
935 return;
936
937 val = FLD_VAL(y, 26, 16) | FLD_VAL(x, 10, 0);
938
939 dispc_write_reg(dispc, DISPC_OVL_POSITION(plane), val);
940}
941
942static void dispc_ovl_set_input_size(struct dispc_device *dispc,
943 enum omap_plane_id plane, int width,
944 int height)
945{
946 u32 val = FLD_VAL(height - 1, 26, 16) | FLD_VAL(width - 1, 10, 0);
947
948 if (plane == OMAP_DSS_GFX || plane == OMAP_DSS_WB)
949 dispc_write_reg(dispc, DISPC_OVL_SIZE(plane), val);
950 else
951 dispc_write_reg(dispc, DISPC_OVL_PICTURE_SIZE(plane), val);
952}
953
954static void dispc_ovl_set_output_size(struct dispc_device *dispc,
955 enum omap_plane_id plane, int width,
956 int height)
957{
958 u32 val;
959
960 BUG_ON(plane == OMAP_DSS_GFX);
961
962 val = FLD_VAL(height - 1, 26, 16) | FLD_VAL(width - 1, 10, 0);
963
964 if (plane == OMAP_DSS_WB)
965 dispc_write_reg(dispc, DISPC_OVL_PICTURE_SIZE(plane), val);
966 else
967 dispc_write_reg(dispc, DISPC_OVL_SIZE(plane), val);
968}
969
970static void dispc_ovl_set_zorder(struct dispc_device *dispc,
971 enum omap_plane_id plane,
972 enum omap_overlay_caps caps, u8 zorder)
973{
974 if ((caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
975 return;
976
977 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), zorder, 27, 26);
978}
979
980static void dispc_ovl_enable_zorder_planes(struct dispc_device *dispc)
981{
982 int i;
983
984 if (!dispc_has_feature(dispc, FEAT_ALPHA_FREE_ZORDER))
985 return;
986
987 for (i = 0; i < dispc_get_num_ovls(dispc); i++)
988 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(i), 1, 25, 25);
989}
990
991static void dispc_ovl_set_pre_mult_alpha(struct dispc_device *dispc,
992 enum omap_plane_id plane,
993 enum omap_overlay_caps caps,
994 bool enable)
995{
996 if ((caps & OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA) == 0)
997 return;
998
999 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), enable ? 1 : 0, 28, 28);
1000}
1001
1002static void dispc_ovl_setup_global_alpha(struct dispc_device *dispc,
1003 enum omap_plane_id plane,
1004 enum omap_overlay_caps caps,
1005 u8 global_alpha)
1006{
1007 static const unsigned int shifts[] = { 0, 8, 16, 24, };
1008 int shift;
1009
1010 if ((caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1011 return;
1012
1013 shift = shifts[plane];
1014 REG_FLD_MOD(dispc, DISPC_GLOBAL_ALPHA, global_alpha, shift + 7, shift);
1015}
1016
1017static void dispc_ovl_set_pix_inc(struct dispc_device *dispc,
1018 enum omap_plane_id plane, s32 inc)
1019{
1020 dispc_write_reg(dispc, DISPC_OVL_PIXEL_INC(plane), inc);
1021}
1022
1023static void dispc_ovl_set_row_inc(struct dispc_device *dispc,
1024 enum omap_plane_id plane, s32 inc)
1025{
1026 dispc_write_reg(dispc, DISPC_OVL_ROW_INC(plane), inc);
1027}
1028
1029static void dispc_ovl_set_color_mode(struct dispc_device *dispc,
1030 enum omap_plane_id plane, u32 fourcc)
1031{
1032 u32 m = 0;
1033 if (plane != OMAP_DSS_GFX) {
1034 switch (fourcc) {
1035 case DRM_FORMAT_NV12:
1036 m = 0x0; break;
1037 case DRM_FORMAT_XRGB4444:
1038 m = 0x1; break;
1039 case DRM_FORMAT_RGBA4444:
1040 m = 0x2; break;
1041 case DRM_FORMAT_RGBX4444:
1042 m = 0x4; break;
1043 case DRM_FORMAT_ARGB4444:
1044 m = 0x5; break;
1045 case DRM_FORMAT_RGB565:
1046 m = 0x6; break;
1047 case DRM_FORMAT_ARGB1555:
1048 m = 0x7; break;
1049 case DRM_FORMAT_XRGB8888:
1050 m = 0x8; break;
1051 case DRM_FORMAT_RGB888:
1052 m = 0x9; break;
1053 case DRM_FORMAT_YUYV:
1054 m = 0xa; break;
1055 case DRM_FORMAT_UYVY:
1056 m = 0xb; break;
1057 case DRM_FORMAT_ARGB8888:
1058 m = 0xc; break;
1059 case DRM_FORMAT_RGBA8888:
1060 m = 0xd; break;
1061 case DRM_FORMAT_RGBX8888:
1062 m = 0xe; break;
1063 case DRM_FORMAT_XRGB1555:
1064 m = 0xf; break;
1065 default:
1066 BUG(); return;
1067 }
1068 } else {
1069 switch (fourcc) {
1070 case DRM_FORMAT_RGBX4444:
1071 m = 0x4; break;
1072 case DRM_FORMAT_ARGB4444:
1073 m = 0x5; break;
1074 case DRM_FORMAT_RGB565:
1075 m = 0x6; break;
1076 case DRM_FORMAT_ARGB1555:
1077 m = 0x7; break;
1078 case DRM_FORMAT_XRGB8888:
1079 m = 0x8; break;
1080 case DRM_FORMAT_RGB888:
1081 m = 0x9; break;
1082 case DRM_FORMAT_XRGB4444:
1083 m = 0xa; break;
1084 case DRM_FORMAT_RGBA4444:
1085 m = 0xb; break;
1086 case DRM_FORMAT_ARGB8888:
1087 m = 0xc; break;
1088 case DRM_FORMAT_RGBA8888:
1089 m = 0xd; break;
1090 case DRM_FORMAT_RGBX8888:
1091 m = 0xe; break;
1092 case DRM_FORMAT_XRGB1555:
1093 m = 0xf; break;
1094 default:
1095 BUG(); return;
1096 }
1097 }
1098
1099 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), m, 4, 1);
1100}
1101
1102static void dispc_ovl_configure_burst_type(struct dispc_device *dispc,
1103 enum omap_plane_id plane,
1104 enum omap_dss_rotation_type rotation)
1105{
1106 if (dispc_has_feature(dispc, FEAT_BURST_2D) == 0)
1107 return;
1108
1109 if (rotation == OMAP_DSS_ROT_TILER)
1110 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), 1, 29, 29);
1111 else
1112 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), 0, 29, 29);
1113}
1114
1115static void dispc_ovl_set_channel_out(struct dispc_device *dispc,
1116 enum omap_plane_id plane,
1117 enum omap_channel channel)
1118{
1119 int shift;
1120 u32 val;
1121 int chan = 0, chan2 = 0;
1122
1123 switch (plane) {
1124 case OMAP_DSS_GFX:
1125 shift = 8;
1126 break;
1127 case OMAP_DSS_VIDEO1:
1128 case OMAP_DSS_VIDEO2:
1129 case OMAP_DSS_VIDEO3:
1130 shift = 16;
1131 break;
1132 default:
1133 BUG();
1134 return;
1135 }
1136
1137 val = dispc_read_reg(dispc, DISPC_OVL_ATTRIBUTES(plane));
1138 if (dispc_has_feature(dispc, FEAT_MGR_LCD2)) {
1139 switch (channel) {
1140 case OMAP_DSS_CHANNEL_LCD:
1141 chan = 0;
1142 chan2 = 0;
1143 break;
1144 case OMAP_DSS_CHANNEL_DIGIT:
1145 chan = 1;
1146 chan2 = 0;
1147 break;
1148 case OMAP_DSS_CHANNEL_LCD2:
1149 chan = 0;
1150 chan2 = 1;
1151 break;
1152 case OMAP_DSS_CHANNEL_LCD3:
1153 if (dispc_has_feature(dispc, FEAT_MGR_LCD3)) {
1154 chan = 0;
1155 chan2 = 2;
1156 } else {
1157 BUG();
1158 return;
1159 }
1160 break;
1161 case OMAP_DSS_CHANNEL_WB:
1162 chan = 0;
1163 chan2 = 3;
1164 break;
1165 default:
1166 BUG();
1167 return;
1168 }
1169
1170 val = FLD_MOD(val, chan, shift, shift);
1171 val = FLD_MOD(val, chan2, 31, 30);
1172 } else {
1173 val = FLD_MOD(val, channel, shift, shift);
1174 }
1175 dispc_write_reg(dispc, DISPC_OVL_ATTRIBUTES(plane), val);
1176}
1177
1178static enum omap_channel dispc_ovl_get_channel_out(struct dispc_device *dispc,
1179 enum omap_plane_id plane)
1180{
1181 int shift;
1182 u32 val;
1183
1184 switch (plane) {
1185 case OMAP_DSS_GFX:
1186 shift = 8;
1187 break;
1188 case OMAP_DSS_VIDEO1:
1189 case OMAP_DSS_VIDEO2:
1190 case OMAP_DSS_VIDEO3:
1191 shift = 16;
1192 break;
1193 default:
1194 BUG();
1195 return 0;
1196 }
1197
1198 val = dispc_read_reg(dispc, DISPC_OVL_ATTRIBUTES(plane));
1199
1200 if (FLD_GET(val, shift, shift) == 1)
1201 return OMAP_DSS_CHANNEL_DIGIT;
1202
1203 if (!dispc_has_feature(dispc, FEAT_MGR_LCD2))
1204 return OMAP_DSS_CHANNEL_LCD;
1205
1206 switch (FLD_GET(val, 31, 30)) {
1207 case 0:
1208 default:
1209 return OMAP_DSS_CHANNEL_LCD;
1210 case 1:
1211 return OMAP_DSS_CHANNEL_LCD2;
1212 case 2:
1213 return OMAP_DSS_CHANNEL_LCD3;
1214 case 3:
1215 return OMAP_DSS_CHANNEL_WB;
1216 }
1217}
1218
1219static void dispc_ovl_set_burst_size(struct dispc_device *dispc,
1220 enum omap_plane_id plane,
1221 enum omap_burst_size burst_size)
1222{
1223 static const unsigned int shifts[] = { 6, 14, 14, 14, 14, };
1224 int shift;
1225
1226 shift = shifts[plane];
1227 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), burst_size,
1228 shift + 1, shift);
1229}
1230
1231static void dispc_configure_burst_sizes(struct dispc_device *dispc)
1232{
1233 int i;
1234 const int burst_size = BURST_SIZE_X8;
1235
1236 /* Configure burst size always to maximum size */
1237 for (i = 0; i < dispc_get_num_ovls(dispc); ++i)
1238 dispc_ovl_set_burst_size(dispc, i, burst_size);
1239 if (dispc->feat->has_writeback)
1240 dispc_ovl_set_burst_size(dispc, OMAP_DSS_WB, burst_size);
1241}
1242
1243static u32 dispc_ovl_get_burst_size(struct dispc_device *dispc,
1244 enum omap_plane_id plane)
1245{
1246 /* burst multiplier is always x8 (see dispc_configure_burst_sizes()) */
1247 return dispc->feat->burst_size_unit * 8;
1248}
1249
1250bool dispc_ovl_color_mode_supported(struct dispc_device *dispc,
1251 enum omap_plane_id plane, u32 fourcc)
1252{
1253 const u32 *modes;
1254 unsigned int i;
1255
1256 modes = dispc->feat->supported_color_modes[plane];
1257
1258 for (i = 0; modes[i]; ++i) {
1259 if (modes[i] == fourcc)
1260 return true;
1261 }
1262
1263 return false;
1264}
1265
1266const u32 *dispc_ovl_get_color_modes(struct dispc_device *dispc,
1267 enum omap_plane_id plane)
1268{
1269 return dispc->feat->supported_color_modes[plane];
1270}
1271
1272static void dispc_mgr_enable_cpr(struct dispc_device *dispc,
1273 enum omap_channel channel, bool enable)
1274{
1275 if (channel == OMAP_DSS_CHANNEL_DIGIT)
1276 return;
1277
1278 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_CPR, enable);
1279}
1280
1281static void dispc_mgr_set_cpr_coef(struct dispc_device *dispc,
1282 enum omap_channel channel,
1283 const struct omap_dss_cpr_coefs *coefs)
1284{
1285 u32 coef_r, coef_g, coef_b;
1286
1287 if (!dss_mgr_is_lcd(channel))
1288 return;
1289
1290 coef_r = FLD_VAL(coefs->rr, 31, 22) | FLD_VAL(coefs->rg, 20, 11) |
1291 FLD_VAL(coefs->rb, 9, 0);
1292 coef_g = FLD_VAL(coefs->gr, 31, 22) | FLD_VAL(coefs->gg, 20, 11) |
1293 FLD_VAL(coefs->gb, 9, 0);
1294 coef_b = FLD_VAL(coefs->br, 31, 22) | FLD_VAL(coefs->bg, 20, 11) |
1295 FLD_VAL(coefs->bb, 9, 0);
1296
1297 dispc_write_reg(dispc, DISPC_CPR_COEF_R(channel), coef_r);
1298 dispc_write_reg(dispc, DISPC_CPR_COEF_G(channel), coef_g);
1299 dispc_write_reg(dispc, DISPC_CPR_COEF_B(channel), coef_b);
1300}
1301
1302static void dispc_ovl_set_vid_color_conv(struct dispc_device *dispc,
1303 enum omap_plane_id plane, bool enable)
1304{
1305 u32 val;
1306
1307 BUG_ON(plane == OMAP_DSS_GFX);
1308
1309 val = dispc_read_reg(dispc, DISPC_OVL_ATTRIBUTES(plane));
1310 val = FLD_MOD(val, enable, 9, 9);
1311 dispc_write_reg(dispc, DISPC_OVL_ATTRIBUTES(plane), val);
1312}
1313
1314static void dispc_ovl_enable_replication(struct dispc_device *dispc,
1315 enum omap_plane_id plane,
1316 enum omap_overlay_caps caps,
1317 bool enable)
1318{
1319 static const unsigned int shifts[] = { 5, 10, 10, 10 };
1320 int shift;
1321
1322 if ((caps & OMAP_DSS_OVL_CAP_REPLICATION) == 0)
1323 return;
1324
1325 shift = shifts[plane];
1326 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), enable, shift, shift);
1327}
1328
1329static void dispc_mgr_set_size(struct dispc_device *dispc,
1330 enum omap_channel channel, u16 width, u16 height)
1331{
1332 u32 val;
1333
1334 val = FLD_VAL(height - 1, dispc->feat->mgr_height_start, 16) |
1335 FLD_VAL(width - 1, dispc->feat->mgr_width_start, 0);
1336
1337 dispc_write_reg(dispc, DISPC_SIZE_MGR(channel), val);
1338}
1339
1340static void dispc_init_fifos(struct dispc_device *dispc)
1341{
1342 u32 size;
1343 int fifo;
1344 u8 start, end;
1345 u32 unit;
1346 int i;
1347
1348 unit = dispc->feat->buffer_size_unit;
1349
1350 dispc_get_reg_field(dispc, FEAT_REG_FIFOSIZE, &start, &end);
1351
1352 for (fifo = 0; fifo < dispc->feat->num_fifos; ++fifo) {
1353 size = REG_GET(dispc, DISPC_OVL_FIFO_SIZE_STATUS(fifo),
1354 start, end);
1355 size *= unit;
1356 dispc->fifo_size[fifo] = size;
1357
1358 /*
1359 * By default fifos are mapped directly to overlays, fifo 0 to
1360 * ovl 0, fifo 1 to ovl 1, etc.
1361 */
1362 dispc->fifo_assignment[fifo] = fifo;
1363 }
1364
1365 /*
1366 * The GFX fifo on OMAP4 is smaller than the other fifos. The small fifo
1367 * causes problems with certain use cases, like using the tiler in 2D
1368 * mode. The below hack swaps the fifos of GFX and WB planes, thus
1369 * giving GFX plane a larger fifo. WB but should work fine with a
1370 * smaller fifo.
1371 */
1372 if (dispc->feat->gfx_fifo_workaround) {
1373 u32 v;
1374
1375 v = dispc_read_reg(dispc, DISPC_GLOBAL_BUFFER);
1376
1377 v = FLD_MOD(v, 4, 2, 0); /* GFX BUF top to WB */
1378 v = FLD_MOD(v, 4, 5, 3); /* GFX BUF bottom to WB */
1379 v = FLD_MOD(v, 0, 26, 24); /* WB BUF top to GFX */
1380 v = FLD_MOD(v, 0, 29, 27); /* WB BUF bottom to GFX */
1381
1382 dispc_write_reg(dispc, DISPC_GLOBAL_BUFFER, v);
1383
1384 dispc->fifo_assignment[OMAP_DSS_GFX] = OMAP_DSS_WB;
1385 dispc->fifo_assignment[OMAP_DSS_WB] = OMAP_DSS_GFX;
1386 }
1387
1388 /*
1389 * Setup default fifo thresholds.
1390 */
1391 for (i = 0; i < dispc_get_num_ovls(dispc); ++i) {
1392 u32 low, high;
1393 const bool use_fifomerge = false;
1394 const bool manual_update = false;
1395
1396 dispc_ovl_compute_fifo_thresholds(dispc, i, &low, &high,
1397 use_fifomerge, manual_update);
1398
1399 dispc_ovl_set_fifo_threshold(dispc, i, low, high);
1400 }
1401
1402 if (dispc->feat->has_writeback) {
1403 u32 low, high;
1404 const bool use_fifomerge = false;
1405 const bool manual_update = false;
1406
1407 dispc_ovl_compute_fifo_thresholds(dispc, OMAP_DSS_WB,
1408 &low, &high, use_fifomerge,
1409 manual_update);
1410
1411 dispc_ovl_set_fifo_threshold(dispc, OMAP_DSS_WB, low, high);
1412 }
1413}
1414
1415static u32 dispc_ovl_get_fifo_size(struct dispc_device *dispc,
1416 enum omap_plane_id plane)
1417{
1418 int fifo;
1419 u32 size = 0;
1420
1421 for (fifo = 0; fifo < dispc->feat->num_fifos; ++fifo) {
1422 if (dispc->fifo_assignment[fifo] == plane)
1423 size += dispc->fifo_size[fifo];
1424 }
1425
1426 return size;
1427}
1428
1429void dispc_ovl_set_fifo_threshold(struct dispc_device *dispc,
1430 enum omap_plane_id plane,
1431 u32 low, u32 high)
1432{
1433 u8 hi_start, hi_end, lo_start, lo_end;
1434 u32 unit;
1435
1436 unit = dispc->feat->buffer_size_unit;
1437
1438 WARN_ON(low % unit != 0);
1439 WARN_ON(high % unit != 0);
1440
1441 low /= unit;
1442 high /= unit;
1443
1444 dispc_get_reg_field(dispc, FEAT_REG_FIFOHIGHTHRESHOLD,
1445 &hi_start, &hi_end);
1446 dispc_get_reg_field(dispc, FEAT_REG_FIFOLOWTHRESHOLD,
1447 &lo_start, &lo_end);
1448
1449 DSSDBG("fifo(%d) threshold (bytes), old %u/%u, new %u/%u\n",
1450 plane,
1451 REG_GET(dispc, DISPC_OVL_FIFO_THRESHOLD(plane),
1452 lo_start, lo_end) * unit,
1453 REG_GET(dispc, DISPC_OVL_FIFO_THRESHOLD(plane),
1454 hi_start, hi_end) * unit,
1455 low * unit, high * unit);
1456
1457 dispc_write_reg(dispc, DISPC_OVL_FIFO_THRESHOLD(plane),
1458 FLD_VAL(high, hi_start, hi_end) |
1459 FLD_VAL(low, lo_start, lo_end));
1460
1461 /*
1462 * configure the preload to the pipeline's high threhold, if HT it's too
1463 * large for the preload field, set the threshold to the maximum value
1464 * that can be held by the preload register
1465 */
1466 if (dispc_has_feature(dispc, FEAT_PRELOAD) &&
1467 dispc->feat->set_max_preload && plane != OMAP_DSS_WB)
1468 dispc_write_reg(dispc, DISPC_OVL_PRELOAD(plane),
1469 min(high, 0xfffu));
1470}
1471
1472void dispc_ovl_compute_fifo_thresholds(struct dispc_device *dispc,
1473 enum omap_plane_id plane,
1474 u32 *fifo_low, u32 *fifo_high,
1475 bool use_fifomerge, bool manual_update)
1476{
1477 /*
1478 * All sizes are in bytes. Both the buffer and burst are made of
1479 * buffer_units, and the fifo thresholds must be buffer_unit aligned.
1480 */
1481 unsigned int buf_unit = dispc->feat->buffer_size_unit;
1482 unsigned int ovl_fifo_size, total_fifo_size, burst_size;
1483 int i;
1484
1485 burst_size = dispc_ovl_get_burst_size(dispc, plane);
1486 ovl_fifo_size = dispc_ovl_get_fifo_size(dispc, plane);
1487
1488 if (use_fifomerge) {
1489 total_fifo_size = 0;
1490 for (i = 0; i < dispc_get_num_ovls(dispc); ++i)
1491 total_fifo_size += dispc_ovl_get_fifo_size(dispc, i);
1492 } else {
1493 total_fifo_size = ovl_fifo_size;
1494 }
1495
1496 /*
1497 * We use the same low threshold for both fifomerge and non-fifomerge
1498 * cases, but for fifomerge we calculate the high threshold using the
1499 * combined fifo size
1500 */
1501
1502 if (manual_update && dispc_has_feature(dispc, FEAT_OMAP3_DSI_FIFO_BUG)) {
1503 *fifo_low = ovl_fifo_size - burst_size * 2;
1504 *fifo_high = total_fifo_size - burst_size;
1505 } else if (plane == OMAP_DSS_WB) {
1506 /*
1507 * Most optimal configuration for writeback is to push out data
1508 * to the interconnect the moment writeback pushes enough pixels
1509 * in the FIFO to form a burst
1510 */
1511 *fifo_low = 0;
1512 *fifo_high = burst_size;
1513 } else {
1514 *fifo_low = ovl_fifo_size - burst_size;
1515 *fifo_high = total_fifo_size - buf_unit;
1516 }
1517}
1518
1519static void dispc_ovl_set_mflag(struct dispc_device *dispc,
1520 enum omap_plane_id plane, bool enable)
1521{
1522 int bit;
1523
1524 if (plane == OMAP_DSS_GFX)
1525 bit = 14;
1526 else
1527 bit = 23;
1528
1529 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), enable, bit, bit);
1530}
1531
1532static void dispc_ovl_set_mflag_threshold(struct dispc_device *dispc,
1533 enum omap_plane_id plane,
1534 int low, int high)
1535{
1536 dispc_write_reg(dispc, DISPC_OVL_MFLAG_THRESHOLD(plane),
1537 FLD_VAL(high, 31, 16) | FLD_VAL(low, 15, 0));
1538}
1539
1540static void dispc_init_mflag(struct dispc_device *dispc)
1541{
1542 int i;
1543
1544 /*
1545 * HACK: NV12 color format and MFLAG seem to have problems working
1546 * together: using two displays, and having an NV12 overlay on one of
1547 * the displays will cause underflows/synclosts when MFLAG_CTRL=2.
1548 * Changing MFLAG thresholds and PRELOAD to certain values seem to
1549 * remove the errors, but there doesn't seem to be a clear logic on
1550 * which values work and which not.
1551 *
1552 * As a work-around, set force MFLAG to always on.
1553 */
1554 dispc_write_reg(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE,
1555 (1 << 0) | /* MFLAG_CTRL = force always on */
1556 (0 << 2)); /* MFLAG_START = disable */
1557
1558 for (i = 0; i < dispc_get_num_ovls(dispc); ++i) {
1559 u32 size = dispc_ovl_get_fifo_size(dispc, i);
1560 u32 unit = dispc->feat->buffer_size_unit;
1561 u32 low, high;
1562
1563 dispc_ovl_set_mflag(dispc, i, true);
1564
1565 /*
1566 * Simulation team suggests below thesholds:
1567 * HT = fifosize * 5 / 8;
1568 * LT = fifosize * 4 / 8;
1569 */
1570
1571 low = size * 4 / 8 / unit;
1572 high = size * 5 / 8 / unit;
1573
1574 dispc_ovl_set_mflag_threshold(dispc, i, low, high);
1575 }
1576
1577 if (dispc->feat->has_writeback) {
1578 u32 size = dispc_ovl_get_fifo_size(dispc, OMAP_DSS_WB);
1579 u32 unit = dispc->feat->buffer_size_unit;
1580 u32 low, high;
1581
1582 dispc_ovl_set_mflag(dispc, OMAP_DSS_WB, true);
1583
1584 /*
1585 * Simulation team suggests below thesholds:
1586 * HT = fifosize * 5 / 8;
1587 * LT = fifosize * 4 / 8;
1588 */
1589
1590 low = size * 4 / 8 / unit;
1591 high = size * 5 / 8 / unit;
1592
1593 dispc_ovl_set_mflag_threshold(dispc, OMAP_DSS_WB, low, high);
1594 }
1595}
1596
1597static void dispc_ovl_set_fir(struct dispc_device *dispc,
1598 enum omap_plane_id plane,
1599 int hinc, int vinc,
1600 enum omap_color_component color_comp)
1601{
1602 u32 val;
1603
1604 if (color_comp == DISPC_COLOR_COMPONENT_RGB_Y) {
1605 u8 hinc_start, hinc_end, vinc_start, vinc_end;
1606
1607 dispc_get_reg_field(dispc, FEAT_REG_FIRHINC,
1608 &hinc_start, &hinc_end);
1609 dispc_get_reg_field(dispc, FEAT_REG_FIRVINC,
1610 &vinc_start, &vinc_end);
1611 val = FLD_VAL(vinc, vinc_start, vinc_end) |
1612 FLD_VAL(hinc, hinc_start, hinc_end);
1613
1614 dispc_write_reg(dispc, DISPC_OVL_FIR(plane), val);
1615 } else {
1616 val = FLD_VAL(vinc, 28, 16) | FLD_VAL(hinc, 12, 0);
1617 dispc_write_reg(dispc, DISPC_OVL_FIR2(plane), val);
1618 }
1619}
1620
1621static void dispc_ovl_set_vid_accu0(struct dispc_device *dispc,
1622 enum omap_plane_id plane, int haccu,
1623 int vaccu)
1624{
1625 u32 val;
1626 u8 hor_start, hor_end, vert_start, vert_end;
1627
1628 dispc_get_reg_field(dispc, FEAT_REG_HORIZONTALACCU,
1629 &hor_start, &hor_end);
1630 dispc_get_reg_field(dispc, FEAT_REG_VERTICALACCU,
1631 &vert_start, &vert_end);
1632
1633 val = FLD_VAL(vaccu, vert_start, vert_end) |
1634 FLD_VAL(haccu, hor_start, hor_end);
1635
1636 dispc_write_reg(dispc, DISPC_OVL_ACCU0(plane), val);
1637}
1638
1639static void dispc_ovl_set_vid_accu1(struct dispc_device *dispc,
1640 enum omap_plane_id plane, int haccu,
1641 int vaccu)
1642{
1643 u32 val;
1644 u8 hor_start, hor_end, vert_start, vert_end;
1645
1646 dispc_get_reg_field(dispc, FEAT_REG_HORIZONTALACCU,
1647 &hor_start, &hor_end);
1648 dispc_get_reg_field(dispc, FEAT_REG_VERTICALACCU,
1649 &vert_start, &vert_end);
1650
1651 val = FLD_VAL(vaccu, vert_start, vert_end) |
1652 FLD_VAL(haccu, hor_start, hor_end);
1653
1654 dispc_write_reg(dispc, DISPC_OVL_ACCU1(plane), val);
1655}
1656
1657static void dispc_ovl_set_vid_accu2_0(struct dispc_device *dispc,
1658 enum omap_plane_id plane, int haccu,
1659 int vaccu)
1660{
1661 u32 val;
1662
1663 val = FLD_VAL(vaccu, 26, 16) | FLD_VAL(haccu, 10, 0);
1664 dispc_write_reg(dispc, DISPC_OVL_ACCU2_0(plane), val);
1665}
1666
1667static void dispc_ovl_set_vid_accu2_1(struct dispc_device *dispc,
1668 enum omap_plane_id plane, int haccu,
1669 int vaccu)
1670{
1671 u32 val;
1672
1673 val = FLD_VAL(vaccu, 26, 16) | FLD_VAL(haccu, 10, 0);
1674 dispc_write_reg(dispc, DISPC_OVL_ACCU2_1(plane), val);
1675}
1676
1677static void dispc_ovl_set_scale_param(struct dispc_device *dispc,
1678 enum omap_plane_id plane,
1679 u16 orig_width, u16 orig_height,
1680 u16 out_width, u16 out_height,
1681 bool five_taps, u8 rotation,
1682 enum omap_color_component color_comp)
1683{
1684 int fir_hinc, fir_vinc;
1685
1686 fir_hinc = 1024 * orig_width / out_width;
1687 fir_vinc = 1024 * orig_height / out_height;
1688
1689 dispc_ovl_set_scale_coef(dispc, plane, fir_hinc, fir_vinc, five_taps,
1690 color_comp);
1691 dispc_ovl_set_fir(dispc, plane, fir_hinc, fir_vinc, color_comp);
1692}
1693
1694static void dispc_ovl_set_accu_uv(struct dispc_device *dispc,
1695 enum omap_plane_id plane,
1696 u16 orig_width, u16 orig_height,
1697 u16 out_width, u16 out_height,
1698 bool ilace, u32 fourcc, u8 rotation)
1699{
1700 int h_accu2_0, h_accu2_1;
1701 int v_accu2_0, v_accu2_1;
1702 int chroma_hinc, chroma_vinc;
1703 int idx;
1704
1705 struct accu {
1706 s8 h0_m, h0_n;
1707 s8 h1_m, h1_n;
1708 s8 v0_m, v0_n;
1709 s8 v1_m, v1_n;
1710 };
1711
1712 const struct accu *accu_table;
1713 const struct accu *accu_val;
1714
1715 static const struct accu accu_nv12[4] = {
1716 { 0, 1, 0, 1 , -1, 2, 0, 1 },
1717 { 1, 2, -3, 4 , 0, 1, 0, 1 },
1718 { -1, 1, 0, 1 , -1, 2, 0, 1 },
1719 { -1, 2, -1, 2 , -1, 1, 0, 1 },
1720 };
1721
1722 static const struct accu accu_nv12_ilace[4] = {
1723 { 0, 1, 0, 1 , -3, 4, -1, 4 },
1724 { -1, 4, -3, 4 , 0, 1, 0, 1 },
1725 { -1, 1, 0, 1 , -1, 4, -3, 4 },
1726 { -3, 4, -3, 4 , -1, 1, 0, 1 },
1727 };
1728
1729 static const struct accu accu_yuv[4] = {
1730 { 0, 1, 0, 1, 0, 1, 0, 1 },
1731 { 0, 1, 0, 1, 0, 1, 0, 1 },
1732 { -1, 1, 0, 1, 0, 1, 0, 1 },
1733 { 0, 1, 0, 1, -1, 1, 0, 1 },
1734 };
1735
1736 /* Note: DSS HW rotates clockwise, DRM_MODE_ROTATE_* counter-clockwise */
1737 switch (rotation & DRM_MODE_ROTATE_MASK) {
1738 default:
1739 case DRM_MODE_ROTATE_0:
1740 idx = 0;
1741 break;
1742 case DRM_MODE_ROTATE_90:
1743 idx = 3;
1744 break;
1745 case DRM_MODE_ROTATE_180:
1746 idx = 2;
1747 break;
1748 case DRM_MODE_ROTATE_270:
1749 idx = 1;
1750 break;
1751 }
1752
1753 switch (fourcc) {
1754 case DRM_FORMAT_NV12:
1755 if (ilace)
1756 accu_table = accu_nv12_ilace;
1757 else
1758 accu_table = accu_nv12;
1759 break;
1760 case DRM_FORMAT_YUYV:
1761 case DRM_FORMAT_UYVY:
1762 accu_table = accu_yuv;
1763 break;
1764 default:
1765 BUG();
1766 return;
1767 }
1768
1769 accu_val = &accu_table[idx];
1770
1771 chroma_hinc = 1024 * orig_width / out_width;
1772 chroma_vinc = 1024 * orig_height / out_height;
1773
1774 h_accu2_0 = (accu_val->h0_m * chroma_hinc / accu_val->h0_n) % 1024;
1775 h_accu2_1 = (accu_val->h1_m * chroma_hinc / accu_val->h1_n) % 1024;
1776 v_accu2_0 = (accu_val->v0_m * chroma_vinc / accu_val->v0_n) % 1024;
1777 v_accu2_1 = (accu_val->v1_m * chroma_vinc / accu_val->v1_n) % 1024;
1778
1779 dispc_ovl_set_vid_accu2_0(dispc, plane, h_accu2_0, v_accu2_0);
1780 dispc_ovl_set_vid_accu2_1(dispc, plane, h_accu2_1, v_accu2_1);
1781}
1782
1783static void dispc_ovl_set_scaling_common(struct dispc_device *dispc,
1784 enum omap_plane_id plane,
1785 u16 orig_width, u16 orig_height,
1786 u16 out_width, u16 out_height,
1787 bool ilace, bool five_taps,
1788 bool fieldmode, u32 fourcc,
1789 u8 rotation)
1790{
1791 int accu0 = 0;
1792 int accu1 = 0;
1793 u32 l;
1794
1795 dispc_ovl_set_scale_param(dispc, plane, orig_width, orig_height,
1796 out_width, out_height, five_taps,
1797 rotation, DISPC_COLOR_COMPONENT_RGB_Y);
1798 l = dispc_read_reg(dispc, DISPC_OVL_ATTRIBUTES(plane));
1799
1800 /* RESIZEENABLE and VERTICALTAPS */
1801 l &= ~((0x3 << 5) | (0x1 << 21));
1802 l |= (orig_width != out_width) ? (1 << 5) : 0;
1803 l |= (orig_height != out_height) ? (1 << 6) : 0;
1804 l |= five_taps ? (1 << 21) : 0;
1805
1806 /* VRESIZECONF and HRESIZECONF */
1807 if (dispc_has_feature(dispc, FEAT_RESIZECONF)) {
1808 l &= ~(0x3 << 7);
1809 l |= (orig_width <= out_width) ? 0 : (1 << 7);
1810 l |= (orig_height <= out_height) ? 0 : (1 << 8);
1811 }
1812
1813 /* LINEBUFFERSPLIT */
1814 if (dispc_has_feature(dispc, FEAT_LINEBUFFERSPLIT)) {
1815 l &= ~(0x1 << 22);
1816 l |= five_taps ? (1 << 22) : 0;
1817 }
1818
1819 dispc_write_reg(dispc, DISPC_OVL_ATTRIBUTES(plane), l);
1820
1821 /*
1822 * field 0 = even field = bottom field
1823 * field 1 = odd field = top field
1824 */
1825 if (ilace && !fieldmode) {
1826 accu1 = 0;
1827 accu0 = ((1024 * orig_height / out_height) / 2) & 0x3ff;
1828 if (accu0 >= 1024/2) {
1829 accu1 = 1024/2;
1830 accu0 -= accu1;
1831 }
1832 }
1833
1834 dispc_ovl_set_vid_accu0(dispc, plane, 0, accu0);
1835 dispc_ovl_set_vid_accu1(dispc, plane, 0, accu1);
1836}
1837
1838static void dispc_ovl_set_scaling_uv(struct dispc_device *dispc,
1839 enum omap_plane_id plane,
1840 u16 orig_width, u16 orig_height,
1841 u16 out_width, u16 out_height,
1842 bool ilace, bool five_taps,
1843 bool fieldmode, u32 fourcc,
1844 u8 rotation)
1845{
1846 int scale_x = out_width != orig_width;
1847 int scale_y = out_height != orig_height;
1848 bool chroma_upscale = plane != OMAP_DSS_WB;
1849 const struct drm_format_info *info;
1850
1851 info = drm_format_info(fourcc);
1852
1853 if (!dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE))
1854 return;
1855
1856 if (!info->is_yuv) {
1857 /* reset chroma resampling for RGB formats */
1858 if (plane != OMAP_DSS_WB)
1859 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES2(plane),
1860 0, 8, 8);
1861 return;
1862 }
1863
1864 dispc_ovl_set_accu_uv(dispc, plane, orig_width, orig_height, out_width,
1865 out_height, ilace, fourcc, rotation);
1866
1867 switch (fourcc) {
1868 case DRM_FORMAT_NV12:
1869 if (chroma_upscale) {
1870 /* UV is subsampled by 2 horizontally and vertically */
1871 orig_height >>= 1;
1872 orig_width >>= 1;
1873 } else {
1874 /* UV is downsampled by 2 horizontally and vertically */
1875 orig_height <<= 1;
1876 orig_width <<= 1;
1877 }
1878
1879 break;
1880 case DRM_FORMAT_YUYV:
1881 case DRM_FORMAT_UYVY:
1882 /* For YUV422 with 90/270 rotation, we don't upsample chroma */
1883 if (!drm_rotation_90_or_270(rotation)) {
1884 if (chroma_upscale)
1885 /* UV is subsampled by 2 horizontally */
1886 orig_width >>= 1;
1887 else
1888 /* UV is downsampled by 2 horizontally */
1889 orig_width <<= 1;
1890 }
1891
1892 /* must use FIR for YUV422 if rotated */
1893 if ((rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0)
1894 scale_x = scale_y = true;
1895
1896 break;
1897 default:
1898 BUG();
1899 return;
1900 }
1901
1902 if (out_width != orig_width)
1903 scale_x = true;
1904 if (out_height != orig_height)
1905 scale_y = true;
1906
1907 dispc_ovl_set_scale_param(dispc, plane, orig_width, orig_height,
1908 out_width, out_height, five_taps,
1909 rotation, DISPC_COLOR_COMPONENT_UV);
1910
1911 if (plane != OMAP_DSS_WB)
1912 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES2(plane),
1913 (scale_x || scale_y) ? 1 : 0, 8, 8);
1914
1915 /* set H scaling */
1916 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), scale_x ? 1 : 0, 5, 5);
1917 /* set V scaling */
1918 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), scale_y ? 1 : 0, 6, 6);
1919}
1920
1921static void dispc_ovl_set_scaling(struct dispc_device *dispc,
1922 enum omap_plane_id plane,
1923 u16 orig_width, u16 orig_height,
1924 u16 out_width, u16 out_height,
1925 bool ilace, bool five_taps,
1926 bool fieldmode, u32 fourcc,
1927 u8 rotation)
1928{
1929 BUG_ON(plane == OMAP_DSS_GFX);
1930
1931 dispc_ovl_set_scaling_common(dispc, plane, orig_width, orig_height,
1932 out_width, out_height, ilace, five_taps,
1933 fieldmode, fourcc, rotation);
1934
1935 dispc_ovl_set_scaling_uv(dispc, plane, orig_width, orig_height,
1936 out_width, out_height, ilace, five_taps,
1937 fieldmode, fourcc, rotation);
1938}
1939
1940static void dispc_ovl_set_rotation_attrs(struct dispc_device *dispc,
1941 enum omap_plane_id plane, u8 rotation,
1942 enum omap_dss_rotation_type rotation_type,
1943 u32 fourcc)
1944{
1945 bool row_repeat = false;
1946 int vidrot = 0;
1947
1948 /* Note: DSS HW rotates clockwise, DRM_MODE_ROTATE_* counter-clockwise */
1949 if (fourcc == DRM_FORMAT_YUYV || fourcc == DRM_FORMAT_UYVY) {
1950
1951 if (rotation & DRM_MODE_REFLECT_X) {
1952 switch (rotation & DRM_MODE_ROTATE_MASK) {
1953 case DRM_MODE_ROTATE_0:
1954 vidrot = 2;
1955 break;
1956 case DRM_MODE_ROTATE_90:
1957 vidrot = 1;
1958 break;
1959 case DRM_MODE_ROTATE_180:
1960 vidrot = 0;
1961 break;
1962 case DRM_MODE_ROTATE_270:
1963 vidrot = 3;
1964 break;
1965 }
1966 } else {
1967 switch (rotation & DRM_MODE_ROTATE_MASK) {
1968 case DRM_MODE_ROTATE_0:
1969 vidrot = 0;
1970 break;
1971 case DRM_MODE_ROTATE_90:
1972 vidrot = 3;
1973 break;
1974 case DRM_MODE_ROTATE_180:
1975 vidrot = 2;
1976 break;
1977 case DRM_MODE_ROTATE_270:
1978 vidrot = 1;
1979 break;
1980 }
1981 }
1982
1983 if (drm_rotation_90_or_270(rotation))
1984 row_repeat = true;
1985 else
1986 row_repeat = false;
1987 }
1988
1989 /*
1990 * OMAP4/5 Errata i631:
1991 * NV12 in 1D mode must use ROTATION=1. Otherwise DSS will fetch extra
1992 * rows beyond the framebuffer, which may cause OCP error.
1993 */
1994 if (fourcc == DRM_FORMAT_NV12 && rotation_type != OMAP_DSS_ROT_TILER)
1995 vidrot = 1;
1996
1997 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), vidrot, 13, 12);
1998 if (dispc_has_feature(dispc, FEAT_ROWREPEATENABLE))
1999 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane),
2000 row_repeat ? 1 : 0, 18, 18);
2001
2002 if (dispc_ovl_color_mode_supported(dispc, plane, DRM_FORMAT_NV12)) {
2003 bool doublestride =
2004 fourcc == DRM_FORMAT_NV12 &&
2005 rotation_type == OMAP_DSS_ROT_TILER &&
2006 !drm_rotation_90_or_270(rotation);
2007
2008 /* DOUBLESTRIDE */
2009 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane),
2010 doublestride, 22, 22);
2011 }
2012}
2013
2014static int color_mode_to_bpp(u32 fourcc)
2015{
2016 switch (fourcc) {
2017 case DRM_FORMAT_NV12:
2018 return 8;
2019 case DRM_FORMAT_RGBX4444:
2020 case DRM_FORMAT_RGB565:
2021 case DRM_FORMAT_ARGB4444:
2022 case DRM_FORMAT_YUYV:
2023 case DRM_FORMAT_UYVY:
2024 case DRM_FORMAT_RGBA4444:
2025 case DRM_FORMAT_XRGB4444:
2026 case DRM_FORMAT_ARGB1555:
2027 case DRM_FORMAT_XRGB1555:
2028 return 16;
2029 case DRM_FORMAT_RGB888:
2030 return 24;
2031 case DRM_FORMAT_XRGB8888:
2032 case DRM_FORMAT_ARGB8888:
2033 case DRM_FORMAT_RGBA8888:
2034 case DRM_FORMAT_RGBX8888:
2035 return 32;
2036 default:
2037 BUG();
2038 return 0;
2039 }
2040}
2041
2042static s32 pixinc(int pixels, u8 ps)
2043{
2044 if (pixels == 1)
2045 return 1;
2046 else if (pixels > 1)
2047 return 1 + (pixels - 1) * ps;
2048 else if (pixels < 0)
2049 return 1 - (-pixels + 1) * ps;
2050
2051 BUG();
2052}
2053
2054static void calc_offset(u16 screen_width, u16 width,
2055 u32 fourcc, bool fieldmode, unsigned int field_offset,
2056 unsigned int *offset0, unsigned int *offset1,
2057 s32 *row_inc, s32 *pix_inc, int x_predecim, int y_predecim,
2058 enum omap_dss_rotation_type rotation_type, u8 rotation)
2059{
2060 u8 ps;
2061
2062 ps = color_mode_to_bpp(fourcc) / 8;
2063
2064 DSSDBG("scrw %d, width %d\n", screen_width, width);
2065
2066 if (rotation_type == OMAP_DSS_ROT_TILER &&
2067 (fourcc == DRM_FORMAT_UYVY || fourcc == DRM_FORMAT_YUYV) &&
2068 drm_rotation_90_or_270(rotation)) {
2069 /*
2070 * HACK: ROW_INC needs to be calculated with TILER units.
2071 * We get such 'screen_width' that multiplying it with the
2072 * YUV422 pixel size gives the correct TILER container width.
2073 * However, 'width' is in pixels and multiplying it with YUV422
2074 * pixel size gives incorrect result. We thus multiply it here
2075 * with 2 to match the 32 bit TILER unit size.
2076 */
2077 width *= 2;
2078 }
2079
2080 /*
2081 * field 0 = even field = bottom field
2082 * field 1 = odd field = top field
2083 */
2084 *offset0 = field_offset * screen_width * ps;
2085 *offset1 = 0;
2086
2087 *row_inc = pixinc(1 + (y_predecim * screen_width - width * x_predecim) +
2088 (fieldmode ? screen_width : 0), ps);
2089 if (fourcc == DRM_FORMAT_YUYV || fourcc == DRM_FORMAT_UYVY)
2090 *pix_inc = pixinc(x_predecim, 2 * ps);
2091 else
2092 *pix_inc = pixinc(x_predecim, ps);
2093}
2094
2095/*
2096 * This function is used to avoid synclosts in OMAP3, because of some
2097 * undocumented horizontal position and timing related limitations.
2098 */
2099static int check_horiz_timing_omap3(unsigned long pclk, unsigned long lclk,
2100 const struct videomode *vm, u16 pos_x,
2101 u16 width, u16 height, u16 out_width, u16 out_height,
2102 bool five_taps)
2103{
2104 const int ds = DIV_ROUND_UP(height, out_height);
2105 unsigned long nonactive;
2106 static const u8 limits[3] = { 8, 10, 20 };
2107 u64 val, blank;
2108 int i;
2109
2110 nonactive = vm->hactive + vm->hfront_porch + vm->hsync_len +
2111 vm->hback_porch - out_width;
2112
2113 i = 0;
2114 if (out_height < height)
2115 i++;
2116 if (out_width < width)
2117 i++;
2118 blank = div_u64((u64)(vm->hback_porch + vm->hsync_len + vm->hfront_porch) *
2119 lclk, pclk);
2120 DSSDBG("blanking period + ppl = %llu (limit = %u)\n", blank, limits[i]);
2121 if (blank <= limits[i])
2122 return -EINVAL;
2123
2124 /* FIXME add checks for 3-tap filter once the limitations are known */
2125 if (!five_taps)
2126 return 0;
2127
2128 /*
2129 * Pixel data should be prepared before visible display point starts.
2130 * So, atleast DS-2 lines must have already been fetched by DISPC
2131 * during nonactive - pos_x period.
2132 */
2133 val = div_u64((u64)(nonactive - pos_x) * lclk, pclk);
2134 DSSDBG("(nonactive - pos_x) * pcd = %llu max(0, DS - 2) * width = %d\n",
2135 val, max(0, ds - 2) * width);
2136 if (val < max(0, ds - 2) * width)
2137 return -EINVAL;
2138
2139 /*
2140 * All lines need to be refilled during the nonactive period of which
2141 * only one line can be loaded during the active period. So, atleast
2142 * DS - 1 lines should be loaded during nonactive period.
2143 */
2144 val = div_u64((u64)nonactive * lclk, pclk);
2145 DSSDBG("nonactive * pcd = %llu, max(0, DS - 1) * width = %d\n",
2146 val, max(0, ds - 1) * width);
2147 if (val < max(0, ds - 1) * width)
2148 return -EINVAL;
2149
2150 return 0;
2151}
2152
2153static unsigned long calc_core_clk_five_taps(unsigned long pclk,
2154 const struct videomode *vm, u16 width,
2155 u16 height, u16 out_width, u16 out_height,
2156 u32 fourcc)
2157{
2158 u32 core_clk = 0;
2159 u64 tmp;
2160
2161 if (height <= out_height && width <= out_width)
2162 return (unsigned long) pclk;
2163
2164 if (height > out_height) {
2165 unsigned int ppl = vm->hactive;
2166
2167 tmp = (u64)pclk * height * out_width;
2168 do_div(tmp, 2 * out_height * ppl);
2169 core_clk = tmp;
2170
2171 if (height > 2 * out_height) {
2172 if (ppl == out_width)
2173 return 0;
2174
2175 tmp = (u64)pclk * (height - 2 * out_height) * out_width;
2176 do_div(tmp, 2 * out_height * (ppl - out_width));
2177 core_clk = max_t(u32, core_clk, tmp);
2178 }
2179 }
2180
2181 if (width > out_width) {
2182 tmp = (u64)pclk * width;
2183 do_div(tmp, out_width);
2184 core_clk = max_t(u32, core_clk, tmp);
2185
2186 if (fourcc == DRM_FORMAT_XRGB8888)
2187 core_clk <<= 1;
2188 }
2189
2190 return core_clk;
2191}
2192
2193static unsigned long calc_core_clk_24xx(unsigned long pclk, u16 width,
2194 u16 height, u16 out_width, u16 out_height, bool mem_to_mem)
2195{
2196 if (height > out_height && width > out_width)
2197 return pclk * 4;
2198 else
2199 return pclk * 2;
2200}
2201
2202static unsigned long calc_core_clk_34xx(unsigned long pclk, u16 width,
2203 u16 height, u16 out_width, u16 out_height, bool mem_to_mem)
2204{
2205 unsigned int hf, vf;
2206
2207 /*
2208 * FIXME how to determine the 'A' factor
2209 * for the no downscaling case ?
2210 */
2211
2212 if (width > 3 * out_width)
2213 hf = 4;
2214 else if (width > 2 * out_width)
2215 hf = 3;
2216 else if (width > out_width)
2217 hf = 2;
2218 else
2219 hf = 1;
2220 if (height > out_height)
2221 vf = 2;
2222 else
2223 vf = 1;
2224
2225 return pclk * vf * hf;
2226}
2227
2228static unsigned long calc_core_clk_44xx(unsigned long pclk, u16 width,
2229 u16 height, u16 out_width, u16 out_height, bool mem_to_mem)
2230{
2231 /*
2232 * If the overlay/writeback is in mem to mem mode, there are no
2233 * downscaling limitations with respect to pixel clock, return 1 as
2234 * required core clock to represent that we have sufficient enough
2235 * core clock to do maximum downscaling
2236 */
2237 if (mem_to_mem)
2238 return 1;
2239
2240 if (width > out_width)
2241 return DIV_ROUND_UP(pclk, out_width) * width;
2242 else
2243 return pclk;
2244}
2245
2246static int dispc_ovl_calc_scaling_24xx(struct dispc_device *dispc,
2247 unsigned long pclk, unsigned long lclk,
2248 const struct videomode *vm,
2249 u16 width, u16 height,
2250 u16 out_width, u16 out_height,
2251 u32 fourcc, bool *five_taps,
2252 int *x_predecim, int *y_predecim,
2253 int *decim_x, int *decim_y,
2254 u16 pos_x, unsigned long *core_clk,
2255 bool mem_to_mem)
2256{
2257 int error;
2258 u16 in_width, in_height;
2259 int min_factor = min(*decim_x, *decim_y);
2260 const int maxsinglelinewidth = dispc->feat->max_line_width;
2261
2262 *five_taps = false;
2263
2264 do {
2265 in_height = height / *decim_y;
2266 in_width = width / *decim_x;
2267 *core_clk = dispc->feat->calc_core_clk(pclk, in_width,
2268 in_height, out_width, out_height, mem_to_mem);
2269 error = (in_width > maxsinglelinewidth || !*core_clk ||
2270 *core_clk > dispc_core_clk_rate(dispc));
2271 if (error) {
2272 if (*decim_x == *decim_y) {
2273 *decim_x = min_factor;
2274 ++*decim_y;
2275 } else {
2276 swap(*decim_x, *decim_y);
2277 if (*decim_x < *decim_y)
2278 ++*decim_x;
2279 }
2280 }
2281 } while (*decim_x <= *x_predecim && *decim_y <= *y_predecim && error);
2282
2283 if (error) {
2284 DSSERR("failed to find scaling settings\n");
2285 return -EINVAL;
2286 }
2287
2288 if (in_width > maxsinglelinewidth) {
2289 DSSERR("Cannot scale max input width exceeded\n");
2290 return -EINVAL;
2291 }
2292 return 0;
2293}
2294
2295static int dispc_ovl_calc_scaling_34xx(struct dispc_device *dispc,
2296 unsigned long pclk, unsigned long lclk,
2297 const struct videomode *vm,
2298 u16 width, u16 height,
2299 u16 out_width, u16 out_height,
2300 u32 fourcc, bool *five_taps,
2301 int *x_predecim, int *y_predecim,
2302 int *decim_x, int *decim_y,
2303 u16 pos_x, unsigned long *core_clk,
2304 bool mem_to_mem)
2305{
2306 int error;
2307 u16 in_width, in_height;
2308 const int maxsinglelinewidth = dispc->feat->max_line_width;
2309
2310 do {
2311 in_height = height / *decim_y;
2312 in_width = width / *decim_x;
2313 *five_taps = in_height > out_height;
2314
2315 if (in_width > maxsinglelinewidth)
2316 if (in_height > out_height &&
2317 in_height < out_height * 2)
2318 *five_taps = false;
2319again:
2320 if (*five_taps)
2321 *core_clk = calc_core_clk_five_taps(pclk, vm,
2322 in_width, in_height, out_width,
2323 out_height, fourcc);
2324 else
2325 *core_clk = dispc->feat->calc_core_clk(pclk, in_width,
2326 in_height, out_width, out_height,
2327 mem_to_mem);
2328
2329 error = check_horiz_timing_omap3(pclk, lclk, vm,
2330 pos_x, in_width, in_height, out_width,
2331 out_height, *five_taps);
2332 if (error && *five_taps) {
2333 *five_taps = false;
2334 goto again;
2335 }
2336
2337 error = (error || in_width > maxsinglelinewidth * 2 ||
2338 (in_width > maxsinglelinewidth && *five_taps) ||
2339 !*core_clk || *core_clk > dispc_core_clk_rate(dispc));
2340
2341 if (!error) {
2342 /* verify that we're inside the limits of scaler */
2343 if (in_width / 4 > out_width)
2344 error = 1;
2345
2346 if (*five_taps) {
2347 if (in_height / 4 > out_height)
2348 error = 1;
2349 } else {
2350 if (in_height / 2 > out_height)
2351 error = 1;
2352 }
2353 }
2354
2355 if (error)
2356 ++*decim_y;
2357 } while (*decim_x <= *x_predecim && *decim_y <= *y_predecim && error);
2358
2359 if (error) {
2360 DSSERR("failed to find scaling settings\n");
2361 return -EINVAL;
2362 }
2363
2364 if (check_horiz_timing_omap3(pclk, lclk, vm, pos_x, in_width,
2365 in_height, out_width, out_height, *five_taps)) {
2366 DSSERR("horizontal timing too tight\n");
2367 return -EINVAL;
2368 }
2369
2370 if (in_width > (maxsinglelinewidth * 2)) {
2371 DSSERR("Cannot setup scaling\n");
2372 DSSERR("width exceeds maximum width possible\n");
2373 return -EINVAL;
2374 }
2375
2376 if (in_width > maxsinglelinewidth && *five_taps) {
2377 DSSERR("cannot setup scaling with five taps\n");
2378 return -EINVAL;
2379 }
2380 return 0;
2381}
2382
2383static int dispc_ovl_calc_scaling_44xx(struct dispc_device *dispc,
2384 unsigned long pclk, unsigned long lclk,
2385 const struct videomode *vm,
2386 u16 width, u16 height,
2387 u16 out_width, u16 out_height,
2388 u32 fourcc, bool *five_taps,
2389 int *x_predecim, int *y_predecim,
2390 int *decim_x, int *decim_y,
2391 u16 pos_x, unsigned long *core_clk,
2392 bool mem_to_mem)
2393{
2394 u16 in_width, in_width_max;
2395 int decim_x_min = *decim_x;
2396 u16 in_height = height / *decim_y;
2397 const int maxsinglelinewidth = dispc->feat->max_line_width;
2398 const int maxdownscale = dispc->feat->max_downscale;
2399
2400 if (mem_to_mem) {
2401 in_width_max = out_width * maxdownscale;
2402 } else {
2403 in_width_max = dispc_core_clk_rate(dispc)
2404 / DIV_ROUND_UP(pclk, out_width);
2405 }
2406
2407 *decim_x = DIV_ROUND_UP(width, in_width_max);
2408
2409 *decim_x = max(*decim_x, decim_x_min);
2410 if (*decim_x > *x_predecim)
2411 return -EINVAL;
2412
2413 do {
2414 in_width = width / *decim_x;
2415 } while (*decim_x <= *x_predecim &&
2416 in_width > maxsinglelinewidth && ++*decim_x);
2417
2418 if (in_width > maxsinglelinewidth) {
2419 DSSERR("Cannot scale width exceeds max line width\n");
2420 return -EINVAL;
2421 }
2422
2423 if (*decim_x > 4 && fourcc != DRM_FORMAT_NV12) {
2424 /*
2425 * Let's disable all scaling that requires horizontal
2426 * decimation with higher factor than 4, until we have
2427 * better estimates of what we can and can not
2428 * do. However, NV12 color format appears to work Ok
2429 * with all decimation factors.
2430 *
2431 * When decimating horizontally by more that 4 the dss
2432 * is not able to fetch the data in burst mode. When
2433 * this happens it is hard to tell if there enough
2434 * bandwidth. Despite what theory says this appears to
2435 * be true also for 16-bit color formats.
2436 */
2437 DSSERR("Not enough bandwidth, too much downscaling (x-decimation factor %d > 4)\n", *decim_x);
2438
2439 return -EINVAL;
2440 }
2441
2442 *core_clk = dispc->feat->calc_core_clk(pclk, in_width, in_height,
2443 out_width, out_height, mem_to_mem);
2444 return 0;
2445}
2446
2447enum omap_overlay_caps dispc_ovl_get_caps(struct dispc_device *dispc, enum omap_plane_id plane)
2448{
2449 return dispc->feat->overlay_caps[plane];
2450}
2451
2452#define DIV_FRAC(dividend, divisor) \
2453 ((dividend) * 100 / (divisor) - ((dividend) / (divisor) * 100))
2454
2455static int dispc_ovl_calc_scaling(struct dispc_device *dispc,
2456 enum omap_plane_id plane,
2457 unsigned long pclk, unsigned long lclk,
2458 enum omap_overlay_caps caps,
2459 const struct videomode *vm,
2460 u16 width, u16 height,
2461 u16 out_width, u16 out_height,
2462 u32 fourcc, bool *five_taps,
2463 int *x_predecim, int *y_predecim, u16 pos_x,
2464 enum omap_dss_rotation_type rotation_type,
2465 bool mem_to_mem)
2466{
2467 int maxhdownscale = dispc->feat->max_downscale;
2468 int maxvdownscale = dispc->feat->max_downscale;
2469 const int max_decim_limit = 16;
2470 unsigned long core_clk = 0;
2471 int decim_x, decim_y, ret;
2472
2473 if (width == out_width && height == out_height)
2474 return 0;
2475
2476 if (dispc->feat->supported_scaler_color_modes) {
2477 const u32 *modes = dispc->feat->supported_scaler_color_modes;
2478 unsigned int i;
2479
2480 for (i = 0; modes[i]; ++i) {
2481 if (modes[i] == fourcc)
2482 break;
2483 }
2484
2485 if (modes[i] == 0)
2486 return -EINVAL;
2487 }
2488
2489 if (plane == OMAP_DSS_WB) {
2490 switch (fourcc) {
2491 case DRM_FORMAT_NV12:
2492 maxhdownscale = maxvdownscale = 2;
2493 break;
2494 case DRM_FORMAT_YUYV:
2495 case DRM_FORMAT_UYVY:
2496 maxhdownscale = 2;
2497 maxvdownscale = 4;
2498 break;
2499 default:
2500 break;
2501 }
2502 }
2503 if (!mem_to_mem && (pclk == 0 || vm->pixelclock == 0)) {
2504 DSSERR("cannot calculate scaling settings: pclk is zero\n");
2505 return -EINVAL;
2506 }
2507
2508 if ((caps & OMAP_DSS_OVL_CAP_SCALE) == 0)
2509 return -EINVAL;
2510
2511 if (mem_to_mem) {
2512 *x_predecim = *y_predecim = 1;
2513 } else {
2514 *x_predecim = max_decim_limit;
2515 *y_predecim = (rotation_type == OMAP_DSS_ROT_TILER &&
2516 dispc_has_feature(dispc, FEAT_BURST_2D)) ?
2517 2 : max_decim_limit;
2518 }
2519
2520 decim_x = DIV_ROUND_UP(DIV_ROUND_UP(width, out_width), maxhdownscale);
2521 decim_y = DIV_ROUND_UP(DIV_ROUND_UP(height, out_height), maxvdownscale);
2522
2523 if (decim_x > *x_predecim || out_width > width * 8)
2524 return -EINVAL;
2525
2526 if (decim_y > *y_predecim || out_height > height * 8)
2527 return -EINVAL;
2528
2529 ret = dispc->feat->calc_scaling(dispc, pclk, lclk, vm, width, height,
2530 out_width, out_height, fourcc,
2531 five_taps, x_predecim, y_predecim,
2532 &decim_x, &decim_y, pos_x, &core_clk,
2533 mem_to_mem);
2534 if (ret)
2535 return ret;
2536
2537 DSSDBG("%dx%d -> %dx%d (%d.%02d x %d.%02d), decim %dx%d %dx%d (%d.%02d x %d.%02d), taps %d, req clk %lu, cur clk %lu\n",
2538 width, height,
2539 out_width, out_height,
2540 out_width / width, DIV_FRAC(out_width, width),
2541 out_height / height, DIV_FRAC(out_height, height),
2542
2543 decim_x, decim_y,
2544 width / decim_x, height / decim_y,
2545 out_width / (width / decim_x), DIV_FRAC(out_width, width / decim_x),
2546 out_height / (height / decim_y), DIV_FRAC(out_height, height / decim_y),
2547
2548 *five_taps ? 5 : 3,
2549 core_clk, dispc_core_clk_rate(dispc));
2550
2551 if (!core_clk || core_clk > dispc_core_clk_rate(dispc)) {
2552 DSSERR("failed to set up scaling, "
2553 "required core clk rate = %lu Hz, "
2554 "current core clk rate = %lu Hz\n",
2555 core_clk, dispc_core_clk_rate(dispc));
2556 return -EINVAL;
2557 }
2558
2559 *x_predecim = decim_x;
2560 *y_predecim = decim_y;
2561 return 0;
2562}
2563
2564void dispc_ovl_get_max_size(struct dispc_device *dispc, u16 *width, u16 *height)
2565{
2566 *width = dispc->feat->ovl_width_max;
2567 *height = dispc->feat->ovl_height_max;
2568}
2569
2570static int dispc_ovl_setup_common(struct dispc_device *dispc,
2571 enum omap_plane_id plane,
2572 enum omap_overlay_caps caps,
2573 u32 paddr, u32 p_uv_addr,
2574 u16 screen_width, int pos_x, int pos_y,
2575 u16 width, u16 height,
2576 u16 out_width, u16 out_height,
2577 u32 fourcc, u8 rotation, u8 zorder,
2578 u8 pre_mult_alpha, u8 global_alpha,
2579 enum omap_dss_rotation_type rotation_type,
2580 bool replication, const struct videomode *vm,
2581 bool mem_to_mem,
2582 enum drm_color_encoding color_encoding,
2583 enum drm_color_range color_range)
2584{
2585 bool five_taps = true;
2586 bool fieldmode = false;
2587 int r, cconv = 0;
2588 unsigned int offset0, offset1;
2589 s32 row_inc;
2590 s32 pix_inc;
2591 u16 frame_width;
2592 unsigned int field_offset = 0;
2593 u16 in_height = height;
2594 u16 in_width = width;
2595 int x_predecim = 1, y_predecim = 1;
2596 bool ilace = !!(vm->flags & DISPLAY_FLAGS_INTERLACED);
2597 unsigned long pclk = dispc_plane_pclk_rate(dispc, plane);
2598 unsigned long lclk = dispc_plane_lclk_rate(dispc, plane);
2599 const struct drm_format_info *info;
2600
2601 info = drm_format_info(fourcc);
2602
2603 /* when setting up WB, dispc_plane_pclk_rate() returns 0 */
2604 if (plane == OMAP_DSS_WB)
2605 pclk = vm->pixelclock;
2606
2607 if (paddr == 0 && rotation_type != OMAP_DSS_ROT_TILER)
2608 return -EINVAL;
2609
2610 if (info->is_yuv && (in_width & 1)) {
2611 DSSERR("input width %d is not even for YUV format\n", in_width);
2612 return -EINVAL;
2613 }
2614
2615 out_width = out_width == 0 ? width : out_width;
2616 out_height = out_height == 0 ? height : out_height;
2617
2618 if (plane != OMAP_DSS_WB) {
2619 if (ilace && height == out_height)
2620 fieldmode = true;
2621
2622 if (ilace) {
2623 if (fieldmode)
2624 in_height /= 2;
2625 pos_y /= 2;
2626 out_height /= 2;
2627
2628 DSSDBG("adjusting for ilace: height %d, pos_y %d, out_height %d\n",
2629 in_height, pos_y, out_height);
2630 }
2631 }
2632
2633 if (!dispc_ovl_color_mode_supported(dispc, plane, fourcc))
2634 return -EINVAL;
2635
2636 r = dispc_ovl_calc_scaling(dispc, plane, pclk, lclk, caps, vm, in_width,
2637 in_height, out_width, out_height, fourcc,
2638 &five_taps, &x_predecim, &y_predecim, pos_x,
2639 rotation_type, mem_to_mem);
2640 if (r)
2641 return r;
2642
2643 in_width = in_width / x_predecim;
2644 in_height = in_height / y_predecim;
2645
2646 if (x_predecim > 1 || y_predecim > 1)
2647 DSSDBG("predecimation %d x %x, new input size %d x %d\n",
2648 x_predecim, y_predecim, in_width, in_height);
2649
2650 if (info->is_yuv && (in_width & 1)) {
2651 DSSDBG("predecimated input width is not even for YUV format\n");
2652 DSSDBG("adjusting input width %d -> %d\n",
2653 in_width, in_width & ~1);
2654
2655 in_width &= ~1;
2656 }
2657
2658 if (info->is_yuv)
2659 cconv = 1;
2660
2661 if (ilace && !fieldmode) {
2662 /*
2663 * when downscaling the bottom field may have to start several
2664 * source lines below the top field. Unfortunately ACCUI
2665 * registers will only hold the fractional part of the offset
2666 * so the integer part must be added to the base address of the
2667 * bottom field.
2668 */
2669 if (!in_height || in_height == out_height)
2670 field_offset = 0;
2671 else
2672 field_offset = in_height / out_height / 2;
2673 }
2674
2675 /* Fields are independent but interleaved in memory. */
2676 if (fieldmode)
2677 field_offset = 1;
2678
2679 offset0 = 0;
2680 offset1 = 0;
2681 row_inc = 0;
2682 pix_inc = 0;
2683
2684 if (plane == OMAP_DSS_WB)
2685 frame_width = out_width;
2686 else
2687 frame_width = in_width;
2688
2689 calc_offset(screen_width, frame_width,
2690 fourcc, fieldmode, field_offset,
2691 &offset0, &offset1, &row_inc, &pix_inc,
2692 x_predecim, y_predecim,
2693 rotation_type, rotation);
2694
2695 DSSDBG("offset0 %u, offset1 %u, row_inc %d, pix_inc %d\n",
2696 offset0, offset1, row_inc, pix_inc);
2697
2698 dispc_ovl_set_color_mode(dispc, plane, fourcc);
2699
2700 dispc_ovl_configure_burst_type(dispc, plane, rotation_type);
2701
2702 if (dispc->feat->reverse_ilace_field_order)
2703 swap(offset0, offset1);
2704
2705 dispc_ovl_set_ba0(dispc, plane, paddr + offset0);
2706 dispc_ovl_set_ba1(dispc, plane, paddr + offset1);
2707
2708 if (fourcc == DRM_FORMAT_NV12) {
2709 dispc_ovl_set_ba0_uv(dispc, plane, p_uv_addr + offset0);
2710 dispc_ovl_set_ba1_uv(dispc, plane, p_uv_addr + offset1);
2711 }
2712
2713 if (dispc->feat->last_pixel_inc_missing)
2714 row_inc += pix_inc - 1;
2715
2716 dispc_ovl_set_row_inc(dispc, plane, row_inc);
2717 dispc_ovl_set_pix_inc(dispc, plane, pix_inc);
2718
2719 DSSDBG("%d,%d %dx%d -> %dx%d\n", pos_x, pos_y, in_width,
2720 in_height, out_width, out_height);
2721
2722 dispc_ovl_set_pos(dispc, plane, caps, pos_x, pos_y);
2723
2724 dispc_ovl_set_input_size(dispc, plane, in_width, in_height);
2725
2726 if (caps & OMAP_DSS_OVL_CAP_SCALE) {
2727 dispc_ovl_set_scaling(dispc, plane, in_width, in_height,
2728 out_width, out_height, ilace, five_taps,
2729 fieldmode, fourcc, rotation);
2730 dispc_ovl_set_output_size(dispc, plane, out_width, out_height);
2731 dispc_ovl_set_vid_color_conv(dispc, plane, cconv);
2732
2733 if (plane != OMAP_DSS_WB)
2734 dispc_ovl_set_csc(dispc, plane, color_encoding, color_range);
2735 }
2736
2737 dispc_ovl_set_rotation_attrs(dispc, plane, rotation, rotation_type,
2738 fourcc);
2739
2740 dispc_ovl_set_zorder(dispc, plane, caps, zorder);
2741 dispc_ovl_set_pre_mult_alpha(dispc, plane, caps, pre_mult_alpha);
2742 dispc_ovl_setup_global_alpha(dispc, plane, caps, global_alpha);
2743
2744 dispc_ovl_enable_replication(dispc, plane, caps, replication);
2745
2746 return 0;
2747}
2748
2749int dispc_ovl_setup(struct dispc_device *dispc,
2750 enum omap_plane_id plane,
2751 const struct omap_overlay_info *oi,
2752 const struct videomode *vm, bool mem_to_mem,
2753 enum omap_channel channel)
2754{
2755 int r;
2756 enum omap_overlay_caps caps = dispc->feat->overlay_caps[plane];
2757 const bool replication = true;
2758
2759 DSSDBG("dispc_ovl_setup %d, pa %pad, pa_uv %pad, sw %d, %d,%d, %dx%d ->"
2760 " %dx%d, cmode %x, rot %d, chan %d repl %d\n",
2761 plane, &oi->paddr, &oi->p_uv_addr, oi->screen_width, oi->pos_x,
2762 oi->pos_y, oi->width, oi->height, oi->out_width, oi->out_height,
2763 oi->fourcc, oi->rotation, channel, replication);
2764
2765 dispc_ovl_set_channel_out(dispc, plane, channel);
2766
2767 r = dispc_ovl_setup_common(dispc, plane, caps, oi->paddr, oi->p_uv_addr,
2768 oi->screen_width, oi->pos_x, oi->pos_y, oi->width, oi->height,
2769 oi->out_width, oi->out_height, oi->fourcc, oi->rotation,
2770 oi->zorder, oi->pre_mult_alpha, oi->global_alpha,
2771 oi->rotation_type, replication, vm, mem_to_mem,
2772 oi->color_encoding, oi->color_range);
2773
2774 return r;
2775}
2776
2777int dispc_ovl_enable(struct dispc_device *dispc,
2778 enum omap_plane_id plane, bool enable)
2779{
2780 DSSDBG("dispc_enable_plane %d, %d\n", plane, enable);
2781
2782 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), enable ? 1 : 0, 0, 0);
2783
2784 return 0;
2785}
2786
2787static void dispc_lcd_enable_signal_polarity(struct dispc_device *dispc,
2788 bool act_high)
2789{
2790 if (!dispc_has_feature(dispc, FEAT_LCDENABLEPOL))
2791 return;
2792
2793 REG_FLD_MOD(dispc, DISPC_CONTROL, act_high ? 1 : 0, 29, 29);
2794}
2795
2796void dispc_lcd_enable_signal(struct dispc_device *dispc, bool enable)
2797{
2798 if (!dispc_has_feature(dispc, FEAT_LCDENABLESIGNAL))
2799 return;
2800
2801 REG_FLD_MOD(dispc, DISPC_CONTROL, enable ? 1 : 0, 28, 28);
2802}
2803
2804void dispc_pck_free_enable(struct dispc_device *dispc, bool enable)
2805{
2806 if (!dispc_has_feature(dispc, FEAT_PCKFREEENABLE))
2807 return;
2808
2809 REG_FLD_MOD(dispc, DISPC_CONTROL, enable ? 1 : 0, 27, 27);
2810}
2811
2812static void dispc_mgr_enable_fifohandcheck(struct dispc_device *dispc,
2813 enum omap_channel channel,
2814 bool enable)
2815{
2816 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_FIFOHANDCHECK, enable);
2817}
2818
2819
2820static void dispc_mgr_set_lcd_type_tft(struct dispc_device *dispc,
2821 enum omap_channel channel)
2822{
2823 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_STNTFT, 1);
2824}
2825
2826static void dispc_set_loadmode(struct dispc_device *dispc,
2827 enum omap_dss_load_mode mode)
2828{
2829 REG_FLD_MOD(dispc, DISPC_CONFIG, mode, 2, 1);
2830}
2831
2832
2833static void dispc_mgr_set_default_color(struct dispc_device *dispc,
2834 enum omap_channel channel, u32 color)
2835{
2836 dispc_write_reg(dispc, DISPC_DEFAULT_COLOR(channel), color);
2837}
2838
2839static void dispc_mgr_set_trans_key(struct dispc_device *dispc,
2840 enum omap_channel ch,
2841 enum omap_dss_trans_key_type type,
2842 u32 trans_key)
2843{
2844 mgr_fld_write(dispc, ch, DISPC_MGR_FLD_TCKSELECTION, type);
2845
2846 dispc_write_reg(dispc, DISPC_TRANS_COLOR(ch), trans_key);
2847}
2848
2849static void dispc_mgr_enable_trans_key(struct dispc_device *dispc,
2850 enum omap_channel ch, bool enable)
2851{
2852 mgr_fld_write(dispc, ch, DISPC_MGR_FLD_TCKENABLE, enable);
2853}
2854
2855static void dispc_mgr_enable_alpha_fixed_zorder(struct dispc_device *dispc,
2856 enum omap_channel ch,
2857 bool enable)
2858{
2859 if (!dispc_has_feature(dispc, FEAT_ALPHA_FIXED_ZORDER))
2860 return;
2861
2862 if (ch == OMAP_DSS_CHANNEL_LCD)
2863 REG_FLD_MOD(dispc, DISPC_CONFIG, enable, 18, 18);
2864 else if (ch == OMAP_DSS_CHANNEL_DIGIT)
2865 REG_FLD_MOD(dispc, DISPC_CONFIG, enable, 19, 19);
2866}
2867
2868void dispc_mgr_setup(struct dispc_device *dispc,
2869 enum omap_channel channel,
2870 const struct omap_overlay_manager_info *info)
2871{
2872 dispc_mgr_set_default_color(dispc, channel, info->default_color);
2873 dispc_mgr_set_trans_key(dispc, channel, info->trans_key_type,
2874 info->trans_key);
2875 dispc_mgr_enable_trans_key(dispc, channel, info->trans_enabled);
2876 dispc_mgr_enable_alpha_fixed_zorder(dispc, channel,
2877 info->partial_alpha_enabled);
2878 if (dispc_has_feature(dispc, FEAT_CPR)) {
2879 dispc_mgr_enable_cpr(dispc, channel, info->cpr_enable);
2880 dispc_mgr_set_cpr_coef(dispc, channel, &info->cpr_coefs);
2881 }
2882}
2883
2884static void dispc_mgr_set_tft_data_lines(struct dispc_device *dispc,
2885 enum omap_channel channel,
2886 u8 data_lines)
2887{
2888 int code;
2889
2890 switch (data_lines) {
2891 case 12:
2892 code = 0;
2893 break;
2894 case 16:
2895 code = 1;
2896 break;
2897 case 18:
2898 code = 2;
2899 break;
2900 case 24:
2901 code = 3;
2902 break;
2903 default:
2904 BUG();
2905 return;
2906 }
2907
2908 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_TFTDATALINES, code);
2909}
2910
2911static void dispc_mgr_set_io_pad_mode(struct dispc_device *dispc,
2912 enum dss_io_pad_mode mode)
2913{
2914 u32 l;
2915 int gpout0, gpout1;
2916
2917 switch (mode) {
2918 case DSS_IO_PAD_MODE_RESET:
2919 gpout0 = 0;
2920 gpout1 = 0;
2921 break;
2922 case DSS_IO_PAD_MODE_RFBI:
2923 gpout0 = 1;
2924 gpout1 = 0;
2925 break;
2926 case DSS_IO_PAD_MODE_BYPASS:
2927 gpout0 = 1;
2928 gpout1 = 1;
2929 break;
2930 default:
2931 BUG();
2932 return;
2933 }
2934
2935 l = dispc_read_reg(dispc, DISPC_CONTROL);
2936 l = FLD_MOD(l, gpout0, 15, 15);
2937 l = FLD_MOD(l, gpout1, 16, 16);
2938 dispc_write_reg(dispc, DISPC_CONTROL, l);
2939}
2940
2941static void dispc_mgr_enable_stallmode(struct dispc_device *dispc,
2942 enum omap_channel channel, bool enable)
2943{
2944 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_STALLMODE, enable);
2945}
2946
2947void dispc_mgr_set_lcd_config(struct dispc_device *dispc,
2948 enum omap_channel channel,
2949 const struct dss_lcd_mgr_config *config)
2950{
2951 dispc_mgr_set_io_pad_mode(dispc, config->io_pad_mode);
2952
2953 dispc_mgr_enable_stallmode(dispc, channel, config->stallmode);
2954 dispc_mgr_enable_fifohandcheck(dispc, channel, config->fifohandcheck);
2955
2956 dispc_mgr_set_clock_div(dispc, channel, &config->clock_info);
2957
2958 dispc_mgr_set_tft_data_lines(dispc, channel, config->video_port_width);
2959
2960 dispc_lcd_enable_signal_polarity(dispc, config->lcden_sig_polarity);
2961
2962 dispc_mgr_set_lcd_type_tft(dispc, channel);
2963}
2964
2965static bool _dispc_mgr_size_ok(struct dispc_device *dispc,
2966 u16 width, u16 height)
2967{
2968 return width <= dispc->feat->mgr_width_max &&
2969 height <= dispc->feat->mgr_height_max;
2970}
2971
2972static bool _dispc_lcd_timings_ok(struct dispc_device *dispc,
2973 int hsync_len, int hfp, int hbp,
2974 int vsw, int vfp, int vbp)
2975{
2976 if (hsync_len < 1 || hsync_len > dispc->feat->sw_max ||
2977 hfp < 1 || hfp > dispc->feat->hp_max ||
2978 hbp < 1 || hbp > dispc->feat->hp_max ||
2979 vsw < 1 || vsw > dispc->feat->sw_max ||
2980 vfp < 0 || vfp > dispc->feat->vp_max ||
2981 vbp < 0 || vbp > dispc->feat->vp_max)
2982 return false;
2983 return true;
2984}
2985
2986static bool _dispc_mgr_pclk_ok(struct dispc_device *dispc,
2987 enum omap_channel channel,
2988 unsigned long pclk)
2989{
2990 if (dss_mgr_is_lcd(channel))
2991 return pclk <= dispc->feat->max_lcd_pclk;
2992 else
2993 return pclk <= dispc->feat->max_tv_pclk;
2994}
2995
2996int dispc_mgr_check_timings(struct dispc_device *dispc,
2997 enum omap_channel channel,
2998 const struct videomode *vm)
2999{
3000 if (!_dispc_mgr_size_ok(dispc, vm->hactive, vm->vactive))
3001 return MODE_BAD;
3002
3003 if (!_dispc_mgr_pclk_ok(dispc, channel, vm->pixelclock))
3004 return MODE_BAD;
3005
3006 if (dss_mgr_is_lcd(channel)) {
3007 /* TODO: OMAP4+ supports interlace for LCD outputs */
3008 if (vm->flags & DISPLAY_FLAGS_INTERLACED)
3009 return MODE_BAD;
3010
3011 if (!_dispc_lcd_timings_ok(dispc, vm->hsync_len,
3012 vm->hfront_porch, vm->hback_porch,
3013 vm->vsync_len, vm->vfront_porch,
3014 vm->vback_porch))
3015 return MODE_BAD;
3016 }
3017
3018 return MODE_OK;
3019}
3020
3021static void _dispc_mgr_set_lcd_timings(struct dispc_device *dispc,
3022 enum omap_channel channel,
3023 const struct videomode *vm)
3024{
3025 u32 timing_h, timing_v, l;
3026 bool onoff, rf, ipc, vs, hs, de;
3027
3028 timing_h = FLD_VAL(vm->hsync_len - 1, dispc->feat->sw_start, 0) |
3029 FLD_VAL(vm->hfront_porch - 1, dispc->feat->fp_start, 8) |
3030 FLD_VAL(vm->hback_porch - 1, dispc->feat->bp_start, 20);
3031 timing_v = FLD_VAL(vm->vsync_len - 1, dispc->feat->sw_start, 0) |
3032 FLD_VAL(vm->vfront_porch, dispc->feat->fp_start, 8) |
3033 FLD_VAL(vm->vback_porch, dispc->feat->bp_start, 20);
3034
3035 dispc_write_reg(dispc, DISPC_TIMING_H(channel), timing_h);
3036 dispc_write_reg(dispc, DISPC_TIMING_V(channel), timing_v);
3037
3038 vs = !!(vm->flags & DISPLAY_FLAGS_VSYNC_LOW);
3039 hs = !!(vm->flags & DISPLAY_FLAGS_HSYNC_LOW);
3040 de = !!(vm->flags & DISPLAY_FLAGS_DE_LOW);
3041 ipc = !!(vm->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE);
3042 onoff = true; /* always use the 'rf' setting */
3043 rf = !!(vm->flags & DISPLAY_FLAGS_SYNC_POSEDGE);
3044
3045 l = FLD_VAL(onoff, 17, 17) |
3046 FLD_VAL(rf, 16, 16) |
3047 FLD_VAL(de, 15, 15) |
3048 FLD_VAL(ipc, 14, 14) |
3049 FLD_VAL(hs, 13, 13) |
3050 FLD_VAL(vs, 12, 12);
3051
3052 /* always set ALIGN bit when available */
3053 if (dispc->feat->supports_sync_align)
3054 l |= (1 << 18);
3055
3056 dispc_write_reg(dispc, DISPC_POL_FREQ(channel), l);
3057
3058 if (dispc->syscon_pol) {
3059 const int shifts[] = {
3060 [OMAP_DSS_CHANNEL_LCD] = 0,
3061 [OMAP_DSS_CHANNEL_LCD2] = 1,
3062 [OMAP_DSS_CHANNEL_LCD3] = 2,
3063 };
3064
3065 u32 mask, val;
3066
3067 mask = (1 << 0) | (1 << 3) | (1 << 6);
3068 val = (rf << 0) | (ipc << 3) | (onoff << 6);
3069
3070 mask <<= 16 + shifts[channel];
3071 val <<= 16 + shifts[channel];
3072
3073 regmap_update_bits(dispc->syscon_pol, dispc->syscon_pol_offset,
3074 mask, val);
3075 }
3076}
3077
3078static int vm_flag_to_int(enum display_flags flags, enum display_flags high,
3079 enum display_flags low)
3080{
3081 if (flags & high)
3082 return 1;
3083 if (flags & low)
3084 return -1;
3085 return 0;
3086}
3087
3088/* change name to mode? */
3089void dispc_mgr_set_timings(struct dispc_device *dispc,
3090 enum omap_channel channel,
3091 const struct videomode *vm)
3092{
3093 unsigned int xtot, ytot;
3094 unsigned long ht, vt;
3095 struct videomode t = *vm;
3096
3097 DSSDBG("channel %d xres %u yres %u\n", channel, t.hactive, t.vactive);
3098
3099 if (dispc_mgr_check_timings(dispc, channel, &t)) {
3100 BUG();
3101 return;
3102 }
3103
3104 if (dss_mgr_is_lcd(channel)) {
3105 _dispc_mgr_set_lcd_timings(dispc, channel, &t);
3106
3107 xtot = t.hactive + t.hfront_porch + t.hsync_len + t.hback_porch;
3108 ytot = t.vactive + t.vfront_porch + t.vsync_len + t.vback_porch;
3109
3110 ht = vm->pixelclock / xtot;
3111 vt = vm->pixelclock / xtot / ytot;
3112
3113 DSSDBG("pck %lu\n", vm->pixelclock);
3114 DSSDBG("hsync_len %d hfp %d hbp %d vsw %d vfp %d vbp %d\n",
3115 t.hsync_len, t.hfront_porch, t.hback_porch,
3116 t.vsync_len, t.vfront_porch, t.vback_porch);
3117 DSSDBG("vsync_level %d hsync_level %d data_pclk_edge %d de_level %d sync_pclk_edge %d\n",
3118 vm_flag_to_int(t.flags, DISPLAY_FLAGS_VSYNC_HIGH, DISPLAY_FLAGS_VSYNC_LOW),
3119 vm_flag_to_int(t.flags, DISPLAY_FLAGS_HSYNC_HIGH, DISPLAY_FLAGS_HSYNC_LOW),
3120 vm_flag_to_int(t.flags, DISPLAY_FLAGS_PIXDATA_POSEDGE, DISPLAY_FLAGS_PIXDATA_NEGEDGE),
3121 vm_flag_to_int(t.flags, DISPLAY_FLAGS_DE_HIGH, DISPLAY_FLAGS_DE_LOW),
3122 vm_flag_to_int(t.flags, DISPLAY_FLAGS_SYNC_POSEDGE, DISPLAY_FLAGS_SYNC_NEGEDGE));
3123
3124 DSSDBG("hsync %luHz, vsync %luHz\n", ht, vt);
3125 } else {
3126 if (t.flags & DISPLAY_FLAGS_INTERLACED)
3127 t.vactive /= 2;
3128
3129 if (dispc->feat->supports_double_pixel)
3130 REG_FLD_MOD(dispc, DISPC_CONTROL,
3131 !!(t.flags & DISPLAY_FLAGS_DOUBLECLK),
3132 19, 17);
3133 }
3134
3135 dispc_mgr_set_size(dispc, channel, t.hactive, t.vactive);
3136}
3137
3138static void dispc_mgr_set_lcd_divisor(struct dispc_device *dispc,
3139 enum omap_channel channel, u16 lck_div,
3140 u16 pck_div)
3141{
3142 BUG_ON(lck_div < 1);
3143 BUG_ON(pck_div < 1);
3144
3145 dispc_write_reg(dispc, DISPC_DIVISORo(channel),
3146 FLD_VAL(lck_div, 23, 16) | FLD_VAL(pck_div, 7, 0));
3147
3148 if (!dispc_has_feature(dispc, FEAT_CORE_CLK_DIV) &&
3149 channel == OMAP_DSS_CHANNEL_LCD)
3150 dispc->core_clk_rate = dispc_fclk_rate(dispc) / lck_div;
3151}
3152
3153static void dispc_mgr_get_lcd_divisor(struct dispc_device *dispc,
3154 enum omap_channel channel, int *lck_div,
3155 int *pck_div)
3156{
3157 u32 l;
3158 l = dispc_read_reg(dispc, DISPC_DIVISORo(channel));
3159 *lck_div = FLD_GET(l, 23, 16);
3160 *pck_div = FLD_GET(l, 7, 0);
3161}
3162
3163static unsigned long dispc_fclk_rate(struct dispc_device *dispc)
3164{
3165 unsigned long r;
3166 enum dss_clk_source src;
3167
3168 src = dss_get_dispc_clk_source(dispc->dss);
3169
3170 if (src == DSS_CLK_SRC_FCK) {
3171 r = dss_get_dispc_clk_rate(dispc->dss);
3172 } else {
3173 struct dss_pll *pll;
3174 unsigned int clkout_idx;
3175
3176 pll = dss_pll_find_by_src(dispc->dss, src);
3177 clkout_idx = dss_pll_get_clkout_idx_for_src(src);
3178
3179 r = pll->cinfo.clkout[clkout_idx];
3180 }
3181
3182 return r;
3183}
3184
3185static unsigned long dispc_mgr_lclk_rate(struct dispc_device *dispc,
3186 enum omap_channel channel)
3187{
3188 int lcd;
3189 unsigned long r;
3190 enum dss_clk_source src;
3191
3192 /* for TV, LCLK rate is the FCLK rate */
3193 if (!dss_mgr_is_lcd(channel))
3194 return dispc_fclk_rate(dispc);
3195
3196 src = dss_get_lcd_clk_source(dispc->dss, channel);
3197
3198 if (src == DSS_CLK_SRC_FCK) {
3199 r = dss_get_dispc_clk_rate(dispc->dss);
3200 } else {
3201 struct dss_pll *pll;
3202 unsigned int clkout_idx;
3203
3204 pll = dss_pll_find_by_src(dispc->dss, src);
3205 clkout_idx = dss_pll_get_clkout_idx_for_src(src);
3206
3207 r = pll->cinfo.clkout[clkout_idx];
3208 }
3209
3210 lcd = REG_GET(dispc, DISPC_DIVISORo(channel), 23, 16);
3211
3212 return r / lcd;
3213}
3214
3215static unsigned long dispc_mgr_pclk_rate(struct dispc_device *dispc,
3216 enum omap_channel channel)
3217{
3218 unsigned long r;
3219
3220 if (dss_mgr_is_lcd(channel)) {
3221 int pcd;
3222 u32 l;
3223
3224 l = dispc_read_reg(dispc, DISPC_DIVISORo(channel));
3225
3226 pcd = FLD_GET(l, 7, 0);
3227
3228 r = dispc_mgr_lclk_rate(dispc, channel);
3229
3230 return r / pcd;
3231 } else {
3232 return dispc->tv_pclk_rate;
3233 }
3234}
3235
3236void dispc_set_tv_pclk(struct dispc_device *dispc, unsigned long pclk)
3237{
3238 dispc->tv_pclk_rate = pclk;
3239}
3240
3241static unsigned long dispc_core_clk_rate(struct dispc_device *dispc)
3242{
3243 return dispc->core_clk_rate;
3244}
3245
3246static unsigned long dispc_plane_pclk_rate(struct dispc_device *dispc,
3247 enum omap_plane_id plane)
3248{
3249 enum omap_channel channel;
3250
3251 if (plane == OMAP_DSS_WB)
3252 return 0;
3253
3254 channel = dispc_ovl_get_channel_out(dispc, plane);
3255
3256 return dispc_mgr_pclk_rate(dispc, channel);
3257}
3258
3259static unsigned long dispc_plane_lclk_rate(struct dispc_device *dispc,
3260 enum omap_plane_id plane)
3261{
3262 enum omap_channel channel;
3263
3264 if (plane == OMAP_DSS_WB)
3265 return 0;
3266
3267 channel = dispc_ovl_get_channel_out(dispc, plane);
3268
3269 return dispc_mgr_lclk_rate(dispc, channel);
3270}
3271
3272static void dispc_dump_clocks_channel(struct dispc_device *dispc,
3273 struct seq_file *s,
3274 enum omap_channel channel)
3275{
3276 int lcd, pcd;
3277 enum dss_clk_source lcd_clk_src;
3278
3279 seq_printf(s, "- %s -\n", mgr_desc[channel].name);
3280
3281 lcd_clk_src = dss_get_lcd_clk_source(dispc->dss, channel);
3282
3283 seq_printf(s, "%s clk source = %s\n", mgr_desc[channel].name,
3284 dss_get_clk_source_name(lcd_clk_src));
3285
3286 dispc_mgr_get_lcd_divisor(dispc, channel, &lcd, &pcd);
3287
3288 seq_printf(s, "lck\t\t%-16lulck div\t%u\n",
3289 dispc_mgr_lclk_rate(dispc, channel), lcd);
3290 seq_printf(s, "pck\t\t%-16lupck div\t%u\n",
3291 dispc_mgr_pclk_rate(dispc, channel), pcd);
3292}
3293
3294void dispc_dump_clocks(struct dispc_device *dispc, struct seq_file *s)
3295{
3296 enum dss_clk_source dispc_clk_src;
3297 int lcd;
3298 u32 l;
3299
3300 if (dispc_runtime_get(dispc))
3301 return;
3302
3303 seq_printf(s, "- DISPC -\n");
3304
3305 dispc_clk_src = dss_get_dispc_clk_source(dispc->dss);
3306 seq_printf(s, "dispc fclk source = %s\n",
3307 dss_get_clk_source_name(dispc_clk_src));
3308
3309 seq_printf(s, "fck\t\t%-16lu\n", dispc_fclk_rate(dispc));
3310
3311 if (dispc_has_feature(dispc, FEAT_CORE_CLK_DIV)) {
3312 seq_printf(s, "- DISPC-CORE-CLK -\n");
3313 l = dispc_read_reg(dispc, DISPC_DIVISOR);
3314 lcd = FLD_GET(l, 23, 16);
3315
3316 seq_printf(s, "lck\t\t%-16lulck div\t%u\n",
3317 (dispc_fclk_rate(dispc)/lcd), lcd);
3318 }
3319
3320 dispc_dump_clocks_channel(dispc, s, OMAP_DSS_CHANNEL_LCD);
3321
3322 if (dispc_has_feature(dispc, FEAT_MGR_LCD2))
3323 dispc_dump_clocks_channel(dispc, s, OMAP_DSS_CHANNEL_LCD2);
3324 if (dispc_has_feature(dispc, FEAT_MGR_LCD3))
3325 dispc_dump_clocks_channel(dispc, s, OMAP_DSS_CHANNEL_LCD3);
3326
3327 dispc_runtime_put(dispc);
3328}
3329
3330static int dispc_dump_regs(struct seq_file *s, void *p)
3331{
3332 struct dispc_device *dispc = s->private;
3333 int i, j;
3334 const char *mgr_names[] = {
3335 [OMAP_DSS_CHANNEL_LCD] = "LCD",
3336 [OMAP_DSS_CHANNEL_DIGIT] = "TV",
3337 [OMAP_DSS_CHANNEL_LCD2] = "LCD2",
3338 [OMAP_DSS_CHANNEL_LCD3] = "LCD3",
3339 };
3340 const char *ovl_names[] = {
3341 [OMAP_DSS_GFX] = "GFX",
3342 [OMAP_DSS_VIDEO1] = "VID1",
3343 [OMAP_DSS_VIDEO2] = "VID2",
3344 [OMAP_DSS_VIDEO3] = "VID3",
3345 [OMAP_DSS_WB] = "WB",
3346 };
3347 const char **p_names;
3348
3349#define DUMPREG(dispc, r) \
3350 seq_printf(s, "%-50s %08x\n", #r, dispc_read_reg(dispc, r))
3351
3352 if (dispc_runtime_get(dispc))
3353 return 0;
3354
3355 /* DISPC common registers */
3356 DUMPREG(dispc, DISPC_REVISION);
3357 DUMPREG(dispc, DISPC_SYSCONFIG);
3358 DUMPREG(dispc, DISPC_SYSSTATUS);
3359 DUMPREG(dispc, DISPC_IRQSTATUS);
3360 DUMPREG(dispc, DISPC_IRQENABLE);
3361 DUMPREG(dispc, DISPC_CONTROL);
3362 DUMPREG(dispc, DISPC_CONFIG);
3363 DUMPREG(dispc, DISPC_CAPABLE);
3364 DUMPREG(dispc, DISPC_LINE_STATUS);
3365 DUMPREG(dispc, DISPC_LINE_NUMBER);
3366 if (dispc_has_feature(dispc, FEAT_ALPHA_FIXED_ZORDER) ||
3367 dispc_has_feature(dispc, FEAT_ALPHA_FREE_ZORDER))
3368 DUMPREG(dispc, DISPC_GLOBAL_ALPHA);
3369 if (dispc_has_feature(dispc, FEAT_MGR_LCD2)) {
3370 DUMPREG(dispc, DISPC_CONTROL2);
3371 DUMPREG(dispc, DISPC_CONFIG2);
3372 }
3373 if (dispc_has_feature(dispc, FEAT_MGR_LCD3)) {
3374 DUMPREG(dispc, DISPC_CONTROL3);
3375 DUMPREG(dispc, DISPC_CONFIG3);
3376 }
3377 if (dispc_has_feature(dispc, FEAT_MFLAG))
3378 DUMPREG(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE);
3379
3380#undef DUMPREG
3381
3382#define DISPC_REG(i, name) name(i)
3383#define DUMPREG(dispc, i, r) seq_printf(s, "%s(%s)%*s %08x\n", #r, p_names[i], \
3384 (int)(48 - strlen(#r) - strlen(p_names[i])), " ", \
3385 dispc_read_reg(dispc, DISPC_REG(i, r)))
3386
3387 p_names = mgr_names;
3388
3389 /* DISPC channel specific registers */
3390 for (i = 0; i < dispc_get_num_mgrs(dispc); i++) {
3391 DUMPREG(dispc, i, DISPC_DEFAULT_COLOR);
3392 DUMPREG(dispc, i, DISPC_TRANS_COLOR);
3393 DUMPREG(dispc, i, DISPC_SIZE_MGR);
3394
3395 if (i == OMAP_DSS_CHANNEL_DIGIT)
3396 continue;
3397
3398 DUMPREG(dispc, i, DISPC_TIMING_H);
3399 DUMPREG(dispc, i, DISPC_TIMING_V);
3400 DUMPREG(dispc, i, DISPC_POL_FREQ);
3401 DUMPREG(dispc, i, DISPC_DIVISORo);
3402
3403 DUMPREG(dispc, i, DISPC_DATA_CYCLE1);
3404 DUMPREG(dispc, i, DISPC_DATA_CYCLE2);
3405 DUMPREG(dispc, i, DISPC_DATA_CYCLE3);
3406
3407 if (dispc_has_feature(dispc, FEAT_CPR)) {
3408 DUMPREG(dispc, i, DISPC_CPR_COEF_R);
3409 DUMPREG(dispc, i, DISPC_CPR_COEF_G);
3410 DUMPREG(dispc, i, DISPC_CPR_COEF_B);
3411 }
3412 }
3413
3414 p_names = ovl_names;
3415
3416 for (i = 0; i < dispc_get_num_ovls(dispc); i++) {
3417 DUMPREG(dispc, i, DISPC_OVL_BA0);
3418 DUMPREG(dispc, i, DISPC_OVL_BA1);
3419 DUMPREG(dispc, i, DISPC_OVL_POSITION);
3420 DUMPREG(dispc, i, DISPC_OVL_SIZE);
3421 DUMPREG(dispc, i, DISPC_OVL_ATTRIBUTES);
3422 DUMPREG(dispc, i, DISPC_OVL_FIFO_THRESHOLD);
3423 DUMPREG(dispc, i, DISPC_OVL_FIFO_SIZE_STATUS);
3424 DUMPREG(dispc, i, DISPC_OVL_ROW_INC);
3425 DUMPREG(dispc, i, DISPC_OVL_PIXEL_INC);
3426
3427 if (dispc_has_feature(dispc, FEAT_PRELOAD))
3428 DUMPREG(dispc, i, DISPC_OVL_PRELOAD);
3429 if (dispc_has_feature(dispc, FEAT_MFLAG))
3430 DUMPREG(dispc, i, DISPC_OVL_MFLAG_THRESHOLD);
3431
3432 if (i == OMAP_DSS_GFX) {
3433 DUMPREG(dispc, i, DISPC_OVL_WINDOW_SKIP);
3434 DUMPREG(dispc, i, DISPC_OVL_TABLE_BA);
3435 continue;
3436 }
3437
3438 DUMPREG(dispc, i, DISPC_OVL_FIR);
3439 DUMPREG(dispc, i, DISPC_OVL_PICTURE_SIZE);
3440 DUMPREG(dispc, i, DISPC_OVL_ACCU0);
3441 DUMPREG(dispc, i, DISPC_OVL_ACCU1);
3442 if (dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE)) {
3443 DUMPREG(dispc, i, DISPC_OVL_BA0_UV);
3444 DUMPREG(dispc, i, DISPC_OVL_BA1_UV);
3445 DUMPREG(dispc, i, DISPC_OVL_FIR2);
3446 DUMPREG(dispc, i, DISPC_OVL_ACCU2_0);
3447 DUMPREG(dispc, i, DISPC_OVL_ACCU2_1);
3448 }
3449 if (dispc_has_feature(dispc, FEAT_ATTR2))
3450 DUMPREG(dispc, i, DISPC_OVL_ATTRIBUTES2);
3451 }
3452
3453 if (dispc->feat->has_writeback) {
3454 i = OMAP_DSS_WB;
3455 DUMPREG(dispc, i, DISPC_OVL_BA0);
3456 DUMPREG(dispc, i, DISPC_OVL_BA1);
3457 DUMPREG(dispc, i, DISPC_OVL_SIZE);
3458 DUMPREG(dispc, i, DISPC_OVL_ATTRIBUTES);
3459 DUMPREG(dispc, i, DISPC_OVL_FIFO_THRESHOLD);
3460 DUMPREG(dispc, i, DISPC_OVL_FIFO_SIZE_STATUS);
3461 DUMPREG(dispc, i, DISPC_OVL_ROW_INC);
3462 DUMPREG(dispc, i, DISPC_OVL_PIXEL_INC);
3463
3464 if (dispc_has_feature(dispc, FEAT_MFLAG))
3465 DUMPREG(dispc, i, DISPC_OVL_MFLAG_THRESHOLD);
3466
3467 DUMPREG(dispc, i, DISPC_OVL_FIR);
3468 DUMPREG(dispc, i, DISPC_OVL_PICTURE_SIZE);
3469 DUMPREG(dispc, i, DISPC_OVL_ACCU0);
3470 DUMPREG(dispc, i, DISPC_OVL_ACCU1);
3471 if (dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE)) {
3472 DUMPREG(dispc, i, DISPC_OVL_BA0_UV);
3473 DUMPREG(dispc, i, DISPC_OVL_BA1_UV);
3474 DUMPREG(dispc, i, DISPC_OVL_FIR2);
3475 DUMPREG(dispc, i, DISPC_OVL_ACCU2_0);
3476 DUMPREG(dispc, i, DISPC_OVL_ACCU2_1);
3477 }
3478 if (dispc_has_feature(dispc, FEAT_ATTR2))
3479 DUMPREG(dispc, i, DISPC_OVL_ATTRIBUTES2);
3480 }
3481
3482#undef DISPC_REG
3483#undef DUMPREG
3484
3485#define DISPC_REG(plane, name, i) name(plane, i)
3486#define DUMPREG(dispc, plane, name, i) \
3487 seq_printf(s, "%s_%d(%s)%*s %08x\n", #name, i, p_names[plane], \
3488 (int)(46 - strlen(#name) - strlen(p_names[plane])), " ", \
3489 dispc_read_reg(dispc, DISPC_REG(plane, name, i)))
3490
3491 /* Video pipeline coefficient registers */
3492
3493 /* start from OMAP_DSS_VIDEO1 */
3494 for (i = 1; i < dispc_get_num_ovls(dispc); i++) {
3495 for (j = 0; j < 8; j++)
3496 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_H, j);
3497
3498 for (j = 0; j < 8; j++)
3499 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_HV, j);
3500
3501 for (j = 0; j < 5; j++)
3502 DUMPREG(dispc, i, DISPC_OVL_CONV_COEF, j);
3503
3504 if (dispc_has_feature(dispc, FEAT_FIR_COEF_V)) {
3505 for (j = 0; j < 8; j++)
3506 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_V, j);
3507 }
3508
3509 if (dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE)) {
3510 for (j = 0; j < 8; j++)
3511 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_H2, j);
3512
3513 for (j = 0; j < 8; j++)
3514 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_HV2, j);
3515
3516 for (j = 0; j < 8; j++)
3517 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_V2, j);
3518 }
3519 }
3520
3521 dispc_runtime_put(dispc);
3522
3523#undef DISPC_REG
3524#undef DUMPREG
3525
3526 return 0;
3527}
3528
3529/* calculate clock rates using dividers in cinfo */
3530int dispc_calc_clock_rates(struct dispc_device *dispc,
3531 unsigned long dispc_fclk_rate,
3532 struct dispc_clock_info *cinfo)
3533{
3534 if (cinfo->lck_div > 255 || cinfo->lck_div == 0)
3535 return -EINVAL;
3536 if (cinfo->pck_div < 1 || cinfo->pck_div > 255)
3537 return -EINVAL;
3538
3539 cinfo->lck = dispc_fclk_rate / cinfo->lck_div;
3540 cinfo->pck = cinfo->lck / cinfo->pck_div;
3541
3542 return 0;
3543}
3544
3545bool dispc_div_calc(struct dispc_device *dispc, unsigned long dispc_freq,
3546 unsigned long pck_min, unsigned long pck_max,
3547 dispc_div_calc_func func, void *data)
3548{
3549 int lckd, lckd_start, lckd_stop;
3550 int pckd, pckd_start, pckd_stop;
3551 unsigned long pck, lck;
3552 unsigned long lck_max;
3553 unsigned long pckd_hw_min, pckd_hw_max;
3554 unsigned int min_fck_per_pck;
3555 unsigned long fck;
3556
3557#ifdef CONFIG_OMAP2_DSS_MIN_FCK_PER_PCK
3558 min_fck_per_pck = CONFIG_OMAP2_DSS_MIN_FCK_PER_PCK;
3559#else
3560 min_fck_per_pck = 0;
3561#endif
3562
3563 pckd_hw_min = dispc->feat->min_pcd;
3564 pckd_hw_max = 255;
3565
3566 lck_max = dss_get_max_fck_rate(dispc->dss);
3567
3568 pck_min = pck_min ? pck_min : 1;
3569 pck_max = pck_max ? pck_max : ULONG_MAX;
3570
3571 lckd_start = max(DIV_ROUND_UP(dispc_freq, lck_max), 1ul);
3572 lckd_stop = min(dispc_freq / pck_min, 255ul);
3573
3574 for (lckd = lckd_start; lckd <= lckd_stop; ++lckd) {
3575 lck = dispc_freq / lckd;
3576
3577 pckd_start = max(DIV_ROUND_UP(lck, pck_max), pckd_hw_min);
3578 pckd_stop = min(lck / pck_min, pckd_hw_max);
3579
3580 for (pckd = pckd_start; pckd <= pckd_stop; ++pckd) {
3581 pck = lck / pckd;
3582
3583 /*
3584 * For OMAP2/3 the DISPC fclk is the same as LCD's logic
3585 * clock, which means we're configuring DISPC fclk here
3586 * also. Thus we need to use the calculated lck. For
3587 * OMAP4+ the DISPC fclk is a separate clock.
3588 */
3589 if (dispc_has_feature(dispc, FEAT_CORE_CLK_DIV))
3590 fck = dispc_core_clk_rate(dispc);
3591 else
3592 fck = lck;
3593
3594 if (fck < pck * min_fck_per_pck)
3595 continue;
3596
3597 if (func(lckd, pckd, lck, pck, data))
3598 return true;
3599 }
3600 }
3601
3602 return false;
3603}
3604
3605void dispc_mgr_set_clock_div(struct dispc_device *dispc,
3606 enum omap_channel channel,
3607 const struct dispc_clock_info *cinfo)
3608{
3609 DSSDBG("lck = %lu (%u)\n", cinfo->lck, cinfo->lck_div);
3610 DSSDBG("pck = %lu (%u)\n", cinfo->pck, cinfo->pck_div);
3611
3612 dispc_mgr_set_lcd_divisor(dispc, channel, cinfo->lck_div,
3613 cinfo->pck_div);
3614}
3615
3616u32 dispc_read_irqstatus(struct dispc_device *dispc)
3617{
3618 return dispc_read_reg(dispc, DISPC_IRQSTATUS);
3619}
3620
3621void dispc_clear_irqstatus(struct dispc_device *dispc, u32 mask)
3622{
3623 dispc_write_reg(dispc, DISPC_IRQSTATUS, mask);
3624}
3625
3626void dispc_write_irqenable(struct dispc_device *dispc, u32 mask)
3627{
3628 u32 old_mask = dispc_read_reg(dispc, DISPC_IRQENABLE);
3629
3630 /* clear the irqstatus for newly enabled irqs */
3631 dispc_clear_irqstatus(dispc, (mask ^ old_mask) & mask);
3632
3633 dispc_write_reg(dispc, DISPC_IRQENABLE, mask);
3634
3635 /* flush posted write */
3636 dispc_read_reg(dispc, DISPC_IRQENABLE);
3637}
3638
3639void dispc_enable_sidle(struct dispc_device *dispc)
3640{
3641 /* SIDLEMODE: smart idle */
3642 REG_FLD_MOD(dispc, DISPC_SYSCONFIG, 2, 4, 3);
3643}
3644
3645void dispc_disable_sidle(struct dispc_device *dispc)
3646{
3647 REG_FLD_MOD(dispc, DISPC_SYSCONFIG, 1, 4, 3); /* SIDLEMODE: no idle */
3648}
3649
3650u32 dispc_mgr_gamma_size(struct dispc_device *dispc,
3651 enum omap_channel channel)
3652{
3653 const struct dispc_gamma_desc *gdesc = &mgr_desc[channel].gamma;
3654
3655 if (!dispc->feat->has_gamma_table)
3656 return 0;
3657
3658 return gdesc->len;
3659}
3660
3661static void dispc_mgr_write_gamma_table(struct dispc_device *dispc,
3662 enum omap_channel channel)
3663{
3664 const struct dispc_gamma_desc *gdesc = &mgr_desc[channel].gamma;
3665 u32 *table = dispc->gamma_table[channel];
3666 unsigned int i;
3667
3668 DSSDBG("%s: channel %d\n", __func__, channel);
3669
3670 for (i = 0; i < gdesc->len; ++i) {
3671 u32 v = table[i];
3672
3673 if (gdesc->has_index)
3674 v |= i << 24;
3675 else if (i == 0)
3676 v |= 1 << 31;
3677
3678 dispc_write_reg(dispc, gdesc->reg, v);
3679 }
3680}
3681
3682static void dispc_restore_gamma_tables(struct dispc_device *dispc)
3683{
3684 DSSDBG("%s()\n", __func__);
3685
3686 if (!dispc->feat->has_gamma_table)
3687 return;
3688
3689 dispc_mgr_write_gamma_table(dispc, OMAP_DSS_CHANNEL_LCD);
3690
3691 dispc_mgr_write_gamma_table(dispc, OMAP_DSS_CHANNEL_DIGIT);
3692
3693 if (dispc_has_feature(dispc, FEAT_MGR_LCD2))
3694 dispc_mgr_write_gamma_table(dispc, OMAP_DSS_CHANNEL_LCD2);
3695
3696 if (dispc_has_feature(dispc, FEAT_MGR_LCD3))
3697 dispc_mgr_write_gamma_table(dispc, OMAP_DSS_CHANNEL_LCD3);
3698}
3699
3700static const struct drm_color_lut dispc_mgr_gamma_default_lut[] = {
3701 { .red = 0, .green = 0, .blue = 0, },
3702 { .red = U16_MAX, .green = U16_MAX, .blue = U16_MAX, },
3703};
3704
3705void dispc_mgr_set_gamma(struct dispc_device *dispc,
3706 enum omap_channel channel,
3707 const struct drm_color_lut *lut,
3708 unsigned int length)
3709{
3710 const struct dispc_gamma_desc *gdesc = &mgr_desc[channel].gamma;
3711 u32 *table = dispc->gamma_table[channel];
3712 uint i;
3713
3714 DSSDBG("%s: channel %d, lut len %u, hw len %u\n", __func__,
3715 channel, length, gdesc->len);
3716
3717 if (!dispc->feat->has_gamma_table)
3718 return;
3719
3720 if (lut == NULL || length < 2) {
3721 lut = dispc_mgr_gamma_default_lut;
3722 length = ARRAY_SIZE(dispc_mgr_gamma_default_lut);
3723 }
3724
3725 for (i = 0; i < length - 1; ++i) {
3726 uint first = i * (gdesc->len - 1) / (length - 1);
3727 uint last = (i + 1) * (gdesc->len - 1) / (length - 1);
3728 uint w = last - first;
3729 u16 r, g, b;
3730 uint j;
3731
3732 if (w == 0)
3733 continue;
3734
3735 for (j = 0; j <= w; j++) {
3736 r = (lut[i].red * (w - j) + lut[i+1].red * j) / w;
3737 g = (lut[i].green * (w - j) + lut[i+1].green * j) / w;
3738 b = (lut[i].blue * (w - j) + lut[i+1].blue * j) / w;
3739
3740 r >>= 16 - gdesc->bits;
3741 g >>= 16 - gdesc->bits;
3742 b >>= 16 - gdesc->bits;
3743
3744 table[first + j] = (r << (gdesc->bits * 2)) |
3745 (g << gdesc->bits) | b;
3746 }
3747 }
3748
3749 if (dispc->is_enabled)
3750 dispc_mgr_write_gamma_table(dispc, channel);
3751}
3752
3753static int dispc_init_gamma_tables(struct dispc_device *dispc)
3754{
3755 int channel;
3756
3757 if (!dispc->feat->has_gamma_table)
3758 return 0;
3759
3760 for (channel = 0; channel < ARRAY_SIZE(dispc->gamma_table); channel++) {
3761 const struct dispc_gamma_desc *gdesc = &mgr_desc[channel].gamma;
3762 u32 *gt;
3763
3764 if (channel == OMAP_DSS_CHANNEL_LCD2 &&
3765 !dispc_has_feature(dispc, FEAT_MGR_LCD2))
3766 continue;
3767
3768 if (channel == OMAP_DSS_CHANNEL_LCD3 &&
3769 !dispc_has_feature(dispc, FEAT_MGR_LCD3))
3770 continue;
3771
3772 gt = devm_kmalloc_array(&dispc->pdev->dev, gdesc->len,
3773 sizeof(u32), GFP_KERNEL);
3774 if (!gt)
3775 return -ENOMEM;
3776
3777 dispc->gamma_table[channel] = gt;
3778
3779 dispc_mgr_set_gamma(dispc, channel, NULL, 0);
3780 }
3781 return 0;
3782}
3783
3784static void _omap_dispc_initial_config(struct dispc_device *dispc)
3785{
3786 u32 l;
3787
3788 /* Exclusively enable DISPC_CORE_CLK and set divider to 1 */
3789 if (dispc_has_feature(dispc, FEAT_CORE_CLK_DIV)) {
3790 l = dispc_read_reg(dispc, DISPC_DIVISOR);
3791 /* Use DISPC_DIVISOR.LCD, instead of DISPC_DIVISOR1.LCD */
3792 l = FLD_MOD(l, 1, 0, 0);
3793 l = FLD_MOD(l, 1, 23, 16);
3794 dispc_write_reg(dispc, DISPC_DIVISOR, l);
3795
3796 dispc->core_clk_rate = dispc_fclk_rate(dispc);
3797 }
3798
3799 /* Use gamma table mode, instead of palette mode */
3800 if (dispc->feat->has_gamma_table)
3801 REG_FLD_MOD(dispc, DISPC_CONFIG, 1, 3, 3);
3802
3803 /* For older DSS versions (FEAT_FUNCGATED) this enables
3804 * func-clock auto-gating. For newer versions
3805 * (dispc->feat->has_gamma_table) this enables tv-out gamma tables.
3806 */
3807 if (dispc_has_feature(dispc, FEAT_FUNCGATED) ||
3808 dispc->feat->has_gamma_table)
3809 REG_FLD_MOD(dispc, DISPC_CONFIG, 1, 9, 9);
3810
3811 dispc_set_loadmode(dispc, OMAP_DSS_LOAD_FRAME_ONLY);
3812
3813 dispc_init_fifos(dispc);
3814
3815 dispc_configure_burst_sizes(dispc);
3816
3817 dispc_ovl_enable_zorder_planes(dispc);
3818
3819 if (dispc->feat->mstandby_workaround)
3820 REG_FLD_MOD(dispc, DISPC_MSTANDBY_CTRL, 1, 0, 0);
3821
3822 if (dispc_has_feature(dispc, FEAT_MFLAG))
3823 dispc_init_mflag(dispc);
3824}
3825
3826static const enum dispc_feature_id omap2_dispc_features_list[] = {
3827 FEAT_LCDENABLEPOL,
3828 FEAT_LCDENABLESIGNAL,
3829 FEAT_PCKFREEENABLE,
3830 FEAT_FUNCGATED,
3831 FEAT_ROWREPEATENABLE,
3832 FEAT_RESIZECONF,
3833};
3834
3835static const enum dispc_feature_id omap3_dispc_features_list[] = {
3836 FEAT_LCDENABLEPOL,
3837 FEAT_LCDENABLESIGNAL,
3838 FEAT_PCKFREEENABLE,
3839 FEAT_FUNCGATED,
3840 FEAT_LINEBUFFERSPLIT,
3841 FEAT_ROWREPEATENABLE,
3842 FEAT_RESIZECONF,
3843 FEAT_CPR,
3844 FEAT_PRELOAD,
3845 FEAT_FIR_COEF_V,
3846 FEAT_ALPHA_FIXED_ZORDER,
3847 FEAT_FIFO_MERGE,
3848 FEAT_OMAP3_DSI_FIFO_BUG,
3849};
3850
3851static const enum dispc_feature_id am43xx_dispc_features_list[] = {
3852 FEAT_LCDENABLEPOL,
3853 FEAT_LCDENABLESIGNAL,
3854 FEAT_PCKFREEENABLE,
3855 FEAT_FUNCGATED,
3856 FEAT_LINEBUFFERSPLIT,
3857 FEAT_ROWREPEATENABLE,
3858 FEAT_RESIZECONF,
3859 FEAT_CPR,
3860 FEAT_PRELOAD,
3861 FEAT_FIR_COEF_V,
3862 FEAT_ALPHA_FIXED_ZORDER,
3863 FEAT_FIFO_MERGE,
3864};
3865
3866static const enum dispc_feature_id omap4_dispc_features_list[] = {
3867 FEAT_MGR_LCD2,
3868 FEAT_CORE_CLK_DIV,
3869 FEAT_HANDLE_UV_SEPARATE,
3870 FEAT_ATTR2,
3871 FEAT_CPR,
3872 FEAT_PRELOAD,
3873 FEAT_FIR_COEF_V,
3874 FEAT_ALPHA_FREE_ZORDER,
3875 FEAT_FIFO_MERGE,
3876 FEAT_BURST_2D,
3877};
3878
3879static const enum dispc_feature_id omap5_dispc_features_list[] = {
3880 FEAT_MGR_LCD2,
3881 FEAT_MGR_LCD3,
3882 FEAT_CORE_CLK_DIV,
3883 FEAT_HANDLE_UV_SEPARATE,
3884 FEAT_ATTR2,
3885 FEAT_CPR,
3886 FEAT_PRELOAD,
3887 FEAT_FIR_COEF_V,
3888 FEAT_ALPHA_FREE_ZORDER,
3889 FEAT_FIFO_MERGE,
3890 FEAT_BURST_2D,
3891 FEAT_MFLAG,
3892};
3893
3894static const struct dss_reg_field omap2_dispc_reg_fields[] = {
3895 [FEAT_REG_FIRHINC] = { 11, 0 },
3896 [FEAT_REG_FIRVINC] = { 27, 16 },
3897 [FEAT_REG_FIFOLOWTHRESHOLD] = { 8, 0 },
3898 [FEAT_REG_FIFOHIGHTHRESHOLD] = { 24, 16 },
3899 [FEAT_REG_FIFOSIZE] = { 8, 0 },
3900 [FEAT_REG_HORIZONTALACCU] = { 9, 0 },
3901 [FEAT_REG_VERTICALACCU] = { 25, 16 },
3902};
3903
3904static const struct dss_reg_field omap3_dispc_reg_fields[] = {
3905 [FEAT_REG_FIRHINC] = { 12, 0 },
3906 [FEAT_REG_FIRVINC] = { 28, 16 },
3907 [FEAT_REG_FIFOLOWTHRESHOLD] = { 11, 0 },
3908 [FEAT_REG_FIFOHIGHTHRESHOLD] = { 27, 16 },
3909 [FEAT_REG_FIFOSIZE] = { 10, 0 },
3910 [FEAT_REG_HORIZONTALACCU] = { 9, 0 },
3911 [FEAT_REG_VERTICALACCU] = { 25, 16 },
3912};
3913
3914static const struct dss_reg_field omap4_dispc_reg_fields[] = {
3915 [FEAT_REG_FIRHINC] = { 12, 0 },
3916 [FEAT_REG_FIRVINC] = { 28, 16 },
3917 [FEAT_REG_FIFOLOWTHRESHOLD] = { 15, 0 },
3918 [FEAT_REG_FIFOHIGHTHRESHOLD] = { 31, 16 },
3919 [FEAT_REG_FIFOSIZE] = { 15, 0 },
3920 [FEAT_REG_HORIZONTALACCU] = { 10, 0 },
3921 [FEAT_REG_VERTICALACCU] = { 26, 16 },
3922};
3923
3924static const enum omap_overlay_caps omap2_dispc_overlay_caps[] = {
3925 /* OMAP_DSS_GFX */
3926 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
3927
3928 /* OMAP_DSS_VIDEO1 */
3929 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_POS |
3930 OMAP_DSS_OVL_CAP_REPLICATION,
3931
3932 /* OMAP_DSS_VIDEO2 */
3933 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_POS |
3934 OMAP_DSS_OVL_CAP_REPLICATION,
3935};
3936
3937static const enum omap_overlay_caps omap3430_dispc_overlay_caps[] = {
3938 /* OMAP_DSS_GFX */
3939 OMAP_DSS_OVL_CAP_GLOBAL_ALPHA | OMAP_DSS_OVL_CAP_POS |
3940 OMAP_DSS_OVL_CAP_REPLICATION,
3941
3942 /* OMAP_DSS_VIDEO1 */
3943 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_POS |
3944 OMAP_DSS_OVL_CAP_REPLICATION,
3945
3946 /* OMAP_DSS_VIDEO2 */
3947 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_GLOBAL_ALPHA |
3948 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
3949};
3950
3951static const enum omap_overlay_caps omap3630_dispc_overlay_caps[] = {
3952 /* OMAP_DSS_GFX */
3953 OMAP_DSS_OVL_CAP_GLOBAL_ALPHA | OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA |
3954 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
3955
3956 /* OMAP_DSS_VIDEO1 */
3957 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_POS |
3958 OMAP_DSS_OVL_CAP_REPLICATION,
3959
3960 /* OMAP_DSS_VIDEO2 */
3961 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_GLOBAL_ALPHA |
3962 OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA | OMAP_DSS_OVL_CAP_POS |
3963 OMAP_DSS_OVL_CAP_REPLICATION,
3964};
3965
3966static const enum omap_overlay_caps omap4_dispc_overlay_caps[] = {
3967 /* OMAP_DSS_GFX */
3968 OMAP_DSS_OVL_CAP_GLOBAL_ALPHA | OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA |
3969 OMAP_DSS_OVL_CAP_ZORDER | OMAP_DSS_OVL_CAP_POS |
3970 OMAP_DSS_OVL_CAP_REPLICATION,
3971
3972 /* OMAP_DSS_VIDEO1 */
3973 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_GLOBAL_ALPHA |
3974 OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA | OMAP_DSS_OVL_CAP_ZORDER |
3975 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
3976
3977 /* OMAP_DSS_VIDEO2 */
3978 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_GLOBAL_ALPHA |
3979 OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA | OMAP_DSS_OVL_CAP_ZORDER |
3980 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
3981
3982 /* OMAP_DSS_VIDEO3 */
3983 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_GLOBAL_ALPHA |
3984 OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA | OMAP_DSS_OVL_CAP_ZORDER |
3985 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
3986};
3987
3988#define COLOR_ARRAY(arr...) (const u32[]) { arr, 0 }
3989
3990static const u32 *omap2_dispc_supported_color_modes[] = {
3991
3992 /* OMAP_DSS_GFX */
3993 COLOR_ARRAY(
3994 DRM_FORMAT_RGBX4444, DRM_FORMAT_RGB565,
3995 DRM_FORMAT_XRGB8888, DRM_FORMAT_RGB888),
3996
3997 /* OMAP_DSS_VIDEO1 */
3998 COLOR_ARRAY(
3999 DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888,
4000 DRM_FORMAT_RGB888, DRM_FORMAT_YUYV,
4001 DRM_FORMAT_UYVY),
4002
4003 /* OMAP_DSS_VIDEO2 */
4004 COLOR_ARRAY(
4005 DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888,
4006 DRM_FORMAT_RGB888, DRM_FORMAT_YUYV,
4007 DRM_FORMAT_UYVY),
4008};
4009
4010static const u32 *omap3_dispc_supported_color_modes[] = {
4011 /* OMAP_DSS_GFX */
4012 COLOR_ARRAY(
4013 DRM_FORMAT_RGBX4444, DRM_FORMAT_ARGB4444,
4014 DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888,
4015 DRM_FORMAT_RGB888, DRM_FORMAT_ARGB8888,
4016 DRM_FORMAT_RGBA8888, DRM_FORMAT_RGBX8888),
4017
4018 /* OMAP_DSS_VIDEO1 */
4019 COLOR_ARRAY(
4020 DRM_FORMAT_XRGB8888, DRM_FORMAT_RGB888,
4021 DRM_FORMAT_RGBX4444, DRM_FORMAT_RGB565,
4022 DRM_FORMAT_YUYV, DRM_FORMAT_UYVY),
4023
4024 /* OMAP_DSS_VIDEO2 */
4025 COLOR_ARRAY(
4026 DRM_FORMAT_RGBX4444, DRM_FORMAT_ARGB4444,
4027 DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888,
4028 DRM_FORMAT_RGB888, DRM_FORMAT_YUYV,
4029 DRM_FORMAT_UYVY, DRM_FORMAT_ARGB8888,
4030 DRM_FORMAT_RGBA8888, DRM_FORMAT_RGBX8888),
4031};
4032
4033static const u32 *omap4_dispc_supported_color_modes[] = {
4034 /* OMAP_DSS_GFX */
4035 COLOR_ARRAY(
4036 DRM_FORMAT_RGBX4444, DRM_FORMAT_ARGB4444,
4037 DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888,
4038 DRM_FORMAT_RGB888, DRM_FORMAT_ARGB8888,
4039 DRM_FORMAT_RGBA8888, DRM_FORMAT_RGBX8888,
4040 DRM_FORMAT_ARGB1555, DRM_FORMAT_XRGB4444,
4041 DRM_FORMAT_RGBA4444, DRM_FORMAT_XRGB1555),
4042
4043 /* OMAP_DSS_VIDEO1 */
4044 COLOR_ARRAY(
4045 DRM_FORMAT_RGB565, DRM_FORMAT_RGBX4444,
4046 DRM_FORMAT_YUYV, DRM_FORMAT_ARGB1555,
4047 DRM_FORMAT_RGBA8888, DRM_FORMAT_NV12,
4048 DRM_FORMAT_RGBA4444, DRM_FORMAT_XRGB8888,
4049 DRM_FORMAT_RGB888, DRM_FORMAT_UYVY,
4050 DRM_FORMAT_ARGB4444, DRM_FORMAT_XRGB1555,
4051 DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB4444,
4052 DRM_FORMAT_RGBX8888),
4053
4054 /* OMAP_DSS_VIDEO2 */
4055 COLOR_ARRAY(
4056 DRM_FORMAT_RGB565, DRM_FORMAT_RGBX4444,
4057 DRM_FORMAT_YUYV, DRM_FORMAT_ARGB1555,
4058 DRM_FORMAT_RGBA8888, DRM_FORMAT_NV12,
4059 DRM_FORMAT_RGBA4444, DRM_FORMAT_XRGB8888,
4060 DRM_FORMAT_RGB888, DRM_FORMAT_UYVY,
4061 DRM_FORMAT_ARGB4444, DRM_FORMAT_XRGB1555,
4062 DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB4444,
4063 DRM_FORMAT_RGBX8888),
4064
4065 /* OMAP_DSS_VIDEO3 */
4066 COLOR_ARRAY(
4067 DRM_FORMAT_RGB565, DRM_FORMAT_RGBX4444,
4068 DRM_FORMAT_YUYV, DRM_FORMAT_ARGB1555,
4069 DRM_FORMAT_RGBA8888, DRM_FORMAT_NV12,
4070 DRM_FORMAT_RGBA4444, DRM_FORMAT_XRGB8888,
4071 DRM_FORMAT_RGB888, DRM_FORMAT_UYVY,
4072 DRM_FORMAT_ARGB4444, DRM_FORMAT_XRGB1555,
4073 DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB4444,
4074 DRM_FORMAT_RGBX8888),
4075
4076 /* OMAP_DSS_WB */
4077 COLOR_ARRAY(
4078 DRM_FORMAT_RGB565, DRM_FORMAT_RGBX4444,
4079 DRM_FORMAT_YUYV, DRM_FORMAT_ARGB1555,
4080 DRM_FORMAT_RGBA8888, DRM_FORMAT_NV12,
4081 DRM_FORMAT_RGBA4444, DRM_FORMAT_XRGB8888,
4082 DRM_FORMAT_RGB888, DRM_FORMAT_UYVY,
4083 DRM_FORMAT_ARGB4444, DRM_FORMAT_XRGB1555,
4084 DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB4444,
4085 DRM_FORMAT_RGBX8888),
4086};
4087
4088static const u32 omap3_dispc_supported_scaler_color_modes[] = {
4089 DRM_FORMAT_XRGB8888, DRM_FORMAT_RGB565, DRM_FORMAT_YUYV,
4090 DRM_FORMAT_UYVY,
4091 0,
4092};
4093
4094static const struct dispc_features omap24xx_dispc_feats = {
4095 .sw_start = 5,
4096 .fp_start = 15,
4097 .bp_start = 27,
4098 .sw_max = 64,
4099 .vp_max = 255,
4100 .hp_max = 256,
4101 .mgr_width_start = 10,
4102 .mgr_height_start = 26,
4103 .mgr_width_max = 2048,
4104 .mgr_height_max = 2048,
4105 .ovl_width_max = 2048,
4106 .ovl_height_max = 2048,
4107 .max_lcd_pclk = 66500000,
4108 .max_downscale = 2,
4109 /*
4110 * Assume the line width buffer to be 768 pixels as OMAP2 DISPC scaler
4111 * cannot scale an image width larger than 768.
4112 */
4113 .max_line_width = 768,
4114 .min_pcd = 2,
4115 .calc_scaling = dispc_ovl_calc_scaling_24xx,
4116 .calc_core_clk = calc_core_clk_24xx,
4117 .num_fifos = 3,
4118 .features = omap2_dispc_features_list,
4119 .num_features = ARRAY_SIZE(omap2_dispc_features_list),
4120 .reg_fields = omap2_dispc_reg_fields,
4121 .num_reg_fields = ARRAY_SIZE(omap2_dispc_reg_fields),
4122 .overlay_caps = omap2_dispc_overlay_caps,
4123 .supported_color_modes = omap2_dispc_supported_color_modes,
4124 .supported_scaler_color_modes = COLOR_ARRAY(DRM_FORMAT_XRGB8888),
4125 .num_mgrs = 2,
4126 .num_ovls = 3,
4127 .buffer_size_unit = 1,
4128 .burst_size_unit = 8,
4129 .no_framedone_tv = true,
4130 .set_max_preload = false,
4131 .last_pixel_inc_missing = true,
4132};
4133
4134static const struct dispc_features omap34xx_rev1_0_dispc_feats = {
4135 .sw_start = 5,
4136 .fp_start = 15,
4137 .bp_start = 27,
4138 .sw_max = 64,
4139 .vp_max = 255,
4140 .hp_max = 256,
4141 .mgr_width_start = 10,
4142 .mgr_height_start = 26,
4143 .mgr_width_max = 2048,
4144 .mgr_height_max = 2048,
4145 .ovl_width_max = 2048,
4146 .ovl_height_max = 2048,
4147 .max_lcd_pclk = 173000000,
4148 .max_tv_pclk = 59000000,
4149 .max_downscale = 4,
4150 .max_line_width = 1024,
4151 .min_pcd = 1,
4152 .calc_scaling = dispc_ovl_calc_scaling_34xx,
4153 .calc_core_clk = calc_core_clk_34xx,
4154 .num_fifos = 3,
4155 .features = omap3_dispc_features_list,
4156 .num_features = ARRAY_SIZE(omap3_dispc_features_list),
4157 .reg_fields = omap3_dispc_reg_fields,
4158 .num_reg_fields = ARRAY_SIZE(omap3_dispc_reg_fields),
4159 .overlay_caps = omap3430_dispc_overlay_caps,
4160 .supported_color_modes = omap3_dispc_supported_color_modes,
4161 .supported_scaler_color_modes = omap3_dispc_supported_scaler_color_modes,
4162 .num_mgrs = 2,
4163 .num_ovls = 3,
4164 .buffer_size_unit = 1,
4165 .burst_size_unit = 8,
4166 .no_framedone_tv = true,
4167 .set_max_preload = false,
4168 .last_pixel_inc_missing = true,
4169};
4170
4171static const struct dispc_features omap34xx_rev3_0_dispc_feats = {
4172 .sw_start = 7,
4173 .fp_start = 19,
4174 .bp_start = 31,
4175 .sw_max = 256,
4176 .vp_max = 4095,
4177 .hp_max = 4096,
4178 .mgr_width_start = 10,
4179 .mgr_height_start = 26,
4180 .mgr_width_max = 2048,
4181 .mgr_height_max = 2048,
4182 .ovl_width_max = 2048,
4183 .ovl_height_max = 2048,
4184 .max_lcd_pclk = 173000000,
4185 .max_tv_pclk = 59000000,
4186 .max_downscale = 4,
4187 .max_line_width = 1024,
4188 .min_pcd = 1,
4189 .calc_scaling = dispc_ovl_calc_scaling_34xx,
4190 .calc_core_clk = calc_core_clk_34xx,
4191 .num_fifos = 3,
4192 .features = omap3_dispc_features_list,
4193 .num_features = ARRAY_SIZE(omap3_dispc_features_list),
4194 .reg_fields = omap3_dispc_reg_fields,
4195 .num_reg_fields = ARRAY_SIZE(omap3_dispc_reg_fields),
4196 .overlay_caps = omap3430_dispc_overlay_caps,
4197 .supported_color_modes = omap3_dispc_supported_color_modes,
4198 .supported_scaler_color_modes = omap3_dispc_supported_scaler_color_modes,
4199 .num_mgrs = 2,
4200 .num_ovls = 3,
4201 .buffer_size_unit = 1,
4202 .burst_size_unit = 8,
4203 .no_framedone_tv = true,
4204 .set_max_preload = false,
4205 .last_pixel_inc_missing = true,
4206};
4207
4208static const struct dispc_features omap36xx_dispc_feats = {
4209 .sw_start = 7,
4210 .fp_start = 19,
4211 .bp_start = 31,
4212 .sw_max = 256,
4213 .vp_max = 4095,
4214 .hp_max = 4096,
4215 .mgr_width_start = 10,
4216 .mgr_height_start = 26,
4217 .mgr_width_max = 2048,
4218 .mgr_height_max = 2048,
4219 .ovl_width_max = 2048,
4220 .ovl_height_max = 2048,
4221 .max_lcd_pclk = 173000000,
4222 .max_tv_pclk = 59000000,
4223 .max_downscale = 4,
4224 .max_line_width = 1024,
4225 .min_pcd = 1,
4226 .calc_scaling = dispc_ovl_calc_scaling_34xx,
4227 .calc_core_clk = calc_core_clk_34xx,
4228 .num_fifos = 3,
4229 .features = omap3_dispc_features_list,
4230 .num_features = ARRAY_SIZE(omap3_dispc_features_list),
4231 .reg_fields = omap3_dispc_reg_fields,
4232 .num_reg_fields = ARRAY_SIZE(omap3_dispc_reg_fields),
4233 .overlay_caps = omap3630_dispc_overlay_caps,
4234 .supported_color_modes = omap3_dispc_supported_color_modes,
4235 .supported_scaler_color_modes = omap3_dispc_supported_scaler_color_modes,
4236 .num_mgrs = 2,
4237 .num_ovls = 3,
4238 .buffer_size_unit = 1,
4239 .burst_size_unit = 8,
4240 .no_framedone_tv = true,
4241 .set_max_preload = false,
4242 .last_pixel_inc_missing = true,
4243};
4244
4245static const struct dispc_features am43xx_dispc_feats = {
4246 .sw_start = 7,
4247 .fp_start = 19,
4248 .bp_start = 31,
4249 .sw_max = 256,
4250 .vp_max = 4095,
4251 .hp_max = 4096,
4252 .mgr_width_start = 10,
4253 .mgr_height_start = 26,
4254 .mgr_width_max = 2048,
4255 .mgr_height_max = 2048,
4256 .ovl_width_max = 2048,
4257 .ovl_height_max = 2048,
4258 .max_lcd_pclk = 173000000,
4259 .max_tv_pclk = 59000000,
4260 .max_downscale = 4,
4261 .max_line_width = 1024,
4262 .min_pcd = 1,
4263 .calc_scaling = dispc_ovl_calc_scaling_34xx,
4264 .calc_core_clk = calc_core_clk_34xx,
4265 .num_fifos = 3,
4266 .features = am43xx_dispc_features_list,
4267 .num_features = ARRAY_SIZE(am43xx_dispc_features_list),
4268 .reg_fields = omap3_dispc_reg_fields,
4269 .num_reg_fields = ARRAY_SIZE(omap3_dispc_reg_fields),
4270 .overlay_caps = omap3430_dispc_overlay_caps,
4271 .supported_color_modes = omap3_dispc_supported_color_modes,
4272 .supported_scaler_color_modes = omap3_dispc_supported_scaler_color_modes,
4273 .num_mgrs = 1,
4274 .num_ovls = 3,
4275 .buffer_size_unit = 1,
4276 .burst_size_unit = 8,
4277 .no_framedone_tv = true,
4278 .set_max_preload = false,
4279 .last_pixel_inc_missing = true,
4280};
4281
4282static const struct dispc_features omap44xx_dispc_feats = {
4283 .sw_start = 7,
4284 .fp_start = 19,
4285 .bp_start = 31,
4286 .sw_max = 256,
4287 .vp_max = 4095,
4288 .hp_max = 4096,
4289 .mgr_width_start = 10,
4290 .mgr_height_start = 26,
4291 .mgr_width_max = 2048,
4292 .mgr_height_max = 2048,
4293 .ovl_width_max = 2048,
4294 .ovl_height_max = 2048,
4295 .max_lcd_pclk = 170000000,
4296 .max_tv_pclk = 185625000,
4297 .max_downscale = 4,
4298 .max_line_width = 2048,
4299 .min_pcd = 1,
4300 .calc_scaling = dispc_ovl_calc_scaling_44xx,
4301 .calc_core_clk = calc_core_clk_44xx,
4302 .num_fifos = 5,
4303 .features = omap4_dispc_features_list,
4304 .num_features = ARRAY_SIZE(omap4_dispc_features_list),
4305 .reg_fields = omap4_dispc_reg_fields,
4306 .num_reg_fields = ARRAY_SIZE(omap4_dispc_reg_fields),
4307 .overlay_caps = omap4_dispc_overlay_caps,
4308 .supported_color_modes = omap4_dispc_supported_color_modes,
4309 .num_mgrs = 3,
4310 .num_ovls = 4,
4311 .buffer_size_unit = 16,
4312 .burst_size_unit = 16,
4313 .gfx_fifo_workaround = true,
4314 .set_max_preload = true,
4315 .supports_sync_align = true,
4316 .has_writeback = true,
4317 .supports_double_pixel = true,
4318 .reverse_ilace_field_order = true,
4319 .has_gamma_table = true,
4320 .has_gamma_i734_bug = true,
4321};
4322
4323static const struct dispc_features omap54xx_dispc_feats = {
4324 .sw_start = 7,
4325 .fp_start = 19,
4326 .bp_start = 31,
4327 .sw_max = 256,
4328 .vp_max = 4095,
4329 .hp_max = 4096,
4330 .mgr_width_start = 11,
4331 .mgr_height_start = 27,
4332 .mgr_width_max = 4096,
4333 .mgr_height_max = 4096,
4334 .ovl_width_max = 2048,
4335 .ovl_height_max = 4096,
4336 .max_lcd_pclk = 170000000,
4337 .max_tv_pclk = 192000000,
4338 .max_downscale = 4,
4339 .max_line_width = 2048,
4340 .min_pcd = 1,
4341 .calc_scaling = dispc_ovl_calc_scaling_44xx,
4342 .calc_core_clk = calc_core_clk_44xx,
4343 .num_fifos = 5,
4344 .features = omap5_dispc_features_list,
4345 .num_features = ARRAY_SIZE(omap5_dispc_features_list),
4346 .reg_fields = omap4_dispc_reg_fields,
4347 .num_reg_fields = ARRAY_SIZE(omap4_dispc_reg_fields),
4348 .overlay_caps = omap4_dispc_overlay_caps,
4349 .supported_color_modes = omap4_dispc_supported_color_modes,
4350 .num_mgrs = 4,
4351 .num_ovls = 4,
4352 .buffer_size_unit = 16,
4353 .burst_size_unit = 16,
4354 .gfx_fifo_workaround = true,
4355 .mstandby_workaround = true,
4356 .set_max_preload = true,
4357 .supports_sync_align = true,
4358 .has_writeback = true,
4359 .supports_double_pixel = true,
4360 .reverse_ilace_field_order = true,
4361 .has_gamma_table = true,
4362 .has_gamma_i734_bug = true,
4363};
4364
4365static irqreturn_t dispc_irq_handler(int irq, void *arg)
4366{
4367 struct dispc_device *dispc = arg;
4368
4369 if (!dispc->is_enabled)
4370 return IRQ_NONE;
4371
4372 return dispc->user_handler(irq, dispc->user_data);
4373}
4374
4375int dispc_request_irq(struct dispc_device *dispc, irq_handler_t handler,
4376 void *dev_id)
4377{
4378 int r;
4379
4380 if (dispc->user_handler != NULL)
4381 return -EBUSY;
4382
4383 dispc->user_handler = handler;
4384 dispc->user_data = dev_id;
4385
4386 /* ensure the dispc_irq_handler sees the values above */
4387 smp_wmb();
4388
4389 r = devm_request_irq(&dispc->pdev->dev, dispc->irq, dispc_irq_handler,
4390 IRQF_SHARED, "OMAP DISPC", dispc);
4391 if (r) {
4392 dispc->user_handler = NULL;
4393 dispc->user_data = NULL;
4394 }
4395
4396 return r;
4397}
4398
4399void dispc_free_irq(struct dispc_device *dispc, void *dev_id)
4400{
4401 devm_free_irq(&dispc->pdev->dev, dispc->irq, dispc);
4402
4403 dispc->user_handler = NULL;
4404 dispc->user_data = NULL;
4405}
4406
4407u32 dispc_get_memory_bandwidth_limit(struct dispc_device *dispc)
4408{
4409 u32 limit = 0;
4410
4411 /* Optional maximum memory bandwidth */
4412 of_property_read_u32(dispc->pdev->dev.of_node, "max-memory-bandwidth",
4413 &limit);
4414
4415 return limit;
4416}
4417
4418/*
4419 * Workaround for errata i734 in DSS dispc
4420 * - LCD1 Gamma Correction Is Not Working When GFX Pipe Is Disabled
4421 *
4422 * For gamma tables to work on LCD1 the GFX plane has to be used at
4423 * least once after DSS HW has come out of reset. The workaround
4424 * sets up a minimal LCD setup with GFX plane and waits for one
4425 * vertical sync irq before disabling the setup and continuing with
4426 * the context restore. The physical outputs are gated during the
4427 * operation. This workaround requires that gamma table's LOADMODE
4428 * is set to 0x2 in DISPC_CONTROL1 register.
4429 *
4430 * For details see:
4431 * OMAP543x Multimedia Device Silicon Revision 2.0 Silicon Errata
4432 * Literature Number: SWPZ037E
4433 * Or some other relevant errata document for the DSS IP version.
4434 */
4435
4436static const struct dispc_errata_i734_data {
4437 struct videomode vm;
4438 struct omap_overlay_info ovli;
4439 struct omap_overlay_manager_info mgri;
4440 struct dss_lcd_mgr_config lcd_conf;
4441} i734 = {
4442 .vm = {
4443 .hactive = 8, .vactive = 1,
4444 .pixelclock = 16000000,
4445 .hsync_len = 8, .hfront_porch = 4, .hback_porch = 4,
4446 .vsync_len = 1, .vfront_porch = 1, .vback_porch = 1,
4447
4448 .flags = DISPLAY_FLAGS_HSYNC_LOW | DISPLAY_FLAGS_VSYNC_LOW |
4449 DISPLAY_FLAGS_DE_HIGH | DISPLAY_FLAGS_SYNC_POSEDGE |
4450 DISPLAY_FLAGS_PIXDATA_POSEDGE,
4451 },
4452 .ovli = {
4453 .screen_width = 1,
4454 .width = 1, .height = 1,
4455 .fourcc = DRM_FORMAT_XRGB8888,
4456 .rotation = DRM_MODE_ROTATE_0,
4457 .rotation_type = OMAP_DSS_ROT_NONE,
4458 .pos_x = 0, .pos_y = 0,
4459 .out_width = 0, .out_height = 0,
4460 .global_alpha = 0xff,
4461 .pre_mult_alpha = 0,
4462 .zorder = 0,
4463 },
4464 .mgri = {
4465 .default_color = 0,
4466 .trans_enabled = false,
4467 .partial_alpha_enabled = false,
4468 .cpr_enable = false,
4469 },
4470 .lcd_conf = {
4471 .io_pad_mode = DSS_IO_PAD_MODE_BYPASS,
4472 .stallmode = false,
4473 .fifohandcheck = false,
4474 .clock_info = {
4475 .lck_div = 1,
4476 .pck_div = 2,
4477 },
4478 .video_port_width = 24,
4479 .lcden_sig_polarity = 0,
4480 },
4481};
4482
4483static struct i734_buf {
4484 size_t size;
4485 dma_addr_t paddr;
4486 void *vaddr;
4487} i734_buf;
4488
4489static int dispc_errata_i734_wa_init(struct dispc_device *dispc)
4490{
4491 if (!dispc->feat->has_gamma_i734_bug)
4492 return 0;
4493
4494 i734_buf.size = i734.ovli.width * i734.ovli.height *
4495 color_mode_to_bpp(i734.ovli.fourcc) / 8;
4496
4497 i734_buf.vaddr = dma_alloc_wc(&dispc->pdev->dev, i734_buf.size,
4498 &i734_buf.paddr, GFP_KERNEL);
4499 if (!i734_buf.vaddr) {
4500 dev_err(&dispc->pdev->dev, "%s: dma_alloc_wc failed\n",
4501 __func__);
4502 return -ENOMEM;
4503 }
4504
4505 return 0;
4506}
4507
4508static void dispc_errata_i734_wa_fini(struct dispc_device *dispc)
4509{
4510 if (!dispc->feat->has_gamma_i734_bug)
4511 return;
4512
4513 dma_free_wc(&dispc->pdev->dev, i734_buf.size, i734_buf.vaddr,
4514 i734_buf.paddr);
4515}
4516
4517static void dispc_errata_i734_wa(struct dispc_device *dispc)
4518{
4519 u32 framedone_irq = dispc_mgr_get_framedone_irq(dispc,
4520 OMAP_DSS_CHANNEL_LCD);
4521 struct omap_overlay_info ovli;
4522 struct dss_lcd_mgr_config lcd_conf;
4523 u32 gatestate;
4524 unsigned int count;
4525
4526 if (!dispc->feat->has_gamma_i734_bug)
4527 return;
4528
4529 gatestate = REG_GET(dispc, DISPC_CONFIG, 8, 4);
4530
4531 ovli = i734.ovli;
4532 ovli.paddr = i734_buf.paddr;
4533 lcd_conf = i734.lcd_conf;
4534
4535 /* Gate all LCD1 outputs */
4536 REG_FLD_MOD(dispc, DISPC_CONFIG, 0x1f, 8, 4);
4537
4538 /* Setup and enable GFX plane */
4539 dispc_ovl_setup(dispc, OMAP_DSS_GFX, &ovli, &i734.vm, false,
4540 OMAP_DSS_CHANNEL_LCD);
4541 dispc_ovl_enable(dispc, OMAP_DSS_GFX, true);
4542
4543 /* Set up and enable display manager for LCD1 */
4544 dispc_mgr_setup(dispc, OMAP_DSS_CHANNEL_LCD, &i734.mgri);
4545 dispc_calc_clock_rates(dispc, dss_get_dispc_clk_rate(dispc->dss),
4546 &lcd_conf.clock_info);
4547 dispc_mgr_set_lcd_config(dispc, OMAP_DSS_CHANNEL_LCD, &lcd_conf);
4548 dispc_mgr_set_timings(dispc, OMAP_DSS_CHANNEL_LCD, &i734.vm);
4549
4550 dispc_clear_irqstatus(dispc, framedone_irq);
4551
4552 /* Enable and shut the channel to produce just one frame */
4553 dispc_mgr_enable(dispc, OMAP_DSS_CHANNEL_LCD, true);
4554 dispc_mgr_enable(dispc, OMAP_DSS_CHANNEL_LCD, false);
4555
4556 /* Busy wait for framedone. We can't fiddle with irq handlers
4557 * in PM resume. Typically the loop runs less than 5 times and
4558 * waits less than a micro second.
4559 */
4560 count = 0;
4561 while (!(dispc_read_irqstatus(dispc) & framedone_irq)) {
4562 if (count++ > 10000) {
4563 dev_err(&dispc->pdev->dev, "%s: framedone timeout\n",
4564 __func__);
4565 break;
4566 }
4567 }
4568 dispc_ovl_enable(dispc, OMAP_DSS_GFX, false);
4569
4570 /* Clear all irq bits before continuing */
4571 dispc_clear_irqstatus(dispc, 0xffffffff);
4572
4573 /* Restore the original state to LCD1 output gates */
4574 REG_FLD_MOD(dispc, DISPC_CONFIG, gatestate, 8, 4);
4575}
4576
4577/* DISPC HW IP initialisation */
4578static const struct of_device_id dispc_of_match[] = {
4579 { .compatible = "ti,omap2-dispc", .data = &omap24xx_dispc_feats },
4580 { .compatible = "ti,omap3-dispc", .data = &omap36xx_dispc_feats },
4581 { .compatible = "ti,omap4-dispc", .data = &omap44xx_dispc_feats },
4582 { .compatible = "ti,omap5-dispc", .data = &omap54xx_dispc_feats },
4583 { .compatible = "ti,dra7-dispc", .data = &omap54xx_dispc_feats },
4584 {},
4585};
4586
4587static const struct soc_device_attribute dispc_soc_devices[] = {
4588 { .machine = "OMAP3[45]*",
4589 .revision = "ES[12].?", .data = &omap34xx_rev1_0_dispc_feats },
4590 { .machine = "OMAP3[45]*", .data = &omap34xx_rev3_0_dispc_feats },
4591 { .machine = "AM35*", .data = &omap34xx_rev3_0_dispc_feats },
4592 { .machine = "AM43*", .data = &am43xx_dispc_feats },
4593 { /* sentinel */ }
4594};
4595
4596static int dispc_bind(struct device *dev, struct device *master, void *data)
4597{
4598 struct platform_device *pdev = to_platform_device(dev);
4599 const struct soc_device_attribute *soc;
4600 struct dss_device *dss = dss_get_device(master);
4601 struct dispc_device *dispc;
4602 u32 rev;
4603 int r = 0;
4604 struct device_node *np = pdev->dev.of_node;
4605
4606 dispc = kzalloc(sizeof(*dispc), GFP_KERNEL);
4607 if (!dispc)
4608 return -ENOMEM;
4609
4610 dispc->pdev = pdev;
4611 platform_set_drvdata(pdev, dispc);
4612 dispc->dss = dss;
4613
4614 /*
4615 * The OMAP3-based models can't be told apart using the compatible
4616 * string, use SoC device matching.
4617 */
4618 soc = soc_device_match(dispc_soc_devices);
4619 if (soc)
4620 dispc->feat = soc->data;
4621 else
4622 dispc->feat = device_get_match_data(&pdev->dev);
4623
4624 r = dispc_errata_i734_wa_init(dispc);
4625 if (r)
4626 goto err_free;
4627
4628 dispc->base = devm_platform_ioremap_resource(pdev, 0);
4629 if (IS_ERR(dispc->base)) {
4630 r = PTR_ERR(dispc->base);
4631 goto err_free;
4632 }
4633
4634 dispc->irq = platform_get_irq(dispc->pdev, 0);
4635 if (dispc->irq < 0) {
4636 DSSERR("platform_get_irq failed\n");
4637 r = -ENODEV;
4638 goto err_free;
4639 }
4640
4641 if (np && of_property_read_bool(np, "syscon-pol")) {
4642 dispc->syscon_pol = syscon_regmap_lookup_by_phandle(np, "syscon-pol");
4643 if (IS_ERR(dispc->syscon_pol)) {
4644 dev_err(&pdev->dev, "failed to get syscon-pol regmap\n");
4645 r = PTR_ERR(dispc->syscon_pol);
4646 goto err_free;
4647 }
4648
4649 if (of_property_read_u32_index(np, "syscon-pol", 1,
4650 &dispc->syscon_pol_offset)) {
4651 dev_err(&pdev->dev, "failed to get syscon-pol offset\n");
4652 r = -EINVAL;
4653 goto err_free;
4654 }
4655 }
4656
4657 r = dispc_init_gamma_tables(dispc);
4658 if (r)
4659 goto err_free;
4660
4661 pm_runtime_enable(&pdev->dev);
4662
4663 r = dispc_runtime_get(dispc);
4664 if (r)
4665 goto err_runtime_get;
4666
4667 _omap_dispc_initial_config(dispc);
4668
4669 rev = dispc_read_reg(dispc, DISPC_REVISION);
4670 dev_dbg(&pdev->dev, "OMAP DISPC rev %d.%d\n",
4671 FLD_GET(rev, 7, 4), FLD_GET(rev, 3, 0));
4672
4673 dispc_runtime_put(dispc);
4674
4675 dss->dispc = dispc;
4676
4677 dispc->debugfs = dss_debugfs_create_file(dss, "dispc", dispc_dump_regs,
4678 dispc);
4679
4680 return 0;
4681
4682err_runtime_get:
4683 pm_runtime_disable(&pdev->dev);
4684err_free:
4685 kfree(dispc);
4686 return r;
4687}
4688
4689static void dispc_unbind(struct device *dev, struct device *master, void *data)
4690{
4691 struct dispc_device *dispc = dev_get_drvdata(dev);
4692 struct dss_device *dss = dispc->dss;
4693
4694 dss_debugfs_remove_file(dispc->debugfs);
4695
4696 dss->dispc = NULL;
4697
4698 pm_runtime_disable(dev);
4699
4700 dispc_errata_i734_wa_fini(dispc);
4701
4702 kfree(dispc);
4703}
4704
4705static const struct component_ops dispc_component_ops = {
4706 .bind = dispc_bind,
4707 .unbind = dispc_unbind,
4708};
4709
4710static int dispc_probe(struct platform_device *pdev)
4711{
4712 return component_add(&pdev->dev, &dispc_component_ops);
4713}
4714
4715static void dispc_remove(struct platform_device *pdev)
4716{
4717 component_del(&pdev->dev, &dispc_component_ops);
4718}
4719
4720static __maybe_unused int dispc_runtime_suspend(struct device *dev)
4721{
4722 struct dispc_device *dispc = dev_get_drvdata(dev);
4723
4724 dispc->is_enabled = false;
4725 /* ensure the dispc_irq_handler sees the is_enabled value */
4726 smp_wmb();
4727 /* wait for current handler to finish before turning the DISPC off */
4728 synchronize_irq(dispc->irq);
4729
4730 dispc_save_context(dispc);
4731
4732 return 0;
4733}
4734
4735static __maybe_unused int dispc_runtime_resume(struct device *dev)
4736{
4737 struct dispc_device *dispc = dev_get_drvdata(dev);
4738
4739 /*
4740 * The reset value for load mode is 0 (OMAP_DSS_LOAD_CLUT_AND_FRAME)
4741 * but we always initialize it to 2 (OMAP_DSS_LOAD_FRAME_ONLY) in
4742 * _omap_dispc_initial_config(). We can thus use it to detect if
4743 * we have lost register context.
4744 */
4745 if (REG_GET(dispc, DISPC_CONFIG, 2, 1) != OMAP_DSS_LOAD_FRAME_ONLY) {
4746 _omap_dispc_initial_config(dispc);
4747
4748 dispc_errata_i734_wa(dispc);
4749
4750 dispc_restore_context(dispc);
4751
4752 dispc_restore_gamma_tables(dispc);
4753 }
4754
4755 dispc->is_enabled = true;
4756 /* ensure the dispc_irq_handler sees the is_enabled value */
4757 smp_wmb();
4758
4759 return 0;
4760}
4761
4762static const struct dev_pm_ops dispc_pm_ops = {
4763 SET_RUNTIME_PM_OPS(dispc_runtime_suspend, dispc_runtime_resume, NULL)
4764 SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
4765};
4766
4767struct platform_driver omap_dispchw_driver = {
4768 .probe = dispc_probe,
4769 .remove = dispc_remove,
4770 .driver = {
4771 .name = "omapdss_dispc",
4772 .pm = &dispc_pm_ops,
4773 .of_match_table = dispc_of_match,
4774 .suppress_bind_attrs = true,
4775 },
4776};
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2009 Nokia Corporation
4 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
5 *
6 * Some code and ideas taken from drivers/video/omap/ driver
7 * by Imre Deak.
8 */
9
10#define DSS_SUBSYS_NAME "DISPC"
11
12#include <linux/kernel.h>
13#include <linux/dma-mapping.h>
14#include <linux/vmalloc.h>
15#include <linux/export.h>
16#include <linux/clk.h>
17#include <linux/io.h>
18#include <linux/jiffies.h>
19#include <linux/seq_file.h>
20#include <linux/delay.h>
21#include <linux/workqueue.h>
22#include <linux/hardirq.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/sizes.h>
26#include <linux/mfd/syscon.h>
27#include <linux/regmap.h>
28#include <linux/of.h>
29#include <linux/of_device.h>
30#include <linux/component.h>
31#include <linux/sys_soc.h>
32#include <drm/drm_fourcc.h>
33#include <drm/drm_blend.h>
34
35#include "omapdss.h"
36#include "dss.h"
37#include "dispc.h"
38
39struct dispc_device;
40
41/* DISPC */
42#define DISPC_SZ_REGS SZ_4K
43
44enum omap_burst_size {
45 BURST_SIZE_X2 = 0,
46 BURST_SIZE_X4 = 1,
47 BURST_SIZE_X8 = 2,
48};
49
50#define REG_GET(dispc, idx, start, end) \
51 FLD_GET(dispc_read_reg(dispc, idx), start, end)
52
53#define REG_FLD_MOD(dispc, idx, val, start, end) \
54 dispc_write_reg(dispc, idx, \
55 FLD_MOD(dispc_read_reg(dispc, idx), val, start, end))
56
57/* DISPC has feature id */
58enum dispc_feature_id {
59 FEAT_LCDENABLEPOL,
60 FEAT_LCDENABLESIGNAL,
61 FEAT_PCKFREEENABLE,
62 FEAT_FUNCGATED,
63 FEAT_MGR_LCD2,
64 FEAT_MGR_LCD3,
65 FEAT_LINEBUFFERSPLIT,
66 FEAT_ROWREPEATENABLE,
67 FEAT_RESIZECONF,
68 /* Independent core clk divider */
69 FEAT_CORE_CLK_DIV,
70 FEAT_HANDLE_UV_SEPARATE,
71 FEAT_ATTR2,
72 FEAT_CPR,
73 FEAT_PRELOAD,
74 FEAT_FIR_COEF_V,
75 FEAT_ALPHA_FIXED_ZORDER,
76 FEAT_ALPHA_FREE_ZORDER,
77 FEAT_FIFO_MERGE,
78 /* An unknown HW bug causing the normal FIFO thresholds not to work */
79 FEAT_OMAP3_DSI_FIFO_BUG,
80 FEAT_BURST_2D,
81 FEAT_MFLAG,
82};
83
84struct dispc_features {
85 u8 sw_start;
86 u8 fp_start;
87 u8 bp_start;
88 u16 sw_max;
89 u16 vp_max;
90 u16 hp_max;
91 u8 mgr_width_start;
92 u8 mgr_height_start;
93 u16 mgr_width_max;
94 u16 mgr_height_max;
95 unsigned long max_lcd_pclk;
96 unsigned long max_tv_pclk;
97 unsigned int max_downscale;
98 unsigned int max_line_width;
99 unsigned int min_pcd;
100 int (*calc_scaling)(struct dispc_device *dispc,
101 unsigned long pclk, unsigned long lclk,
102 const struct videomode *vm,
103 u16 width, u16 height, u16 out_width, u16 out_height,
104 u32 fourcc, bool *five_taps,
105 int *x_predecim, int *y_predecim, int *decim_x, int *decim_y,
106 u16 pos_x, unsigned long *core_clk, bool mem_to_mem);
107 unsigned long (*calc_core_clk) (unsigned long pclk,
108 u16 width, u16 height, u16 out_width, u16 out_height,
109 bool mem_to_mem);
110 u8 num_fifos;
111 const enum dispc_feature_id *features;
112 unsigned int num_features;
113 const struct dss_reg_field *reg_fields;
114 const unsigned int num_reg_fields;
115 const enum omap_overlay_caps *overlay_caps;
116 const u32 **supported_color_modes;
117 unsigned int num_mgrs;
118 unsigned int num_ovls;
119 unsigned int buffer_size_unit;
120 unsigned int burst_size_unit;
121
122 /* swap GFX & WB fifos */
123 bool gfx_fifo_workaround:1;
124
125 /* no DISPC_IRQ_FRAMEDONETV on this SoC */
126 bool no_framedone_tv:1;
127
128 /* revert to the OMAP4 mechanism of DISPC Smart Standby operation */
129 bool mstandby_workaround:1;
130
131 bool set_max_preload:1;
132
133 /* PIXEL_INC is not added to the last pixel of a line */
134 bool last_pixel_inc_missing:1;
135
136 /* POL_FREQ has ALIGN bit */
137 bool supports_sync_align:1;
138
139 bool has_writeback:1;
140
141 bool supports_double_pixel:1;
142
143 /*
144 * Field order for VENC is different than HDMI. We should handle this in
145 * some intelligent manner, but as the SoCs have either HDMI or VENC,
146 * never both, we can just use this flag for now.
147 */
148 bool reverse_ilace_field_order:1;
149
150 bool has_gamma_table:1;
151
152 bool has_gamma_i734_bug:1;
153};
154
155#define DISPC_MAX_NR_FIFOS 5
156#define DISPC_MAX_CHANNEL_GAMMA 4
157
158struct dispc_device {
159 struct platform_device *pdev;
160 void __iomem *base;
161 struct dss_device *dss;
162
163 struct dss_debugfs_entry *debugfs;
164
165 int irq;
166 irq_handler_t user_handler;
167 void *user_data;
168
169 unsigned long core_clk_rate;
170 unsigned long tv_pclk_rate;
171
172 u32 fifo_size[DISPC_MAX_NR_FIFOS];
173 /* maps which plane is using a fifo. fifo-id -> plane-id */
174 int fifo_assignment[DISPC_MAX_NR_FIFOS];
175
176 bool ctx_valid;
177 u32 ctx[DISPC_SZ_REGS / sizeof(u32)];
178
179 u32 *gamma_table[DISPC_MAX_CHANNEL_GAMMA];
180
181 const struct dispc_features *feat;
182
183 bool is_enabled;
184
185 struct regmap *syscon_pol;
186 u32 syscon_pol_offset;
187
188 /* DISPC_CONTROL & DISPC_CONFIG lock*/
189 spinlock_t control_lock;
190};
191
192enum omap_color_component {
193 /* used for all color formats for OMAP3 and earlier
194 * and for RGB and Y color component on OMAP4
195 */
196 DISPC_COLOR_COMPONENT_RGB_Y = 1 << 0,
197 /* used for UV component for
198 * DRM_FORMAT_YUYV, DRM_FORMAT_UYVY, DRM_FORMAT_NV12
199 * color formats on OMAP4
200 */
201 DISPC_COLOR_COMPONENT_UV = 1 << 1,
202};
203
204enum mgr_reg_fields {
205 DISPC_MGR_FLD_ENABLE,
206 DISPC_MGR_FLD_STNTFT,
207 DISPC_MGR_FLD_GO,
208 DISPC_MGR_FLD_TFTDATALINES,
209 DISPC_MGR_FLD_STALLMODE,
210 DISPC_MGR_FLD_TCKENABLE,
211 DISPC_MGR_FLD_TCKSELECTION,
212 DISPC_MGR_FLD_CPR,
213 DISPC_MGR_FLD_FIFOHANDCHECK,
214 /* used to maintain a count of the above fields */
215 DISPC_MGR_FLD_NUM,
216};
217
218/* DISPC register field id */
219enum dispc_feat_reg_field {
220 FEAT_REG_FIRHINC,
221 FEAT_REG_FIRVINC,
222 FEAT_REG_FIFOHIGHTHRESHOLD,
223 FEAT_REG_FIFOLOWTHRESHOLD,
224 FEAT_REG_FIFOSIZE,
225 FEAT_REG_HORIZONTALACCU,
226 FEAT_REG_VERTICALACCU,
227};
228
229struct dispc_reg_field {
230 u16 reg;
231 u8 high;
232 u8 low;
233};
234
235struct dispc_gamma_desc {
236 u32 len;
237 u32 bits;
238 u16 reg;
239 bool has_index;
240};
241
242static const struct {
243 const char *name;
244 u32 vsync_irq;
245 u32 framedone_irq;
246 u32 sync_lost_irq;
247 struct dispc_gamma_desc gamma;
248 struct dispc_reg_field reg_desc[DISPC_MGR_FLD_NUM];
249} mgr_desc[] = {
250 [OMAP_DSS_CHANNEL_LCD] = {
251 .name = "LCD",
252 .vsync_irq = DISPC_IRQ_VSYNC,
253 .framedone_irq = DISPC_IRQ_FRAMEDONE,
254 .sync_lost_irq = DISPC_IRQ_SYNC_LOST,
255 .gamma = {
256 .len = 256,
257 .bits = 8,
258 .reg = DISPC_GAMMA_TABLE0,
259 .has_index = true,
260 },
261 .reg_desc = {
262 [DISPC_MGR_FLD_ENABLE] = { DISPC_CONTROL, 0, 0 },
263 [DISPC_MGR_FLD_STNTFT] = { DISPC_CONTROL, 3, 3 },
264 [DISPC_MGR_FLD_GO] = { DISPC_CONTROL, 5, 5 },
265 [DISPC_MGR_FLD_TFTDATALINES] = { DISPC_CONTROL, 9, 8 },
266 [DISPC_MGR_FLD_STALLMODE] = { DISPC_CONTROL, 11, 11 },
267 [DISPC_MGR_FLD_TCKENABLE] = { DISPC_CONFIG, 10, 10 },
268 [DISPC_MGR_FLD_TCKSELECTION] = { DISPC_CONFIG, 11, 11 },
269 [DISPC_MGR_FLD_CPR] = { DISPC_CONFIG, 15, 15 },
270 [DISPC_MGR_FLD_FIFOHANDCHECK] = { DISPC_CONFIG, 16, 16 },
271 },
272 },
273 [OMAP_DSS_CHANNEL_DIGIT] = {
274 .name = "DIGIT",
275 .vsync_irq = DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_EVSYNC_EVEN,
276 .framedone_irq = DISPC_IRQ_FRAMEDONETV,
277 .sync_lost_irq = DISPC_IRQ_SYNC_LOST_DIGIT,
278 .gamma = {
279 .len = 1024,
280 .bits = 10,
281 .reg = DISPC_GAMMA_TABLE2,
282 .has_index = false,
283 },
284 .reg_desc = {
285 [DISPC_MGR_FLD_ENABLE] = { DISPC_CONTROL, 1, 1 },
286 [DISPC_MGR_FLD_STNTFT] = { },
287 [DISPC_MGR_FLD_GO] = { DISPC_CONTROL, 6, 6 },
288 [DISPC_MGR_FLD_TFTDATALINES] = { },
289 [DISPC_MGR_FLD_STALLMODE] = { },
290 [DISPC_MGR_FLD_TCKENABLE] = { DISPC_CONFIG, 12, 12 },
291 [DISPC_MGR_FLD_TCKSELECTION] = { DISPC_CONFIG, 13, 13 },
292 [DISPC_MGR_FLD_CPR] = { },
293 [DISPC_MGR_FLD_FIFOHANDCHECK] = { DISPC_CONFIG, 16, 16 },
294 },
295 },
296 [OMAP_DSS_CHANNEL_LCD2] = {
297 .name = "LCD2",
298 .vsync_irq = DISPC_IRQ_VSYNC2,
299 .framedone_irq = DISPC_IRQ_FRAMEDONE2,
300 .sync_lost_irq = DISPC_IRQ_SYNC_LOST2,
301 .gamma = {
302 .len = 256,
303 .bits = 8,
304 .reg = DISPC_GAMMA_TABLE1,
305 .has_index = true,
306 },
307 .reg_desc = {
308 [DISPC_MGR_FLD_ENABLE] = { DISPC_CONTROL2, 0, 0 },
309 [DISPC_MGR_FLD_STNTFT] = { DISPC_CONTROL2, 3, 3 },
310 [DISPC_MGR_FLD_GO] = { DISPC_CONTROL2, 5, 5 },
311 [DISPC_MGR_FLD_TFTDATALINES] = { DISPC_CONTROL2, 9, 8 },
312 [DISPC_MGR_FLD_STALLMODE] = { DISPC_CONTROL2, 11, 11 },
313 [DISPC_MGR_FLD_TCKENABLE] = { DISPC_CONFIG2, 10, 10 },
314 [DISPC_MGR_FLD_TCKSELECTION] = { DISPC_CONFIG2, 11, 11 },
315 [DISPC_MGR_FLD_CPR] = { DISPC_CONFIG2, 15, 15 },
316 [DISPC_MGR_FLD_FIFOHANDCHECK] = { DISPC_CONFIG2, 16, 16 },
317 },
318 },
319 [OMAP_DSS_CHANNEL_LCD3] = {
320 .name = "LCD3",
321 .vsync_irq = DISPC_IRQ_VSYNC3,
322 .framedone_irq = DISPC_IRQ_FRAMEDONE3,
323 .sync_lost_irq = DISPC_IRQ_SYNC_LOST3,
324 .gamma = {
325 .len = 256,
326 .bits = 8,
327 .reg = DISPC_GAMMA_TABLE3,
328 .has_index = true,
329 },
330 .reg_desc = {
331 [DISPC_MGR_FLD_ENABLE] = { DISPC_CONTROL3, 0, 0 },
332 [DISPC_MGR_FLD_STNTFT] = { DISPC_CONTROL3, 3, 3 },
333 [DISPC_MGR_FLD_GO] = { DISPC_CONTROL3, 5, 5 },
334 [DISPC_MGR_FLD_TFTDATALINES] = { DISPC_CONTROL3, 9, 8 },
335 [DISPC_MGR_FLD_STALLMODE] = { DISPC_CONTROL3, 11, 11 },
336 [DISPC_MGR_FLD_TCKENABLE] = { DISPC_CONFIG3, 10, 10 },
337 [DISPC_MGR_FLD_TCKSELECTION] = { DISPC_CONFIG3, 11, 11 },
338 [DISPC_MGR_FLD_CPR] = { DISPC_CONFIG3, 15, 15 },
339 [DISPC_MGR_FLD_FIFOHANDCHECK] = { DISPC_CONFIG3, 16, 16 },
340 },
341 },
342};
343
344static unsigned long dispc_fclk_rate(struct dispc_device *dispc);
345static unsigned long dispc_core_clk_rate(struct dispc_device *dispc);
346static unsigned long dispc_mgr_lclk_rate(struct dispc_device *dispc,
347 enum omap_channel channel);
348static unsigned long dispc_mgr_pclk_rate(struct dispc_device *dispc,
349 enum omap_channel channel);
350
351static unsigned long dispc_plane_pclk_rate(struct dispc_device *dispc,
352 enum omap_plane_id plane);
353static unsigned long dispc_plane_lclk_rate(struct dispc_device *dispc,
354 enum omap_plane_id plane);
355
356static void dispc_clear_irqstatus(struct dispc_device *dispc, u32 mask);
357
358static inline void dispc_write_reg(struct dispc_device *dispc, u16 idx, u32 val)
359{
360 __raw_writel(val, dispc->base + idx);
361}
362
363static inline u32 dispc_read_reg(struct dispc_device *dispc, u16 idx)
364{
365 return __raw_readl(dispc->base + idx);
366}
367
368static u32 mgr_fld_read(struct dispc_device *dispc, enum omap_channel channel,
369 enum mgr_reg_fields regfld)
370{
371 const struct dispc_reg_field rfld = mgr_desc[channel].reg_desc[regfld];
372
373 return REG_GET(dispc, rfld.reg, rfld.high, rfld.low);
374}
375
376static void mgr_fld_write(struct dispc_device *dispc, enum omap_channel channel,
377 enum mgr_reg_fields regfld, int val)
378{
379 const struct dispc_reg_field rfld = mgr_desc[channel].reg_desc[regfld];
380 const bool need_lock = rfld.reg == DISPC_CONTROL || rfld.reg == DISPC_CONFIG;
381 unsigned long flags;
382
383 if (need_lock) {
384 spin_lock_irqsave(&dispc->control_lock, flags);
385 REG_FLD_MOD(dispc, rfld.reg, val, rfld.high, rfld.low);
386 spin_unlock_irqrestore(&dispc->control_lock, flags);
387 } else {
388 REG_FLD_MOD(dispc, rfld.reg, val, rfld.high, rfld.low);
389 }
390}
391
392static int dispc_get_num_ovls(struct dispc_device *dispc)
393{
394 return dispc->feat->num_ovls;
395}
396
397static int dispc_get_num_mgrs(struct dispc_device *dispc)
398{
399 return dispc->feat->num_mgrs;
400}
401
402static void dispc_get_reg_field(struct dispc_device *dispc,
403 enum dispc_feat_reg_field id,
404 u8 *start, u8 *end)
405{
406 if (id >= dispc->feat->num_reg_fields)
407 BUG();
408
409 *start = dispc->feat->reg_fields[id].start;
410 *end = dispc->feat->reg_fields[id].end;
411}
412
413static bool dispc_has_feature(struct dispc_device *dispc,
414 enum dispc_feature_id id)
415{
416 unsigned int i;
417
418 for (i = 0; i < dispc->feat->num_features; i++) {
419 if (dispc->feat->features[i] == id)
420 return true;
421 }
422
423 return false;
424}
425
426#define SR(dispc, reg) \
427 dispc->ctx[DISPC_##reg / sizeof(u32)] = dispc_read_reg(dispc, DISPC_##reg)
428#define RR(dispc, reg) \
429 dispc_write_reg(dispc, DISPC_##reg, dispc->ctx[DISPC_##reg / sizeof(u32)])
430
431static void dispc_save_context(struct dispc_device *dispc)
432{
433 int i, j;
434
435 DSSDBG("dispc_save_context\n");
436
437 SR(dispc, IRQENABLE);
438 SR(dispc, CONTROL);
439 SR(dispc, CONFIG);
440 SR(dispc, LINE_NUMBER);
441 if (dispc_has_feature(dispc, FEAT_ALPHA_FIXED_ZORDER) ||
442 dispc_has_feature(dispc, FEAT_ALPHA_FREE_ZORDER))
443 SR(dispc, GLOBAL_ALPHA);
444 if (dispc_has_feature(dispc, FEAT_MGR_LCD2)) {
445 SR(dispc, CONTROL2);
446 SR(dispc, CONFIG2);
447 }
448 if (dispc_has_feature(dispc, FEAT_MGR_LCD3)) {
449 SR(dispc, CONTROL3);
450 SR(dispc, CONFIG3);
451 }
452
453 for (i = 0; i < dispc_get_num_mgrs(dispc); i++) {
454 SR(dispc, DEFAULT_COLOR(i));
455 SR(dispc, TRANS_COLOR(i));
456 SR(dispc, SIZE_MGR(i));
457 if (i == OMAP_DSS_CHANNEL_DIGIT)
458 continue;
459 SR(dispc, TIMING_H(i));
460 SR(dispc, TIMING_V(i));
461 SR(dispc, POL_FREQ(i));
462 SR(dispc, DIVISORo(i));
463
464 SR(dispc, DATA_CYCLE1(i));
465 SR(dispc, DATA_CYCLE2(i));
466 SR(dispc, DATA_CYCLE3(i));
467
468 if (dispc_has_feature(dispc, FEAT_CPR)) {
469 SR(dispc, CPR_COEF_R(i));
470 SR(dispc, CPR_COEF_G(i));
471 SR(dispc, CPR_COEF_B(i));
472 }
473 }
474
475 for (i = 0; i < dispc_get_num_ovls(dispc); i++) {
476 SR(dispc, OVL_BA0(i));
477 SR(dispc, OVL_BA1(i));
478 SR(dispc, OVL_POSITION(i));
479 SR(dispc, OVL_SIZE(i));
480 SR(dispc, OVL_ATTRIBUTES(i));
481 SR(dispc, OVL_FIFO_THRESHOLD(i));
482 SR(dispc, OVL_ROW_INC(i));
483 SR(dispc, OVL_PIXEL_INC(i));
484 if (dispc_has_feature(dispc, FEAT_PRELOAD))
485 SR(dispc, OVL_PRELOAD(i));
486 if (i == OMAP_DSS_GFX) {
487 SR(dispc, OVL_WINDOW_SKIP(i));
488 SR(dispc, OVL_TABLE_BA(i));
489 continue;
490 }
491 SR(dispc, OVL_FIR(i));
492 SR(dispc, OVL_PICTURE_SIZE(i));
493 SR(dispc, OVL_ACCU0(i));
494 SR(dispc, OVL_ACCU1(i));
495
496 for (j = 0; j < 8; j++)
497 SR(dispc, OVL_FIR_COEF_H(i, j));
498
499 for (j = 0; j < 8; j++)
500 SR(dispc, OVL_FIR_COEF_HV(i, j));
501
502 for (j = 0; j < 5; j++)
503 SR(dispc, OVL_CONV_COEF(i, j));
504
505 if (dispc_has_feature(dispc, FEAT_FIR_COEF_V)) {
506 for (j = 0; j < 8; j++)
507 SR(dispc, OVL_FIR_COEF_V(i, j));
508 }
509
510 if (dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE)) {
511 SR(dispc, OVL_BA0_UV(i));
512 SR(dispc, OVL_BA1_UV(i));
513 SR(dispc, OVL_FIR2(i));
514 SR(dispc, OVL_ACCU2_0(i));
515 SR(dispc, OVL_ACCU2_1(i));
516
517 for (j = 0; j < 8; j++)
518 SR(dispc, OVL_FIR_COEF_H2(i, j));
519
520 for (j = 0; j < 8; j++)
521 SR(dispc, OVL_FIR_COEF_HV2(i, j));
522
523 for (j = 0; j < 8; j++)
524 SR(dispc, OVL_FIR_COEF_V2(i, j));
525 }
526 if (dispc_has_feature(dispc, FEAT_ATTR2))
527 SR(dispc, OVL_ATTRIBUTES2(i));
528 }
529
530 if (dispc_has_feature(dispc, FEAT_CORE_CLK_DIV))
531 SR(dispc, DIVISOR);
532
533 dispc->ctx_valid = true;
534
535 DSSDBG("context saved\n");
536}
537
538static void dispc_restore_context(struct dispc_device *dispc)
539{
540 int i, j;
541
542 DSSDBG("dispc_restore_context\n");
543
544 if (!dispc->ctx_valid)
545 return;
546
547 /*RR(dispc, IRQENABLE);*/
548 /*RR(dispc, CONTROL);*/
549 RR(dispc, CONFIG);
550 RR(dispc, LINE_NUMBER);
551 if (dispc_has_feature(dispc, FEAT_ALPHA_FIXED_ZORDER) ||
552 dispc_has_feature(dispc, FEAT_ALPHA_FREE_ZORDER))
553 RR(dispc, GLOBAL_ALPHA);
554 if (dispc_has_feature(dispc, FEAT_MGR_LCD2))
555 RR(dispc, CONFIG2);
556 if (dispc_has_feature(dispc, FEAT_MGR_LCD3))
557 RR(dispc, CONFIG3);
558
559 for (i = 0; i < dispc_get_num_mgrs(dispc); i++) {
560 RR(dispc, DEFAULT_COLOR(i));
561 RR(dispc, TRANS_COLOR(i));
562 RR(dispc, SIZE_MGR(i));
563 if (i == OMAP_DSS_CHANNEL_DIGIT)
564 continue;
565 RR(dispc, TIMING_H(i));
566 RR(dispc, TIMING_V(i));
567 RR(dispc, POL_FREQ(i));
568 RR(dispc, DIVISORo(i));
569
570 RR(dispc, DATA_CYCLE1(i));
571 RR(dispc, DATA_CYCLE2(i));
572 RR(dispc, DATA_CYCLE3(i));
573
574 if (dispc_has_feature(dispc, FEAT_CPR)) {
575 RR(dispc, CPR_COEF_R(i));
576 RR(dispc, CPR_COEF_G(i));
577 RR(dispc, CPR_COEF_B(i));
578 }
579 }
580
581 for (i = 0; i < dispc_get_num_ovls(dispc); i++) {
582 RR(dispc, OVL_BA0(i));
583 RR(dispc, OVL_BA1(i));
584 RR(dispc, OVL_POSITION(i));
585 RR(dispc, OVL_SIZE(i));
586 RR(dispc, OVL_ATTRIBUTES(i));
587 RR(dispc, OVL_FIFO_THRESHOLD(i));
588 RR(dispc, OVL_ROW_INC(i));
589 RR(dispc, OVL_PIXEL_INC(i));
590 if (dispc_has_feature(dispc, FEAT_PRELOAD))
591 RR(dispc, OVL_PRELOAD(i));
592 if (i == OMAP_DSS_GFX) {
593 RR(dispc, OVL_WINDOW_SKIP(i));
594 RR(dispc, OVL_TABLE_BA(i));
595 continue;
596 }
597 RR(dispc, OVL_FIR(i));
598 RR(dispc, OVL_PICTURE_SIZE(i));
599 RR(dispc, OVL_ACCU0(i));
600 RR(dispc, OVL_ACCU1(i));
601
602 for (j = 0; j < 8; j++)
603 RR(dispc, OVL_FIR_COEF_H(i, j));
604
605 for (j = 0; j < 8; j++)
606 RR(dispc, OVL_FIR_COEF_HV(i, j));
607
608 for (j = 0; j < 5; j++)
609 RR(dispc, OVL_CONV_COEF(i, j));
610
611 if (dispc_has_feature(dispc, FEAT_FIR_COEF_V)) {
612 for (j = 0; j < 8; j++)
613 RR(dispc, OVL_FIR_COEF_V(i, j));
614 }
615
616 if (dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE)) {
617 RR(dispc, OVL_BA0_UV(i));
618 RR(dispc, OVL_BA1_UV(i));
619 RR(dispc, OVL_FIR2(i));
620 RR(dispc, OVL_ACCU2_0(i));
621 RR(dispc, OVL_ACCU2_1(i));
622
623 for (j = 0; j < 8; j++)
624 RR(dispc, OVL_FIR_COEF_H2(i, j));
625
626 for (j = 0; j < 8; j++)
627 RR(dispc, OVL_FIR_COEF_HV2(i, j));
628
629 for (j = 0; j < 8; j++)
630 RR(dispc, OVL_FIR_COEF_V2(i, j));
631 }
632 if (dispc_has_feature(dispc, FEAT_ATTR2))
633 RR(dispc, OVL_ATTRIBUTES2(i));
634 }
635
636 if (dispc_has_feature(dispc, FEAT_CORE_CLK_DIV))
637 RR(dispc, DIVISOR);
638
639 /* enable last, because LCD & DIGIT enable are here */
640 RR(dispc, CONTROL);
641 if (dispc_has_feature(dispc, FEAT_MGR_LCD2))
642 RR(dispc, CONTROL2);
643 if (dispc_has_feature(dispc, FEAT_MGR_LCD3))
644 RR(dispc, CONTROL3);
645 /* clear spurious SYNC_LOST_DIGIT interrupts */
646 dispc_clear_irqstatus(dispc, DISPC_IRQ_SYNC_LOST_DIGIT);
647
648 /*
649 * enable last so IRQs won't trigger before
650 * the context is fully restored
651 */
652 RR(dispc, IRQENABLE);
653
654 DSSDBG("context restored\n");
655}
656
657#undef SR
658#undef RR
659
660int dispc_runtime_get(struct dispc_device *dispc)
661{
662 int r;
663
664 DSSDBG("dispc_runtime_get\n");
665
666 r = pm_runtime_get_sync(&dispc->pdev->dev);
667 WARN_ON(r < 0);
668 return r < 0 ? r : 0;
669}
670
671void dispc_runtime_put(struct dispc_device *dispc)
672{
673 int r;
674
675 DSSDBG("dispc_runtime_put\n");
676
677 r = pm_runtime_put_sync(&dispc->pdev->dev);
678 WARN_ON(r < 0 && r != -ENOSYS);
679}
680
681static u32 dispc_mgr_get_vsync_irq(struct dispc_device *dispc,
682 enum omap_channel channel)
683{
684 return mgr_desc[channel].vsync_irq;
685}
686
687static u32 dispc_mgr_get_framedone_irq(struct dispc_device *dispc,
688 enum omap_channel channel)
689{
690 if (channel == OMAP_DSS_CHANNEL_DIGIT && dispc->feat->no_framedone_tv)
691 return 0;
692
693 return mgr_desc[channel].framedone_irq;
694}
695
696static u32 dispc_mgr_get_sync_lost_irq(struct dispc_device *dispc,
697 enum omap_channel channel)
698{
699 return mgr_desc[channel].sync_lost_irq;
700}
701
702static u32 dispc_wb_get_framedone_irq(struct dispc_device *dispc)
703{
704 return DISPC_IRQ_FRAMEDONEWB;
705}
706
707static void dispc_mgr_enable(struct dispc_device *dispc,
708 enum omap_channel channel, bool enable)
709{
710 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_ENABLE, enable);
711 /* flush posted write */
712 mgr_fld_read(dispc, channel, DISPC_MGR_FLD_ENABLE);
713}
714
715static bool dispc_mgr_is_enabled(struct dispc_device *dispc,
716 enum omap_channel channel)
717{
718 return !!mgr_fld_read(dispc, channel, DISPC_MGR_FLD_ENABLE);
719}
720
721static bool dispc_mgr_go_busy(struct dispc_device *dispc,
722 enum omap_channel channel)
723{
724 return mgr_fld_read(dispc, channel, DISPC_MGR_FLD_GO) == 1;
725}
726
727static void dispc_mgr_go(struct dispc_device *dispc, enum omap_channel channel)
728{
729 WARN_ON(!dispc_mgr_is_enabled(dispc, channel));
730 WARN_ON(dispc_mgr_go_busy(dispc, channel));
731
732 DSSDBG("GO %s\n", mgr_desc[channel].name);
733
734 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_GO, 1);
735}
736
737static bool dispc_wb_go_busy(struct dispc_device *dispc)
738{
739 return REG_GET(dispc, DISPC_CONTROL2, 6, 6) == 1;
740}
741
742static void dispc_wb_go(struct dispc_device *dispc)
743{
744 enum omap_plane_id plane = OMAP_DSS_WB;
745 bool enable, go;
746
747 enable = REG_GET(dispc, DISPC_OVL_ATTRIBUTES(plane), 0, 0) == 1;
748
749 if (!enable)
750 return;
751
752 go = REG_GET(dispc, DISPC_CONTROL2, 6, 6) == 1;
753 if (go) {
754 DSSERR("GO bit not down for WB\n");
755 return;
756 }
757
758 REG_FLD_MOD(dispc, DISPC_CONTROL2, 1, 6, 6);
759}
760
761static void dispc_ovl_write_firh_reg(struct dispc_device *dispc,
762 enum omap_plane_id plane, int reg,
763 u32 value)
764{
765 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_H(plane, reg), value);
766}
767
768static void dispc_ovl_write_firhv_reg(struct dispc_device *dispc,
769 enum omap_plane_id plane, int reg,
770 u32 value)
771{
772 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_HV(plane, reg), value);
773}
774
775static void dispc_ovl_write_firv_reg(struct dispc_device *dispc,
776 enum omap_plane_id plane, int reg,
777 u32 value)
778{
779 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_V(plane, reg), value);
780}
781
782static void dispc_ovl_write_firh2_reg(struct dispc_device *dispc,
783 enum omap_plane_id plane, int reg,
784 u32 value)
785{
786 BUG_ON(plane == OMAP_DSS_GFX);
787
788 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_H2(plane, reg), value);
789}
790
791static void dispc_ovl_write_firhv2_reg(struct dispc_device *dispc,
792 enum omap_plane_id plane, int reg,
793 u32 value)
794{
795 BUG_ON(plane == OMAP_DSS_GFX);
796
797 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_HV2(plane, reg), value);
798}
799
800static void dispc_ovl_write_firv2_reg(struct dispc_device *dispc,
801 enum omap_plane_id plane, int reg,
802 u32 value)
803{
804 BUG_ON(plane == OMAP_DSS_GFX);
805
806 dispc_write_reg(dispc, DISPC_OVL_FIR_COEF_V2(plane, reg), value);
807}
808
809static void dispc_ovl_set_scale_coef(struct dispc_device *dispc,
810 enum omap_plane_id plane, int fir_hinc,
811 int fir_vinc, int five_taps,
812 enum omap_color_component color_comp)
813{
814 const struct dispc_coef *h_coef, *v_coef;
815 int i;
816
817 h_coef = dispc_ovl_get_scale_coef(fir_hinc, true);
818 v_coef = dispc_ovl_get_scale_coef(fir_vinc, five_taps);
819
820 if (!h_coef || !v_coef) {
821 dev_err(&dispc->pdev->dev, "%s: failed to find scale coefs\n",
822 __func__);
823 return;
824 }
825
826 for (i = 0; i < 8; i++) {
827 u32 h, hv;
828
829 h = FLD_VAL(h_coef[i].hc0_vc00, 7, 0)
830 | FLD_VAL(h_coef[i].hc1_vc0, 15, 8)
831 | FLD_VAL(h_coef[i].hc2_vc1, 23, 16)
832 | FLD_VAL(h_coef[i].hc3_vc2, 31, 24);
833 hv = FLD_VAL(h_coef[i].hc4_vc22, 7, 0)
834 | FLD_VAL(v_coef[i].hc1_vc0, 15, 8)
835 | FLD_VAL(v_coef[i].hc2_vc1, 23, 16)
836 | FLD_VAL(v_coef[i].hc3_vc2, 31, 24);
837
838 if (color_comp == DISPC_COLOR_COMPONENT_RGB_Y) {
839 dispc_ovl_write_firh_reg(dispc, plane, i, h);
840 dispc_ovl_write_firhv_reg(dispc, plane, i, hv);
841 } else {
842 dispc_ovl_write_firh2_reg(dispc, plane, i, h);
843 dispc_ovl_write_firhv2_reg(dispc, plane, i, hv);
844 }
845
846 }
847
848 if (five_taps) {
849 for (i = 0; i < 8; i++) {
850 u32 v;
851 v = FLD_VAL(v_coef[i].hc0_vc00, 7, 0)
852 | FLD_VAL(v_coef[i].hc4_vc22, 15, 8);
853 if (color_comp == DISPC_COLOR_COMPONENT_RGB_Y)
854 dispc_ovl_write_firv_reg(dispc, plane, i, v);
855 else
856 dispc_ovl_write_firv2_reg(dispc, plane, i, v);
857 }
858 }
859}
860
861struct csc_coef_yuv2rgb {
862 int ry, rcb, rcr, gy, gcb, gcr, by, bcb, bcr;
863 bool full_range;
864};
865
866struct csc_coef_rgb2yuv {
867 int yr, yg, yb, cbr, cbg, cbb, crr, crg, crb;
868 bool full_range;
869};
870
871static void dispc_ovl_write_color_conv_coef(struct dispc_device *dispc,
872 enum omap_plane_id plane,
873 const struct csc_coef_yuv2rgb *ct)
874{
875#define CVAL(x, y) (FLD_VAL(x, 26, 16) | FLD_VAL(y, 10, 0))
876
877 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 0), CVAL(ct->rcr, ct->ry));
878 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 1), CVAL(ct->gy, ct->rcb));
879 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 2), CVAL(ct->gcb, ct->gcr));
880 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 3), CVAL(ct->bcr, ct->by));
881 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 4), CVAL(0, ct->bcb));
882
883 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), ct->full_range, 11, 11);
884
885#undef CVAL
886}
887
888static void dispc_wb_write_color_conv_coef(struct dispc_device *dispc,
889 const struct csc_coef_rgb2yuv *ct)
890{
891 const enum omap_plane_id plane = OMAP_DSS_WB;
892
893#define CVAL(x, y) (FLD_VAL(x, 26, 16) | FLD_VAL(y, 10, 0))
894
895 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 0), CVAL(ct->yg, ct->yr));
896 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 1), CVAL(ct->crr, ct->yb));
897 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 2), CVAL(ct->crb, ct->crg));
898 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 3), CVAL(ct->cbg, ct->cbr));
899 dispc_write_reg(dispc, DISPC_OVL_CONV_COEF(plane, 4), CVAL(0, ct->cbb));
900
901 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), ct->full_range, 11, 11);
902
903#undef CVAL
904}
905
906static void dispc_setup_color_conv_coef(struct dispc_device *dispc)
907{
908 int i;
909 int num_ovl = dispc_get_num_ovls(dispc);
910
911 /* YUV -> RGB, ITU-R BT.601, limited range */
912 const struct csc_coef_yuv2rgb coefs_yuv2rgb_bt601_lim = {
913 298, 0, 409, /* ry, rcb, rcr */
914 298, -100, -208, /* gy, gcb, gcr */
915 298, 516, 0, /* by, bcb, bcr */
916 false, /* limited range */
917 };
918
919 /* RGB -> YUV, ITU-R BT.601, limited range */
920 const struct csc_coef_rgb2yuv coefs_rgb2yuv_bt601_lim = {
921 66, 129, 25, /* yr, yg, yb */
922 -38, -74, 112, /* cbr, cbg, cbb */
923 112, -94, -18, /* crr, crg, crb */
924 false, /* limited range */
925 };
926
927 for (i = 1; i < num_ovl; i++)
928 dispc_ovl_write_color_conv_coef(dispc, i, &coefs_yuv2rgb_bt601_lim);
929
930 if (dispc->feat->has_writeback)
931 dispc_wb_write_color_conv_coef(dispc, &coefs_rgb2yuv_bt601_lim);
932}
933
934static void dispc_ovl_set_ba0(struct dispc_device *dispc,
935 enum omap_plane_id plane, u32 paddr)
936{
937 dispc_write_reg(dispc, DISPC_OVL_BA0(plane), paddr);
938}
939
940static void dispc_ovl_set_ba1(struct dispc_device *dispc,
941 enum omap_plane_id plane, u32 paddr)
942{
943 dispc_write_reg(dispc, DISPC_OVL_BA1(plane), paddr);
944}
945
946static void dispc_ovl_set_ba0_uv(struct dispc_device *dispc,
947 enum omap_plane_id plane, u32 paddr)
948{
949 dispc_write_reg(dispc, DISPC_OVL_BA0_UV(plane), paddr);
950}
951
952static void dispc_ovl_set_ba1_uv(struct dispc_device *dispc,
953 enum omap_plane_id plane, u32 paddr)
954{
955 dispc_write_reg(dispc, DISPC_OVL_BA1_UV(plane), paddr);
956}
957
958static void dispc_ovl_set_pos(struct dispc_device *dispc,
959 enum omap_plane_id plane,
960 enum omap_overlay_caps caps, int x, int y)
961{
962 u32 val;
963
964 if ((caps & OMAP_DSS_OVL_CAP_POS) == 0)
965 return;
966
967 val = FLD_VAL(y, 26, 16) | FLD_VAL(x, 10, 0);
968
969 dispc_write_reg(dispc, DISPC_OVL_POSITION(plane), val);
970}
971
972static void dispc_ovl_set_input_size(struct dispc_device *dispc,
973 enum omap_plane_id plane, int width,
974 int height)
975{
976 u32 val = FLD_VAL(height - 1, 26, 16) | FLD_VAL(width - 1, 10, 0);
977
978 if (plane == OMAP_DSS_GFX || plane == OMAP_DSS_WB)
979 dispc_write_reg(dispc, DISPC_OVL_SIZE(plane), val);
980 else
981 dispc_write_reg(dispc, DISPC_OVL_PICTURE_SIZE(plane), val);
982}
983
984static void dispc_ovl_set_output_size(struct dispc_device *dispc,
985 enum omap_plane_id plane, int width,
986 int height)
987{
988 u32 val;
989
990 BUG_ON(plane == OMAP_DSS_GFX);
991
992 val = FLD_VAL(height - 1, 26, 16) | FLD_VAL(width - 1, 10, 0);
993
994 if (plane == OMAP_DSS_WB)
995 dispc_write_reg(dispc, DISPC_OVL_PICTURE_SIZE(plane), val);
996 else
997 dispc_write_reg(dispc, DISPC_OVL_SIZE(plane), val);
998}
999
1000static void dispc_ovl_set_zorder(struct dispc_device *dispc,
1001 enum omap_plane_id plane,
1002 enum omap_overlay_caps caps, u8 zorder)
1003{
1004 if ((caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
1005 return;
1006
1007 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), zorder, 27, 26);
1008}
1009
1010static void dispc_ovl_enable_zorder_planes(struct dispc_device *dispc)
1011{
1012 int i;
1013
1014 if (!dispc_has_feature(dispc, FEAT_ALPHA_FREE_ZORDER))
1015 return;
1016
1017 for (i = 0; i < dispc_get_num_ovls(dispc); i++)
1018 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(i), 1, 25, 25);
1019}
1020
1021static void dispc_ovl_set_pre_mult_alpha(struct dispc_device *dispc,
1022 enum omap_plane_id plane,
1023 enum omap_overlay_caps caps,
1024 bool enable)
1025{
1026 if ((caps & OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA) == 0)
1027 return;
1028
1029 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), enable ? 1 : 0, 28, 28);
1030}
1031
1032static void dispc_ovl_setup_global_alpha(struct dispc_device *dispc,
1033 enum omap_plane_id plane,
1034 enum omap_overlay_caps caps,
1035 u8 global_alpha)
1036{
1037 static const unsigned int shifts[] = { 0, 8, 16, 24, };
1038 int shift;
1039
1040 if ((caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
1041 return;
1042
1043 shift = shifts[plane];
1044 REG_FLD_MOD(dispc, DISPC_GLOBAL_ALPHA, global_alpha, shift + 7, shift);
1045}
1046
1047static void dispc_ovl_set_pix_inc(struct dispc_device *dispc,
1048 enum omap_plane_id plane, s32 inc)
1049{
1050 dispc_write_reg(dispc, DISPC_OVL_PIXEL_INC(plane), inc);
1051}
1052
1053static void dispc_ovl_set_row_inc(struct dispc_device *dispc,
1054 enum omap_plane_id plane, s32 inc)
1055{
1056 dispc_write_reg(dispc, DISPC_OVL_ROW_INC(plane), inc);
1057}
1058
1059static void dispc_ovl_set_color_mode(struct dispc_device *dispc,
1060 enum omap_plane_id plane, u32 fourcc)
1061{
1062 u32 m = 0;
1063 if (plane != OMAP_DSS_GFX) {
1064 switch (fourcc) {
1065 case DRM_FORMAT_NV12:
1066 m = 0x0; break;
1067 case DRM_FORMAT_XRGB4444:
1068 m = 0x1; break;
1069 case DRM_FORMAT_RGBA4444:
1070 m = 0x2; break;
1071 case DRM_FORMAT_RGBX4444:
1072 m = 0x4; break;
1073 case DRM_FORMAT_ARGB4444:
1074 m = 0x5; break;
1075 case DRM_FORMAT_RGB565:
1076 m = 0x6; break;
1077 case DRM_FORMAT_ARGB1555:
1078 m = 0x7; break;
1079 case DRM_FORMAT_XRGB8888:
1080 m = 0x8; break;
1081 case DRM_FORMAT_RGB888:
1082 m = 0x9; break;
1083 case DRM_FORMAT_YUYV:
1084 m = 0xa; break;
1085 case DRM_FORMAT_UYVY:
1086 m = 0xb; break;
1087 case DRM_FORMAT_ARGB8888:
1088 m = 0xc; break;
1089 case DRM_FORMAT_RGBA8888:
1090 m = 0xd; break;
1091 case DRM_FORMAT_RGBX8888:
1092 m = 0xe; break;
1093 case DRM_FORMAT_XRGB1555:
1094 m = 0xf; break;
1095 default:
1096 BUG(); return;
1097 }
1098 } else {
1099 switch (fourcc) {
1100 case DRM_FORMAT_RGBX4444:
1101 m = 0x4; break;
1102 case DRM_FORMAT_ARGB4444:
1103 m = 0x5; break;
1104 case DRM_FORMAT_RGB565:
1105 m = 0x6; break;
1106 case DRM_FORMAT_ARGB1555:
1107 m = 0x7; break;
1108 case DRM_FORMAT_XRGB8888:
1109 m = 0x8; break;
1110 case DRM_FORMAT_RGB888:
1111 m = 0x9; break;
1112 case DRM_FORMAT_XRGB4444:
1113 m = 0xa; break;
1114 case DRM_FORMAT_RGBA4444:
1115 m = 0xb; break;
1116 case DRM_FORMAT_ARGB8888:
1117 m = 0xc; break;
1118 case DRM_FORMAT_RGBA8888:
1119 m = 0xd; break;
1120 case DRM_FORMAT_RGBX8888:
1121 m = 0xe; break;
1122 case DRM_FORMAT_XRGB1555:
1123 m = 0xf; break;
1124 default:
1125 BUG(); return;
1126 }
1127 }
1128
1129 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), m, 4, 1);
1130}
1131
1132static void dispc_ovl_configure_burst_type(struct dispc_device *dispc,
1133 enum omap_plane_id plane,
1134 enum omap_dss_rotation_type rotation)
1135{
1136 if (dispc_has_feature(dispc, FEAT_BURST_2D) == 0)
1137 return;
1138
1139 if (rotation == OMAP_DSS_ROT_TILER)
1140 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), 1, 29, 29);
1141 else
1142 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), 0, 29, 29);
1143}
1144
1145static void dispc_ovl_set_channel_out(struct dispc_device *dispc,
1146 enum omap_plane_id plane,
1147 enum omap_channel channel)
1148{
1149 int shift;
1150 u32 val;
1151 int chan = 0, chan2 = 0;
1152
1153 switch (plane) {
1154 case OMAP_DSS_GFX:
1155 shift = 8;
1156 break;
1157 case OMAP_DSS_VIDEO1:
1158 case OMAP_DSS_VIDEO2:
1159 case OMAP_DSS_VIDEO3:
1160 shift = 16;
1161 break;
1162 default:
1163 BUG();
1164 return;
1165 }
1166
1167 val = dispc_read_reg(dispc, DISPC_OVL_ATTRIBUTES(plane));
1168 if (dispc_has_feature(dispc, FEAT_MGR_LCD2)) {
1169 switch (channel) {
1170 case OMAP_DSS_CHANNEL_LCD:
1171 chan = 0;
1172 chan2 = 0;
1173 break;
1174 case OMAP_DSS_CHANNEL_DIGIT:
1175 chan = 1;
1176 chan2 = 0;
1177 break;
1178 case OMAP_DSS_CHANNEL_LCD2:
1179 chan = 0;
1180 chan2 = 1;
1181 break;
1182 case OMAP_DSS_CHANNEL_LCD3:
1183 if (dispc_has_feature(dispc, FEAT_MGR_LCD3)) {
1184 chan = 0;
1185 chan2 = 2;
1186 } else {
1187 BUG();
1188 return;
1189 }
1190 break;
1191 case OMAP_DSS_CHANNEL_WB:
1192 chan = 0;
1193 chan2 = 3;
1194 break;
1195 default:
1196 BUG();
1197 return;
1198 }
1199
1200 val = FLD_MOD(val, chan, shift, shift);
1201 val = FLD_MOD(val, chan2, 31, 30);
1202 } else {
1203 val = FLD_MOD(val, channel, shift, shift);
1204 }
1205 dispc_write_reg(dispc, DISPC_OVL_ATTRIBUTES(plane), val);
1206}
1207
1208static enum omap_channel dispc_ovl_get_channel_out(struct dispc_device *dispc,
1209 enum omap_plane_id plane)
1210{
1211 int shift;
1212 u32 val;
1213
1214 switch (plane) {
1215 case OMAP_DSS_GFX:
1216 shift = 8;
1217 break;
1218 case OMAP_DSS_VIDEO1:
1219 case OMAP_DSS_VIDEO2:
1220 case OMAP_DSS_VIDEO3:
1221 shift = 16;
1222 break;
1223 default:
1224 BUG();
1225 return 0;
1226 }
1227
1228 val = dispc_read_reg(dispc, DISPC_OVL_ATTRIBUTES(plane));
1229
1230 if (FLD_GET(val, shift, shift) == 1)
1231 return OMAP_DSS_CHANNEL_DIGIT;
1232
1233 if (!dispc_has_feature(dispc, FEAT_MGR_LCD2))
1234 return OMAP_DSS_CHANNEL_LCD;
1235
1236 switch (FLD_GET(val, 31, 30)) {
1237 case 0:
1238 default:
1239 return OMAP_DSS_CHANNEL_LCD;
1240 case 1:
1241 return OMAP_DSS_CHANNEL_LCD2;
1242 case 2:
1243 return OMAP_DSS_CHANNEL_LCD3;
1244 case 3:
1245 return OMAP_DSS_CHANNEL_WB;
1246 }
1247}
1248
1249static void dispc_ovl_set_burst_size(struct dispc_device *dispc,
1250 enum omap_plane_id plane,
1251 enum omap_burst_size burst_size)
1252{
1253 static const unsigned int shifts[] = { 6, 14, 14, 14, 14, };
1254 int shift;
1255
1256 shift = shifts[plane];
1257 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), burst_size,
1258 shift + 1, shift);
1259}
1260
1261static void dispc_configure_burst_sizes(struct dispc_device *dispc)
1262{
1263 int i;
1264 const int burst_size = BURST_SIZE_X8;
1265
1266 /* Configure burst size always to maximum size */
1267 for (i = 0; i < dispc_get_num_ovls(dispc); ++i)
1268 dispc_ovl_set_burst_size(dispc, i, burst_size);
1269 if (dispc->feat->has_writeback)
1270 dispc_ovl_set_burst_size(dispc, OMAP_DSS_WB, burst_size);
1271}
1272
1273static u32 dispc_ovl_get_burst_size(struct dispc_device *dispc,
1274 enum omap_plane_id plane)
1275{
1276 /* burst multiplier is always x8 (see dispc_configure_burst_sizes()) */
1277 return dispc->feat->burst_size_unit * 8;
1278}
1279
1280static bool dispc_ovl_color_mode_supported(struct dispc_device *dispc,
1281 enum omap_plane_id plane, u32 fourcc)
1282{
1283 const u32 *modes;
1284 unsigned int i;
1285
1286 modes = dispc->feat->supported_color_modes[plane];
1287
1288 for (i = 0; modes[i]; ++i) {
1289 if (modes[i] == fourcc)
1290 return true;
1291 }
1292
1293 return false;
1294}
1295
1296static const u32 *dispc_ovl_get_color_modes(struct dispc_device *dispc,
1297 enum omap_plane_id plane)
1298{
1299 return dispc->feat->supported_color_modes[plane];
1300}
1301
1302static void dispc_mgr_enable_cpr(struct dispc_device *dispc,
1303 enum omap_channel channel, bool enable)
1304{
1305 if (channel == OMAP_DSS_CHANNEL_DIGIT)
1306 return;
1307
1308 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_CPR, enable);
1309}
1310
1311static void dispc_mgr_set_cpr_coef(struct dispc_device *dispc,
1312 enum omap_channel channel,
1313 const struct omap_dss_cpr_coefs *coefs)
1314{
1315 u32 coef_r, coef_g, coef_b;
1316
1317 if (!dss_mgr_is_lcd(channel))
1318 return;
1319
1320 coef_r = FLD_VAL(coefs->rr, 31, 22) | FLD_VAL(coefs->rg, 20, 11) |
1321 FLD_VAL(coefs->rb, 9, 0);
1322 coef_g = FLD_VAL(coefs->gr, 31, 22) | FLD_VAL(coefs->gg, 20, 11) |
1323 FLD_VAL(coefs->gb, 9, 0);
1324 coef_b = FLD_VAL(coefs->br, 31, 22) | FLD_VAL(coefs->bg, 20, 11) |
1325 FLD_VAL(coefs->bb, 9, 0);
1326
1327 dispc_write_reg(dispc, DISPC_CPR_COEF_R(channel), coef_r);
1328 dispc_write_reg(dispc, DISPC_CPR_COEF_G(channel), coef_g);
1329 dispc_write_reg(dispc, DISPC_CPR_COEF_B(channel), coef_b);
1330}
1331
1332static void dispc_ovl_set_vid_color_conv(struct dispc_device *dispc,
1333 enum omap_plane_id plane, bool enable)
1334{
1335 u32 val;
1336
1337 BUG_ON(plane == OMAP_DSS_GFX);
1338
1339 val = dispc_read_reg(dispc, DISPC_OVL_ATTRIBUTES(plane));
1340 val = FLD_MOD(val, enable, 9, 9);
1341 dispc_write_reg(dispc, DISPC_OVL_ATTRIBUTES(plane), val);
1342}
1343
1344static void dispc_ovl_enable_replication(struct dispc_device *dispc,
1345 enum omap_plane_id plane,
1346 enum omap_overlay_caps caps,
1347 bool enable)
1348{
1349 static const unsigned int shifts[] = { 5, 10, 10, 10 };
1350 int shift;
1351
1352 if ((caps & OMAP_DSS_OVL_CAP_REPLICATION) == 0)
1353 return;
1354
1355 shift = shifts[plane];
1356 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), enable, shift, shift);
1357}
1358
1359static void dispc_mgr_set_size(struct dispc_device *dispc,
1360 enum omap_channel channel, u16 width, u16 height)
1361{
1362 u32 val;
1363
1364 val = FLD_VAL(height - 1, dispc->feat->mgr_height_start, 16) |
1365 FLD_VAL(width - 1, dispc->feat->mgr_width_start, 0);
1366
1367 dispc_write_reg(dispc, DISPC_SIZE_MGR(channel), val);
1368}
1369
1370static void dispc_init_fifos(struct dispc_device *dispc)
1371{
1372 u32 size;
1373 int fifo;
1374 u8 start, end;
1375 u32 unit;
1376 int i;
1377
1378 unit = dispc->feat->buffer_size_unit;
1379
1380 dispc_get_reg_field(dispc, FEAT_REG_FIFOSIZE, &start, &end);
1381
1382 for (fifo = 0; fifo < dispc->feat->num_fifos; ++fifo) {
1383 size = REG_GET(dispc, DISPC_OVL_FIFO_SIZE_STATUS(fifo),
1384 start, end);
1385 size *= unit;
1386 dispc->fifo_size[fifo] = size;
1387
1388 /*
1389 * By default fifos are mapped directly to overlays, fifo 0 to
1390 * ovl 0, fifo 1 to ovl 1, etc.
1391 */
1392 dispc->fifo_assignment[fifo] = fifo;
1393 }
1394
1395 /*
1396 * The GFX fifo on OMAP4 is smaller than the other fifos. The small fifo
1397 * causes problems with certain use cases, like using the tiler in 2D
1398 * mode. The below hack swaps the fifos of GFX and WB planes, thus
1399 * giving GFX plane a larger fifo. WB but should work fine with a
1400 * smaller fifo.
1401 */
1402 if (dispc->feat->gfx_fifo_workaround) {
1403 u32 v;
1404
1405 v = dispc_read_reg(dispc, DISPC_GLOBAL_BUFFER);
1406
1407 v = FLD_MOD(v, 4, 2, 0); /* GFX BUF top to WB */
1408 v = FLD_MOD(v, 4, 5, 3); /* GFX BUF bottom to WB */
1409 v = FLD_MOD(v, 0, 26, 24); /* WB BUF top to GFX */
1410 v = FLD_MOD(v, 0, 29, 27); /* WB BUF bottom to GFX */
1411
1412 dispc_write_reg(dispc, DISPC_GLOBAL_BUFFER, v);
1413
1414 dispc->fifo_assignment[OMAP_DSS_GFX] = OMAP_DSS_WB;
1415 dispc->fifo_assignment[OMAP_DSS_WB] = OMAP_DSS_GFX;
1416 }
1417
1418 /*
1419 * Setup default fifo thresholds.
1420 */
1421 for (i = 0; i < dispc_get_num_ovls(dispc); ++i) {
1422 u32 low, high;
1423 const bool use_fifomerge = false;
1424 const bool manual_update = false;
1425
1426 dispc_ovl_compute_fifo_thresholds(dispc, i, &low, &high,
1427 use_fifomerge, manual_update);
1428
1429 dispc_ovl_set_fifo_threshold(dispc, i, low, high);
1430 }
1431
1432 if (dispc->feat->has_writeback) {
1433 u32 low, high;
1434 const bool use_fifomerge = false;
1435 const bool manual_update = false;
1436
1437 dispc_ovl_compute_fifo_thresholds(dispc, OMAP_DSS_WB,
1438 &low, &high, use_fifomerge,
1439 manual_update);
1440
1441 dispc_ovl_set_fifo_threshold(dispc, OMAP_DSS_WB, low, high);
1442 }
1443}
1444
1445static u32 dispc_ovl_get_fifo_size(struct dispc_device *dispc,
1446 enum omap_plane_id plane)
1447{
1448 int fifo;
1449 u32 size = 0;
1450
1451 for (fifo = 0; fifo < dispc->feat->num_fifos; ++fifo) {
1452 if (dispc->fifo_assignment[fifo] == plane)
1453 size += dispc->fifo_size[fifo];
1454 }
1455
1456 return size;
1457}
1458
1459void dispc_ovl_set_fifo_threshold(struct dispc_device *dispc,
1460 enum omap_plane_id plane,
1461 u32 low, u32 high)
1462{
1463 u8 hi_start, hi_end, lo_start, lo_end;
1464 u32 unit;
1465
1466 unit = dispc->feat->buffer_size_unit;
1467
1468 WARN_ON(low % unit != 0);
1469 WARN_ON(high % unit != 0);
1470
1471 low /= unit;
1472 high /= unit;
1473
1474 dispc_get_reg_field(dispc, FEAT_REG_FIFOHIGHTHRESHOLD,
1475 &hi_start, &hi_end);
1476 dispc_get_reg_field(dispc, FEAT_REG_FIFOLOWTHRESHOLD,
1477 &lo_start, &lo_end);
1478
1479 DSSDBG("fifo(%d) threshold (bytes), old %u/%u, new %u/%u\n",
1480 plane,
1481 REG_GET(dispc, DISPC_OVL_FIFO_THRESHOLD(plane),
1482 lo_start, lo_end) * unit,
1483 REG_GET(dispc, DISPC_OVL_FIFO_THRESHOLD(plane),
1484 hi_start, hi_end) * unit,
1485 low * unit, high * unit);
1486
1487 dispc_write_reg(dispc, DISPC_OVL_FIFO_THRESHOLD(plane),
1488 FLD_VAL(high, hi_start, hi_end) |
1489 FLD_VAL(low, lo_start, lo_end));
1490
1491 /*
1492 * configure the preload to the pipeline's high threhold, if HT it's too
1493 * large for the preload field, set the threshold to the maximum value
1494 * that can be held by the preload register
1495 */
1496 if (dispc_has_feature(dispc, FEAT_PRELOAD) &&
1497 dispc->feat->set_max_preload && plane != OMAP_DSS_WB)
1498 dispc_write_reg(dispc, DISPC_OVL_PRELOAD(plane),
1499 min(high, 0xfffu));
1500}
1501
1502void dispc_enable_fifomerge(struct dispc_device *dispc, bool enable)
1503{
1504 if (!dispc_has_feature(dispc, FEAT_FIFO_MERGE)) {
1505 WARN_ON(enable);
1506 return;
1507 }
1508
1509 DSSDBG("FIFO merge %s\n", enable ? "enabled" : "disabled");
1510 REG_FLD_MOD(dispc, DISPC_CONFIG, enable ? 1 : 0, 14, 14);
1511}
1512
1513void dispc_ovl_compute_fifo_thresholds(struct dispc_device *dispc,
1514 enum omap_plane_id plane,
1515 u32 *fifo_low, u32 *fifo_high,
1516 bool use_fifomerge, bool manual_update)
1517{
1518 /*
1519 * All sizes are in bytes. Both the buffer and burst are made of
1520 * buffer_units, and the fifo thresholds must be buffer_unit aligned.
1521 */
1522 unsigned int buf_unit = dispc->feat->buffer_size_unit;
1523 unsigned int ovl_fifo_size, total_fifo_size, burst_size;
1524 int i;
1525
1526 burst_size = dispc_ovl_get_burst_size(dispc, plane);
1527 ovl_fifo_size = dispc_ovl_get_fifo_size(dispc, plane);
1528
1529 if (use_fifomerge) {
1530 total_fifo_size = 0;
1531 for (i = 0; i < dispc_get_num_ovls(dispc); ++i)
1532 total_fifo_size += dispc_ovl_get_fifo_size(dispc, i);
1533 } else {
1534 total_fifo_size = ovl_fifo_size;
1535 }
1536
1537 /*
1538 * We use the same low threshold for both fifomerge and non-fifomerge
1539 * cases, but for fifomerge we calculate the high threshold using the
1540 * combined fifo size
1541 */
1542
1543 if (manual_update && dispc_has_feature(dispc, FEAT_OMAP3_DSI_FIFO_BUG)) {
1544 *fifo_low = ovl_fifo_size - burst_size * 2;
1545 *fifo_high = total_fifo_size - burst_size;
1546 } else if (plane == OMAP_DSS_WB) {
1547 /*
1548 * Most optimal configuration for writeback is to push out data
1549 * to the interconnect the moment writeback pushes enough pixels
1550 * in the FIFO to form a burst
1551 */
1552 *fifo_low = 0;
1553 *fifo_high = burst_size;
1554 } else {
1555 *fifo_low = ovl_fifo_size - burst_size;
1556 *fifo_high = total_fifo_size - buf_unit;
1557 }
1558}
1559
1560static void dispc_ovl_set_mflag(struct dispc_device *dispc,
1561 enum omap_plane_id plane, bool enable)
1562{
1563 int bit;
1564
1565 if (plane == OMAP_DSS_GFX)
1566 bit = 14;
1567 else
1568 bit = 23;
1569
1570 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), enable, bit, bit);
1571}
1572
1573static void dispc_ovl_set_mflag_threshold(struct dispc_device *dispc,
1574 enum omap_plane_id plane,
1575 int low, int high)
1576{
1577 dispc_write_reg(dispc, DISPC_OVL_MFLAG_THRESHOLD(plane),
1578 FLD_VAL(high, 31, 16) | FLD_VAL(low, 15, 0));
1579}
1580
1581static void dispc_init_mflag(struct dispc_device *dispc)
1582{
1583 int i;
1584
1585 /*
1586 * HACK: NV12 color format and MFLAG seem to have problems working
1587 * together: using two displays, and having an NV12 overlay on one of
1588 * the displays will cause underflows/synclosts when MFLAG_CTRL=2.
1589 * Changing MFLAG thresholds and PRELOAD to certain values seem to
1590 * remove the errors, but there doesn't seem to be a clear logic on
1591 * which values work and which not.
1592 *
1593 * As a work-around, set force MFLAG to always on.
1594 */
1595 dispc_write_reg(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE,
1596 (1 << 0) | /* MFLAG_CTRL = force always on */
1597 (0 << 2)); /* MFLAG_START = disable */
1598
1599 for (i = 0; i < dispc_get_num_ovls(dispc); ++i) {
1600 u32 size = dispc_ovl_get_fifo_size(dispc, i);
1601 u32 unit = dispc->feat->buffer_size_unit;
1602 u32 low, high;
1603
1604 dispc_ovl_set_mflag(dispc, i, true);
1605
1606 /*
1607 * Simulation team suggests below thesholds:
1608 * HT = fifosize * 5 / 8;
1609 * LT = fifosize * 4 / 8;
1610 */
1611
1612 low = size * 4 / 8 / unit;
1613 high = size * 5 / 8 / unit;
1614
1615 dispc_ovl_set_mflag_threshold(dispc, i, low, high);
1616 }
1617
1618 if (dispc->feat->has_writeback) {
1619 u32 size = dispc_ovl_get_fifo_size(dispc, OMAP_DSS_WB);
1620 u32 unit = dispc->feat->buffer_size_unit;
1621 u32 low, high;
1622
1623 dispc_ovl_set_mflag(dispc, OMAP_DSS_WB, true);
1624
1625 /*
1626 * Simulation team suggests below thesholds:
1627 * HT = fifosize * 5 / 8;
1628 * LT = fifosize * 4 / 8;
1629 */
1630
1631 low = size * 4 / 8 / unit;
1632 high = size * 5 / 8 / unit;
1633
1634 dispc_ovl_set_mflag_threshold(dispc, OMAP_DSS_WB, low, high);
1635 }
1636}
1637
1638static void dispc_ovl_set_fir(struct dispc_device *dispc,
1639 enum omap_plane_id plane,
1640 int hinc, int vinc,
1641 enum omap_color_component color_comp)
1642{
1643 u32 val;
1644
1645 if (color_comp == DISPC_COLOR_COMPONENT_RGB_Y) {
1646 u8 hinc_start, hinc_end, vinc_start, vinc_end;
1647
1648 dispc_get_reg_field(dispc, FEAT_REG_FIRHINC,
1649 &hinc_start, &hinc_end);
1650 dispc_get_reg_field(dispc, FEAT_REG_FIRVINC,
1651 &vinc_start, &vinc_end);
1652 val = FLD_VAL(vinc, vinc_start, vinc_end) |
1653 FLD_VAL(hinc, hinc_start, hinc_end);
1654
1655 dispc_write_reg(dispc, DISPC_OVL_FIR(plane), val);
1656 } else {
1657 val = FLD_VAL(vinc, 28, 16) | FLD_VAL(hinc, 12, 0);
1658 dispc_write_reg(dispc, DISPC_OVL_FIR2(plane), val);
1659 }
1660}
1661
1662static void dispc_ovl_set_vid_accu0(struct dispc_device *dispc,
1663 enum omap_plane_id plane, int haccu,
1664 int vaccu)
1665{
1666 u32 val;
1667 u8 hor_start, hor_end, vert_start, vert_end;
1668
1669 dispc_get_reg_field(dispc, FEAT_REG_HORIZONTALACCU,
1670 &hor_start, &hor_end);
1671 dispc_get_reg_field(dispc, FEAT_REG_VERTICALACCU,
1672 &vert_start, &vert_end);
1673
1674 val = FLD_VAL(vaccu, vert_start, vert_end) |
1675 FLD_VAL(haccu, hor_start, hor_end);
1676
1677 dispc_write_reg(dispc, DISPC_OVL_ACCU0(plane), val);
1678}
1679
1680static void dispc_ovl_set_vid_accu1(struct dispc_device *dispc,
1681 enum omap_plane_id plane, int haccu,
1682 int vaccu)
1683{
1684 u32 val;
1685 u8 hor_start, hor_end, vert_start, vert_end;
1686
1687 dispc_get_reg_field(dispc, FEAT_REG_HORIZONTALACCU,
1688 &hor_start, &hor_end);
1689 dispc_get_reg_field(dispc, FEAT_REG_VERTICALACCU,
1690 &vert_start, &vert_end);
1691
1692 val = FLD_VAL(vaccu, vert_start, vert_end) |
1693 FLD_VAL(haccu, hor_start, hor_end);
1694
1695 dispc_write_reg(dispc, DISPC_OVL_ACCU1(plane), val);
1696}
1697
1698static void dispc_ovl_set_vid_accu2_0(struct dispc_device *dispc,
1699 enum omap_plane_id plane, int haccu,
1700 int vaccu)
1701{
1702 u32 val;
1703
1704 val = FLD_VAL(vaccu, 26, 16) | FLD_VAL(haccu, 10, 0);
1705 dispc_write_reg(dispc, DISPC_OVL_ACCU2_0(plane), val);
1706}
1707
1708static void dispc_ovl_set_vid_accu2_1(struct dispc_device *dispc,
1709 enum omap_plane_id plane, int haccu,
1710 int vaccu)
1711{
1712 u32 val;
1713
1714 val = FLD_VAL(vaccu, 26, 16) | FLD_VAL(haccu, 10, 0);
1715 dispc_write_reg(dispc, DISPC_OVL_ACCU2_1(plane), val);
1716}
1717
1718static void dispc_ovl_set_scale_param(struct dispc_device *dispc,
1719 enum omap_plane_id plane,
1720 u16 orig_width, u16 orig_height,
1721 u16 out_width, u16 out_height,
1722 bool five_taps, u8 rotation,
1723 enum omap_color_component color_comp)
1724{
1725 int fir_hinc, fir_vinc;
1726
1727 fir_hinc = 1024 * orig_width / out_width;
1728 fir_vinc = 1024 * orig_height / out_height;
1729
1730 dispc_ovl_set_scale_coef(dispc, plane, fir_hinc, fir_vinc, five_taps,
1731 color_comp);
1732 dispc_ovl_set_fir(dispc, plane, fir_hinc, fir_vinc, color_comp);
1733}
1734
1735static void dispc_ovl_set_accu_uv(struct dispc_device *dispc,
1736 enum omap_plane_id plane,
1737 u16 orig_width, u16 orig_height,
1738 u16 out_width, u16 out_height,
1739 bool ilace, u32 fourcc, u8 rotation)
1740{
1741 int h_accu2_0, h_accu2_1;
1742 int v_accu2_0, v_accu2_1;
1743 int chroma_hinc, chroma_vinc;
1744 int idx;
1745
1746 struct accu {
1747 s8 h0_m, h0_n;
1748 s8 h1_m, h1_n;
1749 s8 v0_m, v0_n;
1750 s8 v1_m, v1_n;
1751 };
1752
1753 const struct accu *accu_table;
1754 const struct accu *accu_val;
1755
1756 static const struct accu accu_nv12[4] = {
1757 { 0, 1, 0, 1 , -1, 2, 0, 1 },
1758 { 1, 2, -3, 4 , 0, 1, 0, 1 },
1759 { -1, 1, 0, 1 , -1, 2, 0, 1 },
1760 { -1, 2, -1, 2 , -1, 1, 0, 1 },
1761 };
1762
1763 static const struct accu accu_nv12_ilace[4] = {
1764 { 0, 1, 0, 1 , -3, 4, -1, 4 },
1765 { -1, 4, -3, 4 , 0, 1, 0, 1 },
1766 { -1, 1, 0, 1 , -1, 4, -3, 4 },
1767 { -3, 4, -3, 4 , -1, 1, 0, 1 },
1768 };
1769
1770 static const struct accu accu_yuv[4] = {
1771 { 0, 1, 0, 1, 0, 1, 0, 1 },
1772 { 0, 1, 0, 1, 0, 1, 0, 1 },
1773 { -1, 1, 0, 1, 0, 1, 0, 1 },
1774 { 0, 1, 0, 1, -1, 1, 0, 1 },
1775 };
1776
1777 /* Note: DSS HW rotates clockwise, DRM_MODE_ROTATE_* counter-clockwise */
1778 switch (rotation & DRM_MODE_ROTATE_MASK) {
1779 default:
1780 case DRM_MODE_ROTATE_0:
1781 idx = 0;
1782 break;
1783 case DRM_MODE_ROTATE_90:
1784 idx = 3;
1785 break;
1786 case DRM_MODE_ROTATE_180:
1787 idx = 2;
1788 break;
1789 case DRM_MODE_ROTATE_270:
1790 idx = 1;
1791 break;
1792 }
1793
1794 switch (fourcc) {
1795 case DRM_FORMAT_NV12:
1796 if (ilace)
1797 accu_table = accu_nv12_ilace;
1798 else
1799 accu_table = accu_nv12;
1800 break;
1801 case DRM_FORMAT_YUYV:
1802 case DRM_FORMAT_UYVY:
1803 accu_table = accu_yuv;
1804 break;
1805 default:
1806 BUG();
1807 return;
1808 }
1809
1810 accu_val = &accu_table[idx];
1811
1812 chroma_hinc = 1024 * orig_width / out_width;
1813 chroma_vinc = 1024 * orig_height / out_height;
1814
1815 h_accu2_0 = (accu_val->h0_m * chroma_hinc / accu_val->h0_n) % 1024;
1816 h_accu2_1 = (accu_val->h1_m * chroma_hinc / accu_val->h1_n) % 1024;
1817 v_accu2_0 = (accu_val->v0_m * chroma_vinc / accu_val->v0_n) % 1024;
1818 v_accu2_1 = (accu_val->v1_m * chroma_vinc / accu_val->v1_n) % 1024;
1819
1820 dispc_ovl_set_vid_accu2_0(dispc, plane, h_accu2_0, v_accu2_0);
1821 dispc_ovl_set_vid_accu2_1(dispc, plane, h_accu2_1, v_accu2_1);
1822}
1823
1824static void dispc_ovl_set_scaling_common(struct dispc_device *dispc,
1825 enum omap_plane_id plane,
1826 u16 orig_width, u16 orig_height,
1827 u16 out_width, u16 out_height,
1828 bool ilace, bool five_taps,
1829 bool fieldmode, u32 fourcc,
1830 u8 rotation)
1831{
1832 int accu0 = 0;
1833 int accu1 = 0;
1834 u32 l;
1835
1836 dispc_ovl_set_scale_param(dispc, plane, orig_width, orig_height,
1837 out_width, out_height, five_taps,
1838 rotation, DISPC_COLOR_COMPONENT_RGB_Y);
1839 l = dispc_read_reg(dispc, DISPC_OVL_ATTRIBUTES(plane));
1840
1841 /* RESIZEENABLE and VERTICALTAPS */
1842 l &= ~((0x3 << 5) | (0x1 << 21));
1843 l |= (orig_width != out_width) ? (1 << 5) : 0;
1844 l |= (orig_height != out_height) ? (1 << 6) : 0;
1845 l |= five_taps ? (1 << 21) : 0;
1846
1847 /* VRESIZECONF and HRESIZECONF */
1848 if (dispc_has_feature(dispc, FEAT_RESIZECONF)) {
1849 l &= ~(0x3 << 7);
1850 l |= (orig_width <= out_width) ? 0 : (1 << 7);
1851 l |= (orig_height <= out_height) ? 0 : (1 << 8);
1852 }
1853
1854 /* LINEBUFFERSPLIT */
1855 if (dispc_has_feature(dispc, FEAT_LINEBUFFERSPLIT)) {
1856 l &= ~(0x1 << 22);
1857 l |= five_taps ? (1 << 22) : 0;
1858 }
1859
1860 dispc_write_reg(dispc, DISPC_OVL_ATTRIBUTES(plane), l);
1861
1862 /*
1863 * field 0 = even field = bottom field
1864 * field 1 = odd field = top field
1865 */
1866 if (ilace && !fieldmode) {
1867 accu1 = 0;
1868 accu0 = ((1024 * orig_height / out_height) / 2) & 0x3ff;
1869 if (accu0 >= 1024/2) {
1870 accu1 = 1024/2;
1871 accu0 -= accu1;
1872 }
1873 }
1874
1875 dispc_ovl_set_vid_accu0(dispc, plane, 0, accu0);
1876 dispc_ovl_set_vid_accu1(dispc, plane, 0, accu1);
1877}
1878
1879static void dispc_ovl_set_scaling_uv(struct dispc_device *dispc,
1880 enum omap_plane_id plane,
1881 u16 orig_width, u16 orig_height,
1882 u16 out_width, u16 out_height,
1883 bool ilace, bool five_taps,
1884 bool fieldmode, u32 fourcc,
1885 u8 rotation)
1886{
1887 int scale_x = out_width != orig_width;
1888 int scale_y = out_height != orig_height;
1889 bool chroma_upscale = plane != OMAP_DSS_WB;
1890 const struct drm_format_info *info;
1891
1892 info = drm_format_info(fourcc);
1893
1894 if (!dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE))
1895 return;
1896
1897 if (!info->is_yuv) {
1898 /* reset chroma resampling for RGB formats */
1899 if (plane != OMAP_DSS_WB)
1900 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES2(plane),
1901 0, 8, 8);
1902 return;
1903 }
1904
1905 dispc_ovl_set_accu_uv(dispc, plane, orig_width, orig_height, out_width,
1906 out_height, ilace, fourcc, rotation);
1907
1908 switch (fourcc) {
1909 case DRM_FORMAT_NV12:
1910 if (chroma_upscale) {
1911 /* UV is subsampled by 2 horizontally and vertically */
1912 orig_height >>= 1;
1913 orig_width >>= 1;
1914 } else {
1915 /* UV is downsampled by 2 horizontally and vertically */
1916 orig_height <<= 1;
1917 orig_width <<= 1;
1918 }
1919
1920 break;
1921 case DRM_FORMAT_YUYV:
1922 case DRM_FORMAT_UYVY:
1923 /* For YUV422 with 90/270 rotation, we don't upsample chroma */
1924 if (!drm_rotation_90_or_270(rotation)) {
1925 if (chroma_upscale)
1926 /* UV is subsampled by 2 horizontally */
1927 orig_width >>= 1;
1928 else
1929 /* UV is downsampled by 2 horizontally */
1930 orig_width <<= 1;
1931 }
1932
1933 /* must use FIR for YUV422 if rotated */
1934 if ((rotation & DRM_MODE_ROTATE_MASK) != DRM_MODE_ROTATE_0)
1935 scale_x = scale_y = true;
1936
1937 break;
1938 default:
1939 BUG();
1940 return;
1941 }
1942
1943 if (out_width != orig_width)
1944 scale_x = true;
1945 if (out_height != orig_height)
1946 scale_y = true;
1947
1948 dispc_ovl_set_scale_param(dispc, plane, orig_width, orig_height,
1949 out_width, out_height, five_taps,
1950 rotation, DISPC_COLOR_COMPONENT_UV);
1951
1952 if (plane != OMAP_DSS_WB)
1953 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES2(plane),
1954 (scale_x || scale_y) ? 1 : 0, 8, 8);
1955
1956 /* set H scaling */
1957 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), scale_x ? 1 : 0, 5, 5);
1958 /* set V scaling */
1959 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), scale_y ? 1 : 0, 6, 6);
1960}
1961
1962static void dispc_ovl_set_scaling(struct dispc_device *dispc,
1963 enum omap_plane_id plane,
1964 u16 orig_width, u16 orig_height,
1965 u16 out_width, u16 out_height,
1966 bool ilace, bool five_taps,
1967 bool fieldmode, u32 fourcc,
1968 u8 rotation)
1969{
1970 BUG_ON(plane == OMAP_DSS_GFX);
1971
1972 dispc_ovl_set_scaling_common(dispc, plane, orig_width, orig_height,
1973 out_width, out_height, ilace, five_taps,
1974 fieldmode, fourcc, rotation);
1975
1976 dispc_ovl_set_scaling_uv(dispc, plane, orig_width, orig_height,
1977 out_width, out_height, ilace, five_taps,
1978 fieldmode, fourcc, rotation);
1979}
1980
1981static void dispc_ovl_set_rotation_attrs(struct dispc_device *dispc,
1982 enum omap_plane_id plane, u8 rotation,
1983 enum omap_dss_rotation_type rotation_type,
1984 u32 fourcc)
1985{
1986 bool row_repeat = false;
1987 int vidrot = 0;
1988
1989 /* Note: DSS HW rotates clockwise, DRM_MODE_ROTATE_* counter-clockwise */
1990 if (fourcc == DRM_FORMAT_YUYV || fourcc == DRM_FORMAT_UYVY) {
1991
1992 if (rotation & DRM_MODE_REFLECT_X) {
1993 switch (rotation & DRM_MODE_ROTATE_MASK) {
1994 case DRM_MODE_ROTATE_0:
1995 vidrot = 2;
1996 break;
1997 case DRM_MODE_ROTATE_90:
1998 vidrot = 1;
1999 break;
2000 case DRM_MODE_ROTATE_180:
2001 vidrot = 0;
2002 break;
2003 case DRM_MODE_ROTATE_270:
2004 vidrot = 3;
2005 break;
2006 }
2007 } else {
2008 switch (rotation & DRM_MODE_ROTATE_MASK) {
2009 case DRM_MODE_ROTATE_0:
2010 vidrot = 0;
2011 break;
2012 case DRM_MODE_ROTATE_90:
2013 vidrot = 3;
2014 break;
2015 case DRM_MODE_ROTATE_180:
2016 vidrot = 2;
2017 break;
2018 case DRM_MODE_ROTATE_270:
2019 vidrot = 1;
2020 break;
2021 }
2022 }
2023
2024 if (drm_rotation_90_or_270(rotation))
2025 row_repeat = true;
2026 else
2027 row_repeat = false;
2028 }
2029
2030 /*
2031 * OMAP4/5 Errata i631:
2032 * NV12 in 1D mode must use ROTATION=1. Otherwise DSS will fetch extra
2033 * rows beyond the framebuffer, which may cause OCP error.
2034 */
2035 if (fourcc == DRM_FORMAT_NV12 && rotation_type != OMAP_DSS_ROT_TILER)
2036 vidrot = 1;
2037
2038 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), vidrot, 13, 12);
2039 if (dispc_has_feature(dispc, FEAT_ROWREPEATENABLE))
2040 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane),
2041 row_repeat ? 1 : 0, 18, 18);
2042
2043 if (dispc_ovl_color_mode_supported(dispc, plane, DRM_FORMAT_NV12)) {
2044 bool doublestride =
2045 fourcc == DRM_FORMAT_NV12 &&
2046 rotation_type == OMAP_DSS_ROT_TILER &&
2047 !drm_rotation_90_or_270(rotation);
2048
2049 /* DOUBLESTRIDE */
2050 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane),
2051 doublestride, 22, 22);
2052 }
2053}
2054
2055static int color_mode_to_bpp(u32 fourcc)
2056{
2057 switch (fourcc) {
2058 case DRM_FORMAT_NV12:
2059 return 8;
2060 case DRM_FORMAT_RGBX4444:
2061 case DRM_FORMAT_RGB565:
2062 case DRM_FORMAT_ARGB4444:
2063 case DRM_FORMAT_YUYV:
2064 case DRM_FORMAT_UYVY:
2065 case DRM_FORMAT_RGBA4444:
2066 case DRM_FORMAT_XRGB4444:
2067 case DRM_FORMAT_ARGB1555:
2068 case DRM_FORMAT_XRGB1555:
2069 return 16;
2070 case DRM_FORMAT_RGB888:
2071 return 24;
2072 case DRM_FORMAT_XRGB8888:
2073 case DRM_FORMAT_ARGB8888:
2074 case DRM_FORMAT_RGBA8888:
2075 case DRM_FORMAT_RGBX8888:
2076 return 32;
2077 default:
2078 BUG();
2079 return 0;
2080 }
2081}
2082
2083static s32 pixinc(int pixels, u8 ps)
2084{
2085 if (pixels == 1)
2086 return 1;
2087 else if (pixels > 1)
2088 return 1 + (pixels - 1) * ps;
2089 else if (pixels < 0)
2090 return 1 - (-pixels + 1) * ps;
2091 else
2092 BUG();
2093 return 0;
2094}
2095
2096static void calc_offset(u16 screen_width, u16 width,
2097 u32 fourcc, bool fieldmode, unsigned int field_offset,
2098 unsigned int *offset0, unsigned int *offset1,
2099 s32 *row_inc, s32 *pix_inc, int x_predecim, int y_predecim,
2100 enum omap_dss_rotation_type rotation_type, u8 rotation)
2101{
2102 u8 ps;
2103
2104 ps = color_mode_to_bpp(fourcc) / 8;
2105
2106 DSSDBG("scrw %d, width %d\n", screen_width, width);
2107
2108 if (rotation_type == OMAP_DSS_ROT_TILER &&
2109 (fourcc == DRM_FORMAT_UYVY || fourcc == DRM_FORMAT_YUYV) &&
2110 drm_rotation_90_or_270(rotation)) {
2111 /*
2112 * HACK: ROW_INC needs to be calculated with TILER units.
2113 * We get such 'screen_width' that multiplying it with the
2114 * YUV422 pixel size gives the correct TILER container width.
2115 * However, 'width' is in pixels and multiplying it with YUV422
2116 * pixel size gives incorrect result. We thus multiply it here
2117 * with 2 to match the 32 bit TILER unit size.
2118 */
2119 width *= 2;
2120 }
2121
2122 /*
2123 * field 0 = even field = bottom field
2124 * field 1 = odd field = top field
2125 */
2126 *offset0 = field_offset * screen_width * ps;
2127 *offset1 = 0;
2128
2129 *row_inc = pixinc(1 + (y_predecim * screen_width - width * x_predecim) +
2130 (fieldmode ? screen_width : 0), ps);
2131 if (fourcc == DRM_FORMAT_YUYV || fourcc == DRM_FORMAT_UYVY)
2132 *pix_inc = pixinc(x_predecim, 2 * ps);
2133 else
2134 *pix_inc = pixinc(x_predecim, ps);
2135}
2136
2137/*
2138 * This function is used to avoid synclosts in OMAP3, because of some
2139 * undocumented horizontal position and timing related limitations.
2140 */
2141static int check_horiz_timing_omap3(unsigned long pclk, unsigned long lclk,
2142 const struct videomode *vm, u16 pos_x,
2143 u16 width, u16 height, u16 out_width, u16 out_height,
2144 bool five_taps)
2145{
2146 const int ds = DIV_ROUND_UP(height, out_height);
2147 unsigned long nonactive;
2148 static const u8 limits[3] = { 8, 10, 20 };
2149 u64 val, blank;
2150 int i;
2151
2152 nonactive = vm->hactive + vm->hfront_porch + vm->hsync_len +
2153 vm->hback_porch - out_width;
2154
2155 i = 0;
2156 if (out_height < height)
2157 i++;
2158 if (out_width < width)
2159 i++;
2160 blank = div_u64((u64)(vm->hback_porch + vm->hsync_len + vm->hfront_porch) *
2161 lclk, pclk);
2162 DSSDBG("blanking period + ppl = %llu (limit = %u)\n", blank, limits[i]);
2163 if (blank <= limits[i])
2164 return -EINVAL;
2165
2166 /* FIXME add checks for 3-tap filter once the limitations are known */
2167 if (!five_taps)
2168 return 0;
2169
2170 /*
2171 * Pixel data should be prepared before visible display point starts.
2172 * So, atleast DS-2 lines must have already been fetched by DISPC
2173 * during nonactive - pos_x period.
2174 */
2175 val = div_u64((u64)(nonactive - pos_x) * lclk, pclk);
2176 DSSDBG("(nonactive - pos_x) * pcd = %llu max(0, DS - 2) * width = %d\n",
2177 val, max(0, ds - 2) * width);
2178 if (val < max(0, ds - 2) * width)
2179 return -EINVAL;
2180
2181 /*
2182 * All lines need to be refilled during the nonactive period of which
2183 * only one line can be loaded during the active period. So, atleast
2184 * DS - 1 lines should be loaded during nonactive period.
2185 */
2186 val = div_u64((u64)nonactive * lclk, pclk);
2187 DSSDBG("nonactive * pcd = %llu, max(0, DS - 1) * width = %d\n",
2188 val, max(0, ds - 1) * width);
2189 if (val < max(0, ds - 1) * width)
2190 return -EINVAL;
2191
2192 return 0;
2193}
2194
2195static unsigned long calc_core_clk_five_taps(unsigned long pclk,
2196 const struct videomode *vm, u16 width,
2197 u16 height, u16 out_width, u16 out_height,
2198 u32 fourcc)
2199{
2200 u32 core_clk = 0;
2201 u64 tmp;
2202
2203 if (height <= out_height && width <= out_width)
2204 return (unsigned long) pclk;
2205
2206 if (height > out_height) {
2207 unsigned int ppl = vm->hactive;
2208
2209 tmp = (u64)pclk * height * out_width;
2210 do_div(tmp, 2 * out_height * ppl);
2211 core_clk = tmp;
2212
2213 if (height > 2 * out_height) {
2214 if (ppl == out_width)
2215 return 0;
2216
2217 tmp = (u64)pclk * (height - 2 * out_height) * out_width;
2218 do_div(tmp, 2 * out_height * (ppl - out_width));
2219 core_clk = max_t(u32, core_clk, tmp);
2220 }
2221 }
2222
2223 if (width > out_width) {
2224 tmp = (u64)pclk * width;
2225 do_div(tmp, out_width);
2226 core_clk = max_t(u32, core_clk, tmp);
2227
2228 if (fourcc == DRM_FORMAT_XRGB8888)
2229 core_clk <<= 1;
2230 }
2231
2232 return core_clk;
2233}
2234
2235static unsigned long calc_core_clk_24xx(unsigned long pclk, u16 width,
2236 u16 height, u16 out_width, u16 out_height, bool mem_to_mem)
2237{
2238 if (height > out_height && width > out_width)
2239 return pclk * 4;
2240 else
2241 return pclk * 2;
2242}
2243
2244static unsigned long calc_core_clk_34xx(unsigned long pclk, u16 width,
2245 u16 height, u16 out_width, u16 out_height, bool mem_to_mem)
2246{
2247 unsigned int hf, vf;
2248
2249 /*
2250 * FIXME how to determine the 'A' factor
2251 * for the no downscaling case ?
2252 */
2253
2254 if (width > 3 * out_width)
2255 hf = 4;
2256 else if (width > 2 * out_width)
2257 hf = 3;
2258 else if (width > out_width)
2259 hf = 2;
2260 else
2261 hf = 1;
2262 if (height > out_height)
2263 vf = 2;
2264 else
2265 vf = 1;
2266
2267 return pclk * vf * hf;
2268}
2269
2270static unsigned long calc_core_clk_44xx(unsigned long pclk, u16 width,
2271 u16 height, u16 out_width, u16 out_height, bool mem_to_mem)
2272{
2273 /*
2274 * If the overlay/writeback is in mem to mem mode, there are no
2275 * downscaling limitations with respect to pixel clock, return 1 as
2276 * required core clock to represent that we have sufficient enough
2277 * core clock to do maximum downscaling
2278 */
2279 if (mem_to_mem)
2280 return 1;
2281
2282 if (width > out_width)
2283 return DIV_ROUND_UP(pclk, out_width) * width;
2284 else
2285 return pclk;
2286}
2287
2288static int dispc_ovl_calc_scaling_24xx(struct dispc_device *dispc,
2289 unsigned long pclk, unsigned long lclk,
2290 const struct videomode *vm,
2291 u16 width, u16 height,
2292 u16 out_width, u16 out_height,
2293 u32 fourcc, bool *five_taps,
2294 int *x_predecim, int *y_predecim,
2295 int *decim_x, int *decim_y,
2296 u16 pos_x, unsigned long *core_clk,
2297 bool mem_to_mem)
2298{
2299 int error;
2300 u16 in_width, in_height;
2301 int min_factor = min(*decim_x, *decim_y);
2302 const int maxsinglelinewidth = dispc->feat->max_line_width;
2303
2304 *five_taps = false;
2305
2306 do {
2307 in_height = height / *decim_y;
2308 in_width = width / *decim_x;
2309 *core_clk = dispc->feat->calc_core_clk(pclk, in_width,
2310 in_height, out_width, out_height, mem_to_mem);
2311 error = (in_width > maxsinglelinewidth || !*core_clk ||
2312 *core_clk > dispc_core_clk_rate(dispc));
2313 if (error) {
2314 if (*decim_x == *decim_y) {
2315 *decim_x = min_factor;
2316 ++*decim_y;
2317 } else {
2318 swap(*decim_x, *decim_y);
2319 if (*decim_x < *decim_y)
2320 ++*decim_x;
2321 }
2322 }
2323 } while (*decim_x <= *x_predecim && *decim_y <= *y_predecim && error);
2324
2325 if (error) {
2326 DSSERR("failed to find scaling settings\n");
2327 return -EINVAL;
2328 }
2329
2330 if (in_width > maxsinglelinewidth) {
2331 DSSERR("Cannot scale max input width exceeded\n");
2332 return -EINVAL;
2333 }
2334 return 0;
2335}
2336
2337static int dispc_ovl_calc_scaling_34xx(struct dispc_device *dispc,
2338 unsigned long pclk, unsigned long lclk,
2339 const struct videomode *vm,
2340 u16 width, u16 height,
2341 u16 out_width, u16 out_height,
2342 u32 fourcc, bool *five_taps,
2343 int *x_predecim, int *y_predecim,
2344 int *decim_x, int *decim_y,
2345 u16 pos_x, unsigned long *core_clk,
2346 bool mem_to_mem)
2347{
2348 int error;
2349 u16 in_width, in_height;
2350 const int maxsinglelinewidth = dispc->feat->max_line_width;
2351
2352 do {
2353 in_height = height / *decim_y;
2354 in_width = width / *decim_x;
2355 *five_taps = in_height > out_height;
2356
2357 if (in_width > maxsinglelinewidth)
2358 if (in_height > out_height &&
2359 in_height < out_height * 2)
2360 *five_taps = false;
2361again:
2362 if (*five_taps)
2363 *core_clk = calc_core_clk_five_taps(pclk, vm,
2364 in_width, in_height, out_width,
2365 out_height, fourcc);
2366 else
2367 *core_clk = dispc->feat->calc_core_clk(pclk, in_width,
2368 in_height, out_width, out_height,
2369 mem_to_mem);
2370
2371 error = check_horiz_timing_omap3(pclk, lclk, vm,
2372 pos_x, in_width, in_height, out_width,
2373 out_height, *five_taps);
2374 if (error && *five_taps) {
2375 *five_taps = false;
2376 goto again;
2377 }
2378
2379 error = (error || in_width > maxsinglelinewidth * 2 ||
2380 (in_width > maxsinglelinewidth && *five_taps) ||
2381 !*core_clk || *core_clk > dispc_core_clk_rate(dispc));
2382
2383 if (!error) {
2384 /* verify that we're inside the limits of scaler */
2385 if (in_width / 4 > out_width)
2386 error = 1;
2387
2388 if (*five_taps) {
2389 if (in_height / 4 > out_height)
2390 error = 1;
2391 } else {
2392 if (in_height / 2 > out_height)
2393 error = 1;
2394 }
2395 }
2396
2397 if (error)
2398 ++*decim_y;
2399 } while (*decim_x <= *x_predecim && *decim_y <= *y_predecim && error);
2400
2401 if (error) {
2402 DSSERR("failed to find scaling settings\n");
2403 return -EINVAL;
2404 }
2405
2406 if (check_horiz_timing_omap3(pclk, lclk, vm, pos_x, in_width,
2407 in_height, out_width, out_height, *five_taps)) {
2408 DSSERR("horizontal timing too tight\n");
2409 return -EINVAL;
2410 }
2411
2412 if (in_width > (maxsinglelinewidth * 2)) {
2413 DSSERR("Cannot setup scaling\n");
2414 DSSERR("width exceeds maximum width possible\n");
2415 return -EINVAL;
2416 }
2417
2418 if (in_width > maxsinglelinewidth && *five_taps) {
2419 DSSERR("cannot setup scaling with five taps\n");
2420 return -EINVAL;
2421 }
2422 return 0;
2423}
2424
2425static int dispc_ovl_calc_scaling_44xx(struct dispc_device *dispc,
2426 unsigned long pclk, unsigned long lclk,
2427 const struct videomode *vm,
2428 u16 width, u16 height,
2429 u16 out_width, u16 out_height,
2430 u32 fourcc, bool *five_taps,
2431 int *x_predecim, int *y_predecim,
2432 int *decim_x, int *decim_y,
2433 u16 pos_x, unsigned long *core_clk,
2434 bool mem_to_mem)
2435{
2436 u16 in_width, in_width_max;
2437 int decim_x_min = *decim_x;
2438 u16 in_height = height / *decim_y;
2439 const int maxsinglelinewidth = dispc->feat->max_line_width;
2440 const int maxdownscale = dispc->feat->max_downscale;
2441
2442 if (mem_to_mem) {
2443 in_width_max = out_width * maxdownscale;
2444 } else {
2445 in_width_max = dispc_core_clk_rate(dispc)
2446 / DIV_ROUND_UP(pclk, out_width);
2447 }
2448
2449 *decim_x = DIV_ROUND_UP(width, in_width_max);
2450
2451 *decim_x = *decim_x > decim_x_min ? *decim_x : decim_x_min;
2452 if (*decim_x > *x_predecim)
2453 return -EINVAL;
2454
2455 do {
2456 in_width = width / *decim_x;
2457 } while (*decim_x <= *x_predecim &&
2458 in_width > maxsinglelinewidth && ++*decim_x);
2459
2460 if (in_width > maxsinglelinewidth) {
2461 DSSERR("Cannot scale width exceeds max line width\n");
2462 return -EINVAL;
2463 }
2464
2465 if (*decim_x > 4 && fourcc != DRM_FORMAT_NV12) {
2466 /*
2467 * Let's disable all scaling that requires horizontal
2468 * decimation with higher factor than 4, until we have
2469 * better estimates of what we can and can not
2470 * do. However, NV12 color format appears to work Ok
2471 * with all decimation factors.
2472 *
2473 * When decimating horizontally by more that 4 the dss
2474 * is not able to fetch the data in burst mode. When
2475 * this happens it is hard to tell if there enough
2476 * bandwidth. Despite what theory says this appears to
2477 * be true also for 16-bit color formats.
2478 */
2479 DSSERR("Not enough bandwidth, too much downscaling (x-decimation factor %d > 4)\n", *decim_x);
2480
2481 return -EINVAL;
2482 }
2483
2484 *core_clk = dispc->feat->calc_core_clk(pclk, in_width, in_height,
2485 out_width, out_height, mem_to_mem);
2486 return 0;
2487}
2488
2489#define DIV_FRAC(dividend, divisor) \
2490 ((dividend) * 100 / (divisor) - ((dividend) / (divisor) * 100))
2491
2492static int dispc_ovl_calc_scaling(struct dispc_device *dispc,
2493 enum omap_plane_id plane,
2494 unsigned long pclk, unsigned long lclk,
2495 enum omap_overlay_caps caps,
2496 const struct videomode *vm,
2497 u16 width, u16 height,
2498 u16 out_width, u16 out_height,
2499 u32 fourcc, bool *five_taps,
2500 int *x_predecim, int *y_predecim, u16 pos_x,
2501 enum omap_dss_rotation_type rotation_type,
2502 bool mem_to_mem)
2503{
2504 int maxhdownscale = dispc->feat->max_downscale;
2505 int maxvdownscale = dispc->feat->max_downscale;
2506 const int max_decim_limit = 16;
2507 unsigned long core_clk = 0;
2508 int decim_x, decim_y, ret;
2509
2510 if (width == out_width && height == out_height)
2511 return 0;
2512
2513 if (plane == OMAP_DSS_WB) {
2514 switch (fourcc) {
2515 case DRM_FORMAT_NV12:
2516 maxhdownscale = maxvdownscale = 2;
2517 break;
2518 case DRM_FORMAT_YUYV:
2519 case DRM_FORMAT_UYVY:
2520 maxhdownscale = 2;
2521 maxvdownscale = 4;
2522 break;
2523 default:
2524 break;
2525 }
2526 }
2527 if (!mem_to_mem && (pclk == 0 || vm->pixelclock == 0)) {
2528 DSSERR("cannot calculate scaling settings: pclk is zero\n");
2529 return -EINVAL;
2530 }
2531
2532 if ((caps & OMAP_DSS_OVL_CAP_SCALE) == 0)
2533 return -EINVAL;
2534
2535 if (mem_to_mem) {
2536 *x_predecim = *y_predecim = 1;
2537 } else {
2538 *x_predecim = max_decim_limit;
2539 *y_predecim = (rotation_type == OMAP_DSS_ROT_TILER &&
2540 dispc_has_feature(dispc, FEAT_BURST_2D)) ?
2541 2 : max_decim_limit;
2542 }
2543
2544 decim_x = DIV_ROUND_UP(DIV_ROUND_UP(width, out_width), maxhdownscale);
2545 decim_y = DIV_ROUND_UP(DIV_ROUND_UP(height, out_height), maxvdownscale);
2546
2547 if (decim_x > *x_predecim || out_width > width * 8)
2548 return -EINVAL;
2549
2550 if (decim_y > *y_predecim || out_height > height * 8)
2551 return -EINVAL;
2552
2553 ret = dispc->feat->calc_scaling(dispc, pclk, lclk, vm, width, height,
2554 out_width, out_height, fourcc,
2555 five_taps, x_predecim, y_predecim,
2556 &decim_x, &decim_y, pos_x, &core_clk,
2557 mem_to_mem);
2558 if (ret)
2559 return ret;
2560
2561 DSSDBG("%dx%d -> %dx%d (%d.%02d x %d.%02d), decim %dx%d %dx%d (%d.%02d x %d.%02d), taps %d, req clk %lu, cur clk %lu\n",
2562 width, height,
2563 out_width, out_height,
2564 out_width / width, DIV_FRAC(out_width, width),
2565 out_height / height, DIV_FRAC(out_height, height),
2566
2567 decim_x, decim_y,
2568 width / decim_x, height / decim_y,
2569 out_width / (width / decim_x), DIV_FRAC(out_width, width / decim_x),
2570 out_height / (height / decim_y), DIV_FRAC(out_height, height / decim_y),
2571
2572 *five_taps ? 5 : 3,
2573 core_clk, dispc_core_clk_rate(dispc));
2574
2575 if (!core_clk || core_clk > dispc_core_clk_rate(dispc)) {
2576 DSSERR("failed to set up scaling, "
2577 "required core clk rate = %lu Hz, "
2578 "current core clk rate = %lu Hz\n",
2579 core_clk, dispc_core_clk_rate(dispc));
2580 return -EINVAL;
2581 }
2582
2583 *x_predecim = decim_x;
2584 *y_predecim = decim_y;
2585 return 0;
2586}
2587
2588static int dispc_ovl_setup_common(struct dispc_device *dispc,
2589 enum omap_plane_id plane,
2590 enum omap_overlay_caps caps,
2591 u32 paddr, u32 p_uv_addr,
2592 u16 screen_width, int pos_x, int pos_y,
2593 u16 width, u16 height,
2594 u16 out_width, u16 out_height,
2595 u32 fourcc, u8 rotation, u8 zorder,
2596 u8 pre_mult_alpha, u8 global_alpha,
2597 enum omap_dss_rotation_type rotation_type,
2598 bool replication, const struct videomode *vm,
2599 bool mem_to_mem)
2600{
2601 bool five_taps = true;
2602 bool fieldmode = false;
2603 int r, cconv = 0;
2604 unsigned int offset0, offset1;
2605 s32 row_inc;
2606 s32 pix_inc;
2607 u16 frame_width;
2608 unsigned int field_offset = 0;
2609 u16 in_height = height;
2610 u16 in_width = width;
2611 int x_predecim = 1, y_predecim = 1;
2612 bool ilace = !!(vm->flags & DISPLAY_FLAGS_INTERLACED);
2613 unsigned long pclk = dispc_plane_pclk_rate(dispc, plane);
2614 unsigned long lclk = dispc_plane_lclk_rate(dispc, plane);
2615 const struct drm_format_info *info;
2616
2617 info = drm_format_info(fourcc);
2618
2619 /* when setting up WB, dispc_plane_pclk_rate() returns 0 */
2620 if (plane == OMAP_DSS_WB)
2621 pclk = vm->pixelclock;
2622
2623 if (paddr == 0 && rotation_type != OMAP_DSS_ROT_TILER)
2624 return -EINVAL;
2625
2626 if (info->is_yuv && (in_width & 1)) {
2627 DSSERR("input width %d is not even for YUV format\n", in_width);
2628 return -EINVAL;
2629 }
2630
2631 out_width = out_width == 0 ? width : out_width;
2632 out_height = out_height == 0 ? height : out_height;
2633
2634 if (plane != OMAP_DSS_WB) {
2635 if (ilace && height == out_height)
2636 fieldmode = true;
2637
2638 if (ilace) {
2639 if (fieldmode)
2640 in_height /= 2;
2641 pos_y /= 2;
2642 out_height /= 2;
2643
2644 DSSDBG("adjusting for ilace: height %d, pos_y %d, out_height %d\n",
2645 in_height, pos_y, out_height);
2646 }
2647 }
2648
2649 if (!dispc_ovl_color_mode_supported(dispc, plane, fourcc))
2650 return -EINVAL;
2651
2652 r = dispc_ovl_calc_scaling(dispc, plane, pclk, lclk, caps, vm, in_width,
2653 in_height, out_width, out_height, fourcc,
2654 &five_taps, &x_predecim, &y_predecim, pos_x,
2655 rotation_type, mem_to_mem);
2656 if (r)
2657 return r;
2658
2659 in_width = in_width / x_predecim;
2660 in_height = in_height / y_predecim;
2661
2662 if (x_predecim > 1 || y_predecim > 1)
2663 DSSDBG("predecimation %d x %x, new input size %d x %d\n",
2664 x_predecim, y_predecim, in_width, in_height);
2665
2666 if (info->is_yuv && (in_width & 1)) {
2667 DSSDBG("predecimated input width is not even for YUV format\n");
2668 DSSDBG("adjusting input width %d -> %d\n",
2669 in_width, in_width & ~1);
2670
2671 in_width &= ~1;
2672 }
2673
2674 if (info->is_yuv)
2675 cconv = 1;
2676
2677 if (ilace && !fieldmode) {
2678 /*
2679 * when downscaling the bottom field may have to start several
2680 * source lines below the top field. Unfortunately ACCUI
2681 * registers will only hold the fractional part of the offset
2682 * so the integer part must be added to the base address of the
2683 * bottom field.
2684 */
2685 if (!in_height || in_height == out_height)
2686 field_offset = 0;
2687 else
2688 field_offset = in_height / out_height / 2;
2689 }
2690
2691 /* Fields are independent but interleaved in memory. */
2692 if (fieldmode)
2693 field_offset = 1;
2694
2695 offset0 = 0;
2696 offset1 = 0;
2697 row_inc = 0;
2698 pix_inc = 0;
2699
2700 if (plane == OMAP_DSS_WB)
2701 frame_width = out_width;
2702 else
2703 frame_width = in_width;
2704
2705 calc_offset(screen_width, frame_width,
2706 fourcc, fieldmode, field_offset,
2707 &offset0, &offset1, &row_inc, &pix_inc,
2708 x_predecim, y_predecim,
2709 rotation_type, rotation);
2710
2711 DSSDBG("offset0 %u, offset1 %u, row_inc %d, pix_inc %d\n",
2712 offset0, offset1, row_inc, pix_inc);
2713
2714 dispc_ovl_set_color_mode(dispc, plane, fourcc);
2715
2716 dispc_ovl_configure_burst_type(dispc, plane, rotation_type);
2717
2718 if (dispc->feat->reverse_ilace_field_order)
2719 swap(offset0, offset1);
2720
2721 dispc_ovl_set_ba0(dispc, plane, paddr + offset0);
2722 dispc_ovl_set_ba1(dispc, plane, paddr + offset1);
2723
2724 if (fourcc == DRM_FORMAT_NV12) {
2725 dispc_ovl_set_ba0_uv(dispc, plane, p_uv_addr + offset0);
2726 dispc_ovl_set_ba1_uv(dispc, plane, p_uv_addr + offset1);
2727 }
2728
2729 if (dispc->feat->last_pixel_inc_missing)
2730 row_inc += pix_inc - 1;
2731
2732 dispc_ovl_set_row_inc(dispc, plane, row_inc);
2733 dispc_ovl_set_pix_inc(dispc, plane, pix_inc);
2734
2735 DSSDBG("%d,%d %dx%d -> %dx%d\n", pos_x, pos_y, in_width,
2736 in_height, out_width, out_height);
2737
2738 dispc_ovl_set_pos(dispc, plane, caps, pos_x, pos_y);
2739
2740 dispc_ovl_set_input_size(dispc, plane, in_width, in_height);
2741
2742 if (caps & OMAP_DSS_OVL_CAP_SCALE) {
2743 dispc_ovl_set_scaling(dispc, plane, in_width, in_height,
2744 out_width, out_height, ilace, five_taps,
2745 fieldmode, fourcc, rotation);
2746 dispc_ovl_set_output_size(dispc, plane, out_width, out_height);
2747 dispc_ovl_set_vid_color_conv(dispc, plane, cconv);
2748 }
2749
2750 dispc_ovl_set_rotation_attrs(dispc, plane, rotation, rotation_type,
2751 fourcc);
2752
2753 dispc_ovl_set_zorder(dispc, plane, caps, zorder);
2754 dispc_ovl_set_pre_mult_alpha(dispc, plane, caps, pre_mult_alpha);
2755 dispc_ovl_setup_global_alpha(dispc, plane, caps, global_alpha);
2756
2757 dispc_ovl_enable_replication(dispc, plane, caps, replication);
2758
2759 return 0;
2760}
2761
2762static int dispc_ovl_setup(struct dispc_device *dispc,
2763 enum omap_plane_id plane,
2764 const struct omap_overlay_info *oi,
2765 const struct videomode *vm, bool mem_to_mem,
2766 enum omap_channel channel)
2767{
2768 int r;
2769 enum omap_overlay_caps caps = dispc->feat->overlay_caps[plane];
2770 const bool replication = true;
2771
2772 DSSDBG("dispc_ovl_setup %d, pa %pad, pa_uv %pad, sw %d, %d,%d, %dx%d ->"
2773 " %dx%d, cmode %x, rot %d, chan %d repl %d\n",
2774 plane, &oi->paddr, &oi->p_uv_addr, oi->screen_width, oi->pos_x,
2775 oi->pos_y, oi->width, oi->height, oi->out_width, oi->out_height,
2776 oi->fourcc, oi->rotation, channel, replication);
2777
2778 dispc_ovl_set_channel_out(dispc, plane, channel);
2779
2780 r = dispc_ovl_setup_common(dispc, plane, caps, oi->paddr, oi->p_uv_addr,
2781 oi->screen_width, oi->pos_x, oi->pos_y, oi->width, oi->height,
2782 oi->out_width, oi->out_height, oi->fourcc, oi->rotation,
2783 oi->zorder, oi->pre_mult_alpha, oi->global_alpha,
2784 oi->rotation_type, replication, vm, mem_to_mem);
2785
2786 return r;
2787}
2788
2789static int dispc_wb_setup(struct dispc_device *dispc,
2790 const struct omap_dss_writeback_info *wi,
2791 bool mem_to_mem, const struct videomode *vm,
2792 enum dss_writeback_channel channel_in)
2793{
2794 int r;
2795 u32 l;
2796 enum omap_plane_id plane = OMAP_DSS_WB;
2797 const int pos_x = 0, pos_y = 0;
2798 const u8 zorder = 0, global_alpha = 0;
2799 const bool replication = true;
2800 bool truncation;
2801 int in_width = vm->hactive;
2802 int in_height = vm->vactive;
2803 enum omap_overlay_caps caps =
2804 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA;
2805
2806 if (vm->flags & DISPLAY_FLAGS_INTERLACED)
2807 in_height /= 2;
2808
2809 DSSDBG("dispc_wb_setup, pa %x, pa_uv %x, %d,%d -> %dx%d, cmode %x, "
2810 "rot %d\n", wi->paddr, wi->p_uv_addr, in_width,
2811 in_height, wi->width, wi->height, wi->fourcc, wi->rotation);
2812
2813 r = dispc_ovl_setup_common(dispc, plane, caps, wi->paddr, wi->p_uv_addr,
2814 wi->buf_width, pos_x, pos_y, in_width, in_height, wi->width,
2815 wi->height, wi->fourcc, wi->rotation, zorder,
2816 wi->pre_mult_alpha, global_alpha, wi->rotation_type,
2817 replication, vm, mem_to_mem);
2818 if (r)
2819 return r;
2820
2821 switch (wi->fourcc) {
2822 case DRM_FORMAT_RGB565:
2823 case DRM_FORMAT_RGB888:
2824 case DRM_FORMAT_ARGB4444:
2825 case DRM_FORMAT_RGBA4444:
2826 case DRM_FORMAT_RGBX4444:
2827 case DRM_FORMAT_ARGB1555:
2828 case DRM_FORMAT_XRGB1555:
2829 case DRM_FORMAT_XRGB4444:
2830 truncation = true;
2831 break;
2832 default:
2833 truncation = false;
2834 break;
2835 }
2836
2837 /* setup extra DISPC_WB_ATTRIBUTES */
2838 l = dispc_read_reg(dispc, DISPC_OVL_ATTRIBUTES(plane));
2839 l = FLD_MOD(l, truncation, 10, 10); /* TRUNCATIONENABLE */
2840 l = FLD_MOD(l, channel_in, 18, 16); /* CHANNELIN */
2841 l = FLD_MOD(l, mem_to_mem, 19, 19); /* WRITEBACKMODE */
2842 if (mem_to_mem)
2843 l = FLD_MOD(l, 1, 26, 24); /* CAPTUREMODE */
2844 else
2845 l = FLD_MOD(l, 0, 26, 24); /* CAPTUREMODE */
2846 dispc_write_reg(dispc, DISPC_OVL_ATTRIBUTES(plane), l);
2847
2848 if (mem_to_mem) {
2849 /* WBDELAYCOUNT */
2850 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES2(plane), 0, 7, 0);
2851 } else {
2852 u32 wbdelay;
2853
2854 if (channel_in == DSS_WB_TV_MGR)
2855 wbdelay = vm->vsync_len + vm->vback_porch;
2856 else
2857 wbdelay = vm->vfront_porch + vm->vsync_len +
2858 vm->vback_porch;
2859
2860 if (vm->flags & DISPLAY_FLAGS_INTERLACED)
2861 wbdelay /= 2;
2862
2863 wbdelay = min(wbdelay, 255u);
2864
2865 /* WBDELAYCOUNT */
2866 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES2(plane), wbdelay, 7, 0);
2867 }
2868
2869 return 0;
2870}
2871
2872static bool dispc_has_writeback(struct dispc_device *dispc)
2873{
2874 return dispc->feat->has_writeback;
2875}
2876
2877static int dispc_ovl_enable(struct dispc_device *dispc,
2878 enum omap_plane_id plane, bool enable)
2879{
2880 DSSDBG("dispc_enable_plane %d, %d\n", plane, enable);
2881
2882 REG_FLD_MOD(dispc, DISPC_OVL_ATTRIBUTES(plane), enable ? 1 : 0, 0, 0);
2883
2884 return 0;
2885}
2886
2887static void dispc_lcd_enable_signal_polarity(struct dispc_device *dispc,
2888 bool act_high)
2889{
2890 if (!dispc_has_feature(dispc, FEAT_LCDENABLEPOL))
2891 return;
2892
2893 REG_FLD_MOD(dispc, DISPC_CONTROL, act_high ? 1 : 0, 29, 29);
2894}
2895
2896void dispc_lcd_enable_signal(struct dispc_device *dispc, bool enable)
2897{
2898 if (!dispc_has_feature(dispc, FEAT_LCDENABLESIGNAL))
2899 return;
2900
2901 REG_FLD_MOD(dispc, DISPC_CONTROL, enable ? 1 : 0, 28, 28);
2902}
2903
2904void dispc_pck_free_enable(struct dispc_device *dispc, bool enable)
2905{
2906 if (!dispc_has_feature(dispc, FEAT_PCKFREEENABLE))
2907 return;
2908
2909 REG_FLD_MOD(dispc, DISPC_CONTROL, enable ? 1 : 0, 27, 27);
2910}
2911
2912static void dispc_mgr_enable_fifohandcheck(struct dispc_device *dispc,
2913 enum omap_channel channel,
2914 bool enable)
2915{
2916 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_FIFOHANDCHECK, enable);
2917}
2918
2919
2920static void dispc_mgr_set_lcd_type_tft(struct dispc_device *dispc,
2921 enum omap_channel channel)
2922{
2923 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_STNTFT, 1);
2924}
2925
2926static void dispc_set_loadmode(struct dispc_device *dispc,
2927 enum omap_dss_load_mode mode)
2928{
2929 REG_FLD_MOD(dispc, DISPC_CONFIG, mode, 2, 1);
2930}
2931
2932
2933static void dispc_mgr_set_default_color(struct dispc_device *dispc,
2934 enum omap_channel channel, u32 color)
2935{
2936 dispc_write_reg(dispc, DISPC_DEFAULT_COLOR(channel), color);
2937}
2938
2939static void dispc_mgr_set_trans_key(struct dispc_device *dispc,
2940 enum omap_channel ch,
2941 enum omap_dss_trans_key_type type,
2942 u32 trans_key)
2943{
2944 mgr_fld_write(dispc, ch, DISPC_MGR_FLD_TCKSELECTION, type);
2945
2946 dispc_write_reg(dispc, DISPC_TRANS_COLOR(ch), trans_key);
2947}
2948
2949static void dispc_mgr_enable_trans_key(struct dispc_device *dispc,
2950 enum omap_channel ch, bool enable)
2951{
2952 mgr_fld_write(dispc, ch, DISPC_MGR_FLD_TCKENABLE, enable);
2953}
2954
2955static void dispc_mgr_enable_alpha_fixed_zorder(struct dispc_device *dispc,
2956 enum omap_channel ch,
2957 bool enable)
2958{
2959 if (!dispc_has_feature(dispc, FEAT_ALPHA_FIXED_ZORDER))
2960 return;
2961
2962 if (ch == OMAP_DSS_CHANNEL_LCD)
2963 REG_FLD_MOD(dispc, DISPC_CONFIG, enable, 18, 18);
2964 else if (ch == OMAP_DSS_CHANNEL_DIGIT)
2965 REG_FLD_MOD(dispc, DISPC_CONFIG, enable, 19, 19);
2966}
2967
2968static void dispc_mgr_setup(struct dispc_device *dispc,
2969 enum omap_channel channel,
2970 const struct omap_overlay_manager_info *info)
2971{
2972 dispc_mgr_set_default_color(dispc, channel, info->default_color);
2973 dispc_mgr_set_trans_key(dispc, channel, info->trans_key_type,
2974 info->trans_key);
2975 dispc_mgr_enable_trans_key(dispc, channel, info->trans_enabled);
2976 dispc_mgr_enable_alpha_fixed_zorder(dispc, channel,
2977 info->partial_alpha_enabled);
2978 if (dispc_has_feature(dispc, FEAT_CPR)) {
2979 dispc_mgr_enable_cpr(dispc, channel, info->cpr_enable);
2980 dispc_mgr_set_cpr_coef(dispc, channel, &info->cpr_coefs);
2981 }
2982}
2983
2984static void dispc_mgr_set_tft_data_lines(struct dispc_device *dispc,
2985 enum omap_channel channel,
2986 u8 data_lines)
2987{
2988 int code;
2989
2990 switch (data_lines) {
2991 case 12:
2992 code = 0;
2993 break;
2994 case 16:
2995 code = 1;
2996 break;
2997 case 18:
2998 code = 2;
2999 break;
3000 case 24:
3001 code = 3;
3002 break;
3003 default:
3004 BUG();
3005 return;
3006 }
3007
3008 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_TFTDATALINES, code);
3009}
3010
3011static void dispc_mgr_set_io_pad_mode(struct dispc_device *dispc,
3012 enum dss_io_pad_mode mode)
3013{
3014 u32 l;
3015 int gpout0, gpout1;
3016
3017 switch (mode) {
3018 case DSS_IO_PAD_MODE_RESET:
3019 gpout0 = 0;
3020 gpout1 = 0;
3021 break;
3022 case DSS_IO_PAD_MODE_RFBI:
3023 gpout0 = 1;
3024 gpout1 = 0;
3025 break;
3026 case DSS_IO_PAD_MODE_BYPASS:
3027 gpout0 = 1;
3028 gpout1 = 1;
3029 break;
3030 default:
3031 BUG();
3032 return;
3033 }
3034
3035 l = dispc_read_reg(dispc, DISPC_CONTROL);
3036 l = FLD_MOD(l, gpout0, 15, 15);
3037 l = FLD_MOD(l, gpout1, 16, 16);
3038 dispc_write_reg(dispc, DISPC_CONTROL, l);
3039}
3040
3041static void dispc_mgr_enable_stallmode(struct dispc_device *dispc,
3042 enum omap_channel channel, bool enable)
3043{
3044 mgr_fld_write(dispc, channel, DISPC_MGR_FLD_STALLMODE, enable);
3045}
3046
3047static void dispc_mgr_set_lcd_config(struct dispc_device *dispc,
3048 enum omap_channel channel,
3049 const struct dss_lcd_mgr_config *config)
3050{
3051 dispc_mgr_set_io_pad_mode(dispc, config->io_pad_mode);
3052
3053 dispc_mgr_enable_stallmode(dispc, channel, config->stallmode);
3054 dispc_mgr_enable_fifohandcheck(dispc, channel, config->fifohandcheck);
3055
3056 dispc_mgr_set_clock_div(dispc, channel, &config->clock_info);
3057
3058 dispc_mgr_set_tft_data_lines(dispc, channel, config->video_port_width);
3059
3060 dispc_lcd_enable_signal_polarity(dispc, config->lcden_sig_polarity);
3061
3062 dispc_mgr_set_lcd_type_tft(dispc, channel);
3063}
3064
3065static bool _dispc_mgr_size_ok(struct dispc_device *dispc,
3066 u16 width, u16 height)
3067{
3068 return width <= dispc->feat->mgr_width_max &&
3069 height <= dispc->feat->mgr_height_max;
3070}
3071
3072static bool _dispc_lcd_timings_ok(struct dispc_device *dispc,
3073 int hsync_len, int hfp, int hbp,
3074 int vsw, int vfp, int vbp)
3075{
3076 if (hsync_len < 1 || hsync_len > dispc->feat->sw_max ||
3077 hfp < 1 || hfp > dispc->feat->hp_max ||
3078 hbp < 1 || hbp > dispc->feat->hp_max ||
3079 vsw < 1 || vsw > dispc->feat->sw_max ||
3080 vfp < 0 || vfp > dispc->feat->vp_max ||
3081 vbp < 0 || vbp > dispc->feat->vp_max)
3082 return false;
3083 return true;
3084}
3085
3086static bool _dispc_mgr_pclk_ok(struct dispc_device *dispc,
3087 enum omap_channel channel,
3088 unsigned long pclk)
3089{
3090 if (dss_mgr_is_lcd(channel))
3091 return pclk <= dispc->feat->max_lcd_pclk;
3092 else
3093 return pclk <= dispc->feat->max_tv_pclk;
3094}
3095
3096static int dispc_mgr_check_timings(struct dispc_device *dispc,
3097 enum omap_channel channel,
3098 const struct videomode *vm)
3099{
3100 if (!_dispc_mgr_size_ok(dispc, vm->hactive, vm->vactive))
3101 return MODE_BAD;
3102
3103 if (!_dispc_mgr_pclk_ok(dispc, channel, vm->pixelclock))
3104 return MODE_BAD;
3105
3106 if (dss_mgr_is_lcd(channel)) {
3107 /* TODO: OMAP4+ supports interlace for LCD outputs */
3108 if (vm->flags & DISPLAY_FLAGS_INTERLACED)
3109 return MODE_BAD;
3110
3111 if (!_dispc_lcd_timings_ok(dispc, vm->hsync_len,
3112 vm->hfront_porch, vm->hback_porch,
3113 vm->vsync_len, vm->vfront_porch,
3114 vm->vback_porch))
3115 return MODE_BAD;
3116 }
3117
3118 return MODE_OK;
3119}
3120
3121static void _dispc_mgr_set_lcd_timings(struct dispc_device *dispc,
3122 enum omap_channel channel,
3123 const struct videomode *vm)
3124{
3125 u32 timing_h, timing_v, l;
3126 bool onoff, rf, ipc, vs, hs, de;
3127
3128 timing_h = FLD_VAL(vm->hsync_len - 1, dispc->feat->sw_start, 0) |
3129 FLD_VAL(vm->hfront_porch - 1, dispc->feat->fp_start, 8) |
3130 FLD_VAL(vm->hback_porch - 1, dispc->feat->bp_start, 20);
3131 timing_v = FLD_VAL(vm->vsync_len - 1, dispc->feat->sw_start, 0) |
3132 FLD_VAL(vm->vfront_porch, dispc->feat->fp_start, 8) |
3133 FLD_VAL(vm->vback_porch, dispc->feat->bp_start, 20);
3134
3135 dispc_write_reg(dispc, DISPC_TIMING_H(channel), timing_h);
3136 dispc_write_reg(dispc, DISPC_TIMING_V(channel), timing_v);
3137
3138 if (vm->flags & DISPLAY_FLAGS_VSYNC_HIGH)
3139 vs = false;
3140 else
3141 vs = true;
3142
3143 if (vm->flags & DISPLAY_FLAGS_HSYNC_HIGH)
3144 hs = false;
3145 else
3146 hs = true;
3147
3148 if (vm->flags & DISPLAY_FLAGS_DE_HIGH)
3149 de = false;
3150 else
3151 de = true;
3152
3153 if (vm->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)
3154 ipc = false;
3155 else
3156 ipc = true;
3157
3158 /* always use the 'rf' setting */
3159 onoff = true;
3160
3161 if (vm->flags & DISPLAY_FLAGS_SYNC_POSEDGE)
3162 rf = true;
3163 else
3164 rf = false;
3165
3166 l = FLD_VAL(onoff, 17, 17) |
3167 FLD_VAL(rf, 16, 16) |
3168 FLD_VAL(de, 15, 15) |
3169 FLD_VAL(ipc, 14, 14) |
3170 FLD_VAL(hs, 13, 13) |
3171 FLD_VAL(vs, 12, 12);
3172
3173 /* always set ALIGN bit when available */
3174 if (dispc->feat->supports_sync_align)
3175 l |= (1 << 18);
3176
3177 dispc_write_reg(dispc, DISPC_POL_FREQ(channel), l);
3178
3179 if (dispc->syscon_pol) {
3180 const int shifts[] = {
3181 [OMAP_DSS_CHANNEL_LCD] = 0,
3182 [OMAP_DSS_CHANNEL_LCD2] = 1,
3183 [OMAP_DSS_CHANNEL_LCD3] = 2,
3184 };
3185
3186 u32 mask, val;
3187
3188 mask = (1 << 0) | (1 << 3) | (1 << 6);
3189 val = (rf << 0) | (ipc << 3) | (onoff << 6);
3190
3191 mask <<= 16 + shifts[channel];
3192 val <<= 16 + shifts[channel];
3193
3194 regmap_update_bits(dispc->syscon_pol, dispc->syscon_pol_offset,
3195 mask, val);
3196 }
3197}
3198
3199static int vm_flag_to_int(enum display_flags flags, enum display_flags high,
3200 enum display_flags low)
3201{
3202 if (flags & high)
3203 return 1;
3204 if (flags & low)
3205 return -1;
3206 return 0;
3207}
3208
3209/* change name to mode? */
3210static void dispc_mgr_set_timings(struct dispc_device *dispc,
3211 enum omap_channel channel,
3212 const struct videomode *vm)
3213{
3214 unsigned int xtot, ytot;
3215 unsigned long ht, vt;
3216 struct videomode t = *vm;
3217
3218 DSSDBG("channel %d xres %u yres %u\n", channel, t.hactive, t.vactive);
3219
3220 if (dispc_mgr_check_timings(dispc, channel, &t)) {
3221 BUG();
3222 return;
3223 }
3224
3225 if (dss_mgr_is_lcd(channel)) {
3226 _dispc_mgr_set_lcd_timings(dispc, channel, &t);
3227
3228 xtot = t.hactive + t.hfront_porch + t.hsync_len + t.hback_porch;
3229 ytot = t.vactive + t.vfront_porch + t.vsync_len + t.vback_porch;
3230
3231 ht = vm->pixelclock / xtot;
3232 vt = vm->pixelclock / xtot / ytot;
3233
3234 DSSDBG("pck %lu\n", vm->pixelclock);
3235 DSSDBG("hsync_len %d hfp %d hbp %d vsw %d vfp %d vbp %d\n",
3236 t.hsync_len, t.hfront_porch, t.hback_porch,
3237 t.vsync_len, t.vfront_porch, t.vback_porch);
3238 DSSDBG("vsync_level %d hsync_level %d data_pclk_edge %d de_level %d sync_pclk_edge %d\n",
3239 vm_flag_to_int(t.flags, DISPLAY_FLAGS_VSYNC_HIGH, DISPLAY_FLAGS_VSYNC_LOW),
3240 vm_flag_to_int(t.flags, DISPLAY_FLAGS_HSYNC_HIGH, DISPLAY_FLAGS_HSYNC_LOW),
3241 vm_flag_to_int(t.flags, DISPLAY_FLAGS_PIXDATA_POSEDGE, DISPLAY_FLAGS_PIXDATA_NEGEDGE),
3242 vm_flag_to_int(t.flags, DISPLAY_FLAGS_DE_HIGH, DISPLAY_FLAGS_DE_LOW),
3243 vm_flag_to_int(t.flags, DISPLAY_FLAGS_SYNC_POSEDGE, DISPLAY_FLAGS_SYNC_NEGEDGE));
3244
3245 DSSDBG("hsync %luHz, vsync %luHz\n", ht, vt);
3246 } else {
3247 if (t.flags & DISPLAY_FLAGS_INTERLACED)
3248 t.vactive /= 2;
3249
3250 if (dispc->feat->supports_double_pixel)
3251 REG_FLD_MOD(dispc, DISPC_CONTROL,
3252 !!(t.flags & DISPLAY_FLAGS_DOUBLECLK),
3253 19, 17);
3254 }
3255
3256 dispc_mgr_set_size(dispc, channel, t.hactive, t.vactive);
3257}
3258
3259static void dispc_mgr_set_lcd_divisor(struct dispc_device *dispc,
3260 enum omap_channel channel, u16 lck_div,
3261 u16 pck_div)
3262{
3263 BUG_ON(lck_div < 1);
3264 BUG_ON(pck_div < 1);
3265
3266 dispc_write_reg(dispc, DISPC_DIVISORo(channel),
3267 FLD_VAL(lck_div, 23, 16) | FLD_VAL(pck_div, 7, 0));
3268
3269 if (!dispc_has_feature(dispc, FEAT_CORE_CLK_DIV) &&
3270 channel == OMAP_DSS_CHANNEL_LCD)
3271 dispc->core_clk_rate = dispc_fclk_rate(dispc) / lck_div;
3272}
3273
3274static void dispc_mgr_get_lcd_divisor(struct dispc_device *dispc,
3275 enum omap_channel channel, int *lck_div,
3276 int *pck_div)
3277{
3278 u32 l;
3279 l = dispc_read_reg(dispc, DISPC_DIVISORo(channel));
3280 *lck_div = FLD_GET(l, 23, 16);
3281 *pck_div = FLD_GET(l, 7, 0);
3282}
3283
3284static unsigned long dispc_fclk_rate(struct dispc_device *dispc)
3285{
3286 unsigned long r;
3287 enum dss_clk_source src;
3288
3289 src = dss_get_dispc_clk_source(dispc->dss);
3290
3291 if (src == DSS_CLK_SRC_FCK) {
3292 r = dss_get_dispc_clk_rate(dispc->dss);
3293 } else {
3294 struct dss_pll *pll;
3295 unsigned int clkout_idx;
3296
3297 pll = dss_pll_find_by_src(dispc->dss, src);
3298 clkout_idx = dss_pll_get_clkout_idx_for_src(src);
3299
3300 r = pll->cinfo.clkout[clkout_idx];
3301 }
3302
3303 return r;
3304}
3305
3306static unsigned long dispc_mgr_lclk_rate(struct dispc_device *dispc,
3307 enum omap_channel channel)
3308{
3309 int lcd;
3310 unsigned long r;
3311 enum dss_clk_source src;
3312
3313 /* for TV, LCLK rate is the FCLK rate */
3314 if (!dss_mgr_is_lcd(channel))
3315 return dispc_fclk_rate(dispc);
3316
3317 src = dss_get_lcd_clk_source(dispc->dss, channel);
3318
3319 if (src == DSS_CLK_SRC_FCK) {
3320 r = dss_get_dispc_clk_rate(dispc->dss);
3321 } else {
3322 struct dss_pll *pll;
3323 unsigned int clkout_idx;
3324
3325 pll = dss_pll_find_by_src(dispc->dss, src);
3326 clkout_idx = dss_pll_get_clkout_idx_for_src(src);
3327
3328 r = pll->cinfo.clkout[clkout_idx];
3329 }
3330
3331 lcd = REG_GET(dispc, DISPC_DIVISORo(channel), 23, 16);
3332
3333 return r / lcd;
3334}
3335
3336static unsigned long dispc_mgr_pclk_rate(struct dispc_device *dispc,
3337 enum omap_channel channel)
3338{
3339 unsigned long r;
3340
3341 if (dss_mgr_is_lcd(channel)) {
3342 int pcd;
3343 u32 l;
3344
3345 l = dispc_read_reg(dispc, DISPC_DIVISORo(channel));
3346
3347 pcd = FLD_GET(l, 7, 0);
3348
3349 r = dispc_mgr_lclk_rate(dispc, channel);
3350
3351 return r / pcd;
3352 } else {
3353 return dispc->tv_pclk_rate;
3354 }
3355}
3356
3357void dispc_set_tv_pclk(struct dispc_device *dispc, unsigned long pclk)
3358{
3359 dispc->tv_pclk_rate = pclk;
3360}
3361
3362static unsigned long dispc_core_clk_rate(struct dispc_device *dispc)
3363{
3364 return dispc->core_clk_rate;
3365}
3366
3367static unsigned long dispc_plane_pclk_rate(struct dispc_device *dispc,
3368 enum omap_plane_id plane)
3369{
3370 enum omap_channel channel;
3371
3372 if (plane == OMAP_DSS_WB)
3373 return 0;
3374
3375 channel = dispc_ovl_get_channel_out(dispc, plane);
3376
3377 return dispc_mgr_pclk_rate(dispc, channel);
3378}
3379
3380static unsigned long dispc_plane_lclk_rate(struct dispc_device *dispc,
3381 enum omap_plane_id plane)
3382{
3383 enum omap_channel channel;
3384
3385 if (plane == OMAP_DSS_WB)
3386 return 0;
3387
3388 channel = dispc_ovl_get_channel_out(dispc, plane);
3389
3390 return dispc_mgr_lclk_rate(dispc, channel);
3391}
3392
3393static void dispc_dump_clocks_channel(struct dispc_device *dispc,
3394 struct seq_file *s,
3395 enum omap_channel channel)
3396{
3397 int lcd, pcd;
3398 enum dss_clk_source lcd_clk_src;
3399
3400 seq_printf(s, "- %s -\n", mgr_desc[channel].name);
3401
3402 lcd_clk_src = dss_get_lcd_clk_source(dispc->dss, channel);
3403
3404 seq_printf(s, "%s clk source = %s\n", mgr_desc[channel].name,
3405 dss_get_clk_source_name(lcd_clk_src));
3406
3407 dispc_mgr_get_lcd_divisor(dispc, channel, &lcd, &pcd);
3408
3409 seq_printf(s, "lck\t\t%-16lulck div\t%u\n",
3410 dispc_mgr_lclk_rate(dispc, channel), lcd);
3411 seq_printf(s, "pck\t\t%-16lupck div\t%u\n",
3412 dispc_mgr_pclk_rate(dispc, channel), pcd);
3413}
3414
3415void dispc_dump_clocks(struct dispc_device *dispc, struct seq_file *s)
3416{
3417 enum dss_clk_source dispc_clk_src;
3418 int lcd;
3419 u32 l;
3420
3421 if (dispc_runtime_get(dispc))
3422 return;
3423
3424 seq_printf(s, "- DISPC -\n");
3425
3426 dispc_clk_src = dss_get_dispc_clk_source(dispc->dss);
3427 seq_printf(s, "dispc fclk source = %s\n",
3428 dss_get_clk_source_name(dispc_clk_src));
3429
3430 seq_printf(s, "fck\t\t%-16lu\n", dispc_fclk_rate(dispc));
3431
3432 if (dispc_has_feature(dispc, FEAT_CORE_CLK_DIV)) {
3433 seq_printf(s, "- DISPC-CORE-CLK -\n");
3434 l = dispc_read_reg(dispc, DISPC_DIVISOR);
3435 lcd = FLD_GET(l, 23, 16);
3436
3437 seq_printf(s, "lck\t\t%-16lulck div\t%u\n",
3438 (dispc_fclk_rate(dispc)/lcd), lcd);
3439 }
3440
3441 dispc_dump_clocks_channel(dispc, s, OMAP_DSS_CHANNEL_LCD);
3442
3443 if (dispc_has_feature(dispc, FEAT_MGR_LCD2))
3444 dispc_dump_clocks_channel(dispc, s, OMAP_DSS_CHANNEL_LCD2);
3445 if (dispc_has_feature(dispc, FEAT_MGR_LCD3))
3446 dispc_dump_clocks_channel(dispc, s, OMAP_DSS_CHANNEL_LCD3);
3447
3448 dispc_runtime_put(dispc);
3449}
3450
3451static int dispc_dump_regs(struct seq_file *s, void *p)
3452{
3453 struct dispc_device *dispc = s->private;
3454 int i, j;
3455 const char *mgr_names[] = {
3456 [OMAP_DSS_CHANNEL_LCD] = "LCD",
3457 [OMAP_DSS_CHANNEL_DIGIT] = "TV",
3458 [OMAP_DSS_CHANNEL_LCD2] = "LCD2",
3459 [OMAP_DSS_CHANNEL_LCD3] = "LCD3",
3460 };
3461 const char *ovl_names[] = {
3462 [OMAP_DSS_GFX] = "GFX",
3463 [OMAP_DSS_VIDEO1] = "VID1",
3464 [OMAP_DSS_VIDEO2] = "VID2",
3465 [OMAP_DSS_VIDEO3] = "VID3",
3466 [OMAP_DSS_WB] = "WB",
3467 };
3468 const char **p_names;
3469
3470#define DUMPREG(dispc, r) \
3471 seq_printf(s, "%-50s %08x\n", #r, dispc_read_reg(dispc, r))
3472
3473 if (dispc_runtime_get(dispc))
3474 return 0;
3475
3476 /* DISPC common registers */
3477 DUMPREG(dispc, DISPC_REVISION);
3478 DUMPREG(dispc, DISPC_SYSCONFIG);
3479 DUMPREG(dispc, DISPC_SYSSTATUS);
3480 DUMPREG(dispc, DISPC_IRQSTATUS);
3481 DUMPREG(dispc, DISPC_IRQENABLE);
3482 DUMPREG(dispc, DISPC_CONTROL);
3483 DUMPREG(dispc, DISPC_CONFIG);
3484 DUMPREG(dispc, DISPC_CAPABLE);
3485 DUMPREG(dispc, DISPC_LINE_STATUS);
3486 DUMPREG(dispc, DISPC_LINE_NUMBER);
3487 if (dispc_has_feature(dispc, FEAT_ALPHA_FIXED_ZORDER) ||
3488 dispc_has_feature(dispc, FEAT_ALPHA_FREE_ZORDER))
3489 DUMPREG(dispc, DISPC_GLOBAL_ALPHA);
3490 if (dispc_has_feature(dispc, FEAT_MGR_LCD2)) {
3491 DUMPREG(dispc, DISPC_CONTROL2);
3492 DUMPREG(dispc, DISPC_CONFIG2);
3493 }
3494 if (dispc_has_feature(dispc, FEAT_MGR_LCD3)) {
3495 DUMPREG(dispc, DISPC_CONTROL3);
3496 DUMPREG(dispc, DISPC_CONFIG3);
3497 }
3498 if (dispc_has_feature(dispc, FEAT_MFLAG))
3499 DUMPREG(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE);
3500
3501#undef DUMPREG
3502
3503#define DISPC_REG(i, name) name(i)
3504#define DUMPREG(dispc, i, r) seq_printf(s, "%s(%s)%*s %08x\n", #r, p_names[i], \
3505 (int)(48 - strlen(#r) - strlen(p_names[i])), " ", \
3506 dispc_read_reg(dispc, DISPC_REG(i, r)))
3507
3508 p_names = mgr_names;
3509
3510 /* DISPC channel specific registers */
3511 for (i = 0; i < dispc_get_num_mgrs(dispc); i++) {
3512 DUMPREG(dispc, i, DISPC_DEFAULT_COLOR);
3513 DUMPREG(dispc, i, DISPC_TRANS_COLOR);
3514 DUMPREG(dispc, i, DISPC_SIZE_MGR);
3515
3516 if (i == OMAP_DSS_CHANNEL_DIGIT)
3517 continue;
3518
3519 DUMPREG(dispc, i, DISPC_TIMING_H);
3520 DUMPREG(dispc, i, DISPC_TIMING_V);
3521 DUMPREG(dispc, i, DISPC_POL_FREQ);
3522 DUMPREG(dispc, i, DISPC_DIVISORo);
3523
3524 DUMPREG(dispc, i, DISPC_DATA_CYCLE1);
3525 DUMPREG(dispc, i, DISPC_DATA_CYCLE2);
3526 DUMPREG(dispc, i, DISPC_DATA_CYCLE3);
3527
3528 if (dispc_has_feature(dispc, FEAT_CPR)) {
3529 DUMPREG(dispc, i, DISPC_CPR_COEF_R);
3530 DUMPREG(dispc, i, DISPC_CPR_COEF_G);
3531 DUMPREG(dispc, i, DISPC_CPR_COEF_B);
3532 }
3533 }
3534
3535 p_names = ovl_names;
3536
3537 for (i = 0; i < dispc_get_num_ovls(dispc); i++) {
3538 DUMPREG(dispc, i, DISPC_OVL_BA0);
3539 DUMPREG(dispc, i, DISPC_OVL_BA1);
3540 DUMPREG(dispc, i, DISPC_OVL_POSITION);
3541 DUMPREG(dispc, i, DISPC_OVL_SIZE);
3542 DUMPREG(dispc, i, DISPC_OVL_ATTRIBUTES);
3543 DUMPREG(dispc, i, DISPC_OVL_FIFO_THRESHOLD);
3544 DUMPREG(dispc, i, DISPC_OVL_FIFO_SIZE_STATUS);
3545 DUMPREG(dispc, i, DISPC_OVL_ROW_INC);
3546 DUMPREG(dispc, i, DISPC_OVL_PIXEL_INC);
3547
3548 if (dispc_has_feature(dispc, FEAT_PRELOAD))
3549 DUMPREG(dispc, i, DISPC_OVL_PRELOAD);
3550 if (dispc_has_feature(dispc, FEAT_MFLAG))
3551 DUMPREG(dispc, i, DISPC_OVL_MFLAG_THRESHOLD);
3552
3553 if (i == OMAP_DSS_GFX) {
3554 DUMPREG(dispc, i, DISPC_OVL_WINDOW_SKIP);
3555 DUMPREG(dispc, i, DISPC_OVL_TABLE_BA);
3556 continue;
3557 }
3558
3559 DUMPREG(dispc, i, DISPC_OVL_FIR);
3560 DUMPREG(dispc, i, DISPC_OVL_PICTURE_SIZE);
3561 DUMPREG(dispc, i, DISPC_OVL_ACCU0);
3562 DUMPREG(dispc, i, DISPC_OVL_ACCU1);
3563 if (dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE)) {
3564 DUMPREG(dispc, i, DISPC_OVL_BA0_UV);
3565 DUMPREG(dispc, i, DISPC_OVL_BA1_UV);
3566 DUMPREG(dispc, i, DISPC_OVL_FIR2);
3567 DUMPREG(dispc, i, DISPC_OVL_ACCU2_0);
3568 DUMPREG(dispc, i, DISPC_OVL_ACCU2_1);
3569 }
3570 if (dispc_has_feature(dispc, FEAT_ATTR2))
3571 DUMPREG(dispc, i, DISPC_OVL_ATTRIBUTES2);
3572 }
3573
3574 if (dispc->feat->has_writeback) {
3575 i = OMAP_DSS_WB;
3576 DUMPREG(dispc, i, DISPC_OVL_BA0);
3577 DUMPREG(dispc, i, DISPC_OVL_BA1);
3578 DUMPREG(dispc, i, DISPC_OVL_SIZE);
3579 DUMPREG(dispc, i, DISPC_OVL_ATTRIBUTES);
3580 DUMPREG(dispc, i, DISPC_OVL_FIFO_THRESHOLD);
3581 DUMPREG(dispc, i, DISPC_OVL_FIFO_SIZE_STATUS);
3582 DUMPREG(dispc, i, DISPC_OVL_ROW_INC);
3583 DUMPREG(dispc, i, DISPC_OVL_PIXEL_INC);
3584
3585 if (dispc_has_feature(dispc, FEAT_MFLAG))
3586 DUMPREG(dispc, i, DISPC_OVL_MFLAG_THRESHOLD);
3587
3588 DUMPREG(dispc, i, DISPC_OVL_FIR);
3589 DUMPREG(dispc, i, DISPC_OVL_PICTURE_SIZE);
3590 DUMPREG(dispc, i, DISPC_OVL_ACCU0);
3591 DUMPREG(dispc, i, DISPC_OVL_ACCU1);
3592 if (dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE)) {
3593 DUMPREG(dispc, i, DISPC_OVL_BA0_UV);
3594 DUMPREG(dispc, i, DISPC_OVL_BA1_UV);
3595 DUMPREG(dispc, i, DISPC_OVL_FIR2);
3596 DUMPREG(dispc, i, DISPC_OVL_ACCU2_0);
3597 DUMPREG(dispc, i, DISPC_OVL_ACCU2_1);
3598 }
3599 if (dispc_has_feature(dispc, FEAT_ATTR2))
3600 DUMPREG(dispc, i, DISPC_OVL_ATTRIBUTES2);
3601 }
3602
3603#undef DISPC_REG
3604#undef DUMPREG
3605
3606#define DISPC_REG(plane, name, i) name(plane, i)
3607#define DUMPREG(dispc, plane, name, i) \
3608 seq_printf(s, "%s_%d(%s)%*s %08x\n", #name, i, p_names[plane], \
3609 (int)(46 - strlen(#name) - strlen(p_names[plane])), " ", \
3610 dispc_read_reg(dispc, DISPC_REG(plane, name, i)))
3611
3612 /* Video pipeline coefficient registers */
3613
3614 /* start from OMAP_DSS_VIDEO1 */
3615 for (i = 1; i < dispc_get_num_ovls(dispc); i++) {
3616 for (j = 0; j < 8; j++)
3617 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_H, j);
3618
3619 for (j = 0; j < 8; j++)
3620 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_HV, j);
3621
3622 for (j = 0; j < 5; j++)
3623 DUMPREG(dispc, i, DISPC_OVL_CONV_COEF, j);
3624
3625 if (dispc_has_feature(dispc, FEAT_FIR_COEF_V)) {
3626 for (j = 0; j < 8; j++)
3627 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_V, j);
3628 }
3629
3630 if (dispc_has_feature(dispc, FEAT_HANDLE_UV_SEPARATE)) {
3631 for (j = 0; j < 8; j++)
3632 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_H2, j);
3633
3634 for (j = 0; j < 8; j++)
3635 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_HV2, j);
3636
3637 for (j = 0; j < 8; j++)
3638 DUMPREG(dispc, i, DISPC_OVL_FIR_COEF_V2, j);
3639 }
3640 }
3641
3642 dispc_runtime_put(dispc);
3643
3644#undef DISPC_REG
3645#undef DUMPREG
3646
3647 return 0;
3648}
3649
3650/* calculate clock rates using dividers in cinfo */
3651int dispc_calc_clock_rates(struct dispc_device *dispc,
3652 unsigned long dispc_fclk_rate,
3653 struct dispc_clock_info *cinfo)
3654{
3655 if (cinfo->lck_div > 255 || cinfo->lck_div == 0)
3656 return -EINVAL;
3657 if (cinfo->pck_div < 1 || cinfo->pck_div > 255)
3658 return -EINVAL;
3659
3660 cinfo->lck = dispc_fclk_rate / cinfo->lck_div;
3661 cinfo->pck = cinfo->lck / cinfo->pck_div;
3662
3663 return 0;
3664}
3665
3666bool dispc_div_calc(struct dispc_device *dispc, unsigned long dispc_freq,
3667 unsigned long pck_min, unsigned long pck_max,
3668 dispc_div_calc_func func, void *data)
3669{
3670 int lckd, lckd_start, lckd_stop;
3671 int pckd, pckd_start, pckd_stop;
3672 unsigned long pck, lck;
3673 unsigned long lck_max;
3674 unsigned long pckd_hw_min, pckd_hw_max;
3675 unsigned int min_fck_per_pck;
3676 unsigned long fck;
3677
3678#ifdef CONFIG_OMAP2_DSS_MIN_FCK_PER_PCK
3679 min_fck_per_pck = CONFIG_OMAP2_DSS_MIN_FCK_PER_PCK;
3680#else
3681 min_fck_per_pck = 0;
3682#endif
3683
3684 pckd_hw_min = dispc->feat->min_pcd;
3685 pckd_hw_max = 255;
3686
3687 lck_max = dss_get_max_fck_rate(dispc->dss);
3688
3689 pck_min = pck_min ? pck_min : 1;
3690 pck_max = pck_max ? pck_max : ULONG_MAX;
3691
3692 lckd_start = max(DIV_ROUND_UP(dispc_freq, lck_max), 1ul);
3693 lckd_stop = min(dispc_freq / pck_min, 255ul);
3694
3695 for (lckd = lckd_start; lckd <= lckd_stop; ++lckd) {
3696 lck = dispc_freq / lckd;
3697
3698 pckd_start = max(DIV_ROUND_UP(lck, pck_max), pckd_hw_min);
3699 pckd_stop = min(lck / pck_min, pckd_hw_max);
3700
3701 for (pckd = pckd_start; pckd <= pckd_stop; ++pckd) {
3702 pck = lck / pckd;
3703
3704 /*
3705 * For OMAP2/3 the DISPC fclk is the same as LCD's logic
3706 * clock, which means we're configuring DISPC fclk here
3707 * also. Thus we need to use the calculated lck. For
3708 * OMAP4+ the DISPC fclk is a separate clock.
3709 */
3710 if (dispc_has_feature(dispc, FEAT_CORE_CLK_DIV))
3711 fck = dispc_core_clk_rate(dispc);
3712 else
3713 fck = lck;
3714
3715 if (fck < pck * min_fck_per_pck)
3716 continue;
3717
3718 if (func(lckd, pckd, lck, pck, data))
3719 return true;
3720 }
3721 }
3722
3723 return false;
3724}
3725
3726void dispc_mgr_set_clock_div(struct dispc_device *dispc,
3727 enum omap_channel channel,
3728 const struct dispc_clock_info *cinfo)
3729{
3730 DSSDBG("lck = %lu (%u)\n", cinfo->lck, cinfo->lck_div);
3731 DSSDBG("pck = %lu (%u)\n", cinfo->pck, cinfo->pck_div);
3732
3733 dispc_mgr_set_lcd_divisor(dispc, channel, cinfo->lck_div,
3734 cinfo->pck_div);
3735}
3736
3737int dispc_mgr_get_clock_div(struct dispc_device *dispc,
3738 enum omap_channel channel,
3739 struct dispc_clock_info *cinfo)
3740{
3741 unsigned long fck;
3742
3743 fck = dispc_fclk_rate(dispc);
3744
3745 cinfo->lck_div = REG_GET(dispc, DISPC_DIVISORo(channel), 23, 16);
3746 cinfo->pck_div = REG_GET(dispc, DISPC_DIVISORo(channel), 7, 0);
3747
3748 cinfo->lck = fck / cinfo->lck_div;
3749 cinfo->pck = cinfo->lck / cinfo->pck_div;
3750
3751 return 0;
3752}
3753
3754static u32 dispc_read_irqstatus(struct dispc_device *dispc)
3755{
3756 return dispc_read_reg(dispc, DISPC_IRQSTATUS);
3757}
3758
3759static void dispc_clear_irqstatus(struct dispc_device *dispc, u32 mask)
3760{
3761 dispc_write_reg(dispc, DISPC_IRQSTATUS, mask);
3762}
3763
3764static void dispc_write_irqenable(struct dispc_device *dispc, u32 mask)
3765{
3766 u32 old_mask = dispc_read_reg(dispc, DISPC_IRQENABLE);
3767
3768 /* clear the irqstatus for newly enabled irqs */
3769 dispc_clear_irqstatus(dispc, (mask ^ old_mask) & mask);
3770
3771 dispc_write_reg(dispc, DISPC_IRQENABLE, mask);
3772
3773 /* flush posted write */
3774 dispc_read_reg(dispc, DISPC_IRQENABLE);
3775}
3776
3777void dispc_enable_sidle(struct dispc_device *dispc)
3778{
3779 /* SIDLEMODE: smart idle */
3780 REG_FLD_MOD(dispc, DISPC_SYSCONFIG, 2, 4, 3);
3781}
3782
3783void dispc_disable_sidle(struct dispc_device *dispc)
3784{
3785 REG_FLD_MOD(dispc, DISPC_SYSCONFIG, 1, 4, 3); /* SIDLEMODE: no idle */
3786}
3787
3788static u32 dispc_mgr_gamma_size(struct dispc_device *dispc,
3789 enum omap_channel channel)
3790{
3791 const struct dispc_gamma_desc *gdesc = &mgr_desc[channel].gamma;
3792
3793 if (!dispc->feat->has_gamma_table)
3794 return 0;
3795
3796 return gdesc->len;
3797}
3798
3799static void dispc_mgr_write_gamma_table(struct dispc_device *dispc,
3800 enum omap_channel channel)
3801{
3802 const struct dispc_gamma_desc *gdesc = &mgr_desc[channel].gamma;
3803 u32 *table = dispc->gamma_table[channel];
3804 unsigned int i;
3805
3806 DSSDBG("%s: channel %d\n", __func__, channel);
3807
3808 for (i = 0; i < gdesc->len; ++i) {
3809 u32 v = table[i];
3810
3811 if (gdesc->has_index)
3812 v |= i << 24;
3813 else if (i == 0)
3814 v |= 1 << 31;
3815
3816 dispc_write_reg(dispc, gdesc->reg, v);
3817 }
3818}
3819
3820static void dispc_restore_gamma_tables(struct dispc_device *dispc)
3821{
3822 DSSDBG("%s()\n", __func__);
3823
3824 if (!dispc->feat->has_gamma_table)
3825 return;
3826
3827 dispc_mgr_write_gamma_table(dispc, OMAP_DSS_CHANNEL_LCD);
3828
3829 dispc_mgr_write_gamma_table(dispc, OMAP_DSS_CHANNEL_DIGIT);
3830
3831 if (dispc_has_feature(dispc, FEAT_MGR_LCD2))
3832 dispc_mgr_write_gamma_table(dispc, OMAP_DSS_CHANNEL_LCD2);
3833
3834 if (dispc_has_feature(dispc, FEAT_MGR_LCD3))
3835 dispc_mgr_write_gamma_table(dispc, OMAP_DSS_CHANNEL_LCD3);
3836}
3837
3838static const struct drm_color_lut dispc_mgr_gamma_default_lut[] = {
3839 { .red = 0, .green = 0, .blue = 0, },
3840 { .red = U16_MAX, .green = U16_MAX, .blue = U16_MAX, },
3841};
3842
3843static void dispc_mgr_set_gamma(struct dispc_device *dispc,
3844 enum omap_channel channel,
3845 const struct drm_color_lut *lut,
3846 unsigned int length)
3847{
3848 const struct dispc_gamma_desc *gdesc = &mgr_desc[channel].gamma;
3849 u32 *table = dispc->gamma_table[channel];
3850 uint i;
3851
3852 DSSDBG("%s: channel %d, lut len %u, hw len %u\n", __func__,
3853 channel, length, gdesc->len);
3854
3855 if (!dispc->feat->has_gamma_table)
3856 return;
3857
3858 if (lut == NULL || length < 2) {
3859 lut = dispc_mgr_gamma_default_lut;
3860 length = ARRAY_SIZE(dispc_mgr_gamma_default_lut);
3861 }
3862
3863 for (i = 0; i < length - 1; ++i) {
3864 uint first = i * (gdesc->len - 1) / (length - 1);
3865 uint last = (i + 1) * (gdesc->len - 1) / (length - 1);
3866 uint w = last - first;
3867 u16 r, g, b;
3868 uint j;
3869
3870 if (w == 0)
3871 continue;
3872
3873 for (j = 0; j <= w; j++) {
3874 r = (lut[i].red * (w - j) + lut[i+1].red * j) / w;
3875 g = (lut[i].green * (w - j) + lut[i+1].green * j) / w;
3876 b = (lut[i].blue * (w - j) + lut[i+1].blue * j) / w;
3877
3878 r >>= 16 - gdesc->bits;
3879 g >>= 16 - gdesc->bits;
3880 b >>= 16 - gdesc->bits;
3881
3882 table[first + j] = (r << (gdesc->bits * 2)) |
3883 (g << gdesc->bits) | b;
3884 }
3885 }
3886
3887 if (dispc->is_enabled)
3888 dispc_mgr_write_gamma_table(dispc, channel);
3889}
3890
3891static int dispc_init_gamma_tables(struct dispc_device *dispc)
3892{
3893 int channel;
3894
3895 if (!dispc->feat->has_gamma_table)
3896 return 0;
3897
3898 for (channel = 0; channel < ARRAY_SIZE(dispc->gamma_table); channel++) {
3899 const struct dispc_gamma_desc *gdesc = &mgr_desc[channel].gamma;
3900 u32 *gt;
3901
3902 if (channel == OMAP_DSS_CHANNEL_LCD2 &&
3903 !dispc_has_feature(dispc, FEAT_MGR_LCD2))
3904 continue;
3905
3906 if (channel == OMAP_DSS_CHANNEL_LCD3 &&
3907 !dispc_has_feature(dispc, FEAT_MGR_LCD3))
3908 continue;
3909
3910 gt = devm_kmalloc_array(&dispc->pdev->dev, gdesc->len,
3911 sizeof(u32), GFP_KERNEL);
3912 if (!gt)
3913 return -ENOMEM;
3914
3915 dispc->gamma_table[channel] = gt;
3916
3917 dispc_mgr_set_gamma(dispc, channel, NULL, 0);
3918 }
3919 return 0;
3920}
3921
3922static void _omap_dispc_initial_config(struct dispc_device *dispc)
3923{
3924 u32 l;
3925
3926 /* Exclusively enable DISPC_CORE_CLK and set divider to 1 */
3927 if (dispc_has_feature(dispc, FEAT_CORE_CLK_DIV)) {
3928 l = dispc_read_reg(dispc, DISPC_DIVISOR);
3929 /* Use DISPC_DIVISOR.LCD, instead of DISPC_DIVISOR1.LCD */
3930 l = FLD_MOD(l, 1, 0, 0);
3931 l = FLD_MOD(l, 1, 23, 16);
3932 dispc_write_reg(dispc, DISPC_DIVISOR, l);
3933
3934 dispc->core_clk_rate = dispc_fclk_rate(dispc);
3935 }
3936
3937 /* Use gamma table mode, instead of palette mode */
3938 if (dispc->feat->has_gamma_table)
3939 REG_FLD_MOD(dispc, DISPC_CONFIG, 1, 3, 3);
3940
3941 /* For older DSS versions (FEAT_FUNCGATED) this enables
3942 * func-clock auto-gating. For newer versions
3943 * (dispc->feat->has_gamma_table) this enables tv-out gamma tables.
3944 */
3945 if (dispc_has_feature(dispc, FEAT_FUNCGATED) ||
3946 dispc->feat->has_gamma_table)
3947 REG_FLD_MOD(dispc, DISPC_CONFIG, 1, 9, 9);
3948
3949 dispc_setup_color_conv_coef(dispc);
3950
3951 dispc_set_loadmode(dispc, OMAP_DSS_LOAD_FRAME_ONLY);
3952
3953 dispc_init_fifos(dispc);
3954
3955 dispc_configure_burst_sizes(dispc);
3956
3957 dispc_ovl_enable_zorder_planes(dispc);
3958
3959 if (dispc->feat->mstandby_workaround)
3960 REG_FLD_MOD(dispc, DISPC_MSTANDBY_CTRL, 1, 0, 0);
3961
3962 if (dispc_has_feature(dispc, FEAT_MFLAG))
3963 dispc_init_mflag(dispc);
3964}
3965
3966static const enum dispc_feature_id omap2_dispc_features_list[] = {
3967 FEAT_LCDENABLEPOL,
3968 FEAT_LCDENABLESIGNAL,
3969 FEAT_PCKFREEENABLE,
3970 FEAT_FUNCGATED,
3971 FEAT_ROWREPEATENABLE,
3972 FEAT_RESIZECONF,
3973};
3974
3975static const enum dispc_feature_id omap3_dispc_features_list[] = {
3976 FEAT_LCDENABLEPOL,
3977 FEAT_LCDENABLESIGNAL,
3978 FEAT_PCKFREEENABLE,
3979 FEAT_FUNCGATED,
3980 FEAT_LINEBUFFERSPLIT,
3981 FEAT_ROWREPEATENABLE,
3982 FEAT_RESIZECONF,
3983 FEAT_CPR,
3984 FEAT_PRELOAD,
3985 FEAT_FIR_COEF_V,
3986 FEAT_ALPHA_FIXED_ZORDER,
3987 FEAT_FIFO_MERGE,
3988 FEAT_OMAP3_DSI_FIFO_BUG,
3989};
3990
3991static const enum dispc_feature_id am43xx_dispc_features_list[] = {
3992 FEAT_LCDENABLEPOL,
3993 FEAT_LCDENABLESIGNAL,
3994 FEAT_PCKFREEENABLE,
3995 FEAT_FUNCGATED,
3996 FEAT_LINEBUFFERSPLIT,
3997 FEAT_ROWREPEATENABLE,
3998 FEAT_RESIZECONF,
3999 FEAT_CPR,
4000 FEAT_PRELOAD,
4001 FEAT_FIR_COEF_V,
4002 FEAT_ALPHA_FIXED_ZORDER,
4003 FEAT_FIFO_MERGE,
4004};
4005
4006static const enum dispc_feature_id omap4_dispc_features_list[] = {
4007 FEAT_MGR_LCD2,
4008 FEAT_CORE_CLK_DIV,
4009 FEAT_HANDLE_UV_SEPARATE,
4010 FEAT_ATTR2,
4011 FEAT_CPR,
4012 FEAT_PRELOAD,
4013 FEAT_FIR_COEF_V,
4014 FEAT_ALPHA_FREE_ZORDER,
4015 FEAT_FIFO_MERGE,
4016 FEAT_BURST_2D,
4017};
4018
4019static const enum dispc_feature_id omap5_dispc_features_list[] = {
4020 FEAT_MGR_LCD2,
4021 FEAT_MGR_LCD3,
4022 FEAT_CORE_CLK_DIV,
4023 FEAT_HANDLE_UV_SEPARATE,
4024 FEAT_ATTR2,
4025 FEAT_CPR,
4026 FEAT_PRELOAD,
4027 FEAT_FIR_COEF_V,
4028 FEAT_ALPHA_FREE_ZORDER,
4029 FEAT_FIFO_MERGE,
4030 FEAT_BURST_2D,
4031 FEAT_MFLAG,
4032};
4033
4034static const struct dss_reg_field omap2_dispc_reg_fields[] = {
4035 [FEAT_REG_FIRHINC] = { 11, 0 },
4036 [FEAT_REG_FIRVINC] = { 27, 16 },
4037 [FEAT_REG_FIFOLOWTHRESHOLD] = { 8, 0 },
4038 [FEAT_REG_FIFOHIGHTHRESHOLD] = { 24, 16 },
4039 [FEAT_REG_FIFOSIZE] = { 8, 0 },
4040 [FEAT_REG_HORIZONTALACCU] = { 9, 0 },
4041 [FEAT_REG_VERTICALACCU] = { 25, 16 },
4042};
4043
4044static const struct dss_reg_field omap3_dispc_reg_fields[] = {
4045 [FEAT_REG_FIRHINC] = { 12, 0 },
4046 [FEAT_REG_FIRVINC] = { 28, 16 },
4047 [FEAT_REG_FIFOLOWTHRESHOLD] = { 11, 0 },
4048 [FEAT_REG_FIFOHIGHTHRESHOLD] = { 27, 16 },
4049 [FEAT_REG_FIFOSIZE] = { 10, 0 },
4050 [FEAT_REG_HORIZONTALACCU] = { 9, 0 },
4051 [FEAT_REG_VERTICALACCU] = { 25, 16 },
4052};
4053
4054static const struct dss_reg_field omap4_dispc_reg_fields[] = {
4055 [FEAT_REG_FIRHINC] = { 12, 0 },
4056 [FEAT_REG_FIRVINC] = { 28, 16 },
4057 [FEAT_REG_FIFOLOWTHRESHOLD] = { 15, 0 },
4058 [FEAT_REG_FIFOHIGHTHRESHOLD] = { 31, 16 },
4059 [FEAT_REG_FIFOSIZE] = { 15, 0 },
4060 [FEAT_REG_HORIZONTALACCU] = { 10, 0 },
4061 [FEAT_REG_VERTICALACCU] = { 26, 16 },
4062};
4063
4064static const enum omap_overlay_caps omap2_dispc_overlay_caps[] = {
4065 /* OMAP_DSS_GFX */
4066 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
4067
4068 /* OMAP_DSS_VIDEO1 */
4069 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_POS |
4070 OMAP_DSS_OVL_CAP_REPLICATION,
4071
4072 /* OMAP_DSS_VIDEO2 */
4073 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_POS |
4074 OMAP_DSS_OVL_CAP_REPLICATION,
4075};
4076
4077static const enum omap_overlay_caps omap3430_dispc_overlay_caps[] = {
4078 /* OMAP_DSS_GFX */
4079 OMAP_DSS_OVL_CAP_GLOBAL_ALPHA | OMAP_DSS_OVL_CAP_POS |
4080 OMAP_DSS_OVL_CAP_REPLICATION,
4081
4082 /* OMAP_DSS_VIDEO1 */
4083 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_POS |
4084 OMAP_DSS_OVL_CAP_REPLICATION,
4085
4086 /* OMAP_DSS_VIDEO2 */
4087 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_GLOBAL_ALPHA |
4088 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
4089};
4090
4091static const enum omap_overlay_caps omap3630_dispc_overlay_caps[] = {
4092 /* OMAP_DSS_GFX */
4093 OMAP_DSS_OVL_CAP_GLOBAL_ALPHA | OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA |
4094 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
4095
4096 /* OMAP_DSS_VIDEO1 */
4097 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_POS |
4098 OMAP_DSS_OVL_CAP_REPLICATION,
4099
4100 /* OMAP_DSS_VIDEO2 */
4101 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_GLOBAL_ALPHA |
4102 OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA | OMAP_DSS_OVL_CAP_POS |
4103 OMAP_DSS_OVL_CAP_REPLICATION,
4104};
4105
4106static const enum omap_overlay_caps omap4_dispc_overlay_caps[] = {
4107 /* OMAP_DSS_GFX */
4108 OMAP_DSS_OVL_CAP_GLOBAL_ALPHA | OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA |
4109 OMAP_DSS_OVL_CAP_ZORDER | OMAP_DSS_OVL_CAP_POS |
4110 OMAP_DSS_OVL_CAP_REPLICATION,
4111
4112 /* OMAP_DSS_VIDEO1 */
4113 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_GLOBAL_ALPHA |
4114 OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA | OMAP_DSS_OVL_CAP_ZORDER |
4115 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
4116
4117 /* OMAP_DSS_VIDEO2 */
4118 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_GLOBAL_ALPHA |
4119 OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA | OMAP_DSS_OVL_CAP_ZORDER |
4120 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
4121
4122 /* OMAP_DSS_VIDEO3 */
4123 OMAP_DSS_OVL_CAP_SCALE | OMAP_DSS_OVL_CAP_GLOBAL_ALPHA |
4124 OMAP_DSS_OVL_CAP_PRE_MULT_ALPHA | OMAP_DSS_OVL_CAP_ZORDER |
4125 OMAP_DSS_OVL_CAP_POS | OMAP_DSS_OVL_CAP_REPLICATION,
4126};
4127
4128#define COLOR_ARRAY(arr...) (const u32[]) { arr, 0 }
4129
4130static const u32 *omap2_dispc_supported_color_modes[] = {
4131
4132 /* OMAP_DSS_GFX */
4133 COLOR_ARRAY(
4134 DRM_FORMAT_RGBX4444, DRM_FORMAT_RGB565,
4135 DRM_FORMAT_XRGB8888, DRM_FORMAT_RGB888),
4136
4137 /* OMAP_DSS_VIDEO1 */
4138 COLOR_ARRAY(
4139 DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888,
4140 DRM_FORMAT_RGB888, DRM_FORMAT_YUYV,
4141 DRM_FORMAT_UYVY),
4142
4143 /* OMAP_DSS_VIDEO2 */
4144 COLOR_ARRAY(
4145 DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888,
4146 DRM_FORMAT_RGB888, DRM_FORMAT_YUYV,
4147 DRM_FORMAT_UYVY),
4148};
4149
4150static const u32 *omap3_dispc_supported_color_modes[] = {
4151 /* OMAP_DSS_GFX */
4152 COLOR_ARRAY(
4153 DRM_FORMAT_RGBX4444, DRM_FORMAT_ARGB4444,
4154 DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888,
4155 DRM_FORMAT_RGB888, DRM_FORMAT_ARGB8888,
4156 DRM_FORMAT_RGBA8888, DRM_FORMAT_RGBX8888),
4157
4158 /* OMAP_DSS_VIDEO1 */
4159 COLOR_ARRAY(
4160 DRM_FORMAT_XRGB8888, DRM_FORMAT_RGB888,
4161 DRM_FORMAT_RGBX4444, DRM_FORMAT_RGB565,
4162 DRM_FORMAT_YUYV, DRM_FORMAT_UYVY),
4163
4164 /* OMAP_DSS_VIDEO2 */
4165 COLOR_ARRAY(
4166 DRM_FORMAT_RGBX4444, DRM_FORMAT_ARGB4444,
4167 DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888,
4168 DRM_FORMAT_RGB888, DRM_FORMAT_YUYV,
4169 DRM_FORMAT_UYVY, DRM_FORMAT_ARGB8888,
4170 DRM_FORMAT_RGBA8888, DRM_FORMAT_RGBX8888),
4171};
4172
4173static const u32 *omap4_dispc_supported_color_modes[] = {
4174 /* OMAP_DSS_GFX */
4175 COLOR_ARRAY(
4176 DRM_FORMAT_RGBX4444, DRM_FORMAT_ARGB4444,
4177 DRM_FORMAT_RGB565, DRM_FORMAT_XRGB8888,
4178 DRM_FORMAT_RGB888, DRM_FORMAT_ARGB8888,
4179 DRM_FORMAT_RGBA8888, DRM_FORMAT_RGBX8888,
4180 DRM_FORMAT_ARGB1555, DRM_FORMAT_XRGB4444,
4181 DRM_FORMAT_RGBA4444, DRM_FORMAT_XRGB1555),
4182
4183 /* OMAP_DSS_VIDEO1 */
4184 COLOR_ARRAY(
4185 DRM_FORMAT_RGB565, DRM_FORMAT_RGBX4444,
4186 DRM_FORMAT_YUYV, DRM_FORMAT_ARGB1555,
4187 DRM_FORMAT_RGBA8888, DRM_FORMAT_NV12,
4188 DRM_FORMAT_RGBA4444, DRM_FORMAT_XRGB8888,
4189 DRM_FORMAT_RGB888, DRM_FORMAT_UYVY,
4190 DRM_FORMAT_ARGB4444, DRM_FORMAT_XRGB1555,
4191 DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB4444,
4192 DRM_FORMAT_RGBX8888),
4193
4194 /* OMAP_DSS_VIDEO2 */
4195 COLOR_ARRAY(
4196 DRM_FORMAT_RGB565, DRM_FORMAT_RGBX4444,
4197 DRM_FORMAT_YUYV, DRM_FORMAT_ARGB1555,
4198 DRM_FORMAT_RGBA8888, DRM_FORMAT_NV12,
4199 DRM_FORMAT_RGBA4444, DRM_FORMAT_XRGB8888,
4200 DRM_FORMAT_RGB888, DRM_FORMAT_UYVY,
4201 DRM_FORMAT_ARGB4444, DRM_FORMAT_XRGB1555,
4202 DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB4444,
4203 DRM_FORMAT_RGBX8888),
4204
4205 /* OMAP_DSS_VIDEO3 */
4206 COLOR_ARRAY(
4207 DRM_FORMAT_RGB565, DRM_FORMAT_RGBX4444,
4208 DRM_FORMAT_YUYV, DRM_FORMAT_ARGB1555,
4209 DRM_FORMAT_RGBA8888, DRM_FORMAT_NV12,
4210 DRM_FORMAT_RGBA4444, DRM_FORMAT_XRGB8888,
4211 DRM_FORMAT_RGB888, DRM_FORMAT_UYVY,
4212 DRM_FORMAT_ARGB4444, DRM_FORMAT_XRGB1555,
4213 DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB4444,
4214 DRM_FORMAT_RGBX8888),
4215
4216 /* OMAP_DSS_WB */
4217 COLOR_ARRAY(
4218 DRM_FORMAT_RGB565, DRM_FORMAT_RGBX4444,
4219 DRM_FORMAT_YUYV, DRM_FORMAT_ARGB1555,
4220 DRM_FORMAT_RGBA8888, DRM_FORMAT_NV12,
4221 DRM_FORMAT_RGBA4444, DRM_FORMAT_XRGB8888,
4222 DRM_FORMAT_RGB888, DRM_FORMAT_UYVY,
4223 DRM_FORMAT_ARGB4444, DRM_FORMAT_XRGB1555,
4224 DRM_FORMAT_ARGB8888, DRM_FORMAT_XRGB4444,
4225 DRM_FORMAT_RGBX8888),
4226};
4227
4228static const struct dispc_features omap24xx_dispc_feats = {
4229 .sw_start = 5,
4230 .fp_start = 15,
4231 .bp_start = 27,
4232 .sw_max = 64,
4233 .vp_max = 255,
4234 .hp_max = 256,
4235 .mgr_width_start = 10,
4236 .mgr_height_start = 26,
4237 .mgr_width_max = 2048,
4238 .mgr_height_max = 2048,
4239 .max_lcd_pclk = 66500000,
4240 .max_downscale = 2,
4241 /*
4242 * Assume the line width buffer to be 768 pixels as OMAP2 DISPC scaler
4243 * cannot scale an image width larger than 768.
4244 */
4245 .max_line_width = 768,
4246 .min_pcd = 2,
4247 .calc_scaling = dispc_ovl_calc_scaling_24xx,
4248 .calc_core_clk = calc_core_clk_24xx,
4249 .num_fifos = 3,
4250 .features = omap2_dispc_features_list,
4251 .num_features = ARRAY_SIZE(omap2_dispc_features_list),
4252 .reg_fields = omap2_dispc_reg_fields,
4253 .num_reg_fields = ARRAY_SIZE(omap2_dispc_reg_fields),
4254 .overlay_caps = omap2_dispc_overlay_caps,
4255 .supported_color_modes = omap2_dispc_supported_color_modes,
4256 .num_mgrs = 2,
4257 .num_ovls = 3,
4258 .buffer_size_unit = 1,
4259 .burst_size_unit = 8,
4260 .no_framedone_tv = true,
4261 .set_max_preload = false,
4262 .last_pixel_inc_missing = true,
4263};
4264
4265static const struct dispc_features omap34xx_rev1_0_dispc_feats = {
4266 .sw_start = 5,
4267 .fp_start = 15,
4268 .bp_start = 27,
4269 .sw_max = 64,
4270 .vp_max = 255,
4271 .hp_max = 256,
4272 .mgr_width_start = 10,
4273 .mgr_height_start = 26,
4274 .mgr_width_max = 2048,
4275 .mgr_height_max = 2048,
4276 .max_lcd_pclk = 173000000,
4277 .max_tv_pclk = 59000000,
4278 .max_downscale = 4,
4279 .max_line_width = 1024,
4280 .min_pcd = 1,
4281 .calc_scaling = dispc_ovl_calc_scaling_34xx,
4282 .calc_core_clk = calc_core_clk_34xx,
4283 .num_fifos = 3,
4284 .features = omap3_dispc_features_list,
4285 .num_features = ARRAY_SIZE(omap3_dispc_features_list),
4286 .reg_fields = omap3_dispc_reg_fields,
4287 .num_reg_fields = ARRAY_SIZE(omap3_dispc_reg_fields),
4288 .overlay_caps = omap3430_dispc_overlay_caps,
4289 .supported_color_modes = omap3_dispc_supported_color_modes,
4290 .num_mgrs = 2,
4291 .num_ovls = 3,
4292 .buffer_size_unit = 1,
4293 .burst_size_unit = 8,
4294 .no_framedone_tv = true,
4295 .set_max_preload = false,
4296 .last_pixel_inc_missing = true,
4297};
4298
4299static const struct dispc_features omap34xx_rev3_0_dispc_feats = {
4300 .sw_start = 7,
4301 .fp_start = 19,
4302 .bp_start = 31,
4303 .sw_max = 256,
4304 .vp_max = 4095,
4305 .hp_max = 4096,
4306 .mgr_width_start = 10,
4307 .mgr_height_start = 26,
4308 .mgr_width_max = 2048,
4309 .mgr_height_max = 2048,
4310 .max_lcd_pclk = 173000000,
4311 .max_tv_pclk = 59000000,
4312 .max_downscale = 4,
4313 .max_line_width = 1024,
4314 .min_pcd = 1,
4315 .calc_scaling = dispc_ovl_calc_scaling_34xx,
4316 .calc_core_clk = calc_core_clk_34xx,
4317 .num_fifos = 3,
4318 .features = omap3_dispc_features_list,
4319 .num_features = ARRAY_SIZE(omap3_dispc_features_list),
4320 .reg_fields = omap3_dispc_reg_fields,
4321 .num_reg_fields = ARRAY_SIZE(omap3_dispc_reg_fields),
4322 .overlay_caps = omap3430_dispc_overlay_caps,
4323 .supported_color_modes = omap3_dispc_supported_color_modes,
4324 .num_mgrs = 2,
4325 .num_ovls = 3,
4326 .buffer_size_unit = 1,
4327 .burst_size_unit = 8,
4328 .no_framedone_tv = true,
4329 .set_max_preload = false,
4330 .last_pixel_inc_missing = true,
4331};
4332
4333static const struct dispc_features omap36xx_dispc_feats = {
4334 .sw_start = 7,
4335 .fp_start = 19,
4336 .bp_start = 31,
4337 .sw_max = 256,
4338 .vp_max = 4095,
4339 .hp_max = 4096,
4340 .mgr_width_start = 10,
4341 .mgr_height_start = 26,
4342 .mgr_width_max = 2048,
4343 .mgr_height_max = 2048,
4344 .max_lcd_pclk = 173000000,
4345 .max_tv_pclk = 59000000,
4346 .max_downscale = 4,
4347 .max_line_width = 1024,
4348 .min_pcd = 1,
4349 .calc_scaling = dispc_ovl_calc_scaling_34xx,
4350 .calc_core_clk = calc_core_clk_34xx,
4351 .num_fifos = 3,
4352 .features = omap3_dispc_features_list,
4353 .num_features = ARRAY_SIZE(omap3_dispc_features_list),
4354 .reg_fields = omap3_dispc_reg_fields,
4355 .num_reg_fields = ARRAY_SIZE(omap3_dispc_reg_fields),
4356 .overlay_caps = omap3630_dispc_overlay_caps,
4357 .supported_color_modes = omap3_dispc_supported_color_modes,
4358 .num_mgrs = 2,
4359 .num_ovls = 3,
4360 .buffer_size_unit = 1,
4361 .burst_size_unit = 8,
4362 .no_framedone_tv = true,
4363 .set_max_preload = false,
4364 .last_pixel_inc_missing = true,
4365};
4366
4367static const struct dispc_features am43xx_dispc_feats = {
4368 .sw_start = 7,
4369 .fp_start = 19,
4370 .bp_start = 31,
4371 .sw_max = 256,
4372 .vp_max = 4095,
4373 .hp_max = 4096,
4374 .mgr_width_start = 10,
4375 .mgr_height_start = 26,
4376 .mgr_width_max = 2048,
4377 .mgr_height_max = 2048,
4378 .max_lcd_pclk = 173000000,
4379 .max_tv_pclk = 59000000,
4380 .max_downscale = 4,
4381 .max_line_width = 1024,
4382 .min_pcd = 1,
4383 .calc_scaling = dispc_ovl_calc_scaling_34xx,
4384 .calc_core_clk = calc_core_clk_34xx,
4385 .num_fifos = 3,
4386 .features = am43xx_dispc_features_list,
4387 .num_features = ARRAY_SIZE(am43xx_dispc_features_list),
4388 .reg_fields = omap3_dispc_reg_fields,
4389 .num_reg_fields = ARRAY_SIZE(omap3_dispc_reg_fields),
4390 .overlay_caps = omap3430_dispc_overlay_caps,
4391 .supported_color_modes = omap3_dispc_supported_color_modes,
4392 .num_mgrs = 1,
4393 .num_ovls = 3,
4394 .buffer_size_unit = 1,
4395 .burst_size_unit = 8,
4396 .no_framedone_tv = true,
4397 .set_max_preload = false,
4398 .last_pixel_inc_missing = true,
4399};
4400
4401static const struct dispc_features omap44xx_dispc_feats = {
4402 .sw_start = 7,
4403 .fp_start = 19,
4404 .bp_start = 31,
4405 .sw_max = 256,
4406 .vp_max = 4095,
4407 .hp_max = 4096,
4408 .mgr_width_start = 10,
4409 .mgr_height_start = 26,
4410 .mgr_width_max = 2048,
4411 .mgr_height_max = 2048,
4412 .max_lcd_pclk = 170000000,
4413 .max_tv_pclk = 185625000,
4414 .max_downscale = 4,
4415 .max_line_width = 2048,
4416 .min_pcd = 1,
4417 .calc_scaling = dispc_ovl_calc_scaling_44xx,
4418 .calc_core_clk = calc_core_clk_44xx,
4419 .num_fifos = 5,
4420 .features = omap4_dispc_features_list,
4421 .num_features = ARRAY_SIZE(omap4_dispc_features_list),
4422 .reg_fields = omap4_dispc_reg_fields,
4423 .num_reg_fields = ARRAY_SIZE(omap4_dispc_reg_fields),
4424 .overlay_caps = omap4_dispc_overlay_caps,
4425 .supported_color_modes = omap4_dispc_supported_color_modes,
4426 .num_mgrs = 3,
4427 .num_ovls = 4,
4428 .buffer_size_unit = 16,
4429 .burst_size_unit = 16,
4430 .gfx_fifo_workaround = true,
4431 .set_max_preload = true,
4432 .supports_sync_align = true,
4433 .has_writeback = true,
4434 .supports_double_pixel = true,
4435 .reverse_ilace_field_order = true,
4436 .has_gamma_table = true,
4437 .has_gamma_i734_bug = true,
4438};
4439
4440static const struct dispc_features omap54xx_dispc_feats = {
4441 .sw_start = 7,
4442 .fp_start = 19,
4443 .bp_start = 31,
4444 .sw_max = 256,
4445 .vp_max = 4095,
4446 .hp_max = 4096,
4447 .mgr_width_start = 11,
4448 .mgr_height_start = 27,
4449 .mgr_width_max = 4096,
4450 .mgr_height_max = 4096,
4451 .max_lcd_pclk = 170000000,
4452 .max_tv_pclk = 186000000,
4453 .max_downscale = 4,
4454 .max_line_width = 2048,
4455 .min_pcd = 1,
4456 .calc_scaling = dispc_ovl_calc_scaling_44xx,
4457 .calc_core_clk = calc_core_clk_44xx,
4458 .num_fifos = 5,
4459 .features = omap5_dispc_features_list,
4460 .num_features = ARRAY_SIZE(omap5_dispc_features_list),
4461 .reg_fields = omap4_dispc_reg_fields,
4462 .num_reg_fields = ARRAY_SIZE(omap4_dispc_reg_fields),
4463 .overlay_caps = omap4_dispc_overlay_caps,
4464 .supported_color_modes = omap4_dispc_supported_color_modes,
4465 .num_mgrs = 4,
4466 .num_ovls = 4,
4467 .buffer_size_unit = 16,
4468 .burst_size_unit = 16,
4469 .gfx_fifo_workaround = true,
4470 .mstandby_workaround = true,
4471 .set_max_preload = true,
4472 .supports_sync_align = true,
4473 .has_writeback = true,
4474 .supports_double_pixel = true,
4475 .reverse_ilace_field_order = true,
4476 .has_gamma_table = true,
4477 .has_gamma_i734_bug = true,
4478};
4479
4480static irqreturn_t dispc_irq_handler(int irq, void *arg)
4481{
4482 struct dispc_device *dispc = arg;
4483
4484 if (!dispc->is_enabled)
4485 return IRQ_NONE;
4486
4487 return dispc->user_handler(irq, dispc->user_data);
4488}
4489
4490static int dispc_request_irq(struct dispc_device *dispc, irq_handler_t handler,
4491 void *dev_id)
4492{
4493 int r;
4494
4495 if (dispc->user_handler != NULL)
4496 return -EBUSY;
4497
4498 dispc->user_handler = handler;
4499 dispc->user_data = dev_id;
4500
4501 /* ensure the dispc_irq_handler sees the values above */
4502 smp_wmb();
4503
4504 r = devm_request_irq(&dispc->pdev->dev, dispc->irq, dispc_irq_handler,
4505 IRQF_SHARED, "OMAP DISPC", dispc);
4506 if (r) {
4507 dispc->user_handler = NULL;
4508 dispc->user_data = NULL;
4509 }
4510
4511 return r;
4512}
4513
4514static void dispc_free_irq(struct dispc_device *dispc, void *dev_id)
4515{
4516 devm_free_irq(&dispc->pdev->dev, dispc->irq, dispc);
4517
4518 dispc->user_handler = NULL;
4519 dispc->user_data = NULL;
4520}
4521
4522static u32 dispc_get_memory_bandwidth_limit(struct dispc_device *dispc)
4523{
4524 u32 limit = 0;
4525
4526 /* Optional maximum memory bandwidth */
4527 of_property_read_u32(dispc->pdev->dev.of_node, "max-memory-bandwidth",
4528 &limit);
4529
4530 return limit;
4531}
4532
4533/*
4534 * Workaround for errata i734 in DSS dispc
4535 * - LCD1 Gamma Correction Is Not Working When GFX Pipe Is Disabled
4536 *
4537 * For gamma tables to work on LCD1 the GFX plane has to be used at
4538 * least once after DSS HW has come out of reset. The workaround
4539 * sets up a minimal LCD setup with GFX plane and waits for one
4540 * vertical sync irq before disabling the setup and continuing with
4541 * the context restore. The physical outputs are gated during the
4542 * operation. This workaround requires that gamma table's LOADMODE
4543 * is set to 0x2 in DISPC_CONTROL1 register.
4544 *
4545 * For details see:
4546 * OMAP543x Multimedia Device Silicon Revision 2.0 Silicon Errata
4547 * Literature Number: SWPZ037E
4548 * Or some other relevant errata document for the DSS IP version.
4549 */
4550
4551static const struct dispc_errata_i734_data {
4552 struct videomode vm;
4553 struct omap_overlay_info ovli;
4554 struct omap_overlay_manager_info mgri;
4555 struct dss_lcd_mgr_config lcd_conf;
4556} i734 = {
4557 .vm = {
4558 .hactive = 8, .vactive = 1,
4559 .pixelclock = 16000000,
4560 .hsync_len = 8, .hfront_porch = 4, .hback_porch = 4,
4561 .vsync_len = 1, .vfront_porch = 1, .vback_porch = 1,
4562
4563 .flags = DISPLAY_FLAGS_HSYNC_LOW | DISPLAY_FLAGS_VSYNC_LOW |
4564 DISPLAY_FLAGS_DE_HIGH | DISPLAY_FLAGS_SYNC_POSEDGE |
4565 DISPLAY_FLAGS_PIXDATA_POSEDGE,
4566 },
4567 .ovli = {
4568 .screen_width = 1,
4569 .width = 1, .height = 1,
4570 .fourcc = DRM_FORMAT_XRGB8888,
4571 .rotation = DRM_MODE_ROTATE_0,
4572 .rotation_type = OMAP_DSS_ROT_NONE,
4573 .pos_x = 0, .pos_y = 0,
4574 .out_width = 0, .out_height = 0,
4575 .global_alpha = 0xff,
4576 .pre_mult_alpha = 0,
4577 .zorder = 0,
4578 },
4579 .mgri = {
4580 .default_color = 0,
4581 .trans_enabled = false,
4582 .partial_alpha_enabled = false,
4583 .cpr_enable = false,
4584 },
4585 .lcd_conf = {
4586 .io_pad_mode = DSS_IO_PAD_MODE_BYPASS,
4587 .stallmode = false,
4588 .fifohandcheck = false,
4589 .clock_info = {
4590 .lck_div = 1,
4591 .pck_div = 2,
4592 },
4593 .video_port_width = 24,
4594 .lcden_sig_polarity = 0,
4595 },
4596};
4597
4598static struct i734_buf {
4599 size_t size;
4600 dma_addr_t paddr;
4601 void *vaddr;
4602} i734_buf;
4603
4604static int dispc_errata_i734_wa_init(struct dispc_device *dispc)
4605{
4606 if (!dispc->feat->has_gamma_i734_bug)
4607 return 0;
4608
4609 i734_buf.size = i734.ovli.width * i734.ovli.height *
4610 color_mode_to_bpp(i734.ovli.fourcc) / 8;
4611
4612 i734_buf.vaddr = dma_alloc_wc(&dispc->pdev->dev, i734_buf.size,
4613 &i734_buf.paddr, GFP_KERNEL);
4614 if (!i734_buf.vaddr) {
4615 dev_err(&dispc->pdev->dev, "%s: dma_alloc_wc failed\n",
4616 __func__);
4617 return -ENOMEM;
4618 }
4619
4620 return 0;
4621}
4622
4623static void dispc_errata_i734_wa_fini(struct dispc_device *dispc)
4624{
4625 if (!dispc->feat->has_gamma_i734_bug)
4626 return;
4627
4628 dma_free_wc(&dispc->pdev->dev, i734_buf.size, i734_buf.vaddr,
4629 i734_buf.paddr);
4630}
4631
4632static void dispc_errata_i734_wa(struct dispc_device *dispc)
4633{
4634 u32 framedone_irq = dispc_mgr_get_framedone_irq(dispc,
4635 OMAP_DSS_CHANNEL_LCD);
4636 struct omap_overlay_info ovli;
4637 struct dss_lcd_mgr_config lcd_conf;
4638 u32 gatestate;
4639 unsigned int count;
4640
4641 if (!dispc->feat->has_gamma_i734_bug)
4642 return;
4643
4644 gatestate = REG_GET(dispc, DISPC_CONFIG, 8, 4);
4645
4646 ovli = i734.ovli;
4647 ovli.paddr = i734_buf.paddr;
4648 lcd_conf = i734.lcd_conf;
4649
4650 /* Gate all LCD1 outputs */
4651 REG_FLD_MOD(dispc, DISPC_CONFIG, 0x1f, 8, 4);
4652
4653 /* Setup and enable GFX plane */
4654 dispc_ovl_setup(dispc, OMAP_DSS_GFX, &ovli, &i734.vm, false,
4655 OMAP_DSS_CHANNEL_LCD);
4656 dispc_ovl_enable(dispc, OMAP_DSS_GFX, true);
4657
4658 /* Set up and enable display manager for LCD1 */
4659 dispc_mgr_setup(dispc, OMAP_DSS_CHANNEL_LCD, &i734.mgri);
4660 dispc_calc_clock_rates(dispc, dss_get_dispc_clk_rate(dispc->dss),
4661 &lcd_conf.clock_info);
4662 dispc_mgr_set_lcd_config(dispc, OMAP_DSS_CHANNEL_LCD, &lcd_conf);
4663 dispc_mgr_set_timings(dispc, OMAP_DSS_CHANNEL_LCD, &i734.vm);
4664
4665 dispc_clear_irqstatus(dispc, framedone_irq);
4666
4667 /* Enable and shut the channel to produce just one frame */
4668 dispc_mgr_enable(dispc, OMAP_DSS_CHANNEL_LCD, true);
4669 dispc_mgr_enable(dispc, OMAP_DSS_CHANNEL_LCD, false);
4670
4671 /* Busy wait for framedone. We can't fiddle with irq handlers
4672 * in PM resume. Typically the loop runs less than 5 times and
4673 * waits less than a micro second.
4674 */
4675 count = 0;
4676 while (!(dispc_read_irqstatus(dispc) & framedone_irq)) {
4677 if (count++ > 10000) {
4678 dev_err(&dispc->pdev->dev, "%s: framedone timeout\n",
4679 __func__);
4680 break;
4681 }
4682 }
4683 dispc_ovl_enable(dispc, OMAP_DSS_GFX, false);
4684
4685 /* Clear all irq bits before continuing */
4686 dispc_clear_irqstatus(dispc, 0xffffffff);
4687
4688 /* Restore the original state to LCD1 output gates */
4689 REG_FLD_MOD(dispc, DISPC_CONFIG, gatestate, 8, 4);
4690}
4691
4692static const struct dispc_ops dispc_ops = {
4693 .read_irqstatus = dispc_read_irqstatus,
4694 .clear_irqstatus = dispc_clear_irqstatus,
4695 .write_irqenable = dispc_write_irqenable,
4696
4697 .request_irq = dispc_request_irq,
4698 .free_irq = dispc_free_irq,
4699
4700 .runtime_get = dispc_runtime_get,
4701 .runtime_put = dispc_runtime_put,
4702
4703 .get_num_ovls = dispc_get_num_ovls,
4704 .get_num_mgrs = dispc_get_num_mgrs,
4705
4706 .get_memory_bandwidth_limit = dispc_get_memory_bandwidth_limit,
4707
4708 .mgr_enable = dispc_mgr_enable,
4709 .mgr_is_enabled = dispc_mgr_is_enabled,
4710 .mgr_get_vsync_irq = dispc_mgr_get_vsync_irq,
4711 .mgr_get_framedone_irq = dispc_mgr_get_framedone_irq,
4712 .mgr_get_sync_lost_irq = dispc_mgr_get_sync_lost_irq,
4713 .mgr_go_busy = dispc_mgr_go_busy,
4714 .mgr_go = dispc_mgr_go,
4715 .mgr_set_lcd_config = dispc_mgr_set_lcd_config,
4716 .mgr_check_timings = dispc_mgr_check_timings,
4717 .mgr_set_timings = dispc_mgr_set_timings,
4718 .mgr_setup = dispc_mgr_setup,
4719 .mgr_gamma_size = dispc_mgr_gamma_size,
4720 .mgr_set_gamma = dispc_mgr_set_gamma,
4721
4722 .ovl_enable = dispc_ovl_enable,
4723 .ovl_setup = dispc_ovl_setup,
4724 .ovl_get_color_modes = dispc_ovl_get_color_modes,
4725
4726 .wb_get_framedone_irq = dispc_wb_get_framedone_irq,
4727 .wb_setup = dispc_wb_setup,
4728 .has_writeback = dispc_has_writeback,
4729 .wb_go_busy = dispc_wb_go_busy,
4730 .wb_go = dispc_wb_go,
4731};
4732
4733/* DISPC HW IP initialisation */
4734static const struct of_device_id dispc_of_match[] = {
4735 { .compatible = "ti,omap2-dispc", .data = &omap24xx_dispc_feats },
4736 { .compatible = "ti,omap3-dispc", .data = &omap36xx_dispc_feats },
4737 { .compatible = "ti,omap4-dispc", .data = &omap44xx_dispc_feats },
4738 { .compatible = "ti,omap5-dispc", .data = &omap54xx_dispc_feats },
4739 { .compatible = "ti,dra7-dispc", .data = &omap54xx_dispc_feats },
4740 {},
4741};
4742
4743static const struct soc_device_attribute dispc_soc_devices[] = {
4744 { .machine = "OMAP3[45]*",
4745 .revision = "ES[12].?", .data = &omap34xx_rev1_0_dispc_feats },
4746 { .machine = "OMAP3[45]*", .data = &omap34xx_rev3_0_dispc_feats },
4747 { .machine = "AM35*", .data = &omap34xx_rev3_0_dispc_feats },
4748 { .machine = "AM43*", .data = &am43xx_dispc_feats },
4749 { /* sentinel */ }
4750};
4751
4752static int dispc_bind(struct device *dev, struct device *master, void *data)
4753{
4754 struct platform_device *pdev = to_platform_device(dev);
4755 const struct soc_device_attribute *soc;
4756 struct dss_device *dss = dss_get_device(master);
4757 struct dispc_device *dispc;
4758 u32 rev;
4759 int r = 0;
4760 struct resource *dispc_mem;
4761 struct device_node *np = pdev->dev.of_node;
4762
4763 dispc = kzalloc(sizeof(*dispc), GFP_KERNEL);
4764 if (!dispc)
4765 return -ENOMEM;
4766
4767 dispc->pdev = pdev;
4768 platform_set_drvdata(pdev, dispc);
4769 dispc->dss = dss;
4770
4771 spin_lock_init(&dispc->control_lock);
4772
4773 /*
4774 * The OMAP3-based models can't be told apart using the compatible
4775 * string, use SoC device matching.
4776 */
4777 soc = soc_device_match(dispc_soc_devices);
4778 if (soc)
4779 dispc->feat = soc->data;
4780 else
4781 dispc->feat = of_match_device(dispc_of_match, &pdev->dev)->data;
4782
4783 r = dispc_errata_i734_wa_init(dispc);
4784 if (r)
4785 goto err_free;
4786
4787 dispc_mem = platform_get_resource(dispc->pdev, IORESOURCE_MEM, 0);
4788 dispc->base = devm_ioremap_resource(&pdev->dev, dispc_mem);
4789 if (IS_ERR(dispc->base)) {
4790 r = PTR_ERR(dispc->base);
4791 goto err_free;
4792 }
4793
4794 dispc->irq = platform_get_irq(dispc->pdev, 0);
4795 if (dispc->irq < 0) {
4796 DSSERR("platform_get_irq failed\n");
4797 r = -ENODEV;
4798 goto err_free;
4799 }
4800
4801 if (np && of_property_read_bool(np, "syscon-pol")) {
4802 dispc->syscon_pol = syscon_regmap_lookup_by_phandle(np, "syscon-pol");
4803 if (IS_ERR(dispc->syscon_pol)) {
4804 dev_err(&pdev->dev, "failed to get syscon-pol regmap\n");
4805 r = PTR_ERR(dispc->syscon_pol);
4806 goto err_free;
4807 }
4808
4809 if (of_property_read_u32_index(np, "syscon-pol", 1,
4810 &dispc->syscon_pol_offset)) {
4811 dev_err(&pdev->dev, "failed to get syscon-pol offset\n");
4812 r = -EINVAL;
4813 goto err_free;
4814 }
4815 }
4816
4817 r = dispc_init_gamma_tables(dispc);
4818 if (r)
4819 goto err_free;
4820
4821 pm_runtime_enable(&pdev->dev);
4822
4823 r = dispc_runtime_get(dispc);
4824 if (r)
4825 goto err_runtime_get;
4826
4827 _omap_dispc_initial_config(dispc);
4828
4829 rev = dispc_read_reg(dispc, DISPC_REVISION);
4830 dev_dbg(&pdev->dev, "OMAP DISPC rev %d.%d\n",
4831 FLD_GET(rev, 7, 4), FLD_GET(rev, 3, 0));
4832
4833 dispc_runtime_put(dispc);
4834
4835 dss->dispc = dispc;
4836 dss->dispc_ops = &dispc_ops;
4837
4838 dispc->debugfs = dss_debugfs_create_file(dss, "dispc", dispc_dump_regs,
4839 dispc);
4840
4841 return 0;
4842
4843err_runtime_get:
4844 pm_runtime_disable(&pdev->dev);
4845err_free:
4846 kfree(dispc);
4847 return r;
4848}
4849
4850static void dispc_unbind(struct device *dev, struct device *master, void *data)
4851{
4852 struct dispc_device *dispc = dev_get_drvdata(dev);
4853 struct dss_device *dss = dispc->dss;
4854
4855 dss_debugfs_remove_file(dispc->debugfs);
4856
4857 dss->dispc = NULL;
4858 dss->dispc_ops = NULL;
4859
4860 pm_runtime_disable(dev);
4861
4862 dispc_errata_i734_wa_fini(dispc);
4863
4864 kfree(dispc);
4865}
4866
4867static const struct component_ops dispc_component_ops = {
4868 .bind = dispc_bind,
4869 .unbind = dispc_unbind,
4870};
4871
4872static int dispc_probe(struct platform_device *pdev)
4873{
4874 return component_add(&pdev->dev, &dispc_component_ops);
4875}
4876
4877static int dispc_remove(struct platform_device *pdev)
4878{
4879 component_del(&pdev->dev, &dispc_component_ops);
4880 return 0;
4881}
4882
4883static int dispc_runtime_suspend(struct device *dev)
4884{
4885 struct dispc_device *dispc = dev_get_drvdata(dev);
4886
4887 dispc->is_enabled = false;
4888 /* ensure the dispc_irq_handler sees the is_enabled value */
4889 smp_wmb();
4890 /* wait for current handler to finish before turning the DISPC off */
4891 synchronize_irq(dispc->irq);
4892
4893 dispc_save_context(dispc);
4894
4895 return 0;
4896}
4897
4898static int dispc_runtime_resume(struct device *dev)
4899{
4900 struct dispc_device *dispc = dev_get_drvdata(dev);
4901
4902 /*
4903 * The reset value for load mode is 0 (OMAP_DSS_LOAD_CLUT_AND_FRAME)
4904 * but we always initialize it to 2 (OMAP_DSS_LOAD_FRAME_ONLY) in
4905 * _omap_dispc_initial_config(). We can thus use it to detect if
4906 * we have lost register context.
4907 */
4908 if (REG_GET(dispc, DISPC_CONFIG, 2, 1) != OMAP_DSS_LOAD_FRAME_ONLY) {
4909 _omap_dispc_initial_config(dispc);
4910
4911 dispc_errata_i734_wa(dispc);
4912
4913 dispc_restore_context(dispc);
4914
4915 dispc_restore_gamma_tables(dispc);
4916 }
4917
4918 dispc->is_enabled = true;
4919 /* ensure the dispc_irq_handler sees the is_enabled value */
4920 smp_wmb();
4921
4922 return 0;
4923}
4924
4925static const struct dev_pm_ops dispc_pm_ops = {
4926 .runtime_suspend = dispc_runtime_suspend,
4927 .runtime_resume = dispc_runtime_resume,
4928};
4929
4930struct platform_driver omap_dispchw_driver = {
4931 .probe = dispc_probe,
4932 .remove = dispc_remove,
4933 .driver = {
4934 .name = "omapdss_dispc",
4935 .pm = &dispc_pm_ops,
4936 .of_match_table = dispc_of_match,
4937 .suppress_bind_attrs = true,
4938 },
4939};