2 * Driver for ADI Direct Digital Synthesis ad9852
4 * Copyright (c) 2010 Analog Devices Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/types.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/module.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
22 #define DRV_NAME "ad9852"
24 #define addr_phaad1 0x0
25 #define addr_phaad2 0x1
26 #define addr_fretu1 0x2
27 #define addr_fretu2 0x3
28 #define addr_delfre 0x4
29 #define addr_updclk 0x5
30 #define addr_ramclk 0x6
31 #define addr_contrl 0x7
32 #define addr_optskm 0x8
33 #define addr_optskr 0xa
34 #define addr_dacctl 0xb
36 #define COMPPD (1 << 4)
37 #define REFMULT2 (1 << 2)
38 #define BYPPLL (1 << 5)
39 #define PLLRANG (1 << 6)
41 #define OSKEN (1 << 5)
43 #define read_bit (1 << 7)
45 /* Register format: 1 byte addr + value */
46 struct ad9852_config {
62 struct spi_device *sdev;
65 static ssize_t ad9852_set_parameter(struct device *dev,
66 struct device_attribute *attr,
70 struct spi_transfer xfer;
72 struct ad9852_config *config = (struct ad9852_config *)buf;
73 struct iio_dev *idev = dev_to_iio_dev(dev);
74 struct ad9852_state *st = iio_priv(idev);
77 xfer.tx_buf = &config->phajst0[0];
78 mutex_lock(&st->lock);
80 ret = spi_sync_transfer(st->sdev, &xfer, 1);
85 xfer.tx_buf = &config->phajst1[0];
87 ret = spi_sync_transfer(st->sdev, &xfer, 1);
92 xfer.tx_buf = &config->fretun1[0];
94 ret = spi_sync_transfer(st->sdev, &xfer, 1);
99 xfer.tx_buf = &config->fretun2[0];
101 ret = spi_sync_transfer(st->sdev, &xfer, 1);
106 xfer.tx_buf = &config->dltafre[0];
108 ret = spi_sync_transfer(st->sdev, &xfer, 1);
113 xfer.tx_buf = &config->updtclk[0];
115 ret = spi_sync_transfer(st->sdev, &xfer, 1);
120 xfer.tx_buf = &config->ramprat[0];
122 ret = spi_sync_transfer(st->sdev, &xfer, 1);
127 xfer.tx_buf = &config->control[0];
129 ret = spi_sync_transfer(st->sdev, &xfer, 1);
134 xfer.tx_buf = &config->outpskm[0];
136 ret = spi_sync_transfer(st->sdev, &xfer, 1);
141 xfer.tx_buf = &config->outpskr[0];
143 ret = spi_sync_transfer(st->sdev, &xfer, 1);
148 xfer.tx_buf = &config->daccntl[0];
150 ret = spi_sync_transfer(st->sdev, &xfer, 1);
154 mutex_unlock(&st->lock);
156 return ret ? ret : len;
159 static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad9852_set_parameter, 0);
161 static void ad9852_init(struct ad9852_state *st)
163 struct spi_transfer xfer;
167 config[0] = addr_contrl;
169 config[2] = REFMULT2 | BYPPLL | PLLRANG;
173 mutex_lock(&st->lock);
176 xfer.tx_buf = &config;
178 ret = spi_sync_transfer(st->sdev, &xfer, 1);
183 mutex_unlock(&st->lock);
189 static struct attribute *ad9852_attributes[] = {
190 &iio_dev_attr_dds.dev_attr.attr,
194 static const struct attribute_group ad9852_attribute_group = {
195 .attrs = ad9852_attributes,
198 static const struct iio_info ad9852_info = {
199 .attrs = &ad9852_attribute_group,
200 .driver_module = THIS_MODULE,
203 static int ad9852_probe(struct spi_device *spi)
205 struct ad9852_state *st;
206 struct iio_dev *idev;
209 idev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
213 spi_set_drvdata(spi, idev);
214 mutex_init(&st->lock);
217 idev->dev.parent = &spi->dev;
218 idev->info = &ad9852_info;
219 idev->modes = INDIO_DIRECT_MODE;
221 ret = iio_device_register(idev);
224 spi->max_speed_hz = 2000000;
225 spi->mode = SPI_MODE_3;
226 spi->bits_per_word = 8;
233 static int ad9852_remove(struct spi_device *spi)
235 iio_device_unregister(spi_get_drvdata(spi));
240 static struct spi_driver ad9852_driver = {
243 .owner = THIS_MODULE,
245 .probe = ad9852_probe,
246 .remove = ad9852_remove,
248 module_spi_driver(ad9852_driver);
250 MODULE_AUTHOR("Cliff Cai");
251 MODULE_DESCRIPTION("Analog Devices ad9852 driver");
252 MODULE_LICENSE("GPL v2");
253 MODULE_ALIAS("spi:" DRV_NAME);