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