Merge branch 'staging-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.git] / drivers / staging / iio / dds / ad5930.c
1 /*
2  * Driver for ADI Direct Digital Synthesis ad5930
3  *
4  * Copyright (c) 2010-2010 Analog Devices Inc.
5  *
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.
9  *
10  */
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
18 #include "../iio.h"
19 #include "../sysfs.h"
20
21 #define DRV_NAME "ad5930"
22
23 #define value_mask (u16)0xf000
24 #define addr_shift 12
25
26 /* Register format: 4 bits addr + 12 bits value */
27 struct ad5903_config {
28         u16 control;
29         u16 incnum;
30         u16 frqdelt[2];
31         u16 incitvl;
32         u16 buritvl;
33         u16 strtfrq[2];
34 };
35
36 struct ad5930_state {
37         struct mutex lock;
38         struct iio_dev *idev;
39         struct spi_device *sdev;
40 };
41
42 static ssize_t ad5930_set_parameter(struct device *dev,
43                                         struct device_attribute *attr,
44                                         const char *buf,
45                                         size_t len)
46 {
47         struct spi_message msg;
48         struct spi_transfer xfer;
49         int ret;
50         struct ad5903_config *config = (struct ad5903_config *)buf;
51         struct iio_dev *idev = dev_get_drvdata(dev);
52         struct ad5930_state *st = idev->dev_data;
53
54         config->control = (config->control & ~value_mask);
55         config->incnum = (config->control & ~value_mask) | (1 << addr_shift);
56         config->frqdelt[0] = (config->control & ~value_mask) | (2 << addr_shift);
57         config->frqdelt[1] = (config->control & ~value_mask) | 3 << addr_shift;
58         config->incitvl = (config->control & ~value_mask) | 4 << addr_shift;
59         config->buritvl = (config->control & ~value_mask) | 8 << addr_shift;
60         config->strtfrq[0] = (config->control & ~value_mask) | 0xc << addr_shift;
61         config->strtfrq[1] = (config->control & ~value_mask) | 0xd << addr_shift;
62
63         xfer.len = len;
64         xfer.tx_buf = config;
65         mutex_lock(&st->lock);
66
67         spi_message_init(&msg);
68         spi_message_add_tail(&xfer, &msg);
69         ret = spi_sync(st->sdev, &msg);
70         if (ret)
71                 goto error_ret;
72 error_ret:
73         mutex_unlock(&st->lock);
74
75         return ret ? ret : len;
76 }
77
78 static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad5930_set_parameter, 0);
79
80 static struct attribute *ad5930_attributes[] = {
81         &iio_dev_attr_dds.dev_attr.attr,
82         NULL,
83 };
84
85 static const struct attribute_group ad5930_attribute_group = {
86         .name = DRV_NAME,
87         .attrs = ad5930_attributes,
88 };
89
90 static int __devinit ad5930_probe(struct spi_device *spi)
91 {
92         struct ad5930_state *st;
93         int ret = 0;
94
95         st = kzalloc(sizeof(*st), GFP_KERNEL);
96         if (st == NULL) {
97                 ret = -ENOMEM;
98                 goto error_ret;
99         }
100         spi_set_drvdata(spi, st);
101
102         mutex_init(&st->lock);
103         st->sdev = spi;
104
105         st->idev = iio_allocate_device();
106         if (st->idev == NULL) {
107                 ret = -ENOMEM;
108                 goto error_free_st;
109         }
110         st->idev->dev.parent = &spi->dev;
111         st->idev->num_interrupt_lines = 0;
112         st->idev->event_attrs = NULL;
113
114         st->idev->attrs = &ad5930_attribute_group;
115         st->idev->dev_data = (void *)(st);
116         st->idev->driver_module = THIS_MODULE;
117         st->idev->modes = INDIO_DIRECT_MODE;
118
119         ret = iio_device_register(st->idev);
120         if (ret)
121                 goto error_free_dev;
122         spi->max_speed_hz = 2000000;
123         spi->mode = SPI_MODE_3;
124         spi->bits_per_word = 16;
125         spi_setup(spi);
126
127         return 0;
128
129 error_free_dev:
130         iio_free_device(st->idev);
131 error_free_st:
132         kfree(st);
133 error_ret:
134         return ret;
135 }
136
137 static int __devexit ad5930_remove(struct spi_device *spi)
138 {
139         struct ad5930_state *st = spi_get_drvdata(spi);
140
141         iio_device_unregister(st->idev);
142         kfree(st);
143
144         return 0;
145 }
146
147 static struct spi_driver ad5930_driver = {
148         .driver = {
149                 .name = DRV_NAME,
150                 .owner = THIS_MODULE,
151         },
152         .probe = ad5930_probe,
153         .remove = __devexit_p(ad5930_remove),
154 };
155
156 static __init int ad5930_spi_init(void)
157 {
158         return spi_register_driver(&ad5930_driver);
159 }
160 module_init(ad5930_spi_init);
161
162 static __exit void ad5930_spi_exit(void)
163 {
164         spi_unregister_driver(&ad5930_driver);
165 }
166 module_exit(ad5930_spi_exit);
167
168 MODULE_AUTHOR("Cliff Cai");
169 MODULE_DESCRIPTION("Analog Devices ad5930 driver");
170 MODULE_LICENSE("GPL v2");