2022-04-20 04:46:38

by Charles Mirabile

[permalink] [raw]
Subject: [PATCH v9 0/6] 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 and communicate 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 the test folder in
this repo: https://github.com/underground-software/sensehat.git
- sensehat_joystick_test logs the input events from the
joystick to the console
- sensehat_display_test displays various solid colors on
the LED panel.
- full_sensehat_test displays a single lit cell that can be
moved with the joystick. Pressing the joystick ends the
program.

For more information about the Sense HAT, visit:
https://www.raspberrypi.org/products/sense-hat/

Changes since v8:
- Fixed a few small things in the device tree bindings.
I forgot to include the fact that Rob Herring had already
reviewed them when I posted v8, so Krzysztof Kozlowski
jumped in and offered his input. The suggestions were good
so we decided to include them. Sorry for the confusion.
- Corrected a typo in the description of the display driver.
The format of the special file used by the driver was changed
in v8 and this was described in the cover letter of the patch,
but not in the actual note attached to that patch. This is now
fixed.

Co-developed-by: Daniel Bauman <[email protected]>
Signed-off-by: Daniel Bauman <[email protected]>
Co-developed-by: Mwesigwa Guma <[email protected]>
Signed-off-by: Mwesigwa Guma <[email protected]>
Co-developed-by: Joel Savitz <[email protected]>
Signed-off-by: Joel Savitz <[email protected]>
Signed-off-by: Charles Mirabile <[email protected]>

Charles Mirabile (6):
drivers/mfd: sensehat: Add Raspberry Pi Sense HAT to simple_mfd_i2c
drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick
driver
drivers/auxdisplay: sensehat: Raspberry Pi Sense HAT display driver
dt-bindings: mfd: sensehat: Add Raspberry Pi Sense HAT schema
MAINTAINERS: Add sensehat driver authors to MAINTAINERS
DO NOT MERGE: full sensehat device tree overlay for raspberry pi 4

.../raspberrypi,sensehat-display.yaml | 27 +++
.../input/raspberrypi,sensehat-joystick.yaml | 32 +++
.../bindings/mfd/raspberrypi,sensehat.yaml | 58 +++++
MAINTAINERS | 11 +
drivers/auxdisplay/Kconfig | 8 +
drivers/auxdisplay/Makefile | 1 +
drivers/auxdisplay/sensehat-display.c | 205 ++++++++++++++++++
drivers/input/joystick/Kconfig | 11 +
drivers/input/joystick/Makefile | 1 +
drivers/input/joystick/sensehat-joystick.c | 137 ++++++++++++
drivers/mfd/simple-mfd-i2c.c | 1 +
sensehat.dtbs | 52 +++++
12 files changed, 544 insertions(+)
create mode 100644 Documentation/devicetree/bindings/auxdisplay/raspberrypi,sensehat-display.yaml
create mode 100644 Documentation/devicetree/bindings/input/raspberrypi,sensehat-joystick.yaml
create mode 100644 Documentation/devicetree/bindings/mfd/raspberrypi,sensehat.yaml
create mode 100644 drivers/auxdisplay/sensehat-display.c
create mode 100644 drivers/input/joystick/sensehat-joystick.c
create mode 100644 sensehat.dtbs

--
2.31.1


2022-04-20 13:08:30

by Charles Mirabile

[permalink] [raw]
Subject: [PATCH v9 4/6] dt-bindings: mfd: sensehat: Add Raspberry Pi Sense HAT schema

This patch adds the device tree bindings for the Sense HAT
and each of its children devices in yaml form.

Co-developed-by: Mwesigwa Guma <[email protected]>
Signed-off-by: Mwesigwa Guma <[email protected]>
Co-developed-by: Joel Savitz <[email protected]>
Signed-off-by: Joel Savitz <[email protected]>
Signed-off-by: Charles Mirabile <[email protected]>
---
.../raspberrypi,sensehat-display.yaml | 27 +++++++++
.../input/raspberrypi,sensehat-joystick.yaml | 32 ++++++++++
.../bindings/mfd/raspberrypi,sensehat.yaml | 58 +++++++++++++++++++
3 files changed, 117 insertions(+)
create mode 100644 Documentation/devicetree/bindings/auxdisplay/raspberrypi,sensehat-display.yaml
create mode 100644 Documentation/devicetree/bindings/input/raspberrypi,sensehat-joystick.yaml
create mode 100644 Documentation/devicetree/bindings/mfd/raspberrypi,sensehat.yaml

