libata: bugfix: Remove __le32 in ata_tf_to_fis()
[sfrench/cifs-2.6.git] / include / linux / iio / common / st_sensors.h
1 /*
2  * STMicroelectronics sensors 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 #ifndef ST_SENSORS_H
12 #define ST_SENSORS_H
13
14 #include <linux/i2c.h>
15 #include <linux/spi/spi.h>
16 #include <linux/irqreturn.h>
17 #include <linux/iio/trigger.h>
18 #include <linux/bitops.h>
19
20 #define ST_SENSORS_TX_MAX_LENGTH                2
21 #define ST_SENSORS_RX_MAX_LENGTH                6
22
23 #define ST_SENSORS_ODR_LIST_MAX                 10
24 #define ST_SENSORS_FULLSCALE_AVL_MAX            10
25
26 #define ST_SENSORS_NUMBER_ALL_CHANNELS          4
27 #define ST_SENSORS_ENABLE_ALL_AXIS              0x07
28 #define ST_SENSORS_SCAN_X                       0
29 #define ST_SENSORS_SCAN_Y                       1
30 #define ST_SENSORS_SCAN_Z                       2
31 #define ST_SENSORS_DEFAULT_POWER_ON_VALUE       0x01
32 #define ST_SENSORS_DEFAULT_POWER_OFF_VALUE      0x00
33 #define ST_SENSORS_DEFAULT_WAI_ADDRESS          0x0f
34 #define ST_SENSORS_DEFAULT_AXIS_ADDR            0x20
35 #define ST_SENSORS_DEFAULT_AXIS_MASK            0x07
36 #define ST_SENSORS_DEFAULT_AXIS_N_BIT           3
37
38 #define ST_SENSORS_MAX_NAME                     17
39 #define ST_SENSORS_MAX_4WAI                     7
40
41 #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
42                                         ch2, s, endian, rbits, sbits, addr) \
43 { \
44         .type = device_type, \
45         .modified = mod, \
46         .info_mask_separate = mask, \
47         .scan_index = index, \
48         .channel2 = ch2, \
49         .address = addr, \
50         .scan_type = { \
51                 .sign = s, \
52                 .realbits = rbits, \
53                 .shift = sbits - rbits, \
54                 .storagebits = sbits, \
55                 .endianness = endian, \
56         }, \
57 }
58
59 #define ST_SENSOR_DEV_ATTR_SAMP_FREQ() \
60                 IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, \
61                         st_sensors_sysfs_get_sampling_frequency, \
62                         st_sensors_sysfs_set_sampling_frequency)
63
64 #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
65                 IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
66                         st_sensors_sysfs_sampling_frequency_avail)
67
68 #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
69                 IIO_DEVICE_ATTR(name, S_IRUGO, \
70                         st_sensors_sysfs_scale_avail, NULL , 0);
71
72 struct st_sensor_odr_avl {
73         unsigned int hz;
74         u8 value;
75 };
76
77 struct st_sensor_odr {
78         u8 addr;
79         u8 mask;
80         struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
81 };
82
83 struct st_sensor_power {
84         u8 addr;
85         u8 mask;
86         u8 value_off;
87         u8 value_on;
88 };
89
90 struct st_sensor_axis {
91         u8 addr;
92         u8 mask;
93 };
94
95 struct st_sensor_fullscale_avl {
96         unsigned int num;
97         u8 value;
98         unsigned int gain;
99         unsigned int gain2;
100 };
101
102 struct st_sensor_fullscale {
103         u8 addr;
104         u8 mask;
105         struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
106 };
107
108 /**
109  * struct st_sensor_bdu - ST sensor device block data update
110  * @addr: address of the register.
111  * @mask: mask to write the block data update flag.
112  */
113 struct st_sensor_bdu {
114         u8 addr;
115         u8 mask;
116 };
117
118 /**
119  * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
120  * @addr: address of the register.
121  * @mask: mask to write the on/off value.
122  * struct ig1 - represents the Interrupt Generator 1 of sensors.
123  * @en_addr: address of the enable ig1 register.
124  * @en_mask: mask to write the on/off value for enable.
125  */
126 struct st_sensor_data_ready_irq {
127         u8 addr;
128         u8 mask;
129         struct {
130                 u8 en_addr;
131                 u8 en_mask;
132         } ig1;
133 };
134
135 /**
136  * struct st_sensor_transfer_buffer - ST sensor device I/O buffer
137  * @buf_lock: Mutex to protect rx and tx buffers.
138  * @tx_buf: Buffer used by SPI transfer function to send data to the sensors.
139  *      This buffer is used to avoid DMA not-aligned issue.
140  * @rx_buf: Buffer used by SPI transfer to receive data from sensors.
141  *      This buffer is used to avoid DMA not-aligned issue.
142  */
143 struct st_sensor_transfer_buffer {
144         struct mutex buf_lock;
145         u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH];
146         u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned;
147 };
148
149 /**
150  * struct st_sensor_transfer_function - ST sensor device I/O function
151  * @read_byte: Function used to read one byte.
152  * @write_byte: Function used to write one byte.
153  * @read_multiple_byte: Function used to read multiple byte.
154  */
155 struct st_sensor_transfer_function {
156         int (*read_byte) (struct st_sensor_transfer_buffer *tb,
157                                 struct device *dev, u8 reg_addr, u8 *res_byte);
158         int (*write_byte) (struct st_sensor_transfer_buffer *tb,
159                                 struct device *dev, u8 reg_addr, u8 data);
160         int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb,
161                 struct device *dev, u8 reg_addr, int len, u8 *data,
162                                                         bool multiread_bit);
163 };
164
165 /**
166  * struct st_sensors - ST sensors list
167  * @wai: Contents of WhoAmI register.
168  * @sensors_supported: List of supported sensors by struct itself.
169  * @ch: IIO channels for the sensor.
170  * @odr: Output data rate register and ODR list available.
171  * @pw: Power register of the sensor.
172  * @enable_axis: Enable one or more axis of the sensor.
173  * @fs: Full scale register and full scale list available.
174  * @bdu: Block data update register.
175  * @drdy_irq: Data ready register of the sensor.
176  * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
177  * @bootime: samples to discard when sensor passing from power-down to power-up.
178  */
179 struct st_sensors {
180         u8 wai;
181         char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
182         struct iio_chan_spec *ch;
183         struct st_sensor_odr odr;
184         struct st_sensor_power pw;
185         struct st_sensor_axis enable_axis;
186         struct st_sensor_fullscale fs;
187         struct st_sensor_bdu bdu;
188         struct st_sensor_data_ready_irq drdy_irq;
189         bool multi_read_bit;
190         unsigned int bootime;
191 };
192
193 /**
194  * struct st_sensor_data - ST sensor device status
195  * @dev: Pointer to instance of struct device (I2C or SPI).
196  * @trig: The trigger in use by the core driver.
197  * @sensor: Pointer to the current sensor struct in use.
198  * @current_fullscale: Maximum range of measure by the sensor.
199  * @enabled: Status of the sensor (false->off, true->on).
200  * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread.
201  * @buffer_data: Data used by buffer part.
202  * @odr: Output data rate of the sensor [Hz].
203  * num_data_channels: Number of data channels used in buffer.
204  * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
205  * @tf: Transfer function structure used by I/O operations.
206  * @tb: Transfer buffers and mutex used by I/O operations.
207  */
208 struct st_sensor_data {
209         struct device *dev;
210         struct iio_trigger *trig;
211         struct st_sensors *sensor;
212         struct st_sensor_fullscale_avl *current_fullscale;
213
214         bool enabled;
215         bool multiread_bit;
216
217         char *buffer_data;
218
219         unsigned int odr;
220         unsigned int num_data_channels;
221
222         unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);
223
224         const struct st_sensor_transfer_function *tf;
225         struct st_sensor_transfer_buffer tb;
226 };
227
228 #ifdef CONFIG_IIO_BUFFER
229 irqreturn_t st_sensors_trigger_handler(int irq, void *p);
230
231 int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf);
232 #endif
233
234 #ifdef CONFIG_IIO_TRIGGER
235 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
236                                 const struct iio_trigger_ops *trigger_ops);
237
238 void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
239
240 #else
241 static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
242                                 const struct iio_trigger_ops *trigger_ops)
243 {
244         return 0;
245 }
246 static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
247 {
248         return;
249 }
250 #endif
251
252 int st_sensors_init_sensor(struct iio_dev *indio_dev);
253
254 int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
255
256 int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
257
258 int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
259
260 int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
261
262 int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
263
264 int st_sensors_read_info_raw(struct iio_dev *indio_dev,
265                                 struct iio_chan_spec const *ch, int *val);
266
267 int st_sensors_check_device_support(struct iio_dev *indio_dev,
268                         int num_sensors_list, const struct st_sensors *sensors);
269
270 ssize_t st_sensors_sysfs_get_sampling_frequency(struct device *dev,
271                                 struct device_attribute *attr, char *buf);
272
273 ssize_t st_sensors_sysfs_set_sampling_frequency(struct device *dev,
274                 struct device_attribute *attr, const char *buf, size_t size);
275
276 ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
277                                 struct device_attribute *attr, char *buf);
278
279 ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
280                                 struct device_attribute *attr, char *buf);
281
282 #endif /* ST_SENSORS_H */