ad7192.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AD7192 and similar SPI ADC driver
  4. *
  5. * Copyright 2011-2015 Analog Devices Inc.
  6. */
  7. #include <linux/interrupt.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/err.h>
  18. #include <linux/sched.h>
  19. #include <linux/delay.h>
  20. #include <linux/module.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/property.h>
  23. #include <linux/units.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/sysfs.h>
  26. #include <linux/iio/buffer.h>
  27. #include <linux/iio/trigger.h>
  28. #include <linux/iio/trigger_consumer.h>
  29. #include <linux/iio/triggered_buffer.h>
  30. #include <linux/iio/adc/ad_sigma_delta.h>
  31. /* Registers */
  32. #define AD7192_REG_COMM 0 /* Communications Register (WO, 8-bit) */
  33. #define AD7192_REG_STAT 0 /* Status Register (RO, 8-bit) */
  34. #define AD7192_REG_MODE 1 /* Mode Register (RW, 24-bit */
  35. #define AD7192_REG_CONF 2 /* Configuration Register (RW, 24-bit) */
  36. #define AD7192_REG_DATA 3 /* Data Register (RO, 24/32-bit) */
  37. #define AD7192_REG_ID 4 /* ID Register (RO, 8-bit) */
  38. #define AD7192_REG_GPOCON 5 /* GPOCON Register (RO, 8-bit) */
  39. #define AD7192_REG_OFFSET 6 /* Offset Register (RW, 16-bit */
  40. /* (AD7792)/24-bit (AD7192)) */
  41. #define AD7192_REG_FULLSALE 7 /* Full-Scale Register */
  42. /* (RW, 16-bit (AD7792)/24-bit (AD7192)) */
  43. /* Communications Register Bit Designations (AD7192_REG_COMM) */
  44. #define AD7192_COMM_WEN BIT(7) /* Write Enable */
  45. #define AD7192_COMM_WRITE 0 /* Write Operation */
  46. #define AD7192_COMM_READ BIT(6) /* Read Operation */
  47. #define AD7192_COMM_ADDR_MASK GENMASK(5, 3) /* Register Address Mask */
  48. #define AD7192_COMM_CREAD BIT(2) /* Continuous Read of Data Register */
  49. /* Status Register Bit Designations (AD7192_REG_STAT) */
  50. #define AD7192_STAT_RDY BIT(7) /* Ready */
  51. #define AD7192_STAT_ERR BIT(6) /* Error (Overrange, Underrange) */
  52. #define AD7192_STAT_NOREF BIT(5) /* Error no external reference */
  53. #define AD7192_STAT_PARITY BIT(4) /* Parity */
  54. #define AD7192_STAT_CH3 BIT(2) /* Channel 3 */
  55. #define AD7192_STAT_CH2 BIT(1) /* Channel 2 */
  56. #define AD7192_STAT_CH1 BIT(0) /* Channel 1 */
  57. /* Mode Register Bit Designations (AD7192_REG_MODE) */
  58. #define AD7192_MODE_SEL_MASK GENMASK(23, 21) /* Operation Mode Select Mask */
  59. #define AD7192_MODE_STA_MASK BIT(20) /* Status Register transmission Mask */
  60. #define AD7192_MODE_CLKSRC_MASK GENMASK(19, 18) /* Clock Source Select Mask */
  61. #define AD7192_MODE_AVG_MASK GENMASK(17, 16)
  62. /* Fast Settling Filter Average Select Mask (AD7193 only) */
  63. #define AD7192_MODE_SINC3 BIT(15) /* SINC3 Filter Select */
  64. #define AD7192_MODE_ENPAR BIT(13) /* Parity Enable */
  65. #define AD7192_MODE_CLKDIV BIT(12) /* Clock divide by 2 (AD7190/2 only)*/
  66. #define AD7192_MODE_SCYCLE BIT(11) /* Single cycle conversion */
  67. #define AD7192_MODE_REJ60 BIT(10) /* 50/60Hz notch filter */
  68. /* Filter Update Rate Select Mask */
  69. #define AD7192_MODE_RATE_MASK GENMASK(9, 0)
  70. /* Mode Register: AD7192_MODE_SEL options */
  71. #define AD7192_MODE_CONT 0 /* Continuous Conversion Mode */
  72. #define AD7192_MODE_SINGLE 1 /* Single Conversion Mode */
  73. #define AD7192_MODE_IDLE 2 /* Idle Mode */
  74. #define AD7192_MODE_PWRDN 3 /* Power-Down Mode */
  75. #define AD7192_MODE_CAL_INT_ZERO 4 /* Internal Zero-Scale Calibration */
  76. #define AD7192_MODE_CAL_INT_FULL 5 /* Internal Full-Scale Calibration */
  77. #define AD7192_MODE_CAL_SYS_ZERO 6 /* System Zero-Scale Calibration */
  78. #define AD7192_MODE_CAL_SYS_FULL 7 /* System Full-Scale Calibration */
  79. /* Mode Register: AD7192_MODE_CLKSRC options */
  80. #define AD7192_CLK_EXT_MCLK1_2 0 /* External 4.92 MHz Clock connected*/
  81. /* from MCLK1 to MCLK2 */
  82. #define AD7192_CLK_EXT_MCLK2 1 /* External Clock applied to MCLK2 */
  83. #define AD7192_CLK_INT 2 /* Internal 4.92 MHz Clock not */
  84. /* available at the MCLK2 pin */
  85. #define AD7192_CLK_INT_CO 3 /* Internal 4.92 MHz Clock available*/
  86. /* at the MCLK2 pin */
  87. /* Configuration Register Bit Designations (AD7192_REG_CONF) */
  88. #define AD7192_CONF_CHOP BIT(23) /* CHOP enable */
  89. #define AD7192_CONF_ACX BIT(22) /* AC excitation enable(AD7195 only) */
  90. #define AD7192_CONF_REFSEL BIT(20) /* REFIN1/REFIN2 Reference Select */
  91. #define AD7192_CONF_CHAN_MASK GENMASK(18, 8) /* Channel select mask */
  92. #define AD7192_CONF_BURN BIT(7) /* Burnout current enable */
  93. #define AD7192_CONF_REFDET BIT(6) /* Reference detect enable */
  94. #define AD7192_CONF_BUF BIT(4) /* Buffered Mode Enable */
  95. #define AD7192_CONF_UNIPOLAR BIT(3) /* Unipolar/Bipolar Enable */
  96. #define AD7192_CONF_GAIN_MASK GENMASK(2, 0) /* Gain Select */
  97. #define AD7192_CH_AIN1P_AIN2M BIT(0) /* AIN1(+) - AIN2(-) */
  98. #define AD7192_CH_AIN3P_AIN4M BIT(1) /* AIN3(+) - AIN4(-) */
  99. #define AD7192_CH_TEMP BIT(2) /* Temp Sensor */
  100. #define AD7192_CH_AIN2P_AIN2M BIT(3) /* AIN2(+) - AIN2(-) */
  101. #define AD7192_CH_AIN1 BIT(4) /* AIN1 - AINCOM */
  102. #define AD7192_CH_AIN2 BIT(5) /* AIN2 - AINCOM */
  103. #define AD7192_CH_AIN3 BIT(6) /* AIN3 - AINCOM */
  104. #define AD7192_CH_AIN4 BIT(7) /* AIN4 - AINCOM */
  105. #define AD7193_CH_AIN1P_AIN2M 0x001 /* AIN1(+) - AIN2(-) */
  106. #define AD7193_CH_AIN3P_AIN4M 0x002 /* AIN3(+) - AIN4(-) */
  107. #define AD7193_CH_AIN5P_AIN6M 0x004 /* AIN5(+) - AIN6(-) */
  108. #define AD7193_CH_AIN7P_AIN8M 0x008 /* AIN7(+) - AIN8(-) */
  109. #define AD7193_CH_TEMP 0x100 /* Temp senseor */
  110. #define AD7193_CH_AIN2P_AIN2M 0x200 /* AIN2(+) - AIN2(-) */
  111. #define AD7193_CH_AIN1 0x401 /* AIN1 - AINCOM */
  112. #define AD7193_CH_AIN2 0x402 /* AIN2 - AINCOM */
  113. #define AD7193_CH_AIN3 0x404 /* AIN3 - AINCOM */
  114. #define AD7193_CH_AIN4 0x408 /* AIN4 - AINCOM */
  115. #define AD7193_CH_AIN5 0x410 /* AIN5 - AINCOM */
  116. #define AD7193_CH_AIN6 0x420 /* AIN6 - AINCOM */
  117. #define AD7193_CH_AIN7 0x440 /* AIN7 - AINCOM */
  118. #define AD7193_CH_AIN8 0x480 /* AIN7 - AINCOM */
  119. #define AD7193_CH_AINCOM 0x600 /* AINCOM - AINCOM */
  120. #define AD7194_CH_POS(x) (((x) - 1) << 4)
  121. #define AD7194_CH_NEG(x) ((x) - 1)
  122. /* 10th bit corresponds to CON18(Pseudo) */
  123. #define AD7194_CH(p) (BIT(10) | AD7194_CH_POS(p))
  124. #define AD7194_DIFF_CH(p, n) (AD7194_CH_POS(p) | AD7194_CH_NEG(n))
  125. #define AD7194_CH_TEMP 0x100
  126. #define AD7194_CH_BASE_NR 2
  127. #define AD7194_CH_AIN_START 1
  128. #define AD7194_CH_AIN_NR 16
  129. #define AD7194_CH_MAX_NR 272
  130. /* ID Register Bit Designations (AD7192_REG_ID) */
  131. #define CHIPID_AD7190 0x4
  132. #define CHIPID_AD7192 0x0
  133. #define CHIPID_AD7193 0x2
  134. #define CHIPID_AD7194 0x3
  135. #define CHIPID_AD7195 0x6
  136. #define AD7192_ID_MASK GENMASK(3, 0)
  137. /* GPOCON Register Bit Designations (AD7192_REG_GPOCON) */
  138. #define AD7192_GPOCON_BPDSW BIT(6) /* Bridge power-down switch enable */
  139. #define AD7192_GPOCON_GP32EN BIT(5) /* Digital Output P3 and P2 enable */
  140. #define AD7192_GPOCON_GP10EN BIT(4) /* Digital Output P1 and P0 enable */
  141. #define AD7192_GPOCON_P3DAT BIT(3) /* P3 state */
  142. #define AD7192_GPOCON_P2DAT BIT(2) /* P2 state */
  143. #define AD7192_GPOCON_P1DAT BIT(1) /* P1 state */
  144. #define AD7192_GPOCON_P0DAT BIT(0) /* P0 state */
  145. #define AD7192_EXT_FREQ_MHZ_MIN 2457600
  146. #define AD7192_EXT_FREQ_MHZ_MAX 5120000
  147. #define AD7192_INT_FREQ_MHZ 4915200
  148. #define AD7192_NO_SYNC_FILTER 1
  149. #define AD7192_SYNC3_FILTER 3
  150. #define AD7192_SYNC4_FILTER 4
  151. /* NOTE:
  152. * The AD7190/2/5 features a dual use data out ready DOUT/RDY output.
  153. * In order to avoid contentions on the SPI bus, it's therefore necessary
  154. * to use spi bus locking.
  155. *
  156. * The DOUT/RDY output must also be wired to an interrupt capable GPIO.
  157. */
  158. enum {
  159. AD7192_SYSCALIB_ZERO_SCALE,
  160. AD7192_SYSCALIB_FULL_SCALE,
  161. };
  162. enum {
  163. ID_AD7190,
  164. ID_AD7192,
  165. ID_AD7193,
  166. ID_AD7194,
  167. ID_AD7195,
  168. };
  169. struct ad7192_chip_info {
  170. unsigned int chip_id;
  171. const char *name;
  172. const struct iio_chan_spec *channels;
  173. u8 num_channels;
  174. const struct ad_sigma_delta_info *sigma_delta_info;
  175. const struct iio_info *info;
  176. int (*parse_channels)(struct iio_dev *indio_dev);
  177. };
  178. struct ad7192_state {
  179. const struct ad7192_chip_info *chip_info;
  180. struct clk *mclk;
  181. struct clk_hw int_clk_hw;
  182. u16 int_vref_mv;
  183. u32 aincom_mv;
  184. u32 fclk;
  185. u32 mode;
  186. u32 conf;
  187. u32 scale_avail[8][2];
  188. u32 filter_freq_avail[4][2];
  189. u32 oversampling_ratio_avail[4];
  190. u8 gpocon;
  191. u8 clock_sel;
  192. struct mutex lock; /* protect sensor state */
  193. u8 syscalib_mode[8];
  194. struct ad_sigma_delta sd;
  195. };
  196. static const char * const ad7192_syscalib_modes[] = {
  197. [AD7192_SYSCALIB_ZERO_SCALE] = "zero_scale",
  198. [AD7192_SYSCALIB_FULL_SCALE] = "full_scale",
  199. };
  200. static int ad7192_set_syscalib_mode(struct iio_dev *indio_dev,
  201. const struct iio_chan_spec *chan,
  202. unsigned int mode)
  203. {
  204. struct ad7192_state *st = iio_priv(indio_dev);
  205. st->syscalib_mode[chan->channel] = mode;
  206. return 0;
  207. }
  208. static int ad7192_get_syscalib_mode(struct iio_dev *indio_dev,
  209. const struct iio_chan_spec *chan)
  210. {
  211. struct ad7192_state *st = iio_priv(indio_dev);
  212. return st->syscalib_mode[chan->channel];
  213. }
  214. static ssize_t ad7192_write_syscalib(struct iio_dev *indio_dev,
  215. uintptr_t private,
  216. const struct iio_chan_spec *chan,
  217. const char *buf, size_t len)
  218. {
  219. struct ad7192_state *st = iio_priv(indio_dev);
  220. bool sys_calib;
  221. int ret, temp;
  222. ret = kstrtobool(buf, &sys_calib);
  223. if (ret)
  224. return ret;
  225. temp = st->syscalib_mode[chan->channel];
  226. if (sys_calib) {
  227. if (temp == AD7192_SYSCALIB_ZERO_SCALE)
  228. ret = ad_sd_calibrate(&st->sd, AD7192_MODE_CAL_SYS_ZERO,
  229. chan->address);
  230. else
  231. ret = ad_sd_calibrate(&st->sd, AD7192_MODE_CAL_SYS_FULL,
  232. chan->address);
  233. }
  234. return ret ? ret : len;
  235. }
  236. static const struct iio_enum ad7192_syscalib_mode_enum = {
  237. .items = ad7192_syscalib_modes,
  238. .num_items = ARRAY_SIZE(ad7192_syscalib_modes),
  239. .set = ad7192_set_syscalib_mode,
  240. .get = ad7192_get_syscalib_mode
  241. };
  242. static const struct iio_chan_spec_ext_info ad7192_calibsys_ext_info[] = {
  243. {
  244. .name = "sys_calibration",
  245. .write = ad7192_write_syscalib,
  246. .shared = IIO_SEPARATE,
  247. },
  248. IIO_ENUM("sys_calibration_mode", IIO_SEPARATE,
  249. &ad7192_syscalib_mode_enum),
  250. IIO_ENUM_AVAILABLE("sys_calibration_mode", IIO_SHARED_BY_TYPE,
  251. &ad7192_syscalib_mode_enum),
  252. { }
  253. };
  254. static struct ad7192_state *ad_sigma_delta_to_ad7192(struct ad_sigma_delta *sd)
  255. {
  256. return container_of(sd, struct ad7192_state, sd);
  257. }
  258. static int ad7192_set_channel(struct ad_sigma_delta *sd, unsigned int channel)
  259. {
  260. struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
  261. st->conf &= ~AD7192_CONF_CHAN_MASK;
  262. st->conf |= FIELD_PREP(AD7192_CONF_CHAN_MASK, channel);
  263. return ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
  264. }
  265. static int ad7192_set_mode(struct ad_sigma_delta *sd,
  266. enum ad_sigma_delta_mode mode)
  267. {
  268. struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
  269. st->mode &= ~AD7192_MODE_SEL_MASK;
  270. st->mode |= FIELD_PREP(AD7192_MODE_SEL_MASK, mode);
  271. return ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
  272. }
  273. static int ad7192_append_status(struct ad_sigma_delta *sd, bool append)
  274. {
  275. struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
  276. unsigned int mode = st->mode;
  277. int ret;
  278. mode &= ~AD7192_MODE_STA_MASK;
  279. mode |= FIELD_PREP(AD7192_MODE_STA_MASK, append);
  280. ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, mode);
  281. if (ret < 0)
  282. return ret;
  283. st->mode = mode;
  284. return 0;
  285. }
  286. static int ad7192_disable_all(struct ad_sigma_delta *sd)
  287. {
  288. struct ad7192_state *st = ad_sigma_delta_to_ad7192(sd);
  289. u32 conf = st->conf;
  290. int ret;
  291. conf &= ~AD7192_CONF_CHAN_MASK;
  292. ret = ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, conf);
  293. if (ret < 0)
  294. return ret;
  295. st->conf = conf;
  296. return 0;
  297. }
  298. static const struct ad_sigma_delta_info ad7192_sigma_delta_info = {
  299. .set_channel = ad7192_set_channel,
  300. .append_status = ad7192_append_status,
  301. .disable_all = ad7192_disable_all,
  302. .set_mode = ad7192_set_mode,
  303. .has_registers = true,
  304. .addr_shift = 3,
  305. .read_mask = BIT(6),
  306. .status_ch_mask = GENMASK(3, 0),
  307. .num_slots = 4,
  308. .irq_flags = IRQF_TRIGGER_FALLING,
  309. };
  310. static const struct ad_sigma_delta_info ad7194_sigma_delta_info = {
  311. .set_channel = ad7192_set_channel,
  312. .append_status = ad7192_append_status,
  313. .disable_all = ad7192_disable_all,
  314. .set_mode = ad7192_set_mode,
  315. .has_registers = true,
  316. .addr_shift = 3,
  317. .read_mask = BIT(6),
  318. .status_ch_mask = GENMASK(3, 0),
  319. .irq_flags = IRQF_TRIGGER_FALLING,
  320. };
  321. static const struct ad_sd_calib_data ad7192_calib_arr[8] = {
  322. {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN1},
  323. {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN1},
  324. {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN2},
  325. {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN2},
  326. {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN3},
  327. {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN3},
  328. {AD7192_MODE_CAL_INT_ZERO, AD7192_CH_AIN4},
  329. {AD7192_MODE_CAL_INT_FULL, AD7192_CH_AIN4}
  330. };
  331. static int ad7192_calibrate_all(struct ad7192_state *st)
  332. {
  333. return ad_sd_calibrate_all(&st->sd, ad7192_calib_arr,
  334. ARRAY_SIZE(ad7192_calib_arr));
  335. }
  336. static inline bool ad7192_valid_external_frequency(u32 freq)
  337. {
  338. return (freq >= AD7192_EXT_FREQ_MHZ_MIN &&
  339. freq <= AD7192_EXT_FREQ_MHZ_MAX);
  340. }
  341. /*
  342. * Position 0 of ad7192_clock_names, xtal, corresponds to clock source
  343. * configuration AD7192_CLK_EXT_MCLK1_2 and position 1, mclk, corresponds to
  344. * AD7192_CLK_EXT_MCLK2
  345. */
  346. static const char *const ad7192_clock_names[] = {
  347. "xtal",
  348. "mclk"
  349. };
  350. static struct ad7192_state *clk_hw_to_ad7192(struct clk_hw *hw)
  351. {
  352. return container_of(hw, struct ad7192_state, int_clk_hw);
  353. }
  354. static unsigned long ad7192_clk_recalc_rate(struct clk_hw *hw,
  355. unsigned long parent_rate)
  356. {
  357. return AD7192_INT_FREQ_MHZ;
  358. }
  359. static int ad7192_clk_output_is_enabled(struct clk_hw *hw)
  360. {
  361. struct ad7192_state *st = clk_hw_to_ad7192(hw);
  362. return st->clock_sel == AD7192_CLK_INT_CO;
  363. }
  364. static int ad7192_clk_prepare(struct clk_hw *hw)
  365. {
  366. struct ad7192_state *st = clk_hw_to_ad7192(hw);
  367. int ret;
  368. st->mode &= ~AD7192_MODE_CLKSRC_MASK;
  369. st->mode |= AD7192_CLK_INT_CO;
  370. ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
  371. if (ret)
  372. return ret;
  373. st->clock_sel = AD7192_CLK_INT_CO;
  374. return 0;
  375. }
  376. static void ad7192_clk_unprepare(struct clk_hw *hw)
  377. {
  378. struct ad7192_state *st = clk_hw_to_ad7192(hw);
  379. int ret;
  380. st->mode &= ~AD7192_MODE_CLKSRC_MASK;
  381. st->mode |= AD7192_CLK_INT;
  382. ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
  383. if (ret)
  384. return;
  385. st->clock_sel = AD7192_CLK_INT;
  386. }
  387. static const struct clk_ops ad7192_int_clk_ops = {
  388. .recalc_rate = ad7192_clk_recalc_rate,
  389. .is_enabled = ad7192_clk_output_is_enabled,
  390. .prepare = ad7192_clk_prepare,
  391. .unprepare = ad7192_clk_unprepare,
  392. };
  393. static int ad7192_register_clk_provider(struct ad7192_state *st)
  394. {
  395. struct device *dev = &st->sd.spi->dev;
  396. struct clk_init_data init = {};
  397. int ret;
  398. if (!IS_ENABLED(CONFIG_COMMON_CLK))
  399. return 0;
  400. if (!device_property_present(dev, "#clock-cells"))
  401. return 0;
  402. init.name = devm_kasprintf(dev, GFP_KERNEL, "%s-clk",
  403. fwnode_get_name(dev_fwnode(dev)));
  404. if (!init.name)
  405. return -ENOMEM;
  406. init.ops = &ad7192_int_clk_ops;
  407. st->int_clk_hw.init = &init;
  408. ret = devm_clk_hw_register(dev, &st->int_clk_hw);
  409. if (ret)
  410. return ret;
  411. return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
  412. &st->int_clk_hw);
  413. }
  414. static int ad7192_clock_setup(struct ad7192_state *st)
  415. {
  416. struct device *dev = &st->sd.spi->dev;
  417. int ret;
  418. /*
  419. * The following two if branches are kept for backward compatibility but
  420. * the use of the two devicetree properties is highly discouraged. Clock
  421. * configuration should be done according to the bindings.
  422. */
  423. if (device_property_read_bool(dev, "adi,int-clock-output-enable")) {
  424. st->clock_sel = AD7192_CLK_INT_CO;
  425. st->fclk = AD7192_INT_FREQ_MHZ;
  426. dev_warn(dev, "Property adi,int-clock-output-enable is deprecated! Check bindings!\n");
  427. return 0;
  428. }
  429. if (device_property_read_bool(dev, "adi,clock-xtal")) {
  430. st->clock_sel = AD7192_CLK_EXT_MCLK1_2;
  431. st->mclk = devm_clk_get_enabled(dev, "mclk");
  432. if (IS_ERR(st->mclk))
  433. return dev_err_probe(dev, PTR_ERR(st->mclk),
  434. "Failed to get mclk\n");
  435. st->fclk = clk_get_rate(st->mclk);
  436. if (!ad7192_valid_external_frequency(st->fclk))
  437. return dev_err_probe(dev, -EINVAL,
  438. "External clock frequency out of bounds\n");
  439. dev_warn(dev, "Property adi,clock-xtal is deprecated! Check bindings!\n");
  440. return 0;
  441. }
  442. ret = device_property_match_property_string(dev, "clock-names",
  443. ad7192_clock_names,
  444. ARRAY_SIZE(ad7192_clock_names));
  445. if (ret < 0) {
  446. st->clock_sel = AD7192_CLK_INT;
  447. st->fclk = AD7192_INT_FREQ_MHZ;
  448. ret = ad7192_register_clk_provider(st);
  449. if (ret)
  450. return dev_err_probe(dev, ret,
  451. "Failed to register clock provider\n");
  452. return 0;
  453. }
  454. st->clock_sel = AD7192_CLK_EXT_MCLK1_2 + ret;
  455. st->mclk = devm_clk_get_enabled(dev, ad7192_clock_names[ret]);
  456. if (IS_ERR(st->mclk))
  457. return dev_err_probe(dev, PTR_ERR(st->mclk),
  458. "Failed to get clock source\n");
  459. st->fclk = clk_get_rate(st->mclk);
  460. if (!ad7192_valid_external_frequency(st->fclk))
  461. return dev_err_probe(dev, -EINVAL,
  462. "External clock frequency out of bounds\n");
  463. return 0;
  464. }
  465. static int ad7192_setup(struct iio_dev *indio_dev, struct device *dev)
  466. {
  467. struct ad7192_state *st = iio_priv(indio_dev);
  468. bool rej60_en, refin2_en;
  469. bool buf_en, bipolar, burnout_curr_en;
  470. unsigned long long scale_uv;
  471. int i, ret, id;
  472. /* reset the serial interface */
  473. ret = ad_sd_reset(&st->sd, 48);
  474. if (ret < 0)
  475. return ret;
  476. usleep_range(500, 1000); /* Wait for at least 500us */
  477. /* write/read test for device presence */
  478. ret = ad_sd_read_reg(&st->sd, AD7192_REG_ID, 1, &id);
  479. if (ret)
  480. return ret;
  481. id = FIELD_GET(AD7192_ID_MASK, id);
  482. if (id != st->chip_info->chip_id)
  483. dev_warn(dev, "device ID query failed (0x%X != 0x%X)\n",
  484. id, st->chip_info->chip_id);
  485. st->mode = FIELD_PREP(AD7192_MODE_SEL_MASK, AD7192_MODE_IDLE) |
  486. FIELD_PREP(AD7192_MODE_CLKSRC_MASK, st->clock_sel) |
  487. FIELD_PREP(AD7192_MODE_RATE_MASK, 480);
  488. st->conf = FIELD_PREP(AD7192_CONF_GAIN_MASK, 0);
  489. rej60_en = device_property_read_bool(dev, "adi,rejection-60-Hz-enable");
  490. if (rej60_en)
  491. st->mode |= AD7192_MODE_REJ60;
  492. refin2_en = device_property_read_bool(dev, "adi,refin2-pins-enable");
  493. if (refin2_en && st->chip_info->chip_id != CHIPID_AD7195)
  494. st->conf |= AD7192_CONF_REFSEL;
  495. st->conf &= ~AD7192_CONF_CHOP;
  496. buf_en = device_property_read_bool(dev, "adi,buffer-enable");
  497. if (buf_en)
  498. st->conf |= AD7192_CONF_BUF;
  499. bipolar = device_property_read_bool(dev, "bipolar");
  500. if (!bipolar)
  501. st->conf |= AD7192_CONF_UNIPOLAR;
  502. burnout_curr_en = device_property_read_bool(dev,
  503. "adi,burnout-currents-enable");
  504. if (burnout_curr_en && buf_en) {
  505. st->conf |= AD7192_CONF_BURN;
  506. } else if (burnout_curr_en) {
  507. dev_warn(dev,
  508. "Can't enable burnout currents: see CHOP or buffer\n");
  509. }
  510. ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
  511. if (ret)
  512. return ret;
  513. ret = ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
  514. if (ret)
  515. return ret;
  516. ret = ad7192_calibrate_all(st);
  517. if (ret)
  518. return ret;
  519. /* Populate available ADC input ranges */
  520. for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) {
  521. scale_uv = ((u64)st->int_vref_mv * 100000000)
  522. >> (indio_dev->channels[0].scan_type.realbits -
  523. !FIELD_GET(AD7192_CONF_UNIPOLAR, st->conf));
  524. scale_uv >>= i;
  525. st->scale_avail[i][1] = do_div(scale_uv, 100000000) * 10;
  526. st->scale_avail[i][0] = scale_uv;
  527. }
  528. st->oversampling_ratio_avail[0] = 1;
  529. st->oversampling_ratio_avail[1] = 2;
  530. st->oversampling_ratio_avail[2] = 8;
  531. st->oversampling_ratio_avail[3] = 16;
  532. st->filter_freq_avail[0][0] = 600;
  533. st->filter_freq_avail[1][0] = 800;
  534. st->filter_freq_avail[2][0] = 2300;
  535. st->filter_freq_avail[3][0] = 2720;
  536. st->filter_freq_avail[0][1] = 1000;
  537. st->filter_freq_avail[1][1] = 1000;
  538. st->filter_freq_avail[2][1] = 1000;
  539. st->filter_freq_avail[3][1] = 1000;
  540. return 0;
  541. }
  542. static ssize_t ad7192_show_ac_excitation(struct device *dev,
  543. struct device_attribute *attr,
  544. char *buf)
  545. {
  546. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  547. struct ad7192_state *st = iio_priv(indio_dev);
  548. return sysfs_emit(buf, "%ld\n", FIELD_GET(AD7192_CONF_ACX, st->conf));
  549. }
  550. static ssize_t ad7192_show_bridge_switch(struct device *dev,
  551. struct device_attribute *attr,
  552. char *buf)
  553. {
  554. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  555. struct ad7192_state *st = iio_priv(indio_dev);
  556. return sysfs_emit(buf, "%ld\n",
  557. FIELD_GET(AD7192_GPOCON_BPDSW, st->gpocon));
  558. }
  559. static ssize_t ad7192_set(struct device *dev,
  560. struct device_attribute *attr,
  561. const char *buf,
  562. size_t len)
  563. {
  564. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  565. struct ad7192_state *st = iio_priv(indio_dev);
  566. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  567. int ret;
  568. bool val;
  569. ret = kstrtobool(buf, &val);
  570. if (ret < 0)
  571. return ret;
  572. ret = iio_device_claim_direct_mode(indio_dev);
  573. if (ret)
  574. return ret;
  575. switch ((u32)this_attr->address) {
  576. case AD7192_REG_GPOCON:
  577. if (val)
  578. st->gpocon |= AD7192_GPOCON_BPDSW;
  579. else
  580. st->gpocon &= ~AD7192_GPOCON_BPDSW;
  581. ad_sd_write_reg(&st->sd, AD7192_REG_GPOCON, 1, st->gpocon);
  582. break;
  583. case AD7192_REG_CONF:
  584. if (val)
  585. st->conf |= AD7192_CONF_ACX;
  586. else
  587. st->conf &= ~AD7192_CONF_ACX;
  588. ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
  589. break;
  590. default:
  591. ret = -EINVAL;
  592. }
  593. iio_device_release_direct_mode(indio_dev);
  594. return ret ? ret : len;
  595. }
  596. static int ad7192_compute_f_order(struct ad7192_state *st, bool sinc3_en, bool chop_en)
  597. {
  598. u8 avg_factor_selected, oversampling_ratio;
  599. avg_factor_selected = FIELD_GET(AD7192_MODE_AVG_MASK, st->mode);
  600. if (!avg_factor_selected && !chop_en)
  601. return 1;
  602. oversampling_ratio = st->oversampling_ratio_avail[avg_factor_selected];
  603. if (sinc3_en)
  604. return AD7192_SYNC3_FILTER + oversampling_ratio - 1;
  605. return AD7192_SYNC4_FILTER + oversampling_ratio - 1;
  606. }
  607. static int ad7192_get_f_order(struct ad7192_state *st)
  608. {
  609. bool sinc3_en, chop_en;
  610. sinc3_en = FIELD_GET(AD7192_MODE_SINC3, st->mode);
  611. chop_en = FIELD_GET(AD7192_CONF_CHOP, st->conf);
  612. return ad7192_compute_f_order(st, sinc3_en, chop_en);
  613. }
  614. static int ad7192_compute_f_adc(struct ad7192_state *st, bool sinc3_en,
  615. bool chop_en)
  616. {
  617. unsigned int f_order = ad7192_compute_f_order(st, sinc3_en, chop_en);
  618. return DIV_ROUND_CLOSEST(st->fclk,
  619. f_order * FIELD_GET(AD7192_MODE_RATE_MASK, st->mode));
  620. }
  621. static int ad7192_get_f_adc(struct ad7192_state *st)
  622. {
  623. unsigned int f_order = ad7192_get_f_order(st);
  624. return DIV_ROUND_CLOSEST(st->fclk,
  625. f_order * FIELD_GET(AD7192_MODE_RATE_MASK, st->mode));
  626. }
  627. static void ad7192_update_filter_freq_avail(struct ad7192_state *st)
  628. {
  629. unsigned int fadc;
  630. /* Formulas for filter at page 25 of the datasheet */
  631. fadc = ad7192_compute_f_adc(st, false, true);
  632. st->filter_freq_avail[0][0] = DIV_ROUND_CLOSEST(fadc * 240, 1024);
  633. fadc = ad7192_compute_f_adc(st, true, true);
  634. st->filter_freq_avail[1][0] = DIV_ROUND_CLOSEST(fadc * 240, 1024);
  635. fadc = ad7192_compute_f_adc(st, false, false);
  636. st->filter_freq_avail[2][0] = DIV_ROUND_CLOSEST(fadc * 230, 1024);
  637. fadc = ad7192_compute_f_adc(st, true, false);
  638. st->filter_freq_avail[3][0] = DIV_ROUND_CLOSEST(fadc * 272, 1024);
  639. }
  640. static IIO_DEVICE_ATTR(bridge_switch_en, 0644,
  641. ad7192_show_bridge_switch, ad7192_set,
  642. AD7192_REG_GPOCON);
  643. static IIO_DEVICE_ATTR(ac_excitation_en, 0644,
  644. ad7192_show_ac_excitation, ad7192_set,
  645. AD7192_REG_CONF);
  646. static struct attribute *ad7192_attributes[] = {
  647. &iio_dev_attr_bridge_switch_en.dev_attr.attr,
  648. NULL
  649. };
  650. static const struct attribute_group ad7192_attribute_group = {
  651. .attrs = ad7192_attributes,
  652. };
  653. static struct attribute *ad7195_attributes[] = {
  654. &iio_dev_attr_bridge_switch_en.dev_attr.attr,
  655. &iio_dev_attr_ac_excitation_en.dev_attr.attr,
  656. NULL
  657. };
  658. static const struct attribute_group ad7195_attribute_group = {
  659. .attrs = ad7195_attributes,
  660. };
  661. static unsigned int ad7192_get_temp_scale(bool unipolar)
  662. {
  663. return unipolar ? 2815 * 2 : 2815;
  664. }
  665. static int ad7192_set_3db_filter_freq(struct ad7192_state *st,
  666. int val, int val2)
  667. {
  668. int i, ret, freq;
  669. unsigned int diff_new, diff_old;
  670. int idx = 0;
  671. diff_old = U32_MAX;
  672. freq = val * 1000 + val2;
  673. for (i = 0; i < ARRAY_SIZE(st->filter_freq_avail); i++) {
  674. diff_new = abs(freq - st->filter_freq_avail[i][0]);
  675. if (diff_new < diff_old) {
  676. diff_old = diff_new;
  677. idx = i;
  678. }
  679. }
  680. switch (idx) {
  681. case 0:
  682. st->mode &= ~AD7192_MODE_SINC3;
  683. st->conf |= AD7192_CONF_CHOP;
  684. break;
  685. case 1:
  686. st->mode |= AD7192_MODE_SINC3;
  687. st->conf |= AD7192_CONF_CHOP;
  688. break;
  689. case 2:
  690. st->mode &= ~AD7192_MODE_SINC3;
  691. st->conf &= ~AD7192_CONF_CHOP;
  692. break;
  693. case 3:
  694. st->mode |= AD7192_MODE_SINC3;
  695. st->conf &= ~AD7192_CONF_CHOP;
  696. break;
  697. }
  698. ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
  699. if (ret < 0)
  700. return ret;
  701. return ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, st->conf);
  702. }
  703. static int ad7192_get_3db_filter_freq(struct ad7192_state *st)
  704. {
  705. unsigned int fadc;
  706. fadc = ad7192_get_f_adc(st);
  707. if (FIELD_GET(AD7192_CONF_CHOP, st->conf))
  708. return DIV_ROUND_CLOSEST(fadc * 240, 1024);
  709. if (FIELD_GET(AD7192_MODE_SINC3, st->mode))
  710. return DIV_ROUND_CLOSEST(fadc * 272, 1024);
  711. else
  712. return DIV_ROUND_CLOSEST(fadc * 230, 1024);
  713. }
  714. static int ad7192_read_raw(struct iio_dev *indio_dev,
  715. struct iio_chan_spec const *chan,
  716. int *val,
  717. int *val2,
  718. long m)
  719. {
  720. struct ad7192_state *st = iio_priv(indio_dev);
  721. bool unipolar = FIELD_GET(AD7192_CONF_UNIPOLAR, st->conf);
  722. u8 gain = FIELD_GET(AD7192_CONF_GAIN_MASK, st->conf);
  723. switch (m) {
  724. case IIO_CHAN_INFO_RAW:
  725. return ad_sigma_delta_single_conversion(indio_dev, chan, val);
  726. case IIO_CHAN_INFO_SCALE:
  727. switch (chan->type) {
  728. case IIO_VOLTAGE:
  729. mutex_lock(&st->lock);
  730. *val = st->scale_avail[gain][0];
  731. *val2 = st->scale_avail[gain][1];
  732. mutex_unlock(&st->lock);
  733. return IIO_VAL_INT_PLUS_NANO;
  734. case IIO_TEMP:
  735. *val = 0;
  736. *val2 = 1000000000 / ad7192_get_temp_scale(unipolar);
  737. return IIO_VAL_INT_PLUS_NANO;
  738. default:
  739. return -EINVAL;
  740. }
  741. case IIO_CHAN_INFO_OFFSET:
  742. if (!unipolar)
  743. *val = -(1 << (chan->scan_type.realbits - 1));
  744. else
  745. *val = 0;
  746. switch (chan->type) {
  747. case IIO_VOLTAGE:
  748. /*
  749. * Only applies to pseudo-differential inputs.
  750. * AINCOM voltage has to be converted to "raw" units.
  751. */
  752. if (st->aincom_mv && !chan->differential)
  753. *val += DIV_ROUND_CLOSEST_ULL((u64)st->aincom_mv * NANO,
  754. st->scale_avail[gain][1]);
  755. return IIO_VAL_INT;
  756. /* Kelvin to Celsius */
  757. case IIO_TEMP:
  758. *val -= 273 * ad7192_get_temp_scale(unipolar);
  759. return IIO_VAL_INT;
  760. default:
  761. return -EINVAL;
  762. }
  763. case IIO_CHAN_INFO_SAMP_FREQ:
  764. *val = DIV_ROUND_CLOSEST(ad7192_get_f_adc(st), 1024);
  765. return IIO_VAL_INT;
  766. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  767. *val = ad7192_get_3db_filter_freq(st);
  768. *val2 = 1000;
  769. return IIO_VAL_FRACTIONAL;
  770. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  771. *val = st->oversampling_ratio_avail[FIELD_GET(AD7192_MODE_AVG_MASK, st->mode)];
  772. return IIO_VAL_INT;
  773. }
  774. return -EINVAL;
  775. }
  776. static int ad7192_write_raw(struct iio_dev *indio_dev,
  777. struct iio_chan_spec const *chan,
  778. int val,
  779. int val2,
  780. long mask)
  781. {
  782. struct ad7192_state *st = iio_priv(indio_dev);
  783. int ret, i, div;
  784. unsigned int tmp;
  785. ret = iio_device_claim_direct_mode(indio_dev);
  786. if (ret)
  787. return ret;
  788. mutex_lock(&st->lock);
  789. switch (mask) {
  790. case IIO_CHAN_INFO_SCALE:
  791. ret = -EINVAL;
  792. for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
  793. if (val2 == st->scale_avail[i][1]) {
  794. ret = 0;
  795. tmp = st->conf;
  796. st->conf &= ~AD7192_CONF_GAIN_MASK;
  797. st->conf |= FIELD_PREP(AD7192_CONF_GAIN_MASK, i);
  798. if (tmp == st->conf)
  799. break;
  800. ad_sd_write_reg(&st->sd, AD7192_REG_CONF,
  801. 3, st->conf);
  802. ad7192_calibrate_all(st);
  803. break;
  804. }
  805. break;
  806. case IIO_CHAN_INFO_SAMP_FREQ:
  807. if (!val) {
  808. ret = -EINVAL;
  809. break;
  810. }
  811. div = st->fclk / (val * ad7192_get_f_order(st) * 1024);
  812. if (div < 1 || div > 1023) {
  813. ret = -EINVAL;
  814. break;
  815. }
  816. st->mode &= ~AD7192_MODE_RATE_MASK;
  817. st->mode |= FIELD_PREP(AD7192_MODE_RATE_MASK, div);
  818. ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode);
  819. ad7192_update_filter_freq_avail(st);
  820. break;
  821. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  822. ret = ad7192_set_3db_filter_freq(st, val, val2 / 1000);
  823. break;
  824. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  825. ret = -EINVAL;
  826. for (i = 0; i < ARRAY_SIZE(st->oversampling_ratio_avail); i++)
  827. if (val == st->oversampling_ratio_avail[i]) {
  828. ret = 0;
  829. tmp = st->mode;
  830. st->mode &= ~AD7192_MODE_AVG_MASK;
  831. st->mode |= FIELD_PREP(AD7192_MODE_AVG_MASK, i);
  832. if (tmp == st->mode)
  833. break;
  834. ad_sd_write_reg(&st->sd, AD7192_REG_MODE,
  835. 3, st->mode);
  836. break;
  837. }
  838. ad7192_update_filter_freq_avail(st);
  839. break;
  840. default:
  841. ret = -EINVAL;
  842. }
  843. mutex_unlock(&st->lock);
  844. iio_device_release_direct_mode(indio_dev);
  845. return ret;
  846. }
  847. static int ad7192_write_raw_get_fmt(struct iio_dev *indio_dev,
  848. struct iio_chan_spec const *chan,
  849. long mask)
  850. {
  851. switch (mask) {
  852. case IIO_CHAN_INFO_SCALE:
  853. return IIO_VAL_INT_PLUS_NANO;
  854. case IIO_CHAN_INFO_SAMP_FREQ:
  855. return IIO_VAL_INT;
  856. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  857. return IIO_VAL_INT_PLUS_MICRO;
  858. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  859. return IIO_VAL_INT;
  860. default:
  861. return -EINVAL;
  862. }
  863. }
  864. static int ad7192_read_avail(struct iio_dev *indio_dev,
  865. struct iio_chan_spec const *chan,
  866. const int **vals, int *type, int *length,
  867. long mask)
  868. {
  869. struct ad7192_state *st = iio_priv(indio_dev);
  870. switch (mask) {
  871. case IIO_CHAN_INFO_SCALE:
  872. *vals = (int *)st->scale_avail;
  873. *type = IIO_VAL_INT_PLUS_NANO;
  874. /* Values are stored in a 2D matrix */
  875. *length = ARRAY_SIZE(st->scale_avail) * 2;
  876. return IIO_AVAIL_LIST;
  877. case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
  878. *vals = (int *)st->filter_freq_avail;
  879. *type = IIO_VAL_FRACTIONAL;
  880. *length = ARRAY_SIZE(st->filter_freq_avail) * 2;
  881. return IIO_AVAIL_LIST;
  882. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  883. *vals = (int *)st->oversampling_ratio_avail;
  884. *type = IIO_VAL_INT;
  885. *length = ARRAY_SIZE(st->oversampling_ratio_avail);
  886. return IIO_AVAIL_LIST;
  887. }
  888. return -EINVAL;
  889. }
  890. static int ad7192_update_scan_mode(struct iio_dev *indio_dev, const unsigned long *scan_mask)
  891. {
  892. struct ad7192_state *st = iio_priv(indio_dev);
  893. u32 conf = st->conf;
  894. int ret;
  895. int i;
  896. conf &= ~AD7192_CONF_CHAN_MASK;
  897. for_each_set_bit(i, scan_mask, 8)
  898. conf |= FIELD_PREP(AD7192_CONF_CHAN_MASK, i);
  899. ret = ad_sd_write_reg(&st->sd, AD7192_REG_CONF, 3, conf);
  900. if (ret < 0)
  901. return ret;
  902. st->conf = conf;
  903. return 0;
  904. }
  905. static const struct iio_info ad7192_info = {
  906. .read_raw = ad7192_read_raw,
  907. .write_raw = ad7192_write_raw,
  908. .write_raw_get_fmt = ad7192_write_raw_get_fmt,
  909. .read_avail = ad7192_read_avail,
  910. .attrs = &ad7192_attribute_group,
  911. .validate_trigger = ad_sd_validate_trigger,
  912. .update_scan_mode = ad7192_update_scan_mode,
  913. };
  914. static const struct iio_info ad7194_info = {
  915. .read_raw = ad7192_read_raw,
  916. .write_raw = ad7192_write_raw,
  917. .write_raw_get_fmt = ad7192_write_raw_get_fmt,
  918. .read_avail = ad7192_read_avail,
  919. .validate_trigger = ad_sd_validate_trigger,
  920. };
  921. static const struct iio_info ad7195_info = {
  922. .read_raw = ad7192_read_raw,
  923. .write_raw = ad7192_write_raw,
  924. .write_raw_get_fmt = ad7192_write_raw_get_fmt,
  925. .read_avail = ad7192_read_avail,
  926. .attrs = &ad7195_attribute_group,
  927. .validate_trigger = ad_sd_validate_trigger,
  928. .update_scan_mode = ad7192_update_scan_mode,
  929. };
  930. #define __AD719x_CHANNEL(_si, _channel1, _channel2, _address, _type, \
  931. _mask_all, _mask_type_av, _mask_all_av, _ext_info) \
  932. { \
  933. .type = (_type), \
  934. .differential = ((_channel2) == -1 ? 0 : 1), \
  935. .indexed = 1, \
  936. .channel = (_channel1), \
  937. .channel2 = (_channel2), \
  938. .address = (_address), \
  939. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  940. BIT(IIO_CHAN_INFO_OFFSET), \
  941. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  942. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  943. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
  944. (_mask_all), \
  945. .info_mask_shared_by_type_available = (_mask_type_av), \
  946. .info_mask_shared_by_all_available = \
  947. BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
  948. (_mask_all_av), \
  949. .ext_info = (_ext_info), \
  950. .scan_index = (_si), \
  951. .scan_type = { \
  952. .sign = 'u', \
  953. .realbits = 24, \
  954. .storagebits = 32, \
  955. .endianness = IIO_BE, \
  956. }, \
  957. }
  958. #define AD719x_DIFF_CHANNEL(_si, _channel1, _channel2, _address) \
  959. __AD719x_CHANNEL(_si, _channel1, _channel2, _address, IIO_VOLTAGE, 0, \
  960. BIT(IIO_CHAN_INFO_SCALE), 0, ad7192_calibsys_ext_info)
  961. #define AD719x_CHANNEL(_si, _channel1, _address) \
  962. __AD719x_CHANNEL(_si, _channel1, -1, _address, IIO_VOLTAGE, 0, \
  963. BIT(IIO_CHAN_INFO_SCALE), 0, ad7192_calibsys_ext_info)
  964. #define AD719x_TEMP_CHANNEL(_si, _address) \
  965. __AD719x_CHANNEL(_si, 0, -1, _address, IIO_TEMP, 0, 0, 0, NULL)
  966. #define AD7193_DIFF_CHANNEL(_si, _channel1, _channel2, _address) \
  967. __AD719x_CHANNEL(_si, _channel1, _channel2, _address, \
  968. IIO_VOLTAGE, \
  969. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  970. BIT(IIO_CHAN_INFO_SCALE), \
  971. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  972. ad7192_calibsys_ext_info)
  973. #define AD7193_CHANNEL(_si, _channel1, _address) \
  974. AD7193_DIFF_CHANNEL(_si, _channel1, -1, _address)
  975. static const struct iio_chan_spec ad7192_channels[] = {
  976. AD719x_DIFF_CHANNEL(0, 1, 2, AD7192_CH_AIN1P_AIN2M),
  977. AD719x_DIFF_CHANNEL(1, 3, 4, AD7192_CH_AIN3P_AIN4M),
  978. AD719x_TEMP_CHANNEL(2, AD7192_CH_TEMP),
  979. AD719x_DIFF_CHANNEL(3, 2, 2, AD7192_CH_AIN2P_AIN2M),
  980. AD719x_CHANNEL(4, 1, AD7192_CH_AIN1),
  981. AD719x_CHANNEL(5, 2, AD7192_CH_AIN2),
  982. AD719x_CHANNEL(6, 3, AD7192_CH_AIN3),
  983. AD719x_CHANNEL(7, 4, AD7192_CH_AIN4),
  984. IIO_CHAN_SOFT_TIMESTAMP(8),
  985. };
  986. static const struct iio_chan_spec ad7193_channels[] = {
  987. AD7193_DIFF_CHANNEL(0, 1, 2, AD7193_CH_AIN1P_AIN2M),
  988. AD7193_DIFF_CHANNEL(1, 3, 4, AD7193_CH_AIN3P_AIN4M),
  989. AD7193_DIFF_CHANNEL(2, 5, 6, AD7193_CH_AIN5P_AIN6M),
  990. AD7193_DIFF_CHANNEL(3, 7, 8, AD7193_CH_AIN7P_AIN8M),
  991. AD719x_TEMP_CHANNEL(4, AD7193_CH_TEMP),
  992. AD7193_DIFF_CHANNEL(5, 2, 2, AD7193_CH_AIN2P_AIN2M),
  993. AD7193_CHANNEL(6, 1, AD7193_CH_AIN1),
  994. AD7193_CHANNEL(7, 2, AD7193_CH_AIN2),
  995. AD7193_CHANNEL(8, 3, AD7193_CH_AIN3),
  996. AD7193_CHANNEL(9, 4, AD7193_CH_AIN4),
  997. AD7193_CHANNEL(10, 5, AD7193_CH_AIN5),
  998. AD7193_CHANNEL(11, 6, AD7193_CH_AIN6),
  999. AD7193_CHANNEL(12, 7, AD7193_CH_AIN7),
  1000. AD7193_CHANNEL(13, 8, AD7193_CH_AIN8),
  1001. IIO_CHAN_SOFT_TIMESTAMP(14),
  1002. };
  1003. static bool ad7194_validate_ain_channel(struct device *dev, u32 ain)
  1004. {
  1005. return in_range(ain, AD7194_CH_AIN_START, AD7194_CH_AIN_NR);
  1006. }
  1007. static int ad7194_parse_channels(struct iio_dev *indio_dev)
  1008. {
  1009. struct device *dev = indio_dev->dev.parent;
  1010. struct iio_chan_spec *ad7194_channels;
  1011. const struct iio_chan_spec ad7194_chan = AD7193_CHANNEL(0, 0, 0);
  1012. const struct iio_chan_spec ad7194_chan_diff = AD7193_DIFF_CHANNEL(0, 0, 0, 0);
  1013. const struct iio_chan_spec ad7194_chan_temp = AD719x_TEMP_CHANNEL(0, 0);
  1014. const struct iio_chan_spec ad7194_chan_timestamp = IIO_CHAN_SOFT_TIMESTAMP(0);
  1015. unsigned int num_channels, index = 0;
  1016. u32 ain[2];
  1017. int ret;
  1018. num_channels = device_get_child_node_count(dev);
  1019. if (num_channels > AD7194_CH_MAX_NR)
  1020. return dev_err_probe(dev, -EINVAL, "Too many channels: %u\n",
  1021. num_channels);
  1022. num_channels += AD7194_CH_BASE_NR;
  1023. ad7194_channels = devm_kcalloc(dev, num_channels,
  1024. sizeof(*ad7194_channels), GFP_KERNEL);
  1025. if (!ad7194_channels)
  1026. return -ENOMEM;
  1027. indio_dev->channels = ad7194_channels;
  1028. indio_dev->num_channels = num_channels;
  1029. device_for_each_child_node_scoped(dev, child) {
  1030. ret = fwnode_property_read_u32_array(child, "diff-channels",
  1031. ain, ARRAY_SIZE(ain));
  1032. if (ret == 0) {
  1033. if (!ad7194_validate_ain_channel(dev, ain[0]))
  1034. return dev_err_probe(dev, -EINVAL,
  1035. "Invalid AIN channel: %u\n",
  1036. ain[0]);
  1037. if (!ad7194_validate_ain_channel(dev, ain[1]))
  1038. return dev_err_probe(dev, -EINVAL,
  1039. "Invalid AIN channel: %u\n",
  1040. ain[1]);
  1041. *ad7194_channels = ad7194_chan_diff;
  1042. ad7194_channels->scan_index = index++;
  1043. ad7194_channels->channel = ain[0];
  1044. ad7194_channels->channel2 = ain[1];
  1045. ad7194_channels->address = AD7194_DIFF_CH(ain[0], ain[1]);
  1046. } else {
  1047. ret = fwnode_property_read_u32(child, "single-channel",
  1048. &ain[0]);
  1049. if (ret)
  1050. return dev_err_probe(dev, ret,
  1051. "Missing channel property\n");
  1052. if (!ad7194_validate_ain_channel(dev, ain[0]))
  1053. return dev_err_probe(dev, -EINVAL,
  1054. "Invalid AIN channel: %u\n",
  1055. ain[0]);
  1056. *ad7194_channels = ad7194_chan;
  1057. ad7194_channels->scan_index = index++;
  1058. ad7194_channels->channel = ain[0];
  1059. ad7194_channels->address = AD7194_CH(ain[0]);
  1060. }
  1061. ad7194_channels++;
  1062. }
  1063. *ad7194_channels = ad7194_chan_temp;
  1064. ad7194_channels->scan_index = index++;
  1065. ad7194_channels->address = AD7194_CH_TEMP;
  1066. ad7194_channels++;
  1067. *ad7194_channels = ad7194_chan_timestamp;
  1068. ad7194_channels->scan_index = index;
  1069. return 0;
  1070. }
  1071. static const struct ad7192_chip_info ad7192_chip_info_tbl[] = {
  1072. [ID_AD7190] = {
  1073. .chip_id = CHIPID_AD7190,
  1074. .name = "ad7190",
  1075. .channels = ad7192_channels,
  1076. .num_channels = ARRAY_SIZE(ad7192_channels),
  1077. .sigma_delta_info = &ad7192_sigma_delta_info,
  1078. .info = &ad7192_info,
  1079. },
  1080. [ID_AD7192] = {
  1081. .chip_id = CHIPID_AD7192,
  1082. .name = "ad7192",
  1083. .channels = ad7192_channels,
  1084. .num_channels = ARRAY_SIZE(ad7192_channels),
  1085. .sigma_delta_info = &ad7192_sigma_delta_info,
  1086. .info = &ad7192_info,
  1087. },
  1088. [ID_AD7193] = {
  1089. .chip_id = CHIPID_AD7193,
  1090. .name = "ad7193",
  1091. .channels = ad7193_channels,
  1092. .num_channels = ARRAY_SIZE(ad7193_channels),
  1093. .sigma_delta_info = &ad7192_sigma_delta_info,
  1094. .info = &ad7192_info,
  1095. },
  1096. [ID_AD7194] = {
  1097. .chip_id = CHIPID_AD7194,
  1098. .name = "ad7194",
  1099. .info = &ad7194_info,
  1100. .sigma_delta_info = &ad7194_sigma_delta_info,
  1101. .parse_channels = ad7194_parse_channels,
  1102. },
  1103. [ID_AD7195] = {
  1104. .chip_id = CHIPID_AD7195,
  1105. .name = "ad7195",
  1106. .channels = ad7192_channels,
  1107. .num_channels = ARRAY_SIZE(ad7192_channels),
  1108. .sigma_delta_info = &ad7192_sigma_delta_info,
  1109. .info = &ad7195_info,
  1110. },
  1111. };
  1112. static int ad7192_probe(struct spi_device *spi)
  1113. {
  1114. struct device *dev = &spi->dev;
  1115. struct ad7192_state *st;
  1116. struct iio_dev *indio_dev;
  1117. int ret, avdd_mv;
  1118. if (!spi->irq)
  1119. return dev_err_probe(dev, -ENODEV, "Failed to get IRQ\n");
  1120. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  1121. if (!indio_dev)
  1122. return -ENOMEM;
  1123. st = iio_priv(indio_dev);
  1124. mutex_init(&st->lock);
  1125. /*
  1126. * Regulator aincom is optional to maintain compatibility with older DT.
  1127. * Newer firmware should provide a zero volt fixed supply if wired to
  1128. * ground.
  1129. */
  1130. ret = devm_regulator_get_enable_read_voltage(dev, "aincom");
  1131. if (ret < 0 && ret != -ENODEV)
  1132. return dev_err_probe(dev, ret, "Failed to get AINCOM voltage\n");
  1133. st->aincom_mv = ret == -ENODEV ? 0 : ret / MILLI;
  1134. /* AVDD can optionally be used as reference voltage */
  1135. ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
  1136. if (ret == -ENODEV || ret == -EINVAL) {
  1137. int ret2;
  1138. /*
  1139. * We get -EINVAL if avdd is a supply with unknown voltage. We
  1140. * still need to enable it since it is also a power supply.
  1141. */
  1142. ret2 = devm_regulator_get_enable(dev, "avdd");
  1143. if (ret2)
  1144. return dev_err_probe(dev, ret2,
  1145. "Failed to enable AVDD supply\n");
  1146. } else if (ret < 0) {
  1147. return dev_err_probe(dev, ret, "Failed to get AVDD voltage\n");
  1148. }
  1149. avdd_mv = ret == -ENODEV || ret == -EINVAL ? 0 : ret / MILLI;
  1150. ret = devm_regulator_get_enable(dev, "dvdd");
  1151. if (ret)
  1152. return dev_err_probe(dev, ret, "Failed to enable specified DVdd supply\n");
  1153. /*
  1154. * This is either REFIN1 or REFIN2 depending on adi,refin2-pins-enable.
  1155. * If this supply is not present, fall back to AVDD as reference.
  1156. */
  1157. ret = devm_regulator_get_enable_read_voltage(dev, "vref");
  1158. if (ret == -ENODEV) {
  1159. if (avdd_mv == 0)
  1160. return dev_err_probe(dev, -ENODEV,
  1161. "No reference voltage available\n");
  1162. } else if (ret < 0) {
  1163. return ret;
  1164. }
  1165. st->int_vref_mv = ret == -ENODEV ? avdd_mv : ret / MILLI;
  1166. st->chip_info = spi_get_device_match_data(spi);
  1167. if (!st->chip_info)
  1168. return -ENODEV;
  1169. indio_dev->name = st->chip_info->name;
  1170. indio_dev->modes = INDIO_DIRECT_MODE;
  1171. indio_dev->info = st->chip_info->info;
  1172. if (st->chip_info->parse_channels) {
  1173. ret = st->chip_info->parse_channels(indio_dev);
  1174. if (ret)
  1175. return ret;
  1176. } else {
  1177. indio_dev->channels = st->chip_info->channels;
  1178. indio_dev->num_channels = st->chip_info->num_channels;
  1179. }
  1180. ret = ad_sd_init(&st->sd, indio_dev, spi, st->chip_info->sigma_delta_info);
  1181. if (ret)
  1182. return ret;
  1183. ret = devm_ad_sd_setup_buffer_and_trigger(dev, indio_dev);
  1184. if (ret)
  1185. return ret;
  1186. ret = ad7192_clock_setup(st);
  1187. if (ret)
  1188. return ret;
  1189. ret = ad7192_setup(indio_dev, dev);
  1190. if (ret)
  1191. return ret;
  1192. return devm_iio_device_register(dev, indio_dev);
  1193. }
  1194. static const struct of_device_id ad7192_of_match[] = {
  1195. { .compatible = "adi,ad7190", .data = &ad7192_chip_info_tbl[ID_AD7190] },
  1196. { .compatible = "adi,ad7192", .data = &ad7192_chip_info_tbl[ID_AD7192] },
  1197. { .compatible = "adi,ad7193", .data = &ad7192_chip_info_tbl[ID_AD7193] },
  1198. { .compatible = "adi,ad7194", .data = &ad7192_chip_info_tbl[ID_AD7194] },
  1199. { .compatible = "adi,ad7195", .data = &ad7192_chip_info_tbl[ID_AD7195] },
  1200. { }
  1201. };
  1202. MODULE_DEVICE_TABLE(of, ad7192_of_match);
  1203. static const struct spi_device_id ad7192_ids[] = {
  1204. { "ad7190", (kernel_ulong_t)&ad7192_chip_info_tbl[ID_AD7190] },
  1205. { "ad7192", (kernel_ulong_t)&ad7192_chip_info_tbl[ID_AD7192] },
  1206. { "ad7193", (kernel_ulong_t)&ad7192_chip_info_tbl[ID_AD7193] },
  1207. { "ad7194", (kernel_ulong_t)&ad7192_chip_info_tbl[ID_AD7194] },
  1208. { "ad7195", (kernel_ulong_t)&ad7192_chip_info_tbl[ID_AD7195] },
  1209. { }
  1210. };
  1211. MODULE_DEVICE_TABLE(spi, ad7192_ids);
  1212. static struct spi_driver ad7192_driver = {
  1213. .driver = {
  1214. .name = "ad7192",
  1215. .of_match_table = ad7192_of_match,
  1216. },
  1217. .probe = ad7192_probe,
  1218. .id_table = ad7192_ids,
  1219. };
  1220. module_spi_driver(ad7192_driver);
  1221. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  1222. MODULE_DESCRIPTION("Analog Devices AD7192 and similar ADC");
  1223. MODULE_LICENSE("GPL v2");
  1224. MODULE_IMPORT_NS(IIO_AD_SIGMA_DELTA);