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