max11100.c 4.0 KB

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