cio-dac.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * IIO driver for the Measurement Computing CIO-DAC
  4. * Copyright (C) 2016 William Breathitt Gray
  5. *
  6. * This driver supports the following Measurement Computing devices: CIO-DAC16,
  7. * CIO-DAC06, and PC104-DAC06.
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/device.h>
  11. #include <linux/errno.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/iio/types.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/isa.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #define CIO_DAC_NUM_CHAN 16
  20. #define CIO_DAC_CHAN(chan) { \
  21. .type = IIO_VOLTAGE, \
  22. .channel = chan, \
  23. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  24. .indexed = 1, \
  25. .output = 1 \
  26. }
  27. #define CIO_DAC_EXTENT 32
  28. static unsigned int base[max_num_isa_dev(CIO_DAC_EXTENT)];
  29. static unsigned int num_cio_dac;
  30. module_param_hw_array(base, uint, ioport, &num_cio_dac, 0);
  31. MODULE_PARM_DESC(base, "Measurement Computing CIO-DAC base addresses");
  32. /**
  33. * struct cio_dac_iio - IIO device private data structure
  34. * @chan_out_states: channels' output states
  35. * @base: base port address of the IIO device
  36. */
  37. struct cio_dac_iio {
  38. int chan_out_states[CIO_DAC_NUM_CHAN];
  39. unsigned int base;
  40. };
  41. static int cio_dac_read_raw(struct iio_dev *indio_dev,
  42. struct iio_chan_spec const *chan, int *val, int *val2, long mask)
  43. {
  44. struct cio_dac_iio *const priv = iio_priv(indio_dev);
  45. if (mask != IIO_CHAN_INFO_RAW)
  46. return -EINVAL;
  47. *val = priv->chan_out_states[chan->channel];
  48. return IIO_VAL_INT;
  49. }
  50. static int cio_dac_write_raw(struct iio_dev *indio_dev,
  51. struct iio_chan_spec const *chan, int val, int val2, long mask)
  52. {
  53. struct cio_dac_iio *const priv = iio_priv(indio_dev);
  54. const unsigned int chan_addr_offset = 2 * chan->channel;
  55. if (mask != IIO_CHAN_INFO_RAW)
  56. return -EINVAL;
  57. /* DAC can only accept up to a 16-bit value */
  58. if ((unsigned int)val > 65535)
  59. return -EINVAL;
  60. priv->chan_out_states[chan->channel] = val;
  61. outw(val, priv->base + chan_addr_offset);
  62. return 0;
  63. }
  64. static const struct iio_info cio_dac_info = {
  65. .read_raw = cio_dac_read_raw,
  66. .write_raw = cio_dac_write_raw
  67. };
  68. static const struct iio_chan_spec cio_dac_channels[CIO_DAC_NUM_CHAN] = {
  69. CIO_DAC_CHAN(0), CIO_DAC_CHAN(1), CIO_DAC_CHAN(2), CIO_DAC_CHAN(3),
  70. CIO_DAC_CHAN(4), CIO_DAC_CHAN(5), CIO_DAC_CHAN(6), CIO_DAC_CHAN(7),
  71. CIO_DAC_CHAN(8), CIO_DAC_CHAN(9), CIO_DAC_CHAN(10), CIO_DAC_CHAN(11),
  72. CIO_DAC_CHAN(12), CIO_DAC_CHAN(13), CIO_DAC_CHAN(14), CIO_DAC_CHAN(15)
  73. };
  74. static int cio_dac_probe(struct device *dev, unsigned int id)
  75. {
  76. struct iio_dev *indio_dev;
  77. struct cio_dac_iio *priv;
  78. unsigned int i;
  79. indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
  80. if (!indio_dev)
  81. return -ENOMEM;
  82. if (!devm_request_region(dev, base[id], CIO_DAC_EXTENT,
  83. dev_name(dev))) {
  84. dev_err(dev, "Unable to request port addresses (0x%X-0x%X)\n",
  85. base[id], base[id] + CIO_DAC_EXTENT);
  86. return -EBUSY;
  87. }
  88. indio_dev->info = &cio_dac_info;
  89. indio_dev->modes = INDIO_DIRECT_MODE;
  90. indio_dev->channels = cio_dac_channels;
  91. indio_dev->num_channels = CIO_DAC_NUM_CHAN;
  92. indio_dev->name = dev_name(dev);
  93. priv = iio_priv(indio_dev);
  94. priv->base = base[id];
  95. /* initialize DAC outputs to 0V */
  96. for (i = 0; i < 32; i += 2)
  97. outw(0, base[id] + i);
  98. return devm_iio_device_register(dev, indio_dev);
  99. }
  100. static struct isa_driver cio_dac_driver = {
  101. .probe = cio_dac_probe,
  102. .driver = {
  103. .name = "cio-dac"
  104. }
  105. };
  106. module_isa_driver(cio_dac_driver, num_cio_dac);
  107. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  108. MODULE_DESCRIPTION("Measurement Computing CIO-DAC IIO driver");
  109. MODULE_LICENSE("GPL v2");