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 | */ |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 8 | |
| 9 | #define LOG_CATEGORY UCLASS_CLK |
| 10 | |
Patrick Delaunay | 283dadf | 2021-11-19 15:12:06 +0100 | [diff] [blame] | 11 | #include <clk.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 12 | #include <clk-uclass.h> |
Patrick Delaunay | 283dadf | 2021-11-19 15:12:06 +0100 | [diff] [blame] | 13 | #include <div64.h> |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 14 | #include <log.h> |
Patrick Delaunay | 283dadf | 2021-11-19 15:12:06 +0100 | [diff] [blame] | 15 | #include <malloc.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 16 | #include <dm/device.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <dm/devres.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 18 | #include <linux/clk-provider.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 20 | |
Patrick Delaunay | 283dadf | 2021-11-19 15:12:06 +0100 | [diff] [blame] | 21 | #include "clk.h" |
| 22 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 23 | #define UBOOT_DM_CLK_IMX_FIXED_FACTOR "ccf_clk_fixed_factor" |
| 24 | |
| 25 | static ulong clk_factor_recalc_rate(struct clk *clk) |
| 26 | { |
Sean Anderson | cfc2f02 | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 27 | struct clk_fixed_factor *fix = to_clk_fixed_factor(clk); |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 28 | unsigned long parent_rate = clk_get_parent_rate(clk); |
| 29 | unsigned long long int rate; |
| 30 | |
| 31 | rate = (unsigned long long int)parent_rate * fix->mult; |
| 32 | do_div(rate, fix->div); |
| 33 | return (ulong)rate; |
| 34 | } |
| 35 | |
| 36 | const struct clk_ops ccf_clk_fixed_factor_ops = { |
| 37 | .get_rate = clk_factor_recalc_rate, |
| 38 | }; |
| 39 | |
| 40 | struct clk *clk_hw_register_fixed_factor(struct device *dev, |
| 41 | const char *name, const char *parent_name, unsigned long flags, |
| 42 | unsigned int mult, unsigned int div) |
| 43 | { |
| 44 | struct clk_fixed_factor *fix; |
| 45 | struct clk *clk; |
| 46 | int ret; |
| 47 | |
| 48 | fix = kzalloc(sizeof(*fix), GFP_KERNEL); |
| 49 | if (!fix) |
| 50 | return ERR_PTR(-ENOMEM); |
| 51 | |
| 52 | /* struct clk_fixed_factor assignments */ |
| 53 | fix->mult = mult; |
| 54 | fix->div = div; |
| 55 | clk = &fix->clk; |
Dario Binacchi | 1a62dc1 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 56 | clk->flags = flags; |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 57 | |
| 58 | ret = clk_register(clk, UBOOT_DM_CLK_IMX_FIXED_FACTOR, name, |
| 59 | parent_name); |
| 60 | if (ret) { |
| 61 | kfree(fix); |
| 62 | return ERR_PTR(ret); |
| 63 | } |
| 64 | |
| 65 | return clk; |
| 66 | } |
| 67 | |
| 68 | struct clk *clk_register_fixed_factor(struct device *dev, const char *name, |
| 69 | const char *parent_name, unsigned long flags, |
| 70 | unsigned int mult, unsigned int div) |
| 71 | { |
| 72 | struct clk *clk; |
| 73 | |
| 74 | clk = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult, |
| 75 | div); |
| 76 | if (IS_ERR(clk)) |
| 77 | return ERR_CAST(clk); |
| 78 | return clk; |
| 79 | } |
| 80 | |
| 81 | U_BOOT_DRIVER(imx_clk_fixed_factor) = { |
| 82 | .name = UBOOT_DM_CLK_IMX_FIXED_FACTOR, |
| 83 | .id = UCLASS_CLK, |
| 84 | .ops = &ccf_clk_fixed_factor_ops, |
| 85 | .flags = DM_FLAG_PRE_RELOC, |
| 86 | }; |