harmony.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /* Hewlett-Packard Harmony audio driver
  2. *
  3. * This is a driver for the Harmony audio chipset found
  4. * on the LASI ASIC of various early HP PA-RISC workstations.
  5. *
  6. * Copyright (C) 2004, Kyle McMartin <kyle@{debian.org,parisc-linux.org}>
  7. *
  8. * Based on the previous Harmony incarnations by,
  9. * Copyright 2000 (c) Linuxcare Canada, Alex deVries
  10. * Copyright 2000-2003 (c) Helge Deller
  11. * Copyright 2001 (c) Matthieu Delahaye
  12. * Copyright 2001 (c) Jean-Christophe Vaugeois
  13. * Copyright 2003 (c) Laurent Canet
  14. * Copyright 2004 (c) Stuart Brady
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License, version 2, as
  18. * published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. *
  29. * Notes:
  30. * - graveyard and silence buffers last for lifetime of
  31. * the driver. playback and capture buffers are allocated
  32. * per _open()/_close().
  33. *
  34. * TODO:
  35. *
  36. */
  37. #include <linux/init.h>
  38. #include <linux/slab.h>
  39. #include <linux/time.h>
  40. #include <linux/wait.h>
  41. #include <linux/delay.h>
  42. #include <linux/module.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/dma-mapping.h>
  46. #include <sound/driver.h>
  47. #include <sound/core.h>
  48. #include <sound/pcm.h>
  49. #include <sound/control.h>
  50. #include <sound/rawmidi.h>
  51. #include <sound/initval.h>
  52. #include <sound/info.h>
  53. #include <asm/io.h>
  54. #include <asm/hardware.h>
  55. #include <asm/parisc-device.h>
  56. #include "harmony.h"
  57. static struct parisc_device_id snd_harmony_devtable[] = {
  58. /* bushmaster / flounder */
  59. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007A },
  60. /* 712 / 715 */
  61. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007B },
  62. /* pace */
  63. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007E },
  64. /* outfield / coral II */
  65. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007F },
  66. { 0, }
  67. };
  68. MODULE_DEVICE_TABLE(parisc, snd_harmony_devtable);
  69. #define NAME "harmony"
  70. #define PFX NAME ": "
  71. static unsigned int snd_harmony_rates[] = {
  72. 5512, 6615, 8000, 9600,
  73. 11025, 16000, 18900, 22050,
  74. 27428, 32000, 33075, 37800,
  75. 44100, 48000
  76. };
  77. static unsigned int rate_bits[14] = {
  78. HARMONY_SR_5KHZ, HARMONY_SR_6KHZ, HARMONY_SR_8KHZ,
  79. HARMONY_SR_9KHZ, HARMONY_SR_11KHZ, HARMONY_SR_16KHZ,
  80. HARMONY_SR_18KHZ, HARMONY_SR_22KHZ, HARMONY_SR_27KHZ,
  81. HARMONY_SR_32KHZ, HARMONY_SR_33KHZ, HARMONY_SR_37KHZ,
  82. HARMONY_SR_44KHZ, HARMONY_SR_48KHZ
  83. };
  84. static snd_pcm_hw_constraint_list_t hw_constraint_rates = {
  85. .count = ARRAY_SIZE(snd_harmony_rates),
  86. .list = snd_harmony_rates,
  87. .mask = 0,
  88. };
  89. inline unsigned long
  90. harmony_read(harmony_t *h, unsigned r)
  91. {
  92. return __raw_readl(h->iobase + r);
  93. }
  94. inline void
  95. harmony_write(harmony_t *h, unsigned r, unsigned long v)
  96. {
  97. __raw_writel(v, h->iobase + r);
  98. }
  99. static void
  100. harmony_wait_for_control(harmony_t *h)
  101. {
  102. while (harmony_read(h, HARMONY_CNTL) & HARMONY_CNTL_C) ;
  103. }
  104. inline void
  105. harmony_reset(harmony_t *h)
  106. {
  107. harmony_write(h, HARMONY_RESET, 1);
  108. mdelay(50);
  109. harmony_write(h, HARMONY_RESET, 0);
  110. }
  111. static void
  112. harmony_disable_interrupts(harmony_t *h)
  113. {
  114. u32 dstatus;
  115. harmony_wait_for_control(h);
  116. dstatus = harmony_read(h, HARMONY_DSTATUS);
  117. dstatus &= ~HARMONY_DSTATUS_IE;
  118. harmony_write(h, HARMONY_DSTATUS, dstatus);
  119. }
  120. static void
  121. harmony_enable_interrupts(harmony_t *h)
  122. {
  123. u32 dstatus;
  124. harmony_wait_for_control(h);
  125. dstatus = harmony_read(h, HARMONY_DSTATUS);
  126. dstatus |= HARMONY_DSTATUS_IE;
  127. harmony_write(h, HARMONY_DSTATUS, dstatus);
  128. }
  129. static void
  130. harmony_mute(harmony_t *h)
  131. {
  132. unsigned long flags;
  133. spin_lock_irqsave(&h->mixer_lock, flags);
  134. harmony_wait_for_control(h);
  135. harmony_write(h, HARMONY_GAINCTL, HARMONY_GAIN_SILENCE);
  136. spin_unlock_irqrestore(&h->mixer_lock, flags);
  137. }
  138. static void
  139. harmony_unmute(harmony_t *h)
  140. {
  141. unsigned long flags;
  142. spin_lock_irqsave(&h->mixer_lock, flags);
  143. harmony_wait_for_control(h);
  144. harmony_write(h, HARMONY_GAINCTL, h->st.gain);
  145. spin_unlock_irqrestore(&h->mixer_lock, flags);
  146. }
  147. static void
  148. harmony_set_control(harmony_t *h)
  149. {
  150. u32 ctrl;
  151. unsigned long flags;
  152. spin_lock_irqsave(&h->lock, flags);
  153. ctrl = (HARMONY_CNTL_C |
  154. (h->st.format << 6) |
  155. (h->st.stereo << 5) |
  156. (h->st.rate));
  157. harmony_wait_for_control(h);
  158. harmony_write(h, HARMONY_CNTL, ctrl);
  159. spin_unlock_irqrestore(&h->lock, flags);
  160. }
  161. static irqreturn_t
  162. snd_harmony_interrupt(int irq, void *dev, struct pt_regs *regs)
  163. {
  164. u32 dstatus;
  165. harmony_t *h = dev;
  166. spin_lock(&h->lock);
  167. harmony_disable_interrupts(h);
  168. harmony_wait_for_control(h);
  169. dstatus = harmony_read(h, HARMONY_DSTATUS);
  170. spin_unlock(&h->lock);
  171. if (dstatus & HARMONY_DSTATUS_PN) {
  172. if (h->psubs && h->st.playing) {
  173. spin_lock(&h->lock);
  174. h->pbuf.buf += h->pbuf.count; /* PAGE_SIZE */
  175. h->pbuf.buf %= h->pbuf.size; /* MAX_BUFS*PAGE_SIZE */
  176. harmony_write(h, HARMONY_PNXTADD,
  177. h->pbuf.addr + h->pbuf.buf);
  178. h->stats.play_intr++;
  179. spin_unlock(&h->lock);
  180. snd_pcm_period_elapsed(h->psubs);
  181. } else {
  182. spin_lock(&h->lock);
  183. harmony_write(h, HARMONY_PNXTADD, h->sdma.addr);
  184. h->stats.silence_intr++;
  185. spin_unlock(&h->lock);
  186. }
  187. }
  188. if (dstatus & HARMONY_DSTATUS_RN) {
  189. if (h->csubs && h->st.capturing) {
  190. spin_lock(&h->lock);
  191. h->cbuf.buf += h->cbuf.count;
  192. h->cbuf.buf %= h->cbuf.size;
  193. harmony_write(h, HARMONY_RNXTADD,
  194. h->cbuf.addr + h->cbuf.buf);
  195. h->stats.rec_intr++;
  196. spin_unlock(&h->lock);
  197. snd_pcm_period_elapsed(h->csubs);
  198. } else {
  199. spin_lock(&h->lock);
  200. harmony_write(h, HARMONY_RNXTADD, h->gdma.addr);
  201. h->stats.graveyard_intr++;
  202. spin_unlock(&h->lock);
  203. }
  204. }
  205. spin_lock(&h->lock);
  206. harmony_enable_interrupts(h);
  207. spin_unlock(&h->lock);
  208. return IRQ_HANDLED;
  209. }
  210. static unsigned int
  211. snd_harmony_rate_bits(int rate)
  212. {
  213. unsigned int i;
  214. for (i = 0; i < ARRAY_SIZE(snd_harmony_rates); i++)
  215. if (snd_harmony_rates[i] == rate)
  216. return rate_bits[i];
  217. return HARMONY_SR_44KHZ;
  218. }
  219. static snd_pcm_hardware_t snd_harmony_playback =
  220. {
  221. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  222. SNDRV_PCM_INFO_JOINT_DUPLEX | SNDRV_PCM_INFO_MMAP_VALID |
  223. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  224. .formats = (SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_MU_LAW |
  225. SNDRV_PCM_FMTBIT_A_LAW),
  226. .rates = (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000 |
  227. SNDRV_PCM_RATE_KNOT),
  228. .rate_min = 5512,
  229. .rate_max = 48000,
  230. .channels_min = 1,
  231. .channels_max = 2,
  232. .buffer_bytes_max = MAX_BUF_SIZE,
  233. .period_bytes_min = BUF_SIZE,
  234. .period_bytes_max = BUF_SIZE,
  235. .periods_min = 1,
  236. .periods_max = MAX_BUFS,
  237. .fifo_size = 0,
  238. };
  239. static snd_pcm_hardware_t snd_harmony_capture =
  240. {
  241. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  242. SNDRV_PCM_INFO_JOINT_DUPLEX | SNDRV_PCM_INFO_MMAP_VALID |
  243. SNDRV_PCM_INFO_BLOCK_TRANSFER),
  244. .formats = (SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_MU_LAW |
  245. SNDRV_PCM_FMTBIT_A_LAW),
  246. .rates = (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000 |
  247. SNDRV_PCM_RATE_KNOT),
  248. .rate_min = 5512,
  249. .rate_max = 48000,
  250. .channels_min = 1,
  251. .channels_max = 2,
  252. .buffer_bytes_max = MAX_BUF_SIZE,
  253. .period_bytes_min = BUF_SIZE,
  254. .period_bytes_max = BUF_SIZE,
  255. .periods_min = 1,
  256. .periods_max = MAX_BUFS,
  257. .fifo_size = 0,
  258. };
  259. static int
  260. snd_harmony_playback_trigger(snd_pcm_substream_t *ss, int cmd)
  261. {
  262. harmony_t *h = snd_pcm_substream_chip(ss);
  263. unsigned long flags;
  264. if (h->st.capturing)
  265. return -EBUSY;
  266. spin_lock_irqsave(&h->lock, flags);
  267. switch (cmd) {
  268. case SNDRV_PCM_TRIGGER_START:
  269. h->st.playing = 1;
  270. harmony_write(h, HARMONY_PNXTADD, h->pbuf.addr);
  271. harmony_write(h, HARMONY_RNXTADD, h->gdma.addr);
  272. harmony_unmute(h);
  273. harmony_enable_interrupts(h);
  274. break;
  275. case SNDRV_PCM_TRIGGER_STOP:
  276. h->st.playing = 0;
  277. harmony_mute(h);
  278. harmony_write(h, HARMONY_PNXTADD, h->sdma.addr);
  279. harmony_disable_interrupts(h);
  280. break;
  281. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  282. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  283. case SNDRV_PCM_TRIGGER_SUSPEND:
  284. default:
  285. spin_unlock_irqrestore(&h->lock, flags);
  286. snd_BUG();
  287. return -EINVAL;
  288. }
  289. spin_unlock_irqrestore(&h->lock, flags);
  290. return 0;
  291. }
  292. static int
  293. snd_harmony_capture_trigger(snd_pcm_substream_t *ss, int cmd)
  294. {
  295. harmony_t *h = snd_pcm_substream_chip(ss);
  296. unsigned long flags;
  297. if (h->st.playing)
  298. return -EBUSY;
  299. spin_lock_irqsave(&h->lock, flags);
  300. switch (cmd) {
  301. case SNDRV_PCM_TRIGGER_START:
  302. h->st.capturing = 1;
  303. harmony_write(h, HARMONY_PNXTADD, h->sdma.addr);
  304. harmony_write(h, HARMONY_RNXTADD, h->cbuf.addr);
  305. harmony_unmute(h);
  306. harmony_enable_interrupts(h);
  307. break;
  308. case SNDRV_PCM_TRIGGER_STOP:
  309. h->st.capturing = 0;
  310. harmony_mute(h);
  311. harmony_write(h, HARMONY_RNXTADD, h->gdma.addr);
  312. harmony_disable_interrupts(h);
  313. break;
  314. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  315. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  316. case SNDRV_PCM_TRIGGER_SUSPEND:
  317. default:
  318. spin_unlock_irqrestore(&h->lock, flags);
  319. snd_BUG();
  320. return -EINVAL;
  321. }
  322. spin_unlock_irqrestore(&h->lock, flags);
  323. return 0;
  324. }
  325. static int
  326. snd_harmony_set_data_format(harmony_t *h, int fmt, int force)
  327. {
  328. int o = h->st.format;
  329. int n;
  330. switch(fmt) {
  331. case SNDRV_PCM_FORMAT_S16_BE:
  332. n = HARMONY_DF_16BIT_LINEAR;
  333. break;
  334. case SNDRV_PCM_FORMAT_A_LAW:
  335. n = HARMONY_DF_8BIT_ALAW;
  336. break;
  337. case SNDRV_PCM_FORMAT_MU_LAW:
  338. n = HARMONY_DF_8BIT_ULAW;
  339. break;
  340. default:
  341. n = HARMONY_DF_16BIT_LINEAR;
  342. break;
  343. }
  344. if (force || o != n) {
  345. snd_pcm_format_set_silence(fmt, h->sdma.area, SILENCE_BUFSZ /
  346. (snd_pcm_format_physical_width(fmt)
  347. / 8));
  348. }
  349. return n;
  350. }
  351. static int
  352. snd_harmony_playback_prepare(snd_pcm_substream_t *ss)
  353. {
  354. harmony_t *h = snd_pcm_substream_chip(ss);
  355. snd_pcm_runtime_t *rt = ss->runtime;
  356. if (h->st.capturing)
  357. return -EBUSY;
  358. h->pbuf.size = snd_pcm_lib_buffer_bytes(ss);
  359. h->pbuf.count = snd_pcm_lib_period_bytes(ss);
  360. if (h->pbuf.buf >= h->pbuf.size)
  361. h->pbuf.buf = 0;
  362. h->st.playing = 0;
  363. h->st.rate = snd_harmony_rate_bits(rt->rate);
  364. h->st.format = snd_harmony_set_data_format(h, rt->format, 0);
  365. if (rt->channels == 2)
  366. h->st.stereo = HARMONY_SS_STEREO;
  367. else
  368. h->st.stereo = HARMONY_SS_MONO;
  369. harmony_set_control(h);
  370. h->pbuf.addr = rt->dma_addr;
  371. return 0;
  372. }
  373. static int
  374. snd_harmony_capture_prepare(snd_pcm_substream_t *ss)
  375. {
  376. harmony_t *h = snd_pcm_substream_chip(ss);
  377. snd_pcm_runtime_t *rt = ss->runtime;
  378. if (h->st.playing)
  379. return -EBUSY;
  380. h->cbuf.size = snd_pcm_lib_buffer_bytes(ss);
  381. h->cbuf.count = snd_pcm_lib_period_bytes(ss);
  382. if (h->cbuf.buf >= h->cbuf.size)
  383. h->cbuf.buf = 0;
  384. h->st.capturing = 0;
  385. h->st.rate = snd_harmony_rate_bits(rt->rate);
  386. h->st.format = snd_harmony_set_data_format(h, rt->format, 0);
  387. if (rt->channels == 2)
  388. h->st.stereo = HARMONY_SS_STEREO;
  389. else
  390. h->st.stereo = HARMONY_SS_MONO;
  391. harmony_set_control(h);
  392. h->cbuf.addr = rt->dma_addr;
  393. return 0;
  394. }
  395. static snd_pcm_uframes_t
  396. snd_harmony_playback_pointer(snd_pcm_substream_t *ss)
  397. {
  398. snd_pcm_runtime_t *rt = ss->runtime;
  399. harmony_t *h = snd_pcm_substream_chip(ss);
  400. unsigned long pcuradd;
  401. unsigned long played;
  402. if (!(h->st.playing) || (h->psubs == NULL))
  403. return 0;
  404. if ((h->pbuf.addr == 0) || (h->pbuf.size == 0))
  405. return 0;
  406. pcuradd = harmony_read(h, HARMONY_PCURADD);
  407. played = pcuradd - h->pbuf.addr;
  408. #ifdef HARMONY_DEBUG
  409. printk(KERN_DEBUG PFX "playback_pointer is 0x%lx-0x%lx = %d bytes\n",
  410. pcuradd, h->pbuf.addr, played);
  411. #endif
  412. if (pcuradd > h->pbuf.addr + h->pbuf.size) {
  413. return 0;
  414. }
  415. return bytes_to_frames(rt, played);
  416. }
  417. static snd_pcm_uframes_t
  418. snd_harmony_capture_pointer(snd_pcm_substream_t *ss)
  419. {
  420. snd_pcm_runtime_t *rt = ss->runtime;
  421. harmony_t *h = snd_pcm_substream_chip(ss);
  422. unsigned long rcuradd;
  423. unsigned long caught;
  424. if (!(h->st.capturing) || (h->csubs == NULL))
  425. return 0;
  426. if ((h->cbuf.addr == 0) || (h->cbuf.size == 0))
  427. return 0;
  428. rcuradd = harmony_read(h, HARMONY_RCURADD);
  429. caught = rcuradd - h->cbuf.addr;
  430. #ifdef HARMONY_DEBUG
  431. printk(KERN_DEBUG PFX "capture_pointer is 0x%lx-0x%lx = %d bytes\n",
  432. rcuradd, h->cbuf.addr, caught);
  433. #endif
  434. if (rcuradd > h->cbuf.addr + h->cbuf.size) {
  435. return 0;
  436. }
  437. return bytes_to_frames(rt, caught);
  438. }
  439. static int
  440. snd_harmony_playback_open(snd_pcm_substream_t *ss)
  441. {
  442. harmony_t *h = snd_pcm_substream_chip(ss);
  443. snd_pcm_runtime_t *rt = ss->runtime;
  444. int err;
  445. h->psubs = ss;
  446. rt->hw = snd_harmony_playback;
  447. snd_pcm_hw_constraint_list(rt, 0, SNDRV_PCM_HW_PARAM_RATE,
  448. &hw_constraint_rates);
  449. err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
  450. if (err < 0)
  451. return err;
  452. return 0;
  453. }
  454. static int
  455. snd_harmony_capture_open(snd_pcm_substream_t *ss)
  456. {
  457. harmony_t *h = snd_pcm_substream_chip(ss);
  458. snd_pcm_runtime_t *rt = ss->runtime;
  459. int err;
  460. h->csubs = ss;
  461. rt->hw = snd_harmony_capture;
  462. snd_pcm_hw_constraint_list(rt, 0, SNDRV_PCM_HW_PARAM_RATE,
  463. &hw_constraint_rates);
  464. err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
  465. if (err < 0)
  466. return err;
  467. return 0;
  468. }
  469. static int
  470. snd_harmony_playback_close(snd_pcm_substream_t *ss)
  471. {
  472. harmony_t *h = snd_pcm_substream_chip(ss);
  473. h->psubs = NULL;
  474. return 0;
  475. }
  476. static int
  477. snd_harmony_capture_close(snd_pcm_substream_t *ss)
  478. {
  479. harmony_t *h = snd_pcm_substream_chip(ss);
  480. h->csubs = NULL;
  481. return 0;
  482. }
  483. static int
  484. snd_harmony_hw_params(snd_pcm_substream_t *ss,
  485. snd_pcm_hw_params_t *hw)
  486. {
  487. int err;
  488. harmony_t *h = snd_pcm_substream_chip(ss);
  489. err = snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw));
  490. if (err > 0 && h->dma.type == SNDRV_DMA_TYPE_CONTINUOUS)
  491. ss->runtime->dma_addr = __pa(ss->runtime->dma_area);
  492. return err;
  493. }
  494. static int
  495. snd_harmony_hw_free(snd_pcm_substream_t *ss)
  496. {
  497. return snd_pcm_lib_free_pages(ss);
  498. }
  499. static snd_pcm_ops_t snd_harmony_playback_ops = {
  500. .open = snd_harmony_playback_open,
  501. .close = snd_harmony_playback_close,
  502. .ioctl = snd_pcm_lib_ioctl,
  503. .hw_params = snd_harmony_hw_params,
  504. .hw_free = snd_harmony_hw_free,
  505. .prepare = snd_harmony_playback_prepare,
  506. .trigger = snd_harmony_playback_trigger,
  507. .pointer = snd_harmony_playback_pointer,
  508. };
  509. static snd_pcm_ops_t snd_harmony_capture_ops = {
  510. .open = snd_harmony_capture_open,
  511. .close = snd_harmony_capture_close,
  512. .ioctl = snd_pcm_lib_ioctl,
  513. .hw_params = snd_harmony_hw_params,
  514. .hw_free = snd_harmony_hw_free,
  515. .prepare = snd_harmony_capture_prepare,
  516. .trigger = snd_harmony_capture_trigger,
  517. .pointer = snd_harmony_capture_pointer,
  518. };
  519. static int
  520. snd_harmony_pcm_init(harmony_t *h)
  521. {
  522. snd_pcm_t *pcm;
  523. int err;
  524. harmony_disable_interrupts(h);
  525. err = snd_pcm_new(h->card, "harmony", 0, 1, 1, &pcm);
  526. if (err < 0)
  527. return err;
  528. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  529. &snd_harmony_playback_ops);
  530. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  531. &snd_harmony_capture_ops);
  532. pcm->private_data = h;
  533. pcm->info_flags = 0;
  534. strcpy(pcm->name, "harmony");
  535. h->pcm = pcm;
  536. h->psubs = NULL;
  537. h->csubs = NULL;
  538. /* initialize graveyard buffer */
  539. h->dma.type = SNDRV_DMA_TYPE_DEV;
  540. h->dma.dev = &h->dev->dev;
  541. err = snd_dma_alloc_pages(h->dma.type,
  542. h->dma.dev,
  543. BUF_SIZE*GRAVEYARD_BUFS,
  544. &h->gdma);
  545. if (err < 0) {
  546. printk(KERN_ERR PFX "cannot allocate graveyard buffer!\n");
  547. return err;
  548. }
  549. /* initialize silence buffers */
  550. err = snd_dma_alloc_pages(h->dma.type,
  551. h->dma.dev,
  552. BUF_SIZE*SILENCE_BUFS,
  553. &h->sdma);
  554. if (err < 0) {
  555. printk(KERN_ERR PFX "cannot allocate silence buffer!\n");
  556. return err;
  557. }
  558. /* pre-allocate space for DMA */
  559. err = snd_pcm_lib_preallocate_pages_for_all(pcm, h->dma.type,
  560. h->dma.dev,
  561. MAX_BUF_SIZE,
  562. MAX_BUF_SIZE);
  563. if (err < 0) {
  564. printk(KERN_ERR PFX "buffer allocation error: %d\n", err);
  565. return err;
  566. }
  567. h->st.format = snd_harmony_set_data_format(h,
  568. SNDRV_PCM_FORMAT_S16_BE, 1);
  569. return 0;
  570. }
  571. static void
  572. snd_harmony_set_new_gain(harmony_t *h)
  573. {
  574. harmony_wait_for_control(h);
  575. harmony_write(h, HARMONY_GAINCTL, h->st.gain);
  576. }
  577. static int
  578. snd_harmony_mixercontrol_info(snd_kcontrol_t *kc,
  579. snd_ctl_elem_info_t *uinfo)
  580. {
  581. int mask = (kc->private_value >> 16) & 0xff;
  582. int left_shift = (kc->private_value) & 0xff;
  583. int right_shift = (kc->private_value >> 8) & 0xff;
  584. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN :
  585. SNDRV_CTL_ELEM_TYPE_INTEGER;
  586. uinfo->count = left_shift == right_shift ? 1 : 2;
  587. uinfo->value.integer.min = 0;
  588. uinfo->value.integer.max = mask;
  589. return 0;
  590. }
  591. static int
  592. snd_harmony_volume_get(snd_kcontrol_t *kc,
  593. snd_ctl_elem_value_t *ucontrol)
  594. {
  595. harmony_t *h = snd_kcontrol_chip(kc);
  596. int shift_left = (kc->private_value) & 0xff;
  597. int shift_right = (kc->private_value >> 8) & 0xff;
  598. int mask = (kc->private_value >> 16) & 0xff;
  599. int invert = (kc->private_value >> 24) & 0xff;
  600. int left, right;
  601. unsigned long flags;
  602. spin_lock_irqsave(&h->mixer_lock, flags);
  603. left = (h->st.gain >> shift_left) & mask;
  604. right = (h->st.gain >> shift_right) & mask;
  605. if (invert) {
  606. left = mask - left;
  607. right = mask - right;
  608. }
  609. ucontrol->value.integer.value[0] = left;
  610. if (shift_left != shift_right)
  611. ucontrol->value.integer.value[1] = right;
  612. spin_unlock_irqrestore(&h->mixer_lock, flags);
  613. return 0;
  614. }
  615. static int
  616. snd_harmony_volume_put(snd_kcontrol_t *kc,
  617. snd_ctl_elem_value_t *ucontrol)
  618. {
  619. harmony_t *h = snd_kcontrol_chip(kc);
  620. int shift_left = (kc->private_value) & 0xff;
  621. int shift_right = (kc->private_value >> 8) & 0xff;
  622. int mask = (kc->private_value >> 16) & 0xff;
  623. int invert = (kc->private_value >> 24) & 0xff;
  624. int left, right;
  625. int old_gain = h->st.gain;
  626. unsigned long flags;
  627. spin_lock_irqsave(&h->mixer_lock, flags);
  628. left = ucontrol->value.integer.value[0] & mask;
  629. if (invert)
  630. left = mask - left;
  631. h->st.gain &= ~( (mask << shift_left ) );
  632. h->st.gain |= (left << shift_left);
  633. if (shift_left != shift_right) {
  634. right = ucontrol->value.integer.value[1] & mask;
  635. if (invert)
  636. right = mask - right;
  637. h->st.gain &= ~( (mask << shift_right) );
  638. h->st.gain |= (right << shift_right);
  639. }
  640. snd_harmony_set_new_gain(h);
  641. spin_unlock_irqrestore(&h->mixer_lock, flags);
  642. return h->st.gain != old_gain;
  643. }
  644. static int
  645. snd_harmony_captureroute_info(snd_kcontrol_t *kc,
  646. snd_ctl_elem_info_t *uinfo)
  647. {
  648. static char *texts[2] = { "Line", "Mic" };
  649. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  650. uinfo->count = 1;
  651. uinfo->value.enumerated.items = 2;
  652. if (uinfo->value.enumerated.item > 1)
  653. uinfo->value.enumerated.item = 1;
  654. strcpy(uinfo->value.enumerated.name,
  655. texts[uinfo->value.enumerated.item]);
  656. return 0;
  657. }
  658. static int
  659. snd_harmony_captureroute_get(snd_kcontrol_t *kc,
  660. snd_ctl_elem_value_t *ucontrol)
  661. {
  662. harmony_t *h = snd_kcontrol_chip(kc);
  663. int value;
  664. unsigned long flags;
  665. spin_lock_irqsave(&h->mixer_lock, flags);
  666. value = (h->st.gain >> HARMONY_GAIN_IS_SHIFT) & 1;
  667. ucontrol->value.enumerated.item[0] = value;
  668. spin_unlock_irqrestore(&h->mixer_lock, flags);
  669. return 0;
  670. }
  671. static int
  672. snd_harmony_captureroute_put(snd_kcontrol_t *kc,
  673. snd_ctl_elem_value_t *ucontrol)
  674. {
  675. harmony_t *h = snd_kcontrol_chip(kc);
  676. int value;
  677. int old_gain = h->st.gain;
  678. unsigned long flags;
  679. spin_lock_irqsave(&h->mixer_lock, flags);
  680. value = ucontrol->value.enumerated.item[0] & 1;
  681. h->st.gain &= ~HARMONY_GAIN_IS_MASK;
  682. h->st.gain |= value << HARMONY_GAIN_IS_SHIFT;
  683. snd_harmony_set_new_gain(h);
  684. spin_unlock_irqrestore(&h->mixer_lock, flags);
  685. return h->st.gain != old_gain;
  686. }
  687. #define HARMONY_CONTROLS (sizeof(snd_harmony_controls)/ \
  688. sizeof(snd_kcontrol_new_t))
  689. #define HARMONY_VOLUME(xname, left_shift, right_shift, mask, invert) \
  690. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  691. .info = snd_harmony_mixercontrol_info, \
  692. .get = snd_harmony_volume_get, .put = snd_harmony_volume_put, \
  693. .private_value = ((left_shift) | ((right_shift) << 8) | \
  694. ((mask) << 16) | ((invert) << 24)) }
  695. static snd_kcontrol_new_t snd_harmony_controls[] = {
  696. HARMONY_VOLUME("Master Playback Volume", HARMONY_GAIN_LO_SHIFT,
  697. HARMONY_GAIN_RO_SHIFT, HARMONY_GAIN_OUT, 1),
  698. HARMONY_VOLUME("Capture Volume", HARMONY_GAIN_LI_SHIFT,
  699. HARMONY_GAIN_RI_SHIFT, HARMONY_GAIN_IN, 0),
  700. HARMONY_VOLUME("Monitor Volume", HARMONY_GAIN_MA_SHIFT,
  701. HARMONY_GAIN_MA_SHIFT, HARMONY_GAIN_MA, 1),
  702. {
  703. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  704. .name = "Input Route",
  705. .info = snd_harmony_captureroute_info,
  706. .get = snd_harmony_captureroute_get,
  707. .put = snd_harmony_captureroute_put
  708. },
  709. HARMONY_VOLUME("Internal Speaker Switch", HARMONY_GAIN_SE_SHIFT,
  710. HARMONY_GAIN_SE_SHIFT, 1, 0),
  711. HARMONY_VOLUME("Line-Out Switch", HARMONY_GAIN_LE_SHIFT,
  712. HARMONY_GAIN_LE_SHIFT, 1, 0),
  713. HARMONY_VOLUME("Headphones Switch", HARMONY_GAIN_HE_SHIFT,
  714. HARMONY_GAIN_HE_SHIFT, 1, 0),
  715. };
  716. static void __init
  717. snd_harmony_mixer_reset(harmony_t *h)
  718. {
  719. harmony_mute(h);
  720. harmony_reset(h);
  721. h->st.gain = HARMONY_GAIN_DEFAULT;
  722. harmony_unmute(h);
  723. }
  724. static int __init
  725. snd_harmony_mixer_init(harmony_t *h)
  726. {
  727. snd_card_t *card = h->card;
  728. int idx, err;
  729. snd_assert(h != NULL, return -EINVAL);
  730. strcpy(card->mixername, "Harmony Gain control interface");
  731. for (idx = 0; idx < HARMONY_CONTROLS; idx++) {
  732. err = snd_ctl_add(card,
  733. snd_ctl_new1(&snd_harmony_controls[idx], h));
  734. if (err < 0)
  735. return err;
  736. }
  737. snd_harmony_mixer_reset(h);
  738. return 0;
  739. }
  740. static int
  741. snd_harmony_free(harmony_t *h)
  742. {
  743. if (h->gdma.addr)
  744. snd_dma_free_pages(&h->gdma);
  745. if (h->sdma.addr)
  746. snd_dma_free_pages(&h->sdma);
  747. if (h->irq >= 0)
  748. free_irq(h->irq, h);
  749. if (h->iobase)
  750. iounmap(h->iobase);
  751. parisc_set_drvdata(h->dev, NULL);
  752. kfree(h);
  753. return 0;
  754. }
  755. static int
  756. snd_harmony_dev_free(snd_device_t *dev)
  757. {
  758. harmony_t *h = dev->device_data;
  759. return snd_harmony_free(h);
  760. }
  761. static int __devinit
  762. snd_harmony_create(snd_card_t *card,
  763. struct parisc_device *padev,
  764. harmony_t **rchip)
  765. {
  766. int err;
  767. harmony_t *h;
  768. static snd_device_ops_t ops = {
  769. .dev_free = snd_harmony_dev_free,
  770. };
  771. *rchip = NULL;
  772. h = kmalloc(sizeof(*h), GFP_KERNEL);
  773. if (h == NULL)
  774. return -ENOMEM;
  775. memset(&h->st, 0, sizeof(h->st));
  776. memset(&h->stats, 0, sizeof(h->stats));
  777. memset(&h->pbuf, 0, sizeof(h->pbuf));
  778. memset(&h->cbuf, 0, sizeof(h->cbuf));
  779. h->hpa = padev->hpa.start;
  780. h->card = card;
  781. h->dev = padev;
  782. h->irq = padev->irq;
  783. h->iobase = ioremap_nocache(padev->hpa.start, HARMONY_SIZE);
  784. if (h->iobase == NULL) {
  785. printk(KERN_ERR PFX "unable to remap hpa 0x%lx\n",
  786. padev->hpa.start);
  787. err = -EBUSY;
  788. goto free_and_ret;
  789. }
  790. err = request_irq(h->irq, snd_harmony_interrupt, 0,
  791. "harmony", h);
  792. if (err) {
  793. printk(KERN_ERR PFX "could not obtain interrupt %d",
  794. h->irq);
  795. goto free_and_ret;
  796. }
  797. spin_lock_init(&h->mixer_lock);
  798. spin_lock_init(&h->lock);
  799. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  800. h, &ops)) < 0) {
  801. goto free_and_ret;
  802. }
  803. snd_card_set_dev(card, &padev->dev);
  804. *rchip = h;
  805. return 0;
  806. free_and_ret:
  807. snd_harmony_free(h);
  808. return err;
  809. }
  810. static int __devinit
  811. snd_harmony_probe(struct parisc_device *padev)
  812. {
  813. int err;
  814. static int dev;
  815. snd_card_t *card;
  816. harmony_t *h;
  817. static int index = SNDRV_DEFAULT_IDX1;
  818. static char *id = SNDRV_DEFAULT_STR1;
  819. h = parisc_get_drvdata(padev);
  820. if (h != NULL) {
  821. return -ENODEV;
  822. }
  823. card = snd_card_new(index, id, THIS_MODULE, 0);
  824. if (card == NULL)
  825. return -ENOMEM;
  826. err = snd_harmony_create(card, padev, &h);
  827. if (err < 0) {
  828. goto free_and_ret;
  829. }
  830. err = snd_harmony_pcm_init(h);
  831. if (err < 0) {
  832. goto free_and_ret;
  833. }
  834. err = snd_harmony_mixer_init(h);
  835. if (err < 0) {
  836. goto free_and_ret;
  837. }
  838. strcpy(card->driver, "harmony");
  839. strcpy(card->shortname, "Harmony");
  840. sprintf(card->longname, "%s at 0x%lx, irq %i",
  841. card->shortname, h->hpa, h->irq);
  842. err = snd_card_register(card);
  843. if (err < 0) {
  844. goto free_and_ret;
  845. }
  846. dev++;
  847. parisc_set_drvdata(padev, h);
  848. return 0;
  849. free_and_ret:
  850. snd_card_free(card);
  851. return err;
  852. }
  853. static int __devexit
  854. snd_harmony_remove(struct parisc_device *padev)
  855. {
  856. harmony_t *h = parisc_get_drvdata(padev);
  857. snd_card_free(h->card);
  858. return 0;
  859. }
  860. static struct parisc_driver snd_harmony_driver = {
  861. .name = "harmony",
  862. .id_table = snd_harmony_devtable,
  863. .probe = snd_harmony_probe,
  864. .remove = snd_harmony_remove,
  865. };
  866. static int __init
  867. alsa_harmony_init(void)
  868. {
  869. int err;
  870. err = register_parisc_driver(&snd_harmony_driver);
  871. if (err < 0) {
  872. printk(KERN_ERR PFX "device not found\n");
  873. return err;
  874. }
  875. return 0;
  876. }
  877. static void __exit
  878. alsa_harmony_fini(void)
  879. {
  880. int err;
  881. err = unregister_parisc_driver(&snd_harmony_driver);
  882. if (err < 0) {
  883. printk(KERN_ERR PFX "failed to unregister\n");
  884. }
  885. return;
  886. }
  887. MODULE_LICENSE("GPL");
  888. MODULE_AUTHOR("Kyle McMartin <kyle@parisc-linux.org>");
  889. MODULE_DESCRIPTION("Harmony sound driver");
  890. module_init(alsa_harmony_init);
  891. module_exit(alsa_harmony_fini);