xref: /linux/drivers/iio/common/ssp_sensors/ssp_iio_sensor.h (revision 498495dba268b20e8eadd7fe93c140c68b6cc9d2)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __SSP_IIO_SENSOR_H__
3 #define __SSP_IIO_SENSOR_H__
4 
5 #define SSP_CHANNEL_AG(_type, _mod, _index) \
6 { \
7 		.type = _type,\
8 		.modified = 1,\
9 		.channel2 = _mod,\
10 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
11 		.scan_index = _index,\
12 		.scan_type = {\
13 			.sign = 's',\
14 			.realbits = 16,\
15 			.storagebits = 16,\
16 			.shift = 0,\
17 			.endianness = IIO_LE,\
18 		},\
19 }
20 
21 /* It is defined here as it is a mixed timestamp */
22 #define SSP_CHAN_TIMESTAMP(_si) {					\
23 	.type = IIO_TIMESTAMP,						\
24 	.channel = -1,							\
25 	.scan_index = _si,						\
26 	.scan_type = {							\
27 		.sign = 's',						\
28 		.realbits = 64,						\
29 		.storagebits = 64,					\
30 		},							\
31 }
32 
33 #define SSP_MS_PER_S			1000
34 #define SSP_INVERTED_SCALING_FACTOR	1000000U
35 
36 #define SSP_FACTOR_WITH_MS \
37 	(SSP_INVERTED_SCALING_FACTOR * SSP_MS_PER_S)
38 
39 int ssp_common_buffer_postenable(struct iio_dev *indio_dev);
40 
41 int ssp_common_buffer_postdisable(struct iio_dev *indio_dev);
42 
43 int ssp_common_process_data(struct iio_dev *indio_dev, void *buf,
44 			    unsigned int len, int64_t timestamp);
45 
46 /* Converts time in ms to frequency */
ssp_convert_to_freq(u32 time,int * integer_part,int * fractional)47 static inline void ssp_convert_to_freq(u32 time, int *integer_part,
48 				       int *fractional)
49 {
50 	if (time == 0) {
51 		*fractional = 0;
52 		*integer_part = 0;
53 		return;
54 	}
55 
56 	*integer_part = SSP_FACTOR_WITH_MS / time;
57 	*fractional = *integer_part % SSP_INVERTED_SCALING_FACTOR;
58 	*integer_part = *integer_part / SSP_INVERTED_SCALING_FACTOR;
59 }
60 
61 /* Converts frequency to time in ms */
ssp_convert_to_time(int integer_part,int fractional)62 static inline int ssp_convert_to_time(int integer_part, int fractional)
63 {
64 	u64 value;
65 
66 	value = (u64)integer_part * SSP_INVERTED_SCALING_FACTOR + fractional;
67 	if (value == 0)
68 		return 0;
69 
70 	return div64_u64((u64)SSP_FACTOR_WITH_MS, value);
71 }
72 #endif /* __SSP_IIO_SENSOR_H__ */
73