blob: c8b209da215740fa1028dac5390758b81be76440 [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
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070024/*-----------------------------------------------------------------------
25 * Representation of a SPI slave, i.e. what we're communicating with.
26 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070027 * bus: ID of the bus that the slave is attached to.
28 * cs: ID of the chip select connected to the slave.
29 */
30struct spi_slave {
31 unsigned int bus;
32 unsigned int cs;
33};
34
35/*-----------------------------------------------------------------------
36 * Initialization, must be called once on start up.
37 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070038 */
39void spi_init(void);
40
41/*-----------------------------------------------------------------------
42 * Set up communications parameters for a SPI slave.
43 *
44 * This must be called once for each slave. Note that this function
45 * usually doesn't touch any actual hardware, it only initializes the
46 * contents of spi_slave so that the hardware can be easily
47 * initialized later.
48 *
49 * bus: Bus ID of the slave chip.
50 * cs: Chip select ID of the slave chip on the specified bus.
Furquan Shaikh36b81af2016-12-01 01:02:44 -080051 * slave: Pointer to slave structure that needs to be initialized.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070052 *
Furquan Shaikh36b81af2016-12-01 01:02:44 -080053 * Returns:
54 * 0 on success, -1 on error
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070055 */
Furquan Shaikh36b81af2016-12-01 01:02:44 -080056int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070057
58/*-----------------------------------------------------------------------
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070059 * Claim the bus and prepare it for communication with a given slave.
60 *
61 * This must be called before doing any transfers with a SPI slave. It
62 * will enable and initialize any SPI hardware as necessary, and make
63 * sure that the SCK line is in the correct idle state. It is not
64 * allowed to claim the same bus for several slaves without releasing
65 * the bus in between.
66 *
67 * slave: The SPI slave
68 *
69 * Returns: 0 if the bus was claimed successfully, or a negative value
70 * if it wasn't.
71 */
Furquan Shaikh0dba0252016-11-30 04:34:22 -080072int spi_claim_bus(const struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070073
74/*-----------------------------------------------------------------------
75 * Release the SPI bus
76 *
77 * This must be called once for every call to spi_claim_bus() after
78 * all transfers have finished. It may disable any SPI hardware as
79 * appropriate.
80 *
81 * slave: The SPI slave
82 */
Furquan Shaikh0dba0252016-11-30 04:34:22 -080083void spi_release_bus(const struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070084
85/*-----------------------------------------------------------------------
86 * SPI transfer
87 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070088 * spi_xfer() interface:
89 * slave: The SPI slave which will be sending/receiving the data.
Gabe Black93d9f922014-03-27 21:52:43 -070090 * dout: Pointer to a string of bytes to send out.
91 * bytesout: How many bytes to write.
92 * din: Pointer to a string of bytes that will be filled in.
93 * bytesin: How many bytes to read.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070094 *
95 * Returns: 0 on success, not 0 on failure
96 */
Furquan Shaikh0dba0252016-11-30 04:34:22 -080097int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout,
98 void *din, size_t bytesin);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070099
Kyösti Mälkki11104952014-06-29 16:17:33 +0300100unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len);
101
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700102/*-----------------------------------------------------------------------
103 * Write 8 bits, then read 8 bits.
104 * slave: The SPI slave we're communicating with
105 * byte: Byte to be written
106 *
107 * Returns: The value that was read, or a negative value on error.
108 *
109 * TODO: This function probably shouldn't be inlined.
110 */
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800111static inline int spi_w8r8(const struct spi_slave *slave, unsigned char byte)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700112{
113 unsigned char dout[2];
114 unsigned char din[2];
115 int ret;
116
117 dout[0] = byte;
118 dout[1] = 0;
119
Gabe Black93d9f922014-03-27 21:52:43 -0700120 ret = spi_xfer(slave, dout, 2, din, 2);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700121 return ret < 0 ? ret : din[1];
122}
123
Patrick Georgibc64cae2013-02-11 22:12:55 +0100124#endif /* _SPI_GENERIC_H_ */