Skip to contentSkip to conversation

dt-bindings: serial: sc16is7xx: add reset-gpios

PATCH Superseded Hui Wang 6 participants 24 messages Jun 4, 2024 – Jun 6, 2024 1 message not in the archive

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Hui Wang

author
#0 lore raw

PATCH v2 1/2 dt-bindings: serial: sc16is7xx: add reset-gpios

In some designs, the chip reset pin is connected to a gpio, this gpio needs to be set correctly before probing the driver, so adding a reset-gpios in the device tree.

---
In the v2:
- include the gpio.h
- run the 'make dt_binding_check' and 'make dtbs_check'

 Documentation/devicetree/bindings/serial/nxp,sc16is7xx.yaml | 5 +++++
 1 file changed, 5 insertions(+)
Documentation/devicetree/bindings/serial/nxp,sc16is7xx.yaml +5 -0
index 5dec15b7e7c3..88871480018e 100644
@@ -28,6 +28,9 @@ properties:
   clocks:
     maxItems: 1
 
+  reset-gpios:
+    maxItems: 1
+
   clock-frequency:
     description:
       When there is no clock provider visible to the platform, this
@@ -91,6 +94,7 @@ unevaluatedProperties: false
 examples:
   - |
     #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
     i2c {
         #address-cells = <1>;
         #size-cells = <0>;
@@ -120,6 +124,7 @@ examples:
             compatible = "nxp,sc16is752";
             reg = <0x54>;
             clocks = <&clk20m>;
+            reset-gpios = <&gpio5 13 GPIO_ACTIVE_LOW>;
             interrupt-parent = <&gpio3>;
             interrupts = <7 IRQ_TYPE_EDGE_FALLING>;
             nxp,modem-control-line-ports = <0 1>; /* Ports 0 and 1 as modem control lines */
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 2/2 serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Certain designs connect a gpio to the reset pin, and the reset pin needs to be setup correctly before accessing the chip.

Here adding a function to handle the chip reset. If the reset-gpios is defined in the dt, do the hard reset through this gpio, othwerwise do the soft reset as before.

---
In the v2:
- move the soft reset and hard reset into one fucntion
- move the reset function to sc16is7xx.c and call it in _probe()
- add udelay(5) before deasserting the gpio reset pin

 drivers/tty/serial/sc16is7xx.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/tty/serial/sc16is7xx.c +24 -4
index bf0065d1c8e9..119abfb4607c 100644
@@ -14,6 +14,7 @@
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/export.h>
+#include <linux/gpio/consumer.h>
 #include <linux/gpio/driver.h>
 #include <linux/idr.h>
 #include <linux/kthread.h>
@@ -1467,6 +1468,25 @@ static const struct serial_rs485 sc16is7xx_rs485_supported = {
 	.delay_rts_after_send = 1,	/* Not supported but keep returning -EINVAL */
 };
 
+static int sc16is7xx_reset(struct device *dev, struct regmap *regmaps[])
+{
+	struct gpio_desc *reset_gpiod;
+
+	reset_gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
+	if (!reset_gpiod)
+		/* soft reset device, purging any pending irq / data */
+		regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
+			     SC16IS7XX_IOCONTROL_SRESET_BIT);
+	else if (!IS_ERR(reset_gpiod)) {
+		/* delay 5 us (at least 3 us) and deassert the gpio to exit the hard reset */
+		udelay(5);
+		gpiod_set_value_cansleep(reset_gpiod, 0);
+	} else
+		return PTR_ERR(reset_gpiod);
+
+	return 0;
+}
+
 int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 		    struct regmap *regmaps[], int irq)
 {
@@ -1527,6 +1547,10 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 	s->devtype = devtype;
 	dev_set_drvdata(dev, s);
 
+	ret = sc16is7xx_reset(dev, regmaps);
+	if (ret)
+		goto out_clk;
+
 	kthread_init_worker(&s->kworker);
 	s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker,
 				      "sc16is7xx");
@@ -1536,10 +1560,6 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 	}
 	sched_set_fifo(s->kworker_task);
 
