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/list.h>
17 #include <linux/spinlock.h>
18 #include <linux/atmel-ssc.h>
20 /* Serialize access to ssc_list and user count */
21 static DEFINE_SPINLOCK(user_lock);
22 static LIST_HEAD(ssc_list);
24 struct ssc_device *ssc_request(unsigned int ssc_num)
27 struct ssc_device *ssc;
29 spin_lock(&user_lock);
30 list_for_each_entry(ssc, &ssc_list, list) {
31 if (ssc->pdev->id == ssc_num) {
38 spin_unlock(&user_lock);
39 dev_dbg(&ssc->pdev->dev, "could not find requested device\n");
40 return ERR_PTR(-ENODEV);
44 spin_unlock(&user_lock);
45 dev_dbg(&ssc->pdev->dev, "module busy\n");
46 return ERR_PTR(-EBUSY);
49 spin_unlock(&user_lock);
55 EXPORT_SYMBOL(ssc_request);
57 void ssc_free(struct ssc_device *ssc)
59 spin_lock(&user_lock);
62 clk_disable(ssc->clk);
64 dev_dbg(&ssc->pdev->dev, "device already free\n");
66 spin_unlock(&user_lock);
68 EXPORT_SYMBOL(ssc_free);
70 static int __init ssc_probe(struct platform_device *pdev)
73 struct resource *regs;
74 struct ssc_device *ssc;
76 ssc = kzalloc(sizeof(struct ssc_device), GFP_KERNEL);
78 dev_dbg(&pdev->dev, "out of memory\n");
83 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85 dev_dbg(&pdev->dev, "no mmio resource defined\n");
90 ssc->clk = clk_get(&pdev->dev, "pclk");
91 if (IS_ERR(ssc->clk)) {
92 dev_dbg(&pdev->dev, "no pclk clock defined\n");
98 ssc->regs = ioremap(regs->start, regs->end - regs->start + 1);
100 dev_dbg(&pdev->dev, "ioremap failed\n");
105 /* disable all interrupts */
106 clk_enable(ssc->clk);
107 ssc_writel(ssc->regs, IDR, ~0UL);
108 ssc_readl(ssc->regs, SR);
109 clk_disable(ssc->clk);
111 ssc->irq = platform_get_irq(pdev, 0);
113 dev_dbg(&pdev->dev, "could not get irq\n");
118 spin_lock(&user_lock);
119 list_add_tail(&ssc->list, &ssc_list);
120 spin_unlock(&user_lock);
122 platform_set_drvdata(pdev, ssc);
124 dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
125 ssc->regs, ssc->irq);
139 static int __devexit ssc_remove(struct platform_device *pdev)
141 struct ssc_device *ssc = platform_get_drvdata(pdev);
143 spin_lock(&user_lock);
146 list_del(&ssc->list);
148 spin_unlock(&user_lock);
153 static struct platform_driver ssc_driver = {
154 .remove = __devexit_p(ssc_remove),
157 .owner = THIS_MODULE,
161 static int __init ssc_init(void)
163 return platform_driver_probe(&ssc_driver, ssc_probe);
165 module_init(ssc_init);
167 static void __exit ssc_exit(void)
169 platform_driver_unregister(&ssc_driver);
171 module_exit(ssc_exit);
173 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
174 MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91");
175 MODULE_LICENSE("GPL");
176 MODULE_ALIAS("platform:ssc");