Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 1 | /* |
| 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 Herrmann | 5ef76da | 2013-08-02 14:05:20 +0200 | [diff] [blame] | 27 | #include <linux/platform_data/simplefb.h> |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
| 29 | |
| 30 | static 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 | |
| 37 | static 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 Verhaegen | 1270be4 | 2014-11-14 13:26:48 +0100 | [diff] [blame^] | 44 | #define PSEUDO_PALETTE_SIZE 16 |
| 45 | |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 46 | static 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 Verhaegen | 1270be4 | 2014-11-14 13:26:48 +0100 | [diff] [blame^] | 55 | if (regno >= PSEUDO_PALETTE_SIZE) |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 56 | 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 Herrmann | 498f6d3 | 2013-10-02 16:58:38 +0200 | [diff] [blame] | 71 | static void simplefb_destroy(struct fb_info *info) |
| 72 | { |
| 73 | if (info->screen_base) |
| 74 | iounmap(info->screen_base); |
| 75 | } |
| 76 | |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 77 | static struct fb_ops simplefb_ops = { |
| 78 | .owner = THIS_MODULE, |
David Herrmann | 498f6d3 | 2013-10-02 16:58:38 +0200 | [diff] [blame] | 79 | .fb_destroy = simplefb_destroy, |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 80 | .fb_setcolreg = simplefb_setcolreg, |
| 81 | .fb_fillrect = cfb_fillrect, |
| 82 | .fb_copyarea = cfb_copyarea, |
| 83 | .fb_imageblit = cfb_imageblit, |
| 84 | }; |
| 85 | |
David Herrmann | 5ef76da | 2013-08-02 14:05:20 +0200 | [diff] [blame] | 86 | static struct simplefb_format simplefb_formats[] = SIMPLEFB_FORMATS; |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 87 | |
| 88 | struct simplefb_params { |
| 89 | u32 width; |
| 90 | u32 height; |
| 91 | u32 stride; |
| 92 | struct simplefb_format *format; |
| 93 | }; |
| 94 | |
| 95 | static 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", ¶ms->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", ¶ms->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", ¶ms->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 Herrmann | 5ef76da | 2013-08-02 14:05:20 +0200 | [diff] [blame] | 141 | static int simplefb_parse_pd(struct platform_device *pdev, |
| 142 | struct simplefb_params *params) |
| 143 | { |
Jingoo Han | 129f1be | 2013-09-17 14:11:04 +0900 | [diff] [blame] | 144 | struct simplefb_platform_data *pd = dev_get_platdata(&pdev->dev); |
David Herrmann | 5ef76da | 2013-08-02 14:05:20 +0200 | [diff] [blame] | 145 | 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 Verhaegen | 1270be4 | 2014-11-14 13:26:48 +0100 | [diff] [blame^] | 168 | struct simplefb_par { |
| 169 | u32 palette[PSEUDO_PALETTE_SIZE]; |
| 170 | }; |
| 171 | |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 172 | static int simplefb_probe(struct platform_device *pdev) |
| 173 | { |
| 174 | int ret; |
| 175 | struct simplefb_params params; |
| 176 | struct fb_info *info; |
Luc Verhaegen | 1270be4 | 2014-11-14 13:26:48 +0100 | [diff] [blame^] | 177 | struct simplefb_par *par; |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 178 | struct resource *mem; |
| 179 | |
| 180 | if (fb_get_options("simplefb", NULL)) |
| 181 | return -ENODEV; |
| 182 | |
David Herrmann | 5ef76da | 2013-08-02 14:05:20 +0200 | [diff] [blame] | 183 | ret = -ENODEV; |
Jingoo Han | 129f1be | 2013-09-17 14:11:04 +0900 | [diff] [blame] | 184 | if (dev_get_platdata(&pdev->dev)) |
David Herrmann | 5ef76da | 2013-08-02 14:05:20 +0200 | [diff] [blame] | 185 | ret = simplefb_parse_pd(pdev, ¶ms); |
| 186 | else if (pdev->dev.of_node) |
| 187 | ret = simplefb_parse_dt(pdev, ¶ms); |
| 188 | |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 189 | 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 Verhaegen | 1270be4 | 2014-11-14 13:26:48 +0100 | [diff] [blame^] | 198 | info = framebuffer_alloc(sizeof(struct simplefb_par), &pdev->dev); |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 199 | if (!info) |
| 200 | return -ENOMEM; |
| 201 | platform_set_drvdata(pdev, info); |
| 202 | |
Luc Verhaegen | 1270be4 | 2014-11-14 13:26:48 +0100 | [diff] [blame^] | 203 | par = info->par; |
| 204 | |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 205 | 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 Herrmann | df0960a | 2013-08-02 14:05:21 +0200 | [diff] [blame] | 221 | 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 Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 229 | info->fbops = &simplefb_ops; |
David Herrmann | df0960a | 2013-08-02 14:05:21 +0200 | [diff] [blame] | 230 | info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE; |
David Herrmann | 9e210be | 2013-10-02 16:58:39 +0200 | [diff] [blame] | 231 | info->screen_base = ioremap_wc(info->fix.smem_start, |
| 232 | info->fix.smem_len); |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 233 | if (!info->screen_base) { |
| 234 | framebuffer_release(info); |
| 235 | return -ENODEV; |
| 236 | } |
Luc Verhaegen | 1270be4 | 2014-11-14 13:26:48 +0100 | [diff] [blame^] | 237 | info->pseudo_palette = par->palette; |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 238 | |
Tom Gundersen | 9f192a9 | 2013-09-07 16:08:35 +0200 | [diff] [blame] | 239 | 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 Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 247 | ret = register_framebuffer(info); |
| 248 | if (ret < 0) { |
| 249 | dev_err(&pdev->dev, "Unable to register simplefb: %d\n", ret); |
David Herrmann | 498f6d3 | 2013-10-02 16:58:38 +0200 | [diff] [blame] | 250 | iounmap(info->screen_base); |
Stephen Warren | 26549c8 | 2013-05-24 15:55:13 -0700 | [diff] [blame] | 251 | 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 | |
| 260 | static 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 | |
| 270 | static const struct of_device_id simplefb_of_match[] = { |
| 271 | { .compatible = "simple-framebuffer", }, |
| 272 | { }, |
| 273 | }; |
| 274 | MODULE_DEVICE_TABLE(of, simplefb_of_match); |
| 275 | |
| 276 | static 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 | }; |
| 285 | module_platform_driver(simplefb_driver); |
| 286 | |
| 287 | MODULE_AUTHOR("Stephen Warren <swarren@wwwdotorg.org>"); |
| 288 | MODULE_DESCRIPTION("Simple framebuffer driver"); |
| 289 | MODULE_LICENSE("GPL v2"); |