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 */ |
Kuninori Morimoto | f46a93b | 2015-11-17 08:28:11 +0000 | [diff] [blame] | 42 | #define PDTA (1 << 9) /* Parallel Data Alignment */ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 43 | #define DEL (1 << 8) /* Serial Data Delay */ |
| 44 | #define CKDV(v) (v << 4) /* Serial Clock Division Ratio */ |
| 45 | #define TRMD (1 << 1) /* Transmit/Receive Mode Select */ |
| 46 | #define EN (1 << 0) /* SSI Module Enable */ |
| 47 | |
| 48 | /* |
| 49 | * SSISR |
| 50 | */ |
| 51 | #define UIRQ (1 << 27) /* Underflow Error Interrupt Status */ |
| 52 | #define OIRQ (1 << 26) /* Overflow Error Interrupt Status */ |
| 53 | #define IIRQ (1 << 25) /* Idle Mode Interrupt Status */ |
| 54 | #define DIRQ (1 << 24) /* Data Interrupt Status Flag */ |
| 55 | |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 56 | /* |
| 57 | * SSIWSR |
| 58 | */ |
| 59 | #define CONT (1 << 8) /* WS Continue Function */ |
| 60 | |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 61 | #define SSI_NAME "ssi" |
| 62 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 63 | struct rsnd_ssi { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 64 | struct rsnd_ssi *parent; |
| 65 | struct rsnd_mod mod; |
Kuninori Morimoto | 940e947 | 2015-10-26 08:42:25 +0000 | [diff] [blame] | 66 | struct rsnd_mod *dma; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 67 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 68 | u32 flags; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 69 | u32 cr_own; |
| 70 | u32 cr_clk; |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 71 | u32 cr_mode; |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 72 | int chan; |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 73 | int rate; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 74 | int err; |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 75 | int irq; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 76 | unsigned int usrcnt; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 79 | /* flags */ |
| 80 | #define RSND_SSI_CLK_PIN_SHARE (1 << 0) |
| 81 | #define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */ |
| 82 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 83 | #define for_each_rsnd_ssi(pos, priv, i) \ |
| 84 | for (i = 0; \ |
| 85 | (i < rsnd_ssi_nr(priv)) && \ |
Kuninori Morimoto | dd27d80 | 2014-01-23 18:39:40 -0800 | [diff] [blame] | 86 | ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 87 | i++) |
| 88 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 89 | #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id) |
Kuninori Morimoto | 232c00b | 2015-10-26 08:38:26 +0000 | [diff] [blame] | 90 | #define rsnd_ssi_to_dma(mod) ((ssi)->dma) |
Kuninori Morimoto | dd27d80 | 2014-01-23 18:39:40 -0800 | [diff] [blame] | 91 | #define rsnd_ssi_nr(priv) ((priv)->ssi_nr) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 92 | #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod) |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 93 | #define rsnd_ssi_mode_flags(p) ((p)->flags) |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 94 | #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io)) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 95 | |
Kuninori Morimoto | b415b4d | 2015-10-22 03:15:46 +0000 | [diff] [blame] | 96 | int rsnd_ssi_use_busif(struct rsnd_dai_stream *io) |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 97 | { |
Kuninori Morimoto | b415b4d | 2015-10-22 03:15:46 +0000 | [diff] [blame] | 98 | struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io); |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 99 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 100 | int use_busif = 0; |
| 101 | |
Kuninori Morimoto | 7b466fc | 2014-11-27 08:05:54 +0000 | [diff] [blame] | 102 | if (!rsnd_ssi_is_dma_mode(mod)) |
| 103 | return 0; |
| 104 | |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 105 | if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF)) |
| 106 | use_busif = 1; |
| 107 | if (rsnd_io_to_mod_src(io)) |
| 108 | use_busif = 1; |
| 109 | |
| 110 | return use_busif; |
| 111 | } |
| 112 | |
Kuninori Morimoto | e10369d | 2015-10-26 08:42:09 +0000 | [diff] [blame] | 113 | static void rsnd_ssi_status_clear(struct rsnd_mod *mod) |
| 114 | { |
| 115 | rsnd_mod_write(mod, SSISR, 0); |
| 116 | } |
| 117 | |
| 118 | static u32 rsnd_ssi_status_get(struct rsnd_mod *mod) |
| 119 | { |
| 120 | return rsnd_mod_read(mod, SSISR); |
| 121 | } |
| 122 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 123 | static void rsnd_ssi_status_check(struct rsnd_mod *mod, |
| 124 | u32 bit) |
| 125 | { |
| 126 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 127 | struct device *dev = rsnd_priv_to_dev(priv); |
| 128 | u32 status; |
| 129 | int i; |
| 130 | |
| 131 | for (i = 0; i < 1024; i++) { |
Kuninori Morimoto | e10369d | 2015-10-26 08:42:09 +0000 | [diff] [blame] | 132 | status = rsnd_ssi_status_get(mod); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 133 | if (status & bit) |
| 134 | return; |
| 135 | |
| 136 | udelay(50); |
| 137 | } |
| 138 | |
| 139 | dev_warn(dev, "status check failed\n"); |
| 140 | } |
| 141 | |
Kuninori Morimoto | 37447b4 | 2015-10-26 08:40:41 +0000 | [diff] [blame] | 142 | static int rsnd_ssi_irq_enable(struct rsnd_mod *ssi_mod) |
| 143 | { |
| 144 | struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod); |
| 145 | |
| 146 | if (rsnd_is_gen1(priv)) |
| 147 | return 0; |
| 148 | |
| 149 | /* enable SSI interrupt if Gen2 */ |
| 150 | rsnd_mod_write(ssi_mod, SSI_INT_ENABLE, |
| 151 | rsnd_ssi_is_dma_mode(ssi_mod) ? |
| 152 | 0x0e000000 : 0x0f000000); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int rsnd_ssi_irq_disable(struct rsnd_mod *ssi_mod) |
| 158 | { |
| 159 | struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod); |
| 160 | |
| 161 | if (rsnd_is_gen1(priv)) |
| 162 | return 0; |
| 163 | |
| 164 | /* disable SSI interrupt if Gen2 */ |
| 165 | rsnd_mod_write(ssi_mod, SSI_INT_ENABLE, 0x00000000); |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 170 | static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi, |
Kuninori Morimoto | adcf7d5 | 2013-12-19 19:28:39 -0800 | [diff] [blame] | 171 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 172 | { |
Kuninori Morimoto | 1b13d118 | 2015-01-15 08:08:34 +0000 | [diff] [blame] | 173 | struct rsnd_priv *priv = rsnd_io_to_priv(io); |
Kuninori Morimoto | adcf7d5 | 2013-12-19 19:28:39 -0800 | [diff] [blame] | 174 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 175 | struct device *dev = rsnd_priv_to_dev(priv); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 176 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
Kuninori Morimoto | b76e218 | 2015-09-10 07:02:21 +0000 | [diff] [blame] | 177 | struct rsnd_mod *mod = rsnd_mod_get(ssi); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 178 | struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io); |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 179 | int j, ret; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 180 | int ssi_clk_mul_table[] = { |
| 181 | 1, 2, 4, 8, 16, 6, 12, |
| 182 | }; |
| 183 | unsigned int main_rate; |
Kuninori Morimoto | ba9c949 | 2014-03-03 20:51:21 -0800 | [diff] [blame] | 184 | unsigned int rate = rsnd_src_get_ssi_rate(priv, io, runtime); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 185 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 186 | if (!rsnd_rdai_is_clk_master(rdai)) |
| 187 | return 0; |
| 188 | |
| 189 | if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io)) |
| 190 | return 0; |
| 191 | |
| 192 | if (ssi->usrcnt > 1) { |
| 193 | if (ssi->rate != rate) { |
| 194 | dev_err(dev, "SSI parent/child should use same rate\n"); |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 201 | /* |
| 202 | * Find best clock, and try to start ADG |
| 203 | */ |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 204 | for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 205 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 206 | /* |
| 207 | * this driver is assuming that |
| 208 | * system word is 64fs (= 2 x 32bit) |
| 209 | * see rsnd_ssi_init() |
| 210 | */ |
| 211 | main_rate = rate * 32 * 2 * ssi_clk_mul_table[j]; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 212 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 213 | ret = rsnd_adg_ssi_clk_try_start(mod, main_rate); |
| 214 | if (0 == ret) { |
| 215 | ssi->cr_clk = FORCE | SWL_32 | |
| 216 | SCKD | SWSD | CKDV(j); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 217 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 218 | ssi->rate = rate; |
| 219 | |
| 220 | rsnd_mod_write(mod, SSIWSR, CONT); |
| 221 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 222 | dev_dbg(dev, "%s[%d] outputs %u Hz\n", |
| 223 | rsnd_mod_name(mod), |
| 224 | rsnd_mod_id(mod), rate); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 225 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 226 | return 0; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | dev_err(dev, "unsupported clock rate\n"); |
| 231 | return -EIO; |
| 232 | } |
| 233 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 234 | static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi, |
| 235 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 236 | { |
Kuninori Morimoto | f708d94 | 2015-01-15 08:07:19 +0000 | [diff] [blame] | 237 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
Kuninori Morimoto | b76e218 | 2015-09-10 07:02:21 +0000 | [diff] [blame] | 238 | struct rsnd_mod *mod = rsnd_mod_get(ssi); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 239 | struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 240 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 241 | if (!rsnd_rdai_is_clk_master(rdai)) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 242 | return; |
| 243 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 244 | if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io)) |
| 245 | return; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 246 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 247 | if (ssi->usrcnt > 1) |
| 248 | return; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 249 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 250 | ssi->cr_clk = 0; |
| 251 | ssi->rate = 0; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 252 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 253 | rsnd_adg_ssi_clk_stop(mod); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* |
| 257 | * SSI mod common functions |
| 258 | */ |
| 259 | static int rsnd_ssi_init(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 260 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 261 | struct rsnd_priv *priv) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 262 | { |
| 263 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 264 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 265 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 266 | u32 cr; |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 267 | int ret; |
| 268 | |
| 269 | ssi->usrcnt++; |
| 270 | |
| 271 | rsnd_mod_power_on(mod); |
| 272 | |
| 273 | ret = rsnd_ssi_master_clk_start(ssi, io); |
| 274 | if (ret < 0) |
| 275 | return ret; |
| 276 | |
| 277 | if (rsnd_ssi_is_parent(mod, io)) |
| 278 | return 0; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 279 | |
Kuninori Morimoto | f46a93b | 2015-11-17 08:28:11 +0000 | [diff] [blame] | 280 | cr = FORCE | PDTA; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 281 | |
| 282 | /* |
| 283 | * always use 32bit system word for easy clock calculation. |
| 284 | * see also rsnd_ssi_master_clk_enable() |
| 285 | */ |
| 286 | cr |= SWL_32; |
| 287 | |
| 288 | /* |
| 289 | * init clock settings for SSICR |
| 290 | */ |
| 291 | switch (runtime->sample_bits) { |
| 292 | case 16: |
| 293 | cr |= DWL_16; |
| 294 | break; |
| 295 | case 32: |
| 296 | cr |= DWL_24; |
| 297 | break; |
| 298 | default: |
| 299 | return -EIO; |
| 300 | } |
| 301 | |
| 302 | if (rdai->bit_clk_inv) |
| 303 | cr |= SCKP; |
| 304 | if (rdai->frm_clk_inv) |
| 305 | cr |= SWSP; |
| 306 | if (rdai->data_alignment) |
| 307 | cr |= SDTA; |
| 308 | if (rdai->sys_delay) |
| 309 | cr |= DEL; |
Kuninori Morimoto | 985a4f6 | 2015-01-15 08:06:49 +0000 | [diff] [blame] | 310 | if (rsnd_io_is_play(io)) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 311 | cr |= TRMD; |
| 312 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 313 | ssi->cr_own = cr; |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 314 | |
| 315 | if (rsnd_ssi_is_dma_mode(mod)) { |
| 316 | cr = UIEN | OIEN | /* over/under run */ |
| 317 | DMEN; /* DMA : enable DMA */ |
| 318 | } else { |
| 319 | cr = DIEN; /* PIO : enable Data interrupt */ |
| 320 | } |
| 321 | |
| 322 | ssi->cr_mode = cr; |
| 323 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 324 | ssi->err = -1; /* ignore 1st error */ |
| 325 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 326 | /* clear error status */ |
| 327 | rsnd_ssi_status_clear(mod); |
| 328 | |
| 329 | rsnd_ssi_irq_enable(mod); |
| 330 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int rsnd_ssi_quit(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 335 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 336 | struct rsnd_priv *priv) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 337 | { |
| 338 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 339 | struct device *dev = rsnd_priv_to_dev(priv); |
| 340 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 341 | if (rsnd_ssi_is_parent(mod, io)) |
| 342 | goto rsnd_ssi_quit_end; |
| 343 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 344 | if (ssi->err > 0) |
Kuninori Morimoto | 337b0b4 | 2015-01-15 08:08:57 +0000 | [diff] [blame] | 345 | dev_warn(dev, "%s[%d] under/over flow err = %d\n", |
| 346 | rsnd_mod_name(mod), rsnd_mod_id(mod), ssi->err); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 347 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 348 | ssi->cr_own = 0; |
| 349 | ssi->err = 0; |
| 350 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 351 | rsnd_ssi_irq_disable(mod); |
| 352 | |
| 353 | rsnd_ssi_quit_end: |
| 354 | rsnd_ssi_master_clk_stop(ssi, io); |
| 355 | |
| 356 | rsnd_mod_power_off(mod); |
| 357 | |
| 358 | ssi->usrcnt--; |
| 359 | |
| 360 | if (ssi->usrcnt < 0) |
| 361 | dev_err(dev, "%s[%d] usrcnt error\n", |
| 362 | rsnd_mod_name(mod), rsnd_mod_id(mod)); |
| 363 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 367 | static int rsnd_ssi_hw_params(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 368 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 369 | struct snd_pcm_substream *substream, |
| 370 | struct snd_pcm_hw_params *params) |
| 371 | { |
| 372 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 373 | int chan = params_channels(params); |
| 374 | |
| 375 | /* |
| 376 | * Already working. |
| 377 | * It will happen if SSI has parent/child connection. |
| 378 | */ |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 379 | if (ssi->usrcnt > 1) { |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 380 | /* |
| 381 | * it is error if child <-> parent SSI uses |
| 382 | * different channels. |
| 383 | */ |
| 384 | if (ssi->chan != chan) |
| 385 | return -EIO; |
| 386 | } |
| 387 | |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 388 | ssi->chan = chan; |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
Kuninori Morimoto | e10369d | 2015-10-26 08:42:09 +0000 | [diff] [blame] | 393 | static u32 rsnd_ssi_record_error(struct rsnd_ssi *ssi) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 394 | { |
Kuninori Morimoto | b76e218 | 2015-09-10 07:02:21 +0000 | [diff] [blame] | 395 | struct rsnd_mod *mod = rsnd_mod_get(ssi); |
Kuninori Morimoto | e10369d | 2015-10-26 08:42:09 +0000 | [diff] [blame] | 396 | u32 status = rsnd_ssi_status_get(mod); |
Kuninori Morimoto | b76e218 | 2015-09-10 07:02:21 +0000 | [diff] [blame] | 397 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 398 | /* under/over flow error */ |
| 399 | if (status & (UIRQ | OIRQ)) { |
| 400 | ssi->err++; |
| 401 | |
| 402 | /* clear error status */ |
Kuninori Morimoto | e10369d | 2015-10-26 08:42:09 +0000 | [diff] [blame] | 403 | rsnd_ssi_status_clear(mod); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 404 | } |
Kuninori Morimoto | e10369d | 2015-10-26 08:42:09 +0000 | [diff] [blame] | 405 | |
| 406 | return status; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 409 | static int __rsnd_ssi_start(struct rsnd_mod *mod, |
| 410 | struct rsnd_dai_stream *io, |
| 411 | struct rsnd_priv *priv) |
| 412 | { |
| 413 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 414 | u32 cr; |
| 415 | |
| 416 | cr = ssi->cr_own | |
| 417 | ssi->cr_clk | |
| 418 | ssi->cr_mode | |
| 419 | EN; |
| 420 | |
| 421 | rsnd_mod_write(mod, SSICR, cr); |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Kuninori Morimoto | 4922985 | 2014-11-27 08:07:17 +0000 | [diff] [blame] | 426 | static int rsnd_ssi_start(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 427 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 428 | struct rsnd_priv *priv) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 429 | { |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 430 | /* |
| 431 | * no limit to start |
| 432 | * see also |
| 433 | * rsnd_ssi_stop |
| 434 | * rsnd_ssi_interrupt |
| 435 | */ |
| 436 | return __rsnd_ssi_start(mod, io, priv); |
| 437 | } |
| 438 | |
| 439 | static int __rsnd_ssi_stop(struct rsnd_mod *mod, |
| 440 | struct rsnd_dai_stream *io, |
| 441 | struct rsnd_priv *priv) |
| 442 | { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 443 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 444 | u32 cr; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 445 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 446 | /* |
| 447 | * disable all IRQ, |
| 448 | * and, wait all data was sent |
| 449 | */ |
| 450 | cr = ssi->cr_own | |
| 451 | ssi->cr_clk; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 452 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 453 | rsnd_mod_write(mod, SSICR, cr | EN); |
| 454 | rsnd_ssi_status_check(mod, DIRQ); |
| 455 | |
| 456 | /* |
| 457 | * disable SSI, |
| 458 | * and, wait idle state |
| 459 | */ |
| 460 | rsnd_mod_write(mod, SSICR, cr); /* disabled all */ |
| 461 | rsnd_ssi_status_check(mod, IIRQ); |
Kuninori Morimoto | c17dba8 | 2014-11-27 08:05:09 +0000 | [diff] [blame] | 462 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
Kuninori Morimoto | 4922985 | 2014-11-27 08:07:17 +0000 | [diff] [blame] | 466 | static int rsnd_ssi_stop(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 467 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 468 | struct rsnd_priv *priv) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 469 | { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 470 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 471 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 472 | /* |
| 473 | * don't stop if not last user |
| 474 | * see also |
| 475 | * rsnd_ssi_start |
| 476 | * rsnd_ssi_interrupt |
| 477 | */ |
| 478 | if (ssi->usrcnt > 1) |
| 479 | return 0; |
Kuninori Morimoto | c17dba8 | 2014-11-27 08:05:09 +0000 | [diff] [blame] | 480 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 481 | return __rsnd_ssi_stop(mod, io, priv); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Kuninori Morimoto | bfc0cfe | 2015-06-15 06:26:56 +0000 | [diff] [blame] | 484 | static void __rsnd_ssi_interrupt(struct rsnd_mod *mod, |
| 485 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 486 | { |
Kuninori Morimoto | bfc0cfe | 2015-06-15 06:26:56 +0000 | [diff] [blame] | 487 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 488 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
Kuninori Morimoto | 69e32a5 | 2015-10-26 08:41:36 +0000 | [diff] [blame] | 489 | struct device *dev = rsnd_priv_to_dev(priv); |
Kuninori Morimoto | 765ae7c | 2015-01-15 08:09:13 +0000 | [diff] [blame] | 490 | int is_dma = rsnd_ssi_is_dma_mode(mod); |
Kuninori Morimoto | 02299d9 | 2015-05-21 03:50:23 +0000 | [diff] [blame] | 491 | u32 status; |
Kuninori Morimoto | 75defee | 2015-06-15 06:21:15 +0000 | [diff] [blame] | 492 | bool elapsed = false; |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 493 | |
Kuninori Morimoto | 02299d9 | 2015-05-21 03:50:23 +0000 | [diff] [blame] | 494 | spin_lock(&priv->lock); |
| 495 | |
| 496 | /* ignore all cases if not working */ |
Kuninori Morimoto | d5bbe7d | 2015-06-15 06:27:47 +0000 | [diff] [blame] | 497 | if (!rsnd_io_is_working(io)) |
Kuninori Morimoto | 02299d9 | 2015-05-21 03:50:23 +0000 | [diff] [blame] | 498 | goto rsnd_ssi_interrupt_out; |
| 499 | |
Kuninori Morimoto | e10369d | 2015-10-26 08:42:09 +0000 | [diff] [blame] | 500 | status = rsnd_ssi_record_error(ssi); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 501 | |
| 502 | /* PIO only */ |
Kuninori Morimoto | 765ae7c | 2015-01-15 08:09:13 +0000 | [diff] [blame] | 503 | if (!is_dma && (status & DIRQ)) { |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 504 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 505 | u32 *buf = (u32 *)(runtime->dma_area + |
| 506 | rsnd_dai_pointer_offset(io, 0)); |
| 507 | |
| 508 | /* |
| 509 | * 8/16/32 data can be assesse to TDR/RDR register |
| 510 | * directly as 32bit data |
| 511 | * see rsnd_ssi_init() |
| 512 | */ |
Kuninori Morimoto | 985a4f6 | 2015-01-15 08:06:49 +0000 | [diff] [blame] | 513 | if (rsnd_io_is_play(io)) |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 514 | rsnd_mod_write(mod, SSITDR, *buf); |
| 515 | else |
| 516 | *buf = rsnd_mod_read(mod, SSIRDR); |
| 517 | |
Kuninori Morimoto | 75defee | 2015-06-15 06:21:15 +0000 | [diff] [blame] | 518 | elapsed = rsnd_dai_pointer_update(io, sizeof(*buf)); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Kuninori Morimoto | 12927a8 | 2015-06-15 06:20:54 +0000 | [diff] [blame] | 521 | /* DMA only */ |
| 522 | if (is_dma && (status & (UIRQ | OIRQ))) { |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 523 | /* |
| 524 | * restart SSI |
| 525 | */ |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 526 | dev_dbg(dev, "%s[%d] restart\n", |
| 527 | rsnd_mod_name(mod), rsnd_mod_id(mod)); |
Kuninori Morimoto | 044930b | 2015-03-19 04:13:47 +0000 | [diff] [blame] | 528 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 529 | __rsnd_ssi_stop(mod, io, priv); |
| 530 | __rsnd_ssi_start(mod, io, priv); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Kuninori Morimoto | 69e32a5 | 2015-10-26 08:41:36 +0000 | [diff] [blame] | 533 | if (ssi->err > 1024) { |
| 534 | rsnd_ssi_irq_disable(mod); |
| 535 | |
| 536 | dev_warn(dev, "no more %s[%d] restart\n", |
| 537 | rsnd_mod_name(mod), rsnd_mod_id(mod)); |
| 538 | } |
| 539 | |
Kuninori Morimoto | 02299d9 | 2015-05-21 03:50:23 +0000 | [diff] [blame] | 540 | rsnd_ssi_interrupt_out: |
| 541 | spin_unlock(&priv->lock); |
| 542 | |
Kuninori Morimoto | 75defee | 2015-06-15 06:21:15 +0000 | [diff] [blame] | 543 | if (elapsed) |
| 544 | rsnd_dai_period_elapsed(io); |
Kuninori Morimoto | bfc0cfe | 2015-06-15 06:26:56 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static irqreturn_t rsnd_ssi_interrupt(int irq, void *data) |
| 548 | { |
| 549 | struct rsnd_mod *mod = data; |
| 550 | |
| 551 | rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt); |
Kuninori Morimoto | 75defee | 2015-06-15 06:21:15 +0000 | [diff] [blame] | 552 | |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 553 | return IRQ_HANDLED; |
| 554 | } |
| 555 | |
Kuninori Morimoto | 6cfad78 | 2014-11-27 08:08:10 +0000 | [diff] [blame] | 556 | /* |
| 557 | * SSI PIO |
| 558 | */ |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 559 | static void rsnd_ssi_parent_attach(struct rsnd_mod *mod, |
| 560 | struct rsnd_dai_stream *io, |
| 561 | struct rsnd_priv *priv) |
| 562 | { |
| 563 | if (!__rsnd_ssi_is_pin_sharing(mod)) |
| 564 | return; |
| 565 | |
| 566 | switch (rsnd_mod_id(mod)) { |
| 567 | case 1: |
| 568 | case 2: |
| 569 | rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP); |
| 570 | break; |
| 571 | case 4: |
| 572 | rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP); |
| 573 | break; |
| 574 | case 8: |
| 575 | rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP); |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 580 | static int rsnd_ssi_common_probe(struct rsnd_mod *mod, |
| 581 | struct rsnd_dai_stream *io, |
| 582 | struct rsnd_priv *priv) |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 583 | { |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 584 | struct device *dev = rsnd_priv_to_dev(priv); |
| 585 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 586 | int ret; |
| 587 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 588 | rsnd_ssi_parent_attach(mod, io, priv); |
| 589 | |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 590 | ret = rsnd_ssiu_attach(io, mod); |
| 591 | if (ret < 0) |
| 592 | return ret; |
| 593 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 594 | ret = devm_request_irq(dev, ssi->irq, |
Kuninori Morimoto | 6cfad78 | 2014-11-27 08:08:10 +0000 | [diff] [blame] | 595 | rsnd_ssi_interrupt, |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 596 | IRQF_SHARED, |
Kuninori Morimoto | bfc0cfe | 2015-06-15 06:26:56 +0000 | [diff] [blame] | 597 | dev_name(dev), mod); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 598 | |
| 599 | return ret; |
| 600 | } |
| 601 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 602 | static struct rsnd_mod_ops rsnd_ssi_pio_ops = { |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 603 | .name = SSI_NAME, |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 604 | .probe = rsnd_ssi_common_probe, |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 605 | .init = rsnd_ssi_init, |
| 606 | .quit = rsnd_ssi_quit, |
Kuninori Morimoto | 4922985 | 2014-11-27 08:07:17 +0000 | [diff] [blame] | 607 | .start = rsnd_ssi_start, |
| 608 | .stop = rsnd_ssi_stop, |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 609 | .hw_params = rsnd_ssi_hw_params, |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 610 | }; |
| 611 | |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 612 | static int rsnd_ssi_dma_probe(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 613 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 614 | struct rsnd_priv *priv) |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 615 | { |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 616 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 617 | int dma_id = 0; /* not needed */ |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 618 | int ret; |
| 619 | |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 620 | ret = rsnd_ssi_common_probe(mod, io, priv); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 621 | if (ret) |
Kuninori Morimoto | b543b52 | 2015-03-26 04:02:32 +0000 | [diff] [blame] | 622 | return ret; |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 623 | |
Kuninori Morimoto | 81ecbb6 | 2015-10-26 08:39:20 +0000 | [diff] [blame] | 624 | ssi->dma = rsnd_dma_attach(io, mod, dma_id); |
Kuninori Morimoto | 232c00b | 2015-10-26 08:38:26 +0000 | [diff] [blame] | 625 | if (IS_ERR(ssi->dma)) |
| 626 | return PTR_ERR(ssi->dma); |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 627 | |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | static int rsnd_ssi_dma_remove(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 632 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 633 | struct rsnd_priv *priv) |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 634 | { |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 635 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 636 | struct device *dev = rsnd_priv_to_dev(priv); |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 637 | int irq = ssi->irq; |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 638 | |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 639 | /* PIO will request IRQ again */ |
Kuninori Morimoto | b05ce4c | 2015-10-22 03:13:44 +0000 | [diff] [blame] | 640 | devm_free_irq(dev, irq, mod); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 641 | |
Kuninori Morimoto | 97463e1 | 2014-11-27 08:02:43 +0000 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | static int rsnd_ssi_fallback(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 646 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 647 | struct rsnd_priv *priv) |
Kuninori Morimoto | 97463e1 | 2014-11-27 08:02:43 +0000 | [diff] [blame] | 648 | { |
Kuninori Morimoto | d3a7682 | 2014-11-09 20:00:58 -0800 | [diff] [blame] | 649 | struct device *dev = rsnd_priv_to_dev(priv); |
| 650 | |
Kuninori Morimoto | d3a7682 | 2014-11-09 20:00:58 -0800 | [diff] [blame] | 651 | /* |
| 652 | * fallback to PIO |
| 653 | * |
| 654 | * SSI .probe might be called again. |
| 655 | * see |
| 656 | * rsnd_rdai_continuance_probe() |
| 657 | */ |
| 658 | mod->ops = &rsnd_ssi_pio_ops; |
| 659 | |
| 660 | dev_info(dev, "%s[%d] fallback to PIO mode\n", |
| 661 | rsnd_mod_name(mod), rsnd_mod_id(mod)); |
| 662 | |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
Kuninori Morimoto | 9b99e9a | 2015-06-15 06:26:25 +0000 | [diff] [blame] | 666 | static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io, |
| 667 | struct rsnd_mod *mod) |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 668 | { |
Kuninori Morimoto | 72adc61 | 2015-02-20 10:31:23 +0000 | [diff] [blame] | 669 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
Kuninori Morimoto | 72adc61 | 2015-02-20 10:31:23 +0000 | [diff] [blame] | 670 | int is_play = rsnd_io_is_play(io); |
| 671 | char *name; |
| 672 | |
Kuninori Morimoto | b415b4d | 2015-10-22 03:15:46 +0000 | [diff] [blame] | 673 | if (rsnd_ssi_use_busif(io)) |
Kuninori Morimoto | 72adc61 | 2015-02-20 10:31:23 +0000 | [diff] [blame] | 674 | name = is_play ? "rxu" : "txu"; |
| 675 | else |
| 676 | name = is_play ? "rx" : "tx"; |
| 677 | |
| 678 | return rsnd_dma_request_channel(rsnd_ssi_of_node(priv), |
| 679 | mod, name); |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 682 | static struct rsnd_mod_ops rsnd_ssi_dma_ops = { |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 683 | .name = SSI_NAME, |
Kuninori Morimoto | 72adc61 | 2015-02-20 10:31:23 +0000 | [diff] [blame] | 684 | .dma_req = rsnd_ssi_dma_req, |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 685 | .probe = rsnd_ssi_dma_probe, |
| 686 | .remove = rsnd_ssi_dma_remove, |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 687 | .init = rsnd_ssi_init, |
| 688 | .quit = rsnd_ssi_quit, |
Kuninori Morimoto | 497deba | 2015-10-26 08:43:01 +0000 | [diff] [blame] | 689 | .start = rsnd_ssi_start, |
| 690 | .stop = rsnd_ssi_stop, |
Kuninori Morimoto | 97463e1 | 2014-11-27 08:02:43 +0000 | [diff] [blame] | 691 | .fallback = rsnd_ssi_fallback, |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 692 | .hw_params = rsnd_ssi_hw_params, |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 693 | }; |
| 694 | |
Kuninori Morimoto | 0579541 | 2014-11-27 08:05:01 +0000 | [diff] [blame] | 695 | int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod) |
| 696 | { |
| 697 | return mod->ops == &rsnd_ssi_dma_ops; |
| 698 | } |
| 699 | |
| 700 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 701 | /* |
| 702 | * Non SSI |
| 703 | */ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 704 | static struct rsnd_mod_ops rsnd_ssi_non_ops = { |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 705 | .name = SSI_NAME, |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 706 | }; |
| 707 | |
| 708 | /* |
| 709 | * ssi mod function |
| 710 | */ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 711 | struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id) |
| 712 | { |
Takashi Iwai | 8b14719 | 2013-11-05 18:40:05 +0100 | [diff] [blame] | 713 | if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv))) |
| 714 | id = 0; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 715 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 716 | return rsnd_mod_get(rsnd_ssi_get(priv, id)); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Kuninori Morimoto | b415b4d | 2015-10-22 03:15:46 +0000 | [diff] [blame] | 719 | int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod) |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 720 | { |
| 721 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 722 | |
| 723 | return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE); |
| 724 | } |
| 725 | |
Kuninori Morimoto | 2ea6b07 | 2015-11-10 05:14:12 +0000 | [diff] [blame^] | 726 | int rsnd_ssi_probe(struct rsnd_priv *priv) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 727 | { |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 728 | struct device_node *node; |
| 729 | struct device_node *np; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 730 | struct device *dev = rsnd_priv_to_dev(priv); |
| 731 | struct rsnd_mod_ops *ops; |
| 732 | struct clk *clk; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 733 | struct rsnd_ssi *ssi; |
| 734 | char name[RSND_SSI_NAME_SIZE]; |
Kuninori Morimoto | 2f78dd7 | 2015-03-26 04:02:09 +0000 | [diff] [blame] | 735 | int i, nr, ret; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 736 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 737 | node = rsnd_ssi_of_node(priv); |
| 738 | if (!node) |
| 739 | return -EINVAL; |
Kuninori Morimoto | 90e8e50 | 2014-03-17 19:29:55 -0700 | [diff] [blame] | 740 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 741 | nr = of_get_child_count(node); |
| 742 | if (!nr) { |
| 743 | ret = -EINVAL; |
| 744 | goto rsnd_ssi_probe_done; |
| 745 | } |
| 746 | |
Kuninori Morimoto | dd27d80 | 2014-01-23 18:39:40 -0800 | [diff] [blame] | 747 | ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL); |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 748 | if (!ssi) { |
| 749 | ret = -ENOMEM; |
| 750 | goto rsnd_ssi_probe_done; |
| 751 | } |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 752 | |
Kuninori Morimoto | dd27d80 | 2014-01-23 18:39:40 -0800 | [diff] [blame] | 753 | priv->ssi = ssi; |
| 754 | priv->ssi_nr = nr; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 755 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 756 | i = 0; |
| 757 | for_each_child_of_node(node, np) { |
| 758 | ssi = rsnd_ssi_get(priv, i); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 759 | |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 760 | snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d", |
| 761 | SSI_NAME, i); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 762 | |
Kuninori Morimoto | 60dbb4f | 2013-12-03 22:09:33 -0800 | [diff] [blame] | 763 | clk = devm_clk_get(dev, name); |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 764 | if (IS_ERR(clk)) { |
| 765 | ret = PTR_ERR(clk); |
| 766 | goto rsnd_ssi_probe_done; |
| 767 | } |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 768 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 769 | if (of_get_property(np, "shared-pin", NULL)) |
| 770 | ssi->flags |= RSND_SSI_CLK_PIN_SHARE; |
| 771 | |
| 772 | if (of_get_property(np, "no-busif", NULL)) |
| 773 | ssi->flags |= RSND_SSI_NO_BUSIF; |
| 774 | |
| 775 | ssi->irq = irq_of_parse_and_map(np, 0); |
| 776 | if (!ssi->irq) { |
| 777 | ret = -EINVAL; |
| 778 | goto rsnd_ssi_probe_done; |
| 779 | } |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 780 | |
| 781 | ops = &rsnd_ssi_non_ops; |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 782 | if (of_get_property(np, "pio-transfer", NULL)) |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 783 | ops = &rsnd_ssi_pio_ops; |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 784 | else |
| 785 | ops = &rsnd_ssi_dma_ops; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 786 | |
Kuninori Morimoto | b76e218 | 2015-09-10 07:02:21 +0000 | [diff] [blame] | 787 | ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk, |
| 788 | RSND_MOD_SSI, i); |
Kuninori Morimoto | 2f78dd7 | 2015-03-26 04:02:09 +0000 | [diff] [blame] | 789 | if (ret) |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 790 | goto rsnd_ssi_probe_done; |
| 791 | |
| 792 | i++; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 795 | ret = 0; |
| 796 | |
| 797 | rsnd_ssi_probe_done: |
| 798 | of_node_put(node); |
| 799 | |
| 800 | return ret; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 801 | } |
Kuninori Morimoto | 2f78dd7 | 2015-03-26 04:02:09 +0000 | [diff] [blame] | 802 | |
Kuninori Morimoto | 2ea6b07 | 2015-11-10 05:14:12 +0000 | [diff] [blame^] | 803 | void rsnd_ssi_remove(struct rsnd_priv *priv) |
Kuninori Morimoto | 2f78dd7 | 2015-03-26 04:02:09 +0000 | [diff] [blame] | 804 | { |
| 805 | struct rsnd_ssi *ssi; |
| 806 | int i; |
| 807 | |
| 808 | for_each_rsnd_ssi(ssi, priv, i) { |
Kuninori Morimoto | b76e218 | 2015-09-10 07:02:21 +0000 | [diff] [blame] | 809 | rsnd_mod_quit(rsnd_mod_get(ssi)); |
Kuninori Morimoto | 2f78dd7 | 2015-03-26 04:02:09 +0000 | [diff] [blame] | 810 | } |
| 811 | } |