1 #include <linux/atmel_tc.h>
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>
15 * This is a thin library to solve the problem of how to portably allocate
16 * one of the TC blocks. For simplicity, it doesn't currently expect to
17 * share individual timers between different drivers.
20 #if defined(CONFIG_AVR32)
21 /* AVR32 has these divide PBB */
22 const u8 atmel_tc_divisors[5] = { 0, 4, 8, 16, 32, };
23 EXPORT_SYMBOL(atmel_tc_divisors);
25 #elif defined(CONFIG_ARCH_AT91)
26 /* AT91 has these divide MCK */
27 const u8 atmel_tc_divisors[5] = { 2, 8, 32, 128, 0, };
28 EXPORT_SYMBOL(atmel_tc_divisors);
32 static DEFINE_SPINLOCK(tc_list_lock);
33 static LIST_HEAD(tc_list);
36 * atmel_tc_alloc - allocate a specified TC block
37 * @block: which block to allocate
38 * @name: name to be associated with the iomem resource
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, const char *name)
47 struct platform_device *pdev = NULL;
51 spin_lock(&tc_list_lock);
52 list_for_each_entry(tc, &tc_list, node) {
53 if (tc->pdev->dev.of_node) {
54 if (of_alias_get_id(tc->pdev->dev.of_node, "tcb")
59 } else if (tc->pdev->id == block) {
65 if (!pdev || tc->iomem)
68 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
72 size = resource_size(r);
73 r = request_mem_region(r->start, size, name);
77 tc->regs = ioremap(r->start, size);
84 spin_unlock(&tc_list_lock);
88 release_mem_region(r->start, size);
93 EXPORT_SYMBOL_GPL(atmel_tc_alloc);
96 * atmel_tc_free - release a specified TC block
97 * @tc: Timer/counter block that was returned by atmel_tc_alloc()
99 * This reverses the effect of atmel_tc_alloc(), unmapping the I/O
100 * registers, invalidating the resource returned by that routine and
101 * making the TC available to other drivers.
103 void atmel_tc_free(struct atmel_tc *tc)
105 spin_lock(&tc_list_lock);
108 release_mem_region(tc->iomem->start, resource_size(tc->iomem));
112 spin_unlock(&tc_list_lock);
114 EXPORT_SYMBOL_GPL(atmel_tc_free);
116 #if defined(CONFIG_OF)
117 static struct atmel_tcb_config tcb_rm9200_config = {
121 static struct atmel_tcb_config tcb_sam9x5_config = {
125 static const struct of_device_id atmel_tcb_dt_ids[] = {
127 .compatible = "atmel,at91rm9200-tcb",
128 .data = &tcb_rm9200_config,
130 .compatible = "atmel,at91sam9x5-tcb",
131 .data = &tcb_sam9x5_config,
137 MODULE_DEVICE_TABLE(of, atmel_tcb_dt_ids);
140 static int __init tc_probe(struct platform_device *pdev)
146 if (!platform_get_resource(pdev, IORESOURCE_MEM, 0))
149 irq = platform_get_irq(pdev, 0);
153 tc = kzalloc(sizeof(struct atmel_tc), GFP_KERNEL);
159 clk = clk_get(&pdev->dev, "t0_clk");
165 /* Now take SoC information if available */
166 if (pdev->dev.of_node) {
167 const struct of_device_id *match;
168 match = of_match_node(atmel_tcb_dt_ids, pdev->dev.of_node);
170 tc->tcb_config = match->data;
174 tc->clk[1] = clk_get(&pdev->dev, "t1_clk");
175 if (IS_ERR(tc->clk[1]))
177 tc->clk[2] = clk_get(&pdev->dev, "t2_clk");
178 if (IS_ERR(tc->clk[2]))
182 tc->irq[1] = platform_get_irq(pdev, 1);
185 tc->irq[2] = platform_get_irq(pdev, 2);
189 spin_lock(&tc_list_lock);
190 list_add_tail(&tc->node, &tc_list);
191 spin_unlock(&tc_list_lock);
196 static struct platform_driver tc_driver = {
199 .of_match_table = of_match_ptr(atmel_tcb_dt_ids),
203 static int __init tc_init(void)
205 return platform_driver_probe(&tc_driver, tc_probe);
207 arch_initcall(tc_init);