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 [2] = IIS2MDC_MAGN_DEV_NAME, 341 }, 342 .ch = (struct iio_chan_spec *)st_magn_3_16bit_channels, 343 .odr = { 344 .addr = 0x60, 345 .mask = 0x0c, 346 .odr_avl = { 347 { .hz = 10, .value = 0x00 }, 348 { .hz = 20, .value = 0x01 }, 349 { .hz = 50, .value = 0x02 }, 350 { .hz = 100, .value = 0x03 }, 351 }, 352 }, 353 .pw = { 354 .addr = 0x60, 355 .mask = 0x03, 356 .value_on = 0x00, 357 .value_off = 0x03, 358 }, 359 .fs = { 360 .fs_avl = { 361 [0] = { 362 .num = ST_MAGN_FS_AVL_15000MG, 363 .gain = 1500, 364 }, 365 }, 366 }, 367 .bdu = { 368 .addr = 0x62, 369 .mask = 0x10, 370 }, 371 .drdy_irq = { 372 .int1 = { 373 .addr = 0x62, 374 .mask = 0x01, 375 }, 376 .stat_drdy = { 377 .addr = 0x67, 378 .mask = 0x07, 379 }, 380 }, 381 .multi_read_bit = false, 382 .bootime = 2, 383 }, 384 }; 385 386 static int st_magn_read_raw(struct iio_dev *indio_dev, 387 struct iio_chan_spec const *ch, int *val, 388 int *val2, long mask) 389 { 390 int err; 391 struct st_sensor_data *mdata = iio_priv(indio_dev); 392 393 switch (mask) { 394 case IIO_CHAN_INFO_RAW: 395 err = st_sensors_read_info_raw(indio_dev, ch, val); 396 if (err < 0) 397 goto read_error; 398 399 return IIO_VAL_INT; 400 case IIO_CHAN_INFO_SCALE: 401 *val = 0; 402 if ((ch->scan_index == ST_SENSORS_SCAN_Z) && 403 (mdata->current_fullscale->gain2 != 0)) 404 *val2 = mdata->current_fullscale->gain2; 405 else 406 *val2 = mdata->current_fullscale->gain; 407 return IIO_VAL_INT_PLUS_MICRO; 408 case IIO_CHAN_INFO_SAMP_FREQ: 409 *val = mdata->odr; 410 return IIO_VAL_INT; 411 default: 412 return -EINVAL; 413 } 414 415 read_error: 416 return err; 417 } 418 419 static int st_magn_write_raw(struct iio_dev *indio_dev, 420 struct iio_chan_spec const *chan, int val, int val2, long mask) 421 { 422 int err; 423 424 switch (mask) { 425 case IIO_CHAN_INFO_SCALE: 426 err = st_sensors_set_fullscale_by_gain(indio_dev, val2); 427 break; 428 case IIO_CHAN_INFO_SAMP_FREQ: 429 if (val2) 430 return -EINVAL; 431 mutex_lock(&indio_dev->mlock); 432 err = st_sensors_set_odr(indio_dev, val); 433 mutex_unlock(&indio_dev->mlock); 434 return err; 435 default: 436 err = -EINVAL; 437 } 438 439 return err; 440 } 441 442 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 443 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available); 444 445 static struct attribute *st_magn_attributes[] = { 446 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 447 &iio_dev_attr_in_magn_scale_available.dev_attr.attr, 448 NULL, 449 }; 450 451 static const struct attribute_group st_magn_attribute_group = { 452 .attrs = st_magn_attributes, 453 }; 454 455 static const struct iio_info magn_info = { 456 .attrs = &st_magn_attribute_group, 457 .read_raw = &st_magn_read_raw, 458 .write_raw = &st_magn_write_raw, 459 .debugfs_reg_access = &st_sensors_debugfs_reg_access, 460 }; 461 462 #ifdef CONFIG_IIO_TRIGGER 463 static const struct iio_trigger_ops st_magn_trigger_ops = { 464 .set_trigger_state = ST_MAGN_TRIGGER_SET_STATE, 465 .validate_device = st_sensors_validate_device, 466 }; 467 #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops) 468 #else 469 #define ST_MAGN_TRIGGER_OPS NULL 470 #endif 471 472 /* 473 * st_magn_get_settings() - get sensor settings from device name 474 * @name: device name buffer reference. 475 * 476 * Return: valid reference on success, NULL otherwise. 477 */ 478 const struct st_sensor_settings *st_magn_get_settings(const char *name) 479 { 480 int index = st_sensors_get_settings_index(name, 481 st_magn_sensors_settings, 482 ARRAY_SIZE(st_magn_sensors_settings)); 483 if (index < 0) 484 return NULL; 485 486 return &st_magn_sensors_settings[index]; 487 } 488 EXPORT_SYMBOL(st_magn_get_settings); 489 490 int st_magn_common_probe(struct iio_dev *indio_dev) 491 { 492 struct st_sensor_data *mdata = iio_priv(indio_dev); 493 int err; 494 495 indio_dev->modes = INDIO_DIRECT_MODE; 496 indio_dev->info = &magn_info; 497 498 err = st_sensors_power_enable(indio_dev); 499 if (err) 500 return err; 501 502 err = st_sensors_verify_id(indio_dev); 503 if (err < 0) 504 goto st_magn_power_off; 505 506 mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS; 507 indio_dev->channels = mdata->sensor_settings->ch; 508 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 509 510 mdata->current_fullscale = &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