~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/staging/iio/dds/ad9832.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Driver for ADI Direct Digital Synthesis ad9832
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
 
 *
 
2
 * AD9832 SPI DDS driver
 
3
 *
 
4
 * Copyright 2011 Analog Devices Inc.
 
5
 *
 
6
 * Licensed under the GPL-2.
10
7
 */
11
 
#include <linux/types.h>
12
 
#include <linux/mutex.h>
 
8
 
13
9
#include <linux/device.h>
14
 
#include <linux/spi/spi.h>
 
10
#include <linux/kernel.h>
15
11
#include <linux/slab.h>
16
12
#include <linux/sysfs.h>
 
13
#include <linux/spi/spi.h>
 
14
#include <linux/regulator/consumer.h>
 
15
#include <linux/err.h>
 
16
#include <asm/div64.h>
17
17
 
18
18
#include "../iio.h"
19
19
#include "../sysfs.h"
20
 
 
21
 
#define DRV_NAME "ad9832"
22
 
 
23
 
#define value_mask (u16)0xf000
24
 
#define cmd_shift 12
25
 
#define add_shift 8
26
 
#define AD9832_SYNC (1 << 13)
27
 
#define AD9832_SELSRC (1 << 12)
28
 
#define AD9832_SLEEP (1 << 13)
29
 
#define AD9832_RESET (1 << 12)
30
 
#define AD9832_CLR (1 << 11)
31
 
 
32
 
#define ADD_FREQ0LL 0x0
33
 
#define ADD_FREQ0HL 0x1
34
 
#define ADD_FREQ0LM 0x2
35
 
#define ADD_FREQ0HM 0x3
36
 
#define ADD_FREQ1LL 0x4
37
 
#define ADD_FREQ1HL 0x5
38
 
#define ADD_FREQ1LM 0x6
39
 
#define ADD_FREQ1HM 0x7
40
 
#define ADD_PHASE0L 0x8
41
 
#define ADD_PHASE0H 0x9
42
 
#define ADD_PHASE1L 0xa
43
 
#define ADD_PHASE1H 0xb
44
 
#define ADD_PHASE2L 0xc
45
 
#define ADD_PHASE2H 0xd
46
 
#define ADD_PHASE3L 0xe
47
 
#define ADD_PHASE3H 0xf
48
 
 
49
 
#define CMD_PHA8BITSW 0x1
50
 
#define CMD_PHA16BITSW 0x0
51
 
#define CMD_FRE8BITSW 0x3
52
 
#define CMD_FRE16BITSW 0x2
53
 
#define CMD_SELBITSCTL 0x6
54
 
 
55
 
struct ad9832_setting {
56
 
        u16 freq0[4];
57
 
        u16 freq1[4];
58
 
        u16 phase0[2];
59
 
        u16 phase1[2];
60
 
        u16 phase2[2];
61
 
        u16 phase3[2];
62
 
};
63
 
 
64
 
struct ad9832_state {
65
 
        struct mutex lock;
66
 
        struct iio_dev *idev;
67
 
        struct spi_device *sdev;
68
 
};
69
 
 
70
 
static ssize_t ad9832_set_parameter(struct device *dev,
71
 
                                        struct device_attribute *attr,
72
 
                                        const char *buf,
73
 
                                        size_t len)
74
 
{
75
 
        struct spi_message msg;
76
 
        struct spi_transfer xfer;
 
20
#include "dds.h"
 
21
 
 
22
#include "ad9832.h"
 
23
 
 
24
static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout)
 
25
{
 
26
        unsigned long long freqreg = (u64) fout *
 
27
                                     (u64) ((u64) 1L << AD9832_FREQ_BITS);
 
28
        do_div(freqreg, mclk);
 
29
        return freqreg;
 
30
}
 
31
 
 
32
static int ad9832_write_frequency(struct ad9832_state *st,
 
33
                                  unsigned addr, unsigned long fout)
 
