1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021-2022 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 "als_bh1745.h"
10094332d3Sopenharmony_ci#include <securec.h>
11094332d3Sopenharmony_ci#include "osal_mem.h"
12094332d3Sopenharmony_ci#include "osal_time.h"
13094332d3Sopenharmony_ci#include "sensor_als_driver.h"
14094332d3Sopenharmony_ci#include "sensor_config_controller.h"
15094332d3Sopenharmony_ci#include "sensor_device_manager.h"
16094332d3Sopenharmony_ci
17094332d3Sopenharmony_ci#define HDF_LOG_TAG    khdf_sensor_als_driver
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_ci/* IO config for int-pin and I2C-pin */
20094332d3Sopenharmony_ci#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
21094332d3Sopenharmony_ci#define SENSOR_I2C6_CLK_REG_ADDR  0x114f0048
22094332d3Sopenharmony_ci#define SENSOR_I2C_REG_CFG        0x403
23094332d3Sopenharmony_ci#define SENSOR_TIME_INCREASE      0
24094332d3Sopenharmony_ci#define SENSOR_TIME_DECREASE      1
25094332d3Sopenharmony_ci#define SENSOR_GAIN_INCREASE      0
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_cistatic struct Bh1745DrvData *g_bh1745DrvData = NULL;
28094332d3Sopenharmony_cistatic uint32_t g_timeChangeStatus = SENSOR_TIME_DECREASE;
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_cistatic struct TimeRegAddrValueMap g_timeMap[EXTENDED_ALS_TIME_GROUP_INDEX_MAX] = {
31094332d3Sopenharmony_ci    { EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_0,           BH1745_TIME_160MSEC },
32094332d3Sopenharmony_ci    { EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_1,           BH1745_TIME_320MSEC },
33094332d3Sopenharmony_ci    { EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_2,           BH1745_TIME_640MSEC },
34094332d3Sopenharmony_ci    { EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_3,           BH1745_TIME_1280MSEC },
35094332d3Sopenharmony_ci    { EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_4,           BH1745_TIME_2560MSEC },
36094332d3Sopenharmony_ci    { EXTENDED_ALS_TIME_GROUP_ATTR_VALUE_5,           BH1745_TIME_5120MSEC }
37094332d3Sopenharmony_ci};
38094332d3Sopenharmony_ci
39094332d3Sopenharmony_cistatic struct GainRegAddrValueMap g_gainMap[EXTENDED_ALS_GAIN_GROUP_INDEX_MAX] = {
40094332d3Sopenharmony_ci    { EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_0,              BH1745_GAIN_1X },
41094332d3Sopenharmony_ci    { EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_1,              BH1745_GAIN_2X },
42094332d3Sopenharmony_ci    { EXTENDED_ALS_GAIN_GROUP_ATTR_VALUE_2,              BH1745_GAIN_16X }
43094332d3Sopenharmony_ci};
44094332d3Sopenharmony_ci
45094332d3Sopenharmony_cistatic uint32_t g_red[BH1745_COEFFICIENT_RED] = {
46094332d3Sopenharmony_ci    BH1745_COEFFICIENT_RED_LEVEL_0,
47094332d3Sopenharmony_ci    BH1745_COEFFICIENT_RED_LEVEL_1
48094332d3Sopenharmony_ci};
49094332d3Sopenharmony_ci
50094332d3Sopenharmony_cistatic uint32_t g_green[BH1745_COEFFICIENT_GREEN] = {
51094332d3Sopenharmony_ci    BH1745_COEFFICIENT_GREEN_LEVEL_0,
52094332d3Sopenharmony_ci    BH1745_COEFFICIENT_GREEN_LEVEL_1
53094332d3Sopenharmony_ci};
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_cistatic int32_t DynamicRangCovert(struct SensorCfgData *CfgData, uint32_t *rgbcData)
56094332d3Sopenharmony_ci{
57094332d3Sopenharmony_ci    uint8_t timeItemNum;
58094332d3Sopenharmony_ci    uint8_t regValue;
59094332d3Sopenharmony_ci    int32_t ret;
60094332d3Sopenharmony_ci    uint32_t temp;
61094332d3Sopenharmony_ci    struct SensorRegCfgGroupNode *timeGroupNode = NULL;
62094332d3Sopenharmony_ci    int32_t index = EXTENDED_ALS_TIME_GROUP_INDEX_0;
63094332d3Sopenharmony_ci
64094332d3Sopenharmony_ci    timeGroupNode = CfgData->extendedRegCfgGroup[EXTENDED_ALS_TIME_GROUP];
65094332d3Sopenharmony_ci    timeItemNum = timeGroupNode->itemNum;
66094332d3Sopenharmony_ci    if (timeItemNum > EXTENDED_ALS_TIME_GROUP_INDEX_MAX) {
67094332d3Sopenharmony_ci        HDF_LOGE("%s: TimeItemNum out of range ", __func__);
68094332d3Sopenharmony_ci        return HDF_FAILURE;
69094332d3Sopenharmony_ci    }
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ci    ret = ReadSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, &regValue, sizeof(regValue));
72094332d3Sopenharmony_ci    CHECK_SENSOR_REGIARRAY_RETURN_VALUE(ret, "read");
73094332d3Sopenharmony_ci
74094332d3Sopenharmony_ci    regValue &= timeGroupNode->regCfgItem->mask;
75094332d3Sopenharmony_ci    temp = GetTimeByRegValue(regValue, g_timeMap, timeItemNum);
76094332d3Sopenharmony_ci    index = GetRegGroupIndexByTime(temp, g_timeMap, timeItemNum);
77094332d3Sopenharmony_ci    if (index < 0) {
78094332d3Sopenharmony_ci        HDF_LOGE("%s: Index out of range ", __func__);
79094332d3Sopenharmony_ci        return HDF_FAILURE;
80094332d3Sopenharmony_ci    }
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ci    if (((rgbcData[ALS_R] * BH1745_MULTIPLE_100 > BH1745_TIME_MAX) ||
83094332d3Sopenharmony_ci        (rgbcData[ALS_G] * BH1745_MULTIPLE_100 > BH1745_TIME_MAX)) && (temp >= BH1745_TIME_320MSEC)) {
84094332d3Sopenharmony_ci        g_timeChangeStatus = SENSOR_TIME_DECREASE;
85094332d3Sopenharmony_ci        index = GetRegGroupIndexByTime(temp, g_timeMap, timeItemNum);
86094332d3Sopenharmony_ci        index--;
87094332d3Sopenharmony_ci
88094332d3Sopenharmony_ci        ret = WriteSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, sizeof(regValue));
89094332d3Sopenharmony_ci        CHECK_SENSOR_REGIARRAY_RETURN_VALUE(ret, "write");
90094332d3Sopenharmony_ci    } else if (((rgbcData[ALS_R] * BH1745_MULTIPLE_100 < BH1745_TIME_MIN) ||
91094332d3Sopenharmony_ci        (rgbcData[ALS_G] * BH1745_MULTIPLE_100 < BH1745_TIME_MIN)) && (g_timeChangeStatus == SENSOR_TIME_DECREASE)) {
92094332d3Sopenharmony_ci        g_timeChangeStatus = SENSOR_TIME_INCREASE;
93094332d3Sopenharmony_ci        index = GetRegGroupIndexByTime(temp, g_timeMap, timeItemNum);
94094332d3Sopenharmony_ci        index++;
95094332d3Sopenharmony_ci        if (index >= timeItemNum) {
96094332d3Sopenharmony_ci            HDF_LOGE("%s: Index out of range ", __func__);
97094332d3Sopenharmony_ci            return HDF_FAILURE;
98094332d3Sopenharmony_ci        }
99094332d3Sopenharmony_ci
100094332d3Sopenharmony_ci        ret = WriteSensorRegCfgArray(&CfgData->busCfg, timeGroupNode, index, sizeof(regValue));
101094332d3Sopenharmony_ci        CHECK_SENSOR_REGIARRAY_RETURN_VALUE(ret, "write");
102094332d3Sopenharmony_ci    }
103094332d3Sopenharmony_ci
104094332d3Sopenharmony_ci    return HDF_SUCCESS;
105094332d3Sopenharmony_ci}
106094332d3Sopenharmony_ci
107094332d3Sopenharmony_cistatic int32_t CalLux(struct SensorCfgData *CfgData, struct AlsReportData *reportData, uint32_t *rgbcData)
108094332d3Sopenharmony_ci{
109094332d3Sopenharmony_ci    int32_t ret;
110094332d3Sopenharmony_ci    uint32_t time;
111094332d3Sopenharmony_ci    uint32_t gain;
112094332d3Sopenharmony_ci    uint8_t regValue;
113094332d3Sopenharmony_ci    uint32_t index = 1;
114094332d3Sopenharmony_ci    uint32_t luxTemp;
115094332d3Sopenharmony_ci    uint8_t itemNum;
116094332d3Sopenharmony_ci    struct SensorRegCfgGroupNode *groupNode = NULL;
117094332d3Sopenharmony_ci    int32_t timeIndex = EXTENDED_ALS_TIME_GROUP_INDEX_0;
118094332d3Sopenharmony_ci    int32_t gainIndex = EXTENDED_ALS_GAIN_GROUP_INDEX_0;
119094332d3Sopenharmony_ci
120094332d3Sopenharmony_ci    if (rgbcData[ALS_G] <= 0) {
121094332d3Sopenharmony_ci        HDF_LOGE("%s: RgbcData out of range ", __func__);
122094332d3Sopenharmony_ci        return HDF_FAILURE;
123094332d3Sopenharmony_ci    }
124094332d3Sopenharmony_ci
125094332d3Sopenharmony_ci    if (BH1745_MULTIPLE_100 * rgbcData[ALS_C] / rgbcData[ALS_G] < BH1745_COEFFICIENT_JUDGE) {
126094332d3Sopenharmony_ci        index = 0;
127094332d3Sopenharmony_ci    }
128094332d3Sopenharmony_ci
129094332d3Sopenharmony_ci    luxTemp = g_red[index] * rgbcData[ALS_R] + g_green[index] * rgbcData[ALS_G];
130094332d3Sopenharmony_ci    groupNode = CfgData->extendedRegCfgGroup[EXTENDED_ALS_TIME_GROUP];
131094332d3Sopenharmony_ci    itemNum = groupNode->itemNum;
132094332d3Sopenharmony_ci    if (itemNum > EXTENDED_ALS_TIME_GROUP_INDEX_MAX) {
133094332d3Sopenharmony_ci        HDF_LOGE("%s: ItemNum out of range ", __func__);
134094332d3Sopenharmony_ci        return HDF_FAILURE;
135094332d3Sopenharmony_ci    }
136094332d3Sopenharmony_ci
137094332d3Sopenharmony_ci    ret = ReadSensorRegCfgArray(&CfgData->busCfg, groupNode, timeIndex, &regValue, sizeof(regValue));
138094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
139094332d3Sopenharmony_ci        HDF_LOGE("%s: Failed to read sensor register array ", __func__);
140094332d3Sopenharmony_ci        return HDF_FAILURE;
141094332d3Sopenharmony_ci    }
142094332d3Sopenharmony_ci    regValue &= groupNode->regCfgItem->mask;
143094332d3Sopenharmony_ci    time = GetTimeByRegValue(regValue, g_timeMap, itemNum);
144094332d3Sopenharmony_ci
145094332d3Sopenharmony_ci    regValue = 0;
146094332d3Sopenharmony_ci    groupNode = CfgData->extendedRegCfgGroup[EXTENDED_ALS_GAIN_GROUP];
147094332d3Sopenharmony_ci    itemNum = groupNode->itemNum;
148094332d3Sopenharmony_ci
149094332d3Sopenharmony_ci    ret = ReadSensorRegCfgArray(&CfgData->busCfg, groupNode, gainIndex, &regValue, sizeof(regValue));
150094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
151094332d3Sopenharmony_ci        HDF_LOGE("%s: Failed to read sensor register array ", __func__);
152094332d3Sopenharmony_ci        return HDF_FAILURE;
153094332d3Sopenharmony_ci    }
154094332d3Sopenharmony_ci    regValue &= groupNode->regCfgItem->mask;
155094332d3Sopenharmony_ci    gain = GetGainByRegValue(regValue, g_gainMap, itemNum);
156094332d3Sopenharmony_ci    if (gain == SENSOR_GAIN_INCREASE || time == SENSOR_TIME_INCREASE) {
157094332d3Sopenharmony_ci        return HDF_FAILURE;
158094332d3Sopenharmony_ci    } else {
159094332d3Sopenharmony_ci        reportData->als = ((luxTemp / gain) * BH1745_GAIN_16X / time) * BH1745_TIME_160MSEC;
160094332d3Sopenharmony_ci    }
161094332d3Sopenharmony_ci
162094332d3Sopenharmony_ci    return HDF_SUCCESS;
163094332d3Sopenharmony_ci}
164094332d3Sopenharmony_ci
165094332d3Sopenharmony_cistatic int32_t RawDataConvert(struct SensorCfgData *CfgData, struct AlsReportData *reportData, uint32_t *rgbcData)
166094332d3Sopenharmony_ci{
167094332d3Sopenharmony_ci    int ret;
168094332d3Sopenharmony_ci
169094332d3Sopenharmony_ci    ret = CalLux(CfgData, reportData, rgbcData);
170094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
171094332d3Sopenharmony_ci        HDF_LOGE("%s: Failed to calculate sensor brightness ", __func__);
172094332d3Sopenharmony_ci        return HDF_FAILURE;
173094332d3Sopenharmony_ci    }
174094332d3Sopenharmony_ci
175094332d3Sopenharmony_ci    reportData->als = (reportData->als > 0) ? reportData->als : 0;
176094332d3Sopenharmony_ci
177094332d3Sopenharmony_ci    ret = DynamicRangCovert(CfgData, rgbcData);
178094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "DynamicRangCovert");
179094332d3Sopenharmony_ci
180094332d3Sopenharmony_ci    return HDF_SUCCESS;
181094332d3Sopenharmony_ci}
182094332d3Sopenharmony_ci
183094332d3Sopenharmony_cistatic int32_t ReadBh1745RawData(struct SensorCfgData *data, struct AlsData *rawData, uint64_t *timestamp)
184094332d3Sopenharmony_ci{
185094332d3Sopenharmony_ci    uint8_t status = 0;
186094332d3Sopenharmony_ci    uint8_t reg[ALS_LIGHT_BUTT];
187094332d3Sopenharmony_ci    OsalTimespec time;
188094332d3Sopenharmony_ci
189094332d3Sopenharmony_ci    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
190094332d3Sopenharmony_ci    (void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
191094332d3Sopenharmony_ci
192094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
193094332d3Sopenharmony_ci
194094332d3Sopenharmony_ci    if (OsalGetTime(&time) != HDF_SUCCESS) {
195094332d3Sopenharmony_ci        HDF_LOGE("%s: Get time failed", __func__);
196094332d3Sopenharmony_ci        return HDF_FAILURE;
197094332d3Sopenharmony_ci    }
198094332d3Sopenharmony_ci    *timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
199094332d3Sopenharmony_ci
200094332d3Sopenharmony_ci    int32_t ret = ReadSensor(&data->busCfg, BH1745_MODECONTROL3_ADDR, &status, sizeof(uint8_t));
201094332d3Sopenharmony_ci    if (!(status & BH1745_ALS_DATA_READY_MASK) || (ret != HDF_SUCCESS)) {
202094332d3Sopenharmony_ci        HDF_LOGE("%s: data status [%u] ret [%d]", __func__, status, ret);
203094332d3Sopenharmony_ci        return HDF_FAILURE;
204094332d3Sopenharmony_ci    }
205094332d3Sopenharmony_ci
206094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BH1745_ALS_R_LSB_ADDR, &reg[ALS_R_LSB], sizeof(uint8_t));
207094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
208094332d3Sopenharmony_ci
209094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BH1745_ALS_R_MSB_ADDR, &reg[ALS_R_MSB], sizeof(uint8_t));
210094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
211094332d3Sopenharmony_ci
212094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BH1745_ALS_G_LSB_ADDR, &reg[ALS_G_LSB], sizeof(uint8_t));
213094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
214094332d3Sopenharmony_ci
215094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BH1745_ALS_G_MSB_ADDR, &reg[ALS_G_MSB], sizeof(uint8_t));
216094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
217094332d3Sopenharmony_ci
218094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BH1745_ALS_B_LSB_ADDR, &reg[ALS_B_LSB], sizeof(uint8_t));
219094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
220094332d3Sopenharmony_ci
221094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BH1745_ALS_B_MSB_ADDR, &reg[ALS_B_MSB], sizeof(uint8_t));
222094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
223094332d3Sopenharmony_ci
224094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BH1745_ALS_C_LSB_ADDR, &reg[ALS_C_LSB], sizeof(uint8_t));
225094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
226094332d3Sopenharmony_ci
227094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BH1745_ALS_C_MSB_ADDR, &reg[ALS_C_MSB], sizeof(uint8_t));
228094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
229094332d3Sopenharmony_ci
230094332d3Sopenharmony_ci    rawData->red = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ALS_R_MSB], SENSOR_DATA_WIDTH_8_BIT) |
231094332d3Sopenharmony_ci        reg[ALS_R_LSB]);
232094332d3Sopenharmony_ci    rawData->green = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ALS_G_MSB], SENSOR_DATA_WIDTH_8_BIT) |
233094332d3Sopenharmony_ci        reg[ALS_G_LSB]);
234094332d3Sopenharmony_ci    rawData->blue = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ALS_B_MSB], SENSOR_DATA_WIDTH_8_BIT) |
235094332d3Sopenharmony_ci        reg[ALS_B_LSB]);
236094332d3Sopenharmony_ci    rawData->clear = (uint16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ALS_C_MSB], SENSOR_DATA_WIDTH_8_BIT) |
237094332d3Sopenharmony_ci        reg[ALS_C_LSB]);
238094332d3Sopenharmony_ci
239094332d3Sopenharmony_ci    return HDF_SUCCESS;
240094332d3Sopenharmony_ci}
241094332d3Sopenharmony_ci
242094332d3Sopenharmony_cistatic int32_t ReadBh1745Data(struct SensorCfgData *data, struct SensorReportEvent *event)
243094332d3Sopenharmony_ci{
244094332d3Sopenharmony_ci    int32_t ret;
245094332d3Sopenharmony_ci    struct AlsData rawData = { 0, 0, 0, 0 };
246094332d3Sopenharmony_ci    uint32_t tmp[ALS_LIGHT_NUM];
247094332d3Sopenharmony_ci    static struct AlsReportData reportData;
248094332d3Sopenharmony_ci
249094332d3Sopenharmony_ci    ret = ReadBh1745RawData(data, &rawData, &event->timestamp);
250094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
251094332d3Sopenharmony_ci        HDF_LOGE("%s: BH1745 read raw data failed", __func__);
252094332d3Sopenharmony_ci        return HDF_FAILURE;
253094332d3Sopenharmony_ci    }
254094332d3Sopenharmony_ci
255094332d3Sopenharmony_ci    event->sensorId = SENSOR_TAG_AMBIENT_LIGHT;
256094332d3Sopenharmony_ci    event->option = 0;
257094332d3Sopenharmony_ci    event->mode = SENSOR_WORK_MODE_REALTIME;
258094332d3Sopenharmony_ci
259094332d3Sopenharmony_ci    tmp[ALS_R] = rawData.red;
260094332d3Sopenharmony_ci    tmp[ALS_G] = rawData.green;
261094332d3Sopenharmony_ci    tmp[ALS_B] = rawData.blue;
262094332d3Sopenharmony_ci    tmp[ALS_C] = rawData.clear;
263094332d3Sopenharmony_ci
264094332d3Sopenharmony_ci    ret = RawDataConvert(data, &reportData, tmp);
265094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "RawDataConvert");
266094332d3Sopenharmony_ci
267094332d3Sopenharmony_ci    event->dataLen = sizeof(reportData.als);
268094332d3Sopenharmony_ci    event->data = (uint8_t *)&reportData.als;
269094332d3Sopenharmony_ci
270094332d3Sopenharmony_ci    return ret;
271094332d3Sopenharmony_ci}
272094332d3Sopenharmony_ci
273094332d3Sopenharmony_cistatic int32_t InitBh1745(struct SensorCfgData *data)
274094332d3Sopenharmony_ci{
275094332d3Sopenharmony_ci    int32_t ret;
276094332d3Sopenharmony_ci
277094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
278094332d3Sopenharmony_ci    ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
279094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
280094332d3Sopenharmony_ci        HDF_LOGE("%s: BH1745 sensor init config failed", __func__);
281094332d3Sopenharmony_ci        return HDF_FAILURE;
282094332d3Sopenharmony_ci    }
283094332d3Sopenharmony_ci
284094332d3Sopenharmony_ci    return HDF_SUCCESS;
285094332d3Sopenharmony_ci}
286094332d3Sopenharmony_ci
287094332d3Sopenharmony_cistatic int32_t InitAlsPreConfig(void)
288094332d3Sopenharmony_ci{
289094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
290094332d3Sopenharmony_ci        HDF_LOGE("%s: Data write mux pin failed", __func__);
291094332d3Sopenharmony_ci        return HDF_FAILURE;
292094332d3Sopenharmony_ci    }
293094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
294094332d3Sopenharmony_ci        HDF_LOGE("%s: Clk write mux pin failed", __func__);
295094332d3Sopenharmony_ci        return HDF_FAILURE;
296094332d3Sopenharmony_ci    }
297094332d3Sopenharmony_ci
298094332d3Sopenharmony_ci    return HDF_SUCCESS;
299094332d3Sopenharmony_ci}
300094332d3Sopenharmony_ci
301094332d3Sopenharmony_cistatic int32_t DispatchBH1745(struct HdfDeviceIoClient *client,
302094332d3Sopenharmony_ci    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
303094332d3Sopenharmony_ci{
304094332d3Sopenharmony_ci    (void)client;
305094332d3Sopenharmony_ci    (void)cmd;
306094332d3Sopenharmony_ci    (void)data;
307094332d3Sopenharmony_ci    (void)reply;
308094332d3Sopenharmony_ci
309094332d3Sopenharmony_ci    return HDF_SUCCESS;
310094332d3Sopenharmony_ci}
311094332d3Sopenharmony_ci
312094332d3Sopenharmony_cistatic int32_t Bh1745BindDriver(struct HdfDeviceObject *device)
313094332d3Sopenharmony_ci{
314094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
315094332d3Sopenharmony_ci
316094332d3Sopenharmony_ci    struct Bh1745DrvData *drvData = (struct Bh1745DrvData *)OsalMemCalloc(sizeof(*drvData));
317094332d3Sopenharmony_ci    if (drvData == NULL) {
318094332d3Sopenharmony_ci        HDF_LOGE("%s: Malloc Bh1745 drv data fail", __func__);
319094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
320094332d3Sopenharmony_ci    }
321094332d3Sopenharmony_ci
322094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchBH1745;
323094332d3Sopenharmony_ci    drvData->device = device;
324094332d3Sopenharmony_ci    device->service = &drvData->ioService;
325094332d3Sopenharmony_ci    g_bh1745DrvData = drvData;
326094332d3Sopenharmony_ci
327094332d3Sopenharmony_ci    return HDF_SUCCESS;
328094332d3Sopenharmony_ci}
329094332d3Sopenharmony_ci
330094332d3Sopenharmony_cistatic int32_t Bh1745InitDriver(struct HdfDeviceObject *device)
331094332d3Sopenharmony_ci{
332094332d3Sopenharmony_ci    int32_t ret;
333094332d3Sopenharmony_ci    struct AlsOpsCall ops;
334094332d3Sopenharmony_ci
335094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
336094332d3Sopenharmony_ci    struct Bh1745DrvData *drvData = (struct Bh1745DrvData *)device->service;
337094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
338094332d3Sopenharmony_ci
339094332d3Sopenharmony_ci    ret = InitAlsPreConfig();
340094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
341094332d3Sopenharmony_ci        HDF_LOGE("%s: Init  BH1745 bus mux config", __func__);
342094332d3Sopenharmony_ci        return HDF_FAILURE;
343094332d3Sopenharmony_ci    }
344094332d3Sopenharmony_ci
345094332d3Sopenharmony_ci    drvData->sensorCfg = AlsCreateCfgData(device->property);
346094332d3Sopenharmony_ci    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
347094332d3Sopenharmony_ci        HDF_LOGD("%s: Creating alscfg failed because detection failed", __func__);
348094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
349094332d3Sopenharmony_ci    }
350094332d3Sopenharmony_ci
351094332d3Sopenharmony_ci    ops.Init = NULL;
352094332d3Sopenharmony_ci    ops.ReadData = ReadBh1745Data;
353094332d3Sopenharmony_ci    ret = AlsRegisterChipOps(&ops);
354094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
355094332d3Sopenharmony_ci        HDF_LOGE("%s: Register BH1745 als failed", __func__);
356094332d3Sopenharmony_ci        return HDF_FAILURE;
357094332d3Sopenharmony_ci    }
358094332d3Sopenharmony_ci
359094332d3Sopenharmony_ci    ret = InitBh1745(drvData->sensorCfg);
360094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
361094332d3Sopenharmony_ci        HDF_LOGE("%s: Init BH1745 als failed", __func__);
362094332d3Sopenharmony_ci        return HDF_FAILURE;
363094332d3Sopenharmony_ci    }
364094332d3Sopenharmony_ci
365094332d3Sopenharmony_ci    return HDF_SUCCESS;
366094332d3Sopenharmony_ci}
367094332d3Sopenharmony_ci
368094332d3Sopenharmony_cistatic void Bh1745ReleaseDriver(struct HdfDeviceObject *device)
369094332d3Sopenharmony_ci{
370094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(device);
371094332d3Sopenharmony_ci
372094332d3Sopenharmony_ci    struct Bh1745DrvData *drvData = (struct Bh1745DrvData *)device->service;
373094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(drvData);
374094332d3Sopenharmony_ci
375094332d3Sopenharmony_ci    if (drvData->sensorCfg != NULL) {
376094332d3Sopenharmony_ci        AlsReleaseCfgData(drvData->sensorCfg);
377094332d3Sopenharmony_ci        drvData->sensorCfg = NULL;
378094332d3Sopenharmony_ci    }
379094332d3Sopenharmony_ci    OsalMemFree(drvData);
380094332d3Sopenharmony_ci}
381094332d3Sopenharmony_ci
382094332d3Sopenharmony_cistruct HdfDriverEntry g_alsBh1745DevEntry = {
383094332d3Sopenharmony_ci    .moduleVersion = 1,
384094332d3Sopenharmony_ci    .moduleName = "HDF_SENSOR_ALS_BH1745",
385094332d3Sopenharmony_ci    .Bind = Bh1745BindDriver,
386094332d3Sopenharmony_ci    .Init = Bh1745InitDriver,
387094332d3Sopenharmony_ci    .Release = Bh1745ReleaseDriver,
388094332d3Sopenharmony_ci};
389094332d3Sopenharmony_ci
390094332d3Sopenharmony_ciHDF_INIT(g_alsBh1745DevEntry);
391