b3b8fc15323308f0c21e3cbb1b2a05a765322a26
[platform/core/api/peripheral-io.git] / src / interface / peripheral_interface_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 <sys/ioctl.h>
18 #include <linux/spi/spidev.h>
19
20 #include "peripheral_interface_spi.h"
21
22 int peripheral_interface_spi_close(peripheral_spi_h spi)
23 {
24         int ret = close(spi->fd);
25         CHECK_ERROR(ret != 0);
26
27         return 0;
28 }
29
30 int peripheral_interface_spi_set_mode(peripheral_spi_h spi, peripheral_spi_mode_e mode)
31 {
32         int ret = ioctl(spi->fd, SPI_IOC_WR_MODE, &mode);
33         CHECK_ERROR(ret != 0);
34
35         return 0;
36 }
37
38 int peripheral_interface_spi_set_bit_order(peripheral_spi_h spi, peripheral_spi_bit_order_e bit_order)
39 {
40         int ret = ioctl(spi->fd, SPI_IOC_WR_LSB_FIRST, &bit_order);
41         CHECK_ERROR(ret != 0);
42
43         return 0;
44 }
45
46 int peripheral_interface_spi_set_bits_per_word(peripheral_spi_h spi, uint8_t bits)
47 {
48         int ret = ioctl(spi->fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
49         CHECK_ERROR(ret != 0);
50
51         return 0;
52 }
53
54 int peripheral_interface_spi_set_frequency(peripheral_spi_h spi, uint32_t freq)
55 {
56         int ret = ioctl(spi->fd, SPI_IOC_WR_MAX_SPEED_HZ, &freq);
57         CHECK_ERROR(ret != 0);
58
59         return 0;
60 }
61
62 int peripheral_interface_spi_read(peripheral_spi_h spi, uint8_t *rxbuf, uint32_t length)
63 {
64         int ret;
65         struct spi_ioc_transfer xfer;
66
67         memset(&xfer, 0, sizeof(struct spi_ioc_transfer));
68         xfer.rx_buf = (unsigned long)rxbuf;
69         xfer.len = length;
70
71         ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
72         CHECK_ERROR(ret != 0);
73
74         return 0;
75 }
76
77 int peripheral_interface_spi_write(peripheral_spi_h spi, uint8_t *txbuf, uint32_t length)
78 {
79         int ret;
80         struct spi_ioc_transfer xfer;
81
82         memset(&xfer, 0, sizeof(struct spi_ioc_transfer));
83         xfer.tx_buf = (unsigned long)txbuf;
84         xfer.len = length;
85
86         ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
87         CHECK_ERROR(ret != 0);
88
89         return 0;
90 }
91
92 int peripheral_interface_spi_transfer(peripheral_spi_h spi, uint8_t *txbuf, uint8_t *rxbuf, uint32_t length)
93 {
94         int ret;
95         struct spi_ioc_transfer xfer;
96
97         if (!txbuf || !rxbuf) return -EINVAL;
98
99         memset(&xfer, 0, sizeof(xfer));
100         xfer.tx_buf = (unsigned long)txbuf;
101         xfer.rx_buf = (unsigned long)rxbuf;
102         xfer.len = length;
103
104         ret = ioctl(spi->fd, SPI_IOC_MESSAGE(1), &xfer);
105         CHECK_ERROR(ret != 0);
106
107         return 0;
108 }