~ubuntu-branches/ubuntu/trusty/linux-linaro-omap/trusty

« back to all changes in this revision

Viewing changes to drivers/staging/iio/adc/ad799x_core.c

  • Committer: Package Import Robot
  • Author(s): John Rigby, John Rigby
  • Date: 2011-09-26 10:44:23 UTC
  • Revision ID: package-import@ubuntu.com-20110926104423-57i0gl3v99b3lkfg
Tags: 3.0.0-1007.9
[ John Rigby ]

Enable crypto modules and remove crypto-modules from
exclude-module files
LP: #826021

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * iio/adc/ad799x.c
3
 
 * Copyright (C) 2010 Michael Hennerich, Analog Devices Inc.
 
3
 * Copyright (C) 2010-1011 Michael Hennerich, Analog Devices Inc.
4
4
 *
5
5
 * based on iio/adc/max1363
6
6
 * Copyright (C) 2008-2010 Jonathan Cameron
23
23
 */
24
24
 
25
25
#include <linux/interrupt.h>
26
 
#include <linux/workqueue.h>
27
26
#include <linux/device.h>
28
27
#include <linux/kernel.h>
29
28
#include <linux/sysfs.h>
30
 
#include <linux/list.h>
31
29
#include <linux/i2c.h>
32
30
#include <linux/regulator/consumer.h>
33
31
#include <linux/slab.h>
100
98
        return ret;
101
99
}
102
100
 
103
 
static int ad799x_scan_el_set_state(struct iio_scan_el *scan_el,
104
 
                                       struct iio_dev *indio_dev,
105
 
                                       bool state)
106
 
{
107
 
        struct ad799x_state *st = indio_dev->dev_data;
108
 
        return ad799x_set_scan_mode(st, st->indio_dev->ring->scan_mask);
109
 
}
110
 
 
111
 
/* Here we claim all are 16 bits. This currently does no harm and saves
112
 
 * us a lot of scan element listings */
113
 
 
114
 
