ad4000.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * AD4000 SPI ADC driver
  4. *
  5. * Copyright 2024 Analog Devices Inc.
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/byteorder/generic.h>
  10. #include <linux/cleanup.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/math.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/spi/offload/consumer.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/units.h>
  21. #include <linux/util_macros.h>
  22. #include <linux/iio/iio.h>
  23. #include <linux/iio/buffer.h>
  24. #include <linux/iio/buffer-dmaengine.h>
  25. #include <linux/iio/triggered_buffer.h>
  26. #include <linux/iio/trigger_consumer.h>
  27. #define AD4000_READ_COMMAND 0x54
  28. #define AD4000_WRITE_COMMAND 0x14
  29. #define AD4000_CONFIG_REG_DEFAULT 0xE1
  30. /* AD4000 Configuration Register programmable bits */
  31. #define AD4000_CFG_SPAN_COMP BIT(3) /* Input span compression */
  32. #define AD4000_CFG_HIGHZ BIT(2) /* High impedance mode */
  33. #define AD4000_CFG_TURBO BIT(1) /* Turbo mode */
  34. #define AD4000_SCALE_OPTIONS 2
  35. #define __AD4000_DIFF_CHANNEL(_sign, _real_bits, _storage_bits, _reg_access, _offl)\
  36. { \
  37. .type = IIO_VOLTAGE, \
  38. .indexed = 1, \
  39. .differential = 1, \
  40. .channel = 0, \
  41. .channel2 = 1, \
  42. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  43. BIT(IIO_CHAN_INFO_SCALE) | \
  44. (_offl ? BIT(IIO_CHAN_INFO_SAMP_FREQ) : 0), \
  45. .info_mask_separate_available = _reg_access ? BIT(IIO_CHAN_INFO_SCALE) : 0,\
  46. .scan_index = 0, \
  47. .scan_type = { \
  48. .sign = _sign, \
  49. .realbits = _real_bits, \
  50. .storagebits = _storage_bits, \
  51. .shift = (_offl ? 0 : _storage_bits - _real_bits), \
  52. .endianness = _offl ? IIO_CPU : IIO_BE \
  53. }, \
  54. }
  55. #define AD4000_DIFF_CHANNEL(_sign, _real_bits, _reg_access, _offl) \
  56. __AD4000_DIFF_CHANNEL((_sign), (_real_bits), \
  57. (((_offl) || ((_real_bits) > 16)) ? 32 : 16), \
  58. (_reg_access), (_offl))
  59. /*
  60. * When SPI offload is configured, transfers are executed without CPU
  61. * intervention so no soft timestamp can be recorded when transfers run.
  62. * Because of that, the macros that set timestamp channel are only used when
  63. * transfers are not offloaded.
  64. */
  65. #define AD4000_DIFF_CHANNELS(_sign, _real_bits, _reg_access) \
  66. { \
  67. AD4000_DIFF_CHANNEL(_sign, _real_bits, _reg_access, 0), \
  68. IIO_CHAN_SOFT_TIMESTAMP(1), \
  69. }
  70. #define __AD4000_PSEUDO_DIFF_CHANNEL(_sign, _real_bits, _storage_bits, \
  71. _reg_access, _offl) \
  72. { \
  73. .type = IIO_VOLTAGE, \
  74. .indexed = 1, \
  75. .channel = 0, \
  76. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  77. BIT(IIO_CHAN_INFO_SCALE) | \
  78. BIT(IIO_CHAN_INFO_OFFSET) | \
  79. (_offl ? BIT(IIO_CHAN_INFO_SAMP_FREQ) : 0), \
  80. .info_mask_separate_available = _reg_access ? BIT(IIO_CHAN_INFO_SCALE) : 0,\
  81. .scan_index = 0, \
  82. .scan_type = { \
  83. .sign = _sign, \
  84. .realbits = _real_bits, \
  85. .storagebits = _storage_bits, \
  86. .shift = (_offl ? 0 : _storage_bits - _real_bits), \
  87. .endianness = _offl ? IIO_CPU : IIO_BE \
  88. }, \
  89. }
  90. #define AD4000_PSEUDO_DIFF_CHANNEL(_sign, _real_bits, _reg_access, _offl) \
  91. __AD4000_PSEUDO_DIFF_CHANNEL((_sign), (_real_bits), \
  92. (((_offl) || ((_real_bits) > 16)) ? 32 : 16),\
  93. (_reg_access), (_offl))
  94. #define AD4000_PSEUDO_DIFF_CHANNELS(_sign, _real_bits, _reg_access) \
  95. { \
  96. AD4000_PSEUDO_DIFF_CHANNEL(_sign, _real_bits, _reg_access, 0), \
  97. IIO_CHAN_SOFT_TIMESTAMP(1), \
  98. }
  99. static const char * const ad4000_power_supplies[] = {
  100. "vdd", "vio"
  101. };
  102. enum ad4000_sdi {
  103. AD4000_SDI_MOSI,
  104. AD4000_SDI_VIO,
  105. AD4000_SDI_CS,
  106. AD4000_SDI_GND,
  107. };
  108. /* maps adi,sdi-pin property value to enum */
  109. static const char * const ad4000_sdi_pin[] = {
  110. [AD4000_SDI_MOSI] = "sdi",
  111. [AD4000_SDI_VIO] = "high",
  112. [AD4000_SDI_CS] = "cs",
  113. [AD4000_SDI_GND] = "low",
  114. };
  115. /* Gains stored as fractions of 1000 so they can be expressed by integers. */
  116. static const int ad4000_gains[] = {
  117. 454, 909, 1000, 1900,
  118. };
  119. struct ad4000_time_spec {
  120. int t_conv_ns;
  121. int t_quiet2_ns;
  122. };
  123. /*
  124. * Same timing specifications for all of AD4000, AD4001, ..., AD4008, AD4010,
  125. * ADAQ4001, and ADAQ4003.
  126. */
  127. static const struct ad4000_time_spec ad4000_t_spec = {
  128. .t_conv_ns = 320,
  129. .t_quiet2_ns = 60,
  130. };
  131. /* AD4020, AD4021, AD4022 */
  132. static const struct ad4000_time_spec ad4020_t_spec = {
  133. .t_conv_ns = 350,
  134. .t_quiet2_ns = 60,
  135. };
  136. /* AD7983, AD7984 */
  137. static const struct ad4000_time_spec ad7983_t_spec = {
  138. .t_conv_ns = 500,
  139. .t_quiet2_ns = 0,
  140. };
  141. /* AD7980, AD7982 */
  142. static const struct ad4000_time_spec ad7980_t_spec = {
  143. .t_conv_ns = 800,
  144. .t_quiet2_ns = 0,
  145. };
  146. /* AD7946, AD7686, AD7688, AD7988-5, AD7693 */
  147. static const struct ad4000_time_spec ad7686_t_spec = {
  148. .t_conv_ns = 1600,
  149. .t_quiet2_ns = 0,
  150. };
  151. /* AD7690 */
  152. static const struct ad4000_time_spec ad7690_t_spec = {
  153. .t_conv_ns = 2100,
  154. .t_quiet2_ns = 0,
  155. };
  156. /* AD7942, AD7685, AD7687 */
  157. static const struct ad4000_time_spec ad7687_t_spec = {
  158. .t_conv_ns = 3200,
  159. .t_quiet2_ns = 0,
  160. };
  161. /* AD7691 */
  162. static const struct ad4000_time_spec ad7691_t_spec = {
  163. .t_conv_ns = 3700,
  164. .t_quiet2_ns = 0,
  165. };
  166. /* AD7988-1 */
  167. static const struct ad4000_time_spec ad7988_1_t_spec = {
  168. .t_conv_ns = 9500,
  169. .t_quiet2_ns = 0,
  170. };
  171. struct ad4000_chip_info {
  172. const char *dev_name;
  173. struct iio_chan_spec chan_spec[2];
  174. struct iio_chan_spec reg_access_chan_spec[2];
  175. struct iio_chan_spec offload_chan_spec;
  176. struct iio_chan_spec reg_access_offload_chan_spec;
  177. const struct ad4000_time_spec *time_spec;
  178. bool has_hardware_gain;
  179. int max_rate_hz;
  180. };
  181. static const struct ad4000_chip_info ad4000_chip_info = {
  182. .dev_name = "ad4000",
  183. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 0),
  184. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 1),
  185. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0, 1),
  186. .reg_access_offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 1, 1),
  187. .time_spec = &ad4000_t_spec,
  188. .max_rate_hz = 2 * MEGA,
  189. };
  190. static const struct ad4000_chip_info ad4001_chip_info = {
  191. .dev_name = "ad4001",
  192. .chan_spec = AD4000_DIFF_CHANNELS('s', 16, 0),
  193. .reg_access_chan_spec = AD4000_DIFF_CHANNELS('s', 16, 1),
  194. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0, 1),
  195. .reg_access_offload_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 1, 1),
  196. .time_spec = &ad4000_t_spec,
  197. .max_rate_hz = 2 * MEGA,
  198. };
  199. static const struct ad4000_chip_info ad4002_chip_info = {
  200. .dev_name = "ad4002",
  201. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 18, 0),
  202. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 18, 1),
  203. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 0, 1),
  204. .reg_access_offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 1, 1),
  205. .time_spec = &ad4000_t_spec,
  206. .max_rate_hz = 2 * MEGA,
  207. };
  208. static const struct ad4000_chip_info ad4003_chip_info = {
  209. .dev_name = "ad4003",
  210. .chan_spec = AD4000_DIFF_CHANNELS('s', 18, 0),
  211. .reg_access_chan_spec = AD4000_DIFF_CHANNELS('s', 18, 1),
  212. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0, 1),
  213. .reg_access_offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1, 1),
  214. .time_spec = &ad4000_t_spec,
  215. .max_rate_hz = 2 * MEGA,
  216. };
  217. static const struct ad4000_chip_info ad4004_chip_info = {
  218. .dev_name = "ad4004",
  219. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 0),
  220. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 1),
  221. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0, 1),
  222. .reg_access_offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 1, 1),
  223. .time_spec = &ad4000_t_spec,
  224. .max_rate_hz = 1 * MEGA,
  225. };
  226. static const struct ad4000_chip_info ad4005_chip_info = {
  227. .dev_name = "ad4005",
  228. .chan_spec = AD4000_DIFF_CHANNELS('s', 16, 0),
  229. .reg_access_chan_spec = AD4000_DIFF_CHANNELS('s', 16, 1),
  230. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0, 1),
  231. .reg_access_offload_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 1, 1),
  232. .time_spec = &ad4000_t_spec,
  233. .max_rate_hz = 1 * MEGA,
  234. };
  235. static const struct ad4000_chip_info ad4006_chip_info = {
  236. .dev_name = "ad4006",
  237. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 18, 0),
  238. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 18, 1),
  239. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 0, 1),
  240. .reg_access_offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 1, 1),
  241. .time_spec = &ad4000_t_spec,
  242. .max_rate_hz = 1 * MEGA,
  243. };
  244. static const struct ad4000_chip_info ad4007_chip_info = {
  245. .dev_name = "ad4007",
  246. .chan_spec = AD4000_DIFF_CHANNELS('s', 18, 0),
  247. .reg_access_chan_spec = AD4000_DIFF_CHANNELS('s', 18, 1),
  248. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0, 1),
  249. .reg_access_offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1, 1),
  250. .time_spec = &ad4000_t_spec,
  251. .max_rate_hz = 1 * MEGA,
  252. };
  253. static const struct ad4000_chip_info ad4008_chip_info = {
  254. .dev_name = "ad4008",
  255. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 0),
  256. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 1),
  257. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0, 1),
  258. .reg_access_offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 1, 1),
  259. .time_spec = &ad4000_t_spec,
  260. .max_rate_hz = 500 * KILO,
  261. };
  262. static const struct ad4000_chip_info ad4010_chip_info = {
  263. .dev_name = "ad4010",
  264. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 18, 0),
  265. .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 18, 1),
  266. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 0, 1),
  267. .reg_access_offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 1, 1),
  268. .time_spec = &ad4000_t_spec,
  269. .max_rate_hz = 500 * KILO,
  270. };
  271. static const struct ad4000_chip_info ad4011_chip_info = {
  272. .dev_name = "ad4011",
  273. .chan_spec = AD4000_DIFF_CHANNELS('s', 18, 0),
  274. .reg_access_chan_spec = AD4000_DIFF_CHANNELS('s', 18, 1),
  275. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0, 1),
  276. .reg_access_offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1, 1),
  277. .time_spec = &ad4000_t_spec,
  278. .max_rate_hz = 500 * KILO,
  279. };
  280. static const struct ad4000_chip_info ad4020_chip_info = {
  281. .dev_name = "ad4020",
  282. .chan_spec = AD4000_DIFF_CHANNELS('s', 20, 0),
  283. .reg_access_chan_spec = AD4000_DIFF_CHANNELS('s', 20, 1),
  284. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 0, 1),
  285. .reg_access_offload_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 1, 1),
  286. .time_spec = &ad4020_t_spec,
  287. .max_rate_hz = 1800 * KILO,
  288. };
  289. static const struct ad4000_chip_info ad4021_chip_info = {
  290. .dev_name = "ad4021",
  291. .chan_spec = AD4000_DIFF_CHANNELS('s', 20, 0),
  292. .reg_access_chan_spec = AD4000_DIFF_CHANNELS('s', 20, 1),
  293. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 0, 1),
  294. .reg_access_offload_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 1, 1),
  295. .time_spec = &ad4020_t_spec,
  296. .max_rate_hz = 1 * MEGA,
  297. };
  298. static const struct ad4000_chip_info ad4022_chip_info = {
  299. .dev_name = "ad4022",
  300. .chan_spec = AD4000_DIFF_CHANNELS('s', 20, 0),
  301. .reg_access_chan_spec = AD4000_DIFF_CHANNELS('s', 20, 1),
  302. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 0, 1),
  303. .reg_access_offload_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 1, 1),
  304. .time_spec = &ad4020_t_spec,
  305. .max_rate_hz = 500 * KILO,
  306. };
  307. static const struct ad4000_chip_info adaq4001_chip_info = {
  308. .dev_name = "adaq4001",
  309. .chan_spec = AD4000_DIFF_CHANNELS('s', 16, 0),
  310. .reg_access_chan_spec = AD4000_DIFF_CHANNELS('s', 16, 1),
  311. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0, 1),
  312. .reg_access_offload_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 1, 1),
  313. .time_spec = &ad4000_t_spec,
  314. .has_hardware_gain = true,
  315. .max_rate_hz = 2 * MEGA,
  316. };
  317. static const struct ad4000_chip_info adaq4003_chip_info = {
  318. .dev_name = "adaq4003",
  319. .chan_spec = AD4000_DIFF_CHANNELS('s', 18, 0),
  320. .reg_access_chan_spec = AD4000_DIFF_CHANNELS('s', 18, 1),
  321. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0, 1),
  322. .reg_access_offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1, 1),
  323. .time_spec = &ad4000_t_spec,
  324. .has_hardware_gain = true,
  325. .max_rate_hz = 2 * MEGA,
  326. };
  327. static const struct ad4000_chip_info ad7685_chip_info = {
  328. .dev_name = "ad7685",
  329. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 0),
  330. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0, 1),
  331. .time_spec = &ad7687_t_spec,
  332. .max_rate_hz = 250 * KILO,
  333. };
  334. static const struct ad4000_chip_info ad7686_chip_info = {
  335. .dev_name = "ad7686",
  336. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 0),
  337. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0, 1),
  338. .time_spec = &ad7686_t_spec,
  339. .max_rate_hz = 500 * KILO,
  340. };
  341. static const struct ad4000_chip_info ad7687_chip_info = {
  342. .dev_name = "ad7687",
  343. .chan_spec = AD4000_DIFF_CHANNELS('s', 16, 0),
  344. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0, 1),
  345. .time_spec = &ad7687_t_spec,
  346. .max_rate_hz = 250 * KILO,
  347. };
  348. static const struct ad4000_chip_info ad7688_chip_info = {
  349. .dev_name = "ad7688",
  350. .chan_spec = AD4000_DIFF_CHANNELS('s', 16, 0),
  351. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0, 1),
  352. .time_spec = &ad7686_t_spec,
  353. .max_rate_hz = 500 * KILO,
  354. };
  355. static const struct ad4000_chip_info ad7690_chip_info = {
  356. .dev_name = "ad7690",
  357. .chan_spec = AD4000_DIFF_CHANNELS('s', 18, 0),
  358. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0, 1),
  359. .time_spec = &ad7690_t_spec,
  360. .max_rate_hz = 400 * KILO,
  361. };
  362. static const struct ad4000_chip_info ad7691_chip_info = {
  363. .dev_name = "ad7691",
  364. .chan_spec = AD4000_DIFF_CHANNELS('s', 18, 0),
  365. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0, 1),
  366. .time_spec = &ad7691_t_spec,
  367. .max_rate_hz = 250 * KILO,
  368. };
  369. static const struct ad4000_chip_info ad7693_chip_info = {
  370. .dev_name = "ad7693",
  371. .chan_spec = AD4000_DIFF_CHANNELS('s', 16, 0),
  372. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0, 1),
  373. .time_spec = &ad7686_t_spec,
  374. .max_rate_hz = 500 * KILO,
  375. };
  376. static const struct ad4000_chip_info ad7942_chip_info = {
  377. .dev_name = "ad7942",
  378. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 14, 0),
  379. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 14, 0, 1),
  380. .time_spec = &ad7687_t_spec,
  381. .max_rate_hz = 250 * KILO,
  382. };
  383. static const struct ad4000_chip_info ad7946_chip_info = {
  384. .dev_name = "ad7946",
  385. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 14, 0),
  386. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 14, 0, 1),
  387. .time_spec = &ad7686_t_spec,
  388. .max_rate_hz = 500 * KILO,
  389. };
  390. static const struct ad4000_chip_info ad7980_chip_info = {
  391. .dev_name = "ad7980",
  392. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 0),
  393. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0, 1),
  394. .time_spec = &ad7980_t_spec,
  395. .max_rate_hz = 1 * MEGA,
  396. };
  397. static const struct ad4000_chip_info ad7982_chip_info = {
  398. .dev_name = "ad7982",
  399. .chan_spec = AD4000_DIFF_CHANNELS('s', 18, 0),
  400. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0, 1),
  401. .time_spec = &ad7980_t_spec,
  402. .max_rate_hz = 1 * MEGA,
  403. };
  404. static const struct ad4000_chip_info ad7983_chip_info = {
  405. .dev_name = "ad7983",
  406. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 0),
  407. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0, 1),
  408. .time_spec = &ad7983_t_spec,
  409. .max_rate_hz = 1 * MEGA + 333 * KILO + 333,
  410. };
  411. static const struct ad4000_chip_info ad7984_chip_info = {
  412. .dev_name = "ad7984",
  413. .chan_spec = AD4000_DIFF_CHANNELS('s', 18, 0),
  414. .offload_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0, 1),
  415. .time_spec = &ad7983_t_spec,
  416. .max_rate_hz = 1 * MEGA + 333 * KILO + 333,
  417. };
  418. static const struct ad4000_chip_info ad7988_1_chip_info = {
  419. .dev_name = "ad7988-1",
  420. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 0),
  421. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0, 1),
  422. .time_spec = &ad7988_1_t_spec,
  423. .max_rate_hz = 100 * KILO,
  424. };
  425. static const struct ad4000_chip_info ad7988_5_chip_info = {
  426. .dev_name = "ad7988-5",
  427. .chan_spec = AD4000_PSEUDO_DIFF_CHANNELS('u', 16, 0),
  428. .offload_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0, 1),
  429. .time_spec = &ad7686_t_spec,
  430. .max_rate_hz = 500 * KILO,
  431. };
  432. static const struct spi_offload_config ad4000_offload_config = {
  433. .capability_flags = SPI_OFFLOAD_CAP_TRIGGER |
  434. SPI_OFFLOAD_CAP_RX_STREAM_DMA,
  435. };
  436. struct ad4000_state {
  437. struct spi_device *spi;
  438. struct gpio_desc *cnv_gpio;
  439. struct spi_transfer xfers[2];
  440. struct spi_message msg;
  441. struct spi_transfer offload_xfer;
  442. struct spi_message offload_msg;
  443. struct spi_offload *offload;
  444. struct spi_offload_trigger *offload_trigger;
  445. bool using_offload;
  446. unsigned long offload_trigger_hz;
  447. int max_rate_hz;
  448. struct mutex lock; /* Protect read modify write cycle */
  449. int vref_mv;
  450. enum ad4000_sdi sdi_pin;
  451. bool span_comp;
  452. u16 gain_milli;
  453. int scale_tbl[AD4000_SCALE_OPTIONS][2];
  454. const struct ad4000_time_spec *time_spec;
  455. /*
  456. * DMA (thus cache coherency maintenance) requires the transfer buffers
  457. * to live in their own cache lines.
  458. */
  459. struct {
  460. union {
  461. __be16 sample_buf16_be;
  462. __be32 sample_buf32_be;
  463. u16 sample_buf16;
  464. u32 sample_buf32;
  465. } data;
  466. aligned_s64 timestamp;
  467. } scan __aligned(IIO_DMA_MINALIGN);
  468. u8 tx_buf[2];
  469. u8 rx_buf[2];
  470. };
  471. static void ad4000_fill_scale_tbl(struct ad4000_state *st,
  472. struct iio_chan_spec const *chan)
  473. {
  474. int val, tmp0, tmp1;
  475. int scale_bits;
  476. u64 tmp2;
  477. /*
  478. * ADCs that output two's complement code have one less bit to express
  479. * voltage magnitude.
  480. */
  481. if (chan->scan_type.sign == 's')
  482. scale_bits = chan->scan_type.realbits - 1;
  483. else
  484. scale_bits = chan->scan_type.realbits;
  485. /*
  486. * The gain is stored as a fraction of 1000 and, as we need to
  487. * divide vref_mv by the gain, we invert the gain/1000 fraction.
  488. * Also multiply by an extra MILLI to preserve precision.
  489. * Thus, we have MILLI * MILLI equals MICRO as fraction numerator.
  490. */
  491. val = mult_frac(st->vref_mv, MICRO, st->gain_milli);
  492. /* Would multiply by NANO here but we multiplied by extra MILLI */
  493. tmp2 = (u64)val * MICRO >> scale_bits;
  494. tmp0 = div_s64_rem(tmp2, NANO, &tmp1);
  495. /* Store scale for when span compression is disabled */
  496. st->scale_tbl[0][0] = tmp0; /* Integer part */
  497. st->scale_tbl[0][1] = abs(tmp1); /* Fractional part */
  498. /* Store scale for when span compression is enabled */
  499. st->scale_tbl[1][0] = tmp0;
  500. /* The integer part is always zero so don't bother to divide it. */
  501. if (chan->differential)
  502. st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 4, 5);
  503. else
  504. st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 9, 10);
  505. }
  506. static int ad4000_write_reg(struct ad4000_state *st, uint8_t val)
  507. {
  508. st->tx_buf[0] = AD4000_WRITE_COMMAND;
  509. st->tx_buf[1] = val;
  510. return spi_write(st->spi, st->tx_buf, ARRAY_SIZE(st->tx_buf));
  511. }
  512. static int ad4000_read_reg(struct ad4000_state *st, unsigned int *val)
  513. {
  514. struct spi_transfer t = {
  515. .tx_buf = st->tx_buf,
  516. .rx_buf = st->rx_buf,
  517. .len = 2,
  518. };
  519. int ret;
  520. st->tx_buf[0] = AD4000_READ_COMMAND;
  521. ret = spi_sync_transfer(st->spi, &t, 1);
  522. if (ret < 0)
  523. return ret;
  524. *val = st->rx_buf[1];
  525. return ret;
  526. }
  527. static int ad4000_set_sampling_freq(struct ad4000_state *st, int freq)
  528. {
  529. struct spi_offload_trigger_config config = {
  530. .type = SPI_OFFLOAD_TRIGGER_PERIODIC,
  531. .periodic = {
  532. .frequency_hz = freq,
  533. },
  534. };
  535. int ret;
  536. ret = spi_offload_trigger_validate(st->offload_trigger, &config);
  537. if (ret)
  538. return ret;
  539. st->offload_trigger_hz = config.periodic.frequency_hz;
  540. return 0;
  541. }
  542. static int ad4000_convert_and_acquire(struct ad4000_state *st)
  543. {
  544. int ret;
  545. /*
  546. * In 4-wire mode, the CNV line is held high for the entire conversion
  547. * and acquisition process. In other modes, the CNV GPIO is optional
  548. * and, if provided, replaces controller CS. If CNV GPIO is not defined
  549. * gpiod_set_value_cansleep() has no effect.
  550. */
  551. gpiod_set_value_cansleep(st->cnv_gpio, 1);
  552. ret = spi_sync(st->spi, &st->msg);
  553. gpiod_set_value_cansleep(st->cnv_gpio, 0);
  554. return ret;
  555. }
  556. static int ad4000_single_conversion(struct iio_dev *indio_dev,
  557. const struct iio_chan_spec *chan, int *val)
  558. {
  559. struct ad4000_state *st = iio_priv(indio_dev);
  560. u32 sample;
  561. int ret;
  562. ret = ad4000_convert_and_acquire(st);
  563. if (ret < 0)
  564. return ret;
  565. if (chan->scan_type.endianness == IIO_BE) {
  566. if (chan->scan_type.realbits > 16)
  567. sample = be32_to_cpu(st->scan.data.sample_buf32_be);
  568. else
  569. sample = be16_to_cpu(st->scan.data.sample_buf16_be);
  570. } else {
  571. if (chan->scan_type.realbits > 16)
  572. sample = st->scan.data.sample_buf32;
  573. else
  574. sample = st->scan.data.sample_buf16;
  575. }
  576. sample >>= chan->scan_type.shift;
  577. if (chan->scan_type.sign == 's')
  578. *val = sign_extend32(sample, chan->scan_type.realbits - 1);
  579. else
  580. *val = sample;
  581. return IIO_VAL_INT;
  582. }
  583. static int ad4000_read_raw(struct iio_dev *indio_dev,
  584. struct iio_chan_spec const *chan, int *val,
  585. int *val2, long info)
  586. {
  587. struct ad4000_state *st = iio_priv(indio_dev);
  588. int ret;
  589. switch (info) {
  590. case IIO_CHAN_INFO_RAW:
  591. if (!iio_device_claim_direct(indio_dev))
  592. return -EBUSY;
  593. ret = ad4000_single_conversion(indio_dev, chan, val);
  594. iio_device_release_direct(indio_dev);
  595. return ret;
  596. case IIO_CHAN_INFO_SCALE:
  597. *val = st->scale_tbl[st->span_comp][0];
  598. *val2 = st->scale_tbl[st->span_comp][1];
  599. return IIO_VAL_INT_PLUS_NANO;
  600. case IIO_CHAN_INFO_OFFSET:
  601. *val = 0;
  602. if (st->span_comp)
  603. *val = mult_frac(st->vref_mv, 1, 10);
  604. return IIO_VAL_INT;
  605. case IIO_CHAN_INFO_SAMP_FREQ:
  606. *val = st->offload_trigger_hz;
  607. return IIO_VAL_INT;
  608. default:
  609. return -EINVAL;
  610. }
  611. }
  612. static int ad4000_read_avail(struct iio_dev *indio_dev,
  613. struct iio_chan_spec const *chan,
  614. const int **vals, int *type, int *length,
  615. long info)
  616. {
  617. struct ad4000_state *st = iio_priv(indio_dev);
  618. switch (info) {
  619. case IIO_CHAN_INFO_SCALE:
  620. *vals = (int *)st->scale_tbl;
  621. *length = AD4000_SCALE_OPTIONS * 2;
  622. *type = IIO_VAL_INT_PLUS_NANO;
  623. return IIO_AVAIL_LIST;
  624. default:
  625. return -EINVAL;
  626. }
  627. }
  628. static int ad4000_write_raw_get_fmt(struct iio_dev *indio_dev,
  629. struct iio_chan_spec const *chan, long mask)
  630. {
  631. switch (mask) {
  632. case IIO_CHAN_INFO_SCALE:
  633. return IIO_VAL_INT_PLUS_NANO;
  634. default:
  635. return IIO_VAL_INT_PLUS_MICRO;
  636. }
  637. }
  638. static int __ad4000_write_raw(struct iio_dev *indio_dev,
  639. struct iio_chan_spec const *chan,
  640. int val2)
  641. {
  642. struct ad4000_state *st = iio_priv(indio_dev);
  643. unsigned int reg_val;
  644. bool span_comp_en;
  645. int ret;
  646. guard(mutex)(&st->lock);
  647. ret = ad4000_read_reg(st, &reg_val);
  648. if (ret < 0)
  649. return ret;
  650. span_comp_en = val2 == st->scale_tbl[1][1];
  651. reg_val &= ~AD4000_CFG_SPAN_COMP;
  652. reg_val |= FIELD_PREP(AD4000_CFG_SPAN_COMP, span_comp_en);
  653. ret = ad4000_write_reg(st, reg_val);
  654. if (ret < 0)
  655. return ret;
  656. st->span_comp = span_comp_en;
  657. return 0;
  658. }
  659. static int ad4000_write_raw(struct iio_dev *indio_dev,
  660. struct iio_chan_spec const *chan,
  661. int val, int val2, long mask)
  662. {
  663. struct ad4000_state *st = iio_priv(indio_dev);
  664. int ret;
  665. switch (mask) {
  666. case IIO_CHAN_INFO_SCALE:
  667. if (!iio_device_claim_direct(indio_dev))
  668. return -EBUSY;
  669. ret = __ad4000_write_raw(indio_dev, chan, val2);
  670. iio_device_release_direct(indio_dev);
  671. return ret;
  672. case IIO_CHAN_INFO_SAMP_FREQ:
  673. if (val < 1 || val > st->max_rate_hz)
  674. return -EINVAL;
  675. if (!iio_device_claim_direct(indio_dev))
  676. return -EBUSY;
  677. ret = ad4000_set_sampling_freq(st, val);
  678. iio_device_release_direct(indio_dev);
  679. return ret;
  680. default:
  681. return -EINVAL;
  682. }
  683. }
  684. static irqreturn_t ad4000_trigger_handler(int irq, void *p)
  685. {
  686. struct iio_poll_func *pf = p;
  687. struct iio_dev *indio_dev = pf->indio_dev;
  688. struct ad4000_state *st = iio_priv(indio_dev);
  689. int ret;
  690. ret = ad4000_convert_and_acquire(st);
  691. if (ret < 0)
  692. goto err_out;
  693. iio_push_to_buffers_with_ts(indio_dev, &st->scan, sizeof(st->scan),
  694. pf->timestamp);
  695. err_out:
  696. iio_trigger_notify_done(indio_dev->trig);
  697. return IRQ_HANDLED;
  698. }
  699. static const struct iio_info ad4000_reg_access_info = {
  700. .read_raw = &ad4000_read_raw,
  701. .read_avail = &ad4000_read_avail,
  702. .write_raw = &ad4000_write_raw,
  703. .write_raw_get_fmt = &ad4000_write_raw_get_fmt,
  704. };
  705. static const struct iio_info ad4000_offload_info = {
  706. .read_raw = &ad4000_read_raw,
  707. .write_raw = &ad4000_write_raw,
  708. .write_raw_get_fmt = &ad4000_write_raw_get_fmt,
  709. };
  710. static const struct iio_info ad4000_info = {
  711. .read_raw = &ad4000_read_raw,
  712. };
  713. static int ad4000_offload_buffer_postenable(struct iio_dev *indio_dev)
  714. {
  715. struct ad4000_state *st = iio_priv(indio_dev);
  716. struct spi_offload_trigger_config config = {
  717. .type = SPI_OFFLOAD_TRIGGER_PERIODIC,
  718. .periodic = {
  719. .frequency_hz = st->offload_trigger_hz,
  720. },
  721. };
  722. return spi_offload_trigger_enable(st->offload, st->offload_trigger,
  723. &config);
  724. }
  725. static int ad4000_offload_buffer_predisable(struct iio_dev *indio_dev)
  726. {
  727. struct ad4000_state *st = iio_priv(indio_dev);
  728. spi_offload_trigger_disable(st->offload, st->offload_trigger);
  729. return 0;
  730. }
  731. static const struct iio_buffer_setup_ops ad4000_offload_buffer_setup_ops = {
  732. .postenable = &ad4000_offload_buffer_postenable,
  733. .predisable = &ad4000_offload_buffer_predisable,
  734. };
  735. static int ad4000_spi_offload_setup(struct iio_dev *indio_dev,
  736. struct ad4000_state *st)
  737. {
  738. struct spi_device *spi = st->spi;
  739. struct device *dev = &spi->dev;
  740. struct dma_chan *rx_dma;
  741. int ret;
  742. st->offload_trigger = devm_spi_offload_trigger_get(dev, st->offload,
  743. SPI_OFFLOAD_TRIGGER_PERIODIC);
  744. if (IS_ERR(st->offload_trigger))
  745. return dev_err_probe(dev, PTR_ERR(st->offload_trigger),
  746. "Failed to get offload trigger\n");
  747. ret = ad4000_set_sampling_freq(st, st->max_rate_hz);
  748. if (ret)
  749. return dev_err_probe(dev, ret,
  750. "Failed to set sampling frequency\n");
  751. rx_dma = devm_spi_offload_rx_stream_request_dma_chan(dev, st->offload);
  752. if (IS_ERR(rx_dma))
  753. return dev_err_probe(dev, PTR_ERR(rx_dma),
  754. "Failed to get offload RX DMA\n");
  755. ret = devm_iio_dmaengine_buffer_setup_with_handle(dev, indio_dev, rx_dma,
  756. IIO_BUFFER_DIRECTION_IN);
  757. if (ret)
  758. return dev_err_probe(dev, ret, "Failed to setup DMA buffer\n");
  759. return 0;
  760. }
  761. /*
  762. * This executes a data sample transfer when using SPI offloading. The device
  763. * connections should be in "3-wire" mode, selected either when the adi,sdi-pin
  764. * device tree property is absent or set to "high". Also, the ADC CNV pin must
  765. * be connected to a SPI controller CS (it can't be connected to a GPIO).
  766. *
  767. * In order to achieve the maximum sample rate, we only do one transfer per
  768. * SPI offload trigger. Because the ADC output has a one sample latency (delay)
  769. * when the device is wired in "3-wire" mode and only one transfer per sample is
  770. * being made in turbo mode, the first data sample is not valid because it
  771. * contains the output of an earlier conversion result. We also set transfer
  772. * `bits_per_word` to achieve higher throughput by using the minimum number of
  773. * SCLK cycles. Also, a delay is added to make sure we meet the minimum quiet
  774. * time before releasing the CS line.
  775. *
  776. * Note that, with `bits_per_word` set to the number of ADC precision bits,
  777. * transfers use larger word sizes that get stored in 'in-memory wordsizes' that
  778. * are always in native CPU byte order. Because of that, IIO buffer elements
  779. * ought to be read in CPU endianness which requires setting IIO scan_type
  780. * endianness accordingly (i.e. IIO_CPU).
  781. */
  782. static int ad4000_prepare_offload_message(struct ad4000_state *st,
  783. const struct iio_chan_spec *chan)
  784. {
  785. struct spi_transfer *xfer = &st->offload_xfer;
  786. xfer->bits_per_word = chan->scan_type.realbits;
  787. xfer->len = chan->scan_type.realbits > 16 ? 4 : 2;
  788. xfer->delay.value = st->time_spec->t_quiet2_ns;
  789. xfer->delay.unit = SPI_DELAY_UNIT_NSECS;
  790. xfer->offload_flags = SPI_OFFLOAD_XFER_RX_STREAM;
  791. spi_message_init_with_transfers(&st->offload_msg, xfer, 1);
  792. st->offload_msg.offload = st->offload;
  793. return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->offload_msg);
  794. }
  795. /*
  796. * This executes a data sample transfer for when the device connections are
  797. * in "3-wire" mode, selected when the adi,sdi-pin device tree property is
  798. * absent or set to "high". In this connection mode, the ADC SDI pin is
  799. * connected to MOSI or to VIO and ADC CNV pin is connected either to a SPI
  800. * controller CS or to a GPIO.
  801. * AD4000 series of devices initiate conversions on the rising edge of CNV pin.
  802. *
  803. * If the CNV pin is connected to an SPI controller CS line (which is by default
  804. * active low), the ADC readings would have a latency (delay) of one read.
  805. * Moreover, since we also do ADC sampling for filling the buffer on triggered
  806. * buffer mode, the timestamps of buffer readings would be disarranged.
  807. * To prevent the read latency and reduce the time discrepancy between the
  808. * sample read request and the time of actual sampling by the ADC, do a
  809. * preparatory transfer to pulse the CS/CNV line.
  810. */
  811. static int ad4000_prepare_3wire_mode_message(struct ad4000_state *st,
  812. const struct iio_chan_spec *chan)
  813. {
  814. struct spi_transfer *xfers = st->xfers;
  815. xfers[0].cs_change = 1;
  816. xfers[0].cs_change_delay.value = st->time_spec->t_conv_ns;
  817. xfers[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS;
  818. xfers[1].rx_buf = &st->scan.data;
  819. xfers[1].len = chan->scan_type.realbits > 16 ? 4 : 2;
  820. /*
  821. * If the device is set up for SPI offloading, IIO channel scan_type is
  822. * set to IIO_CPU. When that is the case, use larger SPI word sizes for
  823. * single-shot reads too. Thus, sample data can be correctly handled in
  824. * ad4000_single_conversion() according to scan_type endianness.
  825. */
  826. if (chan->scan_type.endianness != IIO_BE)
  827. xfers[1].bits_per_word = chan->scan_type.realbits;
  828. xfers[1].delay.value = st->time_spec->t_quiet2_ns;
  829. xfers[1].delay.unit = SPI_DELAY_UNIT_NSECS;
  830. spi_message_init_with_transfers(&st->msg, st->xfers, 2);
  831. return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg);
  832. }
  833. /*
  834. * This executes a data sample transfer for when the device connections are
  835. * in "4-wire" mode, selected when the adi,sdi-pin device tree property is
  836. * set to "cs". In this connection mode, the controller CS pin is connected to
  837. * ADC SDI pin and a GPIO is connected to ADC CNV pin.
  838. * The GPIO connected to ADC CNV pin is set outside of the SPI transfer.
  839. */
  840. static int ad4000_prepare_4wire_mode_message(struct ad4000_state *st,
  841. const struct iio_chan_spec *chan)
  842. {
  843. struct spi_transfer *xfers = st->xfers;
  844. /*
  845. * Dummy transfer to cause enough delay between CNV going high and SDI
  846. * going low.
  847. */
  848. xfers[0].cs_off = 1;
  849. xfers[0].delay.value = st->time_spec->t_conv_ns;
  850. xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS;
  851. xfers[1].rx_buf = &st->scan.data;
  852. xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits);
  853. spi_message_init_with_transfers(&st->msg, st->xfers, 2);
  854. return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg);
  855. }
  856. static int ad4000_config(struct ad4000_state *st)
  857. {
  858. unsigned int reg_val = AD4000_CONFIG_REG_DEFAULT;
  859. if (device_property_present(&st->spi->dev, "adi,high-z-input"))
  860. reg_val |= FIELD_PREP(AD4000_CFG_HIGHZ, 1);
  861. if (st->using_offload)
  862. reg_val |= FIELD_PREP(AD4000_CFG_TURBO, 1);
  863. return ad4000_write_reg(st, reg_val);
  864. }
  865. static int ad4000_probe(struct spi_device *spi)
  866. {
  867. const struct ad4000_chip_info *chip;
  868. struct device *dev = &spi->dev;
  869. struct iio_dev *indio_dev;
  870. struct ad4000_state *st;
  871. int gain_idx, ret;
  872. indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
  873. if (!indio_dev)
  874. return -ENOMEM;
  875. chip = spi_get_device_match_data(spi);
  876. if (!chip)
  877. return -EINVAL;
  878. st = iio_priv(indio_dev);
  879. st->spi = spi;
  880. st->time_spec = chip->time_spec;
  881. st->max_rate_hz = chip->max_rate_hz;
  882. ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(ad4000_power_supplies),
  883. ad4000_power_supplies);
  884. if (ret)
  885. return dev_err_probe(dev, ret, "Failed to enable power supplies\n");
  886. ret = devm_regulator_get_enable_read_voltage(dev, "ref");
  887. if (ret < 0)
  888. return dev_err_probe(dev, ret,
  889. "Failed to get ref regulator reference\n");
  890. st->vref_mv = ret / 1000;
  891. st->cnv_gpio = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_HIGH);
  892. if (IS_ERR(st->cnv_gpio))
  893. return dev_err_probe(dev, PTR_ERR(st->cnv_gpio),
  894. "Failed to get CNV GPIO");
  895. st->offload = devm_spi_offload_get(dev, spi, &ad4000_offload_config);
  896. ret = PTR_ERR_OR_ZERO(st->offload);
  897. if (ret && ret != -ENODEV)
  898. return dev_err_probe(dev, ret, "Failed to get offload\n");
  899. st->using_offload = !IS_ERR(st->offload);
  900. if (st->using_offload) {
  901. indio_dev->setup_ops = &ad4000_offload_buffer_setup_ops;
  902. ret = ad4000_spi_offload_setup(indio_dev, st);
  903. if (ret)
  904. return ret;
  905. } else {
  906. ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
  907. &iio_pollfunc_store_time,
  908. &ad4000_trigger_handler,
  909. NULL);
  910. if (ret)
  911. return ret;
  912. }
  913. ret = device_property_match_property_string(dev, "adi,sdi-pin",
  914. ad4000_sdi_pin,
  915. ARRAY_SIZE(ad4000_sdi_pin));
  916. if (ret < 0 && ret != -EINVAL)
  917. return dev_err_probe(dev, ret,
  918. "getting adi,sdi-pin property failed\n");
  919. /* Default to usual SPI connections if pin properties are not present */
  920. st->sdi_pin = ret == -EINVAL ? AD4000_SDI_MOSI : ret;
  921. switch (st->sdi_pin) {
  922. case AD4000_SDI_MOSI:
  923. indio_dev->info = &ad4000_reg_access_info;
  924. /*
  925. * In "3-wire mode", the ADC SDI line must be kept high when
  926. * data is not being clocked out of the controller.
  927. * Request the SPI controller to make MOSI idle high.
  928. */
  929. spi->mode |= SPI_MOSI_IDLE_HIGH;
  930. ret = spi_setup(spi);
  931. if (ret < 0)
  932. return ret;
  933. if (st->using_offload) {
  934. indio_dev->channels = &chip->reg_access_offload_chan_spec;
  935. indio_dev->num_channels = 1;
  936. ret = ad4000_prepare_offload_message(st, indio_dev->channels);
  937. if (ret)
  938. return dev_err_probe(dev, ret,
  939. "Failed to optimize SPI msg\n");
  940. } else {
  941. indio_dev->channels = chip->reg_access_chan_spec;
  942. indio_dev->num_channels = ARRAY_SIZE(chip->reg_access_chan_spec);
  943. }
  944. /*
  945. * Call ad4000_prepare_3wire_mode_message() so single-shot read
  946. * SPI messages are always initialized.
  947. */
  948. ret = ad4000_prepare_3wire_mode_message(st, &indio_dev->channels[0]);
  949. if (ret)
  950. return dev_err_probe(dev, ret,
  951. "Failed to optimize SPI msg\n");
  952. ret = ad4000_config(st);
  953. if (ret < 0)
  954. return dev_err_probe(dev, ret, "Failed to config device\n");
  955. break;
  956. case AD4000_SDI_VIO:
  957. if (st->using_offload) {
  958. indio_dev->info = &ad4000_offload_info;
  959. indio_dev->channels = &chip->offload_chan_spec;
  960. indio_dev->num_channels = 1;
  961. ret = ad4000_prepare_offload_message(st, indio_dev->channels);
  962. if (ret)
  963. return dev_err_probe(dev, ret,
  964. "Failed to optimize SPI msg\n");
  965. } else {
  966. indio_dev->info = &ad4000_info;
  967. indio_dev->channels = chip->chan_spec;
  968. indio_dev->num_channels = ARRAY_SIZE(chip->chan_spec);
  969. }
  970. ret = ad4000_prepare_3wire_mode_message(st, &indio_dev->channels[0]);
  971. if (ret)
  972. return dev_err_probe(dev, ret,
  973. "Failed to optimize SPI msg\n");
  974. break;
  975. case AD4000_SDI_CS:
  976. if (st->using_offload)
  977. return dev_err_probe(dev, -EPROTONOSUPPORT,
  978. "Unsupported sdi-pin + offload config\n");
  979. indio_dev->info = &ad4000_info;
  980. indio_dev->channels = chip->chan_spec;
  981. indio_dev->num_channels = ARRAY_SIZE(chip->chan_spec);
  982. ret = ad4000_prepare_4wire_mode_message(st, &indio_dev->channels[0]);
  983. if (ret)
  984. return dev_err_probe(dev, ret,
  985. "Failed to optimize SPI msg\n");
  986. break;
  987. case AD4000_SDI_GND:
  988. return dev_err_probe(dev, -EPROTONOSUPPORT,
  989. "Unsupported connection mode\n");
  990. default:
  991. return dev_err_probe(dev, -EINVAL, "Unrecognized connection mode\n");
  992. }
  993. indio_dev->name = chip->dev_name;
  994. ret = devm_mutex_init(dev, &st->lock);
  995. if (ret)
  996. return ret;
  997. st->gain_milli = 1000;
  998. if (chip->has_hardware_gain) {
  999. ret = device_property_read_u16(dev, "adi,gain-milli",
  1000. &st->gain_milli);
  1001. if (!ret) {
  1002. /* Match gain value from dt to one of supported gains */
  1003. gain_idx = find_closest(st->gain_milli, ad4000_gains,
  1004. ARRAY_SIZE(ad4000_gains));
  1005. st->gain_milli = ad4000_gains[gain_idx];
  1006. } else {
  1007. return dev_err_probe(dev, ret,
  1008. "Failed to read gain property\n");
  1009. }
  1010. }
  1011. ad4000_fill_scale_tbl(st, &indio_dev->channels[0]);
  1012. return devm_iio_device_register(dev, indio_dev);
  1013. }
  1014. static const struct spi_device_id ad4000_id[] = {
  1015. { "ad4000", (kernel_ulong_t)&ad4000_chip_info },
  1016. { "ad4001", (kernel_ulong_t)&ad4001_chip_info },
  1017. { "ad4002", (kernel_ulong_t)&ad4002_chip_info },
  1018. { "ad4003", (kernel_ulong_t)&ad4003_chip_info },
  1019. { "ad4004", (kernel_ulong_t)&ad4004_chip_info },
  1020. { "ad4005", (kernel_ulong_t)&ad4005_chip_info },
  1021. { "ad4006", (kernel_ulong_t)&ad4006_chip_info },
  1022. { "ad4007", (kernel_ulong_t)&ad4007_chip_info },
  1023. { "ad4008", (kernel_ulong_t)&ad4008_chip_info },
  1024. { "ad4010", (kernel_ulong_t)&ad4010_chip_info },
  1025. { "ad4011", (kernel_ulong_t)&ad4011_chip_info },
  1026. { "ad4020", (kernel_ulong_t)&ad4020_chip_info },
  1027. { "ad4021", (kernel_ulong_t)&ad4021_chip_info },
  1028. { "ad4022", (kernel_ulong_t)&ad4022_chip_info },
  1029. { "adaq4001", (kernel_ulong_t)&adaq4001_chip_info },
  1030. { "adaq4003", (kernel_ulong_t)&adaq4003_chip_info },
  1031. { "ad7685", (kernel_ulong_t)&ad7685_chip_info },
  1032. { "ad7686", (kernel_ulong_t)&ad7686_chip_info },
  1033. { "ad7687", (kernel_ulong_t)&ad7687_chip_info },
  1034. { "ad7688", (kernel_ulong_t)&ad7688_chip_info },
  1035. { "ad7690", (kernel_ulong_t)&ad7690_chip_info },
  1036. { "ad7691", (kernel_ulong_t)&ad7691_chip_info },
  1037. { "ad7693", (kernel_ulong_t)&ad7693_chip_info },
  1038. { "ad7942", (kernel_ulong_t)&ad7942_chip_info },
  1039. { "ad7946", (kernel_ulong_t)&ad7946_chip_info },
  1040. { "ad7980", (kernel_ulong_t)&ad7980_chip_info },
  1041. { "ad7982", (kernel_ulong_t)&ad7982_chip_info },
  1042. { "ad7983", (kernel_ulong_t)&ad7983_chip_info },
  1043. { "ad7984", (kernel_ulong_t)&ad7984_chip_info },
  1044. { "ad7988-1", (kernel_ulong_t)&ad7988_1_chip_info },
  1045. { "ad7988-5", (kernel_ulong_t)&ad7988_5_chip_info },
  1046. { }
  1047. };
  1048. MODULE_DEVICE_TABLE(spi, ad4000_id);
  1049. static const struct of_device_id ad4000_of_match[] = {
  1050. { .compatible = "adi,ad4000", .data = &ad4000_chip_info },
  1051. { .compatible = "adi,ad4001", .data = &ad4001_chip_info },
  1052. { .compatible = "adi,ad4002", .data = &ad4002_chip_info },
  1053. { .compatible = "adi,ad4003", .data = &ad4003_chip_info },
  1054. { .compatible = "adi,ad4004", .data = &ad4004_chip_info },
  1055. { .compatible = "adi,ad4005", .data = &ad4005_chip_info },
  1056. { .compatible = "adi,ad4006", .data = &ad4006_chip_info },
  1057. { .compatible = "adi,ad4007", .data = &ad4007_chip_info },
  1058. { .compatible = "adi,ad4008", .data = &ad4008_chip_info },
  1059. { .compatible = "adi,ad4010", .data = &ad4010_chip_info },
  1060. { .compatible = "adi,ad4011", .data = &ad4011_chip_info },
  1061. { .compatible = "adi,ad4020", .data = &ad4020_chip_info },
  1062. { .compatible = "adi,ad4021", .data = &ad4021_chip_info },
  1063. { .compatible = "adi,ad4022", .data = &ad4022_chip_info },
  1064. { .compatible = "adi,adaq4001", .data = &adaq4001_chip_info },
  1065. { .compatible = "adi,adaq4003", .data = &adaq4003_chip_info },
  1066. { .compatible = "adi,ad7685", .data = &ad7685_chip_info },
  1067. { .compatible = "adi,ad7686", .data = &ad7686_chip_info },
  1068. { .compatible = "adi,ad7687", .data = &ad7687_chip_info },
  1069. { .compatible = "adi,ad7688", .data = &ad7688_chip_info },
  1070. { .compatible = "adi,ad7690", .data = &ad7690_chip_info },
  1071. { .compatible = "adi,ad7691", .data = &ad7691_chip_info },
  1072. { .compatible = "adi,ad7693", .data = &ad7693_chip_info },
  1073. { .compatible = "adi,ad7942", .data = &ad7942_chip_info },
  1074. { .compatible = "adi,ad7946", .data = &ad7946_chip_info },
  1075. { .compatible = "adi,ad7980", .data = &ad7980_chip_info },
  1076. { .compatible = "adi,ad7982", .data = &ad7982_chip_info },
  1077. { .compatible = "adi,ad7983", .data = &ad7983_chip_info },
  1078. { .compatible = "adi,ad7984", .data = &ad7984_chip_info },
  1079. { .compatible = "adi,ad7988-1", .data = &ad7988_1_chip_info },
  1080. { .compatible = "adi,ad7988-5", .data = &ad7988_5_chip_info },
  1081. { }
  1082. };
  1083. MODULE_DEVICE_TABLE(of, ad4000_of_match);
  1084. static struct spi_driver ad4000_driver = {
  1085. .driver = {
  1086. .name = "ad4000",
  1087. .of_match_table = ad4000_of_match,
  1088. },
  1089. .probe = ad4000_probe,
  1090. .id_table = ad4000_id,
  1091. };
  1092. module_spi_driver(ad4000_driver);
  1093. MODULE_AUTHOR("Marcelo Schmitt <marcelo.schmitt@analog.com>");
  1094. MODULE_DESCRIPTION("Analog Devices AD4000 ADC driver");
  1095. MODULE_LICENSE("GPL");
  1096. MODULE_IMPORT_NS("IIO_DMAENGINE_BUFFER");