blob: ec112c13eb0502fb96af03e1e23f44fe968530c5 [file] [log] [blame]
Stephen Warren26549c82013-05-24 15:55:13 -07001/*
2 * Simplest possible simple frame-buffer driver, as a platform device
3 *
4 * Copyright (c) 2013, Stephen Warren
5 *
6 * Based on q40fb.c, which was:
7 * Copyright (C) 2001 Richard Zidlicky <rz@linux-m68k.org>
8 *
9 * Also based on offb.c, which was:
10 * Copyright (C) 1997 Geert Uytterhoeven
11 * Copyright (C) 1996 Paul Mackerras
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms and conditions of the GNU General Public License,
15 * version 2, as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 */
22
23#include <linux/errno.h>
24#include <linux/fb.h>
25#include <linux/io.h>
26#include <linux/module.h>
David Herrmann5ef76da2013-08-02 14:05:20 +020027#include <linux/platform_data/simplefb.h>
Stephen Warren26549c82013-05-24 15:55:13 -070028#include <linux/platform_device.h>
29
30static struct fb_fix_screeninfo simplefb_fix = {
31 .id = "simple",
32 .type = FB_TYPE_PACKED_PIXELS,
33 .visual = FB_VISUAL_TRUECOLOR,
34 .accel = FB_ACCEL_NONE,
35};
36
37static struct fb_var_screeninfo simplefb_var = {
38 .height = -1,
39 .width = -1,
40 .activate = FB_ACTIVATE_NOW,
41 .vmode = FB_VMODE_NONINTERLACED,
42};
43
Luc Verhaegen1270be42014-11-14 13:26:48 +010044#define PSEUDO_PALETTE_SIZE 16
45
Stephen Warren26549c82013-05-24 15:55:13 -070046static int simplefb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
47 u_int transp, struct fb_info *info)
48{
49 u32 *pal = info->pseudo_palette;
50 u32 cr = red >> (16 - info->var.red.length);
51 u32 cg = green >> (16 - info->var.green.length);
52 u32 cb = blue >> (16 - info->var.blue.length);
53 u32 value;
54
Luc Verhaegen1270be42014-11-14 13:26:48 +010055 if (regno >= PSEUDO_PALETTE_SIZE)
Stephen Warren26549c82013-05-24 15:55:13 -070056 return -EINVAL;
57
58 value = (cr << info->var.red.offset) |
59 (cg << info->var.green.offset) |
60 (cb << info->var.blue.offset);
61 if (info->var.transp.length > 0) {
62 u32 mask = (1 << info->var.transp.length) - 1;
63 mask <<= info->var.transp.offset;
64 value |= mask;
65 }
66 pal[regno] = value;
67
68 return 0;
69}
70
David Herrmann498f6d32013-10-02 16:58:38 +020071static void simplefb_destroy(struct fb_info *info)
72{
73 if (info->screen_base)
74 iounmap(info->screen_base);
75}
76
Stephen Warren26549c82013-05-24 15:55:13 -070077static struct fb_ops simplefb_ops = {
78 .owner = THIS_MODULE,
David Herrmann498f6d32013-10-02 16:58:38 +020079 .fb_destroy = simplefb_destroy,
Stephen Warren26549c82013-05-24 15:55:13 -070080 .fb_setcolreg = simplefb_setcolreg,
81 .fb_fillrect = cfb_fillrect,
82 .fb_copyarea = cfb_copyarea,
83 .fb_imageblit = cfb_imageblit,
84};
85
David Herrmann5ef76da2013-08-02 14:05:20 +020086static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS;
Stephen Warren26549c82013-05-24 15:55:13 -070087
88struct simplefb_params {
89 u32 width;
90 u32 height;
91 u32 stride;
92 struct simplefb_format *format;
93};
94
95static int simplefb_parse_dt(struct platform_device *pdev,
96 struct simplefb_params *params)
97{
98 struct device_node *np = pdev->dev.of_node;
99 int ret;
100 const char *format;
101 int i;
102
103 ret = of_property_read_u32(np, "width", &params->width);
104 if (ret) {
105 dev_err(&pdev->dev, "Can't parse width property\n");
106 return ret;
107 }
108
109 ret = of_property_read_u32(np, "height", &params->height);
110 if (ret) {
111 dev_err(&pdev->dev, "Can't parse height property\n");
112 return ret;
113 }
114
115 ret = of_property_read_u32(np, "stride", &params->stride);
116 if (ret) {
117 dev_err(&pdev->dev, "Can't parse stride property\n");
118 return ret;
119 }
120
121 ret = of_property_read_string(np, "format", &format);
122 if (ret) {
123 dev_err(&pdev->dev, "Can't parse format property\n");
124 return ret;
125 }
126 params->format = NULL;
127 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
128 if (strcmp(format, simplefb_formats[i].name))
129 continue;
130 params->format = &simplefb_formats[i];
131 break;
132 }
133 if (!params->format) {
134 dev_err(&pdev->dev, "Invalid format value\n");
135 return -EINVAL;
136 }
137
138 return 0;
139}
140
David Herrmann5ef76da2013-08-02 14:05:20 +0200141static int simplefb_parse_pd(struct platform_device *pdev,
142 struct simplefb_params *params)
143{
Jingoo Han129f1be2013-09-17 14:11:04 +0900144 struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev);
David Herrmann5ef76da2013-08-02 14:05:20 +0200145 int i;
146
147 params->width = pd->width;
148 params->height = pd->height;
149 params->stride = pd->stride;
150
151 params->format = NULL;
152 for (i = 0; i < ARRAY_SIZE(simplefb_formats); i++) {
153 if (strcmp(pd->format, simplefb_formats[i].name))
154 continue;
155
156 params->format = &simplefb_formats[i];
157 break;
158 }
159
160 if (!params->format) {
161 dev_err(&pdev->dev, "Invalid format value\n");
162 return -EINVAL;
163 }
164
165 return 0;
166}
167
Luc Verhaegen1270be42014-11-14 13:26:48 +0100168struct simplefb_par {
169 u32 palette[PSEUDO_PALETTE_SIZE];
170};
171
Stephen Warren26549c82013-05-24 15:55:13 -0700172static int simplefb_probe(struct platform_device *pdev)
173{
174 int ret;
175 struct simplefb_params params;
176 struct fb_info *info;
Luc Verhaegen1270be42014-11-14 13:26:48 +0100177 struct simplefb_par *par;
Stephen Warren26549c82013-05-24 15:55:13 -0700178 struct resource *mem;
179
180 if (fb_get_options("simplefb", NULL))
181 return -ENODEV;
182
David Herrmann5ef76da2013-08-02 14:05:20 +0200183 ret = -ENODEV;
Jingoo Han129f1be2013-09-17 14:11:04 +0900184 if (dev_get_platdata(&pdev->dev))
David Herrmann5ef76da2013-08-02 14:05:20 +0200185 ret = simplefb_parse_pd(pdev, &params);
186 else if (pdev->dev.of_node)
187 ret = simplefb_parse_dt(pdev, &params);
188
Stephen Warren26549c82013-05-24 15:55:13 -0700189 if (ret)
190 return ret;
191
192 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 if (!mem) {
194 dev_err(&pdev->dev, "No memory resource\n");
195 return -EINVAL;
196 }
197
Luc Verhaegen1270be42014-11-14 13:26:48 +0100198 info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev);
Stephen Warren26549c82013-05-24 15:55:13 -0700199 if (!info)
200 return -ENOMEM;
201 platform_set_drvdata(pdev, info);
202
Luc Verhaegen1270be42014-11-14 13:26:48 +0100203 par = info->par;
204
Stephen Warren26549c82013-05-24 15:55:13 -0700205 info->fix = simplefb_fix;
206 info->fix.smem_start = mem->start;
207 info->fix.smem_len = resource_size(mem);
208 info->fix.line_length = params.stride;
209
210 info->var = simplefb_var;
211 info->var.xres = params.width;
212 info->var.yres = params.height;
213 info->var.xres_virtual = params.width;
214 info->var.yres_virtual = params.height;
215 info->var.bits_per_pixel = params.format->bits_per_pixel;
216 info->var.red = params.format->red;
217 info->var.green = params.format->green;
218 info->var.blue = params.format->blue;
219 info->var.transp = params.format->transp;
220
David Herrmanndf0960a2013-08-02 14:05:21 +0200221 info->apertures = alloc_apertures(1);
222 if (!info->apertures) {
223 framebuffer_release(info);
224 return -ENOMEM;
225 }
226 info->apertures->ranges[0].base = info->fix.smem_start;
227 info->apertures->ranges[0].size = info->fix.smem_len;
228
Stephen Warren26549c82013-05-24 15:55:13 -0700229 info->fbops = &simplefb_ops;
David Herrmanndf0960a2013-08-02 14:05:21 +0200230 info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE;
David Herrmann9e210be2013-10-02 16:58:39 +0200231 info->screen_base = ioremap_wc(info->fix.smem_start,
232 info->fix.smem_len);
Stephen Warren26549c82013-05-24 15:55:13 -0700233 if (!info->screen_base) {
234 framebuffer_release(info);
235 return -ENODEV;
236 }
Luc Verhaegen1270be42014-11-14 13:26:48 +0100237 info->pseudo_palette = par->palette;
Stephen Warren26549c82013-05-24 15:55:13 -0700238
Tom Gundersen9f192a92013-09-07 16:08:35 +0200239 dev_info(&pdev->dev, "framebuffer at 0x%lx, 0x%x bytes, mapped to 0x%p\n",
240 info->fix.smem_start, info->fix.smem_len,
241 info->screen_base);
242 dev_info(&pdev->dev, "format=%s, mode=%dx%dx%d, linelength=%d\n",
243 params.format->name,
244 info->var.xres, info->var.yres,
245 info->var.bits_per_pixel, info->fix.line_length);
246
Stephen Warren26549c82013-05-24 15:55:13 -0700247 ret = register_framebuffer(info);
248 if (ret < 0) {
249 dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret);
David Herrmann498f6d32013-10-02 16:58:38 +0200250 iounmap(info->screen_base);
Stephen Warren26549c82013-05-24 15:55:13 -0700251 framebuffer_release(info);
252 return ret;
253 }
254
255 dev_info(&pdev->dev, "fb%d: simplefb registered!\n", info->node);
256
257 return 0;
258}
259
260static int simplefb_remove(struct platform_device *pdev)
261{
262 struct fb_info *info = platform_get_drvdata(pdev);
263
264 unregister_framebuffer(info);
265 framebuffer_release(info);
266
267 return 0;
268}
269
270static const struct of_device_id simplefb_of_match[] = {
271 { .compatible = "simple-framebuffer", },
272 { },
273};
274MODULE_DEVICE_TABLE(of, simplefb_of_match);
275
276static struct platform_driver simplefb_driver = {
277 .driver = {
278 .name = "simple-framebuffer",
279 .owner = THIS_MODULE,
280 .of_match_table = simplefb_of_match,
281 },
282 .probe = simplefb_probe,
283 .remove = simplefb_remove,
284};
285module_platform_driver(simplefb_driver);
286
287MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>");
288MODULE_DESCRIPTION("Simple framebuffer driver");
289MODULE_LICENSE("GPL v2");