]>
Commit | Line | Data |
---|---|---|
b630d57d AP |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Copyright (c) 2019 Western Digital Corporation or its affiliates. | |
4 | * | |
5 | * Author: Anup Patel <[email protected]> | |
6 | */ | |
7 | ||
560e1e00 PD |
8 | #define LOG_CATEGORY UCLASS_CLK |
9 | ||
b630d57d AP |
10 | #include <common.h> |
11 | #include <clk-uclass.h> | |
12 | #include <div64.h> | |
13 | #include <dm.h> | |
560e1e00 | 14 | #include <log.h> |
61b29b82 | 15 | #include <linux/err.h> |
b630d57d AP |
16 | |
17 | struct clk_fixed_factor { | |
18 | struct clk parent; | |
19 | unsigned int div; | |
20 | unsigned int mult; | |
21 | }; | |
22 | ||
23 | #define to_clk_fixed_factor(dev) \ | |
c69cda25 | 24 | ((struct clk_fixed_factor *)dev_get_plat(dev)) |
b630d57d AP |
25 | |
26 | static ulong clk_fixed_factor_get_rate(struct clk *clk) | |
27 | { | |
28 | uint64_t rate; | |
29 | struct clk_fixed_factor *ff = to_clk_fixed_factor(clk->dev); | |
30 | ||
b630d57d AP |
31 | rate = clk_get_rate(&ff->parent); |
32 | if (IS_ERR_VALUE(rate)) | |
33 | return rate; | |
34 | ||
35 | do_div(rate, ff->div); | |
36 | ||
37 | return rate * ff->mult; | |
38 | } | |
39 | ||
40 | const struct clk_ops clk_fixed_factor_ops = { | |
41 | .get_rate = clk_fixed_factor_get_rate, | |
42 | }; | |
43 | ||
d1998a9f | 44 | static int clk_fixed_factor_of_to_plat(struct udevice *dev) |
b630d57d | 45 | { |
dcfc42b1 SG |
46 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
47 | int err; | |
48 | struct clk_fixed_factor *ff = to_clk_fixed_factor(dev); | |
b630d57d | 49 | |
dcfc42b1 SG |
50 | err = clk_get_by_index(dev, 0, &ff->parent); |
51 | if (err) | |
52 | return err; | |
b630d57d | 53 | |
dcfc42b1 SG |
54 | ff->div = dev_read_u32_default(dev, "clock-div", 1); |
55 | ff->mult = dev_read_u32_default(dev, "clock-mult", 1); | |
56 | } | |
b630d57d AP |
57 | |
58 | return 0; | |
59 | } | |
60 | ||
61 | static const struct udevice_id clk_fixed_factor_match[] = { | |
62 | { | |
63 | .compatible = "fixed-factor-clock", | |
64 | }, | |
65 | { /* sentinel */ } | |
66 | }; | |
67 | ||
68 | U_BOOT_DRIVER(clk_fixed_factor) = { | |
69 | .name = "fixed_factor_clock", | |
70 | .id = UCLASS_CLK, | |
71 | .of_match = clk_fixed_factor_match, | |
d1998a9f | 72 | .of_to_plat = clk_fixed_factor_of_to_plat, |
caa4daa2 | 73 | .plat_auto = sizeof(struct clk_fixed_factor), |
b630d57d AP |
74 | .ops = &clk_fixed_factor_ops, |
75 | }; |