sgio2audio.c 26 KB

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