sgio2audio.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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/gfp.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/io.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. struct snd_pcm_runtime *runtime = substream->runtime;
  502. int size = params_buffer_bytes(hw_params);
  503. /* alloc virtual 'dma' area */
  504. if (runtime->dma_area)
  505. vfree(runtime->dma_area);
  506. runtime->dma_area = vmalloc(size);
  507. if (runtime->dma_area == NULL)
  508. return -ENOMEM;
  509. runtime->dma_bytes = size;
  510. return 0;
  511. }
  512. /* hw_free callback */
  513. static int snd_sgio2audio_pcm_hw_free(struct snd_pcm_substream *substream)
  514. {
  515. vfree(substream->runtime->dma_area);
  516. substream->runtime->dma_area = NULL;
  517. return 0;
  518. }
  519. /* prepare callback */
  520. static int snd_sgio2audio_pcm_prepare(struct snd_pcm_substream *substream)
  521. {
  522. struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
  523. struct snd_pcm_runtime *runtime = substream->runtime;
  524. struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
  525. int ch = chan->idx;
  526. unsigned long flags;
  527. spin_lock_irqsave(&chip->channel[ch].lock, flags);
  528. /* Setup the pseudo-dma transfer pointers. */
  529. chip->channel[ch].pos = 0;
  530. chip->channel[ch].size = 0;
  531. chip->channel[ch].substream = substream;
  532. /* set AD1843 format */
  533. /* hardware format is always S16_LE */
  534. switch (substream->stream) {
  535. case SNDRV_PCM_STREAM_PLAYBACK:
  536. ad1843_setup_dac(&chip->ad1843,
  537. ch - 1,
  538. runtime->rate,
  539. SNDRV_PCM_FORMAT_S16_LE,
  540. runtime->channels);
  541. break;
  542. case SNDRV_PCM_STREAM_CAPTURE:
  543. ad1843_setup_adc(&chip->ad1843,
  544. runtime->rate,
  545. SNDRV_PCM_FORMAT_S16_LE,
  546. runtime->channels);
  547. break;
  548. }
  549. spin_unlock_irqrestore(&chip->channel[ch].lock, flags);
  550. return 0;
  551. }
  552. /* trigger callback */
  553. static int snd_sgio2audio_pcm_trigger(struct snd_pcm_substream *substream,
  554. int cmd)
  555. {
  556. switch (cmd) {
  557. case SNDRV_PCM_TRIGGER_START:
  558. /* start the PCM engine */
  559. snd_sgio2audio_dma_start(substream);
  560. break;
  561. case SNDRV_PCM_TRIGGER_STOP:
  562. /* stop the PCM engine */
  563. snd_sgio2audio_dma_stop(substream);
  564. break;
  565. default:
  566. return -EINVAL;
  567. }
  568. return 0;
  569. }
  570. /* pointer callback */
  571. static snd_pcm_uframes_t
  572. snd_sgio2audio_pcm_pointer(struct snd_pcm_substream *substream)
  573. {
  574. struct snd_sgio2audio *chip = snd_pcm_substream_chip(substream);
  575. struct snd_sgio2audio_chan *chan = substream->runtime->private_data;
  576. /* get the current hardware pointer */
  577. return bytes_to_frames(substream->runtime,
  578. chip->channel[chan->idx].pos);
  579. }
  580. /* get the physical page pointer on the given offset */
  581. static struct page *snd_sgio2audio_page(struct snd_pcm_substream *substream,
  582. unsigned long offset)
  583. {
  584. return vmalloc_to_page(substream->runtime->dma_area + offset);
  585. }
  586. /* operators */
  587. static struct snd_pcm_ops snd_sgio2audio_playback1_ops = {
  588. .open = snd_sgio2audio_playback1_open,
  589. .close = snd_sgio2audio_pcm_close,
  590. .ioctl = snd_pcm_lib_ioctl,
  591. .hw_params = snd_sgio2audio_pcm_hw_params,
  592. .hw_free = snd_sgio2audio_pcm_hw_free,
  593. .prepare = snd_sgio2audio_pcm_prepare,
  594. .trigger = snd_sgio2audio_pcm_trigger,
  595. .pointer = snd_sgio2audio_pcm_pointer,
  596. .page = snd_sgio2audio_page,
  597. };
  598. static struct snd_pcm_ops snd_sgio2audio_playback2_ops = {
  599. .open = snd_sgio2audio_playback2_open,
  600. .close = snd_sgio2audio_pcm_close,
  601. .ioctl = snd_pcm_lib_ioctl,
  602. .hw_params = snd_sgio2audio_pcm_hw_params,
  603. .hw_free = snd_sgio2audio_pcm_hw_free,
  604. .prepare = snd_sgio2audio_pcm_prepare,
  605. .trigger = snd_sgio2audio_pcm_trigger,
  606. .pointer = snd_sgio2audio_pcm_pointer,
  607. .page = snd_sgio2audio_page,
  608. };
  609. static struct snd_pcm_ops snd_sgio2audio_capture_ops = {
  610. .open = snd_sgio2audio_capture_open,
  611. .close = snd_sgio2audio_pcm_close,
  612. .ioctl = snd_pcm_lib_ioctl,
  613. .hw_params = snd_sgio2audio_pcm_hw_params,
  614. .hw_free = snd_sgio2audio_pcm_hw_free,
  615. .prepare = snd_sgio2audio_pcm_prepare,
  616. .trigger = snd_sgio2audio_pcm_trigger,
  617. .pointer = snd_sgio2audio_pcm_pointer,
  618. .page = snd_sgio2audio_page,
  619. };
  620. /*
  621. * definitions of capture are omitted here...
  622. */
  623. /* create a pcm device */
  624. static int __devinit snd_sgio2audio_new_pcm(struct snd_sgio2audio *chip)
  625. {
  626. struct snd_pcm *pcm;
  627. int err;
  628. /* create first pcm device with one outputs and one input */
  629. err = snd_pcm_new(chip->card, "SGI O2 Audio", 0, 1, 1, &pcm);
  630. if (err < 0)
  631. return err;
  632. pcm->private_data = chip;
  633. strcpy(pcm->name, "SGI O2 DAC1");
  634. /* set operators */
  635. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  636. &snd_sgio2audio_playback1_ops);
  637. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  638. &snd_sgio2audio_capture_ops);
  639. /* create second pcm device with one outputs and no input */
  640. err = snd_pcm_new(chip->card, "SGI O2 Audio", 1, 1, 0, &pcm);
  641. if (err < 0)
  642. return err;
  643. pcm->private_data = chip;
  644. strcpy(pcm->name, "SGI O2 DAC2");
  645. /* set operators */
  646. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  647. &snd_sgio2audio_playback2_ops);
  648. return 0;
  649. }
  650. static struct {
  651. int idx;
  652. int irq;
  653. irqreturn_t (*isr)(int, void *);
  654. const char *desc;
  655. } snd_sgio2_isr_table[] = {
  656. {
  657. .idx = 0,
  658. .irq = MACEISA_AUDIO1_DMAT_IRQ,
  659. .isr = snd_sgio2audio_dma_in_isr,
  660. .desc = "Capture DMA Channel 0"
  661. }, {
  662. .idx = 0,
  663. .irq = MACEISA_AUDIO1_OF_IRQ,
  664. .isr = snd_sgio2audio_error_isr,
  665. .desc = "Capture Overflow"
  666. }, {
  667. .idx = 1,
  668. .irq = MACEISA_AUDIO2_DMAT_IRQ,
  669. .isr = snd_sgio2audio_dma_out_isr,
  670. .desc = "Playback DMA Channel 1"
  671. }, {
  672. .idx = 1,
  673. .irq = MACEISA_AUDIO2_MERR_IRQ,
  674. .isr = snd_sgio2audio_error_isr,
  675. .desc = "Memory Error Channel 1"
  676. }, {
  677. .idx = 2,
  678. .irq = MACEISA_AUDIO3_DMAT_IRQ,
  679. .isr = snd_sgio2audio_dma_out_isr,
  680. .desc = "Playback DMA Channel 2"
  681. }, {
  682. .idx = 2,
  683. .irq = MACEISA_AUDIO3_MERR_IRQ,
  684. .isr = snd_sgio2audio_error_isr,
  685. .desc = "Memory Error Channel 2"
  686. }
  687. };
  688. /* ALSA driver */
  689. static int snd_sgio2audio_free(struct snd_sgio2audio *chip)
  690. {
  691. int i;
  692. /* reset interface */
  693. writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
  694. udelay(1);
  695. writeq(0, &mace->perif.audio.control);
  696. /* release IRQ's */
  697. for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++)
  698. free_irq(snd_sgio2_isr_table[i].irq,
  699. &chip->channel[snd_sgio2_isr_table[i].idx]);
  700. dma_free_coherent(NULL, MACEISA_RINGBUFFERS_SIZE,
  701. chip->ring_base, chip->ring_base_dma);
  702. /* release card data */
  703. kfree(chip);
  704. return 0;
  705. }
  706. static int snd_sgio2audio_dev_free(struct snd_device *device)
  707. {
  708. struct snd_sgio2audio *chip = device->device_data;
  709. return snd_sgio2audio_free(chip);
  710. }
  711. static struct snd_device_ops ops = {
  712. .dev_free = snd_sgio2audio_dev_free,
  713. };
  714. static int __devinit snd_sgio2audio_create(struct snd_card *card,
  715. struct snd_sgio2audio **rchip)
  716. {
  717. struct snd_sgio2audio *chip;
  718. int i, err;
  719. *rchip = NULL;
  720. /* check if a codec is attached to the interface */
  721. /* (Audio or Audio/Video board present) */
  722. if (!(readq(&mace->perif.audio.control) & AUDIO_CONTROL_CODEC_PRESENT))
  723. return -ENOENT;
  724. chip = kzalloc(sizeof(struct snd_sgio2audio), GFP_KERNEL);
  725. if (chip == NULL)
  726. return -ENOMEM;
  727. chip->card = card;
  728. chip->ring_base = dma_alloc_coherent(NULL, MACEISA_RINGBUFFERS_SIZE,
  729. &chip->ring_base_dma, GFP_USER);
  730. if (chip->ring_base == NULL) {
  731. printk(KERN_ERR
  732. "sgio2audio: could not allocate ring buffers\n");
  733. kfree(chip);
  734. return -ENOMEM;
  735. }
  736. spin_lock_init(&chip->ad1843_lock);
  737. /* initialize channels */
  738. for (i = 0; i < 3; i++) {
  739. spin_lock_init(&chip->channel[i].lock);
  740. chip->channel[i].idx = i;
  741. }
  742. /* allocate IRQs */
  743. for (i = 0; i < ARRAY_SIZE(snd_sgio2_isr_table); i++) {
  744. if (request_irq(snd_sgio2_isr_table[i].irq,
  745. snd_sgio2_isr_table[i].isr,
  746. 0,
  747. snd_sgio2_isr_table[i].desc,
  748. &chip->channel[snd_sgio2_isr_table[i].idx])) {
  749. snd_sgio2audio_free(chip);
  750. printk(KERN_ERR "sgio2audio: cannot allocate irq %d\n",
  751. snd_sgio2_isr_table[i].irq);
  752. return -EBUSY;
  753. }
  754. }
  755. /* reset the interface */
  756. writeq(AUDIO_CONTROL_RESET, &mace->perif.audio.control);
  757. udelay(1);
  758. writeq(0, &mace->perif.audio.control);
  759. msleep_interruptible(1); /* give time to recover */
  760. /* set ring base */
  761. writeq(chip->ring_base_dma, &mace->perif.ctrl.ringbase);
  762. /* attach the AD1843 codec */
  763. chip->ad1843.read = read_ad1843_reg;
  764. chip->ad1843.write = write_ad1843_reg;
  765. chip->ad1843.chip = chip;
  766. /* initialize the AD1843 codec */
  767. err = ad1843_init(&chip->ad1843);
  768. if (err < 0) {
  769. snd_sgio2audio_free(chip);
  770. return err;
  771. }
  772. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  773. if (err < 0) {
  774. snd_sgio2audio_free(chip);
  775. return err;
  776. }
  777. *rchip = chip;
  778. return 0;
  779. }
  780. static int __devinit snd_sgio2audio_probe(struct platform_device *pdev)
  781. {
  782. struct snd_card *card;
  783. struct snd_sgio2audio *chip;
  784. int err;
  785. err = snd_card_create(index, id, THIS_MODULE, 0, &card);
  786. if (err < 0)
  787. return err;
  788. err = snd_sgio2audio_create(card, &chip);
  789. if (err < 0) {
  790. snd_card_free(card);
  791. return err;
  792. }
  793. snd_card_set_dev(card, &pdev->dev);
  794. err = snd_sgio2audio_new_pcm(chip);
  795. if (err < 0) {
  796. snd_card_free(card);
  797. return err;
  798. }
  799. err = snd_sgio2audio_new_mixer(chip);
  800. if (err < 0) {
  801. snd_card_free(card);
  802. return err;
  803. }
  804. strcpy(card->driver, "SGI O2 Audio");
  805. strcpy(card->shortname, "SGI O2 Audio");
  806. sprintf(card->longname, "%s irq %i-%i",
  807. card->shortname,
  808. MACEISA_AUDIO1_DMAT_IRQ,
  809. MACEISA_AUDIO3_MERR_IRQ);
  810. err = snd_card_register(card);
  811. if (err < 0) {
  812. snd_card_free(card);
  813. return err;
  814. }
  815. platform_set_drvdata(pdev, card);
  816. return 0;
  817. }
  818. static int __devexit snd_sgio2audio_remove(struct platform_device *pdev)
  819. {
  820. struct snd_card *card = platform_get_drvdata(pdev);
  821. snd_card_free(card);
  822. platform_set_drvdata(pdev, NULL);
  823. return 0;
  824. }
  825. static struct platform_driver sgio2audio_driver = {
  826. .probe = snd_sgio2audio_probe,
  827. .remove = __devexit_p(snd_sgio2audio_remove),
  828. .driver = {
  829. .name = "sgio2audio",
  830. .owner = THIS_MODULE,
  831. }
  832. };
  833. static int __init alsa_card_sgio2audio_init(void)
  834. {
  835. return platform_driver_register(&sgio2audio_driver);
  836. }
  837. static void __exit alsa_card_sgio2audio_exit(void)
  838. {
  839. platform_driver_unregister(&sgio2audio_driver);
  840. }
  841. module_init(alsa_card_sgio2audio_init)
  842. module_exit(alsa_card_sgio2audio_exit)