Linux 3.9-rc5
[deliverable/linux.git] / drivers / iio / common / st_sensors / st_sensors_spi.c
1 /*
2 * STMicroelectronics sensors spi library driver
3 *
4 * Copyright 2012-2013 STMicroelectronics Inc.
5 *
6 * Denis Ciocca <denis.ciocca@st.com>
7 *
8 * Licensed under the GPL-2.
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/iio/iio.h>
15
16 #include <linux/iio/common/st_sensors_spi.h>
17
18
19 #define ST_SENSORS_SPI_MULTIREAD 0xc0
20 #define ST_SENSORS_SPI_READ 0x80
21
22 static unsigned int st_sensors_spi_get_irq(struct iio_dev *indio_dev)
23 {
24 struct st_sensor_data *sdata = iio_priv(indio_dev);
25
26 return to_spi_device(sdata->dev)->irq;
27 }
28
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)
31 {
32 struct spi_message msg;
33 int err;
34
35 struct spi_transfer xfers[] = {
36 {
37 .tx_buf = tb->tx_buf,
38 .bits_per_word = 8,
39 .len = 1,
40 },
41 {
42 .rx_buf = tb->rx_buf,
43 .bits_per_word = 8,
44 .len = len,
45 }
46 };
47
48 mutex_lock(&tb->buf_lock);
49 if ((multiread_bit) && (len > 1))
50 tb->tx_buf[0] = reg_addr | ST_SENSORS_SPI_MULTIREAD;
51 else
52 tb->tx_buf[0] = reg_addr | ST_SENSORS_SPI_READ;
53
54 spi_message_init(&msg);
55 spi_message_add_tail(&xfers[0], &msg);
56 spi_message_add_tail(&xfers[1], &msg);
57 err = spi_sync(to_spi_device(dev), &msg);
58 if (err)
59 goto acc_spi_read_error;
60
61 memcpy(data, tb->rx_buf, len*sizeof(u8));
62 mutex_unlock(&tb->buf_lock);
63 return len;
64
65 acc_spi_read_error:
66 mutex_unlock(&tb->buf_lock);
67 return err;
68 }
69
70 static int st_sensors_spi_read_byte(struct st_sensor_transfer_buffer *tb,
71 struct device *dev, u8 reg_addr, u8 *res_byte)
72 {
73 return st_sensors_spi_read(tb, dev, reg_addr, 1, res_byte, false);
74 }
75
76 static int st_sensors_spi_read_multiple_byte(
77 struct st_sensor_transfer_buffer *tb, struct device *dev,
78 u8 reg_addr, int len, u8 *data, bool multiread_bit)
79 {
80 return st_sensors_spi_read(tb, dev, reg_addr, len, data, multiread_bit);
81 }
82
83 static int st_sensors_spi_write_byte(struct st_sensor_transfer_buffer *tb,
84 struct device *dev, u8 reg_addr, u8 data)
85 {
86 struct spi_message msg;
87 int err;
88
89 struct spi_transfer xfers = {
90 .tx_buf = tb->tx_buf,
91 .bits_per_word = 8,
92 .len = 2,
93 };
94
95 mutex_lock(&tb->buf_lock);
96 tb->tx_buf[0] = reg_addr;
97 tb->tx_buf[1] = data;
98
99 spi_message_init(&msg);
100 spi_message_add_tail(&xfers, &msg);
101 err = spi_sync(to_spi_device(dev), &msg);
102 mutex_unlock(&tb->buf_lock);
103
104 return err;
105 }
106
107 static const struct st_sensor_transfer_function st_sensors_tf_spi = {
108 .read_byte = st_sensors_spi_read_byte,
109 .write_byte = st_sensors_spi_write_byte,
110 .read_multiple_byte = st_sensors_spi_read_multiple_byte,
111 };
112
113 void st_sensors_spi_configure(struct iio_dev *indio_dev,
114 struct spi_device *spi, struct st_sensor_data *sdata)
115 {
116 spi_set_drvdata(spi, indio_dev);
117
118 indio_dev->dev.parent = &spi->dev;
119 indio_dev->name = spi->modalias;
120
121 sdata->tf = &st_sensors_tf_spi;
122 sdata->get_irq_data_ready = st_sensors_spi_get_irq;
123 }
124 EXPORT_SYMBOL(st_sensors_spi_configure);
125
126 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
127 MODULE_DESCRIPTION("STMicroelectronics ST-sensors spi driver");
128 MODULE_LICENSE("GPL v2");
This page took 0.037186 seconds and 5 git commands to generate.