radio-terratec.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* Terratec ActiveRadio ISA Standalone card driver for Linux radio support
  2. * (c) 1999 R. Offermanns (rolf@offermanns.de)
  3. * based on the aimslab radio driver from M. Kirkwood
  4. * many thanks to Michael Becker and Friedhelm Birth (from TerraTec)
  5. *
  6. *
  7. * History:
  8. * 1999-05-21 First preview release
  9. *
  10. * Notes on the hardware:
  11. * There are two "main" chips on the card:
  12. * - Philips OM5610 (http://www-us.semiconductors.philips.com/acrobat/datasheets/OM5610_2.pdf)
  13. * - Philips SAA6588 (http://www-us.semiconductors.philips.com/acrobat/datasheets/SAA6588_1.pdf)
  14. * (you can get the datasheet at the above links)
  15. *
  16. * Frequency control is done digitally -- ie out(port,encodefreq(95.8));
  17. * Volume Control is done digitally
  18. *
  19. * there is a I2C controlled RDS decoder (SAA6588) onboard, which i would like to support someday
  20. * (as soon i have understand how to get started :)
  21. * If you can help me out with that, please contact me!!
  22. *
  23. *
  24. */
  25. #include <linux/module.h> /* Modules */
  26. #include <linux/init.h> /* Initdata */
  27. #include <linux/ioport.h> /* check_region, request_region */
  28. #include <linux/delay.h> /* udelay */
  29. #include <asm/io.h> /* outb, outb_p */
  30. #include <asm/uaccess.h> /* copy to/from user */
  31. #include <linux/videodev.h> /* kernel radio structs */
  32. #include <linux/config.h> /* CONFIG_RADIO_TERRATEC_PORT */
  33. #include <linux/spinlock.h>
  34. #ifndef CONFIG_RADIO_TERRATEC_PORT
  35. #define CONFIG_RADIO_TERRATEC_PORT 0x590
  36. #endif
  37. /**************** this ones are for the terratec *******************/
  38. #define BASEPORT 0x590
  39. #define VOLPORT 0x591
  40. #define WRT_DIS 0x00
  41. #define CLK_OFF 0x00
  42. #define IIC_DATA 0x01
  43. #define IIC_CLK 0x02
  44. #define DATA 0x04
  45. #define CLK_ON 0x08
  46. #define WRT_EN 0x10
  47. /*******************************************************************/
  48. static int io = CONFIG_RADIO_TERRATEC_PORT;
  49. static int radio_nr = -1;
  50. static spinlock_t lock;
  51. struct tt_device
  52. {
  53. int port;
  54. int curvol;
  55. unsigned long curfreq;
  56. int muted;
  57. };
  58. /* local things */
  59. static void cardWriteVol(int volume)
  60. {
  61. int i;
  62. volume = volume+(volume * 32); // change both channels
  63. spin_lock(&lock);
  64. for (i=0;i<8;i++)
  65. {
  66. if (volume & (0x80>>i))
  67. outb(0x80, VOLPORT);
  68. else outb(0x00, VOLPORT);
  69. }
  70. spin_unlock(&lock);
  71. }
  72. static void tt_mute(struct tt_device *dev)
  73. {
  74. dev->muted = 1;
  75. cardWriteVol(0);
  76. }
  77. static int tt_setvol(struct tt_device *dev, int vol)
  78. {
  79. // printk(KERN_ERR "setvol called, vol = %d\n", vol);
  80. if(vol == dev->curvol) { /* requested volume = current */
  81. if (dev->muted) { /* user is unmuting the card */
  82. dev->muted = 0;
  83. cardWriteVol(vol); /* enable card */
  84. }
  85. return 0;
  86. }
  87. if(vol == 0) { /* volume = 0 means mute the card */
  88. cardWriteVol(0); /* "turn off card" by setting vol to 0 */
  89. dev->curvol = vol; /* track the volume state! */
  90. return 0;
  91. }
  92. dev->muted = 0;
  93. cardWriteVol(vol);
  94. dev->curvol = vol;
  95. return 0;
  96. }
  97. /* this is the worst part in this driver */
  98. /* many more or less strange things are going on here, but hey, it works :) */
  99. static int tt_setfreq(struct tt_device *dev, unsigned long freq1)
  100. {
  101. int freq;
  102. int i;
  103. int p;
  104. int temp;
  105. long rest;
  106. unsigned char buffer[25]; /* we have to bit shift 25 registers */
  107. freq = freq1/160; /* convert the freq. to a nice to handle value */
  108. for(i=24;i>-1;i--)
  109. buffer[i]=0;
  110. rest = freq*10+10700; /* i once had understood what is going on here */
  111. /* maybe some wise guy (friedhelm?) can comment this stuff */
  112. i=13;
  113. p=10;
  114. temp=102400;
  115. while (rest!=0)
  116. {
  117. if (rest%temp == rest)
  118. buffer[i] = 0;
  119. else
  120. {
  121. buffer[i] = 1;
  122. rest = rest-temp;
  123. }
  124. i--;
  125. p--;
  126. temp = temp/2;
  127. }
  128. spin_lock(&lock);
  129. for (i=24;i>-1;i--) /* bit shift the values to the radiocard */
  130. {
  131. if (buffer[i]==1)
  132. {
  133. outb(WRT_EN|DATA, BASEPORT);
  134. outb(WRT_EN|DATA|CLK_ON , BASEPORT);
  135. outb(WRT_EN|DATA, BASEPORT);
  136. }
  137. else
  138. {
  139. outb(WRT_EN|0x00, BASEPORT);
  140. outb(WRT_EN|0x00|CLK_ON , BASEPORT);
  141. }
  142. }
  143. outb(0x00, BASEPORT);
  144. spin_unlock(&lock);
  145. return 0;
  146. }
  147. static int tt_getsigstr(struct tt_device *dev) /* TODO */
  148. {
  149. if (inb(io) & 2) /* bit set = no signal present */
  150. return 0;
  151. return 1; /* signal present */
  152. }
  153. /* implement the video4linux api */
  154. static int tt_do_ioctl(struct inode *inode, struct file *file,
  155. unsigned int cmd, void *arg)
  156. {
  157. struct video_device *dev = video_devdata(file);
  158. struct tt_device *tt=dev->priv;
  159. switch(cmd)
  160. {
  161. case VIDIOCGCAP:
  162. {
  163. struct video_capability *v = arg;
  164. memset(v,0,sizeof(*v));
  165. v->type=VID_TYPE_TUNER;
  166. v->channels=1;
  167. v->audios=1;
  168. strcpy(v->name, "ActiveRadio");
  169. return 0;
  170. }
  171. case VIDIOCGTUNER:
  172. {
  173. struct video_tuner *v = arg;
  174. if(v->tuner) /* Only 1 tuner */
  175. return -EINVAL;
  176. v->rangelow=(87*16000);
  177. v->rangehigh=(108*16000);
  178. v->flags=VIDEO_TUNER_LOW;
  179. v->mode=VIDEO_MODE_AUTO;
  180. strcpy(v->name, "FM");
  181. v->signal=0xFFFF*tt_getsigstr(tt);
  182. return 0;
  183. }
  184. case VIDIOCSTUNER:
  185. {
  186. struct video_tuner *v = arg;
  187. if(v->tuner!=0)
  188. return -EINVAL;
  189. /* Only 1 tuner so no setting needed ! */
  190. return 0;
  191. }
  192. case VIDIOCGFREQ:
  193. {
  194. unsigned long *freq = arg;
  195. *freq = tt->curfreq;
  196. return 0;
  197. }
  198. case VIDIOCSFREQ:
  199. {
  200. unsigned long *freq = arg;
  201. tt->curfreq = *freq;
  202. tt_setfreq(tt, tt->curfreq);
  203. return 0;
  204. }
  205. case VIDIOCGAUDIO:
  206. {
  207. struct video_audio *v = arg;
  208. memset(v,0, sizeof(*v));
  209. v->flags|=VIDEO_AUDIO_MUTABLE|VIDEO_AUDIO_VOLUME;
  210. v->volume=tt->curvol * 6554;
  211. v->step=6554;
  212. strcpy(v->name, "Radio");
  213. return 0;
  214. }
  215. case VIDIOCSAUDIO:
  216. {
  217. struct video_audio *v = arg;
  218. if(v->audio)
  219. return -EINVAL;
  220. if(v->flags&VIDEO_AUDIO_MUTE)
  221. tt_mute(tt);
  222. else
  223. tt_setvol(tt,v->volume/6554);
  224. return 0;
  225. }
  226. default:
  227. return -ENOIOCTLCMD;
  228. }
  229. }
  230. static int tt_ioctl(struct inode *inode, struct file *file,
  231. unsigned int cmd, unsigned long arg)
  232. {
  233. return video_usercopy(inode, file, cmd, arg, tt_do_ioctl);
  234. }
  235. static struct tt_device terratec_unit;
  236. static struct file_operations terratec_fops = {
  237. .owner = THIS_MODULE,
  238. .open = video_exclusive_open,
  239. .release = video_exclusive_release,
  240. .ioctl = tt_ioctl,
  241. .llseek = no_llseek,
  242. };
  243. static struct video_device terratec_radio=
  244. {
  245. .owner = THIS_MODULE,
  246. .name = "TerraTec ActiveRadio",
  247. .type = VID_TYPE_TUNER,
  248. .hardware = VID_HARDWARE_TERRATEC,
  249. .fops = &terratec_fops,
  250. };
  251. static int __init terratec_init(void)
  252. {
  253. if(io==-1)
  254. {
  255. printk(KERN_ERR "You must set an I/O address with io=0x???\n");
  256. return -EINVAL;
  257. }
  258. if (!request_region(io, 2, "terratec"))
  259. {
  260. printk(KERN_ERR "TerraTec: port 0x%x already in use\n", io);
  261. return -EBUSY;
  262. }
  263. terratec_radio.priv=&terratec_unit;
  264. spin_lock_init(&lock);
  265. if(video_register_device(&terratec_radio, VFL_TYPE_RADIO, radio_nr)==-1)
  266. {
  267. release_region(io,2);
  268. return -EINVAL;
  269. }
  270. printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver.\n");
  271. /* mute card - prevents noisy bootups */
  272. /* this ensures that the volume is all the way down */
  273. cardWriteVol(0);
  274. terratec_unit.curvol = 0;
  275. return 0;
  276. }
  277. MODULE_AUTHOR("R.OFFERMANNS & others");
  278. MODULE_DESCRIPTION("A driver for the TerraTec ActiveRadio Standalone radio card.");
  279. MODULE_LICENSE("GPL");
  280. module_param(io, int, 0);
  281. MODULE_PARM_DESC(io, "I/O address of the TerraTec ActiveRadio card (0x590 or 0x591)");
  282. module_param(radio_nr, int, 0);
  283. static void __exit terratec_cleanup_module(void)
  284. {
  285. video_unregister_device(&terratec_radio);
  286. release_region(io,2);
  287. printk(KERN_INFO "TERRATEC ActivRadio Standalone card driver unloaded.\n");
  288. }
  289. module_init(terratec_init);
  290. module_exit(terratec_cleanup_module);