2 * OMAP1 internal LCD controller
4 * Copyright (C) 2004 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/module.h>
22 #include <linux/device.h>
23 #include <linux/interrupt.h>
24 #include <linux/spinlock.h>
25 #include <linux/err.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/vmalloc.h>
30 #include <linux/clk.h>
31 #include <linux/gfp.h>
33 #include <mach/lcdc.h>
36 #include <asm/mach-types.h>
42 #define MODULE_NAME "lcdc"
44 #define MAX_PALETTE_SIZE PAGE_SIZE
47 OMAP_LCDC_LOAD_PALETTE
,
49 OMAP_LCDC_LOAD_PALETTE_AND_FRAME
52 static struct omap_lcd_controller
{
53 enum omapfb_update_mode update_mode
;
56 unsigned long frame_offset
;
61 enum omapfb_color_format color_mode
;
64 dma_addr_t palette_phys
;
68 unsigned int irq_mask
;
69 struct completion last_frame_complete
;
70 struct completion palette_load_complete
;
72 struct omapfb_device
*fbdev
;
74 void (*dma_callback
)(void *data
);
75 void *dma_callback_data
;
80 unsigned long vram_size
;
83 static void inline enable_irqs(int mask
)
85 lcdc
.irq_mask
|= mask
;
88 static void inline disable_irqs(int mask
)
90 lcdc
.irq_mask
&= ~mask
;
93 static void set_load_mode(enum lcdc_load_mode mode
)
97 l
= omap_readl(OMAP_LCDC_CONTROL
);
100 case OMAP_LCDC_LOAD_PALETTE
:
103 case OMAP_LCDC_LOAD_FRAME
:
106 case OMAP_LCDC_LOAD_PALETTE_AND_FRAME
:
111 omap_writel(l
, OMAP_LCDC_CONTROL
);
114 static void enable_controller(void)
118 l
= omap_readl(OMAP_LCDC_CONTROL
);
119 l
|= OMAP_LCDC_CTRL_LCD_EN
;
120 l
&= ~OMAP_LCDC_IRQ_MASK
;
121 l
|= lcdc
.irq_mask
| OMAP_LCDC_IRQ_DONE
; /* enabled IRQs */
122 omap_writel(l
, OMAP_LCDC_CONTROL
);
125 static void disable_controller_async(void)
130 l
= omap_readl(OMAP_LCDC_CONTROL
);
131 mask
= OMAP_LCDC_CTRL_LCD_EN
| OMAP_LCDC_IRQ_MASK
;
133 * Preserve the DONE mask, since we still want to get the
134 * final DONE irq. It will be disabled in the IRQ handler.
136 mask
&= ~OMAP_LCDC_IRQ_DONE
;
138 omap_writel(l
, OMAP_LCDC_CONTROL
);
141 static void disable_controller(void)
143 init_completion(&lcdc
.last_frame_complete
);
144 disable_controller_async();
145 if (!wait_for_completion_timeout(&lcdc
.last_frame_complete
,
146 msecs_to_jiffies(500)))
147 dev_err(lcdc
.fbdev
->dev
, "timeout waiting for FRAME DONE\n");
150 static void reset_controller(u32 status
)
152 static unsigned long reset_count
;
153 static unsigned long last_jiffies
;
155 disable_controller_async();
157 if (reset_count
== 1 || time_after(jiffies
, last_jiffies
+ HZ
)) {
158 dev_err(lcdc
.fbdev
->dev
,
159 "resetting (status %#010x,reset count %lu)\n",
160 status
, reset_count
);
161 last_jiffies
= jiffies
;
163 if (reset_count
< 100) {
167 dev_err(lcdc
.fbdev
->dev
,
168 "too many reset attempts, giving up.\n");
173 * Configure the LCD DMA according to the current mode specified by parameters
174 * in lcdc.fbdev and fbdev->var.
176 static void setup_lcd_dma(void)
178 static const int dma_elem_type
[] = {
180 OMAP_DMA_DATA_TYPE_S8
,
181 OMAP_DMA_DATA_TYPE_S16
,
183 OMAP_DMA_DATA_TYPE_S32
,
185 struct omapfb_plane_struct
*plane
= lcdc
.fbdev
->fb_info
[0]->par
;
186 struct fb_var_screeninfo
*var
= &lcdc
.fbdev
->fb_info
[0]->var
;
188 int esize
, xelem
, yelem
;
190 src
= lcdc
.vram_phys
+ lcdc
.frame_offset
;
192 switch (var
->rotate
) {
194 if (plane
->info
.mirror
|| (src
& 3) ||
195 lcdc
.color_mode
== OMAPFB_COLOR_YUV420
||
200 xelem
= lcdc
.xres
* lcdc
.bpp
/ 8 / esize
;
206 if (cpu_is_omap15xx()) {
210 xelem
= lcdc
.yres
* lcdc
.bpp
/ 16;
218 dev_dbg(lcdc
.fbdev
->dev
,
219 "setup_dma: src %#010lx esize %d xelem %d yelem %d\n",
220 src
, esize
, xelem
, yelem
);
222 omap_set_lcd_dma_b1(src
, xelem
, yelem
, dma_elem_type
[esize
]);
223 if (!cpu_is_omap15xx()) {
227 * YUV support is only for external mode when we have the
228 * YUV window embedded in a 16bpp frame buffer.
230 if (lcdc
.color_mode
== OMAPFB_COLOR_YUV420
)
232 /* Set virtual xres elem size */
233 omap_set_lcd_dma_b1_vxres(
234 lcdc
.screen_width
* bpp
/ 8 / esize
);
235 /* Setup transformations */
236 omap_set_lcd_dma_b1_rotation(var
->rotate
);
237 omap_set_lcd_dma_b1_mirror(plane
->info
.mirror
);
239 omap_setup_lcd_dma();
242 static irqreturn_t
lcdc_irq_handler(int irq
, void *dev_id
)
246 status
= omap_readl(OMAP_LCDC_STATUS
);
248 if (status
& (OMAP_LCDC_STAT_FUF
| OMAP_LCDC_STAT_SYNC_LOST
))
249 reset_controller(status
);
251 if (status
& OMAP_LCDC_STAT_DONE
) {
255 * Disable IRQ_DONE. The status bit will be cleared
256 * only when the controller is reenabled and we don't
257 * want to get more interrupts.
259 l
= omap_readl(OMAP_LCDC_CONTROL
);
260 l
&= ~OMAP_LCDC_IRQ_DONE
;
261 omap_writel(l
, OMAP_LCDC_CONTROL
);
262 complete(&lcdc
.last_frame_complete
);
264 if (status
& OMAP_LCDC_STAT_LOADED_PALETTE
) {
265 disable_controller_async();
266 complete(&lcdc
.palette_load_complete
);
271 * Clear these interrupt status bits.
272 * Sync_lost, FUF bits were cleared by disabling the LCD controller
273 * LOADED_PALETTE can be cleared this way only in palette only
274 * load mode. In other load modes it's cleared by disabling the
277 status
&= ~(OMAP_LCDC_STAT_VSYNC
|
278 OMAP_LCDC_STAT_LOADED_PALETTE
|
280 OMAP_LCDC_STAT_LINE_INT
);
281 omap_writel(status
, OMAP_LCDC_STATUS
);
286 * Change to a new video mode. We defer this to a later time to avoid any
287 * flicker and not to mess up the current LCD DMA context. For this we disable
288 * the LCD controller, which will generate a DONE irq after the last frame has
289 * been transferred. Then it'll be safe to reconfigure both the LCD controller
290 * as well as the LCD DMA.
292 static int omap_lcdc_setup_plane(int plane
, int channel_out
,
293 unsigned long offset
, int screen_width
,
294 int pos_x
, int pos_y
, int width
, int height
,
297 struct fb_var_screeninfo
*var
= &lcdc
.fbdev
->fb_info
[0]->var
;
298 struct lcd_panel
*panel
= lcdc
.fbdev
->panel
;
301 if (var
->rotate
== 0) {
302 rot_x
= panel
->x_res
;
303 rot_y
= panel
->y_res
;
305 rot_x
= panel
->y_res
;
306 rot_y
= panel
->x_res
;
308 if (plane
!= 0 || channel_out
!= 0 || pos_x
!= 0 || pos_y
!= 0 ||
309 width
> rot_x
|| height
> rot_y
) {
311 dev_dbg(lcdc
.fbdev
->dev
,
312 "invalid plane params plane %d pos_x %d pos_y %d "
313 "w %d h %d\n", plane
, pos_x
, pos_y
, width
, height
);
318 lcdc
.frame_offset
= offset
;
321 lcdc
.screen_width
= screen_width
;
322 lcdc
.color_mode
= color_mode
;
324 switch (color_mode
) {
325 case OMAPFB_COLOR_CLUT_8BPP
:
327 lcdc
.palette_code
= 0x3000;
328 lcdc
.palette_size
= 512;
330 case OMAPFB_COLOR_RGB565
:
332 lcdc
.palette_code
= 0x4000;
333 lcdc
.palette_size
= 32;
335 case OMAPFB_COLOR_RGB444
:
337 lcdc
.palette_code
= 0x4000;
338 lcdc
.palette_size
= 32;
340 case OMAPFB_COLOR_YUV420
:
346 case OMAPFB_COLOR_YUV422
:
353 /* FIXME: other BPPs.
354 * bpp1: code 0, size 256
355 * bpp2: code 0x1000 size 256
356 * bpp4: code 0x2000 size 256
357 * bpp12: code 0x4000 size 32
359 dev_dbg(lcdc
.fbdev
->dev
, "invalid color mode %d\n", color_mode
);
369 if (lcdc
.update_mode
== OMAPFB_AUTO_UPDATE
) {
370 disable_controller();
379 static int omap_lcdc_enable_plane(int plane
, int enable
)
381 dev_dbg(lcdc
.fbdev
->dev
,
382 "plane %d enable %d update_mode %d ext_mode %d\n",
383 plane
, enable
, lcdc
.update_mode
, lcdc
.ext_mode
);
384 if (plane
!= OMAPFB_PLANE_GFX
)
391 * Configure the LCD DMA for a palette load operation and do the palette
392 * downloading synchronously. We don't use the frame+palette load mode of
393 * the controller, since the palette can always be downloaded separately.
395 static void load_palette(void)
399 palette
= (u16
*)lcdc
.palette_virt
;
401 *(u16
*)palette
&= 0x0fff;
402 *(u16
*)palette
|= lcdc
.palette_code
;
404 omap_set_lcd_dma_b1(lcdc
.palette_phys
,
405 lcdc
.palette_size
/ 4 + 1, 1, OMAP_DMA_DATA_TYPE_S32
);
407 omap_set_lcd_dma_single_transfer(1);
408 omap_setup_lcd_dma();
410 init_completion(&lcdc
.palette_load_complete
);
411 enable_irqs(OMAP_LCDC_IRQ_LOADED_PALETTE
);
412 set_load_mode(OMAP_LCDC_LOAD_PALETTE
);
414 if (!wait_for_completion_timeout(&lcdc
.palette_load_complete
,
415 msecs_to_jiffies(500)))
416 dev_err(lcdc
.fbdev
->dev
, "timeout waiting for FRAME DONE\n");
417 /* The controller gets disabled in the irq handler */
418 disable_irqs(OMAP_LCDC_IRQ_LOADED_PALETTE
);
421 omap_set_lcd_dma_single_transfer(lcdc
.ext_mode
);
424 /* Used only in internal controller mode */
425 static int omap_lcdc_setcolreg(u_int regno
, u16 red
, u16 green
, u16 blue
,
426 u16 transp
, int update_hw_pal
)
430 if (lcdc
.color_mode
!= OMAPFB_COLOR_CLUT_8BPP
|| regno
> 255)
433 palette
= (u16
*)lcdc
.palette_virt
;
435 palette
[regno
] &= ~0x0fff;
436 palette
[regno
] |= ((red
>> 12) << 8) | ((green
>> 12) << 4 ) |
440 disable_controller();
444 set_load_mode(OMAP_LCDC_LOAD_FRAME
);
451 static void calc_ck_div(int is_tft
, int pck
, int *pck_div
)
456 lck
= clk_get_rate(lcdc
.lcd_ck
);
457 *pck_div
= (lck
+ pck
- 1) / pck
;
459 *pck_div
= max(2, *pck_div
);
461 *pck_div
= max(3, *pck_div
);
462 if (*pck_div
> 255) {
463 /* FIXME: try to adjust logic clock divider as well */
465 dev_warn(lcdc
.fbdev
->dev
, "pixclock %d kHz too low.\n",
470 static void inline setup_regs(void)
473 struct lcd_panel
*panel
= lcdc
.fbdev
->panel
;
474 int is_tft
= panel
->config
& OMAP_LCDC_PANEL_TFT
;
478 l
= omap_readl(OMAP_LCDC_CONTROL
);
479 l
&= ~OMAP_LCDC_CTRL_LCD_TFT
;
480 l
|= is_tft
? OMAP_LCDC_CTRL_LCD_TFT
: 0;
481 #ifdef CONFIG_MACH_OMAP_PALMTE
482 /* FIXME:if (machine_is_omap_palmte()) { */
483 /* PalmTE uses alternate TFT setting in 8BPP mode */
484 l
|= (is_tft
&& panel
->bpp
== 8) ? 0x810000 : 0;
487 omap_writel(l
, OMAP_LCDC_CONTROL
);
489 l
= omap_readl(OMAP_LCDC_TIMING2
);
490 l
&= ~(((1 << 6) - 1) << 20);
491 l
|= (panel
->config
& OMAP_LCDC_SIGNAL_MASK
) << 20;
492 omap_writel(l
, OMAP_LCDC_TIMING2
);
494 l
= panel
->x_res
- 1;
495 l
|= (panel
->hsw
- 1) << 10;
496 l
|= (panel
->hfp
- 1) << 16;
497 l
|= (panel
->hbp
- 1) << 24;
498 omap_writel(l
, OMAP_LCDC_TIMING0
);
500 l
= panel
->y_res
- 1;
501 l
|= (panel
->vsw
- 1) << 10;
502 l
|= panel
->vfp
<< 16;
503 l
|= panel
->vbp
<< 24;
504 omap_writel(l
, OMAP_LCDC_TIMING1
);
506 l
= omap_readl(OMAP_LCDC_TIMING2
);
509 lck
= clk_get_rate(lcdc
.lcd_ck
);
512 calc_ck_div(is_tft
, panel
->pixel_clock
* 1000, &pcd
);
514 dev_warn(lcdc
.fbdev
->dev
,
515 "Pixel clock divider value is obsolete.\n"
516 "Try to set pixel_clock to %lu and pcd to 0 "
517 "in drivers/video/omap/lcd_%s.c and submit a patch.\n",
518 lck
/ panel
->pcd
/ 1000, panel
->name
);
523 l
|= panel
->acb
<< 8;
524 omap_writel(l
, OMAP_LCDC_TIMING2
);
526 /* update panel info with the exact clock */
527 panel
->pixel_clock
= lck
/ pcd
/ 1000;
531 * Configure the LCD controller, download the color palette and start a looped
532 * DMA transfer of the frame image data. Called only in internal
535 static int omap_lcdc_set_update_mode(enum omapfb_update_mode mode
)
539 if (mode
!= lcdc
.update_mode
) {
541 case OMAPFB_AUTO_UPDATE
:
545 /* Setup and start LCD DMA */
548 set_load_mode(OMAP_LCDC_LOAD_FRAME
);
549 enable_irqs(OMAP_LCDC_IRQ_DONE
);
550 /* This will start the actual DMA transfer */
552 lcdc
.update_mode
= mode
;
554 case OMAPFB_UPDATE_DISABLED
:
555 disable_controller();
557 lcdc
.update_mode
= mode
;
567 static enum omapfb_update_mode
omap_lcdc_get_update_mode(void)
569 return lcdc
.update_mode
;
572 /* PM code called only in internal controller mode */
573 static void omap_lcdc_suspend(void)
575 omap_lcdc_set_update_mode(OMAPFB_UPDATE_DISABLED
);
578 static void omap_lcdc_resume(void)
580 omap_lcdc_set_update_mode(OMAPFB_AUTO_UPDATE
);
583 static void omap_lcdc_get_caps(int plane
, struct omapfb_caps
*caps
)
588 int omap_lcdc_set_dma_callback(void (*callback
)(void *data
), void *data
)
590 BUG_ON(callback
== NULL
);
592 if (lcdc
.dma_callback
)
595 lcdc
.dma_callback
= callback
;
596 lcdc
.dma_callback_data
= data
;
600 EXPORT_SYMBOL(omap_lcdc_set_dma_callback
);
602 void omap_lcdc_free_dma_callback(void)
604 lcdc
.dma_callback
= NULL
;
606 EXPORT_SYMBOL(omap_lcdc_free_dma_callback
);
608 static void lcdc_dma_handler(u16 status
, void *data
)
610 if (lcdc
.dma_callback
)
611 lcdc
.dma_callback(lcdc
.dma_callback_data
);
614 static int mmap_kern(void)
616 struct vm_struct
*kvma
;
617 struct vm_area_struct vma
;
621 kvma
= get_vm_area(lcdc
.vram_size
, VM_IOREMAP
);
623 dev_err(lcdc
.fbdev
->dev
, "can't get kernel vm area\n");
626 vma
.vm_mm
= &init_mm
;
628 vaddr
= (unsigned long)kvma
->addr
;
629 vma
.vm_start
= vaddr
;
630 vma
.vm_end
= vaddr
+ lcdc
.vram_size
;
632 pgprot
= pgprot_writecombine(pgprot_kernel
);
633 if (io_remap_pfn_range(&vma
, vaddr
,
634 lcdc
.vram_phys
>> PAGE_SHIFT
,
635 lcdc
.vram_size
, pgprot
) < 0) {
636 dev_err(lcdc
.fbdev
->dev
, "kernel mmap for FB memory failed\n");
640 lcdc
.vram_virt
= (void *)vaddr
;
645 static void unmap_kern(void)
647 vunmap(lcdc
.vram_virt
);
650 static int alloc_palette_ram(void)
652 lcdc
.palette_virt
= dma_alloc_writecombine(lcdc
.fbdev
->dev
,
653 MAX_PALETTE_SIZE
, &lcdc
.palette_phys
, GFP_KERNEL
);
654 if (lcdc
.palette_virt
== NULL
) {
655 dev_err(lcdc
.fbdev
->dev
, "failed to alloc palette memory\n");
658 memset(lcdc
.palette_virt
, 0, MAX_PALETTE_SIZE
);
663 static void free_palette_ram(void)
665 dma_free_writecombine(lcdc
.fbdev
->dev
, MAX_PALETTE_SIZE
,
666 lcdc
.palette_virt
, lcdc
.palette_phys
);
669 static int alloc_fbmem(struct omapfb_mem_region
*region
)
673 struct lcd_panel
*panel
= lcdc
.fbdev
->panel
;
678 frame_size
= PAGE_ALIGN(panel
->x_res
* bpp
/ 8 * panel
->y_res
);
679 if (region
->size
> frame_size
)
680 frame_size
= region
->size
;
681 lcdc
.vram_size
= frame_size
;
682 lcdc
.vram_virt
= dma_alloc_writecombine(lcdc
.fbdev
->dev
,
683 lcdc
.vram_size
, &lcdc
.vram_phys
, GFP_KERNEL
);
684 if (lcdc
.vram_virt
== NULL
) {
685 dev_err(lcdc
.fbdev
->dev
, "unable to allocate FB DMA memory\n");
688 region
->size
= frame_size
;
689 region
->paddr
= lcdc
.vram_phys
;
690 region
->vaddr
= lcdc
.vram_virt
;
693 memset(lcdc
.vram_virt
, 0, lcdc
.vram_size
);
698 static void free_fbmem(void)
700 dma_free_writecombine(lcdc
.fbdev
->dev
, lcdc
.vram_size
,
701 lcdc
.vram_virt
, lcdc
.vram_phys
);
704 static int setup_fbmem(struct omapfb_mem_desc
*req_md
)
708 if (!req_md
->region_cnt
) {
709 dev_err(lcdc
.fbdev
->dev
, "no memory regions defined\n");
713 if (req_md
->region_cnt
> 1) {
714 dev_err(lcdc
.fbdev
->dev
, "only one plane is supported\n");
715 req_md
->region_cnt
= 1;
718 if (req_md
->region
[0].paddr
== 0) {
719 lcdc
.fbmem_allocated
= 1;
720 if ((r
= alloc_fbmem(&req_md
->region
[0])) < 0)
725 lcdc
.vram_phys
= req_md
->region
[0].paddr
;
726 lcdc
.vram_size
= req_md
->region
[0].size
;
728 if ((r
= mmap_kern()) < 0)
731 dev_dbg(lcdc
.fbdev
->dev
, "vram at %08x size %08lx mapped to 0x%p\n",
732 lcdc
.vram_phys
, lcdc
.vram_size
, lcdc
.vram_virt
);
737 static void cleanup_fbmem(void)
739 if (lcdc
.fbmem_allocated
)
745 static int omap_lcdc_init(struct omapfb_device
*fbdev
, int ext_mode
,
746 struct omapfb_mem_desc
*req_vram
)
756 lcdc
.ext_mode
= ext_mode
;
759 omap_writel(l
, OMAP_LCDC_CONTROL
);
762 * According to errata some platforms have a clock rate limitiation
764 lcdc
.lcd_ck
= clk_get(fbdev
->dev
, "lcd_ck");
765 if (IS_ERR(lcdc
.lcd_ck
)) {
766 dev_err(fbdev
->dev
, "unable to access LCD clock\n");
767 r
= PTR_ERR(lcdc
.lcd_ck
);
771 tc_ck
= clk_get(fbdev
->dev
, "tc_ck");
773 dev_err(fbdev
->dev
, "unable to access TC clock\n");
778 rate
= clk_get_rate(tc_ck
);
781 if (machine_is_ams_delta())
783 if (machine_is_omap_h3())
785 r
= clk_set_rate(lcdc
.lcd_ck
, rate
);
787 dev_err(fbdev
->dev
, "failed to adjust LCD rate\n");
790 clk_enable(lcdc
.lcd_ck
);
792 r
= request_irq(OMAP_LCDC_IRQ
, lcdc_irq_handler
, 0, MODULE_NAME
, fbdev
);
794 dev_err(fbdev
->dev
, "unable to get IRQ\n");
798 r
= omap_request_lcd_dma(lcdc_dma_handler
, NULL
);
800 dev_err(fbdev
->dev
, "unable to get LCD DMA\n");
804 omap_set_lcd_dma_single_transfer(ext_mode
);
805 omap_set_lcd_dma_ext_controller(ext_mode
);
808 if ((r
= alloc_palette_ram()) < 0)
811 if ((r
= setup_fbmem(req_vram
)) < 0)
814 pr_info("omapfb: LCDC initialized\n");
823 free_irq(OMAP_LCDC_IRQ
, lcdc
.fbdev
);
825 clk_disable(lcdc
.lcd_ck
);
827 clk_put(lcdc
.lcd_ck
);
832 static void omap_lcdc_cleanup(void)
838 free_irq(OMAP_LCDC_IRQ
, lcdc
.fbdev
);
839 clk_disable(lcdc
.lcd_ck
);
840 clk_put(lcdc
.lcd_ck
);
843 const struct lcd_ctrl omap1_int_ctrl
= {
845 .init
= omap_lcdc_init
,
846 .cleanup
= omap_lcdc_cleanup
,
847 .get_caps
= omap_lcdc_get_caps
,
848 .set_update_mode
= omap_lcdc_set_update_mode
,
849 .get_update_mode
= omap_lcdc_get_update_mode
,
850 .update_window
= NULL
,
851 .suspend
= omap_lcdc_suspend
,
852 .resume
= omap_lcdc_resume
,
853 .setup_plane
= omap_lcdc_setup_plane
,
854 .enable_plane
= omap_lcdc_enable_plane
,
855 .setcolreg
= omap_lcdc_setcolreg
,