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>
32 #include <mach/lcdc.h>
35 #include <asm/mach-types.h>
41 #define MODULE_NAME "lcdc"
43 #define MAX_PALETTE_SIZE PAGE_SIZE
46 OMAP_LCDC_LOAD_PALETTE
,
48 OMAP_LCDC_LOAD_PALETTE_AND_FRAME
51 static struct omap_lcd_controller
{
52 enum omapfb_update_mode update_mode
;
55 unsigned long frame_offset
;
60 enum omapfb_color_format color_mode
;
63 dma_addr_t palette_phys
;
67 unsigned int irq_mask
;
68 struct completion last_frame_complete
;
69 struct completion palette_load_complete
;
71 struct omapfb_device
*fbdev
;
73 void (*dma_callback
)(void *data
);
74 void *dma_callback_data
;
79 unsigned long vram_size
;
82 static void inline enable_irqs(int mask
)
84 lcdc
.irq_mask
|= mask
;
87 static void inline disable_irqs(int mask
)
89 lcdc
.irq_mask
&= ~mask
;
92 static void set_load_mode(enum lcdc_load_mode mode
)
96 l
= omap_readl(OMAP_LCDC_CONTROL
);
99 case OMAP_LCDC_LOAD_PALETTE
:
102 case OMAP_LCDC_LOAD_FRAME
:
105 case OMAP_LCDC_LOAD_PALETTE_AND_FRAME
:
110 omap_writel(l
, OMAP_LCDC_CONTROL
);
113 static void enable_controller(void)
117 l
= omap_readl(OMAP_LCDC_CONTROL
);
118 l
|= OMAP_LCDC_CTRL_LCD_EN
;
119 l
&= ~OMAP_LCDC_IRQ_MASK
;
120 l
|= lcdc
.irq_mask
| OMAP_LCDC_IRQ_DONE
; /* enabled IRQs */
121 omap_writel(l
, OMAP_LCDC_CONTROL
);
124 static void disable_controller_async(void)
129 l
= omap_readl(OMAP_LCDC_CONTROL
);
130 mask
= OMAP_LCDC_CTRL_LCD_EN
| OMAP_LCDC_IRQ_MASK
;
132 * Preserve the DONE mask, since we still want to get the
133 * final DONE irq. It will be disabled in the IRQ handler.
135 mask
&= ~OMAP_LCDC_IRQ_DONE
;
137 omap_writel(l
, OMAP_LCDC_CONTROL
);
140 static void disable_controller(void)
142 init_completion(&lcdc
.last_frame_complete
);
143 disable_controller_async();
144 if (!wait_for_completion_timeout(&lcdc
.last_frame_complete
,
145 msecs_to_jiffies(500)))
146 dev_err(lcdc
.fbdev
->dev
, "timeout waiting for FRAME DONE\n");
149 static void reset_controller(u32 status
)
151 static unsigned long reset_count
;
152 static unsigned long last_jiffies
;
154 disable_controller_async();
156 if (reset_count
== 1 || time_after(jiffies
, last_jiffies
+ HZ
)) {
157 dev_err(lcdc
.fbdev
->dev
,
158 "resetting (status %#010x,reset count %lu)\n",
159 status
, reset_count
);
160 last_jiffies
= jiffies
;
162 if (reset_count
< 100) {
166 dev_err(lcdc
.fbdev
->dev
,
167 "too many reset attempts, giving up.\n");
172 * Configure the LCD DMA according to the current mode specified by parameters
173 * in lcdc.fbdev and fbdev->var.
175 static void setup_lcd_dma(void)
177 static const int dma_elem_type
[] = {
179 OMAP_DMA_DATA_TYPE_S8
,
180 OMAP_DMA_DATA_TYPE_S16
,
182 OMAP_DMA_DATA_TYPE_S32
,
184 struct omapfb_plane_struct
*plane
= lcdc
.fbdev
->fb_info
[0]->par
;
185 struct fb_var_screeninfo
*var
= &lcdc
.fbdev
->fb_info
[0]->var
;
187 int esize
, xelem
, yelem
;
189 src
= lcdc
.vram_phys
+ lcdc
.frame_offset
;
191 switch (var
->rotate
) {
193 if (plane
->info
.mirror
|| (src
& 3) ||
194 lcdc
.color_mode
== OMAPFB_COLOR_YUV420
||
199 xelem
= lcdc
.xres
* lcdc
.bpp
/ 8 / esize
;
205 if (cpu_is_omap15xx()) {
209 xelem
= lcdc
.yres
* lcdc
.bpp
/ 16;
217 dev_dbg(lcdc
.fbdev
->dev
,
218 "setup_dma: src %#010lx esize %d xelem %d yelem %d\n",
219 src
, esize
, xelem
, yelem
);
221 omap_set_lcd_dma_b1(src
, xelem
, yelem
, dma_elem_type
[esize
]);
222 if (!cpu_is_omap15xx()) {
226 * YUV support is only for external mode when we have the
227 * YUV window embedded in a 16bpp frame buffer.
229 if (lcdc
.color_mode
== OMAPFB_COLOR_YUV420
)
231 /* Set virtual xres elem size */
232 omap_set_lcd_dma_b1_vxres(
233 lcdc
.screen_width
* bpp
/ 8 / esize
);
234 /* Setup transformations */
235 omap_set_lcd_dma_b1_rotation(var
->rotate
);
236 omap_set_lcd_dma_b1_mirror(plane
->info
.mirror
);
238 omap_setup_lcd_dma();
241 static irqreturn_t
lcdc_irq_handler(int irq
, void *dev_id
)
245 status
= omap_readl(OMAP_LCDC_STATUS
);
247 if (status
& (OMAP_LCDC_STAT_FUF
| OMAP_LCDC_STAT_SYNC_LOST
))
248 reset_controller(status
);
250 if (status
& OMAP_LCDC_STAT_DONE
) {
254 * Disable IRQ_DONE. The status bit will be cleared
255 * only when the controller is reenabled and we don't
256 * want to get more interrupts.
258 l
= omap_readl(OMAP_LCDC_CONTROL
);
259 l
&= ~OMAP_LCDC_IRQ_DONE
;
260 omap_writel(l
, OMAP_LCDC_CONTROL
);
261 complete(&lcdc
.last_frame_complete
);
263 if (status
& OMAP_LCDC_STAT_LOADED_PALETTE
) {
264 disable_controller_async();
265 complete(&lcdc
.palette_load_complete
);
270 * Clear these interrupt status bits.
271 * Sync_lost, FUF bits were cleared by disabling the LCD controller
272 * LOADED_PALETTE can be cleared this way only in palette only
273 * load mode. In other load modes it's cleared by disabling the
276 status
&= ~(OMAP_LCDC_STAT_VSYNC
|
277 OMAP_LCDC_STAT_LOADED_PALETTE
|
279 OMAP_LCDC_STAT_LINE_INT
);
280 omap_writel(status
, OMAP_LCDC_STATUS
);
285 * Change to a new video mode. We defer this to a later time to avoid any
286 * flicker and not to mess up the current LCD DMA context. For this we disable
287 * the LCD controller, which will generate a DONE irq after the last frame has
288 * been transferred. Then it'll be safe to reconfigure both the LCD controller
289 * as well as the LCD DMA.
291 static int omap_lcdc_setup_plane(int plane
, int channel_out
,
292 unsigned long offset
, int screen_width
,
293 int pos_x
, int pos_y
, int width
, int height
,
296 struct fb_var_screeninfo
*var
= &lcdc
.fbdev
->fb_info
[0]->var
;
297 struct lcd_panel
*panel
= lcdc
.fbdev
->panel
;
300 if (var
->rotate
== 0) {
301 rot_x
= panel
->x_res
;
302 rot_y
= panel
->y_res
;
304 rot_x
= panel
->y_res
;
305 rot_y
= panel
->x_res
;
307 if (plane
!= 0 || channel_out
!= 0 || pos_x
!= 0 || pos_y
!= 0 ||
308 width
> rot_x
|| height
> rot_y
) {
310 dev_dbg(lcdc
.fbdev
->dev
,
311 "invalid plane params plane %d pos_x %d pos_y %d "
312 "w %d h %d\n", plane
, pos_x
, pos_y
, width
, height
);
317 lcdc
.frame_offset
= offset
;
320 lcdc
.screen_width
= screen_width
;
321 lcdc
.color_mode
= color_mode
;
323 switch (color_mode
) {
324 case OMAPFB_COLOR_CLUT_8BPP
:
326 lcdc
.palette_code
= 0x3000;
327 lcdc
.palette_size
= 512;
329 case OMAPFB_COLOR_RGB565
:
331 lcdc
.palette_code
= 0x4000;
332 lcdc
.palette_size
= 32;
334 case OMAPFB_COLOR_RGB444
:
336 lcdc
.palette_code
= 0x4000;
337 lcdc
.palette_size
= 32;
339 case OMAPFB_COLOR_YUV420
:
345 case OMAPFB_COLOR_YUV422
:
352 /* FIXME: other BPPs.
353 * bpp1: code 0, size 256
354 * bpp2: code 0x1000 size 256
355 * bpp4: code 0x2000 size 256
356 * bpp12: code 0x4000 size 32
358 dev_dbg(lcdc
.fbdev
->dev
, "invalid color mode %d\n", color_mode
);
368 if (lcdc
.update_mode
== OMAPFB_AUTO_UPDATE
) {
369 disable_controller();
378 static int omap_lcdc_enable_plane(int plane
, int enable
)
380 dev_dbg(lcdc
.fbdev
->dev
,
381 "plane %d enable %d update_mode %d ext_mode %d\n",
382 plane
, enable
, lcdc
.update_mode
, lcdc
.ext_mode
);
383 if (plane
!= OMAPFB_PLANE_GFX
)
390 * Configure the LCD DMA for a palette load operation and do the palette
391 * downloading synchronously. We don't use the frame+palette load mode of
392 * the controller, since the palette can always be downloaded seperately.
394 static void load_palette(void)
398 palette
= (u16
*)lcdc
.palette_virt
;
400 *(u16
*)palette
&= 0x0fff;
401 *(u16
*)palette
|= lcdc
.palette_code
;
403 omap_set_lcd_dma_b1(lcdc
.palette_phys
,
404 lcdc
.palette_size
/ 4 + 1, 1, OMAP_DMA_DATA_TYPE_S32
);
406 omap_set_lcd_dma_single_transfer(1);
407 omap_setup_lcd_dma();
409 init_completion(&lcdc
.palette_load_complete
);
410 enable_irqs(OMAP_LCDC_IRQ_LOADED_PALETTE
);
411 set_load_mode(OMAP_LCDC_LOAD_PALETTE
);
413 if (!wait_for_completion_timeout(&lcdc
.palette_load_complete
,
414 msecs_to_jiffies(500)))
415 dev_err(lcdc
.fbdev
->dev
, "timeout waiting for FRAME DONE\n");
416 /* The controller gets disabled in the irq handler */
417 disable_irqs(OMAP_LCDC_IRQ_LOADED_PALETTE
);
420 omap_set_lcd_dma_single_transfer(lcdc
.ext_mode
);
423 /* Used only in internal controller mode */
424 static int omap_lcdc_setcolreg(u_int regno
, u16 red
, u16 green
, u16 blue
,
425 u16 transp
, int update_hw_pal
)
429 if (lcdc
.color_mode
!= OMAPFB_COLOR_CLUT_8BPP
|| regno
> 255)
432 palette
= (u16
*)lcdc
.palette_virt
;
434 palette
[regno
] &= ~0x0fff;
435 palette
[regno
] |= ((red
>> 12) << 8) | ((green
>> 12) << 4 ) |
439 disable_controller();
443 set_load_mode(OMAP_LCDC_LOAD_FRAME
);
450 static void calc_ck_div(int is_tft
, int pck
, int *pck_div
)
455 lck
= clk_get_rate(lcdc
.lcd_ck
);
456 *pck_div
= (lck
+ pck
- 1) / pck
;
458 *pck_div
= max(2, *pck_div
);
460 *pck_div
= max(3, *pck_div
);
461 if (*pck_div
> 255) {
462 /* FIXME: try to adjust logic clock divider as well */
464 dev_warn(lcdc
.fbdev
->dev
, "pixclock %d kHz too low.\n",
469 static void inline setup_regs(void)
472 struct lcd_panel
*panel
= lcdc
.fbdev
->panel
;
473 int is_tft
= panel
->config
& OMAP_LCDC_PANEL_TFT
;
477 l
= omap_readl(OMAP_LCDC_CONTROL
);
478 l
&= ~OMAP_LCDC_CTRL_LCD_TFT
;
479 l
|= is_tft
? OMAP_LCDC_CTRL_LCD_TFT
: 0;
480 #ifdef CONFIG_MACH_OMAP_PALMTE
481 /* FIXME:if (machine_is_omap_palmte()) { */
482 /* PalmTE uses alternate TFT setting in 8BPP mode */
483 l
|= (is_tft
&& panel
->bpp
== 8) ? 0x810000 : 0;
486 omap_writel(l
, OMAP_LCDC_CONTROL
);
488 l
= omap_readl(OMAP_LCDC_TIMING2
);
489 l
&= ~(((1 << 6) - 1) << 20);
490 l
|= (panel
->config
& OMAP_LCDC_SIGNAL_MASK
) << 20;
491 omap_writel(l
, OMAP_LCDC_TIMING2
);
493 l
= panel
->x_res
- 1;
494 l
|= (panel
->hsw
- 1) << 10;
495 l
|= (panel
->hfp
- 1) << 16;
496 l
|= (panel
->hbp
- 1) << 24;
497 omap_writel(l
, OMAP_LCDC_TIMING0
);
499 l
= panel
->y_res
- 1;
500 l
|= (panel
->vsw
- 1) << 10;
501 l
|= panel
->vfp
<< 16;
502 l
|= panel
->vbp
<< 24;
503 omap_writel(l
, OMAP_LCDC_TIMING1
);
505 l
= omap_readl(OMAP_LCDC_TIMING2
);
508 lck
= clk_get_rate(lcdc
.lcd_ck
);
511 calc_ck_div(is_tft
, panel
->pixel_clock
* 1000, &pcd
);
513 dev_warn(lcdc
.fbdev
->dev
,
514 "Pixel clock divider value is obsolete.\n"
515 "Try to set pixel_clock to %lu and pcd to 0 "
516 "in drivers/video/omap/lcd_%s.c and submit a patch.\n",
517 lck
/ panel
->pcd
/ 1000, panel
->name
);
522 l
|= panel
->acb
<< 8;
523 omap_writel(l
, OMAP_LCDC_TIMING2
);
525 /* update panel info with the exact clock */
526 panel
->pixel_clock
= lck
/ pcd
/ 1000;
530 * Configure the LCD controller, download the color palette and start a looped
531 * DMA transfer of the frame image data. Called only in internal
534 static int omap_lcdc_set_update_mode(enum omapfb_update_mode mode
)
538 if (mode
!= lcdc
.update_mode
) {
540 case OMAPFB_AUTO_UPDATE
:
544 /* Setup and start LCD DMA */
547 set_load_mode(OMAP_LCDC_LOAD_FRAME
);
548 enable_irqs(OMAP_LCDC_IRQ_DONE
);
549 /* This will start the actual DMA transfer */
551 lcdc
.update_mode
= mode
;
553 case OMAPFB_UPDATE_DISABLED
:
554 disable_controller();
556 lcdc
.update_mode
= mode
;
566 static enum omapfb_update_mode
omap_lcdc_get_update_mode(void)
568 return lcdc
.update_mode
;
571 /* PM code called only in internal controller mode */
572 static void omap_lcdc_suspend(void)
574 if (lcdc
.update_mode
== OMAPFB_AUTO_UPDATE
) {
575 disable_controller();
580 static void omap_lcdc_resume(void)
582 if (lcdc
.update_mode
== OMAPFB_AUTO_UPDATE
) {
586 set_load_mode(OMAP_LCDC_LOAD_FRAME
);
587 enable_irqs(OMAP_LCDC_IRQ_DONE
);
592 static void omap_lcdc_get_caps(int plane
, struct omapfb_caps
*caps
)
597 int omap_lcdc_set_dma_callback(void (*callback
)(void *data
), void *data
)
599 BUG_ON(callback
== NULL
);
601 if (lcdc
.dma_callback
)
604 lcdc
.dma_callback
= callback
;
605 lcdc
.dma_callback_data
= data
;
609 EXPORT_SYMBOL(omap_lcdc_set_dma_callback
);
611 void omap_lcdc_free_dma_callback(void)
613 lcdc
.dma_callback
= NULL
;
615 EXPORT_SYMBOL(omap_lcdc_free_dma_callback
);
617 static void lcdc_dma_handler(u16 status
, void *data
)
619 if (lcdc
.dma_callback
)
620 lcdc
.dma_callback(lcdc
.dma_callback_data
);
623 static int mmap_kern(void)
625 struct vm_struct
*kvma
;
626 struct vm_area_struct vma
;
630 kvma
= get_vm_area(lcdc
.vram_size
, VM_IOREMAP
);
632 dev_err(lcdc
.fbdev
->dev
, "can't get kernel vm area\n");
635 vma
.vm_mm
= &init_mm
;
637 vaddr
= (unsigned long)kvma
->addr
;
638 vma
.vm_start
= vaddr
;
639 vma
.vm_end
= vaddr
+ lcdc
.vram_size
;
641 pgprot
= pgprot_writecombine(pgprot_kernel
);
642 if (io_remap_pfn_range(&vma
, vaddr
,
643 lcdc
.vram_phys
>> PAGE_SHIFT
,
644 lcdc
.vram_size
, pgprot
) < 0) {
645 dev_err(lcdc
.fbdev
->dev
, "kernel mmap for FB memory failed\n");
649 lcdc
.vram_virt
= (void *)vaddr
;
654 static void unmap_kern(void)
656 vunmap(lcdc
.vram_virt
);
659 static int alloc_palette_ram(void)
661 lcdc
.palette_virt
= dma_alloc_writecombine(lcdc
.fbdev
->dev
,
662 MAX_PALETTE_SIZE
, &lcdc
.palette_phys
, GFP_KERNEL
);
663 if (lcdc
.palette_virt
== NULL
) {
664 dev_err(lcdc
.fbdev
->dev
, "failed to alloc palette memory\n");
667 memset(lcdc
.palette_virt
, 0, MAX_PALETTE_SIZE
);
672 static void free_palette_ram(void)
674 dma_free_writecombine(lcdc
.fbdev
->dev
, MAX_PALETTE_SIZE
,
675 lcdc
.palette_virt
, lcdc
.palette_phys
);
678 static int alloc_fbmem(struct omapfb_mem_region
*region
)
682 struct lcd_panel
*panel
= lcdc
.fbdev
->panel
;
687 frame_size
= PAGE_ALIGN(panel
->x_res
* bpp
/ 8 * panel
->y_res
);
688 if (region
->size
> frame_size
)
689 frame_size
= region
->size
;
690 lcdc
.vram_size
= frame_size
;
691 lcdc
.vram_virt
= dma_alloc_writecombine(lcdc
.fbdev
->dev
,
692 lcdc
.vram_size
, &lcdc
.vram_phys
, GFP_KERNEL
);
693 if (lcdc
.vram_virt
== NULL
) {
694 dev_err(lcdc
.fbdev
->dev
, "unable to allocate FB DMA memory\n");
697 region
->size
= frame_size
;
698 region
->paddr
= lcdc
.vram_phys
;
699 region
->vaddr
= lcdc
.vram_virt
;
702 memset(lcdc
.vram_virt
, 0, lcdc
.vram_size
);
707 static void free_fbmem(void)
709 dma_free_writecombine(lcdc
.fbdev
->dev
, lcdc
.vram_size
,
710 lcdc
.vram_virt
, lcdc
.vram_phys
);
713 static int setup_fbmem(struct omapfb_mem_desc
*req_md
)
717 if (!req_md
->region_cnt
) {
718 dev_err(lcdc
.fbdev
->dev
, "no memory regions defined\n");
722 if (req_md
->region_cnt
> 1) {
723 dev_err(lcdc
.fbdev
->dev
, "only one plane is supported\n");
724 req_md
->region_cnt
= 1;
727 if (req_md
->region
[0].paddr
== 0) {
728 lcdc
.fbmem_allocated
= 1;
729 if ((r
= alloc_fbmem(&req_md
->region
[0])) < 0)
734 lcdc
.vram_phys
= req_md
->region
[0].paddr
;
735 lcdc
.vram_size
= req_md
->region
[0].size
;
737 if ((r
= mmap_kern()) < 0)
740 dev_dbg(lcdc
.fbdev
->dev
, "vram at %08x size %08lx mapped to 0x%p\n",
741 lcdc
.vram_phys
, lcdc
.vram_size
, lcdc
.vram_virt
);
746 static void cleanup_fbmem(void)
748 if (lcdc
.fbmem_allocated
)
754 static int omap_lcdc_init(struct omapfb_device
*fbdev
, int ext_mode
,
755 struct omapfb_mem_desc
*req_vram
)
765 lcdc
.ext_mode
= ext_mode
;
768 omap_writel(l
, OMAP_LCDC_CONTROL
);
771 * According to errata some platforms have a clock rate limitiation
773 lcdc
.lcd_ck
= clk_get(fbdev
->dev
, "lcd_ck");
774 if (IS_ERR(lcdc
.lcd_ck
)) {
775 dev_err(fbdev
->dev
, "unable to access LCD clock\n");
776 r
= PTR_ERR(lcdc
.lcd_ck
);
780 tc_ck
= clk_get(fbdev
->dev
, "tc_ck");
782 dev_err(fbdev
->dev
, "unable to access TC clock\n");
787 rate
= clk_get_rate(tc_ck
);
790 if (machine_is_ams_delta())
792 if (machine_is_omap_h3())
794 r
= clk_set_rate(lcdc
.lcd_ck
, rate
);
796 dev_err(fbdev
->dev
, "failed to adjust LCD rate\n");
799 clk_enable(lcdc
.lcd_ck
);
801 r
= request_irq(OMAP_LCDC_IRQ
, lcdc_irq_handler
, 0, MODULE_NAME
, fbdev
);
803 dev_err(fbdev
->dev
, "unable to get IRQ\n");
807 r
= omap_request_lcd_dma(lcdc_dma_handler
, NULL
);
809 dev_err(fbdev
->dev
, "unable to get LCD DMA\n");
813 omap_set_lcd_dma_single_transfer(ext_mode
);
814 omap_set_lcd_dma_ext_controller(ext_mode
);
817 if ((r
= alloc_palette_ram()) < 0)
820 if ((r
= setup_fbmem(req_vram
)) < 0)
823 pr_info("omapfb: LCDC initialized\n");
832 free_irq(OMAP_LCDC_IRQ
, lcdc
.fbdev
);
834 clk_disable(lcdc
.lcd_ck
);
836 clk_put(lcdc
.lcd_ck
);
841 static void omap_lcdc_cleanup(void)
847 free_irq(OMAP_LCDC_IRQ
, lcdc
.fbdev
);
848 clk_disable(lcdc
.lcd_ck
);
849 clk_put(lcdc
.lcd_ck
);
852 const struct lcd_ctrl omap1_int_ctrl
= {
854 .init
= omap_lcdc_init
,
855 .cleanup
= omap_lcdc_cleanup
,
856 .get_caps
= omap_lcdc_get_caps
,
857 .set_update_mode
= omap_lcdc_set_update_mode
,
858 .get_update_mode
= omap_lcdc_get_update_mode
,
859 .update_window
= NULL
,
860 .suspend
= omap_lcdc_suspend
,
861 .resume
= omap_lcdc_resume
,
862 .setup_plane
= omap_lcdc_setup_plane
,
863 .enable_plane
= omap_lcdc_enable_plane
,
864 .setcolreg
= omap_lcdc_setcolreg
,