max5522.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Maxim MAX5522
  4. * Dual, Ultra-Low-Power 10-Bit, Voltage-Output DACs
  5. *
  6. * Copyright 2022 Timesys Corp.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/regmap.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/slab.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/iio/iio.h>
  17. #define MAX5522_MAX_ADDR 15
  18. #define MAX5522_CTRL_NONE 0
  19. #define MAX5522_CTRL_LOAD_IN_A 9
  20. #define MAX5522_CTRL_LOAD_IN_B 10
  21. #define MAX5522_REG_DATA(x) ((x) + MAX5522_CTRL_LOAD_IN_A)
  22. struct max5522_chip_info {
  23. const char *name;
  24. const struct iio_chan_spec *channels;
  25. unsigned int num_channels;
  26. };
  27. struct max5522_state {
  28. struct regmap *regmap;
  29. const struct max5522_chip_info *chip_info;
  30. unsigned short dac_cache[2];
  31. struct regulator *vrefin_reg;
  32. };
  33. #define MAX5522_CHANNEL(chan) { \
  34. .type = IIO_VOLTAGE, \
  35. .indexed = 1, \
  36. .output = 1, \
  37. .channel = chan, \
  38. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  39. BIT(IIO_CHAN_INFO_SCALE), \
  40. .scan_type = { \
  41. .sign = 'u', \
  42. .realbits = 10, \
  43. .storagebits = 16, \
  44. .shift = 2, \
  45. } \
  46. }
  47. static const struct iio_chan_spec max5522_channels[] = {
  48. MAX5522_CHANNEL(0),
  49. MAX5522_CHANNEL(1),
  50. };
  51. enum max5522_type {
  52. ID_MAX5522,
  53. };
  54. static const struct max5522_chip_info max5522_chip_info_tbl[] = {
  55. [ID_MAX5522] = {
  56. .name = "max5522",
  57. .channels = max5522_channels,
  58. .num_channels = 2,
  59. },
  60. };
  61. static inline int max5522_info_to_reg(struct iio_chan_spec const *chan)
  62. {
  63. return MAX5522_REG_DATA(chan->channel);
  64. }
  65. static int max5522_read_raw(struct iio_dev *indio_dev,
  66. struct iio_chan_spec const *chan,
  67. int *val, int *val2, long info)
  68. {
  69. struct max5522_state *state = iio_priv(indio_dev);
  70. int ret;
  71. switch (info) {
  72. case IIO_CHAN_INFO_RAW:
  73. *val = state->dac_cache[chan->channel];
  74. return IIO_VAL_INT;
  75. case IIO_CHAN_INFO_SCALE:
  76. ret = regulator_get_voltage(state->vrefin_reg);
  77. if (ret < 0)
  78. return -EINVAL;
  79. *val = ret / 1000;
  80. *val2 = 10;
  81. return IIO_VAL_FRACTIONAL_LOG2;
  82. default:
  83. return -EINVAL;
  84. }
  85. return -EINVAL;
  86. }
  87. static int max5522_write_raw(struct iio_dev *indio_dev,
  88. struct iio_chan_spec const *chan,
  89. int val, int val2, long info)
  90. {
  91. struct max5522_state *state = iio_priv(indio_dev);
  92. int rval;
  93. if (val > 1023 || val < 0)
  94. return -EINVAL;
  95. rval = regmap_write(state->regmap, max5522_info_to_reg(chan),
  96. val << chan->scan_type.shift);
  97. if (rval < 0)
  98. return rval;
  99. state->dac_cache[chan->channel] = val;
  100. return 0;
  101. }
  102. static const struct iio_info max5522_info = {
  103. .read_raw = max5522_read_raw,
  104. .write_raw = max5522_write_raw,
  105. };
  106. static const struct regmap_config max5522_regmap_config = {
  107. .reg_bits = 4,
  108. .val_bits = 12,
  109. .max_register = MAX5522_MAX_ADDR,
  110. };
  111. static int max5522_spi_probe(struct spi_device *spi)
  112. {
  113. struct iio_dev *indio_dev;
  114. struct max5522_state *state;
  115. int ret;
  116. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
  117. if (indio_dev == NULL) {
  118. dev_err(&spi->dev, "failed to allocate iio device\n");
  119. return -ENOMEM;
  120. }
  121. state = iio_priv(indio_dev);
  122. state->chip_info = spi_get_device_match_data(spi);
  123. if (!state->chip_info)
  124. return -EINVAL;
  125. state->vrefin_reg = devm_regulator_get(&spi->dev, "vrefin");
  126. if (IS_ERR(state->vrefin_reg))
  127. return dev_err_probe(&spi->dev, PTR_ERR(state->vrefin_reg),
  128. "Vrefin regulator not specified\n");
  129. ret = regulator_enable(state->vrefin_reg);
  130. if (ret) {
  131. return dev_err_probe(&spi->dev, ret,
  132. "Failed to enable vref regulators\n");
  133. }
  134. state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config);
  135. if (IS_ERR(state->regmap))
  136. return PTR_ERR(state->regmap);
  137. indio_dev->info = &max5522_info;
  138. indio_dev->modes = INDIO_DIRECT_MODE;
  139. indio_dev->channels = max5522_channels;
  140. indio_dev->num_channels = ARRAY_SIZE(max5522_channels);
  141. indio_dev->name = max5522_chip_info_tbl[ID_MAX5522].name;
  142. return devm_iio_device_register(&spi->dev, indio_dev);
  143. }
  144. static const struct spi_device_id max5522_ids[] = {
  145. { "max5522", (kernel_ulong_t)&max5522_chip_info_tbl[ID_MAX5522] },
  146. {}
  147. };
  148. MODULE_DEVICE_TABLE(spi, max5522_ids);
  149. static const struct of_device_id max5522_of_match[] = {
  150. {
  151. .compatible = "maxim,max5522",
  152. .data = &max5522_chip_info_tbl[ID_MAX5522],
  153. },
  154. {}
  155. };
  156. MODULE_DEVICE_TABLE(of, max5522_of_match);
  157. static struct spi_driver max5522_spi_driver = {
  158. .driver = {
  159. .name = "max5522",
  160. .of_match_table = max5522_of_match,
  161. },
  162. .probe = max5522_spi_probe,
  163. .id_table = max5522_ids,
  164. };
  165. module_spi_driver(max5522_spi_driver);
  166. MODULE_AUTHOR("Angelo Dureghello <angelo.dureghello@timesys.com");
  167. MODULE_DESCRIPTION("MAX5522 DAC driver");
  168. MODULE_LICENSE("GPL");