Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> |
| 3 | * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Gated clock implementation |
| 10 | */ |
| 11 | |
| 12 | #include <linux/clk-provider.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/string.h> |
Fabio Estevam | d7b8c03 | 2013-03-25 09:20:35 -0300 | [diff] [blame] | 18 | #include "clk.h" |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * DOC: basic gatable clock which can gate and ungate it's ouput |
| 22 | * |
| 23 | * Traits of this clock: |
| 24 | * prepare - clk_(un)prepare only ensures parent is (un)prepared |
| 25 | * enable - clk_enable and clk_disable are functional & control gating |
| 26 | * rate - inherits rate from parent. No clk_set_rate support |
| 27 | * parent - fixed parent. No clk_set_parent support |
| 28 | */ |
| 29 | |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 30 | struct clk_gate2 { |
| 31 | struct clk_hw hw; |
| 32 | void __iomem *reg; |
| 33 | u8 bit_idx; |
Stefan Agner | 4568292 | 2016-03-09 18:16:47 -0800 | [diff] [blame] | 34 | u8 cgr_val; |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 35 | u8 flags; |
| 36 | spinlock_t *lock; |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 37 | unsigned int *share_count; |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | #define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw) |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 41 | |
| 42 | static int clk_gate2_enable(struct clk_hw *hw) |
| 43 | { |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 44 | struct clk_gate2 *gate = to_clk_gate2(hw); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 45 | u32 reg; |
| 46 | unsigned long flags = 0; |
| 47 | |
Shawn Guo | 94b5c02 | 2014-04-18 16:07:44 +0800 | [diff] [blame] | 48 | spin_lock_irqsave(gate->lock, flags); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 49 | |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 50 | if (gate->share_count && (*gate->share_count)++ > 0) |
| 51 | goto out; |
| 52 | |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 53 | reg = readl(gate->reg); |
Stefan Agner | 4568292 | 2016-03-09 18:16:47 -0800 | [diff] [blame] | 54 | reg &= ~(3 << gate->bit_idx); |
| 55 | reg |= gate->cgr_val << gate->bit_idx; |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 56 | writel(reg, gate->reg); |
| 57 | |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 58 | out: |
Shawn Guo | 94b5c02 | 2014-04-18 16:07:44 +0800 | [diff] [blame] | 59 | spin_unlock_irqrestore(gate->lock, flags); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static void clk_gate2_disable(struct clk_hw *hw) |
| 65 | { |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 66 | struct clk_gate2 *gate = to_clk_gate2(hw); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 67 | u32 reg; |
| 68 | unsigned long flags = 0; |
| 69 | |
Shawn Guo | 94b5c02 | 2014-04-18 16:07:44 +0800 | [diff] [blame] | 70 | spin_lock_irqsave(gate->lock, flags); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 71 | |
Shawn Guo | 63288b7 | 2014-07-07 10:53:51 +0800 | [diff] [blame] | 72 | if (gate->share_count) { |
| 73 | if (WARN_ON(*gate->share_count == 0)) |
| 74 | goto out; |
| 75 | else if (--(*gate->share_count) > 0) |
| 76 | goto out; |
| 77 | } |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 78 | |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 79 | reg = readl(gate->reg); |
| 80 | reg &= ~(3 << gate->bit_idx); |
| 81 | writel(reg, gate->reg); |
| 82 | |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 83 | out: |
Shawn Guo | 94b5c02 | 2014-04-18 16:07:44 +0800 | [diff] [blame] | 84 | spin_unlock_irqrestore(gate->lock, flags); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Shawn Guo | 63288b7 | 2014-07-07 10:53:51 +0800 | [diff] [blame] | 87 | static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx) |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 88 | { |
Shawn Guo | 63288b7 | 2014-07-07 10:53:51 +0800 | [diff] [blame] | 89 | u32 val = readl(reg); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 90 | |
Shawn Guo | 63288b7 | 2014-07-07 10:53:51 +0800 | [diff] [blame] | 91 | if (((val >> bit_idx) & 1) == 1) |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 92 | return 1; |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Shawn Guo | 63288b7 | 2014-07-07 10:53:51 +0800 | [diff] [blame] | 97 | static int clk_gate2_is_enabled(struct clk_hw *hw) |
| 98 | { |
| 99 | struct clk_gate2 *gate = to_clk_gate2(hw); |
| 100 | |
Anson Huang | 3d27bc5 | 2014-12-10 17:51:42 +0800 | [diff] [blame] | 101 | return clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx); |
| 102 | } |
| 103 | |
| 104 | static void clk_gate2_disable_unused(struct clk_hw *hw) |
| 105 | { |
| 106 | struct clk_gate2 *gate = to_clk_gate2(hw); |
| 107 | unsigned long flags = 0; |
| 108 | u32 reg; |
| 109 | |
| 110 | spin_lock_irqsave(gate->lock, flags); |
| 111 | |
| 112 | if (!gate->share_count || *gate->share_count == 0) { |
| 113 | reg = readl(gate->reg); |
| 114 | reg &= ~(3 << gate->bit_idx); |
| 115 | writel(reg, gate->reg); |
| 116 | } |
| 117 | |
| 118 | spin_unlock_irqrestore(gate->lock, flags); |
Shawn Guo | 63288b7 | 2014-07-07 10:53:51 +0800 | [diff] [blame] | 119 | } |
| 120 | |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 121 | static struct clk_ops clk_gate2_ops = { |
| 122 | .enable = clk_gate2_enable, |
| 123 | .disable = clk_gate2_disable, |
Anson Huang | 3d27bc5 | 2014-12-10 17:51:42 +0800 | [diff] [blame] | 124 | .disable_unused = clk_gate2_disable_unused, |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 125 | .is_enabled = clk_gate2_is_enabled, |
| 126 | }; |
| 127 | |
| 128 | struct clk *clk_register_gate2(struct device *dev, const char *name, |
| 129 | const char *parent_name, unsigned long flags, |
Stefan Agner | 4568292 | 2016-03-09 18:16:47 -0800 | [diff] [blame] | 130 | void __iomem *reg, u8 bit_idx, u8 cgr_val, |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 131 | u8 clk_gate2_flags, spinlock_t *lock, |
| 132 | unsigned int *share_count) |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 133 | { |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 134 | struct clk_gate2 *gate; |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 135 | struct clk *clk; |
| 136 | struct clk_init_data init; |
| 137 | |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 138 | gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 139 | if (!gate) |
| 140 | return ERR_PTR(-ENOMEM); |
| 141 | |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 142 | /* struct clk_gate2 assignments */ |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 143 | gate->reg = reg; |
| 144 | gate->bit_idx = bit_idx; |
Stefan Agner | 4568292 | 2016-03-09 18:16:47 -0800 | [diff] [blame] | 145 | gate->cgr_val = cgr_val; |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 146 | gate->flags = clk_gate2_flags; |
| 147 | gate->lock = lock; |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 148 | gate->share_count = share_count; |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 149 | |
| 150 | init.name = name; |
| 151 | init.ops = &clk_gate2_ops; |
| 152 | init.flags = flags; |
| 153 | init.parent_names = parent_name ? &parent_name : NULL; |
| 154 | init.num_parents = parent_name ? 1 : 0; |
| 155 | |
| 156 | gate->hw.init = &init; |
| 157 | |
| 158 | clk = clk_register(dev, &gate->hw); |
| 159 | if (IS_ERR(clk)) |
Wei Yongjun | ecf026d | 2012-10-25 23:02:18 +0800 | [diff] [blame] | 160 | kfree(gate); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 161 | |
| 162 | return clk; |
| 163 | } |