Add linux-next specific files for 20110801
[linux-2.6/next.git] / drivers / video / sh_mobile_lcdcfb.c
blob499d5489201754d1c99a1166395d564363ca3b7f
1 /*
2 * SuperH Mobile LCDC Framebuffer
4 * Copyright (c) 2008 Magnus Damm
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/mm.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 <linux/module.h>
27 #include <video/sh_mobile_lcdc.h>
28 #include <linux/atomic.h>
30 #include "sh_mobile_lcdcfb.h"
31 #include "sh_mobile_meram.h"
33 #define SIDE_B_OFFSET 0x1000
34 #define MIRROR_OFFSET 0x2000
36 /* shared registers */
37 #define _LDDCKR 0x410
38 #define _LDDCKSTPR 0x414
39 #define _LDINTR 0x468
40 #define _LDSR 0x46c
41 #define _LDCNT1R 0x470
42 #define _LDCNT2R 0x474
43 #define _LDRCNTR 0x478
44 #define _LDDDSR 0x47c
45 #define _LDDWD0R 0x800
46 #define _LDDRDR 0x840
47 #define _LDDWAR 0x900
48 #define _LDDRAR 0x904
50 /* shared registers and their order for context save/restore */
51 static int lcdc_shared_regs[] = {
52 _LDDCKR,
53 _LDDCKSTPR,
54 _LDINTR,
55 _LDDDSR,
56 _LDCNT1R,
57 _LDCNT2R,
59 #define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)
61 #define MAX_XRES 1920
62 #define MAX_YRES 1080
64 static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
65 [LDDCKPAT1R] = 0x400,
66 [LDDCKPAT2R] = 0x404,
67 [LDMT1R] = 0x418,
68 [LDMT2R] = 0x41c,
69 [LDMT3R] = 0x420,
70 [LDDFR] = 0x424,
71 [LDSM1R] = 0x428,
72 [LDSM2R] = 0x42c,
73 [LDSA1R] = 0x430,
74 [LDSA2R] = 0x434,
75 [LDMLSR] = 0x438,
76 [LDHCNR] = 0x448,
77 [LDHSYNR] = 0x44c,
78 [LDVLNR] = 0x450,
79 [LDVSYNR] = 0x454,
80 [LDPMR] = 0x460,
81 [LDHAJR] = 0x4a0,
84 static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
85 [LDDCKPAT1R] = 0x408,
86 [LDDCKPAT2R] = 0x40c,
87 [LDMT1R] = 0x600,
88 [LDMT2R] = 0x604,
89 [LDMT3R] = 0x608,
90 [LDDFR] = 0x60c,
91 [LDSM1R] = 0x610,
92 [LDSM2R] = 0x614,
93 [LDSA1R] = 0x618,
94 [LDMLSR] = 0x620,
95 [LDHCNR] = 0x624,
96 [LDHSYNR] = 0x628,
97 [LDVLNR] = 0x62c,
98 [LDVSYNR] = 0x630,
99 [LDPMR] = 0x63c,
102 #define START_LCDC 0x00000001
103 #define LCDC_RESET 0x00000100
104 #define DISPLAY_BEU 0x00000008
105 #define LCDC_ENABLE 0x00000001
106 #define LDINTR_FE 0x00000400
107 #define LDINTR_VSE 0x00000200
108 #define LDINTR_VEE 0x00000100
109 #define LDINTR_FS 0x00000004
110 #define LDINTR_VSS 0x00000002
111 #define LDINTR_VES 0x00000001
112 #define LDRCNTR_SRS 0x00020000
113 #define LDRCNTR_SRC 0x00010000
114 #define LDRCNTR_MRS 0x00000002
115 #define LDRCNTR_MRC 0x00000001
116 #define LDSR_MRS 0x00000100
118 static const struct fb_videomode default_720p = {
119 .name = "HDMI 720p",
120 .xres = 1280,
121 .yres = 720,
123 .left_margin = 220,
124 .right_margin = 110,
125 .hsync_len = 40,
127 .upper_margin = 20,
128 .lower_margin = 5,
129 .vsync_len = 5,
131 .pixclock = 13468,
132 .refresh = 60,
133 .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
136 struct sh_mobile_lcdc_priv {
137 void __iomem *base;
138 int irq;
139 atomic_t hw_usecnt;
140 struct device *dev;
141 struct clk *dot_clk;
142 unsigned long lddckr;
143 struct sh_mobile_lcdc_chan ch[2];
144 struct notifier_block notifier;
145 unsigned long saved_shared_regs[NR_SHARED_REGS];
146 int started;
147 int forced_bpp; /* 2 channel LCDC must share bpp setting */
148 struct sh_mobile_meram_info *meram_dev;
151 static bool banked(int reg_nr)
153 switch (reg_nr) {
154 case LDMT1R:
155 case LDMT2R:
156 case LDMT3R:
157 case LDDFR:
158 case LDSM1R:
159 case LDSA1R:
160 case LDSA2R:
161 case LDMLSR:
162 case LDHCNR:
163 case LDHSYNR:
164 case LDVLNR:
165 case LDVSYNR:
166 return true;
168 return false;
171 static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
172 int reg_nr, unsigned long data)
174 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
175 if (banked(reg_nr))
176 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
177 SIDE_B_OFFSET);
180 static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
181 int reg_nr, unsigned long data)
183 iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
184 MIRROR_OFFSET);
187 static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
188 int reg_nr)
190 return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
193 static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
194 unsigned long reg_offs, unsigned long data)
196 iowrite32(data, priv->base + reg_offs);
199 static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
200 unsigned long reg_offs)
202 return ioread32(priv->base + reg_offs);
205 static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
206 unsigned long reg_offs,
207 unsigned long mask, unsigned long until)
209 while ((lcdc_read(priv, reg_offs) & mask) != until)
210 cpu_relax();
213 static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
215 return chan->cfg.chan == LCDC_CHAN_SUBLCD;
218 static void lcdc_sys_write_index(void *handle, unsigned long data)
220 struct sh_mobile_lcdc_chan *ch = handle;
222 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
223 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
224 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
225 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
228 static void lcdc_sys_write_data(void *handle, unsigned long data)
230 struct sh_mobile_lcdc_chan *ch = handle;
232 lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
233 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
234 lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
235 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
238 static unsigned long lcdc_sys_read_data(void *handle)
240 struct sh_mobile_lcdc_chan *ch = handle;
242 lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
243 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
244 lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
245 udelay(1);
246 lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
248 return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
251 struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
252 lcdc_sys_write_index,
253 lcdc_sys_write_data,
254 lcdc_sys_read_data,
257 static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
259 if (atomic_inc_and_test(&priv->hw_usecnt)) {
260 pm_runtime_get_sync(priv->dev);
261 if (priv->dot_clk)
262 clk_enable(priv->dot_clk);
266 static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
268 if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
269 if (priv->dot_clk)
270 clk_disable(priv->dot_clk);
271 pm_runtime_put(priv->dev);
275 static int sh_mobile_lcdc_sginit(struct fb_info *info,
276 struct list_head *pagelist)
278 struct sh_mobile_lcdc_chan *ch = info->par;
279 unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
280 struct page *page;
281 int nr_pages = 0;
283 sg_init_table(ch->sglist, nr_pages_max);
285 list_for_each_entry(page, pagelist, lru)
286 sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
288 return nr_pages;
291 static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
292 struct list_head *pagelist)
294 struct sh_mobile_lcdc_chan *ch = info->par;
295 struct sh_mobile_lcdc_board_cfg *bcfg = &ch->cfg.board_cfg;
297 /* enable clocks before accessing hardware */
298 sh_mobile_lcdc_clk_on(ch->lcdc);
301 * It's possible to get here without anything on the pagelist via
302 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
303 * invocation. In the former case, the acceleration routines are
304 * stepped in to when using the framebuffer console causing the
305 * workqueue to be scheduled without any dirty pages on the list.
307 * Despite this, a panel update is still needed given that the
308 * acceleration routines have their own methods for writing in
309 * that still need to be updated.
311 * The fsync() and empty pagelist case could be optimized for,
312 * but we don't bother, as any application exhibiting such
313 * behaviour is fundamentally broken anyways.
315 if (!list_empty(pagelist)) {
316 unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
318 /* trigger panel update */
319 dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
320 if (bcfg->start_transfer)
321 bcfg->start_transfer(bcfg->board_data, ch,
322 &sh_mobile_lcdc_sys_bus_ops);
323 lcdc_write_chan(ch, LDSM2R, 1);
324 dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
325 } else {
326 if (bcfg->start_transfer)
327 bcfg->start_transfer(bcfg->board_data, ch,
328 &sh_mobile_lcdc_sys_bus_ops);
329 lcdc_write_chan(ch, LDSM2R, 1);
333 static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
335 struct fb_deferred_io *fbdefio = info->fbdefio;
337 if (fbdefio)
338 schedule_delayed_work(&info->deferred_work, fbdefio->delay);
341 static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
343 struct sh_mobile_lcdc_priv *priv = data;
344 struct sh_mobile_lcdc_chan *ch;
345 unsigned long tmp;
346 unsigned long ldintr;
347 int is_sub;
348 int k;
350 /* acknowledge interrupt */
351 ldintr = tmp = lcdc_read(priv, _LDINTR);
353 * disable further VSYNC End IRQs, preserve all other enabled IRQs,
354 * write 0 to bits 0-6 to ack all triggered IRQs.
356 tmp &= 0xffffff00 & ~LDINTR_VEE;
357 lcdc_write(priv, _LDINTR, tmp);
359 /* figure out if this interrupt is for main or sub lcd */
360 is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;
362 /* wake up channel and disable clocks */
363 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
364 ch = &priv->ch[k];
366 if (!ch->enabled)
367 continue;
369 /* Frame Start */
370 if (ldintr & LDINTR_FS) {
371 if (is_sub == lcdc_chan_is_sublcd(ch)) {
372 ch->frame_end = 1;
373 wake_up(&ch->frame_end_wait);
375 sh_mobile_lcdc_clk_off(priv);
379 /* VSYNC End */
380 if (ldintr & LDINTR_VES)
381 complete(&ch->vsync_completion);
384 return IRQ_HANDLED;
387 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
388 int start)
390 unsigned long tmp = lcdc_read(priv, _LDCNT2R);
391 int k;
393 /* start or stop the lcdc */
394 if (start)
395 lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
396 else
397 lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);
399 /* wait until power is applied/stopped on all channels */
400 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
401 if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
402 while (1) {
403 tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
404 if (start && tmp == 3)
405 break;
406 if (!start && tmp == 0)
407 break;
408 cpu_relax();
411 if (!start)
412 lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
415 static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
417 struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
418 unsigned long h_total, hsync_pos, display_h_total;
419 u32 tmp;
421 tmp = ch->ldmt1r_value;
422 tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
423 tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
424 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
425 tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
426 tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
427 tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
428 tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
429 lcdc_write_chan(ch, LDMT1R, tmp);
431 /* setup SYS bus */
432 lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
433 lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
435 /* horizontal configuration */
436 h_total = display_var->xres + display_var->hsync_len +
437 display_var->left_margin + display_var->right_margin;
438 tmp = h_total / 8; /* HTCN */
439 tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
440 lcdc_write_chan(ch, LDHCNR, tmp);
442 hsync_pos = display_var->xres + display_var->right_margin;
443 tmp = hsync_pos / 8; /* HSYNP */
444 tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
445 lcdc_write_chan(ch, LDHSYNR, tmp);
447 /* vertical configuration */
448 tmp = display_var->yres + display_var->vsync_len +
449 display_var->upper_margin + display_var->lower_margin; /* VTLN */
450 tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
451 lcdc_write_chan(ch, LDVLNR, tmp);
453 tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
454 tmp |= display_var->vsync_len << 16; /* VSYNW */
455 lcdc_write_chan(ch, LDVSYNR, tmp);
457 /* Adjust horizontal synchronisation for HDMI */
458 display_h_total = display_var->xres + display_var->hsync_len +
459 display_var->left_margin + display_var->right_margin;
460 tmp = ((display_var->xres & 7) << 24) |
461 ((display_h_total & 7) << 16) |
462 ((display_var->hsync_len & 7) << 8) |
463 hsync_pos;
464 lcdc_write_chan(ch, LDHAJR, tmp);
467 static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
469 struct sh_mobile_lcdc_chan *ch;
470 struct sh_mobile_lcdc_board_cfg *board_cfg;
471 unsigned long tmp;
472 int bpp = 0;
473 unsigned long ldddsr;
474 int k, m, ret;
476 /* enable clocks before accessing the hardware */
477 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
478 if (priv->ch[k].enabled) {
479 sh_mobile_lcdc_clk_on(priv);
480 if (!bpp)
481 bpp = priv->ch[k].info->var.bits_per_pixel;
485 /* reset */
486 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
487 lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
489 /* enable LCDC channels */
490 tmp = lcdc_read(priv, _LDCNT2R);
491 tmp |= priv->ch[0].enabled;
492 tmp |= priv->ch[1].enabled;
493 lcdc_write(priv, _LDCNT2R, tmp);
495 /* read data from external memory, avoid using the BEU for now */
496 lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
498 /* stop the lcdc first */
499 sh_mobile_lcdc_start_stop(priv, 0);
501 /* configure clocks */
502 tmp = priv->lddckr;
503 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
504 ch = &priv->ch[k];
506 if (!priv->ch[k].enabled)
507 continue;
509 m = ch->cfg.clock_divider;
510 if (!m)
511 continue;
513 if (m == 1)
514 m = 1 << 6;
515 tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
517 /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider denominator */
518 lcdc_write_chan(ch, LDDCKPAT1R, 0);
519 lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
522 lcdc_write(priv, _LDDCKR, tmp);
524 /* start dotclock again */
525 lcdc_write(priv, _LDDCKSTPR, 0);
526 lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
528 /* interrupts are disabled to begin with */
529 lcdc_write(priv, _LDINTR, 0);
531 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
532 ch = &priv->ch[k];
534 if (!ch->enabled)
535 continue;
537 sh_mobile_lcdc_geometry(ch);
539 /* power supply */
540 lcdc_write_chan(ch, LDPMR, 0);
542 board_cfg = &ch->cfg.board_cfg;
543 if (board_cfg->setup_sys) {
544 ret = board_cfg->setup_sys(board_cfg->board_data,
545 ch, &sh_mobile_lcdc_sys_bus_ops);
546 if (ret)
547 return ret;
551 /* word and long word swap */
552 ldddsr = lcdc_read(priv, _LDDDSR);
553 if (priv->ch[0].info->var.nonstd)
554 lcdc_write(priv, _LDDDSR, ldddsr | 7);
555 else {
556 switch (bpp) {
557 case 16:
558 lcdc_write(priv, _LDDDSR, ldddsr | 6);
559 break;
560 case 24:
561 lcdc_write(priv, _LDDDSR, ldddsr | 7);
562 break;
563 case 32:
564 lcdc_write(priv, _LDDDSR, ldddsr | 4);
565 break;
569 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
570 unsigned long base_addr_y;
571 unsigned long base_addr_c = 0;
572 int pitch;
573 ch = &priv->ch[k];
575 if (!priv->ch[k].enabled)
576 continue;
578 /* set bpp format in PKF[4:0] */
579 tmp = lcdc_read_chan(ch, LDDFR);
580 tmp &= ~0x0003031f;
581 if (ch->info->var.nonstd) {
582 tmp |= (ch->info->var.nonstd << 16);
583 switch (ch->info->var.bits_per_pixel) {
584 case 12:
585 break;
586 case 16:
587 tmp |= (0x1 << 8);
588 break;
589 case 24:
590 tmp |= (0x2 << 8);
591 break;
593 } else {
594 switch (ch->info->var.bits_per_pixel) {
595 case 16:
596 tmp |= 0x03;
597 break;
598 case 24:
599 tmp |= 0x0b;
600 break;
601 case 32:
602 break;
605 lcdc_write_chan(ch, LDDFR, tmp);
607 base_addr_y = ch->info->fix.smem_start;
608 base_addr_c = base_addr_y +
609 ch->info->var.xres *
610 ch->info->var.yres_virtual;
611 pitch = ch->info->fix.line_length;
613 /* test if we can enable meram */
614 if (ch->cfg.meram_cfg && priv->meram_dev &&
615 priv->meram_dev->ops) {
616 struct sh_mobile_meram_cfg *cfg;
617 struct sh_mobile_meram_info *mdev;
618 unsigned long icb_addr_y, icb_addr_c;
619 int icb_pitch;
620 int pf;
622 cfg = ch->cfg.meram_cfg;
623 mdev = priv->meram_dev;
624 /* we need to de-init configured ICBs before we
625 * we can re-initialize them.
627 if (ch->meram_enabled)
628 mdev->ops->meram_unregister(mdev, cfg);
630 ch->meram_enabled = 0;
632 if (ch->info->var.nonstd) {
633 if (ch->info->var.bits_per_pixel == 24)
634 pf = SH_MOBILE_MERAM_PF_NV24;
635 else
636 pf = SH_MOBILE_MERAM_PF_NV;
637 } else {
638 pf = SH_MOBILE_MERAM_PF_RGB;
641 ret = mdev->ops->meram_register(mdev, cfg, pitch,
642 ch->info->var.yres,
644 base_addr_y,
645 base_addr_c,
646 &icb_addr_y,
647 &icb_addr_c,
648 &icb_pitch);
649 if (!ret) {
650 /* set LDSA1R value */
651 base_addr_y = icb_addr_y;
652 pitch = icb_pitch;
654 /* set LDSA2R value if required */
655 if (base_addr_c)
656 base_addr_c = icb_addr_c;
658 ch->meram_enabled = 1;
662 /* point out our frame buffer */
663 lcdc_write_chan(ch, LDSA1R, base_addr_y);
664 if (ch->info->var.nonstd)
665 lcdc_write_chan(ch, LDSA2R, base_addr_c);
667 /* set line size */
668 lcdc_write_chan(ch, LDMLSR, pitch);
670 /* setup deferred io if SYS bus */
671 tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
672 if (ch->ldmt1r_value & (1 << 12) && tmp) {
673 ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
674 ch->defio.delay = msecs_to_jiffies(tmp);
675 ch->info->fbdefio = &ch->defio;
676 fb_deferred_io_init(ch->info);
678 /* one-shot mode */
679 lcdc_write_chan(ch, LDSM1R, 1);
681 /* enable "Frame End Interrupt Enable" bit */
682 lcdc_write(priv, _LDINTR, LDINTR_FE);
684 } else {
685 /* continuous read mode */
686 lcdc_write_chan(ch, LDSM1R, 0);
690 /* display output */
691 lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);
693 /* start the lcdc */
694 sh_mobile_lcdc_start_stop(priv, 1);
695 priv->started = 1;
697 /* tell the board code to enable the panel */
698 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
699 ch = &priv->ch[k];
700 if (!ch->enabled)
701 continue;
703 board_cfg = &ch->cfg.board_cfg;
704 if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
705 board_cfg->display_on(board_cfg->board_data, ch->info);
706 module_put(board_cfg->owner);
709 if (ch->bl) {
710 ch->bl->props.power = FB_BLANK_UNBLANK;
711 backlight_update_status(ch->bl);
715 return 0;
718 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
720 struct sh_mobile_lcdc_chan *ch;
721 struct sh_mobile_lcdc_board_cfg *board_cfg;
722 int k;
724 /* clean up deferred io and ask board code to disable panel */
725 for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
726 ch = &priv->ch[k];
727 if (!ch->enabled)
728 continue;
730 /* deferred io mode:
731 * flush frame, and wait for frame end interrupt
732 * clean up deferred io and enable clock
734 if (ch->info && ch->info->fbdefio) {
735 ch->frame_end = 0;
736 schedule_delayed_work(&ch->info->deferred_work, 0);
737 wait_event(ch->frame_end_wait, ch->frame_end);
738 fb_deferred_io_cleanup(ch->info);
739 ch->info->fbdefio = NULL;
740 sh_mobile_lcdc_clk_on(priv);
743 if (ch->bl) {
744 ch->bl->props.power = FB_BLANK_POWERDOWN;
745 backlight_update_status(ch->bl);
748 board_cfg = &ch->cfg.board_cfg;
749 if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
750 board_cfg->display_off(board_cfg->board_data);
751 module_put(board_cfg->owner);
754 /* disable the meram */
755 if (ch->meram_enabled) {
756 struct sh_mobile_meram_cfg *cfg;
757 struct sh_mobile_meram_info *mdev;
758 cfg = ch->cfg.meram_cfg;
759 mdev = priv->meram_dev;
760 mdev->ops->meram_unregister(mdev, cfg);
761 ch->meram_enabled = 0;
766 /* stop the lcdc */
767 if (priv->started) {
768 sh_mobile_lcdc_start_stop(priv, 0);
769 priv->started = 0;
772 /* stop clocks */
773 for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
774 if (priv->ch[k].enabled)
775 sh_mobile_lcdc_clk_off(priv);
778 static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
780 int ifm, miftyp;
782 switch (ch->cfg.interface_type) {
783 case RGB8: ifm = 0; miftyp = 0; break;
784 case RGB9: ifm = 0; miftyp = 4; break;
785 case RGB12A: ifm = 0; miftyp = 5; break;
786 case RGB12B: ifm = 0; miftyp = 6; break;
787 case RGB16: ifm = 0; miftyp = 7; break;
788 case RGB18: ifm = 0; miftyp = 10; break;
789 case RGB24: ifm = 0; miftyp = 11; break;
790 case SYS8A: ifm = 1; miftyp = 0; break;
791 case SYS8B: ifm = 1; miftyp = 1; break;
792 case SYS8C: ifm = 1; miftyp = 2; break;
793 case SYS8D: ifm = 1; miftyp = 3; break;
794 case SYS9: ifm = 1; miftyp = 4; break;
795 case SYS12: ifm = 1; miftyp = 5; break;
796 case SYS16A: ifm = 1; miftyp = 7; break;
797 case SYS16B: ifm = 1; miftyp = 8; break;
798 case SYS16C: ifm = 1; miftyp = 9; break;
799 case SYS18: ifm = 1; miftyp = 10; break;
800 case SYS24: ifm = 1; miftyp = 11; break;
801 default: goto bad;
804 /* SUBLCD only supports SYS interface */
805 if (lcdc_chan_is_sublcd(ch)) {
806 if (ifm == 0)
807 goto bad;
808 else
809 ifm = 0;
812 ch->ldmt1r_value = (ifm << 12) | miftyp;
813 return 0;
814 bad:
815 return -EINVAL;
818 static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
819 int clock_source,
820 struct sh_mobile_lcdc_priv *priv)
822 char *str;
823 int icksel;
825 switch (clock_source) {
826 case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
827 case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
828 case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
829 default:
830 return -EINVAL;
833 priv->lddckr = icksel << 16;
835 if (str) {
836 priv->dot_clk = clk_get(&pdev->dev, str);
837 if (IS_ERR(priv->dot_clk)) {
838 dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
839 return PTR_ERR(priv->dot_clk);
843 /* Runtime PM support involves two step for this driver:
844 * 1) Enable Runtime PM
845 * 2) Force Runtime PM Resume since hardware is accessed from probe()
847 priv->dev = &pdev->dev;
848 pm_runtime_enable(priv->dev);
849 pm_runtime_resume(priv->dev);
850 return 0;
853 static int sh_mobile_lcdc_setcolreg(u_int regno,
854 u_int red, u_int green, u_int blue,
855 u_int transp, struct fb_info *info)
857 u32 *palette = info->pseudo_palette;
859 if (regno >= PALETTE_NR)
860 return -EINVAL;
862 /* only FB_VISUAL_TRUECOLOR supported */
864 red >>= 16 - info->var.red.length;
865 green >>= 16 - info->var.green.length;
866 blue >>= 16 - info->var.blue.length;
867 transp >>= 16 - info->var.transp.length;
869 palette[regno] = (red << info->var.red.offset) |
870 (green << info->var.green.offset) |
871 (blue << info->var.blue.offset) |
872 (transp << info->var.transp.offset);
874 return 0;
877 static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
878 .id = "SH Mobile LCDC",
879 .type = FB_TYPE_PACKED_PIXELS,
880 .visual = FB_VISUAL_TRUECOLOR,
881 .accel = FB_ACCEL_NONE,
882 .xpanstep = 0,
883 .ypanstep = 1,
884 .ywrapstep = 0,
887 static void sh_mobile_lcdc_fillrect(struct fb_info *info,
888 const struct fb_fillrect *rect)
890 sys_fillrect(info, rect);
891 sh_mobile_lcdc_deferred_io_touch(info);
894 static void sh_mobile_lcdc_copyarea(struct fb_info *info,
895 const struct fb_copyarea *area)
897 sys_copyarea(info, area);
898 sh_mobile_lcdc_deferred_io_touch(info);
901 static void sh_mobile_lcdc_imageblit(struct fb_info *info,
902 const struct fb_image *image)
904 sys_imageblit(info, image);
905 sh_mobile_lcdc_deferred_io_touch(info);
908 static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
909 struct fb_info *info)
911 struct sh_mobile_lcdc_chan *ch = info->par;
912 struct sh_mobile_lcdc_priv *priv = ch->lcdc;
913 unsigned long ldrcntr;
914 unsigned long new_pan_offset;
915 unsigned long base_addr_y, base_addr_c;
916 unsigned long c_offset;
918 if (!var->nonstd)
919 new_pan_offset = (var->yoffset * info->fix.line_length) +
920 (var->xoffset * (info->var.bits_per_pixel / 8));
921 else
922 new_pan_offset = (var->yoffset * info->fix.line_length) +
923 (var->xoffset);
925 if (new_pan_offset == ch->pan_offset)
926 return 0; /* No change, do nothing */
928 ldrcntr = lcdc_read(priv, _LDRCNTR);
930 /* Set the source address for the next refresh */
931 base_addr_y = ch->dma_handle + new_pan_offset;
932 if (var->nonstd) {
933 /* Set y offset */
934 c_offset = (var->yoffset *
935 info->fix.line_length *
936 (info->var.bits_per_pixel - 8)) / 8;
937 base_addr_c = ch->dma_handle + var->xres * var->yres_virtual +
938 c_offset;
939 /* Set x offset */
940 if (info->var.bits_per_pixel == 24)
941 base_addr_c += 2 * var->xoffset;
942 else
943 base_addr_c += var->xoffset;
944 } else
945 base_addr_c = 0;
947 if (!ch->meram_enabled) {
948 lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
949 if (base_addr_c)
950 lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
951 } else {
952 struct sh_mobile_meram_cfg *cfg;
953 struct sh_mobile_meram_info *mdev;
954 unsigned long icb_addr_y, icb_addr_c;
955 int ret;
957 cfg = ch->cfg.meram_cfg;
958 mdev = priv->meram_dev;
959 ret = mdev->ops->meram_update(mdev, cfg,
960 base_addr_y, base_addr_c,
961 &icb_addr_y, &icb_addr_c);
962 if (ret)
963 return ret;
965 lcdc_write_chan_mirror(ch, LDSA1R, icb_addr_y);
966 if (icb_addr_c)
967 lcdc_write_chan_mirror(ch, LDSA2R, icb_addr_c);
971 if (lcdc_chan_is_sublcd(ch))
972 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
973 else
974 lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
976 ch->pan_offset = new_pan_offset;
978 sh_mobile_lcdc_deferred_io_touch(info);
980 return 0;
983 static int sh_mobile_wait_for_vsync(struct fb_info *info)
985 struct sh_mobile_lcdc_chan *ch = info->par;
986 unsigned long ldintr;
987 int ret;
989 /* Enable VSync End interrupt */
990 ldintr = lcdc_read(ch->lcdc, _LDINTR);
991 ldintr |= LDINTR_VEE;
992 lcdc_write(ch->lcdc, _LDINTR, ldintr);
994 ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
995 msecs_to_jiffies(100));
996 if (!ret)
997 return -ETIMEDOUT;
999 return 0;
1002 static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
1003 unsigned long arg)
1005 int retval;
1007 switch (cmd) {
1008 case FBIO_WAITFORVSYNC:
1009 retval = sh_mobile_wait_for_vsync(info);
1010 break;
1012 default:
1013 retval = -ENOIOCTLCMD;
1014 break;
1016 return retval;
1019 static void sh_mobile_fb_reconfig(struct fb_info *info)
1021 struct sh_mobile_lcdc_chan *ch = info->par;
1022 struct fb_videomode mode1, mode2;
1023 struct fb_event event;
1024 int evnt = FB_EVENT_MODE_CHANGE_ALL;
1026 if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1027 /* More framebuffer users are active */
1028 return;
1030 fb_var_to_videomode(&mode1, &ch->display_var);
1031 fb_var_to_videomode(&mode2, &info->var);
1033 if (fb_mode_is_equal(&mode1, &mode2))
1034 return;
1036 /* Display has been re-plugged, framebuffer is free now, reconfigure */
1037 if (fb_set_var(info, &ch->display_var) < 0)
1038 /* Couldn't reconfigure, hopefully, can continue as before */
1039 return;
1041 if (info->var.nonstd)
1042 info->fix.line_length = mode1.xres;
1043 else
1044 info->fix.line_length = mode1.xres * (ch->cfg.bpp / 8);
1047 * fb_set_var() calls the notifier change internally, only if
1048 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
1049 * user event, we have to call the chain ourselves.
1051 event.info = info;
1052 event.data = &mode1;
1053 fb_notifier_call_chain(evnt, &event);
1057 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1058 * user == 1, or with console sem held, if user == 0.
1060 static int sh_mobile_release(struct fb_info *info, int user)
1062 struct sh_mobile_lcdc_chan *ch = info->par;
1064 mutex_lock(&ch->open_lock);
1065 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1067 ch->use_count--;
1069 /* Nothing to reconfigure, when called from fbcon */
1070 if (user) {
1071 console_lock();
1072 sh_mobile_fb_reconfig(info);
1073 console_unlock();
1076 mutex_unlock(&ch->open_lock);
1078 return 0;
1081 static int sh_mobile_open(struct fb_info *info, int user)
1083 struct sh_mobile_lcdc_chan *ch = info->par;
1085 mutex_lock(&ch->open_lock);
1086 ch->use_count++;
1088 dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1089 mutex_unlock(&ch->open_lock);
1091 return 0;
1094 static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
1096 struct sh_mobile_lcdc_chan *ch = info->par;
1097 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1099 if (var->xres > MAX_XRES || var->yres > MAX_YRES ||
1100 var->xres * var->yres * (ch->cfg.bpp / 8) * 2 > info->fix.smem_len) {
1101 dev_warn(info->dev, "Invalid info: %u-%u-%u-%u x %u-%u-%u-%u @ %lukHz!\n",
1102 var->left_margin, var->xres, var->right_margin, var->hsync_len,
1103 var->upper_margin, var->yres, var->lower_margin, var->vsync_len,
1104 PICOS2KHZ(var->pixclock));
1105 return -EINVAL;
1108 /* only accept the forced_bpp for dual channel configurations */
1109 if (p->forced_bpp && p->forced_bpp != var->bits_per_pixel)
1110 return -EINVAL;
1112 switch (var->bits_per_pixel) {
1113 case 16: /* PKF[4:0] = 00011 - RGB 565 */
1114 case 24: /* PKF[4:0] = 01011 - RGB 888 */
1115 case 32: /* PKF[4:0] = 00000 - RGBA 888 */
1116 break;
1117 default:
1118 return -EINVAL;
1121 return 0;
1125 * Screen blanking. Behavior is as follows:
1126 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1127 * FB_BLANK_NORMAL: screen blanked, clocks enabled
1128 * FB_BLANK_VSYNC,
1129 * FB_BLANK_HSYNC,
1130 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1132 static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
1134 struct sh_mobile_lcdc_chan *ch = info->par;
1135 struct sh_mobile_lcdc_priv *p = ch->lcdc;
1137 /* blank the screen? */
1138 if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
1139 struct fb_fillrect rect = {
1140 .width = info->var.xres,
1141 .height = info->var.yres,
1143 sh_mobile_lcdc_fillrect(info, &rect);
1145 /* turn clocks on? */
1146 if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
1147 sh_mobile_lcdc_clk_on(p);
1149 /* turn clocks off? */
1150 if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
1151 /* make sure the screen is updated with the black fill before
1152 * switching the clocks off. one vsync is not enough since
1153 * blanking may occur in the middle of a refresh. deferred io
1154 * mode will reenable the clocks and update the screen in time,
1155 * so it does not need this. */
1156 if (!info->fbdefio) {
1157 sh_mobile_wait_for_vsync(info);
1158 sh_mobile_wait_for_vsync(info);
1160 sh_mobile_lcdc_clk_off(p);
1163 ch->blank_status = blank;
1164 return 0;
1167 static struct fb_ops sh_mobile_lcdc_ops = {
1168 .owner = THIS_MODULE,
1169 .fb_setcolreg = sh_mobile_lcdc_setcolreg,
1170 .fb_read = fb_sys_read,
1171 .fb_write = fb_sys_write,
1172 .fb_fillrect = sh_mobile_lcdc_fillrect,
1173 .fb_copyarea = sh_mobile_lcdc_copyarea,
1174 .fb_imageblit = sh_mobile_lcdc_imageblit,
1175 .fb_blank = sh_mobile_lcdc_blank,
1176 .fb_pan_display = sh_mobile_fb_pan_display,
1177 .fb_ioctl = sh_mobile_ioctl,
1178 .fb_open = sh_mobile_open,
1179 .fb_release = sh_mobile_release,
1180 .fb_check_var = sh_mobile_check_var,
1183 static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
1185 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1186 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1187 int brightness = bdev->props.brightness;
1189 if (bdev->props.power != FB_BLANK_UNBLANK ||
1190 bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
1191 brightness = 0;
1193 return cfg->set_brightness(cfg->board_data, brightness);
1196 static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
1198 struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
1199 struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
1201 return cfg->get_brightness(cfg->board_data);
1204 static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
1205 struct fb_info *info)
1207 return (info->bl_dev == bdev);
1210 static struct backlight_ops sh_mobile_lcdc_bl_ops = {
1211 .options = BL_CORE_SUSPENDRESUME,
1212 .update_status = sh_mobile_lcdc_update_bl,
1213 .get_brightness = sh_mobile_lcdc_get_brightness,
1214 .check_fb = sh_mobile_lcdc_check_fb,
1217 static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
1218 struct sh_mobile_lcdc_chan *ch)
1220 struct backlight_device *bl;
1222 bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
1223 &sh_mobile_lcdc_bl_ops, NULL);
1224 if (IS_ERR(bl)) {
1225 dev_err(parent, "unable to register backlight device: %ld\n",
1226 PTR_ERR(bl));
1227 return NULL;
1230 bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
1231 bl->props.brightness = bl->props.max_brightness;
1232 backlight_update_status(bl);
1234 return bl;
1237 static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
1239 backlight_device_unregister(bdev);
1242 static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp,
1243 int nonstd)
1245 if (nonstd) {
1246 switch (bpp) {
1247 case 12:
1248 case 16:
1249 case 24:
1250 var->bits_per_pixel = bpp;
1251 var->nonstd = nonstd;
1252 return 0;
1253 default:
1254 return -EINVAL;
1258 switch (bpp) {
1259 case 16: /* PKF[4:0] = 00011 - RGB 565 */
1260 var->red.offset = 11;
1261 var->red.length = 5;
1262 var->green.offset = 5;
1263 var->green.length = 6;
1264 var->blue.offset = 0;
1265 var->blue.length = 5;
1266 var->transp.offset = 0;
1267 var->transp.length = 0;
1268 break;
1270 case 24: /* PKF[4:0] = 01011 - RGB 888 */
1271 var->red.offset = 16;
1272 var->red.length = 8;
1273 var->green.offset = 8;
1274 var->green.length = 8;
1275 var->blue.offset = 0;
1276 var->blue.length = 8;
1277 var->transp.offset = 0;
1278 var->transp.length = 0;
1279 break;
1281 case 32: /* PKF[4:0] = 00000 - RGBA 888 */
1282 var->red.offset = 16;
1283 var->red.length = 8;
1284 var->green.offset = 8;
1285 var->green.length = 8;
1286 var->blue.offset = 0;
1287 var->blue.length = 8;
1288 var->transp.offset = 24;
1289 var->transp.length = 8;
1290 break;
1291 default:
1292 return -EINVAL;
1294 var->bits_per_pixel = bpp;
1295 var->red.msb_right = 0;
1296 var->green.msb_right = 0;
1297 var->blue.msb_right = 0;
1298 var->transp.msb_right = 0;
1299 return 0;
1302 static int sh_mobile_lcdc_suspend(struct device *dev)
1304 struct platform_device *pdev = to_platform_device(dev);
1306 sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
1307 return 0;
1310 static int sh_mobile_lcdc_resume(struct device *dev)
1312 struct platform_device *pdev = to_platform_device(dev);
1314 return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
1317 static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
1319 struct platform_device *pdev = to_platform_device(dev);
1320 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
1321 struct sh_mobile_lcdc_chan *ch;
1322 int k, n;
1324 /* save per-channel registers */
1325 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
1326 ch = &p->ch[k];
1327 if (!ch->enabled)
1328 continue;
1329 for (n = 0; n < NR_CH_REGS; n++)
1330 ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
1333 /* save shared registers */
1334 for (n = 0; n < NR_SHARED_REGS; n++)
1335 p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);
1337 /* turn off LCDC hardware */
1338 lcdc_write(p, _LDCNT1R, 0);
1339 return 0;
1342 static int sh_mobile_lcdc_runtime_resume(struct device *dev)
1344 struct platform_device *pdev = to_platform_device(dev);
1345 struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
1346 struct sh_mobile_lcdc_chan *ch;
1347 int k, n;
1349 /* restore per-channel registers */
1350 for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
1351 ch = &p->ch[k];
1352 if (!ch->enabled)
1353 continue;
1354 for (n = 0; n < NR_CH_REGS; n++)
1355 lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
1358 /* restore shared registers */
1359 for (n = 0; n < NR_SHARED_REGS; n++)
1360 lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);
1362 return 0;
1365 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
1366 .suspend = sh_mobile_lcdc_suspend,
1367 .resume = sh_mobile_lcdc_resume,
1368 .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
1369 .runtime_resume = sh_mobile_lcdc_runtime_resume,
1372 /* locking: called with info->lock held */
1373 static int sh_mobile_lcdc_notify(struct notifier_block *nb,
1374 unsigned long action, void *data)
1376 struct fb_event *event = data;
1377 struct fb_info *info = event->info;
1378 struct sh_mobile_lcdc_chan *ch = info->par;
1379 struct sh_mobile_lcdc_board_cfg *board_cfg = &ch->cfg.board_cfg;
1381 if (&ch->lcdc->notifier != nb)
1382 return NOTIFY_DONE;
1384 dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
1385 __func__, action, event->data);
1387 switch(action) {
1388 case FB_EVENT_SUSPEND:
1389 if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
1390 board_cfg->display_off(board_cfg->board_data);
1391 module_put(board_cfg->owner);
1393 sh_mobile_lcdc_stop(ch->lcdc);
1394 break;
1395 case FB_EVENT_RESUME:
1396 mutex_lock(&ch->open_lock);
1397 sh_mobile_fb_reconfig(info);
1398 mutex_unlock(&ch->open_lock);
1400 /* HDMI must be enabled before LCDC configuration */
1401 if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
1402 board_cfg->display_on(board_cfg->board_data, info);
1403 module_put(board_cfg->owner);
1406 sh_mobile_lcdc_start(ch->lcdc);
1409 return NOTIFY_OK;
1412 static int sh_mobile_lcdc_remove(struct platform_device *pdev);
1414 static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
1416 struct fb_info *info;
1417 struct sh_mobile_lcdc_priv *priv;
1418 struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
1419 struct resource *res;
1420 int error;
1421 void *buf;
1422 int i, j;
1424 if (!pdata) {
1425 dev_err(&pdev->dev, "no platform data defined\n");
1426 return -EINVAL;
1429 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1430 i = platform_get_irq(pdev, 0);
1431 if (!res || i < 0) {
1432 dev_err(&pdev->dev, "cannot get platform resources\n");
1433 return -ENOENT;
1436 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1437 if (!priv) {
1438 dev_err(&pdev->dev, "cannot allocate device data\n");
1439 return -ENOMEM;
1442 platform_set_drvdata(pdev, priv);
1444 error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
1445 dev_name(&pdev->dev), priv);
1446 if (error) {
1447 dev_err(&pdev->dev, "unable to request irq\n");
1448 goto err1;
1451 priv->irq = i;
1452 atomic_set(&priv->hw_usecnt, -1);
1454 j = 0;
1455 for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1456 struct sh_mobile_lcdc_chan *ch = priv->ch + j;
1458 ch->lcdc = priv;
1459 memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
1461 error = sh_mobile_lcdc_check_interface(ch);
1462 if (error) {
1463 dev_err(&pdev->dev, "unsupported interface type\n");
1464 goto err1;
1466 init_waitqueue_head(&ch->frame_end_wait);
1467 init_completion(&ch->vsync_completion);
1468 ch->pan_offset = 0;
1470 /* probe the backlight is there is one defined */
1471 if (ch->cfg.bl_info.max_brightness)
1472 ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
1474 switch (pdata->ch[i].chan) {
1475 case LCDC_CHAN_MAINLCD:
1476 ch->enabled = 1 << 1;
1477 ch->reg_offs = lcdc_offs_mainlcd;
1478 j++;
1479 break;
1480 case LCDC_CHAN_SUBLCD:
1481 ch->enabled = 1 << 2;
1482 ch->reg_offs = lcdc_offs_sublcd;
1483 j++;
1484 break;
1488 if (!j) {
1489 dev_err(&pdev->dev, "no channels defined\n");
1490 error = -EINVAL;
1491 goto err1;
1494 /* for dual channel LCDC (MAIN + SUB) force shared bpp setting */
1495 if (j == 2)
1496 priv->forced_bpp = pdata->ch[0].bpp;
1498 priv->base = ioremap_nocache(res->start, resource_size(res));
1499 if (!priv->base)
1500 goto err1;
1502 error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
1503 if (error) {
1504 dev_err(&pdev->dev, "unable to setup clocks\n");
1505 goto err1;
1508 priv->meram_dev = pdata->meram_dev;
1510 for (i = 0; i < j; i++) {
1511 struct fb_var_screeninfo *var;
1512 const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
1513 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1514 struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
1515 const struct fb_videomode *mode = cfg->lcd_cfg;
1516 unsigned long max_size = 0;
1517 int k;
1518 int num_cfg;
1520 ch->info = framebuffer_alloc(0, &pdev->dev);
1521 if (!ch->info) {
1522 dev_err(&pdev->dev, "unable to allocate fb_info\n");
1523 error = -ENOMEM;
1524 break;
1527 info = ch->info;
1528 var = &info->var;
1529 info->fbops = &sh_mobile_lcdc_ops;
1530 info->par = ch;
1532 mutex_init(&ch->open_lock);
1534 for (k = 0, lcd_cfg = mode;
1535 k < cfg->num_cfg && lcd_cfg;
1536 k++, lcd_cfg++) {
1537 unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
1538 /* NV12 buffers must have even number of lines */
1539 if ((cfg->nonstd) && cfg->bpp == 12 &&
1540 (lcd_cfg->yres & 0x1)) {
1541 dev_err(&pdev->dev, "yres must be multiple of 2"
1542 " for YCbCr420 mode.\n");
1543 error = -EINVAL;
1544 goto err1;
1547 if (size > max_size) {
1548 max_cfg = lcd_cfg;
1549 max_size = size;
1553 if (!mode)
1554 max_size = MAX_XRES * MAX_YRES;
1555 else if (max_cfg)
1556 dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
1557 max_cfg->xres, max_cfg->yres);
1559 info->fix = sh_mobile_lcdc_fix;
1560 info->fix.smem_len = max_size * 2 * cfg->bpp / 8;
1562 /* Only pan in 2 line steps for NV12 */
1563 if (cfg->nonstd && cfg->bpp == 12)
1564 info->fix.ypanstep = 2;
1566 if (!mode) {
1567 mode = &default_720p;
1568 num_cfg = 1;
1569 } else {
1570 num_cfg = cfg->num_cfg;
1573 fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
1575 fb_videomode_to_var(var, mode);
1576 var->width = cfg->lcd_size_cfg.width;
1577 var->height = cfg->lcd_size_cfg.height;
1578 /* Default Y virtual resolution is 2x panel size */
1579 var->yres_virtual = var->yres * 2;
1580 var->activate = FB_ACTIVATE_NOW;
1582 error = sh_mobile_lcdc_set_bpp(var, cfg->bpp, cfg->nonstd);
1583 if (error)
1584 break;
1586 buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
1587 &ch->dma_handle, GFP_KERNEL);
1588 if (!buf) {
1589 dev_err(&pdev->dev, "unable to allocate buffer\n");
1590 error = -ENOMEM;
1591 break;
1594 info->pseudo_palette = &ch->pseudo_palette;
1595 info->flags = FBINFO_FLAG_DEFAULT;
1597 error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
1598 if (error < 0) {
1599 dev_err(&pdev->dev, "unable to allocate cmap\n");
1600 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1601 buf, ch->dma_handle);
1602 break;
1605 info->fix.smem_start = ch->dma_handle;
1606 if (var->nonstd)
1607 info->fix.line_length = var->xres;
1608 else
1609 info->fix.line_length = var->xres * (cfg->bpp / 8);
1611 info->screen_base = buf;
1612 info->device = &pdev->dev;
1613 ch->display_var = *var;
1616 if (error)
1617 goto err1;
1619 error = sh_mobile_lcdc_start(priv);
1620 if (error) {
1621 dev_err(&pdev->dev, "unable to start hardware\n");
1622 goto err1;
1625 for (i = 0; i < j; i++) {
1626 struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1628 info = ch->info;
1630 if (info->fbdefio) {
1631 ch->sglist = vmalloc(sizeof(struct scatterlist) *
1632 info->fix.smem_len >> PAGE_SHIFT);
1633 if (!ch->sglist) {
1634 dev_err(&pdev->dev, "cannot allocate sglist\n");
1635 goto err1;
1639 info->bl_dev = ch->bl;
1641 error = register_framebuffer(info);
1642 if (error < 0)
1643 goto err1;
1645 dev_info(info->dev,
1646 "registered %s/%s as %dx%d %dbpp.\n",
1647 pdev->name,
1648 (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
1649 "mainlcd" : "sublcd",
1650 info->var.xres, info->var.yres,
1651 ch->cfg.bpp);
1653 /* deferred io mode: disable clock to save power */
1654 if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
1655 sh_mobile_lcdc_clk_off(priv);
1658 /* Failure ignored */
1659 priv->notifier.notifier_call = sh_mobile_lcdc_notify;
1660 fb_register_client(&priv->notifier);
1662 return 0;
1663 err1:
1664 sh_mobile_lcdc_remove(pdev);
1666 return error;
1669 static int sh_mobile_lcdc_remove(struct platform_device *pdev)
1671 struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
1672 struct fb_info *info;
1673 int i;
1675 fb_unregister_client(&priv->notifier);
1677 for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
1678 if (priv->ch[i].info && priv->ch[i].info->dev)
1679 unregister_framebuffer(priv->ch[i].info);
1681 sh_mobile_lcdc_stop(priv);
1683 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1684 info = priv->ch[i].info;
1686 if (!info || !info->device)
1687 continue;
1689 if (priv->ch[i].sglist)
1690 vfree(priv->ch[i].sglist);
1692 if (info->screen_base)
1693 dma_free_coherent(&pdev->dev, info->fix.smem_len,
1694 info->screen_base,
1695 priv->ch[i].dma_handle);
1696 fb_dealloc_cmap(&info->cmap);
1697 framebuffer_release(info);
1700 for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1701 if (priv->ch[i].bl)
1702 sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
1705 if (priv->dot_clk)
1706 clk_put(priv->dot_clk);
1708 if (priv->dev)
1709 pm_runtime_disable(priv->dev);
1711 if (priv->base)
1712 iounmap(priv->base);
1714 if (priv->irq)
1715 free_irq(priv->irq, priv);
1716 kfree(priv);
1717 return 0;
1720 static struct platform_driver sh_mobile_lcdc_driver = {
1721 .driver = {
1722 .name = "sh_mobile_lcdc_fb",
1723 .owner = THIS_MODULE,
1724 .pm = &sh_mobile_lcdc_dev_pm_ops,
1726 .probe = sh_mobile_lcdc_probe,
1727 .remove = sh_mobile_lcdc_remove,
1730 static int __init sh_mobile_lcdc_init(void)
1732 return platform_driver_register(&sh_mobile_lcdc_driver);
1735 static void __exit sh_mobile_lcdc_exit(void)
1737 platform_driver_unregister(&sh_mobile_lcdc_driver);
1740 module_init(sh_mobile_lcdc_init);
1741 module_exit(sh_mobile_lcdc_exit);
1743 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
1744 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
1745 MODULE_LICENSE("GPL v2");