atmel_tclib.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/export.h>
  12. #include <linux/of.h>
  13. /*
  14. * This is a thin library to solve the problem of how to portably allocate
  15. * one of the TC blocks. For simplicity, it doesn't currently expect to
  16. * share individual timers between different drivers.
  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. #elif defined(CONFIG_ARCH_AT91)
  23. /* AT91 has these divide MCK */
  24. const u8 atmel_tc_divisors[5] = { 2, 8, 32, 128, 0, };
  25. EXPORT_SYMBOL(atmel_tc_divisors);
  26. #endif
  27. static DEFINE_SPINLOCK(tc_list_lock);
  28. static LIST_HEAD(tc_list);
  29. /**
  30. * atmel_tc_alloc - allocate a specified TC block
  31. * @block: which block to allocate
  32. * @name: name to be associated with the iomem resource
  33. *
  34. * Caller allocates a block. If it is available, a pointer to a
  35. * pre-initialized struct atmel_tc is returned. The caller can access
  36. * the registers directly through the "regs" field.
  37. */
  38. struct atmel_tc *atmel_tc_alloc(unsigned block, const char *name)
  39. {
  40. struct atmel_tc *tc;
  41. struct platform_device *pdev = NULL;
  42. struct resource *r;
  43. size_t size;
  44. spin_lock(&tc_list_lock);
  45. list_for_each_entry(tc, &tc_list, node) {
  46. if (tc->pdev->dev.of_node) {
  47. if (of_alias_get_id(tc->pdev->dev.of_node, "tcb")
  48. == block) {
  49. pdev = tc->pdev;
  50. break;
  51. }
  52. } else if (tc->pdev->id == block) {
  53. pdev = tc->pdev;
  54. break;
  55. }
  56. }
  57. if (!pdev || tc->iomem)
  58. goto fail;
  59. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  60. if (!r)
  61. goto fail;
  62. size = resource_size(r);
  63. r = request_mem_region(r->start, size, name);
  64. if (!r)
  65. goto fail;
  66. tc->regs = ioremap(r->start, size);
  67. if (!tc->regs)
  68. goto fail_ioremap;
  69. tc->iomem = r;
  70. out:
  71. spin_unlock(&tc_list_lock);
  72. return tc;
  73. fail_ioremap:
  74. release_mem_region(r->start, size);
  75. fail:
  76. tc = NULL;
  77. goto out;
  78. }
  79. EXPORT_SYMBOL_GPL(atmel_tc_alloc);
  80. /**
  81. * atmel_tc_free - release a specified TC block
  82. * @tc: Timer/counter block that was returned by atmel_tc_alloc()
  83. *
  84. * This reverses the effect of atmel_tc_alloc(), unmapping the I/O
  85. * registers, invalidating the resource returned by that routine and
  86. * making the TC available to other drivers.
  87. */
  88. void atmel_tc_free(struct atmel_tc *tc)
  89. {
  90. spin_lock(&tc_list_lock);
  91. if (tc->regs) {
  92. iounmap(tc->regs);
  93. release_mem_region(tc->iomem->start, resource_size(tc->iomem));
  94. tc->regs = NULL;
  95. tc->iomem = NULL;
  96. }
  97. spin_unlock(&tc_list_lock);
  98. }
  99. EXPORT_SYMBOL_GPL(atmel_tc_free);
  100. #if defined(CONFIG_OF)
  101. static struct atmel_tcb_config tcb_rm9200_config = {
  102. .counter_width = 16,
  103. };
  104. static struct atmel_tcb_config tcb_sam9x5_config = {
  105. .counter_width = 32,
  106. };
  107. static const struct of_device_id atmel_tcb_dt_ids[] = {
  108. {
  109. .compatible = "atmel,at91rm9200-tcb",
  110. .data = &tcb_rm9200_config,
  111. }, {
  112. .compatible = "atmel,at91sam9x5-tcb",
  113. .data = &tcb_sam9x5_config,
  114. }, {
  115. /* sentinel */
  116. }
  117. };
  118. MODULE_DEVICE_TABLE(of, atmel_tcb_dt_ids);
  119. #endif
  120. static int __init tc_probe(struct platform_device *pdev)
  121. {
  122. struct atmel_tc *tc;
  123. struct clk *clk;
  124. int irq;
  125. if (!platform_get_resource(pdev, IORESOURCE_MEM, 0))
  126. return -EINVAL;
  127. irq = platform_get_irq(pdev, 0);
  128. if (irq < 0)
  129. return -EINVAL;
  130. tc = kzalloc(sizeof(struct atmel_tc), GFP_KERNEL);
  131. if (!tc)
  132. return -ENOMEM;
  133. tc->pdev = pdev;
  134. clk = clk_get(&pdev->dev, "t0_clk");
  135. if (IS_ERR(clk)) {
  136. kfree(tc);
  137. return -EINVAL;
  138. }
  139. /* 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;
  145. }
  146. tc->clk[0] = clk;
  147. tc->clk[1] = clk_get(&pdev->dev, "t1_clk");
  148. if (IS_ERR(tc->clk[1]))
  149. tc->clk[1] = clk;
  150. tc->clk[2] = clk_get(&pdev->dev, "t2_clk");
  151. if (IS_ERR(tc->clk[2]))
  152. tc->clk[2] = clk;
  153. tc->irq[0] = irq;
  154. tc->irq[1] = platform_get_irq(pdev, 1);
  155. if (tc->irq[1] < 0)
  156. tc->irq[1] = irq;
  157. tc->irq[2] = platform_get_irq(pdev, 2);
  158. if (tc->irq[2] < 0)
  159. tc->irq[2] = irq;
  160. spin_lock(&tc_list_lock);
  161. list_add_tail(&tc->node, &tc_list);
  162. spin_unlock(&tc_list_lock);
  163. return 0;
  164. }
  165. static struct platform_driver tc_driver = {
  166. .driver = {
  167. .name = "atmel_tcb",
  168. .of_match_table = of_match_ptr(atmel_tcb_dt_ids),
  169. },
  170. };
  171. static int __init tc_init(void)
  172. {
  173. return platform_driver_probe(&tc_driver, tc_probe);
  174. }
  175. arch_initcall(tc_init);