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 sam9rl_mck_characteristics = {
118c2ecf20Sopenharmony_ci	.output = { .min = 0, .max = 94000000 },
128c2ecf20Sopenharmony_ci	.divisors = { 1, 2, 4, 0 },
138c2ecf20Sopenharmony_ci};
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic u8 sam9rl_plla_out[] = { 0, 2 };
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic const struct clk_range sam9rl_plla_outputs[] = {
188c2ecf20Sopenharmony_ci	{ .min = 80000000, .max = 200000000 },
198c2ecf20Sopenharmony_ci	{ .min = 190000000, .max = 240000000 },
208c2ecf20Sopenharmony_ci};
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic const struct clk_pll_characteristics sam9rl_plla_characteristics = {
238c2ecf20Sopenharmony_ci	.input = { .min = 1000000, .max = 32000000 },
248c2ecf20Sopenharmony_ci	.num_output = ARRAY_SIZE(sam9rl_plla_outputs),
258c2ecf20Sopenharmony_ci	.output = sam9rl_plla_outputs,
268c2ecf20Sopenharmony_ci	.out = sam9rl_plla_out,
278c2ecf20Sopenharmony_ci};
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic const struct {
308c2ecf20Sopenharmony_ci	char *n;
318c2ecf20Sopenharmony_ci	char *p;
328c2ecf20Sopenharmony_ci	u8 id;
338c2ecf20Sopenharmony_ci} at91sam9rl_systemck[] = {
348c2ecf20Sopenharmony_ci	{ .n = "pck0",  .p = "prog0",    .id = 8 },
358c2ecf20Sopenharmony_ci	{ .n = "pck1",  .p = "prog1",    .id = 9 },
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic const struct {
398c2ecf20Sopenharmony_ci	char *n;
408c2ecf20Sopenharmony_ci	u8 id;
418c2ecf20Sopenharmony_ci} at91sam9rl_periphck[] = {
428c2ecf20Sopenharmony_ci	{ .n = "pioA_clk",   .id = 2, },
438c2ecf20Sopenharmony_ci	{ .n = "pioB_clk",   .id = 3, },
448c2ecf20Sopenharmony_ci	{ .n = "pioC_clk",   .id = 4, },
458c2ecf20Sopenharmony_ci	{ .n = "pioD_clk",   .id = 5, },
468c2ecf20Sopenharmony_ci	{ .n = "usart0_clk", .id = 6, },
478c2ecf20Sopenharmony_ci	{ .n = "usart1_clk", .id = 7, },
488c2ecf20Sopenharmony_ci	{ .n = "usart2_clk", .id = 8, },
498c2ecf20Sopenharmony_ci	{ .n = "usart3_clk", .id = 9, },
508c2ecf20Sopenharmony_ci	{ .n = "mci0_clk",   .id = 10, },
518c2ecf20Sopenharmony_ci	{ .n = "twi0_clk",   .id = 11, },
528c2ecf20Sopenharmony_ci	{ .n = "twi1_clk",   .id = 12, },
538c2ecf20Sopenharmony_ci	{ .n = "spi0_clk",   .id = 13, },
548c2ecf20Sopenharmony_ci	{ .n = "ssc0_clk",   .id = 14, },
558c2ecf20Sopenharmony_ci	{ .n = "ssc1_clk",   .id = 15, },
568c2ecf20Sopenharmony_ci	{ .n = "tc0_clk",    .id = 16, },
578c2ecf20Sopenharmony_ci	{ .n = "tc1_clk",    .id = 17, },
588c2ecf20Sopenharmony_ci	{ .n = "tc2_clk",    .id = 18, },
598c2ecf20Sopenharmony_ci	{ .n = "pwm_clk",    .id = 19, },
608c2ecf20Sopenharmony_ci	{ .n = "adc_clk",    .id = 20, },
618c2ecf20Sopenharmony_ci	{ .n = "dma0_clk",   .id = 21, },
628c2ecf20Sopenharmony_ci	{ .n = "udphs_clk",  .id = 22, },
638c2ecf20Sopenharmony_ci	{ .n = "lcd_clk",    .id = 23, },
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic void __init at91sam9rl_pmc_setup(struct device_node *np)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	const char *slck_name, *mainxtal_name;
698c2ecf20Sopenharmony_ci	struct pmc_data *at91sam9rl_pmc;
708c2ecf20Sopenharmony_ci	const char *parent_names[6];
718c2ecf20Sopenharmony_ci	struct regmap *regmap;
728c2ecf20Sopenharmony_ci	struct clk_hw *hw;
738c2ecf20Sopenharmony_ci	int i;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "slow_clk");
768c2ecf20Sopenharmony_ci	if (i < 0)
778c2ecf20Sopenharmony_ci		return;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	slck_name = of_clk_get_parent_name(np, i);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "main_xtal");
828c2ecf20Sopenharmony_ci	if (i < 0)
838c2ecf20Sopenharmony_ci		return;
848c2ecf20Sopenharmony_ci	mainxtal_name = of_clk_get_parent_name(np, i);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	regmap = device_node_to_regmap(np);
878c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
888c2ecf20Sopenharmony_ci		return;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	at91sam9rl_pmc = pmc_data_allocate(PMC_PLLACK + 1,
918c2ecf20Sopenharmony_ci					   nck(at91sam9rl_systemck),
928c2ecf20Sopenharmony_ci					   nck(at91sam9rl_periphck), 0, 2);
938c2ecf20Sopenharmony_ci	if (!at91sam9rl_pmc)
948c2ecf20Sopenharmony_ci		return;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	hw = at91_clk_register_rm9200_main(regmap, "mainck", mainxtal_name);
978c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
988c2ecf20Sopenharmony_ci		goto err_free;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	at91sam9rl_pmc->chws[PMC_MAIN] = hw;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
1038c2ecf20Sopenharmony_ci				   &at91rm9200_pll_layout,
1048c2ecf20Sopenharmony_ci				   &sam9rl_plla_characteristics);
1058c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1068c2ecf20Sopenharmony_ci		goto err_free;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	at91sam9rl_pmc->chws[PMC_PLLACK] = hw;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	hw = at91_clk_register_utmi(regmap, NULL, "utmick", "mainck");
1118c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1128c2ecf20Sopenharmony_ci		goto err_free;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	at91sam9rl_pmc->chws[PMC_UTMI] = hw;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	parent_names[0] = slck_name;
1178c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
1188c2ecf20Sopenharmony_ci	parent_names[2] = "pllack";
1198c2ecf20Sopenharmony_ci	parent_names[3] = "utmick";
1208c2ecf20Sopenharmony_ci	hw = at91_clk_register_master(regmap, "masterck", 4, parent_names,
1218c2ecf20Sopenharmony_ci				      &at91rm9200_master_layout,
1228c2ecf20Sopenharmony_ci				      &sam9rl_mck_characteristics);
1238c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1248c2ecf20Sopenharmony_ci		goto err_free;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	at91sam9rl_pmc->chws[PMC_MCK] = hw;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	parent_names[0] = slck_name;
1298c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
1308c2ecf20Sopenharmony_ci	parent_names[2] = "pllack";
1318c2ecf20Sopenharmony_ci	parent_names[3] = "utmick";
1328c2ecf20Sopenharmony_ci	parent_names[4] = "masterck";
1338c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
1348c2ecf20Sopenharmony_ci		char name[6];
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci		snprintf(name, sizeof(name), "prog%d", i);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		hw = at91_clk_register_programmable(regmap, name,
1398c2ecf20Sopenharmony_ci						    parent_names, 5, i,
1408c2ecf20Sopenharmony_ci						    &at91rm9200_programmable_layout,
1418c2ecf20Sopenharmony_ci						    NULL);
1428c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
1438c2ecf20Sopenharmony_ci			goto err_free;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		at91sam9rl_pmc->pchws[i] = hw;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(at91sam9rl_systemck); i++) {
1498c2ecf20Sopenharmony_ci		hw = at91_clk_register_system(regmap, at91sam9rl_systemck[i].n,
1508c2ecf20Sopenharmony_ci					      at91sam9rl_systemck[i].p,
1518c2ecf20Sopenharmony_ci					      at91sam9rl_systemck[i].id);
1528c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
1538c2ecf20Sopenharmony_ci			goto err_free;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		at91sam9rl_pmc->shws[at91sam9rl_systemck[i].id] = hw;
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(at91sam9rl_periphck); i++) {
1598c2ecf20Sopenharmony_ci		hw = at91_clk_register_peripheral(regmap,
1608c2ecf20Sopenharmony_ci						  at91sam9rl_periphck[i].n,
1618c2ecf20Sopenharmony_ci						  "masterck",
1628c2ecf20Sopenharmony_ci						  at91sam9rl_periphck[i].id);
1638c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
1648c2ecf20Sopenharmony_ci			goto err_free;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci		at91sam9rl_pmc->phws[at91sam9rl_periphck[i].id] = hw;
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	of_clk_add_hw_provider(np, of_clk_hw_pmc_get, at91sam9rl_pmc);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	return;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cierr_free:
1748c2ecf20Sopenharmony_ci	kfree(at91sam9rl_pmc);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ciCLK_OF_DECLARE_DRIVER(at91sam9rl_pmc, "atmel,at91sam9rl-pmc", at91sam9rl_pmc_setup);
177