2 * OMAP2 display controller support
4 * Copyright (C) 2005 Nokia Corporation
5 * Author: Imre Deak <imre.deak@nokia.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/kernel.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/vmalloc.h>
24 #include <linux/clk.h>
27 #include <asm/arch/sram.h>
28 #include <asm/arch/omapfb.h>
29 #include <asm/arch/board.h>
33 #define MODULE_NAME "dispc"
35 #define DSS_BASE 0x48050000
36 #define DSS_SYSCONFIG 0x0010
38 #define DISPC_BASE 0x48050400
41 #define DISPC_REVISION 0x0000
42 #define DISPC_SYSCONFIG 0x0010
43 #define DISPC_SYSSTATUS 0x0014
44 #define DISPC_IRQSTATUS 0x0018
45 #define DISPC_IRQENABLE 0x001C
46 #define DISPC_CONTROL 0x0040
47 #define DISPC_CONFIG 0x0044
48 #define DISPC_CAPABLE 0x0048
49 #define DISPC_DEFAULT_COLOR0 0x004C
50 #define DISPC_DEFAULT_COLOR1 0x0050
51 #define DISPC_TRANS_COLOR0 0x0054
52 #define DISPC_TRANS_COLOR1 0x0058
53 #define DISPC_LINE_STATUS 0x005C
54 #define DISPC_LINE_NUMBER 0x0060
55 #define DISPC_TIMING_H 0x0064
56 #define DISPC_TIMING_V 0x0068
57 #define DISPC_POL_FREQ 0x006C
58 #define DISPC_DIVISOR 0x0070
59 #define DISPC_SIZE_DIG 0x0078
60 #define DISPC_SIZE_LCD 0x007C
62 #define DISPC_DATA_CYCLE1 0x01D4
63 #define DISPC_DATA_CYCLE2 0x01D8
64 #define DISPC_DATA_CYCLE3 0x01DC
67 #define DISPC_GFX_BA0 0x0080
68 #define DISPC_GFX_BA1 0x0084
69 #define DISPC_GFX_POSITION 0x0088
70 #define DISPC_GFX_SIZE 0x008C
71 #define DISPC_GFX_ATTRIBUTES 0x00A0
72 #define DISPC_GFX_FIFO_THRESHOLD 0x00A4
73 #define DISPC_GFX_FIFO_SIZE_STATUS 0x00A8
74 #define DISPC_GFX_ROW_INC 0x00AC
75 #define DISPC_GFX_PIXEL_INC 0x00B0
76 #define DISPC_GFX_WINDOW_SKIP 0x00B4
77 #define DISPC_GFX_TABLE_BA 0x00B8
79 /* DISPC Video plane 1/2 */
80 #define DISPC_VID1_BASE 0x00BC
81 #define DISPC_VID2_BASE 0x014C
83 /* Offsets into DISPC_VID1/2_BASE */
84 #define DISPC_VID_BA0 0x0000
85 #define DISPC_VID_BA1 0x0004
86 #define DISPC_VID_POSITION 0x0008
87 #define DISPC_VID_SIZE 0x000C
88 #define DISPC_VID_ATTRIBUTES 0x0010
89 #define DISPC_VID_FIFO_THRESHOLD 0x0014
90 #define DISPC_VID_FIFO_SIZE_STATUS 0x0018
91 #define DISPC_VID_ROW_INC 0x001C
92 #define DISPC_VID_PIXEL_INC 0x0020
93 #define DISPC_VID_FIR 0x0024
94 #define DISPC_VID_PICTURE_SIZE 0x0028
95 #define DISPC_VID_ACCU0 0x002C
96 #define DISPC_VID_ACCU1 0x0030
98 /* 8 elements in 8 byte increments */
99 #define DISPC_VID_FIR_COEF_H0 0x0034
100 /* 8 elements in 8 byte increments */
101 #define DISPC_VID_FIR_COEF_HV0 0x0038
102 /* 5 elements in 4 byte increments */
103 #define DISPC_VID_CONV_COEF0 0x0074
105 #define DISPC_IRQ_FRAMEMASK 0x0001
106 #define DISPC_IRQ_VSYNC 0x0002
107 #define DISPC_IRQ_EVSYNC_EVEN 0x0004
108 #define DISPC_IRQ_EVSYNC_ODD 0x0008
109 #define DISPC_IRQ_ACBIAS_COUNT_STAT 0x0010
110 #define DISPC_IRQ_PROG_LINE_NUM 0x0020
111 #define DISPC_IRQ_GFX_FIFO_UNDERFLOW 0x0040
112 #define DISPC_IRQ_GFX_END_WIN 0x0080
113 #define DISPC_IRQ_PAL_GAMMA_MASK 0x0100
114 #define DISPC_IRQ_OCP_ERR 0x0200
115 #define DISPC_IRQ_VID1_FIFO_UNDERFLOW 0x0400
116 #define DISPC_IRQ_VID1_END_WIN 0x0800
117 #define DISPC_IRQ_VID2_FIFO_UNDERFLOW 0x1000
118 #define DISPC_IRQ_VID2_END_WIN 0x2000
119 #define DISPC_IRQ_SYNC_LOST 0x4000
121 #define DISPC_IRQ_MASK_ALL 0x7fff
123 #define DISPC_IRQ_MASK_ERROR (DISPC_IRQ_GFX_FIFO_UNDERFLOW | \
124 DISPC_IRQ_VID1_FIFO_UNDERFLOW | \
125 DISPC_IRQ_VID2_FIFO_UNDERFLOW | \
128 #define RFBI_CONTROL 0x48050040
130 #define MAX_PALETTE_SIZE (256 * 16)
132 #define FLD_MASK(pos, len) (((1 << len) - 1) << pos)
134 #define MOD_REG_FLD(reg, mask, val) \
135 dispc_write_reg((reg), (dispc_read_reg(reg) & ~(mask)) | (val));
137 #define OMAP2_SRAM_START 0x40200000
138 /* Maximum size, in reality this is smaller if SRAM is partially locked. */
139 #define OMAP2_SRAM_SIZE 0xa0000 /* 640k */
141 /* We support the SDRAM / SRAM types. See OMAPFB_PLANE_MEMTYPE_* in omapfb.h */
142 #define DISPC_MEMTYPE_NUM 2
144 #define RESMAP_SIZE(_page_cnt) \
145 ((_page_cnt + (sizeof(unsigned long) * 8) - 1) / 8)
146 #define RESMAP_PTR(_res_map, _page_nr) \
147 (((_res_map)->map) + (_page_nr) / (sizeof(unsigned long) * 8))
148 #define RESMAP_MASK(_page_nr) \
149 (1 << ((_page_nr) & (sizeof(unsigned long) * 8 - 1)))
160 struct omapfb_mem_desc mem_desc;
161 struct resmap *res_map[DISPC_MEMTYPE_NUM];
162 atomic_t map_count[OMAPFB_PLANE_NUM];
164 dma_addr_t palette_paddr;
169 unsigned long enabled_irqs;
170 void (*irq_callback)(void *);
171 void *irq_callback_data;
172 struct completion frame_done;
174 int fir_hinc[OMAPFB_PLANE_NUM];
175 int fir_vinc[OMAPFB_PLANE_NUM];
177 struct clk *dss_ick, *dss1_fck;
178 struct clk *dss_54m_fck;
180 enum omapfb_update_mode update_mode;
181 struct omapfb_device *fbdev;
183 struct omapfb_color_key color_key;
186 static void enable_lcd_clocks(int enable);
188 static void inline dispc_write_reg(int idx, u32 val)
190 __raw_writel(val, dispc.base + idx);
193 static u32 inline dispc_read_reg(int idx)
195 u32 l = __raw_readl(dispc.base + idx);
199 /* Select RFBI or bypass mode */
200 static void enable_rfbi_mode(int enable)
204 l = dispc_read_reg(DISPC_CONTROL);
205 /* Enable RFBI, GPIO0/1 */
206 l &= ~((1 << 11) | (1 << 15) | (1 << 16));
207 l |= enable ? (1 << 11) : 0;
208 /* RFBI En: GPIO0/1=10 RFBI Dis: GPIO0/1=11 */
210 l |= enable ? 0 : (1 << 16);
211 dispc_write_reg(DISPC_CONTROL, l);
213 /* Set bypass mode in RFBI module */
214 l = __raw_readl(io_p2v(RFBI_CONTROL));
215 l |= enable ? 0 : (1 << 1);
216 __raw_writel(l, io_p2v(RFBI_CONTROL));
219 static void set_lcd_data_lines(int data_lines)
224 switch (data_lines) {
241 l = dispc_read_reg(DISPC_CONTROL);
244 dispc_write_reg(DISPC_CONTROL, l);
247 static void set_load_mode(int mode)
249 BUG_ON(mode & ~(DISPC_LOAD_CLUT_ONLY | DISPC_LOAD_FRAME_ONLY |
250 DISPC_LOAD_CLUT_ONCE_FRAME));
251 MOD_REG_FLD(DISPC_CONFIG, 0x03 << 1, mode << 1);
254 void omap_dispc_set_lcd_size(int x, int y)
256 BUG_ON((x > (1 << 11)) || (y > (1 << 11)));
257 enable_lcd_clocks(1);
258 MOD_REG_FLD(DISPC_SIZE_LCD, FLD_MASK(16, 11) | FLD_MASK(0, 11),
259 ((y - 1) << 16) | (x - 1));
260 enable_lcd_clocks(0);
262 EXPORT_SYMBOL(omap_dispc_set_lcd_size);
264 void omap_dispc_set_digit_size(int x, int y)
266 BUG_ON((x > (1 << 11)) || (y > (1 << 11)));
267 enable_lcd_clocks(1);
268 MOD_REG_FLD(DISPC_SIZE_DIG, FLD_MASK(16, 11) | FLD_MASK(0, 11),
269 ((y - 1) << 16) | (x - 1));
270 enable_lcd_clocks(0);
272 EXPORT_SYMBOL(omap_dispc_set_digit_size);
274 static void setup_plane_fifo(int plane, int ext_mode)
276 const u32 ftrs_reg[] = { DISPC_GFX_FIFO_THRESHOLD,
277 DISPC_VID1_BASE + DISPC_VID_FIFO_THRESHOLD,
278 DISPC_VID2_BASE + DISPC_VID_FIFO_THRESHOLD };
279 const u32 fsz_reg[] = { DISPC_GFX_FIFO_SIZE_STATUS,
280 DISPC_VID1_BASE + DISPC_VID_FIFO_SIZE_STATUS,
281 DISPC_VID2_BASE + DISPC_VID_FIFO_SIZE_STATUS };
287 l = dispc_read_reg(fsz_reg[plane]);
296 MOD_REG_FLD(ftrs_reg[plane], FLD_MASK(16, 9) | FLD_MASK(0, 9),
300 void omap_dispc_enable_lcd_out(int enable)
302 enable_lcd_clocks(1);
303 MOD_REG_FLD(DISPC_CONTROL, 1, enable ? 1 : 0);
304 enable_lcd_clocks(0);
306 EXPORT_SYMBOL(omap_dispc_enable_lcd_out);
308 void omap_dispc_enable_digit_out(int enable)
310 enable_lcd_clocks(1);
311 MOD_REG_FLD(DISPC_CONTROL, 1 << 1, enable ? 1 << 1 : 0);
312 enable_lcd_clocks(0);
314 EXPORT_SYMBOL(omap_dispc_enable_digit_out);
316 static inline int _setup_plane(int plane, int channel_out,
317 u32 paddr, int screen_width,
318 int pos_x, int pos_y, int width, int height,
321 const u32 at_reg[] = { DISPC_GFX_ATTRIBUTES,
322 DISPC_VID1_BASE + DISPC_VID_ATTRIBUTES,
323 DISPC_VID2_BASE + DISPC_VID_ATTRIBUTES };
324 const u32 ba_reg[] = { DISPC_GFX_BA0, DISPC_VID1_BASE + DISPC_VID_BA0,
325 DISPC_VID2_BASE + DISPC_VID_BA0 };
326 const u32 ps_reg[] = { DISPC_GFX_POSITION,
327 DISPC_VID1_BASE + DISPC_VID_POSITION,
328 DISPC_VID2_BASE + DISPC_VID_POSITION };
329 const u32 sz_reg[] = { DISPC_GFX_SIZE,
330 DISPC_VID1_BASE + DISPC_VID_PICTURE_SIZE,
331 DISPC_VID2_BASE + DISPC_VID_PICTURE_SIZE };
332 const u32 ri_reg[] = { DISPC_GFX_ROW_INC,
333 DISPC_VID1_BASE + DISPC_VID_ROW_INC,
334 DISPC_VID2_BASE + DISPC_VID_ROW_INC };
335 const u32 vs_reg[] = { 0, DISPC_VID1_BASE + DISPC_VID_SIZE,
336 DISPC_VID2_BASE + DISPC_VID_SIZE };
338 int chout_shift, burst_shift;
347 dev_dbg(dispc.fbdev->dev, "plane %d channel %d paddr %#08x scr_width %d"
348 " pos_x %d pos_y %d width %d height %d color_mode %d\n",
349 plane, channel_out, paddr, screen_width, pos_x, pos_y,
350 width, height, color_mode);
355 case OMAPFB_PLANE_GFX:
359 case OMAPFB_PLANE_VID1:
360 case OMAPFB_PLANE_VID2:
369 switch (channel_out) {
370 case OMAPFB_CHANNEL_OUT_LCD:
373 case OMAPFB_CHANNEL_OUT_DIGIT:
381 switch (color_mode) {
382 case OMAPFB_COLOR_RGB565:
383 color_code = DISPC_RGB_16_BPP;
386 case OMAPFB_COLOR_YUV422:
389 color_code = DISPC_UYVY_422;
393 case OMAPFB_COLOR_YUY422:
396 color_code = DISPC_YUV2_422;
404 l = dispc_read_reg(at_reg[plane]);
407 l |= color_code << 1;
411 l &= ~(0x03 << burst_shift);
412 l |= DISPC_BURST_8x32 << burst_shift;
414 l &= ~(1 << chout_shift);
415 l |= chout_val << chout_shift;
417 dispc_write_reg(at_reg[plane], l);
419 dispc_write_reg(ba_reg[plane], paddr);
420 MOD_REG_FLD(ps_reg[plane],
421 FLD_MASK(16, 11) | FLD_MASK(0, 11), (pos_y << 16) | pos_x);
423 MOD_REG_FLD(sz_reg[plane], FLD_MASK(16, 11) | FLD_MASK(0, 11),
424 ((height - 1) << 16) | (width - 1));
427 /* Set video size if set_scale hasn't set it */
428 if (!dispc.fir_vinc[plane])
429 MOD_REG_FLD(vs_reg[plane],
430 FLD_MASK(16, 11), (height - 1) << 16);
431 if (!dispc.fir_hinc[plane])
432 MOD_REG_FLD(vs_reg[plane],
433 FLD_MASK(0, 11), width - 1);
436 dispc_write_reg(ri_reg[plane], (screen_width - width) * bpp / 8 + 1);
438 return height * screen_width * bpp / 8;
441 static int omap_dispc_setup_plane(int plane, int channel_out,
442 unsigned long offset,
444 int pos_x, int pos_y, int width, int height,
450 if ((unsigned)plane > dispc.mem_desc.region_cnt)
452 paddr = dispc.mem_desc.region[plane].paddr + offset;
453 enable_lcd_clocks(1);
454 r = _setup_plane(plane, channel_out, paddr,
456 pos_x, pos_y, width, height, color_mode);
457 enable_lcd_clocks(0);
461 static void write_firh_reg(int plane, int reg, u32 value)
466 base = DISPC_VID1_BASE + DISPC_VID_FIR_COEF_H0;
468 base = DISPC_VID2_BASE + DISPC_VID_FIR_COEF_H0;
469 dispc_write_reg(base + reg * 8, value);
472 static void write_firhv_reg(int plane, int reg, u32 value)
477 base = DISPC_VID1_BASE + DISPC_VID_FIR_COEF_HV0;
479 base = DISPC_VID2_BASE + DISPC_VID_FIR_COEF_HV0;
480 dispc_write_reg(base + reg * 8, value);
483 static void set_upsampling_coef_table(int plane)
485 const u32 coef[][2] = {
486 { 0x00800000, 0x00800000 },
487 { 0x0D7CF800, 0x037B02FF },
488 { 0x1E70F5FF, 0x0C6F05FE },
489 { 0x335FF5FE, 0x205907FB },
490 { 0xF74949F7, 0x00404000 },
491 { 0xF55F33FB, 0x075920FE },
492 { 0xF5701EFE, 0x056F0CFF },
493 { 0xF87C0DFF, 0x027B0300 },
497 for (i = 0; i < 8; i++) {
498 write_firh_reg(plane, i, coef[i][0]);
499 write_firhv_reg(plane, i, coef[i][1]);
503 static int omap_dispc_set_scale(int plane,
504 int orig_width, int orig_height,
505 int out_width, int out_height)
507 const u32 at_reg[] = { 0, DISPC_VID1_BASE + DISPC_VID_ATTRIBUTES,
508 DISPC_VID2_BASE + DISPC_VID_ATTRIBUTES };
509 const u32 vs_reg[] = { 0, DISPC_VID1_BASE + DISPC_VID_SIZE,
510 DISPC_VID2_BASE + DISPC_VID_SIZE };
511 const u32 fir_reg[] = { 0, DISPC_VID1_BASE + DISPC_VID_FIR,
512 DISPC_VID2_BASE + DISPC_VID_FIR };
518 if ((unsigned)plane > OMAPFB_PLANE_NUM)
521 if (plane == OMAPFB_PLANE_GFX &&
522 (out_width != orig_width || out_height != orig_height))
525 enable_lcd_clocks(1);
526 if (orig_width < out_width) {
529 * Currently you can only scale both dimensions in one way.
531 if (orig_height > out_height ||
532 orig_width * 8 < out_width ||
533 orig_height * 8 < out_height) {
534 enable_lcd_clocks(0);
537 set_upsampling_coef_table(plane);
538 } else if (orig_width > out_width) {
539 /* Downsampling not yet supported
542 enable_lcd_clocks(0);
545 if (!orig_width || orig_width == out_width)
548 fir_hinc = 1024 * orig_width / out_width;
549 if (!orig_height || orig_height == out_height)
552 fir_vinc = 1024 * orig_height / out_height;
553 dispc.fir_hinc[plane] = fir_hinc;
554 dispc.fir_vinc[plane] = fir_vinc;
556 MOD_REG_FLD(fir_reg[plane],
557 FLD_MASK(16, 12) | FLD_MASK(0, 12),
558 ((fir_vinc & 4095) << 16) |
561 dev_dbg(dispc.fbdev->dev, "out_width %d out_height %d orig_width %d "
562 "orig_height %d fir_hinc %d fir_vinc %d\n",
563 out_width, out_height, orig_width, orig_height,
566 MOD_REG_FLD(vs_reg[plane],
567 FLD_MASK(16, 11) | FLD_MASK(0, 11),
568 ((out_height - 1) << 16) | (out_width - 1));
570 l = dispc_read_reg(at_reg[plane]);
572 l |= fir_hinc ? (1 << 5) : 0;
573 l |= fir_vinc ? (1 << 6) : 0;
574 dispc_write_reg(at_reg[plane], l);
576 enable_lcd_clocks(0);
580 static int omap_dispc_enable_plane(int plane, int enable)
582 const u32 at_reg[] = { DISPC_GFX_ATTRIBUTES,
583 DISPC_VID1_BASE + DISPC_VID_ATTRIBUTES,
584 DISPC_VID2_BASE + DISPC_VID_ATTRIBUTES };
585 if ((unsigned int)plane > dispc.mem_desc.region_cnt)
588 enable_lcd_clocks(1);
589 MOD_REG_FLD(at_reg[plane], 1, enable ? 1 : 0);
590 enable_lcd_clocks(0);
595 static int omap_dispc_set_color_key(struct omapfb_color_key *ck)
600 switch (ck->channel_out) {
601 case OMAPFB_CHANNEL_OUT_LCD:
602 df_reg = DISPC_DEFAULT_COLOR0;
603 tr_reg = DISPC_TRANS_COLOR0;
606 case OMAPFB_CHANNEL_OUT_DIGIT:
607 df_reg = DISPC_DEFAULT_COLOR1;
608 tr_reg = DISPC_TRANS_COLOR1;
614 switch (ck->key_type) {
615 case OMAPFB_COLOR_KEY_DISABLED:
618 case OMAPFB_COLOR_KEY_GFX_DST:
621 case OMAPFB_COLOR_KEY_VID_SRC:
627 enable_lcd_clocks(1);
628 MOD_REG_FLD(DISPC_CONFIG, FLD_MASK(shift, 2), val << shift);
631 dispc_write_reg(tr_reg, ck->trans_key);
632 dispc_write_reg(df_reg, ck->background);
633 enable_lcd_clocks(0);
635 dispc.color_key = *ck;
640 static int omap_dispc_get_color_key(struct omapfb_color_key *ck)
642 *ck = dispc.color_key;
646 static void load_palette(void)
650 static int omap_dispc_set_update_mode(enum omapfb_update_mode mode)
654 if (mode != dispc.update_mode) {
656 case OMAPFB_AUTO_UPDATE:
657 case OMAPFB_MANUAL_UPDATE:
658 enable_lcd_clocks(1);
659 omap_dispc_enable_lcd_out(1);
660 dispc.update_mode = mode;
662 case OMAPFB_UPDATE_DISABLED:
663 init_completion(&dispc.frame_done);
664 omap_dispc_enable_lcd_out(0);
665 if (!wait_for_completion_timeout(&dispc.frame_done,
666 msecs_to_jiffies(500))) {
667 dev_err(dispc.fbdev->dev,
668 "timeout waiting for FRAME DONE\n");
670 dispc.update_mode = mode;
671 enable_lcd_clocks(0);
681 static void omap_dispc_get_caps(int plane, struct omapfb_caps *caps)
683 caps->ctrl |= OMAPFB_CAPS_PLANE_RELOCATE_MEM;
685 caps->ctrl |= OMAPFB_CAPS_PLANE_SCALE;
686 caps->plane_color |= (1 << OMAPFB_COLOR_RGB565) |
687 (1 << OMAPFB_COLOR_YUV422) |
688 (1 << OMAPFB_COLOR_YUY422);
690 caps->plane_color |= (1 << OMAPFB_COLOR_CLUT_8BPP) |
691 (1 << OMAPFB_COLOR_CLUT_4BPP) |
692 (1 << OMAPFB_COLOR_CLUT_2BPP) |
693 (1 << OMAPFB_COLOR_CLUT_1BPP) |
694 (1 << OMAPFB_COLOR_RGB444);
697 static enum omapfb_update_mode omap_dispc_get_update_mode(void)
699 return dispc.update_mode;
702 static void setup_color_conv_coef(void)
704 u32 mask = FLD_MASK(16, 11) | FLD_MASK(0, 11);
705 int cf1_reg = DISPC_VID1_BASE + DISPC_VID_CONV_COEF0;
706 int cf2_reg = DISPC_VID2_BASE + DISPC_VID_CONV_COEF0;
707 int at1_reg = DISPC_VID1_BASE + DISPC_VID_ATTRIBUTES;
708 int at2_reg = DISPC_VID2_BASE + DISPC_VID_ATTRIBUTES;
709 const struct color_conv_coef {
710 int ry, rcr, rcb, gy, gcr, gcb, by, bcr, bcb;
713 298, 409, 0, 298, -208, -100, 298, 0, 517, 0,
715 const struct color_conv_coef *ct;
716 #define CVAL(x, y) (((x & 2047) << 16) | (y & 2047))
720 MOD_REG_FLD(cf1_reg, mask, CVAL(ct->rcr, ct->ry));
721 MOD_REG_FLD(cf1_reg + 4, mask, CVAL(ct->gy, ct->rcb));
722 MOD_REG_FLD(cf1_reg + 8, mask, CVAL(ct->gcb, ct->gcr));
723 MOD_REG_FLD(cf1_reg + 12, mask, CVAL(ct->bcr, ct->by));
724 MOD_REG_FLD(cf1_reg + 16, mask, CVAL(0, ct->bcb));
726 MOD_REG_FLD(cf2_reg, mask, CVAL(ct->rcr, ct->ry));
727 MOD_REG_FLD(cf2_reg + 4, mask, CVAL(ct->gy, ct->rcb));
728 MOD_REG_FLD(cf2_reg + 8, mask, CVAL(ct->gcb, ct->gcr));
729 MOD_REG_FLD(cf2_reg + 12, mask, CVAL(ct->bcr, ct->by));
730 MOD_REG_FLD(cf2_reg + 16, mask, CVAL(0, ct->bcb));
733 MOD_REG_FLD(at1_reg, (1 << 11), ct->full_range);
734 MOD_REG_FLD(at2_reg, (1 << 11), ct->full_range);
737 static void calc_ck_div(int is_tft, int pck, int *lck_div, int *pck_div)
739 unsigned long fck, lck;
743 fck = clk_get_rate(dispc.dss1_fck);
745 *pck_div = (lck + pck - 1) / pck;
747 *pck_div = max(2, *pck_div);
749 *pck_div = max(3, *pck_div);
750 if (*pck_div > 255) {
752 lck = pck * *pck_div;
753 *lck_div = fck / lck;
754 BUG_ON(*lck_div < 1);
755 if (*lck_div > 255) {
757 dev_warn(dispc.fbdev->dev, "pixclock %d kHz too low.\n",
763 static void set_lcd_tft_mode(int enable)
768 MOD_REG_FLD(DISPC_CONTROL, mask, enable ? mask : 0);
771 static void set_lcd_timings(void)
774 int lck_div, pck_div;
775 struct lcd_panel *panel = dispc.fbdev->panel;
776 int is_tft = panel->config & OMAP_LCDC_PANEL_TFT;
779 l = dispc_read_reg(DISPC_TIMING_H);
780 l &= ~(FLD_MASK(0, 6) | FLD_MASK(8, 8) | FLD_MASK(20, 8));
781 l |= ( max(1, (min(64, panel->hsw))) - 1 ) << 0;
782 l |= ( max(1, (min(256, panel->hfp))) - 1 ) << 8;
783 l |= ( max(1, (min(256, panel->hbp))) - 1 ) << 20;
784 dispc_write_reg(DISPC_TIMING_H, l);
786 l = dispc_read_reg(DISPC_TIMING_V);
787 l &= ~(FLD_MASK(0, 6) | FLD_MASK(8, 8) | FLD_MASK(20, 8));
788 l |= ( max(1, (min(64, panel->vsw))) - 1 ) << 0;
789 l |= ( max(0, (min(255, panel->vfp))) - 0 ) << 8;
790 l |= ( max(0, (min(255, panel->vbp))) - 0 ) << 20;
791 dispc_write_reg(DISPC_TIMING_V, l);
793 l = dispc_read_reg(DISPC_POL_FREQ);
794 l &= ~FLD_MASK(12, 6);
795 l |= (panel->config & OMAP_LCDC_SIGNAL_MASK) << 12;
796 l |= panel->acb & 0xff;
797 dispc_write_reg(DISPC_POL_FREQ, l);
799 calc_ck_div(is_tft, panel->pixel_clock * 1000, &lck_div, &pck_div);
801 l = dispc_read_reg(DISPC_DIVISOR);
802 l &= ~(FLD_MASK(16, 8) | FLD_MASK(0, 8));
803 l |= (lck_div << 16) | (pck_div << 0);
804 dispc_write_reg(DISPC_DIVISOR, l);
806 /* update panel info with the exact clock */
807 fck = clk_get_rate(dispc.dss1_fck);
808 panel->pixel_clock = fck / lck_div / pck_div / 1000;
811 int omap_dispc_request_irq(void (*callback)(void *data), void *data)
815 BUG_ON(callback == NULL);
817 if (dispc.irq_callback)
820 dispc.irq_callback = callback;
821 dispc.irq_callback_data = data;
826 EXPORT_SYMBOL(omap_dispc_request_irq);
828 void omap_dispc_enable_irqs(int irq_mask)
830 enable_lcd_clocks(1);
831 dispc.enabled_irqs = irq_mask;
832 irq_mask |= DISPC_IRQ_MASK_ERROR;
833 MOD_REG_FLD(DISPC_IRQENABLE, 0x7fff, irq_mask);
834 enable_lcd_clocks(0);
836 EXPORT_SYMBOL(omap_dispc_enable_irqs);
838 void omap_dispc_disable_irqs(int irq_mask)
840 enable_lcd_clocks(1);
841 dispc.enabled_irqs &= ~irq_mask;
842 irq_mask &= ~DISPC_IRQ_MASK_ERROR;
843 MOD_REG_FLD(DISPC_IRQENABLE, 0x7fff, irq_mask);
844 enable_lcd_clocks(0);
846 EXPORT_SYMBOL(omap_dispc_disable_irqs);
848 void omap_dispc_free_irq(void)
850 enable_lcd_clocks(1);
851 omap_dispc_disable_irqs(DISPC_IRQ_MASK_ALL);
852 dispc.irq_callback = NULL;
853 dispc.irq_callback_data = NULL;
854 enable_lcd_clocks(0);
856 EXPORT_SYMBOL(omap_dispc_free_irq);
858 static irqreturn_t omap_dispc_irq_handler(int irq, void *dev)
860 u32 stat = dispc_read_reg(DISPC_IRQSTATUS);
862 if (stat & DISPC_IRQ_FRAMEMASK)
863 complete(&dispc.frame_done);
865 if (stat & DISPC_IRQ_MASK_ERROR) {
866 if (printk_ratelimit()) {
867 dev_err(dispc.fbdev->dev, "irq error status %04x\n",
872 if ((stat & dispc.enabled_irqs) && dispc.irq_callback)
873 dispc.irq_callback(dispc.irq_callback_data);
875 dispc_write_reg(DISPC_IRQSTATUS, stat);
880 static int get_dss_clocks(void)
882 if (IS_ERR((dispc.dss_ick = clk_get(dispc.fbdev->dev, "dss_ick")))) {
883 dev_err(dispc.fbdev->dev, "can't get dss_ick");
884 return PTR_ERR(dispc.dss_ick);
887 if (IS_ERR((dispc.dss1_fck = clk_get(dispc.fbdev->dev, "dss1_fck")))) {
888 dev_err(dispc.fbdev->dev, "can't get dss1_fck");
889 clk_put(dispc.dss_ick);
890 return PTR_ERR(dispc.dss1_fck);
893 if (IS_ERR((dispc.dss_54m_fck =
894 clk_get(dispc.fbdev->dev, "dss_54m_fck")))) {
895 dev_err(dispc.fbdev->dev, "can't get dss_54m_fck");
896 clk_put(dispc.dss_ick);
897 clk_put(dispc.dss1_fck);
898 return PTR_ERR(dispc.dss_54m_fck);
904 static void put_dss_clocks(void)
906 clk_put(dispc.dss_54m_fck);
907 clk_put(dispc.dss1_fck);
908 clk_put(dispc.dss_ick);
911 static void enable_lcd_clocks(int enable)
914 clk_enable(dispc.dss1_fck);
916 clk_disable(dispc.dss1_fck);
919 static void enable_interface_clocks(int enable)
922 clk_enable(dispc.dss_ick);
924 clk_disable(dispc.dss_ick);
927 static void enable_digit_clocks(int enable)
930 clk_enable(dispc.dss_54m_fck);
932 clk_disable(dispc.dss_54m_fck);
935 static void omap_dispc_suspend(void)
937 if (dispc.update_mode == OMAPFB_AUTO_UPDATE) {
938 init_completion(&dispc.frame_done);
939 omap_dispc_enable_lcd_out(0);
940 if (!wait_for_completion_timeout(&dispc.frame_done,
941 msecs_to_jiffies(500))) {
942 dev_err(dispc.fbdev->dev,
943 "timeout waiting for FRAME DONE\n");
945 enable_lcd_clocks(0);
949 static void omap_dispc_resume(void)
951 if (dispc.update_mode == OMAPFB_AUTO_UPDATE) {
952 enable_lcd_clocks(1);
953 if (!dispc.ext_mode) {
957 omap_dispc_enable_lcd_out(1);
962 static int omap_dispc_update_window(struct fb_info *fbi,
963 struct omapfb_update_window *win,
964 void (*complete_callback)(void *arg),
965 void *complete_callback_data)
967 return dispc.update_mode == OMAPFB_UPDATE_DISABLED ? -ENODEV : 0;
970 static int mmap_kern(struct omapfb_mem_region *region)
972 struct vm_struct *kvma;
973 struct vm_area_struct vma;
977 kvma = get_vm_area(region->size, VM_IOREMAP);
979 dev_err(dispc.fbdev->dev, "can't get kernel vm area\n");
982 vma.vm_mm = &init_mm;
984 vaddr = (unsigned long)kvma->addr;
986 pgprot = pgprot_writecombine(pgprot_kernel);
987 vma.vm_start = vaddr;
988 vma.vm_end = vaddr + region->size;
989 if (io_remap_pfn_range(&vma, vaddr, region->paddr >> PAGE_SHIFT,
990 region->size, pgprot) < 0) {
991 dev_err(dispc.fbdev->dev, "kernel mmap for FBMEM failed\n");
994 region->vaddr = (void *)vaddr;
999 static void mmap_user_open(struct vm_area_struct *vma)
1001 int plane = (int)vma->vm_private_data;
1003 atomic_inc(&dispc.map_count[plane]);
1006 static void mmap_user_close(struct vm_area_struct *vma)
1008 int plane = (int)vma->vm_private_data;
1010 atomic_dec(&dispc.map_count[plane]);
1013 static struct vm_operations_struct mmap_user_ops = {
1014 .open = mmap_user_open,
1015 .close = mmap_user_close,
1018 static int omap_dispc_mmap_user(struct fb_info *info,
1019 struct vm_area_struct *vma)
1021 struct omapfb_plane_struct *plane = info->par;
1023 unsigned long start;
1026 if (vma->vm_end - vma->vm_start == 0)
1028 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
1030 off = vma->vm_pgoff << PAGE_SHIFT;
1032 start = info->fix.smem_start;
1033 len = info->fix.smem_len;
1036 if ((vma->vm_end - vma->vm_start + off) > len)
1039 vma->vm_pgoff = off >> PAGE_SHIFT;
1040 vma->vm_flags |= VM_IO | VM_RESERVED;
1041 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1042 vma->vm_ops = &mmap_user_ops;
1043 vma->vm_private_data = (void *)plane->idx;
1044 if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
1045 vma->vm_end - vma->vm_start, vma->vm_page_prot))
1047 /* vm_ops.open won't be called for mmap itself. */
1048 atomic_inc(&dispc.map_count[plane->idx]);
1052 static void unmap_kern(struct omapfb_mem_region *region)
1054 vunmap(region->vaddr);
1057 static int alloc_palette_ram(void)
1059 dispc.palette_vaddr = dma_alloc_writecombine(dispc.fbdev->dev,
1060 MAX_PALETTE_SIZE, &dispc.palette_paddr, GFP_KERNEL);
1061 if (dispc.palette_vaddr == NULL) {
1062 dev_err(dispc.fbdev->dev, "failed to alloc palette memory\n");
1069 static void free_palette_ram(void)
1071 dma_free_writecombine(dispc.fbdev->dev, MAX_PALETTE_SIZE,
1072 dispc.palette_vaddr, dispc.palette_paddr);
1075 static int alloc_fbmem(struct omapfb_mem_region *region)
1077 region->vaddr = dma_alloc_writecombine(dispc.fbdev->dev,
1078 region->size, ®ion->paddr, GFP_KERNEL);
1080 if (region->vaddr == NULL) {
1081 dev_err(dispc.fbdev->dev, "unable to allocate FB DMA memory\n");
1088 static void free_fbmem(struct omapfb_mem_region *region)
1090 dma_free_writecombine(dispc.fbdev->dev, region->size,
1091 region->vaddr, region->paddr);
1094 static struct resmap *init_resmap(unsigned long start, size_t size)
1097 struct resmap *res_map;
1099 page_cnt = PAGE_ALIGN(size) / PAGE_SIZE;
1101 kzalloc(sizeof(struct resmap) + RESMAP_SIZE(page_cnt), GFP_KERNEL);
1102 if (res_map == NULL)
1104 res_map->start = start;
1105 res_map->page_cnt = page_cnt;
1106 res_map->map = (unsigned long *)(res_map + 1);
1110 static void cleanup_resmap(struct resmap *res_map)
1115 static inline int resmap_mem_type(unsigned long start)
1117 if (start >= OMAP2_SRAM_START &&
1118 start < OMAP2_SRAM_START + OMAP2_SRAM_SIZE)
1119 return OMAPFB_MEMTYPE_SRAM;
1121 return OMAPFB_MEMTYPE_SDRAM;
1124 static inline int resmap_page_reserved(struct resmap *res_map, unsigned page_nr)
1126 return *RESMAP_PTR(res_map, page_nr) & RESMAP_MASK(page_nr) ? 1 : 0;
1129 static inline void resmap_reserve_page(struct resmap *res_map, unsigned page_nr)
1131 BUG_ON(resmap_page_reserved(res_map, page_nr));
1132 *RESMAP_PTR(res_map, page_nr) |= RESMAP_MASK(page_nr);
1135 static inline void resmap_free_page(struct resmap *res_map, unsigned page_nr)
1137 BUG_ON(!resmap_page_reserved(res_map, page_nr));
1138 *RESMAP_PTR(res_map, page_nr) &= ~RESMAP_MASK(page_nr);
1141 static void resmap_reserve_region(unsigned long start, size_t size)
1144 struct resmap *res_map;
1145 unsigned start_page;
1150 mtype = resmap_mem_type(start);
1151 res_map = dispc.res_map[mtype];
1152 dev_dbg(dispc.fbdev->dev, "reserve mem type %d start %08lx size %d\n",
1153 mtype, start, size);
1154 start_page = (start - res_map->start) / PAGE_SIZE;
1155 end_page = start_page + PAGE_ALIGN(size) / PAGE_SIZE;
1156 for (i = start_page; i < end_page; i++)
1157 resmap_reserve_page(res_map, i);
1160 static void resmap_free_region(unsigned long start, size_t size)
1162 struct resmap *res_map;
1163 unsigned start_page;
1168 mtype = resmap_mem_type(start);
1169 res_map = dispc.res_map[mtype];
1170 dev_dbg(dispc.fbdev->dev, "free mem type %d start %08lx size %d\n",
1171 mtype, start, size);
1172 start_page = (start - res_map->start) / PAGE_SIZE;
1173 end_page = start_page + PAGE_ALIGN(size) / PAGE_SIZE;
1174 for (i = start_page; i < end_page; i++)
1175 resmap_free_page(res_map, i);
1178 static unsigned long resmap_alloc_region(int mtype, size_t size)
1182 unsigned start_page;
1183 unsigned long start;
1184 struct resmap *res_map = dispc.res_map[mtype];
1186 BUG_ON(mtype >= DISPC_MEMTYPE_NUM || res_map == NULL || !size);
1188 size = PAGE_ALIGN(size) / PAGE_SIZE;
1191 for (i = 0; i < res_map->page_cnt; i++) {
1192 if (resmap_page_reserved(res_map, i)) {
1195 } else if (++total == size)
1201 start = res_map->start + start_page * PAGE_SIZE;
1202 resmap_reserve_region(start, size * PAGE_SIZE);
1207 /* Note that this will only work for user mappings, we don't deal with
1208 * kernel mappings here, so fbcon will keep using the old region.
1210 static int omap_dispc_setup_mem(int plane, size_t size, int mem_type,
1211 unsigned long *paddr)
1213 struct omapfb_mem_region *rg;
1214 unsigned long new_addr = 0;
1216 if ((unsigned)plane > dispc.mem_desc.region_cnt)
1218 if (mem_type >= DISPC_MEMTYPE_NUM)
1220 if (dispc.res_map[mem_type] == NULL)
1222 rg = &dispc.mem_desc.region[plane];
1223 if (size == rg->size && mem_type == rg->type)
1225 if (atomic_read(&dispc.map_count[plane]))
1228 resmap_free_region(rg->paddr, rg->size);
1230 new_addr = resmap_alloc_region(mem_type, size);
1232 /* Reallocate old region. */
1233 resmap_reserve_region(rg->paddr, rg->size);
1237 rg->paddr = new_addr;
1239 rg->type = mem_type;
1246 static int setup_fbmem(struct omapfb_mem_desc *req_md)
1248 struct omapfb_mem_region *rg;
1251 unsigned long mem_start[DISPC_MEMTYPE_NUM];
1252 unsigned long mem_end[DISPC_MEMTYPE_NUM];
1254 if (!req_md->region_cnt) {
1255 dev_err(dispc.fbdev->dev, "no memory regions defined\n");
1259 rg = &req_md->region[0];
1260 memset(mem_start, 0xff, sizeof(mem_start));
1261 memset(mem_end, 0, sizeof(mem_end));
1263 for (i = 0; i < req_md->region_cnt; i++, rg++) {
1267 if (rg->vaddr == NULL) {
1269 if ((r = mmap_kern(rg)) < 0)
1273 if (rg->type != OMAPFB_MEMTYPE_SDRAM) {
1274 dev_err(dispc.fbdev->dev,
1275 "unsupported memory type\n");
1278 rg->alloc = rg->map = 1;
1279 if ((r = alloc_fbmem(rg)) < 0)
1284 if (rg->paddr < mem_start[mtype])
1285 mem_start[mtype] = rg->paddr;
1286 if (rg->paddr + rg->size > mem_end[mtype])
1287 mem_end[mtype] = rg->paddr + rg->size;
1290 for (i = 0; i < DISPC_MEMTYPE_NUM; i++) {
1291 unsigned long start;
1293 if (mem_end[i] == 0)
1295 start = mem_start[i];
1296 size = mem_end[i] - start;
1297 dispc.res_map[i] = init_resmap(start, size);
1299 if (dispc.res_map[i] == NULL)
1301 /* Initial state is that everything is reserved. This
1302 * includes possible holes as well, which will never be
1305 resmap_reserve_region(start, size);
1308 dispc.mem_desc = *req_md;
1312 for (i = 0; i < DISPC_MEMTYPE_NUM; i++) {
1313 if (dispc.res_map[i] != NULL)
1314 cleanup_resmap(dispc.res_map[i]);
1319 static void cleanup_fbmem(void)
1321 struct omapfb_mem_region *rg;
1324 for (i = 0; i < DISPC_MEMTYPE_NUM; i++) {
1325 if (dispc.res_map[i] != NULL)
1326 cleanup_resmap(dispc.res_map[i]);
1328 rg = &dispc.mem_desc.region[0];
1329 for (i = 0; i < dispc.mem_desc.region_cnt; i++, rg++) {
1339 static int omap_dispc_init(struct omapfb_device *fbdev, int ext_mode,
1340 struct omapfb_mem_desc *req_vram)
1344 struct lcd_panel *panel = fbdev->panel;
1349 memset(&dispc, 0, sizeof(dispc));
1351 dispc.base = io_p2v(DISPC_BASE);
1352 dispc.fbdev = fbdev;
1353 dispc.ext_mode = ext_mode;
1355 init_completion(&dispc.frame_done);
1357 if ((r = get_dss_clocks()) < 0)
1360 enable_interface_clocks(1);
1361 enable_lcd_clocks(1);
1363 #ifdef CONFIG_FB_OMAP_BOOTLOADER_INIT
1364 l = dispc_read_reg(DISPC_CONTROL);
1367 pr_info("omapfb: skipping hardware initialization\n");
1373 /* Reset monitoring works only w/ the 54M clk */
1374 enable_digit_clocks(1);
1377 MOD_REG_FLD(DISPC_SYSCONFIG, 1 << 1, 1 << 1);
1379 while (!(dispc_read_reg(DISPC_SYSSTATUS) & 1)) {
1381 dev_err(dispc.fbdev->dev, "soft reset failed\n");
1383 enable_digit_clocks(0);
1388 enable_digit_clocks(0);
1391 /* Enable smart idle and autoidle */
1392 l = dispc_read_reg(DISPC_CONTROL);
1393 l &= ~((3 << 12) | (3 << 3));
1394 l |= (2 << 12) | (2 << 3) | (1 << 0);
1395 dispc_write_reg(DISPC_SYSCONFIG, l);
1396 omap_writel(1 << 0, DSS_BASE + DSS_SYSCONFIG);
1398 /* Set functional clock autogating */
1399 l = dispc_read_reg(DISPC_CONFIG);
1401 dispc_write_reg(DISPC_CONFIG, l);
1403 l = dispc_read_reg(DISPC_IRQSTATUS);
1404 dispc_write_reg(l, DISPC_IRQSTATUS);
1406 /* Enable those that we handle always */
1407 omap_dispc_enable_irqs(DISPC_IRQ_FRAMEMASK);
1409 if ((r = request_irq(INT_24XX_DSS_IRQ, omap_dispc_irq_handler,
1410 0, MODULE_NAME, fbdev)) < 0) {
1411 dev_err(dispc.fbdev->dev, "can't get DSS IRQ\n");
1415 /* L3 firewall setting: enable access to OCM RAM */
1416 __raw_writel(0x402000b0, io_p2v(0x680050a0));
1418 if ((r = alloc_palette_ram()) < 0)
1421 if ((r = setup_fbmem(req_vram)) < 0)
1425 for (i = 0; i < dispc.mem_desc.region_cnt; i++) {
1426 memset(dispc.mem_desc.region[i].vaddr, 0,
1427 dispc.mem_desc.region[i].size);
1430 /* Set logic clock to fck, pixel clock to fck/2 for now */
1431 MOD_REG_FLD(DISPC_DIVISOR, FLD_MASK(16, 8), 1 << 16);
1432 MOD_REG_FLD(DISPC_DIVISOR, FLD_MASK(0, 8), 2 << 0);
1434 setup_plane_fifo(0, ext_mode);
1435 setup_plane_fifo(1, ext_mode);
1436 setup_plane_fifo(2, ext_mode);
1438 setup_color_conv_coef();
1440 set_lcd_tft_mode(panel->config & OMAP_LCDC_PANEL_TFT);
1441 set_load_mode(DISPC_LOAD_FRAME_ONLY);
1444 set_lcd_data_lines(panel->data_lines);
1445 omap_dispc_set_lcd_size(panel->x_res, panel->y_res);
1448 set_lcd_data_lines(panel->bpp);
1449 enable_rfbi_mode(ext_mode);
1452 l = dispc_read_reg(DISPC_REVISION);
1453 pr_info("omapfb: DISPC version %d.%d initialized\n",
1454 l >> 4 & 0x0f, l & 0x0f);
1455 enable_lcd_clocks(0);
1461 free_irq(INT_24XX_DSS_IRQ, fbdev);
1463 enable_lcd_clocks(0);
1464 enable_interface_clocks(0);
1470 static void omap_dispc_cleanup(void)
1474 omap_dispc_set_update_mode(OMAPFB_UPDATE_DISABLED);
1475 /* This will also disable clocks that are on */
1476 for (i = 0; i < dispc.mem_desc.region_cnt; i++)
1477 omap_dispc_enable_plane(i, 0);
1480 free_irq(INT_24XX_DSS_IRQ, dispc.fbdev);
1481 enable_interface_clocks(0);
1485 const struct lcd_ctrl omap2_int_ctrl = {
1487 .init = omap_dispc_init,
1488 .cleanup = omap_dispc_cleanup,
1489 .get_caps = omap_dispc_get_caps,
1490 .set_update_mode = omap_dispc_set_update_mode,
1491 .get_update_mode = omap_dispc_get_update_mode,
1492 .update_window = omap_dispc_update_window,
1493 .suspend = omap_dispc_suspend,
1494 .resume = omap_dispc_resume,
1495 .setup_plane = omap_dispc_setup_plane,
1496 .setup_mem = omap_dispc_setup_mem,
1497 .set_scale = omap_dispc_set_scale,
1498 .enable_plane = omap_dispc_enable_plane,
1499 .set_color_key = omap_dispc_set_color_key,
1500 .get_color_key = omap_dispc_get_color_key,
1501 .mmap = omap_dispc_mmap_user,