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 regmap_config rc;
21 	struct ksz_device *dev;
22 	int i, ret;
23 
24 	dev = ksz_switch_alloc(&i2c->dev, i2c);
25 	if (!dev)
26 		return -ENOMEM;
27 
28 	for (i = 0; i < ARRAY_SIZE(ksz9477_regmap_config); i++) {
29 		rc = ksz9477_regmap_config[i];
30 		rc.lock_arg = &dev->regmap_mutex;
31 		dev->regmap[i] = devm_regmap_init_i2c(i2c, &rc);
32 		if (IS_ERR(dev->regmap[i])) {
33 			ret = PTR_ERR(dev->regmap[i]);
34 			dev_err(&i2c->dev,
35 				"Failed to initialize regmap%i: %d\n",
36 				ksz9477_regmap_config[i].val_bits, ret);
37 			return ret;
38 		}
39 	}
40 
41 	if (i2c->dev.platform_data)
42 		dev->pdata = i2c->dev.platform_data;
43 
44 	ret = ksz_switch_register(dev);
45 
46 	/* Main DSA driver may not be started yet. */
47 	if (ret)
48 		return ret;
49 
50 	i2c_set_clientdata(i2c, dev);
51 
52 	return 0;
53 }
54 
55 static int ksz9477_i2c_remove(struct i2c_client *i2c)
56 {
57 	struct ksz_device *dev = i2c_get_clientdata(i2c);
58 
59 	if (dev)
60 		ksz_switch_remove(dev);
61 
62 	return 0;
63 }
64 
65 static void ksz9477_i2c_shutdown(struct i2c_client *i2c)
66 {
67 	struct ksz_device *dev = i2c_get_clientdata(i2c);
68 
69 	if (!dev)
70 		return;
71 
72 	if (dev->dev_ops->reset)
73 		dev->dev_ops->reset(dev);
74 
75 	dsa_switch_shutdown(dev->ds);
76 
77 	i2c_set_clientdata(i2c, NULL);
78 }
79 
80 static const struct i2c_device_id ksz9477_i2c_id[] = {
81 	{ "ksz9477-switch", 0 },
82 	{},
83 };
84 
85 MODULE_DEVICE_TABLE(i2c, ksz9477_i2c_id);
86 
87 static const struct of_device_id ksz9477_dt_ids[] = {
88 	{
89 		.compatible = "microchip,ksz9477",
90 		.data = &ksz_switch_chips[KSZ9477]
91 	},
92 	{
93 		.compatible = "microchip,ksz9896",
94 		.data = &ksz_switch_chips[KSZ9896]
95 	},
96 	{
97 		.compatible = "microchip,ksz9897",
98 		.data = &ksz_switch_chips[KSZ9897]
99 	},
100 	{
101 		.compatible = "microchip,ksz9893",
102 		.data = &ksz_switch_chips[KSZ9893]
103 	},
104 	{
105 		.compatible = "microchip,ksz9563",
106 		.data = &ksz_switch_chips[KSZ9893]
107 	},
108 	{
109 		.compatible = "microchip,ksz8563",
110 		.data = &ksz_switch_chips[KSZ9893]
111 	},
112 	{
113 		.compatible = "microchip,ksz9567",
114 		.data = &ksz_switch_chips[KSZ9567]
115 	},
116 	{},
117 };
118 MODULE_DEVICE_TABLE(of, ksz9477_dt_ids);
119 
120 static struct i2c_driver ksz9477_i2c_driver = {
121 	.driver = {
122 		.name	= "ksz9477-switch",
123 		.of_match_table = of_match_ptr(ksz9477_dt_ids),
124 	},
125 	.probe	= ksz9477_i2c_probe,
126 	.remove	= ksz9477_i2c_remove,
127 	.shutdown = ksz9477_i2c_shutdown,
128 	.id_table = ksz9477_i2c_id,
129 };
130 
131 module_i2c_driver(ksz9477_i2c_driver);
132 
133 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
134 MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch I2C access Driver");
135 MODULE_LICENSE("GPL v2");
136