1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Support code for Analog Devices Sigma-Delta ADCs 4 * 5 * Copyright 2012 Analog Devices Inc. 6 * Author: Lars-Peter Clausen <lars@metafoo.de> 7 */ 8 9 #include <linux/interrupt.h> 10 #include <linux/device.h> 11 #include <linux/kernel.h> 12 #include <linux/slab.h> 13 #include <linux/spi/spi.h> 14 #include <linux/err.h> 15 #include <linux/module.h> 16 17 #include <linux/iio/iio.h> 18 #include <linux/iio/sysfs.h> 19 #include <linux/iio/buffer.h> 20 #include <linux/iio/trigger.h> 21 #include <linux/iio/trigger_consumer.h> 22 #include <linux/iio/triggered_buffer.h> 23 #include <linux/iio/adc/ad_sigma_delta.h> 24 25 #include <asm/unaligned.h> 26 27 28 #define AD_SD_COMM_CHAN_MASK 0x3 29 30 #define AD_SD_REG_COMM 0x00 31 #define AD_SD_REG_DATA 0x03 32 33 /** 34 * ad_sd_set_comm() - Set communications register 35 * 36 * @sigma_delta: The sigma delta device 37 * @comm: New value for the communications register 38 */ 39 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm) 40 { 41 /* Some variants use the lower two bits of the communications register 42 * to select the channel */ 43 sigma_delta->comm = comm & AD_SD_COMM_CHAN_MASK; 44 } 45 EXPORT_SYMBOL_GPL(ad_sd_set_comm); 46 47 /** 48 * ad_sd_write_reg() - Write a register 49 * 50 * @sigma_delta: The sigma delta device 51 * @reg: Address of the register 52 * @size: Size of the register (0-3) 53 * @val: Value to write to the register 54 * 55 * Returns 0 on success, an error code otherwise. 56 **/ 57 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg, 58 unsigned int size, unsigned int val) 59 { 60 uint8_t *data = sigma_delta->data; 61 struct spi_transfer t = { 62 .tx_buf = data, 63 .len = size + 1, 64 .cs_change = sigma_delta->keep_cs_asserted, 65 }; 66 struct spi_message m; 67 int ret; 68 69 data[0] = (reg << sigma_delta->info->addr_shift) | sigma_delta->comm; 70 71 switch (size) { 72 case 3: 73 put_unaligned_be24(val, &data[1]); 74 break; 75 case 2: 76 put_unaligned_be16(val, &data[1]); 77 break; 78 case 1: 79 data[1] = val; 80 break; 81 case 0: 82 break; 83 default: 84 return -EINVAL; 85 } 86 87 spi_message_init(&m); 88 spi_message_add_tail(&t, &m); 89 90 if (sigma_delta->bus_locked) 91 ret = spi_sync_locked(sigma_delta->spi, &m); 92 else 93 ret = spi_sync(sigma_delta->spi, &m); 94 95 return ret; 96 } 97 EXPORT_SYMBOL_GPL(ad_sd_write_reg); 98 99 static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta, 100 unsigned int reg, unsigned int size, uint8_t *val) 101 { 102 uint8_t *data = sigma_delta->data; 103 int ret; 104 struct spi_transfer t[] = { 105 { 106 .tx_buf = data, 107 .len = 1, 108 }, { 109 .rx_buf = val, 110 .len = size, 111 .cs_change = sigma_delta->bus_locked, 112 }, 113 }; 114 struct spi_message m; 115 116 spi_message_init(&m); 117 118 if (sigma_delta->info->has_registers) { 119 data[0] = reg << sigma_delta->info->addr_shift; 120 data[0] |= sigma_delta->info->read_mask; 121 data[0] |= sigma_delta->comm; 122 spi_message_add_tail(&t[0], &m); 123 } 124 spi_message_add_tail(&t[1], &m); 125 126 if (sigma_delta->bus_locked) 127 ret = spi_sync_locked(sigma_delta->spi, &m); 128 else 129 ret = spi_sync(sigma_delta->spi, &m); 130 131 return ret; 132 } 133 134 /** 135 * ad_sd_read_reg() - Read a register 136 * 137 * @sigma_delta: The sigma delta device 138 * @reg: Address of the register 139 * @size: Size of the register (1-4) 140 * @val: Read value 141 * 142 * Returns 0 on success, an error code otherwise. 143 **/ 144 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta, 145 unsigned int reg, unsigned int size, unsigned int *val) 146 { 147 int ret; 148 149 ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data); 150 if (ret < 0) 151 goto out; 152 153 switch (size) { 154 case 4: 155 *val = get_unaligned_be32(sigma_delta->data); 156 break; 157 case 3: 158 *val = get_unaligned_be24(&sigma_delta->data[0]); 159 break; 160 case 2: 161 *val = get_unaligned_be16(sigma_delta->data); 162 break; 163 case 1: 164 *val = sigma_delta->data[0]; 165 break; 166 default: 167 ret = -EINVAL; 168 break; 169 } 170 171 out: 172 return ret; 173 } 174 EXPORT_SYMBOL_GPL(ad_sd_read_reg); 175 176 /** 177 * ad_sd_reset() - Reset the serial interface 178 * 179 * @sigma_delta: The sigma delta device 180 * @reset_length: Number of SCLKs with DIN = 1 181 * 182 * Returns 0 on success, an error code otherwise. 183 **/ 184 int ad_sd_reset(struct ad_sigma_delta *sigma_delta, 185 unsigned int reset_length) 186 { 187 uint8_t *buf; 188 unsigned int size; 189 int ret; 190 191 size = DIV_ROUND_UP(reset_length, 8); 192 buf = kcalloc(size, sizeof(*buf), GFP_KERNEL); 193 if (!buf) 194 return -ENOMEM; 195 196 memset(buf, 0xff, size); 197 ret = spi_write(sigma_delta->spi, buf, size); 198 kfree(buf); 199 200 return ret; 201 } 202 EXPORT_SYMBOL_GPL(ad_sd_reset); 203 204 int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta, 205 unsigned int mode, unsigned int channel) 206 { 207 int ret; 208 unsigned long timeout; 209 210 ret = ad_sigma_delta_set_channel(sigma_delta, channel); 211 if (ret) 212 return ret; 213 214 spi_bus_lock(sigma_delta->spi->master); 215 sigma_delta->bus_locked = true; 216 sigma_delta->keep_cs_asserted = true; 217 reinit_completion(&sigma_delta->completion); 218 219 ret = ad_sigma_delta_set_mode(sigma_delta, mode); 220 if (ret < 0) 221 goto out; 222 223 sigma_delta->irq_dis = false; 224 enable_irq(sigma_delta->spi->irq); 225 timeout = wait_for_completion_timeout(&sigma_delta->completion, 2 * HZ); 226 if (timeout == 0) { 227 sigma_delta->irq_dis = true; 228 disable_irq_nosync(sigma_delta->spi->irq); 229 ret = -EIO; 230 } else { 231 ret = 0; 232 } 233 out: 234 sigma_delta->keep_cs_asserted = false; 235 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE); 236 sigma_delta->bus_locked = false; 237 spi_bus_unlock(sigma_delta->spi->master); 238 239 return ret; 240 } 241 EXPORT_SYMBOL_GPL(ad_sd_calibrate); 242 243 /** 244 * ad_sd_calibrate_all() - Performs channel calibration 245 * @sigma_delta: The sigma delta device 246 * @cb: Array of channels and calibration type to perform 247 * @n: Number of items in cb 248 * 249 * Returns 0 on success, an error code otherwise. 250 **/ 251 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta, 252 const struct ad_sd_calib_data *cb, unsigned int n) 253 { 254 unsigned int i; 255 int ret; 256 257 for (i = 0; i < n; i++) { 258 ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel); 259 if (ret) 260 return ret; 261 } 262 263 return 0; 264 } 265 EXPORT_SYMBOL_GPL(ad_sd_calibrate_all); 266 267 /** 268 * ad_sigma_delta_single_conversion() - Performs a single data conversion 269 * @indio_dev: The IIO device 270 * @chan: The conversion is done for this channel 271 * @val: Pointer to the location where to store the read value 272 * 273 * Returns: 0 on success, an error value otherwise. 274 */ 275 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev, 276 const struct iio_chan_spec *chan, int *val) 277 { 278 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 279 unsigned int sample, raw_sample; 280 unsigned int data_reg; 281 int ret = 0; 282 283 if (iio_buffer_enabled(indio_dev)) 284 return -EBUSY; 285 286 mutex_lock(&indio_dev->mlock); 287 ad_sigma_delta_set_channel(sigma_delta, chan->address); 288 289 spi_bus_lock(sigma_delta->spi->master); 290 sigma_delta->bus_locked = true; 291 sigma_delta->keep_cs_asserted = true; 292 reinit_completion(&sigma_delta->completion); 293 294 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE); 295 296 sigma_delta->irq_dis = false; 297 enable_irq(sigma_delta->spi->irq); 298 ret = wait_for_completion_interruptible_timeout( 299 &sigma_delta->completion, HZ); 300 301 if (ret == 0) 302 ret = -EIO; 303 if (ret < 0) 304 goto out; 305 306 if (sigma_delta->info->data_reg != 0) 307 data_reg = sigma_delta->info->data_reg; 308 else 309 data_reg = AD_SD_REG_DATA; 310 311 ret = ad_sd_read_reg(sigma_delta, data_reg, 312 DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8), 313 &raw_sample); 314 315 out: 316 if (!sigma_delta->irq_dis) { 317 disable_irq_nosync(sigma_delta->spi->irq); 318 sigma_delta->irq_dis = true; 319 } 320 321 sigma_delta->keep_cs_asserted = false; 322 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE); 323 sigma_delta->bus_locked = false; 324 spi_bus_unlock(sigma_delta->spi->master); 325 mutex_unlock(&indio_dev->mlock); 326 327 if (ret) 328 return ret; 329 330 sample = raw_sample >> chan->scan_type.shift; 331 sample &= (1 << chan->scan_type.realbits) - 1; 332 *val = sample; 333 334 ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample); 335 if (ret) 336 return ret; 337 338 return IIO_VAL_INT; 339 } 340 EXPORT_SYMBOL_GPL(ad_sigma_delta_single_conversion); 341 342 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev) 343 { 344 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 345 unsigned int channel; 346 int ret; 347 348 ret = iio_triggered_buffer_postenable(indio_dev); 349 if (ret < 0) 350 return ret; 351 352 channel = find_first_bit(indio_dev->active_scan_mask, 353 indio_dev->masklength); 354 ret = ad_sigma_delta_set_channel(sigma_delta, 355 indio_dev->channels[channel].address); 356 if (ret) 357 return ret; 358 359 spi_bus_lock(sigma_delta->spi->master); 360 sigma_delta->bus_locked = true; 361 sigma_delta->keep_cs_asserted = true; 362 363 ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS); 364 if (ret) 365 goto err_unlock; 366 367 sigma_delta->irq_dis = false; 368 enable_irq(sigma_delta->spi->irq); 369 370 return 0; 371 372 err_unlock: 373 spi_bus_unlock(sigma_delta->spi->master); 374 375 return ret; 376 } 377 378 static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev) 379 { 380 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 381 382 reinit_completion(&sigma_delta->completion); 383 wait_for_completion_timeout(&sigma_delta->completion, HZ); 384 385 if (!sigma_delta->irq_dis) { 386 disable_irq_nosync(sigma_delta->spi->irq); 387 sigma_delta->irq_dis = true; 388 } 389 390 sigma_delta->keep_cs_asserted = false; 391 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE); 392 393 sigma_delta->bus_locked = false; 394 return spi_bus_unlock(sigma_delta->spi->master); 395 } 396 397 static irqreturn_t ad_sd_trigger_handler(int irq, void *p) 398 { 399 struct iio_poll_func *pf = p; 400 struct iio_dev *indio_dev = pf->indio_dev; 401 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 402 unsigned int reg_size; 403 unsigned int data_reg; 404 uint8_t data[16]; 405 int ret; 406 407 memset(data, 0x00, 16); 408 409 reg_size = indio_dev->channels[0].scan_type.realbits + 410 indio_dev->channels[0].scan_type.shift; 411 reg_size = DIV_ROUND_UP(reg_size, 8); 412 413 if (sigma_delta->info->data_reg != 0) 414 data_reg = sigma_delta->info->data_reg; 415 else 416 data_reg = AD_SD_REG_DATA; 417 418 switch (reg_size) { 419 case 4: 420 case 2: 421 case 1: 422 ret = ad_sd_read_reg_raw(sigma_delta, data_reg, reg_size, 423 &data[0]); 424 break; 425 case 3: 426 /* We store 24 bit samples in a 32 bit word. Keep the upper 427 * byte set to zero. */ 428 ret = ad_sd_read_reg_raw(sigma_delta, data_reg, reg_size, 429 &data[1]); 430 break; 431 } 432 433 iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp); 434 435 iio_trigger_notify_done(indio_dev->trig); 436 sigma_delta->irq_dis = false; 437 enable_irq(sigma_delta->spi->irq); 438 439 return IRQ_HANDLED; 440 } 441 442 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = { 443 .postenable = &ad_sd_buffer_postenable, 444 .predisable = &iio_triggered_buffer_predisable, 445 .postdisable = &ad_sd_buffer_postdisable, 446 .validate_scan_mask = &iio_validate_scan_mask_onehot, 447 }; 448 449 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private) 450 { 451 struct ad_sigma_delta *sigma_delta = private; 452 453 complete(&sigma_delta->completion); 454 disable_irq_nosync(irq); 455 sigma_delta->irq_dis = true; 456 iio_trigger_poll(sigma_delta->trig); 457 458 return IRQ_HANDLED; 459 } 460 461 /** 462 * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices 463 * @indio_dev: The IIO device 464 * @trig: The new trigger 465 * 466 * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta 467 * device, -EINVAL otherwise. 468 */ 469 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig) 470 { 471 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 472 473 if (sigma_delta->trig != trig) 474 return -EINVAL; 475 476 return 0; 477 } 478 EXPORT_SYMBOL_GPL(ad_sd_validate_trigger); 479 480 static const struct iio_trigger_ops ad_sd_trigger_ops = { 481 }; 482 483 static int ad_sd_probe_trigger(struct iio_dev *indio_dev) 484 { 485 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 486 int ret; 487 488 sigma_delta->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name, 489 indio_dev->id); 490 if (sigma_delta->trig == NULL) { 491 ret = -ENOMEM; 492 goto error_ret; 493 } 494 sigma_delta->trig->ops = &ad_sd_trigger_ops; 495 init_completion(&sigma_delta->completion); 496 497 ret = request_irq(sigma_delta->spi->irq, 498 ad_sd_data_rdy_trig_poll, 499 sigma_delta->info->irq_flags, 500 indio_dev->name, 501 sigma_delta); 502 if (ret) 503 goto error_free_trig; 504 505 if (!sigma_delta->irq_dis) { 506 sigma_delta->irq_dis = true; 507 disable_irq_nosync(sigma_delta->spi->irq); 508 } 509 sigma_delta->trig->dev.parent = &sigma_delta->spi->dev; 510 iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta); 511 512 ret = iio_trigger_register(sigma_delta->trig); 513 if (ret) 514 goto error_free_irq; 515 516 /* select default trigger */ 517 indio_dev->trig = iio_trigger_get(sigma_delta->trig); 518 519 return 0; 520 521 error_free_irq: 522 free_irq(sigma_delta->spi->irq, sigma_delta); 523 error_free_trig: 524 iio_trigger_free(sigma_delta->trig); 525 error_ret: 526 return ret; 527 } 528 529 static void ad_sd_remove_trigger(struct iio_dev *indio_dev) 530 { 531 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 532 533 iio_trigger_unregister(sigma_delta->trig); 534 free_irq(sigma_delta->spi->irq, sigma_delta); 535 iio_trigger_free(sigma_delta->trig); 536 } 537 538 /** 539 * ad_sd_setup_buffer_and_trigger() - 540 * @indio_dev: The IIO device 541 */ 542 int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev) 543 { 544 int ret; 545 546 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 547 &ad_sd_trigger_handler, &ad_sd_buffer_setup_ops); 548 if (ret) 549 return ret; 550 551 ret = ad_sd_probe_trigger(indio_dev); 552 if (ret) { 553 iio_triggered_buffer_cleanup(indio_dev); 554 return ret; 555 } 556 557 return 0; 558 } 559 EXPORT_SYMBOL_GPL(ad_sd_setup_buffer_and_trigger); 560 561 /** 562 * ad_sd_cleanup_buffer_and_trigger() - 563 * @indio_dev: The IIO device 564 */ 565 void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev) 566 { 567 ad_sd_remove_trigger(indio_dev); 568 iio_triggered_buffer_cleanup(indio_dev); 569 } 570 EXPORT_SYMBOL_GPL(ad_sd_cleanup_buffer_and_trigger); 571 572 /** 573 * ad_sd_init() - Initializes a ad_sigma_delta struct 574 * @sigma_delta: The ad_sigma_delta device 575 * @indio_dev: The IIO device which the Sigma Delta device is used for 576 * @spi: The SPI device for the ad_sigma_delta device 577 * @info: Device specific callbacks and options 578 * 579 * This function needs to be called before any other operations are performed on 580 * the ad_sigma_delta struct. 581 */ 582 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev, 583 struct spi_device *spi, const struct ad_sigma_delta_info *info) 584 { 585 sigma_delta->spi = spi; 586 sigma_delta->info = info; 587 iio_device_set_drvdata(indio_dev, sigma_delta); 588 589 return 0; 590 } 591 EXPORT_SYMBOL_GPL(ad_sd_init); 592 593 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 594 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs"); 595 MODULE_LICENSE("GPL v2"); 596