lx6464es.c 27 KB

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