hal2.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * Driver for A2 audio system used in SGI machines
  3. * Copyright (c) 2008 Thomas Bogendoerfer <tsbogend@alpha.fanken.de>
  4. *
  5. * Based on OSS code from Ladislav Michl <ladis@linux-mips.org>, which
  6. * was based on code from Ulf Carlsson
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/io.h>
  28. #include <asm/sgi/hpc3.h>
  29. #include <asm/sgi/ip22.h>
  30. #include <sound/core.h>
  31. #include <sound/control.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm-indirect.h>
  34. #include <sound/initval.h>
  35. #include "hal2.h"
  36. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  37. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  38. module_param(index, int, 0444);
  39. MODULE_PARM_DESC(index, "Index value for SGI HAL2 soundcard.");
  40. module_param(id, charp, 0444);
  41. MODULE_PARM_DESC(id, "ID string for SGI HAL2 soundcard.");
  42. MODULE_DESCRIPTION("ALSA driver for SGI HAL2 audio");
  43. MODULE_AUTHOR("Thomas Bogendoerfer");
  44. MODULE_LICENSE("GPL");
  45. #define H2_BLOCK_SIZE 1024
  46. #define H2_BUF_SIZE 16384
  47. struct hal2_pbus {
  48. struct hpc3_pbus_dmacregs *pbus;
  49. int pbusnr;
  50. unsigned int ctrl; /* Current state of pbus->pbdma_ctrl */
  51. };
  52. struct hal2_desc {
  53. struct hpc_dma_desc desc;
  54. u32 pad; /* padding */
  55. };
  56. struct hal2_codec {
  57. struct snd_pcm_indirect pcm_indirect;
  58. struct snd_pcm_substream *substream;
  59. unsigned char *buffer;
  60. dma_addr_t buffer_dma;
  61. struct hal2_desc *desc;
  62. dma_addr_t desc_dma;
  63. int desc_count;
  64. struct hal2_pbus pbus;
  65. int voices; /* mono/stereo */
  66. unsigned int sample_rate;
  67. unsigned int master; /* Master frequency */
  68. unsigned short mod; /* MOD value */
  69. unsigned short inc; /* INC value */
  70. };
  71. #define H2_MIX_OUTPUT_ATT 0
  72. #define H2_MIX_INPUT_GAIN 1
  73. struct snd_hal2 {
  74. struct snd_card *card;
  75. struct hal2_ctl_regs *ctl_regs; /* HAL2 ctl registers */
  76. struct hal2_aes_regs *aes_regs; /* HAL2 aes registers */
  77. struct hal2_vol_regs *vol_regs; /* HAL2 vol registers */
  78. struct hal2_syn_regs *syn_regs; /* HAL2 syn registers */
  79. struct hal2_codec dac;
  80. struct hal2_codec adc;
  81. };
  82. #define H2_INDIRECT_WAIT(regs) while (hal2_read(&regs->isr) & H2_ISR_TSTATUS);
  83. #define H2_READ_ADDR(addr) (addr | (1<<7))
  84. #define H2_WRITE_ADDR(addr) (addr)
  85. static inline u32 hal2_read(u32 *reg)
  86. {
  87. return __raw_readl(reg);
  88. }
  89. static inline void hal2_write(u32 val, u32 *reg)
  90. {
  91. __raw_writel(val, reg);
  92. }
  93. static u32 hal2_i_read32(struct snd_hal2 *hal2, u16 addr)
  94. {
  95. u32 ret;
  96. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  97. hal2_write(H2_READ_ADDR(addr), &regs->iar);
  98. H2_INDIRECT_WAIT(regs);
  99. ret = hal2_read(&regs->idr0) & 0xffff;
  100. hal2_write(H2_READ_ADDR(addr) | 0x1, &regs->iar);
  101. H2_INDIRECT_WAIT(regs);
  102. ret |= (hal2_read(&regs->idr0) & 0xffff) << 16;
  103. return ret;
  104. }
  105. static void hal2_i_write16(struct snd_hal2 *hal2, u16 addr, u16 val)
  106. {
  107. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  108. hal2_write(val, &regs->idr0);
  109. hal2_write(0, &regs->idr1);
  110. hal2_write(0, &regs->idr2);
  111. hal2_write(0, &regs->idr3);
  112. hal2_write(H2_WRITE_ADDR(addr), &regs->iar);
  113. H2_INDIRECT_WAIT(regs);
  114. }
  115. static void hal2_i_write32(struct snd_hal2 *hal2, u16 addr, u32 val)
  116. {
  117. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  118. hal2_write(val & 0xffff, &regs->idr0);
  119. hal2_write(val >> 16, &regs->idr1);
  120. hal2_write(0, &regs->idr2);
  121. hal2_write(0, &regs->idr3);
  122. hal2_write(H2_WRITE_ADDR(addr), &regs->iar);
  123. H2_INDIRECT_WAIT(regs);
  124. }
  125. static void hal2_i_setbit16(struct snd_hal2 *hal2, u16 addr, u16 bit)
  126. {
  127. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  128. hal2_write(H2_READ_ADDR(addr), &regs->iar);
  129. H2_INDIRECT_WAIT(regs);
  130. hal2_write((hal2_read(&regs->idr0) & 0xffff) | bit, &regs->idr0);
  131. hal2_write(0, &regs->idr1);
  132. hal2_write(0, &regs->idr2);
  133. hal2_write(0, &regs->idr3);
  134. hal2_write(H2_WRITE_ADDR(addr), &regs->iar);
  135. H2_INDIRECT_WAIT(regs);
  136. }
  137. static void hal2_i_clearbit16(struct snd_hal2 *hal2, u16 addr, u16 bit)
  138. {
  139. struct hal2_ctl_regs *regs = hal2->ctl_regs;
  140. hal2_write(H2_READ_ADDR(addr), &regs->iar);
  141. H2_INDIRECT_WAIT(regs);
  142. hal2_write((hal2_read(&regs->idr0) & 0xffff) & ~bit, &regs->idr0);
  143. hal2_write(0, &regs->idr1);
  144. hal2_write(0, &regs->idr2);
  145. hal2_write(0, &regs->idr3);
  146. hal2_write(H2_WRITE_ADDR(addr), &regs->iar);
  147. H2_INDIRECT_WAIT(regs);
  148. }
  149. static int hal2_gain_info(struct snd_kcontrol *kcontrol,
  150. struct snd_ctl_elem_info *uinfo)
  151. {
  152. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  153. uinfo->count = 2;
  154. uinfo->value.integer.min = 0;
  155. switch ((int)kcontrol->private_value) {
  156. case H2_MIX_OUTPUT_ATT:
  157. uinfo->value.integer.max = 31;
  158. break;
  159. case H2_MIX_INPUT_GAIN:
  160. uinfo->value.integer.max = 15;
  161. break;
  162. }
  163. return 0;
  164. }
  165. static int hal2_gain_get(struct snd_kcontrol *kcontrol,
  166. struct snd_ctl_elem_value *ucontrol)
  167. {
  168. struct snd_hal2 *hal2 = snd_kcontrol_chip(kcontrol);
  169. u32 tmp;
  170. int l, r;
  171. switch ((int)kcontrol->private_value) {
  172. case H2_MIX_OUTPUT_ATT:
  173. tmp = hal2_i_read32(hal2, H2I_DAC_C2);
  174. if (tmp & H2I_C2_MUTE) {
  175. l = 0;
  176. r = 0;
  177. } else {
  178. l = 31 - ((tmp >> H2I_C2_L_ATT_SHIFT) & 31);
  179. r = 31 - ((tmp >> H2I_C2_R_ATT_SHIFT) & 31);
  180. }
  181. break;
  182. case H2_MIX_INPUT_GAIN:
  183. tmp = hal2_i_read32(hal2, H2I_ADC_C2);
  184. l = (tmp >> H2I_C2_L_GAIN_SHIFT) & 15;
  185. r = (tmp >> H2I_C2_R_GAIN_SHIFT) & 15;
  186. break;
  187. }
  188. ucontrol->value.integer.value[0] = l;
  189. ucontrol->value.integer.value[1] = r;
  190. return 0;
  191. }
  192. static int hal2_gain_put(struct snd_kcontrol *kcontrol,
  193. struct snd_ctl_elem_value *ucontrol)
  194. {
  195. struct snd_hal2 *hal2 = snd_kcontrol_chip(kcontrol);
  196. u32 old, new;
  197. int l, r;
  198. l = ucontrol->value.integer.value[0];
  199. r = ucontrol->value.integer.value[1];
  200. switch ((int)kcontrol->private_value) {
  201. case H2_MIX_OUTPUT_ATT:
  202. old = hal2_i_read32(hal2, H2I_DAC_C2);
  203. new = old & ~(H2I_C2_L_ATT_M | H2I_C2_R_ATT_M | H2I_C2_MUTE);
  204. if (l | r) {
  205. l = 31 - l;
  206. r = 31 - r;
  207. new |= (l << H2I_C2_L_ATT_SHIFT);
  208. new |= (r << H2I_C2_R_ATT_SHIFT);
  209. } else
  210. new |= H2I_C2_L_ATT_M | H2I_C2_R_ATT_M | H2I_C2_MUTE;
  211. hal2_i_write32(hal2, H2I_DAC_C2, new);
  212. break;
  213. case H2_MIX_INPUT_GAIN:
  214. old = hal2_i_read32(hal2, H2I_ADC_C2);
  215. new = old & ~(H2I_C2_L_GAIN_M | H2I_C2_R_GAIN_M);
  216. new |= (l << H2I_C2_L_GAIN_SHIFT);
  217. new |= (r << H2I_C2_R_GAIN_SHIFT);
  218. hal2_i_write32(hal2, H2I_ADC_C2, new);
  219. break;
  220. }
  221. return old != new;
  222. }
  223. static struct snd_kcontrol_new hal2_ctrl_headphone __devinitdata = {
  224. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  225. .name = "Headphone Playback Volume",
  226. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  227. .private_value = H2_MIX_OUTPUT_ATT,
  228. .info = hal2_gain_info,
  229. .get = hal2_gain_get,
  230. .put = hal2_gain_put,
  231. };
  232. static struct snd_kcontrol_new hal2_ctrl_mic __devinitdata = {
  233. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  234. .name = "Mic Capture Volume",
  235. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  236. .private_value = H2_MIX_INPUT_GAIN,
  237. .info = hal2_gain_info,
  238. .get = hal2_gain_get,
  239. .put = hal2_gain_put,
  240. };
  241. static int __devinit hal2_mixer_create(struct snd_hal2 *hal2)
  242. {
  243. int err;
  244. /* mute DAC */
  245. hal2_i_write32(hal2, H2I_DAC_C2,
  246. H2I_C2_L_ATT_M | H2I_C2_R_ATT_M | H2I_C2_MUTE);
  247. /* mute ADC */
  248. hal2_i_write32(hal2, H2I_ADC_C2, 0);
  249. err = snd_ctl_add(hal2->card,
  250. snd_ctl_new1(&hal2_ctrl_headphone, hal2));
  251. if (err < 0)
  252. return err;
  253. err = snd_ctl_add(hal2->card,
  254. snd_ctl_new1(&hal2_ctrl_mic, hal2));
  255. if (err < 0)
  256. return err;
  257. return 0;
  258. }
  259. static irqreturn_t hal2_interrupt(int irq, void *dev_id)
  260. {
  261. struct snd_hal2 *hal2 = dev_id;
  262. irqreturn_t ret = IRQ_NONE;
  263. /* decide what caused this interrupt */
  264. if (hal2->dac.pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_INT) {
  265. snd_pcm_period_elapsed(hal2->dac.substream);
  266. ret = IRQ_HANDLED;
  267. }
  268. if (hal2->adc.pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_INT) {
  269. snd_pcm_period_elapsed(hal2->adc.substream);
  270. ret = IRQ_HANDLED;
  271. }
  272. return ret;
  273. }
  274. static int hal2_compute_rate(struct hal2_codec *codec, unsigned int rate)
  275. {
  276. unsigned short mod;
  277. if (44100 % rate < 48000 % rate) {
  278. mod = 4 * 44100 / rate;
  279. codec->master = 44100;
  280. } else {
  281. mod = 4 * 48000 / rate;
  282. codec->master = 48000;
  283. }
  284. codec->inc = 4;
  285. codec->mod = mod;
  286. rate = 4 * codec->master / mod;
  287. return rate;
  288. }
  289. static void hal2_set_dac_rate(struct snd_hal2 *hal2)
  290. {
  291. unsigned int master = hal2->dac.master;
  292. int inc = hal2->dac.inc;
  293. int mod = hal2->dac.mod;
  294. hal2_i_write16(hal2, H2I_BRES1_C1, (master == 44100) ? 1 : 0);
  295. hal2_i_write32(hal2, H2I_BRES1_C2,
  296. ((0xffff & (inc - mod - 1)) << 16) | inc);
  297. }
  298. static void hal2_set_adc_rate(struct snd_hal2 *hal2)
  299. {
  300. unsigned int master = hal2->adc.master;
  301. int inc = hal2->adc.inc;
  302. int mod = hal2->adc.mod;
  303. hal2_i_write16(hal2, H2I_BRES2_C1, (master == 44100) ? 1 : 0);
  304. hal2_i_write32(hal2, H2I_BRES2_C2,
  305. ((0xffff & (inc - mod - 1)) << 16) | inc);
  306. }
  307. static void hal2_setup_dac(struct snd_hal2 *hal2)
  308. {
  309. unsigned int fifobeg, fifoend, highwater, sample_size;
  310. struct hal2_pbus *pbus = &hal2->dac.pbus;
  311. /* Now we set up some PBUS information. The PBUS needs information about
  312. * what portion of the fifo it will use. If it's receiving or
  313. * transmitting, and finally whether the stream is little endian or big
  314. * endian. The information is written later, on the start call.
  315. */
  316. sample_size = 2 * hal2->dac.voices;
  317. /* Fifo should be set to hold exactly four samples. Highwater mark
  318. * should be set to two samples. */
  319. highwater = (sample_size * 2) >> 1; /* halfwords */
  320. fifobeg = 0; /* playback is first */
  321. fifoend = (sample_size * 4) >> 3; /* doublewords */
  322. pbus->ctrl = HPC3_PDMACTRL_RT | HPC3_PDMACTRL_LD |
  323. (highwater << 8) | (fifobeg << 16) | (fifoend << 24);
  324. /* We disable everything before we do anything at all */
  325. pbus->pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
  326. hal2_i_clearbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECTX);
  327. /* Setup the HAL2 for playback */
  328. hal2_set_dac_rate(hal2);
  329. /* Set endianess */
  330. hal2_i_clearbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECTX);
  331. /* Set DMA bus */
  332. hal2_i_setbit16(hal2, H2I_DMA_DRV, (1 << pbus->pbusnr));
  333. /* We are using 1st Bresenham clock generator for playback */
  334. hal2_i_write16(hal2, H2I_DAC_C1, (pbus->pbusnr << H2I_C1_DMA_SHIFT)
  335. | (1 << H2I_C1_CLKID_SHIFT)
  336. | (hal2->dac.voices << H2I_C1_DATAT_SHIFT));
  337. }
  338. static void hal2_setup_adc(struct snd_hal2 *hal2)
  339. {
  340. unsigned int fifobeg, fifoend, highwater, sample_size;
  341. struct hal2_pbus *pbus = &hal2->adc.pbus;
  342. sample_size = 2 * hal2->adc.voices;
  343. highwater = (sample_size * 2) >> 1; /* halfwords */
  344. fifobeg = (4 * 4) >> 3; /* record is second */
  345. fifoend = (4 * 4 + sample_size * 4) >> 3; /* doublewords */
  346. pbus->ctrl = HPC3_PDMACTRL_RT | HPC3_PDMACTRL_RCV | HPC3_PDMACTRL_LD |
  347. (highwater << 8) | (fifobeg << 16) | (fifoend << 24);
  348. pbus->pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
  349. hal2_i_clearbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECR);
  350. /* Setup the HAL2 for record */
  351. hal2_set_adc_rate(hal2);
  352. /* Set endianess */
  353. hal2_i_clearbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECR);
  354. /* Set DMA bus */
  355. hal2_i_setbit16(hal2, H2I_DMA_DRV, (1 << pbus->pbusnr));
  356. /* We are using 2nd Bresenham clock generator for record */
  357. hal2_i_write16(hal2, H2I_ADC_C1, (pbus->pbusnr << H2I_C1_DMA_SHIFT)
  358. | (2 << H2I_C1_CLKID_SHIFT)
  359. | (hal2->adc.voices << H2I_C1_DATAT_SHIFT));
  360. }
  361. static void hal2_start_dac(struct snd_hal2 *hal2)
  362. {
  363. struct hal2_pbus *pbus = &hal2->dac.pbus;
  364. pbus->pbus->pbdma_dptr = hal2->dac.desc_dma;
  365. pbus->pbus->pbdma_ctrl = pbus->ctrl | HPC3_PDMACTRL_ACT;
  366. /* enable DAC */
  367. hal2_i_setbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECTX);
  368. }
  369. static void hal2_start_adc(struct snd_hal2 *hal2)
  370. {
  371. struct hal2_pbus *pbus = &hal2->adc.pbus;
  372. pbus->pbus->pbdma_dptr = hal2->adc.desc_dma;
  373. pbus->pbus->pbdma_ctrl = pbus->ctrl | HPC3_PDMACTRL_ACT;
  374. /* enable ADC */
  375. hal2_i_setbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECR);
  376. }
  377. static inline void hal2_stop_dac(struct snd_hal2 *hal2)
  378. {
  379. hal2->dac.pbus.pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
  380. /* The HAL2 itself may remain enabled safely */
  381. }
  382. static inline void hal2_stop_adc(struct snd_hal2 *hal2)
  383. {
  384. hal2->adc.pbus.pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;
  385. }
  386. static int hal2_alloc_dmabuf(struct hal2_codec *codec)
  387. {
  388. struct hal2_desc *desc;
  389. dma_addr_t desc_dma, buffer_dma;
  390. int count = H2_BUF_SIZE / H2_BLOCK_SIZE;
  391. int i;
  392. codec->buffer = dma_alloc_noncoherent(NULL, H2_BUF_SIZE,
  393. &buffer_dma, GFP_KERNEL);
  394. if (!codec->buffer)
  395. return -ENOMEM;
  396. desc = dma_alloc_noncoherent(NULL, count * sizeof(struct hal2_desc),
  397. &desc_dma, GFP_KERNEL);
  398. if (!desc) {
  399. dma_free_noncoherent(NULL, H2_BUF_SIZE,
  400. codec->buffer, buffer_dma);
  401. return -ENOMEM;
  402. }
  403. codec->buffer_dma = buffer_dma;
  404. codec->desc_dma = desc_dma;
  405. codec->desc = desc;
  406. for (i = 0; i < count; i++) {
  407. desc->desc.pbuf = buffer_dma + i * H2_BLOCK_SIZE;
  408. desc->desc.cntinfo = HPCDMA_XIE | H2_BLOCK_SIZE;
  409. desc->desc.pnext = (i == count - 1) ?
  410. desc_dma : desc_dma + (i + 1) * sizeof(struct hal2_desc);
  411. desc++;
  412. }
  413. dma_cache_sync(NULL, codec->desc, count * sizeof(struct hal2_desc),
  414. DMA_TO_DEVICE);
  415. codec->desc_count = count;
  416. return 0;
  417. }
  418. static void hal2_free_dmabuf(struct hal2_codec *codec)
  419. {
  420. dma_free_noncoherent(NULL, codec->desc_count * sizeof(struct hal2_desc),
  421. codec->desc, codec->desc_dma);
  422. dma_free_noncoherent(NULL, H2_BUF_SIZE, codec->buffer,
  423. codec->buffer_dma);
  424. }
  425. static struct snd_pcm_hardware hal2_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 = 1024,
  438. .period_bytes_max = 65536,
  439. .periods_min = 2,
  440. .periods_max = 1024,
  441. };
  442. static int hal2_pcm_hw_params(struct snd_pcm_substream *substream,
  443. struct snd_pcm_hw_params *params)
  444. {
  445. int err;
  446. err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  447. if (err < 0)
  448. return err;
  449. return 0;
  450. }
  451. static int hal2_pcm_hw_free(struct snd_pcm_substream *substream)
  452. {
  453. return snd_pcm_lib_free_pages(substream);
  454. }
  455. static int hal2_playback_open(struct snd_pcm_substream *substream)
  456. {
  457. struct snd_pcm_runtime *runtime = substream->runtime;
  458. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  459. int err;
  460. runtime->hw = hal2_pcm_hw;
  461. err = hal2_alloc_dmabuf(&hal2->dac);
  462. if (err)
  463. return err;
  464. return 0;
  465. }
  466. static int hal2_playback_close(struct snd_pcm_substream *substream)
  467. {
  468. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  469. hal2_free_dmabuf(&hal2->dac);
  470. return 0;
  471. }
  472. static int hal2_playback_prepare(struct snd_pcm_substream *substream)
  473. {
  474. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  475. struct snd_pcm_runtime *runtime = substream->runtime;
  476. struct hal2_codec *dac = &hal2->dac;
  477. dac->voices = runtime->channels;
  478. dac->sample_rate = hal2_compute_rate(dac, runtime->rate);
  479. memset(&dac->pcm_indirect, 0, sizeof(dac->pcm_indirect));
  480. dac->pcm_indirect.hw_buffer_size = H2_BUF_SIZE;
  481. dac->pcm_indirect.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
  482. dac->substream = substream;
  483. hal2_setup_dac(hal2);
  484. return 0;
  485. }
  486. static int hal2_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  487. {
  488. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  489. switch (cmd) {
  490. case SNDRV_PCM_TRIGGER_START:
  491. hal2->dac.pcm_indirect.hw_io = hal2->dac.buffer_dma;
  492. hal2->dac.pcm_indirect.hw_data = 0;
  493. substream->ops->ack(substream);
  494. hal2_start_dac(hal2);
  495. break;
  496. case SNDRV_PCM_TRIGGER_STOP:
  497. hal2_stop_dac(hal2);
  498. break;
  499. default:
  500. return -EINVAL;
  501. }
  502. return 0;
  503. }
  504. static snd_pcm_uframes_t
  505. hal2_playback_pointer(struct snd_pcm_substream *substream)
  506. {
  507. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  508. struct hal2_codec *dac = &hal2->dac;
  509. return snd_pcm_indirect_playback_pointer(substream, &dac->pcm_indirect,
  510. dac->pbus.pbus->pbdma_bptr);
  511. }
  512. static void hal2_playback_transfer(struct snd_pcm_substream *substream,
  513. struct snd_pcm_indirect *rec, size_t bytes)
  514. {
  515. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  516. unsigned char *buf = hal2->dac.buffer + rec->hw_data;
  517. memcpy(buf, substream->runtime->dma_area + rec->sw_data, bytes);
  518. dma_cache_sync(NULL, buf, bytes, DMA_TO_DEVICE);
  519. }
  520. static int hal2_playback_ack(struct snd_pcm_substream *substream)
  521. {
  522. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  523. struct hal2_codec *dac = &hal2->dac;
  524. dac->pcm_indirect.hw_queue_size = H2_BUF_SIZE / 2;
  525. snd_pcm_indirect_playback_transfer(substream,
  526. &dac->pcm_indirect,
  527. hal2_playback_transfer);
  528. return 0;
  529. }
  530. static int hal2_capture_open(struct snd_pcm_substream *substream)
  531. {
  532. struct snd_pcm_runtime *runtime = substream->runtime;
  533. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  534. struct hal2_codec *adc = &hal2->adc;
  535. int err;
  536. runtime->hw = hal2_pcm_hw;
  537. err = hal2_alloc_dmabuf(adc);
  538. if (err)
  539. return err;
  540. return 0;
  541. }
  542. static int hal2_capture_close(struct snd_pcm_substream *substream)
  543. {
  544. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  545. hal2_free_dmabuf(&hal2->adc);
  546. return 0;
  547. }
  548. static int hal2_capture_prepare(struct snd_pcm_substream *substream)
  549. {
  550. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  551. struct snd_pcm_runtime *runtime = substream->runtime;
  552. struct hal2_codec *adc = &hal2->adc;
  553. adc->voices = runtime->channels;
  554. adc->sample_rate = hal2_compute_rate(adc, runtime->rate);
  555. memset(&adc->pcm_indirect, 0, sizeof(adc->pcm_indirect));
  556. adc->pcm_indirect.hw_buffer_size = H2_BUF_SIZE;
  557. adc->pcm_indirect.hw_queue_size = H2_BUF_SIZE / 2;
  558. adc->pcm_indirect.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
  559. adc->substream = substream;
  560. hal2_setup_adc(hal2);
  561. return 0;
  562. }
  563. static int hal2_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  564. {
  565. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  566. switch (cmd) {
  567. case SNDRV_PCM_TRIGGER_START:
  568. hal2->adc.pcm_indirect.hw_io = hal2->adc.buffer_dma;
  569. hal2->adc.pcm_indirect.hw_data = 0;
  570. printk(KERN_DEBUG "buffer_dma %x\n", hal2->adc.buffer_dma);
  571. hal2_start_adc(hal2);
  572. break;
  573. case SNDRV_PCM_TRIGGER_STOP:
  574. hal2_stop_adc(hal2);
  575. break;
  576. default:
  577. return -EINVAL;
  578. }
  579. return 0;
  580. }
  581. static snd_pcm_uframes_t
  582. hal2_capture_pointer(struct snd_pcm_substream *substream)
  583. {
  584. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  585. struct hal2_codec *adc = &hal2->adc;
  586. return snd_pcm_indirect_capture_pointer(substream, &adc->pcm_indirect,
  587. adc->pbus.pbus->pbdma_bptr);
  588. }
  589. static void hal2_capture_transfer(struct snd_pcm_substream *substream,
  590. struct snd_pcm_indirect *rec, size_t bytes)
  591. {
  592. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  593. unsigned char *buf = hal2->adc.buffer + rec->hw_data;
  594. dma_cache_sync(NULL, buf, bytes, DMA_FROM_DEVICE);
  595. memcpy(substream->runtime->dma_area + rec->sw_data, buf, bytes);
  596. }
  597. static int hal2_capture_ack(struct snd_pcm_substream *substream)
  598. {
  599. struct snd_hal2 *hal2 = snd_pcm_substream_chip(substream);
  600. struct hal2_codec *adc = &hal2->adc;
  601. snd_pcm_indirect_capture_transfer(substream,
  602. &adc->pcm_indirect,
  603. hal2_capture_transfer);
  604. return 0;
  605. }
  606. static struct snd_pcm_ops hal2_playback_ops = {
  607. .open = hal2_playback_open,
  608. .close = hal2_playback_close,
  609. .ioctl = snd_pcm_lib_ioctl,
  610. .hw_params = hal2_pcm_hw_params,
  611. .hw_free = hal2_pcm_hw_free,
  612. .prepare = hal2_playback_prepare,
  613. .trigger = hal2_playback_trigger,
  614. .pointer = hal2_playback_pointer,
  615. .ack = hal2_playback_ack,
  616. };
  617. static struct snd_pcm_ops hal2_capture_ops = {
  618. .open = hal2_capture_open,
  619. .close = hal2_capture_close,
  620. .ioctl = snd_pcm_lib_ioctl,
  621. .hw_params = hal2_pcm_hw_params,
  622. .hw_free = hal2_pcm_hw_free,
  623. .prepare = hal2_capture_prepare,
  624. .trigger = hal2_capture_trigger,
  625. .pointer = hal2_capture_pointer,
  626. .ack = hal2_capture_ack,
  627. };
  628. static int __devinit hal2_pcm_create(struct snd_hal2 *hal2)
  629. {
  630. struct snd_pcm *pcm;
  631. int err;
  632. /* create first pcm device with one outputs and one input */
  633. err = snd_pcm_new(hal2->card, "SGI HAL2 Audio", 0, 1, 1, &pcm);
  634. if (err < 0)
  635. return err;
  636. pcm->private_data = hal2;
  637. strcpy(pcm->name, "SGI HAL2");
  638. /* set operators */
  639. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  640. &hal2_playback_ops);
  641. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  642. &hal2_capture_ops);
  643. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  644. snd_dma_continuous_data(GFP_KERNEL),
  645. 0, 1024 * 1024);
  646. return 0;
  647. }
  648. static int hal2_dev_free(struct snd_device *device)
  649. {
  650. struct snd_hal2 *hal2 = device->device_data;
  651. free_irq(SGI_HPCDMA_IRQ, hal2);
  652. kfree(hal2);
  653. return 0;
  654. }
  655. static struct snd_device_ops hal2_ops = {
  656. .dev_free = hal2_dev_free,
  657. };
  658. static void hal2_init_codec(struct hal2_codec *codec, struct hpc3_regs *hpc3,
  659. int index)
  660. {
  661. codec->pbus.pbusnr = index;
  662. codec->pbus.pbus = &hpc3->pbdma[index];
  663. }
  664. static int hal2_detect(struct snd_hal2 *hal2)
  665. {
  666. unsigned short board, major, minor;
  667. unsigned short rev;
  668. /* reset HAL2 */
  669. hal2_write(0, &hal2->ctl_regs->isr);
  670. /* release reset */
  671. hal2_write(H2_ISR_GLOBAL_RESET_N | H2_ISR_CODEC_RESET_N,
  672. &hal2->ctl_regs->isr);
  673. hal2_i_write16(hal2, H2I_RELAY_C, H2I_RELAY_C_STATE);
  674. rev = hal2_read(&hal2->ctl_regs->rev);
  675. if (rev & H2_REV_AUDIO_PRESENT)
  676. return -ENODEV;
  677. board = (rev & H2_REV_BOARD_M) >> 12;
  678. major = (rev & H2_REV_MAJOR_CHIP_M) >> 4;
  679. minor = (rev & H2_REV_MINOR_CHIP_M);
  680. printk(KERN_INFO "SGI HAL2 revision %i.%i.%i\n",
  681. board, major, minor);
  682. return 0;
  683. }
  684. static int hal2_create(struct snd_card *card, struct snd_hal2 **rchip)
  685. {
  686. struct snd_hal2 *hal2;
  687. struct hpc3_regs *hpc3 = hpc3c0;
  688. int err;
  689. hal2 = kzalloc(sizeof(struct snd_hal2), GFP_KERNEL);
  690. if (!hal2)
  691. return -ENOMEM;
  692. hal2->card = card;
  693. if (request_irq(SGI_HPCDMA_IRQ, hal2_interrupt, IRQF_SHARED,
  694. "SGI HAL2", hal2)) {
  695. printk(KERN_ERR "HAL2: Can't get irq %d\n", SGI_HPCDMA_IRQ);
  696. kfree(hal2);
  697. return -EAGAIN;
  698. }
  699. hal2->ctl_regs = (struct hal2_ctl_regs *)hpc3->pbus_extregs[0];
  700. hal2->aes_regs = (struct hal2_aes_regs *)hpc3->pbus_extregs[1];
  701. hal2->vol_regs = (struct hal2_vol_regs *)hpc3->pbus_extregs[2];
  702. hal2->syn_regs = (struct hal2_syn_regs *)hpc3->pbus_extregs[3];
  703. if (hal2_detect(hal2) < 0) {
  704. kfree(hal2);
  705. return -ENODEV;
  706. }
  707. hal2_init_codec(&hal2->dac, hpc3, 0);
  708. hal2_init_codec(&hal2->adc, hpc3, 1);
  709. /*
  710. * All DMA channel interfaces in HAL2 are designed to operate with
  711. * PBUS programmed for 2 cycles in D3, 2 cycles in D4 and 2 cycles
  712. * in D5. HAL2 is a 16-bit device which can accept both big and little
  713. * endian format. It assumes that even address bytes are on high
  714. * portion of PBUS (15:8) and assumes that HPC3 is programmed to
  715. * accept a live (unsynchronized) version of P_DREQ_N from HAL2.
  716. */
  717. #define HAL2_PBUS_DMACFG ((0 << HPC3_DMACFG_D3R_SHIFT) | \
  718. (2 << HPC3_DMACFG_D4R_SHIFT) | \
  719. (2 << HPC3_DMACFG_D5R_SHIFT) | \
  720. (0 << HPC3_DMACFG_D3W_SHIFT) | \
  721. (2 << HPC3_DMACFG_D4W_SHIFT) | \
  722. (2 << HPC3_DMACFG_D5W_SHIFT) | \
  723. HPC3_DMACFG_DS16 | \
  724. HPC3_DMACFG_EVENHI | \
  725. HPC3_DMACFG_RTIME | \
  726. (8 << HPC3_DMACFG_BURST_SHIFT) | \
  727. HPC3_DMACFG_DRQLIVE)
  728. /*
  729. * Ignore what's mentioned in the specification and write value which
  730. * works in The Real World (TM)
  731. */
  732. hpc3->pbus_dmacfg[hal2->dac.pbus.pbusnr][0] = 0x8208844;
  733. hpc3->pbus_dmacfg[hal2->adc.pbus.pbusnr][0] = 0x8208844;
  734. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, hal2, &hal2_ops);
  735. if (err < 0) {
  736. free_irq(SGI_HPCDMA_IRQ, hal2);
  737. kfree(hal2);
  738. return err;
  739. }
  740. *rchip = hal2;
  741. return 0;
  742. }
  743. static int __devinit hal2_probe(struct platform_device *pdev)
  744. {
  745. struct snd_card *card;
  746. struct snd_hal2 *chip;
  747. int err;
  748. err = snd_card_create(index, id, THIS_MODULE, 0, &card);
  749. if (err < 0)
  750. return err;
  751. err = hal2_create(card, &chip);
  752. if (err < 0) {
  753. snd_card_free(card);
  754. return err;
  755. }
  756. snd_card_set_dev(card, &pdev->dev);
  757. err = hal2_pcm_create(chip);
  758. if (err < 0) {
  759. snd_card_free(card);
  760. return err;
  761. }
  762. err = hal2_mixer_create(chip);
  763. if (err < 0) {
  764. snd_card_free(card);
  765. return err;
  766. }
  767. strcpy(card->driver, "SGI HAL2 Audio");
  768. strcpy(card->shortname, "SGI HAL2 Audio");
  769. sprintf(card->longname, "%s irq %i",
  770. card->shortname,
  771. SGI_HPCDMA_IRQ);
  772. err = snd_card_register(card);
  773. if (err < 0) {
  774. snd_card_free(card);
  775. return err;
  776. }
  777. platform_set_drvdata(pdev, card);
  778. return 0;
  779. }
  780. static int __exit hal2_remove(struct platform_device *pdev)
  781. {
  782. struct snd_card *card = platform_get_drvdata(pdev);
  783. snd_card_free(card);
  784. platform_set_drvdata(pdev, NULL);
  785. return 0;
  786. }
  787. static struct platform_driver hal2_driver = {
  788. .probe = hal2_probe,
  789. .remove = __devexit_p(hal2_remove),
  790. .driver = {
  791. .name = "sgihal2",
  792. .owner = THIS_MODULE,
  793. }
  794. };
  795. static int __init alsa_card_hal2_init(void)
  796. {
  797. return platform_driver_register(&hal2_driver);
  798. }
  799. static void __exit alsa_card_hal2_exit(void)
  800. {
  801. platform_driver_unregister(&hal2_driver);
  802. }
  803. module_init(alsa_card_hal2_init);
  804. module_exit(alsa_card_hal2_exit);