sgio2audio.c 24 KB

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