Skip to contentSkip to conversation

: tcan4x5x: rework regmap support

RFC New Marc Kleine-Budde 3 participants 19 messages Dec 16, 2020 – Dec 18, 2020

Subsystems (2)
lore
Share & tools
lore

Marc Kleine-Budde

author maintainer
#0 lore raw

Hello,

this series first cleans up the the SPI regmap and then fixes several problem:
- tcan4x5x_regmap_gather_write(), tcan4x5x_regmap_read():
Do not place variable "addr" on stack and use it as buffer for SPI transfer. Buffers for SPI transfers must be allocated from DMA save memory.
- tcan4x5x_regmap_gather_write(), tcan4x5x_regmap_read():
Halfe number of SPI transfers by using a single buffer + memcpy(). This improves the performance, especially on SPI controllers, which use interrupt based transfers.
- Use "8" bits per word, not "32". This makes it possible to use this driver on SoCs like the Raspberry Pi, which SPI host controller drivers only support 8 bits per word.

With this series, the driver is able to detect a tcan4x5x on a Raspberry Pi.

regards,
Marc

author maintainer
#1 lore raw

PATCH 1/16 can: tcan4x5x: replace DEVICE_NAME by KBUILD_MODNAME

This patch replaces the DEVICE_NAME macro by KBUILD_MODNAME and removed the superfluous DEVICE_NAME.

---
 drivers/net/can/m_can/tcan4x5x.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/net/can/m_can/tcan4x5x.c +3 -4
index 24c737c4fc44..1b5f706674af 100644
@@ -10,7 +10,6 @@
 
 #include "m_can.h"
 
-#define DEVICE_NAME "tcan4x5x"
 #define TCAN4X5X_EXT_CLK_DEF 40000000
 
 #define TCAN4X5X_DEV_ID0 0x00
