1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * atlas-sensor.c - Support for Atlas Scientific OEM SM sensors 4 * 5 * Copyright (C) 2015-2019 Konsulko Group 6 * Author: Matt Ranostay <matt.ranostay@konsulko.com> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/init.h> 11 #include <linux/interrupt.h> 12 #include <linux/delay.h> 13 #include <linux/mutex.h> 14 #include <linux/err.h> 15 #include <linux/irq.h> 16 #include <linux/irq_work.h> 17 #include <linux/i2c.h> 18 #include <linux/of_device.h> 19 #include <linux/regmap.h> 20 #include <linux/iio/iio.h> 21 #include <linux/iio/buffer.h> 22 #include <linux/iio/trigger.h> 23 #include <linux/iio/trigger_consumer.h> 24 #include <linux/iio/triggered_buffer.h> 25 #include <linux/pm_runtime.h> 26 27 #define ATLAS_REGMAP_NAME "atlas_regmap" 28 #define ATLAS_DRV_NAME "atlas" 29 30 #define ATLAS_REG_DEV_TYPE 0x00 31 #define ATLAS_REG_DEV_VERSION 0x01 32 33 #define ATLAS_REG_INT_CONTROL 0x04 34 #define ATLAS_REG_INT_CONTROL_EN BIT(3) 35 36 #define ATLAS_REG_PWR_CONTROL 0x06 37 38 #define ATLAS_REG_PH_CALIB_STATUS 0x0d 39 #define ATLAS_REG_PH_CALIB_STATUS_MASK 0x07 40 #define ATLAS_REG_PH_CALIB_STATUS_LOW BIT(0) 41 #define ATLAS_REG_PH_CALIB_STATUS_MID BIT(1) 42 #define ATLAS_REG_PH_CALIB_STATUS_HIGH BIT(2) 43 44 #define ATLAS_REG_EC_CALIB_STATUS 0x0f 45 #define ATLAS_REG_EC_CALIB_STATUS_MASK 0x0f 46 #define ATLAS_REG_EC_CALIB_STATUS_DRY BIT(0) 47 #define ATLAS_REG_EC_CALIB_STATUS_SINGLE BIT(1) 48 #define ATLAS_REG_EC_CALIB_STATUS_LOW BIT(2) 49 #define ATLAS_REG_EC_CALIB_STATUS_HIGH BIT(3) 50 51 #define ATLAS_REG_DO_CALIB_STATUS 0x09 52 #define ATLAS_REG_DO_CALIB_STATUS_MASK 0x03 53 #define ATLAS_REG_DO_CALIB_STATUS_PRESSURE BIT(0) 54 #define ATLAS_REG_DO_CALIB_STATUS_DO BIT(1) 55 56 #define ATLAS_REG_PH_TEMP_DATA 0x0e 57 #define ATLAS_REG_PH_DATA 0x16 58 59 #define ATLAS_REG_EC_PROBE 0x08 60 #define ATLAS_REG_EC_TEMP_DATA 0x10 61 #define ATLAS_REG_EC_DATA 0x18 62 #define ATLAS_REG_TDS_DATA 0x1c 63 #define ATLAS_REG_PSS_DATA 0x20 64 65 #define ATLAS_REG_ORP_CALIB_STATUS 0x0d 66 #define ATLAS_REG_ORP_DATA 0x0e 67 68 #define ATLAS_REG_DO_TEMP_DATA 0x12 69 #define ATLAS_REG_DO_DATA 0x22 70 71 #define ATLAS_PH_INT_TIME_IN_MS 450 72 #define ATLAS_EC_INT_TIME_IN_MS 650 73 #define ATLAS_ORP_INT_TIME_IN_MS 450 74 #define ATLAS_DO_INT_TIME_IN_MS 450 75 76 enum { 77 ATLAS_PH_SM, 78 ATLAS_EC_SM, 79 ATLAS_ORP_SM, 80 ATLAS_DO_SM, 81 }; 82 83 struct atlas_data { 84 struct i2c_client *client; 85 struct iio_trigger *trig; 86 struct atlas_device *chip; 87 struct regmap *regmap; 88 struct irq_work work; 89 unsigned int interrupt_enabled; 90 91 __be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */ 92 }; 93 94 static const struct regmap_config atlas_regmap_config = { 95 .name = ATLAS_REGMAP_NAME, 96 .reg_bits = 8, 97 .val_bits = 8, 98 }; 99 100 static int atlas_buffer_num_channels(const struct iio_chan_spec *spec) 101 { 102 int idx = 0; 103 104 for (; spec->type != IIO_TIMESTAMP; spec++) 105 idx++; 106 107 return idx; 108 }; 109 110 static const struct iio_chan_spec atlas_ph_channels[] = { 111 { 112 .type = IIO_PH, 113 .address = ATLAS_REG_PH_DATA, 114 .info_mask_separate = 115 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 116 .scan_index = 0, 117 .scan_type = { 118 .sign = 'u', 119 .realbits = 32, 120 .storagebits = 32, 121 .endianness = IIO_BE, 122 }, 123 }, 124 IIO_CHAN_SOFT_TIMESTAMP(1), 125 { 126 .type = IIO_TEMP, 127 .address = ATLAS_REG_PH_TEMP_DATA, 128 .info_mask_separate = 129 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 130 .output = 1, 131 .scan_index = -1 132 }, 133 }; 134 135 #define ATLAS_CONCENTRATION_CHANNEL(_idx, _addr) \ 136 {\ 137 .type = IIO_CONCENTRATION, \ 138 .indexed = 1, \ 139 .channel = _idx, \ 140 .address = _addr, \ 141 .info_mask_separate = \ 142 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \ 143 .scan_index = _idx + 1, \ 144 .scan_type = { \ 145 .sign = 'u', \ 146 .realbits = 32, \ 147 .storagebits = 32, \ 148 .endianness = IIO_BE, \ 149 }, \ 150 } 151 152 static const struct iio_chan_spec atlas_ec_channels[] = { 153 { 154 .type = IIO_ELECTRICALCONDUCTIVITY, 155 .address = ATLAS_REG_EC_DATA, 156 .info_mask_separate = 157 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 158 .scan_index = 0, 159 .scan_type = { 160 .sign = 'u', 161 .realbits = 32, 162 .storagebits = 32, 163 .endianness = IIO_BE, 164 }, 165 }, 166 ATLAS_CONCENTRATION_CHANNEL(0, ATLAS_REG_TDS_DATA), 167 ATLAS_CONCENTRATION_CHANNEL(1, ATLAS_REG_PSS_DATA), 168 IIO_CHAN_SOFT_TIMESTAMP(3), 169 { 170 .type = IIO_TEMP, 171 .address = ATLAS_REG_EC_TEMP_DATA, 172 .info_mask_separate = 173 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 174 .output = 1, 175 .scan_index = -1 176 }, 177 }; 178 179 static const struct iio_chan_spec atlas_orp_channels[] = { 180 { 181 .type = IIO_VOLTAGE, 182 .address = ATLAS_REG_ORP_DATA, 183 .info_mask_separate = 184 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 185 .scan_index = 0, 186 .scan_type = { 187 .sign = 's', 188 .realbits = 32, 189 .storagebits = 32, 190 .endianness = IIO_BE, 191 }, 192 }, 193 IIO_CHAN_SOFT_TIMESTAMP(1), 194 }; 195 196 static const struct iio_chan_spec atlas_do_channels[] = { 197 ATLAS_CONCENTRATION_CHANNEL(0, ATLAS_REG_DO_DATA), 198 IIO_CHAN_SOFT_TIMESTAMP(1), 199 { 200 .type = IIO_TEMP, 201 .address = ATLAS_REG_DO_TEMP_DATA, 202 .info_mask_separate = 203 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 204 .output = 1, 205 .scan_index = -1 206 }, 207 }; 208 209 static int atlas_check_ph_calibration(struct atlas_data *data) 210 { 211 struct device *dev = &data->client->dev; 212 int ret; 213 unsigned int val; 214 215 ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val); 216 if (ret) 217 return ret; 218 219 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) { 220 dev_warn(dev, "device has not been calibrated\n"); 221 return 0; 222 } 223 224 if (!(val & ATLAS_REG_PH_CALIB_STATUS_LOW)) 225 dev_warn(dev, "device missing low point calibration\n"); 226 227 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MID)) 228 dev_warn(dev, "device missing mid point calibration\n"); 229 230 if (!(val & ATLAS_REG_PH_CALIB_STATUS_HIGH)) 231 dev_warn(dev, "device missing high point calibration\n"); 232 233 return 0; 234 } 235 236 static int atlas_check_ec_calibration(struct atlas_data *data) 237 { 238 struct device *dev = &data->client->dev; 239 int ret; 240 unsigned int val; 241 __be16 rval; 242 243 ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &rval, 2); 244 if (ret) 245 return ret; 246 247 val = be16_to_cpu(rval); 248 dev_info(dev, "probe set to K = %d.%.2d", val / 100, val % 100); 249 250 ret = regmap_read(data->regmap, ATLAS_REG_EC_CALIB_STATUS, &val); 251 if (ret) 252 return ret; 253 254 if (!(val & ATLAS_REG_EC_CALIB_STATUS_MASK)) { 255 dev_warn(dev, "device has not been calibrated\n"); 256 return 0; 257 } 258 259 if (!(val & ATLAS_REG_EC_CALIB_STATUS_DRY)) 260 dev_warn(dev, "device missing dry point calibration\n"); 261 262 if (val & ATLAS_REG_EC_CALIB_STATUS_SINGLE) { 263 dev_warn(dev, "device using single point calibration\n"); 264 } else { 265 if (!(val & ATLAS_REG_EC_CALIB_STATUS_LOW)) 266 dev_warn(dev, "device missing low point calibration\n"); 267 268 if (!(val & ATLAS_REG_EC_CALIB_STATUS_HIGH)) 269 dev_warn(dev, "device missing high point calibration\n"); 270 } 271 272 return 0; 273 } 274 275 static int atlas_check_orp_calibration(struct atlas_data *data) 276 { 277 struct device *dev = &data->client->dev; 278 int ret; 279 unsigned int val; 280 281 ret = regmap_read(data->regmap, ATLAS_REG_ORP_CALIB_STATUS, &val); 282 if (ret) 283 return ret; 284 285 if (!val) 286 dev_warn(dev, "device has not been calibrated\n"); 287 288 return 0; 289 } 290 291 static int atlas_check_do_calibration(struct atlas_data *data) 292 { 293 struct device *dev = &data->client->dev; 294 int ret; 295 unsigned int val; 296 297 ret = regmap_read(data->regmap, ATLAS_REG_DO_CALIB_STATUS, &val); 298 if (ret) 299 return ret; 300 301 if (!(val & ATLAS_REG_DO_CALIB_STATUS_MASK)) { 302 dev_warn(dev, "device has not been calibrated\n"); 303 return 0; 304 } 305 306 if (!(val & ATLAS_REG_DO_CALIB_STATUS_PRESSURE)) 307 dev_warn(dev, "device missing atmospheric pressure calibration\n"); 308 309 if (!(val & ATLAS_REG_DO_CALIB_STATUS_DO)) 310 dev_warn(dev, "device missing dissolved oxygen calibration\n"); 311 312 return 0; 313 } 314 315 struct atlas_device { 316 const struct iio_chan_spec *channels; 317 int num_channels; 318 int data_reg; 319 320 int (*calibration)(struct atlas_data *data); 321 int delay; 322 }; 323 324 static struct atlas_device atlas_devices[] = { 325 [ATLAS_PH_SM] = { 326 .channels = atlas_ph_channels, 327 .num_channels = 3, 328 .data_reg = ATLAS_REG_PH_DATA, 329 .calibration = &atlas_check_ph_calibration, 330 .delay = ATLAS_PH_INT_TIME_IN_MS, 331 }, 332 [ATLAS_EC_SM] = { 333 .channels = atlas_ec_channels, 334 .num_channels = 5, 335 .data_reg = ATLAS_REG_EC_DATA, 336 .calibration = &atlas_check_ec_calibration, 337 .delay = ATLAS_EC_INT_TIME_IN_MS, 338 }, 339 [ATLAS_ORP_SM] = { 340 .channels = atlas_orp_channels, 341 .num_channels = 2, 342 .data_reg = ATLAS_REG_ORP_DATA, 343 .calibration = &atlas_check_orp_calibration, 344 .delay = ATLAS_ORP_INT_TIME_IN_MS, 345 }, 346 [ATLAS_DO_SM] = { 347 .channels = atlas_do_channels, 348 .num_channels = 3, 349 .data_reg = ATLAS_REG_DO_DATA, 350 .calibration = &atlas_check_do_calibration, 351 .delay = ATLAS_DO_INT_TIME_IN_MS, 352 }, 353 }; 354 355 static int atlas_set_powermode(struct atlas_data *data, int on) 356 { 357 return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on); 358 } 359 360 static int atlas_set_interrupt(struct atlas_data *data, bool state) 361 { 362 if (!data->interrupt_enabled) 363 return 0; 364 365 return regmap_update_bits(data->regmap, ATLAS_REG_INT_CONTROL, 366 ATLAS_REG_INT_CONTROL_EN, 367 state ? ATLAS_REG_INT_CONTROL_EN : 0); 368 } 369 370 static int atlas_buffer_postenable(struct iio_dev *indio_dev) 371 { 372 struct atlas_data *data = iio_priv(indio_dev); 373 int ret; 374 375 ret = iio_triggered_buffer_postenable(indio_dev); 376 if (ret) 377 return ret; 378 379 ret = pm_runtime_get_sync(&data->client->dev); 380 if (ret < 0) { 381 pm_runtime_put_noidle(&data->client->dev); 382 return ret; 383 } 384 385 return atlas_set_interrupt(data, true); 386 } 387 388 static int atlas_buffer_predisable(struct iio_dev *indio_dev) 389 { 390 struct atlas_data *data = iio_priv(indio_dev); 391 int ret; 392 393 ret = atlas_set_interrupt(data, false); 394 if (ret) 395 return ret; 396 397 pm_runtime_mark_last_busy(&data->client->dev); 398 ret = pm_runtime_put_autosuspend(&data->client->dev); 399 if (ret) 400 return ret; 401 402 return iio_triggered_buffer_predisable(indio_dev); 403 } 404 405 static const struct iio_trigger_ops atlas_interrupt_trigger_ops = { 406 }; 407 408 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = { 409 .postenable = atlas_buffer_postenable, 410 .predisable = atlas_buffer_predisable, 411 }; 412 413 static void atlas_work_handler(struct irq_work *work) 414 { 415 struct atlas_data *data = container_of(work, struct atlas_data, work); 416 417 iio_trigger_poll(data->trig); 418 } 419 420 static irqreturn_t atlas_trigger_handler(int irq, void *private) 421 { 422 struct iio_poll_func *pf = private; 423 struct iio_dev *indio_dev = pf->indio_dev; 424 struct atlas_data *data = iio_priv(indio_dev); 425 int channels = atlas_buffer_num_channels(data->chip->channels); 426 int ret; 427 428 ret = regmap_bulk_read(data->regmap, data->chip->data_reg, 429 (u8 *) &data->buffer, 430 sizeof(__be32) * channels); 431 432 if (!ret) 433 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 434 iio_get_time_ns(indio_dev)); 435 436 iio_trigger_notify_done(indio_dev->trig); 437 438 return IRQ_HANDLED; 439 } 440 441 static irqreturn_t atlas_interrupt_handler(int irq, void *private) 442 { 443 struct iio_dev *indio_dev = private; 444 struct atlas_data *data = iio_priv(indio_dev); 445 446 irq_work_queue(&data->work); 447 448 return IRQ_HANDLED; 449 } 450 451 static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val) 452 { 453 struct device *dev = &data->client->dev; 454 int suspended = pm_runtime_suspended(dev); 455 int ret; 456 457 ret = pm_runtime_get_sync(dev); 458 if (ret < 0) { 459 pm_runtime_put_noidle(dev); 460 return ret; 461 } 462 463 if (suspended) 464 msleep(data->chip->delay); 465 466 ret = regmap_bulk_read(data->regmap, reg, (u8 *) val, sizeof(*val)); 467 468 pm_runtime_mark_last_busy(dev); 469 pm_runtime_put_autosuspend(dev); 470 471 return ret; 472 } 473 474 static int atlas_read_raw(struct iio_dev *indio_dev, 475 struct iio_chan_spec const *chan, 476 int *val, int *val2, long mask) 477 { 478 struct atlas_data *data = iio_priv(indio_dev); 479 480 switch (mask) { 481 case IIO_CHAN_INFO_RAW: { 482 int ret; 483 __be32 reg; 484 485 switch (chan->type) { 486 case IIO_TEMP: 487 ret = regmap_bulk_read(data->regmap, chan->address, 488 (u8 *) ®, sizeof(reg)); 489 break; 490 case IIO_PH: 491 case IIO_CONCENTRATION: 492 case IIO_ELECTRICALCONDUCTIVITY: 493 case IIO_VOLTAGE: 494 ret = iio_device_claim_direct_mode(indio_dev); 495 if (ret) 496 return ret; 497 498 ret = atlas_read_measurement(data, chan->address, ®); 499 500 iio_device_release_direct_mode(indio_dev); 501 break; 502 default: 503 ret = -EINVAL; 504 } 505 506 if (!ret) { 507 *val = be32_to_cpu(reg); 508 ret = IIO_VAL_INT; 509 } 510 return ret; 511 } 512 case IIO_CHAN_INFO_SCALE: 513 switch (chan->type) { 514 case IIO_TEMP: 515 *val = 10; 516 return IIO_VAL_INT; 517 case IIO_PH: 518 *val = 1; /* 0.001 */ 519 *val2 = 1000; 520 break; 521 case IIO_ELECTRICALCONDUCTIVITY: 522 *val = 1; /* 0.00001 */ 523 *val2 = 100000; 524 break; 525 case IIO_CONCENTRATION: 526 *val = 0; /* 0.000000001 */ 527 *val2 = 1000; 528 return IIO_VAL_INT_PLUS_NANO; 529 case IIO_VOLTAGE: 530 *val = 1; /* 0.1 */ 531 *val2 = 10; 532 break; 533 default: 534 return -EINVAL; 535 } 536 return IIO_VAL_FRACTIONAL; 537 } 538 539 return -EINVAL; 540 } 541 542 static int atlas_write_raw(struct iio_dev *indio_dev, 543 struct iio_chan_spec const *chan, 544 int val, int val2, long mask) 545 { 546 struct atlas_data *data = iio_priv(indio_dev); 547 __be32 reg = cpu_to_be32(val / 10); 548 549 if (val2 != 0 || val < 0 || val > 20000) 550 return -EINVAL; 551 552 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP) 553 return -EINVAL; 554 555 return regmap_bulk_write(data->regmap, chan->address, 556 ®, sizeof(reg)); 557 } 558 559 static const struct iio_info atlas_info = { 560 .read_raw = atlas_read_raw, 561 .write_raw = atlas_write_raw, 562 }; 563 564 static const struct i2c_device_id atlas_id[] = { 565 { "atlas-ph-sm", ATLAS_PH_SM}, 566 { "atlas-ec-sm", ATLAS_EC_SM}, 567 { "atlas-orp-sm", ATLAS_ORP_SM}, 568 { "atlas-do-sm", ATLAS_DO_SM}, 569 {} 570 }; 571 MODULE_DEVICE_TABLE(i2c, atlas_id); 572 573 static const struct of_device_id atlas_dt_ids[] = { 574 { .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, }, 575 { .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, }, 576 { .compatible = "atlas,orp-sm", .data = (void *)ATLAS_ORP_SM, }, 577 { .compatible = "atlas,do-sm", .data = (void *)ATLAS_DO_SM, }, 578 { } 579 }; 580 MODULE_DEVICE_TABLE(of, atlas_dt_ids); 581 582 static int atlas_probe(struct i2c_client *client, 583 const struct i2c_device_id *id) 584 { 585 struct atlas_data *data; 586 struct atlas_device *chip; 587 const struct of_device_id *of_id; 588 struct iio_trigger *trig; 589 struct iio_dev *indio_dev; 590 int ret; 591 592 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 593 if (!indio_dev) 594 return -ENOMEM; 595 596 of_id = of_match_device(atlas_dt_ids, &client->dev); 597 if (!of_id) 598 chip = &atlas_devices[id->driver_data]; 599 else 600 chip = &atlas_devices[(unsigned long)of_id->data]; 601 602 indio_dev->info = &atlas_info; 603 indio_dev->name = ATLAS_DRV_NAME; 604 indio_dev->channels = chip->channels; 605 indio_dev->num_channels = chip->num_channels; 606 indio_dev->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE; 607 indio_dev->dev.parent = &client->dev; 608 609 trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d", 610 indio_dev->name, indio_dev->id); 611 612 if (!trig) 613 return -ENOMEM; 614 615 data = iio_priv(indio_dev); 616 data->client = client; 617 data->trig = trig; 618 data->chip = chip; 619 trig->dev.parent = indio_dev->dev.parent; 620 trig->ops = &atlas_interrupt_trigger_ops; 621 iio_trigger_set_drvdata(trig, indio_dev); 622 623 i2c_set_clientdata(client, indio_dev); 624 625 data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config); 626 if (IS_ERR(data->regmap)) { 627 dev_err(&client->dev, "regmap initialization failed\n"); 628 return PTR_ERR(data->regmap); 629 } 630 631 ret = pm_runtime_set_active(&client->dev); 632 if (ret) 633 return ret; 634 635 ret = chip->calibration(data); 636 if (ret) 637 return ret; 638 639 ret = iio_trigger_register(trig); 640 if (ret) { 641 dev_err(&client->dev, "failed to register trigger\n"); 642 return ret; 643 } 644 645 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 646 &atlas_trigger_handler, &atlas_buffer_setup_ops); 647 if (ret) { 648 dev_err(&client->dev, "cannot setup iio trigger\n"); 649 goto unregister_trigger; 650 } 651 652 init_irq_work(&data->work, atlas_work_handler); 653 654 if (client->irq > 0) { 655 /* interrupt pin toggles on new conversion */ 656 ret = devm_request_threaded_irq(&client->dev, client->irq, 657 NULL, atlas_interrupt_handler, 658 IRQF_TRIGGER_RISING | 659 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 660 "atlas_irq", 661 indio_dev); 662 663 if (ret) 664 dev_warn(&client->dev, 665 "request irq (%d) failed\n", client->irq); 666 else 667 data->interrupt_enabled = 1; 668 } 669 670 ret = atlas_set_powermode(data, 1); 671 if (ret) { 672 dev_err(&client->dev, "cannot power device on"); 673 goto unregister_buffer; 674 } 675 676 pm_runtime_enable(&client->dev); 677 pm_runtime_set_autosuspend_delay(&client->dev, 2500); 678 pm_runtime_use_autosuspend(&client->dev); 679 680 ret = iio_device_register(indio_dev); 681 if (ret) { 682 dev_err(&client->dev, "unable to register device\n"); 683 goto unregister_pm; 684 } 685 686 return 0; 687 688 unregister_pm: 689 pm_runtime_disable(&client->dev); 690 atlas_set_powermode(data, 0); 691 692 unregister_buffer: 693 iio_triggered_buffer_cleanup(indio_dev); 694 695 unregister_trigger: 696 iio_trigger_unregister(data->trig); 697 698 return ret; 699 } 700 701 static int atlas_remove(struct i2c_client *client) 702 { 703 struct iio_dev *indio_dev = i2c_get_clientdata(client); 704 struct atlas_data *data = iio_priv(indio_dev); 705 706 iio_device_unregister(indio_dev); 707 iio_triggered_buffer_cleanup(indio_dev); 708 iio_trigger_unregister(data->trig); 709 710 pm_runtime_disable(&client->dev); 711 pm_runtime_set_suspended(&client->dev); 712 pm_runtime_put_noidle(&client->dev); 713 714 return atlas_set_powermode(data, 0); 715 } 716 717 #ifdef CONFIG_PM 718 static int atlas_runtime_suspend(struct device *dev) 719 { 720 struct atlas_data *data = 721 iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 722 723 return atlas_set_powermode(data, 0); 724 } 725 726 static int atlas_runtime_resume(struct device *dev) 727 { 728 struct atlas_data *data = 729 iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 730 731 return atlas_set_powermode(data, 1); 732 } 733 #endif 734 735 static const struct dev_pm_ops atlas_pm_ops = { 736 SET_RUNTIME_PM_OPS(atlas_runtime_suspend, 737 atlas_runtime_resume, NULL) 738 }; 739 740 static struct i2c_driver atlas_driver = { 741 .driver = { 742 .name = ATLAS_DRV_NAME, 743 .of_match_table = of_match_ptr(atlas_dt_ids), 744 .pm = &atlas_pm_ops, 745 }, 746 .probe = atlas_probe, 747 .remove = atlas_remove, 748 .id_table = atlas_id, 749 }; 750 module_i2c_driver(atlas_driver); 751 752 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); 753 MODULE_DESCRIPTION("Atlas Scientific SM sensors"); 754 MODULE_LICENSE("GPL"); 755