harmony.c 25 KB

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