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