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/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
15 #include <linux/clk.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/platform_device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/interrupt.h>
20 #include <linux/vmalloc.h>
21 #include <linux/ioctl.h>
22 #include <linux/slab.h>
23 #include <linux/console.h>
24 #include <linux/backlight.h>
25 #include <linux/gpio.h>
26 #include <video/sh_mobile_lcdc.h>
27 #include <asm/atomic.h>
29 #include "sh_mobile_lcdcfb.h"
31 #define SIDE_B_OFFSET 0x1000
32 #define MIRROR_OFFSET 0x2000
34 /* shared registers */
36 #define _LDDCKSTPR 0x414
39 #define _LDCNT1R 0x470
40 #define _LDCNT2R 0x474
41 #define _LDRCNTR 0x478
43 #define _LDDWD0R 0x800
48 /* shared registers and their order for context save/restore */
49 static int lcdc_shared_regs[] = {
57 #define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
62 static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
82 static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
100 #define START_LCDC 0x00000001
101 #define LCDC_RESET 0x00000100
102 #define DISPLAY_BEU 0x00000008
103 #define LCDC_ENABLE 0x00000001
104 #define LDINTR_FE 0x00000400
105 #define LDINTR_VSE 0x00000200
106 #define LDINTR_VEE 0x00000100
107 #define LDINTR_FS 0x00000004
108 #define LDINTR_VSS 0x00000002
109 #define LDINTR_VES 0x00000001
110 #define LDRCNTR_SRS 0x00020000
111 #define LDRCNTR_SRC 0x00010000
112 #define LDRCNTR_MRS 0x00000002
113 #define LDRCNTR_MRC 0x00000001
114 #define LDSR_MRS 0x00000100
116 static const struct fb_videomode default_720p = {
131 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
134 struct sh_mobile_lcdc_priv {
140 unsigned long lddckr;
141 struct sh_mobile_lcdc_chan ch[2];
142 struct notifier_block notifier;
143 unsigned long saved_shared_regs[NR_SHARED_REGS];
145 int forced_bpp; /* 2 channel LCDC must share bpp setting */
148 static bool banked(int reg_nr)
168 static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
169 int reg_nr, unsigned long data)
171 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
173 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
177 static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
178 int reg_nr, unsigned long data)
180 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
184 static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
187 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
190 static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
191 unsigned long reg_offs, unsigned long data)
193 iowrite32(data, priv->base + reg_offs);
196 static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
197 unsigned long reg_offs)
199 return ioread32(priv->base + reg_offs);
202 static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
203 unsigned long reg_offs,
204 unsigned long mask, unsigned long until)
206 while ((lcdc_read(priv, reg_offs) & mask) != until)
210 static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
212 return chan->cfg.chan == LCDC_CHAN_SUBLCD;
215 static void lcdc_sys_write_index(void *handle, unsigned long data)
217 struct sh_mobile_lcdc_chan *ch = handle;
219 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
220 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
221 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
222 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
225 static void lcdc_sys_write_data(void *handle, unsigned long data)
227 struct sh_mobile_lcdc_chan *ch = handle;
229 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
230 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
231 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
232 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
235 static unsigned long lcdc_sys_read_data(void *handle)
237 struct sh_mobile_lcdc_chan *ch = handle;
239 lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
240 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
241 lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
243 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
245 return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
248 struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
249 lcdc_sys_write_index,
254 static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
256 if (atomic_inc_and_test(&priv->hw_usecnt)) {
257 pm_runtime_get_sync(priv->dev);
259 clk_enable(priv->dot_clk);
263 static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
265 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
267 clk_disable(priv->dot_clk);
268 pm_runtime_put(priv->dev);
272 static int sh_mobile_lcdc_sginit(struct fb_info *info,
273 struct list_head *pagelist)
275 struct sh_mobile_lcdc_chan *ch = info->par;
276 unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
280 sg_init_table(ch->sglist, nr_pages_max);
282 list_for_each_entry(page, pagelist, lru)
283 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
288 static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
289 struct list_head *pagelist)
291 struct sh_mobile_lcdc_chan *ch = info->par;
292 struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_cfg;
294 /* enable clocks before accessing hardware */
295 sh_mobile_lcdc_clk_on(ch->lcdc);
298 * It's possible to get here without anything on the pagelist via
299 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
300 * invocation. In the former case, the acceleration routines are
301 * stepped in to when using the framebuffer console causing the
302 * workqueue to be scheduled without any dirty pages on the list.
304 * Despite this, a panel update is still needed given that the
305 * acceleration routines have their own methods for writing in
306 * that still need to be updated.
308 * The fsync() and empty pagelist case could be optimized for,
309 * but we don't bother, as any application exhibiting such
310 * behaviour is fundamentally broken anyways.
312 if (!list_empty(pagelist)) {
313 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
315 /* trigger panel update */
316 dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
317 if (bcfg->start_transfer)
318 bcfg->start_transfer(bcfg->board_data, ch,
319 &sh_mobile_lcdc_sys_bus_ops);
320 lcdc_write_chan(ch, LDSM2R, 1);
321 dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
323 if (bcfg->start_transfer)
324 bcfg->start_transfer(bcfg->board_data, ch,
325 &sh_mobile_lcdc_sys_bus_ops);
326 lcdc_write_chan(ch, LDSM2R, 1);
330 static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
332 struct fb_deferred_io *fbdefio = info->fbdefio;
335 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
338 static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
340 struct sh_mobile_lcdc_priv *priv = data;
341 struct sh_mobile_lcdc_chan *ch;
343 unsigned long ldintr;
347 /* acknowledge interrupt */
348 ldintr = tmp = lcdc_read(priv, _LDINTR);
350 * disable further VSYNC End IRQs, preserve all other enabled IRQs,
351 * write 0 to bits 0-6 to ack all triggered IRQs.
353 tmp &= 0xffffff00 & ~LDINTR_VEE;
354 lcdc_write(priv, _LDINTR, tmp);
356 /* figure out if this interrupt is for main or sub lcd */
357 is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
359 /* wake up channel and disable clocks */
360 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
367 if (ldintr & LDINTR_FS) {
368 if (is_sub == lcdc_chan_is_sublcd(ch)) {
370 wake_up(&ch->frame_end_wait);
372 sh_mobile_lcdc_clk_off(priv);
377 if (ldintr & LDINTR_VES)
378 complete(&ch->vsync_completion);
384 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
387 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
390 /* start or stop the lcdc */
392 lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
394 lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
396 /* wait until power is applied/stopped on all channels */
397 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
398 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
400 tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
401 if (start && tmp == 3)
403 if (!start && tmp == 0)
409 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
412 static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
414 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
415 unsigned long h_total, hsync_pos, display_h_total;
418 tmp = ch->ldmt1r_value;
419 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
420 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
421 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
422 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
423 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
424 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
425 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
426 lcdc_write_chan(ch, LDMT1R, tmp);
429 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
430 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
432 /* horizontal configuration */
433 h_total = display_var->xres + display_var->hsync_len +
434 display_var->left_margin + display_var->right_margin;
435 tmp = h_total / 8; /* HTCN */
436 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
437 lcdc_write_chan(ch, LDHCNR, tmp);
439 hsync_pos = display_var->xres + display_var->right_margin;
440 tmp = hsync_pos / 8; /* HSYNP */
441 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
442 lcdc_write_chan(ch, LDHSYNR, tmp);
444 /* vertical configuration */
445 tmp = display_var->yres + display_var->vsync_len +
446 display_var->upper_margin + display_var->lower_margin; /* VTLN */
447 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
448 lcdc_write_chan(ch, LDVLNR, tmp);
450 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
451 tmp |= display_var->vsync_len << 16; /* VSYNW */
452 lcdc_write_chan(ch, LDVSYNR, tmp);
454 /* Adjust horizontal synchronisation for HDMI */
455 display_h_total = display_var->xres + display_var->hsync_len +
456 display_var->left_margin + display_var->right_margin;
457 tmp = ((display_var->xres & 7) << 24) |
458 ((display_h_total & 7) << 16) |
459 ((display_var->hsync_len & 7) << 8) |
461 lcdc_write_chan(ch, LDHAJR, tmp);
464 static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
466 struct sh_mobile_lcdc_chan *ch;
467 struct sh_mobile_lcdc_board_cfg *board_cfg;
470 unsigned long ldddsr;
474 /* enable clocks before accessing the hardware */
475 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
476 if (priv->ch[k].enabled) {
477 sh_mobile_lcdc_clk_on(priv);
479 bpp = priv->ch[k].info->var.bits_per_pixel;
484 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
485 lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
487 /* enable LCDC channels */
488 tmp = lcdc_read(priv, _LDCNT2R);
489 tmp |= priv->ch[0].enabled;
490 tmp |= priv->ch[1].enabled;
491 lcdc_write(priv, _LDCNT2R, tmp);
493 /* read data from external memory, avoid using the BEU for now */
494 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
496 /* stop the lcdc first */
497 sh_mobile_lcdc_start_stop(priv, 0);
499 /* configure clocks */
501 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
504 if (!priv->ch[k].enabled)
507 m = ch->cfg.clock_divider;
513 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
515 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider denominator */
516 lcdc_write_chan(ch, LDDCKPAT1R, 0);
517 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
520 lcdc_write(priv, _LDDCKR, tmp);
522 /* start dotclock again */
523 lcdc_write(priv, _LDDCKSTPR, 0);
524 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
526 /* interrupts are disabled to begin with */
527 lcdc_write(priv, _LDINTR, 0);
529 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
535 sh_mobile_lcdc_geometry(ch);
538 lcdc_write_chan(ch, LDPMR, 0);
540 board_cfg = &ch->cfg.board_cfg;
541 if (board_cfg->setup_sys)
542 ret = board_cfg->setup_sys(board_cfg->board_data, ch,
543 &sh_mobile_lcdc_sys_bus_ops);
548 /* word and long word swap */
549 ldddsr = lcdc_read(priv, _LDDDSR);
550 if (priv->ch[0].info->var.nonstd)
551 lcdc_write(priv, _LDDDSR, ldddsr | 7);
555 lcdc_write(priv, _LDDDSR, ldddsr | 6);
558 lcdc_write(priv, _LDDDSR, ldddsr | 7);
561 lcdc_write(priv, _LDDDSR, ldddsr | 4);
566 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
569 if (!priv->ch[k].enabled)
572 /* set bpp format in PKF[4:0] */
573 tmp = lcdc_read_chan(ch, LDDFR);
575 if (ch->info->var.nonstd) {
576 tmp |= (ch->info->var.nonstd << 16);
577 switch (ch->info->var.bits_per_pixel) {
588 switch (ch->info->var.bits_per_pixel) {
599 lcdc_write_chan(ch, LDDFR, tmp);
601 /* point out our frame buffer */
602 lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
603 if (ch->info->var.nonstd)
604 lcdc_write_chan(ch, LDSA2R,
605 ch->info->fix.smem_start +
607 ch->info->var.yres_virtual);
610 lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
612 /* setup deferred io if SYS bus */
613 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
614 if (ch->ldmt1r_value & (1 << 12) && tmp) {
615 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
616 ch->defio.delay = msecs_to_jiffies(tmp);
617 ch->info->fbdefio = &ch->defio;
618 fb_deferred_io_init(ch->info);
621 lcdc_write_chan(ch, LDSM1R, 1);
623 /* enable "Frame End Interrupt Enable" bit */
624 lcdc_write(priv, _LDINTR, LDINTR_FE);
627 /* continuous read mode */
628 lcdc_write_chan(ch, LDSM1R, 0);
633 lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
636 sh_mobile_lcdc_start_stop(priv, 1);
639 /* tell the board code to enable the panel */
640 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
645 board_cfg = &ch->cfg.board_cfg;
646 if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
647 board_cfg->display_on(board_cfg->board_data, ch->info);
648 module_put(board_cfg->owner);
652 ch->bl->props.power = FB_BLANK_UNBLANK;
653 backlight_update_status(ch->bl);
660 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
662 struct sh_mobile_lcdc_chan *ch;
663 struct sh_mobile_lcdc_board_cfg *board_cfg;
666 /* clean up deferred io and ask board code to disable panel */
667 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
673 * flush frame, and wait for frame end interrupt
674 * clean up deferred io and enable clock
676 if (ch->info && ch->info->fbdefio) {
678 schedule_delayed_work(&ch->info->deferred_work, 0);
679 wait_event(ch->frame_end_wait, ch->frame_end);
680 fb_deferred_io_cleanup(ch->info);
681 ch->info->fbdefio = NULL;
682 sh_mobile_lcdc_clk_on(priv);
686 ch->bl->props.power = FB_BLANK_POWERDOWN;
687 backlight_update_status(ch->bl);
690 board_cfg = &ch->cfg.board_cfg;
691 if (try_module_get(board_cfg->owner) && board_cfg->display_off) {
692 board_cfg->display_off(board_cfg->board_data);
693 module_put(board_cfg->owner);
699 sh_mobile_lcdc_start_stop(priv, 0);
704 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
705 if (priv->ch[k].enabled)
706 sh_mobile_lcdc_clk_off(priv);
709 static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
713 switch (ch->cfg.interface_type) {
714 case RGB8: ifm = 0; miftyp = 0; break;
715 case RGB9: ifm = 0; miftyp = 4; break;
716 case RGB12A: ifm = 0; miftyp = 5; break;
717 case RGB12B: ifm = 0; miftyp = 6; break;
718 case RGB16: ifm = 0; miftyp = 7; break;
719 case RGB18: ifm = 0; miftyp = 10; break;
720 case RGB24: ifm = 0; miftyp = 11; break;
721 case SYS8A: ifm = 1; miftyp = 0; break;
722 case SYS8B: ifm = 1; miftyp = 1; break;
723 case SYS8C: ifm = 1; miftyp = 2; break;
724 case SYS8D: ifm = 1; miftyp = 3; break;
725 case SYS9: ifm = 1; miftyp = 4; break;
726 case SYS12: ifm = 1; miftyp = 5; break;
727 case SYS16A: ifm = 1; miftyp = 7; break;
728 case SYS16B: ifm = 1; miftyp = 8; break;
729 case SYS16C: ifm = 1; miftyp = 9; break;
730 case SYS18: ifm = 1; miftyp = 10; break;
731 case SYS24: ifm = 1; miftyp = 11; break;
735 /* SUBLCD only supports SYS interface */
736 if (lcdc_chan_is_sublcd(ch)) {
743 ch->ldmt1r_value = (ifm << 12) | miftyp;
749 static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
751 struct sh_mobile_lcdc_priv *priv)
756 switch (clock_source) {
757 case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
758 case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
759 case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
764 priv->lddckr = icksel << 16;
767 priv->dot_clk = clk_get(&pdev->dev, str);
768 if (IS_ERR(priv->dot_clk)) {
769 dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
770 return PTR_ERR(priv->dot_clk);
774 /* Runtime PM support involves two step for this driver:
775 * 1) Enable Runtime PM
776 * 2) Force Runtime PM Resume since hardware is accessed from probe()
778 priv->dev = &pdev->dev;
779 pm_runtime_enable(priv->dev);
780 pm_runtime_resume(priv->dev);
784 static int sh_mobile_lcdc_setcolreg(u_int regno,
785 u_int red, u_int green, u_int blue,
786 u_int transp, struct fb_info *info)
788 u32 *palette = info->pseudo_palette;
790 if (regno >= PALETTE_NR)
793 /* only FB_VISUAL_TRUECOLOR supported */
795 red >>= 16 - info->var.red.length;
796 green >>= 16 - info->var.green.length;
797 blue >>= 16 - info->var.blue.length;
798 transp >>= 16 - info->var.transp.length;
800 palette[regno] = (red << info->var.red.offset) |
801 (green << info->var.green.offset) |
802 (blue << info->var.blue.offset) |
803 (transp << info->var.transp.offset);
808 static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
809 .id = "SH Mobile LCDC",
810 .type = FB_TYPE_PACKED_PIXELS,
811 .visual = FB_VISUAL_TRUECOLOR,
812 .accel = FB_ACCEL_NONE,
818 static void sh_mobile_lcdc_fillrect(struct fb_info *info,
819 const struct fb_fillrect *rect)
821 sys_fillrect(info, rect);
822 sh_mobile_lcdc_deferred_io_touch(info);
825 static void sh_mobile_lcdc_copyarea(struct fb_info *info,
826 const struct fb_copyarea *area)
828 sys_copyarea(info, area);
829 sh_mobile_lcdc_deferred_io_touch(info);
832 static void sh_mobile_lcdc_imageblit(struct fb_info *info,
833 const struct fb_image *image)
835 sys_imageblit(info, image);
836 sh_mobile_lcdc_deferred_io_touch(info);
839 static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
840 struct fb_info *info)
842 struct sh_mobile_lcdc_chan *ch = info->par;
843 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
844 unsigned long ldrcntr;
845 unsigned long new_pan_offset;
846 unsigned long base_addr_y, base_addr_c;
847 unsigned long c_offset;
850 new_pan_offset = (var->yoffset * info->fix.line_length) +
851 (var->xoffset * (info->var.bits_per_pixel / 8));
853 new_pan_offset = (var->yoffset * info->fix.line_length) +
856 if (new_pan_offset == ch->pan_offset)
857 return 0; /* No change, do nothing */
859 ldrcntr = lcdc_read(priv, _LDRCNTR);
861 /* Set the source address for the next refresh */
862 base_addr_y = ch->dma_handle + new_pan_offset;
865 c_offset = (var->yoffset *
866 info->fix.line_length *
867 (info->var.bits_per_pixel - 8)) / 8;
868 base_addr_c = ch->dma_handle + var->xres * var->yres_virtual +
871 if (info->var.bits_per_pixel == 24)
872 base_addr_c += 2 * var->xoffset;
874 base_addr_c += var->xoffset;
878 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
880 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
882 if (lcdc_chan_is_sublcd(ch))
883 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
885 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
887 ch->pan_offset = new_pan_offset;
889 sh_mobile_lcdc_deferred_io_touch(info);
894 static int sh_mobile_wait_for_vsync(struct fb_info *info)
896 struct sh_mobile_lcdc_chan *ch = info->par;
897 unsigned long ldintr;
900 /* Enable VSync End interrupt */
901 ldintr = lcdc_read(ch->lcdc, _LDINTR);
902 ldintr |= LDINTR_VEE;
903 lcdc_write(ch->lcdc, _LDINTR, ldintr);
905 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
906 msecs_to_jiffies(100));
913 static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
919 case FBIO_WAITFORVSYNC:
920 retval = sh_mobile_wait_for_vsync(info);
924 retval = -ENOIOCTLCMD;
930 static void sh_mobile_fb_reconfig(struct fb_info *info)
932 struct sh_mobile_lcdc_chan *ch = info->par;
933 struct fb_videomode mode1, mode2;
934 struct fb_event event;
935 int evnt = FB_EVENT_MODE_CHANGE_ALL;
937 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
938 /* More framebuffer users are active */
941 fb_var_to_videomode(&mode1, &ch->display_var);
942 fb_var_to_videomode(&mode2, &info->var);
944 if (fb_mode_is_equal(&mode1, &mode2))
947 /* Display has been re-plugged, framebuffer is free now, reconfigure */
948 if (fb_set_var(info, &ch->display_var) < 0)
949 /* Couldn't reconfigure, hopefully, can continue as before */
952 if (info->var.nonstd)
953 info->fix.line_length = mode1.xres;
955 info->fix.line_length = mode1.xres * (ch->cfg.bpp / 8);
958 * fb_set_var() calls the notifier change internally, only if
959 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
960 * user event, we have to call the chain ourselves.
964 fb_notifier_call_chain(evnt, &event);
968 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
969 * user == 1, or with console sem held, if user == 0.
971 static int sh_mobile_release(struct fb_info *info, int user)
973 struct sh_mobile_lcdc_chan *ch = info->par;
975 mutex_lock(&ch->open_lock);
976 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
980 /* Nothing to reconfigure, when called from fbcon */
983 sh_mobile_fb_reconfig(info);
987 mutex_unlock(&ch->open_lock);
992 static int sh_mobile_open(struct fb_info *info, int user)
994 struct sh_mobile_lcdc_chan *ch = info->par;
996 mutex_lock(&ch->open_lock);
999 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1000 mutex_unlock(&ch->open_lock);
1005 static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1007 struct sh_mobile_lcdc_chan *ch = info->par;
1008 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1010 if (var->xres > MAX_XRES || var->yres > MAX_YRES ||
1011 var->xres * var->yres * (ch->cfg.bpp / 8) * 2 > info->fix.smem_len) {
1012 dev_warn(info->dev, "Invalid info: %u-%u-%u-%u x %u-%u-%u-%u @ %lukHz!\n",
1013 var->left_margin, var->xres, var->right_margin, var->hsync_len,
1014 var->upper_margin, var->yres, var->lower_margin, var->vsync_len,
1015 PICOS2KHZ(var->pixclock));
1019 /* only accept the forced_bpp for dual channel configurations */
1020 if (p->forced_bpp && p->forced_bpp != var->bits_per_pixel)
1023 switch (var->bits_per_pixel) {
1024 case 16: /* PKF[4:0] = 00011 - RGB 565 */
1025 case 24: /* PKF[4:0] = 01011 - RGB 888 */
1026 case 32: /* PKF[4:0] = 00000 - RGBA 888 */
1035 static struct fb_ops sh_mobile_lcdc_ops = {
1036 .owner = THIS_MODULE,
1037 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
1038 .fb_read = fb_sys_read,
1039 .fb_write = fb_sys_write,
1040 .fb_fillrect = sh_mobile_lcdc_fillrect,
1041 .fb_copyarea = sh_mobile_lcdc_copyarea,
1042 .fb_imageblit = sh_mobile_lcdc_imageblit,
1043 .fb_pan_display = sh_mobile_fb_pan_display,
1044 .fb_ioctl = sh_mobile_ioctl,
1045 .fb_open = sh_mobile_open,
1046 .fb_release = sh_mobile_release,
1047 .fb_check_var = sh_mobile_check_var,
1050 static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
1052 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1053 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1054 int brightness = bdev->props.brightness;
1056 if (bdev->props.power != FB_BLANK_UNBLANK ||
1057 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
1060 return cfg->set_brightness(cfg->board_data, brightness);
1063 static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
1065 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1066 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1068 return cfg->get_brightness(cfg->board_data);
1071 static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
1072 struct fb_info *info)
1074 return (info->bl_dev == bdev);
1077 static struct backlight_ops sh_mobile_lcdc_bl_ops = {
1078 .options = BL_CORE_SUSPENDRESUME,
1079 .update_status = sh_mobile_lcdc_update_bl,
1080 .get_brightness = sh_mobile_lcdc_get_brightness,
1081 .check_fb = sh_mobile_lcdc_check_fb,
1084 static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
1085 struct sh_mobile_lcdc_chan *ch)
1087 struct backlight_device *bl;
1089 bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
1090 &sh_mobile_lcdc_bl_ops, NULL);
1092 dev_err(parent, "unable to register backlight device: %ld\n",
1097 bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
1098 bl->props.brightness = bl->props.max_brightness;
1099 backlight_update_status(bl);
1104 static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
1106 backlight_device_unregister(bdev);
1109 static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp,
1117 var->bits_per_pixel = bpp;
1118 var->nonstd = nonstd;
1126 case 16: /* PKF[4:0] = 00011 - RGB 565 */
1127 var->red.offset = 11;
1128 var->red.length = 5;
1129 var->green.offset = 5;
1130 var->green.length = 6;
1131 var->blue.offset = 0;
1132 var->blue.length = 5;
1133 var->transp.offset = 0;
1134 var->transp.length = 0;
1137 case 24: /* PKF[4:0] = 01011 - RGB 888 */
1138 var->red.offset = 16;
1139 var->red.length = 8;
1140 var->green.offset = 8;
1141 var->green.length = 8;
1142 var->blue.offset = 0;
1143 var->blue.length = 8;
1144 var->transp.offset = 0;
1145 var->transp.length = 0;
1148 case 32: /* PKF[4:0] = 00000 - RGBA 888 */
1149 var->red.offset = 16;
1150 var->red.length = 8;
1151 var->green.offset = 8;
1152 var->green.length = 8;
1153 var->blue.offset = 0;
1154 var->blue.length = 8;
1155 var->transp.offset = 24;
1156 var->transp.length = 8;
1161 var->bits_per_pixel = bpp;
1162 var->red.msb_right = 0;
1163 var->green.msb_right = 0;
1164 var->blue.msb_right = 0;
1165 var->transp.msb_right = 0;
1169 static int sh_mobile_lcdc_suspend(struct device *dev)
1171 struct platform_device *pdev = to_platform_device(dev);
1173 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
1177 static int sh_mobile_lcdc_resume(struct device *dev)
1179 struct platform_device *pdev = to_platform_device(dev);
1181 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
1184 static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
1186 struct platform_device *pdev = to_platform_device(dev);
1187 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
1188 struct sh_mobile_lcdc_chan *ch;
1191 /* save per-channel registers */
1192 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
1196 for (n = 0; n < NR_CH_REGS; n++)
1197 ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
1200 /* save shared registers */
1201 for (n = 0; n < NR_SHARED_REGS; n++)
1202 p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
1204 /* turn off LCDC hardware */
1205 lcdc_write(p, _LDCNT1R, 0);
1209 static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1211 struct platform_device *pdev = to_platform_device(dev);
1212 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
1213 struct sh_mobile_lcdc_chan *ch;
1216 /* restore per-channel registers */
1217 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
1221 for (n = 0; n < NR_CH_REGS; n++)
1222 lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
1225 /* restore shared registers */
1226 for (n = 0; n < NR_SHARED_REGS; n++)
1227 lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
1232 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
1233 .suspend = sh_mobile_lcdc_suspend,
1234 .resume = sh_mobile_lcdc_resume,
1235 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1236 .runtime_resume = sh_mobile_lcdc_runtime_resume,
1239 /* locking: called with info->lock held */
1240 static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1241 unsigned long action, void *data)
1243 struct fb_event *event = data;
1244 struct fb_info *info = event->info;
1245 struct sh_mobile_lcdc_chan *ch = info->par;
1246 struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
1249 if (&ch->lcdc->notifier != nb)
1252 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1253 __func__, action, event->data);
1256 case FB_EVENT_SUSPEND:
1257 if (try_module_get(board_cfg->owner) && board_cfg->display_off) {
1258 board_cfg->display_off(board_cfg->board_data);
1259 module_put(board_cfg->owner);
1261 pm_runtime_put(info->device);
1262 sh_mobile_lcdc_stop(ch->lcdc);
1264 case FB_EVENT_RESUME:
1265 mutex_lock(&ch->open_lock);
1266 sh_mobile_fb_reconfig(info);
1267 mutex_unlock(&ch->open_lock);
1269 /* HDMI must be enabled before LCDC configuration */
1270 if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
1271 board_cfg->display_on(board_cfg->board_data, info);
1272 module_put(board_cfg->owner);
1275 ret = sh_mobile_lcdc_start(ch->lcdc);
1277 pm_runtime_get_sync(info->device);
1283 static int sh_mobile_lcdc_remove(struct platform_device *pdev);
1285 static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
1287 struct fb_info *info;
1288 struct sh_mobile_lcdc_priv *priv;
1289 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
1290 struct resource *res;
1296 dev_err(&pdev->dev, "no platform data defined\n");
1300 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1301 i = platform_get_irq(pdev, 0);
1302 if (!res || i < 0) {
1303 dev_err(&pdev->dev, "cannot get platform resources\n");
1307 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1309 dev_err(&pdev->dev, "cannot allocate device data\n");
1313 platform_set_drvdata(pdev, priv);
1315 error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
1316 dev_name(&pdev->dev), priv);
1318 dev_err(&pdev->dev, "unable to request irq\n");
1323 atomic_set(&priv->hw_usecnt, -1);
1326 for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1327 struct sh_mobile_lcdc_chan *ch = priv->ch + j;
1330 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1332 error = sh_mobile_lcdc_check_interface(ch);
1334 dev_err(&pdev->dev, "unsupported interface type\n");
1337 init_waitqueue_head(&ch->frame_end_wait);
1338 init_completion(&ch->vsync_completion);
1341 /* probe the backlight is there is one defined */
1342 if (ch->cfg.bl_info.max_brightness)
1343 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
1345 switch (pdata->ch[i].chan) {
1346 case LCDC_CHAN_MAINLCD:
1347 ch->enabled = 1 << 1;
1348 ch->reg_offs = lcdc_offs_mainlcd;
1351 case LCDC_CHAN_SUBLCD:
1352 ch->enabled = 1 << 2;
1353 ch->reg_offs = lcdc_offs_sublcd;
1360 dev_err(&pdev->dev, "no channels defined\n");
1365 /* for dual channel LCDC (MAIN + SUB) force shared bpp setting */
1367 priv->forced_bpp = pdata->ch[0].bpp;
1369 priv->base = ioremap_nocache(res->start, resource_size(res));
1373 error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
1375 dev_err(&pdev->dev, "unable to setup clocks\n");
1379 for (i = 0; i < j; i++) {
1380 struct fb_var_screeninfo *var;
1381 const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
1382 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1383 struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
1384 const struct fb_videomode *mode = cfg->lcd_cfg;
1385 unsigned long max_size = 0;
1389 ch->info = framebuffer_alloc(0, &pdev->dev);
1391 dev_err(&pdev->dev, "unable to allocate fb_info\n");
1398 info->fbops = &sh_mobile_lcdc_ops;
1401 mutex_init(&ch->open_lock);
1403 for (k = 0, lcd_cfg = mode;
1404 k < cfg->num_cfg && lcd_cfg;
1406 unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
1407 /* NV12 buffers must have even number of lines */
1408 if ((cfg->nonstd) && cfg->bpp == 12 &&
1409 (lcd_cfg->yres & 0x1)) {
1410 dev_err(&pdev->dev, "yres must be multiple of 2"
1411 " for YCbCr420 mode.\n");
1416 if (size > max_size) {
1423 max_size = MAX_XRES * MAX_YRES;
1425 dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
1426 max_cfg->xres, max_cfg->yres);
1428 info->fix = sh_mobile_lcdc_fix;
1429 info->fix.smem_len = max_size * 2 * cfg->bpp / 8;
1431 /* Only pan in 2 line steps for NV12 */
1432 if (cfg->nonstd && cfg->bpp == 12)
1433 info->fix.ypanstep = 2;
1436 mode = &default_720p;
1439 num_cfg = cfg->num_cfg;
1442 fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
1444 fb_videomode_to_var(var, mode);
1445 var->width = cfg->lcd_size_cfg.width;
1446 var->height = cfg->lcd_size_cfg.height;
1447 /* Default Y virtual resolution is 2x panel size */
1448 var->yres_virtual = var->yres * 2;
1449 var->activate = FB_ACTIVATE_NOW;
1451 error = sh_mobile_lcdc_set_bpp(var, cfg->bpp, cfg->nonstd);
1455 buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
1456 &ch->dma_handle, GFP_KERNEL);
1458 dev_err(&pdev->dev, "unable to allocate buffer\n");
1463 info->pseudo_palette = &ch->pseudo_palette;
1464 info->flags = FBINFO_FLAG_DEFAULT;
1466 error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1468 dev_err(&pdev->dev, "unable to allocate cmap\n");
1469 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1470 buf, ch->dma_handle);
1474 info->fix.smem_start = ch->dma_handle;
1476 info->fix.line_length = var->xres;
1478 info->fix.line_length = var->xres * (cfg->bpp / 8);
1480 info->screen_base = buf;
1481 info->device = &pdev->dev;
1482 ch->display_var = *var;
1488 error = sh_mobile_lcdc_start(priv);
1490 dev_err(&pdev->dev, "unable to start hardware\n");
1494 for (i = 0; i < j; i++) {
1495 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1499 if (info->fbdefio) {
1500 ch->sglist = vmalloc(sizeof(struct scatterlist) *
1501 info->fix.smem_len >> PAGE_SHIFT);
1503 dev_err(&pdev->dev, "cannot allocate sglist\n");
1508 info->bl_dev = ch->bl;
1510 error = register_framebuffer(info);
1515 "registered %s/%s as %dx%d %dbpp.\n",
1517 (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
1518 "mainlcd" : "sublcd",
1519 info->var.xres, info->var.yres,
1522 /* deferred io mode: disable clock to save power */
1523 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
1524 sh_mobile_lcdc_clk_off(priv);
1527 /* Failure ignored */
1528 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1529 fb_register_client(&priv->notifier);
1533 sh_mobile_lcdc_remove(pdev);
1538 static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1540 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1541 struct fb_info *info;
1544 fb_unregister_client(&priv->notifier);
1546 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
1547 if (priv->ch[i].info && priv->ch[i].info->dev)
1548 unregister_framebuffer(priv->ch[i].info);
1550 sh_mobile_lcdc_stop(priv);
1552 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1553 info = priv->ch[i].info;
1555 if (!info || !info->device)
1558 if (priv->ch[i].sglist)
1559 vfree(priv->ch[i].sglist);
1561 if (info->screen_base)
1562 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1564 priv->ch[i].dma_handle);
1565 fb_dealloc_cmap(&info->cmap);
1566 framebuffer_release(info);
1569 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1571 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
1575 clk_put(priv->dot_clk);
1578 pm_runtime_disable(priv->dev);
1581 iounmap(priv->base);
1584 free_irq(priv->irq, priv);
1589 static struct platform_driver sh_mobile_lcdc_driver = {
1591 .name = "sh_mobile_lcdc_fb",
1592 .owner = THIS_MODULE,
1593 .pm = &sh_mobile_lcdc_dev_pm_ops,
1595 .probe = sh_mobile_lcdc_probe,
1596 .remove = sh_mobile_lcdc_remove,
1599 static int __init sh_mobile_lcdc_init(void)
1601 return platform_driver_register(&sh_mobile_lcdc_driver);
1604 static void __exit sh_mobile_lcdc_exit(void)
1606 platform_driver_unregister(&sh_mobile_lcdc_driver);
1609 module_init(sh_mobile_lcdc_init);
1610 module_exit(sh_mobile_lcdc_exit);
1612 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1613 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1614 MODULE_LICENSE("GPL v2");