162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Ingenic SoC CGU driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2013-2015 Imagination Technologies
662306a36Sopenharmony_ci * Author: Paul Burton <paul.burton@mips.com>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef __DRIVERS_CLK_INGENIC_CGU_H__
1062306a36Sopenharmony_ci#define __DRIVERS_CLK_INGENIC_CGU_H__
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/bitops.h>
1362306a36Sopenharmony_ci#include <linux/clk-provider.h>
1462306a36Sopenharmony_ci#include <linux/of.h>
1562306a36Sopenharmony_ci#include <linux/spinlock.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/**
1862306a36Sopenharmony_ci * struct ingenic_cgu_pll_info - information about a PLL
1962306a36Sopenharmony_ci * @reg: the offset of the PLL's control register within the CGU
2062306a36Sopenharmony_ci * @rate_multiplier: the multiplier needed by pll rate calculation
2162306a36Sopenharmony_ci * @m_shift: the number of bits to shift the multiplier value by (ie. the
2262306a36Sopenharmony_ci *           index of the lowest bit of the multiplier value in the PLL's
2362306a36Sopenharmony_ci *           control register)
2462306a36Sopenharmony_ci * @m_bits: the size of the multiplier field in bits
2562306a36Sopenharmony_ci * @m_offset: the multiplier value which encodes to 0 in the PLL's control
2662306a36Sopenharmony_ci *            register
2762306a36Sopenharmony_ci * @n_shift: the number of bits to shift the divider value by (ie. the
2862306a36Sopenharmony_ci *           index of the lowest bit of the divider value in the PLL's
2962306a36Sopenharmony_ci *           control register)
3062306a36Sopenharmony_ci * @n_bits: the size of the divider field in bits
3162306a36Sopenharmony_ci * @n_offset: the divider value which encodes to 0 in the PLL's control
3262306a36Sopenharmony_ci *            register
3362306a36Sopenharmony_ci * @od_shift: the number of bits to shift the post-VCO divider value by (ie.
3462306a36Sopenharmony_ci *            the index of the lowest bit of the post-VCO divider value in
3562306a36Sopenharmony_ci *            the PLL's control register)
3662306a36Sopenharmony_ci * @od_bits: the size of the post-VCO divider field in bits, or 0 if no
3762306a36Sopenharmony_ci *	     OD field exists (then the OD is fixed to 1)
3862306a36Sopenharmony_ci * @od_max: the maximum post-VCO divider value
3962306a36Sopenharmony_ci * @od_encoding: a pointer to an array mapping post-VCO divider values to
4062306a36Sopenharmony_ci *               their encoded values in the PLL control register, or -1 for
4162306a36Sopenharmony_ci *               unsupported values
4262306a36Sopenharmony_ci * @bypass_reg: the offset of the bypass control register within the CGU
4362306a36Sopenharmony_ci * @bypass_bit: the index of the bypass bit in the PLL control register, or
4462306a36Sopenharmony_ci *              -1 if there is no bypass bit
4562306a36Sopenharmony_ci * @enable_bit: the index of the enable bit in the PLL control register, or
4662306a36Sopenharmony_ci *		-1 if there is no enable bit (ie, the PLL is always on)
4762306a36Sopenharmony_ci * @stable_bit: the index of the stable bit in the PLL control register, or
4862306a36Sopenharmony_ci *		-1 if there is no stable bit
4962306a36Sopenharmony_ci * @set_rate_hook: hook called immediately after updating the CGU register,
5062306a36Sopenharmony_ci *		   before releasing the spinlock
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_cistruct ingenic_cgu_pll_info {
5362306a36Sopenharmony_ci	unsigned reg;
5462306a36Sopenharmony_ci	unsigned rate_multiplier;
5562306a36Sopenharmony_ci	const s8 *od_encoding;
5662306a36Sopenharmony_ci	u8 m_shift, m_bits, m_offset;
5762306a36Sopenharmony_ci	u8 n_shift, n_bits, n_offset;
5862306a36Sopenharmony_ci	u8 od_shift, od_bits, od_max;
5962306a36Sopenharmony_ci	unsigned bypass_reg;
6062306a36Sopenharmony_ci	s8 bypass_bit;
6162306a36Sopenharmony_ci	s8 enable_bit;
6262306a36Sopenharmony_ci	s8 stable_bit;
6362306a36Sopenharmony_ci	void (*calc_m_n_od)(const struct ingenic_cgu_pll_info *pll_info,
6462306a36Sopenharmony_ci			    unsigned long rate, unsigned long parent_rate,
6562306a36Sopenharmony_ci			    unsigned int *m, unsigned int *n, unsigned int *od);
6662306a36Sopenharmony_ci	void (*set_rate_hook)(const struct ingenic_cgu_pll_info *pll_info,
6762306a36Sopenharmony_ci			      unsigned long rate, unsigned long parent_rate);
6862306a36Sopenharmony_ci};
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/**
7162306a36Sopenharmony_ci * struct ingenic_cgu_mux_info - information about a clock mux
7262306a36Sopenharmony_ci * @reg: offset of the mux control register within the CGU
7362306a36Sopenharmony_ci * @shift: number of bits to shift the mux value by (ie. the index of
7462306a36Sopenharmony_ci *         the lowest bit of the mux value within its control register)
7562306a36Sopenharmony_ci * @bits: the size of the mux value in bits
7662306a36Sopenharmony_ci */
7762306a36Sopenharmony_cistruct ingenic_cgu_mux_info {
7862306a36Sopenharmony_ci	unsigned reg;
7962306a36Sopenharmony_ci	u8 shift;
8062306a36Sopenharmony_ci	u8 bits;
8162306a36Sopenharmony_ci};
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/**
8462306a36Sopenharmony_ci * struct ingenic_cgu_div_info - information about a divider
8562306a36Sopenharmony_ci * @reg: offset of the divider control register within the CGU
8662306a36Sopenharmony_ci * @shift: number of bits to left shift the divide value by (ie. the index of
8762306a36Sopenharmony_ci *         the lowest bit of the divide value within its control register)
8862306a36Sopenharmony_ci * @div: number to divide the divider value by (i.e. if the
8962306a36Sopenharmony_ci *	 effective divider value is the value written to the register
9062306a36Sopenharmony_ci *	 multiplied by some constant)
9162306a36Sopenharmony_ci * @bits: the size of the divide value in bits
9262306a36Sopenharmony_ci * @ce_bit: the index of the change enable bit within reg, or -1 if there
9362306a36Sopenharmony_ci *          isn't one
9462306a36Sopenharmony_ci * @busy_bit: the index of the busy bit within reg, or -1 if there isn't one
9562306a36Sopenharmony_ci * @stop_bit: the index of the stop bit within reg, or -1 if there isn't one
9662306a36Sopenharmony_ci * @bypass_mask: mask of parent clocks for which the divider does not apply
9762306a36Sopenharmony_ci * @div_table: optional table to map the value read from the register to the
9862306a36Sopenharmony_ci *             actual divider value
9962306a36Sopenharmony_ci */
10062306a36Sopenharmony_cistruct ingenic_cgu_div_info {
10162306a36Sopenharmony_ci	unsigned reg;
10262306a36Sopenharmony_ci	u8 shift;
10362306a36Sopenharmony_ci	u8 div;
10462306a36Sopenharmony_ci	u8 bits;
10562306a36Sopenharmony_ci	s8 ce_bit;
10662306a36Sopenharmony_ci	s8 busy_bit;
10762306a36Sopenharmony_ci	s8 stop_bit;
10862306a36Sopenharmony_ci	u8 bypass_mask;
10962306a36Sopenharmony_ci	const u8 *div_table;
11062306a36Sopenharmony_ci};
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci/**
11362306a36Sopenharmony_ci * struct ingenic_cgu_fixdiv_info - information about a fixed divider
11462306a36Sopenharmony_ci * @div: the divider applied to the parent clock
11562306a36Sopenharmony_ci */
11662306a36Sopenharmony_cistruct ingenic_cgu_fixdiv_info {
11762306a36Sopenharmony_ci	unsigned div;
11862306a36Sopenharmony_ci};
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci/**
12162306a36Sopenharmony_ci * struct ingenic_cgu_gate_info - information about a clock gate
12262306a36Sopenharmony_ci * @reg: offset of the gate control register within the CGU
12362306a36Sopenharmony_ci * @bit: offset of the bit in the register that controls the gate
12462306a36Sopenharmony_ci * @clear_to_gate: if set, the clock is gated when the bit is cleared
12562306a36Sopenharmony_ci * @delay_us: delay in microseconds after which the clock is considered stable
12662306a36Sopenharmony_ci */
12762306a36Sopenharmony_cistruct ingenic_cgu_gate_info {
12862306a36Sopenharmony_ci	unsigned reg;
12962306a36Sopenharmony_ci	u8 bit;
13062306a36Sopenharmony_ci	bool clear_to_gate;
13162306a36Sopenharmony_ci	u16 delay_us;
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/**
13562306a36Sopenharmony_ci * struct ingenic_cgu_custom_info - information about a custom (SoC) clock
13662306a36Sopenharmony_ci * @clk_ops: custom clock operation callbacks
13762306a36Sopenharmony_ci */
13862306a36Sopenharmony_cistruct ingenic_cgu_custom_info {
13962306a36Sopenharmony_ci	const struct clk_ops *clk_ops;
14062306a36Sopenharmony_ci};
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci/**
14362306a36Sopenharmony_ci * struct ingenic_cgu_clk_info - information about a clock
14462306a36Sopenharmony_ci * @name: name of the clock
14562306a36Sopenharmony_ci * @type: a bitmask formed from CGU_CLK_* values
14662306a36Sopenharmony_ci * @flags: common clock flags to set on this clock
14762306a36Sopenharmony_ci * @parents: an array of the indices of potential parents of this clock
14862306a36Sopenharmony_ci *           within the clock_info array of the CGU, or -1 in entries
14962306a36Sopenharmony_ci *           which correspond to no valid parent
15062306a36Sopenharmony_ci * @pll: information valid if type includes CGU_CLK_PLL
15162306a36Sopenharmony_ci * @gate: information valid if type includes CGU_CLK_GATE
15262306a36Sopenharmony_ci * @mux: information valid if type includes CGU_CLK_MUX
15362306a36Sopenharmony_ci * @div: information valid if type includes CGU_CLK_DIV
15462306a36Sopenharmony_ci * @fixdiv: information valid if type includes CGU_CLK_FIXDIV
15562306a36Sopenharmony_ci * @custom: information valid if type includes CGU_CLK_CUSTOM
15662306a36Sopenharmony_ci */
15762306a36Sopenharmony_cistruct ingenic_cgu_clk_info {
15862306a36Sopenharmony_ci	const char *name;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	enum {
16162306a36Sopenharmony_ci		CGU_CLK_NONE		= 0,
16262306a36Sopenharmony_ci		CGU_CLK_EXT		= BIT(0),
16362306a36Sopenharmony_ci		CGU_CLK_PLL		= BIT(1),
16462306a36Sopenharmony_ci		CGU_CLK_GATE		= BIT(2),
16562306a36Sopenharmony_ci		CGU_CLK_MUX		= BIT(3),
16662306a36Sopenharmony_ci		CGU_CLK_MUX_GLITCHFREE	= BIT(4),
16762306a36Sopenharmony_ci		CGU_CLK_DIV		= BIT(5),
16862306a36Sopenharmony_ci		CGU_CLK_FIXDIV		= BIT(6),
16962306a36Sopenharmony_ci		CGU_CLK_CUSTOM		= BIT(7),
17062306a36Sopenharmony_ci	} type;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	unsigned long flags;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	int parents[4];
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	union {
17762306a36Sopenharmony_ci		struct ingenic_cgu_pll_info pll;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci		struct {
18062306a36Sopenharmony_ci			struct ingenic_cgu_gate_info gate;
18162306a36Sopenharmony_ci			struct ingenic_cgu_mux_info mux;
18262306a36Sopenharmony_ci			struct ingenic_cgu_div_info div;
18362306a36Sopenharmony_ci			struct ingenic_cgu_fixdiv_info fixdiv;
18462306a36Sopenharmony_ci		};
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci		struct ingenic_cgu_custom_info custom;
18762306a36Sopenharmony_ci	};
18862306a36Sopenharmony_ci};
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci/**
19162306a36Sopenharmony_ci * struct ingenic_cgu - data about the CGU
19262306a36Sopenharmony_ci * @np: the device tree node that caused the CGU to be probed
19362306a36Sopenharmony_ci * @base: the ioremap'ed base address of the CGU registers
19462306a36Sopenharmony_ci * @clock_info: an array containing information about implemented clocks
19562306a36Sopenharmony_ci * @clocks: used to provide clocks to DT, allows lookup of struct clk*
19662306a36Sopenharmony_ci * @lock: lock to be held whilst manipulating CGU registers
19762306a36Sopenharmony_ci */
19862306a36Sopenharmony_cistruct ingenic_cgu {
19962306a36Sopenharmony_ci	struct device_node *np;
20062306a36Sopenharmony_ci	void __iomem *base;
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	const struct ingenic_cgu_clk_info *clock_info;
20362306a36Sopenharmony_ci	struct clk_onecell_data clocks;
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	spinlock_t lock;
20662306a36Sopenharmony_ci};
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci/**
20962306a36Sopenharmony_ci * struct ingenic_clk - private data for a clock
21062306a36Sopenharmony_ci * @hw: see Documentation/driver-api/clk.rst
21162306a36Sopenharmony_ci * @cgu: a pointer to the CGU data
21262306a36Sopenharmony_ci * @idx: the index of this clock in cgu->clock_info
21362306a36Sopenharmony_ci */
21462306a36Sopenharmony_cistruct ingenic_clk {
21562306a36Sopenharmony_ci	struct clk_hw hw;
21662306a36Sopenharmony_ci	struct ingenic_cgu *cgu;
21762306a36Sopenharmony_ci	unsigned idx;
21862306a36Sopenharmony_ci};
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci#define to_ingenic_clk(_hw) container_of(_hw, struct ingenic_clk, hw)
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci/**
22362306a36Sopenharmony_ci * ingenic_cgu_new() - create a new CGU instance
22462306a36Sopenharmony_ci * @clock_info: an array of clock information structures describing the clocks
22562306a36Sopenharmony_ci *              which are implemented by the CGU
22662306a36Sopenharmony_ci * @num_clocks: the number of entries in clock_info
22762306a36Sopenharmony_ci * @np: the device tree node which causes this CGU to be probed
22862306a36Sopenharmony_ci *
22962306a36Sopenharmony_ci * Return: a pointer to the CGU instance if initialisation is successful,
23062306a36Sopenharmony_ci *         otherwise NULL.
23162306a36Sopenharmony_ci */
23262306a36Sopenharmony_cistruct ingenic_cgu *
23362306a36Sopenharmony_ciingenic_cgu_new(const struct ingenic_cgu_clk_info *clock_info,
23462306a36Sopenharmony_ci		unsigned num_clocks, struct device_node *np);
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci/**
23762306a36Sopenharmony_ci * ingenic_cgu_register_clocks() - Registers the clocks
23862306a36Sopenharmony_ci * @cgu: pointer to cgu data
23962306a36Sopenharmony_ci *
24062306a36Sopenharmony_ci * Register the clocks described by the CGU with the common clock framework.
24162306a36Sopenharmony_ci *
24262306a36Sopenharmony_ci * Return: 0 on success or -errno if unsuccesful.
24362306a36Sopenharmony_ci */
24462306a36Sopenharmony_ciint ingenic_cgu_register_clocks(struct ingenic_cgu *cgu);
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci#endif /* __DRIVERS_CLK_INGENIC_CGU_H__ */
247