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>
17 #include <linux/module.h>
22 #define DRV_NAME "ad9850"
24 #define value_mask (u16)0xf000
27 /* Register format: 4 bits addr + 12 bits value */
28 struct ad9850_config
{
34 struct spi_device
*sdev
;
37 static ssize_t
ad9850_set_parameter(struct device
*dev
,
38 struct device_attribute
*attr
,
42 struct spi_message msg
;
43 struct spi_transfer xfer
;
45 struct ad9850_config
*config
= (struct ad9850_config
*)buf
;
46 struct iio_dev
*idev
= dev_get_drvdata(dev
);
47 struct ad9850_state
*st
= iio_priv(idev
);
51 mutex_lock(&st
->lock
);
53 spi_message_init(&msg
);
54 spi_message_add_tail(&xfer
, &msg
);
55 ret
= spi_sync(st
->sdev
, &msg
);
59 mutex_unlock(&st
->lock
);
61 return ret
? ret
: len
;
64 static IIO_DEVICE_ATTR(dds
, S_IWUSR
, NULL
, ad9850_set_parameter
, 0);
66 static struct attribute
*ad9850_attributes
[] = {
67 &iio_dev_attr_dds
.dev_attr
.attr
,
71 static const struct attribute_group ad9850_attribute_group
= {
72 .attrs
= ad9850_attributes
,
75 static const struct iio_info ad9850_info
= {
76 .attrs
= &ad9850_attribute_group
,
77 .driver_module
= THIS_MODULE
,
80 static int __devinit
ad9850_probe(struct spi_device
*spi
)
82 struct ad9850_state
*st
;
86 idev
= iio_allocate_device(sizeof(*st
));
91 spi_set_drvdata(spi
, idev
);
93 mutex_init(&st
->lock
);
96 idev
->dev
.parent
= &spi
->dev
;
97 idev
->info
= &ad9850_info
;
98 idev
->modes
= INDIO_DIRECT_MODE
;
100 ret
= iio_device_register(idev
);
103 spi
->max_speed_hz
= 2000000;
104 spi
->mode
= SPI_MODE_3
;
105 spi
->bits_per_word
= 16;
111 iio_free_device(idev
);
116 static int __devexit
ad9850_remove(struct spi_device
*spi
)
118 iio_device_unregister(spi_get_drvdata(spi
));
119 iio_free_device(spi_get_drvdata(spi
));
124 static struct spi_driver ad9850_driver
= {
127 .owner
= THIS_MODULE
,
129 .probe
= ad9850_probe
,
130 .remove
= __devexit_p(ad9850_remove
),
133 static __init
int ad9850_spi_init(void)
135 return spi_register_driver(&ad9850_driver
);
137 module_init(ad9850_spi_init
);
139 static __exit
void ad9850_spi_exit(void)
141 spi_unregister_driver(&ad9850_driver
);
143 module_exit(ad9850_spi_exit
);
145 MODULE_AUTHOR("Cliff Cai");
146 MODULE_DESCRIPTION("Analog Devices ad9850 driver");
147 MODULE_LICENSE("GPL v2");