2 * Driver for ADI Direct Digital Synthesis ad9850
4 * Copyright (c) 2010-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 "ad9850"
23 #define value_mask (u16)0xf000
26 /* Register format: 4 bits addr + 12 bits value */
27 struct ad9850_config
{
33 struct spi_device
*sdev
;
36 static ssize_t
ad9850_set_parameter(struct device
*dev
,
37 struct device_attribute
*attr
,
41 struct spi_message msg
;
42 struct spi_transfer xfer
;
44 struct ad9850_config
*config
= (struct ad9850_config
*)buf
;
45 struct iio_dev
*idev
= dev_get_drvdata(dev
);
46 struct ad9850_state
*st
= iio_priv(idev
);
50 mutex_lock(&st
->lock
);
52 spi_message_init(&msg
);
53 spi_message_add_tail(&xfer
, &msg
);
54 ret
= spi_sync(st
->sdev
, &msg
);
58 mutex_unlock(&st
->lock
);
60 return ret
? ret
: len
;
63 static IIO_DEVICE_ATTR(dds
, S_IWUSR
, NULL
, ad9850_set_parameter
, 0);
65 static struct attribute
*ad9850_attributes
[] = {
66 &iio_dev_attr_dds
.dev_attr
.attr
,
70 static const struct attribute_group ad9850_attribute_group
= {
71 .attrs
= ad9850_attributes
,
74 static const struct iio_info ad9850_info
= {
75 .attrs
= &ad9850_attribute_group
,
76 .driver_module
= THIS_MODULE
,
79 static int __devinit
ad9850_probe(struct spi_device
*spi
)
81 struct ad9850_state
*st
;
85 idev
= iio_allocate_device(sizeof(*st
));
90 spi_set_drvdata(spi
, idev
);
92 mutex_init(&st
->lock
);
95 idev
->dev
.parent
= &spi
->dev
;
96 idev
->info
= &ad9850_info
;
97 idev
->modes
= INDIO_DIRECT_MODE
;
99 ret
= iio_device_register(idev
);
102 spi
->max_speed_hz
= 2000000;
103 spi
->mode
= SPI_MODE_3
;
104 spi
->bits_per_word
= 16;
110 iio_free_device(idev
);
115 static int __devexit
ad9850_remove(struct spi_device
*spi
)
117 iio_device_unregister(spi_get_drvdata(spi
));
122 static struct spi_driver ad9850_driver
= {
125 .owner
= THIS_MODULE
,
127 .probe
= ad9850_probe
,
128 .remove
= __devexit_p(ad9850_remove
),
131 static __init
int ad9850_spi_init(void)
133 return spi_register_driver(&ad9850_driver
);
135 module_init(ad9850_spi_init
);
137 static __exit
void ad9850_spi_exit(void)
139 spi_unregister_driver(&ad9850_driver
);
141 module_exit(ad9850_spi_exit
);
143 MODULE_AUTHOR("Cliff Cai");
144 MODULE_DESCRIPTION("Analog Devices ad9850 driver");
145 MODULE_LICENSE("GPL v2");