Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / dds / ad9850.c
blobd7c9d05f6359651749e01433f0d7e569e28ba861
1 /*
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>
18 #include "../iio.h"
19 #include "../sysfs.h"
21 #define DRV_NAME "ad9850"
23 #define value_mask (u16)0xf000
24 #define addr_shift 12
26 /* Register format: 4 bits addr + 12 bits value */
27 struct ad9850_config {
28 u8 control[5];
31 struct ad9850_state {
32 struct mutex lock;
33 struct spi_device *sdev;
36 static ssize_t ad9850_set_parameter(struct device *dev,
37 struct device_attribute *attr,
38 const char *buf,
39 size_t len)
41 struct spi_message msg;
42 struct spi_transfer xfer;
43 int ret;
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);
48 xfer.len = len;
49 xfer.tx_buf = config;
50 mutex_lock(&st->lock);
52 spi_message_init(&msg);
53 spi_message_add_tail(&xfer, &msg);
54 ret = spi_sync(st->sdev, &msg);
55 if (ret)
56 goto error_ret;
57 error_ret:
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,
67 NULL,
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;
82 struct iio_dev *idev;
83 int ret = 0;
85 idev = iio_allocate_device(sizeof(*st));
86 if (idev == NULL) {
87 ret = -ENOMEM;
88 goto error_ret;
90 spi_set_drvdata(spi, idev);
91 st = iio_priv(idev);
92 mutex_init(&st->lock);
93 st->sdev = spi;
95 idev->dev.parent = &spi->dev;
96 idev->info = &ad9850_info;
97 idev->modes = INDIO_DIRECT_MODE;
99 ret = iio_device_register(idev);
100 if (ret)
101 goto error_free_dev;
102 spi->max_speed_hz = 2000000;
103 spi->mode = SPI_MODE_3;
104 spi->bits_per_word = 16;
105 spi_setup(spi);
107 return 0;
109 error_free_dev:
110 iio_free_device(idev);
111 error_ret:
112 return ret;
115 static int __devexit ad9850_remove(struct spi_device *spi)
117 iio_device_unregister(spi_get_drvdata(spi));
119 return 0;
122 static struct spi_driver ad9850_driver = {
123 .driver = {
124 .name = DRV_NAME,
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");