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 			[2] = LSM303C_MAGN_DEV_NAME,
309 		},
310 		.ch = (struct iio_chan_spec *)st_magn_2_16bit_channels,
311 		.odr = {
312 			.addr = 0x20,
313 			.mask = 0x1c,
314 			.odr_avl = {
315 				{ .hz = 1, .value = 0x00 },
316 				{ .hz = 2, .value = 0x01 },
317 				{ .hz = 3, .value = 0x02 },
318 				{ .hz = 5, .value = 0x03 },
319 				{ .hz = 10, .value = 0x04 },
320 				{ .hz = 20, .value = 0x05 },
321 				{ .hz = 40, .value = 0x06 },
322 				{ .hz = 80, .value = 0x07 },
323 			},
324 		},
325 		.pw = {
326 			.addr = 0x22,
327 			.mask = 0x03,
328 			.value_on = 0x00,
329 			.value_off = 0x03,
330 		},
331 		.fs = {
332 			.addr = 0x21,
333 			.mask = 0x60,
334 			.fs_avl = {
335 				[0] = {
336 					.num = ST_MAGN_FS_AVL_4000MG,
337 					.value = 0x00,
338 					.gain = 146,
339 				},
340 				[1] = {
341 					.num = ST_MAGN_FS_AVL_8000MG,
342 					.value = 0x01,
343 					.gain = 292,
344 				},
345 				[2] = {
346 					.num = ST_MAGN_FS_AVL_12000MG,
347 					.value = 0x02,
348 					.gain = 438,
349 				},
350 				[3] = {
351 					.num = ST_MAGN_FS_AVL_16000MG,
352 					.value = 0x03,
353 					.gain = 584,
354 				},
355 			},
356 		},
357 		.bdu = {
358 			.addr = 0x24,
359 			.mask = 0x40,
360 		},
361 		.drdy_irq = {
362 			/* drdy line is routed drdy pin */
363 			.stat_drdy = {
364 				.addr = ST_SENSORS_DEFAULT_STAT_ADDR,
365 				.mask = 0x07,
366 			},
367 		},
368 		.sim = {
369 			.addr = 0x22,
370 			.value = BIT(2),
371 		},
372 		.multi_read_bit = true,
373 		.bootime = 2,
374 	},
375 	{
376 		.wai = 0x40,
377 		.wai_addr = 0x4f,
378 		.sensors_supported = {
379 			[0] = LSM303AGR_MAGN_DEV_NAME,
380 			[1] = LIS2MDL_MAGN_DEV_NAME,
381 			[2] = IIS2MDC_MAGN_DEV_NAME,
382 		},
383 		.ch = (struct iio_chan_spec *)st_magn_3_16bit_channels,
384 		.odr = {
385 			.addr = 0x60,
386 			.mask = 0x0c,
387 			.odr_avl = {
388 				{ .hz = 10, .value = 0x00 },
389 				{ .hz = 20, .value = 0x01 },
390 				{ .hz = 50, .value = 0x02 },
391 				{ .hz = 100, .value = 0x03 },
392 			},
393 		},
394 		.pw = {
395 			.addr = 0x60,
396 			.mask = 0x03,
397 			.value_on = 0x00,
398 			.value_off = 0x03,
399 		},
400 		.fs = {
401 			.fs_avl = {
402 				[0] = {
403 					.num = ST_MAGN_FS_AVL_15000MG,
404 					.gain = 1500,
405 				},
406 			},
407 		},
408 		.bdu = {
409 			.addr = 0x62,
410 			.mask = 0x10,
411 		},
412 		.drdy_irq = {
413 			.int1 = {
414 				.addr = 0x62,
415 				.mask = 0x01,
416 			},
417 			.stat_drdy = {
418 				.addr = 0x67,
419 				.mask = 0x07,
420 			},
421 		},
422 		.multi_read_bit = false,
423 		.bootime = 2,
424 	},
425 	{
426 		.wai = 0x49,
427 		.wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
428 		.sensors_supported = {
429 			[0] = LSM9DS0_IMU_DEV_NAME,
430 		},
431 		.ch = (struct iio_chan_spec *)st_magn_4_16bit_channels,
432 		.odr = {
433 			.addr = 0x24,
434 			.mask = GENMASK(4, 2),
435 			.odr_avl = {
436 				{ 3, 0x00, },
437 				{ 6, 0x01, },
438 				{ 12, 0x02, },
439 				{ 25, 0x03, },
440 				{ 50, 0x04, },
441 				{ 100, 0x05, },
442 			},
443 		},
444 		.pw = {
445 			.addr = 0x26,
446 			.mask = GENMASK(1, 0),
447 			.value_on = 0x00,
448 			.value_off = 0x03,
449 		},
450 		.fs = {
451 			.addr = 0x25,
452 			.mask = GENMASK(6, 5),
453 			.fs_avl = {
454 				[0] = {
455 					.num = ST_MAGN_FS_AVL_2000MG,
456 					.value = 0x00,
457 					.gain = 73,
458 				},
459 				[1] = {
460 					.num = ST_MAGN_FS_AVL_4000MG,
461 					.value = 0x01,
462 					.gain = 146,
463 				},
464 				[2] = {
465 					.num = ST_MAGN_FS_AVL_8000MG,
466 					.value = 0x02,
467 					.gain = 292,
468 				},
469 				[3] = {
470 					.num = ST_MAGN_FS_AVL_12000MG,
471 					.value = 0x03,
472 					.gain = 438,
473 				},
474 			},
475 		},
476 		.bdu = {
477 			.addr = 0x20,
478 			.mask = BIT(3),
479 		},
480 		.drdy_irq = {
481 			.int1 = {
482 				.addr = 0x22,
483 				.mask = BIT(1),
484 			},
485 			.int2 = {
486 				.addr = 0x23,
487 				.mask = BIT(2),
488 			},
489 			.stat_drdy = {
490 				.addr = 0x07,
491 				.mask = GENMASK(2, 0),
492 			},
493 		},
494 		.sim = {
495 			.addr = 0x21,
496 			.value = BIT(0),
497 		},
498 		.multi_read_bit = true,
499 		.bootime = 2,
500 	},
501 };
502 
503 /* Default magn DRDY is available on INT2 pin */
504 static const struct st_sensors_platform_data default_magn_pdata = {
505 	.drdy_int_pin = 2,
506 };
507 
508 static int st_magn_read_raw(struct iio_dev *indio_dev,
509 			struct iio_chan_spec const *ch, int *val,
510 							int *val2, long mask)
511 {
512 	int err;
513 	struct st_sensor_data *mdata = iio_priv(indio_dev);
514 
515 	switch (mask) {
516 	case IIO_CHAN_INFO_RAW:
517 		err = st_sensors_read_info_raw(indio_dev, ch, val);
518 		if (err < 0)
519 			goto read_error;
520 
521 		return IIO_VAL_INT;
522 	case IIO_CHAN_INFO_SCALE:
523 		*val = 0;
524 		if ((ch->scan_index == ST_SENSORS_SCAN_Z) &&
525 					(mdata->current_fullscale->gain2 != 0))
526 			*val2 = mdata->current_fullscale->gain2;
527 		else
528 			*val2 = mdata->current_fullscale->gain;
529 		return IIO_VAL_INT_PLUS_MICRO;
530 	case IIO_CHAN_INFO_SAMP_FREQ:
531 		*val = mdata->odr;
532 		return IIO_VAL_INT;
533 	default:
534 		return -EINVAL;
535 	}
536 
537 read_error:
538 	return err;
539 }
540 
541 static int st_magn_write_raw(struct iio_dev *indio_dev,
542 		struct iio_chan_spec const *chan, int val, int val2, long mask)
543 {
544 	switch (mask) {
545 	case IIO_CHAN_INFO_SCALE:
546 		return st_sensors_set_fullscale_by_gain(indio_dev, val2);
547 	case IIO_CHAN_INFO_SAMP_FREQ:
548 		if (val2)
549 			return -EINVAL;
550 
551 		return st_sensors_set_odr(indio_dev, val);
552 	default:
553 		return -EINVAL;
554 	}
555 }
556 
557 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
558 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available);
559 
560 static struct attribute *st_magn_attributes[] = {
561 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
562 	&iio_dev_attr_in_magn_scale_available.dev_attr.attr,
563 	NULL,
564 };
565 
566 static const struct attribute_group st_magn_attribute_group = {
567 	.attrs = st_magn_attributes,
568 };
569 
570 static const struct iio_info magn_info = {
571 	.attrs = &st_magn_attribute_group,
572 	.read_raw = &st_magn_read_raw,
573 	.write_raw = &st_magn_write_raw,
574 	.debugfs_reg_access = &st_sensors_debugfs_reg_access,
575 };
576 
577 #ifdef CONFIG_IIO_TRIGGER
578 static const struct iio_trigger_ops st_magn_trigger_ops = {
579 	.set_trigger_state = ST_MAGN_TRIGGER_SET_STATE,
580 	.validate_device = st_sensors_validate_device,
581 };
582 #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
583 #else
584 #define ST_MAGN_TRIGGER_OPS NULL
585 #endif
586 
587 /*
588  * st_magn_get_settings() - get sensor settings from device name
589  * @name: device name buffer reference.
590  *
591  * Return: valid reference on success, NULL otherwise.
592  */
593 const struct st_sensor_settings *st_magn_get_settings(const char *name)
594 {
595 	int index = st_sensors_get_settings_index(name,
596 					st_magn_sensors_settings,
597 					ARRAY_SIZE(st_magn_sensors_settings));
598 	if (index < 0)
599 		return NULL;
600 
601 	return &st_magn_sensors_settings[index];
602 }
603 EXPORT_SYMBOL_NS(st_magn_get_settings, IIO_ST_SENSORS);
604 
605 int st_magn_common_probe(struct iio_dev *indio_dev)
606 {
607 	struct st_sensor_data *mdata = iio_priv(indio_dev);
608 	struct device *parent = indio_dev->dev.parent;
609 	struct st_sensors_platform_data *pdata = dev_get_platdata(parent);
610 	int err;
611 
612 	indio_dev->modes = INDIO_DIRECT_MODE;
613 	indio_dev->info = &magn_info;
614 
615 	err = st_sensors_verify_id(indio_dev);
616 	if (err < 0)
617 		return err;
618 
619 	mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS;
620 	indio_dev->channels = mdata->sensor_settings->ch;
621 	indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS;
622 
623 	err = iio_read_mount_matrix(parent, &mdata->mount_matrix);
624 	if (err)
625 		return err;
626 
627 	mdata->current_fullscale = &mdata->sensor_settings->fs.fs_avl[0];
628 	mdata->odr = mdata->sensor_settings->odr.odr_avl[0].hz;
629 
630 	if (!pdata)
631 		pdata = (struct st_sensors_platform_data *)&default_magn_pdata;
632 
633 	err = st_sensors_init_sensor(indio_dev, pdata);
634 	if (err < 0)
635 		return err;
636 
637 	err = st_magn_allocate_ring(indio_dev);
638 	if (err < 0)
639 		return err;
640 
641 	if (mdata->irq > 0) {
642 		err = st_sensors_allocate_trigger(indio_dev,
643 						ST_MAGN_TRIGGER_OPS);
644 		if (err < 0)
645 			return err;
646 	}
647 
648 	return devm_iio_device_register(parent, indio_dev);
649 }
650 EXPORT_SYMBOL_NS(st_magn_common_probe, IIO_ST_SENSORS);
651 
652 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
653 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
654 MODULE_LICENSE("GPL v2");
655 MODULE_IMPORT_NS(IIO_ST_SENSORS);
656