sh_dac_audio.c 6.1 KB

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