Stephen Boyd | e1bd55e | 2018-12-11 09:57:48 -0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de> |
| 4 | * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org> |
| 5 | * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> |
| 6 | * |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 7 | * Simple multiplexer clock implementation |
| 8 | */ |
| 9 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 10 | #include <linux/clk-provider.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/err.h> |
| 15 | |
| 16 | /* |
| 17 | * DOC: basic adjustable multiplexer 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 only affected by parent switching. No clk_set_rate support |
| 23 | * parent - parent is adjustable through clk_set_parent |
| 24 | */ |
| 25 | |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 26 | int clk_mux_val_to_index(struct clk_hw *hw, u32 *table, unsigned int flags, |
| 27 | unsigned int val) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 28 | { |
Stephen Boyd | 497295a | 2015-06-25 16:53:23 -0700 | [diff] [blame] | 29 | int num_parents = clk_hw_get_num_parents(hw); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 30 | |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 31 | if (table) { |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 32 | int i; |
| 33 | |
| 34 | for (i = 0; i < num_parents; i++) |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 35 | if (table[i] == val) |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 36 | return i; |
| 37 | return -EINVAL; |
| 38 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 39 | |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 40 | if (val && (flags & CLK_MUX_INDEX_BIT)) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 41 | val = ffs(val) - 1; |
| 42 | |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 43 | if (val && (flags & CLK_MUX_INDEX_ONE)) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 44 | val--; |
| 45 | |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 46 | if (val >= num_parents) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 47 | return -EINVAL; |
| 48 | |
| 49 | return val; |
| 50 | } |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 51 | EXPORT_SYMBOL_GPL(clk_mux_val_to_index); |
| 52 | |
| 53 | unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index) |
| 54 | { |
| 55 | unsigned int val = index; |
| 56 | |
| 57 | if (table) { |
| 58 | val = table[index]; |
| 59 | } else { |
| 60 | if (flags & CLK_MUX_INDEX_BIT) |
| 61 | val = 1 << index; |
| 62 | |
| 63 | if (flags & CLK_MUX_INDEX_ONE) |
| 64 | val++; |
| 65 | } |
| 66 | |
| 67 | return val; |
| 68 | } |
| 69 | EXPORT_SYMBOL_GPL(clk_mux_index_to_val); |
| 70 | |
| 71 | static u8 clk_mux_get_parent(struct clk_hw *hw) |
| 72 | { |
| 73 | struct clk_mux *mux = to_clk_mux(hw); |
| 74 | u32 val; |
| 75 | |
| 76 | val = clk_readl(mux->reg) >> mux->shift; |
| 77 | val &= mux->mask; |
| 78 | |
| 79 | return clk_mux_val_to_index(hw, mux->table, mux->flags, val); |
| 80 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 81 | |
| 82 | static int clk_mux_set_parent(struct clk_hw *hw, u8 index) |
| 83 | { |
| 84 | struct clk_mux *mux = to_clk_mux(hw); |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 85 | u32 val = clk_mux_index_to_val(mux->table, mux->flags, index); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 86 | unsigned long flags = 0; |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 87 | u32 reg; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 88 | |
| 89 | if (mux->lock) |
| 90 | spin_lock_irqsave(mux->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 91 | else |
| 92 | __acquire(mux->lock); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 93 | |
Haojian Zhuang | ba492e9 | 2013-06-08 22:47:17 +0800 | [diff] [blame] | 94 | if (mux->flags & CLK_MUX_HIWORD_MASK) { |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 95 | reg = mux->mask << (mux->shift + 16); |
Haojian Zhuang | ba492e9 | 2013-06-08 22:47:17 +0800 | [diff] [blame] | 96 | } else { |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 97 | reg = clk_readl(mux->reg); |
| 98 | reg &= ~(mux->mask << mux->shift); |
Haojian Zhuang | ba492e9 | 2013-06-08 22:47:17 +0800 | [diff] [blame] | 99 | } |
Jerome Brunet | 77deb66 | 2018-02-14 14:43:34 +0100 | [diff] [blame] | 100 | val = val << mux->shift; |
| 101 | reg |= val; |
| 102 | clk_writel(reg, mux->reg); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 103 | |
| 104 | if (mux->lock) |
| 105 | spin_unlock_irqrestore(mux->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 106 | else |
| 107 | __release(mux->lock); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 111 | |
Jerome Brunet | 4ad69b80 | 2018-04-09 15:59:20 +0200 | [diff] [blame] | 112 | static int clk_mux_determine_rate(struct clk_hw *hw, |
| 113 | struct clk_rate_request *req) |
| 114 | { |
| 115 | struct clk_mux *mux = to_clk_mux(hw); |
| 116 | |
| 117 | return clk_mux_determine_rate_flags(hw, req, mux->flags); |
| 118 | } |
| 119 | |
Shawn Guo | 822c250 | 2012-03-27 15:23:22 +0800 | [diff] [blame] | 120 | const struct clk_ops clk_mux_ops = { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 121 | .get_parent = clk_mux_get_parent, |
| 122 | .set_parent = clk_mux_set_parent, |
Jerome Brunet | 4ad69b80 | 2018-04-09 15:59:20 +0200 | [diff] [blame] | 123 | .determine_rate = clk_mux_determine_rate, |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 124 | }; |
| 125 | EXPORT_SYMBOL_GPL(clk_mux_ops); |
| 126 | |
Tomasz Figa | c57acd1 | 2013-07-23 01:49:18 +0200 | [diff] [blame] | 127 | const struct clk_ops clk_mux_ro_ops = { |
| 128 | .get_parent = clk_mux_get_parent, |
| 129 | }; |
| 130 | EXPORT_SYMBOL_GPL(clk_mux_ro_ops); |
| 131 | |
Stephen Boyd | 264b317 | 2016-02-07 00:05:48 -0800 | [diff] [blame] | 132 | struct clk_hw *clk_hw_register_mux_table(struct device *dev, const char *name, |
Sascha Hauer | 2893c37 | 2015-03-31 20:16:52 +0200 | [diff] [blame] | 133 | const char * const *parent_names, u8 num_parents, |
| 134 | unsigned long flags, |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 135 | void __iomem *reg, u8 shift, u32 mask, |
| 136 | u8 clk_mux_flags, u32 *table, spinlock_t *lock) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 137 | { |
| 138 | struct clk_mux *mux; |
Stephen Boyd | 264b317 | 2016-02-07 00:05:48 -0800 | [diff] [blame] | 139 | struct clk_hw *hw; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 140 | struct clk_init_data init; |
Haojian Zhuang | ba492e9 | 2013-06-08 22:47:17 +0800 | [diff] [blame] | 141 | u8 width = 0; |
Stephen Boyd | 264b317 | 2016-02-07 00:05:48 -0800 | [diff] [blame] | 142 | int ret; |
Haojian Zhuang | ba492e9 | 2013-06-08 22:47:17 +0800 | [diff] [blame] | 143 | |
| 144 | if (clk_mux_flags & CLK_MUX_HIWORD_MASK) { |
| 145 | width = fls(mask) - ffs(mask) + 1; |
| 146 | if (width + shift > 16) { |
| 147 | pr_err("mux value exceeds LOWORD field\n"); |
| 148 | return ERR_PTR(-EINVAL); |
| 149 | } |
| 150 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 151 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 152 | /* allocate the mux */ |
Markus Elfring | 1e28733 | 2017-09-26 17:30:06 +0200 | [diff] [blame] | 153 | mux = kzalloc(sizeof(*mux), GFP_KERNEL); |
Markus Elfring | 0b91040 | 2017-09-26 17:23:04 +0200 | [diff] [blame] | 154 | if (!mux) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 155 | return ERR_PTR(-ENOMEM); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 156 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 157 | init.name = name; |
Tomasz Figa | c57acd1 | 2013-07-23 01:49:18 +0200 | [diff] [blame] | 158 | if (clk_mux_flags & CLK_MUX_READ_ONLY) |
| 159 | init.ops = &clk_mux_ro_ops; |
| 160 | else |
| 161 | init.ops = &clk_mux_ops; |
Rajendra Nayak | f7d8caa | 2012-06-01 14:02:47 +0530 | [diff] [blame] | 162 | init.flags = flags | CLK_IS_BASIC; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 163 | init.parent_names = parent_names; |
| 164 | init.num_parents = num_parents; |
| 165 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 166 | /* struct clk_mux assignments */ |
| 167 | mux->reg = reg; |
| 168 | mux->shift = shift; |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 169 | mux->mask = mask; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 170 | mux->flags = clk_mux_flags; |
| 171 | mux->lock = lock; |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 172 | mux->table = table; |
Mike Turquette | 31df9db | 2012-05-06 18:48:11 -0700 | [diff] [blame] | 173 | mux->hw.init = &init; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 174 | |
Stephen Boyd | 264b317 | 2016-02-07 00:05:48 -0800 | [diff] [blame] | 175 | hw = &mux->hw; |
| 176 | ret = clk_hw_register(dev, hw); |
| 177 | if (ret) { |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 178 | kfree(mux); |
Stephen Boyd | 264b317 | 2016-02-07 00:05:48 -0800 | [diff] [blame] | 179 | hw = ERR_PTR(ret); |
| 180 | } |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 181 | |
Stephen Boyd | 264b317 | 2016-02-07 00:05:48 -0800 | [diff] [blame] | 182 | return hw; |
| 183 | } |
| 184 | EXPORT_SYMBOL_GPL(clk_hw_register_mux_table); |
| 185 | |
| 186 | struct clk *clk_register_mux_table(struct device *dev, const char *name, |
| 187 | const char * const *parent_names, u8 num_parents, |
| 188 | unsigned long flags, |
| 189 | void __iomem *reg, u8 shift, u32 mask, |
| 190 | u8 clk_mux_flags, u32 *table, spinlock_t *lock) |
| 191 | { |
| 192 | struct clk_hw *hw; |
| 193 | |
| 194 | hw = clk_hw_register_mux_table(dev, name, parent_names, num_parents, |
| 195 | flags, reg, shift, mask, clk_mux_flags, |
| 196 | table, lock); |
| 197 | if (IS_ERR(hw)) |
| 198 | return ERR_CAST(hw); |
| 199 | return hw->clk; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 200 | } |
Mike Turquette | 5cfe10b | 2013-08-15 19:06:29 -0700 | [diff] [blame] | 201 | EXPORT_SYMBOL_GPL(clk_register_mux_table); |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 202 | |
| 203 | struct clk *clk_register_mux(struct device *dev, const char *name, |
Sascha Hauer | 2893c37 | 2015-03-31 20:16:52 +0200 | [diff] [blame] | 204 | const char * const *parent_names, u8 num_parents, |
| 205 | unsigned long flags, |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 206 | void __iomem *reg, u8 shift, u8 width, |
| 207 | u8 clk_mux_flags, spinlock_t *lock) |
| 208 | { |
| 209 | u32 mask = BIT(width) - 1; |
| 210 | |
| 211 | return clk_register_mux_table(dev, name, parent_names, num_parents, |
| 212 | flags, reg, shift, mask, clk_mux_flags, |
| 213 | NULL, lock); |
| 214 | } |
Mike Turquette | 5cfe10b | 2013-08-15 19:06:29 -0700 | [diff] [blame] | 215 | EXPORT_SYMBOL_GPL(clk_register_mux); |
Krzysztof Kozlowski | 4e3c021 | 2015-01-05 10:52:40 +0100 | [diff] [blame] | 216 | |
Stephen Boyd | 264b317 | 2016-02-07 00:05:48 -0800 | [diff] [blame] | 217 | struct clk_hw *clk_hw_register_mux(struct device *dev, const char *name, |
| 218 | const char * const *parent_names, u8 num_parents, |
| 219 | unsigned long flags, |
| 220 | void __iomem *reg, u8 shift, u8 width, |
| 221 | u8 clk_mux_flags, spinlock_t *lock) |
| 222 | { |
| 223 | u32 mask = BIT(width) - 1; |
| 224 | |
| 225 | return clk_hw_register_mux_table(dev, name, parent_names, num_parents, |
| 226 | flags, reg, shift, mask, clk_mux_flags, |
| 227 | NULL, lock); |
| 228 | } |
| 229 | EXPORT_SYMBOL_GPL(clk_hw_register_mux); |
| 230 | |
Krzysztof Kozlowski | 4e3c021 | 2015-01-05 10:52:40 +0100 | [diff] [blame] | 231 | void clk_unregister_mux(struct clk *clk) |
| 232 | { |
| 233 | struct clk_mux *mux; |
| 234 | struct clk_hw *hw; |
| 235 | |
| 236 | hw = __clk_get_hw(clk); |
| 237 | if (!hw) |
| 238 | return; |
| 239 | |
| 240 | mux = to_clk_mux(hw); |
| 241 | |
| 242 | clk_unregister(clk); |
| 243 | kfree(mux); |
| 244 | } |
| 245 | EXPORT_SYMBOL_GPL(clk_unregister_mux); |
Stephen Boyd | 264b317 | 2016-02-07 00:05:48 -0800 | [diff] [blame] | 246 | |
| 247 | void clk_hw_unregister_mux(struct clk_hw *hw) |
| 248 | { |
| 249 | struct clk_mux *mux; |
| 250 | |
| 251 | mux = to_clk_mux(hw); |
| 252 | |
| 253 | clk_hw_unregister(hw); |
| 254 | kfree(mux); |
| 255 | } |
| 256 | EXPORT_SYMBOL_GPL(clk_hw_unregister_mux); |