2 * AD5504, AD5501 High Voltage Digital to Analog Converter
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/interrupt.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>
21 #include "../events.h"
25 #define AD5504_CHANNEL(_chan) { \
26 .type = IIO_VOLTAGE, \
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
[] = {
42 static int ad5504_spi_write(struct spi_device
*spi
, u8 addr
, u16 val
)
44 u16 tmp
= cpu_to_be16(AD5504_CMD_WRITE
|
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
));
56 struct spi_transfer t
= {
64 spi_message_add_tail(&t
, &m
);
65 ret
= spi_sync(spi
, &m
);
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
,
79 struct ad5504_state
*st
= iio_priv(indio_dev
);
80 unsigned long scale_uv
;
85 ret
= ad5504_spi_read(st
->spi
, chan
->address
);
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
;
102 static int ad5504_write_raw(struct iio_dev
*indio_dev
,
103 struct iio_chan_spec
const *chan
,
108 struct ad5504_state
*st
= iio_priv(indio_dev
);
113 if (val
>= (1 << chan
->scan_type
.realbits
) || val
< 0)
116 return ad5504_spi_write(st
->spi
, chan
->address
, val
);
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
);
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
;
150 return ret
? ret
: len
;
153 static ssize_t
ad5504_read_dac_powerdown(struct device
*dev
,
154 struct device_attribute
*attr
,
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
)
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
);
180 st
->pwr_down_mask
|= (1 << this_attr
->address
);
181 else if (readin
== 1)
182 st
->pwr_down_mask
&= ~(1 << this_attr
->address
);
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
,
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
,
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
,
249 static struct attribute_group ad5504_ev_attribute_group
= {
250 .attrs
= ad5504_ev_attributes
,
254 static irqreturn_t
ad5504_event_handler(int irq
, void *private)
256 iio_push_event(private,
257 IIO_UNMOD_EVENT_CODE(IIO_TEMP
,
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
) {
295 reg
= regulator_get(&spi
->dev
, "vcc");
297 ret
= regulator_enable(reg
);
301 voltage_uv
= regulator_get_voltage(reg
);
304 spi_set_drvdata(spi
, indio_dev
);
305 st
= iio_priv(indio_dev
);
307 st
->vref_mv
= voltage_uv
/ 1000;
309 st
->vref_mv
= pdata
->vref_mv
;
311 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
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;
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
;
328 ret
= request_threaded_irq(spi
->irq
,
330 &ad5504_event_handler
,
331 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
332 spi_get_device_id(st
->spi
)->name
,
335 goto error_disable_reg
;
338 ret
= iio_device_register(indio_dev
);
345 free_irq(spi
->irq
, indio_dev
);
348 regulator_disable(reg
);
353 iio_free_device(indio_dev
);
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
);
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
);
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
= {
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");