diff --git a/Documentation/devicetree/bindings/auxdisplay/raspberrypi,sensehat-display.yaml b/Documentation/devicetree/bindings/auxdisplay/raspberrypi,sensehat-display.yaml
new file mode 100644
index 000000000000..5e41d6b7817d
--- /dev/null
+++ b/Documentation/devicetree/bindings/auxdisplay/raspberrypi,sensehat-display.yaml
@@ -0,0 +1,27 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/auxdisplay/raspberrypi,sensehat-display.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Raspberry Pi Sensehat Display
+
+maintainers:
+ - Charles Mirabile <[email protected]>
+ - Mwesigwa Guma <[email protected]>
+ - Joel Savitz <[email protected]>
+
+description:
+ This device is part of the sensehat multi function device.
+ For more information see ../mfd/raspberrypi,sensehat.yaml.
+
+ This device features a programmable 8x8 RGB LED matrix.
+
+properties:
+ compatible:
+ const: raspberrypi,sensehat-display
+
+required:
+ - compatible
+
+additionalProperties: false
diff --git a/Documentation/devicetree/bindings/input/raspberrypi,sensehat-joystick.yaml b/Documentation/devicetree/bindings/input/raspberrypi,sensehat-joystick.yaml
new file mode 100644
index 000000000000..2c8bc10e6436
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/raspberrypi,sensehat-joystick.yaml
@@ -0,0 +1,32 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/input/raspberrypi,sensehat-joystick.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Raspberry Pi Sensehat Joystick
+
+maintainers:
+ - Charles Mirabile <[email protected]>
+ - Mwesigwa Guma <[email protected]>
+ - Joel Savitz <[email protected]>
+
+description:
+ This device is part of the sensehat multi function device.
+ For more information see ../mfd/raspberrypi,sensehat.yaml.
+
+ This device features a five button joystick (up, down,left,
+ right, click)
+
+properties:
+ compatible:
+ const: raspberrypi,sensehat-joystick
+
+ interrupts:
+ maxItems: 1
+
+required:
+ - compatible
+ - interrupts
+
+additionalProperties: false
diff --git a/Documentation/devicetree/bindings/mfd/raspberrypi,sensehat.yaml b/Documentation/devicetree/bindings/mfd/raspberrypi,sensehat.yaml
new file mode 100644
index 000000000000..6ac43dfc0270
--- /dev/null
+++ b/Documentation/devicetree/bindings/mfd/raspberrypi,sensehat.yaml
@@ -0,0 +1,58 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mfd/raspberrypi,sensehat.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Raspberry Pi Sensehat
+
+maintainers:
+ - Charles Mirabile <[email protected]>
+ - Mwesigwa Guma <[email protected]>
+ - Joel Savitz <[email protected]>
+
+description:
+ The Raspberry Pi Sensehat is an addon board originally developed
+ for the Raspberry Pi that has a joystick and an 8x8 RGB LED display
+ as well as several environmental sensors. It connects via i2c and
+ a gpio for irq.
+
+properties:
+ compatible:
+ const: raspberrypi,sensehat
+
+ reg:
+ maxItems: 1
+
+ joystick:
+ $ref: /schemas/input/raspberrypi,sensehat-joystick.yaml#
+
+ display:
+ $ref: /schemas/auxdisplay/raspberrypi,sensehat-display.yaml#
+
+required:
+ - compatible
+ - reg
+ - joystick
+ - display
+
+additionalProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/interrupt-controller/irq.h>
+ i2c {
+ #address-cells = <1>;
+ #size-cells = <0>;
+ hat@46 {
+ compatible = "raspberrypi,sensehat";
+ reg = <0x46>;
+ display {
+ compatible = "raspberrypi,sensehat-display";
+ };
+ joystick {
+ compatible = "raspberrypi,sensehat-joystick";
+ interrupts = <23 IRQ_TYPE_EDGE_RISING>;
+ };
+ };
+ };
--
2.31.1

2022-04-21 04:14:32

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v9 4/6] dt-bindings: mfd: sensehat: Add Raspberry Pi Sense HAT schema

On 19/04/2022 22:51, Charles Mirabile wrote:
> This patch adds the device tree bindings for the Sense HAT
> and each of its children devices in yaml form.
>
> Co-developed-by: Mwesigwa Guma <[email protected]>
> Signed-off-by: Mwesigwa Guma <[email protected]>
> Co-developed-by: Joel Savitz <[email protected]>
> Signed-off-by: Joel Savitz <[email protected]>
> Signed-off-by: Charles Mirabile <[email protected]>
> ---


Reviewed-by: Krzysztof Kozlowski <[email protected]>


Best regards,
Krzysztof

2022-04-21 08:04:46

by Charles Mirabile

[permalink] [raw]
Subject: [PATCH v9 6/6] DO NOT MERGE: full sensehat device tree overlay for raspberry pi 4

This patch shold not be merged - dtbs files are not stored in the
kernel tree. We just provide this file so the code can be tested.

This overlay is suitable for testing the driver, it can be compiled with
dtc and put in the /boot/overlays/ folder then specified in config.txt
by putting the lines:

dtoverlay= #suppress loading of default overlay for HAT
dtoverlay=sensehat #load custom overlay

at the beginning before any other lines in config.txt

Co-developed-by: Mwesigwa Guma <[email protected]>
Signed-off-by: Mwesigwa Guma <[email protected]>
Co-developed-by: Joel Savitz <[email protected]>
Signed-off-by: Joel Savitz <[email protected]>
Signed-off-by: Charles Mirabile <[email protected]>
---
sensehat.dtbs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 52 insertions(+)
create mode 100644 sensehat.dtbs

