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 [1] = LSM9DS1_MAGN_DEV_NAME, 271 }, 272 .ch = (struct iio_chan_spec *)st_magn_2_16bit_channels, 273 .odr = { 274 .addr = 0x20, 275 .mask = 0x1c, 276 .odr_avl = { 277 { .hz = 1, .value = 0x00 }, 278 { .hz = 2, .value = 0x01 }, 279 { .hz = 3, .value = 0x02 }, 280 { .hz = 5, .value = 0x03 }, 281 { .hz = 10, .value = 0x04 }, 282 { .hz = 20, .value = 0x05 }, 283 { .hz = 40, .value = 0x06 }, 284 { .hz = 80, .value = 0x07 }, 285 }, 286 }, 287 .pw = { 288 .addr = 0x22, 289 .mask = 0x03, 290 .value_on = 0x00, 291 .value_off = 0x03, 292 }, 293 .fs = { 294 .addr = 0x21, 295 .mask = 0x60, 296 .fs_avl = { 297 [0] = { 298 .num = ST_MAGN_FS_AVL_4000MG, 299 .value = 0x00, 300 .gain = 146, 301 }, 302 [1] = { 303 .num = ST_MAGN_FS_AVL_8000MG, 304 .value = 0x01, 305 .gain = 292, 306 }, 307 [2] = { 308 .num = ST_MAGN_FS_AVL_12000MG, 309 .value = 0x02, 310 .gain = 438, 311 }, 312 [3] = { 313 .num = ST_MAGN_FS_AVL_16000MG, 314 .value = 0x03, 315 .gain = 584, 316 }, 317 }, 318 }, 319 .bdu = { 320 .addr = 0x24, 321 .mask = 0x40, 322 }, 323 .drdy_irq = { 324 /* drdy line is routed drdy pin */ 325 .stat_drdy = { 326 .addr = ST_SENSORS_DEFAULT_STAT_ADDR, 327 .mask = 0x07, 328 }, 329 }, 330 .sim = { 331 .addr = 0x22, 332 .value = BIT(2), 333 }, 334 .multi_read_bit = true, 335 .bootime = 2, 336 }, 337 { 338 .wai = 0x40, 339 .wai_addr = 0x4f, 340 .sensors_supported = { 341 [0] = LSM303AGR_MAGN_DEV_NAME, 342 [1] = LIS2MDL_MAGN_DEV_NAME, 343 }, 344 .ch = (struct iio_chan_spec *)st_magn_3_16bit_channels, 345 .odr = { 346 .addr = 0x60, 347 .mask = 0x0c, 348 .odr_avl = { 349 { .hz = 10, .value = 0x00 }, 350 { .hz = 20, .value = 0x01 }, 351 { .hz = 50, .value = 0x02 }, 352 { .hz = 100, .value = 0x03 }, 353 }, 354 }, 355 .pw = { 356 .addr = 0x60, 357 .mask = 0x03, 358 .value_on = 0x00, 359 .value_off = 0x03, 360 }, 361 .fs = { 362 .fs_avl = { 363 [0] = { 364 .num = ST_MAGN_FS_AVL_15000MG, 365 .gain = 1500, 366 }, 367 }, 368 }, 369 .bdu = { 370 .addr = 0x62, 371 .mask = 0x10, 372 }, 373 .drdy_irq = { 374 .int1 = { 375 .addr = 0x62, 376 .mask = 0x01, 377 }, 378 .stat_drdy = { 379 .addr = 0x67, 380 .mask = 0x07, 381 }, 382 }, 383 .multi_read_bit = false, 384 .bootime = 2, 385 }, 386 }; 387 388 static int st_magn_read_raw(struct iio_dev *indio_dev, 389 struct iio_chan_spec const *ch, int *val, 390 int *val2, long mask) 391 { 392 int err; 393 struct st_sensor_data *mdata = iio_priv(indio_dev); 394 395 switch (mask) { 396 case IIO_CHAN_INFO_RAW: 397 err = st_sensors_read_info_raw(indio_dev, ch, val); 398 if (err < 0) 399 goto read_error; 400 401 return IIO_VAL_INT; 402 case IIO_CHAN_INFO_SCALE: 403 *val = 0; 404 if ((ch->scan_index == ST_SENSORS_SCAN_Z) && 405 (mdata->current_fullscale->gain2 != 0)) 406 *val2 = mdata->current_fullscale->gain2; 407 else 408 *val2 = mdata->current_fullscale->gain; 409 return IIO_VAL_INT_PLUS_MICRO; 410 case IIO_CHAN_INFO_SAMP_FREQ: 411 *val = mdata->odr; 412 return IIO_VAL_INT; 413 default: 414 return -EINVAL; 415 } 416 417 read_error: 418 return err; 419 } 420 421 static int st_magn_write_raw(struct iio_dev *indio_dev, 422 struct iio_chan_spec const *chan, int val, int val2, long mask) 423 { 424 int err; 425 426 switch (mask) { 427 case IIO_CHAN_INFO_SCALE: 428 err = st_sensors_set_fullscale_by_gain(indio_dev, val2); 429 break; 430 case IIO_CHAN_INFO_SAMP_FREQ: 431 if (val2) 432 return -EINVAL; 433 mutex_lock(&indio_dev->mlock); 434 err = st_sensors_set_odr(indio_dev, val); 435 mutex_unlock(&indio_dev->mlock); 436 return err; 437 default: 438 err = -EINVAL; 439 } 440 441 return err; 442 } 443 444 static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL(); 445 static ST_SENSORS_DEV_ATTR_SCALE_AVAIL(in_magn_scale_available); 446 447 static struct attribute *st_magn_attributes[] = { 448 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 449 &iio_dev_attr_in_magn_scale_available.dev_attr.attr, 450 NULL, 451 }; 452 453 static const struct attribute_group st_magn_attribute_group = { 454 .attrs = st_magn_attributes, 455 }; 456 457 static const struct iio_info magn_info = { 458 .attrs = &st_magn_attribute_group, 459 .read_raw = &st_magn_read_raw, 460 .write_raw = &st_magn_write_raw, 461 .debugfs_reg_access = &st_sensors_debugfs_reg_access, 462 }; 463 464 #ifdef CONFIG_IIO_TRIGGER 465 static const struct iio_trigger_ops st_magn_trigger_ops = { 466 .set_trigger_state = ST_MAGN_TRIGGER_SET_STATE, 467 .validate_device = st_sensors_validate_device, 468 }; 469 #define ST_MAGN_TRIGGER_OPS (&st_magn_trigger_ops) 470 #else 471 #define ST_MAGN_TRIGGER_OPS NULL 472 #endif 473 474 int st_magn_common_probe(struct iio_dev *indio_dev) 475 { 476 struct st_sensor_data *mdata = iio_priv(indio_dev); 477 int irq = mdata->get_irq_data_ready(indio_dev); 478 int err; 479 480 indio_dev->modes = INDIO_DIRECT_MODE; 481 indio_dev->info = &magn_info; 482 mutex_init(&mdata->tb.buf_lock); 483 484 err = st_sensors_power_enable(indio_dev); 485 if (err) 486 return err; 487 488 err = st_sensors_check_device_support(indio_dev, 489 ARRAY_SIZE(st_magn_sensors_settings), 490 st_magn_sensors_settings); 491 if (err < 0) 492 goto st_magn_power_off; 493 494 mdata->num_data_channels = ST_MAGN_NUMBER_DATA_CHANNELS; 495 mdata->multiread_bit = mdata->sensor_settings->multi_read_bit; 496 indio_dev->channels = mdata->sensor_settings->ch; 497 indio_dev->num_channels = ST_SENSORS_NUMBER_ALL_CHANNELS; 498 499 mdata->current_fullscale = (struct st_sensor_fullscale_avl *) 500 &mdata->sensor_settings->fs.fs_avl[0]; 501 mdata->odr = mdata->sensor_settings->odr.odr_avl[0].hz; 502 503 err = st_sensors_init_sensor(indio_dev, NULL); 504 if (err < 0) 505 goto st_magn_power_off; 506 507 err = st_magn_allocate_ring(indio_dev); 508 if (err < 0) 509 goto st_magn_power_off; 510 511 if (irq > 0) { 512 err = st_sensors_allocate_trigger(indio_dev, 513 ST_MAGN_TRIGGER_OPS); 514 if (err < 0) 515 goto st_magn_probe_trigger_error; 516 } 517 518 err = iio_device_register(indio_dev); 519 if (err) 520 goto st_magn_device_register_error; 521 522 dev_info(&indio_dev->dev, "registered magnetometer %s\n", 523 indio_dev->name); 524 525 return 0; 526 527 st_magn_device_register_error: 528 if (irq > 0) 529 st_sensors_deallocate_trigger(indio_dev); 530 st_magn_probe_trigger_error: 531 st_magn_deallocate_ring(indio_dev); 532 st_magn_power_off: 533 st_sensors_power_disable(indio_dev); 534 535 return err; 536 } 537 EXPORT_SYMBOL(st_magn_common_probe); 538 539 void st_magn_common_remove(struct iio_dev *indio_dev) 540 { 541 struct st_sensor_data *mdata = iio_priv(indio_dev); 542 543 st_sensors_power_disable(indio_dev); 544 545 iio_device_unregister(indio_dev); 546 if (mdata->get_irq_data_ready(indio_dev) > 0) 547 st_sensors_deallocate_trigger(indio_dev); 548 549 st_magn_deallocate_ring(indio_dev); 550 } 551 EXPORT_SYMBOL(st_magn_common_remove); 552 553 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); 554 MODULE_DESCRIPTION("STMicroelectronics magnetometers driver"); 555 MODULE_LICENSE("GPL v2"); 556