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/dma-mapping.h>
16 #include <linux/delay.h>
17 #include <linux/gpio.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/ioctl.h>
21 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/slab.h>
27 #include <linux/videodev2.h>
28 #include <linux/vmalloc.h>
30 #include <video/sh_mobile_lcdc.h>
31 #include <video/sh_mobile_meram.h>
33 #include "sh_mobile_lcdcfb.h"
35 #define SIDE_B_OFFSET 0x1000
36 #define MIRROR_OFFSET 0x2000
41 struct sh_mobile_lcdc_priv
{
48 struct sh_mobile_lcdc_chan ch
[2];
49 struct notifier_block notifier
;
51 int forced_fourcc
; /* 2 channel LCDC must share fourcc setting */
52 struct sh_mobile_meram_info
*meram_dev
;
55 /* -----------------------------------------------------------------------------
59 static unsigned long lcdc_offs_mainlcd
[NR_CH_REGS
] = {
79 static unsigned long lcdc_offs_sublcd
[NR_CH_REGS
] = {
97 static bool banked(int reg_nr
)
117 static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan
*chan
)
119 return chan
->cfg
->chan
== LCDC_CHAN_SUBLCD
;
122 static void lcdc_write_chan(struct sh_mobile_lcdc_chan
*chan
,
123 int reg_nr
, unsigned long data
)
125 iowrite32(data
, chan
->lcdc
->base
+ chan
->reg_offs
[reg_nr
]);
127 iowrite32(data
, chan
->lcdc
->base
+ chan
->reg_offs
[reg_nr
] +
131 static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan
*chan
,
132 int reg_nr
, unsigned long data
)
134 iowrite32(data
, chan
->lcdc
->base
+ chan
->reg_offs
[reg_nr
] +
138 static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan
*chan
,
141 return ioread32(chan
->lcdc
->base
+ chan
->reg_offs
[reg_nr
]);
144 static void lcdc_write(struct sh_mobile_lcdc_priv
*priv
,
145 unsigned long reg_offs
, unsigned long data
)
147 iowrite32(data
, priv
->base
+ reg_offs
);
150 static unsigned long lcdc_read(struct sh_mobile_lcdc_priv
*priv
,
151 unsigned long reg_offs
)
153 return ioread32(priv
->base
+ reg_offs
);
156 static void lcdc_wait_bit(struct sh_mobile_lcdc_priv
*priv
,
157 unsigned long reg_offs
,
158 unsigned long mask
, unsigned long until
)
160 while ((lcdc_read(priv
, reg_offs
) & mask
) != until
)
164 /* -----------------------------------------------------------------------------
168 static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv
*priv
)
170 if (atomic_inc_and_test(&priv
->hw_usecnt
)) {
172 clk_enable(priv
->dot_clk
);
173 pm_runtime_get_sync(priv
->dev
);
174 if (priv
->meram_dev
&& priv
->meram_dev
->pdev
)
175 pm_runtime_get_sync(&priv
->meram_dev
->pdev
->dev
);
179 static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv
*priv
)
181 if (atomic_sub_return(1, &priv
->hw_usecnt
) == -1) {
182 if (priv
->meram_dev
&& priv
->meram_dev
->pdev
)
183 pm_runtime_put_sync(&priv
->meram_dev
->pdev
->dev
);
184 pm_runtime_put(priv
->dev
);
186 clk_disable(priv
->dot_clk
);
190 static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv
*priv
,
196 switch (clock_source
) {
199 priv
->lddckr
= LDDCKR_ICKSEL_BUS
;
201 case LCDC_CLK_PERIPHERAL
:
202 str
= "peripheral_clk";
203 priv
->lddckr
= LDDCKR_ICKSEL_MIPI
;
205 case LCDC_CLK_EXTERNAL
:
207 priv
->lddckr
= LDDCKR_ICKSEL_HDMI
;
216 clk
= clk_get(priv
->dev
, str
);
218 dev_err(priv
->dev
, "cannot get dot clock %s\n", str
);
226 /* -----------------------------------------------------------------------------
227 * Display, panel and deferred I/O
230 static void lcdc_sys_write_index(void *handle
, unsigned long data
)
232 struct sh_mobile_lcdc_chan
*ch
= handle
;
234 lcdc_write(ch
->lcdc
, _LDDWD0R
, data
| LDDWDxR_WDACT
);
235 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
236 lcdc_write(ch
->lcdc
, _LDDWAR
, LDDWAR_WA
|
237 (lcdc_chan_is_sublcd(ch
) ? 2 : 0));
238 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
241 static void lcdc_sys_write_data(void *handle
, unsigned long data
)
243 struct sh_mobile_lcdc_chan
*ch
= handle
;
245 lcdc_write(ch
->lcdc
, _LDDWD0R
, data
| LDDWDxR_WDACT
| LDDWDxR_RSW
);
246 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
247 lcdc_write(ch
->lcdc
, _LDDWAR
, LDDWAR_WA
|
248 (lcdc_chan_is_sublcd(ch
) ? 2 : 0));
249 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
252 static unsigned long lcdc_sys_read_data(void *handle
)
254 struct sh_mobile_lcdc_chan
*ch
= handle
;
256 lcdc_write(ch
->lcdc
, _LDDRDR
, LDDRDR_RSR
);
257 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
258 lcdc_write(ch
->lcdc
, _LDDRAR
, LDDRAR_RA
|
259 (lcdc_chan_is_sublcd(ch
) ? 2 : 0));
261 lcdc_wait_bit(ch
->lcdc
, _LDSR
, LDSR_AS
, 0);
263 return lcdc_read(ch
->lcdc
, _LDDRDR
) & LDDRDR_DRD_MASK
;
266 struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops
= {
267 lcdc_sys_write_index
,
272 static int sh_mobile_lcdc_sginit(struct fb_info
*info
,
273 struct list_head
*pagelist
)
275 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
276 unsigned int nr_pages_max
= ch
->fb_size
>> PAGE_SHIFT
;
280 sg_init_table(ch
->sglist
, nr_pages_max
);
282 list_for_each_entry(page
, pagelist
, lru
)
283 sg_set_page(&ch
->sglist
[nr_pages
++], page
, PAGE_SIZE
, 0);
288 static void sh_mobile_lcdc_deferred_io(struct fb_info
*info
,
289 struct list_head
*pagelist
)
291 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
292 const struct sh_mobile_lcdc_panel_cfg
*panel
= &ch
->cfg
->panel_cfg
;
294 /* enable clocks before accessing hardware */
295 sh_mobile_lcdc_clk_on(ch
->lcdc
);
298 * It's possible to get here without anything on the pagelist via
299 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
300 * invocation. In the former case, the acceleration routines are
301 * stepped in to when using the framebuffer console causing the
302 * workqueue to be scheduled without any dirty pages on the list.
304 * Despite this, a panel update is still needed given that the
305 * acceleration routines have their own methods for writing in
306 * that still need to be updated.
308 * The fsync() and empty pagelist case could be optimized for,
309 * but we don't bother, as any application exhibiting such
310 * behaviour is fundamentally broken anyways.
312 if (!list_empty(pagelist
)) {
313 unsigned int nr_pages
= sh_mobile_lcdc_sginit(info
, pagelist
);
315 /* trigger panel update */
316 dma_map_sg(ch
->lcdc
->dev
, ch
->sglist
, nr_pages
, DMA_TO_DEVICE
);
317 if (panel
->start_transfer
)
318 panel
->start_transfer(ch
, &sh_mobile_lcdc_sys_bus_ops
);
319 lcdc_write_chan(ch
, LDSM2R
, LDSM2R_OSTRG
);
320 dma_unmap_sg(ch
->lcdc
->dev
, ch
->sglist
, nr_pages
,
323 if (panel
->start_transfer
)
324 panel
->start_transfer(ch
, &sh_mobile_lcdc_sys_bus_ops
);
325 lcdc_write_chan(ch
, LDSM2R
, LDSM2R_OSTRG
);
329 static void sh_mobile_lcdc_deferred_io_touch(struct fb_info
*info
)
331 struct fb_deferred_io
*fbdefio
= info
->fbdefio
;
334 schedule_delayed_work(&info
->deferred_work
, fbdefio
->delay
);
337 static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan
*ch
)
339 const struct sh_mobile_lcdc_panel_cfg
*panel
= &ch
->cfg
->panel_cfg
;
344 ret
= ch
->tx_dev
->ops
->display_on(ch
->tx_dev
);
348 if (ret
== SH_MOBILE_LCDC_DISPLAY_DISCONNECTED
)
349 ch
->info
->state
= FBINFO_STATE_SUSPENDED
;
352 /* HDMI must be enabled before LCDC configuration */
353 if (panel
->display_on
)
357 static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan
*ch
)
359 const struct sh_mobile_lcdc_panel_cfg
*panel
= &ch
->cfg
->panel_cfg
;
361 if (panel
->display_off
)
362 panel
->display_off();
365 ch
->tx_dev
->ops
->display_off(ch
->tx_dev
);
369 sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan
*ch
,
370 const struct fb_videomode
*new_mode
)
372 dev_dbg(ch
->info
->dev
, "Old %ux%u, new %ux%u\n",
373 ch
->display
.mode
.xres
, ch
->display
.mode
.yres
,
374 new_mode
->xres
, new_mode
->yres
);
376 /* It can be a different monitor with an equal video-mode */
377 if (fb_mode_is_equal(&ch
->display
.mode
, new_mode
))
380 dev_dbg(ch
->info
->dev
, "Switching %u -> %u lines\n",
381 ch
->display
.mode
.yres
, new_mode
->yres
);
382 ch
->display
.mode
= *new_mode
;
387 static int sh_mobile_check_var(struct fb_var_screeninfo
*var
,
388 struct fb_info
*info
);
390 static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan
*ch
,
391 enum sh_mobile_lcdc_entity_event event
,
392 const struct fb_videomode
*mode
,
393 const struct fb_monspecs
*monspec
)
395 struct fb_info
*info
= ch
->info
;
396 struct fb_var_screeninfo var
;
400 case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT
:
402 if (lock_fb_info(info
)) {
405 ch
->display
.width
= monspec
->max_x
* 10;
406 ch
->display
.height
= monspec
->max_y
* 10;
408 if (!sh_mobile_lcdc_must_reconfigure(ch
, mode
) &&
409 info
->state
== FBINFO_STATE_RUNNING
) {
410 /* First activation with the default monitor.
411 * Just turn on, if we run a resume here, the
414 info
->var
.width
= monspec
->max_x
* 10;
415 info
->var
.height
= monspec
->max_y
* 10;
416 sh_mobile_lcdc_display_on(ch
);
418 /* New monitor or have to wake up */
419 fb_set_suspend(info
, 0);
423 unlock_fb_info(info
);
427 case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT
:
428 /* HDMI disconnect */
429 if (lock_fb_info(info
)) {
431 fb_set_suspend(info
, 1);
433 unlock_fb_info(info
);
437 case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE
:
438 /* Validate a proposed new mode */
439 fb_videomode_to_var(&var
, mode
);
440 var
.bits_per_pixel
= info
->var
.bits_per_pixel
;
441 var
.grayscale
= info
->var
.grayscale
;
442 ret
= sh_mobile_check_var(&var
, info
);
449 /* -----------------------------------------------------------------------------
453 struct sh_mobile_lcdc_format_info
{
460 static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos
[] = {
462 .fourcc
= V4L2_PIX_FMT_RGB565
,
465 .lddfr
= LDDFR_PKF_RGB16
,
467 .fourcc
= V4L2_PIX_FMT_BGR24
,
470 .lddfr
= LDDFR_PKF_RGB24
,
472 .fourcc
= V4L2_PIX_FMT_BGR32
,
475 .lddfr
= LDDFR_PKF_ARGB32
,
477 .fourcc
= V4L2_PIX_FMT_NV12
,
480 .lddfr
= LDDFR_CC
| LDDFR_YF_420
,
482 .fourcc
= V4L2_PIX_FMT_NV21
,
485 .lddfr
= LDDFR_CC
| LDDFR_YF_420
,
487 .fourcc
= V4L2_PIX_FMT_NV16
,
490 .lddfr
= LDDFR_CC
| LDDFR_YF_422
,
492 .fourcc
= V4L2_PIX_FMT_NV61
,
495 .lddfr
= LDDFR_CC
| LDDFR_YF_422
,
497 .fourcc
= V4L2_PIX_FMT_NV24
,
500 .lddfr
= LDDFR_CC
| LDDFR_YF_444
,
502 .fourcc
= V4L2_PIX_FMT_NV42
,
505 .lddfr
= LDDFR_CC
| LDDFR_YF_444
,
509 static const struct sh_mobile_lcdc_format_info
*
510 sh_mobile_format_info(u32 fourcc
)
514 for (i
= 0; i
< ARRAY_SIZE(sh_mobile_format_infos
); ++i
) {
515 if (sh_mobile_format_infos
[i
].fourcc
== fourcc
)
516 return &sh_mobile_format_infos
[i
];
522 static int sh_mobile_format_fourcc(const struct fb_var_screeninfo
*var
)
524 if (var
->grayscale
> 1)
525 return var
->grayscale
;
527 switch (var
->bits_per_pixel
) {
529 return V4L2_PIX_FMT_RGB565
;
531 return V4L2_PIX_FMT_BGR24
;
533 return V4L2_PIX_FMT_BGR32
;
539 static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo
*var
)
541 return var
->grayscale
> 1;
544 /* -----------------------------------------------------------------------------
545 * Start, stop and IRQ
548 static irqreturn_t
sh_mobile_lcdc_irq(int irq
, void *data
)
550 struct sh_mobile_lcdc_priv
*priv
= data
;
551 struct sh_mobile_lcdc_chan
*ch
;
552 unsigned long ldintr
;
556 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
557 ldintr
= lcdc_read(priv
, _LDINTR
);
558 lcdc_write(priv
, _LDINTR
, (ldintr
^ LDINTR_STATUS_MASK
) & ~LDINTR_VEE
);
560 /* figure out if this interrupt is for main or sub lcd */
561 is_sub
= (lcdc_read(priv
, _LDSR
) & LDSR_MSS
) ? 1 : 0;
563 /* wake up channel and disable clocks */
564 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
571 if (ldintr
& LDINTR_FS
) {
572 if (is_sub
== lcdc_chan_is_sublcd(ch
)) {
574 wake_up(&ch
->frame_end_wait
);
576 sh_mobile_lcdc_clk_off(priv
);
581 if (ldintr
& LDINTR_VES
)
582 complete(&ch
->vsync_completion
);
588 static int sh_mobile_wait_for_vsync(struct sh_mobile_lcdc_chan
*ch
)
590 unsigned long ldintr
;
593 /* Enable VSync End interrupt and be careful not to acknowledge any
596 ldintr
= lcdc_read(ch
->lcdc
, _LDINTR
);
597 ldintr
|= LDINTR_VEE
| LDINTR_STATUS_MASK
;
598 lcdc_write(ch
->lcdc
, _LDINTR
, ldintr
);
600 ret
= wait_for_completion_interruptible_timeout(&ch
->vsync_completion
,
601 msecs_to_jiffies(100));
608 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv
*priv
,
611 unsigned long tmp
= lcdc_read(priv
, _LDCNT2R
);
614 /* start or stop the lcdc */
616 lcdc_write(priv
, _LDCNT2R
, tmp
| LDCNT2R_DO
);
618 lcdc_write(priv
, _LDCNT2R
, tmp
& ~LDCNT2R_DO
);
620 /* wait until power is applied/stopped on all channels */
621 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++)
622 if (lcdc_read(priv
, _LDCNT2R
) & priv
->ch
[k
].enabled
)
624 tmp
= lcdc_read_chan(&priv
->ch
[k
], LDPMR
)
626 if (start
&& tmp
== LDPMR_LPS
)
628 if (!start
&& tmp
== 0)
634 lcdc_write(priv
, _LDDCKSTPR
, 1); /* stop dotclock */
637 static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan
*ch
)
639 const struct fb_var_screeninfo
*var
= &ch
->info
->var
;
640 const struct fb_videomode
*mode
= &ch
->display
.mode
;
641 unsigned long h_total
, hsync_pos
, display_h_total
;
644 tmp
= ch
->ldmt1r_value
;
645 tmp
|= (var
->sync
& FB_SYNC_VERT_HIGH_ACT
) ? 0 : LDMT1R_VPOL
;
646 tmp
|= (var
->sync
& FB_SYNC_HOR_HIGH_ACT
) ? 0 : LDMT1R_HPOL
;
647 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DWPOL
) ? LDMT1R_DWPOL
: 0;
648 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DIPOL
) ? LDMT1R_DIPOL
: 0;
649 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DAPOL
) ? LDMT1R_DAPOL
: 0;
650 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_HSCNT
) ? LDMT1R_HSCNT
: 0;
651 tmp
|= (ch
->cfg
->flags
& LCDC_FLAGS_DWCNT
) ? LDMT1R_DWCNT
: 0;
652 lcdc_write_chan(ch
, LDMT1R
, tmp
);
655 lcdc_write_chan(ch
, LDMT2R
, ch
->cfg
->sys_bus_cfg
.ldmt2r
);
656 lcdc_write_chan(ch
, LDMT3R
, ch
->cfg
->sys_bus_cfg
.ldmt3r
);
658 /* horizontal configuration */
659 h_total
= mode
->xres
+ mode
->hsync_len
+ mode
->left_margin
660 + mode
->right_margin
;
661 tmp
= h_total
/ 8; /* HTCN */
662 tmp
|= (min(mode
->xres
, ch
->xres
) / 8) << 16; /* HDCN */
663 lcdc_write_chan(ch
, LDHCNR
, tmp
);
665 hsync_pos
= mode
->xres
+ mode
->right_margin
;
666 tmp
= hsync_pos
/ 8; /* HSYNP */
667 tmp
|= (mode
->hsync_len
/ 8) << 16; /* HSYNW */
668 lcdc_write_chan(ch
, LDHSYNR
, tmp
);
670 /* vertical configuration */
671 tmp
= mode
->yres
+ mode
->vsync_len
+ mode
->upper_margin
672 + mode
->lower_margin
; /* VTLN */
673 tmp
|= min(mode
->yres
, ch
->yres
) << 16; /* VDLN */
674 lcdc_write_chan(ch
, LDVLNR
, tmp
);
676 tmp
= mode
->yres
+ mode
->lower_margin
; /* VSYNP */
677 tmp
|= mode
->vsync_len
<< 16; /* VSYNW */
678 lcdc_write_chan(ch
, LDVSYNR
, tmp
);
680 /* Adjust horizontal synchronisation for HDMI */
681 display_h_total
= mode
->xres
+ mode
->hsync_len
+ mode
->left_margin
682 + mode
->right_margin
;
683 tmp
= ((mode
->xres
& 7) << 24) | ((display_h_total
& 7) << 16)
684 | ((mode
->hsync_len
& 7) << 8) | (hsync_pos
& 7);
685 lcdc_write_chan(ch
, LDHAJR
, tmp
);
689 * __sh_mobile_lcdc_start - Configure and tart the LCDC
692 * Configure all enabled channels and start the LCDC device. All external
693 * devices (clocks, MERAM, panels, ...) are not touched by this function.
695 static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv
*priv
)
697 struct sh_mobile_lcdc_chan
*ch
;
701 /* Enable LCDC channels. Read data from external memory, avoid using the
704 lcdc_write(priv
, _LDCNT2R
, priv
->ch
[0].enabled
| priv
->ch
[1].enabled
);
706 /* Stop the LCDC first and disable all interrupts. */
707 sh_mobile_lcdc_start_stop(priv
, 0);
708 lcdc_write(priv
, _LDINTR
, 0);
710 /* Configure power supply, dot clocks and start them. */
712 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
718 lcdc_write_chan(ch
, LDPMR
, 0);
720 m
= ch
->cfg
->clock_divider
;
724 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
727 lcdc_write_chan(ch
, LDDCKPAT1R
, 0);
728 lcdc_write_chan(ch
, LDDCKPAT2R
, (1 << (m
/2)) - 1);
732 tmp
|= m
<< (lcdc_chan_is_sublcd(ch
) ? 8 : 0);
735 lcdc_write(priv
, _LDDCKR
, tmp
);
736 lcdc_write(priv
, _LDDCKSTPR
, 0);
737 lcdc_wait_bit(priv
, _LDDCKSTPR
, ~0, 0);
739 /* Setup geometry, format, frame buffer memory and operation mode. */
740 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
745 sh_mobile_lcdc_geometry(ch
);
747 tmp
= ch
->format
->lddfr
;
749 if (ch
->format
->yuv
) {
750 switch (ch
->colorspace
) {
751 case V4L2_COLORSPACE_REC709
:
754 case V4L2_COLORSPACE_JPEG
:
760 lcdc_write_chan(ch
, LDDFR
, tmp
);
761 lcdc_write_chan(ch
, LDMLSR
, ch
->line_size
);
762 lcdc_write_chan(ch
, LDSA1R
, ch
->base_addr_y
);
764 lcdc_write_chan(ch
, LDSA2R
, ch
->base_addr_c
);
766 /* When using deferred I/O mode, configure the LCDC for one-shot
767 * operation and enable the frame end interrupt. Otherwise use
768 * continuous read mode.
770 if (ch
->ldmt1r_value
& LDMT1R_IFM
&&
771 ch
->cfg
->sys_bus_cfg
.deferred_io_msec
) {
772 lcdc_write_chan(ch
, LDSM1R
, LDSM1R_OS
);
773 lcdc_write(priv
, _LDINTR
, LDINTR_FE
);
775 lcdc_write_chan(ch
, LDSM1R
, 0);
779 /* Word and long word swap. */
780 switch (priv
->ch
[0].format
->fourcc
) {
781 case V4L2_PIX_FMT_RGB565
:
782 case V4L2_PIX_FMT_NV21
:
783 case V4L2_PIX_FMT_NV61
:
784 case V4L2_PIX_FMT_NV42
:
785 tmp
= LDDDSR_LS
| LDDDSR_WS
;
787 case V4L2_PIX_FMT_BGR24
:
788 case V4L2_PIX_FMT_NV12
:
789 case V4L2_PIX_FMT_NV16
:
790 case V4L2_PIX_FMT_NV24
:
791 tmp
= LDDDSR_LS
| LDDDSR_WS
| LDDDSR_BS
;
793 case V4L2_PIX_FMT_BGR32
:
798 lcdc_write(priv
, _LDDDSR
, tmp
);
800 /* Enable the display output. */
801 lcdc_write(priv
, _LDCNT1R
, LDCNT1R_DE
);
802 sh_mobile_lcdc_start_stop(priv
, 1);
806 static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv
*priv
)
808 struct sh_mobile_meram_info
*mdev
= priv
->meram_dev
;
809 struct sh_mobile_lcdc_chan
*ch
;
814 /* enable clocks before accessing the hardware */
815 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
816 if (priv
->ch
[k
].enabled
)
817 sh_mobile_lcdc_clk_on(priv
);
821 lcdc_write(priv
, _LDCNT2R
, lcdc_read(priv
, _LDCNT2R
) | LDCNT2R_BR
);
822 lcdc_wait_bit(priv
, _LDCNT2R
, LDCNT2R_BR
, 0);
824 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
825 const struct sh_mobile_lcdc_panel_cfg
*panel
;
831 panel
= &ch
->cfg
->panel_cfg
;
832 if (panel
->setup_sys
) {
833 ret
= panel
->setup_sys(ch
, &sh_mobile_lcdc_sys_bus_ops
);
839 /* Compute frame buffer base address and pitch for each channel. */
840 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
848 ch
->base_addr_y
= ch
->dma_handle
;
849 ch
->base_addr_c
= ch
->base_addr_y
+ ch
->xres
* ch
->yres_virtual
;
850 ch
->line_size
= ch
->pitch
;
852 /* Enable MERAM if possible. */
853 if (mdev
== NULL
|| mdev
->ops
== NULL
||
854 ch
->cfg
->meram_cfg
== NULL
)
857 /* we need to de-init configured ICBs before we can
858 * re-initialize them.
861 mdev
->ops
->meram_unregister(mdev
, ch
->meram
);
865 switch (ch
->format
->fourcc
) {
866 case V4L2_PIX_FMT_NV12
:
867 case V4L2_PIX_FMT_NV21
:
868 case V4L2_PIX_FMT_NV16
:
869 case V4L2_PIX_FMT_NV61
:
870 pixelformat
= SH_MOBILE_MERAM_PF_NV
;
872 case V4L2_PIX_FMT_NV24
:
873 case V4L2_PIX_FMT_NV42
:
874 pixelformat
= SH_MOBILE_MERAM_PF_NV24
;
876 case V4L2_PIX_FMT_RGB565
:
877 case V4L2_PIX_FMT_BGR24
:
878 case V4L2_PIX_FMT_BGR32
:
880 pixelformat
= SH_MOBILE_MERAM_PF_RGB
;
884 meram
= mdev
->ops
->meram_register(mdev
, ch
->cfg
->meram_cfg
,
885 ch
->pitch
, ch
->yres
, pixelformat
,
887 if (!IS_ERR(meram
)) {
888 mdev
->ops
->meram_update(mdev
, meram
,
889 ch
->base_addr_y
, ch
->base_addr_c
,
890 &ch
->base_addr_y
, &ch
->base_addr_c
);
895 /* Start the LCDC. */
896 __sh_mobile_lcdc_start(priv
);
898 /* Setup deferred I/O, tell the board code to enable the panels, and
901 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
906 tmp
= ch
->cfg
->sys_bus_cfg
.deferred_io_msec
;
907 if (ch
->ldmt1r_value
& LDMT1R_IFM
&& tmp
) {
908 ch
->defio
.deferred_io
= sh_mobile_lcdc_deferred_io
;
909 ch
->defio
.delay
= msecs_to_jiffies(tmp
);
910 ch
->info
->fbdefio
= &ch
->defio
;
911 fb_deferred_io_init(ch
->info
);
914 sh_mobile_lcdc_display_on(ch
);
917 ch
->bl
->props
.power
= FB_BLANK_UNBLANK
;
918 backlight_update_status(ch
->bl
);
925 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv
*priv
)
927 struct sh_mobile_lcdc_chan
*ch
;
930 /* clean up deferred io and ask board code to disable panel */
931 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
937 * flush frame, and wait for frame end interrupt
938 * clean up deferred io and enable clock
940 if (ch
->info
&& ch
->info
->fbdefio
) {
942 schedule_delayed_work(&ch
->info
->deferred_work
, 0);
943 wait_event(ch
->frame_end_wait
, ch
->frame_end
);
944 fb_deferred_io_cleanup(ch
->info
);
945 ch
->info
->fbdefio
= NULL
;
946 sh_mobile_lcdc_clk_on(priv
);
950 ch
->bl
->props
.power
= FB_BLANK_POWERDOWN
;
951 backlight_update_status(ch
->bl
);
954 sh_mobile_lcdc_display_off(ch
);
956 /* disable the meram */
958 struct sh_mobile_meram_info
*mdev
;
959 mdev
= priv
->meram_dev
;
960 mdev
->ops
->meram_unregister(mdev
, ch
->meram
);
968 sh_mobile_lcdc_start_stop(priv
, 0);
973 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++)
974 if (priv
->ch
[k
].enabled
)
975 sh_mobile_lcdc_clk_off(priv
);
978 /* -----------------------------------------------------------------------------
979 * Frame buffer operations
982 static int sh_mobile_lcdc_setcolreg(u_int regno
,
983 u_int red
, u_int green
, u_int blue
,
984 u_int transp
, struct fb_info
*info
)
986 u32
*palette
= info
->pseudo_palette
;
988 if (regno
>= PALETTE_NR
)
991 /* only FB_VISUAL_TRUECOLOR supported */
993 red
>>= 16 - info
->var
.red
.length
;
994 green
>>= 16 - info
->var
.green
.length
;
995 blue
>>= 16 - info
->var
.blue
.length
;
996 transp
>>= 16 - info
->var
.transp
.length
;
998 palette
[regno
] = (red
<< info
->var
.red
.offset
) |
999 (green
<< info
->var
.green
.offset
) |
1000 (blue
<< info
->var
.blue
.offset
) |
1001 (transp
<< info
->var
.transp
.offset
);
1006 static const struct fb_fix_screeninfo sh_mobile_lcdc_fix
= {
1007 .id
= "SH Mobile LCDC",
1008 .type
= FB_TYPE_PACKED_PIXELS
,
1009 .visual
= FB_VISUAL_TRUECOLOR
,
1010 .accel
= FB_ACCEL_NONE
,
1014 .capabilities
= FB_CAP_FOURCC
,
1017 static void sh_mobile_lcdc_fillrect(struct fb_info
*info
,
1018 const struct fb_fillrect
*rect
)
1020 sys_fillrect(info
, rect
);
1021 sh_mobile_lcdc_deferred_io_touch(info
);
1024 static void sh_mobile_lcdc_copyarea(struct fb_info
*info
,
1025 const struct fb_copyarea
*area
)
1027 sys_copyarea(info
, area
);
1028 sh_mobile_lcdc_deferred_io_touch(info
);
1031 static void sh_mobile_lcdc_imageblit(struct fb_info
*info
,
1032 const struct fb_image
*image
)
1034 sys_imageblit(info
, image
);
1035 sh_mobile_lcdc_deferred_io_touch(info
);
1038 static int sh_mobile_fb_pan_display(struct fb_var_screeninfo
*var
,
1039 struct fb_info
*info
)
1041 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1042 struct sh_mobile_lcdc_priv
*priv
= ch
->lcdc
;
1043 unsigned long ldrcntr
;
1044 unsigned long new_pan_offset
;
1045 unsigned long base_addr_y
, base_addr_c
;
1046 unsigned long c_offset
;
1048 if (!ch
->format
->yuv
)
1049 new_pan_offset
= var
->yoffset
* ch
->pitch
1050 + var
->xoffset
* (ch
->format
->bpp
/ 8);
1052 new_pan_offset
= var
->yoffset
* ch
->pitch
+ var
->xoffset
;
1054 if (new_pan_offset
== ch
->pan_offset
)
1055 return 0; /* No change, do nothing */
1057 ldrcntr
= lcdc_read(priv
, _LDRCNTR
);
1059 /* Set the source address for the next refresh */
1060 base_addr_y
= ch
->dma_handle
+ new_pan_offset
;
1061 if (ch
->format
->yuv
) {
1063 c_offset
= var
->yoffset
* ch
->pitch
1064 * (ch
->format
->bpp
- 8) / 8;
1065 base_addr_c
= ch
->dma_handle
+ ch
->xres
* ch
->yres_virtual
1068 if (ch
->format
->fourcc
== V4L2_PIX_FMT_NV24
)
1069 base_addr_c
+= 2 * var
->xoffset
;
1071 base_addr_c
+= var
->xoffset
;
1075 struct sh_mobile_meram_info
*mdev
;
1077 mdev
= priv
->meram_dev
;
1078 mdev
->ops
->meram_update(mdev
, ch
->meram
,
1079 base_addr_y
, base_addr_c
,
1080 &base_addr_y
, &base_addr_c
);
1083 ch
->base_addr_y
= base_addr_y
;
1084 ch
->base_addr_c
= base_addr_c
;
1086 lcdc_write_chan_mirror(ch
, LDSA1R
, base_addr_y
);
1087 if (ch
->format
->yuv
)
1088 lcdc_write_chan_mirror(ch
, LDSA2R
, base_addr_c
);
1090 if (lcdc_chan_is_sublcd(ch
))
1091 lcdc_write(ch
->lcdc
, _LDRCNTR
, ldrcntr
^ LDRCNTR_SRS
);
1093 lcdc_write(ch
->lcdc
, _LDRCNTR
, ldrcntr
^ LDRCNTR_MRS
);
1095 ch
->pan_offset
= new_pan_offset
;
1097 sh_mobile_lcdc_deferred_io_touch(info
);
1102 static int sh_mobile_ioctl(struct fb_info
*info
, unsigned int cmd
,
1108 case FBIO_WAITFORVSYNC
:
1109 retval
= sh_mobile_wait_for_vsync(info
->par
);
1113 retval
= -ENOIOCTLCMD
;
1119 static void sh_mobile_fb_reconfig(struct fb_info
*info
)
1121 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1122 struct fb_var_screeninfo var
;
1123 struct fb_videomode mode
;
1124 struct fb_event event
;
1125 int evnt
= FB_EVENT_MODE_CHANGE_ALL
;
1127 if (ch
->use_count
> 1 || (ch
->use_count
== 1 && !info
->fbcon_par
))
1128 /* More framebuffer users are active */
1131 fb_var_to_videomode(&mode
, &info
->var
);
1133 if (fb_mode_is_equal(&ch
->display
.mode
, &mode
))
1136 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1138 fb_videomode_to_var(&var
, &ch
->display
.mode
);
1139 var
.width
= ch
->display
.width
;
1140 var
.height
= ch
->display
.height
;
1141 var
.activate
= FB_ACTIVATE_NOW
;
1143 if (fb_set_var(info
, &var
) < 0)
1144 /* Couldn't reconfigure, hopefully, can continue as before */
1148 * fb_set_var() calls the notifier change internally, only if
1149 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1150 * user event, we have to call the chain ourselves.
1153 event
.data
= &ch
->display
.mode
;
1154 fb_notifier_call_chain(evnt
, &event
);
1158 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1159 * user == 1, or with console sem held, if user == 0.
1161 static int sh_mobile_release(struct fb_info
*info
, int user
)
1163 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1165 mutex_lock(&ch
->open_lock
);
1166 dev_dbg(info
->dev
, "%s(): %d users\n", __func__
, ch
->use_count
);
1170 /* Nothing to reconfigure, when called from fbcon */
1173 sh_mobile_fb_reconfig(info
);
1177 mutex_unlock(&ch
->open_lock
);
1182 static int sh_mobile_open(struct fb_info
*info
, int user
)
1184 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1186 mutex_lock(&ch
->open_lock
);
1189 dev_dbg(info
->dev
, "%s(): %d users\n", __func__
, ch
->use_count
);
1190 mutex_unlock(&ch
->open_lock
);
1195 static int sh_mobile_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
1197 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1198 struct sh_mobile_lcdc_priv
*p
= ch
->lcdc
;
1199 unsigned int best_dist
= (unsigned int)-1;
1200 unsigned int best_xres
= 0;
1201 unsigned int best_yres
= 0;
1204 if (var
->xres
> MAX_XRES
|| var
->yres
> MAX_YRES
)
1207 /* If board code provides us with a list of available modes, make sure
1208 * we use one of them. Find the mode closest to the requested one. The
1209 * distance between two modes is defined as the size of the
1210 * non-overlapping parts of the two rectangles.
1212 for (i
= 0; i
< ch
->cfg
->num_modes
; ++i
) {
1213 const struct fb_videomode
*mode
= &ch
->cfg
->lcd_modes
[i
];
1216 /* We can only round up. */
1217 if (var
->xres
> mode
->xres
|| var
->yres
> mode
->yres
)
1220 dist
= var
->xres
* var
->yres
+ mode
->xres
* mode
->yres
1221 - 2 * min(var
->xres
, mode
->xres
)
1222 * min(var
->yres
, mode
->yres
);
1224 if (dist
< best_dist
) {
1225 best_xres
= mode
->xres
;
1226 best_yres
= mode
->yres
;
1231 /* If no available mode can be used, return an error. */
1232 if (ch
->cfg
->num_modes
!= 0) {
1233 if (best_dist
== (unsigned int)-1)
1236 var
->xres
= best_xres
;
1237 var
->yres
= best_yres
;
1240 /* Make sure the virtual resolution is at least as big as the visible
1243 if (var
->xres_virtual
< var
->xres
)
1244 var
->xres_virtual
= var
->xres
;
1245 if (var
->yres_virtual
< var
->yres
)
1246 var
->yres_virtual
= var
->yres
;
1248 if (sh_mobile_format_is_fourcc(var
)) {
1249 const struct sh_mobile_lcdc_format_info
*format
;
1251 format
= sh_mobile_format_info(var
->grayscale
);
1254 var
->bits_per_pixel
= format
->bpp
;
1256 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1260 var
->colorspace
= V4L2_COLORSPACE_SRGB
;
1261 else if (var
->colorspace
!= V4L2_COLORSPACE_REC709
)
1262 var
->colorspace
= V4L2_COLORSPACE_JPEG
;
1264 if (var
->bits_per_pixel
<= 16) { /* RGB 565 */
1265 var
->bits_per_pixel
= 16;
1266 var
->red
.offset
= 11;
1267 var
->red
.length
= 5;
1268 var
->green
.offset
= 5;
1269 var
->green
.length
= 6;
1270 var
->blue
.offset
= 0;
1271 var
->blue
.length
= 5;
1272 var
->transp
.offset
= 0;
1273 var
->transp
.length
= 0;
1274 } else if (var
->bits_per_pixel
<= 24) { /* RGB 888 */
1275 var
->bits_per_pixel
= 24;
1276 var
->red
.offset
= 16;
1277 var
->red
.length
= 8;
1278 var
->green
.offset
= 8;
1279 var
->green
.length
= 8;
1280 var
->blue
.offset
= 0;
1281 var
->blue
.length
= 8;
1282 var
->transp
.offset
= 0;
1283 var
->transp
.length
= 0;
1284 } else if (var
->bits_per_pixel
<= 32) { /* RGBA 888 */
1285 var
->bits_per_pixel
= 32;
1286 var
->red
.offset
= 16;
1287 var
->red
.length
= 8;
1288 var
->green
.offset
= 8;
1289 var
->green
.length
= 8;
1290 var
->blue
.offset
= 0;
1291 var
->blue
.length
= 8;
1292 var
->transp
.offset
= 24;
1293 var
->transp
.length
= 8;
1297 var
->red
.msb_right
= 0;
1298 var
->green
.msb_right
= 0;
1299 var
->blue
.msb_right
= 0;
1300 var
->transp
.msb_right
= 0;
1303 /* Make sure we don't exceed our allocated memory. */
1304 if (var
->xres_virtual
* var
->yres_virtual
* var
->bits_per_pixel
/ 8 >
1308 /* only accept the forced_fourcc for dual channel configurations */
1309 if (p
->forced_fourcc
&&
1310 p
->forced_fourcc
!= sh_mobile_format_fourcc(var
))
1316 static int sh_mobile_set_par(struct fb_info
*info
)
1318 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1321 sh_mobile_lcdc_stop(ch
->lcdc
);
1323 ch
->format
= sh_mobile_format_info(sh_mobile_format_fourcc(&info
->var
));
1324 ch
->colorspace
= info
->var
.colorspace
;
1326 ch
->xres
= info
->var
.xres
;
1327 ch
->xres_virtual
= info
->var
.xres_virtual
;
1328 ch
->yres
= info
->var
.yres
;
1329 ch
->yres_virtual
= info
->var
.yres_virtual
;
1331 if (ch
->format
->yuv
)
1332 ch
->pitch
= info
->var
.xres
;
1334 ch
->pitch
= info
->var
.xres
* ch
->format
->bpp
/ 8;
1336 ret
= sh_mobile_lcdc_start(ch
->lcdc
);
1338 dev_err(info
->dev
, "%s: unable to restart LCDC\n", __func__
);
1340 info
->fix
.line_length
= ch
->pitch
;
1342 if (sh_mobile_format_is_fourcc(&info
->var
)) {
1343 info
->fix
.type
= FB_TYPE_FOURCC
;
1344 info
->fix
.visual
= FB_VISUAL_FOURCC
;
1346 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1347 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
1354 * Screen blanking. Behavior is as follows:
1355 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1356 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1359 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1361 static int sh_mobile_lcdc_blank(int blank
, struct fb_info
*info
)
1363 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1364 struct sh_mobile_lcdc_priv
*p
= ch
->lcdc
;
1366 /* blank the screen? */
1367 if (blank
> FB_BLANK_UNBLANK
&& ch
->blank_status
== FB_BLANK_UNBLANK
) {
1368 struct fb_fillrect rect
= {
1372 sh_mobile_lcdc_fillrect(info
, &rect
);
1374 /* turn clocks on? */
1375 if (blank
<= FB_BLANK_NORMAL
&& ch
->blank_status
> FB_BLANK_NORMAL
) {
1376 sh_mobile_lcdc_clk_on(p
);
1378 /* turn clocks off? */
1379 if (blank
> FB_BLANK_NORMAL
&& ch
->blank_status
<= FB_BLANK_NORMAL
) {
1380 /* make sure the screen is updated with the black fill before
1381 * switching the clocks off. one vsync is not enough since
1382 * blanking may occur in the middle of a refresh. deferred io
1383 * mode will reenable the clocks and update the screen in time,
1384 * so it does not need this. */
1385 if (!info
->fbdefio
) {
1386 sh_mobile_wait_for_vsync(ch
);
1387 sh_mobile_wait_for_vsync(ch
);
1389 sh_mobile_lcdc_clk_off(p
);
1392 ch
->blank_status
= blank
;
1396 static struct fb_ops sh_mobile_lcdc_ops
= {
1397 .owner
= THIS_MODULE
,
1398 .fb_setcolreg
= sh_mobile_lcdc_setcolreg
,
1399 .fb_read
= fb_sys_read
,
1400 .fb_write
= fb_sys_write
,
1401 .fb_fillrect
= sh_mobile_lcdc_fillrect
,
1402 .fb_copyarea
= sh_mobile_lcdc_copyarea
,
1403 .fb_imageblit
= sh_mobile_lcdc_imageblit
,
1404 .fb_blank
= sh_mobile_lcdc_blank
,
1405 .fb_pan_display
= sh_mobile_fb_pan_display
,
1406 .fb_ioctl
= sh_mobile_ioctl
,
1407 .fb_open
= sh_mobile_open
,
1408 .fb_release
= sh_mobile_release
,
1409 .fb_check_var
= sh_mobile_check_var
,
1410 .fb_set_par
= sh_mobile_set_par
,
1414 sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan
*ch
)
1416 if (ch
->info
&& ch
->info
->dev
)
1417 unregister_framebuffer(ch
->info
);
1420 static int __devinit
1421 sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan
*ch
)
1423 struct fb_info
*info
= ch
->info
;
1426 if (info
->fbdefio
) {
1427 ch
->sglist
= vmalloc(sizeof(struct scatterlist
) *
1428 ch
->fb_size
>> PAGE_SHIFT
);
1430 dev_err(ch
->lcdc
->dev
, "cannot allocate sglist\n");
1435 info
->bl_dev
= ch
->bl
;
1437 ret
= register_framebuffer(info
);
1441 dev_info(ch
->lcdc
->dev
, "registered %s/%s as %dx%d %dbpp.\n",
1442 dev_name(ch
->lcdc
->dev
), (ch
->cfg
->chan
== LCDC_CHAN_MAINLCD
) ?
1443 "mainlcd" : "sublcd", info
->var
.xres
, info
->var
.yres
,
1444 info
->var
.bits_per_pixel
);
1446 /* deferred io mode: disable clock to save power */
1447 if (info
->fbdefio
|| info
->state
== FBINFO_STATE_SUSPENDED
)
1448 sh_mobile_lcdc_clk_off(ch
->lcdc
);
1454 sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan
*ch
)
1456 struct fb_info
*info
= ch
->info
;
1458 if (!info
|| !info
->device
)
1464 fb_dealloc_cmap(&info
->cmap
);
1465 framebuffer_release(info
);
1468 static int __devinit
1469 sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan
*ch
,
1470 const struct fb_videomode
*mode
,
1471 unsigned int num_modes
)
1473 struct sh_mobile_lcdc_priv
*priv
= ch
->lcdc
;
1474 struct fb_var_screeninfo
*var
;
1475 struct fb_info
*info
;
1478 /* Allocate and initialize the frame buffer device. Create the modes
1479 * list and allocate the color map.
1481 info
= framebuffer_alloc(0, priv
->dev
);
1483 dev_err(priv
->dev
, "unable to allocate fb_info\n");
1489 info
->flags
= FBINFO_FLAG_DEFAULT
;
1490 info
->fbops
= &sh_mobile_lcdc_ops
;
1491 info
->device
= priv
->dev
;
1492 info
->screen_base
= ch
->fb_mem
;
1493 info
->pseudo_palette
= &ch
->pseudo_palette
;
1496 fb_videomode_to_modelist(mode
, num_modes
, &info
->modelist
);
1498 ret
= fb_alloc_cmap(&info
->cmap
, PALETTE_NR
, 0);
1500 dev_err(priv
->dev
, "unable to allocate cmap\n");
1504 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1505 * for NV12 and NV21.
1507 info
->fix
= sh_mobile_lcdc_fix
;
1508 info
->fix
.smem_start
= ch
->dma_handle
;
1509 info
->fix
.smem_len
= ch
->fb_size
;
1510 info
->fix
.line_length
= ch
->pitch
;
1512 if (ch
->format
->yuv
)
1513 info
->fix
.visual
= FB_VISUAL_FOURCC
;
1515 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
1517 if (ch
->format
->fourcc
== V4L2_PIX_FMT_NV12
||
1518 ch
->format
->fourcc
== V4L2_PIX_FMT_NV21
)
1519 info
->fix
.ypanstep
= 2;
1521 /* Initialize variable screen information using the first mode as
1522 * default. The default Y virtual resolution is twice the panel size to
1523 * allow for double-buffering.
1526 fb_videomode_to_var(var
, mode
);
1527 var
->width
= ch
->cfg
->panel_cfg
.width
;
1528 var
->height
= ch
->cfg
->panel_cfg
.height
;
1529 var
->yres_virtual
= var
->yres
* 2;
1530 var
->activate
= FB_ACTIVATE_NOW
;
1532 /* Use the legacy API by default for RGB formats, and the FOURCC API
1535 if (!ch
->format
->yuv
)
1536 var
->bits_per_pixel
= ch
->format
->bpp
;
1538 var
->grayscale
= ch
->format
->fourcc
;
1540 ret
= sh_mobile_check_var(var
, info
);
1547 /* -----------------------------------------------------------------------------
1551 static int sh_mobile_lcdc_update_bl(struct backlight_device
*bdev
)
1553 struct sh_mobile_lcdc_chan
*ch
= bl_get_data(bdev
);
1554 int brightness
= bdev
->props
.brightness
;
1556 if (bdev
->props
.power
!= FB_BLANK_UNBLANK
||
1557 bdev
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
1560 return ch
->cfg
->bl_info
.set_brightness(brightness
);
1563 static int sh_mobile_lcdc_get_brightness(struct backlight_device
*bdev
)
1565 struct sh_mobile_lcdc_chan
*ch
= bl_get_data(bdev
);
1567 return ch
->cfg
->bl_info
.get_brightness();
1570 static int sh_mobile_lcdc_check_fb(struct backlight_device
*bdev
,
1571 struct fb_info
*info
)
1573 return (info
->bl_dev
== bdev
);
1576 static struct backlight_ops sh_mobile_lcdc_bl_ops
= {
1577 .options
= BL_CORE_SUSPENDRESUME
,
1578 .update_status
= sh_mobile_lcdc_update_bl
,
1579 .get_brightness
= sh_mobile_lcdc_get_brightness
,
1580 .check_fb
= sh_mobile_lcdc_check_fb
,
1583 static struct backlight_device
*sh_mobile_lcdc_bl_probe(struct device
*parent
,
1584 struct sh_mobile_lcdc_chan
*ch
)
1586 struct backlight_device
*bl
;
1588 bl
= backlight_device_register(ch
->cfg
->bl_info
.name
, parent
, ch
,
1589 &sh_mobile_lcdc_bl_ops
, NULL
);
1591 dev_err(parent
, "unable to register backlight device: %ld\n",
1596 bl
->props
.max_brightness
= ch
->cfg
->bl_info
.max_brightness
;
1597 bl
->props
.brightness
= bl
->props
.max_brightness
;
1598 backlight_update_status(bl
);
1603 static void sh_mobile_lcdc_bl_remove(struct backlight_device
*bdev
)
1605 backlight_device_unregister(bdev
);
1608 /* -----------------------------------------------------------------------------
1612 static int sh_mobile_lcdc_suspend(struct device
*dev
)
1614 struct platform_device
*pdev
= to_platform_device(dev
);
1616 sh_mobile_lcdc_stop(platform_get_drvdata(pdev
));
1620 static int sh_mobile_lcdc_resume(struct device
*dev
)
1622 struct platform_device
*pdev
= to_platform_device(dev
);
1624 return sh_mobile_lcdc_start(platform_get_drvdata(pdev
));
1627 static int sh_mobile_lcdc_runtime_suspend(struct device
*dev
)
1629 struct platform_device
*pdev
= to_platform_device(dev
);
1630 struct sh_mobile_lcdc_priv
*priv
= platform_get_drvdata(pdev
);
1632 /* turn off LCDC hardware */
1633 lcdc_write(priv
, _LDCNT1R
, 0);
1638 static int sh_mobile_lcdc_runtime_resume(struct device
*dev
)
1640 struct platform_device
*pdev
= to_platform_device(dev
);
1641 struct sh_mobile_lcdc_priv
*priv
= platform_get_drvdata(pdev
);
1643 __sh_mobile_lcdc_start(priv
);
1648 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops
= {
1649 .suspend
= sh_mobile_lcdc_suspend
,
1650 .resume
= sh_mobile_lcdc_resume
,
1651 .runtime_suspend
= sh_mobile_lcdc_runtime_suspend
,
1652 .runtime_resume
= sh_mobile_lcdc_runtime_resume
,
1655 /* -----------------------------------------------------------------------------
1656 * Framebuffer notifier
1659 /* locking: called with info->lock held */
1660 static int sh_mobile_lcdc_notify(struct notifier_block
*nb
,
1661 unsigned long action
, void *data
)
1663 struct fb_event
*event
= data
;
1664 struct fb_info
*info
= event
->info
;
1665 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1667 if (&ch
->lcdc
->notifier
!= nb
)
1670 dev_dbg(info
->dev
, "%s(): action = %lu, data = %p\n",
1671 __func__
, action
, event
->data
);
1674 case FB_EVENT_SUSPEND
:
1675 sh_mobile_lcdc_display_off(ch
);
1676 sh_mobile_lcdc_stop(ch
->lcdc
);
1678 case FB_EVENT_RESUME
:
1679 mutex_lock(&ch
->open_lock
);
1680 sh_mobile_fb_reconfig(info
);
1681 mutex_unlock(&ch
->open_lock
);
1683 sh_mobile_lcdc_display_on(ch
);
1684 sh_mobile_lcdc_start(ch
->lcdc
);
1690 /* -----------------------------------------------------------------------------
1691 * Probe/remove and driver init/exit
1694 static const struct fb_videomode default_720p __devinitconst
= {
1695 .name
= "HDMI 720p",
1700 .right_margin
= 110,
1709 .sync
= FB_SYNC_VERT_HIGH_ACT
| FB_SYNC_HOR_HIGH_ACT
,
1712 static int sh_mobile_lcdc_remove(struct platform_device
*pdev
)
1714 struct sh_mobile_lcdc_priv
*priv
= platform_get_drvdata(pdev
);
1717 fb_unregister_client(&priv
->notifier
);
1719 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++)
1720 sh_mobile_lcdc_channel_fb_unregister(&priv
->ch
[i
]);
1722 sh_mobile_lcdc_stop(priv
);
1724 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++) {
1725 struct sh_mobile_lcdc_chan
*ch
= &priv
->ch
[i
];
1728 ch
->tx_dev
->lcdc
= NULL
;
1729 module_put(ch
->cfg
->tx_dev
->dev
.driver
->owner
);
1732 sh_mobile_lcdc_channel_fb_cleanup(ch
);
1735 dma_free_coherent(&pdev
->dev
, ch
->fb_size
,
1736 ch
->fb_mem
, ch
->dma_handle
);
1739 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++) {
1741 sh_mobile_lcdc_bl_remove(priv
->ch
[i
].bl
);
1744 if (priv
->dot_clk
) {
1745 pm_runtime_disable(&pdev
->dev
);
1746 clk_put(priv
->dot_clk
);
1750 iounmap(priv
->base
);
1753 free_irq(priv
->irq
, priv
);
1758 static int __devinit
sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan
*ch
)
1760 int interface_type
= ch
->cfg
->interface_type
;
1762 switch (interface_type
) {
1786 /* SUBLCD only supports SYS interface */
1787 if (lcdc_chan_is_sublcd(ch
)) {
1788 if (!(interface_type
& LDMT1R_IFM
))
1791 interface_type
&= ~LDMT1R_IFM
;
1794 ch
->ldmt1r_value
= interface_type
;
1798 static int __devinit
1799 sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv
*priv
,
1800 struct sh_mobile_lcdc_chan
*ch
)
1802 const struct sh_mobile_lcdc_format_info
*format
;
1803 const struct sh_mobile_lcdc_chan_cfg
*cfg
= ch
->cfg
;
1804 const struct fb_videomode
*max_mode
;
1805 const struct fb_videomode
*mode
;
1806 unsigned int num_modes
;
1807 unsigned int max_size
;
1810 mutex_init(&ch
->open_lock
);
1811 ch
->notify
= sh_mobile_lcdc_display_notify
;
1813 /* Validate the format. */
1814 format
= sh_mobile_format_info(cfg
->fourcc
);
1815 if (format
== NULL
) {
1816 dev_err(priv
->dev
, "Invalid FOURCC %08x.\n", cfg
->fourcc
);
1820 /* Iterate through the modes to validate them and find the highest
1826 for (i
= 0, mode
= cfg
->lcd_modes
; i
< cfg
->num_modes
; i
++, mode
++) {
1827 unsigned int size
= mode
->yres
* mode
->xres
;
1829 /* NV12/NV21 buffers must have even number of lines */
1830 if ((cfg
->fourcc
== V4L2_PIX_FMT_NV12
||
1831 cfg
->fourcc
== V4L2_PIX_FMT_NV21
) && (mode
->yres
& 0x1)) {
1832 dev_err(priv
->dev
, "yres must be multiple of 2 for "
1833 "YCbCr420 mode.\n");
1837 if (size
> max_size
) {
1844 max_size
= MAX_XRES
* MAX_YRES
;
1846 dev_dbg(priv
->dev
, "Found largest videomode %ux%u\n",
1847 max_mode
->xres
, max_mode
->yres
);
1849 if (cfg
->lcd_modes
== NULL
) {
1850 mode
= &default_720p
;
1853 mode
= cfg
->lcd_modes
;
1854 num_modes
= cfg
->num_modes
;
1857 /* Use the first mode as default. */
1858 ch
->format
= format
;
1859 ch
->xres
= mode
->xres
;
1860 ch
->xres_virtual
= mode
->xres
;
1861 ch
->yres
= mode
->yres
;
1862 ch
->yres_virtual
= mode
->yres
* 2;
1865 ch
->colorspace
= V4L2_COLORSPACE_SRGB
;
1866 ch
->pitch
= ch
->xres
* format
->bpp
/ 8;
1868 ch
->colorspace
= V4L2_COLORSPACE_REC709
;
1869 ch
->pitch
= ch
->xres
;
1872 ch
->display
.width
= cfg
->panel_cfg
.width
;
1873 ch
->display
.height
= cfg
->panel_cfg
.height
;
1874 ch
->display
.mode
= *mode
;
1876 /* Allocate frame buffer memory. */
1877 ch
->fb_size
= max_size
* format
->bpp
/ 8 * 2;
1878 ch
->fb_mem
= dma_alloc_coherent(priv
->dev
, ch
->fb_size
, &ch
->dma_handle
,
1880 if (ch
->fb_mem
== NULL
) {
1881 dev_err(priv
->dev
, "unable to allocate buffer\n");
1885 /* Initialize the transmitter device if present. */
1887 if (!cfg
->tx_dev
->dev
.driver
||
1888 !try_module_get(cfg
->tx_dev
->dev
.driver
->owner
)) {
1890 "unable to get transmitter device\n");
1893 ch
->tx_dev
= platform_get_drvdata(cfg
->tx_dev
);
1894 ch
->tx_dev
->lcdc
= ch
;
1895 ch
->tx_dev
->def_mode
= *mode
;
1898 return sh_mobile_lcdc_channel_fb_init(ch
, mode
, num_modes
);
1901 static int __devinit
sh_mobile_lcdc_probe(struct platform_device
*pdev
)
1903 struct sh_mobile_lcdc_info
*pdata
= pdev
->dev
.platform_data
;
1904 struct sh_mobile_lcdc_priv
*priv
;
1905 struct resource
*res
;
1911 dev_err(&pdev
->dev
, "no platform data defined\n");
1915 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1916 i
= platform_get_irq(pdev
, 0);
1917 if (!res
|| i
< 0) {
1918 dev_err(&pdev
->dev
, "cannot get platform resources\n");
1922 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1924 dev_err(&pdev
->dev
, "cannot allocate device data\n");
1928 priv
->dev
= &pdev
->dev
;
1929 priv
->meram_dev
= pdata
->meram_dev
;
1930 platform_set_drvdata(pdev
, priv
);
1932 error
= request_irq(i
, sh_mobile_lcdc_irq
, 0,
1933 dev_name(&pdev
->dev
), priv
);
1935 dev_err(&pdev
->dev
, "unable to request irq\n");
1940 atomic_set(&priv
->hw_usecnt
, -1);
1942 for (i
= 0, num_channels
= 0; i
< ARRAY_SIZE(pdata
->ch
); i
++) {
1943 struct sh_mobile_lcdc_chan
*ch
= priv
->ch
+ num_channels
;
1946 ch
->cfg
= &pdata
->ch
[i
];
1948 error
= sh_mobile_lcdc_check_interface(ch
);
1950 dev_err(&pdev
->dev
, "unsupported interface type\n");
1953 init_waitqueue_head(&ch
->frame_end_wait
);
1954 init_completion(&ch
->vsync_completion
);
1957 /* probe the backlight is there is one defined */
1958 if (ch
->cfg
->bl_info
.max_brightness
)
1959 ch
->bl
= sh_mobile_lcdc_bl_probe(&pdev
->dev
, ch
);
1961 switch (pdata
->ch
[i
].chan
) {
1962 case LCDC_CHAN_MAINLCD
:
1963 ch
->enabled
= LDCNT2R_ME
;
1964 ch
->reg_offs
= lcdc_offs_mainlcd
;
1967 case LCDC_CHAN_SUBLCD
:
1968 ch
->enabled
= LDCNT2R_SE
;
1969 ch
->reg_offs
= lcdc_offs_sublcd
;
1975 if (!num_channels
) {
1976 dev_err(&pdev
->dev
, "no channels defined\n");
1981 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
1982 if (num_channels
== 2)
1983 priv
->forced_fourcc
= pdata
->ch
[0].fourcc
;
1985 priv
->base
= ioremap_nocache(res
->start
, resource_size(res
));
1989 error
= sh_mobile_lcdc_setup_clocks(priv
, pdata
->clock_source
);
1991 dev_err(&pdev
->dev
, "unable to setup clocks\n");
1995 /* Enable runtime PM. */
1996 pm_runtime_enable(&pdev
->dev
);
1998 for (i
= 0; i
< num_channels
; i
++) {
1999 struct sh_mobile_lcdc_chan
*ch
= priv
->ch
+ i
;
2001 error
= sh_mobile_lcdc_channel_init(priv
, ch
);
2006 error
= sh_mobile_lcdc_start(priv
);
2008 dev_err(&pdev
->dev
, "unable to start hardware\n");
2012 for (i
= 0; i
< num_channels
; i
++) {
2013 struct sh_mobile_lcdc_chan
*ch
= priv
->ch
+ i
;
2015 error
= sh_mobile_lcdc_channel_fb_register(ch
);
2020 /* Failure ignored */
2021 priv
->notifier
.notifier_call
= sh_mobile_lcdc_notify
;
2022 fb_register_client(&priv
->notifier
);
2026 sh_mobile_lcdc_remove(pdev
);
2031 static struct platform_driver sh_mobile_lcdc_driver
= {
2033 .name
= "sh_mobile_lcdc_fb",
2034 .owner
= THIS_MODULE
,
2035 .pm
= &sh_mobile_lcdc_dev_pm_ops
,
2037 .probe
= sh_mobile_lcdc_probe
,
2038 .remove
= sh_mobile_lcdc_remove
,
2041 module_platform_driver(sh_mobile_lcdc_driver
);
2043 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
2044 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
2045 MODULE_LICENSE("GPL v2");