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