atmel-ssc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_prepare_enable(ssc->clk);
  53. return ssc;
  54. }
  55. EXPORT_SYMBOL(ssc_request);
  56. void ssc_free(struct ssc_device *ssc)
  57. {
  58. bool disable_clk = true;
  59. spin_lock(&user_lock);
  60. if (ssc->user)
  61. ssc->user--;
  62. else {
  63. disable_clk = false;
  64. dev_dbg(&ssc->pdev->dev, "device already free\n");
  65. }
  66. spin_unlock(&user_lock);
  67. if (disable_clk)
  68. clk_disable_unprepare(ssc->clk);
  69. }
  70. EXPORT_SYMBOL(ssc_free);
  71. static struct atmel_ssc_platform_data at91rm9200_config = {
  72. .use_dma = 0,
  73. };
  74. static struct atmel_ssc_platform_data at91sam9g45_config = {
  75. .use_dma = 1,
  76. };
  77. static const struct platform_device_id atmel_ssc_devtypes[] = {
  78. {
  79. .name = "at91rm9200_ssc",
  80. .driver_data = (unsigned long) &at91rm9200_config,
  81. }, {
  82. .name = "at91sam9g45_ssc",
  83. .driver_data = (unsigned long) &at91sam9g45_config,
  84. }, {
  85. /* sentinel */
  86. }
  87. };
  88. #ifdef CONFIG_OF
  89. static const struct of_device_id atmel_ssc_dt_ids[] = {
  90. {
  91. .compatible = "atmel,at91rm9200-ssc",
  92. .data = &at91rm9200_config,
  93. }, {
  94. .compatible = "atmel,at91sam9g45-ssc",
  95. .data = &at91sam9g45_config,
  96. }, {
  97. /* sentinel */
  98. }
  99. };
  100. MODULE_DEVICE_TABLE(of, atmel_ssc_dt_ids);
  101. #endif
  102. static inline const struct atmel_ssc_platform_data * __init
  103. atmel_ssc_get_driver_data(struct platform_device *pdev)
  104. {
  105. if (pdev->dev.of_node) {
  106. const struct of_device_id *match;
  107. match = of_match_node(atmel_ssc_dt_ids, pdev->dev.of_node);
  108. if (match == NULL)
  109. return NULL;
  110. return match->data;
  111. }
  112. return (struct atmel_ssc_platform_data *)
  113. platform_get_device_id(pdev)->driver_data;
  114. }
  115. static int ssc_probe(struct platform_device *pdev)
  116. {
  117. struct resource *regs;
  118. struct ssc_device *ssc;
  119. const struct atmel_ssc_platform_data *plat_dat;
  120. ssc = devm_kzalloc(&pdev->dev, sizeof(struct ssc_device), GFP_KERNEL);
  121. if (!ssc) {
  122. dev_dbg(&pdev->dev, "out of memory\n");
  123. return -ENOMEM;
  124. }
  125. ssc->pdev = pdev;
  126. plat_dat = atmel_ssc_get_driver_data(pdev);
  127. if (!plat_dat)
  128. return -ENODEV;
  129. ssc->pdata = (struct atmel_ssc_platform_data *)plat_dat;
  130. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  131. ssc->regs = devm_ioremap_resource(&pdev->dev, regs);
  132. if (IS_ERR(ssc->regs))
  133. return PTR_ERR(ssc->regs);
  134. ssc->phybase = regs->start;
  135. ssc->clk = devm_clk_get(&pdev->dev, "pclk");
  136. if (IS_ERR(ssc->clk)) {
  137. dev_dbg(&pdev->dev, "no pclk clock defined\n");
  138. return -ENXIO;
  139. }
  140. /* disable all interrupts */
  141. clk_prepare_enable(ssc->clk);
  142. ssc_writel(ssc->regs, IDR, -1);
  143. ssc_readl(ssc->regs, SR);
  144. clk_disable_unprepare(ssc->clk);
  145. ssc->irq = platform_get_irq(pdev, 0);
  146. if (!ssc->irq) {
  147. dev_dbg(&pdev->dev, "could not get irq\n");
  148. return -ENXIO;
  149. }
  150. spin_lock(&user_lock);
  151. list_add_tail(&ssc->list, &ssc_list);
  152. spin_unlock(&user_lock);
  153. platform_set_drvdata(pdev, ssc);
  154. dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
  155. ssc->regs, ssc->irq);
  156. return 0;
  157. }
  158. static int ssc_remove(struct platform_device *pdev)
  159. {
  160. struct ssc_device *ssc = platform_get_drvdata(pdev);
  161. spin_lock(&user_lock);
  162. list_del(&ssc->list);
  163. spin_unlock(&user_lock);
  164. return 0;
  165. }
  166. static struct platform_driver ssc_driver = {
  167. .driver = {
  168. .name = "ssc",
  169. .owner = THIS_MODULE,
  170. .of_match_table = of_match_ptr(atmel_ssc_dt_ids),
  171. },
  172. .id_table = atmel_ssc_devtypes,
  173. .probe = ssc_probe,
  174. .remove = ssc_remove,
  175. };
  176. module_platform_driver(ssc_driver);
  177. MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  178. MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91");
  179. MODULE_LICENSE("GPL");
  180. MODULE_ALIAS("platform:ssc");