sh_dac_audio.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/sched.h>
  4. #include <linux/linkage.h>
  5. #include <linux/slab.h>
  6. #include <linux/fs.h>
  7. #include <linux/sound.h>
  8. #include <linux/soundcard.h>
  9. #include <linux/interrupt.h>
  10. #include <asm/io.h>
  11. #include <asm/uaccess.h>
  12. #include <asm/irq.h>
  13. #include <asm/delay.h>
  14. #include <asm/cpu/dac.h>
  15. #include <asm/machvec.h>
  16. #include <asm/hp6xx/hp6xx.h>
  17. #include <asm/hd64461/hd64461.h>
  18. #define MODNAME "sh_dac_audio"
  19. #define TMU_TOCR_INIT 0x00
  20. #define TMU1_TCR_INIT 0x0020 /* Clock/4, rising edge; interrupt on */
  21. #define TMU1_TSTR_INIT 0x02 /* Bit to turn on TMU1 */
  22. #define TMU_TSTR 0xfffffe92
  23. #define TMU1_TCOR 0xfffffea0
  24. #define TMU1_TCNT 0xfffffea4
  25. #define TMU1_TCR 0xfffffea8
  26. #define BUFFER_SIZE 48000
  27. static int rate;
  28. static int empty;
  29. static char *data_buffer, *buffer_begin, *buffer_end;
  30. static int in_use, device_major;
  31. static void dac_audio_start_timer(void)
  32. {
  33. u8 tstr;
  34. tstr = ctrl_inb(TMU_TSTR);
  35. tstr |= TMU1_TSTR_INIT;
  36. ctrl_outb(tstr, TMU_TSTR);
  37. }
  38. static void dac_audio_stop_timer(void)
  39. {
  40. u8 tstr;
  41. tstr = ctrl_inb(TMU_TSTR);
  42. tstr &= ~TMU1_TSTR_INIT;
  43. ctrl_outb(tstr, TMU_TSTR);
  44. }
  45. static void dac_audio_reset(void)
  46. {
  47. dac_audio_stop_timer();
  48. buffer_begin = buffer_end = data_buffer;
  49. empty = 1;
  50. }
  51. static void dac_audio_sync(void)
  52. {
  53. while (!empty)
  54. schedule();
  55. }
  56. static void dac_audio_start(void)
  57. {
  58. if (mach_is_hp6xx()) {
  59. u16 v = inw(HD64461_GPADR);
  60. v &= ~HD64461_GPADR_SPEAKER;
  61. outw(v, HD64461_GPADR);
  62. }
  63. sh_dac_enable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  64. ctrl_outw(TMU1_TCR_INIT, TMU1_TCR);
  65. }
  66. static void dac_audio_stop(void)
  67. {
  68. dac_audio_stop_timer();
  69. if (mach_is_hp6xx()) {
  70. u16 v = inw(HD64461_GPADR);
  71. v |= HD64461_GPADR_SPEAKER;
  72. outw(v, HD64461_GPADR);
  73. }
  74. sh_dac_disable(CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  75. }
  76. static void dac_audio_set_rate(void)
  77. {
  78. unsigned long interval;
  79. interval = (current_cpu_data.module_clock / 4) / rate;
  80. ctrl_outl(interval, TMU1_TCOR);
  81. ctrl_outl(interval, TMU1_TCNT);
  82. }
  83. static int dac_audio_ioctl(struct inode *inode, 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. file->f_flags |= O_NONBLOCK;
  102. return 0;
  103. case SNDCTL_DSP_GETCAPS:
  104. return 0;
  105. case SOUND_PCM_WRITE_RATE:
  106. val = *(int *)arg;
  107. if (val > 0) {
  108. rate = val;
  109. dac_audio_set_rate();
  110. }
  111. return put_user(rate, (int *)arg);
  112. case SNDCTL_DSP_STEREO:
  113. return put_user(0, (int *)arg);
  114. case SOUND_PCM_WRITE_CHANNELS:
  115. return put_user(1, (int *)arg);
  116. case SNDCTL_DSP_SETDUPLEX:
  117. return -EINVAL;
  118. case SNDCTL_DSP_PROFILE:
  119. return -EINVAL;
  120. case SNDCTL_DSP_GETBLKSIZE:
  121. return put_user(BUFFER_SIZE, (int *)arg);
  122. case SNDCTL_DSP_SETFRAGMENT:
  123. return 0;
  124. default:
  125. printk(KERN_ERR "sh_dac_audio: unimplemented ioctl=0x%x\n",
  126. cmd);
  127. return -EINVAL;
  128. }
  129. return -EINVAL;
  130. }
  131. static ssize_t dac_audio_write(struct file *file, const char *buf, size_t count,
  132. loff_t * ppos)
  133. {
  134. int free;
  135. int nbytes;
  136. if (count < 0)
  137. return -EINVAL;
  138. if (!count) {
  139. dac_audio_sync();
  140. return 0;
  141. }
  142. free = buffer_begin - buffer_end;
  143. if (free < 0)
  144. free += BUFFER_SIZE;
  145. if ((free == 0) && (empty))
  146. free = BUFFER_SIZE;
  147. if (count > free)
  148. count = free;
  149. if (buffer_begin > buffer_end) {
  150. if (copy_from_user((void *)buffer_end, buf, count))
  151. return -EFAULT;
  152. buffer_end += count;
  153. } else {
  154. nbytes = data_buffer + BUFFER_SIZE - buffer_end;
  155. if (nbytes > count) {
  156. if (copy_from_user((void *)buffer_end, buf, count))
  157. return -EFAULT;
  158. buffer_end += count;
  159. } else {
  160. if (copy_from_user((void *)buffer_end, buf, nbytes))
  161. return -EFAULT;
  162. if (copy_from_user
  163. ((void *)data_buffer, buf + nbytes, count - nbytes))
  164. return -EFAULT;
  165. buffer_end = data_buffer + count - nbytes;
  166. }
  167. }
  168. if (empty) {
  169. empty = 0;
  170. dac_audio_start_timer();
  171. }
  172. return count;
  173. }
  174. static ssize_t dac_audio_read(struct file *file, char *buf, size_t count,
  175. loff_t * ppos)
  176. {
  177. return -EINVAL;
  178. }
  179. static int dac_audio_open(struct inode *inode, struct file *file)
  180. {
  181. if (file->f_mode & FMODE_READ)
  182. return -ENODEV;
  183. if (in_use)
  184. return -EBUSY;
  185. in_use = 1;
  186. dac_audio_start();
  187. return 0;
  188. }
  189. static int dac_audio_release(struct inode *inode, struct file *file)
  190. {
  191. dac_audio_sync();
  192. dac_audio_stop();
  193. in_use = 0;
  194. return 0;
  195. }
  196. struct file_operations dac_audio_fops = {
  197. .read = dac_audio_read,
  198. .write = dac_audio_write,
  199. .ioctl = dac_audio_ioctl,
  200. .open = dac_audio_open,
  201. .release = dac_audio_release,
  202. };
  203. static irqreturn_t timer1_interrupt(int irq, void *dev, struct pt_regs *regs)
  204. {
  205. unsigned long timer_status;
  206. timer_status = ctrl_inw(TMU1_TCR);
  207. timer_status &= ~0x100;
  208. ctrl_outw(timer_status, TMU1_TCR);
  209. if (!empty) {
  210. sh_dac_output(*buffer_begin, CONFIG_SOUND_SH_DAC_AUDIO_CHANNEL);
  211. buffer_begin++;
  212. if (buffer_begin == data_buffer + BUFFER_SIZE)
  213. buffer_begin = data_buffer;
  214. if (buffer_begin == buffer_end) {
  215. empty = 1;
  216. dac_audio_stop_timer();
  217. }
  218. }
  219. return IRQ_HANDLED;
  220. }
  221. static int __init dac_audio_init(void)
  222. {
  223. int retval;
  224. if ((device_major = register_sound_dsp(&dac_audio_fops, -1)) < 0) {
  225. printk(KERN_ERR "Cannot register dsp device");
  226. return device_major;
  227. }
  228. in_use = 0;
  229. data_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  230. if (data_buffer == NULL)
  231. return -ENOMEM;
  232. dac_audio_reset();
  233. rate = 8000;
  234. dac_audio_set_rate();
  235. retval =
  236. request_irq(TIMER1_IRQ, timer1_interrupt, IRQF_DISABLED, MODNAME, 0);
  237. if (retval < 0) {
  238. printk(KERN_ERR "sh_dac_audio: IRQ %d request failed\n",
  239. TIMER1_IRQ);
  240. return retval;
  241. }
  242. return 0;
  243. }
  244. static void __exit dac_audio_exit(void)
  245. {
  246. free_irq(TIMER1_IRQ, 0);
  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");