1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021-2022 xu
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 "proximity_apds9960.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_proximity_driver.h"
16094332d3Sopenharmony_ci
17094332d3Sopenharmony_ci#define HDF_LOG_TAG    khdf_sensor_proximity_driver
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_ci#define PROXIMITY_STATE_FAR    5
20094332d3Sopenharmony_ci#define PROXIMITY_STATE_NEAR   0
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_cistatic struct Apds9960DrvData *g_apds9960DrvData = NULL;
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_ci/* IO config for int-pin and I2C-pin */
25094332d3Sopenharmony_ci#define SENSOR_I2C6_DATA_REG_ADDR 0x114f004c
26094332d3Sopenharmony_ci#define SENSOR_I2C6_CLK_REG_ADDR  0x114f0048
27094332d3Sopenharmony_ci#define SENSOR_I2C_REG_CFG        0x403
28094332d3Sopenharmony_ci
29094332d3Sopenharmony_cistatic int32_t ReadApds9960RawData(struct SensorCfgData *data, struct ProximityData *rawData, uint64_t *timestamp)
30094332d3Sopenharmony_ci{
31094332d3Sopenharmony_ci    OsalTimespec time;
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_ci    (void)memset_s(&time, sizeof(time), 0, sizeof(time));
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, APDS9960_PROXIMITY_DATA_ADDR, &rawData->stateFlag, sizeof(uint8_t));
44094332d3Sopenharmony_ci    CHECK_PARSER_RESULT_RETURN_VALUE(ret, "read data");
45094332d3Sopenharmony_ci
46094332d3Sopenharmony_ci    return ret;
47094332d3Sopenharmony_ci}
48094332d3Sopenharmony_ci
49094332d3Sopenharmony_cistatic int32_t ReadApds9960Data(struct SensorCfgData *data)
50094332d3Sopenharmony_ci{
51094332d3Sopenharmony_ci    int32_t ret;
52094332d3Sopenharmony_ci    int32_t tmp;
53094332d3Sopenharmony_ci    struct ProximityData rawData = { 5 };
54094332d3Sopenharmony_ci    struct SensorReportEvent event;
55094332d3Sopenharmony_ci
56094332d3Sopenharmony_ci    (void)memset_s(&event, sizeof(event), 0, sizeof(event));
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_ci    ret = ReadApds9960RawData(data, &rawData, &event.timestamp);
59094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
60094332d3Sopenharmony_ci        return HDF_FAILURE;
61094332d3Sopenharmony_ci    }
62094332d3Sopenharmony_ci
63094332d3Sopenharmony_ci    event.sensorId = SENSOR_TAG_PROXIMITY;
64094332d3Sopenharmony_ci    event.option = 0;
65094332d3Sopenharmony_ci    event.mode = SENSOR_WORK_MODE_ON_CHANGE;
66094332d3Sopenharmony_ci
67094332d3Sopenharmony_ci    if (rawData.stateFlag <= APDS9960_PROXIMITY_THRESHOLD) {
68094332d3Sopenharmony_ci        tmp = PROXIMITY_STATE_FAR;
69094332d3Sopenharmony_ci    } else {
70094332d3Sopenharmony_ci        tmp = PROXIMITY_STATE_NEAR;
71094332d3Sopenharmony_ci    }
72094332d3Sopenharmony_ci
73094332d3Sopenharmony_ci    event.dataLen = sizeof(tmp);
74094332d3Sopenharmony_ci    event.data = (uint8_t *)&tmp;
75094332d3Sopenharmony_ci    ret = ReportSensorEvent(&event);
76094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
77094332d3Sopenharmony_ci        HDF_LOGE("%s: APDS9960 report data failed", __func__);
78094332d3Sopenharmony_ci    }
79094332d3Sopenharmony_ci
80094332d3Sopenharmony_ci    return ret;
81094332d3Sopenharmony_ci}
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_cistatic int32_t InitApda9960(struct SensorCfgData *data)
84094332d3Sopenharmony_ci{
85094332d3Sopenharmony_ci    int32_t ret;
86094332d3Sopenharmony_ci
87094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
88094332d3Sopenharmony_ci    ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
89094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
90094332d3Sopenharmony_ci        HDF_LOGE("%s: APDS9960 sensor init config failed", __func__);
91094332d3Sopenharmony_ci        return HDF_FAILURE;
92094332d3Sopenharmony_ci    }
93094332d3Sopenharmony_ci    return HDF_SUCCESS;
94094332d3Sopenharmony_ci}
95094332d3Sopenharmony_ci
96094332d3Sopenharmony_cistatic int32_t InitProximityPreConfig(void)
97094332d3Sopenharmony_ci{
98094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_I2C6_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
99094332d3Sopenharmony_ci        HDF_LOGE("%s: Data write mux pin failed", __func__);
100094332d3Sopenharmony_ci        return HDF_FAILURE;
101094332d3Sopenharmony_ci    }
102094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_I2C6_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_I2C_REG_CFG) != HDF_SUCCESS) {
103094332d3Sopenharmony_ci        HDF_LOGE("%s: Clk write mux pin failed", __func__);
104094332d3Sopenharmony_ci        return HDF_FAILURE;
105094332d3Sopenharmony_ci    }
106094332d3Sopenharmony_ci
107094332d3Sopenharmony_ci    return HDF_SUCCESS;
108094332d3Sopenharmony_ci}
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_cistatic int32_t DispatchApds9960(struct HdfDeviceIoClient *client,
111094332d3Sopenharmony_ci    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
112094332d3Sopenharmony_ci{
113094332d3Sopenharmony_ci    (void)client;
114094332d3Sopenharmony_ci    (void)cmd;
115094332d3Sopenharmony_ci    (void)data;
116094332d3Sopenharmony_ci    (void)reply;
117094332d3Sopenharmony_ci
118094332d3Sopenharmony_ci    return HDF_SUCCESS;
119094332d3Sopenharmony_ci}
120094332d3Sopenharmony_ci
121094332d3Sopenharmony_cistatic int32_t Apds9960BindDriver(struct HdfDeviceObject *device)
122094332d3Sopenharmony_ci{
123094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
124094332d3Sopenharmony_ci
125094332d3Sopenharmony_ci    struct Apds9960DrvData *drvData = (struct Apds9960DrvData *)OsalMemCalloc(sizeof(*drvData));
126094332d3Sopenharmony_ci    if (drvData == NULL) {
127094332d3Sopenharmony_ci        HDF_LOGE("%s: Malloc Apds9960 drv data fail", __func__);
128094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
129094332d3Sopenharmony_ci    }
130094332d3Sopenharmony_ci
131094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchApds9960;
132094332d3Sopenharmony_ci    drvData->device = device;
133094332d3Sopenharmony_ci    device->service = &drvData->ioService;
134094332d3Sopenharmony_ci    g_apds9960DrvData = drvData;
135094332d3Sopenharmony_ci
136094332d3Sopenharmony_ci    return HDF_SUCCESS;
137094332d3Sopenharmony_ci}
138094332d3Sopenharmony_ci
139094332d3Sopenharmony_cistatic int32_t Apds996InitDriver(struct HdfDeviceObject *device)
140094332d3Sopenharmony_ci{
141094332d3Sopenharmony_ci    int32_t ret;
142094332d3Sopenharmony_ci    struct ProximityOpsCall ops;
143094332d3Sopenharmony_ci
144094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
145094332d3Sopenharmony_ci    struct Apds9960DrvData *drvData = (struct Apds9960DrvData *)device->service;
146094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
147094332d3Sopenharmony_ci
148094332d3Sopenharmony_ci    ret = InitProximityPreConfig();
149094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
150094332d3Sopenharmony_ci        HDF_LOGE("%s: Init  APDS9960 bus mux config", __func__);
151094332d3Sopenharmony_ci        return HDF_FAILURE;
152094332d3Sopenharmony_ci    }
153094332d3Sopenharmony_ci
154094332d3Sopenharmony_ci    drvData->sensorCfg = ProximityCreateCfgData(device->property);
155094332d3Sopenharmony_ci    if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
156094332d3Sopenharmony_ci        HDF_LOGD("%s: Creating proximitycfg failed because detection failed", __func__);
157094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
158094332d3Sopenharmony_ci    }
159094332d3Sopenharmony_ci
160094332d3Sopenharmony_ci    ops.Init = NULL;
161094332d3Sopenharmony_ci    ops.ReadData = ReadApds9960Data;
162094332d3Sopenharmony_ci    ret = ProximityRegisterChipOps(&ops);
163094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
164094332d3Sopenharmony_ci        HDF_LOGE("%s: Register APDS9960 proximity failed", __func__);
165094332d3Sopenharmony_ci        return HDF_FAILURE;
166094332d3Sopenharmony_ci    }
167094332d3Sopenharmony_ci
168094332d3Sopenharmony_ci    ret = InitApda9960(drvData->sensorCfg);
169094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
170094332d3Sopenharmony_ci        HDF_LOGE("%s: Init APDS9960 proximity failed", __func__);
171094332d3Sopenharmony_ci        return HDF_FAILURE;
172094332d3Sopenharmony_ci    }
173094332d3Sopenharmony_ci
174094332d3Sopenharmony_ci    return HDF_SUCCESS;
175094332d3Sopenharmony_ci}
176094332d3Sopenharmony_ci
177094332d3Sopenharmony_ci
178094332d3Sopenharmony_cistatic void Apds996ReleaseDriver(struct HdfDeviceObject *device)
179094332d3Sopenharmony_ci{
180094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(device);
181094332d3Sopenharmony_ci
182094332d3Sopenharmony_ci    struct Apds9960DrvData *drvData = (struct Apds9960DrvData *)device->service;
183094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(drvData);
184094332d3Sopenharmony_ci
185094332d3Sopenharmony_ci    if (drvData->sensorCfg != NULL) {
186094332d3Sopenharmony_ci        ProximityReleaseCfgData(drvData->sensorCfg);
187094332d3Sopenharmony_ci        drvData->sensorCfg = NULL;
188094332d3Sopenharmony_ci    }
189094332d3Sopenharmony_ci    OsalMemFree(drvData);
190094332d3Sopenharmony_ci}
191094332d3Sopenharmony_ci
192094332d3Sopenharmony_cistruct HdfDriverEntry g_proximityApds9960DevEntry = {
193094332d3Sopenharmony_ci    .moduleVersion = 1,
194094332d3Sopenharmony_ci    .moduleName = "HDF_SENSOR_PROXIMITY_APDS9960",
195094332d3Sopenharmony_ci    .Bind = Apds9960BindDriver,
196094332d3Sopenharmony_ci    .Init = Apds996InitDriver,
197094332d3Sopenharmony_ci    .Release = Apds996ReleaseDriver,
198094332d3Sopenharmony_ci};
199094332d3Sopenharmony_ci
200094332d3Sopenharmony_ciHDF_INIT(g_proximityApds9960DevEntry);