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/gpio.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/ioctl.h>
22 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/slab.h>
28 #include <linux/videodev2.h>
29 #include <linux/vmalloc.h>
31 #include <video/sh_mobile_lcdc.h>
33 #include "sh_mobile_lcdcfb.h"
35 /* ----------------------------------------------------------------------------
36 * Overlay register definitions
40 #define LDBCR_UPC(n) (1 << ((n) + 16))
41 #define LDBCR_UPF(n) (1 << ((n) + 8))
42 #define LDBCR_UPD(n) (1 << ((n) + 0))
43 #define LDBnBSIFR(n) (0xb20 + (n) * 0x20 + 0x00)
44 #define LDBBSIFR_EN (1 << 31)
45 #define LDBBSIFR_VS (1 << 29)
46 #define LDBBSIFR_BRSEL (1 << 28)
47 #define LDBBSIFR_MX (1 << 27)
48 #define LDBBSIFR_MY (1 << 26)
49 #define LDBBSIFR_CV3 (3 << 24)
50 #define LDBBSIFR_CV2 (2 << 24)
51 #define LDBBSIFR_CV1 (1 << 24)
52 #define LDBBSIFR_CV0 (0 << 24)
53 #define LDBBSIFR_CV_MASK (3 << 24)
54 #define LDBBSIFR_LAY_MASK (0xff << 16)
55 #define LDBBSIFR_LAY_SHIFT 16
56 #define LDBBSIFR_ROP3_MASK (0xff << 16)
57 #define LDBBSIFR_ROP3_SHIFT 16
58 #define LDBBSIFR_AL_PL8 (3 << 14)
59 #define LDBBSIFR_AL_PL1 (2 << 14)
60 #define LDBBSIFR_AL_PK (1 << 14)
61 #define LDBBSIFR_AL_1 (0 << 14)
62 #define LDBBSIFR_AL_MASK (3 << 14)
63 #define LDBBSIFR_SWPL (1 << 10)
64 #define LDBBSIFR_SWPW (1 << 9)
65 #define LDBBSIFR_SWPB (1 << 8)
66 #define LDBBSIFR_RY (1 << 7)
67 #define LDBBSIFR_CHRR_420 (2 << 0)
68 #define LDBBSIFR_CHRR_422 (1 << 0)
69 #define LDBBSIFR_CHRR_444 (0 << 0)
70 #define LDBBSIFR_RPKF_ARGB32 (0x00 << 0)
71 #define LDBBSIFR_RPKF_RGB16 (0x03 << 0)
72 #define LDBBSIFR_RPKF_RGB24 (0x0b << 0)
73 #define LDBBSIFR_RPKF_MASK (0x1f << 0)
74 #define LDBnBSSZR(n) (0xb20 + (n) * 0x20 + 0x04)
75 #define LDBBSSZR_BVSS_MASK (0xfff << 16)
76 #define LDBBSSZR_BVSS_SHIFT 16
77 #define LDBBSSZR_BHSS_MASK (0xfff << 0)
78 #define LDBBSSZR_BHSS_SHIFT 0
79 #define LDBnBLOCR(n) (0xb20 + (n) * 0x20 + 0x08)
80 #define LDBBLOCR_CVLC_MASK (0xfff << 16)
81 #define LDBBLOCR_CVLC_SHIFT 16
82 #define LDBBLOCR_CHLC_MASK (0xfff << 0)
83 #define LDBBLOCR_CHLC_SHIFT 0
84 #define LDBnBSMWR(n) (0xb20 + (n) * 0x20 + 0x0c)
85 #define LDBBSMWR_BSMWA_MASK (0xffff << 16)
86 #define LDBBSMWR_BSMWA_SHIFT 16
87 #define LDBBSMWR_BSMW_MASK (0xffff << 0)
88 #define LDBBSMWR_BSMW_SHIFT 0
89 #define LDBnBSAYR(n) (0xb20 + (n) * 0x20 + 0x10)
90 #define LDBBSAYR_FG1A_MASK (0xff << 24)
91 #define LDBBSAYR_FG1A_SHIFT 24
92 #define LDBBSAYR_FG1R_MASK (0xff << 16)
93 #define LDBBSAYR_FG1R_SHIFT 16
94 #define LDBBSAYR_FG1G_MASK (0xff << 8)
95 #define LDBBSAYR_FG1G_SHIFT 8
96 #define LDBBSAYR_FG1B_MASK (0xff << 0)
97 #define LDBBSAYR_FG1B_SHIFT 0
98 #define LDBnBSACR(n) (0xb20 + (n) * 0x20 + 0x14)
99 #define LDBBSACR_FG2A_MASK (0xff << 24)
100 #define LDBBSACR_FG2A_SHIFT 24
101 #define LDBBSACR_FG2R_MASK (0xff << 16)
102 #define LDBBSACR_FG2R_SHIFT 16
103 #define LDBBSACR_FG2G_MASK (0xff << 8)
104 #define LDBBSACR_FG2G_SHIFT 8
105 #define LDBBSACR_FG2B_MASK (0xff << 0)
106 #define LDBBSACR_FG2B_SHIFT 0
107 #define LDBnBSAAR(n) (0xb20 + (n) * 0x20 + 0x18)
108 #define LDBBSAAR_AP_MASK (0xff << 24)
109 #define LDBBSAAR_AP_SHIFT 24
110 #define LDBBSAAR_R_MASK (0xff << 16)
111 #define LDBBSAAR_R_SHIFT 16
112 #define LDBBSAAR_GY_MASK (0xff << 8)
113 #define LDBBSAAR_GY_SHIFT 8
114 #define LDBBSAAR_B_MASK (0xff << 0)
115 #define LDBBSAAR_B_SHIFT 0
116 #define LDBnBPPCR(n) (0xb20 + (n) * 0x20 + 0x1c)
117 #define LDBBPPCR_AP_MASK (0xff << 24)
118 #define LDBBPPCR_AP_SHIFT 24
119 #define LDBBPPCR_R_MASK (0xff << 16)
120 #define LDBBPPCR_R_SHIFT 16
121 #define LDBBPPCR_GY_MASK (0xff << 8)
122 #define LDBBPPCR_GY_SHIFT 8
123 #define LDBBPPCR_B_MASK (0xff << 0)
124 #define LDBBPPCR_B_SHIFT 0
125 #define LDBnBBGCL(n) (0xb10 + (n) * 0x04)
126 #define LDBBBGCL_BGA_MASK (0xff << 24)
127 #define LDBBBGCL_BGA_SHIFT 24
128 #define LDBBBGCL_BGR_MASK (0xff << 16)
129 #define LDBBBGCL_BGR_SHIFT 16
130 #define LDBBBGCL_BGG_MASK (0xff << 8)
131 #define LDBBBGCL_BGG_SHIFT 8
132 #define LDBBBGCL_BGB_MASK (0xff << 0)
133 #define LDBBBGCL_BGB_SHIFT 0
135 #define SIDE_B_OFFSET 0x1000
136 #define MIRROR_OFFSET 0x2000
138 #define MAX_XRES 1920
139 #define MAX_YRES 1080
141 enum sh_mobile_lcdc_overlay_mode
{
147 * struct sh_mobile_lcdc_overlay - LCDC display overlay
149 * @channel: LCDC channel this overlay belongs to
150 * @cfg: Overlay configuration
151 * @info: Frame buffer device
152 * @index: Overlay index (0-3)
153 * @base: Overlay registers base address
154 * @enabled: True if the overlay is enabled
155 * @mode: Overlay blending mode (alpha blend or ROP3)
156 * @alpha: Global alpha blending value (0-255, for alpha blending mode)
157 * @rop3: Raster operation (for ROP3 mode)
158 * @fb_mem: Frame buffer virtual memory address
159 * @fb_size: Frame buffer size in bytes
160 * @dma_handle: Frame buffer DMA address
161 * @base_addr_y: Overlay base address (RGB or luma component)
162 * @base_addr_c: Overlay base address (chroma component)
163 * @pan_y_offset: Panning linear offset in bytes (luma component)
164 * @format: Current pixelf format
165 * @xres: Horizontal visible resolution
166 * @xres_virtual: Horizontal total resolution
167 * @yres: Vertical visible resolution
168 * @yres_virtual: Vertical total resolution
169 * @pitch: Overlay line pitch
170 * @pos_x: Horizontal overlay position
171 * @pos_y: Vertical overlay position
173 struct sh_mobile_lcdc_overlay
{
174 struct sh_mobile_lcdc_chan
*channel
;
176 const struct sh_mobile_lcdc_overlay_cfg
*cfg
;
177 struct fb_info
*info
;
183 enum sh_mobile_lcdc_overlay_mode mode
;
188 unsigned long fb_size
;
190 dma_addr_t dma_handle
;
191 unsigned long base_addr_y
;
192 unsigned long base_addr_c
;
193 unsigned long pan_y_offset
;
195 const struct sh_mobile_lcdc_format_info
*format
;
197 unsigned int xres_virtual
;
199 unsigned int yres_virtual
;
205 struct sh_mobile_lcdc_priv
{
211 unsigned long lddckr
;
213 struct sh_mobile_lcdc_chan ch
[2];
214 struct sh_mobile_lcdc_overlay overlays
[4];
216 struct notifier_block notifier
;
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
);
538 sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan
*ch
,
539 const struct fb_videomode
*new_mode
)
541 dev_dbg(ch
->info
->dev
, "Old %ux%u, new %ux%u\n",
542 ch
->display
.mode
.xres
, ch
->display
.mode
.yres
,
543 new_mode
->xres
, new_mode
->yres
);
545 /* It can be a different monitor with an equal video-mode */
546 if (fb_mode_is_equal(&ch
->display
.mode
, new_mode
))
549 dev_dbg(ch
->info
->dev
, "Switching %u -> %u lines\n",
550 ch
->display
.mode
.yres
, new_mode
->yres
);
551 ch
->display
.mode
= *new_mode
;
556 static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo
*var
,
557 struct fb_info
*info
);
559 static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan
*ch
,
560 enum sh_mobile_lcdc_entity_event event
,
561 const struct fb_videomode
*mode
,
562 const struct fb_monspecs
*monspec
)
564 struct fb_info
*info
= ch
->info
;
565 struct fb_var_screeninfo var
;
569 case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT
:
572 if (lock_fb_info(info
)) {
575 ch
->display
.width
= monspec
->max_x
* 10;
576 ch
->display
.height
= monspec
->max_y
* 10;
578 if (!sh_mobile_lcdc_must_reconfigure(ch
, mode
) &&
579 info
->state
== FBINFO_STATE_RUNNING
) {
580 /* First activation with the default monitor.
581 * Just turn on, if we run a resume here, the
584 info
->var
.width
= ch
->display
.width
;
585 info
->var
.height
= ch
->display
.height
;
586 sh_mobile_lcdc_display_on(ch
);
588 /* New monitor or have to wake up */
589 fb_set_suspend(info
, 0);
593 unlock_fb_info(info
);
598 case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT
:
599 /* HDMI disconnect */
601 if (lock_fb_info(info
)) {
602 fb_set_suspend(info
, 1);
603 unlock_fb_info(info
);
608 case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE
:
609 /* Validate a proposed new mode */
610 fb_videomode_to_var(&var
, mode
);
611 var
.bits_per_pixel
= info
->var
.bits_per_pixel
;
612 var
.grayscale
= info
->var
.grayscale
;
613 ret
= sh_mobile_lcdc_check_var(&var
, info
);
620 /* -----------------------------------------------------------------------------
624 struct sh_mobile_lcdc_format_info
{
631 static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos
[] = {
633 .fourcc
= V4L2_PIX_FMT_RGB565
,
636 .lddfr
= LDDFR_PKF_RGB16
,
638 .fourcc
= V4L2_PIX_FMT_BGR24
,
641 .lddfr
= LDDFR_PKF_RGB24
,
643 .fourcc
= V4L2_PIX_FMT_BGR32
,
646 .lddfr
= LDDFR_PKF_ARGB32
,
648 .fourcc
= V4L2_PIX_FMT_NV12
,
651 .lddfr
= LDDFR_CC
| LDDFR_YF_420
,
653 .fourcc
= V4L2_PIX_FMT_NV21
,
656 .lddfr
= LDDFR_CC
| LDDFR_YF_420
,
658 .fourcc
= V4L2_PIX_FMT_NV16
,
661 .lddfr
= LDDFR_CC
| LDDFR_YF_422
,
663 .fourcc
= V4L2_PIX_FMT_NV61
,
666 .lddfr
= LDDFR_CC
| LDDFR_YF_422
,
668 .fourcc
= V4L2_PIX_FMT_NV24
,
671 .lddfr
= LDDFR_CC
| LDDFR_YF_444
,
673 .fourcc
= V4L2_PIX_FMT_NV42
,
676 .lddfr
= LDDFR_CC
| LDDFR_YF_444
,
680 static const struct sh_mobile_lcdc_format_info
*
681 sh_mobile_format_info(u32 fourcc
)
685 for (i
= 0; i
< ARRAY_SIZE(sh_mobile_format_infos
); ++i
) {
686 if (sh_mobile_format_infos
[i
].fourcc
== fourcc
)
687 return &sh_mobile_format_infos
[i
];
693 static int sh_mobile_format_fourcc(const struct fb_var_screeninfo
*var
)
695 if (var
->grayscale
> 1)
696 return var
->grayscale
;
698 switch (var
->bits_per_pixel
) {
700 return V4L2_PIX_FMT_RGB565
;
702 return V4L2_PIX_FMT_BGR24
;
704 return V4L2_PIX_FMT_BGR32
;
710 static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo
*var
)
712 return var
->grayscale
> 1;
715 /* -----------------------------------------------------------------------------
716 * Start, stop and IRQ
719 static irqreturn_t
sh_mobile_lcdc_irq(int irq
, void *data
)
721 struct sh_mobile_lcdc_priv
*priv
= data
;
722 struct sh_mobile_lcdc_chan
*ch
;
723 unsigned long ldintr
;
727 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
728 ldintr
= lcdc_read(priv
, _LDINTR
);
729 lcdc_write(priv
, _LDINTR
, (ldintr
^ LDINTR_STATUS_MASK
) & ~LDINTR_VEE
);
731 /* figure out if this interrupt is for main or sub lcd */
732 is_sub
= (lcdc_read(priv
, _LDSR
) & LDSR_MSS
) ? 1 : 0;
734 /* wake up channel and disable clocks */
735 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
742 if (ldintr
& LDINTR_FS
) {
743 if (is_sub
== lcdc_chan_is_sublcd(ch
)) {
745 wake_up(&ch
->frame_end_wait
);
747 sh_mobile_lcdc_clk_off(priv
);
752 if (ldintr
& LDINTR_VES
)
753 complete(&ch
->vsync_completion
);
759 static int sh_mobile_lcdc_wait_for_vsync(struct sh_mobile_lcdc_chan
*ch
)
761 unsigned long ldintr
;
764 /* Enable VSync End interrupt and be careful not to acknowledge any
767 ldintr
= lcdc_read(ch
->lcdc
, _LDINTR
);
768 ldintr
|= LDINTR_VEE
| LDINTR_STATUS_MASK
;
769 lcdc_write(ch
->lcdc
, _LDINTR
, ldintr
);
771 ret
= wait_for_completion_interruptible_timeout(&ch
->vsync_completion
,
772 msecs_to_jiffies(100));
779 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv
*priv
,
782 unsigned long tmp
= lcdc_read(priv
, _LDCNT2R
);
785 /* start or stop the lcdc */
787 lcdc_write(priv
, _LDCNT2R
, tmp
| LDCNT2R_DO
);
789 lcdc_write(priv
, _LDCNT2R
, tmp
& ~LDCNT2R_DO
);
791 /* wait until power is applied/stopped on all channels */
792 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++)
793 if (lcdc_read(priv
, _LDCNT2R
) & priv
->ch
[k
].enabled
)
795 tmp
= lcdc_read_chan(&priv
->ch
[k
], LDPMR
)
797 if (start
&& tmp
== LDPMR_LPS
)
799 if (!start
&& tmp
== 0)
805 lcdc_write(priv
, _LDDCKSTPR
, 1); /* stop dotclock */
808 static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan
*ch
)
810 const struct fb_var_screeninfo
*var
= &ch
->info
->var
;
811 const struct fb_videomode
*mode
= &ch
->display
.mode
;
812 unsigned long h_total
, hsync_pos
, display_h_total
;
815 tmp
= ch
->ldmt1r_value
;
816 tmp
|= (var
->sync
& FB_SYNC_VERT_HIGH_ACT
) ? 0 : LDMT1R_VPOL
;
817 tmp
|= (var
->sync
& FB_SYNC_HOR_HIGH_ACT
) ? 0 : LDMT1R_HPOL
;
818 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DWPOL
) ? LDMT1R_DWPOL
: 0;
819 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DIPOL
) ? LDMT1R_DIPOL
: 0;
820 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DAPOL
) ? LDMT1R_DAPOL
: 0;
821 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_HSCNT
) ? LDMT1R_HSCNT
: 0;
822 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DWCNT
) ? LDMT1R_DWCNT
: 0;
823 lcdc_write_chan(ch
, LDMT1R
, tmp
);
826 lcdc_write_chan(ch
, LDMT2R
, ch
->cfg
->sys_bus_cfg
.ldmt2r
);
827 lcdc_write_chan(ch
, LDMT3R
, ch
->cfg
->sys_bus_cfg
.ldmt3r
);
829 /* horizontal configuration */
830 h_total
= mode
->xres
+ mode
->hsync_len
+ mode
->left_margin
831 + mode
->right_margin
;
832 tmp
= h_total
/ 8; /* HTCN */
833 tmp
|= (min(mode
->xres
, ch
->xres
) / 8) << 16; /* HDCN */
834 lcdc_write_chan(ch
, LDHCNR
, tmp
);
836 hsync_pos
= mode
->xres
+ mode
->right_margin
;
837 tmp
= hsync_pos
/ 8; /* HSYNP */
838 tmp
|= (mode
->hsync_len
/ 8) << 16; /* HSYNW */
839 lcdc_write_chan(ch
, LDHSYNR
, tmp
);
841 /* vertical configuration */
842 tmp
= mode
->yres
+ mode
->vsync_len
+ mode
->upper_margin
843 + mode
->lower_margin
; /* VTLN */
844 tmp
|= min(mode
->yres
, ch
->yres
) << 16; /* VDLN */
845 lcdc_write_chan(ch
, LDVLNR
, tmp
);
847 tmp
= mode
->yres
+ mode
->lower_margin
; /* VSYNP */
848 tmp
|= mode
->vsync_len
<< 16; /* VSYNW */
849 lcdc_write_chan(ch
, LDVSYNR
, tmp
);
851 /* Adjust horizontal synchronisation for HDMI */
852 display_h_total
= mode
->xres
+ mode
->hsync_len
+ mode
->left_margin
853 + mode
->right_margin
;
854 tmp
= ((mode
->xres
& 7) << 24) | ((display_h_total
& 7) << 16)
855 | ((mode
->hsync_len
& 7) << 8) | (hsync_pos
& 7);
856 lcdc_write_chan(ch
, LDHAJR
, tmp
);
857 lcdc_write_chan_mirror(ch
, LDHAJR
, tmp
);
860 static void sh_mobile_lcdc_overlay_setup(struct sh_mobile_lcdc_overlay
*ovl
)
865 lcdc_write(ovl
->channel
->lcdc
, LDBCR
, LDBCR_UPC(ovl
->index
));
866 lcdc_write_overlay(ovl
, LDBnBSIFR(ovl
->index
), 0);
867 lcdc_write(ovl
->channel
->lcdc
, LDBCR
,
868 LDBCR_UPF(ovl
->index
) | LDBCR_UPD(ovl
->index
));
872 ovl
->base_addr_y
= ovl
->dma_handle
;
873 ovl
->base_addr_c
= ovl
->dma_handle
874 + ovl
->xres_virtual
* ovl
->yres_virtual
;
877 case LCDC_OVERLAY_BLEND
:
878 format
= LDBBSIFR_EN
| (ovl
->alpha
<< LDBBSIFR_LAY_SHIFT
);
881 case LCDC_OVERLAY_ROP3
:
882 format
= LDBBSIFR_EN
| LDBBSIFR_BRSEL
883 | (ovl
->rop3
<< LDBBSIFR_ROP3_SHIFT
);
887 switch (ovl
->format
->fourcc
) {
888 case V4L2_PIX_FMT_RGB565
:
889 case V4L2_PIX_FMT_NV21
:
890 case V4L2_PIX_FMT_NV61
:
891 case V4L2_PIX_FMT_NV42
:
892 format
|= LDBBSIFR_SWPL
| LDBBSIFR_SWPW
;
894 case V4L2_PIX_FMT_BGR24
:
895 case V4L2_PIX_FMT_NV12
:
896 case V4L2_PIX_FMT_NV16
:
897 case V4L2_PIX_FMT_NV24
:
898 format
|= LDBBSIFR_SWPL
| LDBBSIFR_SWPW
| LDBBSIFR_SWPB
;
900 case V4L2_PIX_FMT_BGR32
:
902 format
|= LDBBSIFR_SWPL
;
906 switch (ovl
->format
->fourcc
) {
907 case V4L2_PIX_FMT_RGB565
:
908 format
|= LDBBSIFR_AL_1
| LDBBSIFR_RY
| LDBBSIFR_RPKF_RGB16
;
910 case V4L2_PIX_FMT_BGR24
:
911 format
|= LDBBSIFR_AL_1
| LDBBSIFR_RY
| LDBBSIFR_RPKF_RGB24
;
913 case V4L2_PIX_FMT_BGR32
:
914 format
|= LDBBSIFR_AL_PK
| LDBBSIFR_RY
| LDDFR_PKF_ARGB32
;
916 case V4L2_PIX_FMT_NV12
:
917 case V4L2_PIX_FMT_NV21
:
918 format
|= LDBBSIFR_AL_1
| LDBBSIFR_CHRR_420
;
920 case V4L2_PIX_FMT_NV16
:
921 case V4L2_PIX_FMT_NV61
:
922 format
|= LDBBSIFR_AL_1
| LDBBSIFR_CHRR_422
;
924 case V4L2_PIX_FMT_NV24
:
925 case V4L2_PIX_FMT_NV42
:
926 format
|= LDBBSIFR_AL_1
| LDBBSIFR_CHRR_444
;
930 lcdc_write(ovl
->channel
->lcdc
, LDBCR
, LDBCR_UPC(ovl
->index
));
932 lcdc_write_overlay(ovl
, LDBnBSIFR(ovl
->index
), format
);
934 lcdc_write_overlay(ovl
, LDBnBSSZR(ovl
->index
),
935 (ovl
->yres
<< LDBBSSZR_BVSS_SHIFT
) |
936 (ovl
->xres
<< LDBBSSZR_BHSS_SHIFT
));
937 lcdc_write_overlay(ovl
, LDBnBLOCR(ovl
->index
),
938 (ovl
->pos_y
<< LDBBLOCR_CVLC_SHIFT
) |
939 (ovl
->pos_x
<< LDBBLOCR_CHLC_SHIFT
));
940 lcdc_write_overlay(ovl
, LDBnBSMWR(ovl
->index
),
941 ovl
->pitch
<< LDBBSMWR_BSMW_SHIFT
);
943 lcdc_write_overlay(ovl
, LDBnBSAYR(ovl
->index
), ovl
->base_addr_y
);
944 lcdc_write_overlay(ovl
, LDBnBSACR(ovl
->index
), ovl
->base_addr_c
);
946 lcdc_write(ovl
->channel
->lcdc
, LDBCR
,
947 LDBCR_UPF(ovl
->index
) | LDBCR_UPD(ovl
->index
));
951 * __sh_mobile_lcdc_start - Configure and start the LCDC
954 * Configure all enabled channels and start the LCDC device. All external
955 * devices (clocks, MERAM, panels, ...) are not touched by this function.
957 static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv
*priv
)
959 struct sh_mobile_lcdc_chan
*ch
;
963 /* Enable LCDC channels. Read data from external memory, avoid using the
966 lcdc_write(priv
, _LDCNT2R
, priv
->ch
[0].enabled
| priv
->ch
[1].enabled
);
968 /* Stop the LCDC first and disable all interrupts. */
969 sh_mobile_lcdc_start_stop(priv
, 0);
970 lcdc_write(priv
, _LDINTR
, 0);
972 /* Configure power supply, dot clocks and start them. */
974 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
980 lcdc_write_chan(ch
, LDPMR
, 0);
982 m
= ch
->cfg
->clock_divider
;
986 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
989 lcdc_write_chan(ch
, LDDCKPAT1R
, 0);
990 lcdc_write_chan(ch
, LDDCKPAT2R
, (1 << (m
/2)) - 1);
994 tmp
|= m
<< (lcdc_chan_is_sublcd(ch
) ? 8 : 0);
997 lcdc_write(priv
, _LDDCKR
, tmp
);
998 lcdc_write(priv
, _LDDCKSTPR
, 0);
999 lcdc_wait_bit(priv
, _LDDCKSTPR
, ~0, 0);
1001 /* Setup geometry, format, frame buffer memory and operation mode. */
1002 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
1007 sh_mobile_lcdc_geometry(ch
);
1009 tmp
= ch
->format
->lddfr
;
1011 if (ch
->format
->yuv
) {
1012 switch (ch
->colorspace
) {
1013 case V4L2_COLORSPACE_REC709
:
1016 case V4L2_COLORSPACE_JPEG
:
1022 lcdc_write_chan(ch
, LDDFR
, tmp
);
1023 lcdc_write_chan(ch
, LDMLSR
, ch
->line_size
);
1024 lcdc_write_chan(ch
, LDSA1R
, ch
->base_addr_y
);
1025 if (ch
->format
->yuv
)
1026 lcdc_write_chan(ch
, LDSA2R
, ch
->base_addr_c
);
1028 /* When using deferred I/O mode, configure the LCDC for one-shot
1029 * operation and enable the frame end interrupt. Otherwise use
1030 * continuous read mode.
1032 if (ch
->ldmt1r_value
& LDMT1R_IFM
&&
1033 ch
->cfg
->sys_bus_cfg
.deferred_io_msec
) {
1034 lcdc_write_chan(ch
, LDSM1R
, LDSM1R_OS
);
1035 lcdc_write(priv
, _LDINTR
, LDINTR_FE
);
1037 lcdc_write_chan(ch
, LDSM1R
, 0);
1041 /* Word and long word swap. */
1042 switch (priv
->ch
[0].format
->fourcc
) {
1043 case V4L2_PIX_FMT_RGB565
:
1044 case V4L2_PIX_FMT_NV21
:
1045 case V4L2_PIX_FMT_NV61
:
1046 case V4L2_PIX_FMT_NV42
:
1047 tmp
= LDDDSR_LS
| LDDDSR_WS
;
1049 case V4L2_PIX_FMT_BGR24
:
1050 case V4L2_PIX_FMT_NV12
:
1051 case V4L2_PIX_FMT_NV16
:
1052 case V4L2_PIX_FMT_NV24
:
1053 tmp
= LDDDSR_LS
| LDDDSR_WS
| LDDDSR_BS
;
1055 case V4L2_PIX_FMT_BGR32
:
1060 lcdc_write(priv
, _LDDDSR
, tmp
);
1062 /* Enable the display output. */
1063 lcdc_write(priv
, _LDCNT1R
, LDCNT1R_DE
);
1064 sh_mobile_lcdc_start_stop(priv
, 1);
1068 static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv
*priv
)
1070 struct sh_mobile_lcdc_chan
*ch
;
1075 /* enable clocks before accessing the hardware */
1076 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
1077 if (priv
->ch
[k
].enabled
)
1078 sh_mobile_lcdc_clk_on(priv
);
1082 lcdc_write(priv
, _LDCNT2R
, lcdc_read(priv
, _LDCNT2R
) | LDCNT2R_BR
);
1083 lcdc_wait_bit(priv
, _LDCNT2R
, LDCNT2R_BR
, 0);
1085 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
1086 const struct sh_mobile_lcdc_panel_cfg
*panel
;
1092 panel
= &ch
->cfg
->panel_cfg
;
1093 if (panel
->setup_sys
) {
1094 ret
= panel
->setup_sys(ch
, &sh_mobile_lcdc_sys_bus_ops
);
1100 /* Compute frame buffer base address and pitch for each channel. */
1101 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
1106 ch
->base_addr_y
= ch
->dma_handle
;
1107 ch
->base_addr_c
= ch
->dma_handle
1108 + ch
->xres_virtual
* ch
->yres_virtual
;
1109 ch
->line_size
= ch
->pitch
;
1112 for (k
= 0; k
< ARRAY_SIZE(priv
->overlays
); ++k
) {
1113 struct sh_mobile_lcdc_overlay
*ovl
= &priv
->overlays
[k
];
1114 sh_mobile_lcdc_overlay_setup(ovl
);
1117 /* Start the LCDC. */
1118 __sh_mobile_lcdc_start(priv
);
1120 /* Setup deferred I/O, tell the board code to enable the panels, and
1121 * turn backlight on.
1123 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
1128 tmp
= ch
->cfg
->sys_bus_cfg
.deferred_io_msec
;
1129 if (ch
->ldmt1r_value
& LDMT1R_IFM
&& tmp
) {
1130 ch
->defio
.deferred_io
= sh_mobile_lcdc_deferred_io
;
1131 ch
->defio
.delay
= msecs_to_jiffies(tmp
);
1132 ch
->info
->fbdefio
= &ch
->defio
;
1133 fb_deferred_io_init(ch
->info
);
1136 sh_mobile_lcdc_display_on(ch
);
1139 ch
->bl
->props
.power
= FB_BLANK_UNBLANK
;
1140 backlight_update_status(ch
->bl
);
1147 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv
*priv
)
1149 struct sh_mobile_lcdc_chan
*ch
;
1152 /* clean up deferred io and ask board code to disable panel */
1153 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
1158 /* deferred io mode:
1159 * flush frame, and wait for frame end interrupt
1160 * clean up deferred io and enable clock
1162 if (ch
->info
&& ch
->info
->fbdefio
) {
1164 schedule_delayed_work(&ch
->info
->deferred_work
, 0);
1165 wait_event(ch
->frame_end_wait
, ch
->frame_end
);
1166 fb_deferred_io_cleanup(ch
->info
);
1167 ch
->info
->fbdefio
= NULL
;
1168 sh_mobile_lcdc_clk_on(priv
);
1172 ch
->bl
->props
.power
= FB_BLANK_POWERDOWN
;
1173 backlight_update_status(ch
->bl
);
1176 sh_mobile_lcdc_display_off(ch
);
1180 if (priv
->started
) {
1181 sh_mobile_lcdc_start_stop(priv
, 0);
1186 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++)
1187 if (priv
->ch
[k
].enabled
)
1188 sh_mobile_lcdc_clk_off(priv
);
1191 static int __sh_mobile_lcdc_check_var(struct fb_var_screeninfo
*var
,
1192 struct fb_info
*info
)
1194 if (var
->xres
> MAX_XRES
|| var
->yres
> MAX_YRES
)
1197 /* Make sure the virtual resolution is at least as big as the visible
1200 if (var
->xres_virtual
< var
->xres
)
1201 var
->xres_virtual
= var
->xres
;
1202 if (var
->yres_virtual
< var
->yres
)
1203 var
->yres_virtual
= var
->yres
;
1205 if (sh_mobile_format_is_fourcc(var
)) {
1206 const struct sh_mobile_lcdc_format_info
*format
;
1208 format
= sh_mobile_format_info(var
->grayscale
);
1211 var
->bits_per_pixel
= format
->bpp
;
1213 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1217 var
->colorspace
= V4L2_COLORSPACE_SRGB
;
1218 else if (var
->colorspace
!= V4L2_COLORSPACE_REC709
)
1219 var
->colorspace
= V4L2_COLORSPACE_JPEG
;
1221 if (var
->bits_per_pixel
<= 16) { /* RGB 565 */
1222 var
->bits_per_pixel
= 16;
1223 var
->red
.offset
= 11;
1224 var
->red
.length
= 5;
1225 var
->green
.offset
= 5;
1226 var
->green
.length
= 6;
1227 var
->blue
.offset
= 0;
1228 var
->blue
.length
= 5;
1229 var
->transp
.offset
= 0;
1230 var
->transp
.length
= 0;
1231 } else if (var
->bits_per_pixel
<= 24) { /* RGB 888 */
1232 var
->bits_per_pixel
= 24;
1233 var
->red
.offset
= 16;
1234 var
->red
.length
= 8;
1235 var
->green
.offset
= 8;
1236 var
->green
.length
= 8;
1237 var
->blue
.offset
= 0;
1238 var
->blue
.length
= 8;
1239 var
->transp
.offset
= 0;
1240 var
->transp
.length
= 0;
1241 } else if (var
->bits_per_pixel
<= 32) { /* RGBA 888 */
1242 var
->bits_per_pixel
= 32;
1243 var
->red
.offset
= 16;
1244 var
->red
.length
= 8;
1245 var
->green
.offset
= 8;
1246 var
->green
.length
= 8;
1247 var
->blue
.offset
= 0;
1248 var
->blue
.length
= 8;
1249 var
->transp
.offset
= 24;
1250 var
->transp
.length
= 8;
1254 var
->red
.msb_right
= 0;
1255 var
->green
.msb_right
= 0;
1256 var
->blue
.msb_right
= 0;
1257 var
->transp
.msb_right
= 0;
1260 /* Make sure we don't exceed our allocated memory. */
1261 if (var
->xres_virtual
* var
->yres_virtual
* var
->bits_per_pixel
/ 8 >
1268 /* -----------------------------------------------------------------------------
1269 * Frame buffer operations - Overlays
1273 overlay_alpha_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1275 struct fb_info
*info
= dev_get_drvdata(dev
);
1276 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1278 return scnprintf(buf
, PAGE_SIZE
, "%u\n", ovl
->alpha
);
1282 overlay_alpha_store(struct device
*dev
, struct device_attribute
*attr
,
1283 const char *buf
, size_t count
)
1285 struct fb_info
*info
= dev_get_drvdata(dev
);
1286 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1290 alpha
= simple_strtoul(buf
, &endp
, 10);
1294 if (endp
- buf
!= count
)
1300 if (ovl
->alpha
!= alpha
) {
1303 if (ovl
->mode
== LCDC_OVERLAY_BLEND
&& ovl
->enabled
)
1304 sh_mobile_lcdc_overlay_setup(ovl
);
1311 overlay_mode_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
->mode
);
1320 overlay_mode_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 mode
= simple_strtoul(buf
, &endp
, 10);
1332 if (endp
- buf
!= count
)
1335 if (mode
!= LCDC_OVERLAY_BLEND
&& mode
!= LCDC_OVERLAY_ROP3
)
1338 if (ovl
->mode
!= mode
) {
1342 sh_mobile_lcdc_overlay_setup(ovl
);
1349 overlay_position_show(struct device
*dev
, struct device_attribute
*attr
,
1352 struct fb_info
*info
= dev_get_drvdata(dev
);
1353 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1355 return scnprintf(buf
, PAGE_SIZE
, "%d,%d\n", ovl
->pos_x
, ovl
->pos_y
);
1359 overlay_position_store(struct device
*dev
, struct device_attribute
*attr
,
1360 const char *buf
, size_t count
)
1362 struct fb_info
*info
= dev_get_drvdata(dev
);
1363 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1368 pos_x
= simple_strtol(buf
, &endp
, 10);
1372 pos_y
= simple_strtol(endp
+ 1, &endp
, 10);
1376 if (endp
- buf
!= count
)
1379 if (ovl
->pos_x
!= pos_x
|| ovl
->pos_y
!= pos_y
) {
1384 sh_mobile_lcdc_overlay_setup(ovl
);
1391 overlay_rop3_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1393 struct fb_info
*info
= dev_get_drvdata(dev
);
1394 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1396 return scnprintf(buf
, PAGE_SIZE
, "%u\n", ovl
->rop3
);
1400 overlay_rop3_store(struct device
*dev
, struct device_attribute
*attr
,
1401 const char *buf
, size_t count
)
1403 struct fb_info
*info
= dev_get_drvdata(dev
);
1404 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1408 rop3
= simple_strtoul(buf
, &endp
, 10);
1412 if (endp
- buf
!= count
)
1418 if (ovl
->rop3
!= rop3
) {
1421 if (ovl
->mode
== LCDC_OVERLAY_ROP3
&& ovl
->enabled
)
1422 sh_mobile_lcdc_overlay_setup(ovl
);
1428 static const struct device_attribute overlay_sysfs_attrs
[] = {
1429 __ATTR(ovl_alpha
, S_IRUGO
|S_IWUSR
,
1430 overlay_alpha_show
, overlay_alpha_store
),
1431 __ATTR(ovl_mode
, S_IRUGO
|S_IWUSR
,
1432 overlay_mode_show
, overlay_mode_store
),
1433 __ATTR(ovl_position
, S_IRUGO
|S_IWUSR
,
1434 overlay_position_show
, overlay_position_store
),
1435 __ATTR(ovl_rop3
, S_IRUGO
|S_IWUSR
,
1436 overlay_rop3_show
, overlay_rop3_store
),
1439 static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix
= {
1440 .id
= "SH Mobile LCDC",
1441 .type
= FB_TYPE_PACKED_PIXELS
,
1442 .visual
= FB_VISUAL_TRUECOLOR
,
1443 .accel
= FB_ACCEL_NONE
,
1447 .capabilities
= FB_CAP_FOURCC
,
1450 static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo
*var
,
1451 struct fb_info
*info
)
1453 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1454 unsigned long base_addr_y
;
1455 unsigned long base_addr_c
;
1456 unsigned long y_offset
;
1457 unsigned long c_offset
;
1459 if (!ovl
->format
->yuv
) {
1460 y_offset
= (var
->yoffset
* ovl
->xres_virtual
+ var
->xoffset
)
1461 * ovl
->format
->bpp
/ 8;
1464 unsigned int xsub
= ovl
->format
->bpp
< 24 ? 2 : 1;
1465 unsigned int ysub
= ovl
->format
->bpp
< 16 ? 2 : 1;
1467 y_offset
= var
->yoffset
* ovl
->xres_virtual
+ var
->xoffset
;
1468 c_offset
= var
->yoffset
/ ysub
* ovl
->xres_virtual
* 2 / xsub
1469 + var
->xoffset
* 2 / xsub
;
1472 /* If the Y offset hasn't changed, the C offset hasn't either. There's
1473 * nothing to do in that case.
1475 if (y_offset
== ovl
->pan_y_offset
)
1478 /* Set the source address for the next refresh */
1479 base_addr_y
= ovl
->dma_handle
+ y_offset
;
1480 base_addr_c
= ovl
->dma_handle
+ ovl
->xres_virtual
* ovl
->yres_virtual
1483 ovl
->base_addr_y
= base_addr_y
;
1484 ovl
->base_addr_c
= base_addr_c
;
1485 ovl
->pan_y_offset
= y_offset
;
1487 lcdc_write(ovl
->channel
->lcdc
, LDBCR
, LDBCR_UPC(ovl
->index
));
1489 lcdc_write_overlay(ovl
, LDBnBSAYR(ovl
->index
), ovl
->base_addr_y
);
1490 lcdc_write_overlay(ovl
, LDBnBSACR(ovl
->index
), ovl
->base_addr_c
);
1492 lcdc_write(ovl
->channel
->lcdc
, LDBCR
,
1493 LDBCR_UPF(ovl
->index
) | LDBCR_UPD(ovl
->index
));
1498 static int sh_mobile_lcdc_overlay_ioctl(struct fb_info
*info
, unsigned int cmd
,
1501 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1504 case FBIO_WAITFORVSYNC
:
1505 return sh_mobile_lcdc_wait_for_vsync(ovl
->channel
);
1508 return -ENOIOCTLCMD
;
1512 static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo
*var
,
1513 struct fb_info
*info
)
1515 return __sh_mobile_lcdc_check_var(var
, info
);
1518 static int sh_mobile_lcdc_overlay_set_par(struct fb_info
*info
)
1520 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1523 sh_mobile_format_info(sh_mobile_format_fourcc(&info
->var
));
1525 ovl
->xres
= info
->var
.xres
;
1526 ovl
->xres_virtual
= info
->var
.xres_virtual
;
1527 ovl
->yres
= info
->var
.yres
;
1528 ovl
->yres_virtual
= info
->var
.yres_virtual
;
1530 if (ovl
->format
->yuv
)
1531 ovl
->pitch
= info
->var
.xres_virtual
;
1533 ovl
->pitch
= info
->var
.xres_virtual
* ovl
->format
->bpp
/ 8;
1535 sh_mobile_lcdc_overlay_setup(ovl
);
1537 info
->fix
.line_length
= ovl
->pitch
;
1539 if (sh_mobile_format_is_fourcc(&info
->var
)) {
1540 info
->fix
.type
= FB_TYPE_FOURCC
;
1541 info
->fix
.visual
= FB_VISUAL_FOURCC
;
1543 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1544 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
1550 /* Overlay blanking. Disable the overlay when blanked. */
1551 static int sh_mobile_lcdc_overlay_blank(int blank
, struct fb_info
*info
)
1553 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1555 ovl
->enabled
= !blank
;
1556 sh_mobile_lcdc_overlay_setup(ovl
);
1558 /* Prevent the backlight from receiving a blanking event by returning
1565 sh_mobile_lcdc_overlay_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
1567 struct sh_mobile_lcdc_overlay
*ovl
= info
->par
;
1569 return dma_mmap_coherent(ovl
->channel
->lcdc
->dev
, vma
, ovl
->fb_mem
,
1570 ovl
->dma_handle
, ovl
->fb_size
);
1573 static struct fb_ops sh_mobile_lcdc_overlay_ops
= {
1574 .owner
= THIS_MODULE
,
1575 .fb_read
= fb_sys_read
,
1576 .fb_write
= fb_sys_write
,
1577 .fb_fillrect
= sys_fillrect
,
1578 .fb_copyarea
= sys_copyarea
,
1579 .fb_imageblit
= sys_imageblit
,
1580 .fb_blank
= sh_mobile_lcdc_overlay_blank
,
1581 .fb_pan_display
= sh_mobile_lcdc_overlay_pan
,
1582 .fb_ioctl
= sh_mobile_lcdc_overlay_ioctl
,
1583 .fb_check_var
= sh_mobile_lcdc_overlay_check_var
,
1584 .fb_set_par
= sh_mobile_lcdc_overlay_set_par
,
1585 .fb_mmap
= sh_mobile_lcdc_overlay_mmap
,
1589 sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay
*ovl
)
1591 struct fb_info
*info
= ovl
->info
;
1593 if (info
== NULL
|| info
->dev
== NULL
)
1596 unregister_framebuffer(ovl
->info
);
1600 sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay
*ovl
)
1602 struct sh_mobile_lcdc_priv
*lcdc
= ovl
->channel
->lcdc
;
1603 struct fb_info
*info
= ovl
->info
;
1610 ret
= register_framebuffer(info
);
1614 dev_info(lcdc
->dev
, "registered %s/overlay %u as %dx%d %dbpp.\n",
1615 dev_name(lcdc
->dev
), ovl
->index
, info
->var
.xres
,
1616 info
->var
.yres
, info
->var
.bits_per_pixel
);
1618 for (i
= 0; i
< ARRAY_SIZE(overlay_sysfs_attrs
); ++i
) {
1619 ret
= device_create_file(info
->dev
, &overlay_sysfs_attrs
[i
]);
1628 sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay
*ovl
)
1630 struct fb_info
*info
= ovl
->info
;
1632 if (info
== NULL
|| info
->device
== NULL
)
1635 framebuffer_release(info
);
1639 sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay
*ovl
)
1641 struct sh_mobile_lcdc_priv
*priv
= ovl
->channel
->lcdc
;
1642 struct fb_var_screeninfo
*var
;
1643 struct fb_info
*info
;
1645 /* Allocate and initialize the frame buffer device. */
1646 info
= framebuffer_alloc(0, priv
->dev
);
1648 dev_err(priv
->dev
, "unable to allocate fb_info\n");
1654 info
->flags
= FBINFO_FLAG_DEFAULT
;
1655 info
->fbops
= &sh_mobile_lcdc_overlay_ops
;
1656 info
->device
= priv
->dev
;
1657 info
->screen_base
= ovl
->fb_mem
;
1660 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1661 * for NV12 and NV21.
1663 info
->fix
= sh_mobile_lcdc_overlay_fix
;
1664 snprintf(info
->fix
.id
, sizeof(info
->fix
.id
),
1665 "SH Mobile LCDC Overlay %u", ovl
->index
);
1666 info
->fix
.smem_start
= ovl
->dma_handle
;
1667 info
->fix
.smem_len
= ovl
->fb_size
;
1668 info
->fix
.line_length
= ovl
->pitch
;
1670 if (ovl
->format
->yuv
)
1671 info
->fix
.visual
= FB_VISUAL_FOURCC
;
1673 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
1675 switch (ovl
->format
->fourcc
) {
1676 case V4L2_PIX_FMT_NV12
:
1677 case V4L2_PIX_FMT_NV21
:
1678 info
->fix
.ypanstep
= 2;
1679 case V4L2_PIX_FMT_NV16
:
1680 case V4L2_PIX_FMT_NV61
:
1681 info
->fix
.xpanstep
= 2;
1684 /* Initialize variable screen information. */
1686 memset(var
, 0, sizeof(*var
));
1687 var
->xres
= ovl
->xres
;
1688 var
->yres
= ovl
->yres
;
1689 var
->xres_virtual
= ovl
->xres_virtual
;
1690 var
->yres_virtual
= ovl
->yres_virtual
;
1691 var
->activate
= FB_ACTIVATE_NOW
;
1693 /* Use the legacy API by default for RGB formats, and the FOURCC API
1696 if (!ovl
->format
->yuv
)
1697 var
->bits_per_pixel
= ovl
->format
->bpp
;
1699 var
->grayscale
= ovl
->format
->fourcc
;
1701 return sh_mobile_lcdc_overlay_check_var(var
, info
);
1704 /* -----------------------------------------------------------------------------
1705 * Frame buffer operations - main frame buffer
1708 static int sh_mobile_lcdc_setcolreg(u_int regno
,
1709 u_int red
, u_int green
, u_int blue
,
1710 u_int transp
, struct fb_info
*info
)
1712 u32
*palette
= info
->pseudo_palette
;
1714 if (regno
>= PALETTE_NR
)
1717 /* only FB_VISUAL_TRUECOLOR supported */
1719 red
>>= 16 - info
->var
.red
.length
;
1720 green
>>= 16 - info
->var
.green
.length
;
1721 blue
>>= 16 - info
->var
.blue
.length
;
1722 transp
>>= 16 - info
->var
.transp
.length
;
1724 palette
[regno
] = (red
<< info
->var
.red
.offset
) |
1725 (green
<< info
->var
.green
.offset
) |
1726 (blue
<< info
->var
.blue
.offset
) |
1727 (transp
<< info
->var
.transp
.offset
);
1732 static const struct fb_fix_screeninfo sh_mobile_lcdc_fix
= {
1733 .id
= "SH Mobile LCDC",
1734 .type
= FB_TYPE_PACKED_PIXELS
,
1735 .visual
= FB_VISUAL_TRUECOLOR
,
1736 .accel
= FB_ACCEL_NONE
,
1740 .capabilities
= FB_CAP_FOURCC
,
1743 static void sh_mobile_lcdc_fillrect(struct fb_info
*info
,
1744 const struct fb_fillrect
*rect
)
1746 sys_fillrect(info
, rect
);
1747 sh_mobile_lcdc_deferred_io_touch(info
);
1750 static void sh_mobile_lcdc_copyarea(struct fb_info
*info
,
1751 const struct fb_copyarea
*area
)
1753 sys_copyarea(info
, area
);
1754 sh_mobile_lcdc_deferred_io_touch(info
);
1757 static void sh_mobile_lcdc_imageblit(struct fb_info
*info
,
1758 const struct fb_image
*image
)
1760 sys_imageblit(info
, image
);
1761 sh_mobile_lcdc_deferred_io_touch(info
);
1764 static int sh_mobile_lcdc_pan(struct fb_var_screeninfo
*var
,
1765 struct fb_info
*info
)
1767 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1768 struct sh_mobile_lcdc_priv
*priv
= ch
->lcdc
;
1769 unsigned long ldrcntr
;
1770 unsigned long base_addr_y
, base_addr_c
;
1771 unsigned long y_offset
;
1772 unsigned long c_offset
;
1774 if (!ch
->format
->yuv
) {
1775 y_offset
= (var
->yoffset
* ch
->xres_virtual
+ var
->xoffset
)
1776 * ch
->format
->bpp
/ 8;
1779 unsigned int xsub
= ch
->format
->bpp
< 24 ? 2 : 1;
1780 unsigned int ysub
= ch
->format
->bpp
< 16 ? 2 : 1;
1782 y_offset
= var
->yoffset
* ch
->xres_virtual
+ var
->xoffset
;
1783 c_offset
= var
->yoffset
/ ysub
* ch
->xres_virtual
* 2 / xsub
1784 + var
->xoffset
* 2 / xsub
;
1787 /* If the Y offset hasn't changed, the C offset hasn't either. There's
1788 * nothing to do in that case.
1790 if (y_offset
== ch
->pan_y_offset
)
1793 /* Set the source address for the next refresh */
1794 base_addr_y
= ch
->dma_handle
+ y_offset
;
1795 base_addr_c
= ch
->dma_handle
+ ch
->xres_virtual
* ch
->yres_virtual
1798 ch
->base_addr_y
= base_addr_y
;
1799 ch
->base_addr_c
= base_addr_c
;
1800 ch
->pan_y_offset
= y_offset
;
1802 lcdc_write_chan_mirror(ch
, LDSA1R
, base_addr_y
);
1803 if (ch
->format
->yuv
)
1804 lcdc_write_chan_mirror(ch
, LDSA2R
, base_addr_c
);
1806 ldrcntr
= lcdc_read(priv
, _LDRCNTR
);
1807 if (lcdc_chan_is_sublcd(ch
))
1808 lcdc_write(ch
->lcdc
, _LDRCNTR
, ldrcntr
^ LDRCNTR_SRS
);
1810 lcdc_write(ch
->lcdc
, _LDRCNTR
, ldrcntr
^ LDRCNTR_MRS
);
1813 sh_mobile_lcdc_deferred_io_touch(info
);
1818 static int sh_mobile_lcdc_ioctl(struct fb_info
*info
, unsigned int cmd
,
1821 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1825 case FBIO_WAITFORVSYNC
:
1826 retval
= sh_mobile_lcdc_wait_for_vsync(ch
);
1830 retval
= -ENOIOCTLCMD
;
1836 static void sh_mobile_fb_reconfig(struct fb_info
*info
)
1838 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1839 struct fb_var_screeninfo var
;
1840 struct fb_videomode mode
;
1841 struct fb_event event
;
1842 int evnt
= FB_EVENT_MODE_CHANGE_ALL
;
1844 if (ch
->use_count
> 1 || (ch
->use_count
== 1 && !info
->fbcon_par
))
1845 /* More framebuffer users are active */
1848 fb_var_to_videomode(&mode
, &info
->var
);
1850 if (fb_mode_is_equal(&ch
->display
.mode
, &mode
))
1853 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1855 fb_videomode_to_var(&var
, &ch
->display
.mode
);
1856 var
.width
= ch
->display
.width
;
1857 var
.height
= ch
->display
.height
;
1858 var
.activate
= FB_ACTIVATE_NOW
;
1860 if (fb_set_var(info
, &var
) < 0)
1861 /* Couldn't reconfigure, hopefully, can continue as before */
1865 * fb_set_var() calls the notifier change internally, only if
1866 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1867 * user event, we have to call the chain ourselves.
1870 event
.data
= &ch
->display
.mode
;
1871 fb_notifier_call_chain(evnt
, &event
);
1875 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1876 * user == 1, or with console sem held, if user == 0.
1878 static int sh_mobile_lcdc_release(struct fb_info
*info
, int user
)
1880 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1882 mutex_lock(&ch
->open_lock
);
1883 dev_dbg(info
->dev
, "%s(): %d users\n", __func__
, ch
->use_count
);
1887 /* Nothing to reconfigure, when called from fbcon */
1890 sh_mobile_fb_reconfig(info
);
1894 mutex_unlock(&ch
->open_lock
);
1899 static int sh_mobile_lcdc_open(struct fb_info
*info
, int user
)
1901 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1903 mutex_lock(&ch
->open_lock
);
1906 dev_dbg(info
->dev
, "%s(): %d users\n", __func__
, ch
->use_count
);
1907 mutex_unlock(&ch
->open_lock
);
1912 static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo
*var
,
1913 struct fb_info
*info
)
1915 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1916 struct sh_mobile_lcdc_priv
*p
= ch
->lcdc
;
1917 unsigned int best_dist
= (unsigned int)-1;
1918 unsigned int best_xres
= 0;
1919 unsigned int best_yres
= 0;
1923 /* If board code provides us with a list of available modes, make sure
1924 * we use one of them. Find the mode closest to the requested one. The
1925 * distance between two modes is defined as the size of the
1926 * non-overlapping parts of the two rectangles.
1928 for (i
= 0; i
< ch
->cfg
->num_modes
; ++i
) {
1929 const struct fb_videomode
*mode
= &ch
->cfg
->lcd_modes
[i
];
1932 /* We can only round up. */
1933 if (var
->xres
> mode
->xres
|| var
->yres
> mode
->yres
)
1936 dist
= var
->xres
* var
->yres
+ mode
->xres
* mode
->yres
1937 - 2 * min(var
->xres
, mode
->xres
)
1938 * min(var
->yres
, mode
->yres
);
1940 if (dist
< best_dist
) {
1941 best_xres
= mode
->xres
;
1942 best_yres
= mode
->yres
;
1947 /* If no available mode can be used, return an error. */
1948 if (ch
->cfg
->num_modes
!= 0) {
1949 if (best_dist
== (unsigned int)-1)
1952 var
->xres
= best_xres
;
1953 var
->yres
= best_yres
;
1956 ret
= __sh_mobile_lcdc_check_var(var
, info
);
1960 /* only accept the forced_fourcc for dual channel configurations */
1961 if (p
->forced_fourcc
&&
1962 p
->forced_fourcc
!= sh_mobile_format_fourcc(var
))
1968 static int sh_mobile_lcdc_set_par(struct fb_info
*info
)
1970 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1973 sh_mobile_lcdc_stop(ch
->lcdc
);
1975 ch
->format
= sh_mobile_format_info(sh_mobile_format_fourcc(&info
->var
));
1976 ch
->colorspace
= info
->var
.colorspace
;
1978 ch
->xres
= info
->var
.xres
;
1979 ch
->xres_virtual
= info
->var
.xres_virtual
;
1980 ch
->yres
= info
->var
.yres
;
1981 ch
->yres_virtual
= info
->var
.yres_virtual
;
1983 if (ch
->format
->yuv
)
1984 ch
->pitch
= info
->var
.xres_virtual
;
1986 ch
->pitch
= info
->var
.xres_virtual
* ch
->format
->bpp
/ 8;
1988 ret
= sh_mobile_lcdc_start(ch
->lcdc
);
1990 dev_err(info
->dev
, "%s: unable to restart LCDC\n", __func__
);
1992 info
->fix
.line_length
= ch
->pitch
;
1994 if (sh_mobile_format_is_fourcc(&info
->var
)) {
1995 info
->fix
.type
= FB_TYPE_FOURCC
;
1996 info
->fix
.visual
= FB_VISUAL_FOURCC
;
1998 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1999 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
2006 * Screen blanking. Behavior is as follows:
2007 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
2008 * FB_BLANK_NORMAL: screen blanked, clocks enabled
2011 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
2013 static int sh_mobile_lcdc_blank(int blank
, struct fb_info
*info
)
2015 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
2016 struct sh_mobile_lcdc_priv
*p
= ch
->lcdc
;
2018 /* blank the screen? */
2019 if (blank
> FB_BLANK_UNBLANK
&& ch
->blank_status
== FB_BLANK_UNBLANK
) {
2020 struct fb_fillrect rect
= {
2024 sh_mobile_lcdc_fillrect(info
, &rect
);
2026 /* turn clocks on? */
2027 if (blank
<= FB_BLANK_NORMAL
&& ch
->blank_status
> FB_BLANK_NORMAL
) {
2028 sh_mobile_lcdc_clk_on(p
);
2030 /* turn clocks off? */
2031 if (blank
> FB_BLANK_NORMAL
&& ch
->blank_status
<= FB_BLANK_NORMAL
) {
2032 /* make sure the screen is updated with the black fill before
2033 * switching the clocks off. one vsync is not enough since
2034 * blanking may occur in the middle of a refresh. deferred io
2035 * mode will reenable the clocks and update the screen in time,
2036 * so it does not need this. */
2037 if (!info
->fbdefio
) {
2038 sh_mobile_lcdc_wait_for_vsync(ch
);
2039 sh_mobile_lcdc_wait_for_vsync(ch
);
2041 sh_mobile_lcdc_clk_off(p
);
2044 ch
->blank_status
= blank
;
2049 sh_mobile_lcdc_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
2051 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
2053 return dma_mmap_coherent(ch
->lcdc
->dev
, vma
, ch
->fb_mem
,
2054 ch
->dma_handle
, ch
->fb_size
);
2057 static struct fb_ops sh_mobile_lcdc_ops
= {
2058 .owner
= THIS_MODULE
,
2059 .fb_setcolreg
= sh_mobile_lcdc_setcolreg
,
2060 .fb_read
= fb_sys_read
,
2061 .fb_write
= fb_sys_write
,
2062 .fb_fillrect
= sh_mobile_lcdc_fillrect
,
2063 .fb_copyarea
= sh_mobile_lcdc_copyarea
,
2064 .fb_imageblit
= sh_mobile_lcdc_imageblit
,
2065 .fb_blank
= sh_mobile_lcdc_blank
,
2066 .fb_pan_display
= sh_mobile_lcdc_pan
,
2067 .fb_ioctl
= sh_mobile_lcdc_ioctl
,
2068 .fb_open
= sh_mobile_lcdc_open
,
2069 .fb_release
= sh_mobile_lcdc_release
,
2070 .fb_check_var
= sh_mobile_lcdc_check_var
,
2071 .fb_set_par
= sh_mobile_lcdc_set_par
,
2072 .fb_mmap
= sh_mobile_lcdc_mmap
,
2076 sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan
*ch
)
2078 if (ch
->info
&& ch
->info
->dev
)
2079 unregister_framebuffer(ch
->info
);
2083 sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan
*ch
)
2085 struct fb_info
*info
= ch
->info
;
2088 if (info
->fbdefio
) {
2089 ch
->sglist
= vmalloc(sizeof(struct scatterlist
) *
2090 ch
->fb_size
>> PAGE_SHIFT
);
2095 info
->bl_dev
= ch
->bl
;
2097 ret
= register_framebuffer(info
);
2101 dev_info(ch
->lcdc
->dev
, "registered %s/%s as %dx%d %dbpp.\n",
2102 dev_name(ch
->lcdc
->dev
), (ch
->cfg
->chan
== LCDC_CHAN_MAINLCD
) ?
2103 "mainlcd" : "sublcd", info
->var
.xres
, info
->var
.yres
,
2104 info
->var
.bits_per_pixel
);
2106 /* deferred io mode: disable clock to save power */
2107 if (info
->fbdefio
|| info
->state
== FBINFO_STATE_SUSPENDED
)
2108 sh_mobile_lcdc_clk_off(ch
->lcdc
);
2114 sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan
*ch
)
2116 struct fb_info
*info
= ch
->info
;
2118 if (!info
|| !info
->device
)
2123 fb_dealloc_cmap(&info
->cmap
);
2124 framebuffer_release(info
);
2128 sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan
*ch
,
2129 const struct fb_videomode
*modes
,
2130 unsigned int num_modes
)
2132 struct sh_mobile_lcdc_priv
*priv
= ch
->lcdc
;
2133 struct fb_var_screeninfo
*var
;
2134 struct fb_info
*info
;
2137 /* Allocate and initialize the frame buffer device. Create the modes
2138 * list and allocate the color map.
2140 info
= framebuffer_alloc(0, priv
->dev
);
2142 dev_err(priv
->dev
, "unable to allocate fb_info\n");
2148 info
->flags
= FBINFO_FLAG_DEFAULT
;
2149 info
->fbops
= &sh_mobile_lcdc_ops
;
2150 info
->device
= priv
->dev
;
2151 info
->screen_base
= ch
->fb_mem
;
2152 info
->pseudo_palette
= &ch
->pseudo_palette
;
2155 fb_videomode_to_modelist(modes
, num_modes
, &info
->modelist
);
2157 ret
= fb_alloc_cmap(&info
->cmap
, PALETTE_NR
, 0);
2159 dev_err(priv
->dev
, "unable to allocate cmap\n");
2163 /* Initialize fixed screen information. Restrict pan to 2 lines steps
2164 * for NV12 and NV21.
2166 info
->fix
= sh_mobile_lcdc_fix
;
2167 info
->fix
.smem_start
= ch
->dma_handle
;
2168 info
->fix
.smem_len
= ch
->fb_size
;
2169 info
->fix
.line_length
= ch
->pitch
;
2171 if (ch
->format
->yuv
)
2172 info
->fix
.visual
= FB_VISUAL_FOURCC
;
2174 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
2176 switch (ch
->format
->fourcc
) {
2177 case V4L2_PIX_FMT_NV12
:
2178 case V4L2_PIX_FMT_NV21
:
2179 info
->fix
.ypanstep
= 2;
2180 case V4L2_PIX_FMT_NV16
:
2181 case V4L2_PIX_FMT_NV61
:
2182 info
->fix
.xpanstep
= 2;
2185 /* Initialize variable screen information using the first mode as
2189 fb_videomode_to_var(var
, modes
);
2190 var
->width
= ch
->display
.width
;
2191 var
->height
= ch
->display
.height
;
2192 var
->xres_virtual
= ch
->xres_virtual
;
2193 var
->yres_virtual
= ch
->yres_virtual
;
2194 var
->activate
= FB_ACTIVATE_NOW
;
2196 /* Use the legacy API by default for RGB formats, and the FOURCC API
2199 if (!ch
->format
->yuv
)
2200 var
->bits_per_pixel
= ch
->format
->bpp
;
2202 var
->grayscale
= ch
->format
->fourcc
;
2204 ret
= sh_mobile_lcdc_check_var(var
, info
);
2211 /* -----------------------------------------------------------------------------
2215 static int sh_mobile_lcdc_update_bl(struct backlight_device
*bdev
)
2217 struct sh_mobile_lcdc_chan
*ch
= bl_get_data(bdev
);
2218 int brightness
= bdev
->props
.brightness
;
2220 if (bdev
->props
.power
!= FB_BLANK_UNBLANK
||
2221 bdev
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
2224 ch
->bl_brightness
= brightness
;
2225 return ch
->cfg
->bl_info
.set_brightness(brightness
);
2228 static int sh_mobile_lcdc_get_brightness(struct backlight_device
*bdev
)
2230 struct sh_mobile_lcdc_chan
*ch
= bl_get_data(bdev
);
2232 return ch
->bl_brightness
;
2235 static int sh_mobile_lcdc_check_fb(struct backlight_device
*bdev
,
2236 struct fb_info
*info
)
2238 return (info
->bl_dev
== bdev
);
2241 static const struct backlight_ops sh_mobile_lcdc_bl_ops
= {
2242 .options
= BL_CORE_SUSPENDRESUME
,
2243 .update_status
= sh_mobile_lcdc_update_bl
,
2244 .get_brightness
= sh_mobile_lcdc_get_brightness
,
2245 .check_fb
= sh_mobile_lcdc_check_fb
,
2248 static struct backlight_device
*sh_mobile_lcdc_bl_probe(struct device
*parent
,
2249 struct sh_mobile_lcdc_chan
*ch
)
2251 struct backlight_device
*bl
;
2253 bl
= backlight_device_register(ch
->cfg
->bl_info
.name
, parent
, ch
,
2254 &sh_mobile_lcdc_bl_ops
, NULL
);
2256 dev_err(parent
, "unable to register backlight device: %ld\n",
2261 bl
->props
.max_brightness
= ch
->cfg
->bl_info
.max_brightness
;
2262 bl
->props
.brightness
= bl
->props
.max_brightness
;
2263 backlight_update_status(bl
);
2268 static void sh_mobile_lcdc_bl_remove(struct backlight_device
*bdev
)
2270 backlight_device_unregister(bdev
);
2273 /* -----------------------------------------------------------------------------
2277 static int sh_mobile_lcdc_suspend(struct device
*dev
)
2279 struct platform_device
*pdev
= to_platform_device(dev
);
2281 sh_mobile_lcdc_stop(platform_get_drvdata(pdev
));
2285 static int sh_mobile_lcdc_resume(struct device
*dev
)
2287 struct platform_device
*pdev
= to_platform_device(dev
);
2289 return sh_mobile_lcdc_start(platform_get_drvdata(pdev
));
2292 static int sh_mobile_lcdc_runtime_suspend(struct device
*dev
)
2294 struct sh_mobile_lcdc_priv
*priv
= dev_get_drvdata(dev
);
2296 /* turn off LCDC hardware */
2297 lcdc_write(priv
, _LDCNT1R
, 0);
2302 static int sh_mobile_lcdc_runtime_resume(struct device
*dev
)
2304 struct sh_mobile_lcdc_priv
*priv
= dev_get_drvdata(dev
);
2306 __sh_mobile_lcdc_start(priv
);
2311 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops
= {
2312 .suspend
= sh_mobile_lcdc_suspend
,
2313 .resume
= sh_mobile_lcdc_resume
,
2314 .runtime_suspend
= sh_mobile_lcdc_runtime_suspend
,
2315 .runtime_resume
= sh_mobile_lcdc_runtime_resume
,
2318 /* -----------------------------------------------------------------------------
2319 * Framebuffer notifier
2322 /* locking: called with info->lock held */
2323 static int sh_mobile_lcdc_notify(struct notifier_block
*nb
,
2324 unsigned long action
, void *data
)
2326 struct fb_event
*event
= data
;
2327 struct fb_info
*info
= event
->info
;
2328 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
2330 if (&ch
->lcdc
->notifier
!= nb
)
2333 dev_dbg(info
->dev
, "%s(): action = %lu, data = %p\n",
2334 __func__
, action
, event
->data
);
2337 case FB_EVENT_SUSPEND
:
2338 sh_mobile_lcdc_display_off(ch
);
2339 sh_mobile_lcdc_stop(ch
->lcdc
);
2341 case FB_EVENT_RESUME
:
2342 mutex_lock(&ch
->open_lock
);
2343 sh_mobile_fb_reconfig(info
);
2344 mutex_unlock(&ch
->open_lock
);
2346 sh_mobile_lcdc_display_on(ch
);
2347 sh_mobile_lcdc_start(ch
->lcdc
);
2353 /* -----------------------------------------------------------------------------
2354 * Probe/remove and driver init/exit
2357 static const struct fb_videomode default_720p
= {
2358 .name
= "HDMI 720p",
2363 .right_margin
= 110,
2372 .sync
= FB_SYNC_VERT_HIGH_ACT
| FB_SYNC_HOR_HIGH_ACT
,
2375 static int sh_mobile_lcdc_remove(struct platform_device
*pdev
)
2377 struct sh_mobile_lcdc_priv
*priv
= platform_get_drvdata(pdev
);
2380 fb_unregister_client(&priv
->notifier
);
2382 for (i
= 0; i
< ARRAY_SIZE(priv
->overlays
); i
++)
2383 sh_mobile_lcdc_overlay_fb_unregister(&priv
->overlays
[i
]);
2384 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++)
2385 sh_mobile_lcdc_channel_fb_unregister(&priv
->ch
[i
]);
2387 sh_mobile_lcdc_stop(priv
);
2389 for (i
= 0; i
< ARRAY_SIZE(priv
->overlays
); i
++) {
2390 struct sh_mobile_lcdc_overlay
*ovl
= &priv
->overlays
[i
];
2392 sh_mobile_lcdc_overlay_fb_cleanup(ovl
);
2395 dma_free_coherent(&pdev
->dev
, ovl
->fb_size
,
2396 ovl
->fb_mem
, ovl
->dma_handle
);
2399 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++) {
2400 struct sh_mobile_lcdc_chan
*ch
= &priv
->ch
[i
];
2403 ch
->tx_dev
->lcdc
= NULL
;
2404 module_put(ch
->cfg
->tx_dev
->dev
.driver
->owner
);
2407 sh_mobile_lcdc_channel_fb_cleanup(ch
);
2410 dma_free_coherent(&pdev
->dev
, ch
->fb_size
,
2411 ch
->fb_mem
, ch
->dma_handle
);
2414 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++) {
2415 struct sh_mobile_lcdc_chan
*ch
= &priv
->ch
[i
];
2418 sh_mobile_lcdc_bl_remove(ch
->bl
);
2419 mutex_destroy(&ch
->open_lock
);
2422 if (priv
->dot_clk
) {
2423 pm_runtime_disable(&pdev
->dev
);
2424 clk_put(priv
->dot_clk
);
2428 iounmap(priv
->base
);
2431 free_irq(priv
->irq
, priv
);
2436 static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan
*ch
)
2438 int interface_type
= ch
->cfg
->interface_type
;
2440 switch (interface_type
) {
2464 /* SUBLCD only supports SYS interface */
2465 if (lcdc_chan_is_sublcd(ch
)) {
2466 if (!(interface_type
& LDMT1R_IFM
))
2469 interface_type
&= ~LDMT1R_IFM
;
2472 ch
->ldmt1r_value
= interface_type
;
2477 sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_overlay
*ovl
)
2479 const struct sh_mobile_lcdc_format_info
*format
;
2480 struct device
*dev
= ovl
->channel
->lcdc
->dev
;
2483 if (ovl
->cfg
->fourcc
== 0)
2486 /* Validate the format. */
2487 format
= sh_mobile_format_info(ovl
->cfg
->fourcc
);
2488 if (format
== NULL
) {
2489 dev_err(dev
, "Invalid FOURCC %08x\n", ovl
->cfg
->fourcc
);
2493 ovl
->enabled
= false;
2494 ovl
->mode
= LCDC_OVERLAY_BLEND
;
2500 /* The default Y virtual resolution is twice the panel size to allow for
2503 ovl
->format
= format
;
2504 ovl
->xres
= ovl
->cfg
->max_xres
;
2505 ovl
->xres_virtual
= ovl
->xres
;
2506 ovl
->yres
= ovl
->cfg
->max_yres
;
2507 ovl
->yres_virtual
= ovl
->yres
* 2;
2510 ovl
->pitch
= ovl
->xres_virtual
* format
->bpp
/ 8;
2512 ovl
->pitch
= ovl
->xres_virtual
;
2514 /* Allocate frame buffer memory. */
2515 ovl
->fb_size
= ovl
->cfg
->max_xres
* ovl
->cfg
->max_yres
2516 * format
->bpp
/ 8 * 2;
2517 ovl
->fb_mem
= dma_alloc_coherent(dev
, ovl
->fb_size
, &ovl
->dma_handle
,
2520 dev_err(dev
, "unable to allocate buffer\n");
2524 ret
= sh_mobile_lcdc_overlay_fb_init(ovl
);
2532 sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan
*ch
)
2534 const struct sh_mobile_lcdc_format_info
*format
;
2535 const struct sh_mobile_lcdc_chan_cfg
*cfg
= ch
->cfg
;
2536 struct device
*dev
= ch
->lcdc
->dev
;
2537 const struct fb_videomode
*max_mode
;
2538 const struct fb_videomode
*mode
;
2539 unsigned int num_modes
;
2540 unsigned int max_size
;
2543 ch
->notify
= sh_mobile_lcdc_display_notify
;
2545 /* Validate the format. */
2546 format
= sh_mobile_format_info(cfg
->fourcc
);
2547 if (format
== NULL
) {
2548 dev_err(dev
, "Invalid FOURCC %08x.\n", cfg
->fourcc
);
2552 /* Iterate through the modes to validate them and find the highest
2558 for (i
= 0, mode
= cfg
->lcd_modes
; i
< cfg
->num_modes
; i
++, mode
++) {
2559 unsigned int size
= mode
->yres
* mode
->xres
;
2561 /* NV12/NV21 buffers must have even number of lines */
2562 if ((cfg
->fourcc
== V4L2_PIX_FMT_NV12
||
2563 cfg
->fourcc
== V4L2_PIX_FMT_NV21
) && (mode
->yres
& 0x1)) {
2564 dev_err(dev
, "yres must be multiple of 2 for "
2565 "YCbCr420 mode.\n");
2569 if (size
> max_size
) {
2576 max_size
= MAX_XRES
* MAX_YRES
;
2578 dev_dbg(dev
, "Found largest videomode %ux%u\n",
2579 max_mode
->xres
, max_mode
->yres
);
2581 if (cfg
->lcd_modes
== NULL
) {
2582 mode
= &default_720p
;
2585 mode
= cfg
->lcd_modes
;
2586 num_modes
= cfg
->num_modes
;
2589 /* Use the first mode as default. The default Y virtual resolution is
2590 * twice the panel size to allow for double-buffering.
2592 ch
->format
= format
;
2593 ch
->xres
= mode
->xres
;
2594 ch
->xres_virtual
= mode
->xres
;
2595 ch
->yres
= mode
->yres
;
2596 ch
->yres_virtual
= mode
->yres
* 2;
2599 ch
->colorspace
= V4L2_COLORSPACE_SRGB
;
2600 ch
->pitch
= ch
->xres_virtual
* format
->bpp
/ 8;
2602 ch
->colorspace
= V4L2_COLORSPACE_REC709
;
2603 ch
->pitch
= ch
->xres_virtual
;
2606 ch
->display
.width
= cfg
->panel_cfg
.width
;
2607 ch
->display
.height
= cfg
->panel_cfg
.height
;
2608 ch
->display
.mode
= *mode
;
2610 /* Allocate frame buffer memory. */
2611 ch
->fb_size
= max_size
* format
->bpp
/ 8 * 2;
2612 ch
->fb_mem
= dma_alloc_coherent(dev
, ch
->fb_size
, &ch
->dma_handle
,
2614 if (ch
->fb_mem
== NULL
) {
2615 dev_err(dev
, "unable to allocate buffer\n");
2619 /* Initialize the transmitter device if present. */
2621 if (!cfg
->tx_dev
->dev
.driver
||
2622 !try_module_get(cfg
->tx_dev
->dev
.driver
->owner
)) {
2623 dev_warn(dev
, "unable to get transmitter device\n");
2626 ch
->tx_dev
= platform_get_drvdata(cfg
->tx_dev
);
2627 ch
->tx_dev
->lcdc
= ch
;
2628 ch
->tx_dev
->def_mode
= *mode
;
2631 return sh_mobile_lcdc_channel_fb_init(ch
, mode
, num_modes
);
2634 static int sh_mobile_lcdc_probe(struct platform_device
*pdev
)
2636 struct sh_mobile_lcdc_info
*pdata
= pdev
->dev
.platform_data
;
2637 struct sh_mobile_lcdc_priv
*priv
;
2638 struct resource
*res
;
2644 dev_err(&pdev
->dev
, "no platform data defined\n");
2648 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2649 irq
= platform_get_irq(pdev
, 0);
2650 if (!res
|| irq
< 0) {
2651 dev_err(&pdev
->dev
, "cannot get platform resources\n");
2655 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
2659 priv
->dev
= &pdev
->dev
;
2661 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++)
2662 mutex_init(&priv
->ch
[i
].open_lock
);
2663 platform_set_drvdata(pdev
, priv
);
2665 error
= request_irq(irq
, sh_mobile_lcdc_irq
, 0,
2666 dev_name(&pdev
->dev
), priv
);
2668 dev_err(&pdev
->dev
, "unable to request irq\n");
2673 atomic_set(&priv
->hw_usecnt
, -1);
2675 for (i
= 0, num_channels
= 0; i
< ARRAY_SIZE(pdata
->ch
); i
++) {
2676 struct sh_mobile_lcdc_chan
*ch
= priv
->ch
+ num_channels
;
2679 ch
->cfg
= &pdata
->ch
[i
];
2681 error
= sh_mobile_lcdc_check_interface(ch
);
2683 dev_err(&pdev
->dev
, "unsupported interface type\n");
2686 init_waitqueue_head(&ch
->frame_end_wait
);
2687 init_completion(&ch
->vsync_completion
);
2689 /* probe the backlight is there is one defined */
2690 if (ch
->cfg
->bl_info
.max_brightness
)
2691 ch
->bl
= sh_mobile_lcdc_bl_probe(&pdev
->dev
, ch
);
2693 switch (pdata
->ch
[i
].chan
) {
2694 case LCDC_CHAN_MAINLCD
:
2695 ch
->enabled
= LDCNT2R_ME
;
2696 ch
->reg_offs
= lcdc_offs_mainlcd
;
2699 case LCDC_CHAN_SUBLCD
:
2700 ch
->enabled
= LDCNT2R_SE
;
2701 ch
->reg_offs
= lcdc_offs_sublcd
;
2707 if (!num_channels
) {
2708 dev_err(&pdev
->dev
, "no channels defined\n");
2713 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
2714 if (num_channels
== 2)
2715 priv
->forced_fourcc
= pdata
->ch
[0].fourcc
;
2717 priv
->base
= ioremap_nocache(res
->start
, resource_size(res
));
2723 error
= sh_mobile_lcdc_setup_clocks(priv
, pdata
->clock_source
);
2725 dev_err(&pdev
->dev
, "unable to setup clocks\n");
2729 /* Enable runtime PM. */
2730 pm_runtime_enable(&pdev
->dev
);
2732 for (i
= 0; i
< num_channels
; i
++) {
2733 struct sh_mobile_lcdc_chan
*ch
= &priv
->ch
[i
];
2735 error
= sh_mobile_lcdc_channel_init(ch
);
2740 for (i
= 0; i
< ARRAY_SIZE(pdata
->overlays
); i
++) {
2741 struct sh_mobile_lcdc_overlay
*ovl
= &priv
->overlays
[i
];
2743 ovl
->cfg
= &pdata
->overlays
[i
];
2744 ovl
->channel
= &priv
->ch
[0];
2746 error
= sh_mobile_lcdc_overlay_init(ovl
);
2751 error
= sh_mobile_lcdc_start(priv
);
2753 dev_err(&pdev
->dev
, "unable to start hardware\n");
2757 for (i
= 0; i
< num_channels
; i
++) {
2758 struct sh_mobile_lcdc_chan
*ch
= priv
->ch
+ i
;
2760 error
= sh_mobile_lcdc_channel_fb_register(ch
);
2765 for (i
= 0; i
< ARRAY_SIZE(pdata
->overlays
); i
++) {
2766 struct sh_mobile_lcdc_overlay
*ovl
= &priv
->overlays
[i
];
2768 error
= sh_mobile_lcdc_overlay_fb_register(ovl
);
2773 /* Failure ignored */
2774 priv
->notifier
.notifier_call
= sh_mobile_lcdc_notify
;
2775 fb_register_client(&priv
->notifier
);
2779 sh_mobile_lcdc_remove(pdev
);
2784 static struct platform_driver sh_mobile_lcdc_driver
= {
2786 .name
= "sh_mobile_lcdc_fb",
2787 .pm
= &sh_mobile_lcdc_dev_pm_ops
,
2789 .probe
= sh_mobile_lcdc_probe
,
2790 .remove
= sh_mobile_lcdc_remove
,
2793 module_platform_driver(sh_mobile_lcdc_driver
);
2795 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
2796 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
2797 MODULE_LICENSE("GPL v2");