]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/iio/common/st_sensors.h
Merge tag 'for-linus-20170825' of git://git.infradead.org/linux-mtd
[mirror_ubuntu-artful-kernel.git] / include / linux / iio / common / st_sensors.h
CommitLineData
23491b51
DC
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>
5ea86494 18#include <linux/bitops.h>
77448761 19#include <linux/regulator/consumer.h>
23491b51 20
23cde4d6
DC
21#include <linux/platform_data/st_sensors_pdata.h>
22
23491b51
DC
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
23491b51 30#define ST_SENSORS_ENABLE_ALL_AXIS 0x07
23491b51
DC
31#define ST_SENSORS_SCAN_X 0
32#define ST_SENSORS_SCAN_Y 1
33#define ST_SENSORS_SCAN_Z 2
23491b51
DC
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
97865fe4 40#define ST_SENSORS_DEFAULT_STAT_ADDR 0x27
23491b51
DC
41
42#define ST_SENSORS_MAX_NAME 17
43#define ST_SENSORS_MAX_4WAI 7
44
762011d6
DC
45#define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
46 ch2, s, endian, rbits, sbits, addr) \
23491b51
DC
47{ \
48 .type = device_type, \
762011d6
DC
49 .modified = mod, \
50 .info_mask_separate = mask, \
2d239c9e 51 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
23491b51 52 .scan_index = index, \
762011d6 53 .channel2 = ch2, \
23491b51
DC
54 .address = addr, \
55 .scan_type = { \
762011d6
DC
56 .sign = s, \
57 .realbits = rbits, \
58 .shift = sbits - rbits, \
59 .storagebits = sbits, \
23491b51
DC
60 .endianness = endian, \
61 }, \
62}
63
23491b51
DC
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
72struct st_sensor_odr_avl {
73 unsigned int hz;
74 u8 value;
75};
76
77struct st_sensor_odr {
78 u8 addr;
79 u8 mask;
80 struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
81};
82
83struct st_sensor_power {
84 u8 addr;
85 u8 mask;
86 u8 value_off;
87 u8 value_on;
88};
89
90struct st_sensor_axis {
91 u8 addr;
92 u8 mask;
93};
94
95struct st_sensor_fullscale_avl {
96 unsigned int num;
97 u8 value;
98 unsigned int gain;
99 unsigned int gain2;
100};
101
102struct st_sensor_fullscale {
103 u8 addr;
104 u8 mask;
105 struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
106};
107
a7b8829d
LB
108struct st_sensor_sim {
109 u8 addr;
110 u8 value;
111};
112
23491b51
DC
113/**
114 * struct st_sensor_bdu - ST sensor device block data update
115 * @addr: address of the register.
116 * @mask: mask to write the block data update flag.
117 */
118struct st_sensor_bdu {
119 u8 addr;
120 u8 mask;
121};
122
65e4345c
LW
123/**
124 * struct st_sensor_das - ST sensor device data alignment selection
125 * @addr: address of the register.
126 * @mask: mask to write the das flag for left alignment.
127 */
128struct st_sensor_das {
129 u8 addr;
130 u8 mask;
131};
132
23491b51
DC
133/**
134 * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
135 * @addr: address of the register.
23cde4d6
DC
136 * @mask_int1: mask to enable/disable IRQ on INT1 pin.
137 * @mask_int2: mask to enable/disable IRQ on INT2 pin.
a9fd053b
LW
138 * @addr_ihl: address to enable/disable active low on the INT lines.
139 * @mask_ihl: mask to enable/disable active low on the INT lines.
0e6f6871
LW
140 * @addr_od: address to enable/disable Open Drain on the INT lines.
141 * @mask_od: mask to enable/disable Open Drain on the INT lines.
97865fe4 142 * @addr_stat_drdy: address to read status of DRDY (data ready) interrupt
23491b51
DC
143 * struct ig1 - represents the Interrupt Generator 1 of sensors.
144 * @en_addr: address of the enable ig1 register.
145 * @en_mask: mask to write the on/off value for enable.
146 */
147struct st_sensor_data_ready_irq {
148 u8 addr;
23cde4d6
DC
149 u8 mask_int1;
150 u8 mask_int2;
a9fd053b
LW
151 u8 addr_ihl;
152 u8 mask_ihl;
0e6f6871
LW
153 u8 addr_od;
154 u8 mask_od;
97865fe4 155 u8 addr_stat_drdy;
23491b51
DC
156 struct {
157 u8 en_addr;
158 u8 en_mask;
159 } ig1;
160};
161
162/**
163 * struct st_sensor_transfer_buffer - ST sensor device I/O buffer
164 * @buf_lock: Mutex to protect rx and tx buffers.
165 * @tx_buf: Buffer used by SPI transfer function to send data to the sensors.
166 * This buffer is used to avoid DMA not-aligned issue.
167 * @rx_buf: Buffer used by SPI transfer to receive data from sensors.
168 * This buffer is used to avoid DMA not-aligned issue.
169 */
170struct st_sensor_transfer_buffer {
171 struct mutex buf_lock;
172 u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH];
173 u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned;
174};
175
176/**
177 * struct st_sensor_transfer_function - ST sensor device I/O function
178 * @read_byte: Function used to read one byte.
179 * @write_byte: Function used to write one byte.
180 * @read_multiple_byte: Function used to read multiple byte.
181 */
182struct st_sensor_transfer_function {
183 int (*read_byte) (struct st_sensor_transfer_buffer *tb,
184 struct device *dev, u8 reg_addr, u8 *res_byte);
185 int (*write_byte) (struct st_sensor_transfer_buffer *tb,
186 struct device *dev, u8 reg_addr, u8 data);
187 int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb,
188 struct device *dev, u8 reg_addr, int len, u8 *data,
189 bool multiread_bit);
190};
191
192/**
a7ee8839 193 * struct st_sensor_settings - ST specific sensor settings
23491b51 194 * @wai: Contents of WhoAmI register.
bc27381e 195 * @wai_addr: The address of WhoAmI register.
23491b51
DC
196 * @sensors_supported: List of supported sensors by struct itself.
197 * @ch: IIO channels for the sensor.
198 * @odr: Output data rate register and ODR list available.
199 * @pw: Power register of the sensor.
200 * @enable_axis: Enable one or more axis of the sensor.
201 * @fs: Full scale register and full scale list available.
202 * @bdu: Block data update register.
65e4345c 203 * @das: Data Alignment Selection register.
23491b51 204 * @drdy_irq: Data ready register of the sensor.
a7b8829d 205 * @sim: SPI serial interface mode register of the sensor.
23491b51
DC
206 * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
207 * @bootime: samples to discard when sensor passing from power-down to power-up.
208 */
a7ee8839 209struct st_sensor_settings {
23491b51 210 u8 wai;
bc27381e 211 u8 wai_addr;
23491b51
DC
212 char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
213 struct iio_chan_spec *ch;
ea01f2c1 214 int num_ch;
23491b51
DC
215 struct st_sensor_odr odr;
216 struct st_sensor_power pw;
217 struct st_sensor_axis enable_axis;
218 struct st_sensor_fullscale fs;
219 struct st_sensor_bdu bdu;
65e4345c 220 struct st_sensor_das das;
23491b51 221 struct st_sensor_data_ready_irq drdy_irq;
a7b8829d 222 struct st_sensor_sim sim;
23491b51
DC
223 bool multi_read_bit;
224 unsigned int bootime;
225};
226
227/**
228 * struct st_sensor_data - ST sensor device status
229 * @dev: Pointer to instance of struct device (I2C or SPI).
230 * @trig: The trigger in use by the core driver.
a7ee8839 231 * @sensor_settings: Pointer to the specific sensor settings in use.
23491b51 232 * @current_fullscale: Maximum range of measure by the sensor.
77448761 233 * @vdd: Pointer to sensor's Vdd power supply
71e1980c 234 * @vdd_io: Pointer to sensor's Vdd-IO power supply
23491b51
DC
235 * @enabled: Status of the sensor (false->off, true->on).
236 * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread.
237 * @buffer_data: Data used by buffer part.
238 * @odr: Output data rate of the sensor [Hz].
607a568a 239 * num_data_channels: Number of data channels used in buffer.
23cde4d6 240 * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
0e6f6871 241 * @int_pin_open_drain: Set the interrupt/DRDY to open drain.
23491b51
DC
242 * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
243 * @tf: Transfer function structure used by I/O operations.
244 * @tb: Transfer buffers and mutex used by I/O operations.
90efe055 245 * @edge_irq: the IRQ triggers on edges and need special handling.
65925b65
LW
246 * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
247 * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
23491b51
DC
248 */
249struct st_sensor_data {
250 struct device *dev;
251 struct iio_trigger *trig;
a7ee8839 252 struct st_sensor_settings *sensor_settings;
23491b51 253 struct st_sensor_fullscale_avl *current_fullscale;
77448761 254 struct regulator *vdd;
71e1980c 255 struct regulator *vdd_io;
23491b51
DC
256
257 bool enabled;
258 bool multiread_bit;
259
260 char *buffer_data;
261
262 unsigned int odr;
607a568a 263 unsigned int num_data_channels;
23491b51 264
23cde4d6 265 u8 drdy_int_pin;
0e6f6871 266 bool int_pin_open_drain;
23cde4d6 267
23491b51
DC
268 unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);
269
270 const struct st_sensor_transfer_function *tf;
271 struct st_sensor_transfer_buffer tb;
65925b65 272
90efe055 273 bool edge_irq;
65925b65
LW
274 bool hw_irq_trigger;
275 s64 hw_timestamp;
23491b51
DC
276};
277
278#ifdef CONFIG_IIO_BUFFER
5a4d7291 279irqreturn_t st_sensors_trigger_handler(int irq, void *p);
5a4d7291
GR
280#endif
281
282#ifdef CONFIG_IIO_TRIGGER
23491b51
DC
283int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
284 const struct iio_trigger_ops *trigger_ops);
285
286void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
65925b65
LW
287int st_sensors_validate_device(struct iio_trigger *trig,
288 struct iio_dev *indio_dev);
91ffbabf
DC
289#else
290static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
291 const struct iio_trigger_ops *trigger_ops)
292{
293 return 0;
294}
295static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
296{
297 return;
298}
65925b65 299#define st_sensors_validate_device NULL
23491b51
DC
300#endif
301
23cde4d6
DC
302int st_sensors_init_sensor(struct iio_dev *indio_dev,
303 struct st_sensors_platform_data *pdata);
23491b51
DC
304
305int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
306
307int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
308
14f295c8 309int st_sensors_power_enable(struct iio_dev *indio_dev);
ea7e586b
LW
310
311void st_sensors_power_disable(struct iio_dev *indio_dev);
312
a0175b9c
LW
313int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
314 unsigned reg, unsigned writeval,
315 unsigned *readval);
316
23491b51
DC
317int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
318
319int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
320
321int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
322
323int st_sensors_read_info_raw(struct iio_dev *indio_dev,
324 struct iio_chan_spec const *ch, int *val);
325
326int st_sensors_check_device_support(struct iio_dev *indio_dev,
a7ee8839 327 int num_sensors_list, const struct st_sensor_settings *sensor_settings);
23491b51 328
23491b51
DC
329ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
330 struct device_attribute *attr, char *buf);
331
332ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
333 struct device_attribute *attr, char *buf);
334
335#endif /* ST_SENSORS_H */