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->bus_locked, 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 reinit_completion(&sigma_delta->completion); 222 223 ret = ad_sigma_delta_set_mode(sigma_delta, mode); 224 if (ret < 0) 225 goto out; 226 227 sigma_delta->irq_dis = false; 228 enable_irq(sigma_delta->spi->irq); 229 timeout = wait_for_completion_timeout(&sigma_delta->completion, 2 * HZ); 230 if (timeout == 0) { 231 sigma_delta->irq_dis = true; 232 disable_irq_nosync(sigma_delta->spi->irq); 233 ret = -EIO; 234 } else { 235 ret = 0; 236 } 237 out: 238 sigma_delta->bus_locked = false; 239 spi_bus_unlock(sigma_delta->spi->master); 240 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE); 241 242 return ret; 243 } 244 245 /** 246 * ad_sd_calibrate_all() - Performs channel calibration 247 * @sigma_delta: The sigma delta device 248 * @cb: Array of channels and calibration type to perform 249 * @n: Number of items in cb 250 * 251 * Returns 0 on success, an error code otherwise. 252 **/ 253 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta, 254 const struct ad_sd_calib_data *cb, unsigned int n) 255 { 256 unsigned int i; 257 int ret; 258 259 for (i = 0; i < n; i++) { 260 ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel); 261 if (ret) 262 return ret; 263 } 264 265 return 0; 266 } 267 EXPORT_SYMBOL_GPL(ad_sd_calibrate_all); 268 269 /** 270 * ad_sigma_delta_single_conversion() - Performs a single data conversion 271 * @indio_dev: The IIO device 272 * @chan: The conversion is done for this channel 273 * @val: Pointer to the location where to store the read value 274 * 275 * Returns: 0 on success, an error value otherwise. 276 */ 277 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev, 278 const struct iio_chan_spec *chan, int *val) 279 { 280 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 281 unsigned int sample, raw_sample; 282 unsigned int data_reg; 283 int ret = 0; 284 285 if (iio_buffer_enabled(indio_dev)) 286 return -EBUSY; 287 288 mutex_lock(&indio_dev->mlock); 289 ad_sigma_delta_set_channel(sigma_delta, chan->address); 290 291 spi_bus_lock(sigma_delta->spi->master); 292 sigma_delta->bus_locked = true; 293 reinit_completion(&sigma_delta->completion); 294 295 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE); 296 297 sigma_delta->irq_dis = false; 298 enable_irq(sigma_delta->spi->irq); 299 ret = wait_for_completion_interruptible_timeout( 300 &sigma_delta->completion, HZ); 301 302 sigma_delta->bus_locked = false; 303 spi_bus_unlock(sigma_delta->spi->master); 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 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE); 326 mutex_unlock(&indio_dev->mlock); 327 328 if (ret) 329 return ret; 330 331 sample = raw_sample >> chan->scan_type.shift; 332 sample &= (1 << chan->scan_type.realbits) - 1; 333 *val = sample; 334 335 ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample); 336 if (ret) 337 return ret; 338 339 return IIO_VAL_INT; 340 } 341 EXPORT_SYMBOL_GPL(ad_sigma_delta_single_conversion); 342 343 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev) 344 { 345 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 346 unsigned int channel; 347 int ret; 348 349 ret = iio_triggered_buffer_postenable(indio_dev); 350 if (ret < 0) 351 return ret; 352 353 channel = find_first_bit(indio_dev->active_scan_mask, 354 indio_dev->masklength); 355 ret = ad_sigma_delta_set_channel(sigma_delta, 356 indio_dev->channels[channel].address); 357 if (ret) 358 goto err_predisable; 359 360 spi_bus_lock(sigma_delta->spi->master); 361 sigma_delta->bus_locked = true; 362 ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS); 363 if (ret) 364 goto err_unlock; 365 366 sigma_delta->irq_dis = false; 367 enable_irq(sigma_delta->spi->irq); 368 369 return 0; 370 371 err_unlock: 372 spi_bus_unlock(sigma_delta->spi->master); 373 err_predisable: 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 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE); 391 392 sigma_delta->bus_locked = false; 393 return spi_bus_unlock(sigma_delta->spi->master); 394 } 395 396 static irqreturn_t ad_sd_trigger_handler(int irq, void *p) 397 { 398 struct iio_poll_func *pf = p; 399 struct iio_dev *indio_dev = pf->indio_dev; 400 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 401 unsigned int reg_size; 402 unsigned int data_reg; 403 uint8_t data[16]; 404 int ret; 405 406 memset(data, 0x00, 16); 407 408 reg_size = indio_dev->channels[0].scan_type.realbits + 409 indio_dev->channels[0].scan_type.shift; 410 reg_size = DIV_ROUND_UP(reg_size, 8); 411 412 if (sigma_delta->info->data_reg != 0) 413 data_reg = sigma_delta->info->data_reg; 414 else 415 data_reg = AD_SD_REG_DATA; 416 417 switch (reg_size) { 418 case 4: 419 case 2: 420 case 1: 421 ret = ad_sd_read_reg_raw(sigma_delta, data_reg, reg_size, 422 &data[0]); 423 break; 424 case 3: 425 /* We store 24 bit samples in a 32 bit word. Keep the upper 426 * byte set to zero. */ 427 ret = ad_sd_read_reg_raw(sigma_delta, data_reg, reg_size, 428 &data[1]); 429 break; 430 } 431 432 iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp); 433 434 iio_trigger_notify_done(indio_dev->trig); 435 sigma_delta->irq_dis = false; 436 enable_irq(sigma_delta->spi->irq); 437 438 return IRQ_HANDLED; 439 } 440 441 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = { 442 .postenable = &ad_sd_buffer_postenable, 443 .predisable = &iio_triggered_buffer_predisable, 444 .postdisable = &ad_sd_buffer_postdisable, 445 .validate_scan_mask = &iio_validate_scan_mask_onehot, 446 }; 447 448 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private) 449 { 450 struct ad_sigma_delta *sigma_delta = private; 451 452 complete(&sigma_delta->completion); 453 disable_irq_nosync(irq); 454 sigma_delta->irq_dis = true; 455 iio_trigger_poll(sigma_delta->trig); 456 457 return IRQ_HANDLED; 458 } 459 460 /** 461 * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices 462 * @indio_dev: The IIO device 463 * @trig: The new trigger 464 * 465 * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta 466 * device, -EINVAL otherwise. 467 */ 468 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig) 469 { 470 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 471 472 if (sigma_delta->trig != trig) 473 return -EINVAL; 474 475 return 0; 476 } 477 EXPORT_SYMBOL_GPL(ad_sd_validate_trigger); 478 479 static const struct iio_trigger_ops ad_sd_trigger_ops = { 480 }; 481 482 static int ad_sd_probe_trigger(struct iio_dev *indio_dev) 483 { 484 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 485 int ret; 486 487 sigma_delta->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name, 488 indio_dev->id); 489 if (sigma_delta->trig == NULL) { 490 ret = -ENOMEM; 491 goto error_ret; 492 } 493 sigma_delta->trig->ops = &ad_sd_trigger_ops; 494 init_completion(&sigma_delta->completion); 495 496 ret = request_irq(sigma_delta->spi->irq, 497 ad_sd_data_rdy_trig_poll, 498 IRQF_TRIGGER_LOW, 499 indio_dev->name, 500 sigma_delta); 501 if (ret) 502 goto error_free_trig; 503 504 if (!sigma_delta->irq_dis) { 505 sigma_delta->irq_dis = true; 506 disable_irq_nosync(sigma_delta->spi->irq); 507 } 508 sigma_delta->trig->dev.parent = &sigma_delta->spi->dev; 509 iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta); 510 511 ret = iio_trigger_register(sigma_delta->trig); 512 if (ret) 513 goto error_free_irq; 514 515 /* select default trigger */ 516 indio_dev->trig = iio_trigger_get(sigma_delta->trig); 517 518 return 0; 519 520 error_free_irq: 521 free_irq(sigma_delta->spi->irq, sigma_delta); 522 error_free_trig: 523 iio_trigger_free(sigma_delta->trig); 524 error_ret: 525 return ret; 526 } 527 528 static void ad_sd_remove_trigger(struct iio_dev *indio_dev) 529 { 530 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 531 532 iio_trigger_unregister(sigma_delta->trig); 533 free_irq(sigma_delta->spi->irq, sigma_delta); 534 iio_trigger_free(sigma_delta->trig); 535 } 536 537 /** 538 * ad_sd_setup_buffer_and_trigger() - 539 * @indio_dev: The IIO device 540 */ 541 int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev) 542 { 543 int ret; 544 545 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 546 &ad_sd_trigger_handler, &ad_sd_buffer_setup_ops); 547 if (ret) 548 return ret; 549 550 ret = ad_sd_probe_trigger(indio_dev); 551 if (ret) { 552 iio_triggered_buffer_cleanup(indio_dev); 553 return ret; 554 } 555 556 return 0; 557 } 558 EXPORT_SYMBOL_GPL(ad_sd_setup_buffer_and_trigger); 559 560 /** 561 * ad_sd_cleanup_buffer_and_trigger() - 562 * @indio_dev: The IIO device 563 */ 564 void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev) 565 { 566 ad_sd_remove_trigger(indio_dev); 567 iio_triggered_buffer_cleanup(indio_dev); 568 } 569 EXPORT_SYMBOL_GPL(ad_sd_cleanup_buffer_and_trigger); 570 571 /** 572 * ad_sd_init() - Initializes a ad_sigma_delta struct 573 * @sigma_delta: The ad_sigma_delta device 574 * @indio_dev: The IIO device which the Sigma Delta device is used for 575 * @spi: The SPI device for the ad_sigma_delta device 576 * @info: Device specific callbacks and options 577 * 578 * This function needs to be called before any other operations are performed on 579 * the ad_sigma_delta struct. 580 */ 581 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev, 582 struct spi_device *spi, const struct ad_sigma_delta_info *info) 583 { 584 sigma_delta->spi = spi; 585 sigma_delta->info = info; 586 iio_device_set_drvdata(indio_dev, sigma_delta); 587 588 return 0; 589 } 590 EXPORT_SYMBOL_GPL(ad_sd_init); 591 592 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 593 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs"); 594 MODULE_LICENSE("GPL v2"); 595