All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC 0/2] input: driver for RPi's official 7" touchscreen
@ 2018-12-05 22:09 Nicolas Saenz Julienne
  2018-12-05 22:09 ` [PATCH RFC 1/2] input: add official Raspberry Pi's 7" touchscreen driver Nicolas Saenz Julienne
  2018-12-05 22:09 ` [PATCH RFC 2/2] Input: raspberrypi-ts - add devicetree binding documentation Nicolas Saenz Julienne
  0 siblings, 2 replies; 4+ messages in thread
From: Nicolas Saenz Julienne @ 2018-12-05 22:09 UTC (permalink / raw)
  To: linux-input
  Cc: linux-rpi-kernel, stefan.wahren, eric, agraf,
	Nicolas Saenz Julienne, Dmitry Torokhov, Rob Herring,
	Eugen Hristev, Hans de Goede, Jonathan Cameron, Masanari Iida,
	Zhu Yi, Corentin Labbe, devicetree, linux-kernel

This small series adds support for Raspberry pi's 7" touchscreen. Which
alongside with the backlight driver are the last devices needed to have
a functional touchscreen upstream.

With this setup the board's VC4 firmware takes care of communicating
with the touch chip and provides data though a shared memory area
provided by the driver. The driver takes care of polling the firmware
whenever at around 60Hz since there is no interrupt line available.

The 1.0 revision of the touchscreen is based on the ft5426 chip.
Technically, with some changes in edt-ft54x4.c we should be able to
access the data directly through I2C. Yet this bus is meant to be owned
by RPi's firmware and might access it anytime. For example, to
configure RPi's camera device. As sharing the bus master interface is
not possible a series of alternatives have been tested unsuccessfully
[1]. It seems that we'll be unable to access the chip directly in a
"clean" way which leaves us with this firmware based solution.

The driver was rewritten based on the one available on the downstream
Raspberry Pi kernel tree: https://github.com/raspberrypi/linux/.

This series is based on v4.20-rc5 and was tested on a RPi 3 B+.

[1] https://lists.infradead.org/pipermail/linux-rpi-kernel/2018-December/008444.html
===

Nicolas Saenz Julienne (2):
  input: add official Raspberry Pi's 7" touchscreen driver
  Input: raspberrypi-ts - add devicetree binding documentation

 .../touchscreen/raspberrypi,firmware-ts.txt   |  25 ++
 drivers/input/touchscreen/Kconfig             |  12 +
 drivers/input/touchscreen/Makefile            |   1 +
 drivers/input/touchscreen/raspberrypi-ts.c    | 252 ++++++++++++++++++
 4 files changed, 290 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/touchscreen/raspberrypi,firmware-ts.txt
 create mode 100644 drivers/input/touchscreen/raspberrypi-ts.c

-- 
2.19.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH RFC 1/2] input: add official Raspberry Pi's 7" touchscreen driver
  2018-12-05 22:09 [PATCH RFC 0/2] input: driver for RPi's official 7" touchscreen Nicolas Saenz Julienne
