1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/init.h>
6 #include <linux/ioport.h>
7 #include <linux/kernel.h>
8 #include <linux/platform_device.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/export.h>
13 #include <soc/at91/atmel_tcb.h>
16 * This is a thin library to solve the problem of how to portably allocate
17 * one of the TC blocks. For simplicity, it doesn't currently expect to
18 * share individual timers between different drivers.
21 #if defined(CONFIG_AVR32)
22 /* AVR32 has these divide PBB */
23 const u8 atmel_tc_divisors[5] = { 0, 4, 8, 16, 32, };
24 EXPORT_SYMBOL(atmel_tc_divisors);
26 #elif defined(CONFIG_ARCH_AT91)
27 /* AT91 has these divide MCK */
28 const u8 atmel_tc_divisors[5] = { 2, 8, 32, 128, 0, };
29 EXPORT_SYMBOL(atmel_tc_divisors);
33 static DEFINE_SPINLOCK(tc_list_lock);
34 static LIST_HEAD(tc_list);
37 * atmel_tc_alloc - allocate a specified TC block
38 * @block: which block to allocate
40 * Caller allocates a block. If it is available, a pointer to a
41 * pre-initialized struct atmel_tc is returned. The caller can access
42 * the registers directly through the "regs" field.
44 struct atmel_tc *atmel_tc_alloc(unsigned block)
47 struct platform_device *pdev = NULL;
49 spin_lock(&tc_list_lock);
50 list_for_each_entry(tc, &tc_list, node) {
54 if ((tc->pdev->dev.of_node && tc->id == block) ||
55 (tc->pdev->id == block)) {
61 spin_unlock(&tc_list_lock);
63 return pdev ? tc : NULL;
65 EXPORT_SYMBOL_GPL(atmel_tc_alloc);
68 * atmel_tc_free - release a specified TC block
69 * @tc: Timer/counter block that was returned by atmel_tc_alloc()
71 * This reverses the effect of atmel_tc_alloc(), invalidating the resource
72 * returned by that routine and making the TC available to other drivers.
74 void atmel_tc_free(struct atmel_tc *tc)
76 spin_lock(&tc_list_lock);
78 tc->allocated = false;
79 spin_unlock(&tc_list_lock);
81 EXPORT_SYMBOL_GPL(atmel_tc_free);
83 #if defined(CONFIG_OF)
84 static struct atmel_tcb_config tcb_rm9200_config = {
88 static struct atmel_tcb_config tcb_sam9x5_config = {
92 static const struct of_device_id atmel_tcb_dt_ids[] = {
94 .compatible = "atmel,at91rm9200-tcb",
95 .data = &tcb_rm9200_config,
97 .compatible = "atmel,at91sam9x5-tcb",
98 .data = &tcb_sam9x5_config,
104 MODULE_DEVICE_TABLE(of, atmel_tcb_dt_ids);
107 static int __init tc_probe(struct platform_device *pdev)
115 if (of_get_child_count(pdev->dev.of_node))
118 irq = platform_get_irq(pdev, 0);
122 tc = devm_kzalloc(&pdev->dev, sizeof(struct atmel_tc), GFP_KERNEL);
128 clk = devm_clk_get(&pdev->dev, "t0_clk");
132 tc->slow_clk = devm_clk_get(&pdev->dev, "slow_clk");
133 if (IS_ERR(tc->slow_clk))
134 return PTR_ERR(tc->slow_clk);
136 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
137 tc->regs = devm_ioremap_resource(&pdev->dev, r);
138 if (IS_ERR(tc->regs))
139 return PTR_ERR(tc->regs);
141 /* Now take SoC information if available */
142 if (pdev->dev.of_node) {
143 const struct of_device_id *match;
144 match = of_match_node(atmel_tcb_dt_ids, pdev->dev.of_node);
146 tc->tcb_config = match->data;
148 tc->id = of_alias_get_id(tc->pdev->dev.of_node, "tcb");
154 tc->clk[1] = devm_clk_get(&pdev->dev, "t1_clk");
155 if (IS_ERR(tc->clk[1]))
157 tc->clk[2] = devm_clk_get(&pdev->dev, "t2_clk");
158 if (IS_ERR(tc->clk[2]))
162 tc->irq[1] = platform_get_irq(pdev, 1);
165 tc->irq[2] = platform_get_irq(pdev, 2);
169 for (i = 0; i < 3; i++)
170 writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
172 spin_lock(&tc_list_lock);
173 list_add_tail(&tc->node, &tc_list);
174 spin_unlock(&tc_list_lock);
176 platform_set_drvdata(pdev, tc);
181 static void tc_shutdown(struct platform_device *pdev)
184 struct atmel_tc *tc = platform_get_drvdata(pdev);
186 for (i = 0; i < 3; i++)
187 writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
190 static struct platform_driver tc_driver = {
193 .of_match_table = of_match_ptr(atmel_tcb_dt_ids),
195 .shutdown = tc_shutdown,
198 static int __init tc_init(void)
200 return platform_driver_probe(&tc_driver, tc_probe);
202 arch_initcall(tc_init);