e21bcbbbde95a75a846e8e80ffe66feea960b507
[platform/core/api/peripheral-io.git] / src / peripheral_pwm.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <assert.h>
21 #include <system_info.h>
22
23 #include "peripheral_io.h"
24 #include "peripheral_gdbus_pwm.h"
25 #include "peripheral_common.h"
26 #include "peripheral_internal.h"
27
28 #define PERIPHERAL_IO_PWM_FEATURE "http://tizen.org/feature/peripheral_io.pwm"
29
30 #define PWM_FEATURE_UNKNOWN -1
31 #define PWM_FEATURE_FALSE    0
32 #define PWM_FEATURE_TRUE     1
33
34 static int pwm_feature = PWM_FEATURE_UNKNOWN;
35
36 static bool __is_feature_supported()
37 {
38         int ret = SYSTEM_INFO_ERROR_NONE;
39         bool feature = false;
40
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");
44
45                 pwm_feature = (feature ? PWM_FEATURE_TRUE : PWM_FEATURE_FALSE);
46         }
47
48         return (pwm_feature == PWM_FEATURE_TRUE ? true : false);
49 }
50
51
52 int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm)
53 {
54         peripheral_pwm_h handle;
55         int ret = PERIPHERAL_ERROR_NONE;
56
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");
60
61         /* Initialize */
62         handle = (peripheral_pwm_h)calloc(1, sizeof(struct _peripheral_pwm_s));
63
64         if (handle == NULL) {
65                 _E("Failed to allocate peripheral_pwm_h");
66                 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
67         }
68
69         ret = peripheral_gdbus_pwm_open(handle, chip, pin);
70
71         if (ret != PERIPHERAL_ERROR_NONE) {
72                 _E("Failed to open PWM chip : %d, pin : %d", chip, pin);
73                 free(handle);
74                 handle = NULL;
75         }
76         *pwm = handle;
77
78         return ret;
79 }
80
81 int peripheral_pwm_close(peripheral_pwm_h pwm)
82 {
83         int ret = PERIPHERAL_ERROR_NONE;
84
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");
87
88         if ((ret = peripheral_gdbus_pwm_close(pwm)) < 0)
89                 _E("Failed to close PWM chip, continuing anyway, ret : %d", ret);
90
91         free(pwm);
92
93         return ret;
94 }
95
96 int peripheral_pwm_set_period(peripheral_pwm_h pwm, uint32_t period_ns)
97 {
98         int ret = PERIPHERAL_ERROR_NONE;
99
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");
102
103         // TODO : replace interface function
104
105         return ret;
106 }
107
108 int peripheral_pwm_set_duty_cycle(peripheral_pwm_h pwm, uint32_t duty_cycle_ns)
109 {
110         int ret = PERIPHERAL_ERROR_NONE;
111
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");
114
115         // TODO : replace interface function
116
117         return ret;
118 }
119
120 int peripheral_pwm_set_polarity(peripheral_pwm_h pwm, peripheral_pwm_polarity_e polarity)
121 {
122         int ret = PERIPHERAL_ERROR_NONE;
123
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");
127
128         // TODO : replace interface function
129
130         return ret;
131 }
132
133 int peripheral_pwm_set_enabled(peripheral_pwm_h pwm, bool enable)
134 {
135         int ret = PERIPHERAL_ERROR_NONE;
136
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");
139
140         // TODO : replace interface function
141
142         return ret;
143 }