sa11xx-uda1341.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Driver for Philips UDA1341TS on Compaq iPAQ H3600 soundcard
  3. * Copyright (C) 2002 Tomas Kasparek <tomas.kasparek@seznam.cz>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License.
  7. *
  8. * History:
  9. *
  10. * 2002-03-13 Tomas Kasparek initial release - based on h3600-uda1341.c from OSS
  11. * 2002-03-20 Tomas Kasparek playback over ALSA is working
  12. * 2002-03-28 Tomas Kasparek playback over OSS emulation is working
  13. * 2002-03-29 Tomas Kasparek basic capture is working (native ALSA)
  14. * 2002-03-29 Tomas Kasparek capture is working (OSS emulation)
  15. * 2002-04-04 Tomas Kasparek better rates handling (allow non-standard rates)
  16. * 2003-02-14 Brian Avery fixed full duplex mode, other updates
  17. * 2003-02-20 Tomas Kasparek merged updates by Brian (except HAL)
  18. * 2003-04-19 Jaroslav Kysela recoded DMA stuff to follow 2.4.18rmk3-hh24 kernel
  19. * working suspend and resume
  20. * 2003-04-28 Tomas Kasparek updated work by Jaroslav to compile it under 2.5.x again
  21. * merged HAL layer (patches from Brian)
  22. */
  23. /* $Id: sa11xx-uda1341.c,v 1.27 2005/12/07 09:13:42 cladisch Exp $ */
  24. /***************************************************************************************************
  25. *
  26. * To understand what Alsa Drivers should be doing look at "Writing an Alsa Driver" by Takashi Iwai
  27. * available in the Alsa doc section on the website
  28. *
  29. * A few notes to make things clearer. The UDA1341 is hooked up to Serial port 4 on the SA1100.
  30. * We are using SSP mode to talk to the UDA1341. The UDA1341 bit & wordselect clocks are generated
  31. * by this UART. Unfortunately, the clock only runs if the transmit buffer has something in it.
  32. * So, if we are just recording, we feed the transmit DMA stream a bunch of 0x0000 so that the
  33. * transmit buffer is full and the clock keeps going. The zeroes come from FLUSH_BASE_PHYS which
  34. * is a mem loc that always decodes to 0's w/ no off chip access.
  35. *
  36. * Some alsa terminology:
  37. * frame => num_channels * sample_size e.g stereo 16 bit is 2 * 16 = 32 bytes
  38. * period => the least number of bytes that will generate an interrupt e.g. we have a 1024 byte
  39. * buffer and 4 periods in the runtime structure this means we'll get an int every 256
  40. * bytes or 4 times per buffer.
  41. * A number of the sizes are in frames rather than bytes, use frames_to_bytes and
  42. * bytes_to_frames to convert. The easiest way to tell the units is to look at the
  43. * type i.e. runtime-> buffer_size is in frames and its type is snd_pcm_uframes_t
  44. *
  45. * Notes about the pointer fxn:
  46. * The pointer fxn needs to return the offset into the dma buffer in frames.
  47. * Interrupts must be blocked before calling the dma_get_pos fxn to avoid race with interrupts.
  48. *
  49. * Notes about pause/resume
  50. * Implementing this would be complicated so it's skipped. The problem case is:
  51. * A full duplex connection is going, then play is paused. At this point you need to start xmitting
  52. * 0's to keep the record active which means you cant just freeze the dma and resume it later you'd
  53. * need to save off the dma info, and restore it properly on a resume. Yeach!
  54. *
  55. * Notes about transfer methods:
  56. * The async write calls fail. I probably need to implement something else to support them?
  57. *
  58. ***************************************************************************************************/
  59. #include <linux/config.h>
  60. #include <sound/driver.h>
  61. #include <linux/module.h>
  62. #include <linux/moduleparam.h>
  63. #include <linux/init.h>
  64. #include <linux/err.h>
  65. #include <linux/platform_device.h>
  66. #include <linux/errno.h>
  67. #include <linux/ioctl.h>
  68. #include <linux/delay.h>
  69. #include <linux/slab.h>
  70. #ifdef CONFIG_PM
  71. #include <linux/pm.h>
  72. #endif
  73. #include <asm/hardware.h>
  74. #include <asm/arch/h3600.h>
  75. #include <asm/mach-types.h>
  76. #include <asm/dma.h>
  77. #ifdef CONFIG_H3600_HAL
  78. #include <asm/semaphore.h>
  79. #include <asm/uaccess.h>
  80. #include <asm/arch/h3600_hal.h>
  81. #endif
  82. #include <sound/core.h>
  83. #include <sound/pcm.h>
  84. #include <sound/initval.h>
  85. #include <linux/l3/l3.h>
  86. #undef DEBUG_MODE
  87. #undef DEBUG_FUNCTION_NAMES
  88. #include <sound/uda1341.h>
  89. /*
  90. * FIXME: Is this enough as autodetection of 2.4.X-rmkY-hhZ kernels?
  91. * We use DMA stuff from 2.4.18-rmk3-hh24 here to be able to compile this
  92. * module for Familiar 0.6.1
  93. */
  94. #ifdef CONFIG_H3600_HAL
  95. #define HH_VERSION 1
  96. #endif
  97. /* {{{ Type definitions */
  98. MODULE_AUTHOR("Tomas Kasparek <tomas.kasparek@seznam.cz>");
  99. MODULE_LICENSE("GPL");
  100. MODULE_DESCRIPTION("SA1100/SA1111 + UDA1341TS driver for ALSA");
  101. MODULE_SUPPORTED_DEVICE("{{UDA1341,iPAQ H3600 UDA1341TS}}");
  102. static char *id = NULL; /* ID for this card */
  103. module_param(id, charp, 0444);
  104. MODULE_PARM_DESC(id, "ID string for SA1100/SA1111 + UDA1341TS soundcard.");
  105. struct audio_stream {
  106. char *id; /* identification string */
  107. int stream_id; /* numeric identification */
  108. dma_device_t dma_dev; /* device identifier for DMA */
  109. #ifdef HH_VERSION
  110. dmach_t dmach; /* dma channel identification */
  111. #else
  112. dma_regs_t *dma_regs; /* points to our DMA registers */
  113. #endif
  114. int active:1; /* we are using this stream for transfer now */
  115. int period; /* current transfer period */
  116. int periods; /* current count of periods registerd in the DMA engine */
  117. int tx_spin; /* are we recoding - flag used to do DMA trans. for sync */
  118. unsigned int old_offset;
  119. spinlock_t dma_lock; /* for locking in DMA operations (see dma-sa1100.c in the kernel) */
  120. struct snd_pcm_substream *stream;
  121. };
  122. struct sa11xx_uda1341 {
  123. struct snd_card *card;
  124. struct l3_client *uda1341;
  125. struct snd_pcm *pcm;
  126. long samplerate;
  127. struct audio_stream s[2]; /* playback & capture */
  128. };
  129. static unsigned int rates[] = {
  130. 8000, 10666, 10985, 14647,
  131. 16000, 21970, 22050, 24000,
  132. 29400, 32000, 44100, 48000,
  133. };
  134. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  135. .count = ARRAY_SIZE(rates),
  136. .list = rates,
  137. .mask = 0,
  138. };
  139. static struct platform_device *device;
  140. /* }}} */
  141. /* {{{ Clock and sample rate stuff */
  142. /*
  143. * Stop-gap solution until rest of hh.org HAL stuff is merged.
  144. */
  145. #define GPIO_H3600_CLK_SET0 GPIO_GPIO (12)
  146. #define GPIO_H3600_CLK_SET1 GPIO_GPIO (13)
  147. #ifdef CONFIG_SA1100_H3XXX
  148. #define clr_sa11xx_uda1341_egpio(x) clr_h3600_egpio(x)
  149. #define set_sa11xx_uda1341_egpio(x) set_h3600_egpio(x)
  150. #else
  151. #error This driver could serve H3x00 handhelds only!
  152. #endif
  153. static void sa11xx_uda1341_set_audio_clock(long val)
  154. {
  155. switch (val) {
  156. case 24000: case 32000: case 48000: /* 00: 12.288 MHz */
  157. GPCR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1;
  158. break;
  159. case 22050: case 29400: case 44100: /* 01: 11.2896 MHz */
  160. GPSR = GPIO_H3600_CLK_SET0;
  161. GPCR = GPIO_H3600_CLK_SET1;
  162. break;
  163. case 8000: case 10666: case 16000: /* 10: 4.096 MHz */
  164. GPCR = GPIO_H3600_CLK_SET0;
  165. GPSR = GPIO_H3600_CLK_SET1;
  166. break;
  167. case 10985: case 14647: case 21970: /* 11: 5.6245 MHz */
  168. GPSR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1;
  169. break;
  170. }
  171. }
  172. static void sa11xx_uda1341_set_samplerate(struct sa11xx_uda1341 *sa11xx_uda1341, long rate)
  173. {
  174. int clk_div = 0;
  175. int clk=0;
  176. /* We don't want to mess with clocks when frames are in flight */
  177. Ser4SSCR0 &= ~SSCR0_SSE;
  178. /* wait for any frame to complete */
  179. udelay(125);
  180. /*
  181. * We have the following clock sources:
  182. * 4.096 MHz, 5.6245 MHz, 11.2896 MHz, 12.288 MHz
  183. * Those can be divided either by 256, 384 or 512.
  184. * This makes up 12 combinations for the following samplerates...
  185. */
  186. if (rate >= 48000)
  187. rate = 48000;
  188. else if (rate >= 44100)
  189. rate = 44100;
  190. else if (rate >= 32000)
  191. rate = 32000;
  192. else if (rate >= 29400)
  193. rate = 29400;
  194. else if (rate >= 24000)
  195. rate = 24000;
  196. else if (rate >= 22050)
  197. rate = 22050;
  198. else if (rate >= 21970)
  199. rate = 21970;
  200. else if (rate >= 16000)
  201. rate = 16000;
  202. else if (rate >= 14647)
  203. rate = 14647;
  204. else if (rate >= 10985)
  205. rate = 10985;
  206. else if (rate >= 10666)
  207. rate = 10666;
  208. else
  209. rate = 8000;
  210. /* Set the external clock generator */
  211. #ifdef CONFIG_H3600_HAL
  212. h3600_audio_clock(rate);
  213. #else
  214. sa11xx_uda1341_set_audio_clock(rate);
  215. #endif
  216. /* Select the clock divisor */
  217. switch (rate) {
  218. case 8000:
  219. case 10985:
  220. case 22050:
  221. case 24000:
  222. clk = F512;
  223. clk_div = SSCR0_SerClkDiv(16);
  224. break;
  225. case 16000:
  226. case 21970:
  227. case 44100:
  228. case 48000:
  229. clk = F256;
  230. clk_div = SSCR0_SerClkDiv(8);
  231. break;
  232. case 10666:
  233. case 14647:
  234. case 29400:
  235. case 32000:
  236. clk = F384;
  237. clk_div = SSCR0_SerClkDiv(12);
  238. break;
  239. }
  240. /* FMT setting should be moved away when other FMTs are added (FIXME) */
  241. l3_command(sa11xx_uda1341->uda1341, CMD_FORMAT, (void *)LSB16);
  242. l3_command(sa11xx_uda1341->uda1341, CMD_FS, (void *)clk);
  243. Ser4SSCR0 = (Ser4SSCR0 & ~0xff00) + clk_div + SSCR0_SSE;
  244. sa11xx_uda1341->samplerate = rate;
  245. }
  246. /* }}} */
  247. /* {{{ HW init and shutdown */
  248. static void sa11xx_uda1341_audio_init(struct sa11xx_uda1341 *sa11xx_uda1341)
  249. {
  250. unsigned long flags;
  251. /* Setup DMA stuff */
  252. sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].id = "UDA1341 out";
  253. sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].stream_id = SNDRV_PCM_STREAM_PLAYBACK;
  254. sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK].dma_dev = DMA_Ser4SSPWr;
  255. sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].id = "UDA1341 in";
  256. sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].stream_id = SNDRV_PCM_STREAM_CAPTURE;
  257. sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE].dma_dev = DMA_Ser4SSPRd;
  258. /* Initialize the UDA1341 internal state */
  259. /* Setup the uarts */
  260. local_irq_save(flags);
  261. GAFR |= (GPIO_SSP_CLK);
  262. GPDR &= ~(GPIO_SSP_CLK);
  263. Ser4SSCR0 = 0;
  264. Ser4SSCR0 = SSCR0_DataSize(16) + SSCR0_TI + SSCR0_SerClkDiv(8);
  265. Ser4SSCR1 = SSCR1_SClkIactL + SSCR1_SClk1P + SSCR1_ExtClk;
  266. Ser4SSCR0 |= SSCR0_SSE;
  267. local_irq_restore(flags);
  268. /* Enable the audio power */
  269. #ifdef CONFIG_H3600_HAL
  270. h3600_audio_power(AUDIO_RATE_DEFAULT);
  271. #else
  272. clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
  273. set_sa11xx_uda1341_egpio(IPAQ_EGPIO_AUDIO_ON);
  274. set_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
  275. #endif
  276. /* Wait for the UDA1341 to wake up */
  277. mdelay(1); //FIXME - was removed by Perex - Why?
  278. /* Initialize the UDA1341 internal state */
  279. l3_open(sa11xx_uda1341->uda1341);
  280. /* external clock configuration (after l3_open - regs must be initialized */
  281. sa11xx_uda1341_set_samplerate(sa11xx_uda1341, sa11xx_uda1341->samplerate);
  282. /* Wait for the UDA1341 to wake up */
  283. set_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
  284. mdelay(1);
  285. /* make the left and right channels unswapped (flip the WS latch) */
  286. Ser4SSDR = 0;
  287. #ifdef CONFIG_H3600_HAL
  288. h3600_audio_mute(0);
  289. #else
  290. clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
  291. #endif
  292. }
  293. static void sa11xx_uda1341_audio_shutdown(struct sa11xx_uda1341 *sa11xx_uda1341)
  294. {
  295. /* mute on */
  296. #ifdef CONFIG_H3600_HAL
  297. h3600_audio_mute(1);
  298. #else
  299. set_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
  300. #endif
  301. /* disable the audio power and all signals leading to the audio chip */
  302. l3_close(sa11xx_uda1341->uda1341);
  303. Ser4SSCR0 = 0;
  304. clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_CODEC_NRESET);
  305. /* power off and mute off */
  306. /* FIXME - is muting off necesary??? */
  307. #ifdef CONFIG_H3600_HAL
  308. h3600_audio_power(0);
  309. h3600_audio_mute(0);
  310. #else
  311. clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_AUDIO_ON);
  312. clr_sa11xx_uda1341_egpio(IPAQ_EGPIO_QMUTE);
  313. #endif
  314. }
  315. /* }}} */
  316. /* {{{ DMA staff */
  317. /*
  318. * these are the address and sizes used to fill the xmit buffer
  319. * so we can get a clock in record only mode
  320. */
  321. #define FORCE_CLOCK_ADDR (dma_addr_t)FLUSH_BASE_PHYS
  322. #define FORCE_CLOCK_SIZE 4096 // was 2048
  323. // FIXME Why this value exactly - wrote comment
  324. #define DMA_BUF_SIZE 8176 /* <= MAX_DMA_SIZE from asm/arch-sa1100/dma.h */
  325. #ifdef HH_VERSION
  326. static int audio_dma_request(struct audio_stream *s, void (*callback)(void *, int))
  327. {
  328. int ret;
  329. ret = sa1100_request_dma(&s->dmach, s->id, s->dma_dev);
  330. if (ret < 0) {
  331. printk(KERN_ERR "unable to grab audio dma 0x%x\n", s->dma_dev);
  332. return ret;
  333. }
  334. sa1100_dma_set_callback(s->dmach, callback);
  335. return 0;
  336. }
  337. static inline void audio_dma_free(struct audio_stream *s)
  338. {
  339. sa1100_free_dma(s->dmach);
  340. s->dmach = -1;
  341. }
  342. #else
  343. static int audio_dma_request(struct audio_stream *s, void (*callback)(void *))
  344. {
  345. int ret;
  346. ret = sa1100_request_dma(s->dma_dev, s->id, callback, s, &s->dma_regs);
  347. if (ret < 0)
  348. printk(KERN_ERR "unable to grab audio dma 0x%x\n", s->dma_dev);
  349. return ret;
  350. }
  351. static void audio_dma_free(struct audio_stream *s)
  352. {
  353. sa1100_free_dma(s->dma_regs);
  354. s->dma_regs = 0;
  355. }
  356. #endif
  357. static u_int audio_get_dma_pos(struct audio_stream *s)
  358. {
  359. struct snd_pcm_substream *substream = s->stream;
  360. struct snd_pcm_runtime *runtime = substream->runtime;
  361. unsigned int offset;
  362. unsigned long flags;
  363. dma_addr_t addr;
  364. // this must be called w/ interrupts locked out see dma-sa1100.c in the kernel
  365. spin_lock_irqsave(&s->dma_lock, flags);
  366. #ifdef HH_VERSION
  367. sa1100_dma_get_current(s->dmach, NULL, &addr);
  368. #else
  369. addr = sa1100_get_dma_pos((s)->dma_regs);
  370. #endif
  371. offset = addr - runtime->dma_addr;
  372. spin_unlock_irqrestore(&s->dma_lock, flags);
  373. offset = bytes_to_frames(runtime,offset);
  374. if (offset >= runtime->buffer_size)
  375. offset = 0;
  376. return offset;
  377. }
  378. /*
  379. * this stops the dma and clears the dma ptrs
  380. */
  381. static void audio_stop_dma(struct audio_stream *s)
  382. {
  383. unsigned long flags;
  384. spin_lock_irqsave(&s->dma_lock, flags);
  385. s->active = 0;
  386. s->period = 0;
  387. /* this stops the dma channel and clears the buffer ptrs */
  388. #ifdef HH_VERSION
  389. sa1100_dma_flush_all(s->dmach);
  390. #else
  391. sa1100_clear_dma(s->dma_regs);
  392. #endif
  393. spin_unlock_irqrestore(&s->dma_lock, flags);
  394. }
  395. static void audio_process_dma(struct audio_stream *s)
  396. {
  397. struct snd_pcm_substream *substream = s->stream;
  398. struct snd_pcm_runtime *runtime;
  399. unsigned int dma_size;
  400. unsigned int offset;
  401. int ret;
  402. /* we are requested to process synchronization DMA transfer */
  403. if (s->tx_spin) {
  404. snd_assert(s->stream_id == SNDRV_PCM_STREAM_PLAYBACK, return);
  405. /* fill the xmit dma buffers and return */
  406. #ifdef HH_VERSION
  407. sa1100_dma_set_spin(s->dmach, FORCE_CLOCK_ADDR, FORCE_CLOCK_SIZE);
  408. #else
  409. while (1) {
  410. ret = sa1100_start_dma(s->dma_regs, FORCE_CLOCK_ADDR, FORCE_CLOCK_SIZE);
  411. if (ret)
  412. return;
  413. }
  414. #endif
  415. return;
  416. }
  417. /* must be set here - only valid for running streams, not for forced_clock dma fills */
  418. runtime = substream->runtime;
  419. while (s->active && s->periods < runtime->periods) {
  420. dma_size = frames_to_bytes(runtime, runtime->period_size);
  421. if (s->old_offset) {
  422. /* a little trick, we need resume from old position */
  423. offset = frames_to_bytes(runtime, s->old_offset - 1);
  424. s->old_offset = 0;
  425. s->periods = 0;
  426. s->period = offset / dma_size;
  427. offset %= dma_size;
  428. dma_size = dma_size - offset;
  429. if (!dma_size)
  430. continue; /* special case */
  431. } else {
  432. offset = dma_size * s->period;
  433. snd_assert(dma_size <= DMA_BUF_SIZE, );
  434. }
  435. #ifdef HH_VERSION
  436. ret = sa1100_dma_queue_buffer(s->dmach, s, runtime->dma_addr + offset, dma_size);
  437. if (ret)
  438. return; //FIXME
  439. #else
  440. ret = sa1100_start_dma((s)->dma_regs, runtime->dma_addr + offset, dma_size);
  441. if (ret) {
  442. printk(KERN_ERR "audio_process_dma: cannot queue DMA buffer (%i)\n", ret);
  443. return;
  444. }
  445. #endif
  446. s->period++;
  447. s->period %= runtime->periods;
  448. s->periods++;
  449. }
  450. }
  451. #ifdef HH_VERSION
  452. static void audio_dma_callback(void *data, int size)
  453. #else
  454. static void audio_dma_callback(void *data)
  455. #endif
  456. {
  457. struct audio_stream *s = data;
  458. /*
  459. * If we are getting a callback for an active stream then we inform
  460. * the PCM middle layer we've finished a period
  461. */
  462. if (s->active)
  463. snd_pcm_period_elapsed(s->stream);
  464. spin_lock(&s->dma_lock);
  465. if (!s->tx_spin && s->periods > 0)
  466. s->periods--;
  467. audio_process_dma(s);
  468. spin_unlock(&s->dma_lock);
  469. }
  470. /* }}} */
  471. /* {{{ PCM setting */
  472. /* {{{ trigger & timer */
  473. static int snd_sa11xx_uda1341_trigger(struct snd_pcm_substream *substream, int cmd)
  474. {
  475. struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
  476. int stream_id = substream->pstr->stream;
  477. struct audio_stream *s = &chip->s[stream_id];
  478. struct audio_stream *s1 = &chip->s[stream_id ^ 1];
  479. int err = 0;
  480. /* note local interrupts are already disabled in the midlevel code */
  481. spin_lock(&s->dma_lock);
  482. switch (cmd) {
  483. case SNDRV_PCM_TRIGGER_START:
  484. /* now we need to make sure a record only stream has a clock */
  485. if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
  486. /* we need to force fill the xmit DMA with zeros */
  487. s1->tx_spin = 1;
  488. audio_process_dma(s1);
  489. }
  490. /* this case is when you were recording then you turn on a
  491. * playback stream so we stop (also clears it) the dma first,
  492. * clear the sync flag and then we let it turned on
  493. */
  494. else {
  495. s->tx_spin = 0;
  496. }
  497. /* requested stream startup */
  498. s->active = 1;
  499. audio_process_dma(s);
  500. break;
  501. case SNDRV_PCM_TRIGGER_STOP:
  502. /* requested stream shutdown */
  503. audio_stop_dma(s);
  504. /*
  505. * now we need to make sure a record only stream has a clock
  506. * so if we're stopping a playback with an active capture
  507. * we need to turn the 0 fill dma on for the xmit side
  508. */
  509. if (stream_id == SNDRV_PCM_STREAM_PLAYBACK && s1->active) {
  510. /* we need to force fill the xmit DMA with zeros */
  511. s->tx_spin = 1;
  512. audio_process_dma(s);
  513. }
  514. /*
  515. * we killed a capture only stream, so we should also kill
  516. * the zero fill transmit
  517. */
  518. else {
  519. if (s1->tx_spin) {
  520. s1->tx_spin = 0;
  521. audio_stop_dma(s1);
  522. }
  523. }
  524. break;
  525. case SNDRV_PCM_TRIGGER_SUSPEND:
  526. s->active = 0;
  527. #ifdef HH_VERSION
  528. sa1100_dma_stop(s->dmach);
  529. #else
  530. //FIXME - DMA API
  531. #endif
  532. s->old_offset = audio_get_dma_pos(s) + 1;
  533. #ifdef HH_VERSION
  534. sa1100_dma_flush_all(s->dmach);
  535. #else
  536. //FIXME - DMA API
  537. #endif
  538. s->periods = 0;
  539. break;
  540. case SNDRV_PCM_TRIGGER_RESUME:
  541. s->active = 1;
  542. s->tx_spin = 0;
  543. audio_process_dma(s);
  544. if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
  545. s1->tx_spin = 1;
  546. audio_process_dma(s1);
  547. }
  548. break;
  549. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  550. #ifdef HH_VERSION
  551. sa1100_dma_stop(s->dmach);
  552. #else
  553. //FIXME - DMA API
  554. #endif
  555. s->active = 0;
  556. if (stream_id == SNDRV_PCM_STREAM_PLAYBACK) {
  557. if (s1->active) {
  558. s->tx_spin = 1;
  559. s->old_offset = audio_get_dma_pos(s) + 1;
  560. #ifdef HH_VERSION
  561. sa1100_dma_flush_all(s->dmach);
  562. #else
  563. //FIXME - DMA API
  564. #endif
  565. audio_process_dma(s);
  566. }
  567. } else {
  568. if (s1->tx_spin) {
  569. s1->tx_spin = 0;
  570. #ifdef HH_VERSION
  571. sa1100_dma_flush_all(s1->dmach);
  572. #else
  573. //FIXME - DMA API
  574. #endif
  575. }
  576. }
  577. break;
  578. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  579. s->active = 1;
  580. if (s->old_offset) {
  581. s->tx_spin = 0;
  582. audio_process_dma(s);
  583. break;
  584. }
  585. if (stream_id == SNDRV_PCM_STREAM_CAPTURE && !s1->active) {
  586. s1->tx_spin = 1;
  587. audio_process_dma(s1);
  588. }
  589. #ifdef HH_VERSION
  590. sa1100_dma_resume(s->dmach);
  591. #else
  592. //FIXME - DMA API
  593. #endif
  594. break;
  595. default:
  596. err = -EINVAL;
  597. break;
  598. }
  599. spin_unlock(&s->dma_lock);
  600. return err;
  601. }
  602. static int snd_sa11xx_uda1341_prepare(struct snd_pcm_substream *substream)
  603. {
  604. struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
  605. struct snd_pcm_runtime *runtime = substream->runtime;
  606. struct audio_stream *s = &chip->s[substream->pstr->stream];
  607. /* set requested samplerate */
  608. sa11xx_uda1341_set_samplerate(chip, runtime->rate);
  609. /* set requestd format when available */
  610. /* set FMT here !!! FIXME */
  611. s->period = 0;
  612. s->periods = 0;
  613. return 0;
  614. }
  615. static snd_pcm_uframes_t snd_sa11xx_uda1341_pointer(struct snd_pcm_substream *substream)
  616. {
  617. struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
  618. return audio_get_dma_pos(&chip->s[substream->pstr->stream]);
  619. }
  620. /* }}} */
  621. static struct snd_pcm_hardware snd_sa11xx_uda1341_capture =
  622. {
  623. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  624. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  625. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  626. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  627. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  628. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  629. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |\
  630. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  631. SNDRV_PCM_RATE_KNOT),
  632. .rate_min = 8000,
  633. .rate_max = 48000,
  634. .channels_min = 2,
  635. .channels_max = 2,
  636. .buffer_bytes_max = 64*1024,
  637. .period_bytes_min = 64,
  638. .period_bytes_max = DMA_BUF_SIZE,
  639. .periods_min = 2,
  640. .periods_max = 255,
  641. .fifo_size = 0,
  642. };
  643. static struct snd_pcm_hardware snd_sa11xx_uda1341_playback =
  644. {
  645. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  646. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  647. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  648. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  649. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  650. .rates = (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  651. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |\
  652. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  653. SNDRV_PCM_RATE_KNOT),
  654. .rate_min = 8000,
  655. .rate_max = 48000,
  656. .channels_min = 2,
  657. .channels_max = 2,
  658. .buffer_bytes_max = 64*1024,
  659. .period_bytes_min = 64,
  660. .period_bytes_max = DMA_BUF_SIZE,
  661. .periods_min = 2,
  662. .periods_max = 255,
  663. .fifo_size = 0,
  664. };
  665. static int snd_card_sa11xx_uda1341_open(struct snd_pcm_substream *substream)
  666. {
  667. struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
  668. struct snd_pcm_runtime *runtime = substream->runtime;
  669. int stream_id = substream->pstr->stream;
  670. int err;
  671. chip->s[stream_id].stream = substream;
  672. if (stream_id == SNDRV_PCM_STREAM_PLAYBACK)
  673. runtime->hw = snd_sa11xx_uda1341_playback;
  674. else
  675. runtime->hw = snd_sa11xx_uda1341_capture;
  676. if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
  677. return err;
  678. if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates)) < 0)
  679. return err;
  680. return 0;
  681. }
  682. static int snd_card_sa11xx_uda1341_close(struct snd_pcm_substream *substream)
  683. {
  684. struct sa11xx_uda1341 *chip = snd_pcm_substream_chip(substream);
  685. chip->s[substream->pstr->stream].stream = NULL;
  686. return 0;
  687. }
  688. /* {{{ HW params & free */
  689. static int snd_sa11xx_uda1341_hw_params(struct snd_pcm_substream *substream,
  690. struct snd_pcm_hw_params *hw_params)
  691. {
  692. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  693. }
  694. static int snd_sa11xx_uda1341_hw_free(struct snd_pcm_substream *substream)
  695. {
  696. return snd_pcm_lib_free_pages(substream);
  697. }
  698. /* }}} */
  699. static struct snd_pcm_ops snd_card_sa11xx_uda1341_playback_ops = {
  700. .open = snd_card_sa11xx_uda1341_open,
  701. .close = snd_card_sa11xx_uda1341_close,
  702. .ioctl = snd_pcm_lib_ioctl,
  703. .hw_params = snd_sa11xx_uda1341_hw_params,
  704. .hw_free = snd_sa11xx_uda1341_hw_free,
  705. .prepare = snd_sa11xx_uda1341_prepare,
  706. .trigger = snd_sa11xx_uda1341_trigger,
  707. .pointer = snd_sa11xx_uda1341_pointer,
  708. };
  709. static struct snd_pcm_ops snd_card_sa11xx_uda1341_capture_ops = {
  710. .open = snd_card_sa11xx_uda1341_open,
  711. .close = snd_card_sa11xx_uda1341_close,
  712. .ioctl = snd_pcm_lib_ioctl,
  713. .hw_params = snd_sa11xx_uda1341_hw_params,
  714. .hw_free = snd_sa11xx_uda1341_hw_free,
  715. .prepare = snd_sa11xx_uda1341_prepare,
  716. .trigger = snd_sa11xx_uda1341_trigger,
  717. .pointer = snd_sa11xx_uda1341_pointer,
  718. };
  719. static int __init snd_card_sa11xx_uda1341_pcm(struct sa11xx_uda1341 *sa11xx_uda1341, int device)
  720. {
  721. struct snd_pcm *pcm;
  722. int err;
  723. if ((err = snd_pcm_new(sa11xx_uda1341->card, "UDA1341 PCM", device, 1, 1, &pcm)) < 0)
  724. return err;
  725. /*
  726. * this sets up our initial buffers and sets the dma_type to isa.
  727. * isa works but I'm not sure why (or if) it's the right choice
  728. * this may be too large, trying it for now
  729. */
  730. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  731. snd_dma_isa_data(),
  732. 64*1024, 64*1024);
  733. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_sa11xx_uda1341_playback_ops);
  734. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_sa11xx_uda1341_capture_ops);
  735. pcm->private_data = sa11xx_uda1341;
  736. pcm->info_flags = 0;
  737. strcpy(pcm->name, "UDA1341 PCM");
  738. sa11xx_uda1341_audio_init(sa11xx_uda1341);
  739. /* setup DMA controller */
  740. audio_dma_request(&sa11xx_uda1341->s[SNDRV_PCM_STREAM_PLAYBACK], audio_dma_callback);
  741. audio_dma_request(&sa11xx_uda1341->s[SNDRV_PCM_STREAM_CAPTURE], audio_dma_callback);
  742. sa11xx_uda1341->pcm = pcm;
  743. return 0;
  744. }
  745. /* }}} */
  746. /* {{{ module init & exit */
  747. #ifdef CONFIG_PM
  748. static int snd_sa11xx_uda1341_suspend(struct platform_device *devptr,
  749. pm_message_t state)
  750. {
  751. struct snd_card *card = platform_get_drvdata(devptr);
  752. struct sa11xx_uda1341 *chip = card->private_data;
  753. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  754. snd_pcm_suspend_all(chip->pcm);
  755. #ifdef HH_VERSION
  756. sa1100_dma_sleep(chip->s[SNDRV_PCM_STREAM_PLAYBACK].dmach);
  757. sa1100_dma_sleep(chip->s[SNDRV_PCM_STREAM_CAPTURE].dmach);
  758. #else
  759. //FIXME
  760. #endif
  761. l3_command(chip->uda1341, CMD_SUSPEND, NULL);
  762. sa11xx_uda1341_audio_shutdown(chip);
  763. return 0;
  764. }
  765. static int snd_sa11xx_uda1341_resume(struct platform_device *devptr)
  766. {
  767. struct snd_card *card = platform_get_drvdata(devptr);
  768. struct sa11xx_uda1341 *chip = card->private_data;
  769. sa11xx_uda1341_audio_init(chip);
  770. l3_command(chip->uda1341, CMD_RESUME, NULL);
  771. #ifdef HH_VERSION
  772. sa1100_dma_wakeup(chip->s[SNDRV_PCM_STREAM_PLAYBACK].dmach);
  773. sa1100_dma_wakeup(chip->s[SNDRV_PCM_STREAM_CAPTURE].dmach);
  774. #else
  775. //FIXME
  776. #endif
  777. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  778. return 0;
  779. }
  780. #endif /* COMFIG_PM */
  781. void snd_sa11xx_uda1341_free(struct snd_card *card)
  782. {
  783. struct sa11xx_uda1341 *chip = card->private_data;
  784. audio_dma_free(&chip->s[SNDRV_PCM_STREAM_PLAYBACK]);
  785. audio_dma_free(&chip->s[SNDRV_PCM_STREAM_CAPTURE]);
  786. }
  787. static int __init sa11xx_uda1341_probe(struct platform_device *devptr)
  788. {
  789. int err;
  790. struct snd_card *card;
  791. struct sa11xx_uda1341 *chip;
  792. /* register the soundcard */
  793. card = snd_card_new(-1, id, THIS_MODULE, sizeof(struct sa11xx_uda1341));
  794. if (card == NULL)
  795. return -ENOMEM;
  796. chip = card->private_data;
  797. spin_lock_init(&chip->s[0].dma_lock);
  798. spin_lock_init(&chip->s[1].dma_lock);
  799. card->private_free = snd_sa11xx_uda1341_free;
  800. chip->card = card;
  801. chip->samplerate = AUDIO_RATE_DEFAULT;
  802. // mixer
  803. if ((err = snd_chip_uda1341_mixer_new(card, &chip->uda1341)))
  804. goto nodev;
  805. // PCM
  806. if ((err = snd_card_sa11xx_uda1341_pcm(chip, 0)) < 0)
  807. goto nodev;
  808. strcpy(card->driver, "UDA1341");
  809. strcpy(card->shortname, "H3600 UDA1341TS");
  810. sprintf(card->longname, "Compaq iPAQ H3600 with Philips UDA1341TS");
  811. snd_card_set_dev(card, &devptr->dev);
  812. if ((err = snd_card_register(card)) == 0) {
  813. printk( KERN_INFO "iPAQ audio support initialized\n" );
  814. platform_set_drvdata(devptr, card);
  815. return 0;
  816. }
  817. nodev:
  818. snd_card_free(card);
  819. return err;
  820. }
  821. static int __devexit sa11xx_uda1341_remove(struct platform_device *devptr)
  822. {
  823. snd_card_free(platform_get_drvdata(devptr));
  824. platform_set_drvdata(devptr, NULL);
  825. return 0;
  826. }
  827. #define SA11XX_UDA1341_DRIVER "sa11xx_uda1341"
  828. static struct platform_driver sa11xx_uda1341_driver = {
  829. .probe = sa11xx_uda1341_probe,
  830. .remove = __devexit_p(sa11xx_uda1341_remove),
  831. #ifdef CONFIG_PM
  832. .suspend = snd_sa11xx_uda1341_suspend,
  833. .resume = snd_sa11xx_uda1341_resume,
  834. #endif
  835. .driver = {
  836. .name = SA11XX_UDA1341_DRIVER,
  837. },
  838. };
  839. static int __init sa11xx_uda1341_init(void)
  840. {
  841. int err;
  842. if (!machine_is_h3xxx())
  843. return -ENODEV;
  844. if ((err = platform_driver_register(&sa11xx_uda1341_driver)) < 0)
  845. return err;
  846. device = platform_device_register_simple(SA11XX_UDA1341_DRIVER, -1, NULL, 0);
  847. if (IS_ERR(device)) {
  848. platform_driver_unregister(&sa11xx_uda1341_driver);
  849. return PTR_ERR(device);
  850. }
  851. return 0;
  852. }
  853. static void __exit sa11xx_uda1341_exit(void)
  854. {
  855. platform_device_unregister(device);
  856. platform_driver_unregister(&sa11xx_uda1341_driver);
  857. }
  858. module_init(sa11xx_uda1341_init);
  859. module_exit(sa11xx_uda1341_exit);
  860. /* }}} */
  861. /*
  862. * Local variables:
  863. * indent-tabs-mode: t
  864. * End:
  865. */