cx25821-alsa.c 21 KB

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