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