Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 1 | /* |
| 2 | * omap-mcpdm.c -- OMAP ALSA SoC DAI driver using McPDM port |
| 3 | * |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 4 | * Copyright (C) 2009 - 2011 Texas Instruments |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 5 | * |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 6 | * Author: Misael Lopez Cruz <misael.lopez@ti.com> |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 7 | * Contact: Jorge Eduardo Candelaria <x0107209@ti.com> |
| 8 | * Margarita Olaya <magi.olaya@ti.com> |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 9 | * Peter Ujfalusi <peter.ujfalusi@ti.com> |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * version 2 as published by the Free Software Foundation. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, but |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 23 | * 02110-1301 USA |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/module.h> |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/err.h> |
| 32 | #include <linux/io.h> |
| 33 | #include <linux/irq.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/pm_runtime.h> |
Peter Ujfalusi | 7cb8a1b | 2011-11-15 11:32:14 +0200 | [diff] [blame] | 36 | #include <linux/of_device.h> |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 37 | |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 38 | #include <sound/core.h> |
| 39 | #include <sound/pcm.h> |
| 40 | #include <sound/pcm_params.h> |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 41 | #include <sound/soc.h> |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 42 | #include <sound/dmaengine_pcm.h> |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 43 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 44 | #include "omap-mcpdm.h" |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 45 | |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 46 | struct mcpdm_link_config { |
| 47 | u32 link_mask; /* channel mask for the direction */ |
| 48 | u32 threshold; /* FIFO threshold */ |
| 49 | }; |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 50 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 51 | struct omap_mcpdm { |
| 52 | struct device *dev; |
| 53 | unsigned long phys_base; |
| 54 | void __iomem *io_base; |
| 55 | int irq; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 56 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 57 | struct mutex mutex; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 58 | |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 59 | /* Playback/Capture configuration */ |
| 60 | struct mcpdm_link_config config[2]; |
Peter Ujfalusi | 89b0d55 | 2011-09-26 16:05:58 +0300 | [diff] [blame] | 61 | |
| 62 | /* McPDM dn offsets for rx1, and 2 channels */ |
| 63 | u32 dn_rx_offset; |
Peter Ujfalusi | 81054b2 | 2013-03-20 10:47:13 +0100 | [diff] [blame] | 64 | |
| 65 | /* McPDM needs to be restarted due to runtime reconfiguration */ |
| 66 | bool restart; |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 67 | |
| 68 | struct snd_dmaengine_dai_dma_data dma_data[2]; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /* |
| 72 | * Stream DMA parameters |
| 73 | */ |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 74 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 75 | static inline void omap_mcpdm_write(struct omap_mcpdm *mcpdm, u16 reg, u32 val) |
| 76 | { |
Victor Kamensky | 1b488a4 | 2013-11-16 02:01:19 +0200 | [diff] [blame] | 77 | writel_relaxed(val, mcpdm->io_base + reg); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static inline int omap_mcpdm_read(struct omap_mcpdm *mcpdm, u16 reg) |
| 81 | { |
Victor Kamensky | 1b488a4 | 2013-11-16 02:01:19 +0200 | [diff] [blame] | 82 | return readl_relaxed(mcpdm->io_base + reg); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | #ifdef DEBUG |
| 86 | static void omap_mcpdm_reg_dump(struct omap_mcpdm *mcpdm) |
| 87 | { |
| 88 | dev_dbg(mcpdm->dev, "***********************\n"); |
| 89 | dev_dbg(mcpdm->dev, "IRQSTATUS_RAW: 0x%04x\n", |
| 90 | omap_mcpdm_read(mcpdm, MCPDM_REG_IRQSTATUS_RAW)); |
| 91 | dev_dbg(mcpdm->dev, "IRQSTATUS: 0x%04x\n", |
| 92 | omap_mcpdm_read(mcpdm, MCPDM_REG_IRQSTATUS)); |
| 93 | dev_dbg(mcpdm->dev, "IRQENABLE_SET: 0x%04x\n", |
| 94 | omap_mcpdm_read(mcpdm, MCPDM_REG_IRQENABLE_SET)); |
| 95 | dev_dbg(mcpdm->dev, "IRQENABLE_CLR: 0x%04x\n", |
| 96 | omap_mcpdm_read(mcpdm, MCPDM_REG_IRQENABLE_CLR)); |
| 97 | dev_dbg(mcpdm->dev, "IRQWAKE_EN: 0x%04x\n", |
| 98 | omap_mcpdm_read(mcpdm, MCPDM_REG_IRQWAKE_EN)); |
| 99 | dev_dbg(mcpdm->dev, "DMAENABLE_SET: 0x%04x\n", |
| 100 | omap_mcpdm_read(mcpdm, MCPDM_REG_DMAENABLE_SET)); |
| 101 | dev_dbg(mcpdm->dev, "DMAENABLE_CLR: 0x%04x\n", |
| 102 | omap_mcpdm_read(mcpdm, MCPDM_REG_DMAENABLE_CLR)); |
| 103 | dev_dbg(mcpdm->dev, "DMAWAKEEN: 0x%04x\n", |
| 104 | omap_mcpdm_read(mcpdm, MCPDM_REG_DMAWAKEEN)); |
| 105 | dev_dbg(mcpdm->dev, "CTRL: 0x%04x\n", |
| 106 | omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL)); |
| 107 | dev_dbg(mcpdm->dev, "DN_DATA: 0x%04x\n", |
| 108 | omap_mcpdm_read(mcpdm, MCPDM_REG_DN_DATA)); |
| 109 | dev_dbg(mcpdm->dev, "UP_DATA: 0x%04x\n", |
| 110 | omap_mcpdm_read(mcpdm, MCPDM_REG_UP_DATA)); |
| 111 | dev_dbg(mcpdm->dev, "FIFO_CTRL_DN: 0x%04x\n", |
| 112 | omap_mcpdm_read(mcpdm, MCPDM_REG_FIFO_CTRL_DN)); |
| 113 | dev_dbg(mcpdm->dev, "FIFO_CTRL_UP: 0x%04x\n", |
| 114 | omap_mcpdm_read(mcpdm, MCPDM_REG_FIFO_CTRL_UP)); |
| 115 | dev_dbg(mcpdm->dev, "***********************\n"); |
| 116 | } |
| 117 | #else |
| 118 | static void omap_mcpdm_reg_dump(struct omap_mcpdm *mcpdm) {} |
| 119 | #endif |
| 120 | |
| 121 | /* |
| 122 | * Enables the transfer through the PDM interface to/from the Phoenix |
| 123 | * codec by enabling the corresponding UP or DN channels. |
| 124 | */ |
| 125 | static void omap_mcpdm_start(struct omap_mcpdm *mcpdm) |
| 126 | { |
| 127 | u32 ctrl = omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL); |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 128 | u32 link_mask = mcpdm->config[0].link_mask | mcpdm->config[1].link_mask; |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 129 | |
| 130 | ctrl |= (MCPDM_SW_DN_RST | MCPDM_SW_UP_RST); |
| 131 | omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl); |
| 132 | |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 133 | ctrl |= link_mask; |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 134 | omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl); |
| 135 | |
| 136 | ctrl &= ~(MCPDM_SW_DN_RST | MCPDM_SW_UP_RST); |
| 137 | omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Disables the transfer through the PDM interface to/from the Phoenix |
| 142 | * codec by disabling the corresponding UP or DN channels. |
| 143 | */ |
| 144 | static void omap_mcpdm_stop(struct omap_mcpdm *mcpdm) |
| 145 | { |
| 146 | u32 ctrl = omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL); |
Peter Ujfalusi | 81054b2 | 2013-03-20 10:47:13 +0100 | [diff] [blame] | 147 | u32 link_mask = MCPDM_PDM_DN_MASK | MCPDM_PDM_UP_MASK; |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 148 | |
| 149 | ctrl |= (MCPDM_SW_DN_RST | MCPDM_SW_UP_RST); |
| 150 | omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl); |
| 151 | |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 152 | ctrl &= ~(link_mask); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 153 | omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl); |
| 154 | |
| 155 | ctrl &= ~(MCPDM_SW_DN_RST | MCPDM_SW_UP_RST); |
| 156 | omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl); |
| 157 | |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Is the physical McPDM interface active. |
| 162 | */ |
| 163 | static inline int omap_mcpdm_active(struct omap_mcpdm *mcpdm) |
| 164 | { |
| 165 | return omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL) & |
| 166 | (MCPDM_PDM_DN_MASK | MCPDM_PDM_UP_MASK); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Configures McPDM uplink, and downlink for audio. |
| 171 | * This function should be called before omap_mcpdm_start. |
| 172 | */ |
| 173 | static void omap_mcpdm_open_streams(struct omap_mcpdm *mcpdm) |
| 174 | { |
| 175 | omap_mcpdm_write(mcpdm, MCPDM_REG_IRQENABLE_SET, |
| 176 | MCPDM_DN_IRQ_EMPTY | MCPDM_DN_IRQ_FULL | |
| 177 | MCPDM_UP_IRQ_EMPTY | MCPDM_UP_IRQ_FULL); |
| 178 | |
Peter Ujfalusi | 89b0d55 | 2011-09-26 16:05:58 +0300 | [diff] [blame] | 179 | /* Enable DN RX1/2 offset cancellation feature, if configured */ |
| 180 | if (mcpdm->dn_rx_offset) { |
| 181 | u32 dn_offset = mcpdm->dn_rx_offset; |
| 182 | |
| 183 | omap_mcpdm_write(mcpdm, MCPDM_REG_DN_OFFSET, dn_offset); |
| 184 | dn_offset |= (MCPDM_DN_OFST_RX1_EN | MCPDM_DN_OFST_RX2_EN); |
| 185 | omap_mcpdm_write(mcpdm, MCPDM_REG_DN_OFFSET, dn_offset); |
| 186 | } |
| 187 | |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 188 | omap_mcpdm_write(mcpdm, MCPDM_REG_FIFO_CTRL_DN, |
| 189 | mcpdm->config[SNDRV_PCM_STREAM_PLAYBACK].threshold); |
| 190 | omap_mcpdm_write(mcpdm, MCPDM_REG_FIFO_CTRL_UP, |
| 191 | mcpdm->config[SNDRV_PCM_STREAM_CAPTURE].threshold); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 192 | |
| 193 | omap_mcpdm_write(mcpdm, MCPDM_REG_DMAENABLE_SET, |
| 194 | MCPDM_DMA_DN_ENABLE | MCPDM_DMA_UP_ENABLE); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Cleans McPDM uplink, and downlink configuration. |
| 199 | * This function should be called when the stream is closed. |
| 200 | */ |
| 201 | static void omap_mcpdm_close_streams(struct omap_mcpdm *mcpdm) |
| 202 | { |
| 203 | /* Disable irq request generation for downlink */ |
| 204 | omap_mcpdm_write(mcpdm, MCPDM_REG_IRQENABLE_CLR, |
| 205 | MCPDM_DN_IRQ_EMPTY | MCPDM_DN_IRQ_FULL); |
| 206 | |
| 207 | /* Disable DMA request generation for downlink */ |
| 208 | omap_mcpdm_write(mcpdm, MCPDM_REG_DMAENABLE_CLR, MCPDM_DMA_DN_ENABLE); |
| 209 | |
| 210 | /* Disable irq request generation for uplink */ |
| 211 | omap_mcpdm_write(mcpdm, MCPDM_REG_IRQENABLE_CLR, |
| 212 | MCPDM_UP_IRQ_EMPTY | MCPDM_UP_IRQ_FULL); |
| 213 | |
| 214 | /* Disable DMA request generation for uplink */ |
| 215 | omap_mcpdm_write(mcpdm, MCPDM_REG_DMAENABLE_CLR, MCPDM_DMA_UP_ENABLE); |
Peter Ujfalusi | 89b0d55 | 2011-09-26 16:05:58 +0300 | [diff] [blame] | 216 | |
| 217 | /* Disable RX1/2 offset cancellation */ |
| 218 | if (mcpdm->dn_rx_offset) |
| 219 | omap_mcpdm_write(mcpdm, MCPDM_REG_DN_OFFSET, 0); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static irqreturn_t omap_mcpdm_irq_handler(int irq, void *dev_id) |
| 223 | { |
| 224 | struct omap_mcpdm *mcpdm = dev_id; |
| 225 | int irq_status; |
| 226 | |
| 227 | irq_status = omap_mcpdm_read(mcpdm, MCPDM_REG_IRQSTATUS); |
| 228 | |
| 229 | /* Acknowledge irq event */ |
| 230 | omap_mcpdm_write(mcpdm, MCPDM_REG_IRQSTATUS, irq_status); |
| 231 | |
| 232 | if (irq_status & MCPDM_DN_IRQ_FULL) |
| 233 | dev_dbg(mcpdm->dev, "DN (playback) FIFO Full\n"); |
| 234 | |
| 235 | if (irq_status & MCPDM_DN_IRQ_EMPTY) |
| 236 | dev_dbg(mcpdm->dev, "DN (playback) FIFO Empty\n"); |
| 237 | |
| 238 | if (irq_status & MCPDM_DN_IRQ) |
| 239 | dev_dbg(mcpdm->dev, "DN (playback) write request\n"); |
| 240 | |
| 241 | if (irq_status & MCPDM_UP_IRQ_FULL) |
| 242 | dev_dbg(mcpdm->dev, "UP (capture) FIFO Full\n"); |
| 243 | |
| 244 | if (irq_status & MCPDM_UP_IRQ_EMPTY) |
| 245 | dev_dbg(mcpdm->dev, "UP (capture) FIFO Empty\n"); |
| 246 | |
| 247 | if (irq_status & MCPDM_UP_IRQ) |
| 248 | dev_dbg(mcpdm->dev, "UP (capture) write request\n"); |
| 249 | |
| 250 | return IRQ_HANDLED; |
| 251 | } |
| 252 | |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 253 | static int omap_mcpdm_dai_startup(struct snd_pcm_substream *substream, |
| 254 | struct snd_soc_dai *dai) |
| 255 | { |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 256 | struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai); |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 257 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 258 | mutex_lock(&mcpdm->mutex); |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 259 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 260 | if (!dai->active) { |
Peter Ujfalusi | 68214d9 | 2012-10-04 11:27:16 +0300 | [diff] [blame] | 261 | u32 ctrl = omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 262 | |
Peter Ujfalusi | 68214d9 | 2012-10-04 11:27:16 +0300 | [diff] [blame] | 263 | omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl | MCPDM_WD_EN); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 264 | omap_mcpdm_open_streams(mcpdm); |
| 265 | } |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 266 | mutex_unlock(&mcpdm->mutex); |
| 267 | |
| 268 | return 0; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static void omap_mcpdm_dai_shutdown(struct snd_pcm_substream *substream, |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 272 | struct snd_soc_dai *dai) |
| 273 | { |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 274 | struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai); |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 275 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 276 | mutex_lock(&mcpdm->mutex); |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 277 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 278 | if (!dai->active) { |
| 279 | if (omap_mcpdm_active(mcpdm)) { |
| 280 | omap_mcpdm_stop(mcpdm); |
| 281 | omap_mcpdm_close_streams(mcpdm); |
Peter Ujfalusi | 81054b2 | 2013-03-20 10:47:13 +0100 | [diff] [blame] | 282 | mcpdm->config[0].link_mask = 0; |
| 283 | mcpdm->config[1].link_mask = 0; |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 284 | } |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 285 | } |
| 286 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 287 | mutex_unlock(&mcpdm->mutex); |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static int omap_mcpdm_dai_hw_params(struct snd_pcm_substream *substream, |
| 291 | struct snd_pcm_hw_params *params, |
| 292 | struct snd_soc_dai *dai) |
| 293 | { |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 294 | struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai); |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 295 | int stream = substream->stream; |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 296 | struct snd_dmaengine_dai_dma_data *dma_data; |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 297 | u32 threshold; |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 298 | int channels; |
| 299 | int link_mask = 0; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 300 | |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 301 | channels = params_channels(params); |
| 302 | switch (channels) { |
Peter Ujfalusi | 3b5b516f | 2011-09-23 09:49:43 +0300 | [diff] [blame] | 303 | case 5: |
| 304 | if (stream == SNDRV_PCM_STREAM_CAPTURE) |
| 305 | /* up to 3 channels for capture */ |
| 306 | return -EINVAL; |
| 307 | link_mask |= 1 << 4; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 308 | case 4: |
| 309 | if (stream == SNDRV_PCM_STREAM_CAPTURE) |
Peter Ujfalusi | 3b5b516f | 2011-09-23 09:49:43 +0300 | [diff] [blame] | 310 | /* up to 3 channels for capture */ |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 311 | return -EINVAL; |
| 312 | link_mask |= 1 << 3; |
| 313 | case 3: |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 314 | link_mask |= 1 << 2; |
| 315 | case 2: |
| 316 | link_mask |= 1 << 1; |
| 317 | case 1: |
| 318 | link_mask |= 1 << 0; |
| 319 | break; |
| 320 | default: |
| 321 | /* unsupported number of channels */ |
| 322 | return -EINVAL; |
| 323 | } |
| 324 | |
Peter Ujfalusi | bcd6da7 | 2012-09-14 15:05:57 +0300 | [diff] [blame] | 325 | dma_data = snd_soc_dai_get_dma_data(dai, substream); |
Peter Ujfalusi | b199adf | 2011-08-02 13:35:30 +0300 | [diff] [blame] | 326 | |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 327 | threshold = mcpdm->config[stream].threshold; |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 328 | /* Configure McPDM channels, and DMA packet size */ |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 329 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) { |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 330 | link_mask <<= 3; |
Peter Ujfalusi | 81054b2 | 2013-03-20 10:47:13 +0100 | [diff] [blame] | 331 | |
| 332 | /* If capture is not running assume a stereo stream to come */ |
| 333 | if (!mcpdm->config[!stream].link_mask) |
| 334 | mcpdm->config[!stream].link_mask = 0x3; |
| 335 | |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 336 | dma_data->maxburst = |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 337 | (MCPDM_DN_THRES_MAX - threshold) * channels; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 338 | } else { |
Peter Ujfalusi | 81054b2 | 2013-03-20 10:47:13 +0100 | [diff] [blame] | 339 | /* If playback is not running assume a stereo stream to come */ |
| 340 | if (!mcpdm->config[!stream].link_mask) |
| 341 | mcpdm->config[!stream].link_mask = (0x3 << 3); |
| 342 | |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 343 | dma_data->maxburst = threshold * channels; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 344 | } |
| 345 | |
Peter Ujfalusi | 81054b2 | 2013-03-20 10:47:13 +0100 | [diff] [blame] | 346 | /* Check if we need to restart McPDM with this stream */ |
| 347 | if (mcpdm->config[stream].link_mask && |
| 348 | mcpdm->config[stream].link_mask != link_mask) |
| 349 | mcpdm->restart = true; |
| 350 | |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 351 | mcpdm->config[stream].link_mask = link_mask; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 352 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 353 | return 0; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 354 | } |
| 355 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 356 | static int omap_mcpdm_prepare(struct snd_pcm_substream *substream, |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 357 | struct snd_soc_dai *dai) |
| 358 | { |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 359 | struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai); |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 360 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 361 | if (!omap_mcpdm_active(mcpdm)) { |
| 362 | omap_mcpdm_start(mcpdm); |
| 363 | omap_mcpdm_reg_dump(mcpdm); |
Peter Ujfalusi | 81054b2 | 2013-03-20 10:47:13 +0100 | [diff] [blame] | 364 | } else if (mcpdm->restart) { |
| 365 | omap_mcpdm_stop(mcpdm); |
| 366 | omap_mcpdm_start(mcpdm); |
| 367 | mcpdm->restart = false; |
| 368 | omap_mcpdm_reg_dump(mcpdm); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 369 | } |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 370 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 371 | return 0; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 372 | } |
| 373 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 374 | static const struct snd_soc_dai_ops omap_mcpdm_dai_ops = { |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 375 | .startup = omap_mcpdm_dai_startup, |
| 376 | .shutdown = omap_mcpdm_dai_shutdown, |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 377 | .hw_params = omap_mcpdm_dai_hw_params, |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 378 | .prepare = omap_mcpdm_prepare, |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 379 | }; |
| 380 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 381 | static int omap_mcpdm_probe(struct snd_soc_dai *dai) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 382 | { |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 383 | struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai); |
| 384 | int ret; |
| 385 | |
| 386 | pm_runtime_enable(mcpdm->dev); |
| 387 | |
| 388 | /* Disable lines while request is ongoing */ |
| 389 | pm_runtime_get_sync(mcpdm->dev); |
| 390 | omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, 0x00); |
| 391 | |
Sebastien Guiriec | ddd1753 | 2013-02-13 08:21:54 +0100 | [diff] [blame] | 392 | ret = devm_request_irq(mcpdm->dev, mcpdm->irq, omap_mcpdm_irq_handler, |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 393 | 0, "McPDM", (void *)mcpdm); |
| 394 | |
| 395 | pm_runtime_put_sync(mcpdm->dev); |
| 396 | |
| 397 | if (ret) { |
| 398 | dev_err(mcpdm->dev, "Request for IRQ failed\n"); |
| 399 | pm_runtime_disable(mcpdm->dev); |
| 400 | } |
| 401 | |
| 402 | /* Configure McPDM threshold values */ |
Peter Ujfalusi | 6237663 | 2013-03-20 10:47:12 +0100 | [diff] [blame] | 403 | mcpdm->config[SNDRV_PCM_STREAM_PLAYBACK].threshold = 2; |
| 404 | mcpdm->config[SNDRV_PCM_STREAM_CAPTURE].threshold = |
| 405 | MCPDM_UP_THRES_MAX - 3; |
Peter Ujfalusi | f6563b3 | 2014-04-16 15:46:13 +0300 | [diff] [blame^] | 406 | |
| 407 | snd_soc_dai_init_dma_data(dai, |
| 408 | &mcpdm->dma_data[SNDRV_PCM_STREAM_PLAYBACK], |
| 409 | &mcpdm->dma_data[SNDRV_PCM_STREAM_CAPTURE]); |
| 410 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | static int omap_mcpdm_remove(struct snd_soc_dai *dai) |
| 415 | { |
| 416 | struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai); |
| 417 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 418 | pm_runtime_disable(mcpdm->dev); |
| 419 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 423 | #define OMAP_MCPDM_RATES (SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000) |
| 424 | #define OMAP_MCPDM_FORMATS SNDRV_PCM_FMTBIT_S32_LE |
| 425 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 426 | static struct snd_soc_dai_driver omap_mcpdm_dai = { |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 427 | .probe = omap_mcpdm_probe, |
| 428 | .remove = omap_mcpdm_remove, |
| 429 | .probe_order = SND_SOC_COMP_ORDER_LATE, |
| 430 | .remove_order = SND_SOC_COMP_ORDER_EARLY, |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 431 | .playback = { |
| 432 | .channels_min = 1, |
Peter Ujfalusi | 3b5b516f | 2011-09-23 09:49:43 +0300 | [diff] [blame] | 433 | .channels_max = 5, |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 434 | .rates = OMAP_MCPDM_RATES, |
| 435 | .formats = OMAP_MCPDM_FORMATS, |
Peter Ujfalusi | b4badd4 | 2012-01-18 12:18:24 +0100 | [diff] [blame] | 436 | .sig_bits = 24, |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 437 | }, |
| 438 | .capture = { |
| 439 | .channels_min = 1, |
Peter Ujfalusi | 3b5b516f | 2011-09-23 09:49:43 +0300 | [diff] [blame] | 440 | .channels_max = 3, |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 441 | .rates = OMAP_MCPDM_RATES, |
| 442 | .formats = OMAP_MCPDM_FORMATS, |
Peter Ujfalusi | b4badd4 | 2012-01-18 12:18:24 +0100 | [diff] [blame] | 443 | .sig_bits = 24, |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 444 | }, |
| 445 | .ops = &omap_mcpdm_dai_ops, |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 446 | }; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 447 | |
Kuninori Morimoto | 58709a3 | 2013-03-21 03:33:37 -0700 | [diff] [blame] | 448 | static const struct snd_soc_component_driver omap_mcpdm_component = { |
| 449 | .name = "omap-mcpdm", |
| 450 | }; |
| 451 | |
Peter Ujfalusi | 89b0d55 | 2011-09-26 16:05:58 +0300 | [diff] [blame] | 452 | void omap_mcpdm_configure_dn_offsets(struct snd_soc_pcm_runtime *rtd, |
| 453 | u8 rx1, u8 rx2) |
| 454 | { |
| 455 | struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(rtd->cpu_dai); |
| 456 | |
| 457 | mcpdm->dn_rx_offset = MCPDM_DNOFST_RX1(rx1) | MCPDM_DNOFST_RX2(rx2); |
| 458 | } |
| 459 | EXPORT_SYMBOL_GPL(omap_mcpdm_configure_dn_offsets); |
| 460 | |
Bill Pemberton | 7ff6000 | 2012-12-07 09:26:29 -0500 | [diff] [blame] | 461 | static int asoc_mcpdm_probe(struct platform_device *pdev) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 462 | { |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 463 | struct omap_mcpdm *mcpdm; |
| 464 | struct resource *res; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 465 | |
Peter Ujfalusi | d77ae33 | 2012-07-23 12:39:51 +0300 | [diff] [blame] | 466 | mcpdm = devm_kzalloc(&pdev->dev, sizeof(struct omap_mcpdm), GFP_KERNEL); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 467 | if (!mcpdm) |
| 468 | return -ENOMEM; |
| 469 | |
| 470 | platform_set_drvdata(pdev, mcpdm); |
| 471 | |
| 472 | mutex_init(&mcpdm->mutex); |
| 473 | |
Peter Ujfalusi | 5a40c57 | 2012-09-14 15:05:54 +0300 | [diff] [blame] | 474 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dma"); |
| 475 | if (res == NULL) |
| 476 | return -ENOMEM; |
| 477 | |
Lars-Peter Clausen | 09ae3aa | 2013-04-03 11:06:05 +0200 | [diff] [blame] | 478 | mcpdm->dma_data[0].addr = res->start + MCPDM_REG_DN_DATA; |
| 479 | mcpdm->dma_data[1].addr = res->start + MCPDM_REG_UP_DATA; |
Peter Ujfalusi | 5a40c57 | 2012-09-14 15:05:54 +0300 | [diff] [blame] | 480 | |
Peter Ujfalusi | a8035f0 | 2013-07-11 14:35:44 +0200 | [diff] [blame] | 481 | mcpdm->dma_data[0].filter_data = "dn_link"; |
| 482 | mcpdm->dma_data[1].filter_data = "up_link"; |
Peter Ujfalusi | 5a40c57 | 2012-09-14 15:05:54 +0300 | [diff] [blame] | 483 | |
| 484 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mpu"); |
Silviu-Mihai Popescu | 77c641d | 2013-03-11 17:58:57 +0200 | [diff] [blame] | 485 | mcpdm->io_base = devm_ioremap_resource(&pdev->dev, res); |
| 486 | if (IS_ERR(mcpdm->io_base)) |
| 487 | return PTR_ERR(mcpdm->io_base); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 488 | |
| 489 | mcpdm->irq = platform_get_irq(pdev, 0); |
Peter Ujfalusi | d77ae33 | 2012-07-23 12:39:51 +0300 | [diff] [blame] | 490 | if (mcpdm->irq < 0) |
| 491 | return mcpdm->irq; |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 492 | |
| 493 | mcpdm->dev = &pdev->dev; |
| 494 | |
Sachin Kamat | 6c3cc30 | 2013-09-17 10:28:02 +0530 | [diff] [blame] | 495 | return devm_snd_soc_register_component(&pdev->dev, |
| 496 | &omap_mcpdm_component, |
| 497 | &omap_mcpdm_dai, 1); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Peter Ujfalusi | 7cb8a1b | 2011-11-15 11:32:14 +0200 | [diff] [blame] | 500 | static const struct of_device_id omap_mcpdm_of_match[] = { |
| 501 | { .compatible = "ti,omap4-mcpdm", }, |
| 502 | { } |
| 503 | }; |
| 504 | MODULE_DEVICE_TABLE(of, omap_mcpdm_of_match); |
| 505 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 506 | static struct platform_driver asoc_mcpdm_driver = { |
| 507 | .driver = { |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 508 | .name = "omap-mcpdm", |
| 509 | .owner = THIS_MODULE, |
Peter Ujfalusi | 7cb8a1b | 2011-11-15 11:32:14 +0200 | [diff] [blame] | 510 | .of_match_table = omap_mcpdm_of_match, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 511 | }, |
| 512 | |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 513 | .probe = asoc_mcpdm_probe, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 514 | }; |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 515 | |
Axel Lin | beda5bf5 | 2011-11-25 10:12:16 +0800 | [diff] [blame] | 516 | module_platform_driver(asoc_mcpdm_driver); |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 517 | |
Peter Ujfalusi | d66a547 | 2012-07-06 12:19:10 +0200 | [diff] [blame] | 518 | MODULE_ALIAS("platform:omap-mcpdm"); |
Misael Lopez Cruz | f5f9d7b | 2011-07-05 19:50:45 +0300 | [diff] [blame] | 519 | MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>"); |
Misael Lopez Cruz | db72c2f | 2010-02-22 15:09:22 -0600 | [diff] [blame] | 520 | MODULE_DESCRIPTION("OMAP PDM SoC Interface"); |
| 521 | MODULE_LICENSE("GPL"); |