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>
21 #define DRV_NAME "ad9951"
27 #define OSKEN (1 << 1)
28 #define LOAD_ARR (1 << 2)
30 #define AUTO_SYNC (1 << 7)
33 #define SDIO_IPT (1 << 1)
34 #define CLR_PHA (1 << 2)
35 #define SINE_OPT (1 << 4)
36 #define ACLR_PHA (1 << 5)
38 #define VCO_RANGE (1 << 2)
40 #define CRS_OPT (1 << 1)
41 #define HMANU_SYNC (1 << 2)
42 #define HSPD_SYNC (1 << 3)
44 /* Register format: 1 byte addr + value */
45 struct ad9951_config
{
54 struct spi_device
*sdev
;
57 static ssize_t
ad9951_set_parameter(struct device
*dev
,
58 struct device_attribute
*attr
,
62 struct spi_message msg
;
63 struct spi_transfer xfer
;
65 struct ad9951_config
*config
= (struct ad9951_config
*)buf
;
66 struct iio_dev
*idev
= dev_get_drvdata(dev
);
67 struct ad9951_state
*st
= iio_priv(idev
);
70 xfer
.tx_buf
= &config
->asf
[0];
71 mutex_lock(&st
->lock
);
73 spi_message_init(&msg
);
74 spi_message_add_tail(&xfer
, &msg
);
75 ret
= spi_sync(st
->sdev
, &msg
);
80 xfer
.tx_buf
= &config
->arr
[0];
82 spi_message_init(&msg
);
83 spi_message_add_tail(&xfer
, &msg
);
84 ret
= spi_sync(st
->sdev
, &msg
);
89 xfer
.tx_buf
= &config
->ftw0
[0];
91 spi_message_init(&msg
);
92 spi_message_add_tail(&xfer
, &msg
);
93 ret
= spi_sync(st
->sdev
, &msg
);
98 xfer
.tx_buf
= &config
->ftw1
[0];
100 spi_message_init(&msg
);
101 spi_message_add_tail(&xfer
, &msg
);
102 ret
= spi_sync(st
->sdev
, &msg
);
106 mutex_unlock(&st
->lock
);
108 return ret
? ret
: len
;
111 static IIO_DEVICE_ATTR(dds
, S_IWUSR
, NULL
, ad9951_set_parameter
, 0);
113 static void ad9951_init(struct ad9951_state
*st
)
115 struct spi_message msg
;
116 struct spi_transfer xfer
;
122 cfr
[2] = LSB_FST
| CLR_PHA
| SINE_OPT
| ACLR_PHA
;
123 cfr
[3] = AUTO_OSK
| OSKEN
| LOAD_ARR
;
126 mutex_lock(&st
->lock
);
131 spi_message_init(&msg
);
132 spi_message_add_tail(&xfer
, &msg
);
133 ret
= spi_sync(st
->sdev
, &msg
);
145 spi_message_init(&msg
);
146 spi_message_add_tail(&xfer
, &msg
);
147 ret
= spi_sync(st
->sdev
, &msg
);
152 mutex_unlock(&st
->lock
);
158 static struct attribute
*ad9951_attributes
[] = {
159 &iio_dev_attr_dds
.dev_attr
.attr
,
163 static const struct attribute_group ad9951_attribute_group
= {
165 .attrs
= ad9951_attributes
,
168 static const struct iio_info ad9951_info
= {
169 .attrs
= &ad9951_attribute_group
,
170 .driver_module
= THIS_MODULE
,
173 static int __devinit
ad9951_probe(struct spi_device
*spi
)
175 struct ad9951_state
*st
;
176 struct iio_dev
*idev
;
179 idev
= iio_allocate_device(sizeof(*st
));
184 spi_set_drvdata(spi
, idev
);
186 mutex_init(&st
->lock
);
189 idev
->dev
.parent
= &spi
->dev
;
191 idev
->info
= &ad9951_info
;
192 idev
->modes
= INDIO_DIRECT_MODE
;
194 ret
= iio_device_register(idev
);
197 spi
->max_speed_hz
= 2000000;
198 spi
->mode
= SPI_MODE_3
;
199 spi
->bits_per_word
= 8;
205 iio_free_device(idev
);
211 static int __devexit
ad9951_remove(struct spi_device
*spi
)
213 iio_device_unregister(spi_get_drvdata(spi
));
218 static struct spi_driver ad9951_driver
= {
221 .owner
= THIS_MODULE
,
223 .probe
= ad9951_probe
,
224 .remove
= __devexit_p(ad9951_remove
),
227 static __init
int ad9951_spi_init(void)
229 return spi_register_driver(&ad9951_driver
);
231 module_init(ad9951_spi_init
);
233 static __exit
void ad9951_spi_exit(void)
235 spi_unregister_driver(&ad9951_driver
);
237 module_exit(ad9951_spi_exit
);
239 MODULE_AUTHOR("Cliff Cai");
240 MODULE_DESCRIPTION("Analog Devices ad9951 driver");
241 MODULE_LICENSE("GPL v2");