#define AD799X_SCAN_EL(number)                                          \
115
 
        IIO_SCAN_EL_C(in##number, number, 0, ad799x_scan_el_set_state);
116
 
 
117
 
static AD799X_SCAN_EL(0);
118
 
static AD799X_SCAN_EL(1);
119
 
static AD799X_SCAN_EL(2);
120
 
static AD799X_SCAN_EL(3);
121
 
static AD799X_SCAN_EL(4);
122
 
static AD799X_SCAN_EL(5);
123
 
static AD799X_SCAN_EL(6);
124
 
static AD799X_SCAN_EL(7);
125
 
 
126
 
static IIO_SCAN_EL_TIMESTAMP(8);
127
 
static IIO_CONST_ATTR_SCAN_EL_TYPE(timestamp, s, 64, 64)
128
 
 
129
 
static ssize_t ad799x_show_type(struct device *dev,
130
 
                                struct device_attribute *attr,
131
 
                                char *buf)
132
 
{
133
 
        struct iio_ring_buffer *ring = dev_get_drvdata(dev);
134
 
        struct iio_dev *indio_dev = ring->indio_dev;
135
 
        struct ad799x_state *st = indio_dev->dev_data;
136
 
 
137
 
        return sprintf(buf, "%c%d/%d\n", st->chip_info->sign,
138
 
                       st->chip_info->bits, AD799X_STORAGEBITS);
139
 
}
140
 
static IIO_DEVICE_ATTR(in_type, S_IRUGO, ad799x_show_type, NULL, 0);
141
 
 
142
 
static int ad7991_5_9_set_scan_mode(struct ad799x_state *st, unsigned mask)
143
 
{
144
 
        return i2c_smbus_write_byte(st->client,
145
 
                st->config | (mask << AD799X_CHANNEL_SHIFT));
146
 
}
147
 
 
148
 
static int ad7992_3_4_set_scan_mode(struct ad799x_state *st, unsigned mask)
149
 
{
150
 
        return ad799x_i2c_write8(st, AD7998_CONF_REG,
151
 
                st->config | (mask << AD799X_CHANNEL_SHIFT));
152
 
}
153
 
 
154
 
static int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
 
101
int ad7997_8_set_scan_mode(struct ad799x_state *st, unsigned mask)
155
102
{
156
103
        return ad799x_i2c_write16(st, AD7998_CONF_REG,
157
104
                st->config | (mask << AD799X_CHANNEL_SHIFT));
158
105
}
159
106
 
160
 
int ad799x_set_scan_mode(struct ad799x_state *st, unsigned mask)
161
 
{
162
 
        int ret;
163
 
 
164
 
        if (st->chip_info->ad799x_set_scan_mode != NULL) {
165
 
                ret = st->chip_info->ad799x_set_scan_mode(st, mask);
166
 
                return (ret > 0) ? 0 : ret;
167
 
        }
168
 
 
169
 
        return 0;
170
 
}
171
 
 
172
 
static ssize_t ad799x_read_single_channel(struct device *dev,
173
 
                                   struct device_attribute *attr,
174
 
                                   char *buf)
175
 
{
176
 
        struct iio_dev *dev_info = dev_get_drvdata(dev);
177
 
        struct ad799x_state *st = iio_dev_get_devdata(dev_info);
178
 
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
179
 
        int ret = 0, len = 0;
180
 
        u32 data ;
181
 
        u16 rxbuf[1];
 
107
static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch)
 
108
{
 
109
        u16 rxbuf;
182
110
        u8 cmd;
183
 
        long mask;
184
 
 
185
 
        mutex_lock(&dev_info->mlock);
186
 
        mask = 1 << this_attr->address;
187
 
        /* If ring buffer capture is occurring, query the buffer */
188
 
        if (iio_ring_enabled(dev_info)) {
189
 
                data = ret = ad799x_single_channel_from_ring(st, mask);
190
 
                if (ret < 0)
191
 
                        goto error_ret;
192
 
                ret = 0;
193
 
        } else {
194
 
                switch (st->id) {
195
 
                case ad7991:
196
 
                case ad7995:
197
 
                case ad7999:
198
 
                        cmd = st->config | (mask << AD799X_CHANNEL_SHIFT);
199
 
                        break;
200
 
                case ad7992:
201
 
                case ad7993:
202
 
                case ad7994:
203
 
                        cmd = mask << AD799X_CHANNEL_SHIFT;
204
 
                        break;
205
 
                case ad7997:
206
 
                case ad7998:
207
 
                        cmd = (this_attr->address <<
208
 
                                AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
209
 
                        break;
210
 
                default:
211
 
                        cmd = 0;
212
 
 
213
 
                }
214
 
                ret = ad799x_i2c_read16(st, cmd, rxbuf);
215
 
                if (ret < 0)
216
 
                        goto error_ret;
217
 
 
218
 
                data = rxbuf[0];
219
 
        }
220
 
 
221
 
        /* Pretty print the result */
222
 
        len = sprintf(buf, "%u\n", data & ((1 << (st->chip_info->bits)) - 1));
223
 
 
224
 
error_ret:
225
 
        mutex_unlock(&dev_info->mlock);
226
 
        return ret ? ret : len;
 
111
        int ret;
 
112
 
 
113
        switch (st->id) {
 
114
        case ad7991:
 
115
        case ad7995:
 
116
        case ad7999:
 
117
                cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT);
 
118
                break;
 
119
        case ad7992:
 
120
        case ad7993:
 
121
        case ad7994:
 
122
                cmd = (1 << ch) << AD799X_CHANNEL_SHIFT;
 
123
                break;
 
124
        case ad7997:
 
125
        case ad7998:
 
126
                cmd = (ch << AD799X_CHANNEL_SHIFT) | AD7997_8_READ_SINGLE;
 
127
                break;
 
128
        default:
 
129
                return -EINVAL;
 
130
        }
 
131
 
 
132
        ret = ad799x_i2c_read16(st, cmd, &rxbuf);
 
133
        if (ret < 0)
 
134
                return ret;
 
135
 
 
136
        return rxbuf;
 
137
}
 
138
 
 
139
static int ad799x_read_raw(struct iio_dev *dev_info,
 
140
                           struct iio_chan_spec const *chan,
 
141
                           int *val,
 
142
                           int *val2,
 
143
                           long m)
 
144
{
 
145
        int ret;
 
146
        struct ad799x_state *st = dev_info->dev_data;
 
147
        unsigned int scale_uv;
 
148
 
 
149
        switch (m) {
 
150
        case 0:
 
151
                mutex_lock(&dev_info->mlock);
 
152
                if (iio_ring_enabled(dev_info))
 
153
                        ret = ad799x_single_channel_from_ring(st,
 
154
                                1 << chan->address);
 
155
                else
 
156
                        ret = ad799x_scan_direct(st, chan->address);
 
157
                mutex_unlock(&dev_info->mlock);
 
158
 
 
159
                if (ret < 0)
 
160
                        return ret;
 
161
                *val = (ret >> st->chip_info->channel[0].scan_type.shift) &
 
162
                        RES_MASK(st->chip_info->channel[0].scan_type.realbits);
 
163
                return IIO_VAL_INT;
 
164
        case (1 << IIO_CHAN_INFO_SCALE_SHARED):
 
165
                scale_uv = (st->int_vref_mv * 1000)
 
166
                        >> st->chip_info->channel[0].scan_type.realbits;
 
167
                *val =  scale_uv / 1000;
 
168
                *val2 = (scale_uv % 1000) * 1000;
 
169
                return IIO_VAL_INT_PLUS_MICRO;
 
170
        }
 
171
        return -EINVAL;
227
172
}
228
173
 
229
174
static ssize_t ad799x_read_frequency(struct device *dev,
331
276
        return ret ? ret : len;
332
277
}
333
278
 
334
 
 
335
279
static ssize_t ad799x_read_channel_config(struct device *dev,
336
280
                                        struct device_attribute *attr,
337
281
                                        char *buf)
338
282
{
339
283
        struct iio_dev *dev_info = dev_get_drvdata(dev);
340
284
        struct ad799x_state *st = iio_dev_get_devdata(dev_info);
341
 
        struct iio_event_attr *this_attr = to_iio_event_attr(attr);
 
285
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
342
286
 
343
287
        int ret;
344
288
        u16 val;
345
 
        ret = ad799x_i2c_read16(st, this_attr->mask, &val);
 
289
        ret = ad799x_i2c_read16(st, this_attr->address, &val);
346
290
        if (ret)
347
291
                return ret;
348
292
 
356
300
{
357
301
        struct iio_dev *dev_info = dev_get_drvdata(dev);
358
302
        struct ad799x_state *st = iio_dev_get_devdata(dev_info);
359
 
        struct iio_event_attr *this_attr = to_iio_event_attr(attr);
 
303
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
360
304
 
361
305
        long val;
362
306
        int ret;
366
310
                return ret;
367
311
 
368
312
        mutex_lock(&dev_info->mlock);
369
 
        ret = ad799x_i2c_write16(st, this_attr->mask, val);
 
313
        ret = ad799x_i2c_write16(st, this_attr->address, val);
370
314
        mutex_unlock(&dev_info->mlock);
371
315
 
372
316
        return ret ? ret : len;
373
317
}
374
318
 
375
 
static void ad799x_interrupt_bh(struct work_struct *work_s)
 
319
static irqreturn_t ad799x_event_handler(int irq, void *private)
376
320
{
377
 
        struct ad799x_state *st = container_of(work_s,
378
 
                struct ad799x_state, work_thresh);
 
321
        struct iio_dev *indio_dev = private;
 
322
        struct ad799x_state *st = iio_dev_get_devdata(private);
379
323
        u8 status;
380
 
        int i;
 
324
        int i, ret;
381
325
 
382
 
        if (ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status))
383
 
                goto err_out;
 
326
        ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status);
 
327
        if (ret)
 
328
                return ret;
384
329
 
385
330
        if (!status)
386
 
                goto err_out;
 
331
                return -EIO;
387
332
 
388
333
        ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR);
389
334
 
390
335
        for (i = 0; i < 8; i++) {
391
336
                if (status & (1 << i))
392
 
                        iio_push_event(st->indio_dev, 0,
393
 
                                i & 0x1 ?
394
 
                                IIO_EVENT_CODE_IN_HIGH_THRESH(i >> 1) :
395
 
                                IIO_EVENT_CODE_IN_LOW_THRESH(i >> 1),
396
 
                                st->last_timestamp);
 
337
                        iio_push_event(indio_dev, 0,
 
338
                                       i & 0x1 ?
 
339
                                       IIO_EVENT_CODE_IN_HIGH_THRESH(i >> 1) :
 
340
                                       IIO_EVENT_CODE_IN_LOW_THRESH(i >> 1),
 
341
                                       iio_get_time_ns());
397
342
        }
