1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci *
4094332d3Sopenharmony_ci * HDF is dual licensed: you can use it either under the terms of
5094332d3Sopenharmony_ci * the GPL, or the BSD license, at your option.
6094332d3Sopenharmony_ci * See the LICENSE file in the root of this repository for complete details.
7094332d3Sopenharmony_ci */
8094332d3Sopenharmony_ci
9094332d3Sopenharmony_ci#include "temperature_aht20.h"
10094332d3Sopenharmony_ci#include <securec.h>
11094332d3Sopenharmony_ci#include "osal_mem.h"
12094332d3Sopenharmony_ci#include "osal_time.h"
13094332d3Sopenharmony_ci#include "sensor_config_controller.h"
14094332d3Sopenharmony_ci#include "sensor_device_manager.h"
15094332d3Sopenharmony_ci#include "sensor_temperature_driver.h"
16094332d3Sopenharmony_ci
17094332d3Sopenharmony_ci#define HDF_LOG_TAG    hdf_sensor_temperature_driver
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_cistatic struct Aht20DrvData *g_aht20DrvData = NULL;
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_cistatic struct Aht20DrvData *Aht20GetDrvData(void)
22094332d3Sopenharmony_ci{
23094332d3Sopenharmony_ci    return g_aht20DrvData;
24094332d3Sopenharmony_ci}
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_cistatic int32_t ReadAht20RawData(struct SensorCfgData *data, struct TemperatureData *rawData, uint64_t *timestamp)
27094332d3Sopenharmony_ci{
28094332d3Sopenharmony_ci    OsalTimespec time;
29094332d3Sopenharmony_ci    int32_t count;
30094332d3Sopenharmony_ci    int32_t ret = HDF_SUCCESS;
31094332d3Sopenharmony_ci    uint8_t value[AHT20_TEMP_DATA_BUF_LEN];
32094332d3Sopenharmony_ci    uint64_t tempValue;
33094332d3Sopenharmony_ci    uint8_t measureCmdValue[] = {AHT20_TEMP_MEASURE_ADDR, AHT20_TEMP_MEASURE_ARG0, AHT20_TEMP_MEASURE_ARG1};
34094332d3Sopenharmony_ci
35094332d3Sopenharmony_ci    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
38094332d3Sopenharmony_ci
39094332d3Sopenharmony_ci    if (OsalGetTime(&time) != HDF_SUCCESS) {
40094332d3Sopenharmony_ci        HDF_LOGE("%s: Get time failed", __func__);
41094332d3Sopenharmony_ci        return HDF_FAILURE;
42094332d3Sopenharmony_ci    }
43094332d3Sopenharmony_ci
44094332d3Sopenharmony_ci    *timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
45094332d3Sopenharmony_ci
46094332d3Sopenharmony_ci    ret = WriteSensor(&data->busCfg, measureCmdValue, sizeof(measureCmdValue));
47094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "write data");
48094332d3Sopenharmony_ci
49094332d3Sopenharmony_ci    OsalMDelay(AHT20_TEMP_DELAY_MS);
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, AHT20_TEMP_STATUS_ADDR, value, sizeof(value));
52094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
53094332d3Sopenharmony_ci
54094332d3Sopenharmony_ci    for (count = 0; AHT20_TEMP_IS_BUSY(value[AHT20_TEMP_VALUE_IDX_ZERO]) && (count < AHT20_TEMP_RETRY_TIMES); count++) {
55094332d3Sopenharmony_ci        OsalMDelay(AHT20_TEMP_DELAY_MS);
56094332d3Sopenharmony_ci        ret = ReadSensor(&data->busCfg, AHT20_TEMP_STATUS_ADDR, value, sizeof(value));
57094332d3Sopenharmony_ci        CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
58094332d3Sopenharmony_ci    }
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_ci    if (count >= AHT20_TEMP_RETRY_TIMES) {
61094332d3Sopenharmony_ci        HDF_LOGE("%s:line %d aht20 device status busy!", __func__, __LINE__);
62094332d3Sopenharmony_ci        return HDF_FAILURE;
63094332d3Sopenharmony_ci    }
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ci    tempValue = value[AHT20_TEMP_VALUE_IDX_THREE] & AHT20_TEMP_MASK;
66094332d3Sopenharmony_ci    tempValue = (tempValue << AHT20_TEMP_SHFIT_BITS) | value[AHT20_TEMP_VALUE_IDX_FOUR];
67094332d3Sopenharmony_ci    tempValue = (tempValue << AHT20_TEMP_SHFIT_BITS) | value[AHT20_TEMP_VALUE_IDX_FIVE];
68094332d3Sopenharmony_ci
69094332d3Sopenharmony_ci    rawData->temperature = ((tempValue * AHT20_TEMP_SLOPE) / AHT20_TEMP_RESOLUTION) - AHT20_TEMP_CONSATNT;
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ci    return HDF_SUCCESS;
72094332d3Sopenharmony_ci}
73094332d3Sopenharmony_ci
74094332d3Sopenharmony_cistatic int32_t ReadAht20Data(struct SensorCfgData *data)
75094332d3Sopenharmony_ci{
76094332d3Sopenharmony_ci    int32_t ret;
77094332d3Sopenharmony_ci    static int32_t temperature;
78094332d3Sopenharmony_ci    struct TemperatureData rawData = { 0 };
79094332d3Sopenharmony_ci    OsalTimespec time;
80094332d3Sopenharmony_ci    struct SensorReportEvent event;
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ci    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
83094332d3Sopenharmony_ci    (void)memset_s(&event, sizeof(event), 0, sizeof(event));
84094332d3Sopenharmony_ci
85094332d3Sopenharmony_ci    if (OsalGetTime(&time) != HDF_SUCCESS) {
86094332d3Sopenharmony_ci        HDF_LOGE("%s: Get time failed", __func__);
87094332d3Sopenharmony_ci        return HDF_FAILURE;
88094332d3Sopenharmony_ci    }
89094332d3Sopenharmony_ci
90094332d3Sopenharmony_ci    event.timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT;
91094332d3Sopenharmony_ci
92094332d3Sopenharmony_ci    ret = ReadAht20RawData(data, &rawData, &event.timestamp);
93094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
94094332d3Sopenharmony_ci        HDF_LOGE("%s: AHT20 read raw data failed", __func__);
95094332d3Sopenharmony_ci        return HDF_FAILURE;
96094332d3Sopenharmony_ci    }
97094332d3Sopenharmony_ci
98094332d3Sopenharmony_ci    temperature = rawData.temperature;
99094332d3Sopenharmony_ci
100094332d3Sopenharmony_ci    event.sensorId = SENSOR_TAG_TEMPERATURE;
101094332d3Sopenharmony_ci    event.mode = SENSOR_WORK_MODE_REALTIME;
102094332d3Sopenharmony_ci    event.dataLen = sizeof(temperature);
103094332d3Sopenharmony_ci    event.data = (uint8_t *)&temperature;
104094332d3Sopenharmony_ci    ret = ReportSensorEvent(&event);
105094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
106094332d3Sopenharmony_ci        HDF_LOGE("%s: report data failed", __func__);
107094332d3Sopenharmony_ci    }
108094332d3Sopenharmony_ci
109094332d3Sopenharmony_ci    return ret;
110094332d3Sopenharmony_ci}
111094332d3Sopenharmony_ci
112094332d3Sopenharmony_cistatic int32_t InitAht20(struct SensorCfgData *data)
113094332d3Sopenharmony_ci{
114094332d3Sopenharmony_ci    int32_t ret;
115094332d3Sopenharmony_ci    uint8_t value[AHT20_TEMP_DATA_BUF_LEN];
116094332d3Sopenharmony_ci    uint8_t resetCmd = AHT20_TEMP_RESET_ADDR;
117094332d3Sopenharmony_ci    uint8_t calibrationCmd[] = {AHT20_TEMP_CALIBRATION_ADDR, AHT20_TEMP_CALIBRATION_ARG0, AHT20_TEMP_CALIBRATION_ARG1};
118094332d3Sopenharmony_ci
119094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
120094332d3Sopenharmony_ci    ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
121094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
122094332d3Sopenharmony_ci        HDF_LOGE("%s: sensor init config failed", __func__);
123094332d3Sopenharmony_ci        return HDF_FAILURE;
124094332d3Sopenharmony_ci    }
125094332d3Sopenharmony_ci
126094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, AHT20_TEMP_STATUS_ADDR, value, sizeof(value));
127094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
128094332d3Sopenharmony_ci
129094332d3Sopenharmony_ci    if (AHT20_TEMP_IS_BUSY(value[AHT20_TEMP_VALUE_IDX_ZERO]) || !AHT20_TEMP_IS_CALI(value[AHT20_TEMP_VALUE_IDX_ZERO])) {
130094332d3Sopenharmony_ci        ret = WriteSensor(&data->busCfg, &resetCmd, sizeof(resetCmd));
131094332d3Sopenharmony_ci        CHECK_PARSER_RESULT_RETURN_VALUE(ret, "write data");
132094332d3Sopenharmony_ci
133094332d3Sopenharmony_ci        OsalMDelay(AHT20_TEMP_STARTUP_MS);
134094332d3Sopenharmony_ci
135094332d3Sopenharmony_ci        ret = WriteSensor(&data->busCfg, calibrationCmd, sizeof(calibrationCmd));
136094332d3Sopenharmony_ci        CHECK_PARSER_RESULT_RETURN_VALUE(ret, "write data");
137094332d3Sopenharmony_ci
138094332d3Sopenharmony_ci        OsalMDelay(AHT20_TEMP_CALIBRATION_MS);
139094332d3Sopenharmony_ci    }
140094332d3Sopenharmony_ci
141094332d3Sopenharmony_ci    return HDF_SUCCESS;
142094332d3Sopenharmony_ci}
143094332d3Sopenharmony_ci
144094332d3Sopenharmony_cistatic int32_t DispatchAht20(struct HdfDeviceIoClient *client,
145094332d3Sopenharmony_ci    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
146094332d3Sopenharmony_ci{
147094332d3Sopenharmony_ci    (void)client;
148094332d3Sopenharmony_ci    (void)cmd;
149094332d3Sopenharmony_ci    (void)data;
150094332d3Sopenharmony_ci    (void)reply;
151094332d3Sopenharmony_ci
152094332d3Sopenharmony_ci    return HDF_SUCCESS;
153094332d3Sopenharmony_ci}
154094332d3Sopenharmony_ci
155094332d3Sopenharmony_cistatic int32_t Aht20BindDriver(struct HdfDeviceObject *device)
156094332d3Sopenharmony_ci{
157094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
158094332d3Sopenharmony_ci
159094332d3Sopenharmony_ci    struct Aht20DrvData *drvData = (struct Aht20DrvData *)OsalMemCalloc(sizeof(*drvData));
160094332d3Sopenharmony_ci    if (drvData == NULL) {
161094332d3Sopenharmony_ci        HDF_LOGE("%s: malloc drv data fail", __func__);
162094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
163094332d3Sopenharmony_ci    }
164094332d3Sopenharmony_ci
165094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchAht20;
166094332d3Sopenharmony_ci    drvData->device = device;
167094332d3Sopenharmony_ci    device->service = &drvData->ioService;
168094332d3Sopenharmony_ci    g_aht20DrvData = drvData;
169094332d3Sopenharmony_ci
170094332d3Sopenharmony_ci    return HDF_SUCCESS;
171094332d3Sopenharmony_ci}
172094332d3Sopenharmony_ci
173094332d3Sopenharmony_cistatic int32_t Aht20InitDriver(struct HdfDeviceObject *device)
174094332d3Sopenharmony_ci{
175094332d3Sopenharmony_ci    int32_t ret;
176094332d3Sopenharmony_ci    struct TemperatureOpsCall ops;
177094332d3Sopenharmony_ci
178094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
179094332d3Sopenharmony_ci    struct Aht20DrvData *drvData = (struct Aht20DrvData *)device->service;
180094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
181094332d3Sopenharmony_ci
182094332d3Sopenharmony_ci    drvData->sensorCfg = TemperatureCreateCfgData(device->property);
183094332d3Sopenharmony_ci    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
184094332d3Sopenharmony_ci        HDF_LOGE("%s: Creating temperature cfg failed because detection failed", __func__);
185094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
186094332d3Sopenharmony_ci    }
187094332d3Sopenharmony_ci
188094332d3Sopenharmony_ci    ops.Init = NULL;
189094332d3Sopenharmony_ci    ops.ReadData = ReadAht20Data;
190094332d3Sopenharmony_ci    ret = TemperatureRegisterChipOps(&ops);
191094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
192094332d3Sopenharmony_ci        HDF_LOGE("%s: Register temperature failed", __func__);
193094332d3Sopenharmony_ci        return HDF_FAILURE;
194094332d3Sopenharmony_ci    }
195094332d3Sopenharmony_ci
196094332d3Sopenharmony_ci    ret = InitAht20(drvData->sensorCfg);
197094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
198094332d3Sopenharmony_ci        HDF_LOGE("%s: Init AHT20 temperature sensor failed", __func__);
199094332d3Sopenharmony_ci        return HDF_FAILURE;
200094332d3Sopenharmony_ci    }
201094332d3Sopenharmony_ci
202094332d3Sopenharmony_ci    return HDF_SUCCESS;
203094332d3Sopenharmony_ci}
204094332d3Sopenharmony_ci
205094332d3Sopenharmony_cistatic void Aht20ReleaseDriver(struct HdfDeviceObject *device)
206094332d3Sopenharmony_ci{
207094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(device);
208094332d3Sopenharmony_ci
209094332d3Sopenharmony_ci    struct Aht20DrvData *drvData = (struct Aht20DrvData *)device->service;
210094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(drvData);
211094332d3Sopenharmony_ci
212094332d3Sopenharmony_ci    if (drvData->sensorCfg != NULL) {
213094332d3Sopenharmony_ci        TemperatureReleaseCfgData(drvData->sensorCfg);
214094332d3Sopenharmony_ci        drvData->sensorCfg = NULL;
215094332d3Sopenharmony_ci    }
216094332d3Sopenharmony_ci    OsalMemFree(drvData);
217094332d3Sopenharmony_ci}
218094332d3Sopenharmony_ci
219094332d3Sopenharmony_cistruct HdfDriverEntry g_temperatureAht20DevEntry = {
220094332d3Sopenharmony_ci    .moduleVersion = 1,
221094332d3Sopenharmony_ci    .moduleName = "HDF_SENSOR_TEMPERATURE_AHT20",
222094332d3Sopenharmony_ci    .Bind = Aht20BindDriver,
223094332d3Sopenharmony_ci    .Init = Aht20InitDriver,
224094332d3Sopenharmony_ci    .Release = Aht20ReleaseDriver,
225094332d3Sopenharmony_ci};
226094332d3Sopenharmony_ci
227094332d3Sopenharmony_ciHDF_INIT(g_temperatureAht20DevEntry);
228