sh_dac_audio.c 6.2 KB

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