adpll.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License as
  4. * published by the Free Software Foundation version 2.
  5. *
  6. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  7. * kind, whether express or implied; without even the implied warranty
  8. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/clkdev.h>
  13. #include <linux/clk-provider.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/math64.h>
  17. #include <linux/module.h>
  18. #include <linux/of_device.h>
  19. #include <linux/string.h>
  20. #define ADPLL_PLLSS_MMR_LOCK_OFFSET 0x00 /* Managed by MPPULL */
  21. #define ADPLL_PLLSS_MMR_LOCK_ENABLED 0x1f125B64
  22. #define ADPLL_PLLSS_MMR_UNLOCK_MAGIC 0x1eda4c3d
  23. #define ADPLL_PWRCTRL_OFFSET 0x00
  24. #define ADPLL_PWRCTRL_PONIN 5
  25. #define ADPLL_PWRCTRL_PGOODIN 4
  26. #define ADPLL_PWRCTRL_RET 3
  27. #define ADPLL_PWRCTRL_ISORET 2
  28. #define ADPLL_PWRCTRL_ISOSCAN 1
  29. #define ADPLL_PWRCTRL_OFFMODE 0
  30. #define ADPLL_CLKCTRL_OFFSET 0x04
  31. #define ADPLL_CLKCTRL_CLKDCOLDOEN 29
  32. #define ADPLL_CLKCTRL_IDLE 23
  33. #define ADPLL_CLKCTRL_CLKOUTEN 20
  34. #define ADPLL_CLKINPHIFSEL_ADPLL_S 19 /* REVISIT: which bit? */
  35. #define ADPLL_CLKCTRL_CLKOUTLDOEN_ADPLL_LJ 19
  36. #define ADPLL_CLKCTRL_ULOWCLKEN 18
  37. #define ADPLL_CLKCTRL_CLKDCOLDOPWDNZ 17
  38. #define ADPLL_CLKCTRL_M2PWDNZ 16
  39. #define ADPLL_CLKCTRL_M3PWDNZ_ADPLL_S 15
  40. #define ADPLL_CLKCTRL_LOWCURRSTDBY_ADPLL_S 13
  41. #define ADPLL_CLKCTRL_LPMODE_ADPLL_S 12
  42. #define ADPLL_CLKCTRL_REGM4XEN_ADPLL_S 10
  43. #define ADPLL_CLKCTRL_SELFREQDCO_ADPLL_LJ 10
  44. #define ADPLL_CLKCTRL_TINITZ 0
  45. #define ADPLL_TENABLE_OFFSET 0x08
  46. #define ADPLL_TENABLEDIV_OFFSET 0x8c
  47. #define ADPLL_M2NDIV_OFFSET 0x10
  48. #define ADPLL_M2NDIV_M2 16
  49. #define ADPLL_M2NDIV_M2_ADPLL_S_WIDTH 5
  50. #define ADPLL_M2NDIV_M2_ADPLL_LJ_WIDTH 7
  51. #define ADPLL_MN2DIV_OFFSET 0x14
  52. #define ADPLL_MN2DIV_N2 16
  53. #define ADPLL_FRACDIV_OFFSET 0x18
  54. #define ADPLL_FRACDIV_REGSD 24
  55. #define ADPLL_FRACDIV_FRACTIONALM 0
  56. #define ADPLL_FRACDIV_FRACTIONALM_MASK 0x3ffff
  57. #define ADPLL_BWCTRL_OFFSET 0x1c
  58. #define ADPLL_BWCTRL_BWCONTROL 1
  59. #define ADPLL_BWCTRL_BW_INCR_DECRZ 0
  60. #define ADPLL_RESERVED_OFFSET 0x20
  61. #define ADPLL_STATUS_OFFSET 0x24
  62. #define ADPLL_STATUS_PONOUT 31
  63. #define ADPLL_STATUS_PGOODOUT 30
  64. #define ADPLL_STATUS_LDOPWDN 29
  65. #define ADPLL_STATUS_RECAL_BSTATUS3 28
  66. #define ADPLL_STATUS_RECAL_OPPIN 27
  67. #define ADPLL_STATUS_PHASELOCK 10
  68. #define ADPLL_STATUS_FREQLOCK 9
  69. #define ADPLL_STATUS_BYPASSACK 8
  70. #define ADPLL_STATUS_LOSSREF 6
  71. #define ADPLL_STATUS_CLKOUTENACK 5
  72. #define ADPLL_STATUS_LOCK2 4
  73. #define ADPLL_STATUS_M2CHANGEACK 3
  74. #define ADPLL_STATUS_HIGHJITTER 1
  75. #define ADPLL_STATUS_BYPASS 0
  76. #define ADPLL_STATUS_PREPARED_MASK (BIT(ADPLL_STATUS_PHASELOCK) | \
  77. BIT(ADPLL_STATUS_FREQLOCK))
  78. #define ADPLL_M3DIV_OFFSET 0x28 /* Only on MPUPLL */
  79. #define ADPLL_M3DIV_M3 0
  80. #define ADPLL_M3DIV_M3_WIDTH 5
  81. #define ADPLL_M3DIV_M3_MASK 0x1f
  82. #define ADPLL_RAMPCTRL_OFFSET 0x2c /* Only on MPUPLL */
  83. #define ADPLL_RAMPCTRL_CLKRAMPLEVEL 19
  84. #define ADPLL_RAMPCTRL_CLKRAMPRATE 16
  85. #define ADPLL_RAMPCTRL_RELOCK_RAMP_EN 0
  86. #define MAX_ADPLL_INPUTS 3
  87. #define MAX_ADPLL_OUTPUTS 4
  88. #define ADPLL_MAX_RETRIES 5
  89. #define to_dco(_hw) container_of(_hw, struct ti_adpll_dco_data, hw)
  90. #define to_adpll(_hw) container_of(_hw, struct ti_adpll_data, dco)
  91. #define to_clkout(_hw) container_of(_hw, struct ti_adpll_clkout_data, hw)
  92. enum ti_adpll_clocks {
  93. TI_ADPLL_DCO,
  94. TI_ADPLL_DCO_GATE,
  95. TI_ADPLL_N2,
  96. TI_ADPLL_M2,
  97. TI_ADPLL_M2_GATE,
  98. TI_ADPLL_BYPASS,
  99. TI_ADPLL_HIF,
  100. TI_ADPLL_DIV2,
  101. TI_ADPLL_CLKOUT,
  102. TI_ADPLL_CLKOUT2,
  103. TI_ADPLL_M3,
  104. };
  105. #define TI_ADPLL_NR_CLOCKS (TI_ADPLL_M3 + 1)
  106. enum ti_adpll_inputs {
  107. TI_ADPLL_CLKINP,
  108. TI_ADPLL_CLKINPULOW,
  109. TI_ADPLL_CLKINPHIF,
  110. };
  111. enum ti_adpll_s_outputs {
  112. TI_ADPLL_S_DCOCLKLDO,
  113. TI_ADPLL_S_CLKOUT,
  114. TI_ADPLL_S_CLKOUTX2,
  115. TI_ADPLL_S_CLKOUTHIF,
  116. };
  117. enum ti_adpll_lj_outputs {
  118. TI_ADPLL_LJ_CLKDCOLDO,
  119. TI_ADPLL_LJ_CLKOUT,
  120. TI_ADPLL_LJ_CLKOUTLDO,
  121. };
  122. struct ti_adpll_platform_data {
  123. const bool is_type_s;
  124. const int nr_max_inputs;
  125. const int nr_max_outputs;
  126. const int output_index;
  127. };
  128. struct ti_adpll_clock {
  129. struct clk *clk;
  130. struct clk_lookup *cl;
  131. void (*unregister)(struct clk *clk);
  132. };
  133. struct ti_adpll_dco_data {
  134. struct clk_hw hw;
  135. };
  136. struct ti_adpll_clkout_data {
  137. struct ti_adpll_data *adpll;
  138. struct clk_gate gate;
  139. struct clk_hw hw;
  140. };
  141. struct ti_adpll_data {
  142. struct device *dev;
  143. const struct ti_adpll_platform_data *c;
  144. struct device_node *np;
  145. unsigned long pa;
  146. void __iomem *iobase;
  147. void __iomem *regs;
  148. spinlock_t lock; /* For ADPLL shared register access */
  149. const char *parent_names[MAX_ADPLL_INPUTS];
  150. struct clk *parent_clocks[MAX_ADPLL_INPUTS];
  151. struct ti_adpll_clock *clocks;
  152. struct clk_onecell_data outputs;
  153. struct ti_adpll_dco_data dco;
  154. };
  155. static const char *ti_adpll_clk_get_name(struct ti_adpll_data *d,
  156. int output_index,
  157. const char *postfix)
  158. {
  159. const char *name;
  160. int err;
  161. if (output_index >= 0) {
  162. err = of_property_read_string_index(d->np,
  163. "clock-output-names",
  164. output_index,
  165. &name);
  166. if (err)
  167. return NULL;
  168. } else {
  169. name = devm_kasprintf(d->dev, GFP_KERNEL, "%08lx.adpll.%s",
  170. d->pa, postfix);
  171. }
  172. return name;
  173. }
  174. #define ADPLL_MAX_CON_ID 16 /* See MAX_CON_ID */
  175. static int ti_adpll_setup_clock(struct ti_adpll_data *d, struct clk *clock,
  176. int index, int output_index, const char *name,
  177. void (*unregister)(struct clk *clk))
  178. {
  179. struct clk_lookup *cl;
  180. const char *postfix = NULL;
  181. char con_id[ADPLL_MAX_CON_ID];
  182. d->clocks[index].clk = clock;
  183. d->clocks[index].unregister = unregister;
  184. /* Separate con_id in format "pll040dcoclkldo" to fit MAX_CON_ID */
  185. postfix = strrchr(name, '.');
  186. if (postfix && strlen(postfix) > 1) {
  187. if (strlen(postfix) > ADPLL_MAX_CON_ID)
  188. dev_warn(d->dev, "clock %s con_id lookup may fail\n",
  189. name);
  190. snprintf(con_id, 16, "pll%03lx%s", d->pa & 0xfff, postfix + 1);
  191. cl = clkdev_create(clock, con_id, NULL);
  192. if (!cl)
  193. return -ENOMEM;
  194. d->clocks[index].cl = cl;
  195. } else {
  196. dev_warn(d->dev, "no con_id for clock %s\n", name);
  197. }
  198. if (output_index < 0)
  199. return 0;
  200. d->outputs.clks[output_index] = clock;
  201. d->outputs.clk_num++;
  202. return 0;
  203. }
  204. static int ti_adpll_init_divider(struct ti_adpll_data *d,
  205. enum ti_adpll_clocks index,
  206. int output_index, char *name,
  207. struct clk *parent_clock,
  208. void __iomem *reg,
  209. u8 shift, u8 width,
  210. u8 clk_divider_flags)
  211. {
  212. const char *child_name;
  213. const char *parent_name;
  214. struct clk *clock;
  215. child_name = ti_adpll_clk_get_name(d, output_index, name);
  216. if (!child_name)
  217. return -EINVAL;
  218. parent_name = __clk_get_name(parent_clock);
  219. clock = clk_register_divider(d->dev, child_name, parent_name, 0,
  220. reg, shift, width, clk_divider_flags,
  221. &d->lock);
  222. if (IS_ERR(clock)) {
  223. dev_err(d->dev, "failed to register divider %s: %li\n",
  224. name, PTR_ERR(clock));
  225. return PTR_ERR(clock);
  226. }
  227. return ti_adpll_setup_clock(d, clock, index, output_index, child_name,
  228. clk_unregister_divider);
  229. }
  230. static int ti_adpll_init_mux(struct ti_adpll_data *d,
  231. enum ti_adpll_clocks index,
  232. char *name, struct clk *clk0,
  233. struct clk *clk1,
  234. void __iomem *reg,
  235. u8 shift)
  236. {
  237. const char *child_name;
  238. const char *parents[2];
  239. struct clk *clock;
  240. child_name = ti_adpll_clk_get_name(d, -ENODEV, name);
  241. if (!child_name)
  242. return -ENOMEM;
  243. parents[0] = __clk_get_name(clk0);
  244. parents[1] = __clk_get_name(clk1);
  245. clock = clk_register_mux(d->dev, child_name, parents, 2, 0,
  246. reg, shift, 1, 0, &d->lock);
  247. if (IS_ERR(clock)) {
  248. dev_err(d->dev, "failed to register mux %s: %li\n",
  249. name, PTR_ERR(clock));
  250. return PTR_ERR(clock);
  251. }
  252. return ti_adpll_setup_clock(d, clock, index, -ENODEV, child_name,
  253. clk_unregister_mux);
  254. }
  255. static int ti_adpll_init_gate(struct ti_adpll_data *d,
  256. enum ti_adpll_clocks index,
  257. int output_index, char *name,
  258. struct clk *parent_clock,
  259. void __iomem *reg,
  260. u8 bit_idx,
  261. u8 clk_gate_flags)
  262. {
  263. const char *child_name;
  264. const char *parent_name;
  265. struct clk *clock;
  266. child_name = ti_adpll_clk_get_name(d, output_index, name);
  267. if (!child_name)
  268. return -EINVAL;
  269. parent_name = __clk_get_name(parent_clock);
  270. clock = clk_register_gate(d->dev, child_name, parent_name, 0,
  271. reg, bit_idx, clk_gate_flags,
  272. &d->lock);
  273. if (IS_ERR(clock)) {
  274. dev_err(d->dev, "failed to register gate %s: %li\n",
  275. name, PTR_ERR(clock));
  276. return PTR_ERR(clock);
  277. }
  278. return ti_adpll_setup_clock(d, clock, index, output_index, child_name,
  279. clk_unregister_gate);
  280. }
  281. static int ti_adpll_init_fixed_factor(struct ti_adpll_data *d,
  282. enum ti_adpll_clocks index,
  283. char *name,
  284. struct clk *parent_clock,
  285. unsigned int mult,
  286. unsigned int div)
  287. {
  288. const char *child_name;
  289. const char *parent_name;
  290. struct clk *clock;
  291. child_name = ti_adpll_clk_get_name(d, -ENODEV, name);
  292. if (!child_name)
  293. return -ENOMEM;
  294. parent_name = __clk_get_name(parent_clock);
  295. clock = clk_register_fixed_factor(d->dev, child_name, parent_name,
  296. 0, mult, div);
  297. if (IS_ERR(clock))
  298. return PTR_ERR(clock);
  299. return ti_adpll_setup_clock(d, clock, index, -ENODEV, child_name,
  300. clk_unregister);
  301. }
  302. static void ti_adpll_set_idle_bypass(struct ti_adpll_data *d)
  303. {
  304. unsigned long flags;
  305. u32 v;
  306. spin_lock_irqsave(&d->lock, flags);
  307. v = readl_relaxed(d->regs + ADPLL_CLKCTRL_OFFSET);
  308. v |= BIT(ADPLL_CLKCTRL_IDLE);
  309. writel_relaxed(v, d->regs + ADPLL_CLKCTRL_OFFSET);
  310. spin_unlock_irqrestore(&d->lock, flags);
  311. }
  312. static void ti_adpll_clear_idle_bypass(struct ti_adpll_data *d)
  313. {
  314. unsigned long flags;
  315. u32 v;
  316. spin_lock_irqsave(&d->lock, flags);
  317. v = readl_relaxed(d->regs + ADPLL_CLKCTRL_OFFSET);
  318. v &= ~BIT(ADPLL_CLKCTRL_IDLE);
  319. writel_relaxed(v, d->regs + ADPLL_CLKCTRL_OFFSET);
  320. spin_unlock_irqrestore(&d->lock, flags);
  321. }
  322. static bool ti_adpll_clock_is_bypass(struct ti_adpll_data *d)
  323. {
  324. u32 v;
  325. v = readl_relaxed(d->regs + ADPLL_STATUS_OFFSET);
  326. return v & BIT(ADPLL_STATUS_BYPASS);
  327. }
  328. /*
  329. * Locked and bypass are not actually mutually exclusive: if you only care
  330. * about the DCO clock and not CLKOUT you can clear M2PWDNZ before enabling
  331. * the PLL, resulting in status (FREQLOCK | PHASELOCK | BYPASS) after lock.
  332. */
  333. static bool ti_adpll_is_locked(struct ti_adpll_data *d)
  334. {
  335. u32 v = readl_relaxed(d->regs + ADPLL_STATUS_OFFSET);
  336. return (v & ADPLL_STATUS_PREPARED_MASK) == ADPLL_STATUS_PREPARED_MASK;
  337. }
  338. static int ti_adpll_wait_lock(struct ti_adpll_data *d)
  339. {
  340. int retries = ADPLL_MAX_RETRIES;
  341. do {
  342. if (ti_adpll_is_locked(d))
  343. return 0;
  344. usleep_range(200, 300);
  345. } while (retries--);
  346. dev_err(d->dev, "pll failed to lock\n");
  347. return -ETIMEDOUT;
  348. }
  349. static int ti_adpll_prepare(struct clk_hw *hw)
  350. {
  351. struct ti_adpll_dco_data *dco = to_dco(hw);
  352. struct ti_adpll_data *d = to_adpll(dco);
  353. ti_adpll_clear_idle_bypass(d);
  354. ti_adpll_wait_lock(d);
  355. return 0;
  356. }
  357. static void ti_adpll_unprepare(struct clk_hw *hw)
  358. {
  359. struct ti_adpll_dco_data *dco = to_dco(hw);
  360. struct ti_adpll_data *d = to_adpll(dco);
  361. ti_adpll_set_idle_bypass(d);
  362. }
  363. static int ti_adpll_is_prepared(struct clk_hw *hw)
  364. {
  365. struct ti_adpll_dco_data *dco = to_dco(hw);
  366. struct ti_adpll_data *d = to_adpll(dco);
  367. return ti_adpll_is_locked(d);
  368. }
  369. /*
  370. * Note that the DCO clock is never subject to bypass: if the PLL is off,
  371. * dcoclk is low.
  372. */
  373. static unsigned long ti_adpll_recalc_rate(struct clk_hw *hw,
  374. unsigned long parent_rate)
  375. {
  376. struct ti_adpll_dco_data *dco = to_dco(hw);
  377. struct ti_adpll_data *d = to_adpll(dco);
  378. u32 frac_m, divider, v;
  379. u64 rate;
  380. unsigned long flags;
  381. if (ti_adpll_clock_is_bypass(d))
  382. return 0;
  383. spin_lock_irqsave(&d->lock, flags);
  384. frac_m = readl_relaxed(d->regs + ADPLL_FRACDIV_OFFSET);
  385. frac_m &= ADPLL_FRACDIV_FRACTIONALM_MASK;
  386. rate = (u64)readw_relaxed(d->regs + ADPLL_MN2DIV_OFFSET) << 18;
  387. rate += frac_m;
  388. rate *= parent_rate;
  389. divider = (readw_relaxed(d->regs + ADPLL_M2NDIV_OFFSET) + 1) << 18;
  390. spin_unlock_irqrestore(&d->lock, flags);
  391. do_div(rate, divider);
  392. if (d->c->is_type_s) {
  393. v = readl_relaxed(d->regs + ADPLL_CLKCTRL_OFFSET);
  394. if (v & BIT(ADPLL_CLKCTRL_REGM4XEN_ADPLL_S))
  395. rate *= 4;
  396. rate *= 2;
  397. }
  398. return rate;
  399. }
  400. /* PLL parent is always clkinp, bypass only affects the children */
  401. static u8 ti_adpll_get_parent(struct clk_hw *hw)
  402. {
  403. return 0;
  404. }
  405. static const struct clk_ops ti_adpll_ops = {
  406. .prepare = ti_adpll_prepare,
  407. .unprepare = ti_adpll_unprepare,
  408. .is_prepared = ti_adpll_is_prepared,
  409. .recalc_rate = ti_adpll_recalc_rate,
  410. .get_parent = ti_adpll_get_parent,
  411. };
  412. static int ti_adpll_init_dco(struct ti_adpll_data *d)
  413. {
  414. struct clk_init_data init;
  415. struct clk *clock;
  416. const char *postfix;
  417. int width, err;
  418. d->outputs.clks = devm_kcalloc(d->dev,
  419. MAX_ADPLL_OUTPUTS,
  420. sizeof(struct clk *),
  421. GFP_KERNEL);
  422. if (!d->outputs.clks)
  423. return -ENOMEM;
  424. if (d->c->output_index < 0)
  425. postfix = "dco";
  426. else
  427. postfix = NULL;
  428. init.name = ti_adpll_clk_get_name(d, d->c->output_index, postfix);
  429. if (!init.name)
  430. return -EINVAL;
  431. init.parent_names = d->parent_names;
  432. init.num_parents = d->c->nr_max_inputs;
  433. init.ops = &ti_adpll_ops;
  434. init.flags = CLK_GET_RATE_NOCACHE;
  435. d->dco.hw.init = &init;
  436. if (d->c->is_type_s)
  437. width = 5;
  438. else
  439. width = 4;
  440. /* Internal input clock divider N2 */
  441. err = ti_adpll_init_divider(d, TI_ADPLL_N2, -ENODEV, "n2",
  442. d->parent_clocks[TI_ADPLL_CLKINP],
  443. d->regs + ADPLL_MN2DIV_OFFSET,
  444. ADPLL_MN2DIV_N2, width, 0);
  445. if (err)
  446. return err;
  447. clock = devm_clk_register(d->dev, &d->dco.hw);
  448. if (IS_ERR(clock))
  449. return PTR_ERR(clock);
  450. return ti_adpll_setup_clock(d, clock, TI_ADPLL_DCO, d->c->output_index,
  451. init.name, NULL);
  452. }
  453. static int ti_adpll_clkout_enable(struct clk_hw *hw)
  454. {
  455. struct ti_adpll_clkout_data *co = to_clkout(hw);
  456. struct clk_hw *gate_hw = &co->gate.hw;
  457. __clk_hw_set_clk(gate_hw, hw);
  458. return clk_gate_ops.enable(gate_hw);
  459. }
  460. static void ti_adpll_clkout_disable(struct clk_hw *hw)
  461. {
  462. struct ti_adpll_clkout_data *co = to_clkout(hw);
  463. struct clk_hw *gate_hw = &co->gate.hw;
  464. __clk_hw_set_clk(gate_hw, hw);
  465. clk_gate_ops.disable(gate_hw);
  466. }
  467. static int ti_adpll_clkout_is_enabled(struct clk_hw *hw)
  468. {
  469. struct ti_adpll_clkout_data *co = to_clkout(hw);
  470. struct clk_hw *gate_hw = &co->gate.hw;
  471. __clk_hw_set_clk(gate_hw, hw);
  472. return clk_gate_ops.is_enabled(gate_hw);
  473. }
  474. /* Setting PLL bypass puts clkout and clkoutx2 into bypass */
  475. static u8 ti_adpll_clkout_get_parent(struct clk_hw *hw)
  476. {
  477. struct ti_adpll_clkout_data *co = to_clkout(hw);
  478. struct ti_adpll_data *d = co->adpll;
  479. return ti_adpll_clock_is_bypass(d);
  480. }
  481. static int ti_adpll_init_clkout(struct ti_adpll_data *d,
  482. enum ti_adpll_clocks index,
  483. int output_index, int gate_bit,
  484. char *name, struct clk *clk0,
  485. struct clk *clk1)
  486. {
  487. struct ti_adpll_clkout_data *co;
  488. struct clk_init_data init;
  489. struct clk_ops *ops;
  490. const char *parent_names[2];
  491. const char *child_name;
  492. struct clk *clock;
  493. int err;
  494. co = devm_kzalloc(d->dev, sizeof(*co), GFP_KERNEL);
  495. if (!co)
  496. return -ENOMEM;
  497. co->adpll = d;
  498. err = of_property_read_string_index(d->np,
  499. "clock-output-names",
  500. output_index,
  501. &child_name);
  502. if (err)
  503. return err;
  504. ops = devm_kzalloc(d->dev, sizeof(*ops), GFP_KERNEL);
  505. if (!ops)
  506. return -ENOMEM;
  507. init.name = child_name;
  508. init.ops = ops;
  509. init.flags = CLK_IS_BASIC;
  510. co->hw.init = &init;
  511. parent_names[0] = __clk_get_name(clk0);
  512. parent_names[1] = __clk_get_name(clk1);
  513. init.parent_names = parent_names;
  514. init.num_parents = 2;
  515. ops->get_parent = ti_adpll_clkout_get_parent;
  516. ops->determine_rate = __clk_mux_determine_rate;
  517. if (gate_bit) {
  518. co->gate.lock = &d->lock;
  519. co->gate.reg = d->regs + ADPLL_CLKCTRL_OFFSET;
  520. co->gate.bit_idx = gate_bit;
  521. ops->enable = ti_adpll_clkout_enable;
  522. ops->disable = ti_adpll_clkout_disable;
  523. ops->is_enabled = ti_adpll_clkout_is_enabled;
  524. }
  525. clock = devm_clk_register(d->dev, &co->hw);
  526. if (IS_ERR(clock)) {
  527. dev_err(d->dev, "failed to register output %s: %li\n",
  528. name, PTR_ERR(clock));
  529. return PTR_ERR(clock);
  530. }
  531. return ti_adpll_setup_clock(d, clock, index, output_index, child_name,
  532. NULL);
  533. }
  534. static int ti_adpll_init_children_adpll_s(struct ti_adpll_data *d)
  535. {
  536. int err;
  537. if (!d->c->is_type_s)
  538. return 0;
  539. /* Internal mux, sources from divider N2 or clkinpulow */
  540. err = ti_adpll_init_mux(d, TI_ADPLL_BYPASS, "bypass",
  541. d->clocks[TI_ADPLL_N2].clk,
  542. d->parent_clocks[TI_ADPLL_CLKINPULOW],
  543. d->regs + ADPLL_CLKCTRL_OFFSET,
  544. ADPLL_CLKCTRL_ULOWCLKEN);
  545. if (err)
  546. return err;
  547. /* Internal divider M2, sources DCO */
  548. err = ti_adpll_init_divider(d, TI_ADPLL_M2, -ENODEV, "m2",
  549. d->clocks[TI_ADPLL_DCO].clk,
  550. d->regs + ADPLL_M2NDIV_OFFSET,
  551. ADPLL_M2NDIV_M2,
  552. ADPLL_M2NDIV_M2_ADPLL_S_WIDTH,
  553. CLK_DIVIDER_ONE_BASED);
  554. if (err)
  555. return err;
  556. /* Internal fixed divider, after M2 before clkout */
  557. err = ti_adpll_init_fixed_factor(d, TI_ADPLL_DIV2, "div2",
  558. d->clocks[TI_ADPLL_M2].clk,
  559. 1, 2);
  560. if (err)
  561. return err;
  562. /* Output clkout with a mux and gate, sources from div2 or bypass */
  563. err = ti_adpll_init_clkout(d, TI_ADPLL_CLKOUT, TI_ADPLL_S_CLKOUT,
  564. ADPLL_CLKCTRL_CLKOUTEN, "clkout",
  565. d->clocks[TI_ADPLL_DIV2].clk,
  566. d->clocks[TI_ADPLL_BYPASS].clk);
  567. if (err)
  568. return err;
  569. /* Output clkoutx2 with a mux and gate, sources from M2 or bypass */
  570. err = ti_adpll_init_clkout(d, TI_ADPLL_CLKOUT2, TI_ADPLL_S_CLKOUTX2, 0,
  571. "clkout2", d->clocks[TI_ADPLL_M2].clk,
  572. d->clocks[TI_ADPLL_BYPASS].clk);
  573. if (err)
  574. return err;
  575. /* Internal mux, sources from DCO and clkinphif */
  576. if (d->parent_clocks[TI_ADPLL_CLKINPHIF]) {
  577. err = ti_adpll_init_mux(d, TI_ADPLL_HIF, "hif",
  578. d->clocks[TI_ADPLL_DCO].clk,
  579. d->parent_clocks[TI_ADPLL_CLKINPHIF],
  580. d->regs + ADPLL_CLKCTRL_OFFSET,
  581. ADPLL_CLKINPHIFSEL_ADPLL_S);
  582. if (err)
  583. return err;
  584. }
  585. /* Output clkouthif with a divider M3, sources from hif */
  586. err = ti_adpll_init_divider(d, TI_ADPLL_M3, TI_ADPLL_S_CLKOUTHIF, "m3",
  587. d->clocks[TI_ADPLL_HIF].clk,
  588. d->regs + ADPLL_M3DIV_OFFSET,
  589. ADPLL_M3DIV_M3,
  590. ADPLL_M3DIV_M3_WIDTH,
  591. CLK_DIVIDER_ONE_BASED);
  592. if (err)
  593. return err;
  594. /* Output clock dcoclkldo is the DCO */
  595. return 0;
  596. }
  597. static int ti_adpll_init_children_adpll_lj(struct ti_adpll_data *d)
  598. {
  599. int err;
  600. if (d->c->is_type_s)
  601. return 0;
  602. /* Output clkdcoldo, gated output of DCO */
  603. err = ti_adpll_init_gate(d, TI_ADPLL_DCO_GATE, TI_ADPLL_LJ_CLKDCOLDO,
  604. "clkdcoldo", d->clocks[TI_ADPLL_DCO].clk,
  605. d->regs + ADPLL_CLKCTRL_OFFSET,
  606. ADPLL_CLKCTRL_CLKDCOLDOEN, 0);
  607. if (err)
  608. return err;
  609. /* Internal divider M2, sources from DCO */
  610. err = ti_adpll_init_divider(d, TI_ADPLL_M2, -ENODEV,
  611. "m2", d->clocks[TI_ADPLL_DCO].clk,
  612. d->regs + ADPLL_M2NDIV_OFFSET,
  613. ADPLL_M2NDIV_M2,
  614. ADPLL_M2NDIV_M2_ADPLL_LJ_WIDTH,
  615. CLK_DIVIDER_ONE_BASED);
  616. if (err)
  617. return err;
  618. /* Output clkoutldo, gated output of M2 */
  619. err = ti_adpll_init_gate(d, TI_ADPLL_M2_GATE, TI_ADPLL_LJ_CLKOUTLDO,
  620. "clkoutldo", d->clocks[TI_ADPLL_M2].clk,
  621. d->regs + ADPLL_CLKCTRL_OFFSET,
  622. ADPLL_CLKCTRL_CLKOUTLDOEN_ADPLL_LJ,
  623. 0);
  624. if (err)
  625. return err;
  626. /* Internal mux, sources from divider N2 or clkinpulow */
  627. err = ti_adpll_init_mux(d, TI_ADPLL_BYPASS, "bypass",
  628. d->clocks[TI_ADPLL_N2].clk,
  629. d->parent_clocks[TI_ADPLL_CLKINPULOW],
  630. d->regs + ADPLL_CLKCTRL_OFFSET,
  631. ADPLL_CLKCTRL_ULOWCLKEN);
  632. if (err)
  633. return err;
  634. /* Output clkout, sources M2 or bypass */
  635. err = ti_adpll_init_clkout(d, TI_ADPLL_CLKOUT, TI_ADPLL_S_CLKOUT,
  636. ADPLL_CLKCTRL_CLKOUTEN, "clkout",
  637. d->clocks[TI_ADPLL_M2].clk,
  638. d->clocks[TI_ADPLL_BYPASS].clk);
  639. if (err)
  640. return err;
  641. return 0;
  642. }
  643. static void ti_adpll_free_resources(struct ti_adpll_data *d)
  644. {
  645. int i;
  646. for (i = TI_ADPLL_M3; i >= 0; i--) {
  647. struct ti_adpll_clock *ac = &d->clocks[i];
  648. if (!ac || IS_ERR_OR_NULL(ac->clk))
  649. continue;
  650. if (ac->cl)
  651. clkdev_drop(ac->cl);
  652. if (ac->unregister)
  653. ac->unregister(ac->clk);
  654. }
  655. }
  656. /* MPU PLL manages the lock register for all PLLs */
  657. static void ti_adpll_unlock_all(void __iomem *reg)
  658. {
  659. u32 v;
  660. v = readl_relaxed(reg);
  661. if (v == ADPLL_PLLSS_MMR_LOCK_ENABLED)
  662. writel_relaxed(ADPLL_PLLSS_MMR_UNLOCK_MAGIC, reg);
  663. }
  664. static int ti_adpll_init_registers(struct ti_adpll_data *d)
  665. {
  666. int register_offset = 0;
  667. if (d->c->is_type_s) {
  668. register_offset = 8;
  669. ti_adpll_unlock_all(d->iobase + ADPLL_PLLSS_MMR_LOCK_OFFSET);
  670. }
  671. d->regs = d->iobase + register_offset + ADPLL_PWRCTRL_OFFSET;
  672. return 0;
  673. }
  674. static int ti_adpll_init_inputs(struct ti_adpll_data *d)
  675. {
  676. const char *error = "need at least %i inputs";
  677. struct clk *clock;
  678. int nr_inputs;
  679. nr_inputs = of_clk_get_parent_count(d->np);
  680. if (nr_inputs < d->c->nr_max_inputs) {
  681. dev_err(d->dev, error, nr_inputs);
  682. return -EINVAL;
  683. }
  684. of_clk_parent_fill(d->np, d->parent_names, nr_inputs);
  685. clock = devm_clk_get(d->dev, d->parent_names[0]);
  686. if (IS_ERR(clock)) {
  687. dev_err(d->dev, "could not get clkinp\n");
  688. return PTR_ERR(clock);
  689. }
  690. d->parent_clocks[TI_ADPLL_CLKINP] = clock;
  691. clock = devm_clk_get(d->dev, d->parent_names[1]);
  692. if (IS_ERR(clock)) {
  693. dev_err(d->dev, "could not get clkinpulow clock\n");
  694. return PTR_ERR(clock);
  695. }
  696. d->parent_clocks[TI_ADPLL_CLKINPULOW] = clock;
  697. if (d->c->is_type_s) {
  698. clock = devm_clk_get(d->dev, d->parent_names[2]);
  699. if (IS_ERR(clock)) {
  700. dev_err(d->dev, "could not get clkinphif clock\n");
  701. return PTR_ERR(clock);
  702. }
  703. d->parent_clocks[TI_ADPLL_CLKINPHIF] = clock;
  704. }
  705. return 0;
  706. }
  707. static const struct ti_adpll_platform_data ti_adpll_type_s = {
  708. .is_type_s = true,
  709. .nr_max_inputs = MAX_ADPLL_INPUTS,
  710. .nr_max_outputs = MAX_ADPLL_OUTPUTS,
  711. .output_index = TI_ADPLL_S_DCOCLKLDO,
  712. };
  713. static const struct ti_adpll_platform_data ti_adpll_type_lj = {
  714. .is_type_s = false,
  715. .nr_max_inputs = MAX_ADPLL_INPUTS - 1,
  716. .nr_max_outputs = MAX_ADPLL_OUTPUTS - 1,
  717. .output_index = -EINVAL,
  718. };
  719. static const struct of_device_id ti_adpll_match[] = {
  720. { .compatible = "ti,dm814-adpll-s-clock", &ti_adpll_type_s },
  721. { .compatible = "ti,dm814-adpll-lj-clock", &ti_adpll_type_lj },
  722. {},
  723. };
  724. MODULE_DEVICE_TABLE(of, ti_adpll_match);
  725. static int ti_adpll_probe(struct platform_device *pdev)
  726. {
  727. struct device_node *node = pdev->dev.of_node;
  728. struct device *dev = &pdev->dev;
  729. const struct of_device_id *match;
  730. const struct ti_adpll_platform_data *pdata;
  731. struct ti_adpll_data *d;
  732. struct resource *res;
  733. int err;
  734. match = of_match_device(ti_adpll_match, dev);
  735. if (match)
  736. pdata = match->data;
  737. else
  738. return -ENODEV;
  739. d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
  740. if (!d)
  741. return -ENOMEM;
  742. d->dev = dev;
  743. d->np = node;
  744. d->c = pdata;
  745. dev_set_drvdata(d->dev, d);
  746. spin_lock_init(&d->lock);
  747. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  748. if (!res)
  749. return -ENODEV;
  750. d->pa = res->start;
  751. d->iobase = devm_ioremap_resource(dev, res);
  752. if (IS_ERR(d->iobase)) {
  753. dev_err(dev, "could not get IO base: %li\n",
  754. PTR_ERR(d->iobase));
  755. return PTR_ERR(d->iobase);
  756. }
  757. err = ti_adpll_init_registers(d);
  758. if (err)
  759. return err;
  760. err = ti_adpll_init_inputs(d);
  761. if (err)
  762. return err;
  763. d->clocks = devm_kcalloc(d->dev,
  764. TI_ADPLL_NR_CLOCKS,
  765. sizeof(struct ti_adpll_clock),
  766. GFP_KERNEL);
  767. if (!d->clocks)
  768. return -ENOMEM;
  769. err = ti_adpll_init_dco(d);
  770. if (err) {
  771. dev_err(dev, "could not register dco: %i\n", err);
  772. goto free;
  773. }
  774. err = ti_adpll_init_children_adpll_s(d);
  775. if (err)
  776. goto free;
  777. err = ti_adpll_init_children_adpll_lj(d);
  778. if (err)
  779. goto free;
  780. err = of_clk_add_provider(d->np, of_clk_src_onecell_get, &d->outputs);
  781. if (err)
  782. goto free;
  783. return 0;
  784. free:
  785. WARN_ON(1);
  786. ti_adpll_free_resources(d);
  787. return err;
  788. }
  789. static int ti_adpll_remove(struct platform_device *pdev)
  790. {
  791. struct ti_adpll_data *d = dev_get_drvdata(&pdev->dev);
  792. ti_adpll_free_resources(d);
  793. return 0;
  794. }
  795. static struct platform_driver ti_adpll_driver = {
  796. .driver = {
  797. .name = "ti-adpll",
  798. .of_match_table = ti_adpll_match,
  799. },
  800. .probe = ti_adpll_probe,
  801. .remove = ti_adpll_remove,
  802. };
  803. static int __init ti_adpll_init(void)
  804. {
  805. return platform_driver_register(&ti_adpll_driver);
  806. }
  807. core_initcall(ti_adpll_init);
  808. static void __exit ti_adpll_exit(void)
  809. {
  810. platform_driver_unregister(&ti_adpll_driver);
  811. }
  812. module_exit(ti_adpll_exit);
  813. MODULE_DESCRIPTION("Clock driver for dm814x ADPLL");
  814. MODULE_ALIAS("platform:dm814-adpll-clock");
  815. MODULE_AUTHOR("Tony LIndgren <tony@atomide.com>");
  816. MODULE_LICENSE("GPL v2");