summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Massey <aaronmassey@google.com>2021-09-24 11:22:12 -0600
committerCommit Bot <commit-bot@chromium.org>2021-09-24 19:47:45 +0000
commit465c9dc87a353f48b969da0631c4fd2b6df32a4f (patch)
treeeae7fa5a0f92cb81af315eb7912267167ed078e1
parentfc2b67a878f6c57c02569253032fc5d09f6c581e (diff)
downloadchrome-ec-465c9dc87a353f48b969da0631c4fd2b6df32a4f.tar.gz
zephyr: drivers: thermistors are device tree nodes
Thermistor drivers now query the device tree for configuration. Thermistor tests have been updated to be parameterized on all thermistors enabled in the device tree. BRANCH=none BUG=b:184374937 TEST= 1) zmake testall 2) make runhosttests Cq-Depend: chromium:3161332 Signed-off-by: Aaron Massey <aaronmassey@chromium.org> Change-Id: Ic5330cd5c33e79e192428ca857651de9a225856e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3133812 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Tested-by: Aaron Massey <aaronmassey@google.com> Commit-Queue: Aaron Massey <aaronmassey@google.com>
-rw-r--r--common/temp_sensor.c4
-rw-r--r--driver/temp_sensor/thermistor.c7
-rw-r--r--include/driver/temp_sensor/thermistor.h18
-rw-r--r--include/temp_sensor.h10
-rw-r--r--zephyr/Kconfig.temperature48
-rw-r--r--zephyr/boards/arm/volteer/volteer.dts17
-rw-r--r--zephyr/dts/bindings/temp/cros_ec_temp_sensor.yaml (renamed from zephyr/dts/bindings/temp/temp_sensor.yaml)34
-rw-r--r--zephyr/dts/bindings/temp/cros_ec_thermistor.yaml57
-rw-r--r--zephyr/dts/bindings/temp/temp-3v0-22k6-47k-4050b.yaml15
-rw-r--r--zephyr/dts/bindings/temp/temp-3v3-13k7-47k-4050b.yaml15
-rw-r--r--zephyr/dts/bindings/temp/temp-3v3-30k9-47k-4050b.yaml15
-rw-r--r--zephyr/dts/bindings/temp/temp-3v3-51k1-47k-4050b.yaml15
-rw-r--r--zephyr/dts/bindings/temp/thermistor.yaml29
-rw-r--r--zephyr/include/cros/thermistor/thermistor.dtsi308
-rw-r--r--zephyr/projects/volteer/volteer/prj.conf1
-rw-r--r--zephyr/shim/include/config_chip.h20
-rw-r--r--zephyr/shim/src/temp_sensors.c45
-rw-r--r--zephyr/shim/src/thermal.c56
-rw-r--r--zephyr/test/drivers/overlay.dts34
-rw-r--r--zephyr/test/drivers/prj.conf4
-rw-r--r--zephyr/test/drivers/src/thermistor.c383
21 files changed, 642 insertions, 493 deletions
diff --git a/common/temp_sensor.c b/common/temp_sensor.c
index 55101a5345..69d440a6d5 100644
--- a/common/temp_sensor.c
+++ b/common/temp_sensor.c
@@ -27,7 +27,11 @@ int temp_sensor_read(enum temp_sensor_id id, int *temp_ptr)
return EC_ERROR_INVAL;
sensor = temp_sensors + id;
+#ifdef CONFIG_ZEPHYR
+ return sensor->read(sensor, temp_ptr);
+#else
return sensor->read(sensor->idx, temp_ptr);
+#endif
}
static void update_mapped_memory(void)
diff --git a/driver/temp_sensor/thermistor.c b/driver/temp_sensor/thermistor.c
index d895aa58f0..ffa780cb07 100644
--- a/driver/temp_sensor/thermistor.c
+++ b/driver/temp_sensor/thermistor.c
@@ -70,9 +70,10 @@ int thermistor_linear_interpolate(uint16_t mv,
defined(CONFIG_STEINHART_HART_3V3_13K7_47K_4050B) || \
defined(CONFIG_STEINHART_HART_6V0_51K1_47K_4050B) || \
defined(CONFIG_STEINHART_HART_3V0_22K6_47K_4050B) || \
- defined(CONFIG_STEINHART_HART_3V3_30K9_47K_4050B)
-static int thermistor_get_temperature(int idx_adc, int *temp_ptr,
- const struct thermistor_info *info)
+ defined(CONFIG_STEINHART_HART_3V3_30K9_47K_4050B) || \
+ defined(CONFIG_ZEPHYR)
+int thermistor_get_temperature(int idx_adc, int *temp_ptr,
+ const struct thermistor_info *info)
{
int mv;
diff --git a/include/driver/temp_sensor/thermistor.h b/include/driver/temp_sensor/thermistor.h
index 7ebe2b42ac..adcd5c5be4 100644
--- a/include/driver/temp_sensor/thermistor.h
+++ b/include/driver/temp_sensor/thermistor.h
@@ -147,4 +147,20 @@ int get_temp_3v0_22k6_47k_4050b(int idx_adc, int *temp_ptr);
int get_temp_3v3_30k9_47k_4050b(int idx_adc, int *temp_ptr);
#endif
-#endif /* __CROS_EC_TEMP_SENSOR_THERMISTOR_NCP15WB_H */
+/**
+ * Reads the sensor's ADC channel and uses a lookup table and interpolation to
+ * argument thermistor_info for interpolation to return a temperature in degrees
+ * K.
+ *
+ * @param idx_adc The idx value from the temp_sensor_t struct, which is
+ * the ADC channel to read and convert to degrees K
+ * @param temp_ptr Destination for temperature (in degrees K)
+ * @param info Structure containing information about the underlying thermistor
+ * that is necessary to interpolate temperature
+ *
+ * @return EC_SUCCESS, or non-zero if error.
+ */
+int thermistor_get_temperature(int idx_adc, int *temp_ptr,
+ const struct thermistor_info *info);
+
+#endif /* __CROS_EC_TEMP_SENSOR_THERMISTOR_NCP15WB_H */
diff --git a/include/temp_sensor.h b/include/temp_sensor.h
index e5eff92cf1..50a174193f 100644
--- a/include/temp_sensor.h
+++ b/include/temp_sensor.h
@@ -33,8 +33,18 @@ struct temp_sensor_t {
const char *name;
/* Temperature sensor type. */
enum temp_sensor_type type;
+ /*
+ * TODO(b:201081891) Refactor temp_sensor_t references
+ * to all use OO style sensor argument to get adc idx.
+ */
+#ifdef CONFIG_ZEPHYR
+ /* Read sensor value in K into temp_ptr; return non-zero if error. */
+ int (*read)(const struct temp_sensor_t *sensor, int *temp_ptr);
+ struct thermistor_info *thermistor;
+#else
/* Read sensor value in K into temp_ptr; return non-zero if error. */
int (*read)(int idx, int *temp_ptr);
+#endif
/* Index among the same kind of sensors. */
int idx;
};
diff --git a/zephyr/Kconfig.temperature b/zephyr/Kconfig.temperature
index c103e1a561..4af5aa99f8 100644
--- a/zephyr/Kconfig.temperature
+++ b/zephyr/Kconfig.temperature
@@ -31,54 +31,6 @@ config PLATFORM_EC_THERMISTOR
Enables support for thermistors (resistor whose resistance is
strongly dependent on temperature) as temperature-sensor type.
-config PLATFORM_EC_STEINHART_HART_3V0_22K6_47K_4050B
- bool "Steinhart-hart 3V0_22K6_47K_4050B"
- depends on PLATFORM_EC_THERMISTOR
- help
- Enables support for measuring temperature using the Steinhart-Hart
- equation model and the thermistor configured with the following
- circuit configuration:
- -3.0V reference voltage
- -22.6 kilohm fixed resistor, connected to the reference voltage
- -47 kilohm thermistor reference resistance at 25 C
- -4050 B-constant (thermistor sensitivity)
-
-config PLATFORM_EC_STEINHART_HART_3V3_13K7_47K_4050B
- bool "Steinhart-hart 3V3_13K7_47K_4050B"
- depends on PLATFORM_EC_THERMISTOR
- help
- Enables support for measuring temperature using the Steinhart-Hart
- equation model and the thermistor configured with the following
- circuit configuration:
- -3.3V reference voltage
- -13.7 kilohm fixed resistor, connected to the reference voltage
- -47 kilohm thermistor reference resistance at 25 C
- -4050 B-constant (thermistor sensitivity)
-
-config PLATFORM_EC_STEINHART_HART_3V3_30K9_47K_4050B
- bool "Steinhart-hart 3V3_30K9_47K_4050B"
- depends on PLATFORM_EC_THERMISTOR
- help
- Enables support for measuring temperature using the Steinhart-Hart
- equation model and the thermistor configured with the following
- circuit configuration:
- -3.3V reference voltage
- -30.9 kilohm fixed resistor, connected to the reference voltage
- -47 kilohm thermistor reference resistance at 25 C
- -4050 B-constant (thermistor sensitivity)
-
-config PLATFORM_EC_STEINHART_HART_3V3_51K1_47K_4050B
- bool "Steinhart-hart 3V3_51K1_47K_4050B"
- depends on PLATFORM_EC_THERMISTOR
- default y
- help
- Enables support for measuring temperature using the Steinhart-Hart
- equation model and the thermistor configured with the following
- circuit configuration:
- -3.3V reference voltage
- -51.1 kilohm fixed resistor, connected to the reference voltage
- -47 kilohm thermistor reference resistance at 25 C
- -4050 B-constant (thermistor sensitivity)
endif # PLATFORM_EC_TEMP_SENSOR
diff --git a/zephyr/boards/arm/volteer/volteer.dts b/zephyr/boards/arm/volteer/volteer.dts
index 3c32feaf20..d837f8ab55 100644
--- a/zephyr/boards/arm/volteer/volteer.dts
+++ b/zephyr/boards/arm/volteer/volteer.dts
@@ -11,6 +11,7 @@
#include <dt-bindings/charger/intersil_isl9241.h>
#include <dt-bindings/gpio_defines.h>
#include <nuvoton/npcx7m7fc.dtsi>
+#include <cros/thermistor/thermistor.dtsi>
/ {
model = "Google Volteer EC";
@@ -124,7 +125,8 @@
named-temp-sensors {
charger {
- compatible = "temp-3v3-30k9-47k-4050b";
+ compatible = "cros-ec,temp-sensor";
+ thermistor = <&thermistor_3V3_30K9_47K_4050B>;
label = "TEMP_SENSOR_CHARGER";
enum-name = "TEMP_SENSOR_CHARGER";
temp_fan_off = <40>;
@@ -135,7 +137,8 @@
adc = <&adc_charger>;
};
pp3300_regulator {
- compatible = "temp-3v3-30k9-47k-4050b";
+ compatible = "cros-ec,temp-sensor";
+ thermistor = <&thermistor_3V3_30K9_47K_4050B>;
label = "TEMP_SENSOR_PP3300_REGULATOR";
enum-name = "TEMP_SENSOR_PP3300_REGULATOR";
temp_fan_off = <40>;
@@ -146,7 +149,8 @@
adc = <&adc_pp3300_regulator>;
};
ddr_soc {
- compatible = "temp-3v3-30k9-47k-4050b";
+ compatible = "cros-ec,temp-sensor";
+ thermistor = <&thermistor_3V3_30K9_47K_4050B>;
label = "TEMP_SENSOR_DDR_SOC";
enum-name = "TEMP_SENSOR_DDR_SOC";
temp_fan_off = <35>;
@@ -157,7 +161,8 @@
adc = <&adc_ddr_soc>;
};
fan {
- compatible = "temp-3v3-30k9-47k-4050b";
+ compatible = "cros-ec,temp-sensor";
+ thermistor = <&thermistor_3V3_30K9_47K_4050B>;
label = "TEMP_SENSOR_FAN";
enum-name = "TEMP_SENSOR_FAN";
temp_fan_off = <35>;
@@ -311,3 +316,7 @@
&psl_in4 {
flag = <NPCX_PSL_RISING_EDGE>;
};
+
+&thermistor_3V3_30K9_47K_4050B {
+ status = "okay";
+};
diff --git a/zephyr/dts/bindings/temp/temp_sensor.yaml b/zephyr/dts/bindings/temp/cros_ec_temp_sensor.yaml
index 753cfab967..288dc2d1c8 100644
--- a/zephyr/dts/bindings/temp/temp_sensor.yaml
+++ b/zephyr/dts/bindings/temp/cros_ec_temp_sensor.yaml
@@ -2,11 +2,23 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
-# Common properties for temperature sensors
-# Zero values in degrees K(-273 in degrees C)in thermal thresholds will
-# be ignored
+description: >
+ Common properties for temperature sensors
+ Zero values in degrees K(-273 in degrees C)in thermal thresholds will
+ be ignored
+
+compatible: cros-ec,temp-sensor
properties:
+ adc:
+ required: true
+ type: phandle
+ description: The named adc channel
+
+ thermistor:
+ type: phandle
+ description: Underlying thermistor device if used
+
label:
required: true
type: string
@@ -26,34 +38,26 @@ properties:
- TEMP_SENSOR_PP3300_REGULATOR
temp_fan_off:
- required: false
type: int
- default: -273
description:
Temperature threshold in degrees C when no active cooling
is needed
temp_fan_max:
- required: false
type: int
- default: -273
description:
Temperature threshold in degrees C when max active cooling
is needed
temp_host_warn:
- required: false
type: int
- default: -273
description:
Temperature threshold in degrees C of thermal warn.
Temperatures above the thermal warn threshold generate a
request to the AP to throttle itself.
temp_host_high:
- required: false
type: int
- default: -273
description:
Temperature threshold in degrees C of thermal high.
Temperatures above the thermal high threshold cause the EC to
@@ -61,30 +65,22 @@ properties:
throttle.
temp_host_halt:
- required: false
type: int
- default: -273
description:
Temperature threshold in degrees C that forces AP to shutdown
due to thermal reason
temp_host_release_warn:
- required: false
type: int
- default: -273
description:
Temperature release threshold in degrees C of thermal warn
temp_host_release_high:
- required: false
type: int
- default: -273
description:
Temperature release threshold in degrees C of thermal high
temp_host_release_halt:
- required: false
type: int
- default: -273
description:
Temperature release threshold in degrees C of thermal shutdown
diff --git a/zephyr/dts/bindings/temp/cros_ec_thermistor.yaml b/zephyr/dts/bindings/temp/cros_ec_thermistor.yaml
new file mode 100644
index 0000000000..d4bc32ed3c
--- /dev/null
+++ b/zephyr/dts/bindings/temp/cros_ec_thermistor.yaml
@@ -0,0 +1,57 @@
+# 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.
+
+# For more information:
+# https://www.electronics-tutorials.ws/io/thermistors.html
+
+description: Common properties for thermistors
+
+compatible: cros-ec,thermistor
+
+properties:
+ scaling-factor:
+ required: true
+ type: int
+ description: Scaling factor for voltage pairs
+
+ num-pairs:
+ required: true
+ type: int
+ description: Number of sample data points for linear interpolation
+
+ steinhart-reference-mv:
+ required: true
+ type: int
+ description: >
+ Used only for testing.
+ Is the reference voltage for temperature 25C.
+
+ steinhart-reference-res:
+ required: true
+ type: int
+ description: >
+ Used only for testing.
+ Is the reference resistance for temperature 25C.
+
+child-binding:
+ description: >
+ Data samples derived from Steinhart-Hart
+ equation in a resistor divider circuit.
+ Used in linear interpolation.
+
+ properties:
+ milivolt:
+ type: int
+ required: true
+ description: Voltage reading for a given temperature sample
+
+ temp:
+ type: int
+ required: true
+ description: Temperature (Celcius) in a sample
+
+ sample-index:
+ type: int
+ required: true
+ description: The index of a datum to maintain sample order to interpolate.
diff --git a/zephyr/dts/bindings/temp/temp-3v0-22k6-47k-4050b.yaml b/zephyr/dts/bindings/temp/temp-3v0-22k6-47k-4050b.yaml
deleted file mode 100644
index 09390a3656..0000000000
--- a/zephyr/dts/bindings/temp/temp-3v0-22k6-47k-4050b.yaml
+++ /dev/null
@@ -1,15 +0,0 @@
-# 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.
-
-description: Thermistor 3v0-22k6-47k-4050b
-
-compatible: "temp-3v0-22k6-47k-4050b"
-
-include: [temp_sensor.yaml, thermistor.yaml]
-
-properties:
- get-temp-func:
- enum:
- - get_temp_3v0_22k6_47k_4050b
- default: get_temp_3v0_22k6_47k_4050b
diff --git a/zephyr/dts/bindings/temp/temp-3v3-13k7-47k-4050b.yaml b/zephyr/dts/bindings/temp/temp-3v3-13k7-47k-4050b.yaml
deleted file mode 100644
index 76b6dfaa30..0000000000
--- a/zephyr/dts/bindings/temp/temp-3v3-13k7-47k-4050b.yaml
+++ /dev/null
@@ -1,15 +0,0 @@
-# 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.
-
-description: Thermistor 3v3-13k7-47k-4050b
-
-compatible: "temp-3v3-13k7-47k-4050b"
-
-include: [temp_sensor.yaml, thermistor.yaml]
-
-properties:
- get-temp-func:
- enum:
- - get_temp_3v3_13k7_47k_4050b
- default: get_temp_3v3_13k7_47k_4050b
diff --git a/zephyr/dts/bindings/temp/temp-3v3-30k9-47k-4050b.yaml b/zephyr/dts/bindings/temp/temp-3v3-30k9-47k-4050b.yaml
deleted file mode 100644
index 4d45a0eb51..0000000000
--- a/zephyr/dts/bindings/temp/temp-3v3-30k9-47k-4050b.yaml
+++ /dev/null
@@ -1,15 +0,0 @@
-# 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.
-
-description: Thermistor 3v3-30k9-47k-4050b
-
-compatible: "temp-3v3-30k9-47k-4050b"
-
-include: [temp_sensor.yaml, thermistor.yaml]
-
-properties:
- get-temp-func:
- enum:
- - get_temp_3v3_30k9_47k_4050b
- default: get_temp_3v3_30k9_47k_4050b
diff --git a/zephyr/dts/bindings/temp/temp-3v3-51k1-47k-4050b.yaml b/zephyr/dts/bindings/temp/temp-3v3-51k1-47k-4050b.yaml
deleted file mode 100644
index 0b8343f909..0000000000
--- a/zephyr/dts/bindings/temp/temp-3v3-51k1-47k-4050b.yaml
+++ /dev/null
@@ -1,15 +0,0 @@
-# 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.
-
-description: Thermistor 3v3-51k1-47k-4050b
-
-compatible: "temp-3v3-51k1-47k-4050b"
-
-include: [temp_sensor.yaml, thermistor.yaml]
-
-properties:
- get-temp-func:
- enum:
- - get_temp_3v3_51k1_47k_4050b
- default: get_temp_3v3_51k1_47k_4050b
diff --git a/zephyr/dts/bindings/temp/thermistor.yaml b/zephyr/dts/bindings/temp/thermistor.yaml
deleted file mode 100644
index 66a07de945..0000000000
--- a/zephyr/dts/bindings/temp/thermistor.yaml
+++ /dev/null
@@ -1,29 +0,0 @@
-# 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.
-
-# Common properties for thermistors
-
-properties:
- adc:
- required: true
- type: phandle
- description: named-adc child node connected to the thermistor
-
- get-temp-func:
- type: string
- required: false
- description: >
- Name of the board-provided function which reads the temperature. If you
- have different values and need to add a new function, you can add that
- here.
-
- The name is get_temp_<v>_<rs>_<rn>_<b>:
-
- v: voltage of resistor-divider circuit
- rs: value of the resistor that is in series with the thermistor
- rn: nominal resistance of thermistor at 25C
- b: B value for temperature range 25C to 100C
-
- See https://www.electronics-tutorials.ws/io/thermistors.html for more
- information.
diff --git a/zephyr/include/cros/thermistor/thermistor.dtsi b/zephyr/include/cros/thermistor/thermistor.dtsi
new file mode 100644
index 0000000000..033d5639e2
--- /dev/null
+++ b/zephyr/include/cros/thermistor/thermistor.dtsi
@@ -0,0 +1,308 @@
+/ {
+ thermistor_3V3_30K9_47K_4050B: thermistor-3V3-30K9-47K-4050B {
+ status = "disabled";
+ compatible = "cros-ec,thermistor";
+ scaling-factor = <11>;
+ num-pairs = <10>;
+ steinhart-reference-mv = <3300>;
+ steinhart-reference-res = <30900>;
+
+ /*
+ * Data derived from Steinhart-Hart equation in a resistor
+ * divider circuit with Vdd=3300mV, R = 30.9Kohm, and thermistor
+ * (B = 4050, T0 = 298.15 K, nominal resistance (R0) = 47Kohm).
+ */
+ sample-datum-0 {
+ milivolt = <(2753 / 11)>;
+ temp = <0>;
+ sample-index = <0>;
+ };
+ sample-datum-1 {
+ milivolt = <(2487 / 11)>;
+ temp = <10>;
+ sample-index = <1>;
+ };
+ sample-datum-2 {
+ milivolt = <(2165 / 11)>;
+ temp = <20>;
+ sample-index = <2>;
+ };
+ sample-datum-3 {
+ milivolt = <(1813 / 11)>;
+ temp = <30>;
+ sample-index = <3>;
+ };
+ sample-datum-4 {
+ milivolt = <(1145 / 11)>;
+ temp = <50>;
+ sample-index = <4>;
+ };
+ sample-datum-5 {
+ milivolt = <(878 / 11)>;
+ temp = <60>;
+ sample-index = <5>;
+ };
+ sample-datum-6 {
+ milivolt = <(665 / 11)>;
+ temp = <70>;
+ sample-index = <6>;
+ };
+ sample-datum-7 {
+ milivolt = <(500 / 11)>;
+ temp = <80>;
+ sample-index = <7>;
+ };
+ sample-datum-8 {
+ milivolt = <(375 / 11)>;
+ temp = <90>;
+ sample-index = <8>;
+ };
+ sample-datum-9 {
+ milivolt = <(282 / 11)>;
+ temp = <100>;
+ sample-index = <9>;
+ };
+ };
+
+
+ thermistor_3V0_22K6_47K_4050B: thermistor-3V0-22K6-47K-4050B {
+ status = "disabled";
+ compatible = "cros-ec,thermistor";
+ scaling-factor = <11>;
+ num-pairs = <13>;
+ steinhart-reference-mv = <3000>;
+ steinhart-reference-res = <22600>;
+
+ /*
+ * Data derived from Steinhart-Hart equation in a resistor
+ * divider circuit with Vdd=3000mV, R = 22.6Kohm, and thermistor
+ * (B = 4050, T0 = 298.15 K, nominal resistance (R0) = 47Kohm).
+ */
+ sample-datum-0 {
+ milivolt = <( 2619 / 11)>;
+ temp = <0>;
+ sample-index = <0>;
+ };
+ sample-datum-1 {
+ milivolt = <( 2421 / 11)>;
+ temp = <10>;
+ sample-index = <1>;
+ };
+ sample-datum-2 {
+ milivolt = <( 2168 / 11)>;
+ temp = <20>;
+ sample-index = <2>;
+ };
+ sample-datum-3 {
+ milivolt = <( 1875 / 11)>;
+ temp = <30>;
+ sample-index = <3>;
+ };
+ sample-datum-4 {
+ milivolt = <( 1563 / 11)>;
+ temp = <40>;
+ sample-index = <4>;
+ };
+ sample-datum-5 {
+ milivolt = <( 1262 / 11)>;
+ temp = <50>;
+ sample-index = <5>;
+ };
+ sample-datum-6 {
+ milivolt = <( 994 / 11)>;
+ temp = <60>;
+ sample-index = <6>;
+ };
+ sample-datum-7 {
+ milivolt = <( 769 / 11)>;
+ temp = <70>;
+ sample-index = <7>;
+ };
+ sample-datum-8 {
+ milivolt = <( 588 / 11)>;
+ temp = <80>;
+ sample-index = <8>;
+ };
+ sample-datum-9 {
+ milivolt = <( 513 / 11)>;
+ temp = <85>;
+ sample-index = <9>;
+ };
+ sample-datum-10 {
+ milivolt = <( 448 / 11)>;
+ temp = <90>;
+ sample-index = <10>;
+ };
+ sample-datum-11 {
+ milivolt = <( 390 / 11)>;
+ temp = <95>;
+ sample-index = <11>;
+ };
+ sample-datum-12 {
+ milivolt = <( 340 / 11)>;
+ temp = <100>;
+ sample-index = <12>;
+ };
+ };
+
+ thermistor_3V3_13K7_47K_4050B: thermistor-3V3-13K7-47K-4050B {
+ status = "disabled";
+ compatible = "cros-ec,thermistor";
+ scaling-factor = <13>;
+ num-pairs = <13>;
+ steinhart-reference-mv = <3300>;
+ steinhart-reference-res = <13700>;
+
+ /*
+ * Data derived from Steinhart-Hart equation in a resistor
+ * divider circuit with Vdd=3300mV, R = 13.7Kohm, and thermistor
+ * (B = 4050, T0 = 298.15 K, nominal resistance (R0) = 47Kohm).
+ */
+ sample-datum-0 {
+ milivolt = <(3033 / 13)>;
+ temp = <0>;
+ sample-index = <0>;
+ };
+ sample-datum-1 {
+ milivolt = <(2882 / 13)>;
+ temp = <10>;
+ sample-index = <1>;
+ };
+ sample-datum-2 {
+ milivolt = <(2677 / 13)>;
+ temp = <20>;
+ sample-index = <2>;
+ };
+ sample-datum-3 {
+ milivolt = <(2420 / 13)>;
+ temp = <30>;
+ sample-index = <3>;
+ };
+ sample-datum-4 {
+ milivolt = <(2119 / 13)>;
+ temp = <40>;
+ sample-index = <4>;
+ };
+ sample-datum-5 {
+ milivolt = <(1799 / 13)>;
+ temp = <50>;
+ sample-index = <5>;
+ };
+ sample-datum-6 {
+ milivolt = <(1485 / 13)>;
+ temp = <60>;
+ sample-index = <6>;
+ };
+ sample-datum-7 {
+ milivolt = <(1197 / 13)>;
+ temp = <70>;
+ sample-index = <7>;
+ };
+ sample-datum-8 {
+ milivolt = <( 947 / 13)>;
+ temp = <80>;
+ sample-index = <8>;
+ };
+ sample-datum-9 {
+ milivolt = <( 839 / 13)>;
+ temp = <85>;
+ sample-index = <9>;
+ };
+ sample-datum-10 {
+ milivolt = <( 741 / 13)>;
+ temp = <90>;
+ sample-index = <10>;
+ };
+ sample-datum-11 {
+ milivolt = <( 653 / 13)>;
+ temp = <95>;
+ sample-index = <11>;
+ };
+ sample-datum-12 {
+ milivolt = <( 576 / 13)>;
+ temp = <100>;
+ sample-index = <12>;
+ };
+ };
+
+
+ thermistor_3V3_51K1_47K_4050B: thermistor-3V3-51K1-47K-4050B {
+ status = "disabled";
+ compatible = "cros-ec,thermistor";
+ scaling-factor = <11>;
+ num-pairs = <13>;
+ steinhart-reference-mv = <3300>;
+ steinhart-reference-res = <51100>;
+
+ /*
+ * Data derived from Steinhart-Hart equation in a resistor
+ * divider circuit with Vdd=3300mV, R = 51.1Kohm, and thermistor
+ * (B = 4050, T0 = 298.15 K, nominal resistance (R0) = 47Kohm).
+ */
+ sample-datum-0 {
+ milivolt = <(2484 / 11)>;
+ temp = <0>;
+ sample-index = <0>;
+ };
+ sample-datum-1 {
+ milivolt = <(2142 / 11)>;
+ temp = <10>;
+ sample-index = <1>;
+ };
+ sample-datum-2 {
+ milivolt = <(1767 / 11)>;
+ temp = <20>;
+ sample-index = <2>;
+ };
+ sample-datum-3 {
+ milivolt = <(1400 / 11)>;
+ temp = <30>;
+ sample-index = <3>;
+ };
+ sample-datum-4 {
+ milivolt = <(1072 / 11)>;
+ temp = <40>;
+ sample-index = <4>;
+ };
+ sample-datum-5 {
+ milivolt = <( 802 / 11)>;
+ temp = <50>;
+ sample-index = <5>;
+ };
+ sample-datum-6 {
+ milivolt = <( 593 / 11)>;
+ temp = <60>;
+ sample-index = <6>;
+ };
+ sample-datum-7 {
+ milivolt = <( 436 / 11)>;
+ temp = <70>;
+ sample-index = <7>;
+ };
+ sample-datum-8 {
+ milivolt = <( 321 / 11)>;
+ temp = <80>;
+ sample-index = <8>;
+ };
+ sample-datum-9 {
+ milivolt = <( 276 / 11)>;
+ temp = <85>;
+ sample-index = <9>;
+ };
+ sample-datum-10 {
+ milivolt = <( 237 / 11)>;
+ temp = <90>;
+ sample-index = <10>;
+ };
+ sample-datum-11 {
+ milivolt = <( 204 / 11)>;
+ temp = <95>;
+ sample-index = <11>;
+ };
+ sample-datum-12 {
+ milivolt = <( 177 / 11)>;
+ temp = <100>;
+ sample-index = <12>;
+ };
+ };
+};
diff --git a/zephyr/projects/volteer/volteer/prj.conf b/zephyr/projects/volteer/volteer/prj.conf
index de1a8108e4..f2e10c0b9f 100644
--- a/zephyr/projects/volteer/volteer/prj.conf
+++ b/zephyr/projects/volteer/volteer/prj.conf
@@ -92,7 +92,6 @@ CONFIG_PLATFORM_EC_ALS_TCS3400=y
# Temperature sensors
CONFIG_PLATFORM_EC_TEMP_SENSOR=y
CONFIG_PLATFORM_EC_THERMISTOR=y
-CONFIG_PLATFORM_EC_STEINHART_HART_3V3_30K9_47K_4050B=y
# Miscellaneous tasks
CONFIG_HAS_TASK_KEYPROTO=y
diff --git a/zephyr/shim/include/config_chip.h b/zephyr/shim/include/config_chip.h
index 8c8ba3bbf2..a19a2e1775 100644
--- a/zephyr/shim/include/config_chip.h
+++ b/zephyr/shim/include/config_chip.h
@@ -427,26 +427,6 @@
#define CONFIG_THERMISTOR
#endif
-#undef CONFIG_STEINHART_HART_3V0_22K6_47K_4050B
-#ifdef CONFIG_PLATFORM_EC_STEINHART_HART_3V0_22K6_47K_4050B
-#define CONFIG_STEINHART_HART_3V0_22K6_47K_4050B
-#endif
-
-#undef CONFIG_STEINHART_HART_3V3_13K7_47K_4050B
-#ifdef CONFIG_PLATFORM_EC_STEINHART_HART_3V3_13K7_47K_4050B
-#define CONFIG_STEINHART_HART_3V3_13K7_47K_4050B
-#endif
-
-#undef CONFIG_STEINHART_HART_3V3_30K9_47K_4050B
-#ifdef CONFIG_PLATFORM_EC_STEINHART_HART_3V3_30K9_47K_4050B
-#define CONFIG_STEINHART_HART_3V3_30K9_47K_4050B
-#endif
-
-#undef CONFIG_STEINHART_HART_3V3_51K1_47K_4050B
-#ifdef CONFIG_PLATFORM_EC_STEINHART_HART_3V3_51K1_47K_4050B
-#define CONFIG_STEINHART_HART_3V3_51K1_47K_4050B
-#endif
-
#ifdef CONFIG_PLATFORM_EC_I2C
/* Also see shim/include/i2c/i2c.h which defines the ports enum */
#define CONFIG_I2C_CONTROLLER
diff --git a/zephyr/shim/src/temp_sensors.c b/zephyr/shim/src/temp_sensors.c
index 2544169ba3..4d8be4fa42 100644
--- a/zephyr/shim/src/temp_sensors.c
+++ b/zephyr/shim/src/temp_sensors.c
@@ -8,16 +8,45 @@
#include "adc.h"
#include "temp_sensor/thermistor.h"
-#define TEMP_THERMISTOR(node_id) \
- [ZSHIM_TEMP_SENSOR_ID(node_id)] = { \
- .name = DT_LABEL(node_id), \
- .read = DT_STRING_TOKEN(node_id, get_temp_func), \
- .idx = ZSHIM_ADC_ID(DT_PHANDLE(node_id, adc)), \
- .type = TEMP_SENSOR_TYPE_BOARD, \
+#if DT_NODE_EXISTS(DT_PATH(named_temp_sensors))
+static int thermistor_get_temp(const struct temp_sensor_t *sensor,
+ int *temp_ptr)
+{
+ return thermistor_get_temperature(sensor->idx, temp_ptr,
+ sensor->thermistor);
+}
+
+#define GET_THERMISTOR_DATUM(node_sample_id) \
+ [DT_PROP(node_sample_id, \
+ sample_index)] = { .mv = DT_PROP(node_sample_id, milivolt), \
+ .temp = DT_PROP(node_sample_id, temp) },
+
+#define DEFINE_THERMISTOR_DATA(node_id) \
+ static const struct thermistor_data_pair DT_CAT( \
+ node_id, _thermistor_data)[] = { \
+ DT_FOREACH_CHILD(node_id, GET_THERMISTOR_DATUM) \
+ };
+
+#define GET_THERMISTOR_INFO(node_id) \
+ (&(struct thermistor_info){ \
+ .scaling_factor = DT_PROP(node_id, scaling_factor), \
+ .num_pairs = DT_PROP(node_id, num_pairs), \
+ .data = DT_CAT(node_id, _thermistor_data), \
+ })
+
+#define TEMP_THERMISTOR(node_id) \
+ [ZSHIM_TEMP_SENSOR_ID(node_id)] = { \
+ .name = DT_LABEL(node_id), \
+ .read = &thermistor_get_temp, \
+ .idx = ZSHIM_ADC_ID(DT_PHANDLE(node_id, adc)), \
+ .type = TEMP_SENSOR_TYPE_BOARD, \
+ .thermistor = \
+ GET_THERMISTOR_INFO(DT_PHANDLE(node_id, thermistor)), \
},
-#if DT_NODE_EXISTS(DT_PATH(named_temp_sensors))
+DT_FOREACH_STATUS_OKAY(cros_ec_thermistor, DEFINE_THERMISTOR_DATA)
+
const struct temp_sensor_t temp_sensors[] = {
- DT_FOREACH_CHILD(DT_PATH(named_temp_sensors), TEMP_THERMISTOR)
+ DT_FOREACH_STATUS_OKAY(cros_ec_temp_sensor, TEMP_THERMISTOR)
};
#endif /* named_temp_sensors */
diff --git a/zephyr/shim/src/thermal.c b/zephyr/shim/src/thermal.c
index ecac9242f8..c31e2bfcc6 100644
--- a/zephyr/shim/src/thermal.c
+++ b/zephyr/shim/src/thermal.c
@@ -7,26 +7,42 @@
#include "temp_sensor/temp_sensor.h"
#include "ec_commands.h"
-#define THERMAL_CONFIG(node_id) \
- [ZSHIM_TEMP_SENSOR_ID(node_id)] = { \
- .temp_host = { \
- [EC_TEMP_THRESH_WARN] = \
- C_TO_K(DT_PROP(node_id, temp_host_warn)), \
- [EC_TEMP_THRESH_HIGH] = \
- C_TO_K(DT_PROP(node_id, temp_host_high)), \
- [EC_TEMP_THRESH_HALT] = \
- C_TO_K(DT_PROP(node_id, temp_host_halt)), \
- }, \
- .temp_host_release = { \
- [EC_TEMP_THRESH_WARN] = C_TO_K( \
- DT_PROP(node_id, temp_host_release_warn)), \
- [EC_TEMP_THRESH_HIGH] = C_TO_K( \
- DT_PROP(node_id, temp_host_release_high)), \
- [EC_TEMP_THRESH_HALT] = C_TO_K( \
- DT_PROP(node_id, temp_host_release_halt)), \
- }, \
- .temp_fan_off = C_TO_K(DT_PROP(node_id, temp_fan_off)), \
- .temp_fan_max = C_TO_K(DT_PROP(node_id, temp_fan_max)), \
+#define THERMAL_CONFIG(node_id) \
+ [ZSHIM_TEMP_SENSOR_ID(node_id)] = { \
+ .temp_host = { \
+ [EC_TEMP_THRESH_WARN] = \
+ C_TO_K(DT_PROP_OR(node_id, \
+ temp_host_warn, \
+ -273)), \
+ [EC_TEMP_THRESH_HIGH] = \
+ C_TO_K(DT_PROP_OR(node_id, \
+ temp_host_high, \
+ -273)), \
+ [EC_TEMP_THRESH_HALT] = \
+ C_TO_K(DT_PROP_OR(node_id, \
+ temp_host_halt, \
+ -273)), \
+ }, \
+ .temp_host_release = { \
+ [EC_TEMP_THRESH_WARN] = C_TO_K( \
+ DT_PROP_OR(node_id, \
+ temp_host_release_warn, \
+ -273)), \
+ [EC_TEMP_THRESH_HIGH] = C_TO_K( \
+ DT_PROP_OR(node_id, \
+ temp_host_release_high, \
+ -273)), \
+ [EC_TEMP_THRESH_HALT] = C_TO_K( \
+ DT_PROP_OR(node_id, \
+ temp_host_release_halt, \
+ -273)), \
+ }, \
+ .temp_fan_off = C_TO_K(DT_PROP_OR(node_id, \
+ temp_fan_off, \
+ -273)), \
+ .temp_fan_max = C_TO_K(DT_PROP_OR(node_id, \
+ temp_fan_max, \
+ -273)), \
},
struct ec_thermal_config thermal_params[] = {
diff --git a/zephyr/test/drivers/overlay.dts b/zephyr/test/drivers/overlay.dts
index 90a9864df7..b211d77caa 100644
--- a/zephyr/test/drivers/overlay.dts
+++ b/zephyr/test/drivers/overlay.dts
@@ -4,6 +4,7 @@
*/
#include <dt-bindings/gpio_defines.h>
+#include <cros/thermistor/thermistor.dtsi>
/ {
aliases {
@@ -147,7 +148,9 @@
named-temp-sensors {
charger {
- compatible = "temp-3v3-13k7-47k-4050b";
+ thermistor = <&thermistor_3V3_13K7_47K_4050B>;
+ status = "okay";
+ compatible = "cros-ec,temp-sensor";
label = "TEMP_SENSOR_CHARGER";
enum-name = "TEMP_SENSOR_CHARGER";
temp_fan_off = <40>;
@@ -158,7 +161,9 @@
adc = <&adc_charger>;
};
pp3300-regulator {
- compatible = "temp-3v3-30k9-47k-4050b";
+ thermistor = <&thermistor_3V3_30K9_47K_4050B>;
+ status = "okay";
+ compatible = "cros-ec,temp-sensor";
label = "TEMP_SENSOR_PP3300_REGULATOR";
enum-name = "TEMP_SENSOR_PP3300_REGULATOR";
temp_fan_off = <40>;
@@ -169,7 +174,9 @@
adc = <&adc_pp3300_regulator>;
};
ddr-soc {
- compatible = "temp-3v3-51k1-47k-4050b";
+ thermistor = <&thermistor_3V3_51K1_47K_4050B>;
+ status = "okay";
+ compatible = "cros-ec,temp-sensor";
label = "TEMP_SENSOR_DDR_SOC";
enum-name = "TEMP_SENSOR_DDR_SOC";
temp_fan_off = <35>;
@@ -180,7 +187,9 @@
adc = <&adc_ddr_soc>;
};
fan {
- compatible = "temp-3v0-22k6-47k-4050b";
+ thermistor = <&thermistor_3V0_22K6_47K_4050B>;
+ status = "okay";
+ compatible = "cros-ec,temp-sensor";
label = "TEMP_SENSOR_FAN";
enum-name = "TEMP_SENSOR_FAN";
temp_fan_off = <35>;
@@ -533,3 +542,20 @@
label = "I2C_MOCK";
};
};
+
+/* Enable all thermistors for testing */
+&thermistor_3V3_30K9_47K_4050B {
+ status = "okay";
+};
+
+&thermistor_3V0_22K6_47K_4050B {
+ status = "okay";
+};
+
+&thermistor_3V3_13K7_47K_4050B {
+ status = "okay";
+};
+
+&thermistor_3V3_51K1_47K_4050B {
+ status = "okay";
+};
diff --git a/zephyr/test/drivers/prj.conf b/zephyr/test/drivers/prj.conf
index 6f9cbaa076..99a275f9e2 100644
--- a/zephyr/test/drivers/prj.conf
+++ b/zephyr/test/drivers/prj.conf
@@ -62,10 +62,6 @@ CONFIG_PLATFORM_EC_CBI_EEPROM=y
CONFIG_PLATFORM_EC_ADC=y
CONFIG_PLATFORM_EC_TEMP_SENSOR=y
CONFIG_PLATFORM_EC_THERMISTOR=y
-CONFIG_PLATFORM_EC_STEINHART_HART_3V0_22K6_47K_4050B=y
-CONFIG_PLATFORM_EC_STEINHART_HART_3V3_13K7_47K_4050B=y
-CONFIG_PLATFORM_EC_STEINHART_HART_3V3_30K9_47K_4050B=y
-CONFIG_PLATFORM_EC_STEINHART_HART_3V3_51K1_47K_4050B=y
CONFIG_PLATFORM_EC_SWITCHCAP_LN9310=y
CONFIG_PLATFORM_EC_ACCEL_BMA255=y
CONFIG_PLATFORM_EC_MOTIONSENSE=y
diff --git a/zephyr/test/drivers/src/thermistor.c b/zephyr/test/drivers/src/thermistor.c
index f6e3b5f732..a7137d5f19 100644
--- a/zephyr/test/drivers/src/thermistor.c
+++ b/zephyr/test/drivers/src/thermistor.c
@@ -9,9 +9,11 @@
#include <drivers/adc/adc_emul.h>
#include <drivers/gpio.h>
#include <drivers/gpio/gpio_emul.h>
+#include <temp_sensor.h>
#include "common.h"
#include "../driver/temp_sensor/thermistor.h"
+#include "temp_sensor/temp_sensor.h"
#define GPIO_PG_EC_DSW_PWROK_PATH DT_PATH(named_gpios, pg_ec_dsw_pwrok)
@@ -19,100 +21,74 @@
#define ADC_DEVICE_NODE DT_NODELABEL(adc0)
-#define TEMP_3V3_13K7_47K_4050B_INST DT_INST(0, temp_3v3_13k7_47k_4050b)
-#define ADC_CHANNEL_3V3_13K7_47K_4050B \
- DT_PROP(DT_PHANDLE(TEMP_3V3_13K7_47K_4050B_INST, adc), channel)
-
-#define TEMP_3V3_30K9_47K_4050B_INST DT_INST(0, temp_3v3_30k9_47k_4050b)
-#define ADC_CHANNEL_3V3_30K9_47K_4050B \
- DT_PROP(DT_PHANDLE(TEMP_3V3_30K9_47K_4050B_INST, adc), channel)
-
-#define TEMP_3V3_51K1_47K_4050B_INST DT_INST(0, temp_3v3_51k1_47k_4050b)
-#define ADC_CHANNEL_3V3_51K1_47K_4050B \
- DT_PROP(DT_PHANDLE(TEMP_3V3_51K1_47K_4050B_INST, adc), channel)
-
-#define TEMP_3V0_22K6_47K_4050B_INST DT_INST(0, temp_3v0_22k6_47k_4050b)
-#define ADC_CHANNEL_3V0_22K6_47K_4050B \
- DT_PROP(DT_PHANDLE(TEMP_3V0_22K6_47K_4050B_INST, adc), channel)
+/* TODO replace counting macros with DT macro when
+ * https://github.com/zephyrproject-rtos/zephyr/issues/38715 lands
+ */
+#define _ACCUMULATOR(x)
+#define NAMED_TEMP_SENSORS_SIZE \
+ DT_FOREACH_CHILD(DT_PATH(named_temp_sensors), _ACCUMULATOR) \
+ 0
+#define TEMP_SENSORS_ENABLED_SIZE \
+ DT_FOREACH_STATUS_OKAY(cros_ec_temp_sensor, _ACCUMULATOR) 0
/* Conversion of temperature doesn't need to be 100% accurate */
#define TEMP_EPS 2
+#define A_VALID_VOLTAGE 1000
/**
* Test if get temp function return expected error when ADC is not powered
* (indicated as GPIO pin set to low) and return success after powering on ADC.
*/
static void test_thermistor_power_pin(void)
{
+ int temp;
+ int sensor_idx;
+
const struct device *gpio_dev =
DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_PG_EC_DSW_PWROK_PATH, gpios));
const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
- int temp;
zassert_not_null(gpio_dev, "Cannot get GPIO device");
zassert_not_null(adc_dev, "Cannot get ADC device");
- /* Make sure that ADC return any valid value */
- zassert_ok(adc_emul_const_value_set(adc_dev,
- ADC_CHANNEL_3V3_13K7_47K_4050B,
- 1000),
- "adc_emul_const_value_set() failed");
- zassert_ok(adc_emul_const_value_set(adc_dev,
- ADC_CHANNEL_3V3_30K9_47K_4050B,
- 1000),
- "adc_emul_const_value_set() failed");
- zassert_ok(adc_emul_const_value_set(adc_dev,
- ADC_CHANNEL_3V3_51K1_47K_4050B,
- 1000),
- "adc_emul_const_value_set() failed");
- zassert_ok(adc_emul_const_value_set(adc_dev,
- ADC_CHANNEL_3V0_22K6_47K_4050B,
- 1000),
- "adc_emul_const_value_set() failed");
+ /* Make sure that ADC return a valid value */
+ for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE;
+ sensor_idx++) {
+ const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx];
+
+ zassert_ok(adc_emul_const_value_set(adc_dev,
+ sensor->idx,
+ A_VALID_VOLTAGE),
+ "adc_emul_value_func_set() failed on %s",
+ sensor->name);
+ }
/* pg_ec_dsw_pwrok = 0 means ADC is not powered. */
zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_PG_EC_DSW_PWROK_PORT, 0),
NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- get_temp_3v3_13k7_47k_4050b(
- ADC_CHANNEL_3V3_13K7_47K_4050B, &temp),
- NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- get_temp_3v3_30k9_47k_4050b(
- ADC_CHANNEL_3V3_30K9_47K_4050B, &temp),
- NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- get_temp_3v3_51k1_47k_4050b(
- ADC_CHANNEL_3V3_51K1_47K_4050B, &temp),
- NULL);
- zassert_equal(EC_ERROR_NOT_POWERED,
- get_temp_3v0_22k6_47k_4050b(
- ADC_CHANNEL_3V0_22K6_47K_4050B, &temp),
- NULL);
+
+ for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE;
+ sensor_idx++) {
+ const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx];
+
+ zassert_equal(EC_ERROR_NOT_POWERED, sensor->read(sensor, &temp),
+ "%s failed", sensor->name);
+ }
/* pg_ec_dsw_pwrok = 1 means ADC is powered. */
zassert_ok(gpio_emul_input_set(gpio_dev, GPIO_PG_EC_DSW_PWROK_PORT, 1),
NULL);
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_13k7_47k_4050b(
- ADC_CHANNEL_3V3_13K7_47K_4050B, &temp),
- NULL);
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_30k9_47k_4050b(
- ADC_CHANNEL_3V3_30K9_47K_4050B, &temp),
- NULL);
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_51k1_47k_4050b(
- ADC_CHANNEL_3V3_51K1_47K_4050B, &temp),
- NULL);
- zassert_equal(EC_SUCCESS,
- get_temp_3v0_22k6_47k_4050b(
- ADC_CHANNEL_3V0_22K6_47K_4050B, &temp),
- NULL);
+ for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE;
+ sensor_idx++) {
+ const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx];
+
+ zassert_equal(EC_SUCCESS, sensor->read(sensor, &temp),
+ "%s failed", sensor->name);
+ }
}
-/** Simple ADC emulator custom function which always return error */
+/* Simple ADC emulator custom function which always return error */
static int adc_error_func(const struct device *dev, unsigned int channel,
void *param, uint32_t *result)
{
@@ -122,49 +98,35 @@ static int adc_error_func(const struct device *dev, unsigned int channel,
/** Test if get temp function return expected error on ADC malfunction */
static void test_thermistor_adc_read_error(void)
{
- const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
int temp;
+ int sensor_idx;
+
+ const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
zassert_not_null(adc_dev, "Cannot get ADC device");
/* Return error on all ADC channels */
- zassert_ok(adc_emul_value_func_set(adc_dev,
- ADC_CHANNEL_3V3_13K7_47K_4050B,
- adc_error_func, NULL),
- "adc_emul_value_func_set() failed");
- zassert_ok(adc_emul_value_func_set(adc_dev,
- ADC_CHANNEL_3V3_30K9_47K_4050B,
- adc_error_func, NULL),
- "adc_emul_value_func_set() failed");
- zassert_ok(adc_emul_value_func_set(adc_dev,
- ADC_CHANNEL_3V3_51K1_47K_4050B,
- adc_error_func, NULL),
- "adc_emul_value_func_set() failed");
- zassert_ok(adc_emul_value_func_set(adc_dev,
- ADC_CHANNEL_3V0_22K6_47K_4050B,
- adc_error_func, NULL),
- "adc_emul_value_func_set() failed");
-
- zassert_equal(EC_ERROR_UNKNOWN,
- get_temp_3v3_13k7_47k_4050b(
- ADC_CHANNEL_3V3_13K7_47K_4050B, &temp),
- NULL);
- zassert_equal(EC_ERROR_UNKNOWN,
- get_temp_3v3_30k9_47k_4050b(
- ADC_CHANNEL_3V3_30K9_47K_4050B, &temp),
- NULL);
- zassert_equal(EC_ERROR_UNKNOWN,
- get_temp_3v3_51k1_47k_4050b(
- ADC_CHANNEL_3V3_51K1_47K_4050B, &temp),
- NULL);
- zassert_equal(EC_ERROR_UNKNOWN,
- get_temp_3v0_22k6_47k_4050b(
- ADC_CHANNEL_3V0_22K6_47K_4050B, &temp),
- NULL);
+ for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE;
+ sensor_idx++) {
+ const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx];
+
+ zassert_ok(adc_emul_value_func_set(adc_dev, sensor->idx,
+ adc_error_func, NULL),
+ "adc_emul_value_func_set() failed on %s",
+ sensor->name);
+ }
+
+ for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE;
+ sensor_idx++) {
+ const struct temp_sensor_t *sensor = &temp_sensors[sensor_idx];
+
+ zassert_equal(EC_ERROR_UNKNOWN, sensor->read(sensor, &temp),
+ "%s failed", sensor->name);
+ }
}
/** Get resistance of thermistor for given temperature */
-static int resistance(int t)
+static int resistance_47kohm_B4050(int t)
{
/* Thermistor manufacturer resistance lookup table*/
int r_table[] = {
@@ -222,213 +184,94 @@ static int adc_temperature_func(const struct device *dev, unsigned int channel,
{
struct thermistor_state *s = (struct thermistor_state *)param;
- *result = volt_divider(s->v, s->r, resistance(s->temp_expected));
+ *result = volt_divider(s->v,
+ s->r,
+ resistance_47kohm_B4050(s->temp_expected));
return 0;
}
/** Test conversion from ADC raw value to temperature */
-static void test_thermistor_3v3_13k7_47k_4050b(void)
+static void do_thermistor_test(const struct temp_sensor_t *temp_sensor,
+ int reference_mv, int reference_ohms)
{
- const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
- struct thermistor_state state = {
- .v = 3300,
- .r = 13700,
- };
int temp_expected;
int temp;
- zassert_not_null(adc_dev, "Cannot get ADC device");
-
- /* Setup ADC channel */
- zassert_ok(adc_emul_value_func_set(adc_dev,
- ADC_CHANNEL_3V3_13K7_47K_4050B,
- adc_temperature_func, &state),
- "adc_emul_value_func_set() failed");
-
- /* Makes sure that reference voltage is correct for given thermistor */
- zassert_ok(adc_emul_ref_voltage_set(adc_dev, ADC_REF_INTERNAL, state.v),
- "adc_emul_ref_voltage_set() failed");
-
- /* Test whole supported range from 0*C to 100*C (273*K to 373*K) */
- for (temp_expected = 273; temp_expected <= 373; temp_expected++) {
- state.temp_expected = temp_expected;
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_13k7_47k_4050b(
- ADC_CHANNEL_3V3_13K7_47K_4050B, &temp),
- NULL);
- zassert_within(temp_expected, temp, TEMP_EPS,
- "Expected %d*K, got %d*K", temp_expected, temp);
- }
-
- /* Temperatures below 0*C should be reported as 0*C */
- state.temp_expected = -15 + 273;
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_13k7_47k_4050b(
- ADC_CHANNEL_3V3_13K7_47K_4050B, &temp),
- NULL);
- zassert_equal(273, temp, "Expected %d*K, got %d*K", 273, temp);
-
- /* Temperatures above 100*C should be reported as 100*C */
- state.temp_expected = 115 + 273;
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_13k7_47k_4050b(
- ADC_CHANNEL_3V3_13K7_47K_4050B, &temp),
- NULL);
- zassert_equal(373, temp, "Expected %d*K, got %d*K", 373, temp);
-}
-
-/** Test conversion from ADC raw value to temperature */
-static void test_thermistor_3v3_30k9_47k_4050b(void)
-{
const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
struct thermistor_state state = {
- .v = 3300,
- .r = 30900,
+ .v = reference_mv,
+ .r = reference_ohms,
};
- int temp_expected;
- int temp;
zassert_not_null(adc_dev, "Cannot get ADC device");
/* Setup ADC channel */
zassert_ok(adc_emul_value_func_set(adc_dev,
- ADC_CHANNEL_3V3_30K9_47K_4050B,
+ temp_sensor->idx,
adc_temperature_func, &state),
- "adc_emul_value_func_set() failed");
+ "adc_emul_value_func_set() failed on %s", temp_sensor->name);
/* Makes sure that reference voltage is correct for given thermistor */
zassert_ok(adc_emul_ref_voltage_set(adc_dev, ADC_REF_INTERNAL, state.v),
- "adc_emul_ref_voltage_set() failed");
+ "adc_emul_ref_voltage_set() failed %s on ",
+ temp_sensor->name);
/* Test whole supported range from 0*C to 100*C (273*K to 373*K) */
for (temp_expected = 273; temp_expected <= 373; temp_expected++) {
state.temp_expected = temp_expected;
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_30k9_47k_4050b(
- ADC_CHANNEL_3V3_30K9_47K_4050B, &temp),
- NULL);
+ zassert_equal(EC_SUCCESS, temp_sensor->read(temp_sensor, &temp),
+ "failed on %s", temp_sensor->name);
zassert_within(temp_expected, temp, TEMP_EPS,
- "Expected %d*K, got %d*K", temp_expected, temp);
+ "Expected %d*K, got %d*K on %s", temp_expected,
+ temp, temp_sensor->name);
}
/* Temperatures below 0*C should be reported as 0*C */
state.temp_expected = -15 + 273;
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_30k9_47k_4050b(
- ADC_CHANNEL_3V3_30K9_47K_4050B, &temp),
- NULL);
- zassert_equal(273, temp, "Expected %d*K, got %d*K", 273, temp);
+ zassert_equal(EC_SUCCESS, temp_sensor->read(temp_sensor, &temp),
+ "failed on %s", temp_sensor->name);
+ zassert_equal(273, temp, "Expected %d*K, got %d*K on %s", 273, temp,
+ temp_sensor->name);
/* Temperatures above 100*C should be reported as 100*C */
state.temp_expected = 115 + 273;
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_30k9_47k_4050b(
- ADC_CHANNEL_3V3_30K9_47K_4050B, &temp),
- NULL);
- zassert_equal(373, temp, "Expected %d*K, got %d*K", 373, temp);
+ zassert_equal(EC_SUCCESS, temp_sensor->read(temp_sensor, &temp),
+ "failed on %s", temp_sensor->name);
+ zassert_equal(373, temp, "Expected %d*K, got %d*K on %s", 373, temp,
+ temp_sensor->name);
}
-/** Test conversion from ADC raw value to temperature */
-static void test_thermistor_3v3_51k1_47k_4050b(void)
-{
- const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
- struct thermistor_state state = {
- .v = 3300,
- .r = 51100,
- };
- int temp_expected;
- int temp;
-
- zassert_not_null(adc_dev, "Cannot get ADC device");
+#define GET_THERMISTOR_REF_MV(node_id) \
+ [ZSHIM_TEMP_SENSOR_ID(node_id)] = DT_PROP( \
+ DT_PHANDLE(node_id, thermistor), steinhart_reference_mv),
- /* Setup ADC channel */
- zassert_ok(adc_emul_value_func_set(adc_dev,
- ADC_CHANNEL_3V3_51K1_47K_4050B,
- adc_temperature_func, &state),
- "adc_emul_value_func_set() failed");
-
- /* Makes sure that reference voltage is correct for given thermistor */
- zassert_ok(adc_emul_ref_voltage_set(adc_dev, ADC_REF_INTERNAL, state.v),
- "adc_emul_ref_voltage_set() failed");
+#define GET_THERMISTOR_REF_RES(node_id) \
+ [ZSHIM_TEMP_SENSOR_ID(node_id)] = DT_PROP( \
+ DT_PHANDLE(node_id, thermistor), steinhart_reference_res),
- /* Test whole supported range from 0*C to 100*C (273*K to 373*K) */
- for (temp_expected = 273; temp_expected <= 373; temp_expected++) {
- state.temp_expected = temp_expected;
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_51k1_47k_4050b(
- ADC_CHANNEL_3V3_51K1_47K_4050B, &temp),
- NULL);
- zassert_within(temp_expected, temp, TEMP_EPS,
- "Expected %d*K, got %d*K", temp_expected, temp);
- }
+static void test_thermistors_adc_temperature_conversion(void)
+{
+ int sensor_idx;
- /* Temperatures below 0*C should be reported as 0*C */
- state.temp_expected = -15 + 273;
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_51k1_47k_4050b(
- ADC_CHANNEL_3V3_51K1_47K_4050B, &temp),
- NULL);
- zassert_equal(273, temp, "Expected %d*K, got %d*K", 273, temp);
+ const static int reference_mv_arr[] = { DT_FOREACH_STATUS_OKAY(
+ cros_temp_sensor, GET_THERMISTOR_REF_MV) };
+ const static int reference_res_arr[] = { DT_FOREACH_STATUS_OKAY(
+ cros_temp_sensor, GET_THERMISTOR_REF_RES) };
- /* Temperatures above 100*C should be reported as 100*C */
- state.temp_expected = 115 + 273;
- zassert_equal(EC_SUCCESS,
- get_temp_3v3_51k1_47k_4050b(
- ADC_CHANNEL_3V3_51K1_47K_4050B, &temp),
- NULL);
- zassert_equal(373, temp, "Expected %d*K, got %d*K", 373, temp);
+ for (sensor_idx = 0; sensor_idx < NAMED_TEMP_SENSORS_SIZE; sensor_idx++)
+ do_thermistor_test(&temp_sensors[sensor_idx],
+ reference_mv_arr[sensor_idx],
+ reference_res_arr[sensor_idx]);
}
-/** Test conversion from ADC raw value to temperature */
-static void test_thermistor_3v0_22k6_47k_4050b(void)
+static void test_device_nodes_enabled(void)
{
- const struct device *adc_dev = DEVICE_DT_GET(ADC_DEVICE_NODE);
- struct thermistor_state state = {
- .v = 3000,
- .r = 22600,
- };
- int temp_expected;
- int temp;
-
- zassert_not_null(adc_dev, "Cannot get ADC device");
-
- /* Setup ADC channel */
- zassert_ok(adc_emul_value_func_set(adc_dev,
- ADC_CHANNEL_3V0_22K6_47K_4050B,
- adc_temperature_func, &state),
- "adc_emul_value_func_set() failed");
+ zassert_equal(NAMED_TEMP_SENSORS_SIZE, TEMP_SENSORS_ENABLED_SIZE,
+ "Temperature sensors in device tree and "
+ "those enabled for test differ");
- /* Makes sure that reference voltage is correct for given thermistor */
- zassert_ok(adc_emul_ref_voltage_set(adc_dev, ADC_REF_INTERNAL, state.v),
- "adc_emul_ref_voltage_set() failed");
-
- /* Test whole supported range from 0*C to 100*C (273*K to 373*K) */
- for (temp_expected = 273; temp_expected <= 373; temp_expected++) {
- state.temp_expected = temp_expected;
- zassert_equal(EC_SUCCESS,
- get_temp_3v0_22k6_47k_4050b(
- ADC_CHANNEL_3V0_22K6_47K_4050B, &temp),
- NULL);
- zassert_within(temp_expected, temp, TEMP_EPS,
- "Expected %d*K, got %d*K", temp_expected, temp);
- }
-
- /* Temperatures below 0*C should be reported as 0*C */
- state.temp_expected = -15 + 273;
- zassert_equal(EC_SUCCESS,
- get_temp_3v0_22k6_47k_4050b(
- ADC_CHANNEL_3V0_22K6_47K_4050B, &temp),
- NULL);
- zassert_equal(273, temp, "Expected %d*K, got %d*K", 273, temp);
-
- /* Temperatures above 100*C should be reported as 100*C */
- state.temp_expected = 115 + 273;
- zassert_equal(EC_SUCCESS,
- get_temp_3v0_22k6_47k_4050b(
- ADC_CHANNEL_3V0_22K6_47K_4050B, &temp),
- NULL);
- zassert_equal(373, temp, "Expected %d*K, got %d*K", 373, temp);
+ /* Thermistor nodes being enabled are already tested by compilation. */
}
void test_suite_thermistor(void)
@@ -442,15 +285,11 @@ void test_suite_thermistor(void)
NULL);
ztest_test_suite(thermistor,
+ ztest_user_unit_test(test_device_nodes_enabled),
ztest_user_unit_test(test_thermistor_power_pin),
ztest_user_unit_test(test_thermistor_adc_read_error),
ztest_user_unit_test(
- test_thermistor_3v3_13k7_47k_4050b),
- ztest_user_unit_test(
- test_thermistor_3v3_30k9_47k_4050b),
- ztest_user_unit_test(
- test_thermistor_3v3_51k1_47k_4050b),
- ztest_user_unit_test(
- test_thermistor_3v0_22k6_47k_4050b));
+ test_thermistors_adc_temperature_conversion));
+
ztest_run_test_suite(thermistor);
}