lola.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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/module.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 bool 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) pr_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. again:
  136. timeout = jiffies + msecs_to_jiffies(1000);
  137. for (;;) {
  138. if (chip->polling_mode) {
  139. spin_lock_irq(&chip->reg_lock);
  140. lola_update_rirb(chip);
  141. spin_unlock_irq(&chip->reg_lock);
  142. }
  143. if (!chip->rirb.cmds) {
  144. *val = chip->res;
  145. if (extval)
  146. *extval = chip->res_ex;
  147. verbose_debug("get_response: %x, %x\n",
  148. chip->res, chip->res_ex);
  149. if (chip->res_ex & LOLA_RIRB_EX_ERROR) {
  150. dev_warn(chip->card->dev, "RIRB ERROR: "
  151. "NID=%x, verb=%x, data=%x, ext=%x\n",
  152. chip->last_cmd_nid,
  153. chip->last_verb, chip->last_data,
  154. chip->last_extdata);
  155. return -EIO;
  156. }
  157. return 0;
  158. }
  159. if (time_after(jiffies, timeout))
  160. break;
  161. udelay(20);
  162. cond_resched();
  163. }
  164. dev_warn(chip->card->dev, "RIRB response error\n");
  165. if (!chip->polling_mode) {
  166. dev_warn(chip->card->dev, "switching to polling mode\n");
  167. chip->polling_mode = 1;
  168. goto again;
  169. }
  170. return -EIO;
  171. }
  172. /* aynchronous write of a codec verb with data */
  173. int lola_codec_write(struct lola *chip, unsigned int nid, unsigned int verb,
  174. unsigned int data, unsigned int extdata)
  175. {
  176. verbose_debug("codec_write NID=%x, verb=%x, data=%x, ext=%x\n",
  177. nid, verb, data, extdata);
  178. return corb_send_verb(chip, nid, verb, data, extdata);
  179. }
  180. /* write a codec verb with data and read the returned status */
  181. int lola_codec_read(struct lola *chip, unsigned int nid, unsigned int verb,
  182. unsigned int data, unsigned int extdata,
  183. unsigned int *val, unsigned int *extval)
  184. {
  185. int err;
  186. verbose_debug("codec_read NID=%x, verb=%x, data=%x, ext=%x\n",
  187. nid, verb, data, extdata);
  188. err = corb_send_verb(chip, nid, verb, data, extdata);
  189. if (err < 0)
  190. return err;
  191. err = rirb_get_response(chip, val, extval);
  192. return err;
  193. }
  194. /* flush all pending codec writes */
  195. int lola_codec_flush(struct lola *chip)
  196. {
  197. unsigned int tmp;
  198. return rirb_get_response(chip, &tmp, NULL);
  199. }
  200. /*
  201. * interrupt handler
  202. */
  203. static irqreturn_t lola_interrupt(int irq, void *dev_id)
  204. {
  205. struct lola *chip = dev_id;
  206. unsigned int notify_ins, notify_outs, error_ins, error_outs;
  207. int handled = 0;
  208. int i;
  209. notify_ins = notify_outs = error_ins = error_outs = 0;
  210. spin_lock(&chip->reg_lock);
  211. for (;;) {
  212. unsigned int status, in_sts, out_sts;
  213. unsigned int reg;
  214. status = lola_readl(chip, BAR1, DINTSTS);
  215. if (!status || status == -1)
  216. break;
  217. in_sts = lola_readl(chip, BAR1, DIINTSTS);
  218. out_sts = lola_readl(chip, BAR1, DOINTSTS);
  219. /* clear Input Interrupts */
  220. for (i = 0; in_sts && i < chip->pcm[CAPT].num_streams; i++) {
  221. if (!(in_sts & (1 << i)))
  222. continue;
  223. in_sts &= ~(1 << i);
  224. reg = lola_dsd_read(chip, i, STS);
  225. if (reg & LOLA_DSD_STS_DESE) /* error */
  226. error_ins |= (1 << i);
  227. if (reg & LOLA_DSD_STS_BCIS) /* notify */
  228. notify_ins |= (1 << i);
  229. /* clear */
  230. lola_dsd_write(chip, i, STS, reg);
  231. }
  232. /* clear Output Interrupts */
  233. for (i = 0; out_sts && i < chip->pcm[PLAY].num_streams; i++) {
  234. if (!(out_sts & (1 << i)))
  235. continue;
  236. out_sts &= ~(1 << i);
  237. reg = lola_dsd_read(chip, i + MAX_STREAM_IN_COUNT, STS);
  238. if (reg & LOLA_DSD_STS_DESE) /* error */
  239. error_outs |= (1 << i);
  240. if (reg & LOLA_DSD_STS_BCIS) /* notify */
  241. notify_outs |= (1 << i);
  242. lola_dsd_write(chip, i + MAX_STREAM_IN_COUNT, STS, reg);
  243. }
  244. if (status & LOLA_DINT_CTRL) {
  245. unsigned char rbsts; /* ring status is byte access */
  246. rbsts = lola_readb(chip, BAR0, RIRBSTS);
  247. rbsts &= LOLA_RIRB_INT_MASK;
  248. if (rbsts)
  249. lola_writeb(chip, BAR0, RIRBSTS, rbsts);
  250. rbsts = lola_readb(chip, BAR0, CORBSTS);
  251. rbsts &= LOLA_CORB_INT_MASK;
  252. if (rbsts)
  253. lola_writeb(chip, BAR0, CORBSTS, rbsts);
  254. lola_update_rirb(chip);
  255. }
  256. if (status & (LOLA_DINT_FIFOERR | LOLA_DINT_MUERR)) {
  257. /* clear global fifo error interrupt */
  258. lola_writel(chip, BAR1, DINTSTS,
  259. (status & (LOLA_DINT_FIFOERR | LOLA_DINT_MUERR)));
  260. }
  261. handled = 1;
  262. }
  263. spin_unlock(&chip->reg_lock);
  264. lola_pcm_update(chip, &chip->pcm[CAPT], notify_ins);
  265. lola_pcm_update(chip, &chip->pcm[PLAY], notify_outs);
  266. return IRQ_RETVAL(handled);
  267. }
  268. /*
  269. * controller
  270. */
  271. static int reset_controller(struct lola *chip)
  272. {
  273. unsigned int gctl = lola_readl(chip, BAR0, GCTL);
  274. unsigned long end_time;
  275. if (gctl) {
  276. /* to be sure */
  277. lola_writel(chip, BAR1, BOARD_MODE, 0);
  278. return 0;
  279. }
  280. chip->cold_reset = 1;
  281. lola_writel(chip, BAR0, GCTL, LOLA_GCTL_RESET);
  282. end_time = jiffies + msecs_to_jiffies(200);
  283. do {
  284. msleep(1);
  285. gctl = lola_readl(chip, BAR0, GCTL);
  286. if (gctl)
  287. break;
  288. } while (time_before(jiffies, end_time));
  289. if (!gctl) {
  290. dev_err(chip->card->dev, "cannot reset controller\n");
  291. return -EIO;
  292. }
  293. return 0;
  294. }
  295. static void lola_irq_enable(struct lola *chip)
  296. {
  297. unsigned int val;
  298. /* enalbe all I/O streams */
  299. val = (1 << chip->pcm[PLAY].num_streams) - 1;
  300. lola_writel(chip, BAR1, DOINTCTL, val);
  301. val = (1 << chip->pcm[CAPT].num_streams) - 1;
  302. lola_writel(chip, BAR1, DIINTCTL, val);
  303. /* enable global irqs */
  304. val = LOLA_DINT_GLOBAL | LOLA_DINT_CTRL | LOLA_DINT_FIFOERR |
  305. LOLA_DINT_MUERR;
  306. lola_writel(chip, BAR1, DINTCTL, val);
  307. }
  308. static void lola_irq_disable(struct lola *chip)
  309. {
  310. lola_writel(chip, BAR1, DINTCTL, 0);
  311. lola_writel(chip, BAR1, DIINTCTL, 0);
  312. lola_writel(chip, BAR1, DOINTCTL, 0);
  313. }
  314. static int setup_corb_rirb(struct lola *chip)
  315. {
  316. int err;
  317. unsigned char tmp;
  318. unsigned long end_time;
  319. err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
  320. snd_dma_pci_data(chip->pci),
  321. PAGE_SIZE, &chip->rb);
  322. if (err < 0)
  323. return err;
  324. chip->corb.addr = chip->rb.addr;
  325. chip->corb.buf = (u32 *)chip->rb.area;
  326. chip->rirb.addr = chip->rb.addr + 2048;
  327. chip->rirb.buf = (u32 *)(chip->rb.area + 2048);
  328. /* disable ringbuffer DMAs */
  329. lola_writeb(chip, BAR0, RIRBCTL, 0);
  330. lola_writeb(chip, BAR0, CORBCTL, 0);
  331. end_time = jiffies + msecs_to_jiffies(200);
  332. do {
  333. if (!lola_readb(chip, BAR0, RIRBCTL) &&
  334. !lola_readb(chip, BAR0, CORBCTL))
  335. break;
  336. msleep(1);
  337. } while (time_before(jiffies, end_time));
  338. /* CORB set up */
  339. lola_writel(chip, BAR0, CORBLBASE, (u32)chip->corb.addr);
  340. lola_writel(chip, BAR0, CORBUBASE, upper_32_bits(chip->corb.addr));
  341. /* set the corb size to 256 entries */
  342. lola_writeb(chip, BAR0, CORBSIZE, 0x02);
  343. /* set the corb write pointer to 0 */
  344. lola_writew(chip, BAR0, CORBWP, 0);
  345. /* reset the corb hw read pointer */
  346. lola_writew(chip, BAR0, CORBRP, LOLA_RBRWP_CLR);
  347. /* enable corb dma */
  348. lola_writeb(chip, BAR0, CORBCTL, LOLA_RBCTL_DMA_EN);
  349. /* clear flags if set */
  350. tmp = lola_readb(chip, BAR0, CORBSTS) & LOLA_CORB_INT_MASK;
  351. if (tmp)
  352. lola_writeb(chip, BAR0, CORBSTS, tmp);
  353. chip->corb.wp = 0;
  354. /* RIRB set up */
  355. lola_writel(chip, BAR0, RIRBLBASE, (u32)chip->rirb.addr);
  356. lola_writel(chip, BAR0, RIRBUBASE, upper_32_bits(chip->rirb.addr));
  357. /* set the rirb size to 256 entries */
  358. lola_writeb(chip, BAR0, RIRBSIZE, 0x02);
  359. /* reset the rirb hw write pointer */
  360. lola_writew(chip, BAR0, RIRBWP, LOLA_RBRWP_CLR);
  361. /* set N=1, get RIRB response interrupt for new entry */
  362. lola_writew(chip, BAR0, RINTCNT, 1);
  363. /* enable rirb dma and response irq */
  364. lola_writeb(chip, BAR0, RIRBCTL, LOLA_RBCTL_DMA_EN | LOLA_RBCTL_IRQ_EN);
  365. /* clear flags if set */
  366. tmp = lola_readb(chip, BAR0, RIRBSTS) & LOLA_RIRB_INT_MASK;
  367. if (tmp)
  368. lola_writeb(chip, BAR0, RIRBSTS, tmp);
  369. chip->rirb.rp = chip->rirb.cmds = 0;
  370. return 0;
  371. }
  372. static void stop_corb_rirb(struct lola *chip)
  373. {
  374. /* disable ringbuffer DMAs */
  375. lola_writeb(chip, BAR0, RIRBCTL, 0);
  376. lola_writeb(chip, BAR0, CORBCTL, 0);
  377. }
  378. static void lola_reset_setups(struct lola *chip)
  379. {
  380. /* update the granularity */
  381. lola_set_granularity(chip, chip->granularity, true);
  382. /* update the sample clock */
  383. lola_set_clock_index(chip, chip->clock.cur_index);
  384. /* enable unsolicited events of the clock widget */
  385. lola_enable_clock_events(chip);
  386. /* update the analog gains */
  387. lola_setup_all_analog_gains(chip, CAPT, false); /* input, update */
  388. /* update SRC configuration if applicable */
  389. lola_set_src_config(chip, chip->input_src_mask, false);
  390. /* update the analog outputs */
  391. lola_setup_all_analog_gains(chip, PLAY, false); /* output, update */
  392. }
  393. static int lola_parse_tree(struct lola *chip)
  394. {
  395. unsigned int val;
  396. int nid, err;
  397. err = lola_read_param(chip, 0, LOLA_PAR_VENDOR_ID, &val);
  398. if (err < 0) {
  399. dev_err(chip->card->dev, "Can't read VENDOR_ID\n");
  400. return err;
  401. }
  402. val >>= 16;
  403. if (val != 0x1369) {
  404. dev_err(chip->card->dev, "Unknown codec vendor 0x%x\n", val);
  405. return -EINVAL;
  406. }
  407. err = lola_read_param(chip, 1, LOLA_PAR_FUNCTION_TYPE, &val);
  408. if (err < 0) {
  409. dev_err(chip->card->dev, "Can't read FUNCTION_TYPE\n");
  410. return err;
  411. }
  412. if (val != 1) {
  413. dev_err(chip->card->dev, "Unknown function type %d\n", val);
  414. return -EINVAL;
  415. }
  416. err = lola_read_param(chip, 1, LOLA_PAR_SPECIFIC_CAPS, &val);
  417. if (err < 0) {
  418. dev_err(chip->card->dev, "Can't read SPECCAPS\n");
  419. return err;
  420. }
  421. chip->lola_caps = val;
  422. chip->pin[CAPT].num_pins = LOLA_AFG_INPUT_PIN_COUNT(chip->lola_caps);
  423. chip->pin[PLAY].num_pins = LOLA_AFG_OUTPUT_PIN_COUNT(chip->lola_caps);
  424. dev_dbg(chip->card->dev, "speccaps=0x%x, pins in=%d, out=%d\n",
  425. chip->lola_caps,
  426. chip->pin[CAPT].num_pins, chip->pin[PLAY].num_pins);
  427. if (chip->pin[CAPT].num_pins > MAX_AUDIO_INOUT_COUNT ||
  428. chip->pin[PLAY].num_pins > MAX_AUDIO_INOUT_COUNT) {
  429. dev_err(chip->card->dev, "Invalid Lola-spec caps 0x%x\n", val);
  430. return -EINVAL;
  431. }
  432. nid = 0x02;
  433. err = lola_init_pcm(chip, CAPT, &nid);
  434. if (err < 0)
  435. return err;
  436. err = lola_init_pcm(chip, PLAY, &nid);
  437. if (err < 0)
  438. return err;
  439. err = lola_init_pins(chip, CAPT, &nid);
  440. if (err < 0)
  441. return err;
  442. err = lola_init_pins(chip, PLAY, &nid);
  443. if (err < 0)
  444. return err;
  445. if (LOLA_AFG_CLOCK_WIDGET_PRESENT(chip->lola_caps)) {
  446. err = lola_init_clock_widget(chip, nid);
  447. if (err < 0)
  448. return err;
  449. nid++;
  450. }
  451. if (LOLA_AFG_MIXER_WIDGET_PRESENT(chip->lola_caps)) {
  452. err = lola_init_mixer_widget(chip, nid);
  453. if (err < 0)
  454. return err;
  455. nid++;
  456. }
  457. /* enable unsolicited events of the clock widget */
  458. err = lola_enable_clock_events(chip);
  459. if (err < 0)
  460. return err;
  461. /* if last ResetController was not a ColdReset, we don't know
  462. * the state of the card; initialize here again
  463. */
  464. if (!chip->cold_reset) {
  465. lola_reset_setups(chip);
  466. chip->cold_reset = 1;
  467. } else {
  468. /* set the granularity if it is not the default */
  469. if (chip->granularity != LOLA_GRANULARITY_MIN)
  470. lola_set_granularity(chip, chip->granularity, true);
  471. }
  472. return 0;
  473. }
  474. static void lola_stop_hw(struct lola *chip)
  475. {
  476. stop_corb_rirb(chip);
  477. lola_irq_disable(chip);
  478. }
  479. static void lola_free(struct lola *chip)
  480. {
  481. if (chip->initialized)
  482. lola_stop_hw(chip);
  483. lola_free_pcm(chip);
  484. lola_free_mixer(chip);
  485. if (chip->irq >= 0)
  486. free_irq(chip->irq, (void *)chip);
  487. iounmap(chip->bar[0].remap_addr);
  488. iounmap(chip->bar[1].remap_addr);
  489. if (chip->rb.area)
  490. snd_dma_free_pages(&chip->rb);
  491. pci_release_regions(chip->pci);
  492. pci_disable_device(chip->pci);
  493. kfree(chip);
  494. }
  495. static int lola_dev_free(struct snd_device *device)
  496. {
  497. lola_free(device->device_data);
  498. return 0;
  499. }
  500. static int lola_create(struct snd_card *card, struct pci_dev *pci,
  501. int dev, struct lola **rchip)
  502. {
  503. struct lola *chip;
  504. int err;
  505. unsigned int dever;
  506. static struct snd_device_ops ops = {
  507. .dev_free = lola_dev_free,
  508. };
  509. *rchip = NULL;
  510. err = pci_enable_device(pci);
  511. if (err < 0)
  512. return err;
  513. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  514. if (!chip) {
  515. pci_disable_device(pci);
  516. return -ENOMEM;
  517. }
  518. spin_lock_init(&chip->reg_lock);
  519. mutex_init(&chip->open_mutex);
  520. chip->card = card;
  521. chip->pci = pci;
  522. chip->irq = -1;
  523. chip->granularity = granularity[dev];
  524. switch (chip->granularity) {
  525. case 8:
  526. chip->sample_rate_max = 48000;
  527. break;
  528. case 16:
  529. chip->sample_rate_max = 96000;
  530. break;
  531. case 32:
  532. chip->sample_rate_max = 192000;
  533. break;
  534. default:
  535. dev_warn(chip->card->dev,
  536. "Invalid granularity %d, reset to %d\n",
  537. chip->granularity, LOLA_GRANULARITY_MAX);
  538. chip->granularity = LOLA_GRANULARITY_MAX;
  539. chip->sample_rate_max = 192000;
  540. break;
  541. }
  542. chip->sample_rate_min = sample_rate_min[dev];
  543. if (chip->sample_rate_min > chip->sample_rate_max) {
  544. dev_warn(chip->card->dev,
  545. "Invalid sample_rate_min %d, reset to 16000\n",
  546. chip->sample_rate_min);
  547. chip->sample_rate_min = 16000;
  548. }
  549. err = pci_request_regions(pci, DRVNAME);
  550. if (err < 0) {
  551. kfree(chip);
  552. pci_disable_device(pci);
  553. return err;
  554. }
  555. chip->bar[0].addr = pci_resource_start(pci, 0);
  556. chip->bar[0].remap_addr = pci_ioremap_bar(pci, 0);
  557. chip->bar[1].addr = pci_resource_start(pci, 2);
  558. chip->bar[1].remap_addr = pci_ioremap_bar(pci, 2);
  559. if (!chip->bar[0].remap_addr || !chip->bar[1].remap_addr) {
  560. dev_err(chip->card->dev, "ioremap error\n");
  561. err = -ENXIO;
  562. goto errout;
  563. }
  564. pci_set_master(pci);
  565. err = reset_controller(chip);
  566. if (err < 0)
  567. goto errout;
  568. if (request_irq(pci->irq, lola_interrupt, IRQF_SHARED,
  569. KBUILD_MODNAME, chip)) {
  570. dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
  571. err = -EBUSY;
  572. goto errout;
  573. }
  574. chip->irq = pci->irq;
  575. synchronize_irq(chip->irq);
  576. dever = lola_readl(chip, BAR1, DEVER);
  577. chip->pcm[CAPT].num_streams = (dever >> 0) & 0x3ff;
  578. chip->pcm[PLAY].num_streams = (dever >> 10) & 0x3ff;
  579. chip->version = (dever >> 24) & 0xff;
  580. dev_dbg(chip->card->dev, "streams in=%d, out=%d, version=0x%x\n",
  581. chip->pcm[CAPT].num_streams, chip->pcm[PLAY].num_streams,
  582. chip->version);
  583. /* Test LOLA_BAR1_DEVER */
  584. if (chip->pcm[CAPT].num_streams > MAX_STREAM_IN_COUNT ||
  585. chip->pcm[PLAY].num_streams > MAX_STREAM_OUT_COUNT ||
  586. (!chip->pcm[CAPT].num_streams &&
  587. !chip->pcm[PLAY].num_streams)) {
  588. dev_err(chip->card->dev, "invalid DEVER = %x\n", dever);
  589. err = -EINVAL;
  590. goto errout;
  591. }
  592. err = setup_corb_rirb(chip);
  593. if (err < 0)
  594. goto errout;
  595. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  596. if (err < 0) {
  597. dev_err(chip->card->dev, "Error creating device [card]!\n");
  598. goto errout;
  599. }
  600. strcpy(card->driver, "Lola");
  601. strlcpy(card->shortname, "Digigram Lola", sizeof(card->shortname));
  602. snprintf(card->longname, sizeof(card->longname),
  603. "%s at 0x%lx irq %i",
  604. card->shortname, chip->bar[0].addr, chip->irq);
  605. strcpy(card->mixername, card->shortname);
  606. lola_irq_enable(chip);
  607. chip->initialized = 1;
  608. *rchip = chip;
  609. return 0;
  610. errout:
  611. lola_free(chip);
  612. return err;
  613. }
  614. static int lola_probe(struct pci_dev *pci,
  615. const struct pci_device_id *pci_id)
  616. {
  617. static int dev;
  618. struct snd_card *card;
  619. struct lola *chip;
  620. int err;
  621. if (dev >= SNDRV_CARDS)
  622. return -ENODEV;
  623. if (!enable[dev]) {
  624. dev++;
  625. return -ENOENT;
  626. }
  627. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  628. 0, &card);
  629. if (err < 0) {
  630. dev_err(&pci->dev, "Error creating card!\n");
  631. return err;
  632. }
  633. err = lola_create(card, pci, dev, &chip);
  634. if (err < 0)
  635. goto out_free;
  636. card->private_data = chip;
  637. err = lola_parse_tree(chip);
  638. if (err < 0)
  639. goto out_free;
  640. err = lola_create_pcm(chip);
  641. if (err < 0)
  642. goto out_free;
  643. err = lola_create_mixer(chip);
  644. if (err < 0)
  645. goto out_free;
  646. lola_proc_debug_new(chip);
  647. err = snd_card_register(card);
  648. if (err < 0)
  649. goto out_free;
  650. pci_set_drvdata(pci, card);
  651. dev++;
  652. return err;
  653. out_free:
  654. snd_card_free(card);
  655. return err;
  656. }
  657. static void lola_remove(struct pci_dev *pci)
  658. {
  659. snd_card_free(pci_get_drvdata(pci));
  660. }
  661. /* PCI IDs */
  662. static const struct pci_device_id lola_ids[] = {
  663. { PCI_VDEVICE(DIGIGRAM, 0x0001) },
  664. { 0, }
  665. };
  666. MODULE_DEVICE_TABLE(pci, lola_ids);
  667. /* pci_driver definition */
  668. static struct pci_driver lola_driver = {
  669. .name = KBUILD_MODNAME,
  670. .id_table = lola_ids,
  671. .probe = lola_probe,
  672. .remove = lola_remove,
  673. };
  674. module_pci_driver(lola_driver);