• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "hdf_log.h"
10 #include "osal_mem.h"
11 #include "pwm_core.h"
12 #include "securec.h"
13 
14 #define HDF_LOG_TAG pwm_if
15 #define PWM_NAME_LEN 32
16 
PwmGetDevByNum(uint32_t num)17 static void *PwmGetDevByNum(uint32_t num)
18 {
19     int32_t ret;
20     char *name = NULL;
21     void *pwm = NULL;
22 
23     name = (char *)OsalMemCalloc(PWM_NAME_LEN + 1);
24     if (name == NULL) {
25         return NULL;
26     }
27     ret = snprintf_s(name, PWM_NAME_LEN + 1, PWM_NAME_LEN, "HDF_PLATFORM_PWM_%u", num);
28     if (ret < 0) {
29         HDF_LOGE("PwmGetDevByNum: snprintf_s fail!");
30         OsalMemFree(name);
31         return NULL;
32     }
33     pwm = (void *)DevSvcManagerClntGetService(name);
34     if (pwm == NULL) {
35         HDF_LOGE("PwmGetDevByNum: get service fail!");
36         OsalMemFree(name);
37         return NULL;
38     }
39     OsalMemFree(name);
40     return pwm;
41 }
42 
PwmOpen(uint32_t num)43 DevHandle PwmOpen(uint32_t num)
44 {
45     int32_t ret;
46     void *pwm = PwmGetDevByNum(num);
47 
48     if (pwm == NULL) {
49         HDF_LOGE("PwmOpen: pwm is null!");
50         return NULL;
51     }
52 
53     ret = PwmDeviceGet((struct PwmDev *)pwm);
54     if (ret != HDF_SUCCESS) {
55         HDF_LOGE("PwmOpen: PwmDeviceGet fail, ret: %d!", ret);
56         return NULL;
57     }
58     return (DevHandle)pwm;
59 }
60 
PwmClose(DevHandle handle)61 void PwmClose(DevHandle handle)
62 {
63     if (handle == NULL) {
64         HDF_LOGE("PwmClose: handle is null!");
65         return;
66     }
67 
68     if (PwmDevicePut((struct PwmDev *)handle) != HDF_SUCCESS) {
69         HDF_LOGE("PwmClose: PwmDevicePut fail!");
70         return;
71     }
72 }
73 
PwmSetPeriod(DevHandle handle,uint32_t period)74 int32_t PwmSetPeriod(DevHandle handle, uint32_t period)
75 {
76     struct PwmConfig config;
77     uint32_t curValue;
78     int32_t ret;
79 
80     if (PwmGetConfig(handle, &config) != HDF_SUCCESS) {
81         HDF_LOGE("PwmSetPeriod: PwmGetConfig fail!");
82         return HDF_FAILURE;
83     }
84     curValue = config.period;
85     config.period = period;
86     ret = PwmSetConfig(handle, &config);
87     if (ret == HDF_SUCCESS) {
88         HDF_LOGI("PwmSetPeriod: success. period: %d -> %d!", curValue, config.period);
89     }
90     return ret;
91 }
92 
PwmSetDuty(DevHandle handle,uint32_t duty)93 int32_t PwmSetDuty(DevHandle handle, uint32_t duty)
94 {
95     struct PwmConfig config;
96     uint32_t curValue;
97     int32_t ret;
98 
99     if (PwmGetConfig(handle, &config) != HDF_SUCCESS) {
100         HDF_LOGE("PwmSetDuty: PwmGetConfig fail!");
101         return HDF_FAILURE;
102     }
103     curValue = config.duty;
104     config.duty = duty;
105     ret = PwmSetConfig(handle, &config);
106     if (ret == HDF_SUCCESS) {
107         HDF_LOGI("PwmSetDuty: success. duty: %u -> %u!", curValue, config.duty);
108     }
109     return ret;
110 }
111 
PwmSetPolarity(DevHandle handle,uint8_t polarity)112 int32_t PwmSetPolarity(DevHandle handle, uint8_t polarity)
113 {
114     struct PwmConfig config;
115     uint32_t curValue;
116     int32_t ret;
117 
118     if (PwmGetConfig(handle, &config) != HDF_SUCCESS) {
119         HDF_LOGE("PwmSetPolarity: PwmGetConfig fail!");
120         return HDF_FAILURE;
121     }
122     curValue = config.polarity;
123     config.polarity = polarity;
124     ret = PwmSetConfig(handle, &config);
125     if (ret == HDF_SUCCESS) {
126         HDF_LOGI("PwmSetPolarity: success. polarity: %d -> %d!", curValue, config.polarity);
127     }
128     return ret;
129 }
130 
PwmEnable(DevHandle handle)131 int32_t PwmEnable(DevHandle handle)
132 {
133     struct PwmConfig config;
134     uint32_t curValue;
135     int32_t ret;
136 
137     if (PwmGetConfig(handle, &config) != HDF_SUCCESS) {
138         HDF_LOGE("PwmEnable: PwmGetConfig fail!");
139         return HDF_FAILURE;
140     }
141     curValue = config.status;
142     config.status = PWM_ENABLE_STATUS;
143     ret = PwmSetConfig(handle, &config);
144     if (ret == HDF_SUCCESS) {
145         HDF_LOGI("PwmEnable: success. enable: %u -> %hhu!", curValue, config.status);
146     }
147     return ret;
148 }
149 
PwmDisable(DevHandle handle)150 int32_t PwmDisable(DevHandle handle)
151 {
152     struct PwmConfig config;
153     uint32_t curValue;
154     int32_t ret;
155 
156     if (PwmGetConfig(handle, &config) != HDF_SUCCESS) {
157         HDF_LOGE("PwmDisable: PwmGetConfig fail!");
158         return HDF_FAILURE;
159     }
160     curValue = config.status;
161     config.status = PWM_DISABLE_STATUS;
162     ret = PwmSetConfig(handle, &config);
163     if (ret == HDF_SUCCESS) {
164         HDF_LOGI("PwmDisable: success. enable: %d -> %d!", curValue, config.status);
165     }
166     return ret;
167 }
168 
PwmSetConfig(DevHandle handle,struct PwmConfig * config)169 int32_t PwmSetConfig(DevHandle handle, struct PwmConfig *config)
170 {
171     int32_t ret;
172 
173     if (handle == NULL) {
174         HDF_LOGE("PwmSetConfig: handle is null!");
175         return HDF_ERR_INVALID_OBJECT;
176     }
177     if (config == NULL) {
178         HDF_LOGE("PwmSetConfig: config is null!");
179         return HDF_ERR_INVALID_PARAM;
180     }
181 
182     ret = PwmDeviceSetConfig(handle, config);
183     if (ret != HDF_SUCCESS) {
184         HDF_LOGE("PwmSetConfig: PwmSetConfig fail, ret: %d!", ret);
185     }
186 
187     return HDF_SUCCESS;
188 }
189 
PwmGetConfig(DevHandle handle,struct PwmConfig * config)190 int32_t PwmGetConfig(DevHandle handle, struct PwmConfig *config)
191 {
192     struct PwmDev *pwm = NULL;
193 
194     if (handle == NULL) {
195         HDF_LOGE("PwmGetConfig: handle is null!");
196         return HDF_ERR_INVALID_OBJECT;
197     }
198     if (config == NULL) {
199         HDF_LOGE("PwmGetConfig: config is null!");
200         return HDF_ERR_INVALID_PARAM;
201     }
202     pwm = (struct PwmDev *)handle;
203     *config = pwm->cfg;
204 
205     return HDF_SUCCESS;
206 }
207