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
39 * Caller allocates a block. If it is available, a pointer to a
40 * pre-initialized struct atmel_tc is returned. The caller can access
41 * the registers directly through the "regs" field.
43 struct atmel_tc
*atmel_tc_alloc(unsigned block
)
46 struct platform_device
*pdev
= NULL
;
48 spin_lock(&tc_list_lock
);
49 list_for_each_entry(tc
, &tc_list
, node
) {
53 if ((tc
->pdev
->dev
.of_node
&& tc
->id
== block
) ||
54 (tc
->pdev
->id
== block
)) {
60 spin_unlock(&tc_list_lock
);
62 return pdev
? tc
: NULL
;
64 EXPORT_SYMBOL_GPL(atmel_tc_alloc
);
67 * atmel_tc_free - release a specified TC block
68 * @tc: Timer/counter block that was returned by atmel_tc_alloc()
70 * This reverses the effect of atmel_tc_alloc(), invalidating the resource
71 * returned by that routine and making the TC available to other drivers.
73 void atmel_tc_free(struct atmel_tc
*tc
)
75 spin_lock(&tc_list_lock
);
77 tc
->allocated
= false;
78 spin_unlock(&tc_list_lock
);
80 EXPORT_SYMBOL_GPL(atmel_tc_free
);
82 #if defined(CONFIG_OF)
83 static struct atmel_tcb_config tcb_rm9200_config
= {
87 static struct atmel_tcb_config tcb_sam9x5_config
= {
91 static const struct of_device_id atmel_tcb_dt_ids
[] = {
93 .compatible
= "atmel,at91rm9200-tcb",
94 .data
= &tcb_rm9200_config
,
96 .compatible
= "atmel,at91sam9x5-tcb",
97 .data
= &tcb_sam9x5_config
,
103 MODULE_DEVICE_TABLE(of
, atmel_tcb_dt_ids
);
106 static int __init
tc_probe(struct platform_device
*pdev
)
114 irq
= platform_get_irq(pdev
, 0);
118 tc
= devm_kzalloc(&pdev
->dev
, sizeof(struct atmel_tc
), GFP_KERNEL
);
124 clk
= devm_clk_get(&pdev
->dev
, "t0_clk");
128 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
129 tc
->regs
= devm_ioremap_resource(&pdev
->dev
, r
);
130 if (IS_ERR(tc
->regs
))
131 return PTR_ERR(tc
->regs
);
133 /* Now take SoC information if available */
134 if (pdev
->dev
.of_node
) {
135 const struct of_device_id
*match
;
136 match
= of_match_node(atmel_tcb_dt_ids
, pdev
->dev
.of_node
);
138 tc
->tcb_config
= match
->data
;
140 tc
->id
= of_alias_get_id(tc
->pdev
->dev
.of_node
, "tcb");
146 tc
->clk
[1] = devm_clk_get(&pdev
->dev
, "t1_clk");
147 if (IS_ERR(tc
->clk
[1]))
149 tc
->clk
[2] = devm_clk_get(&pdev
->dev
, "t2_clk");
150 if (IS_ERR(tc
->clk
[2]))
154 tc
->irq
[1] = platform_get_irq(pdev
, 1);
157 tc
->irq
[2] = platform_get_irq(pdev
, 2);
161 for (i
= 0; i
< 3; i
++)
162 writel(ATMEL_TC_ALL_IRQ
, tc
->regs
+ ATMEL_TC_REG(i
, IDR
));
164 spin_lock(&tc_list_lock
);
165 list_add_tail(&tc
->node
, &tc_list
);
166 spin_unlock(&tc_list_lock
);
168 platform_set_drvdata(pdev
, tc
);
173 static void tc_shutdown(struct platform_device
*pdev
)
176 struct atmel_tc
*tc
= platform_get_drvdata(pdev
);
178 for (i
= 0; i
< 3; i
++)
179 writel(ATMEL_TC_ALL_IRQ
, tc
->regs
+ ATMEL_TC_REG(i
, IDR
));
182 static struct platform_driver tc_driver
= {
185 .of_match_table
= of_match_ptr(atmel_tcb_dt_ids
),
187 .shutdown
= tc_shutdown
,
190 static int __init
tc_init(void)
192 return platform_driver_probe(&tc_driver
, tc_probe
);
194 arch_initcall(tc_init
);