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