1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/bits.h>
5 #include <linux/i2c-mux.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/mfd/syscon.h>
8 #include <linux/mutex.h>
9 #include <linux/platform_device.h>
10 #include <linux/regmap.h>
12 enum rtl9300_bus_freq
{
14 RTL9300_I2C_FAST_FREQ
,
19 struct rtl9300_i2c_chan
{
20 struct i2c_adapter adap
;
21 struct rtl9300_i2c
*i2c
;
22 enum rtl9300_bus_freq bus_freq
;
26 #define RTL9300_I2C_MUX_NCHAN 8
29 struct regmap
*regmap
;
31 struct rtl9300_i2c_chan chans
[RTL9300_I2C_MUX_NCHAN
];
37 #define RTL9300_I2C_MST_CTRL1 0x0
38 #define RTL9300_I2C_MST_CTRL1_MEM_ADDR_OFS 8
39 #define RTL9300_I2C_MST_CTRL1_MEM_ADDR_MASK GENMASK(31, 8)
40 #define RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_OFS 4
41 #define RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_MASK GENMASK(6, 4)
42 #define RTL9300_I2C_MST_CTRL1_GPIO_SCL_SEL BIT(3)
43 #define RTL9300_I2C_MST_CTRL1_RWOP BIT(2)
44 #define RTL9300_I2C_MST_CTRL1_I2C_FAIL BIT(1)
45 #define RTL9300_I2C_MST_CTRL1_I2C_TRIG BIT(0)
46 #define RTL9300_I2C_MST_CTRL2 0x4
47 #define RTL9300_I2C_MST_CTRL2_RD_MODE BIT(15)
48 #define RTL9300_I2C_MST_CTRL2_DEV_ADDR_OFS 8
49 #define RTL9300_I2C_MST_CTRL2_DEV_ADDR_MASK GENMASK(14, 8)
50 #define RTL9300_I2C_MST_CTRL2_DATA_WIDTH_OFS 4
51 #define RTL9300_I2C_MST_CTRL2_DATA_WIDTH_MASK GENMASK(7, 4)
52 #define RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_OFS 2
53 #define RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_MASK GENMASK(3, 2)
54 #define RTL9300_I2C_MST_CTRL2_SCL_FREQ_OFS 0
55 #define RTL9300_I2C_MST_CTRL2_SCL_FREQ_MASK GENMASK(1, 0)
56 #define RTL9300_I2C_MST_DATA_WORD0 0x8
57 #define RTL9300_I2C_MST_DATA_WORD1 0xc
58 #define RTL9300_I2C_MST_DATA_WORD2 0x10
59 #define RTL9300_I2C_MST_DATA_WORD3 0x14
61 #define RTL9300_I2C_MST_GLB_CTRL 0x384
63 static int rtl9300_i2c_reg_addr_set(struct rtl9300_i2c
*i2c
, u32 reg
, u16 len
)
68 val
= len
<< RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_OFS
;
69 mask
= RTL9300_I2C_MST_CTRL2_MEM_ADDR_WIDTH_MASK
;
71 ret
= regmap_update_bits(i2c
->regmap
, i2c
->reg_base
+ RTL9300_I2C_MST_CTRL2
, mask
, val
);
75 val
= reg
<< RTL9300_I2C_MST_CTRL1_MEM_ADDR_OFS
;
76 mask
= RTL9300_I2C_MST_CTRL1_MEM_ADDR_MASK
;
78 return regmap_update_bits(i2c
->regmap
, i2c
->reg_base
+ RTL9300_I2C_MST_CTRL1
, mask
, val
);
81 static int rtl9300_i2c_config_io(struct rtl9300_i2c
*i2c
, u8 sda_pin
)
86 ret
= regmap_update_bits(i2c
->regmap
, RTL9300_I2C_MST_GLB_CTRL
, BIT(sda_pin
), BIT(sda_pin
));
90 val
= (sda_pin
<< RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_OFS
) |
91 RTL9300_I2C_MST_CTRL1_GPIO_SCL_SEL
;
92 mask
= RTL9300_I2C_MST_CTRL1_SDA_OUT_SEL_MASK
| RTL9300_I2C_MST_CTRL1_GPIO_SCL_SEL
;
94 return regmap_update_bits(i2c
->regmap
, i2c
->reg_base
+ RTL9300_I2C_MST_CTRL1
, mask
, val
);
97 static int rtl9300_i2c_config_xfer(struct rtl9300_i2c
*i2c
, struct rtl9300_i2c_chan
*chan
,
102 val
= chan
->bus_freq
<< RTL9300_I2C_MST_CTRL2_SCL_FREQ_OFS
;
103 mask
= RTL9300_I2C_MST_CTRL2_SCL_FREQ_MASK
;
105 val
|= addr
<< RTL9300_I2C_MST_CTRL2_DEV_ADDR_OFS
;
106 mask
|= RTL9300_I2C_MST_CTRL2_DEV_ADDR_MASK
;
108 val
|= ((len
- 1) & 0xf) << RTL9300_I2C_MST_CTRL2_DATA_WIDTH_OFS
;
109 mask
|= RTL9300_I2C_MST_CTRL2_DATA_WIDTH_MASK
;
111 mask
|= RTL9300_I2C_MST_CTRL2_RD_MODE
;
113 return regmap_update_bits(i2c
->regmap
, i2c
->reg_base
+ RTL9300_I2C_MST_CTRL2
, mask
, val
);
116 static int rtl9300_i2c_read(struct rtl9300_i2c
*i2c
, u8
*buf
, int len
)
124 ret
= regmap_bulk_read(i2c
->regmap
, i2c
->reg_base
+ RTL9300_I2C_MST_DATA_WORD0
,
125 vals
, ARRAY_SIZE(vals
));
129 for (i
= 0; i
< len
; i
++) {
130 buf
[i
] = vals
[i
/4] & 0xff;
137 static int rtl9300_i2c_write(struct rtl9300_i2c
*i2c
, u8
*buf
, int len
)
145 for (i
= 0; i
< len
; i
++) {
152 return regmap_bulk_write(i2c
->regmap
, i2c
->reg_base
+ RTL9300_I2C_MST_DATA_WORD0
,
153 vals
, ARRAY_SIZE(vals
));
156 static int rtl9300_i2c_writel(struct rtl9300_i2c
*i2c
, u32 data
)
158 return regmap_write(i2c
->regmap
, i2c
->reg_base
+ RTL9300_I2C_MST_DATA_WORD0
, data
);
161 static int rtl9300_i2c_execute_xfer(struct rtl9300_i2c
*i2c
, char read_write
,
162 int size
, union i2c_smbus_data
*data
, int len
)
167 val
= read_write
== I2C_SMBUS_WRITE
? RTL9300_I2C_MST_CTRL1_RWOP
: 0;
168 mask
= RTL9300_I2C_MST_CTRL1_RWOP
;
170 val
|= RTL9300_I2C_MST_CTRL1_I2C_TRIG
;
171 mask
|= RTL9300_I2C_MST_CTRL1_I2C_TRIG
;
173 ret
= regmap_update_bits(i2c
->regmap
, i2c
->reg_base
+ RTL9300_I2C_MST_CTRL1
, mask
, val
);
177 ret
= regmap_read_poll_timeout(i2c
->regmap
, i2c
->reg_base
+ RTL9300_I2C_MST_CTRL1
,
178 val
, !(val
& RTL9300_I2C_MST_CTRL1_I2C_TRIG
), 100, 2000);
182 if (val
& RTL9300_I2C_MST_CTRL1_I2C_FAIL
)
185 if (read_write
== I2C_SMBUS_READ
) {
186 if (size
== I2C_SMBUS_BYTE
|| size
== I2C_SMBUS_BYTE_DATA
) {
187 ret
= regmap_read(i2c
->regmap
,
188 i2c
->reg_base
+ RTL9300_I2C_MST_DATA_WORD0
, &val
);
191 data
->byte
= val
& 0xff;
192 } else if (size
== I2C_SMBUS_WORD_DATA
) {
193 ret
= regmap_read(i2c
->regmap
,
194 i2c
->reg_base
+ RTL9300_I2C_MST_DATA_WORD0
, &val
);
197 data
->word
= val
& 0xffff;
199 ret
= rtl9300_i2c_read(i2c
, &data
->block
[0], len
);
208 static int rtl9300_i2c_smbus_xfer(struct i2c_adapter
*adap
, u16 addr
, unsigned short flags
,
209 char read_write
, u8 command
, int size
,
210 union i2c_smbus_data
*data
)
212 struct rtl9300_i2c_chan
*chan
= i2c_get_adapdata(adap
);
213 struct rtl9300_i2c
*i2c
= chan
->i2c
;
216 mutex_lock(&i2c
->lock
);
217 if (chan
->sda_pin
!= i2c
->sda_pin
) {
218 ret
= rtl9300_i2c_config_io(i2c
, chan
->sda_pin
);
221 i2c
->sda_pin
= chan
->sda_pin
;
225 case I2C_SMBUS_QUICK
:
226 ret
= rtl9300_i2c_config_xfer(i2c
, chan
, addr
, 0);
229 ret
= rtl9300_i2c_reg_addr_set(i2c
, 0, 0);
235 if (read_write
== I2C_SMBUS_WRITE
) {
236 ret
= rtl9300_i2c_config_xfer(i2c
, chan
, addr
, 0);
239 ret
= rtl9300_i2c_reg_addr_set(i2c
, command
, 1);
243 ret
= rtl9300_i2c_config_xfer(i2c
, chan
, addr
, 1);
246 ret
= rtl9300_i2c_reg_addr_set(i2c
, 0, 0);
252 case I2C_SMBUS_BYTE_DATA
:
253 ret
= rtl9300_i2c_reg_addr_set(i2c
, command
, 1);
256 ret
= rtl9300_i2c_config_xfer(i2c
, chan
, addr
, 1);
259 if (read_write
== I2C_SMBUS_WRITE
) {
260 ret
= rtl9300_i2c_writel(i2c
, data
->byte
);
266 case I2C_SMBUS_WORD_DATA
:
267 ret
= rtl9300_i2c_reg_addr_set(i2c
, command
, 1);
270 ret
= rtl9300_i2c_config_xfer(i2c
, chan
, addr
, 2);
273 if (read_write
== I2C_SMBUS_WRITE
) {
274 ret
= rtl9300_i2c_writel(i2c
, data
->word
);
280 case I2C_SMBUS_BLOCK_DATA
:
281 ret
= rtl9300_i2c_reg_addr_set(i2c
, command
, 1);
284 ret
= rtl9300_i2c_config_xfer(i2c
, chan
, addr
, data
->block
[0]);
287 if (read_write
== I2C_SMBUS_WRITE
) {
288 ret
= rtl9300_i2c_write(i2c
, &data
->block
[1], data
->block
[0]);
292 len
= data
->block
[0];
296 dev_err(&adap
->dev
, "Unsupported transaction %d\n", size
);
301 ret
= rtl9300_i2c_execute_xfer(i2c
, read_write
, size
, data
, len
);
304 mutex_unlock(&i2c
->lock
);
309 static u32
rtl9300_i2c_func(struct i2c_adapter
*a
)
311 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
312 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
313 I2C_FUNC_SMBUS_BLOCK_DATA
;
316 static const struct i2c_algorithm rtl9300_i2c_algo
= {
317 .smbus_xfer
= rtl9300_i2c_smbus_xfer
,
318 .functionality
= rtl9300_i2c_func
,
321 static struct i2c_adapter_quirks rtl9300_i2c_quirks
= {
322 .flags
= I2C_AQ_NO_CLK_STRETCH
,
327 static int rtl9300_i2c_probe(struct platform_device
*pdev
)
329 struct device
*dev
= &pdev
->dev
;
330 struct rtl9300_i2c
*i2c
;
331 u32 clock_freq
, sda_pin
;
333 struct fwnode_handle
*child
;
335 i2c
= devm_kzalloc(dev
, sizeof(*i2c
), GFP_KERNEL
);
339 i2c
->regmap
= syscon_node_to_regmap(dev
->parent
->of_node
);
340 if (IS_ERR(i2c
->regmap
))
341 return PTR_ERR(i2c
->regmap
);
344 mutex_init(&i2c
->lock
);
346 ret
= device_property_read_u32(dev
, "reg", &i2c
->reg_base
);
350 platform_set_drvdata(pdev
, i2c
);
352 if (device_get_child_node_count(dev
) >= RTL9300_I2C_MUX_NCHAN
)
353 return dev_err_probe(dev
, -EINVAL
, "Too many channels\n");
355 device_for_each_child_node(dev
, child
) {
356 struct rtl9300_i2c_chan
*chan
= &i2c
->chans
[i
];
357 struct i2c_adapter
*adap
= &chan
->adap
;
359 ret
= fwnode_property_read_u32(child
, "reg", &sda_pin
);
363 ret
= fwnode_property_read_u32(child
, "clock-frequency", &clock_freq
);
365 clock_freq
= I2C_MAX_STANDARD_MODE_FREQ
;
367 switch (clock_freq
) {
368 case I2C_MAX_STANDARD_MODE_FREQ
:
369 chan
->bus_freq
= RTL9300_I2C_STD_FREQ
;
372 case I2C_MAX_FAST_MODE_FREQ
:
373 chan
->bus_freq
= RTL9300_I2C_FAST_FREQ
;
376 dev_warn(i2c
->dev
, "SDA%d clock-frequency %d not supported using default\n",
377 sda_pin
, clock_freq
);
381 chan
->sda_pin
= sda_pin
;
383 adap
= &i2c
->chans
[i
].adap
;
384 adap
->owner
= THIS_MODULE
;
385 adap
->algo
= &rtl9300_i2c_algo
;
386 adap
->quirks
= &rtl9300_i2c_quirks
;
388 adap
->dev
.parent
= dev
;
389 i2c_set_adapdata(adap
, chan
);
390 adap
->dev
.of_node
= to_of_node(child
);
391 snprintf(adap
->name
, sizeof(adap
->name
), "%s SDA%d\n", dev_name(dev
), sda_pin
);
394 ret
= devm_i2c_add_adapter(dev
, adap
);
403 static const struct of_device_id i2c_rtl9300_dt_ids
[] = {
404 { .compatible
= "realtek,rtl9301-i2c" },
405 { .compatible
= "realtek,rtl9302b-i2c" },
406 { .compatible
= "realtek,rtl9302c-i2c" },
407 { .compatible
= "realtek,rtl9303-i2c" },
410 MODULE_DEVICE_TABLE(of
, i2c_rtl9300_dt_ids
);
412 static struct platform_driver rtl9300_i2c_driver
= {
413 .probe
= rtl9300_i2c_probe
,
415 .name
= "i2c-rtl9300",
416 .of_match_table
= i2c_rtl9300_dt_ids
,
420 module_platform_driver(rtl9300_i2c_driver
);
422 MODULE_DESCRIPTION("RTL9300 I2C controller driver");
423 MODULE_LICENSE("GPL");