xref: /linux/drivers/net/dsa/microchip/ksz9477_i2c.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Microchip KSZ9477 series register access through I2C
4  *
5  * Copyright (C) 2018-2024 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 {
19 	const struct ksz_chip_data *chip;
20 	struct device *ddev = &i2c->dev;
21 	struct regmap_config rc;
22 	struct ksz_device *dev;
23 	int i, ret;
24 
25 	dev = ksz_switch_alloc(&i2c->dev, i2c);
26 	if (!dev)
27 		return -ENOMEM;
28 
29 	chip = device_get_match_data(ddev);
30 	if (!chip)
31 		return -EINVAL;
32 
33 	/* Save chip id to do special initialization when probing. */
34 	dev->chip_id = chip->chip_id;
35 	for (i = 0; i < __KSZ_NUM_REGMAPS; i++) {
36 		rc = ksz9477_regmap_config[i];
37 		rc.lock_arg = &dev->regmap_mutex;
38 		dev->regmap[i] = devm_regmap_init_i2c(i2c, &rc);
39 		if (IS_ERR(dev->regmap[i])) {
40 			return dev_err_probe(&i2c->dev, PTR_ERR(dev->regmap[i]),
41 					     "Failed to initialize regmap%i\n",
42 					     ksz9477_regmap_config[i].val_bits);
43 		}
44 	}
45 
46 	if (i2c->dev.platform_data)
47 		dev->pdata = i2c->dev.platform_data;
48 
49 	dev->irq = i2c->irq;
50 
51 	ret = ksz_switch_register(dev);
52 
53 	/* Main DSA driver may not be started yet. */
54 	if (ret)
55 		return ret;
56 
57 	i2c_set_clientdata(i2c, dev);
58 
59 	return 0;
60 }
61 
62 static void ksz9477_i2c_remove(struct i2c_client *i2c)
63 {
64 	struct ksz_device *dev = i2c_get_clientdata(i2c);
65 
66 	if (dev)
67 		ksz_switch_remove(dev);
68 }
69 
70 static void ksz9477_i2c_shutdown(struct i2c_client *i2c)
71 {
72 	struct ksz_device *dev = i2c_get_clientdata(i2c);
73 
74 	if (!dev)
75 		return;
76 
77 	ksz_switch_shutdown(dev);
78 
79 	i2c_set_clientdata(i2c, NULL);
80 }
81 
82 static const struct i2c_device_id ksz9477_i2c_id[] = {
83 	{ "ksz9477-switch" },
84 	{}
85 };
86 
87 MODULE_DEVICE_TABLE(i2c, ksz9477_i2c_id);
88 
89 static const struct of_device_id ksz9477_dt_ids[] = {
90 	{
91 		.compatible = "microchip,ksz9477",
92 		.data = &ksz_switch_chips[KSZ9477]
93 	},
94 	{
95 		.compatible = "microchip,ksz9896",
96 		.data = &ksz_switch_chips[KSZ9896]
97 	},
98 	{
99 		.compatible = "microchip,ksz9897",
100 		.data = &ksz_switch_chips[KSZ9897]
101 	},
102 	{
103 		.compatible = "microchip,ksz9893",
104 		.data = &ksz_switch_chips[KSZ9893]
105 	},
106 	{
107 		.compatible = "microchip,ksz9563",
108 		.data = &ksz_switch_chips[KSZ9563]
109 	},
110 	{
111 		.compatible = "microchip,ksz8563",
112 		.data = &ksz_switch_chips[KSZ8563]
113 	},
114 	{
115 		.compatible = "microchip,ksz8567",
116 		.data = &ksz_switch_chips[KSZ8567]
117 	},
118 	{
119 		.compatible = "microchip,ksz9567",
120 		.data = &ksz_switch_chips[KSZ9567]
121 	},
122 	{
123 		.compatible = "microchip,lan9646",
124 		.data = &ksz_switch_chips[LAN9646]
125 	},
126 	{},
127 };
128 MODULE_DEVICE_TABLE(of, ksz9477_dt_ids);
129 
130 static struct i2c_driver ksz9477_i2c_driver = {
131 	.driver = {
132 		.name	= "ksz9477-switch",
133 		.of_match_table = ksz9477_dt_ids,
134 	},
135 	.probe = ksz9477_i2c_probe,
136 	.remove	= ksz9477_i2c_remove,
137 	.shutdown = ksz9477_i2c_shutdown,
138 	.id_table = ksz9477_i2c_id,
139 };
140 
141 module_i2c_driver(ksz9477_i2c_driver);
142 
143 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
144 MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch I2C access Driver");
145 MODULE_LICENSE("GPL v2");
146