2 * linux/arch/arm/mach-pxa/ssp.c
4 * based on linux/arch/arm/mach-sa1100/ssp.c by Russell King
6 * Copyright (C) 2003 Russell King.
7 * Copyright (C) 2003 Wolfson Microelectronics PLC
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * PXA2xx SSP driver. This provides the generic core for simple
14 * IO-based SSP applications and allows easy port setup for DMA access.
16 * Author: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
19 * 22nd Aug 2003 Initial version.
20 * 20th Dec 2004 Added ssp_config for changing port config without
22 * 4th Aug 2005 Added option to disable irq handler registration and
23 * cleaned up irq and clock detection.
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/errno.h>
31 #include <linux/interrupt.h>
32 #include <linux/ioport.h>
33 #include <linux/init.h>
34 #include <linux/mutex.h>
35 #include <linux/clk.h>
36 #include <linux/err.h>
37 #include <linux/platform_device.h>
41 #include <asm/hardware.h>
42 #include <asm/arch/ssp.h>
43 #include <asm/arch/pxa-regs.h>
45 #define TIMEOUT 100000
47 static irqreturn_t ssp_interrupt(int irq, void *dev_id)
49 struct ssp_dev *dev = (struct ssp_dev*) dev_id;
50 unsigned int status = SSSR_P(dev->port);
52 SSSR_P(dev->port) = status; /* clear status bits */
54 if (status & SSSR_ROR)
55 printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port);
57 if (status & SSSR_TUR)
58 printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port);
60 if (status & SSSR_BCE)
61 printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port);
67 * ssp_write_word - write a word to the SSP port
68 * @data: 32-bit, MSB justified data to write.
70 * Wait for a free entry in the SSP transmit FIFO, and write a data
71 * word to the SSP port.
73 * The caller is expected to perform the necessary locking.
76 * %-ETIMEDOUT timeout occurred
79 int ssp_write_word(struct ssp_dev *dev, u32 data)
81 int timeout = TIMEOUT;
83 while (!(SSSR_P(dev->port) & SSSR_TNF)) {
89 SSDR_P(dev->port) = data;
95 * ssp_read_word - read a word from the SSP port
97 * Wait for a data word in the SSP receive FIFO, and return the
98 * received data. Data is LSB justified.
100 * Note: Currently, if data is not expected to be received, this
101 * function will wait for ever.
103 * The caller is expected to perform the necessary locking.
106 * %-ETIMEDOUT timeout occurred
107 * 32-bit data success
109 int ssp_read_word(struct ssp_dev *dev, u32 *data)
111 int timeout = TIMEOUT;
113 while (!(SSSR_P(dev->port) & SSSR_RNE)) {
119 *data = SSDR_P(dev->port);
124 * ssp_flush - flush the transmit and receive FIFOs
126 * Wait for the SSP to idle, and ensure that the receive FIFO
129 * The caller is expected to perform the necessary locking.
131 int ssp_flush(struct ssp_dev *dev)
133 int timeout = TIMEOUT * 2;
136 while (SSSR_P(dev->port) & SSSR_RNE) {
139 (void) SSDR_P(dev->port);
143 } while (SSSR_P(dev->port) & SSSR_BSY);
149 * ssp_enable - enable the SSP port
151 * Turn on the SSP port.
153 void ssp_enable(struct ssp_dev *dev)
155 SSCR0_P(dev->port) |= SSCR0_SSE;
159 * ssp_disable - shut down the SSP port
161 * Turn off the SSP port, optionally powering it down.
163 void ssp_disable(struct ssp_dev *dev)
165 SSCR0_P(dev->port) &= ~SSCR0_SSE;
169 * ssp_save_state - save the SSP configuration
170 * @ssp: pointer to structure to save SSP configuration
172 * Save the configured SSP state for suspend.
174 void ssp_save_state(struct ssp_dev *dev, struct ssp_state *ssp)
176 ssp->cr0 = SSCR0_P(dev->port);
177 ssp->cr1 = SSCR1_P(dev->port);
178 ssp->to = SSTO_P(dev->port);
179 ssp->psp = SSPSP_P(dev->port);
181 SSCR0_P(dev->port) &= ~SSCR0_SSE;
185 * ssp_restore_state - restore a previously saved SSP configuration
186 * @ssp: pointer to configuration saved by ssp_save_state
188 * Restore the SSP configuration saved previously by ssp_save_state.
190 void ssp_restore_state(struct ssp_dev *dev, struct ssp_state *ssp)
192 SSSR_P(dev->port) = SSSR_ROR | SSSR_TUR | SSSR_BCE;
194 SSCR0_P(dev->port) = ssp->cr0 & ~SSCR0_SSE;
195 SSCR1_P(dev->port) = ssp->cr1;
196 SSTO_P(dev->port) = ssp->to;
197 SSPSP_P(dev->port) = ssp->psp;
199 SSCR0_P(dev->port) = ssp->cr0;
203 * ssp_config - configure SSP port settings
204 * @mode: port operating mode
205 * @flags: port config flags
206 * @psp_flags: port PSP config flags
209 * Port MUST be disabled by ssp_disable before making any config changes.
211 int ssp_config(struct ssp_dev *dev, u32 mode, u32 flags, u32 psp_flags, u32 speed)
215 dev->psp_flags = psp_flags;
218 /* set up port type, speed, port settings */
219 SSCR0_P(dev->port) = (dev->speed | dev->mode);
220 SSCR1_P(dev->port) = dev->flags;
221 SSPSP_P(dev->port) = dev->psp_flags;
227 * ssp_init - setup the SSP port
229 * initialise and claim resources for the SSP port.
232 * %-ENODEV if the SSP port is unavailable
233 * %-EBUSY if the resources are already in use
236 int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags)
238 struct ssp_device *ssp;
241 ssp = ssp_request(port, "SSP");
248 /* do we need to get irq */
249 if (!(init_flags & SSP_NO_IRQ)) {
250 ret = request_irq(ssp->irq, ssp_interrupt,
258 /* turn on SSP port clock */
259 clk_enable(ssp->clk);
268 * ssp_exit - undo the effects of ssp_init
270 * release and free resources for the SSP port.
272 void ssp_exit(struct ssp_dev *dev)
274 struct ssp_device *ssp = dev->ssp;
276 SSCR0_P(dev->port) &= ~SSCR0_SSE;
277 free_irq(dev->irq, dev);
278 clk_disable(ssp->clk);
282 static DEFINE_MUTEX(ssp_lock);
283 static LIST_HEAD(ssp_list);
285 struct ssp_device *ssp_request(int port, const char *label)
287 struct ssp_device *ssp = NULL;
289 mutex_lock(&ssp_lock);
291 list_for_each_entry(ssp, &ssp_list, node) {
292 if (ssp->port_id == port && ssp->use_count == 0) {
299 mutex_unlock(&ssp_lock);
301 if (ssp->port_id != port)
306 EXPORT_SYMBOL(ssp_request);
308 void ssp_free(struct ssp_device *ssp)
310 mutex_lock(&ssp_lock);
311 if (ssp->use_count) {
315 dev_err(&ssp->pdev->dev, "device already free\n");
316 mutex_unlock(&ssp_lock);
318 EXPORT_SYMBOL(ssp_free);
320 static int __devinit ssp_probe(struct platform_device *pdev, int type)
322 struct resource *res;
323 struct ssp_device *ssp;
326 ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
328 dev_err(&pdev->dev, "failed to allocate memory");
332 ssp->clk = clk_get(&pdev->dev, "SSPCLK");
333 if (IS_ERR(ssp->clk)) {
334 ret = PTR_ERR(ssp->clk);
338 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
340 dev_err(&pdev->dev, "no memory resource defined\n");
345 res = request_mem_region(res->start, res->end - res->start + 1,
348 dev_err(&pdev->dev, "failed to request memory resource\n");
353 ssp->phys_base = res->start;
355 ssp->mmio_base = ioremap(res->start, res->end - res->start + 1);
356 if (ssp->mmio_base == NULL) {
357 dev_err(&pdev->dev, "failed to ioremap() registers\n");
362 ssp->irq = platform_get_irq(pdev, 0);
364 dev_err(&pdev->dev, "no IRQ resource defined\n");
369 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
371 dev_err(&pdev->dev, "no SSP RX DRCMR defined\n");
375 ssp->drcmr_rx = res->start;
377 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
379 dev_err(&pdev->dev, "no SSP TX DRCMR defined\n");
383 ssp->drcmr_tx = res->start;
385 /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
386 * starts from 0, do a translation here
388 ssp->port_id = pdev->id + 1;
392 mutex_lock(&ssp_lock);
393 list_add(&ssp->node, &ssp_list);
394 mutex_unlock(&ssp_lock);
396 platform_set_drvdata(pdev, ssp);
400 iounmap(ssp->mmio_base);
402 release_mem_region(res->start, res->end - res->start + 1);
410 static int __devexit ssp_remove(struct platform_device *pdev)
412 struct resource *res;
413 struct ssp_device *ssp;
415 ssp = platform_get_drvdata(pdev);
419 iounmap(ssp->mmio_base);
421 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
422 release_mem_region(res->start, res->end - res->start + 1);
426 mutex_lock(&ssp_lock);
427 list_del(&ssp->node);
428 mutex_unlock(&ssp_lock);
434 static int __devinit pxa25x_ssp_probe(struct platform_device *pdev)
436 return ssp_probe(pdev, PXA25x_SSP);
439 static int __devinit pxa25x_nssp_probe(struct platform_device *pdev)
441 return ssp_probe(pdev, PXA25x_NSSP);
444 static int __devinit pxa27x_ssp_probe(struct platform_device *pdev)
446 return ssp_probe(pdev, PXA27x_SSP);
449 static struct platform_driver pxa25x_ssp_driver = {
451 .name = "pxa25x-ssp",
453 .probe = pxa25x_ssp_probe,
454 .remove = __devexit_p(ssp_remove),
457 static struct platform_driver pxa25x_nssp_driver = {
459 .name = "pxa25x-nssp",
461 .probe = pxa25x_nssp_probe,
462 .remove = __devexit_p(ssp_remove),
465 static struct platform_driver pxa27x_ssp_driver = {
467 .name = "pxa27x-ssp",
469 .probe = pxa27x_ssp_probe,
470 .remove = __devexit_p(ssp_remove),
473 static int __init pxa_ssp_init(void)
477 ret = platform_driver_register(&pxa25x_ssp_driver);
479 printk(KERN_ERR "failed to register pxa25x_ssp_driver");
483 ret = platform_driver_register(&pxa25x_nssp_driver);
485 printk(KERN_ERR "failed to register pxa25x_nssp_driver");
489 ret = platform_driver_register(&pxa27x_ssp_driver);
491 printk(KERN_ERR "failed to register pxa27x_ssp_driver");
498 static void __exit pxa_ssp_exit(void)
500 platform_driver_unregister(&pxa25x_ssp_driver);
501 platform_driver_unregister(&pxa25x_nssp_driver);
502 platform_driver_unregister(&pxa27x_ssp_driver);
505 module_init(pxa_ssp_init);
506 module_exit(pxa_ssp_exit);
508 EXPORT_SYMBOL(ssp_write_word);
509 EXPORT_SYMBOL(ssp_read_word);
510 EXPORT_SYMBOL(ssp_flush);
511 EXPORT_SYMBOL(ssp_enable);
512 EXPORT_SYMBOL(ssp_disable);
513 EXPORT_SYMBOL(ssp_save_state);
514 EXPORT_SYMBOL(ssp_restore_state);
515 EXPORT_SYMBOL(ssp_init);
516 EXPORT_SYMBOL(ssp_exit);
517 EXPORT_SYMBOL(ssp_config);
519 MODULE_DESCRIPTION("PXA SSP driver");
520 MODULE_AUTHOR("Liam Girdwood");
521 MODULE_LICENSE("GPL");