David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 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> |
Nicolas Ferre | 3a61a5d | 2012-01-19 10:13:40 +0100 | [diff] [blame] | 9 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Paul Gortmaker | 7a32129 | 2011-07-10 12:41:41 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Nicolas Ferre | 3a61a5d | 2012-01-19 10:13:40 +0100 | [diff] [blame] | 12 | #include <linux/of.h> |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 13 | |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 14 | /* |
| 15 | * This is a thin library to solve the problem of how to portably allocate |
| 16 | * one of the TC blocks. For simplicity, it doesn't currently expect to |
| 17 | * share individual timers between different drivers. |
| 18 | */ |
| 19 | |
| 20 | #if defined(CONFIG_AVR32) |
| 21 | /* AVR32 has these divide PBB */ |
| 22 | const u8 atmel_tc_divisors[5] = { 0, 4, 8, 16, 32, }; |
| 23 | EXPORT_SYMBOL(atmel_tc_divisors); |
| 24 | |
| 25 | #elif defined(CONFIG_ARCH_AT91) |
| 26 | /* AT91 has these divide MCK */ |
| 27 | const u8 atmel_tc_divisors[5] = { 2, 8, 32, 128, 0, }; |
| 28 | EXPORT_SYMBOL(atmel_tc_divisors); |
| 29 | |
| 30 | #endif |
| 31 | |
| 32 | static DEFINE_SPINLOCK(tc_list_lock); |
| 33 | static LIST_HEAD(tc_list); |
| 34 | |
| 35 | /** |
| 36 | * atmel_tc_alloc - allocate a specified TC block |
| 37 | * @block: which block to allocate |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 38 | * |
| 39 | * Caller allocates a block. If it is available, a pointer to a |
| 40 | * pre-initialized struct atmel_tc is returned. The caller can access |
| 41 | * the registers directly through the "regs" field. |
| 42 | */ |
Gaël PORTAY | 4930d24 | 2014-09-06 19:52:35 +0200 | [diff] [blame^] | 43 | struct atmel_tc *atmel_tc_alloc(unsigned block) |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 44 | { |
| 45 | struct atmel_tc *tc; |
| 46 | struct platform_device *pdev = NULL; |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 47 | |
| 48 | spin_lock(&tc_list_lock); |
| 49 | list_for_each_entry(tc, &tc_list, node) { |
Gaël PORTAY | 4930d24 | 2014-09-06 19:52:35 +0200 | [diff] [blame^] | 50 | if (tc->allocated) |
| 51 | continue; |
| 52 | |
| 53 | if ((tc->pdev->dev.of_node && tc->id == block) || |
| 54 | (tc->pdev->id == block)) { |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 55 | pdev = tc->pdev; |
Gaël PORTAY | 4930d24 | 2014-09-06 19:52:35 +0200 | [diff] [blame^] | 56 | tc->allocated = true; |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 57 | break; |
| 58 | } |
| 59 | } |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 60 | spin_unlock(&tc_list_lock); |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 61 | |
Gaël PORTAY | 4930d24 | 2014-09-06 19:52:35 +0200 | [diff] [blame^] | 62 | return pdev ? tc : NULL; |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 63 | } |
| 64 | EXPORT_SYMBOL_GPL(atmel_tc_alloc); |
| 65 | |
| 66 | /** |
| 67 | * atmel_tc_free - release a specified TC block |
| 68 | * @tc: Timer/counter block that was returned by atmel_tc_alloc() |
| 69 | * |
Gaël PORTAY | 4930d24 | 2014-09-06 19:52:35 +0200 | [diff] [blame^] | 70 | * This reverses the effect of atmel_tc_alloc(), invalidating the resource |
| 71 | * returned by that routine and making the TC available to other drivers. |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 72 | */ |
| 73 | void atmel_tc_free(struct atmel_tc *tc) |
| 74 | { |
| 75 | spin_lock(&tc_list_lock); |
Gaël PORTAY | 4930d24 | 2014-09-06 19:52:35 +0200 | [diff] [blame^] | 76 | if (tc->allocated) |
| 77 | tc->allocated = false; |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 78 | spin_unlock(&tc_list_lock); |
| 79 | } |
| 80 | EXPORT_SYMBOL_GPL(atmel_tc_free); |
| 81 | |
Nicolas Ferre | 3a61a5d | 2012-01-19 10:13:40 +0100 | [diff] [blame] | 82 | #if defined(CONFIG_OF) |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 83 | static struct atmel_tcb_config tcb_rm9200_config = { |
| 84 | .counter_width = 16, |
| 85 | }; |
| 86 | |
| 87 | static struct atmel_tcb_config tcb_sam9x5_config = { |
| 88 | .counter_width = 32, |
| 89 | }; |
| 90 | |
Nicolas Ferre | 3a61a5d | 2012-01-19 10:13:40 +0100 | [diff] [blame] | 91 | static const struct of_device_id atmel_tcb_dt_ids[] = { |
| 92 | { |
| 93 | .compatible = "atmel,at91rm9200-tcb", |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 94 | .data = &tcb_rm9200_config, |
| 95 | }, { |
| 96 | .compatible = "atmel,at91sam9x5-tcb", |
| 97 | .data = &tcb_sam9x5_config, |
Nicolas Ferre | 3a61a5d | 2012-01-19 10:13:40 +0100 | [diff] [blame] | 98 | }, { |
| 99 | /* sentinel */ |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | MODULE_DEVICE_TABLE(of, atmel_tcb_dt_ids); |
| 104 | #endif |
| 105 | |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 106 | static int __init tc_probe(struct platform_device *pdev) |
| 107 | { |
| 108 | struct atmel_tc *tc; |
| 109 | struct clk *clk; |
| 110 | int irq; |
Gaël PORTAY | 4930d24 | 2014-09-06 19:52:35 +0200 | [diff] [blame^] | 111 | struct resource *r; |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 112 | |
| 113 | irq = platform_get_irq(pdev, 0); |
| 114 | if (irq < 0) |
| 115 | return -EINVAL; |
| 116 | |
Gaël PORTAY | 8495497 | 2014-09-06 19:52:34 +0200 | [diff] [blame] | 117 | tc = devm_kzalloc(&pdev->dev, sizeof(struct atmel_tc), GFP_KERNEL); |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 118 | if (!tc) |
| 119 | return -ENOMEM; |
| 120 | |
| 121 | tc->pdev = pdev; |
| 122 | |
Gaël PORTAY | 8495497 | 2014-09-06 19:52:34 +0200 | [diff] [blame] | 123 | clk = devm_clk_get(&pdev->dev, "t0_clk"); |
| 124 | if (IS_ERR(clk)) |
| 125 | return PTR_ERR(clk); |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 126 | |
Gaël PORTAY | 4930d24 | 2014-09-06 19:52:35 +0200 | [diff] [blame^] | 127 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 128 | tc->regs = devm_ioremap_resource(&pdev->dev, r); |
| 129 | if (IS_ERR(tc->regs)) |
| 130 | return PTR_ERR(tc->regs); |
| 131 | |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 132 | /* Now take SoC information if available */ |
| 133 | if (pdev->dev.of_node) { |
| 134 | const struct of_device_id *match; |
| 135 | match = of_match_node(atmel_tcb_dt_ids, pdev->dev.of_node); |
| 136 | if (match) |
| 137 | tc->tcb_config = match->data; |
Gaël PORTAY | 4930d24 | 2014-09-06 19:52:35 +0200 | [diff] [blame^] | 138 | |
| 139 | tc->id = of_alias_get_id(tc->pdev->dev.of_node, "tcb"); |
| 140 | } else { |
| 141 | tc->id = pdev->id; |
Nicolas Ferre | 8e315a7 | 2012-01-19 18:44:49 +0100 | [diff] [blame] | 142 | } |
| 143 | |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 144 | tc->clk[0] = clk; |
Gaël PORTAY | 8495497 | 2014-09-06 19:52:34 +0200 | [diff] [blame] | 145 | tc->clk[1] = devm_clk_get(&pdev->dev, "t1_clk"); |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 146 | if (IS_ERR(tc->clk[1])) |
| 147 | tc->clk[1] = clk; |
Gaël PORTAY | 8495497 | 2014-09-06 19:52:34 +0200 | [diff] [blame] | 148 | tc->clk[2] = devm_clk_get(&pdev->dev, "t2_clk"); |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 149 | if (IS_ERR(tc->clk[2])) |
| 150 | tc->clk[2] = clk; |
| 151 | |
| 152 | tc->irq[0] = irq; |
| 153 | tc->irq[1] = platform_get_irq(pdev, 1); |
| 154 | if (tc->irq[1] < 0) |
| 155 | tc->irq[1] = irq; |
| 156 | tc->irq[2] = platform_get_irq(pdev, 2); |
| 157 | if (tc->irq[2] < 0) |
| 158 | tc->irq[2] = irq; |
| 159 | |
| 160 | spin_lock(&tc_list_lock); |
| 161 | list_add_tail(&tc->node, &tc_list); |
| 162 | spin_unlock(&tc_list_lock); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static struct platform_driver tc_driver = { |
Nicolas Ferre | 3a61a5d | 2012-01-19 10:13:40 +0100 | [diff] [blame] | 168 | .driver = { |
| 169 | .name = "atmel_tcb", |
| 170 | .of_match_table = of_match_ptr(atmel_tcb_dt_ids), |
| 171 | }, |
David Brownell | 2a341f5 | 2008-02-22 17:23:23 -0800 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | static int __init tc_init(void) |
| 175 | { |
| 176 | return platform_driver_probe(&tc_driver, tc_probe); |
| 177 | } |
| 178 | arch_initcall(tc_init); |