lola.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Support for Digigram Lola PCI-e boards
  3. *
  4. * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 59
  18. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/slab.h>
  27. #include <linux/pci.h>
  28. #include <sound/core.h>
  29. #include <sound/control.h>
  30. #include <sound/pcm.h>
  31. #include <sound/initval.h>
  32. #include "lola.h"
  33. /* Standard options */
  34. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  35. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  36. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  37. module_param_array(index, int, NULL, 0444);
  38. MODULE_PARM_DESC(index, "Index value for Digigram Lola driver.");
  39. module_param_array(id, charp, NULL, 0444);
  40. MODULE_PARM_DESC(id, "ID string for Digigram Lola driver.");
  41. module_param_array(enable, bool, NULL, 0444);
  42. MODULE_PARM_DESC(enable, "Enable Digigram Lola driver.");
  43. /* Lola-specific options */
  44. /* for instance use always max granularity which is compatible
  45. * with all sample rates
  46. */
  47. static int granularity[SNDRV_CARDS] = {
  48. [0 ... (SNDRV_CARDS - 1)] = LOLA_GRANULARITY_MAX
  49. };
  50. /* below a sample_rate of 16kHz the analogue audio quality is NOT excellent */
  51. static int sample_rate_min[SNDRV_CARDS] = {
  52. [0 ... (SNDRV_CARDS - 1) ] = 16000
  53. };
  54. module_param_array(granularity, int, NULL, 0444);
  55. MODULE_PARM_DESC(granularity, "Granularity value");
  56. module_param_array(sample_rate_min, int, NULL, 0444);
  57. MODULE_PARM_DESC(sample_rate_min, "Minimal sample rate");
  58. /*
  59. */
  60. MODULE_LICENSE("GPL");
  61. MODULE_SUPPORTED_DEVICE("{{Digigram, Lola}}");
  62. MODULE_DESCRIPTION("Digigram Lola driver");
  63. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  64. #ifdef CONFIG_SND_DEBUG_VERBOSE
  65. static int debug;
  66. module_param(debug, int, 0644);
  67. #define verbose_debug(fmt, args...) \
  68. do { if (debug > 1) printk(KERN_DEBUG SFX fmt, ##args); } while (0)
  69. #else
  70. #define verbose_debug(fmt, args...)
  71. #endif
  72. /*
  73. * pseudo-codec read/write via CORB/RIRB
  74. */
  75. static int corb_send_verb(struct lola *chip, unsigned int nid,
  76. unsigned int verb, unsigned int data,
  77. unsigned int extdata)
  78. {
  79. unsigned long flags;
  80. int ret = -EIO;
  81. chip->last_cmd_nid = nid;
  82. chip->last_verb = verb;
  83. chip->last_data = data;
  84. chip->last_extdata = extdata;
  85. data |= (nid << 20) | (verb << 8);
  86. spin_lock_irqsave(&chip->reg_lock, flags);
  87. if (chip->rirb.cmds < LOLA_CORB_ENTRIES - 1) {
  88. unsigned int wp = chip->corb.wp + 1;
  89. wp %= LOLA_CORB_ENTRIES;
  90. chip->corb.wp = wp;
  91. chip->corb.buf[wp * 2] = cpu_to_le32(data);
  92. chip->corb.buf[wp * 2 + 1] = cpu_to_le32(extdata);
  93. lola_writew(chip, BAR0, CORBWP, wp);
  94. chip->rirb.cmds++;
  95. smp_wmb();
  96. ret = 0;
  97. }
  98. spin_unlock_irqrestore(&chip->reg_lock, flags);
  99. return ret;
  100. }
  101. static void lola_queue_unsol_event(struct lola *chip, unsigned int res,
  102. unsigned int res_ex)
  103. {
  104. lola_update_ext_clock_freq(chip, res);
  105. }
  106. /* retrieve RIRB entry - called from interrupt handler */
  107. static void lola_update_rirb(struct lola *chip)
  108. {
  109. unsigned int rp, wp;
  110. u32 res, res_ex;
  111. wp = lola_readw(chip, BAR0, RIRBWP);
  112. if (wp == chip->rirb.wp)
  113. return;
  114. chip->rirb.wp = wp;
  115. while (chip->rirb.rp != wp) {
  116. chip->rirb.rp++;
  117. chip->rirb.rp %= LOLA_CORB_ENTRIES;
  118. rp = chip->rirb.rp << 1; /* an RIRB entry is 8-bytes */
  119. res_ex = le32_to_cpu(chip->rirb.buf[rp + 1]);
  120. res = le32_to_cpu(chip->rirb.buf[rp]);
  121. if (res_ex & LOLA_RIRB_EX_UNSOL_EV)
  122. lola_queue_unsol_event(chip, res, res_ex);
  123. else if (chip->rirb.cmds) {
  124. chip->res = res;
  125. chip->res_ex = res_ex;
  126. smp_wmb();
  127. chip->rirb.cmds--;
  128. }
  129. }
  130. }
  131. static int rirb_get_response(struct lola *chip, unsigned int *val,
  132. unsigned int *extval)
  133. {
  134. unsigned long timeout;
  135. timeout = jiffies + msecs_to_jiffies(1000);
  136. for (;;) {
  137. if (!chip->rirb.cmds) {
  138. *val = chip->res;
  139. if (extval)
  140. *extval = chip->res_ex;
  141. verbose_debug("get_response: %x, %x\n",
  142. chip->res, chip->res_ex);
  143. if (chip->res_ex & LOLA_RIRB_EX_ERROR) {
  144. printk(KERN_WARNING SFX "RIRB ERROR: "
  145. "NID=%x, verb=%x, data=%x, ext=%x\n",
  146. chip->last_cmd_nid,
  147. chip->last_verb, chip->last_data,
  148. chip->last_extdata);
  149. return -EIO;
  150. }
  151. return 0;
  152. }
  153. if (time_after(jiffies, timeout))
  154. break;
  155. udelay(20);
  156. cond_resched();
  157. lola_update_rirb(chip);
  158. }
  159. printk(KERN_WARNING SFX "RIRB response error\n");
  160. return -EIO;
  161. }
  162. /* aynchronous write of a codec verb with data */
  163. int lola_codec_write(struct lola *chip, unsigned int nid, unsigned int verb,
  164. unsigned int data, unsigned int extdata)
  165. {
  166. verbose_debug("codec_write NID=%x, verb=%x, data=%x, ext=%x\n",
  167. nid, verb, data, extdata);
  168. return corb_send_verb(chip, nid, verb, data, extdata);
  169. }
  170. /* write a codec verb with data and read the returned status */
  171. int lola_codec_read(struct lola *chip, unsigned int nid, unsigned int verb,
  172. unsigned int data, unsigned int extdata,
  173. unsigned int *val, unsigned int *extval)
  174. {
  175. int err;
  176. verbose_debug("codec_read NID=%x, verb=%x, data=%x, ext=%x\n",
  177. nid, verb, data, extdata);
  178. err = corb_send_verb(chip, nid, verb, data, extdata);
  179. if (err < 0)
  180. return err;
  181. err = rirb_get_response(chip, val, extval);
  182. return err;
  183. }
  184. /* flush all pending codec writes */
  185. int lola_codec_flush(struct lola *chip)
  186. {
  187. unsigned int tmp;
  188. return rirb_get_response(chip, &tmp, NULL);
  189. }
  190. /*
  191. * interrupt handler
  192. */
  193. static irqreturn_t lola_interrupt(int irq, void *dev_id)
  194. {
  195. struct lola *chip = dev_id;
  196. unsigned int notify_ins, notify_outs, error_ins, error_outs;
  197. int handled = 0;
  198. int i;
  199. notify_ins = notify_outs = error_ins = error_outs = 0;
  200. spin_lock(&chip->reg_lock);
  201. for (;;) {
  202. unsigned int status, in_sts, out_sts;
  203. unsigned int reg;
  204. status = lola_readl(chip, BAR1, DINTSTS);
  205. if (!status || status == -1)
  206. break;
  207. in_sts = lola_readl(chip, BAR1, DIINTSTS);
  208. out_sts = lola_readl(chip, BAR1, DOINTSTS);
  209. /* clear Input Interrupts */
  210. for (i = 0; in_sts && i < chip->pcm[CAPT].num_streams; i++) {
  211. if (!(in_sts & (1 << i)))
  212. continue;
  213. in_sts &= ~(1 << i);
  214. reg = lola_dsd_read(chip, i, STS);
  215. if (reg & LOLA_DSD_STS_DESE) /* error */
  216. error_ins |= (1 << i);
  217. if (reg & LOLA_DSD_STS_BCIS) /* notify */
  218. notify_ins |= (1 << i);
  219. /* clear */
  220. lola_dsd_write(chip, i, STS, reg);
  221. }
  222. /* clear Output Interrupts */
  223. for (i = 0; out_sts && i < chip->pcm[PLAY].num_streams; i++) {
  224. if (!(out_sts & (1 << i)))
  225. continue;
  226. out_sts &= ~(1 << i);
  227. reg = lola_dsd_read(chip, i + MAX_STREAM_IN_COUNT, STS);
  228. if (reg & LOLA_DSD_STS_DESE) /* error */
  229. error_outs |= (1 << i);
  230. if (reg & LOLA_DSD_STS_BCIS) /* notify */
  231. notify_outs |= (1 << i);
  232. lola_dsd_write(chip, i + MAX_STREAM_IN_COUNT, STS, reg);
  233. }
  234. if (status & LOLA_DINT_CTRL) {
  235. unsigned char rbsts; /* ring status is byte access */
  236. rbsts = lola_readb(chip, BAR0, RIRBSTS);
  237. rbsts &= LOLA_RIRB_INT_MASK;
  238. if (rbsts)
  239. lola_writeb(chip, BAR0, RIRBSTS, rbsts);
  240. rbsts = lola_readb(chip, BAR0, CORBSTS);
  241. rbsts &= LOLA_CORB_INT_MASK;
  242. if (rbsts)
  243. lola_writeb(chip, BAR0, CORBSTS, rbsts);
  244. lola_update_rirb(chip);
  245. }
  246. if (status & (LOLA_DINT_FIFOERR | LOLA_DINT_MUERR)) {
  247. /* clear global fifo error interrupt */
  248. lola_writel(chip, BAR1, DINTSTS,
  249. (status & (LOLA_DINT_FIFOERR | LOLA_DINT_MUERR)));
  250. }
  251. handled = 1;
  252. }
  253. spin_unlock(&chip->reg_lock);
  254. lola_pcm_update(chip, &chip->pcm[CAPT], notify_ins);
  255. lola_pcm_update(chip, &chip->pcm[PLAY], notify_outs);
  256. return IRQ_RETVAL(handled);
  257. }
  258. /*
  259. * controller
  260. */
  261. static int reset_controller(struct lola *chip)
  262. {
  263. unsigned int gctl = lola_readl(chip, BAR0, GCTL);
  264. unsigned long end_time;
  265. if (gctl) {
  266. /* to be sure */
  267. lola_writel(chip, BAR1, BOARD_MODE, 0);
  268. return 0;
  269. }
  270. chip->cold_reset = 1;
  271. lola_writel(chip, BAR0, GCTL, LOLA_GCTL_RESET);
  272. end_time = jiffies + msecs_to_jiffies(200);
  273. do {
  274. msleep(1);
  275. gctl = lola_readl(chip, BAR0, GCTL);
  276. if (gctl)
  277. break;
  278. } while (time_before(jiffies, end_time));
  279. if (!gctl) {
  280. printk(KERN_ERR SFX "cannot reset controller\n");
  281. return -EIO;
  282. }
  283. return 0;
  284. }
  285. static void lola_irq_enable(struct lola *chip)
  286. {
  287. unsigned int val;
  288. /* enalbe all I/O streams */
  289. val = (1 << chip->pcm[PLAY].num_streams) - 1;
  290. lola_writel(chip, BAR1, DOINTCTL, val);
  291. val = (1 << chip->pcm[CAPT].num_streams) - 1;
  292. lola_writel(chip, BAR1, DIINTCTL, val);
  293. /* enable global irqs */
  294. val = LOLA_DINT_GLOBAL | LOLA_DINT_CTRL | LOLA_DINT_FIFOERR |
  295. LOLA_DINT_MUERR;
  296. lola_writel(chip, BAR1, DINTCTL, val);
  297. }
  298. static void lola_irq_disable(struct lola *chip)
  299. {
  300. lola_writel(chip, BAR1, DINTCTL, 0);
  301. lola_writel(chip, BAR1, DIINTCTL, 0);
  302. lola_writel(chip, BAR1, DOINTCTL, 0);
  303. }
  304. static int setup_corb_rirb(struct lola *chip)
  305. {
  306. int err;
  307. unsigned char tmp;
  308. unsigned long end_time;
  309. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  310. snd_dma_pci_data(chip->pci),
  311. PAGE_SIZE, &chip->rb);
  312. if (err < 0)
  313. return err;
  314. chip->corb.addr = chip->rb.addr;
  315. chip->corb.buf = (u32 *)chip->rb.area;
  316. chip->rirb.addr = chip->rb.addr + 2048;
  317. chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
  318. /* disable ringbuffer DMAs */
  319. lola_writeb(chip, BAR0, RIRBCTL, 0);
  320. lola_writeb(chip, BAR0, CORBCTL, 0);
  321. end_time = jiffies + msecs_to_jiffies(200);
  322. do {
  323. if (!lola_readb(chip, BAR0, RIRBCTL) &&
  324. !lola_readb(chip, BAR0, CORBCTL))
  325. break;
  326. msleep(1);
  327. } while (time_before(jiffies, end_time));
  328. /* CORB set up */
  329. lola_writel(chip, BAR0, CORBLBASE, (u32)chip->corb.addr);
  330. lola_writel(chip, BAR0, CORBUBASE, upper_32_bits(chip->corb.addr));
  331. /* set the corb size to 256 entries */
  332. lola_writeb(chip, BAR0, CORBSIZE, 0x02);
  333. /* set the corb write pointer to 0 */
  334. lola_writew(chip, BAR0, CORBWP, 0);
  335. /* reset the corb hw read pointer */
  336. lola_writew(chip, BAR0, CORBRP, LOLA_RBRWP_CLR);
  337. /* enable corb dma */
  338. lola_writeb(chip, BAR0, CORBCTL, LOLA_RBCTL_DMA_EN);
  339. /* clear flags if set */
  340. tmp = lola_readb(chip, BAR0, CORBSTS) & LOLA_CORB_INT_MASK;
  341. if (tmp)
  342. lola_writeb(chip, BAR0, CORBSTS, tmp);
  343. chip->corb.wp = 0;
  344. /* RIRB set up */
  345. lola_writel(chip, BAR0, RIRBLBASE, (u32)chip->rirb.addr);
  346. lola_writel(chip, BAR0, RIRBUBASE, upper_32_bits(chip->rirb.addr));
  347. /* set the rirb size to 256 entries */
  348. lola_writeb(chip, BAR0, RIRBSIZE, 0x02);
  349. /* reset the rirb hw write pointer */
  350. lola_writew(chip, BAR0, RIRBWP, LOLA_RBRWP_CLR);
  351. /* set N=1, get RIRB response interrupt for new entry */
  352. lola_writew(chip, BAR0, RINTCNT, 1);
  353. /* enable rirb dma and response irq */
  354. lola_writeb(chip, BAR0, RIRBCTL, LOLA_RBCTL_DMA_EN | LOLA_RBCTL_IRQ_EN);
  355. /* clear flags if set */
  356. tmp = lola_readb(chip, BAR0, RIRBSTS) & LOLA_RIRB_INT_MASK;
  357. if (tmp)
  358. lola_writeb(chip, BAR0, RIRBSTS, tmp);
  359. chip->rirb.rp = chip->rirb.cmds = 0;
  360. return 0;
  361. }
  362. static void stop_corb_rirb(struct lola *chip)
  363. {
  364. /* disable ringbuffer DMAs */
  365. lola_writeb(chip, BAR0, RIRBCTL, 0);
  366. lola_writeb(chip, BAR0, CORBCTL, 0);
  367. }
  368. static void lola_reset_setups(struct lola *chip)
  369. {
  370. /* update the granularity */
  371. lola_set_granularity(chip, chip->granularity, true);
  372. /* update the sample clock */
  373. lola_set_clock_index(chip, chip->clock.cur_index);
  374. /* enable unsolicited events of the clock widget */
  375. lola_enable_clock_events(chip);
  376. /* update the analog gains */
  377. lola_setup_all_analog_gains(chip, CAPT, false); /* input, update */
  378. /* update SRC configuration if applicable */
  379. lola_set_src_config(chip, chip->input_src_mask, false);
  380. /* update the analog outputs */
  381. lola_setup_all_analog_gains(chip, PLAY, false); /* output, update */
  382. }
  383. static int lola_parse_tree(struct lola *chip)
  384. {
  385. unsigned int val;
  386. int nid, err;
  387. err = lola_read_param(chip, 0, LOLA_PAR_VENDOR_ID, &val);
  388. if (err < 0) {
  389. printk(KERN_ERR SFX "Can't read VENDOR_ID\n");
  390. return err;
  391. }
  392. val >>= 16;
  393. if (val != 0x1369) {
  394. printk(KERN_ERR SFX "Unknown codec vendor 0x%x\n", val);
  395. return -EINVAL;
  396. }
  397. err = lola_read_param(chip, 1, LOLA_PAR_FUNCTION_TYPE, &val);
  398. if (err < 0) {
  399. printk(KERN_ERR SFX "Can't read FUNCTION_TYPE for 0x%x\n", nid);
  400. return err;
  401. }
  402. if (val != 1) {
  403. printk(KERN_ERR SFX "Unknown function type %d\n", val);
  404. return -EINVAL;
  405. }
  406. err = lola_read_param(chip, 1, LOLA_PAR_SPECIFIC_CAPS, &val);
  407. if (err < 0) {
  408. printk(KERN_ERR SFX "Can't read SPECCAPS\n");
  409. return err;
  410. }
  411. chip->lola_caps = val;
  412. chip->pin[CAPT].num_pins = LOLA_AFG_INPUT_PIN_COUNT(chip->lola_caps);
  413. chip->pin[PLAY].num_pins = LOLA_AFG_OUTPUT_PIN_COUNT(chip->lola_caps);
  414. snd_printdd(SFX "speccaps=0x%x, pins in=%d, out=%d\n",
  415. chip->lola_caps,
  416. chip->pin[CAPT].num_pins, chip->pin[PLAY].num_pins);
  417. if (chip->pin[CAPT].num_pins > MAX_AUDIO_INOUT_COUNT ||
  418. chip->pin[PLAY].num_pins > MAX_AUDIO_INOUT_COUNT) {
  419. printk(KERN_ERR SFX "Invalid Lola-spec caps 0x%x\n", val);
  420. return -EINVAL;
  421. }
  422. nid = 0x02;
  423. err = lola_init_pcm(chip, CAPT, &nid);
  424. if (err < 0)
  425. return err;
  426. err = lola_init_pcm(chip, PLAY, &nid);
  427. if (err < 0)
  428. return err;
  429. err = lola_init_pins(chip, CAPT, &nid);
  430. if (err < 0)
  431. return err;
  432. err = lola_init_pins(chip, PLAY, &nid);
  433. if (err < 0)
  434. return err;
  435. if (LOLA_AFG_CLOCK_WIDGET_PRESENT(chip->lola_caps)) {
  436. err = lola_init_clock_widget(chip, nid);
  437. if (err < 0)
  438. return err;
  439. nid++;
  440. }
  441. if (LOLA_AFG_MIXER_WIDGET_PRESENT(chip->lola_caps)) {
  442. err = lola_init_mixer_widget(chip, nid);
  443. if (err < 0)
  444. return err;
  445. nid++;
  446. }
  447. /* enable unsolicited events of the clock widget */
  448. err = lola_enable_clock_events(chip);
  449. if (err < 0)
  450. return err;
  451. /* if last ResetController was not a ColdReset, we don't know
  452. * the state of the card; initialize here again
  453. */
  454. if (!chip->cold_reset) {
  455. lola_reset_setups(chip);
  456. chip->cold_reset = 1;
  457. } else {
  458. /* set the granularity if it is not the default */
  459. if (chip->granularity != LOLA_GRANULARITY_MIN)
  460. lola_set_granularity(chip, chip->granularity, true);
  461. }
  462. return 0;
  463. }
  464. static void lola_stop_hw(struct lola *chip)
  465. {
  466. stop_corb_rirb(chip);
  467. lola_irq_disable(chip);
  468. }
  469. static void lola_free(struct lola *chip)
  470. {
  471. if (chip->initialized)
  472. lola_stop_hw(chip);
  473. lola_free_pcm(chip);
  474. lola_free_mixer(chip);
  475. if (chip->irq >= 0)
  476. free_irq(chip->irq, (void *)chip);
  477. if (chip->bar[0].remap_addr)
  478. iounmap(chip->bar[0].remap_addr);
  479. if (chip->bar[1].remap_addr)
  480. iounmap(chip->bar[1].remap_addr);
  481. if (chip->rb.area)
  482. snd_dma_free_pages(&chip->rb);
  483. pci_release_regions(chip->pci);
  484. pci_disable_device(chip->pci);
  485. kfree(chip);
  486. }
  487. static int lola_dev_free(struct snd_device *device)
  488. {
  489. lola_free(device->device_data);
  490. return 0;
  491. }
  492. static int __devinit lola_create(struct snd_card *card, struct pci_dev *pci,
  493. int dev, struct lola **rchip)
  494. {
  495. struct lola *chip;
  496. int err;
  497. unsigned int dever;
  498. static struct snd_device_ops ops = {
  499. .dev_free = lola_dev_free,
  500. };
  501. *rchip = NULL;
  502. err = pci_enable_device(pci);
  503. if (err < 0)
  504. return err;
  505. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  506. if (!chip) {
  507. snd_printk(KERN_ERR SFX "cannot allocate chip\n");
  508. pci_disable_device(pci);
  509. return -ENOMEM;
  510. }
  511. spin_lock_init(&chip->reg_lock);
  512. mutex_init(&chip->open_mutex);
  513. chip->card = card;
  514. chip->pci = pci;
  515. chip->irq = -1;
  516. chip->granularity = granularity[dev];
  517. switch (chip->granularity) {
  518. case 8:
  519. chip->sample_rate_max = 48000;
  520. break;
  521. case 16:
  522. chip->sample_rate_max = 96000;
  523. break;
  524. case 32:
  525. chip->sample_rate_max = 192000;
  526. break;
  527. default:
  528. snd_printk(KERN_WARNING SFX
  529. "Invalid granularity %d, reset to %d\n",
  530. chip->granularity, LOLA_GRANULARITY_MAX);
  531. chip->granularity = LOLA_GRANULARITY_MAX;
  532. chip->sample_rate_max = 192000;
  533. break;
  534. }
  535. chip->sample_rate_min = sample_rate_min[dev];
  536. if (chip->sample_rate_min > chip->sample_rate_max) {
  537. snd_printk(KERN_WARNING SFX
  538. "Invalid sample_rate_min %d, reset to 16000\n",
  539. chip->sample_rate_min);
  540. chip->sample_rate_min = 16000;
  541. }
  542. err = pci_request_regions(pci, DRVNAME);
  543. if (err < 0) {
  544. kfree(chip);
  545. pci_disable_device(pci);
  546. return err;
  547. }
  548. chip->bar[0].addr = pci_resource_start(pci, 0);
  549. chip->bar[0].remap_addr = pci_ioremap_bar(pci, 0);
  550. chip->bar[1].addr = pci_resource_start(pci, 2);
  551. chip->bar[1].remap_addr = pci_ioremap_bar(pci, 2);
  552. if (!chip->bar[0].remap_addr || !chip->bar[1].remap_addr) {
  553. snd_printk(KERN_ERR SFX "ioremap error\n");
  554. err = -ENXIO;
  555. goto errout;
  556. }
  557. pci_set_master(pci);
  558. err = reset_controller(chip);
  559. if (err < 0)
  560. goto errout;
  561. if (request_irq(pci->irq, lola_interrupt, IRQF_SHARED,
  562. DRVNAME, chip)) {
  563. printk(KERN_ERR SFX "unable to grab IRQ %d\n", pci->irq);
  564. err = -EBUSY;
  565. goto errout;
  566. }
  567. chip->irq = pci->irq;
  568. synchronize_irq(chip->irq);
  569. dever = lola_readl(chip, BAR1, DEVER);
  570. chip->pcm[CAPT].num_streams = (dever >> 0) & 0x3ff;
  571. chip->pcm[PLAY].num_streams = (dever >> 10) & 0x3ff;
  572. chip->version = (dever >> 24) & 0xff;
  573. snd_printdd(SFX "streams in=%d, out=%d, version=0x%x\n",
  574. chip->pcm[CAPT].num_streams, chip->pcm[PLAY].num_streams,
  575. chip->version);
  576. /* Test LOLA_BAR1_DEVER */
  577. if (chip->pcm[CAPT].num_streams > MAX_STREAM_IN_COUNT ||
  578. chip->pcm[PLAY].num_streams > MAX_STREAM_OUT_COUNT ||
  579. (!chip->pcm[CAPT].num_streams &&
  580. !chip->pcm[PLAY].num_streams)) {
  581. printk(KERN_ERR SFX "invalid DEVER = %x\n", dever);
  582. err = -EINVAL;
  583. goto errout;
  584. }
  585. err = setup_corb_rirb(chip);
  586. if (err < 0)
  587. goto errout;
  588. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  589. if (err < 0) {
  590. snd_printk(KERN_ERR SFX "Error creating device [card]!\n");
  591. goto errout;
  592. }
  593. strcpy(card->driver, "Lola");
  594. strlcpy(card->shortname, "Digigram Lola", sizeof(card->shortname));
  595. snprintf(card->longname, sizeof(card->longname),
  596. "%s at 0x%lx irq %i",
  597. card->shortname, chip->bar[0].addr, chip->irq);
  598. strcpy(card->mixername, card->shortname);
  599. lola_irq_enable(chip);
  600. chip->initialized = 1;
  601. *rchip = chip;
  602. return 0;
  603. errout:
  604. lola_free(chip);
  605. return err;
  606. }
  607. static int __devinit lola_probe(struct pci_dev *pci,
  608. const struct pci_device_id *pci_id)
  609. {
  610. static int dev;
  611. struct snd_card *card;
  612. struct lola *chip;
  613. int err;
  614. if (dev >= SNDRV_CARDS)
  615. return -ENODEV;
  616. if (!enable[dev]) {
  617. dev++;
  618. return -ENOENT;
  619. }
  620. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  621. if (err < 0) {
  622. snd_printk(KERN_ERR SFX "Error creating card!\n");
  623. return err;
  624. }
  625. snd_card_set_dev(card, &pci->dev);
  626. err = lola_create(card, pci, dev, &chip);
  627. if (err < 0)
  628. goto out_free;
  629. card->private_data = chip;
  630. err = lola_parse_tree(chip);
  631. if (err < 0)
  632. goto out_free;
  633. err = lola_create_pcm(chip);
  634. if (err < 0)
  635. goto out_free;
  636. err = lola_create_mixer(chip);
  637. if (err < 0)
  638. goto out_free;
  639. lola_proc_debug_new(chip);
  640. err = snd_card_register(card);
  641. if (err < 0)
  642. goto out_free;
  643. pci_set_drvdata(pci, card);
  644. dev++;
  645. return err;
  646. out_free:
  647. snd_card_free(card);
  648. return err;
  649. }
  650. static void __devexit lola_remove(struct pci_dev *pci)
  651. {
  652. snd_card_free(pci_get_drvdata(pci));
  653. pci_set_drvdata(pci, NULL);
  654. }
  655. /* PCI IDs */
  656. static DEFINE_PCI_DEVICE_TABLE(lola_ids) = {
  657. { PCI_VDEVICE(DIGIGRAM, 0x0001) },
  658. { 0, }
  659. };
  660. MODULE_DEVICE_TABLE(pci, lola_ids);
  661. /* pci_driver definition */
  662. static struct pci_driver driver = {
  663. .name = DRVNAME,
  664. .id_table = lola_ids,
  665. .probe = lola_probe,
  666. .remove = __devexit_p(lola_remove),
  667. };
  668. static int __init alsa_card_lola_init(void)
  669. {
  670. return pci_register_driver(&driver);
  671. }
  672. static void __exit alsa_card_lola_exit(void)
  673. {
  674. pci_unregister_driver(&driver);
  675. }
  676. module_init(alsa_card_lola_init)
  677. module_exit(alsa_card_lola_exit)