hal2.c 25 KB

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