David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> |
| 4 | * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> |
| 5 | * |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 6 | * Gated clock implementation |
| 7 | */ |
| 8 | |
| 9 | #include <linux/clk-provider.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 10 | #include <linux/export.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/io.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/string.h> |
| 16 | #include "clk.h" |
| 17 | |
| 18 | /** |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 19 | * DOC: basic gateable clock which can gate and ungate its output |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 20 | * |
| 21 | * Traits of this clock: |
| 22 | * prepare - clk_(un)prepare only ensures parent is (un)prepared |
| 23 | * enable - clk_enable and clk_disable are functional & control gating |
| 24 | * rate - inherits rate from parent. No clk_set_rate support |
| 25 | * parent - fixed parent. No clk_set_parent support |
| 26 | */ |
| 27 | |
| 28 | struct clk_gate2 { |
| 29 | struct clk_hw hw; |
| 30 | void __iomem *reg; |
| 31 | u8 bit_idx; |
| 32 | u8 cgr_val; |
| 33 | u8 flags; |
| 34 | spinlock_t *lock; |
| 35 | unsigned int *share_count; |
| 36 | }; |
| 37 | |
| 38 | #define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw) |
| 39 | |
| 40 | static int clk_gate2_enable(struct clk_hw *hw) |
| 41 | { |
| 42 | struct clk_gate2 *gate = to_clk_gate2(hw); |
| 43 | u32 reg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 44 | unsigned long flags; |
| 45 | int ret = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 46 | |
| 47 | spin_lock_irqsave(gate->lock, flags); |
| 48 | |
| 49 | if (gate->share_count && (*gate->share_count)++ > 0) |
| 50 | goto out; |
| 51 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 52 | if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT) { |
| 53 | ret = clk_gate_ops.enable(hw); |
| 54 | } else { |
| 55 | reg = readl(gate->reg); |
| 56 | reg &= ~(3 << gate->bit_idx); |
| 57 | reg |= gate->cgr_val << gate->bit_idx; |
| 58 | writel(reg, gate->reg); |
| 59 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 60 | |
| 61 | out: |
| 62 | spin_unlock_irqrestore(gate->lock, flags); |
| 63 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 64 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void clk_gate2_disable(struct clk_hw *hw) |
| 68 | { |
| 69 | struct clk_gate2 *gate = to_clk_gate2(hw); |
| 70 | u32 reg; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 71 | unsigned long flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 72 | |
| 73 | spin_lock_irqsave(gate->lock, flags); |
| 74 | |
| 75 | if (gate->share_count) { |
| 76 | if (WARN_ON(*gate->share_count == 0)) |
| 77 | goto out; |
| 78 | else if (--(*gate->share_count) > 0) |
| 79 | goto out; |
| 80 | } |
| 81 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 82 | if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT) { |
| 83 | clk_gate_ops.disable(hw); |
| 84 | } else { |
| 85 | reg = readl(gate->reg); |
| 86 | reg &= ~(3 << gate->bit_idx); |
| 87 | writel(reg, gate->reg); |
| 88 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 89 | |
| 90 | out: |
| 91 | spin_unlock_irqrestore(gate->lock, flags); |
| 92 | } |
| 93 | |
| 94 | static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx) |
| 95 | { |
| 96 | u32 val = readl(reg); |
| 97 | |
| 98 | if (((val >> bit_idx) & 1) == 1) |
| 99 | return 1; |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int clk_gate2_is_enabled(struct clk_hw *hw) |
| 105 | { |
| 106 | struct clk_gate2 *gate = to_clk_gate2(hw); |
| 107 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 108 | if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT) |
| 109 | return clk_gate_ops.is_enabled(hw); |
| 110 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 111 | return clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx); |
| 112 | } |
| 113 | |
| 114 | static void clk_gate2_disable_unused(struct clk_hw *hw) |
| 115 | { |
| 116 | struct clk_gate2 *gate = to_clk_gate2(hw); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 117 | unsigned long flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 118 | u32 reg; |
| 119 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 120 | if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT) |
| 121 | return; |
| 122 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 123 | spin_lock_irqsave(gate->lock, flags); |
| 124 | |
| 125 | if (!gate->share_count || *gate->share_count == 0) { |
| 126 | reg = readl(gate->reg); |
| 127 | reg &= ~(3 << gate->bit_idx); |
| 128 | writel(reg, gate->reg); |
| 129 | } |
| 130 | |
| 131 | spin_unlock_irqrestore(gate->lock, flags); |
| 132 | } |
| 133 | |
| 134 | static const struct clk_ops clk_gate2_ops = { |
| 135 | .enable = clk_gate2_enable, |
| 136 | .disable = clk_gate2_disable, |
| 137 | .disable_unused = clk_gate2_disable_unused, |
| 138 | .is_enabled = clk_gate2_is_enabled, |
| 139 | }; |
| 140 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 141 | struct clk_hw *clk_hw_register_gate2(struct device *dev, const char *name, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 142 | const char *parent_name, unsigned long flags, |
| 143 | void __iomem *reg, u8 bit_idx, u8 cgr_val, |
| 144 | u8 clk_gate2_flags, spinlock_t *lock, |
| 145 | unsigned int *share_count) |
| 146 | { |
| 147 | struct clk_gate2 *gate; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 148 | struct clk_hw *hw; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 149 | struct clk_init_data init; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 150 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 151 | |
| 152 | gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL); |
| 153 | if (!gate) |
| 154 | return ERR_PTR(-ENOMEM); |
| 155 | |
| 156 | /* struct clk_gate2 assignments */ |
| 157 | gate->reg = reg; |
| 158 | gate->bit_idx = bit_idx; |
| 159 | gate->cgr_val = cgr_val; |
| 160 | gate->flags = clk_gate2_flags; |
| 161 | gate->lock = lock; |
| 162 | gate->share_count = share_count; |
| 163 | |
| 164 | init.name = name; |
| 165 | init.ops = &clk_gate2_ops; |
| 166 | init.flags = flags; |
| 167 | init.parent_names = parent_name ? &parent_name : NULL; |
| 168 | init.num_parents = parent_name ? 1 : 0; |
| 169 | |
| 170 | gate->hw.init = &init; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 171 | hw = &gate->hw; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 172 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 173 | ret = clk_hw_register(dev, hw); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 174 | if (ret) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 175 | kfree(gate); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 176 | return ERR_PTR(ret); |
| 177 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 178 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 179 | return hw; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 180 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 181 | EXPORT_SYMBOL_GPL(clk_hw_register_gate2); |