spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / misc / atmel_tclib.c
blob4bcfc375973429a07dbcccf2558ef40237e20f00
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>
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);
32 #endif
34 static DEFINE_SPINLOCK(tc_list_lock);
35 static LIST_HEAD(tc_list);
37 /**
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)
48 struct atmel_tc *tc;
49 struct platform_device *pdev = NULL;
50 struct resource *r;
52 spin_lock(&tc_list_lock);
53 list_for_each_entry(tc, &tc_list, node) {
54 if (tc->pdev->id == block) {
55 pdev = tc->pdev;
56 break;
60 if (!pdev || tc->iomem)
61 goto fail;
63 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
64 r = request_mem_region(r->start, ATMEL_TC_IOMEM_SIZE, name);
65 if (!r)
66 goto fail;
68 tc->regs = ioremap(r->start, ATMEL_TC_IOMEM_SIZE);
69 if (!tc->regs)
70 goto fail_ioremap;
72 tc->iomem = r;
74 out:
75 spin_unlock(&tc_list_lock);
76 return tc;
78 fail_ioremap:
79 release_mem_region(r->start, ATMEL_TC_IOMEM_SIZE);
80 fail:
81 tc = NULL;
82 goto out;
84 EXPORT_SYMBOL_GPL(atmel_tc_alloc);
86 /**
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);
97 if (tc->regs) {
98 iounmap(tc->regs);
99 release_mem_region(tc->iomem->start, ATMEL_TC_IOMEM_SIZE);
100 tc->regs = NULL;
101 tc->iomem = NULL;
103 spin_unlock(&tc_list_lock);
105 EXPORT_SYMBOL_GPL(atmel_tc_free);
107 static int __init tc_probe(struct platform_device *pdev)
109 struct atmel_tc *tc;
110 struct clk *clk;
111 int irq;
113 if (!platform_get_resource(pdev, IORESOURCE_MEM, 0))
114 return -EINVAL;
116 irq = platform_get_irq(pdev, 0);
117 if (irq < 0)
118 return -EINVAL;
120 tc = kzalloc(sizeof(struct atmel_tc), GFP_KERNEL);
121 if (!tc)
122 return -ENOMEM;
124 tc->pdev = pdev;
126 clk = clk_get(&pdev->dev, "t0_clk");
127 if (IS_ERR(clk)) {
128 kfree(tc);
129 return -EINVAL;
132 tc->clk[0] = clk;
133 tc->clk[1] = clk_get(&pdev->dev, "t1_clk");
134 if (IS_ERR(tc->clk[1]))
135 tc->clk[1] = clk;
136 tc->clk[2] = clk_get(&pdev->dev, "t2_clk");
137 if (IS_ERR(tc->clk[2]))
138 tc->clk[2] = clk;
140 tc->irq[0] = irq;
141 tc->irq[1] = platform_get_irq(pdev, 1);
142 if (tc->irq[1] < 0)
143 tc->irq[1] = irq;
144 tc->irq[2] = platform_get_irq(pdev, 2);
145 if (tc->irq[2] < 0)
146 tc->irq[2] = irq;
148 spin_lock(&tc_list_lock);
149 list_add_tail(&tc->node, &tc_list);
150 spin_unlock(&tc_list_lock);
152 return 0;
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);