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