sh_dac_audio.c 6.7 KB

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