[POWERPC] Use menuconfig objects II - Macintosh
[wrt350n-kernel.git] / drivers / video / sm501fb.c
blob0a44c44672c82855c0c3152f1c631ce0d2925fdc
1 /* linux/drivers/video/sm501fb.c
3 * Copyright (c) 2006 Simtec Electronics
4 * Vincent Sanders <vince@simtec.co.uk>
5 * Ben Dooks <ben@simtec.co.uk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Framebuffer driver for the Silicon Motion SM501
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/mm.h>
19 #include <linux/tty.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/fb.h>
23 #include <linux/init.h>
24 #include <linux/vmalloc.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/wait.h>
29 #include <linux/platform_device.h>
30 #include <linux/clk.h>
32 #include <asm/io.h>
33 #include <asm/uaccess.h>
34 #include <asm/div64.h>
36 #ifdef CONFIG_PM
37 #include <linux/pm.h>
38 #endif
40 #include <linux/sm501.h>
41 #include <linux/sm501-regs.h>
43 #define NR_PALETTE 256
45 enum sm501_controller {
46 HEAD_CRT = 0,
47 HEAD_PANEL = 1,
50 /* SM501 memory adress */
51 struct sm501_mem {
52 unsigned long size;
53 unsigned long sm_addr;
54 void __iomem *k_addr;
57 /* private data that is shared between all frambuffers* */
58 struct sm501fb_info {
59 struct device *dev;
60 struct fb_info *fb[2]; /* fb info for both heads */
61 struct resource *fbmem_res; /* framebuffer resource */
62 struct resource *regs_res; /* registers resource */
63 struct sm501_platdata_fb *pdata; /* our platform data */
65 int irq;
66 int swap_endian; /* set to swap rgb=>bgr */
67 void __iomem *regs; /* remapped registers */
68 void __iomem *fbmem; /* remapped framebuffer */
69 size_t fbmem_len; /* length of remapped region */
72 /* per-framebuffer private data */
73 struct sm501fb_par {
74 u32 pseudo_palette[16];
76 enum sm501_controller head;
77 struct sm501_mem cursor;
78 struct sm501_mem screen;
79 struct fb_ops ops;
81 void *store_fb;
82 void *store_cursor;
83 void __iomem *cursor_regs;
84 struct sm501fb_info *info;
87 /* Helper functions */
89 static inline int h_total(struct fb_var_screeninfo *var)
91 return var->xres + var->left_margin +
92 var->right_margin + var->hsync_len;
95 static inline int v_total(struct fb_var_screeninfo *var)
97 return var->yres + var->upper_margin +
98 var->lower_margin + var->vsync_len;
101 /* sm501fb_sync_regs()
103 * This call is mainly for PCI bus systems where we need to
104 * ensure that any writes to the bus are completed before the
105 * next phase, or after completing a function.
108 static inline void sm501fb_sync_regs(struct sm501fb_info *info)
110 readl(info->regs);
113 /* sm501_alloc_mem
115 * This is an attempt to lay out memory for the two framebuffers and
116 * everything else
118 * |fbmem_res->start fbmem_res->end|
119 * | |
120 * |fb[0].fix.smem_start | |fb[1].fix.smem_start | 2K |
121 * |-> fb[0].fix.smem_len <-| spare |-> fb[1].fix.smem_len <-|-> cursors <-|
123 * The "spare" space is for the 2d engine data
124 * the fixed is space for the cursors (2x1Kbyte)
126 * we need to allocate memory for the 2D acceleration engine
127 * command list and the data for the engine to deal with.
129 * - all allocations must be 128bit aligned
130 * - cursors are 64x64x2 bits (1Kbyte)
134 #define SM501_MEMF_CURSOR (1)
135 #define SM501_MEMF_PANEL (2)
136 #define SM501_MEMF_CRT (4)
137 #define SM501_MEMF_ACCEL (8)
139 static int sm501_alloc_mem(struct sm501fb_info *inf, struct sm501_mem *mem,
140 unsigned int why, size_t size)
142 unsigned int ptr = 0;
144 switch (why) {
145 case SM501_MEMF_CURSOR:
146 ptr = inf->fbmem_len - size;
147 inf->fbmem_len = ptr;
148 break;
150 case SM501_MEMF_PANEL:
151 ptr = inf->fbmem_len - size;
152 if (ptr < inf->fb[0]->fix.smem_len)
153 return -ENOMEM;
155 break;
157 case SM501_MEMF_CRT:
158 ptr = 0;
159 break;
161 case SM501_MEMF_ACCEL:
162 ptr = inf->fb[0]->fix.smem_len;
164 if ((ptr + size) >
165 (inf->fb[1]->fix.smem_start - inf->fbmem_res->start))
166 return -ENOMEM;
167 break;
169 default:
170 return -EINVAL;
173 mem->size = size;
174 mem->sm_addr = ptr;
175 mem->k_addr = inf->fbmem + ptr;
177 dev_dbg(inf->dev, "%s: result %08lx, %p - %u, %zd\n",
178 __func__, mem->sm_addr, mem->k_addr, why, size);
180 return 0;
183 /* sm501fb_ps_to_hz
185 * Converts a period in picoseconds to Hz.
187 * Note, we try to keep this in Hz to minimise rounding with
188 * the limited PLL settings on the SM501.
191 static unsigned long sm501fb_ps_to_hz(unsigned long psvalue)
193 unsigned long long numerator=1000000000000ULL;
195 /* 10^12 / picosecond period gives frequency in Hz */
196 do_div(numerator, psvalue);
197 return (unsigned long)numerator;
200 /* sm501fb_hz_to_ps is identical to the oposite transform */
202 #define sm501fb_hz_to_ps(x) sm501fb_ps_to_hz(x)
204 /* sm501fb_setup_gamma
206 * Programs a linear 1.0 gamma ramp in case the gamma
207 * correction is enabled without programming anything else.
210 static void sm501fb_setup_gamma(struct sm501fb_info *fbi,
211 unsigned long palette)
213 unsigned long value = 0;
214 int offset;
216 /* set gamma values */
217 for (offset = 0; offset < 256 * 4; offset += 4) {
218 writel(value, fbi->regs + palette + offset);
219 value += 0x010101; /* Advance RGB by 1,1,1.*/
223 /* sm501fb_check_var
225 * check common variables for both panel and crt
228 static int sm501fb_check_var(struct fb_var_screeninfo *var,
229 struct fb_info *info)
231 struct sm501fb_par *par = info->par;
232 struct sm501fb_info *sm = par->info;
233 unsigned long tmp;
235 /* check we can fit these values into the registers */
237 if (var->hsync_len > 255 || var->vsync_len > 255)
238 return -EINVAL;
240 if ((var->xres + var->right_margin) >= 4096)
241 return -EINVAL;
243 if ((var->yres + var->lower_margin) > 2048)
244 return -EINVAL;
246 /* hard limits of device */
248 if (h_total(var) > 4096 || v_total(var) > 2048)
249 return -EINVAL;
251 /* check our line length is going to be 128 bit aligned */
253 tmp = (var->xres * var->bits_per_pixel) / 8;
254 if ((tmp & 15) != 0)
255 return -EINVAL;
257 /* check the virtual size */
259 if (var->xres_virtual > 4096 || var->yres_virtual > 2048)
260 return -EINVAL;
262 /* can cope with 8,16 or 32bpp */
264 if (var->bits_per_pixel <= 8)
265 var->bits_per_pixel = 8;
266 else if (var->bits_per_pixel <= 16)
267 var->bits_per_pixel = 16;
268 else if (var->bits_per_pixel == 24)
269 var->bits_per_pixel = 32;
271 /* set r/g/b positions and validate bpp */
272 switch(var->bits_per_pixel) {
273 case 8:
274 var->red.length = var->bits_per_pixel;
275 var->red.offset = 0;
276 var->green.length = var->bits_per_pixel;
277 var->green.offset = 0;
278 var->blue.length = var->bits_per_pixel;
279 var->blue.offset = 0;
280 var->transp.length = 0;
282 break;
284 case 16:
285 if (sm->pdata->flags & SM501_FBPD_SWAP_FB_ENDIAN) {
286 var->red.offset = 11;
287 var->green.offset = 5;
288 var->blue.offset = 0;
289 } else {
290 var->blue.offset = 11;
291 var->green.offset = 5;
292 var->red.offset = 0;
295 var->red.length = 5;
296 var->green.length = 6;
297 var->blue.length = 5;
298 var->transp.length = 0;
299 break;
301 case 32:
302 if (sm->pdata->flags & SM501_FBPD_SWAP_FB_ENDIAN) {
303 var->transp.offset = 0;
304 var->red.offset = 8;
305 var->green.offset = 16;
306 var->blue.offset = 24;
307 } else {
308 var->transp.offset = 24;
309 var->red.offset = 16;
310 var->green.offset = 8;
311 var->blue.offset = 0;
314 var->red.length = 8;
315 var->green.length = 8;
316 var->blue.length = 8;
317 var->transp.length = 0;
318 break;
320 default:
321 return -EINVAL;
324 return 0;
328 * sm501fb_check_var_crt():
330 * check the parameters for the CRT head, and either bring them
331 * back into range, or return -EINVAL.
334 static int sm501fb_check_var_crt(struct fb_var_screeninfo *var,
335 struct fb_info *info)
337 return sm501fb_check_var(var, info);
340 /* sm501fb_check_var_pnl():
342 * check the parameters for the CRT head, and either bring them
343 * back into range, or return -EINVAL.
346 static int sm501fb_check_var_pnl(struct fb_var_screeninfo *var,
347 struct fb_info *info)
349 return sm501fb_check_var(var, info);
352 /* sm501fb_set_par_common
354 * set common registers for framebuffers
357 static int sm501fb_set_par_common(struct fb_info *info,
358 struct fb_var_screeninfo *var)
360 struct sm501fb_par *par = info->par;
361 struct sm501fb_info *fbi = par->info;
362 unsigned long pixclock; /* pixelclock in Hz */
363 unsigned long sm501pixclock; /* pixelclock the 501 can achive in Hz */
364 unsigned int mem_type;
365 unsigned int clock_type;
366 unsigned int head_addr;
368 dev_dbg(fbi->dev, "%s: %dx%d, bpp = %d, virtual %dx%d\n",
369 __func__, var->xres, var->yres, var->bits_per_pixel,
370 var->xres_virtual, var->yres_virtual);
372 switch (par->head) {
373 case HEAD_CRT:
374 mem_type = SM501_MEMF_CRT;
375 clock_type = SM501_CLOCK_V2XCLK;
376 head_addr = SM501_DC_CRT_FB_ADDR;
377 break;
379 case HEAD_PANEL:
380 mem_type = SM501_MEMF_PANEL;
381 clock_type = SM501_CLOCK_P2XCLK;
382 head_addr = SM501_DC_PANEL_FB_ADDR;
383 break;
385 default:
386 mem_type = 0; /* stop compiler warnings */
387 head_addr = 0;
388 clock_type = 0;
391 switch (var->bits_per_pixel) {
392 case 8:
393 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
394 break;
396 case 16:
397 info->fix.visual = FB_VISUAL_DIRECTCOLOR;
398 break;
400 case 32:
401 info->fix.visual = FB_VISUAL_TRUECOLOR;
402 break;
405 /* allocate fb memory within 501 */
406 info->fix.line_length = (var->xres_virtual * var->bits_per_pixel)/8;
407 info->fix.smem_len = info->fix.line_length * var->yres_virtual;
409 dev_dbg(fbi->dev, "%s: line length = %u\n", __func__,
410 info->fix.line_length);
412 if (sm501_alloc_mem(fbi, &par->screen, mem_type,
413 info->fix.smem_len)) {
414 dev_err(fbi->dev, "no memory available\n");
415 return -ENOMEM;
418 info->fix.smem_start = fbi->fbmem_res->start + par->screen.sm_addr;
420 info->screen_base = fbi->fbmem + par->screen.sm_addr;
421 info->screen_size = info->fix.smem_len;
423 /* set start of framebuffer to the screen */
425 writel(par->screen.sm_addr | SM501_ADDR_FLIP, fbi->regs + head_addr);
427 /* program CRT clock */
429 pixclock = sm501fb_ps_to_hz(var->pixclock);
431 sm501pixclock = sm501_set_clock(fbi->dev->parent, clock_type,
432 pixclock);
434 /* update fb layer with actual clock used */
435 var->pixclock = sm501fb_hz_to_ps(sm501pixclock);
437 dev_dbg(fbi->dev, "%s: pixclock(ps) = %u, pixclock(Hz) = %lu, "
438 "sm501pixclock = %lu, error = %ld%%\n",
439 __func__, var->pixclock, pixclock, sm501pixclock,
440 ((pixclock - sm501pixclock)*100)/pixclock);
442 return 0;
445 /* sm501fb_set_par_geometry
447 * set the geometry registers for specified framebuffer.
450 static void sm501fb_set_par_geometry(struct fb_info *info,
451 struct fb_var_screeninfo *var)
453 struct sm501fb_par *par = info->par;
454 struct sm501fb_info *fbi = par->info;
455 void __iomem *base = fbi->regs;
456 unsigned long reg;
458 if (par->head == HEAD_CRT)
459 base += SM501_DC_CRT_H_TOT;
460 else
461 base += SM501_DC_PANEL_H_TOT;
463 /* set framebuffer width and display width */
465 reg = info->fix.line_length;
466 reg |= ((var->xres * var->bits_per_pixel)/8) << 16;
468 writel(reg, fbi->regs + (par->head == HEAD_CRT ?
469 SM501_DC_CRT_FB_OFFSET : SM501_DC_PANEL_FB_OFFSET));
471 /* program horizontal total */
473 reg = (h_total(var) - 1) << 16;
474 reg |= (var->xres - 1);
476 writel(reg, base + SM501_OFF_DC_H_TOT);
478 /* program horizontal sync */
480 reg = var->hsync_len << 16;
481 reg |= var->xres + var->right_margin - 1;
483 writel(reg, base + SM501_OFF_DC_H_SYNC);
485 /* program vertical total */
487 reg = (v_total(var) - 1) << 16;
488 reg |= (var->yres - 1);
490 writel(reg, base + SM501_OFF_DC_V_TOT);
492 /* program vertical sync */
493 reg = var->vsync_len << 16;
494 reg |= var->yres + var->lower_margin - 1;
496 writel(reg, base + SM501_OFF_DC_V_SYNC);
499 /* sm501fb_pan_crt
501 * pan the CRT display output within an virtual framebuffer
504 static int sm501fb_pan_crt(struct fb_var_screeninfo *var,
505 struct fb_info *info)
507 struct sm501fb_par *par = info->par;
508 struct sm501fb_info *fbi = par->info;
509 unsigned int bytes_pixel = var->bits_per_pixel / 8;
510 unsigned long reg;
511 unsigned long xoffs;
513 xoffs = var->xoffset * bytes_pixel;
515 reg = readl(fbi->regs + SM501_DC_CRT_CONTROL);
517 reg &= ~SM501_DC_CRT_CONTROL_PIXEL_MASK;
518 reg |= ((xoffs & 15) / bytes_pixel) << 4;
519 writel(reg, fbi->regs + SM501_DC_CRT_CONTROL);
521 reg = (par->screen.sm_addr + xoffs +
522 var->yoffset * info->fix.line_length);
523 writel(reg | SM501_ADDR_FLIP, fbi->regs + SM501_DC_CRT_FB_ADDR);
525 sm501fb_sync_regs(fbi);
526 return 0;
529 /* sm501fb_pan_pnl
531 * pan the panel display output within an virtual framebuffer
534 static int sm501fb_pan_pnl(struct fb_var_screeninfo *var,
535 struct fb_info *info)
537 struct sm501fb_par *par = info->par;
538 struct sm501fb_info *fbi = par->info;
539 unsigned long reg;
541 reg = var->xoffset | (var->xres_virtual << 16);
542 writel(reg, fbi->regs + SM501_DC_PANEL_FB_WIDTH);
544 reg = var->yoffset | (var->yres_virtual << 16);
545 writel(reg, fbi->regs + SM501_DC_PANEL_FB_HEIGHT);
547 sm501fb_sync_regs(fbi);
548 return 0;
551 /* sm501fb_set_par_crt
553 * Set the CRT video mode from the fb_info structure
556 static int sm501fb_set_par_crt(struct fb_info *info)
558 struct sm501fb_par *par = info->par;
559 struct sm501fb_info *fbi = par->info;
560 struct fb_var_screeninfo *var = &info->var;
561 unsigned long control; /* control register */
562 int ret;
564 /* activate new configuration */
566 dev_dbg(fbi->dev, "%s(%p)\n", __func__, info);
568 /* enable CRT DAC - note 0 is on!*/
569 sm501_misc_control(fbi->dev->parent, 0, SM501_MISC_DAC_POWER);
571 control = readl(fbi->regs + SM501_DC_CRT_CONTROL);
573 control &= (SM501_DC_CRT_CONTROL_PIXEL_MASK |
574 SM501_DC_CRT_CONTROL_GAMMA |
575 SM501_DC_CRT_CONTROL_BLANK |
576 SM501_DC_CRT_CONTROL_SEL |
577 SM501_DC_CRT_CONTROL_CP |
578 SM501_DC_CRT_CONTROL_TVP);
580 /* set the sync polarities before we check data source */
582 if ((var->sync & FB_SYNC_HOR_HIGH_ACT) == 0)
583 control |= SM501_DC_CRT_CONTROL_HSP;
585 if ((var->sync & FB_SYNC_VERT_HIGH_ACT) == 0)
586 control |= SM501_DC_CRT_CONTROL_VSP;
588 if ((control & SM501_DC_CRT_CONTROL_SEL) == 0) {
589 /* the head is displaying panel data... */
591 sm501_alloc_mem(fbi, &par->screen, SM501_MEMF_CRT, 0);
592 goto out_update;
595 ret = sm501fb_set_par_common(info, var);
596 if (ret) {
597 dev_err(fbi->dev, "failed to set common parameters\n");
598 return ret;
601 sm501fb_pan_crt(var, info);
602 sm501fb_set_par_geometry(info, var);
604 control |= SM501_FIFO_3; /* fill if >3 free slots */
606 switch(var->bits_per_pixel) {
607 case 8:
608 control |= SM501_DC_CRT_CONTROL_8BPP;
609 break;
611 case 16:
612 control |= SM501_DC_CRT_CONTROL_16BPP;
613 break;
615 case 32:
616 control |= SM501_DC_CRT_CONTROL_32BPP;
617 sm501fb_setup_gamma(fbi, SM501_DC_CRT_PALETTE);
618 break;
620 default:
621 BUG();
624 control |= SM501_DC_CRT_CONTROL_SEL; /* CRT displays CRT data */
625 control |= SM501_DC_CRT_CONTROL_TE; /* enable CRT timing */
626 control |= SM501_DC_CRT_CONTROL_ENABLE; /* enable CRT plane */
628 out_update:
629 dev_dbg(fbi->dev, "new control is %08lx\n", control);
631 writel(control, fbi->regs + SM501_DC_CRT_CONTROL);
632 sm501fb_sync_regs(fbi);
634 return 0;
637 static void sm501fb_panel_power(struct sm501fb_info *fbi, int to)
639 unsigned long control;
640 void __iomem *ctrl_reg = fbi->regs + SM501_DC_PANEL_CONTROL;
642 control = readl(ctrl_reg);
644 if (to && (control & SM501_DC_PANEL_CONTROL_VDD) == 0) {
645 /* enable panel power */
647 control |= SM501_DC_PANEL_CONTROL_VDD; /* FPVDDEN */
648 writel(control, ctrl_reg);
649 sm501fb_sync_regs(fbi);
650 mdelay(10);
652 control |= SM501_DC_PANEL_CONTROL_DATA; /* DATA */
653 writel(control, ctrl_reg);
654 sm501fb_sync_regs(fbi);
655 mdelay(10);
657 control |= SM501_DC_PANEL_CONTROL_BIAS; /* VBIASEN */
658 writel(control, ctrl_reg);
659 sm501fb_sync_regs(fbi);
660 mdelay(10);
662 control |= SM501_DC_PANEL_CONTROL_FPEN;
663 writel(control, ctrl_reg);
665 } else if (!to && (control & SM501_DC_PANEL_CONTROL_VDD) != 0) {
666 /* disable panel power */
668 control &= ~SM501_DC_PANEL_CONTROL_FPEN;
669 writel(control, ctrl_reg);
670 sm501fb_sync_regs(fbi);
671 mdelay(10);
673 control &= ~SM501_DC_PANEL_CONTROL_BIAS;
674 writel(control, ctrl_reg);
675 sm501fb_sync_regs(fbi);
676 mdelay(10);
678 control &= ~SM501_DC_PANEL_CONTROL_DATA;
679 writel(control, ctrl_reg);
680 sm501fb_sync_regs(fbi);
681 mdelay(10);
683 control &= ~SM501_DC_PANEL_CONTROL_VDD;
684 writel(control, ctrl_reg);
685 sm501fb_sync_regs(fbi);
686 mdelay(10);
689 sm501fb_sync_regs(fbi);
692 /* sm501fb_set_par_pnl
694 * Set the panel video mode from the fb_info structure
697 static int sm501fb_set_par_pnl(struct fb_info *info)
699 struct sm501fb_par *par = info->par;
700 struct sm501fb_info *fbi = par->info;
701 struct fb_var_screeninfo *var = &info->var;
702 unsigned long control;
703 unsigned long reg;
704 int ret;
706 dev_dbg(fbi->dev, "%s(%p)\n", __func__, info);
708 /* activate this new configuration */
710 ret = sm501fb_set_par_common(info, var);
711 if (ret)
712 return ret;
714 sm501fb_pan_pnl(var, info);
715 sm501fb_set_par_geometry(info, var);
717 /* update control register */
719 control = readl(fbi->regs + SM501_DC_PANEL_CONTROL);
720 control &= (SM501_DC_PANEL_CONTROL_GAMMA |
721 SM501_DC_PANEL_CONTROL_VDD |
722 SM501_DC_PANEL_CONTROL_DATA |
723 SM501_DC_PANEL_CONTROL_BIAS |
724 SM501_DC_PANEL_CONTROL_FPEN |
725 SM501_DC_PANEL_CONTROL_CP |
726 SM501_DC_PANEL_CONTROL_CK |
727 SM501_DC_PANEL_CONTROL_HP |
728 SM501_DC_PANEL_CONTROL_VP |
729 SM501_DC_PANEL_CONTROL_HPD |
730 SM501_DC_PANEL_CONTROL_VPD);
732 control |= SM501_FIFO_3; /* fill if >3 free slots */
734 switch(var->bits_per_pixel) {
735 case 8:
736 control |= SM501_DC_PANEL_CONTROL_8BPP;
737 break;
739 case 16:
740 control |= SM501_DC_PANEL_CONTROL_16BPP;
741 break;
743 case 32:
744 control |= SM501_DC_PANEL_CONTROL_32BPP;
745 sm501fb_setup_gamma(fbi, SM501_DC_PANEL_PALETTE);
746 break;
748 default:
749 BUG();
752 writel(0x0, fbi->regs + SM501_DC_PANEL_PANNING_CONTROL);
754 /* panel plane top left and bottom right location */
756 writel(0x00, fbi->regs + SM501_DC_PANEL_TL_LOC);
758 reg = var->xres - 1;
759 reg |= (var->yres - 1) << 16;
761 writel(reg, fbi->regs + SM501_DC_PANEL_BR_LOC);
763 /* program panel control register */
765 control |= SM501_DC_PANEL_CONTROL_TE; /* enable PANEL timing */
766 control |= SM501_DC_PANEL_CONTROL_EN; /* enable PANEL gfx plane */
768 if ((var->sync & FB_SYNC_HOR_HIGH_ACT) == 0)
769 control |= SM501_DC_PANEL_CONTROL_HSP;
771 if ((var->sync & FB_SYNC_VERT_HIGH_ACT) == 0)
772 control |= SM501_DC_PANEL_CONTROL_VSP;
774 writel(control, fbi->regs + SM501_DC_PANEL_CONTROL);
775 sm501fb_sync_regs(fbi);
777 /* power the panel up */
778 sm501fb_panel_power(fbi, 1);
779 return 0;
783 /* chan_to_field
785 * convert a colour value into a field position
787 * from pxafb.c
790 static inline unsigned int chan_to_field(unsigned int chan,
791 struct fb_bitfield *bf)
793 chan &= 0xffff;
794 chan >>= 16 - bf->length;
795 return chan << bf->offset;
798 /* sm501fb_setcolreg
800 * set the colour mapping for modes that support palettised data
803 static int sm501fb_setcolreg(unsigned regno,
804 unsigned red, unsigned green, unsigned blue,
805 unsigned transp, struct fb_info *info)
807 struct sm501fb_par *par = info->par;
808 struct sm501fb_info *fbi = par->info;
809 void __iomem *base = fbi->regs;
810 unsigned int val;
812 if (par->head == HEAD_CRT)
813 base += SM501_DC_CRT_PALETTE;
814 else
815 base += SM501_DC_PANEL_PALETTE;
817 switch (info->fix.visual) {
818 case FB_VISUAL_TRUECOLOR:
819 /* true-colour, use pseuo-palette */
821 if (regno < 16) {
822 u32 *pal = par->pseudo_palette;
824 val = chan_to_field(red, &info->var.red);
825 val |= chan_to_field(green, &info->var.green);
826 val |= chan_to_field(blue, &info->var.blue);
828 pal[regno] = val;
830 break;
832 case FB_VISUAL_PSEUDOCOLOR:
833 if (regno < 256) {
834 val = (red >> 8) << 16;
835 val |= (green >> 8) << 8;
836 val |= blue >> 8;
838 writel(val, base + (regno * 4));
841 break;
843 default:
844 return 1; /* unknown type */
847 return 0;
850 /* sm501fb_blank_pnl
852 * Blank or un-blank the panel interface
855 static int sm501fb_blank_pnl(int blank_mode, struct fb_info *info)
857 struct sm501fb_par *par = info->par;
858 struct sm501fb_info *fbi = par->info;
860 dev_dbg(fbi->dev, "%s(mode=%d, %p)\n", __func__, blank_mode, info);
862 switch (blank_mode) {
863 case FB_BLANK_POWERDOWN:
864 sm501fb_panel_power(fbi, 0);
865 break;
867 case FB_BLANK_UNBLANK:
868 sm501fb_panel_power(fbi, 1);
869 break;
871 case FB_BLANK_NORMAL:
872 case FB_BLANK_VSYNC_SUSPEND:
873 case FB_BLANK_HSYNC_SUSPEND:
874 default:
875 return 1;
878 return 0;
881 /* sm501fb_blank_crt
883 * Blank or un-blank the crt interface
886 static int sm501fb_blank_crt(int blank_mode, struct fb_info *info)
888 struct sm501fb_par *par = info->par;
889 struct sm501fb_info *fbi = par->info;
890 unsigned long ctrl;
892 dev_dbg(fbi->dev, "%s(mode=%d, %p)\n", __func__, blank_mode, info);
894 ctrl = readl(fbi->regs + SM501_DC_CRT_CONTROL);
896 switch (blank_mode) {
897 case FB_BLANK_POWERDOWN:
898 ctrl &= ~SM501_DC_CRT_CONTROL_ENABLE;
899 sm501_misc_control(fbi->dev->parent, SM501_MISC_DAC_POWER, 0);
901 case FB_BLANK_NORMAL:
902 ctrl |= SM501_DC_CRT_CONTROL_BLANK;
903 break;
905 case FB_BLANK_UNBLANK:
906 ctrl &= ~SM501_DC_CRT_CONTROL_BLANK;
907 ctrl |= SM501_DC_CRT_CONTROL_ENABLE;
908 sm501_misc_control(fbi->dev->parent, 0, SM501_MISC_DAC_POWER);
909 break;
911 case FB_BLANK_VSYNC_SUSPEND:
912 case FB_BLANK_HSYNC_SUSPEND:
913 default:
914 return 1;
918 writel(ctrl, fbi->regs + SM501_DC_CRT_CONTROL);
919 sm501fb_sync_regs(fbi);
921 return 0;
924 /* sm501fb_cursor
926 * set or change the hardware cursor parameters
929 static int sm501fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
931 struct sm501fb_par *par = info->par;
932 struct sm501fb_info *fbi = par->info;
933 void __iomem *base = fbi->regs;
934 unsigned long hwc_addr;
935 unsigned long fg, bg;
937 dev_dbg(fbi->dev, "%s(%p,%p)\n", __func__, info, cursor);
939 if (par->head == HEAD_CRT)
940 base += SM501_DC_CRT_HWC_BASE;
941 else
942 base += SM501_DC_PANEL_HWC_BASE;
944 /* check not being asked to exceed capabilities */
946 if (cursor->image.width > 64)
947 return -EINVAL;
949 if (cursor->image.height > 64)
950 return -EINVAL;
952 if (cursor->image.depth > 1)
953 return -EINVAL;
955 hwc_addr = readl(base + SM501_OFF_HWC_ADDR);
957 if (cursor->enable)
958 writel(hwc_addr | SM501_HWC_EN, base + SM501_OFF_HWC_ADDR);
959 else
960 writel(hwc_addr & ~SM501_HWC_EN, base + SM501_OFF_HWC_ADDR);
962 /* set data */
963 if (cursor->set & FB_CUR_SETPOS) {
964 unsigned int x = cursor->image.dx;
965 unsigned int y = cursor->image.dy;
967 if (x >= 2048 || y >= 2048 )
968 return -EINVAL;
970 dev_dbg(fbi->dev, "set position %d,%d\n", x, y);
972 //y += cursor->image.height;
974 writel(x | (y << 16), base + SM501_OFF_HWC_LOC);
977 if (cursor->set & FB_CUR_SETCMAP) {
978 unsigned int bg_col = cursor->image.bg_color;
979 unsigned int fg_col = cursor->image.fg_color;
981 dev_dbg(fbi->dev, "%s: update cmap (%08x,%08x)\n",
982 __func__, bg_col, fg_col);
984 bg = ((info->cmap.red[bg_col] & 0xF8) << 8) |
985 ((info->cmap.green[bg_col] & 0xFC) << 3) |
986 ((info->cmap.blue[bg_col] & 0xF8) >> 3);
988 fg = ((info->cmap.red[fg_col] & 0xF8) << 8) |
989 ((info->cmap.green[fg_col] & 0xFC) << 3) |
990 ((info->cmap.blue[fg_col] & 0xF8) >> 3);
992 dev_dbg(fbi->dev, "fgcol %08x, bgcol %08x\n", fg, bg);
994 writel(bg, base + SM501_OFF_HWC_COLOR_1_2);
995 writel(fg, base + SM501_OFF_HWC_COLOR_3);
998 if (cursor->set & FB_CUR_SETSIZE ||
999 cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE)) {
1000 /* SM501 cursor is a two bpp 64x64 bitmap this routine
1001 * clears it to transparent then combines the cursor
1002 * shape plane with the colour plane to set the
1003 * cursor */
1004 int x, y;
1005 const unsigned char *pcol = cursor->image.data;
1006 const unsigned char *pmsk = cursor->mask;
1007 void __iomem *dst = par->cursor.k_addr;
1008 unsigned char dcol = 0;
1009 unsigned char dmsk = 0;
1010 unsigned int op;
1012 dev_dbg(fbi->dev, "%s: setting shape (%d,%d)\n",
1013 __func__, cursor->image.width, cursor->image.height);
1015 for (op = 0; op < (64*64*2)/8; op+=4)
1016 writel(0x0, dst + op);
1018 for (y = 0; y < cursor->image.height; y++) {
1019 for (x = 0; x < cursor->image.width; x++) {
1020 if ((x % 8) == 0) {
1021 dcol = *pcol++;
1022 dmsk = *pmsk++;
1023 } else {
1024 dcol >>= 1;
1025 dmsk >>= 1;
1028 if (dmsk & 1) {
1029 op = (dcol & 1) ? 1 : 3;
1030 op <<= ((x % 4) * 2);
1032 op |= readb(dst + (x / 4));
1033 writeb(op, dst + (x / 4));
1036 dst += (64*2)/8;
1040 sm501fb_sync_regs(fbi); /* ensure cursor data flushed */
1041 return 0;
1044 /* sm501fb_crtsrc_show
1046 * device attribute code to show where the crt output is sourced from
1049 static ssize_t sm501fb_crtsrc_show(struct device *dev,
1050 struct device_attribute *attr, char *buf)
1052 struct sm501fb_info *info = dev_get_drvdata(dev);
1053 unsigned long ctrl;
1055 ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
1056 ctrl &= SM501_DC_CRT_CONTROL_SEL;
1058 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl ? "crt" : "panel");
1061 /* sm501fb_crtsrc_show
1063 * device attribute code to set where the crt output is sourced from
1066 static ssize_t sm501fb_crtsrc_store(struct device *dev,
1067 struct device_attribute *attr,
1068 const char *buf, size_t len)
1070 struct sm501fb_info *info = dev_get_drvdata(dev);
1071 enum sm501_controller head;
1072 unsigned long ctrl;
1074 if (len < 1)
1075 return -EINVAL;
1077 if (strnicmp(buf, "crt", 3) == 0)
1078 head = HEAD_CRT;
1079 else if (strnicmp(buf, "panel", 5) == 0)
1080 head = HEAD_PANEL;
1081 else
1082 return -EINVAL;
1084 dev_info(dev, "setting crt source to head %d\n", head);
1086 ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
1088 if (head == HEAD_CRT) {
1089 ctrl |= SM501_DC_CRT_CONTROL_SEL;
1090 ctrl |= SM501_DC_CRT_CONTROL_ENABLE;
1091 ctrl |= SM501_DC_CRT_CONTROL_TE;
1092 } else {
1093 ctrl &= ~SM501_DC_CRT_CONTROL_SEL;
1094 ctrl &= ~SM501_DC_CRT_CONTROL_ENABLE;
1095 ctrl &= ~SM501_DC_CRT_CONTROL_TE;
1098 writel(ctrl, info->regs + SM501_DC_CRT_CONTROL);
1099 sm501fb_sync_regs(info);
1101 return len;
1104 /* Prepare the device_attr for registration with sysfs later */
1105 static DEVICE_ATTR(crt_src, 0666, sm501fb_crtsrc_show, sm501fb_crtsrc_store);
1107 /* sm501fb_show_regs
1109 * show the primary sm501 registers
1111 static int sm501fb_show_regs(struct sm501fb_info *info, char *ptr,
1112 unsigned int start, unsigned int len)
1114 void __iomem *mem = info->regs;
1115 char *buf = ptr;
1116 unsigned int reg;
1118 for (reg = start; reg < (len + start); reg += 4)
1119 ptr += sprintf(ptr, "%08x = %08x\n", reg, readl(mem + reg));
1121 return ptr - buf;
1124 /* sm501fb_debug_show_crt
1126 * show the crt control and cursor registers
1129 static ssize_t sm501fb_debug_show_crt(struct device *dev,
1130 struct device_attribute *attr, char *buf)
1132 struct sm501fb_info *info = dev_get_drvdata(dev);
1133 char *ptr = buf;
1135 ptr += sm501fb_show_regs(info, ptr, SM501_DC_CRT_CONTROL, 0x40);
1136 ptr += sm501fb_show_regs(info, ptr, SM501_DC_CRT_HWC_BASE, 0x10);
1138 return ptr - buf;
1141 static DEVICE_ATTR(fbregs_crt, 0444, sm501fb_debug_show_crt, NULL);
1143 /* sm501fb_debug_show_pnl
1145 * show the panel control and cursor registers
1148 static ssize_t sm501fb_debug_show_pnl(struct device *dev,
1149 struct device_attribute *attr, char *buf)
1151 struct sm501fb_info *info = dev_get_drvdata(dev);
1152 char *ptr = buf;
1154 ptr += sm501fb_show_regs(info, ptr, 0x0, 0x40);
1155 ptr += sm501fb_show_regs(info, ptr, SM501_DC_PANEL_HWC_BASE, 0x10);
1157 return ptr - buf;
1160 static DEVICE_ATTR(fbregs_pnl, 0444, sm501fb_debug_show_pnl, NULL);
1162 /* framebuffer ops */
1164 static struct fb_ops sm501fb_ops_crt = {
1165 .owner = THIS_MODULE,
1166 .fb_check_var = sm501fb_check_var_crt,
1167 .fb_set_par = sm501fb_set_par_crt,
1168 .fb_blank = sm501fb_blank_crt,
1169 .fb_setcolreg = sm501fb_setcolreg,
1170 .fb_pan_display = sm501fb_pan_crt,
1171 .fb_cursor = sm501fb_cursor,
1172 .fb_fillrect = cfb_fillrect,
1173 .fb_copyarea = cfb_copyarea,
1174 .fb_imageblit = cfb_imageblit,
1177 static struct fb_ops sm501fb_ops_pnl = {
1178 .owner = THIS_MODULE,
1179 .fb_check_var = sm501fb_check_var_pnl,
1180 .fb_set_par = sm501fb_set_par_pnl,
1181 .fb_pan_display = sm501fb_pan_pnl,
1182 .fb_blank = sm501fb_blank_pnl,
1183 .fb_setcolreg = sm501fb_setcolreg,
1184 .fb_cursor = sm501fb_cursor,
1185 .fb_fillrect = cfb_fillrect,
1186 .fb_copyarea = cfb_copyarea,
1187 .fb_imageblit = cfb_imageblit,
1190 /* sm501fb_info_alloc
1192 * creates and initialises an sm501fb_info structure
1195 static struct sm501fb_info *sm501fb_info_alloc(struct fb_info *fbinfo_crt,
1196 struct fb_info *fbinfo_pnl)
1198 struct sm501fb_info *info;
1199 struct sm501fb_par *par;
1201 info = kzalloc(sizeof(struct sm501fb_info), GFP_KERNEL);
1202 if (info) {
1203 /* set the references back */
1205 par = fbinfo_crt->par;
1206 par->info = info;
1207 par->head = HEAD_CRT;
1208 fbinfo_crt->pseudo_palette = &par->pseudo_palette;
1210 par = fbinfo_pnl->par;
1211 par->info = info;
1212 par->head = HEAD_PANEL;
1213 fbinfo_pnl->pseudo_palette = &par->pseudo_palette;
1215 /* store the two fbs into our info */
1216 info->fb[HEAD_CRT] = fbinfo_crt;
1217 info->fb[HEAD_PANEL] = fbinfo_pnl;
1220 return info;
1223 /* sm501_init_cursor
1225 * initialise hw cursor parameters
1228 static int sm501_init_cursor(struct fb_info *fbi, unsigned int reg_base)
1230 struct sm501fb_par *par = fbi->par;
1231 struct sm501fb_info *info = par->info;
1232 int ret;
1234 par->cursor_regs = info->regs + reg_base;
1236 ret = sm501_alloc_mem(info, &par->cursor, SM501_MEMF_CURSOR, 1024);
1237 if (ret < 0)
1238 return ret;
1240 /* initialise the colour registers */
1242 writel(par->cursor.sm_addr, par->cursor_regs + SM501_OFF_HWC_ADDR);
1244 writel(0x00, par->cursor_regs + SM501_OFF_HWC_LOC);
1245 writel(0x00, par->cursor_regs + SM501_OFF_HWC_COLOR_1_2);
1246 writel(0x00, par->cursor_regs + SM501_OFF_HWC_COLOR_3);
1247 sm501fb_sync_regs(info);
1249 return 0;
1252 /* sm501fb_info_start
1254 * fills the par structure claiming resources and remapping etc.
1257 static int sm501fb_start(struct sm501fb_info *info,
1258 struct platform_device *pdev)
1260 struct resource *res;
1261 struct device *dev;
1262 int ret;
1264 info->dev = dev = &pdev->dev;
1265 platform_set_drvdata(pdev, info);
1267 info->irq = ret = platform_get_irq(pdev, 0);
1268 if (ret < 0) {
1269 /* we currently do not use the IRQ */
1270 dev_warn(dev, "no irq for device\n");
1273 /* allocate, reserve and remap resources for registers */
1274 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1275 if (res == NULL) {
1276 dev_err(dev, "no resource definition for registers\n");
1277 ret = -ENOENT;
1278 goto err_release;
1281 info->regs_res = request_mem_region(res->start,
1282 res->end - res->start,
1283 pdev->name);
1285 if (info->regs_res == NULL) {
1286 dev_err(dev, "cannot claim registers\n");
1287 ret = -ENXIO;
1288 goto err_release;
1291 info->regs = ioremap(res->start, (res->end - res->start)+1);
1292 if (info->regs == NULL) {
1293 dev_err(dev, "cannot remap registers\n");
1294 ret = -ENXIO;
1295 goto err_regs_res;
1298 /* allocate, reserve resources for framebuffer */
1299 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1300 if (res == NULL) {
1301 dev_err(dev, "no memory resource defined\n");
1302 ret = -ENXIO;
1303 goto err_regs_map;
1306 info->fbmem_res = request_mem_region(res->start,
1307 (res->end - res->start)+1,
1308 pdev->name);
1309 if (info->fbmem_res == NULL) {
1310 dev_err(dev, "cannot claim framebuffer\n");
1311 ret = -ENXIO;
1312 goto err_regs_map;
1315 info->fbmem = ioremap(res->start, (res->end - res->start)+1);
1316 if (info->fbmem == NULL) {
1317 dev_err(dev, "cannot remap framebuffer\n");
1318 goto err_mem_res;
1321 info->fbmem_len = (res->end - res->start)+1;
1323 /* enable display controller */
1324 sm501_unit_power(dev->parent, SM501_GATE_DISPLAY, 1);
1326 /* setup cursors */
1328 sm501_init_cursor(info->fb[HEAD_CRT], SM501_DC_CRT_HWC_ADDR);
1329 sm501_init_cursor(info->fb[HEAD_PANEL], SM501_DC_PANEL_HWC_ADDR);
1331 return 0; /* everything is setup */
1333 err_mem_res:
1334 release_resource(info->fbmem_res);
1335 kfree(info->fbmem_res);
1337 err_regs_map:
1338 iounmap(info->regs);
1340 err_regs_res:
1341 release_resource(info->regs_res);
1342 kfree(info->regs_res);
1344 err_release:
1345 return ret;
1348 static void sm501fb_stop(struct sm501fb_info *info)
1350 /* disable display controller */
1351 sm501_unit_power(info->dev->parent, SM501_GATE_DISPLAY, 0);
1353 iounmap(info->fbmem);
1354 release_resource(info->fbmem_res);
1355 kfree(info->fbmem_res);
1357 iounmap(info->regs);
1358 release_resource(info->regs_res);
1359 kfree(info->regs_res);
1362 static void sm501fb_info_release(struct sm501fb_info *info)
1364 kfree(info);
1367 static int sm501fb_init_fb(struct fb_info *fb,
1368 enum sm501_controller head,
1369 const char *fbname)
1371 struct sm501_platdata_fbsub *pd;
1372 struct sm501fb_par *par = fb->par;
1373 struct sm501fb_info *info = par->info;
1374 unsigned long ctrl;
1375 unsigned int enable;
1376 int ret;
1378 switch (head) {
1379 case HEAD_CRT:
1380 pd = info->pdata->fb_crt;
1381 ctrl = readl(info->regs + SM501_DC_CRT_CONTROL);
1382 enable = (ctrl & SM501_DC_CRT_CONTROL_ENABLE) ? 1 : 0;
1384 /* ensure we set the correct source register */
1385 if (info->pdata->fb_route != SM501_FB_CRT_PANEL) {
1386 ctrl |= SM501_DC_CRT_CONTROL_SEL;
1387 writel(ctrl, info->regs + SM501_DC_CRT_CONTROL);
1390 break;
1392 case HEAD_PANEL:
1393 pd = info->pdata->fb_pnl;
1394 ctrl = readl(info->regs + SM501_DC_PANEL_CONTROL);
1395 enable = (ctrl & SM501_DC_PANEL_CONTROL_EN) ? 1 : 0;
1396 break;
1398 default:
1399 pd = NULL; /* stop compiler warnings */
1400 ctrl = 0;
1401 enable = 0;
1402 BUG();
1405 dev_info(info->dev, "fb %s %sabled at start\n",
1406 fbname, enable ? "en" : "dis");
1408 /* check to see if our routing allows this */
1410 if (head == HEAD_CRT && info->pdata->fb_route == SM501_FB_CRT_PANEL) {
1411 ctrl &= ~SM501_DC_CRT_CONTROL_SEL;
1412 writel(ctrl, info->regs + SM501_DC_CRT_CONTROL);
1413 enable = 0;
1416 strlcpy(fb->fix.id, fbname, sizeof(fb->fix.id));
1418 memcpy(&par->ops,
1419 (head == HEAD_CRT) ? &sm501fb_ops_crt : &sm501fb_ops_pnl,
1420 sizeof(struct fb_ops));
1422 /* update ops dependant on what we've been passed */
1424 if ((pd->flags & SM501FB_FLAG_USE_HWCURSOR) == 0)
1425 par->ops.fb_cursor = NULL;
1427 fb->fbops = &par->ops;
1428 fb->flags = FBINFO_FLAG_DEFAULT |
1429 FBINFO_HWACCEL_XPAN | FBINFO_HWACCEL_YPAN;
1431 /* fixed data */
1433 fb->fix.type = FB_TYPE_PACKED_PIXELS;
1434 fb->fix.type_aux = 0;
1435 fb->fix.xpanstep = 1;
1436 fb->fix.ypanstep = 1;
1437 fb->fix.ywrapstep = 0;
1438 fb->fix.accel = FB_ACCEL_NONE;
1440 /* screenmode */
1442 fb->var.nonstd = 0;
1443 fb->var.activate = FB_ACTIVATE_NOW;
1444 fb->var.accel_flags = 0;
1445 fb->var.vmode = FB_VMODE_NONINTERLACED;
1446 fb->var.bits_per_pixel = 16;
1448 if (enable && (pd->flags & SM501FB_FLAG_USE_INIT_MODE) && 0) {
1449 /* TODO read the mode from the current display */
1451 } else {
1452 if (pd->def_mode) {
1453 dev_info(info->dev, "using supplied mode\n");
1454 fb_videomode_to_var(&fb->var, pd->def_mode);
1456 fb->var.bits_per_pixel = pd->def_bpp ? pd->def_bpp : 8;
1457 fb->var.xres_virtual = fb->var.xres;
1458 fb->var.yres_virtual = fb->var.yres;
1459 } else {
1460 ret = fb_find_mode(&fb->var, fb,
1461 NULL, NULL, 0, NULL, 8);
1463 if (ret == 0 || ret == 4) {
1464 dev_err(info->dev,
1465 "failed to get initial mode\n");
1466 return -EINVAL;
1471 /* initialise and set the palette */
1472 fb_alloc_cmap(&fb->cmap, NR_PALETTE, 0);
1473 fb_set_cmap(&fb->cmap, fb);
1475 ret = (fb->fbops->fb_check_var)(&fb->var, fb);
1476 if (ret)
1477 dev_err(info->dev, "check_var() failed on initial setup?\n");
1479 /* ensure we've activated our new configuration */
1480 (fb->fbops->fb_set_par)(fb);
1482 return 0;
1485 /* default platform data if none is supplied (ie, PCI device) */
1487 static struct sm501_platdata_fbsub sm501fb_pdata_crt = {
1488 .flags = (SM501FB_FLAG_USE_INIT_MODE |
1489 SM501FB_FLAG_USE_HWCURSOR |
1490 SM501FB_FLAG_USE_HWACCEL |
1491 SM501FB_FLAG_DISABLE_AT_EXIT),
1495 static struct sm501_platdata_fbsub sm501fb_pdata_pnl = {
1496 .flags = (SM501FB_FLAG_USE_INIT_MODE |
1497 SM501FB_FLAG_USE_HWCURSOR |
1498 SM501FB_FLAG_USE_HWACCEL |
1499 SM501FB_FLAG_DISABLE_AT_EXIT),
1502 static struct sm501_platdata_fb sm501fb_def_pdata = {
1503 .fb_route = SM501_FB_OWN,
1504 .fb_crt = &sm501fb_pdata_crt,
1505 .fb_pnl = &sm501fb_pdata_pnl,
1508 static char driver_name_crt[] = "sm501fb-crt";
1509 static char driver_name_pnl[] = "sm501fb-panel";
1511 static int __init sm501fb_probe(struct platform_device *pdev)
1513 struct sm501fb_info *info;
1514 struct device *dev = &pdev->dev;
1515 struct fb_info *fbinfo_crt;
1516 struct fb_info *fbinfo_pnl;
1517 int ret;
1519 /* allocate our framebuffers */
1521 fbinfo_crt = framebuffer_alloc(sizeof(struct sm501fb_par), dev);
1522 if (fbinfo_crt == NULL) {
1523 dev_err(dev, "cannot allocate crt framebuffer\n");
1524 return -ENOMEM;
1527 fbinfo_pnl = framebuffer_alloc(sizeof(struct sm501fb_par), dev);
1528 if (fbinfo_pnl == NULL) {
1529 dev_err(dev, "cannot allocate panel framebuffer\n");
1530 ret = -ENOMEM;
1531 goto fbinfo_crt_alloc_fail;
1534 info = sm501fb_info_alloc(fbinfo_crt, fbinfo_pnl);
1535 if (info == NULL) {
1536 dev_err(dev, "cannot allocate par\n");
1537 ret = -ENOMEM;
1538 goto sm501fb_alloc_fail;
1541 if (dev->parent->platform_data) {
1542 struct sm501_platdata *pd = dev->parent->platform_data;
1543 info->pdata = pd->fb;
1546 if (info->pdata == NULL) {
1547 dev_info(dev, "using default configuration data\n");
1548 info->pdata = &sm501fb_def_pdata;
1551 /* start the framebuffers */
1553 ret = sm501fb_start(info, pdev);
1554 if (ret) {
1555 dev_err(dev, "cannot initialise SM501\n");
1556 goto sm501fb_start_fail;
1559 /* CRT framebuffer setup */
1561 ret = sm501fb_init_fb(fbinfo_crt, HEAD_CRT, driver_name_crt);
1562 if (ret) {
1563 dev_err(dev, "cannot initialise CRT fb\n");
1564 goto sm501fb_start_fail;
1567 /* Panel framebuffer setup */
1569 ret = sm501fb_init_fb(fbinfo_pnl, HEAD_PANEL, driver_name_pnl);
1570 if (ret) {
1571 dev_err(dev, "cannot initialise Panel fb\n");
1572 goto sm501fb_start_fail;
1575 /* register framebuffers */
1577 ret = register_framebuffer(fbinfo_crt);
1578 if (ret < 0) {
1579 dev_err(dev, "failed to register CRT fb (%d)\n", ret);
1580 goto register_crt_fail;
1583 ret = register_framebuffer(fbinfo_pnl);
1584 if (ret < 0) {
1585 dev_err(dev, "failed to register panel fb (%d)\n", ret);
1586 goto register_pnl_fail;
1589 dev_info(dev, "fb%d: %s frame buffer device\n",
1590 fbinfo_crt->node, fbinfo_crt->fix.id);
1592 dev_info(dev, "fb%d: %s frame buffer device\n",
1593 fbinfo_pnl->node, fbinfo_pnl->fix.id);
1595 /* create device files */
1597 ret = device_create_file(dev, &dev_attr_crt_src);
1598 if (ret)
1599 goto crtsrc_fail;
1601 ret = device_create_file(dev, &dev_attr_fbregs_pnl);
1602 if (ret)
1603 goto fbregs_pnl_fail;
1605 ret = device_create_file(dev, &dev_attr_fbregs_crt);
1606 if (ret)
1607 goto fbregs_crt_fail;
1609 /* we registered, return ok */
1610 return 0;
1612 fbregs_crt_fail:
1613 device_remove_file(dev, &dev_attr_fbregs_pnl);
1615 fbregs_pnl_fail:
1616 device_remove_file(dev, &dev_attr_crt_src);
1618 crtsrc_fail:
1619 unregister_framebuffer(fbinfo_pnl);
1621 register_pnl_fail:
1622 unregister_framebuffer(fbinfo_crt);
1624 register_crt_fail:
1625 sm501fb_stop(info);
1627 sm501fb_start_fail:
1628 sm501fb_info_release(info);
1630 sm501fb_alloc_fail:
1631 framebuffer_release(fbinfo_pnl);
1633 fbinfo_crt_alloc_fail:
1634 framebuffer_release(fbinfo_crt);
1636 return ret;
1641 * Cleanup
1643 static int sm501fb_remove(struct platform_device *pdev)
1645 struct sm501fb_info *info = platform_get_drvdata(pdev);
1646 struct fb_info *fbinfo_crt = info->fb[0];
1647 struct fb_info *fbinfo_pnl = info->fb[1];
1649 device_remove_file(&pdev->dev, &dev_attr_fbregs_crt);
1650 device_remove_file(&pdev->dev, &dev_attr_fbregs_pnl);
1651 device_remove_file(&pdev->dev, &dev_attr_crt_src);
1653 unregister_framebuffer(fbinfo_crt);
1654 unregister_framebuffer(fbinfo_pnl);
1656 sm501fb_stop(info);
1657 sm501fb_info_release(info);
1659 framebuffer_release(fbinfo_pnl);
1660 framebuffer_release(fbinfo_crt);
1662 return 0;
1665 #ifdef CONFIG_PM
1667 static int sm501fb_suspend_fb(struct sm501fb_info *info,
1668 enum sm501_controller head)
1670 struct fb_info *fbi = info->fb[head];
1671 struct sm501fb_par *par = fbi->par;
1673 if (par->screen.size == 0)
1674 return 0;
1676 /* backup copies in case chip is powered down over suspend */
1678 par->store_fb = vmalloc(par->screen.size);
1679 if (par->store_fb == NULL) {
1680 dev_err(info->dev, "no memory to store screen\n");
1681 return -ENOMEM;
1684 par->store_cursor = vmalloc(par->cursor.size);
1685 if (par->store_cursor == NULL) {
1686 dev_err(info->dev, "no memory to store cursor\n");
1687 goto err_nocursor;
1690 memcpy_fromio(par->store_fb, par->screen.k_addr, par->screen.size);
1691 memcpy_fromio(par->store_cursor, par->cursor.k_addr, par->cursor.size);
1693 /* blank the relevant interface to ensure unit power minimised */
1694 (par->ops.fb_blank)(FB_BLANK_POWERDOWN, fbi);
1696 return 0;
1698 err_nocursor:
1699 vfree(par->store_fb);
1701 return -ENOMEM;
1705 static void sm501fb_resume_fb(struct sm501fb_info *info,
1706 enum sm501_controller head)
1708 struct fb_info *fbi = info->fb[head];
1709 struct sm501fb_par *par = fbi->par;
1711 if (par->screen.size == 0)
1712 return;
1714 /* re-activate the configuration */
1716 (par->ops.fb_set_par)(fbi);
1718 /* restore the data */
1720 memcpy_toio(par->screen.k_addr, par->store_fb, par->screen.size);
1721 memcpy_toio(par->cursor.k_addr, par->store_cursor, par->cursor.size);
1723 vfree(par->store_fb);
1724 vfree(par->store_cursor);
1728 /* suspend and resume support */
1730 static int sm501fb_suspend(struct platform_device *pdev, pm_message_t state)
1732 struct sm501fb_info *info = platform_get_drvdata(pdev);
1734 sm501fb_suspend_fb(info, HEAD_CRT);
1735 sm501fb_suspend_fb(info, HEAD_PANEL);
1737 /* turn off the clocks, in case the device is not powered down */
1738 sm501_unit_power(info->dev->parent, SM501_GATE_DISPLAY, 0);
1740 return 0;
1743 static int sm501fb_resume(struct platform_device *pdev)
1745 struct sm501fb_info *info = platform_get_drvdata(pdev);
1747 sm501_unit_power(info->dev->parent, SM501_GATE_DISPLAY, 1);
1749 sm501fb_resume_fb(info, HEAD_CRT);
1750 sm501fb_resume_fb(info, HEAD_PANEL);
1752 return 0;
1755 #else
1756 #define sm501fb_suspend NULL
1757 #define sm501fb_resume NULL
1758 #endif
1760 static struct platform_driver sm501fb_driver = {
1761 .probe = sm501fb_probe,
1762 .remove = sm501fb_remove,
1763 .suspend = sm501fb_suspend,
1764 .resume = sm501fb_resume,
1765 .driver = {
1766 .name = "sm501-fb",
1767 .owner = THIS_MODULE,
1771 static int __devinit sm501fb_init(void)
1773 return platform_driver_register(&sm501fb_driver);
1776 static void __exit sm501fb_cleanup(void)
1778 platform_driver_unregister(&sm501fb_driver);
1781 module_init(sm501fb_init);
1782 module_exit(sm501fb_cleanup);
1784 MODULE_AUTHOR("Ben Dooks, Vincent Sanders");
1785 MODULE_DESCRIPTION("SM501 Framebuffer driver");
1786 MODULE_LICENSE("GPL v2");