10 #include <linux/device.h>
11 #include <linux/kernel.h>
13 #include <linux/i2c.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <asm/div64.h>
25 #include "../ring_sw.h"
30 #define AD5933_REG_CONTROL_HB 0x80
31 #define AD5933_REG_CONTROL_LB 0x81
32 #define AD5933_REG_FREQ_START 0x82
33 #define AD5933_REG_FREQ_INC 0x85
34 #define AD5933_REG_INC_NUM 0x88
35 #define AD5933_REG_SETTLING_CYCLES 0x8A
36 #define AD5933_REG_STATUS 0x8F
37 #define AD5933_REG_TEMP_DATA 0x92
38 #define AD5933_REG_REAL_DATA 0x94
39 #define AD5933_REG_IMAG_DATA 0x96
42 #define AD5933_CTRL_INIT_START_FREQ (0x1 << 4)
43 #define AD5933_CTRL_START_SWEEP (0x2 << 4)
44 #define AD5933_CTRL_INC_FREQ (0x3 << 4)
45 #define AD5933_CTRL_REPEAT_FREQ (0x4 << 4)
46 #define AD5933_CTRL_MEASURE_TEMP (0x9 << 4)
47 #define AD5933_CTRL_POWER_DOWN (0xA << 4)
48 #define AD5933_CTRL_STANDBY (0xB << 4)
50 #define AD5933_CTRL_RANGE_2000mVpp (0x0 << 1)
51 #define AD5933_CTRL_RANGE_200mVpp (0x1 << 1)
52 #define AD5933_CTRL_RANGE_400mVpp (0x2 << 1)
53 #define AD5933_CTRL_RANGE_1000mVpp (0x3 << 1)
54 #define AD5933_CTRL_RANGE(x) ((x) << 1)
56 #define AD5933_CTRL_PGA_GAIN_1 (0x1 << 0)
57 #define AD5933_CTRL_PGA_GAIN_5 (0x0 << 0)
60 #define AD5933_CTRL_RESET (0x1 << 4)
61 #define AD5933_CTRL_INT_SYSCLK (0x0 << 3)
62 #define AD5933_CTRL_EXT_SYSCLK (0x1 << 3)
65 #define AD5933_STAT_TEMP_VALID (0x1 << 0)
66 #define AD5933_STAT_DATA_VALID (0x1 << 1)
67 #define AD5933_STAT_SWEEP_DONE (0x1 << 2)
70 #define AD5933_I2C_BLOCK_WRITE 0xA0
71 #define AD5933_I2C_BLOCK_READ 0xA1
72 #define AD5933_I2C_ADDR_POINTER 0xB0
75 #define AD5933_INT_OSC_FREQ_Hz 16776000
76 #define AD5933_MAX_OUTPUT_FREQ_Hz 100000
77 #define AD5933_MAX_RETRIES 100
79 #define AD5933_OUT_RANGE 1
80 #define AD5933_OUT_RANGE_AVAIL 2
81 #define AD5933_OUT_SETTLING_CYCLES 3
82 #define AD5933_IN_PGA_GAIN 4
83 #define AD5933_IN_PGA_GAIN_AVAIL 5
84 #define AD5933_FREQ_POINTS 6
86 #define AD5933_POLL_TIME_ms 10
87 #define AD5933_INIT_EXCITATION_TIME_ms 100
127 .extend_name =
"real_raw",
141 .extend_name =
"imag_raw",
169 static int ad5933_i2c_read(
struct i2c_client *client,
189 return ad5933_i2c_write(st->
client,
196 return ad5933_i2c_write(st->
client,
219 unsigned reg,
unsigned long freq)
221 unsigned long long freqreg;
227 freqreg = (
u64) freq * (
u64) (1 << 27);
242 return ad5933_i2c_write(st->
client, reg, 3, &
dat.d8[1]);
250 ret = ad5933_reset(st);
265 ret = ad5933_i2c_write(st->
client,
276 static void ad5933_calc_out_ranges(
struct ad5933_state *st)
279 unsigned normalized_3v3[4] = {1980, 198, 383, 970};
281 for (i = 0; i < 4; i++)
294 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
298 unsigned long long freqreg;
305 ret = ad5933_i2c_read(st->
client, this_attr->
address, 3, &dat.d8[1]);
315 return sprintf(buf,
"%d\n", (
int) freqreg);
323 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
337 ret = ad5933_set_freq(st, this_attr->
address, val);
340 return ret ? ret : len;
344 ad5933_show_frequency,
345 ad5933_store_frequency,
349 ad5933_show_frequency,
350 ad5933_store_frequency,
357 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
360 int ret = 0, len = 0;
392 return ret ? ret : len;
400 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
416 for (i = 0; i < 4; i++)
420 ret = ad5933_cmd(st, 0);
434 ret = ad5933_cmd(st, 0);
437 val =
clamp(val, 0L, 0x7FFL);
442 val = (val >> 1) | (1 << 9);
444 val = (val >> 2) | (3 << 9);
447 ret = ad5933_i2c_write(st->
client,
451 val =
clamp(val, 0L, 511L);
463 return ret ? ret : len;
501 static struct attribute *ad5933_attributes[] = {
502 &iio_dev_attr_out_voltage0_scale.dev_attr.attr,
503 &iio_dev_attr_out_voltage0_scale_available.dev_attr.attr,
504 &iio_dev_attr_out_voltage0_freq_start.dev_attr.attr,
505 &iio_dev_attr_out_voltage0_freq_increment.dev_attr.attr,
506 &iio_dev_attr_out_voltage0_freq_points.dev_attr.attr,
507 &iio_dev_attr_out_voltage0_settling_cycles.dev_attr.attr,
508 &iio_dev_attr_in_voltage0_scale.dev_attr.attr,
509 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
514 .attrs = ad5933_attributes,
517 static int ad5933_read_raw(
struct iio_dev *indio_dev,
531 if (iio_buffer_enabled(indio_dev)) {
542 ret = ad5933_i2c_read(st->
client,
551 *val = ret * 1000 / 32;
553 *val = (ret - 16384) * 1000 / 32;
563 static const struct iio_info ad5933_info = {
564 .read_raw = &ad5933_read_raw,
565 .attrs = &ad5933_attribute_group,
569 static int ad5933_ring_preenable(
struct iio_dev *indio_dev)
581 ret = ad5933_reset(st);
598 static int ad5933_ring_postenable(
struct iio_dev *indio_dev)
617 static int ad5933_ring_postdisable(
struct iio_dev *indio_dev)
626 .preenable = &ad5933_ring_preenable,
627 .postenable = &ad5933_ring_postenable,
628 .postdisable = &ad5933_ring_postdisable,
631 static int ad5933_register_ring_funcs_and_init(
struct iio_dev *indio_dev)
638 indio_dev->
setup_ops = &ad5933_ring_setup_ops;
669 ad5933_i2c_read(st->
client,
672 scan_count * 2, (
u8 *)buf);
674 if (scan_count == 2) {
705 int ret, voltage_uv = 0;
709 if (indio_dev ==
NULL)
712 st = iio_priv(indio_dev);
713 i2c_set_clientdata(client, indio_dev);
717 st->
pdata = &ad5933_default_pdata;
722 if (!IS_ERR(st->
reg)) {
730 st->
vref_mv = voltage_uv / 1000;
734 if (st->
pdata->ext_clk_Hz) {
742 ad5933_calc_out_ranges(st);
746 indio_dev->
dev.parent = &client->
dev;
747 indio_dev->
info = &ad5933_info;
748 indio_dev->
name =
id->name;
750 indio_dev->
channels = ad5933_channels;
753 ret = ad5933_register_ring_funcs_and_init(indio_dev);
755 goto error_disable_reg;
760 goto error_unreg_ring;
766 ret = ad5933_setup(st);
768 goto error_uninitialize_ring;
772 goto error_uninitialize_ring;
776 error_uninitialize_ring:
781 if (!IS_ERR(st->
reg))
784 if (!IS_ERR(st->
reg))
794 struct iio_dev *indio_dev = i2c_get_clientdata(client);
800 if (!IS_ERR(st->
reg)) {
821 .probe = ad5933_probe,
823 .id_table = ad5933_id,