max5487.c 3.9 KB

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