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