max5481.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Maxim Integrated MAX5481-MAX5484 digital potentiometer driver
  4. * Copyright 2016 Rockwell Collins
  5. *
  6. * Datasheet:
  7. * https://datasheets.maximintegrated.com/en/ds/MAX5481-MAX5484.pdf
  8. */
  9. #include <linux/iio/iio.h>
  10. #include <linux/iio/sysfs.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/property.h>
  14. #include <linux/spi/spi.h>
  15. /* write wiper reg */
  16. #define MAX5481_WRITE_WIPER (0 << 4)
  17. /* copy wiper reg to NV reg */
  18. #define MAX5481_COPY_AB_TO_NV (2 << 4)
  19. /* copy NV reg to wiper reg */
  20. #define MAX5481_COPY_NV_TO_AB (3 << 4)
  21. #define MAX5481_MAX_POS 1023
  22. enum max5481_variant {
  23. max5481,
  24. max5482,
  25. max5483,
  26. max5484,
  27. };
  28. struct max5481_cfg {
  29. int kohms;
  30. };
  31. static const struct max5481_cfg max5481_cfg[] = {
  32. [max5481] = { .kohms = 10, },
  33. [max5482] = { .kohms = 50, },
  34. [max5483] = { .kohms = 10, },
  35. [max5484] = { .kohms = 50, },
  36. };
  37. struct max5481_data {
  38. struct spi_device *spi;
  39. const struct max5481_cfg *cfg;
  40. u8 msg[3] ____cacheline_aligned;
  41. };
  42. #define MAX5481_CHANNEL { \
  43. .type = IIO_RESISTANCE, \
  44. .indexed = 1, \
  45. .output = 1, \
  46. .channel = 0, \
  47. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  48. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  49. }
  50. static const struct iio_chan_spec max5481_channels[] = {
  51. MAX5481_CHANNEL,
  52. };
  53. static int max5481_write_cmd(struct max5481_data *data, u8 cmd, u16 val)
  54. {
  55. struct spi_device *spi = data->spi;
  56. data->msg[0] = cmd;
  57. switch (cmd) {
  58. case MAX5481_WRITE_WIPER:
  59. data->msg[1] = val >> 2;
  60. data->msg[2] = (val & 0x3) << 6;
  61. return spi_write(spi, data->msg, 3);
  62. case MAX5481_COPY_AB_TO_NV:
  63. case MAX5481_COPY_NV_TO_AB:
  64. return spi_write(spi, data->msg, 1);
  65. default:
  66. return -EIO;
  67. }
  68. }
  69. static int max5481_read_raw(struct iio_dev *indio_dev,
  70. struct iio_chan_spec const *chan,
  71. int *val, int *val2, long mask)
  72. {
  73. struct max5481_data *data = iio_priv(indio_dev);
  74. if (mask != IIO_CHAN_INFO_SCALE)
  75. return -EINVAL;
  76. *val = 1000 * data->cfg->kohms;
  77. *val2 = MAX5481_MAX_POS;
  78. return IIO_VAL_FRACTIONAL;
  79. }
  80. static int max5481_write_raw(struct iio_dev *indio_dev,
  81. struct iio_chan_spec const *chan,
  82. int val, int val2, long mask)
  83. {
  84. struct max5481_data *data = iio_priv(indio_dev);
  85. if (mask != IIO_CHAN_INFO_RAW)
  86. return -EINVAL;
  87. if (val < 0 || val > MAX5481_MAX_POS)
  88. return -EINVAL;
  89. return max5481_write_cmd(data, MAX5481_WRITE_WIPER, val);
  90. }
  91. static const struct iio_info max5481_info = {
  92. .read_raw = max5481_read_raw,
  93. .write_raw = max5481_write_raw,
  94. };
  95. static const struct of_device_id max5481_match[] = {
  96. { .compatible = "maxim,max5481", .data = &max5481_cfg[max5481] },
  97. { .compatible = "maxim,max5482", .data = &max5481_cfg[max5482] },
  98. { .compatible = "maxim,max5483", .data = &max5481_cfg[max5483] },
  99. { .compatible = "maxim,max5484", .data = &max5481_cfg[max5484] },
  100. { }
  101. };
  102. MODULE_DEVICE_TABLE(of, max5481_match);
  103. static int max5481_probe(struct spi_device *spi)
  104. {
  105. struct iio_dev *indio_dev;
  106. struct max5481_data *data;
  107. const struct spi_device_id *id = spi_get_device_id(spi);
  108. int ret;
  109. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
  110. if (!indio_dev)
  111. return -ENOMEM;
  112. dev_set_drvdata(&spi->dev, indio_dev);
  113. data = iio_priv(indio_dev);
  114. data->spi = spi;
  115. data->cfg = device_get_match_data(&spi->dev);
  116. if (!data->cfg)
  117. data->cfg = &max5481_cfg[id->driver_data];
  118. indio_dev->name = id->name;
  119. indio_dev->modes = INDIO_DIRECT_MODE;
  120. /* variant specific configuration */
  121. indio_dev->info = &max5481_info;
  122. indio_dev->channels = max5481_channels;
  123. indio_dev->num_channels = ARRAY_SIZE(max5481_channels);
  124. /* restore wiper from NV */
  125. ret = max5481_write_cmd(data, MAX5481_COPY_NV_TO_AB, 0);
  126. if (ret < 0)
  127. return ret;
  128. return iio_device_register(indio_dev);
  129. }
  130. static int max5481_remove(struct spi_device *spi)
  131. {
  132. struct iio_dev *indio_dev = dev_get_drvdata(&spi->dev);
  133. struct max5481_data *data = iio_priv(indio_dev);
  134. iio_device_unregister(indio_dev);
  135. /* save wiper reg to NV reg */
  136. return max5481_write_cmd(data, MAX5481_COPY_AB_TO_NV, 0);
  137. }
  138. static const struct spi_device_id max5481_id_table[] = {
  139. { "max5481", max5481 },
  140. { "max5482", max5482 },
  141. { "max5483", max5483 },
  142. { "max5484", max5484 },
  143. { }
  144. };
  145. MODULE_DEVICE_TABLE(spi, max5481_id_table);
  146. static struct spi_driver max5481_driver = {
  147. .driver = {
  148. .name = "max5481",
  149. .of_match_table = max5481_match,
  150. },
  151. .probe = max5481_probe,
  152. .remove = max5481_remove,
  153. .id_table = max5481_id_table,
  154. };
  155. module_spi_driver(max5481_driver);
  156. MODULE_AUTHOR("Maury Anderson <maury.anderson@rockwellcollins.com>");
  157. MODULE_DESCRIPTION("max5481 SPI driver");
  158. MODULE_LICENSE("GPL v2");