summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMoritz Fischer <moritz.fischer@ettus.com>2016-12-06 09:25:33 -0800
committerchrome-bot <chrome-bot@chromium.org>2016-12-30 18:14:34 -0800
commit73d0b9ca9f57e2693e394e81d7d2ab27f348d613 (patch)
treedd264c879158814cc8ad6928cd7b65cfa6c51ccb
parentbf3f8ca53fac14cafaa8e66f682f5542f71a97ca (diff)
downloadchrome-ec-73d0b9ca9f57e2693e394e81d7d2ab27f348d613.tar.gz
driver: temp_sensor: Add support for TI TMP112 sensor
Add support for the Texas Instruments TMP112 I2C temperature sensor. The sensor provides a single temperature value. BUG=none BRANCH=none TEST=Ran 'temp' console command over and over. Verified values with digital thermometer. Change-Id: Ida4c082309d245c3f7c6282ecea74ce5af746e43 Signed-off-by: Moritz Fischer <moritz.fischer@ettus.com> Reviewed-on: https://chromium-review.googlesource.com/418488 Reviewed-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--driver/build.mk1
-rw-r--r--driver/temp_sensor/tmp112.c82
-rw-r--r--driver/temp_sensor/tmp112.h29
3 files changed, 112 insertions, 0 deletions
diff --git a/driver/build.mk b/driver/build.mk
index 43f68502ea..aab87ee035 100644
--- a/driver/build.mk
+++ b/driver/build.mk
@@ -74,6 +74,7 @@ driver-$(CONFIG_TEMP_SENSOR_G781)+=temp_sensor/g78x.o
driver-$(CONFIG_TEMP_SENSOR_G782)+=temp_sensor/g78x.o
driver-$(CONFIG_TEMP_SENSOR_TMP006)+=temp_sensor/tmp006.o
driver-$(CONFIG_TEMP_SENSOR_TMP432)+=temp_sensor/tmp432.o
+driver-$(CONFIG_TEMP_SENSOR_TMP112)+=temp_sensor/tmp112.o
# Touchpads
driver-$(CONFIG_TOUCHPAD_ELAN)+=touchpad_elan.o
diff --git a/driver/temp_sensor/tmp112.c b/driver/temp_sensor/tmp112.c
new file mode 100644
index 0000000000..f567e2433c
--- /dev/null
+++ b/driver/temp_sensor/tmp112.c
@@ -0,0 +1,82 @@
+/* Copyright (c) 2016 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.
+ */
+
+/* TMP112 temperature sensor module for Chrome EC */
+
+#include "common.h"
+#include "console.h"
+#include "tmp112.h"
+#include "i2c.h"
+#include "hooks.h"
+#include "util.h"
+
+#define TMP112_RESOLUTION 12
+#define TMP112_SHIFT1 (16 - TMP112_RESOLUTION)
+#define TMP112_SHIFT2 (TMP112_RESOLUTION - 8)
+
+static int temp_val_local;
+
+static int raw_read16(const int offset, int *data_ptr)
+{
+ return i2c_read16(I2C_PORT_THERMAL, TMP112_I2C_ADDR, offset, data_ptr);
+}
+
+static int raw_write16(const int offset, int data)
+{
+ return i2c_write16(I2C_PORT_THERMAL, TMP112_I2C_ADDR, offset, data);
+}
+
+static int get_temp(int *temp_ptr)
+{
+ int rv;
+ int temp_raw = 0;
+
+ rv = raw_read16(TMP112_REG_TEMP, &temp_raw);
+ if (rv < 0)
+ return rv;
+
+ *temp_ptr = (int)(int16_t)temp_raw;
+ return EC_SUCCESS;
+}
+
+static inline int tmp112_reg_to_c(int16_t reg)
+{
+ int tmp;
+
+ tmp = (((reg >> TMP112_SHIFT1) * 1000 ) >> TMP112_SHIFT2);
+
+ return tmp / 1000;
+}
+
+int tmp112_get_val(int idx, int *temp_ptr)
+{
+ *temp_ptr = temp_val_local;
+ return EC_SUCCESS;
+}
+
+static void tmp112_poll(void)
+{
+ int temp_c = 0;
+
+ if (get_temp(&temp_c) == EC_SUCCESS)
+ temp_val_local = 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 set_mask, clr_mask;
+
+ /* 12 bit mode */
+ set_mask = (3 << 5);
+
+ /* not oneshot mode */
+ clr_mask = (1 << 7);
+
+ raw_read16(TMP112_REG_CONF, &tmp);
+ raw_write16(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
new file mode 100644
index 0000000000..14ee56da0c
--- /dev/null
+++ b/driver/temp_sensor/tmp112.h
@@ -0,0 +1,29 @@
+/* Copyright (c) 2016 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_TMP112_H
+#define __CROS_EC_TMP112_H
+
+#include "i2c.h"
+
+#define TMP112_I2C_ADDR 0x90 | I2C_FLAG_BIG_ENDIAN
+
+#define TMP112_REG_TEMP 0x00
+#define TMP112_REG_CONF 0x01
+#define TMP112_REG_HYST 0x02
+#define TMP112_REG_MAX 0x03
+
+/**
+ * Get the last polled value of a sensor.
+ *
+ * @param idx Index to read. (Ignored)
+ *
+ * @param temp_ptr Destination for temperature in K.
+ *
+ * @return EC_SUCCESS if successful, non-zero if error.
+ */
+int tmp112_get_val(int idx, int *temp_ptr);
+
+#endif /* __CROS_EC_TMP112_H */