clkgen-fsyn.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /*
  2. * Copyright (C) 2014 STMicroelectronics R&D Ltd
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. */
  9. /*
  10. * Authors:
  11. * Stephen Gallimore <stephen.gallimore@st.com>,
  12. * Pankaj Dev <pankaj.dev@st.com>.
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/of_address.h>
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include "clkgen.h"
  19. /*
  20. * Maximum input clock to the PLL before we divide it down by 2
  21. * although in reality in actual systems this has never been seen to
  22. * be used.
  23. */
  24. #define QUADFS_NDIV_THRESHOLD 30000000
  25. #define PLL_BW_GOODREF (0L)
  26. #define PLL_BW_VBADREF (1L)
  27. #define PLL_BW_BADREF (2L)
  28. #define PLL_BW_VGOODREF (3L)
  29. #define QUADFS_MAX_CHAN 4
  30. struct stm_fs {
  31. unsigned long ndiv;
  32. unsigned long mdiv;
  33. unsigned long pe;
  34. unsigned long sdiv;
  35. unsigned long nsdiv;
  36. };
  37. struct clkgen_quadfs_data {
  38. bool reset_present;
  39. bool bwfilter_present;
  40. bool lockstatus_present;
  41. bool powerup_polarity;
  42. bool standby_polarity;
  43. bool nsdiv_present;
  44. bool nrst_present;
  45. struct clkgen_field ndiv;
  46. struct clkgen_field ref_bw;
  47. struct clkgen_field nreset;
  48. struct clkgen_field npda;
  49. struct clkgen_field lock_status;
  50. struct clkgen_field nrst[QUADFS_MAX_CHAN];
  51. struct clkgen_field nsb[QUADFS_MAX_CHAN];
  52. struct clkgen_field en[QUADFS_MAX_CHAN];
  53. struct clkgen_field mdiv[QUADFS_MAX_CHAN];
  54. struct clkgen_field pe[QUADFS_MAX_CHAN];
  55. struct clkgen_field sdiv[QUADFS_MAX_CHAN];
  56. struct clkgen_field nsdiv[QUADFS_MAX_CHAN];
  57. const struct clk_ops *pll_ops;
  58. int (*get_params)(unsigned long, unsigned long, struct stm_fs *);
  59. int (*get_rate)(unsigned long , const struct stm_fs *,
  60. unsigned long *);
  61. };
  62. static const struct clk_ops st_quadfs_pll_c32_ops;
  63. static const struct clk_ops st_quadfs_fs660c32_ops;
  64. static int clk_fs660c32_dig_get_params(unsigned long input,
  65. unsigned long output, struct stm_fs *fs);
  66. static int clk_fs660c32_dig_get_rate(unsigned long, const struct stm_fs *,
  67. unsigned long *);
  68. static const struct clkgen_quadfs_data st_fs660c32_C = {
  69. .nrst_present = true,
  70. .nrst = { CLKGEN_FIELD(0x2f0, 0x1, 0),
  71. CLKGEN_FIELD(0x2f0, 0x1, 1),
  72. CLKGEN_FIELD(0x2f0, 0x1, 2),
  73. CLKGEN_FIELD(0x2f0, 0x1, 3) },
  74. .npda = CLKGEN_FIELD(0x2f0, 0x1, 12),
  75. .nsb = { CLKGEN_FIELD(0x2f0, 0x1, 8),
  76. CLKGEN_FIELD(0x2f0, 0x1, 9),
  77. CLKGEN_FIELD(0x2f0, 0x1, 10),
  78. CLKGEN_FIELD(0x2f0, 0x1, 11) },
  79. .nsdiv_present = true,
  80. .nsdiv = { CLKGEN_FIELD(0x304, 0x1, 24),
  81. CLKGEN_FIELD(0x308, 0x1, 24),
  82. CLKGEN_FIELD(0x30c, 0x1, 24),
  83. CLKGEN_FIELD(0x310, 0x1, 24) },
  84. .mdiv = { CLKGEN_FIELD(0x304, 0x1f, 15),
  85. CLKGEN_FIELD(0x308, 0x1f, 15),
  86. CLKGEN_FIELD(0x30c, 0x1f, 15),
  87. CLKGEN_FIELD(0x310, 0x1f, 15) },
  88. .en = { CLKGEN_FIELD(0x2fc, 0x1, 0),
  89. CLKGEN_FIELD(0x2fc, 0x1, 1),
  90. CLKGEN_FIELD(0x2fc, 0x1, 2),
  91. CLKGEN_FIELD(0x2fc, 0x1, 3) },
  92. .ndiv = CLKGEN_FIELD(0x2f4, 0x7, 16),
  93. .pe = { CLKGEN_FIELD(0x304, 0x7fff, 0),
  94. CLKGEN_FIELD(0x308, 0x7fff, 0),
  95. CLKGEN_FIELD(0x30c, 0x7fff, 0),
  96. CLKGEN_FIELD(0x310, 0x7fff, 0) },
  97. .sdiv = { CLKGEN_FIELD(0x304, 0xf, 20),
  98. CLKGEN_FIELD(0x308, 0xf, 20),
  99. CLKGEN_FIELD(0x30c, 0xf, 20),
  100. CLKGEN_FIELD(0x310, 0xf, 20) },
  101. .lockstatus_present = true,
  102. .lock_status = CLKGEN_FIELD(0x2f0, 0x1, 24),
  103. .powerup_polarity = 1,
  104. .standby_polarity = 1,
  105. .pll_ops = &st_quadfs_pll_c32_ops,
  106. .get_params = clk_fs660c32_dig_get_params,
  107. .get_rate = clk_fs660c32_dig_get_rate,
  108. };
  109. static const struct clkgen_quadfs_data st_fs660c32_D = {
  110. .nrst_present = true,
  111. .nrst = { CLKGEN_FIELD(0x2a0, 0x1, 0),
  112. CLKGEN_FIELD(0x2a0, 0x1, 1),
  113. CLKGEN_FIELD(0x2a0, 0x1, 2),
  114. CLKGEN_FIELD(0x2a0, 0x1, 3) },
  115. .ndiv = CLKGEN_FIELD(0x2a4, 0x7, 16),
  116. .pe = { CLKGEN_FIELD(0x2b4, 0x7fff, 0),
  117. CLKGEN_FIELD(0x2b8, 0x7fff, 0),
  118. CLKGEN_FIELD(0x2bc, 0x7fff, 0),
  119. CLKGEN_FIELD(0x2c0, 0x7fff, 0) },
  120. .sdiv = { CLKGEN_FIELD(0x2b4, 0xf, 20),
  121. CLKGEN_FIELD(0x2b8, 0xf, 20),
  122. CLKGEN_FIELD(0x2bc, 0xf, 20),
  123. CLKGEN_FIELD(0x2c0, 0xf, 20) },
  124. .npda = CLKGEN_FIELD(0x2a0, 0x1, 12),
  125. .nsb = { CLKGEN_FIELD(0x2a0, 0x1, 8),
  126. CLKGEN_FIELD(0x2a0, 0x1, 9),
  127. CLKGEN_FIELD(0x2a0, 0x1, 10),
  128. CLKGEN_FIELD(0x2a0, 0x1, 11) },
  129. .nsdiv_present = true,
  130. .nsdiv = { CLKGEN_FIELD(0x2b4, 0x1, 24),
  131. CLKGEN_FIELD(0x2b8, 0x1, 24),
  132. CLKGEN_FIELD(0x2bc, 0x1, 24),
  133. CLKGEN_FIELD(0x2c0, 0x1, 24) },
  134. .mdiv = { CLKGEN_FIELD(0x2b4, 0x1f, 15),
  135. CLKGEN_FIELD(0x2b8, 0x1f, 15),
  136. CLKGEN_FIELD(0x2bc, 0x1f, 15),
  137. CLKGEN_FIELD(0x2c0, 0x1f, 15) },
  138. .en = { CLKGEN_FIELD(0x2ac, 0x1, 0),
  139. CLKGEN_FIELD(0x2ac, 0x1, 1),
  140. CLKGEN_FIELD(0x2ac, 0x1, 2),
  141. CLKGEN_FIELD(0x2ac, 0x1, 3) },
  142. .lockstatus_present = true,
  143. .lock_status = CLKGEN_FIELD(0x2A0, 0x1, 24),
  144. .powerup_polarity = 1,
  145. .standby_polarity = 1,
  146. .pll_ops = &st_quadfs_pll_c32_ops,
  147. .get_params = clk_fs660c32_dig_get_params,
  148. .get_rate = clk_fs660c32_dig_get_rate,};
  149. /**
  150. * DOC: A Frequency Synthesizer that multiples its input clock by a fixed factor
  151. *
  152. * Traits of this clock:
  153. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  154. * enable - clk_enable and clk_disable are functional & control the Fsyn
  155. * rate - inherits rate from parent. set_rate/round_rate/recalc_rate
  156. * parent - fixed parent. No clk_set_parent support
  157. */
  158. /**
  159. * struct st_clk_quadfs_pll - A pll which outputs a fixed multiplier of
  160. * its parent clock, found inside a type of
  161. * ST quad channel frequency synthesizer block
  162. *
  163. * @hw: handle between common and hardware-specific interfaces.
  164. * @ndiv: regmap field for the ndiv control.
  165. * @regs_base: base address of the configuration registers.
  166. * @lock: spinlock.
  167. *
  168. */
  169. struct st_clk_quadfs_pll {
  170. struct clk_hw hw;
  171. void __iomem *regs_base;
  172. spinlock_t *lock;
  173. struct clkgen_quadfs_data *data;
  174. u32 ndiv;
  175. };
  176. #define to_quadfs_pll(_hw) container_of(_hw, struct st_clk_quadfs_pll, hw)
  177. static int quadfs_pll_enable(struct clk_hw *hw)
  178. {
  179. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  180. unsigned long flags = 0, timeout = jiffies + msecs_to_jiffies(10);
  181. if (pll->lock)
  182. spin_lock_irqsave(pll->lock, flags);
  183. /*
  184. * Bring block out of reset if we have reset control.
  185. */
  186. if (pll->data->reset_present)
  187. CLKGEN_WRITE(pll, nreset, 1);
  188. /*
  189. * Use a fixed input clock noise bandwidth filter for the moment
  190. */
  191. if (pll->data->bwfilter_present)
  192. CLKGEN_WRITE(pll, ref_bw, PLL_BW_GOODREF);
  193. CLKGEN_WRITE(pll, ndiv, pll->ndiv);
  194. /*
  195. * Power up the PLL
  196. */
  197. CLKGEN_WRITE(pll, npda, !pll->data->powerup_polarity);
  198. if (pll->lock)
  199. spin_unlock_irqrestore(pll->lock, flags);
  200. if (pll->data->lockstatus_present)
  201. while (!CLKGEN_READ(pll, lock_status)) {
  202. if (time_after(jiffies, timeout))
  203. return -ETIMEDOUT;
  204. cpu_relax();
  205. }
  206. return 0;
  207. }
  208. static void quadfs_pll_disable(struct clk_hw *hw)
  209. {
  210. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  211. unsigned long flags = 0;
  212. if (pll->lock)
  213. spin_lock_irqsave(pll->lock, flags);
  214. /*
  215. * Powerdown the PLL and then put block into soft reset if we have
  216. * reset control.
  217. */
  218. CLKGEN_WRITE(pll, npda, pll->data->powerup_polarity);
  219. if (pll->data->reset_present)
  220. CLKGEN_WRITE(pll, nreset, 0);
  221. if (pll->lock)
  222. spin_unlock_irqrestore(pll->lock, flags);
  223. }
  224. static int quadfs_pll_is_enabled(struct clk_hw *hw)
  225. {
  226. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  227. u32 npda = CLKGEN_READ(pll, npda);
  228. return pll->data->powerup_polarity ? !npda : !!npda;
  229. }
  230. static int clk_fs660c32_vco_get_rate(unsigned long input, struct stm_fs *fs,
  231. unsigned long *rate)
  232. {
  233. unsigned long nd = fs->ndiv + 16; /* ndiv value */
  234. *rate = input * nd;
  235. return 0;
  236. }
  237. static unsigned long quadfs_pll_fs660c32_recalc_rate(struct clk_hw *hw,
  238. unsigned long parent_rate)
  239. {
  240. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  241. unsigned long rate = 0;
  242. struct stm_fs params;
  243. params.ndiv = CLKGEN_READ(pll, ndiv);
  244. if (clk_fs660c32_vco_get_rate(parent_rate, &params, &rate))
  245. pr_err("%s:%s error calculating rate\n",
  246. clk_hw_get_name(hw), __func__);
  247. pll->ndiv = params.ndiv;
  248. return rate;
  249. }
  250. static int clk_fs660c32_vco_get_params(unsigned long input,
  251. unsigned long output, struct stm_fs *fs)
  252. {
  253. /* Formula
  254. VCO frequency = (fin x ndiv) / pdiv
  255. ndiv = VCOfreq * pdiv / fin
  256. */
  257. unsigned long pdiv = 1, n;
  258. /* Output clock range: 384Mhz to 660Mhz */
  259. if (output < 384000000 || output > 660000000)
  260. return -EINVAL;
  261. if (input > 40000000)
  262. /* This means that PDIV would be 2 instead of 1.
  263. Not supported today. */
  264. return -EINVAL;
  265. input /= 1000;
  266. output /= 1000;
  267. n = output * pdiv / input;
  268. if (n < 16)
  269. n = 16;
  270. fs->ndiv = n - 16; /* Converting formula value to reg value */
  271. return 0;
  272. }
  273. static long quadfs_pll_fs660c32_round_rate(struct clk_hw *hw,
  274. unsigned long rate,
  275. unsigned long *prate)
  276. {
  277. struct stm_fs params;
  278. if (clk_fs660c32_vco_get_params(*prate, rate, &params))
  279. return rate;
  280. clk_fs660c32_vco_get_rate(*prate, &params, &rate);
  281. pr_debug("%s: %s new rate %ld [ndiv=%u]\n",
  282. __func__, clk_hw_get_name(hw),
  283. rate, (unsigned int)params.ndiv);
  284. return rate;
  285. }
  286. static int quadfs_pll_fs660c32_set_rate(struct clk_hw *hw, unsigned long rate,
  287. unsigned long parent_rate)
  288. {
  289. struct st_clk_quadfs_pll *pll = to_quadfs_pll(hw);
  290. struct stm_fs params;
  291. long hwrate = 0;
  292. unsigned long flags = 0;
  293. int ret;
  294. if (!rate || !parent_rate)
  295. return -EINVAL;
  296. ret = clk_fs660c32_vco_get_params(parent_rate, rate, &params);
  297. if (ret)
  298. return ret;
  299. clk_fs660c32_vco_get_rate(parent_rate, &params, &hwrate);
  300. pr_debug("%s: %s new rate %ld [ndiv=0x%x]\n",
  301. __func__, clk_hw_get_name(hw),
  302. hwrate, (unsigned int)params.ndiv);
  303. if (!hwrate)
  304. return -EINVAL;
  305. pll->ndiv = params.ndiv;
  306. if (pll->lock)
  307. spin_lock_irqsave(pll->lock, flags);
  308. CLKGEN_WRITE(pll, ndiv, pll->ndiv);
  309. if (pll->lock)
  310. spin_unlock_irqrestore(pll->lock, flags);
  311. return 0;
  312. }
  313. static const struct clk_ops st_quadfs_pll_c32_ops = {
  314. .enable = quadfs_pll_enable,
  315. .disable = quadfs_pll_disable,
  316. .is_enabled = quadfs_pll_is_enabled,
  317. .recalc_rate = quadfs_pll_fs660c32_recalc_rate,
  318. .round_rate = quadfs_pll_fs660c32_round_rate,
  319. .set_rate = quadfs_pll_fs660c32_set_rate,
  320. };
  321. static struct clk * __init st_clk_register_quadfs_pll(
  322. const char *name, const char *parent_name,
  323. struct clkgen_quadfs_data *quadfs, void __iomem *reg,
  324. spinlock_t *lock)
  325. {
  326. struct st_clk_quadfs_pll *pll;
  327. struct clk *clk;
  328. struct clk_init_data init;
  329. /*
  330. * Sanity check required pointers.
  331. */
  332. if (WARN_ON(!name || !parent_name))
  333. return ERR_PTR(-EINVAL);
  334. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  335. if (!pll)
  336. return ERR_PTR(-ENOMEM);
  337. init.name = name;
  338. init.ops = quadfs->pll_ops;
  339. init.flags = CLK_IS_BASIC | CLK_GET_RATE_NOCACHE;
  340. init.parent_names = &parent_name;
  341. init.num_parents = 1;
  342. pll->data = quadfs;
  343. pll->regs_base = reg;
  344. pll->lock = lock;
  345. pll->hw.init = &init;
  346. clk = clk_register(NULL, &pll->hw);
  347. if (IS_ERR(clk))
  348. kfree(pll);
  349. return clk;
  350. }
  351. /**
  352. * DOC: A digital frequency synthesizer
  353. *
  354. * Traits of this clock:
  355. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  356. * enable - clk_enable and clk_disable are functional
  357. * rate - set rate is functional
  358. * parent - fixed parent. No clk_set_parent support
  359. */
  360. /**
  361. * struct st_clk_quadfs_fsynth - One clock output from a four channel digital
  362. * frequency synthesizer (fsynth) block.
  363. *
  364. * @hw: handle between common and hardware-specific interfaces
  365. *
  366. * @nsb: regmap field in the output control register for the digital
  367. * standby of this fsynth channel. This control is active low so
  368. * the channel is in standby when the control bit is cleared.
  369. *
  370. * @nsdiv: regmap field in the output control register for
  371. * for the optional divide by 3 of this fsynth channel. This control
  372. * is active low so the divide by 3 is active when the control bit is
  373. * cleared and the divide is bypassed when the bit is set.
  374. */
  375. struct st_clk_quadfs_fsynth {
  376. struct clk_hw hw;
  377. void __iomem *regs_base;
  378. spinlock_t *lock;
  379. struct clkgen_quadfs_data *data;
  380. u32 chan;
  381. /*
  382. * Cached hardware values from set_rate so we can program the
  383. * hardware in enable. There are two reasons for this:
  384. *
  385. * 1. The registers may not be writable until the parent has been
  386. * enabled.
  387. *
  388. * 2. It restores the clock rate when a driver does an enable
  389. * on PM restore, after a suspend to RAM has lost the hardware
  390. * setup.
  391. */
  392. u32 md;
  393. u32 pe;
  394. u32 sdiv;
  395. u32 nsdiv;
  396. };
  397. #define to_quadfs_fsynth(_hw) \
  398. container_of(_hw, struct st_clk_quadfs_fsynth, hw)
  399. static void quadfs_fsynth_program_enable(struct st_clk_quadfs_fsynth *fs)
  400. {
  401. /*
  402. * Pulse the program enable register lsb to make the hardware take
  403. * notice of the new md/pe values with a glitchless transition.
  404. */
  405. CLKGEN_WRITE(fs, en[fs->chan], 1);
  406. CLKGEN_WRITE(fs, en[fs->chan], 0);
  407. }
  408. static void quadfs_fsynth_program_rate(struct st_clk_quadfs_fsynth *fs)
  409. {
  410. unsigned long flags = 0;
  411. /*
  412. * Ensure the md/pe parameters are ignored while we are
  413. * reprogramming them so we can get a glitchless change
  414. * when fine tuning the speed of a running clock.
  415. */
  416. CLKGEN_WRITE(fs, en[fs->chan], 0);
  417. CLKGEN_WRITE(fs, mdiv[fs->chan], fs->md);
  418. CLKGEN_WRITE(fs, pe[fs->chan], fs->pe);
  419. CLKGEN_WRITE(fs, sdiv[fs->chan], fs->sdiv);
  420. if (fs->lock)
  421. spin_lock_irqsave(fs->lock, flags);
  422. if (fs->data->nsdiv_present)
  423. CLKGEN_WRITE(fs, nsdiv[fs->chan], fs->nsdiv);
  424. if (fs->lock)
  425. spin_unlock_irqrestore(fs->lock, flags);
  426. }
  427. static int quadfs_fsynth_enable(struct clk_hw *hw)
  428. {
  429. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  430. unsigned long flags = 0;
  431. pr_debug("%s: %s\n", __func__, clk_hw_get_name(hw));
  432. quadfs_fsynth_program_rate(fs);
  433. if (fs->lock)
  434. spin_lock_irqsave(fs->lock, flags);
  435. CLKGEN_WRITE(fs, nsb[fs->chan], !fs->data->standby_polarity);
  436. if (fs->data->nrst_present)
  437. CLKGEN_WRITE(fs, nrst[fs->chan], 0);
  438. if (fs->lock)
  439. spin_unlock_irqrestore(fs->lock, flags);
  440. quadfs_fsynth_program_enable(fs);
  441. return 0;
  442. }
  443. static void quadfs_fsynth_disable(struct clk_hw *hw)
  444. {
  445. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  446. unsigned long flags = 0;
  447. pr_debug("%s: %s\n", __func__, clk_hw_get_name(hw));
  448. if (fs->lock)
  449. spin_lock_irqsave(fs->lock, flags);
  450. CLKGEN_WRITE(fs, nsb[fs->chan], fs->data->standby_polarity);
  451. if (fs->lock)
  452. spin_unlock_irqrestore(fs->lock, flags);
  453. }
  454. static int quadfs_fsynth_is_enabled(struct clk_hw *hw)
  455. {
  456. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  457. u32 nsb = CLKGEN_READ(fs, nsb[fs->chan]);
  458. pr_debug("%s: %s enable bit = 0x%x\n",
  459. __func__, clk_hw_get_name(hw), nsb);
  460. return fs->data->standby_polarity ? !nsb : !!nsb;
  461. }
  462. #define P20 (uint64_t)(1 << 20)
  463. static int clk_fs660c32_dig_get_rate(unsigned long input,
  464. const struct stm_fs *fs, unsigned long *rate)
  465. {
  466. unsigned long s = (1 << fs->sdiv);
  467. unsigned long ns;
  468. uint64_t res;
  469. /*
  470. * 'nsdiv' is a register value ('BIN') which is translated
  471. * to a decimal value according to following rules.
  472. *
  473. * nsdiv ns.dec
  474. * 0 3
  475. * 1 1
  476. */
  477. ns = (fs->nsdiv == 1) ? 1 : 3;
  478. res = (P20 * (32 + fs->mdiv) + 32 * fs->pe) * s * ns;
  479. *rate = (unsigned long)div64_u64(input * P20 * 32, res);
  480. return 0;
  481. }
  482. static int clk_fs660c32_get_pe(int m, int si, unsigned long *deviation,
  483. signed long input, unsigned long output, uint64_t *p,
  484. struct stm_fs *fs)
  485. {
  486. unsigned long new_freq, new_deviation;
  487. struct stm_fs fs_tmp;
  488. uint64_t val;
  489. val = (uint64_t)output << si;
  490. *p = (uint64_t)input * P20 - (32LL + (uint64_t)m) * val * (P20 / 32LL);
  491. *p = div64_u64(*p, val);
  492. if (*p > 32767LL)
  493. return 1;
  494. fs_tmp.mdiv = (unsigned long) m;
  495. fs_tmp.pe = (unsigned long)*p;
  496. fs_tmp.sdiv = si;
  497. fs_tmp.nsdiv = 1;
  498. clk_fs660c32_dig_get_rate(input, &fs_tmp, &new_freq);
  499. new_deviation = abs(output - new_freq);
  500. if (new_deviation < *deviation) {
  501. fs->mdiv = m;
  502. fs->pe = (unsigned long)*p;
  503. fs->sdiv = si;
  504. fs->nsdiv = 1;
  505. *deviation = new_deviation;
  506. }
  507. return 0;
  508. }
  509. static int clk_fs660c32_dig_get_params(unsigned long input,
  510. unsigned long output, struct stm_fs *fs)
  511. {
  512. int si; /* sdiv_reg (8 downto 0) */
  513. int m; /* md value */
  514. unsigned long new_freq, new_deviation;
  515. /* initial condition to say: "infinite deviation" */
  516. unsigned long deviation = ~0;
  517. uint64_t p, p1, p2; /* pe value */
  518. int r1, r2;
  519. struct stm_fs fs_tmp;
  520. for (si = 0; (si <= 8) && deviation; si++) {
  521. /* Boundary test to avoid useless iteration */
  522. r1 = clk_fs660c32_get_pe(0, si, &deviation,
  523. input, output, &p1, fs);
  524. r2 = clk_fs660c32_get_pe(31, si, &deviation,
  525. input, output, &p2, fs);
  526. /* No solution */
  527. if (r1 && r2 && (p1 > p2))
  528. continue;
  529. /* Try to find best deviation */
  530. for (m = 1; (m < 31) && deviation; m++)
  531. clk_fs660c32_get_pe(m, si, &deviation,
  532. input, output, &p, fs);
  533. }
  534. if (deviation == ~0) /* No solution found */
  535. return -1;
  536. /* pe fine tuning if deviation not 0: +/- 2 around computed pe value */
  537. if (deviation) {
  538. fs_tmp.mdiv = fs->mdiv;
  539. fs_tmp.sdiv = fs->sdiv;
  540. fs_tmp.nsdiv = fs->nsdiv;
  541. if (fs->pe > 2)
  542. p2 = fs->pe - 2;
  543. else
  544. p2 = 0;
  545. for (; p2 < 32768ll && (p2 <= (fs->pe + 2)); p2++) {
  546. fs_tmp.pe = (unsigned long)p2;
  547. clk_fs660c32_dig_get_rate(input, &fs_tmp, &new_freq);
  548. new_deviation = abs(output - new_freq);
  549. /* Check if this is a better solution */
  550. if (new_deviation < deviation) {
  551. fs->pe = (unsigned long)p2;
  552. deviation = new_deviation;
  553. }
  554. }
  555. }
  556. return 0;
  557. }
  558. static int quadfs_fsynt_get_hw_value_for_recalc(struct st_clk_quadfs_fsynth *fs,
  559. struct stm_fs *params)
  560. {
  561. /*
  562. * Get the initial hardware values for recalc_rate
  563. */
  564. params->mdiv = CLKGEN_READ(fs, mdiv[fs->chan]);
  565. params->pe = CLKGEN_READ(fs, pe[fs->chan]);
  566. params->sdiv = CLKGEN_READ(fs, sdiv[fs->chan]);
  567. if (fs->data->nsdiv_present)
  568. params->nsdiv = CLKGEN_READ(fs, nsdiv[fs->chan]);
  569. else
  570. params->nsdiv = 1;
  571. /*
  572. * If All are NULL then assume no clock rate is programmed.
  573. */
  574. if (!params->mdiv && !params->pe && !params->sdiv)
  575. return 1;
  576. fs->md = params->mdiv;
  577. fs->pe = params->pe;
  578. fs->sdiv = params->sdiv;
  579. fs->nsdiv = params->nsdiv;
  580. return 0;
  581. }
  582. static long quadfs_find_best_rate(struct clk_hw *hw, unsigned long drate,
  583. unsigned long prate, struct stm_fs *params)
  584. {
  585. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  586. int (*clk_fs_get_rate)(unsigned long ,
  587. const struct stm_fs *, unsigned long *);
  588. int (*clk_fs_get_params)(unsigned long, unsigned long, struct stm_fs *);
  589. unsigned long rate = 0;
  590. clk_fs_get_rate = fs->data->get_rate;
  591. clk_fs_get_params = fs->data->get_params;
  592. if (!clk_fs_get_params(prate, drate, params))
  593. clk_fs_get_rate(prate, params, &rate);
  594. return rate;
  595. }
  596. static unsigned long quadfs_recalc_rate(struct clk_hw *hw,
  597. unsigned long parent_rate)
  598. {
  599. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  600. unsigned long rate = 0;
  601. struct stm_fs params;
  602. int (*clk_fs_get_rate)(unsigned long ,
  603. const struct stm_fs *, unsigned long *);
  604. clk_fs_get_rate = fs->data->get_rate;
  605. if (quadfs_fsynt_get_hw_value_for_recalc(fs, &params))
  606. return 0;
  607. if (clk_fs_get_rate(parent_rate, &params, &rate)) {
  608. pr_err("%s:%s error calculating rate\n",
  609. clk_hw_get_name(hw), __func__);
  610. }
  611. pr_debug("%s:%s rate %lu\n", clk_hw_get_name(hw), __func__, rate);
  612. return rate;
  613. }
  614. static long quadfs_round_rate(struct clk_hw *hw, unsigned long rate,
  615. unsigned long *prate)
  616. {
  617. struct stm_fs params;
  618. rate = quadfs_find_best_rate(hw, rate, *prate, &params);
  619. pr_debug("%s: %s new rate %ld [sdiv=0x%x,md=0x%x,pe=0x%x,nsdiv3=%u]\n",
  620. __func__, clk_hw_get_name(hw),
  621. rate, (unsigned int)params.sdiv, (unsigned int)params.mdiv,
  622. (unsigned int)params.pe, (unsigned int)params.nsdiv);
  623. return rate;
  624. }
  625. static void quadfs_program_and_enable(struct st_clk_quadfs_fsynth *fs,
  626. struct stm_fs *params)
  627. {
  628. fs->md = params->mdiv;
  629. fs->pe = params->pe;
  630. fs->sdiv = params->sdiv;
  631. fs->nsdiv = params->nsdiv;
  632. /*
  633. * In some integrations you can only change the fsynth programming when
  634. * the parent entity containing it is enabled.
  635. */
  636. quadfs_fsynth_program_rate(fs);
  637. quadfs_fsynth_program_enable(fs);
  638. }
  639. static int quadfs_set_rate(struct clk_hw *hw, unsigned long rate,
  640. unsigned long parent_rate)
  641. {
  642. struct st_clk_quadfs_fsynth *fs = to_quadfs_fsynth(hw);
  643. struct stm_fs params;
  644. long hwrate;
  645. int uninitialized_var(i);
  646. if (!rate || !parent_rate)
  647. return -EINVAL;
  648. memset(&params, 0, sizeof(struct stm_fs));
  649. hwrate = quadfs_find_best_rate(hw, rate, parent_rate, &params);
  650. if (!hwrate)
  651. return -EINVAL;
  652. quadfs_program_and_enable(fs, &params);
  653. return 0;
  654. }
  655. static const struct clk_ops st_quadfs_ops = {
  656. .enable = quadfs_fsynth_enable,
  657. .disable = quadfs_fsynth_disable,
  658. .is_enabled = quadfs_fsynth_is_enabled,
  659. .round_rate = quadfs_round_rate,
  660. .set_rate = quadfs_set_rate,
  661. .recalc_rate = quadfs_recalc_rate,
  662. };
  663. static struct clk * __init st_clk_register_quadfs_fsynth(
  664. const char *name, const char *parent_name,
  665. struct clkgen_quadfs_data *quadfs, void __iomem *reg, u32 chan,
  666. unsigned long flags, spinlock_t *lock)
  667. {
  668. struct st_clk_quadfs_fsynth *fs;
  669. struct clk *clk;
  670. struct clk_init_data init;
  671. /*
  672. * Sanity check required pointers, note that nsdiv3 is optional.
  673. */
  674. if (WARN_ON(!name || !parent_name))
  675. return ERR_PTR(-EINVAL);
  676. fs = kzalloc(sizeof(*fs), GFP_KERNEL);
  677. if (!fs)
  678. return ERR_PTR(-ENOMEM);
  679. init.name = name;
  680. init.ops = &st_quadfs_ops;
  681. init.flags = flags | CLK_GET_RATE_NOCACHE | CLK_IS_BASIC;
  682. init.parent_names = &parent_name;
  683. init.num_parents = 1;
  684. fs->data = quadfs;
  685. fs->regs_base = reg;
  686. fs->chan = chan;
  687. fs->lock = lock;
  688. fs->hw.init = &init;
  689. clk = clk_register(NULL, &fs->hw);
  690. if (IS_ERR(clk))
  691. kfree(fs);
  692. return clk;
  693. }
  694. static void __init st_of_create_quadfs_fsynths(
  695. struct device_node *np, const char *pll_name,
  696. struct clkgen_quadfs_data *quadfs, void __iomem *reg,
  697. spinlock_t *lock)
  698. {
  699. struct clk_onecell_data *clk_data;
  700. int fschan;
  701. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  702. if (!clk_data)
  703. return;
  704. clk_data->clk_num = QUADFS_MAX_CHAN;
  705. clk_data->clks = kzalloc(QUADFS_MAX_CHAN * sizeof(struct clk *),
  706. GFP_KERNEL);
  707. if (!clk_data->clks) {
  708. kfree(clk_data);
  709. return;
  710. }
  711. for (fschan = 0; fschan < QUADFS_MAX_CHAN; fschan++) {
  712. struct clk *clk;
  713. const char *clk_name;
  714. unsigned long flags = 0;
  715. if (of_property_read_string_index(np, "clock-output-names",
  716. fschan, &clk_name)) {
  717. break;
  718. }
  719. /*
  720. * If we read an empty clock name then the channel is unused
  721. */
  722. if (*clk_name == '\0')
  723. continue;
  724. of_clk_detect_critical(np, fschan, &flags);
  725. clk = st_clk_register_quadfs_fsynth(clk_name, pll_name,
  726. quadfs, reg, fschan,
  727. flags, lock);
  728. /*
  729. * If there was an error registering this clock output, clean
  730. * up and move on to the next one.
  731. */
  732. if (!IS_ERR(clk)) {
  733. clk_data->clks[fschan] = clk;
  734. pr_debug("%s: parent %s rate %u\n",
  735. __clk_get_name(clk),
  736. __clk_get_name(clk_get_parent(clk)),
  737. (unsigned int)clk_get_rate(clk));
  738. }
  739. }
  740. of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  741. }
  742. static void __init st_of_quadfs_setup(struct device_node *np,
  743. struct clkgen_quadfs_data *data)
  744. {
  745. struct clk *clk;
  746. const char *pll_name, *clk_parent_name;
  747. void __iomem *reg;
  748. spinlock_t *lock;
  749. reg = of_iomap(np, 0);
  750. if (!reg)
  751. return;
  752. clk_parent_name = of_clk_get_parent_name(np, 0);
  753. if (!clk_parent_name)
  754. return;
  755. pll_name = kasprintf(GFP_KERNEL, "%s.pll", np->name);
  756. if (!pll_name)
  757. return;
  758. lock = kzalloc(sizeof(*lock), GFP_KERNEL);
  759. if (!lock)
  760. goto err_exit;
  761. spin_lock_init(lock);
  762. clk = st_clk_register_quadfs_pll(pll_name, clk_parent_name, data,
  763. reg, lock);
  764. if (IS_ERR(clk))
  765. goto err_exit;
  766. else
  767. pr_debug("%s: parent %s rate %u\n",
  768. __clk_get_name(clk),
  769. __clk_get_name(clk_get_parent(clk)),
  770. (unsigned int)clk_get_rate(clk));
  771. st_of_create_quadfs_fsynths(np, pll_name, data, reg, lock);
  772. err_exit:
  773. kfree(pll_name); /* No longer need local copy of the PLL name */
  774. }
  775. static void __init st_of_quadfs660C_setup(struct device_node *np)
  776. {
  777. st_of_quadfs_setup(np, (struct clkgen_quadfs_data *) &st_fs660c32_C);
  778. }
  779. CLK_OF_DECLARE(quadfs660C, "st,quadfs-pll", st_of_quadfs660C_setup);
  780. static void __init st_of_quadfs660D_setup(struct device_node *np)
  781. {
  782. st_of_quadfs_setup(np, (struct clkgen_quadfs_data *) &st_fs660c32_D);
  783. }
  784. CLK_OF_DECLARE(quadfs660D, "st,quadfs", st_of_quadfs660D_setup);