blob: cf9c4ae367fd25c054ab797523b7571db13e8337 [file] [log] [blame]
Anup Patelb630d572019-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 Glass61b29b82020-02-03 07:36:15 -070012#include <linux/err.h>
Anup Patelb630d572019-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) \
21 ((struct clk_fixed_factor *)dev_get_platdata(dev))
22
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 Patelb630d572019-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
41static int clk_fixed_factor_ofdata_to_platdata(struct udevice *dev)
42{
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,
69 .ofdata_to_platdata = clk_fixed_factor_ofdata_to_platdata,
70 .platdata_auto_alloc_size = sizeof(struct clk_fixed_factor),
71 .ops = &clk_fixed_factor_ops,
72};