1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022-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 "vibrator_drv2605l_driver.h"
10094332d3Sopenharmony_ci#include <securec.h>
11094332d3Sopenharmony_ci#include "device_resource_if.h"
12094332d3Sopenharmony_ci#include "hdf_base.h"
13094332d3Sopenharmony_ci#include "hdf_log.h"
14094332d3Sopenharmony_ci#include "i2c_if.h"
15094332d3Sopenharmony_ci#include "hdf_workqueue.h"
16094332d3Sopenharmony_ci#include "osal_mutex.h"
17094332d3Sopenharmony_ci#include "osal_mem.h"
18094332d3Sopenharmony_ci#include "vibrator_driver.h"
19094332d3Sopenharmony_ci#include "vibrator_parser.h"
20094332d3Sopenharmony_ci#include "vibrator_driver_type.h"
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_ci#define HDF_LOG_TAG    khdf_vibrator_driver
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_cistruct Drv2605lDriverData *g_drv2605lDrvData = NULL;
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_cistatic struct Drv2605lDriverData *GetDrv2605lDrvData(void)
27094332d3Sopenharmony_ci{
28094332d3Sopenharmony_ci    return g_drv2605lDrvData;
29094332d3Sopenharmony_ci}
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_cistatic int32_t GetDrv2605lI2cHandle(struct VibratorI2cCfg *busCfg)
32094332d3Sopenharmony_ci{
33094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(busCfg, HDF_ERR_INVALID_PARAM);
34094332d3Sopenharmony_ci
35094332d3Sopenharmony_ci    busCfg->handle = I2cOpen(busCfg->busNum);
36094332d3Sopenharmony_ci    if (busCfg->handle == NULL) {
37094332d3Sopenharmony_ci        HDF_LOGE("%s: drv2605l i2c Handle invalid", __func__);
38094332d3Sopenharmony_ci        return HDF_FAILURE;
39094332d3Sopenharmony_ci    }
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_ci    return HDF_SUCCESS;
42094332d3Sopenharmony_ci}
43094332d3Sopenharmony_ci
44094332d3Sopenharmony_cistatic void ReleaseDrv2605lBusHandle(struct VibratorI2cCfg *busCfg)
45094332d3Sopenharmony_ci{
46094332d3Sopenharmony_ci    if (busCfg == NULL) {
47094332d3Sopenharmony_ci        HDF_LOGE("%s: drv2605l i2c config invalid", __func__);
48094332d3Sopenharmony_ci        return;
49094332d3Sopenharmony_ci    }
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ci    if (busCfg->handle != NULL) {
52094332d3Sopenharmony_ci        I2cClose(busCfg->handle);
53094332d3Sopenharmony_ci        busCfg->handle = NULL;
54094332d3Sopenharmony_ci    }
55094332d3Sopenharmony_ci}
56094332d3Sopenharmony_ci
57094332d3Sopenharmony_cistatic int32_t ReadDrv2605l(struct VibratorI2cCfg *busCfg, uint16_t regAddr, uint8_t *data, uint16_t dataLen)
58094332d3Sopenharmony_ci{
59094332d3Sopenharmony_ci    int32_t index = 0;
60094332d3Sopenharmony_ci    unsigned char regBuf[I2C_REG_BUF_LEN] = {0};
61094332d3Sopenharmony_ci    struct I2cMsg msg[I2C_READ_MSG_NUM];
62094332d3Sopenharmony_ci
63094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(busCfg, HDF_FAILURE);
64094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(data, HDF_FAILURE);
65094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(busCfg->handle, HDF_FAILURE);
66094332d3Sopenharmony_ci
67094332d3Sopenharmony_ci    msg[I2C_READ_MSG_ADDR_IDX].addr = busCfg->devAddr;
68094332d3Sopenharmony_ci    msg[I2C_READ_MSG_ADDR_IDX].flags = 0;
69094332d3Sopenharmony_ci    msg[I2C_READ_MSG_ADDR_IDX].len = busCfg->regWidth;
70094332d3Sopenharmony_ci    msg[I2C_READ_MSG_ADDR_IDX].buf = regBuf;
71094332d3Sopenharmony_ci
72094332d3Sopenharmony_ci    if (busCfg->regWidth == DRV2605L_ADDR_WIDTH_1_BYTE) {
73094332d3Sopenharmony_ci        regBuf[index++] = regAddr & I2C_BYTE_MASK;
74094332d3Sopenharmony_ci    } else if (busCfg->regWidth == DRV2605L_ADDR_WIDTH_2_BYTE) {
75094332d3Sopenharmony_ci        regBuf[index++] = (regAddr >> I2C_BYTE_OFFSET) & I2C_BYTE_MASK;
76094332d3Sopenharmony_ci        regBuf[index++] = regAddr & I2C_BYTE_MASK;
77094332d3Sopenharmony_ci    } else {
78094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c regWidth[%u] failed", __func__, busCfg->regWidth);
79094332d3Sopenharmony_ci        return HDF_FAILURE;
80094332d3Sopenharmony_ci    }
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ci    msg[I2C_READ_MSG_VALUE_IDX].addr = busCfg->devAddr;
83094332d3Sopenharmony_ci    msg[I2C_READ_MSG_VALUE_IDX].flags = I2C_FLAG_READ;
84094332d3Sopenharmony_ci    msg[I2C_READ_MSG_VALUE_IDX].len = dataLen;
85094332d3Sopenharmony_ci    msg[I2C_READ_MSG_VALUE_IDX].buf = data;
86094332d3Sopenharmony_ci
87094332d3Sopenharmony_ci    if (I2cTransfer(busCfg->handle, msg, I2C_READ_MSG_NUM) != I2C_READ_MSG_NUM) {
88094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c[%u] read failed", __func__, busCfg->busNum);
89094332d3Sopenharmony_ci        return HDF_FAILURE;
90094332d3Sopenharmony_ci    }
91094332d3Sopenharmony_ci
92094332d3Sopenharmony_ci    return HDF_SUCCESS;
93094332d3Sopenharmony_ci}
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_cistatic int32_t WriteDrv2605l(struct VibratorI2cCfg *busCfg, uint8_t *writeData, uint16_t dataLen)
96094332d3Sopenharmony_ci{
97094332d3Sopenharmony_ci    struct I2cMsg msg[I2C_WRITE_MSG_NUM];
98094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(busCfg, HDF_FAILURE);
99094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(writeData, HDF_FAILURE);
100094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(busCfg->handle, HDF_FAILURE);
101094332d3Sopenharmony_ci
102094332d3Sopenharmony_ci    msg[0].addr = busCfg->devAddr;
103094332d3Sopenharmony_ci    msg[0].flags = 0;
104094332d3Sopenharmony_ci    msg[0].len = dataLen;
105094332d3Sopenharmony_ci    msg[0].buf = writeData;
106094332d3Sopenharmony_ci
107094332d3Sopenharmony_ci    if (I2cTransfer(busCfg->handle, msg, I2C_WRITE_MSG_NUM) != I2C_WRITE_MSG_NUM) {
108094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c[%u] write failed", __func__, busCfg->busNum);
109094332d3Sopenharmony_ci        return HDF_FAILURE;
110094332d3Sopenharmony_ci    }
111094332d3Sopenharmony_ci
112094332d3Sopenharmony_ci    return HDF_SUCCESS;
113094332d3Sopenharmony_ci}
114094332d3Sopenharmony_ci
115094332d3Sopenharmony_cistatic int32_t DetectDrv2605lDevice(struct Drv2605lDriverData *drvData)
116094332d3Sopenharmony_ci{
117094332d3Sopenharmony_ci    uint8_t value;
118094332d3Sopenharmony_ci    uint16_t chipIdReg;
119094332d3Sopenharmony_ci    uint16_t chipIdValue;
120094332d3Sopenharmony_ci    int32_t ret;
121094332d3Sopenharmony_ci
122094332d3Sopenharmony_ci    chipIdReg = drvData->drv2605lCfgData->vibratorAttr.chipIdReg;
123094332d3Sopenharmony_ci    chipIdValue = drvData->drv2605lCfgData->vibratorAttr.chipIdValue;
124094332d3Sopenharmony_ci
125094332d3Sopenharmony_ci    ret = GetDrv2605lI2cHandle(&drvData->drv2605lCfgData->vibratorBus.i2cCfg);
126094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
127094332d3Sopenharmony_ci        HDF_LOGE("%s: get drv2605l bus handle failed", __func__);
128094332d3Sopenharmony_ci        ReleaseDrv2605lBusHandle(&drvData->drv2605lCfgData->vibratorBus.i2cCfg);
129094332d3Sopenharmony_ci        return HDF_FAILURE;
130094332d3Sopenharmony_ci    }
131094332d3Sopenharmony_ci
132094332d3Sopenharmony_ci    ret = ReadDrv2605l(&drvData->drv2605lCfgData->vibratorBus.i2cCfg, chipIdReg, &value, sizeof(value));
133094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
134094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c read chip id failed", __func__);
135094332d3Sopenharmony_ci        ReleaseDrv2605lBusHandle(&drvData->drv2605lCfgData->vibratorBus.i2cCfg);
136094332d3Sopenharmony_ci        return HDF_FAILURE;
137094332d3Sopenharmony_ci    }
138094332d3Sopenharmony_ci
139094332d3Sopenharmony_ci    if (value != chipIdValue) {
140094332d3Sopenharmony_ci        HDF_LOGE("%s: drv2605l chip detect failed", __func__);
141094332d3Sopenharmony_ci        ReleaseDrv2605lBusHandle(&drvData->drv2605lCfgData->vibratorBus.i2cCfg);
142094332d3Sopenharmony_ci        return HDF_FAILURE;
143094332d3Sopenharmony_ci    }
144094332d3Sopenharmony_ci
145094332d3Sopenharmony_ci    HDF_LOGD("%s: drv2605l detect chip success", __func__);
146094332d3Sopenharmony_ci    return HDF_SUCCESS;
147094332d3Sopenharmony_ci}
148094332d3Sopenharmony_ci
149094332d3Sopenharmony_cistatic int32_t InitDrv2605lChip(struct VibratorCfgData *drv2605lCfgData)
150094332d3Sopenharmony_ci{
151094332d3Sopenharmony_ci    uint8_t value[DRV2605L_VALUE_BUTT];
152094332d3Sopenharmony_ci
153094332d3Sopenharmony_ci    value[DRV2605L_ADDR_INDEX] = (uint8_t)DRV2605_REG_CONTROL3;
154094332d3Sopenharmony_ci    value[DRV2605L_VALUE_INDEX] = (uint8_t)DRV2605_MODE_OPEN_LOOP;
155094332d3Sopenharmony_ci    if (WriteDrv2605l(&drv2605lCfgData->vibratorBus.i2cCfg, value, sizeof(value)) != HDF_SUCCESS) {
156094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c addr [%0X] write failed", __func__, value[DRV2605L_ADDR_INDEX]);
157094332d3Sopenharmony_ci        return HDF_FAILURE;
158094332d3Sopenharmony_ci    }
159094332d3Sopenharmony_ci
160094332d3Sopenharmony_ci    value[DRV2605L_ADDR_INDEX] = (uint8_t)DRV2605_REG_FEEDBACK;
161094332d3Sopenharmony_ci    value[DRV2605L_VALUE_INDEX] = (uint8_t)DRV2605_MODE_LRA;
162094332d3Sopenharmony_ci    if (WriteDrv2605l(&drv2605lCfgData->vibratorBus.i2cCfg, value, sizeof(value)) != HDF_SUCCESS) {
163094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c addr [%0X] write failed", __func__, value[DRV2605L_ADDR_INDEX]);
164094332d3Sopenharmony_ci        return HDF_FAILURE;
165094332d3Sopenharmony_ci    }
166094332d3Sopenharmony_ci
167094332d3Sopenharmony_ci    value[DRV2605L_ADDR_INDEX] = (uint8_t)DRV2605_REG_RTPIN;
168094332d3Sopenharmony_ci    value[DRV2605L_VALUE_INDEX] = (uint8_t)&drv2605lCfgData->vibratorAttr.defaultIntensity;
169094332d3Sopenharmony_ci    if (WriteDrv2605l(&drv2605lCfgData->vibratorBus.i2cCfg, value, sizeof(value)) != HDF_SUCCESS) {
170094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c addr [%0X] write failed", __func__, value[DRV2605L_ADDR_INDEX]);
171094332d3Sopenharmony_ci        return HDF_FAILURE;
172094332d3Sopenharmony_ci    }
173094332d3Sopenharmony_ci
174094332d3Sopenharmony_ci    value[DRV2605L_ADDR_INDEX] = (uint8_t)DRV2605_REG_LRARESON;
175094332d3Sopenharmony_ci    value[DRV2605L_VALUE_INDEX] = (uint8_t)&drv2605lCfgData->vibratorAttr.defaultFrequency;
176094332d3Sopenharmony_ci    if (WriteDrv2605l(&drv2605lCfgData->vibratorBus.i2cCfg, value, sizeof(value)) != HDF_SUCCESS) {
177094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c addr [%0X] write failed", __func__, value[DRV2605L_ADDR_INDEX]);
178094332d3Sopenharmony_ci        return HDF_FAILURE;
179094332d3Sopenharmony_ci    }
180094332d3Sopenharmony_ci
181094332d3Sopenharmony_ci    return HDF_SUCCESS;
182094332d3Sopenharmony_ci}
183094332d3Sopenharmony_ci
184094332d3Sopenharmony_cistatic int32_t SetModulationParameter(uint16_t intensity, int16_t frequency)
185094332d3Sopenharmony_ci{
186094332d3Sopenharmony_ci    uint8_t value[DRV2605L_VALUE_BUTT];
187094332d3Sopenharmony_ci    struct Drv2605lDriverData *drvData = NULL;
188094332d3Sopenharmony_ci    drvData = GetDrv2605lDrvData();
189094332d3Sopenharmony_ci
190094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
191094332d3Sopenharmony_ci
192094332d3Sopenharmony_ci    if (intensity != 0) {
193094332d3Sopenharmony_ci        value[DRV2605L_ADDR_INDEX] = (uint8_t)DRV2605_REG_RTPIN;
194094332d3Sopenharmony_ci        value[DRV2605L_VALUE_INDEX] = (uint8_t)INTENSITY_MAPPING_VALUE(intensity);
195094332d3Sopenharmony_ci        if (WriteDrv2605l(&drvData->drv2605lCfgData->vibratorBus.i2cCfg, value, sizeof(value)) != HDF_SUCCESS) {
196094332d3Sopenharmony_ci            HDF_LOGE("%s: i2c addr [%0X] write failed", __func__, value[DRV2605L_ADDR_INDEX]);
197094332d3Sopenharmony_ci            return HDF_FAILURE;
198094332d3Sopenharmony_ci        }
199094332d3Sopenharmony_ci    } else {
200094332d3Sopenharmony_ci        HDF_LOGD("%s: the setting of intensity 0 is not supported and \
201094332d3Sopenharmony_ci            will be set as the system default intensity", __func__);
202094332d3Sopenharmony_ci    }
203094332d3Sopenharmony_ci
204094332d3Sopenharmony_ci    if (frequency != 0) {
205094332d3Sopenharmony_ci        value[DRV2605L_ADDR_INDEX] = (uint8_t)DRV2605_REG_LRARESON;
206094332d3Sopenharmony_ci        value[DRV2605L_VALUE_INDEX] = (uint8_t)FREQUENCY_MAPPING_VALUE(frequency);
207094332d3Sopenharmony_ci        if (WriteDrv2605l(&drvData->drv2605lCfgData->vibratorBus.i2cCfg, value, sizeof(value)) != HDF_SUCCESS) {
208094332d3Sopenharmony_ci            HDF_LOGE("%s: i2c addr [%0X] write failed", __func__, value[DRV2605L_ADDR_INDEX]);
209094332d3Sopenharmony_ci            return HDF_FAILURE;
210094332d3Sopenharmony_ci        }
211094332d3Sopenharmony_ci    } else {
212094332d3Sopenharmony_ci        HDF_LOGD("%s: the setting of frequency 0 is not supported and \
213094332d3Sopenharmony_ci            will be set as the system default frequency", __func__);
214094332d3Sopenharmony_ci    }
215094332d3Sopenharmony_ci
216094332d3Sopenharmony_ci    return HDF_SUCCESS;
217094332d3Sopenharmony_ci}
218094332d3Sopenharmony_ci
219094332d3Sopenharmony_cistatic int32_t StartModulationParameter(void)
220094332d3Sopenharmony_ci{
221094332d3Sopenharmony_ci    uint8_t value[DRV2605L_VALUE_BUTT];
222094332d3Sopenharmony_ci    struct Drv2605lDriverData *drvData = NULL;
223094332d3Sopenharmony_ci    drvData = GetDrv2605lDrvData();
224094332d3Sopenharmony_ci
225094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
226094332d3Sopenharmony_ci
227094332d3Sopenharmony_ci    value[DRV2605L_ADDR_INDEX] = (uint8_t)DRV2605_REG_MODE;
228094332d3Sopenharmony_ci    value[DRV2605L_VALUE_INDEX] = (uint8_t)DRV2605_MODE_REALTIME;
229094332d3Sopenharmony_ci    if (WriteDrv2605l(&drvData->drv2605lCfgData->vibratorBus.i2cCfg, value, sizeof(value)) != HDF_SUCCESS) {
230094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c addr [%0X] write failed", __func__, value[DRV2605L_ADDR_INDEX]);
231094332d3Sopenharmony_ci        return HDF_FAILURE;
232094332d3Sopenharmony_ci    }
233094332d3Sopenharmony_ci
234094332d3Sopenharmony_ci    return HDF_SUCCESS;
235094332d3Sopenharmony_ci}
236094332d3Sopenharmony_ci
237094332d3Sopenharmony_cistatic int32_t StopModulationParameter(void)
238094332d3Sopenharmony_ci{
239094332d3Sopenharmony_ci    uint8_t value[DRV2605L_VALUE_BUTT];
240094332d3Sopenharmony_ci    struct Drv2605lDriverData *drvData = NULL;
241094332d3Sopenharmony_ci    drvData = GetDrv2605lDrvData();
242094332d3Sopenharmony_ci
243094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
244094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData->drv2605lCfgData, HDF_FAILURE);
245094332d3Sopenharmony_ci
246094332d3Sopenharmony_ci    value[DRV2605L_ADDR_INDEX] = (uint8_t)DRV2605_REG_MODE;
247094332d3Sopenharmony_ci    value[DRV2605L_VALUE_INDEX] = (uint8_t)DRV2605_MODE_STANDBY;
248094332d3Sopenharmony_ci    if (WriteDrv2605l(&drvData->drv2605lCfgData->vibratorBus.i2cCfg, value, sizeof(value)) != HDF_SUCCESS) {
249094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c addr [%0X] write failed", __func__, value[DRV2605L_ADDR_INDEX]);
250094332d3Sopenharmony_ci        return HDF_FAILURE;
251094332d3Sopenharmony_ci    }
252094332d3Sopenharmony_ci
253094332d3Sopenharmony_ci    value[DRV2605L_ADDR_INDEX] = (uint8_t)DRV2605_REG_RTPIN;
254094332d3Sopenharmony_ci    value[DRV2605L_VALUE_INDEX] = (uint8_t)&drvData->drv2605lCfgData->vibratorAttr.defaultIntensity;
255094332d3Sopenharmony_ci    if (WriteDrv2605l(&drvData->drv2605lCfgData->vibratorBus.i2cCfg, value, sizeof(value)) != HDF_SUCCESS) {
256094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c addr [%0X] write failed", __func__, value[DRV2605L_ADDR_INDEX]);
257094332d3Sopenharmony_ci        return HDF_FAILURE;
258094332d3Sopenharmony_ci    }
259094332d3Sopenharmony_ci
260094332d3Sopenharmony_ci    value[DRV2605L_ADDR_INDEX] = (uint8_t)DRV2605_REG_LRARESON;
261094332d3Sopenharmony_ci    value[DRV2605L_VALUE_INDEX] = (uint8_t)&drvData->drv2605lCfgData->vibratorAttr.defaultFrequency;
262094332d3Sopenharmony_ci    if (WriteDrv2605l(&drvData->drv2605lCfgData->vibratorBus.i2cCfg, value, sizeof(value)) != HDF_SUCCESS) {
263094332d3Sopenharmony_ci        HDF_LOGE("%s: i2c addr [%0X] write failed", __func__, value[DRV2605L_ADDR_INDEX]);
264094332d3Sopenharmony_ci        return HDF_FAILURE;
265094332d3Sopenharmony_ci    }
266094332d3Sopenharmony_ci
267094332d3Sopenharmony_ci    return HDF_SUCCESS;
268094332d3Sopenharmony_ci}
269094332d3Sopenharmony_ci
270094332d3Sopenharmony_cistatic int32_t DispatchDrv2605l(struct HdfDeviceIoClient *client,
271094332d3Sopenharmony_ci    int32_t cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
272094332d3Sopenharmony_ci{
273094332d3Sopenharmony_ci    (void)client;
274094332d3Sopenharmony_ci    (void)cmd;
275094332d3Sopenharmony_ci    (void)data;
276094332d3Sopenharmony_ci    (void)reply;
277094332d3Sopenharmony_ci    return HDF_SUCCESS;
278094332d3Sopenharmony_ci}
279094332d3Sopenharmony_ci
280094332d3Sopenharmony_cistatic int32_t BindDrv2605lDriver(struct HdfDeviceObject *device)
281094332d3Sopenharmony_ci{
282094332d3Sopenharmony_ci    struct Drv2605lDriverData *drvData = NULL;
283094332d3Sopenharmony_ci
284094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(device, HDF_FAILURE);
285094332d3Sopenharmony_ci
286094332d3Sopenharmony_ci    drvData = (struct Drv2605lDriverData *)OsalMemCalloc(sizeof(*drvData));
287094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_MALLOC_FAIL);
288094332d3Sopenharmony_ci
289094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchDrv2605l;
290094332d3Sopenharmony_ci    drvData->device = device;
291094332d3Sopenharmony_ci    device->service = &drvData->ioService;
292094332d3Sopenharmony_ci    g_drv2605lDrvData = drvData;
293094332d3Sopenharmony_ci    return HDF_SUCCESS;
294094332d3Sopenharmony_ci}
295094332d3Sopenharmony_ci
296094332d3Sopenharmony_cistatic int32_t InitDrv2605lDriver(struct HdfDeviceObject *device)
297094332d3Sopenharmony_ci{
298094332d3Sopenharmony_ci    static struct VibratorOps ops;
299094332d3Sopenharmony_ci    struct Drv2605lDriverData *drvData = NULL;
300094332d3Sopenharmony_ci
301094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(device, HDF_FAILURE);
302094332d3Sopenharmony_ci    drvData = (struct Drv2605lDriverData *)device->service;
303094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
304094332d3Sopenharmony_ci
305094332d3Sopenharmony_ci    ops.SetParameter = SetModulationParameter;
306094332d3Sopenharmony_ci    ops.Start = StartModulationParameter;
307094332d3Sopenharmony_ci    ops.Stop = StopModulationParameter;
308094332d3Sopenharmony_ci    ops.StartEffect = NULL;
309094332d3Sopenharmony_ci
310094332d3Sopenharmony_ci    drvData->drv2605lCfgData = (struct VibratorCfgData *)OsalMemCalloc(sizeof(*drvData->drv2605lCfgData));
311094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData->drv2605lCfgData, HDF_ERR_MALLOC_FAIL);
312094332d3Sopenharmony_ci
313094332d3Sopenharmony_ci    if (GetVibratorBaseConfigData(device->property, drvData->drv2605lCfgData) != HDF_SUCCESS) {
314094332d3Sopenharmony_ci        HDF_LOGE("%s: get vibrator base config fail", __func__);
315094332d3Sopenharmony_ci        return HDF_FAILURE;
316094332d3Sopenharmony_ci    }
317094332d3Sopenharmony_ci
318094332d3Sopenharmony_ci    if (DetectDrv2605lDevice(drvData) != HDF_SUCCESS) {
319094332d3Sopenharmony_ci        HDF_LOGE("%s: drv2605l detect chip fail", __func__);
320094332d3Sopenharmony_ci        return HDF_FAILURE;
321094332d3Sopenharmony_ci    }
322094332d3Sopenharmony_ci
323094332d3Sopenharmony_ci    if (InitDrv2605lChip(drvData->drv2605lCfgData) != HDF_SUCCESS) {
324094332d3Sopenharmony_ci        HDF_LOGE("%s: init 2605l chip fail", __func__);
325094332d3Sopenharmony_ci        return HDF_FAILURE;
326094332d3Sopenharmony_ci    }
327094332d3Sopenharmony_ci
328094332d3Sopenharmony_ci    if (RegisterVibratorInfo(&drvData->drv2605lCfgData->vibratorInfo) != HDF_SUCCESS) {
329094332d3Sopenharmony_ci        HDF_LOGE("%s: register vibrator info fail", __func__);
330094332d3Sopenharmony_ci        return HDF_FAILURE;
331094332d3Sopenharmony_ci    }
332094332d3Sopenharmony_ci
333094332d3Sopenharmony_ci    if (RegisterVibratorOps(&ops) != HDF_SUCCESS) {
334094332d3Sopenharmony_ci        HDF_LOGE("%s: register vibrator ops fail", __func__);
335094332d3Sopenharmony_ci        return HDF_FAILURE;
336094332d3Sopenharmony_ci    }
337094332d3Sopenharmony_ci
338094332d3Sopenharmony_ci    return HDF_SUCCESS;
339094332d3Sopenharmony_ci}
340094332d3Sopenharmony_ci
341094332d3Sopenharmony_cistatic void ReleaseDrv2605lDriver(struct HdfDeviceObject *device)
342094332d3Sopenharmony_ci{
343094332d3Sopenharmony_ci    struct Drv2605lDriverData *drvData = NULL;
344094332d3Sopenharmony_ci
345094332d3Sopenharmony_ci    if (device == NULL) {
346094332d3Sopenharmony_ci        HDF_LOGE("%s: device is null", __func__);
347094332d3Sopenharmony_ci        return;
348094332d3Sopenharmony_ci    }
349094332d3Sopenharmony_ci
350094332d3Sopenharmony_ci    drvData = (struct Drv2605lDriverData *)device->service;
351094332d3Sopenharmony_ci    if (drvData == NULL) {
352094332d3Sopenharmony_ci        HDF_LOGE("%s: drvData is null", __func__);
353094332d3Sopenharmony_ci        return;
354094332d3Sopenharmony_ci    }
355094332d3Sopenharmony_ci    ReleaseDrv2605lBusHandle(&drvData->drv2605lCfgData->vibratorBus.i2cCfg);
356094332d3Sopenharmony_ci    OsalMemFree(drvData->drv2605lCfgData);
357094332d3Sopenharmony_ci    OsalMemFree(drvData);
358094332d3Sopenharmony_ci    g_drv2605lDrvData = NULL;
359094332d3Sopenharmony_ci}
360094332d3Sopenharmony_ci
361094332d3Sopenharmony_cistruct HdfDriverEntry g_drv2605lDriverEntry = {
362094332d3Sopenharmony_ci    .moduleVersion = 1,
363094332d3Sopenharmony_ci    .moduleName = "HDF_DRV2605L_VIBRATOR",
364094332d3Sopenharmony_ci    .Bind = BindDrv2605lDriver,
365094332d3Sopenharmony_ci    .Init = InitDrv2605lDriver,
366094332d3Sopenharmony_ci    .Release = ReleaseDrv2605lDriver,
367094332d3Sopenharmony_ci};
368094332d3Sopenharmony_ci
369094332d3Sopenharmony_ciHDF_INIT(g_drv2605lDriverEntry);