WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / dsa / microchip / ksz8795_spi.c
blobf98432a3e2b5562f159a82328d76e3d2c482e187
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Microchip KSZ8795 series register access through SPI
5 * Copyright (C) 2017 Microchip Technology Inc.
6 * Tristram Ha <Tristram.Ha@microchip.com>
7 */
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>
17 #include "ksz_common.h"
19 #define SPI_ADDR_SHIFT 12
20 #define SPI_ADDR_ALIGN 3
21 #define SPI_TURNAROUND_SHIFT 1
23 KSZ_REGMAP_TABLE(ksz8795, 16, SPI_ADDR_SHIFT,
24 SPI_TURNAROUND_SHIFT, SPI_ADDR_ALIGN);
26 static int ksz8795_spi_probe(struct spi_device *spi)
28 struct regmap_config rc;
29 struct ksz_device *dev;
30 int i, ret;
32 dev = ksz_switch_alloc(&spi->dev, spi);
33 if (!dev)
34 return -ENOMEM;
36 for (i = 0; i < ARRAY_SIZE(ksz8795_regmap_config); i++) {
37 rc = ksz8795_regmap_config[i];
38 rc.lock_arg = &dev->regmap_mutex;
39 dev->regmap[i] = devm_regmap_init_spi(spi, &rc);
40 if (IS_ERR(dev->regmap[i])) {
41 ret = PTR_ERR(dev->regmap[i]);
42 dev_err(&spi->dev,
43 "Failed to initialize regmap%i: %d\n",
44 ksz8795_regmap_config[i].val_bits, ret);
45 return ret;
49 if (spi->dev.platform_data)
50 dev->pdata = spi->dev.platform_data;
52 /* setup spi */
53 spi->mode = SPI_MODE_3;
54 ret = spi_setup(spi);
55 if (ret)
56 return ret;
58 ret = ksz8795_switch_register(dev);
60 /* Main DSA driver may not be started yet. */
61 if (ret)
62 return ret;
64 spi_set_drvdata(spi, dev);
66 return 0;
69 static int ksz8795_spi_remove(struct spi_device *spi)
71 struct ksz_device *dev = spi_get_drvdata(spi);
73 if (dev)
74 ksz_switch_remove(dev);
76 return 0;
79 static void ksz8795_spi_shutdown(struct spi_device *spi)
81 struct ksz_device *dev = spi_get_drvdata(spi);
83 if (dev && dev->dev_ops->shutdown)
84 dev->dev_ops->shutdown(dev);
87 static const struct of_device_id ksz8795_dt_ids[] = {
88 { .compatible = "microchip,ksz8765" },
89 { .compatible = "microchip,ksz8794" },
90 { .compatible = "microchip,ksz8795" },
91 {},
93 MODULE_DEVICE_TABLE(of, ksz8795_dt_ids);
95 static struct spi_driver ksz8795_spi_driver = {
96 .driver = {
97 .name = "ksz8795-switch",
98 .owner = THIS_MODULE,
99 .of_match_table = of_match_ptr(ksz8795_dt_ids),
101 .probe = ksz8795_spi_probe,
102 .remove = ksz8795_spi_remove,
103 .shutdown = ksz8795_spi_shutdown,
106 module_spi_driver(ksz8795_spi_driver);
108 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
109 MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch SPI Driver");
110 MODULE_LICENSE("GPL");