1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 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 "pedometer_bmi160.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_pedometer_driver.h"
16094332d3Sopenharmony_ci
17094332d3Sopenharmony_ci#define HDF_LOG_TAG    khdf_sensor_pedometer_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 ReadBmi160PedometerRawData(struct SensorCfgData *data, struct PedometerData *rawData,
27094332d3Sopenharmony_ci    uint64_t *timestamp)
28094332d3Sopenharmony_ci{
29094332d3Sopenharmony_ci    uint8_t reg[PEDOMETER_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;
42094332d3Sopenharmony_ci
43094332d3Sopenharmony_ci    int32_t ret = ReadSensor(&data->busCfg, BMI160_PEDOMETER_LSB_ADDR, &reg[PEDOMETER_NU_LSB], sizeof(uint8_t));
44094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
45094332d3Sopenharmony_ci
46094332d3Sopenharmony_ci    ret = ReadSensor(&data->busCfg, BMI160_PEDOMETER_MSB_ADDR, &reg[PEDOMETER_NU_MSB], sizeof(uint8_t));
47094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
48094332d3Sopenharmony_ci
49094332d3Sopenharmony_ci    rawData->pedometer = (int16_t)(SENSOR_DATA_SHIFT_LEFT(reg[PEDOMETER_NU_MSB], SENSOR_DATA_WIDTH_8_BIT) |
50094332d3Sopenharmony_ci        reg[PEDOMETER_NU_LSB]);
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_ci    return HDF_SUCCESS;
53094332d3Sopenharmony_ci}
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_cistatic int32_t ReadBmi160PedometerData(struct SensorCfgData *data)
56094332d3Sopenharmony_ci{
57094332d3Sopenharmony_ci    int32_t ret;
58094332d3Sopenharmony_ci    struct PedometerData rawData = { 0 };
59094332d3Sopenharmony_ci    static int32_t tmp;
60094332d3Sopenharmony_ci    struct SensorReportEvent event;
61094332d3Sopenharmony_ci
62094332d3Sopenharmony_ci    ret = memset_s(&event, sizeof(event), 0, sizeof(event));
63094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "memset_s");
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
66094332d3Sopenharmony_ci
67094332d3Sopenharmony_ci    ret = ReadBmi160PedometerRawData(data, &rawData, &event.timestamp);
68094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
69094332d3Sopenharmony_ci        HDF_LOGE("%s: BMI160 read raw data failed", __func__);
70094332d3Sopenharmony_ci        return HDF_FAILURE;
71094332d3Sopenharmony_ci    }
72094332d3Sopenharmony_ci
73094332d3Sopenharmony_ci    event.sensorId = SENSOR_TAG_PEDOMETER;
74094332d3Sopenharmony_ci    event.option = 0;
75094332d3Sopenharmony_ci    event.mode = SENSOR_WORK_MODE_REALTIME;
76094332d3Sopenharmony_ci
77094332d3Sopenharmony_ci    tmp = rawData.pedometer;
78094332d3Sopenharmony_ci
79094332d3Sopenharmony_ci    event.dataLen = sizeof(tmp);
80094332d3Sopenharmony_ci    event.data = (uint8_t *)&tmp;
81094332d3Sopenharmony_ci    ret = ReportSensorEvent(&event);
82094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
83094332d3Sopenharmony_ci        HDF_LOGE("%s: BMI160 report data failed", __func__);
84094332d3Sopenharmony_ci    }
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ci    return ret;
87094332d3Sopenharmony_ci}
88094332d3Sopenharmony_ci
89094332d3Sopenharmony_cistatic int32_t InitBmi160(struct SensorCfgData *data)
90094332d3Sopenharmony_ci{
91094332d3Sopenharmony_ci    int32_t ret;
92094332d3Sopenharmony_ci
93094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
94094332d3Sopenharmony_ci    ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
95094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
96094332d3Sopenharmony_ci        HDF_LOGE("%s: BMI160 sensor init config failed", __func__);
97094332d3Sopenharmony_ci        return HDF_FAILURE;
98094332d3Sopenharmony_ci    }
99094332d3Sopenharmony_ci    return HDF_SUCCESS;
100094332d3Sopenharmony_ci}
101094332d3Sopenharmony_ci
102094332d3Sopenharmony_cistatic int32_t InitPedometerPreConfig(void)
103094332d3Sopenharmony_ci{
104094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
105094332d3Sopenharmony_ci        HDF_LOGE("%s: Data write mux pin failed", __func__);
106094332d3Sopenharmony_ci        return HDF_FAILURE;
107094332d3Sopenharmony_ci    }
108094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
109094332d3Sopenharmony_ci        HDF_LOGE("%s: Clk write mux pin failed", __func__);
110094332d3Sopenharmony_ci        return HDF_FAILURE;
111094332d3Sopenharmony_ci    }
112094332d3Sopenharmony_ci
113094332d3Sopenharmony_ci    return HDF_SUCCESS;
114094332d3Sopenharmony_ci}
115094332d3Sopenharmony_ci
116094332d3Sopenharmony_cistatic int32_t DispatchBMI160(struct HdfDeviceIoClient *client,
117094332d3Sopenharmony_ci    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
118094332d3Sopenharmony_ci{
119094332d3Sopenharmony_ci    (void)client;
120094332d3Sopenharmony_ci    (void)cmd;
121094332d3Sopenharmony_ci    (void)data;
122094332d3Sopenharmony_ci    (void)reply;
123094332d3Sopenharmony_ci
124094332d3Sopenharmony_ci    return HDF_SUCCESS;
125094332d3Sopenharmony_ci}
126094332d3Sopenharmony_ci
127094332d3Sopenharmony_cistatic int32_t PedometerBmi160BindDriver(struct HdfDeviceObject *device)
128094332d3Sopenharmony_ci{
129094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
130094332d3Sopenharmony_ci
131094332d3Sopenharmony_ci    struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)OsalMemCalloc(sizeof(*drvData));
132094332d3Sopenharmony_ci    if (drvData == NULL) {
133094332d3Sopenharmony_ci        HDF_LOGE("%s: Malloc Bmi160 drv data fail", __func__);
134094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
135094332d3Sopenharmony_ci    }
136094332d3Sopenharmony_ci
137094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchBMI160;
138094332d3Sopenharmony_ci    drvData->device = device;
139094332d3Sopenharmony_ci    device->service = &drvData->ioService;
140094332d3Sopenharmony_ci    g_bmi160DrvData = drvData;
141094332d3Sopenharmony_ci
142094332d3Sopenharmony_ci    return HDF_SUCCESS;
143094332d3Sopenharmony_ci}
144094332d3Sopenharmony_ci
145094332d3Sopenharmony_cistatic int32_t PedometerBmi160InitDriver(struct HdfDeviceObject *device)
146094332d3Sopenharmony_ci{
147094332d3Sopenharmony_ci    int32_t ret;
148094332d3Sopenharmony_ci    struct PedometerOpsCall ops;
149094332d3Sopenharmony_ci
150094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
151094332d3Sopenharmony_ci    struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
152094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
153094332d3Sopenharmony_ci
154094332d3Sopenharmony_ci    ret = InitPedometerPreConfig();
155094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
156094332d3Sopenharmony_ci        HDF_LOGE("%s: Init  BMI160 bus mux config", __func__);
157094332d3Sopenharmony_ci        return HDF_FAILURE;
158094332d3Sopenharmony_ci    }
159094332d3Sopenharmony_ci
160094332d3Sopenharmony_ci    drvData->sensorCfg = PedometerCreateCfgData(device->property);
161094332d3Sopenharmony_ci    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
162094332d3Sopenharmony_ci        HDF_LOGD("%s: Creating pedometercfg failed because detection failed", __func__);
163094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
164094332d3Sopenharmony_ci    }
165094332d3Sopenharmony_ci
166094332d3Sopenharmony_ci    ops.Init = NULL;
167094332d3Sopenharmony_ci    ops.ReadData = ReadBmi160PedometerData;
168094332d3Sopenharmony_ci    ret = PedometerRegisterChipOps(&ops);
169094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
170094332d3Sopenharmony_ci        HDF_LOGE("%s: Register BMI160 pedometer failed", __func__);
171094332d3Sopenharmony_ci        return HDF_FAILURE;
172094332d3Sopenharmony_ci    }
173094332d3Sopenharmony_ci
174094332d3Sopenharmony_ci    ret = InitBmi160(drvData->sensorCfg);
175094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
176094332d3Sopenharmony_ci        HDF_LOGE("%s: Init BMI160 pedometer failed", __func__);
177094332d3Sopenharmony_ci        return HDF_FAILURE;
178094332d3Sopenharmony_ci    }
179094332d3Sopenharmony_ci
180094332d3Sopenharmony_ci    return HDF_SUCCESS;
181094332d3Sopenharmony_ci}
182094332d3Sopenharmony_ci
183094332d3Sopenharmony_cistatic void PedometerBmi160ReleaseDriver(struct HdfDeviceObject *device)
184094332d3Sopenharmony_ci{
185094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(device);
186094332d3Sopenharmony_ci
187094332d3Sopenharmony_ci    struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
188094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(drvData);
189094332d3Sopenharmony_ci
190094332d3Sopenharmony_ci    if (drvData->sensorCfg != NULL) {
191094332d3Sopenharmony_ci        PedometerReleaseCfgData(drvData->sensorCfg);
192094332d3Sopenharmony_ci        drvData->sensorCfg = NULL;
193094332d3Sopenharmony_ci    }
194094332d3Sopenharmony_ci    OsalMemFree(drvData);
195094332d3Sopenharmony_ci}
196094332d3Sopenharmony_ci
197094332d3Sopenharmony_cistruct HdfDriverEntry g_pedometerBmi160DevEntry = {
198094332d3Sopenharmony_ci    .moduleVersion = 1,
199094332d3Sopenharmony_ci    .moduleName = "HDF_SENSOR_PEDOMETER_BMI160",
200094332d3Sopenharmony_ci    .Bind = PedometerBmi160BindDriver,
201094332d3Sopenharmony_ci    .Init = PedometerBmi160InitDriver,
202094332d3Sopenharmony_ci    .Release = PedometerBmi160ReleaseDriver,
203094332d3Sopenharmony_ci};
204094332d3Sopenharmony_ci
205094332d3Sopenharmony_ciHDF_INIT(g_pedometerBmi160DevEntry);
206