950-0065-mfd-Add-Raspberry-Pi-Sense-HAT-core-driver.patch 23 KB

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