diff --git a/sensehat.dtbs b/sensehat.dtbs
new file mode 100644
index 000000000000..9e5a6d9229b1
--- /dev/null
+++ b/sensehat.dtbs
@@ -0,0 +1,52 @@
+/dts-v1/;
+/plugin/;
+
+/ {
+ compatible = "brcm,bcm2835";
+};
+
+&i2c1 {
+ #address-cells = <0x01>;
+ #size-cells = <0x00>;
+ status = "okay";
+
+ sensehat@46 {
+ compatible = "raspberrypi,sensehat";
+ reg = <0x46>;
+ interrupt-parent = <&gpio>;
+ status = "okay";
+ display {
+ compatible = "raspberrypi,sensehat-display";
+ status = "okay";
+ };
+ joystick {
+ compatible = "raspberrypi,sensehat-joystick";
+ interrupts = <23 1>;
+ status = "okay";
+ };
+ };
+
+ lsm9ds1-magn@1c {
+ compatible = "st,lsm9ds1-magn";
+ reg = <0x1c>;
+ status = "okay";
+ };
+
+ lsm9ds1-accel@6a {
+ 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";
+ };
+};
--
2.31.1

2022-04-21 10:02:29

by Charles Mirabile

[permalink] [raw]
Subject: [PATCH v9 3/6] drivers/auxdisplay: sensehat: Raspberry Pi Sense HAT display driver

This patch adds the driver for the 8x8 RGB LED matrix display
on the Sense HAT. It appears as a character device named sense-hat
in /dev/. That special file is 192 bytes large and contains 64
RGB triplets (3 bytes each) one for each pixel in row major order.

Co-developed-by: Daniel Bauman <[email protected]>
Signed-off-by: Daniel Bauman <[email protected]>
Co-developed-by: Mwesigwa Guma <[email protected]>
Signed-off-by: Mwesigwa Guma <[email protected]>
Co-developed-by: Joel Savitz <[email protected]>
Signed-off-by: Joel Savitz <[email protected]>
Signed-off-by: Charles Mirabile <[email protected]>
---
drivers/auxdisplay/Kconfig | 8 +
drivers/auxdisplay/Makefile | 1 +
drivers/auxdisplay/sensehat-display.c | 205 ++++++++++++++++++++++++++
3 files changed, 214 insertions(+)
create mode 100644 drivers/auxdisplay/sensehat-display.c

