sh_dac_audio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 <linux/hrtimer.h>
  22. #include <asm/io.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/irq.h>
  25. #include <asm/delay.h>
  26. #include <asm/clock.h>
  27. #include <cpu/dac.h>
  28. #include <asm/machvec.h>
  29. #include <mach/hp6xx.h>
  30. #include <asm/hd64461.h>
  31. #define MODNAME "sh_dac_audio"
  32. #define BUFFER_SIZE 48000
  33. static int rate;
  34. static int empty;
  35. static char *data_buffer, *buffer_begin, *buffer_end;
  36. static int in_use, device_major;
  37. static struct hrtimer hrtimer;
  38. static ktime_t wakeups_per_second;
  39. static void dac_audio_start_timer(void)
  40. {
  41. hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
  42. }
  43. static void dac_audio_stop_timer(void)
  44. {
  45. hrtimer_cancel(&hrtimer);
  46. }
  47. static void dac_audio_reset(void)
  48. {
  49. dac_audio_stop_timer();
  50. buffer_begin = buffer_end = data_buffer;
  51. empty = 1;
  52. }
  53. static void dac_audio_sync(void)
  54. {
  55. while (!empty)
  56. schedule();
  57. }
  58. static void dac_audio_start(void)
  59. {
  60. if (mach_is_hp6xx()) {
  61. u16 v = __raw_readw(HD64461_GPADR);
  62. v &= ~HD64461_GPADR_SPEAKER;
  63. __raw_writew(v, HD64461_GPADR);
  64. }
  65. sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  66. }
  67. static void dac_audio_stop(void)
  68. {
  69. dac_audio_stop_timer();
  70. if (mach_is_hp6xx()) {
  71. u16 v = __raw_readw(HD64461_GPADR);
  72. v |= HD64461_GPADR_SPEAKER;
  73. __raw_writew(v, HD64461_GPADR);
  74. }
  75. sh_dac_output(0, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  76. sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  77. }
  78. static void dac_audio_set_rate(void)
  79. {
  80. wakeups_per_second = ktime_set(0, 1000000000 / rate);
  81. }
  82. static int dac_audio_ioctl(struct inode *inode, struct file *file,
  83. unsigned int cmd, unsigned long arg)
  84. {
  85. int val;
  86. switch (cmd) {
  87. case OSS_GETVERSION:
  88. return put_user(SOUND_VERSION, (int *)arg);
  89. case SNDCTL_DSP_SYNC:
  90. dac_audio_sync();
  91. return 0;
  92. case SNDCTL_DSP_RESET:
  93. dac_audio_reset();
  94. return 0;
  95. case SNDCTL_DSP_GETFMTS:
  96. return put_user(AFMT_U8, (int *)arg);
  97. case SNDCTL_DSP_SETFMT:
  98. return put_user(AFMT_U8, (int *)arg);
  99. case SNDCTL_DSP_NONBLOCK:
  100. spin_lock(&file->f_lock);
  101. file->f_flags |= O_NONBLOCK;
  102. spin_unlock(&file->f_lock);
  103. return 0;
  104. case SNDCTL_DSP_GETCAPS:
  105. return 0;
  106. case SOUND_PCM_WRITE_RATE:
  107. val = *(int *)arg;
  108. if (val > 0) {
  109. rate = val;
  110. dac_audio_set_rate();
  111. }
  112. return put_user(rate, (int *)arg);
  113. case SNDCTL_DSP_STEREO:
  114. return put_user(0, (int *)arg);
  115. case SOUND_PCM_WRITE_CHANNELS:
  116. return put_user(1, (int *)arg);
  117. case SNDCTL_DSP_SETDUPLEX:
  118. return -EINVAL;
  119. case SNDCTL_DSP_PROFILE:
  120. return -EINVAL;
  121. case SNDCTL_DSP_GETBLKSIZE:
  122. return put_user(BUFFER_SIZE, (int *)arg);
  123. case SNDCTL_DSP_SETFRAGMENT:
  124. return 0;
  125. default:
  126. printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
  127. cmd);
  128. return -EINVAL;
  129. }
  130. return -EINVAL;
  131. }
  132. static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
  133. loff_t * ppos)
  134. {
  135. int free;
  136. int nbytes;
  137. if (count < 0)
  138. return -EINVAL;
  139. if (!count) {
  140. dac_audio_sync();
  141. return 0;
  142. }
  143. free = buffer_begin - buffer_end;
  144. if (free < 0)
  145. free += BUFFER_SIZE;
  146. if ((free == 0) && (empty))
  147. free = BUFFER_SIZE;
  148. if (count > free)
  149. count = free;
  150. if (buffer_begin > buffer_end) {
  151. if (copy_from_user((void *)buffer_end, buf, count))
  152. return -EFAULT;
  153. buffer_end += count;
  154. } else {
  155. nbytes = data_buffer + BUFFER_SIZE - buffer_end;
  156. if (nbytes > count) {
  157. if (copy_from_user((void *)buffer_end, buf, count))
  158. return -EFAULT;
  159. buffer_end += count;
  160. } else {
  161. if (copy_from_user((void *)buffer_end, buf, nbytes))
  162. return -EFAULT;
  163. if (copy_from_user
  164. ((void *)data_buffer, buf + nbytes, count - nbytes))
  165. return -EFAULT;
  166. buffer_end = data_buffer + count - nbytes;
  167. }
  168. }
  169. if (empty) {
  170. empty = 0;
  171. dac_audio_start_timer();
  172. }
  173. return count;
  174. }
  175. static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
  176. loff_t * ppos)
  177. {
  178. return -EINVAL;
  179. }
  180. static int dac_audio_open(struct inode *inode, struct file *file)
  181. {
  182. if (file->f_mode & FMODE_READ)
  183. return -ENODEV;
  184. if (in_use)
  185. return -EBUSY;
  186. in_use = 1;
  187. dac_audio_start();
  188. return 0;
  189. }
  190. static int dac_audio_release(struct inode *inode, struct file *file)
  191. {
  192. dac_audio_sync();
  193. dac_audio_stop();
  194. in_use = 0;
  195. return 0;
  196. }
  197. const struct file_operations dac_audio_fops = {
  198. .read = dac_audio_read,
  199. .write = dac_audio_write,
  200. .ioctl = dac_audio_ioctl,
  201. .open = dac_audio_open,
  202. .release = dac_audio_release,
  203. };
  204. static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
  205. {
  206. if (!empty) {
  207. sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  208. buffer_begin++;
  209. if (buffer_begin == data_buffer + BUFFER_SIZE)
  210. buffer_begin = data_buffer;
  211. if (buffer_begin == buffer_end)
  212. empty = 1;
  213. }
  214. if (!empty)
  215. hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
  216. return HRTIMER_NORESTART;
  217. }
  218. static int __init dac_audio_init(void)
  219. {
  220. if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
  221. printk(KERN_ERR "Cannot register dsp device");
  222. return device_major;
  223. }
  224. in_use = 0;
  225. data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  226. if (data_buffer == NULL)
  227. return -ENOMEM;
  228. dac_audio_reset();
  229. rate = 8000;
  230. dac_audio_set_rate();
  231. /* Today: High Resolution Timer driven DAC playback.
  232. * The timer callback gets called once per sample. Ouch.
  233. *
  234. * Future: A much better approach would be to use the
  235. * SH7720 CMT+DMAC+DAC hardware combination like this:
  236. * - Program sample rate using CMT0 or CMT1
  237. * - Program DMAC to use CMT for timing and output to DAC
  238. * - Play sound using DMAC, let CPU sleep.
  239. * - While at it, rewrite this driver to use ALSA.
  240. */
  241. hrtimer_init(&hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  242. hrtimer.function = sh_dac_audio_timer;
  243. return 0;
  244. }
  245. static void __exit dac_audio_exit(void)
  246. {
  247. unregister_sound_dsp(device_major);
  248. kfree((void *)data_buffer);
  249. }
  250. module_init(dac_audio_init);
  251. module_exit(dac_audio_exit);
  252. MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
  253. MODULE_DESCRIPTION("SH DAC sound driver");
  254. MODULE_LICENSE("GPL");