harmony.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 <linux/io.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/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 const char * const texts[2] = { "Line", "Mic" };
  652. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  653. }
  654. static int
  655. snd_harmony_captureroute_get(struct snd_kcontrol *kc,
  656. struct snd_ctl_elem_value *ucontrol)
  657. {
  658. struct snd_harmony *h = snd_kcontrol_chip(kc);
  659. int value;
  660. spin_lock_irq(&h->mixer_lock);
  661. value = (h->st.gain >> HARMONY_GAIN_IS_SHIFT) & 1;
  662. ucontrol->value.enumerated.item[0] = value;
  663. spin_unlock_irq(&h->mixer_lock);
  664. return 0;
  665. }
  666. static int
  667. snd_harmony_captureroute_put(struct snd_kcontrol *kc,
  668. struct snd_ctl_elem_value *ucontrol)
  669. {
  670. struct snd_harmony *h = snd_kcontrol_chip(kc);
  671. int value;
  672. int old_gain = h->st.gain;
  673. spin_lock_irq(&h->mixer_lock);
  674. value = ucontrol->value.enumerated.item[0] & 1;
  675. h->st.gain &= ~HARMONY_GAIN_IS_MASK;
  676. h->st.gain |= value << HARMONY_GAIN_IS_SHIFT;
  677. snd_harmony_set_new_gain(h);
  678. spin_unlock_irq(&h->mixer_lock);
  679. return h->st.gain != old_gain;
  680. }
  681. #define HARMONY_CONTROLS ARRAY_SIZE(snd_harmony_controls)
  682. #define HARMONY_VOLUME(xname, left_shift, right_shift, mask, invert) \
  683. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  684. .info = snd_harmony_mixercontrol_info, \
  685. .get = snd_harmony_volume_get, .put = snd_harmony_volume_put, \
  686. .private_value = ((left_shift) | ((right_shift) << 8) | \
  687. ((mask) << 16) | ((invert) << 24)) }
  688. static struct snd_kcontrol_new snd_harmony_controls[] = {
  689. HARMONY_VOLUME("Master Playback Volume", HARMONY_GAIN_LO_SHIFT,
  690. HARMONY_GAIN_RO_SHIFT, HARMONY_GAIN_OUT, 1),
  691. HARMONY_VOLUME("Capture Volume", HARMONY_GAIN_LI_SHIFT,
  692. HARMONY_GAIN_RI_SHIFT, HARMONY_GAIN_IN, 0),
  693. HARMONY_VOLUME("Monitor Volume", HARMONY_GAIN_MA_SHIFT,
  694. HARMONY_GAIN_MA_SHIFT, HARMONY_GAIN_MA, 1),
  695. {
  696. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  697. .name = "Input Route",
  698. .info = snd_harmony_captureroute_info,
  699. .get = snd_harmony_captureroute_get,
  700. .put = snd_harmony_captureroute_put
  701. },
  702. HARMONY_VOLUME("Internal Speaker Switch", HARMONY_GAIN_SE_SHIFT,
  703. HARMONY_GAIN_SE_SHIFT, 1, 0),
  704. HARMONY_VOLUME("Line-Out Switch", HARMONY_GAIN_LE_SHIFT,
  705. HARMONY_GAIN_LE_SHIFT, 1, 0),
  706. HARMONY_VOLUME("Headphones Switch", HARMONY_GAIN_HE_SHIFT,
  707. HARMONY_GAIN_HE_SHIFT, 1, 0),
  708. };
  709. static void
  710. snd_harmony_mixer_reset(struct snd_harmony *h)
  711. {
  712. harmony_mute(h);
  713. harmony_reset(h);
  714. h->st.gain = HARMONY_GAIN_DEFAULT;
  715. harmony_unmute(h);
  716. }
  717. static int
  718. snd_harmony_mixer_init(struct snd_harmony *h)
  719. {
  720. struct snd_card *card;
  721. int idx, err;
  722. if (snd_BUG_ON(!h))
  723. return -EINVAL;
  724. card = h->card;
  725. strcpy(card->mixername, "Harmony Gain control interface");
  726. for (idx = 0; idx < HARMONY_CONTROLS; idx++) {
  727. err = snd_ctl_add(card,
  728. snd_ctl_new1(&snd_harmony_controls[idx], h));
  729. if (err < 0)
  730. return err;
  731. }
  732. snd_harmony_mixer_reset(h);
  733. return 0;
  734. }
  735. static int
  736. snd_harmony_free(struct snd_harmony *h)
  737. {
  738. if (h->gdma.addr)
  739. snd_dma_free_pages(&h->gdma);
  740. if (h->sdma.addr)
  741. snd_dma_free_pages(&h->sdma);
  742. if (h->irq >= 0)
  743. free_irq(h->irq, h);
  744. iounmap(h->iobase);
  745. kfree(h);
  746. return 0;
  747. }
  748. static int
  749. snd_harmony_dev_free(struct snd_device *dev)
  750. {
  751. struct snd_harmony *h = dev->device_data;
  752. return snd_harmony_free(h);
  753. }
  754. static int
  755. snd_harmony_create(struct snd_card *card,
  756. struct parisc_device *padev,
  757. struct snd_harmony **rchip)
  758. {
  759. int err;
  760. struct snd_harmony *h;
  761. static struct snd_device_ops ops = {
  762. .dev_free = snd_harmony_dev_free,
  763. };
  764. *rchip = NULL;
  765. h = kzalloc(sizeof(*h), GFP_KERNEL);
  766. if (h == NULL)
  767. return -ENOMEM;
  768. h->hpa = padev->hpa.start;
  769. h->card = card;
  770. h->dev = padev;
  771. h->irq = -1;
  772. h->iobase = ioremap_nocache(padev->hpa.start, HARMONY_SIZE);
  773. if (h->iobase == NULL) {
  774. printk(KERN_ERR PFX "unable to remap hpa 0x%lx\n",
  775. (unsigned long)padev->hpa.start);
  776. err = -EBUSY;
  777. goto free_and_ret;
  778. }
  779. err = request_irq(padev->irq, snd_harmony_interrupt, 0,
  780. "harmony", h);
  781. if (err) {
  782. printk(KERN_ERR PFX "could not obtain interrupt %d",
  783. padev->irq);
  784. goto free_and_ret;
  785. }
  786. h->irq = padev->irq;
  787. spin_lock_init(&h->mixer_lock);
  788. spin_lock_init(&h->lock);
  789. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  790. h, &ops)) < 0) {
  791. goto free_and_ret;
  792. }
  793. *rchip = h;
  794. return 0;
  795. free_and_ret:
  796. snd_harmony_free(h);
  797. return err;
  798. }
  799. static int
  800. snd_harmony_probe(struct parisc_device *padev)
  801. {
  802. int err;
  803. struct snd_card *card;
  804. struct snd_harmony *h;
  805. err = snd_card_new(&padev->dev, index, id, THIS_MODULE, 0, &card);
  806. if (err < 0)
  807. return err;
  808. err = snd_harmony_create(card, padev, &h);
  809. if (err < 0)
  810. goto free_and_ret;
  811. err = snd_harmony_pcm_init(h);
  812. if (err < 0)
  813. goto free_and_ret;
  814. err = snd_harmony_mixer_init(h);
  815. if (err < 0)
  816. goto free_and_ret;
  817. strcpy(card->driver, "harmony");
  818. strcpy(card->shortname, "Harmony");
  819. sprintf(card->longname, "%s at 0x%lx, irq %i",
  820. card->shortname, h->hpa, h->irq);
  821. err = snd_card_register(card);
  822. if (err < 0)
  823. goto free_and_ret;
  824. parisc_set_drvdata(padev, card);
  825. return 0;
  826. free_and_ret:
  827. snd_card_free(card);
  828. return err;
  829. }
  830. static int
  831. snd_harmony_remove(struct parisc_device *padev)
  832. {
  833. snd_card_free(parisc_get_drvdata(padev));
  834. return 0;
  835. }
  836. static struct parisc_driver snd_harmony_driver = {
  837. .name = "harmony",
  838. .id_table = snd_harmony_devtable,
  839. .probe = snd_harmony_probe,
  840. .remove = snd_harmony_remove,
  841. };
  842. static int __init
  843. alsa_harmony_init(void)
  844. {
  845. return register_parisc_driver(&snd_harmony_driver);
  846. }
  847. static void __exit
  848. alsa_harmony_fini(void)
  849. {
  850. unregister_parisc_driver(&snd_harmony_driver);
  851. }
  852. MODULE_LICENSE("GPL");
  853. MODULE_AUTHOR("Kyle McMartin <kyle@parisc-linux.org>");
  854. MODULE_DESCRIPTION("Harmony sound driver");
  855. module_init(alsa_harmony_init);
  856. module_exit(alsa_harmony_fini);