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 | |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 8 | #define LOG_CATEGORY UCLASS_CLK |
| 9 | |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 10 | #include <clk-uclass.h> |
| 11 | #include <div64.h> |
| 12 | #include <dm.h> |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 14 | #include <linux/err.h> |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 15 | |
| 16 | struct 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 Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 23 | ((struct clk_fixed_factor *)dev_get_plat(dev)) |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 24 | |
| 25 | static 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 Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 30 | 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 | |
| 39 | const struct clk_ops clk_fixed_factor_ops = { |
| 40 | .get_rate = clk_fixed_factor_get_rate, |
| 41 | }; |
| 42 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 43 | static int clk_fixed_factor_of_to_plat(struct udevice *dev) |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 44 | { |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 45 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
| 46 | int err; |
| 47 | struct clk_fixed_factor *ff = to_clk_fixed_factor(dev); |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 48 | |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 49 | err = clk_get_by_index(dev, 0, &ff->parent); |
| 50 | if (err) |
| 51 | return err; |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 52 | |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 53 | ff->div = dev_read_u32_default(dev, "clock-div", 1); |
| 54 | ff->mult = dev_read_u32_default(dev, "clock-mult", 1); |
| 55 | } |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static const struct udevice_id clk_fixed_factor_match[] = { |
| 61 | { |
| 62 | .compatible = "fixed-factor-clock", |
| 63 | }, |
| 64 | { /* sentinel */ } |
| 65 | }; |
| 66 | |
| 67 | U_BOOT_DRIVER(clk_fixed_factor) = { |
| 68 | .name = "fixed_factor_clock", |
| 69 | .id = UCLASS_CLK, |
| 70 | .of_match = clk_fixed_factor_match, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 71 | .of_to_plat = clk_fixed_factor_of_to_plat, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 72 | .plat_auto = sizeof(struct clk_fixed_factor), |
Anup Patel | 8d28c3c | 2019-02-25 08:14:55 +0000 | [diff] [blame] | 73 | .ops = &clk_fixed_factor_ops, |
| 74 | }; |