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 | */ |
Kuninori Morimoto | 7fa72cc | 2017-05-18 01:28:22 +0000 | [diff] [blame] | 14 | #include <sound/simple_card_utils.h> |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 15 | #include <linux/delay.h> |
| 16 | #include "rsnd.h" |
| 17 | #define RSND_SSI_NAME_SIZE 16 |
| 18 | |
| 19 | /* |
| 20 | * SSICR |
| 21 | */ |
| 22 | #define FORCE (1 << 31) /* Fixed */ |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 23 | #define DMEN (1 << 28) /* DMA Enable */ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 24 | #define UIEN (1 << 27) /* Underflow Interrupt Enable */ |
| 25 | #define OIEN (1 << 26) /* Overflow Interrupt Enable */ |
| 26 | #define IIEN (1 << 25) /* Idle Mode Interrupt Enable */ |
| 27 | #define DIEN (1 << 24) /* Data Interrupt Enable */ |
Kuninori Morimoto | 186fadc | 2015-11-30 08:54:03 +0000 | [diff] [blame] | 28 | #define CHNL_4 (1 << 22) /* Channels */ |
| 29 | #define CHNL_6 (2 << 22) /* Channels */ |
| 30 | #define CHNL_8 (3 << 22) /* Channels */ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 31 | #define DWL_8 (0 << 19) /* Data Word Length */ |
| 32 | #define DWL_16 (1 << 19) /* Data Word Length */ |
| 33 | #define DWL_18 (2 << 19) /* Data Word Length */ |
| 34 | #define DWL_20 (3 << 19) /* Data Word Length */ |
| 35 | #define DWL_22 (4 << 19) /* Data Word Length */ |
| 36 | #define DWL_24 (5 << 19) /* Data Word Length */ |
| 37 | #define DWL_32 (6 << 19) /* Data Word Length */ |
| 38 | |
| 39 | #define SWL_32 (3 << 16) /* R/W System Word Length */ |
| 40 | #define SCKD (1 << 15) /* Serial Bit Clock Direction */ |
| 41 | #define SWSD (1 << 14) /* Serial WS Direction */ |
| 42 | #define SCKP (1 << 13) /* Serial Bit Clock Polarity */ |
| 43 | #define SWSP (1 << 12) /* Serial WS Polarity */ |
| 44 | #define SDTA (1 << 10) /* Serial Data Alignment */ |
Kuninori Morimoto | f46a93b | 2015-11-17 08:28:11 +0000 | [diff] [blame] | 45 | #define PDTA (1 << 9) /* Parallel Data Alignment */ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 46 | #define DEL (1 << 8) /* Serial Data Delay */ |
| 47 | #define CKDV(v) (v << 4) /* Serial Clock Division Ratio */ |
| 48 | #define TRMD (1 << 1) /* Transmit/Receive Mode Select */ |
| 49 | #define EN (1 << 0) /* SSI Module Enable */ |
| 50 | |
| 51 | /* |
| 52 | * SSISR |
| 53 | */ |
| 54 | #define UIRQ (1 << 27) /* Underflow Error Interrupt Status */ |
| 55 | #define OIRQ (1 << 26) /* Overflow Error Interrupt Status */ |
| 56 | #define IIRQ (1 << 25) /* Idle Mode Interrupt Status */ |
| 57 | #define DIRQ (1 << 24) /* Data Interrupt Status Flag */ |
| 58 | |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 59 | /* |
| 60 | * SSIWSR |
| 61 | */ |
| 62 | #define CONT (1 << 8) /* WS Continue Function */ |
Kuninori Morimoto | 186fadc | 2015-11-30 08:54:03 +0000 | [diff] [blame] | 63 | #define WS_MODE (1 << 0) /* WS Mode */ |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 64 | |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 65 | #define SSI_NAME "ssi" |
| 66 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 67 | struct rsnd_ssi { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 68 | struct rsnd_mod mod; |
Kuninori Morimoto | 940e947 | 2015-10-26 08:42:25 +0000 | [diff] [blame] | 69 | struct rsnd_mod *dma; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 70 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 71 | u32 flags; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 72 | u32 cr_own; |
| 73 | u32 cr_clk; |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 74 | u32 cr_mode; |
Kuninori Morimoto | 08bada2 | 2015-11-30 08:53:04 +0000 | [diff] [blame] | 75 | u32 wsr; |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 76 | int chan; |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 77 | int rate; |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 78 | int irq; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 79 | unsigned int usrcnt; |
Kuninori Morimoto | a97a06c | 2017-06-07 00:20:47 +0000 | [diff] [blame^] | 80 | |
| 81 | int byte_pos; |
| 82 | int period_pos; |
| 83 | int byte_per_period; |
| 84 | int next_period_byte; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 87 | /* flags */ |
| 88 | #define RSND_SSI_CLK_PIN_SHARE (1 << 0) |
| 89 | #define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */ |
Kuninori Morimoto | 7fa72cc | 2017-05-18 01:28:22 +0000 | [diff] [blame] | 90 | #define RSND_SSI_HDMI0 (1 << 2) /* for HDMI0 */ |
| 91 | #define RSND_SSI_HDMI1 (1 << 3) /* for HDMI1 */ |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 92 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 93 | #define for_each_rsnd_ssi(pos, priv, i) \ |
| 94 | for (i = 0; \ |
| 95 | (i < rsnd_ssi_nr(priv)) && \ |
Kuninori Morimoto | dd27d80 | 2014-01-23 18:39:40 -0800 | [diff] [blame] | 96 | ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \ |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 97 | i++) |
| 98 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 99 | #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id) |
Kuninori Morimoto | 232c00b | 2015-10-26 08:38:26 +0000 | [diff] [blame] | 100 | #define rsnd_ssi_to_dma(mod) ((ssi)->dma) |
Kuninori Morimoto | dd27d80 | 2014-01-23 18:39:40 -0800 | [diff] [blame] | 101 | #define rsnd_ssi_nr(priv) ((priv)->ssi_nr) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 102 | #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] | 103 | #define rsnd_ssi_mode_flags(p) ((p)->flags) |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 104 | #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io)) |
Kuninori Morimoto | c308abe | 2016-02-09 07:04:09 +0000 | [diff] [blame] | 105 | #define rsnd_ssi_is_multi_slave(mod, io) \ |
| 106 | (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod))) |
Kuninori Morimoto | fd9adcf | 2016-02-18 08:19:12 +0000 | [diff] [blame] | 107 | #define rsnd_ssi_is_run_mods(mod, io) \ |
| 108 | (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod))) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 109 | |
Kuninori Morimoto | 7fa72cc | 2017-05-18 01:28:22 +0000 | [diff] [blame] | 110 | int rsnd_ssi_hdmi_port(struct rsnd_dai_stream *io) |
| 111 | { |
| 112 | struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io); |
| 113 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 114 | |
| 115 | if (rsnd_ssi_mode_flags(ssi) & RSND_SSI_HDMI0) |
| 116 | return RSND_SSI_HDMI_PORT0; |
| 117 | |
| 118 | if (rsnd_ssi_mode_flags(ssi) & RSND_SSI_HDMI1) |
| 119 | return RSND_SSI_HDMI_PORT1; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Kuninori Morimoto | b415b4d | 2015-10-22 03:15:46 +0000 | [diff] [blame] | 124 | int rsnd_ssi_use_busif(struct rsnd_dai_stream *io) |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 125 | { |
Kuninori Morimoto | b415b4d | 2015-10-22 03:15:46 +0000 | [diff] [blame] | 126 | struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io); |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 127 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 128 | int use_busif = 0; |
| 129 | |
Kuninori Morimoto | 7b466fc | 2014-11-27 08:05:54 +0000 | [diff] [blame] | 130 | if (!rsnd_ssi_is_dma_mode(mod)) |
| 131 | return 0; |
| 132 | |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 133 | if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF)) |
| 134 | use_busif = 1; |
| 135 | if (rsnd_io_to_mod_src(io)) |
| 136 | use_busif = 1; |
| 137 | |
| 138 | return use_busif; |
| 139 | } |
| 140 | |
Kuninori Morimoto | e10369d | 2015-10-26 08:42:09 +0000 | [diff] [blame] | 141 | static void rsnd_ssi_status_clear(struct rsnd_mod *mod) |
| 142 | { |
| 143 | rsnd_mod_write(mod, SSISR, 0); |
| 144 | } |
| 145 | |
| 146 | static u32 rsnd_ssi_status_get(struct rsnd_mod *mod) |
| 147 | { |
| 148 | return rsnd_mod_read(mod, SSISR); |
| 149 | } |
| 150 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 151 | static void rsnd_ssi_status_check(struct rsnd_mod *mod, |
| 152 | u32 bit) |
| 153 | { |
| 154 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 155 | struct device *dev = rsnd_priv_to_dev(priv); |
| 156 | u32 status; |
| 157 | int i; |
| 158 | |
| 159 | for (i = 0; i < 1024; i++) { |
Kuninori Morimoto | e10369d | 2015-10-26 08:42:09 +0000 | [diff] [blame] | 160 | status = rsnd_ssi_status_get(mod); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 161 | if (status & bit) |
| 162 | return; |
| 163 | |
| 164 | udelay(50); |
| 165 | } |
| 166 | |
Kuninori Morimoto | 1120dbf | 2016-02-18 08:14:09 +0000 | [diff] [blame] | 167 | dev_warn(dev, "%s[%d] status check failed\n", |
| 168 | rsnd_mod_name(mod), rsnd_mod_id(mod)); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Kuninori Morimoto | 4f5c634 | 2016-02-18 08:18:54 +0000 | [diff] [blame] | 171 | static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io) |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 172 | { |
| 173 | struct rsnd_mod *mod; |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 174 | enum rsnd_mod_type types[] = { |
| 175 | RSND_MOD_SSIM1, |
| 176 | RSND_MOD_SSIM2, |
| 177 | RSND_MOD_SSIM3, |
| 178 | }; |
| 179 | int i, mask; |
| 180 | |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 181 | mask = 0; |
| 182 | for (i = 0; i < ARRAY_SIZE(types); i++) { |
| 183 | mod = rsnd_io_to_mod(io, types[i]); |
| 184 | if (!mod) |
| 185 | continue; |
| 186 | |
| 187 | mask |= 1 << rsnd_mod_id(mod); |
| 188 | } |
| 189 | |
| 190 | return mask; |
| 191 | } |
| 192 | |
Kuninori Morimoto | fd9adcf | 2016-02-18 08:19:12 +0000 | [diff] [blame] | 193 | static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io) |
| 194 | { |
| 195 | struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io); |
| 196 | struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io); |
| 197 | |
| 198 | return rsnd_ssi_multi_slaves_runtime(io) | |
| 199 | 1 << rsnd_mod_id(ssi_mod) | |
| 200 | 1 << rsnd_mod_id(ssi_parent_mod); |
| 201 | } |
| 202 | |
Kuninori Morimoto | 4f5c634 | 2016-02-18 08:18:54 +0000 | [diff] [blame] | 203 | u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io) |
| 204 | { |
Kuninori Morimoto | eed76bb | 2016-02-25 05:54:58 +0000 | [diff] [blame] | 205 | if (rsnd_runtime_is_ssi_multi(io)) |
| 206 | return rsnd_ssi_multi_slaves(io); |
Kuninori Morimoto | 4f5c634 | 2016-02-18 08:18:54 +0000 | [diff] [blame] | 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Kuninori Morimoto | 26d34b1 | 2016-02-18 08:14:37 +0000 | [diff] [blame] | 211 | static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod, |
Kuninori Morimoto | adcf7d5 | 2013-12-19 19:28:39 -0800 | [diff] [blame] | 212 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 213 | { |
Kuninori Morimoto | 1b13d118 | 2015-01-15 08:08:34 +0000 | [diff] [blame] | 214 | struct rsnd_priv *priv = rsnd_io_to_priv(io); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 215 | struct device *dev = rsnd_priv_to_dev(priv); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 216 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
Kuninori Morimoto | 26d34b1 | 2016-02-18 08:14:37 +0000 | [diff] [blame] | 217 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 218 | struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io); |
Kuninori Morimoto | eed76bb | 2016-02-25 05:54:58 +0000 | [diff] [blame] | 219 | int chan = rsnd_runtime_channel_for_ssi(io); |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 220 | int j, ret; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 221 | int ssi_clk_mul_table[] = { |
| 222 | 1, 2, 4, 8, 16, 6, 12, |
| 223 | }; |
| 224 | unsigned int main_rate; |
Kuninori Morimoto | cbf1494 | 2016-03-07 05:08:33 +0000 | [diff] [blame] | 225 | unsigned int rate = rsnd_io_is_play(io) ? |
| 226 | rsnd_src_get_out_rate(priv, io) : |
| 227 | rsnd_src_get_in_rate(priv, io); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 228 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 229 | if (!rsnd_rdai_is_clk_master(rdai)) |
| 230 | return 0; |
| 231 | |
| 232 | if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io)) |
| 233 | return 0; |
| 234 | |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 235 | if (rsnd_ssi_is_multi_slave(mod, io)) |
| 236 | return 0; |
| 237 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 238 | if (ssi->usrcnt > 1) { |
| 239 | if (ssi->rate != rate) { |
| 240 | dev_err(dev, "SSI parent/child should use same rate\n"); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 247 | /* |
| 248 | * Find best clock, and try to start ADG |
| 249 | */ |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 250 | for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 251 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 252 | /* |
Kuninori Morimoto | 6b8530c | 2017-03-22 04:02:43 +0000 | [diff] [blame] | 253 | * It will set SSIWSR.CONT here, but SSICR.CKDV = 000 |
| 254 | * with it is not allowed. (SSIWSR.WS_MODE with |
| 255 | * SSICR.CKDV = 000 is not allowed either). |
| 256 | * Skip it. See SSICR.CKDV |
| 257 | */ |
| 258 | if (j == 0) |
| 259 | continue; |
| 260 | |
| 261 | /* |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 262 | * this driver is assuming that |
Kuninori Morimoto | eed76bb | 2016-02-25 05:54:58 +0000 | [diff] [blame] | 263 | * system word is 32bit x chan |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 264 | * see rsnd_ssi_init() |
| 265 | */ |
Kuninori Morimoto | eed76bb | 2016-02-25 05:54:58 +0000 | [diff] [blame] | 266 | main_rate = rate * 32 * chan * ssi_clk_mul_table[j]; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 267 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 268 | ret = rsnd_adg_ssi_clk_try_start(mod, main_rate); |
| 269 | if (0 == ret) { |
| 270 | ssi->cr_clk = FORCE | SWL_32 | |
| 271 | SCKD | SWSD | CKDV(j); |
Kuninori Morimoto | 08bada2 | 2015-11-30 08:53:04 +0000 | [diff] [blame] | 272 | ssi->wsr = CONT; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 273 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 274 | ssi->rate = rate; |
| 275 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 276 | dev_dbg(dev, "%s[%d] outputs %u Hz\n", |
| 277 | rsnd_mod_name(mod), |
| 278 | rsnd_mod_id(mod), rate); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 279 | |
Kuninori Morimoto | eae6fff | 2015-09-10 07:03:48 +0000 | [diff] [blame] | 280 | return 0; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | dev_err(dev, "unsupported clock rate\n"); |
| 285 | return -EIO; |
| 286 | } |
| 287 | |
Kuninori Morimoto | 26d34b1 | 2016-02-18 08:14:37 +0000 | [diff] [blame] | 288 | static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod, |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 289 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 290 | { |
Kuninori Morimoto | f708d94 | 2015-01-15 08:07:19 +0000 | [diff] [blame] | 291 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
Kuninori Morimoto | 26d34b1 | 2016-02-18 08:14:37 +0000 | [diff] [blame] | 292 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 293 | struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 294 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 295 | if (!rsnd_rdai_is_clk_master(rdai)) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 296 | return; |
| 297 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 298 | if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io)) |
| 299 | return; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 300 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 301 | if (ssi->usrcnt > 1) |
| 302 | return; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 303 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 304 | ssi->cr_clk = 0; |
| 305 | ssi->rate = 0; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 306 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 307 | rsnd_adg_ssi_clk_stop(mod); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Kuninori Morimoto | 0dc6bf7 | 2016-02-18 08:17:18 +0000 | [diff] [blame] | 310 | static void rsnd_ssi_config_init(struct rsnd_mod *mod, |
Kuninori Morimoto | 840ada3 | 2015-11-30 08:52:38 +0000 | [diff] [blame] | 311 | struct rsnd_dai_stream *io) |
| 312 | { |
| 313 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
| 314 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
Kuninori Morimoto | 26d34b1 | 2016-02-18 08:14:37 +0000 | [diff] [blame] | 315 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | 840ada3 | 2015-11-30 08:52:38 +0000 | [diff] [blame] | 316 | u32 cr_own; |
| 317 | u32 cr_mode; |
Kuninori Morimoto | 186fadc | 2015-11-30 08:54:03 +0000 | [diff] [blame] | 318 | u32 wsr; |
Kuninori Morimoto | f98ed11 | 2015-12-02 07:34:28 +0000 | [diff] [blame] | 319 | int is_tdm; |
| 320 | |
Kuninori Morimoto | eed76bb | 2016-02-25 05:54:58 +0000 | [diff] [blame] | 321 | is_tdm = rsnd_runtime_is_ssi_tdm(io); |
Kuninori Morimoto | 840ada3 | 2015-11-30 08:52:38 +0000 | [diff] [blame] | 322 | |
| 323 | /* |
| 324 | * always use 32bit system word. |
| 325 | * see also rsnd_ssi_master_clk_enable() |
| 326 | */ |
Kuninori Morimoto | 90431eb | 2017-05-16 01:51:41 +0000 | [diff] [blame] | 327 | cr_own = FORCE | SWL_32; |
Kuninori Morimoto | 840ada3 | 2015-11-30 08:52:38 +0000 | [diff] [blame] | 328 | |
| 329 | if (rdai->bit_clk_inv) |
| 330 | cr_own |= SCKP; |
Kuninori Morimoto | f98ed11 | 2015-12-02 07:34:28 +0000 | [diff] [blame] | 331 | if (rdai->frm_clk_inv ^ is_tdm) |
Kuninori Morimoto | 840ada3 | 2015-11-30 08:52:38 +0000 | [diff] [blame] | 332 | cr_own |= SWSP; |
| 333 | if (rdai->data_alignment) |
| 334 | cr_own |= SDTA; |
| 335 | if (rdai->sys_delay) |
| 336 | cr_own |= DEL; |
| 337 | if (rsnd_io_is_play(io)) |
| 338 | cr_own |= TRMD; |
| 339 | |
| 340 | switch (runtime->sample_bits) { |
| 341 | case 16: |
| 342 | cr_own |= DWL_16; |
| 343 | break; |
| 344 | case 32: |
| 345 | cr_own |= DWL_24; |
| 346 | break; |
Kuninori Morimoto | 840ada3 | 2015-11-30 08:52:38 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Kuninori Morimoto | 26d34b1 | 2016-02-18 08:14:37 +0000 | [diff] [blame] | 349 | if (rsnd_ssi_is_dma_mode(mod)) { |
Kuninori Morimoto | 840ada3 | 2015-11-30 08:52:38 +0000 | [diff] [blame] | 350 | cr_mode = UIEN | OIEN | /* over/under run */ |
| 351 | DMEN; /* DMA : enable DMA */ |
| 352 | } else { |
| 353 | cr_mode = DIEN; /* PIO : enable Data interrupt */ |
| 354 | } |
| 355 | |
Kuninori Morimoto | 186fadc | 2015-11-30 08:54:03 +0000 | [diff] [blame] | 356 | /* |
| 357 | * TDM Extend Mode |
| 358 | * see |
| 359 | * rsnd_ssiu_init_gen2() |
| 360 | */ |
| 361 | wsr = ssi->wsr; |
Kuninori Morimoto | f98ed11 | 2015-12-02 07:34:28 +0000 | [diff] [blame] | 362 | if (is_tdm) { |
Kuninori Morimoto | 186fadc | 2015-11-30 08:54:03 +0000 | [diff] [blame] | 363 | wsr |= WS_MODE; |
| 364 | cr_own |= CHNL_8; |
| 365 | } |
| 366 | |
Kuninori Morimoto | 840ada3 | 2015-11-30 08:52:38 +0000 | [diff] [blame] | 367 | ssi->cr_own = cr_own; |
| 368 | ssi->cr_mode = cr_mode; |
Kuninori Morimoto | 186fadc | 2015-11-30 08:54:03 +0000 | [diff] [blame] | 369 | ssi->wsr = wsr; |
Kuninori Morimoto | 0dc6bf7 | 2016-02-18 08:17:18 +0000 | [diff] [blame] | 370 | } |
Kuninori Morimoto | 840ada3 | 2015-11-30 08:52:38 +0000 | [diff] [blame] | 371 | |
Kuninori Morimoto | 0dc6bf7 | 2016-02-18 08:17:18 +0000 | [diff] [blame] | 372 | static void rsnd_ssi_register_setup(struct rsnd_mod *mod) |
| 373 | { |
| 374 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 375 | |
| 376 | rsnd_mod_write(mod, SSIWSR, ssi->wsr); |
| 377 | rsnd_mod_write(mod, SSICR, ssi->cr_own | |
| 378 | ssi->cr_clk | |
| 379 | ssi->cr_mode); /* without EN */ |
Kuninori Morimoto | 840ada3 | 2015-11-30 08:52:38 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Kuninori Morimoto | a97a06c | 2017-06-07 00:20:47 +0000 | [diff] [blame^] | 382 | static void rsnd_ssi_pointer_init(struct rsnd_mod *mod, |
| 383 | struct rsnd_dai_stream *io) |
| 384 | { |
| 385 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 386 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 387 | |
| 388 | ssi->byte_pos = 0; |
| 389 | ssi->period_pos = 0; |
| 390 | ssi->byte_per_period = runtime->period_size * |
| 391 | runtime->channels * |
| 392 | samples_to_bytes(runtime, 1); |
| 393 | ssi->next_period_byte = ssi->byte_per_period; |
| 394 | } |
| 395 | |
| 396 | static int rsnd_ssi_pointer_offset(struct rsnd_mod *mod, |
| 397 | struct rsnd_dai_stream *io, |
| 398 | int additional) |
| 399 | { |
| 400 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 401 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 402 | int pos = ssi->byte_pos + additional; |
| 403 | |
| 404 | pos %= (runtime->periods * ssi->byte_per_period); |
| 405 | |
| 406 | return pos; |
| 407 | } |
| 408 | |
| 409 | static bool rsnd_ssi_pointer_update(struct rsnd_mod *mod, |
| 410 | struct rsnd_dai_stream *io, |
| 411 | int byte) |
| 412 | { |
| 413 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 414 | |
| 415 | ssi->byte_pos += byte; |
| 416 | |
| 417 | if (ssi->byte_pos >= ssi->next_period_byte) { |
| 418 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 419 | |
| 420 | ssi->period_pos++; |
| 421 | ssi->next_period_byte += ssi->byte_per_period; |
| 422 | |
| 423 | if (ssi->period_pos >= runtime->periods) { |
| 424 | ssi->byte_pos = 0; |
| 425 | ssi->period_pos = 0; |
| 426 | ssi->next_period_byte = ssi->byte_per_period; |
| 427 | } |
| 428 | |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | return false; |
| 433 | } |
| 434 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 435 | /* |
| 436 | * SSI mod common functions |
| 437 | */ |
| 438 | static int rsnd_ssi_init(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 439 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 440 | struct rsnd_priv *priv) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 441 | { |
| 442 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 443 | int ret; |
| 444 | |
Kuninori Morimoto | fd9adcf | 2016-02-18 08:19:12 +0000 | [diff] [blame] | 445 | if (!rsnd_ssi_is_run_mods(mod, io)) |
| 446 | return 0; |
| 447 | |
Kuninori Morimoto | a97a06c | 2017-06-07 00:20:47 +0000 | [diff] [blame^] | 448 | rsnd_ssi_pointer_init(mod, io); |
| 449 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 450 | ssi->usrcnt++; |
| 451 | |
| 452 | rsnd_mod_power_on(mod); |
| 453 | |
Kuninori Morimoto | 26d34b1 | 2016-02-18 08:14:37 +0000 | [diff] [blame] | 454 | ret = rsnd_ssi_master_clk_start(mod, io); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 455 | if (ret < 0) |
| 456 | return ret; |
| 457 | |
Kuninori Morimoto | 0dc6bf7 | 2016-02-18 08:17:18 +0000 | [diff] [blame] | 458 | if (!rsnd_ssi_is_parent(mod, io)) |
| 459 | rsnd_ssi_config_init(mod, io); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 460 | |
Kuninori Morimoto | 0dc6bf7 | 2016-02-18 08:17:18 +0000 | [diff] [blame] | 461 | rsnd_ssi_register_setup(mod); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 462 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 463 | /* clear error status */ |
| 464 | rsnd_ssi_status_clear(mod); |
| 465 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int rsnd_ssi_quit(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 470 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 471 | struct rsnd_priv *priv) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 472 | { |
| 473 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 474 | struct device *dev = rsnd_priv_to_dev(priv); |
| 475 | |
Kuninori Morimoto | fd9adcf | 2016-02-18 08:19:12 +0000 | [diff] [blame] | 476 | if (!rsnd_ssi_is_run_mods(mod, io)) |
| 477 | return 0; |
| 478 | |
Andrzej Hajda | e5d9cfc | 2015-12-24 08:02:39 +0100 | [diff] [blame] | 479 | if (!ssi->usrcnt) { |
| 480 | dev_err(dev, "%s[%d] usrcnt error\n", |
| 481 | rsnd_mod_name(mod), rsnd_mod_id(mod)); |
| 482 | return -EIO; |
| 483 | } |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 484 | |
Kuninori Morimoto | b5b442a | 2016-01-26 04:56:57 +0000 | [diff] [blame] | 485 | if (!rsnd_ssi_is_parent(mod, io)) |
Andrzej Hajda | e5d9cfc | 2015-12-24 08:02:39 +0100 | [diff] [blame] | 486 | ssi->cr_own = 0; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 487 | |
Kuninori Morimoto | 26d34b1 | 2016-02-18 08:14:37 +0000 | [diff] [blame] | 488 | rsnd_ssi_master_clk_stop(mod, io); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 489 | |
| 490 | rsnd_mod_power_off(mod); |
| 491 | |
| 492 | ssi->usrcnt--; |
| 493 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 497 | static int rsnd_ssi_hw_params(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 498 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 499 | struct snd_pcm_substream *substream, |
| 500 | struct snd_pcm_hw_params *params) |
| 501 | { |
| 502 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 503 | int chan = params_channels(params); |
| 504 | |
| 505 | /* |
Kuninori Morimoto | 6bf66b1 | 2016-12-07 02:05:22 +0000 | [diff] [blame] | 506 | * snd_pcm_ops::hw_params will be called *before* |
| 507 | * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0 |
| 508 | * in 1st call. |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 509 | */ |
Kuninori Morimoto | 6bf66b1 | 2016-12-07 02:05:22 +0000 | [diff] [blame] | 510 | if (ssi->usrcnt) { |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 511 | /* |
Kuninori Morimoto | 6bf66b1 | 2016-12-07 02:05:22 +0000 | [diff] [blame] | 512 | * Already working. |
| 513 | * It will happen if SSI has parent/child connection. |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 514 | * it is error if child <-> parent SSI uses |
| 515 | * different channels. |
| 516 | */ |
| 517 | if (ssi->chan != chan) |
| 518 | return -EIO; |
| 519 | } |
| 520 | |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 521 | ssi->chan = chan; |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
Kuninori Morimoto | 6a25c8d | 2016-01-26 04:56:14 +0000 | [diff] [blame] | 526 | static int rsnd_ssi_start(struct rsnd_mod *mod, |
| 527 | struct rsnd_dai_stream *io, |
| 528 | struct rsnd_priv *priv) |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 529 | { |
Kuninori Morimoto | fd9adcf | 2016-02-18 08:19:12 +0000 | [diff] [blame] | 530 | if (!rsnd_ssi_is_run_mods(mod, io)) |
| 531 | return 0; |
| 532 | |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 533 | /* |
| 534 | * EN will be set via SSIU :: SSI_CONTROL |
| 535 | * if Multi channel mode |
| 536 | */ |
Kuninori Morimoto | 4f5c634 | 2016-02-18 08:18:54 +0000 | [diff] [blame] | 537 | if (rsnd_ssi_multi_slaves_runtime(io)) |
Kuninori Morimoto | 0dc6bf7 | 2016-02-18 08:17:18 +0000 | [diff] [blame] | 538 | return 0; |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 539 | |
Kuninori Morimoto | 0dc6bf7 | 2016-02-18 08:17:18 +0000 | [diff] [blame] | 540 | rsnd_mod_bset(mod, SSICR, EN, EN); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
Kuninori Morimoto | 6a25c8d | 2016-01-26 04:56:14 +0000 | [diff] [blame] | 545 | static int rsnd_ssi_stop(struct rsnd_mod *mod, |
| 546 | struct rsnd_dai_stream *io, |
| 547 | struct rsnd_priv *priv) |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 548 | { |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 549 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 550 | u32 cr; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 551 | |
Kuninori Morimoto | fd9adcf | 2016-02-18 08:19:12 +0000 | [diff] [blame] | 552 | if (!rsnd_ssi_is_run_mods(mod, io)) |
| 553 | return 0; |
| 554 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 555 | /* |
Kuninori Morimoto | 6a25c8d | 2016-01-26 04:56:14 +0000 | [diff] [blame] | 556 | * don't stop if not last user |
| 557 | * see also |
| 558 | * rsnd_ssi_start |
| 559 | * rsnd_ssi_interrupt |
| 560 | */ |
| 561 | if (ssi->usrcnt > 1) |
| 562 | return 0; |
| 563 | |
| 564 | /* |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 565 | * disable all IRQ, |
| 566 | * and, wait all data was sent |
| 567 | */ |
| 568 | cr = ssi->cr_own | |
| 569 | ssi->cr_clk; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 570 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 571 | rsnd_mod_write(mod, SSICR, cr | EN); |
| 572 | rsnd_ssi_status_check(mod, DIRQ); |
| 573 | |
| 574 | /* |
| 575 | * disable SSI, |
| 576 | * and, wait idle state |
| 577 | */ |
| 578 | rsnd_mod_write(mod, SSICR, cr); /* disabled all */ |
| 579 | rsnd_ssi_status_check(mod, IIRQ); |
Kuninori Morimoto | c17dba8 | 2014-11-27 08:05:09 +0000 | [diff] [blame] | 580 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 581 | return 0; |
| 582 | } |
| 583 | |
Kuninori Morimoto | 615fb6c | 2016-02-18 08:18:16 +0000 | [diff] [blame] | 584 | static int rsnd_ssi_irq(struct rsnd_mod *mod, |
| 585 | struct rsnd_dai_stream *io, |
| 586 | struct rsnd_priv *priv, |
| 587 | int enable) |
| 588 | { |
| 589 | u32 val = 0; |
| 590 | |
| 591 | if (rsnd_is_gen1(priv)) |
| 592 | return 0; |
| 593 | |
| 594 | if (rsnd_ssi_is_parent(mod, io)) |
| 595 | return 0; |
| 596 | |
Kuninori Morimoto | fd9adcf | 2016-02-18 08:19:12 +0000 | [diff] [blame] | 597 | if (!rsnd_ssi_is_run_mods(mod, io)) |
| 598 | return 0; |
| 599 | |
Kuninori Morimoto | 615fb6c | 2016-02-18 08:18:16 +0000 | [diff] [blame] | 600 | if (enable) |
| 601 | val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000; |
| 602 | |
| 603 | rsnd_mod_write(mod, SSI_INT_ENABLE, val); |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
Kuninori Morimoto | bfc0cfe | 2015-06-15 06:26:56 +0000 | [diff] [blame] | 608 | static void __rsnd_ssi_interrupt(struct rsnd_mod *mod, |
| 609 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 610 | { |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 611 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
Kuninori Morimoto | 765ae7c | 2015-01-15 08:09:13 +0000 | [diff] [blame] | 612 | int is_dma = rsnd_ssi_is_dma_mode(mod); |
Kuninori Morimoto | 02299d9 | 2015-05-21 03:50:23 +0000 | [diff] [blame] | 613 | u32 status; |
Kuninori Morimoto | 75defee | 2015-06-15 06:21:15 +0000 | [diff] [blame] | 614 | bool elapsed = false; |
Kuninori Morimoto | 6a25c8d | 2016-01-26 04:56:14 +0000 | [diff] [blame] | 615 | bool stop = false; |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 616 | |
Kuninori Morimoto | 02299d9 | 2015-05-21 03:50:23 +0000 | [diff] [blame] | 617 | spin_lock(&priv->lock); |
| 618 | |
| 619 | /* ignore all cases if not working */ |
Kuninori Morimoto | d5bbe7d | 2015-06-15 06:27:47 +0000 | [diff] [blame] | 620 | if (!rsnd_io_is_working(io)) |
Kuninori Morimoto | 02299d9 | 2015-05-21 03:50:23 +0000 | [diff] [blame] | 621 | goto rsnd_ssi_interrupt_out; |
| 622 | |
Kuninori Morimoto | 6a25c8d | 2016-01-26 04:56:14 +0000 | [diff] [blame] | 623 | status = rsnd_ssi_status_get(mod); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 624 | |
| 625 | /* PIO only */ |
Kuninori Morimoto | 765ae7c | 2015-01-15 08:09:13 +0000 | [diff] [blame] | 626 | if (!is_dma && (status & DIRQ)) { |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 627 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 628 | u32 *buf = (u32 *)(runtime->dma_area + |
Kuninori Morimoto | a97a06c | 2017-06-07 00:20:47 +0000 | [diff] [blame^] | 629 | rsnd_ssi_pointer_offset(mod, io, 0)); |
Kuninori Morimoto | 7819a94 | 2017-05-24 01:17:10 +0000 | [diff] [blame] | 630 | int shift = 0; |
| 631 | |
| 632 | switch (runtime->sample_bits) { |
| 633 | case 32: |
| 634 | shift = 8; |
| 635 | break; |
| 636 | } |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * 8/16/32 data can be assesse to TDR/RDR register |
| 640 | * directly as 32bit data |
| 641 | * see rsnd_ssi_init() |
| 642 | */ |
Kuninori Morimoto | 985a4f6 | 2015-01-15 08:06:49 +0000 | [diff] [blame] | 643 | if (rsnd_io_is_play(io)) |
Kuninori Morimoto | 7819a94 | 2017-05-24 01:17:10 +0000 | [diff] [blame] | 644 | rsnd_mod_write(mod, SSITDR, (*buf) << shift); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 645 | else |
Kuninori Morimoto | 7819a94 | 2017-05-24 01:17:10 +0000 | [diff] [blame] | 646 | *buf = (rsnd_mod_read(mod, SSIRDR) >> shift); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 647 | |
Kuninori Morimoto | a97a06c | 2017-06-07 00:20:47 +0000 | [diff] [blame^] | 648 | elapsed = rsnd_ssi_pointer_update(mod, io, sizeof(*buf)); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Kuninori Morimoto | 12927a8 | 2015-06-15 06:20:54 +0000 | [diff] [blame] | 651 | /* DMA only */ |
Kuninori Morimoto | 6a25c8d | 2016-01-26 04:56:14 +0000 | [diff] [blame] | 652 | if (is_dma && (status & (UIRQ | OIRQ))) |
| 653 | stop = true; |
Kuninori Morimoto | 69e32a5 | 2015-10-26 08:41:36 +0000 | [diff] [blame] | 654 | |
Kuninori Morimoto | 5342dff | 2015-11-26 11:13:40 +0000 | [diff] [blame] | 655 | rsnd_ssi_status_clear(mod); |
Kuninori Morimoto | 02299d9 | 2015-05-21 03:50:23 +0000 | [diff] [blame] | 656 | rsnd_ssi_interrupt_out: |
| 657 | spin_unlock(&priv->lock); |
| 658 | |
Kuninori Morimoto | 75defee | 2015-06-15 06:21:15 +0000 | [diff] [blame] | 659 | if (elapsed) |
| 660 | rsnd_dai_period_elapsed(io); |
Kuninori Morimoto | 6a25c8d | 2016-01-26 04:56:14 +0000 | [diff] [blame] | 661 | |
| 662 | if (stop) |
| 663 | snd_pcm_stop_xrun(io->substream); |
| 664 | |
Kuninori Morimoto | bfc0cfe | 2015-06-15 06:26:56 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static irqreturn_t rsnd_ssi_interrupt(int irq, void *data) |
| 668 | { |
| 669 | struct rsnd_mod *mod = data; |
| 670 | |
| 671 | rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt); |
Kuninori Morimoto | 75defee | 2015-06-15 06:21:15 +0000 | [diff] [blame] | 672 | |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 673 | return IRQ_HANDLED; |
| 674 | } |
| 675 | |
Kuninori Morimoto | 6cfad78 | 2014-11-27 08:08:10 +0000 | [diff] [blame] | 676 | /* |
| 677 | * SSI PIO |
| 678 | */ |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 679 | static void rsnd_ssi_parent_attach(struct rsnd_mod *mod, |
Kuninori Morimoto | 098bd89 | 2016-02-18 08:17:52 +0000 | [diff] [blame] | 680 | struct rsnd_dai_stream *io) |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 681 | { |
Kuninori Morimoto | 098bd89 | 2016-02-18 08:17:52 +0000 | [diff] [blame] | 682 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
| 683 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
| 684 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 685 | if (!__rsnd_ssi_is_pin_sharing(mod)) |
| 686 | return; |
| 687 | |
Kuninori Morimoto | 098bd89 | 2016-02-18 08:17:52 +0000 | [diff] [blame] | 688 | if (!rsnd_rdai_is_clk_master(rdai)) |
| 689 | return; |
| 690 | |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 691 | switch (rsnd_mod_id(mod)) { |
| 692 | case 1: |
| 693 | case 2: |
| 694 | rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP); |
| 695 | break; |
| 696 | case 4: |
| 697 | rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP); |
| 698 | break; |
| 699 | case 8: |
| 700 | rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP); |
| 701 | break; |
| 702 | } |
| 703 | } |
| 704 | |
Kuninori Morimoto | 098bd89 | 2016-02-18 08:17:52 +0000 | [diff] [blame] | 705 | static int rsnd_ssi_pcm_new(struct rsnd_mod *mod, |
| 706 | struct rsnd_dai_stream *io, |
| 707 | struct snd_soc_pcm_runtime *rtd) |
| 708 | { |
| 709 | /* |
| 710 | * rsnd_rdai_is_clk_master() will be enabled after set_fmt, |
| 711 | * and, pcm_new will be called after it. |
| 712 | * This function reuse pcm_new at this point. |
| 713 | */ |
| 714 | rsnd_ssi_parent_attach(mod, io); |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 719 | static int rsnd_ssi_common_probe(struct rsnd_mod *mod, |
| 720 | struct rsnd_dai_stream *io, |
| 721 | struct rsnd_priv *priv) |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 722 | { |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 723 | struct device *dev = rsnd_priv_to_dev(priv); |
| 724 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 725 | int ret; |
| 726 | |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 727 | /* |
| 728 | * SSIP/SSIU/IRQ are not needed on |
| 729 | * SSI Multi slaves |
| 730 | */ |
| 731 | if (rsnd_ssi_is_multi_slave(mod, io)) |
| 732 | return 0; |
| 733 | |
Kuninori Morimoto | 098bd89 | 2016-02-18 08:17:52 +0000 | [diff] [blame] | 734 | /* |
| 735 | * It can't judge ssi parent at this point |
| 736 | * see rsnd_ssi_pcm_new() |
| 737 | */ |
Kuninori Morimoto | e7d850d | 2015-10-26 08:43:57 +0000 | [diff] [blame] | 738 | |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 739 | ret = rsnd_ssiu_attach(io, mod); |
| 740 | if (ret < 0) |
| 741 | return ret; |
| 742 | |
Kuninori Morimoto | 701172dc | 2016-10-25 00:36:34 +0000 | [diff] [blame] | 743 | /* |
| 744 | * SSI might be called again as PIO fallback |
| 745 | * It is easy to manual handling for IRQ request/free |
| 746 | */ |
| 747 | ret = request_irq(ssi->irq, |
| 748 | rsnd_ssi_interrupt, |
| 749 | IRQF_SHARED, |
| 750 | dev_name(dev), mod); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 751 | |
| 752 | return ret; |
| 753 | } |
| 754 | |
Kuninori Morimoto | 07b7acb | 2017-06-07 00:20:01 +0000 | [diff] [blame] | 755 | static int rsnd_ssi_pointer(struct rsnd_mod *mod, |
| 756 | struct rsnd_dai_stream *io, |
| 757 | snd_pcm_uframes_t *pointer) |
| 758 | { |
Kuninori Morimoto | a97a06c | 2017-06-07 00:20:47 +0000 | [diff] [blame^] | 759 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | 07b7acb | 2017-06-07 00:20:01 +0000 | [diff] [blame] | 760 | struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io); |
| 761 | |
Kuninori Morimoto | a97a06c | 2017-06-07 00:20:47 +0000 | [diff] [blame^] | 762 | *pointer = bytes_to_frames(runtime, ssi->byte_pos); |
Kuninori Morimoto | 07b7acb | 2017-06-07 00:20:01 +0000 | [diff] [blame] | 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 767 | static struct rsnd_mod_ops rsnd_ssi_pio_ops = { |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 768 | .name = SSI_NAME, |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 769 | .probe = rsnd_ssi_common_probe, |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 770 | .init = rsnd_ssi_init, |
| 771 | .quit = rsnd_ssi_quit, |
Kuninori Morimoto | 4922985 | 2014-11-27 08:07:17 +0000 | [diff] [blame] | 772 | .start = rsnd_ssi_start, |
| 773 | .stop = rsnd_ssi_stop, |
Kuninori Morimoto | b5b442a | 2016-01-26 04:56:57 +0000 | [diff] [blame] | 774 | .irq = rsnd_ssi_irq, |
Kuninori Morimoto | 07b7acb | 2017-06-07 00:20:01 +0000 | [diff] [blame] | 775 | .pointer= rsnd_ssi_pointer, |
Kuninori Morimoto | 098bd89 | 2016-02-18 08:17:52 +0000 | [diff] [blame] | 776 | .pcm_new = rsnd_ssi_pcm_new, |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 777 | .hw_params = rsnd_ssi_hw_params, |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 778 | }; |
| 779 | |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 780 | static int rsnd_ssi_dma_probe(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 781 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 782 | struct rsnd_priv *priv) |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 783 | { |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 784 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 785 | int ret; |
| 786 | |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 787 | /* |
| 788 | * SSIP/SSIU/IRQ/DMA are not needed on |
| 789 | * SSI Multi slaves |
| 790 | */ |
| 791 | if (rsnd_ssi_is_multi_slave(mod, io)) |
| 792 | return 0; |
| 793 | |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 794 | ret = rsnd_ssi_common_probe(mod, io, priv); |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 795 | if (ret) |
Kuninori Morimoto | b543b52 | 2015-03-26 04:02:32 +0000 | [diff] [blame] | 796 | return ret; |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 797 | |
Kuninori Morimoto | 355cb84fb | 2016-01-21 01:58:33 +0000 | [diff] [blame] | 798 | /* SSI probe might be called many times in MUX multi path */ |
Kuninori Morimoto | b99305d | 2016-10-25 00:36:13 +0000 | [diff] [blame] | 799 | ret = rsnd_dma_attach(io, mod, &ssi->dma); |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 800 | |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 801 | return ret; |
| 802 | } |
| 803 | |
| 804 | static int rsnd_ssi_dma_remove(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 805 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 806 | struct rsnd_priv *priv) |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 807 | { |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 808 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
Kuninori Morimoto | 1f8754d | 2017-05-16 01:48:24 +0000 | [diff] [blame] | 809 | struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io); |
| 810 | |
| 811 | /* Do nothing for SSI parent mod */ |
| 812 | if (ssi_parent_mod == mod) |
| 813 | return 0; |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 814 | |
Kuninori Morimoto | 4e7d606 | 2014-11-27 08:07:47 +0000 | [diff] [blame] | 815 | /* PIO will request IRQ again */ |
Kuninori Morimoto | 701172dc | 2016-10-25 00:36:34 +0000 | [diff] [blame] | 816 | free_irq(ssi->irq, mod); |
Kuninori Morimoto | 0af5c01 | 2016-10-19 03:56:26 +0000 | [diff] [blame] | 817 | |
Kuninori Morimoto | 97463e1 | 2014-11-27 08:02:43 +0000 | [diff] [blame] | 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | static int rsnd_ssi_fallback(struct rsnd_mod *mod, |
Kuninori Morimoto | 2c0fac1 | 2015-06-15 06:25:20 +0000 | [diff] [blame] | 822 | struct rsnd_dai_stream *io, |
Kuninori Morimoto | 690602f | 2015-01-15 08:07:47 +0000 | [diff] [blame] | 823 | struct rsnd_priv *priv) |
Kuninori Morimoto | 97463e1 | 2014-11-27 08:02:43 +0000 | [diff] [blame] | 824 | { |
Kuninori Morimoto | d3a7682 | 2014-11-09 20:00:58 -0800 | [diff] [blame] | 825 | struct device *dev = rsnd_priv_to_dev(priv); |
| 826 | |
Kuninori Morimoto | d3a7682 | 2014-11-09 20:00:58 -0800 | [diff] [blame] | 827 | /* |
| 828 | * fallback to PIO |
| 829 | * |
| 830 | * SSI .probe might be called again. |
| 831 | * see |
| 832 | * rsnd_rdai_continuance_probe() |
| 833 | */ |
| 834 | mod->ops = &rsnd_ssi_pio_ops; |
| 835 | |
| 836 | dev_info(dev, "%s[%d] fallback to PIO mode\n", |
| 837 | rsnd_mod_name(mod), rsnd_mod_id(mod)); |
| 838 | |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 839 | return 0; |
| 840 | } |
| 841 | |
Kuninori Morimoto | 9b99e9a | 2015-06-15 06:26:25 +0000 | [diff] [blame] | 842 | static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io, |
| 843 | struct rsnd_mod *mod) |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 844 | { |
Kuninori Morimoto | 72adc61 | 2015-02-20 10:31:23 +0000 | [diff] [blame] | 845 | struct rsnd_priv *priv = rsnd_mod_to_priv(mod); |
Kuninori Morimoto | 72adc61 | 2015-02-20 10:31:23 +0000 | [diff] [blame] | 846 | int is_play = rsnd_io_is_play(io); |
| 847 | char *name; |
| 848 | |
Kuninori Morimoto | b415b4d | 2015-10-22 03:15:46 +0000 | [diff] [blame] | 849 | if (rsnd_ssi_use_busif(io)) |
Kuninori Morimoto | 72adc61 | 2015-02-20 10:31:23 +0000 | [diff] [blame] | 850 | name = is_play ? "rxu" : "txu"; |
| 851 | else |
| 852 | name = is_play ? "rx" : "tx"; |
| 853 | |
| 854 | return rsnd_dma_request_channel(rsnd_ssi_of_node(priv), |
| 855 | mod, name); |
Kuninori Morimoto | d9288d0 | 2014-06-22 17:56:23 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 858 | static struct rsnd_mod_ops rsnd_ssi_dma_ops = { |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 859 | .name = SSI_NAME, |
Kuninori Morimoto | 72adc61 | 2015-02-20 10:31:23 +0000 | [diff] [blame] | 860 | .dma_req = rsnd_ssi_dma_req, |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 861 | .probe = rsnd_ssi_dma_probe, |
| 862 | .remove = rsnd_ssi_dma_remove, |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 863 | .init = rsnd_ssi_init, |
| 864 | .quit = rsnd_ssi_quit, |
Kuninori Morimoto | 497deba | 2015-10-26 08:43:01 +0000 | [diff] [blame] | 865 | .start = rsnd_ssi_start, |
| 866 | .stop = rsnd_ssi_stop, |
Kuninori Morimoto | c8e969a | 2016-02-18 08:16:43 +0000 | [diff] [blame] | 867 | .irq = rsnd_ssi_irq, |
Kuninori Morimoto | 098bd89 | 2016-02-18 08:17:52 +0000 | [diff] [blame] | 868 | .pcm_new = rsnd_ssi_pcm_new, |
Kuninori Morimoto | 97463e1 | 2014-11-27 08:02:43 +0000 | [diff] [blame] | 869 | .fallback = rsnd_ssi_fallback, |
Kuninori Morimoto | 919567d | 2015-04-10 08:50:30 +0000 | [diff] [blame] | 870 | .hw_params = rsnd_ssi_hw_params, |
Kuninori Morimoto | 849fc82 | 2013-07-28 18:59:02 -0700 | [diff] [blame] | 871 | }; |
| 872 | |
Kuninori Morimoto | 0579541 | 2014-11-27 08:05:01 +0000 | [diff] [blame] | 873 | int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod) |
| 874 | { |
| 875 | return mod->ops == &rsnd_ssi_dma_ops; |
| 876 | } |
| 877 | |
| 878 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 879 | /* |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 880 | * ssi mod function |
| 881 | */ |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 882 | static void rsnd_ssi_connect(struct rsnd_mod *mod, |
| 883 | struct rsnd_dai_stream *io) |
| 884 | { |
| 885 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
| 886 | enum rsnd_mod_type types[] = { |
| 887 | RSND_MOD_SSI, |
| 888 | RSND_MOD_SSIM1, |
| 889 | RSND_MOD_SSIM2, |
| 890 | RSND_MOD_SSIM3, |
| 891 | }; |
| 892 | enum rsnd_mod_type type; |
| 893 | int i; |
| 894 | |
| 895 | /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */ |
| 896 | for (i = 0; i < ARRAY_SIZE(types); i++) { |
| 897 | type = types[i]; |
| 898 | if (!rsnd_io_to_mod(io, type)) { |
| 899 | rsnd_dai_connect(mod, io, type); |
| 900 | rsnd_set_slot(rdai, 2 * (i + 1), (i + 1)); |
| 901 | return; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | void rsnd_parse_connect_ssi(struct rsnd_dai *rdai, |
| 907 | struct device_node *playback, |
| 908 | struct device_node *capture) |
| 909 | { |
| 910 | struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai); |
| 911 | struct device_node *node; |
| 912 | struct device_node *np; |
| 913 | struct rsnd_mod *mod; |
| 914 | int i; |
| 915 | |
| 916 | node = rsnd_ssi_of_node(priv); |
| 917 | if (!node) |
| 918 | return; |
| 919 | |
| 920 | i = 0; |
| 921 | for_each_child_of_node(node, np) { |
| 922 | mod = rsnd_ssi_mod_get(priv, i); |
| 923 | if (np == playback) |
| 924 | rsnd_ssi_connect(mod, &rdai->playback); |
| 925 | if (np == capture) |
| 926 | rsnd_ssi_connect(mod, &rdai->capture); |
| 927 | i++; |
| 928 | } |
| 929 | |
| 930 | of_node_put(node); |
| 931 | } |
| 932 | |
Kuninori Morimoto | 7fa72cc | 2017-05-18 01:28:22 +0000 | [diff] [blame] | 933 | static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv, |
| 934 | struct rsnd_dai_stream *io, |
| 935 | struct device_node *remote_ep) |
| 936 | { |
| 937 | struct device *dev = rsnd_priv_to_dev(priv); |
| 938 | struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io); |
| 939 | struct rsnd_ssi *ssi; |
| 940 | |
| 941 | if (!mod) |
| 942 | return; |
| 943 | |
| 944 | ssi = rsnd_mod_to_ssi(mod); |
| 945 | |
| 946 | if (strstr(remote_ep->full_name, "hdmi0")) { |
| 947 | ssi->flags |= RSND_SSI_HDMI0; |
| 948 | dev_dbg(dev, "%s[%d] connected to HDMI0\n", |
| 949 | rsnd_mod_name(mod), rsnd_mod_id(mod)); |
| 950 | } |
| 951 | |
| 952 | if (strstr(remote_ep->full_name, "hdmi1")) { |
| 953 | ssi->flags |= RSND_SSI_HDMI1; |
| 954 | dev_dbg(dev, "%s[%d] connected to HDMI1\n", |
| 955 | rsnd_mod_name(mod), rsnd_mod_id(mod)); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv, |
| 960 | struct device_node *endpoint, |
| 961 | int dai_i) |
| 962 | { |
| 963 | struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i); |
| 964 | struct device_node *remote_ep; |
| 965 | |
| 966 | remote_ep = of_graph_get_remote_endpoint(endpoint); |
| 967 | if (!remote_ep) |
| 968 | return; |
| 969 | |
| 970 | __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep); |
| 971 | __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep); |
| 972 | } |
| 973 | |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 974 | struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id) |
| 975 | { |
Takashi Iwai | 8b14719 | 2013-11-05 18:40:05 +0100 | [diff] [blame] | 976 | if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv))) |
| 977 | id = 0; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 978 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 979 | return rsnd_mod_get(rsnd_ssi_get(priv, id)); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Kuninori Morimoto | b415b4d | 2015-10-22 03:15:46 +0000 | [diff] [blame] | 982 | int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod) |
Kuninori Morimoto | 7b5ce97 | 2014-01-23 18:39:32 -0800 | [diff] [blame] | 983 | { |
| 984 | struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod); |
| 985 | |
| 986 | return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE); |
| 987 | } |
| 988 | |
Kuninori Morimoto | 5ba17b42 | 2016-01-21 01:58:07 +0000 | [diff] [blame] | 989 | static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io, |
| 990 | struct rsnd_mod *mod, |
| 991 | enum rsnd_mod_type type) |
| 992 | { |
| 993 | /* |
| 994 | * SSIP (= SSI parent) needs to be special, otherwise, |
| 995 | * 2nd SSI might doesn't start. see also rsnd_mod_call() |
| 996 | * |
| 997 | * We can't include parent SSI status on SSI, because we don't know |
| 998 | * how many SSI requests parent SSI. Thus, it is localed on "io" now. |
| 999 | * ex) trouble case |
| 1000 | * Playback: SSI0 |
| 1001 | * Capture : SSI1 (needs SSI0) |
| 1002 | * |
| 1003 | * 1) start Capture -> SSI0/SSI1 are started. |
| 1004 | * 2) start Playback -> SSI0 doesn't work, because it is already |
| 1005 | * marked as "started" on 1) |
| 1006 | * |
| 1007 | * OTOH, using each mod's status is good for MUX case. |
| 1008 | * It doesn't need to start in 2nd start |
| 1009 | * ex) |
| 1010 | * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0 |
| 1011 | * | |
| 1012 | * IO-1: SRC1 -> CTU2 -+ |
| 1013 | * |
| 1014 | * 1) start IO-0 -> start SSI0 |
| 1015 | * 2) start IO-1 -> SSI0 doesn't need to start, because it is |
| 1016 | * already started on 1) |
| 1017 | */ |
| 1018 | if (type == RSND_MOD_SSIP) |
| 1019 | return &io->parent_ssi_status; |
| 1020 | |
| 1021 | return rsnd_mod_get_status(io, mod, type); |
| 1022 | } |
| 1023 | |
Kuninori Morimoto | 2ea6b07 | 2015-11-10 05:14:12 +0000 | [diff] [blame] | 1024 | int rsnd_ssi_probe(struct rsnd_priv *priv) |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1025 | { |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 1026 | struct device_node *node; |
| 1027 | struct device_node *np; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1028 | struct device *dev = rsnd_priv_to_dev(priv); |
| 1029 | struct rsnd_mod_ops *ops; |
| 1030 | struct clk *clk; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1031 | struct rsnd_ssi *ssi; |
| 1032 | char name[RSND_SSI_NAME_SIZE]; |
Kuninori Morimoto | 2f78dd7 | 2015-03-26 04:02:09 +0000 | [diff] [blame] | 1033 | int i, nr, ret; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1034 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 1035 | node = rsnd_ssi_of_node(priv); |
| 1036 | if (!node) |
| 1037 | return -EINVAL; |
Kuninori Morimoto | 90e8e50 | 2014-03-17 19:29:55 -0700 | [diff] [blame] | 1038 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 1039 | nr = of_get_child_count(node); |
| 1040 | if (!nr) { |
| 1041 | ret = -EINVAL; |
| 1042 | goto rsnd_ssi_probe_done; |
| 1043 | } |
| 1044 | |
Kuninori Morimoto | dd27d80 | 2014-01-23 18:39:40 -0800 | [diff] [blame] | 1045 | ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL); |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 1046 | if (!ssi) { |
| 1047 | ret = -ENOMEM; |
| 1048 | goto rsnd_ssi_probe_done; |
| 1049 | } |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1050 | |
Kuninori Morimoto | dd27d80 | 2014-01-23 18:39:40 -0800 | [diff] [blame] | 1051 | priv->ssi = ssi; |
| 1052 | priv->ssi_nr = nr; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1053 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 1054 | i = 0; |
| 1055 | for_each_child_of_node(node, np) { |
| 1056 | ssi = rsnd_ssi_get(priv, i); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1057 | |
Kuninori Morimoto | 8aefda5 | 2014-05-22 23:25:43 -0700 | [diff] [blame] | 1058 | snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d", |
| 1059 | SSI_NAME, i); |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1060 | |
Kuninori Morimoto | 60dbb4f | 2013-12-03 22:09:33 -0800 | [diff] [blame] | 1061 | clk = devm_clk_get(dev, name); |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 1062 | if (IS_ERR(clk)) { |
| 1063 | ret = PTR_ERR(clk); |
| 1064 | goto rsnd_ssi_probe_done; |
| 1065 | } |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1066 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 1067 | if (of_get_property(np, "shared-pin", NULL)) |
| 1068 | ssi->flags |= RSND_SSI_CLK_PIN_SHARE; |
| 1069 | |
| 1070 | if (of_get_property(np, "no-busif", NULL)) |
| 1071 | ssi->flags |= RSND_SSI_NO_BUSIF; |
| 1072 | |
| 1073 | ssi->irq = irq_of_parse_and_map(np, 0); |
| 1074 | if (!ssi->irq) { |
| 1075 | ret = -EINVAL; |
| 1076 | goto rsnd_ssi_probe_done; |
| 1077 | } |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1078 | |
Julia Lawall | 5193029 | 2016-08-05 10:56:51 +0200 | [diff] [blame] | 1079 | if (of_property_read_bool(np, "pio-transfer")) |
Kuninori Morimoto | ff8f30e | 2014-03-03 20:50:49 -0800 | [diff] [blame] | 1080 | ops = &rsnd_ssi_pio_ops; |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 1081 | else |
| 1082 | ops = &rsnd_ssi_dma_ops; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1083 | |
Kuninori Morimoto | b76e218 | 2015-09-10 07:02:21 +0000 | [diff] [blame] | 1084 | ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk, |
Kuninori Morimoto | 5ba17b42 | 2016-01-21 01:58:07 +0000 | [diff] [blame] | 1085 | rsnd_ssi_get_status, RSND_MOD_SSI, i); |
Kuninori Morimoto | 2f78dd7 | 2015-03-26 04:02:09 +0000 | [diff] [blame] | 1086 | if (ret) |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 1087 | goto rsnd_ssi_probe_done; |
| 1088 | |
| 1089 | i++; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
Kuninori Morimoto | 02534f2 | 2015-11-10 05:11:35 +0000 | [diff] [blame] | 1092 | ret = 0; |
| 1093 | |
| 1094 | rsnd_ssi_probe_done: |
| 1095 | of_node_put(node); |
| 1096 | |
| 1097 | return ret; |
Kuninori Morimoto | ae5c322 | 2013-07-21 21:36:57 -0700 | [diff] [blame] | 1098 | } |
Kuninori Morimoto | 2f78dd7 | 2015-03-26 04:02:09 +0000 | [diff] [blame] | 1099 | |
Kuninori Morimoto | 2ea6b07 | 2015-11-10 05:14:12 +0000 | [diff] [blame] | 1100 | void rsnd_ssi_remove(struct rsnd_priv *priv) |
Kuninori Morimoto | 2f78dd7 | 2015-03-26 04:02:09 +0000 | [diff] [blame] | 1101 | { |
| 1102 | struct rsnd_ssi *ssi; |
| 1103 | int i; |
| 1104 | |
| 1105 | for_each_rsnd_ssi(ssi, priv, i) { |
Kuninori Morimoto | b76e218 | 2015-09-10 07:02:21 +0000 | [diff] [blame] | 1106 | rsnd_mod_quit(rsnd_mod_get(ssi)); |
Kuninori Morimoto | 2f78dd7 | 2015-03-26 04:02:09 +0000 | [diff] [blame] | 1107 | } |
| 1108 | } |