68e97611513b3f68f16047369eae76c888ad1244
[platform/core/api/peripheral-io.git] / src / peripheral_spi.c
1 /*
2  * Copyright (c) 2016-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 "peripheral_io.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <system_info.h>
23
24 #include "peripheral_io.h"
25 #include "peripheral_gdbus_spi.h"
26 #include "peripheral_common.h"
27 #include "peripheral_internal.h"
28
29 #define PERIPHERAL_IO_SPI_FEATURE "http://tizen.org/feature/peripheral_io.spi"
30
31 #define SPI_FEATURE_UNKNOWN -1
32 #define SPI_FEATURE_FALSE    0
33 #define SPI_FEATURE_TRUE     1
34
35 static int spi_feature = SPI_FEATURE_UNKNOWN;
36
37 static bool __is_feature_supported()
38 {
39         int ret = SYSTEM_INFO_ERROR_NONE;
40         bool feature = false;
41
42         if (spi_feature == SPI_FEATURE_UNKNOWN) {
43                 ret = system_info_get_platform_bool(PERIPHERAL_IO_SPI_FEATURE, &feature);
44                 RETVM_IF(ret != SYSTEM_INFO_ERROR_NONE, false, "Failed to get system info");
45
46                 spi_feature = (feature ? SPI_FEATURE_TRUE : SPI_FEATURE_FALSE);
47         }
48
49         return (spi_feature == SPI_FEATURE_TRUE ? true : false);
50 }
51
52 int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi)
53 {
54         peripheral_spi_h handle;
55         int ret = PERIPHERAL_ERROR_NONE;
56
57         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
58         RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi handle");
59         RETVM_IF(bus < 0 || cs < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
60
61         /* Initialize */
62         handle = (peripheral_spi_h)calloc(1, sizeof(struct _peripheral_spi_s));
63
64         if (handle == NULL) {
65                 _E("Failed to allocate peripheral_spi_h");
66                 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
67         }
68
69         ret = peripheral_gdbus_spi_open(handle, bus, cs);
70
71         if (ret != PERIPHERAL_ERROR_NONE) {
72                 _E("SPI open error (%d, %d)", bus, cs);
73                 free(handle);
74                 handle = NULL;
75         }
76         *spi = handle;
77
78         return ret;
79 }
80
81 int peripheral_spi_close(peripheral_spi_h spi)
82 {
83         int ret = PERIPHERAL_ERROR_NONE;
84
85         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
86         RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL");
87
88         ret = peripheral_gdbus_spi_close(spi);
89         if (ret < PERIPHERAL_ERROR_NONE)
90                 _E("Failed to close SPI device, continuing anyway, ret : %d", ret);
91
92         free(spi);
93
94         return ret;
95 }
96
97 int peripheral_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode)
98 {
99         int ret = PERIPHERAL_ERROR_NONE;
100
101         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
102         RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL");
103         RETVM_IF((mode < PERIPHERAL_SPI_MODE_0) || (mode > PERIPHERAL_SPI_MODE_3), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi mode parameter");
104
105         // TODO : replace interface function
106
107         return ret;
108 }
109
110 int peripheral_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_e bit_order)
111 {
112         int ret = PERIPHERAL_ERROR_NONE;
113
114         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
115         RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL");
116         RETVM_IF((bit_order < PERIPHERAL_SPI_BIT_ORDER_MSB) || (bit_order > PERIPHERAL_SPI_BIT_ORDER_LSB), PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid bit order parameter");
117
118         // TODO : replace interface function
119
120         return ret;
121 }
122
123 int peripheral_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits)
124 {
125         int ret = PERIPHERAL_ERROR_NONE;
126
127         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
128         RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL");
129
130         // TODO : replace interface function
131
132         return ret;
133 }
134
135 int peripheral_spi_set_frequency(peripheral_spi_h spi, uint32_t freq_hz)
136 {
137         int ret = PERIPHERAL_ERROR_NONE;
138
139         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
140         RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL");
141
142         // TODO : replace interface function
143
144         return ret;
145 }
146
147 int peripheral_spi_read(peripheral_spi_h spi, uint8_t *data, uint32_t length)
148 {
149         int ret = PERIPHERAL_ERROR_NONE;
150
151         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
152         RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL");
153         RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
154
155         // TODO : replace interface function
156
157         return ret;
158 }
159
160 int peripheral_spi_write(peripheral_spi_h spi, uint8_t *data, uint32_t length)
161 {
162         int ret = PERIPHERAL_ERROR_NONE;
163
164         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
165         RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL");
166         RETVM_IF(data == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
167
168         // TODO : replace interface function
169
170         return ret;
171 }
172
173 int peripheral_spi_transfer(peripheral_spi_h spi, uint8_t *txdata, uint8_t *rxdata, uint32_t length)
174 {
175         int ret = PERIPHERAL_ERROR_NONE;
176
177         RETVM_IF(__is_feature_supported() == false, PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
178         RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "spi handle is NULL");
179         RETVM_IF(txdata == NULL || rxdata == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
180
181         // TODO : replace interface function
182
183         return ret;
184 }