ad7380.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Analog Devices AD738x Simultaneous Sampling SAR ADCs
  4. *
  5. * Copyright 2017 Analog Devices Inc.
  6. * Copyright 2024 BayLibre, SAS
  7. *
  8. * Datasheets of supported parts:
  9. * ad7380/1 : https://www.analog.com/media/en/technical-documentation/data-sheets/AD7380-7381.pdf
  10. * ad7383/4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7383-7384.pdf
  11. * ad7386/7/8 : https://www.analog.com/media/en/technical-documentation/data-sheets/AD7386-7387-7388.pdf
  12. * ad7380-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7380-4.pdf
  13. * ad7381-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7381-4.pdf
  14. * ad7383/4-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7383-4-ad7384-4.pdf
  15. * ad7386/7/8-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7386-4-7387-4-7388-4.pdf
  16. */
  17. #include <linux/align.h>
  18. #include <linux/bitfield.h>
  19. #include <linux/bitops.h>
  20. #include <linux/cleanup.h>
  21. #include <linux/device.h>
  22. #include <linux/err.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/regmap.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/slab.h>
  28. #include <linux/spi/spi.h>
  29. #include <linux/iio/buffer.h>
  30. #include <linux/iio/iio.h>
  31. #include <linux/iio/trigger_consumer.h>
  32. #include <linux/iio/triggered_buffer.h>
  33. #define MAX_NUM_CHANNELS 8
  34. /* 2.5V internal reference voltage */
  35. #define AD7380_INTERNAL_REF_MV 2500
  36. /* reading and writing registers is more reliable at lower than max speed */
  37. #define AD7380_REG_WR_SPEED_HZ 10000000
  38. #define AD7380_REG_WR BIT(15)
  39. #define AD7380_REG_REGADDR GENMASK(14, 12)
  40. #define AD7380_REG_DATA GENMASK(11, 0)
  41. #define AD7380_REG_ADDR_NOP 0x0
  42. #define AD7380_REG_ADDR_CONFIG1 0x1
  43. #define AD7380_REG_ADDR_CONFIG2 0x2
  44. #define AD7380_REG_ADDR_ALERT 0x3
  45. #define AD7380_REG_ADDR_ALERT_LOW_TH 0x4
  46. #define AD7380_REG_ADDR_ALERT_HIGH_TH 0x5
  47. #define AD7380_CONFIG1_CH BIT(11)
  48. #define AD7380_CONFIG1_SEQ BIT(10)
  49. #define AD7380_CONFIG1_OS_MODE BIT(9)
  50. #define AD7380_CONFIG1_OSR GENMASK(8, 6)
  51. #define AD7380_CONFIG1_CRC_W BIT(5)
  52. #define AD7380_CONFIG1_CRC_R BIT(4)
  53. #define AD7380_CONFIG1_ALERTEN BIT(3)
  54. #define AD7380_CONFIG1_RES BIT(2)
  55. #define AD7380_CONFIG1_REFSEL BIT(1)
  56. #define AD7380_CONFIG1_PMODE BIT(0)
  57. #define AD7380_CONFIG2_SDO2 GENMASK(9, 8)
  58. #define AD7380_CONFIG2_SDO BIT(8)
  59. #define AD7380_CONFIG2_RESET GENMASK(7, 0)
  60. #define AD7380_CONFIG2_RESET_SOFT 0x3C
  61. #define AD7380_CONFIG2_RESET_HARD 0xFF
  62. #define AD7380_ALERT_LOW_TH GENMASK(11, 0)
  63. #define AD7380_ALERT_HIGH_TH GENMASK(11, 0)
  64. #define T_CONVERT_NS 190 /* conversion time */
  65. #define T_CONVERT_0_NS 10 /* 1st conversion start time (oversampling) */
  66. #define T_CONVERT_X_NS 500 /* xth conversion start time (oversampling) */
  67. #define T_POWERUP_US 5000 /* Power up */
  68. struct ad7380_timing_specs {
  69. const unsigned int t_csh_ns; /* CS minimum high time */
  70. };
  71. struct ad7380_chip_info {
  72. const char *name;
  73. const struct iio_chan_spec *channels;
  74. unsigned int num_channels;
  75. unsigned int num_simult_channels;
  76. bool has_mux;
  77. const char * const *supplies;
  78. unsigned int num_supplies;
  79. bool external_ref_only;
  80. const char * const *vcm_supplies;
  81. unsigned int num_vcm_supplies;
  82. const unsigned long *available_scan_masks;
  83. const struct ad7380_timing_specs *timing_specs;
  84. };
  85. enum {
  86. AD7380_SCAN_TYPE_NORMAL,
  87. AD7380_SCAN_TYPE_RESOLUTION_BOOST,
  88. };
  89. /* Extended scan types for 12-bit unsigned chips. */
  90. static const struct iio_scan_type ad7380_scan_type_12_u[] = {
  91. [AD7380_SCAN_TYPE_NORMAL] = {
  92. .sign = 'u',
  93. .realbits = 12,
  94. .storagebits = 16,
  95. .endianness = IIO_CPU,
  96. },
  97. [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = {
  98. .sign = 'u',
  99. .realbits = 14,
  100. .storagebits = 16,
  101. .endianness = IIO_CPU,
  102. },
  103. };
  104. /* Extended scan types for 14-bit signed chips. */
  105. static const struct iio_scan_type ad7380_scan_type_14_s[] = {
  106. [AD7380_SCAN_TYPE_NORMAL] = {
  107. .sign = 's',
  108. .realbits = 14,
  109. .storagebits = 16,
  110. .endianness = IIO_CPU,
  111. },
  112. [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = {
  113. .sign = 's',
  114. .realbits = 16,
  115. .storagebits = 16,
  116. .endianness = IIO_CPU,
  117. },
  118. };
  119. /* Extended scan types for 14-bit unsigned chips. */
  120. static const struct iio_scan_type ad7380_scan_type_14_u[] = {
  121. [AD7380_SCAN_TYPE_NORMAL] = {
  122. .sign = 'u',
  123. .realbits = 14,
  124. .storagebits = 16,
  125. .endianness = IIO_CPU,
  126. },
  127. [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = {
  128. .sign = 'u',
  129. .realbits = 16,
  130. .storagebits = 16,
  131. .endianness = IIO_CPU,
  132. },
  133. };
  134. /* Extended scan types for 16-bit signed_chips. */
  135. static const struct iio_scan_type ad7380_scan_type_16_s[] = {
  136. [AD7380_SCAN_TYPE_NORMAL] = {
  137. .sign = 's',
  138. .realbits = 16,
  139. .storagebits = 16,
  140. .endianness = IIO_CPU,
  141. },
  142. [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = {
  143. .sign = 's',
  144. .realbits = 18,
  145. .storagebits = 32,
  146. .endianness = IIO_CPU,
  147. },
  148. };
  149. /* Extended scan types for 16-bit unsigned chips. */
  150. static const struct iio_scan_type ad7380_scan_type_16_u[] = {
  151. [AD7380_SCAN_TYPE_NORMAL] = {
  152. .sign = 'u',
  153. .realbits = 16,
  154. .storagebits = 16,
  155. .endianness = IIO_CPU,
  156. },
  157. [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = {
  158. .sign = 'u',
  159. .realbits = 18,
  160. .storagebits = 32,
  161. .endianness = IIO_CPU,
  162. },
  163. };
  164. #define AD7380_CHANNEL(index, bits, diff, sign) { \
  165. .type = IIO_VOLTAGE, \
  166. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  167. ((diff) ? 0 : BIT(IIO_CHAN_INFO_OFFSET)), \
  168. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  169. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  170. .info_mask_shared_by_type_available = \
  171. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
  172. .indexed = 1, \
  173. .differential = (diff), \
  174. .channel = (diff) ? (2 * (index)) : (index), \
  175. .channel2 = (diff) ? (2 * (index) + 1) : 0, \
  176. .scan_index = (index), \
  177. .has_ext_scan_type = 1, \
  178. .ext_scan_type = ad7380_scan_type_##bits##_##sign, \
  179. .num_ext_scan_type = ARRAY_SIZE(ad7380_scan_type_##bits##_##sign), \
  180. }
  181. #define DEFINE_AD7380_2_CHANNEL(name, bits, diff, sign) \
  182. static const struct iio_chan_spec name[] = { \
  183. AD7380_CHANNEL(0, bits, diff, sign), \
  184. AD7380_CHANNEL(1, bits, diff, sign), \
  185. IIO_CHAN_SOFT_TIMESTAMP(2), \
  186. }
  187. #define DEFINE_AD7380_4_CHANNEL(name, bits, diff, sign) \
  188. static const struct iio_chan_spec name[] = { \
  189. AD7380_CHANNEL(0, bits, diff, sign), \
  190. AD7380_CHANNEL(1, bits, diff, sign), \
  191. AD7380_CHANNEL(2, bits, diff, sign), \
  192. AD7380_CHANNEL(3, bits, diff, sign), \
  193. IIO_CHAN_SOFT_TIMESTAMP(4), \
  194. }
  195. #define DEFINE_AD7380_8_CHANNEL(name, bits, diff, sign) \
  196. static const struct iio_chan_spec name[] = { \
  197. AD7380_CHANNEL(0, bits, diff, sign), \
  198. AD7380_CHANNEL(1, bits, diff, sign), \
  199. AD7380_CHANNEL(2, bits, diff, sign), \
  200. AD7380_CHANNEL(3, bits, diff, sign), \
  201. AD7380_CHANNEL(4, bits, diff, sign), \
  202. AD7380_CHANNEL(5, bits, diff, sign), \
  203. AD7380_CHANNEL(6, bits, diff, sign), \
  204. AD7380_CHANNEL(7, bits, diff, sign), \
  205. IIO_CHAN_SOFT_TIMESTAMP(8), \
  206. }
  207. /* fully differential */
  208. DEFINE_AD7380_2_CHANNEL(ad7380_channels, 16, 1, s);
  209. DEFINE_AD7380_2_CHANNEL(ad7381_channels, 14, 1, s);
  210. DEFINE_AD7380_4_CHANNEL(ad7380_4_channels, 16, 1, s);
  211. DEFINE_AD7380_4_CHANNEL(ad7381_4_channels, 14, 1, s);
  212. /* pseudo differential */
  213. DEFINE_AD7380_2_CHANNEL(ad7383_channels, 16, 0, s);
  214. DEFINE_AD7380_2_CHANNEL(ad7384_channels, 14, 0, s);
  215. DEFINE_AD7380_4_CHANNEL(ad7383_4_channels, 16, 0, s);
  216. DEFINE_AD7380_4_CHANNEL(ad7384_4_channels, 14, 0, s);
  217. /* Single ended */
  218. DEFINE_AD7380_4_CHANNEL(ad7386_channels, 16, 0, u);
  219. DEFINE_AD7380_4_CHANNEL(ad7387_channels, 14, 0, u);
  220. DEFINE_AD7380_4_CHANNEL(ad7388_channels, 12, 0, u);
  221. DEFINE_AD7380_8_CHANNEL(ad7386_4_channels, 16, 0, u);
  222. DEFINE_AD7380_8_CHANNEL(ad7387_4_channels, 14, 0, u);
  223. DEFINE_AD7380_8_CHANNEL(ad7388_4_channels, 12, 0, u);
  224. static const char * const ad7380_supplies[] = {
  225. "vcc", "vlogic",
  226. };
  227. static const char * const ad7380_2_channel_vcm_supplies[] = {
  228. "aina", "ainb",
  229. };
  230. static const char * const ad7380_4_channel_vcm_supplies[] = {
  231. "aina", "ainb", "ainc", "aind",
  232. };
  233. /* Since this is simultaneous sampling, we don't allow individual channels. */
  234. static const unsigned long ad7380_2_channel_scan_masks[] = {
  235. GENMASK(1, 0),
  236. 0
  237. };
  238. static const unsigned long ad7380_4_channel_scan_masks[] = {
  239. GENMASK(3, 0),
  240. 0
  241. };
  242. /*
  243. * Single ended parts have a 2:1 multiplexer in front of each ADC.
  244. *
  245. * From an IIO point of view, all inputs are exported, i.e ad7386/7/8
  246. * export 4 channels and ad7386-4/7-4/8-4 export 8 channels.
  247. *
  248. * Inputs AinX0 of multiplexers correspond to the first half of IIO channels
  249. * (i.e 0-1 or 0-3) and inputs AinX1 correspond to second half (i.e 2-3 or
  250. * 4-7). Example for AD7386/7/8 (2 channels parts):
  251. *
  252. * IIO | AD7386/7/8
  253. * | +----------------------------
  254. * | | _____ ______
  255. * | | | | | |
  256. * voltage0 | AinA0 --|--->| | | |
  257. * | | | mux |----->| ADCA |---
  258. * voltage2 | AinA1 --|--->| | | |
  259. * | | |_____| |_____ |
  260. * | | _____ ______
  261. * | | | | | |
  262. * voltage1 | AinB0 --|--->| | | |
  263. * | | | mux |----->| ADCB |---
  264. * voltage3 | AinB1 --|--->| | | |
  265. * | | |_____| |______|
  266. * | |
  267. * | +----------------------------
  268. *
  269. * Since this is simultaneous sampling for AinX0 OR AinX1 we have two separate
  270. * scan masks.
  271. * When sequencer mode is enabled, chip automatically cycles through
  272. * AinX0 and AinX1 channels. From an IIO point of view, we ca enable all
  273. * channels, at the cost of an extra read, thus dividing the maximum rate by
  274. * two.
  275. */
  276. enum {
  277. AD7380_SCAN_MASK_CH_0,
  278. AD7380_SCAN_MASK_CH_1,
  279. AD7380_SCAN_MASK_SEQ,
  280. };
  281. static const unsigned long ad7380_2x2_channel_scan_masks[] = {
  282. [AD7380_SCAN_MASK_CH_0] = GENMASK(1, 0),
  283. [AD7380_SCAN_MASK_CH_1] = GENMASK(3, 2),
  284. [AD7380_SCAN_MASK_SEQ] = GENMASK(3, 0),
  285. 0
  286. };
  287. static const unsigned long ad7380_2x4_channel_scan_masks[] = {
  288. [AD7380_SCAN_MASK_CH_0] = GENMASK(3, 0),
  289. [AD7380_SCAN_MASK_CH_1] = GENMASK(7, 4),
  290. [AD7380_SCAN_MASK_SEQ] = GENMASK(7, 0),
  291. 0
  292. };
  293. static const struct ad7380_timing_specs ad7380_timing = {
  294. .t_csh_ns = 10,
  295. };
  296. static const struct ad7380_timing_specs ad7380_4_timing = {
  297. .t_csh_ns = 20,
  298. };
  299. /*
  300. * Available oversampling ratios. The indices correspond with the bit value
  301. * expected by the chip. The available ratios depend on the averaging mode,
  302. * only normal averaging is supported for now.
  303. */
  304. static const int ad7380_oversampling_ratios[] = {
  305. 1, 2, 4, 8, 16, 32,
  306. };
  307. static const struct ad7380_chip_info ad7380_chip_info = {
  308. .name = "ad7380",
  309. .channels = ad7380_channels,
  310. .num_channels = ARRAY_SIZE(ad7380_channels),
  311. .num_simult_channels = 2,
  312. .supplies = ad7380_supplies,
  313. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  314. .available_scan_masks = ad7380_2_channel_scan_masks,
  315. .timing_specs = &ad7380_timing,
  316. };
  317. static const struct ad7380_chip_info ad7381_chip_info = {
  318. .name = "ad7381",
  319. .channels = ad7381_channels,
  320. .num_channels = ARRAY_SIZE(ad7381_channels),
  321. .num_simult_channels = 2,
  322. .supplies = ad7380_supplies,
  323. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  324. .available_scan_masks = ad7380_2_channel_scan_masks,
  325. .timing_specs = &ad7380_timing,
  326. };
  327. static const struct ad7380_chip_info ad7383_chip_info = {
  328. .name = "ad7383",
  329. .channels = ad7383_channels,
  330. .num_channels = ARRAY_SIZE(ad7383_channels),
  331. .num_simult_channels = 2,
  332. .supplies = ad7380_supplies,
  333. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  334. .vcm_supplies = ad7380_2_channel_vcm_supplies,
  335. .num_vcm_supplies = ARRAY_SIZE(ad7380_2_channel_vcm_supplies),
  336. .available_scan_masks = ad7380_2_channel_scan_masks,
  337. .timing_specs = &ad7380_timing,
  338. };
  339. static const struct ad7380_chip_info ad7384_chip_info = {
  340. .name = "ad7384",
  341. .channels = ad7384_channels,
  342. .num_channels = ARRAY_SIZE(ad7384_channels),
  343. .num_simult_channels = 2,
  344. .supplies = ad7380_supplies,
  345. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  346. .vcm_supplies = ad7380_2_channel_vcm_supplies,
  347. .num_vcm_supplies = ARRAY_SIZE(ad7380_2_channel_vcm_supplies),
  348. .available_scan_masks = ad7380_2_channel_scan_masks,
  349. .timing_specs = &ad7380_timing,
  350. };
  351. static const struct ad7380_chip_info ad7386_chip_info = {
  352. .name = "ad7386",
  353. .channels = ad7386_channels,
  354. .num_channels = ARRAY_SIZE(ad7386_channels),
  355. .num_simult_channels = 2,
  356. .supplies = ad7380_supplies,
  357. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  358. .has_mux = true,
  359. .available_scan_masks = ad7380_2x2_channel_scan_masks,
  360. .timing_specs = &ad7380_timing,
  361. };
  362. static const struct ad7380_chip_info ad7387_chip_info = {
  363. .name = "ad7387",
  364. .channels = ad7387_channels,
  365. .num_channels = ARRAY_SIZE(ad7387_channels),
  366. .num_simult_channels = 2,
  367. .supplies = ad7380_supplies,
  368. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  369. .has_mux = true,
  370. .available_scan_masks = ad7380_2x2_channel_scan_masks,
  371. .timing_specs = &ad7380_timing,
  372. };
  373. static const struct ad7380_chip_info ad7388_chip_info = {
  374. .name = "ad7388",
  375. .channels = ad7388_channels,
  376. .num_channels = ARRAY_SIZE(ad7388_channels),
  377. .num_simult_channels = 2,
  378. .supplies = ad7380_supplies,
  379. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  380. .has_mux = true,
  381. .available_scan_masks = ad7380_2x2_channel_scan_masks,
  382. .timing_specs = &ad7380_timing,
  383. };
  384. static const struct ad7380_chip_info ad7380_4_chip_info = {
  385. .name = "ad7380-4",
  386. .channels = ad7380_4_channels,
  387. .num_channels = ARRAY_SIZE(ad7380_4_channels),
  388. .num_simult_channels = 4,
  389. .supplies = ad7380_supplies,
  390. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  391. .external_ref_only = true,
  392. .available_scan_masks = ad7380_4_channel_scan_masks,
  393. .timing_specs = &ad7380_4_timing,
  394. };
  395. static const struct ad7380_chip_info ad7381_4_chip_info = {
  396. .name = "ad7381-4",
  397. .channels = ad7381_4_channels,
  398. .num_channels = ARRAY_SIZE(ad7381_4_channels),
  399. .num_simult_channels = 4,
  400. .supplies = ad7380_supplies,
  401. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  402. .available_scan_masks = ad7380_4_channel_scan_masks,
  403. .timing_specs = &ad7380_4_timing,
  404. };
  405. static const struct ad7380_chip_info ad7383_4_chip_info = {
  406. .name = "ad7383-4",
  407. .channels = ad7383_4_channels,
  408. .num_channels = ARRAY_SIZE(ad7383_4_channels),
  409. .num_simult_channels = 4,
  410. .supplies = ad7380_supplies,
  411. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  412. .vcm_supplies = ad7380_4_channel_vcm_supplies,
  413. .num_vcm_supplies = ARRAY_SIZE(ad7380_4_channel_vcm_supplies),
  414. .available_scan_masks = ad7380_4_channel_scan_masks,
  415. .timing_specs = &ad7380_4_timing,
  416. };
  417. static const struct ad7380_chip_info ad7384_4_chip_info = {
  418. .name = "ad7384-4",
  419. .channels = ad7384_4_channels,
  420. .num_channels = ARRAY_SIZE(ad7384_4_channels),
  421. .num_simult_channels = 4,
  422. .supplies = ad7380_supplies,
  423. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  424. .vcm_supplies = ad7380_4_channel_vcm_supplies,
  425. .num_vcm_supplies = ARRAY_SIZE(ad7380_4_channel_vcm_supplies),
  426. .available_scan_masks = ad7380_4_channel_scan_masks,
  427. .timing_specs = &ad7380_4_timing,
  428. };
  429. static const struct ad7380_chip_info ad7386_4_chip_info = {
  430. .name = "ad7386-4",
  431. .channels = ad7386_4_channels,
  432. .num_channels = ARRAY_SIZE(ad7386_4_channels),
  433. .num_simult_channels = 4,
  434. .supplies = ad7380_supplies,
  435. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  436. .has_mux = true,
  437. .available_scan_masks = ad7380_2x4_channel_scan_masks,
  438. .timing_specs = &ad7380_4_timing,
  439. };
  440. static const struct ad7380_chip_info ad7387_4_chip_info = {
  441. .name = "ad7387-4",
  442. .channels = ad7387_4_channels,
  443. .num_channels = ARRAY_SIZE(ad7387_4_channels),
  444. .num_simult_channels = 4,
  445. .supplies = ad7380_supplies,
  446. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  447. .has_mux = true,
  448. .available_scan_masks = ad7380_2x4_channel_scan_masks,
  449. .timing_specs = &ad7380_4_timing,
  450. };
  451. static const struct ad7380_chip_info ad7388_4_chip_info = {
  452. .name = "ad7388-4",
  453. .channels = ad7388_4_channels,
  454. .num_channels = ARRAY_SIZE(ad7388_4_channels),
  455. .num_simult_channels = 4,
  456. .supplies = ad7380_supplies,
  457. .num_supplies = ARRAY_SIZE(ad7380_supplies),
  458. .has_mux = true,
  459. .available_scan_masks = ad7380_2x4_channel_scan_masks,
  460. .timing_specs = &ad7380_4_timing,
  461. };
  462. struct ad7380_state {
  463. const struct ad7380_chip_info *chip_info;
  464. struct spi_device *spi;
  465. struct regmap *regmap;
  466. unsigned int oversampling_ratio;
  467. bool resolution_boost_enabled;
  468. unsigned int ch;
  469. bool seq;
  470. unsigned int vref_mv;
  471. unsigned int vcm_mv[MAX_NUM_CHANNELS];
  472. /* xfers, message an buffer for reading sample data */
  473. struct spi_transfer normal_xfer[2];
  474. struct spi_message normal_msg;
  475. struct spi_transfer seq_xfer[4];
  476. struct spi_message seq_msg;
  477. /*
  478. * DMA (thus cache coherency maintenance) requires the transfer buffers
  479. * to live in their own cache lines.
  480. *
  481. * Make the buffer large enough for MAX_NUM_CHANNELS 32-bit samples and
  482. * one 64-bit aligned 64-bit timestamp.
  483. */
  484. u8 scan_data[ALIGN(MAX_NUM_CHANNELS * sizeof(u32), sizeof(s64))
  485. + sizeof(s64)] __aligned(IIO_DMA_MINALIGN);
  486. /* buffers for reading/writing registers */
  487. u16 tx;
  488. u16 rx;
  489. };
  490. static int ad7380_regmap_reg_write(void *context, unsigned int reg,
  491. unsigned int val)
  492. {
  493. struct ad7380_state *st = context;
  494. struct spi_transfer xfer = {
  495. .speed_hz = AD7380_REG_WR_SPEED_HZ,
  496. .bits_per_word = 16,
  497. .len = 2,
  498. .tx_buf = &st->tx,
  499. };
  500. st->tx = FIELD_PREP(AD7380_REG_WR, 1) |
  501. FIELD_PREP(AD7380_REG_REGADDR, reg) |
  502. FIELD_PREP(AD7380_REG_DATA, val);
  503. return spi_sync_transfer(st->spi, &xfer, 1);
  504. }
  505. static int ad7380_regmap_reg_read(void *context, unsigned int reg,
  506. unsigned int *val)
  507. {
  508. struct ad7380_state *st = context;
  509. struct spi_transfer xfers[] = {
  510. {
  511. .speed_hz = AD7380_REG_WR_SPEED_HZ,
  512. .bits_per_word = 16,
  513. .len = 2,
  514. .tx_buf = &st->tx,
  515. .cs_change = 1,
  516. .cs_change_delay = {
  517. .value = st->chip_info->timing_specs->t_csh_ns,
  518. .unit = SPI_DELAY_UNIT_NSECS,
  519. },
  520. }, {
  521. .speed_hz = AD7380_REG_WR_SPEED_HZ,
  522. .bits_per_word = 16,
  523. .len = 2,
  524. .rx_buf = &st->rx,
  525. },
  526. };
  527. int ret;
  528. st->tx = FIELD_PREP(AD7380_REG_WR, 0) |
  529. FIELD_PREP(AD7380_REG_REGADDR, reg) |
  530. FIELD_PREP(AD7380_REG_DATA, 0);
  531. ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
  532. if (ret < 0)
  533. return ret;
  534. *val = FIELD_GET(AD7380_REG_DATA, st->rx);
  535. return 0;
  536. }
  537. static const struct regmap_config ad7380_regmap_config = {
  538. .reg_bits = 3,
  539. .val_bits = 12,
  540. .reg_read = ad7380_regmap_reg_read,
  541. .reg_write = ad7380_regmap_reg_write,
  542. .max_register = AD7380_REG_ADDR_ALERT_HIGH_TH,
  543. .can_sleep = true,
  544. };
  545. static int ad7380_debugfs_reg_access(struct iio_dev *indio_dev, u32 reg,
  546. u32 writeval, u32 *readval)
  547. {
  548. iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
  549. struct ad7380_state *st = iio_priv(indio_dev);
  550. if (readval)
  551. return regmap_read(st->regmap, reg, readval);
  552. else
  553. return regmap_write(st->regmap, reg, writeval);
  554. }
  555. unreachable();
  556. }
  557. /*
  558. * When switching channel, the ADC require an additional settling time.
  559. * According to the datasheet, data is value on the third CS low. We already
  560. * have an extra toggle before each read (either direct reads or buffered reads)
  561. * to sample correct data, so we just add a single CS toggle at the end of the
  562. * register write.
  563. */
  564. static int ad7380_set_ch(struct ad7380_state *st, unsigned int ch)
  565. {
  566. struct spi_transfer xfer = {
  567. .delay = {
  568. .value = T_CONVERT_NS,
  569. .unit = SPI_DELAY_UNIT_NSECS,
  570. }
  571. };
  572. int ret;
  573. if (st->ch == ch)
  574. return 0;
  575. ret = regmap_update_bits(st->regmap,
  576. AD7380_REG_ADDR_CONFIG1,
  577. AD7380_CONFIG1_CH,
  578. FIELD_PREP(AD7380_CONFIG1_CH, ch));
  579. if (ret)
  580. return ret;
  581. st->ch = ch;
  582. if (st->oversampling_ratio > 1)
  583. xfer.delay.value = T_CONVERT_0_NS +
  584. T_CONVERT_X_NS * (st->oversampling_ratio - 1);
  585. return spi_sync_transfer(st->spi, &xfer, 1);
  586. }
  587. /**
  588. * ad7380_update_xfers - update the SPI transfers base on the current scan type
  589. * @st: device instance specific state
  590. * @scan_type: current scan type
  591. */
  592. static void ad7380_update_xfers(struct ad7380_state *st,
  593. const struct iio_scan_type *scan_type)
  594. {
  595. struct spi_transfer *xfer = st->seq ? st->seq_xfer : st->normal_xfer;
  596. unsigned int t_convert = T_CONVERT_NS;
  597. /*
  598. * In the case of oversampling, conversion time is higher than in normal
  599. * mode. Technically T_CONVERT_X_NS is lower for some chips, but we use
  600. * the maximum value for simplicity for now.
  601. */
  602. if (st->oversampling_ratio > 1)
  603. t_convert = T_CONVERT_0_NS + T_CONVERT_X_NS *
  604. (st->oversampling_ratio - 1);
  605. if (st->seq) {
  606. xfer[0].delay.value = xfer[1].delay.value = t_convert;
  607. xfer[0].delay.unit = xfer[1].delay.unit = SPI_DELAY_UNIT_NSECS;
  608. xfer[2].bits_per_word = xfer[3].bits_per_word =
  609. scan_type->realbits;
  610. xfer[2].len = xfer[3].len =
  611. BITS_TO_BYTES(scan_type->storagebits) *
  612. st->chip_info->num_simult_channels;
  613. xfer[3].rx_buf = xfer[2].rx_buf + xfer[2].len;
  614. /* Additional delay required here when oversampling is enabled */
  615. if (st->oversampling_ratio > 1)
  616. xfer[2].delay.value = t_convert;
  617. else
  618. xfer[2].delay.value = 0;
  619. xfer[2].delay.unit = SPI_DELAY_UNIT_NSECS;
  620. } else {
  621. xfer[0].delay.value = t_convert;
  622. xfer[0].delay.unit = SPI_DELAY_UNIT_NSECS;
  623. xfer[1].bits_per_word = scan_type->realbits;
  624. xfer[1].len = BITS_TO_BYTES(scan_type->storagebits) *
  625. st->chip_info->num_simult_channels;
  626. }
  627. }
  628. static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev)
  629. {
  630. struct ad7380_state *st = iio_priv(indio_dev);
  631. const struct iio_scan_type *scan_type;
  632. struct spi_message *msg = &st->normal_msg;
  633. /*
  634. * Currently, we always read all channels at the same time. The scan_type
  635. * is the same for all channels, so we just pass the first channel.
  636. */
  637. scan_type = iio_get_current_scan_type(indio_dev, &indio_dev->channels[0]);
  638. if (IS_ERR(scan_type))
  639. return PTR_ERR(scan_type);
  640. if (st->chip_info->has_mux) {
  641. unsigned int index;
  642. int ret;
  643. /*
  644. * Depending on the requested scan_mask and current state,
  645. * we need to either change CH bit, or enable sequencer mode
  646. * to sample correct data.
  647. * Sequencer mode is enabled if active mask corresponds to all
  648. * IIO channels enabled. Otherwise, CH bit is set.
  649. */
  650. ret = iio_active_scan_mask_index(indio_dev);
  651. if (ret < 0)
  652. return ret;
  653. index = ret;
  654. if (index == AD7380_SCAN_MASK_SEQ) {
  655. ret = regmap_update_bits(st->regmap,
  656. AD7380_REG_ADDR_CONFIG1,
  657. AD7380_CONFIG1_SEQ,
  658. FIELD_PREP(AD7380_CONFIG1_SEQ, 1));
  659. if (ret)
  660. return ret;
  661. msg = &st->seq_msg;
  662. st->seq = true;
  663. } else {
  664. ret = ad7380_set_ch(st, index);
  665. if (ret)
  666. return ret;
  667. }
  668. }
  669. ad7380_update_xfers(st, scan_type);
  670. return spi_optimize_message(st->spi, msg);
  671. }
  672. static int ad7380_triggered_buffer_postdisable(struct iio_dev *indio_dev)
  673. {
  674. struct ad7380_state *st = iio_priv(indio_dev);
  675. struct spi_message *msg = &st->normal_msg;
  676. int ret;
  677. if (st->seq) {
  678. ret = regmap_update_bits(st->regmap,
  679. AD7380_REG_ADDR_CONFIG1,
  680. AD7380_CONFIG1_SEQ,
  681. FIELD_PREP(AD7380_CONFIG1_SEQ, 0));
  682. if (ret)
  683. return ret;
  684. msg = &st->seq_msg;
  685. st->seq = false;
  686. }
  687. spi_unoptimize_message(msg);
  688. return 0;
  689. }
  690. static const struct iio_buffer_setup_ops ad7380_buffer_setup_ops = {
  691. .preenable = ad7380_triggered_buffer_preenable,
  692. .postdisable = ad7380_triggered_buffer_postdisable,
  693. };
  694. static irqreturn_t ad7380_trigger_handler(int irq, void *p)
  695. {
  696. struct iio_poll_func *pf = p;
  697. struct iio_dev *indio_dev = pf->indio_dev;
  698. struct ad7380_state *st = iio_priv(indio_dev);
  699. struct spi_message *msg = st->seq ? &st->seq_msg : &st->normal_msg;
  700. int ret;
  701. ret = spi_sync(st->spi, msg);
  702. if (ret)
  703. goto out;
  704. iio_push_to_buffers_with_timestamp(indio_dev, &st->scan_data,
  705. pf->timestamp);
  706. out:
  707. iio_trigger_notify_done(indio_dev->trig);
  708. return IRQ_HANDLED;
  709. }
  710. static int ad7380_read_direct(struct ad7380_state *st, unsigned int scan_index,
  711. const struct iio_scan_type *scan_type, int *val)
  712. {
  713. unsigned int index = scan_index;
  714. int ret;
  715. if (st->chip_info->has_mux) {
  716. unsigned int ch = 0;
  717. if (index >= st->chip_info->num_simult_channels) {
  718. index -= st->chip_info->num_simult_channels;
  719. ch = 1;
  720. }
  721. ret = ad7380_set_ch(st, ch);
  722. if (ret)
  723. return ret;
  724. }
  725. ad7380_update_xfers(st, scan_type);
  726. ret = spi_sync(st->spi, &st->normal_msg);
  727. if (ret < 0)
  728. return ret;
  729. if (scan_type->storagebits > 16) {
  730. if (scan_type->sign == 's')
  731. *val = sign_extend32(*(u32 *)(st->scan_data + 4 * index),
  732. scan_type->realbits - 1);
  733. else
  734. *val = *(u32 *)(st->scan_data + 4 * index) &
  735. GENMASK(scan_type->realbits - 1, 0);
  736. } else {
  737. if (scan_type->sign == 's')
  738. *val = sign_extend32(*(u16 *)(st->scan_data + 2 * index),
  739. scan_type->realbits - 1);
  740. else
  741. *val = *(u16 *)(st->scan_data + 2 * index) &
  742. GENMASK(scan_type->realbits - 1, 0);
  743. }
  744. return IIO_VAL_INT;
  745. }
  746. static int ad7380_read_raw(struct iio_dev *indio_dev,
  747. struct iio_chan_spec const *chan,
  748. int *val, int *val2, long info)
  749. {
  750. struct ad7380_state *st = iio_priv(indio_dev);
  751. const struct iio_scan_type *scan_type;
  752. scan_type = iio_get_current_scan_type(indio_dev, chan);
  753. if (IS_ERR(scan_type))
  754. return PTR_ERR(scan_type);
  755. switch (info) {
  756. case IIO_CHAN_INFO_RAW:
  757. iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
  758. return ad7380_read_direct(st, chan->scan_index,
  759. scan_type, val);
  760. }
  761. unreachable();
  762. case IIO_CHAN_INFO_SCALE:
  763. /*
  764. * According to the datasheet, the LSB size is:
  765. * * (2 × VREF) / 2^N, for differential chips
  766. * * VREF / 2^N, for pseudo-differential chips
  767. * where N is the ADC resolution (i.e realbits)
  768. */
  769. *val = st->vref_mv;
  770. *val2 = scan_type->realbits - chan->differential;
  771. return IIO_VAL_FRACTIONAL_LOG2;
  772. case IIO_CHAN_INFO_OFFSET:
  773. /*
  774. * According to IIO ABI, offset is applied before scale,
  775. * so offset is: vcm_mv / scale
  776. */
  777. *val = st->vcm_mv[chan->channel] * (1 << scan_type->realbits)
  778. / st->vref_mv;
  779. return IIO_VAL_INT;
  780. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  781. *val = st->oversampling_ratio;
  782. return IIO_VAL_INT;
  783. default:
  784. return -EINVAL;
  785. }
  786. }
  787. static int ad7380_read_avail(struct iio_dev *indio_dev,
  788. struct iio_chan_spec const *chan,
  789. const int **vals, int *type, int *length,
  790. long mask)
  791. {
  792. switch (mask) {
  793. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  794. *vals = ad7380_oversampling_ratios;
  795. *length = ARRAY_SIZE(ad7380_oversampling_ratios);
  796. *type = IIO_VAL_INT;
  797. return IIO_AVAIL_LIST;
  798. default:
  799. return -EINVAL;
  800. }
  801. }
  802. /**
  803. * ad7380_osr_to_regval - convert ratio to OSR register value
  804. * @ratio: ratio to check
  805. *
  806. * Check if ratio is present in the list of available ratios and return the
  807. * corresponding value that needs to be written to the register to select that
  808. * ratio.
  809. *
  810. * Returns: register value (0 to 7) or -EINVAL if there is not an exact match
  811. */
  812. static int ad7380_osr_to_regval(int ratio)
  813. {
  814. int i;
  815. for (i = 0; i < ARRAY_SIZE(ad7380_oversampling_ratios); i++) {
  816. if (ratio == ad7380_oversampling_ratios[i])
  817. return i;
  818. }
  819. return -EINVAL;
  820. }
  821. static int ad7380_write_raw(struct iio_dev *indio_dev,
  822. struct iio_chan_spec const *chan, int val,
  823. int val2, long mask)
  824. {
  825. struct ad7380_state *st = iio_priv(indio_dev);
  826. int ret, osr, boost;
  827. switch (mask) {
  828. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  829. osr = ad7380_osr_to_regval(val);
  830. if (osr < 0)
  831. return osr;
  832. /* always enable resolution boost when oversampling is enabled */
  833. boost = osr > 0 ? 1 : 0;
  834. iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
  835. ret = regmap_update_bits(st->regmap,
  836. AD7380_REG_ADDR_CONFIG1,
  837. AD7380_CONFIG1_OSR | AD7380_CONFIG1_RES,
  838. FIELD_PREP(AD7380_CONFIG1_OSR, osr) |
  839. FIELD_PREP(AD7380_CONFIG1_RES, boost));
  840. if (ret)
  841. return ret;
  842. st->oversampling_ratio = val;
  843. st->resolution_boost_enabled = boost;
  844. /*
  845. * Perform a soft reset. This will flush the oversampling
  846. * block and FIFO but will maintain the content of the
  847. * configurable registers.
  848. */
  849. return regmap_update_bits(st->regmap,
  850. AD7380_REG_ADDR_CONFIG2,
  851. AD7380_CONFIG2_RESET,
  852. FIELD_PREP(AD7380_CONFIG2_RESET,
  853. AD7380_CONFIG2_RESET_SOFT));
  854. }
  855. unreachable();
  856. default:
  857. return -EINVAL;
  858. }
  859. }
  860. static int ad7380_get_current_scan_type(const struct iio_dev *indio_dev,
  861. const struct iio_chan_spec *chan)
  862. {
  863. struct ad7380_state *st = iio_priv(indio_dev);
  864. return st->resolution_boost_enabled ? AD7380_SCAN_TYPE_RESOLUTION_BOOST
  865. : AD7380_SCAN_TYPE_NORMAL;
  866. }
  867. static const struct iio_info ad7380_info = {
  868. .read_raw = &ad7380_read_raw,
  869. .read_avail = &ad7380_read_avail,
  870. .write_raw = &ad7380_write_raw,
  871. .get_current_scan_type = &ad7380_get_current_scan_type,
  872. .debugfs_reg_access = &ad7380_debugfs_reg_access,
  873. };
  874. static int ad7380_init(struct ad7380_state *st, bool external_ref_en)
  875. {
  876. int ret;
  877. /* perform hard reset */
  878. ret = regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG2,
  879. AD7380_CONFIG2_RESET,
  880. FIELD_PREP(AD7380_CONFIG2_RESET,
  881. AD7380_CONFIG2_RESET_HARD));
  882. if (ret < 0)
  883. return ret;
  884. if (external_ref_en) {
  885. /* select external reference voltage */
  886. ret = regmap_set_bits(st->regmap, AD7380_REG_ADDR_CONFIG1,
  887. AD7380_CONFIG1_REFSEL);
  888. if (ret < 0)
  889. return ret;
  890. }
  891. /* This is the default value after reset. */
  892. st->oversampling_ratio = 1;
  893. st->ch = 0;
  894. st->seq = false;
  895. /* SPI 1-wire mode */
  896. return regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG2,
  897. AD7380_CONFIG2_SDO,
  898. FIELD_PREP(AD7380_CONFIG2_SDO, 1));
  899. }
  900. static int ad7380_probe(struct spi_device *spi)
  901. {
  902. struct iio_dev *indio_dev;
  903. struct ad7380_state *st;
  904. bool external_ref_en;
  905. int ret, i;
  906. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  907. if (!indio_dev)
  908. return -ENOMEM;
  909. st = iio_priv(indio_dev);
  910. st->spi = spi;
  911. st->chip_info = spi_get_device_match_data(spi);
  912. if (!st->chip_info)
  913. return dev_err_probe(&spi->dev, -EINVAL, "missing match data\n");
  914. ret = devm_regulator_bulk_get_enable(&spi->dev, st->chip_info->num_supplies,
  915. st->chip_info->supplies);
  916. if (ret)
  917. return dev_err_probe(&spi->dev, ret,
  918. "Failed to enable power supplies\n");
  919. fsleep(T_POWERUP_US);
  920. if (st->chip_info->external_ref_only) {
  921. ret = devm_regulator_get_enable_read_voltage(&spi->dev,
  922. "refin");
  923. if (ret < 0)
  924. return dev_err_probe(&spi->dev, ret,
  925. "Failed to get refin regulator\n");
  926. st->vref_mv = ret / 1000;
  927. /* these chips don't have a register bit for this */
  928. external_ref_en = false;
  929. } else {
  930. /*
  931. * If there is no REFIO supply, then it means that we are using
  932. * the internal reference, otherwise REFIO is reference voltage.
  933. */
  934. ret = devm_regulator_get_enable_read_voltage(&spi->dev,
  935. "refio");
  936. if (ret < 0 && ret != -ENODEV)
  937. return dev_err_probe(&spi->dev, ret,
  938. "Failed to get refio regulator\n");
  939. external_ref_en = ret != -ENODEV;
  940. st->vref_mv = external_ref_en ? ret / 1000 : AD7380_INTERNAL_REF_MV;
  941. }
  942. if (st->chip_info->num_vcm_supplies > ARRAY_SIZE(st->vcm_mv))
  943. return dev_err_probe(&spi->dev, -EINVAL,
  944. "invalid number of VCM supplies\n");
  945. /*
  946. * pseudo-differential chips have common mode supplies for the negative
  947. * input pin.
  948. */
  949. for (i = 0; i < st->chip_info->num_vcm_supplies; i++) {
  950. const char *vcm = st->chip_info->vcm_supplies[i];
  951. ret = devm_regulator_get_enable_read_voltage(&spi->dev, vcm);
  952. if (ret < 0)
  953. return dev_err_probe(&spi->dev, ret,
  954. "Failed to get %s regulator\n",
  955. vcm);
  956. st->vcm_mv[i] = ret / 1000;
  957. }
  958. st->regmap = devm_regmap_init(&spi->dev, NULL, st, &ad7380_regmap_config);
  959. if (IS_ERR(st->regmap))
  960. return dev_err_probe(&spi->dev, PTR_ERR(st->regmap),
  961. "failed to allocate register map\n");
  962. /*
  963. * Setting up xfer structures for both normal and sequence mode. These
  964. * struct are used for both direct read and triggered buffer. Additional
  965. * fields will be set up in ad7380_update_xfers() based on the current
  966. * state of the driver at the time of the read.
  967. */
  968. /*
  969. * In normal mode a read is composed of two steps:
  970. * - first, toggle CS (no data xfer) to trigger a conversion
  971. * - then, read data
  972. */
  973. st->normal_xfer[0].cs_change = 1;
  974. st->normal_xfer[0].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
  975. st->normal_xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
  976. st->normal_xfer[1].rx_buf = st->scan_data;
  977. spi_message_init_with_transfers(&st->normal_msg, st->normal_xfer,
  978. ARRAY_SIZE(st->normal_xfer));
  979. /*
  980. * In sequencer mode a read is composed of four steps:
  981. * - CS toggle (no data xfer) to get the right point in the sequence
  982. * - CS toggle (no data xfer) to trigger a conversion of AinX0 and
  983. * acquisition of AinX1
  984. * - 2 data reads, to read AinX0 and AinX1
  985. */
  986. st->seq_xfer[0].cs_change = 1;
  987. st->seq_xfer[0].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
  988. st->seq_xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
  989. st->seq_xfer[1].cs_change = 1;
  990. st->seq_xfer[1].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
  991. st->seq_xfer[1].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
  992. st->seq_xfer[2].rx_buf = st->scan_data;
  993. st->seq_xfer[2].cs_change = 1;
  994. st->seq_xfer[2].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns;
  995. st->seq_xfer[2].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
  996. spi_message_init_with_transfers(&st->seq_msg, st->seq_xfer,
  997. ARRAY_SIZE(st->seq_xfer));
  998. indio_dev->channels = st->chip_info->channels;
  999. indio_dev->num_channels = st->chip_info->num_channels;
  1000. indio_dev->name = st->chip_info->name;
  1001. indio_dev->info = &ad7380_info;
  1002. indio_dev->modes = INDIO_DIRECT_MODE;
  1003. indio_dev->available_scan_masks = st->chip_info->available_scan_masks;
  1004. ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
  1005. iio_pollfunc_store_time,
  1006. ad7380_trigger_handler,
  1007. &ad7380_buffer_setup_ops);
  1008. if (ret)
  1009. return ret;
  1010. ret = ad7380_init(st, external_ref_en);
  1011. if (ret)
  1012. return ret;
  1013. return devm_iio_device_register(&spi->dev, indio_dev);
  1014. }
  1015. static const struct of_device_id ad7380_of_match_table[] = {
  1016. { .compatible = "adi,ad7380", .data = &ad7380_chip_info },
  1017. { .compatible = "adi,ad7381", .data = &ad7381_chip_info },
  1018. { .compatible = "adi,ad7383", .data = &ad7383_chip_info },
  1019. { .compatible = "adi,ad7384", .data = &ad7384_chip_info },
  1020. { .compatible = "adi,ad7386", .data = &ad7386_chip_info },
  1021. { .compatible = "adi,ad7387", .data = &ad7387_chip_info },
  1022. { .compatible = "adi,ad7388", .data = &ad7388_chip_info },
  1023. { .compatible = "adi,ad7380-4", .data = &ad7380_4_chip_info },
  1024. { .compatible = "adi,ad7381-4", .data = &ad7381_4_chip_info },
  1025. { .compatible = "adi,ad7383-4", .data = &ad7383_4_chip_info },
  1026. { .compatible = "adi,ad7384-4", .data = &ad7384_4_chip_info },
  1027. { .compatible = "adi,ad7386-4", .data = &ad7386_4_chip_info },
  1028. { .compatible = "adi,ad7387-4", .data = &ad7387_4_chip_info },
  1029. { .compatible = "adi,ad7388-4", .data = &ad7388_4_chip_info },
  1030. { }
  1031. };
  1032. static const struct spi_device_id ad7380_id_table[] = {
  1033. { "ad7380", (kernel_ulong_t)&ad7380_chip_info },
  1034. { "ad7381", (kernel_ulong_t)&ad7381_chip_info },
  1035. { "ad7383", (kernel_ulong_t)&ad7383_chip_info },
  1036. { "ad7384", (kernel_ulong_t)&ad7384_chip_info },
  1037. { "ad7386", (kernel_ulong_t)&ad7386_chip_info },
  1038. { "ad7387", (kernel_ulong_t)&ad7387_chip_info },
  1039. { "ad7388", (kernel_ulong_t)&ad7388_chip_info },
  1040. { "ad7380-4", (kernel_ulong_t)&ad7380_4_chip_info },
  1041. { "ad7381-4", (kernel_ulong_t)&ad7381_4_chip_info },
  1042. { "ad7383-4", (kernel_ulong_t)&ad7383_4_chip_info },
  1043. { "ad7384-4", (kernel_ulong_t)&ad7384_4_chip_info },
  1044. { "ad7386-4", (kernel_ulong_t)&ad7386_4_chip_info },
  1045. { "ad7387-4", (kernel_ulong_t)&ad7387_4_chip_info },
  1046. { "ad7388-4", (kernel_ulong_t)&ad7388_4_chip_info },
  1047. { }
  1048. };
  1049. MODULE_DEVICE_TABLE(spi, ad7380_id_table);
  1050. static struct spi_driver ad7380_driver = {
  1051. .driver = {
  1052. .name = "ad7380",
  1053. .of_match_table = ad7380_of_match_table,
  1054. },
  1055. .probe = ad7380_probe,
  1056. .id_table = ad7380_id_table,
  1057. };
  1058. module_spi_driver(ad7380_driver);
  1059. MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
  1060. MODULE_DESCRIPTION("Analog Devices AD738x ADC driver");
  1061. MODULE_LICENSE("GPL");