Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 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 | * |
Linus Walleij | 401301c | 2012-11-20 22:39:31 +0100 | [diff] [blame] | 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 | * |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 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 | */ |
Stephen Boyd | 6d31e3b2 | 2015-06-19 15:00:46 -0700 | [diff] [blame^] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/export.h> |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/clk-provider.h> |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 21 | #include <linux/io.h> |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 22 | |
| 23 | #include "clk-icst.h" |
| 24 | |
| 25 | /** |
| 26 | * struct clk_icst - ICST VCO clock wrapper |
| 27 | * @hw: corresponding clock hardware entry |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 28 | * @vcoreg: VCO register address |
| 29 | * @lockreg: VCO lock register address |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 30 | * @params: parameters for this ICST instance |
| 31 | * @rate: current rate |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 32 | */ |
| 33 | struct clk_icst { |
| 34 | struct clk_hw hw; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 35 | void __iomem *vcoreg; |
| 36 | void __iomem *lockreg; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 37 | struct icst_params *params; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 38 | unsigned long rate; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #define to_icst(_hw) container_of(_hw, struct clk_icst, hw) |
| 42 | |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 43 | /** |
| 44 | * vco_get() - get ICST VCO settings from a certain register |
| 45 | * @vcoreg: register containing the VCO settings |
| 46 | */ |
| 47 | static 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 | */ |
| 65 | static 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 Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 82 | static 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 Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 88 | if (parent_rate) |
| 89 | icst->params->ref = parent_rate; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 90 | vco = vco_get(icst->vcoreg); |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 91 | icst->rate = icst_hz(icst->params, vco); |
| 92 | return icst->rate; |
| 93 | } |
| 94 | |
| 95 | static 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 | |
| 105 | static 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 Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 111 | if (parent_rate) |
| 112 | icst->params->ref = parent_rate; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 113 | vco = icst_hz_to_vco(icst->params, rate); |
| 114 | icst->rate = icst_hz(icst->params, vco); |
Jonathan Austin | 2f9f64b | 2013-07-23 16:42:18 +0100 | [diff] [blame] | 115 | vco_set(icst->lockreg, icst->vcoreg, vco); |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static 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 Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 125 | struct clk *icst_clk_register(struct device *dev, |
| 126 | const struct clk_icst_desc *desc, |
Linus Walleij | ae6e694 | 2013-11-22 11:30:05 +0100 | [diff] [blame] | 127 | const char *name, |
Linus Walleij | bf6edb4 | 2014-01-20 21:31:41 +0100 | [diff] [blame] | 128 | const char *parent_name, |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 129 | void __iomem *base) |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 130 | { |
| 131 | struct clk *clk; |
| 132 | struct clk_icst *icst; |
| 133 | struct clk_init_data init; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 134 | struct icst_params *pclone; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 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 | } |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 141 | |
| 142 | pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL); |
| 143 | if (!pclone) { |
Colin Ian King | ab7ad35 | 2014-04-12 18:59:14 +0100 | [diff] [blame] | 144 | kfree(icst); |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 145 | pr_err("could not clone ICST params\n"); |
| 146 | return ERR_PTR(-ENOMEM); |
| 147 | } |
| 148 | |
Linus Walleij | ae6e694 | 2013-11-22 11:30:05 +0100 | [diff] [blame] | 149 | init.name = name; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 150 | init.ops = &icst_ops; |
| 151 | init.flags = CLK_IS_ROOT; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 152 | init.parent_names = (parent_name ? &parent_name : NULL); |
| 153 | init.num_parents = (parent_name ? 1 : 0); |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 154 | icst->hw.init = &init; |
Linus Walleij | a183da6 | 2014-01-20 21:46:46 +0100 | [diff] [blame] | 155 | icst->params = pclone; |
Linus Walleij | 7a9ad67 | 2012-11-20 23:01:04 +0100 | [diff] [blame] | 156 | icst->vcoreg = base + desc->vco_offset; |
| 157 | icst->lockreg = base + desc->lock_offset; |
Linus Walleij | 91b87a4 | 2012-06-11 17:29:54 +0200 | [diff] [blame] | 158 | |
| 159 | clk = clk_register(dev, &icst->hw); |
| 160 | if (IS_ERR(clk)) |
| 161 | kfree(icst); |
| 162 | |
| 163 | return clk; |
| 164 | } |
Arnd Bergmann | a218d7f | 2014-05-08 16:56:16 +0200 | [diff] [blame] | 165 | EXPORT_SYMBOL_GPL(icst_clk_register); |