blob: 5a75cdc81891c369d3cfd738d9fb3271e7ca208b [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;
34 u8 flags;
35 spinlock_t *lock;
Shawn Guof9f28cd2014-04-19 10:58:22 +080036 unsigned int *share_count;
Shawn Guo54ee1472014-04-18 15:55:16 +080037};
38
39#define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
Sascha Hauerb75c0152011-04-19 08:33:45 +020040
41static int clk_gate2_enable(struct clk_hw *hw)
42{
Shawn Guo54ee1472014-04-18 15:55:16 +080043 struct clk_gate2 *gate = to_clk_gate2(hw);
Sascha Hauerb75c0152011-04-19 08:33:45 +020044 u32 reg;
45 unsigned long flags = 0;
46
Shawn Guo94b5c022014-04-18 16:07:44 +080047 spin_lock_irqsave(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020048
Shawn Guof9f28cd2014-04-19 10:58:22 +080049 if (gate->share_count && (*gate->share_count)++ > 0)
50 goto out;
51
Sascha Hauerb75c0152011-04-19 08:33:45 +020052 reg = readl(gate->reg);
53 reg |= 3 << gate->bit_idx;
54 writel(reg, gate->reg);
55
Shawn Guof9f28cd2014-04-19 10:58:22 +080056out:
Shawn Guo94b5c022014-04-18 16:07:44 +080057 spin_unlock_irqrestore(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020058
59 return 0;
60}
61
62static void clk_gate2_disable(struct clk_hw *hw)
63{
Shawn Guo54ee1472014-04-18 15:55:16 +080064 struct clk_gate2 *gate = to_clk_gate2(hw);
Sascha Hauerb75c0152011-04-19 08:33:45 +020065 u32 reg;
66 unsigned long flags = 0;
67
Shawn Guo94b5c022014-04-18 16:07:44 +080068 spin_lock_irqsave(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020069
Shawn Guo63288b72014-07-07 10:53:51 +080070 if (gate->share_count) {
71 if (WARN_ON(*gate->share_count == 0))
72 goto out;
73 else if (--(*gate->share_count) > 0)
74 goto out;
75 }
Shawn Guof9f28cd2014-04-19 10:58:22 +080076
Sascha Hauerb75c0152011-04-19 08:33:45 +020077 reg = readl(gate->reg);
78 reg &= ~(3 << gate->bit_idx);
79 writel(reg, gate->reg);
80
Shawn Guof9f28cd2014-04-19 10:58:22 +080081out:
Shawn Guo94b5c022014-04-18 16:07:44 +080082 spin_unlock_irqrestore(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020083}
84
Shawn Guo63288b72014-07-07 10:53:51 +080085static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx)
Sascha Hauerb75c0152011-04-19 08:33:45 +020086{
Shawn Guo63288b72014-07-07 10:53:51 +080087 u32 val = readl(reg);
Sascha Hauerb75c0152011-04-19 08:33:45 +020088
Shawn Guo63288b72014-07-07 10:53:51 +080089 if (((val >> bit_idx) & 1) == 1)
Sascha Hauerb75c0152011-04-19 08:33:45 +020090 return 1;
91
92 return 0;
93}
94
Shawn Guo63288b72014-07-07 10:53:51 +080095static int clk_gate2_is_enabled(struct clk_hw *hw)
96{
97 struct clk_gate2 *gate = to_clk_gate2(hw);
98
99 if (gate->share_count)
Shawn Guo9e1ac462014-09-16 09:35:33 +0800100 return !!__clk_get_enable_count(hw->clk);
Shawn Guo63288b72014-07-07 10:53:51 +0800101 else
102 return clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx);
103}
104
Sascha Hauerb75c0152011-04-19 08:33:45 +0200105static struct clk_ops clk_gate2_ops = {
106 .enable = clk_gate2_enable,
107 .disable = clk_gate2_disable,
108 .is_enabled = clk_gate2_is_enabled,
109};
110
111struct clk *clk_register_gate2(struct device *dev, const char *name,
112 const char *parent_name, unsigned long flags,
113 void __iomem *reg, u8 bit_idx,
Shawn Guof9f28cd2014-04-19 10:58:22 +0800114 u8 clk_gate2_flags, spinlock_t *lock,
115 unsigned int *share_count)
Sascha Hauerb75c0152011-04-19 08:33:45 +0200116{
Shawn Guo54ee1472014-04-18 15:55:16 +0800117 struct clk_gate2 *gate;
Sascha Hauerb75c0152011-04-19 08:33:45 +0200118 struct clk *clk;
119 struct clk_init_data init;
120
Shawn Guo54ee1472014-04-18 15:55:16 +0800121 gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
Sascha Hauerb75c0152011-04-19 08:33:45 +0200122 if (!gate)
123 return ERR_PTR(-ENOMEM);
124
Shawn Guo54ee1472014-04-18 15:55:16 +0800125 /* struct clk_gate2 assignments */
Sascha Hauerb75c0152011-04-19 08:33:45 +0200126 gate->reg = reg;
127 gate->bit_idx = bit_idx;
128 gate->flags = clk_gate2_flags;
129 gate->lock = lock;
Shawn Guof9f28cd2014-04-19 10:58:22 +0800130 gate->share_count = share_count;
Sascha Hauerb75c0152011-04-19 08:33:45 +0200131
132 init.name = name;
133 init.ops = &clk_gate2_ops;
134 init.flags = flags;
135 init.parent_names = parent_name ? &parent_name : NULL;
136 init.num_parents = parent_name ? 1 : 0;
137
138 gate->hw.init = &init;
139
140 clk = clk_register(dev, &gate->hw);
141 if (IS_ERR(clk))
Wei Yongjunecf026d2012-10-25 23:02:18 +0800142 kfree(gate);
Sascha Hauerb75c0152011-04-19 08:33:45 +0200143
144 return clk;
145}