Loading...
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
11/* Number of bytes to reserve for the iomem resource */
12#define ATMEL_TC_IOMEM_SIZE 256
13
14
15/*
16 * This is a thin library to solve the problem of how to portably allocate
17 * one of the TC blocks. For simplicity, it doesn't currently expect to
18 * share individual timers between different drivers.
19 */
20
21#if defined(CONFIG_AVR32)
22/* AVR32 has these divide PBB */
23const u8 atmel_tc_divisors[5] = { 0, 4, 8, 16, 32, };
24EXPORT_SYMBOL(atmel_tc_divisors);
25
26#elif defined(CONFIG_ARCH_AT91)
27/* AT91 has these divide MCK */
28const u8 atmel_tc_divisors[5] = { 2, 8, 32, 128, 0, };
29EXPORT_SYMBOL(atmel_tc_divisors);
30
31#endif
32
33static DEFINE_SPINLOCK(tc_list_lock);
34static LIST_HEAD(tc_list);
35
36/**
37 * atmel_tc_alloc - allocate a specified TC block
38 * @block: which block to allocate
39 * @name: name to be associated with the iomem resource
40 *
41 * Caller allocates a block. If it is available, a pointer to a
42 * pre-initialized struct atmel_tc is returned. The caller can access
43 * the registers directly through the "regs" field.
44 */
45struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name)
46{
47 struct atmel_tc *tc;
48 struct platform_device *pdev = NULL;
49 struct resource *r;
50
51 spin_lock(&tc_list_lock);
52 list_for_each_entry(tc, &tc_list, node) {
53 if (tc->pdev->id == block) {
54 pdev = tc->pdev;
55 break;
56 }
57 }
58
59 if (!pdev || tc->iomem)
60 goto fail;
61
62 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
63 r = request_mem_region(r->start, ATMEL_TC_IOMEM_SIZE, name);
64 if (!r)
65 goto fail;
66
67 tc->regs = ioremap(r->start, ATMEL_TC_IOMEM_SIZE);
68 if (!tc->regs)
69 goto fail_ioremap;
70
71 tc->iomem = r;
72
73out:
74 spin_unlock(&tc_list_lock);
75 return tc;
76
77fail_ioremap:
78 release_mem_region(r->start, ATMEL_TC_IOMEM_SIZE);
79fail:
80 tc = NULL;
81 goto out;
82}
83EXPORT_SYMBOL_GPL(atmel_tc_alloc);
84
85/**
86 * atmel_tc_free - release a specified TC block
87 * @tc: Timer/counter block that was returned by atmel_tc_alloc()
88 *
89 * This reverses the effect of atmel_tc_alloc(), unmapping the I/O
90 * registers, invalidating the resource returned by that routine and
91 * making the TC available to other drivers.
92 */
93void atmel_tc_free(struct atmel_tc *tc)
94{
95 spin_lock(&tc_list_lock);
96 if (tc->regs) {
97 iounmap(tc->regs);
98 release_mem_region(tc->iomem->start, ATMEL_TC_IOMEM_SIZE);
99 tc->regs = NULL;
100 tc->iomem = NULL;
101 }
102 spin_unlock(&tc_list_lock);
103}
104EXPORT_SYMBOL_GPL(atmel_tc_free);
105
106static int __init tc_probe(struct platform_device *pdev)
107{
108 struct atmel_tc *tc;
109 struct clk *clk;
110 int irq;
111
112 if (!platform_get_resource(pdev, IORESOURCE_MEM, 0))
113 return -EINVAL;
114
115 irq = platform_get_irq(pdev, 0);
116 if (irq < 0)
117 return -EINVAL;
118
119 tc = kzalloc(sizeof(struct atmel_tc), GFP_KERNEL);
120 if (!tc)
121 return -ENOMEM;
122
123 tc->pdev = pdev;
124
125 clk = clk_get(&pdev->dev, "t0_clk");
126 if (IS_ERR(clk)) {
127 kfree(tc);
128 return -EINVAL;
129 }
130
131 tc->clk[0] = clk;
132 tc->clk[1] = clk_get(&pdev->dev, "t1_clk");
133 if (IS_ERR(tc->clk[1]))
134 tc->clk[1] = clk;
135 tc->clk[2] = clk_get(&pdev->dev, "t2_clk");
136 if (IS_ERR(tc->clk[2]))
137 tc->clk[2] = clk;
138
139 tc->irq[0] = irq;
140 tc->irq[1] = platform_get_irq(pdev, 1);
141 if (tc->irq[1] < 0)
142 tc->irq[1] = irq;
143 tc->irq[2] = platform_get_irq(pdev, 2);
144 if (tc->irq[2] < 0)
145 tc->irq[2] = irq;
146
147 spin_lock(&tc_list_lock);
148 list_add_tail(&tc->node, &tc_list);
149 spin_unlock(&tc_list_lock);
150
151 return 0;
152}
153
154static struct platform_driver tc_driver = {
155 .driver.name = "atmel_tcb",
156};
157
158static int __init tc_init(void)
159{
160 return platform_driver_probe(&tc_driver, tc_probe);
161}
162arch_initcall(tc_init);
1// SPDX-License-Identifier: GPL-2.0-only
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/module.h>
10#include <linux/slab.h>
11#include <linux/export.h>
12#include <linux/of.h>
13#include <soc/at91/atmel_tcb.h>
14
15/*
16 * This is a thin library to solve the problem of how to portably allocate
17 * one of the TC blocks. For simplicity, it doesn't currently expect to
18 * share individual timers between different drivers.
19 */
20
21#if defined(CONFIG_AVR32)
22/* AVR32 has these divide PBB */
23const u8 atmel_tc_divisors[5] = { 0, 4, 8, 16, 32, };
24EXPORT_SYMBOL(atmel_tc_divisors);
25
26#elif defined(CONFIG_ARCH_AT91)
27/* AT91 has these divide MCK */
28const u8 atmel_tc_divisors[5] = { 2, 8, 32, 128, 0, };
29EXPORT_SYMBOL(atmel_tc_divisors);
30
31#endif
32
33static DEFINE_SPINLOCK(tc_list_lock);
34static LIST_HEAD(tc_list);
35
36/**
37 * atmel_tc_alloc - allocate a specified TC block
38 * @block: which block to allocate
39 *
40 * Caller allocates a block. If it is available, a pointer to a
41 * pre-initialized struct atmel_tc is returned. The caller can access
42 * the registers directly through the "regs" field.
43 */
44struct atmel_tc *atmel_tc_alloc(unsigned block)
45{
46 struct atmel_tc *tc;
47 struct platform_device *pdev = NULL;
48
49 spin_lock(&tc_list_lock);
50 list_for_each_entry(tc, &tc_list, node) {
51 if (tc->allocated)
52 continue;
53
54 if ((tc->pdev->dev.of_node && tc->id == block) ||
55 (tc->pdev->id == block)) {
56 pdev = tc->pdev;
57 tc->allocated = true;
58 break;
59 }
60 }
61 spin_unlock(&tc_list_lock);
62
63 return pdev ? tc : NULL;
64}
65EXPORT_SYMBOL_GPL(atmel_tc_alloc);
66
67/**
68 * atmel_tc_free - release a specified TC block
69 * @tc: Timer/counter block that was returned by atmel_tc_alloc()
70 *
71 * This reverses the effect of atmel_tc_alloc(), invalidating the resource
72 * returned by that routine and making the TC available to other drivers.
73 */
74void atmel_tc_free(struct atmel_tc *tc)
75{
76 spin_lock(&tc_list_lock);
77 if (tc->allocated)
78 tc->allocated = false;
79 spin_unlock(&tc_list_lock);
80}
81EXPORT_SYMBOL_GPL(atmel_tc_free);
82
83#if defined(CONFIG_OF)
84static struct atmel_tcb_config tcb_rm9200_config = {
85 .counter_width = 16,
86};
87
88static struct atmel_tcb_config tcb_sam9x5_config = {
89 .counter_width = 32,
90};
91
92static const struct of_device_id atmel_tcb_dt_ids[] = {
93 {
94 .compatible = "atmel,at91rm9200-tcb",
95 .data = &tcb_rm9200_config,
96 }, {
97 .compatible = "atmel,at91sam9x5-tcb",
98 .data = &tcb_sam9x5_config,
99 }, {
100 /* sentinel */
101 }
102};
103
104MODULE_DEVICE_TABLE(of, atmel_tcb_dt_ids);
105#endif
106
107static int __init tc_probe(struct platform_device *pdev)
108{
109 struct atmel_tc *tc;
110 struct clk *clk;
111 int irq;
112 struct resource *r;
113 unsigned int i;
114
115 if (of_get_child_count(pdev->dev.of_node))
116 return -EBUSY;
117
118 irq = platform_get_irq(pdev, 0);
119 if (irq < 0)
120 return -EINVAL;
121
122 tc = devm_kzalloc(&pdev->dev, sizeof(struct atmel_tc), GFP_KERNEL);
123 if (!tc)
124 return -ENOMEM;
125
126 tc->pdev = pdev;
127
128 clk = devm_clk_get(&pdev->dev, "t0_clk");
129 if (IS_ERR(clk))
130 return PTR_ERR(clk);
131
132 tc->slow_clk = devm_clk_get(&pdev->dev, "slow_clk");
133 if (IS_ERR(tc->slow_clk))
134 return PTR_ERR(tc->slow_clk);
135
136 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
137 tc->regs = devm_ioremap_resource(&pdev->dev, r);
138 if (IS_ERR(tc->regs))
139 return PTR_ERR(tc->regs);
140
141 /* Now take SoC information if available */
142 if (pdev->dev.of_node) {
143 const struct of_device_id *match;
144 match = of_match_node(atmel_tcb_dt_ids, pdev->dev.of_node);
145 if (match)
146 tc->tcb_config = match->data;
147
148 tc->id = of_alias_get_id(tc->pdev->dev.of_node, "tcb");
149 } else {
150 tc->id = pdev->id;
151 }
152
153 tc->clk[0] = clk;
154 tc->clk[1] = devm_clk_get(&pdev->dev, "t1_clk");
155 if (IS_ERR(tc->clk[1]))
156 tc->clk[1] = clk;
157 tc->clk[2] = devm_clk_get(&pdev->dev, "t2_clk");
158 if (IS_ERR(tc->clk[2]))
159 tc->clk[2] = clk;
160
161 tc->irq[0] = irq;
162 tc->irq[1] = platform_get_irq(pdev, 1);
163 if (tc->irq[1] < 0)
164 tc->irq[1] = irq;
165 tc->irq[2] = platform_get_irq(pdev, 2);
166 if (tc->irq[2] < 0)
167 tc->irq[2] = irq;
168
169 for (i = 0; i < 3; i++)
170 writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
171
172 spin_lock(&tc_list_lock);
173 list_add_tail(&tc->node, &tc_list);
174 spin_unlock(&tc_list_lock);
175
176 platform_set_drvdata(pdev, tc);
177
178 return 0;
179}
180
181static void tc_shutdown(struct platform_device *pdev)
182{
183 int i;
184 struct atmel_tc *tc = platform_get_drvdata(pdev);
185
186 for (i = 0; i < 3; i++)
187 writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
188}
189
190static struct platform_driver tc_driver = {
191 .driver = {
192 .name = "atmel_tcb",
193 .of_match_table = of_match_ptr(atmel_tcb_dt_ids),
194 },
195 .shutdown = tc_shutdown,
196};
197
198static int __init tc_init(void)
199{
200 return platform_driver_probe(&tc_driver, tc_probe);
201}
202arch_initcall(tc_init);