staging: brcm80211: remove brcms_b_dotxstatus wrapper function
[zen-stable.git] / drivers / staging / iio / dac / ad5686.c
blobb55ef42a18b0d3ce29553e434a5b14d17056bee2
1 /*
2 * AD5686R, AD5685R, AD5684R Digital to analog converters driver
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
7 */
9 #include <linux/interrupt.h>
10 #include <linux/gpio.h>
11 #include <linux/fs.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/regulator/consumer.h>
20 #include "../iio.h"
21 #include "../sysfs.h"
22 #include "dac.h"
24 #define AD5686_DAC_CHANNELS 4
26 #define AD5686_ADDR(x) ((x) << 16)
27 #define AD5686_CMD(x) ((x) << 20)
29 #define AD5686_ADDR_DAC0 0x1
30 #define AD5686_ADDR_DAC1 0x2
31 #define AD5686_ADDR_DAC2 0x4
32 #define AD5686_ADDR_DAC3 0x8
33 #define AD5686_ADDR_ALL_DAC 0xF
35 #define AD5686_CMD_NOOP 0x0
36 #define AD5686_CMD_WRITE_INPUT_N 0x1
37 #define AD5686_CMD_UPDATE_DAC_N 0x2
38 #define AD5686_CMD_WRITE_INPUT_N_UPDATE_N 0x3
39 #define AD5686_CMD_POWERDOWN_DAC 0x4
40 #define AD5686_CMD_LDAC_MASK 0x5
41 #define AD5686_CMD_RESET 0x6
42 #define AD5686_CMD_INTERNAL_REFER_SETUP 0x7
43 #define AD5686_CMD_DAISY_CHAIN_ENABLE 0x8
44 #define AD5686_CMD_READBACK_ENABLE 0x9
46 #define AD5686_LDAC_PWRDN_NONE 0x0
47 #define AD5686_LDAC_PWRDN_1K 0x1
48 #define AD5686_LDAC_PWRDN_100K 0x2
49 #define AD5686_LDAC_PWRDN_3STATE 0x3
51 /**
52 * struct ad5686_chip_info - chip specific information
53 * @int_vref_mv: AD5620/40/60: the internal reference voltage
54 * @channel: channel specification
57 struct ad5686_chip_info {
58 u16 int_vref_mv;
59 struct iio_chan_spec channel[AD5686_DAC_CHANNELS];
62 /**
63 * struct ad5446_state - driver instance specific data
64 * @spi: spi_device
65 * @chip_info: chip model specific constants, available modes etc
66 * @reg: supply regulator
67 * @vref_mv: actual reference voltage used
68 * @pwr_down_mask: power down mask
69 * @pwr_down_mode: current power down mode
70 * @data: spi transfer buffers
73 struct ad5686_state {
74 struct spi_device *spi;
75 const struct ad5686_chip_info *chip_info;
76 struct regulator *reg;
77 unsigned short vref_mv;
78 unsigned pwr_down_mask;
79 unsigned pwr_down_mode;
81 * DMA (thus cache coherency maintenance) requires the
82 * transfer buffers to live in their own cache lines.
85 union {
86 u32 d32;
87 u8 d8[4];
88 } data[3] ____cacheline_aligned;
91 /**
92 * ad5686_supported_device_ids:
95 enum ad5686_supported_device_ids {
96 ID_AD5684,
97 ID_AD5685,
98 ID_AD5686,
100 #define AD5868_CHANNEL(chan, bits, shift) { \
101 .type = IIO_VOLTAGE, \
102 .indexed = 1, \
103 .output = 1, \
104 .channel = chan, \
105 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED), \
106 .address = AD5686_ADDR_DAC0, \
107 .scan_type = IIO_ST('u', bits, 16, shift) \
109 static const struct ad5686_chip_info ad5686_chip_info_tbl[] = {
110 [ID_AD5684] = {
111 .channel[0] = AD5868_CHANNEL(0, 12, 4),
112 .channel[1] = AD5868_CHANNEL(1, 12, 4),
113 .channel[2] = AD5868_CHANNEL(2, 12, 4),
114 .channel[3] = AD5868_CHANNEL(3, 12, 4),
115 .int_vref_mv = 2500,
117 [ID_AD5685] = {
118 .channel[0] = AD5868_CHANNEL(0, 14, 2),
119 .channel[1] = AD5868_CHANNEL(1, 14, 2),
120 .channel[2] = AD5868_CHANNEL(2, 14, 2),
121 .channel[3] = AD5868_CHANNEL(3, 14, 2),
122 .int_vref_mv = 2500,
124 [ID_AD5686] = {
125 .channel[0] = AD5868_CHANNEL(0, 16, 0),
126 .channel[1] = AD5868_CHANNEL(1, 16, 0),
127 .channel[2] = AD5868_CHANNEL(2, 16, 0),
128 .channel[3] = AD5868_CHANNEL(3, 16, 0),
129 .int_vref_mv = 2500,
133 static int ad5686_spi_write(struct ad5686_state *st,
134 u8 cmd, u8 addr, u16 val, u8 shift)
136 val <<= shift;
138 st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) |
139 AD5686_ADDR(addr) |
140 val);
142 return spi_write(st->spi, &st->data[0].d8[1], 3);
145 static int ad5686_spi_read(struct ad5686_state *st, u8 addr)
147 struct spi_transfer t[] = {
149 .tx_buf = &st->data[0].d8[1],
150 .len = 3,
151 .cs_change = 1,
152 }, {
153 .tx_buf = &st->data[1].d8[1],
154 .rx_buf = &st->data[2].d8[1],
155 .len = 3,
158 struct spi_message m;
159 int ret;
161 spi_message_init(&m);
162 spi_message_add_tail(&t[0], &m);
163 spi_message_add_tail(&t[1], &m);
165 st->data[0].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_READBACK_ENABLE) |
166 AD5686_ADDR(addr));
167 st->data[1].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_NOOP));
169 ret = spi_sync(st->spi, &m);
170 if (ret < 0)
171 return ret;
173 return be32_to_cpu(st->data[2].d32);
176 static ssize_t ad5686_read_powerdown_mode(struct device *dev,
177 struct device_attribute *attr, char *buf)
179 struct iio_dev *indio_dev = dev_get_drvdata(dev);
180 struct ad5686_state *st = iio_priv(indio_dev);
181 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
183 char mode[][15] = {"", "1kohm_to_gnd", "100kohm_to_gnd", "three_state"};
185 return sprintf(buf, "%s\n", mode[(st->pwr_down_mode >>
186 (this_attr->address * 2)) & 0x3]);
189 static ssize_t ad5686_write_powerdown_mode(struct device *dev,
190 struct device_attribute *attr,
191 const char *buf, size_t len)
193 struct iio_dev *indio_dev = dev_get_drvdata(dev);
194 struct ad5686_state *st = iio_priv(indio_dev);
195 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
196 unsigned mode;
198 if (sysfs_streq(buf, "1kohm_to_gnd"))
199 mode = AD5686_LDAC_PWRDN_1K;
200 else if (sysfs_streq(buf, "100kohm_to_gnd"))
201 mode = AD5686_LDAC_PWRDN_100K;
202 else if (sysfs_streq(buf, "three_state"))
203 mode = AD5686_LDAC_PWRDN_3STATE;
204 else
205 return -EINVAL;
207 st->pwr_down_mode &= ~(0x3 << (this_attr->address * 2));
208 st->pwr_down_mode |= (mode << (this_attr->address * 2));
210 return len;
213 static ssize_t ad5686_read_dac_powerdown(struct device *dev,
214 struct device_attribute *attr,
215 char *buf)
217 struct iio_dev *indio_dev = dev_get_drvdata(dev);
218 struct ad5686_state *st = iio_priv(indio_dev);
219 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
221 return sprintf(buf, "%d\n", !!(st->pwr_down_mask &
222 (0x3 << (this_attr->address * 2))));
225 static ssize_t ad5686_write_dac_powerdown(struct device *dev,
226 struct device_attribute *attr,
227 const char *buf, size_t len)
229 bool readin;
230 int ret;
231 struct iio_dev *indio_dev = dev_get_drvdata(dev);
232 struct ad5686_state *st = iio_priv(indio_dev);
233 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
235 ret = strtobool(buf, &readin);
236 if (ret)
237 return ret;
239 if (readin == true)
240 st->pwr_down_mask |= (0x3 << (this_attr->address * 2));
241 else
242 st->pwr_down_mask &= ~(0x3 << (this_attr->address * 2));
244 ret = ad5686_spi_write(st, AD5686_CMD_POWERDOWN_DAC, 0,
245 st->pwr_down_mask & st->pwr_down_mode, 0);
247 return ret ? ret : len;
250 static IIO_CONST_ATTR(out_voltage_powerdown_mode_available,
251 "1kohm_to_gnd 100kohm_to_gnd three_state");
253 #define IIO_DEV_ATTR_DAC_POWERDOWN_MODE(_num) \
254 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown_mode, \
255 S_IRUGO | S_IWUSR, \
256 ad5686_read_powerdown_mode, \
257 ad5686_write_powerdown_mode, _num)
259 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(0);
260 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(1);
261 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(2);
262 static IIO_DEV_ATTR_DAC_POWERDOWN_MODE(3);
264 #define IIO_DEV_ATTR_DAC_POWERDOWN(_num) \
265 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown, \
266 S_IRUGO | S_IWUSR, \
267 ad5686_read_dac_powerdown, \
268 ad5686_write_dac_powerdown, _num)
270 static IIO_DEV_ATTR_DAC_POWERDOWN(0);
271 static IIO_DEV_ATTR_DAC_POWERDOWN(1);
272 static IIO_DEV_ATTR_DAC_POWERDOWN(2);
273 static IIO_DEV_ATTR_DAC_POWERDOWN(3);
275 static struct attribute *ad5686_attributes[] = {
276 &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr,
277 &iio_dev_attr_out_voltage1_powerdown.dev_attr.attr,
278 &iio_dev_attr_out_voltage2_powerdown.dev_attr.attr,
279 &iio_dev_attr_out_voltage3_powerdown.dev_attr.attr,
280 &iio_dev_attr_out_voltage0_powerdown_mode.dev_attr.attr,
281 &iio_dev_attr_out_voltage1_powerdown_mode.dev_attr.attr,
282 &iio_dev_attr_out_voltage2_powerdown_mode.dev_attr.attr,
283 &iio_dev_attr_out_voltage3_powerdown_mode.dev_attr.attr,
284 &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr,
285 NULL,
288 static const struct attribute_group ad5686_attribute_group = {
289 .attrs = ad5686_attributes,
292 static int ad5686_read_raw(struct iio_dev *indio_dev,
293 struct iio_chan_spec const *chan,
294 int *val,
295 int *val2,
296 long m)
298 struct ad5686_state *st = iio_priv(indio_dev);
299 unsigned long scale_uv;
300 int ret;
302 switch (m) {
303 case 0:
304 mutex_lock(&indio_dev->mlock);
305 ret = ad5686_spi_read(st, chan->address);
306 mutex_unlock(&indio_dev->mlock);
307 if (ret < 0)
308 return ret;
309 *val = ret;
310 return IIO_VAL_INT;
311 break;
312 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
313 scale_uv = (st->vref_mv * 100000)
314 >> (chan->scan_type.realbits);
315 *val = scale_uv / 100000;
316 *val2 = (scale_uv % 100000) * 10;
317 return IIO_VAL_INT_PLUS_MICRO;
320 return -EINVAL;
323 static int ad5686_write_raw(struct iio_dev *indio_dev,
324 struct iio_chan_spec const *chan,
325 int val,
326 int val2,
327 long mask)
329 struct ad5686_state *st = iio_priv(indio_dev);
330 int ret;
332 switch (mask) {
333 case 0:
334 if (val > (1 << chan->scan_type.realbits))
335 return -EINVAL;
337 mutex_lock(&indio_dev->mlock);
338 ret = ad5686_spi_write(st,
339 AD5686_CMD_WRITE_INPUT_N_UPDATE_N,
340 chan->address,
341 val,
342 chan->scan_type.shift);
343 mutex_unlock(&indio_dev->mlock);
344 break;
345 default:
346 ret = -EINVAL;
349 return ret;
352 static const struct iio_info ad5686_info = {
353 .read_raw = ad5686_read_raw,
354 .write_raw = ad5686_write_raw,
355 .attrs = &ad5686_attribute_group,
356 .driver_module = THIS_MODULE,
359 static int __devinit ad5686_probe(struct spi_device *spi)
361 struct ad5686_state *st;
362 struct iio_dev *indio_dev;
363 int ret, regdone = 0, voltage_uv = 0;
365 indio_dev = iio_allocate_device(sizeof(*st));
366 if (indio_dev == NULL)
367 return -ENOMEM;
369 st = iio_priv(indio_dev);
370 spi_set_drvdata(spi, indio_dev);
372 st->reg = regulator_get(&spi->dev, "vcc");
373 if (!IS_ERR(st->reg)) {
374 ret = regulator_enable(st->reg);
375 if (ret)
376 goto error_put_reg;
378 voltage_uv = regulator_get_voltage(st->reg);
381 st->chip_info =
382 &ad5686_chip_info_tbl[spi_get_device_id(spi)->driver_data];
384 if (voltage_uv)
385 st->vref_mv = voltage_uv / 1000;
386 else
387 st->vref_mv = st->chip_info->int_vref_mv;
389 st->spi = spi;
391 indio_dev->dev.parent = &spi->dev;
392 indio_dev->name = spi_get_device_id(spi)->name;
393 indio_dev->info = &ad5686_info;
394 indio_dev->modes = INDIO_DIRECT_MODE;
395 indio_dev->channels = st->chip_info->channel;
396 indio_dev->num_channels = AD5686_DAC_CHANNELS;
398 regdone = 1;
399 ret = ad5686_spi_write(st, AD5686_CMD_INTERNAL_REFER_SETUP, 0,
400 !!voltage_uv, 0);
401 if (ret)
402 goto error_disable_reg;
404 ret = iio_device_register(indio_dev);
405 if (ret)
406 goto error_disable_reg;
408 return 0;
410 error_disable_reg:
411 if (!IS_ERR(st->reg))
412 regulator_disable(st->reg);
413 error_put_reg:
414 if (!IS_ERR(st->reg))
415 regulator_put(st->reg);
417 iio_free_device(indio_dev);
419 return ret;
422 static int __devexit ad5686_remove(struct spi_device *spi)
424 struct iio_dev *indio_dev = spi_get_drvdata(spi);
425 struct ad5686_state *st = iio_priv(indio_dev);
427 if (!IS_ERR(st->reg)) {
428 regulator_disable(st->reg);
429 regulator_put(st->reg);
432 iio_device_unregister(indio_dev);
434 return 0;
437 static const struct spi_device_id ad5686_id[] = {
438 {"ad5684", ID_AD5684},
439 {"ad5685", ID_AD5685},
440 {"ad5686", ID_AD5686},
444 static struct spi_driver ad5686_driver = {
445 .driver = {
446 .name = "ad5686",
447 .owner = THIS_MODULE,
449 .probe = ad5686_probe,
450 .remove = __devexit_p(ad5686_remove),
451 .id_table = ad5686_id,
454 static __init int ad5686_spi_init(void)
456 return spi_register_driver(&ad5686_driver);
458 module_init(ad5686_spi_init);
460 static __exit void ad5686_spi_exit(void)
462 spi_unregister_driver(&ad5686_driver);
464 module_exit(ad5686_spi_exit);
466 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
467 MODULE_DESCRIPTION("Analog Devices AD5686/85/84 DAC");
468 MODULE_LICENSE("GPL v2");