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 | |
| 29 | #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw) |
| 30 | |
| 31 | static u8 clk_mux_get_parent(struct clk_hw *hw) |
| 32 | { |
| 33 | struct clk_mux *mux = to_clk_mux(hw); |
Stephen Boyd | 497295a | 2015-06-25 16:53:23 -0700 | [diff] [blame] | 34 | int num_parents = clk_hw_get_num_parents(hw); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 35 | u32 val; |
| 36 | |
| 37 | /* |
| 38 | * FIXME need a mux-specific flag to determine if val is bitwise or numeric |
| 39 | * e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges from 0x1 |
| 40 | * to 0x7 (index starts at one) |
| 41 | * OTOH, pmd_trace_clk_mux_ck uses a separate bit for each clock, so |
| 42 | * val = 0x4 really means "bit 2, index starts at bit 0" |
| 43 | */ |
Gerhard Sittig | aa514ce | 2013-07-22 14:14:40 +0200 | [diff] [blame] | 44 | val = clk_readl(mux->reg) >> mux->shift; |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 45 | val &= mux->mask; |
| 46 | |
| 47 | if (mux->table) { |
| 48 | int i; |
| 49 | |
| 50 | for (i = 0; i < num_parents; i++) |
| 51 | if (mux->table[i] == val) |
| 52 | return i; |
| 53 | return -EINVAL; |
| 54 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 55 | |
| 56 | if (val && (mux->flags & CLK_MUX_INDEX_BIT)) |
| 57 | val = ffs(val) - 1; |
| 58 | |
| 59 | if (val && (mux->flags & CLK_MUX_INDEX_ONE)) |
| 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 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 67 | |
| 68 | static int clk_mux_set_parent(struct clk_hw *hw, u8 index) |
| 69 | { |
| 70 | struct clk_mux *mux = to_clk_mux(hw); |
| 71 | u32 val; |
| 72 | unsigned long flags = 0; |
| 73 | |
Masahiro Yamada | 3837bd2 | 2015-11-05 17:59:39 +0900 | [diff] [blame] | 74 | if (mux->table) { |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 75 | index = mux->table[index]; |
Masahiro Yamada | 3837bd2 | 2015-11-05 17:59:39 +0900 | [diff] [blame] | 76 | } else { |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 77 | if (mux->flags & CLK_MUX_INDEX_BIT) |
Hans de Goede | 6793b3c | 2014-11-19 14:48:59 +0100 | [diff] [blame] | 78 | index = 1 << index; |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 79 | |
| 80 | if (mux->flags & CLK_MUX_INDEX_ONE) |
| 81 | index++; |
| 82 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 83 | |
| 84 | if (mux->lock) |
| 85 | spin_lock_irqsave(mux->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 86 | else |
| 87 | __acquire(mux->lock); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 88 | |
Haojian Zhuang | ba492e9 | 2013-06-08 22:47:17 +0800 | [diff] [blame] | 89 | if (mux->flags & CLK_MUX_HIWORD_MASK) { |
| 90 | val = mux->mask << (mux->shift + 16); |
| 91 | } else { |
Gerhard Sittig | aa514ce | 2013-07-22 14:14:40 +0200 | [diff] [blame] | 92 | val = clk_readl(mux->reg); |
Haojian Zhuang | ba492e9 | 2013-06-08 22:47:17 +0800 | [diff] [blame] | 93 | val &= ~(mux->mask << mux->shift); |
| 94 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 95 | val |= index << mux->shift; |
Gerhard Sittig | aa514ce | 2013-07-22 14:14:40 +0200 | [diff] [blame] | 96 | clk_writel(val, mux->reg); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 97 | |
| 98 | if (mux->lock) |
| 99 | spin_unlock_irqrestore(mux->lock, flags); |
Stephen Boyd | 661e218 | 2015-07-24 12:21:12 -0700 | [diff] [blame] | 100 | else |
| 101 | __release(mux->lock); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 105 | |
Shawn Guo | 822c250 | 2012-03-27 15:23:22 +0800 | [diff] [blame] | 106 | const struct clk_ops clk_mux_ops = { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 107 | .get_parent = clk_mux_get_parent, |
| 108 | .set_parent = clk_mux_set_parent, |
James Hogan | e366fdd | 2013-07-29 12:25:02 +0100 | [diff] [blame] | 109 | .determine_rate = __clk_mux_determine_rate, |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 110 | }; |
| 111 | EXPORT_SYMBOL_GPL(clk_mux_ops); |
| 112 | |
Tomasz Figa | c57acd1 | 2013-07-23 01:49:18 +0200 | [diff] [blame] | 113 | const struct clk_ops clk_mux_ro_ops = { |
| 114 | .get_parent = clk_mux_get_parent, |
| 115 | }; |
| 116 | EXPORT_SYMBOL_GPL(clk_mux_ro_ops); |
| 117 | |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 118 | struct clk *clk_register_mux_table(struct device *dev, const char *name, |
Sascha Hauer | 2893c37 | 2015-03-31 20:16:52 +0200 | [diff] [blame] | 119 | const char * const *parent_names, u8 num_parents, |
| 120 | unsigned long flags, |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 121 | void __iomem *reg, u8 shift, u32 mask, |
| 122 | u8 clk_mux_flags, u32 *table, spinlock_t *lock) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 123 | { |
| 124 | struct clk_mux *mux; |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 125 | struct clk *clk; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 126 | struct clk_init_data init; |
Haojian Zhuang | ba492e9 | 2013-06-08 22:47:17 +0800 | [diff] [blame] | 127 | u8 width = 0; |
| 128 | |
| 129 | if (clk_mux_flags & CLK_MUX_HIWORD_MASK) { |
| 130 | width = fls(mask) - ffs(mask) + 1; |
| 131 | if (width + shift > 16) { |
| 132 | pr_err("mux value exceeds LOWORD field\n"); |
| 133 | return ERR_PTR(-EINVAL); |
| 134 | } |
| 135 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 136 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 137 | /* allocate the mux */ |
Shawn Guo | 10363b5 | 2012-03-27 15:23:20 +0800 | [diff] [blame] | 138 | mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 139 | if (!mux) { |
| 140 | pr_err("%s: could not allocate mux clk\n", __func__); |
| 141 | return ERR_PTR(-ENOMEM); |
| 142 | } |
| 143 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 144 | init.name = name; |
Tomasz Figa | c57acd1 | 2013-07-23 01:49:18 +0200 | [diff] [blame] | 145 | if (clk_mux_flags & CLK_MUX_READ_ONLY) |
| 146 | init.ops = &clk_mux_ro_ops; |
| 147 | else |
| 148 | init.ops = &clk_mux_ops; |
Rajendra Nayak | f7d8caa | 2012-06-01 14:02:47 +0530 | [diff] [blame] | 149 | init.flags = flags | CLK_IS_BASIC; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 150 | init.parent_names = parent_names; |
| 151 | init.num_parents = num_parents; |
| 152 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 153 | /* struct clk_mux assignments */ |
| 154 | mux->reg = reg; |
| 155 | mux->shift = shift; |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 156 | mux->mask = mask; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 157 | mux->flags = clk_mux_flags; |
| 158 | mux->lock = lock; |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 159 | mux->table = table; |
Mike Turquette | 31df9db | 2012-05-06 18:48:11 -0700 | [diff] [blame] | 160 | mux->hw.init = &init; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 161 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 162 | clk = clk_register(dev, &mux->hw); |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 163 | |
| 164 | if (IS_ERR(clk)) |
| 165 | kfree(mux); |
| 166 | |
| 167 | return clk; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 168 | } |
Mike Turquette | 5cfe10b | 2013-08-15 19:06:29 -0700 | [diff] [blame] | 169 | EXPORT_SYMBOL_GPL(clk_register_mux_table); |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 170 | |
| 171 | struct clk *clk_register_mux(struct device *dev, const char *name, |
Sascha Hauer | 2893c37 | 2015-03-31 20:16:52 +0200 | [diff] [blame] | 172 | const char * const *parent_names, u8 num_parents, |
| 173 | unsigned long flags, |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 174 | void __iomem *reg, u8 shift, u8 width, |
| 175 | u8 clk_mux_flags, spinlock_t *lock) |
| 176 | { |
| 177 | u32 mask = BIT(width) - 1; |
| 178 | |
| 179 | return clk_register_mux_table(dev, name, parent_names, num_parents, |
| 180 | flags, reg, shift, mask, clk_mux_flags, |
| 181 | NULL, lock); |
| 182 | } |
Mike Turquette | 5cfe10b | 2013-08-15 19:06:29 -0700 | [diff] [blame] | 183 | EXPORT_SYMBOL_GPL(clk_register_mux); |
Krzysztof Kozlowski | 4e3c021 | 2015-01-05 10:52:40 +0100 | [diff] [blame] | 184 | |
| 185 | void clk_unregister_mux(struct clk *clk) |
| 186 | { |
| 187 | struct clk_mux *mux; |
| 188 | struct clk_hw *hw; |
| 189 | |
| 190 | hw = __clk_get_hw(clk); |
| 191 | if (!hw) |
| 192 | return; |
| 193 | |
| 194 | mux = to_clk_mux(hw); |
| 195 | |
| 196 | clk_unregister(clk); |
| 197 | kfree(mux); |
| 198 | } |
| 199 | EXPORT_SYMBOL_GPL(clk_unregister_mux); |