cio-dac.c 3.9 KB

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