1 /* 2 * Device driver for the the HMC5843 multi-chip module designed 3 * for low field magnetic sensing. 4 * 5 * Copyright (C) 2010 Texas Instruments 6 * 7 * Author: Shubhrajyoti Datta <shubhrajyoti@ti.com> 8 * Acknowledgment: Jonathan Cameron <jic23@kernel.org> for valuable inputs. 9 * Support for HMC5883 and HMC5883L by Peter Meerwald <pmeerw@pmeerw.net>. 10 * Split to multiple files by Josef Gajdusek <atx@atx.name> - 2014 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 */ 22 23 #include <linux/module.h> 24 #include <linux/regmap.h> 25 #include <linux/iio/iio.h> 26 #include <linux/iio/sysfs.h> 27 #include <linux/iio/trigger_consumer.h> 28 #include <linux/iio/buffer.h> 29 #include <linux/iio/triggered_buffer.h> 30 #include <linux/delay.h> 31 32 #include "hmc5843.h" 33 34 /* 35 * Range gain settings in (+-)Ga 36 * Beware: HMC5843 and HMC5883 have different recommended sensor field 37 * ranges; default corresponds to +-1.0 Ga and +-1.3 Ga, respectively 38 */ 39 #define HMC5843_RANGE_GAIN_OFFSET 0x05 40 #define HMC5843_RANGE_GAIN_DEFAULT 0x01 41 #define HMC5843_RANGE_GAIN_MASK 0xe0 42 43 /* Device status */ 44 #define HMC5843_DATA_READY 0x01 45 #define HMC5843_DATA_OUTPUT_LOCK 0x02 46 47 /* Mode register configuration */ 48 #define HMC5843_MODE_CONVERSION_CONTINUOUS 0x00 49 #define HMC5843_MODE_CONVERSION_SINGLE 0x01 50 #define HMC5843_MODE_IDLE 0x02 51 #define HMC5843_MODE_SLEEP 0x03 52 #define HMC5843_MODE_MASK 0x03 53 54 /* 55 * HMC5843: Minimum data output rate 56 * HMC5883: Typical data output rate 57 */ 58 #define HMC5843_RATE_OFFSET 0x02 59 #define HMC5843_RATE_DEFAULT 0x04 60 #define HMC5843_RATE_MASK 0x1c 61 62 /* Device measurement configuration */ 63 #define HMC5843_MEAS_CONF_NORMAL 0x00 64 #define HMC5843_MEAS_CONF_POSITIVE_BIAS 0x01 65 #define HMC5843_MEAS_CONF_NEGATIVE_BIAS 0x02 66 #define HMC5843_MEAS_CONF_MASK 0x03 67 68 /* 69 * API for setting the measurement configuration to 70 * Normal, Positive bias and Negative bias 71 * 72 * From the datasheet: 73 * 0 - Normal measurement configuration (default): In normal measurement 74 * configuration the device follows normal measurement flow. Pins BP 75 * and BN are left floating and high impedance. 76 * 77 * 1 - Positive bias configuration: In positive bias configuration, a 78 * positive current is forced across the resistive load on pins BP 79 * and BN. 80 * 81 * 2 - Negative bias configuration. In negative bias configuration, a 82 * negative current is forced across the resistive load on pins BP 83 * and BN. 84 * 85 * 3 - Only available on HMC5983. Magnetic sensor is disabled. 86 * Temperature sensor is enabled. 87 */ 88 89 static const char *const hmc5843_meas_conf_modes[] = {"normal", "positivebias", 90 "negativebias"}; 91 92 static const char *const hmc5983_meas_conf_modes[] = {"normal", "positivebias", 93 "negativebias", 94 "disabled"}; 95 /* Scaling factors: 10000000/Gain */ 96 static const int hmc5843_regval_to_nanoscale[] = { 97 6173, 7692, 10309, 12821, 18868, 21739, 25641, 35714 98 }; 99 100 static const int hmc5883_regval_to_nanoscale[] = { 101 7812, 9766, 13021, 16287, 24096, 27701, 32573, 45662 102 }; 103 104 static const int hmc5883l_regval_to_nanoscale[] = { 105 7299, 9174, 12195, 15152, 22727, 25641, 30303, 43478 106 }; 107 108 /* 109 * From the datasheet: 110 * Value | HMC5843 | HMC5883/HMC5883L 111 * | Data output rate (Hz) | Data output rate (Hz) 112 * 0 | 0.5 | 0.75 113 * 1 | 1 | 1.5 114 * 2 | 2 | 3 115 * 3 | 5 | 7.5 116 * 4 | 10 (default) | 15 117 * 5 | 20 | 30 118 * 6 | 50 | 75 119 * 7 | Not used | Not used 120 */ 121 static const int hmc5843_regval_to_samp_freq[][2] = { 122 {0, 500000}, {1, 0}, {2, 0}, {5, 0}, {10, 0}, {20, 0}, {50, 0} 123 }; 124 125 static const int hmc5883_regval_to_samp_freq[][2] = { 126 {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0}, 127 {75, 0} 128 }; 129 130 static const int hmc5983_regval_to_samp_freq[][2] = { 131 {0, 750000}, {1, 500000}, {3, 0}, {7, 500000}, {15, 0}, {30, 0}, 132 {75, 0}, {220, 0} 133 }; 134 135 /* Describe chip variants */ 136 struct hmc5843_chip_info { 137 const struct iio_chan_spec *channels; 138 const int (*regval_to_samp_freq)[2]; 139 const int n_regval_to_samp_freq; 140 const int *regval_to_nanoscale; 141 const int n_regval_to_nanoscale; 142 }; 143 144 /* The lower two bits contain the current conversion mode */ 145 static s32 hmc5843_set_mode(struct hmc5843_data *data, u8 operating_mode) 146 { 147 int ret; 148 149 mutex_lock(&data->lock); 150 ret = regmap_update_bits(data->regmap, HMC5843_MODE_REG, 151 HMC5843_MODE_MASK, operating_mode); 152 mutex_unlock(&data->lock); 153 154 return ret; 155 } 156 157 static int hmc5843_wait_measurement(struct hmc5843_data *data) 158 { 159 int tries = 150; 160 unsigned int val; 161 int ret; 162 163 while (tries-- > 0) { 164 ret = regmap_read(data->regmap, HMC5843_STATUS_REG, &val); 165 if (ret < 0) 166 return ret; 167 if (val & HMC5843_DATA_READY) 168 break; 169 msleep(20); 170 } 171 172 if (tries < 0) { 173 dev_err(data->dev, "data not ready\n"); 174 return -EIO; 175 } 176 177 return 0; 178 } 179 180 /* Return the measurement value from the specified channel */ 181 static int hmc5843_read_measurement(struct hmc5843_data *data, 182 int idx, int *val) 183 { 184 __be16 values[3]; 185 int ret; 186 187 mutex_lock(&data->lock); 188 ret = hmc5843_wait_measurement(data); 189 if (ret < 0) { 190 mutex_unlock(&data->lock); 191 return ret; 192 } 193 ret = regmap_bulk_read(data->regmap, HMC5843_DATA_OUT_MSB_REGS, 194 values, sizeof(values)); 195 mutex_unlock(&data->lock); 196 if (ret < 0) 197 return ret; 198 199 *val = sign_extend32(be16_to_cpu(values[idx]), 15); 200 return IIO_VAL_INT; 201 } 202 203 static int hmc5843_set_meas_conf(struct hmc5843_data *data, u8 meas_conf) 204 { 205 int ret; 206 207 mutex_lock(&data->lock); 208 ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_A, 209 HMC5843_MEAS_CONF_MASK, meas_conf); 210 mutex_unlock(&data->lock); 211 212 return ret; 213 } 214 215 static 216 int hmc5843_show_measurement_configuration(struct iio_dev *indio_dev, 217 const struct iio_chan_spec *chan) 218 { 219 struct hmc5843_data *data = iio_priv(indio_dev); 220 unsigned int val; 221 int ret; 222 223 ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_A, &val); 224 if (ret) 225 return ret; 226 227 return val & HMC5843_MEAS_CONF_MASK; 228 } 229 230 static 231 int hmc5843_set_measurement_configuration(struct iio_dev *indio_dev, 232 const struct iio_chan_spec *chan, 233 unsigned int meas_conf) 234 { 235 struct hmc5843_data *data = iio_priv(indio_dev); 236 237 return hmc5843_set_meas_conf(data, meas_conf); 238 } 239 240 static const struct iio_mount_matrix * 241 hmc5843_get_mount_matrix(const struct iio_dev *indio_dev, 242 const struct iio_chan_spec *chan) 243 { 244 struct hmc5843_data *data = iio_priv(indio_dev); 245 246 return &data->orientation; 247 } 248 249 static const struct iio_enum hmc5843_meas_conf_enum = { 250 .items = hmc5843_meas_conf_modes, 251 .num_items = ARRAY_SIZE(hmc5843_meas_conf_modes), 252 .get = hmc5843_show_measurement_configuration, 253 .set = hmc5843_set_measurement_configuration, 254 }; 255 256 static const struct iio_chan_spec_ext_info hmc5843_ext_info[] = { 257 IIO_ENUM("meas_conf", true, &hmc5843_meas_conf_enum), 258 IIO_ENUM_AVAILABLE("meas_conf", &hmc5843_meas_conf_enum), 259 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, hmc5843_get_mount_matrix), 260 { } 261 }; 262 263 static const struct iio_enum hmc5983_meas_conf_enum = { 264 .items = hmc5983_meas_conf_modes, 265 .num_items = ARRAY_SIZE(hmc5983_meas_conf_modes), 266 .get = hmc5843_show_measurement_configuration, 267 .set = hmc5843_set_measurement_configuration, 268 }; 269 270 static const struct iio_chan_spec_ext_info hmc5983_ext_info[] = { 271 IIO_ENUM("meas_conf", true, &hmc5983_meas_conf_enum), 272 IIO_ENUM_AVAILABLE("meas_conf", &hmc5983_meas_conf_enum), 273 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, hmc5843_get_mount_matrix), 274 { } 275 }; 276 277 static 278 ssize_t hmc5843_show_samp_freq_avail(struct device *dev, 279 struct device_attribute *attr, char *buf) 280 { 281 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev)); 282 size_t len = 0; 283 int i; 284 285 for (i = 0; i < data->variant->n_regval_to_samp_freq; i++) 286 len += scnprintf(buf + len, PAGE_SIZE - len, 287 "%d.%d ", data->variant->regval_to_samp_freq[i][0], 288 data->variant->regval_to_samp_freq[i][1]); 289 290 /* replace trailing space by newline */ 291 buf[len - 1] = '\n'; 292 293 return len; 294 } 295 296 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(hmc5843_show_samp_freq_avail); 297 298 static int hmc5843_set_samp_freq(struct hmc5843_data *data, u8 rate) 299 { 300 int ret; 301 302 mutex_lock(&data->lock); 303 ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_A, 304 HMC5843_RATE_MASK, 305 rate << HMC5843_RATE_OFFSET); 306 mutex_unlock(&data->lock); 307 308 return ret; 309 } 310 311 static int hmc5843_get_samp_freq_index(struct hmc5843_data *data, 312 int val, int val2) 313 { 314 int i; 315 316 for (i = 0; i < data->variant->n_regval_to_samp_freq; i++) 317 if (val == data->variant->regval_to_samp_freq[i][0] && 318 val2 == data->variant->regval_to_samp_freq[i][1]) 319 return i; 320 321 return -EINVAL; 322 } 323 324 static int hmc5843_set_range_gain(struct hmc5843_data *data, u8 range) 325 { 326 int ret; 327 328 mutex_lock(&data->lock); 329 ret = regmap_update_bits(data->regmap, HMC5843_CONFIG_REG_B, 330 HMC5843_RANGE_GAIN_MASK, 331 range << HMC5843_RANGE_GAIN_OFFSET); 332 mutex_unlock(&data->lock); 333 334 return ret; 335 } 336 337 static ssize_t hmc5843_show_scale_avail(struct device *dev, 338 struct device_attribute *attr, 339 char *buf) 340 { 341 struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev)); 342 343 size_t len = 0; 344 int i; 345 346 for (i = 0; i < data->variant->n_regval_to_nanoscale; i++) 347 len += scnprintf(buf + len, PAGE_SIZE - len, 348 "0.%09d ", data->variant->regval_to_nanoscale[i]); 349 350 /* replace trailing space by newline */ 351 buf[len - 1] = '\n'; 352 353 return len; 354 } 355 356 static IIO_DEVICE_ATTR(scale_available, S_IRUGO, 357 hmc5843_show_scale_avail, NULL, 0); 358 359 static int hmc5843_get_scale_index(struct hmc5843_data *data, int val, int val2) 360 { 361 int i; 362 363 if (val) 364 return -EINVAL; 365 366 for (i = 0; i < data->variant->n_regval_to_nanoscale; i++) 367 if (val2 == data->variant->regval_to_nanoscale[i]) 368 return i; 369 370 return -EINVAL; 371 } 372 373 static int hmc5843_read_raw(struct iio_dev *indio_dev, 374 struct iio_chan_spec const *chan, 375 int *val, int *val2, long mask) 376 { 377 struct hmc5843_data *data = iio_priv(indio_dev); 378 unsigned int rval; 379 int ret; 380 381 switch (mask) { 382 case IIO_CHAN_INFO_RAW: 383 return hmc5843_read_measurement(data, chan->scan_index, val); 384 case IIO_CHAN_INFO_SCALE: 385 ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_B, &rval); 386 if (ret < 0) 387 return ret; 388 rval >>= HMC5843_RANGE_GAIN_OFFSET; 389 *val = 0; 390 *val2 = data->variant->regval_to_nanoscale[rval]; 391 return IIO_VAL_INT_PLUS_NANO; 392 case IIO_CHAN_INFO_SAMP_FREQ: 393 ret = regmap_read(data->regmap, HMC5843_CONFIG_REG_A, &rval); 394 if (ret < 0) 395 return ret; 396 rval >>= HMC5843_RATE_OFFSET; 397 *val = data->variant->regval_to_samp_freq[rval][0]; 398 *val2 = data->variant->regval_to_samp_freq[rval][1]; 399 return IIO_VAL_INT_PLUS_MICRO; 400 } 401 return -EINVAL; 402 } 403 404 static int hmc5843_write_raw(struct iio_dev *indio_dev, 405 struct iio_chan_spec const *chan, 406 int val, int val2, long mask) 407 { 408 struct hmc5843_data *data = iio_priv(indio_dev); 409 int rate, range; 410 411 switch (mask) { 412 case IIO_CHAN_INFO_SAMP_FREQ: 413 rate = hmc5843_get_samp_freq_index(data, val, val2); 414 if (rate < 0) 415 return -EINVAL; 416 417 return hmc5843_set_samp_freq(data, rate); 418 case IIO_CHAN_INFO_SCALE: 419 range = hmc5843_get_scale_index(data, val, val2); 420 if (range < 0) 421 return -EINVAL; 422 423 return hmc5843_set_range_gain(data, range); 424 default: 425 return -EINVAL; 426 } 427 } 428 429 static int hmc5843_write_raw_get_fmt(struct iio_dev *indio_dev, 430 struct iio_chan_spec const *chan, 431 long mask) 432 { 433 switch (mask) { 434 case IIO_CHAN_INFO_SAMP_FREQ: 435 return IIO_VAL_INT_PLUS_MICRO; 436 case IIO_CHAN_INFO_SCALE: 437 return IIO_VAL_INT_PLUS_NANO; 438 default: 439 return -EINVAL; 440 } 441 } 442 443 static irqreturn_t hmc5843_trigger_handler(int irq, void *p) 444 { 445 struct iio_poll_func *pf = p; 446 struct iio_dev *indio_dev = pf->indio_dev; 447 struct hmc5843_data *data = iio_priv(indio_dev); 448 int ret; 449 450 mutex_lock(&data->lock); 451 ret = hmc5843_wait_measurement(data); 452 if (ret < 0) { 453 mutex_unlock(&data->lock); 454 goto done; 455 } 456 457 ret = regmap_bulk_read(data->regmap, HMC5843_DATA_OUT_MSB_REGS, 458 data->buffer, 3 * sizeof(__be16)); 459 460 mutex_unlock(&data->lock); 461 if (ret < 0) 462 goto done; 463 464 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 465 iio_get_time_ns(indio_dev)); 466 467 done: 468 iio_trigger_notify_done(indio_dev->trig); 469 470 return IRQ_HANDLED; 471 } 472 473 #define HMC5843_CHANNEL(axis, idx) \ 474 { \ 475 .type = IIO_MAGN, \ 476 .modified = 1, \ 477 .channel2 = IIO_MOD_##axis, \ 478 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 479 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 480 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 481 .scan_index = idx, \ 482 .scan_type = { \ 483 .sign = 's', \ 484 .realbits = 16, \ 485 .storagebits = 16, \ 486 .endianness = IIO_BE, \ 487 }, \ 488 .ext_info = hmc5843_ext_info, \ 489 } 490 491 #define HMC5983_CHANNEL(axis, idx) \ 492 { \ 493 .type = IIO_MAGN, \ 494 .modified = 1, \ 495 .channel2 = IIO_MOD_##axis, \ 496 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 497 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 498 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 499 .scan_index = idx, \ 500 .scan_type = { \ 501 .sign = 's', \ 502 .realbits = 16, \ 503 .storagebits = 16, \ 504 .endianness = IIO_BE, \ 505 }, \ 506 .ext_info = hmc5983_ext_info, \ 507 } 508 509 static const struct iio_chan_spec hmc5843_channels[] = { 510 HMC5843_CHANNEL(X, 0), 511 HMC5843_CHANNEL(Y, 1), 512 HMC5843_CHANNEL(Z, 2), 513 IIO_CHAN_SOFT_TIMESTAMP(3), 514 }; 515 516 /* Beware: Y and Z are exchanged on HMC5883 and 5983 */ 517 static const struct iio_chan_spec hmc5883_channels[] = { 518 HMC5843_CHANNEL(X, 0), 519 HMC5843_CHANNEL(Z, 1), 520 HMC5843_CHANNEL(Y, 2), 521 IIO_CHAN_SOFT_TIMESTAMP(3), 522 }; 523 524 static const struct iio_chan_spec hmc5983_channels[] = { 525 HMC5983_CHANNEL(X, 0), 526 HMC5983_CHANNEL(Z, 1), 527 HMC5983_CHANNEL(Y, 2), 528 IIO_CHAN_SOFT_TIMESTAMP(3), 529 }; 530 531 static struct attribute *hmc5843_attributes[] = { 532 &iio_dev_attr_scale_available.dev_attr.attr, 533 &iio_dev_attr_sampling_frequency_available.dev_attr.attr, 534 NULL 535 }; 536 537 static const struct attribute_group hmc5843_group = { 538 .attrs = hmc5843_attributes, 539 }; 540 541 static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = { 542 [HMC5843_ID] = { 543 .channels = hmc5843_channels, 544 .regval_to_samp_freq = hmc5843_regval_to_samp_freq, 545 .n_regval_to_samp_freq = 546 ARRAY_SIZE(hmc5843_regval_to_samp_freq), 547 .regval_to_nanoscale = hmc5843_regval_to_nanoscale, 548 .n_regval_to_nanoscale = 549 ARRAY_SIZE(hmc5843_regval_to_nanoscale), 550 }, 551 [HMC5883_ID] = { 552 .channels = hmc5883_channels, 553 .regval_to_samp_freq = hmc5883_regval_to_samp_freq, 554 .n_regval_to_samp_freq = 555 ARRAY_SIZE(hmc5883_regval_to_samp_freq), 556 .regval_to_nanoscale = hmc5883_regval_to_nanoscale, 557 .n_regval_to_nanoscale = 558 ARRAY_SIZE(hmc5883_regval_to_nanoscale), 559 }, 560 [HMC5883L_ID] = { 561 .channels = hmc5883_channels, 562 .regval_to_samp_freq = hmc5883_regval_to_samp_freq, 563 .n_regval_to_samp_freq = 564 ARRAY_SIZE(hmc5883_regval_to_samp_freq), 565 .regval_to_nanoscale = hmc5883l_regval_to_nanoscale, 566 .n_regval_to_nanoscale = 567 ARRAY_SIZE(hmc5883l_regval_to_nanoscale), 568 }, 569 [HMC5983_ID] = { 570 .channels = hmc5983_channels, 571 .regval_to_samp_freq = hmc5983_regval_to_samp_freq, 572 .n_regval_to_samp_freq = 573 ARRAY_SIZE(hmc5983_regval_to_samp_freq), 574 .regval_to_nanoscale = hmc5883l_regval_to_nanoscale, 575 .n_regval_to_nanoscale = 576 ARRAY_SIZE(hmc5883l_regval_to_nanoscale), 577 } 578 }; 579 580 static int hmc5843_init(struct hmc5843_data *data) 581 { 582 int ret; 583 u8 id[3]; 584 585 ret = regmap_bulk_read(data->regmap, HMC5843_ID_REG, 586 id, ARRAY_SIZE(id)); 587 if (ret < 0) 588 return ret; 589 if (id[0] != 'H' || id[1] != '4' || id[2] != '3') { 590 dev_err(data->dev, "no HMC5843/5883/5883L/5983 sensor\n"); 591 return -ENODEV; 592 } 593 594 ret = hmc5843_set_meas_conf(data, HMC5843_MEAS_CONF_NORMAL); 595 if (ret < 0) 596 return ret; 597 ret = hmc5843_set_samp_freq(data, HMC5843_RATE_DEFAULT); 598 if (ret < 0) 599 return ret; 600 ret = hmc5843_set_range_gain(data, HMC5843_RANGE_GAIN_DEFAULT); 601 if (ret < 0) 602 return ret; 603 return hmc5843_set_mode(data, HMC5843_MODE_CONVERSION_CONTINUOUS); 604 } 605 606 static const struct iio_info hmc5843_info = { 607 .attrs = &hmc5843_group, 608 .read_raw = &hmc5843_read_raw, 609 .write_raw = &hmc5843_write_raw, 610 .write_raw_get_fmt = &hmc5843_write_raw_get_fmt, 611 }; 612 613 static const unsigned long hmc5843_scan_masks[] = {0x7, 0}; 614 615 int hmc5843_common_suspend(struct device *dev) 616 { 617 return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev)), 618 HMC5843_MODE_SLEEP); 619 } 620 EXPORT_SYMBOL(hmc5843_common_suspend); 621 622 int hmc5843_common_resume(struct device *dev) 623 { 624 return hmc5843_set_mode(iio_priv(dev_get_drvdata(dev)), 625 HMC5843_MODE_CONVERSION_CONTINUOUS); 626 } 627 EXPORT_SYMBOL(hmc5843_common_resume); 628 629 int hmc5843_common_probe(struct device *dev, struct regmap *regmap, 630 enum hmc5843_ids id, const char *name) 631 { 632 struct hmc5843_data *data; 633 struct iio_dev *indio_dev; 634 int ret; 635 636 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 637 if (!indio_dev) 638 return -ENOMEM; 639 640 dev_set_drvdata(dev, indio_dev); 641 642 /* default settings at probe */ 643 data = iio_priv(indio_dev); 644 data->dev = dev; 645 data->regmap = regmap; 646 data->variant = &hmc5843_chip_info_tbl[id]; 647 mutex_init(&data->lock); 648 649 ret = iio_read_mount_matrix(dev, "mount-matrix", 650 &data->orientation); 651 if (ret) 652 return ret; 653 654 indio_dev->dev.parent = dev; 655 indio_dev->name = name; 656 indio_dev->info = &hmc5843_info; 657 indio_dev->modes = INDIO_DIRECT_MODE; 658 indio_dev->channels = data->variant->channels; 659 indio_dev->num_channels = 4; 660 indio_dev->available_scan_masks = hmc5843_scan_masks; 661 662 ret = hmc5843_init(data); 663 if (ret < 0) 664 return ret; 665 666 ret = iio_triggered_buffer_setup(indio_dev, NULL, 667 hmc5843_trigger_handler, NULL); 668 if (ret < 0) 669 goto buffer_setup_err; 670 671 ret = iio_device_register(indio_dev); 672 if (ret < 0) 673 goto buffer_cleanup; 674 675 return 0; 676 677 buffer_cleanup: 678 iio_triggered_buffer_cleanup(indio_dev); 679 buffer_setup_err: 680 hmc5843_set_mode(iio_priv(indio_dev), HMC5843_MODE_SLEEP); 681 return ret; 682 } 683 EXPORT_SYMBOL(hmc5843_common_probe); 684 685 int hmc5843_common_remove(struct device *dev) 686 { 687 struct iio_dev *indio_dev = dev_get_drvdata(dev); 688 689 iio_device_unregister(indio_dev); 690 iio_triggered_buffer_cleanup(indio_dev); 691 692 /* sleep mode to save power */ 693 hmc5843_set_mode(iio_priv(indio_dev), HMC5843_MODE_SLEEP); 694 695 return 0; 696 } 697 EXPORT_SYMBOL(hmc5843_common_remove); 698 699 MODULE_AUTHOR("Shubhrajyoti Datta <shubhrajyoti@ti.com>"); 700 MODULE_DESCRIPTION("HMC5843/5883/5883L/5983 core driver"); 701 MODULE_LICENSE("GPL"); 702