34
{
 
35
        unsigned long regval;
 
36
 
 
37
        if (fout > (st->mclk / 2))
 
38
                return -EINVAL;
 
39
 
 
40
        regval = ad9832_calc_freqreg(st->mclk, fout);
 
41
 
 
42
        st->freq_data[0] = cpu_to_be16((AD9832_CMD_FRE8BITSW << CMD_SHIFT) |
 
43
                                        (addr << ADD_SHIFT) |
 
44
                                        ((regval >> 24) & 0xFF));
 
45
        st->freq_data[1] = cpu_to_be16((AD9832_CMD_FRE16BITSW << CMD_SHIFT) |
 
46
                                        ((addr - 1) << ADD_SHIFT) |
 
47
                                        ((regval >> 16) & 0xFF));
 
48
        st->freq_data[2] = cpu_to_be16((AD9832_CMD_FRE8BITSW << CMD_SHIFT) |
 
49
                                        ((addr - 2) << ADD_SHIFT) |
 
50
                                        ((regval >> 8) & 0xFF));
 
51
        st->freq_data[3] = cpu_to_be16((AD9832_CMD_FRE16BITSW << CMD_SHIFT) |
 
52
                                        ((addr - 3) << ADD_SHIFT) |
 
53
                                        ((regval >> 0) & 0xFF));
 
54
 
 
55
        return spi_sync(st->spi, &st->freq_msg);;
 
56
}
 
57
 
 
58
static int ad9832_write_phase(struct ad9832_state *st,
 
59
                                  unsigned long addr, unsigned long phase)
 
60
{
 
61
        if (phase > (1 << AD9832_PHASE_BITS))
 
62
                return -EINVAL;
 
63
 
 
64
        st->phase_data[0] = cpu_to_be16((AD9832_CMD_PHA8BITSW << CMD_SHIFT) |
 
65
                                        (addr << ADD_SHIFT) |
 
66
                                        ((phase >> 8) & 0xFF));
 
67
        st->phase_data[1] = cpu_to_be16((AD9832_CMD_PHA16BITSW << CMD_SHIFT) |
 
68
                                        ((addr - 1) << ADD_SHIFT) |
 
69
                                        (phase & 0xFF));
 
70
 
 
71
        return spi_sync(st->spi, &st->phase_msg);
 
72
}
 
73
 
 
74
static ssize_t ad9832_write(struct device *dev,
 
75
                struct device_attribute *attr,
 
76
                const char *buf,
 
77
                size_t len)
 
