atmel_tclib.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /* Number of bytes to reserve for the iomem resource */
  10. #define ATMEL_TC_IOMEM_SIZE 256
  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. spin_lock(&tc_list_lock);
  42. list_for_each_entry(tc, &tc_list, node) {
  43. if (tc->pdev->id == block) {
  44. pdev = tc->pdev;
  45. break;
  46. }
  47. }
  48. if (!pdev || tc->iomem)
  49. goto fail;
  50. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  51. r = request_mem_region(r->start, ATMEL_TC_IOMEM_SIZE, name);
  52. if (!r)
  53. goto fail;
  54. tc->regs = ioremap(r->start, ATMEL_TC_IOMEM_SIZE);
  55. if (!tc->regs)
  56. goto fail_ioremap;
  57. tc->iomem = r;
  58. out:
  59. spin_unlock(&tc_list_lock);
  60. return tc;
  61. fail_ioremap:
  62. release_resource(r);
  63. fail:
  64. tc = NULL;
  65. goto out;
  66. }
  67. EXPORT_SYMBOL_GPL(atmel_tc_alloc);
  68. /**
  69. * atmel_tc_free - release a specified TC block
  70. * @tc: Timer/counter block that was returned by atmel_tc_alloc()
  71. *
  72. * This reverses the effect of atmel_tc_alloc(), unmapping the I/O
  73. * registers, invalidating the resource returned by that routine and
  74. * making the TC available to other drivers.
  75. */
  76. void atmel_tc_free(struct atmel_tc *tc)
  77. {
  78. spin_lock(&tc_list_lock);
  79. if (tc->regs) {
  80. iounmap(tc->regs);
  81. release_resource(tc->iomem);
  82. tc->regs = NULL;
  83. tc->iomem = NULL;
  84. }
  85. spin_unlock(&tc_list_lock);
  86. }
  87. EXPORT_SYMBOL_GPL(atmel_tc_free);
  88. static int __init tc_probe(struct platform_device *pdev)
  89. {
  90. struct atmel_tc *tc;
  91. struct clk *clk;
  92. int irq;
  93. if (!platform_get_resource(pdev, IORESOURCE_MEM, 0))
  94. return -EINVAL;
  95. irq = platform_get_irq(pdev, 0);
  96. if (irq < 0)
  97. return -EINVAL;
  98. tc = kzalloc(sizeof(struct atmel_tc), GFP_KERNEL);
  99. if (!tc)
  100. return -ENOMEM;
  101. tc->pdev = pdev;
  102. clk = clk_get(&pdev->dev, "t0_clk");
  103. if (IS_ERR(clk)) {
  104. kfree(tc);
  105. return -EINVAL;
  106. }
  107. tc->clk[0] = clk;
  108. tc->clk[1] = clk_get(&pdev->dev, "t1_clk");
  109. if (IS_ERR(tc->clk[1]))
  110. tc->clk[1] = clk;
  111. tc->clk[2] = clk_get(&pdev->dev, "t2_clk");
  112. if (IS_ERR(tc->clk[2]))
  113. tc->clk[2] = clk;
  114. tc->irq[0] = irq;
  115. tc->irq[1] = platform_get_irq(pdev, 1);
  116. if (tc->irq[1] < 0)
  117. tc->irq[1] = irq;
  118. tc->irq[2] = platform_get_irq(pdev, 2);
  119. if (tc->irq[2] < 0)
  120. tc->irq[2] = irq;
  121. spin_lock(&tc_list_lock);
  122. list_add_tail(&tc->node, &tc_list);
  123. spin_unlock(&tc_list_lock);
  124. return 0;
  125. }
  126. static struct platform_driver tc_driver = {
  127. .driver.name = "atmel_tcb",
  128. };
  129. static int __init tc_init(void)
  130. {
  131. return platform_driver_probe(&tc_driver, tc_probe);
  132. }
  133. arch_initcall(tc_init);