ad4134.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2026 Analog Devices, Inc.
  4. * Author: Marcelo Schmitt <marcelo.schmitt@analog.com>
  5. */
  6. #include <linux/array_size.h>
  7. #include <linux/bitfield.h>
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/crc8.h>
  11. #include <linux/delay.h>
  12. #include <linux/dev_printk.h>
  13. #include <linux/err.h>
  14. #include <linux/export.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/types.h>
  18. #include <linux/module.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/reset.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/time.h>
  25. #include <linux/types.h>
  26. #include <linux/unaligned.h>
  27. #include <linux/units.h>
  28. #define AD4134_RESET_TIME_US (10 * USEC_PER_SEC)
  29. #define AD4134_REG_READ_MASK BIT(7)
  30. #define AD4134_SPI_MAX_XFER_LEN 3
  31. #define AD4134_EXT_CLOCK_MHZ (48 * HZ_PER_MHZ)
  32. #define AD4134_NUM_CHANNELS 4
  33. #define AD4134_CHAN_PRECISION_BITS 24
  34. #define AD4134_IFACE_CONFIG_A_REG 0x00
  35. #define AD4134_IFACE_CONFIG_B_REG 0x01
  36. #define AD4134_IFACE_CONFIG_B_SINGLE_INSTR BIT(7)
  37. #define AD4134_DEVICE_CONFIG_REG 0x02
  38. #define AD4134_DEVICE_CONFIG_POWER_MODE_MASK BIT(0)
  39. #define AD4134_POWER_MODE_HIGH_PERF 0x1
  40. #define AD4134_SILICON_REV_REG 0x07
  41. #define AD4134_SCRATCH_PAD_REG 0x0A
  42. #define AD4134_STREAM_MODE_REG 0x0E
  43. #define AD4134_SDO_PIN_SRC_SEL_REG 0x10
  44. #define AD4134_SDO_PIN_SRC_SEL_SDO_SEL_MASK BIT(2)
  45. #define AD4134_DATA_PACKET_CONFIG_REG 0x11
  46. #define AD4134_DATA_PACKET_CONFIG_FRAME_MASK GENMASK(5, 4)
  47. #define AD4134_DATA_PACKET_24BIT_FRAME 0x2
  48. #define AD4134_DIG_IF_CFG_REG 0x12
  49. #define AD4134_DIF_IF_CFG_FORMAT_MASK GENMASK(1, 0)
  50. #define AD4134_DATA_FORMAT_SINGLE_CH_MODE 0x0
  51. #define AD4134_PW_DOWN_CTRL_REG 0x13
  52. #define AD4134_DEVICE_STATUS_REG 0x15
  53. #define AD4134_ODR_VAL_INT_LSB_REG 0x16
  54. #define AD4134_CH3_OFFSET_MSB_REG 0x3E
  55. #define AD4134_AIN_OR_ERROR_REG 0x48
  56. /*
  57. * AD4134 register map ends at address 0x48 and there is no register for
  58. * retrieving ADC sample data. Though, to make use of Linux regmap API both
  59. * for register access and sample read, we define one virtual register for each
  60. * ADC channel. AD4134_CH_VREG(x) maps a channel number to it's virtual register
  61. * address while AD4134_VREG_CH(x) tells which channel given the address.
  62. */
  63. #define AD4134_CH_VREG(x) ((x) + 0x50)
  64. #define AD4134_VREG_CH(x) ((x) - 0x50)
  65. #define AD4134_SPI_CRC_POLYNOM 0x07
  66. #define AD4134_SPI_CRC_INIT_VALUE 0xA5
  67. static unsigned char ad4134_spi_crc_table[CRC8_TABLE_SIZE];
  68. #define AD4134_CHANNEL(_index) { \
  69. .type = IIO_VOLTAGE, \
  70. .indexed = 1, \
  71. .channel = (_index), \
  72. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  73. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  74. }
  75. static const struct iio_chan_spec ad4134_chan_set[] = {
  76. AD4134_CHANNEL(0),
  77. AD4134_CHANNEL(1),
  78. AD4134_CHANNEL(2),
  79. AD4134_CHANNEL(3),
  80. };
  81. struct ad4134_state {
  82. struct spi_device *spi;
  83. struct regmap *regmap;
  84. unsigned long sys_clk_hz;
  85. struct gpio_desc *odr_gpio;
  86. int refin_mv;
  87. /*
  88. * DMA (thus cache coherency maintenance) requires the transfer buffers
  89. * to live in their own cache lines.
  90. */
  91. u8 rx_buf[AD4134_SPI_MAX_XFER_LEN] __aligned(IIO_DMA_MINALIGN);
  92. u8 tx_buf[AD4134_SPI_MAX_XFER_LEN];
  93. };
  94. static const struct regmap_range ad4134_regmap_rd_range[] = {
  95. regmap_reg_range(AD4134_IFACE_CONFIG_A_REG, AD4134_SILICON_REV_REG),
  96. regmap_reg_range(AD4134_SCRATCH_PAD_REG, AD4134_PW_DOWN_CTRL_REG),
  97. regmap_reg_range(AD4134_DEVICE_STATUS_REG, AD4134_AIN_OR_ERROR_REG),
  98. regmap_reg_range(AD4134_CH_VREG(0), AD4134_CH_VREG(AD4134_NUM_CHANNELS)),
  99. };
  100. static const struct regmap_range ad4134_regmap_wr_range[] = {
  101. regmap_reg_range(AD4134_IFACE_CONFIG_A_REG, AD4134_DEVICE_CONFIG_REG),
  102. regmap_reg_range(AD4134_SCRATCH_PAD_REG, AD4134_SCRATCH_PAD_REG),
  103. regmap_reg_range(AD4134_STREAM_MODE_REG, AD4134_PW_DOWN_CTRL_REG),
  104. regmap_reg_range(AD4134_ODR_VAL_INT_LSB_REG, AD4134_CH3_OFFSET_MSB_REG),
  105. };
  106. static const struct regmap_access_table ad4134_regmap_rd_table = {
  107. .yes_ranges = ad4134_regmap_rd_range,
  108. .n_yes_ranges = ARRAY_SIZE(ad4134_regmap_rd_range),
  109. };
  110. static const struct regmap_access_table ad4134_regmap_wr_table = {
  111. .yes_ranges = ad4134_regmap_wr_range,
  112. .n_yes_ranges = ARRAY_SIZE(ad4134_regmap_wr_range),
  113. };
  114. static int ad4134_calc_spi_crc(u8 inst, u8 data)
  115. {
  116. u8 buf[] = { inst, data };
  117. return crc8(ad4134_spi_crc_table, buf, ARRAY_SIZE(buf),
  118. AD4134_SPI_CRC_INIT_VALUE);
  119. }
  120. static void ad4134_prepare_spi_tx_buf(u8 inst, u8 data, u8 *buf)
  121. {
  122. buf[0] = inst;
  123. buf[1] = data;
  124. buf[2] = ad4134_calc_spi_crc(inst, data);
  125. }
  126. static int ad4134_reg_write(void *context, unsigned int reg, unsigned int val)
  127. {
  128. struct ad4134_state *st = context;
  129. struct spi_transfer xfer = {
  130. .tx_buf = st->tx_buf,
  131. .rx_buf = st->rx_buf,
  132. .len = AD4134_SPI_MAX_XFER_LEN,
  133. };
  134. int ret;
  135. ad4134_prepare_spi_tx_buf(reg, val, st->tx_buf);
  136. ret = spi_sync_transfer(st->spi, &xfer, 1);
  137. if (ret)
  138. return ret;
  139. if (st->rx_buf[2] != st->tx_buf[2])
  140. dev_dbg(&st->spi->dev, "reg write CRC check failed\n");
  141. return 0;
  142. }
  143. static int ad4134_data_read(struct ad4134_state *st, unsigned int reg,
  144. unsigned int *val)
  145. {
  146. unsigned int i;
  147. int ret;
  148. /*
  149. * To be able to read data from all 4 channels through a single line, we
  150. * set DOUTx output format to 0 in the digital interface config register
  151. * (0x12). With that, data from all four channels is serialized and
  152. * output on DOUT0. During the probe, we also set SDO_PIN_SRC_SEL in
  153. * DEVICE_CONFIG_1 register to duplicate DOUT0 on the SDO pin. Combined,
  154. * those configurations enable ADC data read through a conventional SPI
  155. * interface. Now we read data from all channels but keep only the bits
  156. * from the requested one.
  157. */
  158. for (i = 0; i < ARRAY_SIZE(ad4134_chan_set); i++) {
  159. ret = spi_write_then_read(st->spi, NULL, 0, st->rx_buf,
  160. BITS_TO_BYTES(AD4134_CHAN_PRECISION_BITS));
  161. if (ret)
  162. return ret;
  163. /*
  164. * AD4134 has a built-in feature that flags when data transfers
  165. * don't run enough clock cycles to read the entire data frame.
  166. * Clock out data from all channels to avoid that.
  167. */
  168. if (i == AD4134_VREG_CH(reg))
  169. *val = get_unaligned_be24(st->rx_buf);
  170. }
  171. return 0;
  172. }
  173. static int ad4134_register_read(struct ad4134_state *st, unsigned int reg,
  174. unsigned int *val)
  175. {
  176. struct spi_transfer xfer = {
  177. .tx_buf = st->tx_buf,
  178. .rx_buf = st->rx_buf,
  179. .len = AD4134_SPI_MAX_XFER_LEN,
  180. };
  181. unsigned int inst;
  182. int ret;
  183. inst = AD4134_REG_READ_MASK | reg;
  184. ad4134_prepare_spi_tx_buf(inst, 0, st->tx_buf);
  185. ret = spi_sync_transfer(st->spi, &xfer, 1);
  186. if (ret)
  187. return ret;
  188. *val = st->rx_buf[1];
  189. /* Check CRC */
  190. if (st->rx_buf[2] != st->tx_buf[2])
  191. dev_dbg(&st->spi->dev, "reg read CRC check failed\n");
  192. return 0;
  193. }
  194. static int ad4134_reg_read(void *context, unsigned int reg, unsigned int *val)
  195. {
  196. struct ad4134_state *st = context;
  197. if (reg >= AD4134_CH_VREG(0))
  198. return ad4134_data_read(st, reg, val);
  199. return ad4134_register_read(st, reg, val);
  200. }
  201. static const struct regmap_config ad4134_regmap_config = {
  202. .reg_read = ad4134_reg_read,
  203. .reg_write = ad4134_reg_write,
  204. .rd_table = &ad4134_regmap_rd_table,
  205. .wr_table = &ad4134_regmap_wr_table,
  206. .max_register = AD4134_CH_VREG(ARRAY_SIZE(ad4134_chan_set)),
  207. };
  208. static int ad4134_read_raw(struct iio_dev *indio_dev,
  209. struct iio_chan_spec const *chan,
  210. int *val, int *val2, long info)
  211. {
  212. struct ad4134_state *st = iio_priv(indio_dev);
  213. int ret;
  214. switch (info) {
  215. case IIO_CHAN_INFO_RAW:
  216. gpiod_set_value_cansleep(st->odr_gpio, 1);
  217. /*
  218. * For slave mode gated DCLK (data sheet page 11), the minimum
  219. * ODR high time is 3 * tDIGCLK. The internal digital clock
  220. * period is tDIGCLK = 1/fDIGCLK = 2/fSYSCLK.
  221. * The System clock frequency (fSYSCLK) is typically 48 MHz.
  222. * Thus, ODR high time = 3 * (2 / (48 * HZ_PER_MHZ))
  223. * ODR high time = 0.000000125 s = 125 ns
  224. * 1 micro second should be more than enough. Not worth it
  225. * tweaking for shorter dealy since this is not a fast data path.
  226. */
  227. fsleep(1);
  228. gpiod_set_value_cansleep(st->odr_gpio, 0);
  229. ret = regmap_read(st->regmap, AD4134_CH_VREG(chan->channel), val);
  230. if (ret)
  231. return ret;
  232. return IIO_VAL_INT;
  233. case IIO_CHAN_INFO_SCALE:
  234. *val = st->refin_mv;
  235. *val2 = AD4134_CHAN_PRECISION_BITS - 1;
  236. return IIO_VAL_FRACTIONAL_LOG2;
  237. default:
  238. return -EINVAL;
  239. }
  240. }
  241. static int ad4134_debugfs_reg_access(struct iio_dev *indio_dev,
  242. unsigned int reg, unsigned int writeval,
  243. unsigned int *readval)
  244. {
  245. struct ad4134_state *st = iio_priv(indio_dev);
  246. if (readval)
  247. return regmap_read(st->regmap, reg, readval);
  248. return regmap_write(st->regmap, reg, writeval);
  249. }
  250. static int ad4134_min_io_mode_setup(struct ad4134_state *st)
  251. {
  252. struct device *dev = &st->spi->dev;
  253. int ret;
  254. st->odr_gpio = devm_gpiod_get(dev, "odr", GPIOD_OUT_LOW);
  255. if (IS_ERR(st->odr_gpio))
  256. return dev_err_probe(dev, PTR_ERR(st->odr_gpio),
  257. "failed to get ODR GPIO\n");
  258. ret = regmap_update_bits(st->regmap, AD4134_DIG_IF_CFG_REG,
  259. AD4134_DIF_IF_CFG_FORMAT_MASK,
  260. FIELD_PREP(AD4134_DIF_IF_CFG_FORMAT_MASK,
  261. AD4134_DATA_FORMAT_SINGLE_CH_MODE));
  262. if (ret)
  263. return dev_err_probe(dev, ret,
  264. "failed to set single channel mode\n");
  265. ret = regmap_set_bits(st->regmap, AD4134_SDO_PIN_SRC_SEL_REG,
  266. AD4134_SDO_PIN_SRC_SEL_SDO_SEL_MASK);
  267. if (ret)
  268. return dev_err_probe(dev, ret,
  269. "failed to set SDO source selection\n");
  270. return regmap_set_bits(st->regmap, AD4134_IFACE_CONFIG_B_REG,
  271. AD4134_IFACE_CONFIG_B_SINGLE_INSTR);
  272. }
  273. static const struct iio_info ad4134_info = {
  274. .read_raw = ad4134_read_raw,
  275. .debugfs_reg_access = ad4134_debugfs_reg_access,
  276. };
  277. static const char * const ad4143_required_regulators[] = {
  278. "avdd5", "dvdd5", "iovdd",
  279. };
  280. static const char * const ad4143_optional_regulators[] = {
  281. "avdd1v8", "dvdd1v8", "clkvdd",
  282. };
  283. static int ad4134_regulator_setup(struct ad4134_state *st)
  284. {
  285. struct device *dev = &st->spi->dev;
  286. int ret;
  287. ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(ad4143_required_regulators),
  288. ad4143_required_regulators);
  289. if (ret)
  290. return dev_err_probe(dev, ret, "failed to enable power supplies\n");
  291. /* Required regulator that we need to read the voltage */
  292. ret = devm_regulator_get_enable_read_voltage(dev, "refin");
  293. if (ret < 0)
  294. return dev_err_probe(dev, ret, "failed to get REFIN voltage.\n");
  295. st->refin_mv = ret / (MICRO / MILLI);
  296. ret = devm_regulator_get_enable_optional(dev, "ldoin");
  297. if (ret < 0 && ret != -ENODEV)
  298. return dev_err_probe(dev, ret, "failed to enable ldoin supply\n");
  299. /* If ldoin was provided, then use the use the internal LDO regulators */
  300. if (ret == 0)
  301. return 0;
  302. /*
  303. * If ldoin is not provided, then avdd1v8, dvdd1v8, and clkvdd are
  304. * required.
  305. */
  306. ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(ad4143_optional_regulators),
  307. ad4143_optional_regulators);
  308. if (ret)
  309. return dev_err_probe(dev, ret, "failed to enable 1V8 power supplies\n");
  310. return 0;
  311. }
  312. static int ad4134_clock_select(struct ad4134_state *st)
  313. {
  314. struct device *dev = &st->spi->dev;
  315. struct clk *xtal_clk, *clkin_clk;
  316. /*
  317. * AD4134 requires one external clock source and only one external clock
  318. * source can be provided at a time. Try to get a crystal provided clock.
  319. * If that fails, try to get a CMOS clock.
  320. */
  321. xtal_clk = devm_clk_get_optional_enabled(dev, "xtal");
  322. if (!xtal_clk)
  323. xtal_clk = devm_clk_get_optional_enabled(dev, "xtal");
  324. if (IS_ERR(xtal_clk))
  325. return dev_err_probe(dev, PTR_ERR(xtal_clk),
  326. "failed to get xtal\n");
  327. clkin_clk = devm_clk_get_optional_enabled(dev, "clkin");
  328. if (!clkin_clk)
  329. clkin_clk = devm_clk_get_optional_enabled(dev, "clkin");
  330. if (IS_ERR(clkin_clk))
  331. return dev_err_probe(dev, PTR_ERR(clkin_clk),
  332. "failed to get clkin\n");
  333. st->sys_clk_hz = clk_get_rate(xtal_clk) | clk_get_rate(clkin_clk);
  334. if (st->sys_clk_hz != AD4134_EXT_CLOCK_MHZ)
  335. dev_warn(dev, "invalid external clock frequency %lu\n",
  336. st->sys_clk_hz);
  337. return 0;
  338. }
  339. static int ad4134_probe(struct spi_device *spi)
  340. {
  341. struct device *dev = &spi->dev;
  342. struct reset_control *rst;
  343. struct iio_dev *indio_dev;
  344. struct ad4134_state *st;
  345. int ret;
  346. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  347. if (!indio_dev)
  348. return -ENOMEM;
  349. st = iio_priv(indio_dev);
  350. st->spi = spi;
  351. indio_dev->name = "ad4134";
  352. indio_dev->channels = ad4134_chan_set;
  353. indio_dev->num_channels = ARRAY_SIZE(ad4134_chan_set);
  354. indio_dev->modes = INDIO_DIRECT_MODE;
  355. indio_dev->info = &ad4134_info;
  356. ret = ad4134_regulator_setup(st);
  357. if (ret)
  358. return ret;
  359. ret = ad4134_clock_select(st);
  360. if (ret)
  361. return ret;
  362. rst = devm_reset_control_get_optional_exclusive_deasserted(dev, NULL);
  363. if (IS_ERR(rst))
  364. return dev_err_probe(dev, PTR_ERR(rst),
  365. "failed to get and deassert reset\n");
  366. crc8_populate_msb(ad4134_spi_crc_table, AD4134_SPI_CRC_POLYNOM);
  367. st->regmap = devm_regmap_init(dev, NULL, st, &ad4134_regmap_config);
  368. if (IS_ERR(st->regmap))
  369. return dev_err_probe(dev, PTR_ERR(st->regmap),
  370. "failed to initialize regmap");
  371. ret = ad4134_min_io_mode_setup(st);
  372. if (ret)
  373. return dev_err_probe(dev, ret,
  374. "failed to setup minimum I/O mode\n");
  375. /* Bump precision to 24-bit */
  376. ret = regmap_update_bits(st->regmap, AD4134_DATA_PACKET_CONFIG_REG,
  377. AD4134_DATA_PACKET_CONFIG_FRAME_MASK,
  378. FIELD_PREP(AD4134_DATA_PACKET_CONFIG_FRAME_MASK,
  379. AD4134_DATA_PACKET_24BIT_FRAME));
  380. if (ret)
  381. return ret;
  382. /* Set high performance power mode */
  383. ret = regmap_update_bits(st->regmap, AD4134_DEVICE_CONFIG_REG,
  384. AD4134_DEVICE_CONFIG_POWER_MODE_MASK,
  385. FIELD_PREP(AD4134_DEVICE_CONFIG_POWER_MODE_MASK,
  386. AD4134_POWER_MODE_HIGH_PERF));
  387. if (ret)
  388. return ret;
  389. return devm_iio_device_register(dev, indio_dev);
  390. }
  391. static const struct spi_device_id ad4134_id[] = {
  392. { "ad4134" },
  393. { }
  394. };
  395. MODULE_DEVICE_TABLE(spi, ad4134_id);
  396. static const struct of_device_id ad4134_of_match[] = {
  397. { .compatible = "adi,ad4134" },
  398. { }
  399. };
  400. MODULE_DEVICE_TABLE(of, ad4134_of_match);
  401. static struct spi_driver ad4134_driver = {
  402. .driver = {
  403. .name = "ad4134",
  404. .of_match_table = ad4134_of_match,
  405. },
  406. .probe = ad4134_probe,
  407. .id_table = ad4134_id,
  408. };
  409. module_spi_driver(ad4134_driver);
  410. MODULE_AUTHOR("Marcelo Schmitt <marcelo.schmitt@analog.com>");
  411. MODULE_DESCRIPTION("Analog Devices AD4134 SPI driver");
  412. MODULE_LICENSE("GPL");
  413. MODULE_IMPORT_NS("IIO_AD4134");