blob: 0803df9d9c7e1846c88462aaa0219ef401cd22b8 [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;
36};
37
38#define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
Sascha Hauerb75c0152011-04-19 08:33:45 +020039
40static int clk_gate2_enable(struct clk_hw *hw)
41{
Shawn Guo54ee1472014-04-18 15:55:16 +080042 struct clk_gate2 *gate = to_clk_gate2(hw);
Sascha Hauerb75c0152011-04-19 08:33:45 +020043 u32 reg;
44 unsigned long flags = 0;
45
Shawn Guo94b5c022014-04-18 16:07:44 +080046 spin_lock_irqsave(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020047
48 reg = readl(gate->reg);
49 reg |= 3 << gate->bit_idx;
50 writel(reg, gate->reg);
51
Shawn Guo94b5c022014-04-18 16:07:44 +080052 spin_unlock_irqrestore(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020053
54 return 0;
55}
56
57static void clk_gate2_disable(struct clk_hw *hw)
58{
Shawn Guo54ee1472014-04-18 15:55:16 +080059 struct clk_gate2 *gate = to_clk_gate2(hw);
Sascha Hauerb75c0152011-04-19 08:33:45 +020060 u32 reg;
61 unsigned long flags = 0;
62
Shawn Guo94b5c022014-04-18 16:07:44 +080063 spin_lock_irqsave(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020064
65 reg = readl(gate->reg);
66 reg &= ~(3 << gate->bit_idx);
67 writel(reg, gate->reg);
68
Shawn Guo94b5c022014-04-18 16:07:44 +080069 spin_unlock_irqrestore(gate->lock, flags);
Sascha Hauerb75c0152011-04-19 08:33:45 +020070}
71
72static int clk_gate2_is_enabled(struct clk_hw *hw)
73{
74 u32 reg;
Shawn Guo54ee1472014-04-18 15:55:16 +080075 struct clk_gate2 *gate = to_clk_gate2(hw);
Sascha Hauerb75c0152011-04-19 08:33:45 +020076
77 reg = readl(gate->reg);
78
Anson Huangb4e844f2013-12-24 14:21:27 -050079 if (((reg >> gate->bit_idx) & 1) == 1)
Sascha Hauerb75c0152011-04-19 08:33:45 +020080 return 1;
81
82 return 0;
83}
84
85static struct clk_ops clk_gate2_ops = {
86 .enable = clk_gate2_enable,
87 .disable = clk_gate2_disable,
88 .is_enabled = clk_gate2_is_enabled,
89};
90
91struct clk *clk_register_gate2(struct device *dev, const char *name,
92 const char *parent_name, unsigned long flags,
93 void __iomem *reg, u8 bit_idx,
94 u8 clk_gate2_flags, spinlock_t *lock)
95{
Shawn Guo54ee1472014-04-18 15:55:16 +080096 struct clk_gate2 *gate;
Sascha Hauerb75c0152011-04-19 08:33:45 +020097 struct clk *clk;
98 struct clk_init_data init;
99
Shawn Guo54ee1472014-04-18 15:55:16 +0800100 gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
Sascha Hauerb75c0152011-04-19 08:33:45 +0200101 if (!gate)
102 return ERR_PTR(-ENOMEM);
103
Shawn Guo54ee1472014-04-18 15:55:16 +0800104 /* struct clk_gate2 assignments */
Sascha Hauerb75c0152011-04-19 08:33:45 +0200105 gate->reg = reg;
106 gate->bit_idx = bit_idx;
107 gate->flags = clk_gate2_flags;
108 gate->lock = lock;
109
110 init.name = name;
111 init.ops = &clk_gate2_ops;
112 init.flags = flags;
113 init.parent_names = parent_name ? &parent_name : NULL;
114 init.num_parents = parent_name ? 1 : 0;
115
116 gate->hw.init = &init;
117
118 clk = clk_register(dev, &gate->hw);
119 if (IS_ERR(clk))
Wei Yongjunecf026d2012-10-25 23:02:18 +0800120 kfree(gate);
Sascha Hauerb75c0152011-04-19 08:33:45 +0200121
122 return clk;
123}