From c72bb08cba84bedfff9e14af77843d20755b5438 Mon Sep 17 00:00:00 2001 From: Kevin Huang Date: Fri, 22 Nov 2024 09:25:21 +0800 Subject: [PATCH 1/2] aalc: Add 2nd src Brick driver and init config setting Summary: - new file: common/dev/bmr4922302_803.c - new file: common/dev/include/bmr4922302_803.h - modified: common/service/sensor/sensor.c - modified: common/service/sensor/sensor.h - modified: meta-facebook/aalc-rpu/src/platform/plat_hook.c - modified: meta-facebook/aalc-rpu/src/platform/plat_hook.h - modified: meta-facebook/aalc-rpu/src/platform/plat_sensor_table.c Description: - Add Brick 2md src bmr4922302_803 driver. - Add function to check sensor is main src or 2nd src Test Plan: - Build code: PASS --- common/dev/bmr4922302_803.c | 112 ++++++++++++++++++ common/dev/include/bmr4922302_803.h | 24 ++++ common/service/sensor/sensor.c | 10 +- common/service/sensor/sensor.h | 1 + .../aalc-rpu/src/platform/plat_hook.c | 4 + .../aalc-rpu/src/platform/plat_hook.h | 3 +- .../aalc-rpu/src/platform/plat_sensor_table.c | 40 +++++++ 7 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 common/dev/bmr4922302_803.c create mode 100644 common/dev/include/bmr4922302_803.h diff --git a/common/dev/bmr4922302_803.c b/common/dev/bmr4922302_803.c new file mode 100644 index 0000000000..28201f1e17 --- /dev/null +++ b/common/dev/bmr4922302_803.c @@ -0,0 +1,112 @@ +/* +* Copyright (c) Meta Platforms, Inc. and affiliates. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include +#include +#include "libutil.h" +#include "sensor.h" +#include "hal_i2c.h" +#include "pmbus.h" +#include "util_pmbus.h" + +#define BMR4922302_803_READ_VOUT_EXP_VALUE (1.0 / (1 << 11)) //2^(-11) 0x15 2's complement in 5-bit + +LOG_MODULE_REGISTER(bmr4922302_803); + +uint8_t bmr4922302_803_read(sensor_cfg *cfg, int *reading) +{ + CHECK_NULL_ARG_WITH_RETURN(cfg, SENSOR_UNSPECIFIED_ERROR); + CHECK_NULL_ARG_WITH_RETURN(reading, SENSOR_UNSPECIFIED_ERROR); + + if (cfg->num > SENSOR_NUM_MAX) { + LOG_ERR("sensor num: 0x%x is invalid", cfg->num); + return SENSOR_UNSPECIFIED_ERROR; + } + + uint8_t retry = 5; + sensor_val *sval = (sensor_val *)reading; + I2C_MSG msg; + memset(sval, 0, sizeof(sensor_val)); + msg.bus = cfg->port; + msg.target_addr = cfg->target_addr; + msg.tx_len = 1; + msg.rx_len = 2; + uint16_t read_back_data; + float val, vin_val, vout_val, iout_val, temp_val; + + switch (cfg->offset) { + case PMBUS_READ_VIN: + msg.data[0] = PMBUS_READ_VIN; + if (i2c_master_read(&msg, retry)) + return SENSOR_FAIL_TO_ACCESS; + + read_back_data = (msg.data[1] << 8) | msg.data[0]; + vin_val = slinear11_to_float(read_back_data); + val = vin_val; + + break; + case PMBUS_READ_VOUT: + msg.data[0] = PMBUS_READ_VOUT; + if (i2c_master_read(&msg, retry)) + return SENSOR_FAIL_TO_ACCESS; + + read_back_data = (msg.data[1] << 8) | msg.data[0]; + vout_val = read_back_data * BMR4922302_803_READ_VOUT_EXP_VALUE; + val = vout_val; + + break; + case PMBUS_READ_IOUT: + msg.data[0] = PMBUS_READ_IOUT; + if (i2c_master_read(&msg, retry)) + return SENSOR_FAIL_TO_ACCESS; + + read_back_data = (msg.data[1] << 8) | msg.data[0]; + iout_val = slinear11_to_float(read_back_data); + val = iout_val; + + break; + case PMBUS_READ_TEMPERATURE_1: + msg.data[0] = PMBUS_READ_TEMPERATURE_1; + if (i2c_master_read(&msg, retry)) + return SENSOR_FAIL_TO_ACCESS; + + read_back_data = (msg.data[1] << 8) | msg.data[0]; + temp_val = slinear11_to_float(read_back_data); + val = temp_val; + + break; + default: + return SENSOR_UNSPECIFIED_ERROR; + } + + sval->integer = val; + sval->fraction = (val - sval->integer) * 1000; + + return SENSOR_READ_SUCCESS; +} + +uint8_t bmr4922302_803_init(sensor_cfg *cfg) +{ + CHECK_NULL_ARG_WITH_RETURN(cfg, SENSOR_INIT_UNSPECIFIED_ERROR); + + if (cfg->num > SENSOR_NUM_MAX) { + return SENSOR_INIT_UNSPECIFIED_ERROR; + } + + cfg->read = bmr4922302_803_read; + return SENSOR_INIT_SUCCESS; +} diff --git a/common/dev/include/bmr4922302_803.h b/common/dev/include/bmr4922302_803.h new file mode 100644 index 0000000000..ef3187f778 --- /dev/null +++ b/common/dev/include/bmr4922302_803.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BMR4922302_803H +#define BMR4922302_803H + +typedef struct _bmr4922302_803_init_arg { + bool is_init; +} bmr4922302_803_init_arg; + +#endif \ No newline at end of file diff --git a/common/service/sensor/sensor.c b/common/service/sensor/sensor.c index 8e0408e107..438257af91 100644 --- a/common/service/sensor/sensor.c +++ b/common/service/sensor/sensor.c @@ -148,6 +148,7 @@ const char *const sensor_type_name[] = { sensor_name_to_num(rtq6056) sensor_name_to_num(mp29816a) sensor_name_to_num(raa228249) + sensor_name_to_num(bmr4922302_803) }; // clang-format on @@ -357,6 +358,9 @@ SENSOR_DRIVE_INIT_DECLARE(mp29816a); #ifdef ENABLE_RAA228249 SENSOR_DRIVE_INIT_DECLARE(raa228249); #endif +#ifndef DISABLE_BMR4922302_803 +SENSOR_DRIVE_INIT_DECLARE(bmr4922302_803); +#endif // The sequence needs to same with SENSOR_DEV ID sensor_drive_api sensor_drive_tbl[] = { @@ -708,7 +712,11 @@ sensor_drive_api sensor_drive_tbl[] = { #else SENSOR_DRIVE_TYPE_UNUSE(raa228249), #endif - +#ifdef ENABLE_PLAT_DEF_SENSOR + SENSOR_DRIVE_TYPE_INIT_MAP(bmr4922302_803), +#else + SENSOR_DRIVE_TYPE_UNUSE(bmr4922302_803), +#endif }; static void init_sensor_num(void) diff --git a/common/service/sensor/sensor.h b/common/service/sensor/sensor.h index a862af567d..f697ceffda 100644 --- a/common/service/sensor/sensor.h +++ b/common/service/sensor/sensor.h @@ -186,6 +186,7 @@ enum SENSOR_DEV { sensor_dev_rtq6056 = 0x44, sensor_dev_mp29816a = 0x45, sensor_dev_raa228249 = 0x46, + sensor_dev_bmr4922302_803 = 0x47, sensor_dev_max }; diff --git a/meta-facebook/aalc-rpu/src/platform/plat_hook.c b/meta-facebook/aalc-rpu/src/platform/plat_hook.c index dae61728bb..827e48b53b 100644 --- a/meta-facebook/aalc-rpu/src/platform/plat_hook.c +++ b/meta-facebook/aalc-rpu/src/platform/plat_hook.c @@ -843,6 +843,10 @@ e50sn12051_init_arg e50sn12051_init_args[] = { [0] = { .is_init = false,}, }; +bmr4922302_803_init_arg bmr4922302_803_init_args[] = { + [0] = { .is_init = false,}, +}; + /************************************************************************************************** * PRE-HOOK/POST-HOOK ARGS **************************************************************************************************/ diff --git a/meta-facebook/aalc-rpu/src/platform/plat_hook.h b/meta-facebook/aalc-rpu/src/platform/plat_hook.h index b017c2b491..3839bf1bd5 100644 --- a/meta-facebook/aalc-rpu/src/platform/plat_hook.h +++ b/meta-facebook/aalc-rpu/src/platform/plat_hook.h @@ -30,6 +30,7 @@ #include "ast_tach.h" #include "xdp710.h" #include "e50sn12051.h" +#include "bmr4922302_803.h" #define BUS_1_MUX_ADDR 0xE0 >> 1 #define BUS_2_MUX_ADDR 0xE2 >> 1 @@ -64,7 +65,7 @@ extern xdp710_init_arg xdp710_init_args[]; extern hdc1080_init_arg hdc1080_init_args[]; extern ast_tach_init_arg ast_tach_init_args[]; extern e50sn12051_init_arg e50sn12051_init_args[]; - +extern bmr4922302_803_init_arg bmr4922302_803_init_args[]; /************************************************************************************************** * POST ARGS **************************************************************************************************/ diff --git a/meta-facebook/aalc-rpu/src/platform/plat_sensor_table.c b/meta-facebook/aalc-rpu/src/platform/plat_sensor_table.c index 6b8837fd84..0f142baba4 100644 --- a/meta-facebook/aalc-rpu/src/platform/plat_sensor_table.c +++ b/meta-facebook/aalc-rpu/src/platform/plat_sensor_table.c @@ -1213,10 +1213,50 @@ static void change_mb_temp_sensor_config() } } +static void change_brick_sensor_config() +{ + #define E50SN12051_MFR_ID 0x0644 + #define BMR4922302_803_MFR_ID 0x0c46 + + uint8_t retry = 5; + I2C_MSG msg; + msg.bus = I2C_BUS4; + msg.target_addr = BPB_BRICK_12V_ADDR; + msg.tx_len = 1; + msg.rx_len = 4; + uint16_t mfr_id; + msg.data[0] = PMBUS_MFR_ID; + + if (i2c_master_read(&msg, retry)) + { + LOG_ERR("Failed to read Brick module MFR_ID"); + return; + } + + mfr_id = (msg.data[0] << 8) | msg.data[1]; + printf("Brick module mfr_id: %x\n", mfr_id); + + if (mfr_id == E50SN12051_MFR_ID) + return; + + for (uint8_t i = 0; i < ARRAY_SIZE(plat_sensor_config); i++) { + sensor_cfg *p = plat_sensor_config + i; + if (p->num == SENSOR_NUM_BPB_BRICK_12V_VIN_VOLT_V || + p->num == SENSOR_NUM_BPB_BRICK_12V_VOUT_VOLT_V || + p->num == SENSOR_NUM_BPB_BRICK_12V_IOUT_CURR_A || + p->num == SENSOR_NUM_BPB_BRICK_12V_TEMP_C) + { + p->type = sensor_dev_bmr4922302_803; + p->init_args = &bmr4922302_803_init_args[0]; + } + } +} + void load_sensor_config(void) { change_mb_temp_sensor_config(); change_dvt_sensor_config(); + change_brick_sensor_config(); memcpy(sensor_config, plat_sensor_config, sizeof(plat_sensor_config)); sensor_config_count = ARRAY_SIZE(plat_sensor_config); From 0acf4234e9a82fe3d3c9a0381aa4b150973fa242 Mon Sep 17 00:00:00 2001 From: Kevin Huang Date: Fri, 22 Nov 2024 09:36:02 +0800 Subject: [PATCH 2/2] fix clang-format --- .../aalc-rpu/src/platform/plat_sensor_table.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/meta-facebook/aalc-rpu/src/platform/plat_sensor_table.c b/meta-facebook/aalc-rpu/src/platform/plat_sensor_table.c index 0f142baba4..8ae1950804 100644 --- a/meta-facebook/aalc-rpu/src/platform/plat_sensor_table.c +++ b/meta-facebook/aalc-rpu/src/platform/plat_sensor_table.c @@ -1215,9 +1215,9 @@ static void change_mb_temp_sensor_config() static void change_brick_sensor_config() { - #define E50SN12051_MFR_ID 0x0644 - #define BMR4922302_803_MFR_ID 0x0c46 - +#define E50SN12051_MFR_ID 0x0644 +#define BMR4922302_803_MFR_ID 0x0c46 + uint8_t retry = 5; I2C_MSG msg; msg.bus = I2C_BUS4; @@ -1227,12 +1227,11 @@ static void change_brick_sensor_config() uint16_t mfr_id; msg.data[0] = PMBUS_MFR_ID; - if (i2c_master_read(&msg, retry)) - { + if (i2c_master_read(&msg, retry)) { LOG_ERR("Failed to read Brick module MFR_ID"); return; } - + mfr_id = (msg.data[0] << 8) | msg.data[1]; printf("Brick module mfr_id: %x\n", mfr_id); @@ -1244,8 +1243,7 @@ static void change_brick_sensor_config() if (p->num == SENSOR_NUM_BPB_BRICK_12V_VIN_VOLT_V || p->num == SENSOR_NUM_BPB_BRICK_12V_VOUT_VOLT_V || p->num == SENSOR_NUM_BPB_BRICK_12V_IOUT_CURR_A || - p->num == SENSOR_NUM_BPB_BRICK_12V_TEMP_C) - { + p->num == SENSOR_NUM_BPB_BRICK_12V_TEMP_C) { p->type = sensor_dev_bmr4922302_803; p->init_args = &bmr4922302_803_init_args[0]; }