diff --git a/drivers/auxdisplay/Kconfig b/drivers/auxdisplay/Kconfig
index 64012cda4d12..9bad1aade7a0 100644
--- a/drivers/auxdisplay/Kconfig
+++ b/drivers/auxdisplay/Kconfig
@@ -203,6 +203,14 @@ 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"
+ depends on I2C
+ select MFD_SIMPLE_MFD_I2C
+ 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
diff --git a/drivers/auxdisplay/Makefile b/drivers/auxdisplay/Makefile
index 6968ed4d3f0a..30b2b7934046 100644
--- a/drivers/auxdisplay/Makefile
+++ b/drivers/auxdisplay/Makefile
@@ -14,3 +14,4 @@ obj-$(CONFIG_HT16K33) += ht16k33.o
obj-$(CONFIG_PARPORT_PANEL) += panel.o
obj-$(CONFIG_LCD2S) += lcd2s.o
obj-$(CONFIG_LINEDISP) += line-display.o
+obj-$(CONFIG_SENSEHAT_DISPLAY) += sensehat-display.o
diff --git a/drivers/auxdisplay/sensehat-display.c b/drivers/auxdisplay/sensehat-display.c
new file mode 100644
index 000000000000..9faaee5cde8a
--- /dev/null
+++ b/drivers/auxdisplay/sensehat-display.c
@@ -0,0 +1,205 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Raspberry Pi Sense HAT 8x8 LED matrix display 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
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/string.h>
+#include <linux/mm.h>
+#include <linux/slab.h>
+#include <linux/uaccess.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/platform_device.h>
+#include <linux/mod_devicetable.h>
+#include <linux/miscdevice.h>
+#include <linux/regmap.h>
+#include <linux/property.h>
+
+#define DISPLAY_SMB_REG 0x00
+#define RGB_555_MASK 0x1f
+#define NUM_LEDS 8
+#define NUM_CHANNELS 3
+
+struct sensehat_display {
+ struct platform_device *pdev;
+ struct miscdevice mdev;
+ struct mutex rw_mtx;
+ u8 vmem[NUM_LEDS][NUM_LEDS][NUM_CHANNELS];
+ struct regmap *regmap;
+};
+
+#define VMEM_SIZE sizeof_field(struct sensehat_display, vmem)
+
+
+static int sensehat_update_display(struct sensehat_display *display)
+{
+ int i, j, k, ret;
+ u8 buff[NUM_LEDS][NUM_CHANNELS][NUM_LEDS];
+
+ for (i = 0; i < NUM_LEDS; ++i)
+ for (j = 0; j < NUM_LEDS; ++j)
+ for (k = 0; k < NUM_CHANNELS; ++k)
+ buff[i][k][j] =
+ display->vmem[i][j][k] & RGB_555_MASK;
+
+ ret = regmap_bulk_write(display->regmap, DISPLAY_SMB_REG, buff,
+ VMEM_SIZE);
+ if (ret < 0)
+ dev_err(&display->pdev->dev,
+ "Update to 8x8 LED matrix display failed");
+ return ret;
+}
+
+static loff_t sensehat_display_llseek(struct file *filp, loff_t offset,
+ int whence)
+{
+ return fixed_size_llseek(filp, offset, whence, VMEM_SIZE);
+}
+
+static ssize_t sensehat_display_read(struct file *filp, char __user *buf,
+ size_t count, loff_t *f_pos)
+{
+ struct sensehat_display *sensehat_display =
+ container_of(filp->private_data, struct sensehat_display, mdev);
+ ssize_t ret = -EFAULT;
+
+ if (*f_pos < 0 || *f_pos >= VMEM_SIZE)
+ return 0;
+ count = min_t(size_t, count, VMEM_SIZE - *f_pos);
+
+ if (mutex_lock_interruptible(&sensehat_display->rw_mtx))
+ return -ERESTARTSYS;
+ if (copy_to_user(buf, *f_pos + (u8 *)sensehat_display->vmem, count))
+ goto out;
+ *f_pos += count;
+ ret = count;
+out:
+ mutex_unlock(&sensehat_display->rw_mtx);
+ return ret;
+}
+
+static ssize_t sensehat_display_write(struct file *filp, const char __user *buf,
+ size_t count, loff_t *f_pos)
+{
+ struct sensehat_display *sensehat_display =
+ container_of(filp->private_data, struct sensehat_display, mdev);
+ int ret = -EFAULT;
+
+ if (*f_pos < 0 || *f_pos >= VMEM_SIZE)
+ return -EFBIG;
+ count = min_t(size_t, count, VMEM_SIZE - *f_pos);
+
+ if (mutex_lock_interruptible(&sensehat_display->rw_mtx))
+ return -ERESTARTSYS;
+ if (copy_from_user(*f_pos + (u8 *)sensehat_display->vmem, buf, count))
+ goto out;
+ ret = sensehat_update_display(sensehat_display);
+ if (ret < 0) {
+ ret = -EIO;
+ goto out;
+ }
+ *f_pos += count;
+ ret = count;
+out:
+ 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,
+ .write = sensehat_display_write,
+};
+
+static int sensehat_display_probe(struct platform_device *pdev)
+{
+ int ret;
+
+ struct sensehat_display *sensehat_display =
+ devm_kmalloc(&pdev->dev, sizeof(*sensehat_display), GFP_KERNEL);
+ if (!sensehat_display)
+ return -ENOMEM;
+
+ sensehat_display->pdev = pdev;
+
+ dev_set_drvdata(&pdev->dev, sensehat_display);
+
+ sensehat_display->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+ if (!sensehat_display->regmap) {
+ dev_err(&pdev->dev,
+ "unable to get sensehat regmap");
+ return -ENODEV;
+ }
+
+ memset(sensehat_display->vmem, 0, VMEM_SIZE);
+
+ mutex_init(&sensehat_display->rw_mtx);
+
+ ret = sensehat_update_display(sensehat_display);
+ if (ret < 0) {
+ dev_err(&pdev->dev,
+ "Could not communicate with sensehat");
+ return ret;
+ }
+
+ sensehat_display->mdev = (struct miscdevice){
+ .minor = MISC_DYNAMIC_MINOR,
+ .name = "sense-hat",
+ .mode = 0666,
+ .fops = &sensehat_display_fops,
+ };
+
+ ret = misc_register(&sensehat_display->mdev);
+ if (ret < 0) {
+ dev_err(&pdev->dev,
+ "Could not register 8x8 LED matrix display.");
+ return ret;
+ }
+
+ dev_info(&pdev->dev,
+ "8x8 LED matrix display registered with minor number %i",
+ sensehat_display->mdev.minor);
+
+ return 0;
+}
+
+static int sensehat_display_remove(struct platform_device *pdev)
+{
+ struct sensehat_display *sensehat_display = dev_get_drvdata(&pdev->dev);
+
+ misc_deregister(&sensehat_display->mdev);
+ return 0;
+}
+
+static const struct of_device_id sensehat_display_device_id[] = {
+ { .compatible = "raspberrypi,sensehat-display" },
+ {},
+};
+MODULE_DEVICE_TABLE(of, sensehat_display_device_id);
+
+static struct platform_driver sensehat_display_driver = {
+ .probe = sensehat_display_probe,
+ .remove = sensehat_display_remove,
+ .driver = {
+ .name = "sensehat-display",
+ .of_match_table = sensehat_display_device_id,
+ },
+};
+
+module_platform_driver(sensehat_display_driver);
+
+MODULE_DESCRIPTION("Raspberry Pi Sense HAT 8x8 LED matrix display driver");
+MODULE_AUTHOR("Charles Mirabile <[email protected]>");
+MODULE_AUTHOR("Serge Schneider <[email protected]>");
+MODULE_LICENSE("GPL");
--
2.31.1

2022-04-22 18:39:18

by Charles Mirabile

[permalink] [raw]
Subject: [PATCH v9 5/6] MAINTAINERS: Add sensehat driver authors to MAINTAINERS

This patch adds the driver authors to MAINAINERS.

Co-developed-by: Mwesigwa Guma <[email protected]>
Signed-off-by: Mwesigwa Guma <[email protected]>
Co-developed-by: Joel Savitz <[email protected]>
Signed-off-by: Joel Savitz <[email protected]>
Signed-off-by: Charles Mirabile <[email protected]>
---
MAINTAINERS | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 40fa1955ca3f..b93f060322a3 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17747,6 +17747,17 @@ F: Documentation/ABI/testing/sysfs-bus-iio-chemical-sunrise-co2
F: Documentation/devicetree/bindings/iio/chemical/senseair,sunrise.yaml
F: drivers/iio/chemical/sunrise_co2.c

