ad7405.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Analog Devices AD7405 driver
  4. *
  5. * Copyright 2025 Analog Devices Inc.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/math64.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/property.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/util_macros.h>
  17. #include <linux/iio/backend.h>
  18. #include <linux/iio/iio.h>
  19. static const unsigned int ad7405_dec_rates_range[] = {
  20. 32, 1, 4096,
  21. };
  22. struct ad7405_chip_info {
  23. const char *name;
  24. const unsigned int full_scale_mv;
  25. };
  26. struct ad7405_state {
  27. struct iio_backend *back;
  28. const struct ad7405_chip_info *info;
  29. unsigned int ref_frequency;
  30. unsigned int dec_rate;
  31. };
  32. static int ad7405_set_dec_rate(struct iio_dev *indio_dev,
  33. const struct iio_chan_spec *chan,
  34. unsigned int dec_rate)
  35. {
  36. struct ad7405_state *st = iio_priv(indio_dev);
  37. int ret;
  38. if (dec_rate > 4096 || dec_rate < 32)
  39. return -EINVAL;
  40. if (!iio_device_claim_direct(indio_dev))
  41. return -EBUSY;
  42. ret = iio_backend_oversampling_ratio_set(st->back, chan->scan_index, dec_rate);
  43. iio_device_release_direct(indio_dev);
  44. if (ret < 0)
  45. return ret;
  46. st->dec_rate = dec_rate;
  47. return 0;
  48. }
  49. static int ad7405_read_raw(struct iio_dev *indio_dev,
  50. const struct iio_chan_spec *chan, int *val,
  51. int *val2, long info)
  52. {
  53. struct ad7405_state *st = iio_priv(indio_dev);
  54. switch (info) {
  55. case IIO_CHAN_INFO_SCALE:
  56. *val = st->info->full_scale_mv;
  57. *val2 = indio_dev->channels[0].scan_type.realbits - 1;
  58. return IIO_VAL_FRACTIONAL_LOG2;
  59. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  60. *val = st->dec_rate;
  61. return IIO_VAL_INT;
  62. case IIO_CHAN_INFO_SAMP_FREQ:
  63. *val = DIV_ROUND_CLOSEST_ULL(st->ref_frequency, st->dec_rate);
  64. return IIO_VAL_INT;
  65. case IIO_CHAN_INFO_OFFSET:
  66. *val = -(1 << (indio_dev->channels[0].scan_type.realbits - 1));
  67. return IIO_VAL_INT;
  68. default:
  69. return -EINVAL;
  70. }
  71. }
  72. static int ad7405_write_raw(struct iio_dev *indio_dev,
  73. struct iio_chan_spec const *chan, int val,
  74. int val2, long info)
  75. {
  76. switch (info) {
  77. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  78. if (val < 0)
  79. return -EINVAL;
  80. return ad7405_set_dec_rate(indio_dev, chan, val);
  81. default:
  82. return -EINVAL;
  83. }
  84. }
  85. static int ad7405_read_avail(struct iio_dev *indio_dev,
  86. struct iio_chan_spec const *chan,
  87. const int **vals, int *type, int *length,
  88. long info)
  89. {
  90. switch (info) {
  91. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  92. *vals = ad7405_dec_rates_range;
  93. *type = IIO_VAL_INT;
  94. return IIO_AVAIL_RANGE;
  95. default:
  96. return -EINVAL;
  97. }
  98. }
  99. static const struct iio_info ad7405_iio_info = {
  100. .read_raw = &ad7405_read_raw,
  101. .write_raw = &ad7405_write_raw,
  102. .read_avail = &ad7405_read_avail,
  103. };
  104. static const struct iio_chan_spec ad7405_channel = {
  105. .type = IIO_VOLTAGE,
  106. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
  107. BIT(IIO_CHAN_INFO_OFFSET),
  108. .info_mask_shared_by_all = IIO_CHAN_INFO_SAMP_FREQ |
  109. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  110. .info_mask_shared_by_all_available =
  111. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  112. .indexed = 1,
  113. .channel = 0,
  114. .channel2 = 1,
  115. .differential = 1,
  116. .scan_index = 0,
  117. .scan_type = {
  118. .sign = 'u',
  119. .realbits = 16,
  120. .storagebits = 16,
  121. },
  122. };
  123. static const struct ad7405_chip_info ad7405_chip_info = {
  124. .name = "ad7405",
  125. .full_scale_mv = 320,
  126. };
  127. static const struct ad7405_chip_info adum7701_chip_info = {
  128. .name = "adum7701",
  129. .full_scale_mv = 320,
  130. };
  131. static const struct ad7405_chip_info adum7702_chip_info = {
  132. .name = "adum7702",
  133. .full_scale_mv = 64,
  134. };
  135. static const struct ad7405_chip_info adum7703_chip_info = {
  136. .name = "adum7703",
  137. .full_scale_mv = 320,
  138. };
  139. static const char * const ad7405_power_supplies[] = {
  140. "vdd1", "vdd2",
  141. };
  142. static int ad7405_probe(struct platform_device *pdev)
  143. {
  144. struct device *dev = &pdev->dev;
  145. struct iio_dev *indio_dev;
  146. struct ad7405_state *st;
  147. struct clk *clk;
  148. int ret;
  149. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  150. if (!indio_dev)
  151. return -ENOMEM;
  152. st = iio_priv(indio_dev);
  153. st->info = device_get_match_data(dev);
  154. if (!st->info)
  155. return dev_err_probe(dev, -EINVAL, "no chip info\n");
  156. ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(ad7405_power_supplies),
  157. ad7405_power_supplies);
  158. if (ret)
  159. return dev_err_probe(dev, ret, "failed to get and enable supplies");
  160. clk = devm_clk_get_enabled(dev, NULL);
  161. if (IS_ERR(clk))
  162. return PTR_ERR(clk);
  163. st->ref_frequency = clk_get_rate(clk);
  164. if (!st->ref_frequency)
  165. return -EINVAL;
  166. indio_dev->name = st->info->name;
  167. indio_dev->channels = &ad7405_channel;
  168. indio_dev->num_channels = 1;
  169. indio_dev->info = &ad7405_iio_info;
  170. st->back = devm_iio_backend_get(dev, NULL);
  171. if (IS_ERR(st->back))
  172. return dev_err_probe(dev, PTR_ERR(st->back),
  173. "failed to get IIO backend");
  174. ret = iio_backend_chan_enable(st->back, 0);
  175. if (ret)
  176. return ret;
  177. ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
  178. if (ret)
  179. return ret;
  180. ret = devm_iio_backend_enable(dev, st->back);
  181. if (ret)
  182. return ret;
  183. /*
  184. * Set 256 decimation rate. The default value in the AXI_ADC register
  185. * is 0, so we set the register with a decimation rate value that is
  186. * functional for all parts.
  187. */
  188. ret = ad7405_set_dec_rate(indio_dev, &indio_dev->channels[0], 256);
  189. if (ret)
  190. return ret;
  191. return devm_iio_device_register(dev, indio_dev);
  192. }
  193. static const struct of_device_id ad7405_of_match[] = {
  194. { .compatible = "adi,ad7405", .data = &ad7405_chip_info, },
  195. { .compatible = "adi,adum7701", .data = &adum7701_chip_info, },
  196. { .compatible = "adi,adum7702", .data = &adum7702_chip_info, },
  197. { .compatible = "adi,adum7703", .data = &adum7703_chip_info, },
  198. { }
  199. };
  200. MODULE_DEVICE_TABLE(of, ad7405_of_match);
  201. static struct platform_driver ad7405_driver = {
  202. .driver = {
  203. .name = "ad7405",
  204. .of_match_table = ad7405_of_match,
  205. },
  206. .probe = ad7405_probe,
  207. };
  208. module_platform_driver(ad7405_driver);
  209. MODULE_AUTHOR("Dragos Bogdan <dragos.bogdan@analog.com>");
  210. MODULE_AUTHOR("Pop Ioan Daniel <pop.ioan-daniel@analog.com>");
  211. MODULE_DESCRIPTION("Analog Devices AD7405 driver");
  212. MODULE_LICENSE("GPL");
  213. MODULE_IMPORT_NS("IIO_BACKEND");