ad7625.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. // SPDX-License-Identifier: (GPL-2.0-only)
  2. /*
  3. * Analog Devices Inc. AD7625 ADC driver
  4. *
  5. * Copyright 2024 Analog Devices Inc.
  6. * Copyright 2024 BayLibre, SAS
  7. *
  8. * Note that this driver requires the AXI ADC IP block configured for
  9. * LVDS to function. See Documentation/iio/ad7625.rst for more
  10. * information.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/iio/backend.h>
  17. #include <linux/iio/iio.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pwm.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/units.h>
  25. #define AD7625_INTERNAL_REF_MV 4096
  26. #define AD7960_MAX_NBW_FREQ (2 * MEGA)
  27. struct ad7625_timing_spec {
  28. /* Max conversion high time (t_{CNVH}). */
  29. unsigned int conv_high_ns;
  30. /* Max conversion to MSB delay (t_{MSB}). */
  31. unsigned int conv_msb_ns;
  32. };
  33. struct ad7625_chip_info {
  34. const char *name;
  35. const unsigned int max_sample_freq_hz;
  36. const struct ad7625_timing_spec *timing_spec;
  37. const struct iio_chan_spec chan_spec;
  38. const bool has_power_down_state;
  39. const bool has_bandwidth_control;
  40. const bool has_internal_vref;
  41. };
  42. /* AD7625_CHAN_SPEC - Define a chan spec structure for a specific chip */
  43. #define AD7625_CHAN_SPEC(_bits) { \
  44. .type = IIO_VOLTAGE, \
  45. .indexed = 1, \
  46. .differential = 1, \
  47. .channel = 0, \
  48. .channel2 = 1, \
  49. .info_mask_separate = BIT(IIO_CHAN_INFO_SCALE), \
  50. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  51. .scan_index = 0, \
  52. .scan_type.sign = 's', \
  53. .scan_type.storagebits = (_bits) > 16 ? 32 : 16, \
  54. .scan_type.realbits = (_bits), \
  55. }
  56. struct ad7625_state {
  57. const struct ad7625_chip_info *info;
  58. struct iio_backend *back;
  59. /* rate of the clock gated by the "clk_gate" PWM */
  60. u32 ref_clk_rate_hz;
  61. /* PWM burst signal for transferring acquired data to the host */
  62. struct pwm_device *clk_gate_pwm;
  63. /*
  64. * PWM control signal for initiating data conversion. Analog
  65. * inputs are sampled beginning on this signal's rising edge.
  66. */
  67. struct pwm_device *cnv_pwm;
  68. /*
  69. * Waveforms containing the last-requested and rounded
  70. * properties for the clk_gate and cnv PWMs
  71. */
  72. struct pwm_waveform clk_gate_wf;
  73. struct pwm_waveform cnv_wf;
  74. unsigned int vref_mv;
  75. u32 sampling_freq_hz;
  76. /*
  77. * Optional GPIOs for controlling device state. EN0 and EN1
  78. * determine voltage reference configuration and on/off state.
  79. * EN2 controls the device -3dB bandwidth (and by extension, max
  80. * sample rate). EN3 controls the VCM reference output. EN2 and
  81. * EN3 are only present for the AD796x devices.
  82. */
  83. struct gpio_desc *en_gpios[4];
  84. bool can_power_down;
  85. bool can_refin;
  86. bool can_ref_4v096;
  87. /*
  88. * Indicate whether the bandwidth can be narrow (9MHz).
  89. * When true, device sample rate must also be < 2MSPS.
  90. */
  91. bool can_narrow_bandwidth;
  92. /* Indicate whether the bandwidth can be wide (28MHz). */
  93. bool can_wide_bandwidth;
  94. bool can_ref_5v;
  95. bool can_snooze;
  96. bool can_test_pattern;
  97. /* Indicate whether there is a REFIN supply connected */
  98. bool have_refin;
  99. };
  100. static const struct ad7625_timing_spec ad7625_timing_spec = {
  101. .conv_high_ns = 40,
  102. .conv_msb_ns = 145,
  103. };
  104. static const struct ad7625_timing_spec ad7626_timing_spec = {
  105. .conv_high_ns = 40,
  106. .conv_msb_ns = 80,
  107. };
  108. /*
  109. * conv_msb_ns is set to 0 instead of the datasheet maximum of 200ns to
  110. * avoid exceeding the minimum conversion time, i.e. it is effectively
  111. * modulo 200 and offset by a full period. Values greater than or equal
  112. * to the period would be rejected by the PWM API.
  113. */
  114. static const struct ad7625_timing_spec ad7960_timing_spec = {
  115. .conv_high_ns = 80,
  116. .conv_msb_ns = 0,
  117. };
  118. static const struct ad7625_chip_info ad7625_chip_info = {
  119. .name = "ad7625",
  120. .max_sample_freq_hz = 6 * MEGA,
  121. .timing_spec = &ad7625_timing_spec,
  122. .chan_spec = AD7625_CHAN_SPEC(16),
  123. .has_power_down_state = false,
  124. .has_bandwidth_control = false,
  125. .has_internal_vref = true,
  126. };
  127. static const struct ad7625_chip_info ad7626_chip_info = {
  128. .name = "ad7626",
  129. .max_sample_freq_hz = 10 * MEGA,
  130. .timing_spec = &ad7626_timing_spec,
  131. .chan_spec = AD7625_CHAN_SPEC(16),
  132. .has_power_down_state = true,
  133. .has_bandwidth_control = false,
  134. .has_internal_vref = true,
  135. };
  136. static const struct ad7625_chip_info ad7960_chip_info = {
  137. .name = "ad7960",
  138. .max_sample_freq_hz = 5 * MEGA,
  139. .timing_spec = &ad7960_timing_spec,
  140. .chan_spec = AD7625_CHAN_SPEC(18),
  141. .has_power_down_state = true,
  142. .has_bandwidth_control = true,
  143. .has_internal_vref = false,
  144. };
  145. static const struct ad7625_chip_info ad7961_chip_info = {
  146. .name = "ad7961",
  147. .max_sample_freq_hz = 5 * MEGA,
  148. .timing_spec = &ad7960_timing_spec,
  149. .chan_spec = AD7625_CHAN_SPEC(16),
  150. .has_power_down_state = true,
  151. .has_bandwidth_control = true,
  152. .has_internal_vref = false,
  153. };
  154. enum ad7960_mode {
  155. AD7960_MODE_POWER_DOWN,
  156. AD7960_MODE_SNOOZE,
  157. AD7960_MODE_NARROW_BANDWIDTH,
  158. AD7960_MODE_WIDE_BANDWIDTH,
  159. AD7960_MODE_TEST_PATTERN,
  160. };
  161. static int ad7625_set_sampling_freq(struct ad7625_state *st, u32 freq)
  162. {
  163. u32 target;
  164. struct pwm_waveform clk_gate_wf = { }, cnv_wf = { };
  165. int ret;
  166. target = DIV_ROUND_UP(NSEC_PER_SEC, freq);
  167. cnv_wf.period_length_ns = clamp(target, 100, 10 * KILO);
  168. /*
  169. * Use the maximum conversion time t_CNVH from the datasheet as
  170. * the duty_cycle for ref_clk, cnv, and clk_gate
  171. */
  172. cnv_wf.duty_length_ns = st->info->timing_spec->conv_high_ns;
  173. ret = pwm_round_waveform_might_sleep(st->cnv_pwm, &cnv_wf);
  174. if (ret)
  175. return ret;
  176. /*
  177. * Set up the burst signal for transferring data. period and
  178. * offset should mirror the CNV signal
  179. */
  180. clk_gate_wf.period_length_ns = cnv_wf.period_length_ns;
  181. clk_gate_wf.duty_length_ns = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC *
  182. st->info->chan_spec.scan_type.realbits,
  183. st->ref_clk_rate_hz);
  184. /* max t_MSB from datasheet */
  185. clk_gate_wf.duty_offset_ns = st->info->timing_spec->conv_msb_ns;
  186. ret = pwm_round_waveform_might_sleep(st->clk_gate_pwm, &clk_gate_wf);
  187. if (ret)
  188. return ret;
  189. st->cnv_wf = cnv_wf;
  190. st->clk_gate_wf = clk_gate_wf;
  191. /* TODO: Add a rounding API for PWMs that can simplify this */
  192. target = DIV_ROUND_CLOSEST(st->ref_clk_rate_hz, freq);
  193. st->sampling_freq_hz = DIV_ROUND_CLOSEST(st->ref_clk_rate_hz,
  194. target);
  195. return 0;
  196. }
  197. static int ad7625_read_raw(struct iio_dev *indio_dev,
  198. const struct iio_chan_spec *chan,
  199. int *val, int *val2, long info)
  200. {
  201. struct ad7625_state *st = iio_priv(indio_dev);
  202. switch (info) {
  203. case IIO_CHAN_INFO_SAMP_FREQ:
  204. *val = st->sampling_freq_hz;
  205. return IIO_VAL_INT;
  206. case IIO_CHAN_INFO_SCALE:
  207. *val = st->vref_mv;
  208. *val2 = chan->scan_type.realbits - 1;
  209. return IIO_VAL_FRACTIONAL_LOG2;
  210. default:
  211. return -EINVAL;
  212. }
  213. }
  214. static int ad7625_write_raw(struct iio_dev *indio_dev,
  215. struct iio_chan_spec const *chan,
  216. int val, int val2, long info)
  217. {
  218. struct ad7625_state *st = iio_priv(indio_dev);
  219. int ret;
  220. switch (info) {
  221. case IIO_CHAN_INFO_SAMP_FREQ:
  222. if (!iio_device_claim_direct(indio_dev))
  223. return -EBUSY;
  224. ret = ad7625_set_sampling_freq(st, val);
  225. iio_device_release_direct(indio_dev);
  226. return ret;
  227. default:
  228. return -EINVAL;
  229. }
  230. }
  231. static int ad7625_parse_mode(struct device *dev, struct ad7625_state *st,
  232. int num_gpios)
  233. {
  234. bool en_always_on[4], en_always_off[4];
  235. bool en_may_be_on[4], en_may_be_off[4];
  236. char en_gpio_buf[4];
  237. char always_on_buf[18];
  238. int i;
  239. for (i = 0; i < num_gpios; i++) {
  240. snprintf(en_gpio_buf, sizeof(en_gpio_buf), "en%d", i);
  241. snprintf(always_on_buf, sizeof(always_on_buf),
  242. "adi,en%d-always-on", i);
  243. /* Set the device to 0b0000 (power-down mode) by default */
  244. st->en_gpios[i] = devm_gpiod_get_optional(dev, en_gpio_buf,
  245. GPIOD_OUT_LOW);
  246. if (IS_ERR(st->en_gpios[i]))
  247. return dev_err_probe(dev, PTR_ERR(st->en_gpios[i]),
  248. "failed to get EN%d GPIO\n", i);
  249. en_always_on[i] = device_property_read_bool(dev, always_on_buf);
  250. if (st->en_gpios[i] && en_always_on[i])
  251. return dev_err_probe(dev, -EINVAL,
  252. "cannot have adi,en%d-always-on and en%d-gpios\n", i, i);
  253. en_may_be_off[i] = !en_always_on[i];
  254. en_may_be_on[i] = en_always_on[i] || st->en_gpios[i];
  255. en_always_off[i] = !en_always_on[i] && !st->en_gpios[i];
  256. }
  257. /*
  258. * Power down is mode 0bXX00, but not all devices have a valid
  259. * power down state.
  260. */
  261. st->can_power_down = en_may_be_off[1] && en_may_be_off[0] &&
  262. st->info->has_power_down_state;
  263. /*
  264. * The REFIN pin can take a 1.2V (AD762x) or 2.048V (AD796x)
  265. * external reference when the mode is 0bXX01.
  266. */
  267. st->can_refin = en_may_be_off[1] && en_may_be_on[0];
  268. /* 4.096V can be applied to REF when the EN mode is 0bXX10. */
  269. st->can_ref_4v096 = en_may_be_on[1] && en_may_be_off[0];
  270. /* Avoid AD796x-specific setup if the part is an AD762x */
  271. if (num_gpios == 2)
  272. return 0;
  273. /* mode 0b1100 (AD796x) is invalid */
  274. if (en_always_on[3] && en_always_on[2] &&
  275. en_always_off[1] && en_always_off[0])
  276. return dev_err_probe(dev, -EINVAL,
  277. "EN GPIOs set to invalid mode 0b1100\n");
  278. /*
  279. * 5V can be applied to the AD796x REF pin when the EN mode is
  280. * the same (0bX001 or 0bX101) as for can_refin, and REFIN is
  281. * 0V.
  282. */
  283. st->can_ref_5v = st->can_refin;
  284. /*
  285. * Bandwidth (AD796x) is controlled solely by EN2. If it's
  286. * specified and not hard-wired, then we can configure it to
  287. * change the bandwidth between 28MHz and 9MHz.
  288. */
  289. st->can_narrow_bandwidth = en_may_be_on[2];
  290. /* Wide bandwidth mode is possible if EN2 can be 0. */
  291. st->can_wide_bandwidth = en_may_be_off[2];
  292. /* Snooze mode (AD796x) is 0bXX11 when REFIN = 0V. */
  293. st->can_snooze = en_may_be_on[1] && en_may_be_on[0];
  294. /* Test pattern mode (AD796x) is 0b0100. */
  295. st->can_test_pattern = en_may_be_off[3] && en_may_be_on[2] &&
  296. en_may_be_off[1] && en_may_be_off[0];
  297. return 0;
  298. }
  299. /* Set EN1 and EN0 based on reference voltage source */
  300. static void ad7625_set_en_gpios_for_vref(struct ad7625_state *st,
  301. bool have_refin, int ref_mv)
  302. {
  303. if (have_refin || ref_mv == 5000) {
  304. gpiod_set_value_cansleep(st->en_gpios[1], 0);
  305. gpiod_set_value_cansleep(st->en_gpios[0], 1);
  306. } else if (ref_mv == 4096) {
  307. gpiod_set_value_cansleep(st->en_gpios[1], 1);
  308. gpiod_set_value_cansleep(st->en_gpios[0], 0);
  309. } else {
  310. /*
  311. * Unreachable by AD796x, since the driver will error if
  312. * neither REF nor REFIN is provided
  313. */
  314. gpiod_set_value_cansleep(st->en_gpios[1], 1);
  315. gpiod_set_value_cansleep(st->en_gpios[0], 1);
  316. }
  317. }
  318. static int ad7960_set_mode(struct ad7625_state *st, enum ad7960_mode mode,
  319. bool have_refin, int ref_mv)
  320. {
  321. switch (mode) {
  322. case AD7960_MODE_POWER_DOWN:
  323. if (!st->can_power_down)
  324. return -EINVAL;
  325. gpiod_set_value_cansleep(st->en_gpios[2], 0);
  326. gpiod_set_value_cansleep(st->en_gpios[1], 0);
  327. gpiod_set_value_cansleep(st->en_gpios[0], 0);
  328. return 0;
  329. case AD7960_MODE_SNOOZE:
  330. if (!st->can_snooze)
  331. return -EINVAL;
  332. gpiod_set_value_cansleep(st->en_gpios[1], 1);
  333. gpiod_set_value_cansleep(st->en_gpios[0], 1);
  334. return 0;
  335. case AD7960_MODE_NARROW_BANDWIDTH:
  336. if (!st->can_narrow_bandwidth)
  337. return -EINVAL;
  338. gpiod_set_value_cansleep(st->en_gpios[2], 1);
  339. ad7625_set_en_gpios_for_vref(st, have_refin, ref_mv);
  340. return 0;
  341. case AD7960_MODE_WIDE_BANDWIDTH:
  342. if (!st->can_wide_bandwidth)
  343. return -EINVAL;
  344. gpiod_set_value_cansleep(st->en_gpios[2], 0);
  345. ad7625_set_en_gpios_for_vref(st, have_refin, ref_mv);
  346. return 0;
  347. case AD7960_MODE_TEST_PATTERN:
  348. if (!st->can_test_pattern)
  349. return -EINVAL;
  350. gpiod_set_value_cansleep(st->en_gpios[3], 0);
  351. gpiod_set_value_cansleep(st->en_gpios[2], 1);
  352. gpiod_set_value_cansleep(st->en_gpios[1], 0);
  353. gpiod_set_value_cansleep(st->en_gpios[0], 0);
  354. return 0;
  355. default:
  356. return -EINVAL;
  357. }
  358. }
  359. static int ad7625_buffer_preenable(struct iio_dev *indio_dev)
  360. {
  361. struct ad7625_state *st = iio_priv(indio_dev);
  362. int ret;
  363. ret = pwm_set_waveform_might_sleep(st->cnv_pwm, &st->cnv_wf, false);
  364. if (ret)
  365. return ret;
  366. ret = pwm_set_waveform_might_sleep(st->clk_gate_pwm,
  367. &st->clk_gate_wf, false);
  368. if (ret) {
  369. /* Disable cnv PWM if clk_gate setup failed */
  370. pwm_disable(st->cnv_pwm);
  371. return ret;
  372. }
  373. return 0;
  374. }
  375. static int ad7625_buffer_postdisable(struct iio_dev *indio_dev)
  376. {
  377. struct ad7625_state *st = iio_priv(indio_dev);
  378. pwm_disable(st->clk_gate_pwm);
  379. pwm_disable(st->cnv_pwm);
  380. return 0;
  381. }
  382. static const struct iio_info ad7625_info = {
  383. .read_raw = ad7625_read_raw,
  384. .write_raw = ad7625_write_raw,
  385. };
  386. static const struct iio_buffer_setup_ops ad7625_buffer_setup_ops = {
  387. .preenable = &ad7625_buffer_preenable,
  388. .postdisable = &ad7625_buffer_postdisable,
  389. };
  390. static int devm_ad7625_pwm_get(struct device *dev,
  391. struct ad7625_state *st)
  392. {
  393. struct clk *ref_clk;
  394. u32 ref_clk_rate_hz;
  395. st->cnv_pwm = devm_pwm_get(dev, "cnv");
  396. if (IS_ERR(st->cnv_pwm))
  397. return dev_err_probe(dev, PTR_ERR(st->cnv_pwm),
  398. "failed to get cnv pwm\n");
  399. /* Preemptively disable the PWM in case it was enabled at boot */
  400. pwm_disable(st->cnv_pwm);
  401. st->clk_gate_pwm = devm_pwm_get(dev, "clk_gate");
  402. if (IS_ERR(st->clk_gate_pwm))
  403. return dev_err_probe(dev, PTR_ERR(st->clk_gate_pwm),
  404. "failed to get clk_gate pwm\n");
  405. /* Preemptively disable the PWM in case it was enabled at boot */
  406. pwm_disable(st->clk_gate_pwm);
  407. ref_clk = devm_clk_get_enabled(dev, NULL);
  408. if (IS_ERR(ref_clk))
  409. return dev_err_probe(dev, PTR_ERR(ref_clk),
  410. "failed to get ref_clk\n");
  411. ref_clk_rate_hz = clk_get_rate(ref_clk);
  412. if (!ref_clk_rate_hz)
  413. return dev_err_probe(dev, -EINVAL,
  414. "failed to get ref_clk rate\n");
  415. st->ref_clk_rate_hz = ref_clk_rate_hz;
  416. return 0;
  417. }
  418. /*
  419. * There are three required input voltages for each device, plus two
  420. * conditionally-optional (depending on part) REF and REFIN voltages
  421. * where their validity depends upon the EN pin configuration.
  422. *
  423. * Power-up info for the device says to bring up vio, then vdd2, then
  424. * vdd1, so list them in that order in the regulator_names array.
  425. *
  426. * The reference voltage source is determined like so:
  427. * - internal reference: neither REF or REFIN is connected (invalid for
  428. * AD796x)
  429. * - internal buffer, external reference: REF not connected, REFIN
  430. * connected
  431. * - external reference: REF connected, REFIN not connected
  432. */
  433. static int devm_ad7625_regulator_setup(struct device *dev,
  434. struct ad7625_state *st)
  435. {
  436. static const char * const regulator_names[] = { "vio", "vdd2", "vdd1" };
  437. int ret, ref_mv;
  438. ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulator_names),
  439. regulator_names);
  440. if (ret)
  441. return ret;
  442. ret = devm_regulator_get_enable_read_voltage(dev, "ref");
  443. if (ret < 0 && ret != -ENODEV)
  444. return dev_err_probe(dev, ret, "failed to get REF voltage\n");
  445. ref_mv = ret == -ENODEV ? 0 : ret / 1000;
  446. ret = devm_regulator_get_enable_optional(dev, "refin");
  447. if (ret < 0 && ret != -ENODEV)
  448. return dev_err_probe(dev, ret, "failed to get REFIN voltage\n");
  449. st->have_refin = ret != -ENODEV;
  450. if (st->have_refin && !st->can_refin)
  451. return dev_err_probe(dev, -EINVAL,
  452. "REFIN provided in unsupported mode\n");
  453. if (!st->info->has_internal_vref && !st->have_refin && !ref_mv)
  454. return dev_err_probe(dev, -EINVAL,
  455. "Need either REFIN or REF\n");
  456. if (st->have_refin && ref_mv)
  457. return dev_err_probe(dev, -EINVAL,
  458. "cannot have both REFIN and REF supplies\n");
  459. if (ref_mv == 4096 && !st->can_ref_4v096)
  460. return dev_err_probe(dev, -EINVAL,
  461. "REF is 4.096V in unsupported mode\n");
  462. if (ref_mv == 5000 && !st->can_ref_5v)
  463. return dev_err_probe(dev, -EINVAL,
  464. "REF is 5V in unsupported mode\n");
  465. st->vref_mv = ref_mv ?: AD7625_INTERNAL_REF_MV;
  466. return 0;
  467. }
  468. static int ad7625_probe(struct platform_device *pdev)
  469. {
  470. struct device *dev = &pdev->dev;
  471. struct iio_dev *indio_dev;
  472. struct ad7625_state *st;
  473. int ret;
  474. u32 default_sample_freq;
  475. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  476. if (!indio_dev)
  477. return -ENOMEM;
  478. st = iio_priv(indio_dev);
  479. st->info = device_get_match_data(dev);
  480. if (!st->info)
  481. return dev_err_probe(dev, -EINVAL, "no chip info\n");
  482. if (device_property_read_bool(dev, "adi,no-dco"))
  483. return dev_err_probe(dev, -EINVAL,
  484. "self-clocked mode not supported\n");
  485. if (st->info->has_bandwidth_control)
  486. ret = ad7625_parse_mode(dev, st, 4);
  487. else
  488. ret = ad7625_parse_mode(dev, st, 2);
  489. if (ret)
  490. return ret;
  491. ret = devm_ad7625_regulator_setup(dev, st);
  492. if (ret)
  493. return ret;
  494. /* Set the device mode based on detected EN configuration. */
  495. if (!st->info->has_bandwidth_control) {
  496. ad7625_set_en_gpios_for_vref(st, st->have_refin, st->vref_mv);
  497. } else {
  498. /*
  499. * If neither sampling mode is available, then report an error,
  500. * since the other modes are not useful defaults.
  501. */
  502. if (st->can_wide_bandwidth) {
  503. ret = ad7960_set_mode(st, AD7960_MODE_WIDE_BANDWIDTH,
  504. st->have_refin, st->vref_mv);
  505. } else if (st->can_narrow_bandwidth) {
  506. ret = ad7960_set_mode(st, AD7960_MODE_NARROW_BANDWIDTH,
  507. st->have_refin, st->vref_mv);
  508. } else {
  509. return dev_err_probe(dev, -EINVAL,
  510. "couldn't set device to wide or narrow bandwidth modes\n");
  511. }
  512. if (ret)
  513. return dev_err_probe(dev, -EINVAL,
  514. "failed to set EN pins\n");
  515. }
  516. ret = devm_ad7625_pwm_get(dev, st);
  517. if (ret)
  518. return ret;
  519. indio_dev->channels = &st->info->chan_spec;
  520. indio_dev->num_channels = 1;
  521. indio_dev->name = st->info->name;
  522. indio_dev->info = &ad7625_info;
  523. indio_dev->setup_ops = &ad7625_buffer_setup_ops;
  524. st->back = devm_iio_backend_get(dev, NULL);
  525. if (IS_ERR(st->back))
  526. return dev_err_probe(dev, PTR_ERR(st->back),
  527. "failed to get IIO backend\n");
  528. ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
  529. if (ret)
  530. return ret;
  531. ret = devm_iio_backend_enable(dev, st->back);
  532. if (ret)
  533. return ret;
  534. /*
  535. * Set the initial sampling frequency to the maximum, unless the
  536. * AD796x device is limited to narrow bandwidth by EN2 == 1, in
  537. * which case the sampling frequency should be limited to 2MSPS
  538. */
  539. default_sample_freq = st->info->max_sample_freq_hz;
  540. if (st->info->has_bandwidth_control && !st->can_wide_bandwidth)
  541. default_sample_freq = AD7960_MAX_NBW_FREQ;
  542. ret = ad7625_set_sampling_freq(st, default_sample_freq);
  543. if (ret)
  544. dev_err_probe(dev, ret,
  545. "failed to set valid sampling frequency\n");
  546. return devm_iio_device_register(dev, indio_dev);
  547. }
  548. static const struct of_device_id ad7625_of_match[] = {
  549. { .compatible = "adi,ad7625", .data = &ad7625_chip_info },
  550. { .compatible = "adi,ad7626", .data = &ad7626_chip_info },
  551. { .compatible = "adi,ad7960", .data = &ad7960_chip_info },
  552. { .compatible = "adi,ad7961", .data = &ad7961_chip_info },
  553. { }
  554. };
  555. MODULE_DEVICE_TABLE(of, ad7625_of_match);
  556. static const struct platform_device_id ad7625_device_ids[] = {
  557. { .name = "ad7625", .driver_data = (kernel_ulong_t)&ad7625_chip_info },
  558. { .name = "ad7626", .driver_data = (kernel_ulong_t)&ad7626_chip_info },
  559. { .name = "ad7960", .driver_data = (kernel_ulong_t)&ad7960_chip_info },
  560. { .name = "ad7961", .driver_data = (kernel_ulong_t)&ad7961_chip_info },
  561. { }
  562. };
  563. MODULE_DEVICE_TABLE(platform, ad7625_device_ids);
  564. static struct platform_driver ad7625_driver = {
  565. .probe = ad7625_probe,
  566. .driver = {
  567. .name = "ad7625",
  568. .of_match_table = ad7625_of_match,
  569. },
  570. .id_table = ad7625_device_ids,
  571. };
  572. module_platform_driver(ad7625_driver);
  573. MODULE_AUTHOR("Trevor Gamblin <tgamblin@baylibre.com>");
  574. MODULE_DESCRIPTION("Analog Devices AD7625 ADC");
  575. MODULE_LICENSE("GPL");
  576. MODULE_IMPORT_NS("IIO_BACKEND");