atmel_tclib.c 3.6 KB

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