2018-11-21 07:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2017-06-01 04:19:19 +08:00
|
|
|
/*
|
2018-11-21 07:55:09 +08:00
|
|
|
* Microchip KSZ9477 series register access through SPI
|
2017-06-01 04:19:19 +08:00
|
|
|
*
|
2019-03-01 11:57:24 +08:00
|
|
|
* Copyright (C) 2017-2019 Microchip Technology Inc.
|
2017-06-01 04:19:19 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
2019-06-26 07:43:45 +08:00
|
|
|
#include <linux/regmap.h>
|
2017-06-01 04:19:19 +08:00
|
|
|
#include <linux/spi/spi.h>
|
|
|
|
|
|
|
|
#include "ksz_priv.h"
|
|
|
|
|
|
|
|
#define SPI_ADDR_SHIFT 24
|
2019-06-26 07:43:45 +08:00
|
|
|
#define SPI_ADDR_ALIGN 3
|
2017-06-01 04:19:19 +08:00
|
|
|
#define SPI_TURNAROUND_SHIFT 5
|
|
|
|
|
2019-06-26 07:43:45 +08:00
|
|
|
/* SPI frame opcodes */
|
|
|
|
#define KS_SPIOP_RD 3
|
|
|
|
#define KS_SPIOP_WR 2
|
2017-06-01 04:19:19 +08:00
|
|
|
|
2019-06-26 07:43:45 +08:00
|
|
|
#define KS_SPIOP_FLAG_MASK(opcode) \
|
|
|
|
swab32((opcode) << (SPI_ADDR_SHIFT + SPI_TURNAROUND_SHIFT))
|
|
|
|
|
|
|
|
#define KSZ_REGMAP_COMMON(width) \
|
|
|
|
{ \
|
|
|
|
.val_bits = (width), \
|
|
|
|
.reg_stride = (width) / 8, \
|
|
|
|
.reg_bits = SPI_ADDR_SHIFT + SPI_ADDR_ALIGN, \
|
|
|
|
.pad_bits = SPI_TURNAROUND_SHIFT, \
|
|
|
|
.max_register = BIT(SPI_ADDR_SHIFT) - 1, \
|
|
|
|
.cache_type = REGCACHE_NONE, \
|
|
|
|
.read_flag_mask = KS_SPIOP_FLAG_MASK(KS_SPIOP_RD), \
|
|
|
|
.write_flag_mask = KS_SPIOP_FLAG_MASK(KS_SPIOP_WR), \
|
|
|
|
.reg_format_endian = REGMAP_ENDIAN_BIG, \
|
|
|
|
.val_format_endian = REGMAP_ENDIAN_BIG \
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct regmap_config ksz9477_regmap_config[] = {
|
|
|
|
KSZ_REGMAP_COMMON(8),
|
|
|
|
KSZ_REGMAP_COMMON(16),
|
|
|
|
KSZ_REGMAP_COMMON(32),
|
|
|
|
};
|
2017-06-01 04:19:19 +08:00
|
|
|
|
2019-06-26 07:43:41 +08:00
|
|
|
static int ksz_spi_read8(struct ksz_device *dev, u32 reg, u8 *val)
|
|
|
|
{
|
2019-06-26 07:43:45 +08:00
|
|
|
unsigned int value;
|
|
|
|
int ret = regmap_read(dev->regmap, reg, &value);
|
|
|
|
|
|
|
|
*val = value;
|
|
|
|
return ret;
|
2019-06-26 07:43:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ksz_spi_read16(struct ksz_device *dev, u32 reg, u16 *val)
|
|
|
|
{
|
2019-06-26 07:43:45 +08:00
|
|
|
int ret = regmap_bulk_read(dev->regmap, reg, val, 2);
|
2019-06-26 07:43:41 +08:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
*val = be16_to_cpu(*val);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ksz_spi_read32(struct ksz_device *dev, u32 reg, u32 *val)
|
|
|
|
{
|
2019-06-26 07:43:45 +08:00
|
|
|
int ret = regmap_bulk_read(dev->regmap, reg, val, 4);
|
2019-06-26 07:43:41 +08:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
*val = be32_to_cpu(*val);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ksz_spi_write8(struct ksz_device *dev, u32 reg, u8 value)
|
|
|
|
{
|
2019-06-26 07:43:45 +08:00
|
|
|
return regmap_write(dev->regmap, reg, value);
|
2019-06-26 07:43:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ksz_spi_write16(struct ksz_device *dev, u32 reg, u16 value)
|
|
|
|
{
|
|
|
|
value = cpu_to_be16(value);
|
2019-06-26 07:43:45 +08:00
|
|
|
return regmap_bulk_write(dev->regmap, reg, &value, 2);
|
2019-06-26 07:43:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ksz_spi_write32(struct ksz_device *dev, u32 reg, u32 value)
|
|
|
|
{
|
|
|
|
value = cpu_to_be32(value);
|
2019-06-26 07:43:45 +08:00
|
|
|
return regmap_bulk_write(dev->regmap, reg, &value, 4);
|
2019-06-26 07:43:41 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 07:55:09 +08:00
|
|
|
static const struct ksz_io_ops ksz9477_spi_ops = {
|
2017-06-01 04:19:19 +08:00
|
|
|
.read8 = ksz_spi_read8,
|
|
|
|
.read16 = ksz_spi_read16,
|
|
|
|
.read32 = ksz_spi_read32,
|
|
|
|
.write8 = ksz_spi_write8,
|
|
|
|
.write16 = ksz_spi_write16,
|
|
|
|
.write32 = ksz_spi_write32,
|
|
|
|
};
|
|
|
|
|
2018-11-21 07:55:09 +08:00
|
|
|
static int ksz9477_spi_probe(struct spi_device *spi)
|
2017-06-01 04:19:19 +08:00
|
|
|
{
|
|
|
|
struct ksz_device *dev;
|
2019-06-26 07:43:45 +08:00
|
|
|
int i, ret;
|
2017-06-01 04:19:19 +08:00
|
|
|
|
2018-11-21 07:55:09 +08:00
|
|
|
dev = ksz_switch_alloc(&spi->dev, &ksz9477_spi_ops, spi);
|
2017-06-01 04:19:19 +08:00
|
|
|
if (!dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-06-26 07:43:45 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(ksz9477_regmap_config); i++) {
|
|
|
|
dev->regmap[i] = devm_regmap_init_spi(spi,
|
|
|
|
&ksz9477_regmap_config[i]);
|
|
|
|
if (IS_ERR(dev->regmap[i])) {
|
|
|
|
ret = PTR_ERR(dev->regmap[i]);
|
|
|
|
dev_err(&spi->dev,
|
|
|
|
"Failed to initialize regmap%i: %d\n",
|
|
|
|
ksz9477_regmap_config[i].val_bits, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 04:19:19 +08:00
|
|
|
if (spi->dev.platform_data)
|
|
|
|
dev->pdata = spi->dev.platform_data;
|
|
|
|
|
2018-11-21 07:55:09 +08:00
|
|
|
ret = ksz9477_switch_register(dev);
|
|
|
|
|
|
|
|
/* Main DSA driver may not be started yet. */
|
2017-06-01 04:19:19 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
spi_set_drvdata(spi, dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-21 07:55:09 +08:00
|
|
|
static int ksz9477_spi_remove(struct spi_device *spi)
|
2017-06-01 04:19:19 +08:00
|
|
|
{
|
|
|
|
struct ksz_device *dev = spi_get_drvdata(spi);
|
|
|
|
|
|
|
|
if (dev)
|
|
|
|
ksz_switch_remove(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-21 07:55:09 +08:00
|
|
|
static void ksz9477_spi_shutdown(struct spi_device *spi)
|
|
|
|
{
|
|
|
|
struct ksz_device *dev = spi_get_drvdata(spi);
|
|
|
|
|
|
|
|
if (dev && dev->dev_ops->shutdown)
|
|
|
|
dev->dev_ops->shutdown(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id ksz9477_dt_ids[] = {
|
2017-06-01 04:19:19 +08:00
|
|
|
{ .compatible = "microchip,ksz9477" },
|
2018-08-15 23:51:23 +08:00
|
|
|
{ .compatible = "microchip,ksz9897" },
|
2019-03-01 11:57:24 +08:00
|
|
|
{ .compatible = "microchip,ksz9893" },
|
|
|
|
{ .compatible = "microchip,ksz9563" },
|
2017-06-01 04:19:19 +08:00
|
|
|
{},
|
|
|
|
};
|
2018-11-21 07:55:09 +08:00
|
|
|
MODULE_DEVICE_TABLE(of, ksz9477_dt_ids);
|
2017-06-01 04:19:19 +08:00
|
|
|
|
2018-11-21 07:55:09 +08:00
|
|
|
static struct spi_driver ksz9477_spi_driver = {
|
2017-06-01 04:19:19 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "ksz9477-switch",
|
|
|
|
.owner = THIS_MODULE,
|
2018-11-21 07:55:09 +08:00
|
|
|
.of_match_table = of_match_ptr(ksz9477_dt_ids),
|
2017-06-01 04:19:19 +08:00
|
|
|
},
|
2018-11-21 07:55:09 +08:00
|
|
|
.probe = ksz9477_spi_probe,
|
|
|
|
.remove = ksz9477_spi_remove,
|
|
|
|
.shutdown = ksz9477_spi_shutdown,
|
2017-06-01 04:19:19 +08:00
|
|
|
};
|
|
|
|
|
2018-11-21 07:55:09 +08:00
|
|
|
module_spi_driver(ksz9477_spi_driver);
|
2017-06-01 04:19:19 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
|
2018-11-21 07:55:09 +08:00
|
|
|
MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch SPI access Driver");
|
2017-06-01 04:19:19 +08:00
|
|
|
MODULE_LICENSE("GPL");
|