78
{
 
79
        struct iio_dev *dev_info = dev_get_drvdata(dev);
 
80
        struct ad9832_state *st = dev_info->dev_data;
 
81
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
77
82
        int ret;
78
 
        struct ad9832_setting config;
79
 
        struct iio_dev *idev = dev_get_drvdata(dev);
80
 
        struct ad9832_state *st = idev->dev_data;
81
 
 
82
 
        config.freq0[0] = (CMD_FRE8BITSW << add_shift | ADD_FREQ0LL << add_shift | buf[0]);
83
 
        config.freq0[1] = (CMD_FRE16BITSW << add_shift | ADD_FREQ0HL << add_shift | buf[1]);
84
 
        config.freq0[2] = (CMD_FRE8BITSW << add_shift | ADD_FREQ0LM << add_shift | buf[2]);
85
 
        config.freq0[3] = (CMD_FRE16BITSW << add_shift | ADD_FREQ0HM << add_shift | buf[3]);
86
 
        config.freq1[0] = (CMD_FRE8BITSW << add_shift | ADD_FREQ1LL << add_shift | buf[4]);
87
 
        config.freq1[1] = (CMD_FRE16BITSW << add_shift | ADD_FREQ1HL << add_shift | buf[5]);
88
 
        config.freq1[2] = (CMD_FRE8BITSW << add_shift | ADD_FREQ1LM << add_shift | buf[6]);
89
 
        config.freq1[3] = (CMD_FRE16BITSW << add_shift | ADD_FREQ1HM << add_shift | buf[7]);
90
 
 
91
 
        config.phase0[0] = (CMD_PHA8BITSW << add_shift | ADD_PHASE0L << add_shift | buf[9]);
92
 
        config.phase0[1] = (CMD_PHA16BITSW << add_shift | ADD_PHASE0H << add_shift | buf[10]);
93
 
        config.phase1[0] = (CMD_PHA8BITSW << add_shift | ADD_PHASE1L << add_shift | buf[11]);
94
 
        config.phase1[1] = (CMD_PHA16BITSW << add_shift | ADD_PHASE1H << add_shift | buf[12]);
95
 
        config.phase2[0] = (CMD_PHA8BITSW << add_shift | ADD_PHASE2L << add_shift | buf[13]);
96
 
        config.phase2[1] = (CMD_PHA16BITSW << add_shift | ADD_PHASE2H << add_shift | buf[14]);
97
 
        config.phase3[0] = (CMD_PHA8BITSW << add_shift | ADD_PHASE3L << add_shift | buf[15]);
98
 
        config.phase3[1] = (CMD_PHA16BITSW << add_shift | ADD_PHASE3H << add_shift | buf[16]);
99
 
 
100
 
        xfer.len = 2 * len;
101
 
        xfer.tx_buf = &config;
102
 
        mutex_lock(&st->lock);
103
 
 
104
 
        spi_message_init(&msg);
105
 
        spi_message_add_tail(&xfer, &msg);
106
 
        ret = spi_sync(st->sdev, &msg);
 
83
        long val;
 
84
 
 
85
        ret = strict_strtoul(buf, 10, &val);
107
86
        if (ret)
108
87
                goto error_ret;
 
88
 
 
89
        mutex_lock(&dev_info->mlock);
 
90
        switch (this_attr->address) {
 
91
        case AD9832_FREQ0HM:
 
92
        case AD9832_FREQ1HM:
 
93
                ret = ad9832_write_frequency(st, this_attr->address, val);
 
94
                break;
 
95
        case AD9832_PHASE0H:
 
96
        case AD9832_PHASE1H:
 
97
        case AD9832_PHASE2H:
 
98
        case AD9832_PHASE3H:
 
99
                ret = ad9832_write_phase(st, this_attr->address, val);
 
100
                break;
 
101
        case AD9832_PINCTRL_EN:
 
102
                if (val)
 
103
                        st->ctrl_ss &= ~AD9832_SELSRC;
 
104
                else
 
105
                        st->ctrl_ss |= AD9832_SELSRC;
 
106
                st->data = cpu_to_be16((AD9832_CMD_SYNCSELSRC << CMD_SHIFT) |
 
107
                                        st->ctrl_ss);
 
108
                ret = spi_sync(st->spi, &st->msg);
 
109
                break;
 
110
        case AD9832_FREQ_SYM:
 
111
                if (val == 1)
 
112
                        st->ctrl_fp |= AD9832_FREQ;
 
113
                else if (val == 0)
 
114
                        st->ctrl_fp &= ~AD9832_FREQ;
 
115
                else {
 
116
                        ret = -EINVAL;
 
117
                        break;
 
118
                }
 
119
                st->data = cpu_to_be16((AD9832_CMD_FPSELECT << CMD_SHIFT) |
 
120
                                        st->ctrl_fp);
 
121
                ret = spi_sync(st->spi, &st->msg);
 
122
                break;
 
123
        case AD9832_PHASE_SYM:
 
124
                if (val < 0 || val > 3) {
 
125
                        ret = -EINVAL;
 
126
                        break;
 
127
                }
 
128
 
 
129
                st->ctrl_fp &= ~AD9832_PHASE(3);
 
130
                st->ctrl_fp |= AD9832_PHASE(val);
 
131
 
 
132
                st->data = cpu_to_be16((AD9832_CMD_FPSELECT << CMD_SHIFT) |
 
133
                                        st->ctrl_fp);
 
134
                ret = spi_sync(st->spi, &st->msg);
 
135
                break;
 
136
        case AD9832_OUTPUT_EN:
 
137
                if (val)
 
138
                        st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP |
 
139
                                        AD9832_CLR);
 
140
                else
 
141
                        st->ctrl_src |= AD9832_RESET;
 
142
 
 
143
                st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
 
144
                                        st->ctrl_src);
 
