sgio2audio.c 27 KB

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