clkt_clksel.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * clkt_clksel.c - OMAP2/3/4 clksel clock functions
  3. *
  4. * Copyright (C) 2005-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2004-2010 Nokia Corporation
  6. *
  7. * Contacts:
  8. * Richard Woodruff <r-woodruff2@ti.com>
  9. * Paul Walmsley
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. *
  16. * clksel clocks are clocks that do not have a fixed parent, or that
  17. * can divide their parent's rate, or possibly both at the same time, based
  18. * on the contents of a hardware register bitfield.
  19. *
  20. * All of the various mux and divider settings can be encoded into
  21. * struct clksel* data structures, and then these can be autogenerated
  22. * from some hardware database for each new chip generation. This
  23. * should avoid the need to write, review, and validate a lot of new
  24. * clock code for each new chip, since it can be exported from the SoC
  25. * design flow. This is now done on OMAP4.
  26. *
  27. * The fusion of mux and divider clocks is a software creation. In
  28. * hardware reality, the multiplexer (parent selection) and the
  29. * divider exist separately. XXX At some point these clksel clocks
  30. * should be split into "divider" clocks and "mux" clocks to better
  31. * match the hardware.
  32. *
  33. * (The name "clksel" comes from the name of the corresponding
  34. * register field in the OMAP2/3 family of SoCs.)
  35. *
  36. * XXX Currently these clocks are only used in the OMAP2/3/4 code, but
  37. * many of the OMAP1 clocks should be convertible to use this
  38. * mechanism.
  39. */
  40. #undef DEBUG
  41. #include <linux/kernel.h>
  42. #include <linux/errno.h>
  43. #ifdef CONFIG_COMMON_CLK
  44. #include <linux/clk-provider.h>
  45. #else
  46. #include <linux/clk.h>
  47. #endif
  48. #include <linux/io.h>
  49. #include <linux/bug.h>
  50. #include "clock.h"
  51. /* Private functions */
  52. /**
  53. * _get_clksel_by_parent() - return clksel struct for a given clk & parent
  54. * @clk: OMAP struct clk ptr to inspect
  55. * @src_clk: OMAP struct clk ptr of the parent clk to search for
  56. *
  57. * Scan the struct clksel array associated with the clock to find
  58. * the element associated with the supplied parent clock address.
  59. * Returns a pointer to the struct clksel on success or NULL on error.
  60. */
  61. #ifdef CONFIG_COMMON_CLK
  62. static const struct clksel *_get_clksel_by_parent(struct clk_hw_omap *clk,
  63. #else
  64. static const struct clksel *_get_clksel_by_parent(struct clk *clk,
  65. #endif
  66. struct clk *src_clk)
  67. {
  68. const struct clksel *clks;
  69. if (!src_clk)
  70. return NULL;
  71. for (clks = clk->clksel; clks->parent; clks++)
  72. if (clks->parent == src_clk)
  73. break; /* Found the requested parent */
  74. if (!clks->parent) {
  75. /* This indicates a data problem */
  76. WARN(1, "clock: %s: could not find parent clock %s in clksel array\n",
  77. #ifdef CONFIG_COMMON_CLK
  78. __clk_get_name(clk->hw.clk), __clk_get_name(src_clk));
  79. #else
  80. __clk_get_name(clk), __clk_get_name(src_clk));
  81. #endif
  82. return NULL;
  83. }
  84. return clks;
  85. }
  86. /**
  87. * _get_div_and_fieldval() - find the new clksel divisor and field value to use
  88. * @src_clk: planned new parent struct clk *
  89. * @clk: struct clk * that is being reparented
  90. * @field_val: pointer to a u32 to contain the register data for the divisor
  91. *
  92. * Given an intended new parent struct clk * @src_clk, and the struct
  93. * clk * @clk to the clock that is being reparented, find the
  94. * appropriate rate divisor for the new clock (returned as the return
  95. * value), and the corresponding register bitfield data to program to
  96. * reach that divisor (returned in the u32 pointed to by @field_val).
  97. * Returns 0 on error, or returns the newly-selected divisor upon
  98. * success (in this latter case, the corresponding register bitfield
  99. * value is passed back in the variable pointed to by @field_val)
  100. */
  101. #ifndef CONFIG_COMMON_CLK
  102. static u8 _get_div_and_fieldval(struct clk *src_clk, struct clk *clk,
  103. u32 *field_val)
  104. {
  105. const struct clksel *clks;
  106. const struct clksel_rate *clkr, *max_clkr = NULL;
  107. u8 max_div = 0;
  108. clks = _get_clksel_by_parent(clk, src_clk);
  109. if (!clks)
  110. return 0;
  111. /*
  112. * Find the highest divisor (e.g., the one resulting in the
  113. * lowest rate) to use as the default. This should avoid
  114. * clock rates that are too high for the device. XXX A better
  115. * solution here would be to try to determine if there is a
  116. * divisor matching the original clock rate before the parent
  117. * switch, and if it cannot be found, to fall back to the
  118. * highest divisor.
  119. */
  120. for (clkr = clks->rates; clkr->div; clkr++) {
  121. if (!(clkr->flags & cpu_mask))
  122. continue;
  123. if (clkr->div > max_div) {
  124. max_div = clkr->div;
  125. max_clkr = clkr;
  126. }
  127. }
  128. if (max_div == 0) {
  129. /* This indicates an error in the clksel data */
  130. WARN(1, "clock: %s: could not find divisor for parent %s\n",
  131. __clk_get_name(clk),
  132. __clk_get_name(__clk_get_parent(src_clk)));
  133. return 0;
  134. }
  135. *field_val = max_clkr->val;
  136. return max_div;
  137. }
  138. #endif
  139. /**
  140. * _write_clksel_reg() - program a clock's clksel register in hardware
  141. * @clk: struct clk * to program
  142. * @v: clksel bitfield value to program (with LSB at bit 0)
  143. *
  144. * Shift the clksel register bitfield value @v to its appropriate
  145. * location in the clksel register and write it in. This function
  146. * will ensure that the write to the clksel_reg reaches its
  147. * destination before returning -- important since PRM and CM register
  148. * accesses can be quite slow compared to ARM cycles -- but does not
  149. * take into account any time the hardware might take to switch the
  150. * clock source.
  151. */
  152. #ifdef CONFIG_COMMON_CLK
  153. static void _write_clksel_reg(struct clk_hw_omap *clk, u32 field_val)
  154. #else
  155. static void _write_clksel_reg(struct clk *clk, u32 field_val)
  156. #endif
  157. {
  158. u32 v;
  159. v = __raw_readl(clk->clksel_reg);
  160. v &= ~clk->clksel_mask;
  161. v |= field_val << __ffs(clk->clksel_mask);
  162. __raw_writel(v, clk->clksel_reg);
  163. v = __raw_readl(clk->clksel_reg); /* OCP barrier */
  164. }
  165. /**
  166. * _clksel_to_divisor() - turn clksel field value into integer divider
  167. * @clk: OMAP struct clk to use
  168. * @field_val: register field value to find
  169. *
  170. * Given a struct clk of a rate-selectable clksel clock, and a register field
  171. * value to search for, find the corresponding clock divisor. The register
  172. * field value should be pre-masked and shifted down so the LSB is at bit 0
  173. * before calling. Returns 0 on error or returns the actual integer divisor
  174. * upon success.
  175. */
  176. #ifdef CONFIG_COMMON_CLK
  177. static u32 _clksel_to_divisor(struct clk_hw_omap *clk, u32 field_val)
  178. #else
  179. static u32 _clksel_to_divisor(struct clk *clk, u32 field_val)
  180. #endif
  181. {
  182. const struct clksel *clks;
  183. const struct clksel_rate *clkr;
  184. struct clk *parent;
  185. #ifdef CONFIG_COMMON_CLK
  186. parent = __clk_get_parent(clk->hw.clk);
  187. #else
  188. parent = __clk_get_parent(clk);
  189. #endif
  190. clks = _get_clksel_by_parent(clk, parent);
  191. if (!clks)
  192. return 0;
  193. for (clkr = clks->rates; clkr->div; clkr++) {
  194. if (!(clkr->flags & cpu_mask))
  195. continue;
  196. if (clkr->val == field_val)
  197. break;
  198. }
  199. if (!clkr->div) {
  200. /* This indicates a data error */
  201. WARN(1, "clock: %s: could not find fieldval %d for parent %s\n",
  202. #ifdef CONFIG_COMMON_CLK
  203. __clk_get_name(clk->hw.clk), field_val,
  204. __clk_get_name(parent));
  205. #else
  206. __clk_get_name(clk), field_val, __clk_get_name(parent));
  207. #endif
  208. return 0;
  209. }
  210. return clkr->div;
  211. }
  212. /**
  213. * _divisor_to_clksel() - turn clksel integer divisor into a field value
  214. * @clk: OMAP struct clk to use
  215. * @div: integer divisor to search for
  216. *
  217. * Given a struct clk of a rate-selectable clksel clock, and a clock
  218. * divisor, find the corresponding register field value. Returns the
  219. * register field value _before_ left-shifting (i.e., LSB is at bit
  220. * 0); or returns 0xFFFFFFFF (~0) upon error.
  221. */
  222. #ifdef CONFIG_COMMON_CLK
  223. static u32 _divisor_to_clksel(struct clk_hw_omap *clk, u32 div)
  224. #else
  225. static u32 _divisor_to_clksel(struct clk *clk, u32 div)
  226. #endif
  227. {
  228. const struct clksel *clks;
  229. const struct clksel_rate *clkr;
  230. struct clk *parent;
  231. /* should never happen */
  232. WARN_ON(div == 0);
  233. #ifdef CONFIG_COMMON_CLK
  234. parent = __clk_get_parent(clk->hw.clk);
  235. #else
  236. parent = __clk_get_parent(clk);
  237. #endif
  238. clks = _get_clksel_by_parent(clk, parent);
  239. if (!clks)
  240. return ~0;
  241. for (clkr = clks->rates; clkr->div; clkr++) {
  242. if (!(clkr->flags & cpu_mask))
  243. continue;
  244. if (clkr->div == div)
  245. break;
  246. }
  247. if (!clkr->div) {
  248. pr_err("clock: %s: could not find divisor %d for parent %s\n",
  249. #ifdef CONFIG_COMMON_CLK
  250. __clk_get_name(clk->hw.clk), div,
  251. __clk_get_name(parent));
  252. #else
  253. __clk_get_name(clk), div, __clk_get_name(parent));
  254. #endif
  255. return ~0;
  256. }
  257. return clkr->val;
  258. }
  259. /**
  260. * _read_divisor() - get current divisor applied to parent clock (from hdwr)
  261. * @clk: OMAP struct clk to use.
  262. *
  263. * Read the current divisor register value for @clk that is programmed
  264. * into the hardware, convert it into the actual divisor value, and
  265. * return it; or return 0 on error.
  266. */
  267. #ifdef CONFIG_COMMON_CLK
  268. static u32 _read_divisor(struct clk_hw_omap *clk)
  269. #else
  270. static u32 _read_divisor(struct clk *clk)
  271. #endif
  272. {
  273. u32 v;
  274. if (!clk->clksel || !clk->clksel_mask)
  275. return 0;
  276. v = __raw_readl(clk->clksel_reg);
  277. v &= clk->clksel_mask;
  278. v >>= __ffs(clk->clksel_mask);
  279. return _clksel_to_divisor(clk, v);
  280. }
  281. /* Public functions */
  282. /**
  283. * omap2_clksel_round_rate_div() - find divisor for the given clock and rate
  284. * @clk: OMAP struct clk to use
  285. * @target_rate: desired clock rate
  286. * @new_div: ptr to where we should store the divisor
  287. *
  288. * Finds 'best' divider value in an array based on the source and target
  289. * rates. The divider array must be sorted with smallest divider first.
  290. * This function is also used by the DPLL3 M2 divider code.
  291. *
  292. * Returns the rounded clock rate or returns 0xffffffff on error.
  293. */
  294. #ifdef CONFIG_COMMON_CLK
  295. u32 omap2_clksel_round_rate_div(struct clk_hw_omap *clk,
  296. unsigned long target_rate,
  297. #else
  298. u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
  299. #endif
  300. u32 *new_div)
  301. {
  302. unsigned long test_rate;
  303. const struct clksel *clks;
  304. const struct clksel_rate *clkr;
  305. u32 last_div = 0;
  306. struct clk *parent;
  307. unsigned long parent_rate;
  308. const char *clk_name;
  309. #ifdef CONFIG_COMMON_CLK
  310. parent = __clk_get_parent(clk->hw.clk);
  311. clk_name = __clk_get_name(clk->hw.clk);
  312. #else
  313. parent = __clk_get_parent(clk);
  314. clk_name = __clk_get_name(clk);
  315. #endif
  316. parent_rate = __clk_get_rate(parent);
  317. if (!clk->clksel || !clk->clksel_mask)
  318. return ~0;
  319. pr_debug("clock: clksel_round_rate_div: %s target_rate %ld\n",
  320. clk_name, target_rate);
  321. *new_div = 1;
  322. clks = _get_clksel_by_parent(clk, parent);
  323. if (!clks)
  324. return ~0;
  325. for (clkr = clks->rates; clkr->div; clkr++) {
  326. if (!(clkr->flags & cpu_mask))
  327. continue;
  328. /* Sanity check */
  329. if (clkr->div <= last_div)
  330. pr_err("clock: %s: clksel_rate table not sorted\n",
  331. clk_name);
  332. last_div = clkr->div;
  333. test_rate = parent_rate / clkr->div;
  334. if (test_rate <= target_rate)
  335. break; /* found it */
  336. }
  337. if (!clkr->div) {
  338. pr_err("clock: %s: could not find divisor for target rate %ld for parent %s\n",
  339. clk_name, target_rate, __clk_get_name(parent));
  340. return ~0;
  341. }
  342. *new_div = clkr->div;
  343. pr_debug("clock: new_div = %d, new_rate = %ld\n", *new_div,
  344. (parent_rate / clkr->div));
  345. return parent_rate / clkr->div;
  346. }
  347. /*
  348. * Clocktype interface functions to the OMAP clock code
  349. * (i.e., those used in struct clk field function pointers, etc.)
  350. */
  351. #ifdef CONFIG_COMMON_CLK
  352. /**
  353. * omap2_clksel_find_parent_index() - return the array index of the current
  354. * hardware parent of @hw
  355. * @hw: struct clk_hw * to find the current hardware parent of
  356. *
  357. * Given a struct clk_hw pointer @hw to the 'hw' member of a struct
  358. * clk_hw_omap record representing a source-selectable hardware clock,
  359. * read the hardware register and determine what its parent is
  360. * currently set to. Intended to be called only by the common clock
  361. * framework struct clk_hw_ops.get_parent function pointer. Return
  362. * the array index of this parent clock upon success -- there is no
  363. * way to return an error, so if we encounter an error, just WARN()
  364. * and pretend that we know that we're doing.
  365. */
  366. u8 omap2_clksel_find_parent_index(struct clk_hw *hw)
  367. {
  368. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  369. const struct clksel *clks;
  370. const struct clksel_rate *clkr;
  371. u32 r, found = 0;
  372. struct clk *parent;
  373. const char *clk_name;
  374. int ret = 0, f = 0;
  375. parent = __clk_get_parent(hw->clk);
  376. clk_name = __clk_get_name(hw->clk);
  377. /* XXX should be able to return an error */
  378. WARN((!clk->clksel || !clk->clksel_mask),
  379. "clock: %s: attempt to call on a non-clksel clock", clk_name);
  380. r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  381. r >>= __ffs(clk->clksel_mask);
  382. for (clks = clk->clksel; clks->parent && !found; clks++) {
  383. for (clkr = clks->rates; clkr->div && !found; clkr++) {
  384. if (!(clkr->flags & cpu_mask))
  385. continue;
  386. if (clkr->val == r) {
  387. found = 1;
  388. ret = f;
  389. }
  390. }
  391. f++;
  392. }
  393. /* This indicates a data error */
  394. WARN(!found, "clock: %s: init parent: could not find regval %0x\n",
  395. clk_name, r);
  396. return ret;
  397. }
  398. #else
  399. /**
  400. * omap2_init_clksel_parent() - set a clksel clk's parent field from the hdwr
  401. * @clk: OMAP clock struct ptr to use
  402. *
  403. * Given a pointer @clk to a source-selectable struct clk, read the
  404. * hardware register and determine what its parent is currently set
  405. * to. Update @clk's .parent field with the appropriate clk ptr. No
  406. * return value.
  407. */
  408. void omap2_init_clksel_parent(struct clk *clk)
  409. {
  410. const struct clksel *clks;
  411. const struct clksel_rate *clkr;
  412. u32 r, found = 0;
  413. struct clk *parent;
  414. const char *clk_name;
  415. if (!clk->clksel || !clk->clksel_mask)
  416. return;
  417. parent = __clk_get_parent(clk);
  418. clk_name = __clk_get_name(clk);
  419. r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
  420. r >>= __ffs(clk->clksel_mask);
  421. for (clks = clk->clksel; clks->parent && !found; clks++) {
  422. for (clkr = clks->rates; clkr->div && !found; clkr++) {
  423. if (!(clkr->flags & cpu_mask))
  424. continue;
  425. if (clkr->val == r) {
  426. if (parent != clks->parent) {
  427. pr_debug("clock: %s: inited parent to %s (was %s)\n",
  428. clk_name,
  429. __clk_get_name(clks->parent),
  430. ((parent) ?
  431. __clk_get_name(parent) :
  432. "NULL"));
  433. clk_reparent(clk, clks->parent);
  434. }
  435. found = 1;
  436. }
  437. }
  438. }
  439. /* This indicates a data error */
  440. WARN(!found, "clock: %s: init parent: could not find regval %0x\n",
  441. clk_name, r);
  442. return;
  443. }
  444. #endif
  445. /**
  446. * omap2_clksel_recalc() - function ptr to pass via struct clk .recalc field
  447. * @clk: struct clk *
  448. *
  449. * This function is intended to be called only by the clock framework.
  450. * Each clksel clock should have its struct clk .recalc field set to this
  451. * function. Returns the clock's current rate, based on its parent's rate
  452. * and its current divisor setting in the hardware.
  453. */
  454. #ifdef CONFIG_COMMON_CLK
  455. unsigned long omap2_clksel_recalc(struct clk_hw *hw, unsigned long parent_rate)
  456. {
  457. unsigned long rate;
  458. u32 div = 0;
  459. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  460. if (!parent_rate)
  461. return 0;
  462. div = _read_divisor(clk);
  463. if (!div)
  464. rate = parent_rate;
  465. else
  466. rate = parent_rate / div;
  467. pr_debug("%s: recalc'd %s's rate to %lu (div %d)\n", __func__,
  468. __clk_get_name(hw->clk), rate, div);
  469. return rate;
  470. }
  471. #else
  472. unsigned long omap2_clksel_recalc(struct clk *clk)
  473. {
  474. unsigned long rate;
  475. u32 div = 0;
  476. struct clk *parent;
  477. div = _read_divisor(clk);
  478. if (div == 0)
  479. return __clk_get_rate(clk);
  480. parent = __clk_get_parent(clk);
  481. rate = __clk_get_rate(parent) / div;
  482. pr_debug("clock: %s: recalc'd rate is %ld (div %d)\n",
  483. __clk_get_name(clk), rate, div);
  484. return rate;
  485. }
  486. #endif
  487. /**
  488. * omap2_clksel_round_rate() - find rounded rate for the given clock and rate
  489. * @clk: OMAP struct clk to use
  490. * @target_rate: desired clock rate
  491. *
  492. * This function is intended to be called only by the clock framework.
  493. * Finds best target rate based on the source clock and possible dividers.
  494. * rates. The divider array must be sorted with smallest divider first.
  495. *
  496. * Returns the rounded clock rate or returns 0xffffffff on error.
  497. */
  498. #ifdef CONFIG_COMMON_CLK
  499. long omap2_clksel_round_rate(struct clk_hw *hw, unsigned long target_rate,
  500. unsigned long *parent_rate)
  501. {
  502. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  503. #else
  504. long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
  505. {
  506. #endif
  507. u32 new_div;
  508. return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
  509. }
  510. /**
  511. * omap2_clksel_set_rate() - program clock rate in hardware
  512. * @clk: struct clk * to program rate
  513. * @rate: target rate to program
  514. *
  515. * This function is intended to be called only by the clock framework.
  516. * Program @clk's rate to @rate in the hardware. The clock can be
  517. * either enabled or disabled when this happens, although if the clock
  518. * is enabled, some downstream devices may glitch or behave
  519. * unpredictably when the clock rate is changed - this depends on the
  520. * hardware. This function does not currently check the usecount of
  521. * the clock, so if multiple drivers are using the clock, and the rate
  522. * is changed, they will all be affected without any notification.
  523. * Returns -EINVAL upon error, or 0 upon success.
  524. */
  525. #ifdef CONFIG_COMMON_CLK
  526. int omap2_clksel_set_rate(struct clk_hw *hw, unsigned long rate,
  527. unsigned long parent_rate)
  528. {
  529. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  530. #else
  531. int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
  532. {
  533. #endif
  534. u32 field_val, validrate, new_div = 0;
  535. if (!clk->clksel || !clk->clksel_mask)
  536. return -EINVAL;
  537. validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
  538. if (validrate != rate)
  539. return -EINVAL;
  540. field_val = _divisor_to_clksel(clk, new_div);
  541. if (field_val == ~0)
  542. return -EINVAL;
  543. _write_clksel_reg(clk, field_val);
  544. #ifdef CONFIG_COMMON_CLK
  545. pr_debug("clock: %s: set rate to %ld\n", __clk_get_name(hw->clk),
  546. __clk_get_rate(hw->clk));
  547. #else
  548. pr_debug("clock: %s: set rate to %ld\n", __clk_get_name(clk),
  549. __clk_get_rate(clk));
  550. clk->rate = __clk_get_rate(__clk_get_parent(clk)) / new_div;
  551. #endif
  552. return 0;
  553. }
  554. /*
  555. * Clksel parent setting function - not passed in struct clk function
  556. * pointer - instead, the OMAP clock code currently assumes that any
  557. * parent-setting clock is a clksel clock, and calls
  558. * omap2_clksel_set_parent() by default
  559. */
  560. /**
  561. * omap2_clksel_set_parent() - change a clock's parent clock
  562. * @clk: struct clk * of the child clock
  563. * @new_parent: struct clk * of the new parent clock
  564. *
  565. * This function is intended to be called only by the clock framework.
  566. * Change the parent clock of clock @clk to @new_parent. This is
  567. * intended to be used while @clk is disabled. This function does not
  568. * currently check the usecount of the clock, so if multiple drivers
  569. * are using the clock, and the parent is changed, they will all be
  570. * affected without any notification. Returns -EINVAL upon error, or
  571. * 0 upon success.
  572. */
  573. #ifdef CONFIG_COMMON_CLK
  574. int omap2_clksel_set_parent(struct clk_hw *hw, u8 field_val)
  575. {
  576. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  577. if (!clk->clksel || !clk->clksel_mask)
  578. return -EINVAL;
  579. _write_clksel_reg(clk, field_val);
  580. return 0;
  581. }
  582. #else
  583. int omap2_clksel_set_parent(struct clk *clk, struct clk *new_parent)
  584. {
  585. u32 field_val = 0;
  586. u32 parent_div;
  587. if (!clk->clksel || !clk->clksel_mask)
  588. return -EINVAL;
  589. parent_div = _get_div_and_fieldval(new_parent, clk, &field_val);
  590. if (!parent_div)
  591. return -EINVAL;
  592. _write_clksel_reg(clk, field_val);
  593. clk_reparent(clk, new_parent);
  594. /* CLKSEL clocks follow their parents' rates, divided by a divisor */
  595. clk->rate = __clk_get_rate(new_parent);
  596. if (parent_div > 0)
  597. __clk_get_rate(clk) /= parent_div;
  598. pr_debug("clock: %s: set parent to %s (new rate %ld)\n",
  599. __clk_get_name(clk),
  600. __clk_get_name(__clk_get_parent(clk)),
  601. __clk_get_rate(clk));
  602. return 0;
  603. }
  604. #endif