1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AD5446 SPI DAC driver
5 * Copyright 2010 Analog Devices Inc.
8 #include <linux/interrupt.h>
9 #include <linux/workqueue.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/list.h>
15 #include <linux/spi/spi.h>
16 #include <linux/i2c.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/err.h>
19 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
24 #define MODE_PWRDWN_1k 0x1
25 #define MODE_PWRDWN_100k 0x2
26 #define MODE_PWRDWN_TRISTATE 0x3
29 * struct ad5446_state - driver instance specific data
31 * @chip_info: chip model specific constants, available modes etc
32 * @reg: supply regulator
33 * @vref_mv: actual reference voltage used
38 const struct ad5446_chip_info
*chip_info
;
39 struct regulator
*reg
;
40 unsigned short vref_mv
;
42 unsigned pwr_down_mode
;
47 * struct ad5446_chip_info - chip specific information
48 * @channel: channel spec for the DAC
49 * @int_vref_mv: AD5620/40/60: the internal reference voltage
50 * @write: chip specific helper function to write to the register
53 struct ad5446_chip_info
{
54 struct iio_chan_spec channel
;
56 int (*write
)(struct ad5446_state
*st
, unsigned val
);
59 static const char * const ad5446_powerdown_modes
[] = {
60 "1kohm_to_gnd", "100kohm_to_gnd", "three_state"
63 static int ad5446_set_powerdown_mode(struct iio_dev
*indio_dev
,
64 const struct iio_chan_spec
*chan
, unsigned int mode
)
66 struct ad5446_state
*st
= iio_priv(indio_dev
);
68 st
->pwr_down_mode
= mode
+ 1;
73 static int ad5446_get_powerdown_mode(struct iio_dev
*indio_dev
,
74 const struct iio_chan_spec
*chan
)
76 struct ad5446_state
*st
= iio_priv(indio_dev
);
78 return st
->pwr_down_mode
- 1;
81 static const struct iio_enum ad5446_powerdown_mode_enum
= {
82 .items
= ad5446_powerdown_modes
,
83 .num_items
= ARRAY_SIZE(ad5446_powerdown_modes
),
84 .get
= ad5446_get_powerdown_mode
,
85 .set
= ad5446_set_powerdown_mode
,
88 static ssize_t
ad5446_read_dac_powerdown(struct iio_dev
*indio_dev
,
90 const struct iio_chan_spec
*chan
,
93 struct ad5446_state
*st
= iio_priv(indio_dev
);
95 return sprintf(buf
, "%d\n", st
->pwr_down
);
98 static ssize_t
ad5446_write_dac_powerdown(struct iio_dev
*indio_dev
,
100 const struct iio_chan_spec
*chan
,
101 const char *buf
, size_t len
)
103 struct ad5446_state
*st
= iio_priv(indio_dev
);
109 ret
= strtobool(buf
, &powerdown
);
113 mutex_lock(&indio_dev
->mlock
);
114 st
->pwr_down
= powerdown
;
117 shift
= chan
->scan_type
.realbits
+ chan
->scan_type
.shift
;
118 val
= st
->pwr_down_mode
<< shift
;
120 val
= st
->cached_val
;
123 ret
= st
->chip_info
->write(st
, val
);
124 mutex_unlock(&indio_dev
->mlock
);
126 return ret
? ret
: len
;
129 static const struct iio_chan_spec_ext_info ad5446_ext_info_powerdown
[] = {
132 .read
= ad5446_read_dac_powerdown
,
133 .write
= ad5446_write_dac_powerdown
,
134 .shared
= IIO_SEPARATE
,
136 IIO_ENUM("powerdown_mode", IIO_SEPARATE
, &ad5446_powerdown_mode_enum
),
137 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum
),
141 #define _AD5446_CHANNEL(bits, storage, _shift, ext) { \
142 .type = IIO_VOLTAGE, \
146 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
147 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
150 .realbits = (bits), \
151 .storagebits = (storage), \
157 #define AD5446_CHANNEL(bits, storage, shift) \
158 _AD5446_CHANNEL(bits, storage, shift, NULL)
160 #define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \
161 _AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown)
163 static int ad5446_read_raw(struct iio_dev
*indio_dev
,
164 struct iio_chan_spec
const *chan
,
169 struct ad5446_state
*st
= iio_priv(indio_dev
);
172 case IIO_CHAN_INFO_RAW
:
173 *val
= st
->cached_val
;
175 case IIO_CHAN_INFO_SCALE
:
177 *val2
= chan
->scan_type
.realbits
;
178 return IIO_VAL_FRACTIONAL_LOG2
;
183 static int ad5446_write_raw(struct iio_dev
*indio_dev
,
184 struct iio_chan_spec
const *chan
,
189 struct ad5446_state
*st
= iio_priv(indio_dev
);
193 case IIO_CHAN_INFO_RAW
:
194 if (val
>= (1 << chan
->scan_type
.realbits
) || val
< 0)
197 val
<<= chan
->scan_type
.shift
;
198 mutex_lock(&indio_dev
->mlock
);
199 st
->cached_val
= val
;
201 ret
= st
->chip_info
->write(st
, val
);
202 mutex_unlock(&indio_dev
->mlock
);
211 static const struct iio_info ad5446_info
= {
212 .read_raw
= ad5446_read_raw
,
213 .write_raw
= ad5446_write_raw
,
216 static int ad5446_probe(struct device
*dev
, const char *name
,
217 const struct ad5446_chip_info
*chip_info
)
219 struct ad5446_state
*st
;
220 struct iio_dev
*indio_dev
;
221 struct regulator
*reg
;
222 int ret
, voltage_uv
= 0;
224 reg
= devm_regulator_get(dev
, "vcc");
226 ret
= regulator_enable(reg
);
230 ret
= regulator_get_voltage(reg
);
232 goto error_disable_reg
;
237 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
238 if (indio_dev
== NULL
) {
240 goto error_disable_reg
;
242 st
= iio_priv(indio_dev
);
243 st
->chip_info
= chip_info
;
245 dev_set_drvdata(dev
, indio_dev
);
249 /* Establish that the iio_dev is a child of the device */
250 indio_dev
->dev
.parent
= dev
;
251 indio_dev
->name
= name
;
252 indio_dev
->info
= &ad5446_info
;
253 indio_dev
->modes
= INDIO_DIRECT_MODE
;
254 indio_dev
->channels
= &st
->chip_info
->channel
;
255 indio_dev
->num_channels
= 1;
257 st
->pwr_down_mode
= MODE_PWRDWN_1k
;
259 if (st
->chip_info
->int_vref_mv
)
260 st
->vref_mv
= st
->chip_info
->int_vref_mv
;
262 st
->vref_mv
= voltage_uv
/ 1000;
264 dev_warn(dev
, "reference voltage unspecified\n");
266 ret
= iio_device_register(indio_dev
);
268 goto error_disable_reg
;
274 regulator_disable(reg
);
278 static int ad5446_remove(struct device
*dev
)
280 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
281 struct ad5446_state
*st
= iio_priv(indio_dev
);
283 iio_device_unregister(indio_dev
);
284 if (!IS_ERR(st
->reg
))
285 regulator_disable(st
->reg
);
290 #if IS_ENABLED(CONFIG_SPI_MASTER)
292 static int ad5446_write(struct ad5446_state
*st
, unsigned val
)
294 struct spi_device
*spi
= to_spi_device(st
->dev
);
295 __be16 data
= cpu_to_be16(val
);
297 return spi_write(spi
, &data
, sizeof(data
));
300 static int ad5660_write(struct ad5446_state
*st
, unsigned val
)
302 struct spi_device
*spi
= to_spi_device(st
->dev
);
305 data
[0] = (val
>> 16) & 0xFF;
306 data
[1] = (val
>> 8) & 0xFF;
307 data
[2] = val
& 0xFF;
309 return spi_write(spi
, data
, sizeof(data
));
313 * ad5446_supported_spi_device_ids:
314 * The AD5620/40/60 parts are available in different fixed internal reference
315 * voltage options. The actual part numbers may look differently
316 * (and a bit cryptic), however this style is used to make clear which
317 * parts are supported here.
319 enum ad5446_supported_spi_device_ids
{
344 static const struct ad5446_chip_info ad5446_spi_chip_info
[] = {
346 .channel
= AD5446_CHANNEL_POWERDOWN(8, 16, 4),
347 .write
= ad5446_write
,
350 .channel
= AD5446_CHANNEL_POWERDOWN(10, 16, 2),
351 .write
= ad5446_write
,
354 .channel
= AD5446_CHANNEL_POWERDOWN(12, 16, 0),
355 .write
= ad5446_write
,
358 .channel
= AD5446_CHANNEL(12, 16, 2),
359 .write
= ad5446_write
,
362 .channel
= AD5446_CHANNEL(14, 16, 0),
363 .write
= ad5446_write
,
366 .channel
= AD5446_CHANNEL(8, 16, 6),
367 .write
= ad5446_write
,
370 .channel
= AD5446_CHANNEL(10, 16, 4),
371 .write
= ad5446_write
,
374 .channel
= AD5446_CHANNEL(16, 16, 0),
375 .write
= ad5446_write
,
378 .channel
= AD5446_CHANNEL(12, 16, 4),
379 .write
= ad5446_write
,
382 .channel
= AD5446_CHANNEL(14, 16, 0),
383 .write
= ad5446_write
,
386 .channel
= AD5446_CHANNEL(16, 16, 0),
387 .write
= ad5446_write
,
390 .channel
= AD5446_CHANNEL_POWERDOWN(8, 16, 6),
391 .write
= ad5446_write
,
394 .channel
= AD5446_CHANNEL_POWERDOWN(10, 16, 4),
395 .write
= ad5446_write
,
398 .channel
= AD5446_CHANNEL_POWERDOWN(12, 16, 2),
399 .write
= ad5446_write
,
402 .channel
= AD5446_CHANNEL_POWERDOWN(14, 16, 0),
403 .write
= ad5446_write
,
406 .channel
= AD5446_CHANNEL_POWERDOWN(12, 16, 2),
408 .write
= ad5446_write
,
411 .channel
= AD5446_CHANNEL_POWERDOWN(12, 16, 2),
413 .write
= ad5446_write
,
416 .channel
= AD5446_CHANNEL_POWERDOWN(14, 16, 0),
418 .write
= ad5446_write
,
421 .channel
= AD5446_CHANNEL_POWERDOWN(14, 16, 0),
423 .write
= ad5446_write
,
426 .channel
= AD5446_CHANNEL_POWERDOWN(16, 16, 0),
428 .write
= ad5660_write
,
431 .channel
= AD5446_CHANNEL_POWERDOWN(16, 16, 0),
433 .write
= ad5660_write
,
436 .channel
= AD5446_CHANNEL_POWERDOWN(16, 16, 0),
437 .write
= ad5660_write
,
441 static const struct spi_device_id ad5446_spi_ids
[] = {
442 {"ad5300", ID_AD5300
},
443 {"ad5310", ID_AD5310
},
444 {"ad5320", ID_AD5320
},
445 {"ad5444", ID_AD5444
},
446 {"ad5446", ID_AD5446
},
447 {"ad5450", ID_AD5450
},
448 {"ad5451", ID_AD5451
},
449 {"ad5452", ID_AD5444
}, /* ad5452 is compatible to the ad5444 */
450 {"ad5453", ID_AD5446
}, /* ad5453 is compatible to the ad5446 */
451 {"ad5512a", ID_AD5512A
},
452 {"ad5541a", ID_AD5541A
},
453 {"ad5542a", ID_AD5541A
}, /* ad5541a and ad5542a are compatible */
454 {"ad5543", ID_AD5541A
}, /* ad5541a and ad5543 are compatible */
455 {"ad5553", ID_AD5553
},
456 {"ad5600", ID_AD5600
},
457 {"ad5601", ID_AD5601
},
458 {"ad5611", ID_AD5611
},
459 {"ad5621", ID_AD5621
},
460 {"ad5641", ID_AD5641
},
461 {"ad5620-2500", ID_AD5620_2500
}, /* AD5620/40/60: */
462 {"ad5620-1250", ID_AD5620_1250
}, /* part numbers may look differently */
463 {"ad5640-2500", ID_AD5640_2500
},
464 {"ad5640-1250", ID_AD5640_1250
},
465 {"ad5660-2500", ID_AD5660_2500
},
466 {"ad5660-1250", ID_AD5660_1250
},
467 {"ad5662", ID_AD5662
},
468 {"dac081s101", ID_AD5300
}, /* compatible Texas Instruments chips */
469 {"dac101s101", ID_AD5310
},
470 {"dac121s101", ID_AD5320
},
471 {"dac7512", ID_AD5320
},
474 MODULE_DEVICE_TABLE(spi
, ad5446_spi_ids
);
477 static const struct of_device_id ad5446_of_ids
[] = {
478 { .compatible
= "ti,dac7512" },
481 MODULE_DEVICE_TABLE(of
, ad5446_of_ids
);
484 static int ad5446_spi_probe(struct spi_device
*spi
)
486 const struct spi_device_id
*id
= spi_get_device_id(spi
);
488 return ad5446_probe(&spi
->dev
, id
->name
,
489 &ad5446_spi_chip_info
[id
->driver_data
]);
492 static int ad5446_spi_remove(struct spi_device
*spi
)
494 return ad5446_remove(&spi
->dev
);
497 static struct spi_driver ad5446_spi_driver
= {
500 .of_match_table
= of_match_ptr(ad5446_of_ids
),
502 .probe
= ad5446_spi_probe
,
503 .remove
= ad5446_spi_remove
,
504 .id_table
= ad5446_spi_ids
,
507 static int __init
ad5446_spi_register_driver(void)
509 return spi_register_driver(&ad5446_spi_driver
);
512 static void ad5446_spi_unregister_driver(void)
514 spi_unregister_driver(&ad5446_spi_driver
);
519 static inline int ad5446_spi_register_driver(void) { return 0; }
520 static inline void ad5446_spi_unregister_driver(void) { }
524 #if IS_ENABLED(CONFIG_I2C)
526 static int ad5622_write(struct ad5446_state
*st
, unsigned val
)
528 struct i2c_client
*client
= to_i2c_client(st
->dev
);
529 __be16 data
= cpu_to_be16(val
);
531 return i2c_master_send(client
, (char *)&data
, sizeof(data
));
535 * ad5446_supported_i2c_device_ids:
536 * The AD5620/40/60 parts are available in different fixed internal reference
537 * voltage options. The actual part numbers may look differently
538 * (and a bit cryptic), however this style is used to make clear which
539 * parts are supported here.
541 enum ad5446_supported_i2c_device_ids
{
547 static const struct ad5446_chip_info ad5446_i2c_chip_info
[] = {
549 .channel
= AD5446_CHANNEL_POWERDOWN(8, 16, 4),
550 .write
= ad5622_write
,
553 .channel
= AD5446_CHANNEL_POWERDOWN(10, 16, 2),
554 .write
= ad5622_write
,
557 .channel
= AD5446_CHANNEL_POWERDOWN(12, 16, 0),
558 .write
= ad5622_write
,
562 static int ad5446_i2c_probe(struct i2c_client
*i2c
,
563 const struct i2c_device_id
*id
)
565 return ad5446_probe(&i2c
->dev
, id
->name
,
566 &ad5446_i2c_chip_info
[id
->driver_data
]);
569 static int ad5446_i2c_remove(struct i2c_client
*i2c
)
571 return ad5446_remove(&i2c
->dev
);
574 static const struct i2c_device_id ad5446_i2c_ids
[] = {
575 {"ad5301", ID_AD5602
},
576 {"ad5311", ID_AD5612
},
577 {"ad5321", ID_AD5622
},
578 {"ad5602", ID_AD5602
},
579 {"ad5612", ID_AD5612
},
580 {"ad5622", ID_AD5622
},
583 MODULE_DEVICE_TABLE(i2c
, ad5446_i2c_ids
);
585 static struct i2c_driver ad5446_i2c_driver
= {
589 .probe
= ad5446_i2c_probe
,
590 .remove
= ad5446_i2c_remove
,
591 .id_table
= ad5446_i2c_ids
,
594 static int __init
ad5446_i2c_register_driver(void)
596 return i2c_add_driver(&ad5446_i2c_driver
);
599 static void __exit
ad5446_i2c_unregister_driver(void)
601 i2c_del_driver(&ad5446_i2c_driver
);
606 static inline int ad5446_i2c_register_driver(void) { return 0; }
607 static inline void ad5446_i2c_unregister_driver(void) { }
611 static int __init
ad5446_init(void)
615 ret
= ad5446_spi_register_driver();
619 ret
= ad5446_i2c_register_driver();
621 ad5446_spi_unregister_driver();
627 module_init(ad5446_init
);
629 static void __exit
ad5446_exit(void)
631 ad5446_i2c_unregister_driver();
632 ad5446_spi_unregister_driver();
634 module_exit(ad5446_exit
);
636 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
637 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC");
638 MODULE_LICENSE("GPL v2");