1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * iio/adc/ad799x.c 4 * Copyright (C) 2010-2011 Michael Hennerich, Analog Devices Inc. 5 * 6 * based on iio/adc/max1363 7 * Copyright (C) 2008-2010 Jonathan Cameron 8 * 9 * based on linux/drivers/i2c/chips/max123x 10 * Copyright (C) 2002-2004 Stefan Eletzhofer 11 * 12 * based on linux/drivers/acron/char/pcf8583.c 13 * Copyright (C) 2000 Russell King 14 * 15 * ad799x.c 16 * 17 * Support for ad7991, ad7995, ad7999, ad7992, ad7993, ad7994, ad7997, 18 * ad7998 and similar chips. 19 */ 20 21 #include <linux/interrupt.h> 22 #include <linux/device.h> 23 #include <linux/kernel.h> 24 #include <linux/sysfs.h> 25 #include <linux/i2c.h> 26 #include <linux/regulator/consumer.h> 27 #include <linux/slab.h> 28 #include <linux/types.h> 29 #include <linux/err.h> 30 #include <linux/module.h> 31 #include <linux/mutex.h> 32 #include <linux/bitops.h> 33 34 #include <linux/iio/iio.h> 35 #include <linux/iio/sysfs.h> 36 #include <linux/iio/events.h> 37 #include <linux/iio/buffer.h> 38 #include <linux/iio/trigger_consumer.h> 39 #include <linux/iio/triggered_buffer.h> 40 41 #define AD799X_CHANNEL_SHIFT 4 42 43 /* 44 * AD7991, AD7995 and AD7999 defines 45 */ 46 47 #define AD7991_REF_SEL 0x08 48 #define AD7991_FLTR 0x04 49 #define AD7991_BIT_TRIAL_DELAY 0x02 50 #define AD7991_SAMPLE_DELAY 0x01 51 52 /* 53 * AD7992, AD7993, AD7994, AD7997 and AD7998 defines 54 */ 55 56 #define AD7998_FLTR BIT(3) 57 #define AD7998_ALERT_EN BIT(2) 58 #define AD7998_BUSY_ALERT BIT(1) 59 #define AD7998_BUSY_ALERT_POL BIT(0) 60 61 #define AD7998_CONV_RES_REG 0x0 62 #define AD7998_ALERT_STAT_REG 0x1 63 #define AD7998_CONF_REG 0x2 64 #define AD7998_CYCLE_TMR_REG 0x3 65 66 #define AD7998_DATALOW_REG(x) ((x) * 3 + 0x4) 67 #define AD7998_DATAHIGH_REG(x) ((x) * 3 + 0x5) 68 #define AD7998_HYST_REG(x) ((x) * 3 + 0x6) 69 70 #define AD7998_CYC_MASK GENMASK(2, 0) 71 #define AD7998_CYC_DIS 0x0 72 #define AD7998_CYC_TCONF_32 0x1 73 #define AD7998_CYC_TCONF_64 0x2 74 #define AD7998_CYC_TCONF_128 0x3 75 #define AD7998_CYC_TCONF_256 0x4 76 #define AD7998_CYC_TCONF_512 0x5 77 #define AD7998_CYC_TCONF_1024 0x6 78 #define AD7998_CYC_TCONF_2048 0x7 79 80 #define AD7998_ALERT_STAT_CLEAR 0xFF 81 82 /* 83 * AD7997 and AD7997 defines 84 */ 85 86 #define AD7997_8_READ_SINGLE BIT(7) 87 #define AD7997_8_READ_SEQUENCE (BIT(6) | BIT(5) | BIT(4)) 88 89 enum { 90 ad7991, 91 ad7995, 92 ad7999, 93 ad7992, 94 ad7993, 95 ad7994, 96 ad7997, 97 ad7998 98 }; 99 100 /** 101 * struct ad799x_chip_config - chip specific information 102 * @channel: channel specification 103 * @default_config: device default configuration 104 * @info: pointer to iio_info struct 105 */ 106 struct ad799x_chip_config { 107 const struct iio_chan_spec channel[9]; 108 u16 default_config; 109 const struct iio_info *info; 110 }; 111 112 /** 113 * struct ad799x_chip_info - chip specific information 114 * @num_channels: number of channels 115 * @noirq_config: device configuration w/o IRQ 116 * @irq_config: device configuration w/IRQ 117 * @has_vref: device supports external reference voltage 118 */ 119 struct ad799x_chip_info { 120 int num_channels; 121 const struct ad799x_chip_config noirq_config; 122 const struct ad799x_chip_config irq_config; 123 bool has_vref; 124 }; 125 126 struct ad799x_state { 127 struct i2c_client *client; 128 const struct ad799x_chip_config *chip_config; 129 struct regulator *reg; 130 struct regulator *vref; 131 /* lock to protect against multiple access to the device */ 132 struct mutex lock; 133 unsigned int id; 134 u16 config; 135 136 u8 *rx_buf; 137 unsigned int transfer_size; 138 }; 139 140 static int ad799x_write_config(struct ad799x_state *st, u16 val) 141 { 142 switch (st->id) { 143 case ad7997: 144 case ad7998: 145 return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG, 146 val); 147 case ad7992: 148 case ad7993: 149 case ad7994: 150 return i2c_smbus_write_byte_data(st->client, AD7998_CONF_REG, 151 val); 152 default: 153 /* Will be written when doing a conversion */ 154 st->config = val; 155 return 0; 156 } 157 } 158 159 static int ad799x_read_config(struct ad799x_state *st) 160 { 161 switch (st->id) { 162 case ad7997: 163 case ad7998: 164 return i2c_smbus_read_word_swapped(st->client, AD7998_CONF_REG); 165 case ad7992: 166 case ad7993: 167 case ad7994: 168 return i2c_smbus_read_byte_data(st->client, AD7998_CONF_REG); 169 default: 170 /* No readback support */ 171 return st->config; 172 } 173 } 174 175 static int ad799x_update_config(struct ad799x_state *st, u16 config) 176 { 177 int ret; 178 179 ret = ad799x_write_config(st, config); 180 if (ret < 0) 181 return ret; 182 ret = ad799x_read_config(st); 183 if (ret < 0) 184 return ret; 185 st->config = ret; 186 187 return 0; 188 } 189 190 static irqreturn_t ad799x_trigger_handler(int irq, void *p) 191 { 192 struct iio_poll_func *pf = p; 193 struct iio_dev *indio_dev = pf->indio_dev; 194 struct ad799x_state *st = iio_priv(indio_dev); 195 int b_sent; 196 u8 cmd; 197 198 switch (st->id) { 199 case ad7991: 200 case ad7995: 201 case ad7999: 202 cmd = st->config | 203 (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT); 204 break; 205 case ad7992: 206 case ad7993: 207 case ad7994: 208 cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) | 209 AD7998_CONV_RES_REG; 210 break; 211 case ad7997: 212 case ad7998: 213 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG; 214 break; 215 default: 216 cmd = 0; 217 } 218 219 b_sent = i2c_smbus_read_i2c_block_data(st->client, 220 cmd, st->transfer_size, st->rx_buf); 221 if (b_sent < 0) 222 goto out; 223 224 iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf, 225 iio_get_time_ns(indio_dev)); 226 out: 227 iio_trigger_notify_done(indio_dev->trig); 228 229 return IRQ_HANDLED; 230 } 231 232 static int ad799x_update_scan_mode(struct iio_dev *indio_dev, 233 const unsigned long *scan_mask) 234 { 235 struct ad799x_state *st = iio_priv(indio_dev); 236 237 kfree(st->rx_buf); 238 st->rx_buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); 239 if (!st->rx_buf) 240 return -ENOMEM; 241 242 st->transfer_size = bitmap_weight(scan_mask, 243 iio_get_masklength(indio_dev)) * 2; 244 245 switch (st->id) { 246 case ad7992: 247 case ad7993: 248 case ad7994: 249 case ad7997: 250 case ad7998: 251 st->config &= ~(GENMASK(7, 0) << AD799X_CHANNEL_SHIFT); 252 st->config |= (*scan_mask << AD799X_CHANNEL_SHIFT); 253 return ad799x_write_config(st, st->config); 254 default: 255 return 0; 256 } 257 } 258 259 static int ad799x_scan_direct(struct ad799x_state *st, unsigned int ch) 260 { 261 u8 cmd; 262 263 switch (st->id) { 264 case ad7991: 265 case ad7995: 266 case ad7999: 267 cmd = st->config | (BIT(ch) << AD799X_CHANNEL_SHIFT); 268 break; 269 case ad7992: 270 case ad7993: 271 case ad7994: 272 cmd = BIT(ch) << AD799X_CHANNEL_SHIFT; 273 break; 274 case ad7997: 275 case ad7998: 276 cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE; 277 break; 278 default: 279 return -EINVAL; 280 } 281 282 return i2c_smbus_read_word_swapped(st->client, cmd); 283 } 284 285 static int ad799x_read_raw(struct iio_dev *indio_dev, 286 struct iio_chan_spec const *chan, 287 int *val, 288 int *val2, 289 long m) 290 { 291 int ret; 292 struct ad799x_state *st = iio_priv(indio_dev); 293 294 switch (m) { 295 case IIO_CHAN_INFO_RAW: 296 if (!iio_device_claim_direct(indio_dev)) 297 return -EBUSY; 298 mutex_lock(&st->lock); 299 ret = ad799x_scan_direct(st, chan->scan_index); 300 mutex_unlock(&st->lock); 301 iio_device_release_direct(indio_dev); 302 303 if (ret < 0) 304 return ret; 305 *val = (ret >> chan->scan_type.shift) & 306 GENMASK(chan->scan_type.realbits - 1, 0); 307 return IIO_VAL_INT; 308 case IIO_CHAN_INFO_SCALE: 309 if (st->vref) 310 ret = regulator_get_voltage(st->vref); 311 else 312 ret = regulator_get_voltage(st->reg); 313 314 if (ret < 0) 315 return ret; 316 *val = ret / 1000; 317 *val2 = chan->scan_type.realbits; 318 return IIO_VAL_FRACTIONAL_LOG2; 319 } 320 return -EINVAL; 321 } 322 static const unsigned int ad7998_frequencies[] = { 323 [AD7998_CYC_DIS] = 0, 324 [AD7998_CYC_TCONF_32] = 15625, 325 [AD7998_CYC_TCONF_64] = 7812, 326 [AD7998_CYC_TCONF_128] = 3906, 327 [AD7998_CYC_TCONF_512] = 976, 328 [AD7998_CYC_TCONF_1024] = 488, 329 [AD7998_CYC_TCONF_2048] = 244, 330 }; 331 332 static ssize_t ad799x_read_frequency(struct device *dev, 333 struct device_attribute *attr, 334 char *buf) 335 { 336 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 337 struct ad799x_state *st = iio_priv(indio_dev); 338 339 int ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG); 340 341 if (ret < 0) 342 return ret; 343 344 return sprintf(buf, "%u\n", ad7998_frequencies[ret & AD7998_CYC_MASK]); 345 } 346 347 static ssize_t ad799x_write_frequency(struct device *dev, 348 struct device_attribute *attr, 349 const char *buf, 350 size_t len) 351 { 352 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 353 struct ad799x_state *st = iio_priv(indio_dev); 354 355 long val; 356 int ret, i; 357 358 ret = kstrtol(buf, 10, &val); 359 if (ret) 360 return ret; 361 362 mutex_lock(&st->lock); 363 364 ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG); 365 if (ret < 0) 366 goto error_ret_mutex; 367 /* Wipe the bits clean */ 368 ret &= ~AD7998_CYC_MASK; 369 370 for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++) 371 if (val == ad7998_frequencies[i]) 372 break; 373 if (i == ARRAY_SIZE(ad7998_frequencies)) { 374 ret = -EINVAL; 375 goto error_ret_mutex; 376 } 377 378 ret = i2c_smbus_write_byte_data(st->client, AD7998_CYCLE_TMR_REG, 379 ret | i); 380 if (ret < 0) 381 goto error_ret_mutex; 382 ret = len; 383 384 error_ret_mutex: 385 mutex_unlock(&st->lock); 386 387 return ret; 388 } 389 390 static int ad799x_read_event_config(struct iio_dev *indio_dev, 391 const struct iio_chan_spec *chan, 392 enum iio_event_type type, 393 enum iio_event_direction dir) 394 { 395 struct ad799x_state *st = iio_priv(indio_dev); 396 397 if (!(st->config & AD7998_ALERT_EN)) 398 return 0; 399 400 if ((st->config >> AD799X_CHANNEL_SHIFT) & BIT(chan->scan_index)) 401 return 1; 402 403 return 0; 404 } 405 406 static int ad799x_write_event_config(struct iio_dev *indio_dev, 407 const struct iio_chan_spec *chan, 408 enum iio_event_type type, 409 enum iio_event_direction dir, 410 bool state) 411 { 412 struct ad799x_state *st = iio_priv(indio_dev); 413 int ret; 414 415 if (!iio_device_claim_direct(indio_dev)) 416 return -EBUSY; 417 418 mutex_lock(&st->lock); 419 420 if (state) 421 st->config |= BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT; 422 else 423 st->config &= ~(BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT); 424 425 if (st->config >> AD799X_CHANNEL_SHIFT) 426 st->config |= AD7998_ALERT_EN; 427 else 428 st->config &= ~AD7998_ALERT_EN; 429 430 ret = ad799x_write_config(st, st->config); 431 mutex_unlock(&st->lock); 432 iio_device_release_direct(indio_dev); 433 return ret; 434 } 435 436 static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan, 437 enum iio_event_direction dir, 438 enum iio_event_info info) 439 { 440 switch (info) { 441 case IIO_EV_INFO_VALUE: 442 if (dir == IIO_EV_DIR_FALLING) 443 return AD7998_DATALOW_REG(chan->channel); 444 else 445 return AD7998_DATAHIGH_REG(chan->channel); 446 case IIO_EV_INFO_HYSTERESIS: 447 return AD7998_HYST_REG(chan->channel); 448 default: 449 return -EINVAL; 450 } 451 452 return 0; 453 } 454 455 static int ad799x_write_event_value(struct iio_dev *indio_dev, 456 const struct iio_chan_spec *chan, 457 enum iio_event_type type, 458 enum iio_event_direction dir, 459 enum iio_event_info info, 460 int val, int val2) 461 { 462 int ret; 463 struct ad799x_state *st = iio_priv(indio_dev); 464 465 if (val < 0 || val > GENMASK(chan->scan_type.realbits - 1, 0)) 466 return -EINVAL; 467 468 ret = i2c_smbus_write_word_swapped(st->client, 469 ad799x_threshold_reg(chan, dir, info), 470 val << chan->scan_type.shift); 471 472 return ret; 473 } 474 475 static int ad799x_read_event_value(struct iio_dev *indio_dev, 476 const struct iio_chan_spec *chan, 477 enum iio_event_type type, 478 enum iio_event_direction dir, 479 enum iio_event_info info, 480 int *val, int *val2) 481 { 482 int ret; 483 struct ad799x_state *st = iio_priv(indio_dev); 484 485 ret = i2c_smbus_read_word_swapped(st->client, 486 ad799x_threshold_reg(chan, dir, info)); 487 if (ret < 0) 488 return ret; 489 *val = (ret >> chan->scan_type.shift) & 490 GENMASK(chan->scan_type.realbits - 1, 0); 491 492 return IIO_VAL_INT; 493 } 494 495 static irqreturn_t ad799x_event_handler(int irq, void *private) 496 { 497 struct iio_dev *indio_dev = private; 498 struct ad799x_state *st = iio_priv(private); 499 int i, ret; 500 501 ret = i2c_smbus_read_byte_data(st->client, AD7998_ALERT_STAT_REG); 502 if (ret <= 0) 503 goto done; 504 505 if (i2c_smbus_write_byte_data(st->client, AD7998_ALERT_STAT_REG, 506 AD7998_ALERT_STAT_CLEAR) < 0) 507 goto done; 508 509 for (i = 0; i < 8; i++) { 510 if (ret & BIT(i)) 511 iio_push_event(indio_dev, 512 i & 0x1 ? 513 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 514 (i >> 1), 515 IIO_EV_TYPE_THRESH, 516 IIO_EV_DIR_RISING) : 517 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 518 (i >> 1), 519 IIO_EV_TYPE_THRESH, 520 IIO_EV_DIR_FALLING), 521 iio_get_time_ns(indio_dev)); 522 } 523 524 done: 525 return IRQ_HANDLED; 526 } 527 528 static IIO_DEV_ATTR_SAMP_FREQ(0644, 529 ad799x_read_frequency, 530 ad799x_write_frequency); 531 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0"); 532 533 static struct attribute *ad799x_event_attributes[] = { 534 &iio_dev_attr_sampling_frequency.dev_attr.attr, 535 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 536 NULL, 537 }; 538 539 static const struct attribute_group ad799x_event_attrs_group = { 540 .attrs = ad799x_event_attributes, 541 }; 542 543 static const struct iio_info ad7991_info = { 544 .read_raw = &ad799x_read_raw, 545 .update_scan_mode = ad799x_update_scan_mode, 546 }; 547 548 static const struct iio_info ad7993_4_7_8_noirq_info = { 549 .read_raw = &ad799x_read_raw, 550 .update_scan_mode = ad799x_update_scan_mode, 551 }; 552 553 static const struct iio_info ad7993_4_7_8_irq_info = { 554 .read_raw = &ad799x_read_raw, 555 .event_attrs = &ad799x_event_attrs_group, 556 .read_event_config = &ad799x_read_event_config, 557 .write_event_config = &ad799x_write_event_config, 558 .read_event_value = &ad799x_read_event_value, 559 .write_event_value = &ad799x_write_event_value, 560 .update_scan_mode = ad799x_update_scan_mode, 561 }; 562 563 static const struct iio_event_spec ad799x_events[] = { 564 { 565 .type = IIO_EV_TYPE_THRESH, 566 .dir = IIO_EV_DIR_RISING, 567 .mask_separate = BIT(IIO_EV_INFO_VALUE) | 568 BIT(IIO_EV_INFO_ENABLE), 569 }, { 570 .type = IIO_EV_TYPE_THRESH, 571 .dir = IIO_EV_DIR_FALLING, 572 .mask_separate = BIT(IIO_EV_INFO_VALUE) | 573 BIT(IIO_EV_INFO_ENABLE), 574 }, { 575 .type = IIO_EV_TYPE_THRESH, 576 .dir = IIO_EV_DIR_EITHER, 577 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS), 578 }, 579 }; 580 581 #define _AD799X_CHANNEL(_index, _realbits, _ev_spec, _num_ev_spec) { \ 582 .type = IIO_VOLTAGE, \ 583 .indexed = 1, \ 584 .channel = (_index), \ 585 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 586 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 587 .scan_index = (_index), \ 588 .scan_type = { \ 589 .sign = 'u', \ 590 .realbits = (_realbits), \ 591 .storagebits = 16, \ 592 .shift = 12 - (_realbits), \ 593 .endianness = IIO_BE, \ 594 }, \ 595 .event_spec = _ev_spec, \ 596 .num_event_specs = _num_ev_spec, \ 597 } 598 599 #define AD799X_CHANNEL(_index, _realbits) \ 600 _AD799X_CHANNEL(_index, _realbits, NULL, 0) 601 602 #define AD799X_CHANNEL_WITH_EVENTS(_index, _realbits) \ 603 _AD799X_CHANNEL(_index, _realbits, ad799x_events, \ 604 ARRAY_SIZE(ad799x_events)) 605 606 static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { 607 [ad7991] = { 608 .num_channels = 5, 609 .has_vref = true, 610 .noirq_config = { 611 .channel = { 612 AD799X_CHANNEL(0, 12), 613 AD799X_CHANNEL(1, 12), 614 AD799X_CHANNEL(2, 12), 615 AD799X_CHANNEL(3, 12), 616 IIO_CHAN_SOFT_TIMESTAMP(4), 617 }, 618 .info = &ad7991_info, 619 }, 620 }, 621 [ad7995] = { 622 .num_channels = 5, 623 .has_vref = true, 624 .noirq_config = { 625 .channel = { 626 AD799X_CHANNEL(0, 10), 627 AD799X_CHANNEL(1, 10), 628 AD799X_CHANNEL(2, 10), 629 AD799X_CHANNEL(3, 10), 630 IIO_CHAN_SOFT_TIMESTAMP(4), 631 }, 632 .info = &ad7991_info, 633 }, 634 }, 635 [ad7999] = { 636 .num_channels = 5, 637 .has_vref = true, 638 .noirq_config = { 639 .channel = { 640 AD799X_CHANNEL(0, 8), 641 AD799X_CHANNEL(1, 8), 642 AD799X_CHANNEL(2, 8), 643 AD799X_CHANNEL(3, 8), 644 IIO_CHAN_SOFT_TIMESTAMP(4), 645 }, 646 .info = &ad7991_info, 647 }, 648 }, 649 [ad7992] = { 650 .num_channels = 3, 651 .noirq_config = { 652 .channel = { 653 AD799X_CHANNEL(0, 12), 654 AD799X_CHANNEL(1, 12), 655 IIO_CHAN_SOFT_TIMESTAMP(3), 656 }, 657 .info = &ad7993_4_7_8_noirq_info, 658 }, 659 .irq_config = { 660 .channel = { 661 AD799X_CHANNEL_WITH_EVENTS(0, 12), 662 AD799X_CHANNEL_WITH_EVENTS(1, 12), 663 IIO_CHAN_SOFT_TIMESTAMP(3), 664 }, 665 .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT, 666 .info = &ad7993_4_7_8_irq_info, 667 }, 668 }, 669 [ad7993] = { 670 .num_channels = 5, 671 .noirq_config = { 672 .channel = { 673 AD799X_CHANNEL(0, 10), 674 AD799X_CHANNEL(1, 10), 675 AD799X_CHANNEL(2, 10), 676 AD799X_CHANNEL(3, 10), 677 IIO_CHAN_SOFT_TIMESTAMP(4), 678 }, 679 .info = &ad7993_4_7_8_noirq_info, 680 }, 681 .irq_config = { 682 .channel = { 683 AD799X_CHANNEL_WITH_EVENTS(0, 10), 684 AD799X_CHANNEL_WITH_EVENTS(1, 10), 685 AD799X_CHANNEL_WITH_EVENTS(2, 10), 686 AD799X_CHANNEL_WITH_EVENTS(3, 10), 687 IIO_CHAN_SOFT_TIMESTAMP(4), 688 }, 689 .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT, 690 .info = &ad7993_4_7_8_irq_info, 691 }, 692 }, 693 [ad7994] = { 694 .num_channels = 5, 695 .has_vref = true, 696 .noirq_config = { 697 .channel = { 698 AD799X_CHANNEL(0, 12), 699 AD799X_CHANNEL(1, 12), 700 AD799X_CHANNEL(2, 12), 701 AD799X_CHANNEL(3, 12), 702 IIO_CHAN_SOFT_TIMESTAMP(4), 703 }, 704 .info = &ad7993_4_7_8_noirq_info, 705 }, 706 .irq_config = { 707 .channel = { 708 AD799X_CHANNEL_WITH_EVENTS(0, 12), 709 AD799X_CHANNEL_WITH_EVENTS(1, 12), 710 AD799X_CHANNEL_WITH_EVENTS(2, 12), 711 AD799X_CHANNEL_WITH_EVENTS(3, 12), 712 IIO_CHAN_SOFT_TIMESTAMP(4), 713 }, 714 .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT, 715 .info = &ad7993_4_7_8_irq_info, 716 }, 717 }, 718 [ad7997] = { 719 .num_channels = 9, 720 .noirq_config = { 721 .channel = { 722 AD799X_CHANNEL(0, 10), 723 AD799X_CHANNEL(1, 10), 724 AD799X_CHANNEL(2, 10), 725 AD799X_CHANNEL(3, 10), 726 AD799X_CHANNEL(4, 10), 727 AD799X_CHANNEL(5, 10), 728 AD799X_CHANNEL(6, 10), 729 AD799X_CHANNEL(7, 10), 730 IIO_CHAN_SOFT_TIMESTAMP(8), 731 }, 732 .info = &ad7993_4_7_8_noirq_info, 733 }, 734 .irq_config = { 735 .channel = { 736 AD799X_CHANNEL_WITH_EVENTS(0, 10), 737 AD799X_CHANNEL_WITH_EVENTS(1, 10), 738 AD799X_CHANNEL_WITH_EVENTS(2, 10), 739 AD799X_CHANNEL_WITH_EVENTS(3, 10), 740 AD799X_CHANNEL(4, 10), 741 AD799X_CHANNEL(5, 10), 742 AD799X_CHANNEL(6, 10), 743 AD799X_CHANNEL(7, 10), 744 IIO_CHAN_SOFT_TIMESTAMP(8), 745 }, 746 .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT, 747 .info = &ad7993_4_7_8_irq_info, 748 }, 749 }, 750 [ad7998] = { 751 .num_channels = 9, 752 .noirq_config = { 753 .channel = { 754 AD799X_CHANNEL(0, 12), 755 AD799X_CHANNEL(1, 12), 756 AD799X_CHANNEL(2, 12), 757 AD799X_CHANNEL(3, 12), 758 AD799X_CHANNEL(4, 12), 759 AD799X_CHANNEL(5, 12), 760 AD799X_CHANNEL(6, 12), 761 AD799X_CHANNEL(7, 12), 762 IIO_CHAN_SOFT_TIMESTAMP(8), 763 }, 764 .info = &ad7993_4_7_8_noirq_info, 765 }, 766 .irq_config = { 767 .channel = { 768 AD799X_CHANNEL_WITH_EVENTS(0, 12), 769 AD799X_CHANNEL_WITH_EVENTS(1, 12), 770 AD799X_CHANNEL_WITH_EVENTS(2, 12), 771 AD799X_CHANNEL_WITH_EVENTS(3, 12), 772 AD799X_CHANNEL(4, 12), 773 AD799X_CHANNEL(5, 12), 774 AD799X_CHANNEL(6, 12), 775 AD799X_CHANNEL(7, 12), 776 IIO_CHAN_SOFT_TIMESTAMP(8), 777 }, 778 .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT, 779 .info = &ad7993_4_7_8_irq_info, 780 }, 781 }, 782 }; 783 784 static int ad799x_probe(struct i2c_client *client) 785 { 786 const struct i2c_device_id *id = i2c_client_get_device_id(client); 787 int ret; 788 int extra_config = 0; 789 struct ad799x_state *st; 790 struct iio_dev *indio_dev; 791 const struct ad799x_chip_info *chip_info = 792 &ad799x_chip_info_tbl[id->driver_data]; 793 794 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st)); 795 if (indio_dev == NULL) 796 return -ENOMEM; 797 798 st = iio_priv(indio_dev); 799 /* this is only used for device removal purposes */ 800 i2c_set_clientdata(client, indio_dev); 801 802 st->id = id->driver_data; 803 if (client->irq > 0 && chip_info->irq_config.info) 804 st->chip_config = &chip_info->irq_config; 805 else 806 st->chip_config = &chip_info->noirq_config; 807 808 /* TODO: Add pdata options for filtering and bit delay */ 809 810 st->reg = devm_regulator_get(&client->dev, "vcc"); 811 if (IS_ERR(st->reg)) 812 return PTR_ERR(st->reg); 813 ret = regulator_enable(st->reg); 814 if (ret) 815 return ret; 816 817 /* check if an external reference is supplied */ 818 if (chip_info->has_vref) { 819 st->vref = devm_regulator_get_optional(&client->dev, "vref"); 820 ret = PTR_ERR_OR_ZERO(st->vref); 821 if (ret) { 822 if (ret != -ENODEV) 823 goto error_disable_reg; 824 st->vref = NULL; 825 dev_info(&client->dev, "Using VCC reference voltage\n"); 826 } 827 828 if (st->vref) { 829 dev_info(&client->dev, "Using external reference voltage\n"); 830 extra_config |= AD7991_REF_SEL; 831 ret = regulator_enable(st->vref); 832 if (ret) 833 goto error_disable_reg; 834 } 835 } 836 837 st->client = client; 838 839 indio_dev->name = id->name; 840 indio_dev->info = st->chip_config->info; 841 842 indio_dev->modes = INDIO_DIRECT_MODE; 843 indio_dev->channels = st->chip_config->channel; 844 indio_dev->num_channels = chip_info->num_channels; 845 846 ret = ad799x_update_config(st, st->chip_config->default_config | extra_config); 847 if (ret) 848 goto error_disable_vref; 849 850 ret = iio_triggered_buffer_setup(indio_dev, NULL, 851 &ad799x_trigger_handler, NULL); 852 if (ret) 853 goto error_disable_vref; 854 855 if (client->irq > 0) { 856 ret = devm_request_threaded_irq(&client->dev, 857 client->irq, 858 NULL, 859 ad799x_event_handler, 860 IRQF_TRIGGER_FALLING | 861 IRQF_ONESHOT, 862 client->name, 863 indio_dev); 864 if (ret) 865 goto error_cleanup_ring; 866 } 867 868 mutex_init(&st->lock); 869 870 ret = iio_device_register(indio_dev); 871 if (ret) 872 goto error_cleanup_ring; 873 874 return 0; 875 876 error_cleanup_ring: 877 iio_triggered_buffer_cleanup(indio_dev); 878 error_disable_vref: 879 if (st->vref) 880 regulator_disable(st->vref); 881 error_disable_reg: 882 regulator_disable(st->reg); 883 884 return ret; 885 } 886 887 static void ad799x_remove(struct i2c_client *client) 888 { 889 struct iio_dev *indio_dev = i2c_get_clientdata(client); 890 struct ad799x_state *st = iio_priv(indio_dev); 891 892 iio_device_unregister(indio_dev); 893 894 iio_triggered_buffer_cleanup(indio_dev); 895 if (st->vref) 896 regulator_disable(st->vref); 897 regulator_disable(st->reg); 898 kfree(st->rx_buf); 899 } 900 901 static int ad799x_suspend(struct device *dev) 902 { 903 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 904 struct ad799x_state *st = iio_priv(indio_dev); 905 906 if (st->vref) 907 regulator_disable(st->vref); 908 regulator_disable(st->reg); 909 910 return 0; 911 } 912 913 static int ad799x_resume(struct device *dev) 914 { 915 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 916 struct ad799x_state *st = iio_priv(indio_dev); 917 int ret; 918 919 ret = regulator_enable(st->reg); 920 if (ret) { 921 dev_err(dev, "Unable to enable vcc regulator\n"); 922 return ret; 923 } 924 925 if (st->vref) { 926 ret = regulator_enable(st->vref); 927 if (ret) { 928 regulator_disable(st->reg); 929 dev_err(dev, "Unable to enable vref regulator\n"); 930 return ret; 931 } 932 } 933 934 /* resync config */ 935 ret = ad799x_update_config(st, st->config); 936 if (ret) { 937 if (st->vref) 938 regulator_disable(st->vref); 939 regulator_disable(st->reg); 940 return ret; 941 } 942 943 return 0; 944 } 945 946 static DEFINE_SIMPLE_DEV_PM_OPS(ad799x_pm_ops, ad799x_suspend, ad799x_resume); 947 948 static const struct i2c_device_id ad799x_id[] = { 949 { "ad7991", ad7991 }, 950 { "ad7995", ad7995 }, 951 { "ad7999", ad7999 }, 952 { "ad7992", ad7992 }, 953 { "ad7993", ad7993 }, 954 { "ad7994", ad7994 }, 955 { "ad7997", ad7997 }, 956 { "ad7998", ad7998 }, 957 { } 958 }; 959 960 MODULE_DEVICE_TABLE(i2c, ad799x_id); 961 962 static struct i2c_driver ad799x_driver = { 963 .driver = { 964 .name = "ad799x", 965 .pm = pm_sleep_ptr(&ad799x_pm_ops), 966 }, 967 .probe = ad799x_probe, 968 .remove = ad799x_remove, 969 .id_table = ad799x_id, 970 }; 971 module_i2c_driver(ad799x_driver); 972 973 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 974 MODULE_DESCRIPTION("Analog Devices AD799x ADC"); 975 MODULE_LICENSE("GPL v2"); 976