adf4350.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * ADF4350/ADF4351 SPI Wideband Synthesizer driver
  3. *
  4. * Copyright 2012-2013 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/err.h>
  15. #include <linux/module.h>
  16. #include <linux/gcd.h>
  17. #include <linux/gpio.h>
  18. #include <asm/div64.h>
  19. #include <linux/clk.h>
  20. #include <linux/of.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/iio/iio.h>
  23. #include <linux/iio/sysfs.h>
  24. #include <linux/iio/frequency/adf4350.h>
  25. enum {
  26. ADF4350_FREQ,
  27. ADF4350_FREQ_REFIN,
  28. ADF4350_FREQ_RESOLUTION,
  29. ADF4350_PWRDOWN,
  30. };
  31. struct adf4350_state {
  32. struct spi_device *spi;
  33. struct regulator *reg;
  34. struct adf4350_platform_data *pdata;
  35. struct clk *clk;
  36. unsigned long clkin;
  37. unsigned long chspc; /* Channel Spacing */
  38. unsigned long fpfd; /* Phase Frequency Detector */
  39. unsigned long min_out_freq;
  40. unsigned r0_fract;
  41. unsigned r0_int;
  42. unsigned r1_mod;
  43. unsigned r4_rf_div_sel;
  44. unsigned long regs[6];
  45. unsigned long regs_hw[6];
  46. unsigned long long freq_req;
  47. /*
  48. * DMA (thus cache coherency maintenance) requires the
  49. * transfer buffers to live in their own cache lines.
  50. */
  51. __be32 val ____cacheline_aligned;
  52. };
  53. static struct adf4350_platform_data default_pdata = {
  54. .channel_spacing = 10000,
  55. .r2_user_settings = ADF4350_REG2_PD_POLARITY_POS |
  56. ADF4350_REG2_CHARGE_PUMP_CURR_uA(2500),
  57. .r3_user_settings = ADF4350_REG3_12BIT_CLKDIV_MODE(0),
  58. .r4_user_settings = ADF4350_REG4_OUTPUT_PWR(3) |
  59. ADF4350_REG4_MUTE_TILL_LOCK_EN,
  60. .gpio_lock_detect = -1,
  61. };
  62. static int adf4350_sync_config(struct adf4350_state *st)
  63. {
  64. int ret, i, doublebuf = 0;
  65. for (i = ADF4350_REG5; i >= ADF4350_REG0; i--) {
  66. if ((st->regs_hw[i] != st->regs[i]) ||
  67. ((i == ADF4350_REG0) && doublebuf)) {
  68. switch (i) {
  69. case ADF4350_REG1:
  70. case ADF4350_REG4:
  71. doublebuf = 1;
  72. break;
  73. }
  74. st->val = cpu_to_be32(st->regs[i] | i);
  75. ret = spi_write(st->spi, &st->val, 4);
  76. if (ret < 0)
  77. return ret;
  78. st->regs_hw[i] = st->regs[i];
  79. dev_dbg(&st->spi->dev, "[%d] 0x%X\n",
  80. i, (u32)st->regs[i] | i);
  81. }
  82. }
  83. return 0;
  84. }
  85. static int adf4350_reg_access(struct iio_dev *indio_dev,
  86. unsigned reg, unsigned writeval,
  87. unsigned *readval)
  88. {
  89. struct adf4350_state *st = iio_priv(indio_dev);
  90. int ret;
  91. if (reg > ADF4350_REG5)
  92. return -EINVAL;
  93. mutex_lock(&indio_dev->mlock);
  94. if (readval == NULL) {
  95. st->regs[reg] = writeval & ~(BIT(0) | BIT(1) | BIT(2));
  96. ret = adf4350_sync_config(st);
  97. } else {
  98. *readval = st->regs_hw[reg];
  99. ret = 0;
  100. }
  101. mutex_unlock(&indio_dev->mlock);
  102. return ret;
  103. }
  104. static int adf4350_tune_r_cnt(struct adf4350_state *st, unsigned short r_cnt)
  105. {
  106. struct adf4350_platform_data *pdata = st->pdata;
  107. do {
  108. r_cnt++;
  109. st->fpfd = (st->clkin * (pdata->ref_doubler_en ? 2 : 1)) /
  110. (r_cnt * (pdata->ref_div2_en ? 2 : 1));
  111. } while (st->fpfd > ADF4350_MAX_FREQ_PFD);
  112. return r_cnt;
  113. }
  114. static int adf4350_set_freq(struct adf4350_state *st, unsigned long long freq)
  115. {
  116. struct adf4350_platform_data *pdata = st->pdata;
  117. u64 tmp;
  118. u32 div_gcd, prescaler, chspc;
  119. u16 mdiv, r_cnt = 0;
  120. u8 band_sel_div;
  121. if (freq > ADF4350_MAX_OUT_FREQ || freq < st->min_out_freq)
  122. return -EINVAL;
  123. if (freq > ADF4350_MAX_FREQ_45_PRESC) {
  124. prescaler = ADF4350_REG1_PRESCALER;
  125. mdiv = 75;
  126. } else {
  127. prescaler = 0;
  128. mdiv = 23;
  129. }
  130. st->r4_rf_div_sel = 0;
  131. while (freq < ADF4350_MIN_VCO_FREQ) {
  132. freq <<= 1;
  133. st->r4_rf_div_sel++;
  134. }
  135. /*
  136. * Allow a predefined reference division factor
  137. * if not set, compute our own
  138. */
  139. if (pdata->ref_div_factor)
  140. r_cnt = pdata->ref_div_factor - 1;
  141. chspc = st->chspc;
  142. do {
  143. do {
  144. do {
  145. r_cnt = adf4350_tune_r_cnt(st, r_cnt);
  146. st->r1_mod = st->fpfd / chspc;
  147. if (r_cnt > ADF4350_MAX_R_CNT) {
  148. /* try higher spacing values */
  149. chspc++;
  150. r_cnt = 0;
  151. }
  152. } while ((st->r1_mod > ADF4350_MAX_MODULUS) && r_cnt);
  153. } while (r_cnt == 0);
  154. tmp = freq * (u64)st->r1_mod + (st->fpfd >> 1);
  155. do_div(tmp, st->fpfd); /* Div round closest (n + d/2)/d */
  156. st->r0_fract = do_div(tmp, st->r1_mod);
  157. st->r0_int = tmp;
  158. } while (mdiv > st->r0_int);
  159. band_sel_div = DIV_ROUND_UP(st->fpfd, ADF4350_MAX_BANDSEL_CLK);
  160. if (st->r0_fract && st->r1_mod) {
  161. div_gcd = gcd(st->r1_mod, st->r0_fract);
  162. st->r1_mod /= div_gcd;
  163. st->r0_fract /= div_gcd;
  164. } else {
  165. st->r0_fract = 0;
  166. st->r1_mod = 1;
  167. }
  168. dev_dbg(&st->spi->dev, "VCO: %llu Hz, PFD %lu Hz\n"
  169. "REF_DIV %d, R0_INT %d, R0_FRACT %d\n"
  170. "R1_MOD %d, RF_DIV %d\nPRESCALER %s, BAND_SEL_DIV %d\n",
  171. freq, st->fpfd, r_cnt, st->r0_int, st->r0_fract, st->r1_mod,
  172. 1 << st->r4_rf_div_sel, prescaler ? "8/9" : "4/5",
  173. band_sel_div);
  174. st->regs[ADF4350_REG0] = ADF4350_REG0_INT(st->r0_int) |
  175. ADF4350_REG0_FRACT(st->r0_fract);
  176. st->regs[ADF4350_REG1] = ADF4350_REG1_PHASE(1) |
  177. ADF4350_REG1_MOD(st->r1_mod) |
  178. prescaler;
  179. st->regs[ADF4350_REG2] =
  180. ADF4350_REG2_10BIT_R_CNT(r_cnt) |
  181. ADF4350_REG2_DOUBLE_BUFF_EN |
  182. (pdata->ref_doubler_en ? ADF4350_REG2_RMULT2_EN : 0) |
  183. (pdata->ref_div2_en ? ADF4350_REG2_RDIV2_EN : 0) |
  184. (pdata->r2_user_settings & (ADF4350_REG2_PD_POLARITY_POS |
  185. ADF4350_REG2_LDP_6ns | ADF4350_REG2_LDF_INT_N |
  186. ADF4350_REG2_CHARGE_PUMP_CURR_uA(5000) |
  187. ADF4350_REG2_MUXOUT(0x7) | ADF4350_REG2_NOISE_MODE(0x3)));
  188. st->regs[ADF4350_REG3] = pdata->r3_user_settings &
  189. (ADF4350_REG3_12BIT_CLKDIV(0xFFF) |
  190. ADF4350_REG3_12BIT_CLKDIV_MODE(0x3) |
  191. ADF4350_REG3_12BIT_CSR_EN |
  192. ADF4351_REG3_CHARGE_CANCELLATION_EN |
  193. ADF4351_REG3_ANTI_BACKLASH_3ns_EN |
  194. ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH);
  195. st->regs[ADF4350_REG4] =
  196. ADF4350_REG4_FEEDBACK_FUND |
  197. ADF4350_REG4_RF_DIV_SEL(st->r4_rf_div_sel) |
  198. ADF4350_REG4_8BIT_BAND_SEL_CLKDIV(band_sel_div) |
  199. ADF4350_REG4_RF_OUT_EN |
  200. (pdata->r4_user_settings &
  201. (ADF4350_REG4_OUTPUT_PWR(0x3) |
  202. ADF4350_REG4_AUX_OUTPUT_PWR(0x3) |
  203. ADF4350_REG4_AUX_OUTPUT_EN |
  204. ADF4350_REG4_AUX_OUTPUT_FUND |
  205. ADF4350_REG4_MUTE_TILL_LOCK_EN));
  206. st->regs[ADF4350_REG5] = ADF4350_REG5_LD_PIN_MODE_DIGITAL;
  207. st->freq_req = freq;
  208. return adf4350_sync_config(st);
  209. }
  210. static ssize_t adf4350_write(struct iio_dev *indio_dev,
  211. uintptr_t private,
  212. const struct iio_chan_spec *chan,
  213. const char *buf, size_t len)
  214. {
  215. struct adf4350_state *st = iio_priv(indio_dev);
  216. unsigned long long readin;
  217. unsigned long tmp;
  218. int ret;
  219. ret = kstrtoull(buf, 10, &readin);
  220. if (ret)
  221. return ret;
  222. mutex_lock(&indio_dev->mlock);
  223. switch ((u32)private) {
  224. case ADF4350_FREQ:
  225. ret = adf4350_set_freq(st, readin);
  226. break;
  227. case ADF4350_FREQ_REFIN:
  228. if (readin > ADF4350_MAX_FREQ_REFIN) {
  229. ret = -EINVAL;
  230. break;
  231. }
  232. if (st->clk) {
  233. tmp = clk_round_rate(st->clk, readin);
  234. if (tmp != readin) {
  235. ret = -EINVAL;
  236. break;
  237. }
  238. ret = clk_set_rate(st->clk, tmp);
  239. if (ret < 0)
  240. break;
  241. }
  242. st->clkin = readin;
  243. ret = adf4350_set_freq(st, st->freq_req);
  244. break;
  245. case ADF4350_FREQ_RESOLUTION:
  246. if (readin == 0)
  247. ret = -EINVAL;
  248. else
  249. st->chspc = readin;
  250. break;
  251. case ADF4350_PWRDOWN:
  252. if (readin)
  253. st->regs[ADF4350_REG2] |= ADF4350_REG2_POWER_DOWN_EN;
  254. else
  255. st->regs[ADF4350_REG2] &= ~ADF4350_REG2_POWER_DOWN_EN;
  256. adf4350_sync_config(st);
  257. break;
  258. default:
  259. ret = -EINVAL;
  260. }
  261. mutex_unlock(&indio_dev->mlock);
  262. return ret ? ret : len;
  263. }
  264. static ssize_t adf4350_read(struct iio_dev *indio_dev,
  265. uintptr_t private,
  266. const struct iio_chan_spec *chan,
  267. char *buf)
  268. {
  269. struct adf4350_state *st = iio_priv(indio_dev);
  270. unsigned long long val;
  271. int ret = 0;
  272. mutex_lock(&indio_dev->mlock);
  273. switch ((u32)private) {
  274. case ADF4350_FREQ:
  275. val = (u64)((st->r0_int * st->r1_mod) + st->r0_fract) *
  276. (u64)st->fpfd;
  277. do_div(val, st->r1_mod * (1 << st->r4_rf_div_sel));
  278. /* PLL unlocked? return error */
  279. if (gpio_is_valid(st->pdata->gpio_lock_detect))
  280. if (!gpio_get_value(st->pdata->gpio_lock_detect)) {
  281. dev_dbg(&st->spi->dev, "PLL un-locked\n");
  282. ret = -EBUSY;
  283. }
  284. break;
  285. case ADF4350_FREQ_REFIN:
  286. if (st->clk)
  287. st->clkin = clk_get_rate(st->clk);
  288. val = st->clkin;
  289. break;
  290. case ADF4350_FREQ_RESOLUTION:
  291. val = st->chspc;
  292. break;
  293. case ADF4350_PWRDOWN:
  294. val = !!(st->regs[ADF4350_REG2] & ADF4350_REG2_POWER_DOWN_EN);
  295. break;
  296. default:
  297. ret = -EINVAL;
  298. val = 0;
  299. }
  300. mutex_unlock(&indio_dev->mlock);
  301. return ret < 0 ? ret : sprintf(buf, "%llu\n", val);
  302. }
  303. #define _ADF4350_EXT_INFO(_name, _ident) { \
  304. .name = _name, \
  305. .read = adf4350_read, \
  306. .write = adf4350_write, \
  307. .private = _ident, \
  308. .shared = IIO_SEPARATE, \
  309. }
  310. static const struct iio_chan_spec_ext_info adf4350_ext_info[] = {
  311. /* Ideally we use IIO_CHAN_INFO_FREQUENCY, but there are
  312. * values > 2^32 in order to support the entire frequency range
  313. * in Hz. Using scale is a bit ugly.
  314. */
  315. _ADF4350_EXT_INFO("frequency", ADF4350_FREQ),
  316. _ADF4350_EXT_INFO("frequency_resolution", ADF4350_FREQ_RESOLUTION),
  317. _ADF4350_EXT_INFO("refin_frequency", ADF4350_FREQ_REFIN),
  318. _ADF4350_EXT_INFO("powerdown", ADF4350_PWRDOWN),
  319. { },
  320. };
  321. static const struct iio_chan_spec adf4350_chan = {
  322. .type = IIO_ALTVOLTAGE,
  323. .indexed = 1,
  324. .output = 1,
  325. .ext_info = adf4350_ext_info,
  326. };
  327. static const struct iio_info adf4350_info = {
  328. .debugfs_reg_access = &adf4350_reg_access,
  329. };
  330. #ifdef CONFIG_OF
  331. static struct adf4350_platform_data *adf4350_parse_dt(struct device *dev)
  332. {
  333. struct device_node *np = dev->of_node;
  334. struct adf4350_platform_data *pdata;
  335. unsigned int tmp;
  336. int ret;
  337. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  338. if (!pdata)
  339. return NULL;
  340. strncpy(&pdata->name[0], np->name, SPI_NAME_SIZE - 1);
  341. tmp = 10000;
  342. of_property_read_u32(np, "adi,channel-spacing", &tmp);
  343. pdata->channel_spacing = tmp;
  344. tmp = 0;
  345. of_property_read_u32(np, "adi,power-up-frequency", &tmp);
  346. pdata->power_up_frequency = tmp;
  347. tmp = 0;
  348. of_property_read_u32(np, "adi,reference-div-factor", &tmp);
  349. pdata->ref_div_factor = tmp;
  350. ret = of_get_gpio(np, 0);
  351. if (ret < 0)
  352. pdata->gpio_lock_detect = -1;
  353. else
  354. pdata->gpio_lock_detect = ret;
  355. pdata->ref_doubler_en = of_property_read_bool(np,
  356. "adi,reference-doubler-enable");
  357. pdata->ref_div2_en = of_property_read_bool(np,
  358. "adi,reference-div2-enable");
  359. /* r2_user_settings */
  360. pdata->r2_user_settings = of_property_read_bool(np,
  361. "adi,phase-detector-polarity-positive-enable") ?
  362. ADF4350_REG2_PD_POLARITY_POS : 0;
  363. pdata->r2_user_settings |= of_property_read_bool(np,
  364. "adi,lock-detect-precision-6ns-enable") ?
  365. ADF4350_REG2_LDP_6ns : 0;
  366. pdata->r2_user_settings |= of_property_read_bool(np,
  367. "adi,lock-detect-function-integer-n-enable") ?
  368. ADF4350_REG2_LDF_INT_N : 0;
  369. tmp = 2500;
  370. of_property_read_u32(np, "adi,charge-pump-current", &tmp);
  371. pdata->r2_user_settings |= ADF4350_REG2_CHARGE_PUMP_CURR_uA(tmp);
  372. tmp = 0;
  373. of_property_read_u32(np, "adi,muxout-select", &tmp);
  374. pdata->r2_user_settings |= ADF4350_REG2_MUXOUT(tmp);
  375. pdata->r2_user_settings |= of_property_read_bool(np,
  376. "adi,low-spur-mode-enable") ?
  377. ADF4350_REG2_NOISE_MODE(0x3) : 0;
  378. /* r3_user_settings */
  379. pdata->r3_user_settings = of_property_read_bool(np,
  380. "adi,cycle-slip-reduction-enable") ?
  381. ADF4350_REG3_12BIT_CSR_EN : 0;
  382. pdata->r3_user_settings |= of_property_read_bool(np,
  383. "adi,charge-cancellation-enable") ?
  384. ADF4351_REG3_CHARGE_CANCELLATION_EN : 0;
  385. pdata->r3_user_settings |= of_property_read_bool(np,
  386. "adi,anti-backlash-3ns-enable") ?
  387. ADF4351_REG3_ANTI_BACKLASH_3ns_EN : 0;
  388. pdata->r3_user_settings |= of_property_read_bool(np,
  389. "adi,band-select-clock-mode-high-enable") ?
  390. ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH : 0;
  391. tmp = 0;
  392. of_property_read_u32(np, "adi,12bit-clk-divider", &tmp);
  393. pdata->r3_user_settings |= ADF4350_REG3_12BIT_CLKDIV(tmp);
  394. tmp = 0;
  395. of_property_read_u32(np, "adi,clk-divider-mode", &tmp);
  396. pdata->r3_user_settings |= ADF4350_REG3_12BIT_CLKDIV_MODE(tmp);
  397. /* r4_user_settings */
  398. pdata->r4_user_settings = of_property_read_bool(np,
  399. "adi,aux-output-enable") ?
  400. ADF4350_REG4_AUX_OUTPUT_EN : 0;
  401. pdata->r4_user_settings |= of_property_read_bool(np,
  402. "adi,aux-output-fundamental-enable") ?
  403. ADF4350_REG4_AUX_OUTPUT_FUND : 0;
  404. pdata->r4_user_settings |= of_property_read_bool(np,
  405. "adi,mute-till-lock-enable") ?
  406. ADF4350_REG4_MUTE_TILL_LOCK_EN : 0;
  407. tmp = 0;
  408. of_property_read_u32(np, "adi,output-power", &tmp);
  409. pdata->r4_user_settings |= ADF4350_REG4_OUTPUT_PWR(tmp);
  410. tmp = 0;
  411. of_property_read_u32(np, "adi,aux-output-power", &tmp);
  412. pdata->r4_user_settings |= ADF4350_REG4_AUX_OUTPUT_PWR(tmp);
  413. return pdata;
  414. }
  415. #else
  416. static
  417. struct adf4350_platform_data *adf4350_parse_dt(struct device *dev)
  418. {
  419. return NULL;
  420. }
  421. #endif
  422. static int adf4350_probe(struct spi_device *spi)
  423. {
  424. struct adf4350_platform_data *pdata;
  425. struct iio_dev *indio_dev;
  426. struct adf4350_state *st;
  427. struct clk *clk = NULL;
  428. int ret;
  429. if (spi->dev.of_node) {
  430. pdata = adf4350_parse_dt(&spi->dev);
  431. if (pdata == NULL)
  432. return -EINVAL;
  433. } else {
  434. pdata = spi->dev.platform_data;
  435. }
  436. if (!pdata) {
  437. dev_warn(&spi->dev, "no platform data? using default\n");
  438. pdata = &default_pdata;
  439. }
  440. if (!pdata->clkin) {
  441. clk = devm_clk_get(&spi->dev, "clkin");
  442. if (IS_ERR(clk))
  443. return -EPROBE_DEFER;
  444. ret = clk_prepare_enable(clk);
  445. if (ret < 0)
  446. return ret;
  447. }
  448. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
  449. if (indio_dev == NULL) {
  450. ret = -ENOMEM;
  451. goto error_disable_clk;
  452. }
  453. st = iio_priv(indio_dev);
  454. st->reg = devm_regulator_get(&spi->dev, "vcc");
  455. if (!IS_ERR(st->reg)) {
  456. ret = regulator_enable(st->reg);
  457. if (ret)
  458. goto error_disable_clk;
  459. }
  460. spi_set_drvdata(spi, indio_dev);
  461. st->spi = spi;
  462. st->pdata = pdata;
  463. indio_dev->dev.parent = &spi->dev;
  464. indio_dev->name = (pdata->name[0] != 0) ? pdata->name :
  465. spi_get_device_id(spi)->name;
  466. indio_dev->info = &adf4350_info;
  467. indio_dev->modes = INDIO_DIRECT_MODE;
  468. indio_dev->channels = &adf4350_chan;
  469. indio_dev->num_channels = 1;
  470. st->chspc = pdata->channel_spacing;
  471. if (clk) {
  472. st->clk = clk;
  473. st->clkin = clk_get_rate(clk);
  474. } else {
  475. st->clkin = pdata->clkin;
  476. }
  477. st->min_out_freq = spi_get_device_id(spi)->driver_data == 4351 ?
  478. ADF4351_MIN_OUT_FREQ : ADF4350_MIN_OUT_FREQ;
  479. memset(st->regs_hw, 0xFF, sizeof(st->regs_hw));
  480. if (gpio_is_valid(pdata->gpio_lock_detect)) {
  481. ret = devm_gpio_request(&spi->dev, pdata->gpio_lock_detect,
  482. indio_dev->name);
  483. if (ret) {
  484. dev_err(&spi->dev, "fail to request lock detect GPIO-%d",
  485. pdata->gpio_lock_detect);
  486. goto error_disable_reg;
  487. }
  488. gpio_direction_input(pdata->gpio_lock_detect);
  489. }
  490. if (pdata->power_up_frequency) {
  491. ret = adf4350_set_freq(st, pdata->power_up_frequency);
  492. if (ret)
  493. goto error_disable_reg;
  494. }
  495. ret = iio_device_register(indio_dev);
  496. if (ret)
  497. goto error_disable_reg;
  498. return 0;
  499. error_disable_reg:
  500. if (!IS_ERR(st->reg))
  501. regulator_disable(st->reg);
  502. error_disable_clk:
  503. if (clk)
  504. clk_disable_unprepare(clk);
  505. return ret;
  506. }
  507. static int adf4350_remove(struct spi_device *spi)
  508. {
  509. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  510. struct adf4350_state *st = iio_priv(indio_dev);
  511. struct regulator *reg = st->reg;
  512. st->regs[ADF4350_REG2] |= ADF4350_REG2_POWER_DOWN_EN;
  513. adf4350_sync_config(st);
  514. iio_device_unregister(indio_dev);
  515. if (st->clk)
  516. clk_disable_unprepare(st->clk);
  517. if (!IS_ERR(reg))
  518. regulator_disable(reg);
  519. return 0;
  520. }
  521. static const struct of_device_id adf4350_of_match[] = {
  522. { .compatible = "adi,adf4350", },
  523. { .compatible = "adi,adf4351", },
  524. { /* sentinel */ },
  525. };
  526. MODULE_DEVICE_TABLE(of, adf4350_of_match);
  527. static const struct spi_device_id adf4350_id[] = {
  528. {"adf4350", 4350},
  529. {"adf4351", 4351},
  530. {}
  531. };
  532. MODULE_DEVICE_TABLE(spi, adf4350_id);
  533. static struct spi_driver adf4350_driver = {
  534. .driver = {
  535. .name = "adf4350",
  536. .of_match_table = of_match_ptr(adf4350_of_match),
  537. },
  538. .probe = adf4350_probe,
  539. .remove = adf4350_remove,
  540. .id_table = adf4350_id,
  541. };
  542. module_spi_driver(adf4350_driver);
  543. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  544. MODULE_DESCRIPTION("Analog Devices ADF4350/ADF4351 PLL");
  545. MODULE_LICENSE("GPL v2");