398
343
 
399
 
err_out:
400
 
        enable_irq(st->client->irq);
401
 
}
402
 
 
403
 
static int ad799x_interrupt(struct iio_dev *dev_info,
404
 
                int index,
405
 
                s64 timestamp,
406
 
                int no_test)
407
 
{
408
 
        struct ad799x_state *st = dev_info->dev_data;
409
 
 
410
 
        st->last_timestamp = timestamp;
411
 
        schedule_work(&st->work_thresh);
412
 
        return 0;
413
 
}
414
 
 
415
 
IIO_EVENT_SH(ad799x, &ad799x_interrupt);
416
 
 
417
 
/* Direct read attribtues */
418
 
static IIO_DEV_ATTR_IN_RAW(0, ad799x_read_single_channel, 0);
419
 
static IIO_DEV_ATTR_IN_RAW(1, ad799x_read_single_channel, 1);
420
 
static IIO_DEV_ATTR_IN_RAW(2, ad799x_read_single_channel, 2);
421
 
static IIO_DEV_ATTR_IN_RAW(3, ad799x_read_single_channel, 3);
422
 
static IIO_DEV_ATTR_IN_RAW(4, ad799x_read_single_channel, 4);
423
 
static IIO_DEV_ATTR_IN_RAW(5, ad799x_read_single_channel, 5);
424
 
static IIO_DEV_ATTR_IN_RAW(6, ad799x_read_single_channel, 6);
425
 
static IIO_DEV_ATTR_IN_RAW(7, ad799x_read_single_channel, 7);
426
 
 
427
 
static ssize_t ad799x_show_scale(struct device *dev,
428
 
                                struct device_attribute *attr,
429
 
                                char *buf)
430
 
{
431
 
        /* Driver currently only support internal vref */
432
 
        struct iio_dev *dev_info = dev_get_drvdata(dev);
433
 
        struct ad799x_state *st = iio_dev_get_devdata(dev_info);
434
 
 
435
 
        /* Corresponds to Vref / 2^(bits) */
436
 
        unsigned int scale_uv = (st->int_vref_mv * 1000) >> st->chip_info->bits;
437
 
 
438
 
        return sprintf(buf, "%d.%03d\n", scale_uv / 1000, scale_uv % 1000);
439
 
}
440
 
 
441
 
static IIO_DEVICE_ATTR(in_scale, S_IRUGO, ad799x_show_scale, NULL, 0);
442
 
 
443
 
static ssize_t ad799x_show_name(struct device *dev,
444
 
                                 struct device_attribute *attr,
445
 
                                 char *buf)
446
 
{
447
 
        struct iio_dev *dev_info = dev_get_drvdata(dev);
448
 
        struct ad799x_state *st = iio_dev_get_devdata(dev_info);
449
 
        return sprintf(buf, "%s\n", st->client->name);
450
 
}
451
 
 
452
 
static IIO_DEVICE_ATTR(name, S_IRUGO, ad799x_show_name, NULL, 0);
453
 
 
454
 
static struct attribute *ad7991_5_9_3_4_device_attrs[] = {
455
 
        &iio_dev_attr_in0_raw.dev_attr.attr,
456
 
        &iio_dev_attr_in1_raw.dev_attr.attr,
457
 
        &iio_dev_attr_in2_raw.dev_attr.attr,
458
 
        &iio_dev_attr_in3_raw.dev_attr.attr,
459
 
        &iio_dev_attr_name.dev_attr.attr,
460
 
        &iio_dev_attr_in_scale.dev_attr.attr,
461
 
        NULL
462
 
};
463
 
 
464
 
static struct attribute_group ad7991_5_9_3_4_dev_attr_group = {
465
 
        .attrs = ad7991_5_9_3_4_device_attrs,
466
 
};
467
 
 
468
 
static struct attribute *ad7991_5_9_3_4_scan_el_attrs[] = {
469
 
        &iio_scan_el_in0.dev_attr.attr,
470
 
        &iio_const_attr_in0_index.dev_attr.attr,
471
 
        &iio_scan_el_in1.dev_attr.attr,
472
 
        &iio_const_attr_in1_index.dev_attr.attr,
473
 
        &iio_scan_el_in2.dev_attr.attr,
474
 
        &iio_const_attr_in2_index.dev_attr.attr,
475
 
        &iio_scan_el_in3.dev_attr.attr,
476
 
        &iio_const_attr_in3_index.dev_attr.attr,
477
 
        &iio_const_attr_timestamp_index.dev_attr.attr,
478
 
        &iio_scan_el_timestamp.dev_attr.attr,
479
 
        &iio_const_attr_timestamp_type.dev_attr.attr,
480
 
        &iio_dev_attr_in_type.dev_attr.attr,
481
 
        NULL,
482
 
};
483
 
 
484
 
static struct attribute_group ad7991_5_9_3_4_scan_el_group = {
485
 
        .name = "scan_elements",
486
 
        .attrs = ad7991_5_9_3_4_scan_el_attrs,
487
 
};
488
 
 
489
 
static struct attribute *ad7992_device_attrs[] = {
490
 
        &iio_dev_attr_in0_raw.dev_attr.attr,
491
 
        &iio_dev_attr_in1_raw.dev_attr.attr,
492
 
        &iio_dev_attr_name.dev_attr.attr,
493
 
        &iio_dev_attr_in_scale.dev_attr.attr,
494
 
        NULL
495
 
};
496
 
 
497
 
static struct attribute_group ad7992_dev_attr_group = {
498
 
        .attrs = ad7992_device_attrs,
499
 
};
500
 
 
501
 
static struct attribute *ad7992_scan_el_attrs[] = {
502
 
        &iio_scan_el_in0.dev_attr.attr,
503
 
        &iio_const_attr_in0_index.dev_attr.attr,
504
 
        &iio_scan_el_in1.dev_attr.attr,
505
 
        &iio_const_attr_in1_index.dev_attr.attr,
506
 
        &iio_const_attr_timestamp_index.dev_attr.attr,
507
 
        &iio_scan_el_timestamp.dev_attr.attr,
508
 
        &iio_const_attr_timestamp_type.dev_attr.attr,
509
 
        &iio_dev_attr_in_type.dev_attr.attr,
510
 