-	/* reset device, purging any pending irq / data */
-	regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
-		     SC16IS7XX_IOCONTROL_SRESET_BIT);
-
 	/* Mark each port line and status as uninitialised. */
 	for (i = 0; i < devtype->nr_uart; ++i) {
 		s->p[i].port.line = SC16IS7XX_MAX_DEVS;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Quoted 8 lines · Hui Wang

Certain designs connect a gpio to the reset pin, and the reset pin needs to be setup correctly before accessing the chip.

Here adding a function to handle the chip reset. If the reset-gpios is defined in the dt, do the hard reset through this gpio, othwerwise do the soft reset as before.

No, stop making up tags!

Best regards,
Krzysztof

#3 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

In reply to Hui Wang
Quoted 1 line · Hui Wang

Certain designs connect a gpio to the reset pin, and the reset pin

GPIO

Quoted 4 lines

needs to be setup correctly before accessing the chip.

Here adding a function to handle the chip reset. If the reset-gpios is defined in the dt, do the hard reset through this gpio, othwerwise do

DT

Quoted 1 line

the soft reset as before.

...

Quoted 5 lines
drivers/tty/serial/sc16is7xx.c +4 -0
+static int sc16is7xx_reset(struct device *dev, struct regmap *regmaps[])
+{
+	struct gpio_desc *reset_gpiod;
 
+	reset_gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1485

Quoted 10 lines
drivers/tty/serial/sc16is7xx.c +10 -0
+	if (!reset_gpiod)
+		/* soft reset device, purging any pending irq / data */
+		regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
+			     SC16IS7XX_IOCONTROL_SRESET_BIT);
+	else if (!IS_ERR(reset_gpiod)) {
+		/* delay 5 us (at least 3 us) and deassert the gpio to exit the hard reset */
+		udelay(5);
+		gpiod_set_value_cansleep(reset_gpiod, 0);
+	} else
+		return PTR_ERR(reset_gpiod);

You can do better here.

	reset_gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
	if (IS_ERR(reset_gpiod))
		return PTR_ERR(reset_gpiod);

	if (reset_gpiod) {
		/* delay 5 us (at least 3 us) and deassert the GPIO to exit the hard reset */
		fsleep(5);
		gpiod_set_value_cansleep(reset_gpiod, 0);
	} else {
		/* soft reset device, purging any pending IRQ / data */
		regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
			     SC16IS7XX_IOCONTROL_SRESET_BIT);
	}
Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+	return 0;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#4 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Quoted 3 lines · Andy Shevchenko
Quoted 1 line · Hui Wang

Certain designs connect a gpio to the reset pin, and the reset pin

GPIO

Got it.

Quoted 5 lines
Quoted 4 lines

needs to be setup correctly before accessing the chip.

Here adding a function to handle the chip reset. If the reset-gpios is defined in the dt, do the hard reset through this gpio, othwerwise do

DT

Got it.

Quoted 20 lines
Quoted 2 lines

the soft reset as before.

...

Quoted 4 lines
drivers/tty/serial/sc16is7xx.c +4 -0
+static int sc16is7xx_reset(struct device *dev, struct regmap *regmaps[])
+{
+	struct gpio_desc *reset_gpiod;
+	reset_gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1485

Quoted 10 lines
drivers/tty/serial/sc16is7xx.c +10 -0
+	if (!reset_gpiod)
+		/* soft reset device, purging any pending irq / data */
+		regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
+			     SC16IS7XX_IOCONTROL_SRESET_BIT);
+	else if (!IS_ERR(reset_gpiod)) {
+		/* delay 5 us (at least 3 us) and deassert the gpio to exit the hard reset */
+		udelay(5);
+		gpiod_set_value_cansleep(reset_gpiod, 0);
+	} else
+		return PTR_ERR(reset_gpiod);

You can do better here.

Quoted 13 lines
	reset_gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
	if (IS_ERR(reset_gpiod))
		return PTR_ERR(reset_gpiod);

	if (reset_gpiod) {
		/* delay 5 us (at least 3 us) and deassert the GPIO to exit the hard reset */
		fsleep(5);
		gpiod_set_value_cansleep(reset_gpiod, 0);
	} else {
		/* soft reset device, purging any pending IRQ / data */
		regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
			     SC16IS7XX_IOCONTROL_SRESET_BIT);
	}

OK, got it, will fix all comment in the v3.

Thanks.

Quoted 2 lines
Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+	return 0;
+}
#5 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

In reply to Hui Wang

On Tue, 4 Jun 2024 21:27:26 +0800
Hui Wang <[email protected]> wrote:

Hi Hui,

Quoted 8 lines

Certain designs connect a gpio to the reset pin, and the reset pin needs to be setup correctly before accessing the chip.

Here adding a function to handle the chip reset. If the reset-gpios is defined in the dt, do the hard reset through this gpio, othwerwise do the soft reset as before.

I never gave you permission to add this tag, remove it. Make sure you fully understand the meaning of tags by reading patches submission guidelines.

Quoted 23 lines

---
In the v2:
- move the soft reset and hard reset into one fucntion
- move the reset function to sc16is7xx.c and call it in _probe()
- add udelay(5) before deasserting the gpio reset pin

 drivers/tty/serial/sc16is7xx.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/tty/serial/sc16is7xx.c +1 -0
index bf0065d1c8e9..119abfb4607c 100644
@@ -14,6 +14,7 @@
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/export.h>
+#include <linux/gpio/consumer.h>
 #include <linux/gpio/driver.h>
 #include <linux/idr.h>
 #include <linux/kthread.h>

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1470

Quoted 3 lines
drivers/tty/serial/sc16is7xx.c +0 -0
@@ -1467,6 +1468,25 @@ static const struct serial_rs485 sc16is7xx_rs485_supported = {
 	.delay_rts_after_send = 1,	/* Not supported but keep returning -EINVAL */
 };
 

