aica.c 19 KB

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