blob: a3893ea2199dafa5412207711a1d33fca04c287f [file] [log] [blame]
Linus Walleij91b87a42012-06-11 17:29:54 +02001/*
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 *
Linus Walleij401301c2012-11-20 22:39:31 +01006 * 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 *
Linus Walleij91b87a42012-06-11 17:29:54 +020012 * 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 */
Stephen Boyd6d31e3b22015-06-19 15:00:46 -070016#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/export.h>
Linus Walleij91b87a42012-06-11 17:29:54 +020019#include <linux/err.h>
20#include <linux/clk-provider.h>
Linus Walleij7a9ad672012-11-20 23:01:04 +010021#include <linux/io.h>
Linus Walleij91b87a42012-06-11 17:29:54 +020022
23#include "clk-icst.h"
24
25/**
26 * struct clk_icst - ICST VCO clock wrapper
27 * @hw: corresponding clock hardware entry
Linus Walleij7a9ad672012-11-20 23:01:04 +010028 * @vcoreg: VCO register address
29 * @lockreg: VCO lock register address
Linus Walleij91b87a42012-06-11 17:29:54 +020030 * @params: parameters for this ICST instance
31 * @rate: current rate
Linus Walleij91b87a42012-06-11 17:29:54 +020032 */
33struct clk_icst {
34 struct clk_hw hw;
Linus Walleij7a9ad672012-11-20 23:01:04 +010035 void __iomem *vcoreg;
36 void __iomem *lockreg;
Linus Walleija183da62014-01-20 21:46:46 +010037 struct icst_params *params;
Linus Walleij91b87a42012-06-11 17:29:54 +020038 unsigned long rate;
Linus Walleij91b87a42012-06-11 17:29:54 +020039};
40
41#define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
42
Linus Walleij7a9ad672012-11-20 23:01:04 +010043/**
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
Linus Walleij91b87a42012-06-11 17:29:54 +020082static 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
Linus Walleija183da62014-01-20 21:46:46 +010088 if (parent_rate)
89 icst->params->ref = parent_rate;
Linus Walleij7a9ad672012-11-20 23:01:04 +010090 vco = vco_get(icst->vcoreg);
Linus Walleij91b87a42012-06-11 17:29:54 +020091 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
Linus Walleija183da62014-01-20 21:46:46 +0100111 if (parent_rate)
112 icst->params->ref = parent_rate;
Linus Walleij91b87a42012-06-11 17:29:54 +0200113 vco = icst_hz_to_vco(icst->params, rate);
114 icst->rate = icst_hz(icst->params, vco);
Jonathan Austin2f9f64b2013-07-23 16:42:18 +0100115 vco_set(icst->lockreg, icst->vcoreg, vco);
Linus Walleij91b87a42012-06-11 17:29:54 +0200116 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
Linus Walleij7a9ad672012-11-20 23:01:04 +0100125struct clk *icst_clk_register(struct device *dev,
126 const struct clk_icst_desc *desc,
Linus Walleijae6e6942013-11-22 11:30:05 +0100127 const char *name,
Linus Walleijbf6edb42014-01-20 21:31:41 +0100128 const char *parent_name,
Linus Walleij7a9ad672012-11-20 23:01:04 +0100129 void __iomem *base)
Linus Walleij91b87a42012-06-11 17:29:54 +0200130{
131 struct clk *clk;
132 struct clk_icst *icst;
133 struct clk_init_data init;
Linus Walleija183da62014-01-20 21:46:46 +0100134 struct icst_params *pclone;
Linus Walleij91b87a42012-06-11 17:29:54 +0200135
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 }
Linus Walleija183da62014-01-20 21:46:46 +0100141
142 pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL);
143 if (!pclone) {
Colin Ian Kingab7ad352014-04-12 18:59:14 +0100144 kfree(icst);
Linus Walleija183da62014-01-20 21:46:46 +0100145 pr_err("could not clone ICST params\n");
146 return ERR_PTR(-ENOMEM);
147 }
148
Linus Walleijae6e6942013-11-22 11:30:05 +0100149 init.name = name;
Linus Walleij91b87a42012-06-11 17:29:54 +0200150 init.ops = &icst_ops;
151 init.flags = CLK_IS_ROOT;
Linus Walleija183da62014-01-20 21:46:46 +0100152 init.parent_names = (parent_name ? &parent_name : NULL);
153 init.num_parents = (parent_name ? 1 : 0);
Linus Walleij91b87a42012-06-11 17:29:54 +0200154 icst->hw.init = &init;
Linus Walleija183da62014-01-20 21:46:46 +0100155 icst->params = pclone;
Linus Walleij7a9ad672012-11-20 23:01:04 +0100156 icst->vcoreg = base + desc->vco_offset;
157 icst->lockreg = base + desc->lock_offset;
Linus Walleij91b87a42012-06-11 17:29:54 +0200158
159 clk = clk_register(dev, &icst->hw);
160 if (IS_ERR(clk))
161 kfree(icst);
162
163 return clk;
164}
Arnd Bergmanna218d7f2014-05-08 16:56:16 +0200165EXPORT_SYMBOL_GPL(icst_clk_register);