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 "hall_ak8789.h"
10094332d3Sopenharmony_ci#include "osal_irq.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_hall_driver.h"
16094332d3Sopenharmony_ci
17094332d3Sopenharmony_ci#define HDF_LOG_TAG    khdf_sensor_hall_driver
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_cistatic struct Ak8789DrvData *g_ak8789DrvData = NULL;
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_ci/* IO config for int-pin and Gpio-pin */
22094332d3Sopenharmony_ci#define SENSOR_HALL_DATA_REG_ADDR 0x114f0040
23094332d3Sopenharmony_ci#define SENSOR_HALL_CLK_REG_ADDR  0x114f0044
24094332d3Sopenharmony_ci#define SENSOR_HALL_REG_CFG       0x400
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_cistatic int32_t InitHallPreConfig(void)
27094332d3Sopenharmony_ci{
28094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_HALL_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_HALL_REG_CFG) != HDF_SUCCESS) {
29094332d3Sopenharmony_ci        HDF_LOGE("%s: Data write mux pin failed", __func__);
30094332d3Sopenharmony_ci        return HDF_FAILURE;
31094332d3Sopenharmony_ci    }
32094332d3Sopenharmony_ci    if (SetSensorPinMux(SENSOR_HALL_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_HALL_REG_CFG) != HDF_SUCCESS) {
33094332d3Sopenharmony_ci        HDF_LOGE("%s: Clk write mux pin failed", __func__);
34094332d3Sopenharmony_ci        return HDF_FAILURE;
35094332d3Sopenharmony_ci    }
36094332d3Sopenharmony_ci    return HDF_SUCCESS;
37094332d3Sopenharmony_ci}
38094332d3Sopenharmony_ci
39094332d3Sopenharmony_cistatic int32_t DispatchAK8789(struct HdfDeviceIoClient *client,
40094332d3Sopenharmony_ci    int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
41094332d3Sopenharmony_ci{
42094332d3Sopenharmony_ci    (void)client;
43094332d3Sopenharmony_ci    (void)cmd;
44094332d3Sopenharmony_ci    (void)data;
45094332d3Sopenharmony_ci    (void)reply;
46094332d3Sopenharmony_ci
47094332d3Sopenharmony_ci    return HDF_SUCCESS;
48094332d3Sopenharmony_ci}
49094332d3Sopenharmony_ci
50094332d3Sopenharmony_cistatic int32_t Ak8789BindDriver(struct HdfDeviceObject *device)
51094332d3Sopenharmony_ci{
52094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
53094332d3Sopenharmony_ci
54094332d3Sopenharmony_ci    struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)OsalMemCalloc(sizeof(*drvData));
55094332d3Sopenharmony_ci    if (drvData == NULL) {
56094332d3Sopenharmony_ci        HDF_LOGE("%s: Malloc Ak8789 drv data fail", __func__);
57094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
58094332d3Sopenharmony_ci    }
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchAK8789;
61094332d3Sopenharmony_ci    drvData->device = device;
62094332d3Sopenharmony_ci    device->service = &drvData->ioService;
63094332d3Sopenharmony_ci    g_ak8789DrvData = drvData;
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ci    return HDF_SUCCESS;
66094332d3Sopenharmony_ci}
67094332d3Sopenharmony_ci
68094332d3Sopenharmony_cistatic int32_t AK8789InitDriver(struct HdfDeviceObject *device)
69094332d3Sopenharmony_ci{
70094332d3Sopenharmony_ci    int32_t ret;
71094332d3Sopenharmony_ci    struct HallOpsCall ops;
72094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
73094332d3Sopenharmony_ci    struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)device->service;
74094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
75094332d3Sopenharmony_ci
76094332d3Sopenharmony_ci    ret = InitHallPreConfig();
77094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
78094332d3Sopenharmony_ci        HDF_LOGE("%s: Init  AK8789 bus mux config", __func__);
79094332d3Sopenharmony_ci        return HDF_FAILURE;
80094332d3Sopenharmony_ci    }
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ci    drvData->sensorCfg = HallCreateCfgData(device->property);
83094332d3Sopenharmony_ci    if (drvData->sensorCfg == NULL) {
84094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
85094332d3Sopenharmony_ci    }
86094332d3Sopenharmony_ci
87094332d3Sopenharmony_ci    ops.Init = NULL;
88094332d3Sopenharmony_ci    ops.ReadData = NULL;
89094332d3Sopenharmony_ci    ret = HallRegisterChipOps(&ops);
90094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
91094332d3Sopenharmony_ci        HDF_LOGE("%s: Register AK8789 hall failed", __func__);
92094332d3Sopenharmony_ci        return HDF_FAILURE;
93094332d3Sopenharmony_ci    }
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_ci    return HDF_SUCCESS;
96094332d3Sopenharmony_ci}
97094332d3Sopenharmony_ci
98094332d3Sopenharmony_cistatic void Ak8789ReleaseDriver(struct HdfDeviceObject *device)
99094332d3Sopenharmony_ci{
100094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(device);
101094332d3Sopenharmony_ci
102094332d3Sopenharmony_ci    struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)device->service;
103094332d3Sopenharmony_ci    CHECK_NULL_PTR_RETURN(drvData);
104094332d3Sopenharmony_ci
105094332d3Sopenharmony_ci    HallReleaseCfgData(drvData->sensorCfg);
106094332d3Sopenharmony_ci    drvData->sensorCfg = NULL;
107094332d3Sopenharmony_ci    OsalMemFree(drvData);
108094332d3Sopenharmony_ci}
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_cistruct HdfDriverEntry g_hallAk8789DevEntry = {
111094332d3Sopenharmony_ci    .moduleVersion = 1,
112094332d3Sopenharmony_ci    .moduleName = "HDF_SENSOR_HALL_AK8789",
113094332d3Sopenharmony_ci    .Bind = Ak8789BindDriver,
114094332d3Sopenharmony_ci    .Init = AK8789InitDriver,
115094332d3Sopenharmony_ci    .Release = Ak8789ReleaseDriver,
116094332d3Sopenharmony_ci};
117094332d3Sopenharmony_ci
118094332d3Sopenharmony_ciHDF_INIT(g_hallAk8789DevEntry);