xref: /openbmc/linux/drivers/clk/meson/clk-cpu-dyndiv.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
126d34431SNeil Armstrong // SPDX-License-Identifier: (GPL-2.0 OR MIT)
226d34431SNeil Armstrong /*
326d34431SNeil Armstrong  * Copyright (c) 2019 BayLibre, SAS.
426d34431SNeil Armstrong  * Author: Neil Armstrong <narmstrong@baylibre.com>
526d34431SNeil Armstrong  */
626d34431SNeil Armstrong 
726d34431SNeil Armstrong #include <linux/clk-provider.h>
826d34431SNeil Armstrong #include <linux/module.h>
926d34431SNeil Armstrong 
1026d34431SNeil Armstrong #include "clk-regmap.h"
1126d34431SNeil Armstrong #include "clk-cpu-dyndiv.h"
1226d34431SNeil Armstrong 
1326d34431SNeil Armstrong static inline struct meson_clk_cpu_dyndiv_data *
meson_clk_cpu_dyndiv_data(struct clk_regmap * clk)1426d34431SNeil Armstrong meson_clk_cpu_dyndiv_data(struct clk_regmap *clk)
1526d34431SNeil Armstrong {
1626d34431SNeil Armstrong 	return (struct meson_clk_cpu_dyndiv_data *)clk->data;
1726d34431SNeil Armstrong }
1826d34431SNeil Armstrong 
meson_clk_cpu_dyndiv_recalc_rate(struct clk_hw * hw,unsigned long prate)1926d34431SNeil Armstrong static unsigned long meson_clk_cpu_dyndiv_recalc_rate(struct clk_hw *hw,
2026d34431SNeil Armstrong 						      unsigned long prate)
2126d34431SNeil Armstrong {
2226d34431SNeil Armstrong 	struct clk_regmap *clk = to_clk_regmap(hw);
2326d34431SNeil Armstrong 	struct meson_clk_cpu_dyndiv_data *data = meson_clk_cpu_dyndiv_data(clk);
2426d34431SNeil Armstrong 
2526d34431SNeil Armstrong 	return divider_recalc_rate(hw, prate,
2626d34431SNeil Armstrong 				   meson_parm_read(clk->map, &data->div),
2726d34431SNeil Armstrong 				   NULL, 0, data->div.width);
2826d34431SNeil Armstrong }
2926d34431SNeil Armstrong 
meson_clk_cpu_dyndiv_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)30*716592fdSMartin Blumenstingl static int meson_clk_cpu_dyndiv_determine_rate(struct clk_hw *hw,
31*716592fdSMartin Blumenstingl 					       struct clk_rate_request *req)
3226d34431SNeil Armstrong {
3326d34431SNeil Armstrong 	struct clk_regmap *clk = to_clk_regmap(hw);
3426d34431SNeil Armstrong 	struct meson_clk_cpu_dyndiv_data *data = meson_clk_cpu_dyndiv_data(clk);
3526d34431SNeil Armstrong 
36*716592fdSMartin Blumenstingl 	return divider_determine_rate(hw, req, NULL, data->div.width, 0);
3726d34431SNeil Armstrong }
3826d34431SNeil Armstrong 
meson_clk_cpu_dyndiv_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)3926d34431SNeil Armstrong static int meson_clk_cpu_dyndiv_set_rate(struct clk_hw *hw, unsigned long rate,
4026d34431SNeil Armstrong 					  unsigned long parent_rate)
4126d34431SNeil Armstrong {
4226d34431SNeil Armstrong 	struct clk_regmap *clk = to_clk_regmap(hw);
4326d34431SNeil Armstrong 	struct meson_clk_cpu_dyndiv_data *data = meson_clk_cpu_dyndiv_data(clk);
4426d34431SNeil Armstrong 	unsigned int val;
4526d34431SNeil Armstrong 	int ret;
4626d34431SNeil Armstrong 
4726d34431SNeil Armstrong 	ret = divider_get_val(rate, parent_rate, NULL, data->div.width, 0);
4826d34431SNeil Armstrong 	if (ret < 0)
4926d34431SNeil Armstrong 		return ret;
5026d34431SNeil Armstrong 
5126d34431SNeil Armstrong 	val = (unsigned int)ret << data->div.shift;
5226d34431SNeil Armstrong 
5326d34431SNeil Armstrong 	/* Write the SYS_CPU_DYN_ENABLE bit before changing the divider */
5426d34431SNeil Armstrong 	meson_parm_write(clk->map, &data->dyn, 1);
5526d34431SNeil Armstrong 
5626d34431SNeil Armstrong 	/* Update the divider while removing the SYS_CPU_DYN_ENABLE bit */
5726d34431SNeil Armstrong 	return regmap_update_bits(clk->map, data->div.reg_off,
5826d34431SNeil Armstrong 				  SETPMASK(data->div.width, data->div.shift) |
5926d34431SNeil Armstrong 				  SETPMASK(data->dyn.width, data->dyn.shift),
6026d34431SNeil Armstrong 				  val);
6126d34431SNeil Armstrong };
6226d34431SNeil Armstrong 
6326d34431SNeil Armstrong const struct clk_ops meson_clk_cpu_dyndiv_ops = {
6426d34431SNeil Armstrong 	.recalc_rate = meson_clk_cpu_dyndiv_recalc_rate,
65*716592fdSMartin Blumenstingl 	.determine_rate = meson_clk_cpu_dyndiv_determine_rate,
6626d34431SNeil Armstrong 	.set_rate = meson_clk_cpu_dyndiv_set_rate,
6726d34431SNeil Armstrong };
6826d34431SNeil Armstrong EXPORT_SYMBOL_GPL(meson_clk_cpu_dyndiv_ops);
6926d34431SNeil Armstrong 
7026d34431SNeil Armstrong MODULE_DESCRIPTION("Amlogic CPU Dynamic Clock divider");
7126d34431SNeil Armstrong MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
7226d34431SNeil Armstrong MODULE_LICENSE("GPL v2");
73