ad7606_par.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AD7606 Parallel Interface ADC driver
  4. *
  5. * Copyright 2011 - 2024 Analog Devices Inc.
  6. * Copyright 2024 BayLibre SAS.
  7. */
  8. #include <linux/err.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/io.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/types.h>
  16. #include <linux/iio/backend.h>
  17. #include <linux/iio/iio.h>
  18. #include "ad7606.h"
  19. #include "ad7606_bus_iface.h"
  20. static int ad7606_par_bus_update_scan_mode(struct iio_dev *indio_dev,
  21. const unsigned long *scan_mask)
  22. {
  23. struct ad7606_state *st = iio_priv(indio_dev);
  24. unsigned int c, ret;
  25. for (c = 0; c < indio_dev->num_channels; c++) {
  26. if (test_bit(c, scan_mask))
  27. ret = iio_backend_chan_enable(st->back, c);
  28. else
  29. ret = iio_backend_chan_disable(st->back, c);
  30. if (ret)
  31. return ret;
  32. }
  33. return 0;
  34. }
  35. static int ad7606_par_bus_setup_iio_backend(struct device *dev,
  36. struct iio_dev *indio_dev)
  37. {
  38. struct ad7606_state *st = iio_priv(indio_dev);
  39. unsigned int c;
  40. int ret;
  41. struct iio_backend_data_fmt data = {
  42. .sign_extend = true,
  43. .enable = true,
  44. };
  45. st->back = devm_iio_backend_get(dev, NULL);
  46. if (IS_ERR(st->back))
  47. return PTR_ERR(st->back);
  48. /* If the device is iio_backend powered the PWM is mandatory */
  49. if (!st->cnvst_pwm)
  50. return dev_err_probe(st->dev, -EINVAL,
  51. "A PWM is mandatory when using backend.\n");
  52. ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
  53. if (ret)
  54. return ret;
  55. ret = devm_iio_backend_enable(dev, st->back);
  56. if (ret)
  57. return ret;
  58. for (c = 0; c < indio_dev->num_channels; c++) {
  59. ret = iio_backend_data_format_set(st->back, c, &data);
  60. if (ret)
  61. return ret;
  62. }
  63. return 0;
  64. }
  65. static int ad7606_par_bus_reg_read(struct ad7606_state *st, unsigned int addr)
  66. {
  67. struct ad7606_platform_data *pdata = st->dev->platform_data;
  68. int val, ret;
  69. ret = pdata->bus_reg_read(st->back, addr, &val);
  70. if (ret)
  71. return ret;
  72. return val;
  73. }
  74. static int ad7606_par_bus_reg_write(struct ad7606_state *st, unsigned int addr,
  75. unsigned int val)
  76. {
  77. struct ad7606_platform_data *pdata = st->dev->platform_data;
  78. return pdata->bus_reg_write(st->back, addr, val);
  79. }
  80. static const struct ad7606_bus_ops ad7606_bi_bops = {
  81. .iio_backend_config = ad7606_par_bus_setup_iio_backend,
  82. .update_scan_mode = ad7606_par_bus_update_scan_mode,
  83. .reg_read = ad7606_par_bus_reg_read,
  84. .reg_write = ad7606_par_bus_reg_write,
  85. };
  86. static int ad7606_par16_read_block(struct device *dev,
  87. int count, void *buf)
  88. {
  89. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  90. struct ad7606_state *st = iio_priv(indio_dev);
  91. /*
  92. * On the parallel interface, the frstdata signal is set to high while
  93. * and after reading the sample of the first channel and low for all
  94. * other channels. This can be used to check that the incoming data is
  95. * correctly aligned. During normal operation the data should never
  96. * become unaligned, but some glitch or electrostatic discharge might
  97. * cause an extra read or clock cycle. Monitoring the frstdata signal
  98. * allows to recover from such failure situations.
  99. */
  100. int num = count;
  101. u16 *_buf = buf;
  102. if (st->gpio_frstdata) {
  103. insw((unsigned long)st->base_address, _buf, 1);
  104. if (!gpiod_get_value(st->gpio_frstdata)) {
  105. ad7606_reset(st);
  106. return -EIO;
  107. }
  108. _buf++;
  109. num--;
  110. }
  111. insw((unsigned long)st->base_address, _buf, num);
  112. return 0;
  113. }
  114. static const struct ad7606_bus_ops ad7606_par16_bops = {
  115. .read_block = ad7606_par16_read_block,
  116. };
  117. static int ad7606_par8_read_block(struct device *dev,
  118. int count, void *buf)
  119. {
  120. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  121. struct ad7606_state *st = iio_priv(indio_dev);
  122. /*
  123. * On the parallel interface, the frstdata signal is set to high while
  124. * and after reading the sample of the first channel and low for all
  125. * other channels. This can be used to check that the incoming data is
  126. * correctly aligned. During normal operation the data should never
  127. * become unaligned, but some glitch or electrostatic discharge might
  128. * cause an extra read or clock cycle. Monitoring the frstdata signal
  129. * allows to recover from such failure situations.
  130. */
  131. int num = count;
  132. u16 *_buf = buf;
  133. if (st->gpio_frstdata) {
  134. insb((unsigned long)st->base_address, _buf, 2);
  135. if (!gpiod_get_value(st->gpio_frstdata)) {
  136. ad7606_reset(st);
  137. return -EIO;
  138. }
  139. _buf++;
  140. num--;
  141. }
  142. insb((unsigned long)st->base_address, _buf, num * 2);
  143. return 0;
  144. }
  145. static const struct ad7606_bus_ops ad7606_par8_bops = {
  146. .read_block = ad7606_par8_read_block,
  147. };
  148. static int ad7606_par_probe(struct platform_device *pdev)
  149. {
  150. const struct ad7606_chip_info *chip_info;
  151. const struct platform_device_id *id;
  152. struct resource *res;
  153. void __iomem *addr;
  154. resource_size_t remap_size;
  155. int irq;
  156. /*
  157. * If a firmware node is available (ACPI or DT), platform_device_id is null
  158. * and we must use get_match_data.
  159. */
  160. if (dev_fwnode(&pdev->dev)) {
  161. chip_info = device_get_match_data(&pdev->dev);
  162. if (device_property_present(&pdev->dev, "io-backends"))
  163. /*
  164. * If a backend is available ,call the core probe with backend
  165. * bops, otherwise use the former bops.
  166. */
  167. return ad7606_probe(&pdev->dev, 0, NULL,
  168. chip_info,
  169. &ad7606_bi_bops);
  170. } else {
  171. id = platform_get_device_id(pdev);
  172. chip_info = (const struct ad7606_chip_info *)id->driver_data;
  173. }
  174. irq = platform_get_irq(pdev, 0);
  175. if (irq < 0)
  176. return irq;
  177. addr = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  178. if (IS_ERR(addr))
  179. return PTR_ERR(addr);
  180. remap_size = resource_size(res);
  181. return ad7606_probe(&pdev->dev, irq, addr, chip_info,
  182. remap_size > 1 ? &ad7606_par16_bops :
  183. &ad7606_par8_bops);
  184. }
  185. static const struct platform_device_id ad7606_driver_ids[] = {
  186. { .name = "ad7605-4", .driver_data = (kernel_ulong_t)&ad7605_4_info, },
  187. { .name = "ad7606-4", .driver_data = (kernel_ulong_t)&ad7606_4_info, },
  188. { .name = "ad7606-6", .driver_data = (kernel_ulong_t)&ad7606_6_info, },
  189. { .name = "ad7606-8", .driver_data = (kernel_ulong_t)&ad7606_8_info, },
  190. { .name = "ad7606b", .driver_data = (kernel_ulong_t)&ad7606b_info, },
  191. { .name = "ad7606c-16", .driver_data = (kernel_ulong_t)&ad7606c_16_info },
  192. { .name = "ad7606c-18", .driver_data = (kernel_ulong_t)&ad7606c_18_info },
  193. { .name = "ad7607", .driver_data = (kernel_ulong_t)&ad7607_info, },
  194. { .name = "ad7608", .driver_data = (kernel_ulong_t)&ad7608_info, },
  195. { .name = "ad7609", .driver_data = (kernel_ulong_t)&ad7609_info, },
  196. { }
  197. };
  198. MODULE_DEVICE_TABLE(platform, ad7606_driver_ids);
  199. static const struct of_device_id ad7606_of_match[] = {
  200. { .compatible = "adi,ad7605-4", .data = &ad7605_4_info },
  201. { .compatible = "adi,ad7606-4", .data = &ad7606_4_info },
  202. { .compatible = "adi,ad7606-6", .data = &ad7606_6_info },
  203. { .compatible = "adi,ad7606-8", .data = &ad7606_8_info },
  204. { .compatible = "adi,ad7606b", .data = &ad7606b_info },
  205. { .compatible = "adi,ad7606c-16", .data = &ad7606c_16_info },
  206. { .compatible = "adi,ad7606c-18", .data = &ad7606c_18_info },
  207. { .compatible = "adi,ad7607", .data = &ad7607_info },
  208. { .compatible = "adi,ad7608", .data = &ad7608_info },
  209. { .compatible = "adi,ad7609", .data = &ad7609_info },
  210. { }
  211. };
  212. MODULE_DEVICE_TABLE(of, ad7606_of_match);
  213. static struct platform_driver ad7606_driver = {
  214. .probe = ad7606_par_probe,
  215. .id_table = ad7606_driver_ids,
  216. .driver = {
  217. .name = "ad7606",
  218. .pm = AD7606_PM_OPS,
  219. .of_match_table = ad7606_of_match,
  220. },
  221. };
  222. module_platform_driver(ad7606_driver);
  223. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  224. MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
  225. MODULE_LICENSE("GPL v2");
  226. MODULE_IMPORT_NS("IIO_AD7606");
  227. MODULE_IMPORT_NS("IIO_BACKEND");