sh_dac_audio.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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) {
  138. dac_audio_sync();
  139. return 0;
  140. }
  141. free = buffer_begin - buffer_end;
  142. if (free < 0)
  143. free += BUFFER_SIZE;
  144. if ((free == 0) && (empty))
  145. free = BUFFER_SIZE;
  146. if (count > free)
  147. count = free;
  148. if (buffer_begin > buffer_end) {
  149. if (copy_from_user((void *)buffer_end, buf, count))
  150. return -EFAULT;
  151. buffer_end += count;
  152. } else {
  153. nbytes = data_buffer + BUFFER_SIZE - buffer_end;
  154. if (nbytes > count) {
  155. if (copy_from_user((void *)buffer_end, buf, count))
  156. return -EFAULT;
  157. buffer_end += count;
  158. } else {
  159. if (copy_from_user((void *)buffer_end, buf, nbytes))
  160. return -EFAULT;
  161. if (copy_from_user
  162. ((void *)data_buffer, buf + nbytes, count - nbytes))
  163. return -EFAULT;
  164. buffer_end = data_buffer + count - nbytes;
  165. }
  166. }
  167. if (empty) {
  168. empty = 0;
  169. dac_audio_start_timer();
  170. }
  171. return count;
  172. }
  173. static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
  174. loff_t * ppos)
  175. {
  176. return -EINVAL;
  177. }
  178. static int dac_audio_open(struct inode *inode, struct file *file)
  179. {
  180. if (file->f_mode & FMODE_READ)
  181. return -ENODEV;
  182. if (in_use)
  183. return -EBUSY;
  184. in_use = 1;
  185. dac_audio_start();
  186. return 0;
  187. }
  188. static int dac_audio_release(struct inode *inode, struct file *file)
  189. {
  190. dac_audio_sync();
  191. dac_audio_stop();
  192. in_use = 0;
  193. return 0;
  194. }
  195. const struct file_operations dac_audio_fops = {
  196. .read = dac_audio_read,
  197. .write = dac_audio_write,
  198. .ioctl = dac_audio_ioctl,
  199. .open = dac_audio_open,
  200. .release = dac_audio_release,
  201. };
  202. static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
  203. {
  204. if (!empty) {
  205. sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  206. buffer_begin++;
  207. if (buffer_begin == data_buffer + BUFFER_SIZE)
  208. buffer_begin = data_buffer;
  209. if (buffer_begin == buffer_end)
  210. empty = 1;
  211. }
  212. if (!empty)
  213. hrtimer_start(&hrtimer, wakeups_per_second, HRTIMER_MODE_REL);
  214. return HRTIMER_NORESTART;
  215. }
  216. static int __init dac_audio_init(void)
  217. {
  218. if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
  219. printk(KERN_ERR "Cannot register dsp device");
  220. return device_major;
  221. }
  222. in_use = 0;
  223. data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  224. if (data_buffer == NULL)
  225. return -ENOMEM;
  226. dac_audio_reset();
  227. rate = 8000;
  228. dac_audio_set_rate();
  229. /* Today: High Resolution Timer driven DAC playback.
  230. * The timer callback gets called once per sample. Ouch.
  231. *
  232. * Future: A much better approach would be to use the
  233. * SH7720 CMT+DMAC+DAC hardware combination like this:
  234. * - Program sample rate using CMT0 or CMT1
  235. * - Program DMAC to use CMT for timing and output to DAC
  236. * - Play sound using DMAC, let CPU sleep.
  237. * - While at it, rewrite this driver to use ALSA.
  238. */
  239. hrtimer_init(&hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  240. hrtimer.function = sh_dac_audio_timer;
  241. return 0;
  242. }
  243. static void __exit dac_audio_exit(void)
  244. {
  245. unregister_sound_dsp(device_major);
  246. kfree((void *)data_buffer);
  247. }
  248. module_init(dac_audio_init);
  249. module_exit(dac_audio_exit);
  250. MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
  251. MODULE_DESCRIPTION("SH DAC sound driver");
  252. MODULE_LICENSE("GPL");