blob: bc96f103bd7caac9c95a977bb3c8559d638bbf9b [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 */
16#include <linux/clk.h>
17#include <linux/clkdev.h>
18#include <linux/err.h>
19#include <linux/clk-provider.h>
Linus Walleij7a9ad672012-11-20 23:01:04 +010020#include <linux/io.h>
Linus Walleij91b87a42012-06-11 17:29:54 +020021
22#include "clk-icst.h"
23
24/**
25 * struct clk_icst - ICST VCO clock wrapper
26 * @hw: corresponding clock hardware entry
Linus Walleij7a9ad672012-11-20 23:01:04 +010027 * @vcoreg: VCO register address
28 * @lockreg: VCO lock register address
Linus Walleij91b87a42012-06-11 17:29:54 +020029 * @params: parameters for this ICST instance
30 * @rate: current rate
Linus Walleij91b87a42012-06-11 17:29:54 +020031 */
32struct clk_icst {
33 struct clk_hw hw;
Linus Walleij7a9ad672012-11-20 23:01:04 +010034 void __iomem *vcoreg;
35 void __iomem *lockreg;
Linus Walleija183da62014-01-20 21:46:46 +010036 struct icst_params *params;
Linus Walleij91b87a42012-06-11 17:29:54 +020037 unsigned long rate;
Linus Walleij91b87a42012-06-11 17:29:54 +020038};
39
40#define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
41
Linus Walleij7a9ad672012-11-20 23:01:04 +010042/**
43 * vco_get() - get ICST VCO settings from a certain register
44 * @vcoreg: register containing the VCO settings
45 */
46static struct icst_vco vco_get(void __iomem *vcoreg)
47{
48 u32 val;
49 struct icst_vco vco;
50
51 val = readl(vcoreg);
52 vco.v = val & 0x1ff;
53 vco.r = (val >> 9) & 0x7f;
54 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 */
64static void vco_set(void __iomem *lockreg,
65 void __iomem *vcoreg,
66 struct icst_vco vco)
67{
68 u32 val;
69
70 val = readl(vcoreg) & ~0x7ffff;
71 val |= vco.v | (vco.r << 9) | (vco.s << 16);
72
73 /* This magic unlocks the VCO so it can be controlled */
74 writel(0xa05f, lockreg);
75 writel(val, vcoreg);
76 /* This locks the VCO again */
77 writel(0, lockreg);
78}
79
80
Linus Walleij91b87a42012-06-11 17:29:54 +020081static 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
Linus Walleija183da62014-01-20 21:46:46 +010087 if (parent_rate)
88 icst->params->ref = parent_rate;
Linus Walleij7a9ad672012-11-20 23:01:04 +010089 vco = vco_get(icst->vcoreg);
Linus Walleij91b87a42012-06-11 17:29:54 +020090 icst->rate = icst_hz(icst->params, vco);
91 return icst->rate;
92}
93
94static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
95 unsigned long *prate)
96{
97 struct clk_icst *icst = to_icst(hw);
98 struct icst_vco vco;
99
100 vco = icst_hz_to_vco(icst->params, rate);
101 return icst_hz(icst->params, vco);
102}
103
104static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
105 unsigned long parent_rate)
106{
107 struct clk_icst *icst = to_icst(hw);
108 struct icst_vco vco;
109
Linus Walleija183da62014-01-20 21:46:46 +0100110 if (parent_rate)
111 icst->params->ref = parent_rate;
Linus Walleij91b87a42012-06-11 17:29:54 +0200112 vco = icst_hz_to_vco(icst->params, rate);
113 icst->rate = icst_hz(icst->params, vco);
Jonathan Austin2f9f64b2013-07-23 16:42:18 +0100114 vco_set(icst->lockreg, icst->vcoreg, vco);
Linus Walleij91b87a42012-06-11 17:29:54 +0200115 return 0;
116}
117
118static const struct clk_ops icst_ops = {
119 .recalc_rate = icst_recalc_rate,
120 .round_rate = icst_round_rate,
121 .set_rate = icst_set_rate,
122};
123
Linus Walleij7a9ad672012-11-20 23:01:04 +0100124struct clk *icst_clk_register(struct device *dev,
125 const struct clk_icst_desc *desc,
Linus Walleijae6e6942013-11-22 11:30:05 +0100126 const char *name,
Linus Walleijbf6edb42014-01-20 21:31:41 +0100127 const char *parent_name,
Linus Walleij7a9ad672012-11-20 23:01:04 +0100128 void __iomem *base)
Linus Walleij91b87a42012-06-11 17:29:54 +0200129{
130 struct clk *clk;
131 struct clk_icst *icst;
132 struct clk_init_data init;
Linus Walleija183da62014-01-20 21:46:46 +0100133 struct icst_params *pclone;
Linus Walleij91b87a42012-06-11 17:29:54 +0200134
135 icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL);
136 if (!icst) {
137 pr_err("could not allocate ICST clock!\n");
138 return ERR_PTR(-ENOMEM);
139 }
Linus Walleija183da62014-01-20 21:46:46 +0100140
141 pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL);
142 if (!pclone) {
Colin Ian Kingab7ad352014-04-12 18:59:14 +0100143 kfree(icst);
Linus Walleija183da62014-01-20 21:46:46 +0100144 pr_err("could not clone ICST params\n");
145 return ERR_PTR(-ENOMEM);
146 }
147
Linus Walleijae6e6942013-11-22 11:30:05 +0100148 init.name = name;
Linus Walleij91b87a42012-06-11 17:29:54 +0200149 init.ops = &icst_ops;
150 init.flags = CLK_IS_ROOT;
Linus Walleija183da62014-01-20 21:46:46 +0100151 init.parent_names = (parent_name ? &parent_name : NULL);
152 init.num_parents = (parent_name ? 1 : 0);
Linus Walleij91b87a42012-06-11 17:29:54 +0200153 icst->hw.init = &init;
Linus Walleija183da62014-01-20 21:46:46 +0100154 icst->params = pclone;
Linus Walleij7a9ad672012-11-20 23:01:04 +0100155 icst->vcoreg = base + desc->vco_offset;
156 icst->lockreg = base + desc->lock_offset;
Linus Walleij91b87a42012-06-11 17:29:54 +0200157
158 clk = clk_register(dev, &icst->hw);
159 if (IS_ERR(clk))
160 kfree(icst);
161
162 return clk;
163}
Arnd Bergmanna218d7f2014-05-08 16:56:16 +0200164EXPORT_SYMBOL_GPL(icst_clk_register);