ad4130.c 53 KB

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