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