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