1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 NovaTech LLC
4 * George McCollister <george.mccollister@gmail.com>
7 #include <linux/bits.h>
9 #include <linux/module.h>
11 #include "xrs700x_reg.h"
13 struct xrs700x_i2c_cmd {
18 static int xrs700x_i2c_reg_read(void *context, unsigned int reg,
21 struct device *dev = context;
22 struct i2c_client *i2c = to_i2c_client(dev);
23 struct xrs700x_i2c_cmd cmd;
26 cmd.reg = cpu_to_be32(reg | 1);
28 ret = i2c_master_send(i2c, (char *)&cmd.reg, sizeof(cmd.reg));
30 dev_err(dev, "xrs i2c_master_send returned %d\n", ret);
34 ret = i2c_master_recv(i2c, (char *)&cmd.val, sizeof(cmd.val));
36 dev_err(dev, "xrs i2c_master_recv returned %d\n", ret);
40 *val = be16_to_cpu(cmd.val);
44 static int xrs700x_i2c_reg_write(void *context, unsigned int reg,
47 struct device *dev = context;
48 struct i2c_client *i2c = to_i2c_client(dev);
49 struct xrs700x_i2c_cmd cmd;
52 cmd.reg = cpu_to_be32(reg);
53 cmd.val = cpu_to_be16(val);
55 ret = i2c_master_send(i2c, (char *)&cmd, sizeof(cmd));
57 dev_err(dev, "xrs i2c_master_send returned %d\n", ret);
64 static const struct regmap_config xrs700x_i2c_regmap_config = {
71 .reg_read = xrs700x_i2c_reg_read,
72 .reg_write = xrs700x_i2c_reg_write,
74 .cache_type = REGCACHE_NONE,
75 .reg_format_endian = REGMAP_ENDIAN_BIG,
76 .val_format_endian = REGMAP_ENDIAN_BIG
79 static int xrs700x_i2c_probe(struct i2c_client *i2c,
80 const struct i2c_device_id *i2c_id)
85 priv = xrs700x_switch_alloc(&i2c->dev, i2c);
89 priv->regmap = devm_regmap_init(&i2c->dev, NULL, &i2c->dev,
90 &xrs700x_i2c_regmap_config);
91 if (IS_ERR(priv->regmap)) {
92 ret = PTR_ERR(priv->regmap);
93 dev_err(&i2c->dev, "Failed to initialize regmap: %d\n", ret);
97 i2c_set_clientdata(i2c, priv);
99 ret = xrs700x_switch_register(priv);
101 /* Main DSA driver may not be started yet. */
108 static int xrs700x_i2c_remove(struct i2c_client *i2c)
110 struct xrs700x *priv = i2c_get_clientdata(i2c);
115 xrs700x_switch_remove(priv);
117 i2c_set_clientdata(i2c, NULL);
122 static void xrs700x_i2c_shutdown(struct i2c_client *i2c)
124 struct xrs700x *priv = i2c_get_clientdata(i2c);
129 xrs700x_switch_shutdown(priv);
131 i2c_set_clientdata(i2c, NULL);
134 static const struct i2c_device_id xrs700x_i2c_id[] = {
135 { "xrs700x-switch", 0 },
139 MODULE_DEVICE_TABLE(i2c, xrs700x_i2c_id);
141 static const struct of_device_id __maybe_unused xrs700x_i2c_dt_ids[] = {
142 { .compatible = "arrow,xrs7003e", .data = &xrs7003e_info },
143 { .compatible = "arrow,xrs7003f", .data = &xrs7003f_info },
144 { .compatible = "arrow,xrs7004e", .data = &xrs7004e_info },
145 { .compatible = "arrow,xrs7004f", .data = &xrs7004f_info },
148 MODULE_DEVICE_TABLE(of, xrs700x_i2c_dt_ids);
150 static struct i2c_driver xrs700x_i2c_driver = {
152 .name = "xrs700x-i2c",
153 .of_match_table = of_match_ptr(xrs700x_i2c_dt_ids),
155 .probe = xrs700x_i2c_probe,
156 .remove = xrs700x_i2c_remove,
157 .shutdown = xrs700x_i2c_shutdown,
158 .id_table = xrs700x_i2c_id,
161 module_i2c_driver(xrs700x_i2c_driver);
163 MODULE_AUTHOR("George McCollister <george.mccollister@gmail.com>");
164 MODULE_DESCRIPTION("Arrow SpeedChips XRS700x DSA I2C driver");
165 MODULE_LICENSE("GPL v2");