aica.c 18 KB

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