lx6464es.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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 <linux/slab.h>
  29. #include <sound/initval.h>
  30. #include <sound/control.h>
  31. #include <sound/info.h>
  32. #include "lx6464es.h"
  33. MODULE_AUTHOR("Tim Blechmann");
  34. MODULE_LICENSE("GPL");
  35. MODULE_DESCRIPTION("digigram lx6464es");
  36. MODULE_SUPPORTED_DEVICE("{digigram lx6464es{}}");
  37. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  38. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  39. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  40. module_param_array(index, int, NULL, 0444);
  41. MODULE_PARM_DESC(index, "Index value for Digigram LX6464ES interface.");
  42. module_param_array(id, charp, NULL, 0444);
  43. MODULE_PARM_DESC(id, "ID string for Digigram LX6464ES interface.");
  44. module_param_array(enable, bool, NULL, 0444);
  45. MODULE_PARM_DESC(enable, "Enable/disable specific Digigram LX6464ES soundcards.");
  46. static const char card_name[] = "LX6464ES";
  47. #define PCI_DEVICE_ID_PLX_LX6464ES PCI_DEVICE_ID_PLX_9056
  48. static DEFINE_PCI_DEVICE_TABLE(snd_lx6464es_ids) = {
  49. { PCI_DEVICE(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES),
  50. .subvendor = PCI_VENDOR_ID_DIGIGRAM,
  51. .subdevice = PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_SERIAL_SUBSYSTEM
  52. }, /* LX6464ES */
  53. { PCI_DEVICE(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES),
  54. .subvendor = PCI_VENDOR_ID_DIGIGRAM,
  55. .subdevice = PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_CAE_SERIAL_SUBSYSTEM
  56. }, /* LX6464ES-CAE */
  57. { 0, },
  58. };
  59. MODULE_DEVICE_TABLE(pci, snd_lx6464es_ids);
  60. /* PGO pour USERo dans le registre pci_0x06/loc_0xEC */
  61. #define CHIPSC_RESET_XILINX (1L<<16)
  62. /* alsa callbacks */
  63. static struct snd_pcm_hardware lx_caps = {
  64. .info = (SNDRV_PCM_INFO_MMAP |
  65. SNDRV_PCM_INFO_INTERLEAVED |
  66. SNDRV_PCM_INFO_MMAP_VALID |
  67. SNDRV_PCM_INFO_SYNC_START),
  68. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  69. SNDRV_PCM_FMTBIT_S16_BE |
  70. SNDRV_PCM_FMTBIT_S24_3LE |
  71. SNDRV_PCM_FMTBIT_S24_3BE),
  72. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  73. SNDRV_PCM_RATE_8000_192000),
  74. .rate_min = 8000,
  75. .rate_max = 192000,
  76. .channels_min = 2,
  77. .channels_max = 64,
  78. .buffer_bytes_max = 64*2*3*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER,
  79. .period_bytes_min = (2*2*MICROBLAZE_IBL_MIN*2),
  80. .period_bytes_max = (4*64*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER),
  81. .periods_min = 2,
  82. .periods_max = MAX_STREAM_BUFFER,
  83. };
  84. static int lx_set_granularity(struct lx6464es *chip, u32 gran);
  85. static int lx_hardware_open(struct lx6464es *chip,
  86. struct snd_pcm_substream *substream)
  87. {
  88. int err = 0;
  89. struct snd_pcm_runtime *runtime = substream->runtime;
  90. int channels = runtime->channels;
  91. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  92. snd_pcm_uframes_t period_size = runtime->period_size;
  93. snd_printd(LXP "allocating pipe for %d channels\n", channels);
  94. err = lx_pipe_allocate(chip, 0, is_capture, channels);
  95. if (err < 0) {
  96. snd_printk(KERN_ERR LXP "allocating pipe failed\n");
  97. return err;
  98. }
  99. err = lx_set_granularity(chip, period_size);
  100. if (err < 0) {
  101. snd_printk(KERN_ERR LXP "setting granularity to %ld failed\n",
  102. period_size);
  103. return err;
  104. }
  105. return 0;
  106. }
  107. static int lx_hardware_start(struct lx6464es *chip,
  108. struct snd_pcm_substream *substream)
  109. {
  110. int err = 0;
  111. struct snd_pcm_runtime *runtime = substream->runtime;
  112. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  113. snd_printd(LXP "setting stream format\n");
  114. err = lx_stream_set_format(chip, runtime, 0, is_capture);
  115. if (err < 0) {
  116. snd_printk(KERN_ERR LXP "setting stream format failed\n");
  117. return err;
  118. }
  119. snd_printd(LXP "starting pipe\n");
  120. err = lx_pipe_start(chip, 0, is_capture);
  121. if (err < 0) {
  122. snd_printk(KERN_ERR LXP "starting pipe failed\n");
  123. return err;
  124. }
  125. snd_printd(LXP "waiting for pipe to start\n");
  126. err = lx_pipe_wait_for_start(chip, 0, is_capture);
  127. if (err < 0) {
  128. snd_printk(KERN_ERR LXP "waiting for pipe failed\n");
  129. return err;
  130. }
  131. return err;
  132. }
  133. static int lx_hardware_stop(struct lx6464es *chip,
  134. struct snd_pcm_substream *substream)
  135. {
  136. int err = 0;
  137. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  138. snd_printd(LXP "pausing pipe\n");
  139. err = lx_pipe_pause(chip, 0, is_capture);
  140. if (err < 0) {
  141. snd_printk(KERN_ERR LXP "pausing pipe failed\n");
  142. return err;
  143. }
  144. snd_printd(LXP "waiting for pipe to become idle\n");
  145. err = lx_pipe_wait_for_idle(chip, 0, is_capture);
  146. if (err < 0) {
  147. snd_printk(KERN_ERR LXP "waiting for pipe failed\n");
  148. return err;
  149. }
  150. snd_printd(LXP "stopping pipe\n");
  151. err = lx_pipe_stop(chip, 0, is_capture);
  152. if (err < 0) {
  153. snd_printk(LXP "stopping pipe failed\n");
  154. return err;
  155. }
  156. return err;
  157. }
  158. static int lx_hardware_close(struct lx6464es *chip,
  159. struct snd_pcm_substream *substream)
  160. {
  161. int err = 0;
  162. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  163. snd_printd(LXP "releasing pipe\n");
  164. err = lx_pipe_release(chip, 0, is_capture);
  165. if (err < 0) {
  166. snd_printk(LXP "releasing pipe failed\n");
  167. return err;
  168. }
  169. return err;
  170. }
  171. static int lx_pcm_open(struct snd_pcm_substream *substream)
  172. {
  173. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  174. struct snd_pcm_runtime *runtime = substream->runtime;
  175. int err = 0;
  176. int board_rate;
  177. snd_printdd("->lx_pcm_open\n");
  178. mutex_lock(&chip->setup_mutex);
  179. /* copy the struct snd_pcm_hardware struct */
  180. runtime->hw = lx_caps;
  181. #if 0
  182. /* buffer-size should better be multiple of period-size */
  183. err = snd_pcm_hw_constraint_integer(runtime,
  184. SNDRV_PCM_HW_PARAM_PERIODS);
  185. if (err < 0) {
  186. snd_printk(KERN_WARNING LXP "could not constrain periods\n");
  187. goto exit;
  188. }
  189. #endif
  190. /* the clock rate cannot be changed */
  191. board_rate = chip->board_sample_rate;
  192. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
  193. board_rate, board_rate);
  194. if (err < 0) {
  195. snd_printk(KERN_WARNING LXP "could not constrain periods\n");
  196. goto exit;
  197. }
  198. /* constrain period size */
  199. err = snd_pcm_hw_constraint_minmax(runtime,
  200. SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  201. MICROBLAZE_IBL_MIN,
  202. MICROBLAZE_IBL_MAX);
  203. if (err < 0) {
  204. snd_printk(KERN_WARNING LXP
  205. "could not constrain period size\n");
  206. goto exit;
  207. }
  208. snd_pcm_hw_constraint_step(runtime, 0,
  209. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
  210. snd_pcm_set_sync(substream);
  211. err = 0;
  212. exit:
  213. runtime->private_data = chip;
  214. mutex_unlock(&chip->setup_mutex);
  215. snd_printdd("<-lx_pcm_open, %d\n", err);
  216. return err;
  217. }
  218. static int lx_pcm_close(struct snd_pcm_substream *substream)
  219. {
  220. int err = 0;
  221. snd_printdd("->lx_pcm_close\n");
  222. return err;
  223. }
  224. static snd_pcm_uframes_t lx_pcm_stream_pointer(struct snd_pcm_substream
  225. *substream)
  226. {
  227. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  228. snd_pcm_uframes_t pos;
  229. unsigned long flags;
  230. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  231. struct lx_stream *lx_stream = is_capture ? &chip->capture_stream :
  232. &chip->playback_stream;
  233. snd_printdd("->lx_pcm_stream_pointer\n");
  234. spin_lock_irqsave(&chip->lock, flags);
  235. pos = lx_stream->frame_pos * substream->runtime->period_size;
  236. spin_unlock_irqrestore(&chip->lock, flags);
  237. snd_printdd(LXP "stream_pointer at %ld\n", pos);
  238. return pos;
  239. }
  240. static int lx_pcm_prepare(struct snd_pcm_substream *substream)
  241. {
  242. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  243. int err = 0;
  244. const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  245. snd_printdd("->lx_pcm_prepare\n");
  246. mutex_lock(&chip->setup_mutex);
  247. if (chip->hardware_running[is_capture]) {
  248. err = lx_hardware_stop(chip, substream);
  249. if (err < 0) {
  250. snd_printk(KERN_ERR LXP "failed to stop hardware. "
  251. "Error code %d\n", err);
  252. goto exit;
  253. }
  254. err = lx_hardware_close(chip, substream);
  255. if (err < 0) {
  256. snd_printk(KERN_ERR LXP "failed to close hardware. "
  257. "Error code %d\n", err);
  258. goto exit;
  259. }
  260. }
  261. snd_printd(LXP "opening hardware\n");
  262. err = lx_hardware_open(chip, substream);
  263. if (err < 0) {
  264. snd_printk(KERN_ERR LXP "failed to open hardware. "
  265. "Error code %d\n", err);
  266. goto exit;
  267. }
  268. err = lx_hardware_start(chip, substream);
  269. if (err < 0) {
  270. snd_printk(KERN_ERR LXP "failed to start hardware. "
  271. "Error code %d\n", err);
  272. goto exit;
  273. }
  274. chip->hardware_running[is_capture] = 1;
  275. if (chip->board_sample_rate != substream->runtime->rate) {
  276. if (!err)
  277. chip->board_sample_rate = substream->runtime->rate;
  278. }
  279. exit:
  280. mutex_unlock(&chip->setup_mutex);
  281. return err;
  282. }
  283. static int lx_pcm_hw_params(struct snd_pcm_substream *substream,
  284. struct snd_pcm_hw_params *hw_params, int is_capture)
  285. {
  286. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  287. int err = 0;
  288. snd_printdd("->lx_pcm_hw_params\n");
  289. mutex_lock(&chip->setup_mutex);
  290. /* set dma buffer */
  291. err = snd_pcm_lib_malloc_pages(substream,
  292. params_buffer_bytes(hw_params));
  293. if (is_capture)
  294. chip->capture_stream.stream = substream;
  295. else
  296. chip->playback_stream.stream = substream;
  297. mutex_unlock(&chip->setup_mutex);
  298. return err;
  299. }
  300. static int lx_pcm_hw_params_playback(struct snd_pcm_substream *substream,
  301. struct snd_pcm_hw_params *hw_params)
  302. {
  303. return lx_pcm_hw_params(substream, hw_params, 0);
  304. }
  305. static int lx_pcm_hw_params_capture(struct snd_pcm_substream *substream,
  306. struct snd_pcm_hw_params *hw_params)
  307. {
  308. return lx_pcm_hw_params(substream, hw_params, 1);
  309. }
  310. static int lx_pcm_hw_free(struct snd_pcm_substream *substream)
  311. {
  312. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  313. int err = 0;
  314. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  315. snd_printdd("->lx_pcm_hw_free\n");
  316. mutex_lock(&chip->setup_mutex);
  317. if (chip->hardware_running[is_capture]) {
  318. err = lx_hardware_stop(chip, substream);
  319. if (err < 0) {
  320. snd_printk(KERN_ERR LXP "failed to stop hardware. "
  321. "Error code %d\n", err);
  322. goto exit;
  323. }
  324. err = lx_hardware_close(chip, substream);
  325. if (err < 0) {
  326. snd_printk(KERN_ERR LXP "failed to close hardware. "
  327. "Error code %d\n", err);
  328. goto exit;
  329. }
  330. chip->hardware_running[is_capture] = 0;
  331. }
  332. err = snd_pcm_lib_free_pages(substream);
  333. if (is_capture)
  334. chip->capture_stream.stream = 0;
  335. else
  336. chip->playback_stream.stream = 0;
  337. exit:
  338. mutex_unlock(&chip->setup_mutex);
  339. return err;
  340. }
  341. static void lx_trigger_start(struct lx6464es *chip, struct lx_stream *lx_stream)
  342. {
  343. struct snd_pcm_substream *substream = lx_stream->stream;
  344. const unsigned int is_capture = lx_stream->is_capture;
  345. int err;
  346. const u32 channels = substream->runtime->channels;
  347. const u32 bytes_per_frame = channels * 3;
  348. const u32 period_size = substream->runtime->period_size;
  349. const u32 periods = substream->runtime->periods;
  350. const u32 period_bytes = period_size * bytes_per_frame;
  351. dma_addr_t buf = substream->dma_buffer.addr;
  352. int i;
  353. u32 needed, freed;
  354. u32 size_array[5];
  355. for (i = 0; i != periods; ++i) {
  356. u32 buffer_index = 0;
  357. err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed,
  358. size_array);
  359. snd_printdd(LXP "starting: needed %d, freed %d\n",
  360. needed, freed);
  361. err = lx_buffer_give(chip, 0, is_capture, period_bytes,
  362. lower_32_bits(buf), upper_32_bits(buf),
  363. &buffer_index);
  364. snd_printdd(LXP "starting: buffer index %x on %p (%d bytes)\n",
  365. buffer_index, (void *)buf, period_bytes);
  366. buf += period_bytes;
  367. }
  368. err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed, size_array);
  369. snd_printdd(LXP "starting: needed %d, freed %d\n", needed, freed);
  370. snd_printd(LXP "starting: starting stream\n");
  371. err = lx_stream_start(chip, 0, is_capture);
  372. if (err < 0)
  373. snd_printk(KERN_ERR LXP "couldn't start stream\n");
  374. else
  375. lx_stream->status = LX_STREAM_STATUS_RUNNING;
  376. lx_stream->frame_pos = 0;
  377. }
  378. static void lx_trigger_stop(struct lx6464es *chip, struct lx_stream *lx_stream)
  379. {
  380. const unsigned int is_capture = lx_stream->is_capture;
  381. int err;
  382. snd_printd(LXP "stopping: stopping stream\n");
  383. err = lx_stream_stop(chip, 0, is_capture);
  384. if (err < 0)
  385. snd_printk(KERN_ERR LXP "couldn't stop stream\n");
  386. else
  387. lx_stream->status = LX_STREAM_STATUS_FREE;
  388. }
  389. static void lx_trigger_tasklet_dispatch_stream(struct lx6464es *chip,
  390. struct lx_stream *lx_stream)
  391. {
  392. switch (lx_stream->status) {
  393. case LX_STREAM_STATUS_SCHEDULE_RUN:
  394. lx_trigger_start(chip, lx_stream);
  395. break;
  396. case LX_STREAM_STATUS_SCHEDULE_STOP:
  397. lx_trigger_stop(chip, lx_stream);
  398. break;
  399. default:
  400. break;
  401. }
  402. }
  403. static void lx_trigger_tasklet(unsigned long data)
  404. {
  405. struct lx6464es *chip = (struct lx6464es *)data;
  406. unsigned long flags;
  407. snd_printdd("->lx_trigger_tasklet\n");
  408. spin_lock_irqsave(&chip->lock, flags);
  409. lx_trigger_tasklet_dispatch_stream(chip, &chip->capture_stream);
  410. lx_trigger_tasklet_dispatch_stream(chip, &chip->playback_stream);
  411. spin_unlock_irqrestore(&chip->lock, flags);
  412. }
  413. static int lx_pcm_trigger_dispatch(struct lx6464es *chip,
  414. struct lx_stream *lx_stream, int cmd)
  415. {
  416. int err = 0;
  417. switch (cmd) {
  418. case SNDRV_PCM_TRIGGER_START:
  419. lx_stream->status = LX_STREAM_STATUS_SCHEDULE_RUN;
  420. break;
  421. case SNDRV_PCM_TRIGGER_STOP:
  422. lx_stream->status = LX_STREAM_STATUS_SCHEDULE_STOP;
  423. break;
  424. default:
  425. err = -EINVAL;
  426. goto exit;
  427. }
  428. tasklet_schedule(&chip->trigger_tasklet);
  429. exit:
  430. return err;
  431. }
  432. static int lx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  433. {
  434. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  435. const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  436. struct lx_stream *stream = is_capture ? &chip->capture_stream :
  437. &chip->playback_stream;
  438. snd_printdd("->lx_pcm_trigger\n");
  439. return lx_pcm_trigger_dispatch(chip, stream, cmd);
  440. }
  441. static int snd_lx6464es_free(struct lx6464es *chip)
  442. {
  443. snd_printdd("->snd_lx6464es_free\n");
  444. lx_irq_disable(chip);
  445. if (chip->irq >= 0)
  446. free_irq(chip->irq, chip);
  447. iounmap(chip->port_dsp_bar);
  448. ioport_unmap(chip->port_plx_remapped);
  449. pci_release_regions(chip->pci);
  450. pci_disable_device(chip->pci);
  451. kfree(chip);
  452. return 0;
  453. }
  454. static int snd_lx6464es_dev_free(struct snd_device *device)
  455. {
  456. return snd_lx6464es_free(device->device_data);
  457. }
  458. /* reset the dsp during initialization */
  459. static int __devinit lx_init_xilinx_reset(struct lx6464es *chip)
  460. {
  461. int i;
  462. u32 plx_reg = lx_plx_reg_read(chip, ePLX_CHIPSC);
  463. snd_printdd("->lx_init_xilinx_reset\n");
  464. /* activate reset of xilinx */
  465. plx_reg &= ~CHIPSC_RESET_XILINX;
  466. lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
  467. msleep(1);
  468. lx_plx_reg_write(chip, ePLX_MBOX3, 0);
  469. msleep(1);
  470. plx_reg |= CHIPSC_RESET_XILINX;
  471. lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
  472. /* deactivate reset of xilinx */
  473. for (i = 0; i != 100; ++i) {
  474. u32 reg_mbox3;
  475. msleep(10);
  476. reg_mbox3 = lx_plx_reg_read(chip, ePLX_MBOX3);
  477. if (reg_mbox3) {
  478. snd_printd(LXP "xilinx reset done\n");
  479. snd_printdd(LXP "xilinx took %d loops\n", i);
  480. break;
  481. }
  482. }
  483. /* todo: add some error handling? */
  484. /* clear mr */
  485. lx_dsp_reg_write(chip, eReg_CSM, 0);
  486. /* le xilinx ES peut ne pas etre encore pret, on attend. */
  487. msleep(600);
  488. return 0;
  489. }
  490. static int __devinit lx_init_xilinx_test(struct lx6464es *chip)
  491. {
  492. u32 reg;
  493. snd_printdd("->lx_init_xilinx_test\n");
  494. /* TEST if we have access to Xilinx/MicroBlaze */
  495. lx_dsp_reg_write(chip, eReg_CSM, 0);
  496. reg = lx_dsp_reg_read(chip, eReg_CSM);
  497. if (reg) {
  498. snd_printk(KERN_ERR LXP "Problem: Reg_CSM %x.\n", reg);
  499. /* PCI9056_SPACE0_REMAP */
  500. lx_plx_reg_write(chip, ePLX_PCICR, 1);
  501. reg = lx_dsp_reg_read(chip, eReg_CSM);
  502. if (reg) {
  503. snd_printk(KERN_ERR LXP "Error: Reg_CSM %x.\n", reg);
  504. return -EAGAIN; /* seems to be appropriate */
  505. }
  506. }
  507. snd_printd(LXP "Xilinx/MicroBlaze access test successful\n");
  508. return 0;
  509. }
  510. /* initialize ethersound */
  511. static int __devinit lx_init_ethersound_config(struct lx6464es *chip)
  512. {
  513. int i;
  514. u32 orig_conf_es = lx_dsp_reg_read(chip, eReg_CONFES);
  515. /* configure 64 io channels */
  516. u32 conf_es = (orig_conf_es & CONFES_READ_PART_MASK) |
  517. (64 << IOCR_INPUTS_OFFSET) |
  518. (64 << IOCR_OUTPUTS_OFFSET) |
  519. (FREQ_RATIO_SINGLE_MODE << FREQ_RATIO_OFFSET);
  520. snd_printdd("->lx_init_ethersound\n");
  521. chip->freq_ratio = FREQ_RATIO_SINGLE_MODE;
  522. /*
  523. * write it to the card !
  524. * this actually kicks the ES xilinx, the first time since poweron.
  525. * the MAC address in the Reg_ADMACESMSB Reg_ADMACESLSB registers
  526. * is not ready before this is done, and the bit 2 in Reg_CSES is set.
  527. * */
  528. lx_dsp_reg_write(chip, eReg_CONFES, conf_es);
  529. for (i = 0; i != 1000; ++i) {
  530. if (lx_dsp_reg_read(chip, eReg_CSES) & 4) {
  531. snd_printd(LXP "ethersound initialized after %dms\n",
  532. i);
  533. goto ethersound_initialized;
  534. }
  535. msleep(1);
  536. }
  537. snd_printk(KERN_WARNING LXP
  538. "ethersound could not be initialized after %dms\n", i);
  539. return -ETIMEDOUT;
  540. ethersound_initialized:
  541. snd_printd(LXP "ethersound initialized\n");
  542. return 0;
  543. }
  544. static int __devinit lx_init_get_version_features(struct lx6464es *chip)
  545. {
  546. u32 dsp_version;
  547. int err;
  548. snd_printdd("->lx_init_get_version_features\n");
  549. err = lx_dsp_get_version(chip, &dsp_version);
  550. if (err == 0) {
  551. u32 freq;
  552. snd_printk(LXP "DSP version: V%02d.%02d #%d\n",
  553. (dsp_version>>16) & 0xff, (dsp_version>>8) & 0xff,
  554. dsp_version & 0xff);
  555. /* later: what firmware version do we expect? */
  556. /* retrieve Play/Rec features */
  557. /* done here because we may have to handle alternate
  558. * DSP files. */
  559. /* later */
  560. /* init the EtherSound sample rate */
  561. err = lx_dsp_get_clock_frequency(chip, &freq);
  562. if (err == 0)
  563. chip->board_sample_rate = freq;
  564. snd_printd(LXP "actual clock frequency %d\n", freq);
  565. } else {
  566. snd_printk(KERN_ERR LXP "DSP corrupted \n");
  567. err = -EAGAIN;
  568. }
  569. return err;
  570. }
  571. static int lx_set_granularity(struct lx6464es *chip, u32 gran)
  572. {
  573. int err = 0;
  574. u32 snapped_gran = MICROBLAZE_IBL_MIN;
  575. snd_printdd("->lx_set_granularity\n");
  576. /* blocksize is a power of 2 */
  577. while ((snapped_gran < gran) &&
  578. (snapped_gran < MICROBLAZE_IBL_MAX)) {
  579. snapped_gran *= 2;
  580. }
  581. if (snapped_gran == chip->pcm_granularity)
  582. return 0;
  583. err = lx_dsp_set_granularity(chip, snapped_gran);
  584. if (err < 0) {
  585. snd_printk(KERN_WARNING LXP "could not set granularity\n");
  586. err = -EAGAIN;
  587. }
  588. if (snapped_gran != gran)
  589. snd_printk(LXP "snapped blocksize to %d\n", snapped_gran);
  590. snd_printd(LXP "set blocksize on board %d\n", snapped_gran);
  591. chip->pcm_granularity = snapped_gran;
  592. return err;
  593. }
  594. /* initialize and test the xilinx dsp chip */
  595. static int __devinit lx_init_dsp(struct lx6464es *chip)
  596. {
  597. int err;
  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);
  617. if (err)
  618. return err;
  619. if (chip->mac_address[0] || chip->mac_address[1] || chip->mac_address[2] ||
  620. chip->mac_address[3] || chip->mac_address[4] || chip->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. chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
  629. chip->mac_address[3], chip->mac_address[4], chip->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. KBUILD_MODNAME, 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. sprintf(card->id, "LX6464ES_%02X%02X%02X",
  873. chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
  874. sprintf(card->shortname, "LX6464ES %02X.%02X.%02X.%02X.%02X.%02X",
  875. chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
  876. chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
  877. sprintf(card->longname, "%s at 0x%lx, 0x%p, irq %i",
  878. card->shortname, chip->port_plx,
  879. chip->port_dsp_bar, chip->irq);
  880. err = snd_card_register(card);
  881. if (err < 0)
  882. goto out_free;
  883. snd_printdd(LXP "initialization successful\n");
  884. pci_set_drvdata(pci, card);
  885. dev++;
  886. return 0;
  887. out_free:
  888. snd_card_free(card);
  889. return err;
  890. }
  891. static void __devexit snd_lx6464es_remove(struct pci_dev *pci)
  892. {
  893. snd_card_free(pci_get_drvdata(pci));
  894. pci_set_drvdata(pci, NULL);
  895. }
  896. static struct pci_driver driver = {
  897. .name = KBUILD_MODNAME,
  898. .id_table = snd_lx6464es_ids,
  899. .probe = snd_lx6464es_probe,
  900. .remove = __devexit_p(snd_lx6464es_remove),
  901. };
  902. /* module initialization */
  903. static int __init mod_init(void)
  904. {
  905. return pci_register_driver(&driver);
  906. }
  907. static void __exit mod_exit(void)
  908. {
  909. pci_unregister_driver(&driver);
  910. }
  911. module_init(mod_init);
  912. module_exit(mod_exit);