All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nicolas Saenz Julienne <nsaenzju@redhat.com>
To: Charles Mirabile <cmirabil@redhat.com>, linux-kernel@vger.kernel.org
Cc: Serge Schneider <serge@raspberrypi.org>,
	Stefan Wahren <stefan.wahren@i2se.com>,
	Mattias Brugger <mbrugger@suse.com>,
	 linux-rpi-kernel@lists.infradead.org,
	linux-arm-kernel@lists.infradead.org,
	fedora-rpi@googlegroups.com,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	 linux-input@vger.kernel.org, Mwesigwa Guma <mguma@redhat.com>,
	Joel Savitz <jsavitz@redhat.com>
Subject: Re: [PATCH 2/6] drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick driver
Date: Tue, 08 Feb 2022 12:44:03 +0100	[thread overview]
Message-ID: <9e55f768356892fcf10de3241419f97124efd4f7.camel@redhat.com> (raw)
In-Reply-To: <20220203002521.162878-3-cmirabil@redhat.com>

On Wed, 2022-02-02 at 19:25 -0500, Charles Mirabile wrote:
> 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: Mwesigwa Guma <mguma@redhat.com>
> Signed-off-by: Mwesigwa Guma <mguma@redhat.com>
> Co-developed-by: Joel Savitz <jsavitz@redhat.com>
> Signed-off-by: Joel Savitz <jsavitz@redhat.com>
> Signed-off-by: Charles Mirabile <cmirabil@redhat.com>
> ---
>  drivers/input/joystick/Kconfig             |  11 ++
>  drivers/input/joystick/Makefile            |   1 +
>  drivers/input/joystick/sensehat-joystick.c | 128 +++++++++++++++++++++
>  3 files changed, 140 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..a688cc8fbde3
> --- /dev/null
> +++ b/drivers/input/joystick/sensehat-joystick.c
> @@ -0,0 +1,128 @@
> +// 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/of_irq.h>
> +#include <linux/property.h>
> +
> +struct sensehat_joystick {
> +	struct platform_device *pdev;
> +	struct input_dev *keys_dev;
> +	unsigned long prev_states;
> +	u32 joystick_register;
> +};
> +
> +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;
> +	struct regmap *regmap =
> +		dev_get_regmap(sensehat_joystick->pdev->dev.parent, NULL);

I think it's better to get this during probe and save it in 'struct
sensehat_joystick'. You'll avoid having to search for it everytime.

> +	unsigned long curr_states, changes;
> +
> +	error = regmap_read(regmap, sensehat_joystick->joystick_register,
> +			    &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;
> +	struct sensehat_joystick *sensehat_joystick = devm_kzalloc(
> +		&pdev->dev, sizeof(*sensehat_joystick), GFP_KERNEL);
> +
> +	sensehat_joystick->pdev = pdev;
> +	sensehat_joystick->keys_dev = devm_input_allocate_device(&pdev->dev);
> +	if (!sensehat_joystick->keys_dev) {
> +		dev_err(&pdev->dev, "Could not allocate input device.\n");
> +		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.\n");
> +		return error;
> +	}
> +
> +	error = device_property_read_u32(&pdev->dev, "reg",
> +					 &sensehat_joystick->joystick_register);
> +	if (error) {
> +		dev_err(&pdev->dev, "Could not read register property.\n");
> +		return error;
> +	}

I don't think you really need to pass this information through the devicetree.
It's just the address to a register, not a HW design detail (i.e. how the board
is layout). Using a define is good enough.

> +
> +	error = devm_request_threaded_irq(&pdev->dev,
> +					  of_irq_get(pdev->dev.of_node, 0),

Use platform_get_irq() and remember the function may fail, both unrecoverable
errors and EPROBE_DEFER when the irqchip isn't ready yet.

> +					  NULL, sensehat_joystick_report,
> +					  IRQF_ONESHOT, "keys",
> +					  sensehat_joystick);
> +
> +	if (error) {
> +		dev_err(&pdev->dev, "IRQ request failed.\n");
> +		return error;
> +	}

nit: space here

> +	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("Serge Schneider <serge@raspberrypi.org>");
> +MODULE_LICENSE("GPL");

-- 
Nicolás Sáenz


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

WARNING: multiple messages have this Message-ID (diff)
From: Nicolas Saenz Julienne <nsaenzju@redhat.com>
To: Charles Mirabile <cmirabil@redhat.com>, linux-kernel@vger.kernel.org
Cc: Serge Schneider <serge@raspberrypi.org>,
	Stefan Wahren <stefan.wahren@i2se.com>,
	Mattias Brugger <mbrugger@suse.com>,
	linux-rpi-kernel@lists.infradead.org,
	linux-arm-kernel@lists.infradead.org,
	fedora-rpi@googlegroups.com,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	linux-input@vger.kernel.org, Mwesigwa Guma <mguma@redhat.com>,
	Joel Savitz <jsavitz@redhat.com>
Subject: Re: [PATCH 2/6] drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick driver
Date: Tue, 08 Feb 2022 12:44:03 +0100	[thread overview]
Message-ID: <9e55f768356892fcf10de3241419f97124efd4f7.camel@redhat.com> (raw)
In-Reply-To: <20220203002521.162878-3-cmirabil@redhat.com>

