2012-06-11 23:29:54 +08:00
|
|
|
/*
|
|
|
|
* Driver for the ICST307 VCO clock found in the ARM Reference designs.
|
|
|
|
* We wrap the custom interface from <asm/hardware/icst.h> into the generic
|
|
|
|
* clock framework.
|
|
|
|
*
|
2015-10-13 20:29:54 +08:00
|
|
|
* Copyright (C) 2012-2015 Linus Walleij
|
2012-11-21 05:39:31 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
2012-06-11 23:29:54 +08:00
|
|
|
* TODO: when all ARM reference designs are migrated to generic clocks, the
|
|
|
|
* ICST clock code from the ARM tree should probably be merged into this
|
|
|
|
* file.
|
|
|
|
*/
|
2015-06-20 06:00:46 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/export.h>
|
2012-06-11 23:29:54 +08:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/clk-provider.h>
|
2012-11-21 06:01:04 +08:00
|
|
|
#include <linux/io.h>
|
2015-10-12 21:52:50 +08:00
|
|
|
#include <linux/regmap.h>
|
2015-10-12 22:14:28 +08:00
|
|
|
#include <linux/mfd/syscon.h>
|
2012-06-11 23:29:54 +08:00
|
|
|
|
|
|
|
#include "clk-icst.h"
|
|
|
|
|
2015-10-12 21:52:50 +08:00
|
|
|
/* Magic unlocking token used on all Versatile boards */
|
|
|
|
#define VERSATILE_LOCK_VAL 0xA05F
|
|
|
|
|
2012-06-11 23:29:54 +08:00
|
|
|
/**
|
|
|
|
* struct clk_icst - ICST VCO clock wrapper
|
|
|
|
* @hw: corresponding clock hardware entry
|
2012-11-21 06:01:04 +08:00
|
|
|
* @vcoreg: VCO register address
|
|
|
|
* @lockreg: VCO lock register address
|
2012-06-11 23:29:54 +08:00
|
|
|
* @params: parameters for this ICST instance
|
|
|
|
* @rate: current rate
|
|
|
|
*/
|
|
|
|
struct clk_icst {
|
|
|
|
struct clk_hw hw;
|
2015-10-12 21:52:50 +08:00
|
|
|
struct regmap *map;
|
|
|
|
u32 vcoreg_off;
|
|
|
|
u32 lockreg_off;
|
2014-01-21 04:46:46 +08:00
|
|
|
struct icst_params *params;
|
2012-06-11 23:29:54 +08:00
|
|
|
unsigned long rate;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define to_icst(_hw) container_of(_hw, struct clk_icst, hw)
|
|
|
|
|
2012-11-21 06:01:04 +08:00
|
|
|
/**
|
2015-10-12 21:52:50 +08:00
|
|
|
* vco_get() - get ICST VCO settings from a certain ICST
|
|
|
|
* @icst: the ICST clock to get
|
|
|
|
* @vco: the VCO struct to return the value in
|
2012-11-21 06:01:04 +08:00
|
|
|
*/
|
2015-10-12 21:52:50 +08:00
|
|
|
static int vco_get(struct clk_icst *icst, struct icst_vco *vco)
|
2012-11-21 06:01:04 +08:00
|
|
|
{
|
|
|
|
u32 val;
|
2015-10-12 21:52:50 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = regmap_read(icst->map, icst->vcoreg_off, &val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
vco->v = val & 0x1ff;
|
|
|
|
vco->r = (val >> 9) & 0x7f;
|
|
|
|
vco->s = (val >> 16) & 03;
|
|
|
|
return 0;
|
2012-11-21 06:01:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vco_set() - commit changes to an ICST VCO
|
2015-10-12 21:52:50 +08:00
|
|
|
* @icst: the ICST clock to set
|
|
|
|
* @vco: the VCO struct to set the changes from
|
2012-11-21 06:01:04 +08:00
|
|
|
*/
|
2015-10-12 21:52:50 +08:00
|
|
|
static int vco_set(struct clk_icst *icst, struct icst_vco vco)
|
2012-11-21 06:01:04 +08:00
|
|
|
{
|
|
|
|
u32 val;
|
2015-10-12 21:52:50 +08:00
|
|
|
int ret;
|
2012-11-21 06:01:04 +08:00
|
|
|
|
2015-10-12 21:52:50 +08:00
|
|
|
ret = regmap_read(icst->map, icst->vcoreg_off, &val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-02-03 21:47:08 +08:00
|
|
|
|
|
|
|
/* Mask the 18 bits used by the VCO */
|
|
|
|
val &= ~0x7ffff;
|
2012-11-21 06:01:04 +08:00
|
|
|
val |= vco.v | (vco.r << 9) | (vco.s << 16);
|
|
|
|
|
|
|
|
/* This magic unlocks the VCO so it can be controlled */
|
2015-10-12 21:52:50 +08:00
|
|
|
ret = regmap_write(icst->map, icst->lockreg_off, VERSATILE_LOCK_VAL);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = regmap_write(icst->map, icst->vcoreg_off, val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2012-11-21 06:01:04 +08:00
|
|
|
/* This locks the VCO again */
|
2015-10-12 21:52:50 +08:00
|
|
|
ret = regmap_write(icst->map, icst->lockreg_off, 0);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
return 0;
|
2012-11-21 06:01:04 +08:00
|
|
|
}
|
|
|
|
|
2012-06-11 23:29:54 +08:00
|
|
|
static unsigned long icst_recalc_rate(struct clk_hw *hw,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_icst *icst = to_icst(hw);
|
|
|
|
struct icst_vco vco;
|
2015-10-12 21:52:50 +08:00
|
|
|
int ret;
|
2012-06-11 23:29:54 +08:00
|
|
|
|
2014-01-21 04:46:46 +08:00
|
|
|
if (parent_rate)
|
|
|
|
icst->params->ref = parent_rate;
|
2015-10-12 21:52:50 +08:00
|
|
|
ret = vco_get(icst, &vco);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("ICST: could not get VCO setting\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-11 23:29:54 +08:00
|
|
|
icst->rate = icst_hz(icst->params, vco);
|
|
|
|
return icst->rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long icst_round_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long *prate)
|
|
|
|
{
|
|
|
|
struct clk_icst *icst = to_icst(hw);
|
|
|
|
struct icst_vco vco;
|
|
|
|
|
|
|
|
vco = icst_hz_to_vco(icst->params, rate);
|
|
|
|
return icst_hz(icst->params, vco);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int icst_set_rate(struct clk_hw *hw, unsigned long rate,
|
|
|
|
unsigned long parent_rate)
|
|
|
|
{
|
|
|
|
struct clk_icst *icst = to_icst(hw);
|
|
|
|
struct icst_vco vco;
|
|
|
|
|
2014-01-21 04:46:46 +08:00
|
|
|
if (parent_rate)
|
|
|
|
icst->params->ref = parent_rate;
|
2012-06-11 23:29:54 +08:00
|
|
|
vco = icst_hz_to_vco(icst->params, rate);
|
|
|
|
icst->rate = icst_hz(icst->params, vco);
|
2015-10-12 21:52:50 +08:00
|
|
|
return vco_set(icst, vco);
|
2012-06-11 23:29:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops icst_ops = {
|
|
|
|
.recalc_rate = icst_recalc_rate,
|
|
|
|
.round_rate = icst_round_rate,
|
|
|
|
.set_rate = icst_set_rate,
|
|
|
|
};
|
|
|
|
|
2015-10-12 22:14:28 +08:00
|
|
|
static struct clk *icst_clk_setup(struct device *dev,
|
|
|
|
const struct clk_icst_desc *desc,
|
|
|
|
const char *name,
|
|
|
|
const char *parent_name,
|
|
|
|
struct regmap *map)
|
2012-06-11 23:29:54 +08:00
|
|
|
{
|
|
|
|
struct clk *clk;
|
|
|
|
struct clk_icst *icst;
|
|
|
|
struct clk_init_data init;
|
2014-01-21 04:46:46 +08:00
|
|
|
struct icst_params *pclone;
|
2012-06-11 23:29:54 +08:00
|
|
|
|
|
|
|
icst = kzalloc(sizeof(struct clk_icst), GFP_KERNEL);
|
|
|
|
if (!icst) {
|
|
|
|
pr_err("could not allocate ICST clock!\n");
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
}
|
2014-01-21 04:46:46 +08:00
|
|
|
|
|
|
|
pclone = kmemdup(desc->params, sizeof(*pclone), GFP_KERNEL);
|
|
|
|
if (!pclone) {
|
2014-04-13 01:59:14 +08:00
|
|
|
kfree(icst);
|
2014-01-21 04:46:46 +08:00
|
|
|
pr_err("could not clone ICST params\n");
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
}
|
|
|
|
|
2013-11-22 18:30:05 +08:00
|
|
|
init.name = name;
|
2012-06-11 23:29:54 +08:00
|
|
|
init.ops = &icst_ops;
|
|
|
|
init.flags = CLK_IS_ROOT;
|
2014-01-21 04:46:46 +08:00
|
|
|
init.parent_names = (parent_name ? &parent_name : NULL);
|
|
|
|
init.num_parents = (parent_name ? 1 : 0);
|
2015-10-12 22:14:28 +08:00
|
|
|
icst->map = map;
|
2012-06-11 23:29:54 +08:00
|
|
|
icst->hw.init = &init;
|
2014-01-21 04:46:46 +08:00
|
|
|
icst->params = pclone;
|
2015-10-12 21:52:50 +08:00
|
|
|
icst->vcoreg_off = desc->vco_offset;
|
|
|
|
icst->lockreg_off = desc->lock_offset;
|
2012-06-11 23:29:54 +08:00
|
|
|
|
|
|
|
clk = clk_register(dev, &icst->hw);
|
2015-10-23 17:36:01 +08:00
|
|
|
if (IS_ERR(clk)) {
|
|
|
|
kfree(pclone);
|
2012-06-11 23:29:54 +08:00
|
|
|
kfree(icst);
|
2015-10-23 17:36:01 +08:00
|
|
|
}
|
2012-06-11 23:29:54 +08:00
|
|
|
|
|
|
|
return clk;
|
|
|
|
}
|
2015-10-12 22:14:28 +08:00
|
|
|
|
|
|
|
struct clk *icst_clk_register(struct device *dev,
|
|
|
|
const struct clk_icst_desc *desc,
|
|
|
|
const char *name,
|
|
|
|
const char *parent_name,
|
|
|
|
void __iomem *base)
|
|
|
|
{
|
|
|
|
struct regmap_config icst_regmap_conf = {
|
|
|
|
.reg_bits = 32,
|
|
|
|
.val_bits = 32,
|
|
|
|
.reg_stride = 4,
|
|
|
|
};
|
|
|
|
struct regmap *map;
|
|
|
|
|
|
|
|
map = regmap_init_mmio(dev, base, &icst_regmap_conf);
|
|
|
|
if (IS_ERR(map)) {
|
|
|
|
pr_err("could not initialize ICST regmap\n");
|
|
|
|
return ERR_CAST(map);
|
|
|
|
}
|
|
|
|
return icst_clk_setup(dev, desc, name, parent_name, map);
|
|
|
|
}
|
2014-05-08 22:56:16 +08:00
|
|
|
EXPORT_SYMBOL_GPL(icst_clk_register);
|
2015-10-13 20:29:54 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_OF
|
|
|
|
/*
|
|
|
|
* In a device tree, an memory-mapped ICST clock appear as a child
|
|
|
|
* of a syscon node. Assume this and probe it only as a child of a
|
|
|
|
* syscon.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const struct icst_params icst525_params = {
|
|
|
|
.vco_max = ICST525_VCO_MAX_5V,
|
|
|
|
.vco_min = ICST525_VCO_MIN,
|
|
|
|
.vd_min = 8,
|
|
|
|
.vd_max = 263,
|
|
|
|
.rd_min = 3,
|
|
|
|
.rd_max = 65,
|
|
|
|
.s2div = icst525_s2div,
|
|
|
|
.idx2s = icst525_idx2s,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct icst_params icst307_params = {
|
|
|
|
.vco_max = ICST307_VCO_MAX,
|
|
|
|
.vco_min = ICST307_VCO_MIN,
|
|
|
|
.vd_min = 4 + 8,
|
|
|
|
.vd_max = 511 + 8,
|
|
|
|
.rd_min = 1 + 2,
|
|
|
|
.rd_max = 127 + 2,
|
|
|
|
.s2div = icst307_s2div,
|
|
|
|
.idx2s = icst307_idx2s,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void __init of_syscon_icst_setup(struct device_node *np)
|
|
|
|
{
|
|
|
|
struct device_node *parent;
|
|
|
|
struct regmap *map;
|
|
|
|
struct clk_icst_desc icst_desc;
|
|
|
|
const char *name = np->name;
|
|
|
|
const char *parent_name;
|
|
|
|
struct clk *regclk;
|
|
|
|
|
|
|
|
/* We do not release this reference, we are using it perpetually */
|
|
|
|
parent = of_get_parent(np);
|
|
|
|
if (!parent) {
|
|
|
|
pr_err("no parent node for syscon ICST clock\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
map = syscon_node_to_regmap(parent);
|
|
|
|
if (IS_ERR(map)) {
|
|
|
|
pr_err("no regmap for syscon ICST clock parent\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (of_property_read_u32(np, "vco-offset", &icst_desc.vco_offset)) {
|
|
|
|
pr_err("no VCO register offset for ICST clock\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (of_property_read_u32(np, "lock-offset", &icst_desc.lock_offset)) {
|
|
|
|
pr_err("no lock register offset for ICST clock\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (of_device_is_compatible(np, "arm,syscon-icst525"))
|
|
|
|
icst_desc.params = &icst525_params;
|
|
|
|
else if (of_device_is_compatible(np, "arm,syscon-icst307"))
|
|
|
|
icst_desc.params = &icst307_params;
|
|
|
|
else {
|
|
|
|
pr_err("unknown ICST clock %s\n", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parent clock name is not the same as node parent */
|
|
|
|
parent_name = of_clk_get_parent_name(np, 0);
|
|
|
|
|
|
|
|
regclk = icst_clk_setup(NULL, &icst_desc, name, parent_name, map);
|
|
|
|
if (IS_ERR(regclk)) {
|
|
|
|
pr_err("error setting up syscon ICST clock %s\n", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
of_clk_add_provider(np, of_clk_src_simple_get, regclk);
|
|
|
|
pr_debug("registered syscon ICST clock %s\n", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
CLK_OF_DECLARE(arm_syscon_icst525_clk,
|
|
|
|
"arm,syscon-icst525", of_syscon_icst_setup);
|
|
|
|
CLK_OF_DECLARE(arm_syscon_icst307_clk,
|
|
|
|
"arm,syscon-icst307", of_syscon_icst_setup);
|
|
|
|
|
|
|
|
#endif
|