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/slab.h>
10 #include <linux/export.h>
13 * This is a thin library to solve the problem of how to portably allocate
14 * one of the TC blocks. For simplicity, it doesn't currently expect to
15 * share individual timers between different drivers.
18 #if defined(CONFIG_AVR32)
19 /* AVR32 has these divide PBB */
20 const u8 atmel_tc_divisors
[5] = { 0, 4, 8, 16, 32, };
21 EXPORT_SYMBOL(atmel_tc_divisors
);
23 #elif defined(CONFIG_ARCH_AT91)
24 /* AT91 has these divide MCK */
25 const u8 atmel_tc_divisors
[5] = { 2, 8, 32, 128, 0, };
26 EXPORT_SYMBOL(atmel_tc_divisors
);
30 static DEFINE_SPINLOCK(tc_list_lock
);
31 static LIST_HEAD(tc_list
);
34 * atmel_tc_alloc - allocate a specified TC block
35 * @block: which block to allocate
36 * @name: name to be associated with the iomem resource
38 * Caller allocates a block. If it is available, a pointer to a
39 * pre-initialized struct atmel_tc is returned. The caller can access
40 * the registers directly through the "regs" field.
42 struct atmel_tc
*atmel_tc_alloc(unsigned block
, const char *name
)
45 struct platform_device
*pdev
= NULL
;
49 spin_lock(&tc_list_lock
);
50 list_for_each_entry(tc
, &tc_list
, node
) {
51 if (tc
->pdev
->id
== block
) {
57 if (!pdev
|| tc
->iomem
)
60 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
64 size
= resource_size(r
);
65 r
= request_mem_region(r
->start
, size
, name
);
69 tc
->regs
= ioremap(r
->start
, size
);
76 spin_unlock(&tc_list_lock
);
80 release_mem_region(r
->start
, size
);
85 EXPORT_SYMBOL_GPL(atmel_tc_alloc
);
88 * atmel_tc_free - release a specified TC block
89 * @tc: Timer/counter block that was returned by atmel_tc_alloc()
91 * This reverses the effect of atmel_tc_alloc(), unmapping the I/O
92 * registers, invalidating the resource returned by that routine and
93 * making the TC available to other drivers.
95 void atmel_tc_free(struct atmel_tc
*tc
)
97 spin_lock(&tc_list_lock
);
100 release_mem_region(tc
->iomem
->start
, resource_size(tc
->iomem
));
104 spin_unlock(&tc_list_lock
);
106 EXPORT_SYMBOL_GPL(atmel_tc_free
);
108 static int __init
tc_probe(struct platform_device
*pdev
)
114 if (!platform_get_resource(pdev
, IORESOURCE_MEM
, 0))
117 irq
= platform_get_irq(pdev
, 0);
121 tc
= kzalloc(sizeof(struct atmel_tc
), GFP_KERNEL
);
127 clk
= clk_get(&pdev
->dev
, "t0_clk");
134 tc
->clk
[1] = clk_get(&pdev
->dev
, "t1_clk");
135 if (IS_ERR(tc
->clk
[1]))
137 tc
->clk
[2] = clk_get(&pdev
->dev
, "t2_clk");
138 if (IS_ERR(tc
->clk
[2]))
142 tc
->irq
[1] = platform_get_irq(pdev
, 1);
145 tc
->irq
[2] = platform_get_irq(pdev
, 2);
149 spin_lock(&tc_list_lock
);
150 list_add_tail(&tc
->node
, &tc_list
);
151 spin_unlock(&tc_list_lock
);
156 static struct platform_driver tc_driver
= {
157 .driver
.name
= "atmel_tcb",
160 static int __init
tc_init(void)
162 return platform_driver_probe(&tc_driver
, tc_probe
);
164 arch_initcall(tc_init
);