ad7192.c 41 KB

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