2 * Driver for ADI Direct Digital Synthesis ad9951
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 "ad9951"
28 #define OSKEN (1 << 1)
29 #define LOAD_ARR (1 << 2)
31 #define AUTO_SYNC (1 << 7)
34 #define SDIO_IPT (1 << 1)
35 #define CLR_PHA (1 << 2)
36 #define SINE_OPT (1 << 4)
37 #define ACLR_PHA (1 << 5)
39 #define VCO_RANGE (1 << 2)
41 #define CRS_OPT (1 << 1)
42 #define HMANU_SYNC (1 << 2)
43 #define HSPD_SYNC (1 << 3)
45 /* Register format: 1 byte addr + value */
46 struct ad9951_config {
55 struct spi_device *sdev;
58 static ssize_t ad9951_set_parameter(struct device *dev,
59 struct device_attribute *attr,
63 struct spi_transfer xfer;
65 struct ad9951_config *config = (struct ad9951_config *)buf;
66 struct iio_dev *idev = dev_to_iio_dev(dev);
67 struct ad9951_state *st = iio_priv(idev);
70 xfer.tx_buf = &config->asf[0];
71 mutex_lock(&st->lock);
73 ret = spi_sync_transfer(st->sdev, &xfer, 1);
78 xfer.tx_buf = &config->arr[0];
80 ret = spi_sync_transfer(st->sdev, &xfer, 1);
85 xfer.tx_buf = &config->ftw0[0];
87 ret = spi_sync_transfer(st->sdev, &xfer, 1);
92 xfer.tx_buf = &config->ftw1[0];
94 ret = spi_sync_transfer(st->sdev, &xfer, 1);
98 mutex_unlock(&st->lock);
100 return ret ? ret : len;
103 static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad9951_set_parameter, 0);
105 static void ad9951_init(struct ad9951_state *st)
107 struct spi_transfer xfer;
113 cfr[2] = LSB_FST | CLR_PHA | SINE_OPT | ACLR_PHA;
114 cfr[3] = AUTO_OSK | OSKEN | LOAD_ARR;
117 mutex_lock(&st->lock);
122 ret = spi_sync_transfer(st->sdev, &xfer, 1);
134 ret = spi_sync_transfer(st->sdev, &xfer, 1);
139 mutex_unlock(&st->lock);
145 static struct attribute *ad9951_attributes[] = {
146 &iio_dev_attr_dds.dev_attr.attr,
150 static const struct attribute_group ad9951_attribute_group = {
151 .attrs = ad9951_attributes,
154 static const struct iio_info ad9951_info = {
155 .attrs = &ad9951_attribute_group,
156 .driver_module = THIS_MODULE,
159 static int ad9951_probe(struct spi_device *spi)
161 struct ad9951_state *st;
162 struct iio_dev *idev;
165 idev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
168 spi_set_drvdata(spi, idev);
170 mutex_init(&st->lock);
173 idev->dev.parent = &spi->dev;
175 idev->info = &ad9951_info;
176 idev->modes = INDIO_DIRECT_MODE;
178 ret = devm_iio_device_register(&spi->dev, idev);
181 spi->max_speed_hz = 2000000;
182 spi->mode = SPI_MODE_3;
183 spi->bits_per_word = 8;
189 static struct spi_driver ad9951_driver = {
192 .owner = THIS_MODULE,
194 .probe = ad9951_probe,
196 module_spi_driver(ad9951_driver);
198 MODULE_AUTHOR("Cliff Cai");
199 MODULE_DESCRIPTION("Analog Devices ad9951 driver");
200 MODULE_LICENSE("GPL v2");
201 MODULE_ALIAS("spi:" DRV_NAME);