On Wed, 2022-02-02 at 19:25 -0500, Charles Mirabile wrote:
> 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: Mwesigwa Guma <mguma@redhat.com>
> Signed-off-by: Mwesigwa Guma <mguma@redhat.com>
> Co-developed-by: Joel Savitz <jsavitz@redhat.com>
> Signed-off-by: Joel Savitz <jsavitz@redhat.com>
> Signed-off-by: Charles Mirabile <cmirabil@redhat.com>
> ---
>  drivers/input/joystick/Kconfig             |  11 ++
>  drivers/input/joystick/Makefile            |   1 +
>  drivers/input/joystick/sensehat-joystick.c | 128 +++++++++++++++++++++
>  3 files changed, 140 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..a688cc8fbde3
> --- /dev/null
> +++ b/drivers/input/joystick/sensehat-joystick.c
> @@ -0,0 +1,128 @@
> +// 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/of_irq.h>
> +#include <linux/property.h>
> +
> +struct sensehat_joystick {
> +	struct platform_device *pdev;
> +	struct input_dev *keys_dev;
> +	unsigned long prev_states;
> +	u32 joystick_register;
> +};
> +
> +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;
> +	struct regmap *regmap =
> +		dev_get_regmap(sensehat_joystick->pdev->dev.parent, NULL);

I think it's better to get this during probe and save it in 'struct
sensehat_joystick'. You'll avoid having to search for it everytime.

> +	unsigned long curr_states, changes;
> +
> +	error = regmap_read(regmap, sensehat_joystick->joystick_register,
> +			    &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;
> +	struct sensehat_joystick *sensehat_joystick = devm_kzalloc(
> +		&pdev->dev, sizeof(*sensehat_joystick), GFP_KERNEL);
> +
> +	sensehat_joystick->pdev = pdev;
> +	sensehat_joystick->keys_dev = devm_input_allocate_device(&pdev->dev);
> +	if (!sensehat_joystick->keys_dev) {
> +		dev_err(&pdev->dev, "Could not allocate input device.\n");
> +		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.\n");
> +		return error;
> +	}
> +
> +	error = device_property_read_u32(&pdev->dev, "reg",
> +					 &sensehat_joystick->joystick_register);
> +	if (error) {
> +		dev_err(&pdev->dev, "Could not read register property.\n");
> +		return error;
> +	}

I don't think you really need to pass this information through the devicetree.
It's just the address to a register, not a HW design detail (i.e. how the board
is layout). Using a define is good enough.

> +
> +	error = devm_request_threaded_irq(&pdev->dev,
> +					  of_irq_get(pdev->dev.of_node, 0),

Use platform_get_irq() and remember the function may fail, both unrecoverable
errors and EPROBE_DEFER when the irqchip isn't ready yet.

> +					  NULL, sensehat_joystick_report,
> +					  IRQF_ONESHOT, "keys",
> +					  sensehat_joystick);
> +
> +	if (error) {
> +		dev_err(&pdev->dev, "IRQ request failed.\n");
> +		return error;
> +	}

nit: space here

> +	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("Serge Schneider <serge@raspberrypi.org>");
> +MODULE_LICENSE("GPL");

-- 
Nicolás Sáenz


  reply	other threads:[~2022-02-08 11:45 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-03  0:25 [PATCH 0/6] Raspberry Pi Sense HAT driver Charles Mirabile
2022-02-03  0:25 ` Charles Mirabile
2022-02-03  0:25 ` [PATCH 1/6] drivers/mfd: sensehat: Add Raspberry Pi Sense HAT to simple_mfd_i2c Charles Mirabile
2022-02-03  0:25   ` Charles Mirabile
2022-02-08 11:23   ` Nicolas Saenz Julienne
2022-02-08 11:23     ` Nicolas Saenz Julienne
2022-02-03  0:25 ` [PATCH 2/6] drivers/input/joystick: sensehat: Raspberry Pi Sense HAT joystick driver Charles Mirabile
2022-02-03  0:25   ` Charles Mirabile
2022-02-08 11:44   ` Nicolas Saenz Julienne [this message]
2022-02-08 11:44     ` Nicolas Saenz Julienne
2022-02-03  0:25 ` [PATCH 3/6] drivers/auxdisplay: sensehat: Raspberry Pi Sense HAT display driver Charles Mirabile
2022-02-03  0:25   ` Charles Mirabile
2022-02-08 12:36   ` Nicolas Saenz Julienne
2022-02-08 12:36     ` Nicolas Saenz Julienne
2022-02-08 14:00   ` Miguel Ojeda
2022-02-08 14:00     ` Miguel Ojeda
2022-02-03  0:25 ` [PATCH 4/6] dt-bindings: mfd: sensehat: Add Raspberry Pi Sense HAT schema Charles Mirabile
2022-02-03  0:25   ` Charles Mirabile
2022-02-03 13:50   ` Rob Herring
2022-02-03 13:50     ` Rob Herring
2022-02-03  0:25 ` [PATCH 5/6] MAINTAINERS: Add sensehat driver authors to MAINTAINERS Charles Mirabile
2022-02-03  0:25   ` Charles Mirabile
2022-02-03  0:25 ` [PATCH 6/6] DO NOT MERGE: full sensehat device tree overlay for raspberry pi 4 Charles Mirabile
2022-02-03  0:25   ` Charles Mirabile

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9e55f768356892fcf10de3241419f97124efd4f7.camel@redhat.com \
    --to=nsaenzju@redhat.com \
    --cc=cmirabil@redhat.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=fedora-rpi@googlegroups.com \
    --cc=jsavitz@redhat.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rpi-kernel@lists.infradead.org \
    --cc=mbrugger@suse.com \
    --cc=mguma@redhat.com \
    --cc=serge@raspberrypi.org \
    --cc=stefan.wahren@i2se.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.