blob: 7a6512a148d432c20e64be7f08944f4e63a00325 [file] [log] [blame]
David Brownell2a341f52008-02-22 17:23:23 -08001#include <linux/atmel_tc.h>
2#include <linux/clk.h>
3#include <linux/err.h>
4#include <linux/init.h>
5#include <linux/io.h>
6#include <linux/ioport.h>
7#include <linux/kernel.h>
8#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Paul Gortmaker7a321292011-07-10 12:41:41 -040010#include <linux/export.h>
David Brownell2a341f52008-02-22 17:23:23 -080011
David Brownell2a341f52008-02-22 17:23:23 -080012/*
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.
16 */
17
18#if defined(CONFIG_AVR32)
19/* AVR32 has these divide PBB */
20const u8 atmel_tc_divisors[5] = { 0, 4, 8, 16, 32, };
21EXPORT_SYMBOL(atmel_tc_divisors);
22
23#elif defined(CONFIG_ARCH_AT91)
24/* AT91 has these divide MCK */
25const u8 atmel_tc_divisors[5] = { 2, 8, 32, 128, 0, };
26EXPORT_SYMBOL(atmel_tc_divisors);
27
28#endif
29
30static DEFINE_SPINLOCK(tc_list_lock);
31static LIST_HEAD(tc_list);
32
33/**
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
37 *
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.
41 */
42struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name)
43{
44 struct atmel_tc *tc;
45 struct platform_device *pdev = NULL;
46 struct resource *r;
Nicolas Ferre29831292012-01-18 16:56:36 +010047 size_t size;
David Brownell2a341f52008-02-22 17:23:23 -080048
49 spin_lock(&tc_list_lock);
50 list_for_each_entry(tc, &tc_list, node) {
51 if (tc->pdev->id == block) {
52 pdev = tc->pdev;
53 break;
54 }
55 }
56
57 if (!pdev || tc->iomem)
58 goto fail;
59
60 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
David Brownell2a341f52008-02-22 17:23:23 -080061 if (!r)
62 goto fail;
63
Nicolas Ferre29831292012-01-18 16:56:36 +010064 size = resource_size(r);
65 r = request_mem_region(r->start, size, name);
66 if (!r)
67 goto fail;
68
69 tc->regs = ioremap(r->start, size);
David Brownell2a341f52008-02-22 17:23:23 -080070 if (!tc->regs)
71 goto fail_ioremap;
72
73 tc->iomem = r;
74
75out:
76 spin_unlock(&tc_list_lock);
77 return tc;
78
79fail_ioremap:
Nicolas Ferre29831292012-01-18 16:56:36 +010080 release_mem_region(r->start, size);
David Brownell2a341f52008-02-22 17:23:23 -080081fail:
82 tc = NULL;
83 goto out;
84}
85EXPORT_SYMBOL_GPL(atmel_tc_alloc);
86
87/**
88 * atmel_tc_free - release a specified TC block
89 * @tc: Timer/counter block that was returned by atmel_tc_alloc()
90 *
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.
94 */
95void atmel_tc_free(struct atmel_tc *tc)
96{
97 spin_lock(&tc_list_lock);
98 if (tc->regs) {
99 iounmap(tc->regs);
Nicolas Ferre29831292012-01-18 16:56:36 +0100100 release_mem_region(tc->iomem->start, resource_size(tc->iomem));
David Brownell2a341f52008-02-22 17:23:23 -0800101 tc->regs = NULL;
102 tc->iomem = NULL;
103 }
104 spin_unlock(&tc_list_lock);
105}
106EXPORT_SYMBOL_GPL(atmel_tc_free);
107
108static int __init tc_probe(struct platform_device *pdev)
109{
110 struct atmel_tc *tc;
111 struct clk *clk;
112 int irq;
113
114 if (!platform_get_resource(pdev, IORESOURCE_MEM, 0))
115 return -EINVAL;
116
117 irq = platform_get_irq(pdev, 0);
118 if (irq < 0)
119 return -EINVAL;
120
121 tc = kzalloc(sizeof(struct atmel_tc), GFP_KERNEL);
122 if (!tc)
123 return -ENOMEM;
124
125 tc->pdev = pdev;
126
127 clk = clk_get(&pdev->dev, "t0_clk");
128 if (IS_ERR(clk)) {
129 kfree(tc);
130 return -EINVAL;
131 }
132
133 tc->clk[0] = clk;
134 tc->clk[1] = clk_get(&pdev->dev, "t1_clk");
135 if (IS_ERR(tc->clk[1]))
136 tc->clk[1] = clk;
137 tc->clk[2] = clk_get(&pdev->dev, "t2_clk");
138 if (IS_ERR(tc->clk[2]))
139 tc->clk[2] = clk;
140
141 tc->irq[0] = irq;
142 tc->irq[1] = platform_get_irq(pdev, 1);
143 if (tc->irq[1] < 0)
144 tc->irq[1] = irq;
145 tc->irq[2] = platform_get_irq(pdev, 2);
146 if (tc->irq[2] < 0)
147 tc->irq[2] = irq;
148
149 spin_lock(&tc_list_lock);
150 list_add_tail(&tc->node, &tc_list);
151 spin_unlock(&tc_list_lock);
152
153 return 0;
154}
155
156static struct platform_driver tc_driver = {
157 .driver.name = "atmel_tcb",
158};
159
160static int __init tc_init(void)
161{
162 return platform_driver_probe(&tc_driver, tc_probe);
163}
164arch_initcall(tc_init);