GNU Linux-libre 4.9.301-gnu1
[releases.git] / drivers / staging / iio / impedance-analyzer / ad5933.c
1 /*
2  * AD5933 AD5934 Impedance Converter, Network Analyzer
3  *
4  * Copyright 2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/sysfs.h>
13 #include <linux/i2c.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/types.h>
16 #include <linux/err.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
19
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/kfifo_buf.h>
24
25 /* AD5933/AD5934 Registers */
26 #define AD5933_REG_CONTROL_HB           0x80    /* R/W, 2 bytes */
27 #define AD5933_REG_CONTROL_LB           0x81    /* R/W, 2 bytes */
28 #define AD5933_REG_FREQ_START           0x82    /* R/W, 3 bytes */
29 #define AD5933_REG_FREQ_INC             0x85    /* R/W, 3 bytes */
30 #define AD5933_REG_INC_NUM              0x88    /* R/W, 2 bytes, 9 bit */
31 #define AD5933_REG_SETTLING_CYCLES      0x8A    /* R/W, 2 bytes */
32 #define AD5933_REG_STATUS               0x8F    /* R, 1 byte */
33 #define AD5933_REG_TEMP_DATA            0x92    /* R, 2 bytes*/
34 #define AD5933_REG_REAL_DATA            0x94    /* R, 2 bytes*/
35 #define AD5933_REG_IMAG_DATA            0x96    /* R, 2 bytes*/
36
37 /* AD5933_REG_CONTROL_HB Bits */
38 #define AD5933_CTRL_INIT_START_FREQ     (0x1 << 4)
39 #define AD5933_CTRL_START_SWEEP         (0x2 << 4)
40 #define AD5933_CTRL_INC_FREQ            (0x3 << 4)
41 #define AD5933_CTRL_REPEAT_FREQ         (0x4 << 4)
42 #define AD5933_CTRL_MEASURE_TEMP        (0x9 << 4)
43 #define AD5933_CTRL_POWER_DOWN          (0xA << 4)
44 #define AD5933_CTRL_STANDBY             (0xB << 4)
45
46 #define AD5933_CTRL_RANGE_2000mVpp      (0x0 << 1)
47 #define AD5933_CTRL_RANGE_200mVpp       (0x1 << 1)
48 #define AD5933_CTRL_RANGE_400mVpp       (0x2 << 1)
49 #define AD5933_CTRL_RANGE_1000mVpp      (0x3 << 1)
50 #define AD5933_CTRL_RANGE(x)            ((x) << 1)
51
52 #define AD5933_CTRL_PGA_GAIN_1          (0x1 << 0)
53 #define AD5933_CTRL_PGA_GAIN_5          (0x0 << 0)
54
55 /* AD5933_REG_CONTROL_LB Bits */
56 #define AD5933_CTRL_RESET               (0x1 << 4)
57 #define AD5933_CTRL_INT_SYSCLK          (0x0 << 3)
58 #define AD5933_CTRL_EXT_SYSCLK          (0x1 << 3)
59
60 /* AD5933_REG_STATUS Bits */
61 #define AD5933_STAT_TEMP_VALID          (0x1 << 0)
62 #define AD5933_STAT_DATA_VALID          (0x1 << 1)
63 #define AD5933_STAT_SWEEP_DONE          (0x1 << 2)
64
65 /* I2C Block Commands */
66 #define AD5933_I2C_BLOCK_WRITE          0xA0
67 #define AD5933_I2C_BLOCK_READ           0xA1
68 #define AD5933_I2C_ADDR_POINTER         0xB0
69
70 /* Device Specs */
71 #define AD5933_INT_OSC_FREQ_Hz          16776000
72 #define AD5933_MAX_OUTPUT_FREQ_Hz       100000
73 #define AD5933_MAX_RETRIES              100
74
75 #define AD5933_OUT_RANGE                1
76 #define AD5933_OUT_RANGE_AVAIL          2
77 #define AD5933_OUT_SETTLING_CYCLES      3
78 #define AD5933_IN_PGA_GAIN              4
79 #define AD5933_IN_PGA_GAIN_AVAIL        5
80 #define AD5933_FREQ_POINTS              6
81
82 #define AD5933_POLL_TIME_ms             10
83 #define AD5933_INIT_EXCITATION_TIME_ms  100
84
85 /**
86  * struct ad5933_platform_data - platform specific data
87  * @ext_clk_Hz:         the external clock frequency in Hz, if not set
88  *                      the driver uses the internal clock (16.776 MHz)
89  * @vref_mv:            the external reference voltage in millivolt
90  */
91
92 struct ad5933_platform_data {
93         unsigned long                   ext_clk_Hz;
94         unsigned short                  vref_mv;
95 };
96
97 struct ad5933_state {
98         struct i2c_client               *client;
99         struct regulator                *reg;
100         struct delayed_work             work;
101         unsigned long                   mclk_hz;
102         unsigned char                   ctrl_hb;
103         unsigned char                   ctrl_lb;
104         unsigned int                    range_avail[4];
105         unsigned short                  vref_mv;
106         unsigned short                  settling_cycles;
107         unsigned short                  freq_points;
108         unsigned int                    freq_start;
109         unsigned int                    freq_inc;
110         unsigned int                    state;
111         unsigned int                    poll_time_jiffies;
112 };
113
114 static struct ad5933_platform_data ad5933_default_pdata  = {
115         .vref_mv = 3300,
116 };
117
118 static const struct iio_chan_spec ad5933_channels[] = {
119         {
120                 .type = IIO_TEMP,
121                 .indexed = 1,
122                 .channel = 0,
123                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
124                         BIT(IIO_CHAN_INFO_SCALE),
125                 .address = AD5933_REG_TEMP_DATA,
126                 .scan_index = -1,
127                 .scan_type = {
128                         .sign = 's',
129                         .realbits = 14,
130                         .storagebits = 16,
131                 },
132         }, { /* Ring Channels */
133                 .type = IIO_VOLTAGE,
134                 .indexed = 1,
135                 .channel = 0,
136                 .extend_name = "real",
137                 .address = AD5933_REG_REAL_DATA,
138                 .scan_index = 0,
139                 .scan_type = {
140                         .sign = 's',
141                         .realbits = 16,
142                         .storagebits = 16,
143                 },
144         }, {
145                 .type = IIO_VOLTAGE,
146                 .indexed = 1,
147                 .channel = 0,
148                 .extend_name = "imag",
149                 .address = AD5933_REG_IMAG_DATA,
150                 .scan_index = 1,
151                 .scan_type = {
152                         .sign = 's',
153                         .realbits = 16,
154                         .storagebits = 16,
155                 },
156         },
157 };
158
159 static int ad5933_i2c_write(struct i2c_client *client, u8 reg, u8 len, u8 *data)
160 {
161         int ret;
162
163         while (len--) {
164                 ret = i2c_smbus_write_byte_data(client, reg++, *data++);
165                 if (ret < 0) {
166                         dev_err(&client->dev, "I2C write error\n");
167                         return ret;
168                 }
169         }
170         return 0;
171 }
172
173 static int ad5933_i2c_read(struct i2c_client *client, u8 reg, u8 len, u8 *data)
174 {
175         int ret;
176
177         while (len--) {
178                 ret = i2c_smbus_read_byte_data(client, reg++);
179                 if (ret < 0) {
180                         dev_err(&client->dev, "I2C read error\n");
181                         return ret;
182                 }
183                 *data++ = ret;
184         }
185         return 0;
186 }
187
188 static int ad5933_cmd(struct ad5933_state *st, unsigned char cmd)
189 {
190         unsigned char dat = st->ctrl_hb | cmd;
191
192         return ad5933_i2c_write(st->client,
193                         AD5933_REG_CONTROL_HB, 1, &dat);
194 }
195
196 static int ad5933_reset(struct ad5933_state *st)
197 {
198         unsigned char dat = st->ctrl_lb | AD5933_CTRL_RESET;
199
200         return ad5933_i2c_write(st->client,
201                         AD5933_REG_CONTROL_LB, 1, &dat);
202 }
203
204 static int ad5933_wait_busy(struct ad5933_state *st, unsigned char event)
205 {
206         unsigned char val, timeout = AD5933_MAX_RETRIES;
207         int ret;
208
209         while (timeout--) {
210                 ret =  ad5933_i2c_read(st->client, AD5933_REG_STATUS, 1, &val);
211                 if (ret < 0)
212                         return ret;
213                 if (val & event)
214                         return val;
215                 cpu_relax();
216                 mdelay(1);
217         }
218
219         return -EAGAIN;
220 }
221
222 static int ad5933_set_freq(struct ad5933_state *st,
223                            unsigned int reg, unsigned long freq)
224 {
225         unsigned long long freqreg;
226         union {
227                 __be32 d32;
228                 u8 d8[4];
229         } dat;
230
231         freqreg = (u64) freq * (u64) (1 << 27);
232         do_div(freqreg, st->mclk_hz / 4);
233
234         switch (reg) {
235         case AD5933_REG_FREQ_START:
236                 st->freq_start = freq;
237                 break;
238         case AD5933_REG_FREQ_INC:
239                 st->freq_inc = freq;
240                 break;
241         default:
242                 return -EINVAL;
243         }
244
245         dat.d32 = cpu_to_be32(freqreg);
246         return ad5933_i2c_write(st->client, reg, 3, &dat.d8[1]);
247 }
248
249 static int ad5933_setup(struct ad5933_state *st)
250 {
251         __be16 dat;
252         int ret;
253
254         ret = ad5933_reset(st);
255         if (ret < 0)
256                 return ret;
257
258         ret = ad5933_set_freq(st, AD5933_REG_FREQ_START, 10000);
259         if (ret < 0)
260                 return ret;
261
262         ret = ad5933_set_freq(st, AD5933_REG_FREQ_INC, 200);
263         if (ret < 0)
264                 return ret;
265
266         st->settling_cycles = 10;
267         dat = cpu_to_be16(st->settling_cycles);
268
269         ret = ad5933_i2c_write(st->client,
270                                AD5933_REG_SETTLING_CYCLES,
271                                2, (u8 *)&dat);
272         if (ret < 0)
273                 return ret;
274
275         st->freq_points = 100;
276         dat = cpu_to_be16(st->freq_points);
277
278         return ad5933_i2c_write(st->client, AD5933_REG_INC_NUM, 2, (u8 *)&dat);
279 }
280
281 static void ad5933_calc_out_ranges(struct ad5933_state *st)
282 {
283         int i;
284         unsigned int normalized_3v3[4] = {1980, 198, 383, 970};
285
286         for (i = 0; i < 4; i++)
287                 st->range_avail[i] = normalized_3v3[i] * st->vref_mv / 3300;
288
289 }
290
291 /*
292  * handles: AD5933_REG_FREQ_START and AD5933_REG_FREQ_INC
293  */
294
295 static ssize_t ad5933_show_frequency(struct device *dev,
296                                      struct device_attribute *attr,
297                                      char *buf)
298 {
299         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
300         struct ad5933_state *st = iio_priv(indio_dev);
301         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
302         int ret;
303         unsigned long long freqreg;
304         union {
305                 __be32 d32;
306                 u8 d8[4];
307         } dat;
308
309         mutex_lock(&indio_dev->mlock);
310         ret = ad5933_i2c_read(st->client, this_attr->address, 3, &dat.d8[1]);
311         mutex_unlock(&indio_dev->mlock);
312         if (ret < 0)
313                 return ret;
314
315         freqreg = be32_to_cpu(dat.d32) & 0xFFFFFF;
316
317         freqreg = (u64)freqreg * (u64)(st->mclk_hz / 4);
318         do_div(freqreg, 1 << 27);
319
320         return sprintf(buf, "%d\n", (int)freqreg);
321 }
322
323 static ssize_t ad5933_store_frequency(struct device *dev,
324                                       struct device_attribute *attr,
325                                       const char *buf,
326                                       size_t len)
327 {
328         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
329         struct ad5933_state *st = iio_priv(indio_dev);
330         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
331         unsigned long val;
332         int ret;
333
334         ret = kstrtoul(buf, 10, &val);
335         if (ret)
336                 return ret;
337
338         if (val > AD5933_MAX_OUTPUT_FREQ_Hz)
339                 return -EINVAL;
340
341         mutex_lock(&indio_dev->mlock);
342         ret = ad5933_set_freq(st, this_attr->address, val);
343         mutex_unlock(&indio_dev->mlock);
344
345         return ret ? ret : len;
346 }
347
348 static IIO_DEVICE_ATTR(out_voltage0_freq_start, S_IRUGO | S_IWUSR,
349                         ad5933_show_frequency,
350                         ad5933_store_frequency,
351                         AD5933_REG_FREQ_START);
352
353 static IIO_DEVICE_ATTR(out_voltage0_freq_increment, S_IRUGO | S_IWUSR,
354                         ad5933_show_frequency,
355                         ad5933_store_frequency,
356                         AD5933_REG_FREQ_INC);
357
358 static ssize_t ad5933_show(struct device *dev,
359                            struct device_attribute *attr,
360                            char *buf)
361 {
362         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
363         struct ad5933_state *st = iio_priv(indio_dev);
364         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
365         int ret = 0, len = 0;
366
367         mutex_lock(&indio_dev->mlock);
368         switch ((u32)this_attr->address) {
369         case AD5933_OUT_RANGE:
370                 len = sprintf(buf, "%u\n",
371                               st->range_avail[(st->ctrl_hb >> 1) & 0x3]);
372                 break;
373         case AD5933_OUT_RANGE_AVAIL:
374                 len = sprintf(buf, "%u %u %u %u\n", st->range_avail[0],
375                               st->range_avail[3], st->range_avail[2],
376                               st->range_avail[1]);
377                 break;
378         case AD5933_OUT_SETTLING_CYCLES:
379                 len = sprintf(buf, "%d\n", st->settling_cycles);
380                 break;
381         case AD5933_IN_PGA_GAIN:
382                 len = sprintf(buf, "%s\n",
383                               (st->ctrl_hb & AD5933_CTRL_PGA_GAIN_1) ?
384                               "1" : "0.2");
385                 break;
386         case AD5933_IN_PGA_GAIN_AVAIL:
387                 len = sprintf(buf, "1 0.2\n");
388                 break;
389         case AD5933_FREQ_POINTS:
390                 len = sprintf(buf, "%d\n", st->freq_points);
391                 break;
392         default:
393                 ret = -EINVAL;
394         }
395
396         mutex_unlock(&indio_dev->mlock);
397         return ret ? ret : len;
398 }
399
400 static ssize_t ad5933_store(struct device *dev,
401                             struct device_attribute *attr,
402                             const char *buf,
403                             size_t len)
404 {
405         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
406         struct ad5933_state *st = iio_priv(indio_dev);
407         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
408         u16 val;
409         int i, ret = 0;
410         __be16 dat;
411
412         if (this_attr->address != AD5933_IN_PGA_GAIN) {
413                 ret = kstrtou16(buf, 10, &val);
414                 if (ret)
415                         return ret;
416         }
417
418         mutex_lock(&indio_dev->mlock);
419         switch ((u32)this_attr->address) {
420         case AD5933_OUT_RANGE:
421                 ret = -EINVAL;
422                 for (i = 0; i < 4; i++)
423                         if (val == st->range_avail[i]) {
424                                 st->ctrl_hb &= ~AD5933_CTRL_RANGE(0x3);
425                                 st->ctrl_hb |= AD5933_CTRL_RANGE(i);
426                                 ret = ad5933_cmd(st, 0);
427                                 break;
428                         }
429                 break;
430         case AD5933_IN_PGA_GAIN:
431                 if (sysfs_streq(buf, "1")) {
432                         st->ctrl_hb |= AD5933_CTRL_PGA_GAIN_1;
433                 } else if (sysfs_streq(buf, "0.2")) {
434                         st->ctrl_hb &= ~AD5933_CTRL_PGA_GAIN_1;
435                 } else {
436                         ret = -EINVAL;
437                         break;
438                 }
439                 ret = ad5933_cmd(st, 0);
440                 break;
441         case AD5933_OUT_SETTLING_CYCLES:
442                 val = clamp(val, (u16)0, (u16)0x7FF);
443                 st->settling_cycles = val;
444
445                 /* 2x, 4x handling, see datasheet */
446                 if (val > 1022)
447                         val = (val >> 2) | (3 << 9);
448                 else if (val > 511)
449                         val = (val >> 1) | (1 << 9);
450
451                 dat = cpu_to_be16(val);
452                 ret = ad5933_i2c_write(st->client,
453                                        AD5933_REG_SETTLING_CYCLES,
454                                        2, (u8 *)&dat);
455                 break;
456         case AD5933_FREQ_POINTS:
457                 val = clamp(val, (u16)0, (u16)511);
458                 st->freq_points = val;
459
460                 dat = cpu_to_be16(val);
461                 ret = ad5933_i2c_write(st->client, AD5933_REG_INC_NUM, 2,
462                                        (u8 *)&dat);
463                 break;
464         default:
465                 ret = -EINVAL;
466         }
467
468         mutex_unlock(&indio_dev->mlock);
469         return ret ? ret : len;
470 }
471
472 static IIO_DEVICE_ATTR(out_voltage0_scale, S_IRUGO | S_IWUSR,
473                         ad5933_show,
474                         ad5933_store,
475                         AD5933_OUT_RANGE);
476
477 static IIO_DEVICE_ATTR(out_voltage0_scale_available, S_IRUGO,
478                         ad5933_show,
479                         NULL,
480                         AD5933_OUT_RANGE_AVAIL);
481
482 static IIO_DEVICE_ATTR(in_voltage0_scale, S_IRUGO | S_IWUSR,
483                         ad5933_show,
484                         ad5933_store,
485                         AD5933_IN_PGA_GAIN);
486
487 static IIO_DEVICE_ATTR(in_voltage0_scale_available, S_IRUGO,
488                         ad5933_show,
489                         NULL,
490                         AD5933_IN_PGA_GAIN_AVAIL);
491
492 static IIO_DEVICE_ATTR(out_voltage0_freq_points, S_IRUGO | S_IWUSR,
493                         ad5933_show,
494                         ad5933_store,
495                         AD5933_FREQ_POINTS);
496
497 static IIO_DEVICE_ATTR(out_voltage0_settling_cycles, S_IRUGO | S_IWUSR,
498                         ad5933_show,
499                         ad5933_store,
500                         AD5933_OUT_SETTLING_CYCLES);
501
502 /* note:
503  * ideally we would handle the scale attributes via the iio_info
504  * (read|write)_raw methods, however this part is a untypical since we
505  * don't create dedicated sysfs channel attributes for out0 and in0.
506  */
507 static struct attribute *ad5933_attributes[] = {
508         &iio_dev_attr_out_voltage0_scale.dev_attr.attr,
509         &iio_dev_attr_out_voltage0_scale_available.dev_attr.attr,
510         &iio_dev_attr_out_voltage0_freq_start.dev_attr.attr,
511         &iio_dev_attr_out_voltage0_freq_increment.dev_attr.attr,
512         &iio_dev_attr_out_voltage0_freq_points.dev_attr.attr,
513         &iio_dev_attr_out_voltage0_settling_cycles.dev_attr.attr,
514         &iio_dev_attr_in_voltage0_scale.dev_attr.attr,
515         &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
516         NULL
517 };
518
519 static const struct attribute_group ad5933_attribute_group = {
520         .attrs = ad5933_attributes,
521 };
522
523 static int ad5933_read_raw(struct iio_dev *indio_dev,
524                            struct iio_chan_spec const *chan,
525                            int *val,
526                            int *val2,
527                            long m)
528 {
529         struct ad5933_state *st = iio_priv(indio_dev);
530         __be16 dat;
531         int ret;
532
533         switch (m) {
534         case IIO_CHAN_INFO_RAW:
535                 mutex_lock(&indio_dev->mlock);
536                 if (iio_buffer_enabled(indio_dev)) {
537                         ret = -EBUSY;
538                         goto out;
539                 }
540                 ret = ad5933_cmd(st, AD5933_CTRL_MEASURE_TEMP);
541                 if (ret < 0)
542                         goto out;
543                 ret = ad5933_wait_busy(st, AD5933_STAT_TEMP_VALID);
544                 if (ret < 0)
545                         goto out;
546
547                 ret = ad5933_i2c_read(st->client,
548                                       AD5933_REG_TEMP_DATA,
549                                       2, (u8 *)&dat);
550                 if (ret < 0)
551                         goto out;
552                 mutex_unlock(&indio_dev->mlock);
553                 *val = sign_extend32(be16_to_cpu(dat), 13);
554
555                 return IIO_VAL_INT;
556         case IIO_CHAN_INFO_SCALE:
557                 *val = 1000;
558                 *val2 = 5;
559                 return IIO_VAL_FRACTIONAL_LOG2;
560         }
561
562         return -EINVAL;
563 out:
564         mutex_unlock(&indio_dev->mlock);
565         return ret;
566 }
567
568 static const struct iio_info ad5933_info = {
569         .read_raw = ad5933_read_raw,
570         .attrs = &ad5933_attribute_group,
571         .driver_module = THIS_MODULE,
572 };
573
574 static int ad5933_ring_preenable(struct iio_dev *indio_dev)
575 {
576         struct ad5933_state *st = iio_priv(indio_dev);
577         int ret;
578
579         if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
580                 return -EINVAL;
581
582         ret = ad5933_reset(st);
583         if (ret < 0)
584                 return ret;
585
586         ret = ad5933_cmd(st, AD5933_CTRL_STANDBY);
587         if (ret < 0)
588                 return ret;
589
590         ret = ad5933_cmd(st, AD5933_CTRL_INIT_START_FREQ);
591         if (ret < 0)
592                 return ret;
593
594         st->state = AD5933_CTRL_INIT_START_FREQ;
595
596         return 0;
597 }
598
599 static int ad5933_ring_postenable(struct iio_dev *indio_dev)
600 {
601         struct ad5933_state *st = iio_priv(indio_dev);
602
603         /* AD5933_CTRL_INIT_START_FREQ:
604          * High Q complex circuits require a long time to reach steady state.
605          * To facilitate the measurement of such impedances, this mode allows
606          * the user full control of the settling time requirement before
607          * entering start frequency sweep mode where the impedance measurement
608          * takes place. In this mode the impedance is excited with the
609          * programmed start frequency (ad5933_ring_preenable),
610          * but no measurement takes place.
611          */
612
613         schedule_delayed_work(&st->work,
614                               msecs_to_jiffies(AD5933_INIT_EXCITATION_TIME_ms));
615         return 0;
616 }
617
618 static int ad5933_ring_postdisable(struct iio_dev *indio_dev)
619 {
620         struct ad5933_state *st = iio_priv(indio_dev);
621
622         cancel_delayed_work_sync(&st->work);
623         return ad5933_cmd(st, AD5933_CTRL_POWER_DOWN);
624 }
625
626 static const struct iio_buffer_setup_ops ad5933_ring_setup_ops = {
627         .preenable = ad5933_ring_preenable,
628         .postenable = ad5933_ring_postenable,
629         .postdisable = ad5933_ring_postdisable,
630 };
631
632 static int ad5933_register_ring_funcs_and_init(struct iio_dev *indio_dev)
633 {
634         struct iio_buffer *buffer;
635
636         buffer = iio_kfifo_allocate();
637         if (!buffer)
638                 return -ENOMEM;
639
640         iio_device_attach_buffer(indio_dev, buffer);
641
642         /* Ring buffer functions - here trigger setup related */
643         indio_dev->setup_ops = &ad5933_ring_setup_ops;
644
645         return 0;
646 }
647
648 static void ad5933_work(struct work_struct *work)
649 {
650         struct ad5933_state *st = container_of(work,
651                 struct ad5933_state, work.work);
652         struct iio_dev *indio_dev = i2c_get_clientdata(st->client);
653         __be16 buf[2];
654         int val[2];
655         unsigned char status;
656         int ret;
657
658         mutex_lock(&indio_dev->mlock);
659         if (st->state == AD5933_CTRL_INIT_START_FREQ) {
660                 /* start sweep */
661                 ad5933_cmd(st, AD5933_CTRL_START_SWEEP);
662                 st->state = AD5933_CTRL_START_SWEEP;
663                 schedule_delayed_work(&st->work, st->poll_time_jiffies);
664                 goto out;
665         }
666
667         ret = ad5933_i2c_read(st->client, AD5933_REG_STATUS, 1, &status);
668         if (ret)
669                 goto out;
670
671         if (status & AD5933_STAT_DATA_VALID) {
672                 int scan_count = bitmap_weight(indio_dev->active_scan_mask,
673                                                indio_dev->masklength);
674                 ret = ad5933_i2c_read(st->client,
675                                 test_bit(1, indio_dev->active_scan_mask) ?
676                                 AD5933_REG_REAL_DATA : AD5933_REG_IMAG_DATA,
677                                 scan_count * 2, (u8 *)buf);
678                 if (ret)
679                         goto out;
680
681                 if (scan_count == 2) {
682                         val[0] = be16_to_cpu(buf[0]);
683                         val[1] = be16_to_cpu(buf[1]);
684                 } else {
685                         val[0] = be16_to_cpu(buf[0]);
686                 }
687                 iio_push_to_buffers(indio_dev, val);
688         } else {
689                 /* no data available - try again later */
690                 schedule_delayed_work(&st->work, st->poll_time_jiffies);
691                 goto out;
692         }
693
694         if (status & AD5933_STAT_SWEEP_DONE) {
695                 /* last sample received - power down do
696                  * nothing until the ring enable is toggled
697                  */
698                 ad5933_cmd(st, AD5933_CTRL_POWER_DOWN);
699         } else {
700                 /* we just received a valid datum, move on to the next */
701                 ad5933_cmd(st, AD5933_CTRL_INC_FREQ);
702                 schedule_delayed_work(&st->work, st->poll_time_jiffies);
703         }
704 out:
705         mutex_unlock(&indio_dev->mlock);
706 }
707
708 static int ad5933_probe(struct i2c_client *client,
709                         const struct i2c_device_id *id)
710 {
711         int ret, voltage_uv = 0;
712         struct ad5933_platform_data *pdata = dev_get_platdata(&client->dev);
713         struct ad5933_state *st;
714         struct iio_dev *indio_dev;
715
716         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
717         if (!indio_dev)
718                 return -ENOMEM;
719
720         st = iio_priv(indio_dev);
721         i2c_set_clientdata(client, indio_dev);
722         st->client = client;
723
724         if (!pdata)
725                 pdata = &ad5933_default_pdata;
726
727         st->reg = devm_regulator_get(&client->dev, "vcc");
728         if (!IS_ERR(st->reg)) {
729                 ret = regulator_enable(st->reg);
730                 if (ret)
731                         return ret;
732                 voltage_uv = regulator_get_voltage(st->reg);
733         }
734
735         if (voltage_uv)
736                 st->vref_mv = voltage_uv / 1000;
737         else
738                 st->vref_mv = pdata->vref_mv;
739
740         if (pdata->ext_clk_Hz) {
741                 st->mclk_hz = pdata->ext_clk_Hz;
742                 st->ctrl_lb = AD5933_CTRL_EXT_SYSCLK;
743         } else {
744                 st->mclk_hz = AD5933_INT_OSC_FREQ_Hz;
745                 st->ctrl_lb = AD5933_CTRL_INT_SYSCLK;
746         }
747
748         ad5933_calc_out_ranges(st);
749         INIT_DELAYED_WORK(&st->work, ad5933_work);
750         st->poll_time_jiffies = msecs_to_jiffies(AD5933_POLL_TIME_ms);
751
752         indio_dev->dev.parent = &client->dev;
753         indio_dev->info = &ad5933_info;
754         indio_dev->name = id->name;
755         indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
756         indio_dev->channels = ad5933_channels;
757         indio_dev->num_channels = ARRAY_SIZE(ad5933_channels);
758
759         ret = ad5933_register_ring_funcs_and_init(indio_dev);
760         if (ret)
761                 goto error_disable_reg;
762
763         ret = ad5933_setup(st);
764         if (ret)
765                 goto error_unreg_ring;
766
767         ret = iio_device_register(indio_dev);
768         if (ret)
769                 goto error_unreg_ring;
770
771         return 0;
772
773 error_unreg_ring:
774         iio_kfifo_free(indio_dev->buffer);
775 error_disable_reg:
776         if (!IS_ERR(st->reg))
777                 regulator_disable(st->reg);
778
779         return ret;
780 }
781
782 static int ad5933_remove(struct i2c_client *client)
783 {
784         struct iio_dev *indio_dev = i2c_get_clientdata(client);
785         struct ad5933_state *st = iio_priv(indio_dev);
786
787         iio_device_unregister(indio_dev);
788         iio_kfifo_free(indio_dev->buffer);
789         if (!IS_ERR(st->reg))
790                 regulator_disable(st->reg);
791
792         return 0;
793 }
794
795 static const struct i2c_device_id ad5933_id[] = {
796         { "ad5933", 0 },
797         { "ad5934", 0 },
798         {}
799 };
800
801 MODULE_DEVICE_TABLE(i2c, ad5933_id);
802
803 static struct i2c_driver ad5933_driver = {
804         .driver = {
805                 .name = "ad5933",
806         },
807         .probe = ad5933_probe,
808         .remove = ad5933_remove,
809         .id_table = ad5933_id,
810 };
811 module_i2c_driver(ad5933_driver);
812
813 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
814 MODULE_DESCRIPTION("Analog Devices AD5933 Impedance Conv. Network Analyzer");
815 MODULE_LICENSE("GPL v2");