tm6000-alsa.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Support for audio capture for tm5600/6000/6010
  3. // Copyright (c) 2007-2008 Mauro Carvalho Chehab <mchehab@kernel.org>
  4. //
  5. // Based on cx88-alsa.c
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/device.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/usb.h>
  11. #include <linux/slab.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/delay.h>
  14. #include <sound/core.h>
  15. #include <sound/pcm.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/control.h>
  18. #include <sound/initval.h>
  19. #include "tm6000.h"
  20. #include "tm6000-regs.h"
  21. #undef dprintk
  22. #define dprintk(level, fmt, arg...) do { \
  23. if (debug >= level) \
  24. printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
  25. } while (0)
  26. /****************************************************************************
  27. Module global static vars
  28. ****************************************************************************/
  29. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  30. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  31. module_param_array(enable, bool, NULL, 0444);
  32. MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
  33. module_param_array(index, int, NULL, 0444);
  34. MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
  35. /****************************************************************************
  36. Module macros
  37. ****************************************************************************/
  38. MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
  39. MODULE_AUTHOR("Mauro Carvalho Chehab");
  40. MODULE_LICENSE("GPL v2");
  41. MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},{{Trident,tm6000},{{Trident,tm6010}");
  42. static unsigned int debug;
  43. module_param(debug, int, 0644);
  44. MODULE_PARM_DESC(debug, "enable debug messages");
  45. /****************************************************************************
  46. Module specific funtions
  47. ****************************************************************************/
  48. /*
  49. * BOARD Specific: Sets audio DMA
  50. */
  51. static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
  52. {
  53. struct tm6000_core *core = chip->core;
  54. dprintk(1, "Starting audio DMA\n");
  55. /* Enables audio */
  56. tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x40, 0x40);
  57. tm6000_set_audio_bitrate(core, 48000);
  58. return 0;
  59. }
  60. /*
  61. * BOARD Specific: Resets audio DMA
  62. */
  63. static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
  64. {
  65. struct tm6000_core *core = chip->core;
  66. dprintk(1, "Stopping audio DMA\n");
  67. /* Disables audio */
  68. tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x00, 0x40);
  69. return 0;
  70. }
  71. static void dsp_buffer_free(struct snd_pcm_substream *substream)
  72. {
  73. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  74. dprintk(2, "Freeing buffer\n");
  75. vfree(substream->runtime->dma_area);
  76. substream->runtime->dma_area = NULL;
  77. substream->runtime->dma_bytes = 0;
  78. }
  79. static int dsp_buffer_alloc(struct snd_pcm_substream *substream, int size)
  80. {
  81. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  82. dprintk(2, "Allocating buffer\n");
  83. if (substream->runtime->dma_area) {
  84. if (substream->runtime->dma_bytes > size)
  85. return 0;
  86. dsp_buffer_free(substream);
  87. }
  88. substream->runtime->dma_area = vmalloc(size);
  89. if (!substream->runtime->dma_area)
  90. return -ENOMEM;
  91. substream->runtime->dma_bytes = size;
  92. return 0;
  93. }
  94. /****************************************************************************
  95. ALSA PCM Interface
  96. ****************************************************************************/
  97. /*
  98. * Digital hardware definition
  99. */
  100. #define DEFAULT_FIFO_SIZE 4096
  101. static const struct snd_pcm_hardware snd_tm6000_digital_hw = {
  102. .info = SNDRV_PCM_INFO_BATCH |
  103. SNDRV_PCM_INFO_MMAP |
  104. SNDRV_PCM_INFO_INTERLEAVED |
  105. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  106. SNDRV_PCM_INFO_MMAP_VALID,
  107. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  108. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
  109. .rate_min = 48000,
  110. .rate_max = 48000,
  111. .channels_min = 2,
  112. .channels_max = 2,
  113. .period_bytes_min = 64,
  114. .period_bytes_max = 12544,
  115. .periods_min = 2,
  116. .periods_max = 98,
  117. .buffer_bytes_max = 62720 * 8,
  118. };
  119. /*
  120. * audio pcm capture open callback
  121. */
  122. static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
  123. {
  124. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  125. struct snd_pcm_runtime *runtime = substream->runtime;
  126. int err;
  127. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  128. SNDRV_PCM_HW_PARAM_PERIODS);
  129. if (err < 0)
  130. goto _error;
  131. chip->substream = substream;
  132. runtime->hw = snd_tm6000_digital_hw;
  133. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  134. return 0;
  135. _error:
  136. dprintk(1, "Error opening PCM!\n");
  137. return err;
  138. }
  139. /*
  140. * audio close callback
  141. */
  142. static int snd_tm6000_close(struct snd_pcm_substream *substream)
  143. {
  144. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  145. struct tm6000_core *core = chip->core;
  146. if (atomic_read(&core->stream_started) > 0) {
  147. atomic_set(&core->stream_started, 0);
  148. schedule_work(&core->wq_trigger);
  149. }
  150. return 0;
  151. }
  152. static int tm6000_fillbuf(struct tm6000_core *core, char *buf, int size)
  153. {
  154. struct snd_tm6000_card *chip = core->adev;
  155. struct snd_pcm_substream *substream = chip->substream;
  156. struct snd_pcm_runtime *runtime;
  157. int period_elapsed = 0;
  158. unsigned int stride, buf_pos;
  159. int length;
  160. if (atomic_read(&core->stream_started) == 0)
  161. return 0;
  162. if (!size || !substream) {
  163. dprintk(1, "substream was NULL\n");
  164. return -EINVAL;
  165. }
  166. runtime = substream->runtime;
  167. if (!runtime || !runtime->dma_area) {
  168. dprintk(1, "runtime was NULL\n");
  169. return -EINVAL;
  170. }
  171. buf_pos = chip->buf_pos;
  172. stride = runtime->frame_bits >> 3;
  173. if (stride == 0) {
  174. dprintk(1, "stride is zero\n");
  175. return -EINVAL;
  176. }
  177. length = size / stride;
  178. if (length == 0) {
  179. dprintk(1, "%s: length was zero\n", __func__);
  180. return -EINVAL;
  181. }
  182. dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size,
  183. runtime->dma_area, buf_pos,
  184. (unsigned int)runtime->buffer_size, stride);
  185. if (buf_pos + length >= runtime->buffer_size) {
  186. unsigned int cnt = runtime->buffer_size - buf_pos;
  187. memcpy(runtime->dma_area + buf_pos * stride, buf, cnt * stride);
  188. memcpy(runtime->dma_area, buf + cnt * stride,
  189. length * stride - cnt * stride);
  190. } else
  191. memcpy(runtime->dma_area + buf_pos * stride, buf,
  192. length * stride);
  193. snd_pcm_stream_lock(substream);
  194. chip->buf_pos += length;
  195. if (chip->buf_pos >= runtime->buffer_size)
  196. chip->buf_pos -= runtime->buffer_size;
  197. chip->period_pos += length;
  198. if (chip->period_pos >= runtime->period_size) {
  199. chip->period_pos -= runtime->period_size;
  200. period_elapsed = 1;
  201. }
  202. snd_pcm_stream_unlock(substream);
  203. if (period_elapsed)
  204. snd_pcm_period_elapsed(substream);
  205. return 0;
  206. }
  207. /*
  208. * hw_params callback
  209. */
  210. static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
  211. struct snd_pcm_hw_params *hw_params)
  212. {
  213. int size, rc;
  214. size = params_period_bytes(hw_params) * params_periods(hw_params);
  215. rc = dsp_buffer_alloc(substream, size);
  216. if (rc < 0)
  217. return rc;
  218. return 0;
  219. }
  220. /*
  221. * hw free callback
  222. */
  223. static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
  224. {
  225. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  226. struct tm6000_core *core = chip->core;
  227. if (atomic_read(&core->stream_started) > 0) {
  228. atomic_set(&core->stream_started, 0);
  229. schedule_work(&core->wq_trigger);
  230. }
  231. dsp_buffer_free(substream);
  232. return 0;
  233. }
  234. /*
  235. * prepare callback
  236. */
  237. static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
  238. {
  239. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  240. chip->buf_pos = 0;
  241. chip->period_pos = 0;
  242. return 0;
  243. }
  244. /*
  245. * trigger callback
  246. */
  247. static void audio_trigger(struct work_struct *work)
  248. {
  249. struct tm6000_core *core = container_of(work, struct tm6000_core,
  250. wq_trigger);
  251. struct snd_tm6000_card *chip = core->adev;
  252. if (atomic_read(&core->stream_started)) {
  253. dprintk(1, "starting capture");
  254. _tm6000_start_audio_dma(chip);
  255. } else {
  256. dprintk(1, "stopping capture");
  257. _tm6000_stop_audio_dma(chip);
  258. }
  259. }
  260. static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
  261. {
  262. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  263. struct tm6000_core *core = chip->core;
  264. int err = 0;
  265. switch (cmd) {
  266. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  267. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  268. case SNDRV_PCM_TRIGGER_START:
  269. atomic_set(&core->stream_started, 1);
  270. break;
  271. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  272. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  273. case SNDRV_PCM_TRIGGER_STOP:
  274. atomic_set(&core->stream_started, 0);
  275. break;
  276. default:
  277. err = -EINVAL;
  278. break;
  279. }
  280. schedule_work(&core->wq_trigger);
  281. return err;
  282. }
  283. /*
  284. * pointer callback
  285. */
  286. static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
  287. {
  288. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  289. return chip->buf_pos;
  290. }
  291. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  292. unsigned long offset)
  293. {
  294. void *pageptr = subs->runtime->dma_area + offset;
  295. return vmalloc_to_page(pageptr);
  296. }
  297. /*
  298. * operators
  299. */
  300. static const struct snd_pcm_ops snd_tm6000_pcm_ops = {
  301. .open = snd_tm6000_pcm_open,
  302. .close = snd_tm6000_close,
  303. .ioctl = snd_pcm_lib_ioctl,
  304. .hw_params = snd_tm6000_hw_params,
  305. .hw_free = snd_tm6000_hw_free,
  306. .prepare = snd_tm6000_prepare,
  307. .trigger = snd_tm6000_card_trigger,
  308. .pointer = snd_tm6000_pointer,
  309. .page = snd_pcm_get_vmalloc_page,
  310. };
  311. /*
  312. * create a PCM device
  313. */
  314. /* FIXME: Control interface - How to control volume/mute? */
  315. /****************************************************************************
  316. Basic Flow for Sound Devices
  317. ****************************************************************************/
  318. /*
  319. * Alsa Constructor - Component probe
  320. */
  321. static int tm6000_audio_init(struct tm6000_core *dev)
  322. {
  323. struct snd_card *card;
  324. struct snd_tm6000_card *chip;
  325. int rc;
  326. static int devnr;
  327. char component[14];
  328. struct snd_pcm *pcm;
  329. if (!dev)
  330. return 0;
  331. if (devnr >= SNDRV_CARDS)
  332. return -ENODEV;
  333. if (!enable[devnr])
  334. return -ENOENT;
  335. rc = snd_card_new(&dev->udev->dev, index[devnr], "tm6000",
  336. THIS_MODULE, 0, &card);
  337. if (rc < 0) {
  338. snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
  339. return rc;
  340. }
  341. strcpy(card->driver, "tm6000-alsa");
  342. strcpy(card->shortname, "TM5600/60x0");
  343. sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
  344. dev->udev->bus->busnum, dev->udev->devnum);
  345. sprintf(component, "USB%04x:%04x",
  346. le16_to_cpu(dev->udev->descriptor.idVendor),
  347. le16_to_cpu(dev->udev->descriptor.idProduct));
  348. snd_component_add(card, component);
  349. chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
  350. if (!chip) {
  351. rc = -ENOMEM;
  352. goto error;
  353. }
  354. chip->core = dev;
  355. chip->card = card;
  356. dev->adev = chip;
  357. spin_lock_init(&chip->reg_lock);
  358. rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
  359. if (rc < 0)
  360. goto error_chip;
  361. pcm->info_flags = 0;
  362. pcm->private_data = chip;
  363. strcpy(pcm->name, "Trident TM5600/60x0");
  364. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
  365. INIT_WORK(&dev->wq_trigger, audio_trigger);
  366. rc = snd_card_register(card);
  367. if (rc < 0)
  368. goto error_chip;
  369. dprintk(1, "Registered audio driver for %s\n", card->longname);
  370. return 0;
  371. error_chip:
  372. kfree(chip);
  373. dev->adev = NULL;
  374. error:
  375. snd_card_free(card);
  376. return rc;
  377. }
  378. static int tm6000_audio_fini(struct tm6000_core *dev)
  379. {
  380. struct snd_tm6000_card *chip;
  381. if (!dev)
  382. return 0;
  383. chip = dev->adev;
  384. if (!chip)
  385. return 0;
  386. if (!chip->card)
  387. return 0;
  388. snd_card_free(chip->card);
  389. chip->card = NULL;
  390. kfree(chip);
  391. dev->adev = NULL;
  392. return 0;
  393. }
  394. static struct tm6000_ops audio_ops = {
  395. .type = TM6000_AUDIO,
  396. .name = "TM6000 Audio Extension",
  397. .init = tm6000_audio_init,
  398. .fini = tm6000_audio_fini,
  399. .fillbuf = tm6000_fillbuf,
  400. };
  401. static int __init tm6000_alsa_register(void)
  402. {
  403. return tm6000_register_extension(&audio_ops);
  404. }
  405. static void __exit tm6000_alsa_unregister(void)
  406. {
  407. tm6000_unregister_extension(&audio_ops);
  408. }
  409. module_init(tm6000_alsa_register);
  410. module_exit(tm6000_alsa_unregister);