18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
38c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
48c2ecf20Sopenharmony_ci#include <linux/slab.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <dt-bindings/clock/at91.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "pmc.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic const struct clk_master_characteristics mck_characteristics = {
118c2ecf20Sopenharmony_ci	.output = { .min = 124000000, .max = 166000000 },
128c2ecf20Sopenharmony_ci	.divisors = { 1, 2, 4, 3 },
138c2ecf20Sopenharmony_ci};
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic u8 plla_out[] = { 0 };
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic u16 plla_icpll[] = { 0 };
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic const struct clk_range plla_outputs[] = {
208c2ecf20Sopenharmony_ci	{ .min = 600000000, .max = 1200000000 },
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic const struct clk_pll_characteristics plla_characteristics = {
248c2ecf20Sopenharmony_ci	.input = { .min = 12000000, .max = 24000000 },
258c2ecf20Sopenharmony_ci	.num_output = ARRAY_SIZE(plla_outputs),
268c2ecf20Sopenharmony_ci	.output = plla_outputs,
278c2ecf20Sopenharmony_ci	.icpll = plla_icpll,
288c2ecf20Sopenharmony_ci	.out = plla_out,
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic const struct clk_pcr_layout sama5d2_pcr_layout = {
328c2ecf20Sopenharmony_ci	.offset = 0x10c,
338c2ecf20Sopenharmony_ci	.cmd = BIT(12),
348c2ecf20Sopenharmony_ci	.gckcss_mask = GENMASK(10, 8),
358c2ecf20Sopenharmony_ci	.pid_mask = GENMASK(6, 0),
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic const struct {
398c2ecf20Sopenharmony_ci	char *n;
408c2ecf20Sopenharmony_ci	char *p;
418c2ecf20Sopenharmony_ci	u8 id;
428c2ecf20Sopenharmony_ci} sama5d2_systemck[] = {
438c2ecf20Sopenharmony_ci	{ .n = "ddrck", .p = "masterck", .id = 2 },
448c2ecf20Sopenharmony_ci	{ .n = "lcdck", .p = "masterck", .id = 3 },
458c2ecf20Sopenharmony_ci	{ .n = "uhpck", .p = "usbck",    .id = 6 },
468c2ecf20Sopenharmony_ci	{ .n = "udpck", .p = "usbck",    .id = 7 },
478c2ecf20Sopenharmony_ci	{ .n = "pck0",  .p = "prog0",    .id = 8 },
488c2ecf20Sopenharmony_ci	{ .n = "pck1",  .p = "prog1",    .id = 9 },
498c2ecf20Sopenharmony_ci	{ .n = "pck2",  .p = "prog2",    .id = 10 },
508c2ecf20Sopenharmony_ci	{ .n = "iscck", .p = "masterck", .id = 18 },
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic const struct {
548c2ecf20Sopenharmony_ci	char *n;
558c2ecf20Sopenharmony_ci	u8 id;
568c2ecf20Sopenharmony_ci	struct clk_range r;
578c2ecf20Sopenharmony_ci} sama5d2_periph32ck[] = {
588c2ecf20Sopenharmony_ci	{ .n = "macb0_clk",   .id = 5,  .r = { .min = 0, .max = 83000000 }, },
598c2ecf20Sopenharmony_ci	{ .n = "tdes_clk",    .id = 11, .r = { .min = 0, .max = 83000000 }, },
608c2ecf20Sopenharmony_ci	{ .n = "matrix1_clk", .id = 14, },
618c2ecf20Sopenharmony_ci	{ .n = "hsmc_clk",    .id = 17, },
628c2ecf20Sopenharmony_ci	{ .n = "pioA_clk",    .id = 18, .r = { .min = 0, .max = 83000000 }, },
638c2ecf20Sopenharmony_ci	{ .n = "flx0_clk",    .id = 19, .r = { .min = 0, .max = 83000000 }, },
648c2ecf20Sopenharmony_ci	{ .n = "flx1_clk",    .id = 20, .r = { .min = 0, .max = 83000000 }, },
658c2ecf20Sopenharmony_ci	{ .n = "flx2_clk",    .id = 21, .r = { .min = 0, .max = 83000000 }, },
668c2ecf20Sopenharmony_ci	{ .n = "flx3_clk",    .id = 22, .r = { .min = 0, .max = 83000000 }, },
678c2ecf20Sopenharmony_ci	{ .n = "flx4_clk",    .id = 23, .r = { .min = 0, .max = 83000000 }, },
688c2ecf20Sopenharmony_ci	{ .n = "uart0_clk",   .id = 24, .r = { .min = 0, .max = 83000000 }, },
698c2ecf20Sopenharmony_ci	{ .n = "uart1_clk",   .id = 25, .r = { .min = 0, .max = 83000000 }, },
708c2ecf20Sopenharmony_ci	{ .n = "uart2_clk",   .id = 26, .r = { .min = 0, .max = 83000000 }, },
718c2ecf20Sopenharmony_ci	{ .n = "uart3_clk",   .id = 27, .r = { .min = 0, .max = 83000000 }, },
728c2ecf20Sopenharmony_ci	{ .n = "uart4_clk",   .id = 28, .r = { .min = 0, .max = 83000000 }, },
738c2ecf20Sopenharmony_ci	{ .n = "twi0_clk",    .id = 29, .r = { .min = 0, .max = 83000000 }, },
748c2ecf20Sopenharmony_ci	{ .n = "twi1_clk",    .id = 30, .r = { .min = 0, .max = 83000000 }, },
758c2ecf20Sopenharmony_ci	{ .n = "spi0_clk",    .id = 33, .r = { .min = 0, .max = 83000000 }, },
768c2ecf20Sopenharmony_ci	{ .n = "spi1_clk",    .id = 34, .r = { .min = 0, .max = 83000000 }, },
778c2ecf20Sopenharmony_ci	{ .n = "tcb0_clk",    .id = 35, .r = { .min = 0, .max = 83000000 }, },
788c2ecf20Sopenharmony_ci	{ .n = "tcb1_clk",    .id = 36, .r = { .min = 0, .max = 83000000 }, },
798c2ecf20Sopenharmony_ci	{ .n = "pwm_clk",     .id = 38, .r = { .min = 0, .max = 83000000 }, },
808c2ecf20Sopenharmony_ci	{ .n = "adc_clk",     .id = 40, .r = { .min = 0, .max = 83000000 }, },
818c2ecf20Sopenharmony_ci	{ .n = "uhphs_clk",   .id = 41, .r = { .min = 0, .max = 83000000 }, },
828c2ecf20Sopenharmony_ci	{ .n = "udphs_clk",   .id = 42, .r = { .min = 0, .max = 83000000 }, },
838c2ecf20Sopenharmony_ci	{ .n = "ssc0_clk",    .id = 43, .r = { .min = 0, .max = 83000000 }, },
848c2ecf20Sopenharmony_ci	{ .n = "ssc1_clk",    .id = 44, .r = { .min = 0, .max = 83000000 }, },
858c2ecf20Sopenharmony_ci	{ .n = "trng_clk",    .id = 47, .r = { .min = 0, .max = 83000000 }, },
868c2ecf20Sopenharmony_ci	{ .n = "pdmic_clk",   .id = 48, .r = { .min = 0, .max = 83000000 }, },
878c2ecf20Sopenharmony_ci	{ .n = "securam_clk", .id = 51, },
888c2ecf20Sopenharmony_ci	{ .n = "i2s0_clk",    .id = 54, .r = { .min = 0, .max = 83000000 }, },
898c2ecf20Sopenharmony_ci	{ .n = "i2s1_clk",    .id = 55, .r = { .min = 0, .max = 83000000 }, },
908c2ecf20Sopenharmony_ci	{ .n = "can0_clk",    .id = 56, .r = { .min = 0, .max = 83000000 }, },
918c2ecf20Sopenharmony_ci	{ .n = "can1_clk",    .id = 57, .r = { .min = 0, .max = 83000000 }, },
928c2ecf20Sopenharmony_ci	{ .n = "ptc_clk",     .id = 58, .r = { .min = 0, .max = 83000000 }, },
938c2ecf20Sopenharmony_ci	{ .n = "classd_clk",  .id = 59, .r = { .min = 0, .max = 83000000 }, },
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic const struct {
978c2ecf20Sopenharmony_ci	char *n;
988c2ecf20Sopenharmony_ci	u8 id;
998c2ecf20Sopenharmony_ci} sama5d2_periphck[] = {
1008c2ecf20Sopenharmony_ci	{ .n = "dma0_clk",    .id = 6, },
1018c2ecf20Sopenharmony_ci	{ .n = "dma1_clk",    .id = 7, },
1028c2ecf20Sopenharmony_ci	{ .n = "aes_clk",     .id = 9, },
1038c2ecf20Sopenharmony_ci	{ .n = "aesb_clk",    .id = 10, },
1048c2ecf20Sopenharmony_ci	{ .n = "sha_clk",     .id = 12, },
1058c2ecf20Sopenharmony_ci	{ .n = "mpddr_clk",   .id = 13, },
1068c2ecf20Sopenharmony_ci	{ .n = "matrix0_clk", .id = 15, },
1078c2ecf20Sopenharmony_ci	{ .n = "sdmmc0_hclk", .id = 31, },
1088c2ecf20Sopenharmony_ci	{ .n = "sdmmc1_hclk", .id = 32, },
1098c2ecf20Sopenharmony_ci	{ .n = "lcdc_clk",    .id = 45, },
1108c2ecf20Sopenharmony_ci	{ .n = "isc_clk",     .id = 46, },
1118c2ecf20Sopenharmony_ci	{ .n = "qspi0_clk",   .id = 52, },
1128c2ecf20Sopenharmony_ci	{ .n = "qspi1_clk",   .id = 53, },
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic const struct {
1168c2ecf20Sopenharmony_ci	char *n;
1178c2ecf20Sopenharmony_ci	u8 id;
1188c2ecf20Sopenharmony_ci	struct clk_range r;
1198c2ecf20Sopenharmony_ci	int chg_pid;
1208c2ecf20Sopenharmony_ci} sama5d2_gck[] = {
1218c2ecf20Sopenharmony_ci	{ .n = "sdmmc0_gclk", .id = 31, .chg_pid = INT_MIN, },
1228c2ecf20Sopenharmony_ci	{ .n = "sdmmc1_gclk", .id = 32, .chg_pid = INT_MIN, },
1238c2ecf20Sopenharmony_ci	{ .n = "tcb0_gclk",   .id = 35, .chg_pid = INT_MIN, .r = { .min = 0, .max = 83000000 }, },
1248c2ecf20Sopenharmony_ci	{ .n = "tcb1_gclk",   .id = 36, .chg_pid = INT_MIN, .r = { .min = 0, .max = 83000000 }, },
1258c2ecf20Sopenharmony_ci	{ .n = "pwm_gclk",    .id = 38, .chg_pid = INT_MIN, .r = { .min = 0, .max = 83000000 }, },
1268c2ecf20Sopenharmony_ci	{ .n = "isc_gclk",    .id = 46, .chg_pid = INT_MIN, },
1278c2ecf20Sopenharmony_ci	{ .n = "pdmic_gclk",  .id = 48, .chg_pid = INT_MIN, },
1288c2ecf20Sopenharmony_ci	{ .n = "i2s0_gclk",   .id = 54, .chg_pid = 5, },
1298c2ecf20Sopenharmony_ci	{ .n = "i2s1_gclk",   .id = 55, .chg_pid = 5, },
1308c2ecf20Sopenharmony_ci	{ .n = "can0_gclk",   .id = 56, .chg_pid = INT_MIN, .r = { .min = 0, .max = 80000000 }, },
1318c2ecf20Sopenharmony_ci	{ .n = "can1_gclk",   .id = 57, .chg_pid = INT_MIN, .r = { .min = 0, .max = 80000000 }, },
1328c2ecf20Sopenharmony_ci	{ .n = "classd_gclk", .id = 59, .chg_pid = 5, .r = { .min = 0, .max = 100000000 }, },
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic const struct clk_programmable_layout sama5d2_programmable_layout = {
1368c2ecf20Sopenharmony_ci	.pres_mask = 0xff,
1378c2ecf20Sopenharmony_ci	.pres_shift = 4,
1388c2ecf20Sopenharmony_ci	.css_mask = 0x7,
1398c2ecf20Sopenharmony_ci	.have_slck_mck = 0,
1408c2ecf20Sopenharmony_ci	.is_pres_direct = 1,
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic void __init sama5d2_pmc_setup(struct device_node *np)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	struct clk_range range = CLK_RANGE(0, 0);
1468c2ecf20Sopenharmony_ci	const char *slck_name, *mainxtal_name;
1478c2ecf20Sopenharmony_ci	struct pmc_data *sama5d2_pmc;
1488c2ecf20Sopenharmony_ci	const char *parent_names[6];
1498c2ecf20Sopenharmony_ci	struct regmap *regmap, *regmap_sfr;
1508c2ecf20Sopenharmony_ci	struct clk_hw *hw;
1518c2ecf20Sopenharmony_ci	int i;
1528c2ecf20Sopenharmony_ci	bool bypass;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "slow_clk");
1558c2ecf20Sopenharmony_ci	if (i < 0)
1568c2ecf20Sopenharmony_ci		return;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	slck_name = of_clk_get_parent_name(np, i);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "main_xtal");
1618c2ecf20Sopenharmony_ci	if (i < 0)
1628c2ecf20Sopenharmony_ci		return;
1638c2ecf20Sopenharmony_ci	mainxtal_name = of_clk_get_parent_name(np, i);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	regmap = device_node_to_regmap(np);
1668c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
1678c2ecf20Sopenharmony_ci		return;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	sama5d2_pmc = pmc_data_allocate(PMC_AUDIOPLLCK + 1,
1708c2ecf20Sopenharmony_ci					nck(sama5d2_systemck),
1718c2ecf20Sopenharmony_ci					nck(sama5d2_periph32ck),
1728c2ecf20Sopenharmony_ci					nck(sama5d2_gck), 3);
1738c2ecf20Sopenharmony_ci	if (!sama5d2_pmc)
1748c2ecf20Sopenharmony_ci		return;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	hw = at91_clk_register_main_rc_osc(regmap, "main_rc_osc", 12000000,
1778c2ecf20Sopenharmony_ci					   100000000);
1788c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1798c2ecf20Sopenharmony_ci		goto err_free;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	bypass = of_property_read_bool(np, "atmel,osc-bypass");
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name,
1848c2ecf20Sopenharmony_ci					bypass);
1858c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1868c2ecf20Sopenharmony_ci		goto err_free;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	parent_names[0] = "main_rc_osc";
1898c2ecf20Sopenharmony_ci	parent_names[1] = "main_osc";
1908c2ecf20Sopenharmony_ci	hw = at91_clk_register_sam9x5_main(regmap, "mainck", parent_names, 2);
1918c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1928c2ecf20Sopenharmony_ci		goto err_free;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	sama5d2_pmc->chws[PMC_MAIN] = hw;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
1978c2ecf20Sopenharmony_ci				   &sama5d3_pll_layout, &plla_characteristics);
1988c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1998c2ecf20Sopenharmony_ci		goto err_free;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	hw = at91_clk_register_plldiv(regmap, "plladivck", "pllack");
2028c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
2038c2ecf20Sopenharmony_ci		goto err_free;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	sama5d2_pmc->chws[PMC_PLLACK] = hw;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	hw = at91_clk_register_audio_pll_frac(regmap, "audiopll_fracck",
2088c2ecf20Sopenharmony_ci					      "mainck");
2098c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
2108c2ecf20Sopenharmony_ci		goto err_free;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	hw = at91_clk_register_audio_pll_pad(regmap, "audiopll_padck",
2138c2ecf20Sopenharmony_ci					     "audiopll_fracck");
2148c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
2158c2ecf20Sopenharmony_ci		goto err_free;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	hw = at91_clk_register_audio_pll_pmc(regmap, "audiopll_pmcck",
2188c2ecf20Sopenharmony_ci					     "audiopll_fracck");
2198c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
2208c2ecf20Sopenharmony_ci		goto err_free;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	sama5d2_pmc->chws[PMC_AUDIOPLLCK] = hw;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	regmap_sfr = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
2258c2ecf20Sopenharmony_ci	if (IS_ERR(regmap_sfr))
2268c2ecf20Sopenharmony_ci		regmap_sfr = NULL;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	hw = at91_clk_register_utmi(regmap, regmap_sfr, "utmick", "mainck");
2298c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
2308c2ecf20Sopenharmony_ci		goto err_free;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	sama5d2_pmc->chws[PMC_UTMI] = hw;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	parent_names[0] = slck_name;
2358c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
2368c2ecf20Sopenharmony_ci	parent_names[2] = "plladivck";
2378c2ecf20Sopenharmony_ci	parent_names[3] = "utmick";
2388c2ecf20Sopenharmony_ci	hw = at91_clk_register_master(regmap, "masterck", 4, parent_names,
2398c2ecf20Sopenharmony_ci				      &at91sam9x5_master_layout,
2408c2ecf20Sopenharmony_ci				      &mck_characteristics);
2418c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
2428c2ecf20Sopenharmony_ci		goto err_free;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	sama5d2_pmc->chws[PMC_MCK] = hw;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	hw = at91_clk_register_h32mx(regmap, "h32mxck", "masterck");
2478c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
2488c2ecf20Sopenharmony_ci		goto err_free;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	sama5d2_pmc->chws[PMC_MCK2] = hw;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	parent_names[0] = "plladivck";
2538c2ecf20Sopenharmony_ci	parent_names[1] = "utmick";
2548c2ecf20Sopenharmony_ci	hw = at91sam9x5_clk_register_usb(regmap, "usbck", parent_names, 2);
2558c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
2568c2ecf20Sopenharmony_ci		goto err_free;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	parent_names[0] = slck_name;
2598c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
2608c2ecf20Sopenharmony_ci	parent_names[2] = "plladivck";
2618c2ecf20Sopenharmony_ci	parent_names[3] = "utmick";
2628c2ecf20Sopenharmony_ci	parent_names[4] = "masterck";
2638c2ecf20Sopenharmony_ci	parent_names[5] = "audiopll_pmcck";
2648c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++) {
2658c2ecf20Sopenharmony_ci		char name[6];
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci		snprintf(name, sizeof(name), "prog%d", i);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci		hw = at91_clk_register_programmable(regmap, name,
2708c2ecf20Sopenharmony_ci						    parent_names, 6, i,
2718c2ecf20Sopenharmony_ci						    &sama5d2_programmable_layout,
2728c2ecf20Sopenharmony_ci						    NULL);
2738c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
2748c2ecf20Sopenharmony_ci			goto err_free;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci		sama5d2_pmc->pchws[i] = hw;
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d2_systemck); i++) {
2808c2ecf20Sopenharmony_ci		hw = at91_clk_register_system(regmap, sama5d2_systemck[i].n,
2818c2ecf20Sopenharmony_ci					      sama5d2_systemck[i].p,
2828c2ecf20Sopenharmony_ci					      sama5d2_systemck[i].id);
2838c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
2848c2ecf20Sopenharmony_ci			goto err_free;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci		sama5d2_pmc->shws[sama5d2_systemck[i].id] = hw;
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d2_periphck); i++) {
2908c2ecf20Sopenharmony_ci		hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
2918c2ecf20Sopenharmony_ci							 &sama5d2_pcr_layout,
2928c2ecf20Sopenharmony_ci							 sama5d2_periphck[i].n,
2938c2ecf20Sopenharmony_ci							 "masterck",
2948c2ecf20Sopenharmony_ci							 sama5d2_periphck[i].id,
2958c2ecf20Sopenharmony_ci							 &range, INT_MIN);
2968c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
2978c2ecf20Sopenharmony_ci			goto err_free;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci		sama5d2_pmc->phws[sama5d2_periphck[i].id] = hw;
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d2_periph32ck); i++) {
3038c2ecf20Sopenharmony_ci		hw = at91_clk_register_sam9x5_peripheral(regmap, &pmc_pcr_lock,
3048c2ecf20Sopenharmony_ci							 &sama5d2_pcr_layout,
3058c2ecf20Sopenharmony_ci							 sama5d2_periph32ck[i].n,
3068c2ecf20Sopenharmony_ci							 "h32mxck",
3078c2ecf20Sopenharmony_ci							 sama5d2_periph32ck[i].id,
3088c2ecf20Sopenharmony_ci							 &sama5d2_periph32ck[i].r,
3098c2ecf20Sopenharmony_ci							 INT_MIN);
3108c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
3118c2ecf20Sopenharmony_ci			goto err_free;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci		sama5d2_pmc->phws[sama5d2_periph32ck[i].id] = hw;
3148c2ecf20Sopenharmony_ci	}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	parent_names[0] = slck_name;
3178c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
3188c2ecf20Sopenharmony_ci	parent_names[2] = "plladivck";
3198c2ecf20Sopenharmony_ci	parent_names[3] = "utmick";
3208c2ecf20Sopenharmony_ci	parent_names[4] = "masterck";
3218c2ecf20Sopenharmony_ci	parent_names[5] = "audiopll_pmcck";
3228c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(sama5d2_gck); i++) {
3238c2ecf20Sopenharmony_ci		hw = at91_clk_register_generated(regmap, &pmc_pcr_lock,
3248c2ecf20Sopenharmony_ci						 &sama5d2_pcr_layout,
3258c2ecf20Sopenharmony_ci						 sama5d2_gck[i].n,
3268c2ecf20Sopenharmony_ci						 parent_names, NULL, 6,
3278c2ecf20Sopenharmony_ci						 sama5d2_gck[i].id,
3288c2ecf20Sopenharmony_ci						 &sama5d2_gck[i].r,
3298c2ecf20Sopenharmony_ci						 sama5d2_gck[i].chg_pid);
3308c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
3318c2ecf20Sopenharmony_ci			goto err_free;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci		sama5d2_pmc->ghws[sama5d2_gck[i].id] = hw;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (regmap_sfr) {
3378c2ecf20Sopenharmony_ci		parent_names[0] = "i2s0_clk";
3388c2ecf20Sopenharmony_ci		parent_names[1] = "i2s0_gclk";
3398c2ecf20Sopenharmony_ci		hw = at91_clk_i2s_mux_register(regmap_sfr, "i2s0_muxclk",
3408c2ecf20Sopenharmony_ci					       parent_names, 2, 0);
3418c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
3428c2ecf20Sopenharmony_ci			goto err_free;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci		sama5d2_pmc->chws[PMC_I2S0_MUX] = hw;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci		parent_names[0] = "i2s1_clk";
3478c2ecf20Sopenharmony_ci		parent_names[1] = "i2s1_gclk";
3488c2ecf20Sopenharmony_ci		hw = at91_clk_i2s_mux_register(regmap_sfr, "i2s1_muxclk",
3498c2ecf20Sopenharmony_ci					       parent_names, 2, 1);
3508c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
3518c2ecf20Sopenharmony_ci			goto err_free;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci		sama5d2_pmc->chws[PMC_I2S1_MUX] = hw;
3548c2ecf20Sopenharmony_ci	}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	of_clk_add_hw_provider(np, of_clk_hw_pmc_get, sama5d2_pmc);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	return;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cierr_free:
3618c2ecf20Sopenharmony_ci	kfree(sama5d2_pmc);
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ciCLK_OF_DECLARE_DRIVER(sama5d2_pmc, "atmel,sama5d2-pmc", sama5d2_pmc_setup);
364