ARM: move LED support code out of arch/arm/kernel/time.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / mach-pxa / ssp.c
blob9ebe658590fa5583ac817aa4dce552f709ebbbab
1 /*
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 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/ioport.h>
26 #include <linux/init.h>
27 #include <linux/mutex.h>
28 #include <linux/clk.h>
29 #include <linux/err.h>
30 #include <linux/platform_device.h>
31 #include <linux/io.h>
33 #include <asm/irq.h>
34 #include <mach/hardware.h>
35 #include <mach/ssp.h>
36 #include <mach/regs-ssp.h>
38 #define TIMEOUT 100000
40 static irqreturn_t ssp_interrupt(int irq, void *dev_id)
42 struct ssp_dev *dev = dev_id;
43 struct ssp_device *ssp = dev->ssp;
44 unsigned int status;
46 status = __raw_readl(ssp->mmio_base + SSSR);
47 __raw_writel(status, ssp->mmio_base + SSSR);
49 if (status & SSSR_ROR)
50 printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port);
52 if (status & SSSR_TUR)
53 printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port);
55 if (status & SSSR_BCE)
56 printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port);
58 return IRQ_HANDLED;
61 /**
62 * ssp_write_word - write a word to the SSP port
63 * @data: 32-bit, MSB justified data to write.
65 * Wait for a free entry in the SSP transmit FIFO, and write a data
66 * word to the SSP port.
68 * The caller is expected to perform the necessary locking.
70 * Returns:
71 * %-ETIMEDOUT timeout occurred
72 * 0 success
74 int ssp_write_word(struct ssp_dev *dev, u32 data)
76 struct ssp_device *ssp = dev->ssp;
77 int timeout = TIMEOUT;
79 while (!(__raw_readl(ssp->mmio_base + SSSR) & SSSR_TNF)) {
80 if (!--timeout)
81 return -ETIMEDOUT;
82 cpu_relax();
85 __raw_writel(data, ssp->mmio_base + SSDR);
87 return 0;
90 /**
91 * ssp_read_word - read a word from the SSP port
93 * Wait for a data word in the SSP receive FIFO, and return the
94 * received data. Data is LSB justified.
96 * Note: Currently, if data is not expected to be received, this
97 * function will wait for ever.
99 * The caller is expected to perform the necessary locking.
101 * Returns:
102 * %-ETIMEDOUT timeout occurred
103 * 32-bit data success
105 int ssp_read_word(struct ssp_dev *dev, u32 *data)
107 struct ssp_device *ssp = dev->ssp;
108 int timeout = TIMEOUT;
110 while (!(__raw_readl(ssp->mmio_base + SSSR) & SSSR_RNE)) {
111 if (!--timeout)
112 return -ETIMEDOUT;
113 cpu_relax();
116 *data = __raw_readl(ssp->mmio_base + SSDR);
117 return 0;
121 * ssp_flush - flush the transmit and receive FIFOs
123 * Wait for the SSP to idle, and ensure that the receive FIFO
124 * is empty.
126 * The caller is expected to perform the necessary locking.
128 int ssp_flush(struct ssp_dev *dev)
130 struct ssp_device *ssp = dev->ssp;
131 int timeout = TIMEOUT * 2;
133 /* ensure TX FIFO is empty instead of not full */
134 if (cpu_is_pxa3xx()) {
135 while (__raw_readl(ssp->mmio_base + SSSR) & 0xf00) {
136 if (!--timeout)
137 return -ETIMEDOUT;
138 cpu_relax();
140 timeout = TIMEOUT * 2;
143 do {
144 while (__raw_readl(ssp->mmio_base + SSSR) & SSSR_RNE) {
145 if (!--timeout)
146 return -ETIMEDOUT;
147 (void)__raw_readl(ssp->mmio_base + SSDR);
149 if (!--timeout)
150 return -ETIMEDOUT;
151 } while (__raw_readl(ssp->mmio_base + SSSR) & SSSR_BSY);
153 return 0;
157 * ssp_enable - enable the SSP port
159 * Turn on the SSP port.
161 void ssp_enable(struct ssp_dev *dev)
163 struct ssp_device *ssp = dev->ssp;
164 uint32_t sscr0;
166 sscr0 = __raw_readl(ssp->mmio_base + SSCR0);
167 sscr0 |= SSCR0_SSE;
168 __raw_writel(sscr0, ssp->mmio_base + SSCR0);
172 * ssp_disable - shut down the SSP port
174 * Turn off the SSP port, optionally powering it down.
176 void ssp_disable(struct ssp_dev *dev)
178 struct ssp_device *ssp = dev->ssp;
179 uint32_t sscr0;
181 sscr0 = __raw_readl(ssp->mmio_base + SSCR0);
182 sscr0 &= ~SSCR0_SSE;
183 __raw_writel(sscr0, ssp->mmio_base + SSCR0);
187 * ssp_save_state - save the SSP configuration
188 * @ssp: pointer to structure to save SSP configuration
190 * Save the configured SSP state for suspend.
192 void ssp_save_state(struct ssp_dev *dev, struct ssp_state *state)
194 struct ssp_device *ssp = dev->ssp;
196 state->cr0 = __raw_readl(ssp->mmio_base + SSCR0);
197 state->cr1 = __raw_readl(ssp->mmio_base + SSCR1);
198 state->to = __raw_readl(ssp->mmio_base + SSTO);
199 state->psp = __raw_readl(ssp->mmio_base + SSPSP);
201 ssp_disable(dev);
205 * ssp_restore_state - restore a previously saved SSP configuration
206 * @ssp: pointer to configuration saved by ssp_save_state
208 * Restore the SSP configuration saved previously by ssp_save_state.
210 void ssp_restore_state(struct ssp_dev *dev, struct ssp_state *state)
212 struct ssp_device *ssp = dev->ssp;
213 uint32_t sssr = SSSR_ROR | SSSR_TUR | SSSR_BCE;
215 __raw_writel(sssr, ssp->mmio_base + SSSR);
217 __raw_writel(state->cr0 & ~SSCR0_SSE, ssp->mmio_base + SSCR0);
218 __raw_writel(state->cr1, ssp->mmio_base + SSCR1);
219 __raw_writel(state->to, ssp->mmio_base + SSTO);
220 __raw_writel(state->psp, ssp->mmio_base + SSPSP);
221 __raw_writel(state->cr0, ssp->mmio_base + SSCR0);
225 * ssp_config - configure SSP port settings
226 * @mode: port operating mode
227 * @flags: port config flags
228 * @psp_flags: port PSP config flags
229 * @speed: port speed
231 * Port MUST be disabled by ssp_disable before making any config changes.
233 int ssp_config(struct ssp_dev *dev, u32 mode, u32 flags, u32 psp_flags, u32 speed)
235 struct ssp_device *ssp = dev->ssp;
237 dev->mode = mode;
238 dev->flags = flags;
239 dev->psp_flags = psp_flags;
240 dev->speed = speed;
242 /* set up port type, speed, port settings */
243 __raw_writel((dev->speed | dev->mode), ssp->mmio_base + SSCR0);
244 __raw_writel(dev->flags, ssp->mmio_base + SSCR1);
245 __raw_writel(dev->psp_flags, ssp->mmio_base + SSPSP);
247 return 0;
251 * ssp_init - setup the SSP port
253 * initialise and claim resources for the SSP port.
255 * Returns:
256 * %-ENODEV if the SSP port is unavailable
257 * %-EBUSY if the resources are already in use
258 * %0 on success
260 int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags)
262 struct ssp_device *ssp;
263 int ret;
265 ssp = ssp_request(port, "SSP");
266 if (ssp == NULL)
267 return -ENODEV;
269 dev->ssp = ssp;
270 dev->port = port;
272 /* do we need to get irq */
273 if (!(init_flags & SSP_NO_IRQ)) {
274 ret = request_irq(ssp->irq, ssp_interrupt,
275 0, "SSP", dev);
276 if (ret)
277 goto out_region;
278 dev->irq = ssp->irq;
279 } else
280 dev->irq = NO_IRQ;
282 /* turn on SSP port clock */
283 clk_enable(ssp->clk);
284 return 0;
286 out_region:
287 ssp_free(ssp);
288 return ret;
292 * ssp_exit - undo the effects of ssp_init
294 * release and free resources for the SSP port.
296 void ssp_exit(struct ssp_dev *dev)
298 struct ssp_device *ssp = dev->ssp;
300 ssp_disable(dev);
301 if (dev->irq != NO_IRQ)
302 free_irq(dev->irq, dev);
303 clk_disable(ssp->clk);
304 ssp_free(ssp);
307 static DEFINE_MUTEX(ssp_lock);
308 static LIST_HEAD(ssp_list);
310 struct ssp_device *ssp_request(int port, const char *label)
312 struct ssp_device *ssp = NULL;
314 mutex_lock(&ssp_lock);
316 list_for_each_entry(ssp, &ssp_list, node) {
317 if (ssp->port_id == port && ssp->use_count == 0) {
318 ssp->use_count++;
319 ssp->label = label;
320 break;
324 mutex_unlock(&ssp_lock);
326 if (&ssp->node == &ssp_list)
327 return NULL;
329 return ssp;
331 EXPORT_SYMBOL(ssp_request);
333 void ssp_free(struct ssp_device *ssp)
335 mutex_lock(&ssp_lock);
336 if (ssp->use_count) {
337 ssp->use_count--;
338 ssp->label = NULL;
339 } else
340 dev_err(&ssp->pdev->dev, "device already free\n");
341 mutex_unlock(&ssp_lock);
343 EXPORT_SYMBOL(ssp_free);
345 static int __devinit ssp_probe(struct platform_device *pdev)
347 const struct platform_device_id *id = platform_get_device_id(pdev);
348 struct resource *res;
349 struct ssp_device *ssp;
350 int ret = 0;
352 ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
353 if (ssp == NULL) {
354 dev_err(&pdev->dev, "failed to allocate memory");
355 return -ENOMEM;
357 ssp->pdev = pdev;
359 ssp->clk = clk_get(&pdev->dev, NULL);
360 if (IS_ERR(ssp->clk)) {
361 ret = PTR_ERR(ssp->clk);
362 goto err_free;
365 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
366 if (res == NULL) {
367 dev_err(&pdev->dev, "no memory resource defined\n");
368 ret = -ENODEV;
369 goto err_free_clk;
372 res = request_mem_region(res->start, res->end - res->start + 1,
373 pdev->name);
374 if (res == NULL) {
375 dev_err(&pdev->dev, "failed to request memory resource\n");
376 ret = -EBUSY;
377 goto err_free_clk;
380 ssp->phys_base = res->start;
382 ssp->mmio_base = ioremap(res->start, res->end - res->start + 1);
383 if (ssp->mmio_base == NULL) {
384 dev_err(&pdev->dev, "failed to ioremap() registers\n");
385 ret = -ENODEV;
386 goto err_free_mem;
389 ssp->irq = platform_get_irq(pdev, 0);
390 if (ssp->irq < 0) {
391 dev_err(&pdev->dev, "no IRQ resource defined\n");
392 ret = -ENODEV;
393 goto err_free_io;
396 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
397 if (res == NULL) {
398 dev_err(&pdev->dev, "no SSP RX DRCMR defined\n");
399 ret = -ENODEV;
400 goto err_free_io;
402 ssp->drcmr_rx = res->start;
404 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
405 if (res == NULL) {
406 dev_err(&pdev->dev, "no SSP TX DRCMR defined\n");
407 ret = -ENODEV;
408 goto err_free_io;
410 ssp->drcmr_tx = res->start;
412 /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
413 * starts from 0, do a translation here
415 ssp->port_id = pdev->id + 1;
416 ssp->use_count = 0;
417 ssp->type = (int)id->driver_data;
419 mutex_lock(&ssp_lock);
420 list_add(&ssp->node, &ssp_list);
421 mutex_unlock(&ssp_lock);
423 platform_set_drvdata(pdev, ssp);
424 return 0;
426 err_free_io:
427 iounmap(ssp->mmio_base);
428 err_free_mem:
429 release_mem_region(res->start, res->end - res->start + 1);
430 err_free_clk:
431 clk_put(ssp->clk);
432 err_free:
433 kfree(ssp);
434 return ret;
437 static int __devexit ssp_remove(struct platform_device *pdev)
439 struct resource *res;
440 struct ssp_device *ssp;
442 ssp = platform_get_drvdata(pdev);
443 if (ssp == NULL)
444 return -ENODEV;
446 iounmap(ssp->mmio_base);
448 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
449 release_mem_region(res->start, res->end - res->start + 1);
451 clk_put(ssp->clk);
453 mutex_lock(&ssp_lock);
454 list_del(&ssp->node);
455 mutex_unlock(&ssp_lock);
457 kfree(ssp);
458 return 0;
461 static const struct platform_device_id ssp_id_table[] = {
462 { "pxa25x-ssp", PXA25x_SSP },
463 { "pxa25x-nssp", PXA25x_NSSP },
464 { "pxa27x-ssp", PXA27x_SSP },
465 { },
468 static struct platform_driver ssp_driver = {
469 .probe = ssp_probe,
470 .remove = __devexit_p(ssp_remove),
471 .driver = {
472 .owner = THIS_MODULE,
473 .name = "pxa2xx-ssp",
475 .id_table = ssp_id_table,
478 static int __init pxa_ssp_init(void)
480 return platform_driver_register(&ssp_driver);
483 static void __exit pxa_ssp_exit(void)
485 platform_driver_unregister(&ssp_driver);
488 arch_initcall(pxa_ssp_init);
489 module_exit(pxa_ssp_exit);
491 EXPORT_SYMBOL(ssp_write_word);
492 EXPORT_SYMBOL(ssp_read_word);
493 EXPORT_SYMBOL(ssp_flush);
494 EXPORT_SYMBOL(ssp_enable);
495 EXPORT_SYMBOL(ssp_disable);
496 EXPORT_SYMBOL(ssp_save_state);
497 EXPORT_SYMBOL(ssp_restore_state);
498 EXPORT_SYMBOL(ssp_init);
499 EXPORT_SYMBOL(ssp_exit);
500 EXPORT_SYMBOL(ssp_config);
502 MODULE_DESCRIPTION("PXA SSP driver");
503 MODULE_AUTHOR("Liam Girdwood");
504 MODULE_LICENSE("GPL");