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