xref: /openbmc/linux/drivers/net/dsa/microchip/ksz9477_i2c.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
120e03777STristram Ha // SPDX-License-Identifier: GPL-2.0
220e03777STristram Ha /*
320e03777STristram Ha  * Microchip KSZ9477 series register access through I2C
420e03777STristram Ha  *
520e03777STristram Ha  * Copyright (C) 2018-2019 Microchip Technology Inc.
620e03777STristram Ha  */
720e03777STristram Ha 
820e03777STristram Ha #include <linux/i2c.h>
920e03777STristram Ha #include <linux/kernel.h>
1020e03777STristram Ha #include <linux/module.h>
1120e03777STristram Ha #include <linux/regmap.h>
1220e03777STristram Ha 
1320e03777STristram Ha #include "ksz_common.h"
1420e03777STristram Ha 
1520e03777STristram Ha KSZ_REGMAP_TABLE(ksz9477, not_used, 16, 0, 0);
1620e03777STristram Ha 
ksz9477_i2c_probe(struct i2c_client * i2c)17f925e215SUwe Kleine-König static int ksz9477_i2c_probe(struct i2c_client *i2c)
1820e03777STristram Ha {
19013572a2SMarek Vasut 	struct regmap_config rc;
2020e03777STristram Ha 	struct ksz_device *dev;
2120e03777STristram Ha 	int i, ret;
2220e03777STristram Ha 
2320e03777STristram Ha 	dev = ksz_switch_alloc(&i2c->dev, i2c);
2420e03777STristram Ha 	if (!dev)
2520e03777STristram Ha 		return -ENOMEM;
2620e03777STristram Ha 
2720e03777STristram Ha 	for (i = 0; i < __KSZ_NUM_REGMAPS; i++) {
28013572a2SMarek Vasut 		rc = ksz9477_regmap_config[i];
29013572a2SMarek Vasut 		rc.lock_arg = &dev->regmap_mutex;
30013572a2SMarek Vasut 		dev->regmap[i] = devm_regmap_init_i2c(i2c, &rc);
3120e03777STristram Ha 		if (IS_ERR(dev->regmap[i])) {
329b183317SRakesh Sankaranarayanan 			return dev_err_probe(&i2c->dev, PTR_ERR(dev->regmap[i]),
339b183317SRakesh Sankaranarayanan 					     "Failed to initialize regmap%i\n",
349b183317SRakesh Sankaranarayanan 					     ksz9477_regmap_config[i].val_bits);
3520e03777STristram Ha 		}
3620e03777STristram Ha 	}
3720e03777STristram Ha 
3820e03777STristram Ha 	if (i2c->dev.platform_data)
3920e03777STristram Ha 		dev->pdata = i2c->dev.platform_data;
4020e03777STristram Ha 
41a9c6db3bSRakesh Sankaranarayanan 	dev->irq = i2c->irq;
42a9c6db3bSRakesh Sankaranarayanan 
43ff3f3a30SArun Ramadoss 	ret = ksz_switch_register(dev);
4420e03777STristram Ha 
4520e03777STristram Ha 	/* Main DSA driver may not be started yet. */
4620e03777STristram Ha 	if (ret)
4720e03777STristram Ha 		return ret;
4820e03777STristram Ha 
4920e03777STristram Ha 	i2c_set_clientdata(i2c, dev);
5020e03777STristram Ha 
5120e03777STristram Ha 	return 0;
5220e03777STristram Ha }
5320e03777STristram Ha 
ksz9477_i2c_remove(struct i2c_client * i2c)54ed5c2f5fSUwe Kleine-König static void ksz9477_i2c_remove(struct i2c_client *i2c)
5520e03777STristram Ha {
5620e03777STristram Ha 	struct ksz_device *dev = i2c_get_clientdata(i2c);
5720e03777STristram Ha 
580650bf52SVladimir Oltean 	if (dev)
5920e03777STristram Ha 		ksz_switch_remove(dev);
6020e03777STristram Ha }
6120e03777STristram Ha 
ksz9477_i2c_shutdown(struct i2c_client * i2c)6220e03777STristram Ha static void ksz9477_i2c_shutdown(struct i2c_client *i2c)
6320e03777STristram Ha {
6420e03777STristram Ha 	struct ksz_device *dev = i2c_get_clientdata(i2c);
6520e03777STristram Ha 
660650bf52SVladimir Oltean 	if (!dev)
670650bf52SVladimir Oltean 		return;
680650bf52SVladimir Oltean 
69673b196fSArun Ramadoss 	if (dev->dev_ops->reset)
70673b196fSArun Ramadoss 		dev->dev_ops->reset(dev);
710650bf52SVladimir Oltean 
720650bf52SVladimir Oltean 	dsa_switch_shutdown(dev->ds);
730650bf52SVladimir Oltean 
740650bf52SVladimir Oltean 	i2c_set_clientdata(i2c, NULL);
7520e03777STristram Ha }
7620e03777STristram Ha 
7720e03777STristram Ha static const struct i2c_device_id ksz9477_i2c_id[] = {
7820e03777STristram Ha 	{ "ksz9477-switch", 0 },
7920e03777STristram Ha 	{},
8020e03777STristram Ha };
8120e03777STristram Ha 
8220e03777STristram Ha MODULE_DEVICE_TABLE(i2c, ksz9477_i2c_id);
8320e03777STristram Ha 
8420e03777STristram Ha static const struct of_device_id ksz9477_dt_ids[] = {
85eee16b14SArun Ramadoss 	{
86eee16b14SArun Ramadoss 		.compatible = "microchip,ksz9477",
87eee16b14SArun Ramadoss 		.data = &ksz_switch_chips[KSZ9477]
88eee16b14SArun Ramadoss 	},
89eee16b14SArun Ramadoss 	{
9013767525SRomain Naour 		.compatible = "microchip,ksz9896",
9113767525SRomain Naour 		.data = &ksz_switch_chips[KSZ9896]
9213767525SRomain Naour 	},
9313767525SRomain Naour 	{
94eee16b14SArun Ramadoss 		.compatible = "microchip,ksz9897",
95eee16b14SArun Ramadoss 		.data = &ksz_switch_chips[KSZ9897]
96eee16b14SArun Ramadoss 	},
97eee16b14SArun Ramadoss 	{
98eee16b14SArun Ramadoss 		.compatible = "microchip,ksz9893",
99eee16b14SArun Ramadoss 		.data = &ksz_switch_chips[KSZ9893]
100eee16b14SArun Ramadoss 	},
101eee16b14SArun Ramadoss 	{
102eee16b14SArun Ramadoss 		.compatible = "microchip,ksz9563",
103ef912fe4SRakesh Sankaranarayanan 		.data = &ksz_switch_chips[KSZ9563]
104eee16b14SArun Ramadoss 	},
105eee16b14SArun Ramadoss 	{
106eee16b14SArun Ramadoss 		.compatible = "microchip,ksz8563",
107360fdc99SAhmad Fatoum 		.data = &ksz_switch_chips[KSZ8563]
108eee16b14SArun Ramadoss 	},
109eee16b14SArun Ramadoss 	{
110eee16b14SArun Ramadoss 		.compatible = "microchip,ksz9567",
111eee16b14SArun Ramadoss 		.data = &ksz_switch_chips[KSZ9567]
112eee16b14SArun Ramadoss 	},
11320e03777STristram Ha 	{},
11420e03777STristram Ha };
11520e03777STristram Ha MODULE_DEVICE_TABLE(of, ksz9477_dt_ids);
11620e03777STristram Ha 
11720e03777STristram Ha static struct i2c_driver ksz9477_i2c_driver = {
11820e03777STristram Ha 	.driver = {
11920e03777STristram Ha 		.name	= "ksz9477-switch",
120*00923ff2SKrzysztof Kozlowski 		.of_match_table = ksz9477_dt_ids,
12120e03777STristram Ha 	},
122f925e215SUwe Kleine-König 	.probe = ksz9477_i2c_probe,
12320e03777STristram Ha 	.remove	= ksz9477_i2c_remove,
12420e03777STristram Ha 	.shutdown = ksz9477_i2c_shutdown,
12520e03777STristram Ha 	.id_table = ksz9477_i2c_id,
12620e03777STristram Ha };
12720e03777STristram Ha 
12820e03777STristram Ha module_i2c_driver(ksz9477_i2c_driver);
12920e03777STristram Ha 
13020e03777STristram Ha MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
13120e03777STristram Ha MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch I2C access Driver");
13220e03777STristram Ha MODULE_LICENSE("GPL v2");
133