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_cistruct sck {
118c2ecf20Sopenharmony_ci	char *n;
128c2ecf20Sopenharmony_ci	char *p;
138c2ecf20Sopenharmony_ci	u8 id;
148c2ecf20Sopenharmony_ci};
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistruct pck {
178c2ecf20Sopenharmony_ci	char *n;
188c2ecf20Sopenharmony_ci	u8 id;
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic const struct clk_master_characteristics rm9200_mck_characteristics = {
228c2ecf20Sopenharmony_ci	.output = { .min = 0, .max = 80000000 },
238c2ecf20Sopenharmony_ci	.divisors = { 1, 2, 3, 4 },
248c2ecf20Sopenharmony_ci};
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic u8 rm9200_pll_out[] = { 0, 2 };
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic const struct clk_range rm9200_pll_outputs[] = {
298c2ecf20Sopenharmony_ci	{ .min = 80000000, .max = 160000000 },
308c2ecf20Sopenharmony_ci	{ .min = 150000000, .max = 180000000 },
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic const struct clk_pll_characteristics rm9200_pll_characteristics = {
348c2ecf20Sopenharmony_ci	.input = { .min = 1000000, .max = 32000000 },
358c2ecf20Sopenharmony_ci	.num_output = ARRAY_SIZE(rm9200_pll_outputs),
368c2ecf20Sopenharmony_ci	.output = rm9200_pll_outputs,
378c2ecf20Sopenharmony_ci	.out = rm9200_pll_out,
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic const struct sck at91rm9200_systemck[] = {
418c2ecf20Sopenharmony_ci	{ .n = "udpck", .p = "usbck",    .id = 1 },
428c2ecf20Sopenharmony_ci	{ .n = "uhpck", .p = "usbck",    .id = 4 },
438c2ecf20Sopenharmony_ci	{ .n = "pck0",  .p = "prog0",    .id = 8 },
448c2ecf20Sopenharmony_ci	{ .n = "pck1",  .p = "prog1",    .id = 9 },
458c2ecf20Sopenharmony_ci	{ .n = "pck2",  .p = "prog2",    .id = 10 },
468c2ecf20Sopenharmony_ci	{ .n = "pck3",  .p = "prog3",    .id = 11 },
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic const struct pck at91rm9200_periphck[] = {
508c2ecf20Sopenharmony_ci	{ .n = "pioA_clk",   .id = 2 },
518c2ecf20Sopenharmony_ci	{ .n = "pioB_clk",   .id = 3 },
528c2ecf20Sopenharmony_ci	{ .n = "pioC_clk",   .id = 4 },
538c2ecf20Sopenharmony_ci	{ .n = "pioD_clk",   .id = 5 },
548c2ecf20Sopenharmony_ci	{ .n = "usart0_clk", .id = 6 },
558c2ecf20Sopenharmony_ci	{ .n = "usart1_clk", .id = 7 },
568c2ecf20Sopenharmony_ci	{ .n = "usart2_clk", .id = 8 },
578c2ecf20Sopenharmony_ci	{ .n = "usart3_clk", .id = 9 },
588c2ecf20Sopenharmony_ci	{ .n = "mci0_clk",   .id = 10 },
598c2ecf20Sopenharmony_ci	{ .n = "udc_clk",    .id = 11 },
608c2ecf20Sopenharmony_ci	{ .n = "twi0_clk",   .id = 12 },
618c2ecf20Sopenharmony_ci	{ .n = "spi0_clk",   .id = 13 },
628c2ecf20Sopenharmony_ci	{ .n = "ssc0_clk",   .id = 14 },
638c2ecf20Sopenharmony_ci	{ .n = "ssc1_clk",   .id = 15 },
648c2ecf20Sopenharmony_ci	{ .n = "ssc2_clk",   .id = 16 },
658c2ecf20Sopenharmony_ci	{ .n = "tc0_clk",    .id = 17 },
668c2ecf20Sopenharmony_ci	{ .n = "tc1_clk",    .id = 18 },
678c2ecf20Sopenharmony_ci	{ .n = "tc2_clk",    .id = 19 },
688c2ecf20Sopenharmony_ci	{ .n = "tc3_clk",    .id = 20 },
698c2ecf20Sopenharmony_ci	{ .n = "tc4_clk",    .id = 21 },
708c2ecf20Sopenharmony_ci	{ .n = "tc5_clk",    .id = 22 },
718c2ecf20Sopenharmony_ci	{ .n = "ohci_clk",   .id = 23 },
728c2ecf20Sopenharmony_ci	{ .n = "macb0_clk",  .id = 24 },
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic void __init at91rm9200_pmc_setup(struct device_node *np)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	const char *slowxtal_name, *mainxtal_name;
788c2ecf20Sopenharmony_ci	struct pmc_data *at91rm9200_pmc;
798c2ecf20Sopenharmony_ci	u32 usb_div[] = { 1, 2, 0, 0 };
808c2ecf20Sopenharmony_ci	const char *parent_names[6];
818c2ecf20Sopenharmony_ci	struct regmap *regmap;
828c2ecf20Sopenharmony_ci	struct clk_hw *hw;
838c2ecf20Sopenharmony_ci	int i;
848c2ecf20Sopenharmony_ci	bool bypass;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "slow_xtal");
878c2ecf20Sopenharmony_ci	if (i < 0)
888c2ecf20Sopenharmony_ci		return;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	slowxtal_name = of_clk_get_parent_name(np, i);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	i = of_property_match_string(np, "clock-names", "main_xtal");
938c2ecf20Sopenharmony_ci	if (i < 0)
948c2ecf20Sopenharmony_ci		return;
958c2ecf20Sopenharmony_ci	mainxtal_name = of_clk_get_parent_name(np, i);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	regmap = device_node_to_regmap(np);
988c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
998c2ecf20Sopenharmony_ci		return;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	at91rm9200_pmc = pmc_data_allocate(PMC_PLLBCK + 1,
1028c2ecf20Sopenharmony_ci					    nck(at91rm9200_systemck),
1038c2ecf20Sopenharmony_ci					    nck(at91rm9200_periphck), 0, 4);
1048c2ecf20Sopenharmony_ci	if (!at91rm9200_pmc)
1058c2ecf20Sopenharmony_ci		return;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	bypass = of_property_read_bool(np, "atmel,osc-bypass");
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	hw = at91_clk_register_main_osc(regmap, "main_osc", mainxtal_name,
1108c2ecf20Sopenharmony_ci					bypass);
1118c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1128c2ecf20Sopenharmony_ci		goto err_free;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	hw = at91_clk_register_rm9200_main(regmap, "mainck", "main_osc");
1158c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1168c2ecf20Sopenharmony_ci		goto err_free;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	at91rm9200_pmc->chws[PMC_MAIN] = hw;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	hw = at91_clk_register_pll(regmap, "pllack", "mainck", 0,
1218c2ecf20Sopenharmony_ci				   &at91rm9200_pll_layout,
1228c2ecf20Sopenharmony_ci				   &rm9200_pll_characteristics);
1238c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1248c2ecf20Sopenharmony_ci		goto err_free;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	at91rm9200_pmc->chws[PMC_PLLACK] = hw;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	hw = at91_clk_register_pll(regmap, "pllbck", "mainck", 1,
1298c2ecf20Sopenharmony_ci				   &at91rm9200_pll_layout,
1308c2ecf20Sopenharmony_ci				   &rm9200_pll_characteristics);
1318c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1328c2ecf20Sopenharmony_ci		goto err_free;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	at91rm9200_pmc->chws[PMC_PLLBCK] = hw;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	parent_names[0] = slowxtal_name;
1378c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
1388c2ecf20Sopenharmony_ci	parent_names[2] = "pllack";
1398c2ecf20Sopenharmony_ci	parent_names[3] = "pllbck";
1408c2ecf20Sopenharmony_ci	hw = at91_clk_register_master(regmap, "masterck", 4, parent_names,
1418c2ecf20Sopenharmony_ci				      &at91rm9200_master_layout,
1428c2ecf20Sopenharmony_ci				      &rm9200_mck_characteristics);
1438c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1448c2ecf20Sopenharmony_ci		goto err_free;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	at91rm9200_pmc->chws[PMC_MCK] = hw;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	hw = at91rm9200_clk_register_usb(regmap, "usbck", "pllbck", usb_div);
1498c2ecf20Sopenharmony_ci	if (IS_ERR(hw))
1508c2ecf20Sopenharmony_ci		goto err_free;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	parent_names[0] = slowxtal_name;
1538c2ecf20Sopenharmony_ci	parent_names[1] = "mainck";
1548c2ecf20Sopenharmony_ci	parent_names[2] = "pllack";
1558c2ecf20Sopenharmony_ci	parent_names[3] = "pllbck";
1568c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
1578c2ecf20Sopenharmony_ci		char name[6];
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		snprintf(name, sizeof(name), "prog%d", i);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci		hw = at91_clk_register_programmable(regmap, name,
1628c2ecf20Sopenharmony_ci						    parent_names, 4, i,
1638c2ecf20Sopenharmony_ci						    &at91rm9200_programmable_layout,
1648c2ecf20Sopenharmony_ci						    NULL);
1658c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
1668c2ecf20Sopenharmony_ci			goto err_free;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci		at91rm9200_pmc->pchws[i] = hw;
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(at91rm9200_systemck); i++) {
1728c2ecf20Sopenharmony_ci		hw = at91_clk_register_system(regmap, at91rm9200_systemck[i].n,
1738c2ecf20Sopenharmony_ci					      at91rm9200_systemck[i].p,
1748c2ecf20Sopenharmony_ci					      at91rm9200_systemck[i].id);
1758c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
1768c2ecf20Sopenharmony_ci			goto err_free;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci		at91rm9200_pmc->shws[at91rm9200_systemck[i].id] = hw;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(at91rm9200_periphck); i++) {
1828c2ecf20Sopenharmony_ci		hw = at91_clk_register_peripheral(regmap,
1838c2ecf20Sopenharmony_ci						  at91rm9200_periphck[i].n,
1848c2ecf20Sopenharmony_ci						  "masterck",
1858c2ecf20Sopenharmony_ci						  at91rm9200_periphck[i].id);
1868c2ecf20Sopenharmony_ci		if (IS_ERR(hw))
1878c2ecf20Sopenharmony_ci			goto err_free;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci		at91rm9200_pmc->phws[at91rm9200_periphck[i].id] = hw;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	of_clk_add_hw_provider(np, of_clk_hw_pmc_get, at91rm9200_pmc);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cierr_free:
1978c2ecf20Sopenharmony_ci	kfree(at91rm9200_pmc);
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci/*
2008c2ecf20Sopenharmony_ci * While the TCB can be used as the clocksource, the system timer is most likely
2018c2ecf20Sopenharmony_ci * to be used instead. However, the pinctrl driver doesn't support probe
2028c2ecf20Sopenharmony_ci * deferring properly. Once this is fixed, this can be switched to a platform
2038c2ecf20Sopenharmony_ci * driver.
2048c2ecf20Sopenharmony_ci */
2058c2ecf20Sopenharmony_ciCLK_OF_DECLARE_DRIVER(at91rm9200_pmc, "atmel,at91rm9200-pmc",
2068c2ecf20Sopenharmony_ci		      at91rm9200_pmc_setup);
207