+SENSEHAT DRIVER
+M: Charles Mirabile <[email protected]>
+M: Mwesigwa Guma <[email protected]>
+M: Joel Savitz <[email protected]>
+S: Maintained
+F: Documentation/devicetree/bindings/auxdisplay/raspberrypi,sensehat-display.yaml
+F: Documentation/devicetree/bindings/input/raspberrypi,sensehat-joystick.yaml
+F: Documentation/devicetree/bindings/mfd/raspberrypi,sensehat.yaml
+F: drivers/auxdisplay/sensehat-display.c
+F: drivers/input/joystick/sensehat-joystick.c
+
SENSIRION SCD30 CARBON DIOXIDE SENSOR DRIVER
M: Tomasz Duszynski <[email protected]>
S: Maintained
--
2.31.1

2022-04-22 19:07:05

by Charles Mirabile

[permalink] [raw]
Subject: [PATCH v9 2/6] drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick driver

This patch adds the driver for the Sense HAT joystick. It outputs BTN_DPAD
key events when moved in any of the four directions and the BTN_SELECT
event when depressed.

Co-developed-by: Daniel Bauman <[email protected]>
Signed-off-by: Daniel Bauman <[email protected]>
Co-developed-by: Mwesigwa Guma <[email protected]>
Signed-off-by: Mwesigwa Guma <[email protected]>
Co-developed-by: Joel Savitz <[email protected]>
Signed-off-by: Joel Savitz <[email protected]>
Signed-off-by: Charles Mirabile <[email protected]>
---
drivers/input/joystick/Kconfig | 11 ++
drivers/input/joystick/Makefile | 1 +
drivers/input/joystick/sensehat-joystick.c | 137 +++++++++++++++++++++
3 files changed, 149 insertions(+)
create mode 100644 drivers/input/joystick/sensehat-joystick.c

diff --git a/drivers/input/joystick/Kconfig b/drivers/input/joystick/Kconfig
index 3b23078bc7b5..505a032e2786 100644
--- a/drivers/input/joystick/Kconfig
+++ b/drivers/input/joystick/Kconfig
@@ -399,4 +399,15 @@ 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 INPUT && I2C
+ select MFD_SIMPLE_MFD_I2C
+ help
+ Say Y here if you want to enable the driver for the
+ the Raspberry Pi Sense HAT.
+
+ To compile this driver as a module, choose M here: the
+ module will be called sensehat_joystick.
+
endif
diff --git a/drivers/input/joystick/Makefile b/drivers/input/joystick/Makefile
index 5174b8aba2dd..39c8b5c6e5ae 100644
--- a/drivers/input/joystick/Makefile
+++ b/drivers/input/joystick/Makefile
@@ -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
diff --git a/drivers/input/joystick/sensehat-joystick.c b/drivers/input/joystick/sensehat-joystick.c
new file mode 100644
index 000000000000..6fed6004f464
--- /dev/null
+++ b/drivers/input/joystick/sensehat-joystick.c
@@ -0,0 +1,137 @@
+// 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
+ */
+
+#include <linux/module.h>
+#include <linux/input.h>
+#include <linux/i2c.h>
+#include <linux/interrupt.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/property.h>
+
+#define JOYSTICK_SMB_REG 0xf2
+
+struct sensehat_joystick {
+ struct platform_device *pdev;
+ struct input_dev *keys_dev;
+ unsigned long prev_states;
+ struct regmap *regmap;
+};
+
+static const unsigned int keymap[] = {
+ BTN_DPAD_DOWN, BTN_DPAD_RIGHT, BTN_DPAD_UP, BTN_SELECT, BTN_DPAD_LEFT,
+};
+
+static irqreturn_t sensehat_joystick_report(int n, void *cookie)
+{
+ int i, error, keys;
+ struct sensehat_joystick *sensehat_joystick = cookie;
+ unsigned long curr_states, changes;
+
+ error = regmap_read(sensehat_joystick->regmap, JOYSTICK_SMB_REG, &keys);
+ if (error < 0) {
+ dev_err(&sensehat_joystick->pdev->dev,
+ "Failed to read joystick state: %d", error);
+ return IRQ_NONE;
+ }
+ curr_states = keys;
+ bitmap_xor(&changes, &curr_states, &sensehat_joystick->prev_states,
+ ARRAY_SIZE(keymap));
+
+ for_each_set_bit(i, &changes, ARRAY_SIZE(keymap)) {
+ input_report_key(sensehat_joystick->keys_dev, keymap[i],
+ curr_states & BIT(i));
+ }
+
+ input_sync(sensehat_joystick->keys_dev);
+ sensehat_joystick->prev_states = keys;
+ return IRQ_HANDLED;
+}
+
+static int sensehat_joystick_probe(struct platform_device *pdev)
+{
+ int error, i, irq;
+ struct sensehat_joystick *sensehat_joystick = devm_kzalloc(
+ &pdev->dev, sizeof(*sensehat_joystick), GFP_KERNEL);
+ if (!sensehat_joystick)
+ return -ENOMEM;
+
+ sensehat_joystick->pdev = pdev;
+
+ sensehat_joystick->regmap = dev_get_regmap(sensehat_joystick->pdev->dev.parent, NULL);
+ if (!sensehat_joystick->regmap) {
+ dev_err(&pdev->dev,
+ "unable to get sensehat regmap");
+ return -ENODEV;
+ }
+
+
+ sensehat_joystick->keys_dev = devm_input_allocate_device(&pdev->dev);
+ if (!sensehat_joystick->keys_dev) {
+ dev_err(&pdev->dev, "Could not allocate input device.");
+ return -ENOMEM;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(keymap); i++)
+ set_bit(keymap[i], sensehat_joystick->keys_dev->keybit);
+
+ sensehat_joystick->keys_dev->name = "Raspberry Pi Sense HAT Joystick";
+ sensehat_joystick->keys_dev->phys = "sensehat-joystick/input0";
+ sensehat_joystick->keys_dev->id.bustype = BUS_I2C;
+ sensehat_joystick->keys_dev->evbit[0] =
+ BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
+
+ error = input_register_device(sensehat_joystick->keys_dev);
+ if (error) {
+ dev_err(&pdev->dev, "Could not register input device.");
+ return error;
+ }
+
+ irq = platform_get_irq(pdev, 0);
+ if (irq < 0) {
+ dev_err(&pdev->dev, "Could not retrieve interrupt request.");
+ return irq;
+ }
+
+ error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
+ sensehat_joystick_report,
+ IRQF_ONESHOT, "keys",
+ sensehat_joystick);
+
+ if (error) {
+ dev_err(&pdev->dev, "IRQ request failed.");
+ return error;
+ }
+
+ return 0;
+}
+
+static const struct of_device_id sensehat_joystick_device_id[] = {
+ { .compatible = "raspberrypi,sensehat-joystick" },
+ {},
+};
+MODULE_DEVICE_TABLE(of, sensehat_joystick_device_id);
+
+static struct platform_driver sensehat_joystick_driver = {
+ .probe = sensehat_joystick_probe,
+ .driver = {
+ .name = "sensehat-joystick",
+ .of_match_table = sensehat_joystick_device_id,
+ },
+};
+
+module_platform_driver(sensehat_joystick_driver);
+
+MODULE_DESCRIPTION("Raspberry Pi Sense HAT joystick driver");
+MODULE_AUTHOR("Charles Mirabile <[email protected]>");
+MODULE_AUTHOR("Serge Schneider <[email protected]>");
+MODULE_LICENSE("GPL");
--
2.31.1

