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_mxc6655xa.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#define MXC6655_RANGE (16384 * 2) 19094332d3Sopenharmony_ci#define MXC6655_PRECISION 12 20094332d3Sopenharmony_ci#define MXC6655_BOUNDARY (0x1 << (MXC6655_PRECISION - 1)) 21094332d3Sopenharmony_ci#define MXC6655_GRAVITY_STEP (MXC6655_RANGE / MXC6655_BOUNDARY) 22094332d3Sopenharmony_ci#define MXC6655_MASK 0x7fff 23094332d3Sopenharmony_ci#define MXC6655_ACCEL_OUTPUT_16BIT 16 24094332d3Sopenharmony_ci#define MXC6655_ACCEL_OUTPUT_MSB 8 25094332d3Sopenharmony_ci 26094332d3Sopenharmony_cistatic struct Mxc6655xaDrvData *g_mxc6655xaDrvData = NULL; 27094332d3Sopenharmony_ci 28094332d3Sopenharmony_cistatic struct Mxc6655xaDrvData *Mxc6655xaGetDrvData(void) 29094332d3Sopenharmony_ci{ 30094332d3Sopenharmony_ci return g_mxc6655xaDrvData; 31094332d3Sopenharmony_ci} 32094332d3Sopenharmony_ci 33094332d3Sopenharmony_cistatic int SensorConvertData(char highByte, char lowByte) 34094332d3Sopenharmony_ci{ 35094332d3Sopenharmony_ci int32_t result; 36094332d3Sopenharmony_ci 37094332d3Sopenharmony_ci result = ((uint32_t)highByte << (MXC6655_PRECISION - MXC6655_ACCEL_OUTPUT_MSB)) | 38094332d3Sopenharmony_ci ((uint32_t)lowByte >> (MXC6655_ACCEL_OUTPUT_16BIT - MXC6655_PRECISION)); 39094332d3Sopenharmony_ci 40094332d3Sopenharmony_ci if (result < MXC6655_BOUNDARY) { 41094332d3Sopenharmony_ci result = result * MXC6655_GRAVITY_STEP; 42094332d3Sopenharmony_ci } else { 43094332d3Sopenharmony_ci result = ~(((~result & (MXC6655_MASK >> (MXC6655_ACCEL_OUTPUT_16BIT - MXC6655_PRECISION))) + 1) * 44094332d3Sopenharmony_ci MXC6655_GRAVITY_STEP) + 1; 45094332d3Sopenharmony_ci } 46094332d3Sopenharmony_ci 47094332d3Sopenharmony_ci return result; 48094332d3Sopenharmony_ci} 49094332d3Sopenharmony_ci 50094332d3Sopenharmony_cistatic int32_t ReadMxc6655xaRawData(struct SensorCfgData *data, struct AccelData *rawData, uint64_t *timestamp) 51094332d3Sopenharmony_ci{ 52094332d3Sopenharmony_ci uint8_t status = 0; 53094332d3Sopenharmony_ci uint8_t reg[ACCEL_AXIS_BUTT]; 54094332d3Sopenharmony_ci OsalTimespec time; 55094332d3Sopenharmony_ci int32_t x; 56094332d3Sopenharmony_ci int32_t y; 57094332d3Sopenharmony_ci int32_t z; 58094332d3Sopenharmony_ci 59094332d3Sopenharmony_ci (void)memset_s(&time, sizeof(time), 0, sizeof(time)); 60094332d3Sopenharmony_ci (void)memset_s(reg, sizeof(reg), 0, sizeof(reg)); 61094332d3Sopenharmony_ci 62094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM); 63094332d3Sopenharmony_ci 64094332d3Sopenharmony_ci if (OsalGetTime(&time) != HDF_SUCCESS) { 65094332d3Sopenharmony_ci HDF_LOGE("%s: Get time failed", __func__); 66094332d3Sopenharmony_ci return HDF_FAILURE; 67094332d3Sopenharmony_ci } 68094332d3Sopenharmony_ci *timestamp = time.sec * SENSOR_SECOND_CONVERT_NANOSECOND + time.usec * SENSOR_CONVERT_UNIT; /* unit nanosecond */ 69094332d3Sopenharmony_ci 70094332d3Sopenharmony_ci int32_t ret = ReadSensor(&data->busCfg, MXC6655XA_STATUS_ADDR, &status, sizeof(uint8_t)); 71094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 72094332d3Sopenharmony_ci HDF_LOGE("%s: data status [%u] ret [%d]", __func__, status, ret); 73094332d3Sopenharmony_ci return HDF_FAILURE; 74094332d3Sopenharmony_ci } 75094332d3Sopenharmony_ci 76094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_X_LSB_ADDR, ®[ACCEL_X_AXIS_LSB], sizeof(uint8_t)); 77094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 78094332d3Sopenharmony_ci 79094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_X_MSB_ADDR, ®[ACCEL_X_AXIS_MSB], sizeof(uint8_t)); 80094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 81094332d3Sopenharmony_ci 82094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_Y_LSB_ADDR, ®[ACCEL_Y_AXIS_LSB], sizeof(uint8_t)); 83094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 84094332d3Sopenharmony_ci 85094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_Y_MSB_ADDR, ®[ACCEL_Y_AXIS_MSB], sizeof(uint8_t)); 86094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 87094332d3Sopenharmony_ci 88094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_Z_LSB_ADDR, ®[ACCEL_Z_AXIS_LSB], sizeof(uint8_t)); 89094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 90094332d3Sopenharmony_ci 91094332d3Sopenharmony_ci ret = ReadSensor(&data->busCfg, MXC6655XA_ACCEL_Z_MSB_ADDR, ®[ACCEL_Z_AXIS_MSB], sizeof(uint8_t)); 92094332d3Sopenharmony_ci CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data"); 93094332d3Sopenharmony_ci 94094332d3Sopenharmony_ci x = SensorConvertData(reg[ACCEL_X_AXIS_MSB], reg[ACCEL_X_AXIS_LSB]); 95094332d3Sopenharmony_ci y = SensorConvertData(reg[ACCEL_Y_AXIS_MSB], reg[ACCEL_Y_AXIS_LSB]); 96094332d3Sopenharmony_ci z = SensorConvertData(reg[ACCEL_Z_AXIS_MSB], reg[ACCEL_Z_AXIS_LSB]); 97094332d3Sopenharmony_ci rawData->x = x; 98094332d3Sopenharmony_ci rawData->y = y; 99094332d3Sopenharmony_ci rawData->z = z; 100094332d3Sopenharmony_ci 101094332d3Sopenharmony_ci return HDF_SUCCESS; 102094332d3Sopenharmony_ci} 103094332d3Sopenharmony_ci 104094332d3Sopenharmony_cistatic int32_t ReadMxc6655xaData(struct SensorCfgData *cfg, struct SensorReportEvent *event) 105094332d3Sopenharmony_ci{ 106094332d3Sopenharmony_ci int32_t ret; 107094332d3Sopenharmony_ci struct AccelData rawData = { 0, 0, 0 }; 108094332d3Sopenharmony_ci static int32_t tmp[ACCEL_AXIS_NUM]; 109094332d3Sopenharmony_ci 110094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(cfg, HDF_ERR_INVALID_PARAM); 111094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(event, HDF_ERR_INVALID_PARAM); 112094332d3Sopenharmony_ci 113094332d3Sopenharmony_ci ret = ReadMxc6655xaRawData(cfg, &rawData, &event->timestamp); 114094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 115094332d3Sopenharmony_ci HDF_LOGE("%s: MXC6655XA read raw data failed", __func__); 116094332d3Sopenharmony_ci return HDF_FAILURE; 117094332d3Sopenharmony_ci } 118094332d3Sopenharmony_ci 119094332d3Sopenharmony_ci event->sensorId = SENSOR_TAG_ACCELEROMETER; 120094332d3Sopenharmony_ci event->option = 0; 121094332d3Sopenharmony_ci event->mode = SENSOR_WORK_MODE_REALTIME; 122094332d3Sopenharmony_ci 123094332d3Sopenharmony_ci rawData.x = rawData.x * MXC6655XA_ACC_SENSITIVITY_2G; 124094332d3Sopenharmony_ci rawData.y = rawData.y * MXC6655XA_ACC_SENSITIVITY_2G; 125094332d3Sopenharmony_ci rawData.z = rawData.z * MXC6655XA_ACC_SENSITIVITY_2G; 126094332d3Sopenharmony_ci 127094332d3Sopenharmony_ci tmp[ACCEL_X_AXIS] = (rawData.x * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT; 128094332d3Sopenharmony_ci tmp[ACCEL_Y_AXIS] = (rawData.y * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT; 129094332d3Sopenharmony_ci tmp[ACCEL_Z_AXIS] = (rawData.z * SENSOR_CONVERT_UNIT) / SENSOR_CONVERT_UNIT; 130094332d3Sopenharmony_ci 131094332d3Sopenharmony_ci ret = SensorRawDataToRemapData(cfg->direction, tmp, sizeof(tmp) / sizeof(tmp[0])); 132094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 133094332d3Sopenharmony_ci HDF_LOGE("%s: MXC6655XA convert raw data failed", __func__); 134094332d3Sopenharmony_ci return HDF_FAILURE; 135094332d3Sopenharmony_ci } 136094332d3Sopenharmony_ci 137094332d3Sopenharmony_ci event->dataLen = sizeof(tmp); 138094332d3Sopenharmony_ci event->data = (uint8_t *)&tmp; 139094332d3Sopenharmony_ci 140094332d3Sopenharmony_ci return ret; 141094332d3Sopenharmony_ci} 142094332d3Sopenharmony_ci 143094332d3Sopenharmony_cistatic int32_t InitMxc6655xa(struct SensorCfgData *data) 144094332d3Sopenharmony_ci{ 145094332d3Sopenharmony_ci int32_t ret; 146094332d3Sopenharmony_ci 147094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM); 148094332d3Sopenharmony_ci ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]); 149094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 150094332d3Sopenharmony_ci HDF_LOGE("%s: MXC6655XA sensor init config failed", __func__); 151094332d3Sopenharmony_ci return HDF_FAILURE; 152094332d3Sopenharmony_ci } 153094332d3Sopenharmony_ci return HDF_SUCCESS; 154094332d3Sopenharmony_ci} 155094332d3Sopenharmony_ci 156094332d3Sopenharmony_cistatic int32_t DispatchMXC6655xa(struct HdfDeviceIoClient *client, 157094332d3Sopenharmony_ci int cmd, struct HdfSBuf *data, struct HdfSBuf *reply) 158094332d3Sopenharmony_ci{ 159094332d3Sopenharmony_ci (void)client; 160094332d3Sopenharmony_ci (void)cmd; 161094332d3Sopenharmony_ci (void)data; 162094332d3Sopenharmony_ci (void)reply; 163094332d3Sopenharmony_ci 164094332d3Sopenharmony_ci return HDF_SUCCESS; 165094332d3Sopenharmony_ci} 166094332d3Sopenharmony_ci 167094332d3Sopenharmony_cistatic int32_t Mxc6655xaBindDriver(struct HdfDeviceObject *device) 168094332d3Sopenharmony_ci{ 169094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM); 170094332d3Sopenharmony_ci 171094332d3Sopenharmony_ci struct Mxc6655xaDrvData *drvData = (struct Mxc6655xaDrvData *)OsalMemCalloc(sizeof(*drvData)); 172094332d3Sopenharmony_ci if (drvData == NULL) { 173094332d3Sopenharmony_ci HDF_LOGE("%s: Malloc MXC6655XA drv data fail", __func__); 174094332d3Sopenharmony_ci return HDF_ERR_MALLOC_FAIL; 175094332d3Sopenharmony_ci } 176094332d3Sopenharmony_ci 177094332d3Sopenharmony_ci drvData->ioService.Dispatch = DispatchMXC6655xa; 178094332d3Sopenharmony_ci drvData->device = device; 179094332d3Sopenharmony_ci device->service = &drvData->ioService; 180094332d3Sopenharmony_ci g_mxc6655xaDrvData = drvData; 181094332d3Sopenharmony_ci 182094332d3Sopenharmony_ci return HDF_SUCCESS; 183094332d3Sopenharmony_ci} 184094332d3Sopenharmony_ci 185094332d3Sopenharmony_cistatic int32_t Mxc6655xaInitDriver(struct HdfDeviceObject *device) 186094332d3Sopenharmony_ci{ 187094332d3Sopenharmony_ci int32_t ret; 188094332d3Sopenharmony_ci struct AccelOpsCall ops; 189094332d3Sopenharmony_ci 190094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM); 191094332d3Sopenharmony_ci struct Mxc6655xaDrvData *drvData = (struct Mxc6655xaDrvData *)device->service; 192094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM); 193094332d3Sopenharmony_ci 194094332d3Sopenharmony_ci drvData->sensorCfg = AccelCreateCfgData(device->property); 195094332d3Sopenharmony_ci if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) { 196094332d3Sopenharmony_ci HDF_LOGD("%s: Creating accelcfg failed because detection failed", __func__); 197094332d3Sopenharmony_ci return HDF_ERR_NOT_SUPPORT; 198094332d3Sopenharmony_ci } 199094332d3Sopenharmony_ci 200094332d3Sopenharmony_ci ops.Init = NULL; 201094332d3Sopenharmony_ci ops.ReadData = ReadMxc6655xaData; 202094332d3Sopenharmony_ci ret = AccelRegisterChipOps(&ops); 203094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 204094332d3Sopenharmony_ci HDF_LOGE("%s: Register MXC6655XA accel failed", __func__); 205094332d3Sopenharmony_ci return HDF_FAILURE; 206094332d3Sopenharmony_ci } 207094332d3Sopenharmony_ci 208094332d3Sopenharmony_ci ret = InitMxc6655xa(drvData->sensorCfg); 209094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 210094332d3Sopenharmony_ci HDF_LOGE("%s: Init MXC6655XA accel failed", __func__); 211094332d3Sopenharmony_ci return HDF_FAILURE; 212094332d3Sopenharmony_ci } 213094332d3Sopenharmony_ci 214094332d3Sopenharmony_ci return HDF_SUCCESS; 215094332d3Sopenharmony_ci} 216094332d3Sopenharmony_ci 217094332d3Sopenharmony_cistatic void Mxc6655xaReleaseDriver(struct HdfDeviceObject *device) 218094332d3Sopenharmony_ci{ 219094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN(device); 220094332d3Sopenharmony_ci 221094332d3Sopenharmony_ci struct Mxc6655xaDrvData *drvData = (struct Mxc6655xaDrvData *)device->service; 222094332d3Sopenharmony_ci CHECK_NULL_PTR_RETURN(drvData); 223094332d3Sopenharmony_ci 224094332d3Sopenharmony_ci if (drvData->sensorCfg != NULL) { 225094332d3Sopenharmony_ci AccelReleaseCfgData(drvData->sensorCfg); 226094332d3Sopenharmony_ci drvData->sensorCfg = NULL; 227094332d3Sopenharmony_ci } 228094332d3Sopenharmony_ci OsalMemFree(drvData); 229094332d3Sopenharmony_ci} 230094332d3Sopenharmony_ci 231094332d3Sopenharmony_cistruct HdfDriverEntry g_accelMxc6655xaDevEntry = { 232094332d3Sopenharmony_ci .moduleVersion = 1, 233094332d3Sopenharmony_ci .moduleName = "HDF_SENSOR_ACCEL_MXC6655XA", 234094332d3Sopenharmony_ci .Bind = Mxc6655xaBindDriver, 235094332d3Sopenharmony_ci .Init = Mxc6655xaInitDriver, 236094332d3Sopenharmony_ci .Release = Mxc6655xaReleaseDriver, 237094332d3Sopenharmony_ci}; 238094332d3Sopenharmony_ci 239094332d3Sopenharmony_ciHDF_INIT(g_accelMxc6655xaDevEntry); 240