diff mbox series

[2/2] serial: sc16is7xx: setup reset pin if it is defined in device tree

Message ID 20240603123710.649549-2-hui.wang@canonical.com
State New
Headers show
Series [1/2] dt-bindings: serial: sc16is7xx: add reset-gpios | expand

Commit Message

Hui Wang June 3, 2024, 12:37 p.m. UTC
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 reset pin. This change has no
impact if there is no reset_gpios defined in the device tree.

Signed-off-by: Hui Wang <hui.wang@canonical.com>
---
 drivers/tty/serial/sc16is7xx.c     | 22 ++++++++++++++++++++++
 drivers/tty/serial/sc16is7xx.h     |  2 ++
 drivers/tty/serial/sc16is7xx_i2c.c |  2 ++
 drivers/tty/serial/sc16is7xx_spi.c |  2 ++
 4 files changed, 28 insertions(+)
diff mbox series

Patch

diff --git a/drivers/tty/serial/sc16is7xx.c b/drivers/tty/serial/sc16is7xx.c
index bf0065d1c8e9..53bfb603b03c 100644
--- a/drivers/tty/serial/sc16is7xx.c
+++ b/drivers/tty/serial/sc16is7xx.c
@@ -19,6 +19,7 @@ 
 #include <linux/kthread.h>
 #include <linux/mod_devicetable.h>
 #include <linux/module.h>
+#include <linux/of_gpio.h>
 #include <linux/property.h>
 #include <linux/regmap.h>
 #include <linux/sched.h>
@@ -1467,6 +1468,27 @@  static const struct serial_rs485 sc16is7xx_rs485_supported = {
 	.delay_rts_after_send = 1,	/* Not supported but keep returning -EINVAL */
 };
 
+void sc16is7xx_setup_reset_pin(struct device *dev)
+{
+	struct device_node *np = dev->of_node;
+	int reset_gpio, err;
+
+	reset_gpio = of_get_named_gpio(np, "reset-gpios", 0);
+	if (!gpio_is_valid(reset_gpio))
+		return;
+
+	err = devm_gpio_request_one(dev, reset_gpio, GPIOF_OUT_INIT_LOW,
+				    "sc16is7xx-reset");
+	if (err) {
+		dev_err(dev, "failed to request sc16is7xx-reset-gpios: %d\n", err);
+		return;
+	}
+
+	/* Deassert the reset pin */
+	gpio_set_value_cansleep(reset_gpio, 1);
+}
+EXPORT_SYMBOL_GPL(sc16is7xx_setup_reset_pin);
+
 int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 		    struct regmap *regmaps[], int irq)
 {
diff --git a/drivers/tty/serial/sc16is7xx.h b/drivers/tty/serial/sc16is7xx.h
index afb784eaee45..f4ae114cc41a 100644
--- a/drivers/tty/serial/sc16is7xx.h
+++ b/drivers/tty/serial/sc16is7xx.h
@@ -33,6 +33,8 @@  const char *sc16is7xx_regmap_name(u8 port_id);
 
 unsigned int sc16is7xx_regmap_port_mask(unsigned int port_id);
 
+void sc16is7xx_setup_reset_pin(struct device *dev);
+
 int sc16is7xx_probe(struct device *dev, const struct sc16is7xx_devtype *devtype,
 		    struct regmap *regmaps[], int irq);
 
diff --git a/drivers/tty/serial/sc16is7xx_i2c.c b/drivers/tty/serial/sc16is7xx_i2c.c
index 3ed47c306d85..9833c3b935c2 100644
--- a/drivers/tty/serial/sc16is7xx_i2c.c
+++ b/drivers/tty/serial/sc16is7xx_i2c.c
@@ -21,6 +21,8 @@  static int sc16is7xx_i2c_probe(struct i2c_client *i2c)
 	if (!devtype)
 		return dev_err_probe(&i2c->dev, -ENODEV, "Failed to match device\n");
 
+	sc16is7xx_setup_reset_pin(&i2c->dev);
+
 	memcpy(&regcfg, &sc16is7xx_regcfg, sizeof(struct regmap_config));
 
 	for (i = 0; i < devtype->nr_uart; i++) {
diff --git a/drivers/tty/serial/sc16is7xx_spi.c b/drivers/tty/serial/sc16is7xx_spi.c
index 73df36f8a7fd..ce38561faaf0 100644
--- a/drivers/tty/serial/sc16is7xx_spi.c
+++ b/drivers/tty/serial/sc16is7xx_spi.c
@@ -38,6 +38,8 @@  static int sc16is7xx_spi_probe(struct spi_device *spi)
 	if (!devtype)
 		return dev_err_probe(&spi->dev, -ENODEV, "Failed to match device\n");
 
+	sc16is7xx_setup_reset_pin(&spi->dev);
+
 	memcpy(&regcfg, &sc16is7xx_regcfg, sizeof(struct regmap_config));
 
 	for (i = 0; i < devtype->nr_uart; i++) {