Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * siu_pcm.c - ALSA driver for Renesas SH7343, SH7722 SIU peripheral. |
| 3 | * |
| 4 | * Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de> |
| 5 | * Copyright (C) 2006 Carlos Munoz <carlos@kenati.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 9 | * the Free Software Foundation; only version 2 of the License. |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/dma-mapping.h> |
| 22 | #include <linux/dmaengine.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/platform_device.h> |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 26 | |
| 27 | #include <sound/control.h> |
| 28 | #include <sound/core.h> |
| 29 | #include <sound/pcm.h> |
| 30 | #include <sound/pcm_params.h> |
Jarkko Nikula | 0d911bae | 2010-11-21 19:48:46 +0200 | [diff] [blame] | 31 | #include <sound/soc.h> |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 32 | |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 33 | #include <asm/siu.h> |
| 34 | |
| 35 | #include "siu.h" |
| 36 | |
| 37 | #define GET_MAX_PERIODS(buf_bytes, period_bytes) \ |
| 38 | ((buf_bytes) / (period_bytes)) |
| 39 | #define PERIOD_OFFSET(buf_addr, period_num, period_bytes) \ |
| 40 | ((buf_addr) + ((period_num) * (period_bytes))) |
| 41 | |
| 42 | #define RWF_STM_RD 0x01 /* Read in progress */ |
| 43 | #define RWF_STM_WT 0x02 /* Write in progress */ |
| 44 | |
| 45 | struct siu_port *siu_ports[SIU_PORT_NUM]; |
| 46 | |
| 47 | /* transfersize is number of u32 dma transfers per period */ |
| 48 | static int siu_pcm_stmwrite_stop(struct siu_port *port_info) |
| 49 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 50 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 51 | u32 __iomem *base = info->reg; |
| 52 | struct siu_stream *siu_stream = &port_info->playback; |
| 53 | u32 stfifo; |
| 54 | |
| 55 | if (!siu_stream->rw_flg) |
| 56 | return -EPERM; |
| 57 | |
| 58 | /* output FIFO disable */ |
| 59 | stfifo = siu_read32(base + SIU_STFIFO); |
| 60 | siu_write32(base + SIU_STFIFO, stfifo & ~0x0c180c18); |
| 61 | pr_debug("%s: STFIFO %x -> %x\n", __func__, |
| 62 | stfifo, stfifo & ~0x0c180c18); |
| 63 | |
| 64 | /* during stmwrite clear */ |
| 65 | siu_stream->rw_flg = 0; |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int siu_pcm_stmwrite_start(struct siu_port *port_info) |
| 71 | { |
| 72 | struct siu_stream *siu_stream = &port_info->playback; |
| 73 | |
| 74 | if (siu_stream->rw_flg) |
| 75 | return -EPERM; |
| 76 | |
| 77 | /* Current period in buffer */ |
| 78 | port_info->playback.cur_period = 0; |
| 79 | |
| 80 | /* during stmwrite flag set */ |
| 81 | siu_stream->rw_flg = RWF_STM_WT; |
| 82 | |
| 83 | /* DMA transfer start */ |
| 84 | tasklet_schedule(&siu_stream->tasklet); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static void siu_dma_tx_complete(void *arg) |
| 90 | { |
| 91 | struct siu_stream *siu_stream = arg; |
| 92 | |
| 93 | if (!siu_stream->rw_flg) |
| 94 | return; |
| 95 | |
| 96 | /* Update completed period count */ |
| 97 | if (++siu_stream->cur_period >= |
| 98 | GET_MAX_PERIODS(siu_stream->buf_bytes, |
| 99 | siu_stream->period_bytes)) |
| 100 | siu_stream->cur_period = 0; |
| 101 | |
| 102 | pr_debug("%s: done period #%d (%u/%u bytes), cookie %d\n", |
| 103 | __func__, siu_stream->cur_period, |
| 104 | siu_stream->cur_period * siu_stream->period_bytes, |
| 105 | siu_stream->buf_bytes, siu_stream->cookie); |
| 106 | |
| 107 | tasklet_schedule(&siu_stream->tasklet); |
| 108 | |
| 109 | /* Notify alsa: a period is done */ |
| 110 | snd_pcm_period_elapsed(siu_stream->substream); |
| 111 | } |
| 112 | |
| 113 | static int siu_pcm_wr_set(struct siu_port *port_info, |
| 114 | dma_addr_t buff, u32 size) |
| 115 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 116 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 117 | u32 __iomem *base = info->reg; |
| 118 | struct siu_stream *siu_stream = &port_info->playback; |
| 119 | struct snd_pcm_substream *substream = siu_stream->substream; |
| 120 | struct device *dev = substream->pcm->card->dev; |
| 121 | struct dma_async_tx_descriptor *desc; |
| 122 | dma_cookie_t cookie; |
| 123 | struct scatterlist sg; |
| 124 | u32 stfifo; |
| 125 | |
| 126 | sg_init_table(&sg, 1); |
| 127 | sg_set_page(&sg, pfn_to_page(PFN_DOWN(buff)), |
| 128 | size, offset_in_page(buff)); |
Markus Pietrek | 7b42176 | 2010-08-04 15:59:50 +0900 | [diff] [blame] | 129 | sg_dma_len(&sg) = size; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 130 | sg_dma_address(&sg) = buff; |
| 131 | |
| 132 | desc = siu_stream->chan->device->device_prep_slave_sg(siu_stream->chan, |
| 133 | &sg, 1, DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 134 | if (!desc) { |
| 135 | dev_err(dev, "Failed to allocate a dma descriptor\n"); |
| 136 | return -ENOMEM; |
| 137 | } |
| 138 | |
| 139 | desc->callback = siu_dma_tx_complete; |
| 140 | desc->callback_param = siu_stream; |
| 141 | cookie = desc->tx_submit(desc); |
| 142 | if (cookie < 0) { |
| 143 | dev_err(dev, "Failed to submit a dma transfer\n"); |
| 144 | return cookie; |
| 145 | } |
| 146 | |
| 147 | siu_stream->tx_desc = desc; |
| 148 | siu_stream->cookie = cookie; |
| 149 | |
| 150 | dma_async_issue_pending(siu_stream->chan); |
| 151 | |
| 152 | /* only output FIFO enable */ |
| 153 | stfifo = siu_read32(base + SIU_STFIFO); |
| 154 | siu_write32(base + SIU_STFIFO, stfifo | (port_info->stfifo & 0x0c180c18)); |
| 155 | dev_dbg(dev, "%s: STFIFO %x -> %x\n", __func__, |
| 156 | stfifo, stfifo | (port_info->stfifo & 0x0c180c18)); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int siu_pcm_rd_set(struct siu_port *port_info, |
| 162 | dma_addr_t buff, size_t size) |
| 163 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 164 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 165 | u32 __iomem *base = info->reg; |
| 166 | struct siu_stream *siu_stream = &port_info->capture; |
| 167 | struct snd_pcm_substream *substream = siu_stream->substream; |
| 168 | struct device *dev = substream->pcm->card->dev; |
| 169 | struct dma_async_tx_descriptor *desc; |
| 170 | dma_cookie_t cookie; |
| 171 | struct scatterlist sg; |
| 172 | u32 stfifo; |
| 173 | |
| 174 | dev_dbg(dev, "%s: %u@%llx\n", __func__, size, (unsigned long long)buff); |
| 175 | |
| 176 | sg_init_table(&sg, 1); |
| 177 | sg_set_page(&sg, pfn_to_page(PFN_DOWN(buff)), |
| 178 | size, offset_in_page(buff)); |
Markus Pietrek | 7b42176 | 2010-08-04 15:59:50 +0900 | [diff] [blame] | 179 | sg_dma_len(&sg) = size; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 180 | sg_dma_address(&sg) = buff; |
| 181 | |
| 182 | desc = siu_stream->chan->device->device_prep_slave_sg(siu_stream->chan, |
| 183 | &sg, 1, DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 184 | if (!desc) { |
| 185 | dev_err(dev, "Failed to allocate dma descriptor\n"); |
| 186 | return -ENOMEM; |
| 187 | } |
| 188 | |
| 189 | desc->callback = siu_dma_tx_complete; |
| 190 | desc->callback_param = siu_stream; |
| 191 | cookie = desc->tx_submit(desc); |
| 192 | if (cookie < 0) { |
| 193 | dev_err(dev, "Failed to submit dma descriptor\n"); |
| 194 | return cookie; |
| 195 | } |
| 196 | |
| 197 | siu_stream->tx_desc = desc; |
| 198 | siu_stream->cookie = cookie; |
| 199 | |
| 200 | dma_async_issue_pending(siu_stream->chan); |
| 201 | |
| 202 | /* only input FIFO enable */ |
| 203 | stfifo = siu_read32(base + SIU_STFIFO); |
| 204 | siu_write32(base + SIU_STFIFO, siu_read32(base + SIU_STFIFO) | |
| 205 | (port_info->stfifo & 0x13071307)); |
| 206 | dev_dbg(dev, "%s: STFIFO %x -> %x\n", __func__, |
| 207 | stfifo, stfifo | (port_info->stfifo & 0x13071307)); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static void siu_io_tasklet(unsigned long data) |
| 213 | { |
| 214 | struct siu_stream *siu_stream = (struct siu_stream *)data; |
| 215 | struct snd_pcm_substream *substream = siu_stream->substream; |
| 216 | struct device *dev = substream->pcm->card->dev; |
| 217 | struct snd_pcm_runtime *rt = substream->runtime; |
| 218 | struct siu_port *port_info = siu_port_info(substream); |
| 219 | |
| 220 | dev_dbg(dev, "%s: flags %x\n", __func__, siu_stream->rw_flg); |
| 221 | |
| 222 | if (!siu_stream->rw_flg) { |
| 223 | dev_dbg(dev, "%s: stream inactive\n", __func__); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 228 | dma_addr_t buff; |
| 229 | size_t count; |
| 230 | u8 *virt; |
| 231 | |
| 232 | buff = (dma_addr_t)PERIOD_OFFSET(rt->dma_addr, |
| 233 | siu_stream->cur_period, |
| 234 | siu_stream->period_bytes); |
| 235 | virt = PERIOD_OFFSET(rt->dma_area, |
| 236 | siu_stream->cur_period, |
| 237 | siu_stream->period_bytes); |
| 238 | count = siu_stream->period_bytes; |
| 239 | |
| 240 | /* DMA transfer start */ |
| 241 | siu_pcm_rd_set(port_info, buff, count); |
| 242 | } else { |
| 243 | siu_pcm_wr_set(port_info, |
| 244 | (dma_addr_t)PERIOD_OFFSET(rt->dma_addr, |
| 245 | siu_stream->cur_period, |
| 246 | siu_stream->period_bytes), |
| 247 | siu_stream->period_bytes); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* Capture */ |
| 252 | static int siu_pcm_stmread_start(struct siu_port *port_info) |
| 253 | { |
| 254 | struct siu_stream *siu_stream = &port_info->capture; |
| 255 | |
| 256 | if (siu_stream->xfer_cnt > 0x1000000) |
| 257 | return -EINVAL; |
| 258 | if (siu_stream->rw_flg) |
| 259 | return -EPERM; |
| 260 | |
| 261 | /* Current period in buffer */ |
| 262 | siu_stream->cur_period = 0; |
| 263 | |
| 264 | /* during stmread flag set */ |
| 265 | siu_stream->rw_flg = RWF_STM_RD; |
| 266 | |
| 267 | tasklet_schedule(&siu_stream->tasklet); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int siu_pcm_stmread_stop(struct siu_port *port_info) |
| 273 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 274 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 275 | u32 __iomem *base = info->reg; |
| 276 | struct siu_stream *siu_stream = &port_info->capture; |
| 277 | struct device *dev = siu_stream->substream->pcm->card->dev; |
| 278 | u32 stfifo; |
| 279 | |
| 280 | if (!siu_stream->rw_flg) |
| 281 | return -EPERM; |
| 282 | |
| 283 | /* input FIFO disable */ |
| 284 | stfifo = siu_read32(base + SIU_STFIFO); |
| 285 | siu_write32(base + SIU_STFIFO, stfifo & ~0x13071307); |
| 286 | dev_dbg(dev, "%s: STFIFO %x -> %x\n", __func__, |
| 287 | stfifo, stfifo & ~0x13071307); |
| 288 | |
| 289 | /* during stmread flag clear */ |
| 290 | siu_stream->rw_flg = 0; |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static int siu_pcm_hw_params(struct snd_pcm_substream *ss, |
| 296 | struct snd_pcm_hw_params *hw_params) |
| 297 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 298 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 299 | struct device *dev = ss->pcm->card->dev; |
| 300 | int ret; |
| 301 | |
| 302 | dev_dbg(dev, "%s: port=%d\n", __func__, info->port_id); |
| 303 | |
| 304 | ret = snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw_params)); |
| 305 | if (ret < 0) |
| 306 | dev_err(dev, "snd_pcm_lib_malloc_pages() failed\n"); |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static int siu_pcm_hw_free(struct snd_pcm_substream *ss) |
| 312 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 313 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 314 | struct siu_port *port_info = siu_port_info(ss); |
| 315 | struct device *dev = ss->pcm->card->dev; |
| 316 | struct siu_stream *siu_stream; |
| 317 | |
| 318 | if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 319 | siu_stream = &port_info->playback; |
| 320 | else |
| 321 | siu_stream = &port_info->capture; |
| 322 | |
| 323 | dev_dbg(dev, "%s: port=%d\n", __func__, info->port_id); |
| 324 | |
| 325 | return snd_pcm_lib_free_pages(ss); |
| 326 | } |
| 327 | |
| 328 | static bool filter(struct dma_chan *chan, void *slave) |
| 329 | { |
| 330 | struct sh_dmae_slave *param = slave; |
| 331 | |
| 332 | pr_debug("%s: slave ID %d\n", __func__, param->slave_id); |
| 333 | |
| 334 | if (unlikely(param->dma_dev != chan->device->dev)) |
| 335 | return false; |
| 336 | |
| 337 | chan->private = param; |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | static int siu_pcm_open(struct snd_pcm_substream *ss) |
| 342 | { |
| 343 | /* Playback / Capture */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 344 | struct snd_soc_pcm_runtime *rtd = ss->private_data; |
Guennadi Liakhovetski | c6834dd | 2010-09-17 12:30:18 +0200 | [diff] [blame] | 345 | struct siu_platform *pdata = rtd->platform->dev->platform_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 346 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 347 | struct siu_port *port_info = siu_port_info(ss); |
| 348 | struct siu_stream *siu_stream; |
| 349 | u32 port = info->port_id; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 350 | struct device *dev = ss->pcm->card->dev; |
| 351 | dma_cap_mask_t mask; |
| 352 | struct sh_dmae_slave *param; |
| 353 | |
| 354 | dma_cap_zero(mask); |
| 355 | dma_cap_set(DMA_SLAVE, mask); |
| 356 | |
| 357 | dev_dbg(dev, "%s, port=%d@%p\n", __func__, port, port_info); |
| 358 | |
| 359 | if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 360 | siu_stream = &port_info->playback; |
| 361 | param = &siu_stream->param; |
Guennadi Liakhovetski | 10440af | 2010-05-19 18:33:54 +0000 | [diff] [blame] | 362 | param->slave_id = port ? pdata->dma_slave_tx_b : |
| 363 | pdata->dma_slave_tx_a; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 364 | } else { |
| 365 | siu_stream = &port_info->capture; |
| 366 | param = &siu_stream->param; |
Guennadi Liakhovetski | 10440af | 2010-05-19 18:33:54 +0000 | [diff] [blame] | 367 | param->slave_id = port ? pdata->dma_slave_rx_b : |
| 368 | pdata->dma_slave_rx_a; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | param->dma_dev = pdata->dma_dev; |
| 372 | /* Get DMA channel */ |
| 373 | siu_stream->chan = dma_request_channel(mask, filter, param); |
| 374 | if (!siu_stream->chan) { |
| 375 | dev_err(dev, "DMA channel allocation failed!\n"); |
| 376 | return -EBUSY; |
| 377 | } |
| 378 | |
| 379 | siu_stream->substream = ss; |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static int siu_pcm_close(struct snd_pcm_substream *ss) |
| 385 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 386 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 387 | struct device *dev = ss->pcm->card->dev; |
| 388 | struct siu_port *port_info = siu_port_info(ss); |
| 389 | struct siu_stream *siu_stream; |
| 390 | |
| 391 | dev_dbg(dev, "%s: port=%d\n", __func__, info->port_id); |
| 392 | |
| 393 | if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 394 | siu_stream = &port_info->playback; |
| 395 | else |
| 396 | siu_stream = &port_info->capture; |
| 397 | |
| 398 | dma_release_channel(siu_stream->chan); |
| 399 | siu_stream->chan = NULL; |
| 400 | |
| 401 | siu_stream->substream = NULL; |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int siu_pcm_prepare(struct snd_pcm_substream *ss) |
| 407 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 408 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 409 | struct siu_port *port_info = siu_port_info(ss); |
| 410 | struct device *dev = ss->pcm->card->dev; |
| 411 | struct snd_pcm_runtime *rt = ss->runtime; |
| 412 | struct siu_stream *siu_stream; |
| 413 | snd_pcm_sframes_t xfer_cnt; |
| 414 | |
| 415 | if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 416 | siu_stream = &port_info->playback; |
| 417 | else |
| 418 | siu_stream = &port_info->capture; |
| 419 | |
| 420 | rt = siu_stream->substream->runtime; |
| 421 | |
| 422 | siu_stream->buf_bytes = snd_pcm_lib_buffer_bytes(ss); |
| 423 | siu_stream->period_bytes = snd_pcm_lib_period_bytes(ss); |
| 424 | |
| 425 | dev_dbg(dev, "%s: port=%d, %d channels, period=%u bytes\n", __func__, |
| 426 | info->port_id, rt->channels, siu_stream->period_bytes); |
| 427 | |
| 428 | /* We only support buffers that are multiples of the period */ |
| 429 | if (siu_stream->buf_bytes % siu_stream->period_bytes) { |
| 430 | dev_err(dev, "%s() - buffer=%d not multiple of period=%d\n", |
| 431 | __func__, siu_stream->buf_bytes, |
| 432 | siu_stream->period_bytes); |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | |
| 436 | xfer_cnt = bytes_to_frames(rt, siu_stream->period_bytes); |
| 437 | if (!xfer_cnt || xfer_cnt > 0x1000000) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | siu_stream->format = rt->format; |
| 441 | siu_stream->xfer_cnt = xfer_cnt; |
| 442 | |
| 443 | dev_dbg(dev, "port=%d buf=%lx buf_bytes=%d period_bytes=%d " |
| 444 | "format=%d channels=%d xfer_cnt=%d\n", info->port_id, |
| 445 | (unsigned long)rt->dma_addr, siu_stream->buf_bytes, |
| 446 | siu_stream->period_bytes, |
| 447 | siu_stream->format, rt->channels, (int)xfer_cnt); |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static int siu_pcm_trigger(struct snd_pcm_substream *ss, int cmd) |
| 453 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 454 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 455 | struct device *dev = ss->pcm->card->dev; |
| 456 | struct siu_port *port_info = siu_port_info(ss); |
| 457 | int ret; |
| 458 | |
| 459 | dev_dbg(dev, "%s: port=%d@%p, cmd=%d\n", __func__, |
| 460 | info->port_id, port_info, cmd); |
| 461 | |
| 462 | switch (cmd) { |
| 463 | case SNDRV_PCM_TRIGGER_START: |
| 464 | if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 465 | ret = siu_pcm_stmwrite_start(port_info); |
| 466 | else |
| 467 | ret = siu_pcm_stmread_start(port_info); |
| 468 | |
| 469 | if (ret < 0) |
| 470 | dev_warn(dev, "%s: start failed on port=%d\n", |
| 471 | __func__, info->port_id); |
| 472 | |
| 473 | break; |
| 474 | case SNDRV_PCM_TRIGGER_STOP: |
| 475 | if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 476 | siu_pcm_stmwrite_stop(port_info); |
| 477 | else |
| 478 | siu_pcm_stmread_stop(port_info); |
| 479 | ret = 0; |
| 480 | |
| 481 | break; |
| 482 | default: |
| 483 | dev_err(dev, "%s() unsupported cmd=%d\n", __func__, cmd); |
| 484 | ret = -EINVAL; |
| 485 | } |
| 486 | |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * So far only resolution of one period is supported, subject to extending the |
| 492 | * dmangine API |
| 493 | */ |
| 494 | static snd_pcm_uframes_t siu_pcm_pointer_dma(struct snd_pcm_substream *ss) |
| 495 | { |
| 496 | struct device *dev = ss->pcm->card->dev; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 497 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 498 | u32 __iomem *base = info->reg; |
| 499 | struct siu_port *port_info = siu_port_info(ss); |
| 500 | struct snd_pcm_runtime *rt = ss->runtime; |
| 501 | size_t ptr; |
| 502 | struct siu_stream *siu_stream; |
| 503 | |
| 504 | if (ss->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 505 | siu_stream = &port_info->playback; |
| 506 | else |
| 507 | siu_stream = &port_info->capture; |
| 508 | |
| 509 | /* |
| 510 | * ptr is the offset into the buffer where the dma is currently at. We |
| 511 | * check if the dma buffer has just wrapped. |
| 512 | */ |
| 513 | ptr = PERIOD_OFFSET(rt->dma_addr, |
| 514 | siu_stream->cur_period, |
| 515 | siu_stream->period_bytes) - rt->dma_addr; |
| 516 | |
| 517 | dev_dbg(dev, |
| 518 | "%s: port=%d, events %x, FSTS %x, xferred %u/%u, cookie %d\n", |
| 519 | __func__, info->port_id, siu_read32(base + SIU_EVNTC), |
| 520 | siu_read32(base + SIU_SBFSTS), ptr, siu_stream->buf_bytes, |
| 521 | siu_stream->cookie); |
| 522 | |
| 523 | if (ptr >= siu_stream->buf_bytes) |
| 524 | ptr = 0; |
| 525 | |
| 526 | return bytes_to_frames(ss->runtime, ptr); |
| 527 | } |
| 528 | |
Liam Girdwood | c6ac984 | 2011-02-04 22:12:30 +0000 | [diff] [blame] | 529 | static int siu_pcm_new(struct snd_soc_pcm_runtime *rtd) |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 530 | { |
| 531 | /* card->dev == socdev->dev, see snd_soc_new_pcms() */ |
Liam Girdwood | c6ac984 | 2011-02-04 22:12:30 +0000 | [diff] [blame] | 532 | struct snd_card *card = rtd->card->snd_card; |
| 533 | struct snd_pcm *pcm = rtd->pcm; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 534 | struct siu_info *info = siu_i2s_data; |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 535 | struct platform_device *pdev = to_platform_device(card->dev); |
| 536 | int ret; |
| 537 | int i; |
| 538 | |
| 539 | /* pdev->id selects between SIUA and SIUB */ |
| 540 | if (pdev->id < 0 || pdev->id >= SIU_PORT_NUM) |
| 541 | return -EINVAL; |
| 542 | |
| 543 | info->port_id = pdev->id; |
| 544 | |
| 545 | /* |
| 546 | * While the siu has 2 ports, only one port can be on at a time (only 1 |
| 547 | * SPB). So far all the boards using the siu had only one of the ports |
| 548 | * wired to a codec. To simplify things, we only register one port with |
| 549 | * alsa. In case both ports are needed, it should be changed here |
| 550 | */ |
| 551 | for (i = pdev->id; i < pdev->id + 1; i++) { |
| 552 | struct siu_port **port_info = &siu_ports[i]; |
| 553 | |
| 554 | ret = siu_init_port(i, port_info, card); |
| 555 | if (ret < 0) |
| 556 | return ret; |
| 557 | |
| 558 | ret = snd_pcm_lib_preallocate_pages_for_all(pcm, |
| 559 | SNDRV_DMA_TYPE_DEV, NULL, |
| 560 | SIU_BUFFER_BYTES_MAX, SIU_BUFFER_BYTES_MAX); |
| 561 | if (ret < 0) { |
| 562 | dev_err(card->dev, |
| 563 | "snd_pcm_lib_preallocate_pages_for_all() err=%d", |
| 564 | ret); |
| 565 | goto fail; |
| 566 | } |
| 567 | |
| 568 | (*port_info)->pcm = pcm; |
| 569 | |
| 570 | /* IO tasklets */ |
| 571 | tasklet_init(&(*port_info)->playback.tasklet, siu_io_tasklet, |
| 572 | (unsigned long)&(*port_info)->playback); |
| 573 | tasklet_init(&(*port_info)->capture.tasklet, siu_io_tasklet, |
| 574 | (unsigned long)&(*port_info)->capture); |
| 575 | } |
| 576 | |
| 577 | dev_info(card->dev, "SuperH SIU driver initialized.\n"); |
| 578 | return 0; |
| 579 | |
| 580 | fail: |
| 581 | siu_free_port(siu_ports[pdev->id]); |
| 582 | dev_err(card->dev, "SIU: failed to initialize.\n"); |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | static void siu_pcm_free(struct snd_pcm *pcm) |
| 587 | { |
| 588 | struct platform_device *pdev = to_platform_device(pcm->card->dev); |
| 589 | struct siu_port *port_info = siu_ports[pdev->id]; |
| 590 | |
| 591 | tasklet_kill(&port_info->capture.tasklet); |
| 592 | tasklet_kill(&port_info->playback.tasklet); |
| 593 | |
| 594 | siu_free_port(port_info); |
| 595 | snd_pcm_lib_preallocate_free_for_all(pcm); |
| 596 | |
| 597 | dev_dbg(pcm->card->dev, "%s\n", __func__); |
| 598 | } |
| 599 | |
| 600 | static struct snd_pcm_ops siu_pcm_ops = { |
| 601 | .open = siu_pcm_open, |
| 602 | .close = siu_pcm_close, |
| 603 | .ioctl = snd_pcm_lib_ioctl, |
| 604 | .hw_params = siu_pcm_hw_params, |
| 605 | .hw_free = siu_pcm_hw_free, |
| 606 | .prepare = siu_pcm_prepare, |
| 607 | .trigger = siu_pcm_trigger, |
| 608 | .pointer = siu_pcm_pointer_dma, |
| 609 | }; |
| 610 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 611 | struct snd_soc_platform_driver siu_platform = { |
| 612 | .ops = &siu_pcm_ops, |
Guennadi Liakhovetski | 895d450 | 2010-01-22 19:09:03 +0100 | [diff] [blame] | 613 | .pcm_new = siu_pcm_new, |
| 614 | .pcm_free = siu_pcm_free, |
| 615 | }; |
| 616 | EXPORT_SYMBOL_GPL(siu_platform); |