blob: 1d740cf49f6277233f8403f85ab78daeae904c28 [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
Patrick Delaunay8767e792021-11-19 15:12:07 +01008#define LOG_CATEGORY UCLASS_CLK
9
Anup Patel8d28c3c2019-02-25 08:14:55 +000010#include <clk-uclass.h>
11#include <div64.h>
12#include <dm.h>
Patrick Delaunay8767e792021-11-19 15:12:07 +010013#include <log.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070014#include <linux/err.h>
Anup Patel8d28c3c2019-02-25 08:14:55 +000015
16struct clk_fixed_factor {
17 struct clk parent;
18 unsigned int div;
19 unsigned int mult;
20};
21
22#define to_clk_fixed_factor(dev) \
Simon Glassfa20e932020-12-03 16:55:20 -070023 ((struct clk_fixed_factor *)dev_get_plat(dev))
Anup Patel8d28c3c2019-02-25 08:14:55 +000024
25static ulong clk_fixed_factor_get_rate(struct clk *clk)
26{
27 uint64_t rate;
28 struct clk_fixed_factor *ff = to_clk_fixed_factor(clk->dev);
29
Anup Patel8d28c3c2019-02-25 08:14:55 +000030 rate = clk_get_rate(&ff->parent);
31 if (IS_ERR_VALUE(rate))
32 return rate;
33
34 do_div(rate, ff->div);
35
36 return rate * ff->mult;
37}
38
39const struct clk_ops clk_fixed_factor_ops = {
40 .get_rate = clk_fixed_factor_get_rate,
41};
42
Simon Glassaad29ae2020-12-03 16:55:21 -070043static int clk_fixed_factor_of_to_plat(struct udevice *dev)
Anup Patel8d28c3c2019-02-25 08:14:55 +000044{
Simon Glass6d70ba02021-08-07 07:24:06 -060045 if (CONFIG_IS_ENABLED(OF_REAL)) {
46 int err;
47 struct clk_fixed_factor *ff = to_clk_fixed_factor(dev);
Anup Patel8d28c3c2019-02-25 08:14:55 +000048
Simon Glass6d70ba02021-08-07 07:24:06 -060049 err = clk_get_by_index(dev, 0, &ff->parent);
50 if (err)
51 return err;
Anup Patel8d28c3c2019-02-25 08:14:55 +000052
Simon Glass6d70ba02021-08-07 07:24:06 -060053 ff->div = dev_read_u32_default(dev, "clock-div", 1);
54 ff->mult = dev_read_u32_default(dev, "clock-mult", 1);
55 }
Anup Patel8d28c3c2019-02-25 08:14:55 +000056
57 return 0;
58}
59
60static const struct udevice_id clk_fixed_factor_match[] = {
61 {
62 .compatible = "fixed-factor-clock",
63 },
64 { /* sentinel */ }
65};
66
67U_BOOT_DRIVER(clk_fixed_factor) = {
68 .name = "fixed_factor_clock",
69 .id = UCLASS_CLK,
70 .of_match = clk_fixed_factor_match,
Simon Glassaad29ae2020-12-03 16:55:21 -070071 .of_to_plat = clk_fixed_factor_of_to_plat,
Simon Glass71fa5b42020-12-03 16:55:18 -070072 .plat_auto = sizeof(struct clk_fixed_factor),
Anup Patel8d28c3c2019-02-25 08:14:55 +000073 .ops = &clk_fixed_factor_ops,
74};