xref: /linux/drivers/i2c/busses/i2c-rtl9300.c (revision c34e9ab9a612ee8b18273398ef75c207b01f516d)
1*c366be72SChris Packham // SPDX-License-Identifier: GPL-2.0-only
2*c366be72SChris Packham 
3*c366be72SChris Packham #include <linux/bits.h>
4*c366be72SChris Packham #include <linux/i2c.h>
5*c366be72SChris Packham #include <linux/i2c-mux.h>
6*c366be72SChris Packham #include <linux/mod_devicetable.h>
7*c366be72SChris Packham #include <linux/mfd/syscon.h>
8*c366be72SChris Packham #include <linux/mutex.h>
9*c366be72SChris Packham #include <linux/platform_device.h>
10*c366be72SChris Packham #include <linux/regmap.h>
11*c366be72SChris Packham 
12*c366be72SChris Packham enum rtl9300_bus_freq {
13*c366be72SChris Packham 	RTL9300_I2C_STD_FREQ,
14*c366be72SChris Packham 	RTL9300_I2C_FAST_FREQ,
15*c366be72SChris Packham };
16*c366be72SChris Packham 
17*c366be72SChris Packham struct rtl9300_i2c;
18*c366be72SChris Packham 
19*c366be72SChris Packham struct rtl9300_i2c_chan {
20*c366be72SChris Packham 	struct i2c_adapter adap;
21*c366be72SChris Packham 	struct rtl9300_i2c *i2c;
22*c366be72SChris Packham 	enum rtl9300_bus_freq bus_freq;
23*c366be72SChris Packham 	u8 sda_pin;
24*c366be72SChris Packham };
25*c366be72SChris Packham 
26*c366be72SChris Packham #define RTL9300_I2C_MUX_NCHAN	8
27*c366be72SChris Packham 
28*c366be72SChris Packham struct rtl9300_i2c {
29*c366be72SChris Packham 	struct regmap *regmap;
30*c366be72SChris Packham 	struct device *dev;
31*c366be72SChris Packham 	struct rtl9300_i2c_chan chans[RTL9300_I2C_MUX_NCHAN];
32*c366be72SChris Packham 	u32 reg_base;
33*c366be72SChris Packham 	u8 sda_pin;
34*c366be72SChris Packham 	struct mutex lock;
35*c366be72SChris Packham };
36*c366be72SChris Packham 
37*c366be72SChris Packham #define RTL9300_I2C_MST_CTRL1				0x0
38*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL1_MEM_ADDR_OFS		8
39*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL1_MEM_ADDR_MASK		GENMASK(31, 8)
40*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_OFS		4
41*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_MASK		GENMASK(6, 4)
42*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL1_GPIO_SCL_SEL		BIT(3)
43*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL1_RWOP			BIT(2)
44*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL1_I2C_FAIL			BIT(1)
45*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL1_I2C_TRIG			BIT(0)
46*c366be72SChris Packham #define RTL9300_I2C_MST_CTRL2				0x4
47*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL2_RD_MODE			BIT(15)
48*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL2_DEV_ADDR_OFS		8
49*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL2_DEV_ADDR_MASK		GENMASK(14, 8)
50*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL2_DATA_WIDTH_OFS		4
51*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL2_DATA_WIDTH_MASK		GENMASK(7, 4)
52*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_OFS	2
53*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_MASK	GENMASK(3, 2)
54*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL2_SCL_FREQ_OFS		0
55*c366be72SChris Packham #define  RTL9300_I2C_MST_CTRL2_SCL_FREQ_MASK		GENMASK(1, 0)
56*c366be72SChris Packham #define RTL9300_I2C_MST_DATA_WORD0			0x8
57*c366be72SChris Packham #define RTL9300_I2C_MST_DATA_WORD1			0xc
58*c366be72SChris Packham #define RTL9300_I2C_MST_DATA_WORD2			0x10
59*c366be72SChris Packham #define RTL9300_I2C_MST_DATA_WORD3			0x14
60*c366be72SChris Packham 
61*c366be72SChris Packham #define RTL9300_I2C_MST_GLB_CTRL			0x384
62*c366be72SChris Packham 
63*c366be72SChris Packham static int rtl9300_i2c_reg_addr_set(struct rtl9300_i2c *i2c, u32 reg, u16 len)
64*c366be72SChris Packham {
65*c366be72SChris Packham 	u32 val, mask;
66*c366be72SChris Packham 	int ret;
67*c366be72SChris Packham 
68*c366be72SChris Packham 	val = len << RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_OFS;
69*c366be72SChris Packham 	mask = RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_MASK;
70*c366be72SChris Packham 
71*c366be72SChris Packham 	ret = regmap_update_bits(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL2, mask, val);
72*c366be72SChris Packham 	if (ret)
73*c366be72SChris Packham 		return ret;
74*c366be72SChris Packham 
75*c366be72SChris Packham 	val = reg << RTL9300_I2C_MST_CTRL1_MEM_ADDR_OFS;
76*c366be72SChris Packham 	mask = RTL9300_I2C_MST_CTRL1_MEM_ADDR_MASK;
77*c366be72SChris Packham 
78*c366be72SChris Packham 	return regmap_update_bits(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL1, mask, val);
79*c366be72SChris Packham }
80*c366be72SChris Packham 
81*c366be72SChris Packham static int rtl9300_i2c_config_io(struct rtl9300_i2c *i2c, u8 sda_pin)
82*c366be72SChris Packham {
83*c366be72SChris Packham 	int ret;
84*c366be72SChris Packham 	u32 val, mask;
85*c366be72SChris Packham 
86*c366be72SChris Packham 	ret = regmap_update_bits(i2c->regmap, RTL9300_I2C_MST_GLB_CTRL, BIT(sda_pin), BIT(sda_pin));
87*c366be72SChris Packham 	if (ret)
88*c366be72SChris Packham 		return ret;
89*c366be72SChris Packham 
90*c366be72SChris Packham 	val = (sda_pin << RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_OFS) |
91*c366be72SChris Packham 		RTL9300_I2C_MST_CTRL1_GPIO_SCL_SEL;
92*c366be72SChris Packham 	mask = RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_MASK | RTL9300_I2C_MST_CTRL1_GPIO_SCL_SEL;
93*c366be72SChris Packham 
94*c366be72SChris Packham 	return regmap_update_bits(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL1, mask, val);
95*c366be72SChris Packham }
96*c366be72SChris Packham 
97*c366be72SChris Packham static int rtl9300_i2c_config_xfer(struct rtl9300_i2c *i2c, struct rtl9300_i2c_chan *chan,
98*c366be72SChris Packham 				   u16 addr, u16 len)
99*c366be72SChris Packham {
100*c366be72SChris Packham 	u32 val, mask;
101*c366be72SChris Packham 
102*c366be72SChris Packham 	val = chan->bus_freq << RTL9300_I2C_MST_CTRL2_SCL_FREQ_OFS;
103*c366be72SChris Packham 	mask = RTL9300_I2C_MST_CTRL2_SCL_FREQ_MASK;
104*c366be72SChris Packham 
105*c366be72SChris Packham 	val |= addr << RTL9300_I2C_MST_CTRL2_DEV_ADDR_OFS;
106*c366be72SChris Packham 	mask |= RTL9300_I2C_MST_CTRL2_DEV_ADDR_MASK;
107*c366be72SChris Packham 
108*c366be72SChris Packham 	val |= ((len - 1) & 0xf) << RTL9300_I2C_MST_CTRL2_DATA_WIDTH_OFS;
109*c366be72SChris Packham 	mask |= RTL9300_I2C_MST_CTRL2_DATA_WIDTH_MASK;
110*c366be72SChris Packham 
111*c366be72SChris Packham 	mask |= RTL9300_I2C_MST_CTRL2_RD_MODE;
112*c366be72SChris Packham 
113*c366be72SChris Packham 	return regmap_update_bits(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL2, mask, val);
114*c366be72SChris Packham }
115*c366be72SChris Packham 
116*c366be72SChris Packham static int rtl9300_i2c_read(struct rtl9300_i2c *i2c, u8 *buf, int len)
117*c366be72SChris Packham {
118*c366be72SChris Packham 	u32 vals[4] = {};
119*c366be72SChris Packham 	int i, ret;
120*c366be72SChris Packham 
121*c366be72SChris Packham 	if (len > 16)
122*c366be72SChris Packham 		return -EIO;
123*c366be72SChris Packham 
124*c366be72SChris Packham 	ret = regmap_bulk_read(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_DATA_WORD0,
125*c366be72SChris Packham 			       vals, ARRAY_SIZE(vals));
126*c366be72SChris Packham 	if (ret)
127*c366be72SChris Packham 		return ret;
128*c366be72SChris Packham 
129*c366be72SChris Packham 	for (i = 0; i < len; i++) {
130*c366be72SChris Packham 		buf[i] = vals[i/4] & 0xff;
131*c366be72SChris Packham 		vals[i/4] >>= 8;
132*c366be72SChris Packham 	}
133*c366be72SChris Packham 
134*c366be72SChris Packham 	return 0;
135*c366be72SChris Packham }
136*c366be72SChris Packham 
137*c366be72SChris Packham static int rtl9300_i2c_write(struct rtl9300_i2c *i2c, u8 *buf, int len)
138*c366be72SChris Packham {
139*c366be72SChris Packham 	u32 vals[4] = {};
140*c366be72SChris Packham 	int i;
141*c366be72SChris Packham 
142*c366be72SChris Packham 	if (len > 16)
143*c366be72SChris Packham 		return -EIO;
144*c366be72SChris Packham 
145*c366be72SChris Packham 	for (i = 0; i < len; i++) {
146*c366be72SChris Packham 		if (i % 4 == 0)
147*c366be72SChris Packham 			vals[i/4] = 0;
148*c366be72SChris Packham 		vals[i/4] <<= 8;
149*c366be72SChris Packham 		vals[i/4] |= buf[i];
150*c366be72SChris Packham 	}
151*c366be72SChris Packham 
152*c366be72SChris Packham 	return regmap_bulk_write(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_DATA_WORD0,
153*c366be72SChris Packham 				vals, ARRAY_SIZE(vals));
154*c366be72SChris Packham }
155*c366be72SChris Packham 
156*c366be72SChris Packham static int rtl9300_i2c_writel(struct rtl9300_i2c *i2c, u32 data)
157*c366be72SChris Packham {
158*c366be72SChris Packham 	return regmap_write(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_DATA_WORD0, data);
159*c366be72SChris Packham }
160*c366be72SChris Packham 
161*c366be72SChris Packham static int rtl9300_i2c_execute_xfer(struct rtl9300_i2c *i2c, char read_write,
162*c366be72SChris Packham 				    int size, union i2c_smbus_data *data, int len)
163*c366be72SChris Packham {
164*c366be72SChris Packham 	u32 val, mask;
165*c366be72SChris Packham 	int ret;
166*c366be72SChris Packham 
167*c366be72SChris Packham 	val = read_write == I2C_SMBUS_WRITE ? RTL9300_I2C_MST_CTRL1_RWOP : 0;
168*c366be72SChris Packham 	mask = RTL9300_I2C_MST_CTRL1_RWOP;
169*c366be72SChris Packham 
170*c366be72SChris Packham 	val |= RTL9300_I2C_MST_CTRL1_I2C_TRIG;
171*c366be72SChris Packham 	mask |= RTL9300_I2C_MST_CTRL1_I2C_TRIG;
172*c366be72SChris Packham 
173*c366be72SChris Packham 	ret = regmap_update_bits(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL1, mask, val);
174*c366be72SChris Packham 	if (ret)
175*c366be72SChris Packham 		return ret;
176*c366be72SChris Packham 
177*c366be72SChris Packham 	ret = regmap_read_poll_timeout(i2c->regmap, i2c->reg_base + RTL9300_I2C_MST_CTRL1,
178*c366be72SChris Packham 				       val, !(val & RTL9300_I2C_MST_CTRL1_I2C_TRIG), 100, 2000);
179*c366be72SChris Packham 	if (ret)
180*c366be72SChris Packham 		return ret;
181*c366be72SChris Packham 
182*c366be72SChris Packham 	if (val & RTL9300_I2C_MST_CTRL1_I2C_FAIL)
183*c366be72SChris Packham 		return -EIO;
184*c366be72SChris Packham 
185*c366be72SChris Packham 	if (read_write == I2C_SMBUS_READ) {
186*c366be72SChris Packham 		if (size == I2C_SMBUS_BYTE || size == I2C_SMBUS_BYTE_DATA) {
187*c366be72SChris Packham 			ret = regmap_read(i2c->regmap,
188*c366be72SChris Packham 					  i2c->reg_base + RTL9300_I2C_MST_DATA_WORD0, &val);
189*c366be72SChris Packham 			if (ret)
190*c366be72SChris Packham 				return ret;
191*c366be72SChris Packham 			data->byte = val & 0xff;
192*c366be72SChris Packham 		} else if (size == I2C_SMBUS_WORD_DATA) {
193*c366be72SChris Packham 			ret = regmap_read(i2c->regmap,
194*c366be72SChris Packham 					  i2c->reg_base + RTL9300_I2C_MST_DATA_WORD0, &val);
195*c366be72SChris Packham 			if (ret)
196*c366be72SChris Packham 				return ret;
197*c366be72SChris Packham 			data->word = val & 0xffff;
198*c366be72SChris Packham 		} else {
199*c366be72SChris Packham 			ret = rtl9300_i2c_read(i2c, &data->block[0], len);
200*c366be72SChris Packham 			if (ret)
201*c366be72SChris Packham 				return ret;
202*c366be72SChris Packham 		}
203*c366be72SChris Packham 	}
204*c366be72SChris Packham 
205*c366be72SChris Packham 	return 0;
206*c366be72SChris Packham }
207*c366be72SChris Packham 
208*c366be72SChris Packham static int rtl9300_i2c_smbus_xfer(struct i2c_adapter *adap, u16 addr, unsigned short flags,
209*c366be72SChris Packham 				  char read_write, u8 command, int size,
210*c366be72SChris Packham 				  union i2c_smbus_data *data)
211*c366be72SChris Packham {
212*c366be72SChris Packham 	struct rtl9300_i2c_chan *chan = i2c_get_adapdata(adap);
213*c366be72SChris Packham 	struct rtl9300_i2c *i2c = chan->i2c;
214*c366be72SChris Packham 	int len = 0, ret;
215*c366be72SChris Packham 
216*c366be72SChris Packham 	mutex_lock(&i2c->lock);
217*c366be72SChris Packham 	if (chan->sda_pin != i2c->sda_pin) {
218*c366be72SChris Packham 		ret = rtl9300_i2c_config_io(i2c, chan->sda_pin);
219*c366be72SChris Packham 		if (ret)
220*c366be72SChris Packham 			goto out_unlock;
221*c366be72SChris Packham 		i2c->sda_pin = chan->sda_pin;
222*c366be72SChris Packham 	}
223*c366be72SChris Packham 
224*c366be72SChris Packham 	switch (size) {
225*c366be72SChris Packham 	case I2C_SMBUS_QUICK:
226*c366be72SChris Packham 		ret = rtl9300_i2c_config_xfer(i2c, chan, addr, 0);
227*c366be72SChris Packham 		if (ret)
228*c366be72SChris Packham 			goto out_unlock;
229*c366be72SChris Packham 		ret = rtl9300_i2c_reg_addr_set(i2c, 0, 0);
230*c366be72SChris Packham 		if (ret)
231*c366be72SChris Packham 			goto out_unlock;
232*c366be72SChris Packham 		break;
233*c366be72SChris Packham 
234*c366be72SChris Packham 	case I2C_SMBUS_BYTE:
235*c366be72SChris Packham 		if (read_write == I2C_SMBUS_WRITE) {
236*c366be72SChris Packham 			ret = rtl9300_i2c_config_xfer(i2c, chan, addr, 0);
237*c366be72SChris Packham 			if (ret)
238*c366be72SChris Packham 				goto out_unlock;
239*c366be72SChris Packham 			ret = rtl9300_i2c_reg_addr_set(i2c, command, 1);
240*c366be72SChris Packham 			if (ret)
241*c366be72SChris Packham 				goto out_unlock;
242*c366be72SChris Packham 		} else {
243*c366be72SChris Packham 			ret = rtl9300_i2c_config_xfer(i2c, chan, addr, 1);
244*c366be72SChris Packham 			if (ret)
245*c366be72SChris Packham 				goto out_unlock;
246*c366be72SChris Packham 			ret = rtl9300_i2c_reg_addr_set(i2c, 0, 0);
247*c366be72SChris Packham 			if (ret)
248*c366be72SChris Packham 				goto out_unlock;
249*c366be72SChris Packham 		}
250*c366be72SChris Packham 		break;
251*c366be72SChris Packham 
252*c366be72SChris Packham 	case I2C_SMBUS_BYTE_DATA:
253*c366be72SChris Packham 		ret = rtl9300_i2c_reg_addr_set(i2c, command, 1);
254*c366be72SChris Packham 		if (ret)
255*c366be72SChris Packham 			goto out_unlock;
256*c366be72SChris Packham 		ret = rtl9300_i2c_config_xfer(i2c, chan, addr, 1);
257*c366be72SChris Packham 		if (ret)
258*c366be72SChris Packham 			goto out_unlock;
259*c366be72SChris Packham 		if (read_write == I2C_SMBUS_WRITE) {
260*c366be72SChris Packham 			ret = rtl9300_i2c_writel(i2c, data->byte);
261*c366be72SChris Packham 			if (ret)
262*c366be72SChris Packham 				goto out_unlock;
263*c366be72SChris Packham 		}
264*c366be72SChris Packham 		break;
265*c366be72SChris Packham 
266*c366be72SChris Packham 	case I2C_SMBUS_WORD_DATA:
267*c366be72SChris Packham 		ret = rtl9300_i2c_reg_addr_set(i2c, command, 1);
268*c366be72SChris Packham 		if (ret)
269*c366be72SChris Packham 			goto out_unlock;
270*c366be72SChris Packham 		ret = rtl9300_i2c_config_xfer(i2c, chan, addr, 2);
271*c366be72SChris Packham 		if (ret)
272*c366be72SChris Packham 			goto out_unlock;
273*c366be72SChris Packham 		if (read_write == I2C_SMBUS_WRITE) {
274*c366be72SChris Packham 			ret = rtl9300_i2c_writel(i2c, data->word);
275*c366be72SChris Packham 			if (ret)
276*c366be72SChris Packham 				goto out_unlock;
277*c366be72SChris Packham 		}
278*c366be72SChris Packham 		break;
279*c366be72SChris Packham 
280*c366be72SChris Packham 	case I2C_SMBUS_BLOCK_DATA:
281*c366be72SChris Packham 		ret = rtl9300_i2c_reg_addr_set(i2c, command, 1);
282*c366be72SChris Packham 		if (ret)
283*c366be72SChris Packham 			goto out_unlock;
284*c366be72SChris Packham 		ret = rtl9300_i2c_config_xfer(i2c, chan, addr, data->block[0]);
285*c366be72SChris Packham 		if (ret)
286*c366be72SChris Packham 			goto out_unlock;
287*c366be72SChris Packham 		if (read_write == I2C_SMBUS_WRITE) {
288*c366be72SChris Packham 			ret = rtl9300_i2c_write(i2c, &data->block[1], data->block[0]);
289*c366be72SChris Packham 			if (ret)
290*c366be72SChris Packham 				goto out_unlock;
291*c366be72SChris Packham 		}
292*c366be72SChris Packham 		len = data->block[0];
293*c366be72SChris Packham 		break;
294*c366be72SChris Packham 
295*c366be72SChris Packham 	default:
296*c366be72SChris Packham 		dev_err(&adap->dev, "Unsupported transaction %d\n", size);
297*c366be72SChris Packham 		ret = -EOPNOTSUPP;
298*c366be72SChris Packham 		goto out_unlock;
299*c366be72SChris Packham 	}
300*c366be72SChris Packham 
301*c366be72SChris Packham 	ret = rtl9300_i2c_execute_xfer(i2c, read_write, size, data, len);
302*c366be72SChris Packham 
303*c366be72SChris Packham out_unlock:
304*c366be72SChris Packham 	mutex_unlock(&i2c->lock);
305*c366be72SChris Packham 
306*c366be72SChris Packham 	return ret;
307*c366be72SChris Packham }
308*c366be72SChris Packham 
309*c366be72SChris Packham static u32 rtl9300_i2c_func(struct i2c_adapter *a)
310*c366be72SChris Packham {
311*c366be72SChris Packham 	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
312*c366be72SChris Packham 	       I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
313*c366be72SChris Packham 	       I2C_FUNC_SMBUS_BLOCK_DATA;
314*c366be72SChris Packham }
315*c366be72SChris Packham 
316*c366be72SChris Packham static const struct i2c_algorithm rtl9300_i2c_algo = {
317*c366be72SChris Packham 	.smbus_xfer	= rtl9300_i2c_smbus_xfer,
318*c366be72SChris Packham 	.functionality	= rtl9300_i2c_func,
319*c366be72SChris Packham };
320*c366be72SChris Packham 
321*c366be72SChris Packham static struct i2c_adapter_quirks rtl9300_i2c_quirks = {
322*c366be72SChris Packham 	.flags		= I2C_AQ_NO_CLK_STRETCH,
323*c366be72SChris Packham 	.max_read_len	= 16,
324*c366be72SChris Packham 	.max_write_len	= 16,
325*c366be72SChris Packham };
326*c366be72SChris Packham 
327*c366be72SChris Packham static int rtl9300_i2c_probe(struct platform_device *pdev)
328*c366be72SChris Packham {
329*c366be72SChris Packham 	struct device *dev = &pdev->dev;
330*c366be72SChris Packham 	struct rtl9300_i2c *i2c;
331*c366be72SChris Packham 	u32 clock_freq, sda_pin;
332*c366be72SChris Packham 	int ret, i = 0;
333*c366be72SChris Packham 	struct fwnode_handle *child;
334*c366be72SChris Packham 
335*c366be72SChris Packham 	i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
336*c366be72SChris Packham 	if (!i2c)
337*c366be72SChris Packham 		return -ENOMEM;
338*c366be72SChris Packham 
339*c366be72SChris Packham 	i2c->regmap = syscon_node_to_regmap(dev->parent->of_node);
340*c366be72SChris Packham 	if (IS_ERR(i2c->regmap))
341*c366be72SChris Packham 		return PTR_ERR(i2c->regmap);
342*c366be72SChris Packham 	i2c->dev = dev;
343*c366be72SChris Packham 
344*c366be72SChris Packham 	mutex_init(&i2c->lock);
345*c366be72SChris Packham 
346*c366be72SChris Packham 	ret = device_property_read_u32(dev, "reg", &i2c->reg_base);
347*c366be72SChris Packham 	if (ret)
348*c366be72SChris Packham 		return ret;
349*c366be72SChris Packham 
350*c366be72SChris Packham 	platform_set_drvdata(pdev, i2c);
351*c366be72SChris Packham 
352*c366be72SChris Packham 	if (device_get_child_node_count(dev) >= RTL9300_I2C_MUX_NCHAN)
353*c366be72SChris Packham 		return dev_err_probe(dev, -EINVAL, "Too many channels\n");
354*c366be72SChris Packham 
355*c366be72SChris Packham 	device_for_each_child_node(dev, child) {
356*c366be72SChris Packham 		struct rtl9300_i2c_chan *chan = &i2c->chans[i];
357*c366be72SChris Packham 		struct i2c_adapter *adap = &chan->adap;
358*c366be72SChris Packham 
359*c366be72SChris Packham 		ret = fwnode_property_read_u32(child, "reg", &sda_pin);
360*c366be72SChris Packham 		if (ret)
361*c366be72SChris Packham 			return ret;
362*c366be72SChris Packham 
363*c366be72SChris Packham 		ret = fwnode_property_read_u32(child, "clock-frequency", &clock_freq);
364*c366be72SChris Packham 		if (ret)
365*c366be72SChris Packham 			clock_freq = I2C_MAX_STANDARD_MODE_FREQ;
366*c366be72SChris Packham 
367*c366be72SChris Packham 		switch (clock_freq) {
368*c366be72SChris Packham 		case I2C_MAX_STANDARD_MODE_FREQ:
369*c366be72SChris Packham 			chan->bus_freq = RTL9300_I2C_STD_FREQ;
370*c366be72SChris Packham 			break;
371*c366be72SChris Packham 
372*c366be72SChris Packham 		case I2C_MAX_FAST_MODE_FREQ:
373*c366be72SChris Packham 			chan->bus_freq = RTL9300_I2C_FAST_FREQ;
374*c366be72SChris Packham 			break;
375*c366be72SChris Packham 		default:
376*c366be72SChris Packham 			dev_warn(i2c->dev, "SDA%d clock-frequency %d not supported using default\n",
377*c366be72SChris Packham 				 sda_pin, clock_freq);
378*c366be72SChris Packham 			break;
379*c366be72SChris Packham 		}
380*c366be72SChris Packham 
381*c366be72SChris Packham 		chan->sda_pin = sda_pin;
382*c366be72SChris Packham 		chan->i2c = i2c;
383*c366be72SChris Packham 		adap = &i2c->chans[i].adap;
384*c366be72SChris Packham 		adap->owner = THIS_MODULE;
385*c366be72SChris Packham 		adap->algo = &rtl9300_i2c_algo;
386*c366be72SChris Packham 		adap->quirks = &rtl9300_i2c_quirks;
387*c366be72SChris Packham 		adap->retries = 3;
388*c366be72SChris Packham 		adap->dev.parent = dev;
389*c366be72SChris Packham 		i2c_set_adapdata(adap, chan);
390*c366be72SChris Packham 		adap->dev.of_node = to_of_node(child);
391*c366be72SChris Packham 		snprintf(adap->name, sizeof(adap->name), "%s SDA%d\n", dev_name(dev), sda_pin);
392*c366be72SChris Packham 		i++;
393*c366be72SChris Packham 
394*c366be72SChris Packham 		ret = devm_i2c_add_adapter(dev, adap);
395*c366be72SChris Packham 		if (ret)
396*c366be72SChris Packham 			return ret;
397*c366be72SChris Packham 	}
398*c366be72SChris Packham 	i2c->sda_pin = 0xff;
399*c366be72SChris Packham 
400*c366be72SChris Packham 	return 0;
401*c366be72SChris Packham }
402*c366be72SChris Packham 
403*c366be72SChris Packham static const struct of_device_id i2c_rtl9300_dt_ids[] = {
404*c366be72SChris Packham 	{ .compatible = "realtek,rtl9301-i2c" },
405*c366be72SChris Packham 	{ .compatible = "realtek,rtl9302b-i2c" },
406*c366be72SChris Packham 	{ .compatible = "realtek,rtl9302c-i2c" },
407*c366be72SChris Packham 	{ .compatible = "realtek,rtl9303-i2c" },
408*c366be72SChris Packham 	{}
409*c366be72SChris Packham };
410*c366be72SChris Packham MODULE_DEVICE_TABLE(of, i2c_rtl9300_dt_ids);
411*c366be72SChris Packham 
412*c366be72SChris Packham static struct platform_driver rtl9300_i2c_driver = {
413*c366be72SChris Packham 	.probe = rtl9300_i2c_probe,
414*c366be72SChris Packham 	.driver = {
415*c366be72SChris Packham 		.name = "i2c-rtl9300",
416*c366be72SChris Packham 		.of_match_table = i2c_rtl9300_dt_ids,
417*c366be72SChris Packham 	},
418*c366be72SChris Packham };
419*c366be72SChris Packham 
420*c366be72SChris Packham module_platform_driver(rtl9300_i2c_driver);
421*c366be72SChris Packham 
422*c366be72SChris Packham MODULE_DESCRIPTION("RTL9300 I2C controller driver");
423*c366be72SChris Packham MODULE_LICENSE("GPL");
424