h3600-uda1341.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:8k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * Glue audio driver for the Compaq iPAQ H3600 & Philips UDA1341 codec.
  3.  *
  4.  * Copyright (c) 2000 Nicolas Pitre <nico@cam.org>
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License.
  8.  *
  9.  * This is the machine specific part of the Compaq iPAQ (aka Bitsy) support.
  10.  * This driver makes use of the UDA1341 and the sa1100-audio modules.
  11.  *
  12.  * History:
  13.  *
  14.  * 2000-05-21 Nicolas Pitre Initial UDA1341 driver release.
  15.  *
  16.  * 2000-07-?? George France Bitsy support.
  17.  *
  18.  * 2000-12-13 Deborah Wallach Fixed power handling for iPAQ/h3600
  19.  *
  20.  * 2001-06-03 Nicolas Pitre Made this file a separate module, based on
  21.  * the former sa1100-uda1341.c driver.
  22.  *
  23.  * 2001-07-13 Nicolas Pitre Fixes for all supported samplerates.
  24.  *
  25.  */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/fs.h>
  30. #include <linux/delay.h>
  31. #include <linux/pm.h>
  32. #include <linux/errno.h>
  33. #include <linux/sound.h>
  34. #include <linux/soundcard.h>
  35. #include <linux/l3/l3.h>
  36. #include <linux/l3/uda1341.h>
  37. #include <asm/semaphore.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/hardware.h>
  40. #include <asm/dma.h>
  41. //#include <asm/arch/h3600_hal.h>
  42. #include "sa1100-audio.h"
  43. #undef DEBUG
  44. #ifdef DEBUG
  45. #define DPRINTK( x... )  printk( ##x )
  46. #else
  47. #define DPRINTK( x... )
  48. #endif
  49. #define AUDIO_NAME "Bitsy_UDA1341"
  50. #define AUDIO_RATE_DEFAULT 44100
  51. static struct l3_client uda1341;
  52. static int
  53. mixer_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
  54. {
  55. /*
  56.  * We only accept mixer (type 'M') ioctls.
  57.  */
  58. if (_IOC_TYPE(cmd) != 'M')
  59. return -EINVAL;
  60. return l3_command(&uda1341, cmd, (void *)arg);
  61. }
  62. static struct file_operations h3600_mixer_fops = {
  63. ioctl: mixer_ioctl,
  64. owner: THIS_MODULE
  65. };
  66. /*
  67.  * Audio interface
  68.  */
  69. static long audio_samplerate = AUDIO_RATE_DEFAULT;
  70. /*
  71.  * Stop-gap solution until rest of hh.org HAL stuff is merged.
  72.  */
  73. #define GPIO_H3600_CLK_SET0 GPIO_GPIO (12)
  74. #define GPIO_H3600_CLK_SET1 GPIO_GPIO (13)
  75. static void h3600_set_audio_clock(long val)
  76. {
  77. switch (val) {
  78. case 24000: case 32000: case 48000: /* 00: 12.288 MHz */
  79. GPCR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1;
  80. break;
  81. case 22050: case 29400: case 44100: /* 01: 11.2896 MHz */
  82. GPSR = GPIO_H3600_CLK_SET0;
  83. GPCR = GPIO_H3600_CLK_SET1;
  84. break;
  85. case 8000: case 10666: case 16000: /* 10: 4.096 MHz */
  86. GPCR = GPIO_H3600_CLK_SET0;
  87. GPSR = GPIO_H3600_CLK_SET1;
  88. break;
  89. case 10985: case 14647: case 21970: /* 11: 5.6245 MHz */
  90. GPSR = GPIO_H3600_CLK_SET0 | GPIO_H3600_CLK_SET1;
  91. break;
  92. }
  93. }
  94. static void h3600_set_samplerate(long val)
  95. {
  96. struct uda1341_cfg cfg;
  97. int clk_div = 0;
  98. /* We don't want to mess with clocks when frames are in flight */
  99. Ser4SSCR0 &= ~SSCR0_SSE;
  100. /* wait for any frame to complete */
  101. udelay(125);
  102. /*
  103.  * We have the following clock sources:
  104.  * 4.096 MHz, 5.6245 MHz, 11.2896 MHz, 12.288 MHz
  105.  * Those can be divided either by 256, 384 or 512.
  106.  * This makes up 12 combinations for the following samplerates...
  107.  */
  108. if (val >= 48000)
  109. val = 48000;
  110. else if (val >= 44100)
  111. val = 44100;
  112. else if (val >= 32000)
  113. val = 32000;
  114. else if (val >= 29400)
  115. val = 29400;
  116. else if (val >= 24000)
  117. val = 24000;
  118. else if (val >= 22050)
  119. val = 22050;
  120. else if (val >= 21970)
  121. val = 21970;
  122. else if (val >= 16000)
  123. val = 16000;
  124. else if (val >= 14647)
  125. val = 14647;
  126. else if (val >= 10985)
  127. val = 10985;
  128. else if (val >= 10666)
  129. val = 10666;
  130. else
  131. val = 8000;
  132. /* Set the external clock generator */
  133. h3600_set_audio_clock(val);
  134. /* Select the clock divisor */
  135. switch (val) {
  136. case 8000:
  137. case 10985:
  138. case 22050:
  139. case 24000:
  140. cfg.fs = 512;
  141. clk_div = SSCR0_SerClkDiv(16);
  142. break;
  143. case 16000:
  144. case 21970:
  145. case 44100:
  146. case 48000:
  147. cfg.fs = 256;
  148. clk_div = SSCR0_SerClkDiv(8);
  149. break;
  150. case 10666:
  151. case 14647:
  152. case 29400:
  153. case 32000:
  154. cfg.fs = 384;
  155. clk_div = SSCR0_SerClkDiv(12);
  156. break;
  157. }
  158. cfg.format = FMT_LSB16;
  159. l3_command(&uda1341, L3_UDA1341_CONFIGURE, &cfg);
  160. Ser4SSCR0 = (Ser4SSCR0 & ~0xff00) + clk_div + SSCR0_SSE;
  161. audio_samplerate = val;
  162. }
  163. static void h3600_audio_init(void *dummy)
  164. {
  165. unsigned long flags;
  166. /* Setup the uarts */
  167. local_irq_save(flags);
  168. GAFR |= (GPIO_SSP_CLK);
  169. GPDR &= ~(GPIO_SSP_CLK);
  170. Ser4SSCR0 = 0;
  171. Ser4SSCR0 = SSCR0_DataSize(16) + SSCR0_TI + SSCR0_SerClkDiv(8);
  172. Ser4SSCR1 = SSCR1_SClkIactL + SSCR1_SClk1P + SSCR1_ExtClk;
  173. Ser4SSCR0 |= SSCR0_SSE;
  174. /* Enable the audio power */
  175. clr_h3600_egpio(IPAQ_EGPIO_CODEC_NRESET);
  176. set_h3600_egpio(IPAQ_EGPIO_AUDIO_ON);
  177. set_h3600_egpio(IPAQ_EGPIO_QMUTE);
  178. local_irq_restore(flags);
  179. /* external clock configuration */
  180. h3600_set_samplerate(audio_samplerate);
  181. /* Wait for the UDA1341 to wake up */
  182. set_h3600_egpio(IPAQ_EGPIO_CODEC_NRESET);
  183. mdelay(1);
  184. /* make the left and right channels unswapped (flip the WS latch ) */
  185. Ser4SSDR = 0;
  186. /* Initialize the UDA1341 internal state */
  187. l3_open(&uda1341);
  188. clr_h3600_egpio(IPAQ_EGPIO_QMUTE);
  189. }
  190. static void h3600_audio_shutdown(void *dummy)
  191. {
  192. /* disable the audio power and all signals leading to the audio chip */
  193. l3_close(&uda1341);
  194. Ser4SSCR0 = 0;
  195. clr_h3600_egpio(IPAQ_EGPIO_CODEC_NRESET);
  196. clr_h3600_egpio(IPAQ_EGPIO_AUDIO_ON);
  197. clr_h3600_egpio(IPAQ_EGPIO_QMUTE);
  198. }
  199. static int h3600_audio_ioctl(struct inode *inode, struct file *file,
  200.      uint cmd, ulong arg)
  201. {
  202. long val;
  203. int ret = 0;
  204. /*
  205.  * These are platform dependent ioctls which are not handled by the
  206.  * generic sa1100-audio module.
  207.  */
  208. switch (cmd) {
  209. case SNDCTL_DSP_STEREO:
  210. ret = get_user(val, (int *) arg);
  211. if (ret)
  212. return ret;
  213. /* the UDA1341 is stereo only */
  214. ret = (val == 0) ? -EINVAL : 1;
  215. return put_user(ret, (int *) arg);
  216. case SNDCTL_DSP_CHANNELS:
  217. case SOUND_PCM_READ_CHANNELS:
  218. /* the UDA1341 is stereo only */
  219. return put_user(2, (long *) arg);
  220. case SNDCTL_DSP_SPEED:
  221. ret = get_user(val, (long *) arg);
  222. if (ret) break;
  223. h3600_set_samplerate(val);
  224. /* fall through */
  225. case SOUND_PCM_READ_RATE:
  226. return put_user(audio_samplerate, (long *) arg);
  227. case SNDCTL_DSP_SETFMT:
  228. case SNDCTL_DSP_GETFMTS:
  229. /* we can do 16-bit only */
  230. return put_user(AFMT_S16_LE, (long *) arg);
  231. default:
  232. /* Maybe this is meant for the mixer (As per OSS Docs) */
  233. return mixer_ioctl(inode, file, cmd, arg);
  234. }
  235. return ret;
  236. }
  237. static audio_stream_t output_stream, input_stream;
  238. static audio_state_t audio_state = {
  239. output_stream: &output_stream,
  240. output_dma: DMA_Ser4SSPWr,
  241. output_id: "UDA1341 out",
  242. input_stream: &input_stream,
  243. input_dma: DMA_Ser4SSPRd,
  244. input_id: "UDA1341 in",
  245. need_tx_for_rx: 1,
  246. hw_init: h3600_audio_init,
  247. hw_shutdown: h3600_audio_shutdown,
  248. client_ioctl: h3600_audio_ioctl,
  249. sem: __MUTEX_INITIALIZER(audio_state.sem),
  250. };
  251. static int h3600_audio_open(struct inode *inode, struct file *file)
  252. {
  253. return sa1100_audio_attach(inode, file, &audio_state);
  254. }
  255. /*
  256.  * Missing fields of this structure will be patched with the call
  257.  * to sa1100_audio_attach().
  258.  */
  259. static struct file_operations h3600_audio_fops = {
  260. open: h3600_audio_open,
  261. owner: THIS_MODULE
  262. };
  263. static int audio_dev_id, mixer_dev_id;
  264. static int __init h3600_uda1341_init(void)
  265. {
  266. int ret;
  267. if (!machine_is_h3xxx())
  268. return -ENODEV;
  269. ret = l3_attach_client(&uda1341, "l3-bit-sa1100-gpio", "uda1341");
  270. if (ret)
  271. goto out;
  272. /* register devices */
  273. audio_dev_id = register_sound_dsp(&h3600_audio_fops, -1);
  274. mixer_dev_id = register_sound_mixer(&h3600_mixer_fops, -1);
  275. printk( KERN_INFO "iPAQ audio support initializedn" );
  276. return 0;
  277. release_l3:
  278. l3_detach_client(&uda1341);
  279. out:
  280. return ret;
  281. }
  282. static void __exit h3600_uda1341_exit(void)
  283. {
  284. unregister_sound_dsp(audio_dev_id);
  285. unregister_sound_mixer(mixer_dev_id);
  286. l3_detach_client(&uda1341);
  287. }
  288. module_init(h3600_uda1341_init);
  289. module_exit(h3600_uda1341_exit);
  290. MODULE_AUTHOR("Nicolas Pitre, George France");
  291. MODULE_DESCRIPTION("Glue audio driver for the Compaq iPAQ H3600 & Philips UDA1341 codec.");
  292. EXPORT_NO_SYMBOLS;