Add function description from original comment "Reset device, purging any pending irq / data", since the comment applies to both hardware and software reset,

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1471

Quoted 1 line
drivers/tty/serial/sc16is7xx.c +1 -0
+static int sc16is7xx_reset(struct device *dev, struct regmap *regmaps[])

Simply pass "struct regmap *regmap" as the second argument. See sc16is7xx_setup_mctrl_ports() for example.

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1473

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+{
+	struct gpio_desc *reset_gpiod;

reset_gpiod -> reset_gpio

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1476

Quoted 3 lines
drivers/tty/serial/sc16is7xx.c +3 -0
+
+	reset_gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
+	if (!reset_gpiod)

Follow Andy's suggestion here.

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1477

Quoted 1 line
drivers/tty/serial/sc16is7xx.c +1 -0
+		/* soft reset device, purging any pending irq / data */

"Software reset".

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1481

Quoted 4 lines
drivers/tty/serial/sc16is7xx.c +4 -0
+		regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
+			     SC16IS7XX_IOCONTROL_SRESET_BIT);
+	else if (!IS_ERR(reset_gpiod)) {
+		/* delay 5 us (at least 3 us) and deassert the gpio to exit the hard reset */

You can omit the "delay 5 us" since it is obvious from the code. Maybe add that "The minimum reset pulse width is 3 us" as stated in the datasheet.

As a general note for your comments: capitalize the first letter,
ex: "Deassert GPIO" and not "deassert GPIO".

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1483

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+		udelay(5);
+		gpiod_set_value_cansleep(reset_gpiod, 0);

Move the comment "deassert the gpio to exit the hard reset" here. You could also simplify it as "Deassert GPIO.".

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1485

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+	} else
+		return PTR_ERR(reset_gpiod);

return dev_err_probe(dev, PTR_ERR(reset_gpiod), "Failed to get reset GPIO\n");

Quoted 34 lines
drivers/tty/serial/sc16is7xx.c +8 -4
+
+	return 0;
+}
+
 int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 		    struct regmap *regmaps[], int irq)
 {
@@ -1527,6 +1547,10 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 	s->devtype = devtype;
 	dev_set_drvdata(dev, s);
 
+	ret = sc16is7xx_reset(dev, regmaps);
+	if (ret)
+		goto out_clk;
+
 	kthread_init_worker(&s->kworker);
 	s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker,
 				      "sc16is7xx");
@@ -1536,10 +1560,6 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 	}
 	sched_set_fifo(s->kworker_task);
 
-	/* reset device, purging any pending irq / data */
-	regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
-		     SC16IS7XX_IOCONTROL_SRESET_BIT);
-
 	/* Mark each port line and status as uninitialised. */
 	for (i = 0; i < devtype->nr_uart; ++i) {
 		s->p[i].port.line = SC16IS7XX_MAX_DEVS;
Signature · 1 line
2.34.1
Signature · 1 line
Hugo Villeneuve
author
#6 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Quoted 8 lines · Hugo Villeneuve

On Tue, 4 Jun 2024 21:27:26 +0800
Hui Wang <[email protected]> wrote:

Hi Hui,

Quoted 3 lines

Certain designs connect a gpio to the reset pin, and the reset pin needs to be setup correctly before accessing the chip.

Quoted 8 lines
Quoted 5 lines

Here adding a function to handle the chip reset. If the reset-gpios is defined in the dt, do the hard reset through this gpio, othwerwise do the soft reset as before.

I never gave you permission to add this tag, remove it. Make sure you fully understand the meaning of tags by reading patches submission guidelines.

I misunderstood the meaning of "reviewed-by", will remove it and study the guidelines.

Quoted 22 lines
Quoted 22 lines

---
In the v2:
- move the soft reset and hard reset into one fucntion
- move the reset function to sc16is7xx.c and call it in _probe()
- add udelay(5) before deasserting the gpio reset pin

  drivers/tty/serial/sc16is7xx.c | 28 ++++++++++++++++++++++++----
  1 file changed, 24 insertions(+), 4 deletions(-)
drivers/tty/serial/sc16is7xx.c +1 -0
index bf0065d1c8e9..119abfb4607c 100644
@@ -14,6 +14,7 @@
  #include <linux/delay.h>
  #include <linux/device.h>
  #include <linux/export.h>
+#include <linux/gpio/consumer.h>
  #include <linux/gpio/driver.h>
  #include <linux/idr.h>
Quoted 8 lines

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1470

Quoted 5 lines
drivers/tty/serial/sc16is7xx.c +0 -0
  #include <linux/kthread.h>
@@ -1467,6 +1468,25 @@ static const struct serial_rs485 sc16is7xx_rs485_supported = {
  	.delay_rts_after_send = 1,	/* Not supported but keep returning -EINVAL */
  };
 

Add function description from original comment "Reset device, purging any pending irq / data", since the comment applies to both hardware and software reset,

Got it.

Quoted 3 lines

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1471

Quoted 1 line
drivers/tty/serial/sc16is7xx.c +1 -0
+static int sc16is7xx_reset(struct device *dev, struct regmap *regmaps[])

Simply pass "struct regmap *regmap" as the second argument. See sc16is7xx_setup_mctrl_ports() for example.

Got it.

Quoted 4 lines

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1473

Quoted 3 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+{
+	struct gpio_desc *reset_gpiod;

reset_gpiod -> reset_gpio

Got it.

Quoted 4 lines

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1476

Quoted 3 lines
drivers/tty/serial/sc16is7xx.c +3 -0
+
+	reset_gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
+	if (!reset_gpiod)

Follow Andy's suggestion here.

OK.

Quoted 3 lines

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1477

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +1 -0
+		/* soft reset device, purging any pending irq / data */

"Software reset".

Got it.

Quoted 10 lines

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1481

Quoted 4 lines
drivers/tty/serial/sc16is7xx.c +4 -0
+		regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
+			     SC16IS7XX_IOCONTROL_SRESET_BIT);
+	else if (!IS_ERR(reset_gpiod)) {
+		/* delay 5 us (at least 3 us) and deassert the gpio to exit the hard reset */

You can omit the "delay 5 us" since it is obvious from the code. Maybe add that "The minimum reset pulse width is 3 us" as stated in the datasheet.

As a general note for your comments: capitalize the first letter,
ex: "Deassert GPIO" and not "deassert GPIO".

OK.

Quoted 6 lines

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1483

Quoted 3 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+		udelay(5);
+		gpiod_set_value_cansleep(reset_gpiod, 0);

Move the comment "deassert the gpio to exit the hard reset" here. You could also simplify it as "Deassert GPIO.".

OK.

Quoted 4 lines

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1485

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+	} else
+		return PTR_ERR(reset_gpiod);

return dev_err_probe(dev, PTR_ERR(reset_gpiod), "Failed to get reset GPIO\n");

Got it, will address all comment in the v3.

Thanks.

Quoted 36 lines
Quoted 35 lines
drivers/tty/serial/sc16is7xx.c +8 -4
+
+	return 0;
+}
+
  int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
  		    struct regmap *regmaps[], int irq)
  {
@@ -1527,6 +1547,10 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
  	s->devtype = devtype;
  	dev_set_drvdata(dev, s);
 
+	ret = sc16is7xx_reset(dev, regmaps);
+	if (ret)
+		goto out_clk;
+
  	kthread_init_worker(&s->kworker);
  	s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker,
  				      "sc16is7xx");
