linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v5 0/2] Input: msg2638: Add support for msg2138 and key events
@ 2022-11-16 18:17 Vincent Knecht
  2022-11-16 18:17 ` [PATCH v5 1/2] dt-bindings: input: touchscreen: msg2638: Document keys support Vincent Knecht
  2022-11-16 18:17 ` [PATCH v5 2/2] Input: msg2638 - Add support for msg2138 key events Vincent Knecht
  0 siblings, 2 replies; 4+ messages in thread
From: Vincent Knecht @ 2022-11-16 18:17 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Vincent Knecht,
	linux-input, devicetree, linux-kernel
  Cc: w.david0, stephan, phone-devel, ~postmarketos/upstreaming

This series is the continuation of v4 [1] from which the first 3 patches
have been merged already.

v5:
  - keep only the non-merged patch
  - remove double-negation in call to input_report_key() (Dmitry)
  - change keycodes retrieval to use device property API (Dmitry)
  - due to previous point, add a check and a warning if more than
    the supported number of keys are found.
    Should that happen, ignore the keys in excess.

[1] https://lore.kernel.org/linux-input/20221110171952.34207-1-vincent.knecht@mailoo.org/T/#t

Vincent Knecht (2):
  dt-bindings: input: touchscreen: msg2638: Document keys support
  Input: msg2638 - Add support for msg2138 key events

 .../input/touchscreen/mstar,msg2638.yaml      |  4 ++
 drivers/input/touchscreen/msg2638.c           | 57 +++++++++++++++++--
 2 files changed, 57 insertions(+), 4 deletions(-)

-- 
2.38.1




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

* [PATCH v5 1/2] dt-bindings: input: touchscreen: msg2638: Document keys support
  2022-11-16 18:17 [PATCH v5 0/2] Input: msg2638: Add support for msg2138 and key events Vincent Knecht
@ 2022-11-16 18:17 ` Vincent Knecht
  2022-11-16 18:17 ` [PATCH v5 2/2] Input: msg2638 - Add support for msg2138 key events Vincent Knecht
  1 sibling, 0 replies; 4+ messages in thread
From: Vincent Knecht @ 2022-11-16 18:17 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Vincent Knecht,
	linux-input, devicetree, linux-kernel
  Cc: w.david0, stephan, phone-devel, ~postmarketos/upstreaming,
	Rob Herring

Document optional linux,keycodes support.

Acked-by: Rob Herring <robh@kernel.org>
Signed-off-by: Vincent Knecht <vincent.knecht@mailoo.org>
---
 .../devicetree/bindings/input/touchscreen/mstar,msg2638.yaml  | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/Documentation/devicetree/bindings/input/touchscreen/mstar,msg2638.yaml b/Documentation/devicetree/bindings/input/touchscreen/mstar,msg2638.yaml
index 2fb7e01bb65a..af4f954de958 100644
--- a/Documentation/devicetree/bindings/input/touchscreen/mstar,msg2638.yaml
+++ b/Documentation/devicetree/bindings/input/touchscreen/mstar,msg2638.yaml
@@ -36,6 +36,10 @@ properties:
   touchscreen-size-x: true
   touchscreen-size-y: true
 
+  linux,keycodes:
+    minItems: 1
+    maxItems: 4
+
 additionalProperties: false
 
 required:
-- 
2.38.1




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

