atmel-ssc.c 3.5 KB

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