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
= info
->fix
.smem_len
>> 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 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 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 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 static bool sh_mobile_format_is_yuv(const struct fb_var_screeninfo
*var
)
546 const struct sh_mobile_lcdc_format_info
*format
;
548 if (var
->grayscale
<= 1)
551 format
= sh_mobile_format_info(var
->grayscale
);
552 return format
? format
->yuv
: false;
555 /* -----------------------------------------------------------------------------
556 * Start, stop and IRQ
559 static irqreturn_t
sh_mobile_lcdc_irq(int irq
, void *data
)
561 struct sh_mobile_lcdc_priv
*priv
= data
;
562 struct sh_mobile_lcdc_chan
*ch
;
563 unsigned long ldintr
;
567 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
568 ldintr
= lcdc_read(priv
, _LDINTR
);
569 lcdc_write(priv
, _LDINTR
, (ldintr
^ LDINTR_STATUS_MASK
) & ~LDINTR_VEE
);
571 /* figure out if this interrupt is for main or sub lcd */
572 is_sub
= (lcdc_read(priv
, _LDSR
) & LDSR_MSS
) ? 1 : 0;
574 /* wake up channel and disable clocks */
575 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
582 if (ldintr
& LDINTR_FS
) {
583 if (is_sub
== lcdc_chan_is_sublcd(ch
)) {
585 wake_up(&ch
->frame_end_wait
);
587 sh_mobile_lcdc_clk_off(priv
);
592 if (ldintr
& LDINTR_VES
)
593 complete(&ch
->vsync_completion
);
599 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv
*priv
,
602 unsigned long tmp
= lcdc_read(priv
, _LDCNT2R
);
605 /* start or stop the lcdc */
607 lcdc_write(priv
, _LDCNT2R
, tmp
| LDCNT2R_DO
);
609 lcdc_write(priv
, _LDCNT2R
, tmp
& ~LDCNT2R_DO
);
611 /* wait until power is applied/stopped on all channels */
612 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++)
613 if (lcdc_read(priv
, _LDCNT2R
) & priv
->ch
[k
].enabled
)
615 tmp
= lcdc_read_chan(&priv
->ch
[k
], LDPMR
)
617 if (start
&& tmp
== LDPMR_LPS
)
619 if (!start
&& tmp
== 0)
625 lcdc_write(priv
, _LDDCKSTPR
, 1); /* stop dotclock */
628 static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan
*ch
)
630 const struct fb_var_screeninfo
*var
= &ch
->info
->var
;
631 const struct fb_videomode
*mode
= &ch
->display
.mode
;
632 unsigned long h_total
, hsync_pos
, display_h_total
;
635 tmp
= ch
->ldmt1r_value
;
636 tmp
|= (var
->sync
& FB_SYNC_VERT_HIGH_ACT
) ? 0 : LDMT1R_VPOL
;
637 tmp
|= (var
->sync
& FB_SYNC_HOR_HIGH_ACT
) ? 0 : LDMT1R_HPOL
;
638 tmp
|= (ch
->cfg
.flags
& LCDC_FLAGS_DWPOL
) ? LDMT1R_DWPOL
: 0;
639 tmp
|= (ch
->cfg
.flags
& LCDC_FLAGS_DIPOL
) ? LDMT1R_DIPOL
: 0;
640 tmp
|= (ch
->cfg
.flags
& LCDC_FLAGS_DAPOL
) ? LDMT1R_DAPOL
: 0;
641 tmp
|= (ch
->cfg
.flags
& LCDC_FLAGS_HSCNT
) ? LDMT1R_HSCNT
: 0;
642 tmp
|= (ch
->cfg
.flags
& LCDC_FLAGS_DWCNT
) ? LDMT1R_DWCNT
: 0;
643 lcdc_write_chan(ch
, LDMT1R
, tmp
);
646 lcdc_write_chan(ch
, LDMT2R
, ch
->cfg
.sys_bus_cfg
.ldmt2r
);
647 lcdc_write_chan(ch
, LDMT3R
, ch
->cfg
.sys_bus_cfg
.ldmt3r
);
649 /* horizontal configuration */
650 h_total
= mode
->xres
+ mode
->hsync_len
+ mode
->left_margin
651 + mode
->right_margin
;
652 tmp
= h_total
/ 8; /* HTCN */
653 tmp
|= (min(mode
->xres
, var
->xres
) / 8) << 16; /* HDCN */
654 lcdc_write_chan(ch
, LDHCNR
, tmp
);
656 hsync_pos
= mode
->xres
+ mode
->right_margin
;
657 tmp
= hsync_pos
/ 8; /* HSYNP */
658 tmp
|= (mode
->hsync_len
/ 8) << 16; /* HSYNW */
659 lcdc_write_chan(ch
, LDHSYNR
, tmp
);
661 /* vertical configuration */
662 tmp
= mode
->yres
+ mode
->vsync_len
+ mode
->upper_margin
663 + mode
->lower_margin
; /* VTLN */
664 tmp
|= min(mode
->yres
, var
->yres
) << 16; /* VDLN */
665 lcdc_write_chan(ch
, LDVLNR
, tmp
);
667 tmp
= mode
->yres
+ mode
->lower_margin
; /* VSYNP */
668 tmp
|= mode
->vsync_len
<< 16; /* VSYNW */
669 lcdc_write_chan(ch
, LDVSYNR
, tmp
);
671 /* Adjust horizontal synchronisation for HDMI */
672 display_h_total
= mode
->xres
+ mode
->hsync_len
+ mode
->left_margin
673 + mode
->right_margin
;
674 tmp
= ((mode
->xres
& 7) << 24) | ((display_h_total
& 7) << 16)
675 | ((mode
->hsync_len
& 7) << 8) | (hsync_pos
& 7);
676 lcdc_write_chan(ch
, LDHAJR
, tmp
);
680 * __sh_mobile_lcdc_start - Configure and tart the LCDC
683 * Configure all enabled channels and start the LCDC device. All external
684 * devices (clocks, MERAM, panels, ...) are not touched by this function.
686 static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv
*priv
)
688 struct sh_mobile_lcdc_chan
*ch
;
692 /* Enable LCDC channels. Read data from external memory, avoid using the
695 lcdc_write(priv
, _LDCNT2R
, priv
->ch
[0].enabled
| priv
->ch
[1].enabled
);
697 /* Stop the LCDC first and disable all interrupts. */
698 sh_mobile_lcdc_start_stop(priv
, 0);
699 lcdc_write(priv
, _LDINTR
, 0);
701 /* Configure power supply, dot clocks and start them. */
703 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
709 lcdc_write_chan(ch
, LDPMR
, 0);
711 m
= ch
->cfg
.clock_divider
;
715 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
718 lcdc_write_chan(ch
, LDDCKPAT1R
, 0);
719 lcdc_write_chan(ch
, LDDCKPAT2R
, (1 << (m
/2)) - 1);
723 tmp
|= m
<< (lcdc_chan_is_sublcd(ch
) ? 8 : 0);
726 lcdc_write(priv
, _LDDCKR
, tmp
);
727 lcdc_write(priv
, _LDDCKSTPR
, 0);
728 lcdc_wait_bit(priv
, _LDDCKSTPR
, ~0, 0);
730 /* Setup geometry, format, frame buffer memory and operation mode. */
731 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
736 sh_mobile_lcdc_geometry(ch
);
738 tmp
= ch
->format
->lddfr
;
740 if (ch
->format
->yuv
) {
741 switch (ch
->info
->var
.colorspace
) {
742 case V4L2_COLORSPACE_REC709
:
745 case V4L2_COLORSPACE_JPEG
:
751 lcdc_write_chan(ch
, LDDFR
, tmp
);
752 lcdc_write_chan(ch
, LDMLSR
, ch
->pitch
);
753 lcdc_write_chan(ch
, LDSA1R
, ch
->base_addr_y
);
755 lcdc_write_chan(ch
, LDSA2R
, ch
->base_addr_c
);
757 /* When using deferred I/O mode, configure the LCDC for one-shot
758 * operation and enable the frame end interrupt. Otherwise use
759 * continuous read mode.
761 if (ch
->ldmt1r_value
& LDMT1R_IFM
&&
762 ch
->cfg
.sys_bus_cfg
.deferred_io_msec
) {
763 lcdc_write_chan(ch
, LDSM1R
, LDSM1R_OS
);
764 lcdc_write(priv
, _LDINTR
, LDINTR_FE
);
766 lcdc_write_chan(ch
, LDSM1R
, 0);
770 /* Word and long word swap. */
771 switch (priv
->ch
[0].format
->fourcc
) {
772 case V4L2_PIX_FMT_RGB565
:
773 case V4L2_PIX_FMT_NV21
:
774 case V4L2_PIX_FMT_NV61
:
775 case V4L2_PIX_FMT_NV42
:
776 tmp
= LDDDSR_LS
| LDDDSR_WS
;
778 case V4L2_PIX_FMT_BGR24
:
779 case V4L2_PIX_FMT_NV12
:
780 case V4L2_PIX_FMT_NV16
:
781 case V4L2_PIX_FMT_NV24
:
782 tmp
= LDDDSR_LS
| LDDDSR_WS
| LDDDSR_BS
;
784 case V4L2_PIX_FMT_BGR32
:
789 lcdc_write(priv
, _LDDDSR
, tmp
);
791 /* Enable the display output. */
792 lcdc_write(priv
, _LDCNT1R
, LDCNT1R_DE
);
793 sh_mobile_lcdc_start_stop(priv
, 1);
797 static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv
*priv
)
799 struct sh_mobile_meram_info
*mdev
= priv
->meram_dev
;
800 struct sh_mobile_lcdc_chan
*ch
;
805 /* enable clocks before accessing the hardware */
806 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
807 if (priv
->ch
[k
].enabled
)
808 sh_mobile_lcdc_clk_on(priv
);
812 lcdc_write(priv
, _LDCNT2R
, lcdc_read(priv
, _LDCNT2R
) | LDCNT2R_BR
);
813 lcdc_wait_bit(priv
, _LDCNT2R
, LDCNT2R_BR
, 0);
815 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
816 struct sh_mobile_lcdc_panel_cfg
*panel
;
822 panel
= &ch
->cfg
.panel_cfg
;
823 if (panel
->setup_sys
) {
824 ret
= panel
->setup_sys(ch
, &sh_mobile_lcdc_sys_bus_ops
);
830 /* Compute frame buffer base address and pitch for each channel. */
831 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
832 struct sh_mobile_meram_cfg
*cfg
;
839 ch
->base_addr_y
= ch
->info
->fix
.smem_start
;
840 ch
->base_addr_c
= ch
->base_addr_y
842 * ch
->info
->var
.yres_virtual
;
843 ch
->pitch
= ch
->info
->fix
.line_length
;
845 /* Enable MERAM if possible. */
846 cfg
= ch
->cfg
.meram_cfg
;
847 if (mdev
== NULL
|| mdev
->ops
== NULL
|| cfg
== NULL
)
850 /* we need to de-init configured ICBs before we can
851 * re-initialize them.
853 if (ch
->meram_enabled
) {
854 mdev
->ops
->meram_unregister(mdev
, cfg
);
855 ch
->meram_enabled
= 0;
858 switch (ch
->format
->fourcc
) {
859 case V4L2_PIX_FMT_NV12
:
860 case V4L2_PIX_FMT_NV21
:
861 case V4L2_PIX_FMT_NV16
:
862 case V4L2_PIX_FMT_NV61
:
863 pixelformat
= SH_MOBILE_MERAM_PF_NV
;
865 case V4L2_PIX_FMT_NV24
:
866 case V4L2_PIX_FMT_NV42
:
867 pixelformat
= SH_MOBILE_MERAM_PF_NV24
;
869 case V4L2_PIX_FMT_RGB565
:
870 case V4L2_PIX_FMT_BGR24
:
871 case V4L2_PIX_FMT_BGR32
:
873 pixelformat
= SH_MOBILE_MERAM_PF_RGB
;
877 ret
= mdev
->ops
->meram_register(mdev
, cfg
, ch
->pitch
,
878 ch
->info
->var
.yres
, pixelformat
,
879 ch
->base_addr_y
, ch
->base_addr_c
,
880 &ch
->base_addr_y
, &ch
->base_addr_c
,
883 ch
->meram_enabled
= 1;
886 /* Start the LCDC. */
887 __sh_mobile_lcdc_start(priv
);
889 /* Setup deferred I/O, tell the board code to enable the panels, and
892 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
897 tmp
= ch
->cfg
.sys_bus_cfg
.deferred_io_msec
;
898 if (ch
->ldmt1r_value
& LDMT1R_IFM
&& tmp
) {
899 ch
->defio
.deferred_io
= sh_mobile_lcdc_deferred_io
;
900 ch
->defio
.delay
= msecs_to_jiffies(tmp
);
901 ch
->info
->fbdefio
= &ch
->defio
;
902 fb_deferred_io_init(ch
->info
);
905 sh_mobile_lcdc_display_on(ch
);
908 ch
->bl
->props
.power
= FB_BLANK_UNBLANK
;
909 backlight_update_status(ch
->bl
);
916 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv
*priv
)
918 struct sh_mobile_lcdc_chan
*ch
;
921 /* clean up deferred io and ask board code to disable panel */
922 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++) {
928 * flush frame, and wait for frame end interrupt
929 * clean up deferred io and enable clock
931 if (ch
->info
&& ch
->info
->fbdefio
) {
933 schedule_delayed_work(&ch
->info
->deferred_work
, 0);
934 wait_event(ch
->frame_end_wait
, ch
->frame_end
);
935 fb_deferred_io_cleanup(ch
->info
);
936 ch
->info
->fbdefio
= NULL
;
937 sh_mobile_lcdc_clk_on(priv
);
941 ch
->bl
->props
.power
= FB_BLANK_POWERDOWN
;
942 backlight_update_status(ch
->bl
);
945 sh_mobile_lcdc_display_off(ch
);
947 /* disable the meram */
948 if (ch
->meram_enabled
) {
949 struct sh_mobile_meram_cfg
*cfg
;
950 struct sh_mobile_meram_info
*mdev
;
951 cfg
= ch
->cfg
.meram_cfg
;
952 mdev
= priv
->meram_dev
;
953 mdev
->ops
->meram_unregister(mdev
, cfg
);
954 ch
->meram_enabled
= 0;
961 sh_mobile_lcdc_start_stop(priv
, 0);
966 for (k
= 0; k
< ARRAY_SIZE(priv
->ch
); k
++)
967 if (priv
->ch
[k
].enabled
)
968 sh_mobile_lcdc_clk_off(priv
);
971 /* -----------------------------------------------------------------------------
972 * Frame buffer operations
975 static int sh_mobile_lcdc_setcolreg(u_int regno
,
976 u_int red
, u_int green
, u_int blue
,
977 u_int transp
, struct fb_info
*info
)
979 u32
*palette
= info
->pseudo_palette
;
981 if (regno
>= PALETTE_NR
)
984 /* only FB_VISUAL_TRUECOLOR supported */
986 red
>>= 16 - info
->var
.red
.length
;
987 green
>>= 16 - info
->var
.green
.length
;
988 blue
>>= 16 - info
->var
.blue
.length
;
989 transp
>>= 16 - info
->var
.transp
.length
;
991 palette
[regno
] = (red
<< info
->var
.red
.offset
) |
992 (green
<< info
->var
.green
.offset
) |
993 (blue
<< info
->var
.blue
.offset
) |
994 (transp
<< info
->var
.transp
.offset
);
999 static struct fb_fix_screeninfo sh_mobile_lcdc_fix
= {
1000 .id
= "SH Mobile LCDC",
1001 .type
= FB_TYPE_PACKED_PIXELS
,
1002 .visual
= FB_VISUAL_TRUECOLOR
,
1003 .accel
= FB_ACCEL_NONE
,
1007 .capabilities
= FB_CAP_FOURCC
,
1010 static void sh_mobile_lcdc_fillrect(struct fb_info
*info
,
1011 const struct fb_fillrect
*rect
)
1013 sys_fillrect(info
, rect
);
1014 sh_mobile_lcdc_deferred_io_touch(info
);
1017 static void sh_mobile_lcdc_copyarea(struct fb_info
*info
,
1018 const struct fb_copyarea
*area
)
1020 sys_copyarea(info
, area
);
1021 sh_mobile_lcdc_deferred_io_touch(info
);
1024 static void sh_mobile_lcdc_imageblit(struct fb_info
*info
,
1025 const struct fb_image
*image
)
1027 sys_imageblit(info
, image
);
1028 sh_mobile_lcdc_deferred_io_touch(info
);
1031 static int sh_mobile_fb_pan_display(struct fb_var_screeninfo
*var
,
1032 struct fb_info
*info
)
1034 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1035 struct sh_mobile_lcdc_priv
*priv
= ch
->lcdc
;
1036 unsigned long ldrcntr
;
1037 unsigned long new_pan_offset
;
1038 unsigned long base_addr_y
, base_addr_c
;
1039 unsigned long c_offset
;
1040 bool yuv
= sh_mobile_format_is_yuv(&info
->var
);
1043 new_pan_offset
= var
->yoffset
* info
->fix
.line_length
1044 + var
->xoffset
* (info
->var
.bits_per_pixel
/ 8);
1046 new_pan_offset
= var
->yoffset
* info
->fix
.line_length
1049 if (new_pan_offset
== ch
->pan_offset
)
1050 return 0; /* No change, do nothing */
1052 ldrcntr
= lcdc_read(priv
, _LDRCNTR
);
1054 /* Set the source address for the next refresh */
1055 base_addr_y
= ch
->dma_handle
+ new_pan_offset
;
1058 c_offset
= var
->yoffset
* info
->fix
.line_length
1059 * (info
->var
.bits_per_pixel
- 8) / 8;
1060 base_addr_c
= ch
->dma_handle
1061 + info
->var
.xres
* info
->var
.yres_virtual
1064 if (ch
->format
->fourcc
== V4L2_PIX_FMT_NV24
)
1065 base_addr_c
+= 2 * var
->xoffset
;
1067 base_addr_c
+= var
->xoffset
;
1070 if (ch
->meram_enabled
) {
1071 struct sh_mobile_meram_cfg
*cfg
;
1072 struct sh_mobile_meram_info
*mdev
;
1075 cfg
= ch
->cfg
.meram_cfg
;
1076 mdev
= priv
->meram_dev
;
1077 ret
= mdev
->ops
->meram_update(mdev
, cfg
,
1078 base_addr_y
, base_addr_c
,
1079 &base_addr_y
, &base_addr_c
);
1084 ch
->base_addr_y
= base_addr_y
;
1085 ch
->base_addr_c
= base_addr_c
;
1087 lcdc_write_chan_mirror(ch
, LDSA1R
, base_addr_y
);
1089 lcdc_write_chan_mirror(ch
, LDSA2R
, base_addr_c
);
1091 if (lcdc_chan_is_sublcd(ch
))
1092 lcdc_write(ch
->lcdc
, _LDRCNTR
, ldrcntr
^ LDRCNTR_SRS
);
1094 lcdc_write(ch
->lcdc
, _LDRCNTR
, ldrcntr
^ LDRCNTR_MRS
);
1096 ch
->pan_offset
= new_pan_offset
;
1098 sh_mobile_lcdc_deferred_io_touch(info
);
1103 static int sh_mobile_wait_for_vsync(struct fb_info
*info
)
1105 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1106 unsigned long ldintr
;
1109 /* Enable VSync End interrupt and be careful not to acknowledge any
1110 * pending interrupt.
1112 ldintr
= lcdc_read(ch
->lcdc
, _LDINTR
);
1113 ldintr
|= LDINTR_VEE
| LDINTR_STATUS_MASK
;
1114 lcdc_write(ch
->lcdc
, _LDINTR
, ldintr
);
1116 ret
= wait_for_completion_interruptible_timeout(&ch
->vsync_completion
,
1117 msecs_to_jiffies(100));
1124 static int sh_mobile_ioctl(struct fb_info
*info
, unsigned int cmd
,
1130 case FBIO_WAITFORVSYNC
:
1131 retval
= sh_mobile_wait_for_vsync(info
);
1135 retval
= -ENOIOCTLCMD
;
1141 static void sh_mobile_fb_reconfig(struct fb_info
*info
)
1143 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1144 struct fb_var_screeninfo var
;
1145 struct fb_videomode mode
;
1146 struct fb_event event
;
1147 int evnt
= FB_EVENT_MODE_CHANGE_ALL
;
1149 if (ch
->use_count
> 1 || (ch
->use_count
== 1 && !info
->fbcon_par
))
1150 /* More framebuffer users are active */
1153 fb_var_to_videomode(&mode
, &info
->var
);
1155 if (fb_mode_is_equal(&ch
->display
.mode
, &mode
))
1158 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1160 fb_videomode_to_var(&var
, &ch
->display
.mode
);
1161 var
.width
= ch
->display
.width
;
1162 var
.height
= ch
->display
.height
;
1163 var
.activate
= FB_ACTIVATE_NOW
;
1165 if (fb_set_var(info
, &var
) < 0)
1166 /* Couldn't reconfigure, hopefully, can continue as before */
1170 * fb_set_var() calls the notifier change internally, only if
1171 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1172 * user event, we have to call the chain ourselves.
1175 event
.data
= &ch
->display
.mode
;
1176 fb_notifier_call_chain(evnt
, &event
);
1180 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1181 * user == 1, or with console sem held, if user == 0.
1183 static int sh_mobile_release(struct fb_info
*info
, int user
)
1185 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1187 mutex_lock(&ch
->open_lock
);
1188 dev_dbg(info
->dev
, "%s(): %d users\n", __func__
, ch
->use_count
);
1192 /* Nothing to reconfigure, when called from fbcon */
1195 sh_mobile_fb_reconfig(info
);
1199 mutex_unlock(&ch
->open_lock
);
1204 static int sh_mobile_open(struct fb_info
*info
, int user
)
1206 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1208 mutex_lock(&ch
->open_lock
);
1211 dev_dbg(info
->dev
, "%s(): %d users\n", __func__
, ch
->use_count
);
1212 mutex_unlock(&ch
->open_lock
);
1217 static int sh_mobile_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
1219 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1220 struct sh_mobile_lcdc_priv
*p
= ch
->lcdc
;
1221 unsigned int best_dist
= (unsigned int)-1;
1222 unsigned int best_xres
= 0;
1223 unsigned int best_yres
= 0;
1226 if (var
->xres
> MAX_XRES
|| var
->yres
> MAX_YRES
)
1229 /* If board code provides us with a list of available modes, make sure
1230 * we use one of them. Find the mode closest to the requested one. The
1231 * distance between two modes is defined as the size of the
1232 * non-overlapping parts of the two rectangles.
1234 for (i
= 0; i
< ch
->cfg
.num_modes
; ++i
) {
1235 const struct fb_videomode
*mode
= &ch
->cfg
.lcd_modes
[i
];
1238 /* We can only round up. */
1239 if (var
->xres
> mode
->xres
|| var
->yres
> mode
->yres
)
1242 dist
= var
->xres
* var
->yres
+ mode
->xres
* mode
->yres
1243 - 2 * min(var
->xres
, mode
->xres
)
1244 * min(var
->yres
, mode
->yres
);
1246 if (dist
< best_dist
) {
1247 best_xres
= mode
->xres
;
1248 best_yres
= mode
->yres
;
1253 /* If no available mode can be used, return an error. */
1254 if (ch
->cfg
.num_modes
!= 0) {
1255 if (best_dist
== (unsigned int)-1)
1258 var
->xres
= best_xres
;
1259 var
->yres
= best_yres
;
1262 /* Make sure the virtual resolution is at least as big as the visible
1265 if (var
->xres_virtual
< var
->xres
)
1266 var
->xres_virtual
= var
->xres
;
1267 if (var
->yres_virtual
< var
->yres
)
1268 var
->yres_virtual
= var
->yres
;
1270 if (sh_mobile_format_is_fourcc(var
)) {
1271 const struct sh_mobile_lcdc_format_info
*format
;
1273 format
= sh_mobile_format_info(var
->grayscale
);
1276 var
->bits_per_pixel
= format
->bpp
;
1278 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1282 var
->colorspace
= V4L2_COLORSPACE_SRGB
;
1283 else if (var
->colorspace
!= V4L2_COLORSPACE_REC709
)
1284 var
->colorspace
= V4L2_COLORSPACE_JPEG
;
1286 if (var
->bits_per_pixel
<= 16) { /* RGB 565 */
1287 var
->bits_per_pixel
= 16;
1288 var
->red
.offset
= 11;
1289 var
->red
.length
= 5;
1290 var
->green
.offset
= 5;
1291 var
->green
.length
= 6;
1292 var
->blue
.offset
= 0;
1293 var
->blue
.length
= 5;
1294 var
->transp
.offset
= 0;
1295 var
->transp
.length
= 0;
1296 } else if (var
->bits_per_pixel
<= 24) { /* RGB 888 */
1297 var
->bits_per_pixel
= 24;
1298 var
->red
.offset
= 16;
1299 var
->red
.length
= 8;
1300 var
->green
.offset
= 8;
1301 var
->green
.length
= 8;
1302 var
->blue
.offset
= 0;
1303 var
->blue
.length
= 8;
1304 var
->transp
.offset
= 0;
1305 var
->transp
.length
= 0;
1306 } else if (var
->bits_per_pixel
<= 32) { /* RGBA 888 */
1307 var
->bits_per_pixel
= 32;
1308 var
->red
.offset
= 16;
1309 var
->red
.length
= 8;
1310 var
->green
.offset
= 8;
1311 var
->green
.length
= 8;
1312 var
->blue
.offset
= 0;
1313 var
->blue
.length
= 8;
1314 var
->transp
.offset
= 24;
1315 var
->transp
.length
= 8;
1319 var
->red
.msb_right
= 0;
1320 var
->green
.msb_right
= 0;
1321 var
->blue
.msb_right
= 0;
1322 var
->transp
.msb_right
= 0;
1325 /* Make sure we don't exceed our allocated memory. */
1326 if (var
->xres_virtual
* var
->yres_virtual
* var
->bits_per_pixel
/ 8 >
1330 /* only accept the forced_fourcc for dual channel configurations */
1331 if (p
->forced_fourcc
&&
1332 p
->forced_fourcc
!= sh_mobile_format_fourcc(var
))
1338 static int sh_mobile_set_par(struct fb_info
*info
)
1340 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1341 u32 line_length
= info
->fix
.line_length
;
1344 sh_mobile_lcdc_stop(ch
->lcdc
);
1346 if (sh_mobile_format_is_yuv(&info
->var
))
1347 info
->fix
.line_length
= info
->var
.xres
;
1349 info
->fix
.line_length
= info
->var
.xres
1350 * info
->var
.bits_per_pixel
/ 8;
1352 ch
->format
= sh_mobile_format_info(sh_mobile_format_fourcc(&info
->var
));
1354 ret
= sh_mobile_lcdc_start(ch
->lcdc
);
1356 dev_err(info
->dev
, "%s: unable to restart LCDC\n", __func__
);
1357 info
->fix
.line_length
= line_length
;
1360 if (sh_mobile_format_is_fourcc(&info
->var
)) {
1361 info
->fix
.type
= FB_TYPE_FOURCC
;
1362 info
->fix
.visual
= FB_VISUAL_FOURCC
;
1364 info
->fix
.type
= FB_TYPE_PACKED_PIXELS
;
1365 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
1372 * Screen blanking. Behavior is as follows:
1373 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1374 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1377 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1379 static int sh_mobile_lcdc_blank(int blank
, struct fb_info
*info
)
1381 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1382 struct sh_mobile_lcdc_priv
*p
= ch
->lcdc
;
1384 /* blank the screen? */
1385 if (blank
> FB_BLANK_UNBLANK
&& ch
->blank_status
== FB_BLANK_UNBLANK
) {
1386 struct fb_fillrect rect
= {
1387 .width
= info
->var
.xres
,
1388 .height
= info
->var
.yres
,
1390 sh_mobile_lcdc_fillrect(info
, &rect
);
1392 /* turn clocks on? */
1393 if (blank
<= FB_BLANK_NORMAL
&& ch
->blank_status
> FB_BLANK_NORMAL
) {
1394 sh_mobile_lcdc_clk_on(p
);
1396 /* turn clocks off? */
1397 if (blank
> FB_BLANK_NORMAL
&& ch
->blank_status
<= FB_BLANK_NORMAL
) {
1398 /* make sure the screen is updated with the black fill before
1399 * switching the clocks off. one vsync is not enough since
1400 * blanking may occur in the middle of a refresh. deferred io
1401 * mode will reenable the clocks and update the screen in time,
1402 * so it does not need this. */
1403 if (!info
->fbdefio
) {
1404 sh_mobile_wait_for_vsync(info
);
1405 sh_mobile_wait_for_vsync(info
);
1407 sh_mobile_lcdc_clk_off(p
);
1410 ch
->blank_status
= blank
;
1414 static struct fb_ops sh_mobile_lcdc_ops
= {
1415 .owner
= THIS_MODULE
,
1416 .fb_setcolreg
= sh_mobile_lcdc_setcolreg
,
1417 .fb_read
= fb_sys_read
,
1418 .fb_write
= fb_sys_write
,
1419 .fb_fillrect
= sh_mobile_lcdc_fillrect
,
1420 .fb_copyarea
= sh_mobile_lcdc_copyarea
,
1421 .fb_imageblit
= sh_mobile_lcdc_imageblit
,
1422 .fb_blank
= sh_mobile_lcdc_blank
,
1423 .fb_pan_display
= sh_mobile_fb_pan_display
,
1424 .fb_ioctl
= sh_mobile_ioctl
,
1425 .fb_open
= sh_mobile_open
,
1426 .fb_release
= sh_mobile_release
,
1427 .fb_check_var
= sh_mobile_check_var
,
1428 .fb_set_par
= sh_mobile_set_par
,
1432 sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan
*ch
)
1434 if (ch
->info
&& ch
->info
->dev
)
1435 unregister_framebuffer(ch
->info
);
1438 static int __devinit
1439 sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan
*ch
)
1441 struct fb_info
*info
= ch
->info
;
1444 if (info
->fbdefio
) {
1445 ch
->sglist
= vmalloc(sizeof(struct scatterlist
) *
1446 ch
->fb_size
>> PAGE_SHIFT
);
1448 dev_err(ch
->lcdc
->dev
, "cannot allocate sglist\n");
1453 info
->bl_dev
= ch
->bl
;
1455 ret
= register_framebuffer(info
);
1459 dev_info(ch
->lcdc
->dev
, "registered %s/%s as %dx%d %dbpp.\n",
1460 dev_name(ch
->lcdc
->dev
), (ch
->cfg
.chan
== LCDC_CHAN_MAINLCD
) ?
1461 "mainlcd" : "sublcd", info
->var
.xres
, info
->var
.yres
,
1462 info
->var
.bits_per_pixel
);
1464 /* deferred io mode: disable clock to save power */
1465 if (info
->fbdefio
|| info
->state
== FBINFO_STATE_SUSPENDED
)
1466 sh_mobile_lcdc_clk_off(ch
->lcdc
);
1472 sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan
*ch
)
1474 struct fb_info
*info
= ch
->info
;
1476 if (!info
|| !info
->device
)
1482 fb_dealloc_cmap(&info
->cmap
);
1483 framebuffer_release(info
);
1486 static int __devinit
1487 sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan
*ch
,
1488 const struct fb_videomode
*mode
,
1489 unsigned int num_modes
)
1491 struct sh_mobile_lcdc_priv
*priv
= ch
->lcdc
;
1492 struct fb_var_screeninfo
*var
;
1493 struct fb_info
*info
;
1496 /* Allocate and initialize the frame buffer device. Create the modes
1497 * list and allocate the color map.
1499 info
= framebuffer_alloc(0, priv
->dev
);
1501 dev_err(priv
->dev
, "unable to allocate fb_info\n");
1507 info
->flags
= FBINFO_FLAG_DEFAULT
;
1508 info
->fbops
= &sh_mobile_lcdc_ops
;
1509 info
->device
= priv
->dev
;
1510 info
->screen_base
= ch
->fb_mem
;
1511 info
->pseudo_palette
= &ch
->pseudo_palette
;
1514 fb_videomode_to_modelist(mode
, num_modes
, &info
->modelist
);
1516 ret
= fb_alloc_cmap(&info
->cmap
, PALETTE_NR
, 0);
1518 dev_err(priv
->dev
, "unable to allocate cmap\n");
1522 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1523 * for NV12 and NV21.
1525 info
->fix
= sh_mobile_lcdc_fix
;
1526 info
->fix
.smem_start
= ch
->dma_handle
;
1527 info
->fix
.smem_len
= ch
->fb_size
;
1528 if (ch
->format
->fourcc
== V4L2_PIX_FMT_NV12
||
1529 ch
->format
->fourcc
== V4L2_PIX_FMT_NV21
)
1530 info
->fix
.ypanstep
= 2;
1532 /* Initialize variable screen information using the first mode as
1533 * default. The default Y virtual resolution is twice the panel size to
1534 * allow for double-buffering.
1537 fb_videomode_to_var(var
, mode
);
1538 var
->width
= ch
->cfg
.panel_cfg
.width
;
1539 var
->height
= ch
->cfg
.panel_cfg
.height
;
1540 var
->yres_virtual
= var
->yres
* 2;
1541 var
->activate
= FB_ACTIVATE_NOW
;
1543 /* Use the legacy API by default for RGB formats, and the FOURCC API
1546 if (!ch
->format
->yuv
)
1547 var
->bits_per_pixel
= ch
->format
->bpp
;
1549 var
->grayscale
= ch
->format
->fourcc
;
1551 ret
= sh_mobile_check_var(var
, info
);
1555 if (ch
->format
->yuv
) {
1556 info
->fix
.line_length
= var
->xres
;
1557 info
->fix
.visual
= FB_VISUAL_FOURCC
;
1559 info
->fix
.line_length
= var
->xres
* ch
->format
->bpp
/ 8;
1560 info
->fix
.visual
= FB_VISUAL_TRUECOLOR
;
1566 /* -----------------------------------------------------------------------------
1570 static int sh_mobile_lcdc_update_bl(struct backlight_device
*bdev
)
1572 struct sh_mobile_lcdc_chan
*ch
= bl_get_data(bdev
);
1573 int brightness
= bdev
->props
.brightness
;
1575 if (bdev
->props
.power
!= FB_BLANK_UNBLANK
||
1576 bdev
->props
.state
& (BL_CORE_SUSPENDED
| BL_CORE_FBBLANK
))
1579 return ch
->cfg
.bl_info
.set_brightness(brightness
);
1582 static int sh_mobile_lcdc_get_brightness(struct backlight_device
*bdev
)
1584 struct sh_mobile_lcdc_chan
*ch
= bl_get_data(bdev
);
1586 return ch
->cfg
.bl_info
.get_brightness();
1589 static int sh_mobile_lcdc_check_fb(struct backlight_device
*bdev
,
1590 struct fb_info
*info
)
1592 return (info
->bl_dev
== bdev
);
1595 static struct backlight_ops sh_mobile_lcdc_bl_ops
= {
1596 .options
= BL_CORE_SUSPENDRESUME
,
1597 .update_status
= sh_mobile_lcdc_update_bl
,
1598 .get_brightness
= sh_mobile_lcdc_get_brightness
,
1599 .check_fb
= sh_mobile_lcdc_check_fb
,
1602 static struct backlight_device
*sh_mobile_lcdc_bl_probe(struct device
*parent
,
1603 struct sh_mobile_lcdc_chan
*ch
)
1605 struct backlight_device
*bl
;
1607 bl
= backlight_device_register(ch
->cfg
.bl_info
.name
, parent
, ch
,
1608 &sh_mobile_lcdc_bl_ops
, NULL
);
1610 dev_err(parent
, "unable to register backlight device: %ld\n",
1615 bl
->props
.max_brightness
= ch
->cfg
.bl_info
.max_brightness
;
1616 bl
->props
.brightness
= bl
->props
.max_brightness
;
1617 backlight_update_status(bl
);
1622 static void sh_mobile_lcdc_bl_remove(struct backlight_device
*bdev
)
1624 backlight_device_unregister(bdev
);
1627 /* -----------------------------------------------------------------------------
1631 static int sh_mobile_lcdc_suspend(struct device
*dev
)
1633 struct platform_device
*pdev
= to_platform_device(dev
);
1635 sh_mobile_lcdc_stop(platform_get_drvdata(pdev
));
1639 static int sh_mobile_lcdc_resume(struct device
*dev
)
1641 struct platform_device
*pdev
= to_platform_device(dev
);
1643 return sh_mobile_lcdc_start(platform_get_drvdata(pdev
));
1646 static int sh_mobile_lcdc_runtime_suspend(struct device
*dev
)
1648 struct platform_device
*pdev
= to_platform_device(dev
);
1649 struct sh_mobile_lcdc_priv
*priv
= platform_get_drvdata(pdev
);
1651 /* turn off LCDC hardware */
1652 lcdc_write(priv
, _LDCNT1R
, 0);
1657 static int sh_mobile_lcdc_runtime_resume(struct device
*dev
)
1659 struct platform_device
*pdev
= to_platform_device(dev
);
1660 struct sh_mobile_lcdc_priv
*priv
= platform_get_drvdata(pdev
);
1662 __sh_mobile_lcdc_start(priv
);
1667 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops
= {
1668 .suspend
= sh_mobile_lcdc_suspend
,
1669 .resume
= sh_mobile_lcdc_resume
,
1670 .runtime_suspend
= sh_mobile_lcdc_runtime_suspend
,
1671 .runtime_resume
= sh_mobile_lcdc_runtime_resume
,
1674 /* -----------------------------------------------------------------------------
1675 * Framebuffer notifier
1678 /* locking: called with info->lock held */
1679 static int sh_mobile_lcdc_notify(struct notifier_block
*nb
,
1680 unsigned long action
, void *data
)
1682 struct fb_event
*event
= data
;
1683 struct fb_info
*info
= event
->info
;
1684 struct sh_mobile_lcdc_chan
*ch
= info
->par
;
1686 if (&ch
->lcdc
->notifier
!= nb
)
1689 dev_dbg(info
->dev
, "%s(): action = %lu, data = %p\n",
1690 __func__
, action
, event
->data
);
1693 case FB_EVENT_SUSPEND
:
1694 sh_mobile_lcdc_display_off(ch
);
1695 sh_mobile_lcdc_stop(ch
->lcdc
);
1697 case FB_EVENT_RESUME
:
1698 mutex_lock(&ch
->open_lock
);
1699 sh_mobile_fb_reconfig(info
);
1700 mutex_unlock(&ch
->open_lock
);
1702 sh_mobile_lcdc_display_on(ch
);
1703 sh_mobile_lcdc_start(ch
->lcdc
);
1709 /* -----------------------------------------------------------------------------
1710 * Probe/remove and driver init/exit
1713 static const struct fb_videomode default_720p __devinitconst
= {
1714 .name
= "HDMI 720p",
1719 .right_margin
= 110,
1728 .sync
= FB_SYNC_VERT_HIGH_ACT
| FB_SYNC_HOR_HIGH_ACT
,
1731 static int sh_mobile_lcdc_remove(struct platform_device
*pdev
)
1733 struct sh_mobile_lcdc_priv
*priv
= platform_get_drvdata(pdev
);
1736 fb_unregister_client(&priv
->notifier
);
1738 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++)
1739 sh_mobile_lcdc_channel_fb_unregister(&priv
->ch
[i
]);
1741 sh_mobile_lcdc_stop(priv
);
1743 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++) {
1744 struct sh_mobile_lcdc_chan
*ch
= &priv
->ch
[i
];
1747 ch
->tx_dev
->lcdc
= NULL
;
1748 module_put(ch
->cfg
.tx_dev
->dev
.driver
->owner
);
1751 sh_mobile_lcdc_channel_fb_cleanup(ch
);
1754 dma_free_coherent(&pdev
->dev
, ch
->fb_size
,
1755 ch
->fb_mem
, ch
->dma_handle
);
1758 for (i
= 0; i
< ARRAY_SIZE(priv
->ch
); i
++) {
1760 sh_mobile_lcdc_bl_remove(priv
->ch
[i
].bl
);
1763 if (priv
->dot_clk
) {
1764 pm_runtime_disable(&pdev
->dev
);
1765 clk_put(priv
->dot_clk
);
1769 iounmap(priv
->base
);
1772 free_irq(priv
->irq
, priv
);
1777 static int __devinit
sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan
*ch
)
1779 int interface_type
= ch
->cfg
.interface_type
;
1781 switch (interface_type
) {
1805 /* SUBLCD only supports SYS interface */
1806 if (lcdc_chan_is_sublcd(ch
)) {
1807 if (!(interface_type
& LDMT1R_IFM
))
1810 interface_type
&= ~LDMT1R_IFM
;
1813 ch
->ldmt1r_value
= interface_type
;
1817 static int __devinit
1818 sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv
*priv
,
1819 struct sh_mobile_lcdc_chan
*ch
)
1821 const struct sh_mobile_lcdc_format_info
*format
;
1822 struct sh_mobile_lcdc_chan_cfg
*cfg
= &ch
->cfg
;
1823 const struct fb_videomode
*max_mode
;
1824 const struct fb_videomode
*mode
;
1825 unsigned int num_modes
;
1826 unsigned int max_size
;
1829 mutex_init(&ch
->open_lock
);
1830 ch
->notify
= sh_mobile_lcdc_display_notify
;
1832 /* Validate the format. */
1833 format
= sh_mobile_format_info(cfg
->fourcc
);
1834 if (format
== NULL
) {
1835 dev_err(priv
->dev
, "Invalid FOURCC %08x.\n", cfg
->fourcc
);
1839 ch
->format
= format
;
1841 /* Iterate through the modes to validate them and find the highest
1847 for (i
= 0, mode
= cfg
->lcd_modes
; i
< cfg
->num_modes
; i
++, mode
++) {
1848 unsigned int size
= mode
->yres
* mode
->xres
;
1850 /* NV12/NV21 buffers must have even number of lines */
1851 if ((cfg
->fourcc
== V4L2_PIX_FMT_NV12
||
1852 cfg
->fourcc
== V4L2_PIX_FMT_NV21
) && (mode
->yres
& 0x1)) {
1853 dev_err(priv
->dev
, "yres must be multiple of 2 for "
1854 "YCbCr420 mode.\n");
1858 if (size
> max_size
) {
1865 max_size
= MAX_XRES
* MAX_YRES
;
1867 dev_dbg(priv
->dev
, "Found largest videomode %ux%u\n",
1868 max_mode
->xres
, max_mode
->yres
);
1870 if (cfg
->lcd_modes
== NULL
) {
1871 mode
= &default_720p
;
1874 mode
= cfg
->lcd_modes
;
1875 num_modes
= cfg
->num_modes
;
1878 ch
->display
.width
= cfg
->panel_cfg
.width
;
1879 ch
->display
.height
= cfg
->panel_cfg
.height
;
1880 ch
->display
.mode
= *mode
;
1882 /* Allocate frame buffer memory. */
1883 ch
->fb_size
= max_size
* format
->bpp
/ 8 * 2;
1884 ch
->fb_mem
= dma_alloc_coherent(priv
->dev
, ch
->fb_size
, &ch
->dma_handle
,
1886 if (ch
->fb_mem
== NULL
) {
1887 dev_err(priv
->dev
, "unable to allocate buffer\n");
1891 /* Initialize the transmitter device if present. */
1893 if (!cfg
->tx_dev
->dev
.driver
||
1894 !try_module_get(cfg
->tx_dev
->dev
.driver
->owner
)) {
1896 "unable to get transmitter device\n");
1899 ch
->tx_dev
= platform_get_drvdata(cfg
->tx_dev
);
1900 ch
->tx_dev
->lcdc
= ch
;
1901 ch
->tx_dev
->def_mode
= *mode
;
1904 return sh_mobile_lcdc_channel_fb_init(ch
, mode
, num_modes
);
1907 static int __devinit
sh_mobile_lcdc_probe(struct platform_device
*pdev
)
1909 struct sh_mobile_lcdc_info
*pdata
= pdev
->dev
.platform_data
;
1910 struct sh_mobile_lcdc_priv
*priv
;
1911 struct resource
*res
;
1917 dev_err(&pdev
->dev
, "no platform data defined\n");
1921 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1922 i
= platform_get_irq(pdev
, 0);
1923 if (!res
|| i
< 0) {
1924 dev_err(&pdev
->dev
, "cannot get platform resources\n");
1928 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1930 dev_err(&pdev
->dev
, "cannot allocate device data\n");
1934 priv
->dev
= &pdev
->dev
;
1935 priv
->meram_dev
= pdata
->meram_dev
;
1936 platform_set_drvdata(pdev
, priv
);
1938 error
= request_irq(i
, sh_mobile_lcdc_irq
, 0,
1939 dev_name(&pdev
->dev
), priv
);
1941 dev_err(&pdev
->dev
, "unable to request irq\n");
1946 atomic_set(&priv
->hw_usecnt
, -1);
1948 for (i
= 0, num_channels
= 0; i
< ARRAY_SIZE(pdata
->ch
); i
++) {
1949 struct sh_mobile_lcdc_chan
*ch
= priv
->ch
+ num_channels
;
1952 memcpy(&ch
->cfg
, &pdata
->ch
[i
], sizeof(pdata
->ch
[i
]));
1954 error
= sh_mobile_lcdc_check_interface(ch
);
1956 dev_err(&pdev
->dev
, "unsupported interface type\n");
1959 init_waitqueue_head(&ch
->frame_end_wait
);
1960 init_completion(&ch
->vsync_completion
);
1963 /* probe the backlight is there is one defined */
1964 if (ch
->cfg
.bl_info
.max_brightness
)
1965 ch
->bl
= sh_mobile_lcdc_bl_probe(&pdev
->dev
, ch
);
1967 switch (pdata
->ch
[i
].chan
) {
1968 case LCDC_CHAN_MAINLCD
:
1969 ch
->enabled
= LDCNT2R_ME
;
1970 ch
->reg_offs
= lcdc_offs_mainlcd
;
1973 case LCDC_CHAN_SUBLCD
:
1974 ch
->enabled
= LDCNT2R_SE
;
1975 ch
->reg_offs
= lcdc_offs_sublcd
;
1981 if (!num_channels
) {
1982 dev_err(&pdev
->dev
, "no channels defined\n");
1987 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
1988 if (num_channels
== 2)
1989 priv
->forced_fourcc
= pdata
->ch
[0].fourcc
;
1991 priv
->base
= ioremap_nocache(res
->start
, resource_size(res
));
1995 error
= sh_mobile_lcdc_setup_clocks(priv
, pdata
->clock_source
);
1997 dev_err(&pdev
->dev
, "unable to setup clocks\n");
2001 /* Enable runtime PM. */
2002 pm_runtime_enable(&pdev
->dev
);
2004 for (i
= 0; i
< num_channels
; i
++) {
2005 struct sh_mobile_lcdc_chan
*ch
= priv
->ch
+ i
;
2007 error
= sh_mobile_lcdc_channel_init(priv
, ch
);
2012 error
= sh_mobile_lcdc_start(priv
);
2014 dev_err(&pdev
->dev
, "unable to start hardware\n");
2018 for (i
= 0; i
< num_channels
; i
++) {
2019 struct sh_mobile_lcdc_chan
*ch
= priv
->ch
+ i
;
2021 error
= sh_mobile_lcdc_channel_fb_register(ch
);
2026 /* Failure ignored */
2027 priv
->notifier
.notifier_call
= sh_mobile_lcdc_notify
;
2028 fb_register_client(&priv
->notifier
);
2032 sh_mobile_lcdc_remove(pdev
);
2037 static struct platform_driver sh_mobile_lcdc_driver
= {
2039 .name
= "sh_mobile_lcdc_fb",
2040 .owner
= THIS_MODULE
,
2041 .pm
= &sh_mobile_lcdc_dev_pm_ops
,
2043 .probe
= sh_mobile_lcdc_probe
,
2044 .remove
= sh_mobile_lcdc_remove
,
2047 module_platform_driver(sh_mobile_lcdc_driver
);
2049 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
2050 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
2051 MODULE_LICENSE("GPL v2");