145
                ret = spi_sync(st->spi, &st->msg);
 
146
                break;
 
147
        default:
 
148
                ret = -ENODEV;
 
149
        }
 
150
        mutex_unlock(&dev_info->mlock);
 
151
 
109
152
error_ret:
110
 
        mutex_unlock(&st->lock);
111
 
 
112
153
        return ret ? ret : len;
113
154
}
114
155
 
115
 
static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad9832_set_parameter, 0);
 
156
/**
 
157
 * see dds.h for further information
 
158
 */
 
159
 
 
160
static IIO_DEV_ATTR_FREQ(0, 0, S_IWUSR, NULL, ad9832_write, AD9832_FREQ0HM);
 
161
static IIO_DEV_ATTR_FREQ(0, 1, S_IWUSR, NULL, ad9832_write, AD9832_FREQ1HM);
 
162
static IIO_DEV_ATTR_FREQSYMBOL(0, S_IWUSR, NULL, ad9832_write, AD9832_FREQ_SYM);
 
163
static IIO_CONST_ATTR_FREQ_SCALE(0, "1"); /* 1Hz */
 
164
 
 
165
static IIO_DEV_ATTR_PHASE(0, 0, S_IWUSR, NULL, ad9832_write, AD9832_PHASE0H);
 
166
static IIO_DEV_ATTR_PHASE(0, 1, S_IWUSR, NULL, ad9832_write, AD9832_PHASE1H);
 
167
static IIO_DEV_ATTR_PHASE(0, 2, S_IWUSR, NULL, ad9832_write, AD9832_PHASE2H);
 
168
static IIO_DEV_ATTR_PHASE(0, 3, S_IWUSR, NULL, ad9832_write, AD9832_PHASE3H);
 
169
static IIO_DEV_ATTR_PHASESYMBOL(0, S_IWUSR, NULL,
 
170
                                ad9832_write, AD9832_PHASE_SYM);
 
171
static IIO_CONST_ATTR_PHASE_SCALE(0, "0.0015339808"); /* 2PI/2^12 rad*/
 
172
 
 
173
static IIO_DEV_ATTR_PINCONTROL_EN(0, S_IWUSR, NULL,
 
174
                                ad9832_write, AD9832_PINCTRL_EN);
 
175
static IIO_DEV_ATTR_OUT_ENABLE(0, S_IWUSR, NULL,
 
176
                                ad9832_write, AD9832_OUTPUT_EN);
116
177
 
117
178
static struct attribute *ad9832_attributes[] = {
118
 
        &iio_dev_attr_dds.dev_attr.attr,
 
179
        &iio_dev_attr_dds0_freq0.dev_attr.attr,
 
180
        &iio_dev_attr_dds0_freq1.dev_attr.attr,
 
181
        &iio_const_attr_dds0_freq_scale.dev_attr.attr,
 
182
        &iio_dev_attr_dds0_phase0.dev_attr.attr,
 
183
        &iio_dev_attr_dds0_phase1.dev_attr.attr,
 
184
        &iio_dev_attr_dds0_phase2.dev_attr.attr,
 
185
        &iio_dev_attr_dds0_phase3.dev_attr.attr,
 
186
        &iio_const_attr_dds0_phase_scale.dev_attr.attr,
 
187
        &iio_dev_attr_dds0_pincontrol_en.dev_attr.attr,
 
188
        &iio_dev_attr_dds0_freqsymbol.dev_attr.attr,
 
189
        &iio_dev_attr_dds0_phasesymbol.dev_attr.attr,
 
190
        &iio_dev_attr_dds0_out_enable.dev_attr.attr,
119
191
        NULL,
120
192
};
121
193
 
