0001
0002
0003
0004
0005
0006
0007
0008
0009 #include "motu.h"
0010
0011 #include <linux/delay.h>
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 #define CLK_828_STATUS_OFFSET 0x0b00
0049 #define CLK_828_STATUS_MASK 0x0000ffff
0050 #define CLK_828_STATUS_FLAG_OPT_IN_IFACE_IS_SPDIF 0x00008000
0051 #define CLK_828_STATUS_FLAG_OPT_OUT_IFACE_IS_SPDIF 0x00004000
0052 #define CLK_828_STATUS_FLAG_FETCH_PCM_FRAMES 0x00000080
0053 #define CLK_828_STATUS_FLAG_ENABLE_OUTPUT 0x00000008
0054 #define CLK_828_STATUS_FLAG_RATE_48000 0x00000004
0055 #define CLK_828_STATUS_MASK_SRC 0x00000023
0056 #define CLK_828_STATUS_FLAG_SRC_ADAT_ON_OPT 0x00000021
0057 #define CLK_828_STATUS_FLAG_SRC_SPH 0x00000003
0058 #define CLK_828_STATUS_FLAG_SRC_SPDIF 0x00000002
0059 #define CLK_828_STATUS_FLAG_SRC_ADAT_ON_DSUB 0x00000001
0060 #define CLK_828_STATUS_FLAG_SRC_INTERNAL 0x00000000
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 #define CLK_896_STATUS_OFFSET 0x0b14
0113 #define CLK_896_STATUS_FLAG_FETCH_ENABLE 0x20000000
0114 #define CLK_896_STATUS_FLAG_OUTPUT_ON 0x03000000
0115 #define CLK_896_STATUS_MASK_SRC 0x00000007
0116 #define CLK_896_STATUS_FLAG_SRC_INTERNAL 0x00000000
0117 #define CLK_896_STATUS_FLAG_SRC_ADAT_ON_OPT 0x00000001
0118 #define CLK_896_STATUS_FLAG_SRC_AESEBU 0x00000002
0119 #define CLK_896_STATUS_FLAG_SRC_SPH 0x00000003
0120 #define CLK_896_STATUS_FLAG_SRC_WORD 0x00000004
0121 #define CLK_896_STATUS_FLAG_SRC_ADAT_ON_DSUB 0x00000005
0122 #define CLK_896_STATUS_MASK_RATE 0x00000018
0123 #define CLK_896_STATUS_FLAG_RATE_44100 0x00000000
0124 #define CLK_896_STATUS_FLAG_RATE_48000 0x00000008
0125 #define CLK_896_STATUS_FLAG_RATE_88200 0x00000010
0126 #define CLK_896_STATUS_FLAG_RATE_96000 0x00000018
0127
0128 static void parse_clock_rate_828(u32 data, unsigned int *rate)
0129 {
0130 if (data & CLK_828_STATUS_FLAG_RATE_48000)
0131 *rate = 48000;
0132 else
0133 *rate = 44100;
0134 }
0135
0136 static int get_clock_rate_828(struct snd_motu *motu, unsigned int *rate)
0137 {
0138 __be32 reg;
0139 int err;
0140
0141 err = snd_motu_transaction_read(motu, CLK_828_STATUS_OFFSET, ®, sizeof(reg));
0142 if (err < 0)
0143 return err;
0144 parse_clock_rate_828(be32_to_cpu(reg), rate);
0145
0146 return 0;
0147 }
0148
0149 static int parse_clock_rate_896(u32 data, unsigned int *rate)
0150 {
0151 switch (data & CLK_896_STATUS_MASK_RATE) {
0152 case CLK_896_STATUS_FLAG_RATE_44100:
0153 *rate = 44100;
0154 break;
0155 case CLK_896_STATUS_FLAG_RATE_48000:
0156 *rate = 48000;
0157 break;
0158 case CLK_896_STATUS_FLAG_RATE_88200:
0159 *rate = 88200;
0160 break;
0161 case CLK_896_STATUS_FLAG_RATE_96000:
0162 *rate = 96000;
0163 break;
0164 default:
0165 return -ENXIO;
0166 }
0167
0168 return 0;
0169 }
0170
0171 static int get_clock_rate_896(struct snd_motu *motu, unsigned int *rate)
0172 {
0173 __be32 reg;
0174 int err;
0175
0176 err = snd_motu_transaction_read(motu, CLK_896_STATUS_OFFSET, ®, sizeof(reg));
0177 if (err < 0)
0178 return err;
0179 return parse_clock_rate_896(be32_to_cpu(reg), rate);
0180 }
0181
0182 int snd_motu_protocol_v1_get_clock_rate(struct snd_motu *motu, unsigned int *rate)
0183 {
0184 if (motu->spec == &snd_motu_spec_828)
0185 return get_clock_rate_828(motu, rate);
0186 else if (motu->spec == &snd_motu_spec_896)
0187 return get_clock_rate_896(motu, rate);
0188 else
0189 return -ENXIO;
0190 }
0191
0192 static int set_clock_rate_828(struct snd_motu *motu, unsigned int rate)
0193 {
0194 __be32 reg;
0195 u32 data;
0196 int err;
0197
0198 err = snd_motu_transaction_read(motu, CLK_828_STATUS_OFFSET, ®, sizeof(reg));
0199 if (err < 0)
0200 return err;
0201 data = be32_to_cpu(reg) & CLK_828_STATUS_MASK;
0202
0203 data &= ~CLK_828_STATUS_FLAG_RATE_48000;
0204 if (rate == 48000)
0205 data |= CLK_828_STATUS_FLAG_RATE_48000;
0206
0207 reg = cpu_to_be32(data);
0208 return snd_motu_transaction_write(motu, CLK_828_STATUS_OFFSET, ®, sizeof(reg));
0209 }
0210
0211 static int set_clock_rate_896(struct snd_motu *motu, unsigned int rate)
0212 {
0213 unsigned int flag;
0214 __be32 reg;
0215 u32 data;
0216 int err;
0217
0218 err = snd_motu_transaction_read(motu, CLK_896_STATUS_OFFSET, ®, sizeof(reg));
0219 if (err < 0)
0220 return err;
0221 data = be32_to_cpu(reg);
0222
0223 switch (rate) {
0224 case 44100:
0225 flag = CLK_896_STATUS_FLAG_RATE_44100;
0226 break;
0227 case 48000:
0228 flag = CLK_896_STATUS_FLAG_RATE_48000;
0229 break;
0230 case 88200:
0231 flag = CLK_896_STATUS_FLAG_RATE_88200;
0232 break;
0233 case 96000:
0234 flag = CLK_896_STATUS_FLAG_RATE_96000;
0235 break;
0236 default:
0237 return -EINVAL;
0238 }
0239
0240 data &= ~CLK_896_STATUS_MASK_RATE;
0241 data |= flag;
0242
0243 reg = cpu_to_be32(data);
0244 return snd_motu_transaction_write(motu, CLK_896_STATUS_OFFSET, ®, sizeof(reg));
0245 }
0246
0247 int snd_motu_protocol_v1_set_clock_rate(struct snd_motu *motu, unsigned int rate)
0248 {
0249 if (motu->spec == &snd_motu_spec_828)
0250 return set_clock_rate_828(motu, rate);
0251 else if (motu->spec == &snd_motu_spec_896)
0252 return set_clock_rate_896(motu, rate);
0253 else
0254 return -ENXIO;
0255 }
0256
0257 static int get_clock_source_828(struct snd_motu *motu, enum snd_motu_clock_source *src)
0258 {
0259 __be32 reg;
0260 u32 data;
0261 int err;
0262
0263 err = snd_motu_transaction_read(motu, CLK_828_STATUS_OFFSET, ®, sizeof(reg));
0264 if (err < 0)
0265 return err;
0266 data = be32_to_cpu(reg) & CLK_828_STATUS_MASK;
0267
0268 switch (data & CLK_828_STATUS_MASK_SRC) {
0269 case CLK_828_STATUS_FLAG_SRC_ADAT_ON_OPT:
0270 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT;
0271 break;
0272 case CLK_828_STATUS_FLAG_SRC_SPH:
0273 *src = SND_MOTU_CLOCK_SOURCE_SPH;
0274 break;
0275 case CLK_828_STATUS_FLAG_SRC_SPDIF:
0276 {
0277 if (data & CLK_828_STATUS_FLAG_OPT_IN_IFACE_IS_SPDIF)
0278 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_COAX;
0279 else
0280 *src = SND_MOTU_CLOCK_SOURCE_SPDIF_ON_OPT;
0281 break;
0282 }
0283 case CLK_828_STATUS_FLAG_SRC_ADAT_ON_DSUB:
0284 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_DSUB;
0285 break;
0286 case CLK_828_STATUS_FLAG_SRC_INTERNAL:
0287 *src = SND_MOTU_CLOCK_SOURCE_INTERNAL;
0288 break;
0289 default:
0290 return -ENXIO;
0291 }
0292
0293 return 0;
0294 }
0295
0296 static int get_clock_source_896(struct snd_motu *motu, enum snd_motu_clock_source *src)
0297 {
0298 __be32 reg;
0299 u32 data;
0300 int err;
0301
0302 err = snd_motu_transaction_read(motu, CLK_896_STATUS_OFFSET, ®, sizeof(reg));
0303 if (err < 0)
0304 return err;
0305 data = be32_to_cpu(reg);
0306
0307 switch (data & CLK_896_STATUS_MASK_SRC) {
0308 case CLK_896_STATUS_FLAG_SRC_INTERNAL:
0309 *src = SND_MOTU_CLOCK_SOURCE_INTERNAL;
0310 break;
0311 case CLK_896_STATUS_FLAG_SRC_ADAT_ON_OPT:
0312 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_OPT;
0313 break;
0314 case CLK_896_STATUS_FLAG_SRC_AESEBU:
0315 *src = SND_MOTU_CLOCK_SOURCE_AESEBU_ON_XLR;
0316 break;
0317 case CLK_896_STATUS_FLAG_SRC_SPH:
0318 *src = SND_MOTU_CLOCK_SOURCE_SPH;
0319 break;
0320 case CLK_896_STATUS_FLAG_SRC_WORD:
0321 *src = SND_MOTU_CLOCK_SOURCE_WORD_ON_BNC;
0322 break;
0323 case CLK_896_STATUS_FLAG_SRC_ADAT_ON_DSUB:
0324 *src = SND_MOTU_CLOCK_SOURCE_ADAT_ON_DSUB;
0325 break;
0326 default:
0327 return -ENXIO;
0328 }
0329
0330 return 0;
0331 }
0332
0333 int snd_motu_protocol_v1_get_clock_source(struct snd_motu *motu, enum snd_motu_clock_source *src)
0334 {
0335 if (motu->spec == &snd_motu_spec_828)
0336 return get_clock_source_828(motu, src);
0337 else if (motu->spec == &snd_motu_spec_896)
0338 return get_clock_source_896(motu, src);
0339 else
0340 return -ENXIO;
0341 }
0342
0343 static int switch_fetching_mode_828(struct snd_motu *motu, bool enable)
0344 {
0345 __be32 reg;
0346 u32 data;
0347 int err;
0348
0349 err = snd_motu_transaction_read(motu, CLK_828_STATUS_OFFSET, ®, sizeof(reg));
0350 if (err < 0)
0351 return err;
0352 data = be32_to_cpu(reg) & CLK_828_STATUS_MASK;
0353
0354 data &= ~(CLK_828_STATUS_FLAG_FETCH_PCM_FRAMES | CLK_828_STATUS_FLAG_ENABLE_OUTPUT);
0355 if (enable) {
0356
0357
0358
0359 msleep(100);
0360 data |= CLK_828_STATUS_FLAG_FETCH_PCM_FRAMES | CLK_828_STATUS_FLAG_ENABLE_OUTPUT;
0361 }
0362
0363 reg = cpu_to_be32(data);
0364 return snd_motu_transaction_write(motu, CLK_828_STATUS_OFFSET, ®, sizeof(reg));
0365 }
0366
0367 static int switch_fetching_mode_896(struct snd_motu *motu, bool enable)
0368 {
0369 __be32 reg;
0370 u32 data;
0371 int err;
0372
0373 err = snd_motu_transaction_read(motu, CLK_896_STATUS_OFFSET, ®, sizeof(reg));
0374 if (err < 0)
0375 return err;
0376 data = be32_to_cpu(reg);
0377
0378 data &= ~CLK_896_STATUS_FLAG_FETCH_ENABLE;
0379 if (enable)
0380 data |= CLK_896_STATUS_FLAG_FETCH_ENABLE | CLK_896_STATUS_FLAG_OUTPUT_ON;
0381
0382 reg = cpu_to_be32(data);
0383 return snd_motu_transaction_write(motu, CLK_896_STATUS_OFFSET, ®, sizeof(reg));
0384 }
0385
0386 int snd_motu_protocol_v1_switch_fetching_mode(struct snd_motu *motu, bool enable)
0387 {
0388 if (motu->spec == &snd_motu_spec_828)
0389 return switch_fetching_mode_828(motu, enable);
0390 else if (motu->spec == &snd_motu_spec_896)
0391 return switch_fetching_mode_896(motu, enable);
0392 else
0393 return -ENXIO;
0394 }
0395
0396 static int detect_packet_formats_828(struct snd_motu *motu)
0397 {
0398 __be32 reg;
0399 u32 data;
0400 int err;
0401
0402 motu->tx_packet_formats.pcm_byte_offset = 4;
0403 motu->tx_packet_formats.msg_chunks = 2;
0404
0405 motu->rx_packet_formats.pcm_byte_offset = 4;
0406 motu->rx_packet_formats.msg_chunks = 0;
0407
0408 err = snd_motu_transaction_read(motu, CLK_828_STATUS_OFFSET, ®, sizeof(reg));
0409 if (err < 0)
0410 return err;
0411 data = be32_to_cpu(reg) & CLK_828_STATUS_MASK;
0412
0413
0414 if (!(data & CLK_828_STATUS_FLAG_OPT_IN_IFACE_IS_SPDIF))
0415 motu->tx_packet_formats.pcm_chunks[0] += 8;
0416
0417 if (!(data & CLK_828_STATUS_FLAG_OPT_OUT_IFACE_IS_SPDIF))
0418 motu->rx_packet_formats.pcm_chunks[0] += 8;
0419
0420 return 0;
0421 }
0422
0423 static int detect_packet_formats_896(struct snd_motu *motu)
0424 {
0425
0426 motu->tx_packet_formats.pcm_byte_offset = 4;
0427 motu->rx_packet_formats.pcm_byte_offset = 4;
0428
0429
0430 motu->tx_packet_formats.msg_chunks = 0;
0431 motu->rx_packet_formats.msg_chunks = 0;
0432
0433
0434
0435 motu->tx_packet_formats.pcm_chunks[0] += 8;
0436 motu->tx_packet_formats.pcm_chunks[1] += 8;
0437
0438 motu->rx_packet_formats.pcm_chunks[0] += 8;
0439 motu->rx_packet_formats.pcm_chunks[1] += 8;
0440
0441 return 0;
0442 }
0443
0444 int snd_motu_protocol_v1_cache_packet_formats(struct snd_motu *motu)
0445 {
0446 memcpy(motu->tx_packet_formats.pcm_chunks, motu->spec->tx_fixed_pcm_chunks,
0447 sizeof(motu->tx_packet_formats.pcm_chunks));
0448 memcpy(motu->rx_packet_formats.pcm_chunks, motu->spec->rx_fixed_pcm_chunks,
0449 sizeof(motu->rx_packet_formats.pcm_chunks));
0450
0451 if (motu->spec == &snd_motu_spec_828)
0452 return detect_packet_formats_828(motu);
0453 else if (motu->spec == &snd_motu_spec_896)
0454 return detect_packet_formats_896(motu);
0455 else
0456 return 0;
0457 }
0458
0459 const struct snd_motu_spec snd_motu_spec_828 = {
0460 .name = "828",
0461 .protocol_version = SND_MOTU_PROTOCOL_V1,
0462 .tx_fixed_pcm_chunks = {10, 0, 0},
0463 .rx_fixed_pcm_chunks = {10, 0, 0},
0464 };
0465
0466 const struct snd_motu_spec snd_motu_spec_896 = {
0467 .name = "896",
0468 .tx_fixed_pcm_chunks = {10, 10, 0},
0469 .rx_fixed_pcm_chunks = {10, 10, 0},
0470 };