18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _CCU_MUX_H_
38c2ecf20Sopenharmony_ci#define _CCU_MUX_H_
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include "ccu_common.h"
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_cistruct ccu_mux_fixed_prediv {
108c2ecf20Sopenharmony_ci	u8	index;
118c2ecf20Sopenharmony_ci	u16	div;
128c2ecf20Sopenharmony_ci};
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistruct ccu_mux_var_prediv {
158c2ecf20Sopenharmony_ci	u8	index;
168c2ecf20Sopenharmony_ci	u8	shift;
178c2ecf20Sopenharmony_ci	u8	width;
188c2ecf20Sopenharmony_ci};
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistruct ccu_mux_internal {
218c2ecf20Sopenharmony_ci	u8		shift;
228c2ecf20Sopenharmony_ci	u8		width;
238c2ecf20Sopenharmony_ci	const u8	*table;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	const struct ccu_mux_fixed_prediv	*fixed_predivs;
268c2ecf20Sopenharmony_ci	u8		n_predivs;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	const struct ccu_mux_var_prediv		*var_predivs;
298c2ecf20Sopenharmony_ci	u8		n_var_predivs;
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define _SUNXI_CCU_MUX_TABLE(_shift, _width, _table)	\
338c2ecf20Sopenharmony_ci	{						\
348c2ecf20Sopenharmony_ci		.shift	= _shift,			\
358c2ecf20Sopenharmony_ci		.width	= _width,			\
368c2ecf20Sopenharmony_ci		.table	= _table,			\
378c2ecf20Sopenharmony_ci	}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define _SUNXI_CCU_MUX(_shift, _width) \
408c2ecf20Sopenharmony_ci	_SUNXI_CCU_MUX_TABLE(_shift, _width, NULL)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistruct ccu_mux {
438c2ecf20Sopenharmony_ci	u16			reg;
448c2ecf20Sopenharmony_ci	u32			enable;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	struct ccu_mux_internal	mux;
478c2ecf20Sopenharmony_ci	struct ccu_common	common;
488c2ecf20Sopenharmony_ci};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define SUNXI_CCU_MUX_TABLE_WITH_GATE(_struct, _name, _parents, _table,	\
518c2ecf20Sopenharmony_ci				     _reg, _shift, _width, _gate,	\
528c2ecf20Sopenharmony_ci				     _flags)				\
538c2ecf20Sopenharmony_ci	struct ccu_mux _struct = {					\
548c2ecf20Sopenharmony_ci		.enable	= _gate,					\
558c2ecf20Sopenharmony_ci		.mux	= _SUNXI_CCU_MUX_TABLE(_shift, _width, _table),	\
568c2ecf20Sopenharmony_ci		.common	= {						\
578c2ecf20Sopenharmony_ci			.reg		= _reg,				\
588c2ecf20Sopenharmony_ci			.hw.init	= CLK_HW_INIT_PARENTS(_name,	\
598c2ecf20Sopenharmony_ci							      _parents, \
608c2ecf20Sopenharmony_ci							      &ccu_mux_ops, \
618c2ecf20Sopenharmony_ci							      _flags),	\
628c2ecf20Sopenharmony_ci		}							\
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define SUNXI_CCU_MUX_WITH_GATE(_struct, _name, _parents, _reg,		\
668c2ecf20Sopenharmony_ci				_shift, _width, _gate, _flags)		\
678c2ecf20Sopenharmony_ci	SUNXI_CCU_MUX_TABLE_WITH_GATE(_struct, _name, _parents, NULL,	\
688c2ecf20Sopenharmony_ci				      _reg, _shift, _width, _gate,	\
698c2ecf20Sopenharmony_ci				      _flags)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define SUNXI_CCU_MUX(_struct, _name, _parents, _reg, _shift, _width,	\
728c2ecf20Sopenharmony_ci		      _flags)						\
738c2ecf20Sopenharmony_ci	SUNXI_CCU_MUX_TABLE_WITH_GATE(_struct, _name, _parents, NULL,	\
748c2ecf20Sopenharmony_ci				      _reg, _shift, _width, 0, _flags)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic inline struct ccu_mux *hw_to_ccu_mux(struct clk_hw *hw)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	struct ccu_common *common = hw_to_ccu_common(hw);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	return container_of(common, struct ccu_mux, common);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciextern const struct clk_ops ccu_mux_ops;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciunsigned long ccu_mux_helper_apply_prediv(struct ccu_common *common,
868c2ecf20Sopenharmony_ci					  struct ccu_mux_internal *cm,
878c2ecf20Sopenharmony_ci					  int parent_index,
888c2ecf20Sopenharmony_ci					  unsigned long parent_rate);
898c2ecf20Sopenharmony_ciint ccu_mux_helper_determine_rate(struct ccu_common *common,
908c2ecf20Sopenharmony_ci				  struct ccu_mux_internal *cm,
918c2ecf20Sopenharmony_ci				  struct clk_rate_request *req,
928c2ecf20Sopenharmony_ci				  unsigned long (*round)(struct ccu_mux_internal *,
938c2ecf20Sopenharmony_ci							 struct clk_hw *,
948c2ecf20Sopenharmony_ci							 unsigned long *,
958c2ecf20Sopenharmony_ci							 unsigned long,
968c2ecf20Sopenharmony_ci							 void *),
978c2ecf20Sopenharmony_ci				  void *data);
988c2ecf20Sopenharmony_ciu8 ccu_mux_helper_get_parent(struct ccu_common *common,
998c2ecf20Sopenharmony_ci			     struct ccu_mux_internal *cm);
1008c2ecf20Sopenharmony_ciint ccu_mux_helper_set_parent(struct ccu_common *common,
1018c2ecf20Sopenharmony_ci			      struct ccu_mux_internal *cm,
1028c2ecf20Sopenharmony_ci			      u8 index);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistruct ccu_mux_nb {
1058c2ecf20Sopenharmony_ci	struct notifier_block	clk_nb;
1068c2ecf20Sopenharmony_ci	struct ccu_common	*common;
1078c2ecf20Sopenharmony_ci	struct ccu_mux_internal	*cm;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	u32	delay_us;	/* How many us to wait after reparenting */
1108c2ecf20Sopenharmony_ci	u8	bypass_index;	/* Which parent to temporarily use */
1118c2ecf20Sopenharmony_ci	u8	original_index;	/* This is set by the notifier callback */
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define to_ccu_mux_nb(_nb) container_of(_nb, struct ccu_mux_nb, clk_nb)
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ciint ccu_mux_notifier_register(struct clk *clk, struct ccu_mux_nb *mux_nb);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#endif /* _CCU_MUX_H_ */
119