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 | |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/clk-provider.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/err.h> |
| 19 | |
| 20 | /* |
| 21 | * DOC: basic adjustable multiplexer clock that cannot gate |
| 22 | * |
| 23 | * Traits of this clock: |
| 24 | * prepare - clk_prepare only ensures that parents are prepared |
| 25 | * enable - clk_enable only ensures that parents are enabled |
| 26 | * rate - rate is only affected by parent switching. No clk_set_rate support |
| 27 | * parent - parent is adjustable through clk_set_parent |
| 28 | */ |
| 29 | |
| 30 | #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw) |
| 31 | |
| 32 | static u8 clk_mux_get_parent(struct clk_hw *hw) |
| 33 | { |
| 34 | struct clk_mux *mux = to_clk_mux(hw); |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 35 | int num_parents = __clk_get_num_parents(hw->clk); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 36 | u32 val; |
| 37 | |
| 38 | /* |
| 39 | * FIXME need a mux-specific flag to determine if val is bitwise or numeric |
| 40 | * e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges from 0x1 |
| 41 | * to 0x7 (index starts at one) |
| 42 | * OTOH, pmd_trace_clk_mux_ck uses a separate bit for each clock, so |
| 43 | * val = 0x4 really means "bit 2, index starts at bit 0" |
| 44 | */ |
| 45 | val = readl(mux->reg) >> mux->shift; |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 46 | val &= mux->mask; |
| 47 | |
| 48 | if (mux->table) { |
| 49 | int i; |
| 50 | |
| 51 | for (i = 0; i < num_parents; i++) |
| 52 | if (mux->table[i] == val) |
| 53 | return i; |
| 54 | return -EINVAL; |
| 55 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 56 | |
| 57 | if (val && (mux->flags & CLK_MUX_INDEX_BIT)) |
| 58 | val = ffs(val) - 1; |
| 59 | |
| 60 | if (val && (mux->flags & CLK_MUX_INDEX_ONE)) |
| 61 | val--; |
| 62 | |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 63 | if (val >= num_parents) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 64 | return -EINVAL; |
| 65 | |
| 66 | return val; |
| 67 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 68 | |
| 69 | static int clk_mux_set_parent(struct clk_hw *hw, u8 index) |
| 70 | { |
| 71 | struct clk_mux *mux = to_clk_mux(hw); |
| 72 | u32 val; |
| 73 | unsigned long flags = 0; |
| 74 | |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 75 | if (mux->table) |
| 76 | index = mux->table[index]; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 77 | |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 78 | else { |
| 79 | if (mux->flags & CLK_MUX_INDEX_BIT) |
| 80 | index = (1 << ffs(index)); |
| 81 | |
| 82 | if (mux->flags & CLK_MUX_INDEX_ONE) |
| 83 | index++; |
| 84 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 85 | |
| 86 | if (mux->lock) |
| 87 | spin_lock_irqsave(mux->lock, flags); |
| 88 | |
| 89 | val = readl(mux->reg); |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 90 | val &= ~(mux->mask << mux->shift); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 91 | val |= index << mux->shift; |
| 92 | writel(val, mux->reg); |
| 93 | |
| 94 | if (mux->lock) |
| 95 | spin_unlock_irqrestore(mux->lock, flags); |
| 96 | |
| 97 | return 0; |
| 98 | } |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 99 | |
Shawn Guo | 822c250 | 2012-03-27 15:23:22 +0800 | [diff] [blame] | 100 | const struct clk_ops clk_mux_ops = { |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 101 | .get_parent = clk_mux_get_parent, |
| 102 | .set_parent = clk_mux_set_parent, |
| 103 | }; |
| 104 | EXPORT_SYMBOL_GPL(clk_mux_ops); |
| 105 | |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 106 | struct clk *clk_register_mux_table(struct device *dev, const char *name, |
Mark Brown | d305fb7 | 2012-03-21 20:01:20 +0000 | [diff] [blame] | 107 | const char **parent_names, u8 num_parents, unsigned long flags, |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 108 | void __iomem *reg, u8 shift, u32 mask, |
| 109 | u8 clk_mux_flags, u32 *table, spinlock_t *lock) |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 110 | { |
| 111 | struct clk_mux *mux; |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 112 | struct clk *clk; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 113 | struct clk_init_data init; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 114 | |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 115 | /* allocate the mux */ |
Shawn Guo | 10363b5 | 2012-03-27 15:23:20 +0800 | [diff] [blame] | 116 | mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL); |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 117 | if (!mux) { |
| 118 | pr_err("%s: could not allocate mux clk\n", __func__); |
| 119 | return ERR_PTR(-ENOMEM); |
| 120 | } |
| 121 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 122 | init.name = name; |
| 123 | init.ops = &clk_mux_ops; |
Rajendra Nayak | f7d8caa | 2012-06-01 14:02:47 +0530 | [diff] [blame] | 124 | init.flags = flags | CLK_IS_BASIC; |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 125 | init.parent_names = parent_names; |
| 126 | init.num_parents = num_parents; |
| 127 | |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 128 | /* struct clk_mux assignments */ |
| 129 | mux->reg = reg; |
| 130 | mux->shift = shift; |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 131 | mux->mask = mask; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 132 | mux->flags = clk_mux_flags; |
| 133 | mux->lock = lock; |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 134 | mux->table = table; |
Mike Turquette | 31df9db | 2012-05-06 18:48:11 -0700 | [diff] [blame] | 135 | mux->hw.init = &init; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 136 | |
Saravana Kannan | 0197b3e | 2012-04-25 22:58:56 -0700 | [diff] [blame] | 137 | clk = clk_register(dev, &mux->hw); |
Mike Turquette | 27d5459 | 2012-03-26 17:51:03 -0700 | [diff] [blame] | 138 | |
| 139 | if (IS_ERR(clk)) |
| 140 | kfree(mux); |
| 141 | |
| 142 | return clk; |
Mike Turquette | 9d9f78e | 2012-03-15 23:11:20 -0700 | [diff] [blame] | 143 | } |
Peter De Schrijver | ce4f331 | 2013-03-22 14:07:53 +0200 | [diff] [blame] | 144 | |
| 145 | struct clk *clk_register_mux(struct device *dev, const char *name, |
| 146 | const char **parent_names, u8 num_parents, unsigned long flags, |
| 147 | void __iomem *reg, u8 shift, u8 width, |
| 148 | u8 clk_mux_flags, spinlock_t *lock) |
| 149 | { |
| 150 | u32 mask = BIT(width) - 1; |
| 151 | |
| 152 | return clk_register_mux_table(dev, name, parent_names, num_parents, |
| 153 | flags, reg, shift, mask, clk_mux_flags, |
| 154 | NULL, lock); |
| 155 | } |