        NULL,
511
 
};
512
 
 
513
 
static struct attribute_group ad7992_scan_el_group = {
514
 
        .name = "scan_elements",
515
 
        .attrs = ad7992_scan_el_attrs,
516
 
};
517
 
 
518
 
static struct attribute *ad7997_8_device_attrs[] = {
519
 
        &iio_dev_attr_in0_raw.dev_attr.attr,
520
 
        &iio_dev_attr_in1_raw.dev_attr.attr,
521
 
        &iio_dev_attr_in2_raw.dev_attr.attr,
522
 
        &iio_dev_attr_in3_raw.dev_attr.attr,
523
 
        &iio_dev_attr_in4_raw.dev_attr.attr,
524
 
        &iio_dev_attr_in5_raw.dev_attr.attr,
525
 
        &iio_dev_attr_in6_raw.dev_attr.attr,
526
 
        &iio_dev_attr_in7_raw.dev_attr.attr,
527
 
        &iio_dev_attr_name.dev_attr.attr,
528
 
        &iio_dev_attr_in_scale.dev_attr.attr,
529
 
        NULL
530
 
};
531
 
 
532
 
static struct attribute_group ad7997_8_dev_attr_group = {
533
 
        .attrs = ad7997_8_device_attrs,
534
 
};
535
 
 
536
 
static struct attribute *ad7997_8_scan_el_attrs[] = {
537
 
        &iio_scan_el_in0.dev_attr.attr,
538
 
        &iio_const_attr_in0_index.dev_attr.attr,
539
 
        &iio_scan_el_in1.dev_attr.attr,
540
 
        &iio_const_attr_in1_index.dev_attr.attr,
541
 
        &iio_scan_el_in2.dev_attr.attr,
542
 
        &iio_const_attr_in2_index.dev_attr.attr,
543
 
        &iio_scan_el_in3.dev_attr.attr,
544
 
        &iio_const_attr_in3_index.dev_attr.attr,
545
 
        &iio_scan_el_in4.dev_attr.attr,
546
 
        &iio_const_attr_in4_index.dev_attr.attr,
547
 
        &iio_scan_el_in5.dev_attr.attr,
548
 
        &iio_const_attr_in5_index.dev_attr.attr,
549
 
        &iio_scan_el_in6.dev_attr.attr,
550
 
        &iio_const_attr_in6_index.dev_attr.attr,
551
 
        &iio_scan_el_in7.dev_attr.attr,
552
 
        &iio_const_attr_in7_index.dev_attr.attr,
553
 
        &iio_const_attr_timestamp_index.dev_attr.attr,
554
 
        &iio_scan_el_timestamp.dev_attr.attr,
555
 
        &iio_const_attr_timestamp_type.dev_attr.attr,
556
 
        &iio_dev_attr_in_type.dev_attr.attr,
557
 
        NULL,
558
 
};
559
 
 
560
 
static struct attribute_group ad7997_8_scan_el_group = {
561
 
        .name = "scan_elements",
562
 
        .attrs = ad7997_8_scan_el_attrs,
563
 
};
564
 
 
565
 
IIO_EVENT_ATTR_SH(in0_thresh_low_value,
566
 
                  iio_event_ad799x,
567
 
                  ad799x_read_channel_config,
568
 
                  ad799x_write_channel_config,
569
 
                  AD7998_DATALOW_CH1_REG);
570
 
 
571
 
IIO_EVENT_ATTR_SH(in0_thresh_high_value,
572
 
                  iio_event_ad799x,
573
 
                  ad799x_read_channel_config,
574
 
                  ad799x_write_channel_config,
575
 
                  AD7998_DATAHIGH_CH1_REG);
576
 
 
577
 
IIO_EVENT_ATTR_SH(in0_thresh_both_hyst_raw,
578
 
                  iio_event_ad799x,
579
 
                  ad799x_read_channel_config,
580
 
                  ad799x_write_channel_config,
581
 
                  AD7998_HYST_CH1_REG);
582
 
 
583
 
IIO_EVENT_ATTR_SH(in1_thresh_low_value,
584
 
                  iio_event_ad799x,
585
 
                  ad799x_read_channel_config,
586
 
                  ad799x_write_channel_config,
587
 
                  AD7998_DATALOW_CH2_REG);
588
 
 
589
 
IIO_EVENT_ATTR_SH(in1_thresh_high_value,
590
 
                  iio_event_ad799x,
591
 
                  ad799x_read_channel_config,
592
 
                  ad799x_write_channel_config,
593
 
                  AD7998_DATAHIGH_CH2_REG);
594
 
 
595
 
IIO_EVENT_ATTR_SH(in1_thresh_both_hyst_raw,
596
 
                  iio_event_ad799x,
597
 
                  ad799x_read_channel_config,
598
 
                  ad799x_write_channel_config,
599
 
                  AD7998_HYST_CH2_REG);
600
 
 
601
 
IIO_EVENT_ATTR_SH(in2_thresh_low_value,
602
 
                  iio_event_ad799x,
603
 
                  ad799x_read_channel_config,
604
 
                  ad799x_write_channel_config,
605
 
                  AD7998_DATALOW_CH3_REG);
606
 
 
607
 
IIO_EVENT_ATTR_SH(in2_thresh_high_value,
608
 
                  iio_event_ad799x,
609
 
                  ad799x_read_channel_config,
610
 
                  ad799x_write_channel_config,
611
 
                  AD7998_DATAHIGH_CH3_REG);
612
 
 
613
 
IIO_EVENT_ATTR_SH(in2_thresh_both_hyst_raw,
614
 
                  iio_event_ad799x,
615
 
                  ad799x_read_channel_config,
616
 
                  ad799x_write_channel_config,
617
 
                  AD7998_HYST_CH3_REG);
618
 
 
619
 
IIO_EVENT_ATTR_SH(in3_thresh_low_value,
620
 
                  iio_event_ad799x,
621
 
                  ad799x_read_channel_config,
622
 
                  ad799x_write_channel_config,
623
 
                  AD7998_DATALOW_CH4_REG);
624
 
 
625
 
IIO_EVENT_ATTR_SH(in3_thresh_high_value,
626
 
                  iio_event_ad799x,
627
 
                  ad799x_read_channel_config,
628
 
                  ad799x_write_channel_config,
629
 
                  AD7998_DATAHIGH_CH4_REG);
630
 
 
631
 
