atmel_tclib.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. *
  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)
  38. {
  39. struct atmel_tc *tc;
  40. struct platform_device *pdev = NULL;
  41. spin_lock(&tc_list_lock);
  42. list_for_each_entry(tc, &tc_list, node) {
  43. if (tc->allocated)
  44. continue;
  45. if ((tc->pdev->dev.of_node && tc->id == block) ||
  46. (tc->pdev->id == block)) {
  47. pdev = tc->pdev;
  48. tc->allocated = true;
  49. break;
  50. }
  51. }
  52. spin_unlock(&tc_list_lock);
  53. return pdev ? tc : NULL;
  54. }
  55. EXPORT_SYMBOL_GPL(atmel_tc_alloc);
  56. /**
  57. * atmel_tc_free - release a specified TC block
  58. * @tc: Timer/counter block that was returned by atmel_tc_alloc()
  59. *
  60. * This reverses the effect of atmel_tc_alloc(), invalidating the resource
  61. * returned by that routine and making the TC available to other drivers.
  62. */
  63. void atmel_tc_free(struct atmel_tc *tc)
  64. {
  65. spin_lock(&tc_list_lock);
  66. if (tc->allocated)
  67. tc->allocated = false;
  68. spin_unlock(&tc_list_lock);
  69. }
  70. EXPORT_SYMBOL_GPL(atmel_tc_free);
  71. #if defined(CONFIG_OF)
  72. static struct atmel_tcb_config tcb_rm9200_config = {
  73. .counter_width = 16,
  74. };
  75. static struct atmel_tcb_config tcb_sam9x5_config = {
  76. .counter_width = 32,
  77. };
  78. static const struct of_device_id atmel_tcb_dt_ids[] = {
  79. {
  80. .compatible = "atmel,at91rm9200-tcb",
  81. .data = &tcb_rm9200_config,
  82. }, {
  83. .compatible = "atmel,at91sam9x5-tcb",
  84. .data = &tcb_sam9x5_config,
  85. }, {
  86. /* sentinel */
  87. }
  88. };
  89. MODULE_DEVICE_TABLE(of, atmel_tcb_dt_ids);
  90. #endif
  91. static int __init tc_probe(struct platform_device *pdev)
  92. {
  93. struct atmel_tc *tc;
  94. struct clk *clk;
  95. int irq;
  96. struct resource *r;
  97. unsigned int i;
  98. irq = platform_get_irq(pdev, 0);
  99. if (irq < 0)
  100. return -EINVAL;
  101. tc = devm_kzalloc(&pdev->dev, sizeof(struct atmel_tc), GFP_KERNEL);
  102. if (!tc)
  103. return -ENOMEM;
  104. tc->pdev = pdev;
  105. clk = devm_clk_get(&pdev->dev, "t0_clk");
  106. if (IS_ERR(clk))
  107. return PTR_ERR(clk);
  108. tc->slow_clk = devm_clk_get(&pdev->dev, "slow_clk");
  109. if (IS_ERR(tc->slow_clk))
  110. return PTR_ERR(tc->slow_clk);
  111. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  112. tc->regs = devm_ioremap_resource(&pdev->dev, r);
  113. if (IS_ERR(tc->regs))
  114. return PTR_ERR(tc->regs);
  115. /* Now take SoC information if available */
  116. if (pdev->dev.of_node) {
  117. const struct of_device_id *match;
  118. match = of_match_node(atmel_tcb_dt_ids, pdev->dev.of_node);
  119. if (match)
  120. tc->tcb_config = match->data;
  121. tc->id = of_alias_get_id(tc->pdev->dev.of_node, "tcb");
  122. } else {
  123. tc->id = pdev->id;
  124. }
  125. tc->clk[0] = clk;
  126. tc->clk[1] = devm_clk_get(&pdev->dev, "t1_clk");
  127. if (IS_ERR(tc->clk[1]))
  128. tc->clk[1] = clk;
  129. tc->clk[2] = devm_clk_get(&pdev->dev, "t2_clk");
  130. if (IS_ERR(tc->clk[2]))
  131. tc->clk[2] = clk;
  132. tc->irq[0] = irq;
  133. tc->irq[1] = platform_get_irq(pdev, 1);
  134. if (tc->irq[1] < 0)
  135. tc->irq[1] = irq;
  136. tc->irq[2] = platform_get_irq(pdev, 2);
  137. if (tc->irq[2] < 0)
  138. tc->irq[2] = irq;
  139. for (i = 0; i < 3; i++)
  140. writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
  141. spin_lock(&tc_list_lock);
  142. list_add_tail(&tc->node, &tc_list);
  143. spin_unlock(&tc_list_lock);
  144. platform_set_drvdata(pdev, tc);
  145. return 0;
  146. }
  147. static void tc_shutdown(struct platform_device *pdev)
  148. {
  149. int i;
  150. struct atmel_tc *tc = platform_get_drvdata(pdev);
  151. for (i = 0; i < 3; i++)
  152. writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
  153. }
  154. static struct platform_driver tc_driver = {
  155. .driver = {
  156. .name = "atmel_tcb",
  157. .of_match_table = of_match_ptr(atmel_tcb_dt_ids),
  158. },
  159. .shutdown = tc_shutdown,
  160. };
  161. static int __init tc_init(void)
  162. {
  163. return platform_driver_probe(&tc_driver, tc_probe);
  164. }
  165. arch_initcall(tc_init);