From 44ce677a6e3cbbe7983bc6fe6ff5eee64fd0c385 Mon Sep 17 00:00:00 2001 From: Zick Wei Date: Thu, 18 Nov 2021 11:08:34 +0800 Subject: driver/temp_sensor: support thermal sensor pct2075 BUG=b:206704936 BRANCH=main TEST=verify thermal sensor work as intended on nipperkin Signed-off-by: Zick Wei Change-Id: Id76d6c09c48a9927e85a0b81b629e9a5639041bd Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3290824 Reviewed-by: Rob Barnes --- common/build.mk | 1 + driver/build.mk | 1 + driver/temp_sensor/pct2075.c | 91 ++++++++++++++++++++++++++++++++++++++++++++ driver/temp_sensor/pct2075.h | 70 ++++++++++++++++++++++++++++++++++ include/config.h | 1 + util/config_allowed.txt | 1 + 6 files changed, 165 insertions(+) create mode 100644 driver/temp_sensor/pct2075.c create mode 100644 driver/temp_sensor/pct2075.h diff --git a/common/build.mk b/common/build.mk index bd0fcb164e..d00a7f8bb1 100644 --- a/common/build.mk +++ b/common/build.mk @@ -30,6 +30,7 @@ common-$(CONFIG_ACCEL_LIS2DS)+=math_util.o common-$(CONFIG_ACCEL_KXCJ9)+=math_util.o common-$(CONFIG_ACCEL_KX022)+=math_util.o common-$(CONFIG_TEMP_SENSOR_TMP112)+=math_util.o +common-$(CONFIG_TEMP_SENSOR_PCT2075)+=math_util.o ifneq ($(CORE),cortex-m) common-$(CONFIG_AES)+=aes.o endif diff --git a/driver/build.mk b/driver/build.mk index cb7262e778..e0ccda2b10 100644 --- a/driver/build.mk +++ b/driver/build.mk @@ -121,6 +121,7 @@ driver-$(CONFIG_TEMP_SENSOR_G753)+=temp_sensor/g753.o driver-$(CONFIG_TEMP_SENSOR_G781)+=temp_sensor/g78x.o driver-$(CONFIG_TEMP_SENSOR_G782)+=temp_sensor/g78x.o driver-$(CONFIG_TEMP_SENSOR_OTI502)+=temp_sensor/oti502.o +driver-$(CONFIG_TEMP_SENSOR_PCT2075)+=temp_sensor/pct2075.o driver-$(CONFIG_TEMP_SENSOR_SB_TSI)+=temp_sensor/sb_tsi.o driver-$(CONFIG_TEMP_SENSOR_TMP006)+=temp_sensor/tmp006.o driver-$(CONFIG_TEMP_SENSOR_TMP112)+=temp_sensor/tmp112.o diff --git a/driver/temp_sensor/pct2075.c b/driver/temp_sensor/pct2075.c new file mode 100644 index 0000000000..bde1521edc --- /dev/null +++ b/driver/temp_sensor/pct2075.c @@ -0,0 +1,91 @@ +/* Copyright 2021 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* PCT2075 temperature sensor module for Chrome EC */ + +#include "common.h" +#include "console.h" +#include "pct2075.h" +#include "i2c.h" +#include "hooks.h" +#include "math_util.h" +#include "util.h" + +#define PCT2075_RESOLUTION 11 +#define PCT2075_SHIFT1 (16 - PCT2075_RESOLUTION) +#define PCT2075_SHIFT2 (PCT2075_RESOLUTION - 8) + +#define CPRINTS(format, args...) cprints(CC_THERMAL, format, ## args) + +static int temp_mk_local[PCT2075_COUNT]; + +static int raw_read16(int sensor, const int offset, int *data_ptr) +{ +#ifdef CONFIG_I2C_BUS_MAY_BE_UNPOWERED + /* + * Don't try to read if the port is unpowered + */ + if (!board_is_i2c_port_powered(pct2075_sensors[sensor].i2c_port)) + return EC_ERROR_NOT_POWERED; +#endif + return i2c_read16(pct2075_sensors[sensor].i2c_port, + pct2075_sensors[sensor].i2c_addr_flags, + offset, data_ptr); +} + +static int get_reg_temp(int sensor, int *temp_ptr) +{ + int temp_raw = 0; + + RETURN_ERROR(raw_read16(sensor, PCT2075_REG_TEMP, &temp_raw)); + + *temp_ptr = (int)(int16_t)temp_raw; + return EC_SUCCESS; +} + +static inline int pct2075_reg_to_mk(int16_t reg) +{ + int temp_mc; + + temp_mc = (((reg >> PCT2075_SHIFT1) * 1000) >> PCT2075_SHIFT2); + + return MILLI_CELSIUS_TO_MILLI_KELVIN(temp_mc); +} + +int pct2075_get_val_k(int idx, int *temp_k_ptr) +{ + if (idx >= PCT2075_COUNT) + return EC_ERROR_INVAL; + + *temp_k_ptr = MILLI_KELVIN_TO_KELVIN(temp_mk_local[idx]); + return EC_SUCCESS; +} + +int pct2075_get_val_mk(int idx, int *temp_mk_ptr) +{ + if (idx >= PCT2075_COUNT) + return EC_ERROR_INVAL; + + *temp_mk_ptr = temp_mk_local[idx]; + return EC_SUCCESS; +} + +static void pct2075_poll(void) +{ + int s; + int temp_reg = 0; + + for (s = 0; s < PCT2075_COUNT; s++) { + if (get_reg_temp(s, &temp_reg) == EC_SUCCESS) + temp_mk_local[s] = pct2075_reg_to_mk(temp_reg); + } +} +DECLARE_HOOK(HOOK_SECOND, pct2075_poll, HOOK_PRIO_TEMP_SENSOR); + +void pct2075_init(void) +{ +/* Incase we need to initialize somthing */ +} +DECLARE_HOOK(HOOK_INIT, pct2075_init, HOOK_PRIO_DEFAULT); diff --git a/driver/temp_sensor/pct2075.h b/driver/temp_sensor/pct2075.h new file mode 100644 index 0000000000..c09d0e383c --- /dev/null +++ b/driver/temp_sensor/pct2075.h @@ -0,0 +1,70 @@ +/* Copyright 2021 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef __CROS_EC_PCT2075_H +#define __CROS_EC_PCT2075_H + +#include "i2c.h" + +#define PCT2075_I2C_ADDR_FLAGS0 (0x48 | I2C_FLAG_BIG_ENDIAN) +#define PCT2075_I2C_ADDR_FLAGS1 (0x49 | I2C_FLAG_BIG_ENDIAN) +#define PCT2075_I2C_ADDR_FLAGS2 (0x4A | I2C_FLAG_BIG_ENDIAN) +#define PCT2075_I2C_ADDR_FLAGS3 (0x4B | I2C_FLAG_BIG_ENDIAN) +#define PCT2075_I2C_ADDR_FLAGS4 (0x4C | I2C_FLAG_BIG_ENDIAN) +#define PCT2075_I2C_ADDR_FLAGS5 (0x4D | I2C_FLAG_BIG_ENDIAN) +#define PCT2075_I2C_ADDR_FLAGS6 (0x4E | I2C_FLAG_BIG_ENDIAN) +#define PCT2075_I2C_ADDR_FLAGS7 (0x4F | I2C_FLAG_BIG_ENDIAN) + +#define PCT2075_REG_TEMP 0x00 +#define PCT2075_REG_CONF 0x01 +#define PCT2075_REG_THYST 0x02 +#define PCT2075_REG_TOS 0x03 + +/* + * I2C port and address information for all the board PCT2075 sensors should be + * defined in an array of the following structures, with an enum PCT2075_sensor + * indexing the array. The enum PCT2075_sensor shall end with a PCT2075_COUNT + * defining the maximum number of sensors for the board. + */ + +struct pct2075_sensor_t { + int i2c_port; + int i2c_addr_flags; +}; + +extern const struct pct2075_sensor_t pct2075_sensors[]; + +/** + * Get the last polled value of a sensor. + * + * @param idx Index to read, from board's enum PCT2075_sensor + * definition + * + * @param temp_k_ptr Destination for temperature in K. + * + * @return EC_SUCCESS if successful, non-zero if error. + */ +int pct2075_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 PCT2075_sensor + * definition + * + * @param temp_mk_ptr Destination for temperature in mK. + * + * @return EC_SUCCESS if successful, non-zero if error. + */ +int pct2075_get_val_mk(int idx, int *temp_mk_ptr); + +/** + * Init the sensors. Note, this will run automatically on HOOK_INIT, but is + * made available for boards which may not always power the sensor in all + * states. + */ +void pct2075_init(void); + +#endif /* __CROS_EC_PCT2075_H */ diff --git a/include/config.h b/include/config.h index b18ddc57d1..5452ccdaa9 100644 --- a/include/config.h +++ b/include/config.h @@ -4024,6 +4024,7 @@ #undef CONFIG_TEMP_SENSOR_G781 /* G781 sensor, on I2C bus */ #undef CONFIG_TEMP_SENSOR_G782 /* G782 sensor, on I2C bus */ #undef CONFIG_TEMP_SENSOR_OTI502 /* OTI502 sensor, on I2C bus */ +#undef CONFIG_TEMP_SENSOR_PCT2075 /* PCT2075 sensor, on I2C bus */ #undef CONFIG_TEMP_SENSOR_SB_TSI /* SB_TSI sensor, on I2C bus */ #undef CONFIG_TEMP_SENSOR_TMP006 /* TI TMP006 sensor, on I2C bus */ #undef CONFIG_TEMP_SENSOR_TMP112 /* TI TMP112 sensor, on I2C bus */ diff --git a/util/config_allowed.txt b/util/config_allowed.txt index 8aff943b61..6b6fcfce75 100644 --- a/util/config_allowed.txt +++ b/util/config_allowed.txt @@ -894,6 +894,7 @@ CONFIG_TEMP_SENSOR_G753 CONFIG_TEMP_SENSOR_G781 CONFIG_TEMP_SENSOR_G782 CONFIG_TEMP_SENSOR_OTI502 +CONFIG_TEMP_SENSOR_PCT2075 CONFIG_TEMP_SENSOR_POWER_GPIO CONFIG_TEMP_SENSOR_SB_TSI CONFIG_TEMP_SENSOR_TMP006 -- cgit v1.2.1