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>
12 /* Number of bytes to reserve for the iomem resource */
13 #define ATMEL_TC_IOMEM_SIZE 256
17 * This is a thin library to solve the problem of how to portably allocate
18 * one of the TC blocks. For simplicity, it doesn't currently expect to
19 * share individual timers between different drivers.
22 #if defined(CONFIG_AVR32)
23 /* AVR32 has these divide PBB */
24 const u8 atmel_tc_divisors
[5] = { 0, 4, 8, 16, 32, };
25 EXPORT_SYMBOL(atmel_tc_divisors
);
27 #elif defined(CONFIG_ARCH_AT91)
28 /* AT91 has these divide MCK */
29 const u8 atmel_tc_divisors
[5] = { 2, 8, 32, 128, 0, };
30 EXPORT_SYMBOL(atmel_tc_divisors
);
34 static DEFINE_SPINLOCK(tc_list_lock
);
35 static LIST_HEAD(tc_list
);
38 * atmel_tc_alloc - allocate a specified TC block
39 * @block: which block to allocate
40 * @name: name to be associated with the iomem resource
42 * Caller allocates a block. If it is available, a pointer to a
43 * pre-initialized struct atmel_tc is returned. The caller can access
44 * the registers directly through the "regs" field.
46 struct atmel_tc
*atmel_tc_alloc(unsigned block
, const char *name
)
49 struct platform_device
*pdev
= NULL
;
52 spin_lock(&tc_list_lock
);
53 list_for_each_entry(tc
, &tc_list
, node
) {
54 if (tc
->pdev
->id
== block
) {
60 if (!pdev
|| tc
->iomem
)
63 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
64 r
= request_mem_region(r
->start
, ATMEL_TC_IOMEM_SIZE
, name
);
68 tc
->regs
= ioremap(r
->start
, ATMEL_TC_IOMEM_SIZE
);
75 spin_unlock(&tc_list_lock
);
79 release_mem_region(r
->start
, ATMEL_TC_IOMEM_SIZE
);
84 EXPORT_SYMBOL_GPL(atmel_tc_alloc
);
87 * atmel_tc_free - release a specified TC block
88 * @tc: Timer/counter block that was returned by atmel_tc_alloc()
90 * This reverses the effect of atmel_tc_alloc(), unmapping the I/O
91 * registers, invalidating the resource returned by that routine and
92 * making the TC available to other drivers.
94 void atmel_tc_free(struct atmel_tc
*tc
)
96 spin_lock(&tc_list_lock
);
99 release_mem_region(tc
->iomem
->start
, ATMEL_TC_IOMEM_SIZE
);
103 spin_unlock(&tc_list_lock
);
105 EXPORT_SYMBOL_GPL(atmel_tc_free
);
107 static int __init
tc_probe(struct platform_device
*pdev
)
113 if (!platform_get_resource(pdev
, IORESOURCE_MEM
, 0))
116 irq
= platform_get_irq(pdev
, 0);
120 tc
= kzalloc(sizeof(struct atmel_tc
), GFP_KERNEL
);
126 clk
= clk_get(&pdev
->dev
, "t0_clk");
133 tc
->clk
[1] = clk_get(&pdev
->dev
, "t1_clk");
134 if (IS_ERR(tc
->clk
[1]))
136 tc
->clk
[2] = clk_get(&pdev
->dev
, "t2_clk");
137 if (IS_ERR(tc
->clk
[2]))
141 tc
->irq
[1] = platform_get_irq(pdev
, 1);
144 tc
->irq
[2] = platform_get_irq(pdev
, 2);
148 spin_lock(&tc_list_lock
);
149 list_add_tail(&tc
->node
, &tc_list
);
150 spin_unlock(&tc_list_lock
);
155 static struct platform_driver tc_driver
= {
156 .driver
.name
= "atmel_tcb",
159 static int __init
tc_init(void)
161 return platform_driver_probe(&tc_driver
, tc_probe
);
163 arch_initcall(tc_init
);