bcm27xx: add linux 5.4 support
[openwrt/staging/jogo.git] / target / linux / bcm27xx / patches-5.4 / 950-0058-mfd-Add-Raspberry-Pi-Sense-HAT-core-driver.patch
1 From 34a1607afd3225d589ee0451c6c16b790e898cbc Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <pelwell@users.noreply.github.com>
3 Date: Tue, 14 Jul 2015 14:32:47 +0100
4 Subject: [PATCH] mfd: Add Raspberry Pi Sense HAT core driver
5
6 mfd: Add rpi_sense_core of compatible string
7 ---
8 drivers/input/joystick/Kconfig | 8 +
9 drivers/input/joystick/Makefile | 2 +-
10 drivers/input/joystick/rpisense-js.c | 153 ++++++++++++
11 drivers/mfd/Kconfig | 8 +
12 drivers/mfd/Makefile | 1 +
13 drivers/mfd/rpisense-core.c | 165 +++++++++++++
14 drivers/video/fbdev/Kconfig | 13 +
15 drivers/video/fbdev/Makefile | 1 +
16 drivers/video/fbdev/rpisense-fb.c | 293 +++++++++++++++++++++++
17 include/linux/mfd/rpisense/core.h | 47 ++++
18 include/linux/mfd/rpisense/framebuffer.h | 32 +++
19 include/linux/mfd/rpisense/joystick.h | 35 +++
20 12 files changed, 757 insertions(+), 1 deletion(-)
21 create mode 100644 drivers/input/joystick/rpisense-js.c
22 create mode 100644 drivers/mfd/rpisense-core.c
23 create mode 100644 drivers/video/fbdev/rpisense-fb.c
24 create mode 100644 include/linux/mfd/rpisense/core.h
25 create mode 100644 include/linux/mfd/rpisense/framebuffer.h
26 create mode 100644 include/linux/mfd/rpisense/joystick.h
27
28 --- a/drivers/input/joystick/Kconfig
29 +++ b/drivers/input/joystick/Kconfig
30 @@ -372,4 +372,12 @@ config JOYSTICK_FSIA6B
31 To compile this driver as a module, choose M here: the
32 module will be called fsia6b.
33
34 +config JOYSTICK_RPISENSE
35 + tristate "Raspberry Pi Sense HAT joystick"
36 + depends on GPIOLIB && INPUT
37 + select MFD_RPISENSE_CORE
38 +
39 + help
40 + This is the joystick driver for the Raspberry Pi Sense HAT
41 +
42 endif
43 --- a/drivers/input/joystick/Makefile
44 +++ b/drivers/input/joystick/Makefile
45 @@ -36,4 +36,4 @@ obj-$(CONFIG_JOYSTICK_WARRIOR) += warri
46 obj-$(CONFIG_JOYSTICK_WALKERA0701) += walkera0701.o
47 obj-$(CONFIG_JOYSTICK_XPAD) += xpad.o
48 obj-$(CONFIG_JOYSTICK_ZHENHUA) += zhenhua.o
49 -
50 +obj-$(CONFIG_JOYSTICK_RPISENSE) += rpisense-js.o
51 --- /dev/null
52 +++ b/drivers/input/joystick/rpisense-js.c
53 @@ -0,0 +1,153 @@
54 +/*
55 + * Raspberry Pi Sense HAT joystick driver
56 + * http://raspberrypi.org
57 + *
58 + * Copyright (C) 2015 Raspberry Pi
59 + *
60 + * Author: Serge Schneider
61 + *
62 + * This program is free software; you can redistribute it and/or modify it
63 + * under the terms of the GNU General Public License as published by the
64 + * Free Software Foundation; either version 2 of the License, or (at your
65 + * option) any later version.
66 + *
67 + */
68 +
69 +#include <linux/module.h>
70 +
71 +#include <linux/mfd/rpisense/joystick.h>
72 +#include <linux/mfd/rpisense/core.h>
73 +
74 +static struct rpisense *rpisense;
75 +static unsigned char keymap[5] = {KEY_DOWN, KEY_RIGHT, KEY_UP, KEY_ENTER, KEY_LEFT,};
76 +
77 +static void keys_work_fn(struct work_struct *work)
78 +{
79 + int i;
80 + static s32 prev_keys;
81 + struct rpisense_js *rpisense_js = &rpisense->joystick;
82 + s32 keys = rpisense_reg_read(rpisense, RPISENSE_KEYS);
83 + s32 changes = keys ^ prev_keys;
84 +
85 + prev_keys = keys;
86 + for (i = 0; i < 5; i++) {
87 + if (changes & 1) {
88 + input_report_key(rpisense_js->keys_dev,
89 + keymap[i], keys & 1);
90 + }
91 + changes >>= 1;
92 + keys >>= 1;
93 + }
94 + input_sync(rpisense_js->keys_dev);
95 +}
96 +
97 +static irqreturn_t keys_irq_handler(int irq, void *pdev)
98 +{
99 + struct rpisense_js *rpisense_js = &rpisense->joystick;
100 +
101 + schedule_work(&rpisense_js->keys_work_s);
102 + return IRQ_HANDLED;
103 +}
104 +
105 +static int rpisense_js_probe(struct platform_device *pdev)
106 +{
107 + int ret;
108 + int i;
109 + struct rpisense_js *rpisense_js;
110 +
111 + rpisense = rpisense_get_dev();
112 + rpisense_js = &rpisense->joystick;
113 +
114 + INIT_WORK(&rpisense_js->keys_work_s, keys_work_fn);
115 +
116 + rpisense_js->keys_dev = input_allocate_device();
117 + if (!rpisense_js->keys_dev) {
118 + dev_err(&pdev->dev, "Could not allocate input device.\n");
119 + return -ENOMEM;
120 + }
121 +
122 + rpisense_js->keys_dev->evbit[0] = BIT_MASK(EV_KEY);
123 + for (i = 0; i < ARRAY_SIZE(keymap); i++) {
124 + set_bit(keymap[i],
125 + rpisense_js->keys_dev->keybit);
126 + }
127 +
128 + rpisense_js->keys_dev->name = "Raspberry Pi Sense HAT Joystick";
129 + rpisense_js->keys_dev->phys = "rpi-sense-joy/input0";
130 + rpisense_js->keys_dev->id.bustype = BUS_I2C;
131 + rpisense_js->keys_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
132 + rpisense_js->keys_dev->keycode = keymap;
133 + rpisense_js->keys_dev->keycodesize = sizeof(unsigned char);
134 + rpisense_js->keys_dev->keycodemax = ARRAY_SIZE(keymap);
135 +
136 + ret = input_register_device(rpisense_js->keys_dev);
137 + if (ret) {
138 + dev_err(&pdev->dev, "Could not register input device.\n");
139 + goto err_keys_alloc;
140 + }
141 +
142 + ret = gpiod_direction_input(rpisense_js->keys_desc);
143 + if (ret) {
144 + dev_err(&pdev->dev, "Could not set keys-int direction.\n");
145 + goto err_keys_reg;
146 + }
147 +
148 + rpisense_js->keys_irq = gpiod_to_irq(rpisense_js->keys_desc);
149 + if (rpisense_js->keys_irq < 0) {
150 + dev_err(&pdev->dev, "Could not determine keys-int IRQ.\n");
151 + ret = rpisense_js->keys_irq;
152 + goto err_keys_reg;
153 + }
154 +
155 + ret = devm_request_irq(&pdev->dev, rpisense_js->keys_irq,
156 + keys_irq_handler, IRQF_TRIGGER_RISING,
157 + "keys", &pdev->dev);
158 + if (ret) {
159 + dev_err(&pdev->dev, "IRQ request failed.\n");
160 + goto err_keys_reg;
161 + }
162 + return 0;
163 +err_keys_reg:
164 + input_unregister_device(rpisense_js->keys_dev);
165 +err_keys_alloc:
166 + input_free_device(rpisense_js->keys_dev);
167 + return ret;
168 +}
169 +
170 +static int rpisense_js_remove(struct platform_device *pdev)
171 +{
172 + struct rpisense_js *rpisense_js = &rpisense->joystick;
173 +
174 + input_unregister_device(rpisense_js->keys_dev);
175 + input_free_device(rpisense_js->keys_dev);
176 + return 0;
177 +}
178 +
179 +#ifdef CONFIG_OF
180 +static const struct of_device_id rpisense_js_id[] = {
181 + { .compatible = "rpi,rpi-sense-js" },
182 + { },
183 +};
184 +MODULE_DEVICE_TABLE(of, rpisense_js_id);
185 +#endif
186 +
187 +static struct platform_device_id rpisense_js_device_id[] = {
188 + { .name = "rpi-sense-js" },
189 + { },
190 +};
191 +MODULE_DEVICE_TABLE(platform, rpisense_js_device_id);
192 +
193 +static struct platform_driver rpisense_js_driver = {
194 + .probe = rpisense_js_probe,
195 + .remove = rpisense_js_remove,
196 + .driver = {
197 + .name = "rpi-sense-js",
198 + .owner = THIS_MODULE,
199 + },
200 +};
201 +
202 +module_platform_driver(rpisense_js_driver);
203 +
204 +MODULE_DESCRIPTION("Raspberry Pi Sense HAT joystick driver");
205 +MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
206 +MODULE_LICENSE("GPL");
207 --- a/drivers/mfd/Kconfig
208 +++ b/drivers/mfd/Kconfig
209 @@ -11,6 +11,14 @@ config MFD_CORE
210 select IRQ_DOMAIN
211 default n
212
213 +config MFD_RPISENSE_CORE
214 + tristate "Raspberry Pi Sense HAT core functions"
215 + depends on I2C
216 + select MFD_CORE
217 + help
218 + This is the core driver for the Raspberry Pi Sense HAT. This provides
219 + the necessary functions to communicate with the hardware.
220 +
221 config MFD_CS5535
222 tristate "AMD CS5535 and CS5536 southbridge core functions"
223 select MFD_CORE
224 --- a/drivers/mfd/Makefile
225 +++ b/drivers/mfd/Makefile
226 @@ -255,4 +255,5 @@ obj-$(CONFIG_RAVE_SP_CORE) += rave-sp.o
227 obj-$(CONFIG_MFD_ROHM_BD70528) += rohm-bd70528.o
228 obj-$(CONFIG_MFD_ROHM_BD718XX) += rohm-bd718x7.o
229 obj-$(CONFIG_MFD_STMFX) += stmfx.o
230 +obj-$(CONFIG_MFD_RPISENSE_CORE) += rpisense-core.o
231
232 --- /dev/null
233 +++ b/drivers/mfd/rpisense-core.c
234 @@ -0,0 +1,165 @@
235 +/*
236 + * Raspberry Pi Sense HAT core driver
237 + * http://raspberrypi.org
238 + *
239 + * Copyright (C) 2015 Raspberry Pi
240 + *
241 + * Author: Serge Schneider
242 + *
243 + * This program is free software; you can redistribute it and/or modify it
244 + * under the terms of the GNU General Public License as published by the
245 + * Free Software Foundation; either version 2 of the License, or (at your
246 + * option) any later version.
247 + *
248 + * This driver is based on wm8350 implementation.
249 + */
250 +
251 +#include <linux/module.h>
252 +#include <linux/moduleparam.h>
253 +#include <linux/err.h>
254 +#include <linux/init.h>
255 +#include <linux/i2c.h>
256 +#include <linux/platform_device.h>
257 +#include <linux/mfd/rpisense/core.h>
258 +#include <linux/slab.h>
259 +
260 +static struct rpisense *rpisense;
261 +
262 +static void rpisense_client_dev_register(struct rpisense *rpisense,
263 + const char *name,
264 + struct platform_device **pdev)
265 +{
266 + int ret;
267 +
268 + *pdev = platform_device_alloc(name, -1);
269 + if (*pdev == NULL) {
270 + dev_err(rpisense->dev, "Failed to allocate %s\n", name);
271 + return;
272 + }
273 +
274 + (*pdev)->dev.parent = rpisense->dev;
275 + platform_set_drvdata(*pdev, rpisense);
276 + ret = platform_device_add(*pdev);
277 + if (ret != 0) {
278 + dev_err(rpisense->dev, "Failed to register %s: %d\n",
279 + name, ret);
280 + platform_device_put(*pdev);
281 + *pdev = NULL;
282 + }
283 +}
284 +
285 +static int rpisense_probe(struct i2c_client *i2c,
286 + const struct i2c_device_id *id)
287 +{
288 + int ret;
289 + struct rpisense_js *rpisense_js;
290 +
291 + rpisense = devm_kzalloc(&i2c->dev, sizeof(struct rpisense), GFP_KERNEL);
292 + if (rpisense == NULL)
293 + return -ENOMEM;
294 +
295 + i2c_set_clientdata(i2c, rpisense);
296 + rpisense->dev = &i2c->dev;
297 + rpisense->i2c_client = i2c;
298 +
299 + ret = rpisense_reg_read(rpisense, RPISENSE_WAI);
300 + if (ret > 0) {
301 + if (ret != 's')
302 + return -EINVAL;
303 + } else {
304 + return ret;
305 + }
306 + ret = rpisense_reg_read(rpisense, RPISENSE_VER);
307 + if (ret < 0)
308 + return ret;
309 +
310 + dev_info(rpisense->dev,
311 + "Raspberry Pi Sense HAT firmware version %i\n", ret);
312 +
313 + rpisense_js = &rpisense->joystick;
314 + rpisense_js->keys_desc = devm_gpiod_get(&i2c->dev,
315 + "keys-int", GPIOD_IN);
316 + if (IS_ERR(rpisense_js->keys_desc)) {
317 + dev_warn(&i2c->dev, "Failed to get keys-int descriptor.\n");
318 + rpisense_js->keys_desc = gpio_to_desc(23);
319 + if (rpisense_js->keys_desc == NULL) {
320 + dev_err(&i2c->dev, "GPIO23 fallback failed.\n");
321 + return PTR_ERR(rpisense_js->keys_desc);
322 + }
323 + }
324 + rpisense_client_dev_register(rpisense, "rpi-sense-js",
325 + &(rpisense->joystick.pdev));
326 + rpisense_client_dev_register(rpisense, "rpi-sense-fb",
327 + &(rpisense->framebuffer.pdev));
328 +
329 + return 0;
330 +}
331 +
332 +static int rpisense_remove(struct i2c_client *i2c)
333 +{
334 + struct rpisense *rpisense = i2c_get_clientdata(i2c);
335 +
336 + platform_device_unregister(rpisense->joystick.pdev);
337 + return 0;
338 +}
339 +
340 +struct rpisense *rpisense_get_dev(void)
341 +{
342 + return rpisense;
343 +}
344 +EXPORT_SYMBOL_GPL(rpisense_get_dev);
345 +
346 +s32 rpisense_reg_read(struct rpisense *rpisense, int reg)
347 +{
348 + int ret = i2c_smbus_read_byte_data(rpisense->i2c_client, reg);
349 +
350 + if (ret < 0)
351 + dev_err(rpisense->dev, "Read from reg %d failed\n", reg);
352 + /* Due to the BCM270x I2C clock stretching bug, some values
353 + * may have MSB set. Clear it to avoid incorrect values.
354 + * */
355 + return ret & 0x7F;
356 +}
357 +EXPORT_SYMBOL_GPL(rpisense_reg_read);
358 +
359 +int rpisense_block_write(struct rpisense *rpisense, const char *buf, int count)
360 +{
361 + int ret = i2c_master_send(rpisense->i2c_client, buf, count);
362 +
363 + if (ret < 0)
364 + dev_err(rpisense->dev, "Block write failed\n");
365 + return ret;
366 +}
367 +EXPORT_SYMBOL_GPL(rpisense_block_write);
368 +
369 +static const struct i2c_device_id rpisense_i2c_id[] = {
370 + { "rpi-sense", 0 },
371 + { }
372 +};
373 +MODULE_DEVICE_TABLE(i2c, rpisense_i2c_id);
374 +
375 +#ifdef CONFIG_OF
376 +static const struct of_device_id rpisense_core_id[] = {
377 + { .compatible = "rpi,rpi-sense" },
378 + { },
379 +};
380 +MODULE_DEVICE_TABLE(of, rpisense_core_id);
381 +#endif
382 +
383 +
384 +static struct i2c_driver rpisense_driver = {
385 + .driver = {
386 + .name = "rpi-sense",
387 + .owner = THIS_MODULE,
388 + },
389 + .probe = rpisense_probe,
390 + .remove = rpisense_remove,
391 + .id_table = rpisense_i2c_id,
392 +};
393 +
394 +module_i2c_driver(rpisense_driver);
395 +
396 +MODULE_DESCRIPTION("Raspberry Pi Sense HAT core driver");
397 +MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
398 +MODULE_LICENSE("GPL");
399 +
400 --- a/drivers/video/fbdev/Kconfig
401 +++ b/drivers/video/fbdev/Kconfig
402 @@ -2277,6 +2277,19 @@ config FB_SM712
403 called sm712fb. If you want to compile it as a module, say M
404 here and read <file:Documentation/kbuild/modules.rst>.
405
406 +config FB_RPISENSE
407 + tristate "Raspberry Pi Sense HAT framebuffer"
408 + depends on FB
409 + select MFD_RPISENSE_CORE
410 + select FB_SYS_FOPS
411 + select FB_SYS_FILLRECT
412 + select FB_SYS_COPYAREA
413 + select FB_SYS_IMAGEBLIT
414 + select FB_DEFERRED_IO
415 +
416 + help
417 + This is the framebuffer driver for the Raspberry Pi Sense HAT
418 +
419 source "drivers/video/fbdev/omap/Kconfig"
420 source "drivers/video/fbdev/omap2/Kconfig"
421 source "drivers/video/fbdev/mmp/Kconfig"
422 --- a/drivers/video/fbdev/Makefile
423 +++ b/drivers/video/fbdev/Makefile
424 @@ -132,6 +132,7 @@ obj-$(CONFIG_FB_MX3) += mx3fb.o
425 obj-$(CONFIG_FB_DA8XX) += da8xx-fb.o
426 obj-$(CONFIG_FB_SSD1307) += ssd1307fb.o
427 obj-$(CONFIG_FB_SIMPLE) += simplefb.o
428 +obj-$(CONFIG_FB_RPISENSE) += rpisense-fb.o
429
430 # the test framebuffer is last
431 obj-$(CONFIG_FB_VIRTUAL) += vfb.o
432 --- /dev/null
433 +++ b/drivers/video/fbdev/rpisense-fb.c
434 @@ -0,0 +1,293 @@
435 +/*
436 + * Raspberry Pi Sense HAT framebuffer driver
437 + * http://raspberrypi.org
438 + *
439 + * Copyright (C) 2015 Raspberry Pi
440 + *
441 + * Author: Serge Schneider
442 + *
443 + * This program is free software; you can redistribute it and/or modify it
444 + * under the terms of the GNU General Public License as published by the
445 + * Free Software Foundation; either version 2 of the License, or (at your
446 + * option) any later version.
447 + *
448 + */
449 +
450 +#include <linux/module.h>
451 +#include <linux/kernel.h>
452 +#include <linux/errno.h>
453 +#include <linux/string.h>
454 +#include <linux/mm.h>
455 +#include <linux/slab.h>
456 +#include <linux/uaccess.h>
457 +#include <linux/delay.h>
458 +#include <linux/fb.h>
459 +#include <linux/init.h>
460 +
461 +#include <linux/mfd/rpisense/framebuffer.h>
462 +#include <linux/mfd/rpisense/core.h>
463 +
464 +static bool lowlight;
465 +module_param(lowlight, bool, 0);
466 +MODULE_PARM_DESC(lowlight, "Reduce LED matrix brightness to one third");
467 +
468 +static struct rpisense *rpisense;
469 +
470 +struct rpisense_fb_param {
471 + char __iomem *vmem;
472 + u8 *vmem_work;
473 + u32 vmemsize;
474 + u8 *gamma;
475 +};
476 +
477 +static u8 gamma_default[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
478 + 0x02, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x07,
479 + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0E, 0x0F, 0x11,
480 + 0x12, 0x14, 0x15, 0x17, 0x19, 0x1B, 0x1D, 0x1F,};
481 +
482 +static u8 gamma_low[32] = {0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
483 + 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
484 + 0x03, 0x03, 0x03, 0x04, 0x04, 0x05, 0x05, 0x06,
485 + 0x06, 0x07, 0x07, 0x08, 0x08, 0x09, 0x0A, 0x0A,};
486 +
487 +static u8 gamma_user[32];
488 +
489 +static struct rpisense_fb_param rpisense_fb_param = {
490 + .vmem = NULL,
491 + .vmemsize = 128,
492 + .gamma = gamma_default,
493 +};
494 +
495 +static struct fb_deferred_io rpisense_fb_defio;
496 +
497 +static struct fb_fix_screeninfo rpisense_fb_fix = {
498 + .id = "RPi-Sense FB",
499 + .type = FB_TYPE_PACKED_PIXELS,
500 + .visual = FB_VISUAL_TRUECOLOR,
501 + .xpanstep = 0,
502 + .ypanstep = 0,
503 + .ywrapstep = 0,
504 + .accel = FB_ACCEL_NONE,
505 + .line_length = 16,
506 +};
507 +
508 +static struct fb_var_screeninfo rpisense_fb_var = {
509 + .xres = 8,
510 + .yres = 8,
511 + .xres_virtual = 8,
512 + .yres_virtual = 8,
513 + .bits_per_pixel = 16,
514 + .red = {11, 5, 0},
515 + .green = {5, 6, 0},
516 + .blue = {0, 5, 0},
517 +};
518 +
519 +static ssize_t rpisense_fb_write(struct fb_info *info,
520 + const char __user *buf, size_t count,
521 + loff_t *ppos)
522 +{
523 + ssize_t res = fb_sys_write(info, buf, count, ppos);
524 +
525 + schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
526 + return res;
527 +}
528 +
529 +static void rpisense_fb_fillrect(struct fb_info *info,
530 + const struct fb_fillrect *rect)
531 +{
532 + sys_fillrect(info, rect);
533 + schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
534 +}
535 +
536 +static void rpisense_fb_copyarea(struct fb_info *info,
537 + const struct fb_copyarea *area)
538 +{
539 + sys_copyarea(info, area);
540 + schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
541 +}
542 +
543 +static void rpisense_fb_imageblit(struct fb_info *info,
544 + const struct fb_image *image)
545 +{
546 + sys_imageblit(info, image);
547 + schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
548 +}
549 +
550 +static void rpisense_fb_deferred_io(struct fb_info *info,
551 + struct list_head *pagelist)
552 +{
553 + int i;
554 + int j;
555 + u8 *vmem_work = rpisense_fb_param.vmem_work;
556 + u16 *mem = (u16 *)rpisense_fb_param.vmem;
557 + u8 *gamma = rpisense_fb_param.gamma;
558 +
559 + vmem_work[0] = 0;
560 + for (j = 0; j < 8; j++) {
561 + for (i = 0; i < 8; i++) {
562 + vmem_work[(j * 24) + i + 1] =
563 + gamma[(mem[(j * 8) + i] >> 11) & 0x1F];
564 + vmem_work[(j * 24) + (i + 8) + 1] =
565 + gamma[(mem[(j * 8) + i] >> 6) & 0x1F];
566 + vmem_work[(j * 24) + (i + 16) + 1] =
567 + gamma[(mem[(j * 8) + i]) & 0x1F];
568 + }
569 + }
570 + rpisense_block_write(rpisense, vmem_work, 193);
571 +}
572 +
573 +static struct fb_deferred_io rpisense_fb_defio = {
574 + .delay = HZ/100,
575 + .deferred_io = rpisense_fb_deferred_io,
576 +};
577 +
578 +static int rpisense_fb_ioctl(struct fb_info *info, unsigned int cmd,
579 + unsigned long arg)
580 +{
581 + switch (cmd) {
582 + case SENSEFB_FBIOGET_GAMMA:
583 + if (copy_to_user((void __user *) arg, rpisense_fb_param.gamma,
584 + sizeof(u8[32])))
585 + return -EFAULT;
586 + return 0;
587 + case SENSEFB_FBIOSET_GAMMA:
588 + if (copy_from_user(gamma_user, (void __user *)arg,
589 + sizeof(u8[32])))
590 + return -EFAULT;
591 + rpisense_fb_param.gamma = gamma_user;
592 + schedule_delayed_work(&info->deferred_work,
593 + rpisense_fb_defio.delay);
594 + return 0;
595 + case SENSEFB_FBIORESET_GAMMA:
596 + switch (arg) {
597 + case 0:
598 + rpisense_fb_param.gamma = gamma_default;
599 + break;
600 + case 1:
601 + rpisense_fb_param.gamma = gamma_low;
602 + break;
603 + case 2:
604 + rpisense_fb_param.gamma = gamma_user;
605 + break;
606 + default:
607 + return -EINVAL;
608 + }
609 + schedule_delayed_work(&info->deferred_work,
610 + rpisense_fb_defio.delay);
611 + break;
612 + default:
613 + return -EINVAL;
614 + }
615 + return 0;
616 +}
617 +
618 +static struct fb_ops rpisense_fb_ops = {
619 + .owner = THIS_MODULE,
620 + .fb_read = fb_sys_read,
621 + .fb_write = rpisense_fb_write,
622 + .fb_fillrect = rpisense_fb_fillrect,
623 + .fb_copyarea = rpisense_fb_copyarea,
624 + .fb_imageblit = rpisense_fb_imageblit,
625 + .fb_ioctl = rpisense_fb_ioctl,
626 +};
627 +
628 +static int rpisense_fb_probe(struct platform_device *pdev)
629 +{
630 + struct fb_info *info;
631 + int ret = -ENOMEM;
632 + struct rpisense_fb *rpisense_fb;
633 +
634 + rpisense = rpisense_get_dev();
635 + rpisense_fb = &rpisense->framebuffer;
636 +
637 + rpisense_fb_param.vmem = vzalloc(rpisense_fb_param.vmemsize);
638 + if (!rpisense_fb_param.vmem)
639 + return ret;
640 +
641 + rpisense_fb_param.vmem_work = devm_kmalloc(&pdev->dev, 193, GFP_KERNEL);
642 + if (!rpisense_fb_param.vmem_work)
643 + goto err_malloc;
644 +
645 + info = framebuffer_alloc(0, &pdev->dev);
646 + if (!info) {
647 + dev_err(&pdev->dev, "Could not allocate framebuffer.\n");
648 + goto err_malloc;
649 + }
650 + rpisense_fb->info = info;
651 +
652 + rpisense_fb_fix.smem_start = (unsigned long)rpisense_fb_param.vmem;
653 + rpisense_fb_fix.smem_len = rpisense_fb_param.vmemsize;
654 +
655 + info->fbops = &rpisense_fb_ops;
656 + info->fix = rpisense_fb_fix;
657 + info->var = rpisense_fb_var;
658 + info->fbdefio = &rpisense_fb_defio;
659 + info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
660 + info->screen_base = rpisense_fb_param.vmem;
661 + info->screen_size = rpisense_fb_param.vmemsize;
662 +
663 + if (lowlight)
664 + rpisense_fb_param.gamma = gamma_low;
665 +
666 + fb_deferred_io_init(info);
667 +
668 + ret = register_framebuffer(info);
669 + if (ret < 0) {
670 + dev_err(&pdev->dev, "Could not register framebuffer.\n");
671 + goto err_fballoc;
672 + }
673 +
674 + fb_info(info, "%s frame buffer device\n", info->fix.id);
675 + schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
676 + return 0;
677 +err_fballoc:
678 + framebuffer_release(info);
679 +err_malloc:
680 + vfree(rpisense_fb_param.vmem);
681 + return ret;
682 +}
683 +
684 +static int rpisense_fb_remove(struct platform_device *pdev)
685 +{
686 + struct rpisense_fb *rpisense_fb = &rpisense->framebuffer;
687 + struct fb_info *info = rpisense_fb->info;
688 +
689 + if (info) {
690 + unregister_framebuffer(info);
691 + fb_deferred_io_cleanup(info);
692 + framebuffer_release(info);
693 + vfree(rpisense_fb_param.vmem);
694 + }
695 +
696 + return 0;
697 +}
698 +
699 +#ifdef CONFIG_OF
700 +static const struct of_device_id rpisense_fb_id[] = {
701 + { .compatible = "rpi,rpi-sense-fb" },
702 + { },
703 +};
704 +MODULE_DEVICE_TABLE(of, rpisense_fb_id);
705 +#endif
706 +
707 +static struct platform_device_id rpisense_fb_device_id[] = {
708 + { .name = "rpi-sense-fb" },
709 + { },
710 +};
711 +MODULE_DEVICE_TABLE(platform, rpisense_fb_device_id);
712 +
713 +static struct platform_driver rpisense_fb_driver = {
714 + .probe = rpisense_fb_probe,
715 + .remove = rpisense_fb_remove,
716 + .driver = {
717 + .name = "rpi-sense-fb",
718 + .owner = THIS_MODULE,
719 + },
720 +};
721 +
722 +module_platform_driver(rpisense_fb_driver);
723 +
724 +MODULE_DESCRIPTION("Raspberry Pi Sense HAT framebuffer driver");
725 +MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
726 +MODULE_LICENSE("GPL");
727 +
728 --- /dev/null
729 +++ b/include/linux/mfd/rpisense/core.h
730 @@ -0,0 +1,47 @@
731 +/*
732 + * Raspberry Pi Sense HAT core driver
733 + * http://raspberrypi.org
734 + *
735 + * Copyright (C) 2015 Raspberry Pi
736 + *
737 + * Author: Serge Schneider
738 + *
739 + * This program is free software; you can redistribute it and/or modify it
740 + * under the terms of the GNU General Public License as published by the
741 + * Free Software Foundation; either version 2 of the License, or (at your
742 + * option) any later version.
743 + *
744 + */
745 +
746 +#ifndef __LINUX_MFD_RPISENSE_CORE_H_
747 +#define __LINUX_MFD_RPISENSE_CORE_H_
748 +
749 +#include <linux/mfd/rpisense/joystick.h>
750 +#include <linux/mfd/rpisense/framebuffer.h>
751 +
752 +/*
753 + * Register values.
754 + */
755 +#define RPISENSE_FB 0x00
756 +#define RPISENSE_WAI 0xF0
757 +#define RPISENSE_VER 0xF1
758 +#define RPISENSE_KEYS 0xF2
759 +#define RPISENSE_EE_WP 0xF3
760 +
761 +#define RPISENSE_ID 's'
762 +
763 +struct rpisense {
764 + struct device *dev;
765 + struct i2c_client *i2c_client;
766 +
767 + /* Client devices */
768 + struct rpisense_js joystick;
769 + struct rpisense_fb framebuffer;
770 +};
771 +
772 +struct rpisense *rpisense_get_dev(void);
773 +s32 rpisense_reg_read(struct rpisense *rpisense, int reg);
774 +int rpisense_reg_write(struct rpisense *rpisense, int reg, u16 val);
775 +int rpisense_block_write(struct rpisense *rpisense, const char *buf, int count);
776 +
777 +#endif
778 --- /dev/null
779 +++ b/include/linux/mfd/rpisense/framebuffer.h
780 @@ -0,0 +1,32 @@
781 +/*
782 + * Raspberry Pi Sense HAT framebuffer driver
783 + * http://raspberrypi.org
784 + *
785 + * Copyright (C) 2015 Raspberry Pi
786 + *
787 + * Author: Serge Schneider
788 + *
789 + * This program is free software; you can redistribute it and/or modify it
790 + * under the terms of the GNU General Public License as published by the
791 + * Free Software Foundation; either version 2 of the License, or (at your
792 + * option) any later version.
793 + *
794 + */
795 +
796 +#ifndef __LINUX_RPISENSE_FB_H_
797 +#define __LINUX_RPISENSE_FB_H_
798 +
799 +#define SENSEFB_FBIO_IOC_MAGIC 0xF1
800 +
801 +#define SENSEFB_FBIOGET_GAMMA _IO(SENSEFB_FBIO_IOC_MAGIC, 0)
802 +#define SENSEFB_FBIOSET_GAMMA _IO(SENSEFB_FBIO_IOC_MAGIC, 1)
803 +#define SENSEFB_FBIORESET_GAMMA _IO(SENSEFB_FBIO_IOC_MAGIC, 2)
804 +
805 +struct rpisense;
806 +
807 +struct rpisense_fb {
808 + struct platform_device *pdev;
809 + struct fb_info *info;
810 +};
811 +
812 +#endif
813 --- /dev/null
814 +++ b/include/linux/mfd/rpisense/joystick.h
815 @@ -0,0 +1,35 @@
816 +/*
817 + * Raspberry Pi Sense HAT joystick driver
818 + * http://raspberrypi.org
819 + *
820 + * Copyright (C) 2015 Raspberry Pi
821 + *
822 + * Author: Serge Schneider
823 + *
824 + * This program is free software; you can redistribute it and/or modify it
825 + * under the terms of the GNU General Public License as published by the
826 + * Free Software Foundation; either version 2 of the License, or (at your
827 + * option) any later version.
828 + *
829 + */
830 +
831 +#ifndef __LINUX_RPISENSE_JOYSTICK_H_
832 +#define __LINUX_RPISENSE_JOYSTICK_H_
833 +
834 +#include <linux/input.h>
835 +#include <linux/interrupt.h>
836 +#include <linux/gpio/consumer.h>
837 +#include <linux/platform_device.h>
838 +
839 +struct rpisense;
840 +
841 +struct rpisense_js {
842 + struct platform_device *pdev;
843 + struct input_dev *keys_dev;
844 + struct gpio_desc *keys_desc;
845 + struct work_struct keys_work_s;
846 + int keys_irq;
847 +};
848 +
849 +
850 +#endif