11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
15 #include <linux/list.h>
17 #include <linux/i2c.h>
20 #include <linux/module.h>
25 #define MODE_PWRDWN_1k 0x1
26 #define MODE_PWRDWN_100k 0x2
27 #define MODE_PWRDWN_TRISTATE 0x3
60 static const char *
const ad5446_powerdown_modes[] = {
61 "1kohm_to_gnd",
"100kohm_to_gnd",
"three_state"
64 static int ad5446_set_powerdown_mode(
struct iio_dev *indio_dev,
74 static int ad5446_get_powerdown_mode(
struct iio_dev *indio_dev,
82 static const struct iio_enum ad5446_powerdown_mode_enum = {
83 .items = ad5446_powerdown_modes,
84 .num_items =
ARRAY_SIZE(ad5446_powerdown_modes),
85 .get = ad5446_get_powerdown_mode,
86 .set = ad5446_set_powerdown_mode,
89 static ssize_t ad5446_read_dac_powerdown(
struct iio_dev *indio_dev,
99 static ssize_t ad5446_write_dac_powerdown(
struct iio_dev *indio_dev,
102 const char *
buf,
size_t len)
127 return ret ? ret : len;
133 .read = ad5446_read_dac_powerdown,
134 .write = ad5446_write_dac_powerdown,
136 IIO_ENUM(
"powerdown_mode",
false, &ad5446_powerdown_mode_enum),
141 #define _AD5446_CHANNEL(bits, storage, shift, ext) { \
142 .type = IIO_VOLTAGE, \
146 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
147 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
148 .scan_type = IIO_ST('u', (bits), (storage), (shift)), \
152 #define AD5446_CHANNEL(bits, storage, shift) \
153 _AD5446_CHANNEL(bits, storage, shift, NULL)
155 #define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \
156 _AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown)
158 static int ad5446_read_raw(
struct iio_dev *indio_dev,
165 unsigned long scale_uv;
173 *val = scale_uv / 1000;
174 *val2 = (scale_uv % 1000) * 1000;
181 static int ad5446_write_raw(
struct iio_dev *indio_dev,
192 if (val >= (1 << chan->
scan_type.realbits) || val < 0)
209 static const struct iio_info ad5446_info = {
210 .read_raw = ad5446_read_raw,
211 .write_raw = ad5446_write_raw,
221 int ret, voltage_uv = 0;
233 if (indio_dev ==
NULL) {
235 goto error_disable_reg;
237 st = iio_priv(indio_dev);
245 indio_dev->
dev.parent =
dev;
247 indio_dev->
info = &ad5446_info;
257 st->
vref_mv = voltage_uv / 1000;
259 dev_warn(dev,
"reference voltage unspecified\n");
263 goto error_free_device;
279 static int ad5446_remove(
struct device *dev)
285 if (!IS_ERR(st->
reg)) {
294 #if IS_ENABLED(CONFIG_SPI_MASTER)
296 static int ad5446_write(
struct ad5446_state *st,
unsigned val)
301 return spi_write(spi, &data,
sizeof(data));
304 static int ad5660_write(
struct ad5446_state *st,
unsigned val)
309 data[0] = (val >> 16) & 0xFF;
310 data[1] = (val >> 8) & 0xFF;
311 data[2] = val & 0xFF;
313 return spi_write(spi, data,
sizeof(data));
323 enum ad5446_supported_spi_device_ids {
349 .write = ad5446_write,
353 .write = ad5446_write,
357 .write = ad5446_write,
361 .write = ad5446_write,
365 .write = ad5446_write,
369 .write = ad5446_write,
373 .write = ad5446_write,
377 .write = ad5446_write,
381 .write = ad5446_write,
385 .write = ad5446_write,
389 .write = ad5446_write,
393 .write = ad5446_write,
397 .write = ad5446_write,
402 .write = ad5446_write,
407 .write = ad5446_write,
412 .write = ad5446_write,
417 .write = ad5446_write,
422 .write = ad5660_write,
427 .write = ad5660_write,
431 .write = ad5660_write,
436 {
"ad5300", ID_AD5300},
437 {
"ad5310", ID_AD5310},
438 {
"ad5320", ID_AD5320},
439 {
"ad5444", ID_AD5444},
440 {
"ad5446", ID_AD5446},
441 {
"ad5450", ID_AD5450},
442 {
"ad5451", ID_AD5451},
443 {
"ad5452", ID_AD5444},
444 {
"ad5453", ID_AD5446},
445 {
"ad5512a", ID_AD5512A},
446 {
"ad5541a", ID_AD5541A},
447 {
"ad5542a", ID_AD5541A},
448 {
"ad5543", ID_AD5541A},
449 {
"ad5553", ID_AD5553},
450 {
"ad5601", ID_AD5601},
451 {
"ad5611", ID_AD5611},
452 {
"ad5621", ID_AD5621},
453 {
"ad5620-2500", ID_AD5620_2500},
454 {
"ad5620-1250", ID_AD5620_1250},
455 {
"ad5640-2500", ID_AD5640_2500},
456 {
"ad5640-1250", ID_AD5640_1250},
457 {
"ad5660-2500", ID_AD5660_2500},
458 {
"ad5660-1250", ID_AD5660_1250},
459 {
"ad5662", ID_AD5662},
468 return ad5446_probe(&spi->
dev, id->
name,
469 &ad5446_spi_chip_info[id->driver_data]);
474 return ad5446_remove(&spi->
dev);
477 static struct spi_driver ad5446_spi_driver = {
482 .probe = ad5446_spi_probe,
484 .id_table = ad5446_spi_ids,
487 static int __init ad5446_spi_register_driver(
void)
492 static void ad5446_spi_unregister_driver(
void)
494 spi_unregister_driver(&ad5446_spi_driver);
499 static inline int ad5446_spi_register_driver(
void) {
return 0; }
500 static inline void ad5446_spi_unregister_driver(
void) { }
504 #if IS_ENABLED(CONFIG_I2C)
506 static int ad5622_write(
struct ad5446_state *st,
unsigned val)
521 enum ad5446_supported_i2c_device_ids {
530 .write = ad5622_write,
534 .write = ad5622_write,
538 .write = ad5622_write,
545 return ad5446_probe(&i2c->
dev, id->
name,
546 &ad5446_i2c_chip_info[id->driver_data]);
551 return ad5446_remove(&i2c->
dev);
555 {
"ad5301", ID_AD5602},
556 {
"ad5311", ID_AD5612},
557 {
"ad5321", ID_AD5622},
558 {
"ad5602", ID_AD5602},
559 {
"ad5612", ID_AD5612},
560 {
"ad5622", ID_AD5622},
565 static struct i2c_driver ad5446_i2c_driver = {
570 .probe = ad5446_i2c_probe,
572 .id_table = ad5446_i2c_ids,
575 static int __init ad5446_i2c_register_driver(
void)
577 return i2c_add_driver(&ad5446_i2c_driver);
580 static void __exit ad5446_i2c_unregister_driver(
void)
587 static inline int ad5446_i2c_register_driver(
void) {
return 0; }
588 static inline void ad5446_i2c_unregister_driver(
void) { }
592 static int __init ad5446_init(
void)
596 ret = ad5446_spi_register_driver();
600 ret = ad5446_i2c_register_driver();
602 ad5446_spi_unregister_driver();
610 static void __exit ad5446_exit(
void)
612 ad5446_i2c_unregister_driver();
613 ad5446_spi_unregister_driver();