2018-12-12 01:57:48 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-09-05 20:21:34 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 - 2014 Texas Instruments Incorporated - http://www.ti.com
|
2015-06-28 22:24:55 +08:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Jyri Sarha <jsarha@ti.com>
|
|
|
|
* Sergej Sawazki <ce3a@gmx.de>
|
2014-09-05 20:21:34 +08:00
|
|
|
*
|
2015-06-28 22:24:55 +08:00
|
|
|
* Gpio controlled clock implementation
|
2014-09-05 20:21:34 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk-provider.h>
|
2015-06-28 22:24:53 +08:00
|
|
|
#include <linux/export.h>
|
2014-09-05 20:21:34 +08:00
|
|
|
#include <linux/slab.h>
|
2014-10-01 01:16:22 +08:00
|
|
|
#include <linux/gpio/consumer.h>
|
2014-09-05 20:21:34 +08:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/device.h>
|
2016-02-03 09:09:26 +08:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/of_device.h>
|
2014-09-05 20:21:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DOC: basic gpio gated clock which can be enabled and disabled
|
|
|
|
* with gpio output
|
|
|
|
* Traits of this clock:
|
|
|
|
* prepare - clk_(un)prepare only ensures parent is (un)prepared
|
|
|
|
* enable - clk_enable and clk_disable are functional & control gpio
|
|
|
|
* rate - inherits rate from parent. No clk_set_rate support
|
|
|
|
* parent - fixed parent. No clk_set_parent support
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int clk_gpio_gate_enable(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gpio *clk = to_clk_gpio(hw);
|
|
|
|
|
|
|
|
gpiod_set_value(clk->gpiod, 1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clk_gpio_gate_disable(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gpio *clk = to_clk_gpio(hw);
|
|
|
|
|
|
|
|
gpiod_set_value(clk->gpiod, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_gpio_gate_is_enabled(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gpio *clk = to_clk_gpio(hw);
|
|
|
|
|
|
|
|
return gpiod_get_value(clk->gpiod);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct clk_ops clk_gpio_gate_ops = {
|
|
|
|
.enable = clk_gpio_gate_enable,
|
|
|
|
.disable = clk_gpio_gate_disable,
|
|
|
|
.is_enabled = clk_gpio_gate_is_enabled,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(clk_gpio_gate_ops);
|
|
|
|
|
2019-02-11 21:58:18 +08:00
|
|
|
static int clk_sleeping_gpio_gate_prepare(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gpio *clk = to_clk_gpio(hw);
|
|
|
|
|
|
|
|
gpiod_set_value_cansleep(clk->gpiod, 1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clk_sleeping_gpio_gate_unprepare(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gpio *clk = to_clk_gpio(hw);
|
|
|
|
|
|
|
|
gpiod_set_value_cansleep(clk->gpiod, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_sleeping_gpio_gate_is_prepared(struct clk_hw *hw)
|
|
|
|
{
|
|
|
|
struct clk_gpio *clk = to_clk_gpio(hw);
|
|
|
|
|
|
|
|
return gpiod_get_value_cansleep(clk->gpiod);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct clk_ops clk_sleeping_gpio_gate_ops = {
|
|
|
|
.prepare = clk_sleeping_gpio_gate_prepare,
|
|
|
|
.unprepare = clk_sleeping_gpio_gate_unprepare,
|
|
|
|
.is_prepared = clk_sleeping_gpio_gate_is_prepared,
|
|
|
|
};
|
|
|
|
|
2014-09-05 20:21:34 +08:00
|
|
|
/**
|
2015-06-28 22:24:55 +08:00
|
|
|
* DOC: basic clock multiplexer which can be controlled with a gpio output
|
|
|
|
* Traits of this clock:
|
|
|
|
* prepare - clk_prepare only ensures that parents are prepared
|
|
|
|
* rate - rate is only affected by parent switching. No clk_set_rate support
|
|
|
|
* parent - parent is adjustable through clk_set_parent
|
2014-09-05 20:21:34 +08:00
|
|
|
*/
|
2015-06-28 22:24:55 +08:00
|
|
|
|
|
|
|
static u8 clk_gpio_mux_get_parent(struct clk_hw *hw)
|
2014-09-05 20:21:34 +08:00
|
|
|
{
|
2015-06-28 22:24:55 +08:00
|
|
|
struct clk_gpio *clk = to_clk_gpio(hw);
|
|
|
|
|
2018-03-13 16:54:03 +08:00
|
|
|
return gpiod_get_value_cansleep(clk->gpiod);
|
2015-06-28 22:24:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int clk_gpio_mux_set_parent(struct clk_hw *hw, u8 index)
|
|
|
|
{
|
|
|
|
struct clk_gpio *clk = to_clk_gpio(hw);
|
|
|
|
|
2018-03-13 16:54:03 +08:00
|
|
|
gpiod_set_value_cansleep(clk->gpiod, index);
|
2015-06-28 22:24:55 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct clk_ops clk_gpio_mux_ops = {
|
|
|
|
.get_parent = clk_gpio_mux_get_parent,
|
|
|
|
.set_parent = clk_gpio_mux_set_parent,
|
|
|
|
.determine_rate = __clk_mux_determine_rate,
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(clk_gpio_mux_ops);
|
|
|
|
|
2016-02-07 16:27:55 +08:00
|
|
|
static struct clk_hw *clk_register_gpio(struct device *dev, const char *name,
|
2017-09-25 00:19:18 +08:00
|
|
|
const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
|
|
|
|
unsigned long flags, const struct clk_ops *clk_gpio_ops)
|
2015-06-28 22:24:55 +08:00
|
|
|
{
|
|
|
|
struct clk_gpio *clk_gpio;
|
2016-02-07 16:27:55 +08:00
|
|
|
struct clk_hw *hw;
|
2015-06-28 22:24:55 +08:00
|
|
|
struct clk_init_data init = {};
|
2014-09-05 20:21:34 +08:00
|
|
|
int err;
|
|
|
|
|
2015-06-28 22:24:55 +08:00
|
|
|
if (dev)
|
|
|
|
clk_gpio = devm_kzalloc(dev, sizeof(*clk_gpio), GFP_KERNEL);
|
|
|
|
else
|
|
|
|
clk_gpio = kzalloc(sizeof(*clk_gpio), GFP_KERNEL);
|
|
|
|
|
|
|
|
if (!clk_gpio)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2014-09-05 20:21:34 +08:00
|
|
|
init.name = name;
|
2015-06-28 22:24:55 +08:00
|
|
|
init.ops = clk_gpio_ops;
|
2019-04-26 01:57:37 +08:00
|
|
|
init.flags = flags;
|
2015-06-28 22:24:55 +08:00
|
|
|
init.parent_names = parent_names;
|
|
|
|
init.num_parents = num_parents;
|
2014-09-05 20:21:34 +08:00
|
|
|
|
2017-09-25 00:19:18 +08:00
|
|
|
clk_gpio->gpiod = gpiod;
|
2014-09-05 20:21:34 +08:00
|
|
|
clk_gpio->hw.init = &init;
|
|
|
|
|
2016-02-07 16:27:55 +08:00
|
|
|
hw = &clk_gpio->hw;
|
2015-06-28 22:24:55 +08:00
|
|
|
if (dev)
|
2016-02-07 16:27:55 +08:00
|
|
|
err = devm_clk_hw_register(dev, hw);
|
2015-06-28 22:24:55 +08:00
|
|
|
else
|
2016-02-07 16:27:55 +08:00
|
|
|
err = clk_hw_register(NULL, hw);
|
2014-09-05 20:21:34 +08:00
|
|
|
|
2016-02-07 16:27:55 +08:00
|
|
|
if (!err)
|
|
|
|
return hw;
|
2014-09-05 20:21:34 +08:00
|
|
|
|
2015-06-28 22:24:55 +08:00
|
|
|
if (!dev) {
|
2014-09-05 20:21:34 +08:00
|
|
|
kfree(clk_gpio);
|
2015-06-28 22:24:55 +08:00
|
|
|
}
|
2014-09-05 20:21:34 +08:00
|
|
|
|
2016-02-07 16:27:55 +08:00
|
|
|
return ERR_PTR(err);
|
2014-09-05 20:21:34 +08:00
|
|
|
}
|
2015-06-28 22:24:55 +08:00
|
|
|
|
|
|
|
/**
|
2016-02-07 16:27:55 +08:00
|
|
|
* clk_hw_register_gpio_gate - register a gpio clock gate with the clock
|
|
|
|
* framework
|
2015-06-28 22:24:55 +08:00
|
|
|
* @dev: device that is registering this clock
|
|
|
|
* @name: name of this clock
|
|
|
|
* @parent_name: name of this clock's parent
|
2017-09-25 00:19:18 +08:00
|
|
|
* @gpiod: gpio descriptor to gate this clock
|
2015-06-28 22:24:55 +08:00
|
|
|
* @flags: clock flags
|
|
|
|
*/
|
2016-02-07 16:27:55 +08:00
|
|
|
struct clk_hw *clk_hw_register_gpio_gate(struct device *dev, const char *name,
|
2017-09-25 00:19:18 +08:00
|
|
|
const char *parent_name, struct gpio_desc *gpiod,
|
2015-06-28 22:24:55 +08:00
|
|
|
unsigned long flags)
|
|
|
|
{
|
2019-02-11 21:58:18 +08:00
|
|
|
const struct clk_ops *ops;
|
|
|
|
|
|
|
|
if (gpiod_cansleep(gpiod))
|
|
|
|
ops = &clk_sleeping_gpio_gate_ops;
|
|
|
|
else
|
|
|
|
ops = &clk_gpio_gate_ops;
|
|
|
|
|
2015-06-28 22:24:55 +08:00
|
|
|
return clk_register_gpio(dev, name,
|
|
|
|
(parent_name ? &parent_name : NULL),
|
2019-02-11 21:58:18 +08:00
|
|
|
(parent_name ? 1 : 0), gpiod, flags, ops);
|
2015-06-28 22:24:55 +08:00
|
|
|
}
|
2016-02-07 16:27:55 +08:00
|
|
|
EXPORT_SYMBOL_GPL(clk_hw_register_gpio_gate);
|
|
|
|
|
|
|
|
struct clk *clk_register_gpio_gate(struct device *dev, const char *name,
|
2017-09-25 00:19:18 +08:00
|
|
|
const char *parent_name, struct gpio_desc *gpiod,
|
2016-02-07 16:27:55 +08:00
|
|
|
unsigned long flags)
|
|
|
|
{
|
|
|
|
struct clk_hw *hw;
|
|
|
|
|
2017-09-25 00:19:18 +08:00
|
|
|
hw = clk_hw_register_gpio_gate(dev, name, parent_name, gpiod, flags);
|
2016-02-07 16:27:55 +08:00
|
|
|
if (IS_ERR(hw))
|
|
|
|
return ERR_CAST(hw);
|
|
|
|
return hw->clk;
|
|
|
|
}
|
2014-09-05 20:21:34 +08:00
|
|
|
EXPORT_SYMBOL_GPL(clk_register_gpio_gate);
|
|
|
|
|
2015-06-28 22:24:55 +08:00
|
|
|
/**
|
2016-02-07 16:27:55 +08:00
|
|
|
* clk_hw_register_gpio_mux - register a gpio clock mux with the clock framework
|
2015-06-28 22:24:55 +08:00
|
|
|
* @dev: device that is registering this clock
|
|
|
|
* @name: name of this clock
|
|
|
|
* @parent_names: names of this clock's parents
|
|
|
|
* @num_parents: number of parents listed in @parent_names
|
2017-09-25 00:19:18 +08:00
|
|
|
* @gpiod: gpio descriptor to gate this clock
|
2015-06-28 22:24:55 +08:00
|
|
|
* @flags: clock flags
|
|
|
|
*/
|
2016-02-07 16:27:55 +08:00
|
|
|
struct clk_hw *clk_hw_register_gpio_mux(struct device *dev, const char *name,
|
2017-09-25 00:19:18 +08:00
|
|
|
const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
|
|
|
|
unsigned long flags)
|
2015-06-28 22:24:55 +08:00
|
|
|
{
|
|
|
|
if (num_parents != 2) {
|
|
|
|
pr_err("mux-clock %s must have 2 parents\n", name);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clk_register_gpio(dev, name, parent_names, num_parents,
|
2017-09-25 00:19:18 +08:00
|
|
|
gpiod, flags, &clk_gpio_mux_ops);
|
2015-06-28 22:24:55 +08:00
|
|
|
}
|
2016-02-07 16:27:55 +08:00
|
|
|
EXPORT_SYMBOL_GPL(clk_hw_register_gpio_mux);
|
|
|
|
|
|
|
|
struct clk *clk_register_gpio_mux(struct device *dev, const char *name,
|
2017-09-25 00:19:18 +08:00
|
|
|
const char * const *parent_names, u8 num_parents, struct gpio_desc *gpiod,
|
|
|
|
unsigned long flags)
|
2016-02-07 16:27:55 +08:00
|
|
|
{
|
|
|
|
struct clk_hw *hw;
|
|
|
|
|
|
|
|
hw = clk_hw_register_gpio_mux(dev, name, parent_names, num_parents,
|
2017-09-25 00:19:18 +08:00
|
|
|
gpiod, flags);
|
2016-02-07 16:27:55 +08:00
|
|
|
if (IS_ERR(hw))
|
|
|
|
return ERR_CAST(hw);
|
|
|
|
return hw->clk;
|
|
|
|
}
|
2015-06-28 22:24:55 +08:00
|
|
|
EXPORT_SYMBOL_GPL(clk_register_gpio_mux);
|
|
|
|
|
2016-02-03 09:09:26 +08:00
|
|
|
static int gpio_clk_driver_probe(struct platform_device *pdev)
|
2014-09-05 20:21:34 +08:00
|
|
|
{
|
2016-02-03 09:09:26 +08:00
|
|
|
struct device_node *node = pdev->dev.of_node;
|
|
|
|
const char **parent_names, *gpio_name;
|
2016-02-20 09:31:52 +08:00
|
|
|
unsigned int num_parents;
|
2017-09-25 00:19:18 +08:00
|
|
|
struct gpio_desc *gpiod;
|
2016-02-03 09:09:26 +08:00
|
|
|
struct clk *clk;
|
2017-09-25 00:19:18 +08:00
|
|
|
bool is_mux;
|
|
|
|
int ret;
|
2016-02-03 09:09:26 +08:00
|
|
|
|
|
|
|
num_parents = of_clk_get_parent_count(node);
|
|
|
|
if (num_parents) {
|
|
|
|
parent_names = devm_kcalloc(&pdev->dev, num_parents,
|
|
|
|
sizeof(char *), GFP_KERNEL);
|
|
|
|
if (!parent_names)
|
|
|
|
return -ENOMEM;
|
2014-09-05 20:21:34 +08:00
|
|
|
|
2016-02-03 09:09:26 +08:00
|
|
|
of_clk_parent_fill(node, parent_names, num_parents);
|
|
|
|
} else {
|
|
|
|
parent_names = NULL;
|
2014-09-05 20:21:34 +08:00
|
|
|
}
|
|
|
|
|
2016-02-03 09:09:26 +08:00
|
|
|
is_mux = of_device_is_compatible(node, "gpio-mux-clock");
|
|
|
|
|
2017-09-25 00:19:18 +08:00
|
|
|
gpio_name = is_mux ? "select" : "enable";
|
2017-09-25 00:19:19 +08:00
|
|
|
gpiod = devm_gpiod_get(&pdev->dev, gpio_name, GPIOD_OUT_LOW);
|
2017-09-25 00:19:18 +08:00
|
|
|
if (IS_ERR(gpiod)) {
|
|
|
|
ret = PTR_ERR(gpiod);
|
|
|
|
if (ret == -EPROBE_DEFER)
|
2018-08-28 23:44:29 +08:00
|
|
|
pr_debug("%pOFn: %s: GPIOs not yet available, retry later\n",
|
|
|
|
node, __func__);
|
2015-06-28 22:24:55 +08:00
|
|
|
else
|
2018-08-28 23:44:29 +08:00
|
|
|
pr_err("%pOFn: %s: Can't get '%s' named GPIO property\n",
|
|
|
|
node, __func__,
|
2016-02-03 09:09:26 +08:00
|
|
|
gpio_name);
|
2017-09-25 00:19:18 +08:00
|
|
|
return ret;
|
2014-09-05 20:21:34 +08:00
|
|
|
}
|
|
|
|
|
2016-02-03 09:09:26 +08:00
|
|
|
if (is_mux)
|
|
|
|
clk = clk_register_gpio_mux(&pdev->dev, node->name,
|
2017-09-25 00:19:18 +08:00
|
|
|
parent_names, num_parents, gpiod, 0);
|
2016-02-03 09:09:26 +08:00
|
|
|
else
|
|
|
|
clk = clk_register_gpio_gate(&pdev->dev, node->name,
|
2017-09-25 00:19:18 +08:00
|
|
|
parent_names ? parent_names[0] : NULL, gpiod,
|
clk: clk-gpio: propagate rate change to parent
For an external clock source, which is gated via a GPIO, the
rate change should typically be propagated to the parent clock.
The situation where we are requiring this propagation, is when an
external clock is connected to override an internal clock (which typically
has a fixed rate). The external clock can have a different rate than the
internal one, and may also be variable, thus requiring the rate
propagation.
This rate change wasn't propagated until now, and it's unclear about cases
where this shouldn't be propagated. Thus, it's unclear whether this is
fixing a bug, or extending the current driver behavior. Also, it's unsure
about whether this may break any existing setups; in the case that it does,
a device-tree property may be added to disable this flag.
Signed-off-by: Michael Hennerich <michael.hennerich@analog.com>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
Link: https://lkml.kernel.org/r/20191108071718.17985-1-alexandru.ardelean@analog.com
Signed-off-by: Stephen Boyd <sboyd@kernel.org>
2019-11-08 15:17:18 +08:00
|
|
|
CLK_SET_RATE_PARENT);
|
2016-02-03 09:09:26 +08:00
|
|
|
if (IS_ERR(clk))
|
|
|
|
return PTR_ERR(clk);
|
2014-09-05 20:21:34 +08:00
|
|
|
|
2016-02-03 09:09:26 +08:00
|
|
|
return of_clk_add_provider(node, of_clk_src_simple_get, clk);
|
2015-06-28 22:24:55 +08:00
|
|
|
}
|
|
|
|
|
2016-02-03 09:09:26 +08:00
|
|
|
static const struct of_device_id gpio_clk_match_table[] = {
|
|
|
|
{ .compatible = "gpio-mux-clock" },
|
|
|
|
{ .compatible = "gpio-gate-clock" },
|
|
|
|
{ }
|
|
|
|
};
|
2015-06-28 22:24:55 +08:00
|
|
|
|
2016-02-03 09:09:26 +08:00
|
|
|
static struct platform_driver gpio_clk_driver = {
|
|
|
|
.probe = gpio_clk_driver_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = "gpio-clk",
|
|
|
|
.of_match_table = gpio_clk_match_table,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
builtin_platform_driver(gpio_clk_driver);
|