sh_dac_audio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 <mach/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(NULL, "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. spin_lock(&file->f_lock);
  115. file->f_flags |= O_NONBLOCK;
  116. spin_unlock(&file->f_lock);
  117. return 0;
  118. case SNDCTL_DSP_GETCAPS:
  119. return 0;
  120. case SOUND_PCM_WRITE_RATE:
  121. val = *(int *)arg;
  122. if (val > 0) {
  123. rate = val;
  124. dac_audio_set_rate();
  125. }
  126. return put_user(rate, (int *)arg);
  127. case SNDCTL_DSP_STEREO:
  128. return put_user(0, (int *)arg);
  129. case SOUND_PCM_WRITE_CHANNELS:
  130. return put_user(1, (int *)arg);
  131. case SNDCTL_DSP_SETDUPLEX:
  132. return -EINVAL;
  133. case SNDCTL_DSP_PROFILE:
  134. return -EINVAL;
  135. case SNDCTL_DSP_GETBLKSIZE:
  136. return put_user(BUFFER_SIZE, (int *)arg);
  137. case SNDCTL_DSP_SETFRAGMENT:
  138. return 0;
  139. default:
  140. printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
  141. cmd);
  142. return -EINVAL;
  143. }
  144. return -EINVAL;
  145. }
  146. static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
  147. loff_t * ppos)
  148. {
  149. int free;
  150. int nbytes;
  151. if (count < 0)
  152. return -EINVAL;
  153. if (!count) {
  154. dac_audio_sync();
  155. return 0;
  156. }
  157. free = buffer_begin - buffer_end;
  158. if (free < 0)
  159. free += BUFFER_SIZE;
  160. if ((free == 0) && (empty))
  161. free = BUFFER_SIZE;
  162. if (count > free)
  163. count = free;
  164. if (buffer_begin > buffer_end) {
  165. if (copy_from_user((void *)buffer_end, buf, count))
  166. return -EFAULT;
  167. buffer_end += count;
  168. } else {
  169. nbytes = data_buffer + BUFFER_SIZE - buffer_end;
  170. if (nbytes > count) {
  171. if (copy_from_user((void *)buffer_end, buf, count))
  172. return -EFAULT;
  173. buffer_end += count;
  174. } else {
  175. if (copy_from_user((void *)buffer_end, buf, nbytes))
  176. return -EFAULT;
  177. if (copy_from_user
  178. ((void *)data_buffer, buf + nbytes, count - nbytes))
  179. return -EFAULT;
  180. buffer_end = data_buffer + count - nbytes;
  181. }
  182. }
  183. if (empty) {
  184. empty = 0;
  185. dac_audio_start_timer();
  186. }
  187. return count;
  188. }
  189. static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
  190. loff_t * ppos)
  191. {
  192. return -EINVAL;
  193. }
  194. static int dac_audio_open(struct inode *inode, struct file *file)
  195. {
  196. if (file->f_mode & FMODE_READ)
  197. return -ENODEV;
  198. if (in_use)
  199. return -EBUSY;
  200. in_use = 1;
  201. dac_audio_start();
  202. return 0;
  203. }
  204. static int dac_audio_release(struct inode *inode, struct file *file)
  205. {
  206. dac_audio_sync();
  207. dac_audio_stop();
  208. in_use = 0;
  209. return 0;
  210. }
  211. const struct file_operations dac_audio_fops = {
  212. .read = dac_audio_read,
  213. .write = dac_audio_write,
  214. .ioctl = dac_audio_ioctl,
  215. .open = dac_audio_open,
  216. .release = dac_audio_release,
  217. };
  218. static irqreturn_t timer1_interrupt(int irq, void *dev)
  219. {
  220. unsigned long timer_status;
  221. timer_status = ctrl_inw(TMU1_TCR);
  222. timer_status &= ~0x100;
  223. ctrl_outw(timer_status, TMU1_TCR);
  224. if (!empty) {
  225. sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  226. buffer_begin++;
  227. if (buffer_begin == data_buffer + BUFFER_SIZE)
  228. buffer_begin = data_buffer;
  229. if (buffer_begin == buffer_end) {
  230. empty = 1;
  231. dac_audio_stop_timer();
  232. }
  233. }
  234. return IRQ_HANDLED;
  235. }
  236. static int __init dac_audio_init(void)
  237. {
  238. int retval;
  239. if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
  240. printk(KERN_ERR "Cannot register dsp device");
  241. return device_major;
  242. }
  243. in_use = 0;
  244. data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  245. if (data_buffer == NULL)
  246. return -ENOMEM;
  247. dac_audio_reset();
  248. rate = 8000;
  249. dac_audio_set_rate();
  250. retval =
  251. request_irq(TIMER1_IRQ, timer1_interrupt, IRQF_DISABLED, MODNAME, 0);
  252. if (retval < 0) {
  253. printk(KERN_ERR "sh_dac_audio: IRQ %d request failed\n",
  254. TIMER1_IRQ);
  255. return retval;
  256. }
  257. return 0;
  258. }
  259. static void __exit dac_audio_exit(void)
  260. {
  261. free_irq(TIMER1_IRQ, 0);
  262. unregister_sound_dsp(device_major);
  263. kfree((void *)data_buffer);
  264. }
  265. module_init(dac_audio_init);
  266. module_exit(dac_audio_exit);
  267. MODULE_AUTHOR("Andriy Skulysh, askulysh@image.kiev.ua");
  268. MODULE_DESCRIPTION("SH DAC sound driver");
  269. MODULE_LICENSE("GPL");