2323#include <zephyr/kernel.h>
2424#include <zephyr/logging/log.h>
2525#include <zephyr/drivers/misc/bh_fwtable.h>
26+ #include <zephyr/drivers/i2c/i2c_dw.h>
2627
2728#define LINEAR_FORMAT_CONSTANT (1 << 9)
2829#define SCALE_LOOP 0.335f
@@ -72,6 +73,7 @@ LOG_MODULE_REGISTER(regulator);
7273/* The default value is the regulator default */
7374static uint8_t vout_cmd_source = VoutCommand ;
7475static const struct device * const fwtable_dev = DEVICE_DT_GET (DT_NODELABEL (fwtable ));
76+ static const struct device * pmbus = DEVICE_DT_GET (DT_NODELABEL (i2c1 ));
7577
7678static float ConvertLinear11ToFloat (uint16_t value )
7779{
@@ -88,22 +90,36 @@ static float ConvertLinear11ToFloat(uint16_t value)
8890/* The function returns the core current in A. */
8991float GetVcoreCurrent (void )
9092{
91- I2CInit (I2CMst , P0V8_VCORE_ADDR , I2CFastMode , PMBUS_MST_ID );
93+ i2c_dw_register_recover_bus_cb (pmbus , tt_bh_i2c_recover_bus , pmbus );
94+
9295 uint16_t iout ;
96+ uint8_t cmd = READ_IOUT ;
97+ int ret ;
98+
99+ ret = i2c_write_read (pmbus , P0V8_VCORE_ADDR , & cmd , 1 , & iout , sizeof (iout ));
100+ if (ret != 0 ) {
101+ LOG_ERR ("PMBus read READ_IOUT failed: %d" , ret );
102+ return 0.0f ;
103+ }
93104
94- I2CReadBytes (PMBUS_MST_ID , READ_IOUT , PMBUS_CMD_BYTE_SIZE , (uint8_t * )& iout ,
95- READ_IOUT_DATA_BYTE_SIZE , PMBUS_FLIP_BYTES );
96105 return ConvertLinear11ToFloat (iout );
97106}
98107
99108/* The function returns the core power in W. */
100109float GetVcorePower (void )
101110{
102- I2CInit (I2CMst , P0V8_VCORE_ADDR , I2CFastMode , PMBUS_MST_ID );
111+ i2c_dw_register_recover_bus_cb (pmbus , tt_bh_i2c_recover_bus , pmbus );
112+
103113 uint16_t pout ;
114+ uint8_t cmd = READ_POUT ;
115+ int ret ;
116+
117+ ret = i2c_write_read (pmbus , P0V8_VCORE_ADDR , & cmd , 1 , & pout , sizeof (pout ));
118+ if (ret != 0 ) {
119+ LOG_ERR ("PMBus read READ_POUT failed: %d" , ret );
120+ return 0.0f ; /* Return 0 on error */
121+ }
104122
105- I2CReadBytes (PMBUS_MST_ID , READ_POUT , PMBUS_CMD_BYTE_SIZE , (uint8_t * )& pout ,
106- READ_POUT_DATA_BYTE_SIZE , PMBUS_FLIP_BYTES );
107123 return ConvertLinear11ToFloat (pout );
108124}
109125
@@ -198,19 +214,36 @@ void set_gddr_vddr(PcbType board_type, uint32_t voltage_in_mv)
198214
199215void SwitchVoutControl (VoltageCmdSource source )
200216{
201- I2CInit (I2CMst , P0V8_VCORE_ADDR , I2CFastMode , PMBUS_MST_ID );
217+ i2c_dw_register_recover_bus_cb (pmbus , tt_bh_i2c_recover_bus , pmbus );
218+
202219 OperationBits operation ;
220+ int ret ;
221+
222+ uint8_t cmd = OPERATION ;
223+
224+ ret = i2c_write_read (pmbus , P0V8_VCORE_ADDR , & cmd , 1 , & operation , sizeof (operation ));
225+ if (ret != 0 ) {
226+ LOG_ERR ("PMBus read OPERATION failed: %d" , ret );
227+ return ;
228+ }
203229
204- I2CReadBytes (PMBUS_MST_ID , OPERATION , PMBUS_CMD_BYTE_SIZE , (uint8_t * )& operation ,
205- OPERATION_DATA_BYTE_SIZE , PMBUS_FLIP_BYTES );
206- operation .transition_control =
207- 1 ; /* copy vout command when control is passed from AVSBus to PMBus */
230+ operation .transition_control = 1 ;
208231 operation .voltage_command_source = source ;
209- I2CWriteBytes (PMBUS_MST_ID , OPERATION , PMBUS_CMD_BYTE_SIZE , (uint8_t * )& operation ,
210- OPERATION_DATA_BYTE_SIZE );
232+
233+ uint8_t write_buf [1 + sizeof (operation )];
234+
235+ write_buf [0 ] = OPERATION ;
236+ memcpy (write_buf + 1 , & operation , sizeof (operation ));
237+
238+ ret = i2c_write (pmbus , write_buf , sizeof (write_buf ), P0V8_VCORE_ADDR );
239+ if (ret != 0 ) {
240+ LOG_ERR ("PMBus write OPERATION failed: %d" , ret );
241+ return ;
242+ }
211243
212244 /* 100us to flush the tx of i2c */
213- WaitUs (100 );
245+ k_busy_wait (100 );
246+
214247 vout_cmd_source = source ;
215248}
216249
0 commit comments