Skip to content

Commit

Permalink
sensor: Add support for defining sensor channel in DT
Browse files Browse the repository at this point in the history
This functionality is needed at least in the case of temperature
sensors. Zephyr specifies three temperature related channels:

- SENSOR_CHAN_DIE_TEMP
- SENSOR_CHAN_AMBIENT_TEMP
- SENSOR_CHAN_GAUGE_TEMP

In addition to that some devices can report temperature from multiple
probes. This normally handled by using a sensor specific channels.

Right now the consumers of such devices need to hardcode the sensor
channel in their logic. Ideally the sensor channel would to use would be
defined in DT. This is necessary to make a "generic" application that
would work with a variety of different sensors. In the future we might
also want to introduce a thermal management framework similar to thermal
zones implemented in Linux. Such logic would need a way to specify a
sensor channel in DT too.

The "sensor-cells" property is marked as optional to retain backward
compatibility with existing sensor DT nodes. For now only temperature
related channels have been redefined in the dt-bindings header.
Note that since dtc can't parse enums, the channel values had to be
redefined as macros.

Signed-off-by: Kornel Dulęba <mindal@semihalf.com>
  • Loading branch information
kornelduleba committed Aug 4, 2023
1 parent 4b9dddc commit 2810e96
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dts/bindings/sensor/sensor-device.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ properties:
This property is defined in the Generic Sensor Property Usages of the HID
Usage Tables specification
(https://usb.org/sites/default/files/hut1_3_0.pdf, section 22.5).
"#sensor-cells":
type: int
required: false
description: |
Number of items to expect in a sensor specifier.
By default it's only one attribute is expected - a sensor channel to use.
sensor-cells:
- channel
6 changes: 6 additions & 0 deletions include/zephyr/drivers/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <zephyr/sys/iterable_sections.h>
#include <zephyr/types.h>

#include <zephyr/dt-bindings/sensor/sensor.h>

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -202,6 +204,10 @@ enum sensor_channel {
SENSOR_CHAN_MAX = INT16_MAX,
};

BUILD_ASSERT(DT_SENSOR_CHAN_DIE_TEMP == SENSOR_CHAN_DIE_TEMP);
BUILD_ASSERT(DT_SENSOR_CHAN_AMBIENT_TEMP == SENSOR_CHAN_AMBIENT_TEMP);
BUILD_ASSERT(DT_SENSOR_CHAN_GAUGE_TEMP == SENSOR_CHAN_GAUGE_TEMP);

/**
* @brief Sensor trigger types.
*/
Expand Down
16 changes: 16 additions & 0 deletions include/zephyr/dt-bindings/sensor/sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2023 Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DT_BINDINGS_SENSOR_H_
#define ZEPHYR_INCLUDE_DT_BINDINGS_SENSOR_H_

#include <zephyr/dt-bindings/dt-util.h>

/* The values were copied over from drivers/sensor.h */
#define DT_SENSOR_CHAN_DIE_TEMP 12
#define DT_SENSOR_CHAN_AMBIENT_TEMP 13
#define DT_SENSOR_CHAN_GAUGE_TEMP 43

#endif

0 comments on commit 2810e96

Please sign in to comment.