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