IIO_EVENT_ATTR_SH(in3_thresh_both_hyst_raw,
632
 
                  iio_event_ad799x,
633
 
                  ad799x_read_channel_config,
634
 
                  ad799x_write_channel_config,
635
 
                  AD7998_HYST_CH4_REG);
 
344
        return IRQ_HANDLED;
 
345
}
 
346
 
 
347
static IIO_DEVICE_ATTR(in0_thresh_low_value,
 
348
                       S_IRUGO | S_IWUSR,
 
349
                       ad799x_read_channel_config,
 
350
                       ad799x_write_channel_config,
 
351
                       AD7998_DATALOW_CH1_REG);
 
352
 
 
353
static IIO_DEVICE_ATTR(in0_thresh_high_value,
 
354
                       S_IRUGO | S_IWUSR,
 
355
                       ad799x_read_channel_config,
 
356
                       ad799x_write_channel_config,
 
357
                       AD7998_DATAHIGH_CH1_REG);
 
358
 
 
359
static IIO_DEVICE_ATTR(in0_thresh_both_hyst_raw,
 
360
                       S_IRUGO | S_IWUSR,
 
361
                       ad799x_read_channel_config,
 
362
                       ad799x_write_channel_config,
 
363
                       AD7998_HYST_CH1_REG);
 
364
 
 
365
static IIO_DEVICE_ATTR(in1_thresh_low_value,
 
366
                       S_IRUGO | S_IWUSR,
 
367
                       ad799x_read_channel_config,
 
368
                       ad799x_write_channel_config,
 
369
                       AD7998_DATALOW_CH2_REG);
 
370
 
 
371
static IIO_DEVICE_ATTR(in1_thresh_high_value,
 
372
                       S_IRUGO | S_IWUSR,
 
373
                       ad799x_read_channel_config,
 
374
                       ad799x_write_channel_config,
 
375
                       AD7998_DATAHIGH_CH2_REG);
 
376
 
 
377
static IIO_DEVICE_ATTR(in1_thresh_both_hyst_raw,
 
378
                       S_IRUGO | S_IWUSR,
 
379
                       ad799x_read_channel_config,
 
380
                       ad799x_write_channel_config,
 
381
                       AD7998_HYST_CH2_REG);
 
382
 
 
383
static IIO_DEVICE_ATTR(in2_thresh_low_value,
 
384
                       S_IRUGO | S_IWUSR,
 
385
                       ad799x_read_channel_config,
 
386
                       ad799x_write_channel_config,
 
387
                       AD7998_DATALOW_CH3_REG);
 
388
 
 
389
static IIO_DEVICE_ATTR(in2_thresh_high_value,
 
390
                       S_IRUGO | S_IWUSR,
 
391
                       ad799x_read_channel_config,
 
392
                       ad799x_write_channel_config,
 
393
                       AD7998_DATAHIGH_CH3_REG);
 
394
 
 
395
static IIO_DEVICE_ATTR(in2_thresh_both_hyst_raw,
 
396
                       S_IRUGO | S_IWUSR,
 
397
                       ad799x_read_channel_config,
 
398
                       ad799x_write_channel_config,
 
399
                       AD7998_HYST_CH3_REG);
 
400
 
 
401
static IIO_DEVICE_ATTR(in3_thresh_low_value,
 
402
                       S_IRUGO | S_IWUSR,
 
403
                       ad799x_read_channel_config,
 
404
                       ad799x_write_channel_config,
 
405
                       AD7998_DATALOW_CH4_REG);
 
406
 
 
407
static IIO_DEVICE_ATTR(in3_thresh_high_value,
 
408
                       S_IRUGO | S_IWUSR,
 
409
                       ad799x_read_channel_config,
 
410
                       ad799x_write_channel_config,
 
411
                       AD7998_DATAHIGH_CH4_REG);
 
412
 
 
413
static IIO_DEVICE_ATTR(in3_thresh_both_hyst_raw,
 
414
                       S_IRUGO | S_IWUSR,
 
415
                       ad799x_read_channel_config,
 
416
                       ad799x_write_channel_config,
 
417
                       AD7998_HYST_CH4_REG);
636
418
 
637
419
static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
638
420
                              ad799x_read_frequency,
640
422
static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("15625 7812 3906 1953 976 488 244 0");
641
423
 
642
424
static struct attribute *ad7993_4_7_8_event_attributes[] = {
643
 
        &iio_event_attr_in0_thresh_low_value.dev_attr.attr,
644
 
        &iio_event_attr_in0_thresh_high_value.dev_attr.attr,
645
 
        &iio_event_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
646
 
        &iio_event_attr_in1_thresh_low_value.dev_attr.attr,
647
 
        &iio_event_attr_in1_thresh_high_value.dev_attr.attr,
648
 
        &iio_event_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
649
 
        &iio_event_attr_in2_thresh_low_value.dev_attr.attr,
650
 
        &iio_event_attr_in2_thresh_high_value.dev_attr.attr,
651
 
        &iio_event_attr_in2_thresh_both_hyst_raw.dev_attr.attr,
652
 
        &iio_event_attr_in3_thresh_low_value.dev_attr.attr,
653
 
        &iio_event_attr_in3_thresh_high_value.dev_attr.attr,
654
 
        &iio_event_attr_in3_thresh_both_hyst_raw.dev_attr.attr,
 
425
        &iio_dev_attr_in0_thresh_low_value.dev_attr.attr,
 
426
        &iio_dev_attr_in0_thresh_high_value.dev_attr.attr,
 
427
        &iio_dev_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
 
428
        &iio_dev_attr_in1_thresh_low_value.dev_attr.attr,
 
429
        &iio_dev_attr_in1_thresh_high_value.dev_attr.attr,
 
430
        &iio_dev_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
 
431
        &iio_dev_attr_in2_thresh_low_value.dev_attr.attr,
 
432
        &iio_dev_attr_in2_thresh_high_value.dev_attr.attr,
 
433
        &iio_dev_attr_in2_thresh_both_hyst_raw.dev_attr.attr,
 
434
        &iio_dev_attr_in3_thresh_low_value.dev_attr.attr,
 
435
        &iio_dev_attr_in3_thresh_high_value.dev_attr.attr,
 
436
        &iio_dev_attr_in3_thresh_both_hyst_raw.dev_attr.attr,
655
437
        &iio_dev_attr_sampling_frequency.dev_attr.attr,
656
438
        &iio_const_attr_sampling_frequency_available.dev_attr.attr,
657
439
        NULL,
662
444
};
663
445
 
