WIP FPC-III support
[linux/fpc-iii.git] / drivers / clk / tegra / clk-utils.c
blob1a5daae4e501489dadbd6c9bbe671693c3407d86
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
4 */
6 #include <asm/div64.h>
8 #include "clk.h"
10 #define div_mask(w) ((1 << (w)) - 1)
12 int div_frac_get(unsigned long rate, unsigned parent_rate, u8 width,
13 u8 frac_width, u8 flags)
15 u64 divider_ux1 = parent_rate;
16 int mul;
18 if (!rate)
19 return 0;
21 mul = 1 << frac_width;
23 if (!(flags & TEGRA_DIVIDER_INT))
24 divider_ux1 *= mul;
26 if (flags & TEGRA_DIVIDER_ROUND_UP)
27 divider_ux1 += rate - 1;
29 do_div(divider_ux1, rate);
31 if (flags & TEGRA_DIVIDER_INT)
32 divider_ux1 *= mul;
34 if (divider_ux1 < mul)
35 return 0;
37 divider_ux1 -= mul;
39 if (divider_ux1 > div_mask(width))
40 return div_mask(width);
42 return divider_ux1;