]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - include/linux/iio/common/st_sensors.h
scsi: qedf: Fix a potential NULL pointer dereference
[mirror_ubuntu-artful-kernel.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 #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 #define ST_SENSORS_DEFAULT_STAT_ADDR 0x27
41
42 #define ST_SENSORS_MAX_NAME 17
43 #define ST_SENSORS_MAX_4WAI 7
44
45 #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
46 ch2, s, endian, rbits, sbits, addr) \
47 { \
48 .type = device_type, \
49 .modified = mod, \
50 .info_mask_separate = mask, \
51 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
52 .scan_index = index, \
53 .channel2 = ch2, \
54 .address = addr, \
55 .scan_type = { \
56 .sign = s, \
57 .realbits = rbits, \
58 .shift = sbits - rbits, \
59 .storagebits = sbits, \
60 .endianness = endian, \
61 }, \
62 }
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_das - ST sensor device data alignment selection
120 * @addr: address of the register.
121 * @mask: mask to write the das flag for left alignment.
122 */
123 struct st_sensor_das {
124 u8 addr;
125 u8 mask;
126 };
127
128 /**
129 * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
130 * @addr: address of the register.
131 * @mask_int1: mask to enable/disable IRQ on INT1 pin.
132 * @mask_int2: mask to enable/disable IRQ on INT2 pin.
133 * @addr_ihl: address to enable/disable active low on the INT lines.
134 * @mask_ihl: mask to enable/disable active low on the INT lines.
135 * @addr_od: address to enable/disable Open Drain on the INT lines.
136 * @mask_od: mask to enable/disable Open Drain on the INT lines.
137 * @addr_stat_drdy: address to read status of DRDY (data ready) interrupt
138 * struct ig1 - represents the Interrupt Generator 1 of sensors.
139 * @en_addr: address of the enable ig1 register.
140 * @en_mask: mask to write the on/off value for enable.
141 */
142 struct st_sensor_data_ready_irq {
143 u8 addr;
144 u8 mask_int1;
145 u8 mask_int2;
146 u8 addr_ihl;
147 u8 mask_ihl;
148 u8 addr_od;
149 u8 mask_od;
150 u8 addr_stat_drdy;
151 struct {
152 u8 en_addr;
153 u8 en_mask;
154 } ig1;
155 };
156
157 /**
158 * struct st_sensor_transfer_buffer - ST sensor device I/O buffer
159 * @buf_lock: Mutex to protect rx and tx buffers.
160 * @tx_buf: Buffer used by SPI transfer function to send data to the sensors.
161 * This buffer is used to avoid DMA not-aligned issue.
162 * @rx_buf: Buffer used by SPI transfer to receive data from sensors.
163 * This buffer is used to avoid DMA not-aligned issue.
164 */
165 struct st_sensor_transfer_buffer {
166 struct mutex buf_lock;
167 u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH];
168 u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned;
169 };
170
171 /**
172 * struct st_sensor_transfer_function - ST sensor device I/O function
173 * @read_byte: Function used to read one byte.
174 * @write_byte: Function used to write one byte.
175 * @read_multiple_byte: Function used to read multiple byte.
176 */
177 struct st_sensor_transfer_function {
178 int (*read_byte) (struct st_sensor_transfer_buffer *tb,
179 struct device *dev, u8 reg_addr, u8 *res_byte);
180 int (*write_byte) (struct st_sensor_transfer_buffer *tb,
181 struct device *dev, u8 reg_addr, u8 data);
182 int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb,
183 struct device *dev, u8 reg_addr, int len, u8 *data,
184 bool multiread_bit);
185 };
186
187 /**
188 * struct st_sensor_settings - ST specific sensor settings
189 * @wai: Contents of WhoAmI register.
190 * @wai_addr: The address of WhoAmI register.
191 * @sensors_supported: List of supported sensors by struct itself.
192 * @ch: IIO channels for the sensor.
193 * @odr: Output data rate register and ODR list available.
194 * @pw: Power register of the sensor.
195 * @enable_axis: Enable one or more axis of the sensor.
196 * @fs: Full scale register and full scale list available.
197 * @bdu: Block data update register.
198 * @das: Data Alignment Selection register.
199 * @drdy_irq: Data ready register of the sensor.
200 * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
201 * @bootime: samples to discard when sensor passing from power-down to power-up.
202 */
203 struct st_sensor_settings {
204 u8 wai;
205 u8 wai_addr;
206 char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
207 struct iio_chan_spec *ch;
208 int num_ch;
209 struct st_sensor_odr odr;
210 struct st_sensor_power pw;
211 struct st_sensor_axis enable_axis;
212 struct st_sensor_fullscale fs;
213 struct st_sensor_bdu bdu;
214 struct st_sensor_das das;
215 struct st_sensor_data_ready_irq drdy_irq;
216 bool multi_read_bit;
217 unsigned int bootime;
218 };
219
220 /**
221 * struct st_sensor_data - ST sensor device status
222 * @dev: Pointer to instance of struct device (I2C or SPI).
223 * @trig: The trigger in use by the core driver.
224 * @sensor_settings: Pointer to the specific sensor settings in use.
225 * @current_fullscale: Maximum range of measure by the sensor.
226 * @vdd: Pointer to sensor's Vdd power supply
227 * @vdd_io: Pointer to sensor's Vdd-IO power supply
228 * @enabled: Status of the sensor (false->off, true->on).
229 * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread.
230 * @buffer_data: Data used by buffer part.
231 * @odr: Output data rate of the sensor [Hz].
232 * num_data_channels: Number of data channels used in buffer.
233 * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
234 * @int_pin_open_drain: Set the interrupt/DRDY to open drain.
235 * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
236 * @tf: Transfer function structure used by I/O operations.
237 * @tb: Transfer buffers and mutex used by I/O operations.
238 * @edge_irq: the IRQ triggers on edges and need special handling.
239 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
240 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
241 */
242 struct st_sensor_data {
243 struct device *dev;
244 struct iio_trigger *trig;
245 struct st_sensor_settings *sensor_settings;
246 struct st_sensor_fullscale_avl *current_fullscale;
247 struct regulator *vdd;
248 struct regulator *vdd_io;
249
250 bool enabled;
251 bool multiread_bit;
252
253 char *buffer_data;
254
255 unsigned int odr;
256 unsigned int num_data_channels;
257
258 u8 drdy_int_pin;
259 bool int_pin_open_drain;
260
261 unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);
262
263 const struct st_sensor_transfer_function *tf;
264 struct st_sensor_transfer_buffer tb;
265
266 bool edge_irq;
267 bool hw_irq_trigger;
268 s64 hw_timestamp;
269 };
270
271 #ifdef CONFIG_IIO_BUFFER
272 irqreturn_t st_sensors_trigger_handler(int irq, void *p);
273 #endif
274
275 #ifdef CONFIG_IIO_TRIGGER
276 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
277 const struct iio_trigger_ops *trigger_ops);
278
279 void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
280 int st_sensors_validate_device(struct iio_trigger *trig,
281 struct iio_dev *indio_dev);
282 #else
283 static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
284 const struct iio_trigger_ops *trigger_ops)
285 {
286 return 0;
287 }
288 static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
289 {
290 return;
291 }
292 #define st_sensors_validate_device NULL
293 #endif
294
295 int st_sensors_init_sensor(struct iio_dev *indio_dev,
296 struct st_sensors_platform_data *pdata);
297
298 int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
299
300 int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
301
302 int st_sensors_power_enable(struct iio_dev *indio_dev);
303
304 void st_sensors_power_disable(struct iio_dev *indio_dev);
305
306 int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
307 unsigned reg, unsigned writeval,
308 unsigned *readval);
309
310 int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
311
312 int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
313
314 int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
315
316 int st_sensors_read_info_raw(struct iio_dev *indio_dev,
317 struct iio_chan_spec const *ch, int *val);
318
319 int st_sensors_check_device_support(struct iio_dev *indio_dev,
320 int num_sensors_list, const struct st_sensor_settings *sensor_settings);
321
322 ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
323 struct device_attribute *attr, char *buf);
324
325 ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
326 struct device_attribute *attr, char *buf);
327
328 #endif /* ST_SENSORS_H */