2 * AD5760, AD5780, AD5781, AD5790, AD5791 Voltage Output Digital to Analog
5 * Copyright 2011 Analog Devices Inc.
7 * Licensed under the GPL-2.
10 #include <linux/interrupt.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 <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/dac/ad5791.h>
24 #define AD5791_RES_MASK(x) ((1 << (x)) - 1)
25 #define AD5791_DAC_MASK AD5791_RES_MASK(20)
26 #define AD5791_DAC_MSB (1 << 19)
28 #define AD5791_CMD_READ (1 << 23)
29 #define AD5791_CMD_WRITE (0 << 23)
30 #define AD5791_ADDR(addr) ((addr) << 20)
33 #define AD5791_ADDR_NOOP 0
34 #define AD5791_ADDR_DAC0 1
35 #define AD5791_ADDR_CTRL 2
36 #define AD5791_ADDR_CLRCODE 3
37 #define AD5791_ADDR_SW_CTRL 4
39 /* Control Register */
40 #define AD5791_CTRL_RBUF (1 << 1)
41 #define AD5791_CTRL_OPGND (1 << 2)
42 #define AD5791_CTRL_DACTRI (1 << 3)
43 #define AD5791_CTRL_BIN2SC (1 << 4)
44 #define AD5791_CTRL_SDODIS (1 << 5)
45 #define AD5761_CTRL_LINCOMP(x) ((x) << 6)
47 #define AD5791_LINCOMP_0_10 0
48 #define AD5791_LINCOMP_10_12 1
49 #define AD5791_LINCOMP_12_16 2
50 #define AD5791_LINCOMP_16_19 3
51 #define AD5791_LINCOMP_19_20 12
53 #define AD5780_LINCOMP_0_10 0
54 #define AD5780_LINCOMP_10_20 12
56 /* Software Control Register */
57 #define AD5791_SWCTRL_LDAC (1 << 0)
58 #define AD5791_SWCTRL_CLR (1 << 1)
59 #define AD5791_SWCTRL_RESET (1 << 2)
61 #define AD5791_DAC_PWRDN_6K 0
62 #define AD5791_DAC_PWRDN_3STATE 1
65 * struct ad5791_chip_info - chip specific information
66 * @get_lin_comp: function pointer to the device specific function
69 struct ad5791_chip_info
{
70 int (*get_lin_comp
) (unsigned int span
);
74 * struct ad5791_state - driver instance specific data
76 * @reg_vdd: positive supply regulator
77 * @reg_vss: negative supply regulator
78 * @chip_info: chip model specific constants
79 * @vref_mv: actual reference voltage used
80 * @vref_neg_mv: voltage of the negative supply
81 * @pwr_down_mode current power down mode
85 struct spi_device
*spi
;
86 struct regulator
*reg_vdd
;
87 struct regulator
*reg_vss
;
88 const struct ad5791_chip_info
*chip_info
;
89 unsigned short vref_mv
;
90 unsigned int vref_neg_mv
;
92 unsigned pwr_down_mode
;
98 } data
[3] ____cacheline_aligned
;
102 * ad5791_supported_device_ids:
105 enum ad5791_supported_device_ids
{
112 static int ad5791_spi_write(struct ad5791_state
*st
, u8 addr
, u32 val
)
114 st
->data
[0].d32
= cpu_to_be32(AD5791_CMD_WRITE
|
116 (val
& AD5791_DAC_MASK
));
118 return spi_write(st
->spi
, &st
->data
[0].d8
[1], 3);
121 static int ad5791_spi_read(struct ad5791_state
*st
, u8 addr
, u32
*val
)
124 struct spi_transfer xfers
[] = {
126 .tx_buf
= &st
->data
[0].d8
[1],
131 .tx_buf
= &st
->data
[1].d8
[1],
132 .rx_buf
= &st
->data
[2].d8
[1],
138 st
->data
[0].d32
= cpu_to_be32(AD5791_CMD_READ
|
140 st
->data
[1].d32
= cpu_to_be32(AD5791_ADDR(AD5791_ADDR_NOOP
));
142 ret
= spi_sync_transfer(st
->spi
, xfers
, ARRAY_SIZE(xfers
));
144 *val
= be32_to_cpu(st
->data
[2].d32
);
149 static const char * const ad5791_powerdown_modes
[] = {
154 static int ad5791_get_powerdown_mode(struct iio_dev
*indio_dev
,
155 const struct iio_chan_spec
*chan
)
157 struct ad5791_state
*st
= iio_priv(indio_dev
);
159 return st
->pwr_down_mode
;
162 static int ad5791_set_powerdown_mode(struct iio_dev
*indio_dev
,
163 const struct iio_chan_spec
*chan
, unsigned int mode
)
165 struct ad5791_state
*st
= iio_priv(indio_dev
);
167 st
->pwr_down_mode
= mode
;
172 static const struct iio_enum ad5791_powerdown_mode_enum
= {
173 .items
= ad5791_powerdown_modes
,
174 .num_items
= ARRAY_SIZE(ad5791_powerdown_modes
),
175 .get
= ad5791_get_powerdown_mode
,
176 .set
= ad5791_set_powerdown_mode
,
179 static ssize_t
ad5791_read_dac_powerdown(struct iio_dev
*indio_dev
,
180 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
182 struct ad5791_state
*st
= iio_priv(indio_dev
);
184 return sprintf(buf
, "%d\n", st
->pwr_down
);
187 static ssize_t
ad5791_write_dac_powerdown(struct iio_dev
*indio_dev
,
188 uintptr_t private, const struct iio_chan_spec
*chan
, const char *buf
,
193 struct ad5791_state
*st
= iio_priv(indio_dev
);
195 ret
= strtobool(buf
, &pwr_down
);
200 st
->ctrl
&= ~(AD5791_CTRL_OPGND
| AD5791_CTRL_DACTRI
);
202 if (st
->pwr_down_mode
== AD5791_DAC_PWRDN_6K
)
203 st
->ctrl
|= AD5791_CTRL_OPGND
;
204 else if (st
->pwr_down_mode
== AD5791_DAC_PWRDN_3STATE
)
205 st
->ctrl
|= AD5791_CTRL_DACTRI
;
207 st
->pwr_down
= pwr_down
;
209 ret
= ad5791_spi_write(st
, AD5791_ADDR_CTRL
, st
->ctrl
);
211 return ret
? ret
: len
;
214 static int ad5791_get_lin_comp(unsigned int span
)
217 return AD5791_LINCOMP_0_10
;
218 else if (span
<= 12000)
219 return AD5791_LINCOMP_10_12
;
220 else if (span
<= 16000)
221 return AD5791_LINCOMP_12_16
;
222 else if (span
<= 19000)
223 return AD5791_LINCOMP_16_19
;
225 return AD5791_LINCOMP_19_20
;
228 static int ad5780_get_lin_comp(unsigned int span
)
231 return AD5780_LINCOMP_0_10
;
233 return AD5780_LINCOMP_10_20
;
235 static const struct ad5791_chip_info ad5791_chip_info_tbl
[] = {
237 .get_lin_comp
= ad5780_get_lin_comp
,
240 .get_lin_comp
= ad5780_get_lin_comp
,
243 .get_lin_comp
= ad5791_get_lin_comp
,
246 .get_lin_comp
= ad5791_get_lin_comp
,
250 static int ad5791_read_raw(struct iio_dev
*indio_dev
,
251 struct iio_chan_spec
const *chan
,
256 struct ad5791_state
*st
= iio_priv(indio_dev
);
261 case IIO_CHAN_INFO_RAW
:
262 ret
= ad5791_spi_read(st
, chan
->address
, val
);
265 *val
&= AD5791_DAC_MASK
;
266 *val
>>= chan
->scan_type
.shift
;
268 case IIO_CHAN_INFO_SCALE
:
270 *val2
= (1 << chan
->scan_type
.realbits
) - 1;
271 return IIO_VAL_FRACTIONAL
;
272 case IIO_CHAN_INFO_OFFSET
:
273 val64
= (((u64
)st
->vref_neg_mv
) << chan
->scan_type
.realbits
);
274 do_div(val64
, st
->vref_mv
);
283 static const struct iio_chan_spec_ext_info ad5791_ext_info
[] = {
286 .shared
= IIO_SHARED_BY_TYPE
,
287 .read
= ad5791_read_dac_powerdown
,
288 .write
= ad5791_write_dac_powerdown
,
290 IIO_ENUM("powerdown_mode", IIO_SHARED_BY_TYPE
,
291 &ad5791_powerdown_mode_enum
),
292 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5791_powerdown_mode_enum
),
296 #define AD5791_CHAN(bits, _shift) { \
297 .type = IIO_VOLTAGE, \
300 .address = AD5791_ADDR_DAC0, \
302 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
303 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
304 BIT(IIO_CHAN_INFO_OFFSET), \
307 .realbits = (bits), \
311 .ext_info = ad5791_ext_info, \
314 static const struct iio_chan_spec ad5791_channels
[] = {
315 [ID_AD5760
] = AD5791_CHAN(16, 4),
316 [ID_AD5780
] = AD5791_CHAN(18, 2),
317 [ID_AD5781
] = AD5791_CHAN(18, 2),
318 [ID_AD5791
] = AD5791_CHAN(20, 0)
321 static int ad5791_write_raw(struct iio_dev
*indio_dev
,
322 struct iio_chan_spec
const *chan
,
327 struct ad5791_state
*st
= iio_priv(indio_dev
);
330 case IIO_CHAN_INFO_RAW
:
331 val
&= AD5791_RES_MASK(chan
->scan_type
.realbits
);
332 val
<<= chan
->scan_type
.shift
;
334 return ad5791_spi_write(st
, chan
->address
, val
);
341 static const struct iio_info ad5791_info
= {
342 .read_raw
= &ad5791_read_raw
,
343 .write_raw
= &ad5791_write_raw
,
344 .driver_module
= THIS_MODULE
,
347 static int ad5791_probe(struct spi_device
*spi
)
349 struct ad5791_platform_data
*pdata
= spi
->dev
.platform_data
;
350 struct iio_dev
*indio_dev
;
351 struct ad5791_state
*st
;
352 int ret
, pos_voltage_uv
= 0, neg_voltage_uv
= 0;
354 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
357 st
= iio_priv(indio_dev
);
358 st
->reg_vdd
= devm_regulator_get(&spi
->dev
, "vdd");
359 if (!IS_ERR(st
->reg_vdd
)) {
360 ret
= regulator_enable(st
->reg_vdd
);
364 ret
= regulator_get_voltage(st
->reg_vdd
);
366 goto error_disable_reg_pos
;
368 pos_voltage_uv
= ret
;
371 st
->reg_vss
= devm_regulator_get(&spi
->dev
, "vss");
372 if (!IS_ERR(st
->reg_vss
)) {
373 ret
= regulator_enable(st
->reg_vss
);
375 goto error_disable_reg_pos
;
377 ret
= regulator_get_voltage(st
->reg_vss
);
379 goto error_disable_reg_neg
;
381 neg_voltage_uv
= ret
;
387 if (!IS_ERR(st
->reg_vss
) && !IS_ERR(st
->reg_vdd
)) {
388 st
->vref_mv
= (pos_voltage_uv
+ neg_voltage_uv
) / 1000;
389 st
->vref_neg_mv
= neg_voltage_uv
/ 1000;
391 st
->vref_mv
= pdata
->vref_pos_mv
+ pdata
->vref_neg_mv
;
392 st
->vref_neg_mv
= pdata
->vref_neg_mv
;
394 dev_warn(&spi
->dev
, "reference voltage unspecified\n");
397 ret
= ad5791_spi_write(st
, AD5791_ADDR_SW_CTRL
, AD5791_SWCTRL_RESET
);
399 goto error_disable_reg_neg
;
401 st
->chip_info
= &ad5791_chip_info_tbl
[spi_get_device_id(spi
)
405 st
->ctrl
= AD5761_CTRL_LINCOMP(st
->chip_info
->get_lin_comp(st
->vref_mv
))
406 | ((pdata
&& pdata
->use_rbuf_gain2
) ? 0 : AD5791_CTRL_RBUF
) |
409 ret
= ad5791_spi_write(st
, AD5791_ADDR_CTRL
, st
->ctrl
|
410 AD5791_CTRL_OPGND
| AD5791_CTRL_DACTRI
);
412 goto error_disable_reg_neg
;
414 spi_set_drvdata(spi
, indio_dev
);
415 indio_dev
->dev
.parent
= &spi
->dev
;
416 indio_dev
->info
= &ad5791_info
;
417 indio_dev
->modes
= INDIO_DIRECT_MODE
;
419 = &ad5791_channels
[spi_get_device_id(spi
)->driver_data
];
420 indio_dev
->num_channels
= 1;
421 indio_dev
->name
= spi_get_device_id(st
->spi
)->name
;
422 ret
= iio_device_register(indio_dev
);
424 goto error_disable_reg_neg
;
428 error_disable_reg_neg
:
429 if (!IS_ERR(st
->reg_vss
))
430 regulator_disable(st
->reg_vss
);
431 error_disable_reg_pos
:
432 if (!IS_ERR(st
->reg_vdd
))
433 regulator_disable(st
->reg_vdd
);
437 static int ad5791_remove(struct spi_device
*spi
)
439 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
440 struct ad5791_state
*st
= iio_priv(indio_dev
);
442 iio_device_unregister(indio_dev
);
443 if (!IS_ERR(st
->reg_vdd
))
444 regulator_disable(st
->reg_vdd
);
446 if (!IS_ERR(st
->reg_vss
))
447 regulator_disable(st
->reg_vss
);
452 static const struct spi_device_id ad5791_id
[] = {
453 {"ad5760", ID_AD5760
},
454 {"ad5780", ID_AD5780
},
455 {"ad5781", ID_AD5781
},
456 {"ad5790", ID_AD5791
},
457 {"ad5791", ID_AD5791
},
460 MODULE_DEVICE_TABLE(spi
, ad5791_id
);
462 static struct spi_driver ad5791_driver
= {
465 .owner
= THIS_MODULE
,
467 .probe
= ad5791_probe
,
468 .remove
= ad5791_remove
,
469 .id_table
= ad5791_id
,
471 module_spi_driver(ad5791_driver
);
473 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
474 MODULE_DESCRIPTION("Analog Devices AD5760/AD5780/AD5781/AD5790/AD5791 DAC");
475 MODULE_LICENSE("GPL v2");