atmel_tclib.c 3.6 KB

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