Raspberry Pi Sense HAT driver
Subsystems (4)
v2 cover Raspberry Pi Sense HAT driver
This patch series adds a set of drivers for operating the Sense HAT peripheral device. This board is an add on for the Raspberry Pi that is designed to connect using the GPIO connector via I2C.
It features:
- a joystick
- an 8x8 RGB LED matrix display
- a whole bunch of environmental sensors with their own drivers (those are already in upstream Linux)
This is a refactor of the work of Serge Schneider, the author of a version of this driver that is currently in the Raspberry Pi downstream kernel. We modified his code to make it suitable for upstream Linux.
A couple of tests are available for the driver in this repo:
https://github.com/underground-software/sensehat/tree/master/tests
- color_test displays various solid colors on the LED panel
- framebuffer_test diplays a single lit cell that is controllable via the arrow keys or the joystick
Known issue:
- We are not sure how to integrate the device tree overlay for the Sense HAT into mainline Linux, or if we even should at this time.
Fortunately, most Linux distrubtions that can run on the Raspberry Pi already include an appropriate device tree overlay since they often import all of the overlays from the downstream Raspberry Pi kernel.
We have included a hypothetical overlay that would work for the sensehat as a fourth patch in this series but we just put it in the root of the source tree for the time being so that patch should not be merged unless a consensus is reached that the overlay should be in the tree, and a proper location is found to put it.
Suggestions are welcome and appreciated.
For more information about the Sense HAT, visit:
https://www.raspberrypi.org/products/sense-hat/
Changes since v1:
- We included a device tree overlay for the Sense HAT
- Based on Feedback from v1 we:
- Changed the style of a few lines
- Adding more error logging
- Changed the compatible strings to use the more common "raspberrypi" instead of just "rpi"
- Renamed everything from rpisense to sensehat
Charles Mirabile (4):
Add core driver and config
Add joystick driver and config
Add display driver and config
Add sensehat device tree overlay
arch/arm/boot/dts/sensehat.dtbs | 51 +++++ drivers/auxdisplay/Kconfig | 7 + drivers/auxdisplay/Makefile | 1 + drivers/auxdisplay/sensehat-display.c | 234 +++++++++++++++++++++ drivers/input/joystick/Kconfig | 8 + drivers/input/joystick/Makefile | 1 + drivers/input/joystick/sensehat-joystick.c | 124 +++++++++++ drivers/mfd/Kconfig | 8 + drivers/mfd/Makefile | 1 + drivers/mfd/sensehat-core.c | 168 +++++++++++++++ include/linux/mfd/sensehat.h | 55 +++++ 11 files changed, 658 insertions(+) create mode 100644 arch/arm/boot/dts/sensehat.dtbs create mode 100644 drivers/auxdisplay/sensehat-display.c create mode 100644 drivers/input/joystick/sensehat-joystick.c create mode 100644 drivers/mfd/sensehat-core.c create mode 100644 include/linux/mfd/sensehat.h
Signature · 1 line
2.27.0
RFC PATCH v2 1/4 drivers/mfd: sensehat: Raspberry Pi Sense HAT core driver
This patch adds the core driver file, containing methods to communicate with the board over I2C. We also add the header file shared by all three drivers, containing common data and definitions. In addition, we add a config option to toggle compilation of the driver.
--- drivers/mfd/Kconfig | 8 ++ drivers/mfd/Makefile | 1 + drivers/mfd/sensehat-core.c | 168 +++++++++++++++++++++++++++++++++++ include/linux/mfd/sensehat.h | 55 ++++++++++++ 4 files changed, 232 insertions(+) create mode 100644 drivers/mfd/sensehat-core.c create mode 100644 include/linux/mfd/sensehat.h
drivers/mfd/Kconfig +8 -0
@@ -11,6 +11,14 @@ config MFD_CORE select IRQ_DOMAIN default n +config MFD_SENSEHAT_CORE + tristate "Raspberry Pi Sense HAT core functions" + depends on I2C + select MFD_CORE + help + This is the core driver for the Raspberry Pi Sense HAT. This provides + the necessary functions to communicate with the hardware. + config MFD_CS5535 tristate "AMD CS5535 and CS5536 southbridge core functions" select MFD_CORE
drivers/mfd/Makefile +1 -0
@@ -263,6 +263,7 @@ obj-$(CONFIG_MFD_ROHM_BD718XX) += rohm-bd718x7.o obj-$(CONFIG_MFD_ROHM_BD957XMUF) += rohm-bd9576.o obj-$(CONFIG_MFD_STMFX) += stmfx.o obj-$(CONFIG_MFD_KHADAS_MCU) += khadas-mcu.o +obj-$(CONFIG_MFD_SENSEHAT_CORE) += sensehat-core.o obj-$(CONFIG_MFD_ACER_A500_EC) += acer-ec-a500.o obj-$(CONFIG_MFD_QCOM_PM8008) += qcom-pm8008.o
drivers/mfd/sensehat-core.c new file +168 -0
168 lines not on this page · raw message
include/linux/mfd/sensehat.h new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [RFC PATCH v2 1/4] drivers/mfd: sensehat: Raspberry Pi Sense HAT core driver
Quoted 9 lines · Charles Mirabile
This patch adds the core driver file, containing methods to communicate with the board over I2C. We also add the header file shared by all three drivers, containing common data and definitions. In addition, we add a config option to toggle compilation of the driver.
Signed-off-by Charles Mirabile <[email protected]>Signed-off-by Mwesigwa Guma <[email protected]>Signed-off-by Joel Savitz <[email protected]>---
Correct me if I'm wrong, but it seems to me that you could completely avoid this driver if you used 'simple-mfd-i2c.c'. See 'kontron,sl28cpld' for an example of how it's used.
Regards,
Nicolas
RFC PATCH v2 2/4 drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick driver
In reply to Charles MirabileThis patch implements support for the joystick. It supports left/right/up/down/enter attached via i2c
--- drivers/input/joystick/Kconfig | 8 ++ drivers/input/joystick/Makefile | 1 + drivers/input/joystick/sensehat-joystick.c | 124 +++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 drivers/input/joystick/sensehat-joystick.c
drivers/input/joystick/Kconfig +8 -0
@@ -399,4 +399,12 @@ config JOYSTICK_N64 Say Y here if you want enable support for the four built-in controller ports on the Nintendo 64 console. +config JOYSTICK_SENSEHAT + tristate "Raspberry Pi Sense HAT joystick" + depends on GPIOLIB && INPUT + select MFD_SENSEHAT_CORE + + help + This is the joystick driver for the Raspberry Pi Sense HAT + endif
drivers/input/joystick/Makefile +1 -0
@@ -28,6 +28,7 @@ obj-$(CONFIG_JOYSTICK_N64) += n64joy.o obj-$(CONFIG_JOYSTICK_PSXPAD_SPI) += psxpad-spi.o obj-$(CONFIG_JOYSTICK_PXRC) += pxrc.o obj-$(CONFIG_JOYSTICK_QWIIC) += qwiic-joystick.o +obj-$(CONFIG_JOYSTICK_SENSEHAT) += sensehat-joystick.o obj-$(CONFIG_JOYSTICK_SIDEWINDER) += sidewinder.o obj-$(CONFIG_JOYSTICK_SPACEBALL) += spaceball.o obj-$(CONFIG_JOYSTICK_SPACEORB) += spaceorb.o
drivers/input/joystick/sensehat-joystick.c new file +124 -0
124 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [RFC PATCH vs 2/4] drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick driver
Quoted 60 lines · Charles Mirabile
This patch implements support for the joystick. It supports left/right/up/down/enter attached via i2c
Signed-off-by Charles Mirabile <[email protected]>Signed-off-by Mwesigwa Guma <[email protected]>Signed-off-by Joel Savitz <[email protected]>--- drivers/input/joystick/Kconfig | 8 ++ drivers/input/joystick/Makefile | 1 + drivers/input/joystick/sensehat-joystick.c | 124 +++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 drivers/input/joystick/sensehat-joystick.cdrivers/input/joystick/Kconfig +8
-0@@ -399,4 +399,12 @@ config JOYSTICK_N64 Say Y here if you want enable support for the four built-in controller ports on the Nintendo 64 console. +config JOYSTICK_SENSEHAT + tristate "Raspberry Pi Sense HAT joystick" + depends on GPIOLIB && INPUT + select MFD_SENSEHAT_CORE + + help + This is the joystick driver for the Raspberry Pi Sense HAT + endifdrivers/input/joystick/Makefile +1
-0@@ -28,6 +28,7 @@ obj-$(CONFIG_JOYSTICK_N64) += n64joy.o obj-$(CONFIG_JOYSTICK_PSXPAD_SPI) += psxpad-spi.o obj-$(CONFIG_JOYSTICK_PXRC) += pxrc.o obj-$(CONFIG_JOYSTICK_QWIIC) += qwiic-joystick.o +obj-$(CONFIG_JOYSTICK_SENSEHAT) += sensehat-joystick.o obj-$(CONFIG_JOYSTICK_SIDEWINDER) += sidewinder.o obj-$(CONFIG_JOYSTICK_SPACEBALL) += spaceball.o obj-$(CONFIG_JOYSTICK_SPACEORB) += spaceorb.odrivers/input/joystick/sensehat-joystick.c new file +12
-0@@ -0,0 +1,124 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Raspberry Pi Sense HAT joystick driver +* http://raspberrypi.org +* +* Copyright (C) 2015 Raspberry Pi +* Copyright (C) 2021 Charles Mirabile, Mwesigwa Guma, Joel Savitz +* +* Original Author: Serge Schneider +* Revised for upstream Linux by: Charles Mirabile, Mwesigwa Guma, Joel Savitz +*/ +
drivers/input/joystick/sensehat-joystick.c line 22
Quoted 10 lines
drivers/input/joystick/sensehat-joystick.c new file +10
-0+#include <linux/module.h> +#include <linux/input.h> +#include <linux/i2c.h> +#include <linux/interrupt.h> +#include <linux/gpio/consumer.h> +#include <linux/platform_device.h> + +#include <linux/mfd/sensehat.h> + +static unsigned char keymap[] = {KEY_DOWN ,KEY_RIGHT ,KEY_UP ,KEY_ENTER ,KEY_LEFT ,};
I'm not sure devices outputing KEY_* events fits input/joystick. Maybe input/misc or input/keyboard? Or else use 'BTN_DPAD_UP/DOWN/LEFT/RIGHT'. Which is what game controllers seem to do.
Regards,
Nicolas
RFC PATCH v2 3/4 drivers/auxdisplay: senshat Raspberry Pi Sense HAT display driver
In reply to Charles MirabileThis patch implements control of the 8x8 RGB LED matrix display.
--- drivers/auxdisplay/Kconfig | 7 + drivers/auxdisplay/Makefile | 1 + drivers/auxdisplay/sensehat-display.c | 233 ++++++++++++++++++++++++++ 3 files changed, 241 insertions(+) create mode 100644 drivers/auxdisplay/sensehat-display.c
drivers/auxdisplay/Kconfig +7 -0
@@ -193,6 +193,13 @@ config ARM_CHARLCD line and the Linux version on the second line, but that's still useful. +config SENSEHAT_DISPLAY + tristate "Raspberry pi Sense HAT display driver" + select MFD_SENSEHAT_CORE + help + This is a driver for the Raspberry Pi Sensehat 8x8 RBG-LED matrix + you can access it as a misc device at /dev/sense-hat + menuconfig PARPORT_PANEL tristate "Parallel port LCD/Keypad Panel support" depends on PARPORT
drivers/auxdisplay/Makefile +1 -0
@@ -13,3 +13,4 @@ obj-$(CONFIG_HD44780) += hd44780.o obj-$(CONFIG_HT16K33) += ht16k33.o obj-$(CONFIG_PARPORT_PANEL) += panel.o obj-$(CONFIG_LCD2S) += lcd2s.o +obj-$(CONFIG_SENSEHAT_DISPLAY) += sensehat-display.o
drivers/auxdisplay/sensehat-display.c new file +233 -0
233 lines not on this page · raw message
Signature · 1 line
2.27.0
Re: [RFC PATCH v2 3/4] drivers/auxdisplay: senshat Raspberry Pi Sense HAT display driver
Hi Charles,
Quoted 1 line · Charles Mirabile
This patch implements control of the 8x8 RGB LED matrix display.
It'd be nice to get a more information on the i2c interface, and what each byte is supposed to represent.
Quoted 4 lines
Signed-off-by Charles Mirabile <[email protected]>Signed-off-by Mwesigwa Guma <[email protected]>Signed-off-by Joel Savitz <[email protected]>---
[...]
Quoted 43 lines
drivers/auxdisplay/sensehat-display.c new file +43
-0+static long sensehat_display_ioctl (struct file *filp,unsigned int cmd, +unsigned long arg) +{ + struct sensehat *sensehat = container_of(filp->private_data, struct sensehat, display.mdev); +struct sensehat_display *sensehat_display = &sensehat->display; +void __user *user_ptr = (void __user *)arg; +u8 temp[GAMMA_SIZE ]; +int ret; + +if (mutex_lock_interruptible (&sensehat_display->rw_mtx)) +return -ERESTARTSYS ; +switch (cmd) { +case SENSEDISP_IOGET_GAMMA : +if (copy_to_user (user_ptr, sensehat_display->gamma,GAMMA_SIZE )) { + ret = -EFAULT ; + goto out_unlock; + } + ret =0 ; + goto out_unlock; +case SENSEDISP_IOSET_GAMMA : +if (copy_from_user (temp, user_ptr,GAMMA_SIZE )) { + ret = -EFAULT ; + goto out_unlock; + } + ret =0 ; + goto out_update; +case SENSEDISP_IORESET_GAMMA : +if (arg <GAMMA_DEFAULT || arg >=GAMMA_PRESET_COUNT ) { + ret = -EINVAL ; + goto out_unlock; + } +memcpy (temp, gamma_presets[arg],GAMMA_SIZE ); + ret =0 ; + goto out_update; +default : + ret = -EINVAL ; + goto out_unlock; + } +out_update: +memcpy (sensehat_display->gamma, temp,GAMMA_SIZE ); +sensehat_update_display (sensehat); +out_unlock: +mutex_unlock (&sensehat_display->rw_mtx);
drivers/auxdisplay/sensehat-display.c line 213
Quoted 10 lines
drivers/auxdisplay/sensehat-display.c new file +10
-0+return ret; +} + +static const struct file_operations sensehat_display_fops = { + .owner =THIS_MODULE , + .llseek = sensehat_display_llseek, + .read = sensehat_display_read, + .write = sensehat_display_write, + .unlocked_ioctl = sensehat_display_ioctl, +};
I doubt this approach will make it upstream. This should use an already existing kernel interface, or if not good enough, extend it. I'm sure this is not the first RGB led matrix to show up anyway. Maybe drivers/leds has infrastructure to deal with this. Or else a fb device?
I presume you want to keep the IOCTL in order to be able to run RPi specific aplications/libraries. It'll be up to them to change once we settle on a proper way of handling this, not the other way around. Note that the RPi engineers have always preffered using official kernel interfaces when available, so I don't think this'll be problematic.
Regards,
Nicolas
Re: [RFC PATCH v2 3/4] drivers/auxdisplay: senshat Raspberry Pi Sense HAT display driver
Quoted 65 lines · On 30/08/2021 15:28, [email protected] wrote:
Hi Charles,
Quoted 1 line · Charles Mirabile
This patch implements control of the 8x8 RGB LED matrix display.
It'd be nice to get a more information on the i2c interface, and what each byte is supposed to represent.
Quoted 4 lines
Signed-off-by Charles Mirabile <[email protected]>Signed-off-by Mwesigwa Guma <[email protected]>Signed-off-by Joel Savitz <[email protected]>---
[...]
Quoted 50 lines
drivers/auxdisplay/sensehat-display.c new file +50
-0+static long sensehat_display_ioctl (struct file *filp,unsigned int cmd, +unsigned long arg) +{ + struct sensehat *sensehat = container_of(filp->private_data, struct sensehat, display.mdev); +struct sensehat_display *sensehat_display = &sensehat->display; +void __user *user_ptr = (void __user *)arg; +u8 temp[GAMMA_SIZE ]; +int ret; + +if (mutex_lock_interruptible (&sensehat_display->rw_mtx)) +return -ERESTARTSYS ; +switch (cmd) { +case SENSEDISP_IOGET_GAMMA : +if (copy_to_user (user_ptr, sensehat_display->gamma,GAMMA_SIZE )) { + ret = -EFAULT ; + goto out_unlock; + } + ret =0 ; + goto out_unlock; +case SENSEDISP_IOSET_GAMMA : +if (copy_from_user (temp, user_ptr,GAMMA_SIZE )) { + ret = -EFAULT ; + goto out_unlock; + } + ret =0 ; + goto out_update; +case SENSEDISP_IORESET_GAMMA : +if (arg <GAMMA_DEFAULT || arg >=GAMMA_PRESET_COUNT ) { + ret = -EINVAL ; + goto out_unlock; + } +memcpy (temp, gamma_presets[arg],GAMMA_SIZE ); + ret =0 ; + goto out_update; +default : + ret = -EINVAL ; + goto out_unlock; + } +out_update: +memcpy (sensehat_display->gamma, temp,GAMMA_SIZE ); +sensehat_update_display (sensehat); +out_unlock: +mutex_unlock (&sensehat_display->rw_mtx); +return ret; +} + +static const struct file_operations sensehat_display_fops = { + .owner =THIS_MODULE , + .llseek = sensehat_display_llseek, + .read = sensehat_display_read,
Quoted 8 lines
drivers/auxdisplay/sensehat-display.c line 213
Quoted 3 lines
drivers/auxdisplay/sensehat-display.c new file +3
-0+ .write = sensehat_display_write, + .unlocked_ioctl = sensehat_display_ioctl, +};I doubt this approach will make it upstream. This should use an already existing kernel interface, or if not good enough, extend it. I'm sure this is not the first RGB led matrix to show up anyway. Maybe
drivers/ledshas infrastructure to deal with this. Or else a fb device?
I'm working on upstreaming that again and my approach is to use framebuffer for this. What is the reason you decided against framebuffer?
Regards,
Matthias
Quoted 10 lines
I presume you want to keep the IOCTL in order to be able to run RPi specific aplications/libraries. It'll be up to them to change once we settle on a proper way of handling this, not the other way around. Note that the RPi engineers have always preffered using official kernel interfaces when available, so I don't think this'll be problematic.
Regards,
Nicolas
RFC PATCH v2 4/4 sensehat: Add device tree overlay (do not merge)
In reply to Charles MirabileThis patch is an RFC. The overlay included here is suitable for the Sense HAT but we are not sure where overlays like this should go, or if we should even include it, because the Raspberry Pi kernel tree already includes that device tree blob that this was based on and most distributions for the Raspberry Pi include those blobs.
The overlay is just in the root of the source tree since we did not know where to put it so this patch should not be merged in this state.
--- sensehat.dtbs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 sensehat.dtbs
sensehat.dtbs new file +51 -0
@@ -0,0 +1,51 @@ +/dts-v1/; + +/ { + compatible = "brcm,bcm2835"; + + fragment@0 { + target = <0xffffffff>; + + __overlay__ { + #address-cells = <0x01>; + #size-cells = <0x00>; + status = "okay"; + + sensehat@46 { + compatible = "raspberrypi,sensehat"; + reg = <0x46>; + keys-int-gpios = <0xffffffff 0x17 0x01>; + status = "okay"; + }; + + lsm9ds1-magn@1c { + compatible = "st,lsm9ds1-magn"; + reg = <0x1c>; + status = "okay"; + }; + + lsm9ds1-accel6a { + compatible = "st,lsm9ds1-accel"; + reg = <0x6a>; + status = "okay"; + }; + + lps25h-press@5c { + compatible = "st,lps25h-press"; + reg = <0x5c>; + status = "okay"; + }; + + hts221-humid@5f { + compatible = "st,hts221-humid\0st,hts221"; + reg = <0x5f>; + status = "okay"; + }; + }; + }; + + __fixups__ { + i2c1 = "/fragment@0:target:0"; + gpio = "/fragment@0/__overlay__/sensehat@46:keys-int-gpios:0"; + }; +};
Signature · 1 line
2.27.0
Re: [RFC PATCH v2 4/4] sensehat: Add device tree overlay (do not merge)
Hi Charles,
Quoted 6 lines · Charles Mirabile
This patch is an RFC. The overlay included here is suitable for the Sense HAT but we are not sure where overlays like this should go, or if we should even include it, because the Raspberry Pi kernel tree already includes that device tree blob that this was based on and most distributions for the Raspberry Pi include those blobs.
Quoted 7 lines
The overlay is just in the root of the source tree since we did not know where to put it so this patch should not be merged in this state.
Signed-off-by Charles Mirabile <[email protected]>Signed-off-by Mwesigwa Guma <[email protected]>Signed-off-by Joel Savitz <[email protected]>---
Once we get this merged, and since upstream doesn't yet mantain overlays, I suggest you merge this overlay in the downstream rpi kernel repo, who actively maintain them. AFAIK a number of distros use that repo as the base for their devicetrees.
That said, I think providing a devicetree binding for this driver is a must for it to be merged (in yaml form).
Regards,
Nicolas
Re: [RFC PATCH v2 4/4] sensehat: Add device tree overlay (do not merge)
In reply to Charles MirabileQuoted 53 lines · Charles Mirabile
This patch is an RFC. The overlay included here is suitable for the Sense HAT but we are not sure where overlays like this should go, or if we should even include it, because the Raspberry Pi kernel tree already includes that device tree blob that this was based on and most distributions for the Raspberry Pi include those blobs.
The overlay is just in the root of the source tree since we did not know where to put it so this patch should not be merged in this state.
Signed-off-by Charles Mirabile <[email protected]>Signed-off-by Mwesigwa Guma <[email protected]>Signed-off-by Joel Savitz <[email protected]>--- sensehat.dtbs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)create mode 100644 sensehat.dtbs
sensehat.dtbs new file +30
-0@@ -0,0 +1,51 @@ +/dts-v1/; + +/ { + compatible = "brcm,bcm2835"; + + fragment@0 { + target = <0xffffffff>; + + __overlay__ { + #address-cells = <0x01>; + #size-cells = <0x00>; + status = "okay"; + + sensehat@46 { + compatible = "raspberrypi,sensehat"; + reg = <0x46>; + keys-int-gpios = <0xffffffff 0x17 0x01>; + status = "okay"; + }; + + lsm9ds1-magn@1c { + compatible = "st,lsm9ds1-magn"; + reg = <0x1c>; + status = "okay"; + }; + + lsm9ds1-accel6a { + compatible = "st,lsm9ds1-accel"; + reg = <0x6a>; + status = "okay";
sensehat.dtbs line 40
Quoted 10 lines
sensehat.dtbs new file +10
-0+ }; + + lps25h-press@5c { + compatible = "st,lps25h-press"; + reg = <0x5c>; + status = "okay"; + }; + + hts221-humid@5f { + compatible = "st,hts221-humid\0st,hts221";
Isn't "st,hts221" good enough?
sensehat.dtbs line 51
Quoted 12 lines
sensehat.dtbs new file +11
-0+ reg = <0x5f>; + status = "okay"; + }; + }; + }; + + __fixups__ { + i2c1 = "/fragment@0:target:0"; + gpio = "/fragment@0/__overlay__/sensehat@46:keys-int-gpios:0"; + }; +};
As Nicolas said, yaml bindings are a must. The overlay can be added as for convenience to the series but it should be made clear that it is not supposed to be upstreamed.
Regards,
Matthias