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 "magnetic_lsm303.h" 10094332d3Sopenharmony_ci#include <securec.h> 11094332d3Sopenharmony_ci#include "osal_mem.h" 12094332d3Sopenharmony_ci#include "osal_time.h" 13094332d3Sopenharmony_ci#include "sensor_config_controller.h" 14094332d3Sopenharmony_ci#include "sensor_device_manager.h" 15094332d3Sopenharmony_ci#include "sensor_magnetic_driver.h" 16094332d3Sopenharmony_ci 17094332d3Sopenharmony_ci#define HDF_LOG_TAG khdf_sensor_magnetic_driver 18094332d3Sopenharmony_ci 19094332d3Sopenharmony_cistatic struct Lsm303DrvData *g_lsm303DrvData = 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 ReadLsm303RawData(struct SensorCfgData *data, struct MagneticData *rawData, uint64_t *timestamp) 27094332d3Sopenharmony_ci{ 28094332d3Sopenharmony_ci uint8_t status = 0; 29094332d3Sopenharmony_ci uint8_t reg[MAGNETIC_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, LSM303_STATUS_ADDR, &status, sizeof(uint8_t)); 44094332d3Sopenharmony_ci if (!(status & LSM303_DATA_READY_MASK) || (ret != HDF_SUCCESS)) { 45094332d3Sopenharmony_ci HDF_LOGE("%s: data status [%u] ret [%d]", __func__, status, ret); 46094332d3Sopenharmony_ci return HDF_FAILURE; 47094332d3Sopenharmony_ci } 48094332d3Sopenharmony_ci 49094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_X_MSB_ADDR, ®[MAGNETIC_X_AXIS_MSB], sizeof(uint8_t)); 50094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 51094332d3Sopenharmony_ci 52094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_X_LSB_ADDR, ®[MAGNETIC_X_AXIS_LSB], sizeof(uint8_t)); 53094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 54094332d3Sopenharmony_ci 55094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_Y_MSB_ADDR, ®[MAGNETIC_Y_AXIS_MSB], sizeof(uint8_t)); 56094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 57094332d3Sopenharmony_ci 58094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_Y_LSB_ADDR, ®[MAGNETIC_Y_AXIS_LSB], sizeof(uint8_t)); 59094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 60094332d3Sopenharmony_ci 61094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_Z_MSB_ADDR, ®[MAGNETIC_Z_AXIS_MSB], sizeof(uint8_t)); 62094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 63094332d3Sopenharmony_ci 64094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, LSM303_MAGNETIC_Z_LSB_ADDR, ®[MAGNETIC_Z_AXIS_LSB], 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[MAGNETIC_X_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) | 68094332d3Sopenharmony_ci reg[MAGNETIC_X_AXIS_LSB]); 69094332d3Sopenharmony_ci rawData->y = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[MAGNETIC_Y_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) | 70094332d3Sopenharmony_ci reg[MAGNETIC_Y_AXIS_LSB]); 71094332d3Sopenharmony_ci rawData->z = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[MAGNETIC_Z_AXIS_MSB], SENSOR_DATA_WIDTH_8_BIT) | 72094332d3Sopenharmony_ci reg[MAGNETIC_Z_AXIS_LSB]); 73094332d3Sopenharmony_ci 74094332d3Sopenharmony_ci return HDF_SUCCESS; 75094332d3Sopenharmony_ci} 76094332d3Sopenharmony_ci 77094332d3Sopenharmony_cistatic int32_t ReadLsm303Data(struct SensorCfgData *data) 78094332d3Sopenharmony_ci{ 79094332d3Sopenharmony_ci struct MagneticData rawData = { 0, 0, 0 }; 80094332d3Sopenharmony_ci int32_t tmp[MAGNETIC_AXIS_NUM]; 81094332d3Sopenharmony_ci struct SensorReportEvent event; 82094332d3Sopenharmony_ci 83094332d3Sopenharmony_ci (void)memset_s(&event, sizeof(event), 0, sizeof(event)); 84094332d3Sopenharmony_ci (void)memset_s(tmp, sizeof(tmp), 0, sizeof(tmp)); 85094332d3Sopenharmony_ci 86094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM); 87094332d3Sopenharmony_ci 88094332d3Sopenharmony_ci int32_t ret = ReadLsm303RawData(data, &rawData, &event.timestamp); 89094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 90094332d3Sopenharmony_ci HDF_LOGE("%s: LSM303 read raw data failed", __func__); 91094332d3Sopenharmony_ci return HDF_FAILURE; 92094332d3Sopenharmony_ci } 93094332d3Sopenharmony_ci 94094332d3Sopenharmony_ci event.sensorId = SENSOR_TAG_MAGNETIC_FIELD; 95094332d3Sopenharmony_ci event.option = 0; 96094332d3Sopenharmony_ci event.mode = SENSOR_WORK_MODE_REALTIME; 97094332d3Sopenharmony_ci 98094332d3Sopenharmony_ci tmp[MAGNETIC_X_AXIS] = rawData.x * LSM303_MAGNETIC_GIN / LSM303DLHC_SENSITIVITY_XY47GA; 99094332d3Sopenharmony_ci tmp[MAGNETIC_Y_AXIS] = rawData.y * LSM303_MAGNETIC_GIN / LSM303DLHC_SENSITIVITY_XY47GA; 100094332d3Sopenharmony_ci tmp[MAGNETIC_Z_AXIS] = rawData.z * LSM303_MAGNETIC_GIN / LSM303DLHC_SENSITIVITY_Z47GA; 101094332d3Sopenharmony_ci 102094332d3Sopenharmony_ci ret = SensorRawDataToRemapData(data->direction, tmp, sizeof(tmp) / sizeof(tmp[0])); 103094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 104094332d3Sopenharmony_ci HDF_LOGE("%s: LSM303 convert raw data failed", __func__); 105094332d3Sopenharmony_ci return HDF_FAILURE; 106094332d3Sopenharmony_ci } 107094332d3Sopenharmony_ci 108094332d3Sopenharmony_ci event.dataLen = sizeof(tmp); 109094332d3Sopenharmony_ci event.data = (uint8_t *)&tmp; 110094332d3Sopenharmony_ci ret = ReportSensorEvent(&event); 111094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 112094332d3Sopenharmony_ci HDF_LOGE("%s: LSM303 report data failed", __func__); 113094332d3Sopenharmony_ci } 114094332d3Sopenharmony_ci 115094332d3Sopenharmony_ci return ret; 116094332d3Sopenharmony_ci} 117094332d3Sopenharmony_ci 118094332d3Sopenharmony_cistatic int32_t InitLsm303(struct SensorCfgData *data) 119094332d3Sopenharmony_ci{ 120094332d3Sopenharmony_ci int32_t ret; 121094332d3Sopenharmony_ci 122094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM); 123094332d3Sopenharmony_ci 124094332d3Sopenharmony_ci ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]); 125094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 126094332d3Sopenharmony_ci HDF_LOGE("%s: Lsm303 sensor init config failed", __func__); 127094332d3Sopenharmony_ci return HDF_FAILURE; 128094332d3Sopenharmony_ci } 129094332d3Sopenharmony_ci 130094332d3Sopenharmony_ci return HDF_SUCCESS; 131094332d3Sopenharmony_ci} 132094332d3Sopenharmony_ci 133094332d3Sopenharmony_cistatic int32_t InitMagneticPreConfig(void) 134094332d3Sopenharmony_ci{ 135094332d3Sopenharmony_ci if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) { 136094332d3Sopenharmony_ci HDF_LOGE("%s: Data write mux pin failed", __func__); 137094332d3Sopenharmony_ci return HDF_FAILURE; 138094332d3Sopenharmony_ci } 139094332d3Sopenharmony_ci if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) { 140094332d3Sopenharmony_ci HDF_LOGE("%s: Clk write mux pin failed", __func__); 141094332d3Sopenharmony_ci return HDF_FAILURE; 142094332d3Sopenharmony_ci } 143094332d3Sopenharmony_ci 144094332d3Sopenharmony_ci return HDF_SUCCESS; 145094332d3Sopenharmony_ci} 146094332d3Sopenharmony_ci 147094332d3Sopenharmony_cistatic int32_t DispatchLsm303(struct HdfDeviceIoClient *client, 148094332d3Sopenharmony_ci int cmd, struct HdfSBuf *data, struct HdfSBuf *reply) 149094332d3Sopenharmony_ci{ 150094332d3Sopenharmony_ci (void)client; 151094332d3Sopenharmony_ci (void)cmd; 152094332d3Sopenharmony_ci (void)data; 153094332d3Sopenharmony_ci (void)reply; 154094332d3Sopenharmony_ci 155094332d3Sopenharmony_ci return HDF_SUCCESS; 156094332d3Sopenharmony_ci} 157094332d3Sopenharmony_ci 158094332d3Sopenharmony_cistatic int32_t Lsm303BindDriver(struct HdfDeviceObject *device) 159094332d3Sopenharmony_ci{ 160094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM); 161094332d3Sopenharmony_ci 162094332d3Sopenharmony_ci struct Lsm303DrvData *drvData = (struct Lsm303DrvData *)OsalMemCalloc(sizeof(*drvData)); 163094332d3Sopenharmony_ci if (drvData == NULL) { 164094332d3Sopenharmony_ci HDF_LOGE("%s: Malloc Lsm303 drv data fail", __func__); 165094332d3Sopenharmony_ci return HDF_ERR_MALLOC_FAIL; 166094332d3Sopenharmony_ci } 167094332d3Sopenharmony_ci 168094332d3Sopenharmony_ci drvData->ioService.Dispatch = DispatchLsm303; 169094332d3Sopenharmony_ci drvData->device = device; 170094332d3Sopenharmony_ci device->service = &drvData->ioService; 171094332d3Sopenharmony_ci g_lsm303DrvData = drvData; 172094332d3Sopenharmony_ci 173094332d3Sopenharmony_ci return HDF_SUCCESS; 174094332d3Sopenharmony_ci} 175094332d3Sopenharmony_ci 176094332d3Sopenharmony_cistatic int32_t Lsm303InitDriver(struct HdfDeviceObject *device) 177094332d3Sopenharmony_ci{ 178094332d3Sopenharmony_ci int32_t ret; 179094332d3Sopenharmony_ci struct MagneticOpsCall ops; 180094332d3Sopenharmony_ci 181094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM); 182094332d3Sopenharmony_ci struct Lsm303DrvData *drvData = (struct Lsm303DrvData *)device->service; 183094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM); 184094332d3Sopenharmony_ci 185094332d3Sopenharmony_ci ret = InitMagneticPreConfig(); 186094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 187094332d3Sopenharmony_ci HDF_LOGE("%s: Init Lsm303 bus mux config", __func__); 188094332d3Sopenharmony_ci return HDF_FAILURE; 189094332d3Sopenharmony_ci } 190094332d3Sopenharmony_ci 191094332d3Sopenharmony_ci drvData->sensorCfg = MagneticCreateCfgData(device->property); 192094332d3Sopenharmony_ci if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) { 193094332d3Sopenharmony_ci HDF_LOGD("%s: Creating magneticcfg failed because detection failed", __func__); 194094332d3Sopenharmony_ci return HDF_ERR_NOT_SUPPORT; 195094332d3Sopenharmony_ci } 196094332d3Sopenharmony_ci 197094332d3Sopenharmony_ci ops.Init = NULL; 198094332d3Sopenharmony_ci ops.ReadData = ReadLsm303Data; 199094332d3Sopenharmony_ci ret = MagneticRegisterChipOps(&ops); 200094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 201094332d3Sopenharmony_ci HDF_LOGE("%s: Register lsm303 magnetic failed", __func__); 202094332d3Sopenharmony_ci return HDF_FAILURE; 203094332d3Sopenharmony_ci } 204094332d3Sopenharmony_ci 205094332d3Sopenharmony_ci ret = InitLsm303(drvData->sensorCfg); 206094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 207094332d3Sopenharmony_ci HDF_LOGE("%s: Init lsm303 magnetic failed", __func__); 208094332d3Sopenharmony_ci return HDF_FAILURE; 209094332d3Sopenharmony_ci } 210094332d3Sopenharmony_ci 211094332d3Sopenharmony_ci return HDF_SUCCESS; 212094332d3Sopenharmony_ci} 213094332d3Sopenharmony_ci 214094332d3Sopenharmony_cistatic void Lsm303ReleaseDriver(struct HdfDeviceObject *device) 215094332d3Sopenharmony_ci{ 216094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN(device); 217094332d3Sopenharmony_ci 218094332d3Sopenharmony_ci struct Lsm303DrvData *drvData = (struct Lsm303DrvData *)device->service; 219094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN(drvData); 220094332d3Sopenharmony_ci 221094332d3Sopenharmony_ci if (drvData->sensorCfg != NULL) { 222094332d3Sopenharmony_ci MagneticReleaseCfgData(drvData->sensorCfg); 223094332d3Sopenharmony_ci drvData->sensorCfg = NULL; 224094332d3Sopenharmony_ci } 225094332d3Sopenharmony_ci OsalMemFree(drvData); 226094332d3Sopenharmony_ci} 227094332d3Sopenharmony_ci 228094332d3Sopenharmony_cistruct HdfDriverEntry g_magneticLsm303DevEntry = { 229094332d3Sopenharmony_ci .moduleVersion = 1, 230094332d3Sopenharmony_ci .moduleName = "HDF_SENSOR_MAGNETIC_LSM303", 231094332d3Sopenharmony_ci .Bind = Lsm303BindDriver, 232094332d3Sopenharmony_ci .Init = Lsm303InitDriver, 233094332d3Sopenharmony_ci .Release = Lsm303ReleaseDriver, 234094332d3Sopenharmony_ci}; 235094332d3Sopenharmony_ci 236094332d3Sopenharmony_ciHDF_INIT(g_magneticLsm303DevEntry);