1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OMAP1 internal LCD controller
5 * Copyright (C) 2004 Nokia Corporation
6 * Author: Imre Deak <imre.deak@nokia.com>
8 #include <linux/module.h>
9 #include <linux/device.h>
10 #include <linux/interrupt.h>
11 #include <linux/spinlock.h>
12 #include <linux/err.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/vmalloc.h>
17 #include <linux/clk.h>
18 #include <linux/gfp.h>
20 #include <linux/soc/ti/omap1-io.h>
21 #include <linux/soc/ti/omap1-soc.h>
22 #include <linux/omap-dma.h>
24 #include <asm/mach-types.h>
31 #define MODULE_NAME "lcdc"
33 #define MAX_PALETTE_SIZE PAGE_SIZE
36 OMAP_LCDC_LOAD_PALETTE
,
38 OMAP_LCDC_LOAD_PALETTE_AND_FRAME
41 static struct omap_lcd_controller
{
42 enum omapfb_update_mode update_mode
;
45 unsigned long frame_offset
;
50 enum omapfb_color_format color_mode
;
53 dma_addr_t palette_phys
;
57 unsigned int irq_mask
;
58 struct completion last_frame_complete
;
59 struct completion palette_load_complete
;
61 struct omapfb_device
*fbdev
;
63 void (*dma_callback
)(void *data
);
64 void *dma_callback_data
;
68 unsigned long vram_size
;
71 static inline void enable_irqs(int mask
)
73 lcdc
.irq_mask
|= mask
;
76 static inline void disable_irqs(int mask
)
78 lcdc
.irq_mask
&= ~mask
;
81 static void set_load_mode(enum lcdc_load_mode mode
)
85 l
= omap_readl(OMAP_LCDC_CONTROL
);
88 case OMAP_LCDC_LOAD_PALETTE
:
91 case OMAP_LCDC_LOAD_FRAME
:
94 case OMAP_LCDC_LOAD_PALETTE_AND_FRAME
:
99 omap_writel(l
, OMAP_LCDC_CONTROL
);
102 static void enable_controller(void)
106 l
= omap_readl(OMAP_LCDC_CONTROL
);
107 l
|= OMAP_LCDC_CTRL_LCD_EN
;
108 l
&= ~OMAP_LCDC_IRQ_MASK
;
109 l
|= lcdc
.irq_mask
| OMAP_LCDC_IRQ_DONE
; /* enabled IRQs */
110 omap_writel(l
, OMAP_LCDC_CONTROL
);
113 static void disable_controller_async(void)
118 l
= omap_readl(OMAP_LCDC_CONTROL
);
119 mask
= OMAP_LCDC_CTRL_LCD_EN
| OMAP_LCDC_IRQ_MASK
;
121 * Preserve the DONE mask, since we still want to get the
122 * final DONE irq. It will be disabled in the IRQ handler.
124 mask
&= ~OMAP_LCDC_IRQ_DONE
;
126 omap_writel(l
, OMAP_LCDC_CONTROL
);
129 static void disable_controller(void)
131 init_completion(&lcdc
.last_frame_complete
);
132 disable_controller_async();
133 if (!wait_for_completion_timeout(&lcdc
.last_frame_complete
,
134 msecs_to_jiffies(500)))
135 dev_err(lcdc
.fbdev
->dev
, "timeout waiting for FRAME DONE\n");
138 static void reset_controller(u32 status
)
140 static unsigned long reset_count
;
141 static unsigned long last_jiffies
;
143 disable_controller_async();
145 if (reset_count
== 1 || time_after(jiffies
, last_jiffies
+ HZ
)) {
146 dev_err(lcdc
.fbdev
->dev
,
147 "resetting (status %#010x,reset count %lu)\n",
148 status
, reset_count
);
149 last_jiffies
= jiffies
;
151 if (reset_count
< 100) {
155 dev_err(lcdc
.fbdev
->dev
,
156 "too many reset attempts, giving up.\n");
161 * Configure the LCD DMA according to the current mode specified by parameters
162 * in lcdc.fbdev and fbdev->var.
164 static void setup_lcd_dma(void)
166 static const int dma_elem_type
[] = {
168 OMAP_DMA_DATA_TYPE_S8
,
169 OMAP_DMA_DATA_TYPE_S16
,
171 OMAP_DMA_DATA_TYPE_S32
,
173 struct omapfb_plane_struct
*plane
= lcdc
.fbdev
->fb_info
[0]->par
;
174 struct fb_var_screeninfo
*var
= &lcdc
.fbdev
->fb_info
[0]->var
;
176 int esize
, xelem
, yelem
;
178 src
= lcdc
.vram_phys
+ lcdc
.frame_offset
;
180 switch (var
->rotate
) {
182 if (plane
->info
.mirror
|| (src
& 3) ||
183 lcdc
.color_mode
== OMAPFB_COLOR_YUV420
||
188 xelem
= lcdc
.xres
* lcdc
.bpp
/ 8 / esize
;
194 if (cpu_is_omap15xx()) {
198 xelem
= lcdc
.yres
* lcdc
.bpp
/ 16;
206 dev_dbg(lcdc
.fbdev
->dev
,
207 "setup_dma: src %#010lx esize %d xelem %d yelem %d\n",
208 src
, esize
, xelem
, yelem
);
210 omap_set_lcd_dma_b1(src
, xelem
, yelem
, dma_elem_type
[esize
]);
211 if (!cpu_is_omap15xx()) {
215 * YUV support is only for external mode when we have the
216 * YUV window embedded in a 16bpp frame buffer.
218 if (lcdc
.color_mode
== OMAPFB_COLOR_YUV420
)
220 /* Set virtual xres elem size */
221 omap_set_lcd_dma_b1_vxres(
222 lcdc
.screen_width
* bpp
/ 8 / esize
);
223 /* Setup transformations */
224 omap_set_lcd_dma_b1_rotation(var
->rotate
);
225 omap_set_lcd_dma_b1_mirror(plane
->info
.mirror
);
227 omap_setup_lcd_dma();
230 static irqreturn_t
lcdc_irq_handler(int irq
, void *dev_id
)
234 status
= omap_readl(OMAP_LCDC_STATUS
);
236 if (status
& (OMAP_LCDC_STAT_FUF
| OMAP_LCDC_STAT_SYNC_LOST
))
237 reset_controller(status
);
239 if (status
& OMAP_LCDC_STAT_DONE
) {
243 * Disable IRQ_DONE. The status bit will be cleared
244 * only when the controller is reenabled and we don't
245 * want to get more interrupts.
247 l
= omap_readl(OMAP_LCDC_CONTROL
);
248 l
&= ~OMAP_LCDC_IRQ_DONE
;
249 omap_writel(l
, OMAP_LCDC_CONTROL
);
250 complete(&lcdc
.last_frame_complete
);
252 if (status
& OMAP_LCDC_STAT_LOADED_PALETTE
) {
253 disable_controller_async();
254 complete(&lcdc
.palette_load_complete
);
259 * Clear these interrupt status bits.
260 * Sync_lost, FUF bits were cleared by disabling the LCD controller
261 * LOADED_PALETTE can be cleared this way only in palette only
262 * load mode. In other load modes it's cleared by disabling the
265 status
&= ~(OMAP_LCDC_STAT_VSYNC
|
266 OMAP_LCDC_STAT_LOADED_PALETTE
|
268 OMAP_LCDC_STAT_LINE_INT
);
269 omap_writel(status
, OMAP_LCDC_STATUS
);
274 * Change to a new video mode. We defer this to a later time to avoid any
275 * flicker and not to mess up the current LCD DMA context. For this we disable
276 * the LCD controller, which will generate a DONE irq after the last frame has
277 * been transferred. Then it'll be safe to reconfigure both the LCD controller
278 * as well as the LCD DMA.
280 static int omap_lcdc_setup_plane(int plane
, int channel_out
,
281 unsigned long offset
, int screen_width
,
282 int pos_x
, int pos_y
, int width
, int height
,
285 struct fb_var_screeninfo
*var
= &lcdc
.fbdev
->fb_info
[0]->var
;
286 struct lcd_panel
*panel
= lcdc
.fbdev
->panel
;
289 if (var
->rotate
== 0) {
290 rot_x
= panel
->x_res
;
291 rot_y
= panel
->y_res
;
293 rot_x
= panel
->y_res
;
294 rot_y
= panel
->x_res
;
296 if (plane
!= 0 || channel_out
!= 0 || pos_x
!= 0 || pos_y
!= 0 ||
297 width
> rot_x
|| height
> rot_y
) {
299 dev_dbg(lcdc
.fbdev
->dev
,
300 "invalid plane params plane %d pos_x %d pos_y %d "
301 "w %d h %d\n", plane
, pos_x
, pos_y
, width
, height
);
306 lcdc
.frame_offset
= offset
;
309 lcdc
.screen_width
= screen_width
;
310 lcdc
.color_mode
= color_mode
;
312 switch (color_mode
) {
313 case OMAPFB_COLOR_CLUT_8BPP
:
315 lcdc
.palette_code
= 0x3000;
316 lcdc
.palette_size
= 512;
318 case OMAPFB_COLOR_RGB565
:
320 lcdc
.palette_code
= 0x4000;
321 lcdc
.palette_size
= 32;
323 case OMAPFB_COLOR_RGB444
:
325 lcdc
.palette_code
= 0x4000;
326 lcdc
.palette_size
= 32;
328 case OMAPFB_COLOR_YUV420
:
334 case OMAPFB_COLOR_YUV422
:
341 /* FIXME: other BPPs.
342 * bpp1: code 0, size 256
343 * bpp2: code 0x1000 size 256
344 * bpp4: code 0x2000 size 256
345 * bpp12: code 0x4000 size 32
347 dev_dbg(lcdc
.fbdev
->dev
, "invalid color mode %d\n", color_mode
);
357 if (lcdc
.update_mode
== OMAPFB_AUTO_UPDATE
) {
358 disable_controller();
367 static int omap_lcdc_enable_plane(int plane
, int enable
)
369 dev_dbg(lcdc
.fbdev
->dev
,
370 "plane %d enable %d update_mode %d ext_mode %d\n",
371 plane
, enable
, lcdc
.update_mode
, lcdc
.ext_mode
);
372 if (plane
!= OMAPFB_PLANE_GFX
)
379 * Configure the LCD DMA for a palette load operation and do the palette
380 * downloading synchronously. We don't use the frame+palette load mode of
381 * the controller, since the palette can always be downloaded separately.
383 static void load_palette(void)
387 palette
= (u16
*)lcdc
.palette_virt
;
389 *(u16
*)palette
&= 0x0fff;
390 *(u16
*)palette
|= lcdc
.palette_code
;
392 omap_set_lcd_dma_b1(lcdc
.palette_phys
,
393 lcdc
.palette_size
/ 4 + 1, 1, OMAP_DMA_DATA_TYPE_S32
);
395 omap_set_lcd_dma_single_transfer(1);
396 omap_setup_lcd_dma();
398 init_completion(&lcdc
.palette_load_complete
);
399 enable_irqs(OMAP_LCDC_IRQ_LOADED_PALETTE
);
400 set_load_mode(OMAP_LCDC_LOAD_PALETTE
);
402 if (!wait_for_completion_timeout(&lcdc
.palette_load_complete
,
403 msecs_to_jiffies(500)))
404 dev_err(lcdc
.fbdev
->dev
, "timeout waiting for FRAME DONE\n");
405 /* The controller gets disabled in the irq handler */
406 disable_irqs(OMAP_LCDC_IRQ_LOADED_PALETTE
);
409 omap_set_lcd_dma_single_transfer(lcdc
.ext_mode
);
412 /* Used only in internal controller mode */
413 static int omap_lcdc_setcolreg(u_int regno
, u16 red
, u16 green
, u16 blue
,
414 u16 transp
, int update_hw_pal
)
418 if (lcdc
.color_mode
!= OMAPFB_COLOR_CLUT_8BPP
|| regno
> 255)
421 palette
= (u16
*)lcdc
.palette_virt
;
423 palette
[regno
] &= ~0x0fff;
424 palette
[regno
] |= ((red
>> 12) << 8) | ((green
>> 12) << 4 ) |
428 disable_controller();
432 set_load_mode(OMAP_LCDC_LOAD_FRAME
);
439 static void calc_ck_div(int is_tft
, int pck
, int *pck_div
)
444 lck
= clk_get_rate(lcdc
.lcd_ck
);
445 *pck_div
= (lck
+ pck
- 1) / pck
;
447 *pck_div
= max(2, *pck_div
);
449 *pck_div
= max(3, *pck_div
);
450 if (*pck_div
> 255) {
451 /* FIXME: try to adjust logic clock divider as well */
453 dev_warn(lcdc
.fbdev
->dev
, "pixclock %d kHz too low.\n",
458 static inline void setup_regs(void)
461 struct lcd_panel
*panel
= lcdc
.fbdev
->panel
;
462 int is_tft
= panel
->config
& OMAP_LCDC_PANEL_TFT
;
466 l
= omap_readl(OMAP_LCDC_CONTROL
);
467 l
&= ~OMAP_LCDC_CTRL_LCD_TFT
;
468 l
|= is_tft
? OMAP_LCDC_CTRL_LCD_TFT
: 0;
469 #ifdef CONFIG_MACH_OMAP_PALMTE
470 /* FIXME:if (machine_is_omap_palmte()) { */
471 /* PalmTE uses alternate TFT setting in 8BPP mode */
472 l
|= (is_tft
&& panel
->bpp
== 8) ? 0x810000 : 0;
475 omap_writel(l
, OMAP_LCDC_CONTROL
);
477 l
= omap_readl(OMAP_LCDC_TIMING2
);
478 l
&= ~(((1 << 6) - 1) << 20);
479 l
|= (panel
->config
& OMAP_LCDC_SIGNAL_MASK
) << 20;
480 omap_writel(l
, OMAP_LCDC_TIMING2
);
482 l
= panel
->x_res
- 1;
483 l
|= (panel
->hsw
- 1) << 10;
484 l
|= (panel
->hfp
- 1) << 16;
485 l
|= (panel
->hbp
- 1) << 24;
486 omap_writel(l
, OMAP_LCDC_TIMING0
);
488 l
= panel
->y_res
- 1;
489 l
|= (panel
->vsw
- 1) << 10;
490 l
|= panel
->vfp
<< 16;
491 l
|= panel
->vbp
<< 24;
492 omap_writel(l
, OMAP_LCDC_TIMING1
);
494 l
= omap_readl(OMAP_LCDC_TIMING2
);
497 lck
= clk_get_rate(lcdc
.lcd_ck
);
500 calc_ck_div(is_tft
, panel
->pixel_clock
* 1000, &pcd
);
502 dev_warn(lcdc
.fbdev
->dev
,
503 "Pixel clock divider value is obsolete.\n"
504 "Try to set pixel_clock to %lu and pcd to 0 "
505 "in drivers/video/omap/lcd_%s.c and submit a patch.\n",
506 lck
/ panel
->pcd
/ 1000, panel
->name
);
511 l
|= panel
->acb
<< 8;
512 omap_writel(l
, OMAP_LCDC_TIMING2
);
514 /* update panel info with the exact clock */
515 panel
->pixel_clock
= lck
/ pcd
/ 1000;
519 * Configure the LCD controller, download the color palette and start a looped
520 * DMA transfer of the frame image data. Called only in internal
523 static int omap_lcdc_set_update_mode(enum omapfb_update_mode mode
)
527 if (mode
!= lcdc
.update_mode
) {
529 case OMAPFB_AUTO_UPDATE
:
533 /* Setup and start LCD DMA */
536 set_load_mode(OMAP_LCDC_LOAD_FRAME
);
537 enable_irqs(OMAP_LCDC_IRQ_DONE
);
538 /* This will start the actual DMA transfer */
540 lcdc
.update_mode
= mode
;
542 case OMAPFB_UPDATE_DISABLED
:
543 disable_controller();
545 lcdc
.update_mode
= mode
;
555 static enum omapfb_update_mode
omap_lcdc_get_update_mode(void)
557 return lcdc
.update_mode
;
560 /* PM code called only in internal controller mode */
561 static void omap_lcdc_suspend(void)
563 omap_lcdc_set_update_mode(OMAPFB_UPDATE_DISABLED
);
566 static void omap_lcdc_resume(void)
568 omap_lcdc_set_update_mode(OMAPFB_AUTO_UPDATE
);
571 static void omap_lcdc_get_caps(int plane
, struct omapfb_caps
*caps
)
576 int omap_lcdc_set_dma_callback(void (*callback
)(void *data
), void *data
)
578 BUG_ON(callback
== NULL
);
580 if (lcdc
.dma_callback
)
583 lcdc
.dma_callback
= callback
;
584 lcdc
.dma_callback_data
= data
;
588 EXPORT_SYMBOL(omap_lcdc_set_dma_callback
);
590 void omap_lcdc_free_dma_callback(void)
592 lcdc
.dma_callback
= NULL
;
594 EXPORT_SYMBOL(omap_lcdc_free_dma_callback
);
596 static void lcdc_dma_handler(u16 status
, void *data
)
598 if (lcdc
.dma_callback
)
599 lcdc
.dma_callback(lcdc
.dma_callback_data
);
602 static int alloc_palette_ram(void)
604 lcdc
.palette_virt
= dma_alloc_wc(lcdc
.fbdev
->dev
, MAX_PALETTE_SIZE
,
605 &lcdc
.palette_phys
, GFP_KERNEL
);
606 if (lcdc
.palette_virt
== NULL
) {
607 dev_err(lcdc
.fbdev
->dev
, "failed to alloc palette memory\n");
610 memset(lcdc
.palette_virt
, 0, MAX_PALETTE_SIZE
);
615 static void free_palette_ram(void)
617 dma_free_wc(lcdc
.fbdev
->dev
, MAX_PALETTE_SIZE
, lcdc
.palette_virt
,
621 static int alloc_fbmem(struct omapfb_mem_region
*region
)
625 struct lcd_panel
*panel
= lcdc
.fbdev
->panel
;
630 frame_size
= PAGE_ALIGN(panel
->x_res
* bpp
/ 8 * panel
->y_res
);
631 if (region
->size
> frame_size
)
632 frame_size
= region
->size
;
633 lcdc
.vram_size
= frame_size
;
634 lcdc
.vram_virt
= dma_alloc_wc(lcdc
.fbdev
->dev
, lcdc
.vram_size
,
635 &lcdc
.vram_phys
, GFP_KERNEL
);
636 if (lcdc
.vram_virt
== NULL
) {
637 dev_err(lcdc
.fbdev
->dev
, "unable to allocate FB DMA memory\n");
640 region
->size
= frame_size
;
641 region
->paddr
= lcdc
.vram_phys
;
642 region
->vaddr
= lcdc
.vram_virt
;
645 memset(lcdc
.vram_virt
, 0, lcdc
.vram_size
);
650 static void free_fbmem(void)
652 dma_free_wc(lcdc
.fbdev
->dev
, lcdc
.vram_size
, lcdc
.vram_virt
,
656 static int setup_fbmem(struct omapfb_mem_desc
*req_md
)
658 if (!req_md
->region_cnt
) {
659 dev_err(lcdc
.fbdev
->dev
, "no memory regions defined\n");
663 if (req_md
->region_cnt
> 1) {
664 dev_err(lcdc
.fbdev
->dev
, "only one plane is supported\n");
665 req_md
->region_cnt
= 1;
668 return alloc_fbmem(&req_md
->region
[0]);
671 static int omap_lcdc_init(struct omapfb_device
*fbdev
, int ext_mode
,
672 struct omapfb_mem_desc
*req_vram
)
682 lcdc
.ext_mode
= ext_mode
;
685 omap_writel(l
, OMAP_LCDC_CONTROL
);
688 * According to errata some platforms have a clock rate limitiation
690 lcdc
.lcd_ck
= clk_get(fbdev
->dev
, "lcd_ck");
691 if (IS_ERR(lcdc
.lcd_ck
)) {
692 dev_err(fbdev
->dev
, "unable to access LCD clock\n");
693 r
= PTR_ERR(lcdc
.lcd_ck
);
697 tc_ck
= clk_get(fbdev
->dev
, "tc_ck");
699 dev_err(fbdev
->dev
, "unable to access TC clock\n");
704 rate
= clk_get_rate(tc_ck
);
707 if (machine_is_ams_delta())
709 r
= clk_set_rate(lcdc
.lcd_ck
, rate
);
711 dev_err(fbdev
->dev
, "failed to adjust LCD rate\n");
714 clk_prepare_enable(lcdc
.lcd_ck
);
716 r
= request_irq(fbdev
->int_irq
, lcdc_irq_handler
, 0, MODULE_NAME
, fbdev
);
718 dev_err(fbdev
->dev
, "unable to get IRQ\n");
722 r
= omap_request_lcd_dma(lcdc_dma_handler
, NULL
);
724 dev_err(fbdev
->dev
, "unable to get LCD DMA\n");
728 omap_set_lcd_dma_single_transfer(ext_mode
);
729 omap_set_lcd_dma_ext_controller(ext_mode
);
732 if ((r
= alloc_palette_ram()) < 0)
735 if ((r
= setup_fbmem(req_vram
)) < 0)
738 pr_info("omapfb: LCDC initialized\n");
747 free_irq(fbdev
->int_irq
, lcdc
.fbdev
);
749 clk_disable_unprepare(lcdc
.lcd_ck
);
751 clk_put(lcdc
.lcd_ck
);
756 static void omap_lcdc_cleanup(void)
762 free_irq(lcdc
.fbdev
->int_irq
, lcdc
.fbdev
);
763 clk_disable_unprepare(lcdc
.lcd_ck
);
764 clk_put(lcdc
.lcd_ck
);
767 const struct lcd_ctrl omap1_int_ctrl
= {
769 .init
= omap_lcdc_init
,
770 .cleanup
= omap_lcdc_cleanup
,
771 .get_caps
= omap_lcdc_get_caps
,
772 .set_update_mode
= omap_lcdc_set_update_mode
,
773 .get_update_mode
= omap_lcdc_get_update_mode
,
774 .update_window
= NULL
,
775 .suspend
= omap_lcdc_suspend
,
776 .resume
= omap_lcdc_resume
,
777 .setup_plane
= omap_lcdc_setup_plane
,
778 .enable_plane
= omap_lcdc_enable_plane
,
779 .setcolreg
= omap_lcdc_setcolreg
,