spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / staging / iio / dds / ad9850.c
blobf4f731bb219115b7833a68db1973ecb295a8e28d
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>
17 #include <linux/module.h>
19 #include "../iio.h"
20 #include "../sysfs.h"
22 #define DRV_NAME "ad9850"
24 #define value_mask (u16)0xf000
25 #define addr_shift 12
27 /* Register format: 4 bits addr + 12 bits value */
28 struct ad9850_config {
29 u8 control[5];
32 struct ad9850_state {
33 struct mutex lock;
34 struct spi_device *sdev;
37 static ssize_t ad9850_set_parameter(struct device *dev,
38 struct device_attribute *attr,
39 const char *buf,
40 size_t len)
42 struct spi_message msg;
43 struct spi_transfer xfer;
44 int ret;
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);
49 xfer.len = len;
50 xfer.tx_buf = config;
51 mutex_lock(&st->lock);
53 spi_message_init(&msg);
54 spi_message_add_tail(&xfer, &msg);
55 ret = spi_sync(st->sdev, &msg);
56 if (ret)
57 goto error_ret;
58 error_ret:
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,
68 NULL,
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;
83 struct iio_dev *idev;
84 int ret = 0;
86 idev = iio_allocate_device(sizeof(*st));
87 if (idev == NULL) {
88 ret = -ENOMEM;
89 goto error_ret;
91 spi_set_drvdata(spi, idev);
92 st = iio_priv(idev);
93 mutex_init(&st->lock);
94 st->sdev = spi;
96 idev->dev.parent = &spi->dev;
97 idev->info = &ad9850_info;
98 idev->modes = INDIO_DIRECT_MODE;
100 ret = iio_device_register(idev);
101 if (ret)
102 goto error_free_dev;
103 spi->max_speed_hz = 2000000;
104 spi->mode = SPI_MODE_3;
105 spi->bits_per_word = 16;
106 spi_setup(spi);
108 return 0;
110 error_free_dev:
111 iio_free_device(idev);
112 error_ret:
113 return ret;
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));
121 return 0;
124 static struct spi_driver ad9850_driver = {
125 .driver = {
126 .name = DRV_NAME,
127 .owner = THIS_MODULE,
129 .probe = ad9850_probe,
130 .remove = __devexit_p(ad9850_remove),
132 module_spi_driver(ad9850_driver);
134 MODULE_AUTHOR("Cliff Cai");
135 MODULE_DESCRIPTION("Analog Devices ad9850 driver");
136 MODULE_LICENSE("GPL v2");
137 MODULE_ALIAS("spi:" DRV_NAME);