1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip KSZ9477 series register access through SPI
5 * Copyright (C) 2017-2019 Microchip Technology Inc.
8 #include <asm/unaligned.h>
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/spi/spi.h>
18 #define SPI_ADDR_SHIFT 24
19 #define SPI_ADDR_ALIGN 3
20 #define SPI_TURNAROUND_SHIFT 5
22 /* SPI frame opcodes */
26 #define KS_SPIOP_FLAG_MASK(opcode) \
27 swab32((opcode) << (SPI_ADDR_SHIFT + SPI_TURNAROUND_SHIFT))
29 #define KSZ_REGMAP_COMMON(width) \
31 .val_bits = (width), \
32 .reg_stride = (width) / 8, \
33 .reg_bits = SPI_ADDR_SHIFT + SPI_ADDR_ALIGN, \
34 .pad_bits = SPI_TURNAROUND_SHIFT, \
35 .max_register = BIT(SPI_ADDR_SHIFT) - 1, \
36 .cache_type = REGCACHE_NONE, \
37 .read_flag_mask = KS_SPIOP_FLAG_MASK(KS_SPIOP_RD), \
38 .write_flag_mask = KS_SPIOP_FLAG_MASK(KS_SPIOP_WR), \
39 .reg_format_endian = REGMAP_ENDIAN_BIG, \
40 .val_format_endian = REGMAP_ENDIAN_BIG \
43 static const struct regmap_config ksz9477_regmap_config[] = {
45 KSZ_REGMAP_COMMON(16),
46 KSZ_REGMAP_COMMON(32),
49 static int ksz_spi_read8(struct ksz_device *dev, u32 reg, u8 *val)
52 int ret = regmap_read(dev->regmap, reg, &value);
58 static int ksz_spi_read16(struct ksz_device *dev, u32 reg, u16 *val)
60 int ret = regmap_bulk_read(dev->regmap, reg, val, 2);
63 *val = be16_to_cpu(*val);
68 static int ksz_spi_read32(struct ksz_device *dev, u32 reg, u32 *val)
70 int ret = regmap_bulk_read(dev->regmap, reg, val, 4);
73 *val = be32_to_cpu(*val);
78 static int ksz_spi_write8(struct ksz_device *dev, u32 reg, u8 value)
80 return regmap_write(dev->regmap, reg, value);
83 static int ksz_spi_write16(struct ksz_device *dev, u32 reg, u16 value)
85 value = cpu_to_be16(value);
86 return regmap_bulk_write(dev->regmap, reg, &value, 2);
89 static int ksz_spi_write32(struct ksz_device *dev, u32 reg, u32 value)
91 value = cpu_to_be32(value);
92 return regmap_bulk_write(dev->regmap, reg, &value, 4);
95 static const struct ksz_io_ops ksz9477_spi_ops = {
96 .read8 = ksz_spi_read8,
97 .read16 = ksz_spi_read16,
98 .read32 = ksz_spi_read32,
99 .write8 = ksz_spi_write8,
100 .write16 = ksz_spi_write16,
101 .write32 = ksz_spi_write32,
104 static int ksz9477_spi_probe(struct spi_device *spi)
106 struct ksz_device *dev;
109 dev = ksz_switch_alloc(&spi->dev, &ksz9477_spi_ops, spi);
113 for (i = 0; i < ARRAY_SIZE(ksz9477_regmap_config); i++) {
114 dev->regmap[i] = devm_regmap_init_spi(spi,
115 &ksz9477_regmap_config[i]);
116 if (IS_ERR(dev->regmap[i])) {
117 ret = PTR_ERR(dev->regmap[i]);
119 "Failed to initialize regmap%i: %d\n",
120 ksz9477_regmap_config[i].val_bits, ret);
125 if (spi->dev.platform_data)
126 dev->pdata = spi->dev.platform_data;
128 ret = ksz9477_switch_register(dev);
130 /* Main DSA driver may not be started yet. */
134 spi_set_drvdata(spi, dev);
139 static int ksz9477_spi_remove(struct spi_device *spi)
141 struct ksz_device *dev = spi_get_drvdata(spi);
144 ksz_switch_remove(dev);
149 static void ksz9477_spi_shutdown(struct spi_device *spi)
151 struct ksz_device *dev = spi_get_drvdata(spi);
153 if (dev && dev->dev_ops->shutdown)
154 dev->dev_ops->shutdown(dev);
157 static const struct of_device_id ksz9477_dt_ids[] = {
158 { .compatible = "microchip,ksz9477" },
159 { .compatible = "microchip,ksz9897" },
160 { .compatible = "microchip,ksz9893" },
161 { .compatible = "microchip,ksz9563" },
164 MODULE_DEVICE_TABLE(of, ksz9477_dt_ids);
166 static struct spi_driver ksz9477_spi_driver = {
168 .name = "ksz9477-switch",
169 .owner = THIS_MODULE,
170 .of_match_table = of_match_ptr(ksz9477_dt_ids),
172 .probe = ksz9477_spi_probe,
173 .remove = ksz9477_spi_remove,
174 .shutdown = ksz9477_spi_shutdown,
177 module_spi_driver(ksz9477_spi_driver);
179 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
180 MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch SPI access Driver");
181 MODULE_LICENSE("GPL");