2 * drivers/video/ubicom32fb.c
3 * Ubicom32 frame buffer driver
5 * (C) Copyright 2009, Ubicom, Inc.
7 * This file is part of the Ubicom32 Linux Kernel Port.
9 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10 * it and/or modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, either version 2 of the
12 * License, or (at your option) any later version.
14 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with the Ubicom32 Linux Kernel Port. If not,
21 * see <http://www.gnu.org/licenses/>.
23 * Ubicom32 implementation derived from (with many thanks):
30 * This driver was based on skeletonfb.c, Skeleton for a frame buffer device by
34 #include <linux/device.h>
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/version.h>
38 #include <linux/errno.h>
39 #include <linux/string.h>
42 #include <linux/init.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/platform_device.h>
45 #include <linux/device.h>
46 #include <linux/uaccess.h>
47 #include <linux/interrupt.h>
50 #include <asm/ip5000.h>
51 #include <asm/vdc_tio.h>
52 #include <asm/ubicom32fb.h>
54 #define DRIVER_NAME "ubicom32fb"
55 #define DRIVER_DESCRIPTION "Ubicom32 frame buffer driver"
57 #define PALETTE_ENTRIES_NO 16
62 * vram_size: VRAM size in kilobytes, subject to alignment
64 static int vram_size = 0;
65 module_param(vram_size, int, 0);
66 MODULE_PARM_DESC(vram, "VRAM size, in kilobytes to allocate, should be at least the size of one screen, subject to alignment");
67 static int init_value = 0;
68 module_param(init_value, int, 0);
69 MODULE_PARM_DESC(init, "Initial value of the framebuffer (16-bit number).");
72 * fb_fix_screeninfo defines the non-changeable properties of the VDC, depending on what mode it is in.
74 static struct fb_fix_screeninfo ubicom32fb_fix = {
76 .type = FB_TYPE_PACKED_PIXELS,
77 .visual = FB_VISUAL_TRUECOLOR,
78 .accel = FB_ACCEL_UBICOM32,
82 * Filled in at probe time when we find out what the hardware supports
84 static struct fb_var_screeninfo ubicom32fb_var;
87 * Private data structure
89 struct ubicom32fb_drvdata {
90 struct fb_info *fbinfo;
94 * The address of the framebuffer in memory
100 * Total size of vram including alignment allowance
105 * Interrupt to set when changing registers
110 * Optional: Interrupt used by TIO to signal us
115 * Base address of the regs for VDC_TIO
117 volatile struct vdc_tio_vp_regs *regs;
120 * non-zero if we are in yuv mode
125 * Fake palette of 16 colors
127 u32 pseudo_palette[PALETTE_ENTRIES_NO];
130 * Wait queue and lock used to block when we need to wait
131 * for something to happen.
133 wait_queue_head_t waitq;
139 * ubicom32fb_set_next_frame
140 * Sets the next frame buffer to display
142 * if sync is TRUE then this function will block until the hardware
143 * acknowledges the change
145 static inline void ubicom32fb_set_next_frame(struct ubicom32fb_drvdata *ud, void *fb, u8_t sync)
147 ud->regs->next_frame_flags = ud->is_yuv ? VDCTIO_NEXT_FRAME_FLAG_YUV : 0;
148 ud->regs->next_frame = (void *)((u32_t)fb | 1);
151 * If we have interrupts, then we can wait on it
153 if (ud->rx_int != -1) {
157 spin_lock_irqsave(&ud->lock, flags);
158 prepare_to_wait(&ud->waitq, &wait, TASK_INTERRUPTIBLE);
159 spin_unlock_irqrestore(&ud->lock, flags);
161 finish_wait(&ud->waitq, &wait);
166 * No interrupt, we will just spin here
168 while (sync && ((u32_t)ud->regs->next_frame & 1));
172 * ubicom32fb_send_command
173 * Sends a command/data pair to the VDC
175 static inline void ubicom32fb_send_command(struct ubicom32fb_drvdata *ud, u16 command, u8_t block)
177 ud->regs->command = command;
178 ubicom32_set_interrupt(ud->vp_int);
179 while (block && ud->regs->command);
184 * Handles any ioctls sent to us
186 static int ubicom32fb_ioctl(struct fb_info *fbi, unsigned int cmd,
189 struct ubicom32fb_drvdata *ud = (struct ubicom32fb_drvdata *)fbi->par;
190 void __user *argp = (void __user *)arg;
191 int retval = -EFAULT;
194 case UBICOM32FB_IOCTL_SET_NEXT_FRAME_SYNC:
195 // check alignment, return -EINVAL if necessary
196 ubicom32fb_set_next_frame(ud, argp, 1);
200 case UBICOM32FB_IOCTL_SET_NEXT_FRAME:
201 // check alignment, return -EINVAL if necessary
202 ubicom32fb_set_next_frame(ud, argp, 0);
206 case UBICOM32FB_IOCTL_SET_MODE:
207 if (!(ud->regs->caps & VDCTIO_CAPS_SUPPORTS_SCALING)) {
210 struct ubicom32fb_mode mode;
211 volatile struct vdc_tio_vp_regs *regs = ud->regs;
214 if (copy_from_user(&mode, argp, sizeof(mode))) {
218 regs->x_in = mode.width;
219 regs->y_in = mode.height;
220 regs->x_out = regs->xres;
221 regs->y_out = regs->yres;
222 if (mode.flags & UBICOM32FB_IOCTL_SET_MODE_FLAG_YUV_SCAN_ORDER) {
223 flags |= VDCTIO_SCALE_FLAG_YUV_SCAN_ORDER;
225 if (mode.flags & UBICOM32FB_IOCTL_SET_MODE_FLAG_YUV_BLOCK_ORDER) {
226 flags |= VDCTIO_SCALE_FLAG_YUV_BLOCK_ORDER;
228 ud->is_yuv = mode.flags & UBICOM32FB_IOCTL_SET_MODE_FLAG_YUV;
230 flags |= VDCTIO_SCALE_FLAG_YUV;
232 if (mode.flags & UBICOM32FB_IOCTL_SET_MODE_FLAG_VRANGE_16_255) {
233 flags |= VDCTIO_SCALE_FLAG_VRANGE_16_255;
235 if (mode.flags & UBICOM32FB_IOCTL_SET_MODE_FLAG_VRANGE_0_255) {
236 flags |= VDCTIO_SCALE_FLAG_VRANGE_0_255;
238 if (mode.flags & UBICOM32FB_IOCTL_SET_MODE_FLAG_VSUB) {
239 flags |= VDCTIO_SCALE_FLAG_VSUB;
241 if (mode.flags & UBICOM32FB_IOCTL_SET_MODE_FLAG_HSUB_2_1) {
242 flags |= VDCTIO_SCALE_FLAG_HSUB_2_1;
244 if (mode.flags & UBICOM32FB_IOCTL_SET_MODE_FLAG_HSUB_1_1) {
245 flags |= VDCTIO_SCALE_FLAG_HSUB_1_1;
247 if (mode.flags & UBICOM32FB_IOCTL_SET_MODE_FLAG_SCALE_ENABLE) {
248 flags |= VDCTIO_SCALE_FLAG_ENABLE;
250 if (mode.next_frame) {
251 flags |= VDCTIO_SCALE_FLAG_SET_FRAME_BUFFER;
252 regs->next_frame = mode.next_frame;
255 regs->scale_flags = flags;
256 ubicom32fb_send_command(ud, VDCTIO_COMMAND_SET_SCALE_MODE, 1);
262 retval = -ENOIOCTLCMD;
270 * ubicom32fb_interrupt
271 * Called by the OS when the TIO has set the rx_int
273 static irqreturn_t ubicom32fb_interrupt(int vec, void *appdata)
275 struct ubicom32fb_drvdata *ud = (struct ubicom32fb_drvdata *)appdata;
277 spin_lock(&ud->lock);
278 if (waitqueue_active(&ud->waitq)) {
281 spin_unlock(&ud->lock);
287 * ubicom32fb_pan_display
288 * Pans the display to a given location. Supports only y direction panning.
290 static int ubicom32fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *fbi)
292 struct ubicom32fb_drvdata *ud = (struct ubicom32fb_drvdata *)fbi->par;
296 * Get the last y line that would be displayed. Since we don't support YWRAP,
297 * it must be less than our virtual y size.
299 u32 lasty = var->yoffset + var->yres;
300 if (lasty > fbi->var.yres_virtual) {
302 * We would fall off the end of our frame buffer if we panned here.
309 * We don't support panning in the x direction
315 * Everything looks sane, go ahead and pan
317 * We have to calculate a new address for the VDC to look at
319 new_addr = ud->fb_aligned + (var->yoffset * fbi->fix.line_length);
322 * Send down the command. The buffer will switch at the next vertical blank
324 ubicom32fb_set_next_frame(ud, (void *)new_addr, 0);
330 * ubicom32fb_setcolreg
331 * Sets a color in our virtual palette
333 static int ubicom32fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue, unsigned transp, struct fb_info *fbi)
335 u32 *palette = fbi->pseudo_palette;
337 if (regno >= PALETTE_ENTRIES_NO) {
342 * We only use 8 bits from each color
349 * Convert any grayscale values
351 if (fbi->var.grayscale) {
352 u16 gray = red + green + blue;
353 gray += (gray >> 2) + (gray >> 3) - (gray >> 7);
363 palette[regno] = (red << fbi->var.red.offset) | (green << fbi->var.green.offset) |
364 (blue << fbi->var.blue.offset);
372 static int ubicom32fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
374 struct ubicom32fb_drvdata *drvdata = (struct ubicom32fb_drvdata *)info->par;
376 vma->vm_start = (unsigned long)(drvdata->fb_aligned);
378 vma->vm_end = vma->vm_start + info->fix.smem_len;
380 /* For those who don't understand how mmap works, go read
381 * Documentation/nommu-mmap.txt.
382 * For those that do, you will know that the VM_MAYSHARE flag
383 * must be set in the vma->vm_flags structure on noMMU
384 * Other flags can be set, and are documented in
388 vma->vm_flags |= VM_MAYSHARE | VM_SHARED;
396 static int ubicom32fb_blank(int blank_mode, struct fb_info *fbi)
400 struct ubicom32fb_drvdata *drvdata = to_ubicom32fb_drvdata(fbi);
402 switch (blank_mode) {
403 case FB_BLANK_UNBLANK:
405 ubicom32fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
408 case FB_BLANK_NORMAL:
409 case FB_BLANK_VSYNC_SUSPEND:
410 case FB_BLANK_HSYNC_SUSPEND:
411 case FB_BLANK_POWERDOWN:
413 ubicom32fb_out_be32(drvdata, REG_CTRL, 0);
418 return 0; /* success */
422 static struct fb_ops ubicom32fb_ops =
424 .owner = THIS_MODULE,
425 .fb_pan_display = ubicom32fb_pan_display,
426 .fb_setcolreg = ubicom32fb_setcolreg,
427 .fb_blank = ubicom32fb_blank,
428 .fb_mmap = ubicom32fb_mmap,
429 .fb_ioctl = ubicom32fb_ioctl,
430 .fb_fillrect = cfb_fillrect,
431 .fb_copyarea = cfb_copyarea,
432 .fb_imageblit = cfb_imageblit,
438 static int ubicom32fb_release(struct device *dev)
440 struct ubicom32fb_drvdata *ud = dev_get_drvdata(dev);
442 #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
443 //ubicom32fb_blank(VESA_POWERDOWN, &drvdata->info);
446 unregister_framebuffer(ud->fbinfo);
448 if (ud->cmap_alloc) {
449 fb_dealloc_cmap(&ud->fbinfo->cmap);
456 if (ud->rx_int != -1) {
457 free_irq(ud->rx_int, ud);
461 * Turn off the display
463 //ubicom32fb_out_be32(drvdata, REG_CTRL, 0);
464 //iounmap(drvdata->regs);
466 framebuffer_release(ud->fbinfo);
467 dev_set_drvdata(dev, NULL);
473 * ubicom32fb_platform_probe
475 static int __init ubicom32fb_platform_probe(struct platform_device *pdev)
477 struct ubicom32fb_drvdata *ud;
478 struct resource *irq_resource_rx;
479 struct resource *irq_resource_tx;
480 struct resource *mem_resource;
481 struct fb_info *fbinfo;
484 struct device *dev = &pdev->dev;
486 struct vdc_tio_vp_regs *regs;
491 irq_resource_tx = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
492 if (!irq_resource_tx) {
493 dev_err(dev, "No tx IRQ resource assigned\n");
497 irq_resource_rx = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
498 if (!irq_resource_rx) {
499 dev_err(dev, "No rx IRQ resource assigned\n");
503 mem_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
504 if (!mem_resource || !mem_resource->start) {
505 dev_err(dev, "No mem resource assigned\n");
508 regs = (struct vdc_tio_vp_regs *)mem_resource->start;
509 if (regs->version != VDCTIO_VP_VERSION) {
510 dev_err(dev, "VDCTIO is not compatible with this driver tio:%x drv:%x\n",
511 regs->version, VDCTIO_VP_VERSION);
516 * This is the minimum VRAM size
518 fbsize = regs->xres * regs->yres * (regs->bpp / 8);
520 vram_size = (fbsize + 1023) / 1024;
522 if (fbsize > (vram_size * 1024)) {
523 dev_err(dev, "Not enough VRAM for display, need >= %u bytes\n", fbsize);
524 return -ENOMEM; // should be ebadparam?
529 * Allocate the framebuffer instance + our private data
531 fbinfo = framebuffer_alloc(sizeof(struct ubicom32fb_drvdata), &pdev->dev);
533 dev_err(dev, "Not enough memory to allocate instance.\n");
538 * Fill in our private data.
540 ud = (struct ubicom32fb_drvdata *)fbinfo->par;
542 ud->regs = (struct vdc_tio_vp_regs *)(mem_resource->start);
543 dev_set_drvdata(dev, ud);
545 ud->vp_int = irq_resource_tx->start;
548 * If we were provided an rx_irq then we need to init the appropriate
549 * queues, locks, and functions.
552 if (irq_resource_rx->start != DEVTREE_IRQ_NONE) {
553 init_waitqueue_head(&ud->waitq);
554 mutex_init(&ud->lock);
555 if (request_irq(ud->rx_int, ubicom32fb_interrupt, IRQF_SHARED, "ubicom32fb_rx", ud)) {
556 dev_err(dev, "Couldn't request rx IRQ\n");
560 ud->rx_int = irq_resource_rx->start;
564 * Allocate and align the requested amount of VRAM
566 ud->total_vram_size = (vram_size * 1024) + regs->fb_align;
567 ud->fb = kmalloc(ud->total_vram_size, GFP_KERNEL);
568 if (ud->fb == NULL) {
569 dev_err(dev, "Couldn't allocate VRAM\n");
574 offset = (u32_t)ud->fb & (regs->fb_align - 1);
576 ud->fb_aligned = ud->fb;
578 offset = regs->fb_align - offset;
579 ud->fb_aligned = ud->fb + offset;
583 * Clear the entire frame buffer
586 memset(ud->fb_aligned, 0, vram_size * 1024);
588 unsigned short *p = ud->fb_aligned;
590 for (i = 0; i < ((vram_size * 1024) / sizeof(u16_t)); i++) {
596 * Fill in the fb_var_screeninfo structure
598 memset(&ubicom32fb_var, 0, sizeof(ubicom32fb_var));
599 ubicom32fb_var.bits_per_pixel = regs->bpp;
600 ubicom32fb_var.red.offset = regs->rshift;
601 ubicom32fb_var.green.offset = regs->gshift;
602 ubicom32fb_var.blue.offset = regs->bshift;
603 ubicom32fb_var.red.length = regs->rbits;
604 ubicom32fb_var.green.length = regs->gbits;
605 ubicom32fb_var.blue.length = regs->bbits;
606 ubicom32fb_var.activate = FB_ACTIVATE_NOW;
610 * Turn on the display
612 ud->reg_ctrl_default = REG_CTRL_ENABLE;
613 if (regs->rotate_screen)
614 ud->reg_ctrl_default |= REG_CTRL_ROTATE;
615 ubicom32fb_out_be32(ud, REG_CTRL, ud->reg_ctrl_default);
619 * Fill in the fb_info structure
621 ud->fbinfo->device = dev;
622 ud->fbinfo->screen_base = (void *)ud->fb_aligned;
623 ud->fbinfo->fbops = &ubicom32fb_ops;
624 ud->fbinfo->fix = ubicom32fb_fix;
625 ud->fbinfo->fix.smem_start = (u32)ud->fb_aligned;
626 ud->fbinfo->fix.smem_len = vram_size * 1024;
627 ud->fbinfo->fix.line_length = regs->xres * (regs->bpp / 8);
628 ud->fbinfo->fix.mmio_start = (u32)regs;
629 ud->fbinfo->fix.mmio_len = sizeof(struct vdc_tio_vp_regs);
632 * We support panning in the y direction only
634 ud->fbinfo->fix.xpanstep = 0;
635 ud->fbinfo->fix.ypanstep = 1;
637 ud->fbinfo->pseudo_palette = ud->pseudo_palette;
638 ud->fbinfo->flags = FBINFO_DEFAULT;
639 ud->fbinfo->var = ubicom32fb_var;
640 ud->fbinfo->var.xres = regs->xres;
641 ud->fbinfo->var.yres = regs->yres;
644 * We cannot pan in the X direction, so xres_virtual is regs->xres
645 * We can pan in the Y direction, so yres_virtual is vram_size / ud->fbinfo->fix.line_length
647 ud->fbinfo->var.xres_virtual = regs->xres;
648 ud->fbinfo->var.yres_virtual = (vram_size * 1024) / ud->fbinfo->fix.line_length;
650 //ud->fbinfo->var.height = regs->height_mm;
651 //ud->fbinfo->var.width = regs->width_mm;
654 * Allocate a color map
656 rc = fb_alloc_cmap(&ud->fbinfo->cmap, PALETTE_ENTRIES_NO, 0);
658 dev_err(dev, "Fail to allocate colormap (%d entries)\n",
662 ud->cmap_alloc = true;
665 * Register new frame buffer
667 rc = register_framebuffer(ud->fbinfo);
669 dev_err(dev, "Could not register frame buffer\n");
676 ud->regs->next_frame = ud->fb;
677 ubicom32fb_send_command(ud, VDCTIO_COMMAND_START, 0);
680 * Tell the log we are here
682 dev_info(dev, "fbaddr=%p align=%p, size=%uKB screen(%ux%u) virt(%ux%u), regs=%p irqtx=%u irqrx=%u\n",
683 ud->fb, ud->fb_aligned, vram_size, ud->fbinfo->var.xres, ud->fbinfo->var.yres,
684 ud->fbinfo->var.xres_virtual, ud->fbinfo->var.yres_virtual, ud->regs,
685 irq_resource_tx->start, irq_resource_rx->start);
693 ubicom32fb_release(dev);
698 * ubicom32fb_platform_remove
700 static int ubicom32fb_platform_remove(struct platform_device *pdev)
702 dev_info(&(pdev->dev), "Ubicom32 FB Driver Remove\n");
703 return ubicom32fb_release(&pdev->dev);
706 static struct platform_driver ubicom32fb_platform_driver = {
707 .probe = ubicom32fb_platform_probe,
708 .remove = ubicom32fb_platform_remove,
711 .owner = THIS_MODULE,
718 * Process kernel boot options
720 static int __init ubicom32fb_setup(char *options)
724 if (!options || !*options) {
728 while ((this_opt = strsep(&options, ",")) != NULL) {
733 if (!strncmp(this_opt, "init_value=", 10)) {
734 init_value = simple_strtoul(this_opt + 11, NULL, 0);
738 if (!strncmp(this_opt, "vram_size=", 10)) {
739 vram_size = simple_strtoul(this_opt + 10, NULL, 0);
750 static int __devinit ubicom32fb_init(void)
754 * Get kernel boot options (in 'video=ubicom32fb:<options>')
758 if (fb_get_options(DRIVER_NAME, &option)) {
761 ubicom32fb_setup(option);
764 return platform_driver_register(&ubicom32fb_platform_driver);
766 module_init(ubicom32fb_init);
771 static void __exit ubicom32fb_exit(void)
773 platform_driver_unregister(&ubicom32fb_platform_driver);
775 module_exit(ubicom32fb_exit);
777 MODULE_LICENSE("GPL");
778 MODULE_AUTHOR("Patrick Tjin <@ubicom.com>");
779 MODULE_DESCRIPTION(DRIVER_DESCRIPTION);