@@ -1536,10 +1560,6 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
  	}
  	sched_set_fifo(s->kworker_task);
 
-	/* reset device, purging any pending irq / data */
-	regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
-		     SC16IS7XX_IOCONTROL_SRESET_BIT);
-
  	/* Mark each port line and status as uninitialised. */
  	for (i = 0; i < devtype->nr_uart; ++i) {
  		s->p[i].port.line = SC16IS7XX_MAX_DEVS;
Signature · 1 line
2.34.1
#7 lore raw

RE: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

In reply to Hugo Villeneuve
Quoted 2 lines

From: Hugo Villeneuve <[email protected]>
Sent: Tuesday, 4 June 2024 16:23

<...>

Quoted 3 lines

Add function description from original comment "Reset device, purging any pending irq / data", since the comment applies to both hardware and software reset,

No it does not (at this moment). See below.

Quoted 22 lines
Quoted 1 line

+static int sc16is7xx_reset(struct device *dev, struct regmap *regmaps[])

Simply pass "struct regmap *regmap" as the second argument. See sc16is7xx_setup_mctrl_ports() for example.

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1473

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+{
+	struct gpio_desc *reset_gpiod;

reset_gpiod -> reset_gpio

Review

Quoted 2 lines
(quoted diff) +2 -0
+	else if (!IS_ERR(reset_gpiod)) {
+		/* delay 5 us (at least 3 us) and deassert the gpio to exit the hard

reset */

You can omit the "delay 5 us" since it is obvious from the code. Maybe add that "The minimum reset pulse width is 3 us" as stated in the datasheet.

As a general note for your comments: capitalize the first letter,
ex: "Deassert GPIO" and not "deassert GPIO".

Quoted 5 lines

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1483

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+		udelay(5);
+		gpiod_set_value_cansleep(reset_gpiod, 0);

Move the comment "deassert the gpio to exit the hard reset" here. You could also simplify it as "Deassert GPIO.".

The problem here is that this only deasserts the reset if it were asserted before. Nothing happens if it already was deasserted. This makes the delay also pretty useless.

To make this a proper reset pulse for the device you must first assert the reset, then wait >3us, and finally deassert the reset.

Maarten Brock

author
#8 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Quoted 33 lines · Maarten Brock
Quoted 2 lines

From: Hugo Villeneuve <[email protected]>
Sent: Tuesday, 4 June 2024 16:23

<...>

Quoted 3 lines

Add function description from original comment "Reset device, purging any pending irq / data", since the comment applies to both hardware and software reset,

No it does not (at this moment). See below.

Quoted 16 lines
Quoted 1 line

+static int sc16is7xx_reset(struct device *dev, struct regmap *regmaps[])

Simply pass "struct regmap *regmap" as the second argument. See sc16is7xx_setup_mctrl_ports() for example.

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1473

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+{
+	struct gpio_desc *reset_gpiod;

reset_gpiod -> reset_gpio

Review

Quoted 2 lines
(quoted diff) +2 -0
+	else if (!IS_ERR(reset_gpiod)) {
+		/* delay 5 us (at least 3 us) and deassert the gpio to exit the hard

reset */

You can omit the "delay 5 us" since it is obvious from the code. Maybe add that "The minimum reset pulse width is 3 us" as stated in the datasheet.

Quoted 8 lines

As a general note for your comments: capitalize the first letter,
ex: "Deassert GPIO" and not "deassert GPIO".

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1483

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+		udelay(5);
+		gpiod_set_value_cansleep(reset_gpiod, 0);

Move the comment "deassert the gpio to exit the hard reset" here. You could also simplify it as "Deassert GPIO.".

Quoted 8 lines

The problem here is that this only deasserts the reset if it were asserted before. Nothing happens if it already was deasserted. This makes the delay also pretty useless.

To make this a proper reset pulse for the device you must first assert the reset, then wait >3us, and finally deassert the reset.

Maarten Brock

Hi Maarten,

My understanding is when calling devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW) and returning a valid (gpio_desc *), the flag GPIOD_OUT_LOW guarantees the GPIO is set to output and low (assert the reset pin).

Thanks,

Hui.

Quoted 1 line
#9 lore raw

RE: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Quoted 41 lines · Maarten Brock
Quoted 40 lines
Quoted 2 lines

From: Hugo Villeneuve <[email protected]>
Sent: Tuesday, 4 June 2024 16:23

<...>

Quoted 3 lines

Add function description from original comment "Reset device, purging any pending irq / data", since the comment applies to both hardware and software reset,

No it does not (at this moment). See below.

Quoted 16 lines
Quoted 1 line

+static int sc16is7xx_reset(struct device *dev, struct regmap *regmaps[])

Simply pass "struct regmap *regmap" as the second argument. See sc16is7xx_setup_mctrl_ports() for example.

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1473

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+{
+	struct gpio_desc *reset_gpiod;

reset_gpiod -> reset_gpio

Review

Quoted 2 lines
(quoted diff) +2 -0
+	else if (!IS_ERR(reset_gpiod)) {
+		/* delay 5 us (at least 3 us) and deassert the gpio to exit the hard

reset */

You can omit the "delay 5 us" since it is obvious from the code. Maybe add that "The minimum reset pulse width is 3 us" as stated in the datasheet.

Quoted 8 lines

As a general note for your comments: capitalize the first letter,
ex: "Deassert GPIO" and not "deassert GPIO".

drivers/tty/serial/sc16is7xx.c static const struct serial_rs485 sc16is7xx_rs485_supported = { · line 1483

Quoted 2 lines
drivers/tty/serial/sc16is7xx.c +2 -0
+		udelay(5);
+		gpiod_set_value_cansleep(reset_gpiod, 0);

Move the comment "deassert the gpio to exit the hard reset" here. You could also simplify it as "Deassert GPIO.".

The problem here is that this only deasserts the reset if it were asserted before. Nothing happens if it already was deasserted. This makes the delay also pretty useless.

To make this a proper reset pulse for the device you must first assert the reset, then wait >3us, and finally deassert the reset.

Quoted 7 lines
Quoted 1 line

Maarten Brock

Hi Maarten,

My understanding is when calling devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW) and returning a valid (gpio_desc *), the flag GPIOD_OUT_LOW guarantees the GPIO is set to output and low (assert the reset pin).

Ah, right. Sorry, I missed that.
So GPIOD_OUT_LOW disregards the inversion from GPIO_ACTIVE_LOW.
And gpiod_set_value_cansleep(reset_gpiod, 0) uses the inversion to make the pin high. Looks fine to me now.

Quoted 3 lines

Thanks,

Hui.

Maarten

#10 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Quoted 3 lines · Maarten Brock
Quoted 3 lines · Maarten Brock
Quoted 2 lines
Quoted 2 lines

From: Hugo Villeneuve <[email protected]>
Sent: Tuesday, 4 June 2024 16:23

<...>

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Add function description from original comment "Reset device, purging any pending irq / data", since the comment applies to both hardware and software reset,

No it does not (at this moment). See below.

...

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines

The problem here is that this only deasserts the reset if it were asserted before. Nothing happens if it already was deasserted. This makes the delay also pretty useless.

To make this a proper reset pulse for the device you must first assert the reset, then wait >3us, and finally deassert the reset.

Quoted 7 lines
Quoted 1 line

Maarten Brock

Hi Maarten,

My understanding is when calling devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW) and returning a valid (gpio_desc *), the flag GPIOD_OUT_LOW guarantees the GPIO is set to output and low (assert the reset pin).

Quoted 4 lines

Ah, right. Sorry, I missed that.
So GPIOD_OUT_LOW disregards the inversion from GPIO_ACTIVE_LOW.
And gpiod_set_value_cansleep(reset_gpiod, 0) uses the inversion to make the pin high. Looks fine to me now.

No, you used the correct terms "assert"/"deassert", the parameter there is logical, means "deassert". It was differently in the legacy GPIO APIs, which are almost gone.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

In reply to Maarten Brock
Quoted 6 lines · Maarten Brock
Quoted 6 lines
Quoted 4 lines

To make this a proper reset pulse for the device you must first assert the reset, then wait >3us, and finally deassert the reset.

Maarten Brock

Hi Maarten,

Quoted 7 lines
Quoted 4 lines

My understanding is when calling devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW) and returning a valid (gpio_desc *), the flag GPIOD_OUT_LOW guarantees the GPIO is set to output and low (assert the reset pin).

Ah, right. Sorry, I missed that.
So GPIOD_OUT_LOW disregards the inversion from GPIO_ACTIVE_LOW.

It doesn't.

Quoted 2 lines

And gpiod_set_value_cansleep(reset_gpiod, 0) uses the inversion to make the pin high. Looks fine to me now.

They both respect pin polarity.

Best regards,
Krzysztof

author
#12 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Quoted 11 lines · Krzysztof Kozlowski
Quoted 10 lines · Maarten Brock
Quoted 10 lines
Quoted 4 lines

To make this a proper reset pulse for the device you must first assert the reset, then wait >3us, and finally deassert the reset.

Maarten Brock

Hi Maarten,

My understanding is when calling devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW) and returning a valid (gpio_desc *), the flag GPIOD_OUT_LOW guarantees the GPIO is set to output and low (assert the reset pin).

Quoted 7 lines
Quoted 2 lines

Ah, right. Sorry, I missed that.
So GPIOD_OUT_LOW disregards the inversion from GPIO_ACTIVE_LOW.

It doesn't.

Quoted 2 lines

And gpiod_set_value_cansleep(reset_gpiod, 0) uses the inversion to make the pin high. Looks fine to me now.

They both respect pin polarity.

Will correct it.

Thanks.

Quoted 4 lines

Best regards,
Krzysztof

#13 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

In reply to Hui Wang
Quoted 3 lines · Hui Wang
Quoted 2 lines · Maarten Brock
Quoted 2 lines

From: Hugo Villeneuve <[email protected]>
Sent: Tuesday, 4 June 2024 16:23

<...>

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Add function description from original comment "Reset device, purging any pending irq / data", since the comment applies to both hardware and software reset,

No it does not (at this moment). See below.

...

Quoted 11 lines
Quoted 6 lines

The problem here is that this only deasserts the reset if it were asserted before. Nothing happens if it already was deasserted. This makes the delay also pretty useless.

To make this a proper reset pulse for the device you must first assert the reset, then wait >3us, and finally deassert the reset.

My understanding is when calling devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW) and returning a valid (gpio_desc *), the flag GPIOD_OUT_LOW guarantees the GPIO is set to output and low (assert the reset pin).

No, this is logical, not physical state. Maarten is correct. How did you test this?

Signature · 2 lines
With Best Regards,
Andy Shevchenko

A message that is not in the archive (sent off-list or not yet received): [email protected]

#15 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Quoted 43 lines · On Wed, 5 Jun 2024 20:55:21 +0800 Hui Wang <[email protected]> wrote:
Quoted 16 lines · Andy Shevchenko
Quoted 3 lines · Hui Wang
Quoted 2 lines · Maarten Brock
Quoted 2 lines

From: Hugo Villeneuve<[email protected]>
Sent: Tuesday, 4 June 2024 16:23

<...>

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Add function description from original comment "Reset device, purging any pending irq / data", since the comment applies to both hardware and software reset,

No it does not (at this moment). See below.

...

Quoted 4 lines
Quoted 4 lines

The problem here is that this only deasserts the reset if it were asserted before. Nothing happens if it already was deasserted. This makes the delay also pretty useless.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

To make this a proper reset pulse for the device you must first assert the reset, then wait >3us, and finally deassert the reset.

My understanding is when calling devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW) and returning a valid (gpio_desc *), the flag GPIOD_OUT_LOW guarantees the GPIO is set to output and low (assert the reset pin).

No, this is logical, not physical state. Maarten is correct. How did you test this?

Yes, Maarten, Krzysztof and you are correct. Thanks for pointing out this error.

If I call "reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);" I will get the result as below:

# cat /sys/kernel/debug/gpio

gpiochip4: GPIOs 128-159, parent: platform/30240000.gpio, 30240000.gpio:
gpio-141 ( |reset ) out lo ACTIVE LOW

If I call "reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);" I will get the result as below:

# cat /sys/kernel/debug/gpio
Quoted 8 lines

gpiochip4: GPIOs 128-159, parent: platform/30240000.gpio, 30240000.gpio:
gpio-141 ( |reset ) out hi ACTIVE LOW

I tested the reset pin by dumping chip registers, if the reset pin is asserted (out lo), I will get the result like this:

# cat /sys/kernel/debug/regmap/spi0.0-port0/registers 1: 10 2: ff 3: 00 
4: ec 5: ff 6: ff 7: ff 8: ff 9: ff a: ff b: ff c: ff d: ff e: ff f: 06

Hi Hui,
the best way to test a reset pin is with a voltmeter, if you can. It is way too easy to get confused with reset pins values/polarities, etc.

By the way, if the reset pin is asserted, you cannot communicate with the device, therefore dumping registers cannot work for debug purpose.

Hugo.

Quoted 14 lines

If the reset pin is deasserted (out hi), I will get:

# cat /sys/kernel/debug/regmap/spi0.0-port0/registers 1: 10 2: 01 3: 00 
4: 00 5: 60 6: 00 7: 2e 8: 40 9: 00 a: 00 b: ff c: 00 d: 00 e: 00 f: 06

My original code set the reset GPIO to high (the reset pin is deasserted) continuously, so I didn't notice this hidden error.

Thanks,

Hui.

Quoted 1 line
Signature · 1 line
Hugo Villeneuve
author
#16 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Quoted 6 lines · Hugo Villeneuve
Quoted 3 lines · On Wed, 5 Jun 2024 20:55:21 +0800 Hui Wang <[email protected]> wrote:
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Hui Wang

On 6/5/24 18:30, Maarten Brock wrote:

...

Quoted 5 lines
Quoted 2 lines
# cat /sys/kernel/debug/regmap/spi0.0-port0/registers 1: 10 2: ff 3: 00
4: ec 5: ff 6: ff 7: ff 8: ff 9: ff a: ff b: ff c: ff d: ff e: ff f: 06

Hi Hui,
the best way to test a reset pin is with a voltmeter, if you can. It is way too easy to get confused with reset pins values/polarities, etc.

Yes. got it.

Quoted 3 lines

By the way, if the reset pin is asserted, you cannot communicate with the device, therefore dumping registers cannot work for debug purpose.

Got it.  I just use it to check the reset pin status. If the returned register values look reasonable (not many 0xff), It means the reset GPIO is de-asserted.

Thanks.

Quoted 5 lines

Hugo.

Quoted 1 line
#17 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

In reply to Hui Wang

On Tue, 4 Jun 2024 21:27:26 +0800
Hui Wang <[email protected]> wrote:

Hi Hui,

Quoted 1 line

Certain designs connect a gpio to the reset pin, and the reset pin

"Some boards..."

Quoted 3 lines

needs to be setup correctly before accessing the chip.

Here adding a function to handle the chip reset. If the reset-gpios is

"Add a function..."

Quoted 79 lines

defined in the dt, do the hard reset through this gpio, othwerwise do the soft reset as before.

---
In the v2:
- move the soft reset and hard reset into one fucntion
- move the reset function to sc16is7xx.c and call it in _probe()
- add udelay(5) before deasserting the gpio reset pin

 drivers/tty/serial/sc16is7xx.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/tty/serial/sc16is7xx.c +24 -4
index bf0065d1c8e9..119abfb4607c 100644
@@ -14,6 +14,7 @@
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/export.h>
+#include <linux/gpio/consumer.h>
 #include <linux/gpio/driver.h>
 #include <linux/idr.h>
 #include <linux/kthread.h>
@@ -1467,6 +1468,25 @@ static const struct serial_rs485 sc16is7xx_rs485_supported = {
 	.delay_rts_after_send = 1,	/* Not supported but keep returning -EINVAL */
 };
 
+static int sc16is7xx_reset(struct device *dev, struct regmap *regmaps[])
+{
+	struct gpio_desc *reset_gpiod;
+
+	reset_gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
+	if (!reset_gpiod)
+		/* soft reset device, purging any pending irq / data */
+		regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
+			     SC16IS7XX_IOCONTROL_SRESET_BIT);
+	else if (!IS_ERR(reset_gpiod)) {
+		/* delay 5 us (at least 3 us) and deassert the gpio to exit the hard reset */
+		udelay(5);
+		gpiod_set_value_cansleep(reset_gpiod, 0);
+	} else
+		return PTR_ERR(reset_gpiod);
+
+	return 0;
+}
+
 int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 		    struct regmap *regmaps[], int irq)
 {
@@ -1527,6 +1547,10 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 	s->devtype = devtype;
 	dev_set_drvdata(dev, s);
 
+	ret = sc16is7xx_reset(dev, regmaps);
+	if (ret)
+		goto out_clk;
+
 	kthread_init_worker(&s->kworker);
 	s->kworker_task = kthread_run(kthread_worker_fn, &s->kworker,
 				      "sc16is7xx");
@@ -1536,10 +1560,6 @@ int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 	}
 	sched_set_fifo(s->kworker_task);
 
