1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics magnetometers driver
4  *
5  * Copyright 2012-2013 STMicroelectronics Inc.
6  *
7  * Denis Ciocca <denis.ciocca@st.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/sysfs.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/trigger.h>
17 
18 #include <linux/iio/common/st_sensors.h>
19 #include "st_magn.h"
20 
21 #define ST_MAGN_NUMBER_DATA_CHANNELS		3
22 
23 /* DEFAULT VALUE FOR SENSORS */
24 #define ST_MAGN_DEFAULT_OUT_X_H_ADDR		0x03
25 #define ST_MAGN_DEFAULT_OUT_Y_H_ADDR		0x07
26 #define ST_MAGN_DEFAULT_OUT_Z_H_ADDR		0x05
27 
28 /* FULLSCALE */
29 #define ST_MAGN_FS_AVL_1300MG			1300
30 #define ST_MAGN_FS_AVL_1900MG			1900
31 #define ST_MAGN_FS_AVL_2000MG			2000
32 #define ST_MAGN_FS_AVL_2500MG			2500
33 #define ST_MAGN_FS_AVL_4000MG			4000
34 #define ST_MAGN_FS_AVL_4700MG			4700
35 #define ST_MAGN_FS_AVL_5600MG			5600
36 #define ST_MAGN_FS_AVL_8000MG			8000
37 #define ST_MAGN_FS_AVL_8100MG			8100
38 #define ST_MAGN_FS_AVL_12000MG			12000
39 #define ST_MAGN_FS_AVL_15000MG			15000
40 #define ST_MAGN_FS_AVL_16000MG			16000
41 
42 /* Special L addresses for Sensor 2 */
43 #define ST_MAGN_2_OUT_X_L_ADDR			0x28
44 #define ST_MAGN_2_OUT_Y_L_ADDR			0x2a
45 #define ST_MAGN_2_OUT_Z_L_ADDR			0x2c
46 
47 /* Special L addresses for sensor 3 */
48 #define ST_MAGN_3_OUT_X_L_ADDR			0x68
49 #define ST_MAGN_3_OUT_Y_L_ADDR			0x6a
50 #define ST_MAGN_3_OUT_Z_L_ADDR			0x6c
51 
52 /* Special L addresses for sensor 4 */
53 #define ST_MAGN_4_OUT_X_L_ADDR			0x08
54 #define ST_MAGN_4_OUT_Y_L_ADDR			0x0a
55 #define ST_MAGN_4_OUT_Z_L_ADDR			0x0c
56 
57 static const struct iio_mount_matrix *
58 st_magn_get_mount_matrix(const struct iio_dev *indio_dev,
59 			 const struct iio_chan_spec *chan)
60 {
61 	struct st_sensor_data *mdata = iio_priv(indio_dev);
62 
63 	return &mdata->mount_matrix;
64 }
65 
66 static const struct iio_chan_spec_ext_info st_magn_mount_matrix_ext_info[] = {
67 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, st_magn_get_mount_matrix),
68 	{ }
69 };
70 
71 static const struct iio_chan_spec st_magn_16bit_channels[] = {
72 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN,
73 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
74 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_BE, 16, 16,
75 			ST_MAGN_DEFAULT_OUT_X_H_ADDR,
76 			st_magn_mount_matrix_ext_info),
77 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN,
78 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
79 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_BE, 16, 16,
80 			ST_MAGN_DEFAULT_OUT_Y_H_ADDR,
81 			st_magn_mount_matrix_ext_info),
82 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN,
83 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
84 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_BE, 16, 16,
85 			ST_MAGN_DEFAULT_OUT_Z_H_ADDR,
86 			st_magn_mount_matrix_ext_info),
87 	IIO_CHAN_SOFT_TIMESTAMP(3)
88 };
89 
90 static const struct iio_chan_spec st_magn_2_16bit_channels[] = {
91 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN,
92 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
93 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
94 			ST_MAGN_2_OUT_X_L_ADDR,
95 			st_magn_mount_matrix_ext_info),
96 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN,
97 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
98 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
99 			ST_MAGN_2_OUT_Y_L_ADDR,
100 			st_magn_mount_matrix_ext_info),
101 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN,
102 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
103 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
104 			ST_MAGN_2_OUT_Z_L_ADDR,
105 			st_magn_mount_matrix_ext_info),
106 	IIO_CHAN_SOFT_TIMESTAMP(3)
107 };
108 
109 static const struct iio_chan_spec st_magn_3_16bit_channels[] = {
110 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN,
111 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
112 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
113 			ST_MAGN_3_OUT_X_L_ADDR,
114 			st_magn_mount_matrix_ext_info),
115 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN,
116 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
117 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
118 			ST_MAGN_3_OUT_Y_L_ADDR,
119 			st_magn_mount_matrix_ext_info),
120 	ST_SENSORS_LSM_CHANNELS_EXT(IIO_MAGN,
121 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
122 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
123 			ST_MAGN_3_OUT_Z_L_ADDR,
124 			st_magn_mount_matrix_ext_info),
125 	IIO_CHAN_SOFT_TIMESTAMP(3)
126 };
127 
128 static const struct iio_chan_spec st_magn_4_16bit_channels[] = {
129 	ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
130 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
131 			ST_SENSORS_SCAN_X, 1, IIO_MOD_X, 's', IIO_LE, 16, 16,
132 			ST_MAGN_4_OUT_X_L_ADDR),
133 	ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
134 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
135 			ST_SENSORS_SCAN_Y, 1, IIO_MOD_Y, 's', IIO_LE, 16, 16,
136 			ST_MAGN_4_OUT_Y_L_ADDR),
137 	ST_SENSORS_LSM_CHANNELS(IIO_MAGN,
138 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
139 			ST_SENSORS_SCAN_Z, 1, IIO_MOD_Z, 's', IIO_LE, 16, 16,
140 			ST_MAGN_4_OUT_Z_L_ADDR),
141 	IIO_CHAN_SOFT_TIMESTAMP(3)
142 };
143 
144 static const struct st_sensor_settings st_magn_sensors_settings[] = {
145 	{
146 		.wai = 0, /* This sensor has no valid WhoAmI report 0 */
147 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
148 		.sensors_supported = {
149 			[0] = LSM303DLH_MAGN_DEV_NAME,
150 		},
151 		.ch = (struct iio_chan_spec *)st_magn_16bit_channels,
152 		.odr = {
153 			.addr = 0x00,
154 			.mask = 0x1c,
155 			.odr_avl = {
156 				{ .hz = 1, .value = 0x00 },
157 				{ .hz = 2, .value = 0x01 },
158 				{ .hz = 3, .value = 0x02 },
159 				{ .hz = 8, .value = 0x03 },
160 				{ .hz = 15, .value = 0x04 },
161 				{ .hz = 30, .value = 0x05 },
162 				{ .hz = 75, .value = 0x06 },
163 				/* 220 Hz, 0x07 reportedly exist */
164 			},
165 		},
166 		.pw = {
167 			.addr = 0x02,
168 			.mask = 0x03,
169 			.value_on = 0x00,
170 			.value_off = 0x03,
171 		},
172 		.fs = {
173 			.addr = 0x01,
174 			.mask = 0xe0,
175 			.fs_avl = {
176 				[0] = {
177 					.num = ST_MAGN_FS_AVL_1300MG,
178 					.value = 0x01,
179 					.gain = 1100,
180 					.gain2 = 980,
181 				},
182 				[1] = {
183 					.num = ST_MAGN_FS_AVL_1900MG,
184 					.value = 0x02,
185 					.gain = 855,
186 					.gain2 = 760,
187 				},
188 				[2] = {
189 					.num = ST_MAGN_FS_AVL_2500MG,
190 					.value = 0x03,
191 					.gain = 670,
192 					.gain2 = 600,
193 				},
194 				[3] = {
195 					.num = ST_MAGN_FS_AVL_4000MG,
196 					.value = 0x04,
197 					.gain = 450,
198 					.gain2 = 400,
199 				},
200 				[4] = {
201 					.num = ST_MAGN_FS_AVL_4700MG,
202 					.value = 0x05,
203 					.gain = 400,
204 					.gain2 = 355,
205 				},
206 				[5] = {
207 					.num = ST_MAGN_FS_AVL_5600MG,
208 					.value = 0x06,
209 					.gain = 330,
210 					.gain2 = 295,
211 				},
212 				[6] = {
213 					.num = ST_MAGN_FS_AVL_8100MG,
214 					.value = 0x07,
215 					.gain = 230,
216 					.gain2 = 205,
217 				},
218 			},
219 		},
220 		.multi_read_bit = false,
221 		.bootime = 2,
222 	},
223 	{
224 		.wai = 0x3c,
225 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
226 		.sensors_supported = {
227 			[0] = LSM303DLHC_MAGN_DEV_NAME,
228 			[1] = LSM303DLM_MAGN_DEV_NAME,
229 		},
230 		.ch = (struct iio_chan_spec *)st_magn_16bit_channels,
231 		.odr = {
232 			.addr = 0x00,
233 			.mask = 0x1c,
234 			.odr_avl = {
235 				{ .hz = 1, .value = 0x00 },
236 				{ .hz = 2, .value = 0x01 },
237 				{ .hz = 3, .value = 0x02 },
238 				{ .hz = 8, .value = 0x03 },
239 				{ .hz = 15, .value = 0x04 },
240 				{ .hz = 30, .value = 0x05 },
241 				{ .hz = 75, .value = 0x06 },
242 				{ .hz = 220, .value = 0x07 },
243 			},
244 		},
245 		.pw = {
246 			.addr = 0x02,
247 			.mask = 0x03,
248 			.value_on = 0x00,
249 			.value_off = 0x03,
250 		},
251 		.fs = {
252 			.addr = 0x01,
253 			.mask = 0xe0,
254 			.fs_avl = {
255 				[0] = {
256 					.num = ST_MAGN_FS_AVL_1300MG,
257 					.value = 0x01,
258 					.gain = 909,
259 					.gain2 = 1020,
260 				},
261 				[1] = {
262 					.num = ST_MAGN_FS_AVL_1900MG,
263 					.value = 0x02,
264 					.gain = 1169,
265 					.gain2 = 1315,
266 				},
267 				[2] = {
268 					.num = ST_MAGN_FS_AVL_2500MG,
269 					.value = 0x03,
270 					.gain = 1492,
271 					.gain2 = 1666,
272 				},
273 				[3] = {
274 					.num = ST_MAGN_FS_AVL_4000MG,
275 					.value = 0x04,
276 					.gain = 2222,
277 					.gain2 = 2500,
278 				},
279 				[4] = {
280 					.num = ST_MAGN_FS_AVL_4700MG,
281 					.value = 0x05,
282 					.gain = 2500,
283 					.gain2 = 2816,
284 				},
285 				[5] = {
286 					.num = ST_MAGN_FS_AVL_5600MG,
287 					.value = 0x06,
288 					.gain = 3030,
289 					.gain2 = 3389,
290 				},
291 				[6] = {
292 					.num = ST_MAGN_FS_AVL_8100MG,
293 					.value = 0x07,
294 					.gain = 4347,
295 					.gain2 = 4878,
296 				},
297 			},
298 		},
299 		.multi_read_bit = false,
300 		.bootime = 2,
301 	},
302 	{
303 		.wai = 0x3d,
304 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
305 		.sensors_supported = {
306 			[0] = LIS3MDL_MAGN_DEV_NAME,
307 			[1] = LSM9DS1_MAGN_DEV_NAME,
308 		},
309 		.ch = (struct iio_chan_spec *)st_magn_2_16bit_channels,
310 		.odr = {
311 			.addr = 0x20,
312 			.mask = 0x1c,
313 			.odr_avl = {
314 				{ .hz = 1, .value = 0x00 },
315 				{ .hz = 2, .value = 0x01 },
316 				{ .hz = 3, .value = 0x02 },
317 				{ .hz = 5, .value = 0x03 },
318 				{ .hz = 10, .value = 0x04 },
319 				{ .hz = 20, .value = 0x05 },
320 				{ .hz = 40, .value = 0x06 },
321 				{ .hz = 80, .value = 0x07 },
322 			},
323 		},
324 		.pw = {
325 			.addr = 0x22,
326 			.mask = 0x03,
327 			.value_on = 0x00,
328 			.value_off = 0x03,
329 		},
330 		.fs = {
331 			.addr = 0x21,
332 			.mask = 0x60,
333 			.fs_avl = {
334 				[0] = {
335 					.num = ST_MAGN_FS_AVL_4000MG,
336 					.value = 0x00,
337 					.gain = 146,
338 				},
339 				[1] = {
340 					.num = ST_MAGN_FS_AVL_8000MG,
341 					.value = 0x01,
342 					.gain = 292,
343 				},
344 				[2] = {
345 					.num = ST_MAGN_FS_AVL_12000MG,
346 					.value = 0x02,
347 					.gain = 438,
348 				},
349 				[3] = {
350 					.num = ST_MAGN_FS_AVL_16000MG,
351 					.value = 0x03,
352 					.gain = 584,
353 				},
354 			},
355 		},
356 		.bdu = {
357 			.addr = 0x24,
358 			.mask = 0x40,
359 		},
360 		.drdy_irq = {
361 			/* drdy line is routed drdy pin */
362 			.stat_drdy = {
363 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
364 				.mask = 0x07,
365 			},
366 		},
367 		.sim = {
368 			.addr = 0x22,
369 			.value = BIT(2),
370 		},
371 		.multi_read_bit = true,
372 		.bootime = 2,
373 	},
374 	{
375 		.wai = 0x40,
376 		.wai_addr = 0x4f,
377 		.sensors_supported = {
378 			[0] = LSM303AGR_MAGN_DEV_NAME,
379 			[1] = LIS2MDL_MAGN_DEV_NAME,
380 			[2] = IIS2MDC_MAGN_DEV_NAME,
381 		},
382 		.ch = (struct iio_chan_spec *)st_magn_3_16bit_channels,
383 		.odr = {
384 			.addr = 0x60,
385 			.mask = 0x0c,
386 			.odr_avl = {
387 				{ .hz = 10, .value = 0x00 },
388 				{ .hz = 20, .value = 0x01 },
389 				{ .hz = 50, .value = 0x02 },
390 				{ .hz = 100, .value = 0x03 },
391 			},
392 		},
393 		.pw = {
394 			.addr = 0x60,
395 			.mask = 0x03,
396 			.value_on = 0x00,
397 			.value_off = 0x03,
398 		},
399 		.fs = {
400 			.fs_avl = {
401 				[0] = {
402 					.num = ST_MAGN_FS_AVL_15000MG,
403 					.gain = 1500,
404 				},
405 			},
406 		},
407 		.bdu = {
408 			.addr = 0x62,
409 			.mask = 0x10,
410 		},
411 		.drdy_irq = {
412 			.int1 = {
413 				.addr = 0x62,
414 				.mask = 0x01,
415 			},
416 			.stat_drdy = {
417 				.addr = 0x67,
418 				.mask = 0x07,
419 			},
420 		},
421 		.multi_read_bit = false,
422 		.bootime = 2,
423 	},
424 	{
425 		.wai = 0x49,
426 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
427 		.sensors_supported = {
428 			[0] = LSM9DS0_IMU_DEV_NAME,
429 		},
430 		.ch = (struct iio_chan_spec *)st_magn_4_16bit_channels,
431 		.odr = {
432 			.addr = 0x24,
433 			.mask = GENMASK(4, 2),
434 			.odr_avl = {
435 				{ 3, 0x00, },
436 				{ 6, 0x01, },
437 				{ 12, 0x02, },
438 				{ 25, 0x03, },
439 				{ 50, 0x04, },
440 				{ 100, 0x05, },
441 			},
442 		},
443 		.pw = {
444 			.addr = 0x26,
445 			.mask = GENMASK(1, 0),
446 			.value_on = 0x00,
447 			.value_off = 0x03,
448 		},
449 		.fs = {
450 			.addr = 0x25,
451 			.mask = GENMASK(6, 5),
452 			.fs_avl = {
453 				[0] = {
454 					.num = ST_MAGN_FS_AVL_2000MG,
455 					.value = 0x00,
456 					.gain = 73,
457 				},
458 				[1] = {
459 					.num = ST_MAGN_FS_AVL_4000MG,
460 					.value = 0x01,
461 					.gain = 146,
462 				},
463 				[2] = {
464 					.num = ST_MAGN_FS_AVL_8000MG,
465 					.value = 0x02,
466 					.gain = 292,
467 				},
468 				[3] = {
469 					.num = ST_MAGN_FS_AVL_12000MG,
470 					.value = 0x03,
471 					.gain = 438,
472 				},
473 			},
474 		},
475 		.bdu = {
476 			.addr = 0x20,
477 			.mask = BIT(3),
478 		},
479 		.drdy_irq = {
480 			.int1 = {
481 				.addr = 0x22,
482 				.mask = BIT(1),
483 			},
484 			.int2 = {
485 				.addr = 0x23,
486 				.mask = BIT(2),
487 			},
488 			.stat_drdy = {
489 				.addr = 0x07,
490 				.mask = GENMASK(2, 0),
491 			},
492 		},
493 		.sim = {
494 			.addr = 0x21,
495 			.value = BIT(0),
496 		},
497 		.multi_read_bit = true,
498 		.bootime = 2,
499 	},
500 };
501 
502 /* Default magn DRDY is available on INT2 pin */
503 static const struct st_sensors_platform_data default_magn_pdata = {
504 	.drdy_int_pin = 2,
505 };
506 
507 static int st_magn_read_raw(struct iio_dev *indio_dev,
508 			struct iio_chan_spec const *ch, int *val,
509 							int *val2, long mask)
510 {
511 	int err;
512 	struct st_sensor_data *mdata = iio_priv(indio_dev);
513 
514 	switch (mask) {
515 	case IIO_CHAN_INFO_RAW:
516 		err = st_sensors_read_info_raw(indio_dev, ch, val);
517 		if (err < 0)
518 			goto read_error;
519 
520 		return IIO_VAL_INT;
521 	case IIO_CHAN_INFO_SCALE:
522 		*val = 0;
523 		if ((ch->scan_index == ST_SENSORS_SCAN_Z) &&
524 					(mdata->current_fullscale->gain2 != 0))
525 			*val2 = mdata->current_fullscale->gain2;
526 		else
527 			*val2 = mdata->current_fullscale->gain;
528 		return IIO_VAL_INT_PLUS_MICRO;
529 	case IIO_CHAN_INFO_SAMP_FREQ:
530 		*val = mdata->odr;
531 		return IIO_VAL_INT;
532 	default:
533 		return -EINVAL;
534 	}
535 
536 read_error:
537 	return err;
538 }
539 
540 static int st_magn_write_raw(struct iio_dev *indio_dev,
541 		struct iio_chan_spec const *chan, int val, int val2, long mask)
542 {
543 	int err;
544 
545 	switch (mask) {
546 	case IIO_CHAN_INFO_SCALE:
547 		err = st_sensors_set_fullscale_by_gain(indio_dev, val2);
548 		break;
549 	case IIO_CHAN_INFO_SAMP_FREQ:
550 		if (val2)
551 			return -EINVAL;
552 		mutex_lock(&indio_dev->mlock);
553 		err = st_sensors_set_odr(indio_dev, val);
554 		mutex_unlock(&indio_dev->mlock);
555 		return err;
556 	default:
557 		err = -EINVAL;
558 	}
559 
560 	return err;
561 }
562 
563 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
564 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available);
565 
566 static struct attribute *st_magn_attributes[] = {
567 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
568 	&iio_dev_attr_in_magn_scale_available.dev_attr.attr,
569 	NULL,
570 };
571 
572 static const struct attribute_group st_magn_attribute_group = {
573 	.attrs = st_magn_attributes,
574 };
575 
576 static const struct iio_info magn_info = {
577 	.attrs = &st_magn_attribute_group,
578 	.read_raw = &st_magn_read_raw,
579 	.write_raw = &st_magn_write_raw,
580 	.debugfs_reg_access = &st_sensors_debugfs_reg_access,
581 };
582 
583 #ifdef CONFIG_IIO_TRIGGER
584 static const struct iio_trigger_ops st_magn_trigger_ops = {
585 	.set_trigger_state = ST_MAGN_TRIGGER_SET_STATE,
586 	.validate_device = st_sensors_validate_device,
587 };
588 #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
589 #else
590 #define ST_MAGN_TRIGGER_OPS NULL
591 #endif
592 
593 /*
594  * st_magn_get_settings() - get sensor settings from device name
595  * @name: device name buffer reference.
596  *
597  * Return: valid reference on success, NULL otherwise.
598  */
599 const struct st_sensor_settings *st_magn_get_settings(const char *name)
600 {
601 	int index = st_sensors_get_settings_index(name,
602 					st_magn_sensors_settings,
603 					ARRAY_SIZE(st_magn_sensors_settings));
604 	if (index < 0)
605 		return NULL;
606 
607 	return &st_magn_sensors_settings[index];
608 }
609 EXPORT_SYMBOL(st_magn_get_settings);
610 
611 int st_magn_common_probe(struct iio_dev *indio_dev)
612 {
613 	struct st_sensor_data *mdata = iio_priv(indio_dev);
614 	struct device *parent = indio_dev->dev.parent;
615 	struct st_sensors_platform_data *pdata = dev_get_platdata(parent);
616 	int err;
617 
618 	indio_dev->modes = INDIO_DIRECT_MODE;
619 	indio_dev->info = &magn_info;
620 
621 	err = st_sensors_verify_id(indio_dev);
622 	if (err < 0)
623 		return err;
624 
625 	mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS;
626 	indio_dev->channels = mdata->sensor_settings->ch;
627 	indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
628 
629 	err = iio_read_mount_matrix(parent, &mdata->mount_matrix);
630 	if (err)
631 		return err;
632 
633 	mdata->current_fullscale = &mdata->sensor_settings->fs.fs_avl[0];
634 	mdata->odr = mdata->sensor_settings->odr.odr_avl[0].hz;
635 
636 	if (!pdata)
637 		pdata = (struct st_sensors_platform_data *)&default_magn_pdata;
638 
639 	err = st_sensors_init_sensor(indio_dev, pdata);
640 	if (err < 0)
641 		return err;
642 
643 	err = st_magn_allocate_ring(indio_dev);
644 	if (err < 0)
645 		return err;
646 
647 	if (mdata->irq > 0) {
648 		err = st_sensors_allocate_trigger(indio_dev,
649 						ST_MAGN_TRIGGER_OPS);
650 		if (err < 0)
651 			return err;
652 	}
653 
654 	return devm_iio_device_register(parent, indio_dev);
655 }
656 EXPORT_SYMBOL(st_magn_common_probe);
657 
658 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
659 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
660 MODULE_LICENSE("GPL v2");
661