atmel-ssc.c 4.9 KB

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