1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Microchip KSZ8795 series register access through SPI
5 * Copyright (C) 2017 Microchip Technology Inc.
6 * Tristram Ha <Tristram.Ha@microchip.com>
9 #include <asm/unaligned.h>
11 #include <linux/delay.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/regmap.h>
15 #include <linux/spi/spi.h>
18 #include "ksz_common.h"
20 #define KSZ8795_SPI_ADDR_SHIFT 12
21 #define KSZ8795_SPI_ADDR_ALIGN 3
22 #define KSZ8795_SPI_TURNAROUND_SHIFT 1
24 #define KSZ8863_SPI_ADDR_SHIFT 8
25 #define KSZ8863_SPI_ADDR_ALIGN 8
26 #define KSZ8863_SPI_TURNAROUND_SHIFT 0
28 KSZ_REGMAP_TABLE(ksz8795, 16, KSZ8795_SPI_ADDR_SHIFT,
29 KSZ8795_SPI_TURNAROUND_SHIFT, KSZ8795_SPI_ADDR_ALIGN);
31 KSZ_REGMAP_TABLE(ksz8863, 16, KSZ8863_SPI_ADDR_SHIFT,
32 KSZ8863_SPI_TURNAROUND_SHIFT, KSZ8863_SPI_ADDR_ALIGN);
34 static int ksz8795_spi_probe(struct spi_device *spi)
36 const struct regmap_config *regmap_config;
37 struct device *ddev = &spi->dev;
38 struct regmap_config rc;
39 struct ksz_device *dev;
43 ksz8 = devm_kzalloc(&spi->dev, sizeof(struct ksz8), GFP_KERNEL);
49 dev = ksz_switch_alloc(&spi->dev, ksz8);
53 regmap_config = device_get_match_data(ddev);
57 for (i = 0; i < ARRAY_SIZE(ksz8795_regmap_config); i++) {
58 rc = regmap_config[i];
59 rc.lock_arg = &dev->regmap_mutex;
60 dev->regmap[i] = devm_regmap_init_spi(spi, &rc);
61 if (IS_ERR(dev->regmap[i])) {
62 ret = PTR_ERR(dev->regmap[i]);
64 "Failed to initialize regmap%i: %d\n",
65 regmap_config[i].val_bits, ret);
70 if (spi->dev.platform_data)
71 dev->pdata = spi->dev.platform_data;
74 spi->mode = SPI_MODE_3;
79 ret = ksz8_switch_register(dev);
81 /* Main DSA driver may not be started yet. */
85 spi_set_drvdata(spi, dev);
90 static int ksz8795_spi_remove(struct spi_device *spi)
92 struct ksz_device *dev = spi_get_drvdata(spi);
95 ksz_switch_remove(dev);
97 spi_set_drvdata(spi, NULL);
102 static void ksz8795_spi_shutdown(struct spi_device *spi)
104 struct ksz_device *dev = spi_get_drvdata(spi);
109 if (dev->dev_ops->shutdown)
110 dev->dev_ops->shutdown(dev);
112 dsa_switch_shutdown(dev->ds);
114 spi_set_drvdata(spi, NULL);
117 static const struct of_device_id ksz8795_dt_ids[] = {
118 { .compatible = "microchip,ksz8765", .data = &ksz8795_regmap_config },
119 { .compatible = "microchip,ksz8794", .data = &ksz8795_regmap_config },
120 { .compatible = "microchip,ksz8795", .data = &ksz8795_regmap_config },
121 { .compatible = "microchip,ksz8863", .data = &ksz8863_regmap_config },
122 { .compatible = "microchip,ksz8873", .data = &ksz8863_regmap_config },
125 MODULE_DEVICE_TABLE(of, ksz8795_dt_ids);
127 static struct spi_driver ksz8795_spi_driver = {
129 .name = "ksz8795-switch",
130 .owner = THIS_MODULE,
131 .of_match_table = of_match_ptr(ksz8795_dt_ids),
133 .probe = ksz8795_spi_probe,
134 .remove = ksz8795_spi_remove,
135 .shutdown = ksz8795_spi_shutdown,
138 module_spi_driver(ksz8795_spi_driver);
140 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
141 MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch SPI Driver");
142 MODULE_LICENSE("GPL");