1 /* sunxvr1000.c: Sun XVR-1000 fb driver for sparc64 systems 2 * 3 * License: GPL 4 * 5 * Copyright (C) 2010 David S. Miller (davem@davemloft.net) 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/fb.h> 10 #include <linux/init.h> 11 #include <linux/of_device.h> 12 13 struct gfb_info { 14 struct fb_info *info; 15 16 char __iomem *fb_base; 17 unsigned long fb_base_phys; 18 19 struct device_node *of_node; 20 21 unsigned int width; 22 unsigned int height; 23 unsigned int depth; 24 unsigned int fb_size; 25 26 u32 pseudo_palette[16]; 27 }; 28 29 static int gfb_get_props(struct gfb_info *gp) 30 { 31 gp->width = of_getintprop_default(gp->of_node, "width", 0); 32 gp->height = of_getintprop_default(gp->of_node, "height", 0); 33 gp->depth = of_getintprop_default(gp->of_node, "depth", 32); 34 35 if (!gp->width || !gp->height) { 36 printk(KERN_ERR "gfb: Critical properties missing for %pOF\n", 37 gp->of_node); 38 return -EINVAL; 39 } 40 41 return 0; 42 } 43 44 static int gfb_setcolreg(unsigned regno, 45 unsigned red, unsigned green, unsigned blue, 46 unsigned transp, struct fb_info *info) 47 { 48 u32 value; 49 50 if (regno < 16) { 51 red >>= 8; 52 green >>= 8; 53 blue >>= 8; 54 55 value = (blue << 16) | (green << 8) | red; 56 ((u32 *)info->pseudo_palette)[regno] = value; 57 } 58 59 return 0; 60 } 61 62 static const struct fb_ops gfb_ops = { 63 .owner = THIS_MODULE, 64 .fb_setcolreg = gfb_setcolreg, 65 .fb_fillrect = cfb_fillrect, 66 .fb_copyarea = cfb_copyarea, 67 .fb_imageblit = cfb_imageblit, 68 }; 69 70 static int gfb_set_fbinfo(struct gfb_info *gp) 71 { 72 struct fb_info *info = gp->info; 73 struct fb_var_screeninfo *var = &info->var; 74 75 info->fbops = &gfb_ops; 76 info->screen_base = gp->fb_base; 77 info->screen_size = gp->fb_size; 78 79 info->pseudo_palette = gp->pseudo_palette; 80 81 /* Fill fix common fields */ 82 strscpy(info->fix.id, "gfb", sizeof(info->fix.id)); 83 info->fix.smem_start = gp->fb_base_phys; 84 info->fix.smem_len = gp->fb_size; 85 info->fix.type = FB_TYPE_PACKED_PIXELS; 86 if (gp->depth == 32 || gp->depth == 24) 87 info->fix.visual = FB_VISUAL_TRUECOLOR; 88 else 89 info->fix.visual = FB_VISUAL_PSEUDOCOLOR; 90 91 var->xres = gp->width; 92 var->yres = gp->height; 93 var->xres_virtual = var->xres; 94 var->yres_virtual = var->yres; 95 var->bits_per_pixel = gp->depth; 96 97 var->red.offset = 0; 98 var->red.length = 8; 99 var->green.offset = 8; 100 var->green.length = 8; 101 var->blue.offset = 16; 102 var->blue.length = 8; 103 var->transp.offset = 0; 104 var->transp.length = 0; 105 106 if (fb_alloc_cmap(&info->cmap, 256, 0)) { 107 printk(KERN_ERR "gfb: Cannot allocate color map.\n"); 108 return -ENOMEM; 109 } 110 111 return 0; 112 } 113 114 static int gfb_probe(struct platform_device *op) 115 { 116 struct device_node *dp = op->dev.of_node; 117 struct fb_info *info; 118 struct gfb_info *gp; 119 int err; 120 121 info = framebuffer_alloc(sizeof(struct gfb_info), &op->dev); 122 if (!info) { 123 err = -ENOMEM; 124 goto err_out; 125 } 126 127 gp = info->par; 128 gp->info = info; 129 gp->of_node = dp; 130 131 gp->fb_base_phys = op->resource[6].start; 132 133 err = gfb_get_props(gp); 134 if (err) 135 goto err_release_fb; 136 137 /* Framebuffer length is the same regardless of resolution. */ 138 info->fix.line_length = 16384; 139 gp->fb_size = info->fix.line_length * gp->height; 140 141 gp->fb_base = of_ioremap(&op->resource[6], 0, 142 gp->fb_size, "gfb fb"); 143 if (!gp->fb_base) { 144 err = -ENOMEM; 145 goto err_release_fb; 146 } 147 148 err = gfb_set_fbinfo(gp); 149 if (err) 150 goto err_unmap_fb; 151 152 printk("gfb: Found device at %pOF\n", dp); 153 154 err = register_framebuffer(info); 155 if (err < 0) { 156 printk(KERN_ERR "gfb: Could not register framebuffer %pOF\n", 157 dp); 158 goto err_unmap_fb; 159 } 160 161 dev_set_drvdata(&op->dev, info); 162 163 return 0; 164 165 err_unmap_fb: 166 of_iounmap(&op->resource[6], gp->fb_base, gp->fb_size); 167 168 err_release_fb: 169 framebuffer_release(info); 170 171 err_out: 172 return err; 173 } 174 175 static const struct of_device_id gfb_match[] = { 176 { 177 .name = "SUNW,gfb", 178 }, 179 {}, 180 }; 181 182 static struct platform_driver gfb_driver = { 183 .probe = gfb_probe, 184 .driver = { 185 .name = "gfb", 186 .of_match_table = gfb_match, 187 .suppress_bind_attrs = true, 188 }, 189 }; 190 191 static int __init gfb_init(void) 192 { 193 if (fb_get_options("gfb", NULL)) 194 return -ENODEV; 195 196 return platform_driver_register(&gfb_driver); 197 } 198 device_initcall(gfb_init); 199