ad4080.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Analog Devices AD4080 SPI ADC driver
  4. *
  5. * Copyright 2025 Analog Devices Inc.
  6. */
  7. #include <linux/array_size.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/bits.h>
  10. #include <linux/clk.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/iio/backend.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/types.h>
  22. #include <linux/unaligned.h>
  23. #include <linux/units.h>
  24. /* Register Definition */
  25. #define AD4080_REG_INTERFACE_CONFIG_A 0x00
  26. #define AD4080_REG_INTERFACE_CONFIG_B 0x01
  27. #define AD4080_REG_DEVICE_CONFIG 0x02
  28. #define AD4080_REG_CHIP_TYPE 0x03
  29. #define AD4080_REG_PRODUCT_ID_L 0x04
  30. #define AD4080_REG_PRODUCT_ID_H 0x05
  31. #define AD4080_REG_CHIP_GRADE 0x06
  32. #define AD4080_REG_SCRATCH_PAD 0x0A
  33. #define AD4080_REG_SPI_REVISION 0x0B
  34. #define AD4080_REG_VENDOR_L 0x0C
  35. #define AD4080_REG_VENDOR_H 0x0D
  36. #define AD4080_REG_STREAM_MODE 0x0E
  37. #define AD4080_REG_TRANSFER_CONFIG 0x0F
  38. #define AD4080_REG_INTERFACE_CONFIG_C 0x10
  39. #define AD4080_REG_INTERFACE_STATUS_A 0x11
  40. #define AD4080_REG_DEVICE_STATUS 0x14
  41. #define AD4080_REG_ADC_DATA_INTF_CONFIG_A 0x15
  42. #define AD4080_REG_ADC_DATA_INTF_CONFIG_B 0x16
  43. #define AD4080_REG_ADC_DATA_INTF_CONFIG_C 0x17
  44. #define AD4080_REG_PWR_CTRL 0x18
  45. #define AD4080_REG_GPIO_CONFIG_A 0x19
  46. #define AD4080_REG_GPIO_CONFIG_B 0x1A
  47. #define AD4080_REG_GPIO_CONFIG_C 0x1B
  48. #define AD4080_REG_GENERAL_CONFIG 0x1C
  49. #define AD4080_REG_FIFO_WATERMARK_LSB 0x1D
  50. #define AD4080_REG_FIFO_WATERMARK_MSB 0x1E
  51. #define AD4080_REG_EVENT_HYSTERESIS_LSB 0x1F
  52. #define AD4080_REG_EVENT_HYSTERESIS_MSB 0x20
  53. #define AD4080_REG_EVENT_DETECTION_HI_LSB 0x21
  54. #define AD4080_REG_EVENT_DETECTION_HI_MSB 0x22
  55. #define AD4080_REG_EVENT_DETECTION_LO_LSB 0x23
  56. #define AD4080_REG_EVENT_DETECTION_LO_MSB 0x24
  57. #define AD4080_REG_OFFSET_LSB 0x25
  58. #define AD4080_REG_OFFSET_MSB 0x26
  59. #define AD4080_REG_GAIN_LSB 0x27
  60. #define AD4080_REG_GAIN_MSB 0x28
  61. #define AD4080_REG_FILTER_CONFIG 0x29
  62. /* AD4080_REG_INTERFACE_CONFIG_A Bit Definition */
  63. #define AD4080_INTERFACE_CONFIG_A_SW_RESET (BIT(7) | BIT(0))
  64. #define AD4080_INTERFACE_CONFIG_A_ADDR_ASC BIT(5)
  65. #define AD4080_INTERFACE_CONFIG_A_SDO_ENABLE BIT(4)
  66. /* AD4080_REG_INTERFACE_CONFIG_B Bit Definition */
  67. #define AD4080_INTERFACE_CONFIG_B_SINGLE_INST BIT(7)
  68. #define AD4080_INTERFACE_CONFIG_B_SHORT_INST BIT(3)
  69. /* AD4080_REG_DEVICE_CONFIG Bit Definition */
  70. #define AD4080_DEVICE_CONFIG_OPERATING_MODES_MSK GENMASK(1, 0)
  71. /* AD4080_REG_TRANSFER_CONFIG Bit Definition */
  72. #define AD4080_TRANSFER_CONFIG_KEEP_STREAM_LENGTH_VAL BIT(2)
  73. /* AD4080_REG_INTERFACE_CONFIG_C Bit Definition */
  74. #define AD4080_INTERFACE_CONFIG_C_STRICT_REG_ACCESS BIT(5)
  75. /* AD4080_REG_ADC_DATA_INTF_CONFIG_A Bit Definition */
  76. #define AD4080_ADC_DATA_INTF_CONFIG_A_RESERVED_CONFIG_A BIT(6)
  77. #define AD4080_ADC_DATA_INTF_CONFIG_A_INTF_CHK_EN BIT(4)
  78. #define AD4080_ADC_DATA_INTF_CONFIG_A_SPI_LVDS_LANES BIT(2)
  79. #define AD4080_ADC_DATA_INTF_CONFIG_A_DATA_INTF_MODE BIT(0)
  80. /* AD4080_REG_ADC_DATA_INTF_CONFIG_B Bit Definition */
  81. #define AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_CNV_CLK_CNT_MSK GENMASK(7, 4)
  82. #define AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_SELF_CLK_MODE BIT(3)
  83. #define AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_CNV_EN BIT(0)
  84. /* AD4080_REG_ADC_DATA_INTF_CONFIG_C Bit Definition */
  85. #define AD4080_ADC_DATA_INTF_CONFIG_C_LVDS_VOD_MSK GENMASK(6, 4)
  86. /* AD4080_REG_PWR_CTRL Bit Definition */
  87. #define AD4080_PWR_CTRL_ANA_DIG_LDO_PD BIT(1)
  88. #define AD4080_PWR_CTRL_INTF_LDO_PD BIT(0)
  89. /* AD4080_REG_GPIO_CONFIG_A Bit Definition */
  90. #define AD4080_GPIO_CONFIG_A_GPO_1_EN BIT(1)
  91. #define AD4080_GPIO_CONFIG_A_GPO_0_EN BIT(0)
  92. /* AD4080_REG_GPIO_CONFIG_B Bit Definition */
  93. #define AD4080_GPIO_CONFIG_B_GPIO_1_SEL_MSK GENMASK(7, 4)
  94. #define AD4080_GPIO_CONFIG_B_GPIO_0_SEL_MSK GENMASK(3, 0)
  95. #define AD4080_GPIO_CONFIG_B_GPIO_SPI_SDO 0
  96. #define AD4080_GPIO_CONFIG_B_GPIO_FIFO_FULL 1
  97. #define AD4080_GPIO_CONFIG_B_GPIO_FIFO_READ_DONE 2
  98. #define AD4080_GPIO_CONFIG_B_GPIO_FILTER_RES_RDY 3
  99. #define AD4080_GPIO_CONFIG_B_GPIO_H_THRESH 4
  100. #define AD4080_GPIO_CONFIG_B_GPIO_L_THRESH 5
  101. #define AD4080_GPIO_CONFIG_B_GPIO_STATUS_ALERT 6
  102. #define AD4080_GPIO_CONFIG_B_GPIO_GPIO_DATA 7
  103. #define AD4080_GPIO_CONFIG_B_GPIO_FILTER_SYNC 8
  104. #define AD4080_GPIO_CONFIG_B_GPIO_EXTERNAL_EVENT 9
  105. /* AD4080_REG_FIFO_CONFIG Bit Definition */
  106. #define AD4080_FIFO_CONFIG_FIFO_MODE_MSK GENMASK(1, 0)
  107. /* AD4080_REG_FILTER_CONFIG Bit Definition */
  108. #define AD4080_FILTER_CONFIG_SINC_DEC_RATE_MSK GENMASK(6, 3)
  109. #define AD4080_FILTER_CONFIG_FILTER_SEL_MSK GENMASK(1, 0)
  110. /* Miscellaneous Definitions */
  111. #define AD4080_SPI_READ BIT(7)
  112. #define AD4080_CHIP_ID 0x0050
  113. #define AD4081_CHIP_ID 0x0051
  114. #define AD4083_CHIP_ID 0x0053
  115. #define AD4084_CHIP_ID 0x0054
  116. #define AD4086_CHIP_ID 0x0056
  117. #define AD4087_CHIP_ID 0x0057
  118. #define AD4080_LVDS_CNV_CLK_CNT_MAX 7
  119. #define AD4080_MAX_SAMP_FREQ 40000000
  120. #define AD4080_MIN_SAMP_FREQ 1250000
  121. enum ad4080_filter_type {
  122. FILTER_NONE,
  123. SINC_1,
  124. SINC_5,
  125. SINC_5_COMP
  126. };
  127. static const unsigned int ad4080_scale_table[][2] = {
  128. { 6000, 0 },
  129. };
  130. static const char *const ad4080_filter_type_iio_enum[] = {
  131. [FILTER_NONE] = "none",
  132. [SINC_1] = "sinc1",
  133. [SINC_5] = "sinc5",
  134. [SINC_5_COMP] = "sinc5+pf1",
  135. };
  136. static const int ad4080_dec_rate_avail[] = {
  137. 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
  138. };
  139. static const int ad4080_dec_rate_none[] = { 1 };
  140. static const char * const ad4080_power_supplies[] = {
  141. "vdd33", "vdd11", "vddldo", "iovdd", "vrefin",
  142. };
  143. struct ad4080_chip_info {
  144. const char *name;
  145. unsigned int product_id;
  146. int num_scales;
  147. const unsigned int (*scale_table)[2];
  148. const struct iio_chan_spec *channels;
  149. unsigned int num_channels;
  150. unsigned int lvds_cnv_clk_cnt_max;
  151. };
  152. struct ad4080_state {
  153. struct regmap *regmap;
  154. struct iio_backend *back;
  155. const struct ad4080_chip_info *info;
  156. /*
  157. * Synchronize access to members the of driver state, and ensure
  158. * atomicity of consecutive regmap operations.
  159. */
  160. struct mutex lock;
  161. unsigned int num_lanes;
  162. unsigned int dec_rate;
  163. unsigned long clk_rate;
  164. enum ad4080_filter_type filter_type;
  165. bool lvds_cnv_en;
  166. };
  167. static const struct regmap_config ad4080_regmap_config = {
  168. .reg_bits = 16,
  169. .val_bits = 8,
  170. .read_flag_mask = BIT(7),
  171. .max_register = 0x29,
  172. };
  173. static int ad4080_reg_access(struct iio_dev *indio_dev, unsigned int reg,
  174. unsigned int writeval, unsigned int *readval)
  175. {
  176. struct ad4080_state *st = iio_priv(indio_dev);
  177. if (readval)
  178. return regmap_read(st->regmap, reg, readval);
  179. return regmap_write(st->regmap, reg, writeval);
  180. }
  181. static int ad4080_get_scale(struct ad4080_state *st, int *val, int *val2)
  182. {
  183. unsigned int tmp;
  184. tmp = (st->info->scale_table[0][0] * 1000000ULL) >>
  185. st->info->channels[0].scan_type.realbits;
  186. *val = tmp / 1000000;
  187. *val2 = tmp % 1000000;
  188. return IIO_VAL_INT_PLUS_NANO;
  189. }
  190. static unsigned int ad4080_get_dec_rate(struct iio_dev *dev,
  191. const struct iio_chan_spec *chan)
  192. {
  193. struct ad4080_state *st = iio_priv(dev);
  194. int ret;
  195. unsigned int data;
  196. ret = regmap_read(st->regmap, AD4080_REG_FILTER_CONFIG, &data);
  197. if (ret)
  198. return ret;
  199. return 1 << (FIELD_GET(AD4080_FILTER_CONFIG_SINC_DEC_RATE_MSK, data) + 1);
  200. }
  201. static int ad4080_set_dec_rate(struct iio_dev *dev,
  202. const struct iio_chan_spec *chan,
  203. unsigned int mode)
  204. {
  205. struct ad4080_state *st = iio_priv(dev);
  206. guard(mutex)(&st->lock);
  207. if ((st->filter_type >= SINC_5 && mode >= 512) || mode < 2)
  208. return -EINVAL;
  209. return regmap_update_bits(st->regmap, AD4080_REG_FILTER_CONFIG,
  210. AD4080_FILTER_CONFIG_SINC_DEC_RATE_MSK,
  211. FIELD_PREP(AD4080_FILTER_CONFIG_SINC_DEC_RATE_MSK,
  212. (ilog2(mode) - 1)));
  213. }
  214. static int ad4080_read_raw(struct iio_dev *indio_dev,
  215. struct iio_chan_spec const *chan,
  216. int *val, int *val2, long m)
  217. {
  218. struct ad4080_state *st = iio_priv(indio_dev);
  219. int dec_rate;
  220. switch (m) {
  221. case IIO_CHAN_INFO_SCALE:
  222. return ad4080_get_scale(st, val, val2);
  223. case IIO_CHAN_INFO_SAMP_FREQ:
  224. dec_rate = ad4080_get_dec_rate(indio_dev, chan);
  225. if (dec_rate < 0)
  226. return dec_rate;
  227. if (st->filter_type == SINC_5_COMP)
  228. dec_rate *= 2;
  229. if (st->filter_type)
  230. *val = DIV_ROUND_CLOSEST(st->clk_rate, dec_rate);
  231. else
  232. *val = st->clk_rate;
  233. return IIO_VAL_INT;
  234. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  235. if (st->filter_type == FILTER_NONE) {
  236. *val = 1;
  237. } else {
  238. *val = ad4080_get_dec_rate(indio_dev, chan);
  239. if (*val < 0)
  240. return *val;
  241. }
  242. return IIO_VAL_INT;
  243. default:
  244. return -EINVAL;
  245. }
  246. }
  247. static int ad4080_write_raw(struct iio_dev *indio_dev,
  248. struct iio_chan_spec const *chan,
  249. int val, int val2, long mask)
  250. {
  251. struct ad4080_state *st = iio_priv(indio_dev);
  252. switch (mask) {
  253. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  254. if (st->filter_type == FILTER_NONE && val > 1)
  255. return -EINVAL;
  256. return ad4080_set_dec_rate(indio_dev, chan, val);
  257. default:
  258. return -EINVAL;
  259. }
  260. }
  261. static int ad4080_lvds_sync_write(struct ad4080_state *st)
  262. {
  263. struct device *dev = regmap_get_device(st->regmap);
  264. int ret;
  265. ret = regmap_set_bits(st->regmap, AD4080_REG_ADC_DATA_INTF_CONFIG_A,
  266. AD4080_ADC_DATA_INTF_CONFIG_A_INTF_CHK_EN);
  267. if (ret)
  268. return ret;
  269. ret = iio_backend_interface_data_align(st->back, 10000);
  270. if (ret)
  271. return dev_err_probe(dev, ret,
  272. "Data alignment process failed\n");
  273. dev_dbg(dev, "Success: Pattern correct and Locked!\n");
  274. return regmap_clear_bits(st->regmap, AD4080_REG_ADC_DATA_INTF_CONFIG_A,
  275. AD4080_ADC_DATA_INTF_CONFIG_A_INTF_CHK_EN);
  276. }
  277. static int ad4080_get_filter_type(struct iio_dev *dev,
  278. const struct iio_chan_spec *chan)
  279. {
  280. struct ad4080_state *st = iio_priv(dev);
  281. unsigned int data;
  282. int ret;
  283. ret = regmap_read(st->regmap, AD4080_REG_FILTER_CONFIG, &data);
  284. if (ret)
  285. return ret;
  286. return FIELD_GET(AD4080_FILTER_CONFIG_FILTER_SEL_MSK, data);
  287. }
  288. static int ad4080_set_filter_type(struct iio_dev *dev,
  289. const struct iio_chan_spec *chan,
  290. unsigned int mode)
  291. {
  292. struct ad4080_state *st = iio_priv(dev);
  293. int dec_rate;
  294. int ret;
  295. guard(mutex)(&st->lock);
  296. dec_rate = ad4080_get_dec_rate(dev, chan);
  297. if (dec_rate < 0)
  298. return dec_rate;
  299. if (mode >= SINC_5 && dec_rate >= 512)
  300. return -EINVAL;
  301. ret = iio_backend_filter_type_set(st->back, mode);
  302. if (ret)
  303. return ret;
  304. ret = regmap_update_bits(st->regmap, AD4080_REG_FILTER_CONFIG,
  305. AD4080_FILTER_CONFIG_FILTER_SEL_MSK,
  306. FIELD_PREP(AD4080_FILTER_CONFIG_FILTER_SEL_MSK,
  307. mode));
  308. if (ret)
  309. return ret;
  310. st->filter_type = mode;
  311. return 0;
  312. }
  313. static int ad4080_read_avail(struct iio_dev *indio_dev,
  314. struct iio_chan_spec const *chan,
  315. const int **vals, int *type, int *length,
  316. long mask)
  317. {
  318. struct ad4080_state *st = iio_priv(indio_dev);
  319. switch (mask) {
  320. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  321. switch (st->filter_type) {
  322. case FILTER_NONE:
  323. *vals = ad4080_dec_rate_none;
  324. *length = ARRAY_SIZE(ad4080_dec_rate_none);
  325. break;
  326. default:
  327. *vals = ad4080_dec_rate_avail;
  328. *length = st->filter_type >= SINC_5 ?
  329. (ARRAY_SIZE(ad4080_dec_rate_avail) - 2) :
  330. ARRAY_SIZE(ad4080_dec_rate_avail);
  331. break;
  332. }
  333. *type = IIO_VAL_INT;
  334. return IIO_AVAIL_LIST;
  335. default:
  336. return -EINVAL;
  337. }
  338. }
  339. static const struct iio_info ad4080_iio_info = {
  340. .debugfs_reg_access = ad4080_reg_access,
  341. .read_raw = ad4080_read_raw,
  342. .write_raw = ad4080_write_raw,
  343. .read_avail = ad4080_read_avail,
  344. };
  345. static const struct iio_enum ad4080_filter_type_enum = {
  346. .items = ad4080_filter_type_iio_enum,
  347. .num_items = ARRAY_SIZE(ad4080_filter_type_iio_enum),
  348. .set = ad4080_set_filter_type,
  349. .get = ad4080_get_filter_type,
  350. };
  351. static struct iio_chan_spec_ext_info ad4080_ext_info[] = {
  352. IIO_ENUM("filter_type", IIO_SHARED_BY_ALL, &ad4080_filter_type_enum),
  353. IIO_ENUM_AVAILABLE("filter_type", IIO_SHARED_BY_ALL,
  354. &ad4080_filter_type_enum),
  355. { }
  356. };
  357. #define AD4080_CHANNEL_DEFINE(bits, storage) { \
  358. .type = IIO_VOLTAGE, \
  359. .indexed = 1, \
  360. .channel = 0, \
  361. .info_mask_separate = BIT(IIO_CHAN_INFO_SCALE), \
  362. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  363. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  364. .info_mask_shared_by_all_available = \
  365. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  366. .ext_info = ad4080_ext_info, \
  367. .scan_index = 0, \
  368. .scan_type = { \
  369. .sign = 's', \
  370. .realbits = (bits), \
  371. .storagebits = (storage), \
  372. }, \
  373. }
  374. static const struct iio_chan_spec ad4080_channel = AD4080_CHANNEL_DEFINE(20, 32);
  375. static const struct iio_chan_spec ad4081_channel = AD4080_CHANNEL_DEFINE(20, 32);
  376. static const struct iio_chan_spec ad4083_channel = AD4080_CHANNEL_DEFINE(16, 16);
  377. static const struct iio_chan_spec ad4084_channel = AD4080_CHANNEL_DEFINE(16, 16);
  378. static const struct iio_chan_spec ad4086_channel = AD4080_CHANNEL_DEFINE(14, 16);
  379. static const struct iio_chan_spec ad4087_channel = AD4080_CHANNEL_DEFINE(14, 16);
  380. static const struct ad4080_chip_info ad4080_chip_info = {
  381. .name = "ad4080",
  382. .product_id = AD4080_CHIP_ID,
  383. .scale_table = ad4080_scale_table,
  384. .num_scales = ARRAY_SIZE(ad4080_scale_table),
  385. .num_channels = 1,
  386. .channels = &ad4080_channel,
  387. .lvds_cnv_clk_cnt_max = AD4080_LVDS_CNV_CLK_CNT_MAX,
  388. };
  389. static const struct ad4080_chip_info ad4081_chip_info = {
  390. .name = "ad4081",
  391. .product_id = AD4081_CHIP_ID,
  392. .scale_table = ad4080_scale_table,
  393. .num_scales = ARRAY_SIZE(ad4080_scale_table),
  394. .num_channels = 1,
  395. .channels = &ad4081_channel,
  396. .lvds_cnv_clk_cnt_max = 2,
  397. };
  398. static const struct ad4080_chip_info ad4083_chip_info = {
  399. .name = "ad4083",
  400. .product_id = AD4083_CHIP_ID,
  401. .scale_table = ad4080_scale_table,
  402. .num_scales = ARRAY_SIZE(ad4080_scale_table),
  403. .num_channels = 1,
  404. .channels = &ad4083_channel,
  405. .lvds_cnv_clk_cnt_max = 5,
  406. };
  407. static const struct ad4080_chip_info ad4084_chip_info = {
  408. .name = "ad4084",
  409. .product_id = AD4084_CHIP_ID,
  410. .scale_table = ad4080_scale_table,
  411. .num_scales = ARRAY_SIZE(ad4080_scale_table),
  412. .num_channels = 1,
  413. .channels = &ad4084_channel,
  414. .lvds_cnv_clk_cnt_max = 2,
  415. };
  416. static const struct ad4080_chip_info ad4086_chip_info = {
  417. .name = "ad4086",
  418. .product_id = AD4086_CHIP_ID,
  419. .scale_table = ad4080_scale_table,
  420. .num_scales = ARRAY_SIZE(ad4080_scale_table),
  421. .num_channels = 1,
  422. .channels = &ad4086_channel,
  423. .lvds_cnv_clk_cnt_max = 4,
  424. };
  425. static const struct ad4080_chip_info ad4087_chip_info = {
  426. .name = "ad4087",
  427. .product_id = AD4087_CHIP_ID,
  428. .scale_table = ad4080_scale_table,
  429. .num_scales = ARRAY_SIZE(ad4080_scale_table),
  430. .num_channels = 1,
  431. .channels = &ad4087_channel,
  432. .lvds_cnv_clk_cnt_max = 1,
  433. };
  434. static int ad4080_setup(struct iio_dev *indio_dev)
  435. {
  436. struct ad4080_state *st = iio_priv(indio_dev);
  437. struct device *dev = regmap_get_device(st->regmap);
  438. __le16 id_le;
  439. u16 id;
  440. int ret;
  441. ret = regmap_write(st->regmap, AD4080_REG_INTERFACE_CONFIG_A,
  442. AD4080_INTERFACE_CONFIG_A_SW_RESET);
  443. if (ret)
  444. return ret;
  445. ret = regmap_write(st->regmap, AD4080_REG_INTERFACE_CONFIG_A,
  446. AD4080_INTERFACE_CONFIG_A_SDO_ENABLE);
  447. if (ret)
  448. return ret;
  449. ret = regmap_bulk_read(st->regmap, AD4080_REG_PRODUCT_ID_L, &id_le,
  450. sizeof(id_le));
  451. if (ret)
  452. return ret;
  453. id = le16_to_cpu(id_le);
  454. if (id != st->info->product_id)
  455. dev_info(dev, "Unrecognized CHIP_ID 0x%X\n", id);
  456. ret = regmap_set_bits(st->regmap, AD4080_REG_GPIO_CONFIG_A,
  457. AD4080_GPIO_CONFIG_A_GPO_1_EN);
  458. if (ret)
  459. return ret;
  460. ret = regmap_write(st->regmap, AD4080_REG_GPIO_CONFIG_B,
  461. FIELD_PREP(AD4080_GPIO_CONFIG_B_GPIO_1_SEL_MSK,
  462. AD4080_GPIO_CONFIG_B_GPIO_FILTER_RES_RDY));
  463. if (ret)
  464. return ret;
  465. ret = iio_backend_num_lanes_set(st->back, st->num_lanes);
  466. if (ret)
  467. return ret;
  468. if (!st->lvds_cnv_en)
  469. return 0;
  470. /* Set maximum LVDS Data Transfer Latency */
  471. ret = regmap_update_bits(st->regmap,
  472. AD4080_REG_ADC_DATA_INTF_CONFIG_B,
  473. AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_CNV_CLK_CNT_MSK,
  474. FIELD_PREP(AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_CNV_CLK_CNT_MSK,
  475. st->info->lvds_cnv_clk_cnt_max));
  476. if (ret)
  477. return ret;
  478. if (st->num_lanes > 1) {
  479. ret = regmap_set_bits(st->regmap, AD4080_REG_ADC_DATA_INTF_CONFIG_A,
  480. AD4080_ADC_DATA_INTF_CONFIG_A_SPI_LVDS_LANES);
  481. if (ret)
  482. return ret;
  483. }
  484. ret = regmap_set_bits(st->regmap,
  485. AD4080_REG_ADC_DATA_INTF_CONFIG_B,
  486. AD4080_ADC_DATA_INTF_CONFIG_B_LVDS_CNV_EN);
  487. if (ret)
  488. return ret;
  489. return ad4080_lvds_sync_write(st);
  490. }
  491. static int ad4080_properties_parse(struct ad4080_state *st)
  492. {
  493. struct device *dev = regmap_get_device(st->regmap);
  494. st->lvds_cnv_en = device_property_read_bool(dev, "adi,lvds-cnv-enable");
  495. st->num_lanes = 1;
  496. device_property_read_u32(dev, "adi,num-lanes", &st->num_lanes);
  497. if (!st->num_lanes || st->num_lanes > 2)
  498. return dev_err_probe(dev, -EINVAL,
  499. "Invalid 'adi,num-lanes' value: %u",
  500. st->num_lanes);
  501. return 0;
  502. }
  503. static int ad4080_probe(struct spi_device *spi)
  504. {
  505. struct iio_dev *indio_dev;
  506. struct device *dev = &spi->dev;
  507. struct ad4080_state *st;
  508. struct clk *clk;
  509. int ret;
  510. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  511. if (!indio_dev)
  512. return -ENOMEM;
  513. st = iio_priv(indio_dev);
  514. ret = devm_regulator_bulk_get_enable(dev,
  515. ARRAY_SIZE(ad4080_power_supplies),
  516. ad4080_power_supplies);
  517. if (ret)
  518. return dev_err_probe(dev, ret,
  519. "failed to get and enable supplies\n");
  520. st->regmap = devm_regmap_init_spi(spi, &ad4080_regmap_config);
  521. if (IS_ERR(st->regmap))
  522. return PTR_ERR(st->regmap);
  523. st->info = spi_get_device_match_data(spi);
  524. if (!st->info)
  525. return -ENODEV;
  526. ret = devm_mutex_init(dev, &st->lock);
  527. if (ret)
  528. return ret;
  529. indio_dev->name = st->info->name;
  530. indio_dev->channels = st->info->channels;
  531. indio_dev->num_channels = st->info->num_channels;
  532. indio_dev->info = &ad4080_iio_info;
  533. ret = ad4080_properties_parse(st);
  534. if (ret)
  535. return ret;
  536. clk = devm_clk_get_enabled(&spi->dev, "cnv");
  537. if (IS_ERR(clk))
  538. return PTR_ERR(clk);
  539. st->clk_rate = clk_get_rate(clk);
  540. st->back = devm_iio_backend_get(dev, NULL);
  541. if (IS_ERR(st->back))
  542. return PTR_ERR(st->back);
  543. ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
  544. if (ret)
  545. return ret;
  546. ret = devm_iio_backend_enable(dev, st->back);
  547. if (ret)
  548. return ret;
  549. ret = ad4080_setup(indio_dev);
  550. if (ret)
  551. return ret;
  552. return devm_iio_device_register(&spi->dev, indio_dev);
  553. }
  554. static const struct spi_device_id ad4080_id[] = {
  555. { "ad4080", (kernel_ulong_t)&ad4080_chip_info },
  556. { "ad4081", (kernel_ulong_t)&ad4081_chip_info },
  557. { "ad4083", (kernel_ulong_t)&ad4083_chip_info },
  558. { "ad4084", (kernel_ulong_t)&ad4084_chip_info },
  559. { "ad4086", (kernel_ulong_t)&ad4086_chip_info },
  560. { "ad4087", (kernel_ulong_t)&ad4087_chip_info },
  561. { }
  562. };
  563. MODULE_DEVICE_TABLE(spi, ad4080_id);
  564. static const struct of_device_id ad4080_of_match[] = {
  565. { .compatible = "adi,ad4080", &ad4080_chip_info },
  566. { .compatible = "adi,ad4081", &ad4081_chip_info },
  567. { .compatible = "adi,ad4083", &ad4083_chip_info },
  568. { .compatible = "adi,ad4084", &ad4084_chip_info },
  569. { .compatible = "adi,ad4086", &ad4086_chip_info },
  570. { .compatible = "adi,ad4087", &ad4087_chip_info },
  571. { }
  572. };
  573. MODULE_DEVICE_TABLE(of, ad4080_of_match);
  574. static struct spi_driver ad4080_driver = {
  575. .driver = {
  576. .name = "ad4080",
  577. .of_match_table = ad4080_of_match,
  578. },
  579. .probe = ad4080_probe,
  580. .id_table = ad4080_id,
  581. };
  582. module_spi_driver(ad4080_driver);
  583. MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com");
  584. MODULE_DESCRIPTION("Analog Devices AD4080");
  585. MODULE_LICENSE("GPL");
  586. MODULE_IMPORT_NS("IIO_BACKEND");