1*4eb3ccf1SLars-Peter Clausen /* 2*4eb3ccf1SLars-Peter Clausen * AD7887 SPI ADC driver 3*4eb3ccf1SLars-Peter Clausen * 4*4eb3ccf1SLars-Peter Clausen * Copyright 2010-2011 Analog Devices Inc. 5*4eb3ccf1SLars-Peter Clausen * 6*4eb3ccf1SLars-Peter Clausen * Licensed under the GPL-2. 7*4eb3ccf1SLars-Peter Clausen */ 8*4eb3ccf1SLars-Peter Clausen 9*4eb3ccf1SLars-Peter Clausen #include <linux/device.h> 10*4eb3ccf1SLars-Peter Clausen #include <linux/kernel.h> 11*4eb3ccf1SLars-Peter Clausen #include <linux/slab.h> 12*4eb3ccf1SLars-Peter Clausen #include <linux/sysfs.h> 13*4eb3ccf1SLars-Peter Clausen #include <linux/spi/spi.h> 14*4eb3ccf1SLars-Peter Clausen #include <linux/regulator/consumer.h> 15*4eb3ccf1SLars-Peter Clausen #include <linux/err.h> 16*4eb3ccf1SLars-Peter Clausen #include <linux/module.h> 17*4eb3ccf1SLars-Peter Clausen #include <linux/interrupt.h> 18*4eb3ccf1SLars-Peter Clausen 19*4eb3ccf1SLars-Peter Clausen #include <linux/iio/iio.h> 20*4eb3ccf1SLars-Peter Clausen #include <linux/iio/sysfs.h> 21*4eb3ccf1SLars-Peter Clausen #include <linux/iio/buffer.h> 22*4eb3ccf1SLars-Peter Clausen 23*4eb3ccf1SLars-Peter Clausen #include <linux/iio/trigger_consumer.h> 24*4eb3ccf1SLars-Peter Clausen #include <linux/iio/triggered_buffer.h> 25*4eb3ccf1SLars-Peter Clausen 26*4eb3ccf1SLars-Peter Clausen #include <linux/platform_data/ad7887.h> 27*4eb3ccf1SLars-Peter Clausen 28*4eb3ccf1SLars-Peter Clausen #define AD7887_REF_DIS (1 << 5) /* on-chip reference disable */ 29*4eb3ccf1SLars-Peter Clausen #define AD7887_DUAL (1 << 4) /* dual-channel mode */ 30*4eb3ccf1SLars-Peter Clausen #define AD7887_CH_AIN1 (1 << 3) /* convert on channel 1, DUAL=1 */ 31*4eb3ccf1SLars-Peter Clausen #define AD7887_CH_AIN0 (0 << 3) /* convert on channel 0, DUAL=0,1 */ 32*4eb3ccf1SLars-Peter Clausen #define AD7887_PM_MODE1 (0) /* CS based shutdown */ 33*4eb3ccf1SLars-Peter Clausen #define AD7887_PM_MODE2 (1) /* full on */ 34*4eb3ccf1SLars-Peter Clausen #define AD7887_PM_MODE3 (2) /* auto shutdown after conversion */ 35*4eb3ccf1SLars-Peter Clausen #define AD7887_PM_MODE4 (3) /* standby mode */ 36*4eb3ccf1SLars-Peter Clausen 37*4eb3ccf1SLars-Peter Clausen enum ad7887_channels { 38*4eb3ccf1SLars-Peter Clausen AD7887_CH0, 39*4eb3ccf1SLars-Peter Clausen AD7887_CH0_CH1, 40*4eb3ccf1SLars-Peter Clausen AD7887_CH1, 41*4eb3ccf1SLars-Peter Clausen }; 42*4eb3ccf1SLars-Peter Clausen 43*4eb3ccf1SLars-Peter Clausen #define RES_MASK(bits) ((1 << (bits)) - 1) 44*4eb3ccf1SLars-Peter Clausen 45*4eb3ccf1SLars-Peter Clausen /** 46*4eb3ccf1SLars-Peter Clausen * struct ad7887_chip_info - chip specifc information 47*4eb3ccf1SLars-Peter Clausen * @int_vref_mv: the internal reference voltage 48*4eb3ccf1SLars-Peter Clausen * @channel: channel specification 49*4eb3ccf1SLars-Peter Clausen */ 50*4eb3ccf1SLars-Peter Clausen struct ad7887_chip_info { 51*4eb3ccf1SLars-Peter Clausen u16 int_vref_mv; 52*4eb3ccf1SLars-Peter Clausen struct iio_chan_spec channel[3]; 53*4eb3ccf1SLars-Peter Clausen }; 54*4eb3ccf1SLars-Peter Clausen 55*4eb3ccf1SLars-Peter Clausen struct ad7887_state { 56*4eb3ccf1SLars-Peter Clausen struct spi_device *spi; 57*4eb3ccf1SLars-Peter Clausen const struct ad7887_chip_info *chip_info; 58*4eb3ccf1SLars-Peter Clausen struct regulator *reg; 59*4eb3ccf1SLars-Peter Clausen struct spi_transfer xfer[4]; 60*4eb3ccf1SLars-Peter Clausen struct spi_message msg[3]; 61*4eb3ccf1SLars-Peter Clausen struct spi_message *ring_msg; 62*4eb3ccf1SLars-Peter Clausen unsigned char tx_cmd_buf[4]; 63*4eb3ccf1SLars-Peter Clausen 64*4eb3ccf1SLars-Peter Clausen /* 65*4eb3ccf1SLars-Peter Clausen * DMA (thus cache coherency maintenance) requires the 66*4eb3ccf1SLars-Peter Clausen * transfer buffers to live in their own cache lines. 67*4eb3ccf1SLars-Peter Clausen * Buffer needs to be large enough to hold two 16 bit samples and a 68*4eb3ccf1SLars-Peter Clausen * 64 bit aligned 64 bit timestamp. 69*4eb3ccf1SLars-Peter Clausen */ 70*4eb3ccf1SLars-Peter Clausen unsigned char data[ALIGN(4, sizeof(s64)) + sizeof(s64)] 71*4eb3ccf1SLars-Peter Clausen ____cacheline_aligned; 72*4eb3ccf1SLars-Peter Clausen }; 73*4eb3ccf1SLars-Peter Clausen 74*4eb3ccf1SLars-Peter Clausen enum ad7887_supported_device_ids { 75*4eb3ccf1SLars-Peter Clausen ID_AD7887 76*4eb3ccf1SLars-Peter Clausen }; 77*4eb3ccf1SLars-Peter Clausen 78*4eb3ccf1SLars-Peter Clausen static int ad7887_ring_preenable(struct iio_dev *indio_dev) 79*4eb3ccf1SLars-Peter Clausen { 80*4eb3ccf1SLars-Peter Clausen struct ad7887_state *st = iio_priv(indio_dev); 81*4eb3ccf1SLars-Peter Clausen int ret; 82*4eb3ccf1SLars-Peter Clausen 83*4eb3ccf1SLars-Peter Clausen ret = iio_sw_buffer_preenable(indio_dev); 84*4eb3ccf1SLars-Peter Clausen if (ret < 0) 85*4eb3ccf1SLars-Peter Clausen return ret; 86*4eb3ccf1SLars-Peter Clausen 87*4eb3ccf1SLars-Peter Clausen /* We know this is a single long so can 'cheat' */ 88*4eb3ccf1SLars-Peter Clausen switch (*indio_dev->active_scan_mask) { 89*4eb3ccf1SLars-Peter Clausen case (1 << 0): 90*4eb3ccf1SLars-Peter Clausen st->ring_msg = &st->msg[AD7887_CH0]; 91*4eb3ccf1SLars-Peter Clausen break; 92*4eb3ccf1SLars-Peter Clausen case (1 << 1): 93*4eb3ccf1SLars-Peter Clausen st->ring_msg = &st->msg[AD7887_CH1]; 94*4eb3ccf1SLars-Peter Clausen /* Dummy read: push CH1 setting down to hardware */ 95*4eb3ccf1SLars-Peter Clausen spi_sync(st->spi, st->ring_msg); 96*4eb3ccf1SLars-Peter Clausen break; 97*4eb3ccf1SLars-Peter Clausen case ((1 << 1) | (1 << 0)): 98*4eb3ccf1SLars-Peter Clausen st->ring_msg = &st->msg[AD7887_CH0_CH1]; 99*4eb3ccf1SLars-Peter Clausen break; 100*4eb3ccf1SLars-Peter Clausen } 101*4eb3ccf1SLars-Peter Clausen 102*4eb3ccf1SLars-Peter Clausen return 0; 103*4eb3ccf1SLars-Peter Clausen } 104*4eb3ccf1SLars-Peter Clausen 105*4eb3ccf1SLars-Peter Clausen static int ad7887_ring_postdisable(struct iio_dev *indio_dev) 106*4eb3ccf1SLars-Peter Clausen { 107*4eb3ccf1SLars-Peter Clausen struct ad7887_state *st = iio_priv(indio_dev); 108*4eb3ccf1SLars-Peter Clausen 109*4eb3ccf1SLars-Peter Clausen /* dummy read: restore default CH0 settin */ 110*4eb3ccf1SLars-Peter Clausen return spi_sync(st->spi, &st->msg[AD7887_CH0]); 111*4eb3ccf1SLars-Peter Clausen } 112*4eb3ccf1SLars-Peter Clausen 113*4eb3ccf1SLars-Peter Clausen /** 114*4eb3ccf1SLars-Peter Clausen * ad7887_trigger_handler() bh of trigger launched polling to ring buffer 115*4eb3ccf1SLars-Peter Clausen * 116*4eb3ccf1SLars-Peter Clausen * Currently there is no option in this driver to disable the saving of 117*4eb3ccf1SLars-Peter Clausen * timestamps within the ring. 118*4eb3ccf1SLars-Peter Clausen **/ 119*4eb3ccf1SLars-Peter Clausen static irqreturn_t ad7887_trigger_handler(int irq, void *p) 120*4eb3ccf1SLars-Peter Clausen { 121*4eb3ccf1SLars-Peter Clausen struct iio_poll_func *pf = p; 122*4eb3ccf1SLars-Peter Clausen struct iio_dev *indio_dev = pf->indio_dev; 123*4eb3ccf1SLars-Peter Clausen struct ad7887_state *st = iio_priv(indio_dev); 124*4eb3ccf1SLars-Peter Clausen s64 time_ns; 125*4eb3ccf1SLars-Peter Clausen int b_sent; 126*4eb3ccf1SLars-Peter Clausen 127*4eb3ccf1SLars-Peter Clausen b_sent = spi_sync(st->spi, st->ring_msg); 128*4eb3ccf1SLars-Peter Clausen if (b_sent) 129*4eb3ccf1SLars-Peter Clausen goto done; 130*4eb3ccf1SLars-Peter Clausen 131*4eb3ccf1SLars-Peter Clausen time_ns = iio_get_time_ns(); 132*4eb3ccf1SLars-Peter Clausen 133*4eb3ccf1SLars-Peter Clausen if (indio_dev->scan_timestamp) 134*4eb3ccf1SLars-Peter Clausen memcpy(st->data + indio_dev->scan_bytes - sizeof(s64), 135*4eb3ccf1SLars-Peter Clausen &time_ns, sizeof(time_ns)); 136*4eb3ccf1SLars-Peter Clausen 137*4eb3ccf1SLars-Peter Clausen iio_push_to_buffer(indio_dev->buffer, st->data); 138*4eb3ccf1SLars-Peter Clausen done: 139*4eb3ccf1SLars-Peter Clausen iio_trigger_notify_done(indio_dev->trig); 140*4eb3ccf1SLars-Peter Clausen 141*4eb3ccf1SLars-Peter Clausen return IRQ_HANDLED; 142*4eb3ccf1SLars-Peter Clausen } 143*4eb3ccf1SLars-Peter Clausen 144*4eb3ccf1SLars-Peter Clausen static const struct iio_buffer_setup_ops ad7887_ring_setup_ops = { 145*4eb3ccf1SLars-Peter Clausen .preenable = &ad7887_ring_preenable, 146*4eb3ccf1SLars-Peter Clausen .postenable = &iio_triggered_buffer_postenable, 147*4eb3ccf1SLars-Peter Clausen .predisable = &iio_triggered_buffer_predisable, 148*4eb3ccf1SLars-Peter Clausen .postdisable = &ad7887_ring_postdisable, 149*4eb3ccf1SLars-Peter Clausen }; 150*4eb3ccf1SLars-Peter Clausen 151*4eb3ccf1SLars-Peter Clausen static int ad7887_scan_direct(struct ad7887_state *st, unsigned ch) 152*4eb3ccf1SLars-Peter Clausen { 153*4eb3ccf1SLars-Peter Clausen int ret = spi_sync(st->spi, &st->msg[ch]); 154*4eb3ccf1SLars-Peter Clausen if (ret) 155*4eb3ccf1SLars-Peter Clausen return ret; 156*4eb3ccf1SLars-Peter Clausen 157*4eb3ccf1SLars-Peter Clausen return (st->data[(ch * 2)] << 8) | st->data[(ch * 2) + 1]; 158*4eb3ccf1SLars-Peter Clausen } 159*4eb3ccf1SLars-Peter Clausen 160*4eb3ccf1SLars-Peter Clausen static int ad7887_read_raw(struct iio_dev *indio_dev, 161*4eb3ccf1SLars-Peter Clausen struct iio_chan_spec const *chan, 162*4eb3ccf1SLars-Peter Clausen int *val, 163*4eb3ccf1SLars-Peter Clausen int *val2, 164*4eb3ccf1SLars-Peter Clausen long m) 165*4eb3ccf1SLars-Peter Clausen { 166*4eb3ccf1SLars-Peter Clausen int ret; 167*4eb3ccf1SLars-Peter Clausen struct ad7887_state *st = iio_priv(indio_dev); 168*4eb3ccf1SLars-Peter Clausen 169*4eb3ccf1SLars-Peter Clausen switch (m) { 170*4eb3ccf1SLars-Peter Clausen case IIO_CHAN_INFO_RAW: 171*4eb3ccf1SLars-Peter Clausen mutex_lock(&indio_dev->mlock); 172*4eb3ccf1SLars-Peter Clausen if (iio_buffer_enabled(indio_dev)) 173*4eb3ccf1SLars-Peter Clausen ret = -EBUSY; 174*4eb3ccf1SLars-Peter Clausen else 175*4eb3ccf1SLars-Peter Clausen ret = ad7887_scan_direct(st, chan->address); 176*4eb3ccf1SLars-Peter Clausen mutex_unlock(&indio_dev->mlock); 177*4eb3ccf1SLars-Peter Clausen 178*4eb3ccf1SLars-Peter Clausen if (ret < 0) 179*4eb3ccf1SLars-Peter Clausen return ret; 180*4eb3ccf1SLars-Peter Clausen *val = ret >> chan->scan_type.shift; 181*4eb3ccf1SLars-Peter Clausen *val &= RES_MASK(chan->scan_type.realbits); 182*4eb3ccf1SLars-Peter Clausen return IIO_VAL_INT; 183*4eb3ccf1SLars-Peter Clausen case IIO_CHAN_INFO_SCALE: 184*4eb3ccf1SLars-Peter Clausen if (st->reg) { 185*4eb3ccf1SLars-Peter Clausen *val = regulator_get_voltage(st->reg); 186*4eb3ccf1SLars-Peter Clausen if (*val < 0) 187*4eb3ccf1SLars-Peter Clausen return *val; 188*4eb3ccf1SLars-Peter Clausen *val /= 1000; 189*4eb3ccf1SLars-Peter Clausen } else { 190*4eb3ccf1SLars-Peter Clausen *val = st->chip_info->int_vref_mv; 191*4eb3ccf1SLars-Peter Clausen } 192*4eb3ccf1SLars-Peter Clausen 193*4eb3ccf1SLars-Peter Clausen *val2 = chan->scan_type.realbits; 194*4eb3ccf1SLars-Peter Clausen 195*4eb3ccf1SLars-Peter Clausen return IIO_VAL_FRACTIONAL_LOG2; 196*4eb3ccf1SLars-Peter Clausen } 197*4eb3ccf1SLars-Peter Clausen return -EINVAL; 198*4eb3ccf1SLars-Peter Clausen } 199*4eb3ccf1SLars-Peter Clausen 200*4eb3ccf1SLars-Peter Clausen 201*4eb3ccf1SLars-Peter Clausen static const struct ad7887_chip_info ad7887_chip_info_tbl[] = { 202*4eb3ccf1SLars-Peter Clausen /* 203*4eb3ccf1SLars-Peter Clausen * More devices added in future 204*4eb3ccf1SLars-Peter Clausen */ 205*4eb3ccf1SLars-Peter Clausen [ID_AD7887] = { 206*4eb3ccf1SLars-Peter Clausen .channel[0] = { 207*4eb3ccf1SLars-Peter Clausen .type = IIO_VOLTAGE, 208*4eb3ccf1SLars-Peter Clausen .indexed = 1, 209*4eb3ccf1SLars-Peter Clausen .channel = 1, 210*4eb3ccf1SLars-Peter Clausen .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | 211*4eb3ccf1SLars-Peter Clausen IIO_CHAN_INFO_SCALE_SHARED_BIT, 212*4eb3ccf1SLars-Peter Clausen .address = 1, 213*4eb3ccf1SLars-Peter Clausen .scan_index = 1, 214*4eb3ccf1SLars-Peter Clausen .scan_type = IIO_ST('u', 12, 16, 0), 215*4eb3ccf1SLars-Peter Clausen }, 216*4eb3ccf1SLars-Peter Clausen .channel[1] = { 217*4eb3ccf1SLars-Peter Clausen .type = IIO_VOLTAGE, 218*4eb3ccf1SLars-Peter Clausen .indexed = 1, 219*4eb3ccf1SLars-Peter Clausen .channel = 0, 220*4eb3ccf1SLars-Peter Clausen .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | 221*4eb3ccf1SLars-Peter Clausen IIO_CHAN_INFO_SCALE_SHARED_BIT, 222*4eb3ccf1SLars-Peter Clausen .address = 0, 223*4eb3ccf1SLars-Peter Clausen .scan_index = 0, 224*4eb3ccf1SLars-Peter Clausen .scan_type = IIO_ST('u', 12, 16, 0), 225*4eb3ccf1SLars-Peter Clausen }, 226*4eb3ccf1SLars-Peter Clausen .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2), 227*4eb3ccf1SLars-Peter Clausen .int_vref_mv = 2500, 228*4eb3ccf1SLars-Peter Clausen }, 229*4eb3ccf1SLars-Peter Clausen }; 230*4eb3ccf1SLars-Peter Clausen 231*4eb3ccf1SLars-Peter Clausen static const struct iio_info ad7887_info = { 232*4eb3ccf1SLars-Peter Clausen .read_raw = &ad7887_read_raw, 233*4eb3ccf1SLars-Peter Clausen .driver_module = THIS_MODULE, 234*4eb3ccf1SLars-Peter Clausen }; 235*4eb3ccf1SLars-Peter Clausen 236*4eb3ccf1SLars-Peter Clausen static int __devinit ad7887_probe(struct spi_device *spi) 237*4eb3ccf1SLars-Peter Clausen { 238*4eb3ccf1SLars-Peter Clausen struct ad7887_platform_data *pdata = spi->dev.platform_data; 239*4eb3ccf1SLars-Peter Clausen struct ad7887_state *st; 240*4eb3ccf1SLars-Peter Clausen struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st)); 241*4eb3ccf1SLars-Peter Clausen uint8_t mode; 242*4eb3ccf1SLars-Peter Clausen int ret; 243*4eb3ccf1SLars-Peter Clausen 244*4eb3ccf1SLars-Peter Clausen if (indio_dev == NULL) 245*4eb3ccf1SLars-Peter Clausen return -ENOMEM; 246*4eb3ccf1SLars-Peter Clausen 247*4eb3ccf1SLars-Peter Clausen st = iio_priv(indio_dev); 248*4eb3ccf1SLars-Peter Clausen 249*4eb3ccf1SLars-Peter Clausen if (!pdata || !pdata->use_onchip_ref) { 250*4eb3ccf1SLars-Peter Clausen st->reg = regulator_get(&spi->dev, "vref"); 251*4eb3ccf1SLars-Peter Clausen if (IS_ERR(st->reg)) { 252*4eb3ccf1SLars-Peter Clausen ret = PTR_ERR(st->reg); 253*4eb3ccf1SLars-Peter Clausen goto error_free; 254*4eb3ccf1SLars-Peter Clausen } 255*4eb3ccf1SLars-Peter Clausen 256*4eb3ccf1SLars-Peter Clausen ret = regulator_enable(st->reg); 257*4eb3ccf1SLars-Peter Clausen if (ret) 258*4eb3ccf1SLars-Peter Clausen goto error_put_reg; 259*4eb3ccf1SLars-Peter Clausen } 260*4eb3ccf1SLars-Peter Clausen 261*4eb3ccf1SLars-Peter Clausen st->chip_info = 262*4eb3ccf1SLars-Peter Clausen &ad7887_chip_info_tbl[spi_get_device_id(spi)->driver_data]; 263*4eb3ccf1SLars-Peter Clausen 264*4eb3ccf1SLars-Peter Clausen spi_set_drvdata(spi, indio_dev); 265*4eb3ccf1SLars-Peter Clausen st->spi = spi; 266*4eb3ccf1SLars-Peter Clausen 267*4eb3ccf1SLars-Peter Clausen /* Estabilish that the iio_dev is a child of the spi device */ 268*4eb3ccf1SLars-Peter Clausen indio_dev->dev.parent = &spi->dev; 269*4eb3ccf1SLars-Peter Clausen indio_dev->name = spi_get_device_id(spi)->name; 270*4eb3ccf1SLars-Peter Clausen indio_dev->info = &ad7887_info; 271*4eb3ccf1SLars-Peter Clausen indio_dev->modes = INDIO_DIRECT_MODE; 272*4eb3ccf1SLars-Peter Clausen 273*4eb3ccf1SLars-Peter Clausen /* Setup default message */ 274*4eb3ccf1SLars-Peter Clausen 275*4eb3ccf1SLars-Peter Clausen mode = AD7887_PM_MODE4; 276*4eb3ccf1SLars-Peter Clausen if (!pdata || !pdata->use_onchip_ref) 277*4eb3ccf1SLars-Peter Clausen mode |= AD7887_REF_DIS; 278*4eb3ccf1SLars-Peter Clausen if (pdata && pdata->en_dual) 279*4eb3ccf1SLars-Peter Clausen mode |= AD7887_DUAL; 280*4eb3ccf1SLars-Peter Clausen 281*4eb3ccf1SLars-Peter Clausen st->tx_cmd_buf[0] = AD7887_CH_AIN0 | mode; 282*4eb3ccf1SLars-Peter Clausen 283*4eb3ccf1SLars-Peter Clausen st->xfer[0].rx_buf = &st->data[0]; 284*4eb3ccf1SLars-Peter Clausen st->xfer[0].tx_buf = &st->tx_cmd_buf[0]; 285*4eb3ccf1SLars-Peter Clausen st->xfer[0].len = 2; 286*4eb3ccf1SLars-Peter Clausen 287*4eb3ccf1SLars-Peter Clausen spi_message_init(&st->msg[AD7887_CH0]); 288*4eb3ccf1SLars-Peter Clausen spi_message_add_tail(&st->xfer[0], &st->msg[AD7887_CH0]); 289*4eb3ccf1SLars-Peter Clausen 290*4eb3ccf1SLars-Peter Clausen if (pdata && pdata->en_dual) { 291*4eb3ccf1SLars-Peter Clausen st->tx_cmd_buf[2] = AD7887_CH_AIN1 | mode; 292*4eb3ccf1SLars-Peter Clausen 293*4eb3ccf1SLars-Peter Clausen st->xfer[1].rx_buf = &st->data[0]; 294*4eb3ccf1SLars-Peter Clausen st->xfer[1].tx_buf = &st->tx_cmd_buf[2]; 295*4eb3ccf1SLars-Peter Clausen st->xfer[1].len = 2; 296*4eb3ccf1SLars-Peter Clausen 297*4eb3ccf1SLars-Peter Clausen st->xfer[2].rx_buf = &st->data[2]; 298*4eb3ccf1SLars-Peter Clausen st->xfer[2].tx_buf = &st->tx_cmd_buf[0]; 299*4eb3ccf1SLars-Peter Clausen st->xfer[2].len = 2; 300*4eb3ccf1SLars-Peter Clausen 301*4eb3ccf1SLars-Peter Clausen spi_message_init(&st->msg[AD7887_CH0_CH1]); 302*4eb3ccf1SLars-Peter Clausen spi_message_add_tail(&st->xfer[1], &st->msg[AD7887_CH0_CH1]); 303*4eb3ccf1SLars-Peter Clausen spi_message_add_tail(&st->xfer[2], &st->msg[AD7887_CH0_CH1]); 304*4eb3ccf1SLars-Peter Clausen 305*4eb3ccf1SLars-Peter Clausen st->xfer[3].rx_buf = &st->data[2]; 306*4eb3ccf1SLars-Peter Clausen st->xfer[3].tx_buf = &st->tx_cmd_buf[2]; 307*4eb3ccf1SLars-Peter Clausen st->xfer[3].len = 2; 308*4eb3ccf1SLars-Peter Clausen 309*4eb3ccf1SLars-Peter Clausen spi_message_init(&st->msg[AD7887_CH1]); 310*4eb3ccf1SLars-Peter Clausen spi_message_add_tail(&st->xfer[3], &st->msg[AD7887_CH1]); 311*4eb3ccf1SLars-Peter Clausen 312*4eb3ccf1SLars-Peter Clausen indio_dev->channels = st->chip_info->channel; 313*4eb3ccf1SLars-Peter Clausen indio_dev->num_channels = 3; 314*4eb3ccf1SLars-Peter Clausen } else { 315*4eb3ccf1SLars-Peter Clausen indio_dev->channels = &st->chip_info->channel[1]; 316*4eb3ccf1SLars-Peter Clausen indio_dev->num_channels = 2; 317*4eb3ccf1SLars-Peter Clausen } 318*4eb3ccf1SLars-Peter Clausen 319*4eb3ccf1SLars-Peter Clausen ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 320*4eb3ccf1SLars-Peter Clausen &ad7887_trigger_handler, &ad7887_ring_setup_ops); 321*4eb3ccf1SLars-Peter Clausen if (ret) 322*4eb3ccf1SLars-Peter Clausen goto error_disable_reg; 323*4eb3ccf1SLars-Peter Clausen 324*4eb3ccf1SLars-Peter Clausen ret = iio_device_register(indio_dev); 325*4eb3ccf1SLars-Peter Clausen if (ret) 326*4eb3ccf1SLars-Peter Clausen goto error_unregister_ring; 327*4eb3ccf1SLars-Peter Clausen 328*4eb3ccf1SLars-Peter Clausen return 0; 329*4eb3ccf1SLars-Peter Clausen error_unregister_ring: 330*4eb3ccf1SLars-Peter Clausen iio_triggered_buffer_cleanup(indio_dev); 331*4eb3ccf1SLars-Peter Clausen error_disable_reg: 332*4eb3ccf1SLars-Peter Clausen if (st->reg) 333*4eb3ccf1SLars-Peter Clausen regulator_disable(st->reg); 334*4eb3ccf1SLars-Peter Clausen error_put_reg: 335*4eb3ccf1SLars-Peter Clausen if (st->reg) 336*4eb3ccf1SLars-Peter Clausen regulator_put(st->reg); 337*4eb3ccf1SLars-Peter Clausen error_free: 338*4eb3ccf1SLars-Peter Clausen iio_device_free(indio_dev); 339*4eb3ccf1SLars-Peter Clausen 340*4eb3ccf1SLars-Peter Clausen return ret; 341*4eb3ccf1SLars-Peter Clausen } 342*4eb3ccf1SLars-Peter Clausen 343*4eb3ccf1SLars-Peter Clausen static int __devexit ad7887_remove(struct spi_device *spi) 344*4eb3ccf1SLars-Peter Clausen { 345*4eb3ccf1SLars-Peter Clausen struct iio_dev *indio_dev = spi_get_drvdata(spi); 346*4eb3ccf1SLars-Peter Clausen struct ad7887_state *st = iio_priv(indio_dev); 347*4eb3ccf1SLars-Peter Clausen 348*4eb3ccf1SLars-Peter Clausen iio_device_unregister(indio_dev); 349*4eb3ccf1SLars-Peter Clausen iio_triggered_buffer_cleanup(indio_dev); 350*4eb3ccf1SLars-Peter Clausen if (st->reg) { 351*4eb3ccf1SLars-Peter Clausen regulator_disable(st->reg); 352*4eb3ccf1SLars-Peter Clausen regulator_put(st->reg); 353*4eb3ccf1SLars-Peter Clausen } 354*4eb3ccf1SLars-Peter Clausen iio_device_free(indio_dev); 355*4eb3ccf1SLars-Peter Clausen 356*4eb3ccf1SLars-Peter Clausen return 0; 357*4eb3ccf1SLars-Peter Clausen } 358*4eb3ccf1SLars-Peter Clausen 359*4eb3ccf1SLars-Peter Clausen static const struct spi_device_id ad7887_id[] = { 360*4eb3ccf1SLars-Peter Clausen {"ad7887", ID_AD7887}, 361*4eb3ccf1SLars-Peter Clausen {} 362*4eb3ccf1SLars-Peter Clausen }; 363*4eb3ccf1SLars-Peter Clausen MODULE_DEVICE_TABLE(spi, ad7887_id); 364*4eb3ccf1SLars-Peter Clausen 365*4eb3ccf1SLars-Peter Clausen static struct spi_driver ad7887_driver = { 366*4eb3ccf1SLars-Peter Clausen .driver = { 367*4eb3ccf1SLars-Peter Clausen .name = "ad7887", 368*4eb3ccf1SLars-Peter Clausen .owner = THIS_MODULE, 369*4eb3ccf1SLars-Peter Clausen }, 370*4eb3ccf1SLars-Peter Clausen .probe = ad7887_probe, 371*4eb3ccf1SLars-Peter Clausen .remove = __devexit_p(ad7887_remove), 372*4eb3ccf1SLars-Peter Clausen .id_table = ad7887_id, 373*4eb3ccf1SLars-Peter Clausen }; 374*4eb3ccf1SLars-Peter Clausen module_spi_driver(ad7887_driver); 375*4eb3ccf1SLars-Peter Clausen 376*4eb3ccf1SLars-Peter Clausen MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); 377*4eb3ccf1SLars-Peter Clausen MODULE_DESCRIPTION("Analog Devices AD7887 ADC"); 378*4eb3ccf1SLars-Peter Clausen MODULE_LICENSE("GPL v2"); 379