18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/clk.h>
38c2ecf20Sopenharmony_ci#include <linux/err.h>
48c2ecf20Sopenharmony_ci#include <linux/init.h>
58c2ecf20Sopenharmony_ci#include <linux/io.h>
68c2ecf20Sopenharmony_ci#include <linux/ioport.h>
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/export.h>
128c2ecf20Sopenharmony_ci#include <linux/of.h>
138c2ecf20Sopenharmony_ci#include <soc/at91/atmel_tcb.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * This is a thin library to solve the problem of how to portably allocate
178c2ecf20Sopenharmony_ci * one of the TC blocks.  For simplicity, it doesn't currently expect to
188c2ecf20Sopenharmony_ci * share individual timers between different drivers.
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#if defined(CONFIG_AVR32)
228c2ecf20Sopenharmony_ci/* AVR32 has these divide PBB */
238c2ecf20Sopenharmony_ciconst u8 atmel_tc_divisors[5] = { 0, 4, 8, 16, 32, };
248c2ecf20Sopenharmony_ciEXPORT_SYMBOL(atmel_tc_divisors);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#elif defined(CONFIG_ARCH_AT91)
278c2ecf20Sopenharmony_ci/* AT91 has these divide MCK */
288c2ecf20Sopenharmony_ciconst u8 atmel_tc_divisors[5] = { 2, 8, 32, 128, 0, };
298c2ecf20Sopenharmony_ciEXPORT_SYMBOL(atmel_tc_divisors);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#endif
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(tc_list_lock);
348c2ecf20Sopenharmony_cistatic LIST_HEAD(tc_list);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/**
378c2ecf20Sopenharmony_ci * atmel_tc_alloc - allocate a specified TC block
388c2ecf20Sopenharmony_ci * @block: which block to allocate
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * Caller allocates a block.  If it is available, a pointer to a
418c2ecf20Sopenharmony_ci * pre-initialized struct atmel_tc is returned. The caller can access
428c2ecf20Sopenharmony_ci * the registers directly through the "regs" field.
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_cistruct atmel_tc *atmel_tc_alloc(unsigned block)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	struct atmel_tc		*tc;
478c2ecf20Sopenharmony_ci	struct platform_device	*pdev = NULL;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	spin_lock(&tc_list_lock);
508c2ecf20Sopenharmony_ci	list_for_each_entry(tc, &tc_list, node) {
518c2ecf20Sopenharmony_ci		if (tc->allocated)
528c2ecf20Sopenharmony_ci			continue;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci		if ((tc->pdev->dev.of_node && tc->id == block) ||
558c2ecf20Sopenharmony_ci		    (tc->pdev->id == block)) {
568c2ecf20Sopenharmony_ci			pdev = tc->pdev;
578c2ecf20Sopenharmony_ci			tc->allocated = true;
588c2ecf20Sopenharmony_ci			break;
598c2ecf20Sopenharmony_ci		}
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci	spin_unlock(&tc_list_lock);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return pdev ? tc : NULL;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(atmel_tc_alloc);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/**
688c2ecf20Sopenharmony_ci * atmel_tc_free - release a specified TC block
698c2ecf20Sopenharmony_ci * @tc: Timer/counter block that was returned by atmel_tc_alloc()
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * This reverses the effect of atmel_tc_alloc(), invalidating the resource
728c2ecf20Sopenharmony_ci * returned by that routine and making the TC available to other drivers.
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_civoid atmel_tc_free(struct atmel_tc *tc)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	spin_lock(&tc_list_lock);
778c2ecf20Sopenharmony_ci	if (tc->allocated)
788c2ecf20Sopenharmony_ci		tc->allocated = false;
798c2ecf20Sopenharmony_ci	spin_unlock(&tc_list_lock);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(atmel_tc_free);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#if defined(CONFIG_OF)
848c2ecf20Sopenharmony_cistatic struct atmel_tcb_config tcb_rm9200_config = {
858c2ecf20Sopenharmony_ci	.counter_width = 16,
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic struct atmel_tcb_config tcb_sam9x5_config = {
898c2ecf20Sopenharmony_ci	.counter_width = 32,
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic const struct of_device_id atmel_tcb_dt_ids[] = {
938c2ecf20Sopenharmony_ci	{
948c2ecf20Sopenharmony_ci		.compatible = "atmel,at91rm9200-tcb",
958c2ecf20Sopenharmony_ci		.data = &tcb_rm9200_config,
968c2ecf20Sopenharmony_ci	}, {
978c2ecf20Sopenharmony_ci		.compatible = "atmel,at91sam9x5-tcb",
988c2ecf20Sopenharmony_ci		.data = &tcb_sam9x5_config,
998c2ecf20Sopenharmony_ci	}, {
1008c2ecf20Sopenharmony_ci		/* sentinel */
1018c2ecf20Sopenharmony_ci	}
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, atmel_tcb_dt_ids);
1058c2ecf20Sopenharmony_ci#endif
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic int __init tc_probe(struct platform_device *pdev)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct atmel_tc *tc;
1108c2ecf20Sopenharmony_ci	struct clk	*clk;
1118c2ecf20Sopenharmony_ci	int		irq;
1128c2ecf20Sopenharmony_ci	unsigned int	i;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	if (of_get_child_count(pdev->dev.of_node))
1158c2ecf20Sopenharmony_ci		return -EBUSY;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
1188c2ecf20Sopenharmony_ci	if (irq < 0)
1198c2ecf20Sopenharmony_ci		return -EINVAL;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	tc = devm_kzalloc(&pdev->dev, sizeof(struct atmel_tc), GFP_KERNEL);
1228c2ecf20Sopenharmony_ci	if (!tc)
1238c2ecf20Sopenharmony_ci		return -ENOMEM;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	tc->pdev = pdev;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	clk = devm_clk_get(&pdev->dev, "t0_clk");
1288c2ecf20Sopenharmony_ci	if (IS_ERR(clk))
1298c2ecf20Sopenharmony_ci		return PTR_ERR(clk);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	tc->slow_clk = devm_clk_get(&pdev->dev, "slow_clk");
1328c2ecf20Sopenharmony_ci	if (IS_ERR(tc->slow_clk))
1338c2ecf20Sopenharmony_ci		return PTR_ERR(tc->slow_clk);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	tc->regs = devm_platform_ioremap_resource(pdev, 0);
1368c2ecf20Sopenharmony_ci	if (IS_ERR(tc->regs))
1378c2ecf20Sopenharmony_ci		return PTR_ERR(tc->regs);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/* Now take SoC information if available */
1408c2ecf20Sopenharmony_ci	if (pdev->dev.of_node) {
1418c2ecf20Sopenharmony_ci		const struct of_device_id *match;
1428c2ecf20Sopenharmony_ci		match = of_match_node(atmel_tcb_dt_ids, pdev->dev.of_node);
1438c2ecf20Sopenharmony_ci		if (match)
1448c2ecf20Sopenharmony_ci			tc->tcb_config = match->data;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci		tc->id = of_alias_get_id(tc->pdev->dev.of_node, "tcb");
1478c2ecf20Sopenharmony_ci	} else {
1488c2ecf20Sopenharmony_ci		tc->id = pdev->id;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	tc->clk[0] = clk;
1528c2ecf20Sopenharmony_ci	tc->clk[1] = devm_clk_get(&pdev->dev, "t1_clk");
1538c2ecf20Sopenharmony_ci	if (IS_ERR(tc->clk[1]))
1548c2ecf20Sopenharmony_ci		tc->clk[1] = clk;
1558c2ecf20Sopenharmony_ci	tc->clk[2] = devm_clk_get(&pdev->dev, "t2_clk");
1568c2ecf20Sopenharmony_ci	if (IS_ERR(tc->clk[2]))
1578c2ecf20Sopenharmony_ci		tc->clk[2] = clk;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	tc->irq[0] = irq;
1608c2ecf20Sopenharmony_ci	tc->irq[1] = platform_get_irq(pdev, 1);
1618c2ecf20Sopenharmony_ci	if (tc->irq[1] < 0)
1628c2ecf20Sopenharmony_ci		tc->irq[1] = irq;
1638c2ecf20Sopenharmony_ci	tc->irq[2] = platform_get_irq(pdev, 2);
1648c2ecf20Sopenharmony_ci	if (tc->irq[2] < 0)
1658c2ecf20Sopenharmony_ci		tc->irq[2] = irq;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++)
1688c2ecf20Sopenharmony_ci		writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	spin_lock(&tc_list_lock);
1718c2ecf20Sopenharmony_ci	list_add_tail(&tc->node, &tc_list);
1728c2ecf20Sopenharmony_ci	spin_unlock(&tc_list_lock);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, tc);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	return 0;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic void tc_shutdown(struct platform_device *pdev)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	int i;
1828c2ecf20Sopenharmony_ci	struct atmel_tc *tc = platform_get_drvdata(pdev);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	for (i = 0; i < 3; i++)
1858c2ecf20Sopenharmony_ci		writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic struct platform_driver tc_driver = {
1898c2ecf20Sopenharmony_ci	.driver = {
1908c2ecf20Sopenharmony_ci		.name	= "atmel_tcb",
1918c2ecf20Sopenharmony_ci		.of_match_table	= of_match_ptr(atmel_tcb_dt_ids),
1928c2ecf20Sopenharmony_ci	},
1938c2ecf20Sopenharmony_ci	.shutdown = tc_shutdown,
1948c2ecf20Sopenharmony_ci};
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic int __init tc_init(void)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	return platform_driver_probe(&tc_driver, tc_probe);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ciarch_initcall(tc_init);
201