men_z188_adc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * MEN 16z188 Analog to Digial Converter
  3. *
  4. * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
  5. * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; version 2 of the License.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/mcb.h>
  14. #include <linux/io.h>
  15. #include <linux/iio/iio.h>
  16. #define Z188_ADC_MAX_CHAN 8
  17. #define Z188_ADC_GAIN 0x0700000
  18. #define Z188_MODE_VOLTAGE BIT(27)
  19. #define Z188_CFG_AUTO 0x1
  20. #define Z188_CTRL_REG 0x40
  21. #define ADC_DATA(x) (((x) >> 2) & 0x7ffffc)
  22. #define ADC_OVR(x) ((x) & 0x1)
  23. struct z188_adc {
  24. struct resource *mem;
  25. void __iomem *base;
  26. };
  27. #define Z188_ADC_CHANNEL(idx) { \
  28. .type = IIO_VOLTAGE, \
  29. .indexed = 1, \
  30. .channel = (idx), \
  31. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  32. }
  33. static const struct iio_chan_spec z188_adc_iio_channels[] = {
  34. Z188_ADC_CHANNEL(0),
  35. Z188_ADC_CHANNEL(1),
  36. Z188_ADC_CHANNEL(2),
  37. Z188_ADC_CHANNEL(3),
  38. Z188_ADC_CHANNEL(4),
  39. Z188_ADC_CHANNEL(5),
  40. Z188_ADC_CHANNEL(6),
  41. Z188_ADC_CHANNEL(7),
  42. };
  43. static int z188_iio_read_raw(struct iio_dev *iio_dev,
  44. struct iio_chan_spec const *chan,
  45. int *val,
  46. int *val2,
  47. long info)
  48. {
  49. struct z188_adc *adc = iio_priv(iio_dev);
  50. int ret;
  51. u16 tmp;
  52. switch (info) {
  53. case IIO_CHAN_INFO_RAW:
  54. tmp = readw(adc->base + chan->channel * 4);
  55. if (ADC_OVR(tmp)) {
  56. dev_info(&iio_dev->dev,
  57. "Oversampling error on ADC channel %d\n",
  58. chan->channel);
  59. return -EIO;
  60. }
  61. *val = ADC_DATA(tmp);
  62. ret = IIO_VAL_INT;
  63. break;
  64. default:
  65. ret = -EINVAL;
  66. break;
  67. }
  68. return ret;
  69. }
  70. static const struct iio_info z188_adc_info = {
  71. .read_raw = &z188_iio_read_raw,
  72. .driver_module = THIS_MODULE,
  73. };
  74. static void men_z188_config_channels(void __iomem *addr)
  75. {
  76. int i;
  77. u32 cfg;
  78. u32 ctl;
  79. ctl = readl(addr + Z188_CTRL_REG);
  80. ctl |= Z188_CFG_AUTO;
  81. writel(ctl, addr + Z188_CTRL_REG);
  82. for (i = 0; i < Z188_ADC_MAX_CHAN; i++) {
  83. cfg = readl(addr + i);
  84. cfg &= ~Z188_ADC_GAIN;
  85. cfg |= Z188_MODE_VOLTAGE;
  86. writel(cfg, addr + i);
  87. }
  88. }
  89. static int men_z188_probe(struct mcb_device *dev,
  90. const struct mcb_device_id *id)
  91. {
  92. struct z188_adc *adc;
  93. struct iio_dev *indio_dev;
  94. struct resource *mem;
  95. indio_dev = devm_iio_device_alloc(&dev->dev, sizeof(struct z188_adc));
  96. if (!indio_dev)
  97. return -ENOMEM;
  98. adc = iio_priv(indio_dev);
  99. indio_dev->name = "z188-adc";
  100. indio_dev->dev.parent = &dev->dev;
  101. indio_dev->info = &z188_adc_info;
  102. indio_dev->modes = INDIO_DIRECT_MODE;
  103. indio_dev->channels = z188_adc_iio_channels;
  104. indio_dev->num_channels = ARRAY_SIZE(z188_adc_iio_channels);
  105. mem = mcb_request_mem(dev, "z188-adc");
  106. if (IS_ERR(mem))
  107. return PTR_ERR(mem);
  108. adc->base = ioremap(mem->start, resource_size(mem));
  109. if (adc->base == NULL)
  110. goto err;
  111. men_z188_config_channels(adc->base);
  112. adc->mem = mem;
  113. mcb_set_drvdata(dev, indio_dev);
  114. return iio_device_register(indio_dev);
  115. err:
  116. mcb_release_mem(mem);
  117. return -ENXIO;
  118. }
  119. static void men_z188_remove(struct mcb_device *dev)
  120. {
  121. struct iio_dev *indio_dev = mcb_get_drvdata(dev);
  122. struct z188_adc *adc = iio_priv(indio_dev);
  123. iio_device_unregister(indio_dev);
  124. iounmap(adc->base);
  125. mcb_release_mem(adc->mem);
  126. }
  127. static const struct mcb_device_id men_z188_ids[] = {
  128. { .device = 0xbc },
  129. { }
  130. };
  131. MODULE_DEVICE_TABLE(mcb, men_z188_ids);
  132. static struct mcb_driver men_z188_driver = {
  133. .driver = {
  134. .name = "z188-adc",
  135. .owner = THIS_MODULE,
  136. },
  137. .probe = men_z188_probe,
  138. .remove = men_z188_remove,
  139. .id_table = men_z188_ids,
  140. };
  141. module_mcb_driver(men_z188_driver);
  142. MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
  143. MODULE_LICENSE("GPL");
  144. MODULE_DESCRIPTION("IIO ADC driver for MEN 16z188 ADC Core");
  145. MODULE_ALIAS("mcb:16z188");