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 	{ .compatible = "microchip,ksz9567" },
81 	{},
82 };
83 MODULE_DEVICE_TABLE(of, ksz9477_dt_ids);
84 
85 static struct i2c_driver ksz9477_i2c_driver = {
86 	.driver = {
87 		.name	= "ksz9477-switch",
88 		.owner	= THIS_MODULE,
89 		.of_match_table = of_match_ptr(ksz9477_dt_ids),
90 	},
91 	.probe	= ksz9477_i2c_probe,
92 	.remove	= ksz9477_i2c_remove,
93 	.shutdown = ksz9477_i2c_shutdown,
94 	.id_table = ksz9477_i2c_id,
95 };
96 
97 module_i2c_driver(ksz9477_i2c_driver);
98 
99 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
100 MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch I2C access Driver");
101 MODULE_LICENSE("GPL v2");
102