2 * SuperH Mobile LCDC Framebuffer
4 * Copyright (c) 2008 Magnus Damm
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
11 #include <linux/atomic.h>
12 #include <linux/backlight.h>
13 #include <linux/clk.h>
14 #include <linux/console.h>
15 #include <linux/ctype.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/delay.h>
18 #include <linux/fbcon.h>
19 #include <linux/gpio.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/ioctl.h>
23 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/slab.h>
29 #include <linux/videodev2.h>
30 #include <linux/vmalloc.h>
32 #include <video/sh_mobile_lcdc.h>
34 #include "sh_mobile_lcdcfb.h"
36 /* ----------------------------------------------------------------------------
37 * Overlay register definitions
41 #define LDBCR_UPC(n) (1 << ((n) + 16))
42 #define LDBCR_UPF(n) (1 << ((n) + 8))
43 #define LDBCR_UPD(n) (1 << ((n) + 0))
44 #define LDBnBSIFR(n) (0xb20 + (n) * 0x20 + 0x00)
45 #define LDBBSIFR_EN (1 << 31)
46 #define LDBBSIFR_VS (1 << 29)
47 #define LDBBSIFR_BRSEL (1 << 28)
48 #define LDBBSIFR_MX (1 << 27)
49 #define LDBBSIFR_MY (1 << 26)
50 #define LDBBSIFR_CV3 (3 << 24)
51 #define LDBBSIFR_CV2 (2 << 24)
52 #define LDBBSIFR_CV1 (1 << 24)
53 #define LDBBSIFR_CV0 (0 << 24)
54 #define LDBBSIFR_CV_MASK (3 << 24)
55 #define LDBBSIFR_LAY_MASK (0xff << 16)
56 #define LDBBSIFR_LAY_SHIFT 16
57 #define LDBBSIFR_ROP3_MASK (0xff << 16)
58 #define LDBBSIFR_ROP3_SHIFT 16
59 #define LDBBSIFR_AL_PL8 (3 << 14)
60 #define LDBBSIFR_AL_PL1 (2 << 14)
61 #define LDBBSIFR_AL_PK (1 << 14)
62 #define LDBBSIFR_AL_1 (0 << 14)
63 #define LDBBSIFR_AL_MASK (3 << 14)
64 #define LDBBSIFR_SWPL (1 << 10)
65 #define LDBBSIFR_SWPW (1 << 9)
66 #define LDBBSIFR_SWPB (1 << 8)
67 #define LDBBSIFR_RY (1 << 7)
68 #define LDBBSIFR_CHRR_420 (2 << 0)
69 #define LDBBSIFR_CHRR_422 (1 << 0)
70 #define LDBBSIFR_CHRR_444 (0 << 0)
71 #define LDBBSIFR_RPKF_ARGB32 (0x00 << 0)
72 #define LDBBSIFR_RPKF_RGB16 (0x03 << 0)
73 #define LDBBSIFR_RPKF_RGB24 (0x0b << 0)
74 #define LDBBSIFR_RPKF_MASK (0x1f << 0)
75 #define LDBnBSSZR(n) (0xb20 + (n) * 0x20 + 0x04)
76 #define LDBBSSZR_BVSS_MASK (0xfff << 16)
77 #define LDBBSSZR_BVSS_SHIFT 16
78 #define LDBBSSZR_BHSS_MASK (0xfff << 0)
79 #define LDBBSSZR_BHSS_SHIFT 0
80 #define LDBnBLOCR(n) (0xb20 + (n) * 0x20 + 0x08)
81 #define LDBBLOCR_CVLC_MASK (0xfff << 16)
82 #define LDBBLOCR_CVLC_SHIFT 16
83 #define LDBBLOCR_CHLC_MASK (0xfff << 0)
84 #define LDBBLOCR_CHLC_SHIFT 0
85 #define LDBnBSMWR(n) (0xb20 + (n) * 0x20 + 0x0c)
86 #define LDBBSMWR_BSMWA_MASK (0xffff << 16)
87 #define LDBBSMWR_BSMWA_SHIFT 16
88 #define LDBBSMWR_BSMW_MASK (0xffff << 0)
89 #define LDBBSMWR_BSMW_SHIFT 0
90 #define LDBnBSAYR(n) (0xb20 + (n) * 0x20 + 0x10)
91 #define LDBBSAYR_FG1A_MASK (0xff << 24)
92 #define LDBBSAYR_FG1A_SHIFT 24
93 #define LDBBSAYR_FG1R_MASK (0xff << 16)
94 #define LDBBSAYR_FG1R_SHIFT 16
95 #define LDBBSAYR_FG1G_MASK (0xff << 8)
96 #define LDBBSAYR_FG1G_SHIFT 8
97 #define LDBBSAYR_FG1B_MASK (0xff << 0)
98 #define LDBBSAYR_FG1B_SHIFT 0
99 #define LDBnBSACR(n) (0xb20 + (n) * 0x20 + 0x14)
100 #define LDBBSACR_FG2A_MASK (0xff << 24)
101 #define LDBBSACR_FG2A_SHIFT 24
102 #define LDBBSACR_FG2R_MASK (0xff << 16)
103 #define LDBBSACR_FG2R_SHIFT 16
104 #define LDBBSACR_FG2G_MASK (0xff << 8)
105 #define LDBBSACR_FG2G_SHIFT 8
106 #define LDBBSACR_FG2B_MASK (0xff << 0)
107 #define LDBBSACR_FG2B_SHIFT 0
108 #define LDBnBSAAR(n) (0xb20 + (n) * 0x20 + 0x18)
109 #define LDBBSAAR_AP_MASK (0xff << 24)
110 #define LDBBSAAR_AP_SHIFT 24
111 #define LDBBSAAR_R_MASK (0xff << 16)
112 #define LDBBSAAR_R_SHIFT 16
113 #define LDBBSAAR_GY_MASK (0xff << 8)
114 #define LDBBSAAR_GY_SHIFT 8
115 #define LDBBSAAR_B_MASK (0xff << 0)
116 #define LDBBSAAR_B_SHIFT 0
117 #define LDBnBPPCR(n) (0xb20 + (n) * 0x20 + 0x1c)
118 #define LDBBPPCR_AP_MASK (0xff << 24)
119 #define LDBBPPCR_AP_SHIFT 24
120 #define LDBBPPCR_R_MASK (0xff << 16)
121 #define LDBBPPCR_R_SHIFT 16
122 #define LDBBPPCR_GY_MASK (0xff << 8)
123 #define LDBBPPCR_GY_SHIFT 8
124 #define LDBBPPCR_B_MASK (0xff << 0)
125 #define LDBBPPCR_B_SHIFT 0
126 #define LDBnBBGCL(n) (0xb10 + (n) * 0x04)
127 #define LDBBBGCL_BGA_MASK (0xff << 24)
128 #define LDBBBGCL_BGA_SHIFT 24
129 #define LDBBBGCL_BGR_MASK (0xff << 16)
130 #define LDBBBGCL_BGR_SHIFT 16
131 #define LDBBBGCL_BGG_MASK (0xff << 8)
132 #define LDBBBGCL_BGG_SHIFT 8
133 #define LDBBBGCL_BGB_MASK (0xff << 0)
134 #define LDBBBGCL_BGB_SHIFT 0
136 #define SIDE_B_OFFSET 0x1000
137 #define MIRROR_OFFSET 0x2000
139 #define MAX_XRES 1920
140 #define MAX_YRES 1080
142 enum sh_mobile_lcdc_overlay_mode
{
148 * struct sh_mobile_lcdc_overlay - LCDC display overlay
150 * @channel: LCDC channel this overlay belongs to
151 * @cfg: Overlay configuration
152 * @info: Frame buffer device
153 * @index: Overlay index (0-3)
154 * @base: Overlay registers base address
155 * @enabled: True if the overlay is enabled
156 * @mode: Overlay blending mode (alpha blend or ROP3)
157 * @alpha: Global alpha blending value (0-255, for alpha blending mode)
158 * @rop3: Raster operation (for ROP3 mode)
159 * @fb_mem: Frame buffer virtual memory address
160 * @fb_size: Frame buffer size in bytes
161 * @dma_handle: Frame buffer DMA address
162 * @base_addr_y: Overlay base address (RGB or luma component)
163 * @base_addr_c: Overlay base address (chroma component)
164 * @pan_y_offset: Panning linear offset in bytes (luma component)
165 * @format: Current pixelf format
166 * @xres: Horizontal visible resolution
167 * @xres_virtual: Horizontal total resolution
168 * @yres: Vertical visible resolution
169 * @yres_virtual: Vertical total resolution
170 * @pitch: Overlay line pitch
171 * @pos_x: Horizontal overlay position
172 * @pos_y: Vertical overlay position
174 struct sh_mobile_lcdc_overlay
{
175 struct sh_mobile_lcdc_chan
*channel
;
177 const struct sh_mobile_lcdc_overlay_cfg
*cfg
;
178 struct fb_info
*info
;
184 enum sh_mobile_lcdc_overlay_mode mode
;
189 unsigned long fb_size
;
191 dma_addr_t dma_handle
;
192 unsigned long base_addr_y
;
193 unsigned long base_addr_c
;
194 unsigned long pan_y_offset
;
196 const struct sh_mobile_lcdc_format_info
*format
;
198 unsigned int xres_virtual
;
200 unsigned int yres_virtual
;
206 struct sh_mobile_lcdc_priv
{
212 unsigned long lddckr
;
214 struct sh_mobile_lcdc_chan ch
[2];
215 struct sh_mobile_lcdc_overlay overlays
[4];
218 int forced_fourcc
; /* 2 channel LCDC must share fourcc setting */
221 /* -----------------------------------------------------------------------------
225 static unsigned long lcdc_offs_mainlcd
[NR_CH_REGS
] = {
226 [LDDCKPAT1R
] = 0x400,
227 [LDDCKPAT2R
] = 0x404,
245 static unsigned long lcdc_offs_sublcd
[NR_CH_REGS
] = {
246 [LDDCKPAT1R
] = 0x408,
247 [LDDCKPAT2R
] = 0x40c,
263 static bool banked(int reg_nr
)
283 static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan
*chan
)
285 return chan
->cfg
->chan
== LCDC_CHAN_SUBLCD
;
288 static void lcdc_write_chan(struct sh_mobile_lcdc_chan
*chan
,
289 int reg_nr
, unsigned long data
)
291 iowrite32(data
, chan
->lcdc
->base
+ chan
->reg_offs
[reg_nr
]);
293 iowrite32(data
, chan
->lcdc
->base
+ chan
->reg_offs
[reg_nr
] +
297 static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan
*chan
,
298 int reg_nr
, unsigned long data
)
300 iowrite32(data
, chan
->lcdc
->base
+ chan
->reg_offs
[reg_nr
] +
304 static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan
*chan
,
307 return ioread32(chan
->lcdc
->base
+ chan
->reg_offs
[reg_nr
]);
310 static void lcdc_write_overlay(struct sh_mobile_lcdc_overlay
*ovl
,
311 int reg
, unsigned long data
)
313 iowrite32(data
, ovl
->channel
->lcdc
->base
+ reg
);
314 iowrite32(data
, ovl
->channel
->lcdc
->base
+ reg
+ SIDE_B_OFFSET
);
317 static void lcdc_write(struct sh_mobile_lcdc_priv
*priv
,
318 unsigned long reg_offs
, unsigned long data
)
320 iowrite32(data
, priv
->base
+ reg_offs
);
323 static unsigned long lcdc_read(struct sh_mobile_lcdc_priv
*priv
,
324 unsigned long reg_offs
)
326 return ioread32(priv
->base
+ reg_offs
);
329 static void lcdc_wait_bit(struct sh_mobile_lcdc_priv
*priv
,
330 unsigned long reg_offs
,
331 unsigned long mask
, unsigned long until
)
333 while ((lcdc_read(priv
, reg_offs
) & mask
) != until
)
337 /* -----------------------------------------------------------------------------
341 static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv
*priv
)
343 if (atomic_inc_and_test(&priv
->hw_usecnt
)) {
345 clk_prepare_enable(priv
->dot_clk
);
346 pm_runtime_get_sync(priv
->dev
);
350 static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv
*priv
)
352 if (atomic_sub_return(1, &priv
->hw_usecnt
) == -1) {
353 pm_runtime_put(priv
->dev
);
355 clk_disable_unprepare(priv
->dot_clk
);
359 static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv
*priv
,
365 switch (clock_source
) {
368 priv
->lddckr
= LDDCKR_ICKSEL_BUS
;
370 case LCDC_CLK_PERIPHERAL
:
371 str
= "peripheral_clk";
372 priv
->lddckr
= LDDCKR_ICKSEL_MIPI
;
374 case LCDC_CLK_EXTERNAL
:
376 priv
->lddckr
= LDDCKR_ICKSEL_HDMI
;
385 clk
= clk_get(priv
->dev
, str
);
387 dev_err(priv
->dev
, "cannot get dot clock %s\n", str
);
395 /* -----------------------------------------------------------------------------
396 * Display, panel and deferred I/O
399 static void lcdc_sys_write_index(void *handle
, unsigned long data
)
401 struct sh_mobile_lcdc_chan
*ch
= handle
;
403 lcdc_write(ch
->lcdc
, _LDDWD0R
, data
| LDDWDxR_WDACT
);
404 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
405 lcdc_write(ch
->lcdc
, _LDDWAR
, LDDWAR_WA
|
406 (lcdc_chan_is_sublcd(ch
) ? 2 : 0));
407 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
410 static void lcdc_sys_write_data(void *handle
, unsigned long data
)
412 struct sh_mobile_lcdc_chan
*ch
= handle
;
414 lcdc_write(ch
->lcdc
, _LDDWD0R
, data
| LDDWDxR_WDACT
| LDDWDxR_RSW
);
415 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
416 lcdc_write(ch
->lcdc
, _LDDWAR
, LDDWAR_WA
|
417 (lcdc_chan_is_sublcd(ch
) ? 2 : 0));
418 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
421 static unsigned long lcdc_sys_read_data(void *handle
)
423 struct sh_mobile_lcdc_chan
*ch
= handle
;
425 lcdc_write(ch
->lcdc
, _LDDRDR
, LDDRDR_RSR
);
426 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
427 lcdc_write(ch
->lcdc
, _LDDRAR
, LDDRAR_RA
|
428 (lcdc_chan_is_sublcd(ch
) ? 2 : 0));
430 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
432 return lcdc_read(ch
->lcdc
, _LDDRDR
) & LDDRDR_DRD_MASK
;
435 static struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops
= {
436 .write_index
= lcdc_sys_write_index
,
437 .write_data
= lcdc_sys_write_data
,
438 .read_data
= lcdc_sys_read_data
,
441 static int sh_mobile_lcdc_sginit(struct fb_info
*info
,
442 struct list_head
*pagelist
)
444 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
445 unsigned int nr_pages_max
= ch
->fb_size
>> PAGE_SHIFT
;
449 sg_init_table(ch
->sglist
, nr_pages_max
);
451 list_for_each_entry(page
, pagelist
, lru
)
452 sg_set_page(&ch
->sglist
[nr_pages
++], page
, PAGE_SIZE
, 0);
457 static void sh_mobile_lcdc_deferred_io(struct fb_info
*info
,
458 struct list_head
*pagelist
)
460 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
461 const struct sh_mobile_lcdc_panel_cfg
*panel
= &ch
->cfg
->panel_cfg
;
463 /* enable clocks before accessing hardware */
464 sh_mobile_lcdc_clk_on(ch
->lcdc
);
467 * It's possible to get here without anything on the pagelist via
468 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
469 * invocation. In the former case, the acceleration routines are
470 * stepped in to when using the framebuffer console causing the
471 * workqueue to be scheduled without any dirty pages on the list.
473 * Despite this, a panel update is still needed given that the
474 * acceleration routines have their own methods for writing in
475 * that still need to be updated.
477 * The fsync() and empty pagelist case could be optimized for,
478 * but we don't bother, as any application exhibiting such
479 * behaviour is fundamentally broken anyways.
481 if (!list_empty(pagelist
)) {
482 unsigned int nr_pages
= sh_mobile_lcdc_sginit(info
, pagelist
);
484 /* trigger panel update */
485 dma_map_sg(ch
->lcdc
->dev
, ch
->sglist
, nr_pages
, DMA_TO_DEVICE
);
486 if (panel
->start_transfer
)
487 panel
->start_transfer(ch
, &sh_mobile_lcdc_sys_bus_ops
);
488 lcdc_write_chan(ch
, LDSM2R
, LDSM2R_OSTRG
);
489 dma_unmap_sg(ch
->lcdc
->dev
, ch
->sglist
, nr_pages
,
492 if (panel
->start_transfer
)
493 panel
->start_transfer(ch
, &sh_mobile_lcdc_sys_bus_ops
);
494 lcdc_write_chan(ch
, LDSM2R
, LDSM2R_OSTRG
);
498 static void sh_mobile_lcdc_deferred_io_touch(struct fb_info
*info
)
500 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
503 schedule_delayed_work(&info
->deferred_work
, fbdefio
->delay
);
506 static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan
*ch
)
508 const struct sh_mobile_lcdc_panel_cfg
*panel
= &ch
->cfg
->panel_cfg
;
513 ret
= ch
->tx_dev
->ops
->display_on(ch
->tx_dev
);
517 if (ret
== SH_MOBILE_LCDC_DISPLAY_DISCONNECTED
)
518 ch
->info
->state
= FBINFO_STATE_SUSPENDED
;
521 /* HDMI must be enabled before LCDC configuration */
522 if (panel
->display_on
)
526 static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan
*ch
)
528 const struct sh_mobile_lcdc_panel_cfg
*panel
= &ch
->cfg
->panel_cfg
;
530 if (panel
->display_off
)
531 panel
->display_off();
534 ch
->tx_dev
->ops
->display_off(ch
->tx_dev
);
537 static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo
*var
,
538 struct fb_info
*info
);
540 /* -----------------------------------------------------------------------------
544 struct sh_mobile_lcdc_format_info
{
551 static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos
[] = {
553 .fourcc
= V4L2_PIX_FMT_RGB565
,
556 .lddfr
= LDDFR_PKF_RGB16
,
558 .fourcc
= V4L2_PIX_FMT_BGR24
,
561 .lddfr
= LDDFR_PKF_RGB24
,
563 .fourcc
= V4L2_PIX_FMT_BGR32
,
566 .lddfr
= LDDFR_PKF_ARGB32
,
568 .fourcc
= V4L2_PIX_FMT_NV12
,
571 .lddfr
= LDDFR_CC
| LDDFR_YF_420
,
573 .fourcc
= V4L2_PIX_FMT_NV21
,
576 .lddfr
= LDDFR_CC
| LDDFR_YF_420
,
578 .fourcc
= V4L2_PIX_FMT_NV16
,
581 .lddfr
= LDDFR_CC
| LDDFR_YF_422
,
583 .fourcc
= V4L2_PIX_FMT_NV61
,
586 .lddfr
= LDDFR_CC
| LDDFR_YF_422
,
588 .fourcc
= V4L2_PIX_FMT_NV24
,
591 .lddfr
= LDDFR_CC
| LDDFR_YF_444
,
593 .fourcc
= V4L2_PIX_FMT_NV42
,
596 .lddfr
= LDDFR_CC
| LDDFR_YF_444
,
600 static const struct sh_mobile_lcdc_format_info
*
601 sh_mobile_format_info(u32 fourcc
)
605 for (i
= 0; i
< ARRAY_SIZE(sh_mobile_format_infos
); ++i
) {
606 if (sh_mobile_format_infos
[i
].fourcc
== fourcc
)
607 return &sh_mobile_format_infos
[i
];
613 static int sh_mobile_format_fourcc(const struct fb_var_screeninfo
*var
)
615 if (var
->grayscale
> 1)
616 return var
->grayscale
;
618 switch (var
->bits_per_pixel
) {
620 return V4L2_PIX_FMT_RGB565
;
622 return V4L2_PIX_FMT_BGR24
;
624 return V4L2_PIX_FMT_BGR32
;
630 static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo
*var
)
632 return var
->grayscale
> 1;
635 /* -----------------------------------------------------------------------------
636 * Start, stop and IRQ
639 static irqreturn_t
sh_mobile_lcdc_irq(int irq
, void *data
)
641 struct sh_mobile_lcdc_priv
*priv
= data
;
642 struct sh_mobile_lcdc_chan
*ch
;
643 unsigned long ldintr
;
647 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
648 ldintr
= lcdc_read(priv
, _LDINTR
);
649 lcdc_write(priv
, _LDINTR
, (ldintr
^ LDINTR_STATUS_MASK
) & ~LDINTR_VEE
);
651 /* figure out if this interrupt is for main or sub lcd */
652 is_sub
= (lcdc_read(priv
, _LDSR
) & LDSR_MSS
) ? 1 : 0;
654 /* wake up channel and disable clocks */
655 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
662 if (ldintr
& LDINTR_FS
) {
663 if (is_sub
== lcdc_chan_is_sublcd(ch
)) {
665 wake_up(&ch
->frame_end_wait
);
667 sh_mobile_lcdc_clk_off(priv
);
672 if (ldintr
& LDINTR_VES
)
673 complete(&ch
->vsync_completion
);
679 static int sh_mobile_lcdc_wait_for_vsync(struct sh_mobile_lcdc_chan
*ch
)
681 unsigned long ldintr
;
684 /* Enable VSync End interrupt and be careful not to acknowledge any
687 ldintr
= lcdc_read(ch
->lcdc
, _LDINTR
);
688 ldintr
|= LDINTR_VEE
| LDINTR_STATUS_MASK
;
689 lcdc_write(ch
->lcdc
, _LDINTR
, ldintr
);
691 ret
= wait_for_completion_interruptible_timeout(&ch
->vsync_completion
,
692 msecs_to_jiffies(100));
699 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv
*priv
,
702 unsigned long tmp
= lcdc_read(priv
, _LDCNT2R
);
705 /* start or stop the lcdc */
707 lcdc_write(priv
, _LDCNT2R
, tmp
| LDCNT2R_DO
);
709 lcdc_write(priv
, _LDCNT2R
, tmp
& ~LDCNT2R_DO
);
711 /* wait until power is applied/stopped on all channels */
712 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++)
713 if (lcdc_read(priv
, _LDCNT2R
) & priv
->ch
[k
].enabled
)
715 tmp
= lcdc_read_chan(&priv
->ch
[k
], LDPMR
)
717 if (start
&& tmp
== LDPMR_LPS
)
719 if (!start
&& tmp
== 0)
725 lcdc_write(priv
, _LDDCKSTPR
, 1); /* stop dotclock */
728 static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan
*ch
)
730 const struct fb_var_screeninfo
*var
= &ch
->info
->var
;
731 const struct fb_videomode
*mode
= &ch
->display
.mode
;
732 unsigned long h_total
, hsync_pos
, display_h_total
;
735 tmp
= ch
->ldmt1r_value
;
736 tmp
|= (var
->sync
& FB_SYNC_VERT_HIGH_ACT
) ? 0 : LDMT1R_VPOL
;
737 tmp
|= (var
->sync
& FB_SYNC_HOR_HIGH_ACT
) ? 0 : LDMT1R_HPOL
;
738 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DWPOL
) ? LDMT1R_DWPOL
: 0;
739 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DIPOL
) ? LDMT1R_DIPOL
: 0;
740 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DAPOL
) ? LDMT1R_DAPOL
: 0;
741 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_HSCNT
) ? LDMT1R_HSCNT
: 0;
742 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DWCNT
) ? LDMT1R_DWCNT
: 0;
743 lcdc_write_chan(ch
, LDMT1R
, tmp
);
746 lcdc_write_chan(ch
, LDMT2R
, ch
->cfg
->sys_bus_cfg
.ldmt2r
);
747 lcdc_write_chan(ch
, LDMT3R
, ch
->cfg
->sys_bus_cfg
.ldmt3r
);
749 /* horizontal configuration */
750 h_total
= mode
->xres
+ mode
->hsync_len
+ mode
->left_margin
751 + mode
->right_margin
;
752 tmp
= h_total
/ 8; /* HTCN */
753 tmp
|= (min(mode
->xres
, ch
->xres
) / 8) << 16; /* HDCN */
754 lcdc_write_chan(ch
, LDHCNR
, tmp
);
756 hsync_pos
= mode
->xres
+ mode
->right_margin
;
757 tmp
= hsync_pos
/ 8; /* HSYNP */
758 tmp
|= (mode
->hsync_len
/ 8) << 16; /* HSYNW */
759 lcdc_write_chan(ch
, LDHSYNR
, tmp
);
761 /* vertical configuration */
762 tmp
= mode
->yres
+ mode
->vsync_len
+ mode
->upper_margin
763 + mode
->lower_margin
; /* VTLN */
764 tmp
|= min(mode
->yres
, ch
->yres
) << 16; /* VDLN */
765 lcdc_write_chan(ch
, LDVLNR
, tmp
);
767 tmp
= mode
->yres
+ mode
->lower_margin
; /* VSYNP */
768 tmp
|= mode
->vsync_len
<< 16; /* VSYNW */
769 lcdc_write_chan(ch
, LDVSYNR
, tmp
);
771 /* Adjust horizontal synchronisation for HDMI */
772 display_h_total
= mode
->xres
+ mode
->hsync_len
+ mode
->left_margin
773 + mode
->right_margin
;
774 tmp
= ((mode
->xres
& 7) << 24) | ((display_h_total
& 7) << 16)
775 | ((mode
->hsync_len
& 7) << 8) | (hsync_pos
& 7);
776 lcdc_write_chan(ch
, LDHAJR
, tmp
);
777 lcdc_write_chan_mirror(ch
, LDHAJR
, tmp
);
780 static void sh_mobile_lcdc_overlay_setup(struct sh_mobile_lcdc_overlay
*ovl
)
785 lcdc_write(ovl
->channel
->lcdc
, LDBCR
, LDBCR_UPC(ovl
->index
));
786 lcdc_write_overlay(ovl
, LDBnBSIFR(ovl
->index
), 0);
787 lcdc_write(ovl
->channel
->lcdc
, LDBCR
,
788 LDBCR_UPF(ovl
->index
) | LDBCR_UPD(ovl
->index
));
792 ovl
->base_addr_y
= ovl
->dma_handle
;
793 ovl
->base_addr_c
= ovl
->dma_handle
794 + ovl
->xres_virtual
* ovl
->yres_virtual
;
797 case LCDC_OVERLAY_BLEND
:
798 format
= LDBBSIFR_EN
| (ovl
->alpha
<< LDBBSIFR_LAY_SHIFT
);
801 case LCDC_OVERLAY_ROP3
:
802 format
= LDBBSIFR_EN
| LDBBSIFR_BRSEL
803 | (ovl
->rop3
<< LDBBSIFR_ROP3_SHIFT
);
807 switch (ovl
->format
->fourcc
) {
808 case V4L2_PIX_FMT_RGB565
:
809 case V4L2_PIX_FMT_NV21
:
810 case V4L2_PIX_FMT_NV61
:
811 case V4L2_PIX_FMT_NV42
:
812 format
|= LDBBSIFR_SWPL
| LDBBSIFR_SWPW
;
814 case V4L2_PIX_FMT_BGR24
:
815 case V4L2_PIX_FMT_NV12
:
816 case V4L2_PIX_FMT_NV16
:
817 case V4L2_PIX_FMT_NV24
:
818 format
|= LDBBSIFR_SWPL
| LDBBSIFR_SWPW
| LDBBSIFR_SWPB
;
820 case V4L2_PIX_FMT_BGR32
:
822 format
|= LDBBSIFR_SWPL
;
826 switch (ovl
->format
->fourcc
) {
827 case V4L2_PIX_FMT_RGB565
:
828 format
|= LDBBSIFR_AL_1
| LDBBSIFR_RY
| LDBBSIFR_RPKF_RGB16
;
830 case V4L2_PIX_FMT_BGR24
:
831 format
|= LDBBSIFR_AL_1
| LDBBSIFR_RY
| LDBBSIFR_RPKF_RGB24
;
833 case V4L2_PIX_FMT_BGR32
:
834 format
|= LDBBSIFR_AL_PK
| LDBBSIFR_RY
| LDDFR_PKF_ARGB32
;
836 case V4L2_PIX_FMT_NV12
:
837 case V4L2_PIX_FMT_NV21
:
838 format
|= LDBBSIFR_AL_1
| LDBBSIFR_CHRR_420
;
840 case V4L2_PIX_FMT_NV16
:
841 case V4L2_PIX_FMT_NV61
:
842 format
|= LDBBSIFR_AL_1
| LDBBSIFR_CHRR_422
;
844 case V4L2_PIX_FMT_NV24
:
845 case V4L2_PIX_FMT_NV42
:
846 format
|= LDBBSIFR_AL_1
| LDBBSIFR_CHRR_444
;
850 lcdc_write(ovl
->channel
->lcdc
, LDBCR
, LDBCR_UPC(ovl
->index
));
852 lcdc_write_overlay(ovl
, LDBnBSIFR(ovl
->index
), format
);
854 lcdc_write_overlay(ovl
, LDBnBSSZR(ovl
->index
),
855 (ovl
->yres
<< LDBBSSZR_BVSS_SHIFT
) |
856 (ovl
->xres
<< LDBBSSZR_BHSS_SHIFT
));
857 lcdc_write_overlay(ovl
, LDBnBLOCR(ovl
->index
),
858 (ovl
->pos_y
<< LDBBLOCR_CVLC_SHIFT
) |
859 (ovl
->pos_x
<< LDBBLOCR_CHLC_SHIFT
));
860 lcdc_write_overlay(ovl
, LDBnBSMWR(ovl
->index
),
861 ovl
->pitch
<< LDBBSMWR_BSMW_SHIFT
);
863 lcdc_write_overlay(ovl
, LDBnBSAYR(ovl
->index
), ovl
->base_addr_y
);
864 lcdc_write_overlay(ovl
, LDBnBSACR(ovl
->index
), ovl
->base_addr_c
);
866 lcdc_write(ovl
->channel
->lcdc
, LDBCR
,
867 LDBCR_UPF(ovl
->index
) | LDBCR_UPD(ovl
->index
));
871 * __sh_mobile_lcdc_start - Configure and start the LCDC
874 * Configure all enabled channels and start the LCDC device. All external
875 * devices (clocks, MERAM, panels, ...) are not touched by this function.
877 static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv
*priv
)
879 struct sh_mobile_lcdc_chan
*ch
;
883 /* Enable LCDC channels. Read data from external memory, avoid using the
886 lcdc_write(priv
, _LDCNT2R
, priv
->ch
[0].enabled
| priv
->ch
[1].enabled
);
888 /* Stop the LCDC first and disable all interrupts. */
889 sh_mobile_lcdc_start_stop(priv
, 0);
890 lcdc_write(priv
, _LDINTR
, 0);
892 /* Configure power supply, dot clocks and start them. */
894 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
900 lcdc_write_chan(ch
, LDPMR
, 0);
902 m
= ch
->cfg
->clock_divider
;
906 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
909 lcdc_write_chan(ch
, LDDCKPAT1R
, 0);
910 lcdc_write_chan(ch
, LDDCKPAT2R
, (1 << (m
/2)) - 1);
914 tmp
|= m
<< (lcdc_chan_is_sublcd(ch
) ? 8 : 0);
917 lcdc_write(priv
, _LDDCKR
, tmp
);
918 lcdc_write(priv
, _LDDCKSTPR
, 0);
919 lcdc_wait_bit(priv
, _LDDCKSTPR
, ~0, 0);
921 /* Setup geometry, format, frame buffer memory and operation mode. */
922 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
927 sh_mobile_lcdc_geometry(ch
);
929 tmp
= ch
->format
->lddfr
;
931 if (ch
->format
->yuv
) {
932 switch (ch
->colorspace
) {
933 case V4L2_COLORSPACE_REC709
:
936 case V4L2_COLORSPACE_JPEG
:
942 lcdc_write_chan(ch
, LDDFR
, tmp
);
943 lcdc_write_chan(ch
, LDMLSR
, ch
->line_size
);
944 lcdc_write_chan(ch
, LDSA1R
, ch
->base_addr_y
);
946 lcdc_write_chan(ch
, LDSA2R
, ch
->base_addr_c
);
948 /* When using deferred I/O mode, configure the LCDC for one-shot
949 * operation and enable the frame end interrupt. Otherwise use
950 * continuous read mode.
952 if (ch
->ldmt1r_value
& LDMT1R_IFM
&&
953 ch
->cfg
->sys_bus_cfg
.deferred_io_msec
) {
954 lcdc_write_chan(ch
, LDSM1R
, LDSM1R_OS
);
955 lcdc_write(priv
, _LDINTR
, LDINTR_FE
);
957 lcdc_write_chan(ch
, LDSM1R
, 0);
961 /* Word and long word swap. */
962 switch (priv
->ch
[0].format
->fourcc
) {
963 case V4L2_PIX_FMT_RGB565
:
964 case V4L2_PIX_FMT_NV21
:
965 case V4L2_PIX_FMT_NV61
:
966 case V4L2_PIX_FMT_NV42
:
967 tmp
= LDDDSR_LS
| LDDDSR_WS
;
969 case V4L2_PIX_FMT_BGR24
:
970 case V4L2_PIX_FMT_NV12
:
971 case V4L2_PIX_FMT_NV16
:
972 case V4L2_PIX_FMT_NV24
:
973 tmp
= LDDDSR_LS
| LDDDSR_WS
| LDDDSR_BS
;
975 case V4L2_PIX_FMT_BGR32
:
980 lcdc_write(priv
, _LDDDSR
, tmp
);
982 /* Enable the display output. */
983 lcdc_write(priv
, _LDCNT1R
, LDCNT1R_DE
);
984 sh_mobile_lcdc_start_stop(priv
, 1);
988 static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv
*priv
)
990 struct sh_mobile_lcdc_chan
*ch
;
995 /* enable clocks before accessing the hardware */
996 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
997 if (priv
->ch
[k
].enabled
)
998 sh_mobile_lcdc_clk_on(priv
);
1002 lcdc_write(priv
, _LDCNT2R
, lcdc_read(priv
, _LDCNT2R
) | LDCNT2R_BR
);
1003 lcdc_wait_bit(priv
, _LDCNT2R
, LDCNT2R_BR
, 0);
1005 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
1006 const struct sh_mobile_lcdc_panel_cfg
*panel
;
1012 panel
= &ch
->cfg
->panel_cfg
;
1013 if (panel
->setup_sys
) {
1014 ret
= panel
->setup_sys(ch
, &sh_mobile_lcdc_sys_bus_ops
);
1020 /* Compute frame buffer base address and pitch for each channel. */
1021 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
1026 ch
->base_addr_y
= ch
->dma_handle
;
1027 ch
->base_addr_c
= ch
->dma_handle
1028 + ch
->xres_virtual
* ch
->yres_virtual
;
1029 ch
->line_size
= ch
->pitch
;
1032 for (k
= 0; k
< ARRAY_SIZE(priv
->overlays
); ++k
) {
1033 struct sh_mobile_lcdc_overlay
*ovl
= &priv
->overlays
[k
];
1034 sh_mobile_lcdc_overlay_setup(ovl
);
1037 /* Start the LCDC. */
1038 __sh_mobile_lcdc_start(priv
);
1040 /* Setup deferred I/O, tell the board code to enable the panels, and
1041 * turn backlight on.
1043 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
1048 tmp
= ch
->cfg
->sys_bus_cfg
.deferred_io_msec
;
1049 if (ch
->ldmt1r_value
& LDMT1R_IFM
&& tmp
) {
1050 ch
->defio
.deferred_io
= sh_mobile_lcdc_deferred_io
;
1051 ch
->defio
.delay
= msecs_to_jiffies(tmp
);
1052 ch
->info
->fbdefio
= &ch
->defio
;
1053 fb_deferred_io_init(ch
->info
);
1056 sh_mobile_lcdc_display_on(ch
);
1059 ch
->bl
->props
.power
= FB_BLANK_UNBLANK
;
1060 backlight_update_status(ch
->bl
);
1067 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv
*priv
)
1069 struct sh_mobile_lcdc_chan
*ch
;
1072 /* clean up deferred io and ask board code to disable panel */
1073 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
1078 /* deferred io mode:
1079 * flush frame, and wait for frame end interrupt
1080 * clean up deferred io and enable clock
1082 if (ch
->info
&& ch
->info
->fbdefio
) {
1084 schedule_delayed_work(&ch
->info
->deferred_work
, 0);
1085 wait_event(ch
->frame_end_wait
, ch
->frame_end
);
1086 fb_deferred_io_cleanup(ch
->info
);
1087 ch
->info
->fbdefio
= NULL
;
1088 sh_mobile_lcdc_clk_on(priv
);
1092 ch
->bl
->props
.power
= FB_BLANK_POWERDOWN
;
1093 backlight_update_status(ch
->bl
);
1096 sh_mobile_lcdc_display_off(ch
);
1100 if (priv
->started
) {
1101 sh_mobile_lcdc_start_stop(priv
, 0);
1106 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++)
1107 if (priv
->ch
[k
].enabled
)
1108 sh_mobile_lcdc_clk_off(priv
);
1111 static int __sh_mobile_lcdc_check_var(struct fb_var_screeninfo
*var
,
1112 struct fb_info
*info
)
1114 if (var
->xres
> MAX_XRES
|| var
->yres
> MAX_YRES
)
1117 /* Make sure the virtual resolution is at least as big as the visible
1120 if (var
->xres_virtual
< var
->xres
)
1121 var
->xres_virtual
= var
->xres
;
1122 if (var
->yres_virtual
< var
->yres
)
1123 var
->yres_virtual
= var
->yres
;
1125 if (sh_mobile_format_is_fourcc(var
)) {
1126 const struct sh_mobile_lcdc_format_info
*format
;
1128 format
= sh_mobile_format_info(var
->grayscale
);
1131 var
->bits_per_pixel
= format
->bpp
;
1133 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1137 var
->colorspace
= V4L2_COLORSPACE_SRGB
;
1138 else if (var
->colorspace
!= V4L2_COLORSPACE_REC709
)
1139 var
->colorspace
= V4L2_COLORSPACE_JPEG
;
1141 if (var
->bits_per_pixel
<= 16) { /* RGB 565 */
1142 var
->bits_per_pixel
= 16;
1143 var
->red
.offset
= 11;
1144 var
->red
.length
= 5;
1145 var
->green
.offset
= 5;
1146 var
->green
.length
= 6;
1147 var
->blue
.offset
= 0;
1148 var
->blue
.length
= 5;
1149 var
->transp
.offset
= 0;
1150 var
->transp
.length
= 0;
1151 } else if (var
->bits_per_pixel
<= 24) { /* RGB 888 */
1152 var
->bits_per_pixel
= 24;
1153 var
->red
.offset
= 16;
1154 var
->red
.length
= 8;
1155 var
->green
.offset
= 8;
1156 var
->green
.length
= 8;
1157 var
->blue
.offset
= 0;
1158 var
->blue
.length
= 8;
1159 var
->transp
.offset
= 0;
1160 var
->transp
.length
= 0;
1161 } else if (var
->bits_per_pixel
<= 32) { /* RGBA 888 */
1162 var
->bits_per_pixel
= 32;
1163 var
->red
.offset
= 16;
1164 var
->red
.length
= 8;
1165 var
->green
.offset
= 8;
1166 var
->green
.length
= 8;
1167 var
->blue
.offset
= 0;
1168 var
->blue
.length
= 8;
1169 var
->transp
.offset
= 24;
1170 var
->transp
.length
= 8;
1174 var
->red
.msb_right
= 0;
1175 var
->green
.msb_right
= 0;
1176 var
->blue
.msb_right
= 0;
1177 var
->transp
.msb_right
= 0;
1180 /* Make sure we don't exceed our allocated memory. */
1181 if (var
->xres_virtual
* var
->yres_virtual
* var
->bits_per_pixel
/ 8 >
1188 /* -----------------------------------------------------------------------------
1189 * Frame buffer operations - Overlays
1193 overlay_alpha_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1195 struct fb_info
*info
= dev_get_drvdata(dev
);
1196 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1198 return scnprintf(buf
, PAGE_SIZE
, "%u\n", ovl
->alpha
);
1202 overlay_alpha_store(struct device
*dev
, struct device_attribute
*attr
,
1203 const char *buf
, size_t count
)
1205 struct fb_info
*info
= dev_get_drvdata(dev
);
1206 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1210 alpha
= simple_strtoul(buf
, &endp
, 10);
1214 if (endp
- buf
!= count
)
1220 if (ovl
->alpha
!= alpha
) {
1223 if (ovl
->mode
== LCDC_OVERLAY_BLEND
&& ovl
->enabled
)
1224 sh_mobile_lcdc_overlay_setup(ovl
);
1231 overlay_mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1233 struct fb_info
*info
= dev_get_drvdata(dev
);
1234 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1236 return scnprintf(buf
, PAGE_SIZE
, "%u\n", ovl
->mode
);
1240 overlay_mode_store(struct device
*dev
, struct device_attribute
*attr
,
1241 const char *buf
, size_t count
)
1243 struct fb_info
*info
= dev_get_drvdata(dev
);
1244 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1248 mode
= simple_strtoul(buf
, &endp
, 10);
1252 if (endp
- buf
!= count
)
1255 if (mode
!= LCDC_OVERLAY_BLEND
&& mode
!= LCDC_OVERLAY_ROP3
)
1258 if (ovl
->mode
!= mode
) {
1262 sh_mobile_lcdc_overlay_setup(ovl
);
1269 overlay_position_show(struct device
*dev
, struct device_attribute
*attr
,
1272 struct fb_info
*info
= dev_get_drvdata(dev
);
1273 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1275 return scnprintf(buf
, PAGE_SIZE
, "%d,%d\n", ovl
->pos_x
, ovl
->pos_y
);
1279 overlay_position_store(struct device
*dev
, struct device_attribute
*attr
,
1280 const char *buf
, size_t count
)
1282 struct fb_info
*info
= dev_get_drvdata(dev
);
1283 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1288 pos_x
= simple_strtol(buf
, &endp
, 10);
1292 pos_y
= simple_strtol(endp
+ 1, &endp
, 10);
1296 if (endp
- buf
!= count
)
1299 if (ovl
->pos_x
!= pos_x
|| ovl
->pos_y
!= pos_y
) {
1304 sh_mobile_lcdc_overlay_setup(ovl
);
1311 overlay_rop3_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1313 struct fb_info
*info
= dev_get_drvdata(dev
);
1314 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1316 return scnprintf(buf
, PAGE_SIZE
, "%u\n", ovl
->rop3
);
1320 overlay_rop3_store(struct device
*dev
, struct device_attribute
*attr
,
1321 const char *buf
, size_t count
)
1323 struct fb_info
*info
= dev_get_drvdata(dev
);
1324 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1328 rop3
= simple_strtoul(buf
, &endp
, 10);
1332 if (endp
- buf
!= count
)
1338 if (ovl
->rop3
!= rop3
) {
1341 if (ovl
->mode
== LCDC_OVERLAY_ROP3
&& ovl
->enabled
)
1342 sh_mobile_lcdc_overlay_setup(ovl
);
1348 static const struct device_attribute overlay_sysfs_attrs
[] = {
1349 __ATTR(ovl_alpha
, S_IRUGO
|S_IWUSR
,
1350 overlay_alpha_show
, overlay_alpha_store
),
1351 __ATTR(ovl_mode
, S_IRUGO
|S_IWUSR
,
1352 overlay_mode_show
, overlay_mode_store
),
1353 __ATTR(ovl_position
, S_IRUGO
|S_IWUSR
,
1354 overlay_position_show
, overlay_position_store
),
1355 __ATTR(ovl_rop3
, S_IRUGO
|S_IWUSR
,
1356 overlay_rop3_show
, overlay_rop3_store
),
1359 static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix
= {
1360 .id
= "SH Mobile LCDC",
1361 .type
= FB_TYPE_PACKED_PIXELS
,
1362 .visual
= FB_VISUAL_TRUECOLOR
,
1363 .accel
= FB_ACCEL_NONE
,
1367 .capabilities
= FB_CAP_FOURCC
,
1370 static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo
*var
,
1371 struct fb_info
*info
)
1373 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1374 unsigned long base_addr_y
;
1375 unsigned long base_addr_c
;
1376 unsigned long y_offset
;
1377 unsigned long c_offset
;
1379 if (!ovl
->format
->yuv
) {
1380 y_offset
= (var
->yoffset
* ovl
->xres_virtual
+ var
->xoffset
)
1381 * ovl
->format
->bpp
/ 8;
1384 unsigned int xsub
= ovl
->format
->bpp
< 24 ? 2 : 1;
1385 unsigned int ysub
= ovl
->format
->bpp
< 16 ? 2 : 1;
1387 y_offset
= var
->yoffset
* ovl
->xres_virtual
+ var
->xoffset
;
1388 c_offset
= var
->yoffset
/ ysub
* ovl
->xres_virtual
* 2 / xsub
1389 + var
->xoffset
* 2 / xsub
;
1392 /* If the Y offset hasn't changed, the C offset hasn't either. There's
1393 * nothing to do in that case.
1395 if (y_offset
== ovl
->pan_y_offset
)
1398 /* Set the source address for the next refresh */
1399 base_addr_y
= ovl
->dma_handle
+ y_offset
;
1400 base_addr_c
= ovl
->dma_handle
+ ovl
->xres_virtual
* ovl
->yres_virtual
1403 ovl
->base_addr_y
= base_addr_y
;
1404 ovl
->base_addr_c
= base_addr_c
;
1405 ovl
->pan_y_offset
= y_offset
;
1407 lcdc_write(ovl
->channel
->lcdc
, LDBCR
, LDBCR_UPC(ovl
->index
));
1409 lcdc_write_overlay(ovl
, LDBnBSAYR(ovl
->index
), ovl
->base_addr_y
);
1410 lcdc_write_overlay(ovl
, LDBnBSACR(ovl
->index
), ovl
->base_addr_c
);
1412 lcdc_write(ovl
->channel
->lcdc
, LDBCR
,
1413 LDBCR_UPF(ovl
->index
) | LDBCR_UPD(ovl
->index
));
1418 static int sh_mobile_lcdc_overlay_ioctl(struct fb_info
*info
, unsigned int cmd
,
1421 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1424 case FBIO_WAITFORVSYNC
:
1425 return sh_mobile_lcdc_wait_for_vsync(ovl
->channel
);
1428 return -ENOIOCTLCMD
;
1432 static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo
*var
,
1433 struct fb_info
*info
)
1435 return __sh_mobile_lcdc_check_var(var
, info
);
1438 static int sh_mobile_lcdc_overlay_set_par(struct fb_info
*info
)
1440 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1443 sh_mobile_format_info(sh_mobile_format_fourcc(&info
->var
));
1445 ovl
->xres
= info
->var
.xres
;
1446 ovl
->xres_virtual
= info
->var
.xres_virtual
;
1447 ovl
->yres
= info
->var
.yres
;
1448 ovl
->yres_virtual
= info
->var
.yres_virtual
;
1450 if (ovl
->format
->yuv
)
1451 ovl
->pitch
= info
->var
.xres_virtual
;
1453 ovl
->pitch
= info
->var
.xres_virtual
* ovl
->format
->bpp
/ 8;
1455 sh_mobile_lcdc_overlay_setup(ovl
);
1457 info
->fix
.line_length
= ovl
->pitch
;
1459 if (sh_mobile_format_is_fourcc(&info
->var
)) {
1460 info
->fix
.type
= FB_TYPE_FOURCC
;
1461 info
->fix
.visual
= FB_VISUAL_FOURCC
;
1463 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1464 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
1470 /* Overlay blanking. Disable the overlay when blanked. */
1471 static int sh_mobile_lcdc_overlay_blank(int blank
, struct fb_info
*info
)
1473 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1475 ovl
->enabled
= !blank
;
1476 sh_mobile_lcdc_overlay_setup(ovl
);
1478 /* Prevent the backlight from receiving a blanking event by returning
1485 sh_mobile_lcdc_overlay_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
1487 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1489 return dma_mmap_coherent(ovl
->channel
->lcdc
->dev
, vma
, ovl
->fb_mem
,
1490 ovl
->dma_handle
, ovl
->fb_size
);
1493 static const struct fb_ops sh_mobile_lcdc_overlay_ops
= {
1494 .owner
= THIS_MODULE
,
1495 .fb_read
= fb_sys_read
,
1496 .fb_write
= fb_sys_write
,
1497 .fb_fillrect
= sys_fillrect
,
1498 .fb_copyarea
= sys_copyarea
,
1499 .fb_imageblit
= sys_imageblit
,
1500 .fb_blank
= sh_mobile_lcdc_overlay_blank
,
1501 .fb_pan_display
= sh_mobile_lcdc_overlay_pan
,
1502 .fb_ioctl
= sh_mobile_lcdc_overlay_ioctl
,
1503 .fb_check_var
= sh_mobile_lcdc_overlay_check_var
,
1504 .fb_set_par
= sh_mobile_lcdc_overlay_set_par
,
1505 .fb_mmap
= sh_mobile_lcdc_overlay_mmap
,
1509 sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay
*ovl
)
1511 struct fb_info
*info
= ovl
->info
;
1513 if (info
== NULL
|| info
->dev
== NULL
)
1516 unregister_framebuffer(ovl
->info
);
1520 sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay
*ovl
)
1522 struct sh_mobile_lcdc_priv
*lcdc
= ovl
->channel
->lcdc
;
1523 struct fb_info
*info
= ovl
->info
;
1530 ret
= register_framebuffer(info
);
1534 dev_info(lcdc
->dev
, "registered %s/overlay %u as %dx%d %dbpp.\n",
1535 dev_name(lcdc
->dev
), ovl
->index
, info
->var
.xres
,
1536 info
->var
.yres
, info
->var
.bits_per_pixel
);
1538 for (i
= 0; i
< ARRAY_SIZE(overlay_sysfs_attrs
); ++i
) {
1539 ret
= device_create_file(info
->dev
, &overlay_sysfs_attrs
[i
]);
1548 sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay
*ovl
)
1550 struct fb_info
*info
= ovl
->info
;
1552 if (info
== NULL
|| info
->device
== NULL
)
1555 framebuffer_release(info
);
1559 sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay
*ovl
)
1561 struct sh_mobile_lcdc_priv
*priv
= ovl
->channel
->lcdc
;
1562 struct fb_var_screeninfo
*var
;
1563 struct fb_info
*info
;
1565 /* Allocate and initialize the frame buffer device. */
1566 info
= framebuffer_alloc(0, priv
->dev
);
1572 info
->flags
= FBINFO_FLAG_DEFAULT
;
1573 info
->fbops
= &sh_mobile_lcdc_overlay_ops
;
1574 info
->device
= priv
->dev
;
1575 info
->screen_buffer
= ovl
->fb_mem
;
1578 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1579 * for NV12 and NV21.
1581 info
->fix
= sh_mobile_lcdc_overlay_fix
;
1582 snprintf(info
->fix
.id
, sizeof(info
->fix
.id
),
1583 "SH Mobile LCDC Overlay %u", ovl
->index
);
1584 info
->fix
.smem_start
= ovl
->dma_handle
;
1585 info
->fix
.smem_len
= ovl
->fb_size
;
1586 info
->fix
.line_length
= ovl
->pitch
;
1588 if (ovl
->format
->yuv
)
1589 info
->fix
.visual
= FB_VISUAL_FOURCC
;
1591 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
1593 switch (ovl
->format
->fourcc
) {
1594 case V4L2_PIX_FMT_NV12
:
1595 case V4L2_PIX_FMT_NV21
:
1596 info
->fix
.ypanstep
= 2;
1598 case V4L2_PIX_FMT_NV16
:
1599 case V4L2_PIX_FMT_NV61
:
1600 info
->fix
.xpanstep
= 2;
1603 /* Initialize variable screen information. */
1605 memset(var
, 0, sizeof(*var
));
1606 var
->xres
= ovl
->xres
;
1607 var
->yres
= ovl
->yres
;
1608 var
->xres_virtual
= ovl
->xres_virtual
;
1609 var
->yres_virtual
= ovl
->yres_virtual
;
1610 var
->activate
= FB_ACTIVATE_NOW
;
1612 /* Use the legacy API by default for RGB formats, and the FOURCC API
1615 if (!ovl
->format
->yuv
)
1616 var
->bits_per_pixel
= ovl
->format
->bpp
;
1618 var
->grayscale
= ovl
->format
->fourcc
;
1620 return sh_mobile_lcdc_overlay_check_var(var
, info
);
1623 /* -----------------------------------------------------------------------------
1624 * Frame buffer operations - main frame buffer
1627 static int sh_mobile_lcdc_setcolreg(u_int regno
,
1628 u_int red
, u_int green
, u_int blue
,
1629 u_int transp
, struct fb_info
*info
)
1631 u32
*palette
= info
->pseudo_palette
;
1633 if (regno
>= PALETTE_NR
)
1636 /* only FB_VISUAL_TRUECOLOR supported */
1638 red
>>= 16 - info
->var
.red
.length
;
1639 green
>>= 16 - info
->var
.green
.length
;
1640 blue
>>= 16 - info
->var
.blue
.length
;
1641 transp
>>= 16 - info
->var
.transp
.length
;
1643 palette
[regno
] = (red
<< info
->var
.red
.offset
) |
1644 (green
<< info
->var
.green
.offset
) |
1645 (blue
<< info
->var
.blue
.offset
) |
1646 (transp
<< info
->var
.transp
.offset
);
1651 static const struct fb_fix_screeninfo sh_mobile_lcdc_fix
= {
1652 .id
= "SH Mobile LCDC",
1653 .type
= FB_TYPE_PACKED_PIXELS
,
1654 .visual
= FB_VISUAL_TRUECOLOR
,
1655 .accel
= FB_ACCEL_NONE
,
1659 .capabilities
= FB_CAP_FOURCC
,
1662 static void sh_mobile_lcdc_fillrect(struct fb_info
*info
,
1663 const struct fb_fillrect
*rect
)
1665 sys_fillrect(info
, rect
);
1666 sh_mobile_lcdc_deferred_io_touch(info
);
1669 static void sh_mobile_lcdc_copyarea(struct fb_info
*info
,
1670 const struct fb_copyarea
*area
)
1672 sys_copyarea(info
, area
);
1673 sh_mobile_lcdc_deferred_io_touch(info
);
1676 static void sh_mobile_lcdc_imageblit(struct fb_info
*info
,
1677 const struct fb_image
*image
)
1679 sys_imageblit(info
, image
);
1680 sh_mobile_lcdc_deferred_io_touch(info
);
1683 static int sh_mobile_lcdc_pan(struct fb_var_screeninfo
*var
,
1684 struct fb_info
*info
)
1686 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1687 struct sh_mobile_lcdc_priv
*priv
= ch
->lcdc
;
1688 unsigned long ldrcntr
;
1689 unsigned long base_addr_y
, base_addr_c
;
1690 unsigned long y_offset
;
1691 unsigned long c_offset
;
1693 if (!ch
->format
->yuv
) {
1694 y_offset
= (var
->yoffset
* ch
->xres_virtual
+ var
->xoffset
)
1695 * ch
->format
->bpp
/ 8;
1698 unsigned int xsub
= ch
->format
->bpp
< 24 ? 2 : 1;
1699 unsigned int ysub
= ch
->format
->bpp
< 16 ? 2 : 1;
1701 y_offset
= var
->yoffset
* ch
->xres_virtual
+ var
->xoffset
;
1702 c_offset
= var
->yoffset
/ ysub
* ch
->xres_virtual
* 2 / xsub
1703 + var
->xoffset
* 2 / xsub
;
1706 /* If the Y offset hasn't changed, the C offset hasn't either. There's
1707 * nothing to do in that case.
1709 if (y_offset
== ch
->pan_y_offset
)
1712 /* Set the source address for the next refresh */
1713 base_addr_y
= ch
->dma_handle
+ y_offset
;
1714 base_addr_c
= ch
->dma_handle
+ ch
->xres_virtual
* ch
->yres_virtual
1717 ch
->base_addr_y
= base_addr_y
;
1718 ch
->base_addr_c
= base_addr_c
;
1719 ch
->pan_y_offset
= y_offset
;
1721 lcdc_write_chan_mirror(ch
, LDSA1R
, base_addr_y
);
1722 if (ch
->format
->yuv
)
1723 lcdc_write_chan_mirror(ch
, LDSA2R
, base_addr_c
);
1725 ldrcntr
= lcdc_read(priv
, _LDRCNTR
);
1726 if (lcdc_chan_is_sublcd(ch
))
1727 lcdc_write(ch
->lcdc
, _LDRCNTR
, ldrcntr
^ LDRCNTR_SRS
);
1729 lcdc_write(ch
->lcdc
, _LDRCNTR
, ldrcntr
^ LDRCNTR_MRS
);
1732 sh_mobile_lcdc_deferred_io_touch(info
);
1737 static int sh_mobile_lcdc_ioctl(struct fb_info
*info
, unsigned int cmd
,
1740 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1744 case FBIO_WAITFORVSYNC
:
1745 retval
= sh_mobile_lcdc_wait_for_vsync(ch
);
1749 retval
= -ENOIOCTLCMD
;
1755 static void sh_mobile_fb_reconfig(struct fb_info
*info
)
1757 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1758 struct fb_var_screeninfo var
;
1759 struct fb_videomode mode
;
1761 if (ch
->use_count
> 1 || (ch
->use_count
== 1 && !info
->fbcon_par
))
1762 /* More framebuffer users are active */
1765 fb_var_to_videomode(&mode
, &info
->var
);
1767 if (fb_mode_is_equal(&ch
->display
.mode
, &mode
))
1770 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1772 fb_videomode_to_var(&var
, &ch
->display
.mode
);
1773 var
.width
= ch
->display
.width
;
1774 var
.height
= ch
->display
.height
;
1775 var
.activate
= FB_ACTIVATE_NOW
;
1777 if (fb_set_var(info
, &var
) < 0)
1778 /* Couldn't reconfigure, hopefully, can continue as before */
1781 fbcon_update_vcs(info
, true);
1785 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1786 * user == 1, or with console sem held, if user == 0.
1788 static int sh_mobile_lcdc_release(struct fb_info
*info
, int user
)
1790 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1792 mutex_lock(&ch
->open_lock
);
1793 dev_dbg(info
->dev
, "%s(): %d users\n", __func__
, ch
->use_count
);
1797 /* Nothing to reconfigure, when called from fbcon */
1800 sh_mobile_fb_reconfig(info
);
1804 mutex_unlock(&ch
->open_lock
);
1809 static int sh_mobile_lcdc_open(struct fb_info
*info
, int user
)
1811 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1813 mutex_lock(&ch
->open_lock
);
1816 dev_dbg(info
->dev
, "%s(): %d users\n", __func__
, ch
->use_count
);
1817 mutex_unlock(&ch
->open_lock
);
1822 static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo
*var
,
1823 struct fb_info
*info
)
1825 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1826 struct sh_mobile_lcdc_priv
*p
= ch
->lcdc
;
1827 unsigned int best_dist
= (unsigned int)-1;
1828 unsigned int best_xres
= 0;
1829 unsigned int best_yres
= 0;
1833 /* If board code provides us with a list of available modes, make sure
1834 * we use one of them. Find the mode closest to the requested one. The
1835 * distance between two modes is defined as the size of the
1836 * non-overlapping parts of the two rectangles.
1838 for (i
= 0; i
< ch
->cfg
->num_modes
; ++i
) {
1839 const struct fb_videomode
*mode
= &ch
->cfg
->lcd_modes
[i
];
1842 /* We can only round up. */
1843 if (var
->xres
> mode
->xres
|| var
->yres
> mode
->yres
)
1846 dist
= var
->xres
* var
->yres
+ mode
->xres
* mode
->yres
1847 - 2 * min(var
->xres
, mode
->xres
)
1848 * min(var
->yres
, mode
->yres
);
1850 if (dist
< best_dist
) {
1851 best_xres
= mode
->xres
;
1852 best_yres
= mode
->yres
;
1857 /* If no available mode can be used, return an error. */
1858 if (ch
->cfg
->num_modes
!= 0) {
1859 if (best_dist
== (unsigned int)-1)
1862 var
->xres
= best_xres
;
1863 var
->yres
= best_yres
;
1866 ret
= __sh_mobile_lcdc_check_var(var
, info
);
1870 /* only accept the forced_fourcc for dual channel configurations */
1871 if (p
->forced_fourcc
&&
1872 p
->forced_fourcc
!= sh_mobile_format_fourcc(var
))
1878 static int sh_mobile_lcdc_set_par(struct fb_info
*info
)
1880 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1883 sh_mobile_lcdc_stop(ch
->lcdc
);
1885 ch
->format
= sh_mobile_format_info(sh_mobile_format_fourcc(&info
->var
));
1886 ch
->colorspace
= info
->var
.colorspace
;
1888 ch
->xres
= info
->var
.xres
;
1889 ch
->xres_virtual
= info
->var
.xres_virtual
;
1890 ch
->yres
= info
->var
.yres
;
1891 ch
->yres_virtual
= info
->var
.yres_virtual
;
1893 if (ch
->format
->yuv
)
1894 ch
->pitch
= info
->var
.xres_virtual
;
1896 ch
->pitch
= info
->var
.xres_virtual
* ch
->format
->bpp
/ 8;
1898 ret
= sh_mobile_lcdc_start(ch
->lcdc
);
1900 dev_err(info
->dev
, "%s: unable to restart LCDC\n", __func__
);
1902 info
->fix
.line_length
= ch
->pitch
;
1904 if (sh_mobile_format_is_fourcc(&info
->var
)) {
1905 info
->fix
.type
= FB_TYPE_FOURCC
;
1906 info
->fix
.visual
= FB_VISUAL_FOURCC
;
1908 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1909 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
1916 * Screen blanking. Behavior is as follows:
1917 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1918 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1921 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1923 static int sh_mobile_lcdc_blank(int blank
, struct fb_info
*info
)
1925 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1926 struct sh_mobile_lcdc_priv
*p
= ch
->lcdc
;
1928 /* blank the screen? */
1929 if (blank
> FB_BLANK_UNBLANK
&& ch
->blank_status
== FB_BLANK_UNBLANK
) {
1930 struct fb_fillrect rect
= {
1934 sh_mobile_lcdc_fillrect(info
, &rect
);
1936 /* turn clocks on? */
1937 if (blank
<= FB_BLANK_NORMAL
&& ch
->blank_status
> FB_BLANK_NORMAL
) {
1938 sh_mobile_lcdc_clk_on(p
);
1940 /* turn clocks off? */
1941 if (blank
> FB_BLANK_NORMAL
&& ch
->blank_status
<= FB_BLANK_NORMAL
) {
1942 /* make sure the screen is updated with the black fill before
1943 * switching the clocks off. one vsync is not enough since
1944 * blanking may occur in the middle of a refresh. deferred io
1945 * mode will reenable the clocks and update the screen in time,
1946 * so it does not need this. */
1947 if (!info
->fbdefio
) {
1948 sh_mobile_lcdc_wait_for_vsync(ch
);
1949 sh_mobile_lcdc_wait_for_vsync(ch
);
1951 sh_mobile_lcdc_clk_off(p
);
1954 ch
->blank_status
= blank
;
1959 sh_mobile_lcdc_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
1961 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1963 return dma_mmap_coherent(ch
->lcdc
->dev
, vma
, ch
->fb_mem
,
1964 ch
->dma_handle
, ch
->fb_size
);
1967 static const struct fb_ops sh_mobile_lcdc_ops
= {
1968 .owner
= THIS_MODULE
,
1969 .fb_setcolreg
= sh_mobile_lcdc_setcolreg
,
1970 .fb_read
= fb_sys_read
,
1971 .fb_write
= fb_sys_write
,
1972 .fb_fillrect
= sh_mobile_lcdc_fillrect
,
1973 .fb_copyarea
= sh_mobile_lcdc_copyarea
,
1974 .fb_imageblit
= sh_mobile_lcdc_imageblit
,
1975 .fb_blank
= sh_mobile_lcdc_blank
,
1976 .fb_pan_display
= sh_mobile_lcdc_pan
,
1977 .fb_ioctl
= sh_mobile_lcdc_ioctl
,
1978 .fb_open
= sh_mobile_lcdc_open
,
1979 .fb_release
= sh_mobile_lcdc_release
,
1980 .fb_check_var
= sh_mobile_lcdc_check_var
,
1981 .fb_set_par
= sh_mobile_lcdc_set_par
,
1982 .fb_mmap
= sh_mobile_lcdc_mmap
,
1986 sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan
*ch
)
1988 if (ch
->info
&& ch
->info
->dev
)
1989 unregister_framebuffer(ch
->info
);
1993 sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan
*ch
)
1995 struct fb_info
*info
= ch
->info
;
1998 if (info
->fbdefio
) {
1999 ch
->sglist
= vmalloc(sizeof(struct scatterlist
) *
2000 ch
->fb_size
>> PAGE_SHIFT
);
2005 info
->bl_dev
= ch
->bl
;
2007 ret
= register_framebuffer(info
);
2011 dev_info(ch
->lcdc
->dev
, "registered %s/%s as %dx%d %dbpp.\n",
2012 dev_name(ch
->lcdc
->dev
), (ch
->cfg
->chan
== LCDC_CHAN_MAINLCD
) ?
2013 "mainlcd" : "sublcd", info
->var
.xres
, info
->var
.yres
,
2014 info
->var
.bits_per_pixel
);
2016 /* deferred io mode: disable clock to save power */
2017 if (info
->fbdefio
|| info
->state
== FBINFO_STATE_SUSPENDED
)
2018 sh_mobile_lcdc_clk_off(ch
->lcdc
);
2024 sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan
*ch
)
2026 struct fb_info
*info
= ch
->info
;
2028 if (!info
|| !info
->device
)
2033 fb_dealloc_cmap(&info
->cmap
);
2034 framebuffer_release(info
);
2038 sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan
*ch
,
2039 const struct fb_videomode
*modes
,
2040 unsigned int num_modes
)
2042 struct sh_mobile_lcdc_priv
*priv
= ch
->lcdc
;
2043 struct fb_var_screeninfo
*var
;
2044 struct fb_info
*info
;
2047 /* Allocate and initialize the frame buffer device. Create the modes
2048 * list and allocate the color map.
2050 info
= framebuffer_alloc(0, priv
->dev
);
2056 info
->flags
= FBINFO_FLAG_DEFAULT
;
2057 info
->fbops
= &sh_mobile_lcdc_ops
;
2058 info
->device
= priv
->dev
;
2059 info
->screen_buffer
= ch
->fb_mem
;
2060 info
->pseudo_palette
= &ch
->pseudo_palette
;
2063 fb_videomode_to_modelist(modes
, num_modes
, &info
->modelist
);
2065 ret
= fb_alloc_cmap(&info
->cmap
, PALETTE_NR
, 0);
2067 dev_err(priv
->dev
, "unable to allocate cmap\n");
2071 /* Initialize fixed screen information. Restrict pan to 2 lines steps
2072 * for NV12 and NV21.
2074 info
->fix
= sh_mobile_lcdc_fix
;
2075 info
->fix
.smem_start
= ch
->dma_handle
;
2076 info
->fix
.smem_len
= ch
->fb_size
;
2077 info
->fix
.line_length
= ch
->pitch
;
2079 if (ch
->format
->yuv
)
2080 info
->fix
.visual
= FB_VISUAL_FOURCC
;
2082 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
2084 switch (ch
->format
->fourcc
) {
2085 case V4L2_PIX_FMT_NV12
:
2086 case V4L2_PIX_FMT_NV21
:
2087 info
->fix
.ypanstep
= 2;
2089 case V4L2_PIX_FMT_NV16
:
2090 case V4L2_PIX_FMT_NV61
:
2091 info
->fix
.xpanstep
= 2;
2094 /* Initialize variable screen information using the first mode as
2098 fb_videomode_to_var(var
, modes
);
2099 var
->width
= ch
->display
.width
;
2100 var
->height
= ch
->display
.height
;
2101 var
->xres_virtual
= ch
->xres_virtual
;
2102 var
->yres_virtual
= ch
->yres_virtual
;
2103 var
->activate
= FB_ACTIVATE_NOW
;
2105 /* Use the legacy API by default for RGB formats, and the FOURCC API
2108 if (!ch
->format
->yuv
)
2109 var
->bits_per_pixel
= ch
->format
->bpp
;
2111 var
->grayscale
= ch
->format
->fourcc
;
2113 ret
= sh_mobile_lcdc_check_var(var
, info
);
2120 /* -----------------------------------------------------------------------------
2124 static int sh_mobile_lcdc_update_bl(struct backlight_device
*bdev
)
2126 struct sh_mobile_lcdc_chan
*ch
= bl_get_data(bdev
);
2127 int brightness
= bdev
->props
.brightness
;
2129 if (bdev
->props
.power
!= FB_BLANK_UNBLANK
||
2130 bdev
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
2133 ch
->bl_brightness
= brightness
;
2134 return ch
->cfg
->bl_info
.set_brightness(brightness
);
2137 static int sh_mobile_lcdc_get_brightness(struct backlight_device
*bdev
)
2139 struct sh_mobile_lcdc_chan
*ch
= bl_get_data(bdev
);
2141 return ch
->bl_brightness
;
2144 static int sh_mobile_lcdc_check_fb(struct backlight_device
*bdev
,
2145 struct fb_info
*info
)
2147 return (info
->bl_dev
== bdev
);
2150 static const struct backlight_ops sh_mobile_lcdc_bl_ops
= {
2151 .options
= BL_CORE_SUSPENDRESUME
,
2152 .update_status
= sh_mobile_lcdc_update_bl
,
2153 .get_brightness
= sh_mobile_lcdc_get_brightness
,
2154 .check_fb
= sh_mobile_lcdc_check_fb
,
2157 static struct backlight_device
*sh_mobile_lcdc_bl_probe(struct device
*parent
,
2158 struct sh_mobile_lcdc_chan
*ch
)
2160 struct backlight_device
*bl
;
2162 bl
= backlight_device_register(ch
->cfg
->bl_info
.name
, parent
, ch
,
2163 &sh_mobile_lcdc_bl_ops
, NULL
);
2165 dev_err(parent
, "unable to register backlight device: %ld\n",
2170 bl
->props
.max_brightness
= ch
->cfg
->bl_info
.max_brightness
;
2171 bl
->props
.brightness
= bl
->props
.max_brightness
;
2172 backlight_update_status(bl
);
2177 static void sh_mobile_lcdc_bl_remove(struct backlight_device
*bdev
)
2179 backlight_device_unregister(bdev
);
2182 /* -----------------------------------------------------------------------------
2186 static int sh_mobile_lcdc_suspend(struct device
*dev
)
2188 struct platform_device
*pdev
= to_platform_device(dev
);
2190 sh_mobile_lcdc_stop(platform_get_drvdata(pdev
));
2194 static int sh_mobile_lcdc_resume(struct device
*dev
)
2196 struct platform_device
*pdev
= to_platform_device(dev
);
2198 return sh_mobile_lcdc_start(platform_get_drvdata(pdev
));
2201 static int sh_mobile_lcdc_runtime_suspend(struct device
*dev
)
2203 struct sh_mobile_lcdc_priv
*priv
= dev_get_drvdata(dev
);
2205 /* turn off LCDC hardware */
2206 lcdc_write(priv
, _LDCNT1R
, 0);
2211 static int sh_mobile_lcdc_runtime_resume(struct device
*dev
)
2213 struct sh_mobile_lcdc_priv
*priv
= dev_get_drvdata(dev
);
2215 __sh_mobile_lcdc_start(priv
);
2220 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops
= {
2221 .suspend
= sh_mobile_lcdc_suspend
,
2222 .resume
= sh_mobile_lcdc_resume
,
2223 .runtime_suspend
= sh_mobile_lcdc_runtime_suspend
,
2224 .runtime_resume
= sh_mobile_lcdc_runtime_resume
,
2227 /* -----------------------------------------------------------------------------
2228 * Framebuffer notifier
2231 /* -----------------------------------------------------------------------------
2232 * Probe/remove and driver init/exit
2235 static const struct fb_videomode default_720p
= {
2236 .name
= "HDMI 720p",
2241 .right_margin
= 110,
2250 .sync
= FB_SYNC_VERT_HIGH_ACT
| FB_SYNC_HOR_HIGH_ACT
,
2253 static int sh_mobile_lcdc_remove(struct platform_device
*pdev
)
2255 struct sh_mobile_lcdc_priv
*priv
= platform_get_drvdata(pdev
);
2258 for (i
= 0; i
< ARRAY_SIZE(priv
->overlays
); i
++)
2259 sh_mobile_lcdc_overlay_fb_unregister(&priv
->overlays
[i
]);
2260 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++)
2261 sh_mobile_lcdc_channel_fb_unregister(&priv
->ch
[i
]);
2263 sh_mobile_lcdc_stop(priv
);
2265 for (i
= 0; i
< ARRAY_SIZE(priv
->overlays
); i
++) {
2266 struct sh_mobile_lcdc_overlay
*ovl
= &priv
->overlays
[i
];
2268 sh_mobile_lcdc_overlay_fb_cleanup(ovl
);
2271 dma_free_coherent(&pdev
->dev
, ovl
->fb_size
,
2272 ovl
->fb_mem
, ovl
->dma_handle
);
2275 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++) {
2276 struct sh_mobile_lcdc_chan
*ch
= &priv
->ch
[i
];
2279 ch
->tx_dev
->lcdc
= NULL
;
2280 module_put(ch
->cfg
->tx_dev
->dev
.driver
->owner
);
2283 sh_mobile_lcdc_channel_fb_cleanup(ch
);
2286 dma_free_coherent(&pdev
->dev
, ch
->fb_size
,
2287 ch
->fb_mem
, ch
->dma_handle
);
2290 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++) {
2291 struct sh_mobile_lcdc_chan
*ch
= &priv
->ch
[i
];
2294 sh_mobile_lcdc_bl_remove(ch
->bl
);
2295 mutex_destroy(&ch
->open_lock
);
2298 if (priv
->dot_clk
) {
2299 pm_runtime_disable(&pdev
->dev
);
2300 clk_put(priv
->dot_clk
);
2304 iounmap(priv
->base
);
2307 free_irq(priv
->irq
, priv
);
2312 static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan
*ch
)
2314 int interface_type
= ch
->cfg
->interface_type
;
2316 switch (interface_type
) {
2340 /* SUBLCD only supports SYS interface */
2341 if (lcdc_chan_is_sublcd(ch
)) {
2342 if (!(interface_type
& LDMT1R_IFM
))
2345 interface_type
&= ~LDMT1R_IFM
;
2348 ch
->ldmt1r_value
= interface_type
;
2353 sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_overlay
*ovl
)
2355 const struct sh_mobile_lcdc_format_info
*format
;
2356 struct device
*dev
= ovl
->channel
->lcdc
->dev
;
2359 if (ovl
->cfg
->fourcc
== 0)
2362 /* Validate the format. */
2363 format
= sh_mobile_format_info(ovl
->cfg
->fourcc
);
2364 if (format
== NULL
) {
2365 dev_err(dev
, "Invalid FOURCC %08x\n", ovl
->cfg
->fourcc
);
2369 ovl
->enabled
= false;
2370 ovl
->mode
= LCDC_OVERLAY_BLEND
;
2376 /* The default Y virtual resolution is twice the panel size to allow for
2379 ovl
->format
= format
;
2380 ovl
->xres
= ovl
->cfg
->max_xres
;
2381 ovl
->xres_virtual
= ovl
->xres
;
2382 ovl
->yres
= ovl
->cfg
->max_yres
;
2383 ovl
->yres_virtual
= ovl
->yres
* 2;
2386 ovl
->pitch
= ovl
->xres_virtual
* format
->bpp
/ 8;
2388 ovl
->pitch
= ovl
->xres_virtual
;
2390 /* Allocate frame buffer memory. */
2391 ovl
->fb_size
= ovl
->cfg
->max_xres
* ovl
->cfg
->max_yres
2392 * format
->bpp
/ 8 * 2;
2393 ovl
->fb_mem
= dma_alloc_coherent(dev
, ovl
->fb_size
, &ovl
->dma_handle
,
2396 dev_err(dev
, "unable to allocate buffer\n");
2400 ret
= sh_mobile_lcdc_overlay_fb_init(ovl
);
2408 sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan
*ch
)
2410 const struct sh_mobile_lcdc_format_info
*format
;
2411 const struct sh_mobile_lcdc_chan_cfg
*cfg
= ch
->cfg
;
2412 struct device
*dev
= ch
->lcdc
->dev
;
2413 const struct fb_videomode
*max_mode
;
2414 const struct fb_videomode
*mode
;
2415 unsigned int num_modes
;
2416 unsigned int max_size
;
2419 /* Validate the format. */
2420 format
= sh_mobile_format_info(cfg
->fourcc
);
2421 if (format
== NULL
) {
2422 dev_err(dev
, "Invalid FOURCC %08x.\n", cfg
->fourcc
);
2426 /* Iterate through the modes to validate them and find the highest
2432 for (i
= 0, mode
= cfg
->lcd_modes
; i
< cfg
->num_modes
; i
++, mode
++) {
2433 unsigned int size
= mode
->yres
* mode
->xres
;
2435 /* NV12/NV21 buffers must have even number of lines */
2436 if ((cfg
->fourcc
== V4L2_PIX_FMT_NV12
||
2437 cfg
->fourcc
== V4L2_PIX_FMT_NV21
) && (mode
->yres
& 0x1)) {
2438 dev_err(dev
, "yres must be multiple of 2 for "
2439 "YCbCr420 mode.\n");
2443 if (size
> max_size
) {
2450 max_size
= MAX_XRES
* MAX_YRES
;
2452 dev_dbg(dev
, "Found largest videomode %ux%u\n",
2453 max_mode
->xres
, max_mode
->yres
);
2455 if (cfg
->lcd_modes
== NULL
) {
2456 mode
= &default_720p
;
2459 mode
= cfg
->lcd_modes
;
2460 num_modes
= cfg
->num_modes
;
2463 /* Use the first mode as default. The default Y virtual resolution is
2464 * twice the panel size to allow for double-buffering.
2466 ch
->format
= format
;
2467 ch
->xres
= mode
->xres
;
2468 ch
->xres_virtual
= mode
->xres
;
2469 ch
->yres
= mode
->yres
;
2470 ch
->yres_virtual
= mode
->yres
* 2;
2473 ch
->colorspace
= V4L2_COLORSPACE_SRGB
;
2474 ch
->pitch
= ch
->xres_virtual
* format
->bpp
/ 8;
2476 ch
->colorspace
= V4L2_COLORSPACE_REC709
;
2477 ch
->pitch
= ch
->xres_virtual
;
2480 ch
->display
.width
= cfg
->panel_cfg
.width
;
2481 ch
->display
.height
= cfg
->panel_cfg
.height
;
2482 ch
->display
.mode
= *mode
;
2484 /* Allocate frame buffer memory. */
2485 ch
->fb_size
= max_size
* format
->bpp
/ 8 * 2;
2486 ch
->fb_mem
= dma_alloc_coherent(dev
, ch
->fb_size
, &ch
->dma_handle
,
2488 if (ch
->fb_mem
== NULL
) {
2489 dev_err(dev
, "unable to allocate buffer\n");
2493 /* Initialize the transmitter device if present. */
2495 if (!cfg
->tx_dev
->dev
.driver
||
2496 !try_module_get(cfg
->tx_dev
->dev
.driver
->owner
)) {
2497 dev_warn(dev
, "unable to get transmitter device\n");
2500 ch
->tx_dev
= platform_get_drvdata(cfg
->tx_dev
);
2501 ch
->tx_dev
->lcdc
= ch
;
2502 ch
->tx_dev
->def_mode
= *mode
;
2505 return sh_mobile_lcdc_channel_fb_init(ch
, mode
, num_modes
);
2508 static int sh_mobile_lcdc_probe(struct platform_device
*pdev
)
2510 struct sh_mobile_lcdc_info
*pdata
= pdev
->dev
.platform_data
;
2511 struct sh_mobile_lcdc_priv
*priv
;
2512 struct resource
*res
;
2518 dev_err(&pdev
->dev
, "no platform data defined\n");
2522 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2523 irq
= platform_get_irq(pdev
, 0);
2524 if (!res
|| irq
< 0) {
2525 dev_err(&pdev
->dev
, "cannot get platform resources\n");
2529 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
2533 priv
->dev
= &pdev
->dev
;
2535 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++)
2536 mutex_init(&priv
->ch
[i
].open_lock
);
2537 platform_set_drvdata(pdev
, priv
);
2539 error
= request_irq(irq
, sh_mobile_lcdc_irq
, 0,
2540 dev_name(&pdev
->dev
), priv
);
2542 dev_err(&pdev
->dev
, "unable to request irq\n");
2547 atomic_set(&priv
->hw_usecnt
, -1);
2549 for (i
= 0, num_channels
= 0; i
< ARRAY_SIZE(pdata
->ch
); i
++) {
2550 struct sh_mobile_lcdc_chan
*ch
= priv
->ch
+ num_channels
;
2553 ch
->cfg
= &pdata
->ch
[i
];
2555 error
= sh_mobile_lcdc_check_interface(ch
);
2557 dev_err(&pdev
->dev
, "unsupported interface type\n");
2560 init_waitqueue_head(&ch
->frame_end_wait
);
2561 init_completion(&ch
->vsync_completion
);
2563 /* probe the backlight is there is one defined */
2564 if (ch
->cfg
->bl_info
.max_brightness
)
2565 ch
->bl
= sh_mobile_lcdc_bl_probe(&pdev
->dev
, ch
);
2567 switch (pdata
->ch
[i
].chan
) {
2568 case LCDC_CHAN_MAINLCD
:
2569 ch
->enabled
= LDCNT2R_ME
;
2570 ch
->reg_offs
= lcdc_offs_mainlcd
;
2573 case LCDC_CHAN_SUBLCD
:
2574 ch
->enabled
= LDCNT2R_SE
;
2575 ch
->reg_offs
= lcdc_offs_sublcd
;
2581 if (!num_channels
) {
2582 dev_err(&pdev
->dev
, "no channels defined\n");
2587 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
2588 if (num_channels
== 2)
2589 priv
->forced_fourcc
= pdata
->ch
[0].fourcc
;
2591 priv
->base
= ioremap(res
->start
, resource_size(res
));
2597 error
= sh_mobile_lcdc_setup_clocks(priv
, pdata
->clock_source
);
2599 dev_err(&pdev
->dev
, "unable to setup clocks\n");
2603 /* Enable runtime PM. */
2604 pm_runtime_enable(&pdev
->dev
);
2606 for (i
= 0; i
< num_channels
; i
++) {
2607 struct sh_mobile_lcdc_chan
*ch
= &priv
->ch
[i
];
2609 error
= sh_mobile_lcdc_channel_init(ch
);
2614 for (i
= 0; i
< ARRAY_SIZE(pdata
->overlays
); i
++) {
2615 struct sh_mobile_lcdc_overlay
*ovl
= &priv
->overlays
[i
];
2617 ovl
->cfg
= &pdata
->overlays
[i
];
2618 ovl
->channel
= &priv
->ch
[0];
2620 error
= sh_mobile_lcdc_overlay_init(ovl
);
2625 error
= sh_mobile_lcdc_start(priv
);
2627 dev_err(&pdev
->dev
, "unable to start hardware\n");
2631 for (i
= 0; i
< num_channels
; i
++) {
2632 struct sh_mobile_lcdc_chan
*ch
= priv
->ch
+ i
;
2634 error
= sh_mobile_lcdc_channel_fb_register(ch
);
2639 for (i
= 0; i
< ARRAY_SIZE(pdata
->overlays
); i
++) {
2640 struct sh_mobile_lcdc_overlay
*ovl
= &priv
->overlays
[i
];
2642 error
= sh_mobile_lcdc_overlay_fb_register(ovl
);
2649 sh_mobile_lcdc_remove(pdev
);
2654 static struct platform_driver sh_mobile_lcdc_driver
= {
2656 .name
= "sh_mobile_lcdc_fb",
2657 .pm
= &sh_mobile_lcdc_dev_pm_ops
,
2659 .probe
= sh_mobile_lcdc_probe
,
2660 .remove
= sh_mobile_lcdc_remove
,
2663 module_platform_driver(sh_mobile_lcdc_driver
);
2665 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
2666 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
2667 MODULE_LICENSE("GPL v2");