Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2019 DENX Software Engineering |
| 4 | * Lukasz Majewski, DENX Software Engineering, lukma@denx.de |
| 5 | * |
| 6 | * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> |
| 7 | */ |
| 8 | #include <common.h> |
| 9 | #include <malloc.h> |
| 10 | #include <clk-uclass.h> |
| 11 | #include <dm/device.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 12 | #include <dm/devres.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 13 | #include <linux/clk-provider.h> |
| 14 | #include <div64.h> |
| 15 | #include <clk.h> |
| 16 | #include "clk.h" |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <linux/err.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 18 | |
| 19 | #define UBOOT_DM_CLK_IMX_FIXED_FACTOR "ccf_clk_fixed_factor" |
| 20 | |
| 21 | static ulong clk_factor_recalc_rate(struct clk *clk) |
| 22 | { |
| 23 | struct clk_fixed_factor *fix = |
| 24 | to_clk_fixed_factor(dev_get_clk_ptr(clk->dev)); |
| 25 | unsigned long parent_rate = clk_get_parent_rate(clk); |
| 26 | unsigned long long int rate; |
| 27 | |
| 28 | rate = (unsigned long long int)parent_rate * fix->mult; |
| 29 | do_div(rate, fix->div); |
| 30 | return (ulong)rate; |
| 31 | } |
| 32 | |
| 33 | const struct clk_ops ccf_clk_fixed_factor_ops = { |
| 34 | .get_rate = clk_factor_recalc_rate, |
| 35 | }; |
| 36 | |
| 37 | struct clk *clk_hw_register_fixed_factor(struct device *dev, |
| 38 | const char *name, const char *parent_name, unsigned long flags, |
| 39 | unsigned int mult, unsigned int div) |
| 40 | { |
| 41 | struct clk_fixed_factor *fix; |
| 42 | struct clk *clk; |
| 43 | int ret; |
| 44 | |
| 45 | fix = kzalloc(sizeof(*fix), GFP_KERNEL); |
| 46 | if (!fix) |
| 47 | return ERR_PTR(-ENOMEM); |
| 48 | |
| 49 | /* struct clk_fixed_factor assignments */ |
| 50 | fix->mult = mult; |
| 51 | fix->div = div; |
| 52 | clk = &fix->clk; |
| 53 | |
| 54 | ret = clk_register(clk, UBOOT_DM_CLK_IMX_FIXED_FACTOR, name, |
| 55 | parent_name); |
| 56 | if (ret) { |
| 57 | kfree(fix); |
| 58 | return ERR_PTR(ret); |
| 59 | } |
| 60 | |
| 61 | return clk; |
| 62 | } |
| 63 | |
| 64 | struct clk *clk_register_fixed_factor(struct device *dev, const char *name, |
| 65 | const char *parent_name, unsigned long flags, |
| 66 | unsigned int mult, unsigned int div) |
| 67 | { |
| 68 | struct clk *clk; |
| 69 | |
| 70 | clk = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult, |
| 71 | div); |
| 72 | if (IS_ERR(clk)) |
| 73 | return ERR_CAST(clk); |
| 74 | return clk; |
| 75 | } |
| 76 | |
| 77 | U_BOOT_DRIVER(imx_clk_fixed_factor) = { |
| 78 | .name = UBOOT_DM_CLK_IMX_FIXED_FACTOR, |
| 79 | .id = UCLASS_CLK, |
| 80 | .ops = &ccf_clk_fixed_factor_ops, |
| 81 | .flags = DM_FLAG_PRE_RELOC, |
| 82 | }; |