atmel-ssc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. int retval = 0;
  64. struct resource *regs;
  65. struct ssc_device *ssc;
  66. ssc = kzalloc(sizeof(struct ssc_device), GFP_KERNEL);
  67. if (!ssc) {
  68. dev_dbg(&pdev->dev, "out of memory\n");
  69. retval = -ENOMEM;
  70. goto out;
  71. }
  72. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  73. if (!regs) {
  74. dev_dbg(&pdev->dev, "no mmio resource defined\n");
  75. retval = -ENXIO;
  76. goto out_free;
  77. }
  78. ssc->clk = clk_get(&pdev->dev, "pclk");
  79. if (IS_ERR(ssc->clk)) {
  80. dev_dbg(&pdev->dev, "no pclk clock defined\n");
  81. retval = -ENXIO;
  82. goto out_free;
  83. }
  84. ssc->pdev = pdev;
  85. ssc->regs = ioremap(regs->start, resource_size(regs));
  86. if (!ssc->regs) {
  87. dev_dbg(&pdev->dev, "ioremap failed\n");
  88. retval = -EINVAL;
  89. goto out_clk;
  90. }
  91. /* disable all interrupts */
  92. clk_enable(ssc->clk);
  93. ssc_writel(ssc->regs, IDR, ~0UL);
  94. ssc_readl(ssc->regs, SR);
  95. clk_disable(ssc->clk);
  96. ssc->irq = platform_get_irq(pdev, 0);
  97. if (!ssc->irq) {
  98. dev_dbg(&pdev->dev, "could not get irq\n");
  99. retval = -ENXIO;
  100. goto out_unmap;
  101. }
  102. spin_lock(&user_lock);
  103. list_add_tail(&ssc->list, &ssc_list);
  104. spin_unlock(&user_lock);
  105. platform_set_drvdata(pdev, ssc);
  106. dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
  107. ssc->regs, ssc->irq);
  108. goto out;
  109. out_unmap:
  110. iounmap(ssc->regs);
  111. out_clk:
  112. clk_put(ssc->clk);
  113. out_free:
  114. kfree(ssc);
  115. out:
  116. return retval;
  117. }
  118. static int __devexit ssc_remove(struct platform_device *pdev)
  119. {
  120. struct ssc_device *ssc = platform_get_drvdata(pdev);
  121. spin_lock(&user_lock);
  122. iounmap(ssc->regs);
  123. clk_put(ssc->clk);
  124. list_del(&ssc->list);
  125. kfree(ssc);
  126. spin_unlock(&user_lock);
  127. return 0;
  128. }
  129. static struct platform_driver ssc_driver = {
  130. .remove = __devexit_p(ssc_remove),
  131. .driver = {
  132. .name = "ssc",
  133. .owner = THIS_MODULE,
  134. },
  135. };
  136. static int __init ssc_init(void)
  137. {
  138. return platform_driver_probe(&ssc_driver, ssc_probe);
  139. }
  140. module_init(ssc_init);
  141. static void __exit ssc_exit(void)
  142. {
  143. platform_driver_unregister(&ssc_driver);
  144. }
  145. module_exit(ssc_exit);
  146. MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  147. MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91");
  148. MODULE_LICENSE("GPL");
  149. MODULE_ALIAS("platform:ssc");