sgio2audio.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /*
  2. * Sound driver for Silicon Graphics O2 Workstations A/V board audio.
  3. *
  4. * Copyright 2003 Vivien Chappelier <vivien.chappelier@linux-mips.org>
  5. * Copyright 2008 Thomas Bogendoerfer <tsbogend@alpha.franken.de>
  6. * Mxier part taken from mace_audio.c:
  7. * Copyright 2007 Thorben Jändling <tj.trevelyan@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/io.h>
  31. #include <linux/slab.h>
  32. #include <asm/ip32/ip32_ints.h>
  33. #include <asm/ip32/mace.h>
  34. #include <sound/core.h>
  35. #include <sound/control.h>
  36. #include <sound/pcm.h>
  37. #define SNDRV_GET_ID
  38. #include <sound/initval.h>
  39. #include <sound/ad1843.h>
  40. MODULE_AUTHOR("Vivien Chappelier <vivien.chappelier@linux-mips.org>");
  41. MODULE_DESCRIPTION("SGI O2 Audio");
  42. MODULE_LICENSE("GPL");
  43. MODULE_SUPPORTED_DEVICE("{{Silicon Graphics, O2 Audio}}");
  44. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  45. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  46. module_param(index, int, 0444);
  47. MODULE_PARM_DESC(index, "Index value for SGI O2 soundcard.");
  48. module_param(id, charp, 0444);
  49. MODULE_PARM_DESC(id, "ID string for SGI O2 soundcard.");
  50. #define AUDIO_CONTROL_RESET BIT(0) /* 1: reset audio interface */
  51. #define AUDIO_CONTROL_CODEC_PRESENT BIT(1) /* 1: codec detected */
  52. #define CODEC_CONTROL_WORD_SHIFT 0
  53. #define CODEC_CONTROL_READ BIT(16)
  54. #define CODEC_CONTROL_ADDRESS_SHIFT 17
  55. #define CHANNEL_CONTROL_RESET BIT(10) /* 1: reset channel */
  56. #define CHANNEL_DMA_ENABLE BIT(9) /* 1: enable DMA transfer */
  57. #define CHANNEL_INT_THRESHOLD_DISABLED (0 << 5) /* interrupt disabled */
  58. #define CHANNEL_INT_THRESHOLD_25 (1 << 5) /* int on buffer >25% full */
  59. #define CHANNEL_INT_THRESHOLD_50 (2 << 5) /* int on buffer >50% full */
  60. #define CHANNEL_INT_THRESHOLD_75 (3 << 5) /* int on buffer >75% full */
  61. #define CHANNEL_INT_THRESHOLD_EMPTY (4 << 5) /* int on buffer empty */
  62. #define CHANNEL_INT_THRESHOLD_NOT_EMPTY (5 << 5) /* int on buffer !empty */
  63. #define CHANNEL_INT_THRESHOLD_FULL (6 << 5) /* int on buffer empty */
  64. #define CHANNEL_INT_THRESHOLD_NOT_FULL (7 << 5) /* int on buffer !empty */
  65. #define CHANNEL_RING_SHIFT 12
  66. #define CHANNEL_RING_SIZE (1 << CHANNEL_RING_SHIFT)
  67. #define CHANNEL_RING_MASK (CHANNEL_RING_SIZE - 1)
  68. #define CHANNEL_LEFT_SHIFT 40
  69. #define CHANNEL_RIGHT_SHIFT 8
  70. struct snd_sgio2audio_chan {
  71. int idx;
  72. struct snd_pcm_substream *substream;
  73. int pos;
  74. snd_pcm_uframes_t size;
  75. spinlock_t lock;
  76. };
  77. /* definition of the chip-specific record */
  78. struct snd_sgio2audio {
  79. struct snd_card *card;
  80. /* codec */
  81. struct snd_ad1843 ad1843;
  82. spinlock_t ad1843_lock;
  83. /* channels */
  84. struct snd_sgio2audio_chan channel[3];
  85. /* resources */
  86. void *ring_base;
  87. dma_addr_t ring_base_dma;
  88. };
  89. /* AD1843 access */
  90. /*
  91. * read_ad1843_reg returns the current contents of a 16 bit AD1843 register.
  92. *
  93. * Returns unsigned register value on success, -errno on failure.
  94. */
  95. static int read_ad1843_reg(void *priv, int reg)
  96. {
  97. struct snd_sgio2audio *chip = priv;
  98. int val;
  99. unsigned long flags;
  100. spin_lock_irqsave(&chip->ad1843_lock, flags);
  101. writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) |
  102. CODEC_CONTROL_READ, &mace->perif.audio.codec_control);
  103. wmb();
  104. val = readq(&mace->perif.audio.codec_control); /* flush bus */
  105. udelay(200);
  106. val = readq(&mace->perif.audio.codec_read);
  107. spin_unlock_irqrestore(&chip->ad1843_lock, flags);
  108. return val;
  109. }
  110. /*
  111. * write_ad1843_reg writes the specified value to a 16 bit AD1843 register.
  112. */
  113. static int write_ad1843_reg(void *priv, int reg, int word)
  114. {
  115. struct snd_sgio2audio *chip = priv;
  116. int val;
  117. unsigned long flags;
  118. spin_lock_irqsave(&chip->ad1843_lock, flags);
  119. writeq((reg << CODEC_CONTROL_ADDRESS_SHIFT) |
  120. (word << CODEC_CONTROL_WORD_SHIFT),
  121. &mace->perif.audio.codec_control);
  122. wmb();
  123. val = readq(&mace->perif.audio.codec_control); /* flush bus */
  124. udelay(200);
  125. spin_unlock_irqrestore(&chip->ad1843_lock, flags);
  126. return 0;
  127. }
  128. static int sgio2audio_gain_info(struct snd_kcontrol *kcontrol,
  129. struct snd_ctl_elem_info *uinfo)
  130. {
  131. struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
  132. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  133. uinfo->count = 2;
  134. uinfo->value.integer.min = 0;
  135. uinfo->value.integer.max = ad1843_get_gain_max(&chip->ad1843,
  136. (int)kcontrol->private_value);
  137. return 0;
  138. }
  139. static int sgio2audio_gain_get(struct snd_kcontrol *kcontrol,
  140. struct snd_ctl_elem_value *ucontrol)
  141. {
  142. struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
  143. int vol;
  144. vol = ad1843_get_gain(&chip->ad1843, (int)kcontrol->private_value);
  145. ucontrol->value.integer.value[0] = (vol >> 8) & 0xFF;
  146. ucontrol->value.integer.value[1] = vol & 0xFF;
  147. return 0;
  148. }
  149. static int sgio2audio_gain_put(struct snd_kcontrol *kcontrol,
  150. struct snd_ctl_elem_value *ucontrol)
  151. {
  152. struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
  153. int newvol, oldvol;
  154. oldvol = ad1843_get_gain(&chip->ad1843, kcontrol->private_value);
  155. newvol = (ucontrol->value.integer.value[0] << 8) |
  156. ucontrol->value.integer.value[1];
  157. newvol = ad1843_set_gain(&chip->ad1843, kcontrol->private_value,
  158. newvol);
  159. return newvol != oldvol;
  160. }
  161. static int sgio2audio_source_info(struct snd_kcontrol *kcontrol,
  162. struct snd_ctl_elem_info *uinfo)
  163. {
  164. static const char *texts[3] = {
  165. "Cam Mic", "Mic", "Line"
  166. };
  167. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  168. uinfo->count = 1;
  169. uinfo->value.enumerated.items = 3;
  170. if (uinfo->value.enumerated.item >= 3)
  171. uinfo->value.enumerated.item = 1;
  172. strcpy(uinfo->value.enumerated.name,
  173. texts[uinfo->value.enumerated.item]);
  174. return 0;
  175. }
  176. static int sgio2audio_source_get(struct snd_kcontrol *kcontrol,
  177. struct snd_ctl_elem_value *ucontrol)
  178. {
  179. struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
  180. ucontrol->value.enumerated.item[0] = ad1843_get_recsrc(&chip->ad1843);
  181. return 0;
  182. }
  183. static int sgio2audio_source_put(struct snd_kcontrol *kcontrol,
  184. struct snd_ctl_elem_value *ucontrol)
  185. {
  186. struct snd_sgio2audio *chip = snd_kcontrol_chip(kcontrol);
  187. int newsrc, oldsrc;
  188. oldsrc = ad1843_get_recsrc(&chip->ad1843);
  189. newsrc = ad1843_set_recsrc(&chip->ad1843,
  190. ucontrol->value.enumerated.item[0]);
  191. return newsrc != oldsrc;
  192. }
  193. /* dac1/pcm0 mixer control */
  194. static struct snd_kcontrol_new sgio2audio_ctrl_pcm0 __devinitdata = {
  195. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  196. .name = "PCM Playback Volume",
  197. .index = 0,
  198. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  199. .private_value = AD1843_GAIN_PCM_0,
  200. .info = sgio2audio_gain_info,
  201. .get = sgio2audio_gain_get,
  202. .put = sgio2audio_gain_put,
  203. };
  204. /* dac2/pcm1 mixer control */
  205. static struct snd_kcontrol_new sgio2audio_ctrl_pcm1 __devinitdata = {
  206. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  207. .name = "PCM Playback Volume",
  208. .index = 1,
  209. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  210. .private_value = AD1843_GAIN_PCM_1,
  211. .info = sgio2audio_gain_info,
  212. .get = sgio2audio_gain_get,
  213. .put = sgio2audio_gain_put,
  214. };
  215. /* record level mixer control */
  216. static struct snd_kcontrol_new sgio2audio_ctrl_reclevel __devinitdata = {
  217. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  218. .name = "Capture Volume",
  219. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  220. .private_value = AD1843_GAIN_RECLEV,
  221. .info = sgio2audio_gain_info,
  222. .get = sgio2audio_gain_get,
  223. .put = sgio2audio_gain_put,
  224. };
  225. /* record level source control */
  226. static struct snd_kcontrol_new sgio2audio_ctrl_recsource __devinitdata = {
  227. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  228. .name = "Capture Source",
  229. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  230. .info = sgio2audio_source_info,
  231. .get = sgio2audio_source_get,
  232. .put = sgio2audio_source_put,
  233. };
  234. /* line mixer control */
  235. static struct snd_kcontrol_new sgio2audio_ctrl_line __devinitdata = {
  236. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  237. .name = "Line Playback Volume",
  238. .index = 0,
  239. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  240. .private_value = AD1843_GAIN_LINE,
  241. .info = sgio2audio_gain_info,
  242. .get = sgio2audio_gain_get,
  243. .put = sgio2audio_gain_put,
  244. };
  245. /* cd mixer control */
  246. static struct snd_kcontrol_new sgio2audio_ctrl_cd __devinitdata = {
  247. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  248. .name = "Line Playback Volume",
  249. .index = 1,
  250. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  251. .private_value = AD1843_GAIN_LINE_2,
  252. .info = sgio2audio_gain_info,
  253. .get = sgio2audio_gain_get,
  254. .put = sgio2audio_gain_put,
  255. };
  256. /* mic mixer control */
  257. static struct snd_kcontrol_new sgio2audio_ctrl_mic __devinitdata = {
  258. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  259. .name = "Mic Playback Volume",
  260. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  261. .private_value = AD1843_GAIN_MIC,
  262. .info = sgio2audio_gain_info,
  263. .get = sgio2audio_gain_get,
  264. .put = sgio2audio_gain_put,
  265. };
  266. static int __devinit snd_sgio2audio_new_mixer(struct snd_sgio2audio *chip)
  267. {
  268. int err;
  269. err = snd_ctl_add(chip->card,
  270. snd_ctl_new1(&sgio2audio_ctrl_pcm0, chip));
  271. if (err < 0)
  272. return err;
  273. err = snd_ctl_add(chip->card,
  274. snd_ctl_new1(&sgio2audio_ctrl_pcm1, chip));
  275. if (err < 0)
  276. return err;
  277. err = snd_ctl_add(chip->card,
  278. snd_ctl_new1(&sgio2audio_ctrl_reclevel, chip));
  279. if (err < 0)
  280. return err;
  281. err = snd_ctl_add(chip->card,
  282. snd_ctl_new1(&sgio2audio_ctrl_recsource, chip));
  283. if (err < 0)
  284. return err;
  285. err = snd_ctl_add(chip->card,
  286. snd_ctl_new1(&sgio2audio_ctrl_line, chip));
  287. if (err < 0)
  288. return err;
  289. err = snd_ctl_add(chip->card,
  290. snd_ctl_new1(&sgio2audio_ctrl_cd, chip));
  291. if (err < 0)
  292. return err;
  293. err = snd_ctl_add(chip->card,
  294. snd_ctl_new1(&sgio2audio_ctrl_mic, chip));
  295. if (err < 0)
  296. return err;
  297. return 0;
  298. }
  299. /* low-level audio interface DMA */
  300. /* get data out of bounce buffer, count must be a multiple of 32 */
  301. /* returns 1 if a period has elapsed */
  302. static int snd_sgio2audio_dma_pull_frag(struct snd_sgio2audio *chip,
  303. unsigned int ch, unsigned int count)
  304. {
  305. int ret;
  306. unsigned long src_base, src_pos, dst_mask;
  307. unsigned char *dst_base;
  308. int dst_pos;
  309. u64 *src;
  310. s16 *dst;
  311. u64 x;
  312. unsigned long flags;
  313. struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime;
  314. spin_lock_irqsave(&chip->channel[ch].lock, flags);
  315. src_base = (unsigned long) chip->ring_base | (ch << CHANNEL_RING_SHIFT);
  316. src_pos = readq(&mace->perif.audio.chan[ch].read_ptr);
  317. dst_base = runtime->dma_area;
  318. dst_pos = chip->channel[ch].pos;
  319. dst_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1;
  320. /* check if a period has elapsed */
  321. chip->channel[ch].size += (count >> 3); /* in frames */
  322. ret = chip->channel[ch].size >= runtime->period_size;
  323. chip->channel[ch].size %= runtime->period_size;
  324. while (count) {
  325. src = (u64 *)(src_base + src_pos);
  326. dst = (s16 *)(dst_base + dst_pos);
  327. x = *src;
  328. dst[0] = (x >> CHANNEL_LEFT_SHIFT) & 0xffff;
  329. dst[1] = (x >> CHANNEL_RIGHT_SHIFT) & 0xffff;
  330. src_pos = (src_pos + sizeof(u64)) & CHANNEL_RING_MASK;
  331. dst_pos = (dst_pos + 2 * sizeof(s16)) & dst_mask;
  332. count -= sizeof(u64);
  333. }
  334. writeq(src_pos, &mace->perif.audio.chan[ch].read_ptr); /* in bytes */
  335. chip->channel[ch].pos = dst_pos;
  336. spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
  337. return ret;
  338. }
  339. /* put some DMA data in bounce buffer, count must be a multiple of 32 */
  340. /* returns 1 if a period has elapsed */
  341. static int snd_sgio2audio_dma_push_frag(struct snd_sgio2audio *chip,
  342. unsigned int ch, unsigned int count)
  343. {
  344. int ret;
  345. s64 l, r;
  346. unsigned long dst_base, dst_pos, src_mask;
  347. unsigned char *src_base;
  348. int src_pos;
  349. u64 *dst;
  350. s16 *src;
  351. unsigned long flags;
  352. struct snd_pcm_runtime *runtime = chip->channel[ch].substream->runtime;
  353. spin_lock_irqsave(&chip->channel[ch].lock, flags);
  354. dst_base = (unsigned long)chip->ring_base | (ch << CHANNEL_RING_SHIFT);
  355. dst_pos = readq(&mace->perif.audio.chan[ch].write_ptr);
  356. src_base = runtime->dma_area;
  357. src_pos = chip->channel[ch].pos;
  358. src_mask = frames_to_bytes(runtime, runtime->buffer_size) - 1;
  359. /* check if a period has elapsed */
  360. chip->channel[ch].size += (count >> 3); /* in frames */
  361. ret = chip->channel[ch].size >= runtime->period_size;
  362. chip->channel[ch].size %= runtime->period_size;
  363. while (count) {
  364. src = (s16 *)(src_base + src_pos);
  365. dst = (u64 *)(dst_base + dst_pos);
  366. l = src[0]; /* sign extend */
  367. r = src[1]; /* sign extend */
  368. *dst = ((l & 0x00ffffff) << CHANNEL_LEFT_SHIFT) |
  369. ((r & 0x00ffffff) << CHANNEL_RIGHT_SHIFT);
  370. dst_pos = (dst_pos + sizeof(u64)) & CHANNEL_RING_MASK;
  371. src_pos = (src_pos + 2 * sizeof(s16)) & src_mask;
  372. count -= sizeof(u64);
  373. }
  374. writeq(dst_pos, &mace->perif.audio.chan[ch].write_ptr); /* in bytes */
  375. chip->channel[ch].pos = src_pos;
  376. spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
  377. return ret;
  378. }
  379. static int snd_sgio2audio_dma_start(struct snd_pcm_substream *substream)
  380. {
  381. struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
  382. struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
  383. int ch = chan->idx;
  384. /* reset DMA channel */
  385. writeq(CHANNEL_CONTROL_RESET, &mace->perif.audio.chan[ch].control);
  386. udelay(10);
  387. writeq(0, &mace->perif.audio.chan[ch].control);
  388. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  389. /* push a full buffer */
  390. snd_sgio2audio_dma_push_frag(chip, ch, CHANNEL_RING_SIZE - 32);
  391. }
  392. /* set DMA to wake on 50% empty and enable interrupt */
  393. writeq(CHANNEL_DMA_ENABLE | CHANNEL_INT_THRESHOLD_50,
  394. &mace->perif.audio.chan[ch].control);
  395. return 0;
  396. }
  397. static int snd_sgio2audio_dma_stop(struct snd_pcm_substream *substream)
  398. {
  399. struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
  400. writeq(0, &mace->perif.audio.chan[chan->idx].control);
  401. return 0;
  402. }
  403. static irqreturn_t snd_sgio2audio_dma_in_isr(int irq, void *dev_id)
  404. {
  405. struct snd_sgio2audio_chan *chan = dev_id;
  406. struct snd_pcm_substream *substream;
  407. struct snd_sgio2audio *chip;
  408. int count, ch;
  409. substream = chan->substream;
  410. chip = snd_pcm_substream_chip(substream);
  411. ch = chan->idx;
  412. /* empty the ring */
  413. count = CHANNEL_RING_SIZE -
  414. readq(&mace->perif.audio.chan[ch].depth) - 32;
  415. if (snd_sgio2audio_dma_pull_frag(chip, ch, count))
  416. snd_pcm_period_elapsed(substream);
  417. return IRQ_HANDLED;
  418. }
  419. static irqreturn_t snd_sgio2audio_dma_out_isr(int irq, void *dev_id)
  420. {
  421. struct snd_sgio2audio_chan *chan = dev_id;
  422. struct snd_pcm_substream *substream;
  423. struct snd_sgio2audio *chip;
  424. int count, ch;
  425. substream = chan->substream;
  426. chip = snd_pcm_substream_chip(substream);
  427. ch = chan->idx;
  428. /* fill the ring */
  429. count = CHANNEL_RING_SIZE -
  430. readq(&mace->perif.audio.chan[ch].depth) - 32;
  431. if (snd_sgio2audio_dma_push_frag(chip, ch, count))
  432. snd_pcm_period_elapsed(substream);
  433. return IRQ_HANDLED;
  434. }
  435. static irqreturn_t snd_sgio2audio_error_isr(int irq, void *dev_id)
  436. {
  437. struct snd_sgio2audio_chan *chan = dev_id;
  438. struct snd_pcm_substream *substream;
  439. substream = chan->substream;
  440. snd_sgio2audio_dma_stop(substream);
  441. snd_sgio2audio_dma_start(substream);
  442. return IRQ_HANDLED;
  443. }
  444. /* PCM part */
  445. /* PCM hardware definition */
  446. static struct snd_pcm_hardware snd_sgio2audio_pcm_hw = {
  447. .info = (SNDRV_PCM_INFO_MMAP |
  448. SNDRV_PCM_INFO_MMAP_VALID |
  449. SNDRV_PCM_INFO_INTERLEAVED |
  450. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  451. .formats = SNDRV_PCM_FMTBIT_S16_BE,
  452. .rates = SNDRV_PCM_RATE_8000_48000,
  453. .rate_min = 8000,
  454. .rate_max = 48000,
  455. .channels_min = 2,
  456. .channels_max = 2,
  457. .buffer_bytes_max = 65536,
  458. .period_bytes_min = 32768,
  459. .period_bytes_max = 65536,
  460. .periods_min = 1,
  461. .periods_max = 1024,
  462. };
  463. /* PCM playback open callback */
  464. static int snd_sgio2audio_playback1_open(struct snd_pcm_substream *substream)
  465. {
  466. struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
  467. struct snd_pcm_runtime *runtime = substream->runtime;
  468. runtime->hw = snd_sgio2audio_pcm_hw;
  469. runtime->private_data = &chip->channel[1];
  470. return 0;
  471. }
  472. static int snd_sgio2audio_playback2_open(struct snd_pcm_substream *substream)
  473. {
  474. struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
  475. struct snd_pcm_runtime *runtime = substream->runtime;
  476. runtime->hw = snd_sgio2audio_pcm_hw;
  477. runtime->private_data = &chip->channel[2];
  478. return 0;
  479. }
  480. /* PCM capture open callback */
  481. static int snd_sgio2audio_capture_open(struct snd_pcm_substream *substream)
  482. {
  483. struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
  484. struct snd_pcm_runtime *runtime = substream->runtime;
  485. runtime->hw = snd_sgio2audio_pcm_hw;
  486. runtime->private_data = &chip->channel[0];
  487. return 0;
  488. }
  489. /* PCM close callback */
  490. static int snd_sgio2audio_pcm_close(struct snd_pcm_substream *substream)
  491. {
  492. struct snd_pcm_runtime *runtime = substream->runtime;
  493. runtime->private_data = NULL;
  494. return 0;
  495. }
  496. /* hw_params callback */
  497. static int snd_sgio2audio_pcm_hw_params(struct snd_pcm_substream *substream,
  498. struct snd_pcm_hw_params *hw_params)
  499. {
  500. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  501. params_buffer_bytes(hw_params));
  502. }
  503. /* hw_free callback */
  504. static int snd_sgio2audio_pcm_hw_free(struct snd_pcm_substream *substream)
  505. {
  506. return snd_pcm_lib_free_vmalloc_buffer(substream);
  507. }
  508. /* prepare callback */
  509. static int snd_sgio2audio_pcm_prepare(struct snd_pcm_substream *substream)
  510. {
  511. struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
  512. struct snd_pcm_runtime *runtime = substream->runtime;
  513. struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
  514. int ch = chan->idx;
  515. unsigned long flags;
  516. spin_lock_irqsave(&chip->channel[ch].lock, flags);
  517. /* Setup the pseudo-dma transfer pointers. */
  518. chip->channel[ch].pos = 0;
  519. chip->channel[ch].size = 0;
  520. chip->channel[ch].substream = substream;
  521. /* set AD1843 format */
  522. /* hardware format is always S16_LE */
  523. switch (substream->stream) {
  524. case SNDRV_PCM_STREAM_PLAYBACK:
  525. ad1843_setup_dac(&chip->ad1843,
  526. ch - 1,
  527. runtime->rate,
  528. SNDRV_PCM_FORMAT_S16_LE,
  529. runtime->channels);
  530. break;
  531. case SNDRV_PCM_STREAM_CAPTURE:
  532. ad1843_setup_adc(&chip->ad1843,
  533. runtime->rate,
  534. SNDRV_PCM_FORMAT_S16_LE,
  535. runtime->channels);
  536. break;
  537. }
  538. spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
  539. return 0;
  540. }
  541. /* trigger callback */
  542. static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream *substream,
  543. int cmd)
  544. {
  545. switch (cmd) {
  546. case SNDRV_PCM_TRIGGER_START:
  547. /* start the PCM engine */
  548. snd_sgio2audio_dma_start(substream);
  549. break;
  550. case SNDRV_PCM_TRIGGER_STOP:
  551. /* stop the PCM engine */
  552. snd_sgio2audio_dma_stop(substream);
  553. break;
  554. default:
  555. return -EINVAL;
  556. }
  557. return 0;
  558. }
  559. /* pointer callback */
  560. static snd_pcm_uframes_t
  561. snd_sgio2audio_pcm_pointer(struct snd_pcm_substream *substream)
  562. {
  563. struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
  564. struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
  565. /* get the current hardware pointer */
  566. return bytes_to_frames(substream->runtime,
  567. chip->channel[chan->idx].pos);
  568. }
  569. /* operators */
  570. static struct snd_pcm_ops snd_sgio2audio_playback1_ops = {
  571. .open = snd_sgio2audio_playback1_open,
  572. .close = snd_sgio2audio_pcm_close,
  573. .ioctl = snd_pcm_lib_ioctl,
  574. .hw_params = snd_sgio2audio_pcm_hw_params,
  575. .hw_free = snd_sgio2audio_pcm_hw_free,
  576. .prepare = snd_sgio2audio_pcm_prepare,
  577. .trigger = snd_sgio2audio_pcm_trigger,
  578. .pointer = snd_sgio2audio_pcm_pointer,
  579. .page = snd_pcm_lib_get_vmalloc_page,
  580. .mmap = snd_pcm_lib_mmap_vmalloc,
  581. };
  582. static struct snd_pcm_ops snd_sgio2audio_playback2_ops = {
  583. .open = snd_sgio2audio_playback2_open,
  584. .close = snd_sgio2audio_pcm_close,
  585. .ioctl = snd_pcm_lib_ioctl,
  586. .hw_params = snd_sgio2audio_pcm_hw_params,
  587. .hw_free = snd_sgio2audio_pcm_hw_free,
  588. .prepare = snd_sgio2audio_pcm_prepare,
  589. .trigger = snd_sgio2audio_pcm_trigger,
  590. .pointer = snd_sgio2audio_pcm_pointer,
  591. .page = snd_pcm_lib_get_vmalloc_page,
  592. .mmap = snd_pcm_lib_mmap_vmalloc,
  593. };
  594. static struct snd_pcm_ops snd_sgio2audio_capture_ops = {
  595. .open = snd_sgio2audio_capture_open,
  596. .close = snd_sgio2audio_pcm_close,
  597. .ioctl = snd_pcm_lib_ioctl,
  598. .hw_params = snd_sgio2audio_pcm_hw_params,
  599. .hw_free = snd_sgio2audio_pcm_hw_free,
  600. .prepare = snd_sgio2audio_pcm_prepare,
  601. .trigger = snd_sgio2audio_pcm_trigger,
  602. .pointer = snd_sgio2audio_pcm_pointer,
  603. .page = snd_pcm_lib_get_vmalloc_page,
  604. .mmap = snd_pcm_lib_mmap_vmalloc,
  605. };
  606. /*
  607. * definitions of capture are omitted here...
  608. */
  609. /* create a pcm device */
  610. static int __devinit snd_sgio2audio_new_pcm(struct snd_sgio2audio *chip)
  611. {
  612. struct snd_pcm *pcm;
  613. int err;
  614. /* create first pcm device with one outputs and one input */
  615. err = snd_pcm_new(chip->card, "SGI O2 Audio", 0, 1, 1, &pcm);
  616. if (err < 0)
  617. return err;
  618. pcm->private_data = chip;
  619. strcpy(pcm->name, "SGI O2 DAC1");
  620. /* set operators */
  621. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  622. &snd_sgio2audio_playback1_ops);
  623. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  624. &snd_sgio2audio_capture_ops);
  625. /* create second pcm device with one outputs and no input */
  626. err = snd_pcm_new(chip->card, "SGI O2 Audio", 1, 1, 0, &pcm);
  627. if (err < 0)
  628. return err;
  629. pcm->private_data = chip;
  630. strcpy(pcm->name, "SGI O2 DAC2");
  631. /* set operators */
  632. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  633. &snd_sgio2audio_playback2_ops);
  634. return 0;
  635. }
  636. static struct {
  637. int idx;
  638. int irq;
  639. irqreturn_t (*isr)(int, void *);
  640. const char *desc;
  641. } snd_sgio2_isr_table[] = {
  642. {
  643. .idx = 0,
  644. .irq = MACEISA_AUDIO1_DMAT_IRQ,
  645. .isr = snd_sgio2audio_dma_in_isr,
  646. .desc = "Capture DMA Channel 0"
  647. }, {
  648. .idx = 0,
  649. .irq = MACEISA_AUDIO1_OF_IRQ,
  650. .isr = snd_sgio2audio_error_isr,
  651. .desc = "Capture Overflow"
  652. }, {
  653. .idx = 1,
  654. .irq = MACEISA_AUDIO2_DMAT_IRQ,
  655. .isr = snd_sgio2audio_dma_out_isr,
  656. .desc = "Playback DMA Channel 1"
  657. }, {
  658. .idx = 1,
  659. .irq = MACEISA_AUDIO2_MERR_IRQ,
  660. .isr = snd_sgio2audio_error_isr,
  661. .desc = "Memory Error Channel 1"
  662. }, {
  663. .idx = 2,
  664. .irq = MACEISA_AUDIO3_DMAT_IRQ,
  665. .isr = snd_sgio2audio_dma_out_isr,
  666. .desc = "Playback DMA Channel 2"
  667. }, {
  668. .idx = 2,
  669. .irq = MACEISA_AUDIO3_MERR_IRQ,
  670. .isr = snd_sgio2audio_error_isr,
  671. .desc = "Memory Error Channel 2"
  672. }
  673. };
  674. /* ALSA driver */
  675. static int snd_sgio2audio_free(struct snd_sgio2audio *chip)
  676. {
  677. int i;
  678. /* reset interface */
  679. writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
  680. udelay(1);
  681. writeq(0, &mace->perif.audio.control);
  682. /* release IRQ's */
  683. for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++)
  684. free_irq(snd_sgio2_isr_table[i].irq,
  685. &chip->channel[snd_sgio2_isr_table[i].idx]);
  686. dma_free_coherent(NULL, MACEISA_RINGBUFFERS_SIZE,
  687. chip->ring_base, chip->ring_base_dma);
  688. /* release card data */
  689. kfree(chip);
  690. return 0;
  691. }
  692. static int snd_sgio2audio_dev_free(struct snd_device *device)
  693. {
  694. struct snd_sgio2audio *chip = device->device_data;
  695. return snd_sgio2audio_free(chip);
  696. }
  697. static struct snd_device_ops ops = {
  698. .dev_free = snd_sgio2audio_dev_free,
  699. };
  700. static int __devinit snd_sgio2audio_create(struct snd_card *card,
  701. struct snd_sgio2audio **rchip)
  702. {
  703. struct snd_sgio2audio *chip;
  704. int i, err;
  705. *rchip = NULL;
  706. /* check if a codec is attached to the interface */
  707. /* (Audio or Audio/Video board present) */
  708. if (!(readq(&mace->perif.audio.control) & AUDIO_CONTROL_CODEC_PRESENT))
  709. return -ENOENT;
  710. chip = kzalloc(sizeof(struct snd_sgio2audio), GFP_KERNEL);
  711. if (chip == NULL)
  712. return -ENOMEM;
  713. chip->card = card;
  714. chip->ring_base = dma_alloc_coherent(NULL, MACEISA_RINGBUFFERS_SIZE,
  715. &chip->ring_base_dma, GFP_USER);
  716. if (chip->ring_base == NULL) {
  717. printk(KERN_ERR
  718. "sgio2audio: could not allocate ring buffers\n");
  719. kfree(chip);
  720. return -ENOMEM;
  721. }
  722. spin_lock_init(&chip->ad1843_lock);
  723. /* initialize channels */
  724. for (i = 0; i < 3; i++) {
  725. spin_lock_init(&chip->channel[i].lock);
  726. chip->channel[i].idx = i;
  727. }
  728. /* allocate IRQs */
  729. for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++) {
  730. if (request_irq(snd_sgio2_isr_table[i].irq,
  731. snd_sgio2_isr_table[i].isr,
  732. 0,
  733. snd_sgio2_isr_table[i].desc,
  734. &chip->channel[snd_sgio2_isr_table[i].idx])) {
  735. snd_sgio2audio_free(chip);
  736. printk(KERN_ERR "sgio2audio: cannot allocate irq %d\n",
  737. snd_sgio2_isr_table[i].irq);
  738. return -EBUSY;
  739. }
  740. }
  741. /* reset the interface */
  742. writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
  743. udelay(1);
  744. writeq(0, &mace->perif.audio.control);
  745. msleep_interruptible(1); /* give time to recover */
  746. /* set ring base */
  747. writeq(chip->ring_base_dma, &mace->perif.ctrl.ringbase);
  748. /* attach the AD1843 codec */
  749. chip->ad1843.read = read_ad1843_reg;
  750. chip->ad1843.write = write_ad1843_reg;
  751. chip->ad1843.chip = chip;
  752. /* initialize the AD1843 codec */
  753. err = ad1843_init(&chip->ad1843);
  754. if (err < 0) {
  755. snd_sgio2audio_free(chip);
  756. return err;
  757. }
  758. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  759. if (err < 0) {
  760. snd_sgio2audio_free(chip);
  761. return err;
  762. }
  763. *rchip = chip;
  764. return 0;
  765. }
  766. static int __devinit snd_sgio2audio_probe(struct platform_device *pdev)
  767. {
  768. struct snd_card *card;
  769. struct snd_sgio2audio *chip;
  770. int err;
  771. err = snd_card_create(index, id, THIS_MODULE, 0, &card);
  772. if (err < 0)
  773. return err;
  774. err = snd_sgio2audio_create(card, &chip);
  775. if (err < 0) {
  776. snd_card_free(card);
  777. return err;
  778. }
  779. snd_card_set_dev(card, &pdev->dev);
  780. err = snd_sgio2audio_new_pcm(chip);
  781. if (err < 0) {
  782. snd_card_free(card);
  783. return err;
  784. }
  785. err = snd_sgio2audio_new_mixer(chip);
  786. if (err < 0) {
  787. snd_card_free(card);
  788. return err;
  789. }
  790. strcpy(card->driver, "SGI O2 Audio");
  791. strcpy(card->shortname, "SGI O2 Audio");
  792. sprintf(card->longname, "%s irq %i-%i",
  793. card->shortname,
  794. MACEISA_AUDIO1_DMAT_IRQ,
  795. MACEISA_AUDIO3_MERR_IRQ);
  796. err = snd_card_register(card);
  797. if (err < 0) {
  798. snd_card_free(card);
  799. return err;
  800. }
  801. platform_set_drvdata(pdev, card);
  802. return 0;
  803. }
  804. static int __devexit snd_sgio2audio_remove(struct platform_device *pdev)
  805. {
  806. struct snd_card *card = platform_get_drvdata(pdev);
  807. snd_card_free(card);
  808. platform_set_drvdata(pdev, NULL);
  809. return 0;
  810. }
  811. static struct platform_driver sgio2audio_driver = {
  812. .probe = snd_sgio2audio_probe,
  813. .remove = __devexit_p(snd_sgio2audio_remove),
  814. .driver = {
  815. .name = "sgio2audio",
  816. .owner = THIS_MODULE,
  817. }
  818. };
  819. static int __init alsa_card_sgio2audio_init(void)
  820. {
  821. return platform_driver_register(&sgio2audio_driver);
  822. }
  823. static void __exit alsa_card_sgio2audio_exit(void)
  824. {
  825. platform_driver_unregister(&sgio2audio_driver);
  826. }
  827. module_init(alsa_card_sgio2audio_init)
  828. module_exit(alsa_card_sgio2audio_exit)