sgio2audio.c 27 KB

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