1 /*
2  * Copyright (c), 2023 Basalte bv
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_I2C_I2C_SC18IM704_H_
8 #define ZEPHYR_DRIVERS_I2C_I2C_SC18IM704_H_
9 
10 #include <stdint.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #define SC18IM704_CMD_STOP			0x50
17 #define SC18IM704_CMD_I2C_START			0x53
18 #define SC18IM704_CMD_READ_REG			0x52
19 #define SC18IM704_CMD_WRITE_REG			0x57
20 #define SC18IM704_CMD_READ_GPIO			0x49
21 #define SC18IM704_CMD_WRITE_GPIO		0x4f
22 #define SC18IM704_CMD_POWER_DOWN		0x5a
23 
24 #define SC18IM704_REG_BRG0			0x00
25 #define SC18IM704_REG_BRG1			0x01
26 #define SC18IM704_REG_GPIO_CONF1		0x02
27 #define SC18IM704_REG_GPIO_CONF2		0x03
28 #define SC18IM704_REG_GPIO_STATE		0x04
29 #define SC18IM704_REG_I2C_ADDR			0x06
30 #define SC18IM704_REG_I2C_CLK_L			0x07
31 #define SC18IM704_REG_I2C_CLK_H			0x08
32 #define SC18IM704_REG_I2C_TIMEOUT		0x09
33 #define SC18IM704_REG_I2C_STAT			0x0a
34 
35 #define SC18IM704_I2C_STAT_OK			0xf0
36 #define SC18IM704_I2C_STAT_NACK_ADDR		0xf1
37 #define SC18IM704_I2C_STAT_NACK_DATA		0xf2
38 #define SC18IM704_I2C_STAT_TIMEOUT		0xf8
39 
40 /**
41  * @brief Claim the SC18IM704 device.
42  *
43  * @warning After calling this routine, the device cannot be used by any other
44  * thread until the sc18im704_release routine is called.
45  *
46  * @param dev SC18IM704 device.
47  *
48  * @retval 0 Device claimed.
49  * @retval -EBUSY The device could not be claimed.
50  */
51 int sc18im704_claim(const struct device *dev);
52 
53 /**
54  * @brief Release the SC18IM704 device claim.
55  *
56  * @warning This routine must only be used after a sc18im704_claim.
57  *
58  * @param dev SC18IM704 device.
59  *
60  * @retval 0 Device claim to release.
61  * @retval -EPERM The current thread hasn't claimed the device.
62  * @retval -EINVAL The device has no active claims.
63  */
64 int sc18im704_release(const struct device *dev);
65 
66 /**
67  * @brief Exchange data with the SC18IM704 device.
68  *
69  * @param dev SC18IM704 device.
70  * @param tx_data The data buffer to write from.
71  * @param tx_len The length of the tx_data buffer.
72  * @param rx_data The data buffer to read to.
73  * @param rx_len The length of the rx_data buffer.
74  *
75  * @retval 0 If successful.
76  * @retval -EAGAIN The device did not respond in time (1 second timeout).
77  */
78 int sc18im704_transfer(const struct device *dev,
79 		       const uint8_t *tx_data, uint8_t tx_len,
80 		       uint8_t *rx_data, uint8_t rx_len);
81 
82 #ifdef __cplusplus
83 }
84 #endif
85 
86 #endif /* ZEPHYR_DRIVERS_I2C_I2C_SC18IM704_H_ */
87