lx6464es.c 26 KB

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