Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / magnetometer / st_magn_core.c
blob79de721e60159cdc6d7b1271e1abac2dd492364a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * STMicroelectronics magnetometers driver
5 * Copyright 2012-2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
8 */
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>
23 #include <linux/iio/common/st_sensors.h>
24 #include "st_magn.h"
26 #define ST_MAGN_NUMBER_DATA_CHANNELS 3
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
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
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
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
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)
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)
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)
104 static const struct st_sensor_settings st_magn_sensors_settings[] = {
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,
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 */
126 .pw = {
127 .addr = 0x02,
128 .mask = 0x03,
129 .value_on = 0x00,
130 .value_off = 0x03,
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,
142 [1] = {
143 .num = ST_MAGN_FS_AVL_1900MG,
144 .value = 0x02,
145 .gain = 855,
146 .gain2 = 760,
148 [2] = {
149 .num = ST_MAGN_FS_AVL_2500MG,
150 .value = 0x03,
151 .gain = 670,
152 .gain2 = 600,
154 [3] = {
155 .num = ST_MAGN_FS_AVL_4000MG,
156 .value = 0x04,
157 .gain = 450,
158 .gain2 = 400,
160 [4] = {
161 .num = ST_MAGN_FS_AVL_4700MG,
162 .value = 0x05,
163 .gain = 400,
164 .gain2 = 355,
166 [5] = {
167 .num = ST_MAGN_FS_AVL_5600MG,
168 .value = 0x06,
169 .gain = 330,
170 .gain2 = 295,
172 [6] = {
173 .num = ST_MAGN_FS_AVL_8100MG,
174 .value = 0x07,
175 .gain = 230,
176 .gain2 = 205,
180 .multi_read_bit = false,
181 .bootime = 2,
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,
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 },
205 .pw = {
206 .addr = 0x02,
207 .mask = 0x03,
208 .value_on = 0x00,
209 .value_off = 0x03,
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,
221 [1] = {
222 .num = ST_MAGN_FS_AVL_1900MG,
223 .value = 0x02,
224 .gain = 1169,
225 .gain2 = 1315,
227 [2] = {
228 .num = ST_MAGN_FS_AVL_2500MG,
229 .value = 0x03,
230 .gain = 1492,
231 .gain2 = 1666,
233 [3] = {
234 .num = ST_MAGN_FS_AVL_4000MG,
235 .value = 0x04,
236 .gain = 2222,
237 .gain2 = 2500,
239 [4] = {
240 .num = ST_MAGN_FS_AVL_4700MG,
241 .value = 0x05,
242 .gain = 2500,
243 .gain2 = 2816,
245 [5] = {
246 .num = ST_MAGN_FS_AVL_5600MG,
247 .value = 0x06,
248 .gain = 3030,
249 .gain2 = 3389,
251 [6] = {
252 .num = ST_MAGN_FS_AVL_8100MG,
253 .value = 0x07,
254 .gain = 4347,
255 .gain2 = 4878,
259 .multi_read_bit = false,
260 .bootime = 2,
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,
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 },
284 .pw = {
285 .addr = 0x22,
286 .mask = 0x03,
287 .value_on = 0x00,
288 .value_off = 0x03,
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,
299 [1] = {
300 .num = ST_MAGN_FS_AVL_8000MG,
301 .value = 0x01,
302 .gain = 292,
304 [2] = {
305 .num = ST_MAGN_FS_AVL_12000MG,
306 .value = 0x02,
307 .gain = 438,
309 [3] = {
310 .num = ST_MAGN_FS_AVL_16000MG,
311 .value = 0x03,
312 .gain = 584,
316 .bdu = {
317 .addr = 0x24,
318 .mask = 0x40,
320 .drdy_irq = {
321 /* drdy line is routed drdy pin */
322 .stat_drdy = {
323 .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
324 .mask = 0x07,
327 .sim = {
328 .addr = 0x22,
329 .value = BIT(2),
331 .multi_read_bit = true,
332 .bootime = 2,
335 .wai = 0x40,
336 .wai_addr = 0x4f,
337 .sensors_supported = {
338 [0] = LSM303AGR_MAGN_DEV_NAME,
339 [1] = LIS2MDL_MAGN_DEV_NAME,
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 },
352 .pw = {
353 .addr = 0x60,
354 .mask = 0x03,
355 .value_on = 0x00,
356 .value_off = 0x03,
358 .fs = {
359 .fs_avl = {
360 [0] = {
361 .num = ST_MAGN_FS_AVL_15000MG,
362 .gain = 1500,
366 .bdu = {
367 .addr = 0x62,
368 .mask = 0x10,
370 .drdy_irq = {
371 .int1 = {
372 .addr = 0x62,
373 .mask = 0x01,
375 .stat_drdy = {
376 .addr = 0x67,
377 .mask = 0x07,
380 .multi_read_bit = false,
381 .bootime = 2,
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)
389 int err;
390 struct st_sensor_data *mdata = iio_priv(indio_dev);
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;
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;
414 read_error:
415 return err;
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)
421 int err;
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;
438 return err;
441 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
442 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available);
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,
450 static const struct attribute_group st_magn_attribute_group = {
451 .attrs = st_magn_attributes,
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,
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,
466 #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops)
467 #else
468 #define ST_MAGN_TRIGGER_OPS NULL
469 #endif
472 * st_magn_get_settings() - get sensor settings from device name
473 * @name: device name buffer reference.
475 * Return: valid reference on success, NULL otherwise.
477 const struct st_sensor_settings *st_magn_get_settings(const char *name)
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;
485 return &st_magn_sensors_settings[index];
487 EXPORT_SYMBOL(st_magn_get_settings);
489 int st_magn_common_probe(struct iio_dev *indio_dev)
491 struct st_sensor_data *mdata = iio_priv(indio_dev);
492 int err;
494 indio_dev->modes = INDIO_DIRECT_MODE;
495 indio_dev->info = &magn_info;
497 err = st_sensors_power_enable(indio_dev);
498 if (err)
499 return err;
501 err = st_sensors_verify_id(indio_dev);
502 if (err < 0)
503 goto st_magn_power_off;
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;
509 mdata->current_fullscale = &mdata->sensor_settings->fs.fs_avl[0];
510 mdata->odr = mdata->sensor_settings->odr.odr_avl[0].hz;
512 err = st_sensors_init_sensor(indio_dev, NULL);
513 if (err < 0)
514 goto st_magn_power_off;
516 err = st_magn_allocate_ring(indio_dev);
517 if (err < 0)
518 goto st_magn_power_off;
520 if (mdata->irq > 0) {
521 err = st_sensors_allocate_trigger(indio_dev,
522 ST_MAGN_TRIGGER_OPS);
523 if (err < 0)
524 goto st_magn_probe_trigger_error;
527 err = iio_device_register(indio_dev);
528 if (err)
529 goto st_magn_device_register_error;
531 dev_info(&indio_dev->dev, "registered magnetometer %s\n",
532 indio_dev->name);
534 return 0;
536 st_magn_device_register_error:
537 if (mdata->irq > 0)
538 st_sensors_deallocate_trigger(indio_dev);
539 st_magn_probe_trigger_error:
540 st_magn_deallocate_ring(indio_dev);
541 st_magn_power_off:
542 st_sensors_power_disable(indio_dev);
544 return err;
546 EXPORT_SYMBOL(st_magn_common_probe);
548 void st_magn_common_remove(struct iio_dev *indio_dev)
550 struct st_sensor_data *mdata = iio_priv(indio_dev);
552 st_sensors_power_disable(indio_dev);
554 iio_device_unregister(indio_dev);
555 if (mdata->irq > 0)
556 st_sensors_deallocate_trigger(indio_dev);
558 st_magn_deallocate_ring(indio_dev);
560 EXPORT_SYMBOL(st_magn_common_remove);
562 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
563 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver");
564 MODULE_LICENSE("GPL v2");