122
194
static const struct attribute_group ad9832_attribute_group = {
123
 
        .name = DRV_NAME,
124
195
        .attrs = ad9832_attributes,
125
196
};
126
197
 
127
 
static void ad9832_init(struct ad9832_state *st)
128
 
{
129
 
        struct spi_message msg;
130
 
        struct spi_transfer xfer;
131
 
        int ret;
132
 
        u16 config = 0;
133
 
 
134
 
        config = 0x3 << 14 | AD9832_SLEEP | AD9832_RESET | AD9832_CLR;
135
 
 
136
 
        mutex_lock(&st->lock);
137
 
 
138
 
        xfer.len = 2;
139
 
        xfer.tx_buf = &config;
140
 
 
141
 
        spi_message_init(&msg);
142
 
        spi_message_add_tail(&xfer, &msg);
143
 
        ret = spi_sync(st->sdev, &msg);
144
 
        if (ret)
145
 
                goto error_ret;
146
 
 
147
 
        config = 0x2 << 14 | AD9832_SYNC | AD9832_SELSRC;
148
 
        xfer.len = 2;
149
 
        xfer.tx_buf = &config;
150
 
 
151
 
        spi_message_init(&msg);
152
 
        spi_message_add_tail(&xfer, &msg);
153
 
        ret = spi_sync(st->sdev, &msg);
154
 
        if (ret)
155
 
                goto error_ret;
156
 
 
157
 
        config = CMD_SELBITSCTL << cmd_shift;
158
 
        xfer.len = 2;
159
 
        xfer.tx_buf = &config;
160
 
 
161
 
        spi_message_init(&msg);
162
 
        spi_message_add_tail(&xfer, &msg);
163
 
        ret = spi_sync(st->sdev, &msg);
164
 
        if (ret)
165
 
                goto error_ret;
166
 
 
167
 
        config = 0x3 << 14;
168
 
 
169
 
        xfer.len = 2;
170
 
        xfer.tx_buf = &config;
171
 
 
172
 
        spi_message_init(&msg);
173
 
        spi_message_add_tail(&xfer, &msg);
174
 
        ret = spi_sync(st->sdev, &msg);
175
 
        if (ret)
176
 
                goto error_ret;
177
 
error_ret:
178
 
        mutex_unlock(&st->lock);
179
 
 
180
 
 
181
 
 
182
 
}
 
198
static const struct iio_info ad9832_info = {
 
199
        .attrs = &ad9832_attribute_group,
 
200
        .driver_module = THIS_MODULE,
 
201
};
183
202
 
