mcp4922.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * mcp4922.c
  3. *
  4. * Driver for Microchip Digital to Analog Converters.
  5. * Supports MCP4902, MCP4912, and MCP4922.
  6. *
  7. * Copyright (c) 2014 EMAC Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/sysfs.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/bitops.h>
  27. #define MCP4922_NUM_CHANNELS 2
  28. enum mcp4922_supported_device_ids {
  29. ID_MCP4902,
  30. ID_MCP4912,
  31. ID_MCP4922,
  32. };
  33. struct mcp4922_state {
  34. struct spi_device *spi;
  35. unsigned int value[MCP4922_NUM_CHANNELS];
  36. unsigned int vref_mv;
  37. struct regulator *vref_reg;
  38. u8 mosi[2] ____cacheline_aligned;
  39. };
  40. #define MCP4922_CHAN(chan, bits) { \
  41. .type = IIO_VOLTAGE, \
  42. .output = 1, \
  43. .indexed = 1, \
  44. .channel = chan, \
  45. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  46. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  47. .scan_type = { \
  48. .sign = 'u', \
  49. .realbits = (bits), \
  50. .storagebits = 16, \
  51. .shift = 12 - (bits), \
  52. }, \
  53. }
  54. static int mcp4922_spi_write(struct mcp4922_state *state, u8 addr, u32 val)
  55. {
  56. state->mosi[1] = val & 0xff;
  57. state->mosi[0] = (addr == 0) ? 0x00 : 0x80;
  58. state->mosi[0] |= 0x30 | ((val >> 8) & 0x0f);
  59. return spi_write(state->spi, state->mosi, 2);
  60. }
  61. static int mcp4922_read_raw(struct iio_dev *indio_dev,
  62. struct iio_chan_spec const *chan,
  63. int *val,
  64. int *val2,
  65. long mask)
  66. {
  67. struct mcp4922_state *state = iio_priv(indio_dev);
  68. switch (mask) {
  69. case IIO_CHAN_INFO_RAW:
  70. *val = state->value[chan->channel];
  71. return IIO_VAL_INT;
  72. case IIO_CHAN_INFO_SCALE:
  73. *val = state->vref_mv;
  74. *val2 = chan->scan_type.realbits;
  75. return IIO_VAL_FRACTIONAL_LOG2;
  76. default:
  77. return -EINVAL;
  78. }
  79. }
  80. static int mcp4922_write_raw(struct iio_dev *indio_dev,
  81. struct iio_chan_spec const *chan,
  82. int val,
  83. int val2,
  84. long mask)
  85. {
  86. struct mcp4922_state *state = iio_priv(indio_dev);
  87. if (val2 != 0)
  88. return -EINVAL;
  89. switch (mask) {
  90. case IIO_CHAN_INFO_RAW:
  91. if (val > GENMASK(chan->scan_type.realbits-1, 0))
  92. return -EINVAL;
  93. val <<= chan->scan_type.shift;
  94. state->value[chan->channel] = val;
  95. return mcp4922_spi_write(state, chan->channel, val);
  96. default:
  97. return -EINVAL;
  98. }
  99. }
  100. static const struct iio_chan_spec mcp4922_channels[3][MCP4922_NUM_CHANNELS] = {
  101. [ID_MCP4902] = { MCP4922_CHAN(0, 8), MCP4922_CHAN(1, 8) },
  102. [ID_MCP4912] = { MCP4922_CHAN(0, 10), MCP4922_CHAN(1, 10) },
  103. [ID_MCP4922] = { MCP4922_CHAN(0, 12), MCP4922_CHAN(1, 12) },
  104. };
  105. static const struct iio_info mcp4922_info = {
  106. .read_raw = &mcp4922_read_raw,
  107. .write_raw = &mcp4922_write_raw,
  108. .driver_module = THIS_MODULE,
  109. };
  110. static int mcp4922_probe(struct spi_device *spi)
  111. {
  112. struct iio_dev *indio_dev;
  113. struct mcp4922_state *state;
  114. const struct spi_device_id *id;
  115. int ret;
  116. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
  117. if (indio_dev == NULL)
  118. return -ENOMEM;
  119. state = iio_priv(indio_dev);
  120. state->spi = spi;
  121. state->vref_reg = devm_regulator_get(&spi->dev, "vref");
  122. if (IS_ERR(state->vref_reg)) {
  123. dev_err(&spi->dev, "Vref regulator not specified\n");
  124. return PTR_ERR(state->vref_reg);
  125. }
  126. ret = regulator_enable(state->vref_reg);
  127. if (ret) {
  128. dev_err(&spi->dev, "Failed to enable vref regulator: %d\n",
  129. ret);
  130. return ret;
  131. }
  132. ret = regulator_get_voltage(state->vref_reg);
  133. if (ret < 0) {
  134. dev_err(&spi->dev, "Failed to read vref regulator: %d\n",
  135. ret);
  136. goto error_disable_reg;
  137. }
  138. state->vref_mv = ret / 1000;
  139. spi_set_drvdata(spi, indio_dev);
  140. id = spi_get_device_id(spi);
  141. indio_dev->dev.parent = &spi->dev;
  142. indio_dev->info = &mcp4922_info;
  143. indio_dev->modes = INDIO_DIRECT_MODE;
  144. indio_dev->channels = mcp4922_channels[id->driver_data];
  145. indio_dev->num_channels = MCP4922_NUM_CHANNELS;
  146. indio_dev->name = id->name;
  147. ret = iio_device_register(indio_dev);
  148. if (ret) {
  149. dev_err(&spi->dev, "Failed to register iio device: %d\n",
  150. ret);
  151. goto error_disable_reg;
  152. }
  153. return 0;
  154. error_disable_reg:
  155. regulator_disable(state->vref_reg);
  156. return ret;
  157. }
  158. static int mcp4922_remove(struct spi_device *spi)
  159. {
  160. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  161. struct mcp4922_state *state;
  162. iio_device_unregister(indio_dev);
  163. state = iio_priv(indio_dev);
  164. regulator_disable(state->vref_reg);
  165. return 0;
  166. }
  167. static const struct spi_device_id mcp4922_id[] = {
  168. {"mcp4902", ID_MCP4902},
  169. {"mcp4912", ID_MCP4912},
  170. {"mcp4922", ID_MCP4922},
  171. {}
  172. };
  173. MODULE_DEVICE_TABLE(spi, mcp4922_id);
  174. static struct spi_driver mcp4922_driver = {
  175. .driver = {
  176. .name = "mcp4922",
  177. },
  178. .probe = mcp4922_probe,
  179. .remove = mcp4922_remove,
  180. .id_table = mcp4922_id,
  181. };
  182. module_spi_driver(mcp4922_driver);
  183. MODULE_AUTHOR("Michael Welling <mwelling@ieee.org>");
  184. MODULE_DESCRIPTION("Microchip MCP4902, MCP4912, MCP4922 DAC");
  185. MODULE_LICENSE("GPL v2");