Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Microchip ksz series register access through SPI
  4 *
  5 * Copyright (C) 2017-2024 Microchip Technology Inc.
  6 *	Tristram Ha <Tristram.Ha@microchip.com>
 
 
 
 
 
 
 
 
 
 
 
  7 */
  8
  9#include <linux/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
 31KSZ_REGMAP_TABLE(ksz8795, 16, KSZ8795_SPI_ADDR_SHIFT,
 32		 KSZ8795_SPI_TURNAROUND_SHIFT, KSZ8795_SPI_ADDR_ALIGN);
 33
 34KSZ_REGMAP_TABLE(ksz8863, 16, KSZ8863_SPI_ADDR_SHIFT,
 35		 KSZ8863_SPI_TURNAROUND_SHIFT, KSZ8863_SPI_ADDR_ALIGN);
 
 36
 37KSZ_REGMAP_TABLE(ksz9477, 32, KSZ9477_SPI_ADDR_SHIFT,
 38		 KSZ9477_SPI_TURNAROUND_SHIFT, KSZ9477_SPI_ADDR_ALIGN);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 39
 40static 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	/* Save chip id to do special initialization when probing. */
 58	dev->chip_id = chip->chip_id;
 59	if (chip->chip_id == KSZ88X3_CHIP_ID)
 60		regmap_config = ksz8863_regmap_config;
 61	else if (chip->chip_id == KSZ8795_CHIP_ID ||
 62		 chip->chip_id == KSZ8794_CHIP_ID ||
 63		 chip->chip_id == KSZ8765_CHIP_ID)
 64		regmap_config = ksz8795_regmap_config;
 65	else if (chip->chip_id == KSZ8895_CHIP_ID ||
 66		 chip->chip_id == KSZ8864_CHIP_ID)
 67		regmap_config = ksz8863_regmap_config;
 68	else
 69		regmap_config = ksz9477_regmap_config;
 70
 71	for (i = 0; i < __KSZ_NUM_REGMAPS; i++) {
 72		rc = regmap_config[i];
 73		rc.lock_arg = &dev->regmap_mutex;
 74		rc.wr_table = chip->wr_table;
 75		rc.rd_table = chip->rd_table;
 76		dev->regmap[i] = devm_regmap_init_spi(spi, &rc);
 77
 78		if (IS_ERR(dev->regmap[i])) {
 79			return dev_err_probe(&spi->dev, PTR_ERR(dev->regmap[i]),
 80					     "Failed to initialize regmap%i\n",
 81					     regmap_config[i].val_bits);
 82		}
 83	}
 84
 85	if (spi->dev.platform_data)
 86		dev->pdata = spi->dev.platform_data;
 87
 88	/* setup spi */
 89	spi->mode = SPI_MODE_3;
 90	ret = spi_setup(spi);
 91	if (ret)
 92		return ret;
 93
 94	dev->irq = spi->irq;
 95
 96	ret = ksz_switch_register(dev);
 97
 98	/* Main DSA driver may not be started yet. */
 99	if (ret)
