cx25821-alsa.c 19 KB

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