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; |
| 34 | u8 flags; |
| 35 | spinlock_t *lock; |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 36 | unsigned int *share_count; |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | #define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw) |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 40 | |
| 41 | static int clk_gate2_enable(struct clk_hw *hw) |
| 42 | { |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 43 | struct clk_gate2 *gate = to_clk_gate2(hw); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 44 | u32 reg; |
| 45 | unsigned long flags = 0; |
| 46 | |
Shawn Guo | 94b5c02 | 2014-04-18 16:07:44 +0800 | [diff] [blame] | 47 | spin_lock_irqsave(gate->lock, flags); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 48 | |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 49 | if (gate->share_count && (*gate->share_count)++ > 0) |
| 50 | goto out; |
| 51 | |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 52 | reg = readl(gate->reg); |
| 53 | reg |= 3 << gate->bit_idx; |
| 54 | writel(reg, gate->reg); |
| 55 | |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 56 | out: |
Shawn Guo | 94b5c02 | 2014-04-18 16:07:44 +0800 | [diff] [blame] | 57 | spin_unlock_irqrestore(gate->lock, flags); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static void clk_gate2_disable(struct clk_hw *hw) |
| 63 | { |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 64 | struct clk_gate2 *gate = to_clk_gate2(hw); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 65 | u32 reg; |
| 66 | unsigned long flags = 0; |
| 67 | |
Shawn Guo | 94b5c02 | 2014-04-18 16:07:44 +0800 | [diff] [blame] | 68 | spin_lock_irqsave(gate->lock, flags); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 69 | |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 70 | if (gate->share_count && --(*gate->share_count) > 0) |
| 71 | goto out; |
| 72 | |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 73 | reg = readl(gate->reg); |
| 74 | reg &= ~(3 << gate->bit_idx); |
| 75 | writel(reg, gate->reg); |
| 76 | |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 77 | out: |
Shawn Guo | 94b5c02 | 2014-04-18 16:07:44 +0800 | [diff] [blame] | 78 | spin_unlock_irqrestore(gate->lock, flags); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static int clk_gate2_is_enabled(struct clk_hw *hw) |
| 82 | { |
| 83 | u32 reg; |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 84 | struct clk_gate2 *gate = to_clk_gate2(hw); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 85 | |
| 86 | reg = readl(gate->reg); |
| 87 | |
Anson Huang | b4e844f | 2013-12-24 14:21:27 -0500 | [diff] [blame] | 88 | if (((reg >> gate->bit_idx) & 1) == 1) |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 89 | return 1; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static struct clk_ops clk_gate2_ops = { |
| 95 | .enable = clk_gate2_enable, |
| 96 | .disable = clk_gate2_disable, |
| 97 | .is_enabled = clk_gate2_is_enabled, |
| 98 | }; |
| 99 | |
| 100 | struct clk *clk_register_gate2(struct device *dev, const char *name, |
| 101 | const char *parent_name, unsigned long flags, |
| 102 | void __iomem *reg, u8 bit_idx, |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 103 | u8 clk_gate2_flags, spinlock_t *lock, |
| 104 | unsigned int *share_count) |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 105 | { |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 106 | struct clk_gate2 *gate; |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 107 | struct clk *clk; |
| 108 | struct clk_init_data init; |
| 109 | |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 110 | gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 111 | if (!gate) |
| 112 | return ERR_PTR(-ENOMEM); |
| 113 | |
Shawn Guo | 54ee147 | 2014-04-18 15:55:16 +0800 | [diff] [blame] | 114 | /* struct clk_gate2 assignments */ |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 115 | gate->reg = reg; |
| 116 | gate->bit_idx = bit_idx; |
| 117 | gate->flags = clk_gate2_flags; |
| 118 | gate->lock = lock; |
Shawn Guo | f9f28cd | 2014-04-19 10:58:22 +0800 | [diff] [blame] | 119 | gate->share_count = share_count; |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 120 | |
| 121 | init.name = name; |
| 122 | init.ops = &clk_gate2_ops; |
| 123 | init.flags = flags; |
| 124 | init.parent_names = parent_name ? &parent_name : NULL; |
| 125 | init.num_parents = parent_name ? 1 : 0; |
| 126 | |
| 127 | gate->hw.init = &init; |
| 128 | |
| 129 | clk = clk_register(dev, &gate->hw); |
| 130 | if (IS_ERR(clk)) |
Wei Yongjun | ecf026d | 2012-10-25 23:02:18 +0800 | [diff] [blame] | 131 | kfree(gate); |
Sascha Hauer | b75c015 | 2011-04-19 08:33:45 +0200 | [diff] [blame] | 132 | |
| 133 | return clk; |
| 134 | } |