hal2.c 24 KB

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