Merge branches 'acpi-hotplug', 'acpi-sysfs' and 'acpi-sleep'
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / frequency / ad9951.c
1 /*
2  * Driver for ADI Direct Digital Synthesis ad9951
3  *
4  * Copyright (c) 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 #include <linux/module.h>
18
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21
22 #define DRV_NAME "ad9951"
23
24 #define CFR1 0x0
25 #define CFR2 0x1
26
27 #define AUTO_OSK        (1)
28 #define OSKEN           (1 << 1)
29 #define LOAD_ARR        (1 << 2)
30
31 #define AUTO_SYNC       (1 << 7)
32
33 #define LSB_FST         (1)
34 #define SDIO_IPT        (1 << 1)
35 #define CLR_PHA         (1 << 2)
36 #define SINE_OPT        (1 << 4)
37 #define ACLR_PHA        (1 << 5)
38
39 #define VCO_RANGE       (1 << 2)
40
41 #define CRS_OPT         (1 << 1)
42 #define HMANU_SYNC      (1 << 2)
43 #define HSPD_SYNC       (1 << 3)
44
45 /* Register format: 1 byte addr + value */
46 struct ad9951_config {
47         u8 asf[3];
48         u8 arr[2];
49         u8 ftw0[5];
50         u8 ftw1[3];
51 };
52
53 struct ad9951_state {
54         struct mutex lock;
55         struct spi_device *sdev;
56 };
57
58 static ssize_t ad9951_set_parameter(struct device *dev,
59                                         struct device_attribute *attr,
60                                         const char *buf,
61                                         size_t len)
62 {
63         struct spi_transfer xfer;
64         int ret;
65         struct ad9951_config *config = (struct ad9951_config *)buf;
66         struct iio_dev *idev = dev_to_iio_dev(dev);
67         struct ad9951_state *st = iio_priv(idev);
68
69         xfer.len = 3;
70         xfer.tx_buf = &config->asf[0];
71         mutex_lock(&st->lock);
72
73         ret = spi_sync_transfer(st->sdev, &xfer, 1);
74         if (ret)
75                 goto error_ret;
76
77         xfer.len = 2;
78         xfer.tx_buf = &config->arr[0];
79
80         ret = spi_sync_transfer(st->sdev, &xfer, 1);
81         if (ret)
82                 goto error_ret;
83
84         xfer.len = 5;
85         xfer.tx_buf = &config->ftw0[0];
86
87         ret = spi_sync_transfer(st->sdev, &xfer, 1);
88         if (ret)
89                 goto error_ret;
90
91         xfer.len = 3;
92         xfer.tx_buf = &config->ftw1[0];
93
94         ret = spi_sync_transfer(st->sdev, &xfer, 1);
95         if (ret)
96                 goto error_ret;
97 error_ret:
98         mutex_unlock(&st->lock);
99
100         return ret ? ret : len;
101 }
102
103 static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad9951_set_parameter, 0);
104
105 static void ad9951_init(struct ad9951_state *st)
106 {
107         struct spi_transfer xfer;
108         int ret;
109         u8 cfr[5];
110
111         cfr[0] = CFR1;
112         cfr[1] = 0;
113         cfr[2] = LSB_FST | CLR_PHA | SINE_OPT | ACLR_PHA;
114         cfr[3] = AUTO_OSK | OSKEN | LOAD_ARR;
115         cfr[4] = 0;
116
117         mutex_lock(&st->lock);
118
119         xfer.len = 5;
120         xfer.tx_buf = &cfr;
121
122         ret = spi_sync_transfer(st->sdev, &xfer, 1);
123         if (ret)
124                 goto error_ret;
125
126         cfr[0] = CFR2;
127         cfr[1] = VCO_RANGE;
128         cfr[2] = HSPD_SYNC;
129         cfr[3] = 0;
130
131         xfer.len = 4;
132         xfer.tx_buf = &cfr;
133
134         ret = spi_sync_transfer(st->sdev, &xfer, 1);
135         if (ret)
136                 goto error_ret;
137
138 error_ret:
139         mutex_unlock(&st->lock);
140
141
142
143 }
144
145 static struct attribute *ad9951_attributes[] = {
146         &iio_dev_attr_dds.dev_attr.attr,
147         NULL,
148 };
149
150 static const struct attribute_group ad9951_attribute_group = {
151         .attrs = ad9951_attributes,
152 };
153
154 static const struct iio_info ad9951_info = {
155         .attrs = &ad9951_attribute_group,
156         .driver_module = THIS_MODULE,
157 };
158
159 static int ad9951_probe(struct spi_device *spi)
160 {
161         struct ad9951_state *st;
162         struct iio_dev *idev;
163         int ret = 0;
164
165         idev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
166         if (!idev)
167                 return -ENOMEM;
168         spi_set_drvdata(spi, idev);
169         st = iio_priv(idev);
170         mutex_init(&st->lock);
171         st->sdev = spi;
172
173         idev->dev.parent = &spi->dev;
174
175         idev->info = &ad9951_info;
176         idev->modes = INDIO_DIRECT_MODE;
177
178         ret = iio_device_register(idev);
179         if (ret)
180                 return ret;
181         spi->max_speed_hz = 2000000;
182         spi->mode = SPI_MODE_3;
183         spi->bits_per_word = 8;
184         spi_setup(spi);
185         ad9951_init(st);
186         return 0;
187 }
188
189 static int ad9951_remove(struct spi_device *spi)
190 {
191         iio_device_unregister(spi_get_drvdata(spi));
192
193         return 0;
194 }
195
196 static struct spi_driver ad9951_driver = {
197         .driver = {
198                 .name = DRV_NAME,
199                 .owner = THIS_MODULE,
200         },
201         .probe = ad9951_probe,
202         .remove = ad9951_remove,
203 };
204 module_spi_driver(ad9951_driver);
205
206 MODULE_AUTHOR("Cliff Cai");
207 MODULE_DESCRIPTION("Analog Devices ad9951 driver");
208 MODULE_LICENSE("GPL v2");
209 MODULE_ALIAS("spi:" DRV_NAME);