2 * AD5760, AD5780, AD5781, AD5791 Voltage Output Digital to Analog Converter
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/interrupt.h>
10 #include <linux/gpio.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>
24 static int ad5791_spi_write(struct spi_device
*spi
, u8 addr
, u32 val
)
31 data
.d32
= cpu_to_be32(AD5791_CMD_WRITE
|
33 (val
& AD5791_DAC_MASK
));
35 return spi_write(spi
, &data
.d8
[1], 3);
38 static int ad5791_spi_read(struct spi_device
*spi
, u8 addr
, u32
*val
)
45 struct spi_message msg
;
46 struct spi_transfer xfers
[] = {
48 .tx_buf
= &data
[0].d8
[1],
53 .tx_buf
= &data
[1].d8
[1],
54 .rx_buf
= &data
[2].d8
[1],
60 data
[0].d32
= cpu_to_be32(AD5791_CMD_READ
|
62 data
[1].d32
= cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP
));
64 spi_message_init(&msg
);
65 spi_message_add_tail(&xfers
[0], &msg
);
66 spi_message_add_tail(&xfers
[1], &msg
);
67 ret
= spi_sync(spi
, &msg
);
69 *val
= be32_to_cpu(data
[2].d32
);
74 static ssize_t
ad5791_write_dac(struct device
*dev
,
75 struct device_attribute
*attr
,
76 const char *buf
, size_t len
)
78 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
79 struct ad5791_state
*st
= iio_dev_get_devdata(indio_dev
);
80 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
84 ret
= strict_strtol(buf
, 10, &readin
);
88 readin
+= (1 << (st
->chip_info
->bits
- 1));
89 readin
&= AD5791_RES_MASK(st
->chip_info
->bits
);
90 readin
<<= st
->chip_info
->left_shift
;
92 ret
= ad5791_spi_write(st
->spi
, this_attr
->address
, readin
);
93 return ret
? ret
: len
;
96 static ssize_t
ad5791_read_dac(struct device
*dev
,
97 struct device_attribute
*attr
,
100 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
101 struct ad5791_state
*st
= iio_dev_get_devdata(indio_dev
);
102 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
106 ret
= ad5791_spi_read(st
->spi
, this_attr
->address
, &val
);
110 val
&= AD5791_DAC_MASK
;
111 val
>>= st
->chip_info
->left_shift
;
112 val
-= (1 << (st
->chip_info
->bits
- 1));
114 return sprintf(buf
, "%d\n", val
);
117 static ssize_t
ad5791_read_powerdown_mode(struct device
*dev
,
118 struct device_attribute
*attr
, char *buf
)
120 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
121 struct ad5791_state
*st
= iio_dev_get_devdata(indio_dev
);
123 const char mode
[][14] = {"6kohm_to_gnd", "three_state"};
125 return sprintf(buf
, "%s\n", mode
[st
->pwr_down_mode
]);
128 static ssize_t
ad5791_write_powerdown_mode(struct device
*dev
,
129 struct device_attribute
*attr
,
130 const char *buf
, size_t len
)
132 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
133 struct ad5791_state
*st
= iio_dev_get_devdata(indio_dev
);
136 if (sysfs_streq(buf
, "6kohm_to_gnd"))
137 st
->pwr_down_mode
= AD5791_DAC_PWRDN_6K
;
138 else if (sysfs_streq(buf
, "three_state"))
139 st
->pwr_down_mode
= AD5791_DAC_PWRDN_3STATE
;
143 return ret
? ret
: len
;
146 static ssize_t
ad5791_read_dac_powerdown(struct device
*dev
,
147 struct device_attribute
*attr
,
150 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
151 struct ad5791_state
*st
= iio_dev_get_devdata(indio_dev
);
153 return sprintf(buf
, "%d\n", st
->pwr_down
);
156 static ssize_t
ad5791_write_dac_powerdown(struct device
*dev
,
157 struct device_attribute
*attr
,
158 const char *buf
, size_t len
)
162 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
163 struct ad5791_state
*st
= iio_dev_get_devdata(indio_dev
);
165 ret
= strict_strtol(buf
, 10, &readin
);
170 st
->pwr_down
= false;
171 st
->ctrl
&= ~(AD5791_CTRL_OPGND
| AD5791_CTRL_DACTRI
);
172 } else if (readin
== 1) {
174 if (st
->pwr_down_mode
== AD5791_DAC_PWRDN_6K
)
175 st
->ctrl
|= AD5791_CTRL_OPGND
;
176 else if (st
->pwr_down_mode
== AD5791_DAC_PWRDN_3STATE
)
177 st
->ctrl
|= AD5791_CTRL_DACTRI
;
181 ret
= ad5791_spi_write(st
->spi
, AD5791_ADDR_CTRL
, st
->ctrl
);
183 return ret
? ret
: len
;
186 static ssize_t
ad5791_show_scale(struct device
*dev
,
187 struct device_attribute
*attr
,
190 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
191 struct ad5791_state
*st
= iio_dev_get_devdata(indio_dev
);
192 /* Corresponds to Vref / 2^(bits) */
193 unsigned int scale_uv
= (st
->vref_mv
* 1000) >> st
->chip_info
->bits
;
195 return sprintf(buf
, "%d.%03d\n", scale_uv
/ 1000, scale_uv
% 1000);
197 static IIO_DEVICE_ATTR(out_scale
, S_IRUGO
, ad5791_show_scale
, NULL
, 0);
199 static ssize_t
ad5791_show_name(struct device
*dev
,
200 struct device_attribute
*attr
,
203 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
204 struct ad5791_state
*st
= iio_dev_get_devdata(indio_dev
);
206 return sprintf(buf
, "%s\n", spi_get_device_id(st
->spi
)->name
);
208 static IIO_DEVICE_ATTR(name
, S_IRUGO
, ad5791_show_name
, NULL
, 0);
210 #define IIO_DEV_ATTR_OUT_RW_RAW(_num, _show, _store, _addr) \
211 IIO_DEVICE_ATTR(out##_num##_raw, \
212 S_IRUGO | S_IWUSR, _show, _store, _addr)
214 static IIO_DEV_ATTR_OUT_RW_RAW(0, ad5791_read_dac
,
215 ad5791_write_dac
, AD5791_ADDR_DAC0
);
217 static IIO_DEVICE_ATTR(out_powerdown_mode
, S_IRUGO
|
218 S_IWUSR
, ad5791_read_powerdown_mode
,
219 ad5791_write_powerdown_mode
, 0);
221 static IIO_CONST_ATTR(out_powerdown_mode_available
,
222 "6kohm_to_gnd three_state");
224 #define IIO_DEV_ATTR_DAC_POWERDOWN(_num, _show, _store, _addr) \
225 IIO_DEVICE_ATTR(out##_num##_powerdown, \
226 S_IRUGO | S_IWUSR, _show, _store, _addr)
228 static IIO_DEV_ATTR_DAC_POWERDOWN(0, ad5791_read_dac_powerdown
,
229 ad5791_write_dac_powerdown
, 0);
231 static struct attribute
*ad5791_attributes
[] = {
232 &iio_dev_attr_out0_raw
.dev_attr
.attr
,
233 &iio_dev_attr_out0_powerdown
.dev_attr
.attr
,
234 &iio_dev_attr_out_powerdown_mode
.dev_attr
.attr
,
235 &iio_const_attr_out_powerdown_mode_available
.dev_attr
.attr
,
236 &iio_dev_attr_out_scale
.dev_attr
.attr
,
237 &iio_dev_attr_name
.dev_attr
.attr
,
241 static const struct attribute_group ad5791_attribute_group
= {
242 .attrs
= ad5791_attributes
,
245 static int ad5791_get_lin_comp(unsigned int span
)
248 return AD5791_LINCOMP_0_10
;
249 else if (span
<= 12000)
250 return AD5791_LINCOMP_10_12
;
251 else if (span
<= 16000)
252 return AD5791_LINCOMP_12_16
;
253 else if (span
<= 19000)
254 return AD5791_LINCOMP_16_19
;
256 return AD5791_LINCOMP_19_20
;
259 static int ad5780_get_lin_comp(unsigned int span
)
262 return AD5780_LINCOMP_0_10
;
264 return AD5780_LINCOMP_10_20
;
267 static const struct ad5791_chip_info ad5791_chip_info_tbl
[] = {
271 .get_lin_comp
= ad5780_get_lin_comp
,
276 .get_lin_comp
= ad5780_get_lin_comp
,
281 .get_lin_comp
= ad5791_get_lin_comp
,
286 .get_lin_comp
= ad5791_get_lin_comp
,
290 static const struct iio_info ad5791_info
= {
291 .attrs
= &ad5791_attribute_group
,
292 .driver_module
= THIS_MODULE
,
295 static int __devinit
ad5791_probe(struct spi_device
*spi
)
297 struct ad5791_platform_data
*pdata
= spi
->dev
.platform_data
;
298 struct ad5791_state
*st
;
299 int ret
, pos_voltage_uv
= 0, neg_voltage_uv
= 0;
301 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
307 spi_set_drvdata(spi
, st
);
309 st
->reg_vdd
= regulator_get(&spi
->dev
, "vdd");
310 if (!IS_ERR(st
->reg_vdd
)) {
311 ret
= regulator_enable(st
->reg_vdd
);
313 goto error_put_reg_pos
;
315 pos_voltage_uv
= regulator_get_voltage(st
->reg_vdd
);
318 st
->reg_vss
= regulator_get(&spi
->dev
, "vss");
319 if (!IS_ERR(st
->reg_vss
)) {
320 ret
= regulator_enable(st
->reg_vss
);
322 goto error_put_reg_neg
;
324 neg_voltage_uv
= regulator_get_voltage(st
->reg_vss
);
327 if (!IS_ERR(st
->reg_vss
) && !IS_ERR(st
->reg_vdd
))
328 st
->vref_mv
= (pos_voltage_uv
- neg_voltage_uv
) / 1000;
330 st
->vref_mv
= pdata
->vref_pos_mv
- pdata
->vref_neg_mv
;
332 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
334 ret
= ad5791_spi_write(spi
, AD5791_ADDR_SW_CTRL
, AD5791_SWCTRL_RESET
);
336 goto error_disable_reg_neg
;
339 &ad5791_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
342 st
->ctrl
= AD5761_CTRL_LINCOMP(st
->chip_info
->get_lin_comp(st
->vref_mv
))
343 | ((pdata
&& pdata
->use_rbuf_gain2
) ? 0 : AD5791_CTRL_RBUF
) |
346 ret
= ad5791_spi_write(spi
, AD5791_ADDR_CTRL
, st
->ctrl
|
347 AD5791_CTRL_OPGND
| AD5791_CTRL_DACTRI
);
349 goto error_disable_reg_neg
;
354 st
->indio_dev
= iio_allocate_device(0);
355 if (st
->indio_dev
== NULL
) {
357 goto error_disable_reg_neg
;
359 st
->indio_dev
->dev
.parent
= &spi
->dev
;
360 st
->indio_dev
->dev_data
= (void *)(st
);
361 st
->indio_dev
->info
= &ad5791_info
;
362 st
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
364 ret
= iio_device_register(st
->indio_dev
);
371 iio_free_device(st
->indio_dev
);
373 error_disable_reg_neg
:
374 if (!IS_ERR(st
->reg_vss
))
375 regulator_disable(st
->reg_vss
);
377 if (!IS_ERR(st
->reg_vss
))
378 regulator_put(st
->reg_vss
);
380 if (!IS_ERR(st
->reg_vdd
))
381 regulator_disable(st
->reg_vdd
);
383 if (!IS_ERR(st
->reg_vdd
))
384 regulator_put(st
->reg_vdd
);
391 static int __devexit
ad5791_remove(struct spi_device
*spi
)
393 struct ad5791_state
*st
= spi_get_drvdata(spi
);
395 iio_device_unregister(st
->indio_dev
);
397 if (!IS_ERR(st
->reg_vdd
)) {
398 regulator_disable(st
->reg_vdd
);
399 regulator_put(st
->reg_vdd
);
402 if (!IS_ERR(st
->reg_vss
)) {
403 regulator_disable(st
->reg_vss
);
404 regulator_put(st
->reg_vss
);
412 static const struct spi_device_id ad5791_id
[] = {
413 {"ad5760", ID_AD5760
},
414 {"ad5780", ID_AD5780
},
415 {"ad5781", ID_AD5781
},
416 {"ad5791", ID_AD5791
},
420 static struct spi_driver ad5791_driver
= {
423 .owner
= THIS_MODULE
,
425 .probe
= ad5791_probe
,
426 .remove
= __devexit_p(ad5791_remove
),
427 .id_table
= ad5791_id
,
430 static __init
int ad5791_spi_init(void)
432 return spi_register_driver(&ad5791_driver
);
434 module_init(ad5791_spi_init
);
436 static __exit
void ad5791_spi_exit(void)
438 spi_unregister_driver(&ad5791_driver
);
440 module_exit(ad5791_spi_exit
);
442 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
443 MODULE_DESCRIPTION("Analog Devices AD5760/AD5780/AD5781/AD5791 DAC");
444 MODULE_LICENSE("GPL v2");