blob: e51f94a937108d84e62b02d74481c94af28e9b69 [file] [log] [blame]
Anup Patel8d28c3c2019-02-25 08:14:55 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2019 Western Digital Corporation or its affiliates.
4 *
5 * Author: Anup Patel <anup.patel@wdc.com>
6 */
7
8#include <common.h>
9#include <clk-uclass.h>
10#include <div64.h>
11#include <dm.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070012#include <linux/err.h>
Anup Patel8d28c3c2019-02-25 08:14:55 +000013
14struct clk_fixed_factor {
15 struct clk parent;
16 unsigned int div;
17 unsigned int mult;
18};
19
20#define to_clk_fixed_factor(dev) \
Simon Glassfa20e932020-12-03 16:55:20 -070021 ((struct clk_fixed_factor *)dev_get_plat(dev))
Anup Patel8d28c3c2019-02-25 08:14:55 +000022
23static ulong clk_fixed_factor_get_rate(struct clk *clk)
24{
25 uint64_t rate;
26 struct clk_fixed_factor *ff = to_clk_fixed_factor(clk->dev);
27
Anup Patel8d28c3c2019-02-25 08:14:55 +000028 rate = clk_get_rate(&ff->parent);
29 if (IS_ERR_VALUE(rate))
30 return rate;
31
32 do_div(rate, ff->div);
33
34 return rate * ff->mult;
35}
36
37const struct clk_ops clk_fixed_factor_ops = {
38 .get_rate = clk_fixed_factor_get_rate,
39};
40
Simon Glassaad29ae2020-12-03 16:55:21 -070041static int clk_fixed_factor_of_to_plat(struct udevice *dev)
Anup Patel8d28c3c2019-02-25 08:14:55 +000042{
43#if !CONFIG_IS_ENABLED(OF_PLATDATA)
44 int err;
45 struct clk_fixed_factor *ff = to_clk_fixed_factor(dev);
46
47 err = clk_get_by_index(dev, 0, &ff->parent);
48 if (err)
49 return err;
50
51 ff->div = dev_read_u32_default(dev, "clock-div", 1);
52 ff->mult = dev_read_u32_default(dev, "clock-mult", 1);
53#endif
54
55 return 0;
56}
57
58static const struct udevice_id clk_fixed_factor_match[] = {
59 {
60 .compatible = "fixed-factor-clock",
61 },
62 { /* sentinel */ }
63};
64
65U_BOOT_DRIVER(clk_fixed_factor) = {
66 .name = "fixed_factor_clock",
67 .id = UCLASS_CLK,
68 .of_match = clk_fixed_factor_match,
Simon Glassaad29ae2020-12-03 16:55:21 -070069 .of_to_plat = clk_fixed_factor_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -070070 .plat_auto = sizeof(struct clk_fixed_factor),
Anup Patel8d28c3c2019-02-25 08:14:55 +000071 .ops = &clk_fixed_factor_ops,
72};