cx25821-alsa.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for the Conexant CX25821 PCIe bridge
  4. *
  5. * Copyright (C) 2009 Conexant Systems Inc.
  6. * Authors <[email protected]>, <[email protected]>
  7. * Based on SAA713x ALSA driver and CX88 driver
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/pci.h>
  17. #include <linux/slab.h>
  18. #include <linux/delay.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/control.h>
  23. #include <sound/initval.h>
  24. #include <sound/tlv.h>
  25. #include "cx25821.h"
  26. #include "cx25821-reg.h"
  27. #define AUDIO_SRAM_CHANNEL SRAM_CH08
  28. #define dprintk(level, fmt, arg...) \
  29. do { \
  30. if (debug >= level) \
  31. pr_info("%s/1: " fmt, chip->dev->name, ##arg); \
  32. } while (0)
  33. #define dprintk_core(level, fmt, arg...) \
  34. do { \
  35. if (debug >= level) \
  36. printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name, ##arg); \
  37. } while (0)
  38. /****************************************************************************
  39. Data type declarations - Can be moded to a header file later
  40. ****************************************************************************/
  41. static int devno;
  42. struct cx25821_audio_buffer {
  43. unsigned int bpl;
  44. struct cx25821_riscmem risc;
  45. void *vaddr;
  46. struct scatterlist *sglist;
  47. int sglen;
  48. unsigned long nr_pages;
  49. };
  50. struct cx25821_audio_dev {
  51. struct cx25821_dev *dev;
  52. struct cx25821_dmaqueue q;
  53. /* pci i/o */
  54. struct pci_dev *pci;
  55. /* audio controls */
  56. int irq;
  57. struct snd_card *card;
  58. unsigned long iobase;
  59. spinlock_t reg_lock;
  60. atomic_t count;
  61. unsigned int dma_size;
  62. unsigned int period_size;
  63. unsigned int num_periods;
  64. struct cx25821_audio_buffer *buf;
  65. struct snd_pcm_substream *substream;
  66. };
  67. /****************************************************************************
  68. Module global static vars
  69. ****************************************************************************/
  70. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  71. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  72. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  73. module_param_array(enable, bool, NULL, 0444);
  74. MODULE_PARM_DESC(enable, "Enable cx25821 soundcard. default enabled.");
  75. module_param_array(index, int, NULL, 0444);
  76. MODULE_PARM_DESC(index, "Index value for cx25821 capture interface(s).");
  77. /****************************************************************************
  78. Module macros
  79. ****************************************************************************/
  80. MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
  81. MODULE_AUTHOR("Hiep Huynh");
  82. MODULE_LICENSE("GPL");
  83. static unsigned int debug;
  84. module_param(debug, int, 0644);
  85. MODULE_PARM_DESC(debug, "enable debug messages");
  86. /****************************************************************************
  87. Module specific functions
  88. ****************************************************************************/
  89. /* Constants taken from cx88-reg.h */
  90. #define AUD_INT_DN_RISCI1 (1 << 0)
  91. #define AUD_INT_UP_RISCI1 (1 << 1)
  92. #define AUD_INT_RDS_DN_RISCI1 (1 << 2)
  93. #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
  94. #define AUD_INT_UP_RISCI2 (1 << 5)
  95. #define AUD_INT_RDS_DN_RISCI2 (1 << 6)
  96. #define AUD_INT_DN_SYNC (1 << 12)
  97. #define AUD_INT_UP_SYNC (1 << 13)
  98. #define AUD_INT_RDS_DN_SYNC (1 << 14)
  99. #define AUD_INT_OPC_ERR (1 << 16)
  100. #define AUD_INT_BER_IRQ (1 << 20)
  101. #define AUD_INT_MCHG_IRQ (1 << 21)
  102. #define GP_COUNT_CONTROL_RESET 0x3
  103. #define PCI_MSK_AUD_EXT (1 << 4)
  104. #define PCI_MSK_AUD_INT (1 << 3)
  105. static int cx25821_alsa_dma_init(struct cx25821_audio_dev *chip,
  106. unsigned long nr_pages)
  107. {
  108. struct cx25821_audio_buffer *buf = chip->buf;
  109. struct page *pg;
  110. int i;
  111. buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
  112. if (NULL == buf->vaddr) {
  113. dprintk(1, "vmalloc_32(%lu pages) failed\n", nr_pages);
  114. return -ENOMEM;
  115. }
  116. dprintk(1, "vmalloc is at addr 0x%p, size=%lu\n",
  117. buf->vaddr,
  118. nr_pages << PAGE_SHIFT);
  119. memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
  120. buf->nr_pages = nr_pages;
  121. buf->sglist = vzalloc(array_size(sizeof(*buf->sglist), buf->nr_pages));
  122. if (NULL == buf->sglist)
  123. goto vzalloc_err;
  124. sg_init_table(buf->sglist, buf->nr_pages);
  125. for (i = 0; i < buf->nr_pages; i++) {
  126. pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
  127. if (NULL == pg)
  128. goto vmalloc_to_page_err;
  129. sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
  130. }
  131. return 0;
  132. vmalloc_to_page_err:
  133. vfree(buf->sglist);
  134. buf->sglist = NULL;
  135. vzalloc_err:
  136. vfree(buf->vaddr);
  137. buf->vaddr = NULL;
  138. return -ENOMEM;
  139. }
  140. static int cx25821_alsa_dma_map(struct cx25821_audio_dev *dev)
  141. {
  142. struct cx25821_audio_buffer *buf = dev->buf;
  143. buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
  144. buf->nr_pages, DMA_FROM_DEVICE);
  145. if (0 == buf->sglen) {
  146. pr_warn("%s: cx25821_alsa_map_sg failed\n", __func__);
  147. return -ENOMEM;
  148. }
  149. return 0;
  150. }
  151. static int cx25821_alsa_dma_unmap(struct cx25821_audio_dev *dev)
  152. {
  153. struct cx25821_audio_buffer *buf = dev->buf;
  154. if (!buf->sglen)
  155. return 0;
  156. dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->nr_pages, DMA_FROM_DEVICE);
  157. buf->sglen = 0;
  158. return 0;
  159. }
  160. static int cx25821_alsa_dma_free(struct cx25821_audio_buffer *buf)
  161. {
  162. vfree(buf->sglist);
  163. buf->sglist = NULL;
  164. vfree(buf->vaddr);
  165. buf->vaddr = NULL;
  166. return 0;
  167. }
  168. /*
  169. * BOARD Specific: Sets audio DMA
  170. */
  171. static int _cx25821_start_audio_dma(struct cx25821_audio_dev *chip)
  172. {
  173. struct cx25821_audio_buffer *buf = chip->buf;
  174. struct cx25821_dev *dev = chip->dev;
  175. const struct sram_channel *audio_ch =
  176. &cx25821_sram_channels[AUDIO_SRAM_CHANNEL];
  177. u32 tmp = 0;
  178. /* enable output on the GPIO 0 for the MCLK ADC (Audio) */
  179. cx25821_set_gpiopin_direction(chip->dev, 0, 0);
  180. /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
  181. cx_clear(AUD_INT_DMA_CTL,
  182. FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
  183. /* setup fifo + format - out channel */
  184. cx25821_sram_channel_setup_audio(chip->dev, audio_ch, buf->bpl,
  185. buf->risc.dma);
  186. /* sets bpl size */
  187. cx_write(AUD_A_LNGTH, buf->bpl);
  188. /* reset counter */
  189. /* GP_COUNT_CONTROL_RESET = 0x3 */
  190. cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
  191. atomic_set(&chip->count, 0);
  192. /* Set the input mode to 16-bit */
  193. tmp = cx_read(AUD_A_CFG);
  194. cx_write(AUD_A_CFG, tmp | FLD_AUD_DST_PK_MODE | FLD_AUD_DST_ENABLE |
  195. FLD_AUD_CLK_ENABLE);
  196. /*
  197. pr_info("DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d byte buffer\n",
  198. buf->bpl, audio_ch->cmds_start,
  199. cx_read(audio_ch->cmds_start + 12)>>1,
  200. chip->num_periods, buf->bpl * chip->num_periods);
  201. */
  202. /* Enables corresponding bits at AUD_INT_STAT */
  203. cx_write(AUD_A_INT_MSK, FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF |
  204. FLD_AUD_DST_SYNC | FLD_AUD_DST_OPC_ERR);
  205. /* Clean any pending interrupt bits already set */
  206. cx_write(AUD_A_INT_STAT, ~0);
  207. /* enable audio irqs */
  208. cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
  209. /* Turn on audio downstream fifo and risc enable 0x101 */
  210. tmp = cx_read(AUD_INT_DMA_CTL);
  211. cx_set(AUD_INT_DMA_CTL, tmp |
  212. (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN));
  213. mdelay(100);
  214. return 0;
  215. }
  216. /*
  217. * BOARD Specific: Resets audio DMA
  218. */
  219. static int _cx25821_stop_audio_dma(struct cx25821_audio_dev *chip)
  220. {
  221. struct cx25821_dev *dev = chip->dev;
  222. /* stop dma */
  223. cx_clear(AUD_INT_DMA_CTL,
  224. FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
  225. /* disable irqs */
  226. cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
  227. cx_clear(AUD_A_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  228. AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
  229. return 0;
  230. }
  231. #define MAX_IRQ_LOOP 50
  232. /*
  233. * BOARD Specific: IRQ dma bits
  234. */
  235. static char *cx25821_aud_irqs[32] = {
  236. "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
  237. NULL, /* reserved */
  238. "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
  239. NULL, /* reserved */
  240. "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
  241. NULL, /* reserved */
  242. "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
  243. NULL, /* reserved */
  244. "opc_err", "par_err", "rip_err", /* 16-18 */
  245. "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
  246. };
  247. /*
  248. * BOARD Specific: Threats IRQ audio specific calls
  249. */
  250. static void cx25821_aud_irq(struct cx25821_audio_dev *chip, u32 status,
  251. u32 mask)
  252. {
  253. struct cx25821_dev *dev = chip->dev;
  254. if (0 == (status & mask))
  255. return;
  256. cx_write(AUD_A_INT_STAT, status);
  257. if (debug > 1 || (status & mask & ~0xff))
  258. cx25821_print_irqbits(dev->name, "irq aud", cx25821_aud_irqs,
  259. ARRAY_SIZE(cx25821_aud_irqs), status, mask);
  260. /* risc op code error */
  261. if (status & AUD_INT_OPC_ERR) {
  262. pr_warn("WARNING %s/1: Audio risc op code error\n", dev->name);
  263. cx_clear(AUD_INT_DMA_CTL,
  264. FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
  265. cx25821_sram_channel_dump_audio(dev,
  266. &cx25821_sram_channels[AUDIO_SRAM_CHANNEL]);
  267. }
  268. if (status & AUD_INT_DN_SYNC) {
  269. pr_warn("WARNING %s: Downstream sync error!\n", dev->name);
  270. cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
  271. return;
  272. }
  273. /* risc1 downstream */
  274. if (status & AUD_INT_DN_RISCI1) {
  275. atomic_set(&chip->count, cx_read(AUD_A_GPCNT));
  276. snd_pcm_period_elapsed(chip->substream);
  277. }
  278. }
  279. /*
  280. * BOARD Specific: Handles IRQ calls
  281. */
  282. static irqreturn_t cx25821_irq(int irq, void *dev_id)
  283. {
  284. struct cx25821_audio_dev *chip = dev_id;
  285. struct cx25821_dev *dev = chip->dev;
  286. u32 status, pci_status;
  287. u32 audint_status, audint_mask;
  288. int loop, handled = 0;
  289. audint_status = cx_read(AUD_A_INT_STAT);
  290. audint_mask = cx_read(AUD_A_INT_MSK);
  291. status = cx_read(PCI_INT_STAT);
  292. for (loop = 0; loop < 1; loop++) {
  293. status = cx_read(PCI_INT_STAT);
  294. if (0 == status) {
  295. status = cx_read(PCI_INT_STAT);
  296. audint_status = cx_read(AUD_A_INT_STAT);
  297. audint_mask = cx_read(AUD_A_INT_MSK);
  298. if (status) {
  299. handled = 1;
  300. cx_write(PCI_INT_STAT, status);
  301. cx25821_aud_irq(chip, audint_status,
  302. audint_mask);
  303. break;
  304. } else {
  305. goto out;
  306. }
  307. }
  308. handled = 1;
  309. cx_write(PCI_INT_STAT, status);
  310. cx25821_aud_irq(chip, audint_status, audint_mask);
  311. }
  312. pci_status = cx_read(PCI_INT_STAT);
  313. if (handled)
  314. cx_write(PCI_INT_STAT, pci_status);
  315. out:
  316. return IRQ_RETVAL(handled);
  317. }
  318. static int dsp_buffer_free(struct cx25821_audio_dev *chip)
  319. {
  320. struct cx25821_riscmem *risc = &chip->buf->risc;
  321. BUG_ON(!chip->dma_size);
  322. dprintk(2, "Freeing buffer\n");
  323. cx25821_alsa_dma_unmap(chip);
  324. cx25821_alsa_dma_free(chip->buf);
  325. dma_free_coherent(&chip->pci->dev, risc->size, risc->cpu, risc->dma);
  326. kfree(chip->buf);
  327. chip->buf = NULL;
  328. chip->dma_size = 0;
  329. return 0;
  330. }
  331. /****************************************************************************
  332. ALSA PCM Interface
  333. ****************************************************************************/
  334. /*
  335. * Digital hardware definition
  336. */
  337. #define DEFAULT_FIFO_SIZE 384
  338. static const struct snd_pcm_hardware snd_cx25821_digital_hw = {
  339. .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  340. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID,
  341. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  342. .rates = SNDRV_PCM_RATE_48000,
  343. .rate_min = 48000,
  344. .rate_max = 48000,
  345. .channels_min = 2,
  346. .channels_max = 2,
  347. /* Analog audio output will be full of clicks and pops if there
  348. are not exactly four lines in the SRAM FIFO buffer. */
  349. .period_bytes_min = DEFAULT_FIFO_SIZE / 3,
  350. .period_bytes_max = DEFAULT_FIFO_SIZE / 3,
  351. .periods_min = 1,
  352. .periods_max = AUDIO_LINE_SIZE,
  353. /* 128 * 128 = 16384 = 1024 * 16 */
  354. .buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE),
  355. };
  356. /*
  357. * audio pcm capture open callback
  358. */
  359. static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream)
  360. {
  361. struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
  362. struct snd_pcm_runtime *runtime = substream->runtime;
  363. int err;
  364. unsigned int bpl = 0;
  365. if (!chip) {
  366. pr_err("DEBUG: cx25821 can't find device struct. Can't proceed with open\n");
  367. return -ENODEV;
  368. }
  369. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  370. SNDRV_PCM_HW_PARAM_PERIODS);
  371. if (err < 0)
  372. goto _error;
  373. chip->substream = substream;
  374. runtime->hw = snd_cx25821_digital_hw;
  375. if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
  376. DEFAULT_FIFO_SIZE) {
  377. /* since there are 3 audio Clusters */
  378. bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3;
  379. bpl &= ~7; /* must be multiple of 8 */
  380. if (bpl > AUDIO_LINE_SIZE)
  381. bpl = AUDIO_LINE_SIZE;
  382. runtime->hw.period_bytes_min = bpl;
  383. runtime->hw.period_bytes_max = bpl;
  384. }
  385. return 0;
  386. _error:
  387. dprintk(1, "Error opening PCM!\n");
  388. return err;
  389. }
  390. /*
  391. * audio close callback
  392. */
  393. static int snd_cx25821_close(struct snd_pcm_substream *substream)
  394. {
  395. return 0;
  396. }
  397. /*
  398. * hw_params callback
  399. */
  400. static int snd_cx25821_hw_params(struct snd_pcm_substream *substream,
  401. struct snd_pcm_hw_params *hw_params)
  402. {
  403. struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
  404. struct cx25821_audio_buffer *buf;
  405. int ret;
  406. if (substream->runtime->dma_area) {
  407. dsp_buffer_free(chip);
  408. substream->runtime->dma_area = NULL;
  409. }
  410. chip->period_size = params_period_bytes(hw_params);
  411. chip->num_periods = params_periods(hw_params);
  412. chip->dma_size = chip->period_size * params_periods(hw_params);
  413. BUG_ON(!chip->dma_size);
  414. BUG_ON(chip->num_periods & (chip->num_periods - 1));
  415. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  416. if (NULL == buf)
  417. return -ENOMEM;
  418. if (chip->period_size > AUDIO_LINE_SIZE)
  419. chip->period_size = AUDIO_LINE_SIZE;
  420. buf->bpl = chip->period_size;
  421. chip->buf = buf;
  422. ret = cx25821_alsa_dma_init(chip,
  423. (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
  424. if (ret < 0)
  425. goto error;
  426. ret = cx25821_alsa_dma_map(chip);
  427. if (ret < 0)
  428. goto error;
  429. ret = cx25821_risc_databuffer_audio(chip->pci, &buf->risc, buf->sglist,
  430. chip->period_size, chip->num_periods, 1);
  431. if (ret < 0) {
  432. pr_info("DEBUG: ERROR after cx25821_risc_databuffer_audio()\n");
  433. goto error;
  434. }
  435. /* Loop back to start of program */
  436. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  437. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  438. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  439. substream->runtime->dma_area = chip->buf->vaddr;
  440. substream->runtime->dma_bytes = chip->dma_size;
  441. substream->runtime->dma_addr = 0;
  442. return 0;
  443. error:
  444. chip->buf = NULL;
  445. kfree(buf);
  446. return ret;
  447. }
  448. /*
  449. * hw free callback
  450. */
  451. static int snd_cx25821_hw_free(struct snd_pcm_substream *substream)
  452. {
  453. struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
  454. if (substream->runtime->dma_area) {
  455. dsp_buffer_free(chip);
  456. substream->runtime->dma_area = NULL;
  457. }
  458. return 0;
  459. }
  460. /*
  461. * prepare callback
  462. */
  463. static int snd_cx25821_prepare(struct snd_pcm_substream *substream)
  464. {
  465. return 0;
  466. }
  467. /*
  468. * trigger callback
  469. */
  470. static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream,
  471. int cmd)
  472. {
  473. struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
  474. int err = 0;
  475. /* Local interrupts are already disabled by ALSA */
  476. spin_lock(&chip->reg_lock);
  477. switch (cmd) {
  478. case SNDRV_PCM_TRIGGER_START:
  479. err = _cx25821_start_audio_dma(chip);
  480. break;
  481. case SNDRV_PCM_TRIGGER_STOP:
  482. err = _cx25821_stop_audio_dma(chip);
  483. break;
  484. default:
  485. err = -EINVAL;
  486. break;
  487. }
  488. spin_unlock(&chip->reg_lock);
  489. return err;
  490. }
  491. /*
  492. * pointer callback
  493. */
  494. static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream
  495. *substream)
  496. {
  497. struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
  498. struct snd_pcm_runtime *runtime = substream->runtime;
  499. u16 count;
  500. count = atomic_read(&chip->count);
  501. return runtime->period_size * (count & (runtime->periods - 1));
  502. }
  503. /*
  504. * page callback (needed for mmap)
  505. */
  506. static struct page *snd_cx25821_page(struct snd_pcm_substream *substream,
  507. unsigned long offset)
  508. {
  509. void *pageptr = substream->runtime->dma_area + offset;
  510. return vmalloc_to_page(pageptr);
  511. }
  512. /*
  513. * operators
  514. */
  515. static const struct snd_pcm_ops snd_cx25821_pcm_ops = {
  516. .open = snd_cx25821_pcm_open,
  517. .close = snd_cx25821_close,
  518. .hw_params = snd_cx25821_hw_params,
  519. .hw_free = snd_cx25821_hw_free,
  520. .prepare = snd_cx25821_prepare,
  521. .trigger = snd_cx25821_card_trigger,
  522. .pointer = snd_cx25821_pointer,
  523. .page = snd_cx25821_page,
  524. };
  525. /*
  526. * ALSA create a PCM device: Called when initializing the board.
  527. * Sets up the name and hooks up the callbacks
  528. */
  529. static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device,
  530. char *name)
  531. {
  532. struct snd_pcm *pcm;
  533. int err;
  534. err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
  535. if (err < 0) {
  536. pr_info("ERROR: FAILED snd_pcm_new() in %s\n", __func__);
  537. return err;
  538. }
  539. pcm->private_data = chip;
  540. pcm->info_flags = 0;
  541. strscpy(pcm->name, name, sizeof(pcm->name));
  542. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops);
  543. return 0;
  544. }
  545. /****************************************************************************
  546. Basic Flow for Sound Devices
  547. ****************************************************************************/
  548. /*
  549. * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
  550. * Only boards with eeprom and byte 1 at eeprom=1 have it
  551. */
  552. static const struct pci_device_id __maybe_unused cx25821_audio_pci_tbl[] = {
  553. {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  554. {0,}
  555. };
  556. MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl);
  557. /*
  558. * Alsa Constructor - Component probe
  559. */
  560. static int cx25821_audio_initdev(struct cx25821_dev *dev)
  561. {
  562. struct snd_card *card;
  563. struct cx25821_audio_dev *chip;
  564. int err;
  565. if (devno >= SNDRV_CARDS) {
  566. pr_info("DEBUG ERROR: devno >= SNDRV_CARDS %s\n", __func__);
  567. return -ENODEV;
  568. }
  569. if (!enable[devno]) {
  570. ++devno;
  571. pr_info("DEBUG ERROR: !enable[devno] %s\n", __func__);
  572. return -ENOENT;
  573. }
  574. err = snd_card_new(&dev->pci->dev, index[devno], id[devno],
  575. THIS_MODULE,
  576. sizeof(struct cx25821_audio_dev), &card);
  577. if (err < 0) {
  578. pr_info("DEBUG ERROR: cannot create snd_card_new in %s\n",
  579. __func__);
  580. return err;
  581. }
  582. strscpy(card->driver, "cx25821", sizeof(card->driver));
  583. /* Card "creation" */
  584. chip = card->private_data;
  585. spin_lock_init(&chip->reg_lock);
  586. chip->dev = dev;
  587. chip->card = card;
  588. chip->pci = dev->pci;
  589. chip->iobase = pci_resource_start(dev->pci, 0);
  590. chip->irq = dev->pci->irq;
  591. err = devm_request_irq(&dev->pci->dev, dev->pci->irq, cx25821_irq,
  592. IRQF_SHARED, chip->dev->name, chip);
  593. if (err < 0) {
  594. pr_err("ERROR %s: can't get IRQ %d for ALSA\n", chip->dev->name,
  595. dev->pci->irq);
  596. goto error;
  597. }
  598. err = snd_cx25821_pcm(chip, 0, "cx25821 Digital");
  599. if (err < 0) {
  600. pr_info("DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
  601. __func__);
  602. goto error;
  603. }
  604. strscpy(card->shortname, "cx25821", sizeof(card->shortname));
  605. sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name,
  606. chip->iobase, chip->irq);
  607. strscpy(card->mixername, "CX25821", sizeof(card->mixername));
  608. pr_info("%s/%i: ALSA support for cx25821 boards\n", card->driver,
  609. devno);
  610. err = snd_card_register(card);
  611. if (err < 0) {
  612. pr_info("DEBUG ERROR: cannot register sound card %s\n",
  613. __func__);
  614. goto error;
  615. }
  616. dev->card = card;
  617. devno++;
  618. return 0;
  619. error:
  620. snd_card_free(card);
  621. return err;
  622. }
  623. /****************************************************************************
  624. LINUX MODULE INIT
  625. ****************************************************************************/
  626. static int cx25821_alsa_exit_callback(struct device *dev, void *data)
  627. {
  628. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  629. struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
  630. snd_card_free(cxdev->card);
  631. return 0;
  632. }
  633. static void cx25821_audio_fini(void)
  634. {
  635. struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
  636. int ret;
  637. ret = driver_for_each_device(drv, NULL, NULL, cx25821_alsa_exit_callback);
  638. if (ret)
  639. pr_err("%s failed to find a cx25821 driver.\n", __func__);
  640. }
  641. static int cx25821_alsa_init_callback(struct device *dev, void *data)
  642. {
  643. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  644. struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
  645. cx25821_audio_initdev(cxdev);
  646. return 0;
  647. }
  648. /*
  649. * Module initializer
  650. *
  651. * Loops through present saa7134 cards, and assigns an ALSA device
  652. * to each one
  653. *
  654. */
  655. static int cx25821_alsa_init(void)
  656. {
  657. struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
  658. return driver_for_each_device(drv, NULL, NULL, cx25821_alsa_init_callback);
  659. }
  660. late_initcall(cx25821_alsa_init);
  661. module_exit(cx25821_audio_fini);