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