aica.c 18 KB

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