2022-04-22 19:23:03

by Charles Mirabile

[permalink] [raw]
Subject: [PATCH v9 1/6] drivers/mfd: sensehat: Add Raspberry Pi Sense HAT to simple_mfd_i2c

This patch adds the compatible string for the Sense HAT device to
the list of compatible strings in the simple_mfd_i2c driver so that
it can match against the device and load its children and their drivers

Co-developed-by: Mwesigwa Guma <[email protected]>
Signed-off-by: Mwesigwa Guma <[email protected]>
Co-developed-by: Joel Savitz <[email protected]>
Signed-off-by: Joel Savitz <[email protected]>
Signed-off-by: Charles Mirabile <[email protected]>
---
drivers/mfd/simple-mfd-i2c.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/mfd/simple-mfd-i2c.c b/drivers/mfd/simple-mfd-i2c.c
index f4c8fc3ee463..c47f36581f3f 100644
--- a/drivers/mfd/simple-mfd-i2c.c
+++ b/drivers/mfd/simple-mfd-i2c.c
@@ -75,6 +75,7 @@ static const struct simple_mfd_data silergy_sy7636a = {
static const struct of_device_id simple_mfd_i2c_of_match[] = {
{ .compatible = "kontron,sl28cpld" },
{ .compatible = "silergy,sy7636a", .data = &silergy_sy7636a},
+ { .compatible = "raspberrypi,sensehat" },
{}
};
MODULE_DEVICE_TABLE(of, simple_mfd_i2c_of_match);
--
2.31.1

2022-04-22 20:25:30

by Miguel Ojeda

[permalink] [raw]
Subject: Re: [PATCH v9 0/6] Raspberry Pi Sense HAT driver

On Fri, Apr 22, 2022 at 1:02 AM Charles Mirabile <[email protected]> wrote:
>
> 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 and communicate via I2C.

Is there any feedback left to address? Which tree should take this?

Thanks for your work upstreaming this!

Cheers,
Miguel

2022-04-26 14:52:30

by Stefan Wahren

[permalink] [raw]
Subject: Re: [PATCH v9 0/6] Raspberry Pi Sense HAT driver

Am 26.04.22 um 12:18 schrieb Nicolas Saenz Julienne:
> Hi Charles, Miguel,
>
> On Mon, 2022-04-25 at 19:34 -0400, Charles Mirabile wrote:
>> Hi Miguel—
>>
>> Thanks for your help in reviewing this patch. I think all of the feedback is cleared up.
>>
>> I am not sure which tree this should go into, but maybe the Raspberry Pi tree maintained
>> by Nicolas Saenz Julienne (git://git.kernel.org/pub/scm/linux/kernel/git/nsaenz/linux-
>> rpi.git)? What do you think? Thanks!
> Since there is no code dependency between the different drivers/patches I
> suggest for each patch to go through its respective maintainer tree. Also, if
> we were to move the whole series through as single tree, it's important to have
> ACKs from all the subsystem maintainers.
Yes, we still need a Ack of Rob for the device tree bindings. Please
give him the common 2 weeks for a review.
>
> I'll do a last review during the week.
>
> Regards,
>

2022-04-26 18:32:20

by Stefan Wahren

[permalink] [raw]
Subject: Re: [PATCH v9 0/6] Raspberry Pi Sense HAT driver

Am 26.04.22 um 14:03 schrieb [email protected]:
> On 26/04/2022 12:48, Stefan Wahren wrote:
>> Am 26.04.22 um 12:18 schrieb Nicolas Saenz Julienne:
>>> Hi Charles, Miguel,
>>>
>>> On Mon, 2022-04-25 at 19:34 -0400, Charles Mirabile wrote:
>>>> Hi Miguel—
>>>>
>>>> Thanks for your help in reviewing this patch. I think all of the feedback is cleared up.
>>>>
>>>> I am not sure which tree this should go into, but maybe the Raspberry Pi tree maintained
>>>> by Nicolas Saenz Julienne (git://git.kernel.org/pub/scm/linux/kernel/git/nsaenz/linux-
>>>> rpi.git)? What do you think? Thanks!
>>> Since there is no code dependency between the different drivers/patches I
>>> suggest for each patch to go through its respective maintainer tree. Also, if
>>> we were to move the whole series through as single tree, it's important to have
>>> ACKs from all the subsystem maintainers.
>> Yes, we still need a Ack of Rob for the device tree bindings. Please give him the common 2 weeks for a review.
> You have one from Krzysztof on the bindings, that should be sufficient?
>
> https://docs.kernel.org/process/maintainers.html#open-firmware-and-flattened-device-tree-bindings
Yes, you are right
>
>>> I'll do a last review during the week.
>>>
>>> Regards,
>>>
> _______________________________________________
> linux-arm-kernel mailing list
> [email protected]
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

2022-04-26 19:33:18

by Nicolas Saenz Julienne

[permalink] [raw]
Subject: Re: [PATCH v9 0/6] Raspberry Pi Sense HAT driver

Hi Charles, Miguel,

On Mon, 2022-04-25 at 19:34 -0400, Charles Mirabile wrote:
> Hi Miguel—
>
> Thanks for your help in reviewing this patch. I think all of the feedback is cleared up.
>
> I am not sure which tree this should go into, but maybe the Raspberry Pi tree maintained
> by Nicolas Saenz Julienne (git://git.kernel.org/pub/scm/linux/kernel/git/nsaenz/linux-
> rpi.git)? What do you think? Thanks!

Since there is no code dependency between the different drivers/patches I
suggest for each patch to go through its respective maintainer tree. Also, if
we were to move the whole series through as single tree, it's important to have
ACKs from all the subsystem maintainers.

I'll do a last review during the week.

Regards,

--
Nicolás Sáenz

2022-05-06 21:56:16

by Pavel Machek

[permalink] [raw]
Subject: Re: [PATCH v9 3/6] drivers/auxdisplay: sensehat: Raspberry Pi Sense HAT display driver

Hi!

> This patch adds the driver for the 8x8 RGB LED matrix display
> on the Sense HAT. It appears as a character device named sense-hat
> in /dev/. That special file is 192 bytes large and contains 64
> RGB triplets (3 bytes each) one for each pixel in row major order.

Is that right interface? Should we treat it as a tiny framebuffer, instead?

Best regards,
Pavel
--
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

2022-05-31 10:29:29

by Randy Dunlap

[permalink] [raw]
Subject: Re: [PATCH v9 2/6] drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick driver

Hi--

On 4/19/22 13:51, Charles Mirabile wrote:
> diff --git a/drivers/input/joystick/Kconfig b/drivers/input/joystick/Kconfig
> index 3b23078bc7b5..505a032e2786 100644
> --- a/drivers/input/joystick/Kconfig
> +++ b/drivers/input/joystick/Kconfig
> @@ -399,4 +399,15 @@ 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 INPUT && I2C
> + select MFD_SIMPLE_MFD_I2C

Looks like this also needs
depends on HAS_IOMEM

since everything in drivers/mfd/Kconfig depends on HAS_IOMEM and
since this 'select' causes a kconfig warning when HAS_IOMEM is not set:

WARNING: unmet direct dependencies detected for MFD_SIMPLE_MFD_I2C
Depends on [n]: HAS_IOMEM [=n] && I2C [=y]
Selected by [y]:
- JOYSTICK_SENSEHAT [=y] && INPUT_JOYSTICK [=y] && INPUT [=y] && I2C [=y]


> + help
> + Say Y here if you want to enable the driver for the
> + the Raspberry Pi Sense HAT.
> +
> + To compile this driver as a module, choose M here: the
> + module will be called sensehat_joystick.
> +
> endif

Thanks.

2022-05-31 18:43:16

by Randy Dunlap

[permalink] [raw]
Subject: Re: [PATCH v9 2/6] drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick driver



On 5/30/22 18:57, Dmitry Torokhov wrote:
> Hi Randy,
>
> On Mon, May 30, 2022 at 06:26:26PM -0700, Randy Dunlap wrote:
>> Hi--
>>
>> On 4/19/22 13:51, Charles Mirabile wrote:
>>> diff --git a/drivers/input/joystick/Kconfig b/drivers/input/joystick/Kconfig
>>> index 3b23078bc7b5..505a032e2786 100644
>>> --- a/drivers/input/joystick/Kconfig
>>> +++ b/drivers/input/joystick/Kconfig
>>> @@ -399,4 +399,15 @@ 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 INPUT && I2C
>>> + select MFD_SIMPLE_MFD_I2C
>>
>> Looks like this also needs
>> depends on HAS_IOMEM
>>
>> since everything in drivers/mfd/Kconfig depends on HAS_IOMEM and
>> since this 'select' causes a kconfig warning when HAS_IOMEM is not set:
>>
>> WARNING: unmet direct dependencies detected for MFD_SIMPLE_MFD_I2C
>> Depends on [n]: HAS_IOMEM [=n] && I2C [=y]
>> Selected by [y]:
>> - JOYSTICK_SENSEHAT [=y] && INPUT_JOYSTICK [=y] && INPUT [=y] && I2C [=y]
>
> Do you mind sending a patch?

OK, will do.

--
~Randy

2022-06-01 06:59:37

by Dmitry Torokhov

[permalink] [raw]
Subject: Re: [PATCH v9 2/6] drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick driver

Hi Randy,

On Mon, May 30, 2022 at 06:26:26PM -0700, Randy Dunlap wrote:
> Hi--
>
> On 4/19/22 13:51, Charles Mirabile wrote:
> > diff --git a/drivers/input/joystick/Kconfig b/drivers/input/joystick/Kconfig
> > index 3b23078bc7b5..505a032e2786 100644
> > --- a/drivers/input/joystick/Kconfig
> > +++ b/drivers/input/joystick/Kconfig
> > @@ -399,4 +399,15 @@ 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 INPUT && I2C
> > + select MFD_SIMPLE_MFD_I2C
>
> Looks like this also needs
> depends on HAS_IOMEM
>
> since everything in drivers/mfd/Kconfig depends on HAS_IOMEM and
> since this 'select' causes a kconfig warning when HAS_IOMEM is not set:
>
> WARNING: unmet direct dependencies detected for MFD_SIMPLE_MFD_I2C
> Depends on [n]: HAS_IOMEM [=n] && I2C [=y]
> Selected by [y]:
> - JOYSTICK_SENSEHAT [=y] && INPUT_JOYSTICK [=y] && INPUT [=y] && I2C [=y]

Do you mind sending a patch?

Thanks.

--
Dmitry

2022-06-23 06:02:48

by Stefan Wahren

[permalink] [raw]
Subject: Re: [PATCH v9 0/6] Raspberry Pi Sense HAT driver

Hi Charles,

Am 19.04.22 um 22:51 schrieb Charles Mirabile:
> 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 and communicate 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 the test folder in
> this repo: https://github.com/underground-software/sensehat.git
> - sensehat_joystick_test logs the input events from the
> joystick to the console
> - sensehat_display_test displays various solid colors on
> the LED panel.
> - full_sensehat_test displays a single lit cell that can be
> moved with the joystick. Pressing the joystick ends the
> program.
>
> For more information about the Sense HAT, visit:
> https://www.raspberrypi.org/products/sense-hat/
>
> Changes since v8:
> - Fixed a few small things in the device tree bindings.
> I forgot to include the fact that Rob Herring had already
> reviewed them when I posted v8, so Krzysztof Kozlowski
> jumped in and offered his input. The suggestions were good
> so we decided to include them. Sorry for the confusion.
> - Corrected a typo in the description of the display driver.
> The format of the special file used by the driver was changed
> in v8 and this was described in the cover letter of the patch,
> but not in the actual note attached to that patch. This is now
> fixed.
>
> Co-developed-by: Daniel Bauman <[email protected]>
> Signed-off-by: Daniel Bauman <[email protected]>
> Co-developed-by: Mwesigwa Guma <[email protected]>
> Signed-off-by: Mwesigwa Guma <[email protected]>
> Co-developed-by: Joel Savitz <[email protected]>
> Signed-off-by: Joel Savitz <[email protected]>
> Signed-off-by: Charles Mirabile <[email protected]>
>
> Charles Mirabile (6):
> drivers/mfd: sensehat: Add Raspberry Pi Sense HAT to simple_mfd_i2c
> drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick
> driver
> drivers/auxdisplay: sensehat: Raspberry Pi Sense HAT display driver
> dt-bindings: mfd: sensehat: Add Raspberry Pi Sense HAT schema
> MAINTAINERS: Add sensehat driver authors to MAINTAINERS
> DO NOT MERGE: full sensehat device tree overlay for raspberry pi 4

since only the joystick patch has been applied, could you please send a
rebased v10 without the joystick driver?

Please take care of the fact that Florian is the new BCM2835 maintainer.

Thanks