1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Congatec Board Controller I2C busses driver
5 * Copyright (C) 2024 Bootlin
6 * Author: Thomas Richard <thomas.richard@bootlin.com>
10 #include <linux/iopoll.h>
11 #include <linux/mfd/cgbc.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
15 #define CGBC_I2C_PRIMARY_BUS_ID 0
16 #define CGBC_I2C_PM_BUS_ID 4
18 #define CGBC_I2C_CMD_START 0x40
19 #define CGBC_I2C_CMD_STAT 0x48
20 #define CGBC_I2C_CMD_DATA 0x50
21 #define CGBC_I2C_CMD_SPEED 0x58
23 #define CGBC_I2C_STAT_IDL 0x00
24 #define CGBC_I2C_STAT_DAT 0x01
25 #define CGBC_I2C_STAT_BUSY 0x02
27 #define CGBC_I2C_START 0x80
28 #define CGBC_I2C_STOP 0x40
30 #define CGBC_I2C_LAST_ACK 0x80 /* send ACK on last read byte */
33 * Reference code defines 1kHz as min freq and 6.1MHz as max freq.
34 * But in practice, the board controller limits the frequency to 1MHz, and the
35 * 1kHz is not functional (minimal working freq is 50kHz).
36 * So use these values as limits.
38 #define CGBC_I2C_FREQ_MIN_HZ 50000 /* 50 kHz */
39 #define CGBC_I2C_FREQ_MAX_HZ 1000000 /* 1 MHz */
41 #define CGBC_I2C_FREQ_UNIT_1KHZ 0x40
42 #define CGBC_I2C_FREQ_UNIT_10KHZ 0x80
43 #define CGBC_I2C_FREQ_UNIT_100KHZ 0xC0
45 #define CGBC_I2C_FREQ_UNIT_MASK 0xC0
46 #define CGBC_I2C_FREQ_VALUE_MASK 0x3F
48 #define CGBC_I2C_READ_MAX_LEN 31
49 #define CGBC_I2C_WRITE_MAX_LEN 32
51 #define CGBC_I2C_CMD_HEADER_SIZE 4
52 #define CGBC_I2C_CMD_SIZE (CGBC_I2C_CMD_HEADER_SIZE + CGBC_I2C_WRITE_MAX_LEN)
55 CGBC_I2C_STATE_DONE
= 0,
63 struct i2c_algo_cgbc_data
{
65 unsigned long read_maxtime_us
;
68 struct cgbc_i2c_data
{
70 struct cgbc_device_data
*cgbc
;
71 struct i2c_adapter adap
;
75 enum cgbc_i2c_state state
;
78 struct cgbc_i2c_transfer
{
86 u8 data
[CGBC_I2C_WRITE_MAX_LEN
];
89 static u8
cgbc_i2c_freq_to_reg(unsigned int bus_frequency
)
93 if (bus_frequency
<= 10000)
94 reg
= CGBC_I2C_FREQ_UNIT_1KHZ
| (bus_frequency
/ 1000);
95 else if (bus_frequency
<= 100000)
96 reg
= CGBC_I2C_FREQ_UNIT_10KHZ
| (bus_frequency
/ 10000);
98 reg
= CGBC_I2C_FREQ_UNIT_100KHZ
| (bus_frequency
/ 100000);
103 static unsigned int cgbc_i2c_reg_to_freq(u8 reg
)
105 unsigned int freq
= reg
& CGBC_I2C_FREQ_VALUE_MASK
;
106 u8 unit
= reg
& CGBC_I2C_FREQ_UNIT_MASK
;
108 if (unit
== CGBC_I2C_FREQ_UNIT_100KHZ
)
109 return freq
* 100000;
110 else if (unit
== CGBC_I2C_FREQ_UNIT_10KHZ
)
116 static int cgbc_i2c_get_status(struct i2c_adapter
*adap
)
118 struct i2c_algo_cgbc_data
*algo_data
= adap
->algo_data
;
119 struct cgbc_i2c_data
*i2c
= i2c_get_adapdata(adap
);
120 struct cgbc_device_data
*cgbc
= i2c
->cgbc
;
121 u8 cmd
= CGBC_I2C_CMD_STAT
| algo_data
->bus_id
;
125 ret
= cgbc_command(cgbc
, &cmd
, sizeof(cmd
), NULL
, 0, &status
);
132 static int cgbc_i2c_set_frequency(struct i2c_adapter
*adap
,
133 unsigned int bus_frequency
)
135 struct i2c_algo_cgbc_data
*algo_data
= adap
->algo_data
;
136 struct cgbc_i2c_data
*i2c
= i2c_get_adapdata(adap
);
137 struct cgbc_device_data
*cgbc
= i2c
->cgbc
;
141 if (bus_frequency
> CGBC_I2C_FREQ_MAX_HZ
||
142 bus_frequency
< CGBC_I2C_FREQ_MIN_HZ
) {
143 dev_info(i2c
->dev
, "invalid frequency %u, using default\n", bus_frequency
);
144 bus_frequency
= I2C_MAX_STANDARD_MODE_FREQ
;
147 cmd
[0] = CGBC_I2C_CMD_SPEED
| algo_data
->bus_id
;
148 cmd
[1] = cgbc_i2c_freq_to_reg(bus_frequency
);
150 ret
= cgbc_command(cgbc
, &cmd
, sizeof(cmd
), &data
, 1, NULL
);
152 return dev_err_probe(i2c
->dev
, ret
,
153 "Failed to initialize I2C bus %s",
158 ret
= cgbc_command(cgbc
, &cmd
, sizeof(cmd
), &data
, 1, NULL
);
160 return dev_err_probe(i2c
->dev
, ret
,
161 "Failed to get I2C bus frequency");
163 bus_frequency
= cgbc_i2c_reg_to_freq(data
);
165 dev_dbg(i2c
->dev
, "%s is running at %d Hz\n", adap
->name
, bus_frequency
);
168 * The read_maxtime_us variable represents the maximum time to wait
169 * for data during a read operation. The maximum amount of data that
170 * can be read by a command is CGBC_I2C_READ_MAX_LEN.
171 * Therefore, calculate the max time to properly size the timeout.
173 algo_data
->read_maxtime_us
= (BITS_PER_BYTE
+ 1) * CGBC_I2C_READ_MAX_LEN
174 * USEC_PER_SEC
/ bus_frequency
;
179 static unsigned int cgbc_i2c_xfer_to_cmd(struct cgbc_i2c_transfer xfer
, u8
*cmd
)
183 cmd
[i
++] = CGBC_I2C_CMD_START
| xfer
.bus_id
;
185 cmd
[i
] = (xfer
.start
) ? CGBC_I2C_START
: 0x00;
187 cmd
[i
] |= CGBC_I2C_STOP
;
188 cmd
[i
++] |= (xfer
.start
) ? xfer
.write
+ 1 : xfer
.write
;
190 cmd
[i
++] = (xfer
.last_ack
) ? (xfer
.read
| CGBC_I2C_LAST_ACK
) : xfer
.read
;
193 cmd
[i
++] = xfer
.addr
;
196 memcpy(&cmd
[i
], &xfer
.data
, xfer
.write
);
198 return i
+ xfer
.write
;
201 static int cgbc_i2c_xfer_msg(struct i2c_adapter
*adap
)
203 struct i2c_algo_cgbc_data
*algo_data
= adap
->algo_data
;
204 struct cgbc_i2c_data
*i2c
= i2c_get_adapdata(adap
);
205 struct cgbc_device_data
*cgbc
= i2c
->cgbc
;
206 struct i2c_msg
*msg
= i2c
->msg
;
207 u8 cmd
[CGBC_I2C_CMD_SIZE
];
208 int ret
, max_len
, len
, i
;
209 unsigned int cmd_len
;
212 struct cgbc_i2c_transfer xfer
= {
213 .bus_id
= algo_data
->bus_id
,
214 .addr
= i2c_8bit_addr_from_msg(msg
),
217 if (i2c
->state
== CGBC_I2C_STATE_DONE
)
220 ret
= cgbc_i2c_get_status(adap
);
222 if (ret
== CGBC_I2C_STAT_BUSY
)
227 if (i2c
->state
== CGBC_I2C_STATE_INIT
||
228 (i2c
->state
== CGBC_I2C_STATE_WRITE
&& msg
->flags
& I2C_M_RD
))
231 i2c
->state
= (msg
->flags
& I2C_M_RD
) ? CGBC_I2C_STATE_READ
: CGBC_I2C_STATE_WRITE
;
233 max_len
= (i2c
->state
== CGBC_I2C_STATE_READ
) ?
234 CGBC_I2C_READ_MAX_LEN
: CGBC_I2C_WRITE_MAX_LEN
;
236 if (msg
->len
- i2c
->pos
> max_len
) {
239 len
= msg
->len
- i2c
->pos
;
245 if (i2c
->state
== CGBC_I2C_STATE_WRITE
) {
249 for (i
= 0; i
< len
; i
++)
250 xfer
.data
[i
] = msg
->buf
[i2c
->pos
+ i
];
252 cmd_len
= cgbc_i2c_xfer_to_cmd(xfer
, &cmd
[0]);
254 ret
= cgbc_command(cgbc
, &cmd
, cmd_len
, NULL
, 0, NULL
);
257 } else if (i2c
->state
== CGBC_I2C_STATE_READ
) {
261 if (i2c
->nmsgs
> 1 || msg
->len
- i2c
->pos
> max_len
)
262 xfer
.read
|= CGBC_I2C_LAST_ACK
;
264 cmd_len
= cgbc_i2c_xfer_to_cmd(xfer
, &cmd
[0]);
265 ret
= cgbc_command(cgbc
, &cmd
, cmd_len
, NULL
, 0, NULL
);
269 ret
= read_poll_timeout(cgbc_i2c_get_status
, ret
,
270 ret
!= CGBC_I2C_STAT_BUSY
, 0,
271 2 * algo_data
->read_maxtime_us
, false, adap
);
275 cmd_data
= CGBC_I2C_CMD_DATA
| algo_data
->bus_id
;
276 ret
= cgbc_command(cgbc
, &cmd_data
, sizeof(cmd_data
),
277 msg
->buf
+ i2c
->pos
, len
, NULL
);
282 if (len
== (msg
->len
- i2c
->pos
)) {
291 i2c
->state
= CGBC_I2C_STATE_DONE
;
296 i2c
->state
= CGBC_I2C_STATE_ERROR
;
300 static int cgbc_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
,
303 struct cgbc_i2c_data
*i2c
= i2c_get_adapdata(adap
);
304 unsigned long timeout
= jiffies
+ HZ
;
307 i2c
->state
= CGBC_I2C_STATE_INIT
;
312 while (time_before(jiffies
, timeout
)) {
313 ret
= cgbc_i2c_xfer_msg(adap
);
314 if (i2c
->state
== CGBC_I2C_STATE_DONE
)
317 if (i2c
->state
== CGBC_I2C_STATE_ERROR
)
321 timeout
= jiffies
+ HZ
;
324 i2c
->state
= CGBC_I2C_STATE_ERROR
;
328 static u32
cgbc_i2c_func(struct i2c_adapter
*adap
)
330 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~(I2C_FUNC_SMBUS_QUICK
));
333 static const struct i2c_algorithm cgbc_i2c_algorithm
= {
334 .master_xfer
= cgbc_i2c_xfer
,
335 .functionality
= cgbc_i2c_func
,
338 static struct i2c_algo_cgbc_data cgbc_i2c_algo_data
[] = {
339 { .bus_id
= CGBC_I2C_PRIMARY_BUS_ID
},
340 { .bus_id
= CGBC_I2C_PM_BUS_ID
},
343 static const struct i2c_adapter cgbc_i2c_adapter
[] = {
345 .owner
= THIS_MODULE
,
346 .name
= "Congatec General Purpose I2C adapter",
347 .class = I2C_CLASS_DEPRECATED
,
348 .algo
= &cgbc_i2c_algorithm
,
349 .algo_data
= &cgbc_i2c_algo_data
[0],
353 .owner
= THIS_MODULE
,
354 .name
= "Congatec Power Management I2C adapter",
355 .class = I2C_CLASS_DEPRECATED
,
356 .algo
= &cgbc_i2c_algorithm
,
357 .algo_data
= &cgbc_i2c_algo_data
[1],
362 static int cgbc_i2c_probe(struct platform_device
*pdev
)
364 struct cgbc_device_data
*cgbc
= dev_get_drvdata(pdev
->dev
.parent
);
365 struct cgbc_i2c_data
*i2c
;
368 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
373 i2c
->dev
= &pdev
->dev
;
374 i2c
->adap
= cgbc_i2c_adapter
[pdev
->id
];
375 i2c
->adap
.dev
.parent
= i2c
->dev
;
376 i2c_set_adapdata(&i2c
->adap
, i2c
);
377 platform_set_drvdata(pdev
, i2c
);
379 ret
= cgbc_i2c_set_frequency(&i2c
->adap
, I2C_MAX_STANDARD_MODE_FREQ
);
383 return i2c_add_numbered_adapter(&i2c
->adap
);
386 static void cgbc_i2c_remove(struct platform_device
*pdev
)
388 struct cgbc_i2c_data
*i2c
= platform_get_drvdata(pdev
);
390 i2c_del_adapter(&i2c
->adap
);
393 static struct platform_driver cgbc_i2c_driver
= {
397 .probe
= cgbc_i2c_probe
,
398 .remove
= cgbc_i2c_remove
,
401 module_platform_driver(cgbc_i2c_driver
);
403 MODULE_DESCRIPTION("Congatec Board Controller I2C Driver");
404 MODULE_AUTHOR("Thomas Richard <thomas.richard@bootlin.com>");
405 MODULE_LICENSE("GPL");
406 MODULE_ALIAS("platform:cgbc_i2c");