175-add-regmap-mux-div.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. From 7727b1cae43e9abac746ef016c3dbf50ee81a6d6 Mon Sep 17 00:00:00 2001
  2. From: Georgi Djakov <georgi.djakov@linaro.org>
  3. Date: Wed, 18 Mar 2015 17:23:29 +0200
  4. Subject: clk: qcom: Add support for regmap mux-div clocks
  5. Add support for hardware that support switching both parent clocks and the
  6. divider at the same time. This avoids generating intermediate frequencies
  7. from either the old parent clock and new divider or new parent clock and
  8. old divider combinations.
  9. Signed-off-by: Georgi Djakov <georgi.djakov@linaro.org>
  10. ---
  11. drivers/clk/qcom/Makefile | 1 +
  12. drivers/clk/qcom/clk-regmap-mux-div.c | 288 ++++++++++++++++++++++++++++++++++
  13. drivers/clk/qcom/clk-regmap-mux-div.h | 63 ++++++++
  14. 3 files changed, 352 insertions(+)
  15. create mode 100644 drivers/clk/qcom/clk-regmap-mux-div.c
  16. create mode 100644 drivers/clk/qcom/clk-regmap-mux-div.h
  17. --- a/drivers/clk/qcom/Makefile
  18. +++ b/drivers/clk/qcom/Makefile
  19. @@ -8,6 +8,7 @@ clk-qcom-y += clk-rcg2.o
  20. clk-qcom-y += clk-branch.o
  21. clk-qcom-y += clk-regmap-divider.o
  22. clk-qcom-y += clk-regmap-mux.o
  23. +clk-qcom-y += clk-regmap-mux-div.o
  24. clk-qcom-$(CONFIG_KRAIT_CLOCKS) += clk-krait.o
  25. obj-$(CONFIG_KPSS_XCC) += kpss-xcc.o
  26. clk-qcom-y += clk-hfpll.o
  27. --- /dev/null
  28. +++ b/drivers/clk/qcom/clk-regmap-mux-div.c
  29. @@ -0,0 +1,288 @@
  30. +/*
  31. + * Copyright (c) 2015, Linaro Limited
  32. + * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  33. + *
  34. + * This software is licensed under the terms of the GNU General Public
  35. + * License version 2, as published by the Free Software Foundation, and
  36. + * may be copied, distributed, and modified under those terms.
  37. + *
  38. + * This program is distributed in the hope that it will be useful,
  39. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. + * GNU General Public License for more details.
  42. + */
  43. +
  44. +#include <linux/bitops.h>
  45. +#include <linux/clk.h>
  46. +#include <linux/delay.h>
  47. +#include <linux/export.h>
  48. +#include <linux/kernel.h>
  49. +#include <linux/regmap.h>
  50. +
  51. +#include "clk-regmap-mux-div.h"
  52. +
  53. +#define CMD_RCGR 0x0
  54. +#define CMD_RCGR_UPDATE BIT(0)
  55. +#define CMD_RCGR_DIRTY_CFG BIT(4)
  56. +#define CMD_RCGR_ROOT_OFF BIT(31)
  57. +#define CFG_RCGR 0x4
  58. +
  59. +static int __mux_div_update_config(struct clk_regmap_mux_div *md)
  60. +{
  61. + int ret;
  62. + u32 val, count;
  63. + const char *name = clk_hw_get_name(&md->clkr.hw);
  64. +
  65. + ret = regmap_update_bits(md->clkr.regmap, CMD_RCGR + md->reg_offset,
  66. + CMD_RCGR_UPDATE, CMD_RCGR_UPDATE);
  67. + if (ret)
  68. + return ret;
  69. +
  70. + /* Wait for update to take effect */
  71. + for (count = 500; count > 0; count--) {
  72. + ret = regmap_read(md->clkr.regmap, CMD_RCGR + md->reg_offset,
  73. + &val);
  74. + if (ret)
  75. + return ret;
  76. + if (!(val & CMD_RCGR_UPDATE))
  77. + return 0;
  78. + udelay(1);
  79. + }
  80. +
  81. + pr_err("%s: RCG did not update its configuration", name);
  82. + return -EBUSY;
  83. +}
  84. +
  85. +static int __mux_div_set_src_div(struct clk_regmap_mux_div *md, u32 src_sel,
  86. + u32 src_div)
  87. +{
  88. + int ret;
  89. + u32 val, mask;
  90. +
  91. + val = (src_div << md->hid_shift) | (src_sel << md->src_shift);
  92. + mask = ((BIT(md->hid_width) - 1) << md->hid_shift) |
  93. + ((BIT(md->src_width) - 1) << md->src_shift);
  94. +
  95. + ret = regmap_update_bits(md->clkr.regmap, CFG_RCGR + md->reg_offset,
  96. + mask, val);
  97. + if (ret)
  98. + return ret;
  99. +
  100. + return __mux_div_update_config(md);
  101. +}
  102. +
  103. +static void __mux_div_get_src_div(struct clk_regmap_mux_div *md, u32 *src_sel,
  104. + u32 *src_div)
  105. +{
  106. + u32 val, div, src;
  107. + const char *name = clk_hw_get_name(&md->clkr.hw);
  108. +
  109. + regmap_read(md->clkr.regmap, CMD_RCGR + md->reg_offset, &val);
  110. +
  111. + if (val & CMD_RCGR_DIRTY_CFG) {
  112. + pr_err("%s: RCG configuration is pending\n", name);
  113. + return;
  114. + }
  115. +
  116. + regmap_read(md->clkr.regmap, CFG_RCGR + md->reg_offset, &val);
  117. + src = (val >> md->src_shift);
  118. + src &= BIT(md->src_width) - 1;
  119. + *src_sel = src;
  120. +
  121. + div = (val >> md->hid_shift);
  122. + div &= BIT(md->hid_width) - 1;
  123. + *src_div = div;
  124. +}
  125. +
  126. +static int mux_div_enable(struct clk_hw *hw)
  127. +{
  128. + struct clk_regmap_mux_div *md = to_clk_regmap_mux_div(hw);
  129. +
  130. + return __mux_div_set_src_div(md, md->src_sel, md->div);
  131. +}
  132. +
  133. +static inline bool is_better_rate(unsigned long req, unsigned long best,
  134. + unsigned long new)
  135. +{
  136. + return (req <= new && new < best) || (best < req && best < new);
  137. +}
  138. +
  139. +static int mux_div_determine_rate(struct clk_hw *hw,
  140. + struct clk_rate_request *req)
  141. +{
  142. + struct clk_regmap_mux_div *md = to_clk_regmap_mux_div(hw);
  143. + unsigned int i, div, max_div;
  144. + unsigned long actual_rate, best_rate = 0;
  145. + unsigned long req_rate = req->rate;
  146. +
  147. + for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  148. + struct clk_hw *parent = clk_hw_get_parent_by_index(hw, i);
  149. + unsigned long parent_rate = clk_hw_get_rate(parent);
  150. +
  151. + max_div = BIT(md->hid_width) - 1;
  152. + for (div = 1; div < max_div; div++) {
  153. + parent_rate = mult_frac(req_rate, div, 2);
  154. + parent_rate = clk_hw_round_rate(parent, parent_rate);
  155. + actual_rate = mult_frac(parent_rate, 2, div);
  156. +
  157. + if (is_better_rate(req_rate, best_rate, actual_rate)) {
  158. + best_rate = actual_rate;
  159. + req->rate = best_rate;
  160. + req->best_parent_rate = parent_rate;
  161. + req->best_parent_hw = parent;
  162. + }
  163. +
  164. + if (actual_rate < req_rate || best_rate <= req_rate)
  165. + break;
  166. + }
  167. + }
  168. +
  169. + if (!best_rate)
  170. + return -EINVAL;
  171. +
  172. + return 0;
  173. +}
  174. +
  175. +static int __mux_div_set_rate_and_parent(struct clk_hw *hw, unsigned long rate,
  176. + unsigned long prate, u32 src_sel)
  177. +{
  178. + struct clk_regmap_mux_div *md = to_clk_regmap_mux_div(hw);
  179. + int ret, i;
  180. + u32 div, max_div, best_src = 0, best_div = 0;
  181. + unsigned long actual_rate, best_rate = 0;
  182. +
  183. + for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
  184. + struct clk_hw *parent = clk_hw_get_parent_by_index(hw, i);
  185. + unsigned long parent_rate = clk_hw_get_rate(parent);
  186. +
  187. + max_div = BIT(md->hid_width) - 1;
  188. + for (div = 1; div < max_div; div++) {
  189. + parent_rate = mult_frac(rate, div, 2);
  190. + parent_rate = clk_hw_round_rate(parent, parent_rate);
  191. + actual_rate = mult_frac(parent_rate, 2, div);
  192. +
  193. + if (is_better_rate(rate, best_rate, actual_rate)) {
  194. + best_rate = actual_rate;
  195. + best_src = md->parent_map[i].cfg;
  196. + best_div = div - 1;
  197. + }
  198. +
  199. + if (actual_rate < rate || best_rate <= rate)
  200. + break;
  201. + }
  202. + }
  203. +
  204. + ret = __mux_div_set_src_div(md, best_src, best_div);
  205. + if (!ret) {
  206. + md->div = best_div;
  207. + md->src_sel = best_src;
  208. + }
  209. +
  210. + return ret;
  211. +}
  212. +
  213. +static u8 mux_div_get_parent(struct clk_hw *hw)
  214. +{
  215. + struct clk_regmap_mux_div *md = to_clk_regmap_mux_div(hw);
  216. + const char *name = clk_hw_get_name(hw);
  217. + u32 i, div, src;
  218. +
  219. + __mux_div_get_src_div(md, &src, &div);
  220. +
  221. + for (i = 0; i < clk_hw_get_num_parents(hw); i++)
  222. + if (src == md->parent_map[i].cfg)
  223. + return i;
  224. +
  225. + pr_err("%s: Can't find parent %d\n", name, src);
  226. + return 0;
  227. +}
  228. +
  229. +static int mux_div_set_parent(struct clk_hw *hw, u8 index)
  230. +{
  231. + struct clk_regmap_mux_div *md = to_clk_regmap_mux_div(hw);
  232. +
  233. + return __mux_div_set_src_div(md, md->parent_map[index].cfg, md->div);
  234. +}
  235. +
  236. +static int mux_div_set_rate(struct clk_hw *hw,
  237. + unsigned long rate, unsigned long prate)
  238. +{
  239. + struct clk_regmap_mux_div *md = to_clk_regmap_mux_div(hw);
  240. +
  241. + return __mux_div_set_rate_and_parent(hw, rate, prate, md->src_sel);
  242. +}
  243. +
  244. +static int mux_div_set_rate_and_parent(struct clk_hw *hw, unsigned long rate,
  245. + unsigned long prate, u8 index)
  246. +{
  247. + struct clk_regmap_mux_div *md = to_clk_regmap_mux_div(hw);
  248. +
  249. + return __mux_div_set_rate_and_parent(hw, rate, prate,
  250. + md->parent_map[index].cfg);
  251. +}
  252. +
  253. +static unsigned long mux_div_recalc_rate(struct clk_hw *hw, unsigned long prate)
  254. +{
  255. + struct clk_regmap_mux_div *md = to_clk_regmap_mux_div(hw);
  256. + u32 div, src;
  257. + int i, num_parents = clk_hw_get_num_parents(hw);
  258. + const char *name = clk_hw_get_name(hw);
  259. +
  260. + __mux_div_get_src_div(md, &src, &div);
  261. + for (i = 0; i < num_parents; i++)
  262. + if (src == md->parent_map[i].cfg) {
  263. + struct clk_hw *p = clk_hw_get_parent_by_index(hw, i);
  264. + unsigned long parent_rate = clk_hw_get_rate(p);
  265. +
  266. + return mult_frac(parent_rate, 2, div + 1);
  267. + }
  268. +
  269. + pr_err("%s: Can't find parent %d\n", name, src);
  270. + return 0;
  271. +}
  272. +
  273. +static struct clk_hw *mux_div_get_safe_parent(struct clk_hw *hw,
  274. + unsigned long *safe_freq)
  275. +{
  276. + int i;
  277. + struct clk_regmap_mux_div *md = to_clk_regmap_mux_div(hw);
  278. +
  279. + if (md->safe_freq)
  280. + *safe_freq = md->safe_freq;
  281. +
  282. + for (i = 0; i < clk_hw_get_num_parents(hw); i++)
  283. + if (md->safe_src == md->parent_map[i].cfg)
  284. + break;
  285. +
  286. + return clk_hw_get_parent_by_index(hw, i);
  287. +}
  288. +
  289. +static void mux_div_disable(struct clk_hw *hw)
  290. +{
  291. + struct clk_regmap_mux_div *md = to_clk_regmap_mux_div(hw);
  292. + struct clk_hw *parent;
  293. + u32 div;
  294. +
  295. + if (!md->safe_freq || !md->safe_src)
  296. + return;
  297. +
  298. + parent = mux_div_get_safe_parent(hw, &md->safe_freq);
  299. + div = divider_get_val(md->safe_freq, clk_get_rate(parent->clk), NULL,
  300. + md->hid_width, CLK_DIVIDER_ROUND_CLOSEST);
  301. + div = 2 * div + 1;
  302. +
  303. + __mux_div_set_src_div(md, md->safe_src, div);
  304. +}
  305. +
  306. +const struct clk_ops clk_regmap_mux_div_ops = {
  307. + .enable = mux_div_enable,
  308. + .disable = mux_div_disable,
  309. + .get_parent = mux_div_get_parent,
  310. + .set_parent = mux_div_set_parent,
  311. + .set_rate = mux_div_set_rate,
  312. + .set_rate_and_parent = mux_div_set_rate_and_parent,
  313. + .determine_rate = mux_div_determine_rate,
  314. + .recalc_rate = mux_div_recalc_rate,
  315. + .get_safe_parent = mux_div_get_safe_parent,
  316. +};
  317. +EXPORT_SYMBOL_GPL(clk_regmap_mux_div_ops);
  318. --- /dev/null
  319. +++ b/drivers/clk/qcom/clk-regmap-mux-div.h
  320. @@ -0,0 +1,63 @@
  321. +/*
  322. + * Copyright (c) 2015, Linaro Limited
  323. + * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  324. + *
  325. + * This software is licensed under the terms of the GNU General Public
  326. + * License version 2, as published by the Free Software Foundation, and
  327. + * may be copied, distributed, and modified under those terms.
  328. + *
  329. + * This program is distributed in the hope that it will be useful,
  330. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  331. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  332. + * GNU General Public License for more details.
  333. + */
  334. +
  335. +#ifndef __QCOM_CLK_REGMAP_MUX_DIV_H__
  336. +#define __QCOM_CLK_REGMAP_MUX_DIV_H__
  337. +
  338. +#include <linux/clk-provider.h>
  339. +#include "clk-regmap.h"
  340. +#include "clk-rcg.h"
  341. +
  342. +/**
  343. + * struct mux_div_clk - combined mux/divider clock
  344. + * @reg_offset: offset of the mux/divider register
  345. + * @hid_width: number of bits in half integer divider
  346. + * @hid_shift: lowest bit of hid value field
  347. + * @src_width: number of bits in source select
  348. + * @src_shift: lowest bit of source select field
  349. + * @div: the divider raw configuration value
  350. + * @src_sel: the mux index which will be used if the clock is enabled
  351. + * @safe_src: the safe source mux index for this clock
  352. + * @safe_freq: When switching rates from A to B, the mux div clock will
  353. + * instead switch from A -> safe_freq -> B. This allows the
  354. + * mux_div clock to change rates while enabled, even if this
  355. + * behavior is not supported by the parent clocks.
  356. + * If changing the rate of parent A also causes the rate of
  357. + * parent B to change, then safe_freq must be defined.
  358. + * safe_freq is expected to have a source clock which is always
  359. + * on and runs at only one rate.
  360. + * @parent_map: pointer to parent_map struct
  361. + * @clkr: handle between common and hardware-specific interfaces
  362. + */
  363. +
  364. +struct clk_regmap_mux_div {
  365. + u32 reg_offset;
  366. + u32 hid_width;
  367. + u32 hid_shift;
  368. + u32 src_width;
  369. + u32 src_shift;
  370. + u32 div;
  371. + u32 src_sel;
  372. + u32 safe_src;
  373. + unsigned long safe_freq;
  374. + const struct parent_map *parent_map;
  375. + struct clk_regmap clkr;
  376. +};
  377. +
  378. +#define to_clk_regmap_mux_div(_hw) \
  379. + container_of(to_clk_regmap(_hw), struct clk_regmap_mux_div, clkr)
  380. +
  381. +extern const struct clk_ops clk_regmap_mux_div_ops;
  382. +
  383. +#endif