staging:iio:dds:ad9850 allocate chip state with iio_dev + fix name of attr group.
[deliverable/linux.git] / drivers / staging / iio / dds / ad9850.c
1 /*
2 * Driver for ADI Direct Digital Synthesis ad9850
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 "ad9850"
22
23 #define value_mask (u16)0xf000
24 #define addr_shift 12
25
26 /* Register format: 4 bits addr + 12 bits value */
27 struct ad9850_config {
28 u8 control[5];
29 };
30
31 struct ad9850_state {
32 struct mutex lock;
33 struct spi_device *sdev;
34 };
35
36 static ssize_t ad9850_set_parameter(struct device *dev,
37 struct device_attribute *attr,
38 const char *buf,
39 size_t len)
40 {
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);
47
48 xfer.len = len;
49 xfer.tx_buf = config;
50 mutex_lock(&st->lock);
51
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);
59
60 return ret ? ret : len;
61 }
62
63 static IIO_DEVICE_ATTR(dds, S_IWUSR, NULL, ad9850_set_parameter, 0);
64
65 static struct attribute *ad9850_attributes[] = {
66 &iio_dev_attr_dds.dev_attr.attr,
67 NULL,
68 };
69
70 static const struct attribute_group ad9850_attribute_group = {
71 .attrs = ad9850_attributes,
72 };
73
74 static const struct iio_info ad9850_info = {
75 .attrs = &ad9850_attribute_group,
76 .driver_module = THIS_MODULE,
77 };
78
79 static int __devinit ad9850_probe(struct spi_device *spi)
80 {
81 struct ad9850_state *st;
82 struct iio_dev *idev;
83 int ret = 0;
84
85 idev = iio_allocate_device(sizeof(*st));
86 if (idev == NULL) {
87 ret = -ENOMEM;
88 goto error_ret;
89 }
90 spi_set_drvdata(spi, idev);
91 st = iio_priv(idev);
92 mutex_init(&st->lock);
93 st->sdev = spi;
94
95 idev->dev.parent = &spi->dev;
96 idev->info = &ad9850_info;
97 idev->modes = INDIO_DIRECT_MODE;
98
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);
106
107 return 0;
108
109 error_free_dev:
110 iio_free_device(idev);
111 error_ret:
112 return ret;
113 }
114
115 static int __devexit ad9850_remove(struct spi_device *spi)
116 {
117 iio_device_unregister(spi_get_drvdata(spi));
118
119 return 0;
120 }
121
122 static struct spi_driver ad9850_driver = {
123 .driver = {
124 .name = DRV_NAME,
125 .owner = THIS_MODULE,
126 },
127 .probe = ad9850_probe,
128 .remove = __devexit_p(ad9850_remove),
129 };
130
131 static __init int ad9850_spi_init(void)
132 {
133 return spi_register_driver(&ad9850_driver);
134 }
135 module_init(ad9850_spi_init);
136
137 static __exit void ad9850_spi_exit(void)
138 {
139 spi_unregister_driver(&ad9850_driver);
140 }
141 module_exit(ad9850_spi_exit);
142
143 MODULE_AUTHOR("Cliff Cai");
144 MODULE_DESCRIPTION("Analog Devices ad9850 driver");
145 MODULE_LICENSE("GPL v2");
This page took 0.034763 seconds and 5 git commands to generate.