ARM: at91/tclib: take iomem size from resource
[linux-2.6.git] / drivers / misc / atmel_tclib.c
1 #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>
9 #include <linux/slab.h>
10 #include <linux/export.h>
11
12 /*
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 */
20 const u8 atmel_tc_divisors[5] = { 0, 4, 8, 16, 32, };
21 EXPORT_SYMBOL(atmel_tc_divisors);
22
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);
27
28 #endif
29
30 static DEFINE_SPINLOCK(tc_list_lock);
31 static 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  */
42 struct 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;
47         size_t                  size;
48
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);
61         if (!r)
62                 goto fail;
63
64         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);
70         if (!tc->regs)
71                 goto fail_ioremap;
72
73         tc->iomem = r;
74
75 out:
76         spin_unlock(&tc_list_lock);
77         return tc;
78
79 fail_ioremap:
80         release_mem_region(r->start, size);
81 fail:
82         tc = NULL;
83         goto out;
84 }
85 EXPORT_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  */
95 void atmel_tc_free(struct atmel_tc *tc)
96 {
97         spin_lock(&tc_list_lock);
98         if (tc->regs) {
99                 iounmap(tc->regs);
100                 release_mem_region(tc->iomem->start, resource_size(tc->iomem));
101                 tc->regs = NULL;
102                 tc->iomem = NULL;
103         }
104         spin_unlock(&tc_list_lock);
105 }
106 EXPORT_SYMBOL_GPL(atmel_tc_free);
107
108 static 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
156 static struct platform_driver tc_driver = {
157         .driver.name    = "atmel_tcb",
158 };
159
160 static int __init tc_init(void)
161 {
162         return platform_driver_probe(&tc_driver, tc_probe);
163 }
164 arch_initcall(tc_init);