i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / staging / iio / dac / ad5504.c
blobbc17205fe722dabbd56ca65be45b95728337a292
1 /*
2 * AD5504, AD5501 High Voltage Digital to Analog Converter
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
7 */
9 #include <linux/interrupt.h>
10 #include <linux/fs.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/module.h>
19 #include "../iio.h"
20 #include "../sysfs.h"
21 #include "../events.h"
22 #include "dac.h"
23 #include "ad5504.h"
25 #define AD5504_CHANNEL(_chan) { \
26 .type = IIO_VOLTAGE, \
27 .indexed = 1, \
28 .output = 1, \
29 .channel = (_chan), \
30 .info_mask = IIO_CHAN_INFO_SCALE_SHARED_BIT, \
31 .address = AD5504_ADDR_DAC(_chan), \
32 .scan_type = IIO_ST('u', 12, 16, 0), \
35 static const struct iio_chan_spec ad5504_channels[] = {
36 AD5504_CHANNEL(0),
37 AD5504_CHANNEL(1),
38 AD5504_CHANNEL(2),
39 AD5504_CHANNEL(3),
42 static int ad5504_spi_write(struct spi_device *spi, u8 addr, u16 val)
44 u16 tmp = cpu_to_be16(AD5504_CMD_WRITE |
45 AD5504_ADDR(addr) |
46 (val & AD5504_RES_MASK));
48 return spi_write(spi, (u8 *)&tmp, 2);
51 static int ad5504_spi_read(struct spi_device *spi, u8 addr)
53 u16 tmp = cpu_to_be16(AD5504_CMD_READ | AD5504_ADDR(addr));
54 u16 val;
55 int ret;
56 struct spi_transfer t = {
57 .tx_buf = &tmp,
58 .rx_buf = &val,
59 .len = 2,
61 struct spi_message m;
63 spi_message_init(&m);
64 spi_message_add_tail(&t, &m);
65 ret = spi_sync(spi, &m);
67 if (ret < 0)
68 return ret;
70 return be16_to_cpu(val) & AD5504_RES_MASK;
73 static int ad5504_read_raw(struct iio_dev *indio_dev,
74 struct iio_chan_spec const *chan,
75 int *val,
76 int *val2,
77 long m)
79 struct ad5504_state *st = iio_priv(indio_dev);
80 unsigned long scale_uv;
81 int ret;
83 switch (m) {
84 case 0:
85 ret = ad5504_spi_read(st->spi, chan->address);
86 if (ret < 0)
87 return ret;
89 *val = ret;
91 return IIO_VAL_INT;
92 case IIO_CHAN_INFO_SCALE:
93 scale_uv = (st->vref_mv * 1000) >> chan->scan_type.realbits;
94 *val = scale_uv / 1000;
95 *val2 = (scale_uv % 1000) * 1000;
96 return IIO_VAL_INT_PLUS_MICRO;
99 return -EINVAL;
102 static int ad5504_write_raw(struct iio_dev *indio_dev,
103 struct iio_chan_spec const *chan,
104 int val,
105 int val2,
106 long mask)
108 struct ad5504_state *st = iio_priv(indio_dev);
109 int ret;
111 switch (mask) {
112 case 0:
113 if (val >= (1 << chan->scan_type.realbits) || val < 0)
114 return -EINVAL;
116 return ad5504_spi_write(st->spi, chan->address, val);
117 default:
118 ret = -EINVAL;
121 return -EINVAL;
124 static ssize_t ad5504_read_powerdown_mode(struct device *dev,
125 struct device_attribute *attr, char *buf)
127 struct iio_dev *indio_dev = dev_get_drvdata(dev);
128 struct ad5504_state *st = iio_priv(indio_dev);
130 const char mode[][14] = {"20kohm_to_gnd", "three_state"};
132 return sprintf(buf, "%s\n", mode[st->pwr_down_mode]);
135 static ssize_t ad5504_write_powerdown_mode(struct device *dev,
136 struct device_attribute *attr,
137 const char *buf, size_t len)
139 struct iio_dev *indio_dev = dev_get_drvdata(dev);
140 struct ad5504_state *st = iio_priv(indio_dev);
141 int ret;
143 if (sysfs_streq(buf, "20kohm_to_gnd"))
144 st->pwr_down_mode = AD5504_DAC_PWRDN_20K;
145 else if (sysfs_streq(buf, "three_state"))
146 st->pwr_down_mode = AD5504_DAC_PWRDN_3STATE;
147 else
148 ret = -EINVAL;
150 return ret ? ret : len;
153 static ssize_t ad5504_read_dac_powerdown(struct device *dev,
154 struct device_attribute *attr,
155 char *buf)
157 struct iio_dev *indio_dev = dev_get_drvdata(dev);
158 struct ad5504_state *st = iio_priv(indio_dev);
159 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
161 return sprintf(buf, "%d\n",
162 !(st->pwr_down_mask & (1 << this_attr->address)));
165 static ssize_t ad5504_write_dac_powerdown(struct device *dev,
166 struct device_attribute *attr,
167 const char *buf, size_t len)
169 long readin;
170 int ret;
171 struct iio_dev *indio_dev = dev_get_drvdata(dev);
172 struct ad5504_state *st = iio_priv(indio_dev);
173 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
175 ret = strict_strtol(buf, 10, &readin);
176 if (ret)
177 return ret;
179 if (readin == 0)
180 st->pwr_down_mask |= (1 << this_attr->address);
181 else if (readin == 1)
182 st->pwr_down_mask &= ~(1 << this_attr->address);
183 else
184 ret = -EINVAL;
186 ret = ad5504_spi_write(st->spi, AD5504_ADDR_CTRL,
187 AD5504_DAC_PWRDWN_MODE(st->pwr_down_mode) |
188 AD5504_DAC_PWR(st->pwr_down_mask));
190 /* writes to the CTRL register must be followed by a NOOP */
191 ad5504_spi_write(st->spi, AD5504_ADDR_NOOP, 0);
193 return ret ? ret : len;
196 static IIO_DEVICE_ATTR(out_voltage_powerdown_mode, S_IRUGO |
197 S_IWUSR, ad5504_read_powerdown_mode,
198 ad5504_write_powerdown_mode, 0);
200 static IIO_CONST_ATTR(out_voltage_powerdown_mode_available,
201 "20kohm_to_gnd three_state");
203 #define IIO_DEV_ATTR_DAC_POWERDOWN(_num, _show, _store, _addr) \
204 IIO_DEVICE_ATTR(out_voltage##_num##_powerdown, \
205 S_IRUGO | S_IWUSR, _show, _store, _addr)
206 static IIO_DEV_ATTR_DAC_POWERDOWN(0, ad5504_read_dac_powerdown,
207 ad5504_write_dac_powerdown, 0);
208 static IIO_DEV_ATTR_DAC_POWERDOWN(1, ad5504_read_dac_powerdown,
209 ad5504_write_dac_powerdown, 1);
210 static IIO_DEV_ATTR_DAC_POWERDOWN(2, ad5504_read_dac_powerdown,
211 ad5504_write_dac_powerdown, 2);
212 static IIO_DEV_ATTR_DAC_POWERDOWN(3, ad5504_read_dac_powerdown,
213 ad5504_write_dac_powerdown, 3);
215 static struct attribute *ad5504_attributes[] = {
216 &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr,
217 &iio_dev_attr_out_voltage1_powerdown.dev_attr.attr,
218 &iio_dev_attr_out_voltage2_powerdown.dev_attr.attr,
219 &iio_dev_attr_out_voltage3_powerdown.dev_attr.attr,
220 &iio_dev_attr_out_voltage_powerdown_mode.dev_attr.attr,
221 &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr,
222 NULL,
225 static const struct attribute_group ad5504_attribute_group = {
226 .attrs = ad5504_attributes,
229 static struct attribute *ad5501_attributes[] = {
230 &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr,
231 &iio_dev_attr_out_voltage_powerdown_mode.dev_attr.attr,
232 &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr,
233 NULL,
236 static const struct attribute_group ad5501_attribute_group = {
237 .attrs = ad5501_attributes,
240 static IIO_CONST_ATTR(temp0_thresh_rising_value, "110000");
241 static IIO_CONST_ATTR(temp0_thresh_rising_en, "1");
243 static struct attribute *ad5504_ev_attributes[] = {
244 &iio_const_attr_temp0_thresh_rising_value.dev_attr.attr,
245 &iio_const_attr_temp0_thresh_rising_en.dev_attr.attr,
246 NULL,
249 static struct attribute_group ad5504_ev_attribute_group = {
250 .attrs = ad5504_ev_attributes,
251 .name = "events",
254 static irqreturn_t ad5504_event_handler(int irq, void *private)
256 iio_push_event(private,
257 IIO_UNMOD_EVENT_CODE(IIO_TEMP,
259 IIO_EV_TYPE_THRESH,
260 IIO_EV_DIR_RISING),
261 iio_get_time_ns());
263 return IRQ_HANDLED;
266 static const struct iio_info ad5504_info = {
267 .write_raw = ad5504_write_raw,
268 .read_raw = ad5504_read_raw,
269 .attrs = &ad5504_attribute_group,
270 .event_attrs = &ad5504_ev_attribute_group,
271 .driver_module = THIS_MODULE,
274 static const struct iio_info ad5501_info = {
275 .write_raw = ad5504_write_raw,
276 .read_raw = ad5504_read_raw,
277 .attrs = &ad5501_attribute_group,
278 .event_attrs = &ad5504_ev_attribute_group,
279 .driver_module = THIS_MODULE,
282 static int __devinit ad5504_probe(struct spi_device *spi)
284 struct ad5504_platform_data *pdata = spi->dev.platform_data;
285 struct iio_dev *indio_dev;
286 struct ad5504_state *st;
287 struct regulator *reg;
288 int ret, voltage_uv = 0;
290 indio_dev = iio_allocate_device(sizeof(*st));
291 if (indio_dev == NULL) {
292 ret = -ENOMEM;
293 goto error_ret;
295 reg = regulator_get(&spi->dev, "vcc");
296 if (!IS_ERR(reg)) {
297 ret = regulator_enable(reg);
298 if (ret)
299 goto error_put_reg;
301 voltage_uv = regulator_get_voltage(reg);
304 spi_set_drvdata(spi, indio_dev);
305 st = iio_priv(indio_dev);
306 if (voltage_uv)
307 st->vref_mv = voltage_uv / 1000;
308 else if (pdata)
309 st->vref_mv = pdata->vref_mv;
310 else
311 dev_warn(&spi->dev, "reference voltage unspecified\n");
313 st->reg = reg;
314 st->spi = spi;
315 indio_dev->dev.parent = &spi->dev;
316 indio_dev->name = spi_get_device_id(st->spi)->name;
317 if (spi_get_device_id(st->spi)->driver_data == ID_AD5501) {
318 indio_dev->info = &ad5501_info;
319 indio_dev->num_channels = 1;
320 } else {
321 indio_dev->info = &ad5504_info;
322 indio_dev->num_channels = 4;
324 indio_dev->channels = ad5504_channels;
325 indio_dev->modes = INDIO_DIRECT_MODE;
327 if (spi->irq) {
328 ret = request_threaded_irq(spi->irq,
329 NULL,
330 &ad5504_event_handler,
331 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
332 spi_get_device_id(st->spi)->name,
333 indio_dev);
334 if (ret)
335 goto error_disable_reg;
338 ret = iio_device_register(indio_dev);
339 if (ret)
340 goto error_free_irq;
342 return 0;
344 error_free_irq:
345 free_irq(spi->irq, indio_dev);
346 error_disable_reg:
347 if (!IS_ERR(reg))
348 regulator_disable(reg);
349 error_put_reg:
350 if (!IS_ERR(reg))
351 regulator_put(reg);
353 iio_free_device(indio_dev);
354 error_ret:
355 return ret;
358 static int __devexit ad5504_remove(struct spi_device *spi)
360 struct iio_dev *indio_dev = spi_get_drvdata(spi);
361 struct ad5504_state *st = iio_priv(indio_dev);
363 iio_device_unregister(indio_dev);
364 if (spi->irq)
365 free_irq(spi->irq, indio_dev);
367 if (!IS_ERR(st->reg)) {
368 regulator_disable(st->reg);
369 regulator_put(st->reg);
371 iio_free_device(indio_dev);
373 return 0;
376 static const struct spi_device_id ad5504_id[] = {
377 {"ad5504", ID_AD5504},
378 {"ad5501", ID_AD5501},
381 MODULE_DEVICE_TABLE(spi, ad5504_id);
383 static struct spi_driver ad5504_driver = {
384 .driver = {
385 .name = "ad5504",
386 .owner = THIS_MODULE,
388 .probe = ad5504_probe,
389 .remove = __devexit_p(ad5504_remove),
390 .id_table = ad5504_id,
392 module_spi_driver(ad5504_driver);
394 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
395 MODULE_DESCRIPTION("Analog Devices AD5501/AD5501 DAC");
396 MODULE_LICENSE("GPL v2");