blob: db44a198a0d9999f3e998033a3f8a517035c5e74 [file] [log] [blame]
Sascha Hauerb75c0152011-04-19 08:33:45 +02001/*
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 Estevamd7b8c032013-03-25 09:20:35 -030018#include "clk.h"
Sascha Hauerb75c0152011-04-19 08:33:45 +020019
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 Guo54ee1472014-04-18 15:55:16 +080030struct clk_gate2 {
31 struct clk_hw hw;
32 void __iomem *reg;
33 u8 bit_idx;
Stefan Agner45682922016-03-09 18:16:47 -080034 u8 cgr_val;
Shawn Guo54ee1472014-04-18 15:55:16 +080035 u8 flags;
36 spinlock_t *lock;
Shawn Guof9f28cd2014-04-19 10:58:22 +080037 unsigned int *share_count;
Shawn Guo54ee1472014-04-18 15:55:16 +080038};
39
40#define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
Sascha Hauerb75c0152011-04-19 08:33:45 +020041
42static int clk_gate2_enable(struct clk_hw *hw)
43{
Shawn Guo54ee1472014-04-18 15:55:16 +080044 struct clk_gate2 *gate = to_clk_gate2(hw);
Sascha Hauerb75c0152011-04-19 08:33:45 +020045 u32 reg;
46 unsigned long flags = 0;
47
Shawn Guo94b5c022014-04-18 16:07:44 +080048 spin_lock_irqsave(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020049
Shawn Guof9f28cd2014-04-19 10:58:22 +080050 if (gate->share_count && (*gate->share_count)++ > 0)
51 goto out;
52
Sascha Hauerb75c0152011-04-19 08:33:45 +020053 reg = readl(gate->reg);
Stefan Agner45682922016-03-09 18:16:47 -080054 reg &= ~(3 << gate->bit_idx);
55 reg |= gate->cgr_val << gate->bit_idx;
Sascha Hauerb75c0152011-04-19 08:33:45 +020056 writel(reg, gate->reg);
57
Shawn Guof9f28cd2014-04-19 10:58:22 +080058out:
Shawn Guo94b5c022014-04-18 16:07:44 +080059 spin_unlock_irqrestore(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020060
61 return 0;
62}
63
64static void clk_gate2_disable(struct clk_hw *hw)
65{
Shawn Guo54ee1472014-04-18 15:55:16 +080066 struct clk_gate2 *gate = to_clk_gate2(hw);
Sascha Hauerb75c0152011-04-19 08:33:45 +020067 u32 reg;
68 unsigned long flags = 0;
69
Shawn Guo94b5c022014-04-18 16:07:44 +080070 spin_lock_irqsave(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020071
Shawn Guo63288b72014-07-07 10:53:51 +080072 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 Guof9f28cd2014-04-19 10:58:22 +080078
Sascha Hauerb75c0152011-04-19 08:33:45 +020079 reg = readl(gate->reg);
80 reg &= ~(3 << gate->bit_idx);
81 writel(reg, gate->reg);
82
Shawn Guof9f28cd2014-04-19 10:58:22 +080083out:
Shawn Guo94b5c022014-04-18 16:07:44 +080084 spin_unlock_irqrestore(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020085}
86
Shawn Guo63288b72014-07-07 10:53:51 +080087static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx)
Sascha Hauerb75c0152011-04-19 08:33:45 +020088{
Shawn Guo63288b72014-07-07 10:53:51 +080089 u32 val = readl(reg);
Sascha Hauerb75c0152011-04-19 08:33:45 +020090
Shawn Guo63288b72014-07-07 10:53:51 +080091 if (((val >> bit_idx) & 1) == 1)
Sascha Hauerb75c0152011-04-19 08:33:45 +020092 return 1;
93
94 return 0;
95}
96
Shawn Guo63288b72014-07-07 10:53:51 +080097static int clk_gate2_is_enabled(struct clk_hw *hw)
98{
99 struct clk_gate2 *gate = to_clk_gate2(hw);
100
Anson Huang3d27bc52014-12-10 17:51:42 +0800101 return clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx);
102}
103
104static 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 Guo63288b72014-07-07 10:53:51 +0800119}
120
Sascha Hauerb75c0152011-04-19 08:33:45 +0200121static struct clk_ops clk_gate2_ops = {
122 .enable = clk_gate2_enable,
123 .disable = clk_gate2_disable,
Anson Huang3d27bc52014-12-10 17:51:42 +0800124 .disable_unused = clk_gate2_disable_unused,
Sascha Hauerb75c0152011-04-19 08:33:45 +0200125 .is_enabled = clk_gate2_is_enabled,
126};
127
128struct clk *clk_register_gate2(struct device *dev, const char *name,
129 const char *parent_name, unsigned long flags,
Stefan Agner45682922016-03-09 18:16:47 -0800130 void __iomem *reg, u8 bit_idx, u8 cgr_val,
Shawn Guof9f28cd2014-04-19 10:58:22 +0800131 u8 clk_gate2_flags, spinlock_t *lock,
132 unsigned int *share_count)
Sascha Hauerb75c0152011-04-19 08:33:45 +0200133{
Shawn Guo54ee1472014-04-18 15:55:16 +0800134 struct clk_gate2 *gate;
Sascha Hauerb75c0152011-04-19 08:33:45 +0200135 struct clk *clk;
136 struct clk_init_data init;
137
Shawn Guo54ee1472014-04-18 15:55:16 +0800138 gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
Sascha Hauerb75c0152011-04-19 08:33:45 +0200139 if (!gate)
140 return ERR_PTR(-ENOMEM);
141
Shawn Guo54ee1472014-04-18 15:55:16 +0800142 /* struct clk_gate2 assignments */
Sascha Hauerb75c0152011-04-19 08:33:45 +0200143 gate->reg = reg;
144 gate->bit_idx = bit_idx;
Stefan Agner45682922016-03-09 18:16:47 -0800145 gate->cgr_val = cgr_val;
Sascha Hauerb75c0152011-04-19 08:33:45 +0200146 gate->flags = clk_gate2_flags;
147 gate->lock = lock;
Shawn Guof9f28cd2014-04-19 10:58:22 +0800148 gate->share_count = share_count;
Sascha Hauerb75c0152011-04-19 08:33:45 +0200149
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 Yongjunecf026d2012-10-25 23:02:18 +0800160 kfree(gate);
Sascha Hauerb75c0152011-04-19 08:33:45 +0200161
162 return clk;
163}