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