1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Microchip ksz series register access through SPI
4 *
5 * Copyright (C) 2017 Microchip Technology Inc.
6 * Tristram Ha <Tristram.Ha@microchip.com>
7 */
8
9 #include <asm/unaligned.h>
10
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>
16
17 #include "ksz_common.h"
18
19 #define KSZ8795_SPI_ADDR_SHIFT 12
20 #define KSZ8795_SPI_ADDR_ALIGN 3
21 #define KSZ8795_SPI_TURNAROUND_SHIFT 1
22
23 #define KSZ8863_SPI_ADDR_SHIFT 8
24 #define KSZ8863_SPI_ADDR_ALIGN 8
25 #define KSZ8863_SPI_TURNAROUND_SHIFT 0
26
27 #define KSZ9477_SPI_ADDR_SHIFT 24
28 #define KSZ9477_SPI_ADDR_ALIGN 3
29 #define KSZ9477_SPI_TURNAROUND_SHIFT 5
30
31 KSZ_REGMAP_TABLE(ksz8795, 16, KSZ8795_SPI_ADDR_SHIFT,
32 KSZ8795_SPI_TURNAROUND_SHIFT, KSZ8795_SPI_ADDR_ALIGN);
33
34 KSZ_REGMAP_TABLE(ksz8863, 16, KSZ8863_SPI_ADDR_SHIFT,
35 KSZ8863_SPI_TURNAROUND_SHIFT, KSZ8863_SPI_ADDR_ALIGN);
36
37 KSZ_REGMAP_TABLE(ksz9477, 32, KSZ9477_SPI_ADDR_SHIFT,
38 KSZ9477_SPI_TURNAROUND_SHIFT, KSZ9477_SPI_ADDR_ALIGN);
39
ksz_spi_probe(struct spi_device * spi)40 static int ksz_spi_probe(struct spi_device *spi)
41 {
42 const struct regmap_config *regmap_config;
43 const struct ksz_chip_data *chip;
44 struct device *ddev = &spi->dev;
45 struct regmap_config rc;
46 struct ksz_device *dev;
47 int i, ret = 0;
48
49 dev = ksz_switch_alloc(&spi->dev, spi);
50 if (!dev)
51 return -ENOMEM;
52
53 chip = device_get_match_data(ddev);
54 if (!chip)
55 return -EINVAL;
56
57 if (chip->chip_id == KSZ8830_CHIP_ID)
58 regmap_config = ksz8863_regmap_config;
59 else if (chip->chip_id == KSZ8795_CHIP_ID ||
60 chip->chip_id == KSZ8794_CHIP_ID ||
61 chip->chip_id == KSZ8765_CHIP_ID)
62 regmap_config = ksz8795_regmap_config;
63 else
64 regmap_config = ksz9477_regmap_config;
65
66 for (i = 0; i < ARRAY_SIZE(ksz8795_regmap_config); i++) {
67 rc = regmap_config[i];
68 rc.lock_arg = &dev->regmap_mutex;
69 rc.wr_table = chip->wr_table;
70 rc.rd_table = chip->rd_table;
71 dev->regmap[i] = devm_regmap_init_spi(spi, &rc);
72
73 if (IS_ERR(dev->regmap[i])) {
74 return dev_err_probe(&spi->dev, PTR_ERR(dev->regmap[i]),
75 "Failed to initialize regmap%i\n",
76 regmap_config[i].val_bits);
77 }
78 }
79
80 if (spi->dev.platform_data)
81 dev->pdata = spi->dev.platform_data;
82
83 /* setup spi */
84 spi->mode = SPI_MODE_3;
85 ret = spi_setup(spi);
86 if (ret)
87 return ret;
88
89 dev->irq = spi->irq;
90
91 ret = ksz_switch_register(dev);
92
93 /* Main DSA driver may not be started yet. */
94 if (ret)
95 return ret;
96
97 spi_set_drvdata(spi, dev);
98
99 return 0;
100 }
101
ksz_spi_remove(struct spi_device * spi)102 static void ksz_spi_remove(struct spi_device *spi)
103 {
104 struct ksz_device *dev = spi_get_drvdata(spi);
105
106 if (dev)
107 ksz_switch_remove(dev);
108 }
109
ksz_spi_shutdown(struct spi_device * spi)110 static void ksz_spi_shutdown(struct spi_device *spi)
111 {
112 struct ksz_device *dev = spi_get_drvdata(spi);
113
114 if (!dev)
115 return;
116
117 if (dev->dev_ops->reset)
118 dev->dev_ops->reset(dev);
119
120 dsa_switch_shutdown(dev->ds);
121
122 spi_set_drvdata(spi, NULL);
123 }
124
125 static const struct of_device_id ksz_dt_ids[] = {
126 {
127 .compatible = "microchip,ksz8765",
128 .data = &ksz_switch_chips[KSZ8765]
129 },
130 {
131 .compatible = "microchip,ksz8794",
132 .data = &ksz_switch_chips[KSZ8794]
133 },
134 {
135 .compatible = "microchip,ksz8795",
136 .data = &ksz_switch_chips[KSZ8795]
137 },
138 {
139 .compatible = "microchip,ksz8863",
140 .data = &ksz_switch_chips[KSZ8830]
141 },
142 {
143 .compatible = "microchip,ksz8873",
144 .data = &ksz_switch_chips[KSZ8830]
145 },
146 {
147 .compatible = "microchip,ksz9477",
148 .data = &ksz_switch_chips[KSZ9477]
149 },
150 {
151 .compatible = "microchip,ksz9896",
152 .data = &ksz_switch_chips[KSZ9896]
153 },
154 {
155 .compatible = "microchip,ksz9897",
156 .data = &ksz_switch_chips[KSZ9897]
157 },
158 {
159 .compatible = "microchip,ksz9893",
160 .data = &ksz_switch_chips[KSZ9893]
161 },
162 {
163 .compatible = "microchip,ksz9563",
164 .data = &ksz_switch_chips[KSZ9563]
165 },
166 {
167 .compatible = "microchip,ksz8563",
168 .data = &ksz_switch_chips[KSZ8563]
169 },
170 {
171 .compatible = "microchip,ksz9567",
172 .data = &ksz_switch_chips[KSZ9567]
173 },
174 {
175 .compatible = "microchip,lan9370",
176 .data = &ksz_switch_chips[LAN9370]
177 },
178 {
179 .compatible = "microchip,lan9371",
180 .data = &ksz_switch_chips[LAN9371]
181 },
182 {
183 .compatible = "microchip,lan9372",
184 .data = &ksz_switch_chips[LAN9372]
185 },
186 {
187 .compatible = "microchip,lan9373",
188 .data = &ksz_switch_chips[LAN9373]
189 },
190 {
191 .compatible = "microchip,lan9374",
192 .data = &ksz_switch_chips[LAN9374]
193 },
194 {},
195 };
196 MODULE_DEVICE_TABLE(of, ksz_dt_ids);
197
198 static const struct spi_device_id ksz_spi_ids[] = {
199 { "ksz8765" },
200 { "ksz8794" },
201 { "ksz8795" },
202 { "ksz8863" },
203 { "ksz8873" },
204 { "ksz9477" },
205 { "ksz9896" },
206 { "ksz9897" },
207 { "ksz9893" },
208 { "ksz9563" },
209 { "ksz8563" },
210 { "ksz9567" },
211 { "lan9370" },
212 { "lan9371" },
213 { "lan9372" },
214 { "lan9373" },
215 { "lan9374" },
216 { },
217 };
218 MODULE_DEVICE_TABLE(spi, ksz_spi_ids);
219
220 static struct spi_driver ksz_spi_driver = {
221 .driver = {
222 .name = "ksz-switch",
223 .owner = THIS_MODULE,
224 .of_match_table = ksz_dt_ids,
225 },
226 .id_table = ksz_spi_ids,
227 .probe = ksz_spi_probe,
228 .remove = ksz_spi_remove,
229 .shutdown = ksz_spi_shutdown,
230 };
231
232 module_spi_driver(ksz_spi_driver);
233
234 MODULE_ALIAS("spi:ksz9477");
235 MODULE_ALIAS("spi:ksz9896");
236 MODULE_ALIAS("spi:ksz9897");
237 MODULE_ALIAS("spi:ksz9893");
238 MODULE_ALIAS("spi:ksz9563");
239 MODULE_ALIAS("spi:ksz8563");
240 MODULE_ALIAS("spi:ksz9567");
241 MODULE_ALIAS("spi:lan937x");
242 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
243 MODULE_DESCRIPTION("Microchip ksz Series Switch SPI Driver");
244 MODULE_LICENSE("GPL");
245