* [PATCH v5 2/2] Input: msg2638 - Add support for msg2138 key events
  2022-11-16 18:17 [PATCH v5 0/2] Input: msg2638: Add support for msg2138 and key events Vincent Knecht
  2022-11-16 18:17 ` [PATCH v5 1/2] dt-bindings: input: touchscreen: msg2638: Document keys support Vincent Knecht
@ 2022-11-16 18:17 ` Vincent Knecht
  2022-11-16 20:33   ` Dmitry Torokhov
  1 sibling, 1 reply; 4+ messages in thread
From: Vincent Knecht @ 2022-11-16 18:17 UTC (permalink / raw)
  To: Dmitry Torokhov, Rob Herring, Krzysztof Kozlowski, Vincent Knecht,
	linux-input, devicetree, linux-kernel
  Cc: w.david0, stephan, phone-devel, ~postmarketos/upstreaming

Some devices with msg2138 have back/menu/home keys.
Add support for them.

Signed-off-by: Vincent Knecht <vincent.knecht@mailoo.org>
---
 drivers/input/touchscreen/msg2638.c | 57 +++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 4 deletions(-)

diff --git a/drivers/input/touchscreen/msg2638.c b/drivers/input/touchscreen/msg2638.c
index 95b18563326a..a0f5e1ecd612 100644
--- a/drivers/input/touchscreen/msg2638.c
+++ b/drivers/input/touchscreen/msg2638.c
@@ -21,6 +21,7 @@
 #include <linux/kernel.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
+#include <linux/property.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 
@@ -29,6 +30,8 @@
 #define MSG2138_MAX_FINGERS		2
 #define MSG2638_MAX_FINGERS		5
 
+#define MAX_BUTTONS			4
+
 #define CHIP_ON_DELAY_MS		15
 #define FIRMWARE_ON_DELAY_MS		50
 #define RESET_DELAY_MIN_US		10000
@@ -72,6 +75,8 @@ struct msg2638_ts_data {
 	struct regulator_bulk_data supplies[2];
 	struct gpio_desc *reset_gpiod;
 	int max_fingers;
+	u32 keycodes[MAX_BUTTONS];
+	int num_keycodes;
 };
 
 static u8 msg2638_checksum(u8 *data, u32 length)
@@ -85,6 +90,18 @@ static u8 msg2638_checksum(u8 *data, u32 length)
 	return (u8)((-sum) & 0xFF);
 }
 
+static void msg2138_report_keys(struct msg2638_ts_data *msg2638, u8 keys)
+{
+	int i;
+
+	/* keys can be 0x00 or 0xff when all keys have been released */
+	if (keys == 0xff)
+		keys = 0;
+
+	for (i = 0; i < msg2638->num_keycodes; ++i)
+		input_report_key(msg2638->input_dev, msg2638->keycodes[i], keys & BIT(i));
+}
+
 static irqreturn_t msg2138_ts_irq_handler(int irq, void *msg2638_handler)
 {
 	struct msg2638_ts_data *msg2638 = msg2638_handler;
@@ -121,9 +138,12 @@ static irqreturn_t msg2138_ts_irq_handler(int irq, void *msg2638_handler)
 	p0 = &touch_event.pkt[0];
 	p1 = &touch_event.pkt[1];
 
-	/* Ignore non-pressed finger data */
-	if (p0->xy_hi == 0xFF && p0->x_low == 0xFF && p0->y_low == 0xFF)
+	/* Ignore non-pressed finger data, but check for key code */
+	if (p0->xy_hi == 0xFF && p0->x_low == 0xFF && p0->y_low == 0xFF) {
+		if (p1->xy_hi == 0xFF && p1->y_low == 0xFF)
+			msg2138_report_keys(msg2638, p1->x_low);
 		goto report;
+	}
 
 	x = ((p0->xy_hi & 0xF0) << 4) | p0->x_low;
 	y = ((p0->xy_hi & 0x0F) << 8) | p0->y_low;
@@ -283,6 +303,7 @@ static int msg2638_init_input_dev(struct msg2638_ts_data *msg2638)
 	struct device *dev = &msg2638->client->dev;
 	struct input_dev *input_dev;
 	int error;
+	int i;
 
 	input_dev = devm_input_allocate_device(dev);
 	if (!input_dev) {
@@ -299,6 +320,14 @@ static int msg2638_init_input_dev(struct msg2638_ts_data *msg2638)
 	input_dev->open = msg2638_input_open;
 	input_dev->close = msg2638_input_close;
 
+	if (msg2638->num_keycodes) {
+		input_dev->keycode = msg2638->keycodes;
+		input_dev->keycodemax = msg2638->num_keycodes;
+		input_dev->keycodesize = sizeof(msg2638->keycodes[0]);
+		for (i = 0; i < msg2638->num_keycodes; i++)
+			input_set_capability(input_dev, EV_KEY, msg2638->keycodes[i]);
+	}
+
 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
 	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
 
@@ -367,9 +396,23 @@ static int msg2638_ts_probe(struct i2c_client *client)
 		return error;
 	}
 
-	error = msg2638_init_input_dev(msg2638);
+	msg2638->num_keycodes = fwnode_property_count_u32(dev->fwnode, "linux,keycodes");
+	if (msg2638->num_keycodes == -EINVAL) {
+		msg2638->num_keycodes = 0;
+	} else if (msg2638->num_keycodes < 0) {
+		dev_err(dev, "Unable to parse linux,keycodes property: %d\n",
+			msg2638->num_keycodes);
+		return msg2638->num_keycodes;
+	} else if (msg2638->num_keycodes > ARRAY_SIZE(msg2638->keycodes)) {
+		dev_warn(dev, "Found %d linux,keycodes but max is %ld, ignoring the rest\n",
+			 msg2638->num_keycodes, ARRAY_SIZE(msg2638->keycodes));
+		msg2638->num_keycodes = ARRAY_SIZE(msg2638->keycodes);
+	}
+
+	error = fwnode_property_read_u32_array(dev->fwnode, "linux,keycodes",
+					       msg2638->keycodes, msg2638->num_keycodes);
 	if (error) {
-		dev_err(dev, "Failed to initialize input device: %d\n", error);
+		dev_err(dev, "Unable to read linux,keycodes values: %d\n", error);
 		return error;
 	}
 
@@ -382,6 +425,12 @@ static int msg2638_ts_probe(struct i2c_client *client)
 		return error;
 	}
 
+	error = msg2638_init_input_dev(msg2638);
+	if (error) {
+		dev_err(dev, "Failed to initialize input device: %d\n", error);
+		return error;
+	}
+
 	return 0;
 }
 
-- 
2.38.1




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

* Re: [PATCH v5 2/2] Input: msg2638 - Add support for msg2138 key events
  2022-11-16 18:17 ` [PATCH v5 2/2] Input: msg2638 - Add support for msg2138 key events Vincent Knecht
