ad9467.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Analog Devices AD9467 SPI ADC driver
  4. *
  5. * Copyright 2012-2020 Analog Devices Inc.
  6. */
  7. #include <linux/bitmap.h>
  8. #include <linux/bitops.h>
  9. #include <linux/cleanup.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/device.h>
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/err.h>
  19. #include <linux/delay.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/of.h>
  22. #include <linux/iio/backend.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/sysfs.h>
  25. #include <linux/clk.h>
  26. /*
  27. * ADI High-Speed ADC common spi interface registers
  28. * See Application-Note AN-877:
  29. * https://www.analog.com/media/en/technical-documentation/application-notes/AN-877.pdf
  30. */
  31. #define AN877_ADC_REG_CHIP_PORT_CONF 0x00
  32. #define AN877_ADC_REG_CHIP_ID 0x01
  33. #define AN877_ADC_REG_CHIP_GRADE 0x02
  34. #define AN877_ADC_REG_CHAN_INDEX 0x05
  35. #define AN877_ADC_REG_TRANSFER 0xFF
  36. #define AN877_ADC_REG_MODES 0x08
  37. #define AN877_ADC_REG_TEST_IO 0x0D
  38. #define AN877_ADC_REG_ADC_INPUT 0x0F
  39. #define AN877_ADC_REG_OFFSET 0x10
  40. #define AN877_ADC_REG_OUTPUT_MODE 0x14
  41. #define AN877_ADC_REG_OUTPUT_ADJUST 0x15
  42. #define AN877_ADC_REG_OUTPUT_PHASE 0x16
  43. #define AN877_ADC_REG_OUTPUT_DELAY 0x17
  44. #define AN877_ADC_REG_VREF 0x18
  45. #define AN877_ADC_REG_ANALOG_INPUT 0x2C
  46. /* AN877_ADC_REG_TEST_IO */
  47. #define AN877_ADC_TESTMODE_OFF 0x0
  48. #define AN877_ADC_TESTMODE_MIDSCALE_SHORT 0x1
  49. #define AN877_ADC_TESTMODE_POS_FULLSCALE 0x2
  50. #define AN877_ADC_TESTMODE_NEG_FULLSCALE 0x3
  51. #define AN877_ADC_TESTMODE_ALT_CHECKERBOARD 0x4
  52. #define AN877_ADC_TESTMODE_PN23_SEQ 0x5
  53. #define AN877_ADC_TESTMODE_PN9_SEQ 0x6
  54. #define AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE 0x7
  55. #define AN877_ADC_TESTMODE_USER 0x8
  56. #define AN877_ADC_TESTMODE_BIT_TOGGLE 0x9
  57. #define AN877_ADC_TESTMODE_SYNC 0xA
  58. #define AN877_ADC_TESTMODE_ONE_BIT_HIGH 0xB
  59. #define AN877_ADC_TESTMODE_MIXED_BIT_FREQUENCY 0xC
  60. #define AN877_ADC_TESTMODE_RAMP 0xF
  61. /* AN877_ADC_REG_TRANSFER */
  62. #define AN877_ADC_TRANSFER_SYNC 0x1
  63. /* AN877_ADC_REG_OUTPUT_MODE */
  64. #define AN877_ADC_OUTPUT_MODE_OFFSET_BINARY 0x0
  65. #define AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT 0x1
  66. #define AN877_ADC_OUTPUT_MODE_GRAY_CODE 0x2
  67. /* AN877_ADC_REG_OUTPUT_PHASE */
  68. #define AN877_ADC_OUTPUT_EVEN_ODD_MODE_EN 0x20
  69. #define AN877_ADC_INVERT_DCO_CLK 0x80
  70. /* AN877_ADC_REG_OUTPUT_DELAY */
  71. #define AN877_ADC_DCO_DELAY_ENABLE 0x80
  72. /*
  73. * Analog Devices AD9265 16-Bit, 125/105/80 MSPS ADC
  74. */
  75. #define CHIPID_AD9265 0x64
  76. #define AD9265_DEF_OUTPUT_MODE 0x40
  77. #define AD9265_REG_VREF_MASK 0xC0
  78. /*
  79. * Analog Devices AD9434 12-Bit, 370/500 MSPS ADC
  80. */
  81. #define CHIPID_AD9434 0x6A
  82. #define AD9434_DEF_OUTPUT_MODE 0x00
  83. #define AD9434_REG_VREF_MASK 0xC0
  84. /*
  85. * Analog Devices AD9467 16-Bit, 200/250 MSPS ADC
  86. */
  87. #define CHIPID_AD9467 0x50
  88. #define AD9467_DEF_OUTPUT_MODE 0x08
  89. #define AD9467_REG_VREF_MASK 0x0F
  90. /*
  91. * Analog Devices AD9643 14-Bit, 170/210/250 MSPS ADC
  92. */
  93. #define CHIPID_AD9643 0x82
  94. #define AD9643_REG_VREF_MASK 0x1F
  95. /*
  96. * Analog Devices AD9652 16-bit 310 MSPS ADC
  97. */
  98. #define CHIPID_AD9652 0xC1
  99. #define AD9652_REG_VREF_MASK 0xC0
  100. /*
  101. * Analog Devices AD9649 14-bit 20/40/65/80 MSPS ADC
  102. */
  103. #define CHIPID_AD9649 0x6F
  104. #define AD9649_TEST_POINTS 8
  105. #define AD9647_MAX_TEST_POINTS 32
  106. #define AD9467_CAN_INVERT(st) \
  107. (!(st)->info->has_dco || (st)->info->has_dco_invert)
  108. struct ad9467_chip_info {
  109. const char *name;
  110. unsigned int id;
  111. const struct iio_chan_spec *channels;
  112. unsigned int num_channels;
  113. const unsigned int (*scale_table)[2];
  114. int num_scales;
  115. unsigned long test_mask;
  116. unsigned int test_mask_len;
  117. unsigned long max_rate;
  118. unsigned int default_output_mode;
  119. unsigned int vref_mask;
  120. unsigned int num_lanes;
  121. unsigned int dco_en;
  122. unsigned int test_points;
  123. /* data clock output */
  124. bool has_dco;
  125. bool has_dco_invert;
  126. };
  127. struct ad9467_chan_test_mode {
  128. struct ad9467_state *st;
  129. unsigned int idx;
  130. u8 mode;
  131. };
  132. struct ad9467_state {
  133. const struct ad9467_chip_info *info;
  134. struct iio_backend *back;
  135. struct spi_device *spi;
  136. struct clk *clk;
  137. /* used for debugfs */
  138. struct ad9467_chan_test_mode *chan_test;
  139. unsigned int output_mode;
  140. unsigned int (*scales)[2];
  141. /*
  142. * Times 2 because we may also invert the signal polarity and run the
  143. * calibration again. For some reference on the test points (ad9265) see:
  144. * https://www.analog.com/media/en/technical-documentation/data-sheets/ad9265.pdf
  145. * at page 38 for the dco output delay. On devices as ad9467, the
  146. * calibration is done at the backend level. For the ADI axi-adc:
  147. * https://wiki.analog.com/resources/fpga/docs/axi_adc_ip
  148. * at the io delay control section.
  149. */
  150. DECLARE_BITMAP(calib_map, AD9647_MAX_TEST_POINTS * 2);
  151. /* number of bits of the map */
  152. unsigned int calib_map_size;
  153. struct gpio_desc *pwrdown_gpio;
  154. /* ensure consistent state obtained on multiple related accesses */
  155. struct mutex lock;
  156. u8 buf[3] __aligned(IIO_DMA_MINALIGN);
  157. };
  158. static int ad9467_spi_read(struct ad9467_state *st, unsigned int reg)
  159. {
  160. unsigned char tbuf[2], rbuf[1];
  161. int ret;
  162. tbuf[0] = 0x80 | (reg >> 8);
  163. tbuf[1] = reg & 0xFF;
  164. ret = spi_write_then_read(st->spi,
  165. tbuf, ARRAY_SIZE(tbuf),
  166. rbuf, ARRAY_SIZE(rbuf));
  167. if (ret < 0)
  168. return ret;
  169. return rbuf[0];
  170. }
  171. static int ad9467_spi_write(struct ad9467_state *st, unsigned int reg,
  172. unsigned int val)
  173. {
  174. st->buf[0] = reg >> 8;
  175. st->buf[1] = reg & 0xFF;
  176. st->buf[2] = val;
  177. return spi_write(st->spi, st->buf, ARRAY_SIZE(st->buf));
  178. }
  179. static int ad9467_reg_access(struct iio_dev *indio_dev, unsigned int reg,
  180. unsigned int writeval, unsigned int *readval)
  181. {
  182. struct ad9467_state *st = iio_priv(indio_dev);
  183. int ret;
  184. if (!readval) {
  185. guard(mutex)(&st->lock);
  186. ret = ad9467_spi_write(st, reg, writeval);
  187. if (ret)
  188. return ret;
  189. return ad9467_spi_write(st, AN877_ADC_REG_TRANSFER,
  190. AN877_ADC_TRANSFER_SYNC);
  191. }
  192. ret = ad9467_spi_read(st, reg);
  193. if (ret < 0)
  194. return ret;
  195. *readval = ret;
  196. return 0;
  197. }
  198. static const unsigned int ad9265_scale_table[][2] = {
  199. {1250, 0x00}, {1500, 0x40}, {1750, 0x80}, {2000, 0xC0},
  200. };
  201. static const unsigned int ad9434_scale_table[][2] = {
  202. {1600, 0x1C}, {1580, 0x1D}, {1550, 0x1E}, {1520, 0x1F}, {1500, 0x00},
  203. {1470, 0x01}, {1440, 0x02}, {1420, 0x03}, {1390, 0x04}, {1360, 0x05},
  204. {1340, 0x06}, {1310, 0x07}, {1280, 0x08}, {1260, 0x09}, {1230, 0x0A},
  205. {1200, 0x0B}, {1180, 0x0C},
  206. };
  207. static const unsigned int ad9467_scale_table[][2] = {
  208. {2000, 0}, {2100, 6}, {2200, 7},
  209. {2300, 8}, {2400, 9}, {2500, 10},
  210. };
  211. static const unsigned int ad9643_scale_table[][2] = {
  212. {2087, 0x0F}, {2065, 0x0E}, {2042, 0x0D}, {2020, 0x0C}, {1997, 0x0B},
  213. {1975, 0x0A}, {1952, 0x09}, {1930, 0x08}, {1907, 0x07}, {1885, 0x06},
  214. {1862, 0x05}, {1840, 0x04}, {1817, 0x03}, {1795, 0x02}, {1772, 0x01},
  215. {1750, 0x00}, {1727, 0x1F}, {1704, 0x1E}, {1681, 0x1D}, {1658, 0x1C},
  216. {1635, 0x1B}, {1612, 0x1A}, {1589, 0x19}, {1567, 0x18}, {1544, 0x17},
  217. {1521, 0x16}, {1498, 0x15}, {1475, 0x14}, {1452, 0x13}, {1429, 0x12},
  218. {1406, 0x11}, {1383, 0x10},
  219. };
  220. static const unsigned int ad9649_scale_table[][2] = {
  221. {2000, 0},
  222. };
  223. static const unsigned int ad9652_scale_table[][2] = {
  224. {1250, 0}, {1125, 1}, {1200, 2}, {1250, 3}, {1000, 5},
  225. };
  226. static void __ad9467_get_scale(struct ad9467_state *st, int index,
  227. unsigned int *val, unsigned int *val2)
  228. {
  229. const struct ad9467_chip_info *info = st->info;
  230. const struct iio_chan_spec *chan = &info->channels[0];
  231. unsigned int tmp;
  232. tmp = (info->scale_table[index][0] * 1000000ULL) >>
  233. chan->scan_type.realbits;
  234. *val = tmp / 1000000;
  235. *val2 = tmp % 1000000;
  236. }
  237. #define AD9467_CHAN(_chan, avai_mask, _si, _bits, _sign) \
  238. { \
  239. .type = IIO_VOLTAGE, \
  240. .indexed = 1, \
  241. .channel = _chan, \
  242. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  243. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  244. .info_mask_shared_by_type_available = avai_mask, \
  245. .scan_index = _si, \
  246. .scan_type = { \
  247. .sign = _sign, \
  248. .realbits = _bits, \
  249. .storagebits = 16, \
  250. }, \
  251. }
  252. static const struct iio_chan_spec ad9434_channels[] = {
  253. AD9467_CHAN(0, BIT(IIO_CHAN_INFO_SCALE), 0, 12, 's'),
  254. };
  255. static const struct iio_chan_spec ad9467_channels[] = {
  256. AD9467_CHAN(0, BIT(IIO_CHAN_INFO_SCALE), 0, 16, 's'),
  257. };
  258. static const struct iio_chan_spec ad9643_channels[] = {
  259. AD9467_CHAN(0, BIT(IIO_CHAN_INFO_SCALE), 0, 14, 's'),
  260. AD9467_CHAN(1, BIT(IIO_CHAN_INFO_SCALE), 1, 14, 's'),
  261. };
  262. static const struct iio_chan_spec ad9649_channels[] = {
  263. AD9467_CHAN(0, 0, 0, 14, 's'),
  264. };
  265. static const struct iio_chan_spec ad9652_channels[] = {
  266. AD9467_CHAN(0, BIT(IIO_CHAN_INFO_SCALE), 0, 16, 's'),
  267. AD9467_CHAN(1, BIT(IIO_CHAN_INFO_SCALE), 1, 16, 's'),
  268. };
  269. static const char * const ad9467_test_modes[] = {
  270. [AN877_ADC_TESTMODE_OFF] = "off",
  271. [AN877_ADC_TESTMODE_MIDSCALE_SHORT] = "midscale_short",
  272. [AN877_ADC_TESTMODE_POS_FULLSCALE] = "pos_fullscale",
  273. [AN877_ADC_TESTMODE_NEG_FULLSCALE] = "neg_fullscale",
  274. [AN877_ADC_TESTMODE_ALT_CHECKERBOARD] = "checkerboard",
  275. [AN877_ADC_TESTMODE_PN23_SEQ] = "prbs23",
  276. [AN877_ADC_TESTMODE_PN9_SEQ] = "prbs9",
  277. [AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE] = "one_zero_toggle",
  278. [AN877_ADC_TESTMODE_USER] = "user",
  279. [AN877_ADC_TESTMODE_BIT_TOGGLE] = "bit_toggle",
  280. [AN877_ADC_TESTMODE_SYNC] = "sync",
  281. [AN877_ADC_TESTMODE_ONE_BIT_HIGH] = "one_bit_high",
  282. [AN877_ADC_TESTMODE_MIXED_BIT_FREQUENCY] = "mixed_bit_frequency",
  283. [AN877_ADC_TESTMODE_RAMP] = "ramp",
  284. };
  285. static const struct ad9467_chip_info ad9467_chip_tbl = {
  286. .name = "ad9467",
  287. .id = CHIPID_AD9467,
  288. .max_rate = 250000000UL,
  289. .scale_table = ad9467_scale_table,
  290. .num_scales = ARRAY_SIZE(ad9467_scale_table),
  291. .channels = ad9467_channels,
  292. .num_channels = ARRAY_SIZE(ad9467_channels),
  293. .test_points = AD9647_MAX_TEST_POINTS,
  294. .test_mask = GENMASK(AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE,
  295. AN877_ADC_TESTMODE_OFF),
  296. .test_mask_len = AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE + 1,
  297. .default_output_mode = AD9467_DEF_OUTPUT_MODE,
  298. .vref_mask = AD9467_REG_VREF_MASK,
  299. .num_lanes = 8,
  300. };
  301. static const struct ad9467_chip_info ad9434_chip_tbl = {
  302. .name = "ad9434",
  303. .id = CHIPID_AD9434,
  304. .max_rate = 500000000UL,
  305. .scale_table = ad9434_scale_table,
  306. .num_scales = ARRAY_SIZE(ad9434_scale_table),
  307. .channels = ad9434_channels,
  308. .num_channels = ARRAY_SIZE(ad9434_channels),
  309. .test_points = AD9647_MAX_TEST_POINTS,
  310. .test_mask = GENMASK(AN877_ADC_TESTMODE_USER, AN877_ADC_TESTMODE_OFF),
  311. .test_mask_len = AN877_ADC_TESTMODE_USER + 1,
  312. .default_output_mode = AD9434_DEF_OUTPUT_MODE,
  313. .vref_mask = AD9434_REG_VREF_MASK,
  314. .num_lanes = 6,
  315. };
  316. static const struct ad9467_chip_info ad9265_chip_tbl = {
  317. .name = "ad9265",
  318. .id = CHIPID_AD9265,
  319. .max_rate = 125000000UL,
  320. .scale_table = ad9265_scale_table,
  321. .num_scales = ARRAY_SIZE(ad9265_scale_table),
  322. .channels = ad9467_channels,
  323. .num_channels = ARRAY_SIZE(ad9467_channels),
  324. .test_points = AD9647_MAX_TEST_POINTS,
  325. .test_mask = GENMASK(AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE,
  326. AN877_ADC_TESTMODE_OFF),
  327. .test_mask_len = AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE + 1,
  328. .default_output_mode = AD9265_DEF_OUTPUT_MODE,
  329. .vref_mask = AD9265_REG_VREF_MASK,
  330. .has_dco = true,
  331. .has_dco_invert = true,
  332. };
  333. static const struct ad9467_chip_info ad9643_chip_tbl = {
  334. .name = "ad9643",
  335. .id = CHIPID_AD9643,
  336. .max_rate = 250000000UL,
  337. .scale_table = ad9643_scale_table,
  338. .num_scales = ARRAY_SIZE(ad9643_scale_table),
  339. .channels = ad9643_channels,
  340. .num_channels = ARRAY_SIZE(ad9643_channels),
  341. .test_points = AD9647_MAX_TEST_POINTS,
  342. .test_mask = BIT(AN877_ADC_TESTMODE_RAMP) |
  343. GENMASK(AN877_ADC_TESTMODE_MIXED_BIT_FREQUENCY, AN877_ADC_TESTMODE_OFF),
  344. .test_mask_len = AN877_ADC_TESTMODE_RAMP + 1,
  345. .vref_mask = AD9643_REG_VREF_MASK,
  346. .has_dco = true,
  347. .has_dco_invert = true,
  348. .dco_en = AN877_ADC_DCO_DELAY_ENABLE,
  349. };
  350. static const struct ad9467_chip_info ad9649_chip_tbl = {
  351. .name = "ad9649",
  352. .id = CHIPID_AD9649,
  353. .max_rate = 80000000UL,
  354. .scale_table = ad9649_scale_table,
  355. .num_scales = ARRAY_SIZE(ad9649_scale_table),
  356. .channels = ad9649_channels,
  357. .num_channels = ARRAY_SIZE(ad9649_channels),
  358. .test_points = AD9649_TEST_POINTS,
  359. .test_mask = GENMASK(AN877_ADC_TESTMODE_MIXED_BIT_FREQUENCY,
  360. AN877_ADC_TESTMODE_OFF),
  361. .test_mask_len = AN877_ADC_TESTMODE_MIXED_BIT_FREQUENCY + 1,
  362. .has_dco = true,
  363. .has_dco_invert = true,
  364. .dco_en = AN877_ADC_DCO_DELAY_ENABLE,
  365. };
  366. static const struct ad9467_chip_info ad9652_chip_tbl = {
  367. .name = "ad9652",
  368. .id = CHIPID_AD9652,
  369. .max_rate = 310000000UL,
  370. .scale_table = ad9652_scale_table,
  371. .num_scales = ARRAY_SIZE(ad9652_scale_table),
  372. .channels = ad9652_channels,
  373. .num_channels = ARRAY_SIZE(ad9652_channels),
  374. .test_points = AD9647_MAX_TEST_POINTS,
  375. .test_mask = GENMASK(AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE,
  376. AN877_ADC_TESTMODE_OFF),
  377. .test_mask_len = AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE + 1,
  378. .vref_mask = AD9652_REG_VREF_MASK,
  379. .has_dco = true,
  380. };
  381. static int ad9467_get_scale(struct ad9467_state *st, int *val, int *val2)
  382. {
  383. const struct ad9467_chip_info *info = st->info;
  384. unsigned int vref_val;
  385. unsigned int i = 0;
  386. int ret;
  387. /* nothing to read if we only have one possible scale */
  388. if (info->num_scales == 1)
  389. goto out_get_scale;
  390. ret = ad9467_spi_read(st, AN877_ADC_REG_VREF);
  391. if (ret < 0)
  392. return ret;
  393. vref_val = ret & info->vref_mask;
  394. for (i = 0; i < info->num_scales; i++) {
  395. if (vref_val == info->scale_table[i][1])
  396. break;
  397. }
  398. if (i == info->num_scales)
  399. return -ERANGE;
  400. out_get_scale:
  401. __ad9467_get_scale(st, i, val, val2);
  402. return IIO_VAL_INT_PLUS_MICRO;
  403. }
  404. static int ad9467_set_scale(struct ad9467_state *st, int val, int val2)
  405. {
  406. const struct ad9467_chip_info *info = st->info;
  407. unsigned int scale_val[2];
  408. unsigned int i;
  409. int ret;
  410. if (val != 0)
  411. return -EINVAL;
  412. if (info->num_scales == 1)
  413. return -EOPNOTSUPP;
  414. for (i = 0; i < info->num_scales; i++) {
  415. __ad9467_get_scale(st, i, &scale_val[0], &scale_val[1]);
  416. if (scale_val[0] != val || scale_val[1] != val2)
  417. continue;
  418. guard(mutex)(&st->lock);
  419. ret = ad9467_spi_write(st, AN877_ADC_REG_VREF,
  420. info->scale_table[i][1]);
  421. if (ret < 0)
  422. return ret;
  423. return ad9467_spi_write(st, AN877_ADC_REG_TRANSFER,
  424. AN877_ADC_TRANSFER_SYNC);
  425. }
  426. return -EINVAL;
  427. }
  428. static int ad9467_outputmode_set(struct ad9467_state *st, unsigned int mode)
  429. {
  430. int ret;
  431. ret = ad9467_spi_write(st, AN877_ADC_REG_OUTPUT_MODE, mode);
  432. if (ret < 0)
  433. return ret;
  434. return ad9467_spi_write(st, AN877_ADC_REG_TRANSFER,
  435. AN877_ADC_TRANSFER_SYNC);
  436. }
  437. static int ad9467_testmode_set(struct ad9467_state *st, unsigned int chan,
  438. unsigned int test_mode)
  439. {
  440. int ret;
  441. if (st->info->num_channels > 1) {
  442. /* so that the test mode is only applied to one channel */
  443. ret = ad9467_spi_write(st, AN877_ADC_REG_CHAN_INDEX, BIT(chan));
  444. if (ret)
  445. return ret;
  446. }
  447. ret = ad9467_spi_write(st, AN877_ADC_REG_TEST_IO, test_mode);
  448. if (ret)
  449. return ret;
  450. if (st->info->num_channels > 1) {
  451. /* go to default state where all channels get write commands */
  452. ret = ad9467_spi_write(st, AN877_ADC_REG_CHAN_INDEX,
  453. GENMASK(st->info->num_channels - 1, 0));
  454. if (ret)
  455. return ret;
  456. }
  457. return ad9467_spi_write(st, AN877_ADC_REG_TRANSFER,
  458. AN877_ADC_TRANSFER_SYNC);
  459. }
  460. static int ad9467_backend_testmode_on(struct ad9467_state *st,
  461. unsigned int chan,
  462. enum iio_backend_test_pattern pattern)
  463. {
  464. struct iio_backend_data_fmt data = {
  465. .enable = false,
  466. };
  467. int ret;
  468. ret = iio_backend_data_format_set(st->back, chan, &data);
  469. if (ret)
  470. return ret;
  471. ret = iio_backend_test_pattern_set(st->back, chan, pattern);
  472. if (ret)
  473. return ret;
  474. return iio_backend_chan_enable(st->back, chan);
  475. }
  476. static int ad9467_backend_testmode_off(struct ad9467_state *st,
  477. unsigned int chan)
  478. {
  479. struct iio_backend_data_fmt data = {
  480. .enable = true,
  481. .sign_extend = true,
  482. };
  483. int ret;
  484. ret = iio_backend_chan_disable(st->back, chan);
  485. if (ret)
  486. return ret;
  487. ret = iio_backend_test_pattern_set(st->back, chan,
  488. IIO_BACKEND_NO_TEST_PATTERN);
  489. if (ret)
  490. return ret;
  491. return iio_backend_data_format_set(st->back, chan, &data);
  492. }
  493. static int ad9647_calibrate_prepare(struct ad9467_state *st)
  494. {
  495. unsigned int c;
  496. int ret;
  497. ret = ad9467_outputmode_set(st, st->info->default_output_mode);
  498. if (ret)
  499. return ret;
  500. for (c = 0; c < st->info->num_channels; c++) {
  501. ret = ad9467_testmode_set(st, c, AN877_ADC_TESTMODE_PN9_SEQ);
  502. if (ret)
  503. return ret;
  504. ret = ad9467_backend_testmode_on(st, c,
  505. IIO_BACKEND_ADI_PRBS_9A);
  506. if (ret)
  507. return ret;
  508. }
  509. return 0;
  510. }
  511. static int ad9647_calibrate_polarity_set(struct ad9467_state *st,
  512. bool invert)
  513. {
  514. enum iio_backend_sample_trigger trigger;
  515. if (st->info->has_dco) {
  516. unsigned int phase = AN877_ADC_OUTPUT_EVEN_ODD_MODE_EN;
  517. if (invert)
  518. phase |= AN877_ADC_INVERT_DCO_CLK;
  519. return ad9467_spi_write(st, AN877_ADC_REG_OUTPUT_PHASE,
  520. phase);
  521. }
  522. if (invert)
  523. trigger = IIO_BACKEND_SAMPLE_TRIGGER_EDGE_FALLING;
  524. else
  525. trigger = IIO_BACKEND_SAMPLE_TRIGGER_EDGE_RISING;
  526. return iio_backend_data_sample_trigger(st->back, trigger);
  527. }
  528. /*
  529. * The idea is pretty simple. Find the max number of successful points in a row
  530. * and get the one in the middle.
  531. */
  532. static unsigned int ad9467_find_optimal_point(const unsigned long *calib_map,
  533. unsigned int start,
  534. unsigned int nbits,
  535. unsigned int *val)
  536. {
  537. unsigned int bit = start, end, start_cnt, cnt = 0;
  538. for_each_clear_bitrange_from(bit, end, calib_map, nbits + start) {
  539. if (end - bit > cnt) {
  540. cnt = end - bit;
  541. start_cnt = bit;
  542. }
  543. }
  544. if (cnt)
  545. *val = start_cnt + cnt / 2;
  546. return cnt;
  547. }
  548. static int ad9467_calibrate_apply(struct ad9467_state *st, unsigned int val)
  549. {
  550. unsigned int lane;
  551. int ret;
  552. if (st->info->has_dco) {
  553. ret = ad9467_spi_write(st, AN877_ADC_REG_OUTPUT_DELAY,
  554. val | st->info->dco_en);
  555. if (ret)
  556. return ret;
  557. return ad9467_spi_write(st, AN877_ADC_REG_TRANSFER,
  558. AN877_ADC_TRANSFER_SYNC);
  559. }
  560. for (lane = 0; lane < st->info->num_lanes; lane++) {
  561. ret = iio_backend_iodelay_set(st->back, lane, val);
  562. if (ret)
  563. return ret;
  564. }
  565. return 0;
  566. }
  567. static int ad9647_calibrate_stop(struct ad9467_state *st)
  568. {
  569. unsigned int c, mode;
  570. int ret;
  571. for (c = 0; c < st->info->num_channels; c++) {
  572. ret = ad9467_backend_testmode_off(st, c);
  573. if (ret)
  574. return ret;
  575. ret = ad9467_testmode_set(st, c, AN877_ADC_TESTMODE_OFF);
  576. if (ret)
  577. return ret;
  578. }
  579. mode = st->info->default_output_mode | AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT;
  580. return ad9467_outputmode_set(st, mode);
  581. }
  582. static int ad9467_calibrate(struct ad9467_state *st)
  583. {
  584. unsigned int point, val, inv_val, cnt, inv_cnt = 0, c;
  585. /*
  586. * Half of the bitmap is for the inverted signal. The number of test
  587. * points is the same though...
  588. */
  589. unsigned int test_points = st->info->test_points;
  590. unsigned long sample_rate = clk_get_rate(st->clk);
  591. struct device *dev = &st->spi->dev;
  592. bool invert = false, stat;
  593. int ret;
  594. /* all points invalid */
  595. bitmap_fill(st->calib_map, st->calib_map_size);
  596. ret = ad9647_calibrate_prepare(st);
  597. if (ret)
  598. return ret;
  599. retune:
  600. ret = ad9647_calibrate_polarity_set(st, invert);
  601. if (ret)
  602. return ret;
  603. for (point = 0; point < st->info->test_points; point++) {
  604. ret = ad9467_calibrate_apply(st, point);
  605. if (ret)
  606. return ret;
  607. for (c = 0; c < st->info->num_channels; c++) {
  608. ret = iio_backend_chan_status(st->back, c, &stat);
  609. if (ret)
  610. return ret;
  611. /*
  612. * A point is considered valid if all channels report no
  613. * error. If one reports an error, then we consider the
  614. * point as invalid and we can break the loop right away.
  615. */
  616. if (stat) {
  617. dev_dbg(dev, "Invalid point(%u, inv:%u) for CH:%u\n",
  618. point, invert, c);
  619. break;
  620. }
  621. if (c == st->info->num_channels - 1)
  622. __clear_bit(point + invert * test_points,
  623. st->calib_map);
  624. }
  625. }
  626. if (!invert) {
  627. cnt = ad9467_find_optimal_point(st->calib_map, 0, test_points,
  628. &val);
  629. /*
  630. * We're happy if we find, at least, three good test points in
  631. * a row.
  632. */
  633. if (cnt < 3) {
  634. if (AD9467_CAN_INVERT(st)) {
  635. invert = true;
  636. goto retune;
  637. }
  638. if (!cnt)
  639. return -EIO;
  640. }
  641. } else {
  642. inv_cnt = ad9467_find_optimal_point(st->calib_map, test_points,
  643. test_points, &inv_val);
  644. if (!inv_cnt && !cnt)
  645. return -EIO;
  646. }
  647. if (inv_cnt < cnt) {
  648. ret = ad9647_calibrate_polarity_set(st, false);
  649. if (ret)
  650. return ret;
  651. } else {
  652. /*
  653. * polarity inverted is the last test to run. Hence, there's no
  654. * need to re-do any configuration. We just need to "normalize"
  655. * the selected value.
  656. */
  657. val = inv_val - test_points;
  658. }
  659. if (st->info->has_dco)
  660. dev_dbg(dev, "%sDCO 0x%X CLK %lu Hz\n", inv_cnt >= cnt ? "INVERT " : "",
  661. val, sample_rate);
  662. else
  663. dev_dbg(dev, "%sIDELAY 0x%x\n", inv_cnt >= cnt ? "INVERT " : "",
  664. val);
  665. ret = ad9467_calibrate_apply(st, val);
  666. if (ret)
  667. return ret;
  668. /* finally apply the optimal value */
  669. return ad9647_calibrate_stop(st);
  670. }
  671. static int ad9467_read_raw(struct iio_dev *indio_dev,
  672. struct iio_chan_spec const *chan,
  673. int *val, int *val2, long m)
  674. {
  675. struct ad9467_state *st = iio_priv(indio_dev);
  676. switch (m) {
  677. case IIO_CHAN_INFO_SCALE:
  678. return ad9467_get_scale(st, val, val2);
  679. case IIO_CHAN_INFO_SAMP_FREQ:
  680. *val = clk_get_rate(st->clk);
  681. return IIO_VAL_INT;
  682. default:
  683. return -EINVAL;
  684. }
  685. }
  686. static int ad9467_write_raw(struct iio_dev *indio_dev,
  687. struct iio_chan_spec const *chan,
  688. int val, int val2, long mask)
  689. {
  690. struct ad9467_state *st = iio_priv(indio_dev);
  691. const struct ad9467_chip_info *info = st->info;
  692. unsigned long sample_rate;
  693. long r_clk;
  694. int ret;
  695. switch (mask) {
  696. case IIO_CHAN_INFO_SCALE:
  697. return ad9467_set_scale(st, val, val2);
  698. case IIO_CHAN_INFO_SAMP_FREQ:
  699. r_clk = clk_round_rate(st->clk, val);
  700. if (r_clk < 0 || r_clk > info->max_rate) {
  701. dev_warn(&st->spi->dev,
  702. "Error setting ADC sample rate %ld", r_clk);
  703. return -EINVAL;
  704. }
  705. sample_rate = clk_get_rate(st->clk);
  706. /*
  707. * clk_set_rate() would also do this but since we would still
  708. * need it for avoiding an unnecessary calibration, do it now.
  709. */
  710. if (sample_rate == r_clk)
  711. return 0;
  712. iio_device_claim_direct_scoped(return -EBUSY, indio_dev) {
  713. ret = clk_set_rate(st->clk, r_clk);
  714. if (ret)
  715. return ret;
  716. guard(mutex)(&st->lock);
  717. ret = ad9467_calibrate(st);
  718. }
  719. return ret;
  720. default:
  721. return -EINVAL;
  722. }
  723. }
  724. static int ad9467_read_avail(struct iio_dev *indio_dev,
  725. struct iio_chan_spec const *chan,
  726. const int **vals, int *type, int *length,
  727. long mask)
  728. {
  729. struct ad9467_state *st = iio_priv(indio_dev);
  730. const struct ad9467_chip_info *info = st->info;
  731. switch (mask) {
  732. case IIO_CHAN_INFO_SCALE:
  733. *vals = (const int *)st->scales;
  734. *type = IIO_VAL_INT_PLUS_MICRO;
  735. /* Values are stored in a 2D matrix */
  736. *length = info->num_scales * 2;
  737. return IIO_AVAIL_LIST;
  738. default:
  739. return -EINVAL;
  740. }
  741. }
  742. static int ad9467_update_scan_mode(struct iio_dev *indio_dev,
  743. const unsigned long *scan_mask)
  744. {
  745. struct ad9467_state *st = iio_priv(indio_dev);
  746. unsigned int c;
  747. int ret;
  748. for (c = 0; c < st->info->num_channels; c++) {
  749. if (test_bit(c, scan_mask))
  750. ret = iio_backend_chan_enable(st->back, c);
  751. else
  752. ret = iio_backend_chan_disable(st->back, c);
  753. if (ret)
  754. return ret;
  755. }
  756. return 0;
  757. }
  758. static struct iio_info ad9467_info = {
  759. .read_raw = ad9467_read_raw,
  760. .write_raw = ad9467_write_raw,
  761. .update_scan_mode = ad9467_update_scan_mode,
  762. .debugfs_reg_access = ad9467_reg_access,
  763. .read_avail = ad9467_read_avail,
  764. };
  765. static int ad9467_scale_fill(struct ad9467_state *st)
  766. {
  767. const struct ad9467_chip_info *info = st->info;
  768. unsigned int i, val1, val2;
  769. st->scales = devm_kmalloc_array(&st->spi->dev, info->num_scales,
  770. sizeof(*st->scales), GFP_KERNEL);
  771. if (!st->scales)
  772. return -ENOMEM;
  773. for (i = 0; i < info->num_scales; i++) {
  774. __ad9467_get_scale(st, i, &val1, &val2);
  775. st->scales[i][0] = val1;
  776. st->scales[i][1] = val2;
  777. }
  778. return 0;
  779. }
  780. static int ad9467_reset(struct device *dev)
  781. {
  782. struct gpio_desc *gpio;
  783. gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  784. if (IS_ERR_OR_NULL(gpio))
  785. return PTR_ERR_OR_ZERO(gpio);
  786. fsleep(1);
  787. gpiod_set_value_cansleep(gpio, 0);
  788. fsleep(10 * USEC_PER_MSEC);
  789. return 0;
  790. }
  791. static int ad9467_iio_backend_get(struct ad9467_state *st)
  792. {
  793. struct device *dev = &st->spi->dev;
  794. struct device_node *__back;
  795. st->back = devm_iio_backend_get(dev, NULL);
  796. if (!IS_ERR(st->back))
  797. return 0;
  798. /* If not found, don't error out as we might have legacy DT property */
  799. if (PTR_ERR(st->back) != -ENOENT)
  800. return PTR_ERR(st->back);
  801. /*
  802. * if we don't get the backend using the normal API's, use the legacy
  803. * 'adi,adc-dev' property. So we get all nodes with that property, and
  804. * look for the one pointing at us. Then we directly lookup that fwnode
  805. * on the backend list of registered devices. This is done so we don't
  806. * make io-backends mandatory which would break DT ABI.
  807. */
  808. for_each_node_with_property(__back, "adi,adc-dev") {
  809. struct device_node *__me;
  810. __me = of_parse_phandle(__back, "adi,adc-dev", 0);
  811. if (!__me)
  812. continue;
  813. if (!device_match_of_node(dev, __me)) {
  814. of_node_put(__me);
  815. continue;
  816. }
  817. of_node_put(__me);
  818. st->back = __devm_iio_backend_get_from_fwnode_lookup(dev,
  819. of_fwnode_handle(__back));
  820. of_node_put(__back);
  821. return PTR_ERR_OR_ZERO(st->back);
  822. }
  823. return -ENODEV;
  824. }
  825. static int ad9467_test_mode_available_show(struct seq_file *s, void *ignored)
  826. {
  827. struct ad9467_state *st = s->private;
  828. unsigned int bit;
  829. for_each_set_bit(bit, &st->info->test_mask, st->info->test_mask_len)
  830. seq_printf(s, "%s\n", ad9467_test_modes[bit]);
  831. return 0;
  832. }
  833. DEFINE_SHOW_ATTRIBUTE(ad9467_test_mode_available);
  834. static ssize_t ad9467_chan_test_mode_read(struct file *file,
  835. char __user *userbuf, size_t count,
  836. loff_t *ppos)
  837. {
  838. struct ad9467_chan_test_mode *chan = file->private_data;
  839. struct ad9467_state *st = chan->st;
  840. char buf[128] = {0};
  841. size_t len;
  842. int ret;
  843. if (chan->mode == AN877_ADC_TESTMODE_PN9_SEQ ||
  844. chan->mode == AN877_ADC_TESTMODE_PN23_SEQ) {
  845. len = scnprintf(buf, sizeof(buf), "Running \"%s\" Test:\n\t",
  846. ad9467_test_modes[chan->mode]);
  847. ret = iio_backend_debugfs_print_chan_status(st->back, chan->idx,
  848. buf + len,
  849. sizeof(buf) - len);
  850. if (ret < 0)
  851. return ret;
  852. len += ret;
  853. } else if (chan->mode == AN877_ADC_TESTMODE_OFF) {
  854. len = scnprintf(buf, sizeof(buf), "No test Running...\n");
  855. } else {
  856. len = scnprintf(buf, sizeof(buf), "Running \"%s\" Test on CH:%u\n",
  857. ad9467_test_modes[chan->mode], chan->idx);
  858. }
  859. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  860. }
  861. static ssize_t ad9467_chan_test_mode_write(struct file *file,
  862. const char __user *userbuf,
  863. size_t count, loff_t *ppos)
  864. {
  865. struct ad9467_chan_test_mode *chan = file->private_data;
  866. struct ad9467_state *st = chan->st;
  867. char test_mode[32] = {0};
  868. unsigned int mode;
  869. int ret;
  870. ret = simple_write_to_buffer(test_mode, sizeof(test_mode) - 1, ppos,
  871. userbuf, count);
  872. if (ret < 0)
  873. return ret;
  874. for_each_set_bit(mode, &st->info->test_mask, st->info->test_mask_len) {
  875. if (sysfs_streq(test_mode, ad9467_test_modes[mode]))
  876. break;
  877. }
  878. if (mode == st->info->test_mask_len)
  879. return -EINVAL;
  880. guard(mutex)(&st->lock);
  881. if (mode == AN877_ADC_TESTMODE_OFF) {
  882. unsigned int out_mode;
  883. if (chan->mode == AN877_ADC_TESTMODE_PN9_SEQ ||
  884. chan->mode == AN877_ADC_TESTMODE_PN23_SEQ) {
  885. ret = ad9467_backend_testmode_off(st, chan->idx);
  886. if (ret)
  887. return ret;
  888. }
  889. ret = ad9467_testmode_set(st, chan->idx, mode);
  890. if (ret)
  891. return ret;
  892. out_mode = st->info->default_output_mode | AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT;
  893. ret = ad9467_outputmode_set(st, out_mode);
  894. if (ret)
  895. return ret;
  896. } else {
  897. ret = ad9467_outputmode_set(st, st->info->default_output_mode);
  898. if (ret)
  899. return ret;
  900. ret = ad9467_testmode_set(st, chan->idx, mode);
  901. if (ret)
  902. return ret;
  903. /* some patterns have a backend matching monitoring block */
  904. if (mode == AN877_ADC_TESTMODE_PN9_SEQ) {
  905. ret = ad9467_backend_testmode_on(st, chan->idx,
  906. IIO_BACKEND_ADI_PRBS_9A);
  907. if (ret)
  908. return ret;
  909. } else if (mode == AN877_ADC_TESTMODE_PN23_SEQ) {
  910. ret = ad9467_backend_testmode_on(st, chan->idx,
  911. IIO_BACKEND_ADI_PRBS_23A);
  912. if (ret)
  913. return ret;
  914. }
  915. }
  916. chan->mode = mode;
  917. return count;
  918. }
  919. static const struct file_operations ad9467_chan_test_mode_fops = {
  920. .open = simple_open,
  921. .read = ad9467_chan_test_mode_read,
  922. .write = ad9467_chan_test_mode_write,
  923. .llseek = default_llseek,
  924. .owner = THIS_MODULE,
  925. };
  926. static ssize_t ad9467_dump_calib_table(struct file *file,
  927. char __user *userbuf,
  928. size_t count, loff_t *ppos)
  929. {
  930. struct ad9467_state *st = file->private_data;
  931. unsigned int bit;
  932. /* +2 for the newline and +1 for the string termination */
  933. unsigned char map[AD9647_MAX_TEST_POINTS * 2 + 3];
  934. ssize_t len = 0;
  935. guard(mutex)(&st->lock);
  936. if (*ppos)
  937. goto out_read;
  938. for (bit = 0; bit < st->calib_map_size; bit++) {
  939. if (AD9467_CAN_INVERT(st) && bit == st->calib_map_size / 2)
  940. len += scnprintf(map + len, sizeof(map) - len, "\n");
  941. len += scnprintf(map + len, sizeof(map) - len, "%c",
  942. test_bit(bit, st->calib_map) ? 'x' : 'o');
  943. }
  944. len += scnprintf(map + len, sizeof(map) - len, "\n");
  945. out_read:
  946. return simple_read_from_buffer(userbuf, count, ppos, map, len);
  947. }
  948. static const struct file_operations ad9467_calib_table_fops = {
  949. .open = simple_open,
  950. .read = ad9467_dump_calib_table,
  951. .llseek = default_llseek,
  952. .owner = THIS_MODULE,
  953. };
  954. static void ad9467_debugfs_init(struct iio_dev *indio_dev)
  955. {
  956. struct dentry *d = iio_get_debugfs_dentry(indio_dev);
  957. struct ad9467_state *st = iio_priv(indio_dev);
  958. char attr_name[32];
  959. unsigned int chan;
  960. if (!IS_ENABLED(CONFIG_DEBUG_FS))
  961. return;
  962. st->chan_test = devm_kcalloc(&st->spi->dev, st->info->num_channels,
  963. sizeof(*st->chan_test), GFP_KERNEL);
  964. if (!st->chan_test)
  965. return;
  966. debugfs_create_file("calibration_table_dump", 0400, d, st,
  967. &ad9467_calib_table_fops);
  968. for (chan = 0; chan < st->info->num_channels; chan++) {
  969. snprintf(attr_name, sizeof(attr_name), "in_voltage%u_test_mode",
  970. chan);
  971. st->chan_test[chan].idx = chan;
  972. st->chan_test[chan].st = st;
  973. debugfs_create_file(attr_name, 0600, d, &st->chan_test[chan],
  974. &ad9467_chan_test_mode_fops);
  975. }
  976. debugfs_create_file("in_voltage_test_mode_available", 0400, d, st,
  977. &ad9467_test_mode_available_fops);
  978. iio_backend_debugfs_add(st->back, indio_dev);
  979. }
  980. static int ad9467_probe(struct spi_device *spi)
  981. {
  982. struct iio_dev *indio_dev;
  983. struct ad9467_state *st;
  984. unsigned int id;
  985. int ret;
  986. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  987. if (!indio_dev)
  988. return -ENOMEM;
  989. st = iio_priv(indio_dev);
  990. st->spi = spi;
  991. st->info = spi_get_device_match_data(spi);
  992. if (!st->info)
  993. return -ENODEV;
  994. st->calib_map_size = st->info->test_points;
  995. if (AD9467_CAN_INVERT(st))
  996. st->calib_map_size *= 2;
  997. st->clk = devm_clk_get_enabled(&spi->dev, "adc-clk");
  998. if (IS_ERR(st->clk))
  999. return PTR_ERR(st->clk);
  1000. st->pwrdown_gpio = devm_gpiod_get_optional(&spi->dev, "powerdown",
  1001. GPIOD_OUT_LOW);
  1002. if (IS_ERR(st->pwrdown_gpio))
  1003. return PTR_ERR(st->pwrdown_gpio);
  1004. ret = ad9467_reset(&spi->dev);
  1005. if (ret)
  1006. return ret;
  1007. ret = ad9467_scale_fill(st);
  1008. if (ret)
  1009. return ret;
  1010. id = ad9467_spi_read(st, AN877_ADC_REG_CHIP_ID);
  1011. if (id != st->info->id) {
  1012. dev_err(&spi->dev, "Mismatch CHIP_ID, got 0x%X, expected 0x%X\n",
  1013. id, st->info->id);
  1014. return -ENODEV;
  1015. }
  1016. if (st->info->num_scales > 1)
  1017. ad9467_info.read_avail = ad9467_read_avail;
  1018. indio_dev->name = st->info->name;
  1019. indio_dev->channels = st->info->channels;
  1020. indio_dev->num_channels = st->info->num_channels;
  1021. indio_dev->info = &ad9467_info;
  1022. ret = ad9467_iio_backend_get(st);
  1023. if (ret)
  1024. return ret;
  1025. ret = devm_iio_backend_request_buffer(&spi->dev, st->back, indio_dev);
  1026. if (ret)
  1027. return ret;
  1028. ret = devm_iio_backend_enable(&spi->dev, st->back);
  1029. if (ret)
  1030. return ret;
  1031. ret = ad9467_calibrate(st);
  1032. if (ret)
  1033. return ret;
  1034. ret = devm_iio_device_register(&spi->dev, indio_dev);
  1035. if (ret)
  1036. return ret;
  1037. ad9467_debugfs_init(indio_dev);
  1038. return 0;
  1039. }
  1040. static const struct of_device_id ad9467_of_match[] = {
  1041. { .compatible = "adi,ad9265", .data = &ad9265_chip_tbl, },
  1042. { .compatible = "adi,ad9434", .data = &ad9434_chip_tbl, },
  1043. { .compatible = "adi,ad9467", .data = &ad9467_chip_tbl, },
  1044. { .compatible = "adi,ad9643", .data = &ad9643_chip_tbl, },
  1045. { .compatible = "adi,ad9649", .data = &ad9649_chip_tbl, },
  1046. { .compatible = "adi,ad9652", .data = &ad9652_chip_tbl, },
  1047. { }
  1048. };
  1049. MODULE_DEVICE_TABLE(of, ad9467_of_match);
  1050. static const struct spi_device_id ad9467_ids[] = {
  1051. { "ad9265", (kernel_ulong_t)&ad9265_chip_tbl },
  1052. { "ad9434", (kernel_ulong_t)&ad9434_chip_tbl },
  1053. { "ad9467", (kernel_ulong_t)&ad9467_chip_tbl },
  1054. { "ad9643", (kernel_ulong_t)&ad9643_chip_tbl },
  1055. { "ad9649", (kernel_ulong_t)&ad9649_chip_tbl, },
  1056. { "ad9652", (kernel_ulong_t)&ad9652_chip_tbl, },
  1057. { }
  1058. };
  1059. MODULE_DEVICE_TABLE(spi, ad9467_ids);
  1060. static struct spi_driver ad9467_driver = {
  1061. .driver = {
  1062. .name = "ad9467",
  1063. .of_match_table = ad9467_of_match,
  1064. },
  1065. .probe = ad9467_probe,
  1066. .id_table = ad9467_ids,
  1067. };
  1068. module_spi_driver(ad9467_driver);
  1069. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  1070. MODULE_DESCRIPTION("Analog Devices AD9467 ADC driver");
  1071. MODULE_LICENSE("GPL v2");
  1072. MODULE_IMPORT_NS(IIO_BACKEND);