1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * AD7606 SPI ADC driver 4 * 5 * Copyright 2011 Analog Devices Inc. 6 */ 7 8 #include <linux/delay.h> 9 #include <linux/device.h> 10 #include <linux/err.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/interrupt.h> 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/regulator/consumer.h> 16 #include <linux/sched.h> 17 #include <linux/slab.h> 18 #include <linux/sysfs.h> 19 #include <linux/util_macros.h> 20 21 #include <linux/iio/iio.h> 22 #include <linux/iio/buffer.h> 23 #include <linux/iio/sysfs.h> 24 #include <linux/iio/trigger.h> 25 #include <linux/iio/triggered_buffer.h> 26 #include <linux/iio/trigger_consumer.h> 27 28 #include "ad7606.h" 29 30 /* 31 * Scales are computed as 5000/32768 and 10000/32768 respectively, 32 * so that when applied to the raw values they provide mV values 33 */ 34 static const unsigned int ad7606_scale_avail[2] = { 35 152588, 305176 36 }; 37 38 static const unsigned int ad7606_oversampling_avail[7] = { 39 1, 2, 4, 8, 16, 32, 64, 40 }; 41 42 static const unsigned int ad7616_oversampling_avail[8] = { 43 1, 2, 4, 8, 16, 32, 64, 128, 44 }; 45 46 static int ad7606_reset(struct ad7606_state *st) 47 { 48 if (st->gpio_reset) { 49 gpiod_set_value(st->gpio_reset, 1); 50 ndelay(100); /* t_reset >= 100ns */ 51 gpiod_set_value(st->gpio_reset, 0); 52 return 0; 53 } 54 55 return -ENODEV; 56 } 57 58 static int ad7606_read_samples(struct ad7606_state *st) 59 { 60 unsigned int num = st->chip_info->num_channels; 61 u16 *data = st->data; 62 int ret; 63 64 /* 65 * The frstdata signal is set to high while and after reading the sample 66 * of the first channel and low for all other channels. This can be used 67 * to check that the incoming data is correctly aligned. During normal 68 * operation the data should never become unaligned, but some glitch or 69 * electrostatic discharge might cause an extra read or clock cycle. 70 * Monitoring the frstdata signal allows to recover from such failure 71 * situations. 72 */ 73 74 if (st->gpio_frstdata) { 75 ret = st->bops->read_block(st->dev, 1, data); 76 if (ret) 77 return ret; 78 79 if (!gpiod_get_value(st->gpio_frstdata)) { 80 ad7606_reset(st); 81 return -EIO; 82 } 83 84 data++; 85 num--; 86 } 87 88 return st->bops->read_block(st->dev, num, data); 89 } 90 91 static irqreturn_t ad7606_trigger_handler(int irq, void *p) 92 { 93 struct iio_poll_func *pf = p; 94 struct iio_dev *indio_dev = pf->indio_dev; 95 struct ad7606_state *st = iio_priv(indio_dev); 96 int ret; 97 98 mutex_lock(&st->lock); 99 100 ret = ad7606_read_samples(st); 101 if (ret == 0) 102 iio_push_to_buffers_with_timestamp(indio_dev, st->data, 103 iio_get_time_ns(indio_dev)); 104 105 iio_trigger_notify_done(indio_dev->trig); 106 /* The rising edge of the CONVST signal starts a new conversion. */ 107 gpiod_set_value(st->gpio_convst, 1); 108 109 mutex_unlock(&st->lock); 110 111 return IRQ_HANDLED; 112 } 113 114 static int ad7606_scan_direct(struct iio_dev *indio_dev, unsigned int ch) 115 { 116 struct ad7606_state *st = iio_priv(indio_dev); 117 int ret; 118 119 gpiod_set_value(st->gpio_convst, 1); 120 ret = wait_for_completion_timeout(&st->completion, 121 msecs_to_jiffies(1000)); 122 if (!ret) { 123 ret = -ETIMEDOUT; 124 goto error_ret; 125 } 126 127 ret = ad7606_read_samples(st); 128 if (ret == 0) 129 ret = st->data[ch]; 130 131 error_ret: 132 gpiod_set_value(st->gpio_convst, 0); 133 134 return ret; 135 } 136 137 static int ad7606_read_raw(struct iio_dev *indio_dev, 138 struct iio_chan_spec const *chan, 139 int *val, 140 int *val2, 141 long m) 142 { 143 int ret; 144 struct ad7606_state *st = iio_priv(indio_dev); 145 146 switch (m) { 147 case IIO_CHAN_INFO_RAW: 148 ret = iio_device_claim_direct_mode(indio_dev); 149 if (ret) 150 return ret; 151 152 ret = ad7606_scan_direct(indio_dev, chan->address); 153 iio_device_release_direct_mode(indio_dev); 154 155 if (ret < 0) 156 return ret; 157 *val = (short)ret; 158 return IIO_VAL_INT; 159 case IIO_CHAN_INFO_SCALE: 160 *val = 0; 161 *val2 = st->scale_avail[st->range[0]]; 162 return IIO_VAL_INT_PLUS_MICRO; 163 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 164 *val = st->oversampling; 165 return IIO_VAL_INT; 166 } 167 return -EINVAL; 168 } 169 170 static ssize_t ad7606_show_avail(char *buf, const unsigned int *vals, 171 unsigned int n, bool micros) 172 { 173 size_t len = 0; 174 int i; 175 176 for (i = 0; i < n; i++) { 177 len += scnprintf(buf + len, PAGE_SIZE - len, 178 micros ? "0.%06u " : "%u ", vals[i]); 179 } 180 buf[len - 1] = '\n'; 181 182 return len; 183 } 184 185 static ssize_t in_voltage_scale_available_show(struct device *dev, 186 struct device_attribute *attr, 187 char *buf) 188 { 189 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 190 struct ad7606_state *st = iio_priv(indio_dev); 191 192 return ad7606_show_avail(buf, st->scale_avail, st->num_scales, true); 193 } 194 195 static IIO_DEVICE_ATTR_RO(in_voltage_scale_available, 0); 196 197 static int ad7606_write_scale_hw(struct iio_dev *indio_dev, int ch, int val) 198 { 199 struct ad7606_state *st = iio_priv(indio_dev); 200 201 gpiod_set_value(st->gpio_range, val); 202 203 return 0; 204 } 205 206 static int ad7606_write_os_hw(struct iio_dev *indio_dev, int val) 207 { 208 struct ad7606_state *st = iio_priv(indio_dev); 209 DECLARE_BITMAP(values, 3); 210 211 values[0] = val; 212 213 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc, 214 st->gpio_os->info, values); 215 216 /* AD7616 requires a reset to update value */ 217 if (st->chip_info->os_req_reset) 218 ad7606_reset(st); 219 220 return 0; 221 } 222 223 static int ad7606_write_raw(struct iio_dev *indio_dev, 224 struct iio_chan_spec const *chan, 225 int val, 226 int val2, 227 long mask) 228 { 229 struct ad7606_state *st = iio_priv(indio_dev); 230 int i, ret, ch = 0; 231 232 switch (mask) { 233 case IIO_CHAN_INFO_SCALE: 234 mutex_lock(&st->lock); 235 i = find_closest(val2, st->scale_avail, st->num_scales); 236 ret = st->write_scale(indio_dev, chan->address, i); 237 if (ret < 0) { 238 mutex_unlock(&st->lock); 239 return ret; 240 } 241 st->range[ch] = i; 242 mutex_unlock(&st->lock); 243 244 return 0; 245 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 246 if (val2) 247 return -EINVAL; 248 i = find_closest(val, st->oversampling_avail, 249 st->num_os_ratios); 250 mutex_lock(&st->lock); 251 ret = st->write_os(indio_dev, i); 252 if (ret < 0) { 253 mutex_unlock(&st->lock); 254 return ret; 255 } 256 st->oversampling = st->oversampling_avail[i]; 257 mutex_unlock(&st->lock); 258 259 return 0; 260 default: 261 return -EINVAL; 262 } 263 } 264 265 static ssize_t ad7606_oversampling_ratio_avail(struct device *dev, 266 struct device_attribute *attr, 267 char *buf) 268 { 269 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 270 struct ad7606_state *st = iio_priv(indio_dev); 271 272 return ad7606_show_avail(buf, st->oversampling_avail, 273 st->num_os_ratios, false); 274 } 275 276 static IIO_DEVICE_ATTR(oversampling_ratio_available, 0444, 277 ad7606_oversampling_ratio_avail, NULL, 0); 278 279 static struct attribute *ad7606_attributes_os_and_range[] = { 280 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr, 281 &iio_dev_attr_oversampling_ratio_available.dev_attr.attr, 282 NULL, 283 }; 284 285 static const struct attribute_group ad7606_attribute_group_os_and_range = { 286 .attrs = ad7606_attributes_os_and_range, 287 }; 288 289 static struct attribute *ad7606_attributes_os[] = { 290 &iio_dev_attr_oversampling_ratio_available.dev_attr.attr, 291 NULL, 292 }; 293 294 static const struct attribute_group ad7606_attribute_group_os = { 295 .attrs = ad7606_attributes_os, 296 }; 297 298 static struct attribute *ad7606_attributes_range[] = { 299 &iio_dev_attr_in_voltage_scale_available.dev_attr.attr, 300 NULL, 301 }; 302 303 static const struct attribute_group ad7606_attribute_group_range = { 304 .attrs = ad7606_attributes_range, 305 }; 306 307 #define AD760X_CHANNEL(num, mask) { \ 308 .type = IIO_VOLTAGE, \ 309 .indexed = 1, \ 310 .channel = num, \ 311 .address = num, \ 312 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 313 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),\ 314 .info_mask_shared_by_all = mask, \ 315 .scan_index = num, \ 316 .scan_type = { \ 317 .sign = 's', \ 318 .realbits = 16, \ 319 .storagebits = 16, \ 320 .endianness = IIO_CPU, \ 321 }, \ 322 } 323 324 #define AD7605_CHANNEL(num) \ 325 AD760X_CHANNEL(num, 0) 326 327 #define AD7606_CHANNEL(num) \ 328 AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO)) 329 330 static const struct iio_chan_spec ad7605_channels[] = { 331 IIO_CHAN_SOFT_TIMESTAMP(4), 332 AD7605_CHANNEL(0), 333 AD7605_CHANNEL(1), 334 AD7605_CHANNEL(2), 335 AD7605_CHANNEL(3), 336 }; 337 338 static const struct iio_chan_spec ad7606_channels[] = { 339 IIO_CHAN_SOFT_TIMESTAMP(8), 340 AD7606_CHANNEL(0), 341 AD7606_CHANNEL(1), 342 AD7606_CHANNEL(2), 343 AD7606_CHANNEL(3), 344 AD7606_CHANNEL(4), 345 AD7606_CHANNEL(5), 346 AD7606_CHANNEL(6), 347 AD7606_CHANNEL(7), 348 }; 349 350 /* 351 * The current assumption that this driver makes for AD7616, is that it's 352 * working in Hardware Mode with Serial, Burst and Sequencer modes activated. 353 * To activate them, following pins must be pulled high: 354 * -SER/PAR 355 * -SEQEN 356 * And following pins must be pulled low: 357 * -WR/BURST 358 * -DB4/SER1W 359 */ 360 static const struct iio_chan_spec ad7616_channels[] = { 361 IIO_CHAN_SOFT_TIMESTAMP(16), 362 AD7606_CHANNEL(0), 363 AD7606_CHANNEL(1), 364 AD7606_CHANNEL(2), 365 AD7606_CHANNEL(3), 366 AD7606_CHANNEL(4), 367 AD7606_CHANNEL(5), 368 AD7606_CHANNEL(6), 369 AD7606_CHANNEL(7), 370 AD7606_CHANNEL(8), 371 AD7606_CHANNEL(9), 372 AD7606_CHANNEL(10), 373 AD7606_CHANNEL(11), 374 AD7606_CHANNEL(12), 375 AD7606_CHANNEL(13), 376 AD7606_CHANNEL(14), 377 AD7606_CHANNEL(15), 378 }; 379 380 static const struct ad7606_chip_info ad7606_chip_info_tbl[] = { 381 /* More devices added in future */ 382 [ID_AD7605_4] = { 383 .channels = ad7605_channels, 384 .num_channels = 5, 385 }, 386 [ID_AD7606_8] = { 387 .channels = ad7606_channels, 388 .num_channels = 9, 389 .oversampling_avail = ad7606_oversampling_avail, 390 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail), 391 }, 392 [ID_AD7606_6] = { 393 .channels = ad7606_channels, 394 .num_channels = 7, 395 .oversampling_avail = ad7606_oversampling_avail, 396 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail), 397 }, 398 [ID_AD7606_4] = { 399 .channels = ad7606_channels, 400 .num_channels = 5, 401 .oversampling_avail = ad7606_oversampling_avail, 402 .oversampling_num = ARRAY_SIZE(ad7606_oversampling_avail), 403 }, 404 [ID_AD7616] = { 405 .channels = ad7616_channels, 406 .num_channels = 17, 407 .oversampling_avail = ad7616_oversampling_avail, 408 .oversampling_num = ARRAY_SIZE(ad7616_oversampling_avail), 409 .os_req_reset = true, 410 }, 411 }; 412 413 static int ad7606_request_gpios(struct ad7606_state *st) 414 { 415 struct device *dev = st->dev; 416 417 st->gpio_convst = devm_gpiod_get(dev, "adi,conversion-start", 418 GPIOD_OUT_LOW); 419 if (IS_ERR(st->gpio_convst)) 420 return PTR_ERR(st->gpio_convst); 421 422 st->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 423 if (IS_ERR(st->gpio_reset)) 424 return PTR_ERR(st->gpio_reset); 425 426 st->gpio_range = devm_gpiod_get_optional(dev, "adi,range", 427 GPIOD_OUT_LOW); 428 if (IS_ERR(st->gpio_range)) 429 return PTR_ERR(st->gpio_range); 430 431 st->gpio_standby = devm_gpiod_get_optional(dev, "standby", 432 GPIOD_OUT_HIGH); 433 if (IS_ERR(st->gpio_standby)) 434 return PTR_ERR(st->gpio_standby); 435 436 st->gpio_frstdata = devm_gpiod_get_optional(dev, "adi,first-data", 437 GPIOD_IN); 438 if (IS_ERR(st->gpio_frstdata)) 439 return PTR_ERR(st->gpio_frstdata); 440 441 if (!st->chip_info->oversampling_num) 442 return 0; 443 444 st->gpio_os = devm_gpiod_get_array_optional(dev, 445 "adi,oversampling-ratio", 446 GPIOD_OUT_LOW); 447 return PTR_ERR_OR_ZERO(st->gpio_os); 448 } 449 450 /* 451 * The BUSY signal indicates when conversions are in progress, so when a rising 452 * edge of CONVST is applied, BUSY goes logic high and transitions low at the 453 * end of the entire conversion process. The falling edge of the BUSY signal 454 * triggers this interrupt. 455 */ 456 static irqreturn_t ad7606_interrupt(int irq, void *dev_id) 457 { 458 struct iio_dev *indio_dev = dev_id; 459 struct ad7606_state *st = iio_priv(indio_dev); 460 461 if (iio_buffer_enabled(indio_dev)) { 462 gpiod_set_value(st->gpio_convst, 0); 463 iio_trigger_poll_chained(st->trig); 464 } else { 465 complete(&st->completion); 466 } 467 468 return IRQ_HANDLED; 469 }; 470 471 static int ad7606_validate_trigger(struct iio_dev *indio_dev, 472 struct iio_trigger *trig) 473 { 474 struct ad7606_state *st = iio_priv(indio_dev); 475 476 if (st->trig != trig) 477 return -EINVAL; 478 479 return 0; 480 } 481 482 static int ad7606_buffer_postenable(struct iio_dev *indio_dev) 483 { 484 struct ad7606_state *st = iio_priv(indio_dev); 485 486 iio_triggered_buffer_postenable(indio_dev); 487 gpiod_set_value(st->gpio_convst, 1); 488 489 return 0; 490 } 491 492 static int ad7606_buffer_predisable(struct iio_dev *indio_dev) 493 { 494 struct ad7606_state *st = iio_priv(indio_dev); 495 496 gpiod_set_value(st->gpio_convst, 0); 497 498 return iio_triggered_buffer_predisable(indio_dev); 499 } 500 501 static const struct iio_buffer_setup_ops ad7606_buffer_ops = { 502 .postenable = &ad7606_buffer_postenable, 503 .predisable = &ad7606_buffer_predisable, 504 }; 505 506 static const struct iio_info ad7606_info_no_os_or_range = { 507 .read_raw = &ad7606_read_raw, 508 .validate_trigger = &ad7606_validate_trigger, 509 }; 510 511 static const struct iio_info ad7606_info_os_and_range = { 512 .read_raw = &ad7606_read_raw, 513 .write_raw = &ad7606_write_raw, 514 .attrs = &ad7606_attribute_group_os_and_range, 515 .validate_trigger = &ad7606_validate_trigger, 516 }; 517 518 static const struct iio_info ad7606_info_os = { 519 .read_raw = &ad7606_read_raw, 520 .write_raw = &ad7606_write_raw, 521 .attrs = &ad7606_attribute_group_os, 522 .validate_trigger = &ad7606_validate_trigger, 523 }; 524 525 static const struct iio_info ad7606_info_range = { 526 .read_raw = &ad7606_read_raw, 527 .write_raw = &ad7606_write_raw, 528 .attrs = &ad7606_attribute_group_range, 529 .validate_trigger = &ad7606_validate_trigger, 530 }; 531 532 static const struct iio_trigger_ops ad7606_trigger_ops = { 533 .validate_device = iio_trigger_validate_own_device, 534 }; 535 536 static void ad7606_regulator_disable(void *data) 537 { 538 struct ad7606_state *st = data; 539 540 regulator_disable(st->reg); 541 } 542 543 int ad7606_probe(struct device *dev, int irq, void __iomem *base_address, 544 const char *name, unsigned int id, 545 const struct ad7606_bus_ops *bops) 546 { 547 struct ad7606_state *st; 548 int ret; 549 struct iio_dev *indio_dev; 550 551 indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 552 if (!indio_dev) 553 return -ENOMEM; 554 555 st = iio_priv(indio_dev); 556 dev_set_drvdata(dev, indio_dev); 557 558 st->dev = dev; 559 mutex_init(&st->lock); 560 st->bops = bops; 561 st->base_address = base_address; 562 /* tied to logic low, analog input range is +/- 5V */ 563 st->range[0] = 0; 564 st->oversampling = 1; 565 st->scale_avail = ad7606_scale_avail; 566 st->num_scales = ARRAY_SIZE(ad7606_scale_avail); 567 568 st->reg = devm_regulator_get(dev, "avcc"); 569 if (IS_ERR(st->reg)) 570 return PTR_ERR(st->reg); 571 572 ret = regulator_enable(st->reg); 573 if (ret) { 574 dev_err(dev, "Failed to enable specified AVcc supply\n"); 575 return ret; 576 } 577 578 ret = devm_add_action_or_reset(dev, ad7606_regulator_disable, st); 579 if (ret) 580 return ret; 581 582 st->chip_info = &ad7606_chip_info_tbl[id]; 583 584 if (st->chip_info->oversampling_num) { 585 st->oversampling_avail = st->chip_info->oversampling_avail; 586 st->num_os_ratios = st->chip_info->oversampling_num; 587 } 588 589 ret = ad7606_request_gpios(st); 590 if (ret) 591 return ret; 592 593 indio_dev->dev.parent = dev; 594 if (st->gpio_os) { 595 if (st->gpio_range) 596 indio_dev->info = &ad7606_info_os_and_range; 597 else 598 indio_dev->info = &ad7606_info_os; 599 } else { 600 if (st->gpio_range) 601 indio_dev->info = &ad7606_info_range; 602 else 603 indio_dev->info = &ad7606_info_no_os_or_range; 604 } 605 indio_dev->modes = INDIO_DIRECT_MODE; 606 indio_dev->name = name; 607 indio_dev->channels = st->chip_info->channels; 608 indio_dev->num_channels = st->chip_info->num_channels; 609 610 init_completion(&st->completion); 611 612 ret = ad7606_reset(st); 613 if (ret) 614 dev_warn(st->dev, "failed to RESET: no RESET GPIO specified\n"); 615 616 st->write_scale = ad7606_write_scale_hw; 617 st->write_os = ad7606_write_os_hw; 618 619 st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", 620 indio_dev->name, indio_dev->id); 621 if (!st->trig) 622 return -ENOMEM; 623 624 st->trig->ops = &ad7606_trigger_ops; 625 st->trig->dev.parent = dev; 626 iio_trigger_set_drvdata(st->trig, indio_dev); 627 ret = devm_iio_trigger_register(dev, st->trig); 628 if (ret) 629 return ret; 630 631 indio_dev->trig = iio_trigger_get(st->trig); 632 633 ret = devm_request_threaded_irq(dev, irq, 634 NULL, 635 &ad7606_interrupt, 636 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 637 name, indio_dev); 638 if (ret) 639 return ret; 640 641 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, 642 &iio_pollfunc_store_time, 643 &ad7606_trigger_handler, 644 &ad7606_buffer_ops); 645 if (ret) 646 return ret; 647 648 return devm_iio_device_register(dev, indio_dev); 649 } 650 EXPORT_SYMBOL_GPL(ad7606_probe); 651 652 #ifdef CONFIG_PM_SLEEP 653 654 static int ad7606_suspend(struct device *dev) 655 { 656 struct iio_dev *indio_dev = dev_get_drvdata(dev); 657 struct ad7606_state *st = iio_priv(indio_dev); 658 659 if (st->gpio_standby) { 660 gpiod_set_value(st->gpio_range, 1); 661 gpiod_set_value(st->gpio_standby, 0); 662 } 663 664 return 0; 665 } 666 667 static int ad7606_resume(struct device *dev) 668 { 669 struct iio_dev *indio_dev = dev_get_drvdata(dev); 670 struct ad7606_state *st = iio_priv(indio_dev); 671 672 if (st->gpio_standby) { 673 gpiod_set_value(st->gpio_range, st->range[0]); 674 gpiod_set_value(st->gpio_standby, 1); 675 ad7606_reset(st); 676 } 677 678 return 0; 679 } 680 681 SIMPLE_DEV_PM_OPS(ad7606_pm_ops, ad7606_suspend, ad7606_resume); 682 EXPORT_SYMBOL_GPL(ad7606_pm_ops); 683 684 #endif 685 686 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 687 MODULE_DESCRIPTION("Analog Devices AD7606 ADC"); 688 MODULE_LICENSE("GPL v2"); 689