cx25821-alsa.c 19 KB

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