ad9523.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * AD9523 SPI Low Jitter Clock Generator
  3. *
  4. * Copyright 2012 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/sysfs.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/err.h>
  16. #include <linux/module.h>
  17. #include <linux/delay.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include <linux/iio/frequency/ad9523.h>
  21. #define AD9523_READ (1 << 15)
  22. #define AD9523_WRITE (0 << 15)
  23. #define AD9523_CNT(x) (((x) - 1) << 13)
  24. #define AD9523_ADDR(x) ((x) & 0xFFF)
  25. #define AD9523_R1B (1 << 16)
  26. #define AD9523_R2B (2 << 16)
  27. #define AD9523_R3B (3 << 16)
  28. #define AD9523_TRANSF_LEN(x) ((x) >> 16)
  29. #define AD9523_SERIAL_PORT_CONFIG (AD9523_R1B | 0x0)
  30. #define AD9523_VERSION_REGISTER (AD9523_R1B | 0x2)
  31. #define AD9523_PART_REGISTER (AD9523_R1B | 0x3)
  32. #define AD9523_READBACK_CTRL (AD9523_R1B | 0x4)
  33. #define AD9523_EEPROM_CUSTOMER_VERSION_ID (AD9523_R2B | 0x6)
  34. #define AD9523_PLL1_REF_A_DIVIDER (AD9523_R2B | 0x11)
  35. #define AD9523_PLL1_REF_B_DIVIDER (AD9523_R2B | 0x13)
  36. #define AD9523_PLL1_REF_TEST_DIVIDER (AD9523_R1B | 0x14)
  37. #define AD9523_PLL1_FEEDBACK_DIVIDER (AD9523_R2B | 0x17)
  38. #define AD9523_PLL1_CHARGE_PUMP_CTRL (AD9523_R2B | 0x19)
  39. #define AD9523_PLL1_INPUT_RECEIVERS_CTRL (AD9523_R1B | 0x1A)
  40. #define AD9523_PLL1_REF_CTRL (AD9523_R1B | 0x1B)
  41. #define AD9523_PLL1_MISC_CTRL (AD9523_R1B | 0x1C)
  42. #define AD9523_PLL1_LOOP_FILTER_CTRL (AD9523_R1B | 0x1D)
  43. #define AD9523_PLL2_CHARGE_PUMP (AD9523_R1B | 0xF0)
  44. #define AD9523_PLL2_FEEDBACK_DIVIDER_AB (AD9523_R1B | 0xF1)
  45. #define AD9523_PLL2_CTRL (AD9523_R1B | 0xF2)
  46. #define AD9523_PLL2_VCO_CTRL (AD9523_R1B | 0xF3)
  47. #define AD9523_PLL2_VCO_DIVIDER (AD9523_R1B | 0xF4)
  48. #define AD9523_PLL2_LOOP_FILTER_CTRL (AD9523_R2B | 0xF6)
  49. #define AD9523_PLL2_R2_DIVIDER (AD9523_R1B | 0xF7)
  50. #define AD9523_CHANNEL_CLOCK_DIST(ch) (AD9523_R3B | (0x192 + 3 * ch))
  51. #define AD9523_PLL1_OUTPUT_CTRL (AD9523_R1B | 0x1BA)
  52. #define AD9523_PLL1_OUTPUT_CHANNEL_CTRL (AD9523_R1B | 0x1BB)
  53. #define AD9523_READBACK_0 (AD9523_R1B | 0x22C)
  54. #define AD9523_READBACK_1 (AD9523_R1B | 0x22D)
  55. #define AD9523_STATUS_SIGNALS (AD9523_R3B | 0x232)
  56. #define AD9523_POWER_DOWN_CTRL (AD9523_R1B | 0x233)
  57. #define AD9523_IO_UPDATE (AD9523_R1B | 0x234)
  58. #define AD9523_EEPROM_DATA_XFER_STATUS (AD9523_R1B | 0xB00)
  59. #define AD9523_EEPROM_ERROR_READBACK (AD9523_R1B | 0xB01)
  60. #define AD9523_EEPROM_CTRL1 (AD9523_R1B | 0xB02)
  61. #define AD9523_EEPROM_CTRL2 (AD9523_R1B | 0xB03)
  62. /* AD9523_SERIAL_PORT_CONFIG */
  63. #define AD9523_SER_CONF_SDO_ACTIVE (1 << 7)
  64. #define AD9523_SER_CONF_SOFT_RESET (1 << 5)
  65. /* AD9523_READBACK_CTRL */
  66. #define AD9523_READBACK_CTRL_READ_BUFFERED (1 << 0)
  67. /* AD9523_PLL1_CHARGE_PUMP_CTRL */
  68. #define AD9523_PLL1_CHARGE_PUMP_CURRENT_nA(x) (((x) / 500) & 0x7F)
  69. #define AD9523_PLL1_CHARGE_PUMP_TRISTATE (1 << 7)
  70. #define AD9523_PLL1_CHARGE_PUMP_MODE_NORMAL (3 << 8)
  71. #define AD9523_PLL1_CHARGE_PUMP_MODE_PUMP_DOWN (2 << 8)
  72. #define AD9523_PLL1_CHARGE_PUMP_MODE_PUMP_UP (1 << 8)
  73. #define AD9523_PLL1_CHARGE_PUMP_MODE_TRISTATE (0 << 8)
  74. #define AD9523_PLL1_BACKLASH_PW_MIN (0 << 10)
  75. #define AD9523_PLL1_BACKLASH_PW_LOW (1 << 10)
  76. #define AD9523_PLL1_BACKLASH_PW_HIGH (2 << 10)
  77. #define AD9523_PLL1_BACKLASH_PW_MAX (3 << 10)
  78. /* AD9523_PLL1_INPUT_RECEIVERS_CTRL */
  79. #define AD9523_PLL1_REF_TEST_RCV_EN (1 << 7)
  80. #define AD9523_PLL1_REFB_DIFF_RCV_EN (1 << 6)
  81. #define AD9523_PLL1_REFA_DIFF_RCV_EN (1 << 5)
  82. #define AD9523_PLL1_REFB_RCV_EN (1 << 4)
  83. #define AD9523_PLL1_REFA_RCV_EN (1 << 3)
  84. #define AD9523_PLL1_REFA_REFB_PWR_CTRL_EN (1 << 2)
  85. #define AD9523_PLL1_OSC_IN_CMOS_NEG_INP_EN (1 << 1)
  86. #define AD9523_PLL1_OSC_IN_DIFF_EN (1 << 0)
  87. /* AD9523_PLL1_REF_CTRL */
  88. #define AD9523_PLL1_BYPASS_REF_TEST_DIV_EN (1 << 7)
  89. #define AD9523_PLL1_BYPASS_FEEDBACK_DIV_EN (1 << 6)
  90. #define AD9523_PLL1_ZERO_DELAY_MODE_INT (1 << 5)
  91. #define AD9523_PLL1_ZERO_DELAY_MODE_EXT (0 << 5)
  92. #define AD9523_PLL1_OSC_IN_PLL_FEEDBACK_EN (1 << 4)
  93. #define AD9523_PLL1_ZD_IN_CMOS_NEG_INP_EN (1 << 3)
  94. #define AD9523_PLL1_ZD_IN_DIFF_EN (1 << 2)
  95. #define AD9523_PLL1_REFB_CMOS_NEG_INP_EN (1 << 1)
  96. #define AD9523_PLL1_REFA_CMOS_NEG_INP_EN (1 << 0)
  97. /* AD9523_PLL1_MISC_CTRL */
  98. #define AD9523_PLL1_REFB_INDEP_DIV_CTRL_EN (1 << 7)
  99. #define AD9523_PLL1_OSC_CTRL_FAIL_VCC_BY2_EN (1 << 6)
  100. #define AD9523_PLL1_REF_MODE(x) ((x) << 2)
  101. #define AD9523_PLL1_BYPASS_REFB_DIV (1 << 1)
  102. #define AD9523_PLL1_BYPASS_REFA_DIV (1 << 0)
  103. /* AD9523_PLL1_LOOP_FILTER_CTRL */
  104. #define AD9523_PLL1_LOOP_FILTER_RZERO(x) ((x) & 0xF)
  105. /* AD9523_PLL2_CHARGE_PUMP */
  106. #define AD9523_PLL2_CHARGE_PUMP_CURRENT_nA(x) ((x) / 3500)
  107. /* AD9523_PLL2_FEEDBACK_DIVIDER_AB */
  108. #define AD9523_PLL2_FB_NDIV_A_CNT(x) (((x) & 0x3) << 6)
  109. #define AD9523_PLL2_FB_NDIV_B_CNT(x) (((x) & 0x3F) << 0)
  110. #define AD9523_PLL2_FB_NDIV(a, b) (4 * (b) + (a))
  111. /* AD9523_PLL2_CTRL */
  112. #define AD9523_PLL2_CHARGE_PUMP_MODE_NORMAL (3 << 0)
  113. #define AD9523_PLL2_CHARGE_PUMP_MODE_PUMP_DOWN (2 << 0)
  114. #define AD9523_PLL2_CHARGE_PUMP_MODE_PUMP_UP (1 << 0)
  115. #define AD9523_PLL2_CHARGE_PUMP_MODE_TRISTATE (0 << 0)
  116. #define AD9523_PLL2_BACKLASH_PW_MIN (0 << 2)
  117. #define AD9523_PLL2_BACKLASH_PW_LOW (1 << 2)
  118. #define AD9523_PLL2_BACKLASH_PW_HIGH (2 << 2)
  119. #define AD9523_PLL2_BACKLASH_PW_MAX (3 << 1)
  120. #define AD9523_PLL2_BACKLASH_CTRL_EN (1 << 4)
  121. #define AD9523_PLL2_FREQ_DOUBLER_EN (1 << 5)
  122. #define AD9523_PLL2_LOCK_DETECT_PWR_DOWN_EN (1 << 7)
  123. /* AD9523_PLL2_VCO_CTRL */
  124. #define AD9523_PLL2_VCO_CALIBRATE (1 << 1)
  125. #define AD9523_PLL2_FORCE_VCO_MIDSCALE (1 << 2)
  126. #define AD9523_PLL2_FORCE_REFERENCE_VALID (1 << 3)
  127. #define AD9523_PLL2_FORCE_RELEASE_SYNC (1 << 4)
  128. /* AD9523_PLL2_VCO_DIVIDER */
  129. #define AD9523_PLL2_VCO_DIV_M1(x) ((((x) - 3) & 0x3) << 0)
  130. #define AD9523_PLL2_VCO_DIV_M2(x) ((((x) - 3) & 0x3) << 4)
  131. #define AD9523_PLL2_VCO_DIV_M1_PWR_DOWN_EN (1 << 2)
  132. #define AD9523_PLL2_VCO_DIV_M2_PWR_DOWN_EN (1 << 6)
  133. /* AD9523_PLL2_LOOP_FILTER_CTRL */
  134. #define AD9523_PLL2_LOOP_FILTER_CPOLE1(x) (((x) & 0x7) << 0)
  135. #define AD9523_PLL2_LOOP_FILTER_RZERO(x) (((x) & 0x7) << 3)
  136. #define AD9523_PLL2_LOOP_FILTER_RPOLE2(x) (((x) & 0x7) << 6)
  137. #define AD9523_PLL2_LOOP_FILTER_RZERO_BYPASS_EN (1 << 8)
  138. /* AD9523_PLL2_R2_DIVIDER */
  139. #define AD9523_PLL2_R2_DIVIDER_VAL(x) (((x) & 0x1F) << 0)
  140. /* AD9523_CHANNEL_CLOCK_DIST */
  141. #define AD9523_CLK_DIST_DIV_PHASE(x) (((x) & 0x3F) << 18)
  142. #define AD9523_CLK_DIST_DIV_PHASE_REV(x) ((ret >> 18) & 0x3F)
  143. #define AD9523_CLK_DIST_DIV(x) ((((x) - 1) & 0x3FF) << 8)
  144. #define AD9523_CLK_DIST_DIV_REV(x) (((ret >> 8) & 0x3FF) + 1)
  145. #define AD9523_CLK_DIST_INV_DIV_OUTPUT_EN (1 << 7)
  146. #define AD9523_CLK_DIST_IGNORE_SYNC_EN (1 << 6)
  147. #define AD9523_CLK_DIST_PWR_DOWN_EN (1 << 5)
  148. #define AD9523_CLK_DIST_LOW_PWR_MODE_EN (1 << 4)
  149. #define AD9523_CLK_DIST_DRIVER_MODE(x) (((x) & 0xF) << 0)
  150. /* AD9523_PLL1_OUTPUT_CTRL */
  151. #define AD9523_PLL1_OUTP_CTRL_VCO_DIV_SEL_CH6_M2 (1 << 7)
  152. #define AD9523_PLL1_OUTP_CTRL_VCO_DIV_SEL_CH5_M2 (1 << 6)
  153. #define AD9523_PLL1_OUTP_CTRL_VCO_DIV_SEL_CH4_M2 (1 << 5)
  154. #define AD9523_PLL1_OUTP_CTRL_CMOS_DRV_WEAK (1 << 4)
  155. #define AD9523_PLL1_OUTP_CTRL_OUTPUT_DIV_1 (0 << 0)
  156. #define AD9523_PLL1_OUTP_CTRL_OUTPUT_DIV_2 (1 << 0)
  157. #define AD9523_PLL1_OUTP_CTRL_OUTPUT_DIV_4 (2 << 0)
  158. #define AD9523_PLL1_OUTP_CTRL_OUTPUT_DIV_8 (4 << 0)
  159. #define AD9523_PLL1_OUTP_CTRL_OUTPUT_DIV_16 (8 << 0)
  160. /* AD9523_PLL1_OUTPUT_CHANNEL_CTRL */
  161. #define AD9523_PLL1_OUTP_CH_CTRL_OUTPUT_PWR_DOWN_EN (1 << 7)
  162. #define AD9523_PLL1_OUTP_CH_CTRL_VCO_DIV_SEL_CH9_M2 (1 << 6)
  163. #define AD9523_PLL1_OUTP_CH_CTRL_VCO_DIV_SEL_CH8_M2 (1 << 5)
  164. #define AD9523_PLL1_OUTP_CH_CTRL_VCO_DIV_SEL_CH7_M2 (1 << 4)
  165. #define AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH3 (1 << 3)
  166. #define AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH2 (1 << 2)
  167. #define AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH1 (1 << 1)
  168. #define AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH0 (1 << 0)
  169. /* AD9523_READBACK_0 */
  170. #define AD9523_READBACK_0_STAT_PLL2_REF_CLK (1 << 7)
  171. #define AD9523_READBACK_0_STAT_PLL2_FB_CLK (1 << 6)
  172. #define AD9523_READBACK_0_STAT_VCXO (1 << 5)
  173. #define AD9523_READBACK_0_STAT_REF_TEST (1 << 4)
  174. #define AD9523_READBACK_0_STAT_REFB (1 << 3)
  175. #define AD9523_READBACK_0_STAT_REFA (1 << 2)
  176. #define AD9523_READBACK_0_STAT_PLL2_LD (1 << 1)
  177. #define AD9523_READBACK_0_STAT_PLL1_LD (1 << 0)
  178. /* AD9523_READBACK_1 */
  179. #define AD9523_READBACK_1_HOLDOVER_ACTIVE (1 << 3)
  180. #define AD9523_READBACK_1_AUTOMODE_SEL_REFB (1 << 2)
  181. #define AD9523_READBACK_1_VCO_CALIB_IN_PROGRESS (1 << 0)
  182. /* AD9523_STATUS_SIGNALS */
  183. #define AD9523_STATUS_SIGNALS_SYNC_MAN_CTRL (1 << 16)
  184. #define AD9523_STATUS_MONITOR_01_PLL12_LOCKED (0x302)
  185. /* AD9523_POWER_DOWN_CTRL */
  186. #define AD9523_POWER_DOWN_CTRL_PLL1_PWR_DOWN (1 << 2)
  187. #define AD9523_POWER_DOWN_CTRL_PLL2_PWR_DOWN (1 << 1)
  188. #define AD9523_POWER_DOWN_CTRL_DIST_PWR_DOWN (1 << 0)
  189. /* AD9523_IO_UPDATE */
  190. #define AD9523_IO_UPDATE_EN (1 << 0)
  191. /* AD9523_EEPROM_DATA_XFER_STATUS */
  192. #define AD9523_EEPROM_DATA_XFER_IN_PROGRESS (1 << 0)
  193. /* AD9523_EEPROM_ERROR_READBACK */
  194. #define AD9523_EEPROM_ERROR_READBACK_FAIL (1 << 0)
  195. /* AD9523_EEPROM_CTRL1 */
  196. #define AD9523_EEPROM_CTRL1_SOFT_EEPROM (1 << 1)
  197. #define AD9523_EEPROM_CTRL1_EEPROM_WRITE_PROT_DIS (1 << 0)
  198. /* AD9523_EEPROM_CTRL2 */
  199. #define AD9523_EEPROM_CTRL2_REG2EEPROM (1 << 0)
  200. #define AD9523_NUM_CHAN 14
  201. #define AD9523_NUM_CHAN_ALT_CLK_SRC 10
  202. /* Helpers to avoid excess line breaks */
  203. #define AD_IFE(_pde, _a, _b) ((pdata->_pde) ? _a : _b)
  204. #define AD_IF(_pde, _a) AD_IFE(_pde, _a, 0)
  205. enum {
  206. AD9523_STAT_PLL1_LD,
  207. AD9523_STAT_PLL2_LD,
  208. AD9523_STAT_REFA,
  209. AD9523_STAT_REFB,
  210. AD9523_STAT_REF_TEST,
  211. AD9523_STAT_VCXO,
  212. AD9523_STAT_PLL2_FB_CLK,
  213. AD9523_STAT_PLL2_REF_CLK,
  214. AD9523_SYNC,
  215. AD9523_EEPROM,
  216. };
  217. enum {
  218. AD9523_VCO1,
  219. AD9523_VCO2,
  220. AD9523_VCXO,
  221. AD9523_NUM_CLK_SRC,
  222. };
  223. struct ad9523_state {
  224. struct spi_device *spi;
  225. struct regulator *reg;
  226. struct ad9523_platform_data *pdata;
  227. struct iio_chan_spec ad9523_channels[AD9523_NUM_CHAN];
  228. struct gpio_desc *pwrdown_gpio;
  229. struct gpio_desc *reset_gpio;
  230. struct gpio_desc *sync_gpio;
  231. unsigned long vcxo_freq;
  232. unsigned long vco_freq;
  233. unsigned long vco_out_freq[AD9523_NUM_CLK_SRC];
  234. unsigned char vco_out_map[AD9523_NUM_CHAN_ALT_CLK_SRC];
  235. /*
  236. * Lock for accessing device registers. Some operations require
  237. * multiple consecutive R/W operations, during which the device
  238. * shouldn't be interrupted. The buffers are also shared across
  239. * all operations so need to be protected on stand alone reads and
  240. * writes.
  241. */
  242. struct mutex lock;
  243. /*
  244. * DMA (thus cache coherency maintenance) requires the
  245. * transfer buffers to live in their own cache lines.
  246. */
  247. union {
  248. __be32 d32;
  249. u8 d8[4];
  250. } data[2] ____cacheline_aligned;
  251. };
  252. static int ad9523_read(struct iio_dev *indio_dev, unsigned int addr)
  253. {
  254. struct ad9523_state *st = iio_priv(indio_dev);
  255. int ret;
  256. /* We encode the register size 1..3 bytes into the register address.
  257. * On transfer we get the size from the register datum, and make sure
  258. * the result is properly aligned.
  259. */
  260. struct spi_transfer t[] = {
  261. {
  262. .tx_buf = &st->data[0].d8[2],
  263. .len = 2,
  264. }, {
  265. .rx_buf = &st->data[1].d8[4 - AD9523_TRANSF_LEN(addr)],
  266. .len = AD9523_TRANSF_LEN(addr),
  267. },
  268. };
  269. st->data[0].d32 = cpu_to_be32(AD9523_READ |
  270. AD9523_CNT(AD9523_TRANSF_LEN(addr)) |
  271. AD9523_ADDR(addr));
  272. ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
  273. if (ret < 0)
  274. dev_err(&indio_dev->dev, "read failed (%d)", ret);
  275. else
  276. ret = be32_to_cpu(st->data[1].d32) & (0xFFFFFF >>
  277. (8 * (3 - AD9523_TRANSF_LEN(addr))));
  278. return ret;
  279. };
  280. static int ad9523_write(struct iio_dev *indio_dev,
  281. unsigned int addr, unsigned int val)
  282. {
  283. struct ad9523_state *st = iio_priv(indio_dev);
  284. int ret;
  285. struct spi_transfer t[] = {
  286. {
  287. .tx_buf = &st->data[0].d8[2],
  288. .len = 2,
  289. }, {
  290. .tx_buf = &st->data[1].d8[4 - AD9523_TRANSF_LEN(addr)],
  291. .len = AD9523_TRANSF_LEN(addr),
  292. },
  293. };
  294. st->data[0].d32 = cpu_to_be32(AD9523_WRITE |
  295. AD9523_CNT(AD9523_TRANSF_LEN(addr)) |
  296. AD9523_ADDR(addr));
  297. st->data[1].d32 = cpu_to_be32(val);
  298. ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t));
  299. if (ret < 0)
  300. dev_err(&indio_dev->dev, "write failed (%d)", ret);
  301. return ret;
  302. }
  303. static int ad9523_io_update(struct iio_dev *indio_dev)
  304. {
  305. return ad9523_write(indio_dev, AD9523_IO_UPDATE, AD9523_IO_UPDATE_EN);
  306. }
  307. static int ad9523_vco_out_map(struct iio_dev *indio_dev,
  308. unsigned int ch, unsigned int out)
  309. {
  310. struct ad9523_state *st = iio_priv(indio_dev);
  311. int ret;
  312. unsigned int mask;
  313. switch (ch) {
  314. case 0 ... 3:
  315. ret = ad9523_read(indio_dev, AD9523_PLL1_OUTPUT_CHANNEL_CTRL);
  316. if (ret < 0)
  317. break;
  318. mask = AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH0 << ch;
  319. if (out) {
  320. ret |= mask;
  321. out = 2;
  322. } else {
  323. ret &= ~mask;
  324. }
  325. ret = ad9523_write(indio_dev,
  326. AD9523_PLL1_OUTPUT_CHANNEL_CTRL, ret);
  327. break;
  328. case 4 ... 6:
  329. ret = ad9523_read(indio_dev, AD9523_PLL1_OUTPUT_CTRL);
  330. if (ret < 0)
  331. break;
  332. mask = AD9523_PLL1_OUTP_CTRL_VCO_DIV_SEL_CH4_M2 << (ch - 4);
  333. if (out)
  334. ret |= mask;
  335. else
  336. ret &= ~mask;
  337. ret = ad9523_write(indio_dev, AD9523_PLL1_OUTPUT_CTRL, ret);
  338. break;
  339. case 7 ... 9:
  340. ret = ad9523_read(indio_dev, AD9523_PLL1_OUTPUT_CHANNEL_CTRL);
  341. if (ret < 0)
  342. break;
  343. mask = AD9523_PLL1_OUTP_CH_CTRL_VCO_DIV_SEL_CH7_M2 << (ch - 7);
  344. if (out)
  345. ret |= mask;
  346. else
  347. ret &= ~mask;
  348. ret = ad9523_write(indio_dev,
  349. AD9523_PLL1_OUTPUT_CHANNEL_CTRL, ret);
  350. break;
  351. default:
  352. return 0;
  353. }
  354. st->vco_out_map[ch] = out;
  355. return ret;
  356. }
  357. static int ad9523_set_clock_provider(struct iio_dev *indio_dev,
  358. unsigned int ch, unsigned long freq)
  359. {
  360. struct ad9523_state *st = iio_priv(indio_dev);
  361. long tmp1, tmp2;
  362. bool use_alt_clk_src;
  363. switch (ch) {
  364. case 0 ... 3:
  365. use_alt_clk_src = (freq == st->vco_out_freq[AD9523_VCXO]);
  366. break;
  367. case 4 ... 9:
  368. tmp1 = st->vco_out_freq[AD9523_VCO1] / freq;
  369. tmp2 = st->vco_out_freq[AD9523_VCO2] / freq;
  370. tmp1 *= freq;
  371. tmp2 *= freq;
  372. use_alt_clk_src = (abs(tmp1 - freq) > abs(tmp2 - freq));
  373. break;
  374. default:
  375. /* Ch 10..14: No action required, return success */
  376. return 0;
  377. }
  378. return ad9523_vco_out_map(indio_dev, ch, use_alt_clk_src);
  379. }
  380. static int ad9523_store_eeprom(struct iio_dev *indio_dev)
  381. {
  382. int ret, tmp;
  383. ret = ad9523_write(indio_dev, AD9523_EEPROM_CTRL1,
  384. AD9523_EEPROM_CTRL1_EEPROM_WRITE_PROT_DIS);
  385. if (ret < 0)
  386. return ret;
  387. ret = ad9523_write(indio_dev, AD9523_EEPROM_CTRL2,
  388. AD9523_EEPROM_CTRL2_REG2EEPROM);
  389. if (ret < 0)
  390. return ret;
  391. tmp = 4;
  392. do {
  393. msleep(20);
  394. ret = ad9523_read(indio_dev,
  395. AD9523_EEPROM_DATA_XFER_STATUS);
  396. if (ret < 0)
  397. return ret;
  398. } while ((ret & AD9523_EEPROM_DATA_XFER_IN_PROGRESS) && tmp--);
  399. ret = ad9523_write(indio_dev, AD9523_EEPROM_CTRL1, 0);
  400. if (ret < 0)
  401. return ret;
  402. ret = ad9523_read(indio_dev, AD9523_EEPROM_ERROR_READBACK);
  403. if (ret < 0)
  404. return ret;
  405. if (ret & AD9523_EEPROM_ERROR_READBACK_FAIL) {
  406. dev_err(&indio_dev->dev, "Verify EEPROM failed");
  407. ret = -EIO;
  408. }
  409. return ret;
  410. }
  411. static int ad9523_sync(struct iio_dev *indio_dev)
  412. {
  413. int ret, tmp;
  414. ret = ad9523_read(indio_dev, AD9523_STATUS_SIGNALS);
  415. if (ret < 0)
  416. return ret;
  417. tmp = ret;
  418. tmp |= AD9523_STATUS_SIGNALS_SYNC_MAN_CTRL;
  419. ret = ad9523_write(indio_dev, AD9523_STATUS_SIGNALS, tmp);
  420. if (ret < 0)
  421. return ret;
  422. ad9523_io_update(indio_dev);
  423. tmp &= ~AD9523_STATUS_SIGNALS_SYNC_MAN_CTRL;
  424. ret = ad9523_write(indio_dev, AD9523_STATUS_SIGNALS, tmp);
  425. if (ret < 0)
  426. return ret;
  427. return ad9523_io_update(indio_dev);
  428. }
  429. static ssize_t ad9523_store(struct device *dev,
  430. struct device_attribute *attr,
  431. const char *buf, size_t len)
  432. {
  433. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  434. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  435. struct ad9523_state *st = iio_priv(indio_dev);
  436. bool state;
  437. int ret;
  438. ret = strtobool(buf, &state);
  439. if (ret < 0)
  440. return ret;
  441. if (!state)
  442. return len;
  443. mutex_lock(&st->lock);
  444. switch ((u32)this_attr->address) {
  445. case AD9523_SYNC:
  446. ret = ad9523_sync(indio_dev);
  447. break;
  448. case AD9523_EEPROM:
  449. ret = ad9523_store_eeprom(indio_dev);
  450. break;
  451. default:
  452. ret = -ENODEV;
  453. }
  454. mutex_unlock(&st->lock);
  455. return ret ? ret : len;
  456. }
  457. static ssize_t ad9523_show(struct device *dev,
  458. struct device_attribute *attr,
  459. char *buf)
  460. {
  461. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  462. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  463. struct ad9523_state *st = iio_priv(indio_dev);
  464. int ret;
  465. mutex_lock(&st->lock);
  466. ret = ad9523_read(indio_dev, AD9523_READBACK_0);
  467. if (ret >= 0) {
  468. ret = sprintf(buf, "%d\n", !!(ret & (1 <<
  469. (u32)this_attr->address)));
  470. }
  471. mutex_unlock(&st->lock);
  472. return ret;
  473. }
  474. static IIO_DEVICE_ATTR(pll1_locked, S_IRUGO,
  475. ad9523_show,
  476. NULL,
  477. AD9523_STAT_PLL1_LD);
  478. static IIO_DEVICE_ATTR(pll2_locked, S_IRUGO,
  479. ad9523_show,
  480. NULL,
  481. AD9523_STAT_PLL2_LD);
  482. static IIO_DEVICE_ATTR(pll1_reference_clk_a_present, S_IRUGO,
  483. ad9523_show,
  484. NULL,
  485. AD9523_STAT_REFA);
  486. static IIO_DEVICE_ATTR(pll1_reference_clk_b_present, S_IRUGO,
  487. ad9523_show,
  488. NULL,
  489. AD9523_STAT_REFB);
  490. static IIO_DEVICE_ATTR(pll1_reference_clk_test_present, S_IRUGO,
  491. ad9523_show,
  492. NULL,
  493. AD9523_STAT_REF_TEST);
  494. static IIO_DEVICE_ATTR(vcxo_clk_present, S_IRUGO,
  495. ad9523_show,
  496. NULL,
  497. AD9523_STAT_VCXO);
  498. static IIO_DEVICE_ATTR(pll2_feedback_clk_present, S_IRUGO,
  499. ad9523_show,
  500. NULL,
  501. AD9523_STAT_PLL2_FB_CLK);
  502. static IIO_DEVICE_ATTR(pll2_reference_clk_present, S_IRUGO,
  503. ad9523_show,
  504. NULL,
  505. AD9523_STAT_PLL2_REF_CLK);
  506. static IIO_DEVICE_ATTR(sync_dividers, S_IWUSR,
  507. NULL,
  508. ad9523_store,
  509. AD9523_SYNC);
  510. static IIO_DEVICE_ATTR(store_eeprom, S_IWUSR,
  511. NULL,
  512. ad9523_store,
  513. AD9523_EEPROM);
  514. static struct attribute *ad9523_attributes[] = {
  515. &iio_dev_attr_sync_dividers.dev_attr.attr,
  516. &iio_dev_attr_store_eeprom.dev_attr.attr,
  517. &iio_dev_attr_pll2_feedback_clk_present.dev_attr.attr,
  518. &iio_dev_attr_pll2_reference_clk_present.dev_attr.attr,
  519. &iio_dev_attr_pll1_reference_clk_a_present.dev_attr.attr,
  520. &iio_dev_attr_pll1_reference_clk_b_present.dev_attr.attr,
  521. &iio_dev_attr_pll1_reference_clk_test_present.dev_attr.attr,
  522. &iio_dev_attr_vcxo_clk_present.dev_attr.attr,
  523. &iio_dev_attr_pll1_locked.dev_attr.attr,
  524. &iio_dev_attr_pll2_locked.dev_attr.attr,
  525. NULL,
  526. };
  527. static const struct attribute_group ad9523_attribute_group = {
  528. .attrs = ad9523_attributes,
  529. };
  530. static int ad9523_read_raw(struct iio_dev *indio_dev,
  531. struct iio_chan_spec const *chan,
  532. int *val,
  533. int *val2,
  534. long m)
  535. {
  536. struct ad9523_state *st = iio_priv(indio_dev);
  537. unsigned int code;
  538. int ret;
  539. mutex_lock(&st->lock);
  540. ret = ad9523_read(indio_dev, AD9523_CHANNEL_CLOCK_DIST(chan->channel));
  541. mutex_unlock(&st->lock);
  542. if (ret < 0)
  543. return ret;
  544. switch (m) {
  545. case IIO_CHAN_INFO_RAW:
  546. *val = !(ret & AD9523_CLK_DIST_PWR_DOWN_EN);
  547. return IIO_VAL_INT;
  548. case IIO_CHAN_INFO_FREQUENCY:
  549. *val = st->vco_out_freq[st->vco_out_map[chan->channel]] /
  550. AD9523_CLK_DIST_DIV_REV(ret);
  551. return IIO_VAL_INT;
  552. case IIO_CHAN_INFO_PHASE:
  553. code = (AD9523_CLK_DIST_DIV_PHASE_REV(ret) * 3141592) /
  554. AD9523_CLK_DIST_DIV_REV(ret);
  555. *val = code / 1000000;
  556. *val2 = code % 1000000;
  557. return IIO_VAL_INT_PLUS_MICRO;
  558. default:
  559. return -EINVAL;
  560. }
  561. };
  562. static int ad9523_write_raw(struct iio_dev *indio_dev,
  563. struct iio_chan_spec const *chan,
  564. int val,
  565. int val2,
  566. long mask)
  567. {
  568. struct ad9523_state *st = iio_priv(indio_dev);
  569. unsigned int reg;
  570. int ret, tmp, code;
  571. mutex_lock(&st->lock);
  572. ret = ad9523_read(indio_dev, AD9523_CHANNEL_CLOCK_DIST(chan->channel));
  573. if (ret < 0)
  574. goto out;
  575. reg = ret;
  576. switch (mask) {
  577. case IIO_CHAN_INFO_RAW:
  578. if (val)
  579. reg &= ~AD9523_CLK_DIST_PWR_DOWN_EN;
  580. else
  581. reg |= AD9523_CLK_DIST_PWR_DOWN_EN;
  582. break;
  583. case IIO_CHAN_INFO_FREQUENCY:
  584. if (val <= 0) {
  585. ret = -EINVAL;
  586. goto out;
  587. }
  588. ret = ad9523_set_clock_provider(indio_dev, chan->channel, val);
  589. if (ret < 0)
  590. goto out;
  591. tmp = st->vco_out_freq[st->vco_out_map[chan->channel]] / val;
  592. tmp = clamp(tmp, 1, 1024);
  593. reg &= ~(0x3FF << 8);
  594. reg |= AD9523_CLK_DIST_DIV(tmp);
  595. break;
  596. case IIO_CHAN_INFO_PHASE:
  597. code = val * 1000000 + val2 % 1000000;
  598. tmp = (code * AD9523_CLK_DIST_DIV_REV(ret)) / 3141592;
  599. tmp = clamp(tmp, 0, 63);
  600. reg &= ~AD9523_CLK_DIST_DIV_PHASE(~0);
  601. reg |= AD9523_CLK_DIST_DIV_PHASE(tmp);
  602. break;
  603. default:
  604. ret = -EINVAL;
  605. goto out;
  606. }
  607. ret = ad9523_write(indio_dev, AD9523_CHANNEL_CLOCK_DIST(chan->channel),
  608. reg);
  609. if (ret < 0)
  610. goto out;
  611. ad9523_io_update(indio_dev);
  612. out:
  613. mutex_unlock(&st->lock);
  614. return ret;
  615. }
  616. static int ad9523_reg_access(struct iio_dev *indio_dev,
  617. unsigned int reg, unsigned int writeval,
  618. unsigned int *readval)
  619. {
  620. struct ad9523_state *st = iio_priv(indio_dev);
  621. int ret;
  622. mutex_lock(&st->lock);
  623. if (readval == NULL) {
  624. ret = ad9523_write(indio_dev, reg | AD9523_R1B, writeval);
  625. ad9523_io_update(indio_dev);
  626. } else {
  627. ret = ad9523_read(indio_dev, reg | AD9523_R1B);
  628. if (ret < 0)
  629. goto out_unlock;
  630. *readval = ret;
  631. ret = 0;
  632. }
  633. out_unlock:
  634. mutex_unlock(&st->lock);
  635. return ret;
  636. }
  637. static const struct iio_info ad9523_info = {
  638. .read_raw = &ad9523_read_raw,
  639. .write_raw = &ad9523_write_raw,
  640. .debugfs_reg_access = &ad9523_reg_access,
  641. .attrs = &ad9523_attribute_group,
  642. };
  643. static int ad9523_setup(struct iio_dev *indio_dev)
  644. {
  645. struct ad9523_state *st = iio_priv(indio_dev);
  646. struct ad9523_platform_data *pdata = st->pdata;
  647. struct ad9523_channel_spec *chan;
  648. unsigned long active_mask = 0;
  649. int ret, i;
  650. ret = ad9523_write(indio_dev, AD9523_SERIAL_PORT_CONFIG,
  651. AD9523_SER_CONF_SOFT_RESET |
  652. (st->spi->mode & SPI_3WIRE ? 0 :
  653. AD9523_SER_CONF_SDO_ACTIVE));
  654. if (ret < 0)
  655. return ret;
  656. ret = ad9523_write(indio_dev, AD9523_READBACK_CTRL,
  657. AD9523_READBACK_CTRL_READ_BUFFERED);
  658. if (ret < 0)
  659. return ret;
  660. ret = ad9523_io_update(indio_dev);
  661. if (ret < 0)
  662. return ret;
  663. /*
  664. * PLL1 Setup
  665. */
  666. ret = ad9523_write(indio_dev, AD9523_PLL1_REF_A_DIVIDER,
  667. pdata->refa_r_div);
  668. if (ret < 0)
  669. return ret;
  670. ret = ad9523_write(indio_dev, AD9523_PLL1_REF_B_DIVIDER,
  671. pdata->refb_r_div);
  672. if (ret < 0)
  673. return ret;
  674. ret = ad9523_write(indio_dev, AD9523_PLL1_FEEDBACK_DIVIDER,
  675. pdata->pll1_feedback_div);
  676. if (ret < 0)
  677. return ret;
  678. ret = ad9523_write(indio_dev, AD9523_PLL1_CHARGE_PUMP_CTRL,
  679. AD9523_PLL1_CHARGE_PUMP_CURRENT_nA(pdata->
  680. pll1_charge_pump_current_nA) |
  681. AD9523_PLL1_CHARGE_PUMP_MODE_NORMAL |
  682. AD9523_PLL1_BACKLASH_PW_MIN);
  683. if (ret < 0)
  684. return ret;
  685. ret = ad9523_write(indio_dev, AD9523_PLL1_INPUT_RECEIVERS_CTRL,
  686. AD_IF(refa_diff_rcv_en, AD9523_PLL1_REFA_RCV_EN) |
  687. AD_IF(refb_diff_rcv_en, AD9523_PLL1_REFB_RCV_EN) |
  688. AD_IF(osc_in_diff_en, AD9523_PLL1_OSC_IN_DIFF_EN) |
  689. AD_IF(osc_in_cmos_neg_inp_en,
  690. AD9523_PLL1_OSC_IN_CMOS_NEG_INP_EN) |
  691. AD_IF(refa_diff_rcv_en, AD9523_PLL1_REFA_DIFF_RCV_EN) |
  692. AD_IF(refb_diff_rcv_en, AD9523_PLL1_REFB_DIFF_RCV_EN));
  693. if (ret < 0)
  694. return ret;
  695. ret = ad9523_write(indio_dev, AD9523_PLL1_REF_CTRL,
  696. AD_IF(zd_in_diff_en, AD9523_PLL1_ZD_IN_DIFF_EN) |
  697. AD_IF(zd_in_cmos_neg_inp_en,
  698. AD9523_PLL1_ZD_IN_CMOS_NEG_INP_EN) |
  699. AD_IF(zero_delay_mode_internal_en,
  700. AD9523_PLL1_ZERO_DELAY_MODE_INT) |
  701. AD_IF(osc_in_feedback_en, AD9523_PLL1_OSC_IN_PLL_FEEDBACK_EN) |
  702. AD_IF(refa_cmos_neg_inp_en, AD9523_PLL1_REFA_CMOS_NEG_INP_EN) |
  703. AD_IF(refb_cmos_neg_inp_en, AD9523_PLL1_REFB_CMOS_NEG_INP_EN));
  704. if (ret < 0)
  705. return ret;
  706. ret = ad9523_write(indio_dev, AD9523_PLL1_MISC_CTRL,
  707. AD9523_PLL1_REFB_INDEP_DIV_CTRL_EN |
  708. AD9523_PLL1_REF_MODE(pdata->ref_mode));
  709. if (ret < 0)
  710. return ret;
  711. ret = ad9523_write(indio_dev, AD9523_PLL1_LOOP_FILTER_CTRL,
  712. AD9523_PLL1_LOOP_FILTER_RZERO(pdata->pll1_loop_filter_rzero));
  713. if (ret < 0)
  714. return ret;
  715. /*
  716. * PLL2 Setup
  717. */
  718. ret = ad9523_write(indio_dev, AD9523_PLL2_CHARGE_PUMP,
  719. AD9523_PLL2_CHARGE_PUMP_CURRENT_nA(pdata->
  720. pll2_charge_pump_current_nA));
  721. if (ret < 0)
  722. return ret;
  723. ret = ad9523_write(indio_dev, AD9523_PLL2_FEEDBACK_DIVIDER_AB,
  724. AD9523_PLL2_FB_NDIV_A_CNT(pdata->pll2_ndiv_a_cnt) |
  725. AD9523_PLL2_FB_NDIV_B_CNT(pdata->pll2_ndiv_b_cnt));
  726. if (ret < 0)
  727. return ret;
  728. ret = ad9523_write(indio_dev, AD9523_PLL2_CTRL,
  729. AD9523_PLL2_CHARGE_PUMP_MODE_NORMAL |
  730. AD9523_PLL2_BACKLASH_CTRL_EN |
  731. AD_IF(pll2_freq_doubler_en, AD9523_PLL2_FREQ_DOUBLER_EN));
  732. if (ret < 0)
  733. return ret;
  734. st->vco_freq = (pdata->vcxo_freq * (pdata->pll2_freq_doubler_en ? 2 : 1)
  735. / pdata->pll2_r2_div) * AD9523_PLL2_FB_NDIV(pdata->
  736. pll2_ndiv_a_cnt, pdata->pll2_ndiv_b_cnt);
  737. ret = ad9523_write(indio_dev, AD9523_PLL2_VCO_CTRL,
  738. AD9523_PLL2_VCO_CALIBRATE);
  739. if (ret < 0)
  740. return ret;
  741. ret = ad9523_write(indio_dev, AD9523_PLL2_VCO_DIVIDER,
  742. AD9523_PLL2_VCO_DIV_M1(pdata->pll2_vco_diff_m1) |
  743. AD9523_PLL2_VCO_DIV_M2(pdata->pll2_vco_diff_m2) |
  744. AD_IFE(pll2_vco_diff_m1, 0,
  745. AD9523_PLL2_VCO_DIV_M1_PWR_DOWN_EN) |
  746. AD_IFE(pll2_vco_diff_m2, 0,
  747. AD9523_PLL2_VCO_DIV_M2_PWR_DOWN_EN));
  748. if (ret < 0)
  749. return ret;
  750. if (pdata->pll2_vco_diff_m1)
  751. st->vco_out_freq[AD9523_VCO1] =
  752. st->vco_freq / pdata->pll2_vco_diff_m1;
  753. if (pdata->pll2_vco_diff_m2)
  754. st->vco_out_freq[AD9523_VCO2] =
  755. st->vco_freq / pdata->pll2_vco_diff_m2;
  756. st->vco_out_freq[AD9523_VCXO] = pdata->vcxo_freq;
  757. ret = ad9523_write(indio_dev, AD9523_PLL2_R2_DIVIDER,
  758. AD9523_PLL2_R2_DIVIDER_VAL(pdata->pll2_r2_div));
  759. if (ret < 0)
  760. return ret;
  761. ret = ad9523_write(indio_dev, AD9523_PLL2_LOOP_FILTER_CTRL,
  762. AD9523_PLL2_LOOP_FILTER_CPOLE1(pdata->cpole1) |
  763. AD9523_PLL2_LOOP_FILTER_RZERO(pdata->rzero) |
  764. AD9523_PLL2_LOOP_FILTER_RPOLE2(pdata->rpole2) |
  765. AD_IF(rzero_bypass_en,
  766. AD9523_PLL2_LOOP_FILTER_RZERO_BYPASS_EN));
  767. if (ret < 0)
  768. return ret;
  769. for (i = 0; i < pdata->num_channels; i++) {
  770. chan = &pdata->channels[i];
  771. if (chan->channel_num < AD9523_NUM_CHAN) {
  772. __set_bit(chan->channel_num, &active_mask);
  773. ret = ad9523_write(indio_dev,
  774. AD9523_CHANNEL_CLOCK_DIST(chan->channel_num),
  775. AD9523_CLK_DIST_DRIVER_MODE(chan->driver_mode) |
  776. AD9523_CLK_DIST_DIV(chan->channel_divider) |
  777. AD9523_CLK_DIST_DIV_PHASE(chan->divider_phase) |
  778. (chan->sync_ignore_en ?
  779. AD9523_CLK_DIST_IGNORE_SYNC_EN : 0) |
  780. (chan->divider_output_invert_en ?
  781. AD9523_CLK_DIST_INV_DIV_OUTPUT_EN : 0) |
  782. (chan->low_power_mode_en ?
  783. AD9523_CLK_DIST_LOW_PWR_MODE_EN : 0) |
  784. (chan->output_dis ?
  785. AD9523_CLK_DIST_PWR_DOWN_EN : 0));
  786. if (ret < 0)
  787. return ret;
  788. ret = ad9523_vco_out_map(indio_dev, chan->channel_num,
  789. chan->use_alt_clock_src);
  790. if (ret < 0)
  791. return ret;
  792. st->ad9523_channels[i].type = IIO_ALTVOLTAGE;
  793. st->ad9523_channels[i].output = 1;
  794. st->ad9523_channels[i].indexed = 1;
  795. st->ad9523_channels[i].channel = chan->channel_num;
  796. st->ad9523_channels[i].extend_name =
  797. chan->extended_name;
  798. st->ad9523_channels[i].info_mask_separate =
  799. BIT(IIO_CHAN_INFO_RAW) |
  800. BIT(IIO_CHAN_INFO_PHASE) |
  801. BIT(IIO_CHAN_INFO_FREQUENCY);
  802. }
  803. }
  804. for_each_clear_bit(i, &active_mask, AD9523_NUM_CHAN)
  805. ad9523_write(indio_dev,
  806. AD9523_CHANNEL_CLOCK_DIST(i),
  807. AD9523_CLK_DIST_DRIVER_MODE(TRISTATE) |
  808. AD9523_CLK_DIST_PWR_DOWN_EN);
  809. ret = ad9523_write(indio_dev, AD9523_POWER_DOWN_CTRL, 0);
  810. if (ret < 0)
  811. return ret;
  812. ret = ad9523_write(indio_dev, AD9523_STATUS_SIGNALS,
  813. AD9523_STATUS_MONITOR_01_PLL12_LOCKED);
  814. if (ret < 0)
  815. return ret;
  816. ret = ad9523_io_update(indio_dev);
  817. if (ret < 0)
  818. return ret;
  819. return 0;
  820. }
  821. static int ad9523_probe(struct spi_device *spi)
  822. {
  823. struct ad9523_platform_data *pdata = spi->dev.platform_data;
  824. struct iio_dev *indio_dev;
  825. struct ad9523_state *st;
  826. int ret;
  827. if (!pdata) {
  828. dev_err(&spi->dev, "no platform data?\n");
  829. return -EINVAL;
  830. }
  831. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  832. if (indio_dev == NULL)
  833. return -ENOMEM;
  834. st = iio_priv(indio_dev);
  835. mutex_init(&st->lock);
  836. st->reg = devm_regulator_get(&spi->dev, "vcc");
  837. if (!IS_ERR(st->reg)) {
  838. ret = regulator_enable(st->reg);
  839. if (ret)
  840. return ret;
  841. }
  842. st->pwrdown_gpio = devm_gpiod_get_optional(&spi->dev, "powerdown",
  843. GPIOD_OUT_HIGH);
  844. if (IS_ERR(st->pwrdown_gpio)) {
  845. ret = PTR_ERR(st->pwrdown_gpio);
  846. goto error_disable_reg;
  847. }
  848. st->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset",
  849. GPIOD_OUT_LOW);
  850. if (IS_ERR(st->reset_gpio)) {
  851. ret = PTR_ERR(st->reset_gpio);
  852. goto error_disable_reg;
  853. }
  854. if (st->reset_gpio) {
  855. udelay(1);
  856. gpiod_direction_output(st->reset_gpio, 1);
  857. }
  858. st->sync_gpio = devm_gpiod_get_optional(&spi->dev, "sync",
  859. GPIOD_OUT_HIGH);
  860. if (IS_ERR(st->sync_gpio)) {
  861. ret = PTR_ERR(st->sync_gpio);
  862. goto error_disable_reg;
  863. }
  864. spi_set_drvdata(spi, indio_dev);
  865. st->spi = spi;
  866. st->pdata = pdata;
  867. indio_dev->dev.parent = &spi->dev;
  868. indio_dev->name = (pdata->name[0] != 0) ? pdata->name :
  869. spi_get_device_id(spi)->name;
  870. indio_dev->info = &ad9523_info;
  871. indio_dev->modes = INDIO_DIRECT_MODE;
  872. indio_dev->channels = st->ad9523_channels;
  873. indio_dev->num_channels = pdata->num_channels;
  874. ret = ad9523_setup(indio_dev);
  875. if (ret < 0)
  876. goto error_disable_reg;
  877. ret = iio_device_register(indio_dev);
  878. if (ret)
  879. goto error_disable_reg;
  880. dev_info(&spi->dev, "probed %s\n", indio_dev->name);
  881. return 0;
  882. error_disable_reg:
  883. if (!IS_ERR(st->reg))
  884. regulator_disable(st->reg);
  885. return ret;
  886. }
  887. static int ad9523_remove(struct spi_device *spi)
  888. {
  889. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  890. struct ad9523_state *st = iio_priv(indio_dev);
  891. iio_device_unregister(indio_dev);
  892. if (!IS_ERR(st->reg))
  893. regulator_disable(st->reg);
  894. return 0;
  895. }
  896. static const struct spi_device_id ad9523_id[] = {
  897. {"ad9523-1", 9523},
  898. {}
  899. };
  900. MODULE_DEVICE_TABLE(spi, ad9523_id);
  901. static struct spi_driver ad9523_driver = {
  902. .driver = {
  903. .name = "ad9523",
  904. },
  905. .probe = ad9523_probe,
  906. .remove = ad9523_remove,
  907. .id_table = ad9523_id,
  908. };
  909. module_spi_driver(ad9523_driver);
  910. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  911. MODULE_DESCRIPTION("Analog Devices AD9523 CLOCKDIST/PLL");
  912. MODULE_LICENSE("GPL v2");