Release 4.11 drivers/staging/iio/frequency/ad9832.c
/*
* AD9832 SPI DDS driver
*
* Copyright 2011 Analog Devices Inc.
*
* Licensed under the GPL-2.
*/
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/spi/spi.h>
#include <linux/regulator/consumer.h>
#include <linux/err.h>
#include <linux/module.h>
#include <asm/div64.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include "dds.h"
#include "ad9832.h"
static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout)
{
unsigned long long freqreg = (u64)fout *
(u64)((u64)1L << AD9832_FREQ_BITS);
do_div(freqreg, mclk);
return freqreg;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Michael Hennerich | 44 | 93.62% | 1 | 50.00% |
Cliff Cai | 3 | 6.38% | 1 | 50.00% |
Total | 47 | 100.00% | 2 | 100.00% |
static int ad9832_write_frequency(struct ad9832_state *st,
unsigned int addr, unsigned long fout)
{
unsigned long regval;
if (fout > (st->mclk / 2))
return -EINVAL;
regval = ad9832_calc_freqreg(st->mclk, fout);
st->freq_data[0] = cpu_to_be16((AD9832_CMD_FRE8BITSW << CMD_SHIFT) |
(addr << ADD_SHIFT) |
((regval >> 24) & 0xFF));
st->freq_data[1] = cpu_to_be16((AD9832_CMD_FRE16BITSW << CMD_SHIFT) |
((addr - 1) << ADD_SHIFT) |
((regval >> 16) & 0xFF));
st->freq_data[2] = cpu_to_be16((AD9832_CMD_FRE8BITSW << CMD_SHIFT) |
((addr - 2) << ADD_SHIFT) |
((regval >> 8) & 0xFF));
st->freq_data[3] = cpu_to_be16((AD9832_CMD_FRE16BITSW << CMD_SHIFT) |
((addr - 3) << ADD_SHIFT) |
((regval >> 0) & 0xFF));
return spi_sync(st->spi, &st->freq_msg);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Michael Hennerich | 142 | 69.95% | 1 | 33.33% |
Cliff Cai | 60 | 29.56% | 1 | 33.33% |
Svetlana Orlik | 1 | 0.49% | 1 | 33.33% |
Total | 203 | 100.00% | 3 | 100.00% |
static int ad9832_write_phase(struct ad9832_state *st,
unsigned long addr, unsigned long phase)
{
if (phase > BIT(AD9832_PHASE_BITS))
return -EINVAL;
st->phase_data[0] = cpu_to_be16((AD9832_CMD_PHA8BITSW << CMD_SHIFT) |
(addr << ADD_SHIFT) |
((phase >> 8) & 0xFF));
st->phase_data[1] = cpu_to_be16((AD9832_CMD_PHA16BITSW << CMD_SHIFT) |
((addr - 1) << ADD_SHIFT) |
(phase & 0xFF));
return spi_sync(st->spi, &st->phase_msg);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Michael Hennerich | 84 | 77.06% | 1 | 33.33% |
Cliff Cai | 22 | 20.18% | 1 | 33.33% |
Cristina Opriceana | 3 | 2.75% | 1 | 33.33% |
Total | 109 | 100.00% | 3 | 100.00% |
static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
const char *buf, size_t len)
{
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
struct ad9832_state *st = iio_priv(indio_dev);
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
int ret;
unsigned long val;
ret = kstrtoul(buf, 10, &val);
if (ret)
goto error_ret;
mutex_lock(&indio_dev->mlock);
switch ((u32)this_attr->address) {
case AD9832_FREQ0HM:
case AD9832_FREQ1HM:
ret = ad9832_write_frequency(st, this_attr->address, val);
break;
case AD9832_PHASE0H:
case AD9832_PHASE1H:
case AD9832_PHASE2H:
case AD9832_PHASE3H:
ret = ad9832_write_phase(st, this_attr->address, val);
break;
case AD9832_PINCTRL_EN:
if (val)
st->ctrl_ss &= ~AD9832_SELSRC;
else
st->ctrl_ss |= AD9832_SELSRC;
st->data = cpu_to_be16((AD9832_CMD_SYNCSELSRC << CMD_SHIFT) |
st->ctrl_ss);
ret = spi_sync(st->spi, &st->msg);
break;
case AD9832_FREQ_SYM:
if (val == 1) {
st->ctrl_fp |= AD9832_FREQ;
} else if (val == 0) {
st->ctrl_fp &= ~AD9832_FREQ;
} else {
ret = -EINVAL;
break;
}
st->data = cpu_to_be16((AD9832_CMD_FPSELECT << CMD_SHIFT) |
st->ctrl_fp);
ret = spi_sync(st->spi, &st->msg);
break;
case AD9832_PHASE_SYM:
if (val > 3) {
ret = -EINVAL;
break;
}
st->ctrl_fp &= ~AD9832_PHASE(3);
st->ctrl_fp |= AD9832_PHASE(val);
st->data = cpu_to_be16((AD9832_CMD_FPSELECT << CMD_SHIFT) |
st->ctrl_fp);
ret = spi_sync(st->spi, &st->msg);
break;
case AD9832_OUTPUT_EN:
if (val)
st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP |
AD9832_CLR);
else
st->ctrl_src |= AD9832_RESET;
st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
st->ctrl_src);
ret = spi_sync(st->spi, &st->msg);
break;
default:
ret = -ENODEV;
}
mutex_unlock(&indio_dev->mlock);
error_ret:
return ret ? ret : len;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Michael Hennerich | 383 | 90.76% | 2 | 25.00% |
Cliff Cai | 25 | 5.92% | 1 | 12.50% |
Jonathan Cameron | 7 | 1.66% | 2 | 25.00% |
Federico Di Pierro | 4 | 0.95% | 1 | 12.50% |
Jingoo Han | 2 | 0.47% | 1 | 12.50% |
Lars-Peter Clausen | 1 | 0.24% | 1 | 12.50% |
Total | 422 | 100.00% | 8 | 100.00% |
/**
* see dds.h for further information
*/
static IIO_DEV_ATTR_FREQ(0, 0, S_IWUSR, NULL, ad9832_write, AD9832_FREQ0HM);
static IIO_DEV_ATTR_FREQ(0, 1, S_IWUSR, NULL, ad9832_write, AD9832_FREQ1HM);
static IIO_DEV_ATTR_FREQSYMBOL(0, S_IWUSR, NULL, ad9832_write, AD9832_FREQ_SYM);
static IIO_CONST_ATTR_FREQ_SCALE(0, "1"); /* 1Hz */
static IIO_DEV_ATTR_PHASE(0, 0, S_IWUSR, NULL, ad9832_write, AD9832_PHASE0H);
static IIO_DEV_ATTR_PHASE(0, 1, S_IWUSR, NULL, ad9832_write, AD9832_PHASE1H);
static IIO_DEV_ATTR_PHASE(0, 2, S_IWUSR, NULL, ad9832_write, AD9832_PHASE2H);
static IIO_DEV_ATTR_PHASE(0, 3, S_IWUSR, NULL, ad9832_write, AD9832_PHASE3H);
static IIO_DEV_ATTR_PHASESYMBOL(0, S_IWUSR, NULL,
ad9832_write, AD9832_PHASE_SYM);
static IIO_CONST_ATTR_PHASE_SCALE(0, "0.0015339808"); /* 2PI/2^12 rad*/
static IIO_DEV_ATTR_PINCONTROL_EN(0, S_IWUSR, NULL,
ad9832_write, AD9832_PINCTRL_EN);
static IIO_DEV_ATTR_OUT_ENABLE(0, S_IWUSR, NULL,
ad9832_write, AD9832_OUTPUT_EN);
static struct attribute *ad9832_attributes[] = {
&iio_dev_attr_out_altvoltage0_frequency0.dev_attr.attr,
&iio_dev_attr_out_altvoltage0_frequency1.dev_attr.attr,
&iio_const_attr_out_altvoltage0_frequency_scale.dev_attr.attr,
&iio_dev_attr_out_altvoltage0_phase0.dev_attr.attr,
&iio_dev_attr_out_altvoltage0_phase1.dev_attr.attr,
&iio_dev_attr_out_altvoltage0_phase2.dev_attr.attr,
&iio_dev_attr_out_altvoltage0_phase3.dev_attr.attr,
&iio_const_attr_out_altvoltage0_phase_scale.dev_attr.attr,
&iio_dev_attr_out_altvoltage0_pincontrol_en.dev_attr.attr,
&iio_dev_attr_out_altvoltage0_frequencysymbol.dev_attr.attr,
&iio_dev_attr_out_altvoltage0_phasesymbol.dev_attr.attr,
&iio_dev_attr_out_altvoltage0_out_enable.dev_attr.attr,
NULL,
};
static const struct attribute_group ad9832_attribute_group = {
.attrs = ad9832_attributes,
};
static const struct iio_info ad9832_info = {
.attrs = &ad9832_attribute_group,
.driver_module = THIS_MODULE,
};
static int ad9832_probe(struct spi_device *spi)
{
struct ad9832_platform_data *pdata = dev_get_platdata(&spi->dev);
struct iio_dev *indio_dev;
struct ad9832_state *st;
int ret;
if (!pdata) {
dev_dbg(&spi->dev, "no platform data?\n");
return -ENODEV;
}
indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
if (!indio_dev)
return -ENOMEM;
spi_set_drvdata(spi, indio_dev);
st = iio_priv(indio_dev);
st->avdd = devm_regulator_get(&spi->dev, "avdd");
if (IS_ERR(st->avdd))
return PTR_ERR(st->avdd);
ret = regulator_enable(st->avdd);
if (ret) {
dev_err(&spi->dev, "Failed to enable specified AVDD supply\n");
return ret;
}
st->dvdd = devm_regulator_get(&spi->dev, "dvdd");
if (IS_ERR(st->dvdd)) {
ret = PTR_ERR(st->dvdd);
goto error_disable_avdd;
}
ret = regulator_enable(st->dvdd);
if (ret) {
dev_err(&spi->dev, "Failed to enable specified DVDD supply\n");
goto error_disable_avdd;
}
st->mclk = pdata->mclk;
st->spi = spi;
indio_dev->dev.parent = &spi->dev;
indio_dev->name = spi_get_device_id(spi)->name;
indio_dev->info = &ad9832_info;
indio_dev->modes = INDIO_DIRECT_MODE;
/* Setup default messages */
st->xfer.tx_buf = &st->data;
st->xfer.len = 2;
spi_message_init(&st->msg);
spi_message_add_tail(&st->xfer, &st->msg);
st->freq_xfer[0].tx_buf = &st->freq_data[0];
st->freq_xfer[0].len = 2;
st->freq_xfer[0].cs_change = 1;
st->freq_xfer[1].tx_buf = &st->freq_data[1];
st->freq_xfer[1].len = 2;
st->freq_xfer[1].cs_change = 1;
st->freq_xfer[2].tx_buf = &st->freq_data[2];
st->freq_xfer[2].len = 2;
st->freq_xfer[2].cs_change = 1;
st->freq_xfer[3].tx_buf = &st->freq_data[3];
st->freq_xfer[3].len = 2;
spi_message_init(&st->freq_msg);
spi_message_add_tail(&st->freq_xfer[0], &st->freq_msg);
spi_message_add_tail(&st->freq_xfer[1], &st->freq_msg);
spi_message_add_tail(&st->freq_xfer[2], &st->freq_msg);
spi_message_add_tail(&st->freq_xfer[3], &st->freq_msg);
st->phase_xfer[0].tx_buf = &st->phase_data[0];
st->phase_xfer[0].len = 2;
st->phase_xfer[0].cs_change = 1;
st->phase_xfer[1].tx_buf = &st->phase_data[1];
st->phase_xfer[1].len = 2;
spi_message_init(&st->phase_msg);
spi_message_add_tail(&st->phase_xfer[0], &st->phase_msg);
spi_message_add_tail(&st->phase_xfer[1], &st->phase_msg);
st->ctrl_src = AD9832_SLEEP | AD9832_RESET | AD9832_CLR;
st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
st->ctrl_src);
ret = spi_sync(st->spi, &st->msg);
if (ret) {
dev_err(&spi->dev, "device init failed\n");
goto error_disable_dvdd;
}
ret = ad9832_write_frequency(st, AD9832_FREQ0HM, pdata->freq0);
if (ret)
goto error_disable_dvdd;
ret = ad9832_write_frequency(st, AD9832_FREQ1HM, pdata->freq1);
if (ret)
goto error_disable_dvdd;
ret = ad9832_write_phase(st, AD9832_PHASE0H, pdata->phase0);
if (ret)
goto error_disable_dvdd;
ret = ad9832_write_phase(st, AD9832_PHASE1H, pdata->phase1);
if (ret)
goto error_disable_dvdd;
ret = ad9832_write_phase(st, AD9832_PHASE2H, pdata->phase2);
if (ret)
goto error_disable_dvdd;
ret = ad9832_write_phase(st, AD9832_PHASE3H, pdata->phase3);
if (ret)
goto error_disable_dvdd;
ret = iio_device_register(indio_dev);
if (ret)
goto error_disable_dvdd;
return 0;
error_disable_dvdd:
regulator_disable(st->dvdd);
error_disable_avdd:
regulator_disable(st->avdd);
return ret;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Michael Hennerich | 464 | 54.40% | 1 | 8.33% |
Cliff Cai | 197 | 23.09% | 1 | 8.33% |
Eva Rachel Retuya | 115 | 13.48% | 4 | 33.33% |
Arnd Bergmann | 39 | 4.57% | 1 | 8.33% |
Jonathan Cameron | 30 | 3.52% | 3 | 25.00% |
Nizam Haider | 4 | 0.47% | 1 | 8.33% |
Sachin Kamat | 4 | 0.47% | 1 | 8.33% |
Total | 853 | 100.00% | 12 | 100.00% |
static int ad9832_remove(struct spi_device *spi)
{
struct iio_dev *indio_dev = spi_get_drvdata(spi);
struct ad9832_state *st = iio_priv(indio_dev);
iio_device_unregister(indio_dev);
regulator_disable(st->dvdd);
regulator_disable(st->avdd);
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Cliff Cai | 23 | 43.40% | 1 | 16.67% |
Jonathan Cameron | 15 | 28.30% | 2 | 33.33% |
Eva Rachel Retuya | 8 | 15.09% | 2 | 33.33% |
Michael Hennerich | 7 | 13.21% | 1 | 16.67% |
Total | 53 | 100.00% | 6 | 100.00% |
static const struct spi_device_id ad9832_id[] = {
{"ad9832", 0},
{"ad9835", 0},
{}
};
MODULE_DEVICE_TABLE(spi, ad9832_id);
static struct spi_driver ad9832_driver = {
.driver = {
.name = "ad9832",
},
.probe = ad9832_probe,
.remove = ad9832_remove,
.id_table = ad9832_id,
};
module_spi_driver(ad9832_driver);
MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
MODULE_DESCRIPTION("Analog Devices AD9832/AD9835 DDS");
MODULE_LICENSE("GPL v2");
Overall Contributors
Person | Tokens | Prop | Commits | CommitProp |
Michael Hennerich | 1452 | 68.91% | 3 | 12.00% |
Cliff Cai | 390 | 18.51% | 1 | 4.00% |
Eva Rachel Retuya | 123 | 5.84% | 4 | 16.00% |
Jonathan Cameron | 73 | 3.46% | 6 | 24.00% |
Arnd Bergmann | 39 | 1.85% | 1 | 4.00% |
Lars-Peter Clausen | 9 | 0.43% | 3 | 12.00% |
Nizam Haider | 4 | 0.19% | 1 | 4.00% |
Federico Di Pierro | 4 | 0.19% | 1 | 4.00% |
Sachin Kamat | 4 | 0.19% | 1 | 4.00% |
Cristina Opriceana | 3 | 0.14% | 1 | 4.00% |
Paul Gortmaker | 3 | 0.14% | 1 | 4.00% |
Jingoo Han | 2 | 0.09% | 1 | 4.00% |
Svetlana Orlik | 1 | 0.05% | 1 | 4.00% |
Total | 2107 | 100.00% | 25 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.