| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ | 
|  | 18 |  | 
|  | 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> | 
| Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame] | 27 | #include <linux/mutex.h> | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 28 | #include <linux/clk.h> | 
|  | 29 | #include <linux/err.h> | 
|  | 30 | #include <linux/platform_device.h> | 
| Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 31 | #include <linux/io.h> | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 32 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <asm/irq.h> | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 34 | #include <mach/hardware.h> | 
|  | 35 | #include <mach/ssp.h> | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 36 | #include <mach/regs-ssp.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 |  | 
| Eric Miao | a056bef | 2010-02-09 11:10:10 +0800 | [diff] [blame] | 38 | #ifdef CONFIG_PXA_SSP_LEGACY | 
|  | 39 |  | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 40 | #define TIMEOUT 100000 | 
|  | 41 |  | 
| Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 42 | static irqreturn_t ssp_interrupt(int irq, void *dev_id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { | 
| Jeff Garzik | 2a7057e | 2007-10-26 05:40:22 -0400 | [diff] [blame] | 44 | struct ssp_dev *dev = dev_id; | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 45 | struct ssp_device *ssp = dev->ssp; | 
|  | 46 | unsigned int status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 |  | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 48 | status = __raw_readl(ssp->mmio_base + SSSR); | 
|  | 49 | __raw_writel(status, ssp->mmio_base + SSSR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 |  | 
|  | 51 | if (status & SSSR_ROR) | 
|  | 52 | printk(KERN_WARNING "SSP(%d): receiver overrun\n", dev->port); | 
|  | 53 |  | 
|  | 54 | if (status & SSSR_TUR) | 
|  | 55 | printk(KERN_WARNING "SSP(%d): transmitter underrun\n", dev->port); | 
|  | 56 |  | 
|  | 57 | if (status & SSSR_BCE) | 
|  | 58 | printk(KERN_WARNING "SSP(%d): bit count error\n", dev->port); | 
|  | 59 |  | 
|  | 60 | return IRQ_HANDLED; | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | /** | 
|  | 64 | * ssp_write_word - write a word to the SSP port | 
|  | 65 | * @data: 32-bit, MSB justified data to write. | 
|  | 66 | * | 
|  | 67 | * Wait for a free entry in the SSP transmit FIFO, and write a data | 
|  | 68 | * word to the SSP port. | 
|  | 69 | * | 
|  | 70 | * The caller is expected to perform the necessary locking. | 
|  | 71 | * | 
|  | 72 | * Returns: | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 73 | *   %-ETIMEDOUT	timeout occurred | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | *   0			success | 
|  | 75 | */ | 
|  | 76 | int ssp_write_word(struct ssp_dev *dev, u32 data) | 
|  | 77 | { | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 78 | struct ssp_device *ssp = dev->ssp; | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 79 | int timeout = TIMEOUT; | 
|  | 80 |  | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 81 | while (!(__raw_readl(ssp->mmio_base + SSSR) & SSSR_TNF)) { | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 82 | if (!--timeout) | 
|  | 83 | return -ETIMEDOUT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | cpu_relax(); | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 85 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 87 | __raw_writel(data, ssp->mmio_base + SSDR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 |  | 
|  | 89 | return 0; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | /** | 
|  | 93 | * ssp_read_word - read a word from the SSP port | 
|  | 94 | * | 
|  | 95 | * Wait for a data word in the SSP receive FIFO, and return the | 
|  | 96 | * received data.  Data is LSB justified. | 
|  | 97 | * | 
|  | 98 | * Note: Currently, if data is not expected to be received, this | 
|  | 99 | * function will wait for ever. | 
|  | 100 | * | 
|  | 101 | * The caller is expected to perform the necessary locking. | 
|  | 102 | * | 
|  | 103 | * Returns: | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 104 | *   %-ETIMEDOUT	timeout occurred | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | *   32-bit data	success | 
|  | 106 | */ | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 107 | int ssp_read_word(struct ssp_dev *dev, u32 *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 109 | struct ssp_device *ssp = dev->ssp; | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 110 | int timeout = TIMEOUT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 |  | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 112 | while (!(__raw_readl(ssp->mmio_base + SSSR) & SSSR_RNE)) { | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 113 | if (!--timeout) | 
|  | 114 | return -ETIMEDOUT; | 
|  | 115 | cpu_relax(); | 
|  | 116 | } | 
|  | 117 |  | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 118 | *data = __raw_readl(ssp->mmio_base + SSDR); | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 119 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
|  | 122 | /** | 
|  | 123 | * ssp_flush - flush the transmit and receive FIFOs | 
|  | 124 | * | 
|  | 125 | * Wait for the SSP to idle, and ensure that the receive FIFO | 
|  | 126 | * is empty. | 
|  | 127 | * | 
|  | 128 | * The caller is expected to perform the necessary locking. | 
|  | 129 | */ | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 130 | int ssp_flush(struct ssp_dev *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 132 | struct ssp_device *ssp = dev->ssp; | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 133 | int timeout = TIMEOUT * 2; | 
|  | 134 |  | 
| eric miao | 732ce16 | 2007-11-23 14:55:59 +0800 | [diff] [blame] | 135 | /* ensure TX FIFO is empty instead of not full */ | 
|  | 136 | if (cpu_is_pxa3xx()) { | 
|  | 137 | while (__raw_readl(ssp->mmio_base + SSSR) & 0xf00) { | 
|  | 138 | if (!--timeout) | 
|  | 139 | return -ETIMEDOUT; | 
|  | 140 | cpu_relax(); | 
|  | 141 | } | 
|  | 142 | timeout = TIMEOUT * 2; | 
|  | 143 | } | 
|  | 144 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | do { | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 146 | while (__raw_readl(ssp->mmio_base + SSSR) & SSSR_RNE) { | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 147 | if (!--timeout) | 
|  | 148 | return -ETIMEDOUT; | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 149 | (void)__raw_readl(ssp->mmio_base + SSDR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 151 | if (!--timeout) | 
|  | 152 | return -ETIMEDOUT; | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 153 | } while (__raw_readl(ssp->mmio_base + SSSR) & SSSR_BSY); | 
| Paul Sokolovsky | 8f1bf87 | 2006-08-27 12:54:56 +0100 | [diff] [blame] | 154 |  | 
|  | 155 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
|  | 158 | /** | 
|  | 159 | * ssp_enable - enable the SSP port | 
|  | 160 | * | 
|  | 161 | * Turn on the SSP port. | 
|  | 162 | */ | 
|  | 163 | void ssp_enable(struct ssp_dev *dev) | 
|  | 164 | { | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 165 | struct ssp_device *ssp = dev->ssp; | 
|  | 166 | uint32_t sscr0; | 
|  | 167 |  | 
|  | 168 | sscr0 = __raw_readl(ssp->mmio_base + SSCR0); | 
|  | 169 | sscr0 |= SSCR0_SSE; | 
|  | 170 | __raw_writel(sscr0, ssp->mmio_base + SSCR0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
|  | 173 | /** | 
|  | 174 | * ssp_disable - shut down the SSP port | 
|  | 175 | * | 
|  | 176 | * Turn off the SSP port, optionally powering it down. | 
|  | 177 | */ | 
|  | 178 | void ssp_disable(struct ssp_dev *dev) | 
|  | 179 | { | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 180 | struct ssp_device *ssp = dev->ssp; | 
|  | 181 | uint32_t sscr0; | 
|  | 182 |  | 
|  | 183 | sscr0 = __raw_readl(ssp->mmio_base + SSCR0); | 
|  | 184 | sscr0 &= ~SSCR0_SSE; | 
|  | 185 | __raw_writel(sscr0, ssp->mmio_base + SSCR0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
|  | 188 | /** | 
|  | 189 | * ssp_save_state - save the SSP configuration | 
|  | 190 | * @ssp: pointer to structure to save SSP configuration | 
|  | 191 | * | 
|  | 192 | * Save the configured SSP state for suspend. | 
|  | 193 | */ | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 194 | void ssp_save_state(struct ssp_dev *dev, struct ssp_state *state) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 196 | struct ssp_device *ssp = dev->ssp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 |  | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 198 | state->cr0 = __raw_readl(ssp->mmio_base + SSCR0); | 
|  | 199 | state->cr1 = __raw_readl(ssp->mmio_base + SSCR1); | 
|  | 200 | state->to  = __raw_readl(ssp->mmio_base + SSTO); | 
|  | 201 | state->psp = __raw_readl(ssp->mmio_base + SSPSP); | 
|  | 202 |  | 
|  | 203 | ssp_disable(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
|  | 206 | /** | 
|  | 207 | * ssp_restore_state - restore a previously saved SSP configuration | 
|  | 208 | * @ssp: pointer to configuration saved by ssp_save_state | 
|  | 209 | * | 
|  | 210 | * Restore the SSP configuration saved previously by ssp_save_state. | 
|  | 211 | */ | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 212 | void ssp_restore_state(struct ssp_dev *dev, struct ssp_state *state) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 214 | struct ssp_device *ssp = dev->ssp; | 
|  | 215 | uint32_t sssr = SSSR_ROR | SSSR_TUR | SSSR_BCE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 |  | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 217 | __raw_writel(sssr, ssp->mmio_base + SSSR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 |  | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 219 | __raw_writel(state->cr0 & ~SSCR0_SSE, ssp->mmio_base + SSCR0); | 
|  | 220 | __raw_writel(state->cr1, ssp->mmio_base + SSCR1); | 
|  | 221 | __raw_writel(state->to,  ssp->mmio_base + SSTO); | 
|  | 222 | __raw_writel(state->psp, ssp->mmio_base + SSPSP); | 
|  | 223 | __raw_writel(state->cr0, ssp->mmio_base + SSCR0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
|  | 226 | /** | 
|  | 227 | * ssp_config - configure SSP port settings | 
|  | 228 | * @mode: port operating mode | 
|  | 229 | * @flags: port config flags | 
|  | 230 | * @psp_flags: port PSP config flags | 
|  | 231 | * @speed: port speed | 
|  | 232 | * | 
|  | 233 | * Port MUST be disabled by ssp_disable before making any config changes. | 
|  | 234 | */ | 
|  | 235 | int ssp_config(struct ssp_dev *dev, u32 mode, u32 flags, u32 psp_flags, u32 speed) | 
|  | 236 | { | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 237 | struct ssp_device *ssp = dev->ssp; | 
|  | 238 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | dev->mode = mode; | 
|  | 240 | dev->flags = flags; | 
|  | 241 | dev->psp_flags = psp_flags; | 
|  | 242 | dev->speed = speed; | 
|  | 243 |  | 
|  | 244 | /* set up port type, speed, port settings */ | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 245 | __raw_writel((dev->speed | dev->mode), ssp->mmio_base + SSCR0); | 
|  | 246 | __raw_writel(dev->flags, ssp->mmio_base + SSCR1); | 
|  | 247 | __raw_writel(dev->psp_flags, ssp->mmio_base + SSPSP); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 |  | 
|  | 249 | return 0; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | /** | 
|  | 253 | * ssp_init - setup the SSP port | 
|  | 254 | * | 
|  | 255 | * initialise and claim resources for the SSP port. | 
|  | 256 | * | 
|  | 257 | * Returns: | 
|  | 258 | *   %-ENODEV	if the SSP port is unavailable | 
|  | 259 | *   %-EBUSY	if the resources are already in use | 
|  | 260 | *   %0		on success | 
|  | 261 | */ | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 262 | int ssp_init(struct ssp_dev *dev, u32 port, u32 init_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 264 | struct ssp_device *ssp; | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 265 | int ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 |  | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 267 | ssp = ssp_request(port, "SSP"); | 
|  | 268 | if (ssp == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | return -ENODEV; | 
|  | 270 |  | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 271 | dev->ssp = ssp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | dev->port = port; | 
|  | 273 |  | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 274 | /* do we need to get irq */ | 
|  | 275 | if (!(init_flags & SSP_NO_IRQ)) { | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 276 | ret = request_irq(ssp->irq, ssp_interrupt, | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 277 | 0, "SSP", dev); | 
|  | 278 | if (ret) | 
|  | 279 | goto out_region; | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 280 | dev->irq = ssp->irq; | 
| Liam Girdwood | b216c01 | 2005-11-10 17:45:39 +0000 | [diff] [blame] | 281 | } else | 
| Mark Brown | bbae020 | 2008-06-19 03:18:09 +0100 | [diff] [blame] | 282 | dev->irq = NO_IRQ; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 |  | 
|  | 284 | /* turn on SSP port clock */ | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 285 | clk_enable(ssp->clk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | return 0; | 
|  | 287 |  | 
|  | 288 | out_region: | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 289 | ssp_free(ssp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | return ret; | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | /** | 
|  | 294 | * ssp_exit - undo the effects of ssp_init | 
|  | 295 | * | 
|  | 296 | * release and free resources for the SSP port. | 
|  | 297 | */ | 
|  | 298 | void ssp_exit(struct ssp_dev *dev) | 
|  | 299 | { | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 300 | struct ssp_device *ssp = dev->ssp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 |  | 
| eric miao | 3dcb00e | 2007-11-30 18:26:56 +0800 | [diff] [blame] | 302 | ssp_disable(dev); | 
| Mark Brown | bbae020 | 2008-06-19 03:18:09 +0100 | [diff] [blame] | 303 | if (dev->irq != NO_IRQ) | 
|  | 304 | free_irq(dev->irq, dev); | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 305 | clk_disable(ssp->clk); | 
|  | 306 | ssp_free(ssp); | 
|  | 307 | } | 
| Eric Miao | a056bef | 2010-02-09 11:10:10 +0800 | [diff] [blame] | 308 | #endif /* CONFIG_PXA_SSP_LEGACY */ | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 309 |  | 
|  | 310 | static DEFINE_MUTEX(ssp_lock); | 
|  | 311 | static LIST_HEAD(ssp_list); | 
|  | 312 |  | 
|  | 313 | struct ssp_device *ssp_request(int port, const char *label) | 
|  | 314 | { | 
|  | 315 | struct ssp_device *ssp = NULL; | 
|  | 316 |  | 
|  | 317 | mutex_lock(&ssp_lock); | 
|  | 318 |  | 
|  | 319 | list_for_each_entry(ssp, &ssp_list, node) { | 
|  | 320 | if (ssp->port_id == port && ssp->use_count == 0) { | 
|  | 321 | ssp->use_count++; | 
|  | 322 | ssp->label = label; | 
|  | 323 | break; | 
|  | 324 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } | 
|  | 326 |  | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 327 | mutex_unlock(&ssp_lock); | 
|  | 328 |  | 
| Guennadi Liakhovetski | a4aff22 | 2008-06-05 10:43:14 +0100 | [diff] [blame] | 329 | if (&ssp->node == &ssp_list) | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 330 | return NULL; | 
|  | 331 |  | 
|  | 332 | return ssp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 334 | EXPORT_SYMBOL(ssp_request); | 
|  | 335 |  | 
|  | 336 | void ssp_free(struct ssp_device *ssp) | 
|  | 337 | { | 
|  | 338 | mutex_lock(&ssp_lock); | 
|  | 339 | if (ssp->use_count) { | 
|  | 340 | ssp->use_count--; | 
|  | 341 | ssp->label = NULL; | 
|  | 342 | } else | 
|  | 343 | dev_err(&ssp->pdev->dev, "device already free\n"); | 
|  | 344 | mutex_unlock(&ssp_lock); | 
|  | 345 | } | 
|  | 346 | EXPORT_SYMBOL(ssp_free); | 
|  | 347 |  | 
| Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 348 | static int __devinit ssp_probe(struct platform_device *pdev) | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 349 | { | 
| Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 350 | const struct platform_device_id *id = platform_get_device_id(pdev); | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 351 | struct resource *res; | 
|  | 352 | struct ssp_device *ssp; | 
|  | 353 | int ret = 0; | 
|  | 354 |  | 
|  | 355 | ssp = kzalloc(sizeof(struct ssp_device), GFP_KERNEL); | 
|  | 356 | if (ssp == NULL) { | 
|  | 357 | dev_err(&pdev->dev, "failed to allocate memory"); | 
|  | 358 | return -ENOMEM; | 
|  | 359 | } | 
| Mark Brown | 919dcb2 | 2008-06-19 02:55:52 +0100 | [diff] [blame] | 360 | ssp->pdev = pdev; | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 361 |  | 
| Russell King | e0d8b13 | 2008-11-11 17:52:32 +0000 | [diff] [blame] | 362 | ssp->clk = clk_get(&pdev->dev, NULL); | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 363 | if (IS_ERR(ssp->clk)) { | 
|  | 364 | ret = PTR_ERR(ssp->clk); | 
|  | 365 | goto err_free; | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
|  | 369 | if (res == NULL) { | 
|  | 370 | dev_err(&pdev->dev, "no memory resource defined\n"); | 
|  | 371 | ret = -ENODEV; | 
|  | 372 | goto err_free_clk; | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | res = request_mem_region(res->start, res->end - res->start + 1, | 
|  | 376 | pdev->name); | 
|  | 377 | if (res == NULL) { | 
|  | 378 | dev_err(&pdev->dev, "failed to request memory resource\n"); | 
|  | 379 | ret = -EBUSY; | 
|  | 380 | goto err_free_clk; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | ssp->phys_base = res->start; | 
|  | 384 |  | 
|  | 385 | ssp->mmio_base = ioremap(res->start, res->end - res->start + 1); | 
|  | 386 | if (ssp->mmio_base == NULL) { | 
|  | 387 | dev_err(&pdev->dev, "failed to ioremap() registers\n"); | 
|  | 388 | ret = -ENODEV; | 
|  | 389 | goto err_free_mem; | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | ssp->irq = platform_get_irq(pdev, 0); | 
|  | 393 | if (ssp->irq < 0) { | 
|  | 394 | dev_err(&pdev->dev, "no IRQ resource defined\n"); | 
|  | 395 | ret = -ENODEV; | 
|  | 396 | goto err_free_io; | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | res = platform_get_resource(pdev, IORESOURCE_DMA, 0); | 
|  | 400 | if (res == NULL) { | 
|  | 401 | dev_err(&pdev->dev, "no SSP RX DRCMR defined\n"); | 
|  | 402 | ret = -ENODEV; | 
|  | 403 | goto err_free_io; | 
|  | 404 | } | 
|  | 405 | ssp->drcmr_rx = res->start; | 
|  | 406 |  | 
|  | 407 | res = platform_get_resource(pdev, IORESOURCE_DMA, 1); | 
|  | 408 | if (res == NULL) { | 
|  | 409 | dev_err(&pdev->dev, "no SSP TX DRCMR defined\n"); | 
|  | 410 | ret = -ENODEV; | 
|  | 411 | goto err_free_io; | 
|  | 412 | } | 
|  | 413 | ssp->drcmr_tx = res->start; | 
|  | 414 |  | 
|  | 415 | /* PXA2xx/3xx SSP ports starts from 1 and the internal pdev->id | 
|  | 416 | * starts from 0, do a translation here | 
|  | 417 | */ | 
|  | 418 | ssp->port_id = pdev->id + 1; | 
|  | 419 | ssp->use_count = 0; | 
| Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 420 | ssp->type = (int)id->driver_data; | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 421 |  | 
|  | 422 | mutex_lock(&ssp_lock); | 
|  | 423 | list_add(&ssp->node, &ssp_list); | 
|  | 424 | mutex_unlock(&ssp_lock); | 
|  | 425 |  | 
|  | 426 | platform_set_drvdata(pdev, ssp); | 
|  | 427 | return 0; | 
|  | 428 |  | 
|  | 429 | err_free_io: | 
|  | 430 | iounmap(ssp->mmio_base); | 
|  | 431 | err_free_mem: | 
|  | 432 | release_mem_region(res->start, res->end - res->start + 1); | 
|  | 433 | err_free_clk: | 
|  | 434 | clk_put(ssp->clk); | 
|  | 435 | err_free: | 
|  | 436 | kfree(ssp); | 
|  | 437 | return ret; | 
|  | 438 | } | 
|  | 439 |  | 
|  | 440 | static int __devexit ssp_remove(struct platform_device *pdev) | 
|  | 441 | { | 
|  | 442 | struct resource *res; | 
|  | 443 | struct ssp_device *ssp; | 
|  | 444 |  | 
|  | 445 | ssp = platform_get_drvdata(pdev); | 
|  | 446 | if (ssp == NULL) | 
|  | 447 | return -ENODEV; | 
|  | 448 |  | 
|  | 449 | iounmap(ssp->mmio_base); | 
|  | 450 |  | 
|  | 451 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | 
|  | 452 | release_mem_region(res->start, res->end - res->start + 1); | 
|  | 453 |  | 
|  | 454 | clk_put(ssp->clk); | 
|  | 455 |  | 
|  | 456 | mutex_lock(&ssp_lock); | 
|  | 457 | list_del(&ssp->node); | 
|  | 458 | mutex_unlock(&ssp_lock); | 
|  | 459 |  | 
|  | 460 | kfree(ssp); | 
|  | 461 | return 0; | 
|  | 462 | } | 
|  | 463 |  | 
| Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 464 | static const struct platform_device_id ssp_id_table[] = { | 
|  | 465 | { "pxa25x-ssp",		PXA25x_SSP }, | 
|  | 466 | { "pxa25x-nssp",	PXA25x_NSSP }, | 
|  | 467 | { "pxa27x-ssp",		PXA27x_SSP }, | 
|  | 468 | { }, | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 469 | }; | 
|  | 470 |  | 
| Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 471 | static struct platform_driver ssp_driver = { | 
|  | 472 | .probe		= ssp_probe, | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 473 | .remove		= __devexit_p(ssp_remove), | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 474 | .driver		= { | 
| Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 475 | .owner	= THIS_MODULE, | 
|  | 476 | .name	= "pxa2xx-ssp", | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 477 | }, | 
| Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 478 | .id_table	= ssp_id_table, | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 479 | }; | 
|  | 480 |  | 
|  | 481 | static int __init pxa_ssp_init(void) | 
|  | 482 | { | 
| Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 483 | return platform_driver_register(&ssp_driver); | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 484 | } | 
|  | 485 |  | 
|  | 486 | static void __exit pxa_ssp_exit(void) | 
|  | 487 | { | 
| Eric Miao | 6427d45 | 2009-10-23 00:09:47 +0800 | [diff] [blame] | 488 | platform_driver_unregister(&ssp_driver); | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 489 | } | 
|  | 490 |  | 
| Russell King | cae0554 | 2007-12-10 15:35:54 +0000 | [diff] [blame] | 491 | arch_initcall(pxa_ssp_init); | 
| eric miao | 8828645 | 2007-12-06 17:56:42 +0800 | [diff] [blame] | 492 | module_exit(pxa_ssp_exit); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 |  | 
| Eric Miao | a056bef | 2010-02-09 11:10:10 +0800 | [diff] [blame] | 494 | #ifdef CONFIG_PXA_SSP_LEGACY | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | EXPORT_SYMBOL(ssp_write_word); | 
|  | 496 | EXPORT_SYMBOL(ssp_read_word); | 
|  | 497 | EXPORT_SYMBOL(ssp_flush); | 
|  | 498 | EXPORT_SYMBOL(ssp_enable); | 
|  | 499 | EXPORT_SYMBOL(ssp_disable); | 
|  | 500 | EXPORT_SYMBOL(ssp_save_state); | 
|  | 501 | EXPORT_SYMBOL(ssp_restore_state); | 
|  | 502 | EXPORT_SYMBOL(ssp_init); | 
|  | 503 | EXPORT_SYMBOL(ssp_exit); | 
|  | 504 | EXPORT_SYMBOL(ssp_config); | 
| Eric Miao | a056bef | 2010-02-09 11:10:10 +0800 | [diff] [blame] | 505 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 |  | 
|  | 507 | MODULE_DESCRIPTION("PXA SSP driver"); | 
|  | 508 | MODULE_AUTHOR("Liam Girdwood"); | 
|  | 509 | MODULE_LICENSE("GPL"); | 
|  | 510 |  |