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