Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Renesas R-Car SSIU support |
| 3 | * |
| 4 | * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include "rsnd.h" |
| 11 | |
| 12 | #define SSIU_NAME "ssiu" |
| 13 | |
| 14 | struct rsnd_ssiu { |
| 15 | struct rsnd_mod mod; |
| 16 | }; |
| 17 | |
| 18 | #define rsnd_ssiu_nr(priv) ((priv)->ssiu_nr) |
| 19 | #define for_each_rsnd_ssiu(pos, priv, i) \ |
| 20 | for (i = 0; \ |
| 21 | (i < rsnd_ssiu_nr(priv)) && \ |
| 22 | ((pos) = ((struct rsnd_ssiu *)(priv)->ssiu + i)); \ |
| 23 | i++) |
| 24 | |
| 25 | static int rsnd_ssiu_init(struct rsnd_mod *mod, |
| 26 | struct rsnd_dai_stream *io, |
| 27 | struct rsnd_priv *priv) |
| 28 | { |
| 29 | struct rsnd_dai *rdai = rsnd_io_to_rdai(io); |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 30 | u32 multi_ssi_slaves = rsnd_ssi_multi_slaves(io); |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 31 | int use_busif = rsnd_ssi_use_busif(io); |
| 32 | int id = rsnd_mod_id(mod); |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 33 | u32 mask1, val1; |
| 34 | u32 mask2, val2; |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * SSI_MODE0 |
| 38 | */ |
| 39 | rsnd_mod_bset(mod, SSI_MODE0, (1 << id), !use_busif << id); |
| 40 | |
| 41 | /* |
| 42 | * SSI_MODE1 |
| 43 | */ |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 44 | mask1 = (1 << 4) | (1 << 20); /* mask sync bit */ |
| 45 | mask2 = (1 << 4); /* mask sync bit */ |
| 46 | val1 = val2 = 0; |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 47 | if (rsnd_ssi_is_pin_sharing(io)) { |
| 48 | int shift = -1; |
| 49 | |
| 50 | switch (id) { |
| 51 | case 1: |
| 52 | shift = 0; |
| 53 | break; |
| 54 | case 2: |
| 55 | shift = 2; |
| 56 | break; |
| 57 | case 4: |
| 58 | shift = 16; |
| 59 | break; |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 60 | default: |
| 61 | return -EINVAL; |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 64 | mask1 |= 0x3 << shift; |
| 65 | val1 = rsnd_rdai_is_clk_master(rdai) ? |
| 66 | 0x2 << shift : 0x1 << shift; |
| 67 | |
| 68 | } else if (multi_ssi_slaves) { |
| 69 | |
| 70 | mask2 |= 0x00000007; |
| 71 | mask1 |= 0x0000000f; |
| 72 | |
| 73 | switch (multi_ssi_slaves) { |
| 74 | case 0x0206: /* SSI0/1/2/9 */ |
| 75 | val2 = (1 << 4) | /* SSI0129 sync */ |
Dan Carpenter | 8012c98 | 2016-01-06 12:38:41 +0300 | [diff] [blame^] | 76 | (rsnd_rdai_is_clk_master(rdai) ? 0x2 : 0x1); |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 77 | /* fall through */ |
| 78 | case 0x0006: /* SSI0/1/2 */ |
| 79 | val1 = rsnd_rdai_is_clk_master(rdai) ? |
| 80 | 0xa : 0x5; |
| 81 | |
| 82 | if (!val2) /* SSI012 sync */ |
| 83 | val1 |= (1 << 4); |
| 84 | } |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 87 | rsnd_mod_bset(mod, SSI_MODE1, mask1, val1); |
| 88 | rsnd_mod_bset(mod, SSI_MODE2, mask2, val2); |
| 89 | |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static struct rsnd_mod_ops rsnd_ssiu_ops_gen1 = { |
| 94 | .name = SSIU_NAME, |
| 95 | .init = rsnd_ssiu_init, |
| 96 | }; |
| 97 | |
| 98 | static int rsnd_ssiu_init_gen2(struct rsnd_mod *mod, |
| 99 | struct rsnd_dai_stream *io, |
| 100 | struct rsnd_priv *priv) |
| 101 | { |
| 102 | int ret; |
| 103 | |
| 104 | ret = rsnd_ssiu_init(mod, io, priv); |
| 105 | if (ret < 0) |
| 106 | return ret; |
| 107 | |
Kuninori Morimoto | c140284 | 2015-12-17 02:57:27 +0000 | [diff] [blame] | 108 | if (rsnd_get_slot_width(io) >= 6) { |
Kuninori Morimoto | 186fadc | 2015-11-30 08:54:03 +0000 | [diff] [blame] | 109 | /* |
| 110 | * TDM Extend Mode |
| 111 | * see |
| 112 | * rsnd_ssi_config_init() |
| 113 | */ |
| 114 | rsnd_mod_write(mod, SSI_MODE, 0x1); |
| 115 | } |
| 116 | |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 117 | if (rsnd_ssi_use_busif(io)) { |
| 118 | u32 val = rsnd_get_dalign(mod, io); |
| 119 | |
| 120 | rsnd_mod_write(mod, SSI_BUSIF_ADINR, |
Kuninori Morimoto | c45f726 | 2015-11-30 08:49:33 +0000 | [diff] [blame] | 121 | rsnd_get_adinr_bit(mod, io) | |
| 122 | rsnd_get_adinr_chan(mod, io)); |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 123 | rsnd_mod_write(mod, SSI_BUSIF_MODE, 1); |
| 124 | rsnd_mod_write(mod, SSI_BUSIF_DALIGN, val); |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int rsnd_ssiu_start_gen2(struct rsnd_mod *mod, |
| 131 | struct rsnd_dai_stream *io, |
| 132 | struct rsnd_priv *priv) |
| 133 | { |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 134 | if (!rsnd_ssi_use_busif(io)) |
| 135 | return 0; |
| 136 | |
| 137 | rsnd_mod_write(mod, SSI_CTRL, 0x1); |
| 138 | |
| 139 | if (rsnd_ssi_multi_slaves(io)) |
| 140 | rsnd_mod_write(mod, SSI_CONTROL, 0x1); |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int rsnd_ssiu_stop_gen2(struct rsnd_mod *mod, |
| 146 | struct rsnd_dai_stream *io, |
| 147 | struct rsnd_priv *priv) |
| 148 | { |
Kuninori Morimoto | b4c83b1 | 2015-12-17 03:00:10 +0000 | [diff] [blame] | 149 | if (!rsnd_ssi_use_busif(io)) |
| 150 | return 0; |
| 151 | |
| 152 | rsnd_mod_write(mod, SSI_CTRL, 0); |
| 153 | |
| 154 | if (rsnd_ssi_multi_slaves(io)) |
| 155 | rsnd_mod_write(mod, SSI_CONTROL, 0); |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static struct rsnd_mod_ops rsnd_ssiu_ops_gen2 = { |
| 161 | .name = SSIU_NAME, |
| 162 | .init = rsnd_ssiu_init_gen2, |
| 163 | .start = rsnd_ssiu_start_gen2, |
| 164 | .stop = rsnd_ssiu_stop_gen2, |
| 165 | }; |
| 166 | |
| 167 | static struct rsnd_mod *rsnd_ssiu_mod_get(struct rsnd_priv *priv, int id) |
| 168 | { |
| 169 | if (WARN_ON(id < 0 || id >= rsnd_ssiu_nr(priv))) |
| 170 | id = 0; |
| 171 | |
| 172 | return rsnd_mod_get((struct rsnd_ssiu *)(priv->ssiu) + id); |
| 173 | } |
| 174 | |
| 175 | int rsnd_ssiu_attach(struct rsnd_dai_stream *io, |
| 176 | struct rsnd_mod *ssi_mod) |
| 177 | { |
| 178 | struct rsnd_priv *priv = rsnd_io_to_priv(io); |
| 179 | struct rsnd_mod *mod = rsnd_ssiu_mod_get(priv, rsnd_mod_id(ssi_mod)); |
| 180 | |
| 181 | rsnd_mod_confirm_ssi(ssi_mod); |
| 182 | |
| 183 | return rsnd_dai_connect(mod, io, mod->type); |
| 184 | } |
| 185 | |
Kuninori Morimoto | 2ea6b07 | 2015-11-10 05:14:12 +0000 | [diff] [blame] | 186 | int rsnd_ssiu_probe(struct rsnd_priv *priv) |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 187 | { |
| 188 | struct device *dev = rsnd_priv_to_dev(priv); |
| 189 | struct rsnd_ssiu *ssiu; |
| 190 | static struct rsnd_mod_ops *ops; |
| 191 | int i, nr, ret; |
| 192 | |
| 193 | /* same number to SSI */ |
| 194 | nr = priv->ssi_nr; |
| 195 | ssiu = devm_kzalloc(dev, sizeof(*ssiu) * nr, GFP_KERNEL); |
| 196 | if (!ssiu) |
| 197 | return -ENOMEM; |
| 198 | |
| 199 | priv->ssiu = ssiu; |
| 200 | priv->ssiu_nr = nr; |
| 201 | |
| 202 | if (rsnd_is_gen1(priv)) |
| 203 | ops = &rsnd_ssiu_ops_gen1; |
| 204 | else |
| 205 | ops = &rsnd_ssiu_ops_gen2; |
| 206 | |
| 207 | for_each_rsnd_ssiu(ssiu, priv, i) { |
| 208 | ret = rsnd_mod_init(priv, rsnd_mod_get(ssiu), |
| 209 | ops, NULL, RSND_MOD_SSIU, i); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Kuninori Morimoto | 2ea6b07 | 2015-11-10 05:14:12 +0000 | [diff] [blame] | 217 | void rsnd_ssiu_remove(struct rsnd_priv *priv) |
Kuninori Morimoto | c7f69ab | 2015-10-26 08:43:41 +0000 | [diff] [blame] | 218 | { |
| 219 | struct rsnd_ssiu *ssiu; |
| 220 | int i; |
| 221 | |
| 222 | for_each_rsnd_ssiu(ssiu, priv, i) { |
| 223 | rsnd_mod_quit(rsnd_mod_get(ssiu)); |
| 224 | } |
| 225 | } |