1 /*
2  * Copyright (C) 2020 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 #pragma once
18 
19 #include <stdbool.h>
20 #include <stddef.h>
21 
22 struct spi_dev_ctx;
23 
24 /**
25  * struct spi_bus_ctx - context structure for SPI devices
26  * @owner:    pointer to &struct spi_dev_ctx currently active on this bus
27  * @num_devs: number of SPI devices attached to this bus
28  */
29 struct spi_bus_ctx {
30     struct spi_dev_ctx* owner;
31     size_t num_devs;
32 };
33 
34 /**
35  * struct spi_seq_entry - individual entry in a sequence of SPI requests
36  * @exec: is invoked when SPI sequence is committed
37  * @priv: command-specific private data
38  *
39  * Sequence of SPI requests is represented by an array of &struct spi_seq_entry,
40  * with each SPI request saved in a &struct spi_seq_entry.
41  *
42  * Implementing spi_seq_*() is not required by SPI driver interface and is not
43  * strictly necessary for this software SPI device. We implement this behavior
44  * for testing purposes.
45  */
46 struct spi_seq_entry {
47     void (*exec)(struct spi_dev_ctx* dev, void* priv);
48     void* priv;
49 };
50 
51 /**
52  * struct spi_dev_ctx - context structure for SPI devices
53  * @bus:      pointer to &struct spi_bus_ctx that this device is attached to
54  * @cmds:     pointer to an array of &struct spi_seq_entry representing a
55  *            sequence of SPI requests
56  * @num_cmds: number of SPI commands in array pointed to by @cmds
57  * @curr_cmd: index of SPI command to be filled out
58  * @loopback: whether this is a loopback device or not
59  */
60 struct spi_dev_ctx {
61     struct spi_bus_ctx* bus;
62     struct spi_seq_entry* cmds;
63     size_t num_cmds;
64     size_t curr_cmd;
65     bool loopback;
66 };
67