be6fee78f439dd04ee14ff2cf0f495a77ce15f07
[safe/jmp/linux-2.6] / arch / arm / mach-pxa / ssp.c
1 /*
2  *  linux/arch/arm/mach-pxa/ssp.c
3  *
4  *  based on linux/arch/arm/mach-sa1100/ssp.c by Russell King
5  *
6  *  Copyright (C) 2003 Russell King.
7  *  Copyright (C) 2003 Wolfson Microelectronics PLC
8  *
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.
12  *
13  *  PXA2xx SSP driver.  This provides the generic core for simple
14  *  IO-based SSP applications and allows easy port setup for DMA access.
15  *
16  *  Author: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
17  *
18  *  Revision history:
19  *   22nd Aug 2003 Initial version.
20  *   20th Dec 2004 Added ssp_config for changing port config without
21  *                 closing the port.
22  *    4th Aug 2005 Added option to disable irq handler registration and
23  *                 cleaned up irq and clock detection.
24  */
25
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>
38
39 #include <asm/io.h>
40 #include <asm/irq.h>
41 #include <asm/hardware.h>
42 #include <asm/arch/ssp.h>
43 #include <asm/arch/pxa-regs.h>
44
45 #define TIMEOUT 100000
46
47 static irqreturn_t ssp_interrupt(int irq, void *dev_id)
48 {
49         struct ssp_dev *dev = (struct ssp_dev*) dev_id;
50         unsigned int status = SSSR_P(dev->port);
51
52         SSSR_P(dev->port) = status; /* clear status bits */
53
54         if (status & SSSR_ROR)
55                 printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port);
56
57         if (status & SSSR_TUR)
58                 printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port);
59
60         if (status & SSSR_BCE)
61                 printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port);
62
63         return IRQ_HANDLED;
64 }
65
66 /**
67  * ssp_write_word - write a word to the SSP port
68  * @data: 32-bit, MSB justified data to write.
69  *
70  * Wait for a free entry in the SSP transmit FIFO, and write a data
71  * word to the SSP port.
72  *
73  * The caller is expected to perform the necessary locking.
74  *
75  * Returns:
76  *   %-ETIMEDOUT        timeout occurred
77  *   0                  success
78  */
79 int ssp_write_word(struct ssp_dev *dev, u32 data)
80 {
81         int timeout = TIMEOUT;
82
83         while (!(SSSR_P(dev->port) & SSSR_TNF)) {
84                 if (!--timeout)
85                         return -ETIMEDOUT;
86                 cpu_relax();
87         }
88
89         SSDR_P(dev->port) = data;
90
91         return 0;
92 }
93
94 /**
95  * ssp_read_word - read a word from the SSP port
96  *
97  * Wait for a data word in the SSP receive FIFO, and return the
98  * received data.  Data is LSB justified.
99  *
100  * Note: Currently, if data is not expected to be received, this
101  * function will wait for ever.
102  *
103  * The caller is expected to perform the necessary locking.
104  *
105  * Returns:
106  *   %-ETIMEDOUT        timeout occurred
107  *   32-bit data        success
108  */
109 int ssp_read_word(struct ssp_dev *dev, u32 *data)
110 {
111         int timeout = TIMEOUT;
112
113         while (!(SSSR_P(dev->port) & SSSR_RNE)) {
114                 if (!--timeout)
115                         return -ETIMEDOUT;
116                 cpu_relax();
117         }
118
119         *data = SSDR_P(dev->port);
120         return 0;
121 }
122
123 /**
124  * ssp_flush - flush the transmit and receive FIFOs
125  *
126  * Wait for the SSP to idle, and ensure that the receive FIFO
127  * is empty.
128  *
129  * The caller is expected to perform the necessary locking.
130  */
131 int ssp_flush(struct ssp_dev *dev)
132 {
133         int timeout = TIMEOUT * 2;
134
135         do {
136                 while (SSSR_P(dev->port) & SSSR_RNE) {
137                         if (!--timeout)
138                                 return -ETIMEDOUT;
139                         (void) SSDR_P(dev->port);
140                 }
141                 if (!--timeout)
142                         return -ETIMEDOUT;
143         } while (SSSR_P(dev->port) & SSSR_BSY);
144
145         return 0;
146 }
147
148 /**
149  * ssp_enable - enable the SSP port
150  *
151  * Turn on the SSP port.
152  */
153 void ssp_enable(struct ssp_dev *dev)
154 {
155         SSCR0_P(dev->port) |= SSCR0_SSE;
156 }
157
158 /**
159  * ssp_disable - shut down the SSP port
160  *
161  * Turn off the SSP port, optionally powering it down.
162  */
163 void ssp_disable(struct ssp_dev *dev)
164 {
165         SSCR0_P(dev->port) &= ~SSCR0_SSE;
166 }
167
168 /**
169  * ssp_save_state - save the SSP configuration
170  * @ssp: pointer to structure to save SSP configuration
171  *
172  * Save the configured SSP state for suspend.
173  */
174 void ssp_save_state(struct ssp_dev *dev, struct ssp_state *ssp)
175 {
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);
180
181         SSCR0_P(dev->port) &= ~SSCR0_SSE;
182 }
183
184 /**
185  * ssp_restore_state - restore a previously saved SSP configuration
186  * @ssp: pointer to configuration saved by ssp_save_state
187  *
188  * Restore the SSP configuration saved previously by ssp_save_state.
189  */
190 void ssp_restore_state(struct ssp_dev *dev, struct ssp_state *ssp)
191 {
192         SSSR_P(dev->port) = SSSR_ROR | SSSR_TUR | SSSR_BCE;
193
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;
198
199         SSCR0_P(dev->port) = ssp->cr0;
200 }
201
202 /**
203  * ssp_config - configure SSP port settings
204  * @mode: port operating mode
205  * @flags: port config flags
206  * @psp_flags: port PSP config flags
207  * @speed: port speed
208  *
209  * Port MUST be disabled by ssp_disable before making any config changes.
210  */
211 int ssp_config(struct ssp_dev *dev, u32 mode, u32 flags, u32 psp_flags, u32 speed)
212 {
213         dev->mode = mode;
214         dev->flags = flags;
215         dev->psp_flags = psp_flags;
216         dev->speed = speed;
217
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;
222
223         return 0;
224 }
225
226 /**
227  * ssp_init - setup the SSP port
228  *
229  * initialise and claim resources for the SSP port.
230  *
231  * Returns:
232  *   %-ENODEV   if the SSP port is unavailable
233  *   %-EBUSY    if the resources are already in use
234  *   %0         on success
235  */
236 int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags)
237 {
238         struct ssp_device *ssp;
239         int ret;
240
241         ssp = ssp_request(port, "SSP");
242         if (ssp == NULL)
243                 return -ENODEV;
244
245         dev->ssp = ssp;
246         dev->port = port;
247
248         /* do we need to get irq */
249         if (!(init_flags & SSP_NO_IRQ)) {
250                 ret = request_irq(ssp->irq, ssp_interrupt,
251                                 0, "SSP", dev);
252                 if (ret)
253                         goto out_region;
254                 dev->irq = ssp->irq;
255         } else
256                 dev->irq = 0;
257
258         /* turn on SSP port clock */
259         clk_enable(ssp->clk);
260         return 0;
261
262 out_region:
263         ssp_free(ssp);
264         return ret;
265 }
266
267 /**
268  * ssp_exit - undo the effects of ssp_init
269  *
270  * release and free resources for the SSP port.
271  */
272 void ssp_exit(struct ssp_dev *dev)
273 {
274         struct ssp_device *ssp = dev->ssp;
275
276         SSCR0_P(dev->port) &= ~SSCR0_SSE;
277         free_irq(dev->irq, dev);
278         clk_disable(ssp->clk);
279         ssp_free(ssp);
280 }
281
282 static DEFINE_MUTEX(ssp_lock);
283 static LIST_HEAD(ssp_list);
284
285 struct ssp_device *ssp_request(int port, const char *label)
286 {
287         struct ssp_device *ssp = NULL;
288
289         mutex_lock(&ssp_lock);
290
291         list_for_each_entry(ssp, &ssp_list, node) {
292                 if (ssp->port_id == port && ssp->use_count == 0) {
293                         ssp->use_count++;
294                         ssp->label = label;
295                         break;
296                 }
297         }
298
299         mutex_unlock(&ssp_lock);
300
301         if (ssp->port_id != port)
302                 return NULL;
303
304         return ssp;
305 }
306 EXPORT_SYMBOL(ssp_request);
307
308 void ssp_free(struct ssp_device *ssp)
309 {
310         mutex_lock(&ssp_lock);
311         if (ssp->use_count) {
312                 ssp->use_count--;
313                 ssp->label = NULL;
314         } else
315                 dev_err(&ssp->pdev->dev, "device already free\n");
316         mutex_unlock(&ssp_lock);
317 }
318 EXPORT_SYMBOL(ssp_free);
319
320 static int __devinit ssp_probe(struct platform_device *pdev, int type)
321 {
322         struct resource *res;
323         struct ssp_device *ssp;
324         int ret = 0;
325
326         ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL);
327         if (ssp == NULL) {
328                 dev_err(&pdev->dev, "failed to allocate memory");
329                 return -ENOMEM;
330         }
331
332         ssp->clk = clk_get(&pdev->dev, "SSPCLK");
333         if (IS_ERR(ssp->clk)) {
334                 ret = PTR_ERR(ssp->clk);
335                 goto err_free;
336         }
337
338         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
339         if (res == NULL) {
340                 dev_err(&pdev->dev, "no memory resource defined\n");
341                 ret = -ENODEV;
342                 goto err_free_clk;
343         }
344
345         res = request_mem_region(res->start, res->end - res->start + 1,
346                         pdev->name);
347         if (res == NULL) {
348                 dev_err(&pdev->dev, "failed to request memory resource\n");
349                 ret = -EBUSY;
350                 goto err_free_clk;
351         }
352
353         ssp->phys_base = res->start;
354
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");
358                 ret = -ENODEV;
359                 goto err_free_mem;
360         }
361
362         ssp->irq = platform_get_irq(pdev, 0);
363         if (ssp->irq < 0) {
364                 dev_err(&pdev->dev, "no IRQ resource defined\n");
365                 ret = -ENODEV;
366                 goto err_free_io;
367         }
368
369         res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
370         if (res == NULL) {
371                 dev_err(&pdev->dev, "no SSP RX DRCMR defined\n");
372                 ret = -ENODEV;
373                 goto err_free_io;
374         }
375         ssp->drcmr_rx = res->start;
376
377         res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
378         if (res == NULL) {
379                 dev_err(&pdev->dev, "no SSP TX DRCMR defined\n");
380                 ret = -ENODEV;
381                 goto err_free_io;
382         }
383         ssp->drcmr_tx = res->start;
384
385         /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id
386          * starts from 0, do a translation here
387          */
388         ssp->port_id = pdev->id + 1;
389         ssp->use_count = 0;
390         ssp->type = type;
391
392         mutex_lock(&ssp_lock);
393         list_add(&ssp->node, &ssp_list);
394         mutex_unlock(&ssp_lock);
395
396         platform_set_drvdata(pdev, ssp);
397         return 0;
398
399 err_free_io:
400         iounmap(ssp->mmio_base);
401 err_free_mem:
402         release_mem_region(res->start, res->end - res->start + 1);
403 err_free_clk:
404         clk_put(ssp->clk);
405 err_free:
406         kfree(ssp);
407         return ret;
408 }
409
410 static int __devexit ssp_remove(struct platform_device *pdev)
411 {
412         struct resource *res;
413         struct ssp_device *ssp;
414
415         ssp = platform_get_drvdata(pdev);
416         if (ssp == NULL)
417                 return -ENODEV;
418
419         iounmap(ssp->mmio_base);
420
421         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
422         release_mem_region(res->start, res->end - res->start + 1);
423
424         clk_put(ssp->clk);
425
426         mutex_lock(&ssp_lock);
427         list_del(&ssp->node);
428         mutex_unlock(&ssp_lock);
429
430         kfree(ssp);
431         return 0;
432 }
433
434 static int __devinit pxa25x_ssp_probe(struct platform_device *pdev)
435 {
436         return ssp_probe(pdev, PXA25x_SSP);
437 }
438
439 static int __devinit pxa25x_nssp_probe(struct platform_device *pdev)
440 {
441         return ssp_probe(pdev, PXA25x_NSSP);
442 }
443
444 static int __devinit pxa27x_ssp_probe(struct platform_device *pdev)
445 {
446         return ssp_probe(pdev, PXA27x_SSP);
447 }
448
449 static struct platform_driver pxa25x_ssp_driver = {
450         .driver         = {
451                 .name   = "pxa25x-ssp",
452         },
453         .probe          = pxa25x_ssp_probe,
454         .remove         = __devexit_p(ssp_remove),
455 };
456
457 static struct platform_driver pxa25x_nssp_driver = {
458         .driver         = {
459                 .name   = "pxa25x-nssp",
460         },
461         .probe          = pxa25x_nssp_probe,
462         .remove         = __devexit_p(ssp_remove),
463 };
464
465 static struct platform_driver pxa27x_ssp_driver = {
466         .driver         = {
467                 .name   = "pxa27x-ssp",
468         },
469         .probe          = pxa27x_ssp_probe,
470         .remove         = __devexit_p(ssp_remove),
471 };
472
473 static int __init pxa_ssp_init(void)
474 {
475         int ret = 0;
476
477         ret = platform_driver_register(&pxa25x_ssp_driver);
478         if (ret) {
479                 printk(KERN_ERR "failed to register pxa25x_ssp_driver");
480                 return ret;
481         }
482
483         ret = platform_driver_register(&pxa25x_nssp_driver);
484         if (ret) {
485                 printk(KERN_ERR "failed to register pxa25x_nssp_driver");
486                 return ret;
487         }
488
489         ret = platform_driver_register(&pxa27x_ssp_driver);
490         if (ret) {
491                 printk(KERN_ERR "failed to register pxa27x_ssp_driver");
492                 return ret;
493         }
494
495         return ret;
496 }
497
498 static void __exit pxa_ssp_exit(void)
499 {
500         platform_driver_unregister(&pxa25x_ssp_driver);
501         platform_driver_unregister(&pxa25x_nssp_driver);
502         platform_driver_unregister(&pxa27x_ssp_driver);
503 }
504
505 module_init(pxa_ssp_init);
506 module_exit(pxa_ssp_exit);
507
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);
518
519 MODULE_DESCRIPTION("PXA SSP driver");
520 MODULE_AUTHOR("Liam Girdwood");
521 MODULE_LICENSE("GPL");
522