linux/audit.h: move ptrace.h include to kernel header
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / video / sh_mobile_lcdcfb.c
blob63203acef812211cb17788454116fcbe77debdd7
1 /*
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
8 * for more details.
9 */
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>
23 #include <linux/mm.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
40 #define LDBCR 0xb00
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 {
143 LCDC_OVERLAY_BLEND,
144 LCDC_OVERLAY_ROP3,
148 * struct sh_mobile_lcdc_overlay - LCDC display overlay
150 * @channel: LCDC channel this overlay belongs to
151 * @cfg: Overlay configuration
152 * @info: Frame buffer device
153 * @index: Overlay index (0-3)
154 * @base: Overlay registers base address
155 * @enabled: True if the overlay is enabled
156 * @mode: Overlay blending mode (alpha blend or ROP3)
157 * @alpha: Global alpha blending value (0-255, for alpha blending mode)
158 * @rop3: Raster operation (for ROP3 mode)
159 * @fb_mem: Frame buffer virtual memory address
160 * @fb_size: Frame buffer size in bytes
161 * @dma_handle: Frame buffer DMA address
162 * @base_addr_y: Overlay base address (RGB or luma component)
163 * @base_addr_c: Overlay base address (chroma component)
164 * @pan_y_offset: Panning linear offset in bytes (luma component)
165 * @format: Current pixelf format
166 * @xres: Horizontal visible resolution
167 * @xres_virtual: Horizontal total resolution
168 * @yres: Vertical visible resolution
169 * @yres_virtual: Vertical total resolution
170 * @pitch: Overlay line pitch
171 * @pos_x: Horizontal overlay position
172 * @pos_y: Vertical overlay position
174 struct sh_mobile_lcdc_overlay {
175 struct sh_mobile_lcdc_chan *channel;
177 const struct sh_mobile_lcdc_overlay_cfg *cfg;
178 struct fb_info *info;
180 unsigned int index;
181 unsigned long base;
183 bool enabled;
184 enum sh_mobile_lcdc_overlay_mode mode;
185 unsigned int alpha;
186 unsigned int rop3;
188 void *fb_mem;
189 unsigned long fb_size;
191 dma_addr_t dma_handle;
192 unsigned long base_addr_y;
193 unsigned long base_addr_c;
194 unsigned long pan_y_offset;
196 const struct sh_mobile_lcdc_format_info *format;
197 unsigned int xres;
198 unsigned int xres_virtual;
199 unsigned int yres;
200 unsigned int yres_virtual;
201 unsigned int pitch;
202 int pos_x;
203 int pos_y;
206 struct sh_mobile_lcdc_priv {
207 void __iomem *base;
208 int irq;
209 atomic_t hw_usecnt;
210 struct device *dev;
211 struct clk *dot_clk;
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;
218 int started;
219 int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
220 struct sh_mobile_meram_info *meram_dev;
223 /* -----------------------------------------------------------------------------
224 * Registers access
227 static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
228 [LDDCKPAT1R] = 0x400,
229 [LDDCKPAT2R] = 0x404,
230 [LDMT1R] = 0x418,
231 [LDMT2R] = 0x41c,
232 [LDMT3R] = 0x420,
233 [LDDFR] = 0x424,
234 [LDSM1R] = 0x428,
235 [LDSM2R] = 0x42c,
236 [LDSA1R] = 0x430,
237 [LDSA2R] = 0x434,
238 [LDMLSR] = 0x438,
239 [LDHCNR] = 0x448,
240 [LDHSYNR] = 0x44c,
241 [LDVLNR] = 0x450,
242 [LDVSYNR] = 0x454,
243 [LDPMR] = 0x460,
244 [LDHAJR] = 0x4a0,
247 static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
248 [LDDCKPAT1R] = 0x408,
249 [LDDCKPAT2R] = 0x40c,
250 [LDMT1R] = 0x600,
251 [LDMT2R] = 0x604,
252 [LDMT3R] = 0x608,
253 [LDDFR] = 0x60c,
254 [LDSM1R] = 0x610,
255 [LDSM2R] = 0x614,
256 [LDSA1R] = 0x618,
257 [LDMLSR] = 0x620,
258 [LDHCNR] = 0x624,
259 [LDHSYNR] = 0x628,
260 [LDVLNR] = 0x62c,
261 [LDVSYNR] = 0x630,
262 [LDPMR] = 0x63c,
265 static bool banked(int reg_nr)
267 switch (reg_nr) {
268 case LDMT1R:
269 case LDMT2R:
270 case LDMT3R:
271 case LDDFR:
272 case LDSM1R:
273 case LDSA1R:
274 case LDSA2R:
275 case LDMLSR:
276 case LDHCNR:
277 case LDHSYNR:
278 case LDVLNR:
279 case LDVSYNR:
280 return true;
282 return false;
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]);
294 if (banked(reg_nr))
295 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
296 SIDE_B_OFFSET);
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] +
303 MIRROR_OFFSET);
306 static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
307 int reg_nr)
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)
336 cpu_relax();
339 /* -----------------------------------------------------------------------------
340 * Clock management
343 static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
345 if (atomic_inc_and_test(&priv->hw_usecnt)) {
346 if (priv->dot_clk)
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);
360 if (priv->dot_clk)
361 clk_disable(priv->dot_clk);
365 static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
366 int clock_source)
368 struct clk *clk;
369 char *str;
371 switch (clock_source) {
372 case LCDC_CLK_BUS:
373 str = "bus_clk";
374 priv->lddckr = LDDCKR_ICKSEL_BUS;
375 break;
376 case LCDC_CLK_PERIPHERAL:
377 str = "peripheral_clk";
378 priv->lddckr = LDDCKR_ICKSEL_MIPI;
379 break;
380 case LCDC_CLK_EXTERNAL:
381 str = NULL;
382 priv->lddckr = LDDCKR_ICKSEL_HDMI;
383 break;
384 default:
385 return -EINVAL;
388 if (str == NULL)
389 return 0;
391 clk = clk_get(priv->dev, str);
392 if (IS_ERR(clk)) {
393 dev_err(priv->dev, "cannot get dot clock %s\n", str);
394 return PTR_ERR(clk);
397 priv->dot_clk = clk;
398 return 0;
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));
435 udelay(1);
436 lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
438 return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
441 static struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
442 lcdc_sys_write_index,
443 lcdc_sys_write_data,
444 lcdc_sys_read_data,
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;
452 struct page *page;
453 int nr_pages = 0;
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);
460 return nr_pages;
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,
496 DMA_TO_DEVICE);
497 } else {
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;
508 if (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;
516 if (ch->tx_dev) {
517 int ret;
519 ret = ch->tx_dev->ops->display_on(ch->tx_dev);
520 if (ret < 0)
521 return;
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)
529 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();
539 if (ch->tx_dev)
540 ch->tx_dev->ops->display_off(ch->tx_dev);
543 static bool
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))
553 return false;
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;
559 return true;
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;
572 int ret = 0;
574 switch (event) {
575 case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
576 /* HDMI plug in */
577 if (lock_fb_info(info)) {
578 console_lock();
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
587 * logo disappears.
589 info->var.width = ch->display.width;
590 info->var.height = ch->display.height;
591 sh_mobile_lcdc_display_on(ch);
592 } else {
593 /* New monitor or have to wake up */
594 fb_set_suspend(info, 0);
597 console_unlock();
598 unlock_fb_info(info);
600 break;
602 case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT:
603 /* HDMI disconnect */
604 if (lock_fb_info(info)) {
605 console_lock();
606 fb_set_suspend(info, 1);
607 console_unlock();
608 unlock_fb_info(info);
610 break;
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);
618 break;
621 return ret;
624 /* -----------------------------------------------------------------------------
625 * Format helpers
628 struct sh_mobile_lcdc_format_info {
629 u32 fourcc;
630 unsigned int bpp;
631 bool yuv;
632 u32 lddfr;
635 static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos[] = {
637 .fourcc = V4L2_PIX_FMT_RGB565,
638 .bpp = 16,
639 .yuv = false,
640 .lddfr = LDDFR_PKF_RGB16,
641 }, {
642 .fourcc = V4L2_PIX_FMT_BGR24,
643 .bpp = 24,
644 .yuv = false,
645 .lddfr = LDDFR_PKF_RGB24,
646 }, {
647 .fourcc = V4L2_PIX_FMT_BGR32,
648 .bpp = 32,
649 .yuv = false,
650 .lddfr = LDDFR_PKF_ARGB32,
651 }, {
652 .fourcc = V4L2_PIX_FMT_NV12,
653 .bpp = 12,
654 .yuv = true,
655 .lddfr = LDDFR_CC | LDDFR_YF_420,
656 }, {
657 .fourcc = V4L2_PIX_FMT_NV21,
658 .bpp = 12,
659 .yuv = true,
660 .lddfr = LDDFR_CC | LDDFR_YF_420,
661 }, {
662 .fourcc = V4L2_PIX_FMT_NV16,
663 .bpp = 16,
664 .yuv = true,
665 .lddfr = LDDFR_CC | LDDFR_YF_422,
666 }, {
667 .fourcc = V4L2_PIX_FMT_NV61,
668 .bpp = 16,
669 .yuv = true,
670 .lddfr = LDDFR_CC | LDDFR_YF_422,
671 }, {
672 .fourcc = V4L2_PIX_FMT_NV24,
673 .bpp = 24,
674 .yuv = true,
675 .lddfr = LDDFR_CC | LDDFR_YF_444,
676 }, {
677 .fourcc = V4L2_PIX_FMT_NV42,
678 .bpp = 24,
679 .yuv = true,
680 .lddfr = LDDFR_CC | LDDFR_YF_444,
684 static const struct sh_mobile_lcdc_format_info *
685 sh_mobile_format_info(u32 fourcc)
687 unsigned int i;
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];
694 return NULL;
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) {
703 case 16:
704 return V4L2_PIX_FMT_RGB565;
705 case 24:
706 return V4L2_PIX_FMT_BGR24;
707 case 32:
708 return V4L2_PIX_FMT_BGR32;
709 default:
710 return 0;
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;
728 int is_sub;
729 int k;
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++) {
740 ch = &priv->ch[k];
742 if (!ch->enabled)
743 continue;
745 /* Frame End */
746 if (ldintr & LDINTR_FS) {
747 if (is_sub == lcdc_chan_is_sublcd(ch)) {
748 ch->frame_end = 1;
749 wake_up(&ch->frame_end_wait);
751 sh_mobile_lcdc_clk_off(priv);
755 /* VSYNC End */
756 if (ldintr & LDINTR_VES)
757 complete(&ch->vsync_completion);
760 return IRQ_HANDLED;
763 static int sh_mobile_lcdc_wait_for_vsync(struct sh_mobile_lcdc_chan *ch)
765 unsigned long ldintr;
766 int ret;
768 /* Enable VSync End interrupt and be careful not to acknowledge any
769 * pending interrupt.
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));
777 if (!ret)
778 return -ETIMEDOUT;
780 return 0;
783 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
784 int start)
786 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
787 int k;
789 /* start or stop the lcdc */
790 if (start)
791 lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
792 else
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)
798 while (1) {
799 tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
800 & LDPMR_LPS;
801 if (start && tmp == LDPMR_LPS)
802 break;
803 if (!start && tmp == 0)
804 break;
805 cpu_relax();
808 if (!start)
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;
817 u32 tmp;
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);
829 /* setup SYS bus */
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)
865 u32 format = 0;
867 if (!ovl->enabled) {
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));
872 return;
875 ovl->base_addr_y = ovl->dma_handle;
876 ovl->base_addr_c = ovl->dma_handle
877 + ovl->xres_virtual * ovl->yres_virtual;
879 switch (ovl->mode) {
880 case LCDC_OVERLAY_BLEND:
881 format = LDBBSIFR_EN | (ovl->alpha << LDBBSIFR_LAY_SHIFT);
882 break;
884 case LCDC_OVERLAY_ROP3:
885 format = LDBBSIFR_EN | LDBBSIFR_BRSEL
886 | (ovl->rop3 << LDBBSIFR_ROP3_SHIFT);
887 break;
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;
896 break;
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;
902 break;
903 case V4L2_PIX_FMT_BGR32:
904 default:
905 format |= LDBBSIFR_SWPL;
906 break;
909 switch (ovl->format->fourcc) {
910 case V4L2_PIX_FMT_RGB565:
911 format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB16;
912 break;
913 case V4L2_PIX_FMT_BGR24:
914 format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB24;
915 break;
916 case V4L2_PIX_FMT_BGR32:
917 format |= LDBBSIFR_AL_PK | LDBBSIFR_RY | LDDFR_PKF_ARGB32;
918 break;
919 case V4L2_PIX_FMT_NV12:
920 case V4L2_PIX_FMT_NV21:
921 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_420;
922 break;
923 case V4L2_PIX_FMT_NV16:
924 case V4L2_PIX_FMT_NV61:
925 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_422;
926 break;
927 case V4L2_PIX_FMT_NV24:
928 case V4L2_PIX_FMT_NV42:
929 format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_444;
930 break;
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
955 * @priv: LCDC device
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;
963 unsigned long tmp;
964 int k, m;
966 /* Enable LCDC channels. Read data from external memory, avoid using the
967 * BEU for now.
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. */
976 tmp = priv->lddckr;
977 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
978 ch = &priv->ch[k];
979 if (!ch->enabled)
980 continue;
982 /* Power supply */
983 lcdc_write_chan(ch, LDPMR, 0);
985 m = ch->cfg->clock_divider;
986 if (!m)
987 continue;
989 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
990 * denominator.
992 lcdc_write_chan(ch, LDDCKPAT1R, 0);
993 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
995 if (m == 1)
996 m = LDDCKR_MOSEL;
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++) {
1006 ch = &priv->ch[k];
1007 if (!ch->enabled)
1008 continue;
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:
1017 tmp |= LDDFR_CF1;
1018 break;
1019 case V4L2_COLORSPACE_JPEG:
1020 tmp |= LDDFR_CF0;
1021 break;
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);
1039 } else {
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;
1051 break;
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;
1057 break;
1058 case V4L2_PIX_FMT_BGR32:
1059 default:
1060 tmp = LDDDSR_LS;
1061 break;
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);
1068 priv->started = 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;
1075 unsigned long tmp;
1076 int ret;
1077 int k;
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);
1085 /* reset */
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;
1092 ch = &priv->ch[k];
1093 if (!ch->enabled)
1094 continue;
1096 panel = &ch->cfg->panel_cfg;
1097 if (panel->setup_sys) {
1098 ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
1099 if (ret)
1100 return ret;
1104 /* Compute frame buffer base address and pitch for each channel. */
1105 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1106 int pixelformat;
1107 void *cache;
1109 ch = &priv->ch[k];
1110 if (!ch->enabled)
1111 continue;
1113 ch->base_addr_y = ch->dma_handle;
1114 ch->base_addr_c = ch->dma_handle
1115 + ch->xres_virtual * ch->yres_virtual;
1116 ch->line_size = ch->pitch;
1118 /* Enable MERAM if possible. */
1119 if (mdev == NULL || ch->cfg->meram_cfg == NULL)
1120 continue;
1122 /* Free the allocated MERAM cache. */
1123 if (ch->cache) {
1124 sh_mobile_meram_cache_free(mdev, ch->cache);
1125 ch->cache = NULL;
1128 switch (ch->format->fourcc) {
1129 case V4L2_PIX_FMT_NV12:
1130 case V4L2_PIX_FMT_NV21:
1131 case V4L2_PIX_FMT_NV16:
1132 case V4L2_PIX_FMT_NV61:
1133 pixelformat = SH_MOBILE_MERAM_PF_NV;
1134 break;
1135 case V4L2_PIX_FMT_NV24:
1136 case V4L2_PIX_FMT_NV42:
1137 pixelformat = SH_MOBILE_MERAM_PF_NV24;
1138 break;
1139 case V4L2_PIX_FMT_RGB565:
1140 case V4L2_PIX_FMT_BGR24:
1141 case V4L2_PIX_FMT_BGR32:
1142 default:
1143 pixelformat = SH_MOBILE_MERAM_PF_RGB;
1144 break;
1147 cache = sh_mobile_meram_cache_alloc(mdev, ch->cfg->meram_cfg,
1148 ch->pitch, ch->yres, pixelformat,
1149 &ch->line_size);
1150 if (!IS_ERR(cache)) {
1151 sh_mobile_meram_cache_update(mdev, cache,
1152 ch->base_addr_y, ch->base_addr_c,
1153 &ch->base_addr_y, &ch->base_addr_c);
1154 ch->cache = cache;
1158 for (k = 0; k < ARRAY_SIZE(priv->overlays); ++k) {
1159 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[k];
1160 sh_mobile_lcdc_overlay_setup(ovl);
1163 /* Start the LCDC. */
1164 __sh_mobile_lcdc_start(priv);
1166 /* Setup deferred I/O, tell the board code to enable the panels, and
1167 * turn backlight on.
1169 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1170 ch = &priv->ch[k];
1171 if (!ch->enabled)
1172 continue;
1174 tmp = ch->cfg->sys_bus_cfg.deferred_io_msec;
1175 if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
1176 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
1177 ch->defio.delay = msecs_to_jiffies(tmp);
1178 ch->info->fbdefio = &ch->defio;
1179 fb_deferred_io_init(ch->info);
1182 sh_mobile_lcdc_display_on(ch);
1184 if (ch->bl) {
1185 ch->bl->props.power = FB_BLANK_UNBLANK;
1186 backlight_update_status(ch->bl);
1190 return 0;
1193 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
1195 struct sh_mobile_lcdc_chan *ch;
1196 int k;
1198 /* clean up deferred io and ask board code to disable panel */
1199 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1200 ch = &priv->ch[k];
1201 if (!ch->enabled)
1202 continue;
1204 /* deferred io mode:
1205 * flush frame, and wait for frame end interrupt
1206 * clean up deferred io and enable clock
1208 if (ch->info && ch->info->fbdefio) {
1209 ch->frame_end = 0;
1210 schedule_delayed_work(&ch->info->deferred_work, 0);
1211 wait_event(ch->frame_end_wait, ch->frame_end);
1212 fb_deferred_io_cleanup(ch->info);
1213 ch->info->fbdefio = NULL;
1214 sh_mobile_lcdc_clk_on(priv);
1217 if (ch->bl) {
1218 ch->bl->props.power = FB_BLANK_POWERDOWN;
1219 backlight_update_status(ch->bl);
1222 sh_mobile_lcdc_display_off(ch);
1224 /* Free the MERAM cache. */
1225 if (ch->cache) {
1226 sh_mobile_meram_cache_free(priv->meram_dev, ch->cache);
1227 ch->cache = 0;
1232 /* stop the lcdc */
1233 if (priv->started) {
1234 sh_mobile_lcdc_start_stop(priv, 0);
1235 priv->started = 0;
1238 /* stop clocks */
1239 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
1240 if (priv->ch[k].enabled)
1241 sh_mobile_lcdc_clk_off(priv);
1244 static int __sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
1245 struct fb_info *info)
1247 if (var->xres > MAX_XRES || var->yres > MAX_YRES)
1248 return -EINVAL;
1250 /* Make sure the virtual resolution is at least as big as the visible
1251 * resolution.
1253 if (var->xres_virtual < var->xres)
1254 var->xres_virtual = var->xres;
1255 if (var->yres_virtual < var->yres)
1256 var->yres_virtual = var->yres;
1258 if (sh_mobile_format_is_fourcc(var)) {
1259 const struct sh_mobile_lcdc_format_info *format;
1261 format = sh_mobile_format_info(var->grayscale);
1262 if (format == NULL)
1263 return -EINVAL;
1264 var->bits_per_pixel = format->bpp;
1266 /* Default to RGB and JPEG color-spaces for RGB and YUV formats
1267 * respectively.
1269 if (!format->yuv)
1270 var->colorspace = V4L2_COLORSPACE_SRGB;
1271 else if (var->colorspace != V4L2_COLORSPACE_REC709)
1272 var->colorspace = V4L2_COLORSPACE_JPEG;
1273 } else {
1274 if (var->bits_per_pixel <= 16) { /* RGB 565 */
1275 var->bits_per_pixel = 16;
1276 var->red.offset = 11;
1277 var->red.length = 5;
1278 var->green.offset = 5;
1279 var->green.length = 6;
1280 var->blue.offset = 0;
1281 var->blue.length = 5;
1282 var->transp.offset = 0;
1283 var->transp.length = 0;
1284 } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
1285 var->bits_per_pixel = 24;
1286 var->red.offset = 16;
1287 var->red.length = 8;
1288 var->green.offset = 8;
1289 var->green.length = 8;
1290 var->blue.offset = 0;
1291 var->blue.length = 8;
1292 var->transp.offset = 0;
1293 var->transp.length = 0;
1294 } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
1295 var->bits_per_pixel = 32;
1296 var->red.offset = 16;
1297 var->red.length = 8;
1298 var->green.offset = 8;
1299 var->green.length = 8;
1300 var->blue.offset = 0;
1301 var->blue.length = 8;
1302 var->transp.offset = 24;
1303 var->transp.length = 8;
1304 } else
1305 return -EINVAL;
1307 var->red.msb_right = 0;
1308 var->green.msb_right = 0;
1309 var->blue.msb_right = 0;
1310 var->transp.msb_right = 0;
1313 /* Make sure we don't exceed our allocated memory. */
1314 if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1315 info->fix.smem_len)
1316 return -EINVAL;
1318 return 0;
1321 /* -----------------------------------------------------------------------------
1322 * Frame buffer operations - Overlays
1325 static ssize_t
1326 overlay_alpha_show(struct device *dev, struct device_attribute *attr, char *buf)
1328 struct fb_info *info = dev_get_drvdata(dev);
1329 struct sh_mobile_lcdc_overlay *ovl = info->par;
1331 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->alpha);
1334 static ssize_t
1335 overlay_alpha_store(struct device *dev, struct device_attribute *attr,
1336 const char *buf, size_t count)
1338 struct fb_info *info = dev_get_drvdata(dev);
1339 struct sh_mobile_lcdc_overlay *ovl = info->par;
1340 unsigned int alpha;
1341 char *endp;
1343 alpha = simple_strtoul(buf, &endp, 10);
1344 if (isspace(*endp))
1345 endp++;
1347 if (endp - buf != count)
1348 return -EINVAL;
1350 if (alpha > 255)
1351 return -EINVAL;
1353 if (ovl->alpha != alpha) {
1354 ovl->alpha = alpha;
1356 if (ovl->mode == LCDC_OVERLAY_BLEND && ovl->enabled)
1357 sh_mobile_lcdc_overlay_setup(ovl);
1360 return count;
1363 static ssize_t
1364 overlay_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1366 struct fb_info *info = dev_get_drvdata(dev);
1367 struct sh_mobile_lcdc_overlay *ovl = info->par;
1369 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->mode);
1372 static ssize_t
1373 overlay_mode_store(struct device *dev, struct device_attribute *attr,
1374 const char *buf, size_t count)
1376 struct fb_info *info = dev_get_drvdata(dev);
1377 struct sh_mobile_lcdc_overlay *ovl = info->par;
1378 unsigned int mode;
1379 char *endp;
1381 mode = simple_strtoul(buf, &endp, 10);
1382 if (isspace(*endp))
1383 endp++;
1385 if (endp - buf != count)
1386 return -EINVAL;
1388 if (mode != LCDC_OVERLAY_BLEND && mode != LCDC_OVERLAY_ROP3)
1389 return -EINVAL;
1391 if (ovl->mode != mode) {
1392 ovl->mode = mode;
1394 if (ovl->enabled)
1395 sh_mobile_lcdc_overlay_setup(ovl);
1398 return count;
1401 static ssize_t
1402 overlay_position_show(struct device *dev, struct device_attribute *attr,
1403 char *buf)
1405 struct fb_info *info = dev_get_drvdata(dev);
1406 struct sh_mobile_lcdc_overlay *ovl = info->par;
1408 return scnprintf(buf, PAGE_SIZE, "%d,%d\n", ovl->pos_x, ovl->pos_y);
1411 static ssize_t
1412 overlay_position_store(struct device *dev, struct device_attribute *attr,
1413 const char *buf, size_t count)
1415 struct fb_info *info = dev_get_drvdata(dev);
1416 struct sh_mobile_lcdc_overlay *ovl = info->par;
1417 char *endp;
1418 int pos_x;
1419 int pos_y;
1421 pos_x = simple_strtol(buf, &endp, 10);
1422 if (*endp != ',')
1423 return -EINVAL;
1425 pos_y = simple_strtol(endp + 1, &endp, 10);
1426 if (isspace(*endp))
1427 endp++;
1429 if (endp - buf != count)
1430 return -EINVAL;
1432 if (ovl->pos_x != pos_x || ovl->pos_y != pos_y) {
1433 ovl->pos_x = pos_x;
1434 ovl->pos_y = pos_y;
1436 if (ovl->enabled)
1437 sh_mobile_lcdc_overlay_setup(ovl);
1440 return count;
1443 static ssize_t
1444 overlay_rop3_show(struct device *dev, struct device_attribute *attr, char *buf)
1446 struct fb_info *info = dev_get_drvdata(dev);
1447 struct sh_mobile_lcdc_overlay *ovl = info->par;
1449 return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->rop3);
1452 static ssize_t
1453 overlay_rop3_store(struct device *dev, struct device_attribute *attr,
1454 const char *buf, size_t count)
1456 struct fb_info *info = dev_get_drvdata(dev);
1457 struct sh_mobile_lcdc_overlay *ovl = info->par;
1458 unsigned int rop3;
1459 char *endp;
1461 rop3 = !!simple_strtoul(buf, &endp, 10);
1462 if (isspace(*endp))
1463 endp++;
1465 if (endp - buf != count)
1466 return -EINVAL;
1468 if (rop3 > 255)
1469 return -EINVAL;
1471 if (ovl->rop3 != rop3) {
1472 ovl->rop3 = rop3;
1474 if (ovl->mode == LCDC_OVERLAY_ROP3 && ovl->enabled)
1475 sh_mobile_lcdc_overlay_setup(ovl);
1478 return count;
1481 static const struct device_attribute overlay_sysfs_attrs[] = {
1482 __ATTR(ovl_alpha, S_IRUGO|S_IWUSR,
1483 overlay_alpha_show, overlay_alpha_store),
1484 __ATTR(ovl_mode, S_IRUGO|S_IWUSR,
1485 overlay_mode_show, overlay_mode_store),
1486 __ATTR(ovl_position, S_IRUGO|S_IWUSR,
1487 overlay_position_show, overlay_position_store),
1488 __ATTR(ovl_rop3, S_IRUGO|S_IWUSR,
1489 overlay_rop3_show, overlay_rop3_store),
1492 static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix = {
1493 .id = "SH Mobile LCDC",
1494 .type = FB_TYPE_PACKED_PIXELS,
1495 .visual = FB_VISUAL_TRUECOLOR,
1496 .accel = FB_ACCEL_NONE,
1497 .xpanstep = 1,
1498 .ypanstep = 1,
1499 .ywrapstep = 0,
1500 .capabilities = FB_CAP_FOURCC,
1503 static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo *var,
1504 struct fb_info *info)
1506 struct sh_mobile_lcdc_overlay *ovl = info->par;
1507 unsigned long base_addr_y;
1508 unsigned long base_addr_c;
1509 unsigned long y_offset;
1510 unsigned long c_offset;
1512 if (!ovl->format->yuv) {
1513 y_offset = (var->yoffset * ovl->xres_virtual + var->xoffset)
1514 * ovl->format->bpp / 8;
1515 c_offset = 0;
1516 } else {
1517 unsigned int xsub = ovl->format->bpp < 24 ? 2 : 1;
1518 unsigned int ysub = ovl->format->bpp < 16 ? 2 : 1;
1520 y_offset = var->yoffset * ovl->xres_virtual + var->xoffset;
1521 c_offset = var->yoffset / ysub * ovl->xres_virtual * 2 / xsub
1522 + var->xoffset * 2 / xsub;
1525 /* If the Y offset hasn't changed, the C offset hasn't either. There's
1526 * nothing to do in that case.
1528 if (y_offset == ovl->pan_y_offset)
1529 return 0;
1531 /* Set the source address for the next refresh */
1532 base_addr_y = ovl->dma_handle + y_offset;
1533 base_addr_c = ovl->dma_handle + ovl->xres_virtual * ovl->yres_virtual
1534 + c_offset;
1536 ovl->base_addr_y = base_addr_y;
1537 ovl->base_addr_c = base_addr_c;
1538 ovl->pan_y_offset = y_offset;
1540 lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
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 lcdc_write(ovl->channel->lcdc, LDBCR,
1546 LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
1548 return 0;
1551 static int sh_mobile_lcdc_overlay_ioctl(struct fb_info *info, unsigned int cmd,
1552 unsigned long arg)
1554 struct sh_mobile_lcdc_overlay *ovl = info->par;
1556 switch (cmd) {
1557 case FBIO_WAITFORVSYNC:
1558 return sh_mobile_lcdc_wait_for_vsync(ovl->channel);
1560 default:
1561 return -ENOIOCTLCMD;
1565 static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo *var,
1566 struct fb_info *info)
1568 return __sh_mobile_lcdc_check_var(var, info);
1571 static int sh_mobile_lcdc_overlay_set_par(struct fb_info *info)
1573 struct sh_mobile_lcdc_overlay *ovl = info->par;
1575 ovl->format =
1576 sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
1578 ovl->xres = info->var.xres;
1579 ovl->xres_virtual = info->var.xres_virtual;
1580 ovl->yres = info->var.yres;
1581 ovl->yres_virtual = info->var.yres_virtual;
1583 if (ovl->format->yuv)
1584 ovl->pitch = info->var.xres_virtual;
1585 else
1586 ovl->pitch = info->var.xres_virtual * ovl->format->bpp / 8;
1588 sh_mobile_lcdc_overlay_setup(ovl);
1590 info->fix.line_length = ovl->pitch;
1592 if (sh_mobile_format_is_fourcc(&info->var)) {
1593 info->fix.type = FB_TYPE_FOURCC;
1594 info->fix.visual = FB_VISUAL_FOURCC;
1595 } else {
1596 info->fix.type = FB_TYPE_PACKED_PIXELS;
1597 info->fix.visual = FB_VISUAL_TRUECOLOR;
1600 return 0;
1603 /* Overlay blanking. Disable the overlay when blanked. */
1604 static int sh_mobile_lcdc_overlay_blank(int blank, struct fb_info *info)
1606 struct sh_mobile_lcdc_overlay *ovl = info->par;
1608 ovl->enabled = !blank;
1609 sh_mobile_lcdc_overlay_setup(ovl);
1611 /* Prevent the backlight from receiving a blanking event by returning
1612 * a non-zero value.
1614 return 1;
1617 static int
1618 sh_mobile_lcdc_overlay_mmap(struct fb_info *info, struct vm_area_struct *vma)
1620 struct sh_mobile_lcdc_overlay *ovl = info->par;
1622 return dma_mmap_coherent(ovl->channel->lcdc->dev, vma, ovl->fb_mem,
1623 ovl->dma_handle, ovl->fb_size);
1626 static struct fb_ops sh_mobile_lcdc_overlay_ops = {
1627 .owner = THIS_MODULE,
1628 .fb_read = fb_sys_read,
1629 .fb_write = fb_sys_write,
1630 .fb_fillrect = sys_fillrect,
1631 .fb_copyarea = sys_copyarea,
1632 .fb_imageblit = sys_imageblit,
1633 .fb_blank = sh_mobile_lcdc_overlay_blank,
1634 .fb_pan_display = sh_mobile_lcdc_overlay_pan,
1635 .fb_ioctl = sh_mobile_lcdc_overlay_ioctl,
1636 .fb_check_var = sh_mobile_lcdc_overlay_check_var,
1637 .fb_set_par = sh_mobile_lcdc_overlay_set_par,
1638 .fb_mmap = sh_mobile_lcdc_overlay_mmap,
1641 static void
1642 sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay *ovl)
1644 struct fb_info *info = ovl->info;
1646 if (info == NULL || info->dev == NULL)
1647 return;
1649 unregister_framebuffer(ovl->info);
1652 static int
1653 sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay *ovl)
1655 struct sh_mobile_lcdc_priv *lcdc = ovl->channel->lcdc;
1656 struct fb_info *info = ovl->info;
1657 unsigned int i;
1658 int ret;
1660 if (info == NULL)
1661 return 0;
1663 ret = register_framebuffer(info);
1664 if (ret < 0)
1665 return ret;
1667 dev_info(lcdc->dev, "registered %s/overlay %u as %dx%d %dbpp.\n",
1668 dev_name(lcdc->dev), ovl->index, info->var.xres,
1669 info->var.yres, info->var.bits_per_pixel);
1671 for (i = 0; i < ARRAY_SIZE(overlay_sysfs_attrs); ++i) {
1672 ret = device_create_file(info->dev, &overlay_sysfs_attrs[i]);
1673 if (ret < 0)
1674 return ret;
1677 return 0;
1680 static void
1681 sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay *ovl)
1683 struct fb_info *info = ovl->info;
1685 if (info == NULL || info->device == NULL)
1686 return;
1688 framebuffer_release(info);
1691 static int
1692 sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay *ovl)
1694 struct sh_mobile_lcdc_priv *priv = ovl->channel->lcdc;
1695 struct fb_var_screeninfo *var;
1696 struct fb_info *info;
1698 /* Allocate and initialize the frame buffer device. */
1699 info = framebuffer_alloc(0, priv->dev);
1700 if (info == NULL) {
1701 dev_err(priv->dev, "unable to allocate fb_info\n");
1702 return -ENOMEM;
1705 ovl->info = info;
1707 info->flags = FBINFO_FLAG_DEFAULT;
1708 info->fbops = &sh_mobile_lcdc_overlay_ops;
1709 info->device = priv->dev;
1710 info->screen_base = ovl->fb_mem;
1711 info->par = ovl;
1713 /* Initialize fixed screen information. Restrict pan to 2 lines steps
1714 * for NV12 and NV21.
1716 info->fix = sh_mobile_lcdc_overlay_fix;
1717 snprintf(info->fix.id, sizeof(info->fix.id),
1718 "SH Mobile LCDC Overlay %u", ovl->index);
1719 info->fix.smem_start = ovl->dma_handle;
1720 info->fix.smem_len = ovl->fb_size;
1721 info->fix.line_length = ovl->pitch;
1723 if (ovl->format->yuv)
1724 info->fix.visual = FB_VISUAL_FOURCC;
1725 else
1726 info->fix.visual = FB_VISUAL_TRUECOLOR;
1728 switch (ovl->format->fourcc) {
1729 case V4L2_PIX_FMT_NV12:
1730 case V4L2_PIX_FMT_NV21:
1731 info->fix.ypanstep = 2;
1732 case V4L2_PIX_FMT_NV16:
1733 case V4L2_PIX_FMT_NV61:
1734 info->fix.xpanstep = 2;
1737 /* Initialize variable screen information. */
1738 var = &info->var;
1739 memset(var, 0, sizeof(*var));
1740 var->xres = ovl->xres;
1741 var->yres = ovl->yres;
1742 var->xres_virtual = ovl->xres_virtual;
1743 var->yres_virtual = ovl->yres_virtual;
1744 var->activate = FB_ACTIVATE_NOW;
1746 /* Use the legacy API by default for RGB formats, and the FOURCC API
1747 * for YUV formats.
1749 if (!ovl->format->yuv)
1750 var->bits_per_pixel = ovl->format->bpp;
1751 else
1752 var->grayscale = ovl->format->fourcc;
1754 return sh_mobile_lcdc_overlay_check_var(var, info);
1757 /* -----------------------------------------------------------------------------
1758 * Frame buffer operations - main frame buffer
1761 static int sh_mobile_lcdc_setcolreg(u_int regno,
1762 u_int red, u_int green, u_int blue,
1763 u_int transp, struct fb_info *info)
1765 u32 *palette = info->pseudo_palette;
1767 if (regno >= PALETTE_NR)
1768 return -EINVAL;
1770 /* only FB_VISUAL_TRUECOLOR supported */
1772 red >>= 16 - info->var.red.length;
1773 green >>= 16 - info->var.green.length;
1774 blue >>= 16 - info->var.blue.length;
1775 transp >>= 16 - info->var.transp.length;
1777 palette[regno] = (red << info->var.red.offset) |
1778 (green << info->var.green.offset) |
1779 (blue << info->var.blue.offset) |
1780 (transp << info->var.transp.offset);
1782 return 0;
1785 static const struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
1786 .id = "SH Mobile LCDC",
1787 .type = FB_TYPE_PACKED_PIXELS,
1788 .visual = FB_VISUAL_TRUECOLOR,
1789 .accel = FB_ACCEL_NONE,
1790 .xpanstep = 1,
1791 .ypanstep = 1,
1792 .ywrapstep = 0,
1793 .capabilities = FB_CAP_FOURCC,
1796 static void sh_mobile_lcdc_fillrect(struct fb_info *info,
1797 const struct fb_fillrect *rect)
1799 sys_fillrect(info, rect);
1800 sh_mobile_lcdc_deferred_io_touch(info);
1803 static void sh_mobile_lcdc_copyarea(struct fb_info *info,
1804 const struct fb_copyarea *area)
1806 sys_copyarea(info, area);
1807 sh_mobile_lcdc_deferred_io_touch(info);
1810 static void sh_mobile_lcdc_imageblit(struct fb_info *info,
1811 const struct fb_image *image)
1813 sys_imageblit(info, image);
1814 sh_mobile_lcdc_deferred_io_touch(info);
1817 static int sh_mobile_lcdc_pan(struct fb_var_screeninfo *var,
1818 struct fb_info *info)
1820 struct sh_mobile_lcdc_chan *ch = info->par;
1821 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
1822 unsigned long ldrcntr;
1823 unsigned long base_addr_y, base_addr_c;
1824 unsigned long y_offset;
1825 unsigned long c_offset;
1827 if (!ch->format->yuv) {
1828 y_offset = (var->yoffset * ch->xres_virtual + var->xoffset)
1829 * ch->format->bpp / 8;
1830 c_offset = 0;
1831 } else {
1832 unsigned int xsub = ch->format->bpp < 24 ? 2 : 1;
1833 unsigned int ysub = ch->format->bpp < 16 ? 2 : 1;
1835 y_offset = var->yoffset * ch->xres_virtual + var->xoffset;
1836 c_offset = var->yoffset / ysub * ch->xres_virtual * 2 / xsub
1837 + var->xoffset * 2 / xsub;
1840 /* If the Y offset hasn't changed, the C offset hasn't either. There's
1841 * nothing to do in that case.
1843 if (y_offset == ch->pan_y_offset)
1844 return 0;
1846 /* Set the source address for the next refresh */
1847 base_addr_y = ch->dma_handle + y_offset;
1848 base_addr_c = ch->dma_handle + ch->xres_virtual * ch->yres_virtual
1849 + c_offset;
1851 if (ch->cache)
1852 sh_mobile_meram_cache_update(priv->meram_dev, ch->cache,
1853 base_addr_y, base_addr_c,
1854 &base_addr_y, &base_addr_c);
1856 ch->base_addr_y = base_addr_y;
1857 ch->base_addr_c = base_addr_c;
1858 ch->pan_y_offset = y_offset;
1860 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
1861 if (ch->format->yuv)
1862 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
1864 ldrcntr = lcdc_read(priv, _LDRCNTR);
1865 if (lcdc_chan_is_sublcd(ch))
1866 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
1867 else
1868 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
1871 sh_mobile_lcdc_deferred_io_touch(info);
1873 return 0;
1876 static int sh_mobile_lcdc_ioctl(struct fb_info *info, unsigned int cmd,
1877 unsigned long arg)
1879 struct sh_mobile_lcdc_chan *ch = info->par;
1880 int retval;
1882 switch (cmd) {
1883 case FBIO_WAITFORVSYNC:
1884 retval = sh_mobile_lcdc_wait_for_vsync(ch);
1885 break;
1887 default:
1888 retval = -ENOIOCTLCMD;
1889 break;
1891 return retval;
1894 static void sh_mobile_fb_reconfig(struct fb_info *info)
1896 struct sh_mobile_lcdc_chan *ch = info->par;
1897 struct fb_var_screeninfo var;
1898 struct fb_videomode mode;
1899 struct fb_event event;
1900 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1902 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1903 /* More framebuffer users are active */
1904 return;
1906 fb_var_to_videomode(&mode, &info->var);
1908 if (fb_mode_is_equal(&ch->display.mode, &mode))
1909 return;
1911 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1912 var = info->var;
1913 fb_videomode_to_var(&var, &ch->display.mode);
1914 var.width = ch->display.width;
1915 var.height = ch->display.height;
1916 var.activate = FB_ACTIVATE_NOW;
1918 if (fb_set_var(info, &var) < 0)
1919 /* Couldn't reconfigure, hopefully, can continue as before */
1920 return;
1923 * fb_set_var() calls the notifier change internally, only if
1924 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1925 * user event, we have to call the chain ourselves.
1927 event.info = info;
1928 event.data = &ch->display.mode;
1929 fb_notifier_call_chain(evnt, &event);
1933 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1934 * user == 1, or with console sem held, if user == 0.
1936 static int sh_mobile_lcdc_release(struct fb_info *info, int user)
1938 struct sh_mobile_lcdc_chan *ch = info->par;
1940 mutex_lock(&ch->open_lock);
1941 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1943 ch->use_count--;
1945 /* Nothing to reconfigure, when called from fbcon */
1946 if (user) {
1947 console_lock();
1948 sh_mobile_fb_reconfig(info);
1949 console_unlock();
1952 mutex_unlock(&ch->open_lock);
1954 return 0;
1957 static int sh_mobile_lcdc_open(struct fb_info *info, int user)
1959 struct sh_mobile_lcdc_chan *ch = info->par;
1961 mutex_lock(&ch->open_lock);
1962 ch->use_count++;
1964 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1965 mutex_unlock(&ch->open_lock);
1967 return 0;
1970 static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
1971 struct fb_info *info)
1973 struct sh_mobile_lcdc_chan *ch = info->par;
1974 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1975 unsigned int best_dist = (unsigned int)-1;
1976 unsigned int best_xres = 0;
1977 unsigned int best_yres = 0;
1978 unsigned int i;
1979 int ret;
1981 /* If board code provides us with a list of available modes, make sure
1982 * we use one of them. Find the mode closest to the requested one. The
1983 * distance between two modes is defined as the size of the
1984 * non-overlapping parts of the two rectangles.
1986 for (i = 0; i < ch->cfg->num_modes; ++i) {
1987 const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
1988 unsigned int dist;
1990 /* We can only round up. */
1991 if (var->xres > mode->xres || var->yres > mode->yres)
1992 continue;
1994 dist = var->xres * var->yres + mode->xres * mode->yres
1995 - 2 * min(var->xres, mode->xres)
1996 * min(var->yres, mode->yres);
1998 if (dist < best_dist) {
1999 best_xres = mode->xres;
2000 best_yres = mode->yres;
2001 best_dist = dist;
2005 /* If no available mode can be used, return an error. */
2006 if (ch->cfg->num_modes != 0) {
2007 if (best_dist == (unsigned int)-1)
2008 return -EINVAL;
2010 var->xres = best_xres;
2011 var->yres = best_yres;
2014 ret = __sh_mobile_lcdc_check_var(var, info);
2015 if (ret < 0)
2016 return ret;
2018 /* only accept the forced_fourcc for dual channel configurations */
2019 if (p->forced_fourcc &&
2020 p->forced_fourcc != sh_mobile_format_fourcc(var))
2021 return -EINVAL;
2023 return 0;
2026 static int sh_mobile_lcdc_set_par(struct fb_info *info)
2028 struct sh_mobile_lcdc_chan *ch = info->par;
2029 int ret;
2031 sh_mobile_lcdc_stop(ch->lcdc);
2033 ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
2034 ch->colorspace = info->var.colorspace;
2036 ch->xres = info->var.xres;
2037 ch->xres_virtual = info->var.xres_virtual;
2038 ch->yres = info->var.yres;
2039 ch->yres_virtual = info->var.yres_virtual;
2041 if (ch->format->yuv)
2042 ch->pitch = info->var.xres_virtual;
2043 else
2044 ch->pitch = info->var.xres_virtual * ch->format->bpp / 8;
2046 ret = sh_mobile_lcdc_start(ch->lcdc);
2047 if (ret < 0)
2048 dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
2050 info->fix.line_length = ch->pitch;
2052 if (sh_mobile_format_is_fourcc(&info->var)) {
2053 info->fix.type = FB_TYPE_FOURCC;
2054 info->fix.visual = FB_VISUAL_FOURCC;
2055 } else {
2056 info->fix.type = FB_TYPE_PACKED_PIXELS;
2057 info->fix.visual = FB_VISUAL_TRUECOLOR;
2060 return ret;
2064 * Screen blanking. Behavior is as follows:
2065 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
2066 * FB_BLANK_NORMAL: screen blanked, clocks enabled
2067 * FB_BLANK_VSYNC,
2068 * FB_BLANK_HSYNC,
2069 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
2071 static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
2073 struct sh_mobile_lcdc_chan *ch = info->par;
2074 struct sh_mobile_lcdc_priv *p = ch->lcdc;
2076 /* blank the screen? */
2077 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
2078 struct fb_fillrect rect = {
2079 .width = ch->xres,
2080 .height = ch->yres,
2082 sh_mobile_lcdc_fillrect(info, &rect);
2084 /* turn clocks on? */
2085 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
2086 sh_mobile_lcdc_clk_on(p);
2088 /* turn clocks off? */
2089 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
2090 /* make sure the screen is updated with the black fill before
2091 * switching the clocks off. one vsync is not enough since
2092 * blanking may occur in the middle of a refresh. deferred io
2093 * mode will reenable the clocks and update the screen in time,
2094 * so it does not need this. */
2095 if (!info->fbdefio) {
2096 sh_mobile_lcdc_wait_for_vsync(ch);
2097 sh_mobile_lcdc_wait_for_vsync(ch);
2099 sh_mobile_lcdc_clk_off(p);
2102 ch->blank_status = blank;
2103 return 0;
2106 static int
2107 sh_mobile_lcdc_mmap(struct fb_info *info, struct vm_area_struct *vma)
2109 struct sh_mobile_lcdc_chan *ch = info->par;
2111 return dma_mmap_coherent(ch->lcdc->dev, vma, ch->fb_mem,
2112 ch->dma_handle, ch->fb_size);
2115 static struct fb_ops sh_mobile_lcdc_ops = {
2116 .owner = THIS_MODULE,
2117 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
2118 .fb_read = fb_sys_read,
2119 .fb_write = fb_sys_write,
2120 .fb_fillrect = sh_mobile_lcdc_fillrect,
2121 .fb_copyarea = sh_mobile_lcdc_copyarea,
2122 .fb_imageblit = sh_mobile_lcdc_imageblit,
2123 .fb_blank = sh_mobile_lcdc_blank,
2124 .fb_pan_display = sh_mobile_lcdc_pan,
2125 .fb_ioctl = sh_mobile_lcdc_ioctl,
2126 .fb_open = sh_mobile_lcdc_open,
2127 .fb_release = sh_mobile_lcdc_release,
2128 .fb_check_var = sh_mobile_lcdc_check_var,
2129 .fb_set_par = sh_mobile_lcdc_set_par,
2130 .fb_mmap = sh_mobile_lcdc_mmap,
2133 static void
2134 sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
2136 if (ch->info && ch->info->dev)
2137 unregister_framebuffer(ch->info);
2140 static int
2141 sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
2143 struct fb_info *info = ch->info;
2144 int ret;
2146 if (info->fbdefio) {
2147 ch->sglist = vmalloc(sizeof(struct scatterlist) *
2148 ch->fb_size >> PAGE_SHIFT);
2149 if (!ch->sglist) {
2150 dev_err(ch->lcdc->dev, "cannot allocate sglist\n");
2151 return -ENOMEM;
2155 info->bl_dev = ch->bl;
2157 ret = register_framebuffer(info);
2158 if (ret < 0)
2159 return ret;
2161 dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
2162 dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
2163 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
2164 info->var.bits_per_pixel);
2166 /* deferred io mode: disable clock to save power */
2167 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
2168 sh_mobile_lcdc_clk_off(ch->lcdc);
2170 return ret;
2173 static void
2174 sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
2176 struct fb_info *info = ch->info;
2178 if (!info || !info->device)
2179 return;
2181 if (ch->sglist)
2182 vfree(ch->sglist);
2184 fb_dealloc_cmap(&info->cmap);
2185 framebuffer_release(info);
2188 static int
2189 sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
2190 const struct fb_videomode *modes,
2191 unsigned int num_modes)
2193 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
2194 struct fb_var_screeninfo *var;
2195 struct fb_info *info;
2196 int ret;
2198 /* Allocate and initialize the frame buffer device. Create the modes
2199 * list and allocate the color map.
2201 info = framebuffer_alloc(0, priv->dev);
2202 if (info == NULL) {
2203 dev_err(priv->dev, "unable to allocate fb_info\n");
2204 return -ENOMEM;
2207 ch->info = info;
2209 info->flags = FBINFO_FLAG_DEFAULT;
2210 info->fbops = &sh_mobile_lcdc_ops;
2211 info->device = priv->dev;
2212 info->screen_base = ch->fb_mem;
2213 info->pseudo_palette = &ch->pseudo_palette;
2214 info->par = ch;
2216 fb_videomode_to_modelist(modes, num_modes, &info->modelist);
2218 ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
2219 if (ret < 0) {
2220 dev_err(priv->dev, "unable to allocate cmap\n");
2221 return ret;
2224 /* Initialize fixed screen information. Restrict pan to 2 lines steps
2225 * for NV12 and NV21.
2227 info->fix = sh_mobile_lcdc_fix;
2228 info->fix.smem_start = ch->dma_handle;
2229 info->fix.smem_len = ch->fb_size;
2230 info->fix.line_length = ch->pitch;
2232 if (ch->format->yuv)
2233 info->fix.visual = FB_VISUAL_FOURCC;
2234 else
2235 info->fix.visual = FB_VISUAL_TRUECOLOR;
2237 switch (ch->format->fourcc) {
2238 case V4L2_PIX_FMT_NV12:
2239 case V4L2_PIX_FMT_NV21:
2240 info->fix.ypanstep = 2;
2241 case V4L2_PIX_FMT_NV16:
2242 case V4L2_PIX_FMT_NV61:
2243 info->fix.xpanstep = 2;
2246 /* Initialize variable screen information using the first mode as
2247 * default.
2249 var = &info->var;
2250 fb_videomode_to_var(var, modes);
2251 var->width = ch->display.width;
2252 var->height = ch->display.height;
2253 var->xres_virtual = ch->xres_virtual;
2254 var->yres_virtual = ch->yres_virtual;
2255 var->activate = FB_ACTIVATE_NOW;
2257 /* Use the legacy API by default for RGB formats, and the FOURCC API
2258 * for YUV formats.
2260 if (!ch->format->yuv)
2261 var->bits_per_pixel = ch->format->bpp;
2262 else
2263 var->grayscale = ch->format->fourcc;
2265 ret = sh_mobile_lcdc_check_var(var, info);
2266 if (ret)
2267 return ret;
2269 return 0;
2272 /* -----------------------------------------------------------------------------
2273 * Backlight
2276 static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
2278 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
2279 int brightness = bdev->props.brightness;
2281 if (bdev->props.power != FB_BLANK_UNBLANK ||
2282 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
2283 brightness = 0;
2285 ch->bl_brightness = brightness;
2286 return ch->cfg->bl_info.set_brightness(brightness);
2289 static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
2291 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
2293 return ch->bl_brightness;
2296 static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
2297 struct fb_info *info)
2299 return (info->bl_dev == bdev);
2302 static struct backlight_ops sh_mobile_lcdc_bl_ops = {
2303 .options = BL_CORE_SUSPENDRESUME,
2304 .update_status = sh_mobile_lcdc_update_bl,
2305 .get_brightness = sh_mobile_lcdc_get_brightness,
2306 .check_fb = sh_mobile_lcdc_check_fb,
2309 static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
2310 struct sh_mobile_lcdc_chan *ch)
2312 struct backlight_device *bl;
2314 bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
2315 &sh_mobile_lcdc_bl_ops, NULL);
2316 if (IS_ERR(bl)) {
2317 dev_err(parent, "unable to register backlight device: %ld\n",
2318 PTR_ERR(bl));
2319 return NULL;
2322 bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
2323 bl->props.brightness = bl->props.max_brightness;
2324 backlight_update_status(bl);
2326 return bl;
2329 static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
2331 backlight_device_unregister(bdev);
2334 /* -----------------------------------------------------------------------------
2335 * Power management
2338 static int sh_mobile_lcdc_suspend(struct device *dev)
2340 struct platform_device *pdev = to_platform_device(dev);
2342 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
2343 return 0;
2346 static int sh_mobile_lcdc_resume(struct device *dev)
2348 struct platform_device *pdev = to_platform_device(dev);
2350 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
2353 static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
2355 struct platform_device *pdev = to_platform_device(dev);
2356 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
2358 /* turn off LCDC hardware */
2359 lcdc_write(priv, _LDCNT1R, 0);
2361 return 0;
2364 static int sh_mobile_lcdc_runtime_resume(struct device *dev)
2366 struct platform_device *pdev = to_platform_device(dev);
2367 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
2369 __sh_mobile_lcdc_start(priv);
2371 return 0;
2374 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
2375 .suspend = sh_mobile_lcdc_suspend,
2376 .resume = sh_mobile_lcdc_resume,
2377 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
2378 .runtime_resume = sh_mobile_lcdc_runtime_resume,
2381 /* -----------------------------------------------------------------------------
2382 * Framebuffer notifier
2385 /* locking: called with info->lock held */
2386 static int sh_mobile_lcdc_notify(struct notifier_block *nb,
2387 unsigned long action, void *data)
2389 struct fb_event *event = data;
2390 struct fb_info *info = event->info;
2391 struct sh_mobile_lcdc_chan *ch = info->par;
2393 if (&ch->lcdc->notifier != nb)
2394 return NOTIFY_DONE;
2396 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
2397 __func__, action, event->data);
2399 switch(action) {
2400 case FB_EVENT_SUSPEND:
2401 sh_mobile_lcdc_display_off(ch);
2402 sh_mobile_lcdc_stop(ch->lcdc);
2403 break;
2404 case FB_EVENT_RESUME:
2405 mutex_lock(&ch->open_lock);
2406 sh_mobile_fb_reconfig(info);
2407 mutex_unlock(&ch->open_lock);
2409 sh_mobile_lcdc_display_on(ch);
2410 sh_mobile_lcdc_start(ch->lcdc);
2413 return NOTIFY_OK;
2416 /* -----------------------------------------------------------------------------
2417 * Probe/remove and driver init/exit
2420 static const struct fb_videomode default_720p = {
2421 .name = "HDMI 720p",
2422 .xres = 1280,
2423 .yres = 720,
2425 .left_margin = 220,
2426 .right_margin = 110,
2427 .hsync_len = 40,
2429 .upper_margin = 20,
2430 .lower_margin = 5,
2431 .vsync_len = 5,
2433 .pixclock = 13468,
2434 .refresh = 60,
2435 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
2438 static int sh_mobile_lcdc_remove(struct platform_device *pdev)
2440 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
2441 unsigned int i;
2443 fb_unregister_client(&priv->notifier);
2445 for (i = 0; i < ARRAY_SIZE(priv->overlays); i++)
2446 sh_mobile_lcdc_overlay_fb_unregister(&priv->overlays[i]);
2447 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
2448 sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
2450 sh_mobile_lcdc_stop(priv);
2452 for (i = 0; i < ARRAY_SIZE(priv->overlays); i++) {
2453 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2455 sh_mobile_lcdc_overlay_fb_cleanup(ovl);
2457 if (ovl->fb_mem)
2458 dma_free_coherent(&pdev->dev, ovl->fb_size,
2459 ovl->fb_mem, ovl->dma_handle);
2462 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
2463 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
2465 if (ch->tx_dev) {
2466 ch->tx_dev->lcdc = NULL;
2467 module_put(ch->cfg->tx_dev->dev.driver->owner);
2470 sh_mobile_lcdc_channel_fb_cleanup(ch);
2472 if (ch->fb_mem)
2473 dma_free_coherent(&pdev->dev, ch->fb_size,
2474 ch->fb_mem, ch->dma_handle);
2477 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
2478 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
2480 if (ch->bl)
2481 sh_mobile_lcdc_bl_remove(ch->bl);
2482 mutex_destroy(&ch->open_lock);
2485 if (priv->dot_clk) {
2486 pm_runtime_disable(&pdev->dev);
2487 clk_put(priv->dot_clk);
2490 if (priv->base)
2491 iounmap(priv->base);
2493 if (priv->irq)
2494 free_irq(priv->irq, priv);
2495 kfree(priv);
2496 return 0;
2499 static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
2501 int interface_type = ch->cfg->interface_type;
2503 switch (interface_type) {
2504 case RGB8:
2505 case RGB9:
2506 case RGB12A:
2507 case RGB12B:
2508 case RGB16:
2509 case RGB18:
2510 case RGB24:
2511 case SYS8A:
2512 case SYS8B:
2513 case SYS8C:
2514 case SYS8D:
2515 case SYS9:
2516 case SYS12:
2517 case SYS16A:
2518 case SYS16B:
2519 case SYS16C:
2520 case SYS18:
2521 case SYS24:
2522 break;
2523 default:
2524 return -EINVAL;
2527 /* SUBLCD only supports SYS interface */
2528 if (lcdc_chan_is_sublcd(ch)) {
2529 if (!(interface_type & LDMT1R_IFM))
2530 return -EINVAL;
2532 interface_type &= ~LDMT1R_IFM;
2535 ch->ldmt1r_value = interface_type;
2536 return 0;
2539 static int
2540 sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_overlay *ovl)
2542 const struct sh_mobile_lcdc_format_info *format;
2543 struct device *dev = ovl->channel->lcdc->dev;
2544 int ret;
2546 if (ovl->cfg->fourcc == 0)
2547 return 0;
2549 /* Validate the format. */
2550 format = sh_mobile_format_info(ovl->cfg->fourcc);
2551 if (format == NULL) {
2552 dev_err(dev, "Invalid FOURCC %08x\n", ovl->cfg->fourcc);
2553 return -EINVAL;
2556 ovl->enabled = false;
2557 ovl->mode = LCDC_OVERLAY_BLEND;
2558 ovl->alpha = 255;
2559 ovl->rop3 = 0;
2560 ovl->pos_x = 0;
2561 ovl->pos_y = 0;
2563 /* The default Y virtual resolution is twice the panel size to allow for
2564 * double-buffering.
2566 ovl->format = format;
2567 ovl->xres = ovl->cfg->max_xres;
2568 ovl->xres_virtual = ovl->xres;
2569 ovl->yres = ovl->cfg->max_yres;
2570 ovl->yres_virtual = ovl->yres * 2;
2572 if (!format->yuv)
2573 ovl->pitch = ovl->xres_virtual * format->bpp / 8;
2574 else
2575 ovl->pitch = ovl->xres_virtual;
2577 /* Allocate frame buffer memory. */
2578 ovl->fb_size = ovl->cfg->max_xres * ovl->cfg->max_yres
2579 * format->bpp / 8 * 2;
2580 ovl->fb_mem = dma_alloc_coherent(dev, ovl->fb_size, &ovl->dma_handle,
2581 GFP_KERNEL);
2582 if (!ovl->fb_mem) {
2583 dev_err(dev, "unable to allocate buffer\n");
2584 return -ENOMEM;
2587 ret = sh_mobile_lcdc_overlay_fb_init(ovl);
2588 if (ret < 0)
2589 return ret;
2591 return 0;
2594 static int
2595 sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch)
2597 const struct sh_mobile_lcdc_format_info *format;
2598 const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
2599 struct device *dev = ch->lcdc->dev;
2600 const struct fb_videomode *max_mode;
2601 const struct fb_videomode *mode;
2602 unsigned int num_modes;
2603 unsigned int max_size;
2604 unsigned int i;
2606 mutex_init(&ch->open_lock);
2607 ch->notify = sh_mobile_lcdc_display_notify;
2609 /* Validate the format. */
2610 format = sh_mobile_format_info(cfg->fourcc);
2611 if (format == NULL) {
2612 dev_err(dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
2613 return -EINVAL;
2616 /* Iterate through the modes to validate them and find the highest
2617 * resolution.
2619 max_mode = NULL;
2620 max_size = 0;
2622 for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
2623 unsigned int size = mode->yres * mode->xres;
2625 /* NV12/NV21 buffers must have even number of lines */
2626 if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
2627 cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
2628 dev_err(dev, "yres must be multiple of 2 for "
2629 "YCbCr420 mode.\n");
2630 return -EINVAL;
2633 if (size > max_size) {
2634 max_mode = mode;
2635 max_size = size;
2639 if (!max_size)
2640 max_size = MAX_XRES * MAX_YRES;
2641 else
2642 dev_dbg(dev, "Found largest videomode %ux%u\n",
2643 max_mode->xres, max_mode->yres);
2645 if (cfg->lcd_modes == NULL) {
2646 mode = &default_720p;
2647 num_modes = 1;
2648 } else {
2649 mode = cfg->lcd_modes;
2650 num_modes = cfg->num_modes;
2653 /* Use the first mode as default. The default Y virtual resolution is
2654 * twice the panel size to allow for double-buffering.
2656 ch->format = format;
2657 ch->xres = mode->xres;
2658 ch->xres_virtual = mode->xres;
2659 ch->yres = mode->yres;
2660 ch->yres_virtual = mode->yres * 2;
2662 if (!format->yuv) {
2663 ch->colorspace = V4L2_COLORSPACE_SRGB;
2664 ch->pitch = ch->xres_virtual * format->bpp / 8;
2665 } else {
2666 ch->colorspace = V4L2_COLORSPACE_REC709;
2667 ch->pitch = ch->xres_virtual;
2670 ch->display.width = cfg->panel_cfg.width;
2671 ch->display.height = cfg->panel_cfg.height;
2672 ch->display.mode = *mode;
2674 /* Allocate frame buffer memory. */
2675 ch->fb_size = max_size * format->bpp / 8 * 2;
2676 ch->fb_mem = dma_alloc_coherent(dev, ch->fb_size, &ch->dma_handle,
2677 GFP_KERNEL);
2678 if (ch->fb_mem == NULL) {
2679 dev_err(dev, "unable to allocate buffer\n");
2680 return -ENOMEM;
2683 /* Initialize the transmitter device if present. */
2684 if (cfg->tx_dev) {
2685 if (!cfg->tx_dev->dev.driver ||
2686 !try_module_get(cfg->tx_dev->dev.driver->owner)) {
2687 dev_warn(dev, "unable to get transmitter device\n");
2688 return -EINVAL;
2690 ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
2691 ch->tx_dev->lcdc = ch;
2692 ch->tx_dev->def_mode = *mode;
2695 return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
2698 static int sh_mobile_lcdc_probe(struct platform_device *pdev)
2700 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
2701 struct sh_mobile_lcdc_priv *priv;
2702 struct resource *res;
2703 int num_channels;
2704 int error;
2705 int i;
2707 if (!pdata) {
2708 dev_err(&pdev->dev, "no platform data defined\n");
2709 return -EINVAL;
2712 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2713 i = platform_get_irq(pdev, 0);
2714 if (!res || i < 0) {
2715 dev_err(&pdev->dev, "cannot get platform resources\n");
2716 return -ENOENT;
2719 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2720 if (!priv) {
2721 dev_err(&pdev->dev, "cannot allocate device data\n");
2722 return -ENOMEM;
2725 priv->dev = &pdev->dev;
2726 priv->meram_dev = pdata->meram_dev;
2727 platform_set_drvdata(pdev, priv);
2729 error = request_irq(i, sh_mobile_lcdc_irq, 0,
2730 dev_name(&pdev->dev), priv);
2731 if (error) {
2732 dev_err(&pdev->dev, "unable to request irq\n");
2733 goto err1;
2736 priv->irq = i;
2737 atomic_set(&priv->hw_usecnt, -1);
2739 for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
2740 struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
2742 ch->lcdc = priv;
2743 ch->cfg = &pdata->ch[i];
2745 error = sh_mobile_lcdc_check_interface(ch);
2746 if (error) {
2747 dev_err(&pdev->dev, "unsupported interface type\n");
2748 goto err1;
2750 init_waitqueue_head(&ch->frame_end_wait);
2751 init_completion(&ch->vsync_completion);
2753 /* probe the backlight is there is one defined */
2754 if (ch->cfg->bl_info.max_brightness)
2755 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
2757 switch (pdata->ch[i].chan) {
2758 case LCDC_CHAN_MAINLCD:
2759 ch->enabled = LDCNT2R_ME;
2760 ch->reg_offs = lcdc_offs_mainlcd;
2761 num_channels++;
2762 break;
2763 case LCDC_CHAN_SUBLCD:
2764 ch->enabled = LDCNT2R_SE;
2765 ch->reg_offs = lcdc_offs_sublcd;
2766 num_channels++;
2767 break;
2771 if (!num_channels) {
2772 dev_err(&pdev->dev, "no channels defined\n");
2773 error = -EINVAL;
2774 goto err1;
2777 /* for dual channel LCDC (MAIN + SUB) force shared format setting */
2778 if (num_channels == 2)
2779 priv->forced_fourcc = pdata->ch[0].fourcc;
2781 priv->base = ioremap_nocache(res->start, resource_size(res));
2782 if (!priv->base)
2783 goto err1;
2785 error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
2786 if (error) {
2787 dev_err(&pdev->dev, "unable to setup clocks\n");
2788 goto err1;
2791 /* Enable runtime PM. */
2792 pm_runtime_enable(&pdev->dev);
2794 for (i = 0; i < num_channels; i++) {
2795 struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
2797 error = sh_mobile_lcdc_channel_init(ch);
2798 if (error)
2799 goto err1;
2802 for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
2803 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2805 ovl->cfg = &pdata->overlays[i];
2806 ovl->channel = &priv->ch[0];
2808 error = sh_mobile_lcdc_overlay_init(ovl);
2809 if (error)
2810 goto err1;
2813 error = sh_mobile_lcdc_start(priv);
2814 if (error) {
2815 dev_err(&pdev->dev, "unable to start hardware\n");
2816 goto err1;
2819 for (i = 0; i < num_channels; i++) {
2820 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
2822 error = sh_mobile_lcdc_channel_fb_register(ch);
2823 if (error)
2824 goto err1;
2827 for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
2828 struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2830 error = sh_mobile_lcdc_overlay_fb_register(ovl);
2831 if (error)
2832 goto err1;
2835 /* Failure ignored */
2836 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
2837 fb_register_client(&priv->notifier);
2839 return 0;
2840 err1:
2841 sh_mobile_lcdc_remove(pdev);
2843 return error;
2846 static struct platform_driver sh_mobile_lcdc_driver = {
2847 .driver = {
2848 .name = "sh_mobile_lcdc_fb",
2849 .owner = THIS_MODULE,
2850 .pm = &sh_mobile_lcdc_dev_pm_ops,
2852 .probe = sh_mobile_lcdc_probe,
2853 .remove = sh_mobile_lcdc_remove,
2856 module_platform_driver(sh_mobile_lcdc_driver);
2858 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
2859 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
2860 MODULE_LICENSE("GPL v2");