spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / staging / iio / dac / ad5791.c
blobac45636a8d72e2dcf4c9e37a7b54b23922da5bed
1 /*
2 * AD5760, AD5780, AD5781, AD5790, AD5791 Voltage Output Digital to Analog
3 * Converter
5 * Copyright 2011 Analog Devices Inc.
7 * Licensed under the GPL-2.
8 */
10 #include <linux/interrupt.h>
11 #include <linux/fs.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/module.h>
20 #include "../iio.h"
21 #include "../sysfs.h"
22 #include "dac.h"
23 #include "ad5791.h"
25 static int ad5791_spi_write(struct spi_device *spi, u8 addr, u32 val)
27 union {
28 u32 d32;
29 u8 d8[4];
30 } data;
32 data.d32 = cpu_to_be32(AD5791_CMD_WRITE |
33 AD5791_ADDR(addr) |
34 (val & AD5791_DAC_MASK));
36 return spi_write(spi, &data.d8[1], 3);
39 static int ad5791_spi_read(struct spi_device *spi, u8 addr, u32 *val)
41 union {
42 u32 d32;
43 u8 d8[4];
44 } data[3];
45 int ret;
46 struct spi_message msg;
47 struct spi_transfer xfers[] = {
49 .tx_buf = &data[0].d8[1],
50 .bits_per_word = 8,
51 .len = 3,
52 .cs_change = 1,
53 }, {
54 .tx_buf = &data[1].d8[1],
55 .rx_buf = &data[2].d8[1],
56 .bits_per_word = 8,
57 .len = 3,
61 data[0].d32 = cpu_to_be32(AD5791_CMD_READ |
62 AD5791_ADDR(addr));
63 data[1].d32 = cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP));
65 spi_message_init(&msg);
66 spi_message_add_tail(&xfers[0], &msg);
67 spi_message_add_tail(&xfers[1], &msg);
68 ret = spi_sync(spi, &msg);
70 *val = be32_to_cpu(data[2].d32);
72 return ret;
75 #define AD5791_CHAN(bits, shift) { \
76 .type = IIO_VOLTAGE, \
77 .output = 1, \
78 .indexed = 1, \
79 .address = AD5791_ADDR_DAC0, \
80 .channel = 0, \
81 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT | \
82 IIO_CHAN_INFO_OFFSET_SHARED_BIT, \
83 .scan_type = IIO_ST('u', bits, 24, shift) \
86 static const struct iio_chan_spec ad5791_channels[] = {
87 [ID_AD5760] = AD5791_CHAN(16, 4),
88 [ID_AD5780] = AD5791_CHAN(18, 2),
89 [ID_AD5781] = AD5791_CHAN(18, 2),
90 [ID_AD5791] = AD5791_CHAN(20, 0)
93 static ssize_t ad5791_read_powerdown_mode(struct device *dev,
94 struct device_attribute *attr, char *buf)
96 struct iio_dev *indio_dev = dev_get_drvdata(dev);
97 struct ad5791_state *st = iio_priv(indio_dev);
99 const char mode[][14] = {"6kohm_to_gnd", "three_state"};
101 return sprintf(buf, "%s\n", mode[st->pwr_down_mode]);
104 static ssize_t ad5791_write_powerdown_mode(struct device *dev,
105 struct device_attribute *attr,
106 const char *buf, size_t len)
108 struct iio_dev *indio_dev = dev_get_drvdata(dev);
109 struct ad5791_state *st = iio_priv(indio_dev);
110 int ret;
112 if (sysfs_streq(buf, "6kohm_to_gnd"))
113 st->pwr_down_mode = AD5791_DAC_PWRDN_6K;
114 else if (sysfs_streq(buf, "three_state"))
115 st->pwr_down_mode = AD5791_DAC_PWRDN_3STATE;
116 else
117 ret = -EINVAL;
119 return ret ? ret : len;
122 static ssize_t ad5791_read_dac_powerdown(struct device *dev,
123 struct device_attribute *attr,
124 char *buf)
126 struct iio_dev *indio_dev = dev_get_drvdata(dev);
127 struct ad5791_state *st = iio_priv(indio_dev);
129 return sprintf(buf, "%d\n", st->pwr_down);
132 static ssize_t ad5791_write_dac_powerdown(struct device *dev,
133 struct device_attribute *attr,
134 const char *buf, size_t len)
136 long readin;
137 int ret;
138 struct iio_dev *indio_dev = dev_get_drvdata(dev);
139 struct ad5791_state *st = iio_priv(indio_dev);
141 ret = strict_strtol(buf, 10, &readin);
142 if (ret)
143 return ret;
145 if (readin == 0) {
146 st->pwr_down = false;
147 st->ctrl &= ~(AD5791_CTRL_OPGND | AD5791_CTRL_DACTRI);
148 } else if (readin == 1) {
149 st->pwr_down = true;
150 if (st->pwr_down_mode == AD5791_DAC_PWRDN_6K)
151 st->ctrl |= AD5791_CTRL_OPGND;
152 else if (st->pwr_down_mode == AD5791_DAC_PWRDN_3STATE)
153 st->ctrl |= AD5791_CTRL_DACTRI;
154 } else
155 ret = -EINVAL;
157 ret = ad5791_spi_write(st->spi, AD5791_ADDR_CTRL, st->ctrl);
159 return ret ? ret : len;
162 static IIO_DEVICE_ATTR(out_voltage_powerdown_mode, S_IRUGO |
163 S_IWUSR, ad5791_read_powerdown_mode,
164 ad5791_write_powerdown_mode, 0);
166 static IIO_CONST_ATTR(out_voltage_powerdown_mode_available,
167 "6kohm_to_gnd three_state");
169 #define IIO_DEV_ATTR_DAC_POWERDOWN(_num, _show, _store, _addr) \
170 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown, \
171 S_IRUGO | S_IWUSR, _show, _store, _addr)
173 static IIO_DEV_ATTR_DAC_POWERDOWN(0, ad5791_read_dac_powerdown,
174 ad5791_write_dac_powerdown, 0);
176 static struct attribute *ad5791_attributes[] = {
177 &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr,
178 &iio_dev_attr_out_voltage_powerdown_mode.dev_attr.attr,
179 &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr,
180 NULL,
183 static const struct attribute_group ad5791_attribute_group = {
184 .attrs = ad5791_attributes,
187 static int ad5791_get_lin_comp(unsigned int span)
189 if (span <= 10000)
190 return AD5791_LINCOMP_0_10;
191 else if (span <= 12000)
192 return AD5791_LINCOMP_10_12;
193 else if (span <= 16000)
194 return AD5791_LINCOMP_12_16;
195 else if (span <= 19000)
196 return AD5791_LINCOMP_16_19;
197 else
198 return AD5791_LINCOMP_19_20;
201 static int ad5780_get_lin_comp(unsigned int span)
203 if (span <= 10000)
204 return AD5780_LINCOMP_0_10;
205 else
206 return AD5780_LINCOMP_10_20;
208 static const struct ad5791_chip_info ad5791_chip_info_tbl[] = {
209 [ID_AD5760] = {
210 .get_lin_comp = ad5780_get_lin_comp,
212 [ID_AD5780] = {
213 .get_lin_comp = ad5780_get_lin_comp,
215 [ID_AD5781] = {
216 .get_lin_comp = ad5791_get_lin_comp,
218 [ID_AD5791] = {
219 .get_lin_comp = ad5791_get_lin_comp,
223 static int ad5791_read_raw(struct iio_dev *indio_dev,
224 struct iio_chan_spec const *chan,
225 int *val,
226 int *val2,
227 long m)
229 struct ad5791_state *st = iio_priv(indio_dev);
230 u64 val64;
231 int ret;
233 switch (m) {
234 case 0:
235 ret = ad5791_spi_read(st->spi, chan->address, val);
236 if (ret)
237 return ret;
238 *val &= AD5791_DAC_MASK;
239 *val >>= chan->scan_type.shift;
240 return IIO_VAL_INT;
241 case IIO_CHAN_INFO_SCALE:
242 *val = 0;
243 *val2 = (((u64)st->vref_mv) * 1000000ULL) >> chan->scan_type.realbits;
244 return IIO_VAL_INT_PLUS_MICRO;
245 case IIO_CHAN_INFO_OFFSET:
246 val64 = (((u64)st->vref_neg_mv) << chan->scan_type.realbits);
247 do_div(val64, st->vref_mv);
248 *val = -val64;
249 return IIO_VAL_INT;
250 default:
251 return -EINVAL;
257 static int ad5791_write_raw(struct iio_dev *indio_dev,
258 struct iio_chan_spec const *chan,
259 int val,
260 int val2,
261 long mask)
263 struct ad5791_state *st = iio_priv(indio_dev);
265 switch (mask) {
266 case 0:
267 val &= AD5791_RES_MASK(chan->scan_type.realbits);
268 val <<= chan->scan_type.shift;
270 return ad5791_spi_write(st->spi, chan->address, val);
272 default:
273 return -EINVAL;
277 static const struct iio_info ad5791_info = {
278 .read_raw = &ad5791_read_raw,
279 .write_raw = &ad5791_write_raw,
280 .attrs = &ad5791_attribute_group,
281 .driver_module = THIS_MODULE,
284 static int __devinit ad5791_probe(struct spi_device *spi)
286 struct ad5791_platform_data *pdata = spi->dev.platform_data;
287 struct iio_dev *indio_dev;
288 struct ad5791_state *st;
289 int ret, pos_voltage_uv = 0, neg_voltage_uv = 0;
291 indio_dev = iio_allocate_device(sizeof(*st));
292 if (indio_dev == NULL) {
293 ret = -ENOMEM;
294 goto error_ret;
296 st = iio_priv(indio_dev);
297 st->reg_vdd = regulator_get(&spi->dev, "vdd");
298 if (!IS_ERR(st->reg_vdd)) {
299 ret = regulator_enable(st->reg_vdd);
300 if (ret)
301 goto error_put_reg_pos;
303 pos_voltage_uv = regulator_get_voltage(st->reg_vdd);
306 st->reg_vss = regulator_get(&spi->dev, "vss");
307 if (!IS_ERR(st->reg_vss)) {
308 ret = regulator_enable(st->reg_vss);
309 if (ret)
310 goto error_put_reg_neg;
312 neg_voltage_uv = regulator_get_voltage(st->reg_vss);
315 st->pwr_down = true;
316 st->spi = spi;
318 if (!IS_ERR(st->reg_vss) && !IS_ERR(st->reg_vdd)) {
319 st->vref_mv = (pos_voltage_uv + neg_voltage_uv) / 1000;
320 st->vref_neg_mv = neg_voltage_uv / 1000;
321 } else if (pdata) {
322 st->vref_mv = pdata->vref_pos_mv + pdata->vref_neg_mv;
323 st->vref_neg_mv = pdata->vref_neg_mv;
324 } else {
325 dev_warn(&spi->dev, "reference voltage unspecified\n");
328 ret = ad5791_spi_write(spi, AD5791_ADDR_SW_CTRL, AD5791_SWCTRL_RESET);
329 if (ret)
330 goto error_disable_reg_neg;
332 st->chip_info = &ad5791_chip_info_tbl[spi_get_device_id(spi)
333 ->driver_data];
336 st->ctrl = AD5761_CTRL_LINCOMP(st->chip_info->get_lin_comp(st->vref_mv))
337 | ((pdata && pdata->use_rbuf_gain2) ? 0 : AD5791_CTRL_RBUF) |
338 AD5791_CTRL_BIN2SC;
340 ret = ad5791_spi_write(spi, AD5791_ADDR_CTRL, st->ctrl |
341 AD5791_CTRL_OPGND | AD5791_CTRL_DACTRI);
342 if (ret)
343 goto error_disable_reg_neg;
345 spi_set_drvdata(spi, indio_dev);
346 indio_dev->dev.parent = &spi->dev;
347 indio_dev->info = &ad5791_info;
348 indio_dev->modes = INDIO_DIRECT_MODE;
349 indio_dev->channels
350 = &ad5791_channels[spi_get_device_id(spi)->driver_data];
351 indio_dev->num_channels = 1;
352 indio_dev->name = spi_get_device_id(st->spi)->name;
353 ret = iio_device_register(indio_dev);
354 if (ret)
355 goto error_disable_reg_neg;
357 return 0;
359 error_disable_reg_neg:
360 if (!IS_ERR(st->reg_vss))
361 regulator_disable(st->reg_vss);
362 error_put_reg_neg:
363 if (!IS_ERR(st->reg_vss))
364 regulator_put(st->reg_vss);
366 if (!IS_ERR(st->reg_vdd))
367 regulator_disable(st->reg_vdd);
368 error_put_reg_pos:
369 if (!IS_ERR(st->reg_vdd))
370 regulator_put(st->reg_vdd);
371 iio_free_device(indio_dev);
372 error_ret:
374 return ret;
377 static int __devexit ad5791_remove(struct spi_device *spi)
379 struct iio_dev *indio_dev = spi_get_drvdata(spi);
380 struct ad5791_state *st = iio_priv(indio_dev);
382 iio_device_unregister(indio_dev);
383 if (!IS_ERR(st->reg_vdd)) {
384 regulator_disable(st->reg_vdd);
385 regulator_put(st->reg_vdd);
388 if (!IS_ERR(st->reg_vss)) {
389 regulator_disable(st->reg_vss);
390 regulator_put(st->reg_vss);
392 iio_free_device(indio_dev);
394 return 0;
397 static const struct spi_device_id ad5791_id[] = {
398 {"ad5760", ID_AD5760},
399 {"ad5780", ID_AD5780},
400 {"ad5781", ID_AD5781},
401 {"ad5790", ID_AD5791},
402 {"ad5791", ID_AD5791},
405 MODULE_DEVICE_TABLE(spi, ad5791_id);
407 static struct spi_driver ad5791_driver = {
408 .driver = {
409 .name = "ad5791",
410 .owner = THIS_MODULE,
412 .probe = ad5791_probe,
413 .remove = __devexit_p(ad5791_remove),
414 .id_table = ad5791_id,
416 module_spi_driver(ad5791_driver);
418 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
419 MODULE_DESCRIPTION("Analog Devices AD5760/AD5780/AD5781/AD5790/AD5791 DAC");
420 MODULE_LICENSE("GPL v2");