xref: /openbmc/linux/drivers/video/fbdev/sunxvr1000.c (revision aa298b30ce566bb7fe0d5967d3d864cf636d8e4f)
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_DEFAULT_IOMEM_OPS,
65 	.fb_setcolreg		= gfb_setcolreg,
66 };
67 
68 static int gfb_set_fbinfo(struct gfb_info *gp)
69 {
70 	struct fb_info *info = gp->info;
71 	struct fb_var_screeninfo *var = &info->var;
72 
73 	info->fbops = &gfb_ops;
74 	info->screen_base = gp->fb_base;
75 	info->screen_size = gp->fb_size;
76 
77 	info->pseudo_palette = gp->pseudo_palette;
78 
79 	/* Fill fix common fields */
80 	strscpy(info->fix.id, "gfb", sizeof(info->fix.id));
81         info->fix.smem_start = gp->fb_base_phys;
82         info->fix.smem_len = gp->fb_size;
83         info->fix.type = FB_TYPE_PACKED_PIXELS;
84 	if (gp->depth == 32 || gp->depth == 24)
85 		info->fix.visual = FB_VISUAL_TRUECOLOR;
86 	else
87 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
88 
89 	var->xres = gp->width;
90 	var->yres = gp->height;
91 	var->xres_virtual = var->xres;
92 	var->yres_virtual = var->yres;
93 	var->bits_per_pixel = gp->depth;
94 
95 	var->red.offset = 0;
96 	var->red.length = 8;
97 	var->green.offset = 8;
98 	var->green.length = 8;
99 	var->blue.offset = 16;
100 	var->blue.length = 8;
101 	var->transp.offset = 0;
102 	var->transp.length = 0;
103 
104 	if (fb_alloc_cmap(&info->cmap, 256, 0)) {
105 		printk(KERN_ERR "gfb: Cannot allocate color map.\n");
106 		return -ENOMEM;
107 	}
108 
109         return 0;
110 }
111 
112 static int gfb_probe(struct platform_device *op)
113 {
114 	struct device_node *dp = op->dev.of_node;
115 	struct fb_info *info;
116 	struct gfb_info *gp;
117 	int err;
118 
119 	info = framebuffer_alloc(sizeof(struct gfb_info), &op->dev);
120 	if (!info) {
121 		err = -ENOMEM;
122 		goto err_out;
123 	}
124 
125 	gp = info->par;
126 	gp->info = info;
127 	gp->of_node = dp;
128 
129 	gp->fb_base_phys = op->resource[6].start;
130 
131 	err = gfb_get_props(gp);
132 	if (err)
133 		goto err_release_fb;
134 
135 	/* Framebuffer length is the same regardless of resolution. */
136 	info->fix.line_length = 16384;
137 	gp->fb_size = info->fix.line_length * gp->height;
138 
139 	gp->fb_base = of_ioremap(&op->resource[6], 0,
140 				 gp->fb_size, "gfb fb");
141 	if (!gp->fb_base) {
142 		err = -ENOMEM;
143 		goto err_release_fb;
144 	}
145 
146 	err = gfb_set_fbinfo(gp);
147 	if (err)
148 		goto err_unmap_fb;
149 
150 	printk("gfb: Found device at %pOF\n", dp);
151 
152 	err = register_framebuffer(info);
153 	if (err < 0) {
154 		printk(KERN_ERR "gfb: Could not register framebuffer %pOF\n",
155 		       dp);
156 		goto err_unmap_fb;
157 	}
158 
159 	dev_set_drvdata(&op->dev, info);
160 
161 	return 0;
162 
163 err_unmap_fb:
164 	of_iounmap(&op->resource[6], gp->fb_base, gp->fb_size);
165 
166 err_release_fb:
167         framebuffer_release(info);
168 
169 err_out:
170 	return err;
171 }
172 
173 static const struct of_device_id gfb_match[] = {
174 	{
175 		.name = "SUNW,gfb",
176 	},
177 	{},
178 };
179 
180 static struct platform_driver gfb_driver = {
181 	.probe		= gfb_probe,
182 	.driver = {
183 		.name			= "gfb",
184 		.of_match_table		= gfb_match,
185 		.suppress_bind_attrs	= true,
186 	},
187 };
188 
189 static int __init gfb_init(void)
190 {
191 	if (fb_get_options("gfb", NULL))
192 		return -ENODEV;
193 
194 	return platform_driver_register(&gfb_driver);
195 }
196 device_initcall(gfb_init);
197