@ 2018-12-05 22:09 ` Nicolas Saenz Julienne
  2018-12-09  4:43   ` Dmitry Torokhov
  2018-12-05 22:09 ` [PATCH RFC 2/2] Input: raspberrypi-ts - add devicetree binding documentation Nicolas Saenz Julienne
  1 sibling, 1 reply; 4+ messages in thread
From: Nicolas Saenz Julienne @ 2018-12-05 22:09 UTC (permalink / raw)
  To: Henrik Rydberg
  Cc: linux-rpi-kernel, stefan.wahren, eric, agraf,
	Nicolas Saenz Julienne, Dmitry Torokhov, Corentin Labbe,
	Rob Herring, Hans de Goede, Masanari Iida, Zhu Yi, Eugen Hristev,
	Jonathan Cameron, Hans-Christian Noren Egtvedt, Heiko Schocher,
	linux-kernel, linux-input

Adds support to Raspberry Pi's 7" touchscreen device. Instead of using
a conventional bus all information is copied into a memory mapped area
by RPi's VC4 firmware.

Based on the driver found in RPi's downstream kernel repository.

Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
---
 drivers/input/touchscreen/Kconfig          |  12 +
 drivers/input/touchscreen/Makefile         |   1 +
 drivers/input/touchscreen/raspberrypi-ts.c | 252 +++++++++++++++++++++
 3 files changed, 265 insertions(+)
 create mode 100644 drivers/input/touchscreen/raspberrypi-ts.c

diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
index 2a80675cfd94..8d0fcb3dc8a8 100644
--- a/drivers/input/touchscreen/Kconfig
+++ b/drivers/input/touchscreen/Kconfig
@@ -696,6 +696,18 @@ config TOUCHSCREEN_EDT_FT5X06
 	  To compile this driver as a module, choose M here: the
 	  module will be called edt-ft5x06.
 
+config TOUCHSCREEN_RASPBERRYPI_TS
+	tristate "Raspberry Pi's firmware base touch screen support"
+	depends on OF
+	help
+	  Say Y here if you have the official Raspberry Pi 7' touchscren
+	  connected on your system.
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called raspberrypi-ts.
+
 config TOUCHSCREEN_MIGOR
 	tristate "Renesas MIGO-R touchscreen"
 	depends on (SH_MIGOR || COMPILE_TEST) && I2C
diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
index 5911a4190cd2..3eccb1925e89 100644
--- a/drivers/input/touchscreen/Makefile
+++ b/drivers/input/touchscreen/Makefile
@@ -109,3 +109,4 @@ obj-$(CONFIG_TOUCHSCREEN_ZET6223)	+= zet6223.o
 obj-$(CONFIG_TOUCHSCREEN_ZFORCE)	+= zforce_ts.o
 obj-$(CONFIG_TOUCHSCREEN_COLIBRI_VF50)	+= colibri-vf50-ts.o
 obj-$(CONFIG_TOUCHSCREEN_ROHM_BU21023)	+= rohm_bu21023.o
+obj-$(CONFIG_TOUCHSCREEN_RASPBERRYPI_TS)	+= raspberrypi-ts.o
diff --git a/drivers/input/touchscreen/raspberrypi-ts.c b/drivers/input/touchscreen/raspberrypi-ts.c
new file mode 100644
index 000000000000..9d29411a5674
--- /dev/null
+++ b/drivers/input/touchscreen/raspberrypi-ts.c
@@ -0,0 +1,252 @@
+/*
+ * Raspberry Pi 3 firmware based touchscreen driver
+ *
+ * Copyright (C) 2015, 2017 Raspberry Pi
+ * Copyright (C) 2018 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/bitops.h>
+#include <linux/dma-mapping.h>
+#include <linux/platform_device.h>
+#include <linux/input.h>
+#include <linux/input/mt.h>
+#include <linux/input/touchscreen.h>
+#include <soc/bcm2835/raspberrypi-firmware.h>
+
+#define RPI_TS_DEFAULT_WIDTH	800
+#define RPI_TS_DEFAULT_HEIGHT	480
+
+#define RPI_TS_MAX_SUPPORTED_POINTS	10
+
+#define RPI_TS_FTS_TOUCH_DOWN		0
+#define RPI_TS_FTS_TOUCH_CONTACT	2
+
+struct rpi_ts {
+	struct input_dev *input;
+	struct touchscreen_properties prop;
+
+	void __iomem *ts_base;
+	dma_addr_t bus_addr;
+
+	struct delayed_work work;
+	int known_ids;
+};
+
+struct rpi_ts_regs {
+	uint8_t device_mode;
+	uint8_t gesture_id;
+	uint8_t num_points;
+	struct rpi_ts_touch {
+		uint8_t xh;
+		uint8_t xl;
+		uint8_t yh;
+		uint8_t yl;
+		uint8_t pressure; /* Not supported */
+		uint8_t area;     /* Not supported */
+	} point[RPI_TS_MAX_SUPPORTED_POINTS];
+};
+
+/*
+ * This process polls the memory based register copy of the touch screen chip
+ * registers using the number of points register to know whether the copy has
+ * been updated (we write 99 to the memory copy, the GPU will write between 0 -
+ * 10 points)
+ */
+static void rpi_ts_work(struct work_struct *work)
+{
+	struct rpi_ts *ts = container_of(work, struct rpi_ts, work.work);
+	struct input_dev *input = ts->input;
+	struct rpi_ts_regs regs;
+	int modified_ids = 0;
+	int released_ids;
+	int event_type;
+	int touchid;
+	int x, y;
+	int i;
+
+	memcpy_fromio(&regs, ts->ts_base, sizeof(struct rpi_ts_regs));
+	iowrite8(99, ts->ts_base + offsetof(struct rpi_ts_regs, num_points));
+
+	if (regs.num_points == 99 ||
+	    (regs.num_points == 0 && ts->known_ids == 0))
+	    goto out;
+
+	for (i = 0; i < regs.num_points; i++) {
+		x = (((int)regs.point[i].xh & 0xf) << 8) + regs.point[i].xl;
+		y = (((int)regs.point[i].yh & 0xf) << 8) + regs.point[i].yl;
+		touchid = (regs.point[i].yh >> 4) & 0xf;
+		event_type = (regs.point[i].xh >> 6) & 0x03;
+
+		modified_ids |= BIT(touchid);
+
+		if (event_type == RPI_TS_FTS_TOUCH_DOWN ||
+		    event_type == RPI_TS_FTS_TOUCH_CONTACT) {
+			input_mt_slot(input, touchid);
+			input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
+			touchscreen_report_pos(input, &ts->prop, x, y, true);
+		}
+	}
+
+	released_ids = ts->known_ids & ~modified_ids;
+	for (i = 0; released_ids && i < RPI_TS_MAX_SUPPORTED_POINTS; i++) {
+		if (released_ids & BIT(i)) {
+			input_mt_slot(input, i);
+			input_mt_report_slot_state(input, MT_TOOL_FINGER, 0);
+			modified_ids &= ~(BIT(i));
+		}
+	}
+	ts->known_ids = modified_ids;
+
+	input_mt_report_pointer_emulation(ts->input, true);
+	input_sync(ts->input);
+
+out:
+	schedule_delayed_work(&ts->work, msecs_to_jiffies(17)); /* 60 fps */
+}
+
+static int rpi_ts_open(struct input_dev *dev)
+{
+	struct rpi_ts *ts = input_get_drvdata(dev);
+
+	schedule_delayed_work(&ts->work, 0);
+
+	return 0;
+}
+
+static void rpi_ts_close(struct input_dev *dev)
+{
+	struct rpi_ts *ts = input_get_drvdata(dev);
+
+	cancel_delayed_work_sync(&ts->work);
+}
+
+static int rpi_ts_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	struct device_node *fw_node;
+	struct input_dev *input;
+	struct rpi_firmware *fw;
+	struct rpi_ts *ts;
+	u32 touchbuf;
+	int err = 0;
+
+	fw_node = of_get_parent(np);
+	if (!fw_node) {
+		dev_err(dev, "Missing firmware node\n");
+		return -ENOENT;
+	}
+
+	fw = rpi_firmware_get(fw_node);
+	if (!fw)
+		return -EPROBE_DEFER;
+
+	ts = devm_kzalloc(dev, sizeof(struct rpi_ts), GFP_KERNEL);
+	if (!ts) {
+		dev_err(dev, "Failed to allocate memory\n");
+		return -ENOMEM;
+	}
+
+	input = input_allocate_device();
+	if (!input) {
+		dev_err(dev, "Failed to allocate input device\n");
+		return -ENOMEM;
+	}
+	ts->input = input;
+
+	ts->ts_base = dma_zalloc_coherent(dev, PAGE_SIZE, &ts->bus_addr,
+					  GFP_KERNEL);
+	if (!ts->ts_base) {
+		dev_err(dev, "failed to dma_alloc_coherent\n");
+		err = -ENOMEM;
+		goto undegister_input_device;
+	}
+
+	touchbuf = (u32)ts->bus_addr;
+	err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
+				    &touchbuf, sizeof(touchbuf));
+
+	if (err || touchbuf != 0) {
+		dev_warn(dev, "Failed to set touchbuf, trying to get err:%x\n",
+			 err);
+		goto clean_dma_buf;
+	}
+
+	INIT_DELAYED_WORK(&ts->work, rpi_ts_work);
+	platform_set_drvdata(pdev, ts);
+
+	input->name = "raspberrypi-ts";
+	input->id.bustype = BUS_HOST;
+	input->open = rpi_ts_open;
+	input->close = rpi_ts_close;
+	input->dev.parent = &pdev->dev;
+
+	__set_bit(EV_KEY, input->evbit);
+	__set_bit(EV_SYN, input->evbit);
+	__set_bit(EV_ABS, input->evbit);
+
+	input_set_abs_params(input, ABS_MT_POSITION_X, 0,
+			     RPI_TS_DEFAULT_WIDTH, 0, 0);
+	input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
+			     RPI_TS_DEFAULT_HEIGHT, 0, 0);
+	touchscreen_parse_properties(input, true, &ts->prop);
+
+	input_mt_init_slots(input, RPI_TS_MAX_SUPPORTED_POINTS,
+			    INPUT_MT_DIRECT);
+
+	input_set_drvdata(input, ts);
+
+	err = input_register_device(input);
+	if (err) {
+		dev_err(dev, "could not register input device, %d\n",
+			err);
+		goto clean_dma_buf;
+	}
+	return 0;
+
+clean_dma_buf:
+	dma_free_coherent(dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);
+undegister_input_device:
+	input_unregister_device(ts->input);
+
+	return err;
+}
+
+static int rpi_ts_remove(struct platform_device *pdev)
+{
+	struct rpi_ts *ts = (struct rpi_ts *)platform_get_drvdata(pdev);
+
+	input_unregister_device(ts->input);
+	dma_free_coherent(&pdev->dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);
+	return 0;
+}
+
+static const struct of_device_id rpi_ts_match[] = {
+	{ .compatible = "raspberrypi,firmware-ts", },
+	{},
+};
+MODULE_DEVICE_TABLE(of, rpi_ts_match);
+
+static struct platform_driver rpi_ts_driver = {
+	.driver = {
+		.name   = "raspberrypi-ts",
+		.owner  = THIS_MODULE,
+		.of_match_table = rpi_ts_match,
+	},
+	.probe          = rpi_ts_probe,
+	.remove         = rpi_ts_remove,
+};
+
+module_platform_driver(rpi_ts_driver);
+
+MODULE_AUTHOR("Gordon Hollingworth");
+MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
+MODULE_DESCRIPTION("Raspberry Pi 3 firmware based touchscreen driver");
+MODULE_LICENSE("GPL");
-- 
2.19.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH RFC 2/2] Input: raspberrypi-ts - add devicetree binding documentation
  2018-12-05 22:09 [PATCH RFC 0/2] input: driver for RPi's official 7" touchscreen Nicolas Saenz Julienne
  2018-12-05 22:09 ` [PATCH RFC 1/2] input: add official Raspberry Pi's 7" touchscreen driver Nicolas Saenz Julienne
