blob: b4a10af0c00a00bc22577f0e1ae6d5dd77498ea1 [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
2 * (C) Copyright 2001
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
4 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070014 */
15
Patrick Georgibc64cae2013-02-11 22:12:55 +010016#ifndef _SPI_GENERIC_H_
17#define _SPI_GENERIC_H_
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070018
19#include <stdint.h>
Furquan Shaikh0dba0252016-11-30 04:34:22 -080020#include <stddef.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070021
22/* Controller-specific definitions: */
23
Furquan Shaikh94f86992016-12-01 07:12:32 -080024struct spi_ctrlr;
25
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070026/*-----------------------------------------------------------------------
27 * Representation of a SPI slave, i.e. what we're communicating with.
28 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070029 * bus: ID of the bus that the slave is attached to.
30 * cs: ID of the chip select connected to the slave.
Furquan Shaikh94f86992016-12-01 07:12:32 -080031 * ctrlr: Pointer to SPI controller structure.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070032 */
33struct spi_slave {
34 unsigned int bus;
35 unsigned int cs;
Furquan Shaikh94f86992016-12-01 07:12:32 -080036 const struct spi_ctrlr *ctrlr;
37};
38
39/*-----------------------------------------------------------------------
40 * Representation of a SPI contoller.
41 *
42 * claim_bus: Claim SPI bus and prepare for communication.
43 * release_bus: Release SPI bus.
44 * xfer: SPI transfer
45 */
46struct spi_ctrlr {
47 int (*claim_bus)(const struct spi_slave *slave);
48 void (*release_bus)(const struct spi_slave *slave);
49 int (*xfer)(const struct spi_slave *slave, const void *dout,
50 size_t bytesout, void *din, size_t bytesin);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070051};
52
53/*-----------------------------------------------------------------------
54 * Initialization, must be called once on start up.
55 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070056 */
57void spi_init(void);
58
59/*-----------------------------------------------------------------------
60 * Set up communications parameters for a SPI slave.
61 *
62 * This must be called once for each slave. Note that this function
63 * usually doesn't touch any actual hardware, it only initializes the
64 * contents of spi_slave so that the hardware can be easily
65 * initialized later.
66 *
67 * bus: Bus ID of the slave chip.
68 * cs: Chip select ID of the slave chip on the specified bus.
Furquan Shaikh36b81af2016-12-01 01:02:44 -080069 * slave: Pointer to slave structure that needs to be initialized.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070070 *
Furquan Shaikh36b81af2016-12-01 01:02:44 -080071 * Returns:
72 * 0 on success, -1 on error
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070073 */
Furquan Shaikh36b81af2016-12-01 01:02:44 -080074int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070075
76/*-----------------------------------------------------------------------
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070077 * Claim the bus and prepare it for communication with a given slave.
78 *
79 * This must be called before doing any transfers with a SPI slave. It
80 * will enable and initialize any SPI hardware as necessary, and make
81 * sure that the SCK line is in the correct idle state. It is not
82 * allowed to claim the same bus for several slaves without releasing
83 * the bus in between.
84 *
85 * slave: The SPI slave
86 *
87 * Returns: 0 if the bus was claimed successfully, or a negative value
88 * if it wasn't.
89 */
Furquan Shaikh0dba0252016-11-30 04:34:22 -080090int spi_claim_bus(const struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070091
92/*-----------------------------------------------------------------------
93 * Release the SPI bus
94 *
95 * This must be called once for every call to spi_claim_bus() after
96 * all transfers have finished. It may disable any SPI hardware as
97 * appropriate.
98 *
99 * slave: The SPI slave
100 */
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800101void spi_release_bus(const struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700102
103/*-----------------------------------------------------------------------
104 * SPI transfer
105 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700106 * spi_xfer() interface:
107 * slave: The SPI slave which will be sending/receiving the data.
Gabe Black93d9f922014-03-27 21:52:43 -0700108 * dout: Pointer to a string of bytes to send out.
109 * bytesout: How many bytes to write.
110 * din: Pointer to a string of bytes that will be filled in.
111 * bytesin: How many bytes to read.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700112 *
113 * Returns: 0 on success, not 0 on failure
114 */
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800115int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout,
116 void *din, size_t bytesin);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700117
Kyösti Mälkki11104952014-06-29 16:17:33 +0300118unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len);
119
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700120/*-----------------------------------------------------------------------
121 * Write 8 bits, then read 8 bits.
122 * slave: The SPI slave we're communicating with
123 * byte: Byte to be written
124 *
125 * Returns: The value that was read, or a negative value on error.
126 *
127 * TODO: This function probably shouldn't be inlined.
128 */
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800129static inline int spi_w8r8(const struct spi_slave *slave, unsigned char byte)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700130{
131 unsigned char dout[2];
132 unsigned char din[2];
133 int ret;
134
135 dout[0] = byte;
136 dout[1] = 0;
137
Gabe Black93d9f922014-03-27 21:52:43 -0700138 ret = spi_xfer(slave, dout, 2, din, 2);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700139 return ret < 0 ? ret : din[1];
140}
141
Patrick Georgibc64cae2013-02-11 22:12:55 +0100142#endif /* _SPI_GENERIC_H_ */