ad7606_par.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AD7606 Parallel Interface ADC driver
  4. *
  5. * Copyright 2011 Analog Devices Inc.
  6. */
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/module.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/types.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/iio/iio.h>
  15. #include "ad7606.h"
  16. static int ad7606_par16_read_block(struct device *dev,
  17. int count, void *buf)
  18. {
  19. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  20. struct ad7606_state *st = iio_priv(indio_dev);
  21. /*
  22. * On the parallel interface, the frstdata signal is set to high while
  23. * and after reading the sample of the first channel and low for all
  24. * other channels. This can be used to check that the incoming data is
  25. * correctly aligned. During normal operation the data should never
  26. * become unaligned, but some glitch or electrostatic discharge might
  27. * cause an extra read or clock cycle. Monitoring the frstdata signal
  28. * allows to recover from such failure situations.
  29. */
  30. int num = count;
  31. u16 *_buf = buf;
  32. if (st->gpio_frstdata) {
  33. insw((unsigned long)st->base_address, _buf, 1);
  34. if (!gpiod_get_value(st->gpio_frstdata)) {
  35. ad7606_reset(st);
  36. return -EIO;
  37. }
  38. _buf++;
  39. num--;
  40. }
  41. insw((unsigned long)st->base_address, _buf, num);
  42. return 0;
  43. }
  44. static const struct ad7606_bus_ops ad7606_par16_bops = {
  45. .read_block = ad7606_par16_read_block,
  46. };
  47. static int ad7606_par8_read_block(struct device *dev,
  48. int count, void *buf)
  49. {
  50. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  51. struct ad7606_state *st = iio_priv(indio_dev);
  52. /*
  53. * On the parallel interface, the frstdata signal is set to high while
  54. * and after reading the sample of the first channel and low for all
  55. * other channels. This can be used to check that the incoming data is
  56. * correctly aligned. During normal operation the data should never
  57. * become unaligned, but some glitch or electrostatic discharge might
  58. * cause an extra read or clock cycle. Monitoring the frstdata signal
  59. * allows to recover from such failure situations.
  60. */
  61. int num = count;
  62. u16 *_buf = buf;
  63. if (st->gpio_frstdata) {
  64. insb((unsigned long)st->base_address, _buf, 2);
  65. if (!gpiod_get_value(st->gpio_frstdata)) {
  66. ad7606_reset(st);
  67. return -EIO;
  68. }
  69. _buf++;
  70. num--;
  71. }
  72. insb((unsigned long)st->base_address, _buf, num * 2);
  73. return 0;
  74. }
  75. static const struct ad7606_bus_ops ad7606_par8_bops = {
  76. .read_block = ad7606_par8_read_block,
  77. };
  78. static int ad7606_par_probe(struct platform_device *pdev)
  79. {
  80. const struct platform_device_id *id = platform_get_device_id(pdev);
  81. struct resource *res;
  82. void __iomem *addr;
  83. resource_size_t remap_size;
  84. int irq;
  85. irq = platform_get_irq(pdev, 0);
  86. if (irq < 0)
  87. return irq;
  88. addr = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  89. if (IS_ERR(addr))
  90. return PTR_ERR(addr);
  91. remap_size = resource_size(res);
  92. return ad7606_probe(&pdev->dev, irq, addr,
  93. id->name, id->driver_data,
  94. remap_size > 1 ? &ad7606_par16_bops :
  95. &ad7606_par8_bops);
  96. }
  97. static const struct platform_device_id ad7606_driver_ids[] = {
  98. { .name = "ad7605-4", .driver_data = ID_AD7605_4, },
  99. { .name = "ad7606-4", .driver_data = ID_AD7606_4, },
  100. { .name = "ad7606-6", .driver_data = ID_AD7606_6, },
  101. { .name = "ad7606-8", .driver_data = ID_AD7606_8, },
  102. { }
  103. };
  104. MODULE_DEVICE_TABLE(platform, ad7606_driver_ids);
  105. static const struct of_device_id ad7606_of_match[] = {
  106. { .compatible = "adi,ad7605-4" },
  107. { .compatible = "adi,ad7606-4" },
  108. { .compatible = "adi,ad7606-6" },
  109. { .compatible = "adi,ad7606-8" },
  110. { }
  111. };
  112. MODULE_DEVICE_TABLE(of, ad7606_of_match);
  113. static struct platform_driver ad7606_driver = {
  114. .probe = ad7606_par_probe,
  115. .id_table = ad7606_driver_ids,
  116. .driver = {
  117. .name = "ad7606",
  118. .pm = AD7606_PM_OPS,
  119. .of_match_table = ad7606_of_match,
  120. },
  121. };
  122. module_platform_driver(ad7606_driver);
  123. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  124. MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
  125. MODULE_LICENSE("GPL v2");
  126. MODULE_IMPORT_NS(IIO_AD7606);