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