1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) KEBA Industrial Automation Gmbh 2024
5 * Driver for KEBA I2C controller FPGA IP core
10 #include <linux/iopoll.h>
11 #include <linux/module.h>
12 #include <linux/misc/keba.h>
14 #define KI2C "i2c-keba"
16 #define KI2C_CAPABILITY_REG 0x02
17 #define KI2C_CAPABILITY_CRYPTO 0x01
18 #define KI2C_CAPABILITY_DC 0x02
20 #define KI2C_CONTROL_REG 0x04
21 #define KI2C_CONTROL_MEN 0x01
22 #define KI2C_CONTROL_MSTA 0x02
23 #define KI2C_CONTROL_RSTA 0x04
24 #define KI2C_CONTROL_MTX 0x08
25 #define KI2C_CONTROL_TXAK 0x10
26 #define KI2C_CONTROL_DISABLE 0x00
28 #define KI2C_CONTROL_DC_REG 0x05
29 #define KI2C_CONTROL_DC_SDA 0x01
30 #define KI2C_CONTROL_DC_SCL 0x02
32 #define KI2C_STATUS_REG 0x08
33 #define KI2C_STATUS_IN_USE 0x01
34 #define KI2C_STATUS_ACK_CYC 0x02
35 #define KI2C_STATUS_RXAK 0x04
36 #define KI2C_STATUS_MCF 0x08
38 #define KI2C_STATUS_DC_REG 0x09
39 #define KI2C_STATUS_DC_SDA 0x01
40 #define KI2C_STATUS_DC_SCL 0x02
42 #define KI2C_DATA_REG 0x0c
44 #define KI2C_INUSE_SLEEP_US (2 * USEC_PER_MSEC)
45 #define KI2C_INUSE_TIMEOUT_US (10 * USEC_PER_SEC)
47 #define KI2C_POLL_DELAY_US 5
50 struct keba_i2c_auxdev
*auxdev
;
52 struct i2c_adapter adapter
;
54 struct i2c_client
**client
;
58 static int ki2c_inuse_lock(struct ki2c
*ki2c
)
64 * The I2C controller has an IN_USE bit for locking access to the
65 * controller. This enables the use of I2C controller by other none
68 * If the I2C controller is free, then the first read returns
69 * IN_USE == 0. After that the I2C controller is locked and further
70 * reads of IN_USE return 1.
72 * The I2C controller is unlocked by writing 1 into IN_USE.
74 * The IN_USE bit acts as a hardware semaphore for the I2C controller.
75 * Poll for semaphore, but sleep while polling to free the CPU.
77 ret
= readb_poll_timeout(ki2c
->base
+ KI2C_STATUS_REG
,
78 sts
, (sts
& KI2C_STATUS_IN_USE
) == 0,
79 KI2C_INUSE_SLEEP_US
, KI2C_INUSE_TIMEOUT_US
);
81 dev_err(&ki2c
->auxdev
->auxdev
.dev
, "%s err!\n", __func__
);
86 static void ki2c_inuse_unlock(struct ki2c
*ki2c
)
88 /* unlock the controller by writing 1 into IN_USE */
89 iowrite8(KI2C_STATUS_IN_USE
, ki2c
->base
+ KI2C_STATUS_REG
);
92 static int ki2c_wait_for_bit(void __iomem
*addr
, u8 mask
, unsigned long timeout
)
96 return readb_poll_timeout(addr
, val
, (val
& mask
), KI2C_POLL_DELAY_US
,
97 jiffies_to_usecs(timeout
));
100 static int ki2c_wait_for_mcf(struct ki2c
*ki2c
)
102 return ki2c_wait_for_bit(ki2c
->base
+ KI2C_STATUS_REG
, KI2C_STATUS_MCF
,
103 ki2c
->adapter
.timeout
);
106 static int ki2c_wait_for_data(struct ki2c
*ki2c
)
110 ret
= ki2c_wait_for_mcf(ki2c
);
114 return ki2c_wait_for_bit(ki2c
->base
+ KI2C_STATUS_REG
,
116 ki2c
->adapter
.timeout
);
119 static int ki2c_wait_for_data_ack(struct ki2c
*ki2c
)
124 ret
= ki2c_wait_for_data(ki2c
);
128 /* RXAK == 0 means ACK reveived */
129 reg
= ioread8(ki2c
->base
+ KI2C_STATUS_REG
);
130 if (reg
& KI2C_STATUS_RXAK
)
136 static int ki2c_has_capability(struct ki2c
*ki2c
, unsigned int cap
)
138 unsigned int reg
= ioread8(ki2c
->base
+ KI2C_CAPABILITY_REG
);
140 return (reg
& cap
) != 0;
143 static int ki2c_get_scl(struct ki2c
*ki2c
)
145 unsigned int reg
= ioread8(ki2c
->base
+ KI2C_STATUS_DC_REG
);
147 /* capability KI2C_CAPABILITY_DC required */
148 return (reg
& KI2C_STATUS_DC_SCL
) != 0;
151 static int ki2c_get_sda(struct ki2c
*ki2c
)
153 unsigned int reg
= ioread8(ki2c
->base
+ KI2C_STATUS_DC_REG
);
155 /* capability KI2C_CAPABILITY_DC required */
156 return (reg
& KI2C_STATUS_DC_SDA
) != 0;
159 static void ki2c_set_scl(struct ki2c
*ki2c
, int val
)
163 /* capability KI2C_CAPABILITY_DC and KI2C_CONTROL_MEN = 0 reqired */
164 control_dc
= ioread8(ki2c
->base
+ KI2C_CONTROL_DC_REG
);
166 control_dc
|= KI2C_CONTROL_DC_SCL
;
168 control_dc
&= ~KI2C_CONTROL_DC_SCL
;
169 iowrite8(control_dc
, ki2c
->base
+ KI2C_CONTROL_DC_REG
);
173 * Resetting bus bitwise is done by checking SDA and applying clock cycles as
174 * long as SDA is low. 9 clock cycles are applied at most.
176 * Clock cycles are generated and udelay() determines the duration of clock
177 * cycles. Generated clock rate is 100 KHz and so duration of both clock levels
178 * is: delay in ns = (10^6 / 100) / 2
180 #define KI2C_RECOVERY_CLK_CNT (9 * 2)
181 #define KI2C_RECOVERY_UDELAY 5
182 static int ki2c_reset_bus_bitwise(struct ki2c
*ki2c
)
188 /* disable I2C controller (MEN = 0) to get direct access to SCL/SDA */
189 iowrite8(0, ki2c
->base
+ KI2C_CONTROL_REG
);
191 /* generate clock cycles */
192 ki2c_set_scl(ki2c
, val
);
193 udelay(KI2C_RECOVERY_UDELAY
);
194 for (i
= 0; i
< KI2C_RECOVERY_CLK_CNT
; i
++) {
196 /* SCL shouldn't be low here */
197 if (!ki2c_get_scl(ki2c
)) {
198 dev_err(&ki2c
->auxdev
->auxdev
.dev
,
199 "SCL is stuck low!\n");
204 /* break if SDA is high */
205 if (ki2c_get_sda(ki2c
))
210 ki2c_set_scl(ki2c
, val
);
211 udelay(KI2C_RECOVERY_UDELAY
);
214 if (!ki2c_get_sda(ki2c
)) {
215 dev_err(&ki2c
->auxdev
->auxdev
.dev
, "SDA is still low!\n");
219 /* reenable controller */
220 iowrite8(KI2C_CONTROL_MEN
, ki2c
->base
+ KI2C_CONTROL_REG
);
226 * Resetting bus bytewise is done by writing start bit, 9 data bits and stop
229 * This is not 100% safe. If target is an EEPROM and a write access was
230 * interrupted during the ACK cycle, this approach might not be able to recover
231 * the bus. The reason is, that after the 9 clock cycles the EEPROM will be in
232 * ACK cycle again and will hold SDA low like it did before the start of the
233 * routine. Furthermore the EEPROM might get written one additional byte with
234 * 0xff into it. Thus, use bitwise approach whenever possible, especially when
235 * EEPROMs are on the bus.
237 static int ki2c_reset_bus_bytewise(struct ki2c
*ki2c
)
241 /* hold data line high for 9 clock cycles */
242 iowrite8(0xFF, ki2c
->base
+ KI2C_DATA_REG
);
244 /* create start condition */
245 iowrite8(KI2C_CONTROL_MEN
| KI2C_CONTROL_MTX
| KI2C_CONTROL_MSTA
| KI2C_CONTROL_TXAK
,
246 ki2c
->base
+ KI2C_CONTROL_REG
);
247 ret
= ki2c_wait_for_mcf(ki2c
);
249 dev_err(&ki2c
->auxdev
->auxdev
.dev
, "Start condition failed\n");
254 /* create stop condition */
255 iowrite8(KI2C_CONTROL_MEN
| KI2C_CONTROL_MTX
| KI2C_CONTROL_TXAK
,
256 ki2c
->base
+ KI2C_CONTROL_REG
);
257 ret
= ki2c_wait_for_mcf(ki2c
);
259 dev_err(&ki2c
->auxdev
->auxdev
.dev
, "Stop condition failed\n");
264 static int ki2c_reset_bus(struct ki2c
*ki2c
)
268 ret
= ki2c_inuse_lock(ki2c
);
273 * If the I2C controller is capable of direct control of SCL/SDA, then a
274 * bitwise reset is used. Otherwise fall back to bytewise reset.
276 if (ki2c_has_capability(ki2c
, KI2C_CAPABILITY_DC
))
277 ret
= ki2c_reset_bus_bitwise(ki2c
);
279 ret
= ki2c_reset_bus_bytewise(ki2c
);
281 ki2c_inuse_unlock(ki2c
);
286 static void ki2c_write_target_addr(struct ki2c
*ki2c
, struct i2c_msg
*m
)
291 /* Bit 0 signals RD/WR */
292 if (m
->flags
& I2C_M_RD
)
295 iowrite8(addr
, ki2c
->base
+ KI2C_DATA_REG
);
298 static int ki2c_start_addr(struct ki2c
*ki2c
, struct i2c_msg
*m
)
303 * Store target address byte in the controller. This has to be done
304 * before sending START condition.
306 ki2c_write_target_addr(ki2c
, m
);
308 /* enable controller for TX */
309 iowrite8(KI2C_CONTROL_MEN
| KI2C_CONTROL_MTX
,
310 ki2c
->base
+ KI2C_CONTROL_REG
);
312 /* send START condition and target address byte */
313 iowrite8(KI2C_CONTROL_MEN
| KI2C_CONTROL_MTX
| KI2C_CONTROL_MSTA
,
314 ki2c
->base
+ KI2C_CONTROL_REG
);
316 ret
= ki2c_wait_for_data_ack(ki2c
);
319 * For EEPROMs this is normal behavior during internal write
322 dev_dbg(&ki2c
->auxdev
->auxdev
.dev
,
323 "%s wait for ACK err at 0x%02x!\n", __func__
, m
->addr
);
328 static int ki2c_repstart_addr(struct ki2c
*ki2c
, struct i2c_msg
*m
)
332 /* repeated start and write is not supported */
333 if ((m
->flags
& I2C_M_RD
) == 0) {
334 dev_err(&ki2c
->auxdev
->auxdev
.dev
,
335 "Repeated start not supported for writes\n");
339 /* send repeated start */
340 iowrite8(KI2C_CONTROL_MEN
| KI2C_CONTROL_MSTA
| KI2C_CONTROL_RSTA
,
341 ki2c
->base
+ KI2C_CONTROL_REG
);
343 ret
= ki2c_wait_for_mcf(ki2c
);
345 dev_err(&ki2c
->auxdev
->auxdev
.dev
,
346 "%s wait for MCF err at 0x%02x!\n", __func__
, m
->addr
);
350 /* write target-address byte */
351 ki2c_write_target_addr(ki2c
, m
);
353 ret
= ki2c_wait_for_data_ack(ki2c
);
355 dev_err(&ki2c
->auxdev
->auxdev
.dev
,
356 "%s wait for ACK err at 0x%02x!\n", __func__
, m
->addr
);
361 static void ki2c_stop(struct ki2c
*ki2c
)
363 iowrite8(KI2C_CONTROL_MEN
, ki2c
->base
+ KI2C_CONTROL_REG
);
364 ki2c_wait_for_mcf(ki2c
);
367 static int ki2c_write(struct ki2c
*ki2c
, const u8
*data
, int len
)
372 for (i
= 0; i
< len
; i
++) {
373 /* write data byte */
374 iowrite8(data
[i
], ki2c
->base
+ KI2C_DATA_REG
);
376 ret
= ki2c_wait_for_data_ack(ki2c
);
384 static int ki2c_read(struct ki2c
*ki2c
, u8
*data
, int len
)
391 return 0; /* nothing to do */
393 control
= KI2C_CONTROL_MEN
| KI2C_CONTROL_MSTA
;
395 /* if just one byte => send tx-nack after transfer */
397 control
|= KI2C_CONTROL_TXAK
;
399 iowrite8(control
, ki2c
->base
+ KI2C_CONTROL_REG
);
401 /* dummy read to start transfer on bus */
402 ioread8(ki2c
->base
+ KI2C_DATA_REG
);
404 for (i
= 0; i
< len
; i
++) {
405 ret
= ki2c_wait_for_data(ki2c
);
410 /* send tx-nack after transfer of last byte */
411 iowrite8(KI2C_CONTROL_MEN
| KI2C_CONTROL_MSTA
| KI2C_CONTROL_TXAK
,
412 ki2c
->base
+ KI2C_CONTROL_REG
);
413 else if (i
== len
- 1)
415 * switch to TX on last byte, so that reading DATA
416 * register does not trigger another read transfer
418 iowrite8(KI2C_CONTROL_MEN
| KI2C_CONTROL_MSTA
| KI2C_CONTROL_MTX
,
419 ki2c
->base
+ KI2C_CONTROL_REG
);
421 /* read byte and start next transfer (if not last byte) */
422 data
[i
] = ioread8(ki2c
->base
+ KI2C_DATA_REG
);
428 static int ki2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[], int num
)
430 struct ki2c
*ki2c
= i2c_get_adapdata(adap
);
434 ret
= ki2c_inuse_lock(ki2c
);
438 for (i
= 0; i
< num
; i
++) {
439 struct i2c_msg
*m
= &msgs
[i
];
442 ret
= ki2c_start_addr(ki2c
, m
);
444 ret
= ki2c_repstart_addr(ki2c
, m
);
448 if (m
->flags
& I2C_M_RD
)
449 ret
= ki2c_read(ki2c
, m
->buf
, m
->len
);
451 ret
= ki2c_write(ki2c
, m
->buf
, m
->len
);
458 ki2c_inuse_unlock(ki2c
);
460 return ret
< 0 ? ret
: num
;
463 static void ki2c_unregister_devices(struct ki2c
*ki2c
)
467 for (i
= 0; i
< ki2c
->client_size
; i
++) {
468 struct i2c_client
*client
= ki2c
->client
[i
];
471 i2c_unregister_device(client
);
475 static int ki2c_register_devices(struct ki2c
*ki2c
)
477 struct i2c_board_info
*info
= ki2c
->auxdev
->info
;
480 /* register all known I2C devices */
481 for (i
= 0; i
< ki2c
->client_size
; i
++) {
482 struct i2c_client
*client
;
483 unsigned short const addr_list
[2] = { info
[i
].addr
,
486 client
= i2c_new_scanned_device(&ki2c
->adapter
, &info
[i
],
488 if (!IS_ERR(client
)) {
489 ki2c
->client
[i
] = client
;
490 } else if (PTR_ERR(client
) != -ENODEV
) {
491 ki2c
->client_size
= i
;
492 ki2c_unregister_devices(ki2c
);
494 return PTR_ERR(client
);
501 static u32
ki2c_func(struct i2c_adapter
*adap
)
503 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
506 static const struct i2c_algorithm ki2c_algo
= {
507 .master_xfer
= ki2c_xfer
,
508 .functionality
= ki2c_func
,
511 static int ki2c_probe(struct auxiliary_device
*auxdev
,
512 const struct auxiliary_device_id
*id
)
514 struct device
*dev
= &auxdev
->dev
;
515 struct i2c_adapter
*adap
;
519 ki2c
= devm_kzalloc(dev
, sizeof(*ki2c
), GFP_KERNEL
);
522 ki2c
->auxdev
= container_of(auxdev
, struct keba_i2c_auxdev
, auxdev
);
523 ki2c
->client
= devm_kcalloc(dev
, ki2c
->auxdev
->info_size
,
524 sizeof(*ki2c
->client
), GFP_KERNEL
);
527 ki2c
->client_size
= ki2c
->auxdev
->info_size
;
528 auxiliary_set_drvdata(auxdev
, ki2c
);
530 ki2c
->base
= devm_ioremap_resource(dev
, &ki2c
->auxdev
->io
);
531 if (IS_ERR(ki2c
->base
))
532 return PTR_ERR(ki2c
->base
);
534 adap
= &ki2c
->adapter
;
535 strscpy(adap
->name
, "KEBA I2C adapter", sizeof(adap
->name
));
536 adap
->owner
= THIS_MODULE
;
537 adap
->class = I2C_CLASS_HWMON
;
538 adap
->algo
= &ki2c_algo
;
539 adap
->dev
.parent
= dev
;
541 i2c_set_adapdata(adap
, ki2c
);
543 /* enable controller */
544 iowrite8(KI2C_CONTROL_MEN
, ki2c
->base
+ KI2C_CONTROL_REG
);
546 /* reset bus before probing I2C devices */
547 ret
= ki2c_reset_bus(ki2c
);
551 ret
= devm_i2c_add_adapter(dev
, adap
);
553 dev_err(dev
, "Failed to add adapter (%d)!\n", ret
);
557 ret
= ki2c_register_devices(ki2c
);
559 dev_err(dev
, "Failed to register devices (%d)!\n", ret
);
566 iowrite8(KI2C_CONTROL_DISABLE
, ki2c
->base
+ KI2C_CONTROL_REG
);
570 static void ki2c_remove(struct auxiliary_device
*auxdev
)
572 struct ki2c
*ki2c
= auxiliary_get_drvdata(auxdev
);
574 ki2c_unregister_devices(ki2c
);
576 /* disable controller */
577 iowrite8(KI2C_CONTROL_DISABLE
, ki2c
->base
+ KI2C_CONTROL_REG
);
579 auxiliary_set_drvdata(auxdev
, NULL
);
582 static const struct auxiliary_device_id ki2c_devtype_aux
[] = {
583 { .name
= "keba.i2c" },
586 MODULE_DEVICE_TABLE(auxiliary
, ki2c_devtype_aux
);
588 static struct auxiliary_driver ki2c_driver_aux
= {
590 .id_table
= ki2c_devtype_aux
,
592 .remove
= ki2c_remove
,
594 module_auxiliary_driver(ki2c_driver_aux
);
596 MODULE_AUTHOR("Gerhard Engleder <eg@keba.com>");
597 MODULE_DESCRIPTION("KEBA I2C bus controller driver");
598 MODULE_LICENSE("GPL");