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