• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "pwm_test.h"
10 #include "device_resource_if.h"
11 #include "hdf_base.h"
12 #include "hdf_device_desc.h"
13 #include "hdf_log.h"
14 
15 #define HDF_LOG_TAG pwm_driver_test_c
16 
17 static struct PwmTestConfig g_config;
18 
PwmTestDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)19 static int32_t PwmTestDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
20 {
21     if (cmd == 0) {
22         if (reply == NULL) {
23             HDF_LOGE("%s: reply is null!", __func__);
24             return HDF_ERR_INVALID_PARAM;
25         }
26         if (!HdfSbufWriteBuffer(reply, &g_config, sizeof(g_config))) {
27             HDF_LOGE("%s: write reply failed", __func__);
28             return HDF_ERR_IO;
29         }
30     } else {
31         HDF_LOGE("%s: cmd no support\n", __func__);
32         return HDF_ERR_NOT_SUPPORT;
33     }
34 
35     return HDF_SUCCESS;
36 }
37 
PwmTestReadConfig(struct PwmTestConfig * config,const struct DeviceResourceNode * node)38 static int32_t PwmTestReadConfig(struct PwmTestConfig *config, const struct DeviceResourceNode *node)
39 {
40     int32_t ret;
41     struct DeviceResourceIface *drsOps = NULL;
42     uint32_t temp;
43 
44     drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
45     if (drsOps == NULL) {
46         HDF_LOGE("%s: drsOps is null.", __func__);
47         return HDF_FAILURE;
48     }
49 
50     if (drsOps->GetUint32 == NULL) {
51         HDF_LOGE("%s: GetUint32 not support.", __func__);
52         return HDF_ERR_NOT_SUPPORT;
53     }
54 
55     ret = drsOps->GetUint32(node, "num", &(config->num), 0);
56     if (ret != HDF_SUCCESS) {
57         HDF_LOGE("%s: read num fail.", __func__);
58         return ret;
59     }
60 
61     ret = drsOps->GetUint32(node, "period", &(config->cfg.period), 0);
62     if (ret != HDF_SUCCESS) {
63         HDF_LOGE("%s: read period fail.", __func__);
64         return ret;
65     }
66 
67     ret = drsOps->GetUint32(node, "duty", &(config->cfg.duty), 0);
68     if (ret != HDF_SUCCESS) {
69         HDF_LOGE("%s: read duty fail.", __func__);
70         return ret;
71     }
72 
73     ret = drsOps->GetUint32(node, "polarity", &(temp), 0);
74     if (ret != HDF_SUCCESS) {
75         HDF_LOGE("%s: read polarity fail.", __func__);
76         return ret;
77     }
78     config->cfg.polarity = temp;
79 
80     ret = drsOps->GetUint32(node, "status", &(temp), 0);
81     if (ret != HDF_SUCCESS) {
82         HDF_LOGE("%s: read status fail", __func__);
83         return ret;
84     }
85     config->cfg.status = temp;
86 
87     return HDF_SUCCESS;
88 }
89 
PwmTestBind(struct HdfDeviceObject * device)90 static int32_t PwmTestBind(struct HdfDeviceObject *device)
91 {
92     int32_t ret;
93     static struct IDeviceIoService service;
94 
95     if (device == NULL || device->property == NULL) {
96         HDF_LOGE("%s: device or config is null!", __func__);
97         return HDF_ERR_INVALID_OBJECT;
98     }
99 
100     ret = PwmTestReadConfig(&g_config, device->property);
101     if (ret != HDF_SUCCESS) {
102         HDF_LOGE("%s: read config failed", __func__);
103         return ret;
104     }
105 
106     service.Dispatch = PwmTestDispatch;
107     device->service = &service;
108     return HDF_SUCCESS;
109 }
110 
PwmTestInit(struct HdfDeviceObject * device)111 static int32_t PwmTestInit(struct HdfDeviceObject *device)
112 {
113     (void)device;
114     return HDF_SUCCESS;
115 }
116 
PwmTestRelease(struct HdfDeviceObject * device)117 static void PwmTestRelease(struct HdfDeviceObject *device)
118 {
119     if (device != NULL) {
120         device->service = NULL;
121     }
122     HDF_LOGI("%s: Done!", __func__);
123     return;
124 }
125 
126 struct HdfDriverEntry g_pwmTestEntry = {
127     .moduleVersion = 1,
128     .Bind = PwmTestBind,
129     .Init = PwmTestInit,
130     .Release = PwmTestRelease,
131     .moduleName = "PLATFORM_PWM_TEST",
132 };
133 HDF_INIT(g_pwmTestEntry);