sh_dac_audio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * sound/oss/sh_dac_audio.c
  3. *
  4. * SH DAC based sound :(
  5. *
  6. * Copyright (C) 2004,2005 Andriy Skulysh
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <linux/linkage.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/sound.h>
  19. #include <linux/soundcard.h>
  20. #include <linux/interrupt.h>
  21. #include <asm/io.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/irq.h>
  24. #include <asm/delay.h>
  25. #include <asm/clock.h>
  26. #include <asm/cpu/dac.h>
  27. #include <asm/cpu/timer.h>
  28. #include <asm/machvec.h>
  29. #include <asm/hp6xx.h>
  30. #include <asm/hd64461.h>
  31. #define MODNAME "sh_dac_audio"
  32. #define TMU_TOCR_INIT 0x00
  33. #define TMU1_TCR_INIT 0x0020 /* Clock/4, rising edge; interrupt on */
  34. #define TMU1_TSTR_INIT 0x02 /* Bit to turn on TMU1 */
  35. #define BUFFER_SIZE 48000
  36. static int rate;
  37. static int empty;
  38. static char *data_buffer, *buffer_begin, *buffer_end;
  39. static int in_use, device_major;
  40. static void dac_audio_start_timer(void)
  41. {
  42. u8 tstr;
  43. tstr = ctrl_inb(TMU_TSTR);
  44. tstr |= TMU1_TSTR_INIT;
  45. ctrl_outb(tstr, TMU_TSTR);
  46. }
  47. static void dac_audio_stop_timer(void)
  48. {
  49. u8 tstr;
  50. tstr = ctrl_inb(TMU_TSTR);
  51. tstr &= ~TMU1_TSTR_INIT;
  52. ctrl_outb(tstr, TMU_TSTR);
  53. }
  54. static void dac_audio_reset(void)
  55. {
  56. dac_audio_stop_timer();
  57. buffer_begin = buffer_end = data_buffer;
  58. empty = 1;
  59. }
  60. static void dac_audio_sync(void)
  61. {
  62. while (!empty)
  63. schedule();
  64. }
  65. static void dac_audio_start(void)
  66. {
  67. if (mach_is_hp6xx()) {
  68. u16 v = inw(HD64461_GPADR);
  69. v &= ~HD64461_GPADR_SPEAKER;
  70. outw(v, HD64461_GPADR);
  71. }
  72. sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  73. ctrl_outw(TMU1_TCR_INIT, TMU1_TCR);
  74. }
  75. static void dac_audio_stop(void)
  76. {
  77. dac_audio_stop_timer();
  78. if (mach_is_hp6xx()) {
  79. u16 v = inw(HD64461_GPADR);
  80. v |= HD64461_GPADR_SPEAKER;
  81. outw(v, HD64461_GPADR);
  82. }
  83. sh_dac_output(0, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  84. sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  85. }
  86. static void dac_audio_set_rate(void)
  87. {
  88. unsigned long interval;
  89. struct clk *clk;
  90. clk = clk_get("module_clk");
  91. interval = (clk_get_rate(clk) / 4) / rate;
  92. clk_put(clk);
  93. ctrl_outl(interval, TMU1_TCOR);
  94. ctrl_outl(interval, TMU1_TCNT);
  95. }
  96. static int dac_audio_ioctl(struct inode *inode, struct file *file,
  97. unsigned int cmd, unsigned long arg)
  98. {
  99. int val;
  100. switch (cmd) {
  101. case OSS_GETVERSION:
  102. return put_user(SOUND_VERSION, (int *)arg);
  103. case SNDCTL_DSP_SYNC:
  104. dac_audio_sync();
  105. return 0;
  106. case SNDCTL_DSP_RESET:
  107. dac_audio_reset();
  108. return 0;
  109. case SNDCTL_DSP_GETFMTS:
  110. return put_user(AFMT_U8, (int *)arg);
  111. case SNDCTL_DSP_SETFMT:
  112. return put_user(AFMT_U8, (int *)arg);
  113. case SNDCTL_DSP_NONBLOCK:
  114. file->f_flags |= O_NONBLOCK;
  115. return 0;
  116. case SNDCTL_DSP_GETCAPS:
  117. return 0;
  118. case SOUND_PCM_WRITE_RATE:
  119. val = *(int *)arg;
  120. if (val > 0) {
  121. rate = val;
  122. dac_audio_set_rate();
  123. }
  124. return put_user(rate, (int *)arg);
  125. case SNDCTL_DSP_STEREO:
  126. return put_user(0, (int *)arg);
  127. case SOUND_PCM_WRITE_CHANNELS:
  128. return put_user(1, (int *)arg);
  129. case SNDCTL_DSP_SETDUPLEX:
  130. return -EINVAL;
  131. case SNDCTL_DSP_PROFILE:
  132. return -EINVAL;
  133. case SNDCTL_DSP_GETBLKSIZE:
  134. return put_user(BUFFER_SIZE, (int *)arg);
  135. case SNDCTL_DSP_SETFRAGMENT:
  136. return 0;
  137. default:
  138. printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
  139. cmd);
  140. return -EINVAL;
  141. }
  142. return -EINVAL;
  143. }
  144. static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
  145. loff_t * ppos)
  146. {
  147. int free;
  148. int nbytes;
  149. if (count < 0)
  150. return -EINVAL;
  151. if (!count) {
  152. dac_audio_sync();
  153. return 0;
  154. }
  155. free = buffer_begin - buffer_end;
  156. if (free < 0)
  157. free += BUFFER_SIZE;
  158. if ((free == 0) && (empty))
  159. free = BUFFER_SIZE;
  160. if (count > free)
  161. count = free;
  162. if (buffer_begin > buffer_end) {
  163. if (copy_from_user((void *)buffer_end, buf, count))
  164. return -EFAULT;
  165. buffer_end += count;
  166. } else {
  167. nbytes = data_buffer + BUFFER_SIZE - buffer_end;
  168. if (nbytes > count) {
  169. if (copy_from_user((void *)buffer_end, buf, count))
  170. return -EFAULT;
  171. buffer_end += count;
  172. } else {
  173. if (copy_from_user((void *)buffer_end, buf, nbytes))
  174. return -EFAULT;
  175. if (copy_from_user
  176. ((void *)data_buffer, buf + nbytes, count - nbytes))
  177. return -EFAULT;
  178. buffer_end = data_buffer + count - nbytes;
  179. }
  180. }
  181. if (empty) {
  182. empty = 0;
  183. dac_audio_start_timer();
  184. }
  185. return count;
  186. }
  187. static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
  188. loff_t * ppos)
  189. {
  190. return -EINVAL;
  191. }
  192. static int dac_audio_open(struct inode *inode, struct file *file)
  193. {
  194. if (file->f_mode & FMODE_READ)
  195. return -ENODEV;
  196. if (in_use)
  197. return -EBUSY;
  198. in_use = 1;
  199. dac_audio_start();
  200. return 0;
  201. }
  202. static int dac_audio_release(struct inode *inode, struct file *file)
  203. {
  204. dac_audio_sync();
  205. dac_audio_stop();
  206. in_use = 0;
  207. return 0;
  208. }
  209. const struct file_operations dac_audio_fops = {
  210. .read = dac_audio_read,
  211. .write = dac_audio_write,
  212. .ioctl = dac_audio_ioctl,
  213. .open = dac_audio_open,
  214. .release = dac_audio_release,
  215. };
  216. static irqreturn_t timer1_interrupt(int irq, void *dev)
  217. {
  218. unsigned long timer_status;
  219. timer_status = ctrl_inw(TMU1_TCR);
  220. timer_status &= ~0x100;
  221. ctrl_outw(timer_status, TMU1_TCR);
  222. if (!empty) {
  223. sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  224. buffer_begin++;
  225. if (buffer_begin == data_buffer + BUFFER_SIZE)
  226. buffer_begin = data_buffer;
  227. if (buffer_begin == buffer_end) {
  228. empty = 1;
  229. dac_audio_stop_timer();
  230. }
  231. }
  232. return IRQ_HANDLED;
  233. }
  234. static int __init dac_audio_init(void)
  235. {
  236. int retval;
  237. if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
  238. printk(KERN_ERR "Cannot register dsp device");
  239. return device_major;
  240. }
  241. in_use = 0;
  242. data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  243. if (data_buffer == NULL)
  244. return -ENOMEM;
  245. dac_audio_reset();
  246. rate = 8000;
  247. dac_audio_set_rate();
  248. retval =
  249. request_irq(TIMER1_IRQ, timer1_interrupt, IRQF_DISABLED, MODNAME, 0);
  250. if (retval < 0) {
  251. printk(KERN_ERR "sh_dac_audio: IRQ %d request failed\n",
  252. TIMER1_IRQ);
  253. return retval;
  254. }
  255. return 0;
  256. }
  257. static void __exit dac_audio_exit(void)
  258. {
  259. free_irq(TIMER1_IRQ, 0);
  260. unregister_sound_dsp(device_major);
  261. kfree((void *)data_buffer);
  262. }
  263. module_init(dac_audio_init);
  264. module_exit(dac_audio_exit);
  265. MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
  266. MODULE_DESCRIPTION("SH DAC sound driver");
  267. MODULE_LICENSE("GPL");