2 * Raspberry Pi Sense HAT framebuffer driver
3 * http://raspberrypi.org
5 * Copyright (C) 2015 Raspberry Pi
7 * Author: Serge Schneider
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <linux/delay.h>
25 #include <linux/init.h>
27 #include <linux/mfd/rpisense/framebuffer.h>
28 #include <linux/mfd/rpisense/core.h>
31 module_param(lowlight, bool, 0);
32 MODULE_PARM_DESC(lowlight, "Reduce LED matrix brightness to one third");
34 static struct rpisense *rpisense;
36 struct rpisense_fb_param {
43 static u8 gamma_default[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
44 0x02, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x07,
45 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0E, 0x0F, 0x11,
46 0x12, 0x14, 0x15, 0x17, 0x19, 0x1B, 0x1D, 0x1F,};
48 static u8 gamma_low[32] = {0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
49 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
50 0x03, 0x03, 0x03, 0x04, 0x04, 0x05, 0x05, 0x06,
51 0x06, 0x07, 0x07, 0x08, 0x08, 0x09, 0x0A, 0x0A,};
53 static u8 gamma_user[32];
55 static u32 pseudo_palette[16];
57 static struct rpisense_fb_param rpisense_fb_param = {
60 .gamma = gamma_default,
63 static struct fb_deferred_io rpisense_fb_defio;
65 static struct fb_fix_screeninfo rpisense_fb_fix = {
67 .type = FB_TYPE_PACKED_PIXELS,
68 .visual = FB_VISUAL_TRUECOLOR,
72 .accel = FB_ACCEL_NONE,
76 static struct fb_var_screeninfo rpisense_fb_var = {
87 static ssize_t rpisense_fb_write(struct fb_info *info,
88 const char __user *buf, size_t count,
91 ssize_t res = fb_sys_write(info, buf, count, ppos);
93 schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
97 static void rpisense_fb_fillrect(struct fb_info *info,
98 const struct fb_fillrect *rect)
100 sys_fillrect(info, rect);
101 schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
104 static void rpisense_fb_copyarea(struct fb_info *info,
105 const struct fb_copyarea *area)
107 sys_copyarea(info, area);
108 schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
111 static void rpisense_fb_imageblit(struct fb_info *info,
112 const struct fb_image *image)
114 sys_imageblit(info, image);
115 schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
118 static void rpisense_fb_deferred_io(struct fb_info *info,
119 struct list_head *pagelist)
123 u8 *vmem_work = rpisense_fb_param.vmem_work;
124 u16 *mem = (u16 *)rpisense_fb_param.vmem;
125 u8 *gamma = rpisense_fb_param.gamma;
128 for (j = 0; j < 8; j++) {
129 for (i = 0; i < 8; i++) {
130 vmem_work[(j * 24) + i + 1] =
131 gamma[(mem[(j * 8) + i] >> 11) & 0x1F];
132 vmem_work[(j * 24) + (i + 8) + 1] =
133 gamma[(mem[(j * 8) + i] >> 6) & 0x1F];
134 vmem_work[(j * 24) + (i + 16) + 1] =
135 gamma[(mem[(j * 8) + i]) & 0x1F];
138 rpisense_block_write(rpisense, vmem_work, 193);
141 static struct fb_deferred_io rpisense_fb_defio = {
143 .deferred_io = rpisense_fb_deferred_io,
146 static int rpisense_fb_ioctl(struct fb_info *info, unsigned int cmd,
150 case SENSEFB_FBIOGET_GAMMA:
151 if (copy_to_user((void __user *) arg, rpisense_fb_param.gamma,
155 case SENSEFB_FBIOSET_GAMMA:
156 if (copy_from_user(gamma_user, (void __user *)arg,
159 rpisense_fb_param.gamma = gamma_user;
160 schedule_delayed_work(&info->deferred_work,
161 rpisense_fb_defio.delay);
163 case SENSEFB_FBIORESET_GAMMA:
166 rpisense_fb_param.gamma = gamma_default;
169 rpisense_fb_param.gamma = gamma_low;
172 rpisense_fb_param.gamma = gamma_user;
177 schedule_delayed_work(&info->deferred_work,
178 rpisense_fb_defio.delay);
186 static struct fb_ops rpisense_fb_ops = {
187 .owner = THIS_MODULE,
188 .fb_read = fb_sys_read,
189 .fb_write = rpisense_fb_write,
190 .fb_fillrect = rpisense_fb_fillrect,
191 .fb_copyarea = rpisense_fb_copyarea,
192 .fb_imageblit = rpisense_fb_imageblit,
193 .fb_ioctl = rpisense_fb_ioctl,
196 static int rpisense_fb_probe(struct platform_device *pdev)
198 struct fb_info *info;
200 struct rpisense_fb *rpisense_fb;
202 rpisense = rpisense_get_dev();
203 rpisense_fb = &rpisense->framebuffer;
205 rpisense_fb_param.vmem = vzalloc(rpisense_fb_param.vmemsize);
206 if (!rpisense_fb_param.vmem)
209 rpisense_fb_param.vmem_work = devm_kmalloc(&pdev->dev, 193, GFP_KERNEL);
210 if (!rpisense_fb_param.vmem_work)
213 info = framebuffer_alloc(0, &pdev->dev);
215 dev_err(&pdev->dev, "Could not allocate framebuffer.\n");
218 rpisense_fb->info = info;
220 rpisense_fb_fix.smem_start = (unsigned long)rpisense_fb_param.vmem;
221 rpisense_fb_fix.smem_len = rpisense_fb_param.vmemsize;
223 info->fbops = &rpisense_fb_ops;
224 info->fix = rpisense_fb_fix;
225 info->var = rpisense_fb_var;
226 info->fbdefio = &rpisense_fb_defio;
227 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
228 info->screen_base = rpisense_fb_param.vmem;
229 info->screen_size = rpisense_fb_param.vmemsize;
230 info->pseudo_palette = pseudo_palette;
233 rpisense_fb_param.gamma = gamma_low;
235 fb_deferred_io_init(info);
237 ret = register_framebuffer(info);
239 dev_err(&pdev->dev, "Could not register framebuffer.\n");
243 fb_info(info, "%s frame buffer device\n", info->fix.id);
244 schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
247 framebuffer_release(info);
249 vfree(rpisense_fb_param.vmem);
253 static int rpisense_fb_remove(struct platform_device *pdev)
255 struct rpisense_fb *rpisense_fb = &rpisense->framebuffer;
256 struct fb_info *info = rpisense_fb->info;
259 unregister_framebuffer(info);
260 fb_deferred_io_cleanup(info);
261 framebuffer_release(info);
262 vfree(rpisense_fb_param.vmem);
269 static const struct of_device_id rpisense_fb_id[] = {
270 { .compatible = "rpi,rpi-sense-fb" },
273 MODULE_DEVICE_TABLE(of, rpisense_fb_id);
276 static struct platform_device_id rpisense_fb_device_id[] = {
277 { .name = "rpi-sense-fb" },
280 MODULE_DEVICE_TABLE(platform, rpisense_fb_device_id);
282 static struct platform_driver rpisense_fb_driver = {
283 .probe = rpisense_fb_probe,
284 .remove = rpisense_fb_remove,
286 .name = "rpi-sense-fb",
287 .owner = THIS_MODULE,
291 module_platform_driver(rpisense_fb_driver);
293 MODULE_DESCRIPTION("Raspberry Pi Sense HAT framebuffer driver");
294 MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
295 MODULE_LICENSE("GPL");