sh_dac_audio.c 6.6 KB

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