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