diff options
-rw-r--r-- | board/mushu/board.h | 7 | ||||
-rw-r--r-- | board/nami/board.h | 7 | ||||
-rw-r--r-- | driver/temp_sensor/f75303.c | 60 | ||||
-rw-r--r-- | driver/temp_sensor/f75303.h | 45 | ||||
-rw-r--r-- | util/config_allowed.txt | 1 | ||||
-rw-r--r-- | zephyr/CMakeLists.txt | 2 | ||||
-rw-r--r-- | zephyr/Kconfig.temperature | 8 | ||||
-rw-r--r-- | zephyr/dts/bindings/temp/cros-ec,temp-sensor-f75303.yaml | 10 | ||||
-rw-r--r-- | zephyr/shim/include/config_chip.h | 5 | ||||
-rw-r--r-- | zephyr/shim/include/temp_sensor/temp_sensor.h | 18 | ||||
-rw-r--r-- | zephyr/shim/src/temp_sensors.c | 40 |
11 files changed, 195 insertions, 8 deletions
diff --git a/board/mushu/board.h b/board/mushu/board.h index 02443bbfa3..f8ae0deb4d 100644 --- a/board/mushu/board.h +++ b/board/mushu/board.h @@ -198,6 +198,13 @@ enum temp_sensor_id { TEMP_SENSOR_COUNT }; +enum f75303_index { + F75303_IDX_LOCAL = 0, + F75303_IDX_REMOTE1, + F75303_IDX_REMOTE2, + F75303_IDX_COUNT, +}; + /* List of possible batteries */ enum battery_type { BATTERY_POWER_TECH, diff --git a/board/nami/board.h b/board/nami/board.h index 652db4826d..2a1ce91d3c 100644 --- a/board/nami/board.h +++ b/board/nami/board.h @@ -223,6 +223,13 @@ enum temp_sensor_id { TEMP_SENSOR_COUNT, }; +enum f75303_index { + F75303_IDX_LOCAL = 0, + F75303_IDX_REMOTE1, + F75303_IDX_REMOTE2, + F75303_IDX_COUNT, +}; + /* * Motion sensors: * When reading through IO memory is set up for sensors (LPC is used), diff --git a/driver/temp_sensor/f75303.c b/driver/temp_sensor/f75303.c index d83f7bc4a6..dbeaf4470c 100644 --- a/driver/temp_sensor/f75303.c +++ b/driver/temp_sensor/f75303.c @@ -10,10 +10,19 @@ #include "f75303.h" #include "hooks.h" #include "i2c.h" +#include "math_util.h" #include "util.h" +#ifdef CONFIG_ZEPHYR +#include "temp_sensor/temp_sensor.h" +#endif + +#define F75303_RESOLUTION 11 +#define F75303_SHIFT1 (16 - F75303_RESOLUTION) +#define F75303_SHIFT2 (F75303_RESOLUTION - 8) + static int temps[F75303_IDX_COUNT]; -static int8_t fake_temp[F75303_IDX_COUNT] = { -1, -1, -1 }; +static int8_t fake_temp[F75303_IDX_COUNT]; /** * Read 8 bits register from temp sensor. @@ -50,6 +59,34 @@ int f75303_get_val(int idx, int *temp) return EC_SUCCESS; } +static inline int f75303_reg_to_mk(int16_t reg) +{ + int temp_mc; + + temp_mc = (((reg >> F75303_SHIFT1) * 1000) >> F75303_SHIFT2); + + return MILLI_CELSIUS_TO_MILLI_KELVIN(temp_mc); +} + +int f75303_get_val_k(int idx, int *temp_k_ptr) +{ + if (idx >= F75303_IDX_COUNT) + return EC_ERROR_INVAL; + + *temp_k_ptr = MILLI_KELVIN_TO_KELVIN(temps[idx]); + return EC_SUCCESS; +} + +int f75303_get_val_mk(int idx, int *temp_mk_ptr) +{ + if (idx >= F75303_IDX_COUNT) + return EC_ERROR_INVAL; + + *temp_mk_ptr = temps[idx]; + return EC_SUCCESS; +} + +#ifndef CONFIG_ZEPHYR static void f75303_sensor_poll(void) { get_temp(F75303_TEMP_LOCAL, &temps[F75303_IDX_LOCAL]); @@ -57,6 +94,18 @@ static void f75303_sensor_poll(void) get_temp(F75303_TEMP_REMOTE2, &temps[F75303_IDX_REMOTE2]); } DECLARE_HOOK(HOOK_SECOND, f75303_sensor_poll, HOOK_PRIO_TEMP_SENSOR); +#else +void f75303_update_temperature(int idx) +{ + int temp_reg = 0; + + if (idx >= F75303_IDX_COUNT) + return; + + if (get_temp(idx, &temp_reg) == EC_SUCCESS) + temps[idx] = f75303_reg_to_mk(temp_reg); +} +#endif /* CONFIG_ZEPHYR */ static int f75303_set_fake_temp(int argc, const char **argv) { @@ -89,3 +138,12 @@ static int f75303_set_fake_temp(int argc, const char **argv) } DECLARE_CONSOLE_COMMAND(f75303, f75303_set_fake_temp, "<index> <value>|off", "Set fake temperature of sensor f75303."); + +static void f75303_init(void) +{ + int index; + + for (index = 0; index < F75303_IDX_COUNT; index++) + fake_temp[index] = -1; +} +DECLARE_HOOK(HOOK_INIT, f75303_init, HOOK_PRIO_TEMP_SENSOR); diff --git a/driver/temp_sensor/f75303.h b/driver/temp_sensor/f75303.h index f8c85231da..4632978145 100644 --- a/driver/temp_sensor/f75303.h +++ b/driver/temp_sensor/f75303.h @@ -14,13 +14,20 @@ #define F75303_I2C_ADDR_FLAGS 0x4C #endif -enum f75303_index { - F75303_IDX_LOCAL = 0, - F75303_IDX_REMOTE1, - F75303_IDX_REMOTE2, - F75303_IDX_COUNT, +/* + * I2C port and address information for all the board F75303 sensors should be + * defined in an array of the following structures, with an enum f75303_sensor + * indexing the array. The enum f75303_sensor shall end with a F75303_IDX_COUNT + * defining the maximum number of sensors for the board. + */ + +struct f75303_sensor_t { + int i2c_port; + int i2c_addr_flags; }; +extern const struct f75303_sensor_t f75303_sensors[]; + /* F75303 register */ #define F75303_TEMP_LOCAL 0x00 #define F75303_TEMP_REMOTE1 0x01 @@ -37,4 +44,32 @@ enum f75303_index { */ int f75303_get_val(int idx, int *temp); +/** + * Get the last polled value of a sensor. + * + * @param idx Index to read, from board's enum f75303_sensor + * definition + * + * @param temp_k_ptr Destination for temperature in K. + * + * @return EC_SUCCESS if successful, non-zero if error. + */ +int f75303_get_val_k(int idx, int *temp_k_ptr); + +/** + * Get the last polled value of a sensor. + * + * @param idx Index to read, from board's enum f75303_sensor + * definition + * + * @param temp_mk_ptr Destination for temperature in mK. + * + * @return EC_SUCCESS if successful, non-zero if error. + */ +int f75303_get_val_mk(int idx, int *temp_mk_ptr); + +#ifdef CONFIG_ZEPHYR +void f75303_update_temperature(int idx); +#endif /* CONFIG_ZEPHYR */ + #endif /* __CROS_EC_F75303_H */ diff --git a/util/config_allowed.txt b/util/config_allowed.txt index 160e208c21..3c792ae0fb 100644 --- a/util/config_allowed.txt +++ b/util/config_allowed.txt @@ -842,7 +842,6 @@ CONFIG_TEMP_SENSOR_ADT7481 CONFIG_TEMP_SENSOR_AMD_R19ME4070 CONFIG_TEMP_SENSOR_BD99992GW CONFIG_TEMP_SENSOR_EC_ADC -CONFIG_TEMP_SENSOR_F75303 CONFIG_TEMP_SENSOR_G753 CONFIG_TEMP_SENSOR_G781 CONFIG_TEMP_SENSOR_G782 diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 25ce9f0f27..5fd91e2335 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -393,6 +393,8 @@ zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_TEMP_SENSOR_SB_TSI "${PLATFORM_EC}/driver/temp_sensor/sb_tsi.c") zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_TEMP_SENSOR_TMP112 "${PLATFORM_EC}/driver/temp_sensor/tmp112.c") +zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_TEMP_SENSOR_F75303 + "${PLATFORM_EC}/driver/temp_sensor/f75303.c") zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_THROTTLE_AP "${PLATFORM_EC}/common/throttle_ap.c") zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_TIMER diff --git a/zephyr/Kconfig.temperature b/zephyr/Kconfig.temperature index b1f6e63c96..bd977b2832 100644 --- a/zephyr/Kconfig.temperature +++ b/zephyr/Kconfig.temperature @@ -66,6 +66,14 @@ config PLATFORM_EC_TEMP_SENSOR_TMP112 Enables support for the CrosEC TMP112 driver, an i2c peripheral temperature sensor from TI. +config PLATFORM_EC_TEMP_SENSOR_F75303 + bool "F75303 support" + default y + depends on DT_HAS_CROS_EC_TEMP_SENSOR_F75303_ENABLED + help + Enables support for the CrosEC F75303 driver, an i2c peripheral + temperature sensor from TI. + endif # PLATFORM_EC_TEMP_SENSOR diff --git a/zephyr/dts/bindings/temp/cros-ec,temp-sensor-f75303.yaml b/zephyr/dts/bindings/temp/cros-ec,temp-sensor-f75303.yaml new file mode 100644 index 0000000000..3ef0bca2ac --- /dev/null +++ b/zephyr/dts/bindings/temp/cros-ec,temp-sensor-f75303.yaml @@ -0,0 +1,10 @@ +# Copyright 2023 The ChromiumOS Authors +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +description: > + Properties for a F75303 I2C temperature sensor + +compatible: "cros-ec,temp-sensor-f75303" + +include: i2c-device.yaml diff --git a/zephyr/shim/include/config_chip.h b/zephyr/shim/include/config_chip.h index 8d70fac7fe..7ecb3b0f13 100644 --- a/zephyr/shim/include/config_chip.h +++ b/zephyr/shim/include/config_chip.h @@ -678,6 +678,11 @@ extern char mock_jump_data[CONFIG_PLATFORM_EC_PRESERVED_END_OF_RAM_SIZE]; #define CONFIG_TEMP_SENSOR_TMP112 #endif +#undef CONFIG_TEMP_SENSOR_F75303 +#ifdef CONFIG_PLATFORM_EC_TEMP_SENSOR_F75303 +#define CONFIG_TEMP_SENSOR_F75303 +#endif + #undef CONFIG_FANS #ifdef CONFIG_PLATFORM_EC_NUM_FANS #define CONFIG_FANS CONFIG_PLATFORM_EC_NUM_FANS diff --git a/zephyr/shim/include/temp_sensor/temp_sensor.h b/zephyr/shim/include/temp_sensor/temp_sensor.h index 5e4cca3063..5ff7662d62 100644 --- a/zephyr/shim/include/temp_sensor/temp_sensor.h +++ b/zephyr/shim/include/temp_sensor/temp_sensor.h @@ -15,6 +15,7 @@ #define PCT2075_COMPAT nxp_pct2075 #define TMP112_COMPAT cros_ec_temp_sensor_tmp112 +#define F75303_COMPAT cros_ec_temp_sensor_f75303 #define SB_TSI_COMPAT amd_sb_tsi #define THERMISTOR_COMPAT cros_ec_temp_sensor_thermistor #define TEMP_SENSORS_COMPAT cros_ec_temp_sensors @@ -27,6 +28,7 @@ #define FOREACH_TEMP_SENSOR(fn) \ DT_FOREACH_STATUS_OKAY(PCT2075_COMPAT, fn) \ DT_FOREACH_STATUS_OKAY(TMP112_COMPAT, fn) \ + DT_FOREACH_STATUS_OKAY(F75303_COMPAT, fn) \ DT_FOREACH_STATUS_OKAY_VARGS(RT9490_CHG_COMPAT, TEMP_RT9490_FN, fn) \ DT_FOREACH_STATUS_OKAY(SB_TSI_COMPAT, fn) \ DT_FOREACH_STATUS_OKAY(THERMISTOR_COMPAT, fn) @@ -141,6 +143,22 @@ enum tmp112_sensor { #undef TMP112_SENSOR_ID_WITH_COMMA +/* F75303 access array */ +/* + * Get the F75303 sensor ID from a hardware device node. + * + * @param node_id: node id of a hardware F75303 sensor node + */ +#define F75303_SENSOR_ID(node_id) DT_CAT(F75303_, node_id) +#define F75303_SENSOR_ID_WITH_COMMA(node_id) F75303_SENSOR_ID(node_id), + +enum f75303_sensor { + DT_FOREACH_STATUS_OKAY(F75303_COMPAT, F75303_SENSOR_ID_WITH_COMMA) + F75303_IDX_COUNT, +}; + +#undef F75303_SENSOR_ID_WITH_COMMA + struct zephyr_temp_sensor { /* Read sensor value in K into temp_ptr; return non-zero if error. */ int (*read)(const struct temp_sensor_t *sensor, int *temp_ptr); diff --git a/zephyr/shim/src/temp_sensors.c b/zephyr/shim/src/temp_sensors.c index ae3a367252..439ea917f9 100644 --- a/zephyr/shim/src/temp_sensors.c +++ b/zephyr/shim/src/temp_sensors.c @@ -6,6 +6,7 @@ #include "adc.h" #include "charger/chg_rt9490.h" #include "driver/charger/rt9490.h" +#include "driver/temp_sensor/f75303.h" #include "hooks.h" #include "temp_sensor.h" #include "temp_sensor/pct2075.h" @@ -198,6 +199,42 @@ const struct tmp112_sensor_t tmp112_sensors[TMP112_COUNT] = { DT_FOREACH_STATUS_OKAY(TMP112_COMPAT, DEFINE_TMP112_DATA) }; +#if DT_HAS_COMPAT_STATUS_OKAY(F75303_COMPAT) +/* The function maybe unused because a temperature sensor can be added to dts + * without a reference in the cros_ec_temp_sensors node. + */ +__maybe_unused static int f75303_get_temp(const struct temp_sensor_t *sensor, + int *temp_ptr) +{ + return f75303_get_val_k(sensor->idx, temp_ptr); +} +#endif /* f75303_COMPAT */ + +#define DEFINE_F75303_DATA(node_id) \ + [F75303_SENSOR_ID(node_id)] = { \ + .i2c_port = I2C_PORT_BY_DEV(node_id), \ + .i2c_addr_flags = DT_REG_ADDR(node_id), \ + }, + +#define GET_ZEPHYR_TEMP_SENSOR_F75303(named_id) \ + (&(const struct zephyr_temp_sensor){ \ + .read = &f75303_get_temp, \ + .thermistor = NULL, \ + .update_temperature = f75303_update_temperature, \ + FILL_POWER_GOOD(named_id) }) + +#define TEMP_F75303(named_id, sensor_id) \ + [TEMP_SENSOR_ID(named_id)] = { \ + .name = DT_NODE_FULL_NAME(sensor_id), \ + .idx = F75303_SENSOR_ID(sensor_id), \ + .type = TEMP_SENSOR_TYPE_BOARD, \ + .zephyr_info = GET_ZEPHYR_TEMP_SENSOR_F75303(named_id), \ + } + +const struct f75303_sensor_t f75303_sensors[F75303_IDX_COUNT] = { + DT_FOREACH_STATUS_OKAY(F75303_COMPAT, DEFINE_F75303_DATA) +}; + /* There can be only one thermistor on RT9490 with current driver */ #define ADD_ONE(node_id) 1 + #if DT_FOREACH_STATUS_OKAY_VARGS(RT9490_CHG_COMPAT, TEMP_RT9490_FN, \ @@ -235,7 +272,8 @@ const struct tmp112_sensor_t tmp112_sensors[TMP112_COUNT] = { CHECK_COMPAT(PCT2075_COMPAT, named_id, sensor_id, TEMP_PCT2075) \ CHECK_COMPAT(SB_TSI_COMPAT, named_id, sensor_id, TEMP_SB_TSI) \ CHECK_COMPAT(TMP112_COMPAT, named_id, sensor_id, TEMP_TMP112) \ - CHECK_COMPAT(RT9490_CHG_COMPAT, named_id, sensor_id, TEMP_RT9490) + CHECK_COMPAT(RT9490_CHG_COMPAT, named_id, sensor_id, TEMP_RT9490) \ + CHECK_COMPAT(F75303_COMPAT, named_id, sensor_id, TEMP_F75303) #define TEMP_SENSOR_ENTRY(named_id) \ TEMP_SENSOR_FIND(named_id, DT_PHANDLE(named_id, sensor)) |