max11100.c 3.7 KB

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