sgio2audio.c 25 KB

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