dice-pcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * dice_pcm.c - a part of driver for DICE based devices
  4. *
  5. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  6. * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  7. */
  8. #include "dice.h"
  9. static int dice_rate_constraint(struct snd_pcm_hw_params *params,
  10. struct snd_pcm_hw_rule *rule)
  11. {
  12. struct snd_pcm_substream *substream = rule->private;
  13. struct snd_dice *dice = substream->private_data;
  14. unsigned int index = substream->pcm->device;
  15. const struct snd_interval *c =
  16. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  17. struct snd_interval *r =
  18. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  19. struct snd_interval rates = {
  20. .min = UINT_MAX, .max = 0, .integer = 1
  21. };
  22. unsigned int *pcm_channels;
  23. enum snd_dice_rate_mode mode;
  24. unsigned int i, rate;
  25. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  26. pcm_channels = dice->tx_pcm_chs[index];
  27. else
  28. pcm_channels = dice->rx_pcm_chs[index];
  29. for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
  30. rate = snd_dice_rates[i];
  31. if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
  32. continue;
  33. if (!snd_interval_test(c, pcm_channels[mode]))
  34. continue;
  35. rates.min = min(rates.min, rate);
  36. rates.max = max(rates.max, rate);
  37. }
  38. return snd_interval_refine(r, &rates);
  39. }
  40. static int dice_channels_constraint(struct snd_pcm_hw_params *params,
  41. struct snd_pcm_hw_rule *rule)
  42. {
  43. struct snd_pcm_substream *substream = rule->private;
  44. struct snd_dice *dice = substream->private_data;
  45. unsigned int index = substream->pcm->device;
  46. const struct snd_interval *r =
  47. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
  48. struct snd_interval *c =
  49. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  50. struct snd_interval channels = {
  51. .min = UINT_MAX, .max = 0, .integer = 1
  52. };
  53. unsigned int *pcm_channels;
  54. enum snd_dice_rate_mode mode;
  55. unsigned int i, rate;
  56. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  57. pcm_channels = dice->tx_pcm_chs[index];
  58. else
  59. pcm_channels = dice->rx_pcm_chs[index];
  60. for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
  61. rate = snd_dice_rates[i];
  62. if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
  63. continue;
  64. if (!snd_interval_test(r, rate))
  65. continue;
  66. channels.min = min(channels.min, pcm_channels[mode]);
  67. channels.max = max(channels.max, pcm_channels[mode]);
  68. }
  69. return snd_interval_refine(c, &channels);
  70. }
  71. static int limit_channels_and_rates(struct snd_dice *dice,
  72. struct snd_pcm_runtime *runtime,
  73. enum amdtp_stream_direction dir,
  74. unsigned int index)
  75. {
  76. struct snd_pcm_hardware *hw = &runtime->hw;
  77. unsigned int *pcm_channels;
  78. unsigned int i;
  79. if (dir == AMDTP_IN_STREAM)
  80. pcm_channels = dice->tx_pcm_chs[index];
  81. else
  82. pcm_channels = dice->rx_pcm_chs[index];
  83. hw->channels_min = UINT_MAX;
  84. hw->channels_max = 0;
  85. for (i = 0; i < ARRAY_SIZE(snd_dice_rates); ++i) {
  86. enum snd_dice_rate_mode mode;
  87. unsigned int rate, channels;
  88. rate = snd_dice_rates[i];
  89. if (snd_dice_stream_get_rate_mode(dice, rate, &mode) < 0)
  90. continue;
  91. hw->rates |= snd_pcm_rate_to_rate_bit(rate);
  92. channels = pcm_channels[mode];
  93. if (channels == 0)
  94. continue;
  95. hw->channels_min = min(hw->channels_min, channels);
  96. hw->channels_max = max(hw->channels_max, channels);
  97. }
  98. snd_pcm_limit_hw_rates(runtime);
  99. return 0;
  100. }
  101. static int init_hw_info(struct snd_dice *dice,
  102. struct snd_pcm_substream *substream)
  103. {
  104. struct snd_pcm_runtime *runtime = substream->runtime;
  105. struct snd_pcm_hardware *hw = &runtime->hw;
  106. unsigned int index = substream->pcm->device;
  107. enum amdtp_stream_direction dir;
  108. struct amdtp_stream *stream;
  109. int err;
  110. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  111. hw->formats = AM824_IN_PCM_FORMAT_BITS;
  112. dir = AMDTP_IN_STREAM;
  113. stream = &dice->tx_stream[index];
  114. } else {
  115. hw->formats = AM824_OUT_PCM_FORMAT_BITS;
  116. dir = AMDTP_OUT_STREAM;
  117. stream = &dice->rx_stream[index];
  118. }
  119. err = limit_channels_and_rates(dice, substream->runtime, dir,
  120. index);
  121. if (err < 0)
  122. return err;
  123. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  124. dice_rate_constraint, substream,
  125. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  126. if (err < 0)
  127. return err;
  128. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  129. dice_channels_constraint, substream,
  130. SNDRV_PCM_HW_PARAM_RATE, -1);
  131. if (err < 0)
  132. return err;
  133. return amdtp_am824_add_pcm_hw_constraints(stream, runtime);
  134. }
  135. static int pcm_open(struct snd_pcm_substream *substream)
  136. {
  137. struct snd_dice *dice = substream->private_data;
  138. struct amdtp_domain *d = &dice->domain;
  139. unsigned int source;
  140. bool internal;
  141. int err;
  142. err = snd_dice_stream_lock_try(dice);
  143. if (err < 0)
  144. return err;
  145. err = init_hw_info(dice, substream);
  146. if (err < 0)
  147. goto err_locked;
  148. err = snd_dice_transaction_get_clock_source(dice, &source);
  149. if (err < 0)
  150. goto err_locked;
  151. switch (source) {
  152. case CLOCK_SOURCE_AES1:
  153. case CLOCK_SOURCE_AES2:
  154. case CLOCK_SOURCE_AES3:
  155. case CLOCK_SOURCE_AES4:
  156. case CLOCK_SOURCE_AES_ANY:
  157. case CLOCK_SOURCE_ADAT:
  158. case CLOCK_SOURCE_TDIF:
  159. case CLOCK_SOURCE_WC:
  160. internal = false;
  161. break;
  162. default:
  163. internal = true;
  164. break;
  165. }
  166. mutex_lock(&dice->mutex);
  167. // When source of clock is not internal or any stream is reserved for
  168. // transmission of PCM frames, the available sampling rate is limited
  169. // at current one.
  170. if (!internal ||
  171. (dice->substreams_counter > 0 && d->events_per_period > 0)) {
  172. unsigned int frames_per_period = d->events_per_period;
  173. unsigned int frames_per_buffer = d->events_per_buffer;
  174. unsigned int rate;
  175. err = snd_dice_transaction_get_rate(dice, &rate);
  176. if (err < 0) {
  177. mutex_unlock(&dice->mutex);
  178. goto err_locked;
  179. }
  180. substream->runtime->hw.rate_min = rate;
  181. substream->runtime->hw.rate_max = rate;
  182. if (frames_per_period > 0) {
  183. // For double_pcm_frame quirk.
  184. if (rate > 96000 && !dice->disable_double_pcm_frames) {
  185. frames_per_period *= 2;
  186. frames_per_buffer *= 2;
  187. }
  188. err = snd_pcm_hw_constraint_minmax(substream->runtime,
  189. SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  190. frames_per_period, frames_per_period);
  191. if (err < 0) {
  192. mutex_unlock(&dice->mutex);
  193. goto err_locked;
  194. }
  195. err = snd_pcm_hw_constraint_minmax(substream->runtime,
  196. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  197. frames_per_buffer, frames_per_buffer);
  198. if (err < 0) {
  199. mutex_unlock(&dice->mutex);
  200. goto err_locked;
  201. }
  202. }
  203. }
  204. mutex_unlock(&dice->mutex);
  205. snd_pcm_set_sync(substream);
  206. return 0;
  207. err_locked:
  208. snd_dice_stream_lock_release(dice);
  209. return err;
  210. }
  211. static int pcm_close(struct snd_pcm_substream *substream)
  212. {
  213. struct snd_dice *dice = substream->private_data;
  214. snd_dice_stream_lock_release(dice);
  215. return 0;
  216. }
  217. static int pcm_hw_params(struct snd_pcm_substream *substream,
  218. struct snd_pcm_hw_params *hw_params)
  219. {
  220. struct snd_dice *dice = substream->private_data;
  221. int err = 0;
  222. if (substream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  223. unsigned int rate = params_rate(hw_params);
  224. unsigned int events_per_period = params_period_size(hw_params);
  225. unsigned int events_per_buffer = params_buffer_size(hw_params);
  226. mutex_lock(&dice->mutex);
  227. // For double_pcm_frame quirk.
  228. if (rate > 96000 && !dice->disable_double_pcm_frames) {
  229. events_per_period /= 2;
  230. events_per_buffer /= 2;
  231. }
  232. err = snd_dice_stream_reserve_duplex(dice, rate,
  233. events_per_period, events_per_buffer);
  234. if (err >= 0)
  235. ++dice->substreams_counter;
  236. mutex_unlock(&dice->mutex);
  237. }
  238. return err;
  239. }
  240. static int pcm_hw_free(struct snd_pcm_substream *substream)
  241. {
  242. struct snd_dice *dice = substream->private_data;
  243. mutex_lock(&dice->mutex);
  244. if (substream->runtime->state != SNDRV_PCM_STATE_OPEN)
  245. --dice->substreams_counter;
  246. snd_dice_stream_stop_duplex(dice);
  247. mutex_unlock(&dice->mutex);
  248. return 0;
  249. }
  250. static int capture_prepare(struct snd_pcm_substream *substream)
  251. {
  252. struct snd_dice *dice = substream->private_data;
  253. struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
  254. int err;
  255. mutex_lock(&dice->mutex);
  256. err = snd_dice_stream_start_duplex(dice);
  257. mutex_unlock(&dice->mutex);
  258. if (err >= 0)
  259. amdtp_stream_pcm_prepare(stream);
  260. return 0;
  261. }
  262. static int playback_prepare(struct snd_pcm_substream *substream)
  263. {
  264. struct snd_dice *dice = substream->private_data;
  265. struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
  266. int err;
  267. mutex_lock(&dice->mutex);
  268. err = snd_dice_stream_start_duplex(dice);
  269. mutex_unlock(&dice->mutex);
  270. if (err >= 0)
  271. amdtp_stream_pcm_prepare(stream);
  272. return err;
  273. }
  274. static int capture_trigger(struct snd_pcm_substream *substream, int cmd)
  275. {
  276. struct snd_dice *dice = substream->private_data;
  277. struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
  278. switch (cmd) {
  279. case SNDRV_PCM_TRIGGER_START:
  280. amdtp_stream_pcm_trigger(stream, substream);
  281. break;
  282. case SNDRV_PCM_TRIGGER_STOP:
  283. amdtp_stream_pcm_trigger(stream, NULL);
  284. break;
  285. default:
  286. return -EINVAL;
  287. }
  288. return 0;
  289. }
  290. static int playback_trigger(struct snd_pcm_substream *substream, int cmd)
  291. {
  292. struct snd_dice *dice = substream->private_data;
  293. struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
  294. switch (cmd) {
  295. case SNDRV_PCM_TRIGGER_START:
  296. amdtp_stream_pcm_trigger(stream, substream);
  297. break;
  298. case SNDRV_PCM_TRIGGER_STOP:
  299. amdtp_stream_pcm_trigger(stream, NULL);
  300. break;
  301. default:
  302. return -EINVAL;
  303. }
  304. return 0;
  305. }
  306. static snd_pcm_uframes_t capture_pointer(struct snd_pcm_substream *substream)
  307. {
  308. struct snd_dice *dice = substream->private_data;
  309. struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
  310. return amdtp_domain_stream_pcm_pointer(&dice->domain, stream);
  311. }
  312. static snd_pcm_uframes_t playback_pointer(struct snd_pcm_substream *substream)
  313. {
  314. struct snd_dice *dice = substream->private_data;
  315. struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
  316. return amdtp_domain_stream_pcm_pointer(&dice->domain, stream);
  317. }
  318. static int capture_ack(struct snd_pcm_substream *substream)
  319. {
  320. struct snd_dice *dice = substream->private_data;
  321. struct amdtp_stream *stream = &dice->tx_stream[substream->pcm->device];
  322. return amdtp_domain_stream_pcm_ack(&dice->domain, stream);
  323. }
  324. static int playback_ack(struct snd_pcm_substream *substream)
  325. {
  326. struct snd_dice *dice = substream->private_data;
  327. struct amdtp_stream *stream = &dice->rx_stream[substream->pcm->device];
  328. return amdtp_domain_stream_pcm_ack(&dice->domain, stream);
  329. }
  330. int snd_dice_create_pcm(struct snd_dice *dice)
  331. {
  332. static const struct snd_pcm_ops capture_ops = {
  333. .open = pcm_open,
  334. .close = pcm_close,
  335. .hw_params = pcm_hw_params,
  336. .hw_free = pcm_hw_free,
  337. .prepare = capture_prepare,
  338. .trigger = capture_trigger,
  339. .pointer = capture_pointer,
  340. .ack = capture_ack,
  341. };
  342. static const struct snd_pcm_ops playback_ops = {
  343. .open = pcm_open,
  344. .close = pcm_close,
  345. .hw_params = pcm_hw_params,
  346. .hw_free = pcm_hw_free,
  347. .prepare = playback_prepare,
  348. .trigger = playback_trigger,
  349. .pointer = playback_pointer,
  350. .ack = playback_ack,
  351. };
  352. struct snd_pcm *pcm;
  353. unsigned int capture, playback;
  354. int i, j;
  355. int err;
  356. for (i = 0; i < MAX_STREAMS; i++) {
  357. capture = playback = 0;
  358. for (j = 0; j < SND_DICE_RATE_MODE_COUNT; ++j) {
  359. if (dice->tx_pcm_chs[i][j] > 0)
  360. capture = 1;
  361. if (dice->rx_pcm_chs[i][j] > 0)
  362. playback = 1;
  363. }
  364. err = snd_pcm_new(dice->card, "DICE", i, playback, capture,
  365. &pcm);
  366. if (err < 0)
  367. return err;
  368. pcm->private_data = dice;
  369. pcm->nonatomic = true;
  370. strcpy(pcm->name, dice->card->shortname);
  371. if (capture > 0)
  372. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  373. &capture_ops);
  374. if (playback > 0)
  375. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  376. &playback_ops);
  377. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
  378. NULL, 0, 0);
  379. }
  380. return 0;
  381. }