blob: 7de7840f613c06f725b375ff3d8f263274456e4e [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
David Brownell2a341f52008-02-22 17:23:23 -08002#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>
Nicolas Ferre3a61a5d2012-01-19 10:13:40 +01009#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Paul Gortmaker7a321292011-07-10 12:41:41 -040011#include <linux/export.h>
Nicolas Ferre3a61a5d2012-01-19 10:13:40 +010012#include <linux/of.h>
Alexandre Bellonic2c91362019-04-26 23:47:10 +020013#include <soc/at91/atmel_tcb.h>
David Brownell2a341f52008-02-22 17:23:23 -080014
David Brownell2a341f52008-02-22 17:23:23 -080015/*
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
David Brownell2a341f52008-02-22 17:23:23 -080039 *
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 */
Gaël PORTAY4930d242014-09-06 19:52:35 +020044struct atmel_tc *atmel_tc_alloc(unsigned block)
David Brownell2a341f52008-02-22 17:23:23 -080045{
46 struct atmel_tc *tc;
47 struct platform_device *pdev = NULL;
David Brownell2a341f52008-02-22 17:23:23 -080048
49 spin_lock(&tc_list_lock);
50 list_for_each_entry(tc, &tc_list, node) {
Gaël PORTAY4930d242014-09-06 19:52:35 +020051 if (tc->allocated)
52 continue;
53
54 if ((tc->pdev->dev.of_node && tc->id == block) ||
55 (tc->pdev->id == block)) {
David Brownell2a341f52008-02-22 17:23:23 -080056 pdev = tc->pdev;
Gaël PORTAY4930d242014-09-06 19:52:35 +020057 tc->allocated = true;
David Brownell2a341f52008-02-22 17:23:23 -080058 break;
59 }
60 }
David Brownell2a341f52008-02-22 17:23:23 -080061 spin_unlock(&tc_list_lock);
David Brownell2a341f52008-02-22 17:23:23 -080062
Gaël PORTAY4930d242014-09-06 19:52:35 +020063 return pdev ? tc : NULL;
David Brownell2a341f52008-02-22 17:23:23 -080064}
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 *
Gaël PORTAY4930d242014-09-06 19:52:35 +020071 * This reverses the effect of atmel_tc_alloc(), invalidating the resource
72 * returned by that routine and making the TC available to other drivers.
David Brownell2a341f52008-02-22 17:23:23 -080073 */
74void atmel_tc_free(struct atmel_tc *tc)
75{
76 spin_lock(&tc_list_lock);
Gaël PORTAY4930d242014-09-06 19:52:35 +020077 if (tc->allocated)
78 tc->allocated = false;
David Brownell2a341f52008-02-22 17:23:23 -080079 spin_unlock(&tc_list_lock);
80}
81EXPORT_SYMBOL_GPL(atmel_tc_free);
82
Nicolas Ferre3a61a5d2012-01-19 10:13:40 +010083#if defined(CONFIG_OF)
Nicolas Ferre8e315a72012-01-19 18:44:49 +010084static 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
Nicolas Ferre3a61a5d2012-01-19 10:13:40 +010092static const struct of_device_id atmel_tcb_dt_ids[] = {
93 {
94 .compatible = "atmel,at91rm9200-tcb",
Nicolas Ferre8e315a72012-01-19 18:44:49 +010095 .data = &tcb_rm9200_config,
96 }, {
97 .compatible = "atmel,at91sam9x5-tcb",
98 .data = &tcb_sam9x5_config,
Nicolas Ferre3a61a5d2012-01-19 10:13:40 +010099 }, {
100 /* sentinel */
101 }
102};
103
104MODULE_DEVICE_TABLE(of, atmel_tcb_dt_ids);
105#endif
106
David Brownell2a341f52008-02-22 17:23:23 -0800107static int __init tc_probe(struct platform_device *pdev)
108{
109 struct atmel_tc *tc;
110 struct clk *clk;
111 int irq;
Gaël PORTAY84f46232014-09-06 19:52:36 +0200112 unsigned int i;
David Brownell2a341f52008-02-22 17:23:23 -0800113
Alexandre Belloni8c937402019-04-26 23:47:18 +0200114 if (of_get_child_count(pdev->dev.of_node))
115 return -EBUSY;
116
David Brownell2a341f52008-02-22 17:23:23 -0800117 irq = platform_get_irq(pdev, 0);
118 if (irq < 0)
119 return -EINVAL;
120
Gaël PORTAY84954972014-09-06 19:52:34 +0200121 tc = devm_kzalloc(&pdev->dev, sizeof(struct atmel_tc), GFP_KERNEL);
David Brownell2a341f52008-02-22 17:23:23 -0800122 if (!tc)
123 return -ENOMEM;
124
125 tc->pdev = pdev;
126
Gaël PORTAY84954972014-09-06 19:52:34 +0200127 clk = devm_clk_get(&pdev->dev, "t0_clk");
128 if (IS_ERR(clk))
129 return PTR_ERR(clk);
David Brownell2a341f52008-02-22 17:23:23 -0800130
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200131 tc->slow_clk = devm_clk_get(&pdev->dev, "slow_clk");
132 if (IS_ERR(tc->slow_clk))
133 return PTR_ERR(tc->slow_clk);
134
YueHaibingad90ff62019-10-09 22:37:52 +0800135 tc->regs = devm_platform_ioremap_resource(pdev, 0);
Gaël PORTAY4930d242014-09-06 19:52:35 +0200136 if (IS_ERR(tc->regs))
137 return PTR_ERR(tc->regs);
138
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100139 /* Now take SoC information if available */
140 if (pdev->dev.of_node) {
141 const struct of_device_id *match;
142 match = of_match_node(atmel_tcb_dt_ids, pdev->dev.of_node);
143 if (match)
144 tc->tcb_config = match->data;
Gaël PORTAY4930d242014-09-06 19:52:35 +0200145
146 tc->id = of_alias_get_id(tc->pdev->dev.of_node, "tcb");
147 } else {
148 tc->id = pdev->id;
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100149 }
150
David Brownell2a341f52008-02-22 17:23:23 -0800151 tc->clk[0] = clk;
Gaël PORTAY84954972014-09-06 19:52:34 +0200152 tc->clk[1] = devm_clk_get(&pdev->dev, "t1_clk");
David Brownell2a341f52008-02-22 17:23:23 -0800153 if (IS_ERR(tc->clk[1]))
154 tc->clk[1] = clk;
Gaël PORTAY84954972014-09-06 19:52:34 +0200155 tc->clk[2] = devm_clk_get(&pdev->dev, "t2_clk");
David Brownell2a341f52008-02-22 17:23:23 -0800156 if (IS_ERR(tc->clk[2]))
157 tc->clk[2] = clk;
158
159 tc->irq[0] = irq;
160 tc->irq[1] = platform_get_irq(pdev, 1);
161 if (tc->irq[1] < 0)
162 tc->irq[1] = irq;
163 tc->irq[2] = platform_get_irq(pdev, 2);
164 if (tc->irq[2] < 0)
165 tc->irq[2] = irq;
166
Gaël PORTAY84f46232014-09-06 19:52:36 +0200167 for (i = 0; i < 3; i++)
168 writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
169
David Brownell2a341f52008-02-22 17:23:23 -0800170 spin_lock(&tc_list_lock);
171 list_add_tail(&tc->node, &tc_list);
172 spin_unlock(&tc_list_lock);
173
Gaël PORTAY84f46232014-09-06 19:52:36 +0200174 platform_set_drvdata(pdev, tc);
175
David Brownell2a341f52008-02-22 17:23:23 -0800176 return 0;
177}
178
Gaël PORTAY84f46232014-09-06 19:52:36 +0200179static void tc_shutdown(struct platform_device *pdev)
180{
181 int i;
182 struct atmel_tc *tc = platform_get_drvdata(pdev);
183
184 for (i = 0; i < 3; i++)
185 writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
186}
187
David Brownell2a341f52008-02-22 17:23:23 -0800188static struct platform_driver tc_driver = {
Nicolas Ferre3a61a5d2012-01-19 10:13:40 +0100189 .driver = {
190 .name = "atmel_tcb",
191 .of_match_table = of_match_ptr(atmel_tcb_dt_ids),
192 },
Gaël PORTAY84f46232014-09-06 19:52:36 +0200193 .shutdown = tc_shutdown,
David Brownell2a341f52008-02-22 17:23:23 -0800194};
195
196static int __init tc_init(void)
197{
198 return platform_driver_probe(&tc_driver, tc_probe);
199}
200arch_initcall(tc_init);