atmel-ssc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /* Serialize access to ssc_list and user count */
  18. static DEFINE_SPINLOCK(user_lock);
  19. static LIST_HEAD(ssc_list);
  20. struct ssc_device *ssc_request(unsigned int ssc_num)
  21. {
  22. int ssc_valid = 0;
  23. struct ssc_device *ssc;
  24. spin_lock(&user_lock);
  25. list_for_each_entry(ssc, &ssc_list, list) {
  26. if (ssc->pdev->id == ssc_num) {
  27. ssc_valid = 1;
  28. break;
  29. }
  30. }
  31. if (!ssc_valid) {
  32. spin_unlock(&user_lock);
  33. pr_err("ssc: ssc%d platform device is missing\n", ssc_num);
  34. return ERR_PTR(-ENODEV);
  35. }
  36. if (ssc->user) {
  37. spin_unlock(&user_lock);
  38. dev_dbg(&ssc->pdev->dev, "module busy\n");
  39. return ERR_PTR(-EBUSY);
  40. }
  41. ssc->user++;
  42. spin_unlock(&user_lock);
  43. clk_enable(ssc->clk);
  44. return ssc;
  45. }
  46. EXPORT_SYMBOL(ssc_request);
  47. void ssc_free(struct ssc_device *ssc)
  48. {
  49. spin_lock(&user_lock);
  50. if (ssc->user) {
  51. ssc->user--;
  52. clk_disable(ssc->clk);
  53. } else {
  54. dev_dbg(&ssc->pdev->dev, "device already free\n");
  55. }
  56. spin_unlock(&user_lock);
  57. }
  58. EXPORT_SYMBOL(ssc_free);
  59. static int __init ssc_probe(struct platform_device *pdev)
  60. {
  61. int retval = 0;
  62. struct resource *regs;
  63. struct ssc_device *ssc;
  64. ssc = kzalloc(sizeof(struct ssc_device), GFP_KERNEL);
  65. if (!ssc) {
  66. dev_dbg(&pdev->dev, "out of memory\n");
  67. retval = -ENOMEM;
  68. goto out;
  69. }
  70. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  71. if (!regs) {
  72. dev_dbg(&pdev->dev, "no mmio resource defined\n");
  73. retval = -ENXIO;
  74. goto out_free;
  75. }
  76. ssc->clk = clk_get(&pdev->dev, "pclk");
  77. if (IS_ERR(ssc->clk)) {
  78. dev_dbg(&pdev->dev, "no pclk clock defined\n");
  79. retval = -ENXIO;
  80. goto out_free;
  81. }
  82. ssc->pdev = pdev;
  83. ssc->regs = ioremap(regs->start, regs->end - regs->start + 1);
  84. if (!ssc->regs) {
  85. dev_dbg(&pdev->dev, "ioremap failed\n");
  86. retval = -EINVAL;
  87. goto out_clk;
  88. }
  89. /* disable all interrupts */
  90. clk_enable(ssc->clk);
  91. ssc_writel(ssc->regs, IDR, ~0UL);
  92. ssc_readl(ssc->regs, SR);
  93. clk_disable(ssc->clk);
  94. ssc->irq = platform_get_irq(pdev, 0);
  95. if (!ssc->irq) {
  96. dev_dbg(&pdev->dev, "could not get irq\n");
  97. retval = -ENXIO;
  98. goto out_unmap;
  99. }
  100. spin_lock(&user_lock);
  101. list_add_tail(&ssc->list, &ssc_list);
  102. spin_unlock(&user_lock);
  103. platform_set_drvdata(pdev, ssc);
  104. dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
  105. ssc->regs, ssc->irq);
  106. goto out;
  107. out_unmap:
  108. iounmap(ssc->regs);
  109. out_clk:
  110. clk_put(ssc->clk);
  111. out_free:
  112. kfree(ssc);
  113. out:
  114. return retval;
  115. }
  116. static int __devexit ssc_remove(struct platform_device *pdev)
  117. {
  118. struct ssc_device *ssc = platform_get_drvdata(pdev);
  119. spin_lock(&user_lock);
  120. iounmap(ssc->regs);
  121. clk_put(ssc->clk);
  122. list_del(&ssc->list);
  123. kfree(ssc);
  124. spin_unlock(&user_lock);
  125. return 0;
  126. }
  127. static struct platform_driver ssc_driver = {
  128. .remove = __devexit_p(ssc_remove),
  129. .driver = {
  130. .name = "ssc",
  131. .owner = THIS_MODULE,
  132. },
  133. };
  134. static int __init ssc_init(void)
  135. {
  136. return platform_driver_probe(&ssc_driver, ssc_probe);
  137. }
  138. module_init(ssc_init);
  139. static void __exit ssc_exit(void)
  140. {
  141. platform_driver_unregister(&ssc_driver);
  142. }
  143. module_exit(ssc_exit);
  144. MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  145. MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91");
  146. MODULE_LICENSE("GPL");
  147. MODULE_ALIAS("platform:ssc");