cx25821-alsa.c 20 KB

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