cx23885-alsa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Support for CX23885 analog audio capture
  5. *
  6. * (c) 2008 Mijhail Moreyra <[email protected]>
  7. * Adapted from cx88-alsa.c
  8. * (c) 2009 Steven Toth <[email protected]>
  9. */
  10. #include "cx23885.h"
  11. #include "cx23885-reg.h"
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/pci.h>
  19. #include <asm/delay.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/control.h>
  24. #include <sound/initval.h>
  25. #include <sound/tlv.h>
  26. #define AUDIO_SRAM_CHANNEL SRAM_CH07
  27. #define dprintk(level, fmt, arg...) do { \
  28. if (audio_debug + 1 > level) \
  29. printk(KERN_DEBUG pr_fmt("%s: alsa: " fmt), \
  30. chip->dev->name, ##arg); \
  31. } while(0)
  32. /****************************************************************************
  33. Module global static vars
  34. ****************************************************************************/
  35. static unsigned int disable_analog_audio;
  36. module_param(disable_analog_audio, int, 0644);
  37. MODULE_PARM_DESC(disable_analog_audio, "disable analog audio ALSA driver");
  38. static unsigned int audio_debug;
  39. module_param(audio_debug, int, 0644);
  40. MODULE_PARM_DESC(audio_debug, "enable debug messages [analog audio]");
  41. /****************************************************************************
  42. Board specific functions
  43. ****************************************************************************/
  44. /* Constants taken from cx88-reg.h */
  45. #define AUD_INT_DN_RISCI1 (1 << 0)
  46. #define AUD_INT_UP_RISCI1 (1 << 1)
  47. #define AUD_INT_RDS_DN_RISCI1 (1 << 2)
  48. #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
  49. #define AUD_INT_UP_RISCI2 (1 << 5)
  50. #define AUD_INT_RDS_DN_RISCI2 (1 << 6)
  51. #define AUD_INT_DN_SYNC (1 << 12)
  52. #define AUD_INT_UP_SYNC (1 << 13)
  53. #define AUD_INT_RDS_DN_SYNC (1 << 14)
  54. #define AUD_INT_OPC_ERR (1 << 16)
  55. #define AUD_INT_BER_IRQ (1 << 20)
  56. #define AUD_INT_MCHG_IRQ (1 << 21)
  57. #define GP_COUNT_CONTROL_RESET 0x3
  58. static int cx23885_alsa_dma_init(struct cx23885_audio_dev *chip,
  59. unsigned long nr_pages)
  60. {
  61. struct cx23885_audio_buffer *buf = chip->buf;
  62. struct page *pg;
  63. int i;
  64. buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
  65. if (NULL == buf->vaddr) {
  66. dprintk(1, "vmalloc_32(%lu pages) failed\n", nr_pages);
  67. return -ENOMEM;
  68. }
  69. dprintk(1, "vmalloc is at addr %p, size=%lu\n",
  70. buf->vaddr, nr_pages << PAGE_SHIFT);
  71. memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
  72. buf->nr_pages = nr_pages;
  73. buf->sglist = vzalloc(array_size(sizeof(*buf->sglist), buf->nr_pages));
  74. if (NULL == buf->sglist)
  75. goto vzalloc_err;
  76. sg_init_table(buf->sglist, buf->nr_pages);
  77. for (i = 0; i < buf->nr_pages; i++) {
  78. pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
  79. if (NULL == pg)
  80. goto vmalloc_to_page_err;
  81. sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
  82. }
  83. return 0;
  84. vmalloc_to_page_err:
  85. vfree(buf->sglist);
  86. buf->sglist = NULL;
  87. vzalloc_err:
  88. vfree(buf->vaddr);
  89. buf->vaddr = NULL;
  90. return -ENOMEM;
  91. }
  92. static int cx23885_alsa_dma_map(struct cx23885_audio_dev *dev)
  93. {
  94. struct cx23885_audio_buffer *buf = dev->buf;
  95. buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
  96. buf->nr_pages, DMA_FROM_DEVICE);
  97. if (0 == buf->sglen) {
  98. pr_warn("%s: cx23885_alsa_map_sg failed\n", __func__);
  99. return -ENOMEM;
  100. }
  101. return 0;
  102. }
  103. static int cx23885_alsa_dma_unmap(struct cx23885_audio_dev *dev)
  104. {
  105. struct cx23885_audio_buffer *buf = dev->buf;
  106. if (!buf->sglen)
  107. return 0;
  108. dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->nr_pages, DMA_FROM_DEVICE);
  109. buf->sglen = 0;
  110. return 0;
  111. }
  112. static int cx23885_alsa_dma_free(struct cx23885_audio_buffer *buf)
  113. {
  114. vfree(buf->sglist);
  115. buf->sglist = NULL;
  116. vfree(buf->vaddr);
  117. buf->vaddr = NULL;
  118. return 0;
  119. }
  120. /*
  121. * BOARD Specific: Sets audio DMA
  122. */
  123. static int cx23885_start_audio_dma(struct cx23885_audio_dev *chip)
  124. {
  125. struct cx23885_audio_buffer *buf = chip->buf;
  126. struct cx23885_dev *dev = chip->dev;
  127. struct sram_channel *audio_ch =
  128. &dev->sram_channels[AUDIO_SRAM_CHANNEL];
  129. dprintk(1, "%s()\n", __func__);
  130. /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
  131. cx_clear(AUD_INT_DMA_CTL, 0x11);
  132. /* setup fifo + format - out channel */
  133. cx23885_sram_channel_setup(chip->dev, audio_ch, buf->bpl,
  134. buf->risc.dma);
  135. /* sets bpl size */
  136. cx_write(AUD_INT_A_LNGTH, buf->bpl);
  137. /* This is required to get good audio (1 seems to be ok) */
  138. cx_write(AUD_INT_A_MODE, 1);
  139. /* reset counter */
  140. cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
  141. atomic_set(&chip->count, 0);
  142. dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d byte buffer\n",
  143. buf->bpl, cx_read(audio_ch->cmds_start+12)>>1,
  144. chip->num_periods, buf->bpl * chip->num_periods);
  145. /* Enables corresponding bits at AUD_INT_STAT */
  146. cx_write(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  147. AUD_INT_DN_RISCI1);
  148. /* Clean any pending interrupt bits already set */
  149. cx_write(AUDIO_INT_INT_STAT, ~0);
  150. /* enable audio irqs */
  151. cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
  152. /* start dma */
  153. cx_set(DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
  154. cx_set(AUD_INT_DMA_CTL, 0x11); /* audio downstream FIFO and
  155. RISC enable */
  156. if (audio_debug)
  157. cx23885_sram_channel_dump(chip->dev, audio_ch);
  158. return 0;
  159. }
  160. /*
  161. * BOARD Specific: Resets audio DMA
  162. */
  163. static int cx23885_stop_audio_dma(struct cx23885_audio_dev *chip)
  164. {
  165. struct cx23885_dev *dev = chip->dev;
  166. dprintk(1, "Stopping audio DMA\n");
  167. /* stop dma */
  168. cx_clear(AUD_INT_DMA_CTL, 0x11);
  169. /* disable irqs */
  170. cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
  171. cx_clear(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  172. AUD_INT_DN_RISCI1);
  173. if (audio_debug)
  174. cx23885_sram_channel_dump(chip->dev,
  175. &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
  176. return 0;
  177. }
  178. /*
  179. * BOARD Specific: Handles audio IRQ
  180. */
  181. int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask)
  182. {
  183. struct cx23885_audio_dev *chip = dev->audio_dev;
  184. if (0 == (status & mask))
  185. return 0;
  186. cx_write(AUDIO_INT_INT_STAT, status);
  187. /* risc op code error */
  188. if (status & AUD_INT_OPC_ERR) {
  189. pr_warn("%s/1: Audio risc op code error\n",
  190. dev->name);
  191. cx_clear(AUD_INT_DMA_CTL, 0x11);
  192. cx23885_sram_channel_dump(dev,
  193. &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
  194. }
  195. if (status & AUD_INT_DN_SYNC) {
  196. dprintk(1, "Downstream sync error\n");
  197. cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
  198. return 1;
  199. }
  200. /* risc1 downstream */
  201. if (status & AUD_INT_DN_RISCI1) {
  202. atomic_set(&chip->count, cx_read(AUD_INT_A_GPCNT));
  203. snd_pcm_period_elapsed(chip->substream);
  204. }
  205. /* FIXME: Any other status should deserve a special handling? */
  206. return 1;
  207. }
  208. static int dsp_buffer_free(struct cx23885_audio_dev *chip)
  209. {
  210. struct cx23885_riscmem *risc;
  211. BUG_ON(!chip->dma_size);
  212. dprintk(2, "Freeing buffer\n");
  213. cx23885_alsa_dma_unmap(chip);
  214. cx23885_alsa_dma_free(chip->buf);
  215. risc = &chip->buf->risc;
  216. dma_free_coherent(&chip->pci->dev, risc->size, risc->cpu, risc->dma);
  217. kfree(chip->buf);
  218. chip->buf = NULL;
  219. chip->dma_size = 0;
  220. return 0;
  221. }
  222. /****************************************************************************
  223. ALSA PCM Interface
  224. ****************************************************************************/
  225. /*
  226. * Digital hardware definition
  227. */
  228. #define DEFAULT_FIFO_SIZE 4096
  229. static const struct snd_pcm_hardware snd_cx23885_digital_hw = {
  230. .info = SNDRV_PCM_INFO_MMAP |
  231. SNDRV_PCM_INFO_INTERLEAVED |
  232. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  233. SNDRV_PCM_INFO_MMAP_VALID,
  234. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  235. .rates = SNDRV_PCM_RATE_48000,
  236. .rate_min = 48000,
  237. .rate_max = 48000,
  238. .channels_min = 2,
  239. .channels_max = 2,
  240. /* Analog audio output will be full of clicks and pops if there
  241. are not exactly four lines in the SRAM FIFO buffer. */
  242. .period_bytes_min = DEFAULT_FIFO_SIZE/4,
  243. .period_bytes_max = DEFAULT_FIFO_SIZE/4,
  244. .periods_min = 1,
  245. .periods_max = 1024,
  246. .buffer_bytes_max = (1024*1024),
  247. };
  248. /*
  249. * audio pcm capture open callback
  250. */
  251. static int snd_cx23885_pcm_open(struct snd_pcm_substream *substream)
  252. {
  253. struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
  254. struct snd_pcm_runtime *runtime = substream->runtime;
  255. int err;
  256. if (!chip) {
  257. pr_err("BUG: cx23885 can't find device struct. Can't proceed with open\n");
  258. return -ENODEV;
  259. }
  260. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  261. SNDRV_PCM_HW_PARAM_PERIODS);
  262. if (err < 0)
  263. goto _error;
  264. chip->substream = substream;
  265. runtime->hw = snd_cx23885_digital_hw;
  266. if (chip->dev->sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
  267. DEFAULT_FIFO_SIZE) {
  268. unsigned int bpl = chip->dev->
  269. sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 4;
  270. bpl &= ~7; /* must be multiple of 8 */
  271. runtime->hw.period_bytes_min = bpl;
  272. runtime->hw.period_bytes_max = bpl;
  273. }
  274. return 0;
  275. _error:
  276. dprintk(1, "Error opening PCM!\n");
  277. return err;
  278. }
  279. /*
  280. * audio close callback
  281. */
  282. static int snd_cx23885_close(struct snd_pcm_substream *substream)
  283. {
  284. return 0;
  285. }
  286. /*
  287. * hw_params callback
  288. */
  289. static int snd_cx23885_hw_params(struct snd_pcm_substream *substream,
  290. struct snd_pcm_hw_params *hw_params)
  291. {
  292. struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
  293. struct cx23885_audio_buffer *buf;
  294. int ret;
  295. if (substream->runtime->dma_area) {
  296. dsp_buffer_free(chip);
  297. substream->runtime->dma_area = NULL;
  298. }
  299. chip->period_size = params_period_bytes(hw_params);
  300. chip->num_periods = params_periods(hw_params);
  301. chip->dma_size = chip->period_size * params_periods(hw_params);
  302. BUG_ON(!chip->dma_size);
  303. BUG_ON(chip->num_periods & (chip->num_periods-1));
  304. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  305. if (NULL == buf)
  306. return -ENOMEM;
  307. buf->bpl = chip->period_size;
  308. chip->buf = buf;
  309. ret = cx23885_alsa_dma_init(chip,
  310. (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
  311. if (ret < 0)
  312. goto error;
  313. ret = cx23885_alsa_dma_map(chip);
  314. if (ret < 0)
  315. goto error;
  316. ret = cx23885_risc_databuffer(chip->pci, &buf->risc, buf->sglist,
  317. chip->period_size, chip->num_periods, 1);
  318. if (ret < 0)
  319. goto error;
  320. /* Loop back to start of program */
  321. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
  322. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  323. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  324. substream->runtime->dma_area = chip->buf->vaddr;
  325. substream->runtime->dma_bytes = chip->dma_size;
  326. substream->runtime->dma_addr = 0;
  327. return 0;
  328. error:
  329. kfree(buf);
  330. chip->buf = NULL;
  331. return ret;
  332. }
  333. /*
  334. * hw free callback
  335. */
  336. static int snd_cx23885_hw_free(struct snd_pcm_substream *substream)
  337. {
  338. struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
  339. if (substream->runtime->dma_area) {
  340. dsp_buffer_free(chip);
  341. substream->runtime->dma_area = NULL;
  342. }
  343. return 0;
  344. }
  345. /*
  346. * prepare callback
  347. */
  348. static int snd_cx23885_prepare(struct snd_pcm_substream *substream)
  349. {
  350. return 0;
  351. }
  352. /*
  353. * trigger callback
  354. */
  355. static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream,
  356. int cmd)
  357. {
  358. struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
  359. int err;
  360. /* Local interrupts are already disabled by ALSA */
  361. spin_lock(&chip->lock);
  362. switch (cmd) {
  363. case SNDRV_PCM_TRIGGER_START:
  364. err = cx23885_start_audio_dma(chip);
  365. break;
  366. case SNDRV_PCM_TRIGGER_STOP:
  367. err = cx23885_stop_audio_dma(chip);
  368. break;
  369. default:
  370. err = -EINVAL;
  371. break;
  372. }
  373. spin_unlock(&chip->lock);
  374. return err;
  375. }
  376. /*
  377. * pointer callback
  378. */
  379. static snd_pcm_uframes_t snd_cx23885_pointer(
  380. struct snd_pcm_substream *substream)
  381. {
  382. struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
  383. struct snd_pcm_runtime *runtime = substream->runtime;
  384. u16 count;
  385. count = atomic_read(&chip->count);
  386. return runtime->period_size * (count & (runtime->periods-1));
  387. }
  388. /*
  389. * page callback (needed for mmap)
  390. */
  391. static struct page *snd_cx23885_page(struct snd_pcm_substream *substream,
  392. unsigned long offset)
  393. {
  394. void *pageptr = substream->runtime->dma_area + offset;
  395. return vmalloc_to_page(pageptr);
  396. }
  397. /*
  398. * operators
  399. */
  400. static const struct snd_pcm_ops snd_cx23885_pcm_ops = {
  401. .open = snd_cx23885_pcm_open,
  402. .close = snd_cx23885_close,
  403. .hw_params = snd_cx23885_hw_params,
  404. .hw_free = snd_cx23885_hw_free,
  405. .prepare = snd_cx23885_prepare,
  406. .trigger = snd_cx23885_card_trigger,
  407. .pointer = snd_cx23885_pointer,
  408. .page = snd_cx23885_page,
  409. };
  410. /*
  411. * create a PCM device
  412. */
  413. static int snd_cx23885_pcm(struct cx23885_audio_dev *chip, int device,
  414. char *name)
  415. {
  416. int err;
  417. struct snd_pcm *pcm;
  418. err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
  419. if (err < 0)
  420. return err;
  421. pcm->private_data = chip;
  422. strscpy(pcm->name, name, sizeof(pcm->name));
  423. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx23885_pcm_ops);
  424. return 0;
  425. }
  426. /****************************************************************************
  427. Basic Flow for Sound Devices
  428. ****************************************************************************/
  429. /*
  430. * Alsa Constructor - Component probe
  431. */
  432. struct cx23885_audio_dev *cx23885_audio_register(struct cx23885_dev *dev)
  433. {
  434. struct snd_card *card;
  435. struct cx23885_audio_dev *chip;
  436. int err;
  437. if (disable_analog_audio)
  438. return NULL;
  439. if (dev->sram_channels[AUDIO_SRAM_CHANNEL].cmds_start == 0) {
  440. pr_warn("%s(): Missing SRAM channel configuration for analog TV Audio\n",
  441. __func__);
  442. return NULL;
  443. }
  444. err = snd_card_new(&dev->pci->dev,
  445. SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  446. THIS_MODULE, sizeof(struct cx23885_audio_dev), &card);
  447. if (err < 0)
  448. goto error_msg;
  449. chip = (struct cx23885_audio_dev *) card->private_data;
  450. chip->dev = dev;
  451. chip->pci = dev->pci;
  452. chip->card = card;
  453. spin_lock_init(&chip->lock);
  454. err = snd_cx23885_pcm(chip, 0, "CX23885 Digital");
  455. if (err < 0)
  456. goto error;
  457. strscpy(card->driver, "CX23885", sizeof(card->driver));
  458. sprintf(card->shortname, "Conexant CX23885");
  459. sprintf(card->longname, "%s at %s", card->shortname, dev->name);
  460. err = snd_card_register(card);
  461. if (err < 0)
  462. goto error;
  463. dprintk(0, "registered ALSA audio device\n");
  464. return chip;
  465. error:
  466. snd_card_free(card);
  467. error_msg:
  468. pr_err("%s(): Failed to register analog audio adapter\n",
  469. __func__);
  470. return NULL;
  471. }
  472. /*
  473. * ALSA destructor
  474. */
  475. void cx23885_audio_unregister(struct cx23885_dev *dev)
  476. {
  477. struct cx23885_audio_dev *chip = dev->audio_dev;
  478. snd_card_free(chip->card);
  479. }