1 /*
2  * Copyright 2024 Google LLC
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 /**
7  * @file
8  * @brief Helper functions to use by the TCPCI-compliant drivers
9  *
10  * This file contains generic TCPCI functions that may be used by the drivers to TCPCI-compliant
11  * devices that want to implement vendor-specific functionality without the need to reimplement the
12  * TCPCI generic functionality and register operations.
13  */
14 
15 #ifndef ZEPHYR_INCLUDE_DRIVERS_USBC_TCPCI_PRIV_H_
16 #define ZEPHYR_INCLUDE_DRIVERS_USBC_TCPCI_PRIV_H_
17 
18 #include <stdint.h>
19 #include <zephyr/drivers/i2c.h>
20 #include <zephyr/usb_c/usbc.h>
21 
22 /**
23  * @brief Structure used to bind the register address to name in registers dump
24  */
25 struct tcpci_reg_dump_map {
26 	/** Address of I2C device register */
27 	uint8_t addr;
28 	/** Human readable name of register */
29 	const char *name;
30 	/** Size in bytes of the register */
31 	uint8_t size;
32 };
33 
34 /** Size of the array containing the standard registers used by tcpci dump command */
35 #define TCPCI_STD_REGS_SIZE 38
36 /**
37  * @brief Array containing the standard TCPCI registers list.
38  * If the TCPC driver contain any vendor-specific registers, it may override the TCPCI dump_std_reg
39  * function tp dump them and should also dump the standard registers using this array.
40  *
41  */
42 extern const struct tcpci_reg_dump_map tcpci_std_regs[TCPCI_STD_REGS_SIZE];
43 
44 /**
45  * @brief Function to read the 8-bit register of TCPCI device
46  *
47  * @param bus I2C bus
48  * @param reg Address of TCPCI register
49  * @param value Pointer to variable that will store the register value
50  * @return int Status of I2C operation, 0 in case of success
51  */
52 int tcpci_read_reg8(const struct i2c_dt_spec *bus, uint8_t reg, uint8_t *value);
53 
54 /**
55  * @brief Function to write a value to the 8-bit register of TCPCI device
56  *
57  * @param bus I2C bus
58  * @param reg Address of TCPCI register
59  * @param value Value that will be written to the device register
60  * @return int Status of I2C operation, 0 in case of success
61  */
62 int tcpci_write_reg8(const struct i2c_dt_spec *bus, uint8_t reg, uint8_t value);
63 
64 /**
65  * @brief Function to read and update part of the 8-bit register of TCPCI device
66  * The function is NOT performing this operation atomically.
67  *
68  * @param bus I2C bus
69  * @param reg Address of TCPCI register
70  * @param mask Bitmask specifying which bits of the device register will be modified
71  * @param value Value that will be written to the device register after being ANDed with mask
72  * @return int Status of I2C operation, 0 in case of success
73  */
74 int tcpci_update_reg8(const struct i2c_dt_spec *bus, uint8_t reg, uint8_t mask, uint8_t value);
75 
76 /**
77  * @brief Function to read the 16-bit register of TCPCI device
78  *
79  * @param bus I2C bus
80  * @param reg Address of TCPCI register
81  * @param value Pointer to variable that will store the register value
82  * @return int Status of I2C operation, 0 in case of success
83  */
84 int tcpci_read_reg16(const struct i2c_dt_spec *bus, uint8_t reg, uint16_t *value);
85 
86 /**
87  * @brief Function to write a value to the 16-bit register of TCPCI device
88  *
89  * @param bus I2C bus
90  * @param reg Address of TCPCI register
91  * @param value Value that will be written to the device register
92  * @return int Status of I2C operation, 0 in case of success
93  */
94 int tcpci_write_reg16(const struct i2c_dt_spec *bus, uint8_t reg, uint16_t value);
95 
96 /**
97  * @brief Function that converts the TCPCI alert register to the tcpc_alert enum
98  * The hard reset value takes priority, where the rest are returned in the bit order from least
99  * significant to most significant.
100  *
101  * @param reg Value of the TCPCI alert register. This parameter must have value other than zero.
102  * @return enum tcpc_alert Value of one of the flags being set in the alert register
103  */
104 enum tcpc_alert tcpci_alert_reg_to_enum(uint16_t reg);
105 
106 /**
107  * @brief Function that reads the CC status registers and converts read values to enums
108  * representing voltages state and partner detection status.
109  *
110  * @param bus I2C bus
111  * @param cc1 pointer to variable where detected CC1 voltage state will be stored
112  * @param cc2 pointer to variable where detected CC2 voltage state will be stored
113  * @return -EINVAL if cc1 or cc2 pointer is NULL
114  * @return int Status of I2C operation, 0 in case of success
115  */
116 int tcpci_tcpm_get_cc(const struct i2c_dt_spec *bus, enum tc_cc_voltage_state *cc1,
117 		      enum tc_cc_voltage_state *cc2);
118 
119 #endif /* ZEPHYR_INCLUDE_DRIVERS_USBC_TCPCI_PRIV_H_ */
120