Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/plat-omap/mcbsp.c |
| 3 | * |
| 4 | * Copyright (C) 2004 Nokia Corporation |
| 5 | * Author: Samuel Ortiz <samuel.ortiz@nokia.com> |
| 6 | * |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * Multichannel mode not supported. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/device.h> |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 19 | #include <linux/wait.h> |
| 20 | #include <linux/completion.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/err.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 23 | #include <linux/clk.h> |
Tony Lindgren | 04fbf6a | 2007-02-12 10:50:53 -0800 | [diff] [blame] | 24 | #include <linux/delay.h> |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 25 | #include <linux/io.h> |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 26 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 27 | #include <mach/dma.h> |
| 28 | #include <mach/mcbsp.h> |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 29 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 30 | struct omap_mcbsp **mcbsp_ptr; |
| 31 | int omap_mcbsp_count; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 32 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 33 | void omap_mcbsp_write(void __iomem *io_base, u16 reg, u32 val) |
| 34 | { |
| 35 | if (cpu_class_is_omap1() || cpu_is_omap2420()) |
| 36 | __raw_writew((u16)val, io_base + reg); |
| 37 | else |
| 38 | __raw_writel(val, io_base + reg); |
| 39 | } |
| 40 | |
| 41 | int omap_mcbsp_read(void __iomem *io_base, u16 reg) |
| 42 | { |
| 43 | if (cpu_class_is_omap1() || cpu_is_omap2420()) |
| 44 | return __raw_readw(io_base + reg); |
| 45 | else |
| 46 | return __raw_readl(io_base + reg); |
| 47 | } |
| 48 | |
| 49 | #define OMAP_MCBSP_READ(base, reg) \ |
| 50 | omap_mcbsp_read(base, OMAP_MCBSP_REG_##reg) |
| 51 | #define OMAP_MCBSP_WRITE(base, reg, val) \ |
| 52 | omap_mcbsp_write(base, OMAP_MCBSP_REG_##reg, val) |
| 53 | |
| 54 | #define omap_mcbsp_check_valid_id(id) (id < omap_mcbsp_count) |
| 55 | #define id_to_mcbsp_ptr(id) mcbsp_ptr[id]; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 56 | |
| 57 | static void omap_mcbsp_dump_reg(u8 id) |
| 58 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 59 | struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id); |
| 60 | |
| 61 | dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id); |
| 62 | dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n", |
| 63 | OMAP_MCBSP_READ(mcbsp->io_base, DRR2)); |
| 64 | dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n", |
| 65 | OMAP_MCBSP_READ(mcbsp->io_base, DRR1)); |
| 66 | dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n", |
| 67 | OMAP_MCBSP_READ(mcbsp->io_base, DXR2)); |
| 68 | dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n", |
| 69 | OMAP_MCBSP_READ(mcbsp->io_base, DXR1)); |
| 70 | dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n", |
| 71 | OMAP_MCBSP_READ(mcbsp->io_base, SPCR2)); |
| 72 | dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n", |
| 73 | OMAP_MCBSP_READ(mcbsp->io_base, SPCR1)); |
| 74 | dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n", |
| 75 | OMAP_MCBSP_READ(mcbsp->io_base, RCR2)); |
| 76 | dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n", |
| 77 | OMAP_MCBSP_READ(mcbsp->io_base, RCR1)); |
| 78 | dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n", |
| 79 | OMAP_MCBSP_READ(mcbsp->io_base, XCR2)); |
| 80 | dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n", |
| 81 | OMAP_MCBSP_READ(mcbsp->io_base, XCR1)); |
| 82 | dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n", |
| 83 | OMAP_MCBSP_READ(mcbsp->io_base, SRGR2)); |
| 84 | dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n", |
| 85 | OMAP_MCBSP_READ(mcbsp->io_base, SRGR1)); |
| 86 | dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n", |
| 87 | OMAP_MCBSP_READ(mcbsp->io_base, PCR0)); |
| 88 | dev_dbg(mcbsp->dev, "***********************\n"); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 91 | static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 92 | { |
Jeff Garzik | e8f2af1 | 2007-10-26 05:40:25 -0400 | [diff] [blame] | 93 | struct omap_mcbsp *mcbsp_tx = dev_id; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 94 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 95 | dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", |
| 96 | OMAP_MCBSP_READ(mcbsp_tx->io_base, SPCR2)); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 97 | |
| 98 | complete(&mcbsp_tx->tx_irq_completion); |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 99 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 100 | return IRQ_HANDLED; |
| 101 | } |
| 102 | |
Linus Torvalds | 0cd61b6 | 2006-10-06 10:53:39 -0700 | [diff] [blame] | 103 | static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 104 | { |
Jeff Garzik | e8f2af1 | 2007-10-26 05:40:25 -0400 | [diff] [blame] | 105 | struct omap_mcbsp *mcbsp_rx = dev_id; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 106 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 107 | dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", |
| 108 | OMAP_MCBSP_READ(mcbsp_rx->io_base, SPCR2)); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 109 | |
| 110 | complete(&mcbsp_rx->rx_irq_completion); |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 111 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 112 | return IRQ_HANDLED; |
| 113 | } |
| 114 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 115 | static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data) |
| 116 | { |
Jeff Garzik | e8f2af1 | 2007-10-26 05:40:25 -0400 | [diff] [blame] | 117 | struct omap_mcbsp *mcbsp_dma_tx = data; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 118 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 119 | dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n", |
| 120 | OMAP_MCBSP_READ(mcbsp_dma_tx->io_base, SPCR2)); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 121 | |
| 122 | /* We can free the channels */ |
| 123 | omap_free_dma(mcbsp_dma_tx->dma_tx_lch); |
| 124 | mcbsp_dma_tx->dma_tx_lch = -1; |
| 125 | |
| 126 | complete(&mcbsp_dma_tx->tx_dma_completion); |
| 127 | } |
| 128 | |
| 129 | static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data) |
| 130 | { |
Jeff Garzik | e8f2af1 | 2007-10-26 05:40:25 -0400 | [diff] [blame] | 131 | struct omap_mcbsp *mcbsp_dma_rx = data; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 132 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 133 | dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n", |
| 134 | OMAP_MCBSP_READ(mcbsp_dma_rx->io_base, SPCR2)); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 135 | |
| 136 | /* We can free the channels */ |
| 137 | omap_free_dma(mcbsp_dma_rx->dma_rx_lch); |
| 138 | mcbsp_dma_rx->dma_rx_lch = -1; |
| 139 | |
| 140 | complete(&mcbsp_dma_rx->rx_dma_completion); |
| 141 | } |
| 142 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 143 | /* |
| 144 | * omap_mcbsp_config simply write a config to the |
| 145 | * appropriate McBSP. |
| 146 | * You either call this function or set the McBSP registers |
| 147 | * by yourself before calling omap_mcbsp_start(). |
| 148 | */ |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 149 | void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 150 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 151 | struct omap_mcbsp *mcbsp; |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 152 | void __iomem *io_base; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 153 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 154 | if (!omap_mcbsp_check_valid_id(id)) { |
| 155 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
| 156 | return; |
| 157 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 158 | mcbsp = id_to_mcbsp_ptr(id); |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 159 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 160 | io_base = mcbsp->io_base; |
| 161 | dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n", |
| 162 | mcbsp->id, mcbsp->phys_base); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 163 | |
| 164 | /* We write the given config */ |
| 165 | OMAP_MCBSP_WRITE(io_base, SPCR2, config->spcr2); |
| 166 | OMAP_MCBSP_WRITE(io_base, SPCR1, config->spcr1); |
| 167 | OMAP_MCBSP_WRITE(io_base, RCR2, config->rcr2); |
| 168 | OMAP_MCBSP_WRITE(io_base, RCR1, config->rcr1); |
| 169 | OMAP_MCBSP_WRITE(io_base, XCR2, config->xcr2); |
| 170 | OMAP_MCBSP_WRITE(io_base, XCR1, config->xcr1); |
| 171 | OMAP_MCBSP_WRITE(io_base, SRGR2, config->srgr2); |
| 172 | OMAP_MCBSP_WRITE(io_base, SRGR1, config->srgr1); |
| 173 | OMAP_MCBSP_WRITE(io_base, MCR2, config->mcr2); |
| 174 | OMAP_MCBSP_WRITE(io_base, MCR1, config->mcr1); |
| 175 | OMAP_MCBSP_WRITE(io_base, PCR0, config->pcr0); |
Tony Lindgren | 3127f8f | 2009-01-15 13:09:54 +0200 | [diff] [blame] | 176 | if (cpu_is_omap2430() || cpu_is_omap34xx()) { |
| 177 | OMAP_MCBSP_WRITE(io_base, XCCR, config->xccr); |
| 178 | OMAP_MCBSP_WRITE(io_base, RCCR, config->rccr); |
| 179 | } |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 180 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 181 | EXPORT_SYMBOL(omap_mcbsp_config); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 182 | |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 183 | /* |
| 184 | * We can choose between IRQ based or polled IO. |
| 185 | * This needs to be called before omap_mcbsp_request(). |
| 186 | */ |
| 187 | int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type) |
| 188 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 189 | struct omap_mcbsp *mcbsp; |
| 190 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 191 | if (!omap_mcbsp_check_valid_id(id)) { |
| 192 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
| 193 | return -ENODEV; |
| 194 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 195 | mcbsp = id_to_mcbsp_ptr(id); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 196 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 197 | spin_lock(&mcbsp->lock); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 198 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 199 | if (!mcbsp->free) { |
| 200 | dev_err(mcbsp->dev, "McBSP%d is currently in use\n", |
| 201 | mcbsp->id); |
| 202 | spin_unlock(&mcbsp->lock); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 203 | return -EINVAL; |
| 204 | } |
| 205 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 206 | mcbsp->io_type = io_type; |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 207 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 208 | spin_unlock(&mcbsp->lock); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 212 | EXPORT_SYMBOL(omap_mcbsp_set_io_type); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 213 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 214 | int omap_mcbsp_request(unsigned int id) |
| 215 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 216 | struct omap_mcbsp *mcbsp; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 217 | int err; |
| 218 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 219 | if (!omap_mcbsp_check_valid_id(id)) { |
| 220 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
| 221 | return -ENODEV; |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 222 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 223 | mcbsp = id_to_mcbsp_ptr(id); |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 224 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 225 | if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request) |
| 226 | mcbsp->pdata->ops->request(id); |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 227 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 228 | clk_enable(mcbsp->clk); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 229 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 230 | spin_lock(&mcbsp->lock); |
| 231 | if (!mcbsp->free) { |
| 232 | dev_err(mcbsp->dev, "McBSP%d is currently in use\n", |
| 233 | mcbsp->id); |
| 234 | spin_unlock(&mcbsp->lock); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 235 | return -1; |
| 236 | } |
| 237 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 238 | mcbsp->free = 0; |
| 239 | spin_unlock(&mcbsp->lock); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 240 | |
Jarkko Nikula | 5a07055 | 2008-10-08 10:01:41 +0300 | [diff] [blame] | 241 | /* |
| 242 | * Make sure that transmitter, receiver and sample-rate generator are |
| 243 | * not running before activating IRQs. |
| 244 | */ |
| 245 | OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR1, 0); |
| 246 | OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR2, 0); |
| 247 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 248 | if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) { |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 249 | /* We need to get IRQs here */ |
Jarkko Nikula | 5a07055 | 2008-10-08 10:01:41 +0300 | [diff] [blame] | 250 | init_completion(&mcbsp->tx_irq_completion); |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 251 | err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler, |
| 252 | 0, "McBSP", (void *)mcbsp); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 253 | if (err != 0) { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 254 | dev_err(mcbsp->dev, "Unable to request TX IRQ %d " |
| 255 | "for McBSP%d\n", mcbsp->tx_irq, |
| 256 | mcbsp->id); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 257 | return err; |
| 258 | } |
| 259 | |
Jarkko Nikula | 5a07055 | 2008-10-08 10:01:41 +0300 | [diff] [blame] | 260 | init_completion(&mcbsp->rx_irq_completion); |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 261 | err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler, |
| 262 | 0, "McBSP", (void *)mcbsp); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 263 | if (err != 0) { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 264 | dev_err(mcbsp->dev, "Unable to request RX IRQ %d " |
| 265 | "for McBSP%d\n", mcbsp->rx_irq, |
| 266 | mcbsp->id); |
| 267 | free_irq(mcbsp->tx_irq, (void *)mcbsp); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 268 | return err; |
| 269 | } |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 272 | return 0; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 273 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 274 | EXPORT_SYMBOL(omap_mcbsp_request); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 275 | |
| 276 | void omap_mcbsp_free(unsigned int id) |
| 277 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 278 | struct omap_mcbsp *mcbsp; |
| 279 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 280 | if (!omap_mcbsp_check_valid_id(id)) { |
| 281 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 282 | return; |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 283 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 284 | mcbsp = id_to_mcbsp_ptr(id); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 285 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 286 | if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free) |
| 287 | mcbsp->pdata->ops->free(id); |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 288 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 289 | clk_disable(mcbsp->clk); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 290 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 291 | spin_lock(&mcbsp->lock); |
| 292 | if (mcbsp->free) { |
| 293 | dev_err(mcbsp->dev, "McBSP%d was not reserved\n", |
| 294 | mcbsp->id); |
| 295 | spin_unlock(&mcbsp->lock); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 299 | mcbsp->free = 1; |
| 300 | spin_unlock(&mcbsp->lock); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 301 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 302 | if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) { |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 303 | /* Free IRQs */ |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 304 | free_irq(mcbsp->rx_irq, (void *)mcbsp); |
| 305 | free_irq(mcbsp->tx_irq, (void *)mcbsp); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 306 | } |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 307 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 308 | EXPORT_SYMBOL(omap_mcbsp_free); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * Here we start the McBSP, by enabling the sample |
| 312 | * generator, both transmitter and receivers, |
| 313 | * and the frame sync. |
| 314 | */ |
| 315 | void omap_mcbsp_start(unsigned int id) |
| 316 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 317 | struct omap_mcbsp *mcbsp; |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 318 | void __iomem *io_base; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 319 | u16 w; |
| 320 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 321 | if (!omap_mcbsp_check_valid_id(id)) { |
| 322 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 323 | return; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 324 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 325 | mcbsp = id_to_mcbsp_ptr(id); |
| 326 | io_base = mcbsp->io_base; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 327 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 328 | mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7; |
| 329 | mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 330 | |
| 331 | /* Start the sample generator */ |
| 332 | w = OMAP_MCBSP_READ(io_base, SPCR2); |
| 333 | OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6)); |
| 334 | |
| 335 | /* Enable transmitter and receiver */ |
| 336 | w = OMAP_MCBSP_READ(io_base, SPCR2); |
| 337 | OMAP_MCBSP_WRITE(io_base, SPCR2, w | 1); |
| 338 | |
| 339 | w = OMAP_MCBSP_READ(io_base, SPCR1); |
| 340 | OMAP_MCBSP_WRITE(io_base, SPCR1, w | 1); |
| 341 | |
| 342 | udelay(100); |
| 343 | |
| 344 | /* Start frame sync */ |
| 345 | w = OMAP_MCBSP_READ(io_base, SPCR2); |
| 346 | OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7)); |
| 347 | |
| 348 | /* Dump McBSP Regs */ |
| 349 | omap_mcbsp_dump_reg(id); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 350 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 351 | EXPORT_SYMBOL(omap_mcbsp_start); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 352 | |
| 353 | void omap_mcbsp_stop(unsigned int id) |
| 354 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 355 | struct omap_mcbsp *mcbsp; |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 356 | void __iomem *io_base; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 357 | u16 w; |
| 358 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 359 | if (!omap_mcbsp_check_valid_id(id)) { |
| 360 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 361 | return; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 362 | } |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 363 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 364 | mcbsp = id_to_mcbsp_ptr(id); |
| 365 | io_base = mcbsp->io_base; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 366 | |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 367 | /* Reset transmitter */ |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 368 | w = OMAP_MCBSP_READ(io_base, SPCR2); |
| 369 | OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1)); |
| 370 | |
| 371 | /* Reset receiver */ |
| 372 | w = OMAP_MCBSP_READ(io_base, SPCR1); |
| 373 | OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(1)); |
| 374 | |
| 375 | /* Reset the sample rate generator */ |
| 376 | w = OMAP_MCBSP_READ(io_base, SPCR2); |
| 377 | OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6)); |
| 378 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 379 | EXPORT_SYMBOL(omap_mcbsp_stop); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 380 | |
Tony Lindgren | bb13b5fd | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 381 | /* polled mcbsp i/o operations */ |
| 382 | int omap_mcbsp_pollwrite(unsigned int id, u16 buf) |
| 383 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 384 | struct omap_mcbsp *mcbsp; |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 385 | void __iomem *base; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 386 | |
| 387 | if (!omap_mcbsp_check_valid_id(id)) { |
| 388 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
| 389 | return -ENODEV; |
| 390 | } |
| 391 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 392 | mcbsp = id_to_mcbsp_ptr(id); |
| 393 | base = mcbsp->io_base; |
| 394 | |
Tony Lindgren | bb13b5fd | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 395 | writew(buf, base + OMAP_MCBSP_REG_DXR1); |
| 396 | /* if frame sync error - clear the error */ |
| 397 | if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) { |
| 398 | /* clear error */ |
| 399 | writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR), |
| 400 | base + OMAP_MCBSP_REG_SPCR2); |
| 401 | /* resend */ |
| 402 | return -1; |
| 403 | } else { |
| 404 | /* wait for transmit confirmation */ |
| 405 | int attemps = 0; |
| 406 | while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) { |
| 407 | if (attemps++ > 1000) { |
| 408 | writew(readw(base + OMAP_MCBSP_REG_SPCR2) & |
| 409 | (~XRST), |
| 410 | base + OMAP_MCBSP_REG_SPCR2); |
| 411 | udelay(10); |
| 412 | writew(readw(base + OMAP_MCBSP_REG_SPCR2) | |
| 413 | (XRST), |
| 414 | base + OMAP_MCBSP_REG_SPCR2); |
| 415 | udelay(10); |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 416 | dev_err(mcbsp->dev, "Could not write to" |
| 417 | " McBSP%d Register\n", mcbsp->id); |
Tony Lindgren | bb13b5fd | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 418 | return -2; |
| 419 | } |
| 420 | } |
| 421 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 422 | |
Tony Lindgren | bb13b5fd | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 423 | return 0; |
| 424 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 425 | EXPORT_SYMBOL(omap_mcbsp_pollwrite); |
Tony Lindgren | bb13b5fd | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 426 | |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 427 | int omap_mcbsp_pollread(unsigned int id, u16 *buf) |
Tony Lindgren | bb13b5fd | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 428 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 429 | struct omap_mcbsp *mcbsp; |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 430 | void __iomem *base; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 431 | |
| 432 | if (!omap_mcbsp_check_valid_id(id)) { |
| 433 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
| 434 | return -ENODEV; |
| 435 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 436 | mcbsp = id_to_mcbsp_ptr(id); |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 437 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 438 | base = mcbsp->io_base; |
Tony Lindgren | bb13b5fd | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 439 | /* if frame sync error - clear the error */ |
| 440 | if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) { |
| 441 | /* clear error */ |
| 442 | writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR), |
| 443 | base + OMAP_MCBSP_REG_SPCR1); |
| 444 | /* resend */ |
| 445 | return -1; |
| 446 | } else { |
| 447 | /* wait for recieve confirmation */ |
| 448 | int attemps = 0; |
| 449 | while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) { |
| 450 | if (attemps++ > 1000) { |
| 451 | writew(readw(base + OMAP_MCBSP_REG_SPCR1) & |
| 452 | (~RRST), |
| 453 | base + OMAP_MCBSP_REG_SPCR1); |
| 454 | udelay(10); |
| 455 | writew(readw(base + OMAP_MCBSP_REG_SPCR1) | |
| 456 | (RRST), |
| 457 | base + OMAP_MCBSP_REG_SPCR1); |
| 458 | udelay(10); |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 459 | dev_err(mcbsp->dev, "Could not read from" |
| 460 | " McBSP%d Register\n", mcbsp->id); |
Tony Lindgren | bb13b5fd | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 461 | return -2; |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | *buf = readw(base + OMAP_MCBSP_REG_DRR1); |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 466 | |
Tony Lindgren | bb13b5fd | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 467 | return 0; |
| 468 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 469 | EXPORT_SYMBOL(omap_mcbsp_pollread); |
Tony Lindgren | bb13b5fd | 2005-07-10 19:58:18 +0100 | [diff] [blame] | 470 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 471 | /* |
| 472 | * IRQ based word transmission. |
| 473 | */ |
| 474 | void omap_mcbsp_xmit_word(unsigned int id, u32 word) |
| 475 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 476 | struct omap_mcbsp *mcbsp; |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 477 | void __iomem *io_base; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 478 | omap_mcbsp_word_length word_length; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 479 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 480 | if (!omap_mcbsp_check_valid_id(id)) { |
| 481 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 482 | return; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 483 | } |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 484 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 485 | mcbsp = id_to_mcbsp_ptr(id); |
| 486 | io_base = mcbsp->io_base; |
| 487 | word_length = mcbsp->tx_word_length; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 488 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 489 | wait_for_completion(&mcbsp->tx_irq_completion); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 490 | |
| 491 | if (word_length > OMAP_MCBSP_WORD_16) |
| 492 | OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16); |
| 493 | OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff); |
| 494 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 495 | EXPORT_SYMBOL(omap_mcbsp_xmit_word); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 496 | |
| 497 | u32 omap_mcbsp_recv_word(unsigned int id) |
| 498 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 499 | struct omap_mcbsp *mcbsp; |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 500 | void __iomem *io_base; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 501 | u16 word_lsb, word_msb = 0; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 502 | omap_mcbsp_word_length word_length; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 503 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 504 | if (!omap_mcbsp_check_valid_id(id)) { |
| 505 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
| 506 | return -ENODEV; |
| 507 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 508 | mcbsp = id_to_mcbsp_ptr(id); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 509 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 510 | word_length = mcbsp->rx_word_length; |
| 511 | io_base = mcbsp->io_base; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 512 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 513 | wait_for_completion(&mcbsp->rx_irq_completion); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 514 | |
| 515 | if (word_length > OMAP_MCBSP_WORD_16) |
| 516 | word_msb = OMAP_MCBSP_READ(io_base, DRR2); |
| 517 | word_lsb = OMAP_MCBSP_READ(io_base, DRR1); |
| 518 | |
| 519 | return (word_lsb | (word_msb << 16)); |
| 520 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 521 | EXPORT_SYMBOL(omap_mcbsp_recv_word); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 522 | |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 523 | int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word) |
| 524 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 525 | struct omap_mcbsp *mcbsp; |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 526 | void __iomem *io_base; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 527 | omap_mcbsp_word_length tx_word_length; |
| 528 | omap_mcbsp_word_length rx_word_length; |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 529 | u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0; |
| 530 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 531 | if (!omap_mcbsp_check_valid_id(id)) { |
| 532 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
| 533 | return -ENODEV; |
| 534 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 535 | mcbsp = id_to_mcbsp_ptr(id); |
| 536 | io_base = mcbsp->io_base; |
| 537 | tx_word_length = mcbsp->tx_word_length; |
| 538 | rx_word_length = mcbsp->rx_word_length; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 539 | |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 540 | if (tx_word_length != rx_word_length) |
| 541 | return -EINVAL; |
| 542 | |
| 543 | /* First we wait for the transmitter to be ready */ |
| 544 | spcr2 = OMAP_MCBSP_READ(io_base, SPCR2); |
| 545 | while (!(spcr2 & XRDY)) { |
| 546 | spcr2 = OMAP_MCBSP_READ(io_base, SPCR2); |
| 547 | if (attempts++ > 1000) { |
| 548 | /* We must reset the transmitter */ |
| 549 | OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST)); |
| 550 | udelay(10); |
| 551 | OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST); |
| 552 | udelay(10); |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 553 | dev_err(mcbsp->dev, "McBSP%d transmitter not " |
| 554 | "ready\n", mcbsp->id); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 555 | return -EAGAIN; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* Now we can push the data */ |
| 560 | if (tx_word_length > OMAP_MCBSP_WORD_16) |
| 561 | OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16); |
| 562 | OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff); |
| 563 | |
| 564 | /* We wait for the receiver to be ready */ |
| 565 | spcr1 = OMAP_MCBSP_READ(io_base, SPCR1); |
| 566 | while (!(spcr1 & RRDY)) { |
| 567 | spcr1 = OMAP_MCBSP_READ(io_base, SPCR1); |
| 568 | if (attempts++ > 1000) { |
| 569 | /* We must reset the receiver */ |
| 570 | OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST)); |
| 571 | udelay(10); |
| 572 | OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST); |
| 573 | udelay(10); |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 574 | dev_err(mcbsp->dev, "McBSP%d receiver not " |
| 575 | "ready\n", mcbsp->id); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 576 | return -EAGAIN; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /* Receiver is ready, let's read the dummy data */ |
| 581 | if (rx_word_length > OMAP_MCBSP_WORD_16) |
| 582 | word_msb = OMAP_MCBSP_READ(io_base, DRR2); |
| 583 | word_lsb = OMAP_MCBSP_READ(io_base, DRR1); |
| 584 | |
| 585 | return 0; |
| 586 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 587 | EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 588 | |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 589 | int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word) |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 590 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 591 | struct omap_mcbsp *mcbsp; |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 592 | u32 clock_word = 0; |
| 593 | void __iomem *io_base; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 594 | omap_mcbsp_word_length tx_word_length; |
| 595 | omap_mcbsp_word_length rx_word_length; |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 596 | u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0; |
| 597 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 598 | if (!omap_mcbsp_check_valid_id(id)) { |
| 599 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
| 600 | return -ENODEV; |
| 601 | } |
| 602 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 603 | mcbsp = id_to_mcbsp_ptr(id); |
| 604 | io_base = mcbsp->io_base; |
| 605 | |
| 606 | tx_word_length = mcbsp->tx_word_length; |
| 607 | rx_word_length = mcbsp->rx_word_length; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 608 | |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 609 | if (tx_word_length != rx_word_length) |
| 610 | return -EINVAL; |
| 611 | |
| 612 | /* First we wait for the transmitter to be ready */ |
| 613 | spcr2 = OMAP_MCBSP_READ(io_base, SPCR2); |
| 614 | while (!(spcr2 & XRDY)) { |
| 615 | spcr2 = OMAP_MCBSP_READ(io_base, SPCR2); |
| 616 | if (attempts++ > 1000) { |
| 617 | /* We must reset the transmitter */ |
| 618 | OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST)); |
| 619 | udelay(10); |
| 620 | OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST); |
| 621 | udelay(10); |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 622 | dev_err(mcbsp->dev, "McBSP%d transmitter not " |
| 623 | "ready\n", mcbsp->id); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 624 | return -EAGAIN; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /* We first need to enable the bus clock */ |
| 629 | if (tx_word_length > OMAP_MCBSP_WORD_16) |
| 630 | OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16); |
| 631 | OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff); |
| 632 | |
| 633 | /* We wait for the receiver to be ready */ |
| 634 | spcr1 = OMAP_MCBSP_READ(io_base, SPCR1); |
| 635 | while (!(spcr1 & RRDY)) { |
| 636 | spcr1 = OMAP_MCBSP_READ(io_base, SPCR1); |
| 637 | if (attempts++ > 1000) { |
| 638 | /* We must reset the receiver */ |
| 639 | OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST)); |
| 640 | udelay(10); |
| 641 | OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST); |
| 642 | udelay(10); |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 643 | dev_err(mcbsp->dev, "McBSP%d receiver not " |
| 644 | "ready\n", mcbsp->id); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 645 | return -EAGAIN; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | /* Receiver is ready, there is something for us */ |
| 650 | if (rx_word_length > OMAP_MCBSP_WORD_16) |
| 651 | word_msb = OMAP_MCBSP_READ(io_base, DRR2); |
| 652 | word_lsb = OMAP_MCBSP_READ(io_base, DRR1); |
| 653 | |
| 654 | word[0] = (word_lsb | (word_msb << 16)); |
| 655 | |
| 656 | return 0; |
| 657 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 658 | EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll); |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 659 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 660 | /* |
| 661 | * Simple DMA based buffer rx/tx routines. |
| 662 | * Nothing fancy, just a single buffer tx/rx through DMA. |
| 663 | * The DMA resources are released once the transfer is done. |
| 664 | * For anything fancier, you should use your own customized DMA |
| 665 | * routines and callbacks. |
| 666 | */ |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 667 | int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer, |
| 668 | unsigned int length) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 669 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 670 | struct omap_mcbsp *mcbsp; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 671 | int dma_tx_ch; |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 672 | int src_port = 0; |
| 673 | int dest_port = 0; |
| 674 | int sync_dev = 0; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 675 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 676 | if (!omap_mcbsp_check_valid_id(id)) { |
| 677 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
| 678 | return -ENODEV; |
| 679 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 680 | mcbsp = id_to_mcbsp_ptr(id); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 681 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 682 | if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX", |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 683 | omap_mcbsp_tx_dma_callback, |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 684 | mcbsp, |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 685 | &dma_tx_ch)) { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 686 | dev_err(mcbsp->dev, " Unable to request DMA channel for " |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 687 | "McBSP%d TX. Trying IRQ based TX\n", |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 688 | mcbsp->id); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 689 | return -EAGAIN; |
| 690 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 691 | mcbsp->dma_tx_lch = dma_tx_ch; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 692 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 693 | dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id, |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 694 | dma_tx_ch); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 695 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 696 | init_completion(&mcbsp->tx_dma_completion); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 697 | |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 698 | if (cpu_class_is_omap1()) { |
| 699 | src_port = OMAP_DMA_PORT_TIPB; |
| 700 | dest_port = OMAP_DMA_PORT_EMIFF; |
| 701 | } |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 702 | if (cpu_class_is_omap2()) |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 703 | sync_dev = mcbsp->dma_tx_sync; |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 704 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 705 | omap_set_dma_transfer_params(mcbsp->dma_tx_lch, |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 706 | OMAP_DMA_DATA_TYPE_S16, |
| 707 | length >> 1, 1, |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 708 | OMAP_DMA_SYNC_ELEMENT, |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 709 | sync_dev, 0); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 710 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 711 | omap_set_dma_dest_params(mcbsp->dma_tx_lch, |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 712 | src_port, |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 713 | OMAP_DMA_AMODE_CONSTANT, |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 714 | mcbsp->phys_base + OMAP_MCBSP_REG_DXR1, |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 715 | 0, 0); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 716 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 717 | omap_set_dma_src_params(mcbsp->dma_tx_lch, |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 718 | dest_port, |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 719 | OMAP_DMA_AMODE_POST_INC, |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 720 | buffer, |
| 721 | 0, 0); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 722 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 723 | omap_start_dma(mcbsp->dma_tx_lch); |
| 724 | wait_for_completion(&mcbsp->tx_dma_completion); |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 725 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 726 | return 0; |
| 727 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 728 | EXPORT_SYMBOL(omap_mcbsp_xmit_buffer); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 729 | |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 730 | int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer, |
| 731 | unsigned int length) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 732 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 733 | struct omap_mcbsp *mcbsp; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 734 | int dma_rx_ch; |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 735 | int src_port = 0; |
| 736 | int dest_port = 0; |
| 737 | int sync_dev = 0; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 738 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 739 | if (!omap_mcbsp_check_valid_id(id)) { |
| 740 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
| 741 | return -ENODEV; |
| 742 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 743 | mcbsp = id_to_mcbsp_ptr(id); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 744 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 745 | if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX", |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 746 | omap_mcbsp_rx_dma_callback, |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 747 | mcbsp, |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 748 | &dma_rx_ch)) { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 749 | dev_err(mcbsp->dev, "Unable to request DMA channel for " |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 750 | "McBSP%d RX. Trying IRQ based RX\n", |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 751 | mcbsp->id); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 752 | return -EAGAIN; |
| 753 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 754 | mcbsp->dma_rx_lch = dma_rx_ch; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 755 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 756 | dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id, |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 757 | dma_rx_ch); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 758 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 759 | init_completion(&mcbsp->rx_dma_completion); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 760 | |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 761 | if (cpu_class_is_omap1()) { |
| 762 | src_port = OMAP_DMA_PORT_TIPB; |
| 763 | dest_port = OMAP_DMA_PORT_EMIFF; |
| 764 | } |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 765 | if (cpu_class_is_omap2()) |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 766 | sync_dev = mcbsp->dma_rx_sync; |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 767 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 768 | omap_set_dma_transfer_params(mcbsp->dma_rx_lch, |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 769 | OMAP_DMA_DATA_TYPE_S16, |
| 770 | length >> 1, 1, |
| 771 | OMAP_DMA_SYNC_ELEMENT, |
| 772 | sync_dev, 0); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 773 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 774 | omap_set_dma_src_params(mcbsp->dma_rx_lch, |
Tony Lindgren | 120db2c | 2006-04-02 17:46:27 +0100 | [diff] [blame] | 775 | src_port, |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 776 | OMAP_DMA_AMODE_CONSTANT, |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 777 | mcbsp->phys_base + OMAP_MCBSP_REG_DRR1, |
Tony Lindgren | 1a8bfa1 | 2005-11-10 14:26:50 +0000 | [diff] [blame] | 778 | 0, 0); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 779 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 780 | omap_set_dma_dest_params(mcbsp->dma_rx_lch, |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 781 | dest_port, |
| 782 | OMAP_DMA_AMODE_POST_INC, |
| 783 | buffer, |
| 784 | 0, 0); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 785 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 786 | omap_start_dma(mcbsp->dma_rx_lch); |
| 787 | wait_for_completion(&mcbsp->rx_dma_completion); |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 788 | |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 789 | return 0; |
| 790 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 791 | EXPORT_SYMBOL(omap_mcbsp_recv_buffer); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 792 | |
| 793 | /* |
| 794 | * SPI wrapper. |
| 795 | * Since SPI setup is much simpler than the generic McBSP one, |
| 796 | * this wrapper just need an omap_mcbsp_spi_cfg structure as an input. |
| 797 | * Once this is done, you can call omap_mcbsp_start(). |
| 798 | */ |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 799 | void omap_mcbsp_set_spi_mode(unsigned int id, |
| 800 | const struct omap_mcbsp_spi_cfg *spi_cfg) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 801 | { |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 802 | struct omap_mcbsp *mcbsp; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 803 | struct omap_mcbsp_reg_cfg mcbsp_cfg; |
| 804 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 805 | if (!omap_mcbsp_check_valid_id(id)) { |
| 806 | printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 807 | return; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 808 | } |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 809 | mcbsp = id_to_mcbsp_ptr(id); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 810 | |
| 811 | memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg)); |
| 812 | |
| 813 | /* SPI has only one frame */ |
| 814 | mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0)); |
| 815 | mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0)); |
| 816 | |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 817 | /* Clock stop mode */ |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 818 | if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY) |
| 819 | mcbsp_cfg.spcr1 |= (1 << 12); |
| 820 | else |
| 821 | mcbsp_cfg.spcr1 |= (3 << 11); |
| 822 | |
| 823 | /* Set clock parities */ |
| 824 | if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING) |
| 825 | mcbsp_cfg.pcr0 |= CLKRP; |
| 826 | else |
| 827 | mcbsp_cfg.pcr0 &= ~CLKRP; |
| 828 | |
| 829 | if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING) |
| 830 | mcbsp_cfg.pcr0 &= ~CLKXP; |
| 831 | else |
| 832 | mcbsp_cfg.pcr0 |= CLKXP; |
| 833 | |
| 834 | /* Set SCLKME to 0 and CLKSM to 1 */ |
| 835 | mcbsp_cfg.pcr0 &= ~SCLKME; |
| 836 | mcbsp_cfg.srgr2 |= CLKSM; |
| 837 | |
| 838 | /* Set FSXP */ |
| 839 | if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH) |
| 840 | mcbsp_cfg.pcr0 &= ~FSXP; |
| 841 | else |
| 842 | mcbsp_cfg.pcr0 |= FSXP; |
| 843 | |
| 844 | if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) { |
| 845 | mcbsp_cfg.pcr0 |= CLKXM; |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 846 | mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 847 | mcbsp_cfg.pcr0 |= FSXM; |
| 848 | mcbsp_cfg.srgr2 &= ~FSGM; |
| 849 | mcbsp_cfg.xcr2 |= XDATDLY(1); |
| 850 | mcbsp_cfg.rcr2 |= RDATDLY(1); |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 851 | } else { |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 852 | mcbsp_cfg.pcr0 &= ~CLKXM; |
| 853 | mcbsp_cfg.srgr1 |= CLKGDV(1); |
| 854 | mcbsp_cfg.pcr0 &= ~FSXM; |
| 855 | mcbsp_cfg.xcr2 &= ~XDATDLY(3); |
| 856 | mcbsp_cfg.rcr2 &= ~RDATDLY(3); |
| 857 | } |
| 858 | |
| 859 | mcbsp_cfg.xcr2 &= ~XPHASE; |
| 860 | mcbsp_cfg.rcr2 &= ~RPHASE; |
| 861 | |
| 862 | omap_mcbsp_config(id, &mcbsp_cfg); |
| 863 | } |
Eduardo Valentin | fb78d80 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 864 | EXPORT_SYMBOL(omap_mcbsp_set_spi_mode); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 865 | |
| 866 | /* |
| 867 | * McBSP1 and McBSP3 are directly mapped on 1610 and 1510. |
| 868 | * 730 has only 2 McBSP, and both of them are MPU peripherals. |
| 869 | */ |
Uwe Kleine-König | 25cef22 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 870 | static int __devinit omap_mcbsp_probe(struct platform_device *pdev) |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 871 | { |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 872 | struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data; |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 873 | struct omap_mcbsp *mcbsp; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 874 | int id = pdev->id - 1; |
| 875 | int ret = 0; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 876 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 877 | if (!pdata) { |
| 878 | dev_err(&pdev->dev, "McBSP device initialized without" |
| 879 | "platform data\n"); |
| 880 | ret = -EINVAL; |
| 881 | goto exit; |
| 882 | } |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 883 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 884 | dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id); |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 885 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 886 | if (id >= omap_mcbsp_count) { |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 887 | dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id); |
| 888 | ret = -EINVAL; |
| 889 | goto exit; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 890 | } |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 891 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 892 | mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL); |
| 893 | if (!mcbsp) { |
| 894 | ret = -ENOMEM; |
| 895 | goto exit; |
| 896 | } |
| 897 | mcbsp_ptr[id] = mcbsp; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 898 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 899 | spin_lock_init(&mcbsp->lock); |
| 900 | mcbsp->id = id + 1; |
| 901 | mcbsp->free = 1; |
| 902 | mcbsp->dma_tx_lch = -1; |
| 903 | mcbsp->dma_rx_lch = -1; |
| 904 | |
| 905 | mcbsp->phys_base = pdata->phys_base; |
| 906 | mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K); |
| 907 | if (!mcbsp->io_base) { |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 908 | ret = -ENOMEM; |
| 909 | goto err_ioremap; |
| 910 | } |
| 911 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 912 | /* Default I/O is IRQ based */ |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 913 | mcbsp->io_type = OMAP_MCBSP_IRQ_IO; |
| 914 | mcbsp->tx_irq = pdata->tx_irq; |
| 915 | mcbsp->rx_irq = pdata->rx_irq; |
| 916 | mcbsp->dma_rx_sync = pdata->dma_rx_sync; |
| 917 | mcbsp->dma_tx_sync = pdata->dma_tx_sync; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 918 | |
| 919 | if (pdata->clk_name) |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 920 | mcbsp->clk = clk_get(&pdev->dev, pdata->clk_name); |
| 921 | if (IS_ERR(mcbsp->clk)) { |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 922 | dev_err(&pdev->dev, |
| 923 | "Invalid clock configuration for McBSP%d.\n", |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 924 | mcbsp->id); |
| 925 | ret = PTR_ERR(mcbsp->clk); |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 926 | goto err_clk; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 927 | } |
| 928 | |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 929 | mcbsp->pdata = pdata; |
| 930 | mcbsp->dev = &pdev->dev; |
| 931 | platform_set_drvdata(pdev, mcbsp); |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 932 | return 0; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 933 | |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 934 | err_clk: |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 935 | iounmap(mcbsp->io_base); |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 936 | err_ioremap: |
Chandra Shekhar | b4b58f5 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 937 | mcbsp->free = 0; |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 938 | exit: |
| 939 | return ret; |
| 940 | } |
| 941 | |
Uwe Kleine-König | 25cef22 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 942 | static int __devexit omap_mcbsp_remove(struct platform_device *pdev) |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 943 | { |
| 944 | struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev); |
| 945 | |
| 946 | platform_set_drvdata(pdev, NULL); |
| 947 | if (mcbsp) { |
| 948 | |
| 949 | if (mcbsp->pdata && mcbsp->pdata->ops && |
| 950 | mcbsp->pdata->ops->free) |
| 951 | mcbsp->pdata->ops->free(mcbsp->id); |
| 952 | |
| 953 | clk_disable(mcbsp->clk); |
| 954 | clk_put(mcbsp->clk); |
| 955 | |
Russell King | d592dd1 | 2008-09-04 14:25:42 +0100 | [diff] [blame] | 956 | iounmap(mcbsp->io_base); |
| 957 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 958 | mcbsp->clk = NULL; |
| 959 | mcbsp->free = 0; |
| 960 | mcbsp->dev = NULL; |
Tony Lindgren | 5e1c5ff | 2005-07-10 19:58:15 +0100 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 966 | static struct platform_driver omap_mcbsp_driver = { |
| 967 | .probe = omap_mcbsp_probe, |
Uwe Kleine-König | 25cef22 | 2008-10-08 10:01:39 +0300 | [diff] [blame] | 968 | .remove = __devexit_p(omap_mcbsp_remove), |
Eduardo Valentin | bc5d0c8 | 2008-07-03 12:24:39 +0300 | [diff] [blame] | 969 | .driver = { |
| 970 | .name = "omap-mcbsp", |
| 971 | }, |
| 972 | }; |
| 973 | |
| 974 | int __init omap_mcbsp_init(void) |
| 975 | { |
| 976 | /* Register the McBSP driver */ |
| 977 | return platform_driver_register(&omap_mcbsp_driver); |
| 978 | } |
| 979 | |