atmel_tclib.c 3.6 KB

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