dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / iio / common / st_sensors / st_sensors_spi.c
blob5b377373f48d147b6a41406ca8468c4fb4dc5fc8
1 /*
2 * STMicroelectronics sensors spi library driver
4 * Copyright 2012-2013 STMicroelectronics Inc.
6 * Denis Ciocca <denis.ciocca@st.com>
8 * Licensed under the GPL-2.
9 */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/iio/iio.h>
16 #include <linux/iio/common/st_sensors_spi.h>
19 #define ST_SENSORS_SPI_MULTIREAD 0xc0
20 #define ST_SENSORS_SPI_READ 0x80
22 static unsigned int st_sensors_spi_get_irq(struct iio_dev *indio_dev)
24 struct st_sensor_data *sdata = iio_priv(indio_dev);
26 return to_spi_device(sdata->dev)->irq;
29 static int st_sensors_spi_read(struct st_sensor_transfer_buffer *tb,
30 struct device *dev, u8 reg_addr, int len, u8 *data, bool multiread_bit)
32 int err;
34 struct spi_transfer xfers[] = {
36 .tx_buf = tb->tx_buf,
37 .bits_per_word = 8,
38 .len = 1,
41 .rx_buf = tb->rx_buf,
42 .bits_per_word = 8,
43 .len = len,
47 mutex_lock(&tb->buf_lock);
48 if ((multiread_bit) && (len > 1))
49 tb->tx_buf[0] = reg_addr | ST_SENSORS_SPI_MULTIREAD;
50 else
51 tb->tx_buf[0] = reg_addr | ST_SENSORS_SPI_READ;
53 err = spi_sync_transfer(to_spi_device(dev), xfers, ARRAY_SIZE(xfers));
54 if (err)
55 goto acc_spi_read_error;
57 memcpy(data, tb->rx_buf, len);
58 mutex_unlock(&tb->buf_lock);
59 return len;
61 acc_spi_read_error:
62 mutex_unlock(&tb->buf_lock);
63 return err;
66 static int st_sensors_spi_read_byte(struct st_sensor_transfer_buffer *tb,
67 struct device *dev, u8 reg_addr, u8 *res_byte)
69 return st_sensors_spi_read(tb, dev, reg_addr, 1, res_byte, false);
72 static int st_sensors_spi_read_multiple_byte(
73 struct st_sensor_transfer_buffer *tb, struct device *dev,
74 u8 reg_addr, int len, u8 *data, bool multiread_bit)
76 return st_sensors_spi_read(tb, dev, reg_addr, len, data, multiread_bit);
79 static int st_sensors_spi_write_byte(struct st_sensor_transfer_buffer *tb,
80 struct device *dev, u8 reg_addr, u8 data)
82 int err;
84 struct spi_transfer xfers = {
85 .tx_buf = tb->tx_buf,
86 .bits_per_word = 8,
87 .len = 2,
90 mutex_lock(&tb->buf_lock);
91 tb->tx_buf[0] = reg_addr;
92 tb->tx_buf[1] = data;
94 err = spi_sync_transfer(to_spi_device(dev), &xfers, 1);
95 mutex_unlock(&tb->buf_lock);
97 return err;
100 static const struct st_sensor_transfer_function st_sensors_tf_spi = {
101 .read_byte = st_sensors_spi_read_byte,
102 .write_byte = st_sensors_spi_write_byte,
103 .read_multiple_byte = st_sensors_spi_read_multiple_byte,
106 void st_sensors_spi_configure(struct iio_dev *indio_dev,
107 struct spi_device *spi, struct st_sensor_data *sdata)
109 spi_set_drvdata(spi, indio_dev);
111 indio_dev->dev.parent = &spi->dev;
112 indio_dev->name = spi->modalias;
114 sdata->dev = &spi->dev;
115 sdata->tf = &st_sensors_tf_spi;
116 sdata->get_irq_data_ready = st_sensors_spi_get_irq;
118 EXPORT_SYMBOL(st_sensors_spi_configure);
120 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
121 MODULE_DESCRIPTION("STMicroelectronics ST-sensors spi driver");
122 MODULE_LICENSE("GPL v2");