Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 1 | // 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 Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 12 | #include <linux/err.h> |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 13 | |
| 14 | struct 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 Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 21 | ((struct clk_fixed_factor *)dev_get_plat(dev)) |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 22 | |
| 23 | static 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 Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 28 | 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 | |
| 37 | const struct clk_ops clk_fixed_factor_ops = { |
| 38 | .get_rate = clk_fixed_factor_get_rate, |
| 39 | }; |
| 40 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame^] | 41 | static int clk_fixed_factor_of_to_plat(struct udevice *dev) |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 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 | |
| 58 | static const struct udevice_id clk_fixed_factor_match[] = { |
| 59 | { |
| 60 | .compatible = "fixed-factor-clock", |
| 61 | }, |
| 62 | { /* sentinel */ } |
| 63 | }; |
| 64 | |
| 65 | U_BOOT_DRIVER(clk_fixed_factor) = { |
| 66 | .name = "fixed_factor_clock", |
| 67 | .id = UCLASS_CLK, |
| 68 | .of_match = clk_fixed_factor_match, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame^] | 69 | .of_to_plat = clk_fixed_factor_of_to_plat, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 70 | .plat_auto = sizeof(struct clk_fixed_factor), |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 71 | .ops = &clk_fixed_factor_ops, |
| 72 | }; |