ad4130.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2022 Analog Devices, Inc.
  4. * Author: Cosmin Tanislav <cosmin.tanislav@analog.com>
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/bitops.h>
  8. #include <linux/cleanup.h>
  9. #include <linux/clk.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/property.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/units.h>
  24. #include <asm/div64.h>
  25. #include <linux/unaligned.h>
  26. #include <linux/iio/buffer.h>
  27. #include <linux/iio/iio.h>
  28. #include <linux/iio/kfifo_buf.h>
  29. #include <linux/iio/sysfs.h>
  30. #define AD4130_NAME "ad4130"
  31. #define AD4130_COMMS_READ_MASK BIT(6)
  32. #define AD4130_STATUS_REG 0x00
  33. #define AD4130_ADC_CONTROL_REG 0x01
  34. #define AD4130_ADC_CONTROL_BIPOLAR_MASK BIT(14)
  35. #define AD4130_ADC_CONTROL_INT_REF_VAL_MASK BIT(13)
  36. #define AD4130_INT_REF_2_5V 2500000
  37. #define AD4130_INT_REF_1_25V 1250000
  38. #define AD4130_ADC_CONTROL_CSB_EN_MASK BIT(9)
  39. #define AD4130_ADC_CONTROL_INT_REF_EN_MASK BIT(8)
  40. #define AD4130_ADC_CONTROL_MODE_MASK GENMASK(5, 2)
  41. #define AD4130_ADC_CONTROL_MCLK_SEL_MASK GENMASK(1, 0)
  42. #define AD4130_MCLK_FREQ_76_8KHZ 76800
  43. #define AD4130_MCLK_FREQ_153_6KHZ 153600
  44. #define AD4130_DATA_REG 0x02
  45. #define AD4130_IO_CONTROL_REG 0x03
  46. #define AD4130_IO_CONTROL_INT_PIN_SEL_MASK GENMASK(9, 8)
  47. #define AD4130_IO_CONTROL_GPIO_DATA_MASK GENMASK(7, 4)
  48. #define AD4130_IO_CONTROL_GPIO_CTRL_MASK GENMASK(3, 0)
  49. #define AD4130_VBIAS_REG 0x04
  50. #define AD4130_ID_REG 0x05
  51. #define AD4130_ERROR_REG 0x06
  52. #define AD4130_ERROR_EN_REG 0x07
  53. #define AD4130_MCLK_COUNT_REG 0x08
  54. #define AD4130_CHANNEL_X_REG(x) (0x09 + (x))
  55. #define AD4130_CHANNEL_EN_MASK BIT(23)
  56. #define AD4130_CHANNEL_SETUP_MASK GENMASK(22, 20)
  57. #define AD4130_CHANNEL_AINP_MASK GENMASK(17, 13)
  58. #define AD4130_CHANNEL_AINM_MASK GENMASK(12, 8)
  59. #define AD4130_CHANNEL_IOUT1_MASK GENMASK(7, 4)
  60. #define AD4130_CHANNEL_IOUT2_MASK GENMASK(3, 0)
  61. #define AD4130_CONFIG_X_REG(x) (0x19 + (x))
  62. #define AD4130_CONFIG_IOUT1_VAL_MASK GENMASK(15, 13)
  63. #define AD4130_CONFIG_IOUT2_VAL_MASK GENMASK(12, 10)
  64. #define AD4130_CONFIG_BURNOUT_MASK GENMASK(9, 8)
  65. #define AD4130_CONFIG_REF_BUFP_MASK BIT(7)
  66. #define AD4130_CONFIG_REF_BUFM_MASK BIT(6)
  67. #define AD4130_CONFIG_REF_SEL_MASK GENMASK(5, 4)
  68. #define AD4130_CONFIG_PGA_MASK GENMASK(3, 1)
  69. #define AD4130_FILTER_X_REG(x) (0x21 + (x))
  70. #define AD4130_FILTER_MODE_MASK GENMASK(15, 12)
  71. #define AD4130_FILTER_SELECT_MASK GENMASK(10, 0)
  72. #define AD4130_FILTER_SELECT_MIN 1
  73. #define AD4130_OFFSET_X_REG(x) (0x29 + (x))
  74. #define AD4130_GAIN_X_REG(x) (0x31 + (x))
  75. #define AD4130_MISC_REG 0x39
  76. #define AD4130_FIFO_CONTROL_REG 0x3a
  77. #define AD4130_FIFO_CONTROL_HEADER_MASK BIT(18)
  78. #define AD4130_FIFO_CONTROL_MODE_MASK GENMASK(17, 16)
  79. #define AD4130_FIFO_CONTROL_WM_INT_EN_MASK BIT(9)
  80. #define AD4130_FIFO_CONTROL_WM_MASK GENMASK(7, 0)
  81. #define AD4130_WATERMARK_256 0
  82. #define AD4130_FIFO_STATUS_REG 0x3b
  83. #define AD4130_FIFO_THRESHOLD_REG 0x3c
  84. #define AD4130_FIFO_DATA_REG 0x3d
  85. #define AD4130_FIFO_SIZE 256
  86. #define AD4130_FIFO_MAX_SAMPLE_SIZE 3
  87. #define AD4130_MAX_ANALOG_PINS 16
  88. #define AD4130_MAX_CHANNELS 16
  89. #define AD4130_MAX_DIFF_INPUTS 30
  90. #define AD4130_MAX_GPIOS 4
  91. #define AD4130_MAX_ODR 2400
  92. #define AD4130_MAX_PGA 8
  93. #define AD4130_MAX_SETUPS 8
  94. #define AD4130_AIN2_P1 0x2
  95. #define AD4130_AIN3_P2 0x3
  96. #define AD4130_RESET_BUF_SIZE 8
  97. #define AD4130_RESET_SLEEP_US (160 * MICRO / AD4130_MCLK_FREQ_76_8KHZ)
  98. #define AD4130_INVALID_SLOT -1
  99. static const unsigned int ad4130_reg_size[] = {
  100. [AD4130_STATUS_REG] = 1,
  101. [AD4130_ADC_CONTROL_REG] = 2,
  102. [AD4130_DATA_REG] = 3,
  103. [AD4130_IO_CONTROL_REG] = 2,
  104. [AD4130_VBIAS_REG] = 2,
  105. [AD4130_ID_REG] = 1,
  106. [AD4130_ERROR_REG] = 2,
  107. [AD4130_ERROR_EN_REG] = 2,
  108. [AD4130_MCLK_COUNT_REG] = 1,
  109. [AD4130_CHANNEL_X_REG(0) ... AD4130_CHANNEL_X_REG(AD4130_MAX_CHANNELS - 1)] = 3,
  110. [AD4130_CONFIG_X_REG(0) ... AD4130_CONFIG_X_REG(AD4130_MAX_SETUPS - 1)] = 2,
  111. [AD4130_FILTER_X_REG(0) ... AD4130_FILTER_X_REG(AD4130_MAX_SETUPS - 1)] = 3,
  112. [AD4130_OFFSET_X_REG(0) ... AD4130_OFFSET_X_REG(AD4130_MAX_SETUPS - 1)] = 3,
  113. [AD4130_GAIN_X_REG(0) ... AD4130_GAIN_X_REG(AD4130_MAX_SETUPS - 1)] = 3,
  114. [AD4130_MISC_REG] = 2,
  115. [AD4130_FIFO_CONTROL_REG] = 3,
  116. [AD4130_FIFO_STATUS_REG] = 1,
  117. [AD4130_FIFO_THRESHOLD_REG] = 3,
  118. [AD4130_FIFO_DATA_REG] = 3,
  119. };
  120. enum ad4130_int_ref_val {
  121. AD4130_INT_REF_VAL_2_5V,
  122. AD4130_INT_REF_VAL_1_25V,
  123. };
  124. enum ad4130_mclk_sel {
  125. AD4130_MCLK_76_8KHZ,
  126. AD4130_MCLK_76_8KHZ_OUT,
  127. AD4130_MCLK_76_8KHZ_EXT,
  128. AD4130_MCLK_153_6KHZ_EXT,
  129. };
  130. enum ad4130_int_pin_sel {
  131. AD4130_INT_PIN_INT,
  132. AD4130_INT_PIN_CLK,
  133. AD4130_INT_PIN_P2,
  134. AD4130_INT_PIN_DOUT,
  135. };
  136. enum ad4130_iout {
  137. AD4130_IOUT_OFF,
  138. AD4130_IOUT_10000NA,
  139. AD4130_IOUT_20000NA,
  140. AD4130_IOUT_50000NA,
  141. AD4130_IOUT_100000NA,
  142. AD4130_IOUT_150000NA,
  143. AD4130_IOUT_200000NA,
  144. AD4130_IOUT_100NA,
  145. AD4130_IOUT_MAX
  146. };
  147. enum ad4130_burnout {
  148. AD4130_BURNOUT_OFF,
  149. AD4130_BURNOUT_500NA,
  150. AD4130_BURNOUT_2000NA,
  151. AD4130_BURNOUT_4000NA,
  152. AD4130_BURNOUT_MAX
  153. };
  154. enum ad4130_ref_sel {
  155. AD4130_REF_REFIN1,
  156. AD4130_REF_REFIN2,
  157. AD4130_REF_REFOUT_AVSS,
  158. AD4130_REF_AVDD_AVSS,
  159. AD4130_REF_SEL_MAX
  160. };
  161. enum ad4130_fifo_mode {
  162. AD4130_FIFO_MODE_DISABLED = 0b00,
  163. AD4130_FIFO_MODE_WM = 0b01,
  164. };
  165. enum ad4130_mode {
  166. AD4130_MODE_CONTINUOUS = 0b0000,
  167. AD4130_MODE_IDLE = 0b0100,
  168. };
  169. enum ad4130_filter_type {
  170. AD4130_FILTER_SINC4,
  171. AD4130_FILTER_SINC4_SINC1,
  172. AD4130_FILTER_SINC3,
  173. AD4130_FILTER_SINC3_REJ60,
  174. AD4130_FILTER_SINC3_SINC1,
  175. AD4130_FILTER_SINC3_PF1,
  176. AD4130_FILTER_SINC3_PF2,
  177. AD4130_FILTER_SINC3_PF3,
  178. AD4130_FILTER_SINC3_PF4,
  179. };
  180. enum ad4130_pin_function {
  181. AD4130_PIN_FN_NONE,
  182. AD4130_PIN_FN_SPECIAL = BIT(0),
  183. AD4130_PIN_FN_DIFF = BIT(1),
  184. AD4130_PIN_FN_EXCITATION = BIT(2),
  185. AD4130_PIN_FN_VBIAS = BIT(3),
  186. };
  187. /*
  188. * If you make adaptations in this struct, you most likely also have to adapt
  189. * ad4130_setup_info_eq(), too.
  190. */
  191. struct ad4130_setup_info {
  192. unsigned int iout0_val;
  193. unsigned int iout1_val;
  194. unsigned int burnout;
  195. unsigned int pga;
  196. unsigned int fs;
  197. u32 ref_sel;
  198. enum ad4130_filter_type filter_type;
  199. bool ref_bufp;
  200. bool ref_bufm;
  201. };
  202. struct ad4130_slot_info {
  203. struct ad4130_setup_info setup;
  204. unsigned int enabled_channels;
  205. unsigned int channels;
  206. };
  207. struct ad4130_chan_info {
  208. struct ad4130_setup_info setup;
  209. u32 iout0;
  210. u32 iout1;
  211. int slot;
  212. bool enabled;
  213. bool initialized;
  214. };
  215. struct ad4130_filter_config {
  216. enum ad4130_filter_type filter_type;
  217. unsigned int odr_div;
  218. unsigned int fs_max;
  219. enum iio_available_type samp_freq_avail_type;
  220. int samp_freq_avail_len;
  221. int samp_freq_avail[3][2];
  222. };
  223. struct ad4130_state {
  224. struct regmap *regmap;
  225. struct spi_device *spi;
  226. struct clk *mclk;
  227. struct regulator_bulk_data regulators[4];
  228. u32 irq_trigger;
  229. u32 inv_irq_trigger;
  230. /*
  231. * Synchronize access to members the of driver state, and ensure
  232. * atomicity of consecutive regmap operations.
  233. */
  234. struct mutex lock;
  235. struct completion completion;
  236. struct iio_chan_spec chans[AD4130_MAX_CHANNELS];
  237. struct ad4130_chan_info chans_info[AD4130_MAX_CHANNELS];
  238. struct ad4130_slot_info slots_info[AD4130_MAX_SETUPS];
  239. enum ad4130_pin_function pins_fn[AD4130_MAX_ANALOG_PINS];
  240. u32 vbias_pins[AD4130_MAX_ANALOG_PINS];
  241. u32 num_vbias_pins;
  242. int scale_tbls[AD4130_REF_SEL_MAX][AD4130_MAX_PGA][2];
  243. struct gpio_chip gc;
  244. struct clk_hw int_clk_hw;
  245. u32 int_pin_sel;
  246. u32 int_ref_uv;
  247. u32 mclk_sel;
  248. bool int_ref_en;
  249. bool bipolar;
  250. unsigned int num_enabled_channels;
  251. unsigned int effective_watermark;
  252. unsigned int watermark;
  253. struct spi_message fifo_msg;
  254. struct spi_transfer fifo_xfer[2];
  255. /*
  256. * DMA (thus cache coherency maintenance) requires any transfer
  257. * buffers to live in their own cache lines. As the use of these
  258. * buffers is synchronous, all of the buffers used for DMA in this
  259. * driver may share a cache line.
  260. */
  261. u8 reset_buf[AD4130_RESET_BUF_SIZE] __aligned(IIO_DMA_MINALIGN);
  262. u8 reg_write_tx_buf[4];
  263. u8 reg_read_tx_buf[1];
  264. u8 reg_read_rx_buf[3];
  265. u8 fifo_tx_buf[2];
  266. u8 fifo_rx_buf[AD4130_FIFO_SIZE *
  267. AD4130_FIFO_MAX_SAMPLE_SIZE];
  268. };
  269. static const char * const ad4130_int_pin_names[] = {
  270. [AD4130_INT_PIN_INT] = "int",
  271. [AD4130_INT_PIN_CLK] = "clk",
  272. [AD4130_INT_PIN_P2] = "p2",
  273. [AD4130_INT_PIN_DOUT] = "dout",
  274. };
  275. static const unsigned int ad4130_iout_current_na_tbl[AD4130_IOUT_MAX] = {
  276. [AD4130_IOUT_OFF] = 0,
  277. [AD4130_IOUT_100NA] = 100,
  278. [AD4130_IOUT_10000NA] = 10000,
  279. [AD4130_IOUT_20000NA] = 20000,
  280. [AD4130_IOUT_50000NA] = 50000,
  281. [AD4130_IOUT_100000NA] = 100000,
  282. [AD4130_IOUT_150000NA] = 150000,
  283. [AD4130_IOUT_200000NA] = 200000,
  284. };
  285. static const unsigned int ad4130_burnout_current_na_tbl[AD4130_BURNOUT_MAX] = {
  286. [AD4130_BURNOUT_OFF] = 0,
  287. [AD4130_BURNOUT_500NA] = 500,
  288. [AD4130_BURNOUT_2000NA] = 2000,
  289. [AD4130_BURNOUT_4000NA] = 4000,
  290. };
  291. #define AD4130_VARIABLE_ODR_CONFIG(_filter_type, _odr_div, _fs_max) \
  292. { \
  293. .filter_type = (_filter_type), \
  294. .odr_div = (_odr_div), \
  295. .fs_max = (_fs_max), \
  296. .samp_freq_avail_type = IIO_AVAIL_RANGE, \
  297. .samp_freq_avail = { \
  298. { AD4130_MAX_ODR, (_odr_div) * (_fs_max) }, \
  299. { AD4130_MAX_ODR, (_odr_div) * (_fs_max) }, \
  300. { AD4130_MAX_ODR, (_odr_div) }, \
  301. }, \
  302. }
  303. #define AD4130_FIXED_ODR_CONFIG(_filter_type, _odr_div) \
  304. { \
  305. .filter_type = (_filter_type), \
  306. .odr_div = (_odr_div), \
  307. .fs_max = AD4130_FILTER_SELECT_MIN, \
  308. .samp_freq_avail_type = IIO_AVAIL_LIST, \
  309. .samp_freq_avail_len = 1, \
  310. .samp_freq_avail = { \
  311. { AD4130_MAX_ODR, (_odr_div) }, \
  312. }, \
  313. }
  314. static const struct ad4130_filter_config ad4130_filter_configs[] = {
  315. AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC4, 1, 10),
  316. AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC4_SINC1, 11, 10),
  317. AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC3, 1, 2047),
  318. AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC3_REJ60, 1, 2047),
  319. AD4130_VARIABLE_ODR_CONFIG(AD4130_FILTER_SINC3_SINC1, 10, 2047),
  320. AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF1, 92),
  321. AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF2, 100),
  322. AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF3, 124),
  323. AD4130_FIXED_ODR_CONFIG(AD4130_FILTER_SINC3_PF4, 148),
  324. };
  325. static const char * const ad4130_filter_types_str[] = {
  326. [AD4130_FILTER_SINC4] = "sinc4",
  327. [AD4130_FILTER_SINC4_SINC1] = "sinc4+sinc1",
  328. [AD4130_FILTER_SINC3] = "sinc3",
  329. [AD4130_FILTER_SINC3_REJ60] = "sinc3+rej60",
  330. [AD4130_FILTER_SINC3_SINC1] = "sinc3+sinc1",
  331. [AD4130_FILTER_SINC3_PF1] = "sinc3+pf1",
  332. [AD4130_FILTER_SINC3_PF2] = "sinc3+pf2",
  333. [AD4130_FILTER_SINC3_PF3] = "sinc3+pf3",
  334. [AD4130_FILTER_SINC3_PF4] = "sinc3+pf4",
  335. };
  336. static int ad4130_get_reg_size(struct ad4130_state *st, unsigned int reg,
  337. unsigned int *size)
  338. {
  339. if (reg >= ARRAY_SIZE(ad4130_reg_size))
  340. return -EINVAL;
  341. *size = ad4130_reg_size[reg];
  342. return 0;
  343. }
  344. static unsigned int ad4130_data_reg_size(struct ad4130_state *st)
  345. {
  346. unsigned int data_reg_size;
  347. int ret;
  348. ret = ad4130_get_reg_size(st, AD4130_DATA_REG, &data_reg_size);
  349. if (ret)
  350. return 0;
  351. return data_reg_size;
  352. }
  353. static unsigned int ad4130_resolution(struct ad4130_state *st)
  354. {
  355. return ad4130_data_reg_size(st) * BITS_PER_BYTE;
  356. }
  357. static int ad4130_reg_write(void *context, unsigned int reg, unsigned int val)
  358. {
  359. struct ad4130_state *st = context;
  360. unsigned int size;
  361. int ret;
  362. ret = ad4130_get_reg_size(st, reg, &size);
  363. if (ret)
  364. return ret;
  365. st->reg_write_tx_buf[0] = reg;
  366. switch (size) {
  367. case 3:
  368. put_unaligned_be24(val, &st->reg_write_tx_buf[1]);
  369. break;
  370. case 2:
  371. put_unaligned_be16(val, &st->reg_write_tx_buf[1]);
  372. break;
  373. case 1:
  374. st->reg_write_tx_buf[1] = val;
  375. break;
  376. default:
  377. return -EINVAL;
  378. }
  379. return spi_write(st->spi, st->reg_write_tx_buf, size + 1);
  380. }
  381. static int ad4130_reg_read(void *context, unsigned int reg, unsigned int *val)
  382. {
  383. struct ad4130_state *st = context;
  384. struct spi_transfer t[] = {
  385. {
  386. .tx_buf = st->reg_read_tx_buf,
  387. .len = sizeof(st->reg_read_tx_buf),
  388. },
  389. {
  390. .rx_buf = st->reg_read_rx_buf,
  391. },
  392. };
  393. unsigned int size;
  394. int ret;
  395. ret = ad4130_get_reg_size(st, reg, &size);
  396. if (ret)
  397. return ret;
  398. st->reg_read_tx_buf[0] = AD4130_COMMS_READ_MASK | reg;
  399. t[1].len = size;
  400. ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
  401. if (ret)
  402. return ret;
  403. switch (size) {
  404. case 3:
  405. *val = get_unaligned_be24(st->reg_read_rx_buf);
  406. break;
  407. case 2:
  408. *val = get_unaligned_be16(st->reg_read_rx_buf);
  409. break;
  410. case 1:
  411. *val = st->reg_read_rx_buf[0];
  412. break;
  413. default:
  414. return -EINVAL;
  415. }
  416. return 0;
  417. }
  418. static const struct regmap_config ad4130_regmap_config = {
  419. .reg_read = ad4130_reg_read,
  420. .reg_write = ad4130_reg_write,
  421. };
  422. static int ad4130_gpio_init_valid_mask(struct gpio_chip *gc,
  423. unsigned long *valid_mask,
  424. unsigned int ngpios)
  425. {
  426. struct ad4130_state *st = gpiochip_get_data(gc);
  427. unsigned int i;
  428. /*
  429. * Output-only GPIO functionality is available on pins AIN2 through
  430. * AIN5. If these pins are used for anything else, do not expose them.
  431. */
  432. for (i = 0; i < ngpios; i++) {
  433. unsigned int pin = i + AD4130_AIN2_P1;
  434. bool valid = st->pins_fn[pin] == AD4130_PIN_FN_NONE;
  435. __assign_bit(i, valid_mask, valid);
  436. }
  437. return 0;
  438. }
  439. static int ad4130_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  440. {
  441. return GPIO_LINE_DIRECTION_OUT;
  442. }
  443. static int ad4130_gpio_set(struct gpio_chip *gc, unsigned int offset,
  444. int value)
  445. {
  446. struct ad4130_state *st = gpiochip_get_data(gc);
  447. unsigned int mask = FIELD_PREP(AD4130_IO_CONTROL_GPIO_DATA_MASK,
  448. BIT(offset));
  449. return regmap_update_bits(st->regmap, AD4130_IO_CONTROL_REG, mask,
  450. value ? mask : 0);
  451. }
  452. static int ad4130_set_mode(struct ad4130_state *st, enum ad4130_mode mode)
  453. {
  454. return regmap_update_bits(st->regmap, AD4130_ADC_CONTROL_REG,
  455. AD4130_ADC_CONTROL_MODE_MASK,
  456. FIELD_PREP(AD4130_ADC_CONTROL_MODE_MASK, mode));
  457. }
  458. static int ad4130_set_watermark_interrupt_en(struct ad4130_state *st, bool en)
  459. {
  460. return regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
  461. AD4130_FIFO_CONTROL_WM_INT_EN_MASK,
  462. FIELD_PREP(AD4130_FIFO_CONTROL_WM_INT_EN_MASK, en));
  463. }
  464. static unsigned int ad4130_watermark_reg_val(unsigned int val)
  465. {
  466. if (val == AD4130_FIFO_SIZE)
  467. val = AD4130_WATERMARK_256;
  468. return val;
  469. }
  470. static int ad4130_set_fifo_mode(struct ad4130_state *st,
  471. enum ad4130_fifo_mode mode)
  472. {
  473. return regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
  474. AD4130_FIFO_CONTROL_MODE_MASK,
  475. FIELD_PREP(AD4130_FIFO_CONTROL_MODE_MASK, mode));
  476. }
  477. static void ad4130_push_fifo_data(struct iio_dev *indio_dev)
  478. {
  479. struct ad4130_state *st = iio_priv(indio_dev);
  480. unsigned int data_reg_size = ad4130_data_reg_size(st);
  481. unsigned int transfer_len = st->effective_watermark * data_reg_size;
  482. unsigned int set_size = st->num_enabled_channels * data_reg_size;
  483. unsigned int i;
  484. int ret;
  485. st->fifo_tx_buf[1] = ad4130_watermark_reg_val(st->effective_watermark);
  486. st->fifo_xfer[1].len = transfer_len;
  487. ret = spi_sync(st->spi, &st->fifo_msg);
  488. if (ret)
  489. return;
  490. for (i = 0; i < transfer_len; i += set_size)
  491. iio_push_to_buffers(indio_dev, &st->fifo_rx_buf[i]);
  492. }
  493. static irqreturn_t ad4130_irq_handler(int irq, void *private)
  494. {
  495. struct iio_dev *indio_dev = private;
  496. struct ad4130_state *st = iio_priv(indio_dev);
  497. if (iio_buffer_enabled(indio_dev))
  498. ad4130_push_fifo_data(indio_dev);
  499. else
  500. complete(&st->completion);
  501. return IRQ_HANDLED;
  502. }
  503. static bool ad4130_setup_info_eq(struct ad4130_setup_info *a,
  504. struct ad4130_setup_info *b)
  505. {
  506. /*
  507. * This is just to make sure that the comparison is adapted after
  508. * struct ad4130_setup_info was changed.
  509. */
  510. static_assert(sizeof(*a) ==
  511. sizeof(struct {
  512. unsigned int iout0_val;
  513. unsigned int iout1_val;
  514. unsigned int burnout;
  515. unsigned int pga;
  516. unsigned int fs;
  517. u32 ref_sel;
  518. enum ad4130_filter_type filter_type;
  519. bool ref_bufp;
  520. bool ref_bufm;
  521. }));
  522. if (a->iout0_val != b->iout0_val ||
  523. a->iout1_val != b->iout1_val ||
  524. a->burnout != b->burnout ||
  525. a->pga != b->pga ||
  526. a->fs != b->fs ||
  527. a->ref_sel != b->ref_sel ||
  528. a->filter_type != b->filter_type ||
  529. a->ref_bufp != b->ref_bufp ||
  530. a->ref_bufm != b->ref_bufm)
  531. return false;
  532. return true;
  533. }
  534. static int ad4130_find_slot(struct ad4130_state *st,
  535. struct ad4130_setup_info *target_setup_info,
  536. unsigned int *slot, bool *overwrite)
  537. {
  538. unsigned int i;
  539. *slot = AD4130_INVALID_SLOT;
  540. *overwrite = false;
  541. for (i = 0; i < AD4130_MAX_SETUPS; i++) {
  542. struct ad4130_slot_info *slot_info = &st->slots_info[i];
  543. /* Immediately accept a matching setup info. */
  544. if (ad4130_setup_info_eq(target_setup_info, &slot_info->setup)) {
  545. *slot = i;
  546. return 0;
  547. }
  548. /* Ignore all setups which are used by enabled channels. */
  549. if (slot_info->enabled_channels)
  550. continue;
  551. /* Find the least used slot. */
  552. if (*slot == AD4130_INVALID_SLOT ||
  553. slot_info->channels < st->slots_info[*slot].channels)
  554. *slot = i;
  555. }
  556. if (*slot == AD4130_INVALID_SLOT)
  557. return -EINVAL;
  558. *overwrite = true;
  559. return 0;
  560. }
  561. static void ad4130_unlink_channel(struct ad4130_state *st, unsigned int channel)
  562. {
  563. struct ad4130_chan_info *chan_info = &st->chans_info[channel];
  564. struct ad4130_slot_info *slot_info = &st->slots_info[chan_info->slot];
  565. chan_info->slot = AD4130_INVALID_SLOT;
  566. slot_info->channels--;
  567. }
  568. static int ad4130_unlink_slot(struct ad4130_state *st, unsigned int slot)
  569. {
  570. unsigned int i;
  571. for (i = 0; i < AD4130_MAX_CHANNELS; i++) {
  572. struct ad4130_chan_info *chan_info = &st->chans_info[i];
  573. if (!chan_info->initialized || chan_info->slot != slot)
  574. continue;
  575. ad4130_unlink_channel(st, i);
  576. }
  577. return 0;
  578. }
  579. static int ad4130_link_channel_slot(struct ad4130_state *st,
  580. unsigned int channel, unsigned int slot)
  581. {
  582. struct ad4130_slot_info *slot_info = &st->slots_info[slot];
  583. struct ad4130_chan_info *chan_info = &st->chans_info[channel];
  584. int ret;
  585. ret = regmap_update_bits(st->regmap, AD4130_CHANNEL_X_REG(channel),
  586. AD4130_CHANNEL_SETUP_MASK,
  587. FIELD_PREP(AD4130_CHANNEL_SETUP_MASK, slot));
  588. if (ret)
  589. return ret;
  590. chan_info->slot = slot;
  591. slot_info->channels++;
  592. return 0;
  593. }
  594. static int ad4130_write_slot_setup(struct ad4130_state *st,
  595. unsigned int slot,
  596. struct ad4130_setup_info *setup_info)
  597. {
  598. unsigned int val;
  599. int ret;
  600. val = FIELD_PREP(AD4130_CONFIG_IOUT1_VAL_MASK, setup_info->iout0_val) |
  601. FIELD_PREP(AD4130_CONFIG_IOUT1_VAL_MASK, setup_info->iout1_val) |
  602. FIELD_PREP(AD4130_CONFIG_BURNOUT_MASK, setup_info->burnout) |
  603. FIELD_PREP(AD4130_CONFIG_REF_BUFP_MASK, setup_info->ref_bufp) |
  604. FIELD_PREP(AD4130_CONFIG_REF_BUFM_MASK, setup_info->ref_bufm) |
  605. FIELD_PREP(AD4130_CONFIG_REF_SEL_MASK, setup_info->ref_sel) |
  606. FIELD_PREP(AD4130_CONFIG_PGA_MASK, setup_info->pga);
  607. ret = regmap_write(st->regmap, AD4130_CONFIG_X_REG(slot), val);
  608. if (ret)
  609. return ret;
  610. val = FIELD_PREP(AD4130_FILTER_MODE_MASK, setup_info->filter_type) |
  611. FIELD_PREP(AD4130_FILTER_SELECT_MASK, setup_info->fs);
  612. ret = regmap_write(st->regmap, AD4130_FILTER_X_REG(slot), val);
  613. if (ret)
  614. return ret;
  615. memcpy(&st->slots_info[slot].setup, setup_info, sizeof(*setup_info));
  616. return 0;
  617. }
  618. static int ad4130_write_channel_setup(struct ad4130_state *st,
  619. unsigned int channel, bool on_enable)
  620. {
  621. struct ad4130_chan_info *chan_info = &st->chans_info[channel];
  622. struct ad4130_setup_info *setup_info = &chan_info->setup;
  623. bool overwrite;
  624. int slot;
  625. int ret;
  626. /*
  627. * The following cases need to be handled.
  628. *
  629. * 1. Enabled and linked channel with setup changes:
  630. * - Find a slot. If not possible, return error.
  631. * - Unlink channel from current slot.
  632. * - If the slot has channels linked to it, unlink all channels, and
  633. * write the new setup to it.
  634. * - Link channel to new slot.
  635. *
  636. * 2. Soon to be enabled and unlinked channel:
  637. * - Find a slot. If not possible, return error.
  638. * - If the slot has channels linked to it, unlink all channels, and
  639. * write the new setup to it.
  640. * - Link channel to the slot.
  641. *
  642. * 3. Disabled and linked channel with setup changes:
  643. * - Unlink channel from current slot.
  644. *
  645. * 4. Soon to be enabled and linked channel:
  646. * 5. Disabled and unlinked channel with setup changes:
  647. * - Do nothing.
  648. */
  649. /* Case 4 */
  650. if (on_enable && chan_info->slot != AD4130_INVALID_SLOT)
  651. return 0;
  652. if (!on_enable && !chan_info->enabled) {
  653. if (chan_info->slot != AD4130_INVALID_SLOT)
  654. /* Case 3 */
  655. ad4130_unlink_channel(st, channel);
  656. /* Cases 3 & 5 */
  657. return 0;
  658. }
  659. /* Cases 1 & 2 */
  660. ret = ad4130_find_slot(st, setup_info, &slot, &overwrite);
  661. if (ret)
  662. return ret;
  663. if (chan_info->slot != AD4130_INVALID_SLOT)
  664. /* Case 1 */
  665. ad4130_unlink_channel(st, channel);
  666. if (overwrite) {
  667. ret = ad4130_unlink_slot(st, slot);
  668. if (ret)
  669. return ret;
  670. ret = ad4130_write_slot_setup(st, slot, setup_info);
  671. if (ret)
  672. return ret;
  673. }
  674. return ad4130_link_channel_slot(st, channel, slot);
  675. }
  676. static int ad4130_set_channel_enable(struct ad4130_state *st,
  677. unsigned int channel, bool status)
  678. {
  679. struct ad4130_chan_info *chan_info = &st->chans_info[channel];
  680. struct ad4130_slot_info *slot_info;
  681. int ret;
  682. if (chan_info->enabled == status)
  683. return 0;
  684. if (status) {
  685. ret = ad4130_write_channel_setup(st, channel, true);
  686. if (ret)
  687. return ret;
  688. }
  689. slot_info = &st->slots_info[chan_info->slot];
  690. ret = regmap_update_bits(st->regmap, AD4130_CHANNEL_X_REG(channel),
  691. AD4130_CHANNEL_EN_MASK,
  692. FIELD_PREP(AD4130_CHANNEL_EN_MASK, status));
  693. if (ret)
  694. return ret;
  695. slot_info->enabled_channels += status ? 1 : -1;
  696. chan_info->enabled = status;
  697. return 0;
  698. }
  699. /*
  700. * Table 58. FILTER_MODE_n bits and Filter Types of the datasheet describes
  701. * the relation between filter mode, ODR and FS.
  702. *
  703. * Notice that the max ODR of each filter mode is not necessarily the
  704. * absolute max ODR supported by the chip.
  705. *
  706. * The ODR divider is not explicitly specified, but it can be deduced based
  707. * on the ODR range of each filter mode.
  708. *
  709. * For example, for Sinc4+Sinc1, max ODR is 218.18. That means that the
  710. * absolute max ODR is divided by 11 to achieve the max ODR of this filter
  711. * mode.
  712. *
  713. * The formulas for converting between ODR and FS for a specific filter
  714. * mode can be deduced from the same table.
  715. *
  716. * Notice that FS = 1 actually means max ODR, and that ODR decreases by
  717. * (maximum ODR / maximum FS) for each increment of FS.
  718. *
  719. * odr = MAX_ODR / odr_div * (1 - (fs - 1) / fs_max) <=>
  720. * odr = MAX_ODR * (1 - (fs - 1) / fs_max) / odr_div <=>
  721. * odr = MAX_ODR * (1 - (fs - 1) / fs_max) / odr_div <=>
  722. * odr = MAX_ODR * (fs_max - fs + 1) / (fs_max * odr_div)
  723. * (used in ad4130_fs_to_freq)
  724. *
  725. * For the opposite formula, FS can be extracted from the last one.
  726. *
  727. * MAX_ODR * (fs_max - fs + 1) = fs_max * odr_div * odr <=>
  728. * fs_max - fs + 1 = fs_max * odr_div * odr / MAX_ODR <=>
  729. * fs = 1 + fs_max - fs_max * odr_div * odr / MAX_ODR
  730. * (used in ad4130_fs_to_freq)
  731. */
  732. static void ad4130_freq_to_fs(enum ad4130_filter_type filter_type,
  733. int val, int val2, unsigned int *fs)
  734. {
  735. const struct ad4130_filter_config *filter_config =
  736. &ad4130_filter_configs[filter_type];
  737. u64 dividend, divisor;
  738. int temp;
  739. dividend = filter_config->fs_max * filter_config->odr_div *
  740. ((u64)val * NANO + val2);
  741. divisor = (u64)AD4130_MAX_ODR * NANO;
  742. temp = AD4130_FILTER_SELECT_MIN + filter_config->fs_max -
  743. DIV64_U64_ROUND_CLOSEST(dividend, divisor);
  744. if (temp < AD4130_FILTER_SELECT_MIN)
  745. temp = AD4130_FILTER_SELECT_MIN;
  746. else if (temp > filter_config->fs_max)
  747. temp = filter_config->fs_max;
  748. *fs = temp;
  749. }
  750. static void ad4130_fs_to_freq(enum ad4130_filter_type filter_type,
  751. unsigned int fs, int *val, int *val2)
  752. {
  753. const struct ad4130_filter_config *filter_config =
  754. &ad4130_filter_configs[filter_type];
  755. unsigned int dividend, divisor;
  756. u64 temp;
  757. dividend = (filter_config->fs_max - fs + AD4130_FILTER_SELECT_MIN) *
  758. AD4130_MAX_ODR;
  759. divisor = filter_config->fs_max * filter_config->odr_div;
  760. temp = div_u64((u64)dividend * NANO, divisor);
  761. *val = div_u64_rem(temp, NANO, val2);
  762. }
  763. static int ad4130_set_filter_type(struct iio_dev *indio_dev,
  764. const struct iio_chan_spec *chan,
  765. unsigned int val)
  766. {
  767. struct ad4130_state *st = iio_priv(indio_dev);
  768. unsigned int channel = chan->scan_index;
  769. struct ad4130_chan_info *chan_info = &st->chans_info[channel];
  770. struct ad4130_setup_info *setup_info = &chan_info->setup;
  771. enum ad4130_filter_type old_filter_type;
  772. int freq_val, freq_val2;
  773. unsigned int old_fs;
  774. int ret = 0;
  775. guard(mutex)(&st->lock);
  776. if (setup_info->filter_type == val)
  777. return 0;
  778. old_fs = setup_info->fs;
  779. old_filter_type = setup_info->filter_type;
  780. /*
  781. * When switching between filter modes, try to match the ODR as
  782. * close as possible. To do this, convert the current FS into ODR
  783. * using the old filter mode, then convert it back into FS using
  784. * the new filter mode.
  785. */
  786. ad4130_fs_to_freq(setup_info->filter_type, setup_info->fs,
  787. &freq_val, &freq_val2);
  788. ad4130_freq_to_fs(val, freq_val, freq_val2, &setup_info->fs);
  789. setup_info->filter_type = val;
  790. ret = ad4130_write_channel_setup(st, channel, false);
  791. if (ret) {
  792. setup_info->fs = old_fs;
  793. setup_info->filter_type = old_filter_type;
  794. return ret;
  795. }
  796. return 0;
  797. }
  798. static int ad4130_get_filter_type(struct iio_dev *indio_dev,
  799. const struct iio_chan_spec *chan)
  800. {
  801. struct ad4130_state *st = iio_priv(indio_dev);
  802. unsigned int channel = chan->scan_index;
  803. struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup;
  804. enum ad4130_filter_type filter_type;
  805. guard(mutex)(&st->lock);
  806. filter_type = setup_info->filter_type;
  807. return filter_type;
  808. }
  809. static const struct iio_enum ad4130_filter_type_enum = {
  810. .items = ad4130_filter_types_str,
  811. .num_items = ARRAY_SIZE(ad4130_filter_types_str),
  812. .set = ad4130_set_filter_type,
  813. .get = ad4130_get_filter_type,
  814. };
  815. static const struct iio_chan_spec_ext_info ad4130_ext_info[] = {
  816. /*
  817. * `filter_type` is the standardized IIO ABI for digital filtering.
  818. * `filter_mode` is just kept for backwards compatibility.
  819. */
  820. IIO_ENUM("filter_mode", IIO_SEPARATE, &ad4130_filter_type_enum),
  821. IIO_ENUM_AVAILABLE("filter_mode", IIO_SHARED_BY_TYPE,
  822. &ad4130_filter_type_enum),
  823. IIO_ENUM("filter_type", IIO_SEPARATE, &ad4130_filter_type_enum),
  824. IIO_ENUM_AVAILABLE("filter_type", IIO_SHARED_BY_TYPE,
  825. &ad4130_filter_type_enum),
  826. { }
  827. };
  828. static const struct iio_chan_spec ad4130_channel_template = {
  829. .type = IIO_VOLTAGE,
  830. .indexed = 1,
  831. .differential = 1,
  832. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  833. BIT(IIO_CHAN_INFO_SCALE) |
  834. BIT(IIO_CHAN_INFO_OFFSET) |
  835. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  836. .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE) |
  837. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  838. .ext_info = ad4130_ext_info,
  839. .scan_type = {
  840. .sign = 'u',
  841. .endianness = IIO_BE,
  842. },
  843. };
  844. static int ad4130_set_channel_pga(struct ad4130_state *st, unsigned int channel,
  845. int val, int val2)
  846. {
  847. struct ad4130_chan_info *chan_info = &st->chans_info[channel];
  848. struct ad4130_setup_info *setup_info = &chan_info->setup;
  849. unsigned int pga, old_pga;
  850. int ret;
  851. for (pga = 0; pga < AD4130_MAX_PGA; pga++)
  852. if (val == st->scale_tbls[setup_info->ref_sel][pga][0] &&
  853. val2 == st->scale_tbls[setup_info->ref_sel][pga][1])
  854. break;
  855. if (pga == AD4130_MAX_PGA)
  856. return -EINVAL;
  857. guard(mutex)(&st->lock);
  858. if (pga == setup_info->pga)
  859. return 0;
  860. old_pga = setup_info->pga;
  861. setup_info->pga = pga;
  862. ret = ad4130_write_channel_setup(st, channel, false);
  863. if (ret) {
  864. setup_info->pga = old_pga;
  865. return ret;
  866. }
  867. return 0;
  868. }
  869. static int ad4130_set_channel_freq(struct ad4130_state *st,
  870. unsigned int channel, int val, int val2)
  871. {
  872. struct ad4130_chan_info *chan_info = &st->chans_info[channel];
  873. struct ad4130_setup_info *setup_info = &chan_info->setup;
  874. unsigned int fs, old_fs;
  875. int ret;
  876. guard(mutex)(&st->lock);
  877. old_fs = setup_info->fs;
  878. ad4130_freq_to_fs(setup_info->filter_type, val, val2, &fs);
  879. if (fs == setup_info->fs)
  880. return 0;
  881. setup_info->fs = fs;
  882. ret = ad4130_write_channel_setup(st, channel, false);
  883. if (ret) {
  884. setup_info->fs = old_fs;
  885. return ret;
  886. }
  887. return 0;
  888. }
  889. static int _ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel,
  890. int *val)
  891. {
  892. struct ad4130_state *st = iio_priv(indio_dev);
  893. int ret;
  894. ret = ad4130_set_channel_enable(st, channel, true);
  895. if (ret)
  896. return ret;
  897. reinit_completion(&st->completion);
  898. ret = ad4130_set_mode(st, AD4130_MODE_CONTINUOUS);
  899. if (ret)
  900. return ret;
  901. ret = wait_for_completion_timeout(&st->completion,
  902. msecs_to_jiffies(1000));
  903. if (!ret)
  904. return -ETIMEDOUT;
  905. ret = ad4130_set_mode(st, AD4130_MODE_IDLE);
  906. if (ret)
  907. return ret;
  908. ret = regmap_read(st->regmap, AD4130_DATA_REG, val);
  909. if (ret)
  910. return ret;
  911. ret = ad4130_set_channel_enable(st, channel, false);
  912. if (ret)
  913. return ret;
  914. return IIO_VAL_INT;
  915. }
  916. static int ad4130_read_sample(struct iio_dev *indio_dev, unsigned int channel,
  917. int *val)
  918. {
  919. struct ad4130_state *st = iio_priv(indio_dev);
  920. guard(mutex)(&st->lock);
  921. return _ad4130_read_sample(indio_dev, channel, val);
  922. }
  923. static int ad4130_read_raw(struct iio_dev *indio_dev,
  924. struct iio_chan_spec const *chan,
  925. int *val, int *val2, long info)
  926. {
  927. struct ad4130_state *st = iio_priv(indio_dev);
  928. unsigned int channel = chan->scan_index;
  929. struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup;
  930. int ret;
  931. switch (info) {
  932. case IIO_CHAN_INFO_RAW:
  933. if (!iio_device_claim_direct(indio_dev))
  934. return -EBUSY;
  935. ret = ad4130_read_sample(indio_dev, channel, val);
  936. iio_device_release_direct(indio_dev);
  937. return ret;
  938. case IIO_CHAN_INFO_SCALE: {
  939. guard(mutex)(&st->lock);
  940. *val = st->scale_tbls[setup_info->ref_sel][setup_info->pga][0];
  941. *val2 = st->scale_tbls[setup_info->ref_sel][setup_info->pga][1];
  942. return IIO_VAL_INT_PLUS_NANO;
  943. }
  944. case IIO_CHAN_INFO_OFFSET:
  945. *val = st->bipolar ? -BIT(chan->scan_type.realbits - 1) : 0;
  946. return IIO_VAL_INT;
  947. case IIO_CHAN_INFO_SAMP_FREQ: {
  948. guard(mutex)(&st->lock);
  949. ad4130_fs_to_freq(setup_info->filter_type, setup_info->fs,
  950. val, val2);
  951. return IIO_VAL_INT_PLUS_NANO;
  952. }
  953. default:
  954. return -EINVAL;
  955. }
  956. }
  957. static int ad4130_read_avail(struct iio_dev *indio_dev,
  958. struct iio_chan_spec const *chan,
  959. const int **vals, int *type, int *length,
  960. long info)
  961. {
  962. struct ad4130_state *st = iio_priv(indio_dev);
  963. unsigned int channel = chan->scan_index;
  964. struct ad4130_setup_info *setup_info = &st->chans_info[channel].setup;
  965. const struct ad4130_filter_config *filter_config;
  966. switch (info) {
  967. case IIO_CHAN_INFO_SCALE:
  968. *vals = (int *)st->scale_tbls[setup_info->ref_sel];
  969. *length = ARRAY_SIZE(st->scale_tbls[setup_info->ref_sel]) * 2;
  970. *type = IIO_VAL_INT_PLUS_NANO;
  971. return IIO_AVAIL_LIST;
  972. case IIO_CHAN_INFO_SAMP_FREQ:
  973. scoped_guard(mutex, &st->lock) {
  974. filter_config = &ad4130_filter_configs[setup_info->filter_type];
  975. }
  976. *vals = (int *)filter_config->samp_freq_avail;
  977. *length = filter_config->samp_freq_avail_len * 2;
  978. *type = IIO_VAL_FRACTIONAL;
  979. return filter_config->samp_freq_avail_type;
  980. default:
  981. return -EINVAL;
  982. }
  983. }
  984. static int ad4130_write_raw_get_fmt(struct iio_dev *indio_dev,
  985. struct iio_chan_spec const *chan,
  986. long info)
  987. {
  988. switch (info) {
  989. case IIO_CHAN_INFO_SCALE:
  990. case IIO_CHAN_INFO_SAMP_FREQ:
  991. return IIO_VAL_INT_PLUS_NANO;
  992. default:
  993. return -EINVAL;
  994. }
  995. }
  996. static int ad4130_write_raw(struct iio_dev *indio_dev,
  997. struct iio_chan_spec const *chan,
  998. int val, int val2, long info)
  999. {
  1000. struct ad4130_state *st = iio_priv(indio_dev);
  1001. unsigned int channel = chan->scan_index;
  1002. switch (info) {
  1003. case IIO_CHAN_INFO_SCALE:
  1004. return ad4130_set_channel_pga(st, channel, val, val2);
  1005. case IIO_CHAN_INFO_SAMP_FREQ:
  1006. return ad4130_set_channel_freq(st, channel, val, val2);
  1007. default:
  1008. return -EINVAL;
  1009. }
  1010. }
  1011. static int ad4130_reg_access(struct iio_dev *indio_dev, unsigned int reg,
  1012. unsigned int writeval, unsigned int *readval)
  1013. {
  1014. struct ad4130_state *st = iio_priv(indio_dev);
  1015. if (readval)
  1016. return regmap_read(st->regmap, reg, readval);
  1017. return regmap_write(st->regmap, reg, writeval);
  1018. }
  1019. static int ad4130_update_scan_mode(struct iio_dev *indio_dev,
  1020. const unsigned long *scan_mask)
  1021. {
  1022. struct ad4130_state *st = iio_priv(indio_dev);
  1023. unsigned int channel;
  1024. unsigned int val = 0;
  1025. int ret;
  1026. guard(mutex)(&st->lock);
  1027. for_each_set_bit(channel, scan_mask, indio_dev->num_channels) {
  1028. ret = ad4130_set_channel_enable(st, channel, true);
  1029. if (ret)
  1030. return ret;
  1031. val++;
  1032. }
  1033. st->num_enabled_channels = val;
  1034. return 0;
  1035. }
  1036. static int ad4130_set_fifo_watermark(struct iio_dev *indio_dev, unsigned int val)
  1037. {
  1038. struct ad4130_state *st = iio_priv(indio_dev);
  1039. unsigned int eff;
  1040. int ret;
  1041. if (val > AD4130_FIFO_SIZE)
  1042. return -EINVAL;
  1043. eff = val * st->num_enabled_channels;
  1044. if (eff > AD4130_FIFO_SIZE)
  1045. /*
  1046. * Always set watermark to a multiple of the number of
  1047. * enabled channels to avoid making the FIFO unaligned.
  1048. */
  1049. eff = rounddown(AD4130_FIFO_SIZE, st->num_enabled_channels);
  1050. guard(mutex)(&st->lock);
  1051. ret = regmap_update_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
  1052. AD4130_FIFO_CONTROL_WM_MASK,
  1053. FIELD_PREP(AD4130_FIFO_CONTROL_WM_MASK,
  1054. ad4130_watermark_reg_val(eff)));
  1055. if (ret)
  1056. return ret;
  1057. st->effective_watermark = eff;
  1058. st->watermark = val;
  1059. return 0;
  1060. }
  1061. static const struct iio_info ad4130_info = {
  1062. .read_raw = ad4130_read_raw,
  1063. .read_avail = ad4130_read_avail,
  1064. .write_raw_get_fmt = ad4130_write_raw_get_fmt,
  1065. .write_raw = ad4130_write_raw,
  1066. .update_scan_mode = ad4130_update_scan_mode,
  1067. .hwfifo_set_watermark = ad4130_set_fifo_watermark,
  1068. .debugfs_reg_access = ad4130_reg_access,
  1069. };
  1070. static int ad4130_buffer_postenable(struct iio_dev *indio_dev)
  1071. {
  1072. struct ad4130_state *st = iio_priv(indio_dev);
  1073. int ret;
  1074. guard(mutex)(&st->lock);
  1075. ret = ad4130_set_watermark_interrupt_en(st, true);
  1076. if (ret)
  1077. return ret;
  1078. ret = irq_set_irq_type(st->spi->irq, st->inv_irq_trigger);
  1079. if (ret)
  1080. return ret;
  1081. ret = ad4130_set_fifo_mode(st, AD4130_FIFO_MODE_WM);
  1082. if (ret)
  1083. return ret;
  1084. return ad4130_set_mode(st, AD4130_MODE_CONTINUOUS);
  1085. }
  1086. static int ad4130_buffer_predisable(struct iio_dev *indio_dev)
  1087. {
  1088. struct ad4130_state *st = iio_priv(indio_dev);
  1089. unsigned int i;
  1090. int ret;
  1091. guard(mutex)(&st->lock);
  1092. ret = ad4130_set_mode(st, AD4130_MODE_IDLE);
  1093. if (ret)
  1094. return ret;
  1095. ret = irq_set_irq_type(st->spi->irq, st->irq_trigger);
  1096. if (ret)
  1097. return ret;
  1098. ret = ad4130_set_fifo_mode(st, AD4130_FIFO_MODE_DISABLED);
  1099. if (ret)
  1100. return ret;
  1101. ret = ad4130_set_watermark_interrupt_en(st, false);
  1102. if (ret)
  1103. return ret;
  1104. /*
  1105. * update_scan_mode() is not called in the disable path, disable all
  1106. * channels here.
  1107. */
  1108. for (i = 0; i < indio_dev->num_channels; i++) {
  1109. ret = ad4130_set_channel_enable(st, i, false);
  1110. if (ret)
  1111. return ret;
  1112. }
  1113. return 0;
  1114. }
  1115. static const struct iio_buffer_setup_ops ad4130_buffer_ops = {
  1116. .postenable = ad4130_buffer_postenable,
  1117. .predisable = ad4130_buffer_predisable,
  1118. };
  1119. static ssize_t hwfifo_watermark_show(struct device *dev,
  1120. struct device_attribute *attr, char *buf)
  1121. {
  1122. struct ad4130_state *st = iio_priv(dev_to_iio_dev(dev));
  1123. unsigned int val;
  1124. guard(mutex)(&st->lock);
  1125. val = st->watermark;
  1126. return sysfs_emit(buf, "%d\n", val);
  1127. }
  1128. static ssize_t hwfifo_enabled_show(struct device *dev,
  1129. struct device_attribute *attr, char *buf)
  1130. {
  1131. struct ad4130_state *st = iio_priv(dev_to_iio_dev(dev));
  1132. unsigned int val;
  1133. int ret;
  1134. ret = regmap_read(st->regmap, AD4130_FIFO_CONTROL_REG, &val);
  1135. if (ret)
  1136. return ret;
  1137. val = FIELD_GET(AD4130_FIFO_CONTROL_MODE_MASK, val);
  1138. return sysfs_emit(buf, "%d\n", val != AD4130_FIFO_MODE_DISABLED);
  1139. }
  1140. static ssize_t hwfifo_watermark_min_show(struct device *dev,
  1141. struct device_attribute *attr,
  1142. char *buf)
  1143. {
  1144. return sysfs_emit(buf, "%s\n", "1");
  1145. }
  1146. static ssize_t hwfifo_watermark_max_show(struct device *dev,
  1147. struct device_attribute *attr,
  1148. char *buf)
  1149. {
  1150. return sysfs_emit(buf, "%s\n", __stringify(AD4130_FIFO_SIZE));
  1151. }
  1152. static IIO_DEVICE_ATTR_RO(hwfifo_watermark_min, 0);
  1153. static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0);
  1154. static IIO_DEVICE_ATTR_RO(hwfifo_watermark, 0);
  1155. static IIO_DEVICE_ATTR_RO(hwfifo_enabled, 0);
  1156. static const struct iio_dev_attr *ad4130_fifo_attributes[] = {
  1157. &iio_dev_attr_hwfifo_watermark_min,
  1158. &iio_dev_attr_hwfifo_watermark_max,
  1159. &iio_dev_attr_hwfifo_watermark,
  1160. &iio_dev_attr_hwfifo_enabled,
  1161. NULL
  1162. };
  1163. static int _ad4130_find_table_index(const unsigned int *tbl, size_t len,
  1164. unsigned int val)
  1165. {
  1166. unsigned int i;
  1167. for (i = 0; i < len; i++)
  1168. if (tbl[i] == val)
  1169. return i;
  1170. return -EINVAL;
  1171. }
  1172. #define ad4130_find_table_index(table, val) \
  1173. _ad4130_find_table_index(table, ARRAY_SIZE(table), val)
  1174. static int ad4130_get_ref_voltage(struct ad4130_state *st,
  1175. enum ad4130_ref_sel ref_sel)
  1176. {
  1177. switch (ref_sel) {
  1178. case AD4130_REF_REFIN1:
  1179. return regulator_get_voltage(st->regulators[2].consumer);
  1180. case AD4130_REF_REFIN2:
  1181. return regulator_get_voltage(st->regulators[3].consumer);
  1182. case AD4130_REF_AVDD_AVSS:
  1183. return regulator_get_voltage(st->regulators[0].consumer);
  1184. case AD4130_REF_REFOUT_AVSS:
  1185. return st->int_ref_uv;
  1186. default:
  1187. return -EINVAL;
  1188. }
  1189. }
  1190. static int ad4130_parse_fw_setup(struct ad4130_state *st,
  1191. struct fwnode_handle *child,
  1192. struct ad4130_setup_info *setup_info)
  1193. {
  1194. struct device *dev = &st->spi->dev;
  1195. u32 tmp;
  1196. int ret;
  1197. tmp = 0;
  1198. fwnode_property_read_u32(child, "adi,excitation-current-0-nanoamp", &tmp);
  1199. ret = ad4130_find_table_index(ad4130_iout_current_na_tbl, tmp);
  1200. if (ret < 0)
  1201. return dev_err_probe(dev, ret,
  1202. "Invalid excitation current %unA\n", tmp);
  1203. setup_info->iout0_val = ret;
  1204. tmp = 0;
  1205. fwnode_property_read_u32(child, "adi,excitation-current-1-nanoamp", &tmp);
  1206. ret = ad4130_find_table_index(ad4130_iout_current_na_tbl, tmp);
  1207. if (ret < 0)
  1208. return dev_err_probe(dev, ret,
  1209. "Invalid excitation current %unA\n", tmp);
  1210. setup_info->iout1_val = ret;
  1211. tmp = 0;
  1212. fwnode_property_read_u32(child, "adi,burnout-current-nanoamp", &tmp);
  1213. ret = ad4130_find_table_index(ad4130_burnout_current_na_tbl, tmp);
  1214. if (ret < 0)
  1215. return dev_err_probe(dev, ret,
  1216. "Invalid burnout current %unA\n", tmp);
  1217. setup_info->burnout = ret;
  1218. setup_info->ref_bufp = fwnode_property_read_bool(child, "adi,buffered-positive");
  1219. setup_info->ref_bufm = fwnode_property_read_bool(child, "adi,buffered-negative");
  1220. setup_info->ref_sel = AD4130_REF_REFIN1;
  1221. fwnode_property_read_u32(child, "adi,reference-select",
  1222. &setup_info->ref_sel);
  1223. if (setup_info->ref_sel >= AD4130_REF_SEL_MAX)
  1224. return dev_err_probe(dev, -EINVAL,
  1225. "Invalid reference selected %u\n",
  1226. setup_info->ref_sel);
  1227. if (setup_info->ref_sel == AD4130_REF_REFOUT_AVSS)
  1228. st->int_ref_en = true;
  1229. ret = ad4130_get_ref_voltage(st, setup_info->ref_sel);
  1230. if (ret < 0)
  1231. return dev_err_probe(dev, ret, "Cannot use reference %u\n",
  1232. setup_info->ref_sel);
  1233. return 0;
  1234. }
  1235. static int ad4130_validate_diff_channel(struct ad4130_state *st, u32 pin)
  1236. {
  1237. struct device *dev = &st->spi->dev;
  1238. if (pin >= AD4130_MAX_DIFF_INPUTS)
  1239. return dev_err_probe(dev, -EINVAL,
  1240. "Invalid differential channel %u\n", pin);
  1241. if (pin >= AD4130_MAX_ANALOG_PINS)
  1242. return 0;
  1243. if (st->pins_fn[pin] == AD4130_PIN_FN_SPECIAL)
  1244. return dev_err_probe(dev, -EINVAL,
  1245. "Pin %u already used with fn %u\n", pin,
  1246. st->pins_fn[pin]);
  1247. st->pins_fn[pin] |= AD4130_PIN_FN_DIFF;
  1248. return 0;
  1249. }
  1250. static int ad4130_validate_diff_channels(struct ad4130_state *st,
  1251. u32 *pins, unsigned int len)
  1252. {
  1253. unsigned int i;
  1254. int ret;
  1255. for (i = 0; i < len; i++) {
  1256. ret = ad4130_validate_diff_channel(st, pins[i]);
  1257. if (ret)
  1258. return ret;
  1259. }
  1260. return 0;
  1261. }
  1262. static int ad4130_validate_excitation_pin(struct ad4130_state *st, u32 pin)
  1263. {
  1264. struct device *dev = &st->spi->dev;
  1265. if (pin >= AD4130_MAX_ANALOG_PINS)
  1266. return dev_err_probe(dev, -EINVAL,
  1267. "Invalid excitation pin %u\n", pin);
  1268. if (st->pins_fn[pin] == AD4130_PIN_FN_SPECIAL)
  1269. return dev_err_probe(dev, -EINVAL,
  1270. "Pin %u already used with fn %u\n", pin,
  1271. st->pins_fn[pin]);
  1272. st->pins_fn[pin] |= AD4130_PIN_FN_EXCITATION;
  1273. return 0;
  1274. }
  1275. static int ad4130_validate_vbias_pin(struct ad4130_state *st, u32 pin)
  1276. {
  1277. struct device *dev = &st->spi->dev;
  1278. if (pin >= AD4130_MAX_ANALOG_PINS)
  1279. return dev_err_probe(dev, -EINVAL, "Invalid vbias pin %u\n",
  1280. pin);
  1281. if (st->pins_fn[pin] == AD4130_PIN_FN_SPECIAL)
  1282. return dev_err_probe(dev, -EINVAL,
  1283. "Pin %u already used with fn %u\n", pin,
  1284. st->pins_fn[pin]);
  1285. st->pins_fn[pin] |= AD4130_PIN_FN_VBIAS;
  1286. return 0;
  1287. }
  1288. static int ad4130_validate_vbias_pins(struct ad4130_state *st,
  1289. u32 *pins, unsigned int len)
  1290. {
  1291. unsigned int i;
  1292. int ret;
  1293. for (i = 0; i < st->num_vbias_pins; i++) {
  1294. ret = ad4130_validate_vbias_pin(st, pins[i]);
  1295. if (ret)
  1296. return ret;
  1297. }
  1298. return 0;
  1299. }
  1300. static int ad4130_parse_fw_channel(struct iio_dev *indio_dev,
  1301. struct fwnode_handle *child)
  1302. {
  1303. struct ad4130_state *st = iio_priv(indio_dev);
  1304. unsigned int resolution = ad4130_resolution(st);
  1305. unsigned int index = indio_dev->num_channels++;
  1306. struct device *dev = &st->spi->dev;
  1307. struct ad4130_chan_info *chan_info;
  1308. struct iio_chan_spec *chan;
  1309. u32 pins[2];
  1310. int ret;
  1311. if (index >= AD4130_MAX_CHANNELS)
  1312. return dev_err_probe(dev, -EINVAL, "Too many channels\n");
  1313. chan = &st->chans[index];
  1314. chan_info = &st->chans_info[index];
  1315. *chan = ad4130_channel_template;
  1316. chan->scan_type.realbits = resolution;
  1317. chan->scan_type.storagebits = resolution;
  1318. chan->scan_index = index;
  1319. chan_info->slot = AD4130_INVALID_SLOT;
  1320. chan_info->setup.fs = AD4130_FILTER_SELECT_MIN;
  1321. chan_info->initialized = true;
  1322. ret = fwnode_property_read_u32_array(child, "diff-channels", pins,
  1323. ARRAY_SIZE(pins));
  1324. if (ret)
  1325. return ret;
  1326. ret = ad4130_validate_diff_channels(st, pins, ARRAY_SIZE(pins));
  1327. if (ret)
  1328. return ret;
  1329. chan->channel = pins[0];
  1330. chan->channel2 = pins[1];
  1331. ret = ad4130_parse_fw_setup(st, child, &chan_info->setup);
  1332. if (ret)
  1333. return ret;
  1334. fwnode_property_read_u32(child, "adi,excitation-pin-0",
  1335. &chan_info->iout0);
  1336. if (chan_info->setup.iout0_val != AD4130_IOUT_OFF) {
  1337. ret = ad4130_validate_excitation_pin(st, chan_info->iout0);
  1338. if (ret)
  1339. return ret;
  1340. }
  1341. fwnode_property_read_u32(child, "adi,excitation-pin-1",
  1342. &chan_info->iout1);
  1343. if (chan_info->setup.iout1_val != AD4130_IOUT_OFF) {
  1344. ret = ad4130_validate_excitation_pin(st, chan_info->iout1);
  1345. if (ret)
  1346. return ret;
  1347. }
  1348. return 0;
  1349. }
  1350. static int ad4130_parse_fw_children(struct iio_dev *indio_dev)
  1351. {
  1352. struct ad4130_state *st = iio_priv(indio_dev);
  1353. struct device *dev = &st->spi->dev;
  1354. int ret;
  1355. indio_dev->channels = st->chans;
  1356. device_for_each_child_node_scoped(dev, child) {
  1357. ret = ad4130_parse_fw_channel(indio_dev, child);
  1358. if (ret)
  1359. return ret;
  1360. }
  1361. return 0;
  1362. }
  1363. static int ad4310_parse_fw(struct iio_dev *indio_dev)
  1364. {
  1365. struct ad4130_state *st = iio_priv(indio_dev);
  1366. struct device *dev = &st->spi->dev;
  1367. u32 ext_clk_freq = AD4130_MCLK_FREQ_76_8KHZ;
  1368. unsigned int i;
  1369. int avdd_uv;
  1370. int irq;
  1371. int ret;
  1372. st->mclk = devm_clk_get_optional(dev, "mclk");
  1373. if (IS_ERR(st->mclk))
  1374. return dev_err_probe(dev, PTR_ERR(st->mclk),
  1375. "Failed to get mclk\n");
  1376. st->int_pin_sel = AD4130_INT_PIN_INT;
  1377. for (i = 0; i < ARRAY_SIZE(ad4130_int_pin_names); i++) {
  1378. irq = fwnode_irq_get_byname(dev_fwnode(dev),
  1379. ad4130_int_pin_names[i]);
  1380. if (irq > 0) {
  1381. st->int_pin_sel = i;
  1382. break;
  1383. }
  1384. }
  1385. if (st->int_pin_sel == AD4130_INT_PIN_DOUT)
  1386. return dev_err_probe(dev, -EINVAL,
  1387. "Cannot use DOUT as interrupt pin\n");
  1388. if (st->int_pin_sel == AD4130_INT_PIN_P2)
  1389. st->pins_fn[AD4130_AIN3_P2] = AD4130_PIN_FN_SPECIAL;
  1390. device_property_read_u32(dev, "adi,ext-clk-freq-hz", &ext_clk_freq);
  1391. if (ext_clk_freq != AD4130_MCLK_FREQ_153_6KHZ &&
  1392. ext_clk_freq != AD4130_MCLK_FREQ_76_8KHZ)
  1393. return dev_err_probe(dev, -EINVAL,
  1394. "Invalid external clock frequency %u\n",
  1395. ext_clk_freq);
  1396. if (st->mclk && ext_clk_freq == AD4130_MCLK_FREQ_153_6KHZ)
  1397. st->mclk_sel = AD4130_MCLK_153_6KHZ_EXT;
  1398. else if (st->mclk)
  1399. st->mclk_sel = AD4130_MCLK_76_8KHZ_EXT;
  1400. else
  1401. st->mclk_sel = AD4130_MCLK_76_8KHZ;
  1402. if (st->int_pin_sel == AD4130_INT_PIN_CLK &&
  1403. st->mclk_sel != AD4130_MCLK_76_8KHZ)
  1404. return dev_err_probe(dev, -EINVAL,
  1405. "Invalid clock %u for interrupt pin %u\n",
  1406. st->mclk_sel, st->int_pin_sel);
  1407. st->int_ref_uv = AD4130_INT_REF_2_5V;
  1408. /*
  1409. * When the AVDD supply is set to below 2.5V the internal reference of
  1410. * 1.25V should be selected.
  1411. * See datasheet page 37, section ADC REFERENCE.
  1412. */
  1413. avdd_uv = regulator_get_voltage(st->regulators[0].consumer);
  1414. if (avdd_uv > 0 && avdd_uv < AD4130_INT_REF_2_5V)
  1415. st->int_ref_uv = AD4130_INT_REF_1_25V;
  1416. st->bipolar = device_property_read_bool(dev, "adi,bipolar");
  1417. ret = device_property_count_u32(dev, "adi,vbias-pins");
  1418. if (ret > 0) {
  1419. if (ret > AD4130_MAX_ANALOG_PINS)
  1420. return dev_err_probe(dev, -EINVAL,
  1421. "Too many vbias pins %u\n", ret);
  1422. st->num_vbias_pins = ret;
  1423. ret = device_property_read_u32_array(dev, "adi,vbias-pins",
  1424. st->vbias_pins,
  1425. st->num_vbias_pins);
  1426. if (ret)
  1427. return dev_err_probe(dev, ret,
  1428. "Failed to read vbias pins\n");
  1429. ret = ad4130_validate_vbias_pins(st, st->vbias_pins,
  1430. st->num_vbias_pins);
  1431. if (ret)
  1432. return ret;
  1433. }
  1434. ret = ad4130_parse_fw_children(indio_dev);
  1435. if (ret)
  1436. return ret;
  1437. return 0;
  1438. }
  1439. static void ad4130_fill_scale_tbls(struct ad4130_state *st)
  1440. {
  1441. unsigned int pow = ad4130_resolution(st) - st->bipolar;
  1442. unsigned int i, j;
  1443. for (i = 0; i < AD4130_REF_SEL_MAX; i++) {
  1444. int ret;
  1445. u64 nv;
  1446. ret = ad4130_get_ref_voltage(st, i);
  1447. if (ret < 0)
  1448. continue;
  1449. nv = (u64)ret * NANO;
  1450. for (j = 0; j < AD4130_MAX_PGA; j++)
  1451. st->scale_tbls[i][j][1] = div_u64(nv >> (pow + j), MILLI);
  1452. }
  1453. }
  1454. static void ad4130_clk_disable_unprepare(void *clk)
  1455. {
  1456. clk_disable_unprepare(clk);
  1457. }
  1458. static int ad4130_set_mclk_sel(struct ad4130_state *st,
  1459. enum ad4130_mclk_sel mclk_sel)
  1460. {
  1461. return regmap_update_bits(st->regmap, AD4130_ADC_CONTROL_REG,
  1462. AD4130_ADC_CONTROL_MCLK_SEL_MASK,
  1463. FIELD_PREP(AD4130_ADC_CONTROL_MCLK_SEL_MASK,
  1464. mclk_sel));
  1465. }
  1466. static unsigned long ad4130_int_clk_recalc_rate(struct clk_hw *hw,
  1467. unsigned long parent_rate)
  1468. {
  1469. return AD4130_MCLK_FREQ_76_8KHZ;
  1470. }
  1471. static int ad4130_int_clk_is_enabled(struct clk_hw *hw)
  1472. {
  1473. struct ad4130_state *st = container_of(hw, struct ad4130_state, int_clk_hw);
  1474. return st->mclk_sel == AD4130_MCLK_76_8KHZ_OUT;
  1475. }
  1476. static int ad4130_int_clk_prepare(struct clk_hw *hw)
  1477. {
  1478. struct ad4130_state *st = container_of(hw, struct ad4130_state, int_clk_hw);
  1479. int ret;
  1480. ret = ad4130_set_mclk_sel(st, AD4130_MCLK_76_8KHZ_OUT);
  1481. if (ret)
  1482. return ret;
  1483. st->mclk_sel = AD4130_MCLK_76_8KHZ_OUT;
  1484. return 0;
  1485. }
  1486. static void ad4130_int_clk_unprepare(struct clk_hw *hw)
  1487. {
  1488. struct ad4130_state *st = container_of(hw, struct ad4130_state, int_clk_hw);
  1489. int ret;
  1490. ret = ad4130_set_mclk_sel(st, AD4130_MCLK_76_8KHZ);
  1491. if (ret)
  1492. return;
  1493. st->mclk_sel = AD4130_MCLK_76_8KHZ;
  1494. }
  1495. static const struct clk_ops ad4130_int_clk_ops = {
  1496. .recalc_rate = ad4130_int_clk_recalc_rate,
  1497. .is_enabled = ad4130_int_clk_is_enabled,
  1498. .prepare = ad4130_int_clk_prepare,
  1499. .unprepare = ad4130_int_clk_unprepare,
  1500. };
  1501. static int ad4130_setup_int_clk(struct ad4130_state *st)
  1502. {
  1503. struct device *dev = &st->spi->dev;
  1504. struct device_node *of_node = dev_of_node(dev);
  1505. struct clk_init_data init = {};
  1506. const char *clk_name;
  1507. int ret;
  1508. if (st->int_pin_sel == AD4130_INT_PIN_CLK ||
  1509. st->mclk_sel != AD4130_MCLK_76_8KHZ)
  1510. return 0;
  1511. if (!of_node)
  1512. return 0;
  1513. clk_name = of_node->name;
  1514. of_property_read_string(of_node, "clock-output-names", &clk_name);
  1515. init.name = clk_name;
  1516. init.ops = &ad4130_int_clk_ops;
  1517. st->int_clk_hw.init = &init;
  1518. ret = devm_clk_hw_register(dev, &st->int_clk_hw);
  1519. if (ret)
  1520. return ret;
  1521. return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
  1522. &st->int_clk_hw);
  1523. }
  1524. static int ad4130_setup(struct iio_dev *indio_dev)
  1525. {
  1526. struct ad4130_state *st = iio_priv(indio_dev);
  1527. struct device *dev = &st->spi->dev;
  1528. unsigned int int_ref_val;
  1529. unsigned long rate = AD4130_MCLK_FREQ_76_8KHZ;
  1530. unsigned int val;
  1531. unsigned int i;
  1532. int ret;
  1533. if (st->mclk_sel == AD4130_MCLK_153_6KHZ_EXT)
  1534. rate = AD4130_MCLK_FREQ_153_6KHZ;
  1535. ret = clk_set_rate(st->mclk, rate);
  1536. if (ret)
  1537. return ret;
  1538. ret = clk_prepare_enable(st->mclk);
  1539. if (ret)
  1540. return ret;
  1541. ret = devm_add_action_or_reset(dev, ad4130_clk_disable_unprepare,
  1542. st->mclk);
  1543. if (ret)
  1544. return ret;
  1545. if (st->int_ref_uv == AD4130_INT_REF_2_5V)
  1546. int_ref_val = AD4130_INT_REF_VAL_2_5V;
  1547. else
  1548. int_ref_val = AD4130_INT_REF_VAL_1_25V;
  1549. /* Switch to SPI 4-wire mode. */
  1550. val = FIELD_PREP(AD4130_ADC_CONTROL_CSB_EN_MASK, 1);
  1551. val |= FIELD_PREP(AD4130_ADC_CONTROL_BIPOLAR_MASK, st->bipolar);
  1552. val |= FIELD_PREP(AD4130_ADC_CONTROL_INT_REF_EN_MASK, st->int_ref_en);
  1553. val |= FIELD_PREP(AD4130_ADC_CONTROL_MODE_MASK, AD4130_MODE_IDLE);
  1554. val |= FIELD_PREP(AD4130_ADC_CONTROL_MCLK_SEL_MASK, st->mclk_sel);
  1555. val |= FIELD_PREP(AD4130_ADC_CONTROL_INT_REF_VAL_MASK, int_ref_val);
  1556. ret = regmap_write(st->regmap, AD4130_ADC_CONTROL_REG, val);
  1557. if (ret)
  1558. return ret;
  1559. /*
  1560. * Configure unused GPIOs for output. If configured, the interrupt
  1561. * function of P2 takes priority over the GPIO out function.
  1562. */
  1563. val = 0;
  1564. for (i = 0; i < AD4130_MAX_GPIOS; i++)
  1565. if (st->pins_fn[i + AD4130_AIN2_P1] == AD4130_PIN_FN_NONE)
  1566. val |= FIELD_PREP(AD4130_IO_CONTROL_GPIO_CTRL_MASK, BIT(i));
  1567. val |= FIELD_PREP(AD4130_IO_CONTROL_INT_PIN_SEL_MASK, st->int_pin_sel);
  1568. ret = regmap_write(st->regmap, AD4130_IO_CONTROL_REG, val);
  1569. if (ret)
  1570. return ret;
  1571. val = 0;
  1572. for (i = 0; i < st->num_vbias_pins; i++)
  1573. val |= BIT(st->vbias_pins[i]);
  1574. ret = regmap_write(st->regmap, AD4130_VBIAS_REG, val);
  1575. if (ret)
  1576. return ret;
  1577. ret = regmap_clear_bits(st->regmap, AD4130_FIFO_CONTROL_REG,
  1578. AD4130_FIFO_CONTROL_HEADER_MASK);
  1579. if (ret)
  1580. return ret;
  1581. /* FIFO watermark interrupt starts out as enabled, disable it. */
  1582. ret = ad4130_set_watermark_interrupt_en(st, false);
  1583. if (ret)
  1584. return ret;
  1585. /* Setup channels. */
  1586. for (i = 0; i < indio_dev->num_channels; i++) {
  1587. struct ad4130_chan_info *chan_info = &st->chans_info[i];
  1588. struct iio_chan_spec *chan = &st->chans[i];
  1589. unsigned int val;
  1590. val = FIELD_PREP(AD4130_CHANNEL_AINP_MASK, chan->channel) |
  1591. FIELD_PREP(AD4130_CHANNEL_AINM_MASK, chan->channel2) |
  1592. FIELD_PREP(AD4130_CHANNEL_IOUT1_MASK, chan_info->iout0) |
  1593. FIELD_PREP(AD4130_CHANNEL_IOUT2_MASK, chan_info->iout1);
  1594. ret = regmap_write(st->regmap, AD4130_CHANNEL_X_REG(i), val);
  1595. if (ret)
  1596. return ret;
  1597. }
  1598. return 0;
  1599. }
  1600. static int ad4130_soft_reset(struct ad4130_state *st)
  1601. {
  1602. int ret;
  1603. ret = spi_write(st->spi, st->reset_buf, sizeof(st->reset_buf));
  1604. if (ret)
  1605. return ret;
  1606. fsleep(AD4130_RESET_SLEEP_US);
  1607. return 0;
  1608. }
  1609. static void ad4130_disable_regulators(void *data)
  1610. {
  1611. struct ad4130_state *st = data;
  1612. regulator_bulk_disable(ARRAY_SIZE(st->regulators), st->regulators);
  1613. }
  1614. static int ad4130_probe(struct spi_device *spi)
  1615. {
  1616. struct device *dev = &spi->dev;
  1617. struct iio_dev *indio_dev;
  1618. struct ad4130_state *st;
  1619. int ret;
  1620. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  1621. if (!indio_dev)
  1622. return -ENOMEM;
  1623. st = iio_priv(indio_dev);
  1624. memset(st->reset_buf, 0xff, sizeof(st->reset_buf));
  1625. init_completion(&st->completion);
  1626. mutex_init(&st->lock);
  1627. st->spi = spi;
  1628. /*
  1629. * Xfer: [ XFR1 ] [ XFR2 ]
  1630. * Master: 0x7D N ......................
  1631. * Slave: ...... DATA1 DATA2 ... DATAN
  1632. */
  1633. st->fifo_tx_buf[0] = AD4130_COMMS_READ_MASK | AD4130_FIFO_DATA_REG;
  1634. st->fifo_xfer[0].tx_buf = st->fifo_tx_buf;
  1635. st->fifo_xfer[0].len = sizeof(st->fifo_tx_buf);
  1636. st->fifo_xfer[1].rx_buf = st->fifo_rx_buf;
  1637. spi_message_init_with_transfers(&st->fifo_msg, st->fifo_xfer,
  1638. ARRAY_SIZE(st->fifo_xfer));
  1639. indio_dev->name = AD4130_NAME;
  1640. indio_dev->modes = INDIO_DIRECT_MODE;
  1641. indio_dev->info = &ad4130_info;
  1642. st->regmap = devm_regmap_init(dev, NULL, st, &ad4130_regmap_config);
  1643. if (IS_ERR(st->regmap))
  1644. return PTR_ERR(st->regmap);
  1645. st->regulators[0].supply = "avdd";
  1646. st->regulators[1].supply = "iovdd";
  1647. st->regulators[2].supply = "refin1";
  1648. st->regulators[3].supply = "refin2";
  1649. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(st->regulators),
  1650. st->regulators);
  1651. if (ret)
  1652. return dev_err_probe(dev, ret, "Failed to get regulators\n");
  1653. ret = regulator_bulk_enable(ARRAY_SIZE(st->regulators), st->regulators);
  1654. if (ret)
  1655. return dev_err_probe(dev, ret, "Failed to enable regulators\n");
  1656. ret = devm_add_action_or_reset(dev, ad4130_disable_regulators, st);
  1657. if (ret)
  1658. return ret;
  1659. ret = ad4130_soft_reset(st);
  1660. if (ret)
  1661. return ret;
  1662. ret = ad4310_parse_fw(indio_dev);
  1663. if (ret)
  1664. return ret;
  1665. ret = ad4130_setup(indio_dev);
  1666. if (ret)
  1667. return ret;
  1668. ret = ad4130_setup_int_clk(st);
  1669. if (ret)
  1670. return ret;
  1671. ad4130_fill_scale_tbls(st);
  1672. st->gc.owner = THIS_MODULE;
  1673. st->gc.label = AD4130_NAME;
  1674. st->gc.base = -1;
  1675. st->gc.ngpio = AD4130_MAX_GPIOS;
  1676. st->gc.parent = dev;
  1677. st->gc.can_sleep = true;
  1678. st->gc.init_valid_mask = ad4130_gpio_init_valid_mask;
  1679. st->gc.get_direction = ad4130_gpio_get_direction;
  1680. st->gc.set = ad4130_gpio_set;
  1681. ret = devm_gpiochip_add_data(dev, &st->gc, st);
  1682. if (ret)
  1683. return ret;
  1684. ret = devm_iio_kfifo_buffer_setup_ext(dev, indio_dev,
  1685. &ad4130_buffer_ops,
  1686. ad4130_fifo_attributes);
  1687. if (ret)
  1688. return ret;
  1689. ret = devm_request_threaded_irq(dev, spi->irq, NULL,
  1690. ad4130_irq_handler, IRQF_ONESHOT,
  1691. indio_dev->name, indio_dev);
  1692. if (ret)
  1693. return dev_err_probe(dev, ret, "Failed to request irq\n");
  1694. /*
  1695. * When the chip enters FIFO mode, IRQ polarity is inverted.
  1696. * When the chip exits FIFO mode, IRQ polarity returns to normal.
  1697. * See datasheet pages: 65, FIFO Watermark Interrupt section,
  1698. * and 71, Bit Descriptions for STATUS Register, RDYB.
  1699. * Cache the normal and inverted IRQ triggers to set them when
  1700. * entering and exiting FIFO mode.
  1701. */
  1702. st->irq_trigger = irq_get_trigger_type(spi->irq);
  1703. if (st->irq_trigger & IRQF_TRIGGER_RISING)
  1704. st->inv_irq_trigger = IRQF_TRIGGER_FALLING;
  1705. else if (st->irq_trigger & IRQF_TRIGGER_FALLING)
  1706. st->inv_irq_trigger = IRQF_TRIGGER_RISING;
  1707. else
  1708. return dev_err_probe(dev, -EINVAL, "Invalid irq flags: %u\n",
  1709. st->irq_trigger);
  1710. return devm_iio_device_register(dev, indio_dev);
  1711. }
  1712. static const struct of_device_id ad4130_of_match[] = {
  1713. {
  1714. .compatible = "adi,ad4130",
  1715. },
  1716. { }
  1717. };
  1718. MODULE_DEVICE_TABLE(of, ad4130_of_match);
  1719. static struct spi_driver ad4130_driver = {
  1720. .driver = {
  1721. .name = AD4130_NAME,
  1722. .of_match_table = ad4130_of_match,
  1723. },
  1724. .probe = ad4130_probe,
  1725. };
  1726. module_spi_driver(ad4130_driver);
  1727. MODULE_AUTHOR("Cosmin Tanislav <cosmin.tanislav@analog.com>");
  1728. MODULE_DESCRIPTION("Analog Devices AD4130 SPI driver");
  1729. MODULE_LICENSE("GPL");