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);
100 static void ksz8795_spi_shutdown(struct spi_device *spi)
102 struct ksz_device *dev = spi_get_drvdata(spi);
104 if (dev && dev->dev_ops->shutdown)
105 dev->dev_ops->shutdown(dev);
108 static const struct of_device_id ksz8795_dt_ids[] = {
109 { .compatible = "microchip,ksz8765", .data = &ksz8795_regmap_config },
110 { .compatible = "microchip,ksz8794", .data = &ksz8795_regmap_config },
111 { .compatible = "microchip,ksz8795", .data = &ksz8795_regmap_config },
112 { .compatible = "microchip,ksz8863", .data = &ksz8863_regmap_config },
113 { .compatible = "microchip,ksz8873", .data = &ksz8863_regmap_config },
116 MODULE_DEVICE_TABLE(of, ksz8795_dt_ids);
118 static struct spi_driver ksz8795_spi_driver = {
120 .name = "ksz8795-switch",
121 .owner = THIS_MODULE,
122 .of_match_table = of_match_ptr(ksz8795_dt_ids),
124 .probe = ksz8795_spi_probe,
125 .remove = ksz8795_spi_remove,
126 .shutdown = ksz8795_spi_shutdown,
129 module_spi_driver(ksz8795_spi_driver);
131 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
132 MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch SPI Driver");
133 MODULE_LICENSE("GPL");