cgu.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Ingenic SoC CGU driver
  3. *
  4. * Copyright (c) 2013-2015 Imagination Technologies
  5. * Author: Paul Burton <paul.burton@imgtec.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/clkdev.h>
  20. #include <linux/delay.h>
  21. #include <linux/math64.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include "cgu.h"
  27. #define MHZ (1000 * 1000)
  28. /**
  29. * ingenic_cgu_gate_get() - get the value of clock gate register bit
  30. * @cgu: reference to the CGU whose registers should be read
  31. * @info: info struct describing the gate bit
  32. *
  33. * Retrieves the state of the clock gate bit described by info. The
  34. * caller must hold cgu->lock.
  35. *
  36. * Return: true if the gate bit is set, else false.
  37. */
  38. static inline bool
  39. ingenic_cgu_gate_get(struct ingenic_cgu *cgu,
  40. const struct ingenic_cgu_gate_info *info)
  41. {
  42. return readl(cgu->base + info->reg) & BIT(info->bit);
  43. }
  44. /**
  45. * ingenic_cgu_gate_set() - set the value of clock gate register bit
  46. * @cgu: reference to the CGU whose registers should be modified
  47. * @info: info struct describing the gate bit
  48. * @val: non-zero to gate a clock, otherwise zero
  49. *
  50. * Sets the given gate bit in order to gate or ungate a clock.
  51. *
  52. * The caller must hold cgu->lock.
  53. */
  54. static inline void
  55. ingenic_cgu_gate_set(struct ingenic_cgu *cgu,
  56. const struct ingenic_cgu_gate_info *info, bool val)
  57. {
  58. u32 clkgr = readl(cgu->base + info->reg);
  59. if (val)
  60. clkgr |= BIT(info->bit);
  61. else
  62. clkgr &= ~BIT(info->bit);
  63. writel(clkgr, cgu->base + info->reg);
  64. }
  65. /*
  66. * PLL operations
  67. */
  68. static unsigned long
  69. ingenic_pll_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  70. {
  71. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  72. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  73. const struct ingenic_cgu_clk_info *clk_info;
  74. const struct ingenic_cgu_pll_info *pll_info;
  75. unsigned m, n, od_enc, od;
  76. bool bypass, enable;
  77. unsigned long flags;
  78. u32 ctl;
  79. clk_info = &cgu->clock_info[ingenic_clk->idx];
  80. BUG_ON(clk_info->type != CGU_CLK_PLL);
  81. pll_info = &clk_info->pll;
  82. spin_lock_irqsave(&cgu->lock, flags);
  83. ctl = readl(cgu->base + pll_info->reg);
  84. spin_unlock_irqrestore(&cgu->lock, flags);
  85. m = (ctl >> pll_info->m_shift) & GENMASK(pll_info->m_bits - 1, 0);
  86. m += pll_info->m_offset;
  87. n = (ctl >> pll_info->n_shift) & GENMASK(pll_info->n_bits - 1, 0);
  88. n += pll_info->n_offset;
  89. od_enc = ctl >> pll_info->od_shift;
  90. od_enc &= GENMASK(pll_info->od_bits - 1, 0);
  91. bypass = !!(ctl & BIT(pll_info->bypass_bit));
  92. enable = !!(ctl & BIT(pll_info->enable_bit));
  93. if (bypass)
  94. return parent_rate;
  95. if (!enable)
  96. return 0;
  97. for (od = 0; od < pll_info->od_max; od++) {
  98. if (pll_info->od_encoding[od] == od_enc)
  99. break;
  100. }
  101. BUG_ON(od == pll_info->od_max);
  102. od++;
  103. return div_u64((u64)parent_rate * m, n * od);
  104. }
  105. static unsigned long
  106. ingenic_pll_calc(const struct ingenic_cgu_clk_info *clk_info,
  107. unsigned long rate, unsigned long parent_rate,
  108. unsigned *pm, unsigned *pn, unsigned *pod)
  109. {
  110. const struct ingenic_cgu_pll_info *pll_info;
  111. unsigned m, n, od;
  112. pll_info = &clk_info->pll;
  113. od = 1;
  114. /*
  115. * The frequency after the input divider must be between 10 and 50 MHz.
  116. * The highest divider yields the best resolution.
  117. */
  118. n = parent_rate / (10 * MHZ);
  119. n = min_t(unsigned, n, 1 << clk_info->pll.n_bits);
  120. n = max_t(unsigned, n, pll_info->n_offset);
  121. m = (rate / MHZ) * od * n / (parent_rate / MHZ);
  122. m = min_t(unsigned, m, 1 << clk_info->pll.m_bits);
  123. m = max_t(unsigned, m, pll_info->m_offset);
  124. if (pm)
  125. *pm = m;
  126. if (pn)
  127. *pn = n;
  128. if (pod)
  129. *pod = od;
  130. return div_u64((u64)parent_rate * m, n * od);
  131. }
  132. static long
  133. ingenic_pll_round_rate(struct clk_hw *hw, unsigned long req_rate,
  134. unsigned long *prate)
  135. {
  136. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  137. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  138. const struct ingenic_cgu_clk_info *clk_info;
  139. clk_info = &cgu->clock_info[ingenic_clk->idx];
  140. BUG_ON(clk_info->type != CGU_CLK_PLL);
  141. return ingenic_pll_calc(clk_info, req_rate, *prate, NULL, NULL, NULL);
  142. }
  143. static int
  144. ingenic_pll_set_rate(struct clk_hw *hw, unsigned long req_rate,
  145. unsigned long parent_rate)
  146. {
  147. const unsigned timeout = 100;
  148. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  149. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  150. const struct ingenic_cgu_clk_info *clk_info;
  151. const struct ingenic_cgu_pll_info *pll_info;
  152. unsigned long rate, flags;
  153. unsigned m, n, od, i;
  154. u32 ctl;
  155. clk_info = &cgu->clock_info[ingenic_clk->idx];
  156. BUG_ON(clk_info->type != CGU_CLK_PLL);
  157. pll_info = &clk_info->pll;
  158. rate = ingenic_pll_calc(clk_info, req_rate, parent_rate,
  159. &m, &n, &od);
  160. if (rate != req_rate)
  161. pr_info("ingenic-cgu: request '%s' rate %luHz, actual %luHz\n",
  162. clk_info->name, req_rate, rate);
  163. spin_lock_irqsave(&cgu->lock, flags);
  164. ctl = readl(cgu->base + pll_info->reg);
  165. ctl &= ~(GENMASK(pll_info->m_bits - 1, 0) << pll_info->m_shift);
  166. ctl |= (m - pll_info->m_offset) << pll_info->m_shift;
  167. ctl &= ~(GENMASK(pll_info->n_bits - 1, 0) << pll_info->n_shift);
  168. ctl |= (n - pll_info->n_offset) << pll_info->n_shift;
  169. ctl &= ~(GENMASK(pll_info->od_bits - 1, 0) << pll_info->od_shift);
  170. ctl |= pll_info->od_encoding[od - 1] << pll_info->od_shift;
  171. ctl &= ~BIT(pll_info->bypass_bit);
  172. ctl |= BIT(pll_info->enable_bit);
  173. writel(ctl, cgu->base + pll_info->reg);
  174. /* wait for the PLL to stabilise */
  175. for (i = 0; i < timeout; i++) {
  176. ctl = readl(cgu->base + pll_info->reg);
  177. if (ctl & BIT(pll_info->stable_bit))
  178. break;
  179. mdelay(1);
  180. }
  181. spin_unlock_irqrestore(&cgu->lock, flags);
  182. if (i == timeout)
  183. return -EBUSY;
  184. return 0;
  185. }
  186. static const struct clk_ops ingenic_pll_ops = {
  187. .recalc_rate = ingenic_pll_recalc_rate,
  188. .round_rate = ingenic_pll_round_rate,
  189. .set_rate = ingenic_pll_set_rate,
  190. };
  191. /*
  192. * Operations for all non-PLL clocks
  193. */
  194. static u8 ingenic_clk_get_parent(struct clk_hw *hw)
  195. {
  196. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  197. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  198. const struct ingenic_cgu_clk_info *clk_info;
  199. u32 reg;
  200. u8 i, hw_idx, idx = 0;
  201. clk_info = &cgu->clock_info[ingenic_clk->idx];
  202. if (clk_info->type & CGU_CLK_MUX) {
  203. reg = readl(cgu->base + clk_info->mux.reg);
  204. hw_idx = (reg >> clk_info->mux.shift) &
  205. GENMASK(clk_info->mux.bits - 1, 0);
  206. /*
  207. * Convert the hardware index to the parent index by skipping
  208. * over any -1's in the parents array.
  209. */
  210. for (i = 0; i < hw_idx; i++) {
  211. if (clk_info->parents[i] != -1)
  212. idx++;
  213. }
  214. }
  215. return idx;
  216. }
  217. static int ingenic_clk_set_parent(struct clk_hw *hw, u8 idx)
  218. {
  219. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  220. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  221. const struct ingenic_cgu_clk_info *clk_info;
  222. unsigned long flags;
  223. u8 curr_idx, hw_idx, num_poss;
  224. u32 reg, mask;
  225. clk_info = &cgu->clock_info[ingenic_clk->idx];
  226. if (clk_info->type & CGU_CLK_MUX) {
  227. /*
  228. * Convert the parent index to the hardware index by adding
  229. * 1 for any -1 in the parents array preceding the given
  230. * index. That is, we want the index of idx'th entry in
  231. * clk_info->parents which does not equal -1.
  232. */
  233. hw_idx = curr_idx = 0;
  234. num_poss = 1 << clk_info->mux.bits;
  235. for (; hw_idx < num_poss; hw_idx++) {
  236. if (clk_info->parents[hw_idx] == -1)
  237. continue;
  238. if (curr_idx == idx)
  239. break;
  240. curr_idx++;
  241. }
  242. /* idx should always be a valid parent */
  243. BUG_ON(curr_idx != idx);
  244. mask = GENMASK(clk_info->mux.bits - 1, 0);
  245. mask <<= clk_info->mux.shift;
  246. spin_lock_irqsave(&cgu->lock, flags);
  247. /* write the register */
  248. reg = readl(cgu->base + clk_info->mux.reg);
  249. reg &= ~mask;
  250. reg |= hw_idx << clk_info->mux.shift;
  251. writel(reg, cgu->base + clk_info->mux.reg);
  252. spin_unlock_irqrestore(&cgu->lock, flags);
  253. return 0;
  254. }
  255. return idx ? -EINVAL : 0;
  256. }
  257. static unsigned long
  258. ingenic_clk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
  259. {
  260. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  261. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  262. const struct ingenic_cgu_clk_info *clk_info;
  263. unsigned long rate = parent_rate;
  264. u32 div_reg, div;
  265. clk_info = &cgu->clock_info[ingenic_clk->idx];
  266. if (clk_info->type & CGU_CLK_DIV) {
  267. div_reg = readl(cgu->base + clk_info->div.reg);
  268. div = (div_reg >> clk_info->div.shift) &
  269. GENMASK(clk_info->div.bits - 1, 0);
  270. div += 1;
  271. rate /= div;
  272. }
  273. return rate;
  274. }
  275. static unsigned
  276. ingenic_clk_calc_div(const struct ingenic_cgu_clk_info *clk_info,
  277. unsigned long parent_rate, unsigned long req_rate)
  278. {
  279. unsigned div;
  280. /* calculate the divide */
  281. div = DIV_ROUND_UP(parent_rate, req_rate);
  282. /* and impose hardware constraints */
  283. div = min_t(unsigned, div, 1 << clk_info->div.bits);
  284. div = max_t(unsigned, div, 1);
  285. return div;
  286. }
  287. static long
  288. ingenic_clk_round_rate(struct clk_hw *hw, unsigned long req_rate,
  289. unsigned long *parent_rate)
  290. {
  291. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  292. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  293. const struct ingenic_cgu_clk_info *clk_info;
  294. long rate = *parent_rate;
  295. clk_info = &cgu->clock_info[ingenic_clk->idx];
  296. if (clk_info->type & CGU_CLK_DIV)
  297. rate /= ingenic_clk_calc_div(clk_info, *parent_rate, req_rate);
  298. else if (clk_info->type & CGU_CLK_FIXDIV)
  299. rate /= clk_info->fixdiv.div;
  300. return rate;
  301. }
  302. static int
  303. ingenic_clk_set_rate(struct clk_hw *hw, unsigned long req_rate,
  304. unsigned long parent_rate)
  305. {
  306. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  307. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  308. const struct ingenic_cgu_clk_info *clk_info;
  309. const unsigned timeout = 100;
  310. unsigned long rate, flags;
  311. unsigned div, i;
  312. u32 reg, mask;
  313. int ret = 0;
  314. clk_info = &cgu->clock_info[ingenic_clk->idx];
  315. if (clk_info->type & CGU_CLK_DIV) {
  316. div = ingenic_clk_calc_div(clk_info, parent_rate, req_rate);
  317. rate = parent_rate / div;
  318. if (rate != req_rate)
  319. return -EINVAL;
  320. spin_lock_irqsave(&cgu->lock, flags);
  321. reg = readl(cgu->base + clk_info->div.reg);
  322. /* update the divide */
  323. mask = GENMASK(clk_info->div.bits - 1, 0);
  324. reg &= ~(mask << clk_info->div.shift);
  325. reg |= (div - 1) << clk_info->div.shift;
  326. /* clear the stop bit */
  327. if (clk_info->div.stop_bit != -1)
  328. reg &= ~BIT(clk_info->div.stop_bit);
  329. /* set the change enable bit */
  330. if (clk_info->div.ce_bit != -1)
  331. reg |= BIT(clk_info->div.ce_bit);
  332. /* update the hardware */
  333. writel(reg, cgu->base + clk_info->div.reg);
  334. /* wait for the change to take effect */
  335. if (clk_info->div.busy_bit != -1) {
  336. for (i = 0; i < timeout; i++) {
  337. reg = readl(cgu->base + clk_info->div.reg);
  338. if (!(reg & BIT(clk_info->div.busy_bit)))
  339. break;
  340. mdelay(1);
  341. }
  342. if (i == timeout)
  343. ret = -EBUSY;
  344. }
  345. spin_unlock_irqrestore(&cgu->lock, flags);
  346. return ret;
  347. }
  348. return -EINVAL;
  349. }
  350. static int ingenic_clk_enable(struct clk_hw *hw)
  351. {
  352. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  353. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  354. const struct ingenic_cgu_clk_info *clk_info;
  355. unsigned long flags;
  356. clk_info = &cgu->clock_info[ingenic_clk->idx];
  357. if (clk_info->type & CGU_CLK_GATE) {
  358. /* ungate the clock */
  359. spin_lock_irqsave(&cgu->lock, flags);
  360. ingenic_cgu_gate_set(cgu, &clk_info->gate, false);
  361. spin_unlock_irqrestore(&cgu->lock, flags);
  362. }
  363. return 0;
  364. }
  365. static void ingenic_clk_disable(struct clk_hw *hw)
  366. {
  367. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  368. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  369. const struct ingenic_cgu_clk_info *clk_info;
  370. unsigned long flags;
  371. clk_info = &cgu->clock_info[ingenic_clk->idx];
  372. if (clk_info->type & CGU_CLK_GATE) {
  373. /* gate the clock */
  374. spin_lock_irqsave(&cgu->lock, flags);
  375. ingenic_cgu_gate_set(cgu, &clk_info->gate, true);
  376. spin_unlock_irqrestore(&cgu->lock, flags);
  377. }
  378. }
  379. static int ingenic_clk_is_enabled(struct clk_hw *hw)
  380. {
  381. struct ingenic_clk *ingenic_clk = to_ingenic_clk(hw);
  382. struct ingenic_cgu *cgu = ingenic_clk->cgu;
  383. const struct ingenic_cgu_clk_info *clk_info;
  384. unsigned long flags;
  385. int enabled = 1;
  386. clk_info = &cgu->clock_info[ingenic_clk->idx];
  387. if (clk_info->type & CGU_CLK_GATE) {
  388. spin_lock_irqsave(&cgu->lock, flags);
  389. enabled = !ingenic_cgu_gate_get(cgu, &clk_info->gate);
  390. spin_unlock_irqrestore(&cgu->lock, flags);
  391. }
  392. return enabled;
  393. }
  394. static const struct clk_ops ingenic_clk_ops = {
  395. .get_parent = ingenic_clk_get_parent,
  396. .set_parent = ingenic_clk_set_parent,
  397. .recalc_rate = ingenic_clk_recalc_rate,
  398. .round_rate = ingenic_clk_round_rate,
  399. .set_rate = ingenic_clk_set_rate,
  400. .enable = ingenic_clk_enable,
  401. .disable = ingenic_clk_disable,
  402. .is_enabled = ingenic_clk_is_enabled,
  403. };
  404. /*
  405. * Setup functions.
  406. */
  407. static int ingenic_register_clock(struct ingenic_cgu *cgu, unsigned idx)
  408. {
  409. const struct ingenic_cgu_clk_info *clk_info = &cgu->clock_info[idx];
  410. struct clk_init_data clk_init;
  411. struct ingenic_clk *ingenic_clk = NULL;
  412. struct clk *clk, *parent;
  413. const char *parent_names[4];
  414. unsigned caps, i, num_possible;
  415. int err = -EINVAL;
  416. BUILD_BUG_ON(ARRAY_SIZE(clk_info->parents) > ARRAY_SIZE(parent_names));
  417. if (clk_info->type == CGU_CLK_EXT) {
  418. clk = of_clk_get_by_name(cgu->np, clk_info->name);
  419. if (IS_ERR(clk)) {
  420. pr_err("%s: no external clock '%s' provided\n",
  421. __func__, clk_info->name);
  422. err = -ENODEV;
  423. goto out;
  424. }
  425. err = clk_register_clkdev(clk, clk_info->name, NULL);
  426. if (err) {
  427. clk_put(clk);
  428. goto out;
  429. }
  430. cgu->clocks.clks[idx] = clk;
  431. return 0;
  432. }
  433. if (!clk_info->type) {
  434. pr_err("%s: no clock type specified for '%s'\n", __func__,
  435. clk_info->name);
  436. goto out;
  437. }
  438. ingenic_clk = kzalloc(sizeof(*ingenic_clk), GFP_KERNEL);
  439. if (!ingenic_clk) {
  440. err = -ENOMEM;
  441. goto out;
  442. }
  443. ingenic_clk->hw.init = &clk_init;
  444. ingenic_clk->cgu = cgu;
  445. ingenic_clk->idx = idx;
  446. clk_init.name = clk_info->name;
  447. clk_init.flags = 0;
  448. clk_init.parent_names = parent_names;
  449. caps = clk_info->type;
  450. if (caps & (CGU_CLK_MUX | CGU_CLK_CUSTOM)) {
  451. clk_init.num_parents = 0;
  452. if (caps & CGU_CLK_MUX)
  453. num_possible = 1 << clk_info->mux.bits;
  454. else
  455. num_possible = ARRAY_SIZE(clk_info->parents);
  456. for (i = 0; i < num_possible; i++) {
  457. if (clk_info->parents[i] == -1)
  458. continue;
  459. parent = cgu->clocks.clks[clk_info->parents[i]];
  460. parent_names[clk_init.num_parents] =
  461. __clk_get_name(parent);
  462. clk_init.num_parents++;
  463. }
  464. BUG_ON(!clk_init.num_parents);
  465. BUG_ON(clk_init.num_parents > ARRAY_SIZE(parent_names));
  466. } else {
  467. BUG_ON(clk_info->parents[0] == -1);
  468. clk_init.num_parents = 1;
  469. parent = cgu->clocks.clks[clk_info->parents[0]];
  470. parent_names[0] = __clk_get_name(parent);
  471. }
  472. if (caps & CGU_CLK_CUSTOM) {
  473. clk_init.ops = clk_info->custom.clk_ops;
  474. caps &= ~CGU_CLK_CUSTOM;
  475. if (caps) {
  476. pr_err("%s: custom clock may not be combined with type 0x%x\n",
  477. __func__, caps);
  478. goto out;
  479. }
  480. } else if (caps & CGU_CLK_PLL) {
  481. clk_init.ops = &ingenic_pll_ops;
  482. caps &= ~CGU_CLK_PLL;
  483. if (caps) {
  484. pr_err("%s: PLL may not be combined with type 0x%x\n",
  485. __func__, caps);
  486. goto out;
  487. }
  488. } else {
  489. clk_init.ops = &ingenic_clk_ops;
  490. }
  491. /* nothing to do for gates or fixed dividers */
  492. caps &= ~(CGU_CLK_GATE | CGU_CLK_FIXDIV);
  493. if (caps & CGU_CLK_MUX) {
  494. if (!(caps & CGU_CLK_MUX_GLITCHFREE))
  495. clk_init.flags |= CLK_SET_PARENT_GATE;
  496. caps &= ~(CGU_CLK_MUX | CGU_CLK_MUX_GLITCHFREE);
  497. }
  498. if (caps & CGU_CLK_DIV) {
  499. caps &= ~CGU_CLK_DIV;
  500. } else {
  501. /* pass rate changes to the parent clock */
  502. clk_init.flags |= CLK_SET_RATE_PARENT;
  503. }
  504. if (caps) {
  505. pr_err("%s: unknown clock type 0x%x\n", __func__, caps);
  506. goto out;
  507. }
  508. clk = clk_register(NULL, &ingenic_clk->hw);
  509. if (IS_ERR(clk)) {
  510. pr_err("%s: failed to register clock '%s'\n", __func__,
  511. clk_info->name);
  512. err = PTR_ERR(clk);
  513. goto out;
  514. }
  515. err = clk_register_clkdev(clk, clk_info->name, NULL);
  516. if (err)
  517. goto out;
  518. cgu->clocks.clks[idx] = clk;
  519. out:
  520. if (err)
  521. kfree(ingenic_clk);
  522. return err;
  523. }
  524. struct ingenic_cgu *
  525. ingenic_cgu_new(const struct ingenic_cgu_clk_info *clock_info,
  526. unsigned num_clocks, struct device_node *np)
  527. {
  528. struct ingenic_cgu *cgu;
  529. cgu = kzalloc(sizeof(*cgu), GFP_KERNEL);
  530. if (!cgu)
  531. goto err_out;
  532. cgu->base = of_iomap(np, 0);
  533. if (!cgu->base) {
  534. pr_err("%s: failed to map CGU registers\n", __func__);
  535. goto err_out_free;
  536. }
  537. cgu->np = np;
  538. cgu->clock_info = clock_info;
  539. cgu->clocks.clk_num = num_clocks;
  540. spin_lock_init(&cgu->lock);
  541. return cgu;
  542. err_out_free:
  543. kfree(cgu);
  544. err_out:
  545. return NULL;
  546. }
  547. int ingenic_cgu_register_clocks(struct ingenic_cgu *cgu)
  548. {
  549. unsigned i;
  550. int err;
  551. cgu->clocks.clks = kcalloc(cgu->clocks.clk_num, sizeof(struct clk *),
  552. GFP_KERNEL);
  553. if (!cgu->clocks.clks) {
  554. err = -ENOMEM;
  555. goto err_out;
  556. }
  557. for (i = 0; i < cgu->clocks.clk_num; i++) {
  558. err = ingenic_register_clock(cgu, i);
  559. if (err)
  560. goto err_out_unregister;
  561. }
  562. err = of_clk_add_provider(cgu->np, of_clk_src_onecell_get,
  563. &cgu->clocks);
  564. if (err)
  565. goto err_out_unregister;
  566. return 0;
  567. err_out_unregister:
  568. for (i = 0; i < cgu->clocks.clk_num; i++) {
  569. if (!cgu->clocks.clks[i])
  570. continue;
  571. if (cgu->clock_info[i].type & CGU_CLK_EXT)
  572. clk_put(cgu->clocks.clks[i]);
  573. else
  574. clk_unregister(cgu->clocks.clks[i]);
  575. }
  576. kfree(cgu->clocks.clks);
  577. err_out:
  578. return err;
  579. }