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 "vibrator_linear_driver.h"
10094332d3Sopenharmony_ci#include <securec.h>
11094332d3Sopenharmony_ci#include "device_resource_if.h"
12094332d3Sopenharmony_ci#include "gpio_if.h"
13094332d3Sopenharmony_ci#include "hdf_base.h"
14094332d3Sopenharmony_ci#include "hdf_device_desc.h"
15094332d3Sopenharmony_ci#include "osal_mem.h"
16094332d3Sopenharmony_ci#include "vibrator_driver.h"
17094332d3Sopenharmony_ci#include "vibrator_parser.h"
18094332d3Sopenharmony_ci#include "vibrator_driver_type.h"
19094332d3Sopenharmony_ci
20094332d3Sopenharmony_ci#define HDF_LOG_TAG    khdf_vibrator_driver
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_cistruct VibratorLinearDriverData *g_linearVibratorData = NULL;
23094332d3Sopenharmony_cistatic struct VibratorLinearDriverData *GetLinearVibratorData(void)
24094332d3Sopenharmony_ci{
25094332d3Sopenharmony_ci    return g_linearVibratorData;
26094332d3Sopenharmony_ci}
27094332d3Sopenharmony_ci
28094332d3Sopenharmony_cistatic int32_t StartLinearVibrator(void)
29094332d3Sopenharmony_ci{
30094332d3Sopenharmony_ci    int32_t ret;
31094332d3Sopenharmony_ci    struct VibratorLinearDriverData *drvData = GetLinearVibratorData();
32094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
33094332d3Sopenharmony_ci
34094332d3Sopenharmony_ci    if (drvData->linearCfgData->vibratorBus.busType != VIBRATOR_BUS_GPIO) {
35094332d3Sopenharmony_ci        HDF_LOGE("%s: vibrator bus type not gpio", __func__);
36094332d3Sopenharmony_ci        return HDF_FAILURE;
37094332d3Sopenharmony_ci    }
38094332d3Sopenharmony_ci
39094332d3Sopenharmony_ci    ret = GpioWrite(drvData->linearCfgData->vibratorBus.GpioNum, GPIO_VAL_HIGH);
40094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
41094332d3Sopenharmony_ci        HDF_LOGE("%s: pull gpio%d to %d level failed", __func__,
42094332d3Sopenharmony_ci            drvData->linearCfgData->vibratorBus.GpioNum, GPIO_VAL_HIGH);
43094332d3Sopenharmony_ci        return ret;
44094332d3Sopenharmony_ci    }
45094332d3Sopenharmony_ci    return HDF_SUCCESS;
46094332d3Sopenharmony_ci}
47094332d3Sopenharmony_ci
48094332d3Sopenharmony_cistatic int32_t StartEffectLinearVibrator(uint32_t effectType)
49094332d3Sopenharmony_ci{
50094332d3Sopenharmony_ci    (void)effectType;
51094332d3Sopenharmony_ci    HDF_LOGE("%s: vibrator set built-in effect no support!", __func__);
52094332d3Sopenharmony_ci    return HDF_SUCCESS;
53094332d3Sopenharmony_ci}
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_cistatic int32_t StopLinearVibrator(void)
56094332d3Sopenharmony_ci{
57094332d3Sopenharmony_ci    int32_t ret;
58094332d3Sopenharmony_ci    struct VibratorLinearDriverData *drvData = GetLinearVibratorData();
59094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_ci    if (drvData->linearCfgData->vibratorBus.busType != VIBRATOR_BUS_GPIO) {
62094332d3Sopenharmony_ci        HDF_LOGE("%s: vibrator bus type not gpio", __func__);
63094332d3Sopenharmony_ci        return HDF_FAILURE;
64094332d3Sopenharmony_ci    }
65094332d3Sopenharmony_ci
66094332d3Sopenharmony_ci    ret = GpioWrite(drvData->linearCfgData->vibratorBus.GpioNum, GPIO_VAL_LOW);
67094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
68094332d3Sopenharmony_ci        HDF_LOGE("%s: pull gpio%d to %d level failed", __func__,
69094332d3Sopenharmony_ci            drvData->linearCfgData->vibratorBus.GpioNum, GPIO_VAL_LOW);
70094332d3Sopenharmony_ci        return ret;
71094332d3Sopenharmony_ci    }
72094332d3Sopenharmony_ci    return HDF_SUCCESS;
73094332d3Sopenharmony_ci}
74094332d3Sopenharmony_ci
75094332d3Sopenharmony_cistatic int32_t DispatchLinearVibrator(struct HdfDeviceIoClient *client,
76094332d3Sopenharmony_ci    int32_t cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
77094332d3Sopenharmony_ci{
78094332d3Sopenharmony_ci    (void)client;
79094332d3Sopenharmony_ci    (void)cmd;
80094332d3Sopenharmony_ci    (void)data;
81094332d3Sopenharmony_ci    (void)reply;
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_ci    return HDF_SUCCESS;
84094332d3Sopenharmony_ci}
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_cistatic int32_t BindLinearVibratorDriver(struct HdfDeviceObject *device)
87094332d3Sopenharmony_ci{
88094332d3Sopenharmony_ci    struct VibratorLinearDriverData *drvData = NULL;
89094332d3Sopenharmony_ci
90094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(device, HDF_FAILURE);
91094332d3Sopenharmony_ci
92094332d3Sopenharmony_ci    drvData = (struct VibratorLinearDriverData *)OsalMemCalloc(sizeof(*drvData));
93094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_MALLOC_FAIL);
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_ci    drvData->ioService.Dispatch = DispatchLinearVibrator;
96094332d3Sopenharmony_ci    drvData->device = device;
97094332d3Sopenharmony_ci    device->service = &drvData->ioService;
98094332d3Sopenharmony_ci    g_linearVibratorData = drvData;
99094332d3Sopenharmony_ci
100094332d3Sopenharmony_ci    return HDF_SUCCESS;
101094332d3Sopenharmony_ci}
102094332d3Sopenharmony_ci
103094332d3Sopenharmony_cistatic int32_t InitLinearVibratorDriver(struct HdfDeviceObject *device)
104094332d3Sopenharmony_ci{
105094332d3Sopenharmony_ci    static struct VibratorOps ops;
106094332d3Sopenharmony_ci    struct VibratorLinearDriverData *drvData = NULL;
107094332d3Sopenharmony_ci
108094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(device, HDF_FAILURE);
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_ci    drvData = (struct VibratorLinearDriverData *)device->service;
111094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE);
112094332d3Sopenharmony_ci
113094332d3Sopenharmony_ci    ops.Start = StartLinearVibrator;
114094332d3Sopenharmony_ci    ops.StartEffect = StartEffectLinearVibrator;
115094332d3Sopenharmony_ci    ops.Stop = StopLinearVibrator;
116094332d3Sopenharmony_ci    ops.SetParameter = NULL;
117094332d3Sopenharmony_ci
118094332d3Sopenharmony_ci    if (RegisterVibratorOps(&ops) != HDF_SUCCESS) {
119094332d3Sopenharmony_ci        HDF_LOGE("%s: register vibrator ops fail", __func__);
120094332d3Sopenharmony_ci        return HDF_FAILURE;
121094332d3Sopenharmony_ci    }
122094332d3Sopenharmony_ci
123094332d3Sopenharmony_ci    drvData->linearCfgData = (struct VibratorCfgData *)OsalMemCalloc(sizeof(*drvData->linearCfgData));
124094332d3Sopenharmony_ci    CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData->linearCfgData, HDF_ERR_MALLOC_FAIL);
125094332d3Sopenharmony_ci
126094332d3Sopenharmony_ci    if (GetVibratorBaseConfigData(device->property, drvData->linearCfgData) != HDF_SUCCESS) {
127094332d3Sopenharmony_ci        HDF_LOGE("%s: parser vibrator cfg fail", __func__);
128094332d3Sopenharmony_ci        return HDF_FAILURE;
129094332d3Sopenharmony_ci    }
130094332d3Sopenharmony_ci
131094332d3Sopenharmony_ci    if (RegisterVibratorInfo(&drvData->linearCfgData->vibratorInfo) != HDF_SUCCESS) {
132094332d3Sopenharmony_ci        HDF_LOGE("%s: register vibrator info fail", __func__);
133094332d3Sopenharmony_ci        return HDF_FAILURE;
134094332d3Sopenharmony_ci    }
135094332d3Sopenharmony_ci
136094332d3Sopenharmony_ci    if (GpioSetDir(drvData->linearCfgData->vibratorBus.GpioNum, GPIO_DIR_OUT) != HDF_SUCCESS) {
137094332d3Sopenharmony_ci        HDF_LOGE("%s: set vibrator gpio fail", __func__);
138094332d3Sopenharmony_ci        return HDF_FAILURE;
139094332d3Sopenharmony_ci    }
140094332d3Sopenharmony_ci    return HDF_SUCCESS;
141094332d3Sopenharmony_ci}
142094332d3Sopenharmony_ci
143094332d3Sopenharmony_cistatic void ReleaseLinearVibratorDriver(struct HdfDeviceObject *device)
144094332d3Sopenharmony_ci{
145094332d3Sopenharmony_ci    struct VibratorLinearDriverData *drvData = NULL;
146094332d3Sopenharmony_ci
147094332d3Sopenharmony_ci    if (device == NULL) {
148094332d3Sopenharmony_ci        HDF_LOGE("%s: Device is null", __func__);
149094332d3Sopenharmony_ci        return;
150094332d3Sopenharmony_ci    }
151094332d3Sopenharmony_ci    drvData = (struct VibratorLinearDriverData *)device->service;
152094332d3Sopenharmony_ci    if (drvData == NULL) {
153094332d3Sopenharmony_ci        HDF_LOGE("%s: DrvData pointer is null", __func__);
154094332d3Sopenharmony_ci        return;
155094332d3Sopenharmony_ci    }
156094332d3Sopenharmony_ci
157094332d3Sopenharmony_ci    OsalMemFree(drvData->linearCfgData);
158094332d3Sopenharmony_ci    OsalMemFree(drvData);
159094332d3Sopenharmony_ci    g_linearVibratorData = NULL;
160094332d3Sopenharmony_ci}
161094332d3Sopenharmony_ci
162094332d3Sopenharmony_cistruct HdfDriverEntry g_linearVibratorDriverEntry = {
163094332d3Sopenharmony_ci    .moduleVersion = 1,
164094332d3Sopenharmony_ci    .moduleName = "HDF_LINEAR_VIBRATOR",
165094332d3Sopenharmony_ci    .Bind = BindLinearVibratorDriver,
166094332d3Sopenharmony_ci    .Init = InitLinearVibratorDriver,
167094332d3Sopenharmony_ci    .Release = ReleaseLinearVibratorDriver,
168094332d3Sopenharmony_ci};
169094332d3Sopenharmony_ci
170094332d3Sopenharmony_ciHDF_INIT(g_linearVibratorDriverEntry);
171