1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Ingenic SoC CGU driver 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * Copyright (c) 2013-2015 Imagination Technologies 6*4882a593Smuzhiyun * Author: Paul Burton <paul.burton@mips.com> 7*4882a593Smuzhiyun */ 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun #ifndef __DRIVERS_CLK_INGENIC_CGU_H__ 10*4882a593Smuzhiyun #define __DRIVERS_CLK_INGENIC_CGU_H__ 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun #include <linux/bitops.h> 13*4882a593Smuzhiyun #include <linux/clk-provider.h> 14*4882a593Smuzhiyun #include <linux/of.h> 15*4882a593Smuzhiyun #include <linux/spinlock.h> 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun /** 18*4882a593Smuzhiyun * struct ingenic_cgu_pll_info - information about a PLL 19*4882a593Smuzhiyun * @reg: the offset of the PLL's control register within the CGU 20*4882a593Smuzhiyun * @rate_multiplier: the multiplier needed by pll rate calculation 21*4882a593Smuzhiyun * @m_shift: the number of bits to shift the multiplier value by (ie. the 22*4882a593Smuzhiyun * index of the lowest bit of the multiplier value in the PLL's 23*4882a593Smuzhiyun * control register) 24*4882a593Smuzhiyun * @m_bits: the size of the multiplier field in bits 25*4882a593Smuzhiyun * @m_offset: the multiplier value which encodes to 0 in the PLL's control 26*4882a593Smuzhiyun * register 27*4882a593Smuzhiyun * @n_shift: the number of bits to shift the divider value by (ie. the 28*4882a593Smuzhiyun * index of the lowest bit of the divider value in the PLL's 29*4882a593Smuzhiyun * control register) 30*4882a593Smuzhiyun * @n_bits: the size of the divider field in bits 31*4882a593Smuzhiyun * @n_offset: the divider value which encodes to 0 in the PLL's control 32*4882a593Smuzhiyun * register 33*4882a593Smuzhiyun * @od_shift: the number of bits to shift the post-VCO divider value by (ie. 34*4882a593Smuzhiyun * the index of the lowest bit of the post-VCO divider value in 35*4882a593Smuzhiyun * the PLL's control register) 36*4882a593Smuzhiyun * @od_bits: the size of the post-VCO divider field in bits 37*4882a593Smuzhiyun * @od_max: the maximum post-VCO divider value 38*4882a593Smuzhiyun * @od_encoding: a pointer to an array mapping post-VCO divider values to 39*4882a593Smuzhiyun * their encoded values in the PLL control register, or -1 for 40*4882a593Smuzhiyun * unsupported values 41*4882a593Smuzhiyun * @bypass_reg: the offset of the bypass control register within the CGU 42*4882a593Smuzhiyun * @bypass_bit: the index of the bypass bit in the PLL control register 43*4882a593Smuzhiyun * @enable_bit: the index of the enable bit in the PLL control register 44*4882a593Smuzhiyun * @stable_bit: the index of the stable bit in the PLL control register 45*4882a593Smuzhiyun * @no_bypass_bit: if set, the PLL has no bypass functionality 46*4882a593Smuzhiyun */ 47*4882a593Smuzhiyun struct ingenic_cgu_pll_info { 48*4882a593Smuzhiyun unsigned reg; 49*4882a593Smuzhiyun unsigned rate_multiplier; 50*4882a593Smuzhiyun const s8 *od_encoding; 51*4882a593Smuzhiyun u8 m_shift, m_bits, m_offset; 52*4882a593Smuzhiyun u8 n_shift, n_bits, n_offset; 53*4882a593Smuzhiyun u8 od_shift, od_bits, od_max; 54*4882a593Smuzhiyun unsigned bypass_reg; 55*4882a593Smuzhiyun u8 bypass_bit; 56*4882a593Smuzhiyun u8 enable_bit; 57*4882a593Smuzhiyun u8 stable_bit; 58*4882a593Smuzhiyun bool no_bypass_bit; 59*4882a593Smuzhiyun }; 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun /** 62*4882a593Smuzhiyun * struct ingenic_cgu_mux_info - information about a clock mux 63*4882a593Smuzhiyun * @reg: offset of the mux control register within the CGU 64*4882a593Smuzhiyun * @shift: number of bits to shift the mux value by (ie. the index of 65*4882a593Smuzhiyun * the lowest bit of the mux value within its control register) 66*4882a593Smuzhiyun * @bits: the size of the mux value in bits 67*4882a593Smuzhiyun */ 68*4882a593Smuzhiyun struct ingenic_cgu_mux_info { 69*4882a593Smuzhiyun unsigned reg; 70*4882a593Smuzhiyun u8 shift; 71*4882a593Smuzhiyun u8 bits; 72*4882a593Smuzhiyun }; 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun /** 75*4882a593Smuzhiyun * struct ingenic_cgu_div_info - information about a divider 76*4882a593Smuzhiyun * @reg: offset of the divider control register within the CGU 77*4882a593Smuzhiyun * @shift: number of bits to left shift the divide value by (ie. the index of 78*4882a593Smuzhiyun * the lowest bit of the divide value within its control register) 79*4882a593Smuzhiyun * @div: number to divide the divider value by (i.e. if the 80*4882a593Smuzhiyun * effective divider value is the value written to the register 81*4882a593Smuzhiyun * multiplied by some constant) 82*4882a593Smuzhiyun * @bits: the size of the divide value in bits 83*4882a593Smuzhiyun * @ce_bit: the index of the change enable bit within reg, or -1 if there 84*4882a593Smuzhiyun * isn't one 85*4882a593Smuzhiyun * @busy_bit: the index of the busy bit within reg, or -1 if there isn't one 86*4882a593Smuzhiyun * @stop_bit: the index of the stop bit within reg, or -1 if there isn't one 87*4882a593Smuzhiyun * @div_table: optional table to map the value read from the register to the 88*4882a593Smuzhiyun * actual divider value 89*4882a593Smuzhiyun */ 90*4882a593Smuzhiyun struct ingenic_cgu_div_info { 91*4882a593Smuzhiyun unsigned reg; 92*4882a593Smuzhiyun u8 shift; 93*4882a593Smuzhiyun u8 div; 94*4882a593Smuzhiyun u8 bits; 95*4882a593Smuzhiyun s8 ce_bit; 96*4882a593Smuzhiyun s8 busy_bit; 97*4882a593Smuzhiyun s8 stop_bit; 98*4882a593Smuzhiyun const u8 *div_table; 99*4882a593Smuzhiyun }; 100*4882a593Smuzhiyun 101*4882a593Smuzhiyun /** 102*4882a593Smuzhiyun * struct ingenic_cgu_fixdiv_info - information about a fixed divider 103*4882a593Smuzhiyun * @div: the divider applied to the parent clock 104*4882a593Smuzhiyun */ 105*4882a593Smuzhiyun struct ingenic_cgu_fixdiv_info { 106*4882a593Smuzhiyun unsigned div; 107*4882a593Smuzhiyun }; 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun /** 110*4882a593Smuzhiyun * struct ingenic_cgu_gate_info - information about a clock gate 111*4882a593Smuzhiyun * @reg: offset of the gate control register within the CGU 112*4882a593Smuzhiyun * @bit: offset of the bit in the register that controls the gate 113*4882a593Smuzhiyun * @clear_to_gate: if set, the clock is gated when the bit is cleared 114*4882a593Smuzhiyun * @delay_us: delay in microseconds after which the clock is considered stable 115*4882a593Smuzhiyun */ 116*4882a593Smuzhiyun struct ingenic_cgu_gate_info { 117*4882a593Smuzhiyun unsigned reg; 118*4882a593Smuzhiyun u8 bit; 119*4882a593Smuzhiyun bool clear_to_gate; 120*4882a593Smuzhiyun u16 delay_us; 121*4882a593Smuzhiyun }; 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun /** 124*4882a593Smuzhiyun * struct ingenic_cgu_custom_info - information about a custom (SoC) clock 125*4882a593Smuzhiyun * @clk_ops: custom clock operation callbacks 126*4882a593Smuzhiyun */ 127*4882a593Smuzhiyun struct ingenic_cgu_custom_info { 128*4882a593Smuzhiyun const struct clk_ops *clk_ops; 129*4882a593Smuzhiyun }; 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun /** 132*4882a593Smuzhiyun * struct ingenic_cgu_clk_info - information about a clock 133*4882a593Smuzhiyun * @name: name of the clock 134*4882a593Smuzhiyun * @type: a bitmask formed from CGU_CLK_* values 135*4882a593Smuzhiyun * @parents: an array of the indices of potential parents of this clock 136*4882a593Smuzhiyun * within the clock_info array of the CGU, or -1 in entries 137*4882a593Smuzhiyun * which correspond to no valid parent 138*4882a593Smuzhiyun * @pll: information valid if type includes CGU_CLK_PLL 139*4882a593Smuzhiyun * @gate: information valid if type includes CGU_CLK_GATE 140*4882a593Smuzhiyun * @mux: information valid if type includes CGU_CLK_MUX 141*4882a593Smuzhiyun * @div: information valid if type includes CGU_CLK_DIV 142*4882a593Smuzhiyun * @fixdiv: information valid if type includes CGU_CLK_FIXDIV 143*4882a593Smuzhiyun * @custom: information valid if type includes CGU_CLK_CUSTOM 144*4882a593Smuzhiyun */ 145*4882a593Smuzhiyun struct ingenic_cgu_clk_info { 146*4882a593Smuzhiyun const char *name; 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun enum { 149*4882a593Smuzhiyun CGU_CLK_NONE = 0, 150*4882a593Smuzhiyun CGU_CLK_EXT = BIT(0), 151*4882a593Smuzhiyun CGU_CLK_PLL = BIT(1), 152*4882a593Smuzhiyun CGU_CLK_GATE = BIT(2), 153*4882a593Smuzhiyun CGU_CLK_MUX = BIT(3), 154*4882a593Smuzhiyun CGU_CLK_MUX_GLITCHFREE = BIT(4), 155*4882a593Smuzhiyun CGU_CLK_DIV = BIT(5), 156*4882a593Smuzhiyun CGU_CLK_FIXDIV = BIT(6), 157*4882a593Smuzhiyun CGU_CLK_CUSTOM = BIT(7), 158*4882a593Smuzhiyun } type; 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun int parents[4]; 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun union { 163*4882a593Smuzhiyun struct ingenic_cgu_pll_info pll; 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun struct { 166*4882a593Smuzhiyun struct ingenic_cgu_gate_info gate; 167*4882a593Smuzhiyun struct ingenic_cgu_mux_info mux; 168*4882a593Smuzhiyun struct ingenic_cgu_div_info div; 169*4882a593Smuzhiyun struct ingenic_cgu_fixdiv_info fixdiv; 170*4882a593Smuzhiyun }; 171*4882a593Smuzhiyun 172*4882a593Smuzhiyun struct ingenic_cgu_custom_info custom; 173*4882a593Smuzhiyun }; 174*4882a593Smuzhiyun }; 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun /** 177*4882a593Smuzhiyun * struct ingenic_cgu - data about the CGU 178*4882a593Smuzhiyun * @np: the device tree node that caused the CGU to be probed 179*4882a593Smuzhiyun * @base: the ioremap'ed base address of the CGU registers 180*4882a593Smuzhiyun * @clock_info: an array containing information about implemented clocks 181*4882a593Smuzhiyun * @clocks: used to provide clocks to DT, allows lookup of struct clk* 182*4882a593Smuzhiyun * @lock: lock to be held whilst manipulating CGU registers 183*4882a593Smuzhiyun */ 184*4882a593Smuzhiyun struct ingenic_cgu { 185*4882a593Smuzhiyun struct device_node *np; 186*4882a593Smuzhiyun void __iomem *base; 187*4882a593Smuzhiyun 188*4882a593Smuzhiyun const struct ingenic_cgu_clk_info *clock_info; 189*4882a593Smuzhiyun struct clk_onecell_data clocks; 190*4882a593Smuzhiyun 191*4882a593Smuzhiyun spinlock_t lock; 192*4882a593Smuzhiyun }; 193*4882a593Smuzhiyun 194*4882a593Smuzhiyun /** 195*4882a593Smuzhiyun * struct ingenic_clk - private data for a clock 196*4882a593Smuzhiyun * @hw: see Documentation/driver-api/clk.rst 197*4882a593Smuzhiyun * @cgu: a pointer to the CGU data 198*4882a593Smuzhiyun * @idx: the index of this clock in cgu->clock_info 199*4882a593Smuzhiyun */ 200*4882a593Smuzhiyun struct ingenic_clk { 201*4882a593Smuzhiyun struct clk_hw hw; 202*4882a593Smuzhiyun struct ingenic_cgu *cgu; 203*4882a593Smuzhiyun unsigned idx; 204*4882a593Smuzhiyun }; 205*4882a593Smuzhiyun 206*4882a593Smuzhiyun #define to_ingenic_clk(_hw) container_of(_hw, struct ingenic_clk, hw) 207*4882a593Smuzhiyun 208*4882a593Smuzhiyun /** 209*4882a593Smuzhiyun * ingenic_cgu_new() - create a new CGU instance 210*4882a593Smuzhiyun * @clock_info: an array of clock information structures describing the clocks 211*4882a593Smuzhiyun * which are implemented by the CGU 212*4882a593Smuzhiyun * @num_clocks: the number of entries in clock_info 213*4882a593Smuzhiyun * @np: the device tree node which causes this CGU to be probed 214*4882a593Smuzhiyun * 215*4882a593Smuzhiyun * Return: a pointer to the CGU instance if initialisation is successful, 216*4882a593Smuzhiyun * otherwise NULL. 217*4882a593Smuzhiyun */ 218*4882a593Smuzhiyun struct ingenic_cgu * 219*4882a593Smuzhiyun ingenic_cgu_new(const struct ingenic_cgu_clk_info *clock_info, 220*4882a593Smuzhiyun unsigned num_clocks, struct device_node *np); 221*4882a593Smuzhiyun 222*4882a593Smuzhiyun /** 223*4882a593Smuzhiyun * ingenic_cgu_register_clocks() - Registers the clocks 224*4882a593Smuzhiyun * @cgu: pointer to cgu data 225*4882a593Smuzhiyun * 226*4882a593Smuzhiyun * Register the clocks described by the CGU with the common clock framework. 227*4882a593Smuzhiyun * 228*4882a593Smuzhiyun * Return: 0 on success or -errno if unsuccesful. 229*4882a593Smuzhiyun */ 230*4882a593Smuzhiyun int ingenic_cgu_register_clocks(struct ingenic_cgu *cgu); 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun #endif /* __DRIVERS_CLK_INGENIC_CGU_H__ */ 233