Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * imx-pcm-fiq.c -- ALSA Soc Audio Layer |
| 3 | * |
| 4 | * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de> |
| 5 | * |
| 6 | * This code is based on code copyrighted by Freescale, |
| 7 | * Liam Girdwood, Javier Martin and probably others. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | */ |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | |
| 23 | #include <sound/core.h> |
| 24 | #include <sound/initval.h> |
| 25 | #include <sound/pcm.h> |
| 26 | #include <sound/pcm_params.h> |
| 27 | #include <sound/soc.h> |
| 28 | |
| 29 | #include <asm/fiq.h> |
| 30 | |
| 31 | #include <mach/ssi.h> |
| 32 | |
| 33 | #include "imx-ssi.h" |
| 34 | |
| 35 | struct imx_pcm_runtime_data { |
| 36 | int period; |
| 37 | int periods; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 38 | unsigned long offset; |
Mark Brown | b4e82b5 | 2010-02-25 12:52:10 +0000 | [diff] [blame] | 39 | unsigned long last_offset; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 40 | unsigned long size; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 41 | struct timer_list timer; |
Mark Brown | b4e82b5 | 2010-02-25 12:52:10 +0000 | [diff] [blame] | 42 | int poll_time; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
Mark Brown | b4e82b5 | 2010-02-25 12:52:10 +0000 | [diff] [blame] | 45 | static inline void imx_ssi_set_next_poll(struct imx_pcm_runtime_data *iprtd) |
| 46 | { |
| 47 | iprtd->timer.expires = jiffies + iprtd->poll_time; |
| 48 | } |
| 49 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 50 | static void imx_ssi_timer_callback(unsigned long data) |
| 51 | { |
| 52 | struct snd_pcm_substream *substream = (void *)data; |
| 53 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 54 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 55 | struct pt_regs regs; |
Mark Brown | b4e82b5 | 2010-02-25 12:52:10 +0000 | [diff] [blame] | 56 | unsigned long delta; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 57 | |
| 58 | get_fiq_regs(®s); |
| 59 | |
| 60 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 61 | iprtd->offset = regs.ARM_r8 & 0xffff; |
| 62 | else |
| 63 | iprtd->offset = regs.ARM_r9 & 0xffff; |
| 64 | |
Mark Brown | b4e82b5 | 2010-02-25 12:52:10 +0000 | [diff] [blame] | 65 | /* How much data have we transferred since the last period report? */ |
| 66 | if (iprtd->offset >= iprtd->last_offset) |
| 67 | delta = iprtd->offset - iprtd->last_offset; |
| 68 | else |
| 69 | delta = runtime->buffer_size + iprtd->offset |
| 70 | - iprtd->last_offset; |
| 71 | |
| 72 | /* If we've transferred at least a period then report it and |
| 73 | * reset our poll time */ |
| 74 | if (delta >= runtime->period_size) { |
| 75 | snd_pcm_period_elapsed(substream); |
| 76 | iprtd->last_offset = iprtd->offset; |
| 77 | |
| 78 | imx_ssi_set_next_poll(iprtd); |
| 79 | } |
| 80 | |
| 81 | /* Restart the timer; if we didn't report we'll run on the next tick */ |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 82 | add_timer(&iprtd->timer); |
Mark Brown | b4e82b5 | 2010-02-25 12:52:10 +0000 | [diff] [blame] | 83 | |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static struct fiq_handler fh = { |
| 87 | .name = DRV_NAME, |
| 88 | }; |
| 89 | |
| 90 | static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream, |
| 91 | struct snd_pcm_hw_params *params) |
| 92 | { |
| 93 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 94 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 95 | |
| 96 | iprtd->size = params_buffer_bytes(params); |
| 97 | iprtd->periods = params_periods(params); |
Mark Brown | b4e82b5 | 2010-02-25 12:52:10 +0000 | [diff] [blame] | 98 | iprtd->period = params_period_bytes(params) ; |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 99 | iprtd->offset = 0; |
Mark Brown | b4e82b5 | 2010-02-25 12:52:10 +0000 | [diff] [blame] | 100 | iprtd->last_offset = 0; |
| 101 | iprtd->poll_time = HZ / (params_rate(params) / params_period_size(params)); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 102 | |
| 103 | snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int snd_imx_pcm_prepare(struct snd_pcm_substream *substream) |
| 109 | { |
| 110 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 111 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 112 | struct pt_regs regs; |
| 113 | |
| 114 | get_fiq_regs(®s); |
| 115 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 116 | regs.ARM_r8 = (iprtd->period * iprtd->periods - 1) << 16; |
| 117 | else |
| 118 | regs.ARM_r9 = (iprtd->period * iprtd->periods - 1) << 16; |
| 119 | |
| 120 | set_fiq_regs(®s); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int fiq_enable; |
| 126 | static int imx_pcm_fiq; |
| 127 | |
| 128 | static int snd_imx_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 129 | { |
| 130 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 131 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 132 | |
| 133 | switch (cmd) { |
| 134 | case SNDRV_PCM_TRIGGER_START: |
| 135 | case SNDRV_PCM_TRIGGER_RESUME: |
| 136 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Mark Brown | b4e82b5 | 2010-02-25 12:52:10 +0000 | [diff] [blame] | 137 | imx_ssi_set_next_poll(iprtd); |
Sascha Hauer | 8380222 | 2009-11-25 16:41:04 +0100 | [diff] [blame] | 138 | add_timer(&iprtd->timer); |
| 139 | if (++fiq_enable == 1) |
| 140 | enable_fiq(imx_pcm_fiq); |
| 141 | |
| 142 | break; |
| 143 | |
| 144 | case SNDRV_PCM_TRIGGER_STOP: |
| 145 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 146 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 147 | del_timer(&iprtd->timer); |
| 148 | if (--fiq_enable == 0) |
| 149 | disable_fiq(imx_pcm_fiq); |
| 150 | |
| 151 | |
| 152 | break; |
| 153 | default: |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static snd_pcm_uframes_t snd_imx_pcm_pointer(struct snd_pcm_substream *substream) |
| 161 | { |
| 162 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 163 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 164 | |
| 165 | return bytes_to_frames(substream->runtime, iprtd->offset); |
| 166 | } |
| 167 | |
| 168 | static struct snd_pcm_hardware snd_imx_hardware = { |
| 169 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 170 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 171 | SNDRV_PCM_INFO_MMAP | |
| 172 | SNDRV_PCM_INFO_MMAP_VALID | |
| 173 | SNDRV_PCM_INFO_PAUSE | |
| 174 | SNDRV_PCM_INFO_RESUME, |
| 175 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 176 | .rate_min = 8000, |
| 177 | .channels_min = 2, |
| 178 | .channels_max = 2, |
| 179 | .buffer_bytes_max = IMX_SSI_DMABUF_SIZE, |
| 180 | .period_bytes_min = 128, |
| 181 | .period_bytes_max = 16 * 1024, |
| 182 | .periods_min = 2, |
| 183 | .periods_max = 255, |
| 184 | .fifo_size = 0, |
| 185 | }; |
| 186 | |
| 187 | static int snd_imx_open(struct snd_pcm_substream *substream) |
| 188 | { |
| 189 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 190 | struct imx_pcm_runtime_data *iprtd; |
| 191 | int ret; |
| 192 | |
| 193 | iprtd = kzalloc(sizeof(*iprtd), GFP_KERNEL); |
| 194 | runtime->private_data = iprtd; |
| 195 | |
| 196 | init_timer(&iprtd->timer); |
| 197 | iprtd->timer.data = (unsigned long)substream; |
| 198 | iprtd->timer.function = imx_ssi_timer_callback; |
| 199 | |
| 200 | ret = snd_pcm_hw_constraint_integer(substream->runtime, |
| 201 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 202 | if (ret < 0) |
| 203 | return ret; |
| 204 | |
| 205 | snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int snd_imx_close(struct snd_pcm_substream *substream) |
| 210 | { |
| 211 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 212 | struct imx_pcm_runtime_data *iprtd = runtime->private_data; |
| 213 | |
| 214 | del_timer_sync(&iprtd->timer); |
| 215 | kfree(iprtd); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static struct snd_pcm_ops imx_pcm_ops = { |
| 221 | .open = snd_imx_open, |
| 222 | .close = snd_imx_close, |
| 223 | .ioctl = snd_pcm_lib_ioctl, |
| 224 | .hw_params = snd_imx_pcm_hw_params, |
| 225 | .prepare = snd_imx_pcm_prepare, |
| 226 | .trigger = snd_imx_pcm_trigger, |
| 227 | .pointer = snd_imx_pcm_pointer, |
| 228 | .mmap = snd_imx_pcm_mmap, |
| 229 | }; |
| 230 | |
| 231 | static int imx_pcm_fiq_new(struct snd_card *card, struct snd_soc_dai *dai, |
| 232 | struct snd_pcm *pcm) |
| 233 | { |
| 234 | int ret; |
| 235 | |
| 236 | ret = imx_pcm_new(card, dai, pcm); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | |
| 240 | if (dai->playback.channels_min) { |
| 241 | struct snd_pcm_substream *substream = |
| 242 | pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; |
| 243 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 244 | |
| 245 | imx_ssi_fiq_tx_buffer = (unsigned long)buf->area; |
| 246 | } |
| 247 | |
| 248 | if (dai->capture.channels_min) { |
| 249 | struct snd_pcm_substream *substream = |
| 250 | pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; |
| 251 | struct snd_dma_buffer *buf = &substream->dma_buffer; |
| 252 | |
| 253 | imx_ssi_fiq_rx_buffer = (unsigned long)buf->area; |
| 254 | } |
| 255 | |
| 256 | set_fiq_handler(&imx_ssi_fiq_start, |
| 257 | &imx_ssi_fiq_end - &imx_ssi_fiq_start); |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static struct snd_soc_platform imx_soc_platform_fiq = { |
| 263 | .pcm_ops = &imx_pcm_ops, |
| 264 | .pcm_new = imx_pcm_fiq_new, |
| 265 | .pcm_free = imx_pcm_free, |
| 266 | }; |
| 267 | |
| 268 | struct snd_soc_platform *imx_ssi_fiq_init(struct platform_device *pdev, |
| 269 | struct imx_ssi *ssi) |
| 270 | { |
| 271 | int ret = 0; |
| 272 | |
| 273 | ret = claim_fiq(&fh); |
| 274 | if (ret) { |
| 275 | dev_err(&pdev->dev, "failed to claim fiq: %d", ret); |
| 276 | return ERR_PTR(ret); |
| 277 | } |
| 278 | |
| 279 | mxc_set_irq_fiq(ssi->irq, 1); |
| 280 | |
| 281 | imx_pcm_fiq = ssi->irq; |
| 282 | |
| 283 | imx_ssi_fiq_base = (unsigned long)ssi->base; |
| 284 | |
| 285 | ssi->dma_params_tx.burstsize = 4; |
| 286 | ssi->dma_params_rx.burstsize = 6; |
| 287 | |
| 288 | return &imx_soc_platform_fiq; |
| 289 | } |
| 290 | |
| 291 | void imx_ssi_fiq_exit(struct platform_device *pdev, |
| 292 | struct imx_ssi *ssi) |
| 293 | { |
| 294 | mxc_set_irq_fiq(ssi->irq, 0); |
| 295 | release_fiq(&fh); |
| 296 | } |
| 297 | |