@ 2018-12-05 22:09 ` Nicolas Saenz Julienne
  1 sibling, 0 replies; 4+ messages in thread
From: Nicolas Saenz Julienne @ 2018-12-05 22:09 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-rpi-kernel, stefan.wahren, eric, agraf,
	Nicolas Saenz Julienne, Dmitry Torokhov, Rob Herring,
	Mark Rutland, linux-input, devicetree

Adds device tree documentation for Raspberry Pi's official 7"
touchscreen. The firmware mailbox interface allows the ARM core to
control the device.

Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
---
 .../touchscreen/raspberrypi,firmware-ts.txt   | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/input/touchscreen/raspberrypi,firmware-ts.txt

diff --git a/Documentation/devicetree/bindings/input/touchscreen/raspberrypi,firmware-ts.txt b/Documentation/devicetree/bindings/input/touchscreen/raspberrypi,firmware-ts.txt
new file mode 100644
index 000000000000..d28189476279
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/touchscreen/raspberrypi,firmware-ts.txt
@@ -0,0 +1,25 @@
+Raspberry Pi 3 firmware based 7" touchscreen
+=====================================
+
+Required properties:
+ - compatible: "raspberrypi,firmware-ts"
+
+Optional properties:
+ - touchscreen-size-x: See touchscreen.txt
+ - touchscreen-size-y: See touchscreen.txt
+ - touchscreen-inverted-x: See touchscreen.txt
+ - touchscreen-inverted-y: See touchscreen.txt
+ - touchscreen-swapped-x-y: See touchscreen.txt
+
+Example:
+
+firmware: firmware-rpi {
+	compatible = "raspberrypi,bcm2835-firmware", "simple-bus";
+	mboxes = <&mailbox>;
+
+	ts: touchscreen {
+		compatible = "raspberrypi,firmware-ts";
+		touchscreen-size-x = <800>;
+		touchscreen-size-y = <480>;
+	};
+};
-- 
2.19.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH RFC 1/2] input: add official Raspberry Pi's 7" touchscreen driver
  2018-12-05 22:09 ` [PATCH RFC 1/2] input: add official Raspberry Pi's 7" touchscreen driver Nicolas Saenz Julienne
