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