1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Microchip KSZ9477 series register access through SPI
4  *
5  * Copyright (C) 2017-2019 Microchip Technology Inc.
6  */
7 
8 #include <asm/unaligned.h>
9 
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/spi/spi.h>
15 
16 #include "ksz_common.h"
17 
18 #define SPI_ADDR_SHIFT			24
19 #define SPI_ADDR_ALIGN			3
20 #define SPI_TURNAROUND_SHIFT		5
21 
22 KSZ_REGMAP_TABLE(ksz9477, 32, SPI_ADDR_SHIFT,
23 		 SPI_TURNAROUND_SHIFT, SPI_ADDR_ALIGN);
24 
ksz9477_spi_probe(struct spi_device * spi)25 static int ksz9477_spi_probe(struct spi_device *spi)
26 {
27 	struct regmap_config rc;
28 	struct ksz_device *dev;
29 	int i, ret;
30 
31 	dev = ksz_switch_alloc(&spi->dev, spi);
32 	if (!dev)
33 		return -ENOMEM;
34 
35 	for (i = 0; i < ARRAY_SIZE(ksz9477_regmap_config); i++) {
36 		rc = ksz9477_regmap_config[i];
37 		rc.lock_arg = &dev->regmap_mutex;
38 		dev->regmap[i] = devm_regmap_init_spi(spi, &rc);
39 		if (IS_ERR(dev->regmap[i])) {
40 			ret = PTR_ERR(dev->regmap[i]);
41 			dev_err(&spi->dev,
42 				"Failed to initialize regmap%i: %d\n",
43 				ksz9477_regmap_config[i].val_bits, ret);
44 			return ret;
45 		}
46 	}
47 
48 	if (spi->dev.platform_data)
49 		dev->pdata = spi->dev.platform_data;
50 
51 	/* setup spi */
52 	spi->mode = SPI_MODE_3;
53 	ret = spi_setup(spi);
54 	if (ret)
55 		return ret;
56 
57 	ret = ksz9477_switch_register(dev);
58 
59 	/* Main DSA driver may not be started yet. */
60 	if (ret)
61 		return ret;
62 
63 	spi_set_drvdata(spi, dev);
64 
65 	return 0;
66 }
67 
ksz9477_spi_remove(struct spi_device * spi)68 static void ksz9477_spi_remove(struct spi_device *spi)
69 {
70 	struct ksz_device *dev = spi_get_drvdata(spi);
71 
72 	if (dev)
73 		ksz_switch_remove(dev);
74 
75 	spi_set_drvdata(spi, NULL);
76 }
77 
ksz9477_spi_shutdown(struct spi_device * spi)78 static void ksz9477_spi_shutdown(struct spi_device *spi)
79 {
80 	struct ksz_device *dev = spi_get_drvdata(spi);
81 
82 	if (dev)
83 		dsa_switch_shutdown(dev->ds);
84 
85 	spi_set_drvdata(spi, NULL);
86 }
87 
88 static const struct of_device_id ksz9477_dt_ids[] = {
89 	{
90 		.compatible = "microchip,ksz9477",
91 		.data = &ksz_switch_chips[KSZ9477]
92 	},
93 	{
94 		.compatible = "microchip,ksz9897",
95 		.data = &ksz_switch_chips[KSZ9897]
96 	},
97 	{
98 		.compatible = "microchip,ksz9893",
99 		.data = &ksz_switch_chips[KSZ9893]
100 	},
101 	{
102 		.compatible = "microchip,ksz9563",
103 		.data = &ksz_switch_chips[KSZ9893]
104 	},
105 	{
106 		.compatible = "microchip,ksz8563",
107 		.data = &ksz_switch_chips[KSZ9893]
108 	},
109 	{
110 		.compatible = "microchip,ksz9567",
111 		.data = &ksz_switch_chips[KSZ9567]
112 	},
113 	{},
114 };
115 MODULE_DEVICE_TABLE(of, ksz9477_dt_ids);
116 
117 static const struct spi_device_id ksz9477_spi_ids[] = {
118 	{ "ksz9477" },
119 	{ "ksz9897" },
120 	{ "ksz9893" },
121 	{ "ksz9563" },
122 	{ "ksz8563" },
123 	{ "ksz9567" },
124 	{ },
125 };
126 MODULE_DEVICE_TABLE(spi, ksz9477_spi_ids);
127 
128 static struct spi_driver ksz9477_spi_driver = {
129 	.driver = {
130 		.name	= "ksz9477-switch",
131 		.owner	= THIS_MODULE,
132 		.of_match_table = of_match_ptr(ksz9477_dt_ids),
133 	},
134 	.id_table = ksz9477_spi_ids,
135 	.probe	= ksz9477_spi_probe,
136 	.remove	= ksz9477_spi_remove,
137 	.shutdown = ksz9477_spi_shutdown,
138 };
139 
140 module_spi_driver(ksz9477_spi_driver);
141 
142 MODULE_ALIAS("spi:ksz9477");
143 MODULE_ALIAS("spi:ksz9897");
144 MODULE_ALIAS("spi:ksz9893");
145 MODULE_ALIAS("spi:ksz9563");
146 MODULE_ALIAS("spi:ksz8563");
147 MODULE_ALIAS("spi:ksz9567");
148 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
149 MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch SPI access Driver");
150 MODULE_LICENSE("GPL");
151