4 * Copyright (C) 2007 Atmel Corporation
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.
11 #include <linux/platform_device.h>
12 #include <linux/list.h>
13 #include <linux/clk.h>
14 #include <linux/err.h>
16 #include <linux/spinlock.h>
17 #include <linux/atmel-ssc.h>
19 /* Serialize access to ssc_list and user count */
20 static DEFINE_SPINLOCK(user_lock);
21 static LIST_HEAD(ssc_list);
23 struct ssc_device *ssc_request(unsigned int ssc_num)
26 struct ssc_device *ssc;
28 spin_lock(&user_lock);
29 list_for_each_entry(ssc, &ssc_list, list) {
30 if (ssc->pdev->id == ssc_num) {
37 spin_unlock(&user_lock);
38 pr_err("ssc: ssc%d platform device is missing\n", ssc_num);
39 return ERR_PTR(-ENODEV);
43 spin_unlock(&user_lock);
44 dev_dbg(&ssc->pdev->dev, "module busy\n");
45 return ERR_PTR(-EBUSY);
48 spin_unlock(&user_lock);
54 EXPORT_SYMBOL(ssc_request);
56 void ssc_free(struct ssc_device *ssc)
58 spin_lock(&user_lock);
61 clk_disable(ssc->clk);
63 dev_dbg(&ssc->pdev->dev, "device already free\n");
65 spin_unlock(&user_lock);
67 EXPORT_SYMBOL(ssc_free);
69 static int __init ssc_probe(struct platform_device *pdev)
72 struct resource *regs;
73 struct ssc_device *ssc;
75 ssc = kzalloc(sizeof(struct ssc_device), GFP_KERNEL);
77 dev_dbg(&pdev->dev, "out of memory\n");
82 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
84 dev_dbg(&pdev->dev, "no mmio resource defined\n");
89 ssc->clk = clk_get(&pdev->dev, "pclk");
90 if (IS_ERR(ssc->clk)) {
91 dev_dbg(&pdev->dev, "no pclk clock defined\n");
97 ssc->regs = ioremap(regs->start, regs->end - regs->start + 1);
99 dev_dbg(&pdev->dev, "ioremap failed\n");
104 /* disable all interrupts */
105 clk_enable(ssc->clk);
106 ssc_writel(ssc->regs, IDR, ~0UL);
107 ssc_readl(ssc->regs, SR);
108 clk_disable(ssc->clk);
110 ssc->irq = platform_get_irq(pdev, 0);
112 dev_dbg(&pdev->dev, "could not get irq\n");
117 spin_lock(&user_lock);
118 list_add_tail(&ssc->list, &ssc_list);
119 spin_unlock(&user_lock);
121 platform_set_drvdata(pdev, ssc);
123 dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
124 ssc->regs, ssc->irq);
138 static int __devexit ssc_remove(struct platform_device *pdev)
140 struct ssc_device *ssc = platform_get_drvdata(pdev);
142 spin_lock(&user_lock);
145 list_del(&ssc->list);
147 spin_unlock(&user_lock);
152 static struct platform_driver ssc_driver = {
153 .remove = __devexit_p(ssc_remove),
156 .owner = THIS_MODULE,
160 static int __init ssc_init(void)
162 return platform_driver_probe(&ssc_driver, ssc_probe);
164 module_init(ssc_init);
166 static void __exit ssc_exit(void)
168 platform_driver_unregister(&ssc_driver);
170 module_exit(ssc_exit);
172 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
173 MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91");
174 MODULE_LICENSE("GPL");
175 MODULE_ALIAS("platform:ssc");