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