summaryrefslogtreecommitdiff
path: root/driver/temp_sensor
diff options
context:
space:
mode:
authorDiana Z <dzigterman@chromium.org>2021-06-03 16:23:50 -0600
committerCommit Bot <commit-bot@chromium.org>2021-06-07 13:51:53 +0000
commite0a14f2a16299e91c7fd29a7ee4f83bcfa406789 (patch)
tree58cea1f28c07c029fa43eff4da36bb34e7485f8e /driver/temp_sensor
parent40c9302f2e73be8009914fa6091a65b0d90c1300 (diff)
downloadchrome-ec-e0a14f2a16299e91c7fd29a7ee4f83bcfa406789.tar.gz
TMP112: Support multiple sensors
A board may have more than one TMP112, in which case we need an array to know which address to use for each sensor and for retrieving that sensor's last read temperature. BRANCH=None BUG=b:188539950 TEST=make -j buildall Signed-off-by: Diana Z <dzigterman@chromium.org> Change-Id: If3ad286010698683f25825872bd8bfd53b8795e6 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2938044 Reviewed-by: Rob Barnes <robbarnes@google.com> Commit-Queue: Rob Barnes <robbarnes@google.com>
Diffstat (limited to 'driver/temp_sensor')
-rw-r--r--driver/temp_sensor/tmp112.c36
-rw-r--r--driver/temp_sensor/tmp112.h17
2 files changed, 39 insertions, 14 deletions
diff --git a/driver/temp_sensor/tmp112.c b/driver/temp_sensor/tmp112.c
index 04f22db89c..9a72bf273f 100644
--- a/driver/temp_sensor/tmp112.c
+++ b/driver/temp_sensor/tmp112.c
@@ -16,26 +16,28 @@
#define TMP112_SHIFT1 (16 - TMP112_RESOLUTION)
#define TMP112_SHIFT2 (TMP112_RESOLUTION - 8)
-static int temp_val_local;
+static int temp_val_local[TMP112_COUNT];
-static int raw_read16(const int offset, int *data_ptr)
+static int raw_read16(int sensor, const int offset, int *data_ptr)
{
- return i2c_read16(I2C_PORT_THERMAL, TMP112_I2C_ADDR_FLAGS0,
+ return i2c_read16(tmp112_sensors[sensor].i2c_port,
+ tmp112_sensors[sensor].i2c_addr_flags,
offset, data_ptr);
}
-static int raw_write16(const int offset, int data)
+static int raw_write16(int sensor, const int offset, int data)
{
- return i2c_write16(I2C_PORT_THERMAL, TMP112_I2C_ADDR_FLAGS0,
+ return i2c_write16(tmp112_sensors[sensor].i2c_port,
+ tmp112_sensors[sensor].i2c_addr_flags,
offset, data);
}
-static int get_temp(int *temp_ptr)
+static int get_temp(int sensor, int *temp_ptr)
{
int rv;
int temp_raw = 0;
- rv = raw_read16(TMP112_REG_TEMP, &temp_raw);
+ rv = raw_read16(sensor, TMP112_REG_TEMP, &temp_raw);
if (rv < 0)
return rv;
@@ -54,22 +56,28 @@ static inline int tmp112_reg_to_c(int16_t reg)
int tmp112_get_val(int idx, int *temp_ptr)
{
- *temp_ptr = temp_val_local;
+ if (idx >= TMP112_COUNT)
+ return EC_ERROR_INVAL;
+
+ *temp_ptr = temp_val_local[idx];
return EC_SUCCESS;
}
static void tmp112_poll(void)
{
+ int s;
int temp_c = 0;
- if (get_temp(&temp_c) == EC_SUCCESS)
- temp_val_local = C_TO_K(tmp112_reg_to_c(temp_c));
+ for (s = 0; s < TMP112_COUNT; s++) {
+ if (get_temp(s, &temp_c) == EC_SUCCESS)
+ temp_val_local[s] = C_TO_K(tmp112_reg_to_c(temp_c));
+ }
}
DECLARE_HOOK(HOOK_SECOND, tmp112_poll, HOOK_PRIO_TEMP_SENSOR);
static void tmp112_init(void)
{
- int tmp;
+ int tmp, s;
int set_mask, clr_mask;
/* 12 bit mode */
@@ -78,7 +86,9 @@ static void tmp112_init(void)
/* not oneshot mode */
clr_mask = BIT(7);
- raw_read16(TMP112_REG_CONF, &tmp);
- raw_write16(TMP112_REG_CONF, (tmp & ~clr_mask) | set_mask);
+ for (s = 0; s < TMP112_COUNT; s++) {
+ raw_read16(s, TMP112_REG_CONF, &tmp);
+ raw_write16(s, TMP112_REG_CONF, (tmp & ~clr_mask) | set_mask);
+ }
}
DECLARE_HOOK(HOOK_INIT, tmp112_init, HOOK_PRIO_DEFAULT);
diff --git a/driver/temp_sensor/tmp112.h b/driver/temp_sensor/tmp112.h
index 3386bc77ed..af9506ec5b 100644
--- a/driver/temp_sensor/tmp112.h
+++ b/driver/temp_sensor/tmp112.h
@@ -18,10 +18,25 @@
#define TMP112_REG_HYST 0x02
#define TMP112_REG_MAX 0x03
+/*
+ * I2C port and address information for all the board TMP112 sensors should be
+ * defined in an array of the following structures, with an enum tmp112_sensor
+ * indexing the array. The enum tmp112_sensor shall end with a TMP112_COUNT
+ * defining the maximum number of sensors for the board.
+ */
+
+struct tmp112_sensor_t {
+ int i2c_port;
+ int i2c_addr_flags;
+};
+
+extern const struct tmp112_sensor_t tmp112_sensors[];
+
/**
* Get the last polled value of a sensor.
*
- * @param idx Index to read. (Ignored)
+ * @param idx Index to read, from board's enum tmp112_sensor
+ * definition
*
* @param temp_ptr Destination for temperature in K.
*