@ 2022-11-16 20:33   ` Dmitry Torokhov
  0 siblings, 0 replies; 4+ messages in thread
From: Dmitry Torokhov @ 2022-11-16 20:33 UTC (permalink / raw)
  To: Vincent Knecht
  Cc: Rob Herring, Krzysztof Kozlowski, linux-input, devicetree,
	linux-kernel, w.david0, stephan, phone-devel,
	~postmarketos/upstreaming

On Wed, Nov 16, 2022 at 07:17:12PM +0100, Vincent Knecht wrote:
> Some devices with msg2138 have back/menu/home keys.
> Add support for them.
> 
> Signed-off-by: Vincent Knecht <vincent.knecht@mailoo.org>
> ---
>  drivers/input/touchscreen/msg2638.c | 57 +++++++++++++++++++++++++++--
>  1 file changed, 53 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/input/touchscreen/msg2638.c b/drivers/input/touchscreen/msg2638.c
> index 95b18563326a..a0f5e1ecd612 100644
> --- a/drivers/input/touchscreen/msg2638.c
> +++ b/drivers/input/touchscreen/msg2638.c
> @@ -21,6 +21,7 @@
>  #include <linux/kernel.h>
>  #include <linux/mod_devicetable.h>
>  #include <linux/module.h>
> +#include <linux/property.h>
>  #include <linux/regulator/consumer.h>
>  #include <linux/slab.h>
>  
> @@ -29,6 +30,8 @@
>  #define MSG2138_MAX_FINGERS		2
>  #define MSG2638_MAX_FINGERS		5
>  
> +#define MAX_BUTTONS			4
> +
>  #define CHIP_ON_DELAY_MS		15
>  #define FIRMWARE_ON_DELAY_MS		50
>  #define RESET_DELAY_MIN_US		10000
> @@ -72,6 +75,8 @@ struct msg2638_ts_data {
>  	struct regulator_bulk_data supplies[2];
>  	struct gpio_desc *reset_gpiod;
>  	int max_fingers;
> +	u32 keycodes[MAX_BUTTONS];
> +	int num_keycodes;
>  };
>  
>  static u8 msg2638_checksum(u8 *data, u32 length)
> @@ -85,6 +90,18 @@ static u8 msg2638_checksum(u8 *data, u32 length)
>  	return (u8)((-sum) & 0xFF);
>  }
>  
> +static void msg2138_report_keys(struct msg2638_ts_data *msg2638, u8 keys)
> +{
> +	int i;
> +
> +	/* keys can be 0x00 or 0xff when all keys have been released */
> +	if (keys == 0xff)
> +		keys = 0;
> +
> +	for (i = 0; i < msg2638->num_keycodes; ++i)
> +		input_report_key(msg2638->input_dev, msg2638->keycodes[i], keys & BIT(i));
> +}
> +
>  static irqreturn_t msg2138_ts_irq_handler(int irq, void *msg2638_handler)
>  {
>  	struct msg2638_ts_data *msg2638 = msg2638_handler;
> @@ -121,9 +138,12 @@ static irqreturn_t msg2138_ts_irq_handler(int irq, void *msg2638_handler)
>  	p0 = &touch_event.pkt[0];
>  	p1 = &touch_event.pkt[1];
>  
> -	/* Ignore non-pressed finger data */
> -	if (p0->xy_hi == 0xFF && p0->x_low == 0xFF && p0->y_low == 0xFF)
> +	/* Ignore non-pressed finger data, but check for key code */
> +	if (p0->xy_hi == 0xFF && p0->x_low == 0xFF && p0->y_low == 0xFF) {
> +		if (p1->xy_hi == 0xFF && p1->y_low == 0xFF)
> +			msg2138_report_keys(msg2638, p1->x_low);
>  		goto report;
> +	}
>  
>  	x = ((p0->xy_hi & 0xF0) << 4) | p0->x_low;
>  	y = ((p0->xy_hi & 0x0F) << 8) | p0->y_low;
> @@ -283,6 +303,7 @@ static int msg2638_init_input_dev(struct msg2638_ts_data *msg2638)
>  	struct device *dev = &msg2638->client->dev;
>  	struct input_dev *input_dev;
>  	int error;
> +	int i;
>  
>  	input_dev = devm_input_allocate_device(dev);
>  	if (!input_dev) {
> @@ -299,6 +320,14 @@ static int msg2638_init_input_dev(struct msg2638_ts_data *msg2638)
>  	input_dev->open = msg2638_input_open;
>  	input_dev->close = msg2638_input_close;
>  
> +	if (msg2638->num_keycodes) {
> +		input_dev->keycode = msg2638->keycodes;
> +		input_dev->keycodemax = msg2638->num_keycodes;
> +		input_dev->keycodesize = sizeof(msg2638->keycodes[0]);
> +		for (i = 0; i < msg2638->num_keycodes; i++)
> +			input_set_capability(input_dev, EV_KEY, msg2638->keycodes[i]);
> +	}
> +
>  	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_X);
>  	input_set_capability(input_dev, EV_ABS, ABS_MT_POSITION_Y);
>  
> @@ -367,9 +396,23 @@ static int msg2638_ts_probe(struct i2c_client *client)
>  		return error;
>  	}
>  
> -	error = msg2638_init_input_dev(msg2638);
> +	msg2638->num_keycodes = fwnode_property_count_u32(dev->fwnode, "linux,keycodes");

Please use device_property_count_u32().

> +	if (msg2638->num_keycodes == -EINVAL) {
> +		msg2638->num_keycodes = 0;
> +	} else if (msg2638->num_keycodes < 0) {
> +		dev_err(dev, "Unable to parse linux,keycodes property: %d\n",
> +			msg2638->num_keycodes);
> +		return msg2638->num_keycodes;
> +	} else if (msg2638->num_keycodes > ARRAY_SIZE(msg2638->keycodes)) {
> +		dev_warn(dev, "Found %d linux,keycodes but max is %ld, ignoring the rest\n",
> +			 msg2638->num_keycodes, ARRAY_SIZE(msg2638->keycodes));

I think you want "%zd" in place of "%ld".

> +		msg2638->num_keycodes = ARRAY_SIZE(msg2638->keycodes);
> +	}
> +
> +	error = fwnode_property_read_u32_array(dev->fwnode, "linux,keycodes",
> +					       msg2638->keycodes, msg2638->num_keycodes);

Please use device_property_read_u32_array().

>  	if (error) {
> -		dev_err(dev, "Failed to initialize input device: %d\n", error);
> +		dev_err(dev, "Unable to read linux,keycodes values: %d\n", error);
>  		return error;
>  	}
>  
> @@ -382,6 +425,12 @@ static int msg2638_ts_probe(struct i2c_client *client)
>  		return error;
>  	}
>  
> +	error = msg2638_init_input_dev(msg2638);
> +	if (error) {
> +		dev_err(dev, "Failed to initialize input device: %d\n", error);
> +		return error;
> +	}
> +
>  	return 0;
>  }
>  
> -- 
> 2.38.1
> 
> 
> 

Thanks.

-- 
Dmitry

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

end of thread, other threads:[~2022-11-16 20:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-16 18:17 [PATCH v5 0/2] Input: msg2638: Add support for msg2138 and key events Vincent Knecht
2022-11-16 18:17 ` [PATCH v5 1/2] dt-bindings: input: touchscreen: msg2638: Document keys support Vincent Knecht
2022-11-16 18:17 ` [PATCH v5 2/2] Input: msg2638 - Add support for msg2138 key events Vincent Knecht
2022-11-16 20:33   ` Dmitry Torokhov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).