atmel-ssc.c 5.1 KB

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