100		return ret;
101
102	spi_set_drvdata(spi, dev);
103
104	return 0;
105}
106
107static void ksz_spi_remove(struct spi_device *spi)
108{
109	struct ksz_device *dev = spi_get_drvdata(spi);
110
111	if (dev)
112		ksz_switch_remove(dev);
113}
114
115static void ksz_spi_shutdown(struct spi_device *spi)
116{
117	struct ksz_device *dev = spi_get_drvdata(spi);
118
119	if (!dev)
120		return;
121
122	ksz_switch_shutdown(dev);
123
124	spi_set_drvdata(spi, NULL);
125}
126
127static 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[KSZ88X3]
143	},
144	{
145		.compatible = "microchip,ksz8864",
146		.data = &ksz_switch_chips[KSZ8864]
147	},
148	{
149		.compatible = "microchip,ksz8873",
150		.data = &ksz_switch_chips[KSZ88X3]
151	},
152	{
153		.compatible = "microchip,ksz8895",
154		.data = &ksz_switch_chips[KSZ8895]
155	},
156	{
157		.compatible = "microchip,ksz9477",
158		.data = &ksz_switch_chips[KSZ9477]
159	},
160	{
161		.compatible = "microchip,ksz9896",
162		.data = &ksz_switch_chips[KSZ9896]
163	},
164	{
165		.compatible = "microchip,ksz9897",
166		.data = &ksz_switch_chips[KSZ9897]
167	},
168	{
169		.compatible = "microchip,ksz9893",
170		.data = &ksz_switch_chips[KSZ9893]
171	},
172	{
173		.compatible = "microchip,ksz9563",
174		.data = &ksz_switch_chips[KSZ9563]
175	},
176	{
177		.compatible = "microchip,ksz8563",
178		.data = &ksz_switch_chips[KSZ8563]
179	},
180	{
181		.compatible = "microchip,ksz8567",
182		.data = &ksz_switch_chips[KSZ8567]
183	},
184	{
185		.compatible = "microchip,ksz9567",
186		.data = &ksz_switch_chips[KSZ9567]
187	},
188	{
189		.compatible = "microchip,lan9370",
190		.data = &ksz_switch_chips[LAN9370]
191	},
192	{
193		.compatible = "microchip,lan9371",
194		.data = &ksz_switch_chips[LAN9371]
195	},
196	{
197		.compatible = "microchip,lan9372",
198		.data = &ksz_switch_chips[LAN9372]
199	},
200	{
201		.compatible = "microchip,lan9373",
202		.data = &ksz_switch_chips[LAN9373]
203	},
204	{
205		.compatible = "microchip,lan9374",
206		.data = &ksz_switch_chips[LAN9374]
207	},
208	{
209		.compatible = "microchip,lan9646",
210		.data = &ksz_switch_chips[LAN9646]
211	},
212	{},
213};
214MODULE_DEVICE_TABLE(of, ksz_dt_ids);
215
216static const struct spi_device_id ksz_spi_ids[] = {
217	{ "ksz8765" },
218	{ "ksz8794" },
219	{ "ksz8795" },
220	{ "ksz8863" },
221	{ "ksz8864" },
222	{ "ksz8873" },
223	{ "ksz8895" },
224	{ "ksz9477" },
225	{ "ksz9896" },
226	{ "ksz9897" },
227	{ "ksz9893" },
228	{ "ksz9563" },
229	{ "ksz8563" },
230	{ "ksz8567" },
231	{ "ksz9567" },
232	{ "lan9370" },
233	{ "lan9371" },
234	{ "lan9372" },
235	{ "lan9373" },
236	{ "lan9374" },
237	{ "lan9646" },
238	{ },
239};
240MODULE_DEVICE_TABLE(spi, ksz_spi_ids);
241
242static struct spi_driver ksz_spi_driver = {
243	.driver = {
244		.name	= "ksz-switch",
245		.of_match_table = ksz_dt_ids,
 
246	},
247	.id_table = ksz_spi_ids,
248	.probe	= ksz_spi_probe,
249	.remove	= ksz_spi_remove,
250	.shutdown = ksz_spi_shutdown,
251};
252
253module_spi_driver(ksz_spi_driver);
254
255MODULE_ALIAS("spi:lan937x");
256MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
257MODULE_DESCRIPTION("Microchip ksz Series Switch SPI Driver");
258MODULE_LICENSE("GPL");
v4.17
 
  1/*
  2 * Microchip KSZ series register access through SPI
  3 *
  4 * Copyright (C) 2017
  5 *
  6 * Permission to use, copy, modify, and/or distribute this software for any
  7 * purpose with or without fee is hereby granted, provided that the above
  8 * copyright notice and this permission notice appear in all copies.
  9 *
 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 17 */
 18
 19#include <asm/unaligned.h>
 20
 21#include <linux/delay.h>
 22#include <linux/kernel.h>
 23#include <linux/module.h>
 
 24#include <linux/spi/spi.h>
 25
 26#include "ksz_priv.h"
 27
 28/* SPI frame opcodes */
 29#define KS_SPIOP_RD			3
 30#define KS_SPIOP_WR			2
 31
 32#define SPI_ADDR_SHIFT			24
 33#define SPI_ADDR_MASK			(BIT(SPI_ADDR_SHIFT) - 1)
 34#define SPI_TURNAROUND_SHIFT		5
 35
 36static int ksz_spi_read_reg(struct spi_device *spi, u32 reg, u8 *val,
 37			    unsigned int len)
 38{
 39	u32 txbuf;
 40	int ret;
 41
 42	txbuf = reg & SPI_ADDR_MASK;
 43	txbuf |= KS_SPIOP_RD << SPI_ADDR_SHIFT;
 44	txbuf <<= SPI_TURNAROUND_SHIFT;
 45	txbuf = cpu_to_be32(txbuf);
 46
 47	ret = spi_write_then_read(spi, &txbuf, 4, val, len);
 48	return ret;
 49}
 50
 51static int ksz_spi_read(struct ksz_device *dev, u32 reg, u8 *data,
 52			unsigned int len)
 53{
 54	struct spi_device *spi = dev->priv;
 55
 56	return ksz_spi_read_reg(spi, reg, data, len);
 57}
 58
 59static int ksz_spi_read8(struct ksz_device *dev, u32 reg, u8 *val)
 60{
 61	return ksz_spi_read(dev, reg, val, 1);
 62}
 63
 64static int ksz_spi_read16(struct ksz_device *dev, u32 reg, u16 *val)
 65{
 66	int ret = ksz_spi_read(dev, reg, (u8 *)val, 2);
 67
 68	if (!ret)
 69		*val = be16_to_cpu(*val);
 
 70
 71	return ret;
 72}
 73
 74static int ksz_spi_read24(struct ksz_device *dev, u32 reg, u32 *val)
 75{
 76	int ret;
 77
 78	*val = 0;
 79	ret = ksz_spi_read(dev, reg, (u8 *)val, 3);
 80	if (!ret) {
 81		*val = be32_to_cpu(*val);
 82		/* convert to 24bit */
 83		*val >>= 8;
 84	}
 85
 86	return ret;
 87}
 88
 89static int ksz_spi_read32(struct ksz_device *dev, u32 reg, u32 *val)
 90{
 91	int ret = ksz_spi_read(dev, reg, (u8 *)val, 4);
 92
 93	if (!ret)
 94		*val = be32_to_cpu(*val);
 95
 96	return ret;
 97}
 98
 99static int ksz_spi_write_reg(struct spi_device *spi, u32 reg, u8 *val,
100			     unsigned int len)
101{
102	u32 txbuf;
103	u8 data[12];
104	int i;
105
106	txbuf = reg & SPI_ADDR_MASK;
107	txbuf |= (KS_SPIOP_WR << SPI_ADDR_SHIFT);
108	txbuf <<= SPI_TURNAROUND_SHIFT;
109	txbuf = cpu_to_be32(txbuf);
110
111	data[0] = txbuf & 0xFF;
112	data[1] = (txbuf & 0xFF00) >> 8;
113	data[2] = (txbuf & 0xFF0000) >> 16;
114	data[3] = (txbuf & 0xFF000000) >> 24;
115	for (i = 0; i < len; i++)
116		data[i + 4] = val[i];
117
118	return spi_write(spi, &data, 4 + len);
119}
120
121static int ksz_spi_write8(struct ksz_device *dev, u32 reg, u8 value)
122{
123	struct spi_device *spi = dev->priv;
124
125	return ksz_spi_write_reg(spi, reg, &value, 1);
126}
127
128static int ksz_spi_write16(struct ksz_device *dev, u32 reg, u16 value)
129{
130	struct spi_device *spi = dev->priv;
131
132	value = cpu_to_be16(value);
133	return ksz_spi_write_reg(spi, reg, (u8 *)&value, 2);
134}
135
136static int ksz_spi_write24(struct ksz_device *dev, u32 reg, u32 value)
137{
138	struct spi_device *spi = dev->priv;
139
140	/* make it to big endian 24bit from MSB */
141	value <<= 8;
142	value = cpu_to_be32(value);
143	return ksz_spi_write_reg(spi, reg, (u8 *)&value, 3);
144}
145
146static int ksz_spi_write32(struct ksz_device *dev, u32 reg, u32 value)
147{
148	struct spi_device *spi = dev->priv;
149
150	value = cpu_to_be32(value);
151	return ksz_spi_write_reg(spi, reg, (u8 *)&value, 4);
152}
153
154static const struct ksz_io_ops ksz_spi_ops = {
155	.read8 = ksz_spi_read8,
156	.read16 = ksz_spi_read16,
157	.read24 = ksz_spi_read24,
158	.read32 = ksz_spi_read32,
159	.write8 = ksz_spi_write8,
160	.write16 = ksz_spi_write16,
161	.write24 = ksz_spi_write24,
162	.write32 = ksz_spi_write32,
163};
164
165static int ksz_spi_probe(struct spi_device *spi)
166{
 
 
 
 
167	struct ksz_device *dev;
168	int ret;
169
170	dev = ksz_switch_alloc(&spi->dev, &ksz_spi_ops, spi);
171	if (!dev)
172		return -ENOMEM;
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174	if (spi->dev.platform_data)
175		dev->pdata = spi->dev.platform_data;
176
 
 
 
 
 
 
 
 
177	ret = ksz_switch_register(dev);
 
 
178	if (ret)
179		return ret;
180
181	spi_set_drvdata(spi, dev);
182
183	return 0;
184}
185
186static int ksz_spi_remove(struct spi_device *spi)
187{
188	struct ksz_device *dev = spi_get_drvdata(spi);
189
190	if (dev)
191		ksz_switch_remove(dev);
 
 
 
 
 
 
 
 
 
 
192
193	return 0;
194}
195
196static const struct of_device_id ksz_dt_ids[] = {
197	{ .compatible = "microchip,ksz9477" },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198	{},
199};
200MODULE_DEVICE_TABLE(of, ksz_dt_ids);
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202static struct spi_driver ksz_spi_driver = {
203	.driver = {
204		.name	= "ksz9477-switch",
205		.owner	= THIS_MODULE,
206		.of_match_table = of_match_ptr(ksz_dt_ids),
207	},
 
208	.probe	= ksz_spi_probe,
209	.remove	= ksz_spi_remove,
 
210};
211
212module_spi_driver(ksz_spi_driver);
213
214MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
215MODULE_DESCRIPTION("Microchip KSZ Series Switch SPI access Driver");
 
216MODULE_LICENSE("GPL");