2 * Copyright (c) 2016-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.
17 #include "peripheral_io.h"
22 #include <system_info.h>
24 #include "peripheral_io.h"
25 #include "peripheral_gdbus_spi.h"
26 #include "peripheral_common.h"
27 #include "peripheral_internal.h"
29 #define PERIPHERAL_IO_SPI_FEATURE "http://tizen.org/feature/peripheral_io.spi"
31 #define SPI_FEATURE_UNKNOWN -1
32 #define SPI_FEATURE_FALSE 0
33 #define SPI_FEATURE_TRUE 1
35 static int spi_feature = SPI_FEATURE_UNKNOWN;
37 static bool __is_feature_supported()
39 int ret = SYSTEM_INFO_ERROR_NONE;
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");
46 spi_feature = (feature ? SPI_FEATURE_TRUE : SPI_FEATURE_FALSE);
49 return (spi_feature == SPI_FEATURE_TRUE ? true : false);
52 int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi)
54 peripheral_spi_h handle;
55 int ret = PERIPHERAL_ERROR_NONE;
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");
62 handle = (peripheral_spi_h)calloc(1, sizeof(struct _peripheral_spi_s));
65 _E("Failed to allocate peripheral_spi_h");
66 return PERIPHERAL_ERROR_OUT_OF_MEMORY;
69 ret = peripheral_gdbus_spi_open(handle, bus, cs);
71 if (ret != PERIPHERAL_ERROR_NONE) {
72 _E("SPI open error (%d, %d)", bus, cs);
81 int peripheral_spi_close(peripheral_spi_h spi)
83 int ret = PERIPHERAL_ERROR_NONE;
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");
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);
97 int peripheral_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode)
99 int ret = PERIPHERAL_ERROR_NONE;
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");
105 // TODO : replace interface function
110 int peripheral_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_e bit_order)
112 int ret = PERIPHERAL_ERROR_NONE;
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");
118 // TODO : replace interface function
123 int peripheral_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits)
125 int ret = PERIPHERAL_ERROR_NONE;
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");
130 // TODO : replace interface function
135 int peripheral_spi_set_frequency(peripheral_spi_h spi, uint32_t freq_hz)
137 int ret = PERIPHERAL_ERROR_NONE;
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");
142 // TODO : replace interface function
147 int peripheral_spi_read(peripheral_spi_h spi, uint8_t *data, uint32_t length)
149 int ret = PERIPHERAL_ERROR_NONE;
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");
155 // TODO : replace interface function
160 int peripheral_spi_write(peripheral_spi_h spi, uint8_t *data, uint32_t length)
162 int ret = PERIPHERAL_ERROR_NONE;
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");
168 // TODO : replace interface function
173 int peripheral_spi_transfer(peripheral_spi_h spi, uint8_t *txdata, uint8_t *rxdata, uint32_t length)
175 int ret = PERIPHERAL_ERROR_NONE;
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");
181 // TODO : replace interface function