1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 #ifndef __SPI_H
18 #define __SPI_H
19 
20 #include <inttypes.h>
21 #include <seos.h>
22 #include <stdlib.h>
23 
24 struct SpiDevice;
25 
26 typedef uint8_t spi_cs_t;
27 typedef uint32_t SpiSpeed;
28 
29 typedef void (*SpiCbkF)(void *cookie, int err);
30 
31 struct SpiMode {
32     enum {
33         SPI_CPOL_IDLE_LO,
34         SPI_CPOL_IDLE_HI,
35     } cpol;
36 
37     enum {
38         SPI_CPHA_LEADING_EDGE,
39         SPI_CPHA_TRAILING_EDGE,
40     } cpha;
41 
42     uint8_t bitsPerWord;
43     enum {
44         SPI_FORMAT_LSB_FIRST,
45         SPI_FORMAT_MSB_FIRST,
46     } format;
47 
48     uint16_t txWord;
49 
50     SpiSpeed speed;
51 
52     bool nssChange;
53 };
54 
55 struct SpiPacket {
56     void *rxBuf;
57     const void *txBuf;
58     size_t size;
59     uint32_t delay;
60 };
61 
62 /**
63  * NOTE:
64  *
65  * To avoid copies, spiMasterRxTx() and spiSlaveRxTx() transfer ownership of
66  * packets[] to the SPI driver.  The SPI driver returns ownership when the
67  * callback is called.
68  *
69  * The caller MUST NOT pass packets[] allocated on the stack, and MUST NOT
70  * deallocate or otherwise mutate packets[] in the meantime.
71  */
72 
73 int spiMasterRequest(uint8_t busId, struct SpiDevice **dev);
74 
75 int spiMasterRxTx(struct SpiDevice *dev, spi_cs_t cs,
76         const struct SpiPacket packets[], size_t n,
77         const struct SpiMode *mode, SpiCbkF callback,
78         void *cookie);
79 
80 int spiMasterRelease(struct SpiDevice *dev);
81 
82 int spiSlaveRequest(uint8_t busId, const struct SpiMode *mode,
83         struct SpiDevice **dev);
84 
85 int spiSlaveRxTx(struct SpiDevice *dev,
86         const struct SpiPacket packets[], size_t n,
87         SpiCbkF callback, void *cookie);
88 
89 int spiSlaveWaitForInactive(struct SpiDevice *dev, SpiCbkF callback,
90         void *cookie);
91 
92 int spiSlaveRelease(struct SpiDevice *dev);
93 #endif /* __SPI_H */
94