lx6464es.c 27 KB

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