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