664
446
static struct attribute *ad7992_event_attributes[] = {
665
 
        &iio_event_attr_in0_thresh_low_value.dev_attr.attr,
666
 
        &iio_event_attr_in0_thresh_high_value.dev_attr.attr,
667
 
        &iio_event_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
668
 
        &iio_event_attr_in1_thresh_low_value.dev_attr.attr,
669
 
        &iio_event_attr_in1_thresh_high_value.dev_attr.attr,
670
 
        &iio_event_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
 
447
        &iio_dev_attr_in0_thresh_low_value.dev_attr.attr,
 
448
        &iio_dev_attr_in0_thresh_high_value.dev_attr.attr,
 
449
        &iio_dev_attr_in0_thresh_both_hyst_raw.dev_attr.attr,
 
450
        &iio_dev_attr_in1_thresh_low_value.dev_attr.attr,
 
451
        &iio_dev_attr_in1_thresh_high_value.dev_attr.attr,
 
452
        &iio_dev_attr_in1_thresh_both_hyst_raw.dev_attr.attr,
671
453
        &iio_dev_attr_sampling_frequency.dev_attr.attr,
672
454
        &iio_const_attr_sampling_frequency_available.dev_attr.attr,
673
455
        NULL,
677
459
        .attrs = ad7992_event_attributes,
678
460
};
679
461
 
 
462
static const struct iio_info ad7991_info = {
 
463
        .read_raw = &ad799x_read_raw,
 
464
        .driver_module = THIS_MODULE,
 
465
};
 
466
 
 
467
static const struct iio_info ad7992_info = {
 
468
        .read_raw = &ad799x_read_raw,
 
469
        .num_interrupt_lines = 1,
 
470
        .event_attrs = &ad7992_event_attrs_group,
 
471
        .driver_module = THIS_MODULE,
 
472
};
 
473
 
 
474
static const struct iio_info ad7993_4_7_8_info = {
 
475
        .read_raw = &ad799x_read_raw,
 
476
        .num_interrupt_lines = 1,
 
477
        .event_attrs = &ad7993_4_7_8_event_attrs_group,
 
478
        .driver_module = THIS_MODULE,
 
479
};
 
480
 
680
481
static const struct ad799x_chip_info ad799x_chip_info_tbl[] = {
681
482
        [ad7991] = {
682
 
                .num_inputs = 4,
683
 
                .bits = 12,
684
 
                .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
 
483
                .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
 
484
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
485
                                       0, 0, IIO_ST('u', 12, 16, 0), 0),
 
486
                .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
 
487
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
488
                                       1, 1, IIO_ST('u', 12, 16, 0), 0),
 
489
                .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
 
490
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
491
                                       2, 2, IIO_ST('u', 12, 16, 0), 0),
 
492
                .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
 
493
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
494
                                       3, 3, IIO_ST('u', 12, 16, 0), 0),
 
495
                .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
 
496
                .num_channels = 5,
685
497
                .int_vref_mv = 4096,
686
 
                .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
687
 
                .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
688
 
                .ad799x_set_scan_mode = ad7991_5_9_set_scan_mode,
 
498
                .info = &ad7991_info,
689
499
        },
690
500
        [ad7995] = {
691
 
                .num_inputs = 4,
692
 
                .bits = 10,
693
 
                .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
 
501
                .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
 
502
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
503
                                       0, 0, IIO_ST('u', 10, 16, 0), 0),
 
504
                .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
 
505
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
506
                                       1, 1, IIO_ST('u', 10, 16, 0), 0),
 
507
                .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
 
508
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
509
                                       2, 2, IIO_ST('u', 10, 16, 0), 0),
 
510
                .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
 
511
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
512
                                       3, 3, IIO_ST('u', 10, 16, 0), 0),
 
513
                .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
 
514
                .num_channels = 5,
694
515
                .int_vref_mv = 1024,
695
 
                .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
696
 
                .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
697
 
                .ad799x_set_scan_mode = ad7991_5_9_set_scan_mode,
 
516
                .info = &ad7991_info,
698
517
        },
699
518
        [ad7999] = {
700
 
                .num_inputs = 4,
701
 
                .bits = 10,
702
 
                .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
 
519
                .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
 
520
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
521
                                       0, 0, IIO_ST('u', 10, 16, 0), 0),
 
522
                .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
 
523
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
524
                                       1, 1, IIO_ST('u', 10, 16, 0), 0),
 
525
                .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
 
526
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
527
                                       2, 2, IIO_ST('u', 10, 16, 0), 0),
 
528
                .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
 
529
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
530
                                       3, 3, IIO_ST('u', 10, 16, 0), 0),
 
531
                .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
 
532
                .num_channels = 5,
703
533
                .int_vref_mv = 1024,
704
 
                .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
705
 
                .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
706
 
                .ad799x_set_scan_mode = ad7991_5_9_set_scan_mode,
 
534
                .info = &ad7991_info,
707
535
        },
708
536
        [ad7992] = {
709
 
                .num_inputs = 2,
710
 
                .bits = 12,
711
 
                .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
 
537
                .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
 
538
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
539
                                       0, 0, IIO_ST('u', 12, 16, 0), 0),
 
540
                .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
 
541
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
542
                                       1, 1, IIO_ST('u', 12, 16, 0), 0),
 
543
                .channel[2] = IIO_CHAN_SOFT_TIMESTAMP(2),
 
544
                .num_channels = 3,
712
545
                .int_vref_mv = 4096,
713
 
                .monitor_mode = true,
714
546
                .default_config = AD7998_ALERT_EN,
715
 
                .dev_attrs = &ad7992_dev_attr_group,
716
 
                .scan_attrs = &ad7992_scan_el_group,
717
 
                .event_attrs = &ad7992_event_attrs_group,
718
 
                .ad799x_set_scan_mode = ad7992_3_4_set_scan_mode,
 
547
                .info = &ad7992_info,
719
548
        },
720
549
        [ad7993] = {
721
 
                .num_inputs = 4,
722
 
                .bits = 10,
723
 
                .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
 
550
                .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
 
551
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
552
                                       0, 0, IIO_ST('u', 10, 16, 0), 0),
 
553
                .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
 
554
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
555
                                       1, 1, IIO_ST('u', 10, 16, 0), 0),
 
