ctpcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
  3. *
  4. * This source file is released under GPL v2 license (no other versions).
  5. * See the COPYING file included in the main directory of this source
  6. * distribution for the license terms and conditions.
  7. *
  8. * @File ctpcm.c
  9. *
  10. * @Brief
  11. * This file contains the definition of the pcm device functions.
  12. *
  13. * @Author Liu Chun
  14. * @Date Apr 2 2008
  15. *
  16. */
  17. #include "ctpcm.h"
  18. #include "cttimer.h"
  19. #include <linux/slab.h>
  20. #include <sound/pcm.h>
  21. /* Hardware descriptions for playback */
  22. static const struct snd_pcm_hardware ct_pcm_playback_hw = {
  23. .info = (SNDRV_PCM_INFO_MMAP |
  24. SNDRV_PCM_INFO_INTERLEAVED |
  25. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  26. SNDRV_PCM_INFO_MMAP_VALID |
  27. SNDRV_PCM_INFO_PAUSE),
  28. .formats = (SNDRV_PCM_FMTBIT_U8 |
  29. SNDRV_PCM_FMTBIT_S16_LE |
  30. SNDRV_PCM_FMTBIT_S24_3LE |
  31. SNDRV_PCM_FMTBIT_S32_LE |
  32. SNDRV_PCM_FMTBIT_FLOAT_LE),
  33. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  34. SNDRV_PCM_RATE_8000_192000),
  35. .rate_min = 8000,
  36. .rate_max = 192000,
  37. .channels_min = 1,
  38. .channels_max = 2,
  39. .buffer_bytes_max = (128*1024),
  40. .period_bytes_min = (64),
  41. .period_bytes_max = (128*1024),
  42. .periods_min = 2,
  43. .periods_max = 1024,
  44. .fifo_size = 0,
  45. };
  46. static const struct snd_pcm_hardware ct_spdif_passthru_playback_hw = {
  47. .info = (SNDRV_PCM_INFO_MMAP |
  48. SNDRV_PCM_INFO_INTERLEAVED |
  49. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  50. SNDRV_PCM_INFO_MMAP_VALID |
  51. SNDRV_PCM_INFO_PAUSE),
  52. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  53. .rates = (SNDRV_PCM_RATE_48000 |
  54. SNDRV_PCM_RATE_44100 |
  55. SNDRV_PCM_RATE_32000),
  56. .rate_min = 32000,
  57. .rate_max = 48000,
  58. .channels_min = 2,
  59. .channels_max = 2,
  60. .buffer_bytes_max = (128*1024),
  61. .period_bytes_min = (64),
  62. .period_bytes_max = (128*1024),
  63. .periods_min = 2,
  64. .periods_max = 1024,
  65. .fifo_size = 0,
  66. };
  67. /* Hardware descriptions for capture */
  68. static const struct snd_pcm_hardware ct_pcm_capture_hw = {
  69. .info = (SNDRV_PCM_INFO_MMAP |
  70. SNDRV_PCM_INFO_INTERLEAVED |
  71. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  72. SNDRV_PCM_INFO_PAUSE |
  73. SNDRV_PCM_INFO_MMAP_VALID),
  74. .formats = (SNDRV_PCM_FMTBIT_U8 |
  75. SNDRV_PCM_FMTBIT_S16_LE |
  76. SNDRV_PCM_FMTBIT_S24_3LE |
  77. SNDRV_PCM_FMTBIT_S32_LE |
  78. SNDRV_PCM_FMTBIT_FLOAT_LE),
  79. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  80. SNDRV_PCM_RATE_8000_96000),
  81. .rate_min = 8000,
  82. .rate_max = 96000,
  83. .channels_min = 1,
  84. .channels_max = 2,
  85. .buffer_bytes_max = (128*1024),
  86. .period_bytes_min = (384),
  87. .period_bytes_max = (64*1024),
  88. .periods_min = 2,
  89. .periods_max = 1024,
  90. .fifo_size = 0,
  91. };
  92. static void ct_atc_pcm_interrupt(struct ct_atc_pcm *atc_pcm)
  93. {
  94. struct ct_atc_pcm *apcm = atc_pcm;
  95. if (!apcm->substream)
  96. return;
  97. snd_pcm_period_elapsed(apcm->substream);
  98. }
  99. static void ct_atc_pcm_free_substream(struct snd_pcm_runtime *runtime)
  100. {
  101. struct ct_atc_pcm *apcm = runtime->private_data;
  102. struct ct_atc *atc = snd_pcm_substream_chip(apcm->substream);
  103. atc->pcm_release_resources(atc, apcm);
  104. ct_timer_instance_free(apcm->timer);
  105. kfree(apcm);
  106. runtime->private_data = NULL;
  107. }
  108. /* pcm playback operations */
  109. static int ct_pcm_playback_open(struct snd_pcm_substream *substream)
  110. {
  111. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  112. struct snd_pcm_runtime *runtime = substream->runtime;
  113. struct ct_atc_pcm *apcm;
  114. int err;
  115. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  116. if (!apcm)
  117. return -ENOMEM;
  118. apcm->substream = substream;
  119. apcm->interrupt = ct_atc_pcm_interrupt;
  120. if (IEC958 == substream->pcm->device) {
  121. runtime->hw = ct_spdif_passthru_playback_hw;
  122. atc->spdif_out_passthru(atc, 1);
  123. } else {
  124. runtime->hw = ct_pcm_playback_hw;
  125. if (FRONT == substream->pcm->device)
  126. runtime->hw.channels_max = 8;
  127. }
  128. err = snd_pcm_hw_constraint_integer(runtime,
  129. SNDRV_PCM_HW_PARAM_PERIODS);
  130. if (err < 0)
  131. goto free_pcm;
  132. err = snd_pcm_hw_constraint_minmax(runtime,
  133. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  134. 1024, UINT_MAX);
  135. if (err < 0)
  136. goto free_pcm;
  137. apcm->timer = ct_timer_instance_new(atc->timer, apcm);
  138. if (!apcm->timer) {
  139. err = -ENOMEM;
  140. goto free_pcm;
  141. }
  142. runtime->private_data = apcm;
  143. runtime->private_free = ct_atc_pcm_free_substream;
  144. return 0;
  145. free_pcm:
  146. kfree(apcm);
  147. return err;
  148. }
  149. static int ct_pcm_playback_close(struct snd_pcm_substream *substream)
  150. {
  151. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  152. /* TODO: Notify mixer inactive. */
  153. if (IEC958 == substream->pcm->device)
  154. atc->spdif_out_passthru(atc, 0);
  155. /* The ct_atc_pcm object will be freed by runtime->private_free */
  156. return 0;
  157. }
  158. static int ct_pcm_hw_params(struct snd_pcm_substream *substream,
  159. struct snd_pcm_hw_params *hw_params)
  160. {
  161. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  162. struct ct_atc_pcm *apcm = substream->runtime->private_data;
  163. int err;
  164. err = snd_pcm_lib_malloc_pages(substream,
  165. params_buffer_bytes(hw_params));
  166. if (err < 0)
  167. return err;
  168. /* clear previous resources */
  169. atc->pcm_release_resources(atc, apcm);
  170. return err;
  171. }
  172. static int ct_pcm_hw_free(struct snd_pcm_substream *substream)
  173. {
  174. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  175. struct ct_atc_pcm *apcm = substream->runtime->private_data;
  176. /* clear previous resources */
  177. atc->pcm_release_resources(atc, apcm);
  178. /* Free snd-allocated pages */
  179. return snd_pcm_lib_free_pages(substream);
  180. }
  181. static int ct_pcm_playback_prepare(struct snd_pcm_substream *substream)
  182. {
  183. int err;
  184. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  185. struct snd_pcm_runtime *runtime = substream->runtime;
  186. struct ct_atc_pcm *apcm = runtime->private_data;
  187. if (IEC958 == substream->pcm->device)
  188. err = atc->spdif_passthru_playback_prepare(atc, apcm);
  189. else
  190. err = atc->pcm_playback_prepare(atc, apcm);
  191. if (err < 0) {
  192. dev_err(atc->card->dev,
  193. "Preparing pcm playback failed!!!\n");
  194. return err;
  195. }
  196. return 0;
  197. }
  198. static int
  199. ct_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  200. {
  201. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  202. struct snd_pcm_runtime *runtime = substream->runtime;
  203. struct ct_atc_pcm *apcm = runtime->private_data;
  204. switch (cmd) {
  205. case SNDRV_PCM_TRIGGER_START:
  206. case SNDRV_PCM_TRIGGER_RESUME:
  207. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  208. atc->pcm_playback_start(atc, apcm);
  209. break;
  210. case SNDRV_PCM_TRIGGER_STOP:
  211. case SNDRV_PCM_TRIGGER_SUSPEND:
  212. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  213. atc->pcm_playback_stop(atc, apcm);
  214. break;
  215. default:
  216. break;
  217. }
  218. return 0;
  219. }
  220. static snd_pcm_uframes_t
  221. ct_pcm_playback_pointer(struct snd_pcm_substream *substream)
  222. {
  223. unsigned long position;
  224. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  225. struct snd_pcm_runtime *runtime = substream->runtime;
  226. struct ct_atc_pcm *apcm = runtime->private_data;
  227. /* Read out playback position */
  228. position = atc->pcm_playback_position(atc, apcm);
  229. position = bytes_to_frames(runtime, position);
  230. if (position >= runtime->buffer_size)
  231. position = 0;
  232. return position;
  233. }
  234. /* pcm capture operations */
  235. static int ct_pcm_capture_open(struct snd_pcm_substream *substream)
  236. {
  237. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  238. struct snd_pcm_runtime *runtime = substream->runtime;
  239. struct ct_atc_pcm *apcm;
  240. int err;
  241. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  242. if (!apcm)
  243. return -ENOMEM;
  244. apcm->started = 0;
  245. apcm->substream = substream;
  246. apcm->interrupt = ct_atc_pcm_interrupt;
  247. runtime->hw = ct_pcm_capture_hw;
  248. runtime->hw.rate_max = atc->rsr * atc->msr;
  249. err = snd_pcm_hw_constraint_integer(runtime,
  250. SNDRV_PCM_HW_PARAM_PERIODS);
  251. if (err < 0)
  252. goto free_pcm;
  253. err = snd_pcm_hw_constraint_minmax(runtime,
  254. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  255. 1024, UINT_MAX);
  256. if (err < 0)
  257. goto free_pcm;
  258. apcm->timer = ct_timer_instance_new(atc->timer, apcm);
  259. if (!apcm->timer) {
  260. err = -ENOMEM;
  261. goto free_pcm;
  262. }
  263. runtime->private_data = apcm;
  264. runtime->private_free = ct_atc_pcm_free_substream;
  265. return 0;
  266. free_pcm:
  267. kfree(apcm);
  268. return err;
  269. }
  270. static int ct_pcm_capture_close(struct snd_pcm_substream *substream)
  271. {
  272. /* The ct_atc_pcm object will be freed by runtime->private_free */
  273. /* TODO: Notify mixer inactive. */
  274. return 0;
  275. }
  276. static int ct_pcm_capture_prepare(struct snd_pcm_substream *substream)
  277. {
  278. int err;
  279. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  280. struct snd_pcm_runtime *runtime = substream->runtime;
  281. struct ct_atc_pcm *apcm = runtime->private_data;
  282. err = atc->pcm_capture_prepare(atc, apcm);
  283. if (err < 0) {
  284. dev_err(atc->card->dev,
  285. "Preparing pcm capture failed!!!\n");
  286. return err;
  287. }
  288. return 0;
  289. }
  290. static int
  291. ct_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  292. {
  293. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  294. struct snd_pcm_runtime *runtime = substream->runtime;
  295. struct ct_atc_pcm *apcm = runtime->private_data;
  296. switch (cmd) {
  297. case SNDRV_PCM_TRIGGER_START:
  298. atc->pcm_capture_start(atc, apcm);
  299. break;
  300. case SNDRV_PCM_TRIGGER_STOP:
  301. atc->pcm_capture_stop(atc, apcm);
  302. break;
  303. default:
  304. atc->pcm_capture_stop(atc, apcm);
  305. break;
  306. }
  307. return 0;
  308. }
  309. static snd_pcm_uframes_t
  310. ct_pcm_capture_pointer(struct snd_pcm_substream *substream)
  311. {
  312. unsigned long position;
  313. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  314. struct snd_pcm_runtime *runtime = substream->runtime;
  315. struct ct_atc_pcm *apcm = runtime->private_data;
  316. /* Read out playback position */
  317. position = atc->pcm_capture_position(atc, apcm);
  318. position = bytes_to_frames(runtime, position);
  319. if (position >= runtime->buffer_size)
  320. position = 0;
  321. return position;
  322. }
  323. /* PCM operators for playback */
  324. static const struct snd_pcm_ops ct_pcm_playback_ops = {
  325. .open = ct_pcm_playback_open,
  326. .close = ct_pcm_playback_close,
  327. .ioctl = snd_pcm_lib_ioctl,
  328. .hw_params = ct_pcm_hw_params,
  329. .hw_free = ct_pcm_hw_free,
  330. .prepare = ct_pcm_playback_prepare,
  331. .trigger = ct_pcm_playback_trigger,
  332. .pointer = ct_pcm_playback_pointer,
  333. .page = snd_pcm_sgbuf_ops_page,
  334. };
  335. /* PCM operators for capture */
  336. static const struct snd_pcm_ops ct_pcm_capture_ops = {
  337. .open = ct_pcm_capture_open,
  338. .close = ct_pcm_capture_close,
  339. .ioctl = snd_pcm_lib_ioctl,
  340. .hw_params = ct_pcm_hw_params,
  341. .hw_free = ct_pcm_hw_free,
  342. .prepare = ct_pcm_capture_prepare,
  343. .trigger = ct_pcm_capture_trigger,
  344. .pointer = ct_pcm_capture_pointer,
  345. .page = snd_pcm_sgbuf_ops_page,
  346. };
  347. static const struct snd_pcm_chmap_elem surround_map[] = {
  348. { .channels = 1,
  349. .map = { SNDRV_CHMAP_MONO } },
  350. { .channels = 2,
  351. .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  352. { }
  353. };
  354. static const struct snd_pcm_chmap_elem clfe_map[] = {
  355. { .channels = 1,
  356. .map = { SNDRV_CHMAP_MONO } },
  357. { .channels = 2,
  358. .map = { SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
  359. { }
  360. };
  361. static const struct snd_pcm_chmap_elem side_map[] = {
  362. { .channels = 1,
  363. .map = { SNDRV_CHMAP_MONO } },
  364. { .channels = 2,
  365. .map = { SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  366. { }
  367. };
  368. /* Create ALSA pcm device */
  369. int ct_alsa_pcm_create(struct ct_atc *atc,
  370. enum CTALSADEVS device,
  371. const char *device_name)
  372. {
  373. struct snd_pcm *pcm;
  374. const struct snd_pcm_chmap_elem *map;
  375. int chs;
  376. int err;
  377. int playback_count, capture_count;
  378. playback_count = (IEC958 == device) ? 1 : 256;
  379. capture_count = (FRONT == device) ? 1 : 0;
  380. err = snd_pcm_new(atc->card, "ctxfi", device,
  381. playback_count, capture_count, &pcm);
  382. if (err < 0) {
  383. dev_err(atc->card->dev, "snd_pcm_new failed!! Err=%d\n",
  384. err);
  385. return err;
  386. }
  387. pcm->private_data = atc;
  388. pcm->info_flags = 0;
  389. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  390. strlcpy(pcm->name, device_name, sizeof(pcm->name));
  391. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ct_pcm_playback_ops);
  392. if (FRONT == device)
  393. snd_pcm_set_ops(pcm,
  394. SNDRV_PCM_STREAM_CAPTURE, &ct_pcm_capture_ops);
  395. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  396. snd_dma_pci_data(atc->pci), 128*1024, 128*1024);
  397. chs = 2;
  398. switch (device) {
  399. case FRONT:
  400. chs = 8;
  401. map = snd_pcm_std_chmaps;
  402. break;
  403. case SURROUND:
  404. map = surround_map;
  405. break;
  406. case CLFE:
  407. map = clfe_map;
  408. break;
  409. case SIDE:
  410. map = side_map;
  411. break;
  412. default:
  413. map = snd_pcm_std_chmaps;
  414. break;
  415. }
  416. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, map, chs,
  417. 0, NULL);
  418. if (err < 0)
  419. return err;
  420. #ifdef CONFIG_PM_SLEEP
  421. atc->pcms[device] = pcm;
  422. #endif
  423. return 0;
  424. }