2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <system_info.h>
23 #include "peripheral_io.h"
24 #include "peripheral_gdbus_pwm.h"
25 #include "peripheral_common.h"
26 #include "peripheral_internal.h"
28 #define PERIPHERAL_IO_PWM_FEATURE "http://tizen.org/feature/peripheral_io.pwm"
30 #define PWM_FEATURE_UNKNOWN -1
31 #define PWM_FEATURE_FALSE 0
32 #define PWM_FEATURE_TRUE 1
34 static int pwm_feature = PWM_FEATURE_UNKNOWN;
36 static bool __is_feature_supported()
38 int ret = SYSTEM_INFO_ERROR_NONE;
41 if (pwm_feature == PWM_FEATURE_UNKNOWN) {
42 ret = system_info_get_platform_bool(PERIPHERAL_IO_PWM_FEATURE, &feature);
43 RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info");
45 pwm_feature = (feature ? PWM_FEATURE_TRUE : PWM_FEATURE_FALSE);
48 return (pwm_feature == PWM_FEATURE_TRUE ? true : false);
52 int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm)
54 peripheral_pwm_h handle;
55 int ret = PERIPHERAL_ERROR_NONE;
57 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
58 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm handle");
59 RETVM_IF(chip < 0 || pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
62 handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s));
65 _E("Failed to allocate peripheral_pwm_h");
66 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
69 ret = peripheral_gdbus_pwm_open(handle, chip, pin);
71 if (ret != PERIPHERAL_ERROR_NONE) {
72 _E("Failed to open PWM chip : %d, pin : %d", chip, pin);
81 int peripheral_pwm_close(peripheral_pwm_h pwm)
83 int ret = PERIPHERAL_ERROR_NONE;
85 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
86 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
88 if ((ret = peripheral_gdbus_pwm_close(pwm)) < 0)
89 _E("Failed to close PWM chip, continuing anyway, ret : %d", ret);
96 int peripheral_pwm_set_period(peripheral_pwm_h pwm, uint32_t period_ns)
98 int ret = PERIPHERAL_ERROR_NONE;
100 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
101 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
103 // TODO : replace interface function
108 int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, uint32_t duty_cycle_ns)
110 int ret = PERIPHERAL_ERROR_NONE;
112 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
113 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
115 // TODO : replace interface function
120 int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity)
122 int ret = PERIPHERAL_ERROR_NONE;
124 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
125 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
126 RETVM_IF((polarity < PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH) || (polarity > PERIPHERAL_PWM_POLARITY_ACTIVE_LOW), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid polarity parameter");
128 // TODO : replace interface function
133 int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, bool enable)
135 int ret = PERIPHERAL_ERROR_NONE;
137 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
138 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
140 // TODO : replace interface function