aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/spi/spi.h
diff options
context:
space:
mode:
authorLars-Peter Clausen <lars@metafoo.de>2013-09-27 10:34:27 -0400
committerMark Brown <broonie@linaro.org>2013-10-03 08:52:03 -0400
commit05071aa864e84000759191438a4a9ff7ba2c360e (patch)
tree08528c6a707ad83eefbf57950fa19cfad86e579b /include/linux/spi/spi.h
parent15c03dd4859ab16f9212238f29dd315654aa94f6 (diff)
spi: Add a spi_w8r16be() helper
This patch adds a new spi_w8r16be() helper, which is similar to spi_w8r16() except that it converts the read data word from big endian to native endianness before returning it. The reason for introducing this new helper is that for SPI slave devices it is quite common that the read 16 bit data word is in big endian. So users of spi_w8r16() have to convert the result to native endianness manually. A second reason is that in this case the endianness of the return value of spi_w8r16() depends on its sign. If it is negative (i.e. a error code) it is already in native endianness, if it is positive it is in big endian. The sparse code checker doesn't like this kind of mixed endianness and special annotations are necessary to keep it quiet (E.g. casting to be16 using __force). Doing the conversion to native endianness in the helper function does not require such annotations since we are not mixing different endiannesses in the same variable. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Mark Brown <broonie@linaro.org>
Diffstat (limited to 'include/linux/spi/spi.h')
-rw-r--r--include/linux/spi/spi.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 887116dbce2c..0e0aebdeb56b 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -823,6 +823,33 @@ static inline ssize_t spi_w8r16(struct spi_device *spi, u8 cmd)
823 return (status < 0) ? status : result; 823 return (status < 0) ? status : result;
824} 824}
825 825
826/**
827 * spi_w8r16be - SPI synchronous 8 bit write followed by 16 bit big-endian read
828 * @spi: device with which data will be exchanged
829 * @cmd: command to be written before data is read back
830 * Context: can sleep
831 *
832 * This returns the (unsigned) sixteen bit number returned by the device in cpu
833 * endianness, or else a negative error code. Callable only from contexts that
834 * can sleep.
835 *
836 * This function is similar to spi_w8r16, with the exception that it will
837 * convert the read 16 bit data word from big-endian to native endianness.
838 *
839 */
840static inline ssize_t spi_w8r16be(struct spi_device *spi, u8 cmd)
841
842{
843 ssize_t status;
844 __be16 result;
845
846 status = spi_write_then_read(spi, &cmd, 1, &result, 2);
847 if (status < 0)
848 return status;
849
850 return be16_to_cpu(result);
851}
852
826/*---------------------------------------------------------------------------*/ 853/*---------------------------------------------------------------------------*/
827 854
828/* 855/*