clk-axm5516.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * drivers/clk/clk-axm5516.c
  3. *
  4. * Provides clock implementations for three different types of clock devices on
  5. * the Axxia device: PLL clock, a clock divider and a clock mux.
  6. *
  7. * Copyright (C) 2014 LSI Corporation
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/slab.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/clk-provider.h>
  20. #include <linux/regmap.h>
  21. #include <dt-bindings/clock/lsi,axm5516-clks.h>
  22. /**
  23. * struct axxia_clk - Common struct to all Axxia clocks.
  24. * @hw: clk_hw for the common clk framework
  25. * @regmap: Regmap for the clock control registers
  26. */
  27. struct axxia_clk {
  28. struct clk_hw hw;
  29. struct regmap *regmap;
  30. };
  31. #define to_axxia_clk(_hw) container_of(_hw, struct axxia_clk, hw)
  32. /**
  33. * struct axxia_pllclk - Axxia PLL generated clock.
  34. * @aclk: Common struct
  35. * @reg: Offset into regmap for PLL control register
  36. */
  37. struct axxia_pllclk {
  38. struct axxia_clk aclk;
  39. u32 reg;
  40. };
  41. #define to_axxia_pllclk(_aclk) container_of(_aclk, struct axxia_pllclk, aclk)
  42. /**
  43. * axxia_pllclk_recalc - Calculate the PLL generated clock rate given the
  44. * parent clock rate.
  45. */
  46. static unsigned long
  47. axxia_pllclk_recalc(struct clk_hw *hw, unsigned long parent_rate)
  48. {
  49. struct axxia_clk *aclk = to_axxia_clk(hw);
  50. struct axxia_pllclk *pll = to_axxia_pllclk(aclk);
  51. unsigned long rate, fbdiv, refdiv, postdiv;
  52. u32 control;
  53. regmap_read(aclk->regmap, pll->reg, &control);
  54. postdiv = ((control >> 0) & 0xf) + 1;
  55. fbdiv = ((control >> 4) & 0xfff) + 3;
  56. refdiv = ((control >> 16) & 0x1f) + 1;
  57. rate = (parent_rate / (refdiv * postdiv)) * fbdiv;
  58. return rate;
  59. }
  60. static const struct clk_ops axxia_pllclk_ops = {
  61. .recalc_rate = axxia_pllclk_recalc,
  62. };
  63. /**
  64. * struct axxia_divclk - Axxia clock divider
  65. * @aclk: Common struct
  66. * @reg: Offset into regmap for PLL control register
  67. * @shift: Bit position for divider value
  68. * @width: Number of bits in divider value
  69. */
  70. struct axxia_divclk {
  71. struct axxia_clk aclk;
  72. u32 reg;
  73. u32 shift;
  74. u32 width;
  75. };
  76. #define to_axxia_divclk(_aclk) container_of(_aclk, struct axxia_divclk, aclk)
  77. /**
  78. * axxia_divclk_recalc_rate - Calculate clock divider output rage
  79. */
  80. static unsigned long
  81. axxia_divclk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  82. {
  83. struct axxia_clk *aclk = to_axxia_clk(hw);
  84. struct axxia_divclk *divclk = to_axxia_divclk(aclk);
  85. u32 ctrl, div;
  86. regmap_read(aclk->regmap, divclk->reg, &ctrl);
  87. div = 1 + ((ctrl >> divclk->shift) & ((1 << divclk->width)-1));
  88. return parent_rate / div;
  89. }
  90. static const struct clk_ops axxia_divclk_ops = {
  91. .recalc_rate = axxia_divclk_recalc_rate,
  92. };
  93. /**
  94. * struct axxia_clkmux - Axxia clock mux
  95. * @aclk: Common struct
  96. * @reg: Offset into regmap for PLL control register
  97. * @shift: Bit position for selection value
  98. * @width: Number of bits in selection value
  99. */
  100. struct axxia_clkmux {
  101. struct axxia_clk aclk;
  102. u32 reg;
  103. u32 shift;
  104. u32 width;
  105. };
  106. #define to_axxia_clkmux(_aclk) container_of(_aclk, struct axxia_clkmux, aclk)
  107. /**
  108. * axxia_clkmux_get_parent - Return the index of selected parent clock
  109. */
  110. static u8 axxia_clkmux_get_parent(struct clk_hw *hw)
  111. {
  112. struct axxia_clk *aclk = to_axxia_clk(hw);
  113. struct axxia_clkmux *mux = to_axxia_clkmux(aclk);
  114. u32 ctrl, parent;
  115. regmap_read(aclk->regmap, mux->reg, &ctrl);
  116. parent = (ctrl >> mux->shift) & ((1 << mux->width) - 1);
  117. return (u8) parent;
  118. }
  119. static const struct clk_ops axxia_clkmux_ops = {
  120. .get_parent = axxia_clkmux_get_parent,
  121. };
  122. /*
  123. * PLLs
  124. */
  125. static struct axxia_pllclk clk_fab_pll = {
  126. .aclk.hw.init = &(struct clk_init_data){
  127. .name = "clk_fab_pll",
  128. .parent_names = (const char *[]){
  129. "clk_ref0"
  130. },
  131. .num_parents = 1,
  132. .ops = &axxia_pllclk_ops,
  133. },
  134. .reg = 0x01800,
  135. };
  136. static struct axxia_pllclk clk_cpu_pll = {
  137. .aclk.hw.init = &(struct clk_init_data){
  138. .name = "clk_cpu_pll",
  139. .parent_names = (const char *[]){
  140. "clk_ref0"
  141. },
  142. .num_parents = 1,
  143. .ops = &axxia_pllclk_ops,
  144. },
  145. .reg = 0x02000,
  146. };
  147. static struct axxia_pllclk clk_sys_pll = {
  148. .aclk.hw.init = &(struct clk_init_data){
  149. .name = "clk_sys_pll",
  150. .parent_names = (const char *[]){
  151. "clk_ref0"
  152. },
  153. .num_parents = 1,
  154. .ops = &axxia_pllclk_ops,
  155. },
  156. .reg = 0x02800,
  157. };
  158. static struct axxia_pllclk clk_sm0_pll = {
  159. .aclk.hw.init = &(struct clk_init_data){
  160. .name = "clk_sm0_pll",
  161. .parent_names = (const char *[]){
  162. "clk_ref2"
  163. },
  164. .num_parents = 1,
  165. .ops = &axxia_pllclk_ops,
  166. },
  167. .reg = 0x03000,
  168. };
  169. static struct axxia_pllclk clk_sm1_pll = {
  170. .aclk.hw.init = &(struct clk_init_data){
  171. .name = "clk_sm1_pll",
  172. .parent_names = (const char *[]){
  173. "clk_ref1"
  174. },
  175. .num_parents = 1,
  176. .ops = &axxia_pllclk_ops,
  177. },
  178. .reg = 0x03800,
  179. };
  180. /*
  181. * Clock dividers
  182. */
  183. static struct axxia_divclk clk_cpu0_div = {
  184. .aclk.hw.init = &(struct clk_init_data){
  185. .name = "clk_cpu0_div",
  186. .parent_names = (const char *[]){
  187. "clk_cpu_pll"
  188. },
  189. .num_parents = 1,
  190. .ops = &axxia_divclk_ops,
  191. },
  192. .reg = 0x10008,
  193. .shift = 0,
  194. .width = 4,
  195. };
  196. static struct axxia_divclk clk_cpu1_div = {
  197. .aclk.hw.init = &(struct clk_init_data){
  198. .name = "clk_cpu1_div",
  199. .parent_names = (const char *[]){
  200. "clk_cpu_pll"
  201. },
  202. .num_parents = 1,
  203. .ops = &axxia_divclk_ops,
  204. },
  205. .reg = 0x10008,
  206. .shift = 4,
  207. .width = 4,
  208. };
  209. static struct axxia_divclk clk_cpu2_div = {
  210. .aclk.hw.init = &(struct clk_init_data){
  211. .name = "clk_cpu2_div",
  212. .parent_names = (const char *[]){
  213. "clk_cpu_pll"
  214. },
  215. .num_parents = 1,
  216. .ops = &axxia_divclk_ops,
  217. },
  218. .reg = 0x10008,
  219. .shift = 8,
  220. .width = 4,
  221. };
  222. static struct axxia_divclk clk_cpu3_div = {
  223. .aclk.hw.init = &(struct clk_init_data){
  224. .name = "clk_cpu3_div",
  225. .parent_names = (const char *[]){
  226. "clk_cpu_pll"
  227. },
  228. .num_parents = 1,
  229. .ops = &axxia_divclk_ops,
  230. },
  231. .reg = 0x10008,
  232. .shift = 12,
  233. .width = 4,
  234. };
  235. static struct axxia_divclk clk_nrcp_div = {
  236. .aclk.hw.init = &(struct clk_init_data){
  237. .name = "clk_nrcp_div",
  238. .parent_names = (const char *[]){
  239. "clk_sys_pll"
  240. },
  241. .num_parents = 1,
  242. .ops = &axxia_divclk_ops,
  243. },
  244. .reg = 0x1000c,
  245. .shift = 0,
  246. .width = 4,
  247. };
  248. static struct axxia_divclk clk_sys_div = {
  249. .aclk.hw.init = &(struct clk_init_data){
  250. .name = "clk_sys_div",
  251. .parent_names = (const char *[]){
  252. "clk_sys_pll"
  253. },
  254. .num_parents = 1,
  255. .ops = &axxia_divclk_ops,
  256. },
  257. .reg = 0x1000c,
  258. .shift = 4,
  259. .width = 4,
  260. };
  261. static struct axxia_divclk clk_fab_div = {
  262. .aclk.hw.init = &(struct clk_init_data){
  263. .name = "clk_fab_div",
  264. .parent_names = (const char *[]){
  265. "clk_fab_pll"
  266. },
  267. .num_parents = 1,
  268. .ops = &axxia_divclk_ops,
  269. },
  270. .reg = 0x1000c,
  271. .shift = 8,
  272. .width = 4,
  273. };
  274. static struct axxia_divclk clk_per_div = {
  275. .aclk.hw.init = &(struct clk_init_data){
  276. .name = "clk_per_div",
  277. .parent_names = (const char *[]){
  278. "clk_sm1_pll"
  279. },
  280. .num_parents = 1,
  281. .flags = CLK_IS_BASIC,
  282. .ops = &axxia_divclk_ops,
  283. },
  284. .reg = 0x1000c,
  285. .shift = 12,
  286. .width = 4,
  287. };
  288. static struct axxia_divclk clk_mmc_div = {
  289. .aclk.hw.init = &(struct clk_init_data){
  290. .name = "clk_mmc_div",
  291. .parent_names = (const char *[]){
  292. "clk_sm1_pll"
  293. },
  294. .num_parents = 1,
  295. .flags = CLK_IS_BASIC,
  296. .ops = &axxia_divclk_ops,
  297. },
  298. .reg = 0x1000c,
  299. .shift = 16,
  300. .width = 4,
  301. };
  302. /*
  303. * Clock MUXes
  304. */
  305. static struct axxia_clkmux clk_cpu0_mux = {
  306. .aclk.hw.init = &(struct clk_init_data){
  307. .name = "clk_cpu0",
  308. .parent_names = (const char *[]){
  309. "clk_ref0",
  310. "clk_cpu_pll",
  311. "clk_cpu0_div",
  312. "clk_cpu0_div"
  313. },
  314. .num_parents = 4,
  315. .ops = &axxia_clkmux_ops,
  316. },
  317. .reg = 0x10000,
  318. .shift = 0,
  319. .width = 2,
  320. };
  321. static struct axxia_clkmux clk_cpu1_mux = {
  322. .aclk.hw.init = &(struct clk_init_data){
  323. .name = "clk_cpu1",
  324. .parent_names = (const char *[]){
  325. "clk_ref0",
  326. "clk_cpu_pll",
  327. "clk_cpu1_div",
  328. "clk_cpu1_div"
  329. },
  330. .num_parents = 4,
  331. .ops = &axxia_clkmux_ops,
  332. },
  333. .reg = 0x10000,
  334. .shift = 2,
  335. .width = 2,
  336. };
  337. static struct axxia_clkmux clk_cpu2_mux = {
  338. .aclk.hw.init = &(struct clk_init_data){
  339. .name = "clk_cpu2",
  340. .parent_names = (const char *[]){
  341. "clk_ref0",
  342. "clk_cpu_pll",
  343. "clk_cpu2_div",
  344. "clk_cpu2_div"
  345. },
  346. .num_parents = 4,
  347. .ops = &axxia_clkmux_ops,
  348. },
  349. .reg = 0x10000,
  350. .shift = 4,
  351. .width = 2,
  352. };
  353. static struct axxia_clkmux clk_cpu3_mux = {
  354. .aclk.hw.init = &(struct clk_init_data){
  355. .name = "clk_cpu3",
  356. .parent_names = (const char *[]){
  357. "clk_ref0",
  358. "clk_cpu_pll",
  359. "clk_cpu3_div",
  360. "clk_cpu3_div"
  361. },
  362. .num_parents = 4,
  363. .ops = &axxia_clkmux_ops,
  364. },
  365. .reg = 0x10000,
  366. .shift = 6,
  367. .width = 2,
  368. };
  369. static struct axxia_clkmux clk_nrcp_mux = {
  370. .aclk.hw.init = &(struct clk_init_data){
  371. .name = "clk_nrcp",
  372. .parent_names = (const char *[]){
  373. "clk_ref0",
  374. "clk_sys_pll",
  375. "clk_nrcp_div",
  376. "clk_nrcp_div"
  377. },
  378. .num_parents = 4,
  379. .ops = &axxia_clkmux_ops,
  380. },
  381. .reg = 0x10004,
  382. .shift = 0,
  383. .width = 2,
  384. };
  385. static struct axxia_clkmux clk_sys_mux = {
  386. .aclk.hw.init = &(struct clk_init_data){
  387. .name = "clk_sys",
  388. .parent_names = (const char *[]){
  389. "clk_ref0",
  390. "clk_sys_pll",
  391. "clk_sys_div",
  392. "clk_sys_div"
  393. },
  394. .num_parents = 4,
  395. .ops = &axxia_clkmux_ops,
  396. },
  397. .reg = 0x10004,
  398. .shift = 2,
  399. .width = 2,
  400. };
  401. static struct axxia_clkmux clk_fab_mux = {
  402. .aclk.hw.init = &(struct clk_init_data){
  403. .name = "clk_fab",
  404. .parent_names = (const char *[]){
  405. "clk_ref0",
  406. "clk_fab_pll",
  407. "clk_fab_div",
  408. "clk_fab_div"
  409. },
  410. .num_parents = 4,
  411. .ops = &axxia_clkmux_ops,
  412. },
  413. .reg = 0x10004,
  414. .shift = 4,
  415. .width = 2,
  416. };
  417. static struct axxia_clkmux clk_per_mux = {
  418. .aclk.hw.init = &(struct clk_init_data){
  419. .name = "clk_per",
  420. .parent_names = (const char *[]){
  421. "clk_ref1",
  422. "clk_per_div"
  423. },
  424. .num_parents = 2,
  425. .ops = &axxia_clkmux_ops,
  426. },
  427. .reg = 0x10004,
  428. .shift = 6,
  429. .width = 1,
  430. };
  431. static struct axxia_clkmux clk_mmc_mux = {
  432. .aclk.hw.init = &(struct clk_init_data){
  433. .name = "clk_mmc",
  434. .parent_names = (const char *[]){
  435. "clk_ref1",
  436. "clk_mmc_div"
  437. },
  438. .num_parents = 2,
  439. .ops = &axxia_clkmux_ops,
  440. },
  441. .reg = 0x10004,
  442. .shift = 9,
  443. .width = 1,
  444. };
  445. /* Table of all supported clocks indexed by the clock identifiers from the
  446. * device tree binding
  447. */
  448. static struct axxia_clk *axmclk_clocks[] = {
  449. [AXXIA_CLK_FAB_PLL] = &clk_fab_pll.aclk,
  450. [AXXIA_CLK_CPU_PLL] = &clk_cpu_pll.aclk,
  451. [AXXIA_CLK_SYS_PLL] = &clk_sys_pll.aclk,
  452. [AXXIA_CLK_SM0_PLL] = &clk_sm0_pll.aclk,
  453. [AXXIA_CLK_SM1_PLL] = &clk_sm1_pll.aclk,
  454. [AXXIA_CLK_FAB_DIV] = &clk_fab_div.aclk,
  455. [AXXIA_CLK_SYS_DIV] = &clk_sys_div.aclk,
  456. [AXXIA_CLK_NRCP_DIV] = &clk_nrcp_div.aclk,
  457. [AXXIA_CLK_CPU0_DIV] = &clk_cpu0_div.aclk,
  458. [AXXIA_CLK_CPU1_DIV] = &clk_cpu1_div.aclk,
  459. [AXXIA_CLK_CPU2_DIV] = &clk_cpu2_div.aclk,
  460. [AXXIA_CLK_CPU3_DIV] = &clk_cpu3_div.aclk,
  461. [AXXIA_CLK_PER_DIV] = &clk_per_div.aclk,
  462. [AXXIA_CLK_MMC_DIV] = &clk_mmc_div.aclk,
  463. [AXXIA_CLK_FAB] = &clk_fab_mux.aclk,
  464. [AXXIA_CLK_SYS] = &clk_sys_mux.aclk,
  465. [AXXIA_CLK_NRCP] = &clk_nrcp_mux.aclk,
  466. [AXXIA_CLK_CPU0] = &clk_cpu0_mux.aclk,
  467. [AXXIA_CLK_CPU1] = &clk_cpu1_mux.aclk,
  468. [AXXIA_CLK_CPU2] = &clk_cpu2_mux.aclk,
  469. [AXXIA_CLK_CPU3] = &clk_cpu3_mux.aclk,
  470. [AXXIA_CLK_PER] = &clk_per_mux.aclk,
  471. [AXXIA_CLK_MMC] = &clk_mmc_mux.aclk,
  472. };
  473. static const struct regmap_config axmclk_regmap_config = {
  474. .reg_bits = 32,
  475. .reg_stride = 4,
  476. .val_bits = 32,
  477. .max_register = 0x1fffc,
  478. .fast_io = true,
  479. };
  480. static const struct of_device_id axmclk_match_table[] = {
  481. { .compatible = "lsi,axm5516-clks" },
  482. { }
  483. };
  484. MODULE_DEVICE_TABLE(of, axmclk_match_table);
  485. struct axmclk_priv {
  486. struct clk_onecell_data onecell;
  487. struct clk *clks[];
  488. };
  489. static int axmclk_probe(struct platform_device *pdev)
  490. {
  491. void __iomem *base;
  492. struct resource *res;
  493. int i, ret;
  494. struct device *dev = &pdev->dev;
  495. struct clk *clk;
  496. struct regmap *regmap;
  497. size_t num_clks;
  498. struct axmclk_priv *priv;
  499. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  500. base = devm_ioremap_resource(dev, res);
  501. if (IS_ERR(base))
  502. return PTR_ERR(base);
  503. regmap = devm_regmap_init_mmio(dev, base, &axmclk_regmap_config);
  504. if (IS_ERR(regmap))
  505. return PTR_ERR(regmap);
  506. num_clks = ARRAY_SIZE(axmclk_clocks);
  507. pr_info("axmclk: supporting %zu clocks\n", num_clks);
  508. priv = devm_kzalloc(dev, sizeof(*priv) + sizeof(*priv->clks) * num_clks,
  509. GFP_KERNEL);
  510. if (!priv)
  511. return -ENOMEM;
  512. priv->onecell.clks = priv->clks;
  513. priv->onecell.clk_num = num_clks;
  514. /* Update each entry with the allocated regmap and register the clock
  515. * with the common clock framework
  516. */
  517. for (i = 0; i < num_clks; i++) {
  518. axmclk_clocks[i]->regmap = regmap;
  519. clk = devm_clk_register(dev, &axmclk_clocks[i]->hw);
  520. if (IS_ERR(clk))
  521. return PTR_ERR(clk);
  522. priv->clks[i] = clk;
  523. }
  524. ret = of_clk_add_provider(dev->of_node,
  525. of_clk_src_onecell_get, &priv->onecell);
  526. return ret;
  527. }
  528. static int axmclk_remove(struct platform_device *pdev)
  529. {
  530. of_clk_del_provider(pdev->dev.of_node);
  531. return 0;
  532. }
  533. static struct platform_driver axmclk_driver = {
  534. .probe = axmclk_probe,
  535. .remove = axmclk_remove,
  536. .driver = {
  537. .name = "clk-axm5516",
  538. .of_match_table = axmclk_match_table,
  539. },
  540. };
  541. static int __init axmclk_init(void)
  542. {
  543. return platform_driver_register(&axmclk_driver);
  544. }
  545. core_initcall(axmclk_init);
  546. static void __exit axmclk_exit(void)
  547. {
  548. platform_driver_unregister(&axmclk_driver);
  549. }
  550. module_exit(axmclk_exit);
  551. MODULE_DESCRIPTION("AXM5516 clock driver");
  552. MODULE_LICENSE("GPL v2");
  553. MODULE_ALIAS("platform:clk-axm5516");