lx6464es.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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 const struct pci_device_id snd_lx6464es_ids[] = {
  49. { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
  50. PCI_VENDOR_ID_DIGIGRAM,
  51. PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_SERIAL_SUBSYSTEM),
  52. }, /* LX6464ES */
  53. { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
  54. PCI_VENDOR_ID_DIGIGRAM,
  55. PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_CAE_SERIAL_SUBSYSTEM),
  56. }, /* LX6464ES-CAE */
  57. { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
  58. PCI_VENDOR_ID_DIGIGRAM,
  59. PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ESE_SERIAL_SUBSYSTEM),
  60. }, /* LX6464ESe */
  61. { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
  62. PCI_VENDOR_ID_DIGIGRAM,
  63. PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ESE_CAE_SERIAL_SUBSYSTEM),
  64. }, /* LX6464ESe-CAE */
  65. { 0, },
  66. };
  67. MODULE_DEVICE_TABLE(pci, snd_lx6464es_ids);
  68. /* PGO pour USERo dans le registre pci_0x06/loc_0xEC */
  69. #define CHIPSC_RESET_XILINX (1L<<16)
  70. /* alsa callbacks */
  71. static const struct snd_pcm_hardware lx_caps = {
  72. .info = (SNDRV_PCM_INFO_MMAP |
  73. SNDRV_PCM_INFO_INTERLEAVED |
  74. SNDRV_PCM_INFO_MMAP_VALID |
  75. SNDRV_PCM_INFO_SYNC_START),
  76. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  77. SNDRV_PCM_FMTBIT_S16_BE |
  78. SNDRV_PCM_FMTBIT_S24_3LE |
  79. SNDRV_PCM_FMTBIT_S24_3BE),
  80. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  81. SNDRV_PCM_RATE_8000_192000),
  82. .rate_min = 8000,
  83. .rate_max = 192000,
  84. .channels_min = 2,
  85. .channels_max = 64,
  86. .buffer_bytes_max = 64*2*3*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER,
  87. .period_bytes_min = (2*2*MICROBLAZE_IBL_MIN*2),
  88. .period_bytes_max = (4*64*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER),
  89. .periods_min = 2,
  90. .periods_max = MAX_STREAM_BUFFER,
  91. };
  92. static int lx_set_granularity(struct lx6464es *chip, u32 gran);
  93. static int lx_hardware_open(struct lx6464es *chip,
  94. struct snd_pcm_substream *substream)
  95. {
  96. int err = 0;
  97. struct snd_pcm_runtime *runtime = substream->runtime;
  98. int channels = runtime->channels;
  99. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  100. snd_pcm_uframes_t period_size = runtime->period_size;
  101. dev_dbg(chip->card->dev, "allocating pipe for %d channels\n", channels);
  102. err = lx_pipe_allocate(chip, 0, is_capture, channels);
  103. if (err < 0) {
  104. dev_err(chip->card->dev, LXP "allocating pipe failed\n");
  105. return err;
  106. }
  107. err = lx_set_granularity(chip, period_size);
  108. if (err < 0) {
  109. dev_err(chip->card->dev, "setting granularity to %ld failed\n",
  110. period_size);
  111. return err;
  112. }
  113. return 0;
  114. }
  115. static int lx_hardware_start(struct lx6464es *chip,
  116. struct snd_pcm_substream *substream)
  117. {
  118. int err = 0;
  119. struct snd_pcm_runtime *runtime = substream->runtime;
  120. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  121. dev_dbg(chip->card->dev, "setting stream format\n");
  122. err = lx_stream_set_format(chip, runtime, 0, is_capture);
  123. if (err < 0) {
  124. dev_err(chip->card->dev, "setting stream format failed\n");
  125. return err;
  126. }
  127. dev_dbg(chip->card->dev, "starting pipe\n");
  128. err = lx_pipe_start(chip, 0, is_capture);
  129. if (err < 0) {
  130. dev_err(chip->card->dev, "starting pipe failed\n");
  131. return err;
  132. }
  133. dev_dbg(chip->card->dev, "waiting for pipe to start\n");
  134. err = lx_pipe_wait_for_start(chip, 0, is_capture);
  135. if (err < 0) {
  136. dev_err(chip->card->dev, "waiting for pipe failed\n");
  137. return err;
  138. }
  139. return err;
  140. }
  141. static int lx_hardware_stop(struct lx6464es *chip,
  142. struct snd_pcm_substream *substream)
  143. {
  144. int err = 0;
  145. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  146. dev_dbg(chip->card->dev, "pausing pipe\n");
  147. err = lx_pipe_pause(chip, 0, is_capture);
  148. if (err < 0) {
  149. dev_err(chip->card->dev, "pausing pipe failed\n");
  150. return err;
  151. }
  152. dev_dbg(chip->card->dev, "waiting for pipe to become idle\n");
  153. err = lx_pipe_wait_for_idle(chip, 0, is_capture);
  154. if (err < 0) {
  155. dev_err(chip->card->dev, "waiting for pipe failed\n");
  156. return err;
  157. }
  158. dev_dbg(chip->card->dev, "stopping pipe\n");
  159. err = lx_pipe_stop(chip, 0, is_capture);
  160. if (err < 0) {
  161. dev_err(chip->card->dev, "stopping pipe failed\n");
  162. return err;
  163. }
  164. return err;
  165. }
  166. static int lx_hardware_close(struct lx6464es *chip,
  167. struct snd_pcm_substream *substream)
  168. {
  169. int err = 0;
  170. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  171. dev_dbg(chip->card->dev, "releasing pipe\n");
  172. err = lx_pipe_release(chip, 0, is_capture);
  173. if (err < 0) {
  174. dev_err(chip->card->dev, "releasing pipe failed\n");
  175. return err;
  176. }
  177. return err;
  178. }
  179. static int lx_pcm_open(struct snd_pcm_substream *substream)
  180. {
  181. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  182. struct snd_pcm_runtime *runtime = substream->runtime;
  183. int err = 0;
  184. int board_rate;
  185. dev_dbg(chip->card->dev, "->lx_pcm_open\n");
  186. mutex_lock(&chip->setup_mutex);
  187. /* copy the struct snd_pcm_hardware struct */
  188. runtime->hw = lx_caps;
  189. #if 0
  190. /* buffer-size should better be multiple of period-size */
  191. err = snd_pcm_hw_constraint_integer(runtime,
  192. SNDRV_PCM_HW_PARAM_PERIODS);
  193. if (err < 0) {
  194. dev_warn(chip->card->dev, "could not constrain periods\n");
  195. goto exit;
  196. }
  197. #endif
  198. /* the clock rate cannot be changed */
  199. board_rate = chip->board_sample_rate;
  200. err = snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_RATE,
  201. board_rate);
  202. if (err < 0) {
  203. dev_warn(chip->card->dev, "could not constrain periods\n");
  204. goto exit;
  205. }
  206. /* constrain period size */
  207. err = snd_pcm_hw_constraint_minmax(runtime,
  208. SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  209. MICROBLAZE_IBL_MIN,
  210. MICROBLAZE_IBL_MAX);
  211. if (err < 0) {
  212. dev_warn(chip->card->dev,
  213. "could not constrain period size\n");
  214. goto exit;
  215. }
  216. snd_pcm_hw_constraint_step(runtime, 0,
  217. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
  218. snd_pcm_set_sync(substream);
  219. err = 0;
  220. exit:
  221. runtime->private_data = chip;
  222. mutex_unlock(&chip->setup_mutex);
  223. dev_dbg(chip->card->dev, "<-lx_pcm_open, %d\n", err);
  224. return err;
  225. }
  226. static int lx_pcm_close(struct snd_pcm_substream *substream)
  227. {
  228. int err = 0;
  229. dev_dbg(substream->pcm->card->dev, "->lx_pcm_close\n");
  230. return err;
  231. }
  232. static snd_pcm_uframes_t lx_pcm_stream_pointer(struct snd_pcm_substream
  233. *substream)
  234. {
  235. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  236. snd_pcm_uframes_t pos;
  237. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  238. struct lx_stream *lx_stream = is_capture ? &chip->capture_stream :
  239. &chip->playback_stream;
  240. dev_dbg(chip->card->dev, "->lx_pcm_stream_pointer\n");
  241. mutex_lock(&chip->lock);
  242. pos = lx_stream->frame_pos * substream->runtime->period_size;
  243. mutex_unlock(&chip->lock);
  244. dev_dbg(chip->card->dev, "stream_pointer at %ld\n", pos);
  245. return pos;
  246. }
  247. static int lx_pcm_prepare(struct snd_pcm_substream *substream)
  248. {
  249. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  250. int err = 0;
  251. const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  252. dev_dbg(chip->card->dev, "->lx_pcm_prepare\n");
  253. mutex_lock(&chip->setup_mutex);
  254. if (chip->hardware_running[is_capture]) {
  255. err = lx_hardware_stop(chip, substream);
  256. if (err < 0) {
  257. dev_err(chip->card->dev, "failed to stop hardware. "
  258. "Error code %d\n", err);
  259. goto exit;
  260. }
  261. err = lx_hardware_close(chip, substream);
  262. if (err < 0) {
  263. dev_err(chip->card->dev, "failed to close hardware. "
  264. "Error code %d\n", err);
  265. goto exit;
  266. }
  267. }
  268. dev_dbg(chip->card->dev, "opening hardware\n");
  269. err = lx_hardware_open(chip, substream);
  270. if (err < 0) {
  271. dev_err(chip->card->dev, "failed to open hardware. "
  272. "Error code %d\n", err);
  273. goto exit;
  274. }
  275. err = lx_hardware_start(chip, substream);
  276. if (err < 0) {
  277. dev_err(chip->card->dev, "failed to start hardware. "
  278. "Error code %d\n", err);
  279. goto exit;
  280. }
  281. chip->hardware_running[is_capture] = 1;
  282. if (chip->board_sample_rate != substream->runtime->rate) {
  283. if (!err)
  284. chip->board_sample_rate = substream->runtime->rate;
  285. }
  286. exit:
  287. mutex_unlock(&chip->setup_mutex);
  288. return err;
  289. }
  290. static int lx_pcm_hw_params(struct snd_pcm_substream *substream,
  291. struct snd_pcm_hw_params *hw_params, int is_capture)
  292. {
  293. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  294. int err = 0;
  295. dev_dbg(chip->card->dev, "->lx_pcm_hw_params\n");
  296. mutex_lock(&chip->setup_mutex);
  297. /* set dma buffer */
  298. err = snd_pcm_lib_malloc_pages(substream,
  299. params_buffer_bytes(hw_params));
  300. if (is_capture)
  301. chip->capture_stream.stream = substream;
  302. else
  303. chip->playback_stream.stream = substream;
  304. mutex_unlock(&chip->setup_mutex);
  305. return err;
  306. }
  307. static int lx_pcm_hw_params_playback(struct snd_pcm_substream *substream,
  308. struct snd_pcm_hw_params *hw_params)
  309. {
  310. return lx_pcm_hw_params(substream, hw_params, 0);
  311. }
  312. static int lx_pcm_hw_params_capture(struct snd_pcm_substream *substream,
  313. struct snd_pcm_hw_params *hw_params)
  314. {
  315. return lx_pcm_hw_params(substream, hw_params, 1);
  316. }
  317. static int lx_pcm_hw_free(struct snd_pcm_substream *substream)
  318. {
  319. struct lx6464es *chip = snd_pcm_substream_chip(substream);
  320. int err = 0;
  321. int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  322. dev_dbg(chip->card->dev, "->lx_pcm_hw_free\n");
  323. mutex_lock(&chip->setup_mutex);
  324. if (chip->hardware_running[is_capture]) {
  325. err = lx_hardware_stop(chip, substream);
  326. if (err < 0) {
  327. dev_err(chip->card->dev, "failed to stop hardware. "
  328. "Error code %d\n", err);
  329. goto exit;
  330. }
  331. err = lx_hardware_close(chip, substream);
  332. if (err < 0) {
  333. dev_err(chip->card->dev, "failed to close hardware. "
  334. "Error code %d\n", err);
  335. goto exit;
  336. }
  337. chip->hardware_running[is_capture] = 0;
  338. }
  339. err = snd_pcm_lib_free_pages(substream);
  340. if (is_capture)
  341. chip->capture_stream.stream = NULL;
  342. else
  343. chip->playback_stream.stream = NULL;
  344. exit:
  345. mutex_unlock(&chip->setup_mutex);
  346. return err;
  347. }
  348. static void lx_trigger_start(struct lx6464es *chip, struct lx_stream *lx_stream)
  349. {
  350. struct snd_pcm_substream *substream = lx_stream->stream;
  351. const unsigned int is_capture = lx_stream->is_capture;
  352. int err;
  353. const u32 channels = substream->runtime->channels;
  354. const u32 bytes_per_frame = channels * 3;
  355. const u32 period_size = substream->runtime->period_size;
  356. const u32 periods = substream->runtime->periods;
  357. const u32 period_bytes = period_size * bytes_per_frame;
  358. dma_addr_t buf = substream->dma_buffer.addr;
  359. int i;
  360. u32 needed, freed;
  361. u32 size_array[5];
  362. for (i = 0; i != periods; ++i) {
  363. u32 buffer_index = 0;
  364. err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed,
  365. size_array);
  366. dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n",
  367. needed, freed);
  368. err = lx_buffer_give(chip, 0, is_capture, period_bytes,
  369. lower_32_bits(buf), upper_32_bits(buf),
  370. &buffer_index);
  371. dev_dbg(chip->card->dev, "starting: buffer index %x on 0x%lx (%d bytes)\n",
  372. buffer_index, (unsigned long)buf, period_bytes);
  373. buf += period_bytes;
  374. }
  375. err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed, size_array);
  376. dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n", needed, freed);
  377. dev_dbg(chip->card->dev, "starting: starting stream\n");
  378. err = lx_stream_start(chip, 0, is_capture);
  379. if (err < 0)
  380. dev_err(chip->card->dev, "couldn't start stream\n");
  381. else
  382. lx_stream->status = LX_STREAM_STATUS_RUNNING;
  383. lx_stream->frame_pos = 0;
  384. }
  385. static void lx_trigger_stop(struct lx6464es *chip, struct lx_stream *lx_stream)
  386. {
  387. const unsigned int is_capture = lx_stream->is_capture;
  388. int err;
  389. dev_dbg(chip->card->dev, "stopping: stopping stream\n");
  390. err = lx_stream_stop(chip, 0, is_capture);
  391. if (err < 0)
  392. dev_err(chip->card->dev, "couldn't stop stream\n");
  393. else
  394. lx_stream->status = LX_STREAM_STATUS_FREE;
  395. }
  396. static void lx_trigger_dispatch_stream(struct lx6464es *chip,
  397. struct lx_stream *lx_stream)
  398. {
  399. switch (lx_stream->status) {
  400. case LX_STREAM_STATUS_SCHEDULE_RUN:
  401. lx_trigger_start(chip, lx_stream);
  402. break;
  403. case LX_STREAM_STATUS_SCHEDULE_STOP:
  404. lx_trigger_stop(chip, lx_stream);
  405. break;
  406. default:
  407. break;
  408. }
  409. }
  410. static int lx_pcm_trigger_dispatch(struct lx6464es *chip,
  411. struct lx_stream *lx_stream, int cmd)
  412. {
  413. int err = 0;
  414. mutex_lock(&chip->lock);
  415. switch (cmd) {
  416. case SNDRV_PCM_TRIGGER_START:
  417. lx_stream->status = LX_STREAM_STATUS_SCHEDULE_RUN;
  418. break;
  419. case SNDRV_PCM_TRIGGER_STOP:
  420. lx_stream->status = LX_STREAM_STATUS_SCHEDULE_STOP;
  421. break;
  422. default:
  423. err = -EINVAL;
  424. goto exit;
  425. }
  426. lx_trigger_dispatch_stream(chip, &chip->capture_stream);
  427. lx_trigger_dispatch_stream(chip, &chip->playback_stream);
  428. exit:
  429. mutex_unlock(&chip->lock);
  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. dev_dbg(chip->card->dev, "->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. dev_dbg(chip->card->dev, "->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 lx_init_xilinx_reset(struct lx6464es *chip)
  460. {
  461. int i;
  462. u32 plx_reg = lx_plx_reg_read(chip, ePLX_CHIPSC);
  463. dev_dbg(chip->card->dev, "->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. dev_dbg(chip->card->dev, "xilinx reset done\n");
  479. dev_dbg(chip->card->dev, "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 lx_init_xilinx_test(struct lx6464es *chip)
  491. {
  492. u32 reg;
  493. dev_dbg(chip->card->dev, "->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. dev_err(chip->card->dev, "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. dev_err(chip->card->dev, "Error: Reg_CSM %x.\n", reg);
  504. return -EAGAIN; /* seems to be appropriate */
  505. }
  506. }
  507. dev_dbg(chip->card->dev, "Xilinx/MicroBlaze access test successful\n");
  508. return 0;
  509. }
  510. /* initialize ethersound */
  511. static int 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. dev_dbg(chip->card->dev, "->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. dev_dbg(chip->card->dev, "ethersound initialized after %dms\n",
  532. i);
  533. goto ethersound_initialized;
  534. }
  535. msleep(1);
  536. }
  537. dev_warn(chip->card->dev,
  538. "ethersound could not be initialized after %dms\n", i);
  539. return -ETIMEDOUT;
  540. ethersound_initialized:
  541. dev_dbg(chip->card->dev, "ethersound initialized\n");
  542. return 0;
  543. }
  544. static int lx_init_get_version_features(struct lx6464es *chip)
  545. {
  546. u32 dsp_version;
  547. int err;
  548. dev_dbg(chip->card->dev, "->lx_init_get_version_features\n");
  549. err = lx_dsp_get_version(chip, &dsp_version);
  550. if (err == 0) {
  551. u32 freq;
  552. dev_info(chip->card->dev, "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. dev_dbg(chip->card->dev, "actual clock frequency %d\n", freq);
  565. } else {
  566. dev_err(chip->card->dev, "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. dev_dbg(chip->card->dev, "->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. dev_warn(chip->card->dev, "could not set granularity\n");
  586. err = -EAGAIN;
  587. }
  588. if (snapped_gran != gran)
  589. dev_err(chip->card->dev, "snapped blocksize to %d\n", snapped_gran);
  590. dev_dbg(chip->card->dev, "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 lx_init_dsp(struct lx6464es *chip)
  596. {
  597. int err;
  598. int i;
  599. dev_dbg(chip->card->dev, "->lx_init_dsp\n");
  600. dev_dbg(chip->card->dev, "initialize board\n");
  601. err = lx_init_xilinx_reset(chip);
  602. if (err)
  603. return err;
  604. dev_dbg(chip->card->dev, "testing board\n");
  605. err = lx_init_xilinx_test(chip);
  606. if (err)
  607. return err;
  608. dev_dbg(chip->card->dev, "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. dev_dbg(chip->card->dev, "mac address ready read after: %dms\n", i);
  627. dev_info(chip->card->dev,
  628. "mac address: %02X.%02X.%02X.%02X.%02X.%02X\n",
  629. chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
  630. chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
  631. err = lx_init_get_version_features(chip);
  632. if (err)
  633. return err;
  634. lx_set_granularity(chip, MICROBLAZE_IBL_DEFAULT);
  635. chip->playback_mute = 0;
  636. return err;
  637. }
  638. static const struct snd_pcm_ops lx_ops_playback = {
  639. .open = lx_pcm_open,
  640. .close = lx_pcm_close,
  641. .ioctl = snd_pcm_lib_ioctl,
  642. .prepare = lx_pcm_prepare,
  643. .hw_params = lx_pcm_hw_params_playback,
  644. .hw_free = lx_pcm_hw_free,
  645. .trigger = lx_pcm_trigger,
  646. .pointer = lx_pcm_stream_pointer,
  647. };
  648. static const struct snd_pcm_ops lx_ops_capture = {
  649. .open = lx_pcm_open,
  650. .close = lx_pcm_close,
  651. .ioctl = snd_pcm_lib_ioctl,
  652. .prepare = lx_pcm_prepare,
  653. .hw_params = lx_pcm_hw_params_capture,
  654. .hw_free = lx_pcm_hw_free,
  655. .trigger = lx_pcm_trigger,
  656. .pointer = lx_pcm_stream_pointer,
  657. };
  658. static int lx_pcm_create(struct lx6464es *chip)
  659. {
  660. int err;
  661. struct snd_pcm *pcm;
  662. u32 size = 64 * /* channels */
  663. 3 * /* 24 bit samples */
  664. MAX_STREAM_BUFFER * /* periods */
  665. MICROBLAZE_IBL_MAX * /* frames per period */
  666. 2; /* duplex */
  667. size = PAGE_ALIGN(size);
  668. /* hardcoded device name & channel count */
  669. err = snd_pcm_new(chip->card, (char *)card_name, 0,
  670. 1, 1, &pcm);
  671. if (err < 0)
  672. return err;
  673. pcm->private_data = chip;
  674. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &lx_ops_playback);
  675. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &lx_ops_capture);
  676. pcm->info_flags = 0;
  677. pcm->nonatomic = true;
  678. strcpy(pcm->name, card_name);
  679. err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  680. snd_dma_pci_data(chip->pci),
  681. size, size);
  682. if (err < 0)
  683. return err;
  684. chip->pcm = pcm;
  685. chip->capture_stream.is_capture = 1;
  686. return 0;
  687. }
  688. static int lx_control_playback_info(struct snd_kcontrol *kcontrol,
  689. struct snd_ctl_elem_info *uinfo)
  690. {
  691. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  692. uinfo->count = 1;
  693. uinfo->value.integer.min = 0;
  694. uinfo->value.integer.max = 1;
  695. return 0;
  696. }
  697. static int lx_control_playback_get(struct snd_kcontrol *kcontrol,
  698. struct snd_ctl_elem_value *ucontrol)
  699. {
  700. struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
  701. ucontrol->value.integer.value[0] = chip->playback_mute;
  702. return 0;
  703. }
  704. static int lx_control_playback_put(struct snd_kcontrol *kcontrol,
  705. struct snd_ctl_elem_value *ucontrol)
  706. {
  707. struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
  708. int changed = 0;
  709. int current_value = chip->playback_mute;
  710. if (current_value != ucontrol->value.integer.value[0]) {
  711. lx_level_unmute(chip, 0, !current_value);
  712. chip->playback_mute = !current_value;
  713. changed = 1;
  714. }
  715. return changed;
  716. }
  717. static const struct snd_kcontrol_new lx_control_playback_switch = {
  718. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  719. .name = "PCM Playback Switch",
  720. .index = 0,
  721. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  722. .private_value = 0,
  723. .info = lx_control_playback_info,
  724. .get = lx_control_playback_get,
  725. .put = lx_control_playback_put
  726. };
  727. static void lx_proc_levels_read(struct snd_info_entry *entry,
  728. struct snd_info_buffer *buffer)
  729. {
  730. u32 levels[64];
  731. int err;
  732. int i, j;
  733. struct lx6464es *chip = entry->private_data;
  734. snd_iprintf(buffer, "capture levels:\n");
  735. err = lx_level_peaks(chip, 1, 64, levels);
  736. if (err < 0)
  737. return;
  738. for (i = 0; i != 8; ++i) {
  739. for (j = 0; j != 8; ++j)
  740. snd_iprintf(buffer, "%08x ", levels[i*8+j]);
  741. snd_iprintf(buffer, "\n");
  742. }
  743. snd_iprintf(buffer, "\nplayback levels:\n");
  744. err = lx_level_peaks(chip, 0, 64, levels);
  745. if (err < 0)
  746. return;
  747. for (i = 0; i != 8; ++i) {
  748. for (j = 0; j != 8; ++j)
  749. snd_iprintf(buffer, "%08x ", levels[i*8+j]);
  750. snd_iprintf(buffer, "\n");
  751. }
  752. snd_iprintf(buffer, "\n");
  753. }
  754. static int lx_proc_create(struct snd_card *card, struct lx6464es *chip)
  755. {
  756. struct snd_info_entry *entry;
  757. int err = snd_card_proc_new(card, "levels", &entry);
  758. if (err < 0)
  759. return err;
  760. snd_info_set_text_ops(entry, chip, lx_proc_levels_read);
  761. return 0;
  762. }
  763. static int snd_lx6464es_create(struct snd_card *card,
  764. struct pci_dev *pci,
  765. struct lx6464es **rchip)
  766. {
  767. struct lx6464es *chip;
  768. int err;
  769. static struct snd_device_ops ops = {
  770. .dev_free = snd_lx6464es_dev_free,
  771. };
  772. dev_dbg(card->dev, "->snd_lx6464es_create\n");
  773. *rchip = NULL;
  774. /* enable PCI device */
  775. err = pci_enable_device(pci);
  776. if (err < 0)
  777. return err;
  778. pci_set_master(pci);
  779. /* check if we can restrict PCI DMA transfers to 32 bits */
  780. err = dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
  781. if (err < 0) {
  782. dev_err(card->dev,
  783. "architecture does not support 32bit PCI busmaster DMA\n");
  784. pci_disable_device(pci);
  785. return -ENXIO;
  786. }
  787. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  788. if (chip == NULL) {
  789. err = -ENOMEM;
  790. goto alloc_failed;
  791. }
  792. chip->card = card;
  793. chip->pci = pci;
  794. chip->irq = -1;
  795. /* initialize synchronization structs */
  796. mutex_init(&chip->lock);
  797. mutex_init(&chip->msg_lock);
  798. mutex_init(&chip->setup_mutex);
  799. /* request resources */
  800. err = pci_request_regions(pci, card_name);
  801. if (err < 0)
  802. goto request_regions_failed;
  803. /* plx port */
  804. chip->port_plx = pci_resource_start(pci, 1);
  805. chip->port_plx_remapped = ioport_map(chip->port_plx,
  806. pci_resource_len(pci, 1));
  807. /* dsp port */
  808. chip->port_dsp_bar = pci_ioremap_bar(pci, 2);
  809. if (!chip->port_dsp_bar) {
  810. dev_err(card->dev, "cannot remap PCI memory region\n");
  811. err = -ENOMEM;
  812. goto remap_pci_failed;
  813. }
  814. err = request_threaded_irq(pci->irq, lx_interrupt, lx_threaded_irq,
  815. IRQF_SHARED, KBUILD_MODNAME, chip);
  816. if (err) {
  817. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  818. goto request_irq_failed;
  819. }
  820. chip->irq = pci->irq;
  821. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  822. if (err < 0)
  823. goto device_new_failed;
  824. err = lx_init_dsp(chip);
  825. if (err < 0) {
  826. dev_err(card->dev, "error during DSP initialization\n");
  827. return err;
  828. }
  829. err = lx_pcm_create(chip);
  830. if (err < 0)
  831. return err;
  832. err = lx_proc_create(card, chip);
  833. if (err < 0)
  834. return err;
  835. err = snd_ctl_add(card, snd_ctl_new1(&lx_control_playback_switch,
  836. chip));
  837. if (err < 0)
  838. return err;
  839. *rchip = chip;
  840. return 0;
  841. device_new_failed:
  842. free_irq(pci->irq, chip);
  843. request_irq_failed:
  844. iounmap(chip->port_dsp_bar);
  845. remap_pci_failed:
  846. pci_release_regions(pci);
  847. request_regions_failed:
  848. kfree(chip);
  849. alloc_failed:
  850. pci_disable_device(pci);
  851. return err;
  852. }
  853. static int snd_lx6464es_probe(struct pci_dev *pci,
  854. const struct pci_device_id *pci_id)
  855. {
  856. static int dev;
  857. struct snd_card *card;
  858. struct lx6464es *chip;
  859. int err;
  860. dev_dbg(&pci->dev, "->snd_lx6464es_probe\n");
  861. if (dev >= SNDRV_CARDS)
  862. return -ENODEV;
  863. if (!enable[dev]) {
  864. dev++;
  865. return -ENOENT;
  866. }
  867. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  868. 0, &card);
  869. if (err < 0)
  870. return err;
  871. err = snd_lx6464es_create(card, pci, &chip);
  872. if (err < 0) {
  873. dev_err(card->dev, "error during snd_lx6464es_create\n");
  874. goto out_free;
  875. }
  876. strcpy(card->driver, "LX6464ES");
  877. sprintf(card->id, "LX6464ES_%02X%02X%02X",
  878. chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
  879. sprintf(card->shortname, "LX6464ES %02X.%02X.%02X.%02X.%02X.%02X",
  880. chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
  881. chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
  882. sprintf(card->longname, "%s at 0x%lx, 0x%p, irq %i",
  883. card->shortname, chip->port_plx,
  884. chip->port_dsp_bar, chip->irq);
  885. err = snd_card_register(card);
  886. if (err < 0)
  887. goto out_free;
  888. dev_dbg(chip->card->dev, "initialization successful\n");
  889. pci_set_drvdata(pci, card);
  890. dev++;
  891. return 0;
  892. out_free:
  893. snd_card_free(card);
  894. return err;
  895. }
  896. static void snd_lx6464es_remove(struct pci_dev *pci)
  897. {
  898. snd_card_free(pci_get_drvdata(pci));
  899. }
  900. static struct pci_driver lx6464es_driver = {
  901. .name = KBUILD_MODNAME,
  902. .id_table = snd_lx6464es_ids,
  903. .probe = snd_lx6464es_probe,
  904. .remove = snd_lx6464es_remove,
  905. };
  906. module_pci_driver(lx6464es_driver);