-	/* reset device, purging any pending irq / data */
-	regmap_write(regmaps[0], SC16IS7XX_IOCONTROL_REG,
-		     SC16IS7XX_IOCONTROL_SRESET_BIT);
-
 	/* Mark each port line and status as uninitialised. */
 	for (i = 0; i < devtype->nr_uart; ++i) {
 		s->p[i].port.line = SC16IS7XX_MAX_DEVS;
Signature · 1 line
2.34.1
Signature · 1 line
Hugo Villeneuve
author
#18 lore raw

Re: [PATCH v2 2/2] serial: sc16is7xx: hard reset the chip if reset-gpios is defined in dt

Quoted 5 lines · Hugo Villeneuve

On Tue, 4 Jun 2024 21:27:26 +0800
Hui Wang <[email protected]> wrote:

Hi Hui,

Quoted 9 lines
Quoted 1 line

Certain designs connect a gpio to the reset pin, and the reset pin

"Some boards..."

Quoted 3 lines

needs to be setup correctly before accessing the chip.

Here adding a function to handle the chip reset. If the reset-gpios is

"Add a function..."

Got them, will fix them in the v3.

Thanks.

#19 lore raw
In reply to Hui Wang
Quoted 6 lines · Hui Wang

In some designs, the chip reset pin is connected to a gpio, this gpio needs to be set correctly before probing the driver, so adding a reset-gpios in the device tree.

Where did these happen?!?!?

NAK

Please talk with your colleagues in Canonical to explain how this works. Then read submitting patches document.

Best regards,
Krzysztof

author
#20 lore raw
Quoted 5 lines · Krzysztof Kozlowski
Quoted 4 lines · Hui Wang

In some designs, the chip reset pin is connected to a gpio, this gpio needs to be set correctly before probing the driver, so adding a reset-gpios in the device tree.

Quoted 8 lines
Quoted 2 lines

Where did these happen?!?!?

NAK

Please talk with your colleagues in Canonical to explain how this works. Then read submitting patches document.

Sorry, my bad, I will read submitting-pathces.rst carefully.

Thanks,

Hui.

Quoted 4 lines

Best regards,
Krzysztof

#21 lore raw
In reply to Hui Wang
Quoted 1 line · Hui Wang

In some designs, the chip reset pin is connected to a gpio, this

GPIO

Quoted 1 line

gpio needs to be set correctly before probing the driver, so adding

GPIO

Quoted 1 line

a reset-gpios in the device tree.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#22 lore raw
Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Hui Wang

In some designs, the chip reset pin is connected to a gpio, this

GPIO

Quoted 1 line

gpio needs to be set correctly before probing the driver, so adding

GPIO

Got them, and will fix them in the v3.

Thanks.

Quoted 2 lines
Quoted 1 line

a reset-gpios in the device tree.

#23 lore raw
In reply to Hui Wang
Quoted 1 line · On Tue, 4 Jun 2024 21:27:25 +0800 Hui Wang <[email protected]> wrote:

In some designs, the chip reset pin is connected to a gpio, this

"and this ..."

Quoted 1 line

gpio needs to be set correctly before probing the driver, so adding

"so add ..."

Quoted 48 lines

a reset-gpios in the device tree.

---
In the v2:
- include the gpio.h
- run the 'make dt_binding_check' and 'make dtbs_check'

 Documentation/devicetree/bindings/serial/nxp,sc16is7xx.yaml | 5 +++++
 1 file changed, 5 insertions(+)
Documentation/devicetree/bindings/serial/nxp,sc16is7xx.yaml +5 -0
index 5dec15b7e7c3..88871480018e 100644
@@ -28,6 +28,9 @@ properties:
   clocks:
     maxItems: 1
 
+  reset-gpios:
+    maxItems: 1
+
   clock-frequency:
     description:
       When there is no clock provider visible to the platform, this
@@ -91,6 +94,7 @@ unevaluatedProperties: false
 examples:
   - |
     #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
     i2c {
         #address-cells = <1>;
         #size-cells = <0>;
@@ -120,6 +124,7 @@ examples:
             compatible = "nxp,sc16is752";
             reg = <0x54>;
             clocks = <&clk20m>;
+            reset-gpios = <&gpio5 13 GPIO_ACTIVE_LOW>;
             interrupt-parent = <&gpio3>;
             interrupts = <7 IRQ_TYPE_EDGE_FALLING>;
             nxp,modem-control-line-ports = <0 1>; /* Ports 0 and 1 as modem control lines */
Signature · 1 line
2.34.1
Signature · 1 line
Hugo Villeneuve
author
#24 lore raw
Quoted 10 lines · Hugo Villeneuve
Quoted 1 line · On Tue, 4 Jun 2024 21:27:25 +0800 Hui Wang <[email protected]> wrote:

In some designs, the chip reset pin is connected to a gpio, this

"and this ..."

Quoted 1 line

gpio needs to be set correctly before probing the driver, so adding

"so add ..."

OK, will fix them in the v3.

Thanks.