aica.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * This code is licenced under
  3. * the General Public Licence
  4. * version 2
  5. *
  6. * Copyright Adrian McMenamin 2005, 2006, 2007
  7. * <adrian@mcmen.demon.co.uk>
  8. * Requires firmware (BSD licenced) available from:
  9. * http://linuxdc.cvs.sourceforge.net/linuxdc/linux-sh-dc/sound/oss/aica/firmware/
  10. * or the maintainer
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as published by
  14. * the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. #include <linux/init.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/slab.h>
  29. #include <linux/time.h>
  30. #include <linux/wait.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/firmware.h>
  34. #include <linux/timer.h>
  35. #include <linux/delay.h>
  36. #include <linux/workqueue.h>
  37. #include <sound/driver.h>
  38. #include <sound/core.h>
  39. #include <sound/control.h>
  40. #include <sound/pcm.h>
  41. #include <sound/initval.h>
  42. #include <sound/info.h>
  43. #include <asm/io.h>
  44. #include <asm/dma.h>
  45. #include <asm/dreamcast/sysasic.h>
  46. #include "aica.h"
  47. MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
  48. MODULE_DESCRIPTION("Dreamcast AICA sound (pcm) driver");
  49. MODULE_LICENSE("GPL");
  50. MODULE_SUPPORTED_DEVICE("{{Yamaha/SEGA, AICA}}");
  51. /* module parameters */
  52. #define CARD_NAME "AICA"
  53. static int index = -1;
  54. static char *id;
  55. static int enable = 1;
  56. module_param(index, int, 0444);
  57. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  58. module_param(id, charp, 0444);
  59. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  60. module_param(enable, bool, 0644);
  61. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  62. /* Use workqueue */
  63. static struct workqueue_struct *aica_queue;
  64. /* Simple platform device */
  65. static struct platform_device *pd;
  66. static struct resource aica_memory_space[2] = {
  67. {
  68. .name = "AICA ARM CONTROL",
  69. .start = ARM_RESET_REGISTER,
  70. .flags = IORESOURCE_MEM,
  71. .end = ARM_RESET_REGISTER + 3,
  72. },
  73. {
  74. .name = "AICA Sound RAM",
  75. .start = SPU_MEMORY_BASE,
  76. .flags = IORESOURCE_MEM,
  77. .end = SPU_MEMORY_BASE + 0x200000 - 1,
  78. },
  79. };
  80. /* SPU specific functions */
  81. /* spu_write_wait - wait for G2-SH FIFO to clear */
  82. static void spu_write_wait(void)
  83. {
  84. int time_count;
  85. time_count = 0;
  86. while (1) {
  87. if (!(readl(G2_FIFO) & 0x11))
  88. break;
  89. /* To ensure hardware failure doesn't wedge kernel */
  90. time_count++;
  91. if (time_count > 0x10000) {
  92. snd_printk
  93. ("WARNING: G2 FIFO appears to be blocked.\n");
  94. break;
  95. }
  96. }
  97. }
  98. /* spu_memset - write to memory in SPU address space */
  99. static void spu_memset(u32 toi, u32 what, int length)
  100. {
  101. int i;
  102. snd_assert(length % 4 == 0, return);
  103. for (i = 0; i < length; i++) {
  104. if (!(i % 8))
  105. spu_write_wait();
  106. writel(what, toi + SPU_MEMORY_BASE);
  107. toi++;
  108. }
  109. }
  110. /* spu_memload - write to SPU address space */
  111. static void spu_memload(u32 toi, void *from, int length)
  112. {
  113. u32 *froml = from;
  114. u32 __iomem *to = (u32 __iomem *) (SPU_MEMORY_BASE + toi);
  115. int i;
  116. u32 val;
  117. length = DIV_ROUND_UP(length, 4);
  118. spu_write_wait();
  119. for (i = 0; i < length; i++) {
  120. if (!(i % 8))
  121. spu_write_wait();
  122. val = *froml;
  123. writel(val, to);
  124. froml++;
  125. to++;
  126. }
  127. }
  128. /* spu_disable - set spu registers to stop sound output */
  129. static void spu_disable(void)
  130. {
  131. int i;
  132. u32 regval;
  133. spu_write_wait();
  134. regval = readl(ARM_RESET_REGISTER);
  135. regval |= 1;
  136. spu_write_wait();
  137. writel(regval, ARM_RESET_REGISTER);
  138. for (i = 0; i < 64; i++) {
  139. spu_write_wait();
  140. regval = readl(SPU_REGISTER_BASE + (i * 0x80));
  141. regval = (regval & ~0x4000) | 0x8000;
  142. spu_write_wait();
  143. writel(regval, SPU_REGISTER_BASE + (i * 0x80));
  144. }
  145. }
  146. /* spu_enable - set spu registers to enable sound output */
  147. static void spu_enable(void)
  148. {
  149. u32 regval = readl(ARM_RESET_REGISTER);
  150. regval &= ~1;
  151. spu_write_wait();
  152. writel(regval, ARM_RESET_REGISTER);
  153. }
  154. /*
  155. * Halt the sound processor, clear the memory,
  156. * load some default ARM7 code, and then restart ARM7
  157. */
  158. static void spu_reset(void)
  159. {
  160. spu_disable();
  161. spu_memset(0, 0, 0x200000 / 4);
  162. /* Put ARM7 in endless loop */
  163. ctrl_outl(0xea000002, SPU_MEMORY_BASE);
  164. spu_enable();
  165. }
  166. /* aica_chn_start - write to spu to start playback */
  167. static void aica_chn_start(void)
  168. {
  169. spu_write_wait();
  170. writel(AICA_CMD_KICK | AICA_CMD_START, (u32 *) AICA_CONTROL_POINT);
  171. }
  172. /* aica_chn_halt - write to spu to halt playback */
  173. static void aica_chn_halt(void)
  174. {
  175. spu_write_wait();
  176. writel(AICA_CMD_KICK | AICA_CMD_STOP, (u32 *) AICA_CONTROL_POINT);
  177. }
  178. /* ALSA code below */
  179. static struct snd_pcm_hardware snd_pcm_aica_playback_hw = {
  180. .info = (SNDRV_PCM_INFO_NONINTERLEAVED),
  181. .formats =
  182. (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |
  183. SNDRV_PCM_FMTBIT_IMA_ADPCM),
  184. .rates = SNDRV_PCM_RATE_8000_48000,
  185. .rate_min = 8000,
  186. .rate_max = 48000,
  187. .channels_min = 1,
  188. .channels_max = 2,
  189. .buffer_bytes_max = AICA_BUFFER_SIZE,
  190. .period_bytes_min = AICA_PERIOD_SIZE,
  191. .period_bytes_max = AICA_PERIOD_SIZE,
  192. .periods_min = AICA_PERIOD_NUMBER,
  193. .periods_max = AICA_PERIOD_NUMBER,
  194. };
  195. static int aica_dma_transfer(int channels, int buffer_size,
  196. struct snd_pcm_substream *substream)
  197. {
  198. int q, err, period_offset;
  199. struct snd_card_aica *dreamcastcard;
  200. struct snd_pcm_runtime *runtime;
  201. err = 0;
  202. dreamcastcard = substream->pcm->private_data;
  203. period_offset = dreamcastcard->clicks;
  204. period_offset %= (AICA_PERIOD_NUMBER / channels);
  205. runtime = substream->runtime;
  206. for (q = 0; q < channels; q++) {
  207. err = dma_xfer(AICA_DMA_CHANNEL,
  208. (unsigned long) (runtime->dma_area +
  209. (AICA_BUFFER_SIZE * q) /
  210. channels +
  211. AICA_PERIOD_SIZE *
  212. period_offset),
  213. AICA_CHANNEL0_OFFSET + q * CHANNEL_OFFSET +
  214. AICA_PERIOD_SIZE * period_offset,
  215. buffer_size / channels, AICA_DMA_MODE);
  216. if (unlikely(err < 0))
  217. break;
  218. dma_wait_for_completion(AICA_DMA_CHANNEL);
  219. }
  220. return err;
  221. }
  222. static void startup_aica(struct snd_card_aica *dreamcastcard)
  223. {
  224. spu_memload(AICA_CHANNEL0_CONTROL_OFFSET,
  225. dreamcastcard->channel, sizeof(struct aica_channel));
  226. aica_chn_start();
  227. }
  228. static void run_spu_dma(struct work_struct *work)
  229. {
  230. int buffer_size;
  231. struct snd_pcm_runtime *runtime;
  232. struct snd_card_aica *dreamcastcard;
  233. dreamcastcard =
  234. container_of(work, struct snd_card_aica, spu_dma_work);
  235. runtime = dreamcastcard->substream->runtime;
  236. if (unlikely(dreamcastcard->dma_check == 0)) {
  237. buffer_size =
  238. frames_to_bytes(runtime, runtime->buffer_size);
  239. if (runtime->channels > 1)
  240. dreamcastcard->channel->flags |= 0x01;
  241. aica_dma_transfer(runtime->channels, buffer_size,
  242. dreamcastcard->substream);
  243. startup_aica(dreamcastcard);
  244. dreamcastcard->clicks =
  245. buffer_size / (AICA_PERIOD_SIZE * runtime->channels);
  246. return;
  247. } else {
  248. aica_dma_transfer(runtime->channels,
  249. AICA_PERIOD_SIZE * runtime->channels,
  250. dreamcastcard->substream);
  251. snd_pcm_period_elapsed(dreamcastcard->substream);
  252. dreamcastcard->clicks++;
  253. if (unlikely(dreamcastcard->clicks >= AICA_PERIOD_NUMBER))
  254. dreamcastcard->clicks %= AICA_PERIOD_NUMBER;
  255. mod_timer(&dreamcastcard->timer, jiffies + 1);
  256. }
  257. }
  258. static void aica_period_elapsed(unsigned long timer_var)
  259. {
  260. /*timer function - so cannot sleep */
  261. int play_period;
  262. struct snd_pcm_runtime *runtime;
  263. struct snd_pcm_substream *substream;
  264. struct snd_card_aica *dreamcastcard;
  265. substream = (struct snd_pcm_substream *) timer_var;
  266. runtime = substream->runtime;
  267. dreamcastcard = substream->pcm->private_data;
  268. /* Have we played out an additional period? */
  269. play_period =
  270. frames_to_bytes(runtime,
  271. readl
  272. (AICA_CONTROL_CHANNEL_SAMPLE_NUMBER)) /
  273. AICA_PERIOD_SIZE;
  274. if (play_period == dreamcastcard->current_period) {
  275. /* reschedule the timer */
  276. mod_timer(&(dreamcastcard->timer), jiffies + 1);
  277. return;
  278. }
  279. if (runtime->channels > 1)
  280. dreamcastcard->current_period = play_period;
  281. if (unlikely(dreamcastcard->dma_check == 0))
  282. dreamcastcard->dma_check = 1;
  283. queue_work(aica_queue, &(dreamcastcard->spu_dma_work));
  284. }
  285. static void spu_begin_dma(struct snd_pcm_substream *substream)
  286. {
  287. struct snd_card_aica *dreamcastcard;
  288. struct snd_pcm_runtime *runtime;
  289. runtime = substream->runtime;
  290. dreamcastcard = substream->pcm->private_data;
  291. /*get the queue to do the work */
  292. queue_work(aica_queue, &(dreamcastcard->spu_dma_work));
  293. /* Timer may already be running */
  294. if (unlikely(dreamcastcard->timer.data)) {
  295. mod_timer(&dreamcastcard->timer, jiffies + 4);
  296. return;
  297. }
  298. init_timer(&(dreamcastcard->timer));
  299. dreamcastcard->timer.data = (unsigned long) substream;
  300. dreamcastcard->timer.function = aica_period_elapsed;
  301. dreamcastcard->timer.expires = jiffies + 4;
  302. add_timer(&(dreamcastcard->timer));
  303. }
  304. static int snd_aicapcm_pcm_open(struct snd_pcm_substream
  305. *substream)
  306. {
  307. struct snd_pcm_runtime *runtime;
  308. struct aica_channel *channel;
  309. struct snd_card_aica *dreamcastcard;
  310. if (!enable)
  311. return -ENOENT;
  312. dreamcastcard = substream->pcm->private_data;
  313. channel = kmalloc(sizeof(struct aica_channel), GFP_KERNEL);
  314. if (!channel)
  315. return -ENOMEM;
  316. /* set defaults for channel */
  317. channel->sfmt = SM_8BIT;
  318. channel->cmd = AICA_CMD_START;
  319. channel->vol = dreamcastcard->master_volume;
  320. channel->pan = 0x80;
  321. channel->pos = 0;
  322. channel->flags = 0; /* default to mono */
  323. dreamcastcard->channel = channel;
  324. runtime = substream->runtime;
  325. runtime->hw = snd_pcm_aica_playback_hw;
  326. spu_enable();
  327. dreamcastcard->clicks = 0;
  328. dreamcastcard->current_period = 0;
  329. dreamcastcard->dma_check = 0;
  330. return 0;
  331. }
  332. static int snd_aicapcm_pcm_close(struct snd_pcm_substream
  333. *substream)
  334. {
  335. struct snd_card_aica *dreamcastcard = substream->pcm->private_data;
  336. flush_workqueue(aica_queue);
  337. if (dreamcastcard->timer.data)
  338. del_timer(&dreamcastcard->timer);
  339. kfree(dreamcastcard->channel);
  340. spu_disable();
  341. return 0;
  342. }
  343. static int snd_aicapcm_pcm_hw_free(struct snd_pcm_substream
  344. *substream)
  345. {
  346. /* Free the DMA buffer */
  347. return snd_pcm_lib_free_pages(substream);
  348. }
  349. static int snd_aicapcm_pcm_hw_params(struct snd_pcm_substream
  350. *substream, struct snd_pcm_hw_params
  351. *hw_params)
  352. {
  353. /* Allocate a DMA buffer using ALSA built-ins */
  354. return
  355. snd_pcm_lib_malloc_pages(substream,
  356. params_buffer_bytes(hw_params));
  357. }
  358. static int snd_aicapcm_pcm_prepare(struct snd_pcm_substream
  359. *substream)
  360. {
  361. struct snd_card_aica *dreamcastcard = substream->pcm->private_data;
  362. if ((substream->runtime)->format == SNDRV_PCM_FORMAT_S16_LE)
  363. dreamcastcard->channel->sfmt = SM_16BIT;
  364. dreamcastcard->channel->freq = substream->runtime->rate;
  365. dreamcastcard->substream = substream;
  366. return 0;
  367. }
  368. static int snd_aicapcm_pcm_trigger(struct snd_pcm_substream
  369. *substream, int cmd)
  370. {
  371. switch (cmd) {
  372. case SNDRV_PCM_TRIGGER_START:
  373. spu_begin_dma(substream);
  374. break;
  375. case SNDRV_PCM_TRIGGER_STOP:
  376. aica_chn_halt();
  377. break;
  378. default:
  379. return -EINVAL;
  380. }
  381. return 0;
  382. }
  383. static unsigned long snd_aicapcm_pcm_pointer(struct snd_pcm_substream
  384. *substream)
  385. {
  386. return readl(AICA_CONTROL_CHANNEL_SAMPLE_NUMBER);
  387. }
  388. static struct snd_pcm_ops snd_aicapcm_playback_ops = {
  389. .open = snd_aicapcm_pcm_open,
  390. .close = snd_aicapcm_pcm_close,
  391. .ioctl = snd_pcm_lib_ioctl,
  392. .hw_params = snd_aicapcm_pcm_hw_params,
  393. .hw_free = snd_aicapcm_pcm_hw_free,
  394. .prepare = snd_aicapcm_pcm_prepare,
  395. .trigger = snd_aicapcm_pcm_trigger,
  396. .pointer = snd_aicapcm_pcm_pointer,
  397. };
  398. /* TO DO: set up to handle more than one pcm instance */
  399. static int __init snd_aicapcmchip(struct snd_card_aica
  400. *dreamcastcard, int pcm_index)
  401. {
  402. struct snd_pcm *pcm;
  403. int err;
  404. /* AICA has no capture ability */
  405. err =
  406. snd_pcm_new(dreamcastcard->card, "AICA PCM", pcm_index, 1, 0,
  407. &pcm);
  408. if (unlikely(err < 0))
  409. return err;
  410. pcm->private_data = dreamcastcard;
  411. strcpy(pcm->name, "AICA PCM");
  412. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  413. &snd_aicapcm_playback_ops);
  414. /* Allocate the DMA buffers */
  415. err =
  416. snd_pcm_lib_preallocate_pages_for_all(pcm,
  417. SNDRV_DMA_TYPE_CONTINUOUS,
  418. snd_dma_continuous_data
  419. (GFP_KERNEL),
  420. AICA_BUFFER_SIZE,
  421. AICA_BUFFER_SIZE);
  422. return err;
  423. }
  424. /* Mixer controls */
  425. static int aica_pcmswitch_info(struct snd_kcontrol *kcontrol,
  426. struct snd_ctl_elem_info *uinfo)
  427. {
  428. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  429. uinfo->count = 1;
  430. uinfo->value.integer.min = 0;
  431. uinfo->value.integer.max = 1;
  432. return 0;
  433. }
  434. static int aica_pcmswitch_get(struct snd_kcontrol *kcontrol,
  435. struct snd_ctl_elem_value *ucontrol)
  436. {
  437. ucontrol->value.integer.value[0] = 1; /* TO DO: Fix me */
  438. return 0;
  439. }
  440. static int aica_pcmswitch_put(struct snd_kcontrol *kcontrol,
  441. struct snd_ctl_elem_value *ucontrol)
  442. {
  443. if (ucontrol->value.integer.value[0] == 1)
  444. return 0; /* TO DO: Fix me */
  445. else
  446. aica_chn_halt();
  447. return 0;
  448. }
  449. static int aica_pcmvolume_info(struct snd_kcontrol *kcontrol,
  450. struct snd_ctl_elem_info *uinfo)
  451. {
  452. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  453. uinfo->count = 1;
  454. uinfo->value.integer.min = 0;
  455. uinfo->value.integer.max = 0xFF;
  456. return 0;
  457. }
  458. static int aica_pcmvolume_get(struct snd_kcontrol *kcontrol,
  459. struct snd_ctl_elem_value *ucontrol)
  460. {
  461. struct snd_card_aica *dreamcastcard;
  462. dreamcastcard = kcontrol->private_data;
  463. if (unlikely(!dreamcastcard->channel))
  464. return -ETXTBSY; /* we've not yet been set up */
  465. ucontrol->value.integer.value[0] = dreamcastcard->channel->vol;
  466. return 0;
  467. }
  468. static int aica_pcmvolume_put(struct snd_kcontrol *kcontrol,
  469. struct snd_ctl_elem_value *ucontrol)
  470. {
  471. struct snd_card_aica *dreamcastcard;
  472. dreamcastcard = kcontrol->private_data;
  473. if (unlikely(!dreamcastcard->channel))
  474. return -ETXTBSY;
  475. if (unlikely(dreamcastcard->channel->vol ==
  476. ucontrol->value.integer.value[0]))
  477. return 0;
  478. dreamcastcard->channel->vol = ucontrol->value.integer.value[0];
  479. dreamcastcard->master_volume = ucontrol->value.integer.value[0];
  480. spu_memload(AICA_CHANNEL0_CONTROL_OFFSET,
  481. dreamcastcard->channel, sizeof(struct aica_channel));
  482. return 1;
  483. }
  484. static struct snd_kcontrol_new snd_aica_pcmswitch_control __devinitdata = {
  485. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  486. .name = "PCM Playback Switch",
  487. .index = 0,
  488. .info = aica_pcmswitch_info,
  489. .get = aica_pcmswitch_get,
  490. .put = aica_pcmswitch_put
  491. };
  492. static struct snd_kcontrol_new snd_aica_pcmvolume_control __devinitdata = {
  493. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  494. .name = "PCM Playback Volume",
  495. .index = 0,
  496. .info = aica_pcmvolume_info,
  497. .get = aica_pcmvolume_get,
  498. .put = aica_pcmvolume_put
  499. };
  500. static int load_aica_firmware(void)
  501. {
  502. int err;
  503. const struct firmware *fw_entry;
  504. spu_reset();
  505. err = request_firmware(&fw_entry, "aica_firmware.bin", &pd->dev);
  506. if (unlikely(err))
  507. return err;
  508. /* write firware into memory */
  509. spu_disable();
  510. spu_memload(0, fw_entry->data, fw_entry->size);
  511. spu_enable();
  512. release_firmware(fw_entry);
  513. return err;
  514. }
  515. static int __devinit add_aicamixer_controls(struct snd_card_aica
  516. *dreamcastcard)
  517. {
  518. int err;
  519. err = snd_ctl_add
  520. (dreamcastcard->card,
  521. snd_ctl_new1(&snd_aica_pcmvolume_control, dreamcastcard));
  522. if (unlikely(err < 0))
  523. return err;
  524. err = snd_ctl_add
  525. (dreamcastcard->card,
  526. snd_ctl_new1(&snd_aica_pcmswitch_control, dreamcastcard));
  527. if (unlikely(err < 0))
  528. return err;
  529. return 0;
  530. }
  531. static int snd_aica_remove(struct platform_device *devptr)
  532. {
  533. struct snd_card_aica *dreamcastcard;
  534. dreamcastcard = platform_get_drvdata(devptr);
  535. if (unlikely(!dreamcastcard))
  536. return -ENODEV;
  537. snd_card_free(dreamcastcard->card);
  538. kfree(dreamcastcard);
  539. platform_set_drvdata(devptr, NULL);
  540. return 0;
  541. }
  542. static int __init snd_aica_probe(struct platform_device *devptr)
  543. {
  544. int err;
  545. struct snd_card_aica *dreamcastcard;
  546. dreamcastcard = kmalloc(sizeof(struct snd_card_aica), GFP_KERNEL);
  547. if (unlikely(!dreamcastcard))
  548. return -ENOMEM;
  549. dreamcastcard->card =
  550. snd_card_new(index, SND_AICA_DRIVER, THIS_MODULE, 0);
  551. if (unlikely(!dreamcastcard->card)) {
  552. kfree(dreamcastcard);
  553. return -ENODEV;
  554. }
  555. strcpy(dreamcastcard->card->driver, "snd_aica");
  556. strcpy(dreamcastcard->card->shortname, SND_AICA_DRIVER);
  557. strcpy(dreamcastcard->card->longname,
  558. "Yamaha AICA Super Intelligent Sound Processor for SEGA Dreamcast");
  559. /* Prepare to use the queue */
  560. INIT_WORK(&(dreamcastcard->spu_dma_work), run_spu_dma);
  561. /* Load the PCM 'chip' */
  562. err = snd_aicapcmchip(dreamcastcard, 0);
  563. if (unlikely(err < 0))
  564. goto freedreamcast;
  565. snd_card_set_dev(dreamcastcard->card, &devptr->dev);
  566. dreamcastcard->timer.data = 0;
  567. dreamcastcard->channel = NULL;
  568. /* Add basic controls */
  569. err = add_aicamixer_controls(dreamcastcard);
  570. if (unlikely(err < 0))
  571. goto freedreamcast;
  572. /* Register the card with ALSA subsystem */
  573. err = snd_card_register(dreamcastcard->card);
  574. if (unlikely(err < 0))
  575. goto freedreamcast;
  576. platform_set_drvdata(devptr, dreamcastcard);
  577. aica_queue = create_workqueue(CARD_NAME);
  578. if (unlikely(!aica_queue))
  579. goto freedreamcast;
  580. snd_printk
  581. ("ALSA Driver for Yamaha AICA Super Intelligent Sound Processor\n");
  582. return 0;
  583. freedreamcast:
  584. snd_card_free(dreamcastcard->card);
  585. kfree(dreamcastcard);
  586. return err;
  587. }
  588. static struct platform_driver snd_aica_driver = {
  589. .probe = snd_aica_probe,
  590. .remove = snd_aica_remove,
  591. .driver = {
  592. .name = SND_AICA_DRIVER},
  593. };
  594. static int __init aica_init(void)
  595. {
  596. int err;
  597. err = platform_driver_register(&snd_aica_driver);
  598. if (unlikely(err < 0))
  599. return err;
  600. pd = platform_device_register_simple(SND_AICA_DRIVER, -1,
  601. aica_memory_space, 2);
  602. if (unlikely(IS_ERR(pd))) {
  603. platform_driver_unregister(&snd_aica_driver);
  604. return PTR_ERR(pd);
  605. }
  606. /* Load the firmware */
  607. return load_aica_firmware();
  608. }
  609. static void __exit aica_exit(void)
  610. {
  611. /* Destroy the aica kernel thread *
  612. * being extra cautious to check if it exists*/
  613. if (likely(aica_queue))
  614. destroy_workqueue(aica_queue);
  615. platform_device_unregister(pd);
  616. platform_driver_unregister(&snd_aica_driver);
  617. /* Kill any sound still playing and reset ARM7 to safe state */
  618. spu_reset();
  619. }
  620. module_init(aica_init);
  621. module_exit(aica_exit);