harmony.c 25 KB

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