]>
Commit | Line | Data |
---|---|---|
fda8d26e | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
23491b51 DC |
2 | /* |
3 | * STMicroelectronics sensors library driver | |
4 | * | |
5 | * Copyright 2012-2013 STMicroelectronics Inc. | |
6 | * | |
7 | * Denis Ciocca <denis.ciocca@st.com> | |
23491b51 DC |
8 | */ |
9 | ||
10 | #ifndef ST_SENSORS_H | |
11 | #define ST_SENSORS_H | |
12 | ||
13 | #include <linux/i2c.h> | |
14 | #include <linux/spi/spi.h> | |
15 | #include <linux/irqreturn.h> | |
16 | #include <linux/iio/trigger.h> | |
5ea86494 | 17 | #include <linux/bitops.h> |
77448761 | 18 | #include <linux/regulator/consumer.h> |
23491b51 | 19 | |
23cde4d6 DC |
20 | #include <linux/platform_data/st_sensors_pdata.h> |
21 | ||
23491b51 DC |
22 | #define ST_SENSORS_TX_MAX_LENGTH 2 |
23 | #define ST_SENSORS_RX_MAX_LENGTH 6 | |
24 | ||
25 | #define ST_SENSORS_ODR_LIST_MAX 10 | |
26 | #define ST_SENSORS_FULLSCALE_AVL_MAX 10 | |
27 | ||
28 | #define ST_SENSORS_NUMBER_ALL_CHANNELS 4 | |
23491b51 | 29 | #define ST_SENSORS_ENABLE_ALL_AXIS 0x07 |
23491b51 DC |
30 | #define ST_SENSORS_SCAN_X 0 |
31 | #define ST_SENSORS_SCAN_Y 1 | |
32 | #define ST_SENSORS_SCAN_Z 2 | |
23491b51 DC |
33 | #define ST_SENSORS_DEFAULT_POWER_ON_VALUE 0x01 |
34 | #define ST_SENSORS_DEFAULT_POWER_OFF_VALUE 0x00 | |
35 | #define ST_SENSORS_DEFAULT_WAI_ADDRESS 0x0f | |
36 | #define ST_SENSORS_DEFAULT_AXIS_ADDR 0x20 | |
37 | #define ST_SENSORS_DEFAULT_AXIS_MASK 0x07 | |
38 | #define ST_SENSORS_DEFAULT_AXIS_N_BIT 3 | |
97865fe4 | 39 | #define ST_SENSORS_DEFAULT_STAT_ADDR 0x27 |
23491b51 DC |
40 | |
41 | #define ST_SENSORS_MAX_NAME 17 | |
49b62373 | 42 | #define ST_SENSORS_MAX_4WAI 8 |
23491b51 | 43 | |
762011d6 DC |
44 | #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \ |
45 | ch2, s, endian, rbits, sbits, addr) \ | |
23491b51 DC |
46 | { \ |
47 | .type = device_type, \ | |
762011d6 DC |
48 | .modified = mod, \ |
49 | .info_mask_separate = mask, \ | |
2d239c9e | 50 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
23491b51 | 51 | .scan_index = index, \ |
762011d6 | 52 | .channel2 = ch2, \ |
23491b51 DC |
53 | .address = addr, \ |
54 | .scan_type = { \ | |
762011d6 DC |
55 | .sign = s, \ |
56 | .realbits = rbits, \ | |
57 | .shift = sbits - rbits, \ | |
58 | .storagebits = sbits, \ | |
23491b51 DC |
59 | .endianness = endian, \ |
60 | }, \ | |
61 | } | |
62 | ||
23491b51 DC |
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 | ||
a7b8829d LB |
107 | struct st_sensor_sim { |
108 | u8 addr; | |
109 | u8 value; | |
110 | }; | |
111 | ||
23491b51 DC |
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 | ||
65e4345c LW |
122 | /** |
123 | * struct st_sensor_das - ST sensor device data alignment selection | |
124 | * @addr: address of the register. | |
125 | * @mask: mask to write the das flag for left alignment. | |
126 | */ | |
127 | struct st_sensor_das { | |
128 | u8 addr; | |
129 | u8 mask; | |
130 | }; | |
131 | ||
a542f9a0 LB |
132 | /** |
133 | * struct st_sensor_int_drdy - ST sensor device drdy line parameters | |
134 | * @addr: address of INT drdy register. | |
135 | * @mask: mask to enable drdy line. | |
136 | * @addr_od: address to enable/disable Open Drain on the INT line. | |
137 | * @mask_od: mask to enable/disable Open Drain on the INT line. | |
138 | */ | |
139 | struct st_sensor_int_drdy { | |
140 | u8 addr; | |
141 | u8 mask; | |
142 | u8 addr_od; | |
143 | u8 mask_od; | |
144 | }; | |
145 | ||
23491b51 DC |
146 | /** |
147 | * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt | |
75d4c6d2 LB |
148 | * struct int1 - data-ready configuration register for INT1 pin. |
149 | * struct int2 - data-ready configuration register for INT2 pin. | |
a9fd053b LW |
150 | * @addr_ihl: address to enable/disable active low on the INT lines. |
151 | * @mask_ihl: mask to enable/disable active low on the INT lines. | |
e72a0601 | 152 | * struct stat_drdy - status register of DRDY (data ready) interrupt. |
23491b51 DC |
153 | * struct ig1 - represents the Interrupt Generator 1 of sensors. |
154 | * @en_addr: address of the enable ig1 register. | |
155 | * @en_mask: mask to write the on/off value for enable. | |
156 | */ | |
157 | struct st_sensor_data_ready_irq { | |
a542f9a0 LB |
158 | struct st_sensor_int_drdy int1; |
159 | struct st_sensor_int_drdy int2; | |
a9fd053b LW |
160 | u8 addr_ihl; |
161 | u8 mask_ihl; | |
e72a0601 LB |
162 | struct { |
163 | u8 addr; | |
164 | u8 mask; | |
165 | } stat_drdy; | |
23491b51 DC |
166 | struct { |
167 | u8 en_addr; | |
168 | u8 en_mask; | |
169 | } ig1; | |
170 | }; | |
171 | ||
172 | /** | |
173 | * struct st_sensor_transfer_buffer - ST sensor device I/O buffer | |
174 | * @buf_lock: Mutex to protect rx and tx buffers. | |
175 | * @tx_buf: Buffer used by SPI transfer function to send data to the sensors. | |
176 | * This buffer is used to avoid DMA not-aligned issue. | |
177 | * @rx_buf: Buffer used by SPI transfer to receive data from sensors. | |
178 | * This buffer is used to avoid DMA not-aligned issue. | |
179 | */ | |
180 | struct st_sensor_transfer_buffer { | |
181 | struct mutex buf_lock; | |
182 | u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH]; | |
183 | u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned; | |
184 | }; | |
185 | ||
186 | /** | |
187 | * struct st_sensor_transfer_function - ST sensor device I/O function | |
188 | * @read_byte: Function used to read one byte. | |
189 | * @write_byte: Function used to write one byte. | |
190 | * @read_multiple_byte: Function used to read multiple byte. | |
191 | */ | |
192 | struct st_sensor_transfer_function { | |
193 | int (*read_byte) (struct st_sensor_transfer_buffer *tb, | |
194 | struct device *dev, u8 reg_addr, u8 *res_byte); | |
195 | int (*write_byte) (struct st_sensor_transfer_buffer *tb, | |
196 | struct device *dev, u8 reg_addr, u8 data); | |
197 | int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb, | |
198 | struct device *dev, u8 reg_addr, int len, u8 *data, | |
199 | bool multiread_bit); | |
200 | }; | |
201 | ||
202 | /** | |
a7ee8839 | 203 | * struct st_sensor_settings - ST specific sensor settings |
23491b51 | 204 | * @wai: Contents of WhoAmI register. |
bc27381e | 205 | * @wai_addr: The address of WhoAmI register. |
23491b51 DC |
206 | * @sensors_supported: List of supported sensors by struct itself. |
207 | * @ch: IIO channels for the sensor. | |
208 | * @odr: Output data rate register and ODR list available. | |
209 | * @pw: Power register of the sensor. | |
210 | * @enable_axis: Enable one or more axis of the sensor. | |
211 | * @fs: Full scale register and full scale list available. | |
212 | * @bdu: Block data update register. | |
65e4345c | 213 | * @das: Data Alignment Selection register. |
23491b51 | 214 | * @drdy_irq: Data ready register of the sensor. |
a7b8829d | 215 | * @sim: SPI serial interface mode register of the sensor. |
23491b51 DC |
216 | * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read. |
217 | * @bootime: samples to discard when sensor passing from power-down to power-up. | |
218 | */ | |
a7ee8839 | 219 | struct st_sensor_settings { |
23491b51 | 220 | u8 wai; |
bc27381e | 221 | u8 wai_addr; |
23491b51 DC |
222 | char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME]; |
223 | struct iio_chan_spec *ch; | |
ea01f2c1 | 224 | int num_ch; |
23491b51 DC |
225 | struct st_sensor_odr odr; |
226 | struct st_sensor_power pw; | |
227 | struct st_sensor_axis enable_axis; | |
228 | struct st_sensor_fullscale fs; | |
229 | struct st_sensor_bdu bdu; | |
65e4345c | 230 | struct st_sensor_das das; |
23491b51 | 231 | struct st_sensor_data_ready_irq drdy_irq; |
a7b8829d | 232 | struct st_sensor_sim sim; |
23491b51 DC |
233 | bool multi_read_bit; |
234 | unsigned int bootime; | |
235 | }; | |
236 | ||
237 | /** | |
238 | * struct st_sensor_data - ST sensor device status | |
239 | * @dev: Pointer to instance of struct device (I2C or SPI). | |
240 | * @trig: The trigger in use by the core driver. | |
a7ee8839 | 241 | * @sensor_settings: Pointer to the specific sensor settings in use. |
23491b51 | 242 | * @current_fullscale: Maximum range of measure by the sensor. |
77448761 | 243 | * @vdd: Pointer to sensor's Vdd power supply |
71e1980c | 244 | * @vdd_io: Pointer to sensor's Vdd-IO power supply |
23491b51 DC |
245 | * @enabled: Status of the sensor (false->off, true->on). |
246 | * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread. | |
247 | * @buffer_data: Data used by buffer part. | |
248 | * @odr: Output data rate of the sensor [Hz]. | |
607a568a | 249 | * num_data_channels: Number of data channels used in buffer. |
23cde4d6 | 250 | * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2). |
0e6f6871 | 251 | * @int_pin_open_drain: Set the interrupt/DRDY to open drain. |
23491b51 DC |
252 | * @get_irq_data_ready: Function to get the IRQ used for data ready signal. |
253 | * @tf: Transfer function structure used by I/O operations. | |
254 | * @tb: Transfer buffers and mutex used by I/O operations. | |
90efe055 | 255 | * @edge_irq: the IRQ triggers on edges and need special handling. |
65925b65 LW |
256 | * @hw_irq_trigger: if we're using the hardware interrupt on the sensor. |
257 | * @hw_timestamp: Latest timestamp from the interrupt handler, when in use. | |
23491b51 DC |
258 | */ |
259 | struct st_sensor_data { | |
260 | struct device *dev; | |
261 | struct iio_trigger *trig; | |
f38ab20b | 262 | struct iio_mount_matrix *mount_matrix; |
a7ee8839 | 263 | struct st_sensor_settings *sensor_settings; |
23491b51 | 264 | struct st_sensor_fullscale_avl *current_fullscale; |
77448761 | 265 | struct regulator *vdd; |
71e1980c | 266 | struct regulator *vdd_io; |
23491b51 DC |
267 | |
268 | bool enabled; | |
269 | bool multiread_bit; | |
270 | ||
271 | char *buffer_data; | |
272 | ||
273 | unsigned int odr; | |
607a568a | 274 | unsigned int num_data_channels; |
23491b51 | 275 | |
23cde4d6 | 276 | u8 drdy_int_pin; |
0e6f6871 | 277 | bool int_pin_open_drain; |
23cde4d6 | 278 | |
23491b51 DC |
279 | unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev); |
280 | ||
281 | const struct st_sensor_transfer_function *tf; | |
282 | struct st_sensor_transfer_buffer tb; | |
65925b65 | 283 | |
90efe055 | 284 | bool edge_irq; |
65925b65 LW |
285 | bool hw_irq_trigger; |
286 | s64 hw_timestamp; | |
23491b51 DC |
287 | }; |
288 | ||
289 | #ifdef CONFIG_IIO_BUFFER | |
5a4d7291 | 290 | irqreturn_t st_sensors_trigger_handler(int irq, void *p); |
5a4d7291 GR |
291 | #endif |
292 | ||
293 | #ifdef CONFIG_IIO_TRIGGER | |
23491b51 DC |
294 | int st_sensors_allocate_trigger(struct iio_dev *indio_dev, |
295 | const struct iio_trigger_ops *trigger_ops); | |
296 | ||
297 | void st_sensors_deallocate_trigger(struct iio_dev *indio_dev); | |
65925b65 LW |
298 | int st_sensors_validate_device(struct iio_trigger *trig, |
299 | struct iio_dev *indio_dev); | |
91ffbabf DC |
300 | #else |
301 | static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev, | |
302 | const struct iio_trigger_ops *trigger_ops) | |
303 | { | |
304 | return 0; | |
305 | } | |
306 | static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev) | |
307 | { | |
308 | return; | |
309 | } | |
65925b65 | 310 | #define st_sensors_validate_device NULL |
23491b51 DC |
311 | #endif |
312 | ||
23cde4d6 DC |
313 | int st_sensors_init_sensor(struct iio_dev *indio_dev, |
314 | struct st_sensors_platform_data *pdata); | |
23491b51 DC |
315 | |
316 | int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable); | |
317 | ||
318 | int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable); | |
319 | ||
14f295c8 | 320 | int st_sensors_power_enable(struct iio_dev *indio_dev); |
ea7e586b LW |
321 | |
322 | void st_sensors_power_disable(struct iio_dev *indio_dev); | |
323 | ||
a0175b9c LW |
324 | int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev, |
325 | unsigned reg, unsigned writeval, | |
326 | unsigned *readval); | |
327 | ||
23491b51 DC |
328 | int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr); |
329 | ||
330 | int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable); | |
331 | ||
332 | int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale); | |
333 | ||
334 | int st_sensors_read_info_raw(struct iio_dev *indio_dev, | |
335 | struct iio_chan_spec const *ch, int *val); | |
336 | ||
337 | int st_sensors_check_device_support(struct iio_dev *indio_dev, | |
a7ee8839 | 338 | int num_sensors_list, const struct st_sensor_settings *sensor_settings); |
23491b51 | 339 | |
23491b51 DC |
340 | ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev, |
341 | struct device_attribute *attr, char *buf); | |
342 | ||
343 | ssize_t st_sensors_sysfs_scale_avail(struct device *dev, | |
344 | struct device_attribute *attr, char *buf); | |
345 | ||
250bbbdb LB |
346 | #ifdef CONFIG_OF |
347 | void st_sensors_of_name_probe(struct device *dev, | |
348 | const struct of_device_id *match, | |
349 | char *name, int len); | |
350 | #else | |
351 | static inline void st_sensors_of_name_probe(struct device *dev, | |
352 | const struct of_device_id *match, | |
353 | char *name, int len) | |
354 | { | |
355 | } | |
356 | #endif | |
357 | ||
23491b51 | 358 | #endif /* ST_SENSORS_H */ |