2 * AD5933 AD5934 Impedance Converter, Network Analyzer
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
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>
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>
25 /* AD5933/AD5934 Registers */
26 #define AD5933_REG_CONTROL_HB 0x80 /* R/W, 1 byte */
27 #define AD5933_REG_CONTROL_LB 0x81 /* R/W, 1 byte */
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*/
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)
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)
52 #define AD5933_CTRL_PGA_GAIN_1 (0x1 << 0)
53 #define AD5933_CTRL_PGA_GAIN_5 (0x0 << 0)
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)
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)
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
71 #define AD5933_INT_OSC_FREQ_Hz 16776000
72 #define AD5933_MAX_OUTPUT_FREQ_Hz 100000
73 #define AD5933_MAX_RETRIES 100
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
82 #define AD5933_POLL_TIME_ms 10
83 #define AD5933_INIT_EXCITATION_TIME_ms 100
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
92 struct ad5933_platform_data {
93 unsigned long ext_clk_Hz;
94 unsigned short vref_mv;
98 struct i2c_client *client;
99 struct regulator *reg;
100 struct delayed_work work;
101 struct mutex lock; /* Protect sensor state */
102 unsigned long mclk_hz;
103 unsigned char ctrl_hb;
104 unsigned char ctrl_lb;
105 unsigned int range_avail[4];
106 unsigned short vref_mv;
107 unsigned short settling_cycles;
108 unsigned short freq_points;
109 unsigned int freq_start;
110 unsigned int freq_inc;
112 unsigned int poll_time_jiffies;
115 static struct ad5933_platform_data ad5933_default_pdata = {
119 #define AD5933_CHANNEL(_type, _extend_name, _info_mask_separate, _address, \
120 _scan_index, _realbits) { \
122 .extend_name = (_extend_name), \
123 .info_mask_separate = (_info_mask_separate), \
124 .address = (_address), \
125 .scan_index = (_scan_index), \
128 .realbits = (_realbits), \
133 static const struct iio_chan_spec ad5933_channels[] = {
134 AD5933_CHANNEL(IIO_TEMP, NULL, BIT(IIO_CHAN_INFO_RAW) |
135 BIT(IIO_CHAN_INFO_SCALE), AD5933_REG_TEMP_DATA, -1, 14),
137 AD5933_CHANNEL(IIO_VOLTAGE, "real", 0, AD5933_REG_REAL_DATA, 0, 16),
138 AD5933_CHANNEL(IIO_VOLTAGE, "imag", 0, AD5933_REG_IMAG_DATA, 1, 16),
141 static int ad5933_i2c_write(struct i2c_client *client, u8 reg, u8 len, u8 *data)
146 ret = i2c_smbus_write_byte_data(client, reg++, *data++);
148 dev_err(&client->dev, "I2C write error\n");
155 static int ad5933_i2c_read(struct i2c_client *client, u8 reg, u8 len, u8 *data)
160 ret = i2c_smbus_read_byte_data(client, reg++);
162 dev_err(&client->dev, "I2C read error\n");
170 static int ad5933_cmd(struct ad5933_state *st, unsigned char cmd)
172 unsigned char dat = st->ctrl_hb | cmd;
174 return ad5933_i2c_write(st->client,
175 AD5933_REG_CONTROL_HB, 1, &dat);
178 static int ad5933_reset(struct ad5933_state *st)
180 unsigned char dat = st->ctrl_lb | AD5933_CTRL_RESET;
182 return ad5933_i2c_write(st->client,
183 AD5933_REG_CONTROL_LB, 1, &dat);
186 static int ad5933_wait_busy(struct ad5933_state *st, unsigned char event)
188 unsigned char val, timeout = AD5933_MAX_RETRIES;
192 ret = ad5933_i2c_read(st->client, AD5933_REG_STATUS, 1, &val);
204 static int ad5933_set_freq(struct ad5933_state *st,
205 unsigned int reg, unsigned long freq)
207 unsigned long long freqreg;
213 freqreg = (u64) freq * (u64) (1 << 27);
214 do_div(freqreg, st->mclk_hz / 4);
217 case AD5933_REG_FREQ_START:
218 st->freq_start = freq;
220 case AD5933_REG_FREQ_INC:
227 dat.d32 = cpu_to_be32(freqreg);
228 return ad5933_i2c_write(st->client, reg, 3, &dat.d8[1]);
231 static int ad5933_setup(struct ad5933_state *st)
236 ret = ad5933_reset(st);
240 ret = ad5933_set_freq(st, AD5933_REG_FREQ_START, 10000);
244 ret = ad5933_set_freq(st, AD5933_REG_FREQ_INC, 200);
248 st->settling_cycles = 10;
249 dat = cpu_to_be16(st->settling_cycles);
251 ret = ad5933_i2c_write(st->client,
252 AD5933_REG_SETTLING_CYCLES,
257 st->freq_points = 100;
258 dat = cpu_to_be16(st->freq_points);
260 return ad5933_i2c_write(st->client, AD5933_REG_INC_NUM, 2, (u8 *)&dat);
263 static void ad5933_calc_out_ranges(struct ad5933_state *st)
266 unsigned int normalized_3v3[4] = {1980, 198, 383, 970};
268 for (i = 0; i < 4; i++)
269 st->range_avail[i] = normalized_3v3[i] * st->vref_mv / 3300;
274 * handles: AD5933_REG_FREQ_START and AD5933_REG_FREQ_INC
277 static ssize_t ad5933_show_frequency(struct device *dev,
278 struct device_attribute *attr,
281 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
282 struct ad5933_state *st = iio_priv(indio_dev);
283 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
285 unsigned long long freqreg;
291 ret = iio_device_claim_direct_mode(indio_dev);
294 ret = ad5933_i2c_read(st->client, this_attr->address, 3, &dat.d8[1]);
295 iio_device_release_direct_mode(indio_dev);
299 freqreg = be32_to_cpu(dat.d32) & 0xFFFFFF;
301 freqreg = (u64)freqreg * (u64)(st->mclk_hz / 4);
302 do_div(freqreg, 1 << 27);
304 return sprintf(buf, "%d\n", (int)freqreg);
307 static ssize_t ad5933_store_frequency(struct device *dev,
308 struct device_attribute *attr,
312 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
313 struct ad5933_state *st = iio_priv(indio_dev);
314 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
318 ret = kstrtoul(buf, 10, &val);
322 if (val > AD5933_MAX_OUTPUT_FREQ_Hz)
325 ret = iio_device_claim_direct_mode(indio_dev);
328 ret = ad5933_set_freq(st, this_attr->address, val);
329 iio_device_release_direct_mode(indio_dev);
331 return ret ? ret : len;
334 static IIO_DEVICE_ATTR(out_voltage0_freq_start, 0644,
335 ad5933_show_frequency,
336 ad5933_store_frequency,
337 AD5933_REG_FREQ_START);
339 static IIO_DEVICE_ATTR(out_voltage0_freq_increment, 0644,
340 ad5933_show_frequency,
341 ad5933_store_frequency,
342 AD5933_REG_FREQ_INC);
344 static ssize_t ad5933_show(struct device *dev,
345 struct device_attribute *attr,
348 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
349 struct ad5933_state *st = iio_priv(indio_dev);
350 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
351 int ret = 0, len = 0;
353 mutex_lock(&st->lock);
354 switch ((u32)this_attr->address) {
355 case AD5933_OUT_RANGE:
356 len = sprintf(buf, "%u\n",
357 st->range_avail[(st->ctrl_hb >> 1) & 0x3]);
359 case AD5933_OUT_RANGE_AVAIL:
360 len = sprintf(buf, "%u %u %u %u\n", st->range_avail[0],
361 st->range_avail[3], st->range_avail[2],
364 case AD5933_OUT_SETTLING_CYCLES:
365 len = sprintf(buf, "%d\n", st->settling_cycles);
367 case AD5933_IN_PGA_GAIN:
368 len = sprintf(buf, "%s\n",
369 (st->ctrl_hb & AD5933_CTRL_PGA_GAIN_1) ?
372 case AD5933_IN_PGA_GAIN_AVAIL:
373 len = sprintf(buf, "1 0.2\n");
375 case AD5933_FREQ_POINTS:
376 len = sprintf(buf, "%d\n", st->freq_points);
382 mutex_unlock(&st->lock);
383 return ret ? ret : len;
386 static ssize_t ad5933_store(struct device *dev,
387 struct device_attribute *attr,
391 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
392 struct ad5933_state *st = iio_priv(indio_dev);
393 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
398 if (this_attr->address != AD5933_IN_PGA_GAIN) {
399 ret = kstrtou16(buf, 10, &val);
404 ret = iio_device_claim_direct_mode(indio_dev);
407 mutex_lock(&st->lock);
408 switch ((u32)this_attr->address) {
409 case AD5933_OUT_RANGE:
411 for (i = 0; i < 4; i++)
412 if (val == st->range_avail[i]) {
413 st->ctrl_hb &= ~AD5933_CTRL_RANGE(0x3);
414 st->ctrl_hb |= AD5933_CTRL_RANGE(i);
415 ret = ad5933_cmd(st, 0);
419 case AD5933_IN_PGA_GAIN:
420 if (sysfs_streq(buf, "1")) {
421 st->ctrl_hb |= AD5933_CTRL_PGA_GAIN_1;
422 } else if (sysfs_streq(buf, "0.2")) {
423 st->ctrl_hb &= ~AD5933_CTRL_PGA_GAIN_1;
428 ret = ad5933_cmd(st, 0);
430 case AD5933_OUT_SETTLING_CYCLES:
431 val = clamp(val, (u16)0, (u16)0x7FF);
432 st->settling_cycles = val;
434 /* 2x, 4x handling, see datasheet */
436 val = (val >> 2) | (3 << 9);
438 val = (val >> 1) | (1 << 9);
440 dat = cpu_to_be16(val);
441 ret = ad5933_i2c_write(st->client,
442 AD5933_REG_SETTLING_CYCLES,
445 case AD5933_FREQ_POINTS:
446 val = clamp(val, (u16)0, (u16)511);
447 st->freq_points = val;
449 dat = cpu_to_be16(val);
450 ret = ad5933_i2c_write(st->client, AD5933_REG_INC_NUM, 2,
457 mutex_unlock(&st->lock);
458 iio_device_release_direct_mode(indio_dev);
459 return ret ? ret : len;
462 static IIO_DEVICE_ATTR(out_voltage0_scale, 0644,
467 static IIO_DEVICE_ATTR(out_voltage0_scale_available, 0444,
470 AD5933_OUT_RANGE_AVAIL);
472 static IIO_DEVICE_ATTR(in_voltage0_scale, 0644,
477 static IIO_DEVICE_ATTR(in_voltage0_scale_available, 0444,
480 AD5933_IN_PGA_GAIN_AVAIL);
482 static IIO_DEVICE_ATTR(out_voltage0_freq_points, 0644,
487 static IIO_DEVICE_ATTR(out_voltage0_settling_cycles, 0644,
490 AD5933_OUT_SETTLING_CYCLES);
493 * ideally we would handle the scale attributes via the iio_info
494 * (read|write)_raw methods, however this part is a untypical since we
495 * don't create dedicated sysfs channel attributes for out0 and in0.
497 static struct attribute *ad5933_attributes[] = {
498 &iio_dev_attr_out_voltage0_scale.dev_attr.attr,
499 &iio_dev_attr_out_voltage0_scale_available.dev_attr.attr,
500 &iio_dev_attr_out_voltage0_freq_start.dev_attr.attr,
501 &iio_dev_attr_out_voltage0_freq_increment.dev_attr.attr,
502 &iio_dev_attr_out_voltage0_freq_points.dev_attr.attr,
503 &iio_dev_attr_out_voltage0_settling_cycles.dev_attr.attr,
504 &iio_dev_attr_in_voltage0_scale.dev_attr.attr,
505 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
509 static const struct attribute_group ad5933_attribute_group = {
510 .attrs = ad5933_attributes,
513 static int ad5933_read_raw(struct iio_dev *indio_dev,
514 struct iio_chan_spec const *chan,
519 struct ad5933_state *st = iio_priv(indio_dev);
524 case IIO_CHAN_INFO_RAW:
525 ret = iio_device_claim_direct_mode(indio_dev);
528 ret = ad5933_cmd(st, AD5933_CTRL_MEASURE_TEMP);
531 ret = ad5933_wait_busy(st, AD5933_STAT_TEMP_VALID);
535 ret = ad5933_i2c_read(st->client,
536 AD5933_REG_TEMP_DATA,
540 iio_device_release_direct_mode(indio_dev);
541 *val = sign_extend32(be16_to_cpu(dat), 13);
544 case IIO_CHAN_INFO_SCALE:
547 return IIO_VAL_FRACTIONAL_LOG2;
552 iio_device_release_direct_mode(indio_dev);
556 static const struct iio_info ad5933_info = {
557 .read_raw = ad5933_read_raw,
558 .attrs = &ad5933_attribute_group,
561 static int ad5933_ring_preenable(struct iio_dev *indio_dev)
563 struct ad5933_state *st = iio_priv(indio_dev);
566 if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength))
569 ret = ad5933_reset(st);
573 ret = ad5933_cmd(st, AD5933_CTRL_STANDBY);
577 ret = ad5933_cmd(st, AD5933_CTRL_INIT_START_FREQ);
581 st->state = AD5933_CTRL_INIT_START_FREQ;
586 static int ad5933_ring_postenable(struct iio_dev *indio_dev)
588 struct ad5933_state *st = iio_priv(indio_dev);
590 /* AD5933_CTRL_INIT_START_FREQ:
591 * High Q complex circuits require a long time to reach steady state.
592 * To facilitate the measurement of such impedances, this mode allows
593 * the user full control of the settling time requirement before
594 * entering start frequency sweep mode where the impedance measurement
595 * takes place. In this mode the impedance is excited with the
596 * programmed start frequency (ad5933_ring_preenable),
597 * but no measurement takes place.
600 schedule_delayed_work(&st->work,
601 msecs_to_jiffies(AD5933_INIT_EXCITATION_TIME_ms));
605 static int ad5933_ring_postdisable(struct iio_dev *indio_dev)
607 struct ad5933_state *st = iio_priv(indio_dev);
609 cancel_delayed_work_sync(&st->work);
610 return ad5933_cmd(st, AD5933_CTRL_POWER_DOWN);
613 static const struct iio_buffer_setup_ops ad5933_ring_setup_ops = {
614 .preenable = ad5933_ring_preenable,
615 .postenable = ad5933_ring_postenable,
616 .postdisable = ad5933_ring_postdisable,
619 static int ad5933_register_ring_funcs_and_init(struct iio_dev *indio_dev)
621 struct iio_buffer *buffer;
623 buffer = iio_kfifo_allocate();
627 iio_device_attach_buffer(indio_dev, buffer);
629 /* Ring buffer functions - here trigger setup related */
630 indio_dev->setup_ops = &ad5933_ring_setup_ops;
635 static void ad5933_work(struct work_struct *work)
637 struct ad5933_state *st = container_of(work,
638 struct ad5933_state, work.work);
639 struct iio_dev *indio_dev = i2c_get_clientdata(st->client);
642 unsigned char status;
645 if (st->state == AD5933_CTRL_INIT_START_FREQ) {
647 ad5933_cmd(st, AD5933_CTRL_START_SWEEP);
648 st->state = AD5933_CTRL_START_SWEEP;
649 schedule_delayed_work(&st->work, st->poll_time_jiffies);
653 ret = ad5933_i2c_read(st->client, AD5933_REG_STATUS, 1, &status);
657 if (status & AD5933_STAT_DATA_VALID) {
658 int scan_count = bitmap_weight(indio_dev->active_scan_mask,
659 indio_dev->masklength);
660 ret = ad5933_i2c_read(st->client,
661 test_bit(1, indio_dev->active_scan_mask) ?
662 AD5933_REG_REAL_DATA : AD5933_REG_IMAG_DATA,
663 scan_count * 2, (u8 *)buf);
667 if (scan_count == 2) {
668 val[0] = be16_to_cpu(buf[0]);
669 val[1] = be16_to_cpu(buf[1]);
671 val[0] = be16_to_cpu(buf[0]);
673 iio_push_to_buffers(indio_dev, val);
675 /* no data available - try again later */
676 schedule_delayed_work(&st->work, st->poll_time_jiffies);
680 if (status & AD5933_STAT_SWEEP_DONE) {
681 /* last sample received - power down do
682 * nothing until the ring enable is toggled
684 ad5933_cmd(st, AD5933_CTRL_POWER_DOWN);
686 /* we just received a valid datum, move on to the next */
687 ad5933_cmd(st, AD5933_CTRL_INC_FREQ);
688 schedule_delayed_work(&st->work, st->poll_time_jiffies);
692 static int ad5933_probe(struct i2c_client *client,
693 const struct i2c_device_id *id)
695 int ret, voltage_uv = 0;
696 struct ad5933_platform_data *pdata = dev_get_platdata(&client->dev);
697 struct ad5933_state *st;
698 struct iio_dev *indio_dev;
700 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
704 st = iio_priv(indio_dev);
705 i2c_set_clientdata(client, indio_dev);
708 mutex_init(&st->lock);
711 pdata = &ad5933_default_pdata;
713 st->reg = devm_regulator_get(&client->dev, "vdd");
715 return PTR_ERR(st->reg);
717 ret = regulator_enable(st->reg);
719 dev_err(&client->dev, "Failed to enable specified VDD supply\n");
722 voltage_uv = regulator_get_voltage(st->reg);
725 st->vref_mv = voltage_uv / 1000;
727 st->vref_mv = pdata->vref_mv;
729 if (pdata->ext_clk_Hz) {
730 st->mclk_hz = pdata->ext_clk_Hz;
731 st->ctrl_lb = AD5933_CTRL_EXT_SYSCLK;
733 st->mclk_hz = AD5933_INT_OSC_FREQ_Hz;
734 st->ctrl_lb = AD5933_CTRL_INT_SYSCLK;
737 ad5933_calc_out_ranges(st);
738 INIT_DELAYED_WORK(&st->work, ad5933_work);
739 st->poll_time_jiffies = msecs_to_jiffies(AD5933_POLL_TIME_ms);
741 indio_dev->dev.parent = &client->dev;
742 indio_dev->info = &ad5933_info;
743 indio_dev->name = id->name;
744 indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
745 indio_dev->channels = ad5933_channels;
746 indio_dev->num_channels = ARRAY_SIZE(ad5933_channels);
748 ret = ad5933_register_ring_funcs_and_init(indio_dev);
750 goto error_disable_reg;
752 ret = ad5933_setup(st);
754 goto error_unreg_ring;
756 ret = iio_device_register(indio_dev);
758 goto error_unreg_ring;
763 iio_kfifo_free(indio_dev->buffer);
765 regulator_disable(st->reg);
770 static int ad5933_remove(struct i2c_client *client)
772 struct iio_dev *indio_dev = i2c_get_clientdata(client);
773 struct ad5933_state *st = iio_priv(indio_dev);
775 iio_device_unregister(indio_dev);
776 iio_kfifo_free(indio_dev->buffer);
777 regulator_disable(st->reg);
782 static const struct i2c_device_id ad5933_id[] = {
788 MODULE_DEVICE_TABLE(i2c, ad5933_id);
790 static struct i2c_driver ad5933_driver = {
794 .probe = ad5933_probe,
795 .remove = ad5933_remove,
796 .id_table = ad5933_id,
798 module_i2c_driver(ad5933_driver);
800 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
801 MODULE_DESCRIPTION("Analog Devices AD5933 Impedance Conv. Network Analyzer");
802 MODULE_LICENSE("GPL v2");