From f8f4e0c36a674963c725f8876728d23ece8cc3ff Mon Sep 17 00:00:00 2001 From: Hyungwoo Yang Date: Fri, 19 Mar 2021 18:20:27 -0700 Subject: zephyr: DT: support sensor driver data This has basic support to create instances of sensor-specific driver data using the information from device tree. BUG=b:173507858 BRANCH=none TEST=make buildall -j8 build volteer on zephyr Signed-off-by: Hyungwoo Yang Change-Id: I196d7595e8bd880e2314290660f08274ad99e172 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2778815 Reviewed-by: Simon Glass --- .../cros-ec,accelgyro-als-channel-scale.yaml | 29 +++++ .../drvdata/cros-ec,accelgyro-als-drv-data.yaml | 44 +++++++ .../drvdata/cros-ec,accelgyro-rgb-calibration.yaml | 63 ++++++++++ .../bindings/motionsense/drvdata/drvdata-base.yaml | 11 ++ .../src/motionsense_driver/drvdata-accelgyro.h | 127 +++++++++++++++++++++ .../src/motionsense_driver/sensor_drv_list.inc | 19 +++ zephyr/shim/src/motionsense_sensors.c | 55 +++++++++ 7 files changed, 348 insertions(+) create mode 100644 zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-als-channel-scale.yaml create mode 100644 zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-als-drv-data.yaml create mode 100644 zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-rgb-calibration.yaml create mode 100644 zephyr/dts/bindings/motionsense/drvdata/drvdata-base.yaml create mode 100644 zephyr/shim/src/motionsense_driver/drvdata-accelgyro.h create mode 100644 zephyr/shim/src/motionsense_driver/sensor_drv_list.inc diff --git a/zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-als-channel-scale.yaml b/zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-als-channel-scale.yaml new file mode 100644 index 0000000000..753edc7ea8 --- /dev/null +++ b/zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-als-channel-scale.yaml @@ -0,0 +1,29 @@ +# 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: | + Bindings for als_channel_scale_t in accelgyro.h. + Each channel has scaling factor for normalization & cover + +compatible: cros-ec,accelgyro-als-channel-scale + +properties: + k-channel-scale: + type: int + required: true + description: Channel scale factor + cover-scale: + type: int + required: true + description: Cover compensation scale factor + +# +# examples: +# +# als-channel-scale { +# compatible = "cros-ec,accelgyro-als-channel-scale"; +# k-channel-scale = <1>; +# cover-scale = <1>; +# }; +# diff --git a/zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-als-drv-data.yaml b/zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-als-drv-data.yaml new file mode 100644 index 0000000000..7d64689cf2 --- /dev/null +++ b/zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-als-drv-data.yaml @@ -0,0 +1,44 @@ +# 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: ALS driver data + +compatible: "cros-ec,accelgyro-als-drv-data" + +child-binding: + description: ALS calibration data + properties: + scale: + type: int + required: true + description: | + Scale, uscale, and offset are used to correct the raw 16 bit ALS data + and then to convert it to 32 bit using the following equations + raw_value += offset;\n + adjusted_value = raw_value * scale + raw_value * uscale / 10000; + uscale: + type: int + required: true + description: uscale + offset: + type: int + required: true + description: offset + +# +# examples: +# als-drv-data { +# compatible = "cros-ec,accelgyro-als-drv-data"; +# als-cal { +# scale = <1>; +# uscale = <0>; +# offset = <0>; +# als-channel-scale { +# compatible = "cros-ec,accelgyro-als-channel-scale"; +# k-channel-scale = <1>; +# cover-scale = <1>; +# }; +# }; +# }; +# diff --git a/zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-rgb-calibration.yaml b/zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-rgb-calibration.yaml new file mode 100644 index 0000000000..4204a63cff --- /dev/null +++ b/zephyr/dts/bindings/motionsense/drvdata/cros-ec,accelgyro-rgb-calibration.yaml @@ -0,0 +1,63 @@ +# 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: bindings for rgb_calibration_t in accelgyro.h + +compatible: "cros-ec,accelgyro-rgb-calibration" + +properties: + irt: + type: int + required: true + description: incandecent scaling factor + +child-binding: + description: RGB ALS Calibration Data + properties: + offset: + type: int + required: true + description: Any offset to add to raw channel data + coeff: + type: array + required: true + description: Clear, R, G, and B coefficients for this channel + +# +# examples: +# +# rgb_calibration { +# compatible = "cros-ec,accelgyro-rgb-calibration"; +# +# irt = <1>; +# +# rgb-cal-x { +# offset = <0>; +# coeff = <0 0 0 0>; +# als-channel-scale { +# compatible = "cros-ec,accelgyro-als-channel-scale"; +# k-channel-scale = <1>; +# cover-scale = <1>; +# }; +# }; +# rgb-cal-y { +# offset = <0>; +# coeff = <0 0 0 0>; +# als-channel-scale { +# compatible = "cros-ec,accelgyro-als-channel-scale"; +# k-channel-scale = <1>; +# cover-scale = <1>; +# }; +# }; +# rgb-cal-z { +# offset = <0>; +# coeff = <0 0 0 0>; +# als-channel-scale { +# compatible = "cros-ec,accelgyro-als-channel-scale"; +# k-channel-scale = <1>; +# cover-scale = <1>; +# }; +# }; +# }; +# diff --git a/zephyr/dts/bindings/motionsense/drvdata/drvdata-base.yaml b/zephyr/dts/bindings/motionsense/drvdata/drvdata-base.yaml new file mode 100644 index 0000000000..dc32d69d21 --- /dev/null +++ b/zephyr/dts/bindings/motionsense/drvdata/drvdata-base.yaml @@ -0,0 +1,11 @@ +# 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 fields for driver data + +include: base.yaml + +properties: + status: + required: true diff --git a/zephyr/shim/src/motionsense_driver/drvdata-accelgyro.h b/zephyr/shim/src/motionsense_driver/drvdata-accelgyro.h new file mode 100644 index 0000000000..069587f90f --- /dev/null +++ b/zephyr/shim/src/motionsense_driver/drvdata-accelgyro.h @@ -0,0 +1,127 @@ +/* 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. + */ + +/* + * Macros are to help creating driver data. A driver data that uses + * any data structures defined in accelgyro.h should use the macros here + * to utilize the information in device tree. + * + */ +#ifndef __ZEPHYR_SHIM_SRC_MOTIONSENSE_DRIVER_DRVDATA_ACCELGYRO_H +#define __ZEPHYR_SHIM_SRC_MOTIONSENSE_DRIVER_DRVDATA_ACCELGYRO_H + +/* + * compatible = "cros-ec,accelgyro-als-channel-scale" + * als_channel_scale_t in accelgyro.h + * + * e.g) The following is the example in DT for als_channel_scale_t + * als-channel-scale { + * compatible = "cros-ec,accelgyro-als-channel-scale"; + * k-channel-scale = <1>; + * cover-scale = <1>; + * }; + */ +#define ACCELGYRO_ALS_CHANNEL_SCALE(id) \ + { \ + .k_channel_scale = \ + ALS_CHANNEL_SCALE(DT_PROP(id, k_channel_scale)),\ + .cover_scale = \ + ALS_CHANNEL_SCALE(DT_PROP(id, cover_scale)), \ + } + +#define ALS_CALIBRATION_CHANNEL_SCALE(id) \ + .als_cal.channel_scale = ACCELGYRO_ALS_CHANNEL_SCALE(id), + +#define ALS_CALIBRATION_SET(id) \ + .als_cal.scale = DT_PROP(id, scale), \ + .als_cal.uscale = DT_PROP(id, uscale), \ + .als_cal.offset = DT_PROP(id, offset), \ + ALS_CALIBRATION_CHANNEL_SCALE(DT_CHILD(id, als_channel_scale)) + +/* + * compatible = "cros-ec,accelgyro-als-drv-data" + * als_drv_data_t in accelgyro.h + * + * e.g) The following is the example in DT for als_drv_data_t + * als-drv-data { + * compatible = "cros-ec,accelgyro-als-drv-data"; + * als-cal { + * scale = <1>; + * uscale = <0>; + * offset = <0>; + * als-channel-scale { + * compatible = "cros-ec,accelgyro-als-channel-scale"; + * k-channel-scale = <1>; + * cover-scale = <1>; + * }; + * }; + * }; + */ +#define ACCELGYRO_ALS_DRV_DATA(id) \ + { \ + ALS_CALIBRATION_SET(DT_CHILD(id, als_cal)) \ + } + +#define RGB_CAL_RGB_SET_SCALE(id) \ + .scale = ACCELGYRO_ALS_CHANNEL_SCALE(id), + +#define RGB_CAL_RGB_SET_ONE(id, suffix) \ + .rgb_cal[suffix] = { \ + .offset = DT_PROP(id, offset), \ + .coeff[0] = FLOAT_TO_FP(DT_PROP_BY_IDX(id, coeff, 0)), \ + .coeff[1] = FLOAT_TO_FP(DT_PROP_BY_IDX(id, coeff, 1)), \ + .coeff[2] = FLOAT_TO_FP(DT_PROP_BY_IDX(id, coeff, 2)), \ + .coeff[3] = FLOAT_TO_FP(DT_PROP_BY_IDX(id, coeff, 3)), \ + RGB_CAL_RGB_SET_SCALE(DT_CHILD(id, als_channel_scale)) \ + }, + +/* + * compatible = "cros-ec,accelgyro-rgb-calibration" + * rgb_calibration_t in accelgyro.h + * + * e.g) The following is the example in DT for rgb_calibration_t + * rgb_calibration { + * compatible = "cros-ec,accelgyro-rgb-calibration"; + * + * irt = <1>; + * + * rgb-cal-x { + * offset = <0>; + * coeff = <0 0 0 0>; + * als-channel-scale { + * compatible = "cros-ec,accelgyro-als-channel-scale"; + * k-channel-scale = <1>; + * cover-scale = <1>; + * }; + * }; + * rgb-cal-y { + * offset = <0>; + * coeff = <0 0 0 0>; + * als-channel-scale { + * compatible = "cros-ec,accelgyro-als-channel-scale"; + * k-channel-scale = <1>; + * cover-scale = <1>; + * }; + * }; + * rgb-cal-z { + * offset = <0>; + * coeff = <0 0 0 0>; + * als-channel-scale { + * compatible = "cros-ec,accelgyro-als-channel-scale"; + * k-channel-scale = <1>; + * cover-scale = <1>; + * }; + * }; + * }; + */ +#define ACCELGYRO_RGB_CALIBRATION(id) \ + { \ + RGB_CAL_RGB_SET_ONE(DT_CHILD(id, rgb_cal_x), X) \ + RGB_CAL_RGB_SET_ONE(DT_CHILD(id, rgb_cal_y), Y) \ + RGB_CAL_RGB_SET_ONE(DT_CHILD(id, rgb_cal_z), Z) \ + .irt = INT_TO_FP(DT_PROP(id, irt)), \ + } + +#endif /* __ZEPHYR_SHIM_SRC_MOTIONSENSE_DRIVER_DRVDATA_ACCELGYRO_H */ diff --git a/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc b/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc new file mode 100644 index 0000000000..f1e918bc1b --- /dev/null +++ b/zephyr/shim/src/motionsense_driver/sensor_drv_list.inc @@ -0,0 +1,19 @@ +/* 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. + */ + +/* + * A driver should create -drvinfo.inc to create + * driver-specific data and an motion sensor entry in + * motion_sensors array that are used by motion sense task. + * + * This file includes the .inc file and is used by motionsense_sensrs.c to + * create the sensor driver data and the entries in mostion_sensors array. + * + * e.g) bma255-drvinfo.inc is provided for BMA255 chip + * + * #ifdef CONFIG_ACCEL_BMA255 + * #include "bma255-drvinfo.inc" + * #endif + */ diff --git a/zephyr/shim/src/motionsense_sensors.c b/zephyr/shim/src/motionsense_sensors.c index 9e8eef6f6f..bba38e9eec 100644 --- a/zephyr/shim/src/motionsense_sensors.c +++ b/zephyr/shim/src/motionsense_sensors.c @@ -4,6 +4,7 @@ */ #include "common.h" +#include "accelgyro.h" #define SENSOR_MUTEX_NODE DT_PATH(motionsense_mutex) #define SENSOR_MUTEX_NAME(id) DT_CAT(MUTEX_, id) @@ -57,3 +58,57 @@ SYS_INIT(init_sensor_mutex, POST_KERNEL, 50); #if DT_NODE_EXISTS(SENSOR_ROT_REF_NODE) DT_FOREACH_CHILD(SENSOR_ROT_REF_NODE, DECLARE_SENSOR_ROT_REF) #endif + +/* + * Declare sensor driver data for + * each child node with status = "okay" of + * "/motionsense-sensor-data" node in DT. + * + * A driver data can be shared among the motion sensors. + */ +#define SENSOR_DATA_NAME(id) DT_CAT(SENSOR_DAT_, id) +#define SENSOR_DATA_NODE DT_PATH(motionsense_sensor_data) + +#define SENSOR_DATA(inst, compat, create_data_macro) \ + create_data_macro(DT_INST(inst, compat), \ + SENSOR_DATA_NAME(DT_INST(inst, compat))) + +/* + * CREATE_SENSOR_DATA is a helper macro that gets + * compat and create_data_macro as parameters. + * + * For each node with compatible = "compat", + * CREATE_SENSOR_DATA expands "create_data_macro" macro with the node id and + * the designated name for the sensor driver data to be created. The + * "create_datda_macro" macro is responsible for creating the sensor driver + * data with the name. + * + * Sensor drivers should provide -drvinfo.inc file and, in the file, + * it should have the macro that creates its sensor driver data using device + * tree and pass the macro via CREATE_SENSOR_DATA. + * + * e.g) The below is contents of tcs3400-drvinfo.inc file. The file has + * CREATE_SENSOR_DATA_TCS3400_CLEAR that creates the static instance of + * "struct als_drv_data_t" with the given name and initializes it + * with device tree. Then use CREATE_SENSOR_DATA. + * + * ----------- bma255-drvinfo.inc ----------- + * #define CREATE_SENSOR_DATA_TCS3400_CLEAR(id, drvdata_name) \ + * static struct als_drv_data_t drvdata_name = \ + * ACCELGYRO_ALS_DRV_DATA(DT_CHILD(id, als_drv_data)); + * + * CREATE_SENSOR_DATA(cros_ec_drvdata_tcs3400_clear, \ + * CREATE_SENSOR_DATA_TCS3400_CLEAR) + */ +#define CREATE_SENSOR_DATA(compat, create_data_macro) \ + UTIL_LISTIFY(DT_NUM_INST_STATUS_OKAY(compat), SENSOR_DATA, \ + compat, create_data_macro) + +/* + * Here, we declare all sensor driver data. How to create the data is + * defined in -drvinfo.inc file and ,in turn, the file is included + * in sensor_drv_list.inc. + */ +#if DT_NODE_EXISTS(SENSOR_DATA_NODE) +#include "motionsense_driver/sensor_drv_list.inc" +#endif -- cgit v1.2.1