atmel-ssc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include <linux/of.h>
  20. /* Serialize access to ssc_list and user count */
  21. static DEFINE_SPINLOCK(user_lock);
  22. static LIST_HEAD(ssc_list);
  23. struct ssc_device *ssc_request(unsigned int ssc_num)
  24. {
  25. int ssc_valid = 0;
  26. struct ssc_device *ssc;
  27. spin_lock(&user_lock);
  28. list_for_each_entry(ssc, &ssc_list, list) {
  29. if (ssc->pdev->dev.of_node) {
  30. if (of_alias_get_id(ssc->pdev->dev.of_node, "ssc")
  31. == ssc_num) {
  32. ssc_valid = 1;
  33. break;
  34. }
  35. } else if (ssc->pdev->id == ssc_num) {
  36. ssc_valid = 1;
  37. break;
  38. }
  39. }
  40. if (!ssc_valid) {
  41. spin_unlock(&user_lock);
  42. pr_err("ssc: ssc%d platform device is missing\n", ssc_num);
  43. return ERR_PTR(-ENODEV);
  44. }
  45. if (ssc->user) {
  46. spin_unlock(&user_lock);
  47. dev_dbg(&ssc->pdev->dev, "module busy\n");
  48. return ERR_PTR(-EBUSY);
  49. }
  50. ssc->user++;
  51. spin_unlock(&user_lock);
  52. clk_enable(ssc->clk);
  53. return ssc;
  54. }
  55. EXPORT_SYMBOL(ssc_request);
  56. void ssc_free(struct ssc_device *ssc)
  57. {
  58. spin_lock(&user_lock);
  59. if (ssc->user) {
  60. ssc->user--;
  61. clk_disable(ssc->clk);
  62. } else {
  63. dev_dbg(&ssc->pdev->dev, "device already free\n");
  64. }
  65. spin_unlock(&user_lock);
  66. }
  67. EXPORT_SYMBOL(ssc_free);
  68. static struct atmel_ssc_platform_data at91rm9200_config = {
  69. .use_dma = 0,
  70. };
  71. static struct atmel_ssc_platform_data at91sam9g45_config = {
  72. .use_dma = 1,
  73. };
  74. static const struct platform_device_id atmel_ssc_devtypes[] = {
  75. {
  76. .name = "at91rm9200_ssc",
  77. .driver_data = (unsigned long) &at91rm9200_config,
  78. }, {
  79. .name = "at91sam9g45_ssc",
  80. .driver_data = (unsigned long) &at91sam9g45_config,
  81. }, {
  82. /* sentinel */
  83. }
  84. };
  85. #ifdef CONFIG_OF
  86. static const struct of_device_id atmel_ssc_dt_ids[] = {
  87. {
  88. .compatible = "atmel,at91rm9200-ssc",
  89. .data = &at91rm9200_config,
  90. }, {
  91. .compatible = "atmel,at91sam9g45-ssc",
  92. .data = &at91sam9g45_config,
  93. }, {
  94. /* sentinel */
  95. }
  96. };
  97. MODULE_DEVICE_TABLE(of, atmel_ssc_dt_ids);
  98. #endif
  99. static inline const struct atmel_ssc_platform_data * __init
  100. atmel_ssc_get_driver_data(struct platform_device *pdev)
  101. {
  102. if (pdev->dev.of_node) {
  103. const struct of_device_id *match;
  104. match = of_match_node(atmel_ssc_dt_ids, pdev->dev.of_node);
  105. if (match == NULL)
  106. return NULL;
  107. return match->data;
  108. }
  109. return (struct atmel_ssc_platform_data *)
  110. platform_get_device_id(pdev)->driver_data;
  111. }
  112. static int ssc_probe(struct platform_device *pdev)
  113. {
  114. struct resource *regs;
  115. struct ssc_device *ssc;
  116. const struct atmel_ssc_platform_data *plat_dat;
  117. ssc = devm_kzalloc(&pdev->dev, sizeof(struct ssc_device), GFP_KERNEL);
  118. if (!ssc) {
  119. dev_dbg(&pdev->dev, "out of memory\n");
  120. return -ENOMEM;
  121. }
  122. ssc->pdev = pdev;
  123. plat_dat = atmel_ssc_get_driver_data(pdev);
  124. if (!plat_dat)
  125. return -ENODEV;
  126. ssc->pdata = (struct atmel_ssc_platform_data *)plat_dat;
  127. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. if (!regs) {
  129. dev_dbg(&pdev->dev, "no mmio resource defined\n");
  130. return -ENXIO;
  131. }
  132. ssc->regs = devm_request_and_ioremap(&pdev->dev, regs);
  133. if (!ssc->regs) {
  134. dev_dbg(&pdev->dev, "ioremap failed\n");
  135. return -EINVAL;
  136. }
  137. ssc->phybase = regs->start;
  138. ssc->clk = devm_clk_get(&pdev->dev, "pclk");
  139. if (IS_ERR(ssc->clk)) {
  140. dev_dbg(&pdev->dev, "no pclk clock defined\n");
  141. return -ENXIO;
  142. }
  143. /* disable all interrupts */
  144. clk_enable(ssc->clk);
  145. ssc_writel(ssc->regs, IDR, ~0UL);
  146. ssc_readl(ssc->regs, SR);
  147. clk_disable(ssc->clk);
  148. ssc->irq = platform_get_irq(pdev, 0);
  149. if (!ssc->irq) {
  150. dev_dbg(&pdev->dev, "could not get irq\n");
  151. return -ENXIO;
  152. }
  153. spin_lock(&user_lock);
  154. list_add_tail(&ssc->list, &ssc_list);
  155. spin_unlock(&user_lock);
  156. platform_set_drvdata(pdev, ssc);
  157. dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
  158. ssc->regs, ssc->irq);
  159. return 0;
  160. }
  161. static int ssc_remove(struct platform_device *pdev)
  162. {
  163. struct ssc_device *ssc = platform_get_drvdata(pdev);
  164. spin_lock(&user_lock);
  165. list_del(&ssc->list);
  166. spin_unlock(&user_lock);
  167. return 0;
  168. }
  169. static struct platform_driver ssc_driver = {
  170. .driver = {
  171. .name = "ssc",
  172. .owner = THIS_MODULE,
  173. .of_match_table = of_match_ptr(atmel_ssc_dt_ids),
  174. },
  175. .id_table = atmel_ssc_devtypes,
  176. .probe = ssc_probe,
  177. .remove = ssc_remove,
  178. };
  179. module_platform_driver(ssc_driver);
  180. MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  181. MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91");
  182. MODULE_LICENSE("GPL");
  183. MODULE_ALIAS("platform:ssc");