1094332d3Sopenharmony_ci/**
2094332d3Sopenharmony_ci* Copyright (c) 2024 Bosch Sensortec GmbH. All rights reserved.
3094332d3Sopenharmony_ci*
4094332d3Sopenharmony_ci* accel_bmi270.c as part of the * /chipsets subdirectory
5094332d3Sopenharmony_ci* is dual licensed: you can use it either under the terms of
6094332d3Sopenharmony_ci* the GPL, or the BSD license, at your option.
7094332d3Sopenharmony_ci* See the LICENSE file in the root of this repository for complete details.
8094332d3Sopenharmony_ci*/
9094332d3Sopenharmony_ci
10094332d3Sopenharmony_ci#include "accel_bmi270.h"
11094332d3Sopenharmony_ci#include <securec.h>
12094332d3Sopenharmony_ci#include "osal_mem.h"
13094332d3Sopenharmony_ci#include "osal_time.h"
14094332d3Sopenharmony_ci#include "sensor_accel_driver.h"
15094332d3Sopenharmony_ci#include "sensor_config_controller.h"
16094332d3Sopenharmony_ci#include "sensor_device_manager.h"
17094332d3Sopenharmony_ci
18094332d3Sopenharmony_ci#define HDF_LOG_TAG    khdf_sensor_accel_driver
19094332d3Sopenharmony_cistatic struct Bmi270DrvData *g_bmi270DrvData = NULL;
20094332d3Sopenharmony_ci//328 bytes
21094332d3Sopenharmony_cistatic uint8_t g_regw_buffer[512];
22094332d3Sopenharmony_ci
23094332d3Sopenharmony_ci/* IO config for int-pin and I2C-pin */
24094332d3Sopenharmony_ci#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
25094332d3Sopenharmony_ci#define SENSOR_I2C6_CLK_REG_ADDR  0x114f0048
26094332d3Sopenharmony_ci#define SENSOR_I2C_REG_CFG        0x403
27094332d3Sopenharmony_ci
28094332d3Sopenharmony_cistatic uint8_t bmi270_cfg_buffer[] = {
29094332d3Sopenharmony_ci    0xc8, 0x2e, 0x00, 0x2e, 0x80, 0x2e, 0x1a, 0x00, 0xc8, 0x2e, 0x00, 0x2e,
30094332d3Sopenharmony_ci    0xc8, 0x2e, 0x00, 0x2e, 0xc8, 0x2e, 0x00, 0x2e, 0xc8, 0x2e, 0x00, 0x2e,
31094332d3Sopenharmony_ci    0xc8, 0x2e, 0x00, 0x2e, 0xc8, 0x2e, 0x00, 0x2e, 0x90, 0x32, 0x21, 0x2e,
32094332d3Sopenharmony_ci    0x59, 0xf5, 0x10, 0x30, 0x21, 0x2e, 0x6a, 0xf5, 0x1a, 0x24, 0x22, 0x00,
33094332d3Sopenharmony_ci    0x80, 0x2e, 0x3b, 0x00, 0xc8, 0x2e, 0x44, 0x47, 0x22, 0x00, 0x37, 0x00,
34094332d3Sopenharmony_ci    0xa4, 0x00, 0xff, 0x0f, 0xd1, 0x00, 0x07, 0xad, 0x80, 0x2e, 0x00, 0xc1,
35094332d3Sopenharmony_ci    0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1,
36094332d3Sopenharmony_ci    0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1,
37094332d3Sopenharmony_ci    0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1, 0x80, 0x2e, 0x00, 0xc1,
38094332d3Sopenharmony_ci    0x80, 0x2e, 0x00, 0xc1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x24,
39094332d3Sopenharmony_ci    0xfc, 0xf5, 0x80, 0x30, 0x40, 0x42, 0x50, 0x50, 0x00, 0x30, 0x12, 0x24,
40094332d3Sopenharmony_ci    0xeb, 0x00, 0x03, 0x30, 0x00, 0x2e, 0xc1, 0x86, 0x5a, 0x0e, 0xfb, 0x2f,
41094332d3Sopenharmony_ci    0x21, 0x2e, 0xfc, 0xf5, 0x13, 0x24, 0x63, 0xf5, 0xe0, 0x3c, 0x48, 0x00,
42094332d3Sopenharmony_ci    0x22, 0x30, 0xf7, 0x80, 0xc2, 0x42, 0xe1, 0x7f, 0x3a, 0x25, 0xfc, 0x86,
43094332d3Sopenharmony_ci    0xf0, 0x7f, 0x41, 0x33, 0x98, 0x2e, 0xc2, 0xc4, 0xd6, 0x6f, 0xf1, 0x30,
44094332d3Sopenharmony_ci    0xf1, 0x08, 0xc4, 0x6f, 0x11, 0x24, 0xff, 0x03, 0x12, 0x24, 0x00, 0xfc,
45094332d3Sopenharmony_ci    0x61, 0x09, 0xa2, 0x08, 0x36, 0xbe, 0x2a, 0xb9, 0x13, 0x24, 0x38, 0x00,
46094332d3Sopenharmony_ci    0x64, 0xbb, 0xd1, 0xbe, 0x94, 0x0a, 0x71, 0x08, 0xd5, 0x42, 0x21, 0xbd,
47094332d3Sopenharmony_ci    0x91, 0xbc, 0xd2, 0x42, 0xc1, 0x42, 0x00, 0xb2, 0xfe, 0x82, 0x05, 0x2f,
48094332d3Sopenharmony_ci    0x50, 0x30, 0x21, 0x2e, 0x21, 0xf2, 0x00, 0x2e, 0x00, 0x2e, 0xd0, 0x2e,
49094332d3Sopenharmony_ci    0xf0, 0x6f, 0x02, 0x30, 0x02, 0x42, 0x20, 0x26, 0xe0, 0x6f, 0x02, 0x31,
50094332d3Sopenharmony_ci    0x03, 0x40, 0x9a, 0x0a, 0x02, 0x42, 0xf0, 0x37, 0x05, 0x2e, 0x5e, 0xf7,
51094332d3Sopenharmony_ci    0x10, 0x08, 0x12, 0x24, 0x1e, 0xf2, 0x80, 0x42, 0x83, 0x84, 0xf1, 0x7f,
52094332d3Sopenharmony_ci    0x0a, 0x25, 0x13, 0x30, 0x83, 0x42, 0x3b, 0x82, 0xf0, 0x6f, 0x00, 0x2e,
53094332d3Sopenharmony_ci    0x00, 0x2e, 0xd0, 0x2e, 0x12, 0x40, 0x52, 0x42, 0x00, 0x2e, 0x12, 0x40,
54094332d3Sopenharmony_ci    0x52, 0x42, 0x3e, 0x84, 0x00, 0x40, 0x40, 0x42, 0x7e, 0x82, 0xe1, 0x7f,
55094332d3Sopenharmony_ci    0xf2, 0x7f, 0x98, 0x2e, 0x6a, 0xd6, 0x21, 0x30, 0x23, 0x2e, 0x61, 0xf5,
56094332d3Sopenharmony_ci    0xeb, 0x2c, 0xe1, 0x6f
57094332d3Sopenharmony_ci};
58094332d3Sopenharmony_ci//328 bytes
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_cistatic struct Bmi270DrvData *Bmi270GetDrvData(void)
61094332d3Sopenharmony_ci{
62094332d3Sopenharmony_ci    return g_bmi270DrvData;
63094332d3Sopenharmony_ci}
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ci/// @brief basic register write function
66094332d3Sopenharmony_ci/// @param data       Sensor configuration data structre pointer
67094332d3Sopenharmony_ci/// @param rega       register address
68094332d3Sopenharmony_ci/// @param buffer     value to write
69094332d3Sopenharmony_ci/// @param len        write len
70094332d3Sopenharmony_ci/// @return           HDF_SUCCESS if success, failed any error
71094332d3Sopenharmony_cistatic int32_t AccBmi270HalRegWrite(struct SensorCfgData *data, uint16_t rega, uint8_t* buffer, uint32_t len)
72094332d3Sopenharmony_ci{
73094332d3Sopenharmony_ci    int32_t rc = HDF_SUCCESS;
74094332d3Sopenharmony_ci    uint32_t idx = 0;
75094332d3Sopenharmony_ci    (void)memset_s(g_regw_buffer, sizeof(g_regw_buffer), 0, sizeof(g_regw_buffer));
76094332d3Sopenharmony_ci
77094332d3Sopenharmony_ci    g_regw_buffer[0] = (rega & 0xFF);
78094332d3Sopenharmony_ci    do {
79094332d3Sopenharmony_ci        g_regw_buffer[idx + 1] = buffer[idx];
80094332d3Sopenharmony_ci        idx ++;
81094332d3Sopenharmony_ci    } while (idx < len);
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_ci    rc = WriteSensor(&data->busCfg, g_regw_buffer, (len + 1));
84094332d3Sopenharmony_ci    OsalUDelay(BMI270_LP_MODE_WRITE_DELAY_IN_US);
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ci    if (rc != HDF_SUCCESS) {
87094332d3Sopenharmony_ci        HDF_LOGE("%s: [BMI270] w reg:%d err", __func__, rega);
88094332d3Sopenharmony_ci    }
89094332d3Sopenharmony_ci
90094332d3Sopenharmony_ci    return rc;
91094332d3Sopenharmony_ci}
92094332d3Sopenharmony_ci
93094332d3Sopenharmony_ci/// @brief basic register read function
94094332d3Sopenharmony_ci/// @param data      Sensor configuration data structre pointer
95094332d3Sopenharmony_ci/// @param rega      register address to read
96094332d3Sopenharmony_ci/// @param buffer    read data buffer
97094332d3Sopenharmony_ci/// @param len       read len
98094332d3Sopenharmony_ci/// @return          HDF_SUCCESS if success, failed any error
99094332d3Sopenharmony_cistatic int32_t AccBmi270HalRegRead(struct SensorCfgData *data, uint16_t rega, uint8_t* buffer, uint32_t len)
100094332d3Sopenharmony_ci{
101094332d3Sopenharmony_ci    int32_t rc = HDF_SUCCESS;
102094332d3Sopenharmony_ci    rc = ReadSensor(&data->busCfg, rega, buffer, len);
103094332d3Sopenharmony_ci
104094332d3Sopenharmony_ci    OsalUDelay(BMI270_NORMAL_MODE_WRITE_DELAY_IN_US);
105094332d3Sopenharmony_ci    if (rc != HDF_SUCCESS) {
106094332d3Sopenharmony_ci        HDF_LOGE("%s: [BMI270] r reg:%d err", __func__, rega);
107094332d3Sopenharmony_ci    }
108094332d3Sopenharmony_ci
109094332d3Sopenharmony_ci    return rc;
110094332d3Sopenharmony_ci}
111094332d3Sopenharmony_ci
112094332d3Sopenharmony_cistatic int32_t AccReadBmi270RawData(struct SensorCfgData *data, struct AccelData *rawData, int64_t *timestamp)
113094332d3Sopenharmony_ci{
114094332d3Sopenharmony_ci    OsalTimespec time;
115094332d3Sopenharmony_ci    uint8_t status = 0;
116094332d3Sopenharmony_ci    uint8_t regv[ACCEL_AXIS_BUTT] = {0};
117094332d3Sopenharmony_ci    int32_t ret = HDF_SUCCESS;
118094332d3Sopenharmony_ci
119094332d3Sopenharmony_ci    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
120094332d3Sopenharmony_ci    (void)memset_s(regv, sizeof(regv), 0, sizeof(regv));
121094332d3Sopenharmony_ci
122094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
123094332d3Sopenharmony_ci
124094332d3Sopenharmony_ci    if (OsalGetTime(&time) != HDF_SUCCESS) {
125094332d3Sopenharmony_ci        HDF_LOGE("%s: Get time failed", __func__);
126094332d3Sopenharmony_ci        return HDF_FAILURE;
127094332d3Sopenharmony_ci    }
128094332d3Sopenharmony_ci    *timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */
129094332d3Sopenharmony_ci
130094332d3Sopenharmony_ci    ret = AccBmi270HalRegRead(data, BMI270_ACCEL_REGA_STATUS, &status, sizeof(uint8_t));
131094332d3Sopenharmony_ci    /* any new data? */
132094332d3Sopenharmony_ci    if (!(status & BMI270_ACCEL_DATA_READY_MASK) || (ret != HDF_SUCCESS)) {
133094332d3Sopenharmony_ci        HDF_LOGI("%s: data status [%hhu] ret [%d]", __func__, status, ret);
134094332d3Sopenharmony_ci        return HDF_FAILURE;
135094332d3Sopenharmony_ci    }
136094332d3Sopenharmony_ci
137094332d3Sopenharmony_ci    ret = AccBmi270HalRegRead(data, BMI270_ACCEL_REGA_X_LSB_ADDR, regv, BMI270_ACC_DATA_FRAME_SIZE);
138094332d3Sopenharmony_ci
139094332d3Sopenharmony_ci    rawData->x = (int16_t)(SENSOR_DATA_SHIFT_LEFT(regv[ACCEL_X_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
140094332d3Sopenharmony_ci        regv[ACCEL_X_AXIS_LSB]);
141094332d3Sopenharmony_ci    rawData->y = (int16_t)(SENSOR_DATA_SHIFT_LEFT(regv[ACCEL_Y_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
142094332d3Sopenharmony_ci        regv[ACCEL_Y_AXIS_LSB]);
143094332d3Sopenharmony_ci    rawData->z = (int16_t)(SENSOR_DATA_SHIFT_LEFT(regv[ACCEL_Z_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) |
144094332d3Sopenharmony_ci        regv[ACCEL_Z_AXIS_LSB]);
145094332d3Sopenharmony_ci
146094332d3Sopenharmony_ci    return ret;
147094332d3Sopenharmony_ci}
148094332d3Sopenharmony_ci
149094332d3Sopenharmony_cistatic int32_t AccReadBmi270Data(struct SensorCfgData *cfg, struct SensorReportEvent *event)
150094332d3Sopenharmony_ci{
151094332d3Sopenharmony_ci    int32_t ret;
152094332d3Sopenharmony_ci    struct AccelData rawData = { 0, 0, 0 };
153094332d3Sopenharmony_ci    static int32_t tmp[ACCEL_AXIS_NUM];
154094332d3Sopenharmony_ci
155094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(cfg, HDF_ERR_INVALID_PARAM);
156094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(event, HDF_ERR_INVALID_PARAM);
157094332d3Sopenharmony_ci
158094332d3Sopenharmony_ci    ret = AccReadBmi270RawData(cfg, &rawData, (int64_t *)&event->timestamp);
159094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
160094332d3Sopenharmony_ci        HDF_LOGE("%s: BMI270 read raw data failed", __func__);
161094332d3Sopenharmony_ci        return HDF_FAILURE;
162094332d3Sopenharmony_ci    }
163094332d3Sopenharmony_ci    event->sensorId = SENSOR_TAG_ACCELEROMETER;
164094332d3Sopenharmony_ci    event->option = 0;
165094332d3Sopenharmony_ci    event->mode = SENSOR_WORK_MODE_REALTIME;
166094332d3Sopenharmony_ci
167094332d3Sopenharmony_ci    rawData.x = rawData.x * BMI270_ACC_SENSITIVITY_2G;
168094332d3Sopenharmony_ci    rawData.y = rawData.y * BMI270_ACC_SENSITIVITY_2G;
169094332d3Sopenharmony_ci    rawData.z = rawData.z * BMI270_ACC_SENSITIVITY_2G;
170094332d3Sopenharmony_ci
171094332d3Sopenharmony_ci    tmp[ACCEL_X_AXIS] = (rawData.x * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
172094332d3Sopenharmony_ci    tmp[ACCEL_Y_AXIS] = (rawData.y * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
173094332d3Sopenharmony_ci    tmp[ACCEL_Z_AXIS] = (rawData.z * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT;
174094332d3Sopenharmony_ci
175094332d3Sopenharmony_ci
176094332d3Sopenharmony_ci    ret = SensorRawDataToRemapData(cfg->direction, tmp, sizeof(tmp) / sizeof(tmp[0]));
177094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
178094332d3Sopenharmony_ci        HDF_LOGE("%s: BMI270 convert raw data failed", __func__);
179094332d3Sopenharmony_ci        return HDF_FAILURE;
180094332d3Sopenharmony_ci    }
181094332d3Sopenharmony_ci
182094332d3Sopenharmony_ci    event->dataLen = sizeof(tmp);
183094332d3Sopenharmony_ci    event->data = (uint8_t *)&tmp;
184094332d3Sopenharmony_ci
185094332d3Sopenharmony_ci    return ret;
186094332d3Sopenharmony_ci}
187094332d3Sopenharmony_ci
188094332d3Sopenharmony_ci// success
189094332d3Sopenharmony_cistatic int32_t Bmi270IMUSwReset(struct SensorCfgData* data)
190094332d3Sopenharmony_ci{
191094332d3Sopenharmony_ci    uint8_t regv = BMI26X_REGV_CMD_SOFT_RESET;
192094332d3Sopenharmony_ci    int32_t rc = AccBmi270HalRegWrite(data, BMI26X_REGA_USR_CMD, &regv, BMI270_ONE_BYTE);
193094332d3Sopenharmony_ci    OsalMDelay(BMI270_RESET_DELAY_IN_MS);
194094332d3Sopenharmony_ci
195094332d3Sopenharmony_ci    return rc;
196094332d3Sopenharmony_ci}
197094332d3Sopenharmony_ci
198094332d3Sopenharmony_cistatic bool Bmi270HalIMUInited(struct SensorCfgData *data)
199094332d3Sopenharmony_ci{
200094332d3Sopenharmony_ci    bool rc = false;
201094332d3Sopenharmony_ci    uint8_t regv = 0;
202094332d3Sopenharmony_ci
203094332d3Sopenharmony_ci    rc = AccBmi270HalRegRead(data, BMI270_REGA_INTERNAL_STATUS, &regv, BMI270_ONE_BYTE);
204094332d3Sopenharmony_ci    if (BST_GET_VAL_BIT(regv, 0)) {
205094332d3Sopenharmony_ci        HDF_LOGI("[BMI270] %s, IMU inited", __LINE__);
206094332d3Sopenharmony_ci        rc = true;
207094332d3Sopenharmony_ci    }
208094332d3Sopenharmony_ci
209094332d3Sopenharmony_ci    return rc;
210094332d3Sopenharmony_ci}
211094332d3Sopenharmony_ci
212094332d3Sopenharmony_cistatic int32_t Bmi270CfgAccPwr(struct SensorCfgData *data, bool en)
213094332d3Sopenharmony_ci{
214094332d3Sopenharmony_ci    uint8_t regv = 0;
215094332d3Sopenharmony_ci    int32_t rc = HDF_SUCCESS;
216094332d3Sopenharmony_ci    rc = AccBmi270HalRegRead(data, BMI270_REGA_PWR_CTRL, &regv, BMI270_ONE_BYTE);
217094332d3Sopenharmony_ci
218094332d3Sopenharmony_ci    if (en) {
219094332d3Sopenharmony_ci        regv = BST_SET_VAL_BIT(regv, BMI270_ACC_POWER_BIT_POS_IN_PWR_CTRL_REG);
220094332d3Sopenharmony_ci    } else {
221094332d3Sopenharmony_ci        regv = BST_CLR_VAL_BIT(regv, BMI270_ACC_POWER_BIT_POS_IN_PWR_CTRL_REG);
222094332d3Sopenharmony_ci    }
223094332d3Sopenharmony_ci
224094332d3Sopenharmony_ci    rc = AccBmi270HalRegWrite(data, BMI270_REGA_PWR_CTRL, &regv, BMI270_ONE_BYTE);
225094332d3Sopenharmony_ci
226094332d3Sopenharmony_ci    return rc;
227094332d3Sopenharmony_ci}
228094332d3Sopenharmony_ci
229094332d3Sopenharmony_ci// @load configuration
230094332d3Sopenharmony_cistatic int32_t Bmi270LoadCfg(struct SensorCfgData *data, uint8_t* cfg_data_buffer,
231094332d3Sopenharmony_ci    uint32_t cfg_size, uint32_t in_burst_write_size)
232094332d3Sopenharmony_ci{
233094332d3Sopenharmony_ci    int32_t rc = HDF_SUCCESS;
234094332d3Sopenharmony_ci    uint8_t regv = 0;
235094332d3Sopenharmony_ci    /* Variable to update the configuration file index */
236094332d3Sopenharmony_ci    uint16_t index = 0;
237094332d3Sopenharmony_ci    /* Array to store address */
238094332d3Sopenharmony_ci    uint8_t addr_array[2] = {0};
239094332d3Sopenharmony_ci    uint32_t size_bw = in_burst_write_size; //burst write size in byte
240094332d3Sopenharmony_ci
241094332d3Sopenharmony_ci    /* Disable loading of the configuration */
242094332d3Sopenharmony_ci    rc = AccBmi270HalRegWrite(data, BMI26X_REGA_USR_TITAN_CTRL, &regv, BMI270_ONE_BYTE);
243094332d3Sopenharmony_ci
244094332d3Sopenharmony_ci    for (index = 0; index < cfg_size; index += size_bw) {
245094332d3Sopenharmony_ci        //1. len
246094332d3Sopenharmony_ci        if (index + in_burst_write_size > cfg_size) {
247094332d3Sopenharmony_ci            size_bw = cfg_size - index;
248094332d3Sopenharmony_ci        }
249094332d3Sopenharmony_ci
250094332d3Sopenharmony_ci        // index low in word
251094332d3Sopenharmony_ci        addr_array[0] = (uint8_t)((index / 2) & 0x0F);
252094332d3Sopenharmony_ci        // ndex high in word
253094332d3Sopenharmony_ci        addr_array[1] = (uint8_t)((index / 2) >> 4);
254094332d3Sopenharmony_ci        rc = AccBmi270HalRegWrite(data, BMI26X_REGA_USR_CONF_STREAM_IDX_LSB, addr_array, BMI270_TWO_BYTE);
255094332d3Sopenharmony_ci
256094332d3Sopenharmony_ci        //2. data
257094332d3Sopenharmony_ci        rc = AccBmi270HalRegWrite(data, BMI26X_REGA_USR_CONF_STREAM_IN,
258094332d3Sopenharmony_ci                                  cfg_data_buffer + index, size_bw);
259094332d3Sopenharmony_ci    }
260094332d3Sopenharmony_ci
261094332d3Sopenharmony_ci    return rc;
262094332d3Sopenharmony_ci}
263094332d3Sopenharmony_ci
264094332d3Sopenharmony_cistatic int32_t Bmi270HalLoadCfg(struct SensorCfgData *data)
265094332d3Sopenharmony_ci{
266094332d3Sopenharmony_ci    int32_t rc = HDF_SUCCESS;
267094332d3Sopenharmony_ci    uint8_t regv = 0;
268094332d3Sopenharmony_ci    uint8_t try_num_to_check_cfg = 0;
269094332d3Sopenharmony_ci    bool cfg_is_avaiblabe = Bmi270HalIMUInited(data);
270094332d3Sopenharmony_ci    if (cfg_is_avaiblabe) {
271094332d3Sopenharmony_ci        return HDF_SUCCESS;
272094332d3Sopenharmony_ci    }
273094332d3Sopenharmony_ci
274094332d3Sopenharmony_ci    rc = Bmi270IMUSwReset(data);
275094332d3Sopenharmony_ci    if (rc != HDF_SUCCESS) {
276094332d3Sopenharmony_ci        return rc;
277094332d3Sopenharmony_ci    }
278094332d3Sopenharmony_ci
279094332d3Sopenharmony_ci    //disable advance power saving
280094332d3Sopenharmony_ci    rc = AccBmi270HalRegWrite(data, BMI270_REGA_PWR_CFG, &regv, BMI270_ONE_BYTE);
281094332d3Sopenharmony_ci
282094332d3Sopenharmony_ci    // load configuration now
283094332d3Sopenharmony_ci    rc = Bmi270LoadCfg(data, bmi270_cfg_buffer,
284094332d3Sopenharmony_ci                       HDF_ARRAY_SIZE(bmi270_cfg_buffer),
285094332d3Sopenharmony_ci                       HDF_ARRAY_SIZE(bmi270_cfg_buffer));
286094332d3Sopenharmony_ci
287094332d3Sopenharmony_ci    regv = 0x01;
288094332d3Sopenharmony_ci    rc = AccBmi270HalRegWrite(data, BMI26X_REGA_USR_TITAN_CTRL, &regv, BMI270_ONE_BYTE);
289094332d3Sopenharmony_ci
290094332d3Sopenharmony_ci    cfg_is_avaiblabe = false;
291094332d3Sopenharmony_ci    rc = HDF_DEV_ERR_DEV_INIT_FAIL;
292094332d3Sopenharmony_ci    do {
293094332d3Sopenharmony_ci        OsalMDelay(BMI270_LOAD_RAM_PATCH_DELAY_IN_MS);
294094332d3Sopenharmony_ci        cfg_is_avaiblabe = Bmi270HalIMUInited(data);
295094332d3Sopenharmony_ci        try_num_to_check_cfg ++;
296094332d3Sopenharmony_ci        if (cfg_is_avaiblabe) {
297094332d3Sopenharmony_ci            rc = HDF_SUCCESS;
298094332d3Sopenharmony_ci            break;
299094332d3Sopenharmony_ci        }
300094332d3Sopenharmony_ci    } while ((try_num_to_check_cfg < BMI26X_CHECK_CONFIGURE_STATUS_TIMES) &&
301094332d3Sopenharmony_ci            (!cfg_is_avaiblabe));
302094332d3Sopenharmony_ci
303094332d3Sopenharmony_ci    return rc;
304094332d3Sopenharmony_ci}
305094332d3Sopenharmony_ci
306094332d3Sopenharmony_cistatic int32_t Bmi270GetWhoAmI(struct SensorCfgData *data)
307094332d3Sopenharmony_ci{
308094332d3Sopenharmony_ci    uint8_t regv = 0;
309094332d3Sopenharmony_ci    int32_t rc = HDF_SUCCESS;
310094332d3Sopenharmony_ci
311094332d3Sopenharmony_ci    rc = AccBmi270HalRegRead(data, 0x00, &regv, BMI270_ONE_BYTE);
312094332d3Sopenharmony_ci    if (rc != HDF_SUCCESS) {
313094332d3Sopenharmony_ci        HDF_LOGE("%s: [BMI270] WARN!!, NO Sensor", __func__);
314094332d3Sopenharmony_ci        return HDF_FAILURE;
315094332d3Sopenharmony_ci    }
316094332d3Sopenharmony_ci
317094332d3Sopenharmony_ci    if (regv != BMI26X_REGV_WHOAMI) {
318094332d3Sopenharmony_ci        rc = HDF_DEV_ERR_NO_DEVICE;
319094332d3Sopenharmony_ci    }
320094332d3Sopenharmony_ci
321094332d3Sopenharmony_ci    return rc;
322094332d3Sopenharmony_ci}
323094332d3Sopenharmony_ci
324094332d3Sopenharmony_cistatic int32_t InitBmi270(struct SensorCfgData *data)
325094332d3Sopenharmony_ci{
326094332d3Sopenharmony_ci    int32_t ret = HDF_SUCCESS;
327094332d3Sopenharmony_ci    uint8_t regv = 0x00;
328094332d3Sopenharmony_ci
329094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
330094332d3Sopenharmony_ci    ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
331094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
332094332d3Sopenharmony_ci        HDF_LOGE("%s: BMI270 sensor init config failed", __func__);
333094332d3Sopenharmony_ci        return HDF_FAILURE;
334094332d3Sopenharmony_ci    }
335094332d3Sopenharmony_ci
336094332d3Sopenharmony_ci    ret = Bmi270CfgAccPwr(data, true);
337094332d3Sopenharmony_ci    //defualt odr:100hz
338094332d3Sopenharmony_ci    // range:+/- 2G
339094332d3Sopenharmony_ci    regv = 0;
340094332d3Sopenharmony_ci    ret = AccBmi270HalRegWrite(data, BMI26X_REGA_ACC_RANGE, &regv, BMI270_ONE_BYTE);
341094332d3Sopenharmony_ci
342094332d3Sopenharmony_ci    return ret;
343094332d3Sopenharmony_ci}
344094332d3Sopenharmony_ci
345094332d3Sopenharmony_cistatic int32_t DispatchBmi270(struct HdfDeviceIoClient *client,
346094332d3Sopenharmony_ci    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
347094332d3Sopenharmony_ci{
348094332d3Sopenharmony_ci    (void)client;
349094332d3Sopenharmony_ci    (void)cmd;
350094332d3Sopenharmony_ci    (void)data;
351094332d3Sopenharmony_ci    (void)reply;
352094332d3Sopenharmony_ci
353094332d3Sopenharmony_ci    return HDF_SUCCESS;
354094332d3Sopenharmony_ci}
355094332d3Sopenharmony_ci
356094332d3Sopenharmony_cistatic int32_t Bmi270BindDriver(struct HdfDeviceObject *device)
357094332d3Sopenharmony_ci{
358094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
359094332d3Sopenharmony_ci
360094332d3Sopenharmony_ci    struct Bmi270DrvData *drvData = (struct Bmi270DrvData *)OsalMemCalloc(sizeof(*drvData));
361094332d3Sopenharmony_ci    if (drvData == NULL) {
362094332d3Sopenharmony_ci        HDF_LOGE("%s: Malloc Bmi270 drv data fail", __func__);
363094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
364094332d3Sopenharmony_ci    }
365094332d3Sopenharmony_ci
366094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchBmi270;
367094332d3Sopenharmony_ci    drvData->device = device;
368094332d3Sopenharmony_ci    device->service = &drvData->ioService;
369094332d3Sopenharmony_ci    g_bmi270DrvData = drvData;
370094332d3Sopenharmony_ci
371094332d3Sopenharmony_ci    return HDF_SUCCESS;
372094332d3Sopenharmony_ci}
373094332d3Sopenharmony_ci
374094332d3Sopenharmony_cistatic int32_t Bmi270InitDriver(struct HdfDeviceObject *device)
375094332d3Sopenharmony_ci{
376094332d3Sopenharmony_ci    int32_t ret;
377094332d3Sopenharmony_ci    struct AccelOpsCall ops;
378094332d3Sopenharmony_ci
379094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
380094332d3Sopenharmony_ci    struct Bmi270DrvData *drvData = (struct Bmi270DrvData *)device->service;
381094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
382094332d3Sopenharmony_ci
383094332d3Sopenharmony_ci    drvData->sensorCfg = AccelCreateCfgData(device->property);
384094332d3Sopenharmony_ci    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
385094332d3Sopenharmony_ci        HDF_LOGD("%s: Creating accelcfg failed because detection failed", __func__);
386094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
387094332d3Sopenharmony_ci    }
388094332d3Sopenharmony_ci
389094332d3Sopenharmony_ci    ops.Init = NULL;
390094332d3Sopenharmony_ci    ops.ReadData = AccReadBmi270Data;
391094332d3Sopenharmony_ci    ret = AccelRegisterChipOps(&ops);
392094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
393094332d3Sopenharmony_ci        HDF_LOGE("%s: Register BMI270 accel failed", __func__);
394094332d3Sopenharmony_ci        return HDF_FAILURE;
395094332d3Sopenharmony_ci    }
396094332d3Sopenharmony_ci
397094332d3Sopenharmony_ci    // check whoami
398094332d3Sopenharmony_ci    ret = Bmi270GetWhoAmI(drvData->sensorCfg);
399094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
400094332d3Sopenharmony_ci        return ret;
401094332d3Sopenharmony_ci    }
402094332d3Sopenharmony_ci
403094332d3Sopenharmony_ci    do {
404094332d3Sopenharmony_ci        ret = Bmi270HalLoadCfg(drvData->sensorCfg);
405094332d3Sopenharmony_ci    } while (0);
406094332d3Sopenharmony_ci
407094332d3Sopenharmony_ci    // init hw
408094332d3Sopenharmony_ci    ret = InitBmi270(drvData->sensorCfg);
409094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
410094332d3Sopenharmony_ci        HDF_LOGE("%s: Init BMI270 accel failed", __func__);
411094332d3Sopenharmony_ci        return HDF_FAILURE;
412094332d3Sopenharmony_ci    }
413094332d3Sopenharmony_ci
414094332d3Sopenharmony_ci    return HDF_SUCCESS;
415094332d3Sopenharmony_ci}
416094332d3Sopenharmony_ci
417094332d3Sopenharmony_cistatic void Bmi270ReleaseDriver(struct HdfDeviceObject *device)
418094332d3Sopenharmony_ci{
419094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(device);
420094332d3Sopenharmony_ci
421094332d3Sopenharmony_ci    struct Bmi270DrvData *drvData = (struct Bmi270DrvData *)device->service;
422094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(drvData);
423094332d3Sopenharmony_ci
424094332d3Sopenharmony_ci    if (drvData->sensorCfg != NULL) {
425094332d3Sopenharmony_ci        AccelReleaseCfgData(drvData->sensorCfg);
426094332d3Sopenharmony_ci        drvData->sensorCfg = NULL;
427094332d3Sopenharmony_ci    }
428094332d3Sopenharmony_ci    OsalMemFree(drvData);
429094332d3Sopenharmony_ci}
430094332d3Sopenharmony_ci
431094332d3Sopenharmony_cistruct HdfDriverEntry g_accelBmi270DevEntry = {
432094332d3Sopenharmony_ci    .moduleVersion = 1,
433094332d3Sopenharmony_ci    .moduleName = "HDF_SENSOR_ACCEL_BMI270",
434094332d3Sopenharmony_ci    .Bind = Bmi270BindDriver,
435094332d3Sopenharmony_ci    .Init = Bmi270InitDriver,
436094332d3Sopenharmony_ci    .Release = Bmi270ReleaseDriver,
437094332d3Sopenharmony_ci};
438094332d3Sopenharmony_ci
439094332d3Sopenharmony_ciHDF_INIT(g_accelBmi270DevEntry);
440