1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Texas Instruments ADS1119 ADC driver. 4 * 5 * Copyright 2024 Toradex 6 */ 7 8 #include <linux/bits.h> 9 #include <linux/bitfield.h> 10 #include <linux/completion.h> 11 #include <linux/delay.h> 12 #include <linux/dev_printk.h> 13 #include <linux/err.h> 14 #include <linux/gpio/consumer.h> 15 #include <linux/interrupt.h> 16 #include <linux/iopoll.h> 17 #include <linux/i2c.h> 18 #include <linux/kernel.h> 19 #include <linux/math.h> 20 #include <linux/module.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/regulator/consumer.h> 23 #include <linux/units.h> 24 25 #include <linux/iio/iio.h> 26 #include <linux/iio/buffer.h> 27 #include <linux/iio/trigger.h> 28 #include <linux/iio/triggered_buffer.h> 29 #include <linux/iio/trigger_consumer.h> 30 31 #define ADS1119_CMD_RESET 0x06 32 #define ADS1119_CMD_POWERDOWN 0x02 33 #define ADS1119_CMD_START_SYNC 0x08 34 #define ADS1119_CMD_RDATA 0x10 35 #define ADS1119_CMD_RREG_CONFIG 0x20 36 #define ADS1119_CMD_RREG_STATUS 0x24 37 #define ADS1119_CMD_WREG 0x40 38 39 #define ADS1119_CMD_RREG(reg) (0x20 | (reg) << 2) 40 41 /* Config register */ 42 #define ADS1119_REG_CONFIG 0x00 43 #define ADS1119_CONFIG_VREF_FIELD BIT(0) 44 #define ADS1119_CONFIG_CM_FIELD BIT(1) 45 #define ADS1119_CONFIG_DR_FIELD GENMASK(3, 2) 46 #define ADS1119_CONFIG_GAIN_FIELD BIT(4) 47 #define ADS1119_CONFIG_MUX_FIELD GENMASK(7, 5) 48 49 #define ADS1119_VREF_INTERNAL 0 50 #define ADS1119_VREF_EXTERNAL 1 51 #define ADS1119_VREF_INTERNAL_VAL 2048000 52 53 #define ADS1119_CM_SINGLE 0 54 #define ADS1119_CM_CONTINUOUS 1 55 56 #define ADS1119_DR_20_SPS 0 57 #define ADS1119_DR_90_SPS 1 58 #define ADS1119_DR_330_SPS 2 59 #define ADS1119_DR_1000_SPS 3 60 61 #define ADS1119_GAIN_1 0 62 #define ADS1119_GAIN_4 1 63 64 #define ADS1119_MUX_AIN0_AIN1 0 65 #define ADS1119_MUX_AIN2_AIN3 1 66 #define ADS1119_MUX_AIN1_AIN2 2 67 #define ADS1119_MUX_AIN0 3 68 #define ADS1119_MUX_AIN1 4 69 #define ADS1119_MUX_AIN2 5 70 #define ADS1119_MUX_AIN3 6 71 #define ADS1119_MUX_SHORTED 7 72 73 /* Status register */ 74 #define ADS1119_REG_STATUS 0x01 75 #define ADS1119_STATUS_DRDY_FIELD BIT(7) 76 77 #define ADS1119_DEFAULT_GAIN 1 78 #define ADS1119_DEFAULT_DATARATE 20 79 80 #define ADS1119_SUSPEND_DELAY 2000 81 82 /* Timeout based on the minimum sample rate of 20 SPS (50000us) */ 83 #define ADS1119_MAX_DRDY_TIMEOUT 85000 84 85 #define ADS1119_MAX_CHANNELS 7 86 #define ADS1119_MAX_SINGLE_CHANNELS 4 87 88 struct ads1119_channel_config { 89 int gain; 90 int datarate; 91 int mux; 92 }; 93 94 struct ads1119_state { 95 struct completion completion; 96 struct i2c_client *client; 97 struct gpio_desc *reset_gpio; 98 struct iio_trigger *trig; 99 struct ads1119_channel_config *channels_cfg; 100 unsigned int num_channels_cfg; 101 unsigned int cached_config; 102 int vref_uV; 103 }; 104 105 static const char * const ads1119_power_supplies[] = { 106 "avdd", "dvdd" 107 }; 108 109 static const int ads1119_available_datarates[] = { 110 20, 90, 330, 1000, 111 }; 112 113 static const int ads1119_available_gains[] = { 114 1, 1, 115 1, 4, 116 }; 117 118 static int ads1119_upd_cfg_reg(struct ads1119_state *st, unsigned int fields, 119 unsigned int val) 120 { 121 unsigned int config = st->cached_config; 122 int ret; 123 124 config &= ~fields; 125 config |= val; 126 127 ret = i2c_smbus_write_byte_data(st->client, ADS1119_CMD_WREG, config); 128 if (ret) 129 return ret; 130 131 st->cached_config = config; 132 133 return 0; 134 } 135 136 static bool ads1119_data_ready(struct ads1119_state *st) 137 { 138 int status; 139 140 status = i2c_smbus_read_byte_data(st->client, ADS1119_CMD_RREG_STATUS); 141 if (status < 0) 142 return false; 143 144 return FIELD_GET(ADS1119_STATUS_DRDY_FIELD, status); 145 } 146 147 static int ads1119_reset(struct ads1119_state *st) 148 { 149 st->cached_config = 0; 150 151 if (!st->reset_gpio) 152 return i2c_smbus_write_byte(st->client, ADS1119_CMD_RESET); 153 154 gpiod_set_value_cansleep(st->reset_gpio, 1); 155 udelay(1); 156 gpiod_set_value_cansleep(st->reset_gpio, 0); 157 udelay(1); 158 159 return 0; 160 } 161 162 static int ads1119_set_conv_mode(struct ads1119_state *st, bool continuous) 163 { 164 unsigned int mode; 165 166 if (continuous) 167 mode = ADS1119_CM_CONTINUOUS; 168 else 169 mode = ADS1119_CM_SINGLE; 170 171 return ads1119_upd_cfg_reg(st, ADS1119_CONFIG_CM_FIELD, 172 FIELD_PREP(ADS1119_CONFIG_CM_FIELD, mode)); 173 } 174 175 static int ads1119_get_hw_gain(int gain) 176 { 177 if (gain == 4) 178 return ADS1119_GAIN_4; 179 else 180 return ADS1119_GAIN_1; 181 } 182 183 static int ads1119_get_hw_datarate(int datarate) 184 { 185 switch (datarate) { 186 case 90: 187 return ADS1119_DR_90_SPS; 188 case 330: 189 return ADS1119_DR_330_SPS; 190 case 1000: 191 return ADS1119_DR_1000_SPS; 192 case 20: 193 default: 194 return ADS1119_DR_20_SPS; 195 } 196 } 197 198 static int ads1119_configure_channel(struct ads1119_state *st, int mux, 199 int gain, int datarate) 200 { 201 int ret; 202 203 ret = ads1119_upd_cfg_reg(st, ADS1119_CONFIG_MUX_FIELD, 204 FIELD_PREP(ADS1119_CONFIG_MUX_FIELD, mux)); 205 if (ret) 206 return ret; 207 208 ret = ads1119_upd_cfg_reg(st, ADS1119_CONFIG_GAIN_FIELD, 209 FIELD_PREP(ADS1119_CONFIG_GAIN_FIELD, 210 ads1119_get_hw_gain(gain))); 211 if (ret) 212 return ret; 213 214 return ads1119_upd_cfg_reg(st, ADS1119_CONFIG_DR_FIELD, 215 FIELD_PREP(ADS1119_CONFIG_DR_FIELD, 216 ads1119_get_hw_datarate(datarate))); 217 } 218 219 static int ads1119_poll_data_ready(struct ads1119_state *st, 220 struct iio_chan_spec const *chan) 221 { 222 unsigned int datarate = st->channels_cfg[chan->address].datarate; 223 unsigned long wait_time; 224 bool data_ready; 225 226 /* Poll 5 times more than the data rate */ 227 wait_time = DIV_ROUND_CLOSEST(MICRO, 5 * datarate); 228 229 return read_poll_timeout(ads1119_data_ready, data_ready, 230 data_ready, wait_time, 231 ADS1119_MAX_DRDY_TIMEOUT, false, st); 232 } 233 234 static int ads1119_read_data(struct ads1119_state *st, 235 struct iio_chan_spec const *chan, 236 unsigned int *val) 237 { 238 unsigned int timeout; 239 int ret = 0; 240 241 timeout = msecs_to_jiffies(ADS1119_MAX_DRDY_TIMEOUT); 242 243 if (!st->client->irq) { 244 ret = ads1119_poll_data_ready(st, chan); 245 if (ret) 246 return ret; 247 } else if (!wait_for_completion_timeout(&st->completion, timeout)) { 248 return -ETIMEDOUT; 249 } 250 251 ret = i2c_smbus_read_word_swapped(st->client, ADS1119_CMD_RDATA); 252 if (ret < 0) 253 return ret; 254 255 *val = ret; 256 257 return 0; 258 } 259 260 static int ads1119_single_conversion(struct ads1119_state *st, 261 struct iio_chan_spec const *chan, 262 int *val, 263 bool calib_offset) 264 { 265 struct device *dev = &st->client->dev; 266 int mux = st->channels_cfg[chan->address].mux; 267 int gain = st->channels_cfg[chan->address].gain; 268 int datarate = st->channels_cfg[chan->address].datarate; 269 unsigned int sample; 270 int ret; 271 272 if (calib_offset) 273 mux = ADS1119_MUX_SHORTED; 274 275 ret = pm_runtime_resume_and_get(dev); 276 if (ret) 277 goto pdown; 278 279 ret = ads1119_configure_channel(st, mux, gain, datarate); 280 if (ret) 281 goto pdown; 282 283 ret = i2c_smbus_write_byte(st->client, ADS1119_CMD_START_SYNC); 284 if (ret) 285 goto pdown; 286 287 ret = ads1119_read_data(st, chan, &sample); 288 if (ret) 289 goto pdown; 290 291 *val = sign_extend32(sample, chan->scan_type.realbits - 1); 292 ret = IIO_VAL_INT; 293 pdown: 294 pm_runtime_mark_last_busy(dev); 295 pm_runtime_put_autosuspend(dev); 296 return ret; 297 } 298 299 static int ads1119_validate_datarate(struct ads1119_state *st, int datarate) 300 { 301 switch (datarate) { 302 case 20: 303 case 90: 304 case 330: 305 case 1000: 306 return datarate; 307 default: 308 return -EINVAL; 309 } 310 } 311 312 static int ads1119_read_avail(struct iio_dev *indio_dev, 313 struct iio_chan_spec const *chan, 314 const int **vals, int *type, int *length, 315 long mask) 316 { 317 switch (mask) { 318 case IIO_CHAN_INFO_SCALE: 319 *type = IIO_VAL_FRACTIONAL; 320 *vals = ads1119_available_gains; 321 *length = ARRAY_SIZE(ads1119_available_gains); 322 return IIO_AVAIL_LIST; 323 case IIO_CHAN_INFO_SAMP_FREQ: 324 *type = IIO_VAL_INT; 325 *vals = ads1119_available_datarates; 326 *length = ARRAY_SIZE(ads1119_available_datarates); 327 return IIO_AVAIL_LIST; 328 default: 329 return -EINVAL; 330 } 331 } 332 333 static int ads1119_read_raw(struct iio_dev *indio_dev, 334 struct iio_chan_spec const *chan, int *val, 335 int *val2, long mask) 336 { 337 struct ads1119_state *st = iio_priv(indio_dev); 338 unsigned int index = chan->address; 339 340 if (index >= st->num_channels_cfg) 341 return -EINVAL; 342 343 switch (mask) { 344 case IIO_CHAN_INFO_RAW: 345 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) 346 return ads1119_single_conversion(st, chan, val, false); 347 unreachable(); 348 case IIO_CHAN_INFO_OFFSET: 349 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) 350 return ads1119_single_conversion(st, chan, val, true); 351 unreachable(); 352 case IIO_CHAN_INFO_SCALE: 353 *val = st->vref_uV / 1000; 354 *val /= st->channels_cfg[index].gain; 355 *val2 = chan->scan_type.realbits - 1; 356 return IIO_VAL_FRACTIONAL_LOG2; 357 case IIO_CHAN_INFO_SAMP_FREQ: 358 *val = st->channels_cfg[index].datarate; 359 return IIO_VAL_INT; 360 default: 361 return -EINVAL; 362 } 363 } 364 365 static int ads1119_write_raw(struct iio_dev *indio_dev, 366 struct iio_chan_spec const *chan, int val, 367 int val2, long mask) 368 { 369 struct ads1119_state *st = iio_priv(indio_dev); 370 unsigned int index = chan->address; 371 int ret; 372 373 if (index >= st->num_channels_cfg) 374 return -EINVAL; 375 376 switch (mask) { 377 case IIO_CHAN_INFO_SCALE: 378 ret = MICRO / ((val * MICRO) + val2); 379 if (ret != 1 && ret != 4) 380 return -EINVAL; 381 382 st->channels_cfg[index].gain = ret; 383 return 0; 384 case IIO_CHAN_INFO_SAMP_FREQ: 385 ret = ads1119_validate_datarate(st, val); 386 if (ret < 0) 387 return ret; 388 389 st->channels_cfg[index].datarate = ret; 390 return 0; 391 default: 392 return -EINVAL; 393 } 394 } 395 396 static int ads1119_debugfs_reg_access(struct iio_dev *indio_dev, 397 unsigned int reg, unsigned int writeval, 398 unsigned int *readval) 399 { 400 struct ads1119_state *st = iio_priv(indio_dev); 401 int ret; 402 403 if (reg > ADS1119_REG_STATUS) 404 return -EINVAL; 405 406 if (readval) { 407 ret = i2c_smbus_read_byte_data(st->client, 408 ADS1119_CMD_RREG(reg)); 409 if (ret < 0) 410 return ret; 411 412 *readval = ret; 413 return 0; 414 } 415 416 if (reg > ADS1119_REG_CONFIG) 417 return -EINVAL; 418 419 return i2c_smbus_write_byte_data(st->client, ADS1119_CMD_WREG, 420 writeval); 421 } 422 423 static const struct iio_info ads1119_info = { 424 .read_avail = ads1119_read_avail, 425 .read_raw = ads1119_read_raw, 426 .write_raw = ads1119_write_raw, 427 .debugfs_reg_access = ads1119_debugfs_reg_access, 428 }; 429 430 static int ads1119_triggered_buffer_preenable(struct iio_dev *indio_dev) 431 { 432 struct ads1119_state *st = iio_priv(indio_dev); 433 struct device *dev = &st->client->dev; 434 unsigned int index; 435 int ret; 436 437 index = find_first_bit(indio_dev->active_scan_mask, 438 iio_get_masklength(indio_dev)); 439 440 ret = ads1119_set_conv_mode(st, true); 441 if (ret) 442 return ret; 443 444 ret = ads1119_configure_channel(st, 445 st->channels_cfg[index].mux, 446 st->channels_cfg[index].gain, 447 st->channels_cfg[index].datarate); 448 if (ret) 449 return ret; 450 451 ret = pm_runtime_resume_and_get(dev); 452 if (ret) 453 return ret; 454 455 return i2c_smbus_write_byte(st->client, ADS1119_CMD_START_SYNC); 456 } 457 458 static int ads1119_triggered_buffer_postdisable(struct iio_dev *indio_dev) 459 { 460 struct ads1119_state *st = iio_priv(indio_dev); 461 struct device *dev = &st->client->dev; 462 int ret; 463 464 ret = ads1119_set_conv_mode(st, false); 465 if (ret) 466 return ret; 467 468 pm_runtime_mark_last_busy(dev); 469 pm_runtime_put_autosuspend(dev); 470 471 return 0; 472 } 473 474 static const struct iio_buffer_setup_ops ads1119_buffer_setup_ops = { 475 .preenable = ads1119_triggered_buffer_preenable, 476 .postdisable = ads1119_triggered_buffer_postdisable, 477 .validate_scan_mask = &iio_validate_scan_mask_onehot, 478 }; 479 480 static const struct iio_trigger_ops ads1119_trigger_ops = { 481 .validate_device = &iio_trigger_validate_own_device, 482 }; 483 484 static irqreturn_t ads1119_irq_handler(int irq, void *dev_id) 485 { 486 struct iio_dev *indio_dev = dev_id; 487 struct ads1119_state *st = iio_priv(indio_dev); 488 489 if (iio_buffer_enabled(indio_dev) && iio_trigger_using_own(indio_dev)) 490 iio_trigger_poll(indio_dev->trig); 491 else 492 complete(&st->completion); 493 494 return IRQ_HANDLED; 495 } 496 497 static irqreturn_t ads1119_trigger_handler(int irq, void *private) 498 { 499 struct iio_poll_func *pf = private; 500 struct iio_dev *indio_dev = pf->indio_dev; 501 struct ads1119_state *st = iio_priv(indio_dev); 502 struct { 503 s16 sample; 504 s64 timestamp __aligned(8); 505 } scan; 506 unsigned int index; 507 int ret; 508 509 memset(&scan, 0, sizeof(scan)); 510 511 if (!iio_trigger_using_own(indio_dev)) { 512 index = find_first_bit(indio_dev->active_scan_mask, 513 iio_get_masklength(indio_dev)); 514 515 ret = ads1119_poll_data_ready(st, &indio_dev->channels[index]); 516 if (ret) { 517 dev_err(&st->client->dev, 518 "Failed to poll data on trigger (%d)\n", ret); 519 goto done; 520 } 521 } 522 523 ret = i2c_smbus_read_word_swapped(st->client, ADS1119_CMD_RDATA); 524 if (ret < 0) { 525 dev_err(&st->client->dev, 526 "Failed to read data on trigger (%d)\n", ret); 527 goto done; 528 } 529 530 scan.sample = ret; 531 532 iio_push_to_buffers_with_timestamp(indio_dev, &scan, 533 iio_get_time_ns(indio_dev)); 534 done: 535 iio_trigger_notify_done(indio_dev->trig); 536 return IRQ_HANDLED; 537 } 538 539 static int ads1119_init(struct ads1119_state *st, bool vref_external) 540 { 541 int ret; 542 543 ret = ads1119_reset(st); 544 if (ret) 545 return ret; 546 547 if (vref_external) 548 return ads1119_upd_cfg_reg(st, 549 ADS1119_CONFIG_VREF_FIELD, 550 FIELD_PREP(ADS1119_CONFIG_VREF_FIELD, 551 ADS1119_VREF_EXTERNAL)); 552 return 0; 553 } 554 555 static int ads1119_map_analog_inputs_mux(int ain_pos, int ain_neg, 556 bool differential) 557 { 558 if (ain_pos >= ADS1119_MAX_SINGLE_CHANNELS) 559 return -EINVAL; 560 561 if (!differential) 562 return ADS1119_MUX_AIN0 + ain_pos; 563 564 if (ain_pos == 0 && ain_neg == 1) 565 return ADS1119_MUX_AIN0_AIN1; 566 else if (ain_pos == 1 && ain_neg == 2) 567 return ADS1119_MUX_AIN1_AIN2; 568 else if (ain_pos == 2 && ain_neg == 3) 569 return ADS1119_MUX_AIN2_AIN3; 570 571 return -EINVAL; 572 } 573 574 static int ads1119_alloc_and_config_channels(struct iio_dev *indio_dev) 575 { 576 const struct iio_chan_spec ads1119_channel = 577 (const struct iio_chan_spec) { 578 .type = IIO_VOLTAGE, 579 .indexed = 1, 580 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 581 BIT(IIO_CHAN_INFO_SCALE) | 582 BIT(IIO_CHAN_INFO_OFFSET) | 583 BIT(IIO_CHAN_INFO_SAMP_FREQ), 584 .info_mask_shared_by_all_available = 585 BIT(IIO_CHAN_INFO_SCALE) | 586 BIT(IIO_CHAN_INFO_SAMP_FREQ), 587 .scan_type = { 588 .sign = 's', 589 .realbits = 16, 590 .storagebits = 16, 591 .endianness = IIO_CPU, 592 }, 593 }; 594 const struct iio_chan_spec ads1119_ts = IIO_CHAN_SOFT_TIMESTAMP(0); 595 struct ads1119_state *st = iio_priv(indio_dev); 596 struct iio_chan_spec *iio_channels, *chan; 597 struct device *dev = &st->client->dev; 598 unsigned int num_channels, i; 599 bool differential; 600 u32 ain[2]; 601 int ret; 602 603 st->num_channels_cfg = device_get_child_node_count(dev); 604 if (st->num_channels_cfg > ADS1119_MAX_CHANNELS) 605 return dev_err_probe(dev, -EINVAL, 606 "Too many channels %d, max is %d\n", 607 st->num_channels_cfg, 608 ADS1119_MAX_CHANNELS); 609 610 st->channels_cfg = devm_kcalloc(dev, st->num_channels_cfg, 611 sizeof(*st->channels_cfg), GFP_KERNEL); 612 if (!st->channels_cfg) 613 return -ENOMEM; 614 615 /* Allocate one more iio channel for the timestamp */ 616 num_channels = st->num_channels_cfg + 1; 617 iio_channels = devm_kcalloc(dev, num_channels, sizeof(*iio_channels), 618 GFP_KERNEL); 619 if (!iio_channels) 620 return -ENOMEM; 621 622 i = 0; 623 624 device_for_each_child_node_scoped(dev, child) { 625 chan = &iio_channels[i]; 626 627 differential = fwnode_property_present(child, "diff-channels"); 628 if (differential) 629 ret = fwnode_property_read_u32_array(child, 630 "diff-channels", 631 ain, 2); 632 else 633 ret = fwnode_property_read_u32(child, "single-channel", 634 &ain[0]); 635 636 if (ret) 637 return dev_err_probe(dev, ret, 638 "Failed to get channel property\n"); 639 640 ret = ads1119_map_analog_inputs_mux(ain[0], ain[1], 641 differential); 642 if (ret < 0) 643 return dev_err_probe(dev, ret, 644 "Invalid channel value\n"); 645 646 st->channels_cfg[i].mux = ret; 647 st->channels_cfg[i].gain = ADS1119_DEFAULT_GAIN; 648 st->channels_cfg[i].datarate = ADS1119_DEFAULT_DATARATE; 649 650 *chan = ads1119_channel; 651 chan->channel = ain[0]; 652 chan->address = i; 653 chan->scan_index = i; 654 655 if (differential) { 656 chan->channel2 = ain[1]; 657 chan->differential = 1; 658 } 659 660 dev_dbg(dev, "channel: index %d, mux %d\n", i, 661 st->channels_cfg[i].mux); 662 663 i++; 664 } 665 666 iio_channels[i] = ads1119_ts; 667 iio_channels[i].address = i; 668 iio_channels[i].scan_index = i; 669 670 indio_dev->channels = iio_channels; 671 indio_dev->num_channels = num_channels; 672 673 return 0; 674 } 675 676 static void ads1119_powerdown(void *data) 677 { 678 struct ads1119_state *st = data; 679 680 i2c_smbus_write_byte(st->client, ADS1119_CMD_POWERDOWN); 681 } 682 683 static int ads1119_probe(struct i2c_client *client) 684 { 685 struct iio_dev *indio_dev; 686 struct ads1119_state *st; 687 struct device *dev = &client->dev; 688 bool vref_external = true; 689 int ret; 690 691 indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 692 if (!indio_dev) 693 return dev_err_probe(dev, -ENOMEM, 694 "Failed to allocate IIO device\n"); 695 696 st = iio_priv(indio_dev); 697 st->client = client; 698 699 indio_dev->name = "ads1119"; 700 indio_dev->info = &ads1119_info; 701 indio_dev->modes = INDIO_DIRECT_MODE; 702 703 i2c_set_clientdata(client, indio_dev); 704 705 ret = devm_regulator_bulk_get_enable(dev, 706 ARRAY_SIZE(ads1119_power_supplies), 707 ads1119_power_supplies); 708 if (ret) 709 return dev_err_probe(dev, ret, 710 "Failed to get and enable supplies\n"); 711 712 st->vref_uV = devm_regulator_get_enable_read_voltage(dev, "vref"); 713 if (st->vref_uV == -ENODEV) { 714 vref_external = false; 715 st->vref_uV = ADS1119_VREF_INTERNAL_VAL; 716 } else if (st->vref_uV < 0) { 717 return dev_err_probe(dev, st->vref_uV, "Failed to get vref\n"); 718 } 719 720 st->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 721 if (IS_ERR(st->reset_gpio)) 722 return dev_err_probe(dev, PTR_ERR(st->reset_gpio), 723 "Failed to get reset gpio\n"); 724 725 ret = ads1119_alloc_and_config_channels(indio_dev); 726 if (ret) 727 return ret; 728 729 init_completion(&st->completion); 730 731 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, 732 ads1119_trigger_handler, 733 &ads1119_buffer_setup_ops); 734 if (ret) 735 return dev_err_probe(dev, ret, "Failed to setup IIO buffer\n"); 736 737 if (client->irq > 0) { 738 ret = devm_request_threaded_irq(dev, client->irq, 739 ads1119_irq_handler, 740 NULL, IRQF_ONESHOT, 741 "ads1119", indio_dev); 742 if (ret) 743 return dev_err_probe(dev, ret, 744 "Failed to allocate irq\n"); 745 746 st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", 747 indio_dev->name, 748 iio_device_id(indio_dev)); 749 if (!st->trig) 750 return dev_err_probe(dev, -ENOMEM, 751 "Failed to allocate IIO trigger\n"); 752 753 st->trig->ops = &ads1119_trigger_ops; 754 iio_trigger_set_drvdata(st->trig, indio_dev); 755 756 ret = devm_iio_trigger_register(dev, st->trig); 757 if (ret) 758 return dev_err_probe(dev, ret, 759 "Failed to register IIO trigger\n"); 760 } 761 762 ret = ads1119_init(st, vref_external); 763 if (ret) 764 return dev_err_probe(dev, ret, 765 "Failed to initialize device\n"); 766 767 pm_runtime_set_autosuspend_delay(dev, ADS1119_SUSPEND_DELAY); 768 pm_runtime_use_autosuspend(dev); 769 pm_runtime_mark_last_busy(dev); 770 pm_runtime_set_active(dev); 771 772 ret = devm_pm_runtime_enable(dev); 773 if (ret) 774 return dev_err_probe(dev, ret, "Failed to enable pm runtime\n"); 775 776 ret = devm_add_action_or_reset(dev, ads1119_powerdown, st); 777 if (ret) 778 return dev_err_probe(dev, ret, 779 "Failed to add powerdown action\n"); 780 781 return devm_iio_device_register(dev, indio_dev); 782 } 783 784 static int ads1119_runtime_suspend(struct device *dev) 785 { 786 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 787 struct ads1119_state *st = iio_priv(indio_dev); 788 789 return i2c_smbus_write_byte(st->client, ADS1119_CMD_POWERDOWN); 790 } 791 792 /* 793 * The ADS1119 does not require a resume function because it automatically 794 * powers on after a reset. 795 * After a power down command, the ADS1119 can still communicate but turns off 796 * its analog parts. To resume from power down, the device will power up again 797 * upon receiving a start/sync command. 798 */ 799 static DEFINE_RUNTIME_DEV_PM_OPS(ads1119_pm_ops, ads1119_runtime_suspend, 800 NULL, NULL); 801 802 static const struct of_device_id __maybe_unused ads1119_of_match[] = { 803 { .compatible = "ti,ads1119" }, 804 { } 805 }; 806 MODULE_DEVICE_TABLE(of, ads1119_of_match); 807 808 static const struct i2c_device_id ads1119_id[] = { 809 { "ads1119" }, 810 { } 811 }; 812 MODULE_DEVICE_TABLE(i2c, ads1119_id); 813 814 static struct i2c_driver ads1119_driver = { 815 .driver = { 816 .name = "ads1119", 817 .of_match_table = ads1119_of_match, 818 .pm = pm_ptr(&ads1119_pm_ops), 819 }, 820 .probe = ads1119_probe, 821 .id_table = ads1119_id, 822 }; 823 module_i2c_driver(ads1119_driver); 824 825 MODULE_AUTHOR("João Paulo Gonçalves <joao.goncalves@toradex.com>"); 826 MODULE_DESCRIPTION("Texas Instruments ADS1119 ADC Driver"); 827 MODULE_LICENSE("GPL"); 828