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