2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/bitops.h>
16 #include <linux/err.h>
17 #include <linux/bug.h>
18 #include <linux/export.h>
19 #include <linux/clk-provider.h>
20 #include <linux/delay.h>
21 #include <linux/regmap.h>
22 #include <linux/math64.h>
24 #include <asm/div64.h>
30 #define CMD_UPDATE BIT(0)
31 #define CMD_ROOT_EN BIT(1)
32 #define CMD_DIRTY_CFG BIT(4)
33 #define CMD_DIRTY_N BIT(5)
34 #define CMD_DIRTY_M BIT(6)
35 #define CMD_DIRTY_D BIT(7)
36 #define CMD_ROOT_OFF BIT(31)
39 #define CFG_SRC_DIV_SHIFT 0
40 #define CFG_SRC_SEL_SHIFT 8
41 #define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT)
42 #define CFG_MODE_SHIFT 12
43 #define CFG_MODE_MASK (0x3 << CFG_MODE_SHIFT)
44 #define CFG_MODE_DUAL_EDGE (0x2 << CFG_MODE_SHIFT)
50 static int clk_rcg2_is_enabled(struct clk_hw
*hw
)
52 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
56 ret
= regmap_read(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ CMD_REG
, &cmd
);
60 return (cmd
& CMD_ROOT_OFF
) == 0;
63 static u8
clk_rcg2_get_parent(struct clk_hw
*hw
)
65 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
66 int num_parents
= __clk_get_num_parents(hw
->clk
);
70 ret
= regmap_read(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ CFG_REG
, &cfg
);
74 cfg
&= CFG_SRC_SEL_MASK
;
75 cfg
>>= CFG_SRC_SEL_SHIFT
;
77 for (i
= 0; i
< num_parents
; i
++)
78 if (cfg
== rcg
->parent_map
[i
].cfg
)
82 pr_debug("%s: Clock %s has invalid parent, using default.\n",
83 __func__
, __clk_get_name(hw
->clk
));
87 static int update_config(struct clk_rcg2
*rcg
)
91 struct clk_hw
*hw
= &rcg
->clkr
.hw
;
92 const char *name
= __clk_get_name(hw
->clk
);
94 ret
= regmap_update_bits(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ CMD_REG
,
95 CMD_UPDATE
, CMD_UPDATE
);
99 /* Wait for update to take effect */
100 for (count
= 500; count
> 0; count
--) {
101 ret
= regmap_read(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ CMD_REG
, &cmd
);
104 if (!(cmd
& CMD_UPDATE
))
109 WARN(1, "%s: rcg didn't update its configuration.", name
);
113 static int clk_rcg2_set_parent(struct clk_hw
*hw
, u8 index
)
115 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
117 u32 cfg
= rcg
->parent_map
[index
].cfg
<< CFG_SRC_SEL_SHIFT
;
119 ret
= regmap_update_bits(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ CFG_REG
,
120 CFG_SRC_SEL_MASK
, cfg
);
124 return update_config(rcg
);
128 * Calculate m/n:d rate
131 * rate = ----------- x ---
135 calc_rate(unsigned long rate
, u32 m
, u32 n
, u32 mode
, u32 hid_div
)
153 clk_rcg2_recalc_rate(struct clk_hw
*hw
, unsigned long parent_rate
)
155 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
156 u32 cfg
, hid_div
, m
= 0, n
= 0, mode
= 0, mask
;
158 regmap_read(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ CFG_REG
, &cfg
);
160 if (rcg
->mnd_width
) {
161 mask
= BIT(rcg
->mnd_width
) - 1;
162 regmap_read(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ M_REG
, &m
);
164 regmap_read(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ N_REG
, &n
);
168 mode
= cfg
& CFG_MODE_MASK
;
169 mode
>>= CFG_MODE_SHIFT
;
172 mask
= BIT(rcg
->hid_width
) - 1;
173 hid_div
= cfg
>> CFG_SRC_DIV_SHIFT
;
176 return calc_rate(parent_rate
, m
, n
, mode
, hid_div
);
179 static long _freq_tbl_determine_rate(struct clk_hw
*hw
,
180 const struct freq_tbl
*f
, unsigned long rate
,
181 unsigned long *p_rate
, struct clk_hw
**p_hw
)
183 unsigned long clk_flags
;
185 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
188 f
= qcom_find_freq(f
, rate
);
192 index
= qcom_find_src_index(hw
, rcg
->parent_map
, f
->src
);
196 clk_flags
= __clk_get_flags(hw
->clk
);
197 p
= clk_get_parent_by_index(hw
->clk
, index
);
198 if (clk_flags
& CLK_SET_RATE_PARENT
) {
201 rate
*= f
->pre_div
+ 1;
211 rate
= __clk_get_rate(p
);
213 *p_hw
= __clk_get_hw(p
);
219 static long clk_rcg2_determine_rate(struct clk_hw
*hw
, unsigned long rate
,
220 unsigned long min_rate
, unsigned long max_rate
,
221 unsigned long *p_rate
, struct clk_hw
**p
)
223 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
225 return _freq_tbl_determine_rate(hw
, rcg
->freq_tbl
, rate
, p_rate
, p
);
228 static int clk_rcg2_configure(struct clk_rcg2
*rcg
, const struct freq_tbl
*f
)
231 struct clk_hw
*hw
= &rcg
->clkr
.hw
;
232 int ret
, index
= qcom_find_src_index(hw
, rcg
->parent_map
, f
->src
);
237 if (rcg
->mnd_width
&& f
->n
) {
238 mask
= BIT(rcg
->mnd_width
) - 1;
239 ret
= regmap_update_bits(rcg
->clkr
.regmap
,
240 rcg
->cmd_rcgr
+ M_REG
, mask
, f
->m
);
244 ret
= regmap_update_bits(rcg
->clkr
.regmap
,
245 rcg
->cmd_rcgr
+ N_REG
, mask
, ~(f
->n
- f
->m
));
249 ret
= regmap_update_bits(rcg
->clkr
.regmap
,
250 rcg
->cmd_rcgr
+ D_REG
, mask
, ~f
->n
);
255 mask
= BIT(rcg
->hid_width
) - 1;
256 mask
|= CFG_SRC_SEL_MASK
| CFG_MODE_MASK
;
257 cfg
= f
->pre_div
<< CFG_SRC_DIV_SHIFT
;
258 cfg
|= rcg
->parent_map
[index
].cfg
<< CFG_SRC_SEL_SHIFT
;
259 if (rcg
->mnd_width
&& f
->n
&& (f
->m
!= f
->n
))
260 cfg
|= CFG_MODE_DUAL_EDGE
;
261 ret
= regmap_update_bits(rcg
->clkr
.regmap
,
262 rcg
->cmd_rcgr
+ CFG_REG
, mask
, cfg
);
266 return update_config(rcg
);
269 static int __clk_rcg2_set_rate(struct clk_hw
*hw
, unsigned long rate
)
271 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
272 const struct freq_tbl
*f
;
274 f
= qcom_find_freq(rcg
->freq_tbl
, rate
);
278 return clk_rcg2_configure(rcg
, f
);
281 static int clk_rcg2_set_rate(struct clk_hw
*hw
, unsigned long rate
,
282 unsigned long parent_rate
)
284 return __clk_rcg2_set_rate(hw
, rate
);
287 static int clk_rcg2_set_rate_and_parent(struct clk_hw
*hw
,
288 unsigned long rate
, unsigned long parent_rate
, u8 index
)
290 return __clk_rcg2_set_rate(hw
, rate
);
293 const struct clk_ops clk_rcg2_ops
= {
294 .is_enabled
= clk_rcg2_is_enabled
,
295 .get_parent
= clk_rcg2_get_parent
,
296 .set_parent
= clk_rcg2_set_parent
,
297 .recalc_rate
= clk_rcg2_recalc_rate
,
298 .determine_rate
= clk_rcg2_determine_rate
,
299 .set_rate
= clk_rcg2_set_rate
,
300 .set_rate_and_parent
= clk_rcg2_set_rate_and_parent
,
302 EXPORT_SYMBOL_GPL(clk_rcg2_ops
);
309 static const struct frac_entry frac_table_675m
[] = { /* link rate of 270M */
310 { 52, 295 }, /* 119 M */
311 { 11, 57 }, /* 130.25 M */
312 { 63, 307 }, /* 138.50 M */
313 { 11, 50 }, /* 148.50 M */
314 { 47, 206 }, /* 154 M */
315 { 31, 100 }, /* 205.25 M */
316 { 107, 269 }, /* 268.50 M */
320 static struct frac_entry frac_table_810m
[] = { /* Link rate of 162M */
321 { 31, 211 }, /* 119 M */
322 { 32, 199 }, /* 130.25 M */
323 { 63, 307 }, /* 138.50 M */
324 { 11, 60 }, /* 148.50 M */
325 { 50, 263 }, /* 154 M */
326 { 31, 120 }, /* 205.25 M */
327 { 119, 359 }, /* 268.50 M */
331 static int clk_edp_pixel_set_rate(struct clk_hw
*hw
, unsigned long rate
,
332 unsigned long parent_rate
)
334 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
335 struct freq_tbl f
= *rcg
->freq_tbl
;
336 const struct frac_entry
*frac
;
338 s64 src_rate
= parent_rate
;
340 u32 mask
= BIT(rcg
->hid_width
) - 1;
343 if (src_rate
== 810000000)
344 frac
= frac_table_810m
;
346 frac
= frac_table_675m
;
348 for (; frac
->num
; frac
++) {
350 request
*= frac
->den
;
351 request
= div_s64(request
, frac
->num
);
352 if ((src_rate
< (request
- delta
)) ||
353 (src_rate
> (request
+ delta
)))
356 regmap_read(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ CFG_REG
,
359 f
.pre_div
>>= CFG_SRC_DIV_SHIFT
;
364 return clk_rcg2_configure(rcg
, &f
);
370 static int clk_edp_pixel_set_rate_and_parent(struct clk_hw
*hw
,
371 unsigned long rate
, unsigned long parent_rate
, u8 index
)
373 /* Parent index is set statically in frequency table */
374 return clk_edp_pixel_set_rate(hw
, rate
, parent_rate
);
377 static long clk_edp_pixel_determine_rate(struct clk_hw
*hw
, unsigned long rate
,
378 unsigned long min_rate
,
379 unsigned long max_rate
,
380 unsigned long *p_rate
, struct clk_hw
**p
)
382 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
383 const struct freq_tbl
*f
= rcg
->freq_tbl
;
384 const struct frac_entry
*frac
;
386 s64 src_rate
= *p_rate
;
388 u32 mask
= BIT(rcg
->hid_width
) - 1;
390 int index
= qcom_find_src_index(hw
, rcg
->parent_map
, f
->src
);
392 /* Force the correct parent */
393 *p
= __clk_get_hw(clk_get_parent_by_index(hw
->clk
, index
));
395 if (src_rate
== 810000000)
396 frac
= frac_table_810m
;
398 frac
= frac_table_675m
;
400 for (; frac
->num
; frac
++) {
402 request
*= frac
->den
;
403 request
= div_s64(request
, frac
->num
);
404 if ((src_rate
< (request
- delta
)) ||
405 (src_rate
> (request
+ delta
)))
408 regmap_read(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ CFG_REG
,
410 hid_div
>>= CFG_SRC_DIV_SHIFT
;
413 return calc_rate(src_rate
, frac
->num
, frac
->den
, !!frac
->den
,
420 const struct clk_ops clk_edp_pixel_ops
= {
421 .is_enabled
= clk_rcg2_is_enabled
,
422 .get_parent
= clk_rcg2_get_parent
,
423 .set_parent
= clk_rcg2_set_parent
,
424 .recalc_rate
= clk_rcg2_recalc_rate
,
425 .set_rate
= clk_edp_pixel_set_rate
,
426 .set_rate_and_parent
= clk_edp_pixel_set_rate_and_parent
,
427 .determine_rate
= clk_edp_pixel_determine_rate
,
429 EXPORT_SYMBOL_GPL(clk_edp_pixel_ops
);
431 static long clk_byte_determine_rate(struct clk_hw
*hw
, unsigned long rate
,
432 unsigned long min_rate
, unsigned long max_rate
,
433 unsigned long *p_rate
, struct clk_hw
**p_hw
)
435 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
436 const struct freq_tbl
*f
= rcg
->freq_tbl
;
437 int index
= qcom_find_src_index(hw
, rcg
->parent_map
, f
->src
);
438 unsigned long parent_rate
, div
;
439 u32 mask
= BIT(rcg
->hid_width
) - 1;
445 p
= clk_get_parent_by_index(hw
->clk
, index
);
446 *p_hw
= __clk_get_hw(p
);
447 *p_rate
= parent_rate
= __clk_round_rate(p
, rate
);
449 div
= DIV_ROUND_UP((2 * parent_rate
), rate
) - 1;
450 div
= min_t(u32
, div
, mask
);
452 return calc_rate(parent_rate
, 0, 0, 0, div
);
455 static int clk_byte_set_rate(struct clk_hw
*hw
, unsigned long rate
,
456 unsigned long parent_rate
)
458 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
459 struct freq_tbl f
= *rcg
->freq_tbl
;
461 u32 mask
= BIT(rcg
->hid_width
) - 1;
463 div
= DIV_ROUND_UP((2 * parent_rate
), rate
) - 1;
464 div
= min_t(u32
, div
, mask
);
468 return clk_rcg2_configure(rcg
, &f
);
471 static int clk_byte_set_rate_and_parent(struct clk_hw
*hw
,
472 unsigned long rate
, unsigned long parent_rate
, u8 index
)
474 /* Parent index is set statically in frequency table */
475 return clk_byte_set_rate(hw
, rate
, parent_rate
);
478 const struct clk_ops clk_byte_ops
= {
479 .is_enabled
= clk_rcg2_is_enabled
,
480 .get_parent
= clk_rcg2_get_parent
,
481 .set_parent
= clk_rcg2_set_parent
,
482 .recalc_rate
= clk_rcg2_recalc_rate
,
483 .set_rate
= clk_byte_set_rate
,
484 .set_rate_and_parent
= clk_byte_set_rate_and_parent
,
485 .determine_rate
= clk_byte_determine_rate
,
487 EXPORT_SYMBOL_GPL(clk_byte_ops
);
489 static const struct frac_entry frac_table_pixel
[] = {
497 static long clk_pixel_determine_rate(struct clk_hw
*hw
, unsigned long rate
,
498 unsigned long min_rate
,
499 unsigned long max_rate
,
500 unsigned long *p_rate
, struct clk_hw
**p
)
502 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
503 unsigned long request
, src_rate
;
505 const struct freq_tbl
*f
= rcg
->freq_tbl
;
506 const struct frac_entry
*frac
= frac_table_pixel
;
507 int index
= qcom_find_src_index(hw
, rcg
->parent_map
, f
->src
);
508 struct clk
*parent
= clk_get_parent_by_index(hw
->clk
, index
);
510 *p
= __clk_get_hw(parent
);
512 for (; frac
->num
; frac
++) {
513 request
= (rate
* frac
->den
) / frac
->num
;
515 src_rate
= __clk_round_rate(parent
, request
);
516 if ((src_rate
< (request
- delta
)) ||
517 (src_rate
> (request
+ delta
)))
521 return (src_rate
* frac
->num
) / frac
->den
;
527 static int clk_pixel_set_rate(struct clk_hw
*hw
, unsigned long rate
,
528 unsigned long parent_rate
)
530 struct clk_rcg2
*rcg
= to_clk_rcg2(hw
);
531 struct freq_tbl f
= *rcg
->freq_tbl
;
532 const struct frac_entry
*frac
= frac_table_pixel
;
533 unsigned long request
, src_rate
;
535 u32 mask
= BIT(rcg
->hid_width
) - 1;
537 int index
= qcom_find_src_index(hw
, rcg
->parent_map
, f
.src
);
538 struct clk
*parent
= clk_get_parent_by_index(hw
->clk
, index
);
540 for (; frac
->num
; frac
++) {
541 request
= (rate
* frac
->den
) / frac
->num
;
543 src_rate
= __clk_round_rate(parent
, request
);
544 if ((src_rate
< (request
- delta
)) ||
545 (src_rate
> (request
+ delta
)))
548 regmap_read(rcg
->clkr
.regmap
, rcg
->cmd_rcgr
+ CFG_REG
,
551 f
.pre_div
>>= CFG_SRC_DIV_SHIFT
;
556 return clk_rcg2_configure(rcg
, &f
);
561 static int clk_pixel_set_rate_and_parent(struct clk_hw
*hw
, unsigned long rate
,
562 unsigned long parent_rate
, u8 index
)
564 /* Parent index is set statically in frequency table */
565 return clk_pixel_set_rate(hw
, rate
, parent_rate
);
568 const struct clk_ops clk_pixel_ops
= {
569 .is_enabled
= clk_rcg2_is_enabled
,
570 .get_parent
= clk_rcg2_get_parent
,
571 .set_parent
= clk_rcg2_set_parent
,
572 .recalc_rate
= clk_rcg2_recalc_rate
,
573 .set_rate
= clk_pixel_set_rate
,
574 .set_rate_and_parent
= clk_pixel_set_rate_and_parent
,
575 .determine_rate
= clk_pixel_determine_rate
,
577 EXPORT_SYMBOL_GPL(clk_pixel_ops
);