ad5686-spi.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * AD5672R, AD5676, AD5676R, AD5681R, AD5682R, AD5683, AD5683R,
  4. * AD5684, AD5684R, AD5685R, AD5686, AD5686R
  5. * Digital to analog converters driver
  6. *
  7. * Copyright 2018 Analog Devices Inc.
  8. */
  9. #include "ad5686.h"
  10. #include <linux/module.h>
  11. #include <linux/spi/spi.h>
  12. static int ad5686_spi_write(struct ad5686_state *st,
  13. u8 cmd, u8 addr, u16 val)
  14. {
  15. struct spi_device *spi = to_spi_device(st->dev);
  16. u8 tx_len, *buf;
  17. switch (st->chip_info->regmap_type) {
  18. case AD5683_REGMAP:
  19. st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) |
  20. AD5683_DATA(val));
  21. buf = &st->data[0].d8[1];
  22. tx_len = 3;
  23. break;
  24. case AD5686_REGMAP:
  25. st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) |
  26. AD5686_ADDR(addr) |
  27. val);
  28. buf = &st->data[0].d8[1];
  29. tx_len = 3;
  30. break;
  31. default:
  32. return -EINVAL;
  33. }
  34. return spi_write(spi, buf, tx_len);
  35. }
  36. static int ad5686_spi_read(struct ad5686_state *st, u8 addr)
  37. {
  38. struct spi_transfer t[] = {
  39. {
  40. .tx_buf = &st->data[0].d8[1],
  41. .len = 3,
  42. .cs_change = 1,
  43. }, {
  44. .tx_buf = &st->data[1].d8[1],
  45. .rx_buf = &st->data[2].d8[1],
  46. .len = 3,
  47. },
  48. };
  49. struct spi_device *spi = to_spi_device(st->dev);
  50. u8 cmd = 0;
  51. int ret;
  52. if (st->chip_info->regmap_type == AD5686_REGMAP)
  53. cmd = AD5686_CMD_READBACK_ENABLE;
  54. else if (st->chip_info->regmap_type == AD5683_REGMAP)
  55. cmd = AD5686_CMD_READBACK_ENABLE_V2;
  56. st->data[0].d32 = cpu_to_be32(AD5686_CMD(cmd) |
  57. AD5686_ADDR(addr));
  58. st->data[1].d32 = cpu_to_be32(AD5686_CMD(AD5686_CMD_NOOP));
  59. ret = spi_sync_transfer(spi, t, ARRAY_SIZE(t));
  60. if (ret < 0)
  61. return ret;
  62. return be32_to_cpu(st->data[2].d32);
  63. }
  64. static int ad5686_spi_probe(struct spi_device *spi)
  65. {
  66. const struct spi_device_id *id = spi_get_device_id(spi);
  67. return ad5686_probe(&spi->dev, id->driver_data, id->name,
  68. ad5686_spi_write, ad5686_spi_read);
  69. }
  70. static int ad5686_spi_remove(struct spi_device *spi)
  71. {
  72. return ad5686_remove(&spi->dev);
  73. }
  74. static const struct spi_device_id ad5686_spi_id[] = {
  75. {"ad5672r", ID_AD5672R},
  76. {"ad5676", ID_AD5676},
  77. {"ad5676r", ID_AD5676R},
  78. {"ad5681r", ID_AD5681R},
  79. {"ad5682r", ID_AD5682R},
  80. {"ad5683", ID_AD5683},
  81. {"ad5683r", ID_AD5683R},
  82. {"ad5684", ID_AD5684},
  83. {"ad5684r", ID_AD5684R},
  84. {"ad5685", ID_AD5685R}, /* Does not exist */
  85. {"ad5685r", ID_AD5685R},
  86. {"ad5686", ID_AD5686},
  87. {"ad5686r", ID_AD5686R},
  88. {}
  89. };
  90. MODULE_DEVICE_TABLE(spi, ad5686_spi_id);
  91. static struct spi_driver ad5686_spi_driver = {
  92. .driver = {
  93. .name = "ad5686",
  94. },
  95. .probe = ad5686_spi_probe,
  96. .remove = ad5686_spi_remove,
  97. .id_table = ad5686_spi_id,
  98. };
  99. module_spi_driver(ad5686_spi_driver);
  100. MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
  101. MODULE_DESCRIPTION("Analog Devices AD5686 and similar multi-channel DACs");
  102. MODULE_LICENSE("GPL v2");