18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0+ */
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// OWL factor clock driver
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright (c) 2014 Actions Semi Inc.
68c2ecf20Sopenharmony_ci// Author: David Liu <liuwei@actions-semi.com>
78c2ecf20Sopenharmony_ci//
88c2ecf20Sopenharmony_ci// Copyright (c) 2018 Linaro Ltd.
98c2ecf20Sopenharmony_ci// Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifndef _OWL_FACTOR_H_
128c2ecf20Sopenharmony_ci#define _OWL_FACTOR_H_
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "owl-common.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistruct clk_factor_table {
178c2ecf20Sopenharmony_ci	unsigned int		val;
188c2ecf20Sopenharmony_ci	unsigned int		mul;
198c2ecf20Sopenharmony_ci	unsigned int		div;
208c2ecf20Sopenharmony_ci};
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct owl_factor_hw {
238c2ecf20Sopenharmony_ci	u32			reg;
248c2ecf20Sopenharmony_ci	u8			shift;
258c2ecf20Sopenharmony_ci	u8			width;
268c2ecf20Sopenharmony_ci	u8			fct_flags;
278c2ecf20Sopenharmony_ci	struct clk_factor_table	*table;
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistruct owl_factor {
318c2ecf20Sopenharmony_ci	struct owl_factor_hw	factor_hw;
328c2ecf20Sopenharmony_ci	struct owl_clk_common	common;
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define OWL_FACTOR_HW(_reg, _shift, _width, _fct_flags, _table)		\
368c2ecf20Sopenharmony_ci	{								\
378c2ecf20Sopenharmony_ci		.reg		= _reg,					\
388c2ecf20Sopenharmony_ci		.shift		= _shift,				\
398c2ecf20Sopenharmony_ci		.width		= _width,				\
408c2ecf20Sopenharmony_ci		.fct_flags	= _fct_flags,				\
418c2ecf20Sopenharmony_ci		.table		= _table,				\
428c2ecf20Sopenharmony_ci	}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define OWL_FACTOR(_struct, _name, _parent, _reg,			\
458c2ecf20Sopenharmony_ci		   _shift, _width, _table, _fct_flags, _flags)		\
468c2ecf20Sopenharmony_ci	struct owl_factor _struct = {					\
478c2ecf20Sopenharmony_ci		.factor_hw = OWL_FACTOR_HW(_reg, _shift,		\
488c2ecf20Sopenharmony_ci					   _width, _fct_flags, _table),	\
498c2ecf20Sopenharmony_ci		.common = {						\
508c2ecf20Sopenharmony_ci			.regmap		= NULL,				\
518c2ecf20Sopenharmony_ci			.hw.init	= CLK_HW_INIT(_name,		\
528c2ecf20Sopenharmony_ci						      _parent,		\
538c2ecf20Sopenharmony_ci						      &owl_factor_ops,	\
548c2ecf20Sopenharmony_ci						      _flags),		\
558c2ecf20Sopenharmony_ci		},							\
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define div_mask(d) ((1 << ((d)->width)) - 1)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic inline struct owl_factor *hw_to_owl_factor(const struct clk_hw *hw)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct owl_clk_common *common = hw_to_owl_clk_common(hw);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	return container_of(common, struct owl_factor, common);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cilong owl_factor_helper_round_rate(struct owl_clk_common *common,
688c2ecf20Sopenharmony_ci				const struct owl_factor_hw *factor_hw,
698c2ecf20Sopenharmony_ci				unsigned long rate,
708c2ecf20Sopenharmony_ci				unsigned long *parent_rate);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciunsigned long owl_factor_helper_recalc_rate(struct owl_clk_common *common,
738c2ecf20Sopenharmony_ci					 const struct owl_factor_hw *factor_hw,
748c2ecf20Sopenharmony_ci					 unsigned long parent_rate);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ciint owl_factor_helper_set_rate(const struct owl_clk_common *common,
778c2ecf20Sopenharmony_ci				const struct owl_factor_hw *factor_hw,
788c2ecf20Sopenharmony_ci				unsigned long rate,
798c2ecf20Sopenharmony_ci				unsigned long parent_rate);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ciextern const struct clk_ops owl_factor_ops;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#endif /* _OWL_FACTOR_H_ */
84