lx6464es.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /* -*- linux-c -*- *
  2. *
  3. * ALSA driver for the digigram lx6464es interface
  4. *
  5. * Copyright (c) 2008, 2009 Tim Blechmann <tim@klingt.org>
  6. *
  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; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; see the file COPYING. If not, write to
  20. * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/delay.h>
  28. #include <sound/initval.h>
  29. #include <sound/control.h>
  30. #include <sound/info.h>
  31. #include "lx6464es.h"
  32. MODULE_AUTHOR("Tim Blechmann");
  33. MODULE_LICENSE("GPL");
  34. MODULE_DESCRIPTION("digigram lx6464es");
  35. MODULE_SUPPORTED_DEVICE("{digigram lx6464es{}}");
  36. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  37. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  38. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  39. static const char card_name[] = "LX6464ES";
  40. #define PCI_DEVICE_ID_PLX_LX6464ES PCI_DEVICE_ID_PLX_9056
  41. static struct pci_device_id snd_lx6464es_ids[] = {
  42. { PCI_DEVICE(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES),
  43. .subvendor = PCI_VENDOR_ID_DIGIGRAM,
  44. .subdevice = PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_SERIAL_SUBSYSTEM
  45. }, /* LX6464ES */
  46. { PCI_DEVICE(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES),
  47. .subvendor = PCI_VENDOR_ID_DIGIGRAM,
  48. .subdevice = PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_CAE_SERIAL_SUBSYSTEM
  49. }, /* LX6464ES-CAE */
  50. { 0, },
  51. };
  52. MODULE_DEVICE_TABLE(pci, snd_lx6464es_ids);
  53. /* PGO pour USERo dans le registre pci_0x06/loc_0xEC */
  54. #define CHIPSC_RESET_XILINX (1L<<16)
  55. /* alsa callbacks */
  56. static struct snd_pcm_hardware lx_caps = {
  57. .info = (SNDRV_PCM_INFO_MMAP |
  58. SNDRV_PCM_INFO_INTERLEAVED |
  59. SNDRV_PCM_INFO_MMAP_VALID |
  60. SNDRV_PCM_INFO_SYNC_START),
  61. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  62. SNDRV_PCM_FMTBIT_S16_BE |
  63. SNDRV_PCM_FMTBIT_S24_3LE |
  64. SNDRV_PCM_FMTBIT_S24_3BE),
  65. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  66. SNDRV_PCM_RATE_8000_192000),
  67. .rate_min = 8000,
  68. .rate_max = 192000,
  69. .channels_min = 2,
  70. .channels_max = 64,
  71. .buffer_bytes_max = 64*2*3*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER,
  72. .period_bytes_min = (2*2*MICROBLAZE_IBL_MIN*2),
  73. .period_bytes_max = (4*64*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER),
  74. .periods_min = 2,
  75. .periods_max = MAX_STREAM_BUFFER,
  76. };
  77. static int lx_set_granularity(struct lx6464es *chip, u32 gran);
  78. static int lx_hardware_open(struct lx6464es *chip,
  79. struct snd_pcm_substream *substream)
  80. {
  81. int err = 0;
  82. struct snd_pcm_runtime *runtime = substream->runtime;
  83. int channels = runtime->channels;
  84. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  85. snd_pcm_uframes_t period_size = runtime->period_size;
  86. snd_printd(LXP "allocating pipe for %d channels\n", channels);
  87. err = lx_pipe_allocate(chip, 0, is_capture, channels);
  88. if (err < 0) {
  89. snd_printk(KERN_ERR LXP "allocating pipe failed\n");
  90. return err;
  91. }
  92. err = lx_set_granularity(chip, period_size);
  93. if (err < 0) {
  94. snd_printk(KERN_ERR LXP "setting granularity to %ld failed\n",
  95. period_size);
  96. return err;
  97. }
  98. return 0;
  99. }
  100. static int lx_hardware_start(struct lx6464es *chip,
  101. struct snd_pcm_substream *substream)
  102. {
  103. int err = 0;
  104. struct snd_pcm_runtime *runtime = substream->runtime;
  105. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  106. snd_printd(LXP "setting stream format\n");
  107. err = lx_stream_set_format(chip, runtime, 0, is_capture);
  108. if (err < 0) {
  109. snd_printk(KERN_ERR LXP "setting stream format failed\n");
  110. return err;
  111. }
  112. snd_printd(LXP "starting pipe\n");
  113. err = lx_pipe_start(chip, 0, is_capture);
  114. if (err < 0) {
  115. snd_printk(KERN_ERR LXP "starting pipe failed\n");
  116. return err;
  117. }
  118. snd_printd(LXP "waiting for pipe to start\n");
  119. err = lx_pipe_wait_for_start(chip, 0, is_capture);
  120. if (err < 0) {
  121. snd_printk(KERN_ERR LXP "waiting for pipe failed\n");
  122. return err;
  123. }
  124. return err;
  125. }
  126. static int lx_hardware_stop(struct lx6464es *chip,
  127. struct snd_pcm_substream *substream)
  128. {
  129. int err = 0;
  130. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  131. snd_printd(LXP "pausing pipe\n");
  132. err = lx_pipe_pause(chip, 0, is_capture);
  133. if (err < 0) {
  134. snd_printk(KERN_ERR LXP "pausing pipe failed\n");
  135. return err;
  136. }
  137. snd_printd(LXP "waiting for pipe to become idle\n");
  138. err = lx_pipe_wait_for_idle(chip, 0, is_capture);
  139. if (err < 0) {
  140. snd_printk(KERN_ERR LXP "waiting for pipe failed\n");
  141. return err;
  142. }
  143. snd_printd(LXP "stopping pipe\n");
  144. err = lx_pipe_stop(chip, 0, is_capture);
  145. if (err < 0) {
  146. snd_printk(LXP "stopping pipe failed\n");
  147. return err;
  148. }
  149. return err;
  150. }
  151. static int lx_hardware_close(struct lx6464es *chip,
  152. struct snd_pcm_substream *substream)
  153. {
  154. int err = 0;
  155. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  156. snd_printd(LXP "releasing pipe\n");
  157. err = lx_pipe_release(chip, 0, is_capture);
  158. if (err < 0) {
  159. snd_printk(LXP "releasing pipe failed\n");
  160. return err;
  161. }
  162. return err;
  163. }
  164. static int lx_pcm_open(struct snd_pcm_substream *substream)
  165. {
  166. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  167. struct snd_pcm_runtime *runtime = substream->runtime;
  168. int err = 0;
  169. int board_rate;
  170. snd_printdd("->lx_pcm_open\n");
  171. mutex_lock(&chip->setup_mutex);
  172. /* copy the struct snd_pcm_hardware struct */
  173. runtime->hw = lx_caps;
  174. #if 0
  175. /* buffer-size should better be multiple of period-size */
  176. err = snd_pcm_hw_constraint_integer(runtime,
  177. SNDRV_PCM_HW_PARAM_PERIODS);
  178. if (err < 0) {
  179. snd_printk(KERN_WARNING LXP "could not constrain periods\n");
  180. goto exit;
  181. }
  182. #endif
  183. /* the clock rate cannot be changed */
  184. board_rate = chip->board_sample_rate;
  185. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
  186. board_rate, board_rate);
  187. if (err < 0) {
  188. snd_printk(KERN_WARNING LXP "could not constrain periods\n");
  189. goto exit;
  190. }
  191. /* constrain period size */
  192. err = snd_pcm_hw_constraint_minmax(runtime,
  193. SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  194. MICROBLAZE_IBL_MIN,
  195. MICROBLAZE_IBL_MAX);
  196. if (err < 0) {
  197. snd_printk(KERN_WARNING LXP
  198. "could not constrain period size\n");
  199. goto exit;
  200. }
  201. snd_pcm_hw_constraint_step(runtime, 0,
  202. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
  203. snd_pcm_set_sync(substream);
  204. err = 0;
  205. exit:
  206. runtime->private_data = chip;
  207. mutex_unlock(&chip->setup_mutex);
  208. snd_printdd("<-lx_pcm_open, %d\n", err);
  209. return err;
  210. }
  211. static int lx_pcm_close(struct snd_pcm_substream *substream)
  212. {
  213. int err = 0;
  214. snd_printdd("->lx_pcm_close\n");
  215. return err;
  216. }
  217. static snd_pcm_uframes_t lx_pcm_stream_pointer(struct snd_pcm_substream
  218. *substream)
  219. {
  220. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  221. snd_pcm_uframes_t pos;
  222. unsigned long flags;
  223. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  224. struct lx_stream *lx_stream = is_capture ? &chip->capture_stream :
  225. &chip->playback_stream;
  226. snd_printdd("->lx_pcm_stream_pointer\n");
  227. spin_lock_irqsave(&chip->lock, flags);
  228. pos = lx_stream->frame_pos * substream->runtime->period_size;
  229. spin_unlock_irqrestore(&chip->lock, flags);
  230. snd_printdd(LXP "stream_pointer at %ld\n", pos);
  231. return pos;
  232. }
  233. static int lx_pcm_prepare(struct snd_pcm_substream *substream)
  234. {
  235. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  236. int err = 0;
  237. const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  238. snd_printdd("->lx_pcm_prepare\n");
  239. mutex_lock(&chip->setup_mutex);
  240. if (chip->hardware_running[is_capture]) {
  241. err = lx_hardware_stop(chip, substream);
  242. if (err < 0) {
  243. snd_printk(KERN_ERR LXP "failed to stop hardware. "
  244. "Error code %d\n", err);
  245. goto exit;
  246. }
  247. err = lx_hardware_close(chip, substream);
  248. if (err < 0) {
  249. snd_printk(KERN_ERR LXP "failed to close hardware. "
  250. "Error code %d\n", err);
  251. goto exit;
  252. }
  253. }
  254. snd_printd(LXP "opening hardware\n");
  255. err = lx_hardware_open(chip, substream);
  256. if (err < 0) {
  257. snd_printk(KERN_ERR LXP "failed to open hardware. "
  258. "Error code %d\n", err);
  259. goto exit;
  260. }
  261. err = lx_hardware_start(chip, substream);
  262. if (err < 0) {
  263. snd_printk(KERN_ERR LXP "failed to start hardware. "
  264. "Error code %d\n", err);
  265. goto exit;
  266. }
  267. chip->hardware_running[is_capture] = 1;
  268. if (chip->board_sample_rate != substream->runtime->rate) {
  269. if (!err)
  270. chip->board_sample_rate = substream->runtime->rate;
  271. }
  272. exit:
  273. mutex_unlock(&chip->setup_mutex);
  274. return err;
  275. }
  276. static int lx_pcm_hw_params(struct snd_pcm_substream *substream,
  277. struct snd_pcm_hw_params *hw_params, int is_capture)
  278. {
  279. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  280. int err = 0;
  281. snd_printdd("->lx_pcm_hw_params\n");
  282. mutex_lock(&chip->setup_mutex);
  283. /* set dma buffer */
  284. err = snd_pcm_lib_malloc_pages(substream,
  285. params_buffer_bytes(hw_params));
  286. if (is_capture)
  287. chip->capture_stream.stream = substream;
  288. else
  289. chip->playback_stream.stream = substream;
  290. mutex_unlock(&chip->setup_mutex);
  291. return err;
  292. }
  293. static int lx_pcm_hw_params_playback(struct snd_pcm_substream *substream,
  294. struct snd_pcm_hw_params *hw_params)
  295. {
  296. return lx_pcm_hw_params(substream, hw_params, 0);
  297. }
  298. static int lx_pcm_hw_params_capture(struct snd_pcm_substream *substream,
  299. struct snd_pcm_hw_params *hw_params)
  300. {
  301. return lx_pcm_hw_params(substream, hw_params, 1);
  302. }
  303. static int lx_pcm_hw_free(struct snd_pcm_substream *substream)
  304. {
  305. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  306. int err = 0;
  307. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  308. snd_printdd("->lx_pcm_hw_free\n");
  309. mutex_lock(&chip->setup_mutex);
  310. if (chip->hardware_running[is_capture]) {
  311. err = lx_hardware_stop(chip, substream);
  312. if (err < 0) {
  313. snd_printk(KERN_ERR LXP "failed to stop hardware. "
  314. "Error code %d\n", err);
  315. goto exit;
  316. }
  317. err = lx_hardware_close(chip, substream);
  318. if (err < 0) {
  319. snd_printk(KERN_ERR LXP "failed to close hardware. "
  320. "Error code %d\n", err);
  321. goto exit;
  322. }
  323. chip->hardware_running[is_capture] = 0;
  324. }
  325. err = snd_pcm_lib_free_pages(substream);
  326. if (is_capture)
  327. chip->capture_stream.stream = 0;
  328. else
  329. chip->playback_stream.stream = 0;
  330. exit:
  331. mutex_unlock(&chip->setup_mutex);
  332. return err;
  333. }
  334. static void lx_trigger_start(struct lx6464es *chip, struct lx_stream *lx_stream)
  335. {
  336. struct snd_pcm_substream *substream = lx_stream->stream;
  337. const int is_capture = lx_stream->is_capture;
  338. int err;
  339. const u32 channels = substream->runtime->channels;
  340. const u32 bytes_per_frame = channels * 3;
  341. const u32 period_size = substream->runtime->period_size;
  342. const u32 periods = substream->runtime->periods;
  343. const u32 period_bytes = period_size * bytes_per_frame;
  344. dma_addr_t buf = substream->dma_buffer.addr;
  345. int i;
  346. u32 needed, freed;
  347. u32 size_array[5];
  348. for (i = 0; i != periods; ++i) {
  349. u32 buffer_index = 0;
  350. err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed,
  351. size_array);
  352. snd_printdd(LXP "starting: needed %d, freed %d\n",
  353. needed, freed);
  354. err = lx_buffer_give(chip, 0, is_capture, period_bytes,
  355. lower_32_bits(buf), upper_32_bits(buf),
  356. &buffer_index);
  357. snd_printdd(LXP "starting: buffer index %x on %p (%d bytes)\n",
  358. buffer_index, (void *)buf, period_bytes);
  359. buf += period_bytes;
  360. }
  361. err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed, size_array);
  362. snd_printdd(LXP "starting: needed %d, freed %d\n", needed, freed);
  363. snd_printd(LXP "starting: starting stream\n");
  364. err = lx_stream_start(chip, 0, is_capture);
  365. if (err < 0)
  366. snd_printk(KERN_ERR LXP "couldn't start stream\n");
  367. else
  368. lx_stream->status = LX_STREAM_STATUS_RUNNING;
  369. lx_stream->frame_pos = 0;
  370. }
  371. static void lx_trigger_stop(struct lx6464es *chip, struct lx_stream *lx_stream)
  372. {
  373. const int is_capture = lx_stream->is_capture;
  374. int err;
  375. snd_printd(LXP "stopping: stopping stream\n");
  376. err = lx_stream_stop(chip, 0, is_capture);
  377. if (err < 0)
  378. snd_printk(KERN_ERR LXP "couldn't stop stream\n");
  379. else
  380. lx_stream->status = LX_STREAM_STATUS_FREE;
  381. }
  382. static void lx_trigger_tasklet_dispatch_stream(struct lx6464es *chip,
  383. struct lx_stream *lx_stream)
  384. {
  385. switch (lx_stream->status) {
  386. case LX_STREAM_STATUS_SCHEDULE_RUN:
  387. lx_trigger_start(chip, lx_stream);
  388. break;
  389. case LX_STREAM_STATUS_SCHEDULE_STOP:
  390. lx_trigger_stop(chip, lx_stream);
  391. break;
  392. default:
  393. break;
  394. }
  395. }
  396. static void lx_trigger_tasklet(unsigned long data)
  397. {
  398. struct lx6464es *chip = (struct lx6464es *)data;
  399. unsigned long flags;
  400. snd_printdd("->lx_trigger_tasklet\n");
  401. spin_lock_irqsave(&chip->lock, flags);
  402. lx_trigger_tasklet_dispatch_stream(chip, &chip->capture_stream);
  403. lx_trigger_tasklet_dispatch_stream(chip, &chip->playback_stream);
  404. spin_unlock_irqrestore(&chip->lock, flags);
  405. }
  406. static int lx_pcm_trigger_dispatch(struct lx6464es *chip,
  407. struct lx_stream *lx_stream, int cmd)
  408. {
  409. int err = 0;
  410. switch (cmd) {
  411. case SNDRV_PCM_TRIGGER_START:
  412. lx_stream->status = LX_STREAM_STATUS_SCHEDULE_RUN;
  413. break;
  414. case SNDRV_PCM_TRIGGER_STOP:
  415. lx_stream->status = LX_STREAM_STATUS_SCHEDULE_STOP;
  416. break;
  417. default:
  418. err = -EINVAL;
  419. goto exit;
  420. }
  421. tasklet_schedule(&chip->trigger_tasklet);
  422. exit:
  423. return err;
  424. }
  425. static int lx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  426. {
  427. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  428. const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  429. struct lx_stream *stream = is_capture ? &chip->capture_stream :
  430. &chip->playback_stream;
  431. snd_printdd("->lx_pcm_trigger\n");
  432. return lx_pcm_trigger_dispatch(chip, stream, cmd);
  433. }
  434. static int snd_lx6464es_free(struct lx6464es *chip)
  435. {
  436. snd_printdd("->snd_lx6464es_free\n");
  437. lx_irq_disable(chip);
  438. if (chip->irq >= 0)
  439. free_irq(chip->irq, chip);
  440. iounmap(chip->port_dsp_bar);
  441. ioport_unmap(chip->port_plx_remapped);
  442. pci_release_regions(chip->pci);
  443. pci_disable_device(chip->pci);
  444. kfree(chip);
  445. return 0;
  446. }
  447. static int snd_lx6464es_dev_free(struct snd_device *device)
  448. {
  449. return snd_lx6464es_free(device->device_data);
  450. }
  451. /* reset the dsp during initialization */
  452. static int __devinit lx_init_xilinx_reset(struct lx6464es *chip)
  453. {
  454. int i;
  455. u32 plx_reg = lx_plx_reg_read(chip, ePLX_CHIPSC);
  456. snd_printdd("->lx_init_xilinx_reset\n");
  457. /* activate reset of xilinx */
  458. plx_reg &= ~CHIPSC_RESET_XILINX;
  459. lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
  460. msleep(1);
  461. lx_plx_reg_write(chip, ePLX_MBOX3, 0);
  462. msleep(1);
  463. plx_reg |= CHIPSC_RESET_XILINX;
  464. lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
  465. /* deactivate reset of xilinx */
  466. for (i = 0; i != 100; ++i) {
  467. u32 reg_mbox3;
  468. msleep(10);
  469. reg_mbox3 = lx_plx_reg_read(chip, ePLX_MBOX3);
  470. if (reg_mbox3) {
  471. snd_printd(LXP "xilinx reset done\n");
  472. snd_printdd(LXP "xilinx took %d loops\n", i);
  473. break;
  474. }
  475. }
  476. /* todo: add some error handling? */
  477. /* clear mr */
  478. lx_dsp_reg_write(chip, eReg_CSM, 0);
  479. /* le xilinx ES peut ne pas etre encore pret, on attend. */
  480. msleep(600);
  481. return 0;
  482. }
  483. static int __devinit lx_init_xilinx_test(struct lx6464es *chip)
  484. {
  485. u32 reg;
  486. snd_printdd("->lx_init_xilinx_test\n");
  487. /* TEST if we have access to Xilinx/MicroBlaze */
  488. lx_dsp_reg_write(chip, eReg_CSM, 0);
  489. reg = lx_dsp_reg_read(chip, eReg_CSM);
  490. if (reg) {
  491. snd_printk(KERN_ERR LXP "Problem: Reg_CSM %x.\n", reg);
  492. /* PCI9056_SPACE0_REMAP */
  493. lx_plx_reg_write(chip, ePLX_PCICR, 1);
  494. reg = lx_dsp_reg_read(chip, eReg_CSM);
  495. if (reg) {
  496. snd_printk(KERN_ERR LXP "Error: Reg_CSM %x.\n", reg);
  497. return -EAGAIN; /* seems to be appropriate */
  498. }
  499. }
  500. snd_printd(LXP "Xilinx/MicroBlaze access test successful\n");
  501. return 0;
  502. }
  503. /* initialize ethersound */
  504. static int __devinit lx_init_ethersound_config(struct lx6464es *chip)
  505. {
  506. int i;
  507. u32 orig_conf_es = lx_dsp_reg_read(chip, eReg_CONFES);
  508. u32 default_conf_es = (64 << IOCR_OUTPUTS_OFFSET) |
  509. (64 << IOCR_INPUTS_OFFSET) |
  510. (FREQ_RATIO_SINGLE_MODE << FREQ_RATIO_OFFSET);
  511. u32 conf_es = (orig_conf_es & CONFES_READ_PART_MASK)
  512. | (default_conf_es & CONFES_WRITE_PART_MASK);
  513. snd_printdd("->lx_init_ethersound\n");
  514. chip->freq_ratio = FREQ_RATIO_SINGLE_MODE;
  515. /*
  516. * write it to the card !
  517. * this actually kicks the ES xilinx, the first time since poweron.
  518. * the MAC address in the Reg_ADMACESMSB Reg_ADMACESLSB registers
  519. * is not ready before this is done, and the bit 2 in Reg_CSES is set.
  520. * */
  521. lx_dsp_reg_write(chip, eReg_CONFES, conf_es);
  522. for (i = 0; i != 1000; ++i) {
  523. if (lx_dsp_reg_read(chip, eReg_CSES) & 4) {
  524. snd_printd(LXP "ethersound initialized after %dms\n",
  525. i);
  526. goto ethersound_initialized;
  527. }
  528. msleep(1);
  529. }
  530. snd_printk(KERN_WARNING LXP
  531. "ethersound could not be initialized after %dms\n", i);
  532. return -ETIMEDOUT;
  533. ethersound_initialized:
  534. snd_printd(LXP "ethersound initialized\n");
  535. return 0;
  536. }
  537. static int __devinit lx_init_get_version_features(struct lx6464es *chip)
  538. {
  539. u32 dsp_version;
  540. int err;
  541. snd_printdd("->lx_init_get_version_features\n");
  542. err = lx_dsp_get_version(chip, &dsp_version);
  543. if (err == 0) {
  544. u32 freq;
  545. snd_printk(LXP "DSP version: V%02d.%02d #%d\n",
  546. (dsp_version>>16) & 0xff, (dsp_version>>8) & 0xff,
  547. dsp_version & 0xff);
  548. /* later: what firmware version do we expect? */
  549. /* retrieve Play/Rec features */
  550. /* done here because we may have to handle alternate
  551. * DSP files. */
  552. /* later */
  553. /* init the EtherSound sample rate */
  554. err = lx_dsp_get_clock_frequency(chip, &freq);
  555. if (err == 0)
  556. chip->board_sample_rate = freq;
  557. snd_printd(LXP "actual clock frequency %d\n", freq);
  558. } else {
  559. snd_printk(KERN_ERR LXP "DSP corrupted \n");
  560. err = -EAGAIN;
  561. }
  562. return err;
  563. }
  564. static int lx_set_granularity(struct lx6464es *chip, u32 gran)
  565. {
  566. int err = 0;
  567. u32 snapped_gran = MICROBLAZE_IBL_MIN;
  568. snd_printdd("->lx_set_granularity\n");
  569. /* blocksize is a power of 2 */
  570. while ((snapped_gran < gran) &&
  571. (snapped_gran < MICROBLAZE_IBL_MAX)) {
  572. snapped_gran *= 2;
  573. }
  574. if (snapped_gran == chip->pcm_granularity)
  575. return 0;
  576. err = lx_dsp_set_granularity(chip, snapped_gran);
  577. if (err < 0) {
  578. snd_printk(KERN_WARNING LXP "could not set granularity\n");
  579. err = -EAGAIN;
  580. }
  581. if (snapped_gran != gran)
  582. snd_printk(LXP "snapped blocksize to %d\n", snapped_gran);
  583. snd_printd(LXP "set blocksize on board %d\n", snapped_gran);
  584. chip->pcm_granularity = snapped_gran;
  585. return err;
  586. }
  587. /* initialize and test the xilinx dsp chip */
  588. static int __devinit lx_init_dsp(struct lx6464es *chip)
  589. {
  590. int err;
  591. u8 mac_address[6];
  592. int i;
  593. snd_printdd("->lx_init_dsp\n");
  594. snd_printd(LXP "initialize board\n");
  595. err = lx_init_xilinx_reset(chip);
  596. if (err)
  597. return err;
  598. snd_printd(LXP "testing board\n");
  599. err = lx_init_xilinx_test(chip);
  600. if (err)
  601. return err;
  602. snd_printd(LXP "initialize ethersound configuration\n");
  603. err = lx_init_ethersound_config(chip);
  604. if (err)
  605. return err;
  606. lx_irq_enable(chip);
  607. /** \todo the mac address should be ready by not, but it isn't,
  608. * so we wait for it */
  609. for (i = 0; i != 1000; ++i) {
  610. err = lx_dsp_get_mac(chip, mac_address);
  611. if (err)
  612. return err;
  613. if (mac_address[0] || mac_address[1] || mac_address[2] ||
  614. mac_address[3] || mac_address[4] || mac_address[5])
  615. goto mac_ready;
  616. msleep(1);
  617. }
  618. return -ETIMEDOUT;
  619. mac_ready:
  620. snd_printd(LXP "mac address ready read after: %dms\n", i);
  621. snd_printk(LXP "mac address: %02X.%02X.%02X.%02X.%02X.%02X\n",
  622. mac_address[0], mac_address[1], mac_address[2],
  623. mac_address[3], mac_address[4], mac_address[5]);
  624. err = lx_init_get_version_features(chip);
  625. if (err)
  626. return err;
  627. lx_set_granularity(chip, MICROBLAZE_IBL_DEFAULT);
  628. chip->playback_mute = 0;
  629. return err;
  630. }
  631. static struct snd_pcm_ops lx_ops_playback = {
  632. .open = lx_pcm_open,
  633. .close = lx_pcm_close,
  634. .ioctl = snd_pcm_lib_ioctl,
  635. .prepare = lx_pcm_prepare,
  636. .hw_params = lx_pcm_hw_params_playback,
  637. .hw_free = lx_pcm_hw_free,
  638. .trigger = lx_pcm_trigger,
  639. .pointer = lx_pcm_stream_pointer,
  640. };
  641. static struct snd_pcm_ops lx_ops_capture = {
  642. .open = lx_pcm_open,
  643. .close = lx_pcm_close,
  644. .ioctl = snd_pcm_lib_ioctl,
  645. .prepare = lx_pcm_prepare,
  646. .hw_params = lx_pcm_hw_params_capture,
  647. .hw_free = lx_pcm_hw_free,
  648. .trigger = lx_pcm_trigger,
  649. .pointer = lx_pcm_stream_pointer,
  650. };
  651. static int __devinit lx_pcm_create(struct lx6464es *chip)
  652. {
  653. int err;
  654. struct snd_pcm *pcm;
  655. u32 size = 64 * /* channels */
  656. 3 * /* 24 bit samples */
  657. MAX_STREAM_BUFFER * /* periods */
  658. MICROBLAZE_IBL_MAX * /* frames per period */
  659. 2; /* duplex */
  660. size = PAGE_ALIGN(size);
  661. /* hardcoded device name & channel count */
  662. err = snd_pcm_new(chip->card, (char *)card_name, 0,
  663. 1, 1, &pcm);
  664. pcm->private_data = chip;
  665. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &lx_ops_playback);
  666. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &lx_ops_capture);
  667. pcm->info_flags = 0;
  668. strcpy(pcm->name, card_name);
  669. err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  670. snd_dma_pci_data(chip->pci),
  671. size, size);
  672. if (err < 0)
  673. return err;
  674. chip->pcm = pcm;
  675. chip->capture_stream.is_capture = 1;
  676. return 0;
  677. }
  678. static int lx_control_playback_info(struct snd_kcontrol *kcontrol,
  679. struct snd_ctl_elem_info *uinfo)
  680. {
  681. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  682. uinfo->count = 1;
  683. uinfo->value.integer.min = 0;
  684. uinfo->value.integer.max = 1;
  685. return 0;
  686. }
  687. static int lx_control_playback_get(struct snd_kcontrol *kcontrol,
  688. struct snd_ctl_elem_value *ucontrol)
  689. {
  690. struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
  691. ucontrol->value.integer.value[0] = chip->playback_mute;
  692. return 0;
  693. }
  694. static int lx_control_playback_put(struct snd_kcontrol *kcontrol,
  695. struct snd_ctl_elem_value *ucontrol)
  696. {
  697. struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
  698. int changed = 0;
  699. int current_value = chip->playback_mute;
  700. if (current_value != ucontrol->value.integer.value[0]) {
  701. lx_level_unmute(chip, 0, !current_value);
  702. chip->playback_mute = !current_value;
  703. changed = 1;
  704. }
  705. return changed;
  706. }
  707. static struct snd_kcontrol_new lx_control_playback_switch __devinitdata = {
  708. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  709. .name = "PCM Playback Switch",
  710. .index = 0,
  711. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  712. .private_value = 0,
  713. .info = lx_control_playback_info,
  714. .get = lx_control_playback_get,
  715. .put = lx_control_playback_put
  716. };
  717. static void lx_proc_levels_read(struct snd_info_entry *entry,
  718. struct snd_info_buffer *buffer)
  719. {
  720. u32 levels[64];
  721. int err;
  722. int i, j;
  723. struct lx6464es *chip = entry->private_data;
  724. snd_iprintf(buffer, "capture levels:\n");
  725. err = lx_level_peaks(chip, 1, 64, levels);
  726. if (err < 0)
  727. return;
  728. for (i = 0; i != 8; ++i) {
  729. for (j = 0; j != 8; ++j)
  730. snd_iprintf(buffer, "%08x ", levels[i*8+j]);
  731. snd_iprintf(buffer, "\n");
  732. }
  733. snd_iprintf(buffer, "\nplayback levels:\n");
  734. err = lx_level_peaks(chip, 0, 64, levels);
  735. if (err < 0)
  736. return;
  737. for (i = 0; i != 8; ++i) {
  738. for (j = 0; j != 8; ++j)
  739. snd_iprintf(buffer, "%08x ", levels[i*8+j]);
  740. snd_iprintf(buffer, "\n");
  741. }
  742. snd_iprintf(buffer, "\n");
  743. }
  744. static int __devinit lx_proc_create(struct snd_card *card, struct lx6464es *chip)
  745. {
  746. struct snd_info_entry *entry;
  747. int err = snd_card_proc_new(card, "levels", &entry);
  748. if (err < 0)
  749. return err;
  750. snd_info_set_text_ops(entry, chip, lx_proc_levels_read);
  751. return 0;
  752. }
  753. static int __devinit snd_lx6464es_create(struct snd_card *card,
  754. struct pci_dev *pci,
  755. struct lx6464es **rchip)
  756. {
  757. struct lx6464es *chip;
  758. int err;
  759. static struct snd_device_ops ops = {
  760. .dev_free = snd_lx6464es_dev_free,
  761. };
  762. snd_printdd("->snd_lx6464es_create\n");
  763. *rchip = NULL;
  764. /* enable PCI device */
  765. err = pci_enable_device(pci);
  766. if (err < 0)
  767. return err;
  768. pci_set_master(pci);
  769. /* check if we can restrict PCI DMA transfers to 32 bits */
  770. err = pci_set_dma_mask(pci, DMA_32BIT_MASK);
  771. if (err < 0) {
  772. snd_printk(KERN_ERR "architecture does not support "
  773. "32bit PCI busmaster DMA\n");
  774. pci_disable_device(pci);
  775. return -ENXIO;
  776. }
  777. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  778. if (chip == NULL) {
  779. err = -ENOMEM;
  780. goto alloc_failed;
  781. }
  782. chip->card = card;
  783. chip->pci = pci;
  784. chip->irq = -1;
  785. /* initialize synchronization structs */
  786. spin_lock_init(&chip->lock);
  787. spin_lock_init(&chip->msg_lock);
  788. mutex_init(&chip->setup_mutex);
  789. tasklet_init(&chip->trigger_tasklet, lx_trigger_tasklet,
  790. (unsigned long)chip);
  791. tasklet_init(&chip->tasklet_capture, lx_tasklet_capture,
  792. (unsigned long)chip);
  793. tasklet_init(&chip->tasklet_playback, lx_tasklet_playback,
  794. (unsigned long)chip);
  795. /* request resources */
  796. err = pci_request_regions(pci, card_name);
  797. if (err < 0)
  798. goto request_regions_failed;
  799. /* plx port */
  800. chip->port_plx = pci_resource_start(pci, 1);
  801. chip->port_plx_remapped = ioport_map(chip->port_plx,
  802. pci_resource_len(pci, 1));
  803. /* dsp port */
  804. chip->port_dsp_bar = pci_ioremap_bar(pci, 2);
  805. err = request_irq(pci->irq, lx_interrupt, IRQF_SHARED,
  806. card_name, chip);
  807. if (err) {
  808. snd_printk(KERN_ERR LXP "unable to grab IRQ %d\n", pci->irq);
  809. goto request_irq_failed;
  810. }
  811. chip->irq = pci->irq;
  812. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  813. if (err < 0)
  814. goto device_new_failed;
  815. err = lx_init_dsp(chip);
  816. if (err < 0) {
  817. snd_printk(KERN_ERR LXP "error during DSP initialization\n");
  818. return err;
  819. }
  820. err = lx_pcm_create(chip);
  821. if (err < 0)
  822. return err;
  823. err = lx_proc_create(card, chip);
  824. if (err < 0)
  825. return err;
  826. err = snd_ctl_add(card, snd_ctl_new1(&lx_control_playback_switch,
  827. chip));
  828. if (err < 0)
  829. return err;
  830. snd_card_set_dev(card, &pci->dev);
  831. *rchip = chip;
  832. return 0;
  833. device_new_failed:
  834. free_irq(pci->irq, chip);
  835. request_irq_failed:
  836. pci_release_regions(pci);
  837. request_regions_failed:
  838. kfree(chip);
  839. alloc_failed:
  840. pci_disable_device(pci);
  841. return err;
  842. }
  843. static int __devinit snd_lx6464es_probe(struct pci_dev *pci,
  844. const struct pci_device_id *pci_id)
  845. {
  846. static int dev;
  847. struct snd_card *card;
  848. struct lx6464es *chip;
  849. int err;
  850. snd_printdd("->snd_lx6464es_probe\n");
  851. if (dev >= SNDRV_CARDS)
  852. return -ENODEV;
  853. if (!enable[dev]) {
  854. dev++;
  855. return -ENOENT;
  856. }
  857. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  858. if (err < 0)
  859. return err;
  860. err = snd_lx6464es_create(card, pci, &chip);
  861. if (err < 0) {
  862. snd_printk(KERN_ERR LXP "error during snd_lx6464es_create\n");
  863. goto out_free;
  864. }
  865. strcpy(card->driver, "lx6464es");
  866. strcpy(card->shortname, "Digigram LX6464ES");
  867. sprintf(card->longname, "%s at 0x%lx, 0x%p, irq %i",
  868. card->shortname, chip->port_plx,
  869. chip->port_dsp_bar, chip->irq);
  870. err = snd_card_register(card);
  871. if (err < 0)
  872. goto out_free;
  873. snd_printdd(LXP "initialization successful\n");
  874. pci_set_drvdata(pci, card);
  875. dev++;
  876. return 0;
  877. out_free:
  878. snd_card_free(card);
  879. return err;
  880. }
  881. static void __devexit snd_lx6464es_remove(struct pci_dev *pci)
  882. {
  883. snd_card_free(pci_get_drvdata(pci));
  884. pci_set_drvdata(pci, NULL);
  885. }
  886. static struct pci_driver driver = {
  887. .name = "Digigram LX6464ES",
  888. .id_table = snd_lx6464es_ids,
  889. .probe = snd_lx6464es_probe,
  890. .remove = __devexit_p(snd_lx6464es_remove),
  891. };
  892. /* module initialization */
  893. static int __init mod_init(void)
  894. {
  895. return pci_register_driver(&driver);
  896. }
  897. static void __exit mod_exit(void)
  898. {
  899. pci_unregister_driver(&driver);
  900. }
  901. module_init(mod_init);
  902. module_exit(mod_exit);