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 "vibrator_linear_driver.h"
10 #include <securec.h>
11 #include "device_resource_if.h"
12 #include "gpio_if.h"
13 #include "hdf_base.h"
14 #include "hdf_device_desc.h"
15 #include "osal_mem.h"
16 #include "vibrator_driver.h"
17 #include "vibrator_parser.h"
18 #include "vibrator_driver_type.h"
19
20 #define HDF_LOG_TAG khdf_vibrator_driver
21
22 struct VibratorLinearDriverData *g_linearVibratorData = NULL;
GetLinearVibratorData(void)23 static struct VibratorLinearDriverData *GetLinearVibratorData(void)
24 {
25 return g_linearVibratorData;
26 }
27
StartLinearVibrator(void)28 static int32_t StartLinearVibrator(void)
29 {
30 int32_t ret;
31 struct VibratorLinearDriverData *drvData = GetLinearVibratorData();
32 CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
33
34 if (drvData->linearCfgData->vibratorBus.busType != VIBRATOR_BUS_GPIO) {
35 HDF_LOGE("%s: vibrator bus type not gpio", __func__);
36 return HDF_FAILURE;
37 }
38
39 ret = GpioWrite(drvData->linearCfgData->vibratorBus.GpioNum, GPIO_VAL_HIGH);
40 if (ret != HDF_SUCCESS) {
41 HDF_LOGE("%s: pull gpio%d to %d level failed", __func__,
42 drvData->linearCfgData->vibratorBus.GpioNum, GPIO_VAL_HIGH);
43 return ret;
44 }
45 return HDF_SUCCESS;
46 }
47
StartEffectLinearVibrator(uint32_t effectType)48 static int32_t StartEffectLinearVibrator(uint32_t effectType)
49 {
50 (void)effectType;
51 HDF_LOGE("%s: vibrator set built-in effect no support!", __func__);
52 return HDF_SUCCESS;
53 }
54
StopLinearVibrator(void)55 static int32_t StopLinearVibrator(void)
56 {
57 int32_t ret;
58 struct VibratorLinearDriverData *drvData = GetLinearVibratorData();
59 CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
60
61 if (drvData->linearCfgData->vibratorBus.busType != VIBRATOR_BUS_GPIO) {
62 HDF_LOGE("%s: vibrator bus type not gpio", __func__);
63 return HDF_FAILURE;
64 }
65
66 ret = GpioWrite(drvData->linearCfgData->vibratorBus.GpioNum, GPIO_VAL_LOW);
67 if (ret != HDF_SUCCESS) {
68 HDF_LOGE("%s: pull gpio%d to %d level failed", __func__,
69 drvData->linearCfgData->vibratorBus.GpioNum, GPIO_VAL_LOW);
70 return ret;
71 }
72 return HDF_SUCCESS;
73 }
74
DispatchLinearVibrator(struct HdfDeviceIoClient * client,int32_t cmd,struct HdfSBuf * data,struct HdfSBuf * reply)75 static int32_t DispatchLinearVibrator(struct HdfDeviceIoClient *client,
76 int32_t cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
77 {
78 (void)client;
79 (void)cmd;
80 (void)data;
81 (void)reply;
82
83 return HDF_SUCCESS;
84 }
85
BindLinearVibratorDriver(struct HdfDeviceObject * device)86 int32_t BindLinearVibratorDriver(struct HdfDeviceObject *device)
87 {
88 struct VibratorLinearDriverData *drvData = NULL;
89
90 CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(device, HDF_FAILURE);
91
92 drvData = (struct VibratorLinearDriverData *)OsalMemCalloc(sizeof(*drvData));
93 CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_MALLOC_FAIL);
94
95 drvData->ioService.Dispatch = DispatchLinearVibrator;
96 drvData->device = device;
97 device->service = &drvData->ioService;
98 g_linearVibratorData = drvData;
99
100 return HDF_SUCCESS;
101 }
102
InitLinearVibratorDriver(struct HdfDeviceObject * device)103 int32_t InitLinearVibratorDriver(struct HdfDeviceObject *device)
104 {
105 static struct VibratorOps ops;
106 struct VibratorLinearDriverData *drvData = NULL;
107
108 CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(device, HDF_FAILURE);
109
110 drvData = (struct VibratorLinearDriverData *)device->service;
111 CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
112
113 ops.Start = StartLinearVibrator;
114 ops.StartEffect = StartEffectLinearVibrator;
115 ops.Stop = StopLinearVibrator;
116 ops.SetParameter = NULL;
117
118 if (RegisterVibratorOps(&ops) != HDF_SUCCESS) {
119 HDF_LOGE("%s: register vibrator ops fail", __func__);
120 return HDF_FAILURE;
121 }
122
123 drvData->linearCfgData = (struct VibratorCfgData *)OsalMemCalloc(sizeof(*drvData->linearCfgData));
124 CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData->linearCfgData, HDF_ERR_MALLOC_FAIL);
125
126 if (GetVibratorBaseConfigData(device->property, drvData->linearCfgData) != HDF_SUCCESS) {
127 HDF_LOGE("%s: parser vibrator cfg fail", __func__);
128 return HDF_FAILURE;
129 }
130
131 if (RegisterVibratorInfo(&drvData->linearCfgData->vibratorInfo) != HDF_SUCCESS) {
132 HDF_LOGE("%s: register vibrator info fail", __func__);
133 return HDF_FAILURE;
134 }
135
136 if (GpioSetDir(drvData->linearCfgData->vibratorBus.GpioNum, GPIO_DIR_OUT) != HDF_SUCCESS) {
137 HDF_LOGE("%s: set vibrator gpio fail", __func__);
138 return HDF_FAILURE;
139 }
140 return HDF_SUCCESS;
141 }
142
ReleaseLinearVibratorDriver(struct HdfDeviceObject * device)143 void ReleaseLinearVibratorDriver(struct HdfDeviceObject *device)
144 {
145 struct VibratorLinearDriverData *drvData = NULL;
146
147 if (device == NULL) {
148 HDF_LOGE("%s: Device is null", __func__);
149 return;
150 }
151 drvData = (struct VibratorLinearDriverData *)device->service;
152 if (drvData == NULL) {
153 HDF_LOGE("%s: DrvData pointer is null", __func__);
154 return;
155 }
156
157 OsalMemFree(drvData->linearCfgData);
158 OsalMemFree(drvData);
159 g_linearVibratorData = NULL;
160 }
161
162 struct HdfDriverEntry g_linearVibratorDriverEntry = {
163 .moduleVersion = 1,
164 .moduleName = "HDF_LINEAR_VIBRATOR",
165 .Bind = BindLinearVibratorDriver,
166 .Init = InitLinearVibratorDriver,
167 .Release = ReleaseLinearVibratorDriver,
168 };
169
170 HDF_INIT(g_linearVibratorDriverEntry);
171