From: Marek Vasut <marex@denx.de>
To: netdev@vger.kernel.org
Cc: f.fainelli@gmail.com, andrew@lunn.ch, Marek Vasut <marex@denx.de>,
Tristram Ha <Tristram.Ha@microchip.com>,
Woojung Huh <Woojung.Huh@microchip.com>
Subject: [RFT][PATCH 6/7] net: dsa: microchip: Initial SPI regmap support
Date: Thu, 20 Dec 2018 02:06:46 +0100 [thread overview]
Message-ID: <20181220010647.4059-7-marex@denx.de> (raw)
In-Reply-To: <20181220010647.4059-1-marex@denx.de>
Add basic SPI regmap support into the driver.
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Andrew Lunn <andrew@lunn.ch>
Cc: Florian Fainelli <f.fainelli@gmail.com>
Cc: Tristram Ha <Tristram.Ha@microchip.com>
Cc: Woojung Huh <Woojung.Huh@microchip.com>
---
drivers/net/dsa/microchip/Kconfig | 1 +
drivers/net/dsa/microchip/ksz9477_spi.c | 85 ++++++++-----------------
drivers/net/dsa/microchip/ksz_priv.h | 1 +
3 files changed, 30 insertions(+), 57 deletions(-)
diff --git a/drivers/net/dsa/microchip/Kconfig b/drivers/net/dsa/microchip/Kconfig
index bea29fde9f3d..3a00cb1b372a 100644
--- a/drivers/net/dsa/microchip/Kconfig
+++ b/drivers/net/dsa/microchip/Kconfig
@@ -1,4 +1,5 @@
config NET_DSA_MICROCHIP_KSZ_COMMON
+ select REGMAP_SPI
tristate
menuconfig NET_DSA_MICROCHIP_KSZ9477
diff --git a/drivers/net/dsa/microchip/ksz9477_spi.c b/drivers/net/dsa/microchip/ksz9477_spi.c
index 4fc36dc2195a..3cf3c8143e98 100644
--- a/drivers/net/dsa/microchip/ksz9477_spi.c
+++ b/drivers/net/dsa/microchip/ksz9477_spi.c
@@ -10,6 +10,7 @@
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
+#include <linux/regmap.h>
#include <linux/spi/spi.h>
#include "ksz_priv.h"
@@ -22,66 +23,29 @@
#define SPI_ADDR_MASK (BIT(SPI_ADDR_SHIFT) - 1)
#define SPI_TURNAROUND_SHIFT 5
-/* Enough to read all switch port registers. */
-#define SPI_TX_BUF_LEN 0x100
-
-static u32 ksz9477_spi_cmd(u32 reg, bool read)
-{
- u32 txbuf;
-
- txbuf = reg & SPI_ADDR_MASK;
- txbuf |= (read ? KS_SPIOP_RD : KS_SPIOP_WR) << SPI_ADDR_SHIFT;
- txbuf <<= SPI_TURNAROUND_SHIFT;
- txbuf = cpu_to_be32(txbuf);
-
- return txbuf;
-}
-
-static int ksz9477_spi_read_reg(struct spi_device *spi, u32 reg, u8 *val,
- unsigned int len)
-{
- u32 txbuf = ksz9477_spi_cmd(reg, true);
-
- return spi_write_then_read(spi, &txbuf, 4, val, len);
-}
-
-static int ksz9477_spi_write_reg(struct spi_device *spi, u32 reg, u8 *val,
- unsigned int len)
-{
- u32 *txbuf = (u32 *)val;
-
- *txbuf = ksz9477_spi_cmd(reg, false);
-
- return spi_write(spi, txbuf, 4 + len);
-}
-
-static int ksz_spi_read(struct ksz_device *dev, u32 reg, u8 *data,
- unsigned int len)
-{
- struct spi_device *spi = dev->priv;
-
- return ksz9477_spi_read_reg(spi, reg, data, len);
-}
-
-static int ksz_spi_write(struct ksz_device *dev, u32 reg, void *data,
- unsigned int len)
-{
- struct spi_device *spi = dev->priv;
- u8 txbuf[8];
-
- memcpy(txbuf + 4, data, len);
-
- return ksz9477_spi_write_reg(spi, reg, txbuf, len);
-}
+static const struct regmap_config ksz9477_regmap_config = {
+ .reg_bits = 32,
+ .val_bits = 8,
+ .max_register = 0x100,
+ .cache_type = REGCACHE_RBTREE,
+ .read_flag_mask = KS_SPIOP_RD << SPI_ADDR_SHIFT,
+ .write_flag_mask = KS_SPIOP_WR << SPI_ADDR_SHIFT,
+ .reg_format_endian = REGMAP_ENDIAN_BIG,
+ .val_format_endian = REGMAP_ENDIAN_BIG,
+};
static int ksz_spi_read8(struct ksz_device *dev, u32 reg, u8 *val)
{
- return ksz_spi_read(dev, reg, val, 1);
+ unsigned int value;
+ int ret = regmap_read(dev->regmap, reg, &value);
+
+ *val = value;
+ return ret;
}
static int ksz_spi_read16(struct ksz_device *dev, u32 reg, u16 *val)
{
- int ret = ksz_spi_read(dev, reg, (u8 *)val, 2);
+ int ret = regmap_bulk_read(dev->regmap, reg, val, 2);
if (!ret)
*val = be16_to_cpu(*val);
@@ -91,7 +55,7 @@ static int ksz_spi_read16(struct ksz_device *dev, u32 reg, u16 *val)
static int ksz_spi_read32(struct ksz_device *dev, u32 reg, u32 *val)
{
- int ret = ksz_spi_read(dev, reg, (u8 *)val, 4);
+ int ret = regmap_bulk_read(dev->regmap, reg, val, 4);
if (!ret)
*val = be32_to_cpu(*val);
@@ -101,19 +65,19 @@ static int ksz_spi_read32(struct ksz_device *dev, u32 reg, u32 *val)
static int ksz_spi_write8(struct ksz_device *dev, u32 reg, u8 value)
{
- return ksz_spi_write(dev, reg, &value, 1);
+ return regmap_write(dev->regmap, reg, value);
}
static int ksz_spi_write16(struct ksz_device *dev, u32 reg, u16 value)
{
value = cpu_to_be16(value);
- return ksz_spi_write(dev, reg, &value, 2);
+ return regmap_bulk_write(dev->regmap, reg, &value, 2);
}
static int ksz_spi_write32(struct ksz_device *dev, u32 reg, u32 value)
{
value = cpu_to_be32(value);
- return ksz_spi_write(dev, reg, &value, 4);
+ return regmap_bulk_write(dev->regmap, reg, &value, 4);
}
static const struct ksz_io_ops ksz9477_spi_ops = {
@@ -134,6 +98,13 @@ static int ksz9477_spi_probe(struct spi_device *spi)
if (!dev)
return -ENOMEM;
+ dev->regmap = devm_regmap_init_spi(spi, &ksz9477_regmap_config);
+ if (IS_ERR(dev->regmap)) {
+ ret = PTR_ERR(dev->regmap);
+ dev_err(&spi->dev, "Failed to initialize regmap: %d\n", ret);
+ return ret;
+ }
+
if (spi->dev.platform_data)
dev->pdata = spi->dev.platform_data;
diff --git a/drivers/net/dsa/microchip/ksz_priv.h b/drivers/net/dsa/microchip/ksz_priv.h
index 3ab14ee0e36b..f9429e1fc7fe 100644
--- a/drivers/net/dsa/microchip/ksz_priv.h
+++ b/drivers/net/dsa/microchip/ksz_priv.h
@@ -56,6 +56,7 @@ struct ksz_device {
const struct ksz_dev_ops *dev_ops;
struct device *dev;
+ struct regmap *regmap;
void *priv;
--
2.19.2
next prev parent reply other threads:[~2018-12-20 1:07 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-12-20 1:06 [RFT][PATCH 0/7] net: dsa: microchip: Convert to regmap Marek Vasut
2018-12-20 1:06 ` [RFT][PATCH 1/7] net: dsa: microchip: Remove ksz_{read,write}24() Marek Vasut
2018-12-20 1:06 ` [RFT][PATCH 2/7] net: dsa: microchip: Remove ksz_{get,set}() Marek Vasut
2018-12-20 1:06 ` [RFT][PATCH 3/7] net: dsa: microchip: Inline ksz_spi.h Marek Vasut
2018-12-20 1:06 ` [RFT][PATCH 4/7] net: dsa: microchip: Remove dev->txbuf Marek Vasut
2018-12-20 1:20 ` Florian Fainelli
2018-12-20 9:41 ` Andrew Lunn
2018-12-20 14:35 ` Marek Vasut
2018-12-21 1:02 ` Tristram.Ha
2018-12-21 2:04 ` Marek Vasut
2018-12-21 9:30 ` Andrew Lunn
2018-12-20 18:57 ` Marek Vasut
2018-12-20 1:06 ` [RFT][PATCH 5/7] net: dsa: microchip: Factor out register access opcode generation Marek Vasut
2018-12-20 1:06 ` Marek Vasut [this message]
2018-12-21 1:30 ` [RFT][PATCH 6/7] net: dsa: microchip: Initial SPI regmap support Tristram.Ha
2018-12-21 2:04 ` Marek Vasut
2018-12-20 1:06 ` [RFT][PATCH 7/7] net: dsa: microchip: Dispose of ksz_io_ops Marek Vasut
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181220010647.4059-7-marex@denx.de \
--to=marex@denx.de \
--cc=Tristram.Ha@microchip.com \
--cc=Woojung.Huh@microchip.com \
--cc=andrew@lunn.ch \
--cc=f.fainelli@gmail.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.