sh_dac_audio.c 6.2 KB

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