atmel_tclib.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0-only
  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. #include <soc/at91/atmel_tcb.h>
  14. /*
  15. * This is a thin library to solve the problem of how to portably allocate
  16. * one of the TC blocks. For simplicity, it doesn't currently expect to
  17. * share individual timers between different drivers.
  18. */
  19. #if defined(CONFIG_AVR32)
  20. /* AVR32 has these divide PBB */
  21. const u8 atmel_tc_divisors[5] = { 0, 4, 8, 16, 32, };
  22. EXPORT_SYMBOL(atmel_tc_divisors);
  23. #elif defined(CONFIG_ARCH_AT91)
  24. /* AT91 has these divide MCK */
  25. const u8 atmel_tc_divisors[5] = { 2, 8, 32, 128, 0, };
  26. EXPORT_SYMBOL(atmel_tc_divisors);
  27. #endif
  28. static DEFINE_SPINLOCK(tc_list_lock);
  29. static LIST_HEAD(tc_list);
  30. /**
  31. * atmel_tc_alloc - allocate a specified TC block
  32. * @block: which block to allocate
  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)
  39. {
  40. struct atmel_tc *tc;
  41. struct platform_device *pdev = NULL;
  42. spin_lock(&tc_list_lock);
  43. list_for_each_entry(tc, &tc_list, node) {
  44. if (tc->allocated)
  45. continue;
  46. if ((tc->pdev->dev.of_node && tc->id == block) ||
  47. (tc->pdev->id == block)) {
  48. pdev = tc->pdev;
  49. tc->allocated = true;
  50. break;
  51. }
  52. }
  53. spin_unlock(&tc_list_lock);
  54. return pdev ? tc : NULL;
  55. }
  56. EXPORT_SYMBOL_GPL(atmel_tc_alloc);
  57. /**
  58. * atmel_tc_free - release a specified TC block
  59. * @tc: Timer/counter block that was returned by atmel_tc_alloc()
  60. *
  61. * This reverses the effect of atmel_tc_alloc(), invalidating the resource
  62. * returned by that routine and making the TC available to other drivers.
  63. */
  64. void atmel_tc_free(struct atmel_tc *tc)
  65. {
  66. spin_lock(&tc_list_lock);
  67. if (tc->allocated)
  68. tc->allocated = false;
  69. spin_unlock(&tc_list_lock);
  70. }
  71. EXPORT_SYMBOL_GPL(atmel_tc_free);
  72. #if defined(CONFIG_OF)
  73. static struct atmel_tcb_config tcb_rm9200_config = {
  74. .counter_width = 16,
  75. };
  76. static struct atmel_tcb_config tcb_sam9x5_config = {
  77. .counter_width = 32,
  78. };
  79. static const struct of_device_id atmel_tcb_dt_ids[] = {
  80. {
  81. .compatible = "atmel,at91rm9200-tcb",
  82. .data = &tcb_rm9200_config,
  83. }, {
  84. .compatible = "atmel,at91sam9x5-tcb",
  85. .data = &tcb_sam9x5_config,
  86. }, {
  87. /* sentinel */
  88. }
  89. };
  90. MODULE_DEVICE_TABLE(of, atmel_tcb_dt_ids);
  91. #endif
  92. static int __init tc_probe(struct platform_device *pdev)
  93. {
  94. struct atmel_tc *tc;
  95. struct clk *clk;
  96. int irq;
  97. unsigned int i;
  98. if (of_get_child_count(pdev->dev.of_node))
  99. return -EBUSY;
  100. irq = platform_get_irq(pdev, 0);
  101. if (irq < 0)
  102. return -EINVAL;
  103. tc = devm_kzalloc(&pdev->dev, sizeof(struct atmel_tc), GFP_KERNEL);
  104. if (!tc)
  105. return -ENOMEM;
  106. tc->pdev = pdev;
  107. clk = devm_clk_get(&pdev->dev, "t0_clk");
  108. if (IS_ERR(clk))
  109. return PTR_ERR(clk);
  110. tc->slow_clk = devm_clk_get(&pdev->dev, "slow_clk");
  111. if (IS_ERR(tc->slow_clk))
  112. return PTR_ERR(tc->slow_clk);
  113. tc->regs = devm_platform_ioremap_resource(pdev, 0);
  114. if (IS_ERR(tc->regs))
  115. return PTR_ERR(tc->regs);
  116. /* Now take SoC information if available */
  117. if (pdev->dev.of_node) {
  118. const struct of_device_id *match;
  119. match = of_match_node(atmel_tcb_dt_ids, pdev->dev.of_node);
  120. if (match)
  121. tc->tcb_config = match->data;
  122. tc->id = of_alias_get_id(tc->pdev->dev.of_node, "tcb");
  123. } else {
  124. tc->id = pdev->id;
  125. }
  126. tc->clk[0] = clk;
  127. tc->clk[1] = devm_clk_get(&pdev->dev, "t1_clk");
  128. if (IS_ERR(tc->clk[1]))
  129. tc->clk[1] = clk;
  130. tc->clk[2] = devm_clk_get(&pdev->dev, "t2_clk");
  131. if (IS_ERR(tc->clk[2]))
  132. tc->clk[2] = clk;
  133. tc->irq[0] = irq;
  134. tc->irq[1] = platform_get_irq(pdev, 1);
  135. if (tc->irq[1] < 0)
  136. tc->irq[1] = irq;
  137. tc->irq[2] = platform_get_irq(pdev, 2);
  138. if (tc->irq[2] < 0)
  139. tc->irq[2] = irq;
  140. for (i = 0; i < 3; i++)
  141. writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
  142. spin_lock(&tc_list_lock);
  143. list_add_tail(&tc->node, &tc_list);
  144. spin_unlock(&tc_list_lock);
  145. platform_set_drvdata(pdev, tc);
  146. return 0;
  147. }
  148. static void tc_shutdown(struct platform_device *pdev)
  149. {
  150. int i;
  151. struct atmel_tc *tc = platform_get_drvdata(pdev);
  152. for (i = 0; i < 3; i++)
  153. writel(ATMEL_TC_ALL_IRQ, tc->regs + ATMEL_TC_REG(i, IDR));
  154. }
  155. static struct platform_driver tc_driver = {
  156. .driver = {
  157. .name = "atmel_tcb",
  158. .of_match_table = of_match_ptr(atmel_tcb_dt_ids),
  159. },
  160. .shutdown = tc_shutdown,
  161. };
  162. static int __init tc_init(void)
  163. {
  164. return platform_driver_probe(&tc_driver, tc_probe);
  165. }
  166. arch_initcall(tc_init);