max11100.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * iio/adc/max11100.c
  4. * Maxim max11100 ADC Driver with IIO interface
  5. *
  6. * Copyright (C) 2016-17 Renesas Electronics Corporation
  7. * Copyright (C) 2016-17 Jacopo Mondi
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/driver.h>
  17. /*
  18. * LSB is the ADC single digital step
  19. * 1 LSB = (vref_mv / 2 ^ 16)
  20. *
  21. * LSB is used to calculate analog voltage value
  22. * from the number of ADC steps count
  23. *
  24. * Ain = (count * LSB)
  25. */
  26. #define MAX11100_LSB_DIV (1 << 16)
  27. struct max11100_state {
  28. struct regulator *vref_reg;
  29. struct spi_device *spi;
  30. /*
  31. * DMA (thus cache coherency maintenance) requires the
  32. * transfer buffers to live in their own cache lines.
  33. */
  34. u8 buffer[3] ____cacheline_aligned;
  35. };
  36. static const struct iio_chan_spec max11100_channels[] = {
  37. { /* [0] */
  38. .type = IIO_VOLTAGE,
  39. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  40. BIT(IIO_CHAN_INFO_SCALE),
  41. },
  42. };
  43. static int max11100_read_single(struct iio_dev *indio_dev, int *val)
  44. {
  45. int ret;
  46. struct max11100_state *state = iio_priv(indio_dev);
  47. ret = spi_read(state->spi, state->buffer, sizeof(state->buffer));
  48. if (ret) {
  49. dev_err(&indio_dev->dev, "SPI transfer failed\n");
  50. return ret;
  51. }
  52. /* the first 8 bits sent out from ADC must be 0s */
  53. if (state->buffer[0]) {
  54. dev_err(&indio_dev->dev, "Invalid value: buffer[0] != 0\n");
  55. return -EINVAL;
  56. }
  57. *val = (state->buffer[1] << 8) | state->buffer[2];
  58. return 0;
  59. }
  60. static int max11100_read_raw(struct iio_dev *indio_dev,
  61. struct iio_chan_spec const *chan,
  62. int *val, int *val2, long info)
  63. {
  64. int ret, vref_uv;
  65. struct max11100_state *state = iio_priv(indio_dev);
  66. switch (info) {
  67. case IIO_CHAN_INFO_RAW:
  68. ret = max11100_read_single(indio_dev, val);
  69. if (ret)
  70. return ret;
  71. return IIO_VAL_INT;
  72. case IIO_CHAN_INFO_SCALE:
  73. vref_uv = regulator_get_voltage(state->vref_reg);
  74. if (vref_uv < 0)
  75. /* dummy regulator "get_voltage" returns -EINVAL */
  76. return -EINVAL;
  77. *val = vref_uv / 1000;
  78. *val2 = MAX11100_LSB_DIV;
  79. return IIO_VAL_FRACTIONAL;
  80. }
  81. return -EINVAL;
  82. }
  83. static const struct iio_info max11100_info = {
  84. .read_raw = max11100_read_raw,
  85. };
  86. static int max11100_probe(struct spi_device *spi)
  87. {
  88. int ret;
  89. struct iio_dev *indio_dev;
  90. struct max11100_state *state;
  91. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
  92. if (!indio_dev)
  93. return -ENOMEM;
  94. spi_set_drvdata(spi, indio_dev);
  95. state = iio_priv(indio_dev);
  96. state->spi = spi;
  97. indio_dev->name = "max11100";
  98. indio_dev->info = &max11100_info;
  99. indio_dev->modes = INDIO_DIRECT_MODE;
  100. indio_dev->channels = max11100_channels;
  101. indio_dev->num_channels = ARRAY_SIZE(max11100_channels);
  102. state->vref_reg = devm_regulator_get(&spi->dev, "vref");
  103. if (IS_ERR(state->vref_reg))
  104. return PTR_ERR(state->vref_reg);
  105. ret = regulator_enable(state->vref_reg);
  106. if (ret)
  107. return ret;
  108. ret = iio_device_register(indio_dev);
  109. if (ret)
  110. goto disable_regulator;
  111. return 0;
  112. disable_regulator:
  113. regulator_disable(state->vref_reg);
  114. return ret;
  115. }
  116. static int max11100_remove(struct spi_device *spi)
  117. {
  118. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  119. struct max11100_state *state = iio_priv(indio_dev);
  120. iio_device_unregister(indio_dev);
  121. regulator_disable(state->vref_reg);
  122. return 0;
  123. }
  124. static const struct of_device_id max11100_ids[] = {
  125. {.compatible = "maxim,max11100"},
  126. { },
  127. };
  128. MODULE_DEVICE_TABLE(of, max11100_ids);
  129. static struct spi_driver max11100_driver = {
  130. .driver = {
  131. .name = "max11100",
  132. .of_match_table = max11100_ids,
  133. },
  134. .probe = max11100_probe,
  135. .remove = max11100_remove,
  136. };
  137. module_spi_driver(max11100_driver);
  138. MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
  139. MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
  140. MODULE_LICENSE("GPL v2");