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 "gyro_bmi160.h" 10094332d3Sopenharmony_ci#include <securec.h> 11094332d3Sopenharmony_ci#include "osal_mem.h" 12094332d3Sopenharmony_ci#include "osal_time.h" 13094332d3Sopenharmony_ci#include "sensor_gyro_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_gyro_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 ReadBmi160GyroRawData(struct SensorCfgData *data, struct GyroData *rawData, uint64_t *timestamp) 27094332d3Sopenharmony_ci{ 28094332d3Sopenharmony_ci uint8_t status = 0; 29094332d3Sopenharmony_ci uint8_t reg[GYRO_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_GYRO_DATA_READY_MASK) || (ret != HDF_SUCCESS)) { 45094332d3Sopenharmony_ci return HDF_FAILURE; 46094332d3Sopenharmony_ci } 47094332d3Sopenharmony_ci 48094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, BMI160_GYRO_X_LSB_ADDR, ®[GYRO_X_AXIS_LSB], sizeof(uint8_t)); 49094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 50094332d3Sopenharmony_ci 51094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, BMI160_GYRO_X_MSB_ADDR, ®[GYRO_X_AXIS_MSB], sizeof(uint8_t)); 52094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 53094332d3Sopenharmony_ci 54094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, BMI160_GYRO_Y_LSB_ADDR, ®[GYRO_Y_AXIS_LSB], sizeof(uint8_t)); 55094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 56094332d3Sopenharmony_ci 57094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, BMI160_GYRO_Y_MSB_ADDR, ®[GYRO_Y_AXIS_MSB], sizeof(uint8_t)); 58094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 59094332d3Sopenharmony_ci 60094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, BMI160_GYRO_Z_LSB_ADDR, ®[GYRO_Z_AXIS_LSB], sizeof(uint8_t)); 61094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 62094332d3Sopenharmony_ci 63094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, BMI160_GYRO_Z_MSB_ADDR, ®[GYRO_Z_AXIS_MSB], sizeof(uint8_t)); 64094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 65094332d3Sopenharmony_ci 66094332d3Sopenharmony_ci rawData->x = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[GYRO_X_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) | 67094332d3Sopenharmony_ci reg[GYRO_X_AXIS_LSB]); 68094332d3Sopenharmony_ci rawData->y = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[GYRO_Y_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) | 69094332d3Sopenharmony_ci reg[GYRO_Y_AXIS_LSB]); 70094332d3Sopenharmony_ci rawData->z = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[GYRO_Z_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) | 71094332d3Sopenharmony_ci reg[GYRO_Z_AXIS_LSB]); 72094332d3Sopenharmony_ci 73094332d3Sopenharmony_ci return ret; 74094332d3Sopenharmony_ci} 75094332d3Sopenharmony_ci 76094332d3Sopenharmony_cistatic int32_t ReadBmi160GyroData(struct SensorCfgData *data) 77094332d3Sopenharmony_ci{ 78094332d3Sopenharmony_ci int32_t ret; 79094332d3Sopenharmony_ci struct GyroData rawData = { 0, 0, 0 }; 80094332d3Sopenharmony_ci int32_t tmp[GYRO_AXIS_NUM]; 81094332d3Sopenharmony_ci struct SensorReportEvent event; 82094332d3Sopenharmony_ci 83094332d3Sopenharmony_ci (void)memset_s(&event, sizeof(event), 0, sizeof(event)); 84094332d3Sopenharmony_ci 85094332d3Sopenharmony_ci ret = ReadBmi160GyroRawData(data, &rawData, &event.timestamp); 86094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 87094332d3Sopenharmony_ci return HDF_FAILURE; 88094332d3Sopenharmony_ci } 89094332d3Sopenharmony_ci 90094332d3Sopenharmony_ci event.sensorId = SENSOR_TAG_GYROSCOPE; 91094332d3Sopenharmony_ci event.option = 0; 92094332d3Sopenharmony_ci event.mode = SENSOR_WORK_MODE_REALTIME; 93094332d3Sopenharmony_ci 94094332d3Sopenharmony_ci tmp[GYRO_X_AXIS] = rawData.x * BMI160_GYRO_SENSITIVITY_2000DPS; 95094332d3Sopenharmony_ci tmp[GYRO_Y_AXIS] = rawData.y * BMI160_GYRO_SENSITIVITY_2000DPS; 96094332d3Sopenharmony_ci tmp[GYRO_Z_AXIS] = rawData.z * BMI160_GYRO_SENSITIVITY_2000DPS; 97094332d3Sopenharmony_ci 98094332d3Sopenharmony_ci ret = SensorRawDataToRemapData(data->direction, tmp, sizeof(tmp) / sizeof(tmp[0])); 99094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 100094332d3Sopenharmony_ci HDF_LOGE("%s: BMI160 convert raw data failed", __func__); 101094332d3Sopenharmony_ci return HDF_FAILURE; 102094332d3Sopenharmony_ci } 103094332d3Sopenharmony_ci 104094332d3Sopenharmony_ci event.dataLen = sizeof(tmp); 105094332d3Sopenharmony_ci event.data = (uint8_t *)&tmp; 106094332d3Sopenharmony_ci ret = ReportSensorEvent(&event); 107094332d3Sopenharmony_ci return ret; 108094332d3Sopenharmony_ci} 109094332d3Sopenharmony_cistatic int32_t InitBmi160(struct SensorCfgData *data) 110094332d3Sopenharmony_ci{ 111094332d3Sopenharmony_ci int32_t ret; 112094332d3Sopenharmony_ci 113094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM); 114094332d3Sopenharmony_ci ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]); 115094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 116094332d3Sopenharmony_ci HDF_LOGE("%s: BMI160 sensor init config failed", __func__); 117094332d3Sopenharmony_ci return HDF_FAILURE; 118094332d3Sopenharmony_ci } 119094332d3Sopenharmony_ci return HDF_SUCCESS; 120094332d3Sopenharmony_ci} 121094332d3Sopenharmony_ci 122094332d3Sopenharmony_cistatic int32_t InitGyroPreConfig(void) 123094332d3Sopenharmony_ci{ 124094332d3Sopenharmony_ci if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) { 125094332d3Sopenharmony_ci HDF_LOGE("%s: Data write mux pin failed", __func__); 126094332d3Sopenharmony_ci return HDF_FAILURE; 127094332d3Sopenharmony_ci } 128094332d3Sopenharmony_ci if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) { 129094332d3Sopenharmony_ci HDF_LOGE("%s: Clk write mux pin failed", __func__); 130094332d3Sopenharmony_ci return HDF_FAILURE; 131094332d3Sopenharmony_ci } 132094332d3Sopenharmony_ci 133094332d3Sopenharmony_ci return HDF_SUCCESS; 134094332d3Sopenharmony_ci} 135094332d3Sopenharmony_ci 136094332d3Sopenharmony_cistatic int32_t DispatchBMI160(struct HdfDeviceIoClient *client, 137094332d3Sopenharmony_ci int cmd, struct HdfSBuf *data, struct HdfSBuf *reply) 138094332d3Sopenharmony_ci{ 139094332d3Sopenharmony_ci (void)client; 140094332d3Sopenharmony_ci (void)cmd; 141094332d3Sopenharmony_ci (void)data; 142094332d3Sopenharmony_ci (void)reply; 143094332d3Sopenharmony_ci 144094332d3Sopenharmony_ci return HDF_SUCCESS; 145094332d3Sopenharmony_ci} 146094332d3Sopenharmony_ci 147094332d3Sopenharmony_cistatic int32_t GyroBmi160BindDriver(struct HdfDeviceObject *device) 148094332d3Sopenharmony_ci{ 149094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM); 150094332d3Sopenharmony_ci 151094332d3Sopenharmony_ci struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)OsalMemCalloc(sizeof(*drvData)); 152094332d3Sopenharmony_ci if (drvData == NULL) { 153094332d3Sopenharmony_ci HDF_LOGE("%s: Malloc Bmi160 drv data fail", __func__); 154094332d3Sopenharmony_ci return HDF_ERR_MALLOC_FAIL; 155094332d3Sopenharmony_ci } 156094332d3Sopenharmony_ci 157094332d3Sopenharmony_ci drvData->ioService.Dispatch = DispatchBMI160; 158094332d3Sopenharmony_ci drvData->device = device; 159094332d3Sopenharmony_ci device->service = &drvData->ioService; 160094332d3Sopenharmony_ci g_bmi160DrvData = drvData; 161094332d3Sopenharmony_ci 162094332d3Sopenharmony_ci return HDF_SUCCESS; 163094332d3Sopenharmony_ci} 164094332d3Sopenharmony_ci 165094332d3Sopenharmony_cistatic int32_t GyroBmi160InitDriver(struct HdfDeviceObject *device) 166094332d3Sopenharmony_ci{ 167094332d3Sopenharmony_ci int32_t ret; 168094332d3Sopenharmony_ci struct GyroOpsCall ops; 169094332d3Sopenharmony_ci 170094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM); 171094332d3Sopenharmony_ci struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service; 172094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM); 173094332d3Sopenharmony_ci 174094332d3Sopenharmony_ci ret = InitGyroPreConfig(); 175094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 176094332d3Sopenharmony_ci HDF_LOGE("%s: Init BMI160 bus mux config", __func__); 177094332d3Sopenharmony_ci return HDF_FAILURE; 178094332d3Sopenharmony_ci } 179094332d3Sopenharmony_ci 180094332d3Sopenharmony_ci drvData->sensorCfg = GyroCreateCfgData(device->property); 181094332d3Sopenharmony_ci if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) { 182094332d3Sopenharmony_ci HDF_LOGD("%s: Creating gyrocfg failed because detection failed", __func__); 183094332d3Sopenharmony_ci return HDF_ERR_NOT_SUPPORT; 184094332d3Sopenharmony_ci } 185094332d3Sopenharmony_ci 186094332d3Sopenharmony_ci ops.Init = NULL; 187094332d3Sopenharmony_ci ops.ReadData = ReadBmi160GyroData; 188094332d3Sopenharmony_ci ret = GyroRegisterChipOps(&ops); 189094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 190094332d3Sopenharmony_ci HDF_LOGE("%s: Register BMI160 gyro failed", __func__); 191094332d3Sopenharmony_ci return HDF_FAILURE; 192094332d3Sopenharmony_ci } 193094332d3Sopenharmony_ci 194094332d3Sopenharmony_ci ret = InitBmi160(drvData->sensorCfg); 195094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 196094332d3Sopenharmony_ci HDF_LOGE("%s: Init BMI160 gyro failed", __func__); 197094332d3Sopenharmony_ci return HDF_FAILURE; 198094332d3Sopenharmony_ci } 199094332d3Sopenharmony_ci 200094332d3Sopenharmony_ci return HDF_SUCCESS; 201094332d3Sopenharmony_ci} 202094332d3Sopenharmony_ci 203094332d3Sopenharmony_cistatic void GyroBmi160ReleaseDriver(struct HdfDeviceObject *device) 204094332d3Sopenharmony_ci{ 205094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN(device); 206094332d3Sopenharmony_ci 207094332d3Sopenharmony_ci struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service; 208094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN(drvData); 209094332d3Sopenharmony_ci 210094332d3Sopenharmony_ci if (drvData->sensorCfg != NULL) { 211094332d3Sopenharmony_ci GyroReleaseCfgData(drvData->sensorCfg); 212094332d3Sopenharmony_ci drvData->sensorCfg = NULL; 213094332d3Sopenharmony_ci } 214094332d3Sopenharmony_ci 215094332d3Sopenharmony_ci OsalMemFree(drvData); 216094332d3Sopenharmony_ci} 217094332d3Sopenharmony_ci 218094332d3Sopenharmony_cistruct HdfDriverEntry g_gyroBmi160DevEntry = { 219094332d3Sopenharmony_ci .moduleVersion = 1, 220094332d3Sopenharmony_ci .moduleName = "HDF_SENSOR_GYRO_BMI160", 221094332d3Sopenharmony_ci .Bind = GyroBmi160BindDriver, 222094332d3Sopenharmony_ci .Init = GyroBmi160InitDriver, 223094332d3Sopenharmony_ci .Release = GyroBmi160ReleaseDriver, 224094332d3Sopenharmony_ci}; 225094332d3Sopenharmony_ci 226094332d3Sopenharmony_ciHDF_INIT(g_gyroBmi160DevEntry); 227