184
203
static int __devinit ad9832_probe(struct spi_device *spi)
185
204
{
 
205
        struct ad9832_platform_data *pdata = spi->dev.platform_data;
186
206
        struct ad9832_state *st;
187
 
        int ret = 0;
 
207
        int ret;
 
208
 
 
209
        if (!pdata) {
 
210
                dev_dbg(&spi->dev, "no platform data?\n");
 
211
                return -ENODEV;
 
212
        }
188
213
 
189
214
        st = kzalloc(sizeof(*st), GFP_KERNEL);
190
215
        if (st == NULL) {
191
216
                ret = -ENOMEM;
192
217
                goto error_ret;
193
218
        }
 
219
 
 
220
        st->reg = regulator_get(&spi->dev, "vcc");
 
221
        if (!IS_ERR(st->reg)) {
 
222
                ret = regulator_enable(st->reg);
 
223
                if (ret)
 
224
                        goto error_put_reg;
 
225
        }
 
226
 
 
227
        st->mclk = pdata->mclk;
 
228
 
194
229
        spi_set_drvdata(spi, st);
195
 
 
196
 
        mutex_init(&st->lock);
197
 
        st->sdev = spi;
198
 
 
199
 
        st->idev = iio_allocate_device();
200
 
        if (st->idev == NULL) {
 
230
        st->spi = spi;
 
231
 
 
232
        st->indio_dev = iio_allocate_device(0);
 
233
        if (st->indio_dev == NULL) {
201
234
                ret = -ENOMEM;
202
 
                goto error_free_st;
203
 
        }
204
 
        st->idev->dev.parent = &spi->dev;
205
 
        st->idev->num_interrupt_lines = 0;
206
 
        st->idev->event_attrs = NULL;
207
 
 
208
 
        st->idev->attrs = &ad9832_attribute_group;
209
 
        st->idev->dev_data = (void *)(st);
210
 
        st->idev->driver_module = THIS_MODULE;
211
 
        st->idev->modes = INDIO_DIRECT_MODE;
212
 
 
213
 
        ret = iio_device_register(st->idev);
214
 
        if (ret)
215
 
                goto error_free_dev;
216
 
        spi->max_speed_hz = 2000000;
217
 
        spi->mode = SPI_MODE_3;
218
 
        spi->bits_per_word = 16;
219
 
        spi_setup(spi);
220
 
        ad9832_init(st);
 
235
                goto error_disable_reg;
 
236
        }
 
237
 
 
238
        st->indio_dev->dev.parent = &spi->dev;
 
239
        st->indio_dev->name = spi_get_device_id(spi)->name;
 
240
        st->indio_dev->info = &ad9832_info;
 
241
        st->indio_dev->dev_data = (void *) st;
 
242
        st->indio_dev->modes = INDIO_DIRECT_MODE;
 
243
 
 
244
        /* Setup default messages */
 
245
 
 
246
        st->xfer.tx_buf = &st->data;
 
247
        st->xfer.len = 2;
 
248
 
 
249
        spi_message_init(&st->msg);
 
250
        spi_message_add_tail(&st->xfer, &st->msg);
 
251
 
 
252
        st->freq_xfer[0].tx_buf = &st->freq_data[0];
 
253
        st->freq_xfer[0].len = 2;
 
254
        st->freq_xfer[0].cs_change = 1;
 
255
        st->freq_xfer[1].tx_buf = &st->freq_data[1];
 
256
        st->freq_xfer[1].len = 2;
 
257
        st->freq_xfer[1].cs_change = 1;
 
258
        st->freq_xfer[2].tx_buf = &st->freq_data[2];
 
259
        st->freq_xfer[2].len = 2;
 
260
        st->freq_xfer[2].cs_change = 1;
 
261
        st->freq_xfer[3].tx_buf = &st->freq_data[3];
 
262
        st->freq_xfer[3].len = 2;
 
263
 
 
264
        spi_message_init(&st->freq_msg);
 
265
        spi_message_add_tail(&st->freq_xfer[0], &st->freq_msg);
 
266
        spi_message_add_tail(&st->freq_xfer[1], &st->freq_msg);
 
267
        spi_message_add_tail(&st->freq_xfer[2], &st->freq_msg);
 
268
        spi_message_add_tail(&st->freq_xfer[3], &st->freq_msg);
 
269
 
 
270
        st->phase_xfer[0].tx_buf = &st->phase_data[0];
 
271
        st->phase_xfer[0].len = 2;
 
272
        st->phase_xfer[0].cs_change = 1;
 
273
        st->phase_xfer[1].tx_buf = &st->phase_data[1];
 
274
        st->phase_xfer[1].len = 2;
 
275
 
 
276
        spi_message_init(&st->phase_msg);
 
277
        spi_message_add_tail(&st->phase_xfer[0], &st->phase_msg);
 
278
        spi_message_add_tail(&st->phase_xfer[1], &st->phase_msg);
 
279
 
 
280
        st->ctrl_src = AD9832_SLEEP | AD9832_RESET | AD9832_CLR;
 
281
        st->data = cpu_to_be16((AD9832_CMD_SLEEPRESCLR << CMD_SHIFT) |
 
282
                                        st->ctrl_src);
 
283
        ret = spi_sync(st->spi, &st->msg);
 
284
        if (ret) {
 
285
                dev_err(&spi->dev, "device init failed\n");
 
286
                goto error_free_device;
 
287
        }
 
288
 
 
289
        ret = ad9832_write_frequency(st, AD9832_FREQ0HM, pdata->freq0);
 
290
        if (ret)
 
291
                goto error_free_device;
 
292
 
 
293
        ret = ad9832_write_frequency(st, AD9832_FREQ1HM, pdata->freq1);
 
294
        if (ret)
 
295
                goto error_free_device;
 
296
 
 
297
        ret = ad9832_write_phase(st, AD9832_PHASE0H, pdata->phase0);
 
298
        if (ret)
 
299
                goto error_free_device;
 
300
 
 
301
        ret = ad9832_write_phase(st, AD9832_PHASE1H, pdata->phase1);
 
302
        if (ret)
 
303
                goto error_free_device;
 
304
 
 
305
        ret = ad9832_write_phase(st, AD9832_PHASE2H, pdata->phase2);
 
306
        if (ret)
 
307
                goto error_free_device;
 
308
 
 
309
        ret = ad9832_write_phase(st, AD9832_PHASE3H, pdata->phase3);
 
310
        if (ret)
 
311
                goto error_free_device;
 
312
 
 
313
        ret = iio_device_register(st->indio_dev);
 
314
        if (ret)
 
315
                goto error_free_device;
 
316
 
221
317
        return 0;
222
318
 
223
 
error_free_dev:
224
 
        iio_free_device(st->idev);
225
 
error_free_st:
 
319
error_free_device:
 
320
        iio_free_device(st->indio_dev);
 
321
error_disable_reg:
 
322
        if (!IS_ERR(st->reg))
 
323
                regulator_disable(st->reg);
 
324
error_put_reg:
 
325
        if (!IS_ERR(st->reg))
 
326
                regulator_put(st->reg);
226
327
        kfree(st);
227
328
error_ret:
228
329
        return ret;
232
333
{
233
334
        struct ad9832_state *st = spi_get_drvdata(spi);
234
335
 
235
 
        iio_device_unregister(st->idev);
 
336
        iio_device_unregister(st->indio_dev);
 
337
        if (!IS_ERR(st->reg)) {
 
338
                regulator_disable(st->reg);
 
339
                regulator_put(st->reg);
 
340
        }
236
341
        kfree(st);
237
 
 
238
342
        return 0;
239
343
}
240
344
 
 
345
static const struct spi_device_id ad9832_id[] = {
 
346
        {"ad9832", 0},
 
347
        {"ad9835", 0},
 
348
        {}
 
349
};
 
350
 
241
351
static struct spi_driver ad9832_driver = {
242
352
        .driver = {
243
 
                .name = DRV_NAME,
244
 
                .owner = THIS_MODULE,
 
353
                .name   = "ad9832",
 
354
                .bus    = &spi_bus_type,
 
355
                .owner  = THIS_MODULE,
245
356
        },
246
 
        .probe = ad9832_probe,
247
 
        .remove = __devexit_p(ad9832_remove),
 
357
        .probe          = ad9832_probe,
 
358
        .remove         = __devexit_p(ad9832_remove),
 
359
        .id_table       = ad9832_id,
248
360
};
249
361
 
250
 
static __init int ad9832_spi_init(void)
 
362
static int __init ad9832_init(void)
251
363
{
252
364
        return spi_register_driver(&ad9832_driver);
253
365
}
254
 
module_init(ad9832_spi_init);
 
366
module_init(ad9832_init);
255
367
 
256
 
static __exit void ad9832_spi_exit(void)
 
368
static void __exit ad9832_exit(void)
257
369
{
258
370
        spi_unregister_driver(&ad9832_driver);
259
371
}
260
 
module_exit(ad9832_spi_exit);
 
372
module_exit(ad9832_exit);
261
373
 
262
 
MODULE_AUTHOR("Cliff Cai");
263
 
MODULE_DESCRIPTION("Analog Devices ad9832 driver");
 
374
MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
 
375
MODULE_DESCRIPTION("Analog Devices AD9832/AD9835 DDS");
264
376
MODULE_LICENSE("GPL v2");
 
377
MODULE_ALIAS("spi:ad9832");