max5481.c 5.2 KB

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