162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2016 Maxime Ripard. All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef _CCU_DIV_H_
762306a36Sopenharmony_ci#define _CCU_DIV_H_
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/clk-provider.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include "ccu_common.h"
1262306a36Sopenharmony_ci#include "ccu_mux.h"
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci/**
1562306a36Sopenharmony_ci * struct ccu_div_internal - Internal divider description
1662306a36Sopenharmony_ci * @shift: Bit offset of the divider in its register
1762306a36Sopenharmony_ci * @width: Width of the divider field in its register
1862306a36Sopenharmony_ci * @max: Maximum value allowed for that divider. This is the
1962306a36Sopenharmony_ci *       arithmetic value, not the maximum value to be set in the
2062306a36Sopenharmony_ci *       register.
2162306a36Sopenharmony_ci * @flags: clk_divider flags to apply on this divider
2262306a36Sopenharmony_ci * @table: Divider table pointer (if applicable)
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * That structure represents a single divider, and is meant to be
2562306a36Sopenharmony_ci * embedded in other structures representing the various clock
2662306a36Sopenharmony_ci * classes.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * It is basically a wrapper around the clk_divider functions
2962306a36Sopenharmony_ci * arguments.
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_cistruct ccu_div_internal {
3262306a36Sopenharmony_ci	u8			shift;
3362306a36Sopenharmony_ci	u8			width;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	u32			max;
3662306a36Sopenharmony_ci	u32			offset;
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	u32			flags;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci	struct clk_div_table	*table;
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci#define _SUNXI_CCU_DIV_TABLE_FLAGS(_shift, _width, _table, _flags)	\
4462306a36Sopenharmony_ci	{								\
4562306a36Sopenharmony_ci		.shift	= _shift,					\
4662306a36Sopenharmony_ci		.width	= _width,					\
4762306a36Sopenharmony_ci		.flags	= _flags,					\
4862306a36Sopenharmony_ci		.table	= _table,					\
4962306a36Sopenharmony_ci	}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci#define _SUNXI_CCU_DIV_TABLE(_shift, _width, _table)			\
5262306a36Sopenharmony_ci	_SUNXI_CCU_DIV_TABLE_FLAGS(_shift, _width, _table, 0)
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define _SUNXI_CCU_DIV_OFFSET_MAX_FLAGS(_shift, _width, _off, _max, _flags) \
5562306a36Sopenharmony_ci	{								\
5662306a36Sopenharmony_ci		.shift	= _shift,					\
5762306a36Sopenharmony_ci		.width	= _width,					\
5862306a36Sopenharmony_ci		.flags	= _flags,					\
5962306a36Sopenharmony_ci		.max	= _max,						\
6062306a36Sopenharmony_ci		.offset	= _off,						\
6162306a36Sopenharmony_ci	}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci#define _SUNXI_CCU_DIV_MAX_FLAGS(_shift, _width, _max, _flags)		\
6462306a36Sopenharmony_ci	_SUNXI_CCU_DIV_OFFSET_MAX_FLAGS(_shift, _width, 1, _max, _flags)
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#define _SUNXI_CCU_DIV_FLAGS(_shift, _width, _flags)			\
6762306a36Sopenharmony_ci	_SUNXI_CCU_DIV_MAX_FLAGS(_shift, _width, 0, _flags)
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci#define _SUNXI_CCU_DIV_MAX(_shift, _width, _max)			\
7062306a36Sopenharmony_ci	_SUNXI_CCU_DIV_MAX_FLAGS(_shift, _width, _max, 0)
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#define _SUNXI_CCU_DIV_OFFSET(_shift, _width, _offset)			\
7362306a36Sopenharmony_ci	_SUNXI_CCU_DIV_OFFSET_MAX_FLAGS(_shift, _width, _offset, 0, 0)
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#define _SUNXI_CCU_DIV(_shift, _width)					\
7662306a36Sopenharmony_ci	_SUNXI_CCU_DIV_FLAGS(_shift, _width, 0)
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistruct ccu_div {
7962306a36Sopenharmony_ci	u32			enable;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	struct ccu_div_internal	div;
8262306a36Sopenharmony_ci	struct ccu_mux_internal	mux;
8362306a36Sopenharmony_ci	struct ccu_common	common;
8462306a36Sopenharmony_ci	unsigned int		fixed_post_div;
8562306a36Sopenharmony_ci};
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci#define SUNXI_CCU_DIV_TABLE_WITH_GATE(_struct, _name, _parent, _reg,	\
8862306a36Sopenharmony_ci				      _shift, _width,			\
8962306a36Sopenharmony_ci				      _table, _gate, _flags)		\
9062306a36Sopenharmony_ci	struct ccu_div _struct = {					\
9162306a36Sopenharmony_ci		.div		= _SUNXI_CCU_DIV_TABLE(_shift, _width,	\
9262306a36Sopenharmony_ci						       _table),		\
9362306a36Sopenharmony_ci		.enable		= _gate,				\
9462306a36Sopenharmony_ci		.common	= {						\
9562306a36Sopenharmony_ci			.reg		= _reg,				\
9662306a36Sopenharmony_ci			.hw.init	= CLK_HW_INIT(_name,		\
9762306a36Sopenharmony_ci						      _parent,		\
9862306a36Sopenharmony_ci						      &ccu_div_ops,	\
9962306a36Sopenharmony_ci						      _flags),		\
10062306a36Sopenharmony_ci		}							\
10162306a36Sopenharmony_ci	}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci#define SUNXI_CCU_DIV_TABLE(_struct, _name, _parent, _reg,		\
10562306a36Sopenharmony_ci			    _shift, _width,				\
10662306a36Sopenharmony_ci			    _table, _flags)				\
10762306a36Sopenharmony_ci	SUNXI_CCU_DIV_TABLE_WITH_GATE(_struct, _name, _parent, _reg,	\
10862306a36Sopenharmony_ci				      _shift, _width, _table, 0,	\
10962306a36Sopenharmony_ci				      _flags)
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci#define SUNXI_CCU_DIV_TABLE_HW(_struct, _name, _parent, _reg,		\
11262306a36Sopenharmony_ci			       _shift, _width,				\
11362306a36Sopenharmony_ci			       _table, _flags)				\
11462306a36Sopenharmony_ci	struct ccu_div _struct = {					\
11562306a36Sopenharmony_ci		.div		= _SUNXI_CCU_DIV_TABLE(_shift, _width,	\
11662306a36Sopenharmony_ci						       _table),		\
11762306a36Sopenharmony_ci		.common	= {						\
11862306a36Sopenharmony_ci			.reg		= _reg,				\
11962306a36Sopenharmony_ci			.hw.init	= CLK_HW_INIT_HW(_name,		\
12062306a36Sopenharmony_ci							 _parent,	\
12162306a36Sopenharmony_ci							 &ccu_div_ops,	\
12262306a36Sopenharmony_ci							 _flags),	\
12362306a36Sopenharmony_ci		}							\
12462306a36Sopenharmony_ci	}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci#define SUNXI_CCU_M_WITH_MUX_TABLE_GATE(_struct, _name,			\
12862306a36Sopenharmony_ci					_parents, _table,		\
12962306a36Sopenharmony_ci					_reg,				\
13062306a36Sopenharmony_ci					_mshift, _mwidth,		\
13162306a36Sopenharmony_ci					_muxshift, _muxwidth,		\
13262306a36Sopenharmony_ci					_gate, _flags)			\
13362306a36Sopenharmony_ci	struct ccu_div _struct = {					\
13462306a36Sopenharmony_ci		.enable	= _gate,					\
13562306a36Sopenharmony_ci		.div	= _SUNXI_CCU_DIV(_mshift, _mwidth),		\
13662306a36Sopenharmony_ci		.mux	= _SUNXI_CCU_MUX_TABLE(_muxshift, _muxwidth, _table), \
13762306a36Sopenharmony_ci		.common	= {						\
13862306a36Sopenharmony_ci			.reg		= _reg,				\
13962306a36Sopenharmony_ci			.hw.init	= CLK_HW_INIT_PARENTS(_name,	\
14062306a36Sopenharmony_ci							      _parents, \
14162306a36Sopenharmony_ci							      &ccu_div_ops, \
14262306a36Sopenharmony_ci							      _flags),	\
14362306a36Sopenharmony_ci		},							\
14462306a36Sopenharmony_ci	}
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci#define SUNXI_CCU_M_WITH_MUX_TABLE_GATE_CLOSEST(_struct, _name,		\
14762306a36Sopenharmony_ci						_parents, _table,	\
14862306a36Sopenharmony_ci						_reg,			\
14962306a36Sopenharmony_ci						_mshift, _mwidth,	\
15062306a36Sopenharmony_ci						_muxshift, _muxwidth,	\
15162306a36Sopenharmony_ci						_gate, _flags)		\
15262306a36Sopenharmony_ci	struct ccu_div _struct = {					\
15362306a36Sopenharmony_ci		.enable	= _gate,					\
15462306a36Sopenharmony_ci		.div	= _SUNXI_CCU_DIV_FLAGS(_mshift, _mwidth, CLK_DIVIDER_ROUND_CLOSEST), \
15562306a36Sopenharmony_ci		.mux	= _SUNXI_CCU_MUX_TABLE(_muxshift, _muxwidth, _table), \
15662306a36Sopenharmony_ci		.common	= {						\
15762306a36Sopenharmony_ci			.reg		= _reg,				\
15862306a36Sopenharmony_ci			.hw.init	= CLK_HW_INIT_PARENTS(_name,	\
15962306a36Sopenharmony_ci							      _parents, \
16062306a36Sopenharmony_ci							      &ccu_div_ops, \
16162306a36Sopenharmony_ci							      _flags),	\
16262306a36Sopenharmony_ci			.features	= CCU_FEATURE_CLOSEST_RATE,	\
16362306a36Sopenharmony_ci		},							\
16462306a36Sopenharmony_ci	}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci#define SUNXI_CCU_M_WITH_MUX_GATE(_struct, _name, _parents, _reg,	\
16762306a36Sopenharmony_ci				  _mshift, _mwidth, _muxshift, _muxwidth, \
16862306a36Sopenharmony_ci				  _gate, _flags)			\
16962306a36Sopenharmony_ci	SUNXI_CCU_M_WITH_MUX_TABLE_GATE(_struct, _name,			\
17062306a36Sopenharmony_ci					_parents, NULL,			\
17162306a36Sopenharmony_ci					_reg, _mshift, _mwidth,		\
17262306a36Sopenharmony_ci					_muxshift, _muxwidth,		\
17362306a36Sopenharmony_ci					_gate, _flags)
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci#define SUNXI_CCU_M_WITH_MUX_GATE_CLOSEST(_struct, _name, _parents,	\
17662306a36Sopenharmony_ci					  _reg, _mshift, _mwidth,	\
17762306a36Sopenharmony_ci					  _muxshift, _muxwidth,		\
17862306a36Sopenharmony_ci					  _gate, _flags)		\
17962306a36Sopenharmony_ci	SUNXI_CCU_M_WITH_MUX_TABLE_GATE_CLOSEST(_struct, _name,		\
18062306a36Sopenharmony_ci						_parents, NULL,		\
18162306a36Sopenharmony_ci						_reg, _mshift, _mwidth,	\
18262306a36Sopenharmony_ci						_muxshift, _muxwidth,	\
18362306a36Sopenharmony_ci						_gate, _flags)
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci#define SUNXI_CCU_M_WITH_MUX(_struct, _name, _parents, _reg,		\
18662306a36Sopenharmony_ci			     _mshift, _mwidth, _muxshift, _muxwidth,	\
18762306a36Sopenharmony_ci			     _flags)					\
18862306a36Sopenharmony_ci	SUNXI_CCU_M_WITH_MUX_TABLE_GATE(_struct, _name,			\
18962306a36Sopenharmony_ci					_parents, NULL,			\
19062306a36Sopenharmony_ci					_reg, _mshift, _mwidth,		\
19162306a36Sopenharmony_ci					_muxshift, _muxwidth,		\
19262306a36Sopenharmony_ci					0, _flags)
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci#define SUNXI_CCU_M_WITH_GATE(_struct, _name, _parent, _reg,		\
19662306a36Sopenharmony_ci			      _mshift, _mwidth,	_gate,			\
19762306a36Sopenharmony_ci			      _flags)					\
19862306a36Sopenharmony_ci	struct ccu_div _struct = {					\
19962306a36Sopenharmony_ci		.enable	= _gate,					\
20062306a36Sopenharmony_ci		.div	= _SUNXI_CCU_DIV(_mshift, _mwidth),		\
20162306a36Sopenharmony_ci		.common	= {						\
20262306a36Sopenharmony_ci			.reg		= _reg,				\
20362306a36Sopenharmony_ci			.hw.init	= CLK_HW_INIT(_name,		\
20462306a36Sopenharmony_ci						      _parent,		\
20562306a36Sopenharmony_ci						      &ccu_div_ops,	\
20662306a36Sopenharmony_ci						      _flags),		\
20762306a36Sopenharmony_ci		},							\
20862306a36Sopenharmony_ci	}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci#define SUNXI_CCU_M(_struct, _name, _parent, _reg, _mshift, _mwidth,	\
21162306a36Sopenharmony_ci		    _flags)						\
21262306a36Sopenharmony_ci	SUNXI_CCU_M_WITH_GATE(_struct, _name, _parent, _reg,		\
21362306a36Sopenharmony_ci			      _mshift, _mwidth, 0, _flags)
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci#define SUNXI_CCU_M_DATA_WITH_MUX_GATE(_struct, _name, _parents, _reg,	\
21662306a36Sopenharmony_ci				       _mshift, _mwidth,		\
21762306a36Sopenharmony_ci				       _muxshift, _muxwidth,		\
21862306a36Sopenharmony_ci				       _gate, _flags)			\
21962306a36Sopenharmony_ci	struct ccu_div _struct = {					\
22062306a36Sopenharmony_ci		.enable	= _gate,					\
22162306a36Sopenharmony_ci		.div	= _SUNXI_CCU_DIV(_mshift, _mwidth),		\
22262306a36Sopenharmony_ci		.mux	= _SUNXI_CCU_MUX(_muxshift, _muxwidth),		\
22362306a36Sopenharmony_ci		.common	= {						\
22462306a36Sopenharmony_ci			.reg		= _reg,				\
22562306a36Sopenharmony_ci			.hw.init	= CLK_HW_INIT_PARENTS_DATA(_name, \
22662306a36Sopenharmony_ci								   _parents, \
22762306a36Sopenharmony_ci								   &ccu_div_ops, \
22862306a36Sopenharmony_ci								   _flags), \
22962306a36Sopenharmony_ci		},							\
23062306a36Sopenharmony_ci	}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci#define SUNXI_CCU_M_DATA_WITH_MUX(_struct, _name, _parents, _reg,	\
23362306a36Sopenharmony_ci				  _mshift, _mwidth,			\
23462306a36Sopenharmony_ci				  _muxshift, _muxwidth,			\
23562306a36Sopenharmony_ci				  _flags)				\
23662306a36Sopenharmony_ci	SUNXI_CCU_M_DATA_WITH_MUX_GATE(_struct, _name, _parents, _reg,  \
23762306a36Sopenharmony_ci				       _mshift, _mwidth,		\
23862306a36Sopenharmony_ci				       _muxshift, _muxwidth,		\
23962306a36Sopenharmony_ci				       0, _flags)
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci#define SUNXI_CCU_M_HW_WITH_MUX_GATE(_struct, _name, _parents, _reg,	\
24262306a36Sopenharmony_ci				     _mshift, _mwidth, _muxshift, _muxwidth, \
24362306a36Sopenharmony_ci				     _gate, _flags)			\
24462306a36Sopenharmony_ci	struct ccu_div _struct = {					\
24562306a36Sopenharmony_ci		.enable	= _gate,					\
24662306a36Sopenharmony_ci		.div	= _SUNXI_CCU_DIV(_mshift, _mwidth),		\
24762306a36Sopenharmony_ci		.mux	= _SUNXI_CCU_MUX(_muxshift, _muxwidth),		\
24862306a36Sopenharmony_ci		.common	= {						\
24962306a36Sopenharmony_ci			.reg		= _reg,				\
25062306a36Sopenharmony_ci			.hw.init	= CLK_HW_INIT_PARENTS_HW(_name,	\
25162306a36Sopenharmony_ci								 _parents, \
25262306a36Sopenharmony_ci								 &ccu_div_ops, \
25362306a36Sopenharmony_ci								 _flags), \
25462306a36Sopenharmony_ci		},							\
25562306a36Sopenharmony_ci	}
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci#define SUNXI_CCU_M_HWS_WITH_GATE(_struct, _name, _parent, _reg,	\
25862306a36Sopenharmony_ci				  _mshift, _mwidth, _gate,		\
25962306a36Sopenharmony_ci				  _flags)				\
26062306a36Sopenharmony_ci	struct ccu_div _struct = {					\
26162306a36Sopenharmony_ci		.enable	= _gate,					\
26262306a36Sopenharmony_ci		.div	= _SUNXI_CCU_DIV(_mshift, _mwidth),		\
26362306a36Sopenharmony_ci		.common	= {						\
26462306a36Sopenharmony_ci			.reg		= _reg,				\
26562306a36Sopenharmony_ci			.hw.init	= CLK_HW_INIT_HWS(_name,	\
26662306a36Sopenharmony_ci							  _parent,	\
26762306a36Sopenharmony_ci							  &ccu_div_ops,	\
26862306a36Sopenharmony_ci							  _flags),	\
26962306a36Sopenharmony_ci		},							\
27062306a36Sopenharmony_ci	}
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci#define SUNXI_CCU_M_HWS(_struct, _name, _parent, _reg, _mshift,		\
27362306a36Sopenharmony_ci			_mwidth, _flags)				\
27462306a36Sopenharmony_ci	SUNXI_CCU_M_HWS_WITH_GATE(_struct, _name, _parent, _reg,	\
27562306a36Sopenharmony_ci				  _mshift, _mwidth, 0, _flags)
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_cistatic inline struct ccu_div *hw_to_ccu_div(struct clk_hw *hw)
27862306a36Sopenharmony_ci{
27962306a36Sopenharmony_ci	struct ccu_common *common = hw_to_ccu_common(hw);
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci	return container_of(common, struct ccu_div, common);
28262306a36Sopenharmony_ci}
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ciextern const struct clk_ops ccu_div_ops;
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci#endif /* _CCU_DIV_H_ */
287