max5487.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * max5487.c - Support for MAX5487, MAX5488, MAX5489 digital potentiometers
  4. *
  5. * Copyright (C) 2016 Cristina-Gabriela Moraru <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/spi/spi.h>
  9. #include <linux/acpi.h>
  10. #include <linux/iio/sysfs.h>
  11. #include <linux/iio/iio.h>
  12. #define MAX5487_WRITE_WIPER_A (0x01 << 8)
  13. #define MAX5487_WRITE_WIPER_B (0x02 << 8)
  14. /* copy both wiper regs to NV regs */
  15. #define MAX5487_COPY_AB_TO_NV (0x23 << 8)
  16. /* copy both NV regs to wiper regs */
  17. #define MAX5487_COPY_NV_TO_AB (0x33 << 8)
  18. #define MAX5487_MAX_POS 255
  19. struct max5487_data {
  20. struct spi_device *spi;
  21. int kohms;
  22. };
  23. #define MAX5487_CHANNEL(ch, addr) { \
  24. .type = IIO_RESISTANCE, \
  25. .indexed = 1, \
  26. .output = 1, \
  27. .channel = ch, \
  28. .address = addr, \
  29. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  30. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  31. }
  32. static const struct iio_chan_spec max5487_channels[] = {
  33. MAX5487_CHANNEL(0, MAX5487_WRITE_WIPER_A),
  34. MAX5487_CHANNEL(1, MAX5487_WRITE_WIPER_B),
  35. };
  36. static int max5487_write_cmd(struct spi_device *spi, u16 cmd)
  37. {
  38. return spi_write(spi, (const void *) &cmd, sizeof(u16));
  39. }
  40. static int max5487_read_raw(struct iio_dev *indio_dev,
  41. struct iio_chan_spec const *chan,
  42. int *val, int *val2, long mask)
  43. {
  44. struct max5487_data *data = iio_priv(indio_dev);
  45. if (mask != IIO_CHAN_INFO_SCALE)
  46. return -EINVAL;
  47. *val = 1000 * data->kohms;
  48. *val2 = MAX5487_MAX_POS;
  49. return IIO_VAL_FRACTIONAL;
  50. }
  51. static int max5487_write_raw(struct iio_dev *indio_dev,
  52. struct iio_chan_spec const *chan,
  53. int val, int val2, long mask)
  54. {
  55. struct max5487_data *data = iio_priv(indio_dev);
  56. if (mask != IIO_CHAN_INFO_RAW)
  57. return -EINVAL;
  58. if (val < 0 || val > MAX5487_MAX_POS)
  59. return -EINVAL;
  60. return max5487_write_cmd(data->spi, chan->address | val);
  61. }
  62. static const struct iio_info max5487_info = {
  63. .read_raw = max5487_read_raw,
  64. .write_raw = max5487_write_raw,
  65. };
  66. static int max5487_spi_probe(struct spi_device *spi)
  67. {
  68. struct iio_dev *indio_dev;
  69. struct max5487_data *data;
  70. const struct spi_device_id *id = spi_get_device_id(spi);
  71. int ret;
  72. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
  73. if (!indio_dev)
  74. return -ENOMEM;
  75. spi_set_drvdata(spi, indio_dev);
  76. data = iio_priv(indio_dev);
  77. data->spi = spi;
  78. data->kohms = id->driver_data;
  79. indio_dev->info = &max5487_info;
  80. indio_dev->name = id->name;
  81. indio_dev->modes = INDIO_DIRECT_MODE;
  82. indio_dev->channels = max5487_channels;
  83. indio_dev->num_channels = ARRAY_SIZE(max5487_channels);
  84. /* restore both wiper regs from NV regs */
  85. ret = max5487_write_cmd(data->spi, MAX5487_COPY_NV_TO_AB);
  86. if (ret < 0)
  87. return ret;
  88. return iio_device_register(indio_dev);
  89. }
  90. static void max5487_spi_remove(struct spi_device *spi)
  91. {
  92. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  93. int ret;
  94. iio_device_unregister(indio_dev);
  95. /* save both wiper regs to NV regs */
  96. ret = max5487_write_cmd(spi, MAX5487_COPY_AB_TO_NV);
  97. if (ret)
  98. dev_warn(&spi->dev, "Failed to save wiper regs to NV regs\n");
  99. }
  100. static const struct spi_device_id max5487_id[] = {
  101. { "MAX5487", 10 },
  102. { "MAX5488", 50 },
  103. { "MAX5489", 100 },
  104. { }
  105. };
  106. MODULE_DEVICE_TABLE(spi, max5487_id);
  107. static const struct acpi_device_id max5487_acpi_match[] = {
  108. { "MAX5487", 10 },
  109. { "MAX5488", 50 },
  110. { "MAX5489", 100 },
  111. { },
  112. };
  113. MODULE_DEVICE_TABLE(acpi, max5487_acpi_match);
  114. static struct spi_driver max5487_driver = {
  115. .driver = {
  116. .name = "max5487",
  117. .acpi_match_table = ACPI_PTR(max5487_acpi_match),
  118. },
  119. .id_table = max5487_id,
  120. .probe = max5487_spi_probe,
  121. .remove = max5487_spi_remove
  122. };
  123. module_spi_driver(max5487_driver);
  124. MODULE_AUTHOR("Cristina-Gabriela Moraru <[email protected]>");
  125. MODULE_DESCRIPTION("max5487 SPI driver");
  126. MODULE_LICENSE("GPL v2");