556
                .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
 
557
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
558
                                       2, 2, IIO_ST('u', 10, 16, 0), 0),
 
559
                .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
 
560
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
561
                                       3, 3, IIO_ST('u', 10, 16, 0), 0),
 
562
                .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
 
563
                .num_channels = 5,
724
564
                .int_vref_mv = 1024,
725
 
                .monitor_mode = true,
726
565
                .default_config = AD7998_ALERT_EN,
727
 
                .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
728
 
                .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
729
 
                .event_attrs = &ad7993_4_7_8_event_attrs_group,
730
 
                .ad799x_set_scan_mode = ad7992_3_4_set_scan_mode,
 
566
                .info = &ad7993_4_7_8_info,
731
567
        },
732
568
        [ad7994] = {
733
 
                .num_inputs = 4,
734
 
                .bits = 12,
735
 
                .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
 
569
                .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
 
570
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
571
                                       0, 0, IIO_ST('u', 12, 16, 0), 0),
 
572
                .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
 
573
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
574
                                       1, 1, IIO_ST('u', 12, 16, 0), 0),
 
575
                .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
 
576
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
577
                                       2, 2, IIO_ST('u', 12, 16, 0), 0),
 
578
                .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
 
579
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
580
                                       3, 3, IIO_ST('u', 12, 16, 0), 0),
 
581
                .channel[4] = IIO_CHAN_SOFT_TIMESTAMP(4),
 
582
                .num_channels = 5,
736
583
                .int_vref_mv = 4096,
737
 
                .monitor_mode = true,
738
584
                .default_config = AD7998_ALERT_EN,
739
 
                .dev_attrs = &ad7991_5_9_3_4_dev_attr_group,
740
 
                .scan_attrs = &ad7991_5_9_3_4_scan_el_group,
741
 
                .event_attrs = &ad7993_4_7_8_event_attrs_group,
742
 
                .ad799x_set_scan_mode = ad7992_3_4_set_scan_mode,
 
585
                .info = &ad7993_4_7_8_info,
743
586
        },
744
587
        [ad7997] = {
745
 
                .num_inputs = 8,
746
 
                .bits = 10,
747
 
                .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
 
588
                .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
 
589
                                          (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
590
                                          0, 0, IIO_ST('u', 10, 16, 0), 0),
 
591
                .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
 
592
                                          (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
593
                                          1, 1, IIO_ST('u', 10, 16, 0), 0),
 
594
                .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
 
595
                                          (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
596
                                          2, 2, IIO_ST('u', 10, 16, 0), 0),
 
597
                .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
 
598
                                          (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
599
                                          3, 3, IIO_ST('u', 10, 16, 0), 0),
 
600
                .channel[4] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
 
601
                                          (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
602
                                          4, 4, IIO_ST('u', 10, 16, 0), 0),
 
603
                .channel[5] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
 
604
                                          (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
605
                                          5, 5, IIO_ST('u', 10, 16, 0), 0),
 
606
                .channel[6] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0,
 
607
                                          (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
608
                                          6, 6, IIO_ST('u', 10, 16, 0), 0),
 
609
                .channel[7] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0,
 
610
                                          (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
611
                                          7, 7, IIO_ST('u', 10, 16, 0), 0),
 
612
                .channel[8] = IIO_CHAN_SOFT_TIMESTAMP(8),
 
613
                .num_channels = 9,
748
614
                .int_vref_mv = 1024,
749
 
                .monitor_mode = true,
750
615
                .default_config = AD7998_ALERT_EN,
751
 
                .dev_attrs = &ad7997_8_dev_attr_group,
752
 
                .scan_attrs = &ad7997_8_scan_el_group,
753
 
                .event_attrs = &ad7993_4_7_8_event_attrs_group,
754
 
                .ad799x_set_scan_mode = ad7997_8_set_scan_mode,
 
616
                .info = &ad7993_4_7_8_info,
755
617
        },
756
618
        [ad7998] = {
757
 
                .num_inputs = 8,
758
 
                .bits = 12,
759
 
                .sign = IIO_SCAN_EL_TYPE_UNSIGNED,
 
619
                .channel[0] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 0, 0,
 
620
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
621
                                       0, 0, IIO_ST('u', 12, 16, 0), 0),
 
622
                .channel[1] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
 
623
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
624
                                       1, 1, IIO_ST('u', 12, 16, 0), 0),
 
625
                .channel[2] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 2, 0,
 
626
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
627
                                       2, 2, IIO_ST('u', 12, 16, 0), 0),
 
628
                .channel[3] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 3, 0,
 
629
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
630
                                       3, 3, IIO_ST('u', 12, 16, 0), 0),
 
631
                .channel[4] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 4, 0,
 
632
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
633
                                       4, 4, IIO_ST('u', 12, 16, 0), 0),
 
634
                .channel[5] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 5, 0,
 
635
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
636
                                       5, 5, IIO_ST('u', 12, 16, 0), 0),
 
637
                .channel[6] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 6, 0,
 
638
                                       (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
639
                                       6, 6, IIO_ST('u', 12, 16, 0), 0),
 
640
                .channel[7] = IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 7, 0,
 
641
                                          (1 << IIO_CHAN_INFO_SCALE_SHARED),
 
642
                                          7, 7, IIO_ST('u', 12, 16, 0), 0),
 
643
                .channel[8] = IIO_CHAN_SOFT_TIMESTAMP(8),
 
644
                .num_channels = 9,
760
645
                .int_vref_mv = 4096,
761
 
                .monitor_mode = true,
762
646
                .default_config = AD7998_ALERT_EN,
763
 
                .dev_attrs = &ad7997_8_dev_attr_group,
764
 
                .scan_attrs = &ad7997_8_scan_el_group,
765
 
                .event_attrs = &ad7993_4_7_8_event_attrs_group,
766
 
                .ad799x_set_scan_mode = ad7997_8_set_scan_mode,
 
647
                .info = &ad7993_4_7_8_info,
767
648
        },
768
649
};
769
650
 
