sa11xx-uda1341.c 27 KB

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