/drivers/staging/iio/dds/ad9834.c
C | 464 lines | 381 code | 72 blank | 11 comment | 65 complexity | 0cf290a6e03ad9ecf62c51499748b587 MD5 | raw file
- /*
- * AD9833/AD9834/AD9837/AD9838 SPI DDS driver
- *
- * Copyright 2010-2011 Analog Devices Inc.
- *
- * Licensed under the GPL-2.
- */
- #include <linux/interrupt.h>
- #include <linux/workqueue.h>
- #include <linux/device.h>
- #include <linux/kernel.h>
- #include <linux/slab.h>
- #include <linux/sysfs.h>
- #include <linux/list.h>
- #include <linux/spi/spi.h>
- #include <linux/regulator/consumer.h>
- #include <linux/err.h>
- #include <asm/div64.h>
- #include "../iio.h"
- #include "../sysfs.h"
- #include "dds.h"
- #include "ad9834.h"
- static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
- {
- unsigned long long freqreg = (u64) fout * (u64) (1 << AD9834_FREQ_BITS);
- do_div(freqreg, mclk);
- return freqreg;
- }
- static int ad9834_write_frequency(struct ad9834_state *st,
- unsigned long addr, unsigned long fout)
- {
- unsigned long regval;
- if (fout > (st->mclk / 2))
- return -EINVAL;
- regval = ad9834_calc_freqreg(st->mclk, fout);
- st->freq_data[0] = cpu_to_be16(addr | (regval &
- RES_MASK(AD9834_FREQ_BITS / 2)));
- st->freq_data[1] = cpu_to_be16(addr | ((regval >>
- (AD9834_FREQ_BITS / 2)) &
- RES_MASK(AD9834_FREQ_BITS / 2)));
- return spi_sync(st->spi, &st->freq_msg);
- }
- static int ad9834_write_phase(struct ad9834_state *st,
- unsigned long addr, unsigned long phase)
- {
- if (phase > (1 << AD9834_PHASE_BITS))
- return -EINVAL;
- st->data = cpu_to_be16(addr | phase);
- return spi_sync(st->spi, &st->msg);
- }
- static ssize_t ad9834_write(struct device *dev,
- struct device_attribute *attr,
- const char *buf,
- size_t len)
- {
- struct iio_dev *dev_info = dev_get_drvdata(dev);
- struct ad9834_state *st = iio_priv(dev_info);
- struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
- int ret;
- long val;
- ret = strict_strtoul(buf, 10, &val);
- if (ret)
- goto error_ret;
- mutex_lock(&dev_info->mlock);
- switch (this_attr->address) {
- case AD9834_REG_FREQ0:
- case AD9834_REG_FREQ1:
- ret = ad9834_write_frequency(st, this_attr->address, val);
- break;
- case AD9834_REG_PHASE0:
- case AD9834_REG_PHASE1:
- ret = ad9834_write_phase(st, this_attr->address, val);
- break;
- case AD9834_OPBITEN:
- if (st->control & AD9834_MODE) {
- ret = -EINVAL; /* AD9843 reserved mode */
- break;
- }
- if (val)
- st->control |= AD9834_OPBITEN;
- else
- st->control &= ~AD9834_OPBITEN;
- st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
- ret = spi_sync(st->spi, &st->msg);
- break;
- case AD9834_PIN_SW:
- if (val)
- st->control |= AD9834_PIN_SW;
- else
- st->control &= ~AD9834_PIN_SW;
- st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
- ret = spi_sync(st->spi, &st->msg);
- break;
- case AD9834_FSEL:
- case AD9834_PSEL:
- if (val == 0)
- st->control &= ~(this_attr->address | AD9834_PIN_SW);
- else if (val == 1) {
- st->control |= this_attr->address;
- st->control &= ~AD9834_PIN_SW;
- } else {
- ret = -EINVAL;
- break;
- }
- st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
- ret = spi_sync(st->spi, &st->msg);
- break;
- case AD9834_RESET:
- if (val)
- st->control &= ~AD9834_RESET;
- else
- st->control |= AD9834_RESET;
- st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
- ret = spi_sync(st->spi, &st->msg);
- break;
- default:
- ret = -ENODEV;
- }
- mutex_unlock(&dev_info->mlock);
- error_ret:
- return ret ? ret : len;
- }
- static ssize_t ad9834_store_wavetype(struct device *dev,
- struct device_attribute *attr,
- const char *buf,
- size_t len)
- {
- struct iio_dev *dev_info = dev_get_drvdata(dev);
- struct ad9834_state *st = iio_priv(dev_info);
- struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
- int ret = 0;
- bool is_ad9833_7 = (st->devid == ID_AD9833) || (st->devid == ID_AD9837);
- mutex_lock(&dev_info->mlock);
- switch (this_attr->address) {
- case 0:
- if (sysfs_streq(buf, "sine")) {
- st->control &= ~AD9834_MODE;
- if (is_ad9833_7)
- st->control &= ~AD9834_OPBITEN;
- } else if (sysfs_streq(buf, "triangle")) {
- if (is_ad9833_7) {
- st->control &= ~AD9834_OPBITEN;
- st->control |= AD9834_MODE;
- } else if (st->control & AD9834_OPBITEN) {
- ret = -EINVAL; /* AD9843 reserved mode */
- } else {
- st->control |= AD9834_MODE;
- }
- } else if (is_ad9833_7 && sysfs_streq(buf, "square")) {
- st->control &= ~AD9834_MODE;
- st->control |= AD9834_OPBITEN;
- } else {
- ret = -EINVAL;
- }
- break;
- case 1:
- if (sysfs_streq(buf, "square") &&
- !(st->control & AD9834_MODE)) {
- st->control &= ~AD9834_MODE;
- st->control |= AD9834_OPBITEN;
- } else {
- ret = -EINVAL;
- }
- break;
- default:
- ret = -EINVAL;
- break;
- }
- if (!ret) {
- st->data = cpu_to_be16(AD9834_REG_CMD | st->control);
- ret = spi_sync(st->spi, &st->msg);
- }
- mutex_unlock(&dev_info->mlock);
- return ret ? ret : len;
- }
- static ssize_t ad9834_show_out0_wavetype_available(struct device *dev,
- struct device_attribute *attr,
- char *buf)
- {
- struct iio_dev *dev_info = dev_get_drvdata(dev);
- struct ad9834_state *st = iio_priv(dev_info);
- char *str;
- if ((st->devid == ID_AD9833) || (st->devid == ID_AD9837))
- str = "sine triangle square";
- else if (st->control & AD9834_OPBITEN)
- str = "sine";
- else
- str = "sine triangle";
- return sprintf(buf, "%s\n", str);
- }
- static IIO_DEVICE_ATTR(dds0_out0_wavetype_available, S_IRUGO,
- ad9834_show_out0_wavetype_available, NULL, 0);
- static ssize_t ad9834_show_out1_wavetype_available(struct device *dev,
- struct device_attribute *attr,
- char *buf)
- {
- struct iio_dev *dev_info = dev_get_drvdata(dev);
- struct ad9834_state *st = iio_priv(dev_info);
- char *str;
- if (st->control & AD9834_MODE)
- str = "";
- else
- str = "square";
- return sprintf(buf, "%s\n", str);
- }
- static IIO_DEVICE_ATTR(dds0_out1_wavetype_available, S_IRUGO,
- ad9834_show_out1_wavetype_available, NULL, 0);
- /**
- * see dds.h for further information
- */
- static IIO_DEV_ATTR_FREQ(0, 0, S_IWUSR, NULL, ad9834_write, AD9834_REG_FREQ0);
- static IIO_DEV_ATTR_FREQ(0, 1, S_IWUSR, NULL, ad9834_write, AD9834_REG_FREQ1);