1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor 4 * 5 * Copyright 2010-2011 Analog Devices Inc. 6 */ 7 8 #include <linux/device.h> 9 #include <linux/err.h> 10 #include <linux/i2c.h> 11 #include <linux/interrupt.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/mutex.h> 15 #include <linux/regulator/consumer.h> 16 #include <linux/slab.h> 17 #include <linux/sysfs.h> 18 19 #include <linux/iio/iio.h> 20 #include <linux/iio/sysfs.h> 21 #include <linux/iio/events.h> 22 23 #include <linux/platform_data/ad7291.h> 24 25 /* 26 * Simplified handling 27 * 28 * If no events enabled - single polled channel read 29 * If event enabled direct reads disable unless channel 30 * is in the read mask. 31 * 32 * The noise-delayed bit as per datasheet suggestion is always enabled. 33 */ 34 35 /* 36 * AD7291 registers definition 37 */ 38 #define AD7291_COMMAND 0x00 39 #define AD7291_VOLTAGE 0x01 40 #define AD7291_T_SENSE 0x02 41 #define AD7291_T_AVERAGE 0x03 42 #define AD7291_DATA_HIGH(x) ((x) * 3 + 0x4) 43 #define AD7291_DATA_LOW(x) ((x) * 3 + 0x5) 44 #define AD7291_HYST(x) ((x) * 3 + 0x6) 45 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F 46 #define AD7291_T_ALERT_STATUS 0x20 47 48 #define AD7291_BITS 12 49 #define AD7291_VOLTAGE_LIMIT_COUNT 8 50 51 52 /* 53 * AD7291 command 54 */ 55 #define AD7291_AUTOCYCLE BIT(0) 56 #define AD7291_RESET BIT(1) 57 #define AD7291_ALERT_CLEAR BIT(2) 58 #define AD7291_ALERT_POLARITY BIT(3) 59 #define AD7291_EXT_REF BIT(4) 60 #define AD7291_NOISE_DELAY BIT(5) 61 #define AD7291_T_SENSE_MASK BIT(7) 62 #define AD7291_VOLTAGE_MASK GENMASK(15, 8) 63 #define AD7291_VOLTAGE_OFFSET 8 64 65 /* 66 * AD7291 value masks 67 */ 68 #define AD7291_VALUE_MASK GENMASK(11, 0) 69 70 /* 71 * AD7291 alert register bits 72 */ 73 #define AD7291_T_LOW BIT(0) 74 #define AD7291_T_HIGH BIT(1) 75 #define AD7291_T_AVG_LOW BIT(2) 76 #define AD7291_T_AVG_HIGH BIT(3) 77 #define AD7291_V_LOW(x) BIT((x) * 2) 78 #define AD7291_V_HIGH(x) BIT((x) * 2 + 1) 79 80 81 struct ad7291_chip_info { 82 struct i2c_client *client; 83 struct regulator *reg; 84 u16 command; 85 u16 c_mask; /* Active voltage channels for events */ 86 struct mutex state_lock; 87 }; 88 89 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data) 90 { 91 struct i2c_client *client = chip->client; 92 int ret = 0; 93 94 ret = i2c_smbus_read_word_swapped(client, reg); 95 if (ret < 0) { 96 dev_err(&client->dev, "I2C read error\n"); 97 return ret; 98 } 99 100 *data = ret; 101 102 return 0; 103 } 104 105 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data) 106 { 107 return i2c_smbus_write_word_swapped(chip->client, reg, data); 108 } 109 110 static irqreturn_t ad7291_event_handler(int irq, void *private) 111 { 112 struct iio_dev *indio_dev = private; 113 struct ad7291_chip_info *chip = iio_priv(private); 114 u16 t_status, v_status; 115 u16 command; 116 int i; 117 s64 timestamp = iio_get_time_ns(indio_dev); 118 119 if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status)) 120 return IRQ_HANDLED; 121 122 if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status)) 123 return IRQ_HANDLED; 124 125 if (!(t_status || v_status)) 126 return IRQ_HANDLED; 127 128 command = chip->command | AD7291_ALERT_CLEAR; 129 ad7291_i2c_write(chip, AD7291_COMMAND, command); 130 131 command = chip->command & ~AD7291_ALERT_CLEAR; 132 ad7291_i2c_write(chip, AD7291_COMMAND, command); 133 134 /* For now treat t_sense and t_sense_average the same */ 135 if ((t_status & AD7291_T_LOW) || (t_status & AD7291_T_AVG_LOW)) 136 iio_push_event(indio_dev, 137 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 138 0, 139 IIO_EV_TYPE_THRESH, 140 IIO_EV_DIR_FALLING), 141 timestamp); 142 if ((t_status & AD7291_T_HIGH) || (t_status & AD7291_T_AVG_HIGH)) 143 iio_push_event(indio_dev, 144 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 145 0, 146 IIO_EV_TYPE_THRESH, 147 IIO_EV_DIR_RISING), 148 timestamp); 149 150 for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) { 151 if (v_status & AD7291_V_LOW(i)) 152 iio_push_event(indio_dev, 153 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 154 i, 155 IIO_EV_TYPE_THRESH, 156 IIO_EV_DIR_FALLING), 157 timestamp); 158 if (v_status & AD7291_V_HIGH(i)) 159 iio_push_event(indio_dev, 160 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 161 i, 162 IIO_EV_TYPE_THRESH, 163 IIO_EV_DIR_RISING), 164 timestamp); 165 } 166 167 return IRQ_HANDLED; 168 } 169 170 static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan, 171 enum iio_event_direction dir, 172 enum iio_event_info info) 173 { 174 unsigned int offset; 175 176 switch (chan->type) { 177 case IIO_VOLTAGE: 178 offset = chan->channel; 179 break; 180 case IIO_TEMP: 181 offset = AD7291_VOLTAGE_OFFSET; 182 break; 183 default: 184 return 0; 185 } 186 187 switch (info) { 188 case IIO_EV_INFO_VALUE: 189 if (dir == IIO_EV_DIR_FALLING) 190 return AD7291_DATA_HIGH(offset); 191 else 192 return AD7291_DATA_LOW(offset); 193 case IIO_EV_INFO_HYSTERESIS: 194 return AD7291_HYST(offset); 195 default: 196 break; 197 } 198 return 0; 199 } 200 201 static int ad7291_read_event_value(struct iio_dev *indio_dev, 202 const struct iio_chan_spec *chan, 203 enum iio_event_type type, 204 enum iio_event_direction dir, 205 enum iio_event_info info, 206 int *val, int *val2) 207 { 208 struct ad7291_chip_info *chip = iio_priv(indio_dev); 209 int ret; 210 u16 uval; 211 212 ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info), 213 &uval); 214 if (ret < 0) 215 return ret; 216 217 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) 218 *val = uval & AD7291_VALUE_MASK; 219 220 else 221 *val = sign_extend32(uval, 11); 222 223 return IIO_VAL_INT; 224 } 225 226 static int ad7291_write_event_value(struct iio_dev *indio_dev, 227 const struct iio_chan_spec *chan, 228 enum iio_event_type type, 229 enum iio_event_direction dir, 230 enum iio_event_info info, 231 int val, int val2) 232 { 233 struct ad7291_chip_info *chip = iio_priv(indio_dev); 234 235 if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) { 236 if (val > AD7291_VALUE_MASK || val < 0) 237 return -EINVAL; 238 } else { 239 if (val > 2047 || val < -2048) 240 return -EINVAL; 241 } 242 243 return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info), 244 val); 245 } 246 247 static int ad7291_read_event_config(struct iio_dev *indio_dev, 248 const struct iio_chan_spec *chan, 249 enum iio_event_type type, 250 enum iio_event_direction dir) 251 { 252 struct ad7291_chip_info *chip = iio_priv(indio_dev); 253 /* 254 * To be enabled the channel must simply be on. If any are enabled 255 * we are in continuous sampling mode 256 */ 257 258 switch (chan->type) { 259 case IIO_VOLTAGE: 260 return !!(chip->c_mask & BIT(15 - chan->channel)); 261 case IIO_TEMP: 262 /* always on */ 263 return 1; 264 default: 265 return -EINVAL; 266 } 267 268 } 269 270 static int ad7291_write_event_config(struct iio_dev *indio_dev, 271 const struct iio_chan_spec *chan, 272 enum iio_event_type type, 273 enum iio_event_direction dir, 274 int state) 275 { 276 int ret = 0; 277 struct ad7291_chip_info *chip = iio_priv(indio_dev); 278 unsigned int mask; 279 u16 regval; 280 281 mutex_lock(&chip->state_lock); 282 regval = chip->command; 283 /* 284 * To be enabled the channel must simply be on. If any are enabled 285 * use continuous sampling mode. 286 * Possible to disable temp as well but that makes single read tricky. 287 */ 288 289 mask = BIT(15 - chan->channel); 290 291 switch (chan->type) { 292 case IIO_VOLTAGE: 293 if ((!state) && (chip->c_mask & mask)) 294 chip->c_mask &= ~mask; 295 else if (state && (!(chip->c_mask & mask))) 296 chip->c_mask |= mask; 297 else 298 break; 299 300 regval &= ~AD7291_AUTOCYCLE; 301 regval |= chip->c_mask; 302 if (chip->c_mask) /* Enable autocycle? */ 303 regval |= AD7291_AUTOCYCLE; 304 305 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval); 306 if (ret < 0) 307 goto error_ret; 308 309 chip->command = regval; 310 break; 311 default: 312 ret = -EINVAL; 313 } 314 315 error_ret: 316 mutex_unlock(&chip->state_lock); 317 return ret; 318 } 319 320 static int ad7291_read_raw(struct iio_dev *indio_dev, 321 struct iio_chan_spec const *chan, 322 int *val, 323 int *val2, 324 long mask) 325 { 326 int ret; 327 struct ad7291_chip_info *chip = iio_priv(indio_dev); 328 u16 regval; 329 330 switch (mask) { 331 case IIO_CHAN_INFO_RAW: 332 switch (chan->type) { 333 case IIO_VOLTAGE: 334 mutex_lock(&chip->state_lock); 335 /* If in autocycle mode drop through */ 336 if (chip->command & AD7291_AUTOCYCLE) { 337 mutex_unlock(&chip->state_lock); 338 return -EBUSY; 339 } 340 /* Enable this channel alone */ 341 regval = chip->command & (~AD7291_VOLTAGE_MASK); 342 regval |= BIT(15 - chan->channel); 343 ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval); 344 if (ret < 0) { 345 mutex_unlock(&chip->state_lock); 346 return ret; 347 } 348 /* Read voltage */ 349 ret = i2c_smbus_read_word_swapped(chip->client, 350 AD7291_VOLTAGE); 351 if (ret < 0) { 352 mutex_unlock(&chip->state_lock); 353 return ret; 354 } 355 *val = ret & AD7291_VALUE_MASK; 356 mutex_unlock(&chip->state_lock); 357 return IIO_VAL_INT; 358 case IIO_TEMP: 359 /* Assumes tsense bit of command register always set */ 360 ret = i2c_smbus_read_word_swapped(chip->client, 361 AD7291_T_SENSE); 362 if (ret < 0) 363 return ret; 364 *val = sign_extend32(ret, 11); 365 return IIO_VAL_INT; 366 default: 367 return -EINVAL; 368 } 369 case IIO_CHAN_INFO_AVERAGE_RAW: 370 ret = i2c_smbus_read_word_swapped(chip->client, 371 AD7291_T_AVERAGE); 372 if (ret < 0) 373 return ret; 374 *val = sign_extend32(ret, 11); 375 return IIO_VAL_INT; 376 case IIO_CHAN_INFO_SCALE: 377 switch (chan->type) { 378 case IIO_VOLTAGE: 379 if (chip->reg) { 380 int vref; 381 382 vref = regulator_get_voltage(chip->reg); 383 if (vref < 0) 384 return vref; 385 *val = vref / 1000; 386 } else { 387 *val = 2500; 388 } 389 *val2 = AD7291_BITS; 390 return IIO_VAL_FRACTIONAL_LOG2; 391 case IIO_TEMP: 392 /* 393 * One LSB of the ADC corresponds to 0.25 deg C. 394 * The temperature reading is in 12-bit twos 395 * complement format 396 */ 397 *val = 250; 398 return IIO_VAL_INT; 399 default: 400 return -EINVAL; 401 } 402 default: 403 return -EINVAL; 404 } 405 } 406 407 static const struct iio_event_spec ad7291_events[] = { 408 { 409 .type = IIO_EV_TYPE_THRESH, 410 .dir = IIO_EV_DIR_RISING, 411 .mask_separate = BIT(IIO_EV_INFO_VALUE) | 412 BIT(IIO_EV_INFO_ENABLE), 413 }, { 414 .type = IIO_EV_TYPE_THRESH, 415 .dir = IIO_EV_DIR_FALLING, 416 .mask_separate = BIT(IIO_EV_INFO_VALUE) | 417 BIT(IIO_EV_INFO_ENABLE), 418 }, { 419 .type = IIO_EV_TYPE_THRESH, 420 .dir = IIO_EV_DIR_EITHER, 421 .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS), 422 }, 423 }; 424 425 #define AD7291_VOLTAGE_CHAN(_chan) \ 426 { \ 427 .type = IIO_VOLTAGE, \ 428 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 429 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 430 .indexed = 1, \ 431 .channel = _chan, \ 432 .event_spec = ad7291_events, \ 433 .num_event_specs = ARRAY_SIZE(ad7291_events), \ 434 } 435 436 static const struct iio_chan_spec ad7291_channels[] = { 437 AD7291_VOLTAGE_CHAN(0), 438 AD7291_VOLTAGE_CHAN(1), 439 AD7291_VOLTAGE_CHAN(2), 440 AD7291_VOLTAGE_CHAN(3), 441 AD7291_VOLTAGE_CHAN(4), 442 AD7291_VOLTAGE_CHAN(5), 443 AD7291_VOLTAGE_CHAN(6), 444 AD7291_VOLTAGE_CHAN(7), 445 { 446 .type = IIO_TEMP, 447 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 448 BIT(IIO_CHAN_INFO_AVERAGE_RAW) | 449 BIT(IIO_CHAN_INFO_SCALE), 450 .indexed = 1, 451 .channel = 0, 452 .event_spec = ad7291_events, 453 .num_event_specs = ARRAY_SIZE(ad7291_events), 454 } 455 }; 456 457 static const struct iio_info ad7291_info = { 458 .read_raw = &ad7291_read_raw, 459 .read_event_config = &ad7291_read_event_config, 460 .write_event_config = &ad7291_write_event_config, 461 .read_event_value = &ad7291_read_event_value, 462 .write_event_value = &ad7291_write_event_value, 463 }; 464 465 static int ad7291_probe(struct i2c_client *client, 466 const struct i2c_device_id *id) 467 { 468 struct ad7291_platform_data *pdata = client->dev.platform_data; 469 struct ad7291_chip_info *chip; 470 struct iio_dev *indio_dev; 471 int ret; 472 473 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); 474 if (!indio_dev) 475 return -ENOMEM; 476 chip = iio_priv(indio_dev); 477 478 if (pdata && pdata->use_external_ref) { 479 chip->reg = devm_regulator_get(&client->dev, "vref"); 480 if (IS_ERR(chip->reg)) 481 return PTR_ERR(chip->reg); 482 483 ret = regulator_enable(chip->reg); 484 if (ret) 485 return ret; 486 } 487 488 mutex_init(&chip->state_lock); 489 /* this is only used for device removal purposes */ 490 i2c_set_clientdata(client, indio_dev); 491 492 chip->client = client; 493 494 chip->command = AD7291_NOISE_DELAY | 495 AD7291_T_SENSE_MASK | /* Tsense always enabled */ 496 AD7291_ALERT_POLARITY; /* set irq polarity low level */ 497 498 if (pdata && pdata->use_external_ref) 499 chip->command |= AD7291_EXT_REF; 500 501 indio_dev->name = id->name; 502 indio_dev->channels = ad7291_channels; 503 indio_dev->num_channels = ARRAY_SIZE(ad7291_channels); 504 505 indio_dev->info = &ad7291_info; 506 indio_dev->modes = INDIO_DIRECT_MODE; 507 508 ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET); 509 if (ret) { 510 ret = -EIO; 511 goto error_disable_reg; 512 } 513 514 ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command); 515 if (ret) { 516 ret = -EIO; 517 goto error_disable_reg; 518 } 519 520 if (client->irq > 0) { 521 ret = request_threaded_irq(client->irq, 522 NULL, 523 &ad7291_event_handler, 524 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 525 id->name, 526 indio_dev); 527 if (ret) 528 goto error_disable_reg; 529 } 530 531 ret = iio_device_register(indio_dev); 532 if (ret) 533 goto error_unreg_irq; 534 535 return 0; 536 537 error_unreg_irq: 538 if (client->irq) 539 free_irq(client->irq, indio_dev); 540 error_disable_reg: 541 if (chip->reg) 542 regulator_disable(chip->reg); 543 544 return ret; 545 } 546 547 static int ad7291_remove(struct i2c_client *client) 548 { 549 struct iio_dev *indio_dev = i2c_get_clientdata(client); 550 struct ad7291_chip_info *chip = iio_priv(indio_dev); 551 552 iio_device_unregister(indio_dev); 553 554 if (client->irq) 555 free_irq(client->irq, indio_dev); 556 557 if (chip->reg) 558 regulator_disable(chip->reg); 559 560 return 0; 561 } 562 563 static const struct i2c_device_id ad7291_id[] = { 564 { "ad7291", 0 }, 565 {} 566 }; 567 568 MODULE_DEVICE_TABLE(i2c, ad7291_id); 569 570 static struct i2c_driver ad7291_driver = { 571 .driver = { 572 .name = KBUILD_MODNAME, 573 }, 574 .probe = ad7291_probe, 575 .remove = ad7291_remove, 576 .id_table = ad7291_id, 577 }; 578 module_i2c_driver(ad7291_driver); 579 580 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>"); 581 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver"); 582 MODULE_LICENSE("GPL v2"); 583