realtek: Replace C++ style comments
[openwrt/openwrt.git] / target / linux / realtek / files-5.15 / drivers / i2c / muxes / i2c-mux-rtl9300.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * I2C multiplexer for the 2 I2C Masters of the RTL9300
4 * with up to 8 channels each, but which are not entirely
5 * independent of each other
6 */
7 #include <linux/i2c-mux.h>
8 #include <linux/module.h>
9 #include <linux/mux/consumer.h>
10 #include <linux/of_device.h>
11 #include <linux/of_address.h>
12 #include <linux/platform_device.h>
13
14 #include "../busses/i2c-rtl9300.h"
15
16 #define NUM_MASTERS 2
17 #define NUM_BUSSES 8
18
19 #define REG(mst, x) (mux->base + x + (mst ? mux->i2c->mst2_offset : 0))
20 #define REG_MASK(mst, clear, set, reg) \
21 writel((readl(REG((mst),(reg))) & ~(clear)) | (set), REG((mst),(reg)))
22
23 struct channel {
24 u8 sda_num;
25 u8 scl_num;
26 };
27
28 static struct channel channels[NUM_MASTERS * NUM_BUSSES];
29
30 struct rtl9300_mux {
31 void __iomem *base;
32 struct device *dev;
33 struct i2c_adapter *parent;
34 struct rtl9300_i2c * i2c;
35 };
36
37 struct i2c_mux_data {
38 int scl0_pin;
39 int scl1_pin;
40 int sda0_pin;
41 int sda_pins;
42 int (*i2c_mux_select)(struct i2c_mux_core *muxc, u32 chan);
43 int (*i2c_mux_deselect)(struct i2c_mux_core *muxc, u32 chan);
44 void (*sda_sel)(struct i2c_mux_core *muxc, int pin);
45 };
46
47 static int rtl9300_i2c_mux_select(struct i2c_mux_core *muxc, u32 chan)
48 {
49 struct rtl9300_mux *mux = i2c_mux_priv(muxc);
50
51 /* Set SCL pin */
52 REG_MASK(channels[chan].scl_num, 0,
53 BIT(RTL9300_I2C_CTRL1_GPIO8_SCL_SEL), RTL9300_I2C_CTRL1);
54
55 /* Set SDA pin */
56 REG_MASK(channels[chan].scl_num, 0x7 << RTL9300_I2C_CTRL1_SDA_OUT_SEL,
57 channels[chan].sda_num << RTL9300_I2C_CTRL1_SDA_OUT_SEL, RTL9300_I2C_CTRL1);
58
59 mux->i2c->sda_num = channels[chan].sda_num;
60 mux->i2c->scl_num = channels[chan].scl_num;
61
62 return 0;
63 }
64
65 static int rtl9310_i2c_mux_select(struct i2c_mux_core *muxc, u32 chan)
66 {
67 struct rtl9300_mux *mux = i2c_mux_priv(muxc);
68
69 /* Set SCL pin */
70 REG_MASK(0, 0, BIT(RTL9310_I2C_MST_IF_SEL_GPIO_SCL_SEL + channels[chan].scl_num),
71 RTL9310_I2C_MST_IF_SEL);
72
73 /* Set SDA pin */
74 REG_MASK(channels[chan].scl_num, 0xf << RTL9310_I2C_CTRL_SDA_OUT_SEL,
75 channels[chan].sda_num << RTL9310_I2C_CTRL_SDA_OUT_SEL, RTL9310_I2C_CTRL);
76
77 mux->i2c->sda_num = channels[chan].sda_num;
78 mux->i2c->scl_num = channels[chan].scl_num;
79
80 return 0;
81 }
82
83 static int rtl9300_i2c_mux_deselect(struct i2c_mux_core *muxc, u32 chan)
84 {
85 return 0;
86 }
87
88 static void rtl9300_sda_sel(struct i2c_mux_core *muxc, int pin)
89 {
90 struct rtl9300_mux *mux = i2c_mux_priv(muxc);
91 u32 v;
92
93 /* Set SDA pin to I2C functionality */
94 v = readl(REG(0, RTL9300_I2C_MST_GLB_CTRL));
95 v |= BIT(pin);
96 writel(v, REG(0, RTL9300_I2C_MST_GLB_CTRL));
97 }
98
99 static void rtl9310_sda_sel(struct i2c_mux_core *muxc, int pin)
100 {
101 struct rtl9300_mux *mux = i2c_mux_priv(muxc);
102 u32 v;
103
104 /* Set SDA pin to I2C functionality */
105 v = readl(REG(0, RTL9310_I2C_MST_IF_SEL));
106 v |= BIT(pin);
107 writel(v, REG(0, RTL9310_I2C_MST_IF_SEL));
108 }
109
110 static struct device_node *mux_parent_adapter(struct device *dev, struct rtl9300_mux *mux)
111 {
112 struct device_node *node = dev->of_node;
113 struct device_node *parent_np;
114 struct i2c_adapter *parent;
115
116 parent_np = of_parse_phandle(node, "i2c-parent", 0);
117 if (!parent_np) {
118 dev_err(dev, "Cannot parse i2c-parent\n");
119 return ERR_PTR(-ENODEV);
120 }
121 parent = of_find_i2c_adapter_by_node(parent_np);
122 of_node_put(parent_np);
123 if (!parent)
124 return ERR_PTR(-EPROBE_DEFER);
125
126 if (!(of_device_is_compatible(parent_np, "realtek,rtl9300-i2c") ||
127 of_device_is_compatible(parent_np, "realtek,rtl9310-i2c"))){
128 dev_err(dev, "I2C parent not an RTL9300 I2C controller\n");
129 return ERR_PTR(-ENODEV);
130 }
131
132 mux->parent = parent;
133 mux->i2c = (struct rtl9300_i2c *)i2c_get_adapdata(parent);
134 mux->base = mux->i2c->base;
135
136 return parent_np;
137 }
138
139 struct i2c_mux_data rtl9300_i2c_mux_data = {
140 .scl0_pin = 8,
141 .scl1_pin = 17,
142 .sda0_pin = 9,
143 .sda_pins = 8,
144 .i2c_mux_select = rtl9300_i2c_mux_select,
145 .i2c_mux_deselect = rtl9300_i2c_mux_deselect,
146 .sda_sel = rtl9300_sda_sel,
147 };
148
149 struct i2c_mux_data rtl9310_i2c_mux_data = {
150 .scl0_pin = 13,
151 .scl1_pin = 14,
152 .sda0_pin = 0,
153 .sda_pins = 16,
154 .i2c_mux_select = rtl9310_i2c_mux_select,
155 .i2c_mux_deselect = rtl9300_i2c_mux_deselect,
156 .sda_sel = rtl9310_sda_sel,
157 };
158
159 static const struct of_device_id rtl9300_i2c_mux_of_match[] = {
160 { .compatible = "realtek,i2c-mux-rtl9300", .data = (void *) &rtl9300_i2c_mux_data},
161 { .compatible = "realtek,i2c-mux-rtl9310", .data = (void *) &rtl9310_i2c_mux_data},
162 {},
163 };
164
165 MODULE_DEVICE_TABLE(of, rtl9300_i2c_mux_of_match);
166
167 static int rtl9300_i2c_mux_probe(struct platform_device *pdev)
168 {
169 struct device *dev = &pdev->dev;
170 struct device_node *node = dev->of_node;
171 struct device_node *parent_np;
172 struct device_node *child;
173 struct i2c_mux_core *muxc;
174 struct rtl9300_mux *mux;
175 struct i2c_mux_data *mux_data;
176 int children;
177 int ret;
178
179 pr_info("%s probing I2C adapter\n", __func__);
180
181 if (!node) {
182 dev_err(dev, "No DT found\n");
183 return -EINVAL;
184 }
185
186 mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL);
187 if (!mux)
188 return -ENOMEM;
189
190 mux->dev = dev;
191
192 mux_data = (struct i2c_mux_data *) device_get_match_data(dev);
193
194 parent_np = mux_parent_adapter(dev, mux);
195 if (IS_ERR(parent_np))
196 return dev_err_probe(dev, PTR_ERR(parent_np), "i2c-parent adapter not found\n");
197
198 pr_info("%s base memory %08x\n", __func__, (u32)mux->base);
199
200 children = of_get_child_count(node);
201
202 muxc = i2c_mux_alloc(mux->parent, dev, children, 0, 0,
203 mux_data->i2c_mux_select, mux_data->i2c_mux_deselect);
204 if (!muxc) {
205 ret = -ENOMEM;
206 goto err_parent;
207 }
208 muxc->priv = mux;
209
210 platform_set_drvdata(pdev, muxc);
211
212 for_each_child_of_node(node, child) {
213 u32 chan;
214 u32 pin;
215
216 ret = of_property_read_u32(child, "reg", &chan);
217 if (ret < 0) {
218 dev_err(dev, "no reg property for node '%pOFn'\n",
219 child);
220 goto err_children;
221 }
222
223 if (chan >= NUM_MASTERS * NUM_BUSSES) {
224 dev_err(dev, "invalid reg %u\n", chan);
225 ret = -EINVAL;
226 goto err_children;
227 }
228
229 if (of_property_read_u32(child, "scl-pin", &pin)) {
230 dev_warn(dev, "SCL pin not found in DT, using default\n");
231 pin = mux_data->scl0_pin;
232 }
233 if (!(pin == mux_data->scl0_pin || pin == mux_data->scl1_pin)) {
234 dev_warn(dev, "SCL pin %d not supported\n", pin);
235 ret = -EINVAL;
236 goto err_children;
237 }
238 channels[chan].scl_num = pin == mux_data->scl0_pin ? 0 : 1;
239 pr_info("%s channel %d scl_num %d\n", __func__, chan, channels[chan].scl_num);
240
241 if (of_property_read_u32(child, "sda-pin", &pin)) {
242 dev_warn(dev, "SDA pin not found in DT, using default \n");
243 pin = mux_data->sda0_pin;
244 }
245 channels[chan].sda_num = pin - mux_data->sda0_pin;
246 if (channels[chan].sda_num < 0 || channels[chan].sda_num >= mux_data->sda_pins) {
247 dev_warn(dev, "SDA pin %d not supported\n", pin);
248 return -EINVAL;
249 }
250 pr_info("%s channel %d sda_num %d\n", __func__, chan, channels[chan].sda_num);
251
252 mux_data->sda_sel(muxc, channels[chan].sda_num);
253
254 ret = i2c_mux_add_adapter(muxc, 0, chan, 0);
255 if (ret)
256 goto err_children;
257 }
258
259 dev_info(dev, "%d-port mux on %s adapter\n", children, mux->parent->name);
260
261 return 0;
262
263 err_children:
264 i2c_mux_del_adapters(muxc);
265 err_parent:
266 i2c_put_adapter(mux->parent);
267
268 return ret;
269 }
270
271 static int rtl9300_i2c_mux_remove(struct platform_device *pdev)
272 {
273 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
274
275 i2c_mux_del_adapters(muxc);
276 i2c_put_adapter(muxc->parent);
277
278 return 0;
279 }
280
281 static struct platform_driver i2c_mux_driver = {
282 .probe = rtl9300_i2c_mux_probe,
283 .remove = rtl9300_i2c_mux_remove,
284 .driver = {
285 .name = "i2c-mux-rtl9300",
286 .of_match_table = rtl9300_i2c_mux_of_match,
287 },
288 };
289 module_platform_driver(i2c_mux_driver);
290
291 MODULE_DESCRIPTION("RTL9300 I2C multiplexer driver");
292 MODULE_AUTHOR("Birger Koblitz");
293 MODULE_LICENSE("GPL v2");