@ 2018-12-09  4:43   ` Dmitry Torokhov
  0 siblings, 0 replies; 4+ messages in thread
From: Dmitry Torokhov @ 2018-12-09  4:43 UTC (permalink / raw)
  To: Nicolas Saenz Julienne
  Cc: Henrik Rydberg, linux-rpi-kernel, stefan.wahren, eric, agraf,
	Corentin Labbe, Rob Herring, Hans de Goede, Masanari Iida,
	Zhu Yi, Eugen Hristev, Jonathan Cameron,
	Hans-Christian Noren Egtvedt, Heiko Schocher, linux-kernel,
	linux-input

Hi Nicolas,

On Wed, Dec 05, 2018 at 11:09:03PM +0100, Nicolas Saenz Julienne wrote:
> Adds support to Raspberry Pi's 7" touchscreen device. Instead of using
> a conventional bus all information is copied into a memory mapped area
> by RPi's VC4 firmware.
> 
> Based on the driver found in RPi's downstream kernel repository.
> 
> Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
> ---
>  drivers/input/touchscreen/Kconfig          |  12 +
>  drivers/input/touchscreen/Makefile         |   1 +
>  drivers/input/touchscreen/raspberrypi-ts.c | 252 +++++++++++++++++++++
>  3 files changed, 265 insertions(+)
>  create mode 100644 drivers/input/touchscreen/raspberrypi-ts.c
> 
> diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
> index 2a80675cfd94..8d0fcb3dc8a8 100644
> --- a/drivers/input/touchscreen/Kconfig
> +++ b/drivers/input/touchscreen/Kconfig
> @@ -696,6 +696,18 @@ config TOUCHSCREEN_EDT_FT5X06
>  	  To compile this driver as a module, choose M here: the
>  	  module will be called edt-ft5x06.
>  
> +config TOUCHSCREEN_RASPBERRYPI_TS
> +	tristate "Raspberry Pi's firmware base touch screen support"
> +	depends on OF

I think "depends on ASPBERRYPI_FIRMWARE || COMPILE_TEST" is more
precise dependency.

> +	help
> +	  Say Y here if you have the official Raspberry Pi 7' touchscren
> +	  connected on your system.
> +
> +	  If unsure, say N.
> +
> +	  To compile this driver as a module, choose M here: the
> +	  module will be called raspberrypi-ts.
> +
>  config TOUCHSCREEN_MIGOR
>  	tristate "Renesas MIGO-R touchscreen"
>  	depends on (SH_MIGOR || COMPILE_TEST) && I2C
> diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
> index 5911a4190cd2..3eccb1925e89 100644
> --- a/drivers/input/touchscreen/Makefile
> +++ b/drivers/input/touchscreen/Makefile
> @@ -109,3 +109,4 @@ obj-$(CONFIG_TOUCHSCREEN_ZET6223)	+= zet6223.o
>  obj-$(CONFIG_TOUCHSCREEN_ZFORCE)	+= zforce_ts.o
>  obj-$(CONFIG_TOUCHSCREEN_COLIBRI_VF50)	+= colibri-vf50-ts.o
>  obj-$(CONFIG_TOUCHSCREEN_ROHM_BU21023)	+= rohm_bu21023.o
> +obj-$(CONFIG_TOUCHSCREEN_RASPBERRYPI_TS)	+= raspberrypi-ts.o
> diff --git a/drivers/input/touchscreen/raspberrypi-ts.c b/drivers/input/touchscreen/raspberrypi-ts.c
> new file mode 100644
> index 000000000000..9d29411a5674
> --- /dev/null
> +++ b/drivers/input/touchscreen/raspberrypi-ts.c
> @@ -0,0 +1,252 @@
> +/*
> + * Raspberry Pi 3 firmware based touchscreen driver
> + *
> + * Copyright (C) 2015, 2017 Raspberry Pi
> + * Copyright (C) 2018 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.

Please use SPDX tag for the license.

> + */
> +
> +#include <linux/io.h>
> +#include <linux/slab.h>
> +#include <linux/module.h>
> +#include <linux/bitops.h>
> +#include <linux/dma-mapping.h>
> +#include <linux/platform_device.h>
> +#include <linux/input.h>
> +#include <linux/input/mt.h>
> +#include <linux/input/touchscreen.h>
> +#include <soc/bcm2835/raspberrypi-firmware.h>
> +
> +#define RPI_TS_DEFAULT_WIDTH	800
> +#define RPI_TS_DEFAULT_HEIGHT	480
> +
> +#define RPI_TS_MAX_SUPPORTED_POINTS	10
> +
> +#define RPI_TS_FTS_TOUCH_DOWN		0
> +#define RPI_TS_FTS_TOUCH_CONTACT	2
> +
> +struct rpi_ts {
> +	struct input_dev *input;

Since the only way is to poll, please use polled input device instead of
standard one. It will take care of scheduling polling, stopping it when
needed, etc.

> +	struct touchscreen_properties prop;
> +
> +	void __iomem *ts_base;
> +	dma_addr_t bus_addr;
> +
> +	struct delayed_work work;
> +	int known_ids;
> +};
> +
> +struct rpi_ts_regs {
> +	uint8_t device_mode;
> +	uint8_t gesture_id;
> +	uint8_t num_points;

Simply u8 in kernel.

> +	struct rpi_ts_touch {
> +		uint8_t xh;
> +		uint8_t xl;
> +		uint8_t yh;
> +		uint8_t yl;
> +		uint8_t pressure; /* Not supported */
> +		uint8_t area;     /* Not supported */
> +	} point[RPI_TS_MAX_SUPPORTED_POINTS];
> +};
> +
> +/*
> + * This process polls the memory based register copy of the touch screen chip
> + * registers using the number of points register to know whether the copy has
> + * been updated (we write 99 to the memory copy, the GPU will write between 0 -
> + * 10 points)
> + */
> +static void rpi_ts_work(struct work_struct *work)
> +{
> +	struct rpi_ts *ts = container_of(work, struct rpi_ts, work.work);
> +	struct input_dev *input = ts->input;
> +	struct rpi_ts_regs regs;
> +	int modified_ids = 0;
> +	int released_ids;
> +	int event_type;
> +	int touchid;
> +	int x, y;
> +	int i;
> +
> +	memcpy_fromio(&regs, ts->ts_base, sizeof(struct rpi_ts_regs));
> +	iowrite8(99, ts->ts_base + offsetof(struct rpi_ts_regs, num_points));
> +
> +	if (regs.num_points == 99 ||
> +	    (regs.num_points == 0 && ts->known_ids == 0))
> +	    goto out;
> +
> +	for (i = 0; i < regs.num_points; i++) {
> +		x = (((int)regs.point[i].xh & 0xf) << 8) + regs.point[i].xl;
> +		y = (((int)regs.point[i].yh & 0xf) << 8) + regs.point[i].yl;
> +		touchid = (regs.point[i].yh >> 4) & 0xf;
> +		event_type = (regs.point[i].xh >> 6) & 0x03;
> +
> +		modified_ids |= BIT(touchid);
> +
> +		if (event_type == RPI_TS_FTS_TOUCH_DOWN ||
> +		    event_type == RPI_TS_FTS_TOUCH_CONTACT) {
> +			input_mt_slot(input, touchid);
> +			input_mt_report_slot_state(input, MT_TOOL_FINGER, 1);
> +			touchscreen_report_pos(input, &ts->prop, x, y, true);
> +		}
> +	}
> +
> +	released_ids = ts->known_ids & ~modified_ids;
> +	for (i = 0; released_ids && i < RPI_TS_MAX_SUPPORTED_POINTS; i++) {

If you make released_ids long instead of int you can use
for_each_set_bit() here.

> +		if (released_ids & BIT(i)) {
> +			input_mt_slot(input, i);
> +			input_mt_report_slot_state(input, MT_TOOL_FINGER, 0);
> +			modified_ids &= ~(BIT(i));
> +		}
> +	}
> +	ts->known_ids = modified_ids;
> +
> +	input_mt_report_pointer_emulation(ts->input, true);

input_mt_sync_frame() and then you would not need to call
input_mt_report_pointer_emulation() if you passed right flags to
input_mt_init_slots().

> +	input_sync(ts->input);
> +
> +out:
> +	schedule_delayed_work(&ts->work, msecs_to_jiffies(17)); /* 60 fps */
> +}
> +
> +static int rpi_ts_open(struct input_dev *dev)
> +{
> +	struct rpi_ts *ts = input_get_drvdata(dev);
> +
> +	schedule_delayed_work(&ts->work, 0);
> +
> +	return 0;
> +}
> +
> +static void rpi_ts_close(struct input_dev *dev)
> +{
> +	struct rpi_ts *ts = input_get_drvdata(dev);
> +
> +	cancel_delayed_work_sync(&ts->work);
> +}

Not needed if using polled input device.

> +
> +static int rpi_ts_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct device_node *np = dev->of_node;
> +	struct device_node *fw_node;
> +	struct input_dev *input;
> +	struct rpi_firmware *fw;
> +	struct rpi_ts *ts;
> +	u32 touchbuf;
> +	int err = 0;
> +
> +	fw_node = of_get_parent(np);
> +	if (!fw_node) {
> +		dev_err(dev, "Missing firmware node\n");
> +		return -ENOENT;
> +	}
> +
> +	fw = rpi_firmware_get(fw_node);

You are leaking node here I believe. Stick of_node_put() after
rpi_firmware_get() and before "if".

> +	if (!fw)
> +		return -EPROBE_DEFER;
> +
> +	ts = devm_kzalloc(dev, sizeof(struct rpi_ts), GFP_KERNEL);

sizeof(*ts)

> +	if (!ts) {
> +		dev_err(dev, "Failed to allocate memory\n");
> +		return -ENOMEM;
> +	}
> +
> +	input = input_allocate_device();

devm.

> +	if (!input) {
> +		dev_err(dev, "Failed to allocate input device\n");
> +		return -ENOMEM;
> +	}
> +	ts->input = input;
> +
> +	ts->ts_base = dma_zalloc_coherent(dev, PAGE_SIZE, &ts->bus_addr,
> +					  GFP_KERNEL);
> +	if (!ts->ts_base) {
> +		dev_err(dev, "failed to dma_alloc_coherent\n");
> +		err = -ENOMEM;
> +		goto undegister_input_device;
> +	}
> +
> +	touchbuf = (u32)ts->bus_addr;
> +	err = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
> +				    &touchbuf, sizeof(touchbuf));
> +
> +	if (err || touchbuf != 0) {
> +		dev_warn(dev, "Failed to set touchbuf, trying to get err:%x\n",
> +			 err);
> +		goto clean_dma_buf;
> +	}
> +
> +	INIT_DELAYED_WORK(&ts->work, rpi_ts_work);
> +	platform_set_drvdata(pdev, ts);
> +
> +	input->name = "raspberrypi-ts";
> +	input->id.bustype = BUS_HOST;
> +	input->open = rpi_ts_open;
> +	input->close = rpi_ts_close;
> +	input->dev.parent = &pdev->dev;
> +
> +	__set_bit(EV_KEY, input->evbit);
> +	__set_bit(EV_SYN, input->evbit);
> +	__set_bit(EV_ABS, input->evbit);
> +
> +	input_set_abs_params(input, ABS_MT_POSITION_X, 0,
> +			     RPI_TS_DEFAULT_WIDTH, 0, 0);
> +	input_set_abs_params(input, ABS_MT_POSITION_Y, 0,
> +			     RPI_TS_DEFAULT_HEIGHT, 0, 0);
> +	touchscreen_parse_properties(input, true, &ts->prop);
> +
> +	input_mt_init_slots(input, RPI_TS_MAX_SUPPORTED_POINTS,
> +			    INPUT_MT_DIRECT);
> +
> +	input_set_drvdata(input, ts);
> +
> +	err = input_register_device(input);
> +	if (err) {
> +		dev_err(dev, "could not register input device, %d\n",
> +			err);
> +		goto clean_dma_buf;
> +	}
> +	return 0;
> +
> +clean_dma_buf:
> +	dma_free_coherent(dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);

I wild make freeing this area a devm custom action
(devm_add_action_or_reset). Then you can get rid of rpi_ts_remove().

> +undegister_input_device:
> +	input_unregister_device(ts->input);
> +
> +	return err;
> +}
> +
> +static int rpi_ts_remove(struct platform_device *pdev)
> +{
> +	struct rpi_ts *ts = (struct rpi_ts *)platform_get_drvdata(pdev);
> +
> +	input_unregister_device(ts->input);
> +	dma_free_coherent(&pdev->dev, PAGE_SIZE, ts->ts_base, ts->bus_addr);
> +	return 0;
> +}
> +
> +static const struct of_device_id rpi_ts_match[] = {
> +	{ .compatible = "raspberrypi,firmware-ts", },
> +	{},
> +};
> +MODULE_DEVICE_TABLE(of, rpi_ts_match);
> +
> +static struct platform_driver rpi_ts_driver = {
> +	.driver = {
> +		.name   = "raspberrypi-ts",
> +		.owner  = THIS_MODULE,

I am pretty sure we no longer need to set owner explicitly.

> +		.of_match_table = rpi_ts_match,
> +	},
> +	.probe          = rpi_ts_probe,
> +	.remove         = rpi_ts_remove,
> +};
> +
> +module_platform_driver(rpi_ts_driver);
> +
> +MODULE_AUTHOR("Gordon Hollingworth");
> +MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
> +MODULE_DESCRIPTION("Raspberry Pi 3 firmware based touchscreen driver");
> +MODULE_LICENSE("GPL");

This should be "GPL v2" to math the license notice at the top of the
file.

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-12-09  4:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-05 22:09 [PATCH RFC 0/2] input: driver for RPi's official 7" touchscreen Nicolas Saenz Julienne
2018-12-05 22:09 ` [PATCH RFC 1/2] input: add official Raspberry Pi's 7" touchscreen driver Nicolas Saenz Julienne
2018-12-09  4:43   ` Dmitry Torokhov
2018-12-05 22:09 ` [PATCH RFC 2/2] Input: raspberrypi-ts - add devicetree binding documentation Nicolas Saenz Julienne

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.