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 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 ret = PTR_ERR(dev->regmap[i]); 75 dev_err(&spi->dev, 76 "Failed to initialize regmap%i: %d\n", 77 regmap_config[i].val_bits, ret); 78 return ret; 79 } 80 } 81 82 if (spi->dev.platform_data) 83 dev->pdata = spi->dev.platform_data; 84 85 /* setup spi */ 86 spi->mode = SPI_MODE_3; 87 ret = spi_setup(spi); 88 if (ret) 89 return ret; 90 91 dev->irq = spi->irq; 92 93 ret = ksz_switch_register(dev); 94 95 /* Main DSA driver may not be started yet. */ 96 if (ret) 97 return ret; 98 99 spi_set_drvdata(spi, dev); 100 101 return 0; 102 } 103 104 static void ksz_spi_remove(struct spi_device *spi) 105 { 106 struct ksz_device *dev = spi_get_drvdata(spi); 107 108 if (dev) 109 ksz_switch_remove(dev); 110 } 111 112 static void ksz_spi_shutdown(struct spi_device *spi) 113 { 114 struct ksz_device *dev = spi_get_drvdata(spi); 115 116 if (!dev) 117 return; 118 119 if (dev->dev_ops->reset) 120 dev->dev_ops->reset(dev); 121 122 dsa_switch_shutdown(dev->ds); 123 124 spi_set_drvdata(spi, NULL); 125 } 126 127 static const struct of_device_id ksz_dt_ids[] = { 128 { 129 .compatible = "microchip,ksz8765", 130 .data = &ksz_switch_chips[KSZ8765] 131 }, 132 { 133 .compatible = "microchip,ksz8794", 134 .data = &ksz_switch_chips[KSZ8794] 135 }, 136 { 137 .compatible = "microchip,ksz8795", 138 .data = &ksz_switch_chips[KSZ8795] 139 }, 140 { 141 .compatible = "microchip,ksz8863", 142 .data = &ksz_switch_chips[KSZ8830] 143 }, 144 { 145 .compatible = "microchip,ksz8873", 146 .data = &ksz_switch_chips[KSZ8830] 147 }, 148 { 149 .compatible = "microchip,ksz9477", 150 .data = &ksz_switch_chips[KSZ9477] 151 }, 152 { 153 .compatible = "microchip,ksz9896", 154 .data = &ksz_switch_chips[KSZ9896] 155 }, 156 { 157 .compatible = "microchip,ksz9897", 158 .data = &ksz_switch_chips[KSZ9897] 159 }, 160 { 161 .compatible = "microchip,ksz9893", 162 .data = &ksz_switch_chips[KSZ9893] 163 }, 164 { 165 .compatible = "microchip,ksz9563", 166 .data = &ksz_switch_chips[KSZ9893] 167 }, 168 { 169 .compatible = "microchip,ksz8563", 170 .data = &ksz_switch_chips[KSZ8563] 171 }, 172 { 173 .compatible = "microchip,ksz9567", 174 .data = &ksz_switch_chips[KSZ9567] 175 }, 176 { 177 .compatible = "microchip,lan9370", 178 .data = &ksz_switch_chips[LAN9370] 179 }, 180 { 181 .compatible = "microchip,lan9371", 182 .data = &ksz_switch_chips[LAN9371] 183 }, 184 { 185 .compatible = "microchip,lan9372", 186 .data = &ksz_switch_chips[LAN9372] 187 }, 188 { 189 .compatible = "microchip,lan9373", 190 .data = &ksz_switch_chips[LAN9373] 191 }, 192 { 193 .compatible = "microchip,lan9374", 194 .data = &ksz_switch_chips[LAN9374] 195 }, 196 {}, 197 }; 198 MODULE_DEVICE_TABLE(of, ksz_dt_ids); 199 200 static const struct spi_device_id ksz_spi_ids[] = { 201 { "ksz8765" }, 202 { "ksz8794" }, 203 { "ksz8795" }, 204 { "ksz8863" }, 205 { "ksz8873" }, 206 { "ksz9477" }, 207 { "ksz9896" }, 208 { "ksz9897" }, 209 { "ksz9893" }, 210 { "ksz9563" }, 211 { "ksz8563" }, 212 { "ksz9567" }, 213 { "lan9370" }, 214 { "lan9371" }, 215 { "lan9372" }, 216 { "lan9373" }, 217 { "lan9374" }, 218 { }, 219 }; 220 MODULE_DEVICE_TABLE(spi, ksz_spi_ids); 221 222 static struct spi_driver ksz_spi_driver = { 223 .driver = { 224 .name = "ksz-switch", 225 .owner = THIS_MODULE, 226 .of_match_table = ksz_dt_ids, 227 }, 228 .id_table = ksz_spi_ids, 229 .probe = ksz_spi_probe, 230 .remove = ksz_spi_remove, 231 .shutdown = ksz_spi_shutdown, 232 }; 233 234 module_spi_driver(ksz_spi_driver); 235 236 MODULE_ALIAS("spi:ksz9477"); 237 MODULE_ALIAS("spi:ksz9896"); 238 MODULE_ALIAS("spi:ksz9897"); 239 MODULE_ALIAS("spi:ksz9893"); 240 MODULE_ALIAS("spi:ksz9563"); 241 MODULE_ALIAS("spi:ksz8563"); 242 MODULE_ALIAS("spi:ksz9567"); 243 MODULE_ALIAS("spi:lan937x"); 244 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>"); 245 MODULE_DESCRIPTION("Microchip ksz Series Switch SPI Driver"); 246 MODULE_LICENSE("GPL"); 247