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()
40 if (pwm_feature == PWM_FEATURE_UNKNOWN) {
41 system_info_get_platform_bool(PERIPHERAL_IO_PWM_FEATURE, &feature);
42 pwm_feature = (feature ? PWM_FEATURE_TRUE : PWM_FEATURE_FALSE);
45 return (pwm_feature == PWM_FEATURE_TRUE ? true : false);
49 int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm)
51 peripheral_pwm_h handle;
52 int ret = PERIPHERAL_ERROR_NONE;
54 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
55 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm handle");
56 RETVM_IF(chip < 0 || pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
59 handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s));
62 _E("Failed to allocate peripheral_pwm_h");
63 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
68 ret = peripheral_gdbus_pwm_open(handle, chip, pin);
70 if (ret != PERIPHERAL_ERROR_NONE) {
71 _E("Failed to open PWM chip : %d, pin : %d", chip, pin);
80 int peripheral_pwm_close(peripheral_pwm_h pwm)
82 int ret = PERIPHERAL_ERROR_NONE;
84 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
85 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
87 if ((ret = peripheral_gdbus_pwm_close(pwm)) < 0)
88 _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)
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 ret = peripheral_gdbus_pwm_set_period(pwm, (int)period_ns);
104 if (ret != PERIPHERAL_ERROR_NONE)
105 _E("Failed to set period, ret : %d", ret);
110 int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, uint32_t duty_cycle_ns)
114 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
115 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
117 ret = peripheral_gdbus_pwm_set_duty_cycle(pwm, (int)duty_cycle_ns);
118 if (ret != PERIPHERAL_ERROR_NONE)
119 _E("Failed to set duty cycle, ret : %d", ret);
124 int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity)
128 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
129 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
130 RETVM_IF((polarity < PERIPHERAL_PWM_POLARITY_ACTIVE_HIGH) || (polarity > PERIPHERAL_PWM_POLARITY_ACTIVE_LOW), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid polarity parameter");
132 ret = peripheral_gdbus_pwm_set_polarity(pwm, polarity);
133 if (ret != PERIPHERAL_ERROR_NONE)
134 _E("Failed to set polarity, ret : %d", ret);
139 int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, bool enable)
143 RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "PWM feature is not supported");
144 RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "pwm handle is NULL");
146 ret = peripheral_gdbus_pwm_set_enable(pwm, enable);
147 if (ret != PERIPHERAL_ERROR_NONE)
148 _E("Failed to set enable, ret : %d", ret);