Skip to content

Commit 20e0377

Browse files
triha2workdavem330
authored andcommitted
net: dsa: microchip: add KSZ9477 I2C driver
Add KSZ9477 I2C driver support. The code ksz9477.c and ksz_common.c are used together to generate the I2C driver. Signed-off-by: Tristram Ha <[email protected]> [[email protected]: bring up to date, use ksz_common regmap macros] Signed-off-by: George McCollister <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 421bceb commit 20e0377

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

drivers/net/dsa/microchip/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ menuconfig NET_DSA_MICROCHIP_KSZ9477
99
help
1010
This driver adds support for Microchip KSZ9477 switch chips.
1111

12+
config NET_DSA_MICROCHIP_KSZ9477_I2C
13+
tristate "KSZ9477 series I2C connected switch driver"
14+
depends on NET_DSA_MICROCHIP_KSZ9477 && I2C
15+
select REGMAP_I2C
16+
help
17+
Select to enable support for registering switches configured through I2C.
18+
1219
config NET_DSA_MICROCHIP_KSZ9477_SPI
1320
tristate "KSZ9477 series SPI connected switch driver"
1421
depends on NET_DSA_MICROCHIP_KSZ9477 && SPI

drivers/net/dsa/microchip/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON) += ksz_common.o
33
obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477) += ksz9477.o
4+
obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_I2C) += ksz9477_i2c.o
45
obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_SPI) += ksz9477_spi.o
56
obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ8795) += ksz8795.o
67
obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ8795_SPI) += ksz8795_spi.o
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Microchip KSZ9477 series register access through I2C
4+
*
5+
* Copyright (C) 2018-2019 Microchip Technology Inc.
6+
*/
7+
8+
#include <linux/i2c.h>
9+
#include <linux/kernel.h>
10+
#include <linux/module.h>
11+
#include <linux/regmap.h>
12+
13+
#include "ksz_common.h"
14+
15+
KSZ_REGMAP_TABLE(ksz9477, not_used, 16, 0, 0);
16+
17+
static int ksz9477_i2c_probe(struct i2c_client *i2c,
18+
const struct i2c_device_id *i2c_id)
19+
{
20+
struct ksz_device *dev;
21+
int i, ret;
22+
23+
dev = ksz_switch_alloc(&i2c->dev, i2c);
24+
if (!dev)
25+
return -ENOMEM;
26+
27+
for (i = 0; i < ARRAY_SIZE(ksz9477_regmap_config); i++) {
28+
dev->regmap[i] = devm_regmap_init_i2c(i2c,
29+
&ksz9477_regmap_config[i]);
30+
if (IS_ERR(dev->regmap[i])) {
31+
ret = PTR_ERR(dev->regmap[i]);
32+
dev_err(&i2c->dev,
33+
"Failed to initialize regmap%i: %d\n",
34+
ksz9477_regmap_config[i].val_bits, ret);
35+
return ret;
36+
}
37+
}
38+
39+
if (i2c->dev.platform_data)
40+
dev->pdata = i2c->dev.platform_data;
41+
42+
ret = ksz9477_switch_register(dev);
43+
44+
/* Main DSA driver may not be started yet. */
45+
if (ret)
46+
return ret;
47+
48+
i2c_set_clientdata(i2c, dev);
49+
50+
return 0;
51+
}
52+
53+
static int ksz9477_i2c_remove(struct i2c_client *i2c)
54+
{
55+
struct ksz_device *dev = i2c_get_clientdata(i2c);
56+
57+
ksz_switch_remove(dev);
58+
59+
return 0;
60+
}
61+
62+
static void ksz9477_i2c_shutdown(struct i2c_client *i2c)
63+
{
64+
struct ksz_device *dev = i2c_get_clientdata(i2c);
65+
66+
if (dev && dev->dev_ops->shutdown)
67+
dev->dev_ops->shutdown(dev);
68+
}
69+
70+
static const struct i2c_device_id ksz9477_i2c_id[] = {
71+
{ "ksz9477-switch", 0 },
72+
{},
73+
};
74+
75+
MODULE_DEVICE_TABLE(i2c, ksz9477_i2c_id);
76+
77+
static const struct of_device_id ksz9477_dt_ids[] = {
78+
{ .compatible = "microchip,ksz9477" },
79+
{ .compatible = "microchip,ksz9897" },
80+
{},
81+
};
82+
MODULE_DEVICE_TABLE(of, ksz9477_dt_ids);
83+
84+
static struct i2c_driver ksz9477_i2c_driver = {
85+
.driver = {
86+
.name = "ksz9477-switch",
87+
.owner = THIS_MODULE,
88+
.of_match_table = of_match_ptr(ksz9477_dt_ids),
89+
},
90+
.probe = ksz9477_i2c_probe,
91+
.remove = ksz9477_i2c_remove,
92+
.shutdown = ksz9477_i2c_shutdown,
93+
.id_table = ksz9477_i2c_id,
94+
};
95+
96+
module_i2c_driver(ksz9477_i2c_driver);
97+
98+
MODULE_AUTHOR("Tristram Ha <[email protected]>");
99+
MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch I2C access Driver");
100+
MODULE_LICENSE("GPL v2");

drivers/net/dsa/microchip/ksz_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
294294
#define KSZ_SPI_OP_RD 3
295295
#define KSZ_SPI_OP_WR 2
296296

297+
#define swabnot_used(x) 0
298+
297299
#define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad) \
298300
swab##swp((opcode) << ((regbits) + (regpad)))
299301

0 commit comments

Comments
 (0)