Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Standard functionality for the common clock API. |
| 9 | */ |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/clk-provider.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/err.h> |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 14 | #include <linux/of.h> |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * DOC: basic fixed multiplier and divider clock that cannot gate |
| 18 | * |
| 19 | * Traits of this clock: |
| 20 | * prepare - clk_prepare only ensures that parents are prepared |
| 21 | * enable - clk_enable only ensures that parents are enabled |
| 22 | * rate - rate is fixed. clk->rate = parent->rate / div * mult |
| 23 | * parent - fixed parent. No clk_set_parent support |
| 24 | */ |
| 25 | |
| 26 | #define to_clk_fixed_factor(_hw) container_of(_hw, struct clk_fixed_factor, hw) |
| 27 | |
| 28 | static unsigned long clk_factor_recalc_rate(struct clk_hw *hw, |
| 29 | unsigned long parent_rate) |
| 30 | { |
| 31 | struct clk_fixed_factor *fix = to_clk_fixed_factor(hw); |
Haojian Zhuang | bab5330 | 2012-12-03 16:14:37 +0800 | [diff] [blame] | 32 | unsigned long long int rate; |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 33 | |
Haojian Zhuang | bab5330 | 2012-12-03 16:14:37 +0800 | [diff] [blame] | 34 | rate = (unsigned long long int)parent_rate * fix->mult; |
| 35 | do_div(rate, fix->div); |
| 36 | return (unsigned long)rate; |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static long clk_factor_round_rate(struct clk_hw *hw, unsigned long rate, |
| 40 | unsigned long *prate) |
| 41 | { |
| 42 | struct clk_fixed_factor *fix = to_clk_fixed_factor(hw); |
| 43 | |
| 44 | if (__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT) { |
| 45 | unsigned long best_parent; |
| 46 | |
| 47 | best_parent = (rate / fix->mult) * fix->div; |
| 48 | *prate = __clk_round_rate(__clk_get_parent(hw->clk), |
| 49 | best_parent); |
| 50 | } |
| 51 | |
| 52 | return (*prate / fix->div) * fix->mult; |
| 53 | } |
| 54 | |
| 55 | static int clk_factor_set_rate(struct clk_hw *hw, unsigned long rate, |
| 56 | unsigned long parent_rate) |
| 57 | { |
Daniel Thompson | 3037e9e | 2015-06-10 21:04:54 +0100 | [diff] [blame^] | 58 | /* |
| 59 | * We must report success but we can do so unconditionally because |
| 60 | * clk_factor_round_rate returns values that ensure this call is a |
| 61 | * nop. |
| 62 | */ |
| 63 | |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
Daniel Thompson | 3037e9e | 2015-06-10 21:04:54 +0100 | [diff] [blame^] | 67 | const struct clk_ops clk_fixed_factor_ops = { |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 68 | .round_rate = clk_factor_round_rate, |
| 69 | .set_rate = clk_factor_set_rate, |
| 70 | .recalc_rate = clk_factor_recalc_rate, |
| 71 | }; |
| 72 | EXPORT_SYMBOL_GPL(clk_fixed_factor_ops); |
| 73 | |
| 74 | struct clk *clk_register_fixed_factor(struct device *dev, const char *name, |
| 75 | const char *parent_name, unsigned long flags, |
| 76 | unsigned int mult, unsigned int div) |
| 77 | { |
| 78 | struct clk_fixed_factor *fix; |
| 79 | struct clk_init_data init; |
| 80 | struct clk *clk; |
| 81 | |
| 82 | fix = kmalloc(sizeof(*fix), GFP_KERNEL); |
Stephen Boyd | d122db7 | 2015-05-14 16:47:10 -0700 | [diff] [blame] | 83 | if (!fix) |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 84 | return ERR_PTR(-ENOMEM); |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 85 | |
| 86 | /* struct clk_fixed_factor assignments */ |
| 87 | fix->mult = mult; |
| 88 | fix->div = div; |
| 89 | fix->hw.init = &init; |
| 90 | |
| 91 | init.name = name; |
| 92 | init.ops = &clk_fixed_factor_ops; |
Rajendra Nayak | f7d8caa | 2012-06-01 14:02:47 +0530 | [diff] [blame] | 93 | init.flags = flags | CLK_IS_BASIC; |
Sascha Hauer | f0948f5 | 2012-05-03 15:36:14 +0530 | [diff] [blame] | 94 | init.parent_names = &parent_name; |
| 95 | init.num_parents = 1; |
| 96 | |
| 97 | clk = clk_register(dev, &fix->hw); |
| 98 | |
| 99 | if (IS_ERR(clk)) |
| 100 | kfree(fix); |
| 101 | |
| 102 | return clk; |
| 103 | } |
Mike Turquette | 5cfe10b | 2013-08-15 19:06:29 -0700 | [diff] [blame] | 104 | EXPORT_SYMBOL_GPL(clk_register_fixed_factor); |
| 105 | |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 106 | #ifdef CONFIG_OF |
| 107 | /** |
| 108 | * of_fixed_factor_clk_setup() - Setup function for simple fixed factor clock |
| 109 | */ |
| 110 | void __init of_fixed_factor_clk_setup(struct device_node *node) |
| 111 | { |
| 112 | struct clk *clk; |
| 113 | const char *clk_name = node->name; |
| 114 | const char *parent_name; |
| 115 | u32 div, mult; |
| 116 | |
| 117 | if (of_property_read_u32(node, "clock-div", &div)) { |
| 118 | pr_err("%s Fixed factor clock <%s> must have a clock-div property\n", |
| 119 | __func__, node->name); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if (of_property_read_u32(node, "clock-mult", &mult)) { |
Ezequiel Garcia | fe2fd5c | 2013-09-25 16:08:46 -0300 | [diff] [blame] | 124 | pr_err("%s Fixed factor clock <%s> must have a clock-mult property\n", |
Gregory CLEMENT | 79b1664 | 2013-04-12 13:57:44 +0200 | [diff] [blame] | 125 | __func__, node->name); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | of_property_read_string(node, "clock-output-names", &clk_name); |
| 130 | parent_name = of_clk_get_parent_name(node, 0); |
| 131 | |
| 132 | clk = clk_register_fixed_factor(NULL, clk_name, parent_name, 0, |
| 133 | mult, div); |
| 134 | if (!IS_ERR(clk)) |
| 135 | of_clk_add_provider(node, of_clk_src_simple_get, clk); |
| 136 | } |
| 137 | EXPORT_SYMBOL_GPL(of_fixed_factor_clk_setup); |
| 138 | CLK_OF_DECLARE(fixed_factor_clk, "fixed-factor-clock", |
| 139 | of_fixed_factor_clk_setup); |
| 140 | #endif |