2 * SuperH Mobile LCDC Framebuffer
4 * Copyright (c) 2008 Magnus Damm
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
11 #include <linux/atomic.h>
12 #include <linux/backlight.h>
13 #include <linux/clk.h>
14 #include <linux/console.h>
15 #include <linux/ctype.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/delay.h>
18 #include <linux/gpio.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/ioctl.h>
22 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/slab.h>
28 #include <linux/videodev2.h>
29 #include <linux/vmalloc.h>
31 #include <video/sh_mobile_lcdc.h>
32 #include <video/sh_mobile_meram.h>
34 #include "sh_mobile_lcdcfb.h"
36 /* ----------------------------------------------------------------------------
37 * Overlay register definitions
41 #define LDBCR_UPC(n) (1 << ((n) + 16))
42 #define LDBCR_UPF(n) (1 << ((n) + 8))
43 #define LDBCR_UPD(n) (1 << ((n) + 0))
44 #define LDBnBSIFR(n) (0xb20 + (n) * 0x20 + 0x00)
45 #define LDBBSIFR_EN (1 << 31)
46 #define LDBBSIFR_VS (1 << 29)
47 #define LDBBSIFR_BRSEL (1 << 28)
48 #define LDBBSIFR_MX (1 << 27)
49 #define LDBBSIFR_MY (1 << 26)
50 #define LDBBSIFR_CV3 (3 << 24)
51 #define LDBBSIFR_CV2 (2 << 24)
52 #define LDBBSIFR_CV1 (1 << 24)
53 #define LDBBSIFR_CV0 (0 << 24)
54 #define LDBBSIFR_CV_MASK (3 << 24)
55 #define LDBBSIFR_LAY_MASK (0xff << 16)
56 #define LDBBSIFR_LAY_SHIFT 16
57 #define LDBBSIFR_ROP3_MASK (0xff << 16)
58 #define LDBBSIFR_ROP3_SHIFT 16
59 #define LDBBSIFR_AL_PL8 (3 << 14)
60 #define LDBBSIFR_AL_PL1 (2 << 14)
61 #define LDBBSIFR_AL_PK (1 << 14)
62 #define LDBBSIFR_AL_1 (0 << 14)
63 #define LDBBSIFR_AL_MASK (3 << 14)
64 #define LDBBSIFR_SWPL (1 << 10)
65 #define LDBBSIFR_SWPW (1 << 9)
66 #define LDBBSIFR_SWPB (1 << 8)
67 #define LDBBSIFR_RY (1 << 7)
68 #define LDBBSIFR_CHRR_420 (2 << 0)
69 #define LDBBSIFR_CHRR_422 (1 << 0)
70 #define LDBBSIFR_CHRR_444 (0 << 0)
71 #define LDBBSIFR_RPKF_ARGB32 (0x00 << 0)
72 #define LDBBSIFR_RPKF_RGB16 (0x03 << 0)
73 #define LDBBSIFR_RPKF_RGB24 (0x0b << 0)
74 #define LDBBSIFR_RPKF_MASK (0x1f << 0)
75 #define LDBnBSSZR(n) (0xb20 + (n) * 0x20 + 0x04)
76 #define LDBBSSZR_BVSS_MASK (0xfff << 16)
77 #define LDBBSSZR_BVSS_SHIFT 16
78 #define LDBBSSZR_BHSS_MASK (0xfff << 0)
79 #define LDBBSSZR_BHSS_SHIFT 0
80 #define LDBnBLOCR(n) (0xb20 + (n) * 0x20 + 0x08)
81 #define LDBBLOCR_CVLC_MASK (0xfff << 16)
82 #define LDBBLOCR_CVLC_SHIFT 16
83 #define LDBBLOCR_CHLC_MASK (0xfff << 0)
84 #define LDBBLOCR_CHLC_SHIFT 0
85 #define LDBnBSMWR(n) (0xb20 + (n) * 0x20 + 0x0c)
86 #define LDBBSMWR_BSMWA_MASK (0xffff << 16)
87 #define LDBBSMWR_BSMWA_SHIFT 16
88 #define LDBBSMWR_BSMW_MASK (0xffff << 0)
89 #define LDBBSMWR_BSMW_SHIFT 0
90 #define LDBnBSAYR(n) (0xb20 + (n) * 0x20 + 0x10)
91 #define LDBBSAYR_FG1A_MASK (0xff << 24)
92 #define LDBBSAYR_FG1A_SHIFT 24
93 #define LDBBSAYR_FG1R_MASK (0xff << 16)
94 #define LDBBSAYR_FG1R_SHIFT 16
95 #define LDBBSAYR_FG1G_MASK (0xff << 8)
96 #define LDBBSAYR_FG1G_SHIFT 8
97 #define LDBBSAYR_FG1B_MASK (0xff << 0)
98 #define LDBBSAYR_FG1B_SHIFT 0
99 #define LDBnBSACR(n) (0xb20 + (n) * 0x20 + 0x14)
100 #define LDBBSACR_FG2A_MASK (0xff << 24)
101 #define LDBBSACR_FG2A_SHIFT 24
102 #define LDBBSACR_FG2R_MASK (0xff << 16)
103 #define LDBBSACR_FG2R_SHIFT 16
104 #define LDBBSACR_FG2G_MASK (0xff << 8)
105 #define LDBBSACR_FG2G_SHIFT 8
106 #define LDBBSACR_FG2B_MASK (0xff << 0)
107 #define LDBBSACR_FG2B_SHIFT 0
108 #define LDBnBSAAR(n) (0xb20 + (n) * 0x20 + 0x18)
109 #define LDBBSAAR_AP_MASK (0xff << 24)
110 #define LDBBSAAR_AP_SHIFT 24
111 #define LDBBSAAR_R_MASK (0xff << 16)
112 #define LDBBSAAR_R_SHIFT 16
113 #define LDBBSAAR_GY_MASK (0xff << 8)
114 #define LDBBSAAR_GY_SHIFT 8
115 #define LDBBSAAR_B_MASK (0xff << 0)
116 #define LDBBSAAR_B_SHIFT 0
117 #define LDBnBPPCR(n) (0xb20 + (n) * 0x20 + 0x1c)
118 #define LDBBPPCR_AP_MASK (0xff << 24)
119 #define LDBBPPCR_AP_SHIFT 24
120 #define LDBBPPCR_R_MASK (0xff << 16)
121 #define LDBBPPCR_R_SHIFT 16
122 #define LDBBPPCR_GY_MASK (0xff << 8)
123 #define LDBBPPCR_GY_SHIFT 8
124 #define LDBBPPCR_B_MASK (0xff << 0)
125 #define LDBBPPCR_B_SHIFT 0
126 #define LDBnBBGCL(n) (0xb10 + (n) * 0x04)
127 #define LDBBBGCL_BGA_MASK (0xff << 24)
128 #define LDBBBGCL_BGA_SHIFT 24
129 #define LDBBBGCL_BGR_MASK (0xff << 16)
130 #define LDBBBGCL_BGR_SHIFT 16
131 #define LDBBBGCL_BGG_MASK (0xff << 8)
132 #define LDBBBGCL_BGG_SHIFT 8
133 #define LDBBBGCL_BGB_MASK (0xff << 0)
134 #define LDBBBGCL_BGB_SHIFT 0
136 #define SIDE_B_OFFSET 0x1000
137 #define MIRROR_OFFSET 0x2000
139 #define MAX_XRES 1920
140 #define MAX_YRES 1080
142 enum sh_mobile_lcdc_overlay_mode {
148 * struct sh_mobile_lcdc_overlay - LCDC display overlay
150 * @channel: LCDC channel this overlay belongs to
151 * @cfg: Overlay configuration
152 * @info: Frame buffer device
153 * @index: Overlay index (0-3)
154 * @base: Overlay registers base address
155 * @enabled: True if the overlay is enabled
156 * @mode: Overlay blending mode (alpha blend or ROP3)
157 * @alpha: Global alpha blending value (0-255, for alpha blending mode)
158 * @rop3: Raster operation (for ROP3 mode)
159 * @fb_mem: Frame buffer virtual memory address
160 * @fb_size: Frame buffer size in bytes
161 * @dma_handle: Frame buffer DMA address
162 * @base_addr_y: Overlay base address (RGB or luma component)
163 * @base_addr_c: Overlay base address (chroma component)
164 * @pan_offset: Current pan offset in bytes
165 * @format: Current pixelf format
166 * @xres: Horizontal visible resolution
167 * @xres_virtual: Horizontal total resolution
168 * @yres: Vertical visible resolution
169 * @yres_virtual: Vertical total resolution
170 * @pitch: Overlay line pitch
171 * @pos_x: Horizontal overlay position
172 * @pos_y: Vertical overlay position
174 struct sh_mobile_lcdc_overlay {
175 struct sh_mobile_lcdc_chan *channel;
177 const struct sh_mobile_lcdc_overlay_cfg *cfg;
178 struct fb_info *info;
184 enum sh_mobile_lcdc_overlay_mode mode;
189 unsigned long fb_size;
191 dma_addr_t dma_handle;
192 unsigned long base_addr_y;
193 unsigned long base_addr_c;
194 unsigned long pan_offset;
196 const struct sh_mobile_lcdc_format_info *format;
198 unsigned int xres_virtual;
200 unsigned int yres_virtual;
206 struct sh_mobile_lcdc_priv {
212 unsigned long lddckr;
214 struct sh_mobile_lcdc_chan ch[2];
215 struct sh_mobile_lcdc_overlay overlays[4];
217 struct notifier_block notifier;
219 int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
220 struct sh_mobile_meram_info *meram_dev;
223 /* -----------------------------------------------------------------------------
227 static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
228 [LDDCKPAT1R] = 0x400,
229 [LDDCKPAT2R] = 0x404,
247 static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
248 [LDDCKPAT1R] = 0x408,
249 [LDDCKPAT2R] = 0x40c,
265 static bool banked(int reg_nr)
285 static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
287 return chan->cfg->chan == LCDC_CHAN_SUBLCD;
290 static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
291 int reg_nr, unsigned long data)
293 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
295 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
299 static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
300 int reg_nr, unsigned long data)
302 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
306 static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
309 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
312 static void lcdc_write_overlay(struct sh_mobile_lcdc_overlay *ovl,
313 int reg, unsigned long data)
315 iowrite32(data, ovl->channel->lcdc->base + reg);
316 iowrite32(data, ovl->channel->lcdc->base + reg + SIDE_B_OFFSET);
319 static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
320 unsigned long reg_offs, unsigned long data)
322 iowrite32(data, priv->base + reg_offs);
325 static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
326 unsigned long reg_offs)
328 return ioread32(priv->base + reg_offs);
331 static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
332 unsigned long reg_offs,
333 unsigned long mask, unsigned long until)
335 while ((lcdc_read(priv, reg_offs) & mask) != until)
339 /* -----------------------------------------------------------------------------
343 static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
345 if (atomic_inc_and_test(&priv->hw_usecnt)) {
347 clk_enable(priv->dot_clk);
348 pm_runtime_get_sync(priv->dev);
349 if (priv->meram_dev && priv->meram_dev->pdev)
350 pm_runtime_get_sync(&priv->meram_dev->pdev->dev);
354 static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
356 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
357 if (priv->meram_dev && priv->meram_dev->pdev)
358 pm_runtime_put_sync(&priv->meram_dev->pdev->dev);
359 pm_runtime_put(priv->dev);
361 clk_disable(priv->dot_clk);
365 static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
371 switch (clock_source) {
374 priv->lddckr = LDDCKR_ICKSEL_BUS;
376 case LCDC_CLK_PERIPHERAL:
377 str = "peripheral_clk";
378 priv->lddckr = LDDCKR_ICKSEL_MIPI;
380 case LCDC_CLK_EXTERNAL:
382 priv->lddckr = LDDCKR_ICKSEL_HDMI;
391 clk = clk_get(priv->dev, str);
393 dev_err(priv->dev, "cannot get dot clock %s\n", str);
401 /* -----------------------------------------------------------------------------
402 * Display, panel and deferred I/O
405 static void lcdc_sys_write_index(void *handle, unsigned long data)
407 struct sh_mobile_lcdc_chan *ch = handle;
409 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
410 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
411 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
412 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
413 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
416 static void lcdc_sys_write_data(void *handle, unsigned long data)
418 struct sh_mobile_lcdc_chan *ch = handle;
420 lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
421 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
422 lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
423 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
424 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
427 static unsigned long lcdc_sys_read_data(void *handle)
429 struct sh_mobile_lcdc_chan *ch = handle;
431 lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
432 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
433 lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
434 (lcdc_chan_is_sublcd(ch) ? 2 : 0));
436 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
438 return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
441 struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
442 lcdc_sys_write_index,
447 static int sh_mobile_lcdc_sginit(struct fb_info *info,
448 struct list_head *pagelist)
450 struct sh_mobile_lcdc_chan *ch = info->par;
451 unsigned int nr_pages_max = ch->fb_size >> PAGE_SHIFT;
455 sg_init_table(ch->sglist, nr_pages_max);
457 list_for_each_entry(page, pagelist, lru)
458 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
463 static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
464 struct list_head *pagelist)
466 struct sh_mobile_lcdc_chan *ch = info->par;
467 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
469 /* enable clocks before accessing hardware */
470 sh_mobile_lcdc_clk_on(ch->lcdc);
473 * It's possible to get here without anything on the pagelist via
474 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
475 * invocation. In the former case, the acceleration routines are
476 * stepped in to when using the framebuffer console causing the
477 * workqueue to be scheduled without any dirty pages on the list.
479 * Despite this, a panel update is still needed given that the
480 * acceleration routines have their own methods for writing in
481 * that still need to be updated.
483 * The fsync() and empty pagelist case could be optimized for,
484 * but we don't bother, as any application exhibiting such
485 * behaviour is fundamentally broken anyways.
487 if (!list_empty(pagelist)) {
488 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
490 /* trigger panel update */
491 dma_map_sg(ch->lcdc->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
492 if (panel->start_transfer)
493 panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
494 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
495 dma_unmap_sg(ch->lcdc->dev, ch->sglist, nr_pages,
498 if (panel->start_transfer)
499 panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
500 lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
504 static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
506 struct fb_deferred_io *fbdefio = info->fbdefio;
509 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
512 static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
514 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
519 ret = ch->tx_dev->ops->display_on(ch->tx_dev);
523 if (ret == SH_MOBILE_LCDC_DISPLAY_DISCONNECTED)
524 ch->info->state = FBINFO_STATE_SUSPENDED;
527 /* HDMI must be enabled before LCDC configuration */
528 if (panel->display_on)
532 static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
534 const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
536 if (panel->display_off)
537 panel->display_off();
540 ch->tx_dev->ops->display_off(ch->tx_dev);
544 sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan *ch,
545 const struct fb_videomode *new_mode)
547 dev_dbg(ch->info->dev, "Old %ux%u, new %ux%u\n",
548 ch->display.mode.xres, ch->display.mode.yres,
549 new_mode->xres, new_mode->yres);
551 /* It can be a different monitor with an equal video-mode */
552 if (fb_mode_is_equal(&ch->display.mode, new_mode))
555 dev_dbg(ch->info->dev, "Switching %u -> %u lines\n",
556 ch->display.mode.yres, new_mode->yres);
557 ch->display.mode = *new_mode;
562 static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
563 struct fb_info *info);
565 static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan *ch,
566 enum sh_mobile_lcdc_entity_event event,
567 const struct fb_videomode *mode,
568 const struct fb_monspecs *monspec)
570 struct fb_info *info = ch->info;
571 struct fb_var_screeninfo var;
575 case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
577 if (lock_fb_info(info)) {
580 ch->display.width = monspec->max_x * 10;
581 ch->display.height = monspec->max_y * 10;
583 if (!sh_mobile_lcdc_must_reconfigure(ch, mode) &&
584 info->state == FBINFO_STATE_RUNNING) {
585 /* First activation with the default monitor.
586 * Just turn on, if we run a resume here, the
589 info->var.width = monspec->max_x * 10;
590 info->var.height = monspec->max_y * 10;
591 sh_mobile_lcdc_display_on(ch);
593 /* New monitor or have to wake up */
594 fb_set_suspend(info, 0);
598 unlock_fb_info(info);
602 case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT:
603 /* HDMI disconnect */
604 if (lock_fb_info(info)) {
606 fb_set_suspend(info, 1);
608 unlock_fb_info(info);
612 case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE:
613 /* Validate a proposed new mode */
614 fb_videomode_to_var(&var, mode);
615 var.bits_per_pixel = info->var.bits_per_pixel;
616 var.grayscale = info->var.grayscale;
617 ret = sh_mobile_lcdc_check_var(&var, info);
624 /* -----------------------------------------------------------------------------
628 struct sh_mobile_lcdc_format_info {
635 static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos[] = {
637 .fourcc = V4L2_PIX_FMT_RGB565,
640 .lddfr = LDDFR_PKF_RGB16,
642 .fourcc = V4L2_PIX_FMT_BGR24,
645 .lddfr = LDDFR_PKF_RGB24,
647 .fourcc = V4L2_PIX_FMT_BGR32,
650 .lddfr = LDDFR_PKF_ARGB32,
652 .fourcc = V4L2_PIX_FMT_NV12,
655 .lddfr = LDDFR_CC | LDDFR_YF_420,
657 .fourcc = V4L2_PIX_FMT_NV21,
660 .lddfr = LDDFR_CC | LDDFR_YF_420,
662 .fourcc = V4L2_PIX_FMT_NV16,
665 .lddfr = LDDFR_CC | LDDFR_YF_422,
667 .fourcc = V4L2_PIX_FMT_NV61,
670 .lddfr = LDDFR_CC | LDDFR_YF_422,
672 .fourcc = V4L2_PIX_FMT_NV24,
675 .lddfr = LDDFR_CC | LDDFR_YF_444,
677 .fourcc = V4L2_PIX_FMT_NV42,
680 .lddfr = LDDFR_CC | LDDFR_YF_444,
684 static const struct sh_mobile_lcdc_format_info *
685 sh_mobile_format_info(u32 fourcc)
689 for (i = 0; i < ARRAY_SIZE(sh_mobile_format_infos); ++i) {
690 if (sh_mobile_format_infos[i].fourcc == fourcc)
691 return &sh_mobile_format_infos[i];
697 static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
699 if (var->grayscale > 1)
700 return var->grayscale;
702 switch (var->bits_per_pixel) {
704 return V4L2_PIX_FMT_RGB565;
706 return V4L2_PIX_FMT_BGR24;
708 return V4L2_PIX_FMT_BGR32;
714 static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
716 return var->grayscale > 1;
719 /* -----------------------------------------------------------------------------
720 * Start, stop and IRQ
723 static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
725 struct sh_mobile_lcdc_priv *priv = data;
726 struct sh_mobile_lcdc_chan *ch;
727 unsigned long ldintr;
731 /* Acknowledge interrupts and disable further VSYNC End IRQs. */
732 ldintr = lcdc_read(priv, _LDINTR);
733 lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
735 /* figure out if this interrupt is for main or sub lcd */
736 is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
738 /* wake up channel and disable clocks */
739 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
746 if (ldintr & LDINTR_FS) {
747 if (is_sub == lcdc_chan_is_sublcd(ch)) {
749 wake_up(&ch->frame_end_wait);
751 sh_mobile_lcdc_clk_off(priv);
756 if (ldintr & LDINTR_VES)
757 complete(&ch->vsync_completion);
763 static int sh_mobile_lcdc_wait_for_vsync(struct sh_mobile_lcdc_chan *ch)
765 unsigned long ldintr;
768 /* Enable VSync End interrupt and be careful not to acknowledge any
771 ldintr = lcdc_read(ch->lcdc, _LDINTR);
772 ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
773 lcdc_write(ch->lcdc, _LDINTR, ldintr);
775 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
776 msecs_to_jiffies(100));
783 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
786 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
789 /* start or stop the lcdc */
791 lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
793 lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
795 /* wait until power is applied/stopped on all channels */
796 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
797 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
799 tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
801 if (start && tmp == LDPMR_LPS)
803 if (!start && tmp == 0)
809 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
812 static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
814 const struct fb_var_screeninfo *var = &ch->info->var;
815 const struct fb_videomode *mode = &ch->display.mode;
816 unsigned long h_total, hsync_pos, display_h_total;
819 tmp = ch->ldmt1r_value;
820 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
821 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
822 tmp |= (ch->cfg->flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
823 tmp |= (ch->cfg->flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
824 tmp |= (ch->cfg->flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
825 tmp |= (ch->cfg->flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
826 tmp |= (ch->cfg->flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
827 lcdc_write_chan(ch, LDMT1R, tmp);
830 lcdc_write_chan(ch, LDMT2R, ch->cfg->sys_bus_cfg.ldmt2r);
831 lcdc_write_chan(ch, LDMT3R, ch->cfg->sys_bus_cfg.ldmt3r);
833 /* horizontal configuration */
834 h_total = mode->xres + mode->hsync_len + mode->left_margin
835 + mode->right_margin;
836 tmp = h_total / 8; /* HTCN */
837 tmp |= (min(mode->xres, ch->xres) / 8) << 16; /* HDCN */
838 lcdc_write_chan(ch, LDHCNR, tmp);
840 hsync_pos = mode->xres + mode->right_margin;
841 tmp = hsync_pos / 8; /* HSYNP */
842 tmp |= (mode->hsync_len / 8) << 16; /* HSYNW */
843 lcdc_write_chan(ch, LDHSYNR, tmp);
845 /* vertical configuration */
846 tmp = mode->yres + mode->vsync_len + mode->upper_margin
847 + mode->lower_margin; /* VTLN */
848 tmp |= min(mode->yres, ch->yres) << 16; /* VDLN */
849 lcdc_write_chan(ch, LDVLNR, tmp);
851 tmp = mode->yres + mode->lower_margin; /* VSYNP */
852 tmp |= mode->vsync_len << 16; /* VSYNW */
853 lcdc_write_chan(ch, LDVSYNR, tmp);
855 /* Adjust horizontal synchronisation for HDMI */
856 display_h_total = mode->xres + mode->hsync_len + mode->left_margin
857 + mode->right_margin;
858 tmp = ((mode->xres & 7) << 24) | ((display_h_total & 7) << 16)
859 | ((mode->hsync_len & 7) << 8) | (hsync_pos & 7);
860 lcdc_write_chan(ch, LDHAJR, tmp);
863 static void sh_mobile_lcdc_overlay_setup(struct sh_mobile_lcdc_overlay *ovl)
868 lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
869 lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), 0);
870 lcdc_write(ovl->channel->lcdc, LDBCR,
871 LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
875 ovl->base_addr_y = ovl->dma_handle;
876 ovl->base_addr_c = ovl->base_addr_y + ovl->xres
880 case LCDC_OVERLAY_BLEND:
881 format = LDBBSIFR_EN | (ovl->alpha << LDBBSIFR_LAY_SHIFT);
884 case LCDC_OVERLAY_ROP3:
885 format = LDBBSIFR_EN | LDBBSIFR_BRSEL
886 | (ovl->rop3 << LDBBSIFR_ROP3_SHIFT);
890 switch (ovl->format->fourcc) {
891 case V4L2_PIX_FMT_RGB565:
892 case V4L2_PIX_FMT_NV21:
893 case V4L2_PIX_FMT_NV61:
894 case V4L2_PIX_FMT_NV42:
895 format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW;
897 case V4L2_PIX_FMT_BGR24:
898 case V4L2_PIX_FMT_NV12:
899 case V4L2_PIX_FMT_NV16:
900 case V4L2_PIX_FMT_NV24:
901 format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW | LDBBSIFR_SWPB;
903 case V4L2_PIX_FMT_BGR32:
905 format |= LDBBSIFR_SWPL;
909 switch (ovl->format->fourcc) {
910 case V4L2_PIX_FMT_RGB565:
911 format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB16;
913 case V4L2_PIX_FMT_BGR24:
914 format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB24;
916 case V4L2_PIX_FMT_BGR32:
917 format |= LDBBSIFR_AL_PK | LDBBSIFR_RY | LDDFR_PKF_ARGB32;
919 case V4L2_PIX_FMT_NV12:
920 case V4L2_PIX_FMT_NV21:
921 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_420;
923 case V4L2_PIX_FMT_NV16:
924 case V4L2_PIX_FMT_NV61:
925 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_422;
927 case V4L2_PIX_FMT_NV24:
928 case V4L2_PIX_FMT_NV42:
929 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_444;
933 lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
935 lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), format);
937 lcdc_write_overlay(ovl, LDBnBSSZR(ovl->index),
938 (ovl->yres << LDBBSSZR_BVSS_SHIFT) |
939 (ovl->xres << LDBBSSZR_BHSS_SHIFT));
940 lcdc_write_overlay(ovl, LDBnBLOCR(ovl->index),
941 (ovl->pos_y << LDBBLOCR_CVLC_SHIFT) |
942 (ovl->pos_x << LDBBLOCR_CHLC_SHIFT));
943 lcdc_write_overlay(ovl, LDBnBSMWR(ovl->index),
944 ovl->pitch << LDBBSMWR_BSMW_SHIFT);
946 lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
947 lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
949 lcdc_write(ovl->channel->lcdc, LDBCR,
950 LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
954 * __sh_mobile_lcdc_start - Configure and start the LCDC
957 * Configure all enabled channels and start the LCDC device. All external
958 * devices (clocks, MERAM, panels, ...) are not touched by this function.
960 static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
962 struct sh_mobile_lcdc_chan *ch;
966 /* Enable LCDC channels. Read data from external memory, avoid using the
969 lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
971 /* Stop the LCDC first and disable all interrupts. */
972 sh_mobile_lcdc_start_stop(priv, 0);
973 lcdc_write(priv, _LDINTR, 0);
975 /* Configure power supply, dot clocks and start them. */
977 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
983 lcdc_write_chan(ch, LDPMR, 0);
985 m = ch->cfg->clock_divider;
989 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
992 lcdc_write_chan(ch, LDDCKPAT1R, 0);
993 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
997 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
1000 lcdc_write(priv, _LDDCKR, tmp);
1001 lcdc_write(priv, _LDDCKSTPR, 0);
1002 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
1004 /* Setup geometry, format, frame buffer memory and operation mode. */
1005 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1010 sh_mobile_lcdc_geometry(ch);
1012 tmp = ch->format->lddfr;
1014 if (ch->format->yuv) {
1015 switch (ch->colorspace) {
1016 case V4L2_COLORSPACE_REC709:
1019 case V4L2_COLORSPACE_JPEG:
1025 lcdc_write_chan(ch, LDDFR, tmp);
1026 lcdc_write_chan(ch, LDMLSR, ch->line_size);
1027 lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
1028 if (ch->format->yuv)
1029 lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
1031 /* When using deferred I/O mode, configure the LCDC for one-shot
1032 * operation and enable the frame end interrupt. Otherwise use
1033 * continuous read mode.
1035 if (ch->ldmt1r_value & LDMT1R_IFM &&
1036 ch->cfg->sys_bus_cfg.deferred_io_msec) {
1037 lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
1038 lcdc_write(priv, _LDINTR, LDINTR_FE);
1040 lcdc_write_chan(ch, LDSM1R, 0);
1044 /* Word and long word swap. */
1045 switch (priv->ch[0].format->fourcc) {
1046 case V4L2_PIX_FMT_RGB565:
1047 case V4L2_PIX_FMT_NV21:
1048 case V4L2_PIX_FMT_NV61:
1049 case V4L2_PIX_FMT_NV42:
1050 tmp = LDDDSR_LS | LDDDSR_WS;
1052 case V4L2_PIX_FMT_BGR24:
1053 case V4L2_PIX_FMT_NV12:
1054 case V4L2_PIX_FMT_NV16:
1055 case V4L2_PIX_FMT_NV24:
1056 tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
1058 case V4L2_PIX_FMT_BGR32:
1063 lcdc_write(priv, _LDDDSR, tmp);
1065 /* Enable the display output. */
1066 lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
1067 sh_mobile_lcdc_start_stop(priv, 1);
1071 static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
1073 struct sh_mobile_meram_info *mdev = priv->meram_dev;
1074 struct sh_mobile_lcdc_chan *ch;
1079 /* enable clocks before accessing the hardware */
1080 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1081 if (priv->ch[k].enabled)
1082 sh_mobile_lcdc_clk_on(priv);
1086 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
1087 lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
1089 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1090 const struct sh_mobile_lcdc_panel_cfg *panel;
1096 panel = &ch->cfg->panel_cfg;
1097 if (panel->setup_sys) {
1098 ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
1104 /* Compute frame buffer base address and pitch for each channel. */
1105 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1113 ch->base_addr_y = ch->dma_handle;
1114 ch->base_addr_c = ch->base_addr_y + ch->xres * ch->yres_virtual;
1115 ch->line_size = ch->pitch;
1117 /* Enable MERAM if possible. */
1118 if (mdev == NULL || ch->cfg->meram_cfg == NULL)
1121 /* Free the allocated MERAM cache. */
1123 sh_mobile_meram_cache_free(mdev, ch->cache);
1127 switch (ch->format->fourcc) {
1128 case V4L2_PIX_FMT_NV12:
1129 case V4L2_PIX_FMT_NV21:
1130 case V4L2_PIX_FMT_NV16:
1131 case V4L2_PIX_FMT_NV61:
1132 pixelformat = SH_MOBILE_MERAM_PF_NV;
1134 case V4L2_PIX_FMT_NV24:
1135 case V4L2_PIX_FMT_NV42:
1136 pixelformat = SH_MOBILE_MERAM_PF_NV24;
1138 case V4L2_PIX_FMT_RGB565:
1139 case V4L2_PIX_FMT_BGR24:
1140 case V4L2_PIX_FMT_BGR32:
1142 pixelformat = SH_MOBILE_MERAM_PF_RGB;
1146 cache = sh_mobile_meram_cache_alloc(mdev, ch->cfg->meram_cfg,
1147 ch->pitch, ch->yres, pixelformat,
1149 if (!IS_ERR(cache)) {
1150 sh_mobile_meram_cache_update(mdev, cache,
1151 ch->base_addr_y, ch->base_addr_c,
1152 &ch->base_addr_y, &ch->base_addr_c);
1157 for (k = 0; k < ARRAY_SIZE(priv->overlays); ++k) {
1158 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[k];
1159 sh_mobile_lcdc_overlay_setup(ovl);
1162 /* Start the LCDC. */
1163 __sh_mobile_lcdc_start(priv);
1165 /* Setup deferred I/O, tell the board code to enable the panels, and
1166 * turn backlight on.
1168 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1173 tmp = ch->cfg->sys_bus_cfg.deferred_io_msec;
1174 if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
1175 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
1176 ch->defio.delay = msecs_to_jiffies(tmp);
1177 ch->info->fbdefio = &ch->defio;
1178 fb_deferred_io_init(ch->info);
1181 sh_mobile_lcdc_display_on(ch);
1184 ch->bl->props.power = FB_BLANK_UNBLANK;
1185 backlight_update_status(ch->bl);
1192 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
1194 struct sh_mobile_lcdc_chan *ch;
1197 /* clean up deferred io and ask board code to disable panel */
1198 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1203 /* deferred io mode:
1204 * flush frame, and wait for frame end interrupt
1205 * clean up deferred io and enable clock
1207 if (ch->info && ch->info->fbdefio) {
1209 schedule_delayed_work(&ch->info->deferred_work, 0);
1210 wait_event(ch->frame_end_wait, ch->frame_end);
1211 fb_deferred_io_cleanup(ch->info);
1212 ch->info->fbdefio = NULL;
1213 sh_mobile_lcdc_clk_on(priv);
1217 ch->bl->props.power = FB_BLANK_POWERDOWN;
1218 backlight_update_status(ch->bl);
1221 sh_mobile_lcdc_display_off(ch);
1223 /* Free the MERAM cache. */
1225 sh_mobile_meram_cache_free(priv->meram_dev, ch->cache);
1232 if (priv->started) {
1233 sh_mobile_lcdc_start_stop(priv, 0);
1238 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
1239 if (priv->ch[k].enabled)
1240 sh_mobile_lcdc_clk_off(priv);
1243 static int __sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
1244 struct fb_info *info)
1246 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
1249 /* Make sure the virtual resolution is at least as big as the visible
1252 if (var->xres_virtual < var->xres)
1253 var->xres_virtual = var->xres;
1254 if (var->yres_virtual < var->yres)
1255 var->yres_virtual = var->yres;
1257 if (sh_mobile_format_is_fourcc(var)) {
1258 const struct sh_mobile_lcdc_format_info *format;
1260 format = sh_mobile_format_info(var->grayscale);
1263 var->bits_per_pixel = format->bpp;
1265 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1269 var->colorspace = V4L2_COLORSPACE_SRGB;
1270 else if (var->colorspace != V4L2_COLORSPACE_REC709)
1271 var->colorspace = V4L2_COLORSPACE_JPEG;
1273 if (var->bits_per_pixel <= 16) { /* RGB 565 */
1274 var->bits_per_pixel = 16;
1275 var->red.offset = 11;
1276 var->red.length = 5;
1277 var->green.offset = 5;
1278 var->green.length = 6;
1279 var->blue.offset = 0;
1280 var->blue.length = 5;
1281 var->transp.offset = 0;
1282 var->transp.length = 0;
1283 } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
1284 var->bits_per_pixel = 24;
1285 var->red.offset = 16;
1286 var->red.length = 8;
1287 var->green.offset = 8;
1288 var->green.length = 8;
1289 var->blue.offset = 0;
1290 var->blue.length = 8;
1291 var->transp.offset = 0;
1292 var->transp.length = 0;
1293 } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
1294 var->bits_per_pixel = 32;
1295 var->red.offset = 16;
1296 var->red.length = 8;
1297 var->green.offset = 8;
1298 var->green.length = 8;
1299 var->blue.offset = 0;
1300 var->blue.length = 8;
1301 var->transp.offset = 24;
1302 var->transp.length = 8;
1306 var->red.msb_right = 0;
1307 var->green.msb_right = 0;
1308 var->blue.msb_right = 0;
1309 var->transp.msb_right = 0;
1312 /* Make sure we don't exceed our allocated memory. */
1313 if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1320 /* -----------------------------------------------------------------------------
1321 * Frame buffer operations - Overlays
1325 overlay_alpha_show(struct device *dev, struct device_attribute *attr, char *buf)
1327 struct fb_info *info = dev_get_drvdata(dev);
1328 struct sh_mobile_lcdc_overlay *ovl = info->par;
1330 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->alpha);
1334 overlay_alpha_store(struct device *dev, struct device_attribute *attr,
1335 const char *buf, size_t count)
1337 struct fb_info *info = dev_get_drvdata(dev);
1338 struct sh_mobile_lcdc_overlay *ovl = info->par;
1342 alpha = simple_strtoul(buf, &endp, 10);
1346 if (endp - buf != count)
1352 if (ovl->alpha != alpha) {
1355 if (ovl->mode == LCDC_OVERLAY_BLEND && ovl->enabled)
1356 sh_mobile_lcdc_overlay_setup(ovl);
1363 overlay_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1365 struct fb_info *info = dev_get_drvdata(dev);
1366 struct sh_mobile_lcdc_overlay *ovl = info->par;
1368 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->mode);
1372 overlay_mode_store(struct device *dev, struct device_attribute *attr,
1373 const char *buf, size_t count)
1375 struct fb_info *info = dev_get_drvdata(dev);
1376 struct sh_mobile_lcdc_overlay *ovl = info->par;
1380 mode = simple_strtoul(buf, &endp, 10);
1384 if (endp - buf != count)
1387 if (mode != LCDC_OVERLAY_BLEND && mode != LCDC_OVERLAY_ROP3)
1390 if (ovl->mode != mode) {
1394 sh_mobile_lcdc_overlay_setup(ovl);
1401 overlay_position_show(struct device *dev, struct device_attribute *attr,
1404 struct fb_info *info = dev_get_drvdata(dev);
1405 struct sh_mobile_lcdc_overlay *ovl = info->par;
1407 return scnprintf(buf, PAGE_SIZE, "%d,%d\n", ovl->pos_x, ovl->pos_y);
1411 overlay_position_store(struct device *dev, struct device_attribute *attr,
1412 const char *buf, size_t count)
1414 struct fb_info *info = dev_get_drvdata(dev);
1415 struct sh_mobile_lcdc_overlay *ovl = info->par;
1420 pos_x = simple_strtol(buf, &endp, 10);
1424 pos_y = simple_strtol(endp + 1, &endp, 10);
1428 if (endp - buf != count)
1431 if (ovl->pos_x != pos_x || ovl->pos_y != pos_y) {
1436 sh_mobile_lcdc_overlay_setup(ovl);
1443 overlay_rop3_show(struct device *dev, struct device_attribute *attr, char *buf)
1445 struct fb_info *info = dev_get_drvdata(dev);
1446 struct sh_mobile_lcdc_overlay *ovl = info->par;
1448 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->rop3);
1452 overlay_rop3_store(struct device *dev, struct device_attribute *attr,
1453 const char *buf, size_t count)
1455 struct fb_info *info = dev_get_drvdata(dev);
1456 struct sh_mobile_lcdc_overlay *ovl = info->par;
1460 rop3 = !!simple_strtoul(buf, &endp, 10);
1464 if (endp - buf != count)
1470 if (ovl->rop3 != rop3) {
1473 if (ovl->mode == LCDC_OVERLAY_ROP3 && ovl->enabled)
1474 sh_mobile_lcdc_overlay_setup(ovl);
1480 static const struct device_attribute overlay_sysfs_attrs[] = {
1481 __ATTR(ovl_alpha, S_IRUGO|S_IWUSR,
1482 overlay_alpha_show, overlay_alpha_store),
1483 __ATTR(ovl_mode, S_IRUGO|S_IWUSR,
1484 overlay_mode_show, overlay_mode_store),
1485 __ATTR(ovl_position, S_IRUGO|S_IWUSR,
1486 overlay_position_show, overlay_position_store),
1487 __ATTR(ovl_rop3, S_IRUGO|S_IWUSR,
1488 overlay_rop3_show, overlay_rop3_store),
1491 static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix = {
1492 .id = "SH Mobile LCDC",
1493 .type = FB_TYPE_PACKED_PIXELS,
1494 .visual = FB_VISUAL_TRUECOLOR,
1495 .accel = FB_ACCEL_NONE,
1499 .capabilities = FB_CAP_FOURCC,
1502 static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo *var,
1503 struct fb_info *info)
1505 struct sh_mobile_lcdc_overlay *ovl = info->par;
1506 unsigned long base_addr_y;
1507 unsigned long base_addr_c;
1508 unsigned long pan_offset;
1509 unsigned long c_offset;
1511 if (!ovl->format->yuv)
1512 pan_offset = var->yoffset * ovl->pitch
1513 + var->xoffset * (ovl->format->bpp / 8);
1515 pan_offset = var->yoffset * ovl->pitch + var->xoffset;
1517 if (pan_offset == ovl->pan_offset)
1518 return 0; /* No change, do nothing */
1520 /* Set the source address for the next refresh */
1521 base_addr_y = ovl->dma_handle + pan_offset;
1523 ovl->base_addr_y = base_addr_y;
1524 ovl->base_addr_c = base_addr_y;
1526 if (ovl->format->yuv) {
1528 c_offset = var->yoffset * ovl->pitch
1529 * (ovl->format->bpp - 8) / 8;
1530 base_addr_c = ovl->dma_handle
1531 + ovl->xres * ovl->yres_virtual
1534 if (ovl->format->fourcc == V4L2_PIX_FMT_NV24)
1535 base_addr_c += 2 * var->xoffset;
1537 base_addr_c += var->xoffset;
1539 ovl->base_addr_c = base_addr_c;
1542 lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
1543 lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
1545 ovl->pan_offset = pan_offset;
1550 static int sh_mobile_lcdc_overlay_ioctl(struct fb_info *info, unsigned int cmd,
1553 struct sh_mobile_lcdc_overlay *ovl = info->par;
1556 case FBIO_WAITFORVSYNC:
1557 return sh_mobile_lcdc_wait_for_vsync(ovl->channel);
1560 return -ENOIOCTLCMD;
1564 static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo *var,
1565 struct fb_info *info)
1567 return __sh_mobile_lcdc_check_var(var, info);
1570 static int sh_mobile_lcdc_overlay_set_par(struct fb_info *info)
1572 struct sh_mobile_lcdc_overlay *ovl = info->par;
1575 sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
1577 ovl->xres = info->var.xres;
1578 ovl->xres_virtual = info->var.xres_virtual;
1579 ovl->yres = info->var.yres;
1580 ovl->yres_virtual = info->var.yres_virtual;
1582 if (ovl->format->yuv)
1583 ovl->pitch = info->var.xres;
1585 ovl->pitch = info->var.xres * ovl->format->bpp / 8;
1587 sh_mobile_lcdc_overlay_setup(ovl);
1589 info->fix.line_length = ovl->pitch;
1591 if (sh_mobile_format_is_fourcc(&info->var)) {
1592 info->fix.type = FB_TYPE_FOURCC;
1593 info->fix.visual = FB_VISUAL_FOURCC;
1595 info->fix.type = FB_TYPE_PACKED_PIXELS;
1596 info->fix.visual = FB_VISUAL_TRUECOLOR;
1602 /* Overlay blanking. Disable the overlay when blanked. */
1603 static int sh_mobile_lcdc_overlay_blank(int blank, struct fb_info *info)
1605 struct sh_mobile_lcdc_overlay *ovl = info->par;
1607 ovl->enabled = !blank;
1608 sh_mobile_lcdc_overlay_setup(ovl);
1610 /* Prevent the backlight from receiving a blanking event by returning
1616 static struct fb_ops sh_mobile_lcdc_overlay_ops = {
1617 .owner = THIS_MODULE,
1618 .fb_read = fb_sys_read,
1619 .fb_write = fb_sys_write,
1620 .fb_fillrect = sys_fillrect,
1621 .fb_copyarea = sys_copyarea,
1622 .fb_imageblit = sys_imageblit,
1623 .fb_blank = sh_mobile_lcdc_overlay_blank,
1624 .fb_pan_display = sh_mobile_lcdc_overlay_pan,
1625 .fb_ioctl = sh_mobile_lcdc_overlay_ioctl,
1626 .fb_check_var = sh_mobile_lcdc_overlay_check_var,
1627 .fb_set_par = sh_mobile_lcdc_overlay_set_par,
1631 sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay *ovl)
1633 struct fb_info *info = ovl->info;
1635 if (info == NULL || info->dev == NULL)
1638 unregister_framebuffer(ovl->info);
1641 static int __devinit
1642 sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay *ovl)
1644 struct sh_mobile_lcdc_priv *lcdc = ovl->channel->lcdc;
1645 struct fb_info *info = ovl->info;
1652 ret = register_framebuffer(info);
1656 dev_info(lcdc->dev, "registered %s/overlay %u as %dx%d %dbpp.\n",
1657 dev_name(lcdc->dev), ovl->index, info->var.xres,
1658 info->var.yres, info->var.bits_per_pixel);
1660 for (i = 0; i < ARRAY_SIZE(overlay_sysfs_attrs); ++i) {
1661 ret = device_create_file(info->dev, &overlay_sysfs_attrs[i]);
1670 sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay *ovl)
1672 struct fb_info *info = ovl->info;
1674 if (info == NULL || info->device == NULL)
1677 framebuffer_release(info);
1680 static int __devinit
1681 sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay *ovl)
1683 struct sh_mobile_lcdc_priv *priv = ovl->channel->lcdc;
1684 struct fb_var_screeninfo *var;
1685 struct fb_info *info;
1687 /* Allocate and initialize the frame buffer device. */
1688 info = framebuffer_alloc(0, priv->dev);
1690 dev_err(priv->dev, "unable to allocate fb_info\n");
1696 info->flags = FBINFO_FLAG_DEFAULT;
1697 info->fbops = &sh_mobile_lcdc_overlay_ops;
1698 info->device = priv->dev;
1699 info->screen_base = ovl->fb_mem;
1702 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1703 * for NV12 and NV21.
1705 info->fix = sh_mobile_lcdc_overlay_fix;
1706 snprintf(info->fix.id, sizeof(info->fix.id),
1707 "SH Mobile LCDC Overlay %u", ovl->index);
1708 info->fix.smem_start = ovl->dma_handle;
1709 info->fix.smem_len = ovl->fb_size;
1710 info->fix.line_length = ovl->pitch;
1712 if (ovl->format->yuv)
1713 info->fix.visual = FB_VISUAL_FOURCC;
1715 info->fix.visual = FB_VISUAL_TRUECOLOR;
1717 if (ovl->format->fourcc == V4L2_PIX_FMT_NV12 ||
1718 ovl->format->fourcc == V4L2_PIX_FMT_NV21)
1719 info->fix.ypanstep = 2;
1721 /* Initialize variable screen information. */
1723 memset(var, 0, sizeof(*var));
1724 var->xres = ovl->xres;
1725 var->yres = ovl->yres;
1726 var->xres_virtual = ovl->xres_virtual;
1727 var->yres_virtual = ovl->yres_virtual;
1728 var->activate = FB_ACTIVATE_NOW;
1730 /* Use the legacy API by default for RGB formats, and the FOURCC API
1733 if (!ovl->format->yuv)
1734 var->bits_per_pixel = ovl->format->bpp;
1736 var->grayscale = ovl->format->fourcc;
1738 return sh_mobile_lcdc_overlay_check_var(var, info);
1741 /* -----------------------------------------------------------------------------
1742 * Frame buffer operations - main frame buffer
1745 static int sh_mobile_lcdc_setcolreg(u_int regno,
1746 u_int red, u_int green, u_int blue,
1747 u_int transp, struct fb_info *info)
1749 u32 *palette = info->pseudo_palette;
1751 if (regno >= PALETTE_NR)
1754 /* only FB_VISUAL_TRUECOLOR supported */
1756 red >>= 16 - info->var.red.length;
1757 green >>= 16 - info->var.green.length;
1758 blue >>= 16 - info->var.blue.length;
1759 transp >>= 16 - info->var.transp.length;
1761 palette[regno] = (red << info->var.red.offset) |
1762 (green << info->var.green.offset) |
1763 (blue << info->var.blue.offset) |
1764 (transp << info->var.transp.offset);
1769 static const struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
1770 .id = "SH Mobile LCDC",
1771 .type = FB_TYPE_PACKED_PIXELS,
1772 .visual = FB_VISUAL_TRUECOLOR,
1773 .accel = FB_ACCEL_NONE,
1777 .capabilities = FB_CAP_FOURCC,
1780 static void sh_mobile_lcdc_fillrect(struct fb_info *info,
1781 const struct fb_fillrect *rect)
1783 sys_fillrect(info, rect);
1784 sh_mobile_lcdc_deferred_io_touch(info);
1787 static void sh_mobile_lcdc_copyarea(struct fb_info *info,
1788 const struct fb_copyarea *area)
1790 sys_copyarea(info, area);
1791 sh_mobile_lcdc_deferred_io_touch(info);
1794 static void sh_mobile_lcdc_imageblit(struct fb_info *info,
1795 const struct fb_image *image)
1797 sys_imageblit(info, image);
1798 sh_mobile_lcdc_deferred_io_touch(info);
1801 static int sh_mobile_lcdc_pan(struct fb_var_screeninfo *var,
1802 struct fb_info *info)
1804 struct sh_mobile_lcdc_chan *ch = info->par;
1805 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
1806 unsigned long ldrcntr;
1807 unsigned long new_pan_offset;
1808 unsigned long base_addr_y, base_addr_c = 0;
1809 unsigned long c_offset;
1811 if (!ch->format->yuv)
1812 new_pan_offset = var->yoffset * ch->pitch
1813 + var->xoffset * (ch->format->bpp / 8);
1815 new_pan_offset = var->yoffset * ch->pitch + var->xoffset;
1817 if (new_pan_offset == ch->pan_offset)
1818 return 0; /* No change, do nothing */
1820 ldrcntr = lcdc_read(priv, _LDRCNTR);
1822 /* Set the source address for the next refresh */
1823 base_addr_y = ch->dma_handle + new_pan_offset;
1824 if (ch->format->yuv) {
1826 c_offset = var->yoffset * ch->pitch
1827 * (ch->format->bpp - 8) / 8;
1828 base_addr_c = ch->dma_handle + ch->xres * ch->yres_virtual
1831 if (ch->format->fourcc == V4L2_PIX_FMT_NV24)
1832 base_addr_c += 2 * var->xoffset;
1834 base_addr_c += var->xoffset;
1838 sh_mobile_meram_cache_update(priv->meram_dev, ch->cache,
1839 base_addr_y, base_addr_c,
1840 &base_addr_y, &base_addr_c);
1842 ch->base_addr_y = base_addr_y;
1843 ch->base_addr_c = base_addr_c;
1845 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
1846 if (ch->format->yuv)
1847 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
1849 if (lcdc_chan_is_sublcd(ch))
1850 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
1852 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
1854 ch->pan_offset = new_pan_offset;
1856 sh_mobile_lcdc_deferred_io_touch(info);
1861 static int sh_mobile_lcdc_ioctl(struct fb_info *info, unsigned int cmd,
1864 struct sh_mobile_lcdc_chan *ch = info->par;
1868 case FBIO_WAITFORVSYNC:
1869 retval = sh_mobile_lcdc_wait_for_vsync(ch);
1873 retval = -ENOIOCTLCMD;
1879 static void sh_mobile_fb_reconfig(struct fb_info *info)
1881 struct sh_mobile_lcdc_chan *ch = info->par;
1882 struct fb_var_screeninfo var;
1883 struct fb_videomode mode;
1884 struct fb_event event;
1885 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1887 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1888 /* More framebuffer users are active */
1891 fb_var_to_videomode(&mode, &info->var);
1893 if (fb_mode_is_equal(&ch->display.mode, &mode))
1896 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1898 fb_videomode_to_var(&var, &ch->display.mode);
1899 var.width = ch->display.width;
1900 var.height = ch->display.height;
1901 var.activate = FB_ACTIVATE_NOW;
1903 if (fb_set_var(info, &var) < 0)
1904 /* Couldn't reconfigure, hopefully, can continue as before */
1908 * fb_set_var() calls the notifier change internally, only if
1909 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1910 * user event, we have to call the chain ourselves.
1913 event.data = &ch->display.mode;
1914 fb_notifier_call_chain(evnt, &event);
1918 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1919 * user == 1, or with console sem held, if user == 0.
1921 static int sh_mobile_lcdc_release(struct fb_info *info, int user)
1923 struct sh_mobile_lcdc_chan *ch = info->par;
1925 mutex_lock(&ch->open_lock);
1926 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1930 /* Nothing to reconfigure, when called from fbcon */
1933 sh_mobile_fb_reconfig(info);
1937 mutex_unlock(&ch->open_lock);
1942 static int sh_mobile_lcdc_open(struct fb_info *info, int user)
1944 struct sh_mobile_lcdc_chan *ch = info->par;
1946 mutex_lock(&ch->open_lock);
1949 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1950 mutex_unlock(&ch->open_lock);
1955 static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
1956 struct fb_info *info)
1958 struct sh_mobile_lcdc_chan *ch = info->par;
1959 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1960 unsigned int best_dist = (unsigned int)-1;
1961 unsigned int best_xres = 0;
1962 unsigned int best_yres = 0;
1966 /* If board code provides us with a list of available modes, make sure
1967 * we use one of them. Find the mode closest to the requested one. The
1968 * distance between two modes is defined as the size of the
1969 * non-overlapping parts of the two rectangles.
1971 for (i = 0; i < ch->cfg->num_modes; ++i) {
1972 const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
1975 /* We can only round up. */
1976 if (var->xres > mode->xres || var->yres > mode->yres)
1979 dist = var->xres * var->yres + mode->xres * mode->yres
1980 - 2 * min(var->xres, mode->xres)
1981 * min(var->yres, mode->yres);
1983 if (dist < best_dist) {
1984 best_xres = mode->xres;
1985 best_yres = mode->yres;
1990 /* If no available mode can be used, return an error. */
1991 if (ch->cfg->num_modes != 0) {
1992 if (best_dist == (unsigned int)-1)
1995 var->xres = best_xres;
1996 var->yres = best_yres;
1999 ret = __sh_mobile_lcdc_check_var(var, info);
2003 /* only accept the forced_fourcc for dual channel configurations */
2004 if (p->forced_fourcc &&
2005 p->forced_fourcc != sh_mobile_format_fourcc(var))
2011 static int sh_mobile_lcdc_set_par(struct fb_info *info)
2013 struct sh_mobile_lcdc_chan *ch = info->par;
2016 sh_mobile_lcdc_stop(ch->lcdc);
2018 ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
2019 ch->colorspace = info->var.colorspace;
2021 ch->xres = info->var.xres;
2022 ch->xres_virtual = info->var.xres_virtual;
2023 ch->yres = info->var.yres;
2024 ch->yres_virtual = info->var.yres_virtual;
2026 if (ch->format->yuv)
2027 ch->pitch = info->var.xres;
2029 ch->pitch = info->var.xres * ch->format->bpp / 8;
2031 ret = sh_mobile_lcdc_start(ch->lcdc);
2033 dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
2035 info->fix.line_length = ch->pitch;
2037 if (sh_mobile_format_is_fourcc(&info->var)) {
2038 info->fix.type = FB_TYPE_FOURCC;
2039 info->fix.visual = FB_VISUAL_FOURCC;
2041 info->fix.type = FB_TYPE_PACKED_PIXELS;
2042 info->fix.visual = FB_VISUAL_TRUECOLOR;
2049 * Screen blanking. Behavior is as follows:
2050 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
2051 * FB_BLANK_NORMAL: screen blanked, clocks enabled
2054 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
2056 static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
2058 struct sh_mobile_lcdc_chan *ch = info->par;
2059 struct sh_mobile_lcdc_priv *p = ch->lcdc;
2061 /* blank the screen? */
2062 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
2063 struct fb_fillrect rect = {
2067 sh_mobile_lcdc_fillrect(info, &rect);
2069 /* turn clocks on? */
2070 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
2071 sh_mobile_lcdc_clk_on(p);
2073 /* turn clocks off? */
2074 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
2075 /* make sure the screen is updated with the black fill before
2076 * switching the clocks off. one vsync is not enough since
2077 * blanking may occur in the middle of a refresh. deferred io
2078 * mode will reenable the clocks and update the screen in time,
2079 * so it does not need this. */
2080 if (!info->fbdefio) {
2081 sh_mobile_lcdc_wait_for_vsync(ch);
2082 sh_mobile_lcdc_wait_for_vsync(ch);
2084 sh_mobile_lcdc_clk_off(p);
2087 ch->blank_status = blank;
2091 static struct fb_ops sh_mobile_lcdc_ops = {
2092 .owner = THIS_MODULE,
2093 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
2094 .fb_read = fb_sys_read,
2095 .fb_write = fb_sys_write,
2096 .fb_fillrect = sh_mobile_lcdc_fillrect,
2097 .fb_copyarea = sh_mobile_lcdc_copyarea,
2098 .fb_imageblit = sh_mobile_lcdc_imageblit,
2099 .fb_blank = sh_mobile_lcdc_blank,
2100 .fb_pan_display = sh_mobile_lcdc_pan,
2101 .fb_ioctl = sh_mobile_lcdc_ioctl,
2102 .fb_open = sh_mobile_lcdc_open,
2103 .fb_release = sh_mobile_lcdc_release,
2104 .fb_check_var = sh_mobile_lcdc_check_var,
2105 .fb_set_par = sh_mobile_lcdc_set_par,
2109 sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
2111 if (ch->info && ch->info->dev)
2112 unregister_framebuffer(ch->info);
2115 static int __devinit
2116 sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
2118 struct fb_info *info = ch->info;
2121 if (info->fbdefio) {
2122 ch->sglist = vmalloc(sizeof(struct scatterlist) *
2123 ch->fb_size >> PAGE_SHIFT);
2125 dev_err(ch->lcdc->dev, "cannot allocate sglist\n");
2130 info->bl_dev = ch->bl;
2132 ret = register_framebuffer(info);
2136 dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
2137 dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
2138 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
2139 info->var.bits_per_pixel);
2141 /* deferred io mode: disable clock to save power */
2142 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
2143 sh_mobile_lcdc_clk_off(ch->lcdc);
2149 sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
2151 struct fb_info *info = ch->info;
2153 if (!info || !info->device)
2159 fb_dealloc_cmap(&info->cmap);
2160 framebuffer_release(info);
2163 static int __devinit
2164 sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
2165 const struct fb_videomode *mode,
2166 unsigned int num_modes)
2168 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
2169 struct fb_var_screeninfo *var;
2170 struct fb_info *info;
2173 /* Allocate and initialize the frame buffer device. Create the modes
2174 * list and allocate the color map.
2176 info = framebuffer_alloc(0, priv->dev);
2178 dev_err(priv->dev, "unable to allocate fb_info\n");
2184 info->flags = FBINFO_FLAG_DEFAULT;
2185 info->fbops = &sh_mobile_lcdc_ops;
2186 info->device = priv->dev;
2187 info->screen_base = ch->fb_mem;
2188 info->pseudo_palette = &ch->pseudo_palette;
2191 fb_videomode_to_modelist(mode, num_modes, &info->modelist);
2193 ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
2195 dev_err(priv->dev, "unable to allocate cmap\n");
2199 /* Initialize fixed screen information. Restrict pan to 2 lines steps
2200 * for NV12 and NV21.
2202 info->fix = sh_mobile_lcdc_fix;
2203 info->fix.smem_start = ch->dma_handle;
2204 info->fix.smem_len = ch->fb_size;
2205 info->fix.line_length = ch->pitch;
2207 if (ch->format->yuv)
2208 info->fix.visual = FB_VISUAL_FOURCC;
2210 info->fix.visual = FB_VISUAL_TRUECOLOR;
2212 if (ch->format->fourcc == V4L2_PIX_FMT_NV12 ||
2213 ch->format->fourcc == V4L2_PIX_FMT_NV21)
2214 info->fix.ypanstep = 2;
2216 /* Initialize variable screen information using the first mode as
2217 * default. The default Y virtual resolution is twice the panel size to
2218 * allow for double-buffering.
2221 fb_videomode_to_var(var, mode);
2222 var->width = ch->cfg->panel_cfg.width;
2223 var->height = ch->cfg->panel_cfg.height;
2224 var->yres_virtual = var->yres * 2;
2225 var->activate = FB_ACTIVATE_NOW;
2227 /* Use the legacy API by default for RGB formats, and the FOURCC API
2230 if (!ch->format->yuv)
2231 var->bits_per_pixel = ch->format->bpp;
2233 var->grayscale = ch->format->fourcc;
2235 ret = sh_mobile_lcdc_check_var(var, info);
2242 /* -----------------------------------------------------------------------------
2246 static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
2248 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
2249 int brightness = bdev->props.brightness;
2251 if (bdev->props.power != FB_BLANK_UNBLANK ||
2252 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
2255 return ch->cfg->bl_info.set_brightness(brightness);
2258 static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
2260 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
2262 return ch->cfg->bl_info.get_brightness();
2265 static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
2266 struct fb_info *info)
2268 return (info->bl_dev == bdev);
2271 static struct backlight_ops sh_mobile_lcdc_bl_ops = {
2272 .options = BL_CORE_SUSPENDRESUME,
2273 .update_status = sh_mobile_lcdc_update_bl,
2274 .get_brightness = sh_mobile_lcdc_get_brightness,
2275 .check_fb = sh_mobile_lcdc_check_fb,
2278 static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
2279 struct sh_mobile_lcdc_chan *ch)
2281 struct backlight_device *bl;
2283 bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
2284 &sh_mobile_lcdc_bl_ops, NULL);
2286 dev_err(parent, "unable to register backlight device: %ld\n",
2291 bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
2292 bl->props.brightness = bl->props.max_brightness;
2293 backlight_update_status(bl);
2298 static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
2300 backlight_device_unregister(bdev);
2303 /* -----------------------------------------------------------------------------
2307 static int sh_mobile_lcdc_suspend(struct device *dev)
2309 struct platform_device *pdev = to_platform_device(dev);
2311 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
2315 static int sh_mobile_lcdc_resume(struct device *dev)
2317 struct platform_device *pdev = to_platform_device(dev);
2319 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
2322 static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
2324 struct platform_device *pdev = to_platform_device(dev);
2325 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
2327 /* turn off LCDC hardware */
2328 lcdc_write(priv, _LDCNT1R, 0);
2333 static int sh_mobile_lcdc_runtime_resume(struct device *dev)
2335 struct platform_device *pdev = to_platform_device(dev);
2336 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
2338 __sh_mobile_lcdc_start(priv);
2343 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
2344 .suspend = sh_mobile_lcdc_suspend,
2345 .resume = sh_mobile_lcdc_resume,
2346 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
2347 .runtime_resume = sh_mobile_lcdc_runtime_resume,
2350 /* -----------------------------------------------------------------------------
2351 * Framebuffer notifier
2354 /* locking: called with info->lock held */
2355 static int sh_mobile_lcdc_notify(struct notifier_block *nb,
2356 unsigned long action, void *data)
2358 struct fb_event *event = data;
2359 struct fb_info *info = event->info;
2360 struct sh_mobile_lcdc_chan *ch = info->par;
2362 if (&ch->lcdc->notifier != nb)
2365 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
2366 __func__, action, event->data);
2369 case FB_EVENT_SUSPEND:
2370 sh_mobile_lcdc_display_off(ch);
2371 sh_mobile_lcdc_stop(ch->lcdc);
2373 case FB_EVENT_RESUME:
2374 mutex_lock(&ch->open_lock);
2375 sh_mobile_fb_reconfig(info);
2376 mutex_unlock(&ch->open_lock);
2378 sh_mobile_lcdc_display_on(ch);
2379 sh_mobile_lcdc_start(ch->lcdc);
2385 /* -----------------------------------------------------------------------------
2386 * Probe/remove and driver init/exit
2389 static const struct fb_videomode default_720p __devinitconst = {
2390 .name = "HDMI 720p",
2395 .right_margin = 110,
2404 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
2407 static int sh_mobile_lcdc_remove(struct platform_device *pdev)
2409 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
2412 fb_unregister_client(&priv->notifier);
2414 for (i = 0; i < ARRAY_SIZE(priv->overlays); i++)
2415 sh_mobile_lcdc_overlay_fb_unregister(&priv->overlays[i]);
2416 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
2417 sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
2419 sh_mobile_lcdc_stop(priv);
2421 for (i = 0; i < ARRAY_SIZE(priv->overlays); i++) {
2422 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2424 sh_mobile_lcdc_overlay_fb_cleanup(ovl);
2427 dma_free_coherent(&pdev->dev, ovl->fb_size,
2428 ovl->fb_mem, ovl->dma_handle);
2431 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
2432 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
2435 ch->tx_dev->lcdc = NULL;
2436 module_put(ch->cfg->tx_dev->dev.driver->owner);
2439 sh_mobile_lcdc_channel_fb_cleanup(ch);
2442 dma_free_coherent(&pdev->dev, ch->fb_size,
2443 ch->fb_mem, ch->dma_handle);
2446 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
2448 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
2451 if (priv->dot_clk) {
2452 pm_runtime_disable(&pdev->dev);
2453 clk_put(priv->dot_clk);
2457 iounmap(priv->base);
2460 free_irq(priv->irq, priv);
2465 static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
2467 int interface_type = ch->cfg->interface_type;
2469 switch (interface_type) {
2493 /* SUBLCD only supports SYS interface */
2494 if (lcdc_chan_is_sublcd(ch)) {
2495 if (!(interface_type & LDMT1R_IFM))
2498 interface_type &= ~LDMT1R_IFM;
2501 ch->ldmt1r_value = interface_type;
2505 static int __devinit
2506 sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_priv *priv,
2507 struct sh_mobile_lcdc_overlay *ovl)
2509 const struct sh_mobile_lcdc_format_info *format;
2512 if (ovl->cfg->fourcc == 0)
2515 /* Validate the format. */
2516 format = sh_mobile_format_info(ovl->cfg->fourcc);
2517 if (format == NULL) {
2518 dev_err(priv->dev, "Invalid FOURCC %08x\n", ovl->cfg->fourcc);
2522 ovl->enabled = false;
2523 ovl->mode = LCDC_OVERLAY_BLEND;
2529 /* The default Y virtual resolution is twice the panel size to allow for
2532 ovl->format = format;
2533 ovl->xres = ovl->cfg->max_xres;
2534 ovl->xres_virtual = ovl->xres;
2535 ovl->yres = ovl->cfg->max_yres;
2536 ovl->yres_virtual = ovl->yres * 2;
2539 ovl->pitch = ovl->xres * format->bpp / 8;
2541 ovl->pitch = ovl->xres;
2543 /* Allocate frame buffer memory. */
2544 ovl->fb_size = ovl->cfg->max_xres * ovl->cfg->max_yres
2545 * format->bpp / 8 * 2;
2546 ovl->fb_mem = dma_alloc_coherent(priv->dev, ovl->fb_size,
2547 &ovl->dma_handle, GFP_KERNEL);
2549 dev_err(priv->dev, "unable to allocate buffer\n");
2553 ret = sh_mobile_lcdc_overlay_fb_init(ovl);
2560 static int __devinit
2561 sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
2562 struct sh_mobile_lcdc_chan *ch)
2564 const struct sh_mobile_lcdc_format_info *format;
2565 const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
2566 const struct fb_videomode *max_mode;
2567 const struct fb_videomode *mode;
2568 unsigned int num_modes;
2569 unsigned int max_size;
2572 mutex_init(&ch->open_lock);
2573 ch->notify = sh_mobile_lcdc_display_notify;
2575 /* Validate the format. */
2576 format = sh_mobile_format_info(cfg->fourcc);
2577 if (format == NULL) {
2578 dev_err(priv->dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
2582 /* Iterate through the modes to validate them and find the highest
2588 for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
2589 unsigned int size = mode->yres * mode->xres;
2591 /* NV12/NV21 buffers must have even number of lines */
2592 if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
2593 cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
2594 dev_err(priv->dev, "yres must be multiple of 2 for "
2595 "YCbCr420 mode.\n");
2599 if (size > max_size) {
2606 max_size = MAX_XRES * MAX_YRES;
2608 dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
2609 max_mode->xres, max_mode->yres);
2611 if (cfg->lcd_modes == NULL) {
2612 mode = &default_720p;
2615 mode = cfg->lcd_modes;
2616 num_modes = cfg->num_modes;
2619 /* Use the first mode as default. */
2620 ch->format = format;
2621 ch->xres = mode->xres;
2622 ch->xres_virtual = mode->xres;
2623 ch->yres = mode->yres;
2624 ch->yres_virtual = mode->yres * 2;
2627 ch->colorspace = V4L2_COLORSPACE_SRGB;
2628 ch->pitch = ch->xres * format->bpp / 8;
2630 ch->colorspace = V4L2_COLORSPACE_REC709;
2631 ch->pitch = ch->xres;
2634 ch->display.width = cfg->panel_cfg.width;
2635 ch->display.height = cfg->panel_cfg.height;
2636 ch->display.mode = *mode;
2638 /* Allocate frame buffer memory. */
2639 ch->fb_size = max_size * format->bpp / 8 * 2;
2640 ch->fb_mem = dma_alloc_coherent(priv->dev, ch->fb_size, &ch->dma_handle,
2642 if (ch->fb_mem == NULL) {
2643 dev_err(priv->dev, "unable to allocate buffer\n");
2647 /* Initialize the transmitter device if present. */
2649 if (!cfg->tx_dev->dev.driver ||
2650 !try_module_get(cfg->tx_dev->dev.driver->owner)) {
2652 "unable to get transmitter device\n");
2655 ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
2656 ch->tx_dev->lcdc = ch;
2657 ch->tx_dev->def_mode = *mode;
2660 return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
2663 static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
2665 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
2666 struct sh_mobile_lcdc_priv *priv;
2667 struct resource *res;
2673 dev_err(&pdev->dev, "no platform data defined\n");
2677 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2678 i = platform_get_irq(pdev, 0);
2679 if (!res || i < 0) {
2680 dev_err(&pdev->dev, "cannot get platform resources\n");
2684 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2686 dev_err(&pdev->dev, "cannot allocate device data\n");
2690 priv->dev = &pdev->dev;
2691 priv->meram_dev = pdata->meram_dev;
2692 platform_set_drvdata(pdev, priv);
2694 error = request_irq(i, sh_mobile_lcdc_irq, 0,
2695 dev_name(&pdev->dev), priv);
2697 dev_err(&pdev->dev, "unable to request irq\n");
2702 atomic_set(&priv->hw_usecnt, -1);
2704 for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
2705 struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
2708 ch->cfg = &pdata->ch[i];
2710 error = sh_mobile_lcdc_check_interface(ch);
2712 dev_err(&pdev->dev, "unsupported interface type\n");
2715 init_waitqueue_head(&ch->frame_end_wait);
2716 init_completion(&ch->vsync_completion);
2719 /* probe the backlight is there is one defined */
2720 if (ch->cfg->bl_info.max_brightness)
2721 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
2723 switch (pdata->ch[i].chan) {
2724 case LCDC_CHAN_MAINLCD:
2725 ch->enabled = LDCNT2R_ME;
2726 ch->reg_offs = lcdc_offs_mainlcd;
2729 case LCDC_CHAN_SUBLCD:
2730 ch->enabled = LDCNT2R_SE;
2731 ch->reg_offs = lcdc_offs_sublcd;
2737 if (!num_channels) {
2738 dev_err(&pdev->dev, "no channels defined\n");
2743 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
2744 if (num_channels == 2)
2745 priv->forced_fourcc = pdata->ch[0].fourcc;
2747 priv->base = ioremap_nocache(res->start, resource_size(res));
2751 error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
2753 dev_err(&pdev->dev, "unable to setup clocks\n");
2757 /* Enable runtime PM. */
2758 pm_runtime_enable(&pdev->dev);
2760 for (i = 0; i < num_channels; i++) {
2761 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
2763 error = sh_mobile_lcdc_channel_init(priv, ch);
2768 for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
2769 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2771 ovl->cfg = &pdata->overlays[i];
2772 ovl->channel = &priv->ch[0];
2774 error = sh_mobile_lcdc_overlay_init(priv, ovl);
2779 error = sh_mobile_lcdc_start(priv);
2781 dev_err(&pdev->dev, "unable to start hardware\n");
2785 for (i = 0; i < num_channels; i++) {
2786 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
2788 error = sh_mobile_lcdc_channel_fb_register(ch);
2793 for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
2794 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2796 error = sh_mobile_lcdc_overlay_fb_register(ovl);
2801 /* Failure ignored */
2802 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
2803 fb_register_client(&priv->notifier);
2807 sh_mobile_lcdc_remove(pdev);
2812 static struct platform_driver sh_mobile_lcdc_driver = {
2814 .name = "sh_mobile_lcdc_fb",
2815 .owner = THIS_MODULE,
2816 .pm = &sh_mobile_lcdc_dev_pm_ops,
2818 .probe = sh_mobile_lcdc_probe,
2819 .remove = sh_mobile_lcdc_remove,
2822 module_platform_driver(sh_mobile_lcdc_driver);
2824 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
2825 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
2826 MODULE_LICENSE("GPL v2");