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