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 <video/sh_mobile_lcdc.h>
25 #include <asm/atomic.h>
27 #include "sh_mobile_lcdcfb.h"
29 #define SIDE_B_OFFSET 0x1000
30 #define MIRROR_OFFSET 0x2000
32 /* shared registers */
34 #define _LDDCKSTPR 0x414
37 #define _LDCNT1R 0x470
38 #define _LDCNT2R 0x474
39 #define _LDRCNTR 0x478
41 #define _LDDWD0R 0x800
46 /* shared registers and their order for context save/restore */
47 static int lcdc_shared_regs[] = {
55 #define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
60 static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
79 static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
97 #define START_LCDC 0x00000001
98 #define LCDC_RESET 0x00000100
99 #define DISPLAY_BEU 0x00000008
100 #define LCDC_ENABLE 0x00000001
101 #define LDINTR_FE 0x00000400
102 #define LDINTR_VSE 0x00000200
103 #define LDINTR_VEE 0x00000100
104 #define LDINTR_FS 0x00000004
105 #define LDINTR_VSS 0x00000002
106 #define LDINTR_VES 0x00000001
107 #define LDRCNTR_SRS 0x00020000
108 #define LDRCNTR_SRC 0x00010000
109 #define LDRCNTR_MRS 0x00000002
110 #define LDRCNTR_MRC 0x00000001
111 #define LDSR_MRS 0x00000100
113 static const struct fb_videomode default_720p = {
128 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
131 struct sh_mobile_lcdc_priv {
137 unsigned long lddckr;
138 struct sh_mobile_lcdc_chan ch[2];
139 struct notifier_block notifier;
140 unsigned long saved_shared_regs[NR_SHARED_REGS];
144 static bool banked(int reg_nr)
163 static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
164 int reg_nr, unsigned long data)
166 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
168 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
172 static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
173 int reg_nr, unsigned long data)
175 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
179 static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
182 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
185 static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
186 unsigned long reg_offs, unsigned long data)
188 iowrite32(data, priv->base + reg_offs);
191 static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
192 unsigned long reg_offs)
194 return ioread32(priv->base + reg_offs);
197 static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
198 unsigned long reg_offs,
199 unsigned long mask, unsigned long until)
201 while ((lcdc_read(priv, reg_offs) & mask) != until)
205 static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
207 return chan->cfg.chan == LCDC_CHAN_SUBLCD;
210 static void lcdc_sys_write_index(void *handle, unsigned long data)
212 struct sh_mobile_lcdc_chan *ch = handle;
214 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
215 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
216 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
217 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
220 static void lcdc_sys_write_data(void *handle, unsigned long data)
222 struct sh_mobile_lcdc_chan *ch = handle;
224 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
225 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
226 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
227 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
230 static unsigned long lcdc_sys_read_data(void *handle)
232 struct sh_mobile_lcdc_chan *ch = handle;
234 lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
235 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
236 lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
238 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
240 return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
243 struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
244 lcdc_sys_write_index,
249 static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
251 if (atomic_inc_and_test(&priv->hw_usecnt)) {
252 pm_runtime_get_sync(priv->dev);
254 clk_enable(priv->dot_clk);
258 static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
260 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
262 clk_disable(priv->dot_clk);
263 pm_runtime_put(priv->dev);
267 static int sh_mobile_lcdc_sginit(struct fb_info *info,
268 struct list_head *pagelist)
270 struct sh_mobile_lcdc_chan *ch = info->par;
271 unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
275 sg_init_table(ch->sglist, nr_pages_max);
277 list_for_each_entry(page, pagelist, lru)
278 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
283 static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
284 struct list_head *pagelist)
286 struct sh_mobile_lcdc_chan *ch = info->par;
287 struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_cfg;
289 /* enable clocks before accessing hardware */
290 sh_mobile_lcdc_clk_on(ch->lcdc);
293 * It's possible to get here without anything on the pagelist via
294 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
295 * invocation. In the former case, the acceleration routines are
296 * stepped in to when using the framebuffer console causing the
297 * workqueue to be scheduled without any dirty pages on the list.
299 * Despite this, a panel update is still needed given that the
300 * acceleration routines have their own methods for writing in
301 * that still need to be updated.
303 * The fsync() and empty pagelist case could be optimized for,
304 * but we don't bother, as any application exhibiting such
305 * behaviour is fundamentally broken anyways.
307 if (!list_empty(pagelist)) {
308 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
310 /* trigger panel update */
311 dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
312 if (bcfg->start_transfer)
313 bcfg->start_transfer(bcfg->board_data, ch,
314 &sh_mobile_lcdc_sys_bus_ops);
315 lcdc_write_chan(ch, LDSM2R, 1);
316 dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
318 if (bcfg->start_transfer)
319 bcfg->start_transfer(bcfg->board_data, ch,
320 &sh_mobile_lcdc_sys_bus_ops);
321 lcdc_write_chan(ch, LDSM2R, 1);
325 static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
327 struct fb_deferred_io *fbdefio = info->fbdefio;
330 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
333 static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
335 struct sh_mobile_lcdc_priv *priv = data;
336 struct sh_mobile_lcdc_chan *ch;
338 unsigned long ldintr;
342 /* acknowledge interrupt */
343 ldintr = tmp = lcdc_read(priv, _LDINTR);
345 * disable further VSYNC End IRQs, preserve all other enabled IRQs,
346 * write 0 to bits 0-6 to ack all triggered IRQs.
348 tmp &= 0xffffff00 & ~LDINTR_VEE;
349 lcdc_write(priv, _LDINTR, tmp);
351 /* figure out if this interrupt is for main or sub lcd */
352 is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
354 /* wake up channel and disable clocks */
355 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
362 if (ldintr & LDINTR_FS) {
363 if (is_sub == lcdc_chan_is_sublcd(ch)) {
365 wake_up(&ch->frame_end_wait);
367 sh_mobile_lcdc_clk_off(priv);
372 if (ldintr & LDINTR_VES)
373 complete(&ch->vsync_completion);
379 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
382 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
385 /* start or stop the lcdc */
387 lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
389 lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
391 /* wait until power is applied/stopped on all channels */
392 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
393 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
395 tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
396 if (start && tmp == 3)
398 if (!start && tmp == 0)
404 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
407 static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
409 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
410 unsigned long h_total, hsync_pos, display_h_total;
413 tmp = ch->ldmt1r_value;
414 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
415 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
416 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
417 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
418 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
419 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
420 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
421 lcdc_write_chan(ch, LDMT1R, tmp);
424 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
425 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
427 /* horizontal configuration */
428 h_total = display_var->xres + display_var->hsync_len +
429 display_var->left_margin + display_var->right_margin;
430 tmp = h_total / 8; /* HTCN */
431 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
432 lcdc_write_chan(ch, LDHCNR, tmp);
434 hsync_pos = display_var->xres + display_var->right_margin;
435 tmp = hsync_pos / 8; /* HSYNP */
436 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
437 lcdc_write_chan(ch, LDHSYNR, tmp);
439 /* vertical configuration */
440 tmp = display_var->yres + display_var->vsync_len +
441 display_var->upper_margin + display_var->lower_margin; /* VTLN */
442 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
443 lcdc_write_chan(ch, LDVLNR, tmp);
445 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
446 tmp |= display_var->vsync_len << 16; /* VSYNW */
447 lcdc_write_chan(ch, LDVSYNR, tmp);
449 /* Adjust horizontal synchronisation for HDMI */
450 display_h_total = display_var->xres + display_var->hsync_len +
451 display_var->left_margin + display_var->right_margin;
452 tmp = ((display_var->xres & 7) << 24) |
453 ((display_h_total & 7) << 16) |
454 ((display_var->hsync_len & 7) << 8) |
456 lcdc_write_chan(ch, LDHAJR, tmp);
459 static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
461 struct sh_mobile_lcdc_chan *ch;
462 struct sh_mobile_lcdc_board_cfg *board_cfg;
467 /* enable clocks before accessing the hardware */
468 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
469 if (priv->ch[k].enabled)
470 sh_mobile_lcdc_clk_on(priv);
473 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
474 lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
476 /* enable LCDC channels */
477 tmp = lcdc_read(priv, _LDCNT2R);
478 tmp |= priv->ch[0].enabled;
479 tmp |= priv->ch[1].enabled;
480 lcdc_write(priv, _LDCNT2R, tmp);
482 /* read data from external memory, avoid using the BEU for now */
483 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
485 /* stop the lcdc first */
486 sh_mobile_lcdc_start_stop(priv, 0);
488 /* configure clocks */
490 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
493 if (!priv->ch[k].enabled)
496 m = ch->cfg.clock_divider;
502 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
504 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider denominator */
505 lcdc_write_chan(ch, LDDCKPAT1R, 0);
506 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
509 lcdc_write(priv, _LDDCKR, tmp);
511 /* start dotclock again */
512 lcdc_write(priv, _LDDCKSTPR, 0);
513 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
515 /* interrupts are disabled to begin with */
516 lcdc_write(priv, _LDINTR, 0);
518 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
524 sh_mobile_lcdc_geometry(ch);
527 lcdc_write_chan(ch, LDPMR, 0);
529 board_cfg = &ch->cfg.board_cfg;
530 if (board_cfg->setup_sys)
531 ret = board_cfg->setup_sys(board_cfg->board_data, ch,
532 &sh_mobile_lcdc_sys_bus_ops);
537 /* word and long word swap */
538 lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 6);
540 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
543 if (!priv->ch[k].enabled)
546 /* set bpp format in PKF[4:0] */
547 tmp = lcdc_read_chan(ch, LDDFR);
549 tmp |= (ch->info->var.bits_per_pixel == 16) ? 3 : 0;
550 lcdc_write_chan(ch, LDDFR, tmp);
552 /* point out our frame buffer */
553 lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
556 lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
558 /* setup deferred io if SYS bus */
559 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
560 if (ch->ldmt1r_value & (1 << 12) && tmp) {
561 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
562 ch->defio.delay = msecs_to_jiffies(tmp);
563 ch->info->fbdefio = &ch->defio;
564 fb_deferred_io_init(ch->info);
567 lcdc_write_chan(ch, LDSM1R, 1);
569 /* enable "Frame End Interrupt Enable" bit */
570 lcdc_write(priv, _LDINTR, LDINTR_FE);
573 /* continuous read mode */
574 lcdc_write_chan(ch, LDSM1R, 0);
579 lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
582 sh_mobile_lcdc_start_stop(priv, 1);
585 /* tell the board code to enable the panel */
586 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
591 board_cfg = &ch->cfg.board_cfg;
592 if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
593 board_cfg->display_on(board_cfg->board_data, ch->info);
594 module_put(board_cfg->owner);
601 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
603 struct sh_mobile_lcdc_chan *ch;
604 struct sh_mobile_lcdc_board_cfg *board_cfg;
607 /* clean up deferred io and ask board code to disable panel */
608 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
614 * flush frame, and wait for frame end interrupt
615 * clean up deferred io and enable clock
617 if (ch->info && ch->info->fbdefio) {
619 schedule_delayed_work(&ch->info->deferred_work, 0);
620 wait_event(ch->frame_end_wait, ch->frame_end);
621 fb_deferred_io_cleanup(ch->info);
622 ch->info->fbdefio = NULL;
623 sh_mobile_lcdc_clk_on(priv);
626 board_cfg = &ch->cfg.board_cfg;
627 if (try_module_get(board_cfg->owner) && board_cfg->display_off) {
628 board_cfg->display_off(board_cfg->board_data);
629 module_put(board_cfg->owner);
635 sh_mobile_lcdc_start_stop(priv, 0);
640 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
641 if (priv->ch[k].enabled)
642 sh_mobile_lcdc_clk_off(priv);
645 static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
649 switch (ch->cfg.interface_type) {
650 case RGB8: ifm = 0; miftyp = 0; break;
651 case RGB9: ifm = 0; miftyp = 4; break;
652 case RGB12A: ifm = 0; miftyp = 5; break;
653 case RGB12B: ifm = 0; miftyp = 6; break;
654 case RGB16: ifm = 0; miftyp = 7; break;
655 case RGB18: ifm = 0; miftyp = 10; break;
656 case RGB24: ifm = 0; miftyp = 11; break;
657 case SYS8A: ifm = 1; miftyp = 0; break;
658 case SYS8B: ifm = 1; miftyp = 1; break;
659 case SYS8C: ifm = 1; miftyp = 2; break;
660 case SYS8D: ifm = 1; miftyp = 3; break;
661 case SYS9: ifm = 1; miftyp = 4; break;
662 case SYS12: ifm = 1; miftyp = 5; break;
663 case SYS16A: ifm = 1; miftyp = 7; break;
664 case SYS16B: ifm = 1; miftyp = 8; break;
665 case SYS16C: ifm = 1; miftyp = 9; break;
666 case SYS18: ifm = 1; miftyp = 10; break;
667 case SYS24: ifm = 1; miftyp = 11; break;
671 /* SUBLCD only supports SYS interface */
672 if (lcdc_chan_is_sublcd(ch)) {
679 ch->ldmt1r_value = (ifm << 12) | miftyp;
685 static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
687 struct sh_mobile_lcdc_priv *priv)
692 switch (clock_source) {
693 case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
694 case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
695 case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
700 priv->lddckr = icksel << 16;
703 priv->dot_clk = clk_get(&pdev->dev, str);
704 if (IS_ERR(priv->dot_clk)) {
705 dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
706 return PTR_ERR(priv->dot_clk);
710 /* Runtime PM support involves two step for this driver:
711 * 1) Enable Runtime PM
712 * 2) Force Runtime PM Resume since hardware is accessed from probe()
714 priv->dev = &pdev->dev;
715 pm_runtime_enable(priv->dev);
716 pm_runtime_resume(priv->dev);
720 static int sh_mobile_lcdc_setcolreg(u_int regno,
721 u_int red, u_int green, u_int blue,
722 u_int transp, struct fb_info *info)
724 u32 *palette = info->pseudo_palette;
726 if (regno >= PALETTE_NR)
729 /* only FB_VISUAL_TRUECOLOR supported */
731 red >>= 16 - info->var.red.length;
732 green >>= 16 - info->var.green.length;
733 blue >>= 16 - info->var.blue.length;
734 transp >>= 16 - info->var.transp.length;
736 palette[regno] = (red << info->var.red.offset) |
737 (green << info->var.green.offset) |
738 (blue << info->var.blue.offset) |
739 (transp << info->var.transp.offset);
744 static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
745 .id = "SH Mobile LCDC",
746 .type = FB_TYPE_PACKED_PIXELS,
747 .visual = FB_VISUAL_TRUECOLOR,
748 .accel = FB_ACCEL_NONE,
754 static void sh_mobile_lcdc_fillrect(struct fb_info *info,
755 const struct fb_fillrect *rect)
757 sys_fillrect(info, rect);
758 sh_mobile_lcdc_deferred_io_touch(info);
761 static void sh_mobile_lcdc_copyarea(struct fb_info *info,
762 const struct fb_copyarea *area)
764 sys_copyarea(info, area);
765 sh_mobile_lcdc_deferred_io_touch(info);
768 static void sh_mobile_lcdc_imageblit(struct fb_info *info,
769 const struct fb_image *image)
771 sys_imageblit(info, image);
772 sh_mobile_lcdc_deferred_io_touch(info);
775 static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
776 struct fb_info *info)
778 struct sh_mobile_lcdc_chan *ch = info->par;
779 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
780 unsigned long ldrcntr;
781 unsigned long new_pan_offset;
783 new_pan_offset = (var->yoffset * info->fix.line_length) +
784 (var->xoffset * (info->var.bits_per_pixel / 8));
786 if (new_pan_offset == ch->pan_offset)
787 return 0; /* No change, do nothing */
789 ldrcntr = lcdc_read(priv, _LDRCNTR);
791 /* Set the source address for the next refresh */
792 lcdc_write_chan_mirror(ch, LDSA1R, ch->dma_handle + new_pan_offset);
793 if (lcdc_chan_is_sublcd(ch))
794 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
796 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
798 ch->pan_offset = new_pan_offset;
800 sh_mobile_lcdc_deferred_io_touch(info);
805 static int sh_mobile_wait_for_vsync(struct fb_info *info)
807 struct sh_mobile_lcdc_chan *ch = info->par;
808 unsigned long ldintr;
811 /* Enable VSync End interrupt */
812 ldintr = lcdc_read(ch->lcdc, _LDINTR);
813 ldintr |= LDINTR_VEE;
814 lcdc_write(ch->lcdc, _LDINTR, ldintr);
816 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
817 msecs_to_jiffies(100));
824 static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
830 case FBIO_WAITFORVSYNC:
831 retval = sh_mobile_wait_for_vsync(info);
835 retval = -ENOIOCTLCMD;
841 static void sh_mobile_fb_reconfig(struct fb_info *info)
843 struct sh_mobile_lcdc_chan *ch = info->par;
844 struct fb_videomode mode1, mode2;
845 struct fb_event event;
846 int evnt = FB_EVENT_MODE_CHANGE_ALL;
848 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
849 /* More framebuffer users are active */
852 fb_var_to_videomode(&mode1, &ch->display_var);
853 fb_var_to_videomode(&mode2, &info->var);
855 if (fb_mode_is_equal(&mode1, &mode2))
858 /* Display has been re-plugged, framebuffer is free now, reconfigure */
859 if (fb_set_var(info, &ch->display_var) < 0)
860 /* Couldn't reconfigure, hopefully, can continue as before */
863 info->fix.line_length = mode1.xres * (ch->cfg.bpp / 8);
866 * fb_set_var() calls the notifier change internally, only if
867 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
868 * user event, we have to call the chain ourselves.
872 fb_notifier_call_chain(evnt, &event);
876 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
877 * user == 1, or with console sem held, if user == 0.
879 static int sh_mobile_release(struct fb_info *info, int user)
881 struct sh_mobile_lcdc_chan *ch = info->par;
883 mutex_lock(&ch->open_lock);
884 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
888 /* Nothing to reconfigure, when called from fbcon */
890 acquire_console_sem();
891 sh_mobile_fb_reconfig(info);
892 release_console_sem();
895 mutex_unlock(&ch->open_lock);
900 static int sh_mobile_open(struct fb_info *info, int user)
902 struct sh_mobile_lcdc_chan *ch = info->par;
904 mutex_lock(&ch->open_lock);
907 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
908 mutex_unlock(&ch->open_lock);
913 static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
915 struct sh_mobile_lcdc_chan *ch = info->par;
917 if (var->xres > MAX_XRES || var->yres > MAX_YRES ||
918 var->xres * var->yres * (ch->cfg.bpp / 8) * 2 > info->fix.smem_len) {
919 dev_warn(info->dev, "Invalid info: %u-%u-%u-%u x %u-%u-%u-%u @ %ukHz!\n",
920 var->left_margin, var->xres, var->right_margin, var->hsync_len,
921 var->upper_margin, var->yres, var->lower_margin, var->vsync_len,
922 PICOS2KHZ(var->pixclock));
928 static struct fb_ops sh_mobile_lcdc_ops = {
929 .owner = THIS_MODULE,
930 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
931 .fb_read = fb_sys_read,
932 .fb_write = fb_sys_write,
933 .fb_fillrect = sh_mobile_lcdc_fillrect,
934 .fb_copyarea = sh_mobile_lcdc_copyarea,
935 .fb_imageblit = sh_mobile_lcdc_imageblit,
936 .fb_pan_display = sh_mobile_fb_pan_display,
937 .fb_ioctl = sh_mobile_ioctl,
938 .fb_open = sh_mobile_open,
939 .fb_release = sh_mobile_release,
940 .fb_check_var = sh_mobile_check_var,
943 static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
946 case 16: /* PKF[4:0] = 00011 - RGB 565 */
947 var->red.offset = 11;
949 var->green.offset = 5;
950 var->green.length = 6;
951 var->blue.offset = 0;
952 var->blue.length = 5;
953 var->transp.offset = 0;
954 var->transp.length = 0;
957 case 32: /* PKF[4:0] = 00000 - RGB 888
958 * sh7722 pdf says 00RRGGBB but reality is GGBB00RR
959 * this may be because LDDDSR has word swap enabled..
963 var->green.offset = 24;
964 var->green.length = 8;
965 var->blue.offset = 16;
966 var->blue.length = 8;
967 var->transp.offset = 0;
968 var->transp.length = 0;
973 var->bits_per_pixel = bpp;
974 var->red.msb_right = 0;
975 var->green.msb_right = 0;
976 var->blue.msb_right = 0;
977 var->transp.msb_right = 0;
981 static int sh_mobile_lcdc_suspend(struct device *dev)
983 struct platform_device *pdev = to_platform_device(dev);
985 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
989 static int sh_mobile_lcdc_resume(struct device *dev)
991 struct platform_device *pdev = to_platform_device(dev);
993 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
996 static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
998 struct platform_device *pdev = to_platform_device(dev);
999 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
1000 struct sh_mobile_lcdc_chan *ch;
1003 /* save per-channel registers */
1004 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
1008 for (n = 0; n < NR_CH_REGS; n++)
1009 ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
1012 /* save shared registers */
1013 for (n = 0; n < NR_SHARED_REGS; n++)
1014 p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
1016 /* turn off LCDC hardware */
1017 lcdc_write(p, _LDCNT1R, 0);
1021 static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1023 struct platform_device *pdev = to_platform_device(dev);
1024 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
1025 struct sh_mobile_lcdc_chan *ch;
1028 /* restore per-channel registers */
1029 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
1033 for (n = 0; n < NR_CH_REGS; n++)
1034 lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
1037 /* restore shared registers */
1038 for (n = 0; n < NR_SHARED_REGS; n++)
1039 lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
1044 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
1045 .suspend = sh_mobile_lcdc_suspend,
1046 .resume = sh_mobile_lcdc_resume,
1047 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1048 .runtime_resume = sh_mobile_lcdc_runtime_resume,
1051 /* locking: called with info->lock held */
1052 static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1053 unsigned long action, void *data)
1055 struct fb_event *event = data;
1056 struct fb_info *info = event->info;
1057 struct sh_mobile_lcdc_chan *ch = info->par;
1058 struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
1061 if (&ch->lcdc->notifier != nb)
1064 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1065 __func__, action, event->data);
1068 case FB_EVENT_SUSPEND:
1069 if (try_module_get(board_cfg->owner) && board_cfg->display_off) {
1070 board_cfg->display_off(board_cfg->board_data);
1071 module_put(board_cfg->owner);
1073 pm_runtime_put(info->device);
1074 sh_mobile_lcdc_stop(ch->lcdc);
1076 case FB_EVENT_RESUME:
1077 mutex_lock(&ch->open_lock);
1078 sh_mobile_fb_reconfig(info);
1079 mutex_unlock(&ch->open_lock);
1081 /* HDMI must be enabled before LCDC configuration */
1082 if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
1083 board_cfg->display_on(board_cfg->board_data, info);
1084 module_put(board_cfg->owner);
1087 ret = sh_mobile_lcdc_start(ch->lcdc);
1089 pm_runtime_get_sync(info->device);
1095 static int sh_mobile_lcdc_remove(struct platform_device *pdev);
1097 static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
1099 struct fb_info *info;
1100 struct sh_mobile_lcdc_priv *priv;
1101 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
1102 struct resource *res;
1108 dev_err(&pdev->dev, "no platform data defined\n");
1112 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1113 i = platform_get_irq(pdev, 0);
1114 if (!res || i < 0) {
1115 dev_err(&pdev->dev, "cannot get platform resources\n");
1119 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1121 dev_err(&pdev->dev, "cannot allocate device data\n");
1125 platform_set_drvdata(pdev, priv);
1127 error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
1128 dev_name(&pdev->dev), priv);
1130 dev_err(&pdev->dev, "unable to request irq\n");
1135 atomic_set(&priv->hw_usecnt, -1);
1138 for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1139 struct sh_mobile_lcdc_chan *ch = priv->ch + j;
1142 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1144 error = sh_mobile_lcdc_check_interface(ch);
1146 dev_err(&pdev->dev, "unsupported interface type\n");
1149 init_waitqueue_head(&ch->frame_end_wait);
1150 init_completion(&ch->vsync_completion);
1153 switch (pdata->ch[i].chan) {
1154 case LCDC_CHAN_MAINLCD:
1155 ch->enabled = 1 << 1;
1156 ch->reg_offs = lcdc_offs_mainlcd;
1159 case LCDC_CHAN_SUBLCD:
1160 ch->enabled = 1 << 2;
1161 ch->reg_offs = lcdc_offs_sublcd;
1168 dev_err(&pdev->dev, "no channels defined\n");
1173 priv->base = ioremap_nocache(res->start, resource_size(res));
1177 error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
1179 dev_err(&pdev->dev, "unable to setup clocks\n");
1183 for (i = 0; i < j; i++) {
1184 struct fb_var_screeninfo *var;
1185 const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
1186 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1187 struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
1188 const struct fb_videomode *mode = cfg->lcd_cfg;
1189 unsigned long max_size = 0;
1193 ch->info = framebuffer_alloc(0, &pdev->dev);
1195 dev_err(&pdev->dev, "unable to allocate fb_info\n");
1202 info->fbops = &sh_mobile_lcdc_ops;
1205 mutex_init(&ch->open_lock);
1207 for (k = 0, lcd_cfg = mode;
1208 k < cfg->num_cfg && lcd_cfg;
1210 unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
1212 if (size > max_size) {
1219 max_size = MAX_XRES * MAX_YRES;
1221 dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
1222 max_cfg->xres, max_cfg->yres);
1224 info->fix = sh_mobile_lcdc_fix;
1225 info->fix.smem_len = max_size * (cfg->bpp / 8) * 2;
1228 mode = &default_720p;
1231 num_cfg = cfg->num_cfg;
1234 fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
1236 fb_videomode_to_var(var, mode);
1237 var->width = cfg->lcd_size_cfg.width;
1238 var->height = cfg->lcd_size_cfg.height;
1239 /* Default Y virtual resolution is 2x panel size */
1240 var->yres_virtual = var->yres * 2;
1241 var->activate = FB_ACTIVATE_NOW;
1243 error = sh_mobile_lcdc_set_bpp(var, cfg->bpp);
1247 buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
1248 &ch->dma_handle, GFP_KERNEL);
1250 dev_err(&pdev->dev, "unable to allocate buffer\n");
1255 info->pseudo_palette = &ch->pseudo_palette;
1256 info->flags = FBINFO_FLAG_DEFAULT;
1258 error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1260 dev_err(&pdev->dev, "unable to allocate cmap\n");
1261 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1262 buf, ch->dma_handle);
1266 info->fix.smem_start = ch->dma_handle;
1267 info->fix.line_length = var->xres * (cfg->bpp / 8);
1268 info->screen_base = buf;
1269 info->device = &pdev->dev;
1270 ch->display_var = *var;
1276 error = sh_mobile_lcdc_start(priv);
1278 dev_err(&pdev->dev, "unable to start hardware\n");
1282 for (i = 0; i < j; i++) {
1283 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1287 if (info->fbdefio) {
1288 ch->sglist = vmalloc(sizeof(struct scatterlist) *
1289 info->fix.smem_len >> PAGE_SHIFT);
1291 dev_err(&pdev->dev, "cannot allocate sglist\n");
1296 error = register_framebuffer(info);
1301 "registered %s/%s as %dx%d %dbpp.\n",
1303 (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
1304 "mainlcd" : "sublcd",
1305 info->var.xres, info->var.yres,
1308 /* deferred io mode: disable clock to save power */
1309 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
1310 sh_mobile_lcdc_clk_off(priv);
1313 /* Failure ignored */
1314 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1315 fb_register_client(&priv->notifier);
1319 sh_mobile_lcdc_remove(pdev);
1324 static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1326 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1327 struct fb_info *info;
1330 fb_unregister_client(&priv->notifier);
1332 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
1333 if (priv->ch[i].info && priv->ch[i].info->dev)
1334 unregister_framebuffer(priv->ch[i].info);
1336 sh_mobile_lcdc_stop(priv);
1338 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1339 info = priv->ch[i].info;
1341 if (!info || !info->device)
1344 if (priv->ch[i].sglist)
1345 vfree(priv->ch[i].sglist);
1347 if (info->screen_base)
1348 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1350 priv->ch[i].dma_handle);
1351 fb_dealloc_cmap(&info->cmap);
1352 framebuffer_release(info);
1356 clk_put(priv->dot_clk);
1359 pm_runtime_disable(priv->dev);
1362 iounmap(priv->base);
1365 free_irq(priv->irq, priv);
1370 static struct platform_driver sh_mobile_lcdc_driver = {
1372 .name = "sh_mobile_lcdc_fb",
1373 .owner = THIS_MODULE,
1374 .pm = &sh_mobile_lcdc_dev_pm_ops,
1376 .probe = sh_mobile_lcdc_probe,
1377 .remove = sh_mobile_lcdc_remove,
1380 static int __init sh_mobile_lcdc_init(void)
1382 return platform_driver_register(&sh_mobile_lcdc_driver);
1385 static void __exit sh_mobile_lcdc_exit(void)
1387 platform_driver_unregister(&sh_mobile_lcdc_driver);
1390 module_init(sh_mobile_lcdc_init);
1391 module_exit(sh_mobile_lcdc_exit);
1393 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1394 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1395 MODULE_LICENSE("GPL v2");