Message ID | 20240506-mcp251xfd-gpio-feature-v2-5-615b16fa8789@ew.tq-group.com (mailing list archive) |
---|---|
State | Awaiting Upstream |
Delegated to: | Netdev Maintainers |
Headers | show |
Series | can: mcp251xfd: add gpio functionality | expand |
Context | Check | Description |
---|---|---|
netdev/tree_selection | success | Series ignored based on subject, async |
On 06.05.2024 07:59:47, Gregor Herburger wrote: > The mcp251xfd devices allow two pins to be configured as gpio. Add this > functionality to driver. > > Signed-off-by: Gregor Herburger <gregor.herburger@ew.tq-group.com> > --- > drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 173 +++++++++++++++++++++++++ > drivers/net/can/spi/mcp251xfd/mcp251xfd.h | 6 + > 2 files changed, 179 insertions(+) > > diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c > index 4739ad80ef2a..de301f3a2f4e 100644 > --- a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c > +++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c > @@ -16,6 +16,7 @@ > #include <linux/bitfield.h> > #include <linux/clk.h> > #include <linux/device.h> > +#include <linux/gpio/driver.h> > #include <linux/mod_devicetable.h> > #include <linux/module.h> > #include <linux/pm_runtime.h> > @@ -1768,6 +1769,172 @@ static int mcp251xfd_register_check_rx_int(struct mcp251xfd_priv *priv) > return 0; > } > > +#ifdef CONFIG_GPIOLIB > +static const char * const mcp251xfd_gpio_names[] = {"GPIO0", "GPIO1"}; please add spaces after { and before }. > + > +static int mcp251xfd_gpio_request(struct gpio_chip *chip, unsigned int offset) > +{ > + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); > + u32 pin_mask = MCP251XFD_REG_IOCON_PM0 << offset; Can you add MCP251XFD_REG_IOCON_PM(), MCP251XFD_REG_IOCON_TRIS(), MCP251XFD_REG_IOCON_LAT(), MCP251XFD_REG_IOCON_GPIO() macros? > + int ret; > + > + if (priv->rx_int && offset == 1) { > + netdev_err(priv->ndev, "Can't use GPIO 1 with RX-INT!\n"); > + return -EINVAL; > + } > + > + ret = pm_runtime_resume_and_get(priv->ndev->dev.parent); > + if (ret) > + return ret; > + > + return regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, > + pin_mask, pin_mask); > +} > + > +static void mcp251xfd_gpio_free(struct gpio_chip *chip, unsigned int offset) > +{ > + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); > + > + pm_runtime_put(priv->ndev->dev.parent); > +} > + > +static int mcp251xfd_gpio_get_direction(struct gpio_chip *chip, > + unsigned int offset) > +{ > + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); > + u32 mask = MCP251XFD_REG_IOCON_TRIS0 << offset; > + u32 val; > + > + regmap_read(priv->map_reg, MCP251XFD_REG_IOCON, &val); Please print an error if regmap_read() throws an error. > + > + if (mask & val) > + return GPIO_LINE_DIRECTION_IN; > + > + return GPIO_LINE_DIRECTION_OUT; > +} > + > +static int mcp251xfd_gpio_get(struct gpio_chip *chip, unsigned int offset) > +{ > + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); > + u32 mask = MCP251XFD_REG_IOCON_GPIO0 << offset; > + u32 val; > + > + regmap_read(priv->map_reg, MCP251XFD_REG_IOCON, &val); same here > + > + return !!(mask & val); > +} > + > +static int mcp251xfd_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask, > + unsigned long *bit) > +{ > + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); > + u32 val; > + int ret; > + > + ret = regmap_read(priv->map_reg, MCP251XFD_REG_IOCON, &val); > + if (ret) > + return ret; > + > + *bit = FIELD_GET(MCP251XFD_REG_IOCON_GPIO_MASK, val) & *mask; > + > + return 0; > +} > + > +static int mcp251xfd_gpio_direction_output(struct gpio_chip *chip, > + unsigned int offset, int value) > +{ > + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); > + u32 dir_mask = MCP251XFD_REG_IOCON_TRIS0 << offset; > + u32 val_mask = MCP251XFD_REG_IOCON_LAT0 << offset; > + u32 val; > + > + if (value) > + val = val_mask; > + else > + val = 0; > + > + return regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, > + dir_mask | val_mask, val); > +} > + > +static int mcp251xfd_gpio_direction_input(struct gpio_chip *chip, > + unsigned int offset) > +{ > + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); > + u32 dir_mask = MCP251XFD_REG_IOCON_TRIS0 << offset; > + > + return regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, > + dir_mask, dir_mask); > +} > + > +static void mcp251xfd_gpio_set(struct gpio_chip *chip, unsigned int offset, > + int value) > +{ > + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); > + u32 val_mask = MCP251XFD_REG_IOCON_LAT0 << offset; > + u32 val; > + int ret; > + > + if (value) > + val = val_mask; > + else > + val = 0; > + > + ret = regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, > + val_mask, val); > + if (ret) > + dev_warn(&priv->spi->dev, > + "Failed to set GPIO %u: %d\n", offset, ret); dev_error() > +} > + > +static void mcp251xfd_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask, > + unsigned long *bits) > +{ > + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); > + u32 val; > + int ret; > + > + val = FIELD_PREP(MCP251XFD_REG_IOCON_LAT_MASK, *bits); > + > + ret = regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, > + MCP251XFD_REG_IOCON_LAT_MASK, val); > + if (ret) > + dev_warn(&priv->spi->dev, "Failed to set GPIOs %d\n", ret); dev_error() regards, Marc
On 06.05.2024 07:59:47, Gregor Herburger wrote: > The mcp251xfd devices allow two pins to be configured as gpio. Add this > functionality to driver. > > Signed-off-by: Gregor Herburger <gregor.herburger@ew.tq-group.com> > --- > drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 173 +++++++++++++++++++++++++ > drivers/net/can/spi/mcp251xfd/mcp251xfd.h | 6 + > 2 files changed, 179 insertions(+) > > diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c > index 4739ad80ef2a..de301f3a2f4e 100644 > --- a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c > +++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c > @@ -16,6 +16,7 @@ > #include <linux/bitfield.h> > #include <linux/clk.h> > #include <linux/device.h> > +#include <linux/gpio/driver.h> > #include <linux/mod_devicetable.h> > #include <linux/module.h> > #include <linux/pm_runtime.h> > @@ -1768,6 +1769,172 @@ static int mcp251xfd_register_check_rx_int(struct mcp251xfd_priv *priv) > return 0; > } > > +#ifdef CONFIG_GPIOLIB > +static const char * const mcp251xfd_gpio_names[] = {"GPIO0", "GPIO1"}; > + > +static int mcp251xfd_gpio_request(struct gpio_chip *chip, unsigned int offset) > +{ > + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); > + u32 pin_mask = MCP251XFD_REG_IOCON_PM0 << offset; > + int ret; > + > + if (priv->rx_int && offset == 1) { > + netdev_err(priv->ndev, "Can't use GPIO 1 with RX-INT!\n"); > + return -EINVAL; > + } > + > + ret = pm_runtime_resume_and_get(priv->ndev->dev.parent); > + if (ret) > + return ret; > + > + return regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, > + pin_mask, pin_mask); The PM bits _should_ be 1 here, as it's the reset default. But you have to convert mcp251xfd_chip_rx_int_enable() and mcp251xfd_chip_rx_int_disable() to from regmap_write() to regmap_update_bits(). Please do this in a separate patch before adding the gpio support. > +} regards, Marc
diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c index 4739ad80ef2a..de301f3a2f4e 100644 --- a/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c @@ -16,6 +16,7 @@ #include <linux/bitfield.h> #include <linux/clk.h> #include <linux/device.h> +#include <linux/gpio/driver.h> #include <linux/mod_devicetable.h> #include <linux/module.h> #include <linux/pm_runtime.h> @@ -1768,6 +1769,172 @@ static int mcp251xfd_register_check_rx_int(struct mcp251xfd_priv *priv) return 0; } +#ifdef CONFIG_GPIOLIB +static const char * const mcp251xfd_gpio_names[] = {"GPIO0", "GPIO1"}; + +static int mcp251xfd_gpio_request(struct gpio_chip *chip, unsigned int offset) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 pin_mask = MCP251XFD_REG_IOCON_PM0 << offset; + int ret; + + if (priv->rx_int && offset == 1) { + netdev_err(priv->ndev, "Can't use GPIO 1 with RX-INT!\n"); + return -EINVAL; + } + + ret = pm_runtime_resume_and_get(priv->ndev->dev.parent); + if (ret) + return ret; + + return regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, + pin_mask, pin_mask); +} + +static void mcp251xfd_gpio_free(struct gpio_chip *chip, unsigned int offset) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + + pm_runtime_put(priv->ndev->dev.parent); +} + +static int mcp251xfd_gpio_get_direction(struct gpio_chip *chip, + unsigned int offset) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 mask = MCP251XFD_REG_IOCON_TRIS0 << offset; + u32 val; + + regmap_read(priv->map_reg, MCP251XFD_REG_IOCON, &val); + + if (mask & val) + return GPIO_LINE_DIRECTION_IN; + + return GPIO_LINE_DIRECTION_OUT; +} + +static int mcp251xfd_gpio_get(struct gpio_chip *chip, unsigned int offset) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 mask = MCP251XFD_REG_IOCON_GPIO0 << offset; + u32 val; + + regmap_read(priv->map_reg, MCP251XFD_REG_IOCON, &val); + + return !!(mask & val); +} + +static int mcp251xfd_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask, + unsigned long *bit) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 val; + int ret; + + ret = regmap_read(priv->map_reg, MCP251XFD_REG_IOCON, &val); + if (ret) + return ret; + + *bit = FIELD_GET(MCP251XFD_REG_IOCON_GPIO_MASK, val) & *mask; + + return 0; +} + +static int mcp251xfd_gpio_direction_output(struct gpio_chip *chip, + unsigned int offset, int value) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 dir_mask = MCP251XFD_REG_IOCON_TRIS0 << offset; + u32 val_mask = MCP251XFD_REG_IOCON_LAT0 << offset; + u32 val; + + if (value) + val = val_mask; + else + val = 0; + + return regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, + dir_mask | val_mask, val); +} + +static int mcp251xfd_gpio_direction_input(struct gpio_chip *chip, + unsigned int offset) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 dir_mask = MCP251XFD_REG_IOCON_TRIS0 << offset; + + return regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, + dir_mask, dir_mask); +} + +static void mcp251xfd_gpio_set(struct gpio_chip *chip, unsigned int offset, + int value) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 val_mask = MCP251XFD_REG_IOCON_LAT0 << offset; + u32 val; + int ret; + + if (value) + val = val_mask; + else + val = 0; + + ret = regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, + val_mask, val); + if (ret) + dev_warn(&priv->spi->dev, + "Failed to set GPIO %u: %d\n", offset, ret); +} + +static void mcp251xfd_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask, + unsigned long *bits) +{ + struct mcp251xfd_priv *priv = gpiochip_get_data(chip); + u32 val; + int ret; + + val = FIELD_PREP(MCP251XFD_REG_IOCON_LAT_MASK, *bits); + + ret = regmap_update_bits(priv->map_reg, MCP251XFD_REG_IOCON, + MCP251XFD_REG_IOCON_LAT_MASK, val); + if (ret) + dev_warn(&priv->spi->dev, "Failed to set GPIOs %d\n", ret); +} + +static int mcp251fdx_gpio_setup(struct mcp251xfd_priv *priv) +{ + struct gpio_chip *gc = &priv->gc; + + if (!device_property_present(&priv->spi->dev, "gpio-controller")) + return 0; + + gc->label = dev_name(&priv->spi->dev); + gc->parent = &priv->spi->dev; + gc->owner = THIS_MODULE; + gc->request = mcp251xfd_gpio_request; + gc->free = mcp251xfd_gpio_free; + gc->get_direction = mcp251xfd_gpio_get_direction; + gc->direction_output = mcp251xfd_gpio_direction_output; + gc->direction_input = mcp251xfd_gpio_direction_input; + gc->get = mcp251xfd_gpio_get; + gc->get_multiple = mcp251xfd_gpio_get_multiple; + gc->set = mcp251xfd_gpio_set; + gc->set_multiple = mcp251xfd_gpio_set_multiple; + gc->base = -1; + gc->can_sleep = true; + gc->ngpio = ARRAY_SIZE(mcp251xfd_gpio_names); + gc->names = mcp251xfd_gpio_names; + + return devm_gpiochip_add_data(&priv->spi->dev, gc, priv); +} +#else +static inline int mcp251fdx_gpio_setup(struct mcp251xfd_priv *priv) +{ + return 0; +} +#endif + static int mcp251xfd_register_get_dev_id(const struct mcp251xfd_priv *priv, u32 *dev_id, u32 *effective_speed_hz_slow, @@ -2141,6 +2308,12 @@ static int mcp251xfd_probe(struct spi_device *spi) if (err) goto out_free_candev; + err = mcp251fdx_gpio_setup(priv); + if (err) { + dev_err_probe(&spi->dev, err, "Failed to register gpio-controller.\n"); + goto out_free_candev; + } + err = mcp251xfd_register(priv); if (err) { dev_err_probe(&spi->dev, err, "Failed to detect %s.\n", diff --git a/drivers/net/can/spi/mcp251xfd/mcp251xfd.h b/drivers/net/can/spi/mcp251xfd/mcp251xfd.h index 75d5a8a25415..dc34da848f00 100644 --- a/drivers/net/can/spi/mcp251xfd/mcp251xfd.h +++ b/drivers/net/can/spi/mcp251xfd/mcp251xfd.h @@ -15,6 +15,7 @@ #include <linux/can/dev.h> #include <linux/can/rx-offload.h> #include <linux/gpio/consumer.h> +#include <linux/gpio/driver.h> #include <linux/kernel.h> #include <linux/netdevice.h> #include <linux/regmap.h> @@ -337,8 +338,10 @@ #define MCP251XFD_REG_IOCON_PM0 BIT(24) #define MCP251XFD_REG_IOCON_GPIO1 BIT(17) #define MCP251XFD_REG_IOCON_GPIO0 BIT(16) +#define MCP251XFD_REG_IOCON_GPIO_MASK GENMASK(17, 16) #define MCP251XFD_REG_IOCON_LAT1 BIT(9) #define MCP251XFD_REG_IOCON_LAT0 BIT(8) +#define MCP251XFD_REG_IOCON_LAT_MASK GENMASK(9, 8) #define MCP251XFD_REG_IOCON_XSTBYEN BIT(6) #define MCP251XFD_REG_IOCON_TRIS1 BIT(1) #define MCP251XFD_REG_IOCON_TRIS0 BIT(0) @@ -660,6 +663,9 @@ struct mcp251xfd_priv { struct mcp251xfd_devtype_data devtype_data; struct can_berr_counter bec; +#ifdef CONFIG_GPIOLIB + struct gpio_chip gc; +#endif }; #define MCP251XFD_IS(_model) \
The mcp251xfd devices allow two pins to be configured as gpio. Add this functionality to driver. Signed-off-by: Gregor Herburger <gregor.herburger@ew.tq-group.com> --- drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c | 173 +++++++++++++++++++++++++ drivers/net/can/spi/mcp251xfd/mcp251xfd.h | 6 + 2 files changed, 179 insertions(+)