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 "accel_bmi160.h"
10094332d3Sopenharmony_ci#include <securec.h>
11094332d3Sopenharmony_ci#include "osal_mem.h"
12094332d3Sopenharmony_ci#include "osal_time.h"
13094332d3Sopenharmony_ci#include "sensor_accel_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_accel_driver
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_cistatic struct Bmi160DrvData *g_bmi160DrvData = NULL;
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_ci/* IO config for int-pin and I2C-pin */
22094332d3Sopenharmony_ci#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
23094332d3Sopenharmony_ci#define SENSOR_I2C6_CLK_REG_ADDR  0x114f0048
24094332d3Sopenharmony_ci#define SENSOR_I2C_REG_CFG        0x403
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_cistatic int32_t ReadBmi160RawData(struct SensorCfgData *data, struct AccelData *rawData, uint64_t *timestamp)
27094332d3Sopenharmony_ci{
28094332d3Sopenharmony_ci    uint8_t status = 0;
29094332d3Sopenharmony_ci    uint8_t reg[ACCEL_AXIS_BUTT];
30094332d3Sopenharmony_ci    OsalTimespec time;
31094332d3Sopenharmony_ci
32094332d3Sopenharmony_ci    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
33094332d3Sopenharmony_ci    (void)memset_s(reg, sizeof(reg), 0, sizeof(reg));
34094332d3Sopenharmony_ci
35094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_ci    if (OsalGetTime(&time) != HDF_SUCCESS) {
38094332d3Sopenharmony_ci        HDF_LOGE("%s: Get time failed", __func__);
39094332d3Sopenharmony_ci        return HDF_FAILURE;
40094332d3Sopenharmony_ci    }
41094332d3Sopenharmony_ci    *timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
42094332d3Sopenharmony_ci
43094332d3Sopenharmony_ci    int32_t ret = ReadSensor(&data->busCfg, BMI160_STATUS_ADDR, &status, sizeof(uint8_t));
44094332d3Sopenharmony_ci    if (!(status & BMI160_ACCEL_DATA_READY_MASK) || (ret != HDF_SUCCESS)) {
45094332d3Sopenharmony_ci        HDF_LOGE("%s: data status [%hhu] ret [%d]", __func__, status, ret);
46094332d3Sopenharmony_ci        return HDF_FAILURE;
47094332d3Sopenharmony_ci    }
48094332d3Sopenharmony_ci
49094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMI160_ACCEL_X_LSB_ADDR, &reg[ACCEL_X_AXIS_LSB], sizeof(uint8_t));
50094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMI160_ACCEL_X_MSB_ADDR, &reg[ACCEL_X_AXIS_MSB], sizeof(uint8_t));
53094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMI160_ACCEL_Y_LSB_ADDR, &reg[ACCEL_Y_AXIS_LSB], sizeof(uint8_t));
56094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMI160_ACCEL_Y_MSB_ADDR, &reg[ACCEL_Y_AXIS_MSB], sizeof(uint8_t));
59094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMI160_ACCEL_Z_LSB_ADDR, &reg[ACCEL_Z_AXIS_LSB], sizeof(uint8_t));
62094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
63094332d3Sopenharmony_ci
64094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMI160_ACCEL_Z_MSB_ADDR, &reg[ACCEL_Z_AXIS_MSB], sizeof(uint8_t));
65094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
66094332d3Sopenharmony_ci
67094332d3Sopenharmony_ci    rawData->x = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ACCEL_X_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
68094332d3Sopenharmony_ci        reg[ACCEL_X_AXIS_LSB]);
69094332d3Sopenharmony_ci    rawData->y = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ACCEL_Y_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
70094332d3Sopenharmony_ci        reg[ACCEL_Y_AXIS_LSB]);
71094332d3Sopenharmony_ci    rawData->z = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[ACCEL_Z_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
72094332d3Sopenharmony_ci        reg[ACCEL_Z_AXIS_LSB]);
73094332d3Sopenharmony_ci
74094332d3Sopenharmony_ci    return HDF_SUCCESS;
75094332d3Sopenharmony_ci}
76094332d3Sopenharmony_ci
77094332d3Sopenharmony_cistatic int32_t ReadBmi160Data(struct SensorCfgData *cfg, struct SensorReportEvent *event)
78094332d3Sopenharmony_ci{
79094332d3Sopenharmony_ci    int32_t ret;
80094332d3Sopenharmony_ci    struct AccelData rawData = { 0, 0, 0 };
81094332d3Sopenharmony_ci    static int32_t tmp[ACCEL_AXIS_NUM];
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(cfg, HDF_ERR_INVALID_PARAM);
84094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(event, HDF_ERR_INVALID_PARAM);
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ci    ret = ReadBmi160RawData(cfg, &rawData, &event->timestamp);
87094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
88094332d3Sopenharmony_ci        HDF_LOGE("%s: BMI160 read raw data failed", __func__);
89094332d3Sopenharmony_ci        return HDF_FAILURE;
90094332d3Sopenharmony_ci    }
91094332d3Sopenharmony_ci
92094332d3Sopenharmony_ci    event->sensorId = SENSOR_TAG_ACCELEROMETER;
93094332d3Sopenharmony_ci    event->option = 0;
94094332d3Sopenharmony_ci    event->mode = SENSOR_WORK_MODE_REALTIME;
95094332d3Sopenharmony_ci
96094332d3Sopenharmony_ci    rawData.x = rawData.x * BMI160_ACC_SENSITIVITY_2G;
97094332d3Sopenharmony_ci    rawData.y = rawData.y * BMI160_ACC_SENSITIVITY_2G;
98094332d3Sopenharmony_ci    rawData.z = rawData.z * BMI160_ACC_SENSITIVITY_2G;
99094332d3Sopenharmony_ci
100094332d3Sopenharmony_ci    tmp[ACCEL_X_AXIS] = (rawData.x * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
101094332d3Sopenharmony_ci    tmp[ACCEL_Y_AXIS] = (rawData.y * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
102094332d3Sopenharmony_ci    tmp[ACCEL_Z_AXIS] = (rawData.z * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
103094332d3Sopenharmony_ci
104094332d3Sopenharmony_ci    ret = SensorRawDataToRemapData(cfg->direction, tmp, sizeof(tmp) / sizeof(tmp[0]));
105094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
106094332d3Sopenharmony_ci        HDF_LOGE("%s: BMI160 convert raw data failed", __func__);
107094332d3Sopenharmony_ci        return HDF_FAILURE;
108094332d3Sopenharmony_ci    }
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_ci    event->dataLen = sizeof(tmp);
111094332d3Sopenharmony_ci    event->data = (uint8_t *)&tmp;
112094332d3Sopenharmony_ci
113094332d3Sopenharmony_ci    return ret;
114094332d3Sopenharmony_ci}
115094332d3Sopenharmony_ci
116094332d3Sopenharmony_cistatic int32_t InitBmi160(struct SensorCfgData *data)
117094332d3Sopenharmony_ci{
118094332d3Sopenharmony_ci    int32_t ret;
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: BMI160 sensor init config failed", __func__);
124094332d3Sopenharmony_ci        return HDF_FAILURE;
125094332d3Sopenharmony_ci    }
126094332d3Sopenharmony_ci    return HDF_SUCCESS;
127094332d3Sopenharmony_ci}
128094332d3Sopenharmony_ci
129094332d3Sopenharmony_cistatic int32_t InitAccelPreConfig(void)
130094332d3Sopenharmony_ci{
131094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
132094332d3Sopenharmony_ci        HDF_LOGE("%s: Data write mux pin failed", __func__);
133094332d3Sopenharmony_ci        return HDF_FAILURE;
134094332d3Sopenharmony_ci    }
135094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
136094332d3Sopenharmony_ci        HDF_LOGE("%s: Clk write mux pin failed", __func__);
137094332d3Sopenharmony_ci        return HDF_FAILURE;
138094332d3Sopenharmony_ci    }
139094332d3Sopenharmony_ci
140094332d3Sopenharmony_ci    return HDF_SUCCESS;
141094332d3Sopenharmony_ci}
142094332d3Sopenharmony_ci
143094332d3Sopenharmony_cistatic int32_t DispatchBMI160(struct HdfDeviceIoClient *client,
144094332d3Sopenharmony_ci    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
145094332d3Sopenharmony_ci{
146094332d3Sopenharmony_ci    (void)client;
147094332d3Sopenharmony_ci    (void)cmd;
148094332d3Sopenharmony_ci    (void)data;
149094332d3Sopenharmony_ci    (void)reply;
150094332d3Sopenharmony_ci
151094332d3Sopenharmony_ci    return HDF_SUCCESS;
152094332d3Sopenharmony_ci}
153094332d3Sopenharmony_ci
154094332d3Sopenharmony_cistatic int32_t Bmi160BindDriver(struct HdfDeviceObject *device)
155094332d3Sopenharmony_ci{
156094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
157094332d3Sopenharmony_ci
158094332d3Sopenharmony_ci    struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)OsalMemCalloc(sizeof(*drvData));
159094332d3Sopenharmony_ci    if (drvData == NULL) {
160094332d3Sopenharmony_ci        HDF_LOGE("%s: Malloc Bmi160 drv data fail", __func__);
161094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
162094332d3Sopenharmony_ci    }
163094332d3Sopenharmony_ci
164094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchBMI160;
165094332d3Sopenharmony_ci    drvData->device = device;
166094332d3Sopenharmony_ci    device->service = &drvData->ioService;
167094332d3Sopenharmony_ci    g_bmi160DrvData = drvData;
168094332d3Sopenharmony_ci
169094332d3Sopenharmony_ci    return HDF_SUCCESS;
170094332d3Sopenharmony_ci}
171094332d3Sopenharmony_ci
172094332d3Sopenharmony_cistatic int32_t Bmi160InitDriver(struct HdfDeviceObject *device)
173094332d3Sopenharmony_ci{
174094332d3Sopenharmony_ci    int32_t ret;
175094332d3Sopenharmony_ci    struct AccelOpsCall ops;
176094332d3Sopenharmony_ci
177094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
178094332d3Sopenharmony_ci    struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
179094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
180094332d3Sopenharmony_ci
181094332d3Sopenharmony_ci    ret = InitAccelPreConfig();
182094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
183094332d3Sopenharmony_ci        HDF_LOGE("%s: Init BMI160 bus mux config", __func__);
184094332d3Sopenharmony_ci        return HDF_FAILURE;
185094332d3Sopenharmony_ci    }
186094332d3Sopenharmony_ci
187094332d3Sopenharmony_ci    drvData->sensorCfg = AccelCreateCfgData(device->property);
188094332d3Sopenharmony_ci    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
189094332d3Sopenharmony_ci        HDF_LOGD("%s: Creating accelcfg failed because detection failed", __func__);
190094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
191094332d3Sopenharmony_ci    }
192094332d3Sopenharmony_ci
193094332d3Sopenharmony_ci    ops.Init = NULL;
194094332d3Sopenharmony_ci    ops.ReadData = ReadBmi160Data;
195094332d3Sopenharmony_ci    ret = AccelRegisterChipOps(&ops);
196094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
197094332d3Sopenharmony_ci        HDF_LOGE("%s: Register BMI160 accel failed", __func__);
198094332d3Sopenharmony_ci        return HDF_FAILURE;
199094332d3Sopenharmony_ci    }
200094332d3Sopenharmony_ci
201094332d3Sopenharmony_ci    ret = InitBmi160(drvData->sensorCfg);
202094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
203094332d3Sopenharmony_ci        HDF_LOGE("%s: Init BMI160 accel failed", __func__);
204094332d3Sopenharmony_ci        return HDF_FAILURE;
205094332d3Sopenharmony_ci    }
206094332d3Sopenharmony_ci
207094332d3Sopenharmony_ci    return HDF_SUCCESS;
208094332d3Sopenharmony_ci}
209094332d3Sopenharmony_ci
210094332d3Sopenharmony_cistatic void Bmi160ReleaseDriver(struct HdfDeviceObject *device)
211094332d3Sopenharmony_ci{
212094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(device);
213094332d3Sopenharmony_ci
214094332d3Sopenharmony_ci    struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
215094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(drvData);
216094332d3Sopenharmony_ci
217094332d3Sopenharmony_ci    if (drvData->sensorCfg != NULL) {
218094332d3Sopenharmony_ci        AccelReleaseCfgData(drvData->sensorCfg);
219094332d3Sopenharmony_ci        drvData->sensorCfg = NULL;
220094332d3Sopenharmony_ci    }
221094332d3Sopenharmony_ci    OsalMemFree(drvData);
222094332d3Sopenharmony_ci}
223094332d3Sopenharmony_ci
224094332d3Sopenharmony_cistruct HdfDriverEntry g_accelBmi160DevEntry = {
225094332d3Sopenharmony_ci    .moduleVersion = 1,
226094332d3Sopenharmony_ci    .moduleName = "HDF_SENSOR_ACCEL_BMI160",
227094332d3Sopenharmony_ci    .Bind = Bmi160BindDriver,
228094332d3Sopenharmony_ci    .Init = Bmi160InitDriver,
229094332d3Sopenharmony_ci    .Release = Bmi160ReleaseDriver,
230094332d3Sopenharmony_ci};
231094332d3Sopenharmony_ci
232094332d3Sopenharmony_ciHDF_INIT(g_accelBmi160DevEntry);
233