-
Notifications
You must be signed in to change notification settings - Fork 8.2k
drivers: sensor: add temp sensor for sf32lb #99463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4a3dee4
dts: bindings: sensor: add sifli,sf32lb-tsen
ck-telecom 38e9ddf
dts: arm: sifli: sf32lb52x: define temp sensor
ck-telecom 72c77b2
drivers: sensor: sifli: add sf32lb temp sensor
ck-telecom 6d4bf22
boards: sifli: sf32lb52_devkit_lcd: enable temp sensor
ck-telecom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,3 +149,7 @@ | |
| pinctrl-names = "default"; | ||
| clock-frequency = <I2C_BITRATE_FAST>; | ||
| }; | ||
|
|
||
| temp_sensor: &tsen { | ||
| status = "okay"; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) 2025, Qingsong Gou <gouqs@hotmail.com> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| add_subdirectory_ifdef(CONFIG_SF32LB_TSEN sf32lb_tsen) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) 2025, Qingsong Gou <gouqs@hotmail.com> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| source "drivers/sensor/sifli/sf32lb_tsen/Kconfig" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Copyright (c) 2025, Qingsong Gou <gouqs@hotmail.com> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| zephyr_library() | ||
|
|
||
| zephyr_library_sources(sf32lb_tsen.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Copyright (c) 2025, Qingsong Gou <gouqs@hotmail.com> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| config SF32LB_TSEN | ||
| bool "SIFLI SF32LB TSEN temperature sensor driver" | ||
| default y | ||
| depends on DT_HAS_SIFLI_SF32LB_TSEN_ENABLED | ||
| help | ||
| Enable driver for the SIFLI SF32LB on-chip temperature sensor (TSEN). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * Copyright (c) 2025, Qingsong Gou <gouqs@hotmail.com> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #define DT_DRV_COMPAT sifli_sf32lb_tsen | ||
|
|
||
| #include <zephyr/kernel.h> | ||
| #include <zephyr/device.h> | ||
| #include <zephyr/drivers/clock_control/sf32lb.h> | ||
| #include <zephyr/drivers/sensor.h> | ||
| #include <zephyr/logging/log.h> | ||
|
|
||
| #include <register.h> | ||
|
|
||
| #define TSEN_CTRL_REG offsetof(TSEN_TypeDef, TSEN_CTRL_REG) | ||
| #define TSEN_RDATA offsetof(TSEN_TypeDef, TSEN_RDATA) | ||
| #define TSEN_IRQ offsetof(TSEN_TypeDef, TSEN_IRQ) | ||
|
|
||
| #define SYS_CFG_ANAU_CR offsetof(HPSYS_CFG_TypeDef, ANAU_CR) | ||
|
|
||
| LOG_MODULE_REGISTER(sf32lb_tsen, CONFIG_SENSOR_LOG_LEVEL); | ||
|
|
||
| struct sf32lb_tsen_config { | ||
| uintptr_t base; | ||
| uintptr_t cfg_base; | ||
| struct sf32lb_clock_dt_spec clock; | ||
| }; | ||
|
|
||
| struct sf32lb_tsen_data { | ||
| struct k_mutex mutex; | ||
| uint32_t last_temp; | ||
| }; | ||
|
|
||
| static int sf32lb_tsen_sample_fetch(const struct device *dev, enum sensor_channel chan) | ||
| { | ||
| const struct sf32lb_tsen_config *config = dev->config; | ||
| struct sf32lb_tsen_data *data = dev->data; | ||
|
|
||
| k_mutex_lock(&data->mutex, K_FOREVER); | ||
|
|
||
| while (!sys_test_bit(config->base + TSEN_IRQ, TSEN_TSEN_IRQ_TSEN_IRSR_Pos)) { | ||
| k_msleep(1); | ||
| } | ||
|
|
||
| data->last_temp = sys_read32(config->base + TSEN_RDATA); | ||
|
|
||
| sys_set_bit(config->base + TSEN_IRQ, TSEN_TSEN_IRQ_TSEN_ICR_Pos); | ||
|
|
||
| k_mutex_unlock(&data->mutex); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int sf32lb_tsen_channel_get(const struct device *dev, enum sensor_channel chan, | ||
| struct sensor_value *val) | ||
| { | ||
| struct sf32lb_tsen_data *data = dev->data; | ||
| float temp; | ||
|
|
||
| if (chan != SENSOR_CHAN_DIE_TEMP) { | ||
| return -ENOTSUP; | ||
| } | ||
|
|
||
| temp = ((int32_t)data->last_temp + 3000) * 749.2916 / 10100 - 277; /* see manual 8.2.3.2 */ | ||
|
|
||
| return sensor_value_from_float(val, temp); | ||
| } | ||
|
|
||
| static DEVICE_API(sensor, sf32lb_tsen_driver_api) = { | ||
| .sample_fetch = sf32lb_tsen_sample_fetch, | ||
| .channel_get = sf32lb_tsen_channel_get, | ||
| }; | ||
|
|
||
| static int sf32lb_tsen_init(const struct device *dev) | ||
| { | ||
| const struct sf32lb_tsen_config *config = dev->config; | ||
| struct sf32lb_tsen_data *data = dev->data; | ||
| int ret; | ||
|
|
||
| if (!sf32lb_clock_is_ready_dt(&config->clock)) { | ||
| return -ENODEV; | ||
| } | ||
|
|
||
| ret = sf32lb_clock_control_on_dt(&config->clock); | ||
| if (ret < 0) { | ||
| return ret; | ||
| } | ||
|
|
||
| if (!sys_test_bit(config->cfg_base + SYS_CFG_ANAU_CR, HPSYS_CFG_ANAU_CR_EN_BG_Pos)) { | ||
| sys_set_bit(config->cfg_base + SYS_CFG_ANAU_CR, HPSYS_CFG_ANAU_CR_EN_BG_Pos); | ||
| } | ||
|
|
||
| sys_clear_bit(config->base + TSEN_CTRL_REG, TSEN_TSEN_CTRL_REG_ANAU_TSEN_RSTB_Pos); | ||
| sys_set_bit(config->base + TSEN_CTRL_REG, TSEN_TSEN_CTRL_REG_ANAU_TSEN_EN_Pos); | ||
| sys_set_bit(config->base + TSEN_CTRL_REG, TSEN_TSEN_CTRL_REG_ANAU_TSEN_PU_Pos); | ||
| sys_set_bit(config->base + TSEN_CTRL_REG, TSEN_TSEN_CTRL_REG_ANAU_TSEN_RSTB_Pos); | ||
| k_busy_wait(20); | ||
| sys_set_bit(config->base + TSEN_CTRL_REG, TSEN_TSEN_CTRL_REG_ANAU_TSEN_RUN_Pos); | ||
|
|
||
| k_mutex_init(&data->mutex); | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| #define SF32LB_TSEN_DEFINE(inst) \ | ||
| static struct sf32lb_tsen_data sf32lb_tsen_data_##inst; \ | ||
| static const struct sf32lb_tsen_config sf32lb_tsen_config_##inst = { \ | ||
| .base = DT_INST_REG_ADDR(inst), \ | ||
| .cfg_base = DT_REG_ADDR(DT_INST_PHANDLE(inst, sifli_cfg)), \ | ||
| .clock = SF32LB_CLOCK_DT_INST_SPEC_GET(inst), \ | ||
| }; \ | ||
| SENSOR_DEVICE_DT_INST_DEFINE(inst, sf32lb_tsen_init, NULL, &sf32lb_tsen_data_##inst, \ | ||
| &sf32lb_tsen_config_##inst, POST_KERNEL, \ | ||
| CONFIG_SENSOR_INIT_PRIORITY, &sf32lb_tsen_driver_api); | ||
|
|
||
| DT_INST_FOREACH_STATUS_OKAY(SF32LB_TSEN_DEFINE) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Copyright (c) 2025, Qingsong Gou <gouqs@hotmail.com> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: SF32LB temperature sensor | ||
|
|
||
| compatible: "sifli,sf32lb-tsen" | ||
|
|
||
| include: [base.yaml, sifli-sf32lb-cfg.yaml] | ||
|
|
||
| properties: | ||
| reg: | ||
| required: true | ||
|
|
||
| clocks: | ||
| required: true | ||
|
|
||
| sifli,cfg: | ||
| required: true |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.