1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "hall_ak8789.h"
10 #include "osal_irq.h"
11 #include "osal_mem.h"
12 #include "osal_time.h"
13 #include "sensor_config_controller.h"
14 #include "sensor_device_manager.h"
15 #include "sensor_hall_driver.h"
16 
17 #define HDF_LOG_TAG    khdf_sensor_hall_driver
18 
19 static struct Ak8789DrvData *g_ak8789DrvData = NULL;
20 
21 /* IO config for int-pin and Gpio-pin */
22 #define SENSOR_HALL_DATA_REG_ADDR 0x114f0040
23 #define SENSOR_HALL_CLK_REG_ADDR  0x114f0044
24 #define SENSOR_HALL_REG_CFG       0x400
25 
InitHallPreConfig(void)26 static int32_t InitHallPreConfig(void)
27 {
28     if (SetSensorPinMux(SENSOR_HALL_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_HALL_REG_CFG) != HDF_SUCCESS) {
29         HDF_LOGE("%s: Data write mux pin failed", __func__);
30         return HDF_FAILURE;
31     }
32     if (SetSensorPinMux(SENSOR_HALL_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_HALL_REG_CFG) != HDF_SUCCESS) {
33         HDF_LOGE("%s: Clk write mux pin failed", __func__);
34         return HDF_FAILURE;
35     }
36     return HDF_SUCCESS;
37 }
38 
DispatchAK8789(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)39 static int32_t DispatchAK8789(struct HdfDeviceIoClient *client,
40     int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
41 {
42     (void)client;
43     (void)cmd;
44     (void)data;
45     (void)reply;
46 
47     return HDF_SUCCESS;
48 }
49 
Ak8789BindDriver(struct HdfDeviceObject * device)50 static int32_t Ak8789BindDriver(struct HdfDeviceObject *device)
51 {
52     CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
53 
54     struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)OsalMemCalloc(sizeof(*drvData));
55     if (drvData == NULL) {
56         HDF_LOGE("%s: Malloc Ak8789 drv data fail", __func__);
57         return HDF_ERR_MALLOC_FAIL;
58     }
59 
60     drvData->ioService.Dispatch = DispatchAK8789;
61     drvData->device = device;
62     device->service = &drvData->ioService;
63     g_ak8789DrvData = drvData;
64 
65     return HDF_SUCCESS;
66 }
67 
AK8789InitDriver(struct HdfDeviceObject * device)68 static int32_t AK8789InitDriver(struct HdfDeviceObject *device)
69 {
70     int32_t ret;
71     struct HallOpsCall ops;
72     CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
73     struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)device->service;
74     CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
75 
76     ret = InitHallPreConfig();
77     if (ret != HDF_SUCCESS) {
78         HDF_LOGE("%s: Init  AK8789 bus mux config", __func__);
79         return HDF_FAILURE;
80     }
81 
82     drvData->sensorCfg = HallCreateCfgData(device->property);
83     if (drvData->sensorCfg == NULL) {
84         return HDF_ERR_NOT_SUPPORT;
85     }
86 
87     ops.Init = NULL;
88     ops.ReadData = NULL;
89     ret = HallRegisterChipOps(&ops);
90     if (ret != HDF_SUCCESS) {
91         HDF_LOGE("%s: Register AK8789 hall failed", __func__);
92         return HDF_FAILURE;
93     }
94 
95     return HDF_SUCCESS;
96 }
97 
Ak8789ReleaseDriver(struct HdfDeviceObject * device)98 static void Ak8789ReleaseDriver(struct HdfDeviceObject *device)
99 {
100     CHECK_NULL_PTR_RETURN(device);
101 
102     struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)device->service;
103     CHECK_NULL_PTR_RETURN(drvData);
104 
105     HallReleaseCfgData(drvData->sensorCfg);
106     drvData->sensorCfg = NULL;
107     OsalMemFree(drvData);
108 }
109 
110 struct HdfDriverEntry g_hallAk8789DevEntry = {
111     .moduleVersion = 1,
112     .moduleName = "HDF_SENSOR_HALL_AK8789",
113     .Bind = Ak8789BindDriver,
114     .Init = AK8789InitDriver,
115     .Release = Ak8789ReleaseDriver,
116 };
117 
118 HDF_INIT(g_hallAk8789DevEntry);