blob: 0eb24b87fc39435795bad51eb582930d994baee0 [file] [log] [blame]
Lukasz Majewski4de44bb2019-06-24 15:50:45 +02001// 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 Glassd66c5f72020-02-03 07:36:15 -070012#include <dm/devres.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020013#include <linux/clk-provider.h>
14#include <div64.h>
15#include <clk.h>
16#include "clk.h"
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <linux/err.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020018
19#define UBOOT_DM_CLK_IMX_FIXED_FACTOR "ccf_clk_fixed_factor"
20
21static ulong clk_factor_recalc_rate(struct clk *clk)
22{
Sean Andersoncfc2f022020-06-24 06:41:06 -040023 struct clk_fixed_factor *fix = to_clk_fixed_factor(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020024 unsigned long parent_rate = clk_get_parent_rate(clk);
25 unsigned long long int rate;
26
27 rate = (unsigned long long int)parent_rate * fix->mult;
28 do_div(rate, fix->div);
29 return (ulong)rate;
30}
31
32const struct clk_ops ccf_clk_fixed_factor_ops = {
33 .get_rate = clk_factor_recalc_rate,
34};
35
36struct clk *clk_hw_register_fixed_factor(struct device *dev,
37 const char *name, const char *parent_name, unsigned long flags,
38 unsigned int mult, unsigned int div)
39{
40 struct clk_fixed_factor *fix;
41 struct clk *clk;
42 int ret;
43
44 fix = kzalloc(sizeof(*fix), GFP_KERNEL);
45 if (!fix)
46 return ERR_PTR(-ENOMEM);
47
48 /* struct clk_fixed_factor assignments */
49 fix->mult = mult;
50 fix->div = div;
51 clk = &fix->clk;
52
53 ret = clk_register(clk, UBOOT_DM_CLK_IMX_FIXED_FACTOR, name,
54 parent_name);
55 if (ret) {
56 kfree(fix);
57 return ERR_PTR(ret);
58 }
59
60 return clk;
61}
62
63struct clk *clk_register_fixed_factor(struct device *dev, const char *name,
64 const char *parent_name, unsigned long flags,
65 unsigned int mult, unsigned int div)
66{
67 struct clk *clk;
68
69 clk = clk_hw_register_fixed_factor(dev, name, parent_name, flags, mult,
70 div);
71 if (IS_ERR(clk))
72 return ERR_CAST(clk);
73 return clk;
74}
75
76U_BOOT_DRIVER(imx_clk_fixed_factor) = {
77 .name = UBOOT_DM_CLK_IMX_FIXED_FACTOR,
78 .id = UCLASS_CLK,
79 .ops = &ccf_clk_fixed_factor_ops,
80 .flags = DM_FLAG_PRE_RELOC,
81};