@@ -132,7 +131,7 @@ static inline struct tcan4x5x_priv *cdev_to_priv(struct m_can_classdev *cdev)
 }
 
 static struct can_bittiming_const tcan4x5x_bittiming_const = {
-	.name = DEVICE_NAME,
+	.name = KBUILD_MODNAME,
 	.tseg1_min = 2,
 	.tseg1_max = 31,
 	.tseg2_min = 2,
@@ -144,7 +143,7 @@ static struct can_bittiming_const tcan4x5x_bittiming_const = {
 };
 
 static struct can_bittiming_const tcan4x5x_data_bittiming_const = {
-	.name = DEVICE_NAME,
+	.name = KBUILD_MODNAME,
 	.tseg1_min = 1,
 	.tseg1_max = 32,
 	.tseg2_min = 1,
@@ -544,7 +543,7 @@ MODULE_DEVICE_TABLE(spi, tcan4x5x_id_table);
 
 static struct spi_driver tcan4x5x_can_driver = {
 	.driver = {
-		.name = DEVICE_NAME,
+		.name = KBUILD_MODNAME,
 		.of_match_table = tcan4x5x_of_match,
 		.pm = NULL,
 	},

base-commit: efd5a1584537698220578227e6467638307c2a0b

Signature · 1 line
2.29.2
author maintainer
#2 lore raw

PATCH 2/16 can: tcan4x5x: beautify indention of tcan4x5x_of_match and tcan4x5x_id_table

In reply to Marc Kleine-Budde

This patch beautifies the indention of the tcan4x5x_of_match and tcan4x5x_id_table.

---
 drivers/net/can/m_can/tcan4x5x.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/net/can/m_can/tcan4x5x.c +8 -5
index 1b5f706674af..1b47c9d32c30 100644
@@ -527,17 +527,20 @@ static int tcan4x5x_can_remove(struct spi_device *spi)
 }
 
 static const struct of_device_id tcan4x5x_of_match[] = {
-	{ .compatible = "ti,tcan4x5x", },
-	{ }
+	{
+		.compatible = "ti,tcan4x5x",
+	}, {
+		/* sentinel */
+	},
 };
 MODULE_DEVICE_TABLE(of, tcan4x5x_of_match);
 
 static const struct spi_device_id tcan4x5x_id_table[] = {
 	{
-		.name		= "tcan4x5x",
-		.driver_data	= 0,
+		.name = "tcan4x5x",
+	}, {
+		/* sentinel */
 	},
-	{ }
 };
 MODULE_DEVICE_TABLE(spi, tcan4x5x_id_table);
 
Signature · 1 line
2.29.2
author maintainer
#3 lore raw

PATCH 3/16 can: tcan4x5x: rename tcan4x5x.c -> tcan4x5x-core.c

In reply to Marc Kleine-Budde

This is a preparation patch to move the regmap related code into a seperate file. This patch removes the tcan4x5x.c to tcan4x5x-core.c.

---
 drivers/net/can/m_can/Makefile                        | 3 +++
 drivers/net/can/m_can/{tcan4x5x.c => tcan4x5x-core.c} | 0
 2 files changed, 3 insertions(+)
 rename drivers/net/can/m_can/{tcan4x5x.c => tcan4x5x-core.c} (100%)
drivers/net/can/m_can/Makefile +3 -0
index ef7963ff2006..91f9190dc007 100644
@@ -7,3 +7,6 @@ obj-$(CONFIG_CAN_M_CAN) += m_can.o
 obj-$(CONFIG_CAN_M_CAN_PCI) += m_can_pci.o
 obj-$(CONFIG_CAN_M_CAN_PLATFORM) += m_can_platform.o
 obj-$(CONFIG_CAN_M_CAN_TCAN4X5X) += tcan4x5x.o
+
+tcan4x5x-objs :=
+tcan4x5x-objs += tcan4x5x-core.o
drivers/net/can/m_can/tcan4x5x-core.c renamed from drivers/net/can/m_can/tcan4x5x.c +0 -0
similarity index 100%
rename from drivers/net/can/m_can/tcan4x5x.c
rename to drivers/net/can/m_can/tcan4x5x-core.c
Signature · 1 line
2.29.2
author maintainer
#4 lore raw

PATCH 4/16 can: tcan4x5x: move regmap code into seperate file

In reply to Marc Kleine-Budde

This patch moves the regmap code into a seperate file.

---
 drivers/net/can/m_can/Makefile          |   1 +
 drivers/net/can/m_can/tcan4x5x-core.c   | 104 +-----------------------
 drivers/net/can/m_can/tcan4x5x-regmap.c |  95 ++++++++++++++++++++++
 drivers/net/can/m_can/tcan4x5x.h        |  34 ++++++++
 4 files changed, 133 insertions(+), 101 deletions(-)
 create mode 100644 drivers/net/can/m_can/tcan4x5x-regmap.c
 create mode 100644 drivers/net/can/m_can/tcan4x5x.h
drivers/net/can/m_can/Makefile +1 -0
index 91f9190dc007..d717bbc9e033 100644
@@ -10,3 +10,4 @@ obj-$(CONFIG_CAN_M_CAN_TCAN4X5X) += tcan4x5x.o
 
 tcan4x5x-objs :=
 tcan4x5x-objs += tcan4x5x-core.o
+tcan4x5x-objs += tcan4x5x-regmap.o
drivers/net/can/m_can/tcan4x5x-core.c +3 -101

145 lines not on this page · raw message

drivers/net/can/m_can/tcan4x5x-regmap.c new file +95 -0

95 lines not on this page · raw message

drivers/net/can/m_can/tcan4x5x.h new file +34 -0
new file mode 100644
index 000000000000..e5bdd91b8005
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * tcan4x5x - Texas Instruments TCAN4x5x Family CAN controller driver
+ *
+ * Copyright (c) 2020 Pengutronix,
+ *                    Marc Kleine-Budde <[email protected]>
+ */
+
+#ifndef _TCAN4X5X_H
+#define _TCAN4X5X_H
+
+#include <linux/gpio/consumer.h>
+#include <linux/regmap.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
+#include <linux/spi/spi.h>
+
+#include "m_can.h"
+
+struct tcan4x5x_priv {
+	struct m_can_classdev cdev;
+
+	struct regmap *regmap;
+	struct spi_device *spi;
+
+	struct gpio_desc *reset_gpio;
+	struct gpio_desc *device_wake_gpio;
+	struct gpio_desc *device_state_gpio;
+	struct regulator *power;
+};
+
+int tcan4x5x_regmap_init(struct tcan4x5x_priv *priv);
+
+#endif
Signature · 1 line
2.29.2
author maintainer
#5 lore raw

PATCH 5/16 can: tcan4x5x: mark struct regmap_bus tcan4x5x_bus as constant

In reply to Marc Kleine-Budde

This patch marks the struct regmap_bus tcan4x5x_bus as constant.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/can/m_can/tcan4x5x-regmap.c +1 -1
index f130c3586543..1d139554fc16 100644
@@ -76,7 +76,7 @@ static const struct regmap_config tcan4x5x_regmap = {
 	.max_register = TCAN4X5X_MAX_REGISTER,
 };
 
-static struct regmap_bus tcan4x5x_bus = {
+static const struct regmap_bus tcan4x5x_bus = {
 	.write = tcan4x5x_regmap_write,
 	.gather_write = regmap_spi_gather_write,
 	.async_write = regmap_spi_async_write,
Signature · 1 line
2.29.2
author maintainer
#6 lore raw

PATCH 6/16 can: tcan4x5x: tcan4x5x_bus: remove not needed read_flag_mask

In reply to Marc Kleine-Budde

With C99 initializers, all non mentioned members are initialzied to 0, so remove not needed initialization of read_flag_mask.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 1 -
 1 file changed, 1 deletion(-)
drivers/net/can/m_can/tcan4x5x-regmap.c +0 -1
index 1d139554fc16..20bf9a57c9b1 100644
@@ -82,7 +82,6 @@ static const struct regmap_bus tcan4x5x_bus = {
 	.async_write = regmap_spi_async_write,
 	.async_alloc = regmap_spi_async_alloc,
 	.read = tcan4x5x_regmap_read,
-	.read_flag_mask = 0x00,
 	.reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
 	.val_format_endian_default = REGMAP_ENDIAN_NATIVE,
 };
Signature · 1 line
2.29.2
author maintainer
#7 lore raw

PATCH 7/16 can: tcan4x5x: remove regmap async support

In reply to Marc Kleine-Budde

The driver doesn't use regmap async support, so remove the stubs.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 15 ---------------
 1 file changed, 15 deletions(-)
drivers/net/can/m_can/tcan4x5x-regmap.c +0 -15
index 20bf9a57c9b1..8905fc36b00a 100644
@@ -44,19 +44,6 @@ static int tcan4x5x_regmap_write(void *context, const void *data, size_t count)
 	return regmap_spi_gather_write(context, reg, 4, val, count - 4);
 }
 
-static int regmap_spi_async_write(void *context,
-				  const void *reg, size_t reg_len,
-				  const void *val, size_t val_len,
-				  struct regmap_async *a)
-{
-	return -ENOTSUPP;
-}
-
-static struct regmap_async *regmap_spi_async_alloc(void)
-{
-	return NULL;
-}
-
 static int tcan4x5x_regmap_read(void *context,
 				const void *reg, size_t reg_size,
 				void *val, size_t val_size)
@@ -79,8 +66,6 @@ static const struct regmap_config tcan4x5x_regmap = {
 static const struct regmap_bus tcan4x5x_bus = {
 	.write = tcan4x5x_regmap_write,
 	.gather_write = regmap_spi_gather_write,
-	.async_write = regmap_spi_async_write,
-	.async_alloc = regmap_spi_async_alloc,
 	.read = tcan4x5x_regmap_read,
 	.reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
 	.val_format_endian_default = REGMAP_ENDIAN_NATIVE,
Signature · 1 line
2.29.2
author maintainer
#8 lore raw

PATCH 8/16 can: tcan4x5x: rename regmap_spi_gather_write() -> tcan4x5x_regmap_gather_write()

In reply to Marc Kleine-Budde

This patch renames the regmap_spi_gather_write() function to tcan4x5x_regmap_gather_write(). Now it has a "tcan4x5x_" prefix as all other functions in this driver.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/net/can/m_can/tcan4x5x-regmap.c +5 -5
index 8905fc36b00a..8f718f4395c3 100644
@@ -14,9 +14,9 @@
 
 #define TCAN4X5X_MAX_REGISTER 0x8fff
 
-static int regmap_spi_gather_write(void *context, const void *reg,
-				   size_t reg_len, const void *val,
-				   size_t val_len)
+static int tcan4x5x_regmap_gather_write(void *context, const void *reg,
+					size_t reg_len, const void *val,
+					size_t val_len)
 {
 	struct device *dev = context;
 	struct spi_device *spi = to_spi_device(dev);
@@ -41,7 +41,7 @@ static int tcan4x5x_regmap_write(void *context, const void *data, size_t count)
 	u16 *reg = (u16 *)(data);
 	const u32 *val = data + 4;
 
-	return regmap_spi_gather_write(context, reg, 4, val, count - 4);
+	return tcan4x5x_regmap_gather_write(context, reg, 4, val, count - 4);
 }
 
 static int tcan4x5x_regmap_read(void *context,
@@ -65,7 +65,7 @@ static const struct regmap_config tcan4x5x_regmap = {
 
 static const struct regmap_bus tcan4x5x_bus = {
 	.write = tcan4x5x_regmap_write,
-	.gather_write = regmap_spi_gather_write,
+	.gather_write = tcan4x5x_regmap_gather_write,
 	.read = tcan4x5x_regmap_read,
 	.reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
 	.val_format_endian_default = REGMAP_ENDIAN_NATIVE,
Signature · 1 line
2.29.2
author maintainer
#9 lore raw

PATCH 9/16 can: tcan4x5x: tcan4x5x_regmap_write(): remove not needed casts and replace 4 by sizeof

In reply to Marc Kleine-Budde

This patch simplifies the tcan4x5x_regmap_write(0 function by removing not needed casts and replaces hardcoded "4" by appropriate sizeof()s.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/net/can/m_can/tcan4x5x-regmap.c +3 -4
index 8f718f4395c3..6345bcb7704f 100644
@@ -38,10 +38,9 @@ static int tcan4x5x_regmap_gather_write(void *context, const void *reg,
 
 static int tcan4x5x_regmap_write(void *context, const void *data, size_t count)
 {
-	u16 *reg = (u16 *)(data);
-	const u32 *val = data + 4;
-
-	return tcan4x5x_regmap_gather_write(context, reg, 4, val, count - 4);
+	return tcan4x5x_regmap_gather_write(context, data, sizeof(u32),
+					    data + sizeof(u32),
+					    count - sizeof(u32));
 }
 
 static int tcan4x5x_regmap_read(void *context,
Signature · 1 line
2.29.2
author maintainer
#10 lore raw

PATCH 10/16 can: tcan4x5x: tcan4x5x_regmap_init(): use spi as context pointer

In reply to Marc Kleine-Budde

This patch replaces the context pointer of the regmap callback functions by a pointer to the spi_device. This saves one level of indirection in the callbacks.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/net/can/m_can/tcan4x5x-regmap.c +3 -5
index 6345bcb7704f..4d43c145fdec 100644
@@ -18,8 +18,7 @@ static int tcan4x5x_regmap_gather_write(void *context, const void *reg,
 					size_t reg_len, const void *val,
 					size_t val_len)
 {
-	struct device *dev = context;
-	struct spi_device *spi = to_spi_device(dev);
+	struct spi_device *spi = context;
 	struct spi_message m;
 	u32 addr;
 	struct spi_transfer t[2] = {
@@ -47,8 +46,7 @@ static int tcan4x5x_regmap_read(void *context,
 				const void *reg, size_t reg_size,
 				void *val, size_t val_size)
 {
-	struct device *dev = context;
-	struct spi_device *spi = to_spi_device(dev);
+	struct spi_device *spi = context;
 
 	u32 addr = TCAN4X5X_READ_CMD | (*((u16 *)reg) << 8) | val_size >> 2;
 
@@ -73,6 +71,6 @@ static const struct regmap_bus tcan4x5x_bus = {
 int tcan4x5x_regmap_init(struct tcan4x5x_priv *priv)
 {
 	priv->regmap = devm_regmap_init(&priv->spi->dev, &tcan4x5x_bus,
-					&priv->spi->dev, &tcan4x5x_regmap);
+					priv->spi, &tcan4x5x_regmap);
 	return PTR_ERR_OR_ZERO(priv->regmap);
 }
Signature · 1 line
2.29.2
author maintainer
#11 lore raw

PATCH 11/16 can: tcan4x5x: fix max register value

In reply to Marc Kleine-Budde

This patch fixes the max register value for the regmap.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/can/m_can/tcan4x5x-regmap.c +1 -1
index 4d43c145fdec..73be4b661305 100644
@@ -12,7 +12,7 @@
 #define TCAN4X5X_WRITE_CMD (0x61 << 24)
 #define TCAN4X5X_READ_CMD (0x41 << 24)
 
-#define TCAN4X5X_MAX_REGISTER 0x8fff
+#define TCAN4X5X_MAX_REGISTER 0x8ffc
 
 static int tcan4x5x_regmap_gather_write(void *context, const void *reg,
 					size_t reg_len, const void *val,
Signature · 1 line
2.29.2
author maintainer
#12 lore raw

PATCH 12/16 can: tcan4x5x: tcan4x5x_regmap: set reg_stride to 4

In reply to Marc Kleine-Budde

This patch sets the regmap stide to 4, as the chip only supports access on 32 bit alligned access.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/can/m_can/tcan4x5x-regmap.c +1 -0
index 73be4b661305..c6963437064c 100644
@@ -55,6 +55,7 @@ static int tcan4x5x_regmap_read(void *context,
 
 static const struct regmap_config tcan4x5x_regmap = {
 	.reg_bits = 32,
+	.reg_stride = 4,
 	.val_bits = 32,
 	.cache_type = REGCACHE_NONE,
 	.max_register = TCAN4X5X_MAX_REGISTER,
Signature · 1 line
2.29.2
author maintainer
#13 lore raw

PATCH 13/16 can: tcan4x5x: add max_raw_{read,write} of 256

In reply to Marc Kleine-Budde

The tcan4x5x chip support bulk read/write, but as the length field is only 8 bits wide, the maximum is 256. A length of 0 is treated as 256.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/can/m_can/tcan4x5x-regmap.c +2 -0
index c6963437064c..f113881fb012 100644
@@ -67,6 +67,8 @@ static const struct regmap_bus tcan4x5x_bus = {
 	.read = tcan4x5x_regmap_read,
 	.reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
 	.val_format_endian_default = REGMAP_ENDIAN_NATIVE,
+	.max_raw_read = 256,
+	.max_raw_write = 256,
 };
 
 int tcan4x5x_regmap_init(struct tcan4x5x_priv *priv)
Signature · 1 line
2.29.2
author maintainer
#14 lore raw

PATCH 14/16 can: tcan4x5x: add {wr,rd}_table

In reply to Marc Kleine-Budde

The memory space of the chip is not fully populated, so add a regmap range table to document this.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/net/can/m_can/tcan4x5x-regmap.c +14 -0
index f113881fb012..5ea162578619 100644
@@ -53,10 +53,24 @@ static int tcan4x5x_regmap_read(void *context,
 	return spi_write_then_read(spi, &addr, reg_size, (u32 *)val, val_size);
 }
 
+static const struct regmap_range tcan4x5x_reg_table_yes_range[] = {
+	regmap_reg_range(0x0000, 0x002c),	/* Device ID and SPI Registers */
+	regmap_reg_range(0x0800, 0x083c),	/* Device configuration registers and Interrupt Flags*/
+	regmap_reg_range(0x1000, 0x10fc),	/* M_CAN */
+	regmap_reg_range(0x8000, 0x87fc),	/* MRAM */
+};
+
+static const struct regmap_access_table tcan4x5x_reg_table = {
+	.yes_ranges = tcan4x5x_reg_table_yes_range,
+	.n_yes_ranges = ARRAY_SIZE(tcan4x5x_reg_table_yes_range),
+};
+
 static const struct regmap_config tcan4x5x_regmap = {
 	.reg_bits = 32,
 	.reg_stride = 4,
 	.val_bits = 32,
+	.wr_table = &tcan4x5x_reg_table,
+	.rd_table = &tcan4x5x_reg_table,
 	.cache_type = REGCACHE_NONE,
 	.max_register = TCAN4X5X_MAX_REGISTER,
 };
Signature · 1 line
2.29.2
author maintainer
#15 lore raw

PATCH 15/16 can: tcan4x5x: rework SPI access

In reply to Marc Kleine-Budde

This patch reworks the SPI access and fixes several probems:
- tcan4x5x_regmap_gather_write(), tcan4x5x_regmap_read():
Do not place variable "addr" on stack and use it as buffer for SPI transfer. Buffers for SPI transfers must be allocated from DMA save memory.
- tcan4x5x_regmap_gather_write(), tcan4x5x_regmap_read():
Halfe number of SPI transfers by using a single buffer + memcpy(). This improves the performance, especially on SPI controllers, which use interrupt based transfers.
- Use "8" bits per word, not "32". This makes it possible to use this driver on SoCs like the Raspberry Pi, which SPI host controller drivers only support 8 bits per word.

Note: this breaks half duplex only controllers. Support for them will be re-added in the next patch.

---
 drivers/net/can/m_can/tcan4x5x-core.c   |  2 +-
 drivers/net/can/m_can/tcan4x5x-regmap.c | 87 +++++++++++++++++--------
 drivers/net/can/m_can/tcan4x5x.h        | 23 +++++++
 3 files changed, 84 insertions(+), 28 deletions(-)
drivers/net/can/m_can/tcan4x5x-core.c +1 -1
index 739b8f89a335..d37843a74663 100644
@@ -380,7 +380,7 @@ static int tcan4x5x_can_probe(struct spi_device *spi)
 	spi_set_drvdata(spi, priv);
 
 	/* Configure the SPI bus */
-	spi->bits_per_word = 32;
+	spi->bits_per_word = 8;
 	ret = spi_setup(spi);
 	if (ret)
 		goto out_m_can_class_free_dev;
drivers/net/can/m_can/tcan4x5x-regmap.c +60 -27

129 lines not on this page · raw message

drivers/net/can/m_can/tcan4x5x.h +23 -0
index e5bdd91b8005..7bf264f8e81f 100644
@@ -17,6 +17,19 @@
 
 #include "m_can.h"
 
+#define TCAN4X5X_SANITIZE_SPI 1
+
+struct __packed tcan4x5x_buf_cmd {
+	u8 cmd;
+	__be16 addr;
+	u8 len;
+};
+
+struct __packed tcan4x5x_map_buf {
+	struct tcan4x5x_buf_cmd cmd;
+	u8 data[256 * sizeof(u32)];
+} ____cacheline_aligned;
+
 struct tcan4x5x_priv {
 	struct m_can_classdev cdev;
 
@@ -27,8 +40,18 @@ struct tcan4x5x_priv {
 	struct gpio_desc *device_wake_gpio;
 	struct gpio_desc *device_state_gpio;
 	struct regulator *power;
+
+	struct tcan4x5x_map_buf map_buf_rx;
+	struct tcan4x5x_map_buf map_buf_tx;
 };
 
+static inline void
+tcan4x5x_spi_cmd_set_len(struct tcan4x5x_buf_cmd *cmd, u8 len)
+{
+	/* number of u32 */
+	cmd->len = len >> 2;
+}
+
 int tcan4x5x_regmap_init(struct tcan4x5x_priv *priv);
 
 #endif
Signature · 1 line
2.29.2
author maintainer
#16 lore raw

PATCH 16/16 can: tcan4x5x: add support for half-duplex controllers

In reply to Marc Kleine-Budde

This patch adds back support for half-duplex controllers, which was removed in the last patch.

---
 drivers/net/can/m_can/tcan4x5x-regmap.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/net/can/m_can/tcan4x5x-regmap.c +15 -6
index 660e9d87dffb..ca80dbaf7a3f 100644
@@ -51,7 +51,7 @@ static int tcan4x5x_regmap_read(void *context,
 	struct tcan4x5x_priv *priv = spi_get_drvdata(spi);
 	struct tcan4x5x_map_buf *buf_rx = &priv->map_buf_rx;
 	struct tcan4x5x_map_buf *buf_tx = &priv->map_buf_tx;
-	struct spi_transfer xfer[] = {
+	struct spi_transfer xfer[2] = {
 		{
 			.tx_buf = buf_tx,
 		}
@@ -66,17 +66,26 @@ static int tcan4x5x_regmap_read(void *context,
 	       sizeof(buf_tx->cmd.addr));
 	tcan4x5x_spi_cmd_set_len(&buf_tx->cmd, val_len);
 
-	xfer[0].rx_buf = buf_rx;
-	xfer[0].len = sizeof(buf_tx->cmd) + val_len;
+	if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX) {
+		xfer[0].len = sizeof(buf_tx->cmd);
+
+		xfer[1].rx_buf = val_buf;
+		xfer[1].len = val_len;
+		spi_message_add_tail(&xfer[1], &msg);
+	} else {
+		xfer[0].rx_buf = buf_rx;
+		xfer[0].len = sizeof(buf_tx->cmd) + val_len;
 
-	if (TCAN4X5X_SANITIZE_SPI)
-		memset(buf_tx->data, 0x0, val_len);
+		if (TCAN4X5X_SANITIZE_SPI)
+			memset(buf_tx->data, 0x0, val_len);
+	}
 
 	err = spi_sync(spi, &msg);
 	if (err)
 		return err;
 
-	memcpy(val_buf, buf_rx->data, val_len);
+	if (!(spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX))
+		memcpy(val_buf, buf_rx->data, val_len);
 
 	return 0;
 }
Signature · 1 line
2.29.2
Reviewed-by
#17 lore raw
In reply to Marc Kleine-Budde

Marc

Quoted 15 lines · Marc Kleine-Budde

Hello,

this series first cleans up the the SPI regmap and then fixes several problem:
- tcan4x5x_regmap_gather_write(), tcan4x5x_regmap_read():
Do not place variable "addr" on stack and use it as buffer for SPI transfer. Buffers for SPI transfers must be allocated from DMA save memory.
- tcan4x5x_regmap_gather_write(), tcan4x5x_regmap_read():
Halfe number of SPI transfers by using a single buffer + memcpy(). This improves the performance, especially on SPI controllers, which use interrupt based transfers.
- Use "8" bits per word, not "32". This makes it possible to use this driver on SoCs like the Raspberry Pi, which SPI host controller drivers only support 8 bits per word.

Quoted 5 lines

With this series, the driver is able to detect a tcan4x5x on a Raspberry Pi.

regards,
Marc

Thanks for the clean up.

Dan

Quoted 1 line
Tested-by
#18 lore raw
In reply to Marc Kleine-Budde
Quoted 15 lines · Marc Kleine-Budde

Hello,

this series first cleans up the the SPI regmap and then fixes several problem:
- tcan4x5x_regmap_gather_write(), tcan4x5x_regmap_read():
Do not place variable "addr" on stack and use it as buffer for SPI transfer. Buffers for SPI transfers must be allocated from DMA save memory.
- tcan4x5x_regmap_gather_write(), tcan4x5x_regmap_read():
Halfe number of SPI transfers by using a single buffer + memcpy(). This improves the performance, especially on SPI controllers, which use interrupt based transfers.
- Use "8" bits per word, not "32". This makes it possible to use this driver on SoCs like the Raspberry Pi, which SPI host controller drivers only support 8 bits per word.

Quoted 5 lines

With this series, the driver is able to detect a tcan4x5x on a Raspberry Pi.

regards,
Marc

For this series...

/Sean