ctpcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 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 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 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. kfree(apcm);
  132. return err;
  133. }
  134. err = snd_pcm_hw_constraint_minmax(runtime,
  135. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  136. 1024, UINT_MAX);
  137. if (err < 0) {
  138. kfree(apcm);
  139. return err;
  140. }
  141. apcm->timer = ct_timer_instance_new(atc->timer, apcm);
  142. if (!apcm->timer) {
  143. kfree(apcm);
  144. return -ENOMEM;
  145. }
  146. runtime->private_data = apcm;
  147. runtime->private_free = ct_atc_pcm_free_substream;
  148. return 0;
  149. }
  150. static int ct_pcm_playback_close(struct snd_pcm_substream *substream)
  151. {
  152. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  153. /* TODO: Notify mixer inactive. */
  154. if (IEC958 == substream->pcm->device)
  155. atc->spdif_out_passthru(atc, 0);
  156. /* The ct_atc_pcm object will be freed by runtime->private_free */
  157. return 0;
  158. }
  159. static int ct_pcm_hw_params(struct snd_pcm_substream *substream,
  160. struct snd_pcm_hw_params *hw_params)
  161. {
  162. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  163. struct ct_atc_pcm *apcm = substream->runtime->private_data;
  164. int err;
  165. err = snd_pcm_lib_malloc_pages(substream,
  166. params_buffer_bytes(hw_params));
  167. if (err < 0)
  168. return err;
  169. /* clear previous resources */
  170. atc->pcm_release_resources(atc, apcm);
  171. return err;
  172. }
  173. static int ct_pcm_hw_free(struct snd_pcm_substream *substream)
  174. {
  175. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  176. struct ct_atc_pcm *apcm = substream->runtime->private_data;
  177. /* clear previous resources */
  178. atc->pcm_release_resources(atc, apcm);
  179. /* Free snd-allocated pages */
  180. return snd_pcm_lib_free_pages(substream);
  181. }
  182. static int ct_pcm_playback_prepare(struct snd_pcm_substream *substream)
  183. {
  184. int err;
  185. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  186. struct snd_pcm_runtime *runtime = substream->runtime;
  187. struct ct_atc_pcm *apcm = runtime->private_data;
  188. if (IEC958 == substream->pcm->device)
  189. err = atc->spdif_passthru_playback_prepare(atc, apcm);
  190. else
  191. err = atc->pcm_playback_prepare(atc, apcm);
  192. if (err < 0) {
  193. dev_err(atc->card->dev,
  194. "Preparing pcm playback failed!!!\n");
  195. return err;
  196. }
  197. return 0;
  198. }
  199. static int
  200. ct_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  201. {
  202. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  203. struct snd_pcm_runtime *runtime = substream->runtime;
  204. struct ct_atc_pcm *apcm = runtime->private_data;
  205. switch (cmd) {
  206. case SNDRV_PCM_TRIGGER_START:
  207. case SNDRV_PCM_TRIGGER_RESUME:
  208. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  209. atc->pcm_playback_start(atc, apcm);
  210. break;
  211. case SNDRV_PCM_TRIGGER_STOP:
  212. case SNDRV_PCM_TRIGGER_SUSPEND:
  213. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  214. atc->pcm_playback_stop(atc, apcm);
  215. break;
  216. default:
  217. break;
  218. }
  219. return 0;
  220. }
  221. static snd_pcm_uframes_t
  222. ct_pcm_playback_pointer(struct snd_pcm_substream *substream)
  223. {
  224. unsigned long position;
  225. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  226. struct snd_pcm_runtime *runtime = substream->runtime;
  227. struct ct_atc_pcm *apcm = runtime->private_data;
  228. /* Read out playback position */
  229. position = atc->pcm_playback_position(atc, apcm);
  230. position = bytes_to_frames(runtime, position);
  231. if (position >= runtime->buffer_size)
  232. position = 0;
  233. return position;
  234. }
  235. /* pcm capture operations */
  236. static int ct_pcm_capture_open(struct snd_pcm_substream *substream)
  237. {
  238. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  239. struct snd_pcm_runtime *runtime = substream->runtime;
  240. struct ct_atc_pcm *apcm;
  241. int err;
  242. apcm = kzalloc(sizeof(*apcm), GFP_KERNEL);
  243. if (!apcm)
  244. return -ENOMEM;
  245. apcm->started = 0;
  246. apcm->substream = substream;
  247. apcm->interrupt = ct_atc_pcm_interrupt;
  248. runtime->hw = ct_pcm_capture_hw;
  249. runtime->hw.rate_max = atc->rsr * atc->msr;
  250. err = snd_pcm_hw_constraint_integer(runtime,
  251. SNDRV_PCM_HW_PARAM_PERIODS);
  252. if (err < 0) {
  253. kfree(apcm);
  254. return err;
  255. }
  256. err = snd_pcm_hw_constraint_minmax(runtime,
  257. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  258. 1024, UINT_MAX);
  259. if (err < 0) {
  260. kfree(apcm);
  261. return err;
  262. }
  263. apcm->timer = ct_timer_instance_new(atc->timer, apcm);
  264. if (!apcm->timer) {
  265. kfree(apcm);
  266. return -ENOMEM;
  267. }
  268. runtime->private_data = apcm;
  269. runtime->private_free = ct_atc_pcm_free_substream;
  270. return 0;
  271. }
  272. static int ct_pcm_capture_close(struct snd_pcm_substream *substream)
  273. {
  274. /* The ct_atc_pcm object will be freed by runtime->private_free */
  275. /* TODO: Notify mixer inactive. */
  276. return 0;
  277. }
  278. static int ct_pcm_capture_prepare(struct snd_pcm_substream *substream)
  279. {
  280. int err;
  281. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  282. struct snd_pcm_runtime *runtime = substream->runtime;
  283. struct ct_atc_pcm *apcm = runtime->private_data;
  284. err = atc->pcm_capture_prepare(atc, apcm);
  285. if (err < 0) {
  286. dev_err(atc->card->dev,
  287. "Preparing pcm capture failed!!!\n");
  288. return err;
  289. }
  290. return 0;
  291. }
  292. static int
  293. ct_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  294. {
  295. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  296. struct snd_pcm_runtime *runtime = substream->runtime;
  297. struct ct_atc_pcm *apcm = runtime->private_data;
  298. switch (cmd) {
  299. case SNDRV_PCM_TRIGGER_START:
  300. atc->pcm_capture_start(atc, apcm);
  301. break;
  302. case SNDRV_PCM_TRIGGER_STOP:
  303. atc->pcm_capture_stop(atc, apcm);
  304. break;
  305. default:
  306. atc->pcm_capture_stop(atc, apcm);
  307. break;
  308. }
  309. return 0;
  310. }
  311. static snd_pcm_uframes_t
  312. ct_pcm_capture_pointer(struct snd_pcm_substream *substream)
  313. {
  314. unsigned long position;
  315. struct ct_atc *atc = snd_pcm_substream_chip(substream);
  316. struct snd_pcm_runtime *runtime = substream->runtime;
  317. struct ct_atc_pcm *apcm = runtime->private_data;
  318. /* Read out playback position */
  319. position = atc->pcm_capture_position(atc, apcm);
  320. position = bytes_to_frames(runtime, position);
  321. if (position >= runtime->buffer_size)
  322. position = 0;
  323. return position;
  324. }
  325. /* PCM operators for playback */
  326. static struct snd_pcm_ops ct_pcm_playback_ops = {
  327. .open = ct_pcm_playback_open,
  328. .close = ct_pcm_playback_close,
  329. .ioctl = snd_pcm_lib_ioctl,
  330. .hw_params = ct_pcm_hw_params,
  331. .hw_free = ct_pcm_hw_free,
  332. .prepare = ct_pcm_playback_prepare,
  333. .trigger = ct_pcm_playback_trigger,
  334. .pointer = ct_pcm_playback_pointer,
  335. .page = snd_pcm_sgbuf_ops_page,
  336. };
  337. /* PCM operators for capture */
  338. static struct snd_pcm_ops ct_pcm_capture_ops = {
  339. .open = ct_pcm_capture_open,
  340. .close = ct_pcm_capture_close,
  341. .ioctl = snd_pcm_lib_ioctl,
  342. .hw_params = ct_pcm_hw_params,
  343. .hw_free = ct_pcm_hw_free,
  344. .prepare = ct_pcm_capture_prepare,
  345. .trigger = ct_pcm_capture_trigger,
  346. .pointer = ct_pcm_capture_pointer,
  347. .page = snd_pcm_sgbuf_ops_page,
  348. };
  349. static const struct snd_pcm_chmap_elem surround_map[] = {
  350. { .channels = 1,
  351. .map = { SNDRV_CHMAP_MONO } },
  352. { .channels = 2,
  353. .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  354. { }
  355. };
  356. static const struct snd_pcm_chmap_elem clfe_map[] = {
  357. { .channels = 1,
  358. .map = { SNDRV_CHMAP_MONO } },
  359. { .channels = 2,
  360. .map = { SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
  361. { }
  362. };
  363. static const struct snd_pcm_chmap_elem side_map[] = {
  364. { .channels = 1,
  365. .map = { SNDRV_CHMAP_MONO } },
  366. { .channels = 2,
  367. .map = { SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  368. { }
  369. };
  370. /* Create ALSA pcm device */
  371. int ct_alsa_pcm_create(struct ct_atc *atc,
  372. enum CTALSADEVS device,
  373. const char *device_name)
  374. {
  375. struct snd_pcm *pcm;
  376. const struct snd_pcm_chmap_elem *map;
  377. int chs;
  378. int err;
  379. int playback_count, capture_count;
  380. playback_count = (IEC958 == device) ? 1 : 256;
  381. capture_count = (FRONT == device) ? 1 : 0;
  382. err = snd_pcm_new(atc->card, "ctxfi", device,
  383. playback_count, capture_count, &pcm);
  384. if (err < 0) {
  385. dev_err(atc->card->dev, "snd_pcm_new failed!! Err=%d\n",
  386. err);
  387. return err;
  388. }
  389. pcm->private_data = atc;
  390. pcm->info_flags = 0;
  391. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  392. strlcpy(pcm->name, device_name, sizeof(pcm->name));
  393. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ct_pcm_playback_ops);
  394. if (FRONT == device)
  395. snd_pcm_set_ops(pcm,
  396. SNDRV_PCM_STREAM_CAPTURE, &ct_pcm_capture_ops);
  397. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
  398. snd_dma_pci_data(atc->pci), 128*1024, 128*1024);
  399. chs = 2;
  400. switch (device) {
  401. case FRONT:
  402. chs = 8;
  403. map = snd_pcm_std_chmaps;
  404. break;
  405. case SURROUND:
  406. map = surround_map;
  407. break;
  408. case CLFE:
  409. map = clfe_map;
  410. break;
  411. case SIDE:
  412. map = side_map;
  413. break;
  414. default:
  415. map = snd_pcm_std_chmaps;
  416. break;
  417. }
  418. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, map, chs,
  419. 0, NULL);
  420. if (err < 0)
  421. return err;
  422. #ifdef CONFIG_PM_SLEEP
  423. atc->pcms[device] = pcm;
  424. #endif
  425. return 0;
  426. }