Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Renesas R-Car SSIU/SSI support |
| 3 | * |
| 4 | * Copyright (C) 2013 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 6 | * |
| 7 | * Based on fsi.c |
| 8 | * Kuninori Morimoto <morimoto.kuninori@renesas.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | #include <linux/delay.h> |
| 15 | #include "rsnd.h" |
| 16 | #define RSND_SSI_NAME_SIZE 16 |
| 17 | |
| 18 | /* |
| 19 | * SSICR |
| 20 | */ |
| 21 | #define FORCE (1 << 31) /* Fixed */ |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 22 | #define DMEN (1 << 28) /* DMA Enable */ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 23 | #define UIEN (1 << 27) /* Underflow Interrupt Enable */ |
| 24 | #define OIEN (1 << 26) /* Overflow Interrupt Enable */ |
| 25 | #define IIEN (1 << 25) /* Idle Mode Interrupt Enable */ |
| 26 | #define DIEN (1 << 24) /* Data Interrupt Enable */ |
| 27 | |
| 28 | #define DWL_8 (0 << 19) /* Data Word Length */ |
| 29 | #define DWL_16 (1 << 19) /* Data Word Length */ |
| 30 | #define DWL_18 (2 << 19) /* Data Word Length */ |
| 31 | #define DWL_20 (3 << 19) /* Data Word Length */ |
| 32 | #define DWL_22 (4 << 19) /* Data Word Length */ |
| 33 | #define DWL_24 (5 << 19) /* Data Word Length */ |
| 34 | #define DWL_32 (6 << 19) /* Data Word Length */ |
| 35 | |
| 36 | #define SWL_32 (3 << 16) /* R/W System Word Length */ |
| 37 | #define SCKD (1 << 15) /* Serial Bit Clock Direction */ |
| 38 | #define SWSD (1 << 14) /* Serial WS Direction */ |
| 39 | #define SCKP (1 << 13) /* Serial Bit Clock Polarity */ |
| 40 | #define SWSP (1 << 12) /* Serial WS Polarity */ |
| 41 | #define SDTA (1 << 10) /* Serial Data Alignment */ |
| 42 | #define DEL (1 << 8) /* Serial Data Delay */ |
| 43 | #define CKDV(v) (v << 4) /* Serial Clock Division Ratio */ |
| 44 | #define TRMD (1 << 1) /* Transmit/Receive Mode Select */ |
| 45 | #define EN (1 << 0) /* SSI Module Enable */ |
| 46 | |
| 47 | /* |
| 48 | * SSISR |
| 49 | */ |
| 50 | #define UIRQ (1 << 27) /* Underflow Error Interrupt Status */ |
| 51 | #define OIRQ (1 << 26) /* Overflow Error Interrupt Status */ |
| 52 | #define IIRQ (1 << 25) /* Idle Mode Interrupt Status */ |
| 53 | #define DIRQ (1 << 24) /* Data Interrupt Status Flag */ |
| 54 | |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 55 | /* |
| 56 | * SSIWSR |
| 57 | */ |
| 58 | #define CONT (1 << 8) /* WS Continue Function */ |
| 59 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 60 | struct rsnd_ssi { |
| 61 | struct clk *clk; |
| 62 | struct rsnd_ssi_platform_info *info; /* rcar_snd.h */ |
| 63 | struct rsnd_ssi *parent; |
| 64 | struct rsnd_mod mod; |
| 65 | |
| 66 | struct rsnd_dai *rdai; |
| 67 | struct rsnd_dai_stream *io; |
| 68 | u32 cr_own; |
| 69 | u32 cr_clk; |
| 70 | u32 cr_etc; |
| 71 | int err; |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 72 | int dma_offset; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 73 | unsigned int usrcnt; |
| 74 | unsigned int rate; |
| 75 | }; |
| 76 | |
| 77 | struct rsnd_ssiu { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 78 | int ssi_nr; |
| 79 | struct rsnd_ssi *ssi; |
| 80 | }; |
| 81 | |
| 82 | #define for_each_rsnd_ssi(pos, priv, i) \ |
| 83 | for (i = 0; \ |
| 84 | (i < rsnd_ssi_nr(priv)) && \ |
| 85 | ((pos) = ((struct rsnd_ssiu *)((priv)->ssiu))->ssi + i); \ |
| 86 | i++) |
| 87 | |
| 88 | #define rsnd_ssi_nr(priv) (((struct rsnd_ssiu *)((priv)->ssiu))->ssi_nr) |
| 89 | #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod) |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 90 | #define rsnd_dma_to_ssi(dma) rsnd_mod_to_ssi(rsnd_dma_to_mod(dma)) |
| 91 | #define rsnd_ssi_pio_available(ssi) ((ssi)->info->pio_irq > 0) |
| 92 | #define rsnd_ssi_dma_available(ssi) \ |
| 93 | rsnd_dma_available(rsnd_mod_to_dma(&(ssi)->mod)) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 94 | #define rsnd_ssi_clk_from_parent(ssi) ((ssi)->parent) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 95 | #define rsnd_ssi_mode_flags(p) ((p)->info->flags) |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 96 | #define rsnd_ssi_dai_id(ssi) ((ssi)->info->dai_id) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 97 | #define rsnd_ssi_to_ssiu(ssi)\ |
| 98 | (((struct rsnd_ssiu *)((ssi) - rsnd_mod_id(&(ssi)->mod))) - 1) |
| 99 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 100 | static void rsnd_ssi_status_check(struct rsnd_mod *mod, |
| 101 | u32 bit) |
| 102 | { |
| 103 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 104 | struct device *dev = rsnd_priv_to_dev(priv); |
| 105 | u32 status; |
| 106 | int i; |
| 107 | |
| 108 | for (i = 0; i < 1024; i++) { |
| 109 | status = rsnd_mod_read(mod, SSISR); |
| 110 | if (status & bit) |
| 111 | return; |
| 112 | |
| 113 | udelay(50); |
| 114 | } |
| 115 | |
| 116 | dev_warn(dev, "status check failed\n"); |
| 117 | } |
| 118 | |
| 119 | static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi, |
Kuninori Morimoto | adcf7d5 | 2013-12-19 19:28:39 -0800 | [diff] [blame] | 120 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 121 | { |
| 122 | struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod); |
Kuninori Morimoto | adcf7d5 | 2013-12-19 19:28:39 -0800 | [diff] [blame] | 123 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 124 | struct device *dev = rsnd_priv_to_dev(priv); |
| 125 | int i, j, ret; |
| 126 | int adg_clk_div_table[] = { |
| 127 | 1, 6, /* see adg.c */ |
| 128 | }; |
| 129 | int ssi_clk_mul_table[] = { |
| 130 | 1, 2, 4, 8, 16, 6, 12, |
| 131 | }; |
| 132 | unsigned int main_rate; |
Kuninori Morimoto | ef74940 | 2013-12-19 19:28:51 -0800 | [diff] [blame] | 133 | unsigned int rate = rsnd_scu_get_ssi_rate(priv, &ssi->mod, runtime); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * Find best clock, and try to start ADG |
| 137 | */ |
| 138 | for (i = 0; i < ARRAY_SIZE(adg_clk_div_table); i++) { |
| 139 | for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) { |
| 140 | |
| 141 | /* |
| 142 | * this driver is assuming that |
| 143 | * system word is 64fs (= 2 x 32bit) |
Kuninori Morimoto | 729aca5 | 2013-12-19 19:26:44 -0800 | [diff] [blame] | 144 | * see rsnd_ssi_init() |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 145 | */ |
| 146 | main_rate = rate / adg_clk_div_table[i] |
| 147 | * 32 * 2 * ssi_clk_mul_table[j]; |
| 148 | |
| 149 | ret = rsnd_adg_ssi_clk_try_start(&ssi->mod, main_rate); |
| 150 | if (0 == ret) { |
| 151 | ssi->rate = rate; |
| 152 | ssi->cr_clk = FORCE | SWL_32 | |
| 153 | SCKD | SWSD | CKDV(j); |
| 154 | |
| 155 | dev_dbg(dev, "ssi%d outputs %u Hz\n", |
| 156 | rsnd_mod_id(&ssi->mod), rate); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | dev_err(dev, "unsupported clock rate\n"); |
| 164 | return -EIO; |
| 165 | } |
| 166 | |
| 167 | static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi) |
| 168 | { |
| 169 | ssi->rate = 0; |
| 170 | ssi->cr_clk = 0; |
| 171 | rsnd_adg_ssi_clk_stop(&ssi->mod); |
| 172 | } |
| 173 | |
| 174 | static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi, |
| 175 | struct rsnd_dai *rdai, |
| 176 | struct rsnd_dai_stream *io) |
| 177 | { |
| 178 | struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod); |
| 179 | struct device *dev = rsnd_priv_to_dev(priv); |
| 180 | u32 cr; |
| 181 | |
| 182 | if (0 == ssi->usrcnt) { |
| 183 | clk_enable(ssi->clk); |
| 184 | |
Kuninori Morimoto | e779a20 | 2014-01-23 18:39:12 -0800 | [diff] [blame] | 185 | if (rsnd_dai_is_clk_master(rdai)) { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 186 | if (rsnd_ssi_clk_from_parent(ssi)) |
| 187 | rsnd_ssi_hw_start(ssi->parent, rdai, io); |
| 188 | else |
Kuninori Morimoto | adcf7d5 | 2013-12-19 19:28:39 -0800 | [diff] [blame] | 189 | rsnd_ssi_master_clk_start(ssi, io); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | cr = ssi->cr_own | |
| 194 | ssi->cr_clk | |
| 195 | ssi->cr_etc | |
| 196 | EN; |
| 197 | |
| 198 | rsnd_mod_write(&ssi->mod, SSICR, cr); |
| 199 | |
| 200 | ssi->usrcnt++; |
| 201 | |
| 202 | dev_dbg(dev, "ssi%d hw started\n", rsnd_mod_id(&ssi->mod)); |
| 203 | } |
| 204 | |
| 205 | static void rsnd_ssi_hw_stop(struct rsnd_ssi *ssi, |
| 206 | struct rsnd_dai *rdai) |
| 207 | { |
| 208 | struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod); |
| 209 | struct device *dev = rsnd_priv_to_dev(priv); |
| 210 | u32 cr; |
| 211 | |
| 212 | if (0 == ssi->usrcnt) /* stop might be called without start */ |
| 213 | return; |
| 214 | |
| 215 | ssi->usrcnt--; |
| 216 | |
| 217 | if (0 == ssi->usrcnt) { |
| 218 | /* |
| 219 | * disable all IRQ, |
| 220 | * and, wait all data was sent |
| 221 | */ |
| 222 | cr = ssi->cr_own | |
| 223 | ssi->cr_clk; |
| 224 | |
| 225 | rsnd_mod_write(&ssi->mod, SSICR, cr | EN); |
| 226 | rsnd_ssi_status_check(&ssi->mod, DIRQ); |
| 227 | |
| 228 | /* |
| 229 | * disable SSI, |
| 230 | * and, wait idle state |
| 231 | */ |
| 232 | rsnd_mod_write(&ssi->mod, SSICR, cr); /* disabled all */ |
| 233 | rsnd_ssi_status_check(&ssi->mod, IIRQ); |
| 234 | |
Kuninori Morimoto | e779a20 | 2014-01-23 18:39:12 -0800 | [diff] [blame] | 235 | if (rsnd_dai_is_clk_master(rdai)) { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 236 | if (rsnd_ssi_clk_from_parent(ssi)) |
| 237 | rsnd_ssi_hw_stop(ssi->parent, rdai); |
| 238 | else |
| 239 | rsnd_ssi_master_clk_stop(ssi); |
| 240 | } |
| 241 | |
| 242 | clk_disable(ssi->clk); |
| 243 | } |
| 244 | |
| 245 | dev_dbg(dev, "ssi%d hw stopped\n", rsnd_mod_id(&ssi->mod)); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * SSI mod common functions |
| 250 | */ |
| 251 | static int rsnd_ssi_init(struct rsnd_mod *mod, |
| 252 | struct rsnd_dai *rdai, |
| 253 | struct rsnd_dai_stream *io) |
| 254 | { |
| 255 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 256 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 257 | u32 cr; |
| 258 | |
| 259 | cr = FORCE; |
| 260 | |
| 261 | /* |
| 262 | * always use 32bit system word for easy clock calculation. |
| 263 | * see also rsnd_ssi_master_clk_enable() |
| 264 | */ |
| 265 | cr |= SWL_32; |
| 266 | |
| 267 | /* |
| 268 | * init clock settings for SSICR |
| 269 | */ |
| 270 | switch (runtime->sample_bits) { |
| 271 | case 16: |
| 272 | cr |= DWL_16; |
| 273 | break; |
| 274 | case 32: |
| 275 | cr |= DWL_24; |
| 276 | break; |
| 277 | default: |
| 278 | return -EIO; |
| 279 | } |
| 280 | |
| 281 | if (rdai->bit_clk_inv) |
| 282 | cr |= SCKP; |
| 283 | if (rdai->frm_clk_inv) |
| 284 | cr |= SWSP; |
| 285 | if (rdai->data_alignment) |
| 286 | cr |= SDTA; |
| 287 | if (rdai->sys_delay) |
| 288 | cr |= DEL; |
| 289 | if (rsnd_dai_is_play(rdai, io)) |
| 290 | cr |= TRMD; |
| 291 | |
| 292 | /* |
| 293 | * set ssi parameter |
| 294 | */ |
| 295 | ssi->rdai = rdai; |
| 296 | ssi->io = io; |
| 297 | ssi->cr_own = cr; |
| 298 | ssi->err = -1; /* ignore 1st error */ |
| 299 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int rsnd_ssi_quit(struct rsnd_mod *mod, |
| 304 | struct rsnd_dai *rdai, |
| 305 | struct rsnd_dai_stream *io) |
| 306 | { |
| 307 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 308 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 309 | struct device *dev = rsnd_priv_to_dev(priv); |
| 310 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 311 | if (ssi->err > 0) |
| 312 | dev_warn(dev, "ssi under/over flow err = %d\n", ssi->err); |
| 313 | |
| 314 | ssi->rdai = NULL; |
| 315 | ssi->io = NULL; |
| 316 | ssi->cr_own = 0; |
| 317 | ssi->err = 0; |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status) |
| 323 | { |
| 324 | /* under/over flow error */ |
| 325 | if (status & (UIRQ | OIRQ)) { |
| 326 | ssi->err++; |
| 327 | |
| 328 | /* clear error status */ |
| 329 | rsnd_mod_write(&ssi->mod, SSISR, 0); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * SSI PIO |
| 335 | */ |
| 336 | static irqreturn_t rsnd_ssi_pio_interrupt(int irq, void *data) |
| 337 | { |
| 338 | struct rsnd_ssi *ssi = data; |
| 339 | struct rsnd_dai_stream *io = ssi->io; |
| 340 | u32 status = rsnd_mod_read(&ssi->mod, SSISR); |
| 341 | irqreturn_t ret = IRQ_NONE; |
| 342 | |
| 343 | if (io && (status & DIRQ)) { |
| 344 | struct rsnd_dai *rdai = ssi->rdai; |
| 345 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 346 | u32 *buf = (u32 *)(runtime->dma_area + |
| 347 | rsnd_dai_pointer_offset(io, 0)); |
| 348 | |
| 349 | rsnd_ssi_record_error(ssi, status); |
| 350 | |
| 351 | /* |
| 352 | * 8/16/32 data can be assesse to TDR/RDR register |
| 353 | * directly as 32bit data |
| 354 | * see rsnd_ssi_init() |
| 355 | */ |
| 356 | if (rsnd_dai_is_play(rdai, io)) |
| 357 | rsnd_mod_write(&ssi->mod, SSITDR, *buf); |
| 358 | else |
| 359 | *buf = rsnd_mod_read(&ssi->mod, SSIRDR); |
| 360 | |
| 361 | rsnd_dai_pointer_update(io, sizeof(*buf)); |
| 362 | |
| 363 | ret = IRQ_HANDLED; |
| 364 | } |
| 365 | |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static int rsnd_ssi_pio_start(struct rsnd_mod *mod, |
| 370 | struct rsnd_dai *rdai, |
| 371 | struct rsnd_dai_stream *io) |
| 372 | { |
| 373 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 374 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 375 | |
| 376 | /* enable PIO IRQ */ |
| 377 | ssi->cr_etc = UIEN | OIEN | DIEN; |
| 378 | |
Kuninori Morimoto | 52ea2a7 | 2013-12-19 19:28:19 -0800 | [diff] [blame] | 379 | /* enable PIO interrupt if gen2 */ |
| 380 | if (rsnd_is_gen2(priv)) |
| 381 | rsnd_mod_write(&ssi->mod, INT_ENABLE, 0x0f000000); |
Kuninori Morimoto | 507d466 | 2013-11-28 18:43:45 -0800 | [diff] [blame] | 382 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 383 | rsnd_ssi_hw_start(ssi, rdai, io); |
| 384 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int rsnd_ssi_pio_stop(struct rsnd_mod *mod, |
| 389 | struct rsnd_dai *rdai, |
| 390 | struct rsnd_dai_stream *io) |
| 391 | { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 392 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 393 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 394 | ssi->cr_etc = 0; |
| 395 | |
| 396 | rsnd_ssi_hw_stop(ssi, rdai); |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static struct rsnd_mod_ops rsnd_ssi_pio_ops = { |
| 402 | .name = "ssi (pio)", |
| 403 | .init = rsnd_ssi_init, |
| 404 | .quit = rsnd_ssi_quit, |
| 405 | .start = rsnd_ssi_pio_start, |
| 406 | .stop = rsnd_ssi_pio_stop, |
| 407 | }; |
| 408 | |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 409 | static int rsnd_ssi_dma_inquiry(struct rsnd_dma *dma, dma_addr_t *buf, int *len) |
| 410 | { |
| 411 | struct rsnd_ssi *ssi = rsnd_dma_to_ssi(dma); |
| 412 | struct rsnd_dai_stream *io = ssi->io; |
| 413 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 414 | |
| 415 | *len = io->byte_per_period; |
| 416 | *buf = runtime->dma_addr + |
| 417 | rsnd_dai_pointer_offset(io, ssi->dma_offset + *len); |
| 418 | ssi->dma_offset = *len; /* it cares A/B plane */ |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int rsnd_ssi_dma_complete(struct rsnd_dma *dma) |
| 424 | { |
| 425 | struct rsnd_ssi *ssi = rsnd_dma_to_ssi(dma); |
| 426 | struct rsnd_dai_stream *io = ssi->io; |
| 427 | u32 status = rsnd_mod_read(&ssi->mod, SSISR); |
| 428 | |
| 429 | rsnd_ssi_record_error(ssi, status); |
| 430 | |
| 431 | rsnd_dai_pointer_update(ssi->io, io->byte_per_period); |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static int rsnd_ssi_dma_start(struct rsnd_mod *mod, |
| 437 | struct rsnd_dai *rdai, |
| 438 | struct rsnd_dai_stream *io) |
| 439 | { |
| 440 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 441 | struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod); |
| 442 | |
| 443 | /* enable DMA transfer */ |
| 444 | ssi->cr_etc = DMEN; |
| 445 | ssi->dma_offset = 0; |
| 446 | |
| 447 | rsnd_dma_start(dma); |
| 448 | |
| 449 | rsnd_ssi_hw_start(ssi, ssi->rdai, io); |
| 450 | |
| 451 | /* enable WS continue */ |
Kuninori Morimoto | e779a20 | 2014-01-23 18:39:12 -0800 | [diff] [blame] | 452 | if (rsnd_dai_is_clk_master(rdai)) |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 453 | rsnd_mod_write(&ssi->mod, SSIWSR, CONT); |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static int rsnd_ssi_dma_stop(struct rsnd_mod *mod, |
| 459 | struct rsnd_dai *rdai, |
| 460 | struct rsnd_dai_stream *io) |
| 461 | { |
| 462 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 463 | struct rsnd_dma *dma = rsnd_mod_to_dma(&ssi->mod); |
| 464 | |
| 465 | ssi->cr_etc = 0; |
| 466 | |
| 467 | rsnd_ssi_hw_stop(ssi, rdai); |
| 468 | |
| 469 | rsnd_dma_stop(dma); |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static struct rsnd_mod_ops rsnd_ssi_dma_ops = { |
| 475 | .name = "ssi (dma)", |
| 476 | .init = rsnd_ssi_init, |
| 477 | .quit = rsnd_ssi_quit, |
| 478 | .start = rsnd_ssi_dma_start, |
| 479 | .stop = rsnd_ssi_dma_stop, |
| 480 | }; |
| 481 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 482 | /* |
| 483 | * Non SSI |
| 484 | */ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 485 | static struct rsnd_mod_ops rsnd_ssi_non_ops = { |
| 486 | .name = "ssi (non)", |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | /* |
| 490 | * ssi mod function |
| 491 | */ |
Kuninori Morimoto | 4b4dab8 | 2013-07-28 18:58:29 -0700 | [diff] [blame] | 492 | struct rsnd_mod *rsnd_ssi_mod_get_frm_dai(struct rsnd_priv *priv, |
| 493 | int dai_id, int is_play) |
| 494 | { |
| 495 | struct rsnd_ssi *ssi; |
| 496 | int i, has_play; |
| 497 | |
| 498 | is_play = !!is_play; |
| 499 | |
| 500 | for_each_rsnd_ssi(ssi, priv, i) { |
| 501 | if (rsnd_ssi_dai_id(ssi) != dai_id) |
| 502 | continue; |
| 503 | |
| 504 | has_play = !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_PLAY); |
| 505 | |
| 506 | if (is_play == has_play) |
| 507 | return &ssi->mod; |
| 508 | } |
| 509 | |
| 510 | return NULL; |
| 511 | } |
| 512 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 513 | struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id) |
| 514 | { |
Takashi Iwai | 8b14719 | 2013-11-05 18:40:05 +0100 | [diff] [blame] | 515 | if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv))) |
| 516 | id = 0; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 517 | |
| 518 | return &(((struct rsnd_ssiu *)(priv->ssiu))->ssi + id)->mod; |
| 519 | } |
| 520 | |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame^] | 521 | int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod) |
| 522 | { |
| 523 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 524 | |
| 525 | return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE); |
| 526 | } |
| 527 | |
| 528 | static void rsnd_ssi_parent_clk_setup(struct rsnd_priv *priv, struct rsnd_ssi *ssi) |
| 529 | { |
| 530 | if (!rsnd_ssi_is_pin_sharing(&ssi->mod)) |
| 531 | return; |
| 532 | |
| 533 | switch (rsnd_mod_id(&ssi->mod)) { |
| 534 | case 1: |
| 535 | case 2: |
| 536 | ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 0)); |
| 537 | break; |
| 538 | case 4: |
| 539 | ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 3)); |
| 540 | break; |
| 541 | case 8: |
| 542 | ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 7)); |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 547 | int rsnd_ssi_probe(struct platform_device *pdev, |
| 548 | struct rcar_snd_info *info, |
| 549 | struct rsnd_priv *priv) |
| 550 | { |
| 551 | struct rsnd_ssi_platform_info *pinfo; |
| 552 | struct device *dev = rsnd_priv_to_dev(priv); |
| 553 | struct rsnd_mod_ops *ops; |
| 554 | struct clk *clk; |
| 555 | struct rsnd_ssiu *ssiu; |
| 556 | struct rsnd_ssi *ssi; |
| 557 | char name[RSND_SSI_NAME_SIZE]; |
| 558 | int i, nr, ret; |
| 559 | |
| 560 | /* |
| 561 | * init SSI |
| 562 | */ |
| 563 | nr = info->ssi_info_nr; |
| 564 | ssiu = devm_kzalloc(dev, sizeof(*ssiu) + (sizeof(*ssi) * nr), |
| 565 | GFP_KERNEL); |
| 566 | if (!ssiu) { |
| 567 | dev_err(dev, "SSI allocate failed\n"); |
| 568 | return -ENOMEM; |
| 569 | } |
| 570 | |
| 571 | priv->ssiu = ssiu; |
| 572 | ssiu->ssi = (struct rsnd_ssi *)(ssiu + 1); |
| 573 | ssiu->ssi_nr = nr; |
| 574 | |
| 575 | for_each_rsnd_ssi(ssi, priv, i) { |
| 576 | pinfo = &info->ssi_info[i]; |
| 577 | |
| 578 | snprintf(name, RSND_SSI_NAME_SIZE, "ssi.%d", i); |
| 579 | |
Kuninori Morimoto | 60dbb4f | 2013-12-03 22:09:33 -0800 | [diff] [blame] | 580 | clk = devm_clk_get(dev, name); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 581 | if (IS_ERR(clk)) |
| 582 | return PTR_ERR(clk); |
| 583 | |
| 584 | ssi->info = pinfo; |
| 585 | ssi->clk = clk; |
| 586 | |
| 587 | ops = &rsnd_ssi_non_ops; |
| 588 | |
| 589 | /* |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 590 | * SSI DMA case |
| 591 | */ |
| 592 | if (pinfo->dma_id > 0) { |
| 593 | ret = rsnd_dma_init( |
| 594 | priv, rsnd_mod_to_dma(&ssi->mod), |
| 595 | (rsnd_ssi_mode_flags(ssi) & RSND_SSI_PLAY), |
| 596 | pinfo->dma_id, |
| 597 | rsnd_ssi_dma_inquiry, |
| 598 | rsnd_ssi_dma_complete); |
| 599 | if (ret < 0) |
| 600 | dev_info(dev, "SSI DMA failed. try PIO transter\n"); |
| 601 | else |
| 602 | ops = &rsnd_ssi_dma_ops; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 603 | |
| 604 | dev_dbg(dev, "SSI%d use DMA transfer\n", i); |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 608 | * SSI PIO case |
| 609 | */ |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 610 | if (!rsnd_ssi_dma_available(ssi) && |
| 611 | rsnd_ssi_pio_available(ssi)) { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 612 | ret = devm_request_irq(dev, pinfo->pio_irq, |
| 613 | &rsnd_ssi_pio_interrupt, |
| 614 | IRQF_SHARED, |
| 615 | dev_name(dev), ssi); |
| 616 | if (ret) { |
| 617 | dev_err(dev, "SSI request interrupt failed\n"); |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | ops = &rsnd_ssi_pio_ops; |
Kuninori Morimoto | 374a5281 | 2013-07-28 18:59:12 -0700 | [diff] [blame] | 622 | |
| 623 | dev_dbg(dev, "SSI%d use PIO transfer\n", i); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | rsnd_mod_init(priv, &ssi->mod, ops, i); |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame^] | 627 | |
| 628 | rsnd_ssi_parent_clk_setup(priv, ssi); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 631 | dev_dbg(dev, "ssi probed\n"); |
| 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | void rsnd_ssi_remove(struct platform_device *pdev, |
| 637 | struct rsnd_priv *priv) |
| 638 | { |
| 639 | struct rsnd_ssi *ssi; |
| 640 | int i; |
| 641 | |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 642 | for_each_rsnd_ssi(ssi, priv, i) { |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 643 | if (rsnd_ssi_dma_available(ssi)) |
| 644 | rsnd_dma_quit(priv, rsnd_mod_to_dma(&ssi->mod)); |
| 645 | } |
| 646 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 647 | } |