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 { 198 .type = IIO_CONCENTRATION, 199 .address = ATLAS_REG_DO_DATA, 200 .info_mask_separate = 201 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 202 .scan_index = 0, 203 .scan_type = { 204 .sign = 'u', 205 .realbits = 32, 206 .storagebits = 32, 207 .endianness = IIO_BE, 208 }, 209 }, 210 IIO_CHAN_SOFT_TIMESTAMP(1), 211 { 212 .type = IIO_TEMP, 213 .address = ATLAS_REG_DO_TEMP_DATA, 214 .info_mask_separate = 215 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 216 .output = 1, 217 .scan_index = -1 218 }, 219 }; 220 221 static int atlas_check_ph_calibration(struct atlas_data *data) 222 { 223 struct device *dev = &data->client->dev; 224 int ret; 225 unsigned int val; 226 227 ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val); 228 if (ret) 229 return ret; 230 231 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) { 232 dev_warn(dev, "device has not been calibrated\n"); 233 return 0; 234 } 235 236 if (!(val & ATLAS_REG_PH_CALIB_STATUS_LOW)) 237 dev_warn(dev, "device missing low point calibration\n"); 238 239 if (!(val & ATLAS_REG_PH_CALIB_STATUS_MID)) 240 dev_warn(dev, "device missing mid point calibration\n"); 241 242 if (!(val & ATLAS_REG_PH_CALIB_STATUS_HIGH)) 243 dev_warn(dev, "device missing high point calibration\n"); 244 245 return 0; 246 } 247 248 static int atlas_check_ec_calibration(struct atlas_data *data) 249 { 250 struct device *dev = &data->client->dev; 251 int ret; 252 unsigned int val; 253 __be16 rval; 254 255 ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &rval, 2); 256 if (ret) 257 return ret; 258 259 val = be16_to_cpu(rval); 260 dev_info(dev, "probe set to K = %d.%.2d", val / 100, val % 100); 261 262 ret = regmap_read(data->regmap, ATLAS_REG_EC_CALIB_STATUS, &val); 263 if (ret) 264 return ret; 265 266 if (!(val & ATLAS_REG_EC_CALIB_STATUS_MASK)) { 267 dev_warn(dev, "device has not been calibrated\n"); 268 return 0; 269 } 270 271 if (!(val & ATLAS_REG_EC_CALIB_STATUS_DRY)) 272 dev_warn(dev, "device missing dry point calibration\n"); 273 274 if (val & ATLAS_REG_EC_CALIB_STATUS_SINGLE) { 275 dev_warn(dev, "device using single point calibration\n"); 276 } else { 277 if (!(val & ATLAS_REG_EC_CALIB_STATUS_LOW)) 278 dev_warn(dev, "device missing low point calibration\n"); 279 280 if (!(val & ATLAS_REG_EC_CALIB_STATUS_HIGH)) 281 dev_warn(dev, "device missing high point calibration\n"); 282 } 283 284 return 0; 285 } 286 287 static int atlas_check_orp_calibration(struct atlas_data *data) 288 { 289 struct device *dev = &data->client->dev; 290 int ret; 291 unsigned int val; 292 293 ret = regmap_read(data->regmap, ATLAS_REG_ORP_CALIB_STATUS, &val); 294 if (ret) 295 return ret; 296 297 if (!val) 298 dev_warn(dev, "device has not been calibrated\n"); 299 300 return 0; 301 } 302 303 static int atlas_check_do_calibration(struct atlas_data *data) 304 { 305 struct device *dev = &data->client->dev; 306 int ret; 307 unsigned int val; 308 309 ret = regmap_read(data->regmap, ATLAS_REG_DO_CALIB_STATUS, &val); 310 if (ret) 311 return ret; 312 313 if (!(val & ATLAS_REG_DO_CALIB_STATUS_MASK)) { 314 dev_warn(dev, "device has not been calibrated\n"); 315 return 0; 316 } 317 318 if (!(val & ATLAS_REG_DO_CALIB_STATUS_PRESSURE)) 319 dev_warn(dev, "device missing atmospheric pressure calibration\n"); 320 321 if (!(val & ATLAS_REG_DO_CALIB_STATUS_DO)) 322 dev_warn(dev, "device missing dissolved oxygen calibration\n"); 323 324 return 0; 325 } 326 327 struct atlas_device { 328 const struct iio_chan_spec *channels; 329 int num_channels; 330 int data_reg; 331 332 int (*calibration)(struct atlas_data *data); 333 int delay; 334 }; 335 336 static struct atlas_device atlas_devices[] = { 337 [ATLAS_PH_SM] = { 338 .channels = atlas_ph_channels, 339 .num_channels = 3, 340 .data_reg = ATLAS_REG_PH_DATA, 341 .calibration = &atlas_check_ph_calibration, 342 .delay = ATLAS_PH_INT_TIME_IN_MS, 343 }, 344 [ATLAS_EC_SM] = { 345 .channels = atlas_ec_channels, 346 .num_channels = 5, 347 .data_reg = ATLAS_REG_EC_DATA, 348 .calibration = &atlas_check_ec_calibration, 349 .delay = ATLAS_EC_INT_TIME_IN_MS, 350 }, 351 [ATLAS_ORP_SM] = { 352 .channels = atlas_orp_channels, 353 .num_channels = 2, 354 .data_reg = ATLAS_REG_ORP_DATA, 355 .calibration = &atlas_check_orp_calibration, 356 .delay = ATLAS_ORP_INT_TIME_IN_MS, 357 }, 358 [ATLAS_DO_SM] = { 359 .channels = atlas_do_channels, 360 .num_channels = 3, 361 .data_reg = ATLAS_REG_DO_DATA, 362 .calibration = &atlas_check_do_calibration, 363 .delay = ATLAS_DO_INT_TIME_IN_MS, 364 }, 365 }; 366 367 static int atlas_set_powermode(struct atlas_data *data, int on) 368 { 369 return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on); 370 } 371 372 static int atlas_set_interrupt(struct atlas_data *data, bool state) 373 { 374 if (!data->interrupt_enabled) 375 return 0; 376 377 return regmap_update_bits(data->regmap, ATLAS_REG_INT_CONTROL, 378 ATLAS_REG_INT_CONTROL_EN, 379 state ? ATLAS_REG_INT_CONTROL_EN : 0); 380 } 381 382 static int atlas_buffer_postenable(struct iio_dev *indio_dev) 383 { 384 struct atlas_data *data = iio_priv(indio_dev); 385 int ret; 386 387 ret = iio_triggered_buffer_postenable(indio_dev); 388 if (ret) 389 return ret; 390 391 ret = pm_runtime_get_sync(&data->client->dev); 392 if (ret < 0) { 393 pm_runtime_put_noidle(&data->client->dev); 394 return ret; 395 } 396 397 return atlas_set_interrupt(data, true); 398 } 399 400 static int atlas_buffer_predisable(struct iio_dev *indio_dev) 401 { 402 struct atlas_data *data = iio_priv(indio_dev); 403 int ret; 404 405 ret = atlas_set_interrupt(data, false); 406 if (ret) 407 return ret; 408 409 pm_runtime_mark_last_busy(&data->client->dev); 410 ret = pm_runtime_put_autosuspend(&data->client->dev); 411 if (ret) 412 return ret; 413 414 return iio_triggered_buffer_predisable(indio_dev); 415 } 416 417 static const struct iio_trigger_ops atlas_interrupt_trigger_ops = { 418 }; 419 420 static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = { 421 .postenable = atlas_buffer_postenable, 422 .predisable = atlas_buffer_predisable, 423 }; 424 425 static void atlas_work_handler(struct irq_work *work) 426 { 427 struct atlas_data *data = container_of(work, struct atlas_data, work); 428 429 iio_trigger_poll(data->trig); 430 } 431 432 static irqreturn_t atlas_trigger_handler(int irq, void *private) 433 { 434 struct iio_poll_func *pf = private; 435 struct iio_dev *indio_dev = pf->indio_dev; 436 struct atlas_data *data = iio_priv(indio_dev); 437 int channels = atlas_buffer_num_channels(data->chip->channels); 438 int ret; 439 440 ret = regmap_bulk_read(data->regmap, data->chip->data_reg, 441 (u8 *) &data->buffer, 442 sizeof(__be32) * channels); 443 444 if (!ret) 445 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 446 iio_get_time_ns(indio_dev)); 447 448 iio_trigger_notify_done(indio_dev->trig); 449 450 return IRQ_HANDLED; 451 } 452 453 static irqreturn_t atlas_interrupt_handler(int irq, void *private) 454 { 455 struct iio_dev *indio_dev = private; 456 struct atlas_data *data = iio_priv(indio_dev); 457 458 irq_work_queue(&data->work); 459 460 return IRQ_HANDLED; 461 } 462 463 static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val) 464 { 465 struct device *dev = &data->client->dev; 466 int suspended = pm_runtime_suspended(dev); 467 int ret; 468 469 ret = pm_runtime_get_sync(dev); 470 if (ret < 0) { 471 pm_runtime_put_noidle(dev); 472 return ret; 473 } 474 475 if (suspended) 476 msleep(data->chip->delay); 477 478 ret = regmap_bulk_read(data->regmap, reg, (u8 *) val, sizeof(*val)); 479 480 pm_runtime_mark_last_busy(dev); 481 pm_runtime_put_autosuspend(dev); 482 483 return ret; 484 } 485 486 static int atlas_read_raw(struct iio_dev *indio_dev, 487 struct iio_chan_spec const *chan, 488 int *val, int *val2, long mask) 489 { 490 struct atlas_data *data = iio_priv(indio_dev); 491 492 switch (mask) { 493 case IIO_CHAN_INFO_RAW: { 494 int ret; 495 __be32 reg; 496 497 switch (chan->type) { 498 case IIO_TEMP: 499 ret = regmap_bulk_read(data->regmap, chan->address, 500 (u8 *) ®, sizeof(reg)); 501 break; 502 case IIO_PH: 503 case IIO_CONCENTRATION: 504 case IIO_ELECTRICALCONDUCTIVITY: 505 case IIO_VOLTAGE: 506 ret = iio_device_claim_direct_mode(indio_dev); 507 if (ret) 508 return ret; 509 510 ret = atlas_read_measurement(data, chan->address, ®); 511 512 iio_device_release_direct_mode(indio_dev); 513 break; 514 default: 515 ret = -EINVAL; 516 } 517 518 if (!ret) { 519 *val = be32_to_cpu(reg); 520 ret = IIO_VAL_INT; 521 } 522 return ret; 523 } 524 case IIO_CHAN_INFO_SCALE: 525 switch (chan->type) { 526 case IIO_TEMP: 527 *val = 10; 528 return IIO_VAL_INT; 529 case IIO_PH: 530 *val = 1; /* 0.001 */ 531 *val2 = 1000; 532 break; 533 case IIO_ELECTRICALCONDUCTIVITY: 534 *val = 1; /* 0.00001 */ 535 *val2 = 100000; 536 break; 537 case IIO_CONCENTRATION: 538 *val = 0; /* 0.000000001 */ 539 *val2 = 1000; 540 return IIO_VAL_INT_PLUS_NANO; 541 case IIO_VOLTAGE: 542 *val = 1; /* 0.1 */ 543 *val2 = 10; 544 break; 545 default: 546 return -EINVAL; 547 } 548 return IIO_VAL_FRACTIONAL; 549 } 550 551 return -EINVAL; 552 } 553 554 static int atlas_write_raw(struct iio_dev *indio_dev, 555 struct iio_chan_spec const *chan, 556 int val, int val2, long mask) 557 { 558 struct atlas_data *data = iio_priv(indio_dev); 559 __be32 reg = cpu_to_be32(val / 10); 560 561 if (val2 != 0 || val < 0 || val > 20000) 562 return -EINVAL; 563 564 if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP) 565 return -EINVAL; 566 567 return regmap_bulk_write(data->regmap, chan->address, 568 ®, sizeof(reg)); 569 } 570 571 static const struct iio_info atlas_info = { 572 .read_raw = atlas_read_raw, 573 .write_raw = atlas_write_raw, 574 }; 575 576 static const struct i2c_device_id atlas_id[] = { 577 { "atlas-ph-sm", ATLAS_PH_SM}, 578 { "atlas-ec-sm", ATLAS_EC_SM}, 579 { "atlas-orp-sm", ATLAS_ORP_SM}, 580 { "atlas-do-sm", ATLAS_DO_SM}, 581 {} 582 }; 583 MODULE_DEVICE_TABLE(i2c, atlas_id); 584 585 static const struct of_device_id atlas_dt_ids[] = { 586 { .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, }, 587 { .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, }, 588 { .compatible = "atlas,orp-sm", .data = (void *)ATLAS_ORP_SM, }, 589 { .compatible = "atlas,do-sm", .data = (void *)ATLAS_DO_SM, }, 590 { } 591 }; 592 MODULE_DEVICE_TABLE(of, atlas_dt_ids); 593 594 static int atlas_probe(struct i2c_client *client, 595 const struct i2c_device_id *id) 596 { 597 struct atlas_data *data; 598 struct atlas_device *chip; 599 const struct of_device_id *of_id; 600 struct iio_trigger *trig; 601 struct iio_dev *indio_dev; 602 int ret; 603 604 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 605 if (!indio_dev) 606 return -ENOMEM; 607 608 of_id = of_match_device(atlas_dt_ids, &client->dev); 609 if (!of_id) 610 chip = &atlas_devices[id->driver_data]; 611 else 612 chip = &atlas_devices[(unsigned long)of_id->data]; 613 614 indio_dev->info = &atlas_info; 615 indio_dev->name = ATLAS_DRV_NAME; 616 indio_dev->channels = chip->channels; 617 indio_dev->num_channels = chip->num_channels; 618 indio_dev->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE; 619 indio_dev->dev.parent = &client->dev; 620 621 trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d", 622 indio_dev->name, indio_dev->id); 623 624 if (!trig) 625 return -ENOMEM; 626 627 data = iio_priv(indio_dev); 628 data->client = client; 629 data->trig = trig; 630 data->chip = chip; 631 trig->dev.parent = indio_dev->dev.parent; 632 trig->ops = &atlas_interrupt_trigger_ops; 633 iio_trigger_set_drvdata(trig, indio_dev); 634 635 i2c_set_clientdata(client, indio_dev); 636 637 data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config); 638 if (IS_ERR(data->regmap)) { 639 dev_err(&client->dev, "regmap initialization failed\n"); 640 return PTR_ERR(data->regmap); 641 } 642 643 ret = pm_runtime_set_active(&client->dev); 644 if (ret) 645 return ret; 646 647 ret = chip->calibration(data); 648 if (ret) 649 return ret; 650 651 ret = iio_trigger_register(trig); 652 if (ret) { 653 dev_err(&client->dev, "failed to register trigger\n"); 654 return ret; 655 } 656 657 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 658 &atlas_trigger_handler, &atlas_buffer_setup_ops); 659 if (ret) { 660 dev_err(&client->dev, "cannot setup iio trigger\n"); 661 goto unregister_trigger; 662 } 663 664 init_irq_work(&data->work, atlas_work_handler); 665 666 if (client->irq > 0) { 667 /* interrupt pin toggles on new conversion */ 668 ret = devm_request_threaded_irq(&client->dev, client->irq, 669 NULL, atlas_interrupt_handler, 670 IRQF_TRIGGER_RISING | 671 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 672 "atlas_irq", 673 indio_dev); 674 675 if (ret) 676 dev_warn(&client->dev, 677 "request irq (%d) failed\n", client->irq); 678 else 679 data->interrupt_enabled = 1; 680 } 681 682 ret = atlas_set_powermode(data, 1); 683 if (ret) { 684 dev_err(&client->dev, "cannot power device on"); 685 goto unregister_buffer; 686 } 687 688 pm_runtime_enable(&client->dev); 689 pm_runtime_set_autosuspend_delay(&client->dev, 2500); 690 pm_runtime_use_autosuspend(&client->dev); 691 692 ret = iio_device_register(indio_dev); 693 if (ret) { 694 dev_err(&client->dev, "unable to register device\n"); 695 goto unregister_pm; 696 } 697 698 return 0; 699 700 unregister_pm: 701 pm_runtime_disable(&client->dev); 702 atlas_set_powermode(data, 0); 703 704 unregister_buffer: 705 iio_triggered_buffer_cleanup(indio_dev); 706 707 unregister_trigger: 708 iio_trigger_unregister(data->trig); 709 710 return ret; 711 } 712 713 static int atlas_remove(struct i2c_client *client) 714 { 715 struct iio_dev *indio_dev = i2c_get_clientdata(client); 716 struct atlas_data *data = iio_priv(indio_dev); 717 718 iio_device_unregister(indio_dev); 719 iio_triggered_buffer_cleanup(indio_dev); 720 iio_trigger_unregister(data->trig); 721 722 pm_runtime_disable(&client->dev); 723 pm_runtime_set_suspended(&client->dev); 724 pm_runtime_put_noidle(&client->dev); 725 726 return atlas_set_powermode(data, 0); 727 } 728 729 #ifdef CONFIG_PM 730 static int atlas_runtime_suspend(struct device *dev) 731 { 732 struct atlas_data *data = 733 iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 734 735 return atlas_set_powermode(data, 0); 736 } 737 738 static int atlas_runtime_resume(struct device *dev) 739 { 740 struct atlas_data *data = 741 iio_priv(i2c_get_clientdata(to_i2c_client(dev))); 742 743 return atlas_set_powermode(data, 1); 744 } 745 #endif 746 747 static const struct dev_pm_ops atlas_pm_ops = { 748 SET_RUNTIME_PM_OPS(atlas_runtime_suspend, 749 atlas_runtime_resume, NULL) 750 }; 751 752 static struct i2c_driver atlas_driver = { 753 .driver = { 754 .name = ATLAS_DRV_NAME, 755 .of_match_table = of_match_ptr(atlas_dt_ids), 756 .pm = &atlas_pm_ops, 757 }, 758 .probe = atlas_probe, 759 .remove = atlas_remove, 760 .id_table = atlas_id, 761 }; 762 module_i2c_driver(atlas_driver); 763 764 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); 765 MODULE_DESCRIPTION("Atlas Scientific SM sensors"); 766 MODULE_LICENSE("GPL"); 767