772
653
{
773
654
        int ret, regdone = 0;
774
655
        struct ad799x_platform_data *pdata = client->dev.platform_data;
775
 
        struct ad799x_state *st = kzalloc(sizeof(*st), GFP_KERNEL);
776
 
        if (st == NULL) {
777
 
                ret = -ENOMEM;
778
 
                goto error_ret;
779
 
        }
780
 
 
 
656
        struct ad799x_state *st;
 
657
        struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
 
658
 
 
659
        if (indio_dev == NULL)
 
660
                return -ENOMEM;
 
661
 
 
662
        st = iio_priv(indio_dev);
781
663
        /* this is only used for device removal purposes */
782
 
        i2c_set_clientdata(client, st);
 
664
        i2c_set_clientdata(client, indio_dev);
783
665
 
784
 
        atomic_set(&st->protect_ring, 0);
785
666
        st->id = id->driver_data;
786
667
        st->chip_info = &ad799x_chip_info_tbl[st->id];
787
668
        st->config = st->chip_info->default_config;
801
682
        }
802
683
        st->client = client;
803
684
 
804
 
        st->indio_dev = iio_allocate_device();
805
 
        if (st->indio_dev == NULL) {
806
 
                ret = -ENOMEM;
 
685
        indio_dev->dev.parent = &client->dev;
 
686
        indio_dev->name = id->name;
 
687
        indio_dev->info = st->chip_info->info;
 
688
        indio_dev->name = id->name;
 
689
        indio_dev->dev_data = (void *)(st);
 
690
 
 
691
        indio_dev->modes = INDIO_DIRECT_MODE;
 
692
        indio_dev->channels = st->chip_info->channel;
 
693
        indio_dev->num_channels = st->chip_info->num_channels;
 
694
 
 
695
        ret = ad799x_register_ring_funcs_and_init(indio_dev);
 
696
        if (ret)
807
697
                goto error_disable_reg;
808
 
        }
809
 
 
810
 
        /* Estabilish that the iio_dev is a child of the i2c device */
811
 
        st->indio_dev->dev.parent = &client->dev;
812
 
        st->indio_dev->attrs = st->chip_info->dev_attrs;
813
 
        st->indio_dev->event_attrs = st->chip_info->event_attrs;
814
 
 
815
 
        st->indio_dev->dev_data = (void *)(st);
816
 
        st->indio_dev->driver_module = THIS_MODULE;
817
 
        st->indio_dev->modes = INDIO_DIRECT_MODE;
818
 
        st->indio_dev->num_interrupt_lines = 1;
819
 
 
820
 
        ret = ad799x_set_scan_mode(st, 0);
821
 
        if (ret)
822
 
                goto error_free_device;
823
 
 
824
 
        ret = ad799x_register_ring_funcs_and_init(st->indio_dev);
825
 
        if (ret)
826
 
                goto error_free_device;
827
 
 
828
 
        ret = iio_device_register(st->indio_dev);
 
698
 
 
699
        ret = iio_device_register(indio_dev);
829
700
        if (ret)
830
701
                goto error_cleanup_ring;
831
702
        regdone = 1;
832
703
 
833
 
        ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
 
704
        ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
 
705
                                          indio_dev->channels,
 
706
                                          indio_dev->num_channels);
834
707
        if (ret)
835
708
                goto error_cleanup_ring;
836
709
 
837
 
        if (client->irq > 0 && st->chip_info->monitor_mode) {
838
 
                INIT_WORK(&st->work_thresh, ad799x_interrupt_bh);
839
 
 
840
 
                ret = iio_register_interrupt_line(client->irq,
841
 
                                st->indio_dev,
842
 
                                0,
843
 
                                IRQF_TRIGGER_FALLING,
844
 
                                client->name);
 
710
        if (client->irq > 0) {
 
711
                ret = request_threaded_irq(client->irq,
 
712
                                           NULL,
 
713
                                           ad799x_event_handler,
 
714
                                           IRQF_TRIGGER_FALLING |
 
715
                                           IRQF_ONESHOT,
 
716
                                           client->name,
 
717
                                           indio_dev);
845
718
                if (ret)
846
719
                        goto error_cleanup_ring;
847
 
 
848
 
                /*
849
 
                 * The event handler list element refer to iio_event_ad799x.
850
 
                 * All event attributes bind to the same event handler.
851
 
                 * So, only register event handler once.
852
 
                 */
853
 
                iio_add_event_to_list(&iio_event_ad799x,
854
 
                                &st->indio_dev->interrupts[0]->ev_list);
855
720
        }
856
721
 
857
722
        return 0;
 
723
 
858
724
error_cleanup_ring:
859
 
        ad799x_ring_cleanup(st->indio_dev);
860
 
error_free_device:
861
 
        if (!regdone)
862
 
                iio_free_device(st->indio_dev);
863
 
        else
864
 
                iio_device_unregister(st->indio_dev);
 
725
        ad799x_ring_cleanup(indio_dev);
865
726
error_disable_reg:
866
727
        if (!IS_ERR(st->reg))
867
728
                regulator_disable(st->reg);
868
729
error_put_reg:
869
730
        if (!IS_ERR(st->reg))
870
731
                regulator_put(st->reg);
871
 
        kfree(st);
872
 
error_ret:
 
732
        if (regdone)
 
733
                iio_device_unregister(indio_dev);
 
734
        else
 
735
                iio_free_device(indio_dev);
 
736
 
873
737
        return ret;
874
738
}
875
739
 
876
740
static __devexit int ad799x_remove(struct i2c_client *client)
877
741
{
878
 
        struct ad799x_state *st = i2c_get_clientdata(client);
879
 
        struct iio_dev *indio_dev = st->indio_dev;
 
742
        struct iio_dev *indio_dev = i2c_get_clientdata(client);
 
743
        struct ad799x_state *st = iio_priv(indio_dev);
880
744
 
881
 
        if (client->irq > 0 && st->chip_info->monitor_mode)
882
 
                iio_unregister_interrupt_line(indio_dev, 0);
 
745
        if (client->irq > 0)
 
746
                free_irq(client->irq, indio_dev);
883
747
 
884
748
        iio_ring_buffer_unregister(indio_dev->ring);
885
749
        ad799x_ring_cleanup(indio_dev);
 
750
        if (!IS_ERR(st->reg)) {
 
751
                regulator_disable(st->reg);
 
752
                regulator_put(st->reg);
 
753
        }
886
754
        iio_device_unregister(indio_dev);
887
 
        if (!IS_ERR(st->reg)) {
888
 
                regulator_disable(st->reg);
889
 
                regulator_put(st->reg);
890
 
        }
891
 
        kfree(st);
892
755
 
893
756
        return 0;
894
757
}