Branch data Line data Source code
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 : : *
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 : : *
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 : : */
16 : : #include <linux/clk.h>
17 : : #include <linux/clkdev.h>
18 : : #include <linux/err.h>
19 : : #include <linux/clk-provider.h>
20 : : #include <linux/io.h>
21 : :
22 : : #include "clk-icst.h"
23 : :
24 : : /**
25 : : * struct clk_icst - ICST VCO clock wrapper
26 : : * @hw: corresponding clock hardware entry
27 : : * @vcoreg: VCO register address
28 : : * @lockreg: VCO lock register address
29 : : * @params: parameters for this ICST instance
30 : : * @rate: current rate
31 : : */
32 : : struct clk_icst {
33 : : struct clk_hw hw;
34 : : void __iomem *vcoreg;
35 : : void __iomem *lockreg;
36 : : const struct icst_params *params;
37 : : unsigned long rate;
38 : : };
39 : :
40 : : #define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
41 : :
42 : : /**
43 : : * vco_get() - get ICST VCO settings from a certain register
44 : : * @vcoreg: register containing the VCO settings
45 : : */
46 : : static struct icst_vco vco_get(void __iomem *vcoreg)
47 : : {
48 : : u32 val;
49 : : struct icst_vco vco;
50 : :
51 : 0 : val = readl(vcoreg);
52 : 0 : vco.v = val & 0x1ff;
53 : 0 : vco.r = (val >> 9) & 0x7f;
54 : 0 : vco.s = (val >> 16) & 03;
55 : : return vco;
56 : : }
57 : :
58 : : /**
59 : : * vco_set() - commit changes to an ICST VCO
60 : : * @locreg: register to poke to unlock the VCO for writing
61 : : * @vcoreg: register containing the VCO settings
62 : : * @vco: ICST VCO parameters to commit
63 : : */
64 : 0 : static void vco_set(void __iomem *lockreg,
65 : : void __iomem *vcoreg,
66 : : struct icst_vco vco)
67 : : {
68 : : u32 val;
69 : :
70 : 0 : val = readl(vcoreg) & ~0x7ffff;
71 : 0 : val |= vco.v | (vco.r << 9) | (vco.s << 16);
72 : :
73 : : /* This magic unlocks the VCO so it can be controlled */
74 : 0 : writel(0xa05f, lockreg);
75 : 0 : writel(val, vcoreg);
76 : : /* This locks the VCO again */
77 : 0 : writel(0, lockreg);
78 : 0 : }
79 : :
80 : :
81 : 0 : static unsigned long icst_recalc_rate(struct clk_hw *hw,
82 : : unsigned long parent_rate)
83 : : {
84 : : struct clk_icst *icst = to_icst(hw);
85 : : struct icst_vco vco;
86 : :
87 : 0 : vco = vco_get(icst->vcoreg);
88 : 0 : icst->rate = icst_hz(icst->params, vco);
89 : 0 : return icst->rate;
90 : : }
91 : :
92 : 0 : static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
93 : : unsigned long *prate)
94 : : {
95 : : struct clk_icst *icst = to_icst(hw);
96 : : struct icst_vco vco;
97 : :
98 : 0 : vco = icst_hz_to_vco(icst->params, rate);
99 : 0 : return icst_hz(icst->params, vco);
100 : : }
101 : :
102 : 0 : static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
103 : : unsigned long parent_rate)
104 : : {
105 : : struct clk_icst *icst = to_icst(hw);
106 : : struct icst_vco vco;
107 : :
108 : 0 : vco = icst_hz_to_vco(icst->params, rate);
109 : 0 : icst->rate = icst_hz(icst->params, vco);
110 : 0 : vco_set(icst->lockreg, icst->vcoreg, vco);
111 : 0 : return 0;
112 : : }
113 : :
114 : : static const struct clk_ops icst_ops = {
115 : : .recalc_rate = icst_recalc_rate,
116 : : .round_rate = icst_round_rate,
117 : : .set_rate = icst_set_rate,
118 : : };
119 : :
120 : 0 : struct clk *icst_clk_register(struct device *dev,
121 : : const struct clk_icst_desc *desc,
122 : : const char *name,
123 : : void __iomem *base)
124 : : {
125 : : struct clk *clk;
126 : : struct clk_icst *icst;
127 : : struct clk_init_data init;
128 : :
129 : : icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL);
130 [ # # ]: 0 : if (!icst) {
131 : 0 : pr_err("could not allocate ICST clock!\n");
132 : 0 : return ERR_PTR(-ENOMEM);
133 : : }
134 : 0 : init.name = name;
135 : 0 : init.ops = &icst_ops;
136 : 0 : init.flags = CLK_IS_ROOT;
137 : 0 : init.parent_names = NULL;
138 : 0 : init.num_parents = 0;
139 : 0 : icst->hw.init = &init;
140 : 0 : icst->params = desc->params;
141 : 0 : icst->vcoreg = base + desc->vco_offset;
142 : 0 : icst->lockreg = base + desc->lock_offset;
143 : :
144 : 0 : clk = clk_register(dev, &icst->hw);
145 [ # # ]: 0 : if (IS_ERR(clk))
146 : 0 : kfree(icst);
147 : :
148 : 0 : return clk;
149 : : }
|