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