sa11xx-uda1341.c 26 KB

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