1 /*
2 * Copyright (c) 2023 Centralp
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nxp_tempmon
8
9 #include <zephyr/device.h>
10 #include <zephyr/devicetree.h>
11 #include <zephyr/drivers/sensor.h>
12
13 /* OTP Controller Analog Register 1 for calibration values */
14 #define OCOTP_ANA1_ROOM_COUNT_SHIFT 20
15 #define OCOTP_ANA1_ROOM_COUNT_MASK (BIT_MASK(12) << OCOTP_ANA1_ROOM_COUNT_SHIFT)
16 #define OCOTP_ANA1_HOT_COUNT_SHIFT 8
17 #define OCOTP_ANA1_HOT_COUNT_MASK (BIT_MASK(12) << OCOTP_ANA1_HOT_COUNT_SHIFT)
18 #define OCOTP_ANA1_HOT_TEMP_SHIFT 0
19 #define OCOTP_ANA1_HOT_TEMP_MASK (BIT_MASK(8) << OCOTP_ANA1_HOT_TEMP_SHIFT)
20
21 #define TEMPMON_ROOM_TEMP 25.0f
22
23 struct nxp_tempmon_data {
24 uint8_t hot_temp;
25 uint16_t hot_cnt;
26 uint16_t room_cnt;
27 uint16_t temp_cnt;
28 };
29
30 struct nxp_tempmon_config {
31 TEMPMON_Type *base;
32 };
33
nxp_tempmon_sample_fetch(const struct device * dev,enum sensor_channel chan)34 static int nxp_tempmon_sample_fetch(const struct device *dev, enum sensor_channel chan)
35 {
36 struct nxp_tempmon_data *data = dev->data;
37 const struct nxp_tempmon_config *cfg = dev->config;
38
39 if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_DIE_TEMP) {
40 return -ENOTSUP;
41 }
42
43 /* Start the measure */
44 cfg->base->TEMPSENSE0 |= TEMPMON_TEMPSENSE0_MEASURE_TEMP_MASK;
45
46 /* Wait until measure is ready */
47 while (!(cfg->base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_FINISHED_MASK)) {
48 }
49 data->temp_cnt = (cfg->base->TEMPSENSE0 & TEMPMON_TEMPSENSE0_TEMP_CNT_MASK) >>
50 TEMPMON_TEMPSENSE0_TEMP_CNT_SHIFT;
51
52 /* Stop the measure */
53 cfg->base->TEMPSENSE0 &= ~TEMPMON_TEMPSENSE0_MEASURE_TEMP_MASK;
54
55 return 0;
56 }
57
nxp_tempmon_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)58 static int nxp_tempmon_channel_get(const struct device *dev, enum sensor_channel chan,
59 struct sensor_value *val)
60 {
61 const struct nxp_tempmon_data *data = dev->data;
62 float temp;
63
64 if (chan != SENSOR_CHAN_DIE_TEMP) {
65 return -ENOTSUP;
66 }
67
68 temp = (float)data->hot_temp - ((float)data->temp_cnt - (float)data->hot_cnt) *
69 (((float)data->hot_temp - TEMPMON_ROOM_TEMP) /
70 ((float)data->room_cnt - (float)data->hot_cnt));
71
72 return sensor_value_from_double(val, temp);
73 }
74
75 static DEVICE_API(sensor, nxp_tempmon_driver_api) = {
76 .sample_fetch = nxp_tempmon_sample_fetch,
77 .channel_get = nxp_tempmon_channel_get,
78 };
79
nxp_tempmon_init(const struct device * dev)80 static int nxp_tempmon_init(const struct device *dev)
81 {
82 struct nxp_tempmon_data *data = dev->data;
83 const struct nxp_tempmon_config *cfg = dev->config;
84 uint32_t ocotp_ana1;
85
86 /* Enable the temperature sensor */
87 cfg->base->TEMPSENSE0 &= ~TEMPMON_TEMPSENSE0_POWER_DOWN_MASK;
88
89 /* Single measure with no repeat mode */
90 cfg->base->TEMPSENSE1 = TEMPMON_TEMPSENSE1_MEASURE_FREQ(0);
91
92 /* Read calibration data */
93 ocotp_ana1 = OCOTP->ANA1;
94 data->hot_temp = (ocotp_ana1 & OCOTP_ANA1_HOT_TEMP_MASK) >> OCOTP_ANA1_HOT_TEMP_SHIFT;
95 data->hot_cnt = (ocotp_ana1 & OCOTP_ANA1_HOT_COUNT_MASK) >> OCOTP_ANA1_HOT_COUNT_SHIFT;
96 data->room_cnt = (ocotp_ana1 & OCOTP_ANA1_ROOM_COUNT_MASK) >> OCOTP_ANA1_ROOM_COUNT_SHIFT;
97
98 return 0;
99 }
100
101 static struct nxp_tempmon_data nxp_tempmon_dev_data = {
102 .hot_temp = 0,
103 .hot_cnt = 0,
104 .room_cnt = 0,
105 .temp_cnt = 0,
106 };
107
108 static const struct nxp_tempmon_config nxp_tempmon_dev_config = {
109 .base = (TEMPMON_Type *)DT_INST_REG_ADDR(0),
110 };
111
112 SENSOR_DEVICE_DT_INST_DEFINE(0, nxp_tempmon_init, NULL, &nxp_tempmon_dev_data,
113 &nxp_tempmon_dev_config, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,
114 &nxp_tempmon_driver_api);
115