• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    hdf_sensor_hall
18 
19 static struct Ak8789DrvData *g_ak8789DrvData = NULL;
20 
Ak8789GetDrvData(void)21 static struct Ak8789DrvData *Ak8789GetDrvData(void)
22 {
23     return g_ak8789DrvData;
24 }
25 
26 /* IO config for int-pin and Gpio-pin */
27 #define SENSOR_HALL_DATA_REG_ADDR 0x114f0040
28 #define SENSOR_HALL_CLK_REG_ADDR  0x114f0044
29 #define SENSOR_HALL_REG_CFG       0x400
30 
InitHallPreConfig(void)31 static int32_t InitHallPreConfig(void)
32 {
33     if (SetSensorPinMux(SENSOR_HALL_DATA_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_HALL_REG_CFG) != HDF_SUCCESS) {
34         HDF_LOGE("%s: Data write mux pin failed", __func__);
35         return HDF_FAILURE;
36     }
37     if (SetSensorPinMux(SENSOR_HALL_CLK_REG_ADDR, SENSOR_ADDR_WIDTH_4_BYTE, SENSOR_HALL_REG_CFG) != HDF_SUCCESS) {
38         HDF_LOGE("%s: Clk write mux pin failed", __func__);
39         return HDF_FAILURE;
40     }
41     return HDF_SUCCESS;
42 }
43 
DispatchAK8789(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)44 static int32_t DispatchAK8789(struct HdfDeviceIoClient *client,
45     int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
46 {
47     (void)client;
48     (void)cmd;
49     (void)data;
50     (void)reply;
51 
52     return HDF_SUCCESS;
53 }
54 
Ak8789BindDriver(struct HdfDeviceObject * device)55 int32_t Ak8789BindDriver(struct HdfDeviceObject *device)
56 {
57     CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
58 
59     struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)OsalMemCalloc(sizeof(*drvData));
60     if (drvData == NULL) {
61         HDF_LOGE("%s: Malloc Ak8789 drv data fail", __func__);
62         return HDF_ERR_MALLOC_FAIL;
63     }
64 
65     drvData->ioService.Dispatch = DispatchAK8789;
66     drvData->device = device;
67     device->service = &drvData->ioService;
68     g_ak8789DrvData = drvData;
69 
70     return HDF_SUCCESS;
71 }
72 
AK8789InitDriver(struct HdfDeviceObject * device)73 int32_t AK8789InitDriver(struct HdfDeviceObject *device)
74 {
75     int32_t ret;
76     struct HallOpsCall ops;
77     CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
78     struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)device->service;
79     CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
80 
81     ret = InitHallPreConfig();
82     if (ret != HDF_SUCCESS) {
83         HDF_LOGE("%s: Init  AK8789 bus mux config", __func__);
84         return HDF_FAILURE;
85     }
86 
87     drvData->sensorCfg = HallCreateCfgData(device->property);
88     if (drvData->sensorCfg == NULL) {
89         return HDF_ERR_NOT_SUPPORT;
90     }
91 
92     ops.Init = NULL;
93     ops.ReadData = NULL;
94     ret = HallRegisterChipOps(&ops);
95     if (ret != HDF_SUCCESS) {
96         HDF_LOGE("%s: Register AK8789 hall failed", __func__);
97         return HDF_FAILURE;
98     }
99 
100     return HDF_SUCCESS;
101 }
102 
Ak8789ReleaseDriver(struct HdfDeviceObject * device)103 void Ak8789ReleaseDriver(struct HdfDeviceObject *device)
104 {
105     CHECK_NULL_PTR_RETURN(device);
106 
107     struct Ak8789DrvData *drvData = (struct Ak8789DrvData *)device->service;
108     CHECK_NULL_PTR_RETURN(drvData);
109 
110     HallReleaseCfgData(drvData->sensorCfg);
111     drvData->sensorCfg = NULL;
112     OsalMemFree(drvData);
113 }
114 
115 struct HdfDriverEntry g_hallAk8789DevEntry = {
116     .moduleVersion = 1,
117     .moduleName = "HDF_SENSOR_HALL_AK8789",
118     .Bind = Ak8789BindDriver,
119     .Init = AK8789InitDriver,
120     .Release = Ak8789ReleaseDriver,
121 };
122 
123 HDF_INIT(g_hallAk8789DevEntry);