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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. From 17ec32c056210154217ea601c86a16685d58f408 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 083/381] 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 | 2 +
  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, 750 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 <serge@raspberrypi.org>");
  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. @@ -194,3 +194,5 @@ intel-soc-pmic-objs := intel_soc_pmic_c
  221. intel-soc-pmic-$(CONFIG_INTEL_PMC_IPC) += intel_soc_pmic_bxtwc.o
  222. obj-$(CONFIG_INTEL_SOC_PMIC) += intel-soc-pmic.o
  223. obj-$(CONFIG_MFD_MT6397) += mt6397-core.o
  224. +
  225. +obj-$(CONFIG_MFD_RPISENSE_CORE) += rpisense-core.o
  226. --- /dev/null
  227. +++ b/drivers/mfd/rpisense-core.c
  228. @@ -0,0 +1,157 @@
  229. +/*
  230. + * Raspberry Pi Sense HAT core driver
  231. + * http://raspberrypi.org
  232. + *
  233. + * Copyright (C) 2015 Raspberry Pi
  234. + *
  235. + * Author: Serge Schneider
  236. + *
  237. + * This program is free software; you can redistribute it and/or modify it
  238. + * under the terms of the GNU General Public License as published by the
  239. + * Free Software Foundation; either version 2 of the License, or (at your
  240. + * option) any later version.
  241. + *
  242. + * This driver is based on wm8350 implementation.
  243. + */
  244. +
  245. +#include <linux/module.h>
  246. +#include <linux/moduleparam.h>
  247. +#include <linux/err.h>
  248. +#include <linux/init.h>
  249. +#include <linux/i2c.h>
  250. +#include <linux/platform_device.h>
  251. +#include <linux/mfd/rpisense/core.h>
  252. +#include <linux/slab.h>
  253. +
  254. +static struct rpisense *rpisense;
  255. +
  256. +static void rpisense_client_dev_register(struct rpisense *rpisense,
  257. + const char *name,
  258. + struct platform_device **pdev)
  259. +{
  260. + int ret;
  261. +
  262. + *pdev = platform_device_alloc(name, -1);
  263. + if (*pdev == NULL) {
  264. + dev_err(rpisense->dev, "Failed to allocate %s\n", name);
  265. + return;
  266. + }
  267. +
  268. + (*pdev)->dev.parent = rpisense->dev;
  269. + platform_set_drvdata(*pdev, rpisense);
  270. + ret = platform_device_add(*pdev);
  271. + if (ret != 0) {
  272. + dev_err(rpisense->dev, "Failed to register %s: %d\n",
  273. + name, ret);
  274. + platform_device_put(*pdev);
  275. + *pdev = NULL;
  276. + }
  277. +}
  278. +
  279. +static int rpisense_probe(struct i2c_client *i2c,
  280. + const struct i2c_device_id *id)
  281. +{
  282. + int ret;
  283. + struct rpisense_js *rpisense_js;
  284. +
  285. + rpisense = devm_kzalloc(&i2c->dev, sizeof(struct rpisense), GFP_KERNEL);
  286. + if (rpisense == NULL)
  287. + return -ENOMEM;
  288. +
  289. + i2c_set_clientdata(i2c, rpisense);
  290. + rpisense->dev = &i2c->dev;
  291. + rpisense->i2c_client = i2c;
  292. +
  293. + ret = rpisense_reg_read(rpisense, RPISENSE_WAI);
  294. + if (ret > 0) {
  295. + if (ret != 's')
  296. + return -EINVAL;
  297. + } else {
  298. + return ret;
  299. + }
  300. + ret = rpisense_reg_read(rpisense, RPISENSE_VER);
  301. + if (ret < 0)
  302. + return ret;
  303. +
  304. + dev_info(rpisense->dev,
  305. + "Raspberry Pi Sense HAT firmware version %i\n", ret);
  306. +
  307. + rpisense_js = &rpisense->joystick;
  308. + rpisense_js->keys_desc = devm_gpiod_get(&i2c->dev,
  309. + "keys-int", GPIOD_IN);
  310. + if (IS_ERR(rpisense_js->keys_desc)) {
  311. + dev_warn(&i2c->dev, "Failed to get keys-int descriptor.\n");
  312. + rpisense_js->keys_desc = gpio_to_desc(23);
  313. + if (rpisense_js->keys_desc == NULL) {
  314. + dev_err(&i2c->dev, "GPIO23 fallback failed.\n");
  315. + return PTR_ERR(rpisense_js->keys_desc);
  316. + }
  317. + }
  318. + rpisense_client_dev_register(rpisense, "rpi-sense-js",
  319. + &(rpisense->joystick.pdev));
  320. + rpisense_client_dev_register(rpisense, "rpi-sense-fb",
  321. + &(rpisense->framebuffer.pdev));
  322. +
  323. + return 0;
  324. +}
  325. +
  326. +static int rpisense_remove(struct i2c_client *i2c)
  327. +{
  328. + struct rpisense *rpisense = i2c_get_clientdata(i2c);
  329. +
  330. + platform_device_unregister(rpisense->joystick.pdev);
  331. + return 0;
  332. +}
  333. +
  334. +struct rpisense *rpisense_get_dev(void)
  335. +{
  336. + return rpisense;
  337. +}
  338. +EXPORT_SYMBOL_GPL(rpisense_get_dev);
  339. +
  340. +s32 rpisense_reg_read(struct rpisense *rpisense, int reg)
  341. +{
  342. + int ret = i2c_smbus_read_byte_data(rpisense->i2c_client, reg);
  343. +
  344. + if (ret < 0)
  345. + dev_err(rpisense->dev, "Read from reg %d failed\n", reg);
  346. + /* Due to the BCM270x I2C clock stretching bug, some values
  347. + * may have MSB set. Clear it to avoid incorrect values.
  348. + * */
  349. + return ret & 0x7F;
  350. +}
  351. +EXPORT_SYMBOL_GPL(rpisense_reg_read);
  352. +
  353. +int rpisense_block_write(struct rpisense *rpisense, const char *buf, int count)
  354. +{
  355. + int ret = i2c_master_send(rpisense->i2c_client, buf, count);
  356. +
  357. + if (ret < 0)
  358. + dev_err(rpisense->dev, "Block write failed\n");
  359. + return ret;
  360. +}
  361. +EXPORT_SYMBOL_GPL(rpisense_block_write);
  362. +
  363. +static const struct i2c_device_id rpisense_i2c_id[] = {
  364. + { "rpi-sense", 0 },
  365. + { }
  366. +};
  367. +MODULE_DEVICE_TABLE(i2c, rpisense_i2c_id);
  368. +
  369. +
  370. +static struct i2c_driver rpisense_driver = {
  371. + .driver = {
  372. + .name = "rpi-sense",
  373. + .owner = THIS_MODULE,
  374. + },
  375. + .probe = rpisense_probe,
  376. + .remove = rpisense_remove,
  377. + .id_table = rpisense_i2c_id,
  378. +};
  379. +
  380. +module_i2c_driver(rpisense_driver);
  381. +
  382. +MODULE_DESCRIPTION("Raspberry Pi Sense HAT core driver");
  383. +MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
  384. +MODULE_LICENSE("GPL");
  385. +
  386. --- a/drivers/video/fbdev/Kconfig
  387. +++ b/drivers/video/fbdev/Kconfig
  388. @@ -2505,3 +2505,16 @@ config FB_SM712
  389. This driver is also available as a module. The module will be
  390. called sm712fb. If you want to compile it as a module, say M
  391. here and read <file:Documentation/kbuild/modules.txt>.
  392. +
  393. +config FB_RPISENSE
  394. + tristate "Raspberry Pi Sense HAT framebuffer"
  395. + depends on FB
  396. + select MFD_RPISENSE_CORE
  397. + select FB_SYS_FOPS
  398. + select FB_SYS_FILLRECT
  399. + select FB_SYS_COPYAREA
  400. + select FB_SYS_IMAGEBLIT
  401. + select FB_DEFERRED_IO
  402. +
  403. + help
  404. + This is the framebuffer driver for the Raspberry Pi Sense HAT
  405. --- a/drivers/video/fbdev/Makefile
  406. +++ b/drivers/video/fbdev/Makefile
  407. @@ -150,6 +150,7 @@ obj-$(CONFIG_FB_DA8XX) += da8xx-fb.o
  408. obj-$(CONFIG_FB_MXS) += mxsfb.o
  409. obj-$(CONFIG_FB_SSD1307) += ssd1307fb.o
  410. obj-$(CONFIG_FB_SIMPLE) += simplefb.o
  411. +obj-$(CONFIG_FB_RPISENSE) += rpisense-fb.o
  412. # the test framebuffer is last
  413. obj-$(CONFIG_FB_VIRTUAL) += vfb.o
  414. --- /dev/null
  415. +++ b/drivers/video/fbdev/rpisense-fb.c
  416. @@ -0,0 +1,293 @@
  417. +/*
  418. + * Raspberry Pi Sense HAT framebuffer driver
  419. + * http://raspberrypi.org
  420. + *
  421. + * Copyright (C) 2015 Raspberry Pi
  422. + *
  423. + * Author: Serge Schneider
  424. + *
  425. + * This program is free software; you can redistribute it and/or modify it
  426. + * under the terms of the GNU General Public License as published by the
  427. + * Free Software Foundation; either version 2 of the License, or (at your
  428. + * option) any later version.
  429. + *
  430. + */
  431. +
  432. +#include <linux/module.h>
  433. +#include <linux/kernel.h>
  434. +#include <linux/errno.h>
  435. +#include <linux/string.h>
  436. +#include <linux/mm.h>
  437. +#include <linux/slab.h>
  438. +#include <linux/uaccess.h>
  439. +#include <linux/delay.h>
  440. +#include <linux/fb.h>
  441. +#include <linux/init.h>
  442. +
  443. +#include <linux/mfd/rpisense/framebuffer.h>
  444. +#include <linux/mfd/rpisense/core.h>
  445. +
  446. +static bool lowlight;
  447. +module_param(lowlight, bool, 0);
  448. +MODULE_PARM_DESC(lowlight, "Reduce LED matrix brightness to one third");
  449. +
  450. +static struct rpisense *rpisense;
  451. +
  452. +struct rpisense_fb_param {
  453. + char __iomem *vmem;
  454. + u8 *vmem_work;
  455. + u32 vmemsize;
  456. + u8 *gamma;
  457. +};
  458. +
  459. +static u8 gamma_default[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  460. + 0x02, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x07,
  461. + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0E, 0x0F, 0x11,
  462. + 0x12, 0x14, 0x15, 0x17, 0x19, 0x1B, 0x1D, 0x1F,};
  463. +
  464. +static u8 gamma_low[32] = {0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  465. + 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02,
  466. + 0x03, 0x03, 0x03, 0x04, 0x04, 0x05, 0x05, 0x06,
  467. + 0x06, 0x07, 0x07, 0x08, 0x08, 0x09, 0x0A, 0x0A,};
  468. +
  469. +static u8 gamma_user[32];
  470. +
  471. +static struct rpisense_fb_param rpisense_fb_param = {
  472. + .vmem = NULL,
  473. + .vmemsize = 128,
  474. + .gamma = gamma_default,
  475. +};
  476. +
  477. +static struct fb_deferred_io rpisense_fb_defio;
  478. +
  479. +static struct fb_fix_screeninfo rpisense_fb_fix = {
  480. + .id = "RPi-Sense FB",
  481. + .type = FB_TYPE_PACKED_PIXELS,
  482. + .visual = FB_VISUAL_TRUECOLOR,
  483. + .xpanstep = 0,
  484. + .ypanstep = 0,
  485. + .ywrapstep = 0,
  486. + .accel = FB_ACCEL_NONE,
  487. + .line_length = 16,
  488. +};
  489. +
  490. +static struct fb_var_screeninfo rpisense_fb_var = {
  491. + .xres = 8,
  492. + .yres = 8,
  493. + .xres_virtual = 8,
  494. + .yres_virtual = 8,
  495. + .bits_per_pixel = 16,
  496. + .red = {11, 5, 0},
  497. + .green = {5, 6, 0},
  498. + .blue = {0, 5, 0},
  499. +};
  500. +
  501. +static ssize_t rpisense_fb_write(struct fb_info *info,
  502. + const char __user *buf, size_t count,
  503. + loff_t *ppos)
  504. +{
  505. + ssize_t res = fb_sys_write(info, buf, count, ppos);
  506. +
  507. + schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
  508. + return res;
  509. +}
  510. +
  511. +static void rpisense_fb_fillrect(struct fb_info *info,
  512. + const struct fb_fillrect *rect)
  513. +{
  514. + sys_fillrect(info, rect);
  515. + schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
  516. +}
  517. +
  518. +static void rpisense_fb_copyarea(struct fb_info *info,
  519. + const struct fb_copyarea *area)
  520. +{
  521. + sys_copyarea(info, area);
  522. + schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
  523. +}
  524. +
  525. +static void rpisense_fb_imageblit(struct fb_info *info,
  526. + const struct fb_image *image)
  527. +{
  528. + sys_imageblit(info, image);
  529. + schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
  530. +}
  531. +
  532. +static void rpisense_fb_deferred_io(struct fb_info *info,
  533. + struct list_head *pagelist)
  534. +{
  535. + int i;
  536. + int j;
  537. + u8 *vmem_work = rpisense_fb_param.vmem_work;
  538. + u16 *mem = (u16 *)rpisense_fb_param.vmem;
  539. + u8 *gamma = rpisense_fb_param.gamma;
  540. +
  541. + vmem_work[0] = 0;
  542. + for (j = 0; j < 8; j++) {
  543. + for (i = 0; i < 8; i++) {
  544. + vmem_work[(j * 24) + i + 1] =
  545. + gamma[(mem[(j * 8) + i] >> 11) & 0x1F];
  546. + vmem_work[(j * 24) + (i + 8) + 1] =
  547. + gamma[(mem[(j * 8) + i] >> 6) & 0x1F];
  548. + vmem_work[(j * 24) + (i + 16) + 1] =
  549. + gamma[(mem[(j * 8) + i]) & 0x1F];
  550. + }
  551. + }
  552. + rpisense_block_write(rpisense, vmem_work, 193);
  553. +}
  554. +
  555. +static struct fb_deferred_io rpisense_fb_defio = {
  556. + .delay = HZ/100,
  557. + .deferred_io = rpisense_fb_deferred_io,
  558. +};
  559. +
  560. +static int rpisense_fb_ioctl(struct fb_info *info, unsigned int cmd,
  561. + unsigned long arg)
  562. +{
  563. + switch (cmd) {
  564. + case SENSEFB_FBIOGET_GAMMA:
  565. + if (copy_to_user((void __user *) arg, rpisense_fb_param.gamma,
  566. + sizeof(u8[32])))
  567. + return -EFAULT;
  568. + return 0;
  569. + case SENSEFB_FBIOSET_GAMMA:
  570. + if (copy_from_user(gamma_user, (void __user *)arg,
  571. + sizeof(u8[32])))
  572. + return -EFAULT;
  573. + rpisense_fb_param.gamma = gamma_user;
  574. + schedule_delayed_work(&info->deferred_work,
  575. + rpisense_fb_defio.delay);
  576. + return 0;
  577. + case SENSEFB_FBIORESET_GAMMA:
  578. + switch (arg) {
  579. + case 0:
  580. + rpisense_fb_param.gamma = gamma_default;
  581. + break;
  582. + case 1:
  583. + rpisense_fb_param.gamma = gamma_low;
  584. + break;
  585. + case 2:
  586. + rpisense_fb_param.gamma = gamma_user;
  587. + break;
  588. + default:
  589. + return -EINVAL;
  590. + }
  591. + schedule_delayed_work(&info->deferred_work,
  592. + rpisense_fb_defio.delay);
  593. + break;
  594. + default:
  595. + return -EINVAL;
  596. + }
  597. + return 0;
  598. +}
  599. +
  600. +static struct fb_ops rpisense_fb_ops = {
  601. + .owner = THIS_MODULE,
  602. + .fb_read = fb_sys_read,
  603. + .fb_write = rpisense_fb_write,
  604. + .fb_fillrect = rpisense_fb_fillrect,
  605. + .fb_copyarea = rpisense_fb_copyarea,
  606. + .fb_imageblit = rpisense_fb_imageblit,
  607. + .fb_ioctl = rpisense_fb_ioctl,
  608. +};
  609. +
  610. +static int rpisense_fb_probe(struct platform_device *pdev)
  611. +{
  612. + struct fb_info *info;
  613. + int ret = -ENOMEM;
  614. + struct rpisense_fb *rpisense_fb;
  615. +
  616. + rpisense = rpisense_get_dev();
  617. + rpisense_fb = &rpisense->framebuffer;
  618. +
  619. + rpisense_fb_param.vmem = vzalloc(rpisense_fb_param.vmemsize);
  620. + if (!rpisense_fb_param.vmem)
  621. + return ret;
  622. +
  623. + rpisense_fb_param.vmem_work = devm_kmalloc(&pdev->dev, 193, GFP_KERNEL);
  624. + if (!rpisense_fb_param.vmem_work)
  625. + goto err_malloc;
  626. +
  627. + info = framebuffer_alloc(0, &pdev->dev);
  628. + if (!info) {
  629. + dev_err(&pdev->dev, "Could not allocate framebuffer.\n");
  630. + goto err_malloc;
  631. + }
  632. + rpisense_fb->info = info;
  633. +
  634. + rpisense_fb_fix.smem_start = (unsigned long)rpisense_fb_param.vmem;
  635. + rpisense_fb_fix.smem_len = rpisense_fb_param.vmemsize;
  636. +
  637. + info->fbops = &rpisense_fb_ops;
  638. + info->fix = rpisense_fb_fix;
  639. + info->var = rpisense_fb_var;
  640. + info->fbdefio = &rpisense_fb_defio;
  641. + info->flags = FBINFO_FLAG_DEFAULT | FBINFO_VIRTFB;
  642. + info->screen_base = rpisense_fb_param.vmem;
  643. + info->screen_size = rpisense_fb_param.vmemsize;
  644. +
  645. + if (lowlight)
  646. + rpisense_fb_param.gamma = gamma_low;
  647. +
  648. + fb_deferred_io_init(info);
  649. +
  650. + ret = register_framebuffer(info);
  651. + if (ret < 0) {
  652. + dev_err(&pdev->dev, "Could not register framebuffer.\n");
  653. + goto err_fballoc;
  654. + }
  655. +
  656. + fb_info(info, "%s frame buffer device\n", info->fix.id);
  657. + schedule_delayed_work(&info->deferred_work, rpisense_fb_defio.delay);
  658. + return 0;
  659. +err_fballoc:
  660. + framebuffer_release(info);
  661. +err_malloc:
  662. + vfree(rpisense_fb_param.vmem);
  663. + return ret;
  664. +}
  665. +
  666. +static int rpisense_fb_remove(struct platform_device *pdev)
  667. +{
  668. + struct rpisense_fb *rpisense_fb = &rpisense->framebuffer;
  669. + struct fb_info *info = rpisense_fb->info;
  670. +
  671. + if (info) {
  672. + unregister_framebuffer(info);
  673. + fb_deferred_io_cleanup(info);
  674. + framebuffer_release(info);
  675. + vfree(rpisense_fb_param.vmem);
  676. + }
  677. +
  678. + return 0;
  679. +}
  680. +
  681. +#ifdef CONFIG_OF
  682. +static const struct of_device_id rpisense_fb_id[] = {
  683. + { .compatible = "rpi,rpi-sense-fb" },
  684. + { },
  685. +};
  686. +MODULE_DEVICE_TABLE(of, rpisense_fb_id);
  687. +#endif
  688. +
  689. +static struct platform_device_id rpisense_fb_device_id[] = {
  690. + { .name = "rpi-sense-fb" },
  691. + { },
  692. +};
  693. +MODULE_DEVICE_TABLE(platform, rpisense_fb_device_id);
  694. +
  695. +static struct platform_driver rpisense_fb_driver = {
  696. + .probe = rpisense_fb_probe,
  697. + .remove = rpisense_fb_remove,
  698. + .driver = {
  699. + .name = "rpi-sense-fb",
  700. + .owner = THIS_MODULE,
  701. + },
  702. +};
  703. +
  704. +module_platform_driver(rpisense_fb_driver);
  705. +
  706. +MODULE_DESCRIPTION("Raspberry Pi Sense HAT framebuffer driver");
  707. +MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
  708. +MODULE_LICENSE("GPL");
  709. +
  710. --- /dev/null
  711. +++ b/include/linux/mfd/rpisense/core.h
  712. @@ -0,0 +1,47 @@
  713. +/*
  714. + * Raspberry Pi Sense HAT core driver
  715. + * http://raspberrypi.org
  716. + *
  717. + * Copyright (C) 2015 Raspberry Pi
  718. + *
  719. + * Author: Serge Schneider
  720. + *
  721. + * This program is free software; you can redistribute it and/or modify it
  722. + * under the terms of the GNU General Public License as published by the
  723. + * Free Software Foundation; either version 2 of the License, or (at your
  724. + * option) any later version.
  725. + *
  726. + */
  727. +
  728. +#ifndef __LINUX_MFD_RPISENSE_CORE_H_
  729. +#define __LINUX_MFD_RPISENSE_CORE_H_
  730. +
  731. +#include <linux/mfd/rpisense/joystick.h>
  732. +#include <linux/mfd/rpisense/framebuffer.h>
  733. +
  734. +/*
  735. + * Register values.
  736. + */
  737. +#define RPISENSE_FB 0x00
  738. +#define RPISENSE_WAI 0xF0
  739. +#define RPISENSE_VER 0xF1
  740. +#define RPISENSE_KEYS 0xF2
  741. +#define RPISENSE_EE_WP 0xF3
  742. +
  743. +#define RPISENSE_ID 's'
  744. +
  745. +struct rpisense {
  746. + struct device *dev;
  747. + struct i2c_client *i2c_client;
  748. +
  749. + /* Client devices */
  750. + struct rpisense_js joystick;
  751. + struct rpisense_fb framebuffer;
  752. +};
  753. +
  754. +struct rpisense *rpisense_get_dev(void);
  755. +s32 rpisense_reg_read(struct rpisense *rpisense, int reg);
  756. +int rpisense_reg_write(struct rpisense *rpisense, int reg, u16 val);
  757. +int rpisense_block_write(struct rpisense *rpisense, const char *buf, int count);
  758. +
  759. +#endif
  760. --- /dev/null
  761. +++ b/include/linux/mfd/rpisense/framebuffer.h
  762. @@ -0,0 +1,32 @@
  763. +/*
  764. + * Raspberry Pi Sense HAT framebuffer driver
  765. + * http://raspberrypi.org
  766. + *
  767. + * Copyright (C) 2015 Raspberry Pi
  768. + *
  769. + * Author: Serge Schneider
  770. + *
  771. + * This program is free software; you can redistribute it and/or modify it
  772. + * under the terms of the GNU General Public License as published by the
  773. + * Free Software Foundation; either version 2 of the License, or (at your
  774. + * option) any later version.
  775. + *
  776. + */
  777. +
  778. +#ifndef __LINUX_RPISENSE_FB_H_
  779. +#define __LINUX_RPISENSE_FB_H_
  780. +
  781. +#define SENSEFB_FBIO_IOC_MAGIC 0xF1
  782. +
  783. +#define SENSEFB_FBIOGET_GAMMA _IO(SENSEFB_FBIO_IOC_MAGIC, 0)
  784. +#define SENSEFB_FBIOSET_GAMMA _IO(SENSEFB_FBIO_IOC_MAGIC, 1)
  785. +#define SENSEFB_FBIORESET_GAMMA _IO(SENSEFB_FBIO_IOC_MAGIC, 2)
  786. +
  787. +struct rpisense;
  788. +
  789. +struct rpisense_fb {
  790. + struct platform_device *pdev;
  791. + struct fb_info *info;
  792. +};
  793. +
  794. +#endif
  795. --- /dev/null
  796. +++ b/include/linux/mfd/rpisense/joystick.h
  797. @@ -0,0 +1,35 @@
  798. +/*
  799. + * Raspberry Pi Sense HAT joystick driver
  800. + * http://raspberrypi.org
  801. + *
  802. + * Copyright (C) 2015 Raspberry Pi
  803. + *
  804. + * Author: Serge Schneider
  805. + *
  806. + * This program is free software; you can redistribute it and/or modify it
  807. + * under the terms of the GNU General Public License as published by the
  808. + * Free Software Foundation; either version 2 of the License, or (at your
  809. + * option) any later version.
  810. + *
  811. + */
  812. +
  813. +#ifndef __LINUX_RPISENSE_JOYSTICK_H_
  814. +#define __LINUX_RPISENSE_JOYSTICK_H_
  815. +
  816. +#include <linux/input.h>
  817. +#include <linux/interrupt.h>
  818. +#include <linux/gpio/consumer.h>
  819. +#include <linux/platform_device.h>
  820. +
  821. +struct rpisense;
  822. +
  823. +struct rpisense_js {
  824. + struct platform_device *pdev;
  825. + struct input_dev *keys_dev;
  826. + struct gpio_desc *keys_desc;
  827. + struct work_struct keys_work_s;
  828. + int keys_irq;
  829. +};
  830. +
  831. +
  832. +#endif