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 "humidity_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_humidity_driver.h"
16094332d3Sopenharmony_ci
17094332d3Sopenharmony_ci#define HDF_LOG_TAG    hdf_sensor_humidity_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 HumidityData *rawData, uint64_t *timestamp)
27094332d3Sopenharmony_ci{
28094332d3Sopenharmony_ci    OsalTimespec time;
29094332d3Sopenharmony_ci    int32_t cnt;
30094332d3Sopenharmony_ci    uint64_t humidityValue;
31094332d3Sopenharmony_ci    int32_t ret = HDF_SUCCESS;
32094332d3Sopenharmony_ci    uint8_t value[AHT20_HUM_DATA_BUF_LEN] = {0};
33094332d3Sopenharmony_ci    uint8_t measureCmdValue[] = {AHT20_HUM_MEASURE_ADDR, AHT20_HUM_MEASURE_ARG0, AHT20_HUM_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_HUM_DELAY_MS);
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, AHT20_HUM_STATUS_ADDR, value, sizeof(value));
52094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
53094332d3Sopenharmony_ci
54094332d3Sopenharmony_ci    for (cnt = 0; AHT20_HUM_IS_BUSY(value[AHT20_HUM_VALUE_IDX_ZERO]) && (cnt < AHT20_HUM_RETRY_TIMES); cnt++) {
55094332d3Sopenharmony_ci        OsalMDelay(AHT20_HUM_DELAY_MS);
56094332d3Sopenharmony_ci        ret = ReadSensor(&data->busCfg, AHT20_HUM_STATUS_ADDR, value, sizeof(value));
57094332d3Sopenharmony_ci        CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
58094332d3Sopenharmony_ci    }
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_ci    if (cnt >= AHT20_HUM_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    humidityValue = value[AHT20_HUM_VALUE_IDX_ONE];
66094332d3Sopenharmony_ci    humidityValue = (humidityValue << AHT20_HUM_SHFIT_EIGHT_BITS) | value[AHT20_HUM_VALUE_IDX_TWO];
67094332d3Sopenharmony_ci    humidityValue = (humidityValue << AHT20_HUM_SHFIT_FOUR_BITS) | \
68094332d3Sopenharmony_ci        ((value[AHT20_HUM_VALUE_IDX_THREE] & AHT20_HUM_MASK) >> AHT20_HUM_SHFIT_FOUR_BITS);
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_ci    rawData->humidity = ((humidityValue * AHT20_HUM_SLOPE) / AHT20_HUM_RESOLUTION);
71094332d3Sopenharmony_ci
72094332d3Sopenharmony_ci    return HDF_SUCCESS;
73094332d3Sopenharmony_ci}
74094332d3Sopenharmony_ci
75094332d3Sopenharmony_cistatic int32_t ReadAht20Data(struct SensorCfgData *data)
76094332d3Sopenharmony_ci{
77094332d3Sopenharmony_ci    int32_t ret;
78094332d3Sopenharmony_ci    static int32_t humidity;
79094332d3Sopenharmony_ci    struct HumidityData rawData = { 0 };
80094332d3Sopenharmony_ci    OsalTimespec time;
81094332d3Sopenharmony_ci    struct SensorReportEvent event;
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_ci    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
84094332d3Sopenharmony_ci    (void)memset_s(&event, sizeof(event), 0, sizeof(event));
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ci    if (OsalGetTime(&time) != HDF_SUCCESS) {
87094332d3Sopenharmony_ci        HDF_LOGE("%s: Get time failed", __func__);
88094332d3Sopenharmony_ci        return HDF_FAILURE;
89094332d3Sopenharmony_ci    }
90094332d3Sopenharmony_ci
91094332d3Sopenharmony_ci    event.timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT;
92094332d3Sopenharmony_ci
93094332d3Sopenharmony_ci    ret = ReadAht20RawData(data, &rawData, &event.timestamp);
94094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
95094332d3Sopenharmony_ci        HDF_LOGE("%s: AHT20 read raw data failed", __func__);
96094332d3Sopenharmony_ci        return HDF_FAILURE;
97094332d3Sopenharmony_ci    }
98094332d3Sopenharmony_ci
99094332d3Sopenharmony_ci    humidity = rawData.humidity;
100094332d3Sopenharmony_ci
101094332d3Sopenharmony_ci    event.sensorId = SENSOR_TAG_HUMIDITY;
102094332d3Sopenharmony_ci    event.mode = SENSOR_WORK_MODE_REALTIME;
103094332d3Sopenharmony_ci    event.dataLen = sizeof(humidity);
104094332d3Sopenharmony_ci    event.data = (uint8_t *)&humidity;
105094332d3Sopenharmony_ci    ret = ReportSensorEvent(&event);
106094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
107094332d3Sopenharmony_ci        HDF_LOGE("%s: report data failed", __func__);
108094332d3Sopenharmony_ci    }
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_ci    return ret;
111094332d3Sopenharmony_ci}
112094332d3Sopenharmony_ci
113094332d3Sopenharmony_cistatic int32_t InitAht20(struct SensorCfgData *data)
114094332d3Sopenharmony_ci{
115094332d3Sopenharmony_ci    int32_t ret;
116094332d3Sopenharmony_ci    uint8_t value[AHT20_HUM_DATA_BUF_LEN];
117094332d3Sopenharmony_ci    uint8_t resetCmd = AHT20_HUM_RESET_ADDR;
118094332d3Sopenharmony_ci    uint8_t calibrationCmd[] = {AHT20_HUM_CALIBRATION_ADDR, AHT20_HUM_CALIBRATION_ARG0, AHT20_HUM_CALIBRATION_ARG1};
119094332d3Sopenharmony_ci
120094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
121094332d3Sopenharmony_ci    ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
122094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
123094332d3Sopenharmony_ci        HDF_LOGE("%s: sensor init config failed", __func__);
124094332d3Sopenharmony_ci        return HDF_FAILURE;
125094332d3Sopenharmony_ci    }
126094332d3Sopenharmony_ci
127094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, AHT20_HUM_STATUS_ADDR, value, sizeof(value));
128094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
129094332d3Sopenharmony_ci
130094332d3Sopenharmony_ci    if (AHT20_HUM_IS_BUSY(value[AHT20_HUM_VALUE_IDX_ZERO]) || !AHT20_HUM_IS_CALI(value[AHT20_HUM_VALUE_IDX_ZERO])) {
131094332d3Sopenharmony_ci        ret = WriteSensor(&data->busCfg, &resetCmd, sizeof(resetCmd));
132094332d3Sopenharmony_ci        CHECK_PARSER_RESULT_RETURN_VALUE(ret, "write data");
133094332d3Sopenharmony_ci
134094332d3Sopenharmony_ci        OsalMDelay(AHT20_HUM_STARTUP_MS);
135094332d3Sopenharmony_ci
136094332d3Sopenharmony_ci        ret = WriteSensor(&data->busCfg, calibrationCmd, sizeof(calibrationCmd));
137094332d3Sopenharmony_ci        CHECK_PARSER_RESULT_RETURN_VALUE(ret, "write data");
138094332d3Sopenharmony_ci
139094332d3Sopenharmony_ci        OsalMDelay(AHT20_HUM_CALIBRATION_MS);
140094332d3Sopenharmony_ci    }
141094332d3Sopenharmony_ci
142094332d3Sopenharmony_ci    return HDF_SUCCESS;
143094332d3Sopenharmony_ci}
144094332d3Sopenharmony_ci
145094332d3Sopenharmony_cistatic int32_t DispatchAht20(struct HdfDeviceIoClient *client,
146094332d3Sopenharmony_ci    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
147094332d3Sopenharmony_ci{
148094332d3Sopenharmony_ci    (void)client;
149094332d3Sopenharmony_ci    (void)cmd;
150094332d3Sopenharmony_ci    (void)data;
151094332d3Sopenharmony_ci    (void)reply;
152094332d3Sopenharmony_ci
153094332d3Sopenharmony_ci    return HDF_SUCCESS;
154094332d3Sopenharmony_ci}
155094332d3Sopenharmony_ci
156094332d3Sopenharmony_cistatic int32_t Aht20BindDriver(struct HdfDeviceObject *device)
157094332d3Sopenharmony_ci{
158094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
159094332d3Sopenharmony_ci
160094332d3Sopenharmony_ci    struct Aht20DrvData *drvData = (struct Aht20DrvData *)OsalMemCalloc(sizeof(*drvData));
161094332d3Sopenharmony_ci    if (drvData == NULL) {
162094332d3Sopenharmony_ci        HDF_LOGE("%s: malloc drv data fail", __func__);
163094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
164094332d3Sopenharmony_ci    }
165094332d3Sopenharmony_ci
166094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchAht20;
167094332d3Sopenharmony_ci    drvData->device = device;
168094332d3Sopenharmony_ci    device->service = &drvData->ioService;
169094332d3Sopenharmony_ci    g_aht20DrvData = drvData;
170094332d3Sopenharmony_ci
171094332d3Sopenharmony_ci    return HDF_SUCCESS;
172094332d3Sopenharmony_ci}
173094332d3Sopenharmony_ci
174094332d3Sopenharmony_cistatic int32_t Aht20InitDriver(struct HdfDeviceObject *device)
175094332d3Sopenharmony_ci{
176094332d3Sopenharmony_ci    int32_t ret;
177094332d3Sopenharmony_ci    struct HumidityOpsCall ops;
178094332d3Sopenharmony_ci
179094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
180094332d3Sopenharmony_ci    struct Aht20DrvData *drvData = (struct Aht20DrvData *)device->service;
181094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
182094332d3Sopenharmony_ci
183094332d3Sopenharmony_ci    drvData->sensorCfg = HumidityCreateCfgData(device->property);
184094332d3Sopenharmony_ci    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
185094332d3Sopenharmony_ci        HDF_LOGE("%s: Creating humidity cfg failed because detection failed", __func__);
186094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
187094332d3Sopenharmony_ci    }
188094332d3Sopenharmony_ci
189094332d3Sopenharmony_ci    ops.Init = NULL;
190094332d3Sopenharmony_ci    ops.ReadData = ReadAht20Data;
191094332d3Sopenharmony_ci    ret = HumidityRegisterChipOps(&ops);
192094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
193094332d3Sopenharmony_ci        HDF_LOGE("%s: Register humidity failed", __func__);
194094332d3Sopenharmony_ci        return HDF_FAILURE;
195094332d3Sopenharmony_ci    }
196094332d3Sopenharmony_ci
197094332d3Sopenharmony_ci    ret = InitAht20(drvData->sensorCfg);
198094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
199094332d3Sopenharmony_ci        HDF_LOGE("%s: Init AHT20 humidity sensor failed", __func__);
200094332d3Sopenharmony_ci        return HDF_FAILURE;
201094332d3Sopenharmony_ci    }
202094332d3Sopenharmony_ci
203094332d3Sopenharmony_ci    return HDF_SUCCESS;
204094332d3Sopenharmony_ci}
205094332d3Sopenharmony_ci
206094332d3Sopenharmony_cistatic void Aht20ReleaseDriver(struct HdfDeviceObject *device)
207094332d3Sopenharmony_ci{
208094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(device);
209094332d3Sopenharmony_ci
210094332d3Sopenharmony_ci    struct Aht20DrvData *drvData = (struct Aht20DrvData *)device->service;
211094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(drvData);
212094332d3Sopenharmony_ci
213094332d3Sopenharmony_ci    if (drvData->sensorCfg != NULL) {
214094332d3Sopenharmony_ci        HumidityReleaseCfgData(drvData->sensorCfg);
215094332d3Sopenharmony_ci        drvData->sensorCfg = NULL;
216094332d3Sopenharmony_ci    }
217094332d3Sopenharmony_ci    OsalMemFree(drvData);
218094332d3Sopenharmony_ci}
219094332d3Sopenharmony_ci
220094332d3Sopenharmony_cistruct HdfDriverEntry g_humidityAht20DevEntry = {
221094332d3Sopenharmony_ci    .moduleVersion = 1,
222094332d3Sopenharmony_ci    .moduleName = "HDF_SENSOR_HUMIDITY_AHT20",
223094332d3Sopenharmony_ci    .Bind = Aht20BindDriver,
224094332d3Sopenharmony_ci    .Init = Aht20InitDriver,
225094332d3Sopenharmony_ci    .Release = Aht20ReleaseDriver,
226094332d3Sopenharmony_ci};
227094332d3Sopenharmony_ci
228094332d3Sopenharmony_ciHDF_INIT(g_humidityAht20DevEntry);
229