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];
142 int forced_bpp; /* 2 channel LCDC must share bpp setting */
145 static bool banked(int reg_nr)
164 static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
165 int reg_nr, unsigned long data)
167 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
169 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
173 static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
174 int reg_nr, unsigned long data)
176 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
180 static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
183 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
186 static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
187 unsigned long reg_offs, unsigned long data)
189 iowrite32(data, priv->base + reg_offs);
192 static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
193 unsigned long reg_offs)
195 return ioread32(priv->base + reg_offs);
198 static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
199 unsigned long reg_offs,
200 unsigned long mask, unsigned long until)
202 while ((lcdc_read(priv, reg_offs) & mask) != until)
206 static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
208 return chan->cfg.chan == LCDC_CHAN_SUBLCD;
211 static void lcdc_sys_write_index(void *handle, unsigned long data)
213 struct sh_mobile_lcdc_chan *ch = handle;
215 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
216 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
217 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
218 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
221 static void lcdc_sys_write_data(void *handle, unsigned long data)
223 struct sh_mobile_lcdc_chan *ch = handle;
225 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
226 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
227 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
228 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
231 static unsigned long lcdc_sys_read_data(void *handle)
233 struct sh_mobile_lcdc_chan *ch = handle;
235 lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
236 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
237 lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
239 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
241 return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
244 struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
245 lcdc_sys_write_index,
250 static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
252 if (atomic_inc_and_test(&priv->hw_usecnt)) {
253 pm_runtime_get_sync(priv->dev);
255 clk_enable(priv->dot_clk);
259 static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
261 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
263 clk_disable(priv->dot_clk);
264 pm_runtime_put(priv->dev);
268 static int sh_mobile_lcdc_sginit(struct fb_info *info,
269 struct list_head *pagelist)
271 struct sh_mobile_lcdc_chan *ch = info->par;
272 unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
276 sg_init_table(ch->sglist, nr_pages_max);
278 list_for_each_entry(page, pagelist, lru)
279 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
284 static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
285 struct list_head *pagelist)
287 struct sh_mobile_lcdc_chan *ch = info->par;
288 struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_cfg;
290 /* enable clocks before accessing hardware */
291 sh_mobile_lcdc_clk_on(ch->lcdc);
294 * It's possible to get here without anything on the pagelist via
295 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
296 * invocation. In the former case, the acceleration routines are
297 * stepped in to when using the framebuffer console causing the
298 * workqueue to be scheduled without any dirty pages on the list.
300 * Despite this, a panel update is still needed given that the
301 * acceleration routines have their own methods for writing in
302 * that still need to be updated.
304 * The fsync() and empty pagelist case could be optimized for,
305 * but we don't bother, as any application exhibiting such
306 * behaviour is fundamentally broken anyways.
308 if (!list_empty(pagelist)) {
309 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
311 /* trigger panel update */
312 dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
313 if (bcfg->start_transfer)
314 bcfg->start_transfer(bcfg->board_data, ch,
315 &sh_mobile_lcdc_sys_bus_ops);
316 lcdc_write_chan(ch, LDSM2R, 1);
317 dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
319 if (bcfg->start_transfer)
320 bcfg->start_transfer(bcfg->board_data, ch,
321 &sh_mobile_lcdc_sys_bus_ops);
322 lcdc_write_chan(ch, LDSM2R, 1);
326 static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
328 struct fb_deferred_io *fbdefio = info->fbdefio;
331 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
334 static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
336 struct sh_mobile_lcdc_priv *priv = data;
337 struct sh_mobile_lcdc_chan *ch;
339 unsigned long ldintr;
343 /* acknowledge interrupt */
344 ldintr = tmp = lcdc_read(priv, _LDINTR);
346 * disable further VSYNC End IRQs, preserve all other enabled IRQs,
347 * write 0 to bits 0-6 to ack all triggered IRQs.
349 tmp &= 0xffffff00 & ~LDINTR_VEE;
350 lcdc_write(priv, _LDINTR, tmp);
352 /* figure out if this interrupt is for main or sub lcd */
353 is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
355 /* wake up channel and disable clocks */
356 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
363 if (ldintr & LDINTR_FS) {
364 if (is_sub == lcdc_chan_is_sublcd(ch)) {
366 wake_up(&ch->frame_end_wait);
368 sh_mobile_lcdc_clk_off(priv);
373 if (ldintr & LDINTR_VES)
374 complete(&ch->vsync_completion);
380 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
383 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
386 /* start or stop the lcdc */
388 lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
390 lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
392 /* wait until power is applied/stopped on all channels */
393 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
394 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
396 tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
397 if (start && tmp == 3)
399 if (!start && tmp == 0)
405 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
408 static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
410 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
411 unsigned long h_total, hsync_pos, display_h_total;
414 tmp = ch->ldmt1r_value;
415 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
416 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
417 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
418 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
419 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
420 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
421 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
422 lcdc_write_chan(ch, LDMT1R, tmp);
425 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
426 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
428 /* horizontal configuration */
429 h_total = display_var->xres + display_var->hsync_len +
430 display_var->left_margin + display_var->right_margin;
431 tmp = h_total / 8; /* HTCN */
432 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
433 lcdc_write_chan(ch, LDHCNR, tmp);
435 hsync_pos = display_var->xres + display_var->right_margin;
436 tmp = hsync_pos / 8; /* HSYNP */
437 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
438 lcdc_write_chan(ch, LDHSYNR, tmp);
440 /* vertical configuration */
441 tmp = display_var->yres + display_var->vsync_len +
442 display_var->upper_margin + display_var->lower_margin; /* VTLN */
443 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
444 lcdc_write_chan(ch, LDVLNR, tmp);
446 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
447 tmp |= display_var->vsync_len << 16; /* VSYNW */
448 lcdc_write_chan(ch, LDVSYNR, tmp);
450 /* Adjust horizontal synchronisation for HDMI */
451 display_h_total = display_var->xres + display_var->hsync_len +
452 display_var->left_margin + display_var->right_margin;
453 tmp = ((display_var->xres & 7) << 24) |
454 ((display_h_total & 7) << 16) |
455 ((display_var->hsync_len & 7) << 8) |
457 lcdc_write_chan(ch, LDHAJR, tmp);
460 static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
462 struct sh_mobile_lcdc_chan *ch;
463 struct sh_mobile_lcdc_board_cfg *board_cfg;
469 /* enable clocks before accessing the hardware */
470 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
471 if (priv->ch[k].enabled) {
472 sh_mobile_lcdc_clk_on(priv);
474 bpp = priv->ch[k].info->var.bits_per_pixel;
479 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
480 lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
482 /* enable LCDC channels */
483 tmp = lcdc_read(priv, _LDCNT2R);
484 tmp |= priv->ch[0].enabled;
485 tmp |= priv->ch[1].enabled;
486 lcdc_write(priv, _LDCNT2R, tmp);
488 /* read data from external memory, avoid using the BEU for now */
489 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
491 /* stop the lcdc first */
492 sh_mobile_lcdc_start_stop(priv, 0);
494 /* configure clocks */
496 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
499 if (!priv->ch[k].enabled)
502 m = ch->cfg.clock_divider;
508 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
510 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider denominator */
511 lcdc_write_chan(ch, LDDCKPAT1R, 0);
512 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
515 lcdc_write(priv, _LDDCKR, tmp);
517 /* start dotclock again */
518 lcdc_write(priv, _LDDCKSTPR, 0);
519 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
521 /* interrupts are disabled to begin with */
522 lcdc_write(priv, _LDINTR, 0);
524 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
530 sh_mobile_lcdc_geometry(ch);
533 lcdc_write_chan(ch, LDPMR, 0);
535 board_cfg = &ch->cfg.board_cfg;
536 if (board_cfg->setup_sys)
537 ret = board_cfg->setup_sys(board_cfg->board_data, ch,
538 &sh_mobile_lcdc_sys_bus_ops);
543 /* word and long word swap */
546 lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 6);
549 lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 7);
552 lcdc_write(priv, _LDDDSR, lcdc_read(priv, _LDDDSR) | 4);
556 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
559 if (!priv->ch[k].enabled)
562 /* set bpp format in PKF[4:0] */
563 tmp = lcdc_read_chan(ch, LDDFR);
565 switch (ch->info->var.bits_per_pixel) {
575 lcdc_write_chan(ch, LDDFR, tmp);
577 /* point out our frame buffer */
578 lcdc_write_chan(ch, LDSA1R, ch->info->fix.smem_start);
581 lcdc_write_chan(ch, LDMLSR, ch->info->fix.line_length);
583 /* setup deferred io if SYS bus */
584 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
585 if (ch->ldmt1r_value & (1 << 12) && tmp) {
586 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
587 ch->defio.delay = msecs_to_jiffies(tmp);
588 ch->info->fbdefio = &ch->defio;
589 fb_deferred_io_init(ch->info);
592 lcdc_write_chan(ch, LDSM1R, 1);
594 /* enable "Frame End Interrupt Enable" bit */
595 lcdc_write(priv, _LDINTR, LDINTR_FE);
598 /* continuous read mode */
599 lcdc_write_chan(ch, LDSM1R, 0);
604 lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
607 sh_mobile_lcdc_start_stop(priv, 1);
610 /* tell the board code to enable the panel */
611 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
616 board_cfg = &ch->cfg.board_cfg;
617 if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
618 board_cfg->display_on(board_cfg->board_data, ch->info);
619 module_put(board_cfg->owner);
626 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
628 struct sh_mobile_lcdc_chan *ch;
629 struct sh_mobile_lcdc_board_cfg *board_cfg;
632 /* clean up deferred io and ask board code to disable panel */
633 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
639 * flush frame, and wait for frame end interrupt
640 * clean up deferred io and enable clock
642 if (ch->info && ch->info->fbdefio) {
644 schedule_delayed_work(&ch->info->deferred_work, 0);
645 wait_event(ch->frame_end_wait, ch->frame_end);
646 fb_deferred_io_cleanup(ch->info);
647 ch->info->fbdefio = NULL;
648 sh_mobile_lcdc_clk_on(priv);
651 board_cfg = &ch->cfg.board_cfg;
652 if (try_module_get(board_cfg->owner) && board_cfg->display_off) {
653 board_cfg->display_off(board_cfg->board_data);
654 module_put(board_cfg->owner);
660 sh_mobile_lcdc_start_stop(priv, 0);
665 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
666 if (priv->ch[k].enabled)
667 sh_mobile_lcdc_clk_off(priv);
670 static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
674 switch (ch->cfg.interface_type) {
675 case RGB8: ifm = 0; miftyp = 0; break;
676 case RGB9: ifm = 0; miftyp = 4; break;
677 case RGB12A: ifm = 0; miftyp = 5; break;
678 case RGB12B: ifm = 0; miftyp = 6; break;
679 case RGB16: ifm = 0; miftyp = 7; break;
680 case RGB18: ifm = 0; miftyp = 10; break;
681 case RGB24: ifm = 0; miftyp = 11; break;
682 case SYS8A: ifm = 1; miftyp = 0; break;
683 case SYS8B: ifm = 1; miftyp = 1; break;
684 case SYS8C: ifm = 1; miftyp = 2; break;
685 case SYS8D: ifm = 1; miftyp = 3; break;
686 case SYS9: ifm = 1; miftyp = 4; break;
687 case SYS12: ifm = 1; miftyp = 5; break;
688 case SYS16A: ifm = 1; miftyp = 7; break;
689 case SYS16B: ifm = 1; miftyp = 8; break;
690 case SYS16C: ifm = 1; miftyp = 9; break;
691 case SYS18: ifm = 1; miftyp = 10; break;
692 case SYS24: ifm = 1; miftyp = 11; break;
696 /* SUBLCD only supports SYS interface */
697 if (lcdc_chan_is_sublcd(ch)) {
704 ch->ldmt1r_value = (ifm << 12) | miftyp;
710 static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
712 struct sh_mobile_lcdc_priv *priv)
717 switch (clock_source) {
718 case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
719 case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
720 case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
725 priv->lddckr = icksel << 16;
728 priv->dot_clk = clk_get(&pdev->dev, str);
729 if (IS_ERR(priv->dot_clk)) {
730 dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
731 return PTR_ERR(priv->dot_clk);
735 /* Runtime PM support involves two step for this driver:
736 * 1) Enable Runtime PM
737 * 2) Force Runtime PM Resume since hardware is accessed from probe()
739 priv->dev = &pdev->dev;
740 pm_runtime_enable(priv->dev);
741 pm_runtime_resume(priv->dev);
745 static int sh_mobile_lcdc_setcolreg(u_int regno,
746 u_int red, u_int green, u_int blue,
747 u_int transp, struct fb_info *info)
749 u32 *palette = info->pseudo_palette;
751 if (regno >= PALETTE_NR)
754 /* only FB_VISUAL_TRUECOLOR supported */
756 red >>= 16 - info->var.red.length;
757 green >>= 16 - info->var.green.length;
758 blue >>= 16 - info->var.blue.length;
759 transp >>= 16 - info->var.transp.length;
761 palette[regno] = (red << info->var.red.offset) |
762 (green << info->var.green.offset) |
763 (blue << info->var.blue.offset) |
764 (transp << info->var.transp.offset);
769 static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
770 .id = "SH Mobile LCDC",
771 .type = FB_TYPE_PACKED_PIXELS,
772 .visual = FB_VISUAL_TRUECOLOR,
773 .accel = FB_ACCEL_NONE,
779 static void sh_mobile_lcdc_fillrect(struct fb_info *info,
780 const struct fb_fillrect *rect)
782 sys_fillrect(info, rect);
783 sh_mobile_lcdc_deferred_io_touch(info);
786 static void sh_mobile_lcdc_copyarea(struct fb_info *info,
787 const struct fb_copyarea *area)
789 sys_copyarea(info, area);
790 sh_mobile_lcdc_deferred_io_touch(info);
793 static void sh_mobile_lcdc_imageblit(struct fb_info *info,
794 const struct fb_image *image)
796 sys_imageblit(info, image);
797 sh_mobile_lcdc_deferred_io_touch(info);
800 static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
801 struct fb_info *info)
803 struct sh_mobile_lcdc_chan *ch = info->par;
804 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
805 unsigned long ldrcntr;
806 unsigned long new_pan_offset;
808 new_pan_offset = (var->yoffset * info->fix.line_length) +
809 (var->xoffset * (info->var.bits_per_pixel / 8));
811 if (new_pan_offset == ch->pan_offset)
812 return 0; /* No change, do nothing */
814 ldrcntr = lcdc_read(priv, _LDRCNTR);
816 /* Set the source address for the next refresh */
817 lcdc_write_chan_mirror(ch, LDSA1R, ch->dma_handle + new_pan_offset);
818 if (lcdc_chan_is_sublcd(ch))
819 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
821 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
823 ch->pan_offset = new_pan_offset;
825 sh_mobile_lcdc_deferred_io_touch(info);
830 static int sh_mobile_wait_for_vsync(struct fb_info *info)
832 struct sh_mobile_lcdc_chan *ch = info->par;
833 unsigned long ldintr;
836 /* Enable VSync End interrupt */
837 ldintr = lcdc_read(ch->lcdc, _LDINTR);
838 ldintr |= LDINTR_VEE;
839 lcdc_write(ch->lcdc, _LDINTR, ldintr);
841 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
842 msecs_to_jiffies(100));
849 static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
855 case FBIO_WAITFORVSYNC:
856 retval = sh_mobile_wait_for_vsync(info);
860 retval = -ENOIOCTLCMD;
866 static void sh_mobile_fb_reconfig(struct fb_info *info)
868 struct sh_mobile_lcdc_chan *ch = info->par;
869 struct fb_videomode mode1, mode2;
870 struct fb_event event;
871 int evnt = FB_EVENT_MODE_CHANGE_ALL;
873 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
874 /* More framebuffer users are active */
877 fb_var_to_videomode(&mode1, &ch->display_var);
878 fb_var_to_videomode(&mode2, &info->var);
880 if (fb_mode_is_equal(&mode1, &mode2))
883 /* Display has been re-plugged, framebuffer is free now, reconfigure */
884 if (fb_set_var(info, &ch->display_var) < 0)
885 /* Couldn't reconfigure, hopefully, can continue as before */
888 info->fix.line_length = mode1.xres * (ch->cfg.bpp / 8);
891 * fb_set_var() calls the notifier change internally, only if
892 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
893 * user event, we have to call the chain ourselves.
897 fb_notifier_call_chain(evnt, &event);
901 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
902 * user == 1, or with console sem held, if user == 0.
904 static int sh_mobile_release(struct fb_info *info, int user)
906 struct sh_mobile_lcdc_chan *ch = info->par;
908 mutex_lock(&ch->open_lock);
909 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
913 /* Nothing to reconfigure, when called from fbcon */
916 sh_mobile_fb_reconfig(info);
920 mutex_unlock(&ch->open_lock);
925 static int sh_mobile_open(struct fb_info *info, int user)
927 struct sh_mobile_lcdc_chan *ch = info->par;
929 mutex_lock(&ch->open_lock);
932 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
933 mutex_unlock(&ch->open_lock);
938 static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
940 struct sh_mobile_lcdc_chan *ch = info->par;
941 struct sh_mobile_lcdc_priv *p = ch->lcdc;
943 if (var->xres > MAX_XRES || var->yres > MAX_YRES ||
944 var->xres * var->yres * (ch->cfg.bpp / 8) * 2 > info->fix.smem_len) {
945 dev_warn(info->dev, "Invalid info: %u-%u-%u-%u x %u-%u-%u-%u @ %lukHz!\n",
946 var->left_margin, var->xres, var->right_margin, var->hsync_len,
947 var->upper_margin, var->yres, var->lower_margin, var->vsync_len,
948 PICOS2KHZ(var->pixclock));
952 /* only accept the forced_bpp for dual channel configurations */
953 if (p->forced_bpp && p->forced_bpp != var->bits_per_pixel)
956 switch (var->bits_per_pixel) {
957 case 16: /* PKF[4:0] = 00011 - RGB 565 */
958 case 24: /* PKF[4:0] = 01011 - RGB 888 */
959 case 32: /* PKF[4:0] = 00000 - RGBA 888 */
968 static struct fb_ops sh_mobile_lcdc_ops = {
969 .owner = THIS_MODULE,
970 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
971 .fb_read = fb_sys_read,
972 .fb_write = fb_sys_write,
973 .fb_fillrect = sh_mobile_lcdc_fillrect,
974 .fb_copyarea = sh_mobile_lcdc_copyarea,
975 .fb_imageblit = sh_mobile_lcdc_imageblit,
976 .fb_pan_display = sh_mobile_fb_pan_display,
977 .fb_ioctl = sh_mobile_ioctl,
978 .fb_open = sh_mobile_open,
979 .fb_release = sh_mobile_release,
980 .fb_check_var = sh_mobile_check_var,
983 static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp)
986 case 16: /* PKF[4:0] = 00011 - RGB 565 */
987 var->red.offset = 11;
989 var->green.offset = 5;
990 var->green.length = 6;
991 var->blue.offset = 0;
992 var->blue.length = 5;
993 var->transp.offset = 0;
994 var->transp.length = 0;
997 case 24: /* PKF[4:0] = 01011 - RGB 888 */
998 var->red.offset = 16;
1000 var->green.offset = 8;
1001 var->green.length = 8;
1002 var->blue.offset = 0;
1003 var->blue.length = 8;
1004 var->transp.offset = 0;
1005 var->transp.length = 0;
1008 case 32: /* PKF[4:0] = 00000 - RGBA 888 */
1009 var->red.offset = 16;
1010 var->red.length = 8;
1011 var->green.offset = 8;
1012 var->green.length = 8;
1013 var->blue.offset = 0;
1014 var->blue.length = 8;
1015 var->transp.offset = 24;
1016 var->transp.length = 8;
1021 var->bits_per_pixel = bpp;
1022 var->red.msb_right = 0;
1023 var->green.msb_right = 0;
1024 var->blue.msb_right = 0;
1025 var->transp.msb_right = 0;
1029 static int sh_mobile_lcdc_suspend(struct device *dev)
1031 struct platform_device *pdev = to_platform_device(dev);
1033 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
1037 static int sh_mobile_lcdc_resume(struct device *dev)
1039 struct platform_device *pdev = to_platform_device(dev);
1041 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
1044 static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
1046 struct platform_device *pdev = to_platform_device(dev);
1047 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
1048 struct sh_mobile_lcdc_chan *ch;
1051 /* save per-channel registers */
1052 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
1056 for (n = 0; n < NR_CH_REGS; n++)
1057 ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
1060 /* save shared registers */
1061 for (n = 0; n < NR_SHARED_REGS; n++)
1062 p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
1064 /* turn off LCDC hardware */
1065 lcdc_write(p, _LDCNT1R, 0);
1069 static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1071 struct platform_device *pdev = to_platform_device(dev);
1072 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
1073 struct sh_mobile_lcdc_chan *ch;
1076 /* restore per-channel registers */
1077 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
1081 for (n = 0; n < NR_CH_REGS; n++)
1082 lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
1085 /* restore shared registers */
1086 for (n = 0; n < NR_SHARED_REGS; n++)
1087 lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
1092 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
1093 .suspend = sh_mobile_lcdc_suspend,
1094 .resume = sh_mobile_lcdc_resume,
1095 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1096 .runtime_resume = sh_mobile_lcdc_runtime_resume,
1099 /* locking: called with info->lock held */
1100 static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1101 unsigned long action, void *data)
1103 struct fb_event *event = data;
1104 struct fb_info *info = event->info;
1105 struct sh_mobile_lcdc_chan *ch = info->par;
1106 struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
1109 if (&ch->lcdc->notifier != nb)
1112 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1113 __func__, action, event->data);
1116 case FB_EVENT_SUSPEND:
1117 if (try_module_get(board_cfg->owner) && board_cfg->display_off) {
1118 board_cfg->display_off(board_cfg->board_data);
1119 module_put(board_cfg->owner);
1121 pm_runtime_put(info->device);
1122 sh_mobile_lcdc_stop(ch->lcdc);
1124 case FB_EVENT_RESUME:
1125 mutex_lock(&ch->open_lock);
1126 sh_mobile_fb_reconfig(info);
1127 mutex_unlock(&ch->open_lock);
1129 /* HDMI must be enabled before LCDC configuration */
1130 if (try_module_get(board_cfg->owner) && board_cfg->display_on) {
1131 board_cfg->display_on(board_cfg->board_data, info);
1132 module_put(board_cfg->owner);
1135 ret = sh_mobile_lcdc_start(ch->lcdc);
1137 pm_runtime_get_sync(info->device);
1143 static int sh_mobile_lcdc_remove(struct platform_device *pdev);
1145 static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
1147 struct fb_info *info;
1148 struct sh_mobile_lcdc_priv *priv;
1149 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
1150 struct resource *res;
1156 dev_err(&pdev->dev, "no platform data defined\n");
1160 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1161 i = platform_get_irq(pdev, 0);
1162 if (!res || i < 0) {
1163 dev_err(&pdev->dev, "cannot get platform resources\n");
1167 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1169 dev_err(&pdev->dev, "cannot allocate device data\n");
1173 platform_set_drvdata(pdev, priv);
1175 error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
1176 dev_name(&pdev->dev), priv);
1178 dev_err(&pdev->dev, "unable to request irq\n");
1183 atomic_set(&priv->hw_usecnt, -1);
1186 for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1187 struct sh_mobile_lcdc_chan *ch = priv->ch + j;
1190 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1192 error = sh_mobile_lcdc_check_interface(ch);
1194 dev_err(&pdev->dev, "unsupported interface type\n");
1197 init_waitqueue_head(&ch->frame_end_wait);
1198 init_completion(&ch->vsync_completion);
1201 switch (pdata->ch[i].chan) {
1202 case LCDC_CHAN_MAINLCD:
1203 ch->enabled = 1 << 1;
1204 ch->reg_offs = lcdc_offs_mainlcd;
1207 case LCDC_CHAN_SUBLCD:
1208 ch->enabled = 1 << 2;
1209 ch->reg_offs = lcdc_offs_sublcd;
1216 dev_err(&pdev->dev, "no channels defined\n");
1221 /* for dual channel LCDC (MAIN + SUB) force shared bpp setting */
1223 priv->forced_bpp = pdata->ch[0].bpp;
1225 priv->base = ioremap_nocache(res->start, resource_size(res));
1229 error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
1231 dev_err(&pdev->dev, "unable to setup clocks\n");
1235 for (i = 0; i < j; i++) {
1236 struct fb_var_screeninfo *var;
1237 const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
1238 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1239 struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
1240 const struct fb_videomode *mode = cfg->lcd_cfg;
1241 unsigned long max_size = 0;
1245 ch->info = framebuffer_alloc(0, &pdev->dev);
1247 dev_err(&pdev->dev, "unable to allocate fb_info\n");
1254 info->fbops = &sh_mobile_lcdc_ops;
1257 mutex_init(&ch->open_lock);
1259 for (k = 0, lcd_cfg = mode;
1260 k < cfg->num_cfg && lcd_cfg;
1262 unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
1264 if (size > max_size) {
1271 max_size = MAX_XRES * MAX_YRES;
1273 dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
1274 max_cfg->xres, max_cfg->yres);
1276 info->fix = sh_mobile_lcdc_fix;
1277 info->fix.smem_len = max_size * (cfg->bpp / 8) * 2;
1280 mode = &default_720p;
1283 num_cfg = cfg->num_cfg;
1286 fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
1288 fb_videomode_to_var(var, mode);
1289 var->width = cfg->lcd_size_cfg.width;
1290 var->height = cfg->lcd_size_cfg.height;
1291 /* Default Y virtual resolution is 2x panel size */
1292 var->yres_virtual = var->yres * 2;
1293 var->activate = FB_ACTIVATE_NOW;
1295 error = sh_mobile_lcdc_set_bpp(var, cfg->bpp);
1299 buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
1300 &ch->dma_handle, GFP_KERNEL);
1302 dev_err(&pdev->dev, "unable to allocate buffer\n");
1307 info->pseudo_palette = &ch->pseudo_palette;
1308 info->flags = FBINFO_FLAG_DEFAULT;
1310 error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1312 dev_err(&pdev->dev, "unable to allocate cmap\n");
1313 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1314 buf, ch->dma_handle);
1318 info->fix.smem_start = ch->dma_handle;
1319 info->fix.line_length = var->xres * (cfg->bpp / 8);
1320 info->screen_base = buf;
1321 info->device = &pdev->dev;
1322 ch->display_var = *var;
1328 error = sh_mobile_lcdc_start(priv);
1330 dev_err(&pdev->dev, "unable to start hardware\n");
1334 for (i = 0; i < j; i++) {
1335 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1339 if (info->fbdefio) {
1340 ch->sglist = vmalloc(sizeof(struct scatterlist) *
1341 info->fix.smem_len >> PAGE_SHIFT);
1343 dev_err(&pdev->dev, "cannot allocate sglist\n");
1348 error = register_framebuffer(info);
1353 "registered %s/%s as %dx%d %dbpp.\n",
1355 (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
1356 "mainlcd" : "sublcd",
1357 info->var.xres, info->var.yres,
1360 /* deferred io mode: disable clock to save power */
1361 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
1362 sh_mobile_lcdc_clk_off(priv);
1365 /* Failure ignored */
1366 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1367 fb_register_client(&priv->notifier);
1371 sh_mobile_lcdc_remove(pdev);
1376 static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1378 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1379 struct fb_info *info;
1382 fb_unregister_client(&priv->notifier);
1384 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
1385 if (priv->ch[i].info && priv->ch[i].info->dev)
1386 unregister_framebuffer(priv->ch[i].info);
1388 sh_mobile_lcdc_stop(priv);
1390 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1391 info = priv->ch[i].info;
1393 if (!info || !info->device)
1396 if (priv->ch[i].sglist)
1397 vfree(priv->ch[i].sglist);
1399 if (info->screen_base)
1400 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1402 priv->ch[i].dma_handle);
1403 fb_dealloc_cmap(&info->cmap);
1404 framebuffer_release(info);
1408 clk_put(priv->dot_clk);
1411 pm_runtime_disable(priv->dev);
1414 iounmap(priv->base);
1417 free_irq(priv->irq, priv);
1422 static struct platform_driver sh_mobile_lcdc_driver = {
1424 .name = "sh_mobile_lcdc_fb",
1425 .owner = THIS_MODULE,
1426 .pm = &sh_mobile_lcdc_dev_pm_ops,
1428 .probe = sh_mobile_lcdc_probe,
1429 .remove = sh_mobile_lcdc_remove,
1432 static int __init sh_mobile_lcdc_init(void)
1434 return platform_driver_register(&sh_mobile_lcdc_driver);
1437 static void __exit sh_mobile_lcdc_exit(void)
1439 platform_driver_unregister(&sh_mobile_lcdc_driver);
1442 module_init(sh_mobile_lcdc_init);
1443 module_exit(sh_mobile_lcdc_exit);
1445 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1446 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1447 MODULE_LICENSE("GPL v2");