lx6464es.c 28 KB

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