ad4170-4.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Analog Devices AD4170-4 ADC driver
  4. *
  5. * Copyright (C) 2025 Analog Devices, Inc.
  6. * Author: Ana-Maria Cusco <ana-maria.cusco@analog.com>
  7. * Author: Marcelo Schmitt <marcelo.schmitt@analog.com>
  8. */
  9. #include <linux/array_size.h>
  10. #include <linux/bitfield.h>
  11. #include <linux/bitmap.h>
  12. #include <linux/bitops.h>
  13. #include <linux/bits.h>
  14. #include <linux/cleanup.h>
  15. #include <linux/clk.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/err.h>
  20. #include <linux/gpio/driver.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/iio.h>
  23. #include <linux/iio/trigger.h>
  24. #include <linux/iio/trigger_consumer.h>
  25. #include <linux/iio/triggered_buffer.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/irq.h>
  28. #include <linux/math64.h>
  29. #include <linux/minmax.h>
  30. #include <linux/module.h>
  31. #include <linux/property.h>
  32. #include <linux/regmap.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/spi/spi.h>
  35. #include <linux/time.h>
  36. #include <linux/types.h>
  37. #include <linux/unaligned.h>
  38. #include <linux/units.h>
  39. #include <linux/util_macros.h>
  40. /*
  41. * AD4170 registers
  42. * Multibyte register addresses point to the most significant byte which is the
  43. * address to use to get the most significant byte first (address accessed is
  44. * decremented by one for each data byte)
  45. *
  46. * Each register address define follows the AD4170_<REG_NAME>_REG format.
  47. * Each mask follows the AD4170_<REG_NAME>_<FIELD_NAME> format.
  48. * E.g. AD4170_PIN_MUXING_DIG_AUX1_CTRL_MSK is for accessing DIG_AUX1_CTRL field
  49. * of PIN_MUXING_REG.
  50. * Each constant follows the AD4170_<REG_NAME>_<FIELD_NAME>_<FUNCTION> format.
  51. * E.g. AD4170_PIN_MUXING_DIG_AUX1_DISABLED is the value written to
  52. * DIG_AUX1_CTRL field of PIN_MUXING register to disable DIG_AUX1 pin.
  53. * Some register names and register field names are shortened versions of
  54. * their datasheet counterpart names to provide better code readability.
  55. */
  56. #define AD4170_CONFIG_A_REG 0x00
  57. #define AD4170_DATA_24B_REG 0x1E
  58. #define AD4170_PIN_MUXING_REG 0x69
  59. #define AD4170_CLOCK_CTRL_REG 0x6B
  60. #define AD4170_ADC_CTRL_REG 0x71
  61. #define AD4170_CHAN_EN_REG 0x79
  62. #define AD4170_CHAN_SETUP_REG(x) (0x81 + 4 * (x))
  63. #define AD4170_CHAN_MAP_REG(x) (0x83 + 4 * (x))
  64. #define AD4170_MISC_REG(x) (0xC1 + 14 * (x))
  65. #define AD4170_AFE_REG(x) (0xC3 + 14 * (x))
  66. #define AD4170_FILTER_REG(x) (0xC5 + 14 * (x))
  67. #define AD4170_FILTER_FS_REG(x) (0xC7 + 14 * (x))
  68. #define AD4170_OFFSET_REG(x) (0xCA + 14 * (x))
  69. #define AD4170_GAIN_REG(x) (0xCD + 14 * (x))
  70. #define AD4170_V_BIAS_REG 0x135
  71. #define AD4170_CURRENT_SRC_REG(x) (0x139 + 2 * (x))
  72. #define AD4170_GPIO_MODE_REG 0x191
  73. #define AD4170_GPIO_OUTPUT_REG 0x193
  74. #define AD4170_GPIO_INPUT_REG 0x195
  75. #define AD4170_ADC_CTRL_CONT_READ_EXIT_REG 0x200 /* virtual reg */
  76. #define AD4170_REG_READ_MASK BIT(14)
  77. /* AD4170_CONFIG_A_REG - INTERFACE_CONFIG_A REGISTER */
  78. #define AD4170_SW_RESET_MSK (BIT(7) | BIT(0))
  79. /* AD4170_PIN_MUXING_REG */
  80. #define AD4170_PIN_MUXING_DIG_AUX1_CTRL_MSK GENMASK(5, 4)
  81. /* AD4170_CLOCK_CTRL_REG */
  82. #define AD4170_CLOCK_CTRL_CLOCKSEL_MSK GENMASK(1, 0)
  83. /* AD4170_ADC_CTRL_REG */
  84. #define AD4170_ADC_CTRL_MULTI_DATA_REG_SEL_MSK BIT(7)
  85. #define AD4170_ADC_CTRL_CONT_READ_MSK GENMASK(5, 4)
  86. #define AD4170_ADC_CTRL_MODE_MSK GENMASK(3, 0)
  87. /* AD4170_CHAN_EN_REG */
  88. #define AD4170_CHAN_EN(ch) BIT(ch)
  89. /* AD4170_CHAN_SETUP_REG */
  90. #define AD4170_CHAN_SETUP_SETUP_MSK GENMASK(2, 0)
  91. /* AD4170_CHAN_MAP_REG */
  92. #define AD4170_CHAN_MAP_AINP_MSK GENMASK(12, 8)
  93. #define AD4170_CHAN_MAP_AINM_MSK GENMASK(4, 0)
  94. /* AD4170_MISC_REG */
  95. #define AD4170_MISC_CHOP_IEXC_MSK GENMASK(15, 14)
  96. #define AD4170_MISC_CHOP_ADC_MSK GENMASK(9, 8)
  97. /* AD4170_AFE_REG */
  98. #define AD4170_AFE_REF_BUF_M_MSK GENMASK(11, 10)
  99. #define AD4170_AFE_REF_BUF_P_MSK GENMASK(9, 8)
  100. #define AD4170_AFE_REF_SELECT_MSK GENMASK(6, 5)
  101. #define AD4170_AFE_BIPOLAR_MSK BIT(4)
  102. #define AD4170_AFE_PGA_GAIN_MSK GENMASK(3, 0)
  103. /* AD4170_FILTER_REG */
  104. #define AD4170_FILTER_FILTER_TYPE_MSK GENMASK(3, 0)
  105. /* AD4170_CURRENT_SRC_REG */
  106. #define AD4170_CURRENT_SRC_I_OUT_PIN_MSK GENMASK(12, 8)
  107. #define AD4170_CURRENT_SRC_I_OUT_VAL_MSK GENMASK(2, 0)
  108. /* AD4170_GPIO_MODE_REG */
  109. #define AD4170_GPIO_MODE_GPIO0_MSK GENMASK(1, 0)
  110. #define AD4170_GPIO_MODE_GPIO1_MSK GENMASK(3, 2)
  111. #define AD4170_GPIO_MODE_GPIO2_MSK GENMASK(5, 4)
  112. #define AD4170_GPIO_MODE_GPIO3_MSK GENMASK(7, 6)
  113. /* AD4170_GPIO_OUTPUT_REG */
  114. #define AD4170_GPIO_OUTPUT_GPIO_MSK(x) BIT(x)
  115. /* AD4170 register constants */
  116. /* AD4170_CLOCK_CTRL_REG constants */
  117. #define AD4170_CLOCK_CTRL_CLOCKSEL_INT 0x0
  118. #define AD4170_CLOCK_CTRL_CLOCKSEL_INT_OUT 0x1
  119. #define AD4170_CLOCK_CTRL_CLOCKSEL_EXT 0x2
  120. #define AD4170_CLOCK_CTRL_CLOCKSEL_EXT_XTAL 0x3
  121. /* AD4170_CHAN_MAP_REG constants */
  122. #define AD4170_CHAN_MAP_AIN(x) (x)
  123. #define AD4170_CHAN_MAP_TEMP_SENSOR 17
  124. #define AD4170_CHAN_MAP_AVDD_AVSS_P 18
  125. #define AD4170_CHAN_MAP_AVDD_AVSS_N 18
  126. #define AD4170_CHAN_MAP_IOVDD_DGND_P 19
  127. #define AD4170_CHAN_MAP_IOVDD_DGND_N 19
  128. #define AD4170_CHAN_MAP_AVSS 23
  129. #define AD4170_CHAN_MAP_DGND 24
  130. #define AD4170_CHAN_MAP_REFIN1_P 25
  131. #define AD4170_CHAN_MAP_REFIN1_N 26
  132. #define AD4170_CHAN_MAP_REFIN2_P 27
  133. #define AD4170_CHAN_MAP_REFIN2_N 28
  134. #define AD4170_CHAN_MAP_REFOUT 29
  135. /* AD4170_MISC_REG constants */
  136. #define AD4170_MISC_CHOP_IEXC_PAIR1 0x1
  137. #define AD4170_MISC_CHOP_IEXC_PAIR2 0x2
  138. #define AD4170_MISC_CHOP_IEXC_BOTH 0x3
  139. /* AD4170_PIN_MUXING_REG constants */
  140. #define AD4170_PIN_MUXING_DIG_AUX1_DISABLED 0x0
  141. #define AD4170_PIN_MUXING_DIG_AUX1_RDY 0x1
  142. /* AD4170_ADC_CTRL_REG constants */
  143. #define AD4170_ADC_CTRL_MODE_CONT 0x0
  144. #define AD4170_ADC_CTRL_MODE_SINGLE 0x4
  145. #define AD4170_ADC_CTRL_MODE_IDLE 0x7
  146. #define AD4170_ADC_CTRL_CONT_READ_DISABLE 0x0
  147. #define AD4170_ADC_CTRL_CONT_READ_ENABLE 0x1
  148. /* AD4170_FILTER_REG constants */
  149. #define AD4170_FILTER_FILTER_TYPE_SINC5_AVG 0x0
  150. #define AD4170_FILTER_FILTER_TYPE_SINC5 0x4
  151. #define AD4170_FILTER_FILTER_TYPE_SINC3 0x6
  152. /* AD4170_CURRENT_SRC_REG constants */
  153. #define AD4170_CURRENT_SRC_I_OUT_PIN_AIN(x) (x)
  154. #define AD4170_CURRENT_SRC_I_OUT_PIN_GPIO(x) ((x) + 17)
  155. /* AD4170_GPIO_MODE_REG constants */
  156. #define AD4170_GPIO_MODE_GPIO_INPUT 1
  157. #define AD4170_GPIO_MODE_GPIO_OUTPUT 2
  158. /* Device properties and auxiliary constants */
  159. #define AD4170_NUM_ANALOG_PINS 9
  160. #define AD4170_NUM_GPIO_PINS 4
  161. #define AD4170_MAX_ADC_CHANNELS 16
  162. #define AD4170_MAX_IIO_CHANNELS (AD4170_MAX_ADC_CHANNELS + 1)
  163. #define AD4170_MAX_ANALOG_PINS 8
  164. #define AD4170_MAX_SETUPS 8
  165. #define AD4170_INVALID_SETUP 9
  166. #define AD4170_SPI_INST_PHASE_LEN 2
  167. #define AD4170_SPI_MAX_XFER_LEN 6
  168. #define AD4170_NUM_CURRENT_SRC 4
  169. #define AD4170_DEFAULT_SAMP_RATE (125 * HZ_PER_KHZ)
  170. #define AD4170_INT_REF_2_5V 2500000
  171. /* Internal and external clock properties */
  172. #define AD4170_INT_CLOCK_16MHZ (16 * HZ_PER_MHZ)
  173. #define AD4170_EXT_CLOCK_MHZ_MIN (1 * HZ_PER_MHZ)
  174. #define AD4170_EXT_CLOCK_MHZ_MAX (17 * HZ_PER_MHZ)
  175. #define AD4170_NUM_PGA_OPTIONS 10
  176. /* Digital filter properties */
  177. #define AD4170_SINC3_MIN_FS 4
  178. #define AD4170_SINC3_MAX_FS 65532
  179. #define AD4170_SINC5_MIN_FS 1
  180. #define AD4170_SINC5_MAX_FS 256
  181. #define AD4170_GAIN_REG_DEFAULT 0x555555
  182. #define AD4170_ADC_CTRL_CONT_READ_EXIT 0xA5
  183. /* Analog pin functions */
  184. #define AD4170_PIN_UNASSIGNED 0x00
  185. #define AD4170_PIN_ANALOG_IN 0x01
  186. #define AD4170_PIN_CURRENT_OUT 0x02
  187. #define AD4170_PIN_VBIAS 0x04
  188. /* GPIO pin functions */
  189. #define AD4170_GPIO_UNASSIGNED 0x00
  190. #define AD4170_GPIO_AC_EXCITATION 0x02
  191. #define AD4170_GPIO_OUTPUT 0x04
  192. /* Current source */
  193. #define AD4170_CURRENT_SRC_DISABLED 0xFF
  194. static const unsigned int ad4170_reg_size[] = {
  195. [AD4170_CONFIG_A_REG] = 1,
  196. [AD4170_DATA_24B_REG] = 3,
  197. [AD4170_PIN_MUXING_REG] = 2,
  198. [AD4170_CLOCK_CTRL_REG] = 2,
  199. [AD4170_ADC_CTRL_REG] = 2,
  200. [AD4170_CHAN_EN_REG] = 2,
  201. /*
  202. * CHANNEL_SETUP and CHANNEL_MAP register are all 2 byte size each and
  203. * their addresses are interleaved such that we have CHANNEL_SETUP0
  204. * address followed by CHANNEL_MAP0 address, followed by CHANNEL_SETUP1,
  205. * and so on until CHANNEL_MAP15.
  206. * Thus, initialize the register size for them only once.
  207. */
  208. [AD4170_CHAN_SETUP_REG(0) ... AD4170_CHAN_MAP_REG(AD4170_MAX_ADC_CHANNELS - 1)] = 2,
  209. /*
  210. * MISC, AFE, FILTER, FILTER_FS, OFFSET, and GAIN register addresses are
  211. * also interleaved but MISC, AFE, FILTER, FILTER_FS, OFFSET are 16-bit
  212. * while OFFSET, GAIN are 24-bit registers so we can't init them all to
  213. * the same size.
  214. */
  215. [AD4170_MISC_REG(0) ... AD4170_FILTER_FS_REG(0)] = 2,
  216. [AD4170_MISC_REG(1) ... AD4170_FILTER_FS_REG(1)] = 2,
  217. [AD4170_MISC_REG(2) ... AD4170_FILTER_FS_REG(2)] = 2,
  218. [AD4170_MISC_REG(3) ... AD4170_FILTER_FS_REG(3)] = 2,
  219. [AD4170_MISC_REG(4) ... AD4170_FILTER_FS_REG(4)] = 2,
  220. [AD4170_MISC_REG(5) ... AD4170_FILTER_FS_REG(5)] = 2,
  221. [AD4170_MISC_REG(6) ... AD4170_FILTER_FS_REG(6)] = 2,
  222. [AD4170_MISC_REG(7) ... AD4170_FILTER_FS_REG(7)] = 2,
  223. [AD4170_OFFSET_REG(0) ... AD4170_GAIN_REG(0)] = 3,
  224. [AD4170_OFFSET_REG(1) ... AD4170_GAIN_REG(1)] = 3,
  225. [AD4170_OFFSET_REG(2) ... AD4170_GAIN_REG(2)] = 3,
  226. [AD4170_OFFSET_REG(3) ... AD4170_GAIN_REG(3)] = 3,
  227. [AD4170_OFFSET_REG(4) ... AD4170_GAIN_REG(4)] = 3,
  228. [AD4170_OFFSET_REG(5) ... AD4170_GAIN_REG(5)] = 3,
  229. [AD4170_OFFSET_REG(6) ... AD4170_GAIN_REG(6)] = 3,
  230. [AD4170_OFFSET_REG(7) ... AD4170_GAIN_REG(7)] = 3,
  231. [AD4170_V_BIAS_REG] = 2,
  232. [AD4170_CURRENT_SRC_REG(0) ... AD4170_CURRENT_SRC_REG(3)] = 2,
  233. [AD4170_GPIO_MODE_REG] = 2,
  234. [AD4170_GPIO_OUTPUT_REG] = 2,
  235. [AD4170_GPIO_INPUT_REG] = 2,
  236. [AD4170_ADC_CTRL_CONT_READ_EXIT_REG] = 0,
  237. };
  238. enum ad4170_ref_buf {
  239. AD4170_REF_BUF_PRE, /* Pre-charge referrence buffer */
  240. AD4170_REF_BUF_FULL, /* Full referrence buffering */
  241. AD4170_REF_BUF_BYPASS, /* Bypass referrence buffering */
  242. };
  243. /* maps adi,positive/negative-reference-buffer property values to enum */
  244. static const char * const ad4170_ref_buf_str[] = {
  245. [AD4170_REF_BUF_PRE] = "precharge",
  246. [AD4170_REF_BUF_FULL] = "full",
  247. [AD4170_REF_BUF_BYPASS] = "disabled",
  248. };
  249. enum ad4170_ref_select {
  250. AD4170_REF_REFIN1,
  251. AD4170_REF_REFIN2,
  252. AD4170_REF_REFOUT,
  253. AD4170_REF_AVDD,
  254. };
  255. enum ad4170_filter_type {
  256. AD4170_SINC5_AVG,
  257. AD4170_SINC5,
  258. AD4170_SINC3,
  259. };
  260. enum ad4170_regulator {
  261. AD4170_AVDD_SUP,
  262. AD4170_AVSS_SUP,
  263. AD4170_IOVDD_SUP,
  264. AD4170_REFIN1P_SUP,
  265. AD4170_REFIN1N_SUP,
  266. AD4170_REFIN2P_SUP,
  267. AD4170_REFIN2N_SUP,
  268. AD4170_MAX_SUP,
  269. };
  270. static const char *const ad4170_clk_sel[] = {
  271. "ext-clk", "xtal",
  272. };
  273. enum ad4170_int_pin_sel {
  274. AD4170_INT_PIN_SDO,
  275. AD4170_INT_PIN_DIG_AUX1,
  276. };
  277. static const char * const ad4170_int_pin_names[] = {
  278. [AD4170_INT_PIN_SDO] = "sdo",
  279. [AD4170_INT_PIN_DIG_AUX1] = "dig_aux1",
  280. };
  281. static const unsigned int ad4170_sinc3_filt_fs_tbl[] = {
  282. 4, 8, 12, 16, 20, 40, 48, 80, /* 0 - 7 */
  283. 100, 256, 500, 1000, 5000, 8332, 10000, 25000, /* 8 - 15 */
  284. 50000, 65532, /* 16 - 17 */
  285. };
  286. #define AD4170_MAX_FS_TBL_SIZE ARRAY_SIZE(ad4170_sinc3_filt_fs_tbl)
  287. static const unsigned int ad4170_sinc5_filt_fs_tbl[] = {
  288. 1, 2, 4, 8, 12, 16, 20, 40, 48, 80, 100, 256,
  289. };
  290. static const unsigned int ad4170_iout_pin_tbl[] = {
  291. AD4170_CURRENT_SRC_I_OUT_PIN_AIN(0),
  292. AD4170_CURRENT_SRC_I_OUT_PIN_AIN(1),
  293. AD4170_CURRENT_SRC_I_OUT_PIN_AIN(2),
  294. AD4170_CURRENT_SRC_I_OUT_PIN_AIN(3),
  295. AD4170_CURRENT_SRC_I_OUT_PIN_AIN(4),
  296. AD4170_CURRENT_SRC_I_OUT_PIN_AIN(5),
  297. AD4170_CURRENT_SRC_I_OUT_PIN_AIN(6),
  298. AD4170_CURRENT_SRC_I_OUT_PIN_AIN(7),
  299. AD4170_CURRENT_SRC_I_OUT_PIN_AIN(8),
  300. AD4170_CURRENT_SRC_I_OUT_PIN_GPIO(0),
  301. AD4170_CURRENT_SRC_I_OUT_PIN_GPIO(1),
  302. AD4170_CURRENT_SRC_I_OUT_PIN_GPIO(2),
  303. AD4170_CURRENT_SRC_I_OUT_PIN_GPIO(3),
  304. };
  305. static const unsigned int ad4170_iout_current_ua_tbl[] = {
  306. 0, 10, 50, 100, 250, 500, 1000, 1500,
  307. };
  308. enum ad4170_sensor_enum {
  309. AD4170_ADC_SENSOR = 0,
  310. AD4170_WEIGH_SCALE_SENSOR = 1,
  311. AD4170_RTD_SENSOR = 2,
  312. AD4170_THERMOCOUPLE_SENSOR = 3,
  313. };
  314. /* maps adi,sensor-type property value to enum */
  315. static const char * const ad4170_sensor_type[] = {
  316. [AD4170_ADC_SENSOR] = "adc",
  317. [AD4170_WEIGH_SCALE_SENSOR] = "weighscale",
  318. [AD4170_RTD_SENSOR] = "rtd",
  319. [AD4170_THERMOCOUPLE_SENSOR] = "thermocouple",
  320. };
  321. struct ad4170_chip_info {
  322. const char *name;
  323. };
  324. static const struct ad4170_chip_info ad4170_chip_info = {
  325. .name = "ad4170-4",
  326. };
  327. static const struct ad4170_chip_info ad4190_chip_info = {
  328. .name = "ad4190-4",
  329. };
  330. static const struct ad4170_chip_info ad4195_chip_info = {
  331. .name = "ad4195-4",
  332. };
  333. /*
  334. * There are 8 of each MISC, AFE, FILTER, FILTER_FS, OFFSET, and GAIN
  335. * configuration registers. That is, there are 8 miscellaneous registers, MISC0
  336. * to MISC7. Each MISC register is associated with a setup; MISCN is associated
  337. * with setup number N. The other 5 above mentioned types of registers have
  338. * analogous structure. A setup is a set of those registers. For example,
  339. * setup 1 comprises of MISC1, AFE1, FILTER1, FILTER_FS1, OFFSET1, and GAIN1
  340. * registers. Also, there are 16 CHANNEL_SETUP registers (CHANNEL_SETUP0 to
  341. * CHANNEL_SETUP15). Each channel setup is associated with one of the 8 possible
  342. * setups. Thus, AD4170 can support up to 16 channels but, since there are only
  343. * 8 available setups, channels must share settings if more than 8 channels are
  344. * configured.
  345. *
  346. * If this struct is modified, ad4170_setup_eq() will probably need to be
  347. * updated too.
  348. */
  349. struct ad4170_setup {
  350. u16 misc;
  351. u16 afe;
  352. u16 filter;
  353. u16 filter_fs;
  354. u32 offset; /* For calibration purposes */
  355. u32 gain; /* For calibration purposes */
  356. };
  357. struct ad4170_setup_info {
  358. struct ad4170_setup setup;
  359. unsigned int enabled_channels;
  360. unsigned int channels;
  361. };
  362. struct ad4170_chan_info {
  363. unsigned int input_range_uv;
  364. unsigned int setup_num; /* Index to access state setup_infos array */
  365. struct ad4170_setup setup; /* cached setup */
  366. int offset_tbl[10];
  367. u32 scale_tbl[10][2];
  368. bool initialized;
  369. bool enabled;
  370. };
  371. static const char * const ad4170_filt_names[] = {
  372. [AD4170_SINC5_AVG] = "sinc5+avg",
  373. [AD4170_SINC5] = "sinc5",
  374. [AD4170_SINC3] = "sinc3",
  375. };
  376. struct ad4170_state {
  377. struct mutex lock; /* Protect read-modify-write and multi write sequences */
  378. int vrefs_uv[AD4170_MAX_SUP];
  379. u32 mclk_hz;
  380. struct ad4170_setup_info setup_infos[AD4170_MAX_SETUPS];
  381. struct ad4170_chan_info chan_infos[AD4170_MAX_ADC_CHANNELS];
  382. struct completion completion;
  383. struct iio_chan_spec chans[AD4170_MAX_IIO_CHANNELS];
  384. struct spi_device *spi;
  385. struct regmap *regmap;
  386. int sps_tbl[ARRAY_SIZE(ad4170_filt_names)][AD4170_MAX_FS_TBL_SIZE][2];
  387. __be32 bounce_buffer[AD4170_MAX_ADC_CHANNELS];
  388. struct spi_message msg;
  389. struct spi_transfer xfer;
  390. struct iio_trigger *trig;
  391. struct clk_hw int_clk_hw;
  392. unsigned int clock_ctrl;
  393. unsigned int pins_fn[AD4170_NUM_ANALOG_PINS];
  394. int gpio_fn[AD4170_NUM_GPIO_PINS];
  395. unsigned int cur_src_pins[AD4170_NUM_CURRENT_SRC];
  396. struct gpio_chip gpiochip;
  397. /*
  398. * DMA (thus cache coherency maintenance) requires the transfer buffers
  399. * to live in their own cache lines.
  400. */
  401. u8 rx_buf[4] __aligned(IIO_DMA_MINALIGN);
  402. };
  403. static void ad4170_fill_sps_tbl(struct ad4170_state *st)
  404. {
  405. unsigned int tmp0, tmp1, i;
  406. /*
  407. * The ODR can be calculated the same way for sinc5+avg, sinc5, and
  408. * sinc3 filter types with the exception that sinc5 filter has a
  409. * narrowed range of allowed FILTER_FS values.
  410. */
  411. for (i = 0; i < ARRAY_SIZE(ad4170_sinc3_filt_fs_tbl); i++) {
  412. tmp0 = div_u64_rem(st->mclk_hz, 32 * ad4170_sinc3_filt_fs_tbl[i],
  413. &tmp1);
  414. tmp1 = mult_frac(tmp1, MICRO, 32 * ad4170_sinc3_filt_fs_tbl[i]);
  415. /* Fill sinc5+avg filter SPS table */
  416. st->sps_tbl[AD4170_SINC5_AVG][i][0] = tmp0; /* Integer part */
  417. st->sps_tbl[AD4170_SINC5_AVG][i][1] = tmp1; /* Fractional part */
  418. /* Fill sinc3 filter SPS table */
  419. st->sps_tbl[AD4170_SINC3][i][0] = tmp0; /* Integer part */
  420. st->sps_tbl[AD4170_SINC3][i][1] = tmp1; /* Fractional part */
  421. }
  422. /* Sinc5 filter ODR doesn't use all FILTER_FS bits */
  423. for (i = 0; i < ARRAY_SIZE(ad4170_sinc5_filt_fs_tbl); i++) {
  424. tmp0 = div_u64_rem(st->mclk_hz, 32 * ad4170_sinc5_filt_fs_tbl[i],
  425. &tmp1);
  426. tmp1 = mult_frac(tmp1, MICRO, 32 * ad4170_sinc5_filt_fs_tbl[i]);
  427. /* Fill sinc5 filter SPS table */
  428. st->sps_tbl[AD4170_SINC5][i][0] = tmp0; /* Integer part */
  429. st->sps_tbl[AD4170_SINC5][i][1] = tmp1; /* Fractional part */
  430. }
  431. }
  432. static int ad4170_debugfs_reg_access(struct iio_dev *indio_dev,
  433. unsigned int reg, unsigned int writeval,
  434. unsigned int *readval)
  435. {
  436. struct ad4170_state *st = iio_priv(indio_dev);
  437. if (readval)
  438. return regmap_read(st->regmap, reg, readval);
  439. return regmap_write(st->regmap, reg, writeval);
  440. }
  441. static int ad4170_get_reg_size(struct ad4170_state *st, unsigned int reg,
  442. unsigned int *size)
  443. {
  444. if (reg >= ARRAY_SIZE(ad4170_reg_size))
  445. return -EINVAL;
  446. *size = ad4170_reg_size[reg];
  447. return 0;
  448. }
  449. static int ad4170_reg_write(void *context, unsigned int reg, unsigned int val)
  450. {
  451. struct ad4170_state *st = context;
  452. u8 tx_buf[AD4170_SPI_MAX_XFER_LEN];
  453. unsigned int size;
  454. int ret;
  455. ret = ad4170_get_reg_size(st, reg, &size);
  456. if (ret)
  457. return ret;
  458. put_unaligned_be16(reg, tx_buf);
  459. switch (size) {
  460. case 3:
  461. put_unaligned_be24(val, &tx_buf[AD4170_SPI_INST_PHASE_LEN]);
  462. break;
  463. case 2:
  464. put_unaligned_be16(val, &tx_buf[AD4170_SPI_INST_PHASE_LEN]);
  465. break;
  466. case 1:
  467. tx_buf[AD4170_SPI_INST_PHASE_LEN] = val;
  468. break;
  469. case 0:
  470. /* Write continuous read exit code */
  471. tx_buf[0] = AD4170_ADC_CTRL_CONT_READ_EXIT;
  472. return spi_write_then_read(st->spi, tx_buf, 1, NULL, 0);
  473. default:
  474. return -EINVAL;
  475. }
  476. return spi_write_then_read(st->spi, tx_buf,
  477. AD4170_SPI_INST_PHASE_LEN + size, NULL, 0);
  478. }
  479. static int ad4170_reg_read(void *context, unsigned int reg, unsigned int *val)
  480. {
  481. struct ad4170_state *st = context;
  482. u8 tx_buf[AD4170_SPI_INST_PHASE_LEN];
  483. unsigned int size;
  484. int ret;
  485. put_unaligned_be16(AD4170_REG_READ_MASK | reg, tx_buf);
  486. ret = ad4170_get_reg_size(st, reg, &size);
  487. if (ret)
  488. return ret;
  489. ret = spi_write_then_read(st->spi, tx_buf, ARRAY_SIZE(tx_buf),
  490. st->rx_buf, size);
  491. if (ret)
  492. return ret;
  493. switch (size) {
  494. case 3:
  495. *val = get_unaligned_be24(st->rx_buf);
  496. return 0;
  497. case 2:
  498. *val = get_unaligned_be16(st->rx_buf);
  499. return 0;
  500. case 1:
  501. *val = st->rx_buf[0];
  502. return 0;
  503. default:
  504. return -EINVAL;
  505. }
  506. }
  507. static const struct regmap_config ad4170_regmap_config = {
  508. .reg_read = ad4170_reg_read,
  509. .reg_write = ad4170_reg_write,
  510. };
  511. static bool ad4170_setup_eq(struct ad4170_setup *a, struct ad4170_setup *b)
  512. {
  513. if (a->misc != b->misc ||
  514. a->afe != b->afe ||
  515. a->filter != b->filter ||
  516. a->filter_fs != b->filter_fs ||
  517. a->offset != b->offset ||
  518. a->gain != b->gain)
  519. return false;
  520. return true;
  521. }
  522. static int ad4170_find_setup(struct ad4170_state *st,
  523. struct ad4170_setup *target_setup,
  524. unsigned int *setup_num, bool *overwrite)
  525. {
  526. unsigned int i;
  527. *setup_num = AD4170_INVALID_SETUP;
  528. *overwrite = false;
  529. for (i = 0; i < AD4170_MAX_SETUPS; i++) {
  530. struct ad4170_setup_info *setup_info = &st->setup_infos[i];
  531. /* Immediately accept a matching setup. */
  532. if (ad4170_setup_eq(target_setup, &setup_info->setup)) {
  533. *setup_num = i;
  534. return 0;
  535. }
  536. /* Ignore all setups which are used by enabled channels. */
  537. if (setup_info->enabled_channels)
  538. continue;
  539. /* Find the least used slot. */
  540. if (*setup_num == AD4170_INVALID_SETUP ||
  541. setup_info->channels < st->setup_infos[*setup_num].channels)
  542. *setup_num = i;
  543. }
  544. if (*setup_num == AD4170_INVALID_SETUP)
  545. return -EINVAL;
  546. *overwrite = true;
  547. return 0;
  548. }
  549. static void ad4170_unlink_channel(struct ad4170_state *st, unsigned int channel)
  550. {
  551. struct ad4170_chan_info *chan_info = &st->chan_infos[channel];
  552. struct ad4170_setup_info *setup_info = &st->setup_infos[chan_info->setup_num];
  553. chan_info->setup_num = AD4170_INVALID_SETUP;
  554. setup_info->channels--;
  555. }
  556. static int ad4170_unlink_setup(struct ad4170_state *st, unsigned int setup_num)
  557. {
  558. unsigned int i;
  559. for (i = 0; i < AD4170_MAX_ADC_CHANNELS; i++) {
  560. struct ad4170_chan_info *chan_info = &st->chan_infos[i];
  561. if (!chan_info->initialized || chan_info->setup_num != setup_num)
  562. continue;
  563. ad4170_unlink_channel(st, i);
  564. }
  565. return 0;
  566. }
  567. static int ad4170_link_channel_setup(struct ad4170_state *st,
  568. unsigned int chan_addr,
  569. unsigned int setup_num)
  570. {
  571. struct ad4170_setup_info *setup_info = &st->setup_infos[setup_num];
  572. struct ad4170_chan_info *chan_info = &st->chan_infos[chan_addr];
  573. int ret;
  574. ret = regmap_update_bits(st->regmap, AD4170_CHAN_SETUP_REG(chan_addr),
  575. AD4170_CHAN_SETUP_SETUP_MSK,
  576. FIELD_PREP(AD4170_CHAN_SETUP_SETUP_MSK, setup_num));
  577. if (ret)
  578. return ret;
  579. chan_info->setup_num = setup_num;
  580. setup_info->channels++;
  581. return 0;
  582. }
  583. static int ad4170_write_setup(struct ad4170_state *st, unsigned int setup_num,
  584. struct ad4170_setup *setup)
  585. {
  586. int ret;
  587. /*
  588. * It is recommended to place the ADC in standby mode or idle mode to
  589. * write to OFFSET and GAIN registers.
  590. */
  591. ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
  592. AD4170_ADC_CTRL_MODE_MSK,
  593. FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK,
  594. AD4170_ADC_CTRL_MODE_IDLE));
  595. if (ret)
  596. return ret;
  597. ret = regmap_write(st->regmap, AD4170_MISC_REG(setup_num), setup->misc);
  598. if (ret)
  599. return ret;
  600. ret = regmap_write(st->regmap, AD4170_AFE_REG(setup_num), setup->afe);
  601. if (ret)
  602. return ret;
  603. ret = regmap_write(st->regmap, AD4170_FILTER_REG(setup_num),
  604. setup->filter);
  605. if (ret)
  606. return ret;
  607. ret = regmap_write(st->regmap, AD4170_FILTER_FS_REG(setup_num),
  608. setup->filter_fs);
  609. if (ret)
  610. return ret;
  611. ret = regmap_write(st->regmap, AD4170_OFFSET_REG(setup_num),
  612. setup->offset);
  613. if (ret)
  614. return ret;
  615. ret = regmap_write(st->regmap, AD4170_GAIN_REG(setup_num), setup->gain);
  616. if (ret)
  617. return ret;
  618. memcpy(&st->setup_infos[setup_num].setup, setup, sizeof(*setup));
  619. return 0;
  620. }
  621. static int ad4170_write_channel_setup(struct ad4170_state *st,
  622. unsigned int chan_addr, bool on_enable)
  623. {
  624. struct ad4170_chan_info *chan_info = &st->chan_infos[chan_addr];
  625. bool overwrite;
  626. int setup_num;
  627. int ret;
  628. /*
  629. * Similar to AD4130 driver, the following cases need to be handled.
  630. *
  631. * 1. Enabled and linked channel with setup changes:
  632. * - Find a setup. If not possible, return error.
  633. * - Unlink channel from current setup.
  634. * - If the setup found has only disabled channels linked to it,
  635. * unlink all channels, and write the new setup to it.
  636. * - Link channel to new setup.
  637. *
  638. * 2. Soon to be enabled and unlinked channel:
  639. * - Find a setup. If not possible, return error.
  640. * - If the setup found has only disabled channels linked to it,
  641. * unlink all channels, and write the new setup to it.
  642. * - Link channel to the setup.
  643. *
  644. * 3. Disabled and linked channel with setup changes:
  645. * - Unlink channel from current setup.
  646. *
  647. * 4. Soon to be enabled and linked channel:
  648. * 5. Disabled and unlinked channel with setup changes:
  649. * - Do nothing.
  650. */
  651. /* Cases 3, 4, and 5 */
  652. if (chan_info->setup_num != AD4170_INVALID_SETUP) {
  653. /* Case 4 */
  654. if (on_enable)
  655. return 0;
  656. /* Case 3 */
  657. if (!chan_info->enabled) {
  658. ad4170_unlink_channel(st, chan_addr);
  659. return 0;
  660. }
  661. } else if (!on_enable && !chan_info->enabled) {
  662. /* Case 5 */
  663. return 0;
  664. }
  665. /* Cases 1 & 2 */
  666. ret = ad4170_find_setup(st, &chan_info->setup, &setup_num, &overwrite);
  667. if (ret)
  668. return ret;
  669. if (chan_info->setup_num != AD4170_INVALID_SETUP)
  670. /* Case 1 */
  671. ad4170_unlink_channel(st, chan_addr);
  672. if (overwrite) {
  673. ret = ad4170_unlink_setup(st, setup_num);
  674. if (ret)
  675. return ret;
  676. ret = ad4170_write_setup(st, setup_num, &chan_info->setup);
  677. if (ret)
  678. return ret;
  679. }
  680. return ad4170_link_channel_setup(st, chan_addr, setup_num);
  681. }
  682. static int ad4170_set_channel_enable(struct ad4170_state *st,
  683. unsigned int chan_addr, bool status)
  684. {
  685. struct ad4170_chan_info *chan_info = &st->chan_infos[chan_addr];
  686. struct ad4170_setup_info *setup_info;
  687. int ret;
  688. if (chan_info->enabled == status)
  689. return 0;
  690. if (status) {
  691. ret = ad4170_write_channel_setup(st, chan_addr, true);
  692. if (ret)
  693. return ret;
  694. }
  695. setup_info = &st->setup_infos[chan_info->setup_num];
  696. ret = regmap_update_bits(st->regmap, AD4170_CHAN_EN_REG,
  697. AD4170_CHAN_EN(chan_addr),
  698. status ? AD4170_CHAN_EN(chan_addr) : 0);
  699. if (ret)
  700. return ret;
  701. setup_info->enabled_channels += status ? 1 : -1;
  702. chan_info->enabled = status;
  703. return 0;
  704. }
  705. static int __ad4170_get_filter_type(unsigned int filter)
  706. {
  707. u16 f_conf = FIELD_GET(AD4170_FILTER_FILTER_TYPE_MSK, filter);
  708. switch (f_conf) {
  709. case AD4170_FILTER_FILTER_TYPE_SINC5_AVG:
  710. return AD4170_SINC5_AVG;
  711. case AD4170_FILTER_FILTER_TYPE_SINC5:
  712. return AD4170_SINC5;
  713. case AD4170_FILTER_FILTER_TYPE_SINC3:
  714. return AD4170_SINC3;
  715. default:
  716. return -EINVAL;
  717. }
  718. }
  719. static int ad4170_set_filter_type(struct iio_dev *indio_dev,
  720. struct iio_chan_spec const *chan,
  721. unsigned int val)
  722. {
  723. struct ad4170_state *st = iio_priv(indio_dev);
  724. struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
  725. struct ad4170_setup *setup = &chan_info->setup;
  726. unsigned int filter_type_conf;
  727. int ret;
  728. switch (val) {
  729. case AD4170_SINC5_AVG:
  730. filter_type_conf = AD4170_FILTER_FILTER_TYPE_SINC5_AVG;
  731. break;
  732. case AD4170_SINC5:
  733. filter_type_conf = AD4170_FILTER_FILTER_TYPE_SINC5;
  734. break;
  735. case AD4170_SINC3:
  736. filter_type_conf = AD4170_FILTER_FILTER_TYPE_SINC3;
  737. break;
  738. default:
  739. return -EINVAL;
  740. }
  741. /*
  742. * The filters provide the same ODR for a given filter_fs value but
  743. * there are different minimum and maximum filter_fs limits for each
  744. * filter. The filter_fs value will be adjusted if the current filter_fs
  745. * is out of the limits of the just requested filter. Since the
  746. * filter_fs value affects the ODR (sampling_frequency), changing the
  747. * filter may lead to a change in the sampling frequency.
  748. */
  749. scoped_guard(mutex, &st->lock) {
  750. if (!iio_device_claim_direct(indio_dev))
  751. return -EBUSY;
  752. if (val == AD4170_SINC5_AVG || val == AD4170_SINC3)
  753. setup->filter_fs = clamp(val, AD4170_SINC3_MIN_FS,
  754. AD4170_SINC3_MAX_FS);
  755. else
  756. setup->filter_fs = clamp(val, AD4170_SINC5_MIN_FS,
  757. AD4170_SINC5_MAX_FS);
  758. setup->filter &= ~AD4170_FILTER_FILTER_TYPE_MSK;
  759. setup->filter |= FIELD_PREP(AD4170_FILTER_FILTER_TYPE_MSK,
  760. filter_type_conf);
  761. ret = ad4170_write_channel_setup(st, chan->address, false);
  762. iio_device_release_direct(indio_dev);
  763. }
  764. return ret;
  765. }
  766. static int ad4170_get_filter_type(struct iio_dev *indio_dev,
  767. struct iio_chan_spec const *chan)
  768. {
  769. struct ad4170_state *st = iio_priv(indio_dev);
  770. struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
  771. struct ad4170_setup *setup = &chan_info->setup;
  772. return __ad4170_get_filter_type(setup->filter);
  773. }
  774. static const struct iio_enum ad4170_filter_type_enum = {
  775. .items = ad4170_filt_names,
  776. .num_items = ARRAY_SIZE(ad4170_filt_names),
  777. .get = ad4170_get_filter_type,
  778. .set = ad4170_set_filter_type,
  779. };
  780. static const struct iio_chan_spec_ext_info ad4170_filter_type_ext_info[] = {
  781. IIO_ENUM("filter_type", IIO_SEPARATE, &ad4170_filter_type_enum),
  782. IIO_ENUM_AVAILABLE("filter_type", IIO_SHARED_BY_TYPE,
  783. &ad4170_filter_type_enum),
  784. { }
  785. };
  786. static const struct iio_chan_spec ad4170_channel_template = {
  787. .type = IIO_VOLTAGE,
  788. .indexed = 1,
  789. .differential = 1,
  790. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  791. BIT(IIO_CHAN_INFO_SCALE) |
  792. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  793. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  794. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  795. BIT(IIO_CHAN_INFO_OFFSET),
  796. .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE) |
  797. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  798. .ext_info = ad4170_filter_type_ext_info,
  799. .scan_type = {
  800. .realbits = 24,
  801. .storagebits = 32,
  802. .shift = 8,
  803. .endianness = IIO_BE,
  804. },
  805. };
  806. static const struct iio_chan_spec ad4170_temp_channel_template = {
  807. .type = IIO_TEMP,
  808. .indexed = 0,
  809. .channel = 17,
  810. .channel2 = 17,
  811. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  812. BIT(IIO_CHAN_INFO_SCALE) |
  813. BIT(IIO_CHAN_INFO_OFFSET) |
  814. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  815. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  816. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  817. .info_mask_separate_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  818. .scan_type = {
  819. .sign = 's',
  820. .realbits = 24,
  821. .storagebits = 32,
  822. .shift = 8,
  823. .endianness = IIO_BE,
  824. },
  825. };
  826. /*
  827. * Receives the number of a multiplexed AD4170 input (ain_n), and stores the
  828. * voltage (in µV) of the specified input into ain_voltage. If the input number
  829. * is a ordinary analog input (AIN0 to AIN8), stores zero into ain_voltage.
  830. * If a voltage regulator required by a special input is unavailable, return
  831. * error code. Return 0 on success.
  832. */
  833. static int ad4170_get_ain_voltage_uv(struct ad4170_state *st, int ain_n,
  834. int *ain_voltage)
  835. {
  836. struct device *dev = &st->spi->dev;
  837. int v_diff;
  838. *ain_voltage = 0;
  839. /*
  840. * The voltage bias (vbias) sets the common-mode voltage of the channel
  841. * to (AVDD + AVSS)/2. If provided, AVSS supply provides the magnitude
  842. * (absolute value) of the negative voltage supplied to the AVSS pin.
  843. * So, we do AVDD - AVSS to compute the DC voltage generated by the bias
  844. * voltage generator.
  845. */
  846. if (st->pins_fn[ain_n] & AD4170_PIN_VBIAS) {
  847. int v_diff = st->vrefs_uv[AD4170_AVDD_SUP] - st->vrefs_uv[AD4170_AVSS_SUP];
  848. *ain_voltage = v_diff / 2;
  849. return 0;
  850. }
  851. if (ain_n <= AD4170_CHAN_MAP_TEMP_SENSOR)
  852. return 0;
  853. switch (ain_n) {
  854. case AD4170_CHAN_MAP_AVDD_AVSS_N:
  855. v_diff = st->vrefs_uv[AD4170_AVDD_SUP] - st->vrefs_uv[AD4170_AVSS_SUP];
  856. *ain_voltage = v_diff / 5;
  857. return 0;
  858. case AD4170_CHAN_MAP_IOVDD_DGND_N:
  859. *ain_voltage = st->vrefs_uv[AD4170_IOVDD_SUP] / 5;
  860. return 0;
  861. case AD4170_CHAN_MAP_AVSS:
  862. *ain_voltage = st->vrefs_uv[AD4170_AVSS_SUP];
  863. return 0;
  864. case AD4170_CHAN_MAP_DGND:
  865. *ain_voltage = 0;
  866. return 0;
  867. case AD4170_CHAN_MAP_REFIN1_P:
  868. if (st->vrefs_uv[AD4170_REFIN1P_SUP] == -ENODEV)
  869. return dev_err_probe(dev, -ENODEV,
  870. "input set to REFIN+ but ref not provided\n");
  871. *ain_voltage = st->vrefs_uv[AD4170_REFIN1P_SUP];
  872. return 0;
  873. case AD4170_CHAN_MAP_REFIN1_N:
  874. if (st->vrefs_uv[AD4170_REFIN1N_SUP] == -ENODEV)
  875. return dev_err_probe(dev, -ENODEV,
  876. "input set to REFIN- but ref not provided\n");
  877. *ain_voltage = st->vrefs_uv[AD4170_REFIN1N_SUP];
  878. return 0;
  879. case AD4170_CHAN_MAP_REFIN2_P:
  880. if (st->vrefs_uv[AD4170_REFIN2P_SUP] == -ENODEV)
  881. return dev_err_probe(dev, -ENODEV,
  882. "input set to REFIN2+ but ref not provided\n");
  883. *ain_voltage = st->vrefs_uv[AD4170_REFIN2P_SUP];
  884. return 0;
  885. case AD4170_CHAN_MAP_REFIN2_N:
  886. if (st->vrefs_uv[AD4170_REFIN2N_SUP] == -ENODEV)
  887. return dev_err_probe(dev, -ENODEV,
  888. "input set to REFIN2- but ref not provided\n");
  889. *ain_voltage = st->vrefs_uv[AD4170_REFIN2N_SUP];
  890. return 0;
  891. case AD4170_CHAN_MAP_REFOUT:
  892. /* REFOUT is 2.5V relative to AVSS so take that into account */
  893. *ain_voltage = st->vrefs_uv[AD4170_AVSS_SUP] + AD4170_INT_REF_2_5V;
  894. return 0;
  895. default:
  896. return -EINVAL;
  897. }
  898. }
  899. static int ad4170_validate_analog_input(struct ad4170_state *st, int pin)
  900. {
  901. if (pin <= AD4170_MAX_ANALOG_PINS) {
  902. if (st->pins_fn[pin] & AD4170_PIN_CURRENT_OUT)
  903. return dev_err_probe(&st->spi->dev, -EINVAL,
  904. "Pin %d already used with fn %u.\n",
  905. pin, st->pins_fn[pin]);
  906. st->pins_fn[pin] |= AD4170_PIN_ANALOG_IN;
  907. }
  908. return 0;
  909. }
  910. static int ad4170_validate_channel_input(struct ad4170_state *st, int pin, bool com)
  911. {
  912. /* Check common-mode input pin is mapped to a special input. */
  913. if (com && (pin < AD4170_CHAN_MAP_AVDD_AVSS_P || pin > AD4170_CHAN_MAP_REFOUT))
  914. return dev_err_probe(&st->spi->dev, -EINVAL,
  915. "Invalid common-mode input pin number. %d\n",
  916. pin);
  917. /* Check differential input pin is mapped to a analog input pin. */
  918. if (!com && pin > AD4170_MAX_ANALOG_PINS)
  919. return dev_err_probe(&st->spi->dev, -EINVAL,
  920. "Invalid analog input pin number. %d\n",
  921. pin);
  922. return ad4170_validate_analog_input(st, pin);
  923. }
  924. /*
  925. * Verifies whether the channel input configuration is valid by checking the
  926. * input numbers.
  927. * Returns 0 on valid channel input configuration. -EINVAL otherwise.
  928. */
  929. static int ad4170_validate_channel(struct ad4170_state *st,
  930. struct iio_chan_spec const *chan)
  931. {
  932. int ret;
  933. ret = ad4170_validate_channel_input(st, chan->channel, false);
  934. if (ret)
  935. return ret;
  936. return ad4170_validate_channel_input(st, chan->channel2,
  937. !chan->differential);
  938. }
  939. /*
  940. * Verifies whether the channel configuration is valid by checking the provided
  941. * input type, polarity, and voltage references result in a sane input range.
  942. * Returns negative error code on failure.
  943. */
  944. static int ad4170_get_input_range(struct ad4170_state *st,
  945. struct iio_chan_spec const *chan,
  946. unsigned int ch_reg, unsigned int ref_sel)
  947. {
  948. bool bipolar = chan->scan_type.sign == 's';
  949. struct device *dev = &st->spi->dev;
  950. int refp, refn, ain_voltage, ret;
  951. switch (ref_sel) {
  952. case AD4170_REF_REFIN1:
  953. if (st->vrefs_uv[AD4170_REFIN1P_SUP] == -ENODEV ||
  954. st->vrefs_uv[AD4170_REFIN1N_SUP] == -ENODEV)
  955. return dev_err_probe(dev, -ENODEV,
  956. "REFIN± selected but not provided\n");
  957. refp = st->vrefs_uv[AD4170_REFIN1P_SUP];
  958. refn = st->vrefs_uv[AD4170_REFIN1N_SUP];
  959. break;
  960. case AD4170_REF_REFIN2:
  961. if (st->vrefs_uv[AD4170_REFIN2P_SUP] == -ENODEV ||
  962. st->vrefs_uv[AD4170_REFIN2N_SUP] == -ENODEV)
  963. return dev_err_probe(dev, -ENODEV,
  964. "REFIN2± selected but not provided\n");
  965. refp = st->vrefs_uv[AD4170_REFIN2P_SUP];
  966. refn = st->vrefs_uv[AD4170_REFIN2N_SUP];
  967. break;
  968. case AD4170_REF_AVDD:
  969. refp = st->vrefs_uv[AD4170_AVDD_SUP];
  970. refn = st->vrefs_uv[AD4170_AVSS_SUP];
  971. break;
  972. case AD4170_REF_REFOUT:
  973. /* REFOUT is 2.5 V relative to AVSS */
  974. refp = st->vrefs_uv[AD4170_AVSS_SUP] + AD4170_INT_REF_2_5V;
  975. refn = st->vrefs_uv[AD4170_AVSS_SUP];
  976. break;
  977. default:
  978. return -EINVAL;
  979. }
  980. /*
  981. * Find out the analog input range from the channel type, polarity, and
  982. * voltage reference selection.
  983. * AD4170 channels are either differential or pseudo-differential.
  984. * Diff input voltage range: −VREF/gain to +VREF/gain (datasheet page 6)
  985. * Pseudo-diff input voltage range: 0 to VREF/gain (datasheet page 6)
  986. */
  987. if (chan->differential) {
  988. if (!bipolar)
  989. return dev_err_probe(dev, -EINVAL,
  990. "Channel %u differential unipolar\n",
  991. ch_reg);
  992. /*
  993. * Differential bipolar channel.
  994. * avss-supply is never above 0V.
  995. * Assuming refin1n-supply not above 0V.
  996. * Assuming refin2n-supply not above 0V.
  997. */
  998. return refp + abs(refn);
  999. }
  1000. /*
  1001. * Some configurations can lead to invalid setups.
  1002. * For example, if AVSS = -2.5V, REF_SELECT set to REFOUT (REFOUT/AVSS),
  1003. * and pseudo-diff channel configuration set, then the input range
  1004. * should go from 0V to +VREF (single-ended - datasheet pg 10), but
  1005. * REFOUT/AVSS range would be -2.5V to 0V.
  1006. * Check the positive reference is higher than 0V for pseudo-diff
  1007. * channels.
  1008. * Note that at this point in the code, refp can only be >= 0 since all
  1009. * error codes from reading the regulator voltage have been checked
  1010. * either at ad4170_regulator_setup() or above in this function.
  1011. */
  1012. if (refp == 0)
  1013. return dev_err_probe(dev, -EINVAL,
  1014. "REF+ == GND for pseudo-diff chan %u\n",
  1015. ch_reg);
  1016. if (bipolar)
  1017. return refp;
  1018. /*
  1019. * Pseudo-differential unipolar channel.
  1020. * Input expected to swing from IN- to +VREF.
  1021. */
  1022. ret = ad4170_get_ain_voltage_uv(st, chan->channel2, &ain_voltage);
  1023. if (ret)
  1024. return ret;
  1025. if (refp - ain_voltage <= 0)
  1026. return dev_err_probe(dev, -EINVAL,
  1027. "Negative input >= REF+ for pseudo-diff chan %u\n",
  1028. ch_reg);
  1029. return refp - ain_voltage;
  1030. }
  1031. static int __ad4170_read_sample(struct iio_dev *indio_dev,
  1032. struct iio_chan_spec const *chan, int *val)
  1033. {
  1034. struct ad4170_state *st = iio_priv(indio_dev);
  1035. unsigned long settling_time_ms;
  1036. int ret;
  1037. reinit_completion(&st->completion);
  1038. ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
  1039. AD4170_ADC_CTRL_MODE_MSK,
  1040. FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK,
  1041. AD4170_ADC_CTRL_MODE_SINGLE));
  1042. if (ret)
  1043. return ret;
  1044. /*
  1045. * When a channel is manually selected by the user, the ADC needs an
  1046. * extra time to provide the first stable conversion. The ADC settling
  1047. * time depends on the filter type, filter frequency, and ADC clock
  1048. * frequency (see datasheet page 53). The maximum settling time among
  1049. * all filter configurations is 6291164 / fCLK. Use that formula to wait
  1050. * for sufficient time whatever the filter configuration may be.
  1051. */
  1052. settling_time_ms = DIV_ROUND_UP(6291164 * MILLI, st->mclk_hz);
  1053. ret = wait_for_completion_timeout(&st->completion,
  1054. msecs_to_jiffies(settling_time_ms));
  1055. if (!ret)
  1056. dev_dbg(&st->spi->dev,
  1057. "No Data Ready signal. Reading after delay.\n");
  1058. ret = regmap_read(st->regmap, AD4170_DATA_24B_REG, val);
  1059. if (ret)
  1060. return ret;
  1061. if (chan->scan_type.sign == 's')
  1062. *val = sign_extend32(*val, chan->scan_type.realbits - 1);
  1063. return 0;
  1064. }
  1065. static int ad4170_read_sample(struct iio_dev *indio_dev,
  1066. struct iio_chan_spec const *chan, int *val)
  1067. {
  1068. struct ad4170_state *st = iio_priv(indio_dev);
  1069. struct device *dev = &st->spi->dev;
  1070. int ret, ret2;
  1071. /*
  1072. * The ADC sequences through all enabled channels. That can lead to
  1073. * incorrect channel being sampled if a previous read would have left a
  1074. * different channel enabled. Thus, always enable and disable the
  1075. * channel on single-shot read.
  1076. */
  1077. ret = ad4170_set_channel_enable(st, chan->address, true);
  1078. if (ret)
  1079. return ret;
  1080. ret = __ad4170_read_sample(indio_dev, chan, val);
  1081. if (ret) {
  1082. dev_err(dev, "failed to read sample: %d\n", ret);
  1083. ret2 = ad4170_set_channel_enable(st, chan->address, false);
  1084. if (ret2)
  1085. dev_err(dev, "failed to disable channel: %d\n", ret2);
  1086. return ret;
  1087. }
  1088. ret = ad4170_set_channel_enable(st, chan->address, false);
  1089. if (ret)
  1090. return ret;
  1091. return IIO_VAL_INT;
  1092. }
  1093. static int ad4170_read_raw(struct iio_dev *indio_dev,
  1094. struct iio_chan_spec const *chan,
  1095. int *val, int *val2, long info)
  1096. {
  1097. struct ad4170_state *st = iio_priv(indio_dev);
  1098. struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
  1099. struct ad4170_setup *setup = &chan_info->setup;
  1100. enum ad4170_filter_type f_type;
  1101. unsigned int pga, fs_idx;
  1102. int ret;
  1103. guard(mutex)(&st->lock);
  1104. switch (info) {
  1105. case IIO_CHAN_INFO_RAW:
  1106. if (!iio_device_claim_direct(indio_dev))
  1107. return -EBUSY;
  1108. ret = ad4170_read_sample(indio_dev, chan, val);
  1109. iio_device_release_direct(indio_dev);
  1110. return ret;
  1111. case IIO_CHAN_INFO_SCALE:
  1112. pga = FIELD_GET(AD4170_AFE_PGA_GAIN_MSK, setup->afe);
  1113. switch (chan->type) {
  1114. case IIO_VOLTAGE:
  1115. *val = chan_info->scale_tbl[pga][0];
  1116. *val2 = chan_info->scale_tbl[pga][1];
  1117. return IIO_VAL_INT_PLUS_NANO;
  1118. case IIO_TEMP:
  1119. /*
  1120. * The scale_tbl converts output codes to mV units so
  1121. * multiply by MILLI to make the factor convert to µV.
  1122. * Then, apply the temperature sensor change sensitivity
  1123. * of 477 μV/K. Finally, multiply the result by MILLI
  1124. * again to comply with milli degrees Celsius IIO ABI.
  1125. */
  1126. *val = 0;
  1127. *val2 = DIV_ROUND_CLOSEST(chan_info->scale_tbl[pga][1] * MILLI, 477) *
  1128. MILLI;
  1129. return IIO_VAL_INT_PLUS_NANO;
  1130. default:
  1131. return -EINVAL;
  1132. }
  1133. case IIO_CHAN_INFO_OFFSET:
  1134. pga = FIELD_GET(AD4170_AFE_PGA_GAIN_MSK, setup->afe);
  1135. *val = chan_info->offset_tbl[pga];
  1136. return IIO_VAL_INT;
  1137. case IIO_CHAN_INFO_SAMP_FREQ:
  1138. f_type = __ad4170_get_filter_type(setup->filter);
  1139. switch (f_type) {
  1140. case AD4170_SINC5_AVG:
  1141. case AD4170_SINC3:
  1142. fs_idx = find_closest(setup->filter_fs,
  1143. ad4170_sinc3_filt_fs_tbl,
  1144. ARRAY_SIZE(ad4170_sinc3_filt_fs_tbl));
  1145. *val = st->sps_tbl[f_type][fs_idx][0];
  1146. *val2 = st->sps_tbl[f_type][fs_idx][1];
  1147. return IIO_VAL_INT_PLUS_MICRO;
  1148. case AD4170_SINC5:
  1149. fs_idx = find_closest(setup->filter_fs,
  1150. ad4170_sinc5_filt_fs_tbl,
  1151. ARRAY_SIZE(ad4170_sinc5_filt_fs_tbl));
  1152. *val = st->sps_tbl[f_type][fs_idx][0];
  1153. *val2 = st->sps_tbl[f_type][fs_idx][1];
  1154. return IIO_VAL_INT_PLUS_MICRO;
  1155. default:
  1156. return -EINVAL;
  1157. }
  1158. case IIO_CHAN_INFO_CALIBBIAS:
  1159. *val = setup->offset;
  1160. return IIO_VAL_INT;
  1161. case IIO_CHAN_INFO_CALIBSCALE:
  1162. *val = setup->gain;
  1163. return IIO_VAL_INT;
  1164. default:
  1165. return -EINVAL;
  1166. }
  1167. }
  1168. static int ad4170_fill_scale_tbl(struct iio_dev *indio_dev,
  1169. struct iio_chan_spec const *chan)
  1170. {
  1171. struct ad4170_state *st = iio_priv(indio_dev);
  1172. struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
  1173. struct device *dev = &st->spi->dev;
  1174. int bipolar = chan->scan_type.sign == 's' ? 1 : 0;
  1175. int precision_bits = chan->scan_type.realbits;
  1176. int pga, ainm_voltage, ret;
  1177. unsigned long long offset;
  1178. ainm_voltage = 0;
  1179. ret = ad4170_get_ain_voltage_uv(st, chan->channel2, &ainm_voltage);
  1180. if (ret < 0)
  1181. return dev_err_probe(dev, ret, "Failed to fill scale table\n");
  1182. for (pga = 0; pga < AD4170_NUM_PGA_OPTIONS; pga++) {
  1183. u64 nv;
  1184. unsigned int lshift, rshift;
  1185. /*
  1186. * The PGA options are numbered from 0 to 9, with option 0 being
  1187. * a gain of 2^0 (no actual gain), and 7 meaning a gain of 2^7.
  1188. * Option 8, though, sets a gain of 0.5, so the input signal can
  1189. * be attenuated by 2 rather than amplified. Option 9, allows
  1190. * the signal to bypass the PGA circuitry (no gain).
  1191. *
  1192. * The scale factor to get ADC output codes to values in mV
  1193. * units is given by:
  1194. * _scale = (input_range / gain) / 2^precision
  1195. * AD4170 gain is a power of 2 so the above can be written as
  1196. * _scale = input_range / 2^(precision + gain)
  1197. * Keep the input range in µV to avoid truncating the less
  1198. * significant bits when right shifting it so to preserve scale
  1199. * precision.
  1200. */
  1201. nv = (u64)chan_info->input_range_uv * NANO;
  1202. lshift = !!(pga & BIT(3)); /* handle PGA options 8 and 9 */
  1203. rshift = precision_bits - bipolar + (pga & GENMASK(2, 0)) - lshift;
  1204. chan_info->scale_tbl[pga][0] = 0;
  1205. chan_info->scale_tbl[pga][1] = div_u64(nv >> rshift, MILLI);
  1206. /*
  1207. * If the negative input is not at GND, the conversion result
  1208. * (which is relative to IN-) will be offset by the level at IN-.
  1209. * Use the scale factor the other way around to go from a known
  1210. * voltage to the corresponding ADC output code.
  1211. * With that, we are able to get to what would be the output
  1212. * code for the voltage at the negative input.
  1213. * If the negative input is not fixed, there is no offset.
  1214. */
  1215. offset = ((unsigned long long)abs(ainm_voltage)) * MICRO;
  1216. offset = DIV_ROUND_CLOSEST_ULL(offset, chan_info->scale_tbl[pga][1]);
  1217. /*
  1218. * After divided by the scale, offset will always fit into 31
  1219. * bits. For _raw + _offset to be relative to GND, the value
  1220. * provided as _offset is of opposite sign than the real offset.
  1221. */
  1222. if (ainm_voltage > 0)
  1223. chan_info->offset_tbl[pga] = -(int)(offset);
  1224. else
  1225. chan_info->offset_tbl[pga] = (int)(offset);
  1226. }
  1227. return 0;
  1228. }
  1229. static int ad4170_read_avail(struct iio_dev *indio_dev,
  1230. struct iio_chan_spec const *chan,
  1231. const int **vals, int *type, int *length,
  1232. long info)
  1233. {
  1234. struct ad4170_state *st = iio_priv(indio_dev);
  1235. struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
  1236. enum ad4170_filter_type f_type;
  1237. switch (info) {
  1238. case IIO_CHAN_INFO_SCALE:
  1239. *vals = (int *)chan_info->scale_tbl;
  1240. *length = ARRAY_SIZE(chan_info->scale_tbl) * 2;
  1241. *type = IIO_VAL_INT_PLUS_NANO;
  1242. return IIO_AVAIL_LIST;
  1243. case IIO_CHAN_INFO_SAMP_FREQ:
  1244. *type = IIO_VAL_INT_PLUS_MICRO;
  1245. f_type = ad4170_get_filter_type(indio_dev, chan);
  1246. switch (f_type) {
  1247. case AD4170_SINC5_AVG:
  1248. case AD4170_SINC3:
  1249. /* Read sps_tbl here to ensure in bounds array access */
  1250. *vals = (int *)st->sps_tbl[f_type];
  1251. *length = ARRAY_SIZE(ad4170_sinc3_filt_fs_tbl) * 2;
  1252. return IIO_AVAIL_LIST;
  1253. case AD4170_SINC5:
  1254. /* Read sps_tbl here to ensure in bounds array access */
  1255. *vals = (int *)st->sps_tbl[f_type];
  1256. *length = ARRAY_SIZE(ad4170_sinc5_filt_fs_tbl) * 2;
  1257. return IIO_AVAIL_LIST;
  1258. default:
  1259. return -EINVAL;
  1260. }
  1261. default:
  1262. return -EINVAL;
  1263. }
  1264. }
  1265. static int ad4170_set_pga(struct ad4170_state *st,
  1266. struct iio_chan_spec const *chan, int val, int val2)
  1267. {
  1268. struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
  1269. struct ad4170_setup *setup = &chan_info->setup;
  1270. unsigned int pga;
  1271. for (pga = 0; pga < AD4170_NUM_PGA_OPTIONS; pga++) {
  1272. if (val == chan_info->scale_tbl[pga][0] &&
  1273. val2 == chan_info->scale_tbl[pga][1])
  1274. break;
  1275. }
  1276. if (pga == AD4170_NUM_PGA_OPTIONS)
  1277. return -EINVAL;
  1278. guard(mutex)(&st->lock);
  1279. setup->afe &= ~AD4170_AFE_PGA_GAIN_MSK;
  1280. setup->afe |= FIELD_PREP(AD4170_AFE_PGA_GAIN_MSK, pga);
  1281. return ad4170_write_channel_setup(st, chan->address, false);
  1282. }
  1283. static int ad4170_set_channel_freq(struct ad4170_state *st,
  1284. struct iio_chan_spec const *chan, int val,
  1285. int val2)
  1286. {
  1287. struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
  1288. struct ad4170_setup *setup = &chan_info->setup;
  1289. enum ad4170_filter_type f_type = __ad4170_get_filter_type(setup->filter);
  1290. unsigned int filt_fs_tbl_size, i;
  1291. switch (f_type) {
  1292. case AD4170_SINC5_AVG:
  1293. case AD4170_SINC3:
  1294. filt_fs_tbl_size = ARRAY_SIZE(ad4170_sinc3_filt_fs_tbl);
  1295. break;
  1296. case AD4170_SINC5:
  1297. filt_fs_tbl_size = ARRAY_SIZE(ad4170_sinc5_filt_fs_tbl);
  1298. break;
  1299. }
  1300. for (i = 0; i < filt_fs_tbl_size; i++) {
  1301. if (st->sps_tbl[f_type][i][0] == val &&
  1302. st->sps_tbl[f_type][i][1] == val2)
  1303. break;
  1304. }
  1305. if (i == filt_fs_tbl_size)
  1306. return -EINVAL;
  1307. guard(mutex)(&st->lock);
  1308. if (f_type == AD4170_SINC5)
  1309. setup->filter_fs = ad4170_sinc5_filt_fs_tbl[i];
  1310. else
  1311. setup->filter_fs = ad4170_sinc3_filt_fs_tbl[i];
  1312. return ad4170_write_channel_setup(st, chan->address, false);
  1313. }
  1314. static int ad4170_set_calib_offset(struct ad4170_state *st,
  1315. struct iio_chan_spec const *chan, int val)
  1316. {
  1317. struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
  1318. struct ad4170_setup *setup = &chan_info->setup;
  1319. guard(mutex)(&st->lock);
  1320. setup->offset = val;
  1321. return ad4170_write_channel_setup(st, chan->address, false);
  1322. }
  1323. static int ad4170_set_calib_gain(struct ad4170_state *st,
  1324. struct iio_chan_spec const *chan, int val)
  1325. {
  1326. struct ad4170_chan_info *chan_info = &st->chan_infos[chan->address];
  1327. struct ad4170_setup *setup = &chan_info->setup;
  1328. guard(mutex)(&st->lock);
  1329. setup->gain = val;
  1330. return ad4170_write_channel_setup(st, chan->address, false);
  1331. }
  1332. static int __ad4170_write_raw(struct iio_dev *indio_dev,
  1333. struct iio_chan_spec const *chan, int val,
  1334. int val2, long info)
  1335. {
  1336. struct ad4170_state *st = iio_priv(indio_dev);
  1337. switch (info) {
  1338. case IIO_CHAN_INFO_SCALE:
  1339. return ad4170_set_pga(st, chan, val, val2);
  1340. case IIO_CHAN_INFO_SAMP_FREQ:
  1341. return ad4170_set_channel_freq(st, chan, val, val2);
  1342. case IIO_CHAN_INFO_CALIBBIAS:
  1343. return ad4170_set_calib_offset(st, chan, val);
  1344. case IIO_CHAN_INFO_CALIBSCALE:
  1345. return ad4170_set_calib_gain(st, chan, val);
  1346. default:
  1347. return -EINVAL;
  1348. }
  1349. }
  1350. static int ad4170_write_raw(struct iio_dev *indio_dev,
  1351. struct iio_chan_spec const *chan, int val,
  1352. int val2, long info)
  1353. {
  1354. int ret;
  1355. if (!iio_device_claim_direct(indio_dev))
  1356. return -EBUSY;
  1357. ret = __ad4170_write_raw(indio_dev, chan, val, val2, info);
  1358. iio_device_release_direct(indio_dev);
  1359. return ret;
  1360. }
  1361. static int ad4170_write_raw_get_fmt(struct iio_dev *indio_dev,
  1362. struct iio_chan_spec const *chan,
  1363. long info)
  1364. {
  1365. switch (info) {
  1366. case IIO_CHAN_INFO_SCALE:
  1367. return IIO_VAL_INT_PLUS_NANO;
  1368. case IIO_CHAN_INFO_SAMP_FREQ:
  1369. return IIO_VAL_INT_PLUS_MICRO;
  1370. case IIO_CHAN_INFO_CALIBBIAS:
  1371. case IIO_CHAN_INFO_CALIBSCALE:
  1372. return IIO_VAL_INT;
  1373. default:
  1374. return -EINVAL;
  1375. }
  1376. }
  1377. static int ad4170_update_scan_mode(struct iio_dev *indio_dev,
  1378. const unsigned long *active_scan_mask)
  1379. {
  1380. struct ad4170_state *st = iio_priv(indio_dev);
  1381. unsigned int chan_index;
  1382. int ret;
  1383. iio_for_each_active_channel(indio_dev, chan_index) {
  1384. ret = ad4170_set_channel_enable(st, chan_index, true);
  1385. if (ret)
  1386. return ret;
  1387. }
  1388. return 0;
  1389. }
  1390. static const struct iio_info ad4170_info = {
  1391. .read_raw = ad4170_read_raw,
  1392. .read_avail = ad4170_read_avail,
  1393. .write_raw = ad4170_write_raw,
  1394. .write_raw_get_fmt = ad4170_write_raw_get_fmt,
  1395. .update_scan_mode = ad4170_update_scan_mode,
  1396. .debugfs_reg_access = ad4170_debugfs_reg_access,
  1397. };
  1398. static int ad4170_soft_reset(struct ad4170_state *st)
  1399. {
  1400. int ret;
  1401. ret = regmap_write(st->regmap, AD4170_CONFIG_A_REG,
  1402. AD4170_SW_RESET_MSK);
  1403. if (ret)
  1404. return ret;
  1405. /* AD4170-4 requires 1 ms between reset and any register access. */
  1406. fsleep(1 * USEC_PER_MSEC);
  1407. return 0;
  1408. }
  1409. static int ad4170_gpio_get(struct gpio_chip *gc, unsigned int offset)
  1410. {
  1411. struct iio_dev *indio_dev = gpiochip_get_data(gc);
  1412. struct ad4170_state *st = iio_priv(indio_dev);
  1413. unsigned int val;
  1414. int ret;
  1415. if (!iio_device_claim_direct(indio_dev))
  1416. return -EBUSY;
  1417. ret = regmap_read(st->regmap, AD4170_GPIO_MODE_REG, &val);
  1418. if (ret)
  1419. goto err_release;
  1420. /*
  1421. * If the GPIO is configured as an input, read the current value from
  1422. * AD4170_GPIO_INPUT_REG. Otherwise, read the input value from
  1423. * AD4170_GPIO_OUTPUT_REG.
  1424. */
  1425. if (val & BIT(offset * 2))
  1426. ret = regmap_read(st->regmap, AD4170_GPIO_INPUT_REG, &val);
  1427. else
  1428. ret = regmap_read(st->regmap, AD4170_GPIO_OUTPUT_REG, &val);
  1429. if (ret)
  1430. goto err_release;
  1431. ret = !!(val & BIT(offset));
  1432. err_release:
  1433. iio_device_release_direct(indio_dev);
  1434. return ret;
  1435. }
  1436. static int ad4170_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
  1437. {
  1438. struct iio_dev *indio_dev = gpiochip_get_data(gc);
  1439. struct ad4170_state *st = iio_priv(indio_dev);
  1440. int ret;
  1441. if (!iio_device_claim_direct(indio_dev))
  1442. return -EBUSY;
  1443. ret = regmap_assign_bits(st->regmap, AD4170_GPIO_OUTPUT_REG,
  1444. BIT(offset), !!value);
  1445. iio_device_release_direct(indio_dev);
  1446. return ret;
  1447. }
  1448. static int ad4170_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  1449. {
  1450. struct iio_dev *indio_dev = gpiochip_get_data(gc);
  1451. struct ad4170_state *st = iio_priv(indio_dev);
  1452. unsigned int val;
  1453. int ret;
  1454. if (!iio_device_claim_direct(indio_dev))
  1455. return -EBUSY;
  1456. ret = regmap_read(st->regmap, AD4170_GPIO_MODE_REG, &val);
  1457. if (ret)
  1458. goto err_release;
  1459. if (val & BIT(offset * 2 + 1))
  1460. ret = GPIO_LINE_DIRECTION_OUT;
  1461. else
  1462. ret = GPIO_LINE_DIRECTION_IN;
  1463. err_release:
  1464. iio_device_release_direct(indio_dev);
  1465. return ret;
  1466. }
  1467. static int ad4170_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
  1468. {
  1469. struct iio_dev *indio_dev = gpiochip_get_data(gc);
  1470. struct ad4170_state *st = iio_priv(indio_dev);
  1471. unsigned long gpio_mask;
  1472. int ret;
  1473. if (!iio_device_claim_direct(indio_dev))
  1474. return -EBUSY;
  1475. switch (offset) {
  1476. case 0:
  1477. gpio_mask = AD4170_GPIO_MODE_GPIO0_MSK;
  1478. break;
  1479. case 1:
  1480. gpio_mask = AD4170_GPIO_MODE_GPIO1_MSK;
  1481. break;
  1482. case 2:
  1483. gpio_mask = AD4170_GPIO_MODE_GPIO2_MSK;
  1484. break;
  1485. case 3:
  1486. gpio_mask = AD4170_GPIO_MODE_GPIO3_MSK;
  1487. break;
  1488. default:
  1489. ret = -EINVAL;
  1490. goto err_release;
  1491. }
  1492. ret = regmap_update_bits(st->regmap, AD4170_GPIO_MODE_REG, gpio_mask,
  1493. AD4170_GPIO_MODE_GPIO_INPUT << (2 * offset));
  1494. err_release:
  1495. iio_device_release_direct(indio_dev);
  1496. return ret;
  1497. }
  1498. static int ad4170_gpio_direction_output(struct gpio_chip *gc,
  1499. unsigned int offset, int value)
  1500. {
  1501. struct iio_dev *indio_dev = gpiochip_get_data(gc);
  1502. struct ad4170_state *st = iio_priv(indio_dev);
  1503. unsigned long gpio_mask;
  1504. int ret;
  1505. ret = ad4170_gpio_set(gc, offset, value);
  1506. if (ret)
  1507. return ret;
  1508. if (!iio_device_claim_direct(indio_dev))
  1509. return -EBUSY;
  1510. switch (offset) {
  1511. case 0:
  1512. gpio_mask = AD4170_GPIO_MODE_GPIO0_MSK;
  1513. break;
  1514. case 1:
  1515. gpio_mask = AD4170_GPIO_MODE_GPIO1_MSK;
  1516. break;
  1517. case 2:
  1518. gpio_mask = AD4170_GPIO_MODE_GPIO2_MSK;
  1519. break;
  1520. case 3:
  1521. gpio_mask = AD4170_GPIO_MODE_GPIO3_MSK;
  1522. break;
  1523. default:
  1524. ret = -EINVAL;
  1525. goto err_release;
  1526. }
  1527. ret = regmap_update_bits(st->regmap, AD4170_GPIO_MODE_REG, gpio_mask,
  1528. AD4170_GPIO_MODE_GPIO_OUTPUT << (2 * offset));
  1529. err_release:
  1530. iio_device_release_direct(indio_dev);
  1531. return ret;
  1532. }
  1533. static int ad4170_gpio_init_valid_mask(struct gpio_chip *gc,
  1534. unsigned long *valid_mask,
  1535. unsigned int ngpios)
  1536. {
  1537. struct ad4170_state *st = gpiochip_get_data(gc);
  1538. unsigned int i;
  1539. /* Only expose GPIOs that were not assigned any other function. */
  1540. for (i = 0; i < ngpios; i++) {
  1541. bool valid = st->gpio_fn[i] == AD4170_GPIO_UNASSIGNED;
  1542. __assign_bit(i, valid_mask, valid);
  1543. }
  1544. return 0;
  1545. }
  1546. static int ad4170_gpio_init(struct iio_dev *indio_dev)
  1547. {
  1548. struct ad4170_state *st = iio_priv(indio_dev);
  1549. st->gpiochip.label = "ad4170_gpios";
  1550. st->gpiochip.base = -1;
  1551. st->gpiochip.ngpio = AD4170_NUM_GPIO_PINS;
  1552. st->gpiochip.parent = &st->spi->dev;
  1553. st->gpiochip.can_sleep = true;
  1554. st->gpiochip.init_valid_mask = ad4170_gpio_init_valid_mask;
  1555. st->gpiochip.get_direction = ad4170_gpio_get_direction;
  1556. st->gpiochip.direction_input = ad4170_gpio_direction_input;
  1557. st->gpiochip.direction_output = ad4170_gpio_direction_output;
  1558. st->gpiochip.get = ad4170_gpio_get;
  1559. st->gpiochip.set = ad4170_gpio_set;
  1560. st->gpiochip.owner = THIS_MODULE;
  1561. return devm_gpiochip_add_data(&st->spi->dev, &st->gpiochip, indio_dev);
  1562. }
  1563. static int ad4170_validate_excitation_pin(struct ad4170_state *st, u32 pin)
  1564. {
  1565. struct device *dev = &st->spi->dev;
  1566. unsigned int i;
  1567. /* Check the pin number is valid */
  1568. for (i = 0; i < ARRAY_SIZE(ad4170_iout_pin_tbl); i++)
  1569. if (ad4170_iout_pin_tbl[i] == pin)
  1570. break;
  1571. if (i == ARRAY_SIZE(ad4170_iout_pin_tbl))
  1572. return dev_err_probe(dev, -EINVAL,
  1573. "Invalid excitation pin: %u\n",
  1574. pin);
  1575. /* Check the pin is available */
  1576. if (pin <= AD4170_MAX_ANALOG_PINS) {
  1577. if (st->pins_fn[pin] != AD4170_PIN_UNASSIGNED)
  1578. return dev_err_probe(dev, -EINVAL,
  1579. "Pin %u already used with fn %u\n",
  1580. pin, st->pins_fn[pin]);
  1581. st->pins_fn[pin] |= AD4170_PIN_CURRENT_OUT;
  1582. } else {
  1583. unsigned int gpio = pin - AD4170_CURRENT_SRC_I_OUT_PIN_GPIO(0);
  1584. if (st->gpio_fn[gpio] != AD4170_GPIO_UNASSIGNED)
  1585. return dev_err_probe(dev, -EINVAL,
  1586. "GPIO %u already used with fn %u\n",
  1587. gpio, st->gpio_fn[gpio]);
  1588. st->gpio_fn[gpio] |= AD4170_GPIO_AC_EXCITATION;
  1589. }
  1590. return 0;
  1591. }
  1592. static int ad4170_validate_excitation_pins(struct ad4170_state *st,
  1593. u32 *exc_pins, int num_exc_pins)
  1594. {
  1595. unsigned int i;
  1596. int ret;
  1597. for (i = 0; i < num_exc_pins; i++) {
  1598. ret = ad4170_validate_excitation_pin(st, exc_pins[i]);
  1599. if (ret)
  1600. return ret;
  1601. }
  1602. return 0;
  1603. }
  1604. static const char *const ad4170_i_out_pin_dt_props[] = {
  1605. "adi,excitation-pin-0",
  1606. "adi,excitation-pin-1",
  1607. "adi,excitation-pin-2",
  1608. "adi,excitation-pin-3",
  1609. };
  1610. static const char *const ad4170_i_out_val_dt_props[] = {
  1611. "adi,excitation-current-0-microamp",
  1612. "adi,excitation-current-1-microamp",
  1613. "adi,excitation-current-2-microamp",
  1614. "adi,excitation-current-3-microamp",
  1615. };
  1616. /*
  1617. * Parses firmware data describing output current source setup. There are 4
  1618. * excitation currents (IOUT0 to IOUT3) that can be configured independently.
  1619. * Excitation currents are added if they are output on the same pin.
  1620. */
  1621. static int ad4170_parse_exc_current(struct ad4170_state *st,
  1622. struct fwnode_handle *child,
  1623. unsigned int *exc_pins,
  1624. unsigned int *exc_curs,
  1625. unsigned int *num_exc_pins)
  1626. {
  1627. struct device *dev = &st->spi->dev;
  1628. unsigned int num_pins, i, j;
  1629. u32 pin, val;
  1630. int ret;
  1631. num_pins = 0;
  1632. for (i = 0; i < AD4170_NUM_CURRENT_SRC; i++) {
  1633. /* Parse excitation current output pin properties. */
  1634. pin = AD4170_CURRENT_SRC_I_OUT_PIN_AIN(0);
  1635. ret = fwnode_property_read_u32(child, ad4170_i_out_pin_dt_props[i],
  1636. &pin);
  1637. if (ret)
  1638. continue;
  1639. exc_pins[num_pins] = pin;
  1640. /* Parse excitation current value properties. */
  1641. val = ad4170_iout_current_ua_tbl[0];
  1642. fwnode_property_read_u32(child,
  1643. ad4170_i_out_val_dt_props[i], &val);
  1644. for (j = 0; j < ARRAY_SIZE(ad4170_iout_current_ua_tbl); j++)
  1645. if (ad4170_iout_current_ua_tbl[j] == val)
  1646. break;
  1647. if (j == ARRAY_SIZE(ad4170_iout_current_ua_tbl))
  1648. return dev_err_probe(dev, -EINVAL, "Invalid %s: %uuA\n",
  1649. ad4170_i_out_val_dt_props[i], val);
  1650. exc_curs[num_pins] = j;
  1651. num_pins++;
  1652. }
  1653. *num_exc_pins = num_pins;
  1654. return 0;
  1655. }
  1656. static int ad4170_setup_current_src(struct ad4170_state *st,
  1657. struct fwnode_handle *child,
  1658. struct ad4170_setup *setup, u32 *exc_pins,
  1659. unsigned int *exc_curs, int num_exc_pins,
  1660. bool ac_excited)
  1661. {
  1662. unsigned int exc_cur_pair, i, j;
  1663. int ret;
  1664. for (i = 0; i < num_exc_pins; i++) {
  1665. unsigned int exc_cur = exc_curs[i];
  1666. unsigned int pin = exc_pins[i];
  1667. unsigned int current_src = 0;
  1668. for (j = 0; j < AD4170_NUM_CURRENT_SRC; j++)
  1669. if (st->cur_src_pins[j] == AD4170_CURRENT_SRC_DISABLED)
  1670. break;
  1671. if (j == AD4170_NUM_CURRENT_SRC)
  1672. return dev_err_probe(&st->spi->dev, -EINVAL,
  1673. "Too many excitation current sources\n");
  1674. current_src |= FIELD_PREP(AD4170_CURRENT_SRC_I_OUT_PIN_MSK, pin);
  1675. current_src |= FIELD_PREP(AD4170_CURRENT_SRC_I_OUT_VAL_MSK, exc_cur);
  1676. st->cur_src_pins[j] = pin;
  1677. ret = regmap_write(st->regmap, AD4170_CURRENT_SRC_REG(j),
  1678. current_src);
  1679. if (ret)
  1680. return ret;
  1681. }
  1682. if (!ac_excited)
  1683. return 0;
  1684. if (num_exc_pins < 2)
  1685. return dev_err_probe(&st->spi->dev, -EINVAL,
  1686. "Current chopping requested but only one pin provided: %u\n",
  1687. exc_pins[0]);
  1688. /*
  1689. * Two use cases to handle here:
  1690. * - 2 pairs of excitation currents;
  1691. * - 1 pair of excitation currents.
  1692. */
  1693. if (num_exc_pins == 4) {
  1694. for (i = 0; i < AD4170_NUM_CURRENT_SRC; i++)
  1695. if (st->cur_src_pins[i] != exc_pins[i])
  1696. return dev_err_probe(&st->spi->dev, -EINVAL,
  1697. "Unable to use 4 exc pins\n");
  1698. } else {
  1699. /*
  1700. * Excitation current chopping is configured in pairs. Current
  1701. * sources IOUT0 and IOUT1 form pair 1, IOUT2 and IOUT3 make up
  1702. * pair 2. So, if current chopping was requested, check if the
  1703. * first end of the first pair of excitation currents is
  1704. * available. Try the next pair if IOUT0 has already been
  1705. * configured for another channel.
  1706. */
  1707. i = st->cur_src_pins[0] == exc_pins[0] ? 0 : 2;
  1708. if (st->cur_src_pins[i] != exc_pins[0] ||
  1709. st->cur_src_pins[i + 1] != exc_pins[1])
  1710. return dev_err_probe(&st->spi->dev, -EINVAL,
  1711. "Failed to setup current chopping\n");
  1712. st->cur_src_pins[i] = exc_pins[0];
  1713. st->cur_src_pins[i + 1] = exc_pins[1];
  1714. if (i == 0)
  1715. exc_cur_pair = AD4170_MISC_CHOP_IEXC_PAIR1;
  1716. else
  1717. exc_cur_pair = AD4170_MISC_CHOP_IEXC_PAIR2;
  1718. }
  1719. /*
  1720. * Configure excitation current chopping.
  1721. * Chop both pairs if using four excitation pins.
  1722. */
  1723. setup->misc |= FIELD_PREP(AD4170_MISC_CHOP_IEXC_MSK,
  1724. num_exc_pins == 2 ?
  1725. exc_cur_pair :
  1726. AD4170_MISC_CHOP_IEXC_BOTH);
  1727. return 0;
  1728. }
  1729. static int ad4170_setup_bridge(struct ad4170_state *st,
  1730. struct fwnode_handle *child,
  1731. struct ad4170_setup *setup, u32 *exc_pins,
  1732. unsigned int *exc_curs, int num_exc_pins,
  1733. bool ac_excited)
  1734. {
  1735. unsigned long gpio_mask;
  1736. unsigned int i;
  1737. int ret;
  1738. /*
  1739. * If a specific current is provided through
  1740. * adi,excitation-current-n-microamp, set excitation pins provided
  1741. * through adi,excitation-pin-n to excite the bridge circuit.
  1742. */
  1743. for (i = 0; i < num_exc_pins; i++)
  1744. if (exc_curs[i] > 0)
  1745. return ad4170_setup_current_src(st, child, setup, exc_pins,
  1746. exc_curs, num_exc_pins,
  1747. ac_excited);
  1748. /*
  1749. * Else, use predefined ACX1, ACX1 negated, ACX2, ACX2 negated signals
  1750. * to AC excite the bridge. Those signals are output on GPIO2, GPIO0,
  1751. * GPIO3, and GPIO1, respectively. If only two pins are specified for AC
  1752. * excitation, use ACX1 and ACX2 (GPIO2 and GPIO3).
  1753. *
  1754. * Also, to avoid any short-circuit condition when more than one channel
  1755. * is enabled, set GPIO2 and GPIO0 high, and set GPIO1 and GPIO3 low to
  1756. * DC excite the bridge whenever a channel without AC excitation is
  1757. * selected. That is needed because GPIO pins are controlled by the next
  1758. * highest priority GPIO function when a channel doesn't enable AC
  1759. * excitation. See datasheet Figure 113 Weigh Scale (AC Excitation) for
  1760. * the reference circuit diagram.
  1761. */
  1762. if (num_exc_pins == 2) {
  1763. setup->misc |= FIELD_PREP(AD4170_MISC_CHOP_ADC_MSK, 0x3);
  1764. gpio_mask = AD4170_GPIO_MODE_GPIO3_MSK | AD4170_GPIO_MODE_GPIO2_MSK;
  1765. ret = regmap_update_bits(st->regmap, AD4170_GPIO_MODE_REG, gpio_mask,
  1766. FIELD_PREP(AD4170_GPIO_MODE_GPIO3_MSK,
  1767. AD4170_GPIO_MODE_GPIO_OUTPUT) |
  1768. FIELD_PREP(AD4170_GPIO_MODE_GPIO2_MSK,
  1769. AD4170_GPIO_MODE_GPIO_OUTPUT));
  1770. if (ret)
  1771. return ret;
  1772. /*
  1773. * Set GPIO2 high and GPIO3 low to DC excite the bridge when
  1774. * a different channel is selected.
  1775. */
  1776. gpio_mask = AD4170_GPIO_OUTPUT_GPIO_MSK(3) |
  1777. AD4170_GPIO_OUTPUT_GPIO_MSK(2);
  1778. ret = regmap_update_bits(st->regmap, AD4170_GPIO_OUTPUT_REG, gpio_mask,
  1779. FIELD_PREP(AD4170_GPIO_OUTPUT_GPIO_MSK(3), 0) |
  1780. FIELD_PREP(AD4170_GPIO_OUTPUT_GPIO_MSK(2), 1));
  1781. if (ret)
  1782. return ret;
  1783. st->gpio_fn[3] |= AD4170_GPIO_OUTPUT;
  1784. st->gpio_fn[2] |= AD4170_GPIO_OUTPUT;
  1785. } else {
  1786. setup->misc |= FIELD_PREP(AD4170_MISC_CHOP_ADC_MSK, 0x2);
  1787. gpio_mask = AD4170_GPIO_MODE_GPIO3_MSK | AD4170_GPIO_MODE_GPIO2_MSK |
  1788. AD4170_GPIO_MODE_GPIO1_MSK | AD4170_GPIO_MODE_GPIO0_MSK;
  1789. ret = regmap_update_bits(st->regmap, AD4170_GPIO_MODE_REG, gpio_mask,
  1790. FIELD_PREP(AD4170_GPIO_MODE_GPIO3_MSK,
  1791. AD4170_GPIO_MODE_GPIO_OUTPUT) |
  1792. FIELD_PREP(AD4170_GPIO_MODE_GPIO2_MSK,
  1793. AD4170_GPIO_MODE_GPIO_OUTPUT) |
  1794. FIELD_PREP(AD4170_GPIO_MODE_GPIO1_MSK,
  1795. AD4170_GPIO_MODE_GPIO_OUTPUT) |
  1796. FIELD_PREP(AD4170_GPIO_MODE_GPIO0_MSK,
  1797. AD4170_GPIO_MODE_GPIO_OUTPUT));
  1798. if (ret)
  1799. return ret;
  1800. /*
  1801. * Set GPIO2 and GPIO0 high, and set GPIO1 and GPIO3 low to DC
  1802. * excite the bridge when a different channel is selected.
  1803. */
  1804. gpio_mask = AD4170_GPIO_OUTPUT_GPIO_MSK(3) |
  1805. AD4170_GPIO_OUTPUT_GPIO_MSK(2) |
  1806. AD4170_GPIO_OUTPUT_GPIO_MSK(1) |
  1807. AD4170_GPIO_OUTPUT_GPIO_MSK(0);
  1808. ret = regmap_update_bits(st->regmap, AD4170_GPIO_OUTPUT_REG, gpio_mask,
  1809. FIELD_PREP(AD4170_GPIO_OUTPUT_GPIO_MSK(3), 0) |
  1810. FIELD_PREP(AD4170_GPIO_OUTPUT_GPIO_MSK(2), 1) |
  1811. FIELD_PREP(AD4170_GPIO_OUTPUT_GPIO_MSK(1), 0) |
  1812. FIELD_PREP(AD4170_GPIO_OUTPUT_GPIO_MSK(0), 1));
  1813. if (ret)
  1814. return ret;
  1815. st->gpio_fn[3] |= AD4170_GPIO_OUTPUT;
  1816. st->gpio_fn[2] |= AD4170_GPIO_OUTPUT;
  1817. st->gpio_fn[1] |= AD4170_GPIO_OUTPUT;
  1818. st->gpio_fn[0] |= AD4170_GPIO_OUTPUT;
  1819. }
  1820. return 0;
  1821. }
  1822. static int ad4170_setup_rtd(struct ad4170_state *st,
  1823. struct fwnode_handle *child,
  1824. struct ad4170_setup *setup, u32 *exc_pins,
  1825. unsigned int *exc_curs, int num_exc_pins, bool ac_excited)
  1826. {
  1827. return ad4170_setup_current_src(st, child, setup, exc_pins,
  1828. exc_curs, num_exc_pins, ac_excited);
  1829. }
  1830. static int ad4170_parse_external_sensor(struct ad4170_state *st,
  1831. struct fwnode_handle *child,
  1832. struct ad4170_setup *setup,
  1833. struct iio_chan_spec *chan,
  1834. unsigned int s_type)
  1835. {
  1836. unsigned int num_exc_pins, reg_val;
  1837. struct device *dev = &st->spi->dev;
  1838. u32 pins[2], exc_pins[4], exc_curs[4];
  1839. bool ac_excited;
  1840. int ret;
  1841. ret = fwnode_property_read_u32_array(child, "diff-channels", pins,
  1842. ARRAY_SIZE(pins));
  1843. if (ret)
  1844. return dev_err_probe(dev, ret,
  1845. "Failed to read sensor diff-channels\n");
  1846. chan->differential = true;
  1847. chan->channel = pins[0];
  1848. chan->channel2 = pins[1];
  1849. ret = ad4170_parse_exc_current(st, child, exc_pins, exc_curs, &num_exc_pins);
  1850. if (ret)
  1851. return ret;
  1852. /* The external sensor may not need excitation from the ADC chip. */
  1853. if (num_exc_pins == 0)
  1854. return 0;
  1855. ret = ad4170_validate_excitation_pins(st, exc_pins, num_exc_pins);
  1856. if (ret)
  1857. return ret;
  1858. ac_excited = fwnode_property_read_bool(child, "adi,excitation-ac");
  1859. if (s_type == AD4170_THERMOCOUPLE_SENSOR) {
  1860. if (st->pins_fn[chan->channel2] & AD4170_PIN_VBIAS) {
  1861. reg_val = BIT(chan->channel2);
  1862. ret = regmap_write(st->regmap, AD4170_V_BIAS_REG, reg_val);
  1863. if (ret)
  1864. dev_err_probe(dev, ret, "Failed to set vbias\n");
  1865. }
  1866. }
  1867. if (s_type == AD4170_WEIGH_SCALE_SENSOR)
  1868. ret = ad4170_setup_bridge(st, child, setup, exc_pins, exc_curs,
  1869. num_exc_pins, ac_excited);
  1870. else
  1871. ret = ad4170_setup_rtd(st, child, setup, exc_pins, exc_curs,
  1872. num_exc_pins, ac_excited);
  1873. return ret;
  1874. }
  1875. static int ad4170_parse_reference(struct ad4170_state *st,
  1876. struct fwnode_handle *child,
  1877. struct ad4170_setup *setup)
  1878. {
  1879. struct device *dev = &st->spi->dev;
  1880. const char *propname;
  1881. u32 aux;
  1882. int ret;
  1883. /* Optional positive reference buffering */
  1884. propname = "adi,positive-reference-buffer";
  1885. ret = device_property_match_property_string(dev, propname,
  1886. ad4170_ref_buf_str,
  1887. ARRAY_SIZE(ad4170_ref_buf_str));
  1888. /* Default to full precharge buffer enabled. */
  1889. setup->afe |= FIELD_PREP(AD4170_AFE_REF_BUF_P_MSK,
  1890. ret >= 0 ? ret : AD4170_REF_BUF_FULL);
  1891. /* Optional negative reference buffering */
  1892. propname = "adi,negative-reference-buffer";
  1893. ret = device_property_match_property_string(dev, propname,
  1894. ad4170_ref_buf_str,
  1895. ARRAY_SIZE(ad4170_ref_buf_str));
  1896. /* Default to full precharge buffer enabled. */
  1897. setup->afe |= FIELD_PREP(AD4170_AFE_REF_BUF_M_MSK,
  1898. ret >= 0 ? ret : AD4170_REF_BUF_FULL);
  1899. /* Optional voltage reference selection */
  1900. propname = "adi,reference-select";
  1901. aux = AD4170_REF_REFOUT; /* Default reference selection. */
  1902. fwnode_property_read_u32(child, propname, &aux);
  1903. if (aux > AD4170_REF_AVDD)
  1904. return dev_err_probe(dev, -EINVAL, "Invalid %s: %u\n",
  1905. propname, aux);
  1906. setup->afe |= FIELD_PREP(AD4170_AFE_REF_SELECT_MSK, aux);
  1907. return 0;
  1908. }
  1909. static int ad4170_parse_adc_channel_type(struct device *dev,
  1910. struct fwnode_handle *child,
  1911. struct iio_chan_spec *chan)
  1912. {
  1913. const char *propname, *propname2;
  1914. int ret, ret2;
  1915. u32 pins[2];
  1916. propname = "single-channel";
  1917. propname2 = "diff-channels";
  1918. if (!fwnode_property_present(child, propname) &&
  1919. !fwnode_property_present(child, propname2))
  1920. return dev_err_probe(dev, -EINVAL,
  1921. "Channel must define one of %s or %s.\n",
  1922. propname, propname2);
  1923. /* Parse differential channel configuration */
  1924. ret = fwnode_property_read_u32_array(child, propname2, pins,
  1925. ARRAY_SIZE(pins));
  1926. if (!ret) {
  1927. chan->differential = true;
  1928. chan->channel = pins[0];
  1929. chan->channel2 = pins[1];
  1930. return 0;
  1931. }
  1932. /* Failed to parse diff chan so try pseudo-diff chan props */
  1933. propname2 = "common-mode-channel";
  1934. if (fwnode_property_present(child, propname) &&
  1935. !fwnode_property_present(child, propname2))
  1936. return dev_err_probe(dev, -EINVAL,
  1937. "When %s is defined, %s must be defined too\n",
  1938. propname, propname2);
  1939. /* Parse pseudo-differential channel configuration */
  1940. ret = fwnode_property_read_u32(child, propname, &pins[0]);
  1941. ret2 = fwnode_property_read_u32(child, propname2, &pins[1]);
  1942. if (!ret && !ret2) {
  1943. chan->differential = false;
  1944. chan->channel = pins[0];
  1945. chan->channel2 = pins[1];
  1946. return 0;
  1947. }
  1948. return dev_err_probe(dev, -EINVAL,
  1949. "Failed to parse channel %lu input. %d, %d\n",
  1950. chan->address, ret, ret2);
  1951. }
  1952. static int ad4170_parse_channel_node(struct iio_dev *indio_dev,
  1953. struct fwnode_handle *child,
  1954. unsigned int chan_num)
  1955. {
  1956. struct ad4170_state *st = iio_priv(indio_dev);
  1957. unsigned int s_type = AD4170_ADC_SENSOR;
  1958. struct device *dev = &st->spi->dev;
  1959. struct ad4170_chan_info *chan_info;
  1960. struct ad4170_setup *setup;
  1961. struct iio_chan_spec *chan;
  1962. unsigned int ref_select;
  1963. unsigned int ch_reg;
  1964. bool bipolar;
  1965. int ret;
  1966. ret = fwnode_property_read_u32(child, "reg", &ch_reg);
  1967. if (ret)
  1968. return dev_err_probe(dev, ret, "Failed to read channel reg\n");
  1969. if (ch_reg >= AD4170_MAX_ADC_CHANNELS)
  1970. return dev_err_probe(dev, -EINVAL,
  1971. "Channel idx greater than no of channels\n");
  1972. chan = &st->chans[chan_num];
  1973. *chan = ad4170_channel_template;
  1974. chan->address = ch_reg;
  1975. chan->scan_index = ch_reg;
  1976. chan_info = &st->chan_infos[chan->address];
  1977. chan_info->setup_num = AD4170_INVALID_SETUP;
  1978. chan_info->initialized = true;
  1979. setup = &chan_info->setup;
  1980. ret = ad4170_parse_reference(st, child, setup);
  1981. if (ret)
  1982. return ret;
  1983. ret = fwnode_property_match_property_string(child, "adi,sensor-type",
  1984. ad4170_sensor_type,
  1985. ARRAY_SIZE(ad4170_sensor_type));
  1986. /* Default to conventional ADC channel if sensor type not present */
  1987. s_type = ret < 0 ? AD4170_ADC_SENSOR : ret;
  1988. switch (s_type) {
  1989. case AD4170_ADC_SENSOR:
  1990. ret = ad4170_parse_adc_channel_type(dev, child, chan);
  1991. if (ret)
  1992. return ret;
  1993. break;
  1994. case AD4170_WEIGH_SCALE_SENSOR:
  1995. case AD4170_THERMOCOUPLE_SENSOR:
  1996. case AD4170_RTD_SENSOR:
  1997. ret = ad4170_parse_external_sensor(st, child, setup, chan, s_type);
  1998. if (ret)
  1999. return ret;
  2000. break;
  2001. default:
  2002. return -EINVAL;
  2003. }
  2004. bipolar = fwnode_property_read_bool(child, "bipolar");
  2005. setup->afe |= FIELD_PREP(AD4170_AFE_BIPOLAR_MSK, bipolar);
  2006. if (bipolar)
  2007. chan->scan_type.sign = 's';
  2008. else
  2009. chan->scan_type.sign = 'u';
  2010. ret = ad4170_validate_channel(st, chan);
  2011. if (ret)
  2012. return ret;
  2013. ref_select = FIELD_GET(AD4170_AFE_REF_SELECT_MSK, setup->afe);
  2014. ret = ad4170_get_input_range(st, chan, ch_reg, ref_select);
  2015. if (ret < 0)
  2016. return dev_err_probe(dev, ret, "Invalid input config\n");
  2017. chan_info->input_range_uv = ret;
  2018. return 0;
  2019. }
  2020. static int ad4170_parse_channels(struct iio_dev *indio_dev)
  2021. {
  2022. struct ad4170_state *st = iio_priv(indio_dev);
  2023. struct device *dev = &st->spi->dev;
  2024. unsigned int num_channels;
  2025. unsigned int chan_num;
  2026. int ret;
  2027. num_channels = device_get_child_node_count(dev);
  2028. if (num_channels > AD4170_MAX_ADC_CHANNELS)
  2029. return dev_err_probe(dev, -EINVAL, "Too many channels\n");
  2030. /* Add one for temperature */
  2031. num_channels = min(num_channels + 1, AD4170_MAX_ADC_CHANNELS);
  2032. chan_num = 0;
  2033. device_for_each_child_node_scoped(dev, child) {
  2034. ret = ad4170_parse_channel_node(indio_dev, child, chan_num++);
  2035. if (ret)
  2036. return ret;
  2037. }
  2038. /*
  2039. * Add internal temperature sensor channel if the maximum number of
  2040. * channels has not been reached.
  2041. */
  2042. if (num_channels < AD4170_MAX_ADC_CHANNELS) {
  2043. struct ad4170_setup *setup = &st->chan_infos[chan_num].setup;
  2044. st->chans[chan_num] = ad4170_temp_channel_template;
  2045. st->chans[chan_num].address = chan_num;
  2046. st->chans[chan_num].scan_index = chan_num;
  2047. st->chan_infos[chan_num].setup_num = AD4170_INVALID_SETUP;
  2048. st->chan_infos[chan_num].initialized = true;
  2049. setup->afe |= FIELD_PREP(AD4170_AFE_REF_SELECT_MSK,
  2050. AD4170_REF_AVDD);
  2051. ret = ad4170_get_input_range(st, &st->chans[chan_num], chan_num,
  2052. AD4170_REF_AVDD);
  2053. if (ret < 0)
  2054. return dev_err_probe(dev, ret, "Invalid input config\n");
  2055. st->chan_infos[chan_num].input_range_uv = ret;
  2056. chan_num++;
  2057. }
  2058. /* Add timestamp channel */
  2059. struct iio_chan_spec ts_chan = IIO_CHAN_SOFT_TIMESTAMP(chan_num);
  2060. st->chans[chan_num] = ts_chan;
  2061. num_channels = num_channels + 1;
  2062. indio_dev->num_channels = num_channels;
  2063. indio_dev->channels = st->chans;
  2064. return 0;
  2065. }
  2066. static struct ad4170_state *clk_hw_to_ad4170(struct clk_hw *hw)
  2067. {
  2068. return container_of(hw, struct ad4170_state, int_clk_hw);
  2069. }
  2070. static unsigned long ad4170_sel_clk(struct ad4170_state *st,
  2071. unsigned int clk_sel)
  2072. {
  2073. st->clock_ctrl &= ~AD4170_CLOCK_CTRL_CLOCKSEL_MSK;
  2074. st->clock_ctrl |= FIELD_PREP(AD4170_CLOCK_CTRL_CLOCKSEL_MSK, clk_sel);
  2075. return regmap_write(st->regmap, AD4170_CLOCK_CTRL_REG, st->clock_ctrl);
  2076. }
  2077. static unsigned long ad4170_clk_recalc_rate(struct clk_hw *hw,
  2078. unsigned long parent_rate)
  2079. {
  2080. return AD4170_INT_CLOCK_16MHZ;
  2081. }
  2082. static int ad4170_clk_output_is_enabled(struct clk_hw *hw)
  2083. {
  2084. struct ad4170_state *st = clk_hw_to_ad4170(hw);
  2085. u32 clk_sel;
  2086. clk_sel = FIELD_GET(AD4170_CLOCK_CTRL_CLOCKSEL_MSK, st->clock_ctrl);
  2087. return clk_sel == AD4170_CLOCK_CTRL_CLOCKSEL_INT_OUT;
  2088. }
  2089. static int ad4170_clk_output_prepare(struct clk_hw *hw)
  2090. {
  2091. struct ad4170_state *st = clk_hw_to_ad4170(hw);
  2092. return ad4170_sel_clk(st, AD4170_CLOCK_CTRL_CLOCKSEL_INT_OUT);
  2093. }
  2094. static void ad4170_clk_output_unprepare(struct clk_hw *hw)
  2095. {
  2096. struct ad4170_state *st = clk_hw_to_ad4170(hw);
  2097. ad4170_sel_clk(st, AD4170_CLOCK_CTRL_CLOCKSEL_INT);
  2098. }
  2099. static const struct clk_ops ad4170_int_clk_ops = {
  2100. .recalc_rate = ad4170_clk_recalc_rate,
  2101. .is_enabled = ad4170_clk_output_is_enabled,
  2102. .prepare = ad4170_clk_output_prepare,
  2103. .unprepare = ad4170_clk_output_unprepare,
  2104. };
  2105. static int ad4170_register_clk_provider(struct iio_dev *indio_dev)
  2106. {
  2107. struct ad4170_state *st = iio_priv(indio_dev);
  2108. struct device *dev = indio_dev->dev.parent;
  2109. struct clk_init_data init = {};
  2110. int ret;
  2111. if (device_property_read_string(dev, "clock-output-names", &init.name)) {
  2112. init.name = devm_kasprintf(dev, GFP_KERNEL, "%pfw",
  2113. dev_fwnode(dev));
  2114. if (!init.name)
  2115. return -ENOMEM;
  2116. }
  2117. init.ops = &ad4170_int_clk_ops;
  2118. st->int_clk_hw.init = &init;
  2119. ret = devm_clk_hw_register(dev, &st->int_clk_hw);
  2120. if (ret)
  2121. return ret;
  2122. return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
  2123. &st->int_clk_hw);
  2124. }
  2125. static int ad4170_clock_select(struct iio_dev *indio_dev)
  2126. {
  2127. struct ad4170_state *st = iio_priv(indio_dev);
  2128. struct device *dev = &st->spi->dev;
  2129. struct clk *ext_clk;
  2130. int ret;
  2131. ext_clk = devm_clk_get_optional_enabled(dev, NULL);
  2132. if (IS_ERR(ext_clk))
  2133. return dev_err_probe(dev, PTR_ERR(ext_clk),
  2134. "Failed to get external clock\n");
  2135. if (!ext_clk) {
  2136. /* Use internal clock reference */
  2137. st->mclk_hz = AD4170_INT_CLOCK_16MHZ;
  2138. st->clock_ctrl |= FIELD_PREP(AD4170_CLOCK_CTRL_CLOCKSEL_MSK,
  2139. AD4170_CLOCK_CTRL_CLOCKSEL_INT_OUT);
  2140. if (!device_property_present(&st->spi->dev, "#clock-cells"))
  2141. return 0;
  2142. return ad4170_register_clk_provider(indio_dev);
  2143. }
  2144. /* Read optional clock-names prop to specify the external clock type */
  2145. ret = device_property_match_property_string(dev, "clock-names",
  2146. ad4170_clk_sel,
  2147. ARRAY_SIZE(ad4170_clk_sel));
  2148. ret = ret < 0 ? 0 : ret; /* Default to external clock if no clock-names */
  2149. st->clock_ctrl |= FIELD_PREP(AD4170_CLOCK_CTRL_CLOCKSEL_MSK,
  2150. AD4170_CLOCK_CTRL_CLOCKSEL_EXT + ret);
  2151. st->mclk_hz = clk_get_rate(ext_clk);
  2152. if (st->mclk_hz < AD4170_EXT_CLOCK_MHZ_MIN ||
  2153. st->mclk_hz > AD4170_EXT_CLOCK_MHZ_MAX) {
  2154. return dev_err_probe(dev, -EINVAL,
  2155. "Invalid external clock frequency %u\n",
  2156. st->mclk_hz);
  2157. }
  2158. return 0;
  2159. }
  2160. static int ad4170_parse_firmware(struct iio_dev *indio_dev)
  2161. {
  2162. unsigned int vbias_pins[AD4170_MAX_ANALOG_PINS];
  2163. struct ad4170_state *st = iio_priv(indio_dev);
  2164. struct device *dev = &st->spi->dev;
  2165. unsigned int num_vbias_pins;
  2166. int reg_data, ret;
  2167. u32 int_pin_sel;
  2168. unsigned int i;
  2169. ret = ad4170_clock_select(indio_dev);
  2170. if (ret)
  2171. return dev_err_probe(dev, ret, "Failed to setup device clock\n");
  2172. ret = regmap_write(st->regmap, AD4170_CLOCK_CTRL_REG, st->clock_ctrl);
  2173. if (ret)
  2174. return ret;
  2175. for (i = 0; i < AD4170_NUM_CURRENT_SRC; i++)
  2176. st->cur_src_pins[i] = AD4170_CURRENT_SRC_DISABLED;
  2177. /* On power on, device defaults to using SDO pin for data ready signal */
  2178. int_pin_sel = AD4170_INT_PIN_SDO;
  2179. ret = device_property_match_property_string(dev, "interrupt-names",
  2180. ad4170_int_pin_names,
  2181. ARRAY_SIZE(ad4170_int_pin_names));
  2182. if (ret >= 0)
  2183. int_pin_sel = ret;
  2184. reg_data = FIELD_PREP(AD4170_PIN_MUXING_DIG_AUX1_CTRL_MSK,
  2185. int_pin_sel == AD4170_INT_PIN_DIG_AUX1 ?
  2186. AD4170_PIN_MUXING_DIG_AUX1_RDY :
  2187. AD4170_PIN_MUXING_DIG_AUX1_DISABLED);
  2188. ret = regmap_update_bits(st->regmap, AD4170_PIN_MUXING_REG,
  2189. AD4170_PIN_MUXING_DIG_AUX1_CTRL_MSK, reg_data);
  2190. if (ret)
  2191. return ret;
  2192. ret = device_property_count_u32(dev, "adi,vbias-pins");
  2193. if (ret > 0) {
  2194. if (ret > AD4170_MAX_ANALOG_PINS)
  2195. return dev_err_probe(dev, -EINVAL,
  2196. "Too many vbias pins %u\n", ret);
  2197. num_vbias_pins = ret;
  2198. ret = device_property_read_u32_array(dev, "adi,vbias-pins",
  2199. vbias_pins,
  2200. num_vbias_pins);
  2201. if (ret)
  2202. return dev_err_probe(dev, ret,
  2203. "Failed to read vbias pins\n");
  2204. for (i = 0; i < num_vbias_pins; i++)
  2205. st->pins_fn[vbias_pins[i]] |= AD4170_PIN_VBIAS;
  2206. }
  2207. ret = ad4170_parse_channels(indio_dev);
  2208. if (ret)
  2209. return ret;
  2210. /* Only create a GPIO chip if flagged for it */
  2211. if (device_property_read_bool(dev, "gpio-controller")) {
  2212. ret = ad4170_gpio_init(indio_dev);
  2213. if (ret)
  2214. return ret;
  2215. }
  2216. return 0;
  2217. }
  2218. static int ad4170_initial_config(struct iio_dev *indio_dev)
  2219. {
  2220. struct ad4170_state *st = iio_priv(indio_dev);
  2221. struct device *dev = &st->spi->dev;
  2222. unsigned int i;
  2223. int ret;
  2224. ad4170_fill_sps_tbl(st);
  2225. ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
  2226. AD4170_ADC_CTRL_MODE_MSK,
  2227. FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK,
  2228. AD4170_ADC_CTRL_MODE_IDLE));
  2229. if (ret)
  2230. return dev_err_probe(dev, ret,
  2231. "Failed to set ADC mode to idle\n");
  2232. for (i = 0; i < indio_dev->num_channels; i++) {
  2233. struct ad4170_chan_info *chan_info;
  2234. struct iio_chan_spec const *chan;
  2235. struct ad4170_setup *setup;
  2236. unsigned int val;
  2237. chan = &indio_dev->channels[i];
  2238. if (chan->type == IIO_TIMESTAMP)
  2239. continue;
  2240. chan_info = &st->chan_infos[chan->address];
  2241. setup = &chan_info->setup;
  2242. setup->gain = AD4170_GAIN_REG_DEFAULT;
  2243. ret = ad4170_write_channel_setup(st, chan->address, false);
  2244. if (ret)
  2245. return dev_err_probe(dev, ret,
  2246. "Failed to write channel setup\n");
  2247. val = FIELD_PREP(AD4170_CHAN_MAP_AINP_MSK, chan->channel) |
  2248. FIELD_PREP(AD4170_CHAN_MAP_AINM_MSK, chan->channel2);
  2249. ret = regmap_write(st->regmap, AD4170_CHAN_MAP_REG(i), val);
  2250. if (ret)
  2251. return dev_err_probe(dev, ret,
  2252. "Failed to write CHAN_MAP_REG\n");
  2253. ret = ad4170_set_channel_freq(st, chan,
  2254. AD4170_DEFAULT_SAMP_RATE, 0);
  2255. if (ret)
  2256. return dev_err_probe(dev, ret,
  2257. "Failed to set channel freq\n");
  2258. ret = ad4170_fill_scale_tbl(indio_dev, chan);
  2259. if (ret)
  2260. return dev_err_probe(dev, ret,
  2261. "Failed to fill scale tbl\n");
  2262. }
  2263. /* Disable all channels to avoid reading from unexpected channel */
  2264. ret = regmap_write(st->regmap, AD4170_CHAN_EN_REG, 0);
  2265. if (ret)
  2266. return dev_err_probe(dev, ret,
  2267. "Failed to disable channels\n");
  2268. /*
  2269. * Configure channels to share the same data output register, i.e. data
  2270. * can be read from the same register address regardless of channel
  2271. * number.
  2272. */
  2273. return regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
  2274. AD4170_ADC_CTRL_MULTI_DATA_REG_SEL_MSK,
  2275. AD4170_ADC_CTRL_MULTI_DATA_REG_SEL_MSK);
  2276. }
  2277. static int ad4170_prepare_spi_message(struct ad4170_state *st)
  2278. {
  2279. /*
  2280. * Continuous data register read is enabled on buffer postenable so
  2281. * no instruction phase is needed meaning we don't need to send the
  2282. * register address to read data. Transfer only needs the read buffer.
  2283. */
  2284. st->xfer.rx_buf = &st->rx_buf;
  2285. st->xfer.len = BITS_TO_BYTES(ad4170_channel_template.scan_type.realbits);
  2286. spi_message_init_with_transfers(&st->msg, &st->xfer, 1);
  2287. return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg);
  2288. }
  2289. static int ad4170_buffer_postenable(struct iio_dev *indio_dev)
  2290. {
  2291. struct ad4170_state *st = iio_priv(indio_dev);
  2292. int ret;
  2293. ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
  2294. AD4170_ADC_CTRL_MODE_MSK,
  2295. FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK,
  2296. AD4170_ADC_CTRL_MODE_CONT));
  2297. if (ret)
  2298. return ret;
  2299. /*
  2300. * This enables continuous read of the ADC data register. The ADC must
  2301. * be in continuous conversion mode.
  2302. */
  2303. return regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
  2304. AD4170_ADC_CTRL_CONT_READ_MSK,
  2305. FIELD_PREP(AD4170_ADC_CTRL_CONT_READ_MSK,
  2306. AD4170_ADC_CTRL_CONT_READ_ENABLE));
  2307. }
  2308. static int ad4170_buffer_predisable(struct iio_dev *indio_dev)
  2309. {
  2310. struct ad4170_state *st = iio_priv(indio_dev);
  2311. unsigned int i;
  2312. int ret;
  2313. /*
  2314. * Use a high register address (virtual register) to request a write of
  2315. * 0xA5 to the ADC during the first 8 SCLKs of the ADC data read cycle,
  2316. * thus exiting continuous read.
  2317. */
  2318. ret = regmap_write(st->regmap, AD4170_ADC_CTRL_CONT_READ_EXIT_REG, 0);
  2319. if (ret)
  2320. return ret;
  2321. ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
  2322. AD4170_ADC_CTRL_CONT_READ_MSK,
  2323. FIELD_PREP(AD4170_ADC_CTRL_CONT_READ_MSK,
  2324. AD4170_ADC_CTRL_CONT_READ_DISABLE));
  2325. if (ret)
  2326. return ret;
  2327. ret = regmap_update_bits(st->regmap, AD4170_ADC_CTRL_REG,
  2328. AD4170_ADC_CTRL_MODE_MSK,
  2329. FIELD_PREP(AD4170_ADC_CTRL_MODE_MSK,
  2330. AD4170_ADC_CTRL_MODE_IDLE));
  2331. if (ret)
  2332. return ret;
  2333. /*
  2334. * The ADC sequences through all the enabled channels (see datasheet
  2335. * page 95). That can lead to incorrect channel being read if a
  2336. * single-shot read (or buffered read with different active_scan_mask)
  2337. * is done after buffer disable. Disable all channels so only requested
  2338. * channels will be read.
  2339. */
  2340. for (i = 0; i < indio_dev->num_channels; i++) {
  2341. if (indio_dev->channels[i].type == IIO_TIMESTAMP)
  2342. continue;
  2343. ret = ad4170_set_channel_enable(st, i, false);
  2344. if (ret)
  2345. return ret;
  2346. }
  2347. return 0;
  2348. }
  2349. static bool ad4170_validate_scan_mask(struct iio_dev *indio_dev,
  2350. const unsigned long *scan_mask)
  2351. {
  2352. unsigned int masklength = iio_get_masklength(indio_dev);
  2353. unsigned int enabled;
  2354. /*
  2355. * The channel sequencer cycles through the enabled channels in
  2356. * sequential order, from channel 0 to channel 15, bypassing disabled
  2357. * channels. When more than one channel is enabled, channel 0 must
  2358. * always be enabled. See datasheet channel_en register description at
  2359. * page 95.
  2360. */
  2361. enabled = bitmap_weight(scan_mask, masklength);
  2362. if (enabled > 1)
  2363. return test_bit(0, scan_mask);
  2364. return enabled == 1;
  2365. }
  2366. static const struct iio_buffer_setup_ops ad4170_buffer_ops = {
  2367. .postenable = ad4170_buffer_postenable,
  2368. .predisable = ad4170_buffer_predisable,
  2369. .validate_scan_mask = ad4170_validate_scan_mask,
  2370. };
  2371. static irqreturn_t ad4170_trigger_handler(int irq, void *p)
  2372. {
  2373. struct iio_poll_func *pf = p;
  2374. struct iio_dev *indio_dev = pf->indio_dev;
  2375. struct ad4170_state *st = iio_priv(indio_dev);
  2376. unsigned int chan_index;
  2377. unsigned int i = 0;
  2378. int ret;
  2379. iio_for_each_active_channel(indio_dev, chan_index) {
  2380. ret = spi_sync(st->spi, &st->msg);
  2381. if (ret)
  2382. goto err_out;
  2383. memcpy(&st->bounce_buffer[i++], st->rx_buf, ARRAY_SIZE(st->rx_buf));
  2384. }
  2385. iio_push_to_buffers_with_ts(indio_dev, st->bounce_buffer,
  2386. sizeof(st->bounce_buffer),
  2387. iio_get_time_ns(indio_dev));
  2388. err_out:
  2389. iio_trigger_notify_done(indio_dev->trig);
  2390. return IRQ_HANDLED;
  2391. }
  2392. static const struct iio_trigger_ops ad4170_trigger_ops = {
  2393. .validate_device = iio_trigger_validate_own_device,
  2394. };
  2395. static irqreturn_t ad4170_irq_handler(int irq, void *dev_id)
  2396. {
  2397. struct iio_dev *indio_dev = dev_id;
  2398. struct ad4170_state *st = iio_priv(indio_dev);
  2399. if (iio_buffer_enabled(indio_dev))
  2400. iio_trigger_poll(st->trig);
  2401. else
  2402. complete(&st->completion);
  2403. return IRQ_HANDLED;
  2404. };
  2405. static int ad4170_trigger_setup(struct iio_dev *indio_dev)
  2406. {
  2407. struct ad4170_state *st = iio_priv(indio_dev);
  2408. struct device *dev = &st->spi->dev;
  2409. int ret;
  2410. st->trig = devm_iio_trigger_alloc(dev, "%s-trig%d",
  2411. indio_dev->name,
  2412. iio_device_id(indio_dev));
  2413. if (!st->trig)
  2414. return -ENOMEM;
  2415. st->trig->ops = &ad4170_trigger_ops;
  2416. iio_trigger_set_drvdata(st->trig, indio_dev);
  2417. ret = devm_iio_trigger_register(dev, st->trig);
  2418. if (ret)
  2419. return dev_err_probe(dev, ret, "Failed to register trigger\n");
  2420. indio_dev->trig = iio_trigger_get(st->trig);
  2421. return 0;
  2422. }
  2423. static int ad4170_regulator_setup(struct ad4170_state *st)
  2424. {
  2425. struct device *dev = &st->spi->dev;
  2426. int ret;
  2427. /* Required regulators */
  2428. ret = devm_regulator_get_enable_read_voltage(dev, "avdd");
  2429. if (ret < 0)
  2430. return dev_err_probe(dev, ret, "Failed to get AVDD voltage.\n");
  2431. st->vrefs_uv[AD4170_AVDD_SUP] = ret;
  2432. ret = devm_regulator_get_enable_read_voltage(dev, "iovdd");
  2433. if (ret < 0)
  2434. return dev_err_probe(dev, ret, "Failed to get IOVDD voltage.\n");
  2435. st->vrefs_uv[AD4170_IOVDD_SUP] = ret;
  2436. /* Optional regulators */
  2437. ret = devm_regulator_get_enable_read_voltage(dev, "avss");
  2438. if (ret < 0 && ret != -ENODEV)
  2439. return dev_err_probe(dev, ret, "Failed to get AVSS voltage.\n");
  2440. /*
  2441. * Assume AVSS at GND (0V) if not provided.
  2442. * REVISIT: AVSS is never above system ground level (i.e. AVSS is either
  2443. * GND or a negative voltage). But we currently don't have support for
  2444. * reading negative voltages with the regulator framework. So, the
  2445. * current AD4170 support reads a positive value from the regulator,
  2446. * then inverts sign to make that negative.
  2447. */
  2448. st->vrefs_uv[AD4170_AVSS_SUP] = ret == -ENODEV ? 0 : -ret;
  2449. ret = devm_regulator_get_enable_read_voltage(dev, "refin1p");
  2450. if (ret < 0 && ret != -ENODEV)
  2451. return dev_err_probe(dev, ret, "Failed to get REFIN+ voltage.\n");
  2452. st->vrefs_uv[AD4170_REFIN1P_SUP] = ret;
  2453. ret = devm_regulator_get_enable_read_voltage(dev, "refin1n");
  2454. if (ret < 0 && ret != -ENODEV)
  2455. return dev_err_probe(dev, ret, "Failed to get REFIN- voltage.\n");
  2456. /*
  2457. * Negative supplies are assumed to provide negative voltage.
  2458. * REVISIT when support for negative regulator voltage read be available
  2459. * in the regulator framework.
  2460. */
  2461. st->vrefs_uv[AD4170_REFIN1N_SUP] = ret == -ENODEV ? -ENODEV : -ret;
  2462. ret = devm_regulator_get_enable_read_voltage(dev, "refin2p");
  2463. if (ret < 0 && ret != -ENODEV)
  2464. return dev_err_probe(dev, ret, "Failed to get REFIN2+ voltage.\n");
  2465. st->vrefs_uv[AD4170_REFIN2P_SUP] = ret;
  2466. ret = devm_regulator_get_enable_read_voltage(dev, "refin2n");
  2467. if (ret < 0 && ret != -ENODEV)
  2468. return dev_err_probe(dev, ret, "Failed to get REFIN2- voltage.\n");
  2469. /*
  2470. * Negative supplies are assumed to provide negative voltage.
  2471. * REVISIT when support for negative regulator voltage read be available
  2472. * in the regulator framework.
  2473. */
  2474. st->vrefs_uv[AD4170_REFIN2N_SUP] = ret == -ENODEV ? -ENODEV : -ret;
  2475. return 0;
  2476. }
  2477. static int ad4170_probe(struct spi_device *spi)
  2478. {
  2479. const struct ad4170_chip_info *chip;
  2480. struct device *dev = &spi->dev;
  2481. struct iio_dev *indio_dev;
  2482. struct ad4170_state *st;
  2483. int ret;
  2484. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  2485. if (!indio_dev)
  2486. return -ENOMEM;
  2487. st = iio_priv(indio_dev);
  2488. st->spi = spi;
  2489. ret = devm_mutex_init(dev, &st->lock);
  2490. if (ret)
  2491. return ret;
  2492. chip = spi_get_device_match_data(spi);
  2493. if (!chip)
  2494. return -EINVAL;
  2495. indio_dev->name = chip->name;
  2496. indio_dev->info = &ad4170_info;
  2497. st->regmap = devm_regmap_init(dev, NULL, st, &ad4170_regmap_config);
  2498. if (IS_ERR(st->regmap))
  2499. return dev_err_probe(dev, PTR_ERR(st->regmap),
  2500. "Failed to initialize regmap\n");
  2501. ret = ad4170_regulator_setup(st);
  2502. if (ret)
  2503. return ret;
  2504. ret = ad4170_soft_reset(st);
  2505. if (ret)
  2506. return ret;
  2507. ret = ad4170_parse_firmware(indio_dev);
  2508. if (ret)
  2509. return dev_err_probe(dev, ret, "Failed to parse firmware\n");
  2510. ret = ad4170_initial_config(indio_dev);
  2511. if (ret)
  2512. return dev_err_probe(dev, ret, "Failed to setup device\n");
  2513. init_completion(&st->completion);
  2514. if (spi->irq) {
  2515. ret = devm_request_irq(dev, spi->irq, &ad4170_irq_handler,
  2516. IRQF_NO_THREAD, indio_dev->name, indio_dev);
  2517. if (ret)
  2518. return ret;
  2519. ret = ad4170_trigger_setup(indio_dev);
  2520. if (ret)
  2521. return ret;
  2522. }
  2523. ret = ad4170_prepare_spi_message(st);
  2524. if (ret)
  2525. return dev_err_probe(dev, ret, "Failed to prepare SPI message\n");
  2526. ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
  2527. &ad4170_trigger_handler,
  2528. &ad4170_buffer_ops);
  2529. if (ret)
  2530. return dev_err_probe(dev, ret, "Failed to setup read buffer\n");
  2531. return devm_iio_device_register(dev, indio_dev);
  2532. }
  2533. static const struct spi_device_id ad4170_id_table[] = {
  2534. { "ad4170-4", (kernel_ulong_t)&ad4170_chip_info },
  2535. { "ad4190-4", (kernel_ulong_t)&ad4190_chip_info },
  2536. { "ad4195-4", (kernel_ulong_t)&ad4195_chip_info },
  2537. { }
  2538. };
  2539. MODULE_DEVICE_TABLE(spi, ad4170_id_table);
  2540. static const struct of_device_id ad4170_of_match[] = {
  2541. { .compatible = "adi,ad4170-4", .data = &ad4170_chip_info },
  2542. { .compatible = "adi,ad4190-4", .data = &ad4190_chip_info },
  2543. { .compatible = "adi,ad4195-4", .data = &ad4195_chip_info },
  2544. { }
  2545. };
  2546. MODULE_DEVICE_TABLE(of, ad4170_of_match);
  2547. static struct spi_driver ad4170_driver = {
  2548. .driver = {
  2549. .name = "ad4170-4",
  2550. .of_match_table = ad4170_of_match,
  2551. },
  2552. .probe = ad4170_probe,
  2553. .id_table = ad4170_id_table,
  2554. };
  2555. module_spi_driver(ad4170_driver);
  2556. MODULE_AUTHOR("Ana-Maria Cusco <ana-maria.cusco@analog.com>");
  2557. MODULE_AUTHOR("Marcelo Schmitt <marcelo.schmitt@analog.com>");
  2558. MODULE_DESCRIPTION("Analog Devices AD4170 SPI driver");
  2559. MODULE_LICENSE("GPL");