Merge branch 'pm-cpufreq'
[linux-2.6-block.git] / drivers / clk / versatile / clk-icst.c
CommitLineData
91b87a47
LW
1/*
2 * Driver for the ICST307 VCO clock found in the ARM Reference designs.
3 * We wrap the custom interface from <asm/hardware/icst.h> into the generic
4 * clock framework.
5 *
401301cc
LW
6 * Copyright (C) 2012 Linus Walleij
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
91b87a47
LW
12 * TODO: when all ARM reference designs are migrated to generic clocks, the
13 * ICST clock code from the ARM tree should probably be merged into this
14 * file.
15 */
6d31e3b2
SB
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/export.h>
91b87a47
LW
19#include <linux/err.h>
20#include <linux/clk-provider.h>
7a9ad671 21#include <linux/io.h>
91b87a47
LW
22
23#include "clk-icst.h"
24
25/**
26 * struct clk_icst - ICST VCO clock wrapper
27 * @hw: corresponding clock hardware entry
7a9ad671
LW
28 * @vcoreg: VCO register address
29 * @lockreg: VCO lock register address
91b87a47
LW
30 * @params: parameters for this ICST instance
31 * @rate: current rate
91b87a47
LW
32 */
33struct clk_icst {
34 struct clk_hw hw;
7a9ad671
LW
35 void __iomem *vcoreg;
36 void __iomem *lockreg;
a183da63 37 struct icst_params *params;
91b87a47 38 unsigned long rate;
91b87a47
LW
39};
40
41#define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
42
7a9ad671
LW
43/**
44 * vco_get() - get ICST VCO settings from a certain register
45 * @vcoreg: register containing the VCO settings
46 */
47static struct icst_vco vco_get(void __iomem *vcoreg)
48{
49 u32 val;
50 struct icst_vco vco;
51
52 val = readl(vcoreg);
53 vco.v = val & 0x1ff;
54 vco.r = (val >> 9) & 0x7f;
55 vco.s = (val >> 16) & 03;
56 return vco;
57}
58
59/**
60 * vco_set() - commit changes to an ICST VCO
61 * @locreg: register to poke to unlock the VCO for writing
62 * @vcoreg: register containing the VCO settings
63 * @vco: ICST VCO parameters to commit
64 */
65static void vco_set(void __iomem *lockreg,
66 void __iomem *vcoreg,
67 struct icst_vco vco)
68{
69 u32 val;
70
71 val = readl(vcoreg) & ~0x7ffff;
72 val |= vco.v | (vco.r << 9) | (vco.s << 16);
73
74 /* This magic unlocks the VCO so it can be controlled */
75 writel(0xa05f, lockreg);
76 writel(val, vcoreg);
77 /* This locks the VCO again */
78 writel(0, lockreg);
79}
80
81
91b87a47
LW
82static unsigned long icst_recalc_rate(struct clk_hw *hw,
83 unsigned long parent_rate)
84{
85 struct clk_icst *icst = to_icst(hw);
86 struct icst_vco vco;
87
a183da63
LW
88 if (parent_rate)
89 icst->params->ref = parent_rate;
7a9ad671 90 vco = vco_get(icst->vcoreg);
91b87a47
LW
91 icst->rate = icst_hz(icst->params, vco);
92 return icst->rate;
93}
94
95static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
96 unsigned long *prate)
97{
98 struct clk_icst *icst = to_icst(hw);
99 struct icst_vco vco;
100
101 vco = icst_hz_to_vco(icst->params, rate);
102 return icst_hz(icst->params, vco);
103}
104
105static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
106 unsigned long parent_rate)
107{
108 struct clk_icst *icst = to_icst(hw);
109 struct icst_vco vco;
110
a183da63
LW
111 if (parent_rate)
112 icst->params->ref = parent_rate;
91b87a47
LW
113 vco = icst_hz_to_vco(icst->params, rate);
114 icst->rate = icst_hz(icst->params, vco);
2f9f64bc 115 vco_set(icst->lockreg, icst->vcoreg, vco);
91b87a47
LW
116 return 0;
117}
118
119static const struct clk_ops icst_ops = {
120 .recalc_rate = icst_recalc_rate,
121 .round_rate = icst_round_rate,
122 .set_rate = icst_set_rate,
123};
124
7a9ad671
LW
125struct clk *icst_clk_register(struct device *dev,
126 const struct clk_icst_desc *desc,
ae6e694e 127 const char *name,
bf6edb4b 128 const char *parent_name,
7a9ad671 129 void __iomem *base)
91b87a47
LW
130{
131 struct clk *clk;
132 struct clk_icst *icst;
133 struct clk_init_data init;
a183da63 134 struct icst_params *pclone;
91b87a47
LW
135
136 icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL);
137 if (!icst) {
138 pr_err("could not allocate ICST clock!\n");
139 return ERR_PTR(-ENOMEM);
140 }
a183da63
LW
141
142 pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL);
143 if (!pclone) {
ab7ad353 144 kfree(icst);
a183da63
LW
145 pr_err("could not clone ICST params\n");
146 return ERR_PTR(-ENOMEM);
147 }
148
ae6e694e 149 init.name = name;
91b87a47
LW
150 init.ops = &icst_ops;
151 init.flags = CLK_IS_ROOT;
a183da63
LW
152 init.parent_names = (parent_name ? &parent_name : NULL);
153 init.num_parents = (parent_name ? 1 : 0);
91b87a47 154 icst->hw.init = &init;
a183da63 155 icst->params = pclone;
7a9ad671
LW
156 icst->vcoreg = base + desc->vco_offset;
157 icst->lockreg = base + desc->lock_offset;
91b87a47
LW
158
159 clk = clk_register(dev, &icst->hw);
160 if (IS_ERR(clk))
161 kfree(icst);
162
163 return clk;
164}
a218d7fa 165EXPORT_SYMBOL_GPL(icst_clk_register);