Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. |
| 4 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 5 | * 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 Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Patrick Georgi | bc64cae | 2013-02-11 22:12:55 +0100 | [diff] [blame] | 16 | #ifndef _SPI_GENERIC_H_ |
| 17 | #define _SPI_GENERIC_H_ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 18 | |
| 19 | #include <stdint.h> |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 20 | #include <stddef.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 21 | |
| 22 | /* Controller-specific definitions: */ |
| 23 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 24 | struct spi_ctrlr; |
| 25 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 26 | /*----------------------------------------------------------------------- |
| 27 | * Representation of a SPI slave, i.e. what we're communicating with. |
| 28 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 29 | * bus: ID of the bus that the slave is attached to. |
| 30 | * cs: ID of the chip select connected to the slave. |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 31 | * ctrlr: Pointer to SPI controller structure. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 32 | */ |
| 33 | struct spi_slave { |
| 34 | unsigned int bus; |
| 35 | unsigned int cs; |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 36 | 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 |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 45 | * setup: Setup given SPI device bus. |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 46 | */ |
| 47 | struct spi_ctrlr { |
| 48 | int (*claim_bus)(const struct spi_slave *slave); |
| 49 | void (*release_bus)(const struct spi_slave *slave); |
| 50 | int (*xfer)(const struct spi_slave *slave, const void *dout, |
| 51 | size_t bytesout, void *din, size_t bytesin); |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 52 | int (*setup)(const struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /*----------------------------------------------------------------------- |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 56 | * Structure defining mapping of SPI buses to controller. |
| 57 | * |
| 58 | * ctrlr: Pointer to controller structure managing the given SPI buses. |
| 59 | * bus_start: Start bus number managed by the controller. |
| 60 | * bus_end: End bus number manager by the controller. |
| 61 | */ |
| 62 | struct spi_ctrlr_buses { |
| 63 | const struct spi_ctrlr *ctrlr; |
| 64 | unsigned int bus_start; |
| 65 | unsigned int bus_end; |
| 66 | }; |
| 67 | |
| 68 | /* Mapping of SPI buses to controllers - should be defined by platform. */ |
| 69 | extern const struct spi_ctrlr_buses spi_ctrlr_bus_map[]; |
| 70 | extern const size_t spi_ctrlr_bus_map_count; |
| 71 | |
| 72 | /*----------------------------------------------------------------------- |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 73 | * Initialization, must be called once on start up. |
| 74 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 75 | */ |
| 76 | void spi_init(void); |
| 77 | |
| 78 | /*----------------------------------------------------------------------- |
| 79 | * Set up communications parameters for a SPI slave. |
| 80 | * |
| 81 | * This must be called once for each slave. Note that this function |
| 82 | * usually doesn't touch any actual hardware, it only initializes the |
| 83 | * contents of spi_slave so that the hardware can be easily |
| 84 | * initialized later. |
| 85 | * |
| 86 | * bus: Bus ID of the slave chip. |
| 87 | * cs: Chip select ID of the slave chip on the specified bus. |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 88 | * slave: Pointer to slave structure that needs to be initialized. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 89 | * |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 90 | * Returns: |
| 91 | * 0 on success, -1 on error |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 92 | */ |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 93 | int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 94 | |
| 95 | /*----------------------------------------------------------------------- |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 96 | * Claim the bus and prepare it for communication with a given slave. |
| 97 | * |
| 98 | * This must be called before doing any transfers with a SPI slave. It |
| 99 | * will enable and initialize any SPI hardware as necessary, and make |
| 100 | * sure that the SCK line is in the correct idle state. It is not |
| 101 | * allowed to claim the same bus for several slaves without releasing |
| 102 | * the bus in between. |
| 103 | * |
| 104 | * slave: The SPI slave |
| 105 | * |
| 106 | * Returns: 0 if the bus was claimed successfully, or a negative value |
| 107 | * if it wasn't. |
| 108 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 109 | int spi_claim_bus(const struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 110 | |
| 111 | /*----------------------------------------------------------------------- |
| 112 | * Release the SPI bus |
| 113 | * |
| 114 | * This must be called once for every call to spi_claim_bus() after |
| 115 | * all transfers have finished. It may disable any SPI hardware as |
| 116 | * appropriate. |
| 117 | * |
| 118 | * slave: The SPI slave |
| 119 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 120 | void spi_release_bus(const struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 121 | |
| 122 | /*----------------------------------------------------------------------- |
| 123 | * SPI transfer |
| 124 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 125 | * spi_xfer() interface: |
| 126 | * slave: The SPI slave which will be sending/receiving the data. |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 127 | * dout: Pointer to a string of bytes to send out. |
| 128 | * bytesout: How many bytes to write. |
| 129 | * din: Pointer to a string of bytes that will be filled in. |
| 130 | * bytesin: How many bytes to read. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 131 | * |
| 132 | * Returns: 0 on success, not 0 on failure |
| 133 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 134 | int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout, |
| 135 | void *din, size_t bytesin); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 136 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 137 | unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len); |
| 138 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 139 | /*----------------------------------------------------------------------- |
| 140 | * Write 8 bits, then read 8 bits. |
| 141 | * slave: The SPI slave we're communicating with |
| 142 | * byte: Byte to be written |
| 143 | * |
| 144 | * Returns: The value that was read, or a negative value on error. |
| 145 | * |
| 146 | * TODO: This function probably shouldn't be inlined. |
| 147 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 148 | static inline int spi_w8r8(const struct spi_slave *slave, unsigned char byte) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 149 | { |
| 150 | unsigned char dout[2]; |
| 151 | unsigned char din[2]; |
| 152 | int ret; |
| 153 | |
| 154 | dout[0] = byte; |
| 155 | dout[1] = 0; |
| 156 | |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 157 | ret = spi_xfer(slave, dout, 2, din, 2); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 158 | return ret < 0 ? ret : din[1]; |
| 159 | } |
| 160 | |
Patrick Georgi | bc64cae | 2013-02-11 22:12:55 +0100 | [diff] [blame] | 161 | #endif /* _SPI_GENERIC_H_ */ |