atmel-ssc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Atmel SSC driver
  3. *
  4. * Copyright (C) 2007 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/platform_device.h>
  11. #include <linux/list.h>
  12. #include <linux/clk.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/atmel-ssc.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. /* Serialize access to ssc_list and user count */
  20. static DEFINE_SPINLOCK(user_lock);
  21. static LIST_HEAD(ssc_list);
  22. struct ssc_device *ssc_request(unsigned int ssc_num)
  23. {
  24. int ssc_valid = 0;
  25. struct ssc_device *ssc;
  26. spin_lock(&user_lock);
  27. list_for_each_entry(ssc, &ssc_list, list) {
  28. if (ssc->pdev->id == ssc_num) {
  29. ssc_valid = 1;
  30. break;
  31. }
  32. }
  33. if (!ssc_valid) {
  34. spin_unlock(&user_lock);
  35. pr_err("ssc: ssc%d platform device is missing\n", ssc_num);
  36. return ERR_PTR(-ENODEV);
  37. }
  38. if (ssc->user) {
  39. spin_unlock(&user_lock);
  40. dev_dbg(&ssc->pdev->dev, "module busy\n");
  41. return ERR_PTR(-EBUSY);
  42. }
  43. ssc->user++;
  44. spin_unlock(&user_lock);
  45. clk_enable(ssc->clk);
  46. return ssc;
  47. }
  48. EXPORT_SYMBOL(ssc_request);
  49. void ssc_free(struct ssc_device *ssc)
  50. {
  51. spin_lock(&user_lock);
  52. if (ssc->user) {
  53. ssc->user--;
  54. clk_disable(ssc->clk);
  55. } else {
  56. dev_dbg(&ssc->pdev->dev, "device already free\n");
  57. }
  58. spin_unlock(&user_lock);
  59. }
  60. EXPORT_SYMBOL(ssc_free);
  61. static int __init ssc_probe(struct platform_device *pdev)
  62. {
  63. struct resource *regs;
  64. struct ssc_device *ssc;
  65. ssc = devm_kzalloc(&pdev->dev, sizeof(struct ssc_device), GFP_KERNEL);
  66. if (!ssc) {
  67. dev_dbg(&pdev->dev, "out of memory\n");
  68. return -ENOMEM;
  69. }
  70. ssc->pdev = pdev;
  71. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  72. if (!regs) {
  73. dev_dbg(&pdev->dev, "no mmio resource defined\n");
  74. return -ENXIO;
  75. }
  76. ssc->regs = devm_request_and_ioremap(&pdev->dev, regs);
  77. if (!ssc->regs) {
  78. dev_dbg(&pdev->dev, "ioremap failed\n");
  79. return -EINVAL;
  80. }
  81. ssc->clk = devm_clk_get(&pdev->dev, "pclk");
  82. if (IS_ERR(ssc->clk)) {
  83. dev_dbg(&pdev->dev, "no pclk clock defined\n");
  84. return -ENXIO;
  85. }
  86. /* disable all interrupts */
  87. clk_enable(ssc->clk);
  88. ssc_writel(ssc->regs, IDR, ~0UL);
  89. ssc_readl(ssc->regs, SR);
  90. clk_disable(ssc->clk);
  91. ssc->irq = platform_get_irq(pdev, 0);
  92. if (!ssc->irq) {
  93. dev_dbg(&pdev->dev, "could not get irq\n");
  94. return -ENXIO;
  95. }
  96. spin_lock(&user_lock);
  97. list_add_tail(&ssc->list, &ssc_list);
  98. spin_unlock(&user_lock);
  99. platform_set_drvdata(pdev, ssc);
  100. dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
  101. ssc->regs, ssc->irq);
  102. return 0;
  103. }
  104. static int __devexit ssc_remove(struct platform_device *pdev)
  105. {
  106. struct ssc_device *ssc = platform_get_drvdata(pdev);
  107. spin_lock(&user_lock);
  108. list_del(&ssc->list);
  109. spin_unlock(&user_lock);
  110. return 0;
  111. }
  112. static struct platform_driver ssc_driver = {
  113. .remove = __devexit_p(ssc_remove),
  114. .driver = {
  115. .name = "ssc",
  116. .owner = THIS_MODULE,
  117. },
  118. };
  119. static int __init ssc_init(void)
  120. {
  121. return platform_driver_probe(&ssc_driver, ssc_probe);
  122. }
  123. module_init(ssc_init);
  124. static void __exit ssc_exit(void)
  125. {
  126. platform_driver_unregister(&ssc_driver);
  127. }
  128. module_exit(ssc_exit);
  129. MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  130. MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91");
  131. MODULE_LICENSE("GPL");
  132. MODULE_ALIAS("platform:ssc");