1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * AD5446 SPI DAC driver 4 * 5 * Copyright 2010 Analog Devices Inc. 6 */ 7 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> 20 21 #include <linux/iio/iio.h> 22 #include <linux/iio/sysfs.h> 23 24 #include <asm/unaligned.h> 25 26 #define MODE_PWRDWN_1k 0x1 27 #define MODE_PWRDWN_100k 0x2 28 #define MODE_PWRDWN_TRISTATE 0x3 29 30 /** 31 * struct ad5446_state - driver instance specific data 32 * @spi: spi_device 33 * @chip_info: chip model specific constants, available modes etc 34 * @reg: supply regulator 35 * @vref_mv: actual reference voltage used 36 * @lock lock to protect the data buffer during write ops 37 */ 38 39 struct ad5446_state { 40 struct device *dev; 41 const struct ad5446_chip_info *chip_info; 42 struct regulator *reg; 43 unsigned short vref_mv; 44 unsigned cached_val; 45 unsigned pwr_down_mode; 46 unsigned pwr_down; 47 struct mutex lock; 48 }; 49 50 /** 51 * struct ad5446_chip_info - chip specific information 52 * @channel: channel spec for the DAC 53 * @int_vref_mv: AD5620/40/60: the internal reference voltage 54 * @write: chip specific helper function to write to the register 55 */ 56 57 struct ad5446_chip_info { 58 struct iio_chan_spec channel; 59 u16 int_vref_mv; 60 int (*write)(struct ad5446_state *st, unsigned val); 61 }; 62 63 static const char * const ad5446_powerdown_modes[] = { 64 "1kohm_to_gnd", "100kohm_to_gnd", "three_state" 65 }; 66 67 static int ad5446_set_powerdown_mode(struct iio_dev *indio_dev, 68 const struct iio_chan_spec *chan, unsigned int mode) 69 { 70 struct ad5446_state *st = iio_priv(indio_dev); 71 72 st->pwr_down_mode = mode + 1; 73 74 return 0; 75 } 76 77 static int ad5446_get_powerdown_mode(struct iio_dev *indio_dev, 78 const struct iio_chan_spec *chan) 79 { 80 struct ad5446_state *st = iio_priv(indio_dev); 81 82 return st->pwr_down_mode - 1; 83 } 84 85 static const struct iio_enum ad5446_powerdown_mode_enum = { 86 .items = ad5446_powerdown_modes, 87 .num_items = ARRAY_SIZE(ad5446_powerdown_modes), 88 .get = ad5446_get_powerdown_mode, 89 .set = ad5446_set_powerdown_mode, 90 }; 91 92 static ssize_t ad5446_read_dac_powerdown(struct iio_dev *indio_dev, 93 uintptr_t private, 94 const struct iio_chan_spec *chan, 95 char *buf) 96 { 97 struct ad5446_state *st = iio_priv(indio_dev); 98 99 return sprintf(buf, "%d\n", st->pwr_down); 100 } 101 102 static ssize_t ad5446_write_dac_powerdown(struct iio_dev *indio_dev, 103 uintptr_t private, 104 const struct iio_chan_spec *chan, 105 const char *buf, size_t len) 106 { 107 struct ad5446_state *st = iio_priv(indio_dev); 108 unsigned int shift; 109 unsigned int val; 110 bool powerdown; 111 int ret; 112 113 ret = strtobool(buf, &powerdown); 114 if (ret) 115 return ret; 116 117 mutex_lock(&st->lock); 118 st->pwr_down = powerdown; 119 120 if (st->pwr_down) { 121 shift = chan->scan_type.realbits + chan->scan_type.shift; 122 val = st->pwr_down_mode << shift; 123 } else { 124 val = st->cached_val; 125 } 126 127 ret = st->chip_info->write(st, val); 128 mutex_unlock(&st->lock); 129 130 return ret ? ret : len; 131 } 132 133 static const struct iio_chan_spec_ext_info ad5446_ext_info_powerdown[] = { 134 { 135 .name = "powerdown", 136 .read = ad5446_read_dac_powerdown, 137 .write = ad5446_write_dac_powerdown, 138 .shared = IIO_SEPARATE, 139 }, 140 IIO_ENUM("powerdown_mode", IIO_SEPARATE, &ad5446_powerdown_mode_enum), 141 IIO_ENUM_AVAILABLE("powerdown_mode", &ad5446_powerdown_mode_enum), 142 { }, 143 }; 144 145 #define _AD5446_CHANNEL(bits, storage, _shift, ext) { \ 146 .type = IIO_VOLTAGE, \ 147 .indexed = 1, \ 148 .output = 1, \ 149 .channel = 0, \ 150 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 151 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 152 .scan_type = { \ 153 .sign = 'u', \ 154 .realbits = (bits), \ 155 .storagebits = (storage), \ 156 .shift = (_shift), \ 157 }, \ 158 .ext_info = (ext), \ 159 } 160 161 #define AD5446_CHANNEL(bits, storage, shift) \ 162 _AD5446_CHANNEL(bits, storage, shift, NULL) 163 164 #define AD5446_CHANNEL_POWERDOWN(bits, storage, shift) \ 165 _AD5446_CHANNEL(bits, storage, shift, ad5446_ext_info_powerdown) 166 167 static int ad5446_read_raw(struct iio_dev *indio_dev, 168 struct iio_chan_spec const *chan, 169 int *val, 170 int *val2, 171 long m) 172 { 173 struct ad5446_state *st = iio_priv(indio_dev); 174 175 switch (m) { 176 case IIO_CHAN_INFO_RAW: 177 *val = st->cached_val; 178 return IIO_VAL_INT; 179 case IIO_CHAN_INFO_SCALE: 180 *val = st->vref_mv; 181 *val2 = chan->scan_type.realbits; 182 return IIO_VAL_FRACTIONAL_LOG2; 183 } 184 return -EINVAL; 185 } 186 187 static int ad5446_write_raw(struct iio_dev *indio_dev, 188 struct iio_chan_spec const *chan, 189 int val, 190 int val2, 191 long mask) 192 { 193 struct ad5446_state *st = iio_priv(indio_dev); 194 int ret = 0; 195 196 switch (mask) { 197 case IIO_CHAN_INFO_RAW: 198 if (val >= (1 << chan->scan_type.realbits) || val < 0) 199 return -EINVAL; 200 201 val <<= chan->scan_type.shift; 202 mutex_lock(&st->lock); 203 st->cached_val = val; 204 if (!st->pwr_down) 205 ret = st->chip_info->write(st, val); 206 mutex_unlock(&st->lock); 207 break; 208 default: 209 ret = -EINVAL; 210 } 211 212 return ret; 213 } 214 215 static const struct iio_info ad5446_info = { 216 .read_raw = ad5446_read_raw, 217 .write_raw = ad5446_write_raw, 218 }; 219 220 static int ad5446_probe(struct device *dev, const char *name, 221 const struct ad5446_chip_info *chip_info) 222 { 223 struct ad5446_state *st; 224 struct iio_dev *indio_dev; 225 struct regulator *reg; 226 int ret, voltage_uv = 0; 227 228 reg = devm_regulator_get(dev, "vcc"); 229 if (!IS_ERR(reg)) { 230 ret = regulator_enable(reg); 231 if (ret) 232 return ret; 233 234 ret = regulator_get_voltage(reg); 235 if (ret < 0) 236 goto error_disable_reg; 237 238 voltage_uv = ret; 239 } 240 241 indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 242 if (indio_dev == NULL) { 243 ret = -ENOMEM; 244 goto error_disable_reg; 245 } 246 st = iio_priv(indio_dev); 247 st->chip_info = chip_info; 248 249 dev_set_drvdata(dev, indio_dev); 250 st->reg = reg; 251 st->dev = dev; 252 253 /* Establish that the iio_dev is a child of the device */ 254 indio_dev->dev.parent = dev; 255 indio_dev->name = name; 256 indio_dev->info = &ad5446_info; 257 indio_dev->modes = INDIO_DIRECT_MODE; 258 indio_dev->channels = &st->chip_info->channel; 259 indio_dev->num_channels = 1; 260 261 mutex_init(&st->lock); 262 263 st->pwr_down_mode = MODE_PWRDWN_1k; 264 265 if (st->chip_info->int_vref_mv) 266 st->vref_mv = st->chip_info->int_vref_mv; 267 else if (voltage_uv) 268 st->vref_mv = voltage_uv / 1000; 269 else 270 dev_warn(dev, "reference voltage unspecified\n"); 271 272 ret = iio_device_register(indio_dev); 273 if (ret) 274 goto error_disable_reg; 275 276 return 0; 277 278 error_disable_reg: 279 if (!IS_ERR(reg)) 280 regulator_disable(reg); 281 return ret; 282 } 283 284 static int ad5446_remove(struct device *dev) 285 { 286 struct iio_dev *indio_dev = dev_get_drvdata(dev); 287 struct ad5446_state *st = iio_priv(indio_dev); 288 289 iio_device_unregister(indio_dev); 290 if (!IS_ERR(st->reg)) 291 regulator_disable(st->reg); 292 293 return 0; 294 } 295 296 #if IS_ENABLED(CONFIG_SPI_MASTER) 297 298 static int ad5446_write(struct ad5446_state *st, unsigned val) 299 { 300 struct spi_device *spi = to_spi_device(st->dev); 301 __be16 data = cpu_to_be16(val); 302 303 return spi_write(spi, &data, sizeof(data)); 304 } 305 306 static int ad5660_write(struct ad5446_state *st, unsigned val) 307 { 308 struct spi_device *spi = to_spi_device(st->dev); 309 uint8_t data[3]; 310 311 put_unaligned_be24(val, &data[0]); 312 313 return spi_write(spi, data, sizeof(data)); 314 } 315 316 /** 317 * ad5446_supported_spi_device_ids: 318 * The AD5620/40/60 parts are available in different fixed internal reference 319 * voltage options. The actual part numbers may look differently 320 * (and a bit cryptic), however this style is used to make clear which 321 * parts are supported here. 322 */ 323 enum ad5446_supported_spi_device_ids { 324 ID_AD5300, 325 ID_AD5310, 326 ID_AD5320, 327 ID_AD5444, 328 ID_AD5446, 329 ID_AD5450, 330 ID_AD5451, 331 ID_AD5541A, 332 ID_AD5512A, 333 ID_AD5553, 334 ID_AD5600, 335 ID_AD5601, 336 ID_AD5611, 337 ID_AD5621, 338 ID_AD5641, 339 ID_AD5620_2500, 340 ID_AD5620_1250, 341 ID_AD5640_2500, 342 ID_AD5640_1250, 343 ID_AD5660_2500, 344 ID_AD5660_1250, 345 ID_AD5662, 346 }; 347 348 static const struct ad5446_chip_info ad5446_spi_chip_info[] = { 349 [ID_AD5300] = { 350 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4), 351 .write = ad5446_write, 352 }, 353 [ID_AD5310] = { 354 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2), 355 .write = ad5446_write, 356 }, 357 [ID_AD5320] = { 358 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0), 359 .write = ad5446_write, 360 }, 361 [ID_AD5444] = { 362 .channel = AD5446_CHANNEL(12, 16, 2), 363 .write = ad5446_write, 364 }, 365 [ID_AD5446] = { 366 .channel = AD5446_CHANNEL(14, 16, 0), 367 .write = ad5446_write, 368 }, 369 [ID_AD5450] = { 370 .channel = AD5446_CHANNEL(8, 16, 6), 371 .write = ad5446_write, 372 }, 373 [ID_AD5451] = { 374 .channel = AD5446_CHANNEL(10, 16, 4), 375 .write = ad5446_write, 376 }, 377 [ID_AD5541A] = { 378 .channel = AD5446_CHANNEL(16, 16, 0), 379 .write = ad5446_write, 380 }, 381 [ID_AD5512A] = { 382 .channel = AD5446_CHANNEL(12, 16, 4), 383 .write = ad5446_write, 384 }, 385 [ID_AD5553] = { 386 .channel = AD5446_CHANNEL(14, 16, 0), 387 .write = ad5446_write, 388 }, 389 [ID_AD5600] = { 390 .channel = AD5446_CHANNEL(16, 16, 0), 391 .write = ad5446_write, 392 }, 393 [ID_AD5601] = { 394 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 6), 395 .write = ad5446_write, 396 }, 397 [ID_AD5611] = { 398 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 4), 399 .write = ad5446_write, 400 }, 401 [ID_AD5621] = { 402 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), 403 .write = ad5446_write, 404 }, 405 [ID_AD5641] = { 406 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0), 407 .write = ad5446_write, 408 }, 409 [ID_AD5620_2500] = { 410 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), 411 .int_vref_mv = 2500, 412 .write = ad5446_write, 413 }, 414 [ID_AD5620_1250] = { 415 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 2), 416 .int_vref_mv = 1250, 417 .write = ad5446_write, 418 }, 419 [ID_AD5640_2500] = { 420 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0), 421 .int_vref_mv = 2500, 422 .write = ad5446_write, 423 }, 424 [ID_AD5640_1250] = { 425 .channel = AD5446_CHANNEL_POWERDOWN(14, 16, 0), 426 .int_vref_mv = 1250, 427 .write = ad5446_write, 428 }, 429 [ID_AD5660_2500] = { 430 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), 431 .int_vref_mv = 2500, 432 .write = ad5660_write, 433 }, 434 [ID_AD5660_1250] = { 435 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), 436 .int_vref_mv = 1250, 437 .write = ad5660_write, 438 }, 439 [ID_AD5662] = { 440 .channel = AD5446_CHANNEL_POWERDOWN(16, 16, 0), 441 .write = ad5660_write, 442 }, 443 }; 444 445 static const struct spi_device_id ad5446_spi_ids[] = { 446 {"ad5300", ID_AD5300}, 447 {"ad5310", ID_AD5310}, 448 {"ad5320", ID_AD5320}, 449 {"ad5444", ID_AD5444}, 450 {"ad5446", ID_AD5446}, 451 {"ad5450", ID_AD5450}, 452 {"ad5451", ID_AD5451}, 453 {"ad5452", ID_AD5444}, /* ad5452 is compatible to the ad5444 */ 454 {"ad5453", ID_AD5446}, /* ad5453 is compatible to the ad5446 */ 455 {"ad5512a", ID_AD5512A}, 456 {"ad5541a", ID_AD5541A}, 457 {"ad5542a", ID_AD5541A}, /* ad5541a and ad5542a are compatible */ 458 {"ad5543", ID_AD5541A}, /* ad5541a and ad5543 are compatible */ 459 {"ad5553", ID_AD5553}, 460 {"ad5600", ID_AD5600}, 461 {"ad5601", ID_AD5601}, 462 {"ad5611", ID_AD5611}, 463 {"ad5621", ID_AD5621}, 464 {"ad5641", ID_AD5641}, 465 {"ad5620-2500", ID_AD5620_2500}, /* AD5620/40/60: */ 466 {"ad5620-1250", ID_AD5620_1250}, /* part numbers may look differently */ 467 {"ad5640-2500", ID_AD5640_2500}, 468 {"ad5640-1250", ID_AD5640_1250}, 469 {"ad5660-2500", ID_AD5660_2500}, 470 {"ad5660-1250", ID_AD5660_1250}, 471 {"ad5662", ID_AD5662}, 472 {"dac081s101", ID_AD5300}, /* compatible Texas Instruments chips */ 473 {"dac101s101", ID_AD5310}, 474 {"dac121s101", ID_AD5320}, 475 {"dac7512", ID_AD5320}, 476 {} 477 }; 478 MODULE_DEVICE_TABLE(spi, ad5446_spi_ids); 479 480 #ifdef CONFIG_OF 481 static const struct of_device_id ad5446_of_ids[] = { 482 { .compatible = "ti,dac7512" }, 483 { } 484 }; 485 MODULE_DEVICE_TABLE(of, ad5446_of_ids); 486 #endif 487 488 static int ad5446_spi_probe(struct spi_device *spi) 489 { 490 const struct spi_device_id *id = spi_get_device_id(spi); 491 492 return ad5446_probe(&spi->dev, id->name, 493 &ad5446_spi_chip_info[id->driver_data]); 494 } 495 496 static int ad5446_spi_remove(struct spi_device *spi) 497 { 498 return ad5446_remove(&spi->dev); 499 } 500 501 static struct spi_driver ad5446_spi_driver = { 502 .driver = { 503 .name = "ad5446", 504 .of_match_table = of_match_ptr(ad5446_of_ids), 505 }, 506 .probe = ad5446_spi_probe, 507 .remove = ad5446_spi_remove, 508 .id_table = ad5446_spi_ids, 509 }; 510 511 static int __init ad5446_spi_register_driver(void) 512 { 513 return spi_register_driver(&ad5446_spi_driver); 514 } 515 516 static void ad5446_spi_unregister_driver(void) 517 { 518 spi_unregister_driver(&ad5446_spi_driver); 519 } 520 521 #else 522 523 static inline int ad5446_spi_register_driver(void) { return 0; } 524 static inline void ad5446_spi_unregister_driver(void) { } 525 526 #endif 527 528 #if IS_ENABLED(CONFIG_I2C) 529 530 static int ad5622_write(struct ad5446_state *st, unsigned val) 531 { 532 struct i2c_client *client = to_i2c_client(st->dev); 533 __be16 data = cpu_to_be16(val); 534 535 return i2c_master_send(client, (char *)&data, sizeof(data)); 536 } 537 538 /** 539 * ad5446_supported_i2c_device_ids: 540 * The AD5620/40/60 parts are available in different fixed internal reference 541 * voltage options. The actual part numbers may look differently 542 * (and a bit cryptic), however this style is used to make clear which 543 * parts are supported here. 544 */ 545 enum ad5446_supported_i2c_device_ids { 546 ID_AD5602, 547 ID_AD5612, 548 ID_AD5622, 549 }; 550 551 static const struct ad5446_chip_info ad5446_i2c_chip_info[] = { 552 [ID_AD5602] = { 553 .channel = AD5446_CHANNEL_POWERDOWN(8, 16, 4), 554 .write = ad5622_write, 555 }, 556 [ID_AD5612] = { 557 .channel = AD5446_CHANNEL_POWERDOWN(10, 16, 2), 558 .write = ad5622_write, 559 }, 560 [ID_AD5622] = { 561 .channel = AD5446_CHANNEL_POWERDOWN(12, 16, 0), 562 .write = ad5622_write, 563 }, 564 }; 565 566 static int ad5446_i2c_probe(struct i2c_client *i2c, 567 const struct i2c_device_id *id) 568 { 569 return ad5446_probe(&i2c->dev, id->name, 570 &ad5446_i2c_chip_info[id->driver_data]); 571 } 572 573 static int ad5446_i2c_remove(struct i2c_client *i2c) 574 { 575 return ad5446_remove(&i2c->dev); 576 } 577 578 static const struct i2c_device_id ad5446_i2c_ids[] = { 579 {"ad5301", ID_AD5602}, 580 {"ad5311", ID_AD5612}, 581 {"ad5321", ID_AD5622}, 582 {"ad5602", ID_AD5602}, 583 {"ad5612", ID_AD5612}, 584 {"ad5622", ID_AD5622}, 585 {} 586 }; 587 MODULE_DEVICE_TABLE(i2c, ad5446_i2c_ids); 588 589 static struct i2c_driver ad5446_i2c_driver = { 590 .driver = { 591 .name = "ad5446", 592 }, 593 .probe = ad5446_i2c_probe, 594 .remove = ad5446_i2c_remove, 595 .id_table = ad5446_i2c_ids, 596 }; 597 598 static int __init ad5446_i2c_register_driver(void) 599 { 600 return i2c_add_driver(&ad5446_i2c_driver); 601 } 602 603 static void __exit ad5446_i2c_unregister_driver(void) 604 { 605 i2c_del_driver(&ad5446_i2c_driver); 606 } 607 608 #else 609 610 static inline int ad5446_i2c_register_driver(void) { return 0; } 611 static inline void ad5446_i2c_unregister_driver(void) { } 612 613 #endif 614 615 static int __init ad5446_init(void) 616 { 617 int ret; 618 619 ret = ad5446_spi_register_driver(); 620 if (ret) 621 return ret; 622 623 ret = ad5446_i2c_register_driver(); 624 if (ret) { 625 ad5446_spi_unregister_driver(); 626 return ret; 627 } 628 629 return 0; 630 } 631 module_init(ad5446_init); 632 633 static void __exit ad5446_exit(void) 634 { 635 ad5446_i2c_unregister_driver(); 636 ad5446_spi_unregister_driver(); 637 } 638 module_exit(ad5446_exit); 639 640 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 641 MODULE_DESCRIPTION("Analog Devices AD5444/AD5446 DAC"); 642 MODULE_LICENSE("GPL v2"); 643