• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * STMicroelectronics sensors library driver
4  *
5  * Copyright 2012-2013 STMicroelectronics Inc.
6  *
7  * Denis Ciocca <denis.ciocca@st.com>
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>
17 #include <linux/bitops.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/regmap.h>
20 
21 #include <linux/platform_data/st_sensors_pdata.h>
22 
23 /*
24  * Buffer size max case: 2bytes per channel, 3 channels in total +
25  *			 8bytes timestamp channel (s64)
26  */
27 #define ST_SENSORS_MAX_BUFFER_SIZE		(ALIGN(2 * 3, sizeof(s64)) + \
28 						 sizeof(s64))
29 
30 #define ST_SENSORS_ODR_LIST_MAX			10
31 #define ST_SENSORS_FULLSCALE_AVL_MAX		10
32 
33 #define ST_SENSORS_NUMBER_ALL_CHANNELS		4
34 #define ST_SENSORS_ENABLE_ALL_AXIS		0x07
35 #define ST_SENSORS_SCAN_X			0
36 #define ST_SENSORS_SCAN_Y			1
37 #define ST_SENSORS_SCAN_Z			2
38 #define ST_SENSORS_DEFAULT_POWER_ON_VALUE	0x01
39 #define ST_SENSORS_DEFAULT_POWER_OFF_VALUE	0x00
40 #define ST_SENSORS_DEFAULT_WAI_ADDRESS		0x0f
41 #define ST_SENSORS_DEFAULT_AXIS_ADDR		0x20
42 #define ST_SENSORS_DEFAULT_AXIS_MASK		0x07
43 #define ST_SENSORS_DEFAULT_AXIS_N_BIT		3
44 #define ST_SENSORS_DEFAULT_STAT_ADDR		0x27
45 
46 #define ST_SENSORS_MAX_NAME			17
47 #define ST_SENSORS_MAX_4WAI			8
48 
49 #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
50 					ch2, s, endian, rbits, sbits, addr) \
51 { \
52 	.type = device_type, \
53 	.modified = mod, \
54 	.info_mask_separate = mask, \
55 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
56 	.scan_index = index, \
57 	.channel2 = ch2, \
58 	.address = addr, \
59 	.scan_type = { \
60 		.sign = s, \
61 		.realbits = rbits, \
62 		.shift = sbits - rbits, \
63 		.storagebits = sbits, \
64 		.endianness = endian, \
65 	}, \
66 }
67 
68 #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
69 		IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
70 			st_sensors_sysfs_sampling_frequency_avail)
71 
72 #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
73 		IIO_DEVICE_ATTR(name, S_IRUGO, \
74 			st_sensors_sysfs_scale_avail, NULL , 0);
75 
76 struct st_sensor_odr_avl {
77 	unsigned int hz;
78 	u8 value;
79 };
80 
81 struct st_sensor_odr {
82 	u8 addr;
83 	u8 mask;
84 	struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
85 };
86 
87 struct st_sensor_power {
88 	u8 addr;
89 	u8 mask;
90 	u8 value_off;
91 	u8 value_on;
92 };
93 
94 struct st_sensor_axis {
95 	u8 addr;
96 	u8 mask;
97 };
98 
99 struct st_sensor_fullscale_avl {
100 	unsigned int num;
101 	u8 value;
102 	unsigned int gain;
103 	unsigned int gain2;
104 };
105 
106 struct st_sensor_fullscale {
107 	u8 addr;
108 	u8 mask;
109 	struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
110 };
111 
112 struct st_sensor_sim {
113 	u8 addr;
114 	u8 value;
115 };
116 
117 /**
118  * struct st_sensor_bdu - ST sensor device block data update
119  * @addr: address of the register.
120  * @mask: mask to write the block data update flag.
121  */
122 struct st_sensor_bdu {
123 	u8 addr;
124 	u8 mask;
125 };
126 
127 /**
128  * struct st_sensor_das - ST sensor device data alignment selection
129  * @addr: address of the register.
130  * @mask: mask to write the das flag for left alignment.
131  */
132 struct st_sensor_das {
133 	u8 addr;
134 	u8 mask;
135 };
136 
137 /**
138  * struct st_sensor_int_drdy - ST sensor device drdy line parameters
139  * @addr: address of INT drdy register.
140  * @mask: mask to enable drdy line.
141  * @addr_od: address to enable/disable Open Drain on the INT line.
142  * @mask_od: mask to enable/disable Open Drain on the INT line.
143  */
144 struct st_sensor_int_drdy {
145 	u8 addr;
146 	u8 mask;
147 	u8 addr_od;
148 	u8 mask_od;
149 };
150 
151 /**
152  * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
153  * struct int1 - data-ready configuration register for INT1 pin.
154  * struct int2 - data-ready configuration register for INT2 pin.
155  * @addr_ihl: address to enable/disable active low on the INT lines.
156  * @mask_ihl: mask to enable/disable active low on the INT lines.
157  * struct stat_drdy - status register of DRDY (data ready) interrupt.
158  * struct ig1 - represents the Interrupt Generator 1 of sensors.
159  * @en_addr: address of the enable ig1 register.
160  * @en_mask: mask to write the on/off value for enable.
161  */
162 struct st_sensor_data_ready_irq {
163 	struct st_sensor_int_drdy int1;
164 	struct st_sensor_int_drdy int2;
165 	u8 addr_ihl;
166 	u8 mask_ihl;
167 	struct {
168 		u8 addr;
169 		u8 mask;
170 	} stat_drdy;
171 	struct {
172 		u8 en_addr;
173 		u8 en_mask;
174 	} ig1;
175 };
176 
177 /**
178  * struct st_sensor_settings - ST specific sensor settings
179  * @wai: Contents of WhoAmI register.
180  * @wai_addr: The address of WhoAmI register.
181  * @sensors_supported: List of supported sensors by struct itself.
182  * @ch: IIO channels for the sensor.
183  * @odr: Output data rate register and ODR list available.
184  * @pw: Power register of the sensor.
185  * @enable_axis: Enable one or more axis of the sensor.
186  * @fs: Full scale register and full scale list available.
187  * @bdu: Block data update register.
188  * @das: Data Alignment Selection register.
189  * @drdy_irq: Data ready register of the sensor.
190  * @sim: SPI serial interface mode register of the sensor.
191  * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
192  * @bootime: samples to discard when sensor passing from power-down to power-up.
193  */
194 struct st_sensor_settings {
195 	u8 wai;
196 	u8 wai_addr;
197 	char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
198 	struct iio_chan_spec *ch;
199 	int num_ch;
200 	struct st_sensor_odr odr;
201 	struct st_sensor_power pw;
202 	struct st_sensor_axis enable_axis;
203 	struct st_sensor_fullscale fs;
204 	struct st_sensor_bdu bdu;
205 	struct st_sensor_das das;
206 	struct st_sensor_data_ready_irq drdy_irq;
207 	struct st_sensor_sim sim;
208 	bool multi_read_bit;
209 	unsigned int bootime;
210 };
211 
212 /**
213  * struct st_sensor_data - ST sensor device status
214  * @dev: Pointer to instance of struct device (I2C or SPI).
215  * @trig: The trigger in use by the core driver.
216  * @sensor_settings: Pointer to the specific sensor settings in use.
217  * @current_fullscale: Maximum range of measure by the sensor.
218  * @vdd: Pointer to sensor's Vdd power supply
219  * @vdd_io: Pointer to sensor's Vdd-IO power supply
220  * @regmap: Pointer to specific sensor regmap configuration.
221  * @enabled: Status of the sensor (false->off, true->on).
222  * @odr: Output data rate of the sensor [Hz].
223  * num_data_channels: Number of data channels used in buffer.
224  * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
225  * @int_pin_open_drain: Set the interrupt/DRDY to open drain.
226  * @irq: the IRQ number.
227  * @edge_irq: the IRQ triggers on edges and need special handling.
228  * @hw_irq_trigger: if we're using the hardware interrupt on the sensor.
229  * @hw_timestamp: Latest timestamp from the interrupt handler, when in use.
230  * @buffer_data: Data used by buffer part.
231  * @odr_lock: Local lock for preventing concurrent ODR accesses/changes
232  */
233 struct st_sensor_data {
234 	struct device *dev;
235 	struct iio_trigger *trig;
236 	struct iio_mount_matrix *mount_matrix;
237 	struct st_sensor_settings *sensor_settings;
238 	struct st_sensor_fullscale_avl *current_fullscale;
239 	struct regulator *vdd;
240 	struct regulator *vdd_io;
241 	struct regmap *regmap;
242 
243 	bool enabled;
244 
245 	unsigned int odr;
246 	unsigned int num_data_channels;
247 
248 	u8 drdy_int_pin;
249 	bool int_pin_open_drain;
250 	int irq;
251 
252 	bool edge_irq;
253 	bool hw_irq_trigger;
254 	s64 hw_timestamp;
255 
256 	char buffer_data[ST_SENSORS_MAX_BUFFER_SIZE] ____cacheline_aligned;
257 
258 	struct mutex odr_lock;
259 };
260 
261 #ifdef CONFIG_IIO_BUFFER
262 irqreturn_t st_sensors_trigger_handler(int irq, void *p);
263 #endif
264 
265 #ifdef CONFIG_IIO_TRIGGER
266 int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
267 				const struct iio_trigger_ops *trigger_ops);
268 
269 void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
270 int st_sensors_validate_device(struct iio_trigger *trig,
271 			       struct iio_dev *indio_dev);
272 #else
st_sensors_allocate_trigger(struct iio_dev * indio_dev,const struct iio_trigger_ops * trigger_ops)273 static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
274 				const struct iio_trigger_ops *trigger_ops)
275 {
276 	return 0;
277 }
st_sensors_deallocate_trigger(struct iio_dev * indio_dev)278 static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
279 {
280 	return;
281 }
282 #define st_sensors_validate_device NULL
283 #endif
284 
285 int st_sensors_init_sensor(struct iio_dev *indio_dev,
286 					struct st_sensors_platform_data *pdata);
287 
288 int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
289 
290 int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
291 
292 int st_sensors_power_enable(struct iio_dev *indio_dev);
293 
294 void st_sensors_power_disable(struct iio_dev *indio_dev);
295 
296 int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
297 				  unsigned reg, unsigned writeval,
298 				  unsigned *readval);
299 
300 int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
301 
302 int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
303 
304 int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
305 
306 int st_sensors_read_info_raw(struct iio_dev *indio_dev,
307 				struct iio_chan_spec const *ch, int *val);
308 
309 int st_sensors_get_settings_index(const char *name,
310 				  const struct st_sensor_settings *list,
311 				  const int list_length);
312 
313 int st_sensors_verify_id(struct iio_dev *indio_dev);
314 
315 ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
316 				struct device_attribute *attr, char *buf);
317 
318 ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
319 				struct device_attribute *attr, char *buf);
320 
321 void st_sensors_dev_name_probe(struct device *dev, char *name